(gnus-summary-goto-subjects): Fix typo.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-make-false-root-always t
113   "Always make a false dummy root."
114   :group 'gnus-thread
115   :type 'boolean)
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include:
145 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
146 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 \(they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-default-high-score 0
211   "*Default threshold for a high scored article.
212 An article will be highlighted as high scored if its score is greater
213 than this score."
214   :group 'gnus-score-default
215   :type 'integer)
216
217 (defcustom gnus-summary-default-low-score 0
218   "*Default threshold for a low scored article.
219 An article will be highlighted as low scored if its score is smaller
220 than this score."
221   :group 'gnus-score-default
222   :type 'integer)
223
224 (defcustom gnus-summary-zcore-fuzz 0
225   "*Fuzziness factor for the zcore in the summary buffer.
226 Articles with scores closer than this to `gnus-summary-default-score'
227 will not be marked."
228   :group 'gnus-summary-format
229   :type 'integer)
230
231 (defcustom gnus-simplify-subject-fuzzy-regexp nil
232   "*Strings to be removed when doing fuzzy matches.
233 This can either be a regular expression or list of regular expressions
234 that will be removed from subject strings if fuzzy subject
235 simplification is selected."
236   :group 'gnus-thread
237   :type '(repeat regexp))
238
239 (defcustom gnus-show-threads t
240   "*If non-nil, display threads in summary mode."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-subtree nil
245   "*If non-nil, hide all threads initially.
246 This can be a predicate specifier which says which threads to hide.
247 If threads are hidden, you have to run the command
248 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
249 to expose hidden threads."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-hide-killed t
254   "*If non-nil, hide killed threads automatically."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-ignore-subject t
259   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
260 If nil, articles that have different subjects from their parents will
261 start separate threads."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-operation-ignore-subject t
266   "*If non-nil, subjects will be ignored when doing thread commands.
267 This affects commands like `gnus-summary-kill-thread' and
268 `gnus-summary-lower-thread'.
269
270 If this variable is nil, articles in the same thread with different
271 subjects will not be included in the operation in question.  If this
272 variable is `fuzzy', only articles that have subjects that are fuzzily
273 equal will be included."
274   :group 'gnus-thread
275   :type '(choice (const :tag "off" nil)
276                  (const fuzzy)
277                  (sexp :tag "on" t)))
278
279 (defcustom gnus-thread-indent-level 4
280   "*Number that says how much each sub-thread should be indented."
281   :group 'gnus-thread
282   :type 'integer)
283
284 (defcustom gnus-auto-extend-newsgroup t
285   "*If non-nil, extend newsgroup forward and backward when requested."
286   :group 'gnus-summary-choose
287   :type 'boolean)
288
289 (defcustom gnus-auto-select-first t
290   "*If non-nil, select the article under point.
291 Which article this is is controlled by the `gnus-auto-select-subject'
292 variable.
293
294 If you want to prevent automatic selection of articles in some
295 newsgroups, set the variable to nil in `gnus-select-group-hook'."
296   :group 'gnus-group-select
297   :type '(choice (const :tag "none" nil)
298                  (sexp :menu-tag "first" t)))
299
300 (defcustom gnus-auto-select-subject 'unread
301   "*Says what subject to place under point when entering a group.
302
303 This variable can either be the symbols `first' (place point on the
304 first subject), `unread' (place point on the subject line of the first
305 unread article), `best' (place point on the subject line of the
306 higest-scored article), `unseen' (place point on the subject line of
307 the first unseen article), 'unseen-or-unread' (place point on the subject
308 line of the first unseen article or, if all article have been seen, on the
309 subject line of the first unread article), or a function to be called to
310 place point on some subject line."
311   :group 'gnus-group-select
312   :type '(choice (const best)
313                  (const unread)
314                  (const first)
315                  (const unseen)
316                  (const unseen-or-unread)))
317
318 (defcustom gnus-auto-select-next t
319   "*If non-nil, offer to go to the next group from the end of the previous.
320 If the value is t and the next newsgroup is empty, Gnus will exit
321 summary mode and go back to group mode.  If the value is neither nil
322 nor t, Gnus will select the following unread newsgroup.  In
323 particular, if the value is the symbol `quietly', the next unread
324 newsgroup will be selected without any confirmation, and if it is
325 `almost-quietly', the next group will be selected without any
326 confirmation if you are located on the last article in the group.
327 Finally, if this variable is `slightly-quietly', the `Z n' command
328 will go to the next group without confirmation."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "off" nil)
331                  (const quietly)
332                  (const almost-quietly)
333                  (const slightly-quietly)
334                  (sexp :menu-tag "on" t)))
335
336 (defcustom gnus-auto-select-same nil
337   "*If non-nil, select the next article with the same subject.
338 If there are no more articles with the same subject, go to
339 the first unread article."
340   :group 'gnus-summary-maneuvering
341   :type 'boolean)
342
343 (defcustom gnus-summary-check-current nil
344   "*If non-nil, consider the current article when moving.
345 The \"unread\" movement commands will stay on the same line if the
346 current article is unread."
347   :group 'gnus-summary-maneuvering
348   :type 'boolean)
349
350 (defcustom gnus-auto-center-summary t
351   "*If non-nil, always center the current summary buffer.
352 In particular, if `vertical' do only vertical recentering.  If non-nil
353 and non-`vertical', do both horizontal and vertical recentering."
354   :group 'gnus-summary-maneuvering
355   :type '(choice (const :tag "none" nil)
356                  (const vertical)
357                  (integer :tag "height")
358                  (sexp :menu-tag "both" t)))
359
360 (defcustom gnus-show-all-headers nil
361   "*If non-nil, don't hide any headers."
362   :group 'gnus-article-hiding
363   :group 'gnus-article-headers
364   :type 'boolean)
365
366 (defcustom gnus-summary-ignore-duplicates nil
367   "*If non-nil, ignore articles with identical Message-ID headers."
368   :group 'gnus-summary
369   :type 'boolean)
370
371 (defcustom gnus-single-article-buffer t
372   "*If non-nil, display all articles in the same buffer.
373 If nil, each group will get its own article buffer."
374   :group 'gnus-article-various
375   :type 'boolean)
376
377 (defcustom gnus-break-pages t
378   "*If non-nil, do page breaking on articles.
379 The page delimiter is specified by the `gnus-page-delimiter'
380 variable."
381   :group 'gnus-article-various
382   :type 'boolean)
383
384 (defcustom gnus-move-split-methods nil
385   "*Variable used to suggest where articles are to be moved to.
386 It uses the same syntax as the `gnus-split-methods' variable.
387 However, whereas `gnus-split-methods' specifies file names as targets,
388 this variable specifies group names."
389   :group 'gnus-summary-mail
390   :type '(repeat (choice (list :value (fun) function)
391                          (cons :value ("" "") regexp (repeat string))
392                          (sexp :value nil))))
393
394 (defcustom gnus-unread-mark ?           ;Whitespace
395   "*Mark used for unread articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-ticked-mark ?!
400   "*Mark used for ticked articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-dormant-mark ??
405   "*Mark used for dormant articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-del-mark ?r
410   "*Mark used for del'd articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-read-mark ?R
415   "*Mark used for read articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-expirable-mark ?E
420   "*Mark used for expirable articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-killed-mark ?K
425   "*Mark used for killed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-spam-mark ?H
430   "*Mark used for spam articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-souped-mark ?F
435   "*Mark used for souped articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-kill-file-mark ?X
440   "*Mark used for articles killed by kill files."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-low-score-mark ?Y
445   "*Mark used for articles with a low score."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-catchup-mark ?C
450   "*Mark used for articles that are caught up."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-replied-mark ?A
455   "*Mark used for articles that have been replied to."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-forwarded-mark ?F
460   "*Mark used for articles that have been forwarded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-recent-mark ?N
465   "*Mark used for articles that are recent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-cached-mark ?*
470   "*Mark used for articles that are in the cache."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-saved-mark ?S
475   "*Mark used for articles that have been saved."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unseen-mark ?.
480   "*Mark used for articles that haven't been seen."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-no-mark ?               ;Whitespace
485   "*Mark used for articles that have no other secondary mark."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-ancient-mark ?O
490   "*Mark used for ancient articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-sparse-mark ?Q
495   "*Mark used for sparsely reffed articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-canceled-mark ?G
500   "*Mark used for canceled articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-duplicate-mark ?M
505   "*Mark used for duplicate articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-undownloaded-mark ?-
510   "*Mark used for articles that weren't downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-downloaded-mark ?+
515   "*Mark used for articles that were downloaded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-downloadable-mark ?%
520   "*Mark used for articles that are to be downloaded."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-unsendable-mark ?=
525   "*Mark used for articles that won't be sent."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-score-over-mark ?+
530   "*Score mark used for articles with high scores."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-score-below-mark ?-
535   "*Score mark used for articles with low scores."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-empty-thread-mark ?     ;Whitespace
540   "*There is no thread under the article."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-not-empty-thread-mark ?=
545   "*There is a thread under the article."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-view-pseudo-asynchronously nil
550   "*If non-nil, Gnus will view pseudo-articles asynchronously."
551   :group 'gnus-extract-view
552   :type 'boolean)
553
554 (defcustom gnus-auto-expirable-marks
555   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
556         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
557         gnus-souped-mark gnus-duplicate-mark)
558   "*The list of marks converted into expiration if a group is auto-expirable."
559   :version "21.1"
560   :group 'gnus-summary
561   :type '(repeat character))
562
563 (defcustom gnus-inhibit-user-auto-expire t
564   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
565   :version "21.1"
566   :group 'gnus-summary
567   :type 'boolean)
568
569 (defcustom gnus-view-pseudos nil
570   "*If `automatic', pseudo-articles will be viewed automatically.
571 If `not-confirm', pseudos will be viewed automatically, and the user
572 will not be asked to confirm the command."
573   :group 'gnus-extract-view
574   :type '(choice (const :tag "off" nil)
575                  (const automatic)
576                  (const not-confirm)))
577
578 (defcustom gnus-view-pseudos-separately t
579   "*If non-nil, one pseudo-article will be created for each file to be viewed.
580 If nil, all files that use the same viewing command will be given as a
581 list of parameters to that command."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-insert-pseudo-articles t
586   "*If non-nil, insert pseudo-articles when decoding articles."
587   :group 'gnus-extract-view
588   :type 'boolean)
589
590 (defcustom gnus-summary-dummy-line-format
591   "   %(:                             :%) %S\n"
592   "*The format specification for the dummy roots in the summary buffer.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions.
595
596 %S  The subject
597
598 General format specifiers can also be used.
599 See `(gnus)Formatting Variables'."
600   :link '(custom-manual "(gnus)Formatting Variables")
601   :group 'gnus-threading
602   :type 'string)
603
604 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
605   "*The format specification for the summary mode line.
606 It works along the same lines as a normal formatting string,
607 with some simple extensions:
608
609 %G  Group name
610 %p  Unprefixed group name
611 %A  Current article number
612 %z  Current article score
613 %V  Gnus version
614 %U  Number of unread articles in the group
615 %e  Number of unselected articles in the group
616 %Z  A string with unread/unselected article counts
617 %g  Shortish group name
618 %S  Subject of the current article
619 %u  User-defined spec
620 %s  Current score file name
621 %d  Number of dormant articles
622 %r  Number of articles that have been marked as read in this session
623 %E  Number of articles expunged by the score files"
624   :group 'gnus-summary-format
625   :type 'string)
626
627 (defcustom gnus-list-identifiers nil
628   "Regexp that matches list identifiers to be removed from subject.
629 This can also be a list of regexps."
630   :version "21.1"
631   :group 'gnus-summary-format
632   :group 'gnus-article-hiding
633   :type '(choice (const :tag "none" nil)
634                  (regexp :value ".*")
635                  (repeat :value (".*") regexp)))
636
637 (defcustom gnus-summary-mark-below 0
638   "*Mark all articles with a score below this variable as read.
639 This variable is local to each summary buffer and usually set by the
640 score file."
641   :group 'gnus-score-default
642   :type 'integer)
643
644 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
645   "*List of functions used for sorting articles in the summary buffer.
646
647 Each function takes two articles and returns non-nil if the first
648 article should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-article-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-article-sort-by-number',
657 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
658 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
659 and `gnus-article-sort-by-score'.
660
661 When threading is turned on, the variable `gnus-thread-sort-functions'
662 controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-article-sort-by-number)
665                          (function-item gnus-article-sort-by-author)
666                          (function-item gnus-article-sort-by-subject)
667                          (function-item gnus-article-sort-by-date)
668                          (function-item gnus-article-sort-by-score)
669                          (function-item gnus-article-sort-by-random)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
673   "*List of functions used for sorting threads in the summary buffer.
674 By default, threads are sorted by article number.
675
676 Each function takes two threads and returns non-nil if the first
677 thread should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-thread-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-thread-sort-by-number',
686 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
687 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
688 `gnus-thread-sort-by-most-recent-number',
689 `gnus-thread-sort-by-most-recent-date',
690 `gnus-thread-sort-by-random', and
691 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
692
693 When threading is turned off, the variable
694 `gnus-article-sort-functions' controls how articles are sorted."
695   :group 'gnus-summary-sort
696   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
697                          (function-item gnus-thread-sort-by-author)
698                          (function-item gnus-thread-sort-by-subject)
699                          (function-item gnus-thread-sort-by-date)
700                          (function-item gnus-thread-sort-by-score)
701                          (function-item gnus-thread-sort-by-total-score)
702                          (function-item gnus-thread-sort-by-random)
703                          (function :tag "other"))))
704
705 (defcustom gnus-thread-score-function '+
706   "*Function used for calculating the total score of a thread.
707
708 The function is called with the scores of the article and each
709 subthread and should then return the score of the thread.
710
711 Some functions you can use are `+', `max', or `min'."
712   :group 'gnus-summary-sort
713   :type 'function)
714
715 (defcustom gnus-summary-expunge-below nil
716   "All articles that have a score less than this variable will be expunged.
717 This variable is local to the summary buffers."
718   :group 'gnus-score-default
719   :type '(choice (const :tag "off" nil)
720                  integer))
721
722 (defcustom gnus-thread-expunge-below nil
723   "All threads that have a total score less than this variable will be expunged.
724 See `gnus-thread-score-function' for en explanation of what a
725 \"thread score\" is.
726
727 This variable is local to the summary buffers."
728   :group 'gnus-threading
729   :group 'gnus-score-default
730   :type '(choice (const :tag "off" nil)
731                  integer))
732
733 (defcustom gnus-summary-mode-hook nil
734   "*A hook for Gnus summary mode.
735 This hook is run before any variables are set in the summary buffer."
736   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
737   :group 'gnus-summary-various
738   :type 'hook)
739
740 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
741 (when (featurep 'xemacs)
742   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
743   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
744   (add-hook 'gnus-summary-mode-hook
745             'gnus-xmas-switch-horizontal-scrollbar-off))
746
747 (defcustom gnus-summary-menu-hook nil
748   "*Hook run after the creation of the summary mode menu."
749   :group 'gnus-summary-visual
750   :type 'hook)
751
752 (defcustom gnus-summary-exit-hook nil
753   "*A hook called on exit from the summary buffer.
754 It will be called with point in the group buffer."
755   :group 'gnus-summary-exit
756   :type 'hook)
757
758 (defcustom gnus-summary-prepare-hook nil
759   "*A hook called after the summary buffer has been generated.
760 If you want to modify the summary buffer, you can use this hook."
761   :group 'gnus-summary-various
762   :type 'hook)
763
764 (defcustom gnus-summary-prepared-hook nil
765   "*A hook called as the last thing after the summary buffer has been generated."
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 (defcustom gnus-summary-generate-hook nil
770   "*A hook run just before generating the summary buffer.
771 This hook is commonly used to customize threading variables and the
772 like."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-select-group-hook nil
777   "*A hook called when a newsgroup is selected.
778
779 If you'd like to simplify subjects like the
780 `gnus-summary-next-same-subject' command does, you can use the
781 following hook:
782
783  (add-hook gnus-select-group-hook
784            (lambda ()
785              (mapcar (lambda (header)
786                        (mail-header-set-subject
787                         header
788                         (gnus-simplify-subject
789                          (mail-header-subject header) 're-only)))
790                      gnus-newsgroup-headers)))"
791   :group 'gnus-group-select
792   :type 'hook)
793
794 (defcustom gnus-select-article-hook nil
795   "*A hook called when an article is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-visual-mark-article-hook
800   (list 'gnus-highlight-selected-summary)
801   "*Hook run after selecting an article in the summary buffer.
802 It is meant to be used for highlighting the article in some way.  It
803 is not run if `gnus-visual' is nil."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-parse-headers-hook nil
808   "*A hook called before parsing the headers."
809   :group 'gnus-various
810   :type 'hook)
811
812 (defcustom gnus-exit-group-hook nil
813   "*A hook called when exiting summary mode.
814 This hook is not called from the non-updating exit commands like `Q'."
815   :group 'gnus-various
816   :type 'hook)
817
818 (defcustom gnus-summary-update-hook
819   (list 'gnus-summary-highlight-line)
820   "*A hook called when a summary line is changed.
821 The hook will not be called if `gnus-visual' is nil.
822
823 The default function `gnus-summary-highlight-line' will
824 highlight the line according to the `gnus-summary-highlight'
825 variable."
826   :group 'gnus-summary-visual
827   :type 'hook)
828
829 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
830   "*A hook called when an article is selected for the first time.
831 The hook is intended to mark an article as read (or unread)
832 automatically when it is selected."
833   :group 'gnus-summary-choose
834   :type 'hook)
835
836 (defcustom gnus-group-no-more-groups-hook nil
837   "*A hook run when returning to group mode having no more (unread) groups."
838   :group 'gnus-group-select
839   :type 'hook)
840
841 (defcustom gnus-ps-print-hook nil
842   "*A hook run before ps-printing something from Gnus."
843   :group 'gnus-summary
844   :type 'hook)
845
846 (defcustom gnus-summary-display-arrow
847   (and (fboundp 'display-graphic-p)
848        (display-graphic-p))
849   "*If non-nil, display an arrow highlighting the current article."
850   :version "21.1"
851   :group 'gnus-summary
852   :type 'boolean)
853
854 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
855   "Face used for highlighting the current article in the summary buffer."
856   :group 'gnus-summary-visual
857   :type 'face)
858
859 (defvar gnus-tmp-downloaded nil)
860
861 (defcustom gnus-summary-highlight
862   '(((eq mark gnus-canceled-mark)
863      . gnus-summary-cancelled-face)
864     ((and uncached (> score default-high))
865      . gnus-summary-high-uncached-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-uncached-face)
868     (uncached
869      . gnus-summary-normal-uncached-face)
870     ((and (> score default-high)
871           (or (eq mark gnus-dormant-mark)
872               (eq mark gnus-ticked-mark)))
873      . gnus-summary-high-ticked-face)
874     ((and (< score default-low)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-low-ticked-face)
878     ((or (eq mark gnus-dormant-mark)
879          (eq mark gnus-ticked-mark))
880      . gnus-summary-normal-ticked-face)
881     ((and (> score default-high) (eq mark gnus-ancient-mark))
882      . gnus-summary-high-ancient-face)
883     ((and (< score default-low) (eq mark gnus-ancient-mark))
884      . gnus-summary-low-ancient-face)
885     ((eq mark gnus-ancient-mark)
886      . gnus-summary-normal-ancient-face)
887     ((and (> score default-high) (eq mark gnus-unread-mark))
888      . gnus-summary-high-unread-face)
889     ((and (< score default-low) (eq mark gnus-unread-mark))
890      . gnus-summary-low-unread-face)
891     ((eq mark gnus-unread-mark)
892      . gnus-summary-normal-unread-face)
893     ((> score default-high)
894      . gnus-summary-high-read-face)
895     ((< score default-low)
896      . gnus-summary-low-read-face)
897     (t
898      . gnus-summary-normal-read-face))
899   "*Controls the highlighting of summary buffer lines.
900
901 A list of (FORM . FACE) pairs.  When deciding how a a particular
902 summary line should be displayed, each form is evaluated.  The content
903 of the face field after the first true form is used.  You can change
904 how those summary lines are displayed, by editing the face field.
905
906 You can use the following variables in the FORM field.
907
908 score:        The article's score
909 default:      The default article score.
910 default-high: The default score for high scored articles.
911 default-low:  The default score for low scored articles.
912 below:        The score below which articles are automatically marked as read.
913 mark:         The articles mark."
914   :group 'gnus-summary-visual
915   :type '(repeat (cons (sexp :tag "Form" nil)
916                        face)))
917
918 (defcustom gnus-alter-header-function nil
919   "Function called to allow alteration of article header structures.
920 The function is called with one parameter, the article header vector,
921 which it may alter in any way.")
922
923 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
924   "Variable that says which function should be used to decode a string with encoded words.")
925
926 (defcustom gnus-extra-headers '(To Newsgroups)
927   "*Extra headers to parse."
928   :version "21.1"
929   :group 'gnus-summary
930   :type '(repeat symbol))
931
932 (defcustom gnus-ignored-from-addresses
933   (and user-mail-address (regexp-quote user-mail-address))
934   "*Regexp of From headers that may be suppressed in favor of To headers."
935   :version "21.1"
936   :group 'gnus-summary
937   :type 'regexp)
938
939 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
940   "List of charsets that should be ignored.
941 When these charsets are used in the \"charset\" parameter, the
942 default charset will be used instead."
943   :version "21.1"
944   :type '(repeat symbol)
945   :group 'gnus-charset)
946
947 (gnus-define-group-parameter
948  ignored-charsets
949  :type list
950  :function-document
951  "Return the ignored charsets of GROUP."
952  :variable gnus-group-ignored-charsets-alist
953  :variable-default
954  '(("alt\\.chinese\\.text" iso-8859-1))
955  :variable-document
956  "Alist of regexps (to match group names) and charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959  :variable-group gnus-charset
960  :variable-type '(repeat (cons (regexp :tag "Group")
961                                (repeat symbol)))
962  :parameter-type '(choice :tag "Ignored charsets"
963                           :value nil
964                           (repeat (symbol)))
965  :parameter-document       "\
966 List of charsets that should be ignored.
967
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead.")
970
971 (defcustom gnus-group-highlight-words-alist nil
972   "Alist of group regexps and highlight regexps.
973 This variable uses the same syntax as `gnus-emphasis-alist'."
974   :version "21.1"
975   :type '(repeat (cons (regexp :tag "Group")
976                        (repeat (list (regexp :tag "Highlight regexp")
977                                      (number :tag "Group for entire word" 0)
978                                      (number :tag "Group for displayed part" 0)
979                                      (symbol :tag "Face"
980                                              gnus-emphasis-highlight-words)))))
981   :group 'gnus-summary-visual)
982
983 (defcustom gnus-summary-show-article-charset-alist
984   nil
985   "Alist of number and charset.
986 The article will be shown with the charset corresponding to the
987 numbered argument.
988 For example: ((1 . cn-gb-2312) (2 . big5))."
989   :version "21.1"
990   :type '(repeat (cons (number :tag "Argument" 1)
991                        (symbol :tag "Charset")))
992   :group 'gnus-charset)
993
994 (defcustom gnus-preserve-marks t
995   "Whether marks are preserved when moving, copying and respooling messages."
996   :version "21.1"
997   :type 'boolean
998   :group 'gnus-summary-marks)
999
1000 (defcustom gnus-alter-articles-to-read-function nil
1001   "Function to be called to alter the list of articles to be selected."
1002   :type '(choice (const nil) function)
1003   :group 'gnus-summary)
1004
1005 (defcustom gnus-orphan-score nil
1006   "*All orphans get this score added.  Set in the score file."
1007   :group 'gnus-score-default
1008   :type '(choice (const nil)
1009                  integer))
1010
1011 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1012   "*A regexp to match MIME parts when saving multiple parts of a message
1013 with gnus-summary-save-parts (X m). This regexp will be used by default
1014 when prompting the user for which type of files to save."
1015   :group 'gnus-summary
1016   :type 'regexp)
1017
1018 (defcustom gnus-read-all-available-headers nil
1019   "Whether Gnus should parse all headers made available to it.
1020 This is mostly relevant for slow backends where the user may
1021 wish to widen the summary buffer to include all headers
1022 that were fetched.  Say, for nnultimate groups."
1023   :group 'gnus-summary
1024   :type '(choice boolean regexp))
1025
1026 (defcustom gnus-summary-muttprint-program "muttprint"
1027   "Command (and optional arguments) used to run Muttprint."
1028   :version "21.3"
1029   :group 'gnus-summary
1030   :type 'string)
1031
1032 (defcustom gnus-article-loose-mime nil
1033   "If non-nil, don't require MIME-Version header.
1034 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1035 supply the MIME-Version header or deliberately strip it From the mail.
1036 Set it to non-nil, Gnus will treat some articles as MIME even if
1037 the MIME-Version header is missed."
1038   :version "21.3"
1039   :type 'boolean
1040   :group 'gnus-article)
1041
1042 ;;; Internal variables
1043
1044 (defvar gnus-summary-display-cache nil)
1045 (defvar gnus-article-mime-handles nil)
1046 (defvar gnus-article-decoded-p nil)
1047 (defvar gnus-article-charset nil)
1048 (defvar gnus-article-ignored-charsets nil)
1049 (defvar gnus-scores-exclude-files nil)
1050 (defvar gnus-page-broken nil)
1051
1052 (defvar gnus-original-article nil)
1053 (defvar gnus-article-internal-prepare-hook nil)
1054 (defvar gnus-newsgroup-process-stack nil)
1055
1056 (defvar gnus-thread-indent-array nil)
1057 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1058 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1059   "Function called to sort the articles within a thread after it has been gathered together.")
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073 (defvar gnus-newsgroup-display nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1087         ?s)
1088     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1099     (?L gnus-tmp-lines ?s)
1100     (?O gnus-tmp-downloaded ?c)
1101     (?I gnus-tmp-indentation ?s)
1102     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1103     (?R gnus-tmp-replied ?c)
1104     (?\[ gnus-tmp-opening-bracket ?c)
1105     (?\] gnus-tmp-closing-bracket ?c)
1106     (?\> (make-string gnus-tmp-level ? ) ?s)
1107     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1108     (?i gnus-tmp-score ?d)
1109     (?z gnus-tmp-score-char ?c)
1110     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1111     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1112     (?U gnus-tmp-unread ?c)
1113     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1114         ?s)
1115     (?t (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1117         ?d)
1118     (?e (gnus-summary-number-of-articles-in-thread
1119          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1120         ?c)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?P (gnus-pick-line-number) ?d)
1123     (?B gnus-tmp-thread-tree-header-string ?s)
1124     (user-date (gnus-user-date
1125                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1126   "An alist of format specifications that can appear in summary lines.
1127 These are paired with what variables they correspond with, along with
1128 the type of the variable (string, integer, character, etc).")
1129
1130 (defvar gnus-summary-dummy-line-format-alist
1131   `((?S gnus-tmp-subject ?s)
1132     (?N gnus-tmp-number ?d)
1133     (?u gnus-tmp-user-defined ?s)))
1134
1135 (defvar gnus-summary-mode-line-format-alist
1136   `((?G gnus-tmp-group-name ?s)
1137     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1138     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1139     (?A gnus-tmp-article-number ?d)
1140     (?Z gnus-tmp-unread-and-unselected ?s)
1141     (?V gnus-version ?s)
1142     (?U gnus-tmp-unread-and-unticked ?d)
1143     (?S gnus-tmp-subject ?s)
1144     (?e gnus-tmp-unselected ?d)
1145     (?u gnus-tmp-user-defined ?s)
1146     (?d (length gnus-newsgroup-dormant) ?d)
1147     (?t (length gnus-newsgroup-marked) ?d)
1148     (?h (length gnus-newsgroup-spam-marked) ?d)
1149     (?r (length gnus-newsgroup-reads) ?d)
1150     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1151     (?E gnus-newsgroup-expunged-tally ?d)
1152     (?s (gnus-current-score-file-nondirectory) ?s)))
1153
1154 (defvar gnus-last-search-regexp nil
1155   "Default regexp for article search command.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-agentized nil
1161   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1162 (defvar gnus-newsgroup-begin nil)
1163 (defvar gnus-newsgroup-end nil)
1164 (defvar gnus-newsgroup-last-rmail nil)
1165 (defvar gnus-newsgroup-last-mail nil)
1166 (defvar gnus-newsgroup-last-folder nil)
1167 (defvar gnus-newsgroup-last-file nil)
1168 (defvar gnus-newsgroup-auto-expire nil)
1169 (defvar gnus-newsgroup-active nil)
1170
1171 (defvar gnus-newsgroup-data nil)
1172 (defvar gnus-newsgroup-data-reverse nil)
1173 (defvar gnus-newsgroup-limit nil)
1174 (defvar gnus-newsgroup-limits nil)
1175
1176 (defvar gnus-newsgroup-unreads nil
1177   "Sorted list of unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-unselected nil
1180   "Sorted list of unselected unread articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-reads nil
1183   "Alist of read articles and article marks in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-expunged-tally nil)
1186
1187 (defvar gnus-newsgroup-marked nil
1188   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1189
1190 (defvar gnus-newsgroup-spam-marked nil
1191   "List of ranges of articles that have been marked as spam.")
1192
1193 (defvar gnus-newsgroup-killed nil
1194   "List of ranges of articles that have been through the scoring process.")
1195
1196 (defvar gnus-newsgroup-cached nil
1197   "Sorted list of articles that come from the article cache.")
1198
1199 (defvar gnus-newsgroup-saved nil
1200   "List of articles that have been saved.")
1201
1202 (defvar gnus-newsgroup-kill-headers nil)
1203
1204 (defvar gnus-newsgroup-replied nil
1205   "List of articles that have been replied to in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-forwarded nil
1208   "List of articles that have been forwarded in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-recent nil
1211   "List of articles that have are recent in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-expirable nil
1214   "Sorted list of articles in the current newsgroup that can be expired.")
1215
1216 (defvar gnus-newsgroup-processable nil
1217   "List of articles in the current newsgroup that can be processed.")
1218
1219 (defvar gnus-newsgroup-downloadable nil
1220   "Sorted list of articles in the current newsgroup that can be processed.")
1221
1222 (defvar gnus-newsgroup-undownloaded nil
1223   "List of articles in the current newsgroup that haven't been downloaded..")
1224
1225 (defvar gnus-newsgroup-unsendable nil
1226   "List of articles in the current newsgroup that won't be sent.")
1227
1228 (defvar gnus-newsgroup-bookmarks nil
1229   "List of articles in the current newsgroup that have bookmarks.")
1230
1231 (defvar gnus-newsgroup-dormant nil
1232   "Sorted list of dormant articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-unseen nil
1235   "List of unseen articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-seen nil
1238   "Range of seen articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-articles nil
1241   "List of articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-scored nil
1244   "List of scored articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-headers nil
1247   "List of article headers in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-threads nil)
1250
1251 (defvar gnus-newsgroup-prepared nil
1252   "Whether the current group has been prepared properly.")
1253
1254 (defvar gnus-newsgroup-ancient nil
1255   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-sparse nil)
1258
1259 (defvar gnus-current-article nil)
1260 (defvar gnus-article-current nil)
1261 (defvar gnus-current-headers nil)
1262 (defvar gnus-have-all-headers nil)
1263 (defvar gnus-last-article nil)
1264 (defvar gnus-newsgroup-history nil)
1265 (defvar gnus-newsgroup-charset nil)
1266 (defvar gnus-newsgroup-ephemeral-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1268
1269 (defvar gnus-article-before-search nil)
1270
1271 (defconst gnus-summary-local-variables
1272   '(gnus-newsgroup-name
1273     gnus-newsgroup-begin gnus-newsgroup-end
1274     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1275     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1276     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1277     gnus-newsgroup-unselected gnus-newsgroup-marked
1278     gnus-newsgroup-spam-marked
1279     gnus-newsgroup-reads gnus-newsgroup-saved
1280     gnus-newsgroup-replied gnus-newsgroup-forwarded
1281     gnus-newsgroup-recent
1282     gnus-newsgroup-expirable
1283     gnus-newsgroup-processable gnus-newsgroup-killed
1284     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1285     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1286     gnus-newsgroup-seen gnus-newsgroup-articles
1287     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1288     gnus-newsgroup-headers gnus-newsgroup-threads
1289     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1290     gnus-current-article gnus-current-headers gnus-have-all-headers
1291     gnus-last-article gnus-article-internal-prepare-hook
1292     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1293     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1294     gnus-thread-expunge-below
1295     gnus-score-alist gnus-current-score-file
1296     (gnus-summary-expunge-below . global)
1297     (gnus-summary-mark-below . global)
1298     (gnus-orphan-score . global)
1299     gnus-newsgroup-active gnus-scores-exclude-files
1300     gnus-newsgroup-history gnus-newsgroup-ancient
1301     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1302     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1303     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1304     (gnus-newsgroup-expunged-tally . 0)
1305     gnus-cache-removable-articles gnus-newsgroup-cached
1306     gnus-newsgroup-data gnus-newsgroup-data-reverse
1307     gnus-newsgroup-limit gnus-newsgroup-limits
1308     gnus-newsgroup-charset gnus-newsgroup-display)
1309   "Variables that are buffer-local to the summary buffers.")
1310
1311 (defvar gnus-newsgroup-variables nil
1312   "A list of variables that have separate values in different newsgroups.
1313 A list of newsgroup (summary buffer) local variables, or cons of
1314 variables and their default values (when the default values are not
1315 nil), that should be made global while the summary buffer is active.
1316 These variables can be used to set variables in the group parameters
1317 while still allowing them to affect operations done in other
1318 buffers. For example:
1319
1320 \(setq gnus-newsgroup-variables
1321      '(message-use-followup-to
1322        (gnus-visible-headers .
1323          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1324 ")
1325
1326 ;; Byte-compiler warning.
1327 (eval-when-compile (defvar gnus-article-mode-map))
1328
1329 ;; MIME stuff.
1330
1331 (defvar gnus-decode-encoded-word-methods
1332   '(mail-decode-encoded-word-string)
1333   "List of methods used to decode encoded words.
1334
1335 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1336 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1337 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1338 whose names match REGEXP.
1339
1340 For example:
1341 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1342  mail-decode-encoded-word-string
1343  (\"chinese\" . rfc1843-decode-string))")
1344
1345 (defvar gnus-decode-encoded-word-methods-cache nil)
1346
1347 (defun gnus-multi-decode-encoded-word-string (string)
1348   "Apply the functions from `gnus-encoded-word-methods' that match."
1349   (unless (and gnus-decode-encoded-word-methods-cache
1350                (eq gnus-newsgroup-name
1351                    (car gnus-decode-encoded-word-methods-cache)))
1352     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1353     (mapcar (lambda (x)
1354               (if (symbolp x)
1355                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1356                 (if (and gnus-newsgroup-name
1357                          (string-match (car x) gnus-newsgroup-name))
1358                     (nconc gnus-decode-encoded-word-methods-cache
1359                            (list (cdr x))))))
1360             gnus-decode-encoded-word-methods))
1361   (let ((xlist gnus-decode-encoded-word-methods-cache))
1362     (pop xlist)
1363     (while xlist
1364       (setq string (funcall (pop xlist) string))))
1365   string)
1366
1367 ;; Subject simplification.
1368
1369 (defun gnus-simplify-whitespace (str)
1370   "Remove excessive whitespace from STR."
1371   (let ((mystr str))
1372     ;; Multiple spaces.
1373     (while (string-match "[ \t][ \t]+" mystr)
1374       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1375                           " "
1376                           (substring mystr (match-end 0)))))
1377     ;; Leading spaces.
1378     (when (string-match "^[ \t]+" mystr)
1379       (setq mystr (substring mystr (match-end 0))))
1380     ;; Trailing spaces.
1381     (when (string-match "[ \t]+$" mystr)
1382       (setq mystr (substring mystr 0 (match-beginning 0))))
1383     mystr))
1384
1385 (defun gnus-simplify-all-whitespace (str)
1386   "Remove all whitespace from STR."
1387   (let ((mystr str))
1388     (while (string-match "[ \t\n]+" mystr)
1389       (setq mystr (replace-match "" nil nil mystr)))
1390     mystr))
1391
1392 (defsubst gnus-simplify-subject-re (subject)
1393   "Remove \"Re:\" from subject lines."
1394   (if (string-match message-subject-re-regexp subject)
1395       (substring subject (match-end 0))
1396     subject))
1397
1398 (defun gnus-simplify-subject (subject &optional re-only)
1399   "Remove `Re:' and words in parentheses.
1400 If RE-ONLY is non-nil, strip leading `Re:'s only."
1401   (let ((case-fold-search t))           ;Ignore case.
1402     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1403     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1404       (setq subject (substring subject (match-end 0))))
1405     ;; Remove uninteresting prefixes.
1406     (when (and (not re-only)
1407                gnus-simplify-ignored-prefixes
1408                (string-match gnus-simplify-ignored-prefixes subject))
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove words in parentheses from end.
1411     (unless re-only
1412       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1413         (setq subject (substring subject 0 (match-beginning 0)))))
1414     ;; Return subject string.
1415     subject))
1416
1417 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1418 ;; all whitespace.
1419 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1420   (goto-char (point-min))
1421   (while (re-search-forward regexp nil t)
1422     (replace-match (or newtext ""))))
1423
1424 (defun gnus-simplify-buffer-fuzzy ()
1425   "Simplify string in the buffer fuzzily.
1426 The string in the accessible portion of the current buffer is simplified.
1427 It is assumed to be a single-line subject.
1428 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1429 matter is removed.  Additional things can be deleted by setting
1430 `gnus-simplify-subject-fuzzy-regexp'."
1431   (let ((case-fold-search t)
1432         (modified-tick))
1433     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1434
1435     (while (not (eq modified-tick (buffer-modified-tick)))
1436       (setq modified-tick (buffer-modified-tick))
1437       (cond
1438        ((listp gnus-simplify-subject-fuzzy-regexp)
1439         (mapcar 'gnus-simplify-buffer-fuzzy-step
1440                 gnus-simplify-subject-fuzzy-regexp))
1441        (gnus-simplify-subject-fuzzy-regexp
1442         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1443       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1444       (gnus-simplify-buffer-fuzzy-step
1445        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1446       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1447
1448     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1449     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1450     (gnus-simplify-buffer-fuzzy-step " $")
1451     (gnus-simplify-buffer-fuzzy-step "^ +")))
1452
1453 (defun gnus-simplify-subject-fuzzy (subject)
1454   "Simplify a subject string fuzzily.
1455 See `gnus-simplify-buffer-fuzzy' for details."
1456   (save-excursion
1457     (gnus-set-work-buffer)
1458     (let ((case-fold-search t))
1459       ;; Remove uninteresting prefixes.
1460       (when (and gnus-simplify-ignored-prefixes
1461                  (string-match gnus-simplify-ignored-prefixes subject))
1462         (setq subject (substring subject (match-end 0))))
1463       (insert subject)
1464       (inline (gnus-simplify-buffer-fuzzy))
1465       (buffer-string))))
1466
1467 (defsubst gnus-simplify-subject-fully (subject)
1468   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1469   (cond
1470    (gnus-simplify-subject-functions
1471     (gnus-map-function gnus-simplify-subject-functions subject))
1472    ((null gnus-summary-gather-subject-limit)
1473     (gnus-simplify-subject-re subject))
1474    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1475     (gnus-simplify-subject-fuzzy subject))
1476    ((numberp gnus-summary-gather-subject-limit)
1477     (gnus-limit-string (gnus-simplify-subject-re subject)
1478                        gnus-summary-gather-subject-limit))
1479    (t
1480     subject)))
1481
1482 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1483   "Check whether two subjects are equal.
1484 If optional argument simple-first is t, first argument is already
1485 simplified."
1486   (cond
1487    ((null simple-first)
1488     (equal (gnus-simplify-subject-fully s1)
1489            (gnus-simplify-subject-fully s2)))
1490    (t
1491     (equal s1
1492            (gnus-simplify-subject-fully s2)))))
1493
1494 (defun gnus-summary-bubble-group ()
1495   "Increase the score of the current group.
1496 This is a handy function to add to `gnus-summary-exit-hook' to
1497 increase the score of each group you read."
1498   (gnus-group-add-score gnus-newsgroup-name))
1499
1500 \f
1501 ;;;
1502 ;;; Gnus summary mode
1503 ;;;
1504
1505 (put 'gnus-summary-mode 'mode-class 'special)
1506
1507 (defvar gnus-article-commands-menu)
1508
1509 (when t
1510   ;; Non-orthogonal keys
1511
1512   (gnus-define-keys gnus-summary-mode-map
1513     " " gnus-summary-next-page
1514     "\177" gnus-summary-prev-page
1515     [delete] gnus-summary-prev-page
1516     [backspace] gnus-summary-prev-page
1517     "\r" gnus-summary-scroll-up
1518     "\M-\r" gnus-summary-scroll-down
1519     "n" gnus-summary-next-unread-article
1520     "p" gnus-summary-prev-unread-article
1521     "N" gnus-summary-next-article
1522     "P" gnus-summary-prev-article
1523     "\M-\C-n" gnus-summary-next-same-subject
1524     "\M-\C-p" gnus-summary-prev-same-subject
1525     "\M-n" gnus-summary-next-unread-subject
1526     "\M-p" gnus-summary-prev-unread-subject
1527     "." gnus-summary-first-unread-article
1528     "," gnus-summary-best-unread-article
1529     "\M-s" gnus-summary-search-article-forward
1530     "\M-r" gnus-summary-search-article-backward
1531     "<" gnus-summary-beginning-of-article
1532     ">" gnus-summary-end-of-article
1533     "j" gnus-summary-goto-article
1534     "^" gnus-summary-refer-parent-article
1535     "\M-^" gnus-summary-refer-article
1536     "u" gnus-summary-tick-article-forward
1537     "!" gnus-summary-tick-article-forward
1538     "U" gnus-summary-tick-article-backward
1539     "d" gnus-summary-mark-as-read-forward
1540     "D" gnus-summary-mark-as-read-backward
1541     "E" gnus-summary-mark-as-expirable
1542     "\M-u" gnus-summary-clear-mark-forward
1543     "\M-U" gnus-summary-clear-mark-backward
1544     "k" gnus-summary-kill-same-subject-and-select
1545     "\C-k" gnus-summary-kill-same-subject
1546     "\M-\C-k" gnus-summary-kill-thread
1547     "\M-\C-l" gnus-summary-lower-thread
1548     "e" gnus-summary-edit-article
1549     "#" gnus-summary-mark-as-processable
1550     "\M-#" gnus-summary-unmark-as-processable
1551     "\M-\C-t" gnus-summary-toggle-threads
1552     "\M-\C-s" gnus-summary-show-thread
1553     "\M-\C-h" gnus-summary-hide-thread
1554     "\M-\C-f" gnus-summary-next-thread
1555     "\M-\C-b" gnus-summary-prev-thread
1556     [(meta down)] gnus-summary-next-thread
1557     [(meta up)] gnus-summary-prev-thread
1558     "\M-\C-u" gnus-summary-up-thread
1559     "\M-\C-d" gnus-summary-down-thread
1560     "&" gnus-summary-execute-command
1561     "c" gnus-summary-catchup-and-exit
1562     "\C-w" gnus-summary-mark-region-as-read
1563     "\C-t" gnus-summary-toggle-truncation
1564     "?" gnus-summary-mark-as-dormant
1565     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1566     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1567     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1568     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1569     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1570     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1571     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1572     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1573     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1574     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1575     "=" gnus-summary-expand-window
1576     "\C-x\C-s" gnus-summary-reselect-current-group
1577     "\M-g" gnus-summary-rescan-group
1578     "w" gnus-summary-stop-page-breaking
1579     "\C-c\C-r" gnus-summary-caesar-message
1580     "f" gnus-summary-followup
1581     "F" gnus-summary-followup-with-original
1582     "C" gnus-summary-cancel-article
1583     "r" gnus-summary-reply
1584     "R" gnus-summary-reply-with-original
1585     "\C-c\C-f" gnus-summary-mail-forward
1586     "o" gnus-summary-save-article
1587     "\C-o" gnus-summary-save-article-mail
1588     "|" gnus-summary-pipe-output
1589     "\M-k" gnus-summary-edit-local-kill
1590     "\M-K" gnus-summary-edit-global-kill
1591     ;; "V" gnus-version
1592     "\C-c\C-d" gnus-summary-describe-group
1593     "q" gnus-summary-exit
1594     "Q" gnus-summary-exit-no-update
1595     "\C-c\C-i" gnus-info-find-node
1596     gnus-mouse-2 gnus-mouse-pick-article
1597     "m" gnus-summary-mail-other-window
1598     "a" gnus-summary-post-news
1599     "i" gnus-summary-news-other-window
1600     "x" gnus-summary-limit-to-unread
1601     "s" gnus-summary-isearch-article
1602     "t" gnus-summary-toggle-header
1603     "g" gnus-summary-show-article
1604     "l" gnus-summary-goto-last-article
1605     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1606     "\C-d" gnus-summary-enter-digest-group
1607     "\M-\C-d" gnus-summary-read-document
1608     "\M-\C-e" gnus-summary-edit-parameters
1609     "\M-\C-a" gnus-summary-customize-parameters
1610     "\C-c\C-b" gnus-bug
1611     "*" gnus-cache-enter-article
1612     "\M-*" gnus-cache-remove-article
1613     "\M-&" gnus-summary-universal-argument
1614     "\C-l" gnus-recenter
1615     "I" gnus-summary-increase-score
1616     "L" gnus-summary-lower-score
1617     "\M-i" gnus-symbolic-argument
1618     "h" gnus-summary-select-article-buffer
1619
1620     "b" gnus-article-view-part
1621     "\M-t" gnus-summary-toggle-display-buttonized
1622
1623     "V" gnus-summary-score-map
1624     "X" gnus-uu-extract-map
1625     "S" gnus-summary-send-map)
1626
1627   ;; Sort of orthogonal keymap
1628   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1629     "t" gnus-summary-tick-article-forward
1630     "!" gnus-summary-tick-article-forward
1631     "d" gnus-summary-mark-as-read-forward
1632     "r" gnus-summary-mark-as-read-forward
1633     "c" gnus-summary-clear-mark-forward
1634     " " gnus-summary-clear-mark-forward
1635     "e" gnus-summary-mark-as-expirable
1636     "x" gnus-summary-mark-as-expirable
1637     "?" gnus-summary-mark-as-dormant
1638     "b" gnus-summary-set-bookmark
1639     "B" gnus-summary-remove-bookmark
1640     "#" gnus-summary-mark-as-processable
1641     "\M-#" gnus-summary-unmark-as-processable
1642     "S" gnus-summary-limit-include-expunged
1643     "C" gnus-summary-catchup
1644     "H" gnus-summary-catchup-to-here
1645     "h" gnus-summary-catchup-from-here
1646     "\C-c" gnus-summary-catchup-all
1647     "k" gnus-summary-kill-same-subject-and-select
1648     "K" gnus-summary-kill-same-subject
1649     "P" gnus-uu-mark-map)
1650
1651   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1652     "c" gnus-summary-clear-above
1653     "u" gnus-summary-tick-above
1654     "m" gnus-summary-mark-above
1655     "k" gnus-summary-kill-below)
1656
1657   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1658     "/" gnus-summary-limit-to-subject
1659     "n" gnus-summary-limit-to-articles
1660     "w" gnus-summary-pop-limit
1661     "s" gnus-summary-limit-to-subject
1662     "a" gnus-summary-limit-to-author
1663     "u" gnus-summary-limit-to-unread
1664     "m" gnus-summary-limit-to-marks
1665     "M" gnus-summary-limit-exclude-marks
1666     "v" gnus-summary-limit-to-score
1667     "*" gnus-summary-limit-include-cached
1668     "D" gnus-summary-limit-include-dormant
1669     "T" gnus-summary-limit-include-thread
1670     "d" gnus-summary-limit-exclude-dormant
1671     "t" gnus-summary-limit-to-age
1672     "." gnus-summary-limit-to-unseen
1673     "x" gnus-summary-limit-to-extra
1674     "p" gnus-summary-limit-to-display-predicate
1675     "E" gnus-summary-limit-include-expunged
1676     "c" gnus-summary-limit-exclude-childless-dormant
1677     "C" gnus-summary-limit-mark-excluded-as-read
1678     "o" gnus-summary-insert-old-articles
1679     "N" gnus-summary-insert-new-articles)
1680
1681   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1682     "n" gnus-summary-next-unread-article
1683     "p" gnus-summary-prev-unread-article
1684     "N" gnus-summary-next-article
1685     "P" gnus-summary-prev-article
1686     "\C-n" gnus-summary-next-same-subject
1687     "\C-p" gnus-summary-prev-same-subject
1688     "\M-n" gnus-summary-next-unread-subject
1689     "\M-p" gnus-summary-prev-unread-subject
1690     "f" gnus-summary-first-unread-article
1691     "b" gnus-summary-best-unread-article
1692     "j" gnus-summary-goto-article
1693     "g" gnus-summary-goto-subject
1694     "l" gnus-summary-goto-last-article
1695     "o" gnus-summary-pop-article)
1696
1697   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1698     "k" gnus-summary-kill-thread
1699     "l" gnus-summary-lower-thread
1700     "i" gnus-summary-raise-thread
1701     "T" gnus-summary-toggle-threads
1702     "t" gnus-summary-rethread-current
1703     "^" gnus-summary-reparent-thread
1704     "s" gnus-summary-show-thread
1705     "S" gnus-summary-show-all-threads
1706     "h" gnus-summary-hide-thread
1707     "H" gnus-summary-hide-all-threads
1708     "n" gnus-summary-next-thread
1709     "p" gnus-summary-prev-thread
1710     "u" gnus-summary-up-thread
1711     "o" gnus-summary-top-thread
1712     "d" gnus-summary-down-thread
1713     "#" gnus-uu-mark-thread
1714     "\M-#" gnus-uu-unmark-thread)
1715
1716   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1717     "g" gnus-summary-prepare
1718     "c" gnus-summary-insert-cached-articles
1719     "d" gnus-summary-insert-dormant-articles)
1720
1721   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1722     "c" gnus-summary-catchup-and-exit
1723     "C" gnus-summary-catchup-all-and-exit
1724     "E" gnus-summary-exit-no-update
1725     "Q" gnus-summary-exit
1726     "Z" gnus-summary-exit
1727     "n" gnus-summary-catchup-and-goto-next-group
1728     "R" gnus-summary-reselect-current-group
1729     "G" gnus-summary-rescan-group
1730     "N" gnus-summary-next-group
1731     "s" gnus-summary-save-newsrc
1732     "P" gnus-summary-prev-group)
1733
1734   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1735     " " gnus-summary-next-page
1736     "n" gnus-summary-next-page
1737     "\177" gnus-summary-prev-page
1738     [delete] gnus-summary-prev-page
1739     "p" gnus-summary-prev-page
1740     "\r" gnus-summary-scroll-up
1741     "\M-\r" gnus-summary-scroll-down
1742     "<" gnus-summary-beginning-of-article
1743     ">" gnus-summary-end-of-article
1744     "b" gnus-summary-beginning-of-article
1745     "e" gnus-summary-end-of-article
1746     "^" gnus-summary-refer-parent-article
1747     "r" gnus-summary-refer-parent-article
1748     "D" gnus-summary-enter-digest-group
1749     "R" gnus-summary-refer-references
1750     "T" gnus-summary-refer-thread
1751     "g" gnus-summary-show-article
1752     "s" gnus-summary-isearch-article
1753     "P" gnus-summary-print-article
1754     "M" gnus-mailing-list-insinuate
1755     "t" gnus-article-babel)
1756
1757   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1758     "b" gnus-article-add-buttons
1759     "B" gnus-article-add-buttons-to-head
1760     "o" gnus-article-treat-overstrike
1761     "e" gnus-article-emphasize
1762     "w" gnus-article-fill-cited-article
1763     "Q" gnus-article-fill-long-lines
1764     "C" gnus-article-capitalize-sentences
1765     "c" gnus-article-remove-cr
1766     "q" gnus-article-de-quoted-unreadable
1767     "6" gnus-article-de-base64-unreadable
1768     "Z" gnus-article-decode-HZ
1769     "h" gnus-article-wash-html
1770     "u" gnus-article-unsplit-urls
1771     "s" gnus-summary-force-verify-and-decrypt
1772     "f" gnus-article-display-x-face
1773     "l" gnus-summary-stop-page-breaking
1774     "r" gnus-summary-caesar-message
1775     "m" gnus-summary-morse-message
1776     "t" gnus-summary-toggle-header
1777     "g" gnus-treat-smiley
1778     "v" gnus-summary-verbose-headers
1779     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1780     "p" gnus-article-verify-x-pgp-sig
1781     "d" gnus-article-treat-dumbquotes
1782     "k" gnus-article-outlook-deuglify-article)
1783
1784   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1785     "a" gnus-article-hide
1786     "h" gnus-article-hide-headers
1787     "b" gnus-article-hide-boring-headers
1788     "s" gnus-article-hide-signature
1789     "c" gnus-article-hide-citation
1790     "C" gnus-article-hide-citation-in-followups
1791     "l" gnus-article-hide-list-identifiers
1792     "p" gnus-article-hide-pgp
1793     "B" gnus-article-strip-banner
1794     "P" gnus-article-hide-pem
1795     "\C-c" gnus-article-hide-citation-maybe)
1796
1797   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1798     "a" gnus-article-highlight
1799     "h" gnus-article-highlight-headers
1800     "c" gnus-article-highlight-citation
1801     "s" gnus-article-highlight-signature)
1802
1803   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1804     "f" gnus-article-treat-fold-headers
1805     "u" gnus-article-treat-unfold-headers
1806     "n" gnus-article-treat-fold-newsgroups)
1807
1808   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1809     "x" gnus-article-display-x-face
1810     "s" gnus-treat-smiley
1811     "D" gnus-article-remove-images
1812     "f" gnus-treat-from-picon
1813     "m" gnus-treat-mail-picon
1814     "n" gnus-treat-newsgroups-picon)
1815
1816   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1817     "w" gnus-article-decode-mime-words
1818     "c" gnus-article-decode-charset
1819     "v" gnus-mime-view-all-parts
1820     "b" gnus-article-view-part)
1821
1822   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1823     "z" gnus-article-date-ut
1824     "u" gnus-article-date-ut
1825     "l" gnus-article-date-local
1826     "p" gnus-article-date-english
1827     "e" gnus-article-date-lapsed
1828     "o" gnus-article-date-original
1829     "i" gnus-article-date-iso8601
1830     "s" gnus-article-date-user)
1831
1832   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1833     "t" gnus-article-remove-trailing-blank-lines
1834     "l" gnus-article-strip-leading-blank-lines
1835     "m" gnus-article-strip-multiple-blank-lines
1836     "a" gnus-article-strip-blank-lines
1837     "A" gnus-article-strip-all-blank-lines
1838     "s" gnus-article-strip-leading-space
1839     "e" gnus-article-strip-trailing-space
1840     "w" gnus-article-remove-leading-whitespace)
1841
1842   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1843     "v" gnus-version
1844     "f" gnus-summary-fetch-faq
1845     "d" gnus-summary-describe-group
1846     "h" gnus-summary-describe-briefly
1847     "i" gnus-info-find-node
1848     "c" gnus-group-fetch-charter
1849     "C" gnus-group-fetch-control)
1850
1851   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1852     "e" gnus-summary-expire-articles
1853     "\M-\C-e" gnus-summary-expire-articles-now
1854     "\177" gnus-summary-delete-article
1855     [delete] gnus-summary-delete-article
1856     [backspace] gnus-summary-delete-article
1857     "m" gnus-summary-move-article
1858     "r" gnus-summary-respool-article
1859     "w" gnus-summary-edit-article
1860     "c" gnus-summary-copy-article
1861     "B" gnus-summary-crosspost-article
1862     "q" gnus-summary-respool-query
1863     "t" gnus-summary-respool-trace
1864     "i" gnus-summary-import-article
1865     "I" gnus-summary-create-article
1866     "p" gnus-summary-article-posted-p)
1867
1868   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1869     "o" gnus-summary-save-article
1870     "m" gnus-summary-save-article-mail
1871     "F" gnus-summary-write-article-file
1872     "r" gnus-summary-save-article-rmail
1873     "f" gnus-summary-save-article-file
1874     "b" gnus-summary-save-article-body-file
1875     "h" gnus-summary-save-article-folder
1876     "v" gnus-summary-save-article-vm
1877     "p" gnus-summary-pipe-output
1878     "P" gnus-summary-muttprint
1879     "s" gnus-soup-add-article)
1880
1881   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1882     "b" gnus-summary-display-buttonized
1883     "m" gnus-summary-repair-multipart
1884     "v" gnus-article-view-part
1885     "o" gnus-article-save-part
1886     "c" gnus-article-copy-part
1887     "C" gnus-article-view-part-as-charset
1888     "e" gnus-article-view-part-externally
1889     "E" gnus-article-encrypt-body
1890     "i" gnus-article-inline-part
1891     "|" gnus-article-pipe-part)
1892
1893   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1894     "p" gnus-summary-mark-as-processable
1895     "u" gnus-summary-unmark-as-processable
1896     "U" gnus-summary-unmark-all-processable
1897     "v" gnus-uu-mark-over
1898     "s" gnus-uu-mark-series
1899     "r" gnus-uu-mark-region
1900     "g" gnus-uu-unmark-region
1901     "R" gnus-uu-mark-by-regexp
1902     "G" gnus-uu-unmark-by-regexp
1903     "t" gnus-uu-mark-thread
1904     "T" gnus-uu-unmark-thread
1905     "a" gnus-uu-mark-all
1906     "b" gnus-uu-mark-buffer
1907     "S" gnus-uu-mark-sparse
1908     "k" gnus-summary-kill-process-mark
1909     "y" gnus-summary-yank-process-mark
1910     "w" gnus-summary-save-process-mark
1911     "i" gnus-uu-invert-processable)
1912
1913   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1914     ;;"x" gnus-uu-extract-any
1915     "m" gnus-summary-save-parts
1916     "u" gnus-uu-decode-uu
1917     "U" gnus-uu-decode-uu-and-save
1918     "s" gnus-uu-decode-unshar
1919     "S" gnus-uu-decode-unshar-and-save
1920     "o" gnus-uu-decode-save
1921     "O" gnus-uu-decode-save
1922     "b" gnus-uu-decode-binhex
1923     "B" gnus-uu-decode-binhex
1924     "p" gnus-uu-decode-postscript
1925     "P" gnus-uu-decode-postscript-and-save)
1926
1927   (gnus-define-keys
1928       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1929     "u" gnus-uu-decode-uu-view
1930     "U" gnus-uu-decode-uu-and-save-view
1931     "s" gnus-uu-decode-unshar-view
1932     "S" gnus-uu-decode-unshar-and-save-view
1933     "o" gnus-uu-decode-save-view
1934     "O" gnus-uu-decode-save-view
1935     "b" gnus-uu-decode-binhex-view
1936     "B" gnus-uu-decode-binhex-view
1937     "p" gnus-uu-decode-postscript-view
1938     "P" gnus-uu-decode-postscript-and-save-view))
1939
1940 (defvar gnus-article-post-menu nil)
1941
1942 (defconst gnus-summary-menu-maxlen 20)
1943
1944 (defun gnus-summary-menu-split (menu)
1945   ;; If we have lots of elements, divide them into groups of 20
1946   ;; and make a pane (or submenu) for each one.
1947   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1948       (let ((menu menu) sublists next
1949             (i 1))
1950         (while menu
1951           ;; Pull off the next gnus-summary-menu-maxlen elements
1952           ;; and make them the next element of sublist.
1953           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1954           (if next
1955               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1956                       nil))
1957           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1958                                              (aref (car (last menu)) 0)) menu)
1959                                sublists))
1960           (setq i (1+ i))
1961           (setq menu next))
1962         (nreverse sublists))
1963     ;; Few elements--put them all in one pane.
1964     menu))
1965
1966 (defun gnus-summary-make-menu-bar ()
1967   (gnus-turn-off-edit-menu 'summary)
1968
1969   (unless (boundp 'gnus-summary-misc-menu)
1970
1971     (easy-menu-define
1972       gnus-summary-kill-menu gnus-summary-mode-map ""
1973       (cons
1974        "Score"
1975        (nconc
1976         (list
1977          ["Customize" gnus-score-customize t])
1978         (gnus-make-score-map 'increase)
1979         (gnus-make-score-map 'lower)
1980         '(("Mark"
1981            ["Kill below" gnus-summary-kill-below t]
1982            ["Mark above" gnus-summary-mark-above t]
1983            ["Tick above" gnus-summary-tick-above t]
1984            ["Clear above" gnus-summary-clear-above t])
1985           ["Current score" gnus-summary-current-score t]
1986           ["Set score" gnus-summary-set-score t]
1987           ["Switch current score file..." gnus-score-change-score-file t]
1988           ["Set mark below..." gnus-score-set-mark-below t]
1989           ["Set expunge below..." gnus-score-set-expunge-below t]
1990           ["Edit current score file" gnus-score-edit-current-scores t]
1991           ["Edit score file" gnus-score-edit-file t]
1992           ["Trace score" gnus-score-find-trace t]
1993           ["Find words" gnus-score-find-favourite-words t]
1994           ["Rescore buffer" gnus-summary-rescore t]
1995           ["Increase score..." gnus-summary-increase-score t]
1996           ["Lower score..." gnus-summary-lower-score t]))))
1997
1998     ;; Define both the Article menu in the summary buffer and the
1999     ;; equivalent Commands menu in the article buffer here for
2000     ;; consistency.
2001     (let ((innards
2002            `(("Hide"
2003               ["All" gnus-article-hide t]
2004               ["Headers" gnus-article-hide-headers t]
2005               ["Signature" gnus-article-hide-signature t]
2006               ["Citation" gnus-article-hide-citation t]
2007               ["List identifiers" gnus-article-hide-list-identifiers t]
2008               ["PGP" gnus-article-hide-pgp t]
2009               ["Banner" gnus-article-strip-banner t]
2010               ["Boring headers" gnus-article-hide-boring-headers t])
2011              ("Highlight"
2012               ["All" gnus-article-highlight t]
2013               ["Headers" gnus-article-highlight-headers t]
2014               ["Signature" gnus-article-highlight-signature t]
2015               ["Citation" gnus-article-highlight-citation t])
2016              ("MIME"
2017               ["Words" gnus-article-decode-mime-words t]
2018               ["Charset" gnus-article-decode-charset t]
2019               ["QP" gnus-article-de-quoted-unreadable t]
2020               ["Base64" gnus-article-de-base64-unreadable t]
2021               ["View MIME buttons" gnus-summary-display-buttonized t]
2022               ["View all" gnus-mime-view-all-parts t]
2023               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2024               ["Encrypt body" gnus-article-encrypt-body t]
2025               ["Extract all parts" gnus-summary-save-parts t])
2026              ("Date"
2027               ["Local" gnus-article-date-local t]
2028               ["ISO8601" gnus-article-date-iso8601 t]
2029               ["UT" gnus-article-date-ut t]
2030               ["Original" gnus-article-date-original t]
2031               ["Lapsed" gnus-article-date-lapsed t]
2032               ["User-defined" gnus-article-date-user t])
2033              ("Display"
2034               ["Remove images" gnus-article-remove-images t]
2035               ["Toggle smiley" gnus-treat-smiley t]
2036               ["Show X-Face" gnus-article-display-x-face t]
2037               ["Show picons in From" gnus-treat-from-picon t]
2038               ["Show picons in mail headers" gnus-treat-mail-picon t]
2039               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2040               ("View as different encoding"
2041                ,@(gnus-summary-menu-split
2042                   (mapcar
2043                    (lambda (cs)
2044                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2045                      ;; forms for menu commands, we should provide intern'ed
2046                      ;; function symbols.
2047                      (let ((command (intern (format "\
2048 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2049                        (fset command
2050                              `(lambda ()
2051                                 (interactive)
2052                                 (let ((gnus-summary-show-article-charset-alist
2053                                        '((1 . ,cs))))
2054                                   (gnus-summary-show-article 1))))
2055                        `[,(symbol-name cs) ,command t]))
2056                    (sort (if (fboundp 'coding-system-list)
2057                              (coding-system-list)
2058                            (mapcar 'car mm-mime-mule-charset-alist))
2059                          'string<)))))
2060              ("Washing"
2061               ("Remove Blanks"
2062                ["Leading" gnus-article-strip-leading-blank-lines t]
2063                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2064                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2065                ["All of the above" gnus-article-strip-blank-lines t]
2066                ["All" gnus-article-strip-all-blank-lines t]
2067                ["Leading space" gnus-article-strip-leading-space t]
2068                ["Trailing space" gnus-article-strip-trailing-space t]
2069                ["Leading space in headers"
2070                 gnus-article-remove-leading-whitespace t])
2071               ["Overstrike" gnus-article-treat-overstrike t]
2072               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2073               ["Emphasis" gnus-article-emphasize t]
2074               ["Word wrap" gnus-article-fill-cited-article t]
2075               ["Fill long lines" gnus-article-fill-long-lines t]
2076               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2077               ["CR" gnus-article-remove-cr t]
2078               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2079               ["Base64" gnus-article-de-base64-unreadable t]
2080               ["Rot 13" gnus-summary-caesar-message
2081                ,@(if (featurep 'xemacs) '(t)
2082                    '(:help "\"Caesar rotate\" article by 13"))]
2083               ["Morse decode" gnus-summary-morse-message t]
2084               ["Unix pipe..." gnus-summary-pipe-message t]
2085               ["Add buttons" gnus-article-add-buttons t]
2086               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2087               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2088               ["Verbose header" gnus-summary-verbose-headers t]
2089               ["Toggle header" gnus-summary-toggle-header t]
2090               ["Unfold headers" gnus-article-treat-unfold-headers t]
2091               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2092               ["Html" gnus-article-wash-html t]
2093               ["URLs" gnus-article-unsplit-urls t]
2094               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2095               ["HZ" gnus-article-decode-HZ t]
2096               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2097               )
2098              ("Output"
2099               ["Save in default format" gnus-summary-save-article
2100                ,@(if (featurep 'xemacs) '(t)
2101                    '(:help "Save article using default method"))]
2102               ["Save in file" gnus-summary-save-article-file
2103                ,@(if (featurep 'xemacs) '(t)
2104                    '(:help "Save article in file"))]
2105               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2106               ["Save in MH folder" gnus-summary-save-article-folder t]
2107               ["Save in VM folder" gnus-summary-save-article-vm t]
2108               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2109               ["Save body in file" gnus-summary-save-article-body-file t]
2110               ["Pipe through a filter" gnus-summary-pipe-output t]
2111               ["Add to SOUP packet" gnus-soup-add-article t]
2112               ["Print with Muttprint" gnus-summary-muttprint t]
2113               ["Print" gnus-summary-print-article t])
2114              ("Backend"
2115               ["Respool article..." gnus-summary-respool-article t]
2116               ["Move article..." gnus-summary-move-article
2117                (gnus-check-backend-function
2118                 'request-move-article gnus-newsgroup-name)]
2119               ["Copy article..." gnus-summary-copy-article t]
2120               ["Crosspost article..." gnus-summary-crosspost-article
2121                (gnus-check-backend-function
2122                 'request-replace-article gnus-newsgroup-name)]
2123               ["Import file..." gnus-summary-import-article t]
2124               ["Create article..." gnus-summary-create-article t]
2125               ["Check if posted" gnus-summary-article-posted-p t]
2126               ["Edit article" gnus-summary-edit-article
2127                (not (gnus-group-read-only-p))]
2128               ["Delete article" gnus-summary-delete-article
2129                (gnus-check-backend-function
2130                 'request-expire-articles gnus-newsgroup-name)]
2131               ["Query respool" gnus-summary-respool-query t]
2132               ["Trace respool" gnus-summary-respool-trace t]
2133               ["Delete expirable articles" gnus-summary-expire-articles-now
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)])
2136              ("Extract"
2137               ["Uudecode" gnus-uu-decode-uu
2138                ,@(if (featurep 'xemacs) '(t)
2139                    '(:help "Decode uuencoded article(s)"))]
2140               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2141               ["Unshar" gnus-uu-decode-unshar t]
2142               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2143               ["Save" gnus-uu-decode-save t]
2144               ["Binhex" gnus-uu-decode-binhex t]
2145               ["Postscript" gnus-uu-decode-postscript t]
2146               ["All MIME parts" gnus-summary-save-parts t])
2147              ("Cache"
2148               ["Enter article" gnus-cache-enter-article t]
2149               ["Remove article" gnus-cache-remove-article t])
2150              ["Translate" gnus-article-babel t]
2151              ["Select article buffer" gnus-summary-select-article-buffer t]
2152              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2153              ["Isearch article..." gnus-summary-isearch-article t]
2154              ["Beginning of the article" gnus-summary-beginning-of-article t]
2155              ["End of the article" gnus-summary-end-of-article t]
2156              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2157              ["Fetch referenced articles" gnus-summary-refer-references t]
2158              ["Fetch current thread" gnus-summary-refer-thread t]
2159              ["Fetch article with id..." gnus-summary-refer-article t]
2160              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2161              ["Redisplay" gnus-summary-show-article t]
2162              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2163       (easy-menu-define
2164         gnus-summary-article-menu gnus-summary-mode-map ""
2165         (cons "Article" innards))
2166
2167       (if (not (keymapp gnus-summary-article-menu))
2168           (easy-menu-define
2169             gnus-article-commands-menu gnus-article-mode-map ""
2170             (cons "Commands" innards))
2171         ;; in Emacs, don't share menu.
2172         (setq gnus-article-commands-menu
2173               (copy-keymap gnus-summary-article-menu))
2174         (define-key gnus-article-mode-map [menu-bar commands]
2175           (cons "Commands" gnus-article-commands-menu))))
2176
2177     (easy-menu-define
2178       gnus-summary-thread-menu gnus-summary-mode-map ""
2179       '("Threads"
2180         ["Toggle threading" gnus-summary-toggle-threads t]
2181         ["Hide threads" gnus-summary-hide-all-threads t]
2182         ["Show threads" gnus-summary-show-all-threads t]
2183         ["Hide thread" gnus-summary-hide-thread t]
2184         ["Show thread" gnus-summary-show-thread t]
2185         ["Go to next thread" gnus-summary-next-thread t]
2186         ["Go to previous thread" gnus-summary-prev-thread t]
2187         ["Go down thread" gnus-summary-down-thread t]
2188         ["Go up thread" gnus-summary-up-thread t]
2189         ["Top of thread" gnus-summary-top-thread t]
2190         ["Mark thread as read" gnus-summary-kill-thread t]
2191         ["Lower thread score" gnus-summary-lower-thread t]
2192         ["Raise thread score" gnus-summary-raise-thread t]
2193         ["Rethread current" gnus-summary-rethread-current t]))
2194
2195     (easy-menu-define
2196       gnus-summary-post-menu gnus-summary-mode-map ""
2197       `("Post"
2198         ["Send a message (mail or news)" gnus-summary-post-news
2199          ,@(if (featurep 'xemacs) '(t)
2200              '(:help "Post an article"))]
2201         ["Followup" gnus-summary-followup
2202          ,@(if (featurep 'xemacs) '(t)
2203              '(:help "Post followup to this article"))]
2204         ["Followup and yank" gnus-summary-followup-with-original
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post followup to this article, quoting its contents"))]
2207         ["Supersede article" gnus-summary-supersede-article t]
2208         ["Cancel article" gnus-summary-cancel-article
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Cancel an article you posted"))]
2211         ["Reply" gnus-summary-reply t]
2212         ["Reply and yank" gnus-summary-reply-with-original t]
2213         ["Wide reply" gnus-summary-wide-reply t]
2214         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Mail a reply, quoting this article"))]
2217         ["Very wide reply" gnus-summary-very-wide-reply t]
2218         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Mail a very wide reply, quoting this article"))]
2221         ["Mail forward" gnus-summary-mail-forward t]
2222         ["Post forward" gnus-summary-post-forward t]
2223         ["Digest and mail" gnus-uu-digest-mail-forward t]
2224         ["Digest and post" gnus-uu-digest-post-forward t]
2225         ["Resend message" gnus-summary-resend-message t]
2226         ["Resend message edit" gnus-summary-resend-message-edit t]
2227         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2228         ["Send a mail" gnus-summary-mail-other-window t]
2229         ["Create a local message" gnus-summary-news-other-window t]
2230         ["Uuencode and post" gnus-uu-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post a uuencoded article"))]
2233         ["Followup via news" gnus-summary-followup-to-mail t]
2234         ["Followup via news and yank"
2235          gnus-summary-followup-to-mail-with-original t]
2236         ;;("Draft"
2237         ;;["Send" gnus-summary-send-draft t]
2238         ;;["Send bounced" gnus-resend-bounced-mail t])
2239         ))
2240
2241     (cond
2242      ((not (keymapp gnus-summary-post-menu))
2243       (setq gnus-article-post-menu gnus-summary-post-menu))
2244      ((not gnus-article-post-menu)
2245       ;; Don't share post menu.
2246       (setq gnus-article-post-menu
2247             (copy-keymap gnus-summary-post-menu))))
2248     (define-key gnus-article-mode-map [menu-bar post]
2249       (cons "Post" gnus-article-post-menu))
2250
2251     (easy-menu-define
2252       gnus-summary-misc-menu gnus-summary-mode-map ""
2253       `("Gnus"
2254         ("Mark Read"
2255          ["Mark as read" gnus-summary-mark-as-read-forward t]
2256          ["Mark same subject and select"
2257           gnus-summary-kill-same-subject-and-select t]
2258          ["Mark same subject" gnus-summary-kill-same-subject t]
2259          ["Catchup" gnus-summary-catchup
2260           ,@(if (featurep 'xemacs) '(t)
2261               '(:help "Mark unread articles in this group as read"))]
2262          ["Catchup all" gnus-summary-catchup-all t]
2263          ["Catchup to here" gnus-summary-catchup-to-here t]
2264          ["Catchup from here" gnus-summary-catchup-from-here t]
2265          ["Catchup region" gnus-summary-mark-region-as-read t]
2266          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2267         ("Mark Various"
2268          ["Tick" gnus-summary-tick-article-forward t]
2269          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2270          ["Remove marks" gnus-summary-clear-mark-forward t]
2271          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2272          ["Set bookmark" gnus-summary-set-bookmark t]
2273          ["Remove bookmark" gnus-summary-remove-bookmark t])
2274         ("Limit to"
2275          ["Marks..." gnus-summary-limit-to-marks t]
2276          ["Subject..." gnus-summary-limit-to-subject t]
2277          ["Author..." gnus-summary-limit-to-author t]
2278          ["Age..." gnus-summary-limit-to-age t]
2279          ["Extra..." gnus-summary-limit-to-extra t]
2280          ["Score..." gnus-summary-limit-to-score t]
2281          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2282          ["Unread" gnus-summary-limit-to-unread t]
2283          ["Unseen" gnus-summary-limit-to-unseen t]
2284          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2285          ["Next articles" gnus-summary-limit-to-articles t]
2286          ["Pop limit" gnus-summary-pop-limit t]
2287          ["Show dormant" gnus-summary-limit-include-dormant t]
2288          ["Hide childless dormant"
2289           gnus-summary-limit-exclude-childless-dormant t]
2290          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2291          ["Hide marked" gnus-summary-limit-exclude-marks t]
2292          ["Show expunged" gnus-summary-limit-include-expunged t])
2293         ("Process Mark"
2294          ["Set mark" gnus-summary-mark-as-processable t]
2295          ["Remove mark" gnus-summary-unmark-as-processable t]
2296          ["Remove all marks" gnus-summary-unmark-all-processable t]
2297          ["Mark above" gnus-uu-mark-over t]
2298          ["Mark series" gnus-uu-mark-series t]
2299          ["Mark region" gnus-uu-mark-region t]
2300          ["Unmark region" gnus-uu-unmark-region t]
2301          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2302          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2303          ["Mark all" gnus-uu-mark-all t]
2304          ["Mark buffer" gnus-uu-mark-buffer t]
2305          ["Mark sparse" gnus-uu-mark-sparse t]
2306          ["Mark thread" gnus-uu-mark-thread t]
2307          ["Unmark thread" gnus-uu-unmark-thread t]
2308          ("Process Mark Sets"
2309           ["Kill" gnus-summary-kill-process-mark t]
2310           ["Yank" gnus-summary-yank-process-mark
2311            gnus-newsgroup-process-stack]
2312           ["Save" gnus-summary-save-process-mark t]))
2313         ("Scroll article"
2314          ["Page forward" gnus-summary-next-page
2315           ,@(if (featurep 'xemacs) '(t)
2316               '(:help "Show next page of article"))]
2317          ["Page backward" gnus-summary-prev-page
2318           ,@(if (featurep 'xemacs) '(t)
2319               '(:help "Show previous page of article"))]
2320          ["Line forward" gnus-summary-scroll-up t])
2321         ("Move"
2322          ["Next unread article" gnus-summary-next-unread-article t]
2323          ["Previous unread article" gnus-summary-prev-unread-article t]
2324          ["Next article" gnus-summary-next-article t]
2325          ["Previous article" gnus-summary-prev-article t]
2326          ["Next unread subject" gnus-summary-next-unread-subject t]
2327          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2328          ["Next article same subject" gnus-summary-next-same-subject t]
2329          ["Previous article same subject" gnus-summary-prev-same-subject t]
2330          ["First unread article" gnus-summary-first-unread-article t]
2331          ["Best unread article" gnus-summary-best-unread-article t]
2332          ["Go to subject number..." gnus-summary-goto-subject t]
2333          ["Go to article number..." gnus-summary-goto-article t]
2334          ["Go to the last article" gnus-summary-goto-last-article t]
2335          ["Pop article off history" gnus-summary-pop-article t])
2336         ("Sort"
2337          ["Sort by number" gnus-summary-sort-by-number t]
2338          ["Sort by author" gnus-summary-sort-by-author t]
2339          ["Sort by subject" gnus-summary-sort-by-subject t]
2340          ["Sort by date" gnus-summary-sort-by-date t]
2341          ["Sort by score" gnus-summary-sort-by-score t]
2342          ["Sort by lines" gnus-summary-sort-by-lines t]
2343          ["Sort by characters" gnus-summary-sort-by-chars t]
2344          ["Randomize" gnus-summary-sort-by-random t]
2345          ["Original sort" gnus-summary-sort-by-original t])
2346         ("Help"
2347          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2348          ["Describe group" gnus-summary-describe-group t]
2349          ["Fetch charter" gnus-group-fetch-charter
2350           ,@(if (featurep 'xemacs) nil
2351               '(:help "Display the charter of the current group"))]
2352          ["Fetch control message" gnus-group-fetch-control
2353           ,@(if (featurep 'xemacs) nil
2354               '(:help "Display the archived control message for the current group"))]
2355          ["Read manual" gnus-info-find-node t])
2356         ("Modes"
2357          ["Pick and read" gnus-pick-mode t]
2358          ["Binary" gnus-binary-mode t])
2359         ("Regeneration"
2360          ["Regenerate" gnus-summary-prepare t]
2361          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2362          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2363          ["Toggle threading" gnus-summary-toggle-threads t])
2364         ["See old articles" gnus-summary-insert-old-articles t]
2365         ["See new articles" gnus-summary-insert-new-articles t]
2366         ["Filter articles..." gnus-summary-execute-command t]
2367         ["Run command on subjects..." gnus-summary-universal-argument t]
2368         ["Search articles forward..." gnus-summary-search-article-forward t]
2369         ["Search articles backward..." gnus-summary-search-article-backward t]
2370         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2371         ["Expand window" gnus-summary-expand-window t]
2372         ["Expire expirable articles" gnus-summary-expire-articles
2373          (gnus-check-backend-function
2374           'request-expire-articles gnus-newsgroup-name)]
2375         ["Edit local kill file" gnus-summary-edit-local-kill t]
2376         ["Edit main kill file" gnus-summary-edit-global-kill t]
2377         ["Edit group parameters" gnus-summary-edit-parameters t]
2378         ["Customize group parameters" gnus-summary-customize-parameters t]
2379         ["Send a bug report" gnus-bug t]
2380         ("Exit"
2381          ["Catchup and exit" gnus-summary-catchup-and-exit
2382           ,@(if (featurep 'xemacs) '(t)
2383               '(:help "Mark unread articles in this group as read, then exit"))]
2384          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2385          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2386          ["Exit group" gnus-summary-exit
2387           ,@(if (featurep 'xemacs) '(t)
2388               '(:help "Exit current group, return to group selection mode"))]
2389          ["Exit group without updating" gnus-summary-exit-no-update t]
2390          ["Exit and goto next group" gnus-summary-next-group t]
2391          ["Exit and goto prev group" gnus-summary-prev-group t]
2392          ["Reselect group" gnus-summary-reselect-current-group t]
2393          ["Rescan group" gnus-summary-rescan-group t]
2394          ["Update dribble" gnus-summary-save-newsrc t])))
2395
2396     (gnus-run-hooks 'gnus-summary-menu-hook)))
2397
2398 (defvar gnus-summary-tool-bar-map nil)
2399
2400 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2401 (defun gnus-summary-make-tool-bar ()
2402   (if (and (fboundp 'tool-bar-add-item-from-menu)
2403            (default-value 'tool-bar-mode)
2404            (not gnus-summary-tool-bar-map))
2405       (setq gnus-summary-tool-bar-map
2406             (let ((tool-bar-map (make-sparse-keymap))
2407                   (load-path (mm-image-load-path)))
2408               (tool-bar-add-item-from-menu
2409                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2410               (tool-bar-add-item-from-menu
2411                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2412               (tool-bar-add-item-from-menu
2413                'gnus-summary-post-news "post" gnus-summary-mode-map)
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-followup "followup" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-reply "reply" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2438               tool-bar-map)))
2439   (if gnus-summary-tool-bar-map
2440       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2441
2442 (defun gnus-score-set-default (var value)
2443   "A version of set that updates the GNU Emacs menu-bar."
2444   (set var value)
2445   ;; It is the message that forces the active status to be updated.
2446   (message ""))
2447
2448 (defun gnus-make-score-map (type)
2449   "Make a summary score map of type TYPE."
2450   (if t
2451       nil
2452     (let ((headers '(("author" "from" string)
2453                      ("subject" "subject" string)
2454                      ("article body" "body" string)
2455                      ("article head" "head" string)
2456                      ("xref" "xref" string)
2457                      ("extra header" "extra" string)
2458                      ("lines" "lines" number)
2459                      ("followups to author" "followup" string)))
2460           (types '((number ("less than" <)
2461                            ("greater than" >)
2462                            ("equal" =))
2463                    (string ("substring" s)
2464                            ("exact string" e)
2465                            ("fuzzy string" f)
2466                            ("regexp" r))))
2467           (perms '(("temporary" (current-time-string))
2468                    ("permanent" nil)
2469                    ("immediate" now)))
2470           header)
2471       (list
2472        (apply
2473         'nconc
2474         (list
2475          (if (eq type 'lower)
2476              "Lower score"
2477            "Increase score"))
2478         (let (outh)
2479           (while headers
2480             (setq header (car headers))
2481             (setq outh
2482                   (cons
2483                    (apply
2484                     'nconc
2485                     (list (car header))
2486                     (let ((ts (cdr (assoc (nth 2 header) types)))
2487                           outt)
2488                       (while ts
2489                         (setq outt
2490                               (cons
2491                                (apply
2492                                 'nconc
2493                                 (list (caar ts))
2494                                 (let ((ps perms)
2495                                       outp)
2496                                   (while ps
2497                                     (setq outp
2498                                           (cons
2499                                            (vector
2500                                             (caar ps)
2501                                             (list
2502                                              'gnus-summary-score-entry
2503                                              (nth 1 header)
2504                                              (if (or (string= (nth 1 header)
2505                                                               "head")
2506                                                      (string= (nth 1 header)
2507                                                               "body"))
2508                                                  ""
2509                                                (list 'gnus-summary-header
2510                                                      (nth 1 header)))
2511                                              (list 'quote (nth 1 (car ts)))
2512                                              (list 'gnus-score-delta-default
2513                                                    nil)
2514                                              (nth 1 (car ps))
2515                                              t)
2516                                             t)
2517                                            outp))
2518                                     (setq ps (cdr ps)))
2519                                   (list (nreverse outp))))
2520                                outt))
2521                         (setq ts (cdr ts)))
2522                       (list (nreverse outt))))
2523                    outh))
2524             (setq headers (cdr headers)))
2525           (list (nreverse outh))))))))
2526
2527 \f
2528
2529 (defun gnus-summary-mode (&optional group)
2530   "Major mode for reading articles.
2531
2532 All normal editing commands are switched off.
2533 \\<gnus-summary-mode-map>
2534 Each line in this buffer represents one article.  To read an
2535 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2536 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2537 respectively.
2538
2539 You can also post articles and send mail from this buffer.  To
2540 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2541 of an article, type `\\[gnus-summary-reply]'.
2542
2543 There are approx. one gazillion commands you can execute in this
2544 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2545
2546 The following commands are available:
2547
2548 \\{gnus-summary-mode-map}"
2549   (interactive)
2550   (kill-all-local-variables)
2551   (when (gnus-visual-p 'summary-menu 'menu)
2552     (gnus-summary-make-menu-bar)
2553     (gnus-summary-make-tool-bar))
2554   (gnus-summary-make-local-variables)
2555   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2556     (gnus-summary-make-local-variables))
2557   (gnus-make-thread-indent-array)
2558   (gnus-simplify-mode-line)
2559   (setq major-mode 'gnus-summary-mode)
2560   (setq mode-name "Summary")
2561   (make-local-variable 'minor-mode-alist)
2562   (use-local-map gnus-summary-mode-map)
2563   (buffer-disable-undo)
2564   (setq buffer-read-only t)             ;Disable modification
2565   (setq truncate-lines t)
2566   (setq selective-display t)
2567   (setq selective-display-ellipses t)   ;Display `...'
2568   (gnus-summary-set-display-table)
2569   (gnus-set-default-directory)
2570   (setq gnus-newsgroup-name group)
2571   (make-local-variable 'gnus-summary-line-format)
2572   (make-local-variable 'gnus-summary-line-format-spec)
2573   (make-local-variable 'gnus-summary-dummy-line-format)
2574   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2575   (make-local-variable 'gnus-summary-mark-positions)
2576   (make-local-hook 'pre-command-hook)
2577   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2578   (gnus-run-hooks 'gnus-summary-mode-hook)
2579   (turn-on-gnus-mailing-list-mode)
2580   (mm-enable-multibyte)
2581   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2582   (gnus-update-summary-mark-positions))
2583
2584 (defun gnus-summary-make-local-variables ()
2585   "Make all the local summary buffer variables."
2586   (let (global)
2587     (dolist (local gnus-summary-local-variables)
2588       (if (consp local)
2589           (progn
2590             (if (eq (cdr local) 'global)
2591                 ;; Copy the global value of the variable.
2592                 (setq global (symbol-value (car local)))
2593               ;; Use the value from the list.
2594               (setq global (eval (cdr local))))
2595             (set (make-local-variable (car local)) global))
2596         ;; Simple nil-valued local variable.
2597         (set (make-local-variable local) nil)))))
2598
2599 (defun gnus-summary-clear-local-variables ()
2600   (let ((locals gnus-summary-local-variables))
2601     (while locals
2602       (if (consp (car locals))
2603           (and (vectorp (caar locals))
2604                (set (caar locals) nil))
2605         (and (vectorp (car locals))
2606              (set (car locals) nil)))
2607       (setq locals (cdr locals)))))
2608
2609 ;; Summary data functions.
2610
2611 (defmacro gnus-data-number (data)
2612   `(car ,data))
2613
2614 (defmacro gnus-data-set-number (data number)
2615   `(setcar ,data ,number))
2616
2617 (defmacro gnus-data-mark (data)
2618   `(nth 1 ,data))
2619
2620 (defmacro gnus-data-set-mark (data mark)
2621   `(setcar (nthcdr 1 ,data) ,mark))
2622
2623 (defmacro gnus-data-pos (data)
2624   `(nth 2 ,data))
2625
2626 (defmacro gnus-data-set-pos (data pos)
2627   `(setcar (nthcdr 2 ,data) ,pos))
2628
2629 (defmacro gnus-data-header (data)
2630   `(nth 3 ,data))
2631
2632 (defmacro gnus-data-set-header (data header)
2633   `(setf (nth 3 ,data) ,header))
2634
2635 (defmacro gnus-data-level (data)
2636   `(nth 4 ,data))
2637
2638 (defmacro gnus-data-unread-p (data)
2639   `(= (nth 1 ,data) gnus-unread-mark))
2640
2641 (defmacro gnus-data-read-p (data)
2642   `(/= (nth 1 ,data) gnus-unread-mark))
2643
2644 (defmacro gnus-data-pseudo-p (data)
2645   `(consp (nth 3 ,data)))
2646
2647 (defmacro gnus-data-find (number)
2648   `(assq ,number gnus-newsgroup-data))
2649
2650 (defmacro gnus-data-find-list (number &optional data)
2651   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2652      (memq (assq ,number bdata)
2653            bdata)))
2654
2655 (defmacro gnus-data-make (number mark pos header level)
2656   `(list ,number ,mark ,pos ,header ,level))
2657
2658 (defun gnus-data-enter (after-article number mark pos header level offset)
2659   (let ((data (gnus-data-find-list after-article)))
2660     (unless data
2661       (error "No such article: %d" after-article))
2662     (setcdr data (cons (gnus-data-make number mark pos header level)
2663                        (cdr data)))
2664     (setq gnus-newsgroup-data-reverse nil)
2665     (gnus-data-update-list (cddr data) offset)))
2666
2667 (defun gnus-data-enter-list (after-article list &optional offset)
2668   (when list
2669     (let ((data (and after-article (gnus-data-find-list after-article)))
2670           (ilist list))
2671       (if (not (or data
2672                    after-article))
2673           (let ((odata gnus-newsgroup-data))
2674             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2675             (when offset
2676               (gnus-data-update-list odata offset)))
2677       ;; Find the last element in the list to be spliced into the main
2678         ;; list.
2679         (while (cdr list)
2680           (setq list (cdr list)))
2681         (if (not data)
2682             (progn
2683               (setcdr list gnus-newsgroup-data)
2684               (setq gnus-newsgroup-data ilist)
2685               (when offset
2686                 (gnus-data-update-list (cdr list) offset)))
2687           (setcdr list (cdr data))
2688           (setcdr data ilist)
2689           (when offset
2690             (gnus-data-update-list (cdr list) offset))))
2691       (setq gnus-newsgroup-data-reverse nil))))
2692
2693 (defun gnus-data-remove (article &optional offset)
2694   (let ((data gnus-newsgroup-data))
2695     (if (= (gnus-data-number (car data)) article)
2696         (progn
2697           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2698                 gnus-newsgroup-data-reverse nil)
2699           (when offset
2700             (gnus-data-update-list gnus-newsgroup-data offset)))
2701       (while (cdr data)
2702         (when (= (gnus-data-number (cadr data)) article)
2703           (setcdr data (cddr data))
2704           (when offset
2705             (gnus-data-update-list (cdr data) offset))
2706           (setq data nil
2707                 gnus-newsgroup-data-reverse nil))
2708         (setq data (cdr data))))))
2709
2710 (defmacro gnus-data-list (backward)
2711   `(if ,backward
2712        (or gnus-newsgroup-data-reverse
2713            (setq gnus-newsgroup-data-reverse
2714                  (reverse gnus-newsgroup-data)))
2715      gnus-newsgroup-data))
2716
2717 (defun gnus-data-update-list (data offset)
2718   "Add OFFSET to the POS of all data entries in DATA."
2719   (setq gnus-newsgroup-data-reverse nil)
2720   (while data
2721     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2722     (setq data (cdr data))))
2723
2724 (defun gnus-summary-article-pseudo-p (article)
2725   "Say whether this article is a pseudo article or not."
2726   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2727
2728 (defmacro gnus-summary-article-sparse-p (article)
2729   "Say whether this article is a sparse article or not."
2730   `(memq ,article gnus-newsgroup-sparse))
2731
2732 (defmacro gnus-summary-article-ancient-p (article)
2733   "Say whether this article is a sparse article or not."
2734   `(memq ,article gnus-newsgroup-ancient))
2735
2736 (defun gnus-article-parent-p (number)
2737   "Say whether this article is a parent or not."
2738   (let ((data (gnus-data-find-list number)))
2739     (and (cdr data)              ; There has to be an article after...
2740          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2741             (gnus-data-level (nth 1 data))))))
2742
2743 (defun gnus-article-children (number)
2744   "Return a list of all children to NUMBER."
2745   (let* ((data (gnus-data-find-list number))
2746          (level (gnus-data-level (car data)))
2747          children)
2748     (setq data (cdr data))
2749     (while (and data
2750                 (= (gnus-data-level (car data)) (1+ level)))
2751       (push (gnus-data-number (car data)) children)
2752       (setq data (cdr data)))
2753     children))
2754
2755 (defmacro gnus-summary-skip-intangible ()
2756   "If the current article is intangible, then jump to a different article."
2757   '(let ((to (get-text-property (point) 'gnus-intangible)))
2758      (and to (gnus-summary-goto-subject to))))
2759
2760 (defmacro gnus-summary-article-intangible-p ()
2761   "Say whether this article is intangible or not."
2762   '(get-text-property (point) 'gnus-intangible))
2763
2764 (defun gnus-article-read-p (article)
2765   "Say whether ARTICLE is read or not."
2766   (not (or (memq article gnus-newsgroup-marked)
2767            (memq article gnus-newsgroup-spam-marked)
2768            (memq article gnus-newsgroup-unreads)
2769            (memq article gnus-newsgroup-unselected)
2770            (memq article gnus-newsgroup-dormant))))
2771
2772 ;; Some summary mode macros.
2773
2774 (defmacro gnus-summary-article-number ()
2775   "The article number of the article on the current line.
2776 If there isn't an article number here, then we return the current
2777 article number."
2778   '(progn
2779      (gnus-summary-skip-intangible)
2780      (or (get-text-property (point) 'gnus-number)
2781          (gnus-summary-last-subject))))
2782
2783 (defmacro gnus-summary-article-header (&optional number)
2784   "Return the header of article NUMBER."
2785   `(gnus-data-header (gnus-data-find
2786                       ,(or number '(gnus-summary-article-number)))))
2787
2788 (defmacro gnus-summary-thread-level (&optional number)
2789   "Return the level of thread that starts with article NUMBER."
2790   `(if (and (eq gnus-summary-make-false-root 'dummy)
2791             (get-text-property (point) 'gnus-intangible))
2792        0
2793      (gnus-data-level (gnus-data-find
2794                        ,(or number '(gnus-summary-article-number))))))
2795
2796 (defmacro gnus-summary-article-mark (&optional number)
2797   "Return the mark of article NUMBER."
2798   `(gnus-data-mark (gnus-data-find
2799                     ,(or number '(gnus-summary-article-number)))))
2800
2801 (defmacro gnus-summary-article-pos (&optional number)
2802   "Return the position of the line of article NUMBER."
2803   `(gnus-data-pos (gnus-data-find
2804                    ,(or number '(gnus-summary-article-number)))))
2805
2806 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2807 (defmacro gnus-summary-article-subject (&optional number)
2808   "Return current subject string or nil if nothing."
2809   `(let ((headers
2810           ,(if number
2811                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2812              '(gnus-data-header (assq (gnus-summary-article-number)
2813                                       gnus-newsgroup-data)))))
2814      (and headers
2815           (vectorp headers)
2816           (mail-header-subject headers))))
2817
2818 (defmacro gnus-summary-article-score (&optional number)
2819   "Return current article score."
2820   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2821                   gnus-newsgroup-scored))
2822        gnus-summary-default-score 0))
2823
2824 (defun gnus-summary-article-children (&optional number)
2825   "Return a list of article numbers that are children of article NUMBER."
2826   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2827          (level (gnus-data-level (car data)))
2828          l children)
2829     (while (and (setq data (cdr data))
2830                 (> (setq l (gnus-data-level (car data))) level))
2831       (and (= (1+ level) l)
2832            (push (gnus-data-number (car data))
2833                  children)))
2834     (nreverse children)))
2835
2836 (defun gnus-summary-article-parent (&optional number)
2837   "Return the article number of the parent of article NUMBER."
2838   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2839                                     (gnus-data-list t)))
2840          (level (gnus-data-level (car data))))
2841     (if (zerop level)
2842         ()                              ; This is a root.
2843       ;; We search until we find an article with a level less than
2844       ;; this one.  That function has to be the parent.
2845       (while (and (setq data (cdr data))
2846                   (not (< (gnus-data-level (car data)) level))))
2847       (and data (gnus-data-number (car data))))))
2848
2849 (defun gnus-unread-mark-p (mark)
2850   "Say whether MARK is the unread mark."
2851   (= mark gnus-unread-mark))
2852
2853 (defun gnus-read-mark-p (mark)
2854   "Say whether MARK is one of the marks that mark as read.
2855 This is all marks except unread, ticked, dormant, and expirable."
2856   (not (or (= mark gnus-unread-mark)
2857            (= mark gnus-ticked-mark)
2858            (= mark gnus-dormant-mark)
2859            (= mark gnus-expirable-mark))))
2860
2861 (defmacro gnus-article-mark (number)
2862   "Return the MARK of article NUMBER.
2863 This macro should only be used when computing the mark the \"first\"
2864 time; i.e., when generating the summary lines.  After that,
2865 `gnus-summary-article-mark' should be used to examine the
2866 marks of articles."
2867   `(cond
2868     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2869     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2870     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2871     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2872     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2873     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2874     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2875     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2876            gnus-ancient-mark))))
2877
2878 ;; Saving hidden threads.
2879
2880 (defmacro gnus-save-hidden-threads (&rest forms)
2881   "Save hidden threads, eval FORMS, and restore the hidden threads."
2882   (let ((config (make-symbol "config")))
2883     `(let ((,config (gnus-hidden-threads-configuration)))
2884        (unwind-protect
2885            (save-excursion
2886              ,@forms)
2887          (gnus-restore-hidden-threads-configuration ,config)))))
2888 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2889 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2890
2891 (defun gnus-data-compute-positions ()
2892   "Compute the positions of all articles."
2893   (setq gnus-newsgroup-data-reverse nil)
2894   (let ((data gnus-newsgroup-data))
2895     (save-excursion
2896       (gnus-save-hidden-threads
2897         (gnus-summary-show-all-threads)
2898         (goto-char (point-min))
2899         (while data
2900           (while (get-text-property (point) 'gnus-intangible)
2901             (forward-line 1))
2902           (gnus-data-set-pos (car data) (+ (point) 3))
2903           (setq data (cdr data))
2904           (forward-line 1))))))
2905
2906 (defun gnus-hidden-threads-configuration ()
2907   "Return the current hidden threads configuration."
2908   (save-excursion
2909     (let (config)
2910       (goto-char (point-min))
2911       (while (search-forward "\r" nil t)
2912         (push (1- (point)) config))
2913       config)))
2914
2915 (defun gnus-restore-hidden-threads-configuration (config)
2916   "Restore hidden threads configuration from CONFIG."
2917   (save-excursion
2918     (let (point buffer-read-only)
2919       (while (setq point (pop config))
2920         (when (and (< point (point-max))
2921                    (goto-char point)
2922                    (eq (char-after) ?\n))
2923           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2924
2925 ;; Various summary mode internalish functions.
2926
2927 (defun gnus-mouse-pick-article (e)
2928   (interactive "e")
2929   (mouse-set-point e)
2930   (gnus-summary-next-page nil t))
2931
2932 (defun gnus-summary-set-display-table ()
2933   "Change the display table.
2934 Odd characters have a tendency to mess
2935 up nicely formatted displays - we make all possible glyphs
2936 display only a single character."
2937
2938   ;; We start from the standard display table, if any.
2939   (let ((table (or (copy-sequence standard-display-table)
2940                    (make-display-table)))
2941         (i 32))
2942     ;; Nix out all the control chars...
2943     (while (>= (setq i (1- i)) 0)
2944       (aset table i [??]))
2945    ;; ... but not newline and cr, of course.  (cr is necessary for the
2946     ;; selective display).
2947     (aset table ?\n nil)
2948     (aset table ?\r nil)
2949     ;; We keep TAB as well.
2950     (aset table ?\t nil)
2951     ;; We nix out any glyphs over 126 that are not set already.
2952     (let ((i 256))
2953       (while (>= (setq i (1- i)) 127)
2954         ;; Only modify if the entry is nil.
2955         (unless (aref table i)
2956           (aset table i [??]))))
2957     (setq buffer-display-table table)))
2958
2959 (defun gnus-summary-set-article-display-arrow (pos)
2960   "Update the overlay arrow to point to line at position POS."
2961   (when (and gnus-summary-display-arrow
2962              (boundp 'overlay-arrow-position)
2963              (boundp 'overlay-arrow-string))
2964     (save-excursion
2965       (goto-char pos)
2966       (beginning-of-line)
2967       (unless overlay-arrow-position
2968         (setq overlay-arrow-position (make-marker)))
2969       (setq overlay-arrow-string "=>"
2970             overlay-arrow-position (set-marker overlay-arrow-position
2971                                                (point)
2972                                                (current-buffer))))))
2973
2974 (defun gnus-summary-buffer-name (group)
2975   "Return the summary buffer name of GROUP."
2976   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2977
2978 (defun gnus-summary-setup-buffer (group)
2979   "Initialize summary buffer."
2980   (let ((buffer (gnus-summary-buffer-name group))
2981         (dead-name (concat "*Dead Summary "
2982                            (gnus-group-decoded-name group) "*")))
2983     ;; If a dead summary buffer exists, we kill it.
2984     (when (gnus-buffer-live-p dead-name)
2985       (gnus-kill-buffer dead-name))
2986     (if (get-buffer buffer)
2987         (progn
2988           (set-buffer buffer)
2989           (setq gnus-summary-buffer (current-buffer))
2990           (not gnus-newsgroup-prepared))
2991       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2992       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2993       (gnus-summary-mode group)
2994       (when gnus-carpal
2995         (gnus-carpal-setup-buffer 'summary))
2996       (unless gnus-single-article-buffer
2997         (make-local-variable 'gnus-article-buffer)
2998         (make-local-variable 'gnus-article-current)
2999         (make-local-variable 'gnus-original-article-buffer))
3000       (setq gnus-newsgroup-name group)
3001       ;; Set any local variables in the group parameters.
3002       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3003       t)))
3004
3005 (defun gnus-set-global-variables ()
3006   "Set the global equivalents of the buffer-local variables.
3007 They are set to the latest values they had.  These reflect the summary
3008 buffer that was in action when the last article was fetched."
3009   (when (eq major-mode 'gnus-summary-mode)
3010     (setq gnus-summary-buffer (current-buffer))
3011     (let ((name gnus-newsgroup-name)
3012           (marked gnus-newsgroup-marked)
3013           (spam gnus-newsgroup-spam-marked)
3014           (unread gnus-newsgroup-unreads)
3015           (headers gnus-current-headers)
3016           (data gnus-newsgroup-data)
3017           (summary gnus-summary-buffer)
3018           (article-buffer gnus-article-buffer)
3019           (original gnus-original-article-buffer)
3020           (gac gnus-article-current)
3021           (reffed gnus-reffed-article-number)
3022           (score-file gnus-current-score-file)
3023           (default-charset gnus-newsgroup-charset)
3024           vlist)
3025       (let ((locals gnus-newsgroup-variables))
3026         (while locals
3027           (if (consp (car locals))
3028               (push (eval (caar locals)) vlist)
3029             (push (eval (car locals)) vlist))
3030           (setq locals (cdr locals)))
3031         (setq vlist (nreverse vlist)))
3032       (save-excursion
3033         (set-buffer gnus-group-buffer)
3034         (setq gnus-newsgroup-name name
3035               gnus-newsgroup-marked marked
3036               gnus-newsgroup-spam-marked spam
3037               gnus-newsgroup-unreads unread
3038               gnus-current-headers headers
3039               gnus-newsgroup-data data
3040               gnus-article-current gac
3041               gnus-summary-buffer summary
3042               gnus-article-buffer article-buffer
3043               gnus-original-article-buffer original
3044               gnus-reffed-article-number reffed
3045               gnus-current-score-file score-file
3046               gnus-newsgroup-charset default-charset)
3047         (let ((locals gnus-newsgroup-variables))
3048           (while locals
3049             (if (consp (car locals))
3050                 (set (caar locals) (pop vlist))
3051               (set (car locals) (pop vlist)))
3052             (setq locals (cdr locals))))
3053         ;; The article buffer also has local variables.
3054         (when (gnus-buffer-live-p gnus-article-buffer)
3055           (set-buffer gnus-article-buffer)
3056           (setq gnus-summary-buffer summary))))))
3057
3058 (defun gnus-summary-article-unread-p (article)
3059   "Say whether ARTICLE is unread or not."
3060   (memq article gnus-newsgroup-unreads))
3061
3062 (defun gnus-summary-first-article-p (&optional article)
3063   "Return whether ARTICLE is the first article in the buffer."
3064   (if (not (setq article (or article (gnus-summary-article-number))))
3065       nil
3066     (eq article (caar gnus-newsgroup-data))))
3067
3068 (defun gnus-summary-last-article-p (&optional article)
3069   "Return whether ARTICLE is the last article in the buffer."
3070   (if (not (setq article (or article (gnus-summary-article-number))))
3071       ;; All non-existent numbers are the last article.  :-)
3072       t
3073     (not (cdr (gnus-data-find-list article)))))
3074
3075 (defun gnus-make-thread-indent-array ()
3076   (let ((n 200))
3077     (unless (and gnus-thread-indent-array
3078                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3079       (setq gnus-thread-indent-array (make-vector 201 "")
3080             gnus-thread-indent-array-level gnus-thread-indent-level)
3081       (while (>= n 0)
3082         (aset gnus-thread-indent-array n
3083               (make-string (* n gnus-thread-indent-level) ? ))
3084         (setq n (1- n))))))
3085
3086 (defun gnus-update-summary-mark-positions ()
3087   "Compute where the summary marks are to go."
3088   (save-excursion
3089     (when (gnus-buffer-exists-p gnus-summary-buffer)
3090       (set-buffer gnus-summary-buffer))
3091     (let ((gnus-replied-mark 129)
3092           (gnus-score-below-mark 130)
3093           (gnus-score-over-mark 130)
3094           (gnus-downloaded-mark 131)
3095           (spec gnus-summary-line-format-spec)
3096           gnus-visual pos)
3097       (save-excursion
3098         (gnus-set-work-buffer)
3099         (let ((gnus-summary-line-format-spec spec)
3100               (gnus-newsgroup-downloadable '(0)))
3101           (gnus-summary-insert-line
3102            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3103            0 nil nil 128 t nil "" nil 1)
3104           (goto-char (point-min))
3105           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3106                                              (- (point) (point-min) 1)))))
3107           (goto-char (point-min))
3108           (push (cons 'replied (and (search-forward "\201" nil t)
3109                                     (- (point) (point-min) 1)))
3110                 pos)
3111           (goto-char (point-min))
3112           (push (cons 'score (and (search-forward "\202" nil t)
3113                                   (- (point) (point-min) 1)))
3114                 pos)
3115           (goto-char (point-min))
3116           (push (cons 'download
3117                       (and (search-forward "\203" nil t)
3118                            (- (point) (point-min) 1)))
3119                 pos)))
3120       (setq gnus-summary-mark-positions pos))))
3121
3122 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3123   "Insert a dummy root in the summary buffer."
3124   (beginning-of-line)
3125   (gnus-add-text-properties
3126    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3127    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3128
3129 (defun gnus-summary-extract-address-component (from)
3130   (or (car (funcall gnus-extract-address-components from))
3131       from))
3132
3133 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3134   (let ((mail-parse-charset gnus-newsgroup-charset)
3135         ; Is it really necessary to do this next part for each summary line?
3136         ; Luckily, doesn't seem to slow things down much.
3137         (mail-parse-ignored-charsets
3138          (save-excursion (set-buffer gnus-summary-buffer)
3139                          gnus-newsgroup-ignored-charsets)))
3140     (or
3141      (and gnus-ignored-from-addresses
3142           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3143           (let ((extra-headers (mail-header-extra header))
3144                 to
3145                 newsgroups)
3146             (cond
3147              ((setq to (cdr (assq 'To extra-headers)))
3148               (concat "-> "
3149                       (inline
3150                         (gnus-summary-extract-address-component
3151                          (funcall gnus-decode-encoded-word-function to)))))
3152              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3153               (concat "=> " newsgroups)))))
3154      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3155
3156 (defun gnus-summary-insert-line (gnus-tmp-header
3157                                  gnus-tmp-level gnus-tmp-current
3158                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3159                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3160                                  &optional gnus-tmp-dummy gnus-tmp-score
3161                                  gnus-tmp-process)
3162   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3163          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3164          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3165          (gnus-tmp-score-char
3166           (if (or (null gnus-summary-default-score)
3167                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3168                       gnus-summary-zcore-fuzz))
3169               ?                         ;Whitespace
3170             (if (< gnus-tmp-score gnus-summary-default-score)
3171                 gnus-score-below-mark gnus-score-over-mark)))
3172          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3173          (gnus-tmp-replied
3174           (cond (gnus-tmp-process gnus-process-mark)
3175                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3176                  gnus-cached-mark)
3177                 (gnus-tmp-replied gnus-replied-mark)
3178                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3179                  gnus-forwarded-mark)
3180                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3181                  gnus-saved-mark)
3182                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3183                  gnus-recent-mark)
3184                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3185                  gnus-unseen-mark)
3186                 (t gnus-no-mark)))
3187          (gnus-tmp-downloaded
3188           (cond (undownloaded 
3189                  gnus-undownloaded-mark)
3190                 (gnus-newsgroup-agentized
3191                  gnus-downloaded-mark)
3192                 (t
3193                  gnus-no-mark)))
3194          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3195          (gnus-tmp-name
3196           (cond
3197            ((string-match "<[^>]+> *$" gnus-tmp-from)
3198             (let ((beg (match-beginning 0)))
3199               (or (and (string-match "^\".+\"" gnus-tmp-from)
3200                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3201                   (substring gnus-tmp-from 0 beg))))
3202            ((string-match "(.+)" gnus-tmp-from)
3203             (substring gnus-tmp-from
3204                        (1+ (match-beginning 0)) (1- (match-end 0))))
3205            (t gnus-tmp-from)))
3206          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3207          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3208          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3209          (buffer-read-only nil))
3210     (when (string= gnus-tmp-name "")
3211       (setq gnus-tmp-name gnus-tmp-from))
3212     (unless (numberp gnus-tmp-lines)
3213       (setq gnus-tmp-lines -1))
3214     (if (= gnus-tmp-lines -1)
3215         (setq gnus-tmp-lines "?")
3216       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3217     (gnus-put-text-property
3218      (point)
3219      (progn (eval gnus-summary-line-format-spec) (point))
3220      'gnus-number gnus-tmp-number)
3221     (when (gnus-visual-p 'summary-highlight 'highlight)
3222       (forward-line -1)
3223       (gnus-run-hooks 'gnus-summary-update-hook)
3224       (forward-line 1))))
3225
3226 (defun gnus-summary-update-line (&optional dont-update)
3227   "Update summary line after change."
3228   (when (and gnus-summary-default-score
3229              (not gnus-summary-inhibit-highlight))
3230     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3231            (article (gnus-summary-article-number))
3232            (score (gnus-summary-article-score article)))
3233       (unless dont-update
3234         (if (and gnus-summary-mark-below
3235                  (< (gnus-summary-article-score)
3236                     gnus-summary-mark-below))
3237             ;; This article has a low score, so we mark it as read.
3238             (when (memq article gnus-newsgroup-unreads)
3239               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3240           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3241             ;; This article was previously marked as read on account
3242             ;; of a low score, but now it has risen, so we mark it as
3243             ;; unread.
3244             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3245         (gnus-summary-update-mark
3246          (if (or (null gnus-summary-default-score)
3247                  (<= (abs (- score gnus-summary-default-score))
3248                      gnus-summary-zcore-fuzz))
3249              ?                          ;Whitespace
3250            (if (< score gnus-summary-default-score)
3251                gnus-score-below-mark gnus-score-over-mark))
3252          'score))
3253       ;; Do visual highlighting.
3254       (when (gnus-visual-p 'summary-highlight 'highlight)
3255         (gnus-run-hooks 'gnus-summary-update-hook)))))
3256
3257 (defvar gnus-tmp-new-adopts nil)
3258
3259 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3260   "Return the number of articles in THREAD.
3261 This may be 0 in some cases -- if none of the articles in
3262 the thread are to be displayed."
3263   (let* ((number
3264          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3265           (cond
3266            ((not (listp thread))
3267             1)
3268            ((and (consp thread) (cdr thread))
3269             (apply
3270              '+ 1 (mapcar
3271                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3272            ((null thread)
3273             1)
3274            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3275             1)
3276            (t 0))))
3277     (when (and level (zerop level) gnus-tmp-new-adopts)
3278       (incf number
3279             (apply '+ (mapcar
3280                        'gnus-summary-number-of-articles-in-thread
3281                        gnus-tmp-new-adopts))))
3282     (if char
3283         (if (> number 1) gnus-not-empty-thread-mark
3284           gnus-empty-thread-mark)
3285       number)))
3286
3287 (defsubst gnus-summary-line-message-size (head)
3288   "Return pretty-printed version of message size.
3289 This function is intended to be used in
3290 `gnus-summary-line-format-alist', which see."
3291   (let ((c (or (mail-header-chars head) -1)))
3292     (cond ((< c 0) "n/a")               ; chars not available
3293           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3294           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3295           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3296           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3297
3298
3299 (defun gnus-summary-set-local-parameters (group)
3300   "Go through the local params of GROUP and set all variable specs in that list."
3301   (let ((params (gnus-group-find-parameter group))
3302         (vars '(quit-config))           ; Ignore quit-config.
3303         elem)
3304     (while params
3305       (setq elem (car params)
3306             params (cdr params))
3307       (and (consp elem)                 ; Has to be a cons.
3308            (consp (cdr elem))           ; The cdr has to be a list.
3309            (symbolp (car elem))         ; Has to be a symbol in there.
3310            (not (memq (car elem) vars))
3311            (ignore-errors               ; So we set it.
3312              (push (car elem) vars)
3313              (make-local-variable (car elem))
3314              (set (car elem) (eval (nth 1 elem))))))))
3315
3316 (defun gnus-summary-read-group (group &optional show-all no-article
3317                                       kill-buffer no-display backward
3318                                       select-articles)
3319   "Start reading news in newsgroup GROUP.
3320 If SHOW-ALL is non-nil, already read articles are also listed.
3321 If NO-ARTICLE is non-nil, no article is selected initially.
3322 If NO-DISPLAY, don't generate a summary buffer."
3323   (let (result)
3324     (while (and group
3325                 (null (setq result
3326                             (let ((gnus-auto-select-next nil))
3327                               (or (gnus-summary-read-group-1
3328                                    group show-all no-article
3329                                    kill-buffer no-display
3330                                    select-articles)
3331                                   (setq show-all nil
3332                                         select-articles nil)))))
3333                 (eq gnus-auto-select-next 'quietly))
3334       (set-buffer gnus-group-buffer)
3335       ;; The entry function called above goes to the next
3336       ;; group automatically, so we go two groups back
3337       ;; if we are searching for the previous group.
3338       (when backward
3339         (gnus-group-prev-unread-group 2))
3340       (if (not (equal group (gnus-group-group-name)))
3341           (setq group (gnus-group-group-name))
3342         (setq group nil)))
3343     result))
3344
3345 (defun gnus-summary-read-group-1 (group show-all no-article
3346                                         kill-buffer no-display
3347                                         &optional select-articles)
3348   ;; Killed foreign groups can't be entered.
3349   ;;  (when (and (not (gnus-group-native-p group))
3350   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3351   ;;    (error "Dead non-native groups can't be entered"))
3352   (gnus-message 5 "Retrieving newsgroup: %s..."
3353                 (gnus-group-decoded-name group))
3354   (let* ((new-group (gnus-summary-setup-buffer group))
3355          (quit-config (gnus-group-quit-config group))
3356          (did-select (and new-group (gnus-select-newsgroup
3357                                      group show-all select-articles))))
3358     (cond
3359      ;; This summary buffer exists already, so we just select it.
3360      ((not new-group)
3361       (gnus-set-global-variables)
3362       (when kill-buffer
3363         (gnus-kill-or-deaden-summary kill-buffer))
3364       (gnus-configure-windows 'summary 'force)
3365       (gnus-set-mode-line 'summary)
3366       (gnus-summary-position-point)
3367       (message "")
3368       t)
3369      ;; We couldn't select this group.
3370      ((null did-select)
3371       (when (and (eq major-mode 'gnus-summary-mode)
3372                  (not (equal (current-buffer) kill-buffer)))
3373         (kill-buffer (current-buffer))
3374         (if (not quit-config)
3375             (progn
3376               ;; Update the info -- marks might need to be removed,
3377               ;; for instance.
3378               (gnus-summary-update-info)
3379               (set-buffer gnus-group-buffer)
3380               (gnus-group-jump-to-group group)
3381               (gnus-group-next-unread-group 1))
3382           (gnus-handle-ephemeral-exit quit-config)))
3383       (let ((grpinfo (gnus-get-info group)))
3384         (if (null (gnus-info-read grpinfo))
3385             (gnus-message 3 "Group %s contains no messages"
3386                           (gnus-group-decoded-name group))
3387           (gnus-message 3 "Can't select group")))
3388       nil)
3389      ;; The user did a `C-g' while prompting for number of articles,
3390      ;; so we exit this group.
3391      ((eq did-select 'quit)
3392       (and (eq major-mode 'gnus-summary-mode)
3393            (not (equal (current-buffer) kill-buffer))
3394            (kill-buffer (current-buffer)))
3395       (when kill-buffer
3396         (gnus-kill-or-deaden-summary kill-buffer))
3397       (if (not quit-config)
3398           (progn
3399             (set-buffer gnus-group-buffer)
3400             (gnus-group-jump-to-group group)
3401             (gnus-group-next-unread-group 1)
3402             (gnus-configure-windows 'group 'force))
3403         (gnus-handle-ephemeral-exit quit-config))
3404       ;; Finally signal the quit.
3405       (signal 'quit nil))
3406      ;; The group was successfully selected.
3407      (t
3408       (gnus-set-global-variables)
3409       ;; Save the active value in effect when the group was entered.
3410       (setq gnus-newsgroup-active
3411             (gnus-copy-sequence
3412              (gnus-active gnus-newsgroup-name)))
3413       ;; You can change the summary buffer in some way with this hook.
3414       (gnus-run-hooks 'gnus-select-group-hook)
3415       (gnus-update-format-specifications
3416        nil 'summary 'summary-mode 'summary-dummy)
3417       (gnus-update-summary-mark-positions)
3418       ;; Do score processing.
3419       (when gnus-use-scoring
3420         (gnus-possibly-score-headers))
3421       ;; Check whether to fill in the gaps in the threads.
3422       (when gnus-build-sparse-threads
3423         (gnus-build-sparse-threads))
3424       ;; Find the initial limit.
3425       (if gnus-show-threads
3426           (if show-all
3427               (let ((gnus-newsgroup-dormant nil))
3428                 (gnus-summary-initial-limit show-all))
3429             (gnus-summary-initial-limit show-all))
3430         ;; When unthreaded, all articles are always shown.
3431         (setq gnus-newsgroup-limit
3432               (mapcar
3433                (lambda (header) (mail-header-number header))
3434                gnus-newsgroup-headers)))
3435       ;; Generate the summary buffer.
3436       (unless no-display
3437         (gnus-summary-prepare))
3438       (when gnus-use-trees
3439         (gnus-tree-open group)
3440         (setq gnus-summary-highlight-line-function
3441               'gnus-tree-highlight-article))
3442       ;; If the summary buffer is empty, but there are some low-scored
3443       ;; articles or some excluded dormants, we include these in the
3444       ;; buffer.
3445       (when (and (zerop (buffer-size))
3446                  (not no-display))
3447         (cond (gnus-newsgroup-dormant
3448                (gnus-summary-limit-include-dormant))
3449               ((and gnus-newsgroup-scored show-all)
3450                (gnus-summary-limit-include-expunged t))))
3451       ;; Function `gnus-apply-kill-file' must be called in this hook.
3452       (gnus-run-hooks 'gnus-apply-kill-hook)
3453       (if (and (zerop (buffer-size))
3454                (not no-display))
3455           (progn
3456             ;; This newsgroup is empty.
3457             (gnus-summary-catchup-and-exit nil t)
3458             (gnus-message 6 "No unread news")
3459             (when kill-buffer
3460               (gnus-kill-or-deaden-summary kill-buffer))
3461             ;; Return nil from this function.
3462             nil)
3463         ;; Hide conversation thread subtrees.  We cannot do this in
3464         ;; gnus-summary-prepare-hook since kill processing may not
3465         ;; work with hidden articles.
3466         (gnus-summary-maybe-hide-threads)
3467         (when kill-buffer
3468           (gnus-kill-or-deaden-summary kill-buffer))
3469         (gnus-summary-auto-select-subject)
3470         ;; Show first unread article if requested.
3471         (if (and (not no-article)
3472                  (not no-display)
3473                  gnus-newsgroup-unreads
3474                  gnus-auto-select-first)
3475             (progn
3476               (gnus-configure-windows 'summary)
3477               (let ((art (gnus-summary-article-number)))
3478                 (unless (or (memq art gnus-newsgroup-undownloaded)
3479                             (memq art gnus-newsgroup-downloadable))
3480                   (gnus-summary-goto-article art))))
3481           ;; Don't select any articles.
3482           (gnus-summary-position-point)
3483           (gnus-configure-windows 'summary 'force)
3484           (gnus-set-mode-line 'summary))
3485         (when (get-buffer-window gnus-group-buffer t)
3486           ;; Gotta use windows, because recenter does weird stuff if
3487           ;; the current buffer ain't the displayed window.
3488           (let ((owin (selected-window)))
3489             (select-window (get-buffer-window gnus-group-buffer t))
3490             (when (gnus-group-goto-group group)
3491               (recenter))
3492             (select-window owin)))
3493         ;; Mark this buffer as "prepared".
3494         (setq gnus-newsgroup-prepared t)
3495         (gnus-run-hooks 'gnus-summary-prepared-hook)
3496         (gnus-group-update-group group)
3497         t)))))
3498
3499 (defun gnus-summary-auto-select-subject ()
3500   "Select the subject line on initial group entry."
3501   (goto-char (point-min))
3502   (cond
3503    ((eq gnus-auto-select-subject 'best)
3504     (gnus-summary-best-unread-subject))
3505    ((eq gnus-auto-select-subject 'unread)
3506     (gnus-summary-first-unread-subject))
3507    ((eq gnus-auto-select-subject 'unseen)
3508     (gnus-summary-first-unseen-subject))
3509    ((eq gnus-auto-select-subject 'unseen-or-unread)
3510     (gnus-summary-first-unseen-or-unread-subject))
3511    ((eq gnus-auto-select-subject 'first)
3512     ;; Do nothing.
3513     )
3514    ((gnus-functionp gnus-auto-select-subject)
3515     (funcall gnus-auto-select-subject))))
3516
3517 (defun gnus-summary-prepare ()
3518   "Generate the summary buffer."
3519   (interactive)
3520   (let ((buffer-read-only nil))
3521     (erase-buffer)
3522     (setq gnus-newsgroup-data nil
3523           gnus-newsgroup-data-reverse nil)
3524     (gnus-run-hooks 'gnus-summary-generate-hook)
3525     ;; Generate the buffer, either with threads or without.
3526     (when gnus-newsgroup-headers
3527       (gnus-summary-prepare-threads
3528        (if gnus-show-threads
3529            (gnus-sort-gathered-threads
3530             (funcall gnus-summary-thread-gathering-function
3531                      (gnus-sort-threads
3532                       (gnus-cut-threads (gnus-make-threads)))))
3533          ;; Unthreaded display.
3534          (gnus-sort-articles gnus-newsgroup-headers))))
3535     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3536     ;; Call hooks for modifying summary buffer.
3537     (goto-char (point-min))
3538     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3539
3540 (defsubst gnus-general-simplify-subject (subject)
3541   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3542   (setq subject
3543         (cond
3544          ;; Truncate the subject.
3545          (gnus-simplify-subject-functions
3546           (gnus-map-function gnus-simplify-subject-functions subject))
3547          ((numberp gnus-summary-gather-subject-limit)
3548           (setq subject (gnus-simplify-subject-re subject))
3549           (if (> (length subject) gnus-summary-gather-subject-limit)
3550               (substring subject 0 gnus-summary-gather-subject-limit)
3551             subject))
3552          ;; Fuzzily simplify it.
3553          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3554           (gnus-simplify-subject-fuzzy subject))
3555          ;; Just remove the leading "Re:".
3556          (t
3557           (gnus-simplify-subject-re subject))))
3558
3559   (if (and gnus-summary-gather-exclude-subject
3560            (string-match gnus-summary-gather-exclude-subject subject))
3561       nil                         ; This article shouldn't be gathered
3562     subject))
3563
3564 (defun gnus-summary-simplify-subject-query ()
3565   "Query where the respool algorithm would put this article."
3566   (interactive)
3567   (gnus-summary-select-article)
3568   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3569
3570 (defun gnus-gather-threads-by-subject (threads)
3571   "Gather threads by looking at Subject headers."
3572   (if (not gnus-summary-make-false-root)
3573       threads
3574     (let ((hashtb (gnus-make-hashtable 1024))
3575           (prev threads)
3576           (result threads)
3577           subject hthread whole-subject)
3578       (while threads
3579         (setq subject (gnus-general-simplify-subject
3580                        (setq whole-subject (mail-header-subject
3581                                             (caar threads)))))
3582         (when subject
3583           (if (setq hthread (gnus-gethash subject hashtb))
3584               (progn
3585                 ;; We enter a dummy root into the thread, if we
3586                 ;; haven't done that already.
3587                 (unless (stringp (caar hthread))
3588                   (setcar hthread (list whole-subject (car hthread))))
3589                 ;; We add this new gathered thread to this gathered
3590                 ;; thread.
3591                 (setcdr (car hthread)
3592                         (nconc (cdar hthread) (list (car threads))))
3593                 ;; Remove it from the list of threads.
3594                 (setcdr prev (cdr threads))
3595                 (setq threads prev))
3596             ;; Enter this thread into the hash table.
3597             (gnus-sethash subject
3598                           (if gnus-summary-make-false-root-always
3599                               (progn
3600                                 ;; If you want a dummy root above all
3601                                 ;; threads...
3602                                 (setcar threads (list whole-subject
3603                                                       (car threads)))
3604                                 threads)
3605                             threads)
3606                           hashtb)))
3607         (setq prev threads)
3608         (setq threads (cdr threads)))
3609       result)))
3610
3611 (defun gnus-gather-threads-by-references (threads)
3612   "Gather threads by looking at References headers."
3613   (let ((idhashtb (gnus-make-hashtable 1024))
3614         (thhashtb (gnus-make-hashtable 1024))
3615         (prev threads)
3616         (result threads)
3617         ids references id gthread gid entered ref)
3618     (while threads
3619       (when (setq references (mail-header-references (caar threads)))
3620         (setq id (mail-header-id (caar threads))
3621               ids (inline (gnus-split-references references))
3622               entered nil)
3623         (while (setq ref (pop ids))
3624           (setq ids (delete ref ids))
3625           (if (not (setq gid (gnus-gethash ref idhashtb)))
3626               (progn
3627                 (gnus-sethash ref id idhashtb)
3628                 (gnus-sethash id threads thhashtb))
3629             (setq gthread (gnus-gethash gid thhashtb))
3630             (unless entered
3631               ;; We enter a dummy root into the thread, if we
3632               ;; haven't done that already.
3633               (unless (stringp (caar gthread))
3634                 (setcar gthread (list (mail-header-subject (caar gthread))
3635                                       (car gthread))))
3636               ;; We add this new gathered thread to this gathered
3637               ;; thread.
3638               (setcdr (car gthread)
3639                       (nconc (cdar gthread) (list (car threads)))))
3640             ;; Add it into the thread hash table.
3641             (gnus-sethash id gthread thhashtb)
3642             (setq entered t)
3643             ;; Remove it from the list of threads.
3644             (setcdr prev (cdr threads))
3645             (setq threads prev))))
3646       (setq prev threads)
3647       (setq threads (cdr threads)))
3648     result))
3649
3650 (defun gnus-sort-gathered-threads (threads)
3651   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3652   (let ((result threads))
3653     (while threads
3654       (when (stringp (caar threads))
3655         (setcdr (car threads)
3656                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3657       (setq threads (cdr threads)))
3658     result))
3659
3660 (defun gnus-thread-loop-p (root thread)
3661   "Say whether ROOT is in THREAD."
3662   (let ((stack (list thread))
3663         (infloop 0)
3664         th)
3665     (while (setq thread (pop stack))
3666       (setq th (cdr thread))
3667       (while (and th
3668                   (not (eq (caar th) root)))
3669         (pop th))
3670       (if th
3671           ;; We have found a loop.
3672           (let (ref-dep)
3673             (setcdr thread (delq (car th) (cdr thread)))
3674             (if (boundp (setq ref-dep (intern "none"
3675                                               gnus-newsgroup-dependencies)))
3676                 (setcdr (symbol-value ref-dep)
3677                         (nconc (cdr (symbol-value ref-dep))
3678                                (list (car th))))
3679               (set ref-dep (list nil (car th))))
3680             (setq infloop 1
3681                   stack nil))
3682         ;; Push all the subthreads onto the stack.
3683         (push (cdr thread) stack)))
3684     infloop))
3685
3686 (defun gnus-make-threads ()
3687   "Go through the dependency hashtb and find the roots.  Return all threads."
3688   (let (threads)
3689     (while (catch 'infloop
3690              (mapatoms
3691               (lambda (refs)
3692                 ;; Deal with self-referencing References loops.
3693                 (when (and (car (symbol-value refs))
3694                            (not (zerop
3695                                  (apply
3696                                   '+
3697                                   (mapcar
3698                                    (lambda (thread)
3699                                      (gnus-thread-loop-p
3700                                       (car (symbol-value refs)) thread))
3701                                    (cdr (symbol-value refs)))))))
3702                   (setq threads nil)
3703                   (throw 'infloop t))
3704                 (unless (car (symbol-value refs))
3705                   ;; These threads do not refer back to any other
3706                   ;; articles, so they're roots.
3707                   (setq threads (append (cdr (symbol-value refs)) threads))))
3708               gnus-newsgroup-dependencies)))
3709     threads))
3710
3711 ;; Build the thread tree.
3712 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3713   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3714
3715 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3716 if it was already present.
3717
3718 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3719 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3720 Message-IDs will be renamed to a unique Message-ID before being
3721 entered.
3722
3723 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3724   (let* ((id (mail-header-id header))
3725          (id-dep (and id (intern id dependencies)))
3726          parent-id ref ref-dep ref-header replaced)
3727     ;; Enter this `header' in the `dependencies' table.
3728     (cond
3729      ((not id-dep)
3730       (setq header nil))
3731      ;; The first two cases do the normal part: enter a new `header'
3732      ;; in the `dependencies' table.
3733      ((not (boundp id-dep))
3734       (set id-dep (list header)))
3735      ((null (car (symbol-value id-dep)))
3736       (setcar (symbol-value id-dep) header))
3737
3738      ;; From here the `header' was already present in the
3739      ;; `dependencies' table.
3740      (force-new
3741       ;; Overrides an existing entry;
3742       ;; just set the header part of the entry.
3743       (setcar (symbol-value id-dep) header)
3744       (setq replaced t))
3745
3746      ;; Renames the existing `header' to a unique Message-ID.
3747      ((not gnus-summary-ignore-duplicates)
3748       ;; An article with this Message-ID has already been seen.
3749       ;; We rename the Message-ID.
3750       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3751            (list header))
3752       (mail-header-set-id header id))
3753
3754      ;; The last case ignores an existing entry, except it adds any
3755      ;; additional Xrefs (in case the two articles came from different
3756      ;; servers.
3757      ;; Also sets `header' to `nil' meaning that the `dependencies'
3758      ;; table was *not* modified.
3759      (t
3760       (mail-header-set-xref
3761        (car (symbol-value id-dep))
3762        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3763                    "")
3764                (or (mail-header-xref header) "")))
3765       (setq header nil)))
3766
3767     (when (and header (not replaced))
3768       ;; First check that we are not creating a References loop.
3769       (setq parent-id (gnus-parent-id (mail-header-references header)))
3770       (setq ref parent-id)
3771       (while (and ref
3772                   (setq ref-dep (intern-soft ref dependencies))
3773                   (boundp ref-dep)
3774                   (setq ref-header (car (symbol-value ref-dep))))
3775         (if (string= id ref)
3776             ;; Yuk!  This is a reference loop.  Make the article be a
3777             ;; root article.
3778             (progn
3779               (mail-header-set-references (car (symbol-value id-dep)) "none")
3780               (setq ref nil)
3781               (setq parent-id nil))
3782           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3783       (setq ref-dep (intern (or parent-id "none") dependencies))
3784       (if (boundp ref-dep)
3785           (setcdr (symbol-value ref-dep)
3786                   (nconc (cdr (symbol-value ref-dep))
3787                          (list (symbol-value id-dep))))
3788         (set ref-dep (list nil (symbol-value id-dep)))))
3789     header))
3790
3791 (defun gnus-extract-message-id-from-in-reply-to (string)
3792   (if (string-match "<[^>]+>" string)
3793       (substring string (match-beginning 0) (match-end 0))
3794     nil))
3795
3796 (defun gnus-build-sparse-threads ()
3797   (let ((headers gnus-newsgroup-headers)
3798         (mail-parse-charset gnus-newsgroup-charset)
3799         (gnus-summary-ignore-duplicates t)
3800         header references generation relations
3801         subject child end new-child date)
3802     ;; First we create an alist of generations/relations, where
3803     ;; generations is how much we trust the relation, and the relation
3804     ;; is parent/child.
3805     (gnus-message 7 "Making sparse threads...")
3806     (save-excursion
3807       (nnheader-set-temp-buffer " *gnus sparse threads*")
3808       (while (setq header (pop headers))
3809         (when (and (setq references (mail-header-references header))
3810                    (not (string= references "")))
3811           (insert references)
3812           (setq child (mail-header-id header)
3813                 subject (mail-header-subject header)
3814                 date (mail-header-date header)
3815                 generation 0)
3816           (while (search-backward ">" nil t)
3817             (setq end (1+ (point)))
3818             (when (search-backward "<" nil t)
3819               (setq new-child (buffer-substring (point) end))
3820               (push (list (incf generation)
3821                           child (setq child new-child)
3822                           subject date)
3823                     relations)))
3824           (when child
3825             (push (list (1+ generation) child nil subject) relations))
3826           (erase-buffer)))
3827       (kill-buffer (current-buffer)))
3828     ;; Sort over trustworthiness.
3829     (mapcar
3830      (lambda (relation)
3831        (when (gnus-dependencies-add-header
3832               (make-full-mail-header
3833                gnus-reffed-article-number
3834                (nth 3 relation) "" (or (nth 4 relation) "")
3835                (nth 1 relation)
3836                (or (nth 2 relation) "") 0 0 "")
3837               gnus-newsgroup-dependencies nil)
3838          (push gnus-reffed-article-number gnus-newsgroup-limit)
3839          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3840          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3841                gnus-newsgroup-reads)
3842          (decf gnus-reffed-article-number)))
3843      (sort relations 'car-less-than-car))
3844     (gnus-message 7 "Making sparse threads...done")))
3845
3846 (defun gnus-build-old-threads ()
3847   ;; Look at all the articles that refer back to old articles, and
3848   ;; fetch the headers for the articles that aren't there.  This will
3849   ;; build complete threads - if the roots haven't been expired by the
3850   ;; server, that is.
3851   (let ((mail-parse-charset gnus-newsgroup-charset)
3852         id heads)
3853     (mapatoms
3854      (lambda (refs)
3855        (when (not (car (symbol-value refs)))
3856          (setq heads (cdr (symbol-value refs)))
3857          (while heads
3858            (if (memq (mail-header-number (caar heads))
3859                      gnus-newsgroup-dormant)
3860                (setq heads (cdr heads))
3861              (setq id (symbol-name refs))
3862              (while (and (setq id (gnus-build-get-header id))
3863                          (not (car (gnus-id-to-thread id)))))
3864              (setq heads nil)))))
3865      gnus-newsgroup-dependencies)))
3866
3867 ;; This function has to be called with point after the article number
3868 ;; on the beginning of the line.
3869 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3870   (let ((eol (gnus-point-at-eol))
3871         (buffer (current-buffer))
3872         header references in-reply-to)
3873
3874     ;; overview: [num subject from date id refs chars lines misc]
3875     (unwind-protect
3876         (let (x)
3877           (narrow-to-region (point) eol)
3878           (unless (eobp)
3879             (forward-char))
3880
3881           (setq header
3882                 (make-full-mail-header
3883                  number                 ; number
3884                  (condition-case ()     ; subject
3885                      (funcall gnus-decode-encoded-word-function
3886                               (setq x (nnheader-nov-field)))
3887                    (error x))
3888                  (condition-case ()     ; from
3889                      (funcall gnus-decode-encoded-word-function
3890                               (setq x (nnheader-nov-field)))
3891                    (error x))
3892                  (nnheader-nov-field)   ; date
3893                  (nnheader-nov-read-message-id) ; id
3894                  (setq references (nnheader-nov-field)) ; refs
3895                  (nnheader-nov-read-integer) ; chars
3896                  (nnheader-nov-read-integer) ; lines
3897                  (unless (eobp)
3898                    (if (looking-at "Xref: ")
3899                        (goto-char (match-end 0)))
3900                    (nnheader-nov-field)) ; Xref
3901                  (nnheader-nov-parse-extra)))) ; extra
3902
3903       (widen))
3904
3905     (when (and (string= references "")
3906                (setq in-reply-to (mail-header-extra header))
3907                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3908       (mail-header-set-references
3909        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3910
3911     (when gnus-alter-header-function
3912       (funcall gnus-alter-header-function header))
3913     (gnus-dependencies-add-header header dependencies force-new)))
3914
3915 (defun gnus-build-get-header (id)
3916   "Look through the buffer of NOV lines and find the header to ID.
3917 Enter this line into the dependencies hash table, and return
3918 the id of the parent article (if any)."
3919   (let ((deps gnus-newsgroup-dependencies)
3920         found header)
3921     (prog1
3922         (save-excursion
3923           (set-buffer nntp-server-buffer)
3924           (let ((case-fold-search nil))
3925             (goto-char (point-min))
3926             (while (and (not found)
3927                         (search-forward id nil t))
3928               (beginning-of-line)
3929               (setq found (looking-at
3930                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3931                                    (regexp-quote id))))
3932               (or found (beginning-of-line 2)))
3933             (when found
3934               (beginning-of-line)
3935               (and
3936                (setq header (gnus-nov-parse-line
3937                              (read (current-buffer)) deps))
3938                (gnus-parent-id (mail-header-references header))))))
3939       (when header
3940         (let ((number (mail-header-number header)))
3941           (push number gnus-newsgroup-limit)
3942           (push header gnus-newsgroup-headers)
3943           (if (memq number gnus-newsgroup-unselected)
3944               (progn
3945                 (setq gnus-newsgroup-unreads
3946                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3947                                                number))
3948                 (setq gnus-newsgroup-unselected
3949                       (delq number gnus-newsgroup-unselected)))
3950             (push number gnus-newsgroup-ancient)))))))
3951
3952 (defun gnus-build-all-threads ()
3953   "Read all the headers."
3954   (let ((gnus-summary-ignore-duplicates t)
3955         (mail-parse-charset gnus-newsgroup-charset)
3956         (dependencies gnus-newsgroup-dependencies)
3957         header article)
3958     (save-excursion
3959       (set-buffer nntp-server-buffer)
3960       (let ((case-fold-search nil))
3961         (goto-char (point-min))
3962         (while (not (eobp))
3963           (ignore-errors
3964             (setq article (read (current-buffer))
3965                   header (gnus-nov-parse-line article dependencies)))
3966           (when header
3967             (save-excursion
3968               (set-buffer gnus-summary-buffer)
3969               (push header gnus-newsgroup-headers)
3970               (if (memq (setq article (mail-header-number header))
3971                         gnus-newsgroup-unselected)
3972                   (progn
3973                     (setq gnus-newsgroup-unreads
3974                           (gnus-add-to-sorted-list
3975                            gnus-newsgroup-unreads article))
3976                     (setq gnus-newsgroup-unselected
3977                           (delq article gnus-newsgroup-unselected)))
3978                 (push article gnus-newsgroup-ancient)))
3979             (forward-line 1)))))))
3980
3981 (defun gnus-summary-update-article-line (article header)
3982   "Update the line for ARTICLE using HEADERS."
3983   (let* ((id (mail-header-id header))
3984          (thread (gnus-id-to-thread id)))
3985     (unless thread
3986       (error "Article in no thread"))
3987     ;; Update the thread.
3988     (setcar thread header)
3989     (gnus-summary-goto-subject article)
3990     (let* ((datal (gnus-data-find-list article))
3991            (data (car datal))
3992            (length (when (cdr datal)
3993                      (- (gnus-data-pos data)
3994                         (gnus-data-pos (cadr datal)))))
3995            (buffer-read-only nil)
3996            (level (gnus-summary-thread-level)))
3997       (gnus-delete-line)
3998       (gnus-summary-insert-line
3999        header level nil 
4000        (memq article gnus-newsgroup-undownloaded)
4001        (gnus-article-mark article)
4002        (memq article gnus-newsgroup-replied)
4003        (memq article gnus-newsgroup-expirable)
4004        ;; Only insert the Subject string when it's different
4005        ;; from the previous Subject string.
4006        (if (and
4007             gnus-show-threads
4008             (gnus-subject-equal
4009              (condition-case ()
4010                  (mail-header-subject
4011                   (gnus-data-header
4012                    (cadr
4013                     (gnus-data-find-list
4014                      article
4015                      (gnus-data-list t)))))
4016                ;; Error on the side of excessive subjects.
4017                (error ""))
4018              (mail-header-subject header)))
4019            ""
4020          (mail-header-subject header))
4021        nil (cdr (assq article gnus-newsgroup-scored))
4022        (memq article gnus-newsgroup-processable))
4023       (when length
4024         (gnus-data-update-list
4025          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4026
4027 (defun gnus-summary-update-article (article &optional iheader)
4028   "Update ARTICLE in the summary buffer."
4029   (set-buffer gnus-summary-buffer)
4030   (let* ((header (gnus-summary-article-header article))
4031          (id (mail-header-id header))
4032          (data (gnus-data-find article))
4033          (thread (gnus-id-to-thread id))
4034          (references (mail-header-references header))
4035          (parent
4036           (gnus-id-to-thread
4037            (or (gnus-parent-id
4038                 (when (and references
4039                            (not (equal "" references)))
4040                   references))
4041                "none")))
4042          (buffer-read-only nil)
4043          (old (car thread)))
4044     (when thread
4045       (unless iheader
4046         (setcar thread nil)
4047         (when parent
4048           (delq thread parent)))
4049       (if (gnus-summary-insert-subject id header)
4050           ;; Set the (possibly) new article number in the data structure.
4051           (gnus-data-set-number data (gnus-id-to-article id))
4052         (setcar thread old)
4053         nil))))
4054
4055 (defun gnus-rebuild-thread (id &optional line)
4056   "Rebuild the thread containing ID.
4057 If LINE, insert the rebuilt thread starting on line LINE."
4058   (let ((buffer-read-only nil)
4059         old-pos current thread data)
4060     (if (not gnus-show-threads)
4061         (setq thread (list (car (gnus-id-to-thread id))))
4062       ;; Get the thread this article is part of.
4063       (setq thread (gnus-remove-thread id)))
4064     (setq old-pos (gnus-point-at-bol))
4065     (setq current (save-excursion
4066                     (and (re-search-backward "[\r\n]" nil t)
4067                          (gnus-summary-article-number))))
4068     ;; If this is a gathered thread, we have to go some re-gathering.
4069     (when (stringp (car thread))
4070       (let ((subject (car thread))
4071             roots thr)
4072         (setq thread (cdr thread))
4073         (while thread
4074           (unless (memq (setq thr (gnus-id-to-thread
4075                                    (gnus-root-id
4076                                     (mail-header-id (caar thread)))))
4077                         roots)
4078             (push thr roots))
4079           (setq thread (cdr thread)))
4080         ;; We now have all (unique) roots.
4081         (if (= (length roots) 1)
4082             ;; All the loose roots are now one solid root.
4083             (setq thread (car roots))
4084           (setq thread (cons subject (gnus-sort-threads roots))))))
4085     (let (threads)
4086       ;; We then insert this thread into the summary buffer.
4087       (when line
4088         (goto-char (point-min))
4089         (forward-line (1- line)))
4090       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4091         (if gnus-show-threads
4092             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4093           (gnus-summary-prepare-unthreaded thread))
4094         (setq data (nreverse gnus-newsgroup-data))
4095         (setq threads gnus-newsgroup-threads))
4096       ;; We splice the new data into the data structure.
4097       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4098       ;;!!! then we want to insert at the beginning of the buffer.
4099       ;;!!! That happens to be true with Gnus now, but that may
4100       ;;!!! change in the future.  Perhaps.
4101       (gnus-data-enter-list
4102        (if line nil current) data (- (point) old-pos))
4103       (setq gnus-newsgroup-threads
4104             (nconc threads gnus-newsgroup-threads))
4105       (gnus-data-compute-positions))))
4106
4107 (defun gnus-number-to-header (number)
4108   "Return the header for article NUMBER."
4109   (let ((headers gnus-newsgroup-headers))
4110     (while (and headers
4111                 (not (= number (mail-header-number (car headers)))))
4112       (pop headers))
4113     (when headers
4114       (car headers))))
4115
4116 (defun gnus-parent-headers (in-headers &optional generation)
4117   "Return the headers of the GENERATIONeth parent of HEADERS."
4118   (unless generation
4119     (setq generation 1))
4120   (let ((parent t)
4121         (headers in-headers)
4122         references)
4123     (while (and parent
4124                 (not (zerop generation))
4125                 (setq references (mail-header-references headers)))
4126       (setq headers (if (and references
4127                              (setq parent (gnus-parent-id references)))
4128                         (car (gnus-id-to-thread parent))
4129                       nil))
4130       (decf generation))
4131     (and (not (eq headers in-headers))
4132          headers)))
4133
4134 (defun gnus-id-to-thread (id)
4135   "Return the (sub-)thread where ID appears."
4136   (gnus-gethash id gnus-newsgroup-dependencies))
4137
4138 (defun gnus-id-to-article (id)
4139   "Return the article number of ID."
4140   (let ((thread (gnus-id-to-thread id)))
4141     (when (and thread
4142                (car thread))
4143       (mail-header-number (car thread)))))
4144
4145 (defun gnus-id-to-header (id)
4146   "Return the article headers of ID."
4147   (car (gnus-id-to-thread id)))
4148
4149 (defun gnus-article-displayed-root-p (article)
4150   "Say whether ARTICLE is a root(ish) article."
4151   (let ((level (gnus-summary-thread-level article))
4152         (refs (mail-header-references  (gnus-summary-article-header article)))
4153         particle)
4154     (cond
4155      ((null level) nil)
4156      ((zerop level) t)
4157      ((null refs) t)
4158      ((null (gnus-parent-id refs)) t)
4159      ((and (= 1 level)
4160            (null (setq particle (gnus-id-to-article
4161                                  (gnus-parent-id refs))))
4162            (null (gnus-summary-thread-level particle)))))))
4163
4164 (defun gnus-root-id (id)
4165   "Return the id of the root of the thread where ID appears."
4166   (let (last-id prev)
4167     (while (and id (setq prev (car (gnus-id-to-thread id))))
4168       (setq last-id id
4169             id (gnus-parent-id (mail-header-references prev))))
4170     last-id))
4171
4172 (defun gnus-articles-in-thread (thread)
4173   "Return the list of articles in THREAD."
4174   (cons (mail-header-number (car thread))
4175         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4176
4177 (defun gnus-remove-thread (id &optional dont-remove)
4178   "Remove the thread that has ID in it."
4179   (let (headers thread last-id)
4180     ;; First go up in this thread until we find the root.
4181     (setq last-id (gnus-root-id id)
4182           headers (message-flatten-list (gnus-id-to-thread last-id)))
4183     ;; We have now found the real root of this thread.  It might have
4184     ;; been gathered into some loose thread, so we have to search
4185     ;; through the threads to find the thread we wanted.
4186     (let ((threads gnus-newsgroup-threads)
4187           sub)
4188       (while threads
4189         (setq sub (car threads))
4190         (if (stringp (car sub))
4191             ;; This is a gathered thread, so we look at the roots
4192             ;; below it to find whether this article is in this
4193             ;; gathered root.
4194             (progn
4195               (setq sub (cdr sub))
4196               (while sub
4197                 (when (member (caar sub) headers)
4198                   (setq thread (car threads)
4199                         threads nil
4200                         sub nil))
4201                 (setq sub (cdr sub))))
4202           ;; It's an ordinary thread, so we check it.
4203           (when (eq (car sub) (car headers))
4204             (setq thread sub
4205                   threads nil)))
4206         (setq threads (cdr threads)))
4207       ;; If this article is in no thread, then it's a root.
4208       (if thread
4209           (unless dont-remove
4210             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4211         (setq thread (gnus-id-to-thread last-id)))
4212       (when thread
4213         (prog1
4214             thread                      ; We return this thread.
4215           (unless dont-remove
4216             (if (stringp (car thread))
4217                 (progn
4218                   ;; If we use dummy roots, then we have to remove the
4219                   ;; dummy root as well.
4220                   (when (eq gnus-summary-make-false-root 'dummy)
4221                     ;; We go to the dummy root by going to
4222                     ;; the first sub-"thread", and then one line up.
4223                     (gnus-summary-goto-article
4224                      (mail-header-number (caadr thread)))
4225                     (forward-line -1)
4226                     (gnus-delete-line)
4227                     (gnus-data-compute-positions))
4228                   (setq thread (cdr thread))
4229                   (while thread
4230                     (gnus-remove-thread-1 (car thread))
4231                     (setq thread (cdr thread))))
4232               (gnus-remove-thread-1 thread))))))))
4233
4234 (defun gnus-remove-thread-1 (thread)
4235   "Remove the thread THREAD recursively."
4236   (let ((number (mail-header-number (pop thread)))
4237         d)
4238     (setq thread (reverse thread))
4239     (while thread
4240       (gnus-remove-thread-1 (pop thread)))
4241     (when (setq d (gnus-data-find number))
4242       (goto-char (gnus-data-pos d))
4243       (gnus-summary-show-thread)
4244       (gnus-data-remove
4245        number
4246        (- (gnus-point-at-bol)
4247           (prog1
4248               (1+ (gnus-point-at-eol))
4249             (gnus-delete-line)))))))
4250
4251 (defun gnus-sort-threads-1 (threads func)
4252   (sort (mapcar (lambda (thread)
4253                   (cons (car thread)
4254                         (and (cdr thread)
4255                              (gnus-sort-threads-1 (cdr thread) func))))
4256                 threads) func))
4257
4258 (defun gnus-sort-threads (threads)
4259   "Sort THREADS."
4260   (if (not gnus-thread-sort-functions)
4261       threads
4262     (gnus-message 8 "Sorting threads...")
4263     (let ((max-lisp-eval-depth 5000))
4264       (prog1 (gnus-sort-threads-1
4265          threads
4266          (gnus-make-sort-function gnus-thread-sort-functions))
4267         (gnus-message 8 "Sorting threads...done")))))
4268
4269 (defun gnus-sort-articles (articles)
4270   "Sort ARTICLES."
4271   (when gnus-article-sort-functions
4272     (gnus-message 7 "Sorting articles...")
4273     (prog1
4274         (setq gnus-newsgroup-headers
4275               (sort articles (gnus-make-sort-function
4276                               gnus-article-sort-functions)))
4277       (gnus-message 7 "Sorting articles...done"))))
4278
4279 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4280 (defmacro gnus-thread-header (thread)
4281   "Return header of first article in THREAD.
4282 Note that THREAD must never, ever be anything else than a variable -
4283 using some other form will lead to serious barfage."
4284   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4285   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4286   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4287         (vector thread) 2))
4288
4289 (defsubst gnus-article-sort-by-number (h1 h2)
4290   "Sort articles by article number."
4291   (< (mail-header-number h1)
4292      (mail-header-number h2)))
4293
4294 (defun gnus-thread-sort-by-number (h1 h2)
4295   "Sort threads by root article number."
4296   (gnus-article-sort-by-number
4297    (gnus-thread-header h1) (gnus-thread-header h2)))
4298
4299 (defsubst gnus-article-sort-by-random (h1 h2)
4300   "Sort articles by article number."
4301   (zerop (random 2)))
4302
4303 (defun gnus-thread-sort-by-random (h1 h2)
4304   "Sort threads by root article number."
4305   (gnus-article-sort-by-random
4306    (gnus-thread-header h1) (gnus-thread-header h2)))
4307
4308 (defsubst gnus-article-sort-by-lines (h1 h2)
4309   "Sort articles by article Lines header."
4310   (< (mail-header-lines h1)
4311      (mail-header-lines h2)))
4312
4313 (defun gnus-thread-sort-by-lines (h1 h2)
4314   "Sort threads by root article Lines header."
4315   (gnus-article-sort-by-lines
4316    (gnus-thread-header h1) (gnus-thread-header h2)))
4317
4318 (defsubst gnus-article-sort-by-chars (h1 h2)
4319   "Sort articles by octet length."
4320   (< (mail-header-chars h1)
4321      (mail-header-chars h2)))
4322
4323 (defun gnus-thread-sort-by-chars (h1 h2)
4324   "Sort threads by root article octet length."
4325   (gnus-article-sort-by-chars
4326    (gnus-thread-header h1) (gnus-thread-header h2)))
4327
4328 (defsubst gnus-article-sort-by-author (h1 h2)
4329   "Sort articles by root author."
4330   (string-lessp
4331    (let ((extract (funcall
4332                    gnus-extract-address-components
4333                    (mail-header-from h1))))
4334      (or (car extract) (cadr extract) ""))
4335    (let ((extract (funcall
4336                    gnus-extract-address-components
4337                    (mail-header-from h2))))
4338      (or (car extract) (cadr extract) ""))))
4339
4340 (defun gnus-thread-sort-by-author (h1 h2)
4341   "Sort threads by root author."
4342   (gnus-article-sort-by-author
4343    (gnus-thread-header h1)  (gnus-thread-header h2)))
4344
4345 (defsubst gnus-article-sort-by-subject (h1 h2)
4346   "Sort articles by root subject."
4347   (string-lessp
4348    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4349    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4350
4351 (defun gnus-thread-sort-by-subject (h1 h2)
4352   "Sort threads by root subject."
4353   (gnus-article-sort-by-subject
4354    (gnus-thread-header h1) (gnus-thread-header h2)))
4355
4356 (defsubst gnus-article-sort-by-date (h1 h2)
4357   "Sort articles by root article date."
4358   (time-less-p
4359    (gnus-date-get-time (mail-header-date h1))
4360    (gnus-date-get-time (mail-header-date h2))))
4361
4362 (defun gnus-thread-sort-by-date (h1 h2)
4363   "Sort threads by root article date."
4364   (gnus-article-sort-by-date
4365    (gnus-thread-header h1) (gnus-thread-header h2)))
4366
4367 (defsubst gnus-article-sort-by-score (h1 h2)
4368   "Sort articles by root article score.
4369 Unscored articles will be counted as having a score of zero."
4370   (> (or (cdr (assq (mail-header-number h1)
4371                     gnus-newsgroup-scored))
4372          gnus-summary-default-score 0)
4373      (or (cdr (assq (mail-header-number h2)
4374                     gnus-newsgroup-scored))
4375          gnus-summary-default-score 0)))
4376
4377 (defun gnus-thread-sort-by-score (h1 h2)
4378   "Sort threads by root article score."
4379   (gnus-article-sort-by-score
4380    (gnus-thread-header h1) (gnus-thread-header h2)))
4381
4382 (defun gnus-thread-sort-by-total-score (h1 h2)
4383   "Sort threads by the sum of all scores in the thread.
4384 Unscored articles will be counted as having a score of zero."
4385   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4386
4387 (defun gnus-thread-total-score (thread)
4388   ;; This function find the total score of THREAD.
4389   (cond
4390    ((null thread)
4391     0)
4392    ((consp thread)
4393     (if (stringp (car thread))
4394         (apply gnus-thread-score-function 0
4395                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4396       (gnus-thread-total-score-1 thread)))
4397    (t
4398     (gnus-thread-total-score-1 (list thread)))))
4399
4400 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4401   "Sort threads such that the thread with the most recently arrived article comes first."
4402   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4403
4404 (defun gnus-thread-highest-number (thread)
4405   "Return the highest article number in THREAD."
4406   (apply 'max (mapcar (lambda (header)
4407                         (mail-header-number header))
4408                       (message-flatten-list thread))))
4409
4410 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4411   "Sort threads such that the thread with the most recently dated article comes first."
4412   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4413
4414 (defun gnus-thread-latest-date (thread)
4415   "Return the highest article date in THREAD."
4416   (let ((previous-time 0))
4417     (apply 'max
4418            (mapcar
4419             (lambda (header)
4420               (setq previous-time
4421                     (time-to-seconds
4422                      (condition-case ()
4423                          (mail-header-parse-date (mail-header-date header))
4424                        (error previous-time)))))
4425             (sort
4426              (message-flatten-list thread)
4427              (lambda (h1 h2)
4428                (< (mail-header-number h1)
4429                   (mail-header-number h2))))))))
4430
4431 (defun gnus-thread-total-score-1 (root)
4432   ;; This function find the total score of the thread below ROOT.
4433   (setq root (car root))
4434   (apply gnus-thread-score-function
4435          (or (append
4436               (mapcar 'gnus-thread-total-score
4437                       (cdr (gnus-id-to-thread (mail-header-id root))))
4438               (when (> (mail-header-number root) 0)
4439                 (list (or (cdr (assq (mail-header-number root)
4440                                      gnus-newsgroup-scored))
4441                           gnus-summary-default-score 0))))
4442              (list gnus-summary-default-score)
4443              '(0))))
4444
4445 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4446 (defvar gnus-tmp-prev-subject nil)
4447 (defvar gnus-tmp-false-parent nil)
4448 (defvar gnus-tmp-root-expunged nil)
4449 (defvar gnus-tmp-dummy-line nil)
4450
4451 (eval-when-compile (defvar gnus-tmp-header))
4452 (defun gnus-extra-header (type &optional header)
4453   "Return the extra header of TYPE."
4454   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4455       ""))
4456
4457 (defvar gnus-tmp-thread-tree-header-string "")
4458
4459 (defcustom gnus-sum-thread-tree-root "> "
4460   "With %B spec, used for the root of a thread.
4461 If nil, use subject instead."
4462   :type 'string
4463   :group 'gnus-thread)
4464 (defcustom gnus-sum-thread-tree-single-indent ""
4465   "With %B spec, used for a thread with just one message.
4466 If nil, use subject instead."
4467   :type 'string
4468   :group 'gnus-thread)
4469 (defcustom gnus-sum-thread-tree-vertical "| "
4470   "With %B spec, used for drawing a vertical line."
4471   :type 'string
4472   :group 'gnus-thread)
4473 (defcustom gnus-sum-thread-tree-indent "  "
4474   "With %B spec, used for indenting."
4475   :type 'string
4476   :group 'gnus-thread)
4477 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4478   "With %B spec, used for a leaf with brothers."
4479   :type 'string
4480   :group 'gnus-thread)
4481 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4482   "With %B spec, used for a leaf without brothers."
4483   :type 'string
4484   :group 'gnus-thread)
4485
4486 (defun gnus-summary-prepare-threads (threads)
4487   "Prepare summary buffer from THREADS and indentation LEVEL.
4488 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4489 or a straight list of headers."
4490   (gnus-message 7 "Generating summary...")
4491
4492   (setq gnus-newsgroup-threads threads)
4493   (beginning-of-line)
4494
4495   (let ((gnus-tmp-level 0)
4496         (default-score (or gnus-summary-default-score 0))
4497         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4498         (building-line-count gnus-summary-display-while-building)
4499         (building-count (integerp gnus-summary-display-while-building))
4500         thread number subject stack state gnus-tmp-gathered beg-match
4501         new-roots gnus-tmp-new-adopts thread-end simp-subject
4502         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4503         gnus-tmp-replied gnus-tmp-subject-or-nil
4504         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4505         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4506         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4507         tree-stack)
4508
4509     (setq gnus-tmp-prev-subject nil)
4510
4511     (if (vectorp (car threads))
4512         ;; If this is a straight (sic) list of headers, then a
4513         ;; threaded summary display isn't required, so we just create
4514         ;; an unthreaded one.
4515         (gnus-summary-prepare-unthreaded threads)
4516
4517       ;; Do the threaded display.
4518
4519       (if gnus-summary-display-while-building
4520           (switch-to-buffer (buffer-name)))
4521       (while (or threads stack gnus-tmp-new-adopts new-roots)
4522
4523         (if (and (= gnus-tmp-level 0)
4524                  (or (not stack)
4525                      (= (caar stack) 0))
4526                  (not gnus-tmp-false-parent)
4527                  (or gnus-tmp-new-adopts new-roots))
4528             (if gnus-tmp-new-adopts
4529                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4530                       thread (list (car gnus-tmp-new-adopts))
4531                       gnus-tmp-header (caar thread)
4532                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4533               (when new-roots
4534                 (setq thread (list (car new-roots))
4535                       gnus-tmp-header (caar thread)
4536                       new-roots (cdr new-roots))))
4537
4538           (if threads
4539               ;; If there are some threads, we do them before the
4540               ;; threads on the stack.
4541               (setq thread threads
4542                     gnus-tmp-header (caar thread))
4543             ;; There were no current threads, so we pop something off
4544             ;; the stack.
4545             (setq state (car stack)
4546                   gnus-tmp-level (car state)
4547                   tree-stack (cadr state)
4548                   thread (caddr state)
4549                   stack (cdr stack)
4550                   gnus-tmp-header (caar thread))))
4551
4552         (setq gnus-tmp-false-parent nil)
4553         (setq gnus-tmp-root-expunged nil)
4554         (setq thread-end nil)
4555
4556         (if (stringp gnus-tmp-header)
4557             ;; The header is a dummy root.
4558             (cond
4559              ((eq gnus-summary-make-false-root 'adopt)
4560               ;; We let the first article adopt the rest.
4561               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4562                                                (cddar thread)))
4563               (setq gnus-tmp-gathered
4564                     (nconc (mapcar
4565                             (lambda (h) (mail-header-number (car h)))
4566                             (cddar thread))
4567                            gnus-tmp-gathered))
4568               (setq thread (cons (list (caar thread)
4569                                        (cadar thread))
4570                                  (cdr thread)))
4571               (setq gnus-tmp-level -1
4572                     gnus-tmp-false-parent t))
4573              ((eq gnus-summary-make-false-root 'empty)
4574               ;; We print adopted articles with empty subject fields.
4575               (setq gnus-tmp-gathered
4576                     (nconc (mapcar
4577                             (lambda (h) (mail-header-number (car h)))
4578                             (cddar thread))
4579                            gnus-tmp-gathered))
4580               (setq gnus-tmp-level -1))
4581              ((eq gnus-summary-make-false-root 'dummy)
4582               ;; We remember that we probably want to output a dummy
4583               ;; root.
4584               (setq gnus-tmp-dummy-line gnus-tmp-header)
4585               (setq gnus-tmp-prev-subject gnus-tmp-header))
4586              (t
4587               ;; We do not make a root for the gathered
4588               ;; sub-threads at all.
4589               (setq gnus-tmp-level -1)))
4590
4591           (setq number (mail-header-number gnus-tmp-header)
4592                 subject (mail-header-subject gnus-tmp-header)
4593                 simp-subject (gnus-simplify-subject-fully subject))
4594
4595           (cond
4596            ;; If the thread has changed subject, we might want to make
4597            ;; this subthread into a root.
4598            ((and (null gnus-thread-ignore-subject)
4599                  (not (zerop gnus-tmp-level))
4600                  gnus-tmp-prev-subject
4601                  (not (string= gnus-tmp-prev-subject simp-subject)))
4602             (setq new-roots (nconc new-roots (list (car thread)))
4603                   thread-end t
4604                   gnus-tmp-header nil))
4605            ;; If the article lies outside the current limit,
4606            ;; then we do not display it.
4607            ((not (memq number gnus-newsgroup-limit))
4608             (setq gnus-tmp-gathered
4609                   (nconc (mapcar
4610                           (lambda (h) (mail-header-number (car h)))
4611                           (cdar thread))
4612                          gnus-tmp-gathered))
4613             (setq gnus-tmp-new-adopts (if (cdar thread)
4614                                           (append gnus-tmp-new-adopts
4615                                                   (cdar thread))
4616                                         gnus-tmp-new-adopts)
4617                   thread-end t
4618                   gnus-tmp-header nil)
4619             (when (zerop gnus-tmp-level)
4620               (setq gnus-tmp-root-expunged t)))
4621            ;; Perhaps this article is to be marked as read?
4622            ((and gnus-summary-mark-below
4623                  (< (or (cdr (assq number gnus-newsgroup-scored))
4624                         default-score)
4625                     gnus-summary-mark-below)
4626                  ;; Don't touch sparse articles.
4627                  (not (gnus-summary-article-sparse-p number))
4628                  (not (gnus-summary-article-ancient-p number)))
4629             (setq gnus-newsgroup-unreads
4630                   (delq number gnus-newsgroup-unreads))
4631             (if gnus-newsgroup-auto-expire
4632                 (setq gnus-newsgroup-expirable
4633                       (gnus-add-to-sorted-list
4634                        gnus-newsgroup-expirable number))
4635               (push (cons number gnus-low-score-mark)
4636                     gnus-newsgroup-reads))))
4637
4638           (when gnus-tmp-header
4639             ;; We may have an old dummy line to output before this
4640             ;; article.
4641             (when (and gnus-tmp-dummy-line
4642                        (gnus-subject-equal
4643                         gnus-tmp-dummy-line
4644                         (mail-header-subject gnus-tmp-header)))
4645               (gnus-summary-insert-dummy-line
4646                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4647               (setq gnus-tmp-dummy-line nil))
4648
4649             ;; Compute the mark.
4650             (setq gnus-tmp-unread (gnus-article-mark number))
4651
4652             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4653                                   gnus-tmp-header gnus-tmp-level)
4654                   gnus-newsgroup-data)
4655
4656             ;; Actually insert the line.
4657             (setq
4658              gnus-tmp-subject-or-nil
4659              (cond
4660               ((and gnus-thread-ignore-subject
4661                     gnus-tmp-prev-subject
4662                     (not (string= gnus-tmp-prev-subject simp-subject)))
4663                subject)
4664               ((zerop gnus-tmp-level)
4665                (if (and (eq gnus-summary-make-false-root 'empty)
4666                         (memq number gnus-tmp-gathered)
4667                         gnus-tmp-prev-subject
4668                         (string= gnus-tmp-prev-subject simp-subject))
4669                    gnus-summary-same-subject
4670                  subject))
4671               (t gnus-summary-same-subject)))
4672             (if (and (eq gnus-summary-make-false-root 'adopt)
4673                      (= gnus-tmp-level 1)
4674                      (memq number gnus-tmp-gathered))
4675                 (setq gnus-tmp-opening-bracket ?\<
4676                       gnus-tmp-closing-bracket ?\>)
4677               (setq gnus-tmp-opening-bracket ?\[
4678                     gnus-tmp-closing-bracket ?\]))
4679             (setq
4680              gnus-tmp-indentation
4681              (aref gnus-thread-indent-array gnus-tmp-level)
4682              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4683              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4684                                 gnus-summary-default-score 0)
4685              gnus-tmp-score-char
4686              (if (or (null gnus-summary-default-score)
4687                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4688                          gnus-summary-zcore-fuzz))
4689                  ?                      ;Whitespace
4690                (if (< gnus-tmp-score gnus-summary-default-score)
4691                    gnus-score-below-mark gnus-score-over-mark))
4692              gnus-tmp-replied
4693              (cond ((memq number gnus-newsgroup-processable)
4694                     gnus-process-mark)
4695                    ((memq number gnus-newsgroup-cached)
4696                     gnus-cached-mark)
4697                    ((memq number gnus-newsgroup-replied)
4698                     gnus-replied-mark)
4699                    ((memq number gnus-newsgroup-forwarded)
4700                     gnus-forwarded-mark)
4701                    ((memq number gnus-newsgroup-saved)
4702                     gnus-saved-mark)
4703                    ((memq number gnus-newsgroup-recent)
4704                     gnus-recent-mark)
4705                    ((memq number gnus-newsgroup-unseen)
4706                     gnus-unseen-mark)
4707                    (t gnus-no-mark))
4708              gnus-tmp-downloaded
4709              (cond ((memq number gnus-newsgroup-undownloaded) 
4710                     gnus-undownloaded-mark)
4711                    (gnus-newsgroup-agentized
4712                     gnus-downloaded-mark)
4713                    (t
4714                     gnus-no-mark))
4715              gnus-tmp-from (mail-header-from gnus-tmp-header)
4716              gnus-tmp-name
4717              (cond
4718               ((string-match "<[^>]+> *$" gnus-tmp-from)
4719                (setq beg-match (match-beginning 0))
4720                (or (and (string-match "^\".+\"" gnus-tmp-from)
4721                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4722                    (substring gnus-tmp-from 0 beg-match)))
4723               ((string-match "(.+)" gnus-tmp-from)
4724                (substring gnus-tmp-from
4725                           (1+ (match-beginning 0)) (1- (match-end 0))))
4726               (t gnus-tmp-from))
4727              gnus-tmp-thread-tree-header-string
4728              (cond
4729               ((not gnus-show-threads) "")
4730               ((zerop gnus-tmp-level)
4731                (if (cdar thread)
4732                    (or gnus-sum-thread-tree-root subject)
4733                  (or gnus-sum-thread-tree-single-indent subject)))
4734               (t
4735                (concat (apply 'concat
4736                               (mapcar (lambda (item)
4737                                         (if (= item 1)
4738                                             gnus-sum-thread-tree-vertical
4739                                           gnus-sum-thread-tree-indent))
4740                                       (cdr (reverse tree-stack))))
4741                        (if (nth 1 thread)
4742                            gnus-sum-thread-tree-leaf-with-other
4743                          gnus-sum-thread-tree-single-leaf)))))
4744             (when (string= gnus-tmp-name "")
4745               (setq gnus-tmp-name gnus-tmp-from))
4746             (unless (numberp gnus-tmp-lines)
4747               (setq gnus-tmp-lines -1))
4748             (if (= gnus-tmp-lines -1)
4749                 (setq gnus-tmp-lines "?")
4750               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4751             (gnus-put-text-property
4752              (point)
4753              (progn (eval gnus-summary-line-format-spec) (point))
4754              'gnus-number number)
4755             (when gnus-visual-p
4756               (forward-line -1)
4757               (gnus-run-hooks 'gnus-summary-update-hook)
4758               (forward-line 1))
4759
4760             (setq gnus-tmp-prev-subject simp-subject)))
4761
4762         (when (nth 1 thread)
4763           (push (list (max 0 gnus-tmp-level)
4764                       (copy-list tree-stack)
4765                       (nthcdr 1 thread))
4766                 stack))
4767         (push (if (nth 1 thread) 1 0) tree-stack)
4768         (incf gnus-tmp-level)
4769         (setq threads (if thread-end nil (cdar thread)))
4770         (if gnus-summary-display-while-building
4771             (if building-count
4772                 (progn
4773                   ;; use a set frequency
4774                   (setq building-line-count (1- building-line-count))
4775                   (when (= building-line-count 0)
4776                     (sit-for 0)
4777                     (setq building-line-count
4778                           gnus-summary-display-while-building)))
4779               ;; always
4780               (sit-for 0)))
4781         (unless threads
4782           (setq gnus-tmp-level 0)))))
4783   (gnus-message 7 "Generating summary...done"))
4784
4785 (defun gnus-summary-prepare-unthreaded (headers)
4786   "Generate an unthreaded summary buffer based on HEADERS."
4787   (let (header number mark)
4788
4789     (beginning-of-line)
4790
4791     (while headers
4792       ;; We may have to root out some bad articles...
4793       (when (memq (setq number (mail-header-number
4794                                 (setq header (pop headers))))
4795                   gnus-newsgroup-limit)
4796         ;; Mark article as read when it has a low score.
4797         (when (and gnus-summary-mark-below
4798                    (< (or (cdr (assq number gnus-newsgroup-scored))
4799                           gnus-summary-default-score 0)
4800                       gnus-summary-mark-below)
4801                    (not (gnus-summary-article-ancient-p number)))
4802           (setq gnus-newsgroup-unreads
4803                 (delq number gnus-newsgroup-unreads))
4804           (if gnus-newsgroup-auto-expire
4805               (push number gnus-newsgroup-expirable)
4806             (push (cons number gnus-low-score-mark)
4807                   gnus-newsgroup-reads)))
4808
4809         (setq mark (gnus-article-mark number))
4810         (push (gnus-data-make number mark (1+ (point)) header 0)
4811               gnus-newsgroup-data)
4812         (gnus-summary-insert-line
4813          header 0 number
4814          (memq number gnus-newsgroup-undownloaded)
4815          mark (memq number gnus-newsgroup-replied)
4816          (memq number gnus-newsgroup-expirable)
4817          (mail-header-subject header) nil
4818          (cdr (assq number gnus-newsgroup-scored))
4819          (memq number gnus-newsgroup-processable))))))
4820
4821 (defun gnus-summary-remove-list-identifiers ()
4822   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4823   (let ((regexp (if (consp gnus-list-identifiers)
4824                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4825                   gnus-list-identifiers))
4826         changed subject)
4827     (when regexp
4828       (dolist (header gnus-newsgroup-headers)
4829         (setq subject (mail-header-subject header)
4830               changed nil)
4831         (while (string-match
4832                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4833                 subject)
4834           (setq subject
4835                 (concat (substring subject 0 (match-beginning 2))
4836                         (substring subject (match-end 0)))
4837                 changed t))
4838         (when (and changed
4839                    (string-match
4840                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4841           (setq subject
4842                 (concat (substring subject 0 (match-beginning 1))
4843                         (substring subject (match-end 1)))))
4844         (when changed
4845           (mail-header-set-subject header subject))))))
4846
4847 (defun gnus-fetch-headers (articles)
4848   "Fetch headers of ARTICLES."
4849   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4850     (gnus-message 5 "Fetching headers for %s..." name)
4851     (prog1
4852         (if (eq 'nov
4853                 (setq gnus-headers-retrieved-by
4854                       (gnus-retrieve-headers
4855                        articles gnus-newsgroup-name
4856                        ;; We might want to fetch old headers, but
4857                        ;; not if there is only 1 article.
4858                        (and (or (and
4859                                  (not (eq gnus-fetch-old-headers 'some))
4860                                  (not (numberp gnus-fetch-old-headers)))
4861                                 (> (length articles) 1))
4862                             gnus-fetch-old-headers))))
4863             (gnus-get-newsgroup-headers-xover
4864              articles nil nil gnus-newsgroup-name t)
4865           (gnus-get-newsgroup-headers))
4866       (gnus-message 5 "Fetching headers for %s...done" name))))
4867
4868 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4869   "Select newsgroup GROUP.
4870 If READ-ALL is non-nil, all articles in the group are selected.
4871 If SELECT-ARTICLES, only select those articles from GROUP."
4872   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4873          ;;!!! Dirty hack; should be removed.
4874          (gnus-summary-ignore-duplicates
4875           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4876               t
4877             gnus-summary-ignore-duplicates))
4878          (info (nth 2 entry))
4879          articles fetched-articles cached)
4880
4881     (unless (gnus-check-server
4882              (set (make-local-variable 'gnus-current-select-method)
4883                   (gnus-find-method-for-group group)))
4884       (error "Couldn't open server"))
4885
4886     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4887         (gnus-activate-group group)     ; Or we can activate it...
4888         (progn                          ; Or we bug out.
4889           (when (equal major-mode 'gnus-summary-mode)
4890             (kill-buffer (current-buffer)))
4891           (error "Couldn't activate group %s: %s"
4892                  group (gnus-status-message group))))
4893
4894     (unless (gnus-request-group group t)
4895       (when (equal major-mode 'gnus-summary-mode)
4896         (kill-buffer (current-buffer)))
4897       (error "Couldn't request group %s: %s"
4898              group (gnus-status-message group)))
4899
4900     (setq gnus-newsgroup-name group
4901           gnus-newsgroup-unselected nil
4902           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4903
4904     (let ((display (gnus-group-find-parameter group 'display)))
4905       (setq gnus-newsgroup-display
4906             (cond
4907              ((not (zerop (or (car-safe read-all) 0)))
4908               ;; The user entered the group with C-u SPC/RET, let's show
4909               ;; all articles.
4910               'gnus-not-ignore)
4911              ((eq display 'all)
4912               'gnus-not-ignore)
4913              ((arrayp display)
4914               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4915              ((numberp display)
4916               ;; The following is probably the "correct" solution, but
4917               ;; it makes Gnus fetch all headers and then limit the
4918               ;; articles (which is slow), so instead we hack the
4919               ;; select-articles parameter instead. -- Simon Josefsson
4920               ;; <jas@kth.se>
4921               ;;
4922               ;; (gnus-byte-compile
4923               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4924               ;;                         display)))))
4925               (setq select-articles
4926                     (gnus-uncompress-range
4927                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4928                              (if (> tmp 0)
4929                                  tmp
4930                                1))
4931                            (cdr (gnus-active group)))))
4932               nil)
4933              (t
4934               nil))))
4935
4936     (gnus-summary-setup-default-charset)
4937
4938     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4939     (when (gnus-virtual-group-p group)
4940       (setq cached gnus-newsgroup-cached))
4941
4942     (setq gnus-newsgroup-unreads
4943           (gnus-sorted-ndifference
4944            (gnus-sorted-ndifference gnus-newsgroup-unreads
4945                                     gnus-newsgroup-marked)
4946            gnus-newsgroup-dormant))
4947
4948     (setq gnus-newsgroup-processable nil)
4949
4950     (gnus-update-read-articles group gnus-newsgroup-unreads)
4951
4952     ;; Adjust and set lists of article marks.
4953     (when info
4954       (gnus-adjust-marked-articles info))
4955     (if (setq articles select-articles)
4956         (setq gnus-newsgroup-unselected
4957               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4958       (setq articles (gnus-articles-to-read group read-all)))
4959
4960     (cond
4961      ((null articles)
4962       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4963       'quit)
4964      ((eq articles 0) nil)
4965      (t
4966       ;; Init the dependencies hash table.
4967       (setq gnus-newsgroup-dependencies
4968             (gnus-make-hashtable (length articles)))
4969       (gnus-set-global-variables)
4970       ;; Retrieve the headers and read them in.
4971
4972       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4973
4974       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4975       (when cached
4976         (setq gnus-newsgroup-cached cached))
4977
4978       ;; Suppress duplicates?
4979       (when gnus-suppress-duplicates
4980         (gnus-dup-suppress-articles))
4981
4982       ;; Set the initial limit.
4983       (setq gnus-newsgroup-limit (copy-sequence articles))
4984       ;; Remove canceled articles from the list of unread articles.
4985       (setq fetched-articles
4986             (mapcar (lambda (headers) (mail-header-number headers))
4987                     gnus-newsgroup-headers))
4988       (setq gnus-newsgroup-articles fetched-articles)
4989       (setq gnus-newsgroup-unreads
4990             (gnus-sorted-nintersection
4991              gnus-newsgroup-unreads fetched-articles))
4992       (gnus-compute-unseen-list)
4993
4994       ;; Removed marked articles that do not exist.
4995       (gnus-update-missing-marks
4996        (gnus-sorted-difference articles fetched-articles))
4997       ;; We might want to build some more threads first.
4998       (when (and gnus-fetch-old-headers
4999                  (eq gnus-headers-retrieved-by 'nov))
5000         (if (eq gnus-fetch-old-headers 'invisible)
5001             (gnus-build-all-threads)
5002           (gnus-build-old-threads)))
5003       ;; Let the Gnus agent mark articles as read.
5004       (when gnus-agent
5005         (gnus-agent-get-undownloaded-list))
5006       ;; Remove list identifiers from subject
5007       (when gnus-list-identifiers
5008         (gnus-summary-remove-list-identifiers))
5009       ;; Check whether auto-expire is to be done in this group.
5010       (setq gnus-newsgroup-auto-expire
5011             (gnus-group-auto-expirable-p group))
5012       ;; Set up the article buffer now, if necessary.
5013       (unless gnus-single-article-buffer
5014         (gnus-article-setup-buffer))
5015       ;; First and last article in this newsgroup.
5016       (when gnus-newsgroup-headers
5017         (setq gnus-newsgroup-begin
5018               (mail-header-number (car gnus-newsgroup-headers))
5019               gnus-newsgroup-end
5020               (mail-header-number
5021                (gnus-last-element gnus-newsgroup-headers))))
5022       ;; GROUP is successfully selected.
5023       (or gnus-newsgroup-headers t)))))
5024
5025 (defun gnus-compute-unseen-list ()
5026   ;; The `seen' marks are treated specially.
5027   (if (not gnus-newsgroup-seen)
5028       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5029     (setq gnus-newsgroup-unseen
5030           (gnus-inverse-list-range-intersection
5031            gnus-newsgroup-articles gnus-newsgroup-seen))))
5032
5033 (defun gnus-summary-display-make-predicate (display)
5034   (require 'gnus-agent)
5035   (when (= (length display) 1)
5036     (setq display (car display)))
5037   (unless gnus-summary-display-cache
5038     (dolist (elem (append '((unread . unread)
5039                             (read . read)
5040                             (unseen . unseen))
5041                           gnus-article-mark-lists))
5042       (push (cons (cdr elem)
5043                   (gnus-byte-compile
5044                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5045             gnus-summary-display-cache)))
5046   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5047         (gnus-category-predicate-cache gnus-summary-display-cache))
5048     (gnus-get-predicate display)))
5049
5050 ;; Uses the dynamically bound `number' variable.
5051 (eval-when-compile
5052   (defvar number))
5053 (defun gnus-article-marked-p (type &optional article)
5054   (let ((article (or article number)))
5055     (cond
5056      ((eq type 'tick)
5057       (memq article gnus-newsgroup-marked))
5058      ((eq type 'spam)
5059       (memq article gnus-newsgroup-spam-marked))
5060      ((eq type 'unsend)
5061       (memq article gnus-newsgroup-unsendable))
5062      ((eq type 'undownload)
5063       (memq article gnus-newsgroup-undownloaded))
5064      ((eq type 'download)
5065       (memq article gnus-newsgroup-downloadable))
5066      ((eq type 'unread)
5067       (memq article gnus-newsgroup-unreads))
5068      ((eq type 'read)
5069       (memq article gnus-newsgroup-reads))
5070      ((eq type 'dormant)
5071       (memq article gnus-newsgroup-dormant) )
5072      ((eq type 'expire)
5073       (memq article gnus-newsgroup-expirable))
5074      ((eq type 'reply)
5075       (memq article gnus-newsgroup-replied))
5076      ((eq type 'killed)
5077       (memq article gnus-newsgroup-killed))
5078      ((eq type 'bookmark)
5079       (assq article gnus-newsgroup-bookmarks))
5080      ((eq type 'score)
5081       (assq article gnus-newsgroup-scored))
5082      ((eq type 'save)
5083       (memq article gnus-newsgroup-saved))
5084      ((eq type 'cache)
5085       (memq article gnus-newsgroup-cached))
5086      ((eq type 'forward)
5087       (memq article gnus-newsgroup-forwarded))
5088      ((eq type 'seen)
5089       (not (memq article gnus-newsgroup-unseen)))
5090      ((eq type 'recent)
5091       (memq article gnus-newsgroup-recent))
5092      (t t))))
5093
5094 (defun gnus-articles-to-read (group &optional read-all)
5095   "Find out what articles the user wants to read."
5096   (let* ((display (gnus-group-find-parameter group 'display))
5097          (articles
5098           ;; Select all articles if `read-all' is non-nil, or if there
5099           ;; are no unread articles.
5100           (if (or read-all
5101                   (and (zerop (length gnus-newsgroup-marked))
5102                        (zerop (length gnus-newsgroup-unreads)))
5103                   ;; Fetch all if the predicate is non-nil.
5104                   gnus-newsgroup-display)
5105               ;; We want to select the headers for all the articles in
5106               ;; the group, so we select either all the active
5107               ;; articles in the group, or (if that's nil), the
5108               ;; articles in the cache.
5109               (or
5110                (gnus-uncompress-range (gnus-active group))
5111                (gnus-cache-articles-in-group group))
5112             ;; Select only the "normal" subset of articles.
5113             (gnus-sorted-nunion
5114              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5115              gnus-newsgroup-unreads)))
5116          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5117          (scored (length scored-list))
5118          (number (length articles))
5119          (marked (+ (length gnus-newsgroup-marked)
5120                     (length gnus-newsgroup-dormant)))
5121          (select
5122           (cond
5123            ((numberp read-all)
5124             read-all)
5125            ((numberp gnus-newsgroup-display)
5126             gnus-newsgroup-display)
5127            (t
5128             (condition-case ()
5129                 (cond
5130                  ((and (or (<= scored marked) (= scored number))
5131                        (numberp gnus-large-newsgroup)
5132                        (> number gnus-large-newsgroup))
5133                   (let* ((cursor-in-echo-area nil)
5134                          (initial (gnus-parameter-large-newsgroup-initial
5135                                    gnus-newsgroup-name))
5136                          (input
5137                           (read-string
5138                            (format
5139                             "How many articles from %s (%s %d): "
5140                             (gnus-limit-string
5141                              (gnus-group-decoded-name gnus-newsgroup-name)
5142                              35)
5143                             (if initial "max" "default")
5144                             number)
5145                            (if initial
5146                                (cons (number-to-string initial)
5147                                      0)))))
5148                     (if (string-match "^[ \t]*$" input) number input)))
5149                  ((and (> scored marked) (< scored number)
5150                        (> (- scored number) 20))
5151                   (let ((input
5152                          (read-string
5153                           (format "%s %s (%d scored, %d total): "
5154                                   "How many articles from"
5155                                   (gnus-group-decoded-name group)
5156                                   scored number))))
5157                     (if (string-match "^[ \t]*$" input)
5158                         number input)))
5159                  (t number))
5160               (quit
5161                (message "Quit getting the articles to read")
5162                nil))))))
5163     (setq select (if (stringp select) (string-to-number select) select))
5164     (if (or (null select) (zerop select))
5165         select
5166       (if (and (not (zerop scored)) (<= (abs select) scored))
5167           (progn
5168             (setq articles (sort scored-list '<))
5169             (setq number (length articles)))
5170         (setq articles (copy-sequence articles)))
5171
5172       (when (< (abs select) number)
5173         (if (< select 0)
5174             ;; Select the N oldest articles.
5175             (setcdr (nthcdr (1- (abs select)) articles) nil)
5176           ;; Select the N most recent articles.
5177           (setq articles (nthcdr (- number select) articles))))
5178       (setq gnus-newsgroup-unselected
5179             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5180       (when gnus-alter-articles-to-read-function
5181         (setq gnus-newsgroup-unreads
5182               (sort
5183                (funcall gnus-alter-articles-to-read-function
5184                         gnus-newsgroup-name gnus-newsgroup-unreads)
5185                '<)))
5186       articles)))
5187
5188 (defun gnus-killed-articles (killed articles)
5189   (let (out)
5190     (while articles
5191       (when (inline (gnus-member-of-range (car articles) killed))
5192         (push (car articles) out))
5193       (setq articles (cdr articles)))
5194     out))
5195
5196 (defun gnus-uncompress-marks (marks)
5197   "Uncompress the mark ranges in MARKS."
5198   (let ((uncompressed '(score bookmark))
5199         out)
5200     (while marks
5201       (if (memq (caar marks) uncompressed)
5202           (push (car marks) out)
5203         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5204       (setq marks (cdr marks)))
5205     out))
5206
5207 (defun gnus-article-mark-to-type (mark)
5208   "Return the type of MARK."
5209   (or (cadr (assq mark gnus-article-special-mark-lists))
5210       'list))
5211
5212 (defun gnus-article-unpropagatable-p (mark)
5213   "Return whether MARK should be propagated to backend."
5214   (memq mark gnus-article-unpropagated-mark-lists))
5215
5216 (defun gnus-adjust-marked-articles (info)
5217   "Set all article lists and remove all marks that are no longer valid."
5218   (let* ((marked-lists (gnus-info-marks info))
5219          (active (gnus-active (gnus-info-group info)))
5220          (min (car active))
5221          (max (cdr active))
5222          (types gnus-article-mark-lists)
5223          marks var articles article mark mark-type)
5224
5225     (dolist (marks marked-lists)
5226       (setq mark (car marks)
5227             mark-type (gnus-article-mark-to-type mark)
5228             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5229
5230       ;; We set the variable according to the type of the marks list,
5231       ;; and then adjust the marks to a subset of the active articles.
5232       (cond
5233        ;; Adjust "simple" lists.
5234        ((eq mark-type 'list)
5235         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5236         (when (memq mark '(tick dormant expire reply save))
5237           (while articles
5238             (when (or (< (setq article (pop articles)) min) (> article max))
5239               (set var (delq article (symbol-value var)))))))
5240        ;; Adjust assocs.
5241        ((eq mark-type 'tuple)
5242         (set var (setq articles (cdr marks)))
5243         (when (not (listp (cdr (symbol-value var))))
5244           (set var (list (symbol-value var))))
5245         (when (not (listp (cdr articles)))
5246           (setq articles (list articles)))
5247         (while articles
5248           (when (or (not (consp (setq article (pop articles))))
5249                     (< (car article) min)
5250                     (> (car article) max))
5251             (set var (delq article (symbol-value var))))))
5252        ;; Adjust ranges (sloppily).
5253        ((eq mark-type 'range)
5254         (cond
5255          ((eq mark 'seen)
5256           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5257           ;; It should be (seen (NUM1 . NUM2)).
5258           (when (numberp (cddr marks))
5259             (setcdr marks (list (cdr marks))))
5260           (setq articles (cdr marks))
5261           (while (and articles
5262                       (or (and (consp (car articles))
5263                                (> min (cdar articles)))
5264                           (and (numberp (car articles))
5265                                (> min (car articles)))))
5266             (pop articles))
5267           (set var articles))))))))
5268
5269 (defun gnus-update-missing-marks (missing)
5270   "Go through the list of MISSING articles and remove them from the mark lists."
5271   (when missing
5272     (let (var m)
5273       ;; Go through all types.
5274       (dolist (elem gnus-article-mark-lists)
5275         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5276           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5277           (when (symbol-value var)
5278             ;; This list has articles.  So we delete all missing
5279             ;; articles from it.
5280             (setq m missing)
5281             (while m
5282               (set var (delq (pop m) (symbol-value var))))))))))
5283
5284 (defun gnus-update-marks ()
5285   "Enter the various lists of marked articles into the newsgroup info list."
5286   (let ((types gnus-article-mark-lists)
5287         (info (gnus-get-info gnus-newsgroup-name))
5288         type list newmarked symbol delta-marks)
5289     (when info
5290       ;; Add all marks lists to the list of marks lists.
5291       (while (setq type (pop types))
5292         (setq list (symbol-value
5293                     (setq symbol
5294                           (intern (format "gnus-newsgroup-%s" (car type))))))
5295
5296         (when list
5297           ;; Get rid of the entries of the articles that have the
5298           ;; default score.
5299           (when (and (eq (cdr type) 'score)
5300                      gnus-save-score
5301                      list)
5302             (let* ((arts list)
5303                    (prev (cons nil list))
5304                    (all prev))
5305               (while arts
5306                 (if (or (not (consp (car arts)))
5307                         (= (cdar arts) gnus-summary-default-score))
5308                     (setcdr prev (cdr arts))
5309                   (setq prev arts))
5310                 (setq arts (cdr arts)))
5311               (setq list (cdr all)))))
5312
5313         (when (eq (cdr type) 'seen)
5314           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5315
5316         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5317           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5318
5319         (when (and (gnus-check-backend-function
5320                     'request-set-mark gnus-newsgroup-name)
5321                    (not (gnus-article-unpropagatable-p (cdr type))))
5322           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5323                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5324                  (add (gnus-remove-from-range
5325                        (gnus-copy-sequence list) old)))
5326             (when add
5327               (push (list add 'add (list (cdr type))) delta-marks))
5328             (when del
5329               (push (list del 'del (list (cdr type))) delta-marks))))
5330
5331         (when list
5332           (push (cons (cdr type) list) newmarked)))
5333
5334       (when delta-marks
5335         (unless (gnus-check-group gnus-newsgroup-name)
5336           (error "Can't open server for %s" gnus-newsgroup-name))
5337         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5338
5339       ;; Enter these new marks into the info of the group.
5340       (if (nthcdr 3 info)
5341           (setcar (nthcdr 3 info) newmarked)
5342         ;; Add the marks lists to the end of the info.
5343         (when newmarked
5344           (setcdr (nthcdr 2 info) (list newmarked))))
5345
5346       ;; Cut off the end of the info if there's nothing else there.
5347       (let ((i 5))
5348         (while (and (> i 2)
5349                     (not (nth i info)))
5350           (when (nthcdr (decf i) info)
5351             (setcdr (nthcdr i info) nil)))))))
5352
5353 (defun gnus-set-mode-line (where)
5354   "Set the mode line of the article or summary buffers.
5355 If WHERE is `summary', the summary mode line format will be used."
5356   ;; Is this mode line one we keep updated?
5357   (when (and (memq where gnus-updated-mode-lines)
5358              (symbol-value
5359               (intern (format "gnus-%s-mode-line-format-spec" where))))
5360     (let (mode-string)
5361       (save-excursion
5362         ;; We evaluate this in the summary buffer since these
5363         ;; variables are buffer-local to that buffer.
5364         (set-buffer gnus-summary-buffer)
5365        ;; We bind all these variables that are used in the `eval' form
5366         ;; below.
5367         (let* ((mformat (symbol-value
5368                          (intern
5369                           (format "gnus-%s-mode-line-format-spec" where))))
5370                (gnus-tmp-group-name (gnus-group-decoded-name
5371                                      gnus-newsgroup-name))
5372                (gnus-tmp-article-number (or gnus-current-article 0))
5373                (gnus-tmp-unread gnus-newsgroup-unreads)
5374                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5375                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5376                (gnus-tmp-unread-and-unselected
5377                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5378                             (zerop gnus-tmp-unselected))
5379                        "")
5380                       ((zerop gnus-tmp-unselected)
5381                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5382                       (t (format "{%d(+%d) more}"
5383                                  gnus-tmp-unread-and-unticked
5384                                  gnus-tmp-unselected))))
5385                (gnus-tmp-subject
5386                 (if (and gnus-current-headers
5387                          (vectorp gnus-current-headers))
5388                     (gnus-mode-string-quote
5389                      (mail-header-subject gnus-current-headers))
5390                   ""))
5391                bufname-length max-len
5392                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5393           (setq mode-string (eval mformat))
5394           (setq bufname-length (if (string-match "%b" mode-string)
5395                                    (- (length
5396                                        (buffer-name
5397                                         (if (eq where 'summary)
5398                                             nil
5399                                           (get-buffer gnus-article-buffer))))
5400                                       2)
5401                                  0))
5402           (setq max-len (max 4 (if gnus-mode-non-string-length
5403                                    (- (window-width)
5404                                       gnus-mode-non-string-length
5405                                       bufname-length)
5406                                  (length mode-string))))
5407           ;; We might have to chop a bit of the string off...
5408           (when (> (length mode-string) max-len)
5409             (setq mode-string
5410                   (concat (truncate-string-to-width mode-string (- max-len 3))
5411                           "...")))
5412           ;; Pad the mode string a bit.
5413           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5414       ;; Update the mode line.
5415       (setq mode-line-buffer-identification
5416             (gnus-mode-line-buffer-identification (list mode-string)))
5417       (set-buffer-modified-p t))))
5418
5419 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5420   "Go through the HEADERS list and add all Xrefs to a hash table.
5421 The resulting hash table is returned, or nil if no Xrefs were found."
5422   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5423          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5424          (xref-hashtb (gnus-make-hashtable))
5425          start group entry number xrefs header)
5426     (while headers
5427       (setq header (pop headers))
5428       (when (and (setq xrefs (mail-header-xref header))
5429                  (not (memq (setq number (mail-header-number header))
5430                             unreads)))
5431         (setq start 0)
5432         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5433           (setq start (match-end 0))
5434           (setq group (if prefix
5435                           (concat prefix (substring xrefs (match-beginning 1)
5436                                                     (match-end 1)))
5437                         (substring xrefs (match-beginning 1) (match-end 1))))
5438           (setq number
5439                 (string-to-int (substring xrefs (match-beginning 2)
5440                                           (match-end 2))))
5441           (if (setq entry (gnus-gethash group xref-hashtb))
5442               (setcdr entry (cons number (cdr entry)))
5443             (gnus-sethash group (cons number nil) xref-hashtb)))))
5444     (and start xref-hashtb)))
5445
5446 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5447   "Look through all the headers and mark the Xrefs as read."
5448   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5449         name entry info xref-hashtb idlist method nth4)
5450     (save-excursion
5451       (set-buffer gnus-group-buffer)
5452       (when (setq xref-hashtb
5453                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5454         (mapatoms
5455          (lambda (group)
5456            (unless (string= from-newsgroup (setq name (symbol-name group)))
5457              (setq idlist (symbol-value group))
5458              ;; Dead groups are not updated.
5459              (and (prog1
5460                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5461                             info (nth 2 entry))
5462                     (when (stringp (setq nth4 (gnus-info-method info)))
5463                       (setq nth4 (gnus-server-to-method nth4))))
5464                   ;; Only do the xrefs if the group has the same
5465                   ;; select method as the group we have just read.
5466                   (or (gnus-methods-equal-p
5467                        nth4 (gnus-find-method-for-group from-newsgroup))
5468                       virtual
5469                       (equal nth4 (setq method (gnus-find-method-for-group
5470                                                 from-newsgroup)))
5471                       (and (equal (car nth4) (car method))
5472                            (equal (nth 1 nth4) (nth 1 method))))
5473                   gnus-use-cross-reference
5474                   (or (not (eq gnus-use-cross-reference t))
5475                       virtual
5476                       ;; Only do cross-references on subscribed
5477                       ;; groups, if that is what is wanted.
5478                       (<= (gnus-info-level info) gnus-level-subscribed))
5479                   (gnus-group-make-articles-read name idlist))))
5480          xref-hashtb)))))
5481
5482 (defun gnus-compute-read-articles (group articles)
5483   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5484          (info (nth 2 entry))
5485          (active (gnus-active group))
5486          ninfo)
5487     (when entry
5488       ;; First peel off all invalid article numbers.
5489       (when active
5490         (let ((ids articles)
5491               id first)
5492           (while (setq id (pop ids))
5493             (when (and first (> id (cdr active)))
5494               ;; We'll end up in this situation in one particular
5495               ;; obscure situation.  If you re-scan a group and get
5496               ;; a new article that is cross-posted to a different
5497               ;; group that has not been re-scanned, you might get
5498               ;; crossposted article that has a higher number than
5499               ;; Gnus believes possible.  So we re-activate this
5500               ;; group as well.  This might mean doing the
5501               ;; crossposting thingy will *increase* the number
5502               ;; of articles in some groups.  Tsk, tsk.
5503               (setq active (or (gnus-activate-group group) active)))
5504             (when (or (> id (cdr active))
5505                       (< id (car active)))
5506               (setq articles (delq id articles))))))
5507       ;; If the read list is nil, we init it.
5508       (if (and active
5509                (null (gnus-info-read info))
5510                (> (car active) 1))
5511           (setq ninfo (cons 1 (1- (car active))))
5512         (setq ninfo (gnus-info-read info)))
5513       ;; Then we add the read articles to the range.
5514       (gnus-add-to-range
5515        ninfo (setq articles (sort articles '<))))))
5516
5517 (defun gnus-group-make-articles-read (group articles)
5518   "Update the info of GROUP to say that ARTICLES are read."
5519   (let* ((num 0)
5520          (entry (gnus-gethash group gnus-newsrc-hashtb))
5521          (info (nth 2 entry))
5522          (active (gnus-active group))
5523          range)
5524     (when entry
5525       (setq range (gnus-compute-read-articles group articles))
5526       (save-excursion
5527         (set-buffer gnus-group-buffer)
5528         (gnus-undo-register
5529           `(progn
5530              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5531              (gnus-info-set-read ',info ',(gnus-info-read info))
5532              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5533              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5534              (gnus-group-update-group ,group t))))
5535       ;; Add the read articles to the range.
5536       (gnus-info-set-read info range)
5537       (gnus-request-set-mark group (list (list range 'add '(read))))
5538       ;; Then we have to re-compute how many unread
5539       ;; articles there are in this group.
5540       (when active
5541         (cond
5542          ((not range)
5543           (setq num (- (1+ (cdr active)) (car active))))
5544          ((not (listp (cdr range)))
5545           (setq num (- (cdr active) (- (1+ (cdr range))
5546                                        (car range)))))
5547          (t
5548           (while range
5549             (if (numberp (car range))
5550                 (setq num (1+ num))
5551               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5552             (setq range (cdr range)))
5553           (setq num (- (cdr active) num))))
5554         ;; Update the number of unread articles.
5555         (setcar entry num)
5556         ;; Update the group buffer.
5557         (gnus-group-update-group group t)))))
5558
5559 (defvar gnus-newsgroup-none-id 0)
5560
5561 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5562   (let ((cur nntp-server-buffer)
5563         (dependencies
5564          (or dependencies
5565              (save-excursion (set-buffer gnus-summary-buffer)
5566                              gnus-newsgroup-dependencies)))
5567         headers id end ref
5568         (mail-parse-charset gnus-newsgroup-charset)
5569         (mail-parse-ignored-charsets
5570          (save-excursion (condition-case nil
5571                              (set-buffer gnus-summary-buffer)
5572                            (error))
5573                          gnus-newsgroup-ignored-charsets)))
5574     (save-excursion
5575       (set-buffer nntp-server-buffer)
5576       ;; Translate all TAB characters into SPACE characters.
5577       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5578       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5579       (gnus-run-hooks 'gnus-parse-headers-hook)
5580       (let ((case-fold-search t)
5581             in-reply-to header p lines chars)
5582         (goto-char (point-min))
5583         ;; Search to the beginning of the next header.  Error messages
5584         ;; do not begin with 2 or 3.
5585         (while (re-search-forward "^[23][0-9]+ " nil t)
5586           (setq id nil
5587                 ref nil)
5588           ;; This implementation of this function, with nine
5589           ;; search-forwards instead of the one re-search-forward and
5590           ;; a case (which basically was the old function) is actually
5591           ;; about twice as fast, even though it looks messier.  You
5592           ;; can't have everything, I guess.  Speed and elegance
5593           ;; doesn't always go hand in hand.
5594           (setq
5595            header
5596            (vector
5597             ;; Number.
5598             (prog1
5599                 (read cur)
5600               (end-of-line)
5601               (setq p (point))
5602               (narrow-to-region (point)
5603                                 (or (and (search-forward "\n.\n" nil t)
5604                                          (- (point) 2))
5605                                     (point))))
5606             ;; Subject.
5607             (progn
5608               (goto-char p)
5609               (if (search-forward "\nsubject:" nil t)
5610                   (funcall gnus-decode-encoded-word-function
5611                            (nnheader-header-value))
5612                 "(none)"))
5613             ;; From.
5614             (progn
5615               (goto-char p)
5616               (if (search-forward "\nfrom:" nil t)
5617                   (funcall gnus-decode-encoded-word-function
5618                            (nnheader-header-value))
5619                 "(nobody)"))
5620             ;; Date.
5621             (progn
5622               (goto-char p)
5623               (if (search-forward "\ndate:" nil t)
5624                   (nnheader-header-value) ""))
5625             ;; Message-ID.
5626             (progn
5627               (goto-char p)
5628               (setq id (if (re-search-forward
5629                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5630                            ;; We do it this way to make sure the Message-ID
5631                            ;; is (somewhat) syntactically valid.
5632                            (buffer-substring (match-beginning 1)
5633                                              (match-end 1))
5634                          ;; If there was no message-id, we just fake one
5635                          ;; to make subsequent routines simpler.
5636                          (nnheader-generate-fake-message-id))))
5637             ;; References.
5638             (progn
5639               (goto-char p)
5640               (if (search-forward "\nreferences:" nil t)
5641                   (progn
5642                     (setq end (point))
5643                     (prog1
5644                         (nnheader-header-value)
5645                       (setq ref
5646                             (buffer-substring
5647                              (progn
5648                                (end-of-line)
5649                                (search-backward ">" end t)
5650                                (1+ (point)))
5651                              (progn
5652                                (search-backward "<" end t)
5653                                (point))))))
5654                 ;; Get the references from the in-reply-to header if there
5655                 ;; were no references and the in-reply-to header looks
5656                 ;; promising.
5657                 (if (and (search-forward "\nin-reply-to:" nil t)
5658                          (setq in-reply-to (nnheader-header-value))
5659                          (string-match "<[^>]+>" in-reply-to))
5660                     (let (ref2)
5661                       (setq ref (substring in-reply-to (match-beginning 0)
5662                                            (match-end 0)))
5663                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5664                         (setq ref2 (substring in-reply-to (match-beginning 0)
5665                                               (match-end 0)))
5666                         (when (> (length ref2) (length ref))
5667                           (setq ref ref2)))
5668                       ref)
5669                   (setq ref nil))))
5670             ;; Chars.
5671             (progn
5672               (goto-char p)
5673               (if (search-forward "\nchars: " nil t)
5674                   (if (numberp (setq chars (ignore-errors (read cur))))
5675                       chars -1)
5676                 -1))
5677             ;; Lines.
5678             (progn
5679               (goto-char p)
5680               (if (search-forward "\nlines: " nil t)
5681                   (if (numberp (setq lines (ignore-errors (read cur))))
5682                       lines -1)
5683                 -1))
5684             ;; Xref.
5685             (progn
5686               (goto-char p)
5687               (and (search-forward "\nxref:" nil t)
5688                    (nnheader-header-value)))
5689             ;; Extra.
5690             (when gnus-extra-headers
5691               (let ((extra gnus-extra-headers)
5692                     out)
5693                 (while extra
5694                   (goto-char p)
5695                   (when (search-forward
5696                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5697                     (push (cons (car extra) (nnheader-header-value))
5698                           out))
5699                   (pop extra))
5700                 out))))
5701           (when (equal id ref)
5702             (setq ref nil))
5703
5704           (when gnus-alter-header-function
5705             (funcall gnus-alter-header-function header)
5706             (setq id (mail-header-id header)
5707                   ref (gnus-parent-id (mail-header-references header))))
5708
5709           (when (setq header
5710                       (gnus-dependencies-add-header
5711                        header dependencies force-new))
5712             (push header headers))
5713           (goto-char (point-max))
5714           (widen))
5715         (nreverse headers)))))
5716
5717 ;; Goes through the xover lines and returns a list of vectors
5718 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5719                                                   force-new dependencies
5720                                                   group also-fetch-heads)
5721   "Parse the news overview data in the server buffer.
5722 Return a list of headers that match SEQUENCE (see
5723 `nntp-retrieve-headers')."
5724   ;; Get the Xref when the users reads the articles since most/some
5725   ;; NNTP servers do not include Xrefs when using XOVER.
5726   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5727   (let ((mail-parse-charset gnus-newsgroup-charset)
5728         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5729         (cur nntp-server-buffer)
5730         (dependencies (or dependencies gnus-newsgroup-dependencies))
5731         (allp (cond
5732                ((eq gnus-read-all-available-headers t)
5733                 t)
5734                ((stringp gnus-read-all-available-headers)
5735                 (string-match gnus-read-all-available-headers group))
5736                (t
5737                 nil)))
5738         number headers header)
5739     (save-excursion
5740       (set-buffer nntp-server-buffer)
5741       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5742       ;; Allow the user to mangle the headers before parsing them.
5743       (gnus-run-hooks 'gnus-parse-headers-hook)
5744       (goto-char (point-min))
5745       (gnus-parse-without-error
5746         (while (and (or sequence allp)
5747                     (not (eobp)))
5748           (setq number (read cur))
5749           (when (not allp)
5750             (while (and sequence
5751                         (< (car sequence) number))
5752               (setq sequence (cdr sequence))))
5753           (when (and (or allp
5754                          (and sequence
5755                               (eq number (car sequence))))
5756                      (progn
5757                        (setq sequence (cdr sequence))
5758                        (setq header (inline
5759                                       (gnus-nov-parse-line
5760                                        number dependencies force-new)))))
5761             (push header headers))
5762           (forward-line 1)))
5763       ;; A common bug in inn is that if you have posted an article and
5764       ;; then retrieves the active file, it will answer correctly --
5765       ;; the new article is included.  However, a NOV entry for the
5766       ;; article may not have been generated yet, so this may fail.
5767       ;; We work around this problem by retrieving the last few
5768       ;; headers using HEAD.
5769       (if (or (not also-fetch-heads)
5770               (not sequence))
5771           ;; We (probably) got all the headers.
5772           (nreverse headers)
5773         (let ((gnus-nov-is-evil t))
5774           (nconc
5775            (nreverse headers)
5776            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5777              (gnus-get-newsgroup-headers))))))))
5778
5779 (defun gnus-article-get-xrefs ()
5780   "Fill in the Xref value in `gnus-current-headers', if necessary.
5781 This is meant to be called in `gnus-article-internal-prepare-hook'."
5782   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5783                                  gnus-current-headers)))
5784     (or (not gnus-use-cross-reference)
5785         (not headers)
5786         (and (mail-header-xref headers)
5787              (not (string= (mail-header-xref headers) "")))
5788         (let ((case-fold-search t)
5789               xref)
5790           (save-restriction
5791             (nnheader-narrow-to-headers)
5792             (goto-char (point-min))
5793             (when (or (and (not (eobp))
5794                            (eq (downcase (char-after)) ?x)
5795                            (looking-at "Xref:"))
5796                       (search-forward "\nXref:" nil t))
5797               (goto-char (1+ (match-end 0)))
5798               (setq xref (buffer-substring (point)
5799                                            (progn (end-of-line) (point))))
5800               (mail-header-set-xref headers xref)))))))
5801
5802 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5803   "Find article ID and insert the summary line for that article.
5804 OLD-HEADER can either be a header or a line number to insert
5805 the subject line on."
5806   (let* ((line (and (numberp old-header) old-header))
5807          (old-header (and (vectorp old-header) old-header))
5808          (header (cond ((and old-header use-old-header)
5809                         old-header)
5810                        ((and (numberp id)
5811                              (gnus-number-to-header id))
5812                         (gnus-number-to-header id))
5813                        (t
5814                         (gnus-read-header id))))
5815          (number (and (numberp id) id))
5816          d)
5817     (when header
5818       ;; Rebuild the thread that this article is part of and go to the
5819       ;; article we have fetched.
5820       (when (and (not gnus-show-threads)
5821                  old-header)
5822         (when (and number
5823                    (setq d (gnus-data-find (mail-header-number old-header))))
5824           (goto-char (gnus-data-pos d))
5825           (gnus-data-remove
5826            number
5827            (- (gnus-point-at-bol)
5828               (prog1
5829                   (1+ (gnus-point-at-eol))
5830                 (gnus-delete-line))))))
5831       (when old-header
5832         (mail-header-set-number header (mail-header-number old-header)))
5833       (setq gnus-newsgroup-sparse
5834             (delq (setq number (mail-header-number header))
5835                   gnus-newsgroup-sparse))
5836       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5837       (push number gnus-newsgroup-limit)
5838       (gnus-rebuild-thread (mail-header-id header) line)
5839       (gnus-summary-goto-subject number nil t))
5840     (when (and (numberp number)
5841                (> number 0))
5842       ;; We have to update the boundaries even if we can't fetch the
5843       ;; article if ID is a number -- so that the next `P' or `N'
5844       ;; command will fetch the previous (or next) article even
5845       ;; if the one we tried to fetch this time has been canceled.
5846       (when (> number gnus-newsgroup-end)
5847         (setq gnus-newsgroup-end number))
5848       (when (< number gnus-newsgroup-begin)
5849         (setq gnus-newsgroup-begin number))
5850       (setq gnus-newsgroup-unselected
5851             (delq number gnus-newsgroup-unselected)))
5852     ;; Report back a success?
5853     (and header (mail-header-number header))))
5854
5855 ;;; Process/prefix in the summary buffer
5856
5857 (defun gnus-summary-work-articles (n)
5858   "Return a list of articles to be worked upon.
5859 The prefix argument, the list of process marked articles, and the
5860 current article will be taken into consideration."
5861   (save-excursion
5862     (set-buffer gnus-summary-buffer)
5863     (cond
5864      (n
5865       ;; A numerical prefix has been given.
5866       (setq n (prefix-numeric-value n))
5867       (let ((backward (< n 0))
5868             (n (abs (prefix-numeric-value n)))
5869             articles article)
5870         (save-excursion
5871           (while
5872               (and (> n 0)
5873                    (push (setq article (gnus-summary-article-number))
5874                          articles)
5875                    (if backward
5876                        (gnus-summary-find-prev nil article)
5877                      (gnus-summary-find-next nil article)))
5878             (decf n)))
5879         (nreverse articles)))
5880      ((and (gnus-region-active-p) (mark))
5881       (message "region active")
5882       ;; Work on the region between point and mark.
5883       (let ((max (max (point) (mark)))
5884             articles article)
5885         (save-excursion
5886           (goto-char (min (min (point) (mark))))
5887           (while
5888               (and
5889                (push (setq article (gnus-summary-article-number)) articles)
5890                (gnus-summary-find-next nil article)
5891                (< (point) max)))
5892           (nreverse articles))))
5893      (gnus-newsgroup-processable
5894       ;; There are process-marked articles present.
5895       ;; Save current state.
5896       (gnus-summary-save-process-mark)
5897       ;; Return the list.
5898       (reverse gnus-newsgroup-processable))
5899      (t
5900       ;; Just return the current article.
5901       (list (gnus-summary-article-number))))))
5902
5903 (defmacro gnus-summary-iterate (arg &rest forms)
5904   "Iterate over the process/prefixed articles and do FORMS.
5905 ARG is the interactive prefix given to the command.  FORMS will be
5906 executed with point over the summary line of the articles."
5907   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5908     `(let ((,articles (gnus-summary-work-articles ,arg)))
5909        (while ,articles
5910          (gnus-summary-goto-subject (car ,articles))
5911          ,@forms
5912          (pop ,articles)))))
5913
5914 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5915 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5916
5917 (defun gnus-summary-save-process-mark ()
5918   "Push the current set of process marked articles on the stack."
5919   (interactive)
5920   (push (copy-sequence gnus-newsgroup-processable)
5921         gnus-newsgroup-process-stack))
5922
5923 (defun gnus-summary-kill-process-mark ()
5924   "Push the current set of process marked articles on the stack and unmark."
5925   (interactive)
5926   (gnus-summary-save-process-mark)
5927   (gnus-summary-unmark-all-processable))
5928
5929 (defun gnus-summary-yank-process-mark ()
5930   "Pop the last process mark state off the stack and restore it."
5931   (interactive)
5932   (unless gnus-newsgroup-process-stack
5933     (error "Empty mark stack"))
5934   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5935
5936 (defun gnus-summary-process-mark-set (set)
5937   "Make SET into the current process marked articles."
5938   (gnus-summary-unmark-all-processable)
5939   (while set
5940     (gnus-summary-set-process-mark (pop set))))
5941
5942 ;;; Searching and stuff
5943
5944 (defun gnus-summary-search-group (&optional backward use-level)
5945   "Search for next unread newsgroup.
5946 If optional argument BACKWARD is non-nil, search backward instead."
5947   (save-excursion
5948     (set-buffer gnus-group-buffer)
5949     (when (gnus-group-search-forward
5950            backward nil (if use-level (gnus-group-group-level) nil))
5951       (gnus-group-group-name))))
5952
5953 (defun gnus-summary-best-group (&optional exclude-group)
5954   "Find the name of the best unread group.
5955 If EXCLUDE-GROUP, do not go to this group."
5956   (save-excursion
5957     (set-buffer gnus-group-buffer)
5958     (save-excursion
5959       (gnus-group-best-unread-group exclude-group))))
5960
5961 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5962   (if backward (gnus-summary-find-prev)
5963     (let* ((dummy (gnus-summary-article-intangible-p))
5964            (article (or article (gnus-summary-article-number)))
5965            (arts (gnus-data-find-list article))
5966            result)
5967       (when (and (not dummy)
5968                  (or (not gnus-summary-check-current)
5969                      (not unread)
5970                      (not (gnus-data-unread-p (car arts)))))
5971         (setq arts (cdr arts)))
5972       (when (setq result
5973                   (if unread
5974                       (progn
5975                         (while arts
5976                           (when (or (and undownloaded
5977                                          (memq (car arts) gnus-newsgroup-undownloaded))
5978                                     (gnus-data-unread-p (car arts)))
5979                             (setq result (car arts)
5980                                   arts nil))
5981                           (setq arts (cdr arts)))
5982                         result)
5983                     (car arts)))
5984         (goto-char (gnus-data-pos result))
5985         (gnus-data-number result)))))
5986
5987 (defun gnus-summary-find-prev (&optional unread article)
5988   (let* ((eobp (eobp))
5989          (article (or article (gnus-summary-article-number)))
5990          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5991          result)
5992     (when (and (not eobp)
5993                (or (not gnus-summary-check-current)
5994                    (not unread)
5995                    (not (gnus-data-unread-p (car arts)))))
5996       (setq arts (cdr arts)))
5997     (when (setq result
5998                 (if unread
5999                     (progn
6000                       (while arts
6001                         (when (gnus-data-unread-p (car arts))
6002                           (setq result (car arts)
6003                                 arts nil))
6004                         (setq arts (cdr arts)))
6005                       result)
6006                   (car arts)))
6007       (goto-char (gnus-data-pos result))
6008       (gnus-data-number result))))
6009
6010 (defun gnus-summary-find-subject (subject &optional unread backward article)
6011   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6012          (article (or article (gnus-summary-article-number)))
6013          (articles (gnus-data-list backward))
6014          (arts (gnus-data-find-list article articles))
6015          result)
6016     (when (or (not gnus-summary-check-current)
6017               (not unread)
6018               (not (gnus-data-unread-p (car arts))))
6019       (setq arts (cdr arts)))
6020     (while arts
6021       (and (or (not unread)
6022                (gnus-data-unread-p (car arts)))
6023            (vectorp (gnus-data-header (car arts)))
6024            (gnus-subject-equal
6025             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6026            (setq result (car arts)
6027                  arts nil))
6028       (setq arts (cdr arts)))
6029     (and result
6030          (goto-char (gnus-data-pos result))
6031          (gnus-data-number result))))
6032
6033 (defun gnus-summary-search-forward (&optional unread subject backward)
6034   "Search forward for an article.
6035 If UNREAD, look for unread articles.  If SUBJECT, look for
6036 articles with that subject.  If BACKWARD, search backward instead."
6037   (cond (subject (gnus-summary-find-subject subject unread backward))
6038         (backward (gnus-summary-find-prev unread))
6039         (t (gnus-summary-find-next unread))))
6040
6041 (defun gnus-recenter (&optional n)
6042   "Center point in window and redisplay frame.
6043 Also do horizontal recentering."
6044   (interactive "P")
6045   (when (and gnus-auto-center-summary
6046              (not (eq gnus-auto-center-summary 'vertical)))
6047     (gnus-horizontal-recenter))
6048   (recenter n))
6049
6050 (defun gnus-summary-recenter ()
6051   "Center point in the summary window.
6052 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6053 displayed, no centering will be performed."
6054   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6055 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6056   (interactive)
6057   (let* ((top (cond ((< (window-height) 4) 0)
6058                     ((< (window-height) 7) 1)
6059                     (t (if (numberp gnus-auto-center-summary)
6060                            gnus-auto-center-summary
6061                          2))))
6062          (height (1- (window-height)))
6063          (bottom (save-excursion (goto-char (point-max))
6064                                  (forward-line (- height))
6065                                  (point)))
6066          (window (get-buffer-window (current-buffer))))
6067     ;; The user has to want it.
6068     (when gnus-auto-center-summary
6069       (when (get-buffer-window gnus-article-buffer)
6070         ;; Only do recentering when the article buffer is displayed,
6071       ;; Set the window start to either `bottom', which is the biggest
6072         ;; possible valid number, or the second line from the top,
6073         ;; whichever is the least.
6074         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6075           (if (> bottom top-pos)
6076               ;; Keep the second line from the top visible
6077               (set-window-start window top-pos t)
6078             ;; Try to keep the bottom line visible; if it's partially
6079             ;; obscured, either scroll one more line to make it fully
6080             ;; visible, or revert to using TOP-POS.
6081             (save-excursion
6082               (goto-char (point-max))
6083               (forward-line -1)
6084               (let ((last-line-start (point)))
6085                 (goto-char bottom)
6086                 (set-window-start window (point) t)
6087                 (when (not (pos-visible-in-window-p last-line-start window))
6088                   (forward-line 1)
6089                   (set-window-start window (min (point) top-pos) t)))))))
6090       ;; Do horizontal recentering while we're at it.
6091       (when (and (get-buffer-window (current-buffer) t)
6092                  (not (eq gnus-auto-center-summary 'vertical)))
6093         (let ((selected (selected-window)))
6094           (select-window (get-buffer-window (current-buffer) t))
6095           (gnus-summary-position-point)
6096           (gnus-horizontal-recenter)
6097           (select-window selected))))))
6098
6099 (defun gnus-summary-jump-to-group (newsgroup)
6100   "Move point to NEWSGROUP in group mode buffer."
6101   ;; Keep update point of group mode buffer if visible.
6102   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6103       (save-window-excursion
6104         ;; Take care of tree window mode.
6105         (when (get-buffer-window gnus-group-buffer)
6106           (pop-to-buffer gnus-group-buffer))
6107         (gnus-group-jump-to-group newsgroup))
6108     (save-excursion
6109       ;; Take care of tree window mode.
6110       (if (get-buffer-window gnus-group-buffer)
6111           (pop-to-buffer gnus-group-buffer)
6112         (set-buffer gnus-group-buffer))
6113       (gnus-group-jump-to-group newsgroup))))
6114
6115 ;; This function returns a list of article numbers based on the
6116 ;; difference between the ranges of read articles in this group and
6117 ;; the range of active articles.
6118 (defun gnus-list-of-unread-articles (group)
6119   (let* ((read (gnus-info-read (gnus-get-info group)))
6120          (active (or (gnus-active group) (gnus-activate-group group)))
6121          (last (cdr active))
6122          first nlast unread)
6123     ;; If none are read, then all are unread.
6124     (if (not read)
6125         (setq first (car active))
6126       ;; If the range of read articles is a single range, then the
6127       ;; first unread article is the article after the last read
6128       ;; article.  Sounds logical, doesn't it?
6129       (if (and (not (listp (cdr read)))
6130                (or (< (car read) (car active))
6131                    (progn (setq read (list read))
6132                           nil)))
6133           (setq first (max (car active) (1+ (cdr read))))
6134         ;; `read' is a list of ranges.
6135         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6136                                   (caar read)))
6137                   1)
6138           (setq first (car active)))
6139         (while read
6140           (when first
6141             (while (< first nlast)
6142               (push first unread)
6143               (setq first (1+ first))))
6144           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6145           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6146           (setq read (cdr read)))))
6147     ;; And add the last unread articles.
6148     (while (<= first last)
6149       (push first unread)
6150       (setq first (1+ first)))
6151     ;; Return the list of unread articles.
6152     (delq 0 (nreverse unread))))
6153
6154 (defun gnus-list-of-read-articles (group)
6155   "Return a list of unread, unticked and non-dormant articles."
6156   (let* ((info (gnus-get-info group))
6157          (marked (gnus-info-marks info))
6158          (active (gnus-active group)))
6159     (and info active
6160          (gnus-list-range-difference
6161           (gnus-list-range-difference
6162            (gnus-sorted-complement
6163             (gnus-uncompress-range active)
6164             (gnus-list-of-unread-articles group))
6165            (cdr (assq 'dormant marked)))
6166           (cdr (assq 'tick marked))))))
6167
6168 ;; Various summary commands
6169
6170 (defun gnus-summary-select-article-buffer ()
6171   "Reconfigure windows to show article buffer."
6172   (interactive)
6173   (if (not (gnus-buffer-live-p gnus-article-buffer))
6174       (error "There is no article buffer for this summary buffer")
6175     (gnus-configure-windows 'article)
6176     (select-window (get-buffer-window gnus-article-buffer))))
6177
6178 (defun gnus-summary-universal-argument (arg)
6179   "Perform any operation on all articles that are process/prefixed."
6180   (interactive "P")
6181   (let ((articles (gnus-summary-work-articles arg))
6182         func article)
6183     (if (eq
6184          (setq
6185           func
6186           (key-binding
6187            (read-key-sequence
6188             (substitute-command-keys
6189              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6190          'undefined)
6191         (gnus-error 1 "Undefined key")
6192       (save-excursion
6193         (while articles
6194           (gnus-summary-goto-subject (setq article (pop articles)))
6195           (let (gnus-newsgroup-processable)
6196             (command-execute func))
6197           (gnus-summary-remove-process-mark article)))))
6198   (gnus-summary-position-point))
6199
6200 (defun gnus-summary-toggle-truncation (&optional arg)
6201   "Toggle truncation of summary lines.
6202 With arg, turn line truncation on if arg is positive."
6203   (interactive "P")
6204   (setq truncate-lines
6205         (if (null arg) (not truncate-lines)
6206           (> (prefix-numeric-value arg) 0)))
6207   (redraw-display))
6208
6209 (defun gnus-summary-find-uncancelled ()
6210   "Return the number of an uncancelled article.
6211 The current article is considered, then following articles, then previous
6212 articles.  If all articles are cancelled then return a dummy 0."
6213   (let (found)
6214     (dolist (rev '(nil t))
6215       (unless found      ; don't demand the reverse list if we don't need it
6216         (let ((data (gnus-data-find-list
6217                      (gnus-summary-article-number) (gnus-data-list rev))))
6218           (while (and data (not found))
6219             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6220                 (setq found (gnus-data-number (car data))))
6221             (setq data (cdr data))))))
6222     (or found 0)))
6223
6224 (defun gnus-summary-reselect-current-group (&optional all rescan)
6225   "Exit and then reselect the current newsgroup.
6226 The prefix argument ALL means to select all articles."
6227   (interactive "P")
6228   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6229     (error "Ephemeral groups can't be reselected"))
6230   (let ((current-subject (gnus-summary-find-uncancelled))
6231         (group gnus-newsgroup-name))
6232     (setq gnus-newsgroup-begin nil)
6233     (gnus-summary-exit)
6234     ;; We have to adjust the point of group mode buffer because
6235     ;; point was moved to the next unread newsgroup by exiting.
6236     (gnus-summary-jump-to-group group)
6237     (when rescan
6238       (save-excursion
6239         (gnus-group-get-new-news-this-group 1)))
6240     (gnus-group-read-group all t)
6241     (gnus-summary-goto-subject current-subject nil t)))
6242
6243 (defun gnus-summary-rescan-group (&optional all)
6244   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6245   (interactive "P")
6246   (gnus-summary-reselect-current-group all t))
6247
6248 (defun gnus-summary-update-info (&optional non-destructive)
6249   (save-excursion
6250     (let ((group gnus-newsgroup-name))
6251       (when group
6252         (when gnus-newsgroup-kill-headers
6253           (setq gnus-newsgroup-killed
6254                 (gnus-compress-sequence
6255                  (gnus-sorted-union
6256                   (gnus-list-range-intersection
6257                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6258                   gnus-newsgroup-unreads)
6259                  t)))
6260         (unless (listp (cdr gnus-newsgroup-killed))
6261           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6262         (let ((headers gnus-newsgroup-headers))
6263           ;; Set the new ranges of read articles.
6264           (save-excursion
6265             (set-buffer gnus-group-buffer)
6266             (gnus-undo-force-boundary))
6267           (gnus-update-read-articles
6268            group (gnus-sorted-union
6269                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6270           ;; Set the current article marks.
6271           (let ((gnus-newsgroup-scored
6272                  (if (and (not gnus-save-score)
6273                           (not non-destructive))
6274                      nil
6275                    gnus-newsgroup-scored)))
6276             (save-excursion
6277               (gnus-update-marks)))
6278           ;; Do the cross-ref thing.
6279           (when gnus-use-cross-reference
6280             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6281           ;; Do not switch windows but change the buffer to work.
6282           (set-buffer gnus-group-buffer)
6283           (unless (gnus-ephemeral-group-p group)
6284             (gnus-group-update-group group)))))))
6285
6286 (defun gnus-summary-save-newsrc (&optional force)
6287   "Save the current number of read/marked articles in the dribble buffer.
6288 The dribble buffer will then be saved.
6289 If FORCE (the prefix), also save the .newsrc file(s)."
6290   (interactive "P")
6291   (gnus-summary-update-info t)
6292   (if force
6293       (gnus-save-newsrc-file)
6294     (gnus-dribble-save)))
6295
6296 (defun gnus-summary-exit (&optional temporary)
6297   "Exit reading current newsgroup, and then return to group selection mode.
6298 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6299   (interactive)
6300   (gnus-set-global-variables)
6301   (when (gnus-buffer-live-p gnus-article-buffer)
6302     (save-excursion
6303       (set-buffer gnus-article-buffer)
6304       (mm-destroy-parts gnus-article-mime-handles)
6305       ;; Set it to nil for safety reason.
6306       (setq gnus-article-mime-handle-alist nil)
6307       (setq gnus-article-mime-handles nil)))
6308   (gnus-kill-save-kill-buffer)
6309   (gnus-async-halt-prefetch)
6310   (let* ((group gnus-newsgroup-name)
6311          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6312          (gnus-group-is-exiting-p t)
6313          (mode major-mode)
6314          (group-point nil)
6315          (buf (current-buffer)))
6316     (unless quit-config
6317       ;; Do adaptive scoring, and possibly save score files.
6318       (when gnus-newsgroup-adaptive
6319         (gnus-score-adaptive))
6320       (when gnus-use-scoring
6321         (gnus-score-save)))
6322     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6323     ;; If we have several article buffers, we kill them at exit.
6324     (unless gnus-single-article-buffer
6325       (gnus-kill-buffer gnus-original-article-buffer)
6326       (setq gnus-article-current nil))
6327     (when gnus-use-cache
6328       (gnus-cache-possibly-remove-articles)
6329       (gnus-cache-save-buffers))
6330     (gnus-async-prefetch-remove-group group)
6331     (when gnus-suppress-duplicates
6332       (gnus-dup-enter-articles))
6333     (when gnus-use-trees
6334       (gnus-tree-close group))
6335     (when gnus-use-cache
6336       (gnus-cache-write-active))
6337     ;; Remove entries for this group.
6338     (nnmail-purge-split-history (gnus-group-real-name group))
6339     ;; Make all changes in this group permanent.
6340     (unless quit-config
6341       (gnus-run-hooks 'gnus-exit-group-hook)
6342       (gnus-summary-update-info))
6343     (gnus-close-group group)
6344     ;; Make sure where we were, and go to next newsgroup.
6345     (set-buffer gnus-group-buffer)
6346     (unless quit-config
6347       (gnus-group-jump-to-group group))
6348     (gnus-run-hooks 'gnus-summary-exit-hook)
6349     (unless (or quit-config
6350                 ;; If this group has disappeared from the summary
6351                 ;; buffer, don't skip forwards.
6352                 (not (string= group (gnus-group-group-name))))
6353       (gnus-group-next-unread-group 1))
6354     (setq group-point (point))
6355     (if temporary
6356         nil                             ;Nothing to do.
6357       ;; If we have several article buffers, we kill them at exit.
6358       (unless gnus-single-article-buffer
6359         (gnus-kill-buffer gnus-article-buffer)
6360         (gnus-kill-buffer gnus-original-article-buffer)
6361         (setq gnus-article-current nil))
6362       (set-buffer buf)
6363       (if (not gnus-kill-summary-on-exit)
6364           (progn
6365             (gnus-deaden-summary)
6366             (setq mode nil))
6367         ;; We set all buffer-local variables to nil.  It is unclear why
6368         ;; this is needed, but if we don't, buffer-local variables are
6369         ;; not garbage-collected, it seems.  This would the lead to en
6370         ;; ever-growing Emacs.
6371         (gnus-summary-clear-local-variables)
6372         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6373           (gnus-summary-clear-local-variables))
6374         (when (get-buffer gnus-article-buffer)
6375           (bury-buffer gnus-article-buffer))
6376         ;; We clear the global counterparts of the buffer-local
6377         ;; variables as well, just to be on the safe side.
6378         (set-buffer gnus-group-buffer)
6379         (gnus-summary-clear-local-variables)
6380         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6381           (gnus-summary-clear-local-variables)))
6382       (setq gnus-current-select-method gnus-select-method)
6383       (pop-to-buffer gnus-group-buffer)
6384       (if (not quit-config)
6385           (progn
6386             (goto-char group-point)
6387             (gnus-configure-windows 'group 'force))
6388         (gnus-handle-ephemeral-exit quit-config))
6389       ;; Return to group mode buffer.
6390       (when (eq mode 'gnus-summary-mode)
6391         (gnus-kill-buffer buf))
6392       ;; Clear the current group name.
6393       (unless quit-config
6394         (setq gnus-newsgroup-name nil)))))
6395
6396 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6397 (defun gnus-summary-exit-no-update (&optional no-questions)
6398   "Quit reading current newsgroup without updating read article info."
6399   (interactive)
6400   (let* ((group gnus-newsgroup-name)
6401          (gnus-group-is-exiting-p t)
6402          (quit-config (gnus-group-quit-config group)))
6403     (when (or no-questions
6404               gnus-expert-user
6405               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6406       (gnus-async-halt-prefetch)
6407       (mapcar 'funcall
6408               (delq 'gnus-summary-expire-articles
6409                     (copy-sequence gnus-summary-prepare-exit-hook)))
6410       (when (gnus-buffer-live-p gnus-article-buffer)
6411         (save-excursion
6412           (set-buffer gnus-article-buffer)
6413           (mm-destroy-parts gnus-article-mime-handles)
6414           ;; Set it to nil for safety reason.
6415           (setq gnus-article-mime-handle-alist nil)
6416           (setq gnus-article-mime-handles nil)))
6417       ;; If we have several article buffers, we kill them at exit.
6418       (unless gnus-single-article-buffer
6419         (gnus-kill-buffer gnus-article-buffer)
6420         (gnus-kill-buffer gnus-original-article-buffer)
6421         (setq gnus-article-current nil))
6422       (if (not gnus-kill-summary-on-exit)
6423           (gnus-deaden-summary)
6424         (gnus-close-group group)
6425         (gnus-summary-clear-local-variables)
6426         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6427           (gnus-summary-clear-local-variables))
6428         (set-buffer gnus-group-buffer)
6429         (gnus-summary-clear-local-variables)
6430         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6431           (gnus-summary-clear-local-variables))
6432         (when (get-buffer gnus-summary-buffer)
6433           (kill-buffer gnus-summary-buffer)))
6434       (unless gnus-single-article-buffer
6435         (setq gnus-article-current nil))
6436       (when gnus-use-trees
6437         (gnus-tree-close group))
6438       (gnus-async-prefetch-remove-group group)
6439       (when (get-buffer gnus-article-buffer)
6440         (bury-buffer gnus-article-buffer))
6441       ;; Return to the group buffer.
6442       (gnus-configure-windows 'group 'force)
6443       ;; Clear the current group name.
6444       (setq gnus-newsgroup-name nil)
6445       (gnus-group-update-group group)
6446       (when (equal (gnus-group-group-name) group)
6447         (gnus-group-next-unread-group 1))
6448       (when quit-config
6449         (gnus-handle-ephemeral-exit quit-config)))))
6450
6451 (defun gnus-handle-ephemeral-exit (quit-config)
6452   "Handle movement when leaving an ephemeral group.
6453 The state which existed when entering the ephemeral is reset."
6454   (if (not (buffer-name (car quit-config)))
6455       (gnus-configure-windows 'group 'force)
6456     (set-buffer (car quit-config))
6457     (cond ((eq major-mode 'gnus-summary-mode)
6458            (gnus-set-global-variables))
6459           ((eq major-mode 'gnus-article-mode)
6460            (save-excursion
6461              ;; The `gnus-summary-buffer' variable may point
6462              ;; to the old summary buffer when using a single
6463              ;; article buffer.
6464              (unless (gnus-buffer-live-p gnus-summary-buffer)
6465                (set-buffer gnus-group-buffer))
6466              (set-buffer gnus-summary-buffer)
6467              (gnus-set-global-variables))))
6468     (if (or (eq (cdr quit-config) 'article)
6469             (eq (cdr quit-config) 'pick))
6470         (progn
6471           ;; The current article may be from the ephemeral group
6472           ;; thus it is best that we reload this article
6473           (gnus-summary-show-article)
6474           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6475               (gnus-configure-windows 'pick 'force)
6476             (gnus-configure-windows (cdr quit-config) 'force)))
6477       (gnus-configure-windows (cdr quit-config) 'force))
6478     (when (eq major-mode 'gnus-summary-mode)
6479       (gnus-summary-next-subject 1 nil t)
6480       (gnus-summary-recenter)
6481       (gnus-summary-position-point))))
6482
6483 ;;; Dead summaries.
6484
6485 (defvar gnus-dead-summary-mode-map nil)
6486
6487 (unless gnus-dead-summary-mode-map
6488   (setq gnus-dead-summary-mode-map (make-keymap))
6489   (suppress-keymap gnus-dead-summary-mode-map)
6490   (substitute-key-definition
6491    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6492   (dolist (key '("\C-d" "\r" "\177" [delete]))
6493     (define-key gnus-dead-summary-mode-map
6494       key 'gnus-summary-wake-up-the-dead))
6495   (dolist (key '("q" "Q"))
6496     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6497
6498 (defvar gnus-dead-summary-mode nil
6499   "Minor mode for Gnus summary buffers.")
6500
6501 (defun gnus-dead-summary-mode (&optional arg)
6502   "Minor mode for Gnus summary buffers."
6503   (interactive "P")
6504   (when (eq major-mode 'gnus-summary-mode)
6505     (make-local-variable 'gnus-dead-summary-mode)
6506     (setq gnus-dead-summary-mode
6507           (if (null arg) (not gnus-dead-summary-mode)
6508             (> (prefix-numeric-value arg) 0)))
6509     (when gnus-dead-summary-mode
6510       (gnus-add-minor-mode
6511        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6512
6513 (defun gnus-deaden-summary ()
6514   "Make the current summary buffer into a dead summary buffer."
6515   ;; Kill any previous dead summary buffer.
6516   (when (and gnus-dead-summary
6517              (buffer-name gnus-dead-summary))
6518     (save-excursion
6519       (set-buffer gnus-dead-summary)
6520       (when gnus-dead-summary-mode
6521         (kill-buffer (current-buffer)))))
6522   ;; Make this the current dead summary.
6523   (setq gnus-dead-summary (current-buffer))
6524   (gnus-dead-summary-mode 1)
6525   (let ((name (buffer-name)))
6526     (when (string-match "Summary" name)
6527       (rename-buffer
6528        (concat (substring name 0 (match-beginning 0)) "Dead "
6529                (substring name (match-beginning 0)))
6530        t)
6531       (bury-buffer))))
6532
6533 (defun gnus-kill-or-deaden-summary (buffer)
6534   "Kill or deaden the summary BUFFER."
6535   (save-excursion
6536     (when (and (buffer-name buffer)
6537                (not gnus-single-article-buffer))
6538       (save-excursion
6539         (set-buffer buffer)
6540         (gnus-kill-buffer gnus-article-buffer)
6541         (gnus-kill-buffer gnus-original-article-buffer)))
6542     (cond
6543      ;; Kill the buffer.
6544      (gnus-kill-summary-on-exit
6545       (when (and gnus-use-trees
6546                  (gnus-buffer-exists-p buffer))
6547         (save-excursion
6548           (set-buffer buffer)
6549           (gnus-tree-close gnus-newsgroup-name)))
6550       (gnus-kill-buffer buffer))
6551      ;; Deaden the buffer.
6552      ((gnus-buffer-exists-p buffer)
6553       (save-excursion
6554         (set-buffer buffer)
6555         (gnus-deaden-summary))))))
6556
6557 (defun gnus-summary-wake-up-the-dead (&rest args)
6558   "Wake up the dead summary buffer."
6559   (interactive)
6560   (gnus-dead-summary-mode -1)
6561   (let ((name (buffer-name)))
6562     (when (string-match "Dead " name)
6563       (rename-buffer
6564        (concat (substring name 0 (match-beginning 0))
6565                (substring name (match-end 0)))
6566        t)))
6567   (gnus-message 3 "This dead summary is now alive again"))
6568
6569 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6570 (defun gnus-summary-fetch-faq (&optional faq-dir)
6571   "Fetch the FAQ for the current group.
6572 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6573 in."
6574   (interactive
6575    (list
6576     (when current-prefix-arg
6577       (completing-read
6578        "FAQ dir: " (and (listp gnus-group-faq-directory)
6579                         (mapcar (lambda (file) (list file))
6580                                 gnus-group-faq-directory))))))
6581   (let (gnus-faq-buffer)
6582     (when (setq gnus-faq-buffer
6583                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6584       (gnus-configure-windows 'summary-faq))))
6585
6586 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6587 (defun gnus-summary-describe-group (&optional force)
6588   "Describe the current newsgroup."
6589   (interactive "P")
6590   (gnus-group-describe-group force gnus-newsgroup-name))
6591
6592 (defun gnus-summary-describe-briefly ()
6593   "Describe summary mode commands briefly."
6594   (interactive)
6595   (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")))
6596
6597 ;; Walking around group mode buffer from summary mode.
6598
6599 (defun gnus-summary-next-group (&optional no-article target-group backward)
6600   "Exit current newsgroup and then select next unread newsgroup.
6601 If prefix argument NO-ARTICLE is non-nil, no article is selected
6602 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6603 previous group instead."
6604   (interactive "P")
6605   ;; Stop pre-fetching.
6606   (gnus-async-halt-prefetch)
6607   (let ((current-group gnus-newsgroup-name)
6608         (current-buffer (current-buffer))
6609         entered)
6610     (while (not entered)
6611       ;; Then we find what group we are supposed to enter.
6612       (set-buffer gnus-group-buffer)
6613       (gnus-group-jump-to-group current-group)
6614       (setq target-group
6615             (or target-group
6616                 (if (eq gnus-keep-same-level 'best)
6617                     (gnus-summary-best-group gnus-newsgroup-name)
6618                   (gnus-summary-search-group backward gnus-keep-same-level))))
6619       (if (not target-group)
6620           ;; There are no further groups, so we return to the group
6621           ;; buffer.
6622           (progn
6623             (gnus-message 5 "Returning to the group buffer")
6624             (setq entered t)
6625             (when (gnus-buffer-live-p current-buffer)
6626               (set-buffer current-buffer)
6627               (gnus-summary-exit))
6628             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6629         ;; We try to enter the target group.
6630         (gnus-group-jump-to-group target-group)
6631         (let ((unreads (gnus-group-group-unread)))
6632           (if (and (or (eq t unreads)
6633                        (and unreads (not (zerop unreads))))
6634                    (progn
6635                      ;; Now we semi-exit this group to update Xrefs
6636                      ;; and all variables.  We can't do a real exit,
6637                      ;; because the window conf must remain the same
6638                      ;; in case the user is prompted for info, and we
6639                      ;; don't want the window conf to change before
6640                      ;; that...
6641                      (when (gnus-buffer-live-p current-buffer)
6642                        (set-buffer current-buffer)
6643                        (gnus-summary-exit t))
6644                      (gnus-summary-read-group
6645                       target-group nil no-article
6646                       (and (buffer-name current-buffer) current-buffer)
6647                       nil backward)))
6648               (setq entered t)
6649             (setq current-group target-group
6650                   target-group nil)))))))
6651
6652 (defun gnus-summary-prev-group (&optional no-article)
6653   "Exit current newsgroup and then select previous unread newsgroup.
6654 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6655   (interactive "P")
6656   (gnus-summary-next-group no-article nil t))
6657
6658 ;; Walking around summary lines.
6659
6660 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6661   "Go to the first unread subject.
6662 If UNREAD is non-nil, go to the first unread article.
6663 Returns the article selected or nil if there are no unread articles."
6664   (interactive "P")
6665   (prog1
6666       (cond
6667        ;; Empty summary.
6668        ((null gnus-newsgroup-data)
6669         (gnus-message 3 "No articles in the group")
6670         nil)
6671        ;; Pick the first article.
6672        ((not unread)
6673         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6674         (gnus-data-number (car gnus-newsgroup-data)))
6675        ;; No unread articles.
6676        ((null gnus-newsgroup-unreads)
6677         (gnus-message 3 "No more unread articles")
6678         nil)
6679        ;; Find the first unread article.
6680        (t
6681         (let ((data gnus-newsgroup-data))
6682           (while (and data
6683                       (and (not (and undownloaded
6684                                      (memq (car data) gnus-newsgroup-undownloaded)))
6685                            (if unseen
6686                                (or (not (memq
6687                                          (gnus-data-number (car data))
6688                                          gnus-newsgroup-unseen))
6689                                    (not (gnus-data-unread-p (car data))))
6690                              (not (gnus-data-unread-p (car data))))))
6691             (setq data (cdr data)))
6692           (when data
6693             (goto-char (gnus-data-pos (car data)))
6694             (gnus-data-number (car data))))))
6695     (gnus-summary-position-point)))
6696
6697 (defun gnus-summary-next-subject (n &optional unread dont-display)
6698   "Go to next N'th summary line.
6699 If N is negative, go to the previous N'th subject line.
6700 If UNREAD is non-nil, only unread articles are selected.
6701 The difference between N and the actual number of steps taken is
6702 returned."
6703   (interactive "p")
6704   (let ((backward (< n 0))
6705         (n (abs n)))
6706     (while (and (> n 0)
6707                 (if backward
6708                     (gnus-summary-find-prev unread)
6709                   (gnus-summary-find-next unread)))
6710       (unless (zerop (setq n (1- n)))
6711         (gnus-summary-show-thread)))
6712     (when (/= 0 n)
6713       (gnus-message 7 "No more%s articles"
6714                     (if unread " unread" "")))
6715     (unless dont-display
6716       (gnus-summary-recenter)
6717       (gnus-summary-position-point))
6718     n))
6719
6720 (defun gnus-summary-next-unread-subject (n)
6721   "Go to next N'th unread summary line."
6722   (interactive "p")
6723   (gnus-summary-next-subject n t))
6724
6725 (defun gnus-summary-prev-subject (n &optional unread)
6726   "Go to previous N'th summary line.
6727 If optional argument UNREAD is non-nil, only unread article is selected."
6728   (interactive "p")
6729   (gnus-summary-next-subject (- n) unread))
6730
6731 (defun gnus-summary-prev-unread-subject (n)
6732   "Go to previous N'th unread summary line."
6733   (interactive "p")
6734   (gnus-summary-next-subject (- n) t))
6735
6736 (defun gnus-summary-goto-subjects (articles)
6737   "Insert the subject header for ARTICLES in the current buffer."
6738   (save-excursion
6739     (dolist (article articles)
6740       (gnus-summary-goto-subject article t)))
6741   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6742   (gnus-summary-position-point))
6743   
6744 (defun gnus-summary-goto-subject (article &optional force silent)
6745   "Go the subject line of ARTICLE.
6746 If FORCE, also allow jumping to articles not currently shown."
6747   (interactive "nArticle number: ")
6748   (unless (numberp article)
6749     (error "Article %s is not a number" article))
6750   (let ((b (point))
6751         (data (gnus-data-find article)))
6752     ;; We read in the article if we have to.
6753     (and (not data)
6754          force
6755          (gnus-summary-insert-subject
6756           article
6757           (if (or (numberp force) (vectorp force)) force)
6758           t)
6759          (setq data (gnus-data-find article)))
6760     (goto-char b)
6761     (if (not data)
6762         (progn
6763           (unless silent
6764             (gnus-message 3 "Can't find article %d" article))
6765           nil)
6766       (let ((pt (gnus-data-pos data)))
6767         (goto-char pt)
6768         (gnus-summary-set-article-display-arrow pt))
6769       (gnus-summary-position-point)
6770       article)))
6771
6772 ;; Walking around summary lines with displaying articles.
6773
6774 (defun gnus-summary-expand-window (&optional arg)
6775   "Make the summary buffer take up the entire Emacs frame.
6776 Given a prefix, will force an `article' buffer configuration."
6777   (interactive "P")
6778   (if arg
6779       (gnus-configure-windows 'article 'force)
6780     (gnus-configure-windows 'summary 'force)))
6781
6782 (defun gnus-summary-display-article (article &optional all-header)
6783   "Display ARTICLE in article buffer."
6784   (when (gnus-buffer-live-p gnus-article-buffer)
6785     (with-current-buffer gnus-article-buffer
6786       (mm-enable-multibyte)))
6787   (gnus-set-global-variables)
6788   (when (gnus-buffer-live-p gnus-article-buffer)
6789     (with-current-buffer gnus-article-buffer
6790       (setq gnus-article-charset gnus-newsgroup-charset)
6791       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6792       (mm-enable-multibyte)))
6793   (if (null article)
6794       nil
6795     (prog1
6796         (if gnus-summary-display-article-function
6797             (funcall gnus-summary-display-article-function article all-header)
6798           (gnus-article-prepare article all-header))
6799       (gnus-run-hooks 'gnus-select-article-hook)
6800       (when (and gnus-current-article
6801                  (not (zerop gnus-current-article)))
6802         (gnus-summary-goto-subject gnus-current-article))
6803       (gnus-summary-recenter)
6804       (when (and gnus-use-trees gnus-show-threads)
6805         (gnus-possibly-generate-tree article)
6806         (gnus-highlight-selected-tree article))
6807       ;; Successfully display article.
6808       (gnus-article-set-window-start
6809        (cdr (assq article gnus-newsgroup-bookmarks))))))
6810
6811 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6812   "Select the current article.
6813 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6814 non-nil, the article will be re-fetched even if it already present in
6815 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6816 be displayed."
6817   ;; Make sure we are in the summary buffer to work around bbdb bug.
6818   (unless (eq major-mode 'gnus-summary-mode)
6819     (set-buffer gnus-summary-buffer))
6820   (let ((article (or article (gnus-summary-article-number)))
6821         (all-headers (not (not all-headers))) ;Must be t or nil.
6822         gnus-summary-display-article-function)
6823     (and (not pseudo)
6824          (gnus-summary-article-pseudo-p article)
6825          (error "This is a pseudo-article"))
6826     (save-excursion
6827       (set-buffer gnus-summary-buffer)
6828       (if (or (and gnus-single-article-buffer
6829                    (or (null gnus-current-article)
6830                        (null gnus-article-current)
6831                        (null (get-buffer gnus-article-buffer))
6832                        (not (eq article (cdr gnus-article-current)))
6833                        (not (equal (car gnus-article-current)
6834                                    gnus-newsgroup-name))))
6835               (and (not gnus-single-article-buffer)
6836                    (or (null gnus-current-article)
6837                        (not (eq gnus-current-article article))))
6838               force)
6839           ;; The requested article is different from the current article.
6840           (progn
6841             (gnus-summary-display-article article all-headers)
6842             (when (gnus-buffer-live-p gnus-article-buffer)
6843               (with-current-buffer gnus-article-buffer
6844                 (if (not gnus-article-decoded-p) ;; a local variable
6845                     (mm-disable-multibyte))))
6846             (gnus-article-set-window-start
6847              (cdr (assq article gnus-newsgroup-bookmarks)))
6848             article)
6849         'old))))
6850
6851 (defun gnus-summary-force-verify-and-decrypt ()
6852   (interactive)
6853   (let ((mm-verify-option 'known)
6854         (mm-decrypt-option 'known)
6855         (gnus-buttonized-mime-types (append (list "multipart/signed"
6856                                                   "multipart/encrypted")
6857                                             gnus-buttonized-mime-types)))
6858     (gnus-summary-select-article nil 'force)))
6859
6860 (defun gnus-summary-set-current-mark (&optional current-mark)
6861   "Obsolete function."
6862   nil)
6863
6864 (defun gnus-summary-next-article (&optional unread subject backward push)
6865   "Select the next article.
6866 If UNREAD, only unread articles are selected.
6867 If SUBJECT, only articles with SUBJECT are selected.
6868 If BACKWARD, the previous article is selected instead of the next."
6869   (interactive "P")
6870   (cond
6871    ;; Is there such an article?
6872    ((and (gnus-summary-search-forward unread subject backward)
6873          (or (gnus-summary-display-article (gnus-summary-article-number))
6874              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6875     (gnus-summary-position-point))
6876    ;; If not, we try the first unread, if that is wanted.
6877    ((and subject
6878          gnus-auto-select-same
6879          (gnus-summary-first-unread-article))
6880     (gnus-summary-position-point)
6881     (gnus-message 6 "Wrapped"))
6882    ;; Try to get next/previous article not displayed in this group.
6883    ((and gnus-auto-extend-newsgroup
6884          (not unread) (not subject))
6885     (gnus-summary-goto-article
6886      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6887      nil (count-lines (point-min) (point))))
6888    ;; Go to next/previous group.
6889    (t
6890     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6891       (gnus-summary-jump-to-group gnus-newsgroup-name))
6892     (let ((cmd last-command-char)
6893           (point
6894            (save-excursion
6895              (set-buffer gnus-group-buffer)
6896              (point)))
6897           (group
6898            (if (eq gnus-keep-same-level 'best)
6899                (gnus-summary-best-group gnus-newsgroup-name)
6900              (gnus-summary-search-group backward gnus-keep-same-level))))
6901       ;; For some reason, the group window gets selected.  We change
6902       ;; it back.
6903       (select-window (get-buffer-window (current-buffer)))
6904       ;; Select next unread newsgroup automagically.
6905       (cond
6906        ((or (not gnus-auto-select-next)
6907             (not cmd))
6908         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6909        ((or (eq gnus-auto-select-next 'quietly)
6910             (and (eq gnus-auto-select-next 'slightly-quietly)
6911                  push)
6912             (and (eq gnus-auto-select-next 'almost-quietly)
6913                  (gnus-summary-last-article-p)))
6914         ;; Select quietly.
6915         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6916             (gnus-summary-exit)
6917           (gnus-message 7 "No more%s articles (%s)..."
6918                         (if unread " unread" "")
6919                         (if group (concat "selecting " group)
6920                           "exiting"))
6921           (gnus-summary-next-group nil group backward)))
6922        (t
6923         (when (gnus-key-press-event-p last-input-event)
6924           (gnus-summary-walk-group-buffer
6925            gnus-newsgroup-name cmd unread backward point))))))))
6926
6927 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6928   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6929                       (?\C-p (gnus-group-prev-unread-group 1))))
6930         (cursor-in-echo-area t)
6931         keve key group ended prompt)
6932     (save-excursion
6933       (set-buffer gnus-group-buffer)
6934       (goto-char start)
6935       (setq group
6936             (if (eq gnus-keep-same-level 'best)
6937                 (gnus-summary-best-group gnus-newsgroup-name)
6938               (gnus-summary-search-group backward gnus-keep-same-level))))
6939     (while (not ended)
6940       (setq prompt
6941             (format
6942              "No more%s articles%s " (if unread " unread" "")
6943              (if (and group
6944                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6945                  (format " (Type %s for %s [%s])"
6946                          (single-key-description cmd) group
6947                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6948                (format " (Type %s to exit %s)"
6949                        (single-key-description cmd)
6950                        gnus-newsgroup-name))))
6951       ;; Confirm auto selection.
6952       (setq key (car (setq keve (gnus-read-event-char prompt)))
6953             ended t)
6954       (cond
6955        ((assq key keystrokes)
6956         (let ((obuf (current-buffer)))
6957           (switch-to-buffer gnus-group-buffer)
6958           (when group
6959             (gnus-group-jump-to-group group))
6960           (eval (cadr (assq key keystrokes)))
6961           (setq group (gnus-group-group-name))
6962           (switch-to-buffer obuf))
6963         (setq ended nil))
6964        ((equal key cmd)
6965         (if (or (not group)
6966                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6967             (gnus-summary-exit)
6968           (gnus-summary-next-group nil group backward)))
6969        (t
6970         (push (cdr keve) unread-command-events))))))
6971
6972 (defun gnus-summary-next-unread-article ()
6973   "Select unread article after current one."
6974   (interactive)
6975   (gnus-summary-next-article
6976    (or (not (eq gnus-summary-goto-unread 'never))
6977        (gnus-summary-last-article-p (gnus-summary-article-number)))
6978    (and gnus-auto-select-same
6979         (gnus-summary-article-subject))))
6980
6981 (defun gnus-summary-prev-article (&optional unread subject)
6982   "Select the article after the current one.
6983 If UNREAD is non-nil, only unread articles are selected."
6984   (interactive "P")
6985   (gnus-summary-next-article unread subject t))
6986
6987 (defun gnus-summary-prev-unread-article ()
6988   "Select unread article before current one."
6989   (interactive)
6990   (gnus-summary-prev-article
6991    (or (not (eq gnus-summary-goto-unread 'never))
6992        (gnus-summary-first-article-p (gnus-summary-article-number)))
6993    (and gnus-auto-select-same
6994         (gnus-summary-article-subject))))
6995
6996 (defun gnus-summary-next-page (&optional lines circular stop)
6997   "Show next page of the selected article.
6998 If at the end of the current article, select the next article.
6999 LINES says how many lines should be scrolled up.
7000
7001 If CIRCULAR is non-nil, go to the start of the article instead of
7002 selecting the next article when reaching the end of the current
7003 article.
7004
7005 If STOP is non-nil, just stop when reaching the end of the message."
7006   (interactive "P")
7007   (setq gnus-summary-buffer (current-buffer))
7008   (gnus-set-global-variables)
7009   (let ((article (gnus-summary-article-number))
7010         (article-window (get-buffer-window gnus-article-buffer t))
7011         endp)
7012     ;; If the buffer is empty, we have no article.
7013     (unless article
7014       (error "No article to select"))
7015     (gnus-configure-windows 'article)
7016     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7017         (if (and (eq gnus-summary-goto-unread 'never)
7018                  (not (gnus-summary-last-article-p article)))
7019             (gnus-summary-next-article)
7020           (gnus-summary-next-unread-article))
7021       (if (or (null gnus-current-article)
7022               (null gnus-article-current)
7023               (/= article (cdr gnus-article-current))
7024               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7025           ;; Selected subject is different from current article's.
7026           (gnus-summary-display-article article)
7027         (when article-window
7028           (gnus-eval-in-buffer-window gnus-article-buffer
7029             (setq endp (gnus-article-next-page lines)))
7030           (when endp
7031             (cond (stop
7032                    (gnus-message 3 "End of message"))
7033                   (circular
7034                    (gnus-summary-beginning-of-article))
7035                   (lines
7036                    (gnus-message 3 "End of message"))
7037                   ((null lines)
7038                    (if (and (eq gnus-summary-goto-unread 'never)
7039                             (not (gnus-summary-last-article-p article)))
7040                        (gnus-summary-next-article)
7041                      (gnus-summary-next-unread-article))))))))
7042     (gnus-summary-recenter)
7043     (gnus-summary-position-point)))
7044
7045 (defun gnus-summary-prev-page (&optional lines move)
7046   "Show previous page of selected article.
7047 Argument LINES specifies lines to be scrolled down.
7048 If MOVE, move to the previous unread article if point is at
7049 the beginning of the buffer."
7050   (interactive "P")
7051   (let ((article (gnus-summary-article-number))
7052         (article-window (get-buffer-window gnus-article-buffer t))
7053         endp)
7054     (gnus-configure-windows 'article)
7055     (if (or (null gnus-current-article)
7056             (null gnus-article-current)
7057             (/= article (cdr gnus-article-current))
7058             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7059         ;; Selected subject is different from current article's.
7060         (gnus-summary-display-article article)
7061       (gnus-summary-recenter)
7062       (when article-window
7063         (gnus-eval-in-buffer-window gnus-article-buffer
7064           (setq endp (gnus-article-prev-page lines)))
7065         (when (and move endp)
7066           (cond (lines
7067                  (gnus-message 3 "Beginning of message"))
7068                 ((null lines)
7069                  (if (and (eq gnus-summary-goto-unread 'never)
7070                           (not (gnus-summary-first-article-p article)))
7071                      (gnus-summary-prev-article)
7072                    (gnus-summary-prev-unread-article))))))))
7073   (gnus-summary-position-point))
7074
7075 (defun gnus-summary-prev-page-or-article (&optional lines)
7076   "Show previous page of selected article.
7077 Argument LINES specifies lines to be scrolled down.
7078 If at the beginning of the article, go to the next article."
7079   (interactive "P")
7080   (gnus-summary-prev-page lines t))
7081
7082 (defun gnus-summary-scroll-up (lines)
7083   "Scroll up (or down) one line current article.
7084 Argument LINES specifies lines to be scrolled up (or down if negative)."
7085   (interactive "p")
7086   (gnus-configure-windows 'article)
7087   (gnus-summary-show-thread)
7088   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7089     (gnus-eval-in-buffer-window gnus-article-buffer
7090       (cond ((> lines 0)
7091              (when (gnus-article-next-page lines)
7092                (gnus-message 3 "End of message")))
7093             ((< lines 0)
7094              (gnus-article-prev-page (- lines))))))
7095   (gnus-summary-recenter)
7096   (gnus-summary-position-point))
7097
7098 (defun gnus-summary-scroll-down (lines)
7099   "Scroll down (or up) one line current article.
7100 Argument LINES specifies lines to be scrolled down (or up if negative)."
7101   (interactive "p")
7102   (gnus-summary-scroll-up (- lines)))
7103
7104 (defun gnus-summary-next-same-subject ()
7105   "Select next article which has the same subject as current one."
7106   (interactive)
7107   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7108
7109 (defun gnus-summary-prev-same-subject ()
7110   "Select previous article which has the same subject as current one."
7111   (interactive)
7112   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7113
7114 (defun gnus-summary-next-unread-same-subject ()
7115   "Select next unread article which has the same subject as current one."
7116   (interactive)
7117   (gnus-summary-next-article t (gnus-summary-article-subject)))
7118
7119 (defun gnus-summary-prev-unread-same-subject ()
7120   "Select previous unread article which has the same subject as current one."
7121   (interactive)
7122   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7123
7124 (defun gnus-summary-first-unread-article ()
7125   "Select the first unread article.
7126 Return nil if there are no unread articles."
7127   (interactive)
7128   (prog1
7129       (when (gnus-summary-first-subject t)
7130         (gnus-summary-show-thread)
7131         (gnus-summary-first-subject t)
7132         (gnus-summary-display-article (gnus-summary-article-number)))
7133     (gnus-summary-position-point)))
7134
7135 (defun gnus-summary-first-unread-subject ()
7136   "Place the point on the subject line of the first unread article.
7137 Return nil if there are no unread articles."
7138   (interactive)
7139   (prog1
7140       (when (gnus-summary-first-subject t)
7141         (gnus-summary-show-thread)
7142         (gnus-summary-first-subject t))
7143     (gnus-summary-position-point)))
7144
7145 (defun gnus-summary-first-unseen-subject ()
7146   "Place the point on the subject line of the first unseen article.
7147 Return nil if there are no unseen articles."
7148   (interactive)
7149   (prog1
7150       (when (gnus-summary-first-subject t t t)
7151         (gnus-summary-show-thread)
7152         (gnus-summary-first-subject t t t))
7153     (gnus-summary-position-point)))
7154
7155 (defun gnus-summary-first-unseen-or-unread-subject ()
7156   "Place the point on the subject line of the first unseen article.
7157 Return nil if there are no unseen articles."
7158   (interactive)
7159   (prog1
7160       (unless (when (gnus-summary-first-subject t t t)
7161                 (gnus-summary-show-thread)
7162                 (gnus-summary-first-subject t t t))
7163         (when (gnus-summary-first-subject t)
7164           (gnus-summary-show-thread)
7165           (gnus-summary-first-subject t)))
7166     (gnus-summary-position-point)))
7167
7168 (defun gnus-summary-first-article ()
7169   "Select the first article.
7170 Return nil if there are no articles."
7171   (interactive)
7172   (prog1
7173       (when (gnus-summary-first-subject)
7174         (gnus-summary-show-thread)
7175         (gnus-summary-first-subject)
7176         (gnus-summary-display-article (gnus-summary-article-number)))
7177     (gnus-summary-position-point)))
7178
7179 (defun gnus-summary-best-unread-article (&optional arg)
7180   "Select the unread article with the highest score.
7181 If given a prefix argument, select the next unread article that has a
7182 score higher than the default score."
7183   (interactive "P")
7184   (let ((article (if arg
7185                      (gnus-summary-better-unread-subject)
7186                    (gnus-summary-best-unread-subject))))
7187     (if article
7188         (gnus-summary-goto-article article)
7189       (error "No unread articles"))))
7190
7191 (defun gnus-summary-best-unread-subject ()
7192   "Select the unread subject with the highest score."
7193   (interactive)
7194   (let ((best -1000000)
7195         (data gnus-newsgroup-data)
7196         article score)
7197     (while data
7198       (and (gnus-data-unread-p (car data))
7199            (> (setq score
7200                     (gnus-summary-article-score (gnus-data-number (car data))))
7201               best)
7202            (setq best score
7203                  article (gnus-data-number (car data))))
7204       (setq data (cdr data)))
7205     (when article
7206       (gnus-summary-goto-subject article))
7207     (gnus-summary-position-point)
7208     article))
7209
7210 (defun gnus-summary-better-unread-subject ()
7211   "Select the first unread subject that has a score over the default score."
7212   (interactive)
7213   (let ((data gnus-newsgroup-data)
7214         article score)
7215     (while (and (setq article (gnus-data-number (car data)))
7216                 (or (gnus-data-read-p (car data))
7217                     (not (> (gnus-summary-article-score article)
7218                             gnus-summary-default-score))))
7219       (setq data (cdr data)))
7220     (when article
7221       (gnus-summary-goto-subject article))
7222     (gnus-summary-position-point)
7223     article))
7224
7225 (defun gnus-summary-last-subject ()
7226   "Go to the last displayed subject line in the group."
7227   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7228     (when article
7229       (gnus-summary-goto-subject article))))
7230
7231 (defun gnus-summary-goto-article (article &optional all-headers force)
7232   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7233 If ALL-HEADERS is non-nil, no header lines are hidden.
7234 If FORCE, go to the article even if it isn't displayed.  If FORCE
7235 is a number, it is the line the article is to be displayed on."
7236   (interactive
7237    (list
7238     (completing-read
7239      "Article number or Message-ID: "
7240      (mapcar (lambda (number) (list (int-to-string number)))
7241              gnus-newsgroup-limit))
7242     current-prefix-arg
7243     t))
7244   (prog1
7245       (if (and (stringp article)
7246                (string-match "@" article))
7247           (gnus-summary-refer-article article)
7248         (when (stringp article)
7249           (setq article (string-to-number article)))
7250         (if (gnus-summary-goto-subject article force)
7251             (gnus-summary-display-article article all-headers)
7252           (gnus-message 4 "Couldn't go to article %s" article) nil))
7253     (gnus-summary-position-point)))
7254
7255 (defun gnus-summary-goto-last-article ()
7256   "Go to the previously read article."
7257   (interactive)
7258   (prog1
7259       (when gnus-last-article
7260         (gnus-summary-goto-article gnus-last-article nil t))
7261     (gnus-summary-position-point)))
7262
7263 (defun gnus-summary-pop-article (number)
7264   "Pop one article off the history and go to the previous.
7265 NUMBER articles will be popped off."
7266   (interactive "p")
7267   (let (to)
7268     (setq gnus-newsgroup-history
7269           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7270     (if to
7271         (gnus-summary-goto-article (car to) nil t)
7272       (error "Article history empty")))
7273   (gnus-summary-position-point))
7274
7275 ;; Summary commands and functions for limiting the summary buffer.
7276
7277 (defun gnus-summary-limit-to-articles (n)
7278   "Limit the summary buffer to the next N articles.
7279 If not given a prefix, use the process marked articles instead."
7280   (interactive "P")
7281   (prog1
7282       (let ((articles (gnus-summary-work-articles n)))
7283         (setq gnus-newsgroup-processable nil)
7284         (gnus-summary-limit articles))
7285     (gnus-summary-position-point)))
7286
7287 (defun gnus-summary-pop-limit (&optional total)
7288   "Restore the previous limit.
7289 If given a prefix, remove all limits."
7290   (interactive "P")
7291   (when total
7292     (setq gnus-newsgroup-limits
7293           (list (mapcar (lambda (h) (mail-header-number h))
7294                         gnus-newsgroup-headers))))
7295   (unless gnus-newsgroup-limits
7296     (error "No limit to pop"))
7297   (prog1
7298       (gnus-summary-limit nil 'pop)
7299     (gnus-summary-position-point)))
7300
7301 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7302   "Limit the summary buffer to articles that have subjects that match a regexp.
7303 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7304   (interactive
7305    (list (read-string (if current-prefix-arg
7306                           "Exclude subject (regexp): "
7307                         "Limit to subject (regexp): "))
7308          nil current-prefix-arg))
7309   (unless header
7310     (setq header "subject"))
7311   (when (not (equal "" subject))
7312     (prog1
7313         (let ((articles (gnus-summary-find-matching
7314                          (or header "subject") subject 'all nil nil
7315                          not-matching)))
7316           (unless articles
7317             (error "Found no matches for \"%s\"" subject))
7318           (gnus-summary-limit articles))
7319       (gnus-summary-position-point))))
7320
7321 (defun gnus-summary-limit-to-author (from &optional not-matching)
7322   "Limit the summary buffer to articles that have authors that match a regexp.
7323 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7324   (interactive
7325    (list (read-string (if current-prefix-arg
7326                           "Exclude author (regexp): "
7327                         "Limit to author (regexp): "))
7328          current-prefix-arg))
7329   (gnus-summary-limit-to-subject from "from" not-matching))
7330
7331 (defun gnus-summary-limit-to-age (age &optional younger-p)
7332   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7333 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7334 articles that are younger than AGE days."
7335   (interactive
7336    (let ((younger current-prefix-arg)
7337          (days-got nil)
7338          days)
7339      (while (not days-got)
7340        (setq days (if younger
7341                       (read-string "Limit to articles younger than (in days, older when negative): ")
7342                     (read-string
7343                      "Limit to articles older than (in days, younger when negative): ")))
7344        (when (> (length days) 0)
7345          (setq days (read days)))
7346        (if (numberp days)
7347            (progn
7348              (setq days-got t)
7349              (if (< days 0)
7350                  (progn
7351                    (setq younger (not younger))
7352                    (setq days (* days -1)))))
7353          (message "Please enter a number.")
7354          (sleep-for 1)))
7355      (list days younger)))
7356   (prog1
7357       (let ((data gnus-newsgroup-data)
7358             (cutoff (days-to-time age))
7359             articles d date is-younger)
7360         (while (setq d (pop data))
7361           (when (and (vectorp (gnus-data-header d))
7362                      (setq date (mail-header-date (gnus-data-header d))))
7363             (setq is-younger (time-less-p
7364                               (time-since (condition-case ()
7365                                               (date-to-time date)
7366                                             (error '(0 0))))
7367                               cutoff))
7368             (when (if younger-p
7369                       is-younger
7370                     (not is-younger))
7371               (push (gnus-data-number d) articles))))
7372         (gnus-summary-limit (nreverse articles)))
7373     (gnus-summary-position-point)))
7374
7375 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7376   "Limit the summary buffer to articles that match an 'extra' header."
7377   (interactive
7378    (let ((header
7379           (intern
7380            (gnus-completing-read-with-default
7381             (symbol-name (car gnus-extra-headers))
7382             (if current-prefix-arg
7383                 "Exclude extra header:"
7384               "Limit extra header:")
7385             (mapcar (lambda (x)
7386                       (cons (symbol-name x) x))
7387                     gnus-extra-headers)
7388             nil
7389             t))))
7390      (list header
7391            (read-string (format "%s header %s (regexp): "
7392                                 (if current-prefix-arg "Exclude" "Limit to")
7393                                 header))
7394            current-prefix-arg)))
7395   (when (not (equal "" regexp))
7396     (prog1
7397         (let ((articles (gnus-summary-find-matching
7398                          (cons 'extra header) regexp 'all nil nil
7399                          not-matching)))
7400           (unless articles
7401             (error "Found no matches for \"%s\"" regexp))
7402           (gnus-summary-limit articles))
7403       (gnus-summary-position-point))))
7404
7405 (defun gnus-summary-limit-to-display-predicate ()
7406   "Limit the summary buffer to the predicated in the `display' group parameter."
7407   (interactive)
7408   (unless gnus-newsgroup-display
7409     (error "There is no `display' group parameter"))
7410   (let (articles)
7411     (dolist (number gnus-newsgroup-articles)
7412       (when (funcall gnus-newsgroup-display)
7413         (push number articles)))
7414     (gnus-summary-limit articles))
7415   (gnus-summary-position-point))
7416
7417 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7418 (make-obsolete
7419  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7420
7421 (defun gnus-summary-limit-to-unread (&optional all)
7422   "Limit the summary buffer to articles that are not marked as read.
7423 If ALL is non-nil, limit strictly to unread articles."
7424   (interactive "P")
7425   (if all
7426       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7427     (gnus-summary-limit-to-marks
7428      ;; Concat all the marks that say that an article is read and have
7429      ;; those removed.
7430      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7431            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7432            gnus-low-score-mark gnus-expirable-mark
7433            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7434            gnus-duplicate-mark gnus-souped-mark)
7435      'reverse)))
7436
7437 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7438 (make-obsolete 'gnus-summary-delete-marked-with
7439                'gnus-summary-limit-exclude-marks)
7440
7441 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7442   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7443 If REVERSE, limit the summary buffer to articles that are marked
7444 with MARKS.  MARKS can either be a string of marks or a list of marks.
7445 Returns how many articles were removed."
7446   (interactive "sMarks: ")
7447   (gnus-summary-limit-to-marks marks t))
7448
7449 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7450   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7451 If REVERSE (the prefix), limit the summary buffer to articles that are
7452 not marked with MARKS.  MARKS can either be a string of marks or a
7453 list of marks.
7454 Returns how many articles were removed."
7455   (interactive "sMarks: \nP")
7456   (prog1
7457       (let ((data gnus-newsgroup-data)
7458             (marks (if (listp marks) marks
7459                      (append marks nil))) ; Transform to list.
7460             articles)
7461         (while data
7462           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7463                   (memq (gnus-data-mark (car data)) marks))
7464             (push (gnus-data-number (car data)) articles))
7465           (setq data (cdr data)))
7466         (gnus-summary-limit articles))
7467     (gnus-summary-position-point)))
7468
7469 (defun gnus-summary-limit-to-score (score)
7470   "Limit to articles with score at or above SCORE."
7471   (interactive "NLimit to articles with score of at least: ")
7472   (let ((data gnus-newsgroup-data)
7473         articles)
7474     (while data
7475       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7476                 score)
7477         (push (gnus-data-number (car data)) articles))
7478       (setq data (cdr data)))
7479     (prog1
7480         (gnus-summary-limit articles)
7481       (gnus-summary-position-point))))
7482
7483 (defun gnus-summary-limit-to-unseen ()
7484   "Limit to unseen articles."
7485   (interactive)
7486   (prog1
7487       (gnus-summary-limit gnus-newsgroup-unseen)
7488     (gnus-summary-position-point)))
7489
7490 (defun gnus-summary-limit-include-thread (id)
7491   "Display all the hidden articles that is in the thread with ID in it.
7492 When called interactively, ID is the Message-ID of the current
7493 article."
7494   (interactive (list (mail-header-id (gnus-summary-article-header))))
7495   (let ((articles (gnus-articles-in-thread
7496                    (gnus-id-to-thread (gnus-root-id id)))))
7497     (prog1
7498         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7499       (gnus-summary-limit-include-matching-articles
7500        "subject"
7501        (regexp-quote (gnus-simplify-subject-re
7502                       (mail-header-subject (gnus-id-to-header id)))))
7503       (gnus-summary-position-point))))
7504
7505 (defun gnus-summary-limit-include-matching-articles (header regexp)
7506   "Display all the hidden articles that have HEADERs that match REGEXP."
7507   (interactive (list (read-string "Match on header: ")
7508                      (read-string "Regexp: ")))
7509   (let ((articles (gnus-find-matching-articles header regexp)))
7510     (prog1
7511         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7512       (gnus-summary-position-point))))
7513
7514 (defun gnus-summary-insert-dormant-articles ()
7515   "Insert all the dormat articles for this group into the current buffer."
7516   (interactive)
7517   (let ((gnus-verbose (max 6 gnus-verbose)))
7518     (if (not gnus-newsgroup-dormant)
7519         (gnus-message 3 "No cached articles for this group")
7520       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7521
7522 (defun gnus-summary-limit-include-dormant ()
7523   "Display all the hidden articles that are marked as dormant.
7524 Note that this command only works on a subset of the articles currently
7525 fetched for this group."
7526   (interactive)
7527   (unless gnus-newsgroup-dormant
7528     (error "There are no dormant articles in this group"))
7529   (prog1
7530       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7531     (gnus-summary-position-point)))
7532
7533 (defun gnus-summary-limit-exclude-dormant ()
7534   "Hide all dormant articles."
7535   (interactive)
7536   (prog1
7537       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7538     (gnus-summary-position-point)))
7539
7540 (defun gnus-summary-limit-exclude-childless-dormant ()
7541   "Hide all dormant articles that have no children."
7542   (interactive)
7543   (let ((data (gnus-data-list t))
7544         articles d children)
7545     ;; Find all articles that are either not dormant or have
7546     ;; children.
7547     (while (setq d (pop data))
7548       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7549                 (and (setq children
7550                            (gnus-article-children (gnus-data-number d)))
7551                      (let (found)
7552                        (while children
7553                          (when (memq (car children) articles)
7554                            (setq children nil
7555                                  found t))
7556                          (pop children))
7557                        found)))
7558         (push (gnus-data-number d) articles)))
7559     ;; Do the limiting.
7560     (prog1
7561         (gnus-summary-limit articles)
7562       (gnus-summary-position-point))))
7563
7564 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7565   "Mark all unread excluded articles as read.
7566 If ALL, mark even excluded ticked and dormants as read."
7567   (interactive "P")
7568   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7569   (let ((articles (gnus-sorted-ndifference
7570                    (sort
7571                     (mapcar (lambda (h) (mail-header-number h))
7572                             gnus-newsgroup-headers)
7573                     '<)
7574                    gnus-newsgroup-limit))
7575         article)
7576     (setq gnus-newsgroup-unreads
7577           (gnus-sorted-intersection gnus-newsgroup-unreads
7578                                     gnus-newsgroup-limit))
7579     (if all
7580         (setq gnus-newsgroup-dormant nil
7581               gnus-newsgroup-marked nil
7582               gnus-newsgroup-reads
7583               (nconc
7584                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7585                gnus-newsgroup-reads))
7586       (while (setq article (pop articles))
7587         (unless (or (memq article gnus-newsgroup-dormant)
7588                     (memq article gnus-newsgroup-marked))
7589           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7590
7591 (defun gnus-summary-limit (articles &optional pop)
7592   (if pop
7593       ;; We pop the previous limit off the stack and use that.
7594       (setq articles (car gnus-newsgroup-limits)
7595             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7596     ;; We use the new limit, so we push the old limit on the stack.
7597     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7598   ;; Set the limit.
7599   (setq gnus-newsgroup-limit articles)
7600   (let ((total (length gnus-newsgroup-data))
7601         (data (gnus-data-find-list (gnus-summary-article-number)))
7602         (gnus-summary-mark-below nil)   ; Inhibit this.
7603         found)
7604     ;; This will do all the work of generating the new summary buffer
7605     ;; according to the new limit.
7606     (gnus-summary-prepare)
7607     ;; Hide any threads, possibly.
7608     (gnus-summary-maybe-hide-threads)
7609     ;; Try to return to the article you were at, or one in the
7610     ;; neighborhood.
7611     (when data
7612       ;; We try to find some article after the current one.
7613       (while data
7614         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7615           (setq data nil
7616                 found t))
7617         (setq data (cdr data))))
7618     (unless found
7619       ;; If there is no data, that means that we were after the last
7620       ;; article.  The same goes when we can't find any articles
7621       ;; after the current one.
7622       (goto-char (point-max))
7623       (gnus-summary-find-prev))
7624     (gnus-set-mode-line 'summary)
7625     ;; We return how many articles were removed from the summary
7626     ;; buffer as a result of the new limit.
7627     (- total (length gnus-newsgroup-data))))
7628
7629 (defsubst gnus-invisible-cut-children (threads)
7630   (let ((num 0))
7631     (while threads
7632       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7633         (incf num))
7634       (pop threads))
7635     (< num 2)))
7636
7637 (defsubst gnus-cut-thread (thread)
7638   "Go forwards in the thread until we find an article that we want to display."
7639   (when (or (eq gnus-fetch-old-headers 'some)
7640             (eq gnus-fetch-old-headers 'invisible)
7641             (numberp gnus-fetch-old-headers)
7642             (eq gnus-build-sparse-threads 'some)
7643             (eq gnus-build-sparse-threads 'more))
7644     ;; Deal with old-fetched headers and sparse threads.
7645     (while (and
7646             thread
7647             (or
7648              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7649              (gnus-summary-article-ancient-p
7650               (mail-header-number (car thread))))
7651             (if (or (<= (length (cdr thread)) 1)
7652                     (eq gnus-fetch-old-headers 'invisible))
7653                 (setq gnus-newsgroup-limit
7654                       (delq (mail-header-number (car thread))
7655                             gnus-newsgroup-limit)
7656                       thread (cadr thread))
7657               (when (gnus-invisible-cut-children (cdr thread))
7658                 (let ((th (cdr thread)))
7659                   (while th
7660                     (if (memq (mail-header-number (caar th))
7661                               gnus-newsgroup-limit)
7662                         (setq thread (car th)
7663                               th nil)
7664                       (setq th (cdr th))))))))))
7665   thread)
7666
7667 (defun gnus-cut-threads (threads)
7668   "Cut off all uninteresting articles from the beginning of threads."
7669   (when (or (eq gnus-fetch-old-headers 'some)
7670             (eq gnus-fetch-old-headers 'invisible)
7671             (numberp gnus-fetch-old-headers)
7672             (eq gnus-build-sparse-threads 'some)
7673             (eq gnus-build-sparse-threads 'more))
7674     (let ((th threads))
7675       (while th
7676         (setcar th (gnus-cut-thread (car th)))
7677         (setq th (cdr th)))))
7678   ;; Remove nixed out threads.
7679   (delq nil threads))
7680
7681 (defun gnus-summary-initial-limit (&optional show-if-empty)
7682   "Figure out what the initial limit is supposed to be on group entry.
7683 This entails weeding out unwanted dormants, low-scored articles,
7684 fetch-old-headers verbiage, and so on."
7685   ;; Most groups have nothing to remove.
7686   (if (or gnus-inhibit-limiting
7687           (and (null gnus-newsgroup-dormant)
7688                (eq gnus-newsgroup-display 'gnus-not-ignore)
7689                (not (eq gnus-fetch-old-headers 'some))
7690                (not (numberp gnus-fetch-old-headers))
7691                (not (eq gnus-fetch-old-headers 'invisible))
7692                (null gnus-summary-expunge-below)
7693                (not (eq gnus-build-sparse-threads 'some))
7694                (not (eq gnus-build-sparse-threads 'more))
7695                (null gnus-thread-expunge-below)
7696                (not gnus-use-nocem)))
7697       ()                                ; Do nothing.
7698     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7699     (setq gnus-newsgroup-limit nil)
7700     (mapatoms
7701      (lambda (node)
7702        (unless (car (symbol-value node))
7703          ;; These threads have no parents -- they are roots.
7704          (let ((nodes (cdr (symbol-value node)))
7705                thread)
7706            (while nodes
7707              (if (and gnus-thread-expunge-below
7708                       (< (gnus-thread-total-score (car nodes))
7709                          gnus-thread-expunge-below))
7710                  (gnus-expunge-thread (pop nodes))
7711                (setq thread (pop nodes))
7712                (gnus-summary-limit-children thread))))))
7713      gnus-newsgroup-dependencies)
7714     ;; If this limitation resulted in an empty group, we might
7715     ;; pop the previous limit and use it instead.
7716     (when (and (not gnus-newsgroup-limit)
7717                show-if-empty)
7718       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7719     gnus-newsgroup-limit))
7720
7721 (defun gnus-summary-limit-children (thread)
7722   "Return 1 if this subthread is visible and 0 if it is not."
7723   ;; First we get the number of visible children to this thread.  This
7724   ;; is done by recursing down the thread using this function, so this
7725   ;; will really go down to a leaf article first, before slowly
7726   ;; working its way up towards the root.
7727   (when thread
7728     (let* ((max-lisp-eval-depth 5000)
7729            (children
7730            (if (cdr thread)
7731                (apply '+ (mapcar 'gnus-summary-limit-children
7732                                  (cdr thread)))
7733              0))
7734           (number (mail-header-number (car thread)))
7735           score)
7736       (if (and
7737            (not (memq number gnus-newsgroup-marked))
7738            (or
7739             ;; If this article is dormant and has absolutely no visible
7740             ;; children, then this article isn't visible.
7741             (and (memq number gnus-newsgroup-dormant)
7742                  (zerop children))
7743             ;; If this is "fetch-old-headered" and there is no
7744             ;; visible children, then we don't want this article.
7745             (and (or (eq gnus-fetch-old-headers 'some)
7746                      (numberp gnus-fetch-old-headers))
7747                  (gnus-summary-article-ancient-p number)
7748                  (zerop children))
7749             ;; If this is "fetch-old-headered" and `invisible', then
7750             ;; we don't want this article.
7751             (and (eq gnus-fetch-old-headers 'invisible)
7752                  (gnus-summary-article-ancient-p number))
7753             ;; If this is a sparsely inserted article with no children,
7754             ;; we don't want it.
7755             (and (eq gnus-build-sparse-threads 'some)
7756                  (gnus-summary-article-sparse-p number)
7757                  (zerop children))
7758             ;; If we use expunging, and this article is really
7759             ;; low-scored, then we don't want this article.
7760             (when (and gnus-summary-expunge-below
7761                        (< (setq score
7762                                 (or (cdr (assq number gnus-newsgroup-scored))
7763                                     gnus-summary-default-score))
7764                           gnus-summary-expunge-below))
7765               ;; We increase the expunge-tally here, but that has
7766               ;; nothing to do with the limits, really.
7767               (incf gnus-newsgroup-expunged-tally)
7768               ;; We also mark as read here, if that's wanted.
7769               (when (and gnus-summary-mark-below
7770                          (< score gnus-summary-mark-below))
7771                 (setq gnus-newsgroup-unreads
7772                       (delq number gnus-newsgroup-unreads))
7773                 (if gnus-newsgroup-auto-expire
7774                     (push number gnus-newsgroup-expirable)
7775                   (push (cons number gnus-low-score-mark)
7776                         gnus-newsgroup-reads)))
7777               t)
7778             ;; Do the `display' group parameter.
7779             (and gnus-newsgroup-display
7780                  (not (funcall gnus-newsgroup-display)))
7781             ;; Check NoCeM things.
7782             (if (and gnus-use-nocem
7783                      (gnus-nocem-unwanted-article-p
7784                       (mail-header-id (car thread))))
7785                 (progn
7786                   (setq gnus-newsgroup-unreads
7787                         (delq number gnus-newsgroup-unreads))
7788                   t))))
7789           ;; Nope, invisible article.
7790           0
7791         ;; Ok, this article is to be visible, so we add it to the limit
7792         ;; and return 1.
7793         (push number gnus-newsgroup-limit)
7794         1))))
7795
7796 (defun gnus-expunge-thread (thread)
7797   "Mark all articles in THREAD as read."
7798   (let* ((number (mail-header-number (car thread))))
7799     (incf gnus-newsgroup-expunged-tally)
7800     ;; We also mark as read here, if that's wanted.
7801     (setq gnus-newsgroup-unreads
7802           (delq number gnus-newsgroup-unreads))
7803     (if gnus-newsgroup-auto-expire
7804         (push number gnus-newsgroup-expirable)
7805       (push (cons number gnus-low-score-mark)
7806             gnus-newsgroup-reads)))
7807   ;; Go recursively through all subthreads.
7808   (mapcar 'gnus-expunge-thread (cdr thread)))
7809
7810 ;; Summary article oriented commands
7811
7812 (defun gnus-summary-refer-parent-article (n)
7813   "Refer parent article N times.
7814 If N is negative, go to ancestor -N instead.
7815 The difference between N and the number of articles fetched is returned."
7816   (interactive "p")
7817   (let ((skip 1)
7818         error header ref)
7819     (when (not (natnump n))
7820       (setq skip (abs n)
7821             n 1))
7822     (while (and (> n 0)
7823                 (not error))
7824       (setq header (gnus-summary-article-header))
7825       (if (and (eq (mail-header-number header)
7826                    (cdr gnus-article-current))
7827                (equal gnus-newsgroup-name
7828                       (car gnus-article-current)))
7829           ;; If we try to find the parent of the currently
7830           ;; displayed article, then we take a look at the actual
7831           ;; References header, since this is slightly more
7832           ;; reliable than the References field we got from the
7833           ;; server.
7834           (save-excursion
7835             (set-buffer gnus-original-article-buffer)
7836             (nnheader-narrow-to-headers)
7837             (unless (setq ref (message-fetch-field "references"))
7838               (setq ref (message-fetch-field "in-reply-to")))
7839             (widen))
7840         (setq ref
7841               ;; It's not the current article, so we take a bet on
7842               ;; the value we got from the server.
7843               (mail-header-references header)))
7844       (if (and ref
7845                (not (equal ref "")))
7846           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7847             (gnus-message 1 "Couldn't find parent"))
7848         (gnus-message 1 "No references in article %d"
7849                       (gnus-summary-article-number))
7850         (setq error t))
7851       (decf n))
7852     (gnus-summary-position-point)
7853     n))
7854
7855 (defun gnus-summary-refer-references ()
7856   "Fetch all articles mentioned in the References header.
7857 Return the number of articles fetched."
7858   (interactive)
7859   (let ((ref (mail-header-references (gnus-summary-article-header)))
7860         (current (gnus-summary-article-number))
7861         (n 0))
7862     (if (or (not ref)
7863             (equal ref ""))
7864         (error "No References in the current article")
7865       ;; For each Message-ID in the References header...
7866       (while (string-match "<[^>]*>" ref)
7867         (incf n)
7868         ;; ... fetch that article.
7869         (gnus-summary-refer-article
7870          (prog1 (match-string 0 ref)
7871            (setq ref (substring ref (match-end 0))))))
7872       (gnus-summary-goto-subject current)
7873       (gnus-summary-position-point)
7874       n)))
7875
7876 (defun gnus-summary-refer-thread (&optional limit)
7877   "Fetch all articles in the current thread.
7878 If LIMIT (the numerical prefix), fetch that many old headers instead
7879 of what's specified by the `gnus-refer-thread-limit' variable."
7880   (interactive "P")
7881   (let ((id (mail-header-id (gnus-summary-article-header)))
7882         (limit (if limit (prefix-numeric-value limit)
7883                  gnus-refer-thread-limit)))
7884     ;; We want to fetch LIMIT *old* headers, but we also have to
7885     ;; re-fetch all the headers in the current buffer, because many of
7886     ;; them may be undisplayed.  So we adjust LIMIT.
7887     (when (numberp limit)
7888       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7889     (unless (eq gnus-fetch-old-headers 'invisible)
7890       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7891       ;; Retrieve the headers and read them in.
7892       (if (eq (gnus-retrieve-headers
7893                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7894               'nov)
7895           (gnus-build-all-threads)
7896         (error "Can't fetch thread from backends that don't support NOV"))
7897       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7898     (gnus-summary-limit-include-thread id)))
7899
7900 (defun gnus-summary-refer-article (message-id)
7901   "Fetch an article specified by MESSAGE-ID."
7902   (interactive "sMessage-ID: ")
7903   (when (and (stringp message-id)
7904              (not (zerop (length message-id))))
7905     ;; Construct the correct Message-ID if necessary.
7906     ;; Suggested by tale@pawl.rpi.edu.
7907     (unless (string-match "^<" message-id)
7908       (setq message-id (concat "<" message-id)))
7909     (unless (string-match ">$" message-id)
7910       (setq message-id (concat message-id ">")))
7911     (let* ((header (gnus-id-to-header message-id))
7912            (sparse (and header
7913                         (gnus-summary-article-sparse-p
7914                          (mail-header-number header))
7915                         (memq (mail-header-number header)
7916                               gnus-newsgroup-limit)))
7917            number)
7918       (cond
7919        ;; If the article is present in the buffer we just go to it.
7920        ((and header
7921              (or (not (gnus-summary-article-sparse-p
7922                        (mail-header-number header)))
7923                  sparse))
7924         (prog1
7925             (gnus-summary-goto-article
7926              (mail-header-number header) nil t)
7927           (when sparse
7928             (gnus-summary-update-article (mail-header-number header)))))
7929        (t
7930         ;; We fetch the article.
7931         (catch 'found
7932           (dolist (gnus-override-method (gnus-refer-article-methods))
7933             (gnus-check-server gnus-override-method)
7934             ;; Fetch the header, and display the article.
7935             (when (setq number (gnus-summary-insert-subject message-id))
7936               (gnus-summary-select-article nil nil nil number)
7937               (throw 'found t)))
7938           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7939
7940 (defun gnus-refer-article-methods ()
7941   "Return a list of referable methods."
7942   (cond
7943    ;; No method, so we default to current and native.
7944    ((null gnus-refer-article-method)
7945     (list gnus-current-select-method gnus-select-method))
7946    ;; Current.
7947    ((eq 'current gnus-refer-article-method)
7948     (list gnus-current-select-method))
7949    ;; List of select methods.
7950    ((not (and (symbolp (car gnus-refer-article-method))
7951               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7952     (let (out)
7953       (dolist (method gnus-refer-article-method)
7954         (push (if (eq 'current method)
7955                   gnus-current-select-method
7956                 method)
7957               out))
7958       (nreverse out)))
7959    ;; One single select method.
7960    (t
7961     (list gnus-refer-article-method))))
7962
7963 (defun gnus-summary-edit-parameters ()
7964   "Edit the group parameters of the current group."
7965   (interactive)
7966   (gnus-group-edit-group gnus-newsgroup-name 'params))
7967
7968 (defun gnus-summary-customize-parameters ()
7969   "Customize the group parameters of the current group."
7970   (interactive)
7971   (gnus-group-customize gnus-newsgroup-name))
7972
7973 (defun gnus-summary-enter-digest-group (&optional force)
7974   "Enter an nndoc group based on the current article.
7975 If FORCE, force a digest interpretation.  If not, try
7976 to guess what the document format is."
7977   (interactive "P")
7978   (let ((conf gnus-current-window-configuration))
7979     (save-excursion
7980       (gnus-summary-select-article))
7981     (setq gnus-current-window-configuration conf)
7982     (let* ((name (format "%s-%d"
7983                          (gnus-group-prefixed-name
7984                           gnus-newsgroup-name (list 'nndoc ""))
7985                          (save-excursion
7986                            (set-buffer gnus-summary-buffer)
7987                            gnus-current-article)))
7988            (ogroup gnus-newsgroup-name)
7989            (params (append (gnus-info-params (gnus-get-info ogroup))
7990                            (list (cons 'to-group ogroup))
7991                            (list (cons 'save-article-group ogroup))))
7992            (case-fold-search t)
7993            (buf (current-buffer))
7994            dig to-address)
7995       (save-excursion
7996         (set-buffer gnus-original-article-buffer)
7997         ;; Have the digest group inherit the main mail address of
7998         ;; the parent article.
7999         (when (setq to-address (or (gnus-fetch-field "reply-to")
8000                                    (gnus-fetch-field "from")))
8001           (setq params (append
8002                         (list (cons 'to-address
8003                                     (funcall gnus-decode-encoded-word-function
8004                                              to-address))))))
8005         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8006         (insert-buffer-substring gnus-original-article-buffer)
8007         ;; Remove lines that may lead nndoc to misinterpret the
8008         ;; document type.
8009         (narrow-to-region
8010          (goto-char (point-min))
8011          (or (search-forward "\n\n" nil t) (point)))
8012         (goto-char (point-min))
8013         (delete-matching-lines "^Path:\\|^From ")
8014         (widen))
8015       (unwind-protect
8016           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8017                     (gnus-newsgroup-ephemeral-ignored-charsets
8018                      gnus-newsgroup-ignored-charsets))
8019                 (gnus-group-read-ephemeral-group
8020                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8021                               (nndoc-article-type
8022                                ,(if force 'mbox 'guess)))
8023                  t nil nil nil
8024                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8025                                                         "ADAPT")))))
8026               ;; Make all postings to this group go to the parent group.
8027               (nconc (gnus-info-params (gnus-get-info name))
8028                      params)
8029             ;; Couldn't select this doc group.
8030             (switch-to-buffer buf)
8031             (gnus-set-global-variables)
8032             (gnus-configure-windows 'summary)
8033             (gnus-message 3 "Article couldn't be entered?"))
8034         (kill-buffer dig)))))
8035
8036 (defun gnus-summary-read-document (n)
8037   "Open a new group based on the current article(s).
8038 This will allow you to read digests and other similar
8039 documents as newsgroups.
8040 Obeys the standard process/prefix convention."
8041   (interactive "P")
8042   (let* ((articles (gnus-summary-work-articles n))
8043          (ogroup gnus-newsgroup-name)
8044          (params (append (gnus-info-params (gnus-get-info ogroup))
8045                          (list (cons 'to-group ogroup))))
8046          article group egroup groups vgroup)
8047     (while (setq article (pop articles))
8048       (setq group (format "%s-%d" gnus-newsgroup-name article))
8049       (gnus-summary-remove-process-mark article)
8050       (when (gnus-summary-display-article article)
8051         (save-excursion
8052           (with-temp-buffer
8053             (insert-buffer-substring gnus-original-article-buffer)
8054             ;; Remove some headers that may lead nndoc to make
8055             ;; the wrong guess.
8056             (message-narrow-to-head)
8057             (goto-char (point-min))
8058             (delete-matching-lines "^\\(Path\\):\\|^From ")
8059             (widen)
8060             (if (setq egroup
8061                       (gnus-group-read-ephemeral-group
8062                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8063                                      (nndoc-article-type guess))
8064                        t nil t))
8065                 (progn
8066             ;; Make all postings to this group go to the parent group.
8067                   (nconc (gnus-info-params (gnus-get-info egroup))
8068                          params)
8069                   (push egroup groups))
8070               ;; Couldn't select this doc group.
8071               (gnus-error 3 "Article couldn't be entered"))))))
8072     ;; Now we have selected all the documents.
8073     (cond
8074      ((not groups)
8075       (error "None of the articles could be interpreted as documents"))
8076      ((gnus-group-read-ephemeral-group
8077        (setq vgroup (format
8078                      "nnvirtual:%s-%s" gnus-newsgroup-name
8079                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8080        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8081        t
8082        (cons (current-buffer) 'summary)))
8083      (t
8084       (error "Couldn't select virtual nndoc group")))))
8085
8086 (defun gnus-summary-isearch-article (&optional regexp-p)
8087   "Do incremental search forward on the current article.
8088 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8089   (interactive "P")
8090   (gnus-summary-select-article)
8091   (gnus-configure-windows 'article)
8092   (gnus-eval-in-buffer-window gnus-article-buffer
8093     (save-restriction
8094       (widen)
8095       (isearch-forward regexp-p))))
8096
8097 (defun gnus-summary-search-article-forward (regexp &optional backward)
8098   "Search for an article containing REGEXP forward.
8099 If BACKWARD, search backward instead."
8100   (interactive
8101    (list (read-string
8102           (format "Search article %s (regexp%s): "
8103                   (if current-prefix-arg "backward" "forward")
8104                   (if gnus-last-search-regexp
8105                       (concat ", default " gnus-last-search-regexp)
8106                     "")))
8107          current-prefix-arg))
8108   (if (string-equal regexp "")
8109       (setq regexp (or gnus-last-search-regexp ""))
8110     (setq gnus-last-search-regexp regexp)
8111     (setq gnus-article-before-search gnus-current-article))
8112   ;; Intentionally set gnus-last-article.
8113   (setq gnus-last-article gnus-article-before-search)
8114   (let ((gnus-last-article gnus-last-article))
8115     (if (gnus-summary-search-article regexp backward)
8116         (gnus-summary-show-thread)
8117       (error "Search failed: \"%s\"" regexp))))
8118
8119 (defun gnus-summary-search-article-backward (regexp)
8120   "Search for an article containing REGEXP backward."
8121   (interactive
8122    (list (read-string
8123           (format "Search article backward (regexp%s): "
8124                   (if gnus-last-search-regexp
8125                       (concat ", default " gnus-last-search-regexp)
8126                     "")))))
8127   (gnus-summary-search-article-forward regexp 'backward))
8128
8129 (defun gnus-summary-search-article (regexp &optional backward)
8130   "Search for an article containing REGEXP.
8131 Optional argument BACKWARD means do search for backward.
8132 `gnus-select-article-hook' is not called during the search."
8133   ;; We have to require this here to make sure that the following
8134   ;; dynamic binding isn't shadowed by autoloading.
8135   (require 'gnus-async)
8136   (require 'gnus-art)
8137   (let ((gnus-select-article-hook nil)  ;Disable hook.
8138         (gnus-article-prepare-hook nil)
8139         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8140         (gnus-use-article-prefetch nil)
8141         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8142         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8143         (sum (current-buffer))
8144         (gnus-display-mime-function nil)
8145         (found nil)
8146         point)
8147     (gnus-save-hidden-threads
8148       (gnus-summary-select-article)
8149       (set-buffer gnus-article-buffer)
8150       (goto-char (window-point (get-buffer-window (current-buffer))))
8151       (when backward
8152         (forward-line -1))
8153       (while (not found)
8154         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8155         (if (if backward
8156                 (re-search-backward regexp nil t)
8157               (re-search-forward regexp nil t))
8158             ;; We found the regexp.
8159             (progn
8160               (setq found 'found)
8161               (beginning-of-line)
8162               (set-window-start
8163                (get-buffer-window (current-buffer))
8164                (point))
8165               (forward-line 1)
8166               (set-window-point
8167                (get-buffer-window (current-buffer))
8168                (point))
8169               (set-buffer sum)
8170               (setq point (point)))
8171           ;; We didn't find it, so we go to the next article.
8172           (set-buffer sum)
8173           (setq found 'not)
8174           (while (eq found 'not)
8175             (if (not (if backward (gnus-summary-find-prev)
8176                        (gnus-summary-find-next)))
8177                 ;; No more articles.
8178                 (setq found t)
8179               ;; Select the next article and adjust point.
8180               (unless (gnus-summary-article-sparse-p
8181                        (gnus-summary-article-number))
8182                 (setq found nil)
8183                 (gnus-summary-select-article)
8184                 (set-buffer gnus-article-buffer)
8185                 (widen)
8186                 (goto-char (if backward (point-max) (point-min))))))))
8187       (gnus-message 7 ""))
8188     ;; Return whether we found the regexp.
8189     (when (eq found 'found)
8190       (goto-char point)
8191       (gnus-summary-show-thread)
8192       (gnus-summary-goto-subject gnus-current-article)
8193       (gnus-summary-position-point)
8194       t)))
8195
8196 (defun gnus-find-matching-articles (header regexp)
8197   "Return a list of all articles that match REGEXP on HEADER.
8198 This search includes all articles in the current group that Gnus has
8199 fetched headers for, whether they are displayed or not."
8200   (let ((articles nil)
8201         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8202         (case-fold-search t))
8203     (dolist (header gnus-newsgroup-headers)
8204       (when (string-match regexp (funcall func header))
8205         (push (mail-header-number header) articles)))
8206     (nreverse articles)))
8207
8208 (defun gnus-summary-find-matching (header regexp &optional backward unread
8209                                           not-case-fold not-matching)
8210   "Return a list of all articles that match REGEXP on HEADER.
8211 The search stars on the current article and goes forwards unless
8212 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8213 If UNREAD is non-nil, only unread articles will
8214 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8215 in the comparisons. If NOT-MATCHING, return a list of all articles that
8216 not match REGEXP on HEADER."
8217   (let ((case-fold-search (not not-case-fold))
8218         articles d func)
8219     (if (consp header)
8220         (if (eq (car header) 'extra)
8221             (setq func
8222                   `(lambda (h)
8223                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8224                          "")))
8225           (error "%s is an invalid header" header))
8226       (unless (fboundp (intern (concat "mail-header-" header)))
8227         (error "%s is not a valid header" header))
8228       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8229     (dolist (d (if (eq backward 'all)
8230                    gnus-newsgroup-data
8231                  (gnus-data-find-list
8232                   (gnus-summary-article-number)
8233                   (gnus-data-list backward))))
8234       (when (and (or (not unread)       ; We want all articles...
8235                      (gnus-data-unread-p d)) ; Or just unreads.
8236                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8237                  (if not-matching
8238                      (not (string-match
8239                            regexp
8240                            (funcall func (gnus-data-header d))))
8241                    (string-match regexp
8242                                  (funcall func (gnus-data-header d)))))
8243         (push (gnus-data-number d) articles))) ; Success!
8244     (nreverse articles)))
8245
8246 (defun gnus-summary-execute-command (header regexp command &optional backward)
8247   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8248 If HEADER is an empty string (or nil), the match is done on the entire
8249 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8250   (interactive
8251    (list (let ((completion-ignore-case t))
8252            (completing-read
8253             "Header name: "
8254             (mapcar (lambda (header) (list (format "%s" header)))
8255                     (append
8256                      '("Number" "Subject" "From" "Lines" "Date"
8257                        "Message-ID" "Xref" "References" "Body")
8258                      gnus-extra-headers))
8259             nil 'require-match))
8260          (read-string "Regexp: ")
8261          (read-key-sequence "Command: ")
8262          current-prefix-arg))
8263   (when (equal header "Body")
8264     (setq header ""))
8265   ;; Hidden thread subtrees must be searched as well.
8266   (gnus-summary-show-all-threads)
8267   ;; We don't want to change current point nor window configuration.
8268   (save-excursion
8269     (save-window-excursion
8270       (let (gnus-visual
8271             gnus-treat-strip-trailing-blank-lines
8272             gnus-treat-strip-leading-blank-lines
8273             gnus-treat-strip-multiple-blank-lines
8274             gnus-treat-hide-boring-headers
8275             gnus-treat-fold-newsgroups
8276             gnus-article-prepare-hook)
8277         (gnus-message 6 "Executing %s..." (key-description command))
8278         ;; We'd like to execute COMMAND interactively so as to give arguments.
8279         (gnus-execute header regexp
8280                       `(call-interactively ',(key-binding command))
8281                       backward)
8282         (gnus-message 6 "Executing %s...done" (key-description command))))))
8283
8284 (defun gnus-summary-beginning-of-article ()
8285   "Scroll the article back to the beginning."
8286   (interactive)
8287   (gnus-summary-select-article)
8288   (gnus-configure-windows 'article)
8289   (gnus-eval-in-buffer-window gnus-article-buffer
8290     (widen)
8291     (goto-char (point-min))
8292     (when gnus-page-broken
8293       (gnus-narrow-to-page))))
8294
8295 (defun gnus-summary-end-of-article ()
8296   "Scroll to the end of the article."
8297   (interactive)
8298   (gnus-summary-select-article)
8299   (gnus-configure-windows 'article)
8300   (gnus-eval-in-buffer-window gnus-article-buffer
8301     (widen)
8302     (goto-char (point-max))
8303     (recenter -3)
8304     (when gnus-page-broken
8305       (gnus-narrow-to-page))))
8306
8307 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8308   "Truncate to LEN and quote all \"(\"'s in STRING."
8309   (gnus-replace-in-string (if (and len (> (length string) len))
8310                               (substring string 0 len)
8311                             string)
8312                           "[()]" "\\\\\\&"))
8313
8314 (defun gnus-summary-print-article (&optional filename n)
8315   "Generate and print a PostScript image of the N next (mail) articles.
8316
8317 If N is negative, print the N previous articles.  If N is nil and articles
8318 have been marked with the process mark, print these instead.
8319
8320 If the optional first argument FILENAME is nil, send the image to the
8321 printer.  If FILENAME is a string, save the PostScript image in a file with
8322 that name.  If FILENAME is a number, prompt the user for the name of the file
8323 to save in."
8324   (interactive (list (ps-print-preprint current-prefix-arg)))
8325   (dolist (article (gnus-summary-work-articles n))
8326     (gnus-summary-select-article nil nil 'pseudo article)
8327     (gnus-eval-in-buffer-window gnus-article-buffer
8328       (gnus-print-buffer))
8329     (gnus-summary-remove-process-mark article))
8330   (ps-despool filename))
8331
8332 (defun gnus-print-buffer ()
8333   (let ((buffer (generate-new-buffer " *print*")))
8334     (unwind-protect
8335         (progn
8336           (copy-to-buffer buffer (point-min) (point-max))
8337           (set-buffer buffer)
8338           (gnus-article-delete-invisible-text)
8339           (gnus-remove-text-with-property 'gnus-decoration)
8340           (when (gnus-visual-p 'article-highlight 'highlight)
8341             ;; Copy-to-buffer doesn't copy overlay.  So redo
8342             ;; highlight.
8343             (let ((gnus-article-buffer buffer))
8344               (gnus-article-highlight-citation t)
8345               (gnus-article-highlight-signature)))
8346           (let ((ps-left-header
8347                  (list
8348                   (concat "("
8349                           (gnus-summary-print-truncate-and-quote
8350                            (mail-header-subject gnus-current-headers)
8351                            66) ")")
8352                   (concat "("
8353                           (gnus-summary-print-truncate-and-quote
8354                            (mail-header-from gnus-current-headers)
8355                            45) ")")))
8356                 (ps-right-header
8357                  (list
8358                   "/pagenumberstring load"
8359                   (concat "("
8360                           (mail-header-date gnus-current-headers) ")"))))
8361             (gnus-run-hooks 'gnus-ps-print-hook)
8362             (save-excursion
8363               (if window-system
8364                   (ps-spool-buffer-with-faces)
8365                 (ps-spool-buffer)))))
8366       (kill-buffer buffer))))
8367
8368 (defun gnus-summary-show-article (&optional arg)
8369   "Force redisplaying of the current article.
8370 If ARG (the prefix) is a number, show the article with the charset
8371 defined in `gnus-summary-show-article-charset-alist', or the charset
8372 input.
8373 If ARG (the prefix) is non-nil and not a number, show the raw article
8374 without any article massaging functions being run.  Normally, the key strokes
8375 are `C-u g'."
8376   (interactive "P")
8377   (cond
8378    ((numberp arg)
8379     (gnus-summary-show-article t)
8380     (let ((gnus-newsgroup-charset
8381            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8382                (mm-read-coding-system
8383                 "View as charset: " ;; actually it is coding system.
8384                 (save-excursion
8385                   (set-buffer gnus-article-buffer)
8386                   (mm-detect-coding-region (point) (point-max))))))
8387           (gnus-newsgroup-ignored-charsets 'gnus-all))
8388       (gnus-summary-select-article nil 'force)
8389       (let ((deps gnus-newsgroup-dependencies)
8390             head header lines)
8391         (save-excursion
8392           (set-buffer gnus-original-article-buffer)
8393           (save-restriction
8394             (message-narrow-to-head)
8395             (setq head (buffer-string))
8396             (goto-char (point-min))
8397             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8398               (goto-char (point-max))
8399               (widen)
8400               (setq lines (1- (count-lines (point) (point-max))))))
8401           (with-temp-buffer
8402             (insert (format "211 %d Article retrieved.\n"
8403                             (cdr gnus-article-current)))
8404             (insert head)
8405             (if lines (insert (format "Lines: %d\n" lines)))
8406             (insert ".\n")
8407             (let ((nntp-server-buffer (current-buffer)))
8408               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8409         (gnus-data-set-header
8410          (gnus-data-find (cdr gnus-article-current))
8411          header)
8412         (gnus-summary-update-article-line
8413          (cdr gnus-article-current) header)
8414         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8415           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8416    ((not arg)
8417     ;; Select the article the normal way.
8418     (gnus-summary-select-article nil 'force))
8419    (t
8420     ;; We have to require this here to make sure that the following
8421     ;; dynamic binding isn't shadowed by autoloading.
8422     (require 'gnus-async)
8423     (require 'gnus-art)
8424     ;; Bind the article treatment functions to nil.
8425     (let ((gnus-have-all-headers t)
8426           gnus-article-prepare-hook
8427           gnus-article-decode-hook
8428           gnus-display-mime-function
8429           gnus-break-pages)
8430       ;; Destroy any MIME parts.
8431       (when (gnus-buffer-live-p gnus-article-buffer)
8432         (save-excursion
8433           (set-buffer gnus-article-buffer)
8434           (mm-destroy-parts gnus-article-mime-handles)
8435           ;; Set it to nil for safety reason.
8436           (setq gnus-article-mime-handle-alist nil)
8437           (setq gnus-article-mime-handles nil)))
8438       (gnus-summary-select-article nil 'force))))
8439   (gnus-summary-goto-subject gnus-current-article)
8440   (gnus-summary-position-point))
8441
8442 (defun gnus-summary-show-raw-article ()
8443   "Show the raw article without any article massaging functions being run."
8444   (interactive)
8445   (gnus-summary-show-article t))
8446
8447 (defun gnus-summary-verbose-headers (&optional arg)
8448   "Toggle permanent full header display.
8449 If ARG is a positive number, turn header display on.
8450 If ARG is a negative number, turn header display off."
8451   (interactive "P")
8452   (setq gnus-show-all-headers
8453         (cond ((or (not (numberp arg))
8454                    (zerop arg))
8455                (not gnus-show-all-headers))
8456               ((natnump arg)
8457                t)))
8458   (gnus-summary-show-article))
8459
8460 (defun gnus-summary-toggle-header (&optional arg)
8461   "Show the headers if they are hidden, or hide them if they are shown.
8462 If ARG is a positive number, show the entire header.
8463 If ARG is a negative number, hide the unwanted header lines."
8464   (interactive "P")
8465   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8466                      (get-buffer-window gnus-article-buffer t))))
8467     (with-current-buffer gnus-article-buffer
8468       (widen)
8469       (article-narrow-to-head)
8470       (let* ((buffer-read-only nil)
8471              (inhibit-point-motion-hooks t)
8472              (hidden (if (numberp arg)
8473                          (>= arg 0)
8474                        (gnus-article-hidden-text-p 'headers)))
8475              s e)
8476         (delete-region (point-min) (point-max))
8477         (with-current-buffer gnus-original-article-buffer
8478           (goto-char (setq s (point-min)))
8479           (setq e (if (search-forward "\n\n" nil t)
8480                       (1- (point))
8481                     (point-max))))
8482         (insert-buffer-substring gnus-original-article-buffer s e)
8483         (article-decode-encoded-words)
8484         (if hidden
8485             (let ((gnus-treat-hide-headers nil)
8486                   (gnus-treat-hide-boring-headers nil))
8487               (gnus-delete-wash-type 'headers)
8488               (gnus-treat-article 'head))
8489           (gnus-treat-article 'head))
8490         (widen)
8491         (if window
8492             (set-window-start window (goto-char (point-min))))
8493         (setq gnus-page-broken
8494               (when gnus-break-pages
8495                 (gnus-narrow-to-page)
8496                 t))
8497         (gnus-set-mode-line 'article)))))
8498
8499 (defun gnus-summary-show-all-headers ()
8500   "Make all header lines visible."
8501   (interactive)
8502   (gnus-summary-toggle-header 1))
8503
8504 (defun gnus-summary-caesar-message (&optional arg)
8505   "Caesar rotate the current article by 13.
8506 The numerical prefix specifies how many places to rotate each letter
8507 forward."
8508   (interactive "P")
8509   (gnus-summary-select-article)
8510   (let ((mail-header-separator ""))
8511     (gnus-eval-in-buffer-window gnus-article-buffer
8512       (save-restriction
8513         (widen)
8514         (let ((start (window-start))
8515               buffer-read-only)
8516           (message-caesar-buffer-body arg)
8517           (set-window-start (get-buffer-window (current-buffer)) start))))))
8518
8519 (autoload 'unmorse-region "morse"
8520   "Convert morse coded text in region to ordinary ASCII text."
8521   t)
8522
8523 (defun gnus-summary-morse-message (&optional arg)
8524   "Morse decode the current article."
8525   (interactive "P")
8526   (gnus-summary-select-article)
8527   (let ((mail-header-separator ""))
8528     (gnus-eval-in-buffer-window gnus-article-buffer
8529       (save-excursion
8530         (save-restriction
8531           (widen)
8532           (let ((pos (window-start))
8533                 buffer-read-only)
8534             (goto-char (point-min))
8535             (when (message-goto-body)
8536               (gnus-narrow-to-body))
8537             (goto-char (point-min))
8538             (while (re-search-forward "·" (point-max) t)
8539               (replace-match "."))
8540             (unmorse-region (point-min) (point-max))
8541             (widen)
8542             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8543
8544 (defun gnus-summary-stop-page-breaking ()
8545   "Stop page breaking in the current article."
8546   (interactive)
8547   (gnus-summary-select-article)
8548   (gnus-eval-in-buffer-window gnus-article-buffer
8549     (widen)
8550     (when (gnus-visual-p 'page-marker)
8551       (let ((buffer-read-only nil))
8552         (gnus-remove-text-with-property 'gnus-prev)
8553         (gnus-remove-text-with-property 'gnus-next))
8554       (setq gnus-page-broken nil))))
8555
8556 (defun gnus-summary-move-article (&optional n to-newsgroup
8557                                             select-method action)
8558   "Move the current article to a different newsgroup.
8559 If N is a positive number, move the N next articles.
8560 If N is a negative number, move the N previous articles.
8561 If N is nil and any articles have been marked with the process mark,
8562 move those articles instead.
8563 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8564 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8565 re-spool using this method.
8566
8567 When called interactively with TO-NEWSGROUP being nil, the value of
8568 the variable `gnus-move-split-methods' is used for finding a default
8569 for the target newsgroup.
8570
8571 For this function to work, both the current newsgroup and the
8572 newsgroup that you want to move to have to support the `request-move'
8573 and `request-accept' functions.
8574
8575 ACTION can be either `move' (the default), `crosspost' or `copy'."
8576   (interactive "P")
8577   (unless action
8578     (setq action 'move))
8579   ;; Check whether the source group supports the required functions.
8580   (cond ((and (eq action 'move)
8581               (not (gnus-check-backend-function
8582                     'request-move-article gnus-newsgroup-name)))
8583          (error "The current group does not support article moving"))
8584         ((and (eq action 'crosspost)
8585               (not (gnus-check-backend-function
8586                     'request-replace-article gnus-newsgroup-name)))
8587          (error "The current group does not support article editing")))
8588   (let ((articles (gnus-summary-work-articles n))
8589         (prefix (if (gnus-check-backend-function
8590                      'request-move-article gnus-newsgroup-name)
8591                     (gnus-group-real-prefix gnus-newsgroup-name)
8592                   ""))
8593         (names '((move "Move" "Moving")
8594                  (copy "Copy" "Copying")
8595                  (crosspost "Crosspost" "Crossposting")))
8596         (copy-buf (save-excursion
8597                     (nnheader-set-temp-buffer " *copy article*")))
8598         art-group to-method new-xref article to-groups)
8599     (unless (assq action names)
8600       (error "Unknown action %s" action))
8601     ;; Read the newsgroup name.
8602     (when (and (not to-newsgroup)
8603                (not select-method))
8604       (if (and gnus-move-split-methods
8605                (not
8606                 (and (memq gnus-current-article articles)
8607                      (gnus-buffer-live-p gnus-original-article-buffer))))
8608           ;; When `gnus-move-split-methods' is non-nil, we have to
8609           ;; select an article to give `gnus-read-move-group-name' an
8610           ;; opportunity to suggest an appropriate default.  However,
8611           ;; we needn't render or mark the article.
8612           (let ((gnus-display-mime-function nil)
8613                 (gnus-article-prepare-hook nil)
8614                 (gnus-mark-article-hook nil))
8615             (gnus-summary-select-article nil nil nil (car articles))))
8616       (setq to-newsgroup
8617             (gnus-read-move-group-name
8618              (cadr (assq action names))
8619              (symbol-value (intern (format "gnus-current-%s-group" action)))
8620              articles prefix))
8621       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8622     (setq to-method (or select-method
8623                         (gnus-server-to-method
8624                          (gnus-group-method to-newsgroup))))
8625     ;; Check the method we are to move this article to...
8626     (unless (gnus-check-backend-function
8627              'request-accept-article (car to-method))
8628       (error "%s does not support article copying" (car to-method)))
8629     (unless (gnus-check-server to-method)
8630       (error "Can't open server %s" (car to-method)))
8631     (gnus-message 6 "%s to %s: %s..."
8632                   (caddr (assq action names))
8633                   (or (car select-method) to-newsgroup) articles)
8634     (while articles
8635       (setq article (pop articles))
8636       (setq
8637        art-group
8638        (cond
8639         ;; Move the article.
8640         ((eq action 'move)
8641          ;; Remove this article from future suppression.
8642          (gnus-dup-unsuppress-article article)
8643          (gnus-request-move-article
8644           article                       ; Article to move
8645           gnus-newsgroup-name           ; From newsgroup
8646           (nth 1 (gnus-find-method-for-group
8647                   gnus-newsgroup-name)) ; Server
8648           (list 'gnus-request-accept-article
8649                 to-newsgroup (list 'quote select-method)
8650                 (not articles) t)       ; Accept form
8651           (not articles)))              ; Only save nov last time
8652         ;; Copy the article.
8653         ((eq action 'copy)
8654          (save-excursion
8655            (set-buffer copy-buf)
8656            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8657              (gnus-request-accept-article
8658               to-newsgroup select-method (not articles) t))))
8659         ;; Crosspost the article.
8660         ((eq action 'crosspost)
8661          (let ((xref (message-tokenize-header
8662                       (mail-header-xref (gnus-summary-article-header article))
8663                       " ")))
8664            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8665                                   ":" (number-to-string article)))
8666            (unless xref
8667              (setq xref (list (system-name))))
8668            (setq new-xref
8669                  (concat
8670                   (mapconcat 'identity
8671                              (delete "Xref:" (delete new-xref xref))
8672                              " ")
8673                   " " new-xref))
8674            (save-excursion
8675              (set-buffer copy-buf)
8676              ;; First put the article in the destination group.
8677              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8678              (when (consp (setq art-group
8679                                 (gnus-request-accept-article
8680                                  to-newsgroup select-method (not articles))))
8681                (setq new-xref (concat new-xref " " (car art-group)
8682                                       ":"
8683                                       (number-to-string (cdr art-group))))
8684                ;; Now we have the new Xrefs header, so we insert
8685                ;; it and replace the new article.
8686                (nnheader-replace-header "Xref" new-xref)
8687                (gnus-request-replace-article
8688                 (cdr art-group) to-newsgroup (current-buffer))
8689                art-group))))))
8690       (cond
8691        ((not art-group)
8692         (gnus-message 1 "Couldn't %s article %s: %s"
8693                       (cadr (assq action names)) article
8694                       (nnheader-get-report (car to-method))))
8695        ((eq art-group 'junk)
8696         (when (eq action 'move)
8697           (gnus-summary-mark-article article gnus-canceled-mark)
8698           (gnus-message 4 "Deleted article %s" article)))
8699        (t
8700         (let* ((pto-group (gnus-group-prefixed-name
8701                            (car art-group) to-method))
8702                (entry
8703                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8704                (info (nth 2 entry))
8705                (to-group (gnus-info-group info))
8706                to-marks)
8707           ;; Update the group that has been moved to.
8708           (when (and info
8709                      (memq action '(move copy)))
8710             (unless (member to-group to-groups)
8711               (push to-group to-groups))
8712
8713             (unless (memq article gnus-newsgroup-unreads)
8714               (push 'read to-marks)
8715               (gnus-info-set-read
8716                info (gnus-add-to-range (gnus-info-read info)
8717                                        (list (cdr art-group)))))
8718
8719             ;; See whether the article is to be put in the cache.
8720             (let ((marks gnus-article-mark-lists)
8721                   (to-article (cdr art-group)))
8722
8723               ;; Enter the article into the cache in the new group,
8724               ;; if that is required.
8725               (when gnus-use-cache
8726                 (gnus-cache-possibly-enter-article
8727                  to-group to-article
8728                  (memq article gnus-newsgroup-marked)
8729                  (memq article gnus-newsgroup-dormant)
8730                  (memq article gnus-newsgroup-unreads)))
8731
8732               (when gnus-preserve-marks
8733                 ;; Copy any marks over to the new group.
8734                 (when (and (equal to-group gnus-newsgroup-name)
8735                            (not (memq article gnus-newsgroup-unreads)))
8736                   ;; Mark this article as read in this group.
8737                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8738                   (setcdr (gnus-active to-group) to-article)
8739                   (setcdr gnus-newsgroup-active to-article))
8740
8741                 (while marks
8742                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8743                     (when (memq article (symbol-value
8744                                          (intern (format "gnus-newsgroup-%s"
8745                                                          (caar marks)))))
8746                       (push (cdar marks) to-marks)
8747                       ;; If the other group is the same as this group,
8748                       ;; then we have to add the mark to the list.
8749                       (when (equal to-group gnus-newsgroup-name)
8750                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8751                              (cons to-article
8752                                    (symbol-value
8753                                     (intern (format "gnus-newsgroup-%s"
8754                                                     (caar marks)))))))
8755                       ;; Copy the marks to other group.
8756                       (gnus-add-marked-articles
8757                        to-group (cdar marks) (list to-article) info)))
8758                   (setq marks (cdr marks)))
8759
8760                 (gnus-request-set-mark
8761                  to-group (list (list (list to-article) 'add to-marks))))
8762
8763               (gnus-dribble-enter
8764                (concat "(gnus-group-set-info '"
8765                        (gnus-prin1-to-string (gnus-get-info to-group))
8766                        ")"))))
8767
8768           ;; Update the Xref header in this article to point to
8769           ;; the new crossposted article we have just created.
8770           (when (eq action 'crosspost)
8771             (save-excursion
8772               (set-buffer copy-buf)
8773               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8774               (nnheader-replace-header "Xref" new-xref)
8775               (gnus-request-replace-article
8776                article gnus-newsgroup-name (current-buffer)))))
8777
8778         ;;;!!!Why is this necessary?
8779         (set-buffer gnus-summary-buffer)
8780
8781         (gnus-summary-goto-subject article)
8782         (when (eq action 'move)
8783           (gnus-summary-mark-article article gnus-canceled-mark))))
8784       (gnus-summary-remove-process-mark article))
8785     ;; Re-activate all groups that have been moved to.
8786     (save-excursion
8787       (set-buffer gnus-group-buffer)
8788       (let ((gnus-group-marked to-groups))
8789         (gnus-group-get-new-news-this-group nil t)))
8790
8791     (gnus-kill-buffer copy-buf)
8792     (gnus-summary-position-point)
8793     (gnus-set-mode-line 'summary)))
8794
8795 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8796   "Move the current article to a different newsgroup.
8797 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8798 When called interactively, if TO-NEWSGROUP is nil, use the value of
8799 the variable `gnus-move-split-methods' for finding a default target
8800 newsgroup.
8801 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8802 re-spool using this method."
8803   (interactive "P")
8804   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8805
8806 (defun gnus-summary-crosspost-article (&optional n)
8807   "Crosspost the current article to some other group."
8808   (interactive "P")
8809   (gnus-summary-move-article n nil nil 'crosspost))
8810
8811 (defcustom gnus-summary-respool-default-method nil
8812   "Default method type for respooling an article.
8813 If nil, use to the current newsgroup method."
8814   :type 'symbol
8815   :group 'gnus-summary-mail)
8816
8817 (defcustom gnus-summary-display-while-building nil
8818   "If not-nil, show and update the summary buffer as it's being built.
8819 If the value is t, update the buffer after every line is inserted.  If
8820 the value is an integer (N), update the display every N lines."
8821   :group 'gnus-thread
8822   :type '(choice (const :tag "off" nil)
8823                  number
8824                  (const :tag "frequently" t)))
8825
8826 (defun gnus-summary-respool-article (&optional n method)
8827   "Respool the current article.
8828 The article will be squeezed through the mail spooling process again,
8829 which means that it will be put in some mail newsgroup or other
8830 depending on `nnmail-split-methods'.
8831 If N is a positive number, respool the N next articles.
8832 If N is a negative number, respool the N previous articles.
8833 If N is nil and any articles have been marked with the process mark,
8834 respool those articles instead.
8835
8836 Respooling can be done both from mail groups and \"real\" newsgroups.
8837 In the former case, the articles in question will be moved from the
8838 current group into whatever groups they are destined to.  In the
8839 latter case, they will be copied into the relevant groups."
8840   (interactive
8841    (list current-prefix-arg
8842          (let* ((methods (gnus-methods-using 'respool))
8843                 (methname
8844                  (symbol-name (or gnus-summary-respool-default-method
8845                                   (car (gnus-find-method-for-group
8846                                         gnus-newsgroup-name)))))
8847                 (method
8848                  (gnus-completing-read-with-default
8849                   methname "What backend do you want to use when respooling?"
8850                   methods nil t nil 'gnus-mail-method-history))
8851                 ms)
8852            (cond
8853             ((zerop (length (setq ms (gnus-servers-using-backend
8854                                       (intern method)))))
8855              (list (intern method) ""))
8856             ((= 1 (length ms))
8857              (car ms))
8858             (t
8859              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8860                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8861                            ms-alist))))))))
8862   (unless method
8863     (error "No method given for respooling"))
8864   (if (assoc (symbol-name
8865               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8866              (gnus-methods-using 'respool))
8867       (gnus-summary-move-article n nil method)
8868     (gnus-summary-copy-article n nil method)))
8869
8870 (defun gnus-summary-import-article (file &optional edit)
8871   "Import an arbitrary file into a mail newsgroup."
8872   (interactive "fImport file: \nP")
8873   (let ((group gnus-newsgroup-name)
8874         (now (current-time))
8875         atts lines group-art)
8876     (unless (gnus-check-backend-function 'request-accept-article group)
8877       (error "%s does not support article importing" group))
8878     (or (file-readable-p file)
8879         (not (file-regular-p file))
8880         (error "Can't read %s" file))
8881     (save-excursion
8882       (set-buffer (gnus-get-buffer-create " *import file*"))
8883       (erase-buffer)
8884       (nnheader-insert-file-contents file)
8885       (goto-char (point-min))
8886       (if (nnheader-article-p)
8887           (save-restriction
8888             (goto-char (point-min))
8889             (search-forward "\n\n" nil t)
8890             (narrow-to-region (point-min) (1- (point)))
8891             (goto-char (point-min))
8892             (unless (re-search-forward "^date:" nil t)
8893               (goto-char (point-max))
8894               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8895        ;; This doesn't look like an article, so we fudge some headers.
8896         (setq atts (file-attributes file)
8897               lines (count-lines (point-min) (point-max)))
8898         (insert "From: " (read-string "From: ") "\n"
8899                 "Subject: " (read-string "Subject: ") "\n"
8900                 "Date: " (message-make-date (nth 5 atts)) "\n"
8901                 "Message-ID: " (message-make-message-id) "\n"
8902                 "Lines: " (int-to-string lines) "\n"
8903                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8904       (setq group-art (gnus-request-accept-article group nil t))
8905       (kill-buffer (current-buffer)))
8906     (setq gnus-newsgroup-active (gnus-activate-group group))
8907     (forward-line 1)
8908     (gnus-summary-goto-article (cdr group-art) nil t)
8909     (when edit
8910       (gnus-summary-edit-article))))
8911
8912 (defun gnus-summary-create-article ()
8913   "Create an article in a mail newsgroup."
8914   (interactive)
8915   (let ((group gnus-newsgroup-name)
8916         (now (current-time))
8917         group-art)
8918     (unless (gnus-check-backend-function 'request-accept-article group)
8919       (error "%s does not support article importing" group))
8920     (save-excursion
8921       (set-buffer (gnus-get-buffer-create " *import file*"))
8922       (erase-buffer)
8923       (goto-char (point-min))
8924       ;; This doesn't look like an article, so we fudge some headers.
8925       (insert "From: " (read-string "From: ") "\n"
8926               "Subject: " (read-string "Subject: ") "\n"
8927               "Date: " (message-make-date now) "\n"
8928               "Message-ID: " (message-make-message-id) "\n")
8929       (setq group-art (gnus-request-accept-article group nil t))
8930       (kill-buffer (current-buffer)))
8931     (setq gnus-newsgroup-active (gnus-activate-group group))
8932     (forward-line 1)
8933     (gnus-summary-goto-article (cdr group-art) nil t)
8934     (gnus-summary-edit-article)))
8935
8936 (defun gnus-summary-article-posted-p ()
8937   "Say whether the current (mail) article is available from news as well.
8938 This will be the case if the article has both been mailed and posted."
8939   (interactive)
8940   (let ((id (mail-header-references (gnus-summary-article-header)))
8941         (gnus-override-method (car (gnus-refer-article-methods))))
8942     (if (gnus-request-head id "")
8943         (gnus-message 2 "The current message was found on %s"
8944                       gnus-override-method)
8945       (gnus-message 2 "The current message couldn't be found on %s"
8946                     gnus-override-method)
8947       nil)))
8948
8949 (defun gnus-summary-expire-articles (&optional now)
8950   "Expire all articles that are marked as expirable in the current group."
8951   (interactive)
8952   (when (gnus-check-backend-function
8953          'request-expire-articles gnus-newsgroup-name)
8954     ;; This backend supports expiry.
8955     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8956            (expirable (if total
8957                           (progn
8958                             ;; We need to update the info for
8959                             ;; this group for `gnus-list-of-read-articles'
8960                             ;; to give us the right answer.
8961                             (gnus-run-hooks 'gnus-exit-group-hook)
8962                             (gnus-summary-update-info)
8963                             (gnus-list-of-read-articles gnus-newsgroup-name))
8964                         (setq gnus-newsgroup-expirable
8965                               (sort gnus-newsgroup-expirable '<))))
8966            (expiry-wait (if now 'immediate
8967                           (gnus-group-find-parameter
8968                            gnus-newsgroup-name 'expiry-wait)))
8969            (nnmail-expiry-target
8970             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8971                 nnmail-expiry-target))
8972            es)
8973       (when expirable
8974         ;; There are expirable articles in this group, so we run them
8975         ;; through the expiry process.
8976         (gnus-message 6 "Expiring articles...")
8977         (unless (gnus-check-group gnus-newsgroup-name)
8978           (error "Can't open server for %s" gnus-newsgroup-name))
8979         ;; The list of articles that weren't expired is returned.
8980         (save-excursion
8981           (if expiry-wait
8982               (let ((nnmail-expiry-wait-function nil)
8983                     (nnmail-expiry-wait expiry-wait))
8984                 (setq es (gnus-request-expire-articles
8985                           expirable gnus-newsgroup-name)))
8986             (setq es (gnus-request-expire-articles
8987                       expirable gnus-newsgroup-name)))
8988           (unless total
8989             (setq gnus-newsgroup-expirable es))
8990           ;; We go through the old list of expirable, and mark all
8991           ;; really expired articles as nonexistent.
8992           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8993             (let ((gnus-use-cache nil))
8994               (dolist (article expirable)
8995                 (when (and (not (memq article es))
8996                            (gnus-data-find article))
8997                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8998         (gnus-message 6 "Expiring articles...done")))))
8999
9000 (defun gnus-summary-expire-articles-now ()
9001   "Expunge all expirable articles in the current group.
9002 This means that *all* articles that are marked as expirable will be
9003 deleted forever, right now."
9004   (interactive)
9005   (or gnus-expert-user
9006       (gnus-yes-or-no-p
9007        "Are you really, really, really sure you want to delete all these messages? ")
9008       (error "Phew!"))
9009   (gnus-summary-expire-articles t))
9010
9011 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9012 (defun gnus-summary-delete-article (&optional n)
9013   "Delete the N next (mail) articles.
9014 This command actually deletes articles.  This is not a marking
9015 command.  The article will disappear forever from your life, never to
9016 return.
9017 If N is negative, delete backwards.
9018 If N is nil and articles have been marked with the process mark,
9019 delete these instead."
9020   (interactive "P")
9021   (unless (gnus-check-backend-function 'request-expire-articles
9022                                        gnus-newsgroup-name)
9023     (error "The current newsgroup does not support article deletion"))
9024   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9025     (error "Couldn't open server"))
9026   ;; Compute the list of articles to delete.
9027   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9028         (nnmail-expiry-target 'delete)
9029         not-deleted)
9030     (if (and gnus-novice-user
9031              (not (gnus-yes-or-no-p
9032                    (format "Do you really want to delete %s forever? "
9033                            (if (> (length articles) 1)
9034                                (format "these %s articles" (length articles))
9035                              "this article")))))
9036         ()
9037       ;; Delete the articles.
9038       (setq not-deleted (gnus-request-expire-articles
9039                          articles gnus-newsgroup-name 'force))
9040       (while articles
9041         (gnus-summary-remove-process-mark (car articles))
9042         ;; The backend might not have been able to delete the article
9043         ;; after all.
9044         (unless (memq (car articles) not-deleted)
9045           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9046         (setq articles (cdr articles)))
9047       (when not-deleted
9048         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9049     (gnus-summary-position-point)
9050     (gnus-set-mode-line 'summary)
9051     not-deleted))
9052
9053 (defun gnus-summary-edit-article (&optional arg)
9054   "Edit the current article.
9055 This will have permanent effect only in mail groups.
9056 If ARG is nil, edit the decoded articles.
9057 If ARG is 1, edit the raw articles.
9058 If ARG is 2, edit the raw articles even in read-only groups.
9059 If ARG is 3, edit the articles with the current handles.
9060 Otherwise, allow editing of articles even in read-only
9061 groups."
9062   (interactive "P")
9063   (let (force raw current-handles)
9064     (cond
9065      ((null arg))
9066      ((eq arg 1)
9067       (setq raw t))
9068      ((eq arg 2)
9069       (setq raw t
9070             force t))
9071      ((eq arg 3)
9072       (setq current-handles
9073             (and (gnus-buffer-live-p gnus-article-buffer)
9074                  (with-current-buffer gnus-article-buffer
9075                    (prog1
9076                        gnus-article-mime-handles
9077                      (setq gnus-article-mime-handles nil))))))
9078      (t
9079       (setq force t)))
9080     (when (and raw (not force)
9081                (member gnus-newsgroup-name '("nndraft:delayed"
9082                                              "nndraft:drafts"
9083                                              "nndraft:queue")))
9084       (error "Can't edit the raw article in group %s"
9085              gnus-newsgroup-name))
9086     (save-excursion
9087       (set-buffer gnus-summary-buffer)
9088       (let ((mail-parse-charset gnus-newsgroup-charset)
9089             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9090         (gnus-set-global-variables)
9091         (when (and (not force)
9092                    (gnus-group-read-only-p))
9093           (error "The current newsgroup does not support article editing"))
9094         (gnus-summary-show-article t)
9095         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9096           (with-current-buffer gnus-article-buffer
9097             (mm-enable-multibyte)))
9098         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9099             (setq raw t))
9100         (gnus-article-edit-article
9101          (if raw 'ignore
9102            `(lambda ()
9103               (let ((mbl mml-buffer-list))
9104                 (setq mml-buffer-list nil)
9105                 (mime-to-mml ,'current-handles)
9106                 (let ((mbl1 mml-buffer-list))
9107                   (setq mml-buffer-list mbl)
9108                   (set (make-local-variable 'mml-buffer-list) mbl1))
9109                 (make-local-hook 'kill-buffer-hook)
9110                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9111          `(lambda (no-highlight)
9112             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9113                   (message-options message-options)
9114                   (message-options-set-recipient)
9115                   (mail-parse-ignored-charsets
9116                    ',gnus-newsgroup-ignored-charsets))
9117               ,(if (not raw) '(progn
9118                                 (mml-to-mime)
9119                                 (mml-destroy-buffers)
9120                                 (remove-hook 'kill-buffer-hook
9121                                              'mml-destroy-buffers t)
9122                                 (kill-local-variable 'mml-buffer-list)))
9123               (gnus-summary-edit-article-done
9124                ,(or (mail-header-references gnus-current-headers) "")
9125                ,(gnus-group-read-only-p)
9126                ,gnus-summary-buffer no-highlight))))))))
9127
9128 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9129
9130 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9131                                                  no-highlight)
9132   "Make edits to the current article permanent."
9133   (interactive)
9134   (save-excursion
9135    ;; The buffer restriction contains the entire article if it exists.
9136     (when (article-goto-body)
9137       (let ((lines (count-lines (point) (point-max)))
9138             (length (- (point-max) (point)))
9139             (case-fold-search t)
9140             (body (copy-marker (point))))
9141         (goto-char (point-min))
9142         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9143           (delete-region (match-beginning 1) (match-end 1))
9144           (insert (number-to-string length)))
9145         (goto-char (point-min))
9146         (when (re-search-forward
9147                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9148           (delete-region (match-beginning 1) (match-end 1))
9149           (insert (number-to-string length)))
9150         (goto-char (point-min))
9151         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9152           (delete-region (match-beginning 1) (match-end 1))
9153           (insert (number-to-string lines))))))
9154   ;; Replace the article.
9155   (let ((buf (current-buffer)))
9156     (with-temp-buffer
9157       (insert-buffer-substring buf)
9158
9159       (if (and (not read-only)
9160                (not (gnus-request-replace-article
9161                      (cdr gnus-article-current) (car gnus-article-current)
9162                      (current-buffer) t)))
9163           (error "Couldn't replace article")
9164         ;; Update the summary buffer.
9165         (if (and references
9166                  (equal (message-tokenize-header references " ")
9167                         (message-tokenize-header
9168                          (or (message-fetch-field "references") "") " ")))
9169             ;; We only have to update this line.
9170             (save-excursion
9171               (save-restriction
9172                 (message-narrow-to-head)
9173                 (let ((head (buffer-string))
9174                       header)
9175                   (with-temp-buffer
9176                     (insert (format "211 %d Article retrieved.\n"
9177                                     (cdr gnus-article-current)))
9178                     (insert head)
9179                     (insert ".\n")
9180                     (let ((nntp-server-buffer (current-buffer)))
9181                       (setq header (car (gnus-get-newsgroup-headers
9182                                          nil t))))
9183                     (save-excursion
9184                       (set-buffer gnus-summary-buffer)
9185                       (gnus-data-set-header
9186                        (gnus-data-find (cdr gnus-article-current))
9187                        header)
9188                       (gnus-summary-update-article-line
9189                        (cdr gnus-article-current) header)
9190                       (if (gnus-summary-goto-subject
9191                            (cdr gnus-article-current) nil t)
9192                           (gnus-summary-update-secondary-mark
9193                            (cdr gnus-article-current))))))))
9194           ;; Update threads.
9195           (set-buffer (or buffer gnus-summary-buffer))
9196           (gnus-summary-update-article (cdr gnus-article-current))
9197           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9198               (gnus-summary-update-secondary-mark
9199                (cdr gnus-article-current))))
9200         ;; Prettify the article buffer again.
9201         (unless no-highlight
9202           (save-excursion
9203             (set-buffer gnus-article-buffer)
9204             ;;;!!! Fix this -- article should be rehighlighted.
9205             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9206             (set-buffer gnus-original-article-buffer)
9207             (gnus-request-article
9208              (cdr gnus-article-current)
9209              (car gnus-article-current) (current-buffer))))
9210         ;; Prettify the summary buffer line.
9211         (when (gnus-visual-p 'summary-highlight 'highlight)
9212           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9213
9214 (defun gnus-summary-edit-wash (key)
9215   "Perform editing command KEY in the article buffer."
9216   (interactive
9217    (list
9218     (progn
9219       (message "%s" (concat (this-command-keys) "- "))
9220       (read-char))))
9221   (message "")
9222   (gnus-summary-edit-article)
9223   (execute-kbd-macro (concat (this-command-keys) key))
9224   (gnus-article-edit-done))
9225
9226 ;;; Respooling
9227
9228 (defun gnus-summary-respool-query (&optional silent trace)
9229   "Query where the respool algorithm would put this article."
9230   (interactive)
9231   (let (gnus-mark-article-hook)
9232     (gnus-summary-select-article)
9233     (save-excursion
9234       (set-buffer gnus-original-article-buffer)
9235       (save-restriction
9236         (message-narrow-to-head)
9237         (let ((groups (nnmail-article-group 'identity trace)))
9238           (unless silent
9239             (if groups
9240                 (message "This message would go to %s"
9241                          (mapconcat 'car groups ", "))
9242               (message "This message would go to no groups"))
9243             groups))))))
9244
9245 (defun gnus-summary-respool-trace ()
9246   "Trace where the respool algorithm would put this article.
9247 Display a buffer showing all fancy splitting patterns which matched."
9248   (interactive)
9249   (gnus-summary-respool-query nil t))
9250
9251 ;; Summary marking commands.
9252
9253 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9254   "Mark articles which has the same subject as read, and then select the next.
9255 If UNMARK is positive, remove any kind of mark.
9256 If UNMARK is negative, tick articles."
9257   (interactive "P")
9258   (when unmark
9259     (setq unmark (prefix-numeric-value unmark)))
9260   (let ((count
9261          (gnus-summary-mark-same-subject
9262           (gnus-summary-article-subject) unmark)))
9263     ;; Select next unread article.  If auto-select-same mode, should
9264     ;; select the first unread article.
9265     (gnus-summary-next-article t (and gnus-auto-select-same
9266                                       (gnus-summary-article-subject)))
9267     (gnus-message 7 "%d article%s marked as %s"
9268                   count (if (= count 1) " is" "s are")
9269                   (if unmark "unread" "read"))))
9270
9271 (defun gnus-summary-kill-same-subject (&optional unmark)
9272   "Mark articles which has the same subject as read.
9273 If UNMARK is positive, remove any kind of mark.
9274 If UNMARK is negative, tick articles."
9275   (interactive "P")
9276   (when unmark
9277     (setq unmark (prefix-numeric-value unmark)))
9278   (let ((count
9279          (gnus-summary-mark-same-subject
9280           (gnus-summary-article-subject) unmark)))
9281     ;; If marked as read, go to next unread subject.
9282     (when (null unmark)
9283       ;; Go to next unread subject.
9284       (gnus-summary-next-subject 1 t))
9285     (gnus-message 7 "%d articles are marked as %s"
9286                   count (if unmark "unread" "read"))))
9287
9288 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9289   "Mark articles with same SUBJECT as read, and return marked number.
9290 If optional argument UNMARK is positive, remove any kinds of marks.
9291 If optional argument UNMARK is negative, mark articles as unread instead."
9292   (let ((count 1))
9293     (save-excursion
9294       (cond
9295        ((null unmark)                   ; Mark as read.
9296         (while (and
9297                 (progn
9298                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9299                   (gnus-summary-show-thread) t)
9300                 (gnus-summary-find-subject subject))
9301           (setq count (1+ count))))
9302        ((> unmark 0)                    ; Tick.
9303         (while (and
9304                 (progn
9305                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9306                   (gnus-summary-show-thread) t)
9307                 (gnus-summary-find-subject subject))
9308           (setq count (1+ count))))
9309        (t                               ; Mark as unread.
9310         (while (and
9311                 (progn
9312                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9313                   (gnus-summary-show-thread) t)
9314                 (gnus-summary-find-subject subject))
9315           (setq count (1+ count)))))
9316       (gnus-set-mode-line 'summary)
9317       ;; Return the number of marked articles.
9318       count)))
9319
9320 (defun gnus-summary-mark-as-processable (n &optional unmark)
9321   "Set the process mark on the next N articles.
9322 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9323 the process mark instead.  The difference between N and the actual
9324 number of articles marked is returned."
9325   (interactive "P")
9326   (if (and (null n) (gnus-region-active-p))
9327       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9328     (setq n (prefix-numeric-value n))
9329     (let ((backward (< n 0))
9330           (n (abs n)))
9331       (while (and
9332               (> n 0)
9333               (if unmark
9334                   (gnus-summary-remove-process-mark
9335                    (gnus-summary-article-number))
9336                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9337               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9338         (setq n (1- n)))
9339       (when (/= 0 n)
9340         (gnus-message 7 "No more articles"))
9341       (gnus-summary-recenter)
9342       (gnus-summary-position-point)
9343       n)))
9344
9345 (defun gnus-summary-unmark-as-processable (n)
9346   "Remove the process mark from the next N articles.
9347 If N is negative, unmark backward instead.  The difference between N and
9348 the actual number of articles unmarked is returned."
9349   (interactive "P")
9350   (gnus-summary-mark-as-processable n t))
9351
9352 (defun gnus-summary-unmark-all-processable ()
9353   "Remove the process mark from all articles."
9354   (interactive)
9355   (save-excursion
9356     (while gnus-newsgroup-processable
9357       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9358   (gnus-summary-position-point))
9359
9360 (defun gnus-summary-add-mark (article type)
9361   "Mark ARTICLE with a mark of TYPE."
9362   (let ((vtype (car (assq type gnus-article-mark-lists)))
9363         var)
9364     (if (not vtype)
9365         (error "No such mark type: %s" type)
9366       (setq var (intern (format "gnus-newsgroup-%s" type)))
9367       (set var (cons article (symbol-value var)))
9368       (if (memq type '(processable cached replied forwarded recent saved))
9369           (gnus-summary-update-secondary-mark article)
9370         ;;; !!! This is bogus.  We should find out what primary
9371         ;;; !!! mark we want to set.
9372         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9373
9374 (defun gnus-summary-mark-as-expirable (n)
9375   "Mark N articles forward as expirable.
9376 If N is negative, mark backward instead.  The difference between N and
9377 the actual number of articles marked is returned."
9378   (interactive "p")
9379   (gnus-summary-mark-forward n gnus-expirable-mark))
9380
9381 (defun gnus-summary-mark-as-spam (n)
9382   "Mark N articles forward as spam.
9383 If N is negative, mark backward instead.  The difference between N and
9384 the actual number of articles marked is returned."
9385   (interactive "p")
9386   (gnus-summary-mark-forward n gnus-spam-mark))
9387
9388 (defun gnus-summary-mark-article-as-replied (article)
9389   "Mark ARTICLE as replied to and update the summary line.
9390 ARTICLE can also be a list of articles."
9391   (interactive (list (gnus-summary-article-number)))
9392   (let ((articles (if (listp article) article (list article))))
9393     (dolist (article articles)
9394       (push article gnus-newsgroup-replied)
9395       (let ((buffer-read-only nil))
9396         (when (gnus-summary-goto-subject article nil t)
9397           (gnus-summary-update-secondary-mark article))))))
9398
9399 (defun gnus-summary-mark-article-as-forwarded (article)
9400   "Mark ARTICLE as forwarded and update the summary line.
9401 ARTICLE can also be a list of articles."
9402   (let ((articles (if (listp article) article (list article))))
9403     (dolist (article articles)
9404       (push article gnus-newsgroup-forwarded)
9405       (let ((buffer-read-only nil))
9406         (when (gnus-summary-goto-subject article nil t)
9407           (gnus-summary-update-secondary-mark article))))))
9408
9409 (defun gnus-summary-set-bookmark (article)
9410   "Set a bookmark in current article."
9411   (interactive (list (gnus-summary-article-number)))
9412   (when (or (not (get-buffer gnus-article-buffer))
9413             (not gnus-current-article)
9414             (not gnus-article-current)
9415             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9416     (error "No current article selected"))
9417   ;; Remove old bookmark, if one exists.
9418   (gnus-pull article gnus-newsgroup-bookmarks)
9419   ;; Set the new bookmark, which is on the form
9420   ;; (article-number . line-number-in-body).
9421   (push
9422    (cons article
9423          (save-excursion
9424            (set-buffer gnus-article-buffer)
9425            (count-lines
9426             (min (point)
9427                  (save-excursion
9428                    (article-goto-body)
9429                    (point)))
9430             (point))))
9431    gnus-newsgroup-bookmarks)
9432   (gnus-message 6 "A bookmark has been added to the current article."))
9433
9434 (defun gnus-summary-remove-bookmark (article)
9435   "Remove the bookmark from the current article."
9436   (interactive (list (gnus-summary-article-number)))
9437   ;; Remove old bookmark, if one exists.
9438   (if (not (assq article gnus-newsgroup-bookmarks))
9439       (gnus-message 6 "No bookmark in current article.")
9440     (gnus-pull article gnus-newsgroup-bookmarks)
9441     (gnus-message 6 "Removed bookmark.")))
9442
9443 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9444 (defun gnus-summary-mark-as-dormant (n)
9445   "Mark N articles forward as dormant.
9446 If N is negative, mark backward instead.  The difference between N and
9447 the actual number of articles marked is returned."
9448   (interactive "p")
9449   (gnus-summary-mark-forward n gnus-dormant-mark))
9450
9451 (defun gnus-summary-set-process-mark (article)
9452   "Set the process mark on ARTICLE and update the summary line."
9453   (setq gnus-newsgroup-processable
9454         (cons article
9455               (delq article gnus-newsgroup-processable)))
9456   (when (gnus-summary-goto-subject article)
9457     (gnus-summary-show-thread)
9458     (gnus-summary-goto-subject article)
9459     (gnus-summary-update-secondary-mark article)))
9460
9461 (defun gnus-summary-remove-process-mark (article)
9462   "Remove the process mark from ARTICLE and update the summary line."
9463   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9464   (when (gnus-summary-goto-subject article)
9465     (gnus-summary-show-thread)
9466     (gnus-summary-goto-subject article)
9467     (gnus-summary-update-secondary-mark article)))
9468
9469 (defun gnus-summary-set-saved-mark (article)
9470   "Set the process mark on ARTICLE and update the summary line."
9471   (push article gnus-newsgroup-saved)
9472   (when (gnus-summary-goto-subject article)
9473     (gnus-summary-update-secondary-mark article)))
9474
9475 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9476   "Mark N articles as read forwards.
9477 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9478 The difference between N and the actual number of articles marked is
9479 returned.
9480 If NO-EXPIRE, auto-expiry will be inhibited."
9481   (interactive "p")
9482   (gnus-summary-show-thread)
9483   (let ((backward (< n 0))
9484         (gnus-summary-goto-unread
9485          (and gnus-summary-goto-unread
9486               (not (eq gnus-summary-goto-unread 'never))
9487               (not (memq mark (list gnus-unread-mark
9488                                     gnus-ticked-mark gnus-dormant-mark)))))
9489         (n (abs n))
9490         (mark (or mark gnus-del-mark)))
9491     (while (and (> n 0)
9492                 (gnus-summary-mark-article nil mark no-expire)
9493                 (zerop (gnus-summary-next-subject
9494                         (if backward -1 1)
9495                         (and gnus-summary-goto-unread
9496                              (not (eq gnus-summary-goto-unread 'never)))
9497                         t)))
9498       (setq n (1- n)))
9499     (when (/= 0 n)
9500       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9501     (gnus-summary-recenter)
9502     (gnus-summary-position-point)
9503     (gnus-set-mode-line 'summary)
9504     n))
9505
9506 (defun gnus-summary-mark-article-as-read (mark)
9507   "Mark the current article quickly as read with MARK."
9508   (let ((article (gnus-summary-article-number)))
9509     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9510     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9511     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9512     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9513     (push (cons article mark) gnus-newsgroup-reads)
9514     ;; Possibly remove from cache, if that is used.
9515     (when gnus-use-cache
9516       (gnus-cache-enter-remove-article article))
9517     ;; Allow the backend to change the mark.
9518     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9519     ;; Check for auto-expiry.
9520     (when (and gnus-newsgroup-auto-expire
9521                (memq mark gnus-auto-expirable-marks))
9522       (setq mark gnus-expirable-mark)
9523       ;; Let the backend know about the mark change.
9524       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9525       (push article gnus-newsgroup-expirable))
9526     ;; Set the mark in the buffer.
9527     (gnus-summary-update-mark mark 'unread)
9528     t))
9529
9530 (defun gnus-summary-mark-article-as-unread (mark)
9531   "Mark the current article quickly as unread with MARK."
9532   (let* ((article (gnus-summary-article-number))
9533          (old-mark (gnus-summary-article-mark article)))
9534     ;; Allow the backend to change the mark.
9535     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9536     (if (eq mark old-mark)
9537         t
9538       (if (<= article 0)
9539           (progn
9540             (gnus-error 1 "Can't mark negative article numbers")
9541             nil)
9542         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9543         (setq gnus-newsgroup-spam-marked
9544               (delq article gnus-newsgroup-spam-marked))
9545         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9546         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9547         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9548         (cond ((= mark gnus-ticked-mark)
9549                (setq gnus-newsgroup-marked
9550                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9551                                               article)))
9552               ((= mark gnus-spam-mark)
9553                (setq gnus-newsgroup-spam-marked
9554                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9555                                               article)))
9556               ((= mark gnus-dormant-mark)
9557                (setq gnus-newsgroup-dormant
9558                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9559                                               article)))
9560               (t
9561                (setq gnus-newsgroup-unreads
9562                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9563                                               article))))
9564         (gnus-pull article gnus-newsgroup-reads)
9565
9566         ;; See whether the article is to be put in the cache.
9567         (and gnus-use-cache
9568              (vectorp (gnus-summary-article-header article))
9569              (save-excursion
9570                (gnus-cache-possibly-enter-article
9571                 gnus-newsgroup-name article
9572                 (= mark gnus-ticked-mark)
9573                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9574
9575         ;; Fix the mark.
9576         (gnus-summary-update-mark mark 'unread)
9577         t))))
9578
9579 (defun gnus-summary-mark-article (&optional article mark no-expire)
9580   "Mark ARTICLE with MARK.  MARK can be any character.
9581 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9582 `??' (dormant) and `?E' (expirable).
9583 If MARK is nil, then the default character `?r' is used.
9584 If ARTICLE is nil, then the article on the current line will be
9585 marked.
9586 If NO-EXPIRE, auto-expiry will be inhibited."
9587   ;; The mark might be a string.
9588   (when (stringp mark)
9589     (setq mark (aref mark 0)))
9590   ;; If no mark is given, then we check auto-expiring.
9591   (when (null mark)
9592     (setq mark gnus-del-mark))
9593   (when (and (not no-expire)
9594              gnus-newsgroup-auto-expire
9595              (memq mark gnus-auto-expirable-marks))
9596     (setq mark gnus-expirable-mark))
9597   (let ((article (or article (gnus-summary-article-number)))
9598         (old-mark (gnus-summary-article-mark article)))
9599     ;; Allow the backend to change the mark.
9600     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9601     (if (eq mark old-mark)
9602         t
9603       (unless article
9604         (error "No article on current line"))
9605       (if (not (if (or (= mark gnus-unread-mark)
9606                        (= mark gnus-ticked-mark)
9607                        (= mark gnus-spam-mark)
9608                        (= mark gnus-dormant-mark))
9609                    (gnus-mark-article-as-unread article mark)
9610                  (gnus-mark-article-as-read article mark)))
9611           t
9612         ;; See whether the article is to be put in the cache.
9613         (and gnus-use-cache
9614              (not (= mark gnus-canceled-mark))
9615              (vectorp (gnus-summary-article-header article))
9616              (save-excursion
9617                (gnus-cache-possibly-enter-article
9618                 gnus-newsgroup-name article
9619                 (= mark gnus-ticked-mark)
9620                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9621
9622         (when (gnus-summary-goto-subject article nil t)
9623           (let ((buffer-read-only nil))
9624             (gnus-summary-show-thread)
9625             ;; Fix the mark.
9626             (gnus-summary-update-mark mark 'unread)
9627             t))))))
9628
9629 (defun gnus-summary-update-secondary-mark (article)
9630   "Update the secondary (read, process, cache) mark."
9631   (gnus-summary-update-mark
9632    (cond ((memq article gnus-newsgroup-processable)
9633           gnus-process-mark)
9634          ((memq article gnus-newsgroup-cached)
9635           gnus-cached-mark)
9636          ((memq article gnus-newsgroup-replied)
9637           gnus-replied-mark)
9638          ((memq article gnus-newsgroup-forwarded)
9639           gnus-forwarded-mark)
9640          ((memq article gnus-newsgroup-saved)
9641           gnus-saved-mark)
9642          ((memq article gnus-newsgroup-recent)
9643           gnus-recent-mark)
9644          ((memq article gnus-newsgroup-unseen)
9645           gnus-unseen-mark)
9646          (t gnus-no-mark))
9647    'replied)
9648   (when (gnus-visual-p 'summary-highlight 'highlight)
9649     (gnus-run-hooks 'gnus-summary-update-hook))
9650   t)
9651
9652 (defun gnus-summary-update-download-mark (article)
9653   "Update the secondary (read, process, cache) mark."
9654   (gnus-summary-update-mark
9655    (cond ((memq article gnus-newsgroup-undownloaded) 
9656           gnus-undownloaded-mark)
9657          (gnus-newsgroup-agentized
9658           gnus-downloaded-mark)
9659          (t
9660           gnus-no-mark))
9661    'download)
9662   (gnus-summary-update-line t)
9663   t)
9664
9665 (defun gnus-summary-update-mark (mark type)
9666   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9667         (buffer-read-only nil))
9668     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9669     (when forward
9670       (when (looking-at "\r")
9671         (incf forward))
9672       (when (<= (+ forward (point)) (point-max))
9673         ;; Go to the right position on the line.
9674         (goto-char (+ forward (point)))
9675         ;; Replace the old mark with the new mark.
9676         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9677         ;; Optionally update the marks by some user rule.
9678         (when (eq type 'unread)
9679           (gnus-data-set-mark
9680            (gnus-data-find (gnus-summary-article-number)) mark)
9681           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9682
9683 (defun gnus-mark-article-as-read (article &optional mark)
9684   "Enter ARTICLE in the pertinent lists and remove it from others."
9685   ;; Make the article expirable.
9686   (let ((mark (or mark gnus-del-mark)))
9687     (setq gnus-newsgroup-expirable
9688           (if (= mark gnus-expirable-mark)
9689               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9690             (delq article gnus-newsgroup-expirable)))
9691     ;; Remove from unread and marked lists.
9692     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9693     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9694     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9695     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9696     (push (cons article mark) gnus-newsgroup-reads)
9697     ;; Possibly remove from cache, if that is used.
9698     (when gnus-use-cache
9699       (gnus-cache-enter-remove-article article))
9700     t))
9701
9702 (defun gnus-mark-article-as-unread (article &optional mark)
9703   "Enter ARTICLE in the pertinent lists and remove it from others."
9704   (let ((mark (or mark gnus-ticked-mark)))
9705     (if (<= article 0)
9706         (progn
9707           (gnus-error 1 "Can't mark negative article numbers")
9708           nil)
9709       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9710             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9711             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9712             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9713             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9714
9715       ;; Unsuppress duplicates?
9716       (when gnus-suppress-duplicates
9717         (gnus-dup-unsuppress-article article))
9718
9719       (cond ((= mark gnus-ticked-mark)
9720              (setq gnus-newsgroup-marked
9721                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9722             ((= mark gnus-spam-mark)
9723              (setq gnus-newsgroup-spam-marked
9724                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9725                                             article)))
9726             ((= mark gnus-dormant-mark)
9727              (setq gnus-newsgroup-dormant
9728                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9729             (t
9730              (setq gnus-newsgroup-unreads
9731                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9732       (gnus-pull article gnus-newsgroup-reads)
9733       t)))
9734
9735 (defalias 'gnus-summary-mark-as-unread-forward
9736   'gnus-summary-tick-article-forward)
9737 (make-obsolete 'gnus-summary-mark-as-unread-forward
9738                'gnus-summary-tick-article-forward)
9739 (defun gnus-summary-tick-article-forward (n)
9740   "Tick N articles forwards.
9741 If N is negative, tick backwards instead.
9742 The difference between N and the number of articles ticked is returned."
9743   (interactive "p")
9744   (gnus-summary-mark-forward n gnus-ticked-mark))
9745
9746 (defalias 'gnus-summary-mark-as-unread-backward
9747   'gnus-summary-tick-article-backward)
9748 (make-obsolete 'gnus-summary-mark-as-unread-backward
9749                'gnus-summary-tick-article-backward)
9750 (defun gnus-summary-tick-article-backward (n)
9751   "Tick N articles backwards.
9752 The difference between N and the number of articles ticked is returned."
9753   (interactive "p")
9754   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9755
9756 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9757 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9758 (defun gnus-summary-tick-article (&optional article clear-mark)
9759   "Mark current article as unread.
9760 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9761 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9762   (interactive)
9763   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9764                                        gnus-ticked-mark)))
9765
9766 (defun gnus-summary-mark-as-read-forward (n)
9767   "Mark N articles as read forwards.
9768 If N is negative, mark backwards instead.
9769 The difference between N and the actual number of articles marked is
9770 returned."
9771   (interactive "p")
9772   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9773
9774 (defun gnus-summary-mark-as-read-backward (n)
9775   "Mark the N articles as read backwards.
9776 The difference between N and the actual number of articles marked is
9777 returned."
9778   (interactive "p")
9779   (gnus-summary-mark-forward
9780    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9781
9782 (defun gnus-summary-mark-as-read (&optional article mark)
9783   "Mark current article as read.
9784 ARTICLE specifies the article to be marked as read.
9785 MARK specifies a string to be inserted at the beginning of the line."
9786   (gnus-summary-mark-article article mark))
9787
9788 (defun gnus-summary-clear-mark-forward (n)
9789   "Clear marks from N articles forward.
9790 If N is negative, clear backward instead.
9791 The difference between N and the number of marks cleared is returned."
9792   (interactive "p")
9793   (gnus-summary-mark-forward n gnus-unread-mark))
9794
9795 (defun gnus-summary-clear-mark-backward (n)
9796   "Clear marks from N articles backward.
9797 The difference between N and the number of marks cleared is returned."
9798   (interactive "p")
9799   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9800
9801 (defun gnus-summary-mark-unread-as-read ()
9802   "Intended to be used by `gnus-summary-mark-article-hook'."
9803   (when (memq gnus-current-article gnus-newsgroup-unreads)
9804     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9805
9806 (defun gnus-summary-mark-read-and-unread-as-read ()
9807   "Intended to be used by `gnus-summary-mark-article-hook'."
9808   (let ((mark (gnus-summary-article-mark)))
9809     (when (or (gnus-unread-mark-p mark)
9810               (gnus-read-mark-p mark))
9811       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9812
9813 (defun gnus-summary-mark-unread-as-ticked ()
9814   "Intended to be used by `gnus-summary-mark-article-hook'."
9815   (when (memq gnus-current-article gnus-newsgroup-unreads)
9816     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9817
9818 (defun gnus-summary-mark-region-as-read (point mark all)
9819   "Mark all unread articles between point and mark as read.
9820 If given a prefix, mark all articles between point and mark as read,
9821 even ticked and dormant ones."
9822   (interactive "r\nP")
9823   (save-excursion
9824     (let (article)
9825       (goto-char point)
9826       (beginning-of-line)
9827       (while (and
9828               (< (point) mark)
9829               (progn
9830                 (when (or all
9831                           (memq (setq article (gnus-summary-article-number))
9832                                 gnus-newsgroup-unreads))
9833                   (gnus-summary-mark-article article gnus-del-mark))
9834                 t)
9835               (gnus-summary-find-next))))))
9836
9837 (defun gnus-summary-mark-below (score mark)
9838   "Mark articles with score less than SCORE with MARK."
9839   (interactive "P\ncMark: ")
9840   (setq score (if score
9841                   (prefix-numeric-value score)
9842                 (or gnus-summary-default-score 0)))
9843   (save-excursion
9844     (set-buffer gnus-summary-buffer)
9845     (goto-char (point-min))
9846     (while
9847         (progn
9848           (and (< (gnus-summary-article-score) score)
9849                (gnus-summary-mark-article nil mark))
9850           (gnus-summary-find-next)))))
9851
9852 (defun gnus-summary-kill-below (&optional score)
9853   "Mark articles with score below SCORE as read."
9854   (interactive "P")
9855   (gnus-summary-mark-below score gnus-killed-mark))
9856
9857 (defun gnus-summary-clear-above (&optional score)
9858   "Clear all marks from articles with score above SCORE."
9859   (interactive "P")
9860   (gnus-summary-mark-above score gnus-unread-mark))
9861
9862 (defun gnus-summary-tick-above (&optional score)
9863   "Tick all articles with score above SCORE."
9864   (interactive "P")
9865   (gnus-summary-mark-above score gnus-ticked-mark))
9866
9867 (defun gnus-summary-mark-above (score mark)
9868   "Mark articles with score over SCORE with MARK."
9869   (interactive "P\ncMark: ")
9870   (setq score (if score
9871                   (prefix-numeric-value score)
9872                 (or gnus-summary-default-score 0)))
9873   (save-excursion
9874     (set-buffer gnus-summary-buffer)
9875     (goto-char (point-min))
9876     (while (and (progn
9877                   (when (> (gnus-summary-article-score) score)
9878                     (gnus-summary-mark-article nil mark))
9879                   t)
9880                 (gnus-summary-find-next)))))
9881
9882 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9883 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9884 (defun gnus-summary-limit-include-expunged (&optional no-error)
9885   "Display all the hidden articles that were expunged for low scores."
9886   (interactive)
9887   (let ((buffer-read-only nil))
9888     (let ((scored gnus-newsgroup-scored)
9889           headers h)
9890       (while scored
9891         (unless (gnus-summary-article-header (caar scored))
9892           (and (setq h (gnus-number-to-header (caar scored)))
9893                (< (cdar scored) gnus-summary-expunge-below)
9894                (push h headers)))
9895         (setq scored (cdr scored)))
9896       (if (not headers)
9897           (when (not no-error)
9898             (error "No expunged articles hidden"))
9899         (goto-char (point-min))
9900         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9901         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9902         (mapcar (lambda (x) (push (mail-header-number x)
9903                                   gnus-newsgroup-limit))
9904                 headers)
9905         (gnus-summary-prepare-unthreaded (nreverse headers))
9906         (goto-char (point-min))
9907         (gnus-summary-position-point)
9908         t))))
9909
9910 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9911   "Mark all unread articles in this newsgroup as read.
9912 If prefix argument ALL is non-nil, ticked and dormant articles will
9913 also be marked as read.
9914 If QUIETLY is non-nil, no questions will be asked.
9915 If TO-HERE is non-nil, it should be a point in the buffer.  All
9916 articles before (after, if REVERSE is set) this point will be marked as read.
9917 Note that this function will only catch up the unread article
9918 in the current summary buffer limitation.
9919 The number of articles marked as read is returned."
9920   (interactive "P")
9921   (prog1
9922       (save-excursion
9923         (when (or quietly
9924                   (not gnus-interactive-catchup) ;Without confirmation?
9925                   gnus-expert-user
9926                   (gnus-y-or-n-p
9927                    (if all
9928                        "Mark absolutely all articles as read? "
9929                      "Mark all unread articles as read? ")))
9930           (if (and not-mark
9931                    (not gnus-newsgroup-adaptive)
9932                    (not gnus-newsgroup-auto-expire)
9933                    (not gnus-suppress-duplicates)
9934                    (or (not gnus-use-cache)
9935                        (eq gnus-use-cache 'passive)))
9936               (progn
9937                 (when all
9938                   (setq gnus-newsgroup-marked nil
9939                         gnus-newsgroup-spam-marked nil
9940                         gnus-newsgroup-dormant nil))
9941                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9942             ;; We actually mark all articles as canceled, which we
9943             ;; have to do when using auto-expiry or adaptive scoring.
9944             (gnus-summary-show-all-threads)
9945             (if (and to-here reverse)
9946                 (progn
9947                   (goto-char to-here)
9948                   (while (and
9949                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9950                           (gnus-summary-find-next (not all) nil nil t))))
9951               (when (gnus-summary-first-subject (not all) t)
9952                 (while (and
9953                         (if to-here (< (point) to-here) t)
9954                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9955                         (gnus-summary-find-next (not all) nil nil t)))))
9956             (gnus-set-mode-line 'summary))
9957           t))
9958     (gnus-summary-position-point)))
9959
9960 (defun gnus-summary-catchup-to-here (&optional all)
9961   "Mark all unticked articles before the current one as read.
9962 If ALL is non-nil, also mark ticked and dormant articles as read."
9963   (interactive "P")
9964   (save-excursion
9965     (gnus-save-hidden-threads
9966       (let ((beg (point)))
9967         ;; We check that there are unread articles.
9968         (when (or all (gnus-summary-find-prev))
9969           (gnus-summary-catchup all t beg)))))
9970   (gnus-summary-position-point))
9971
9972 (defun gnus-summary-catchup-from-here (&optional all)
9973   "Mark all unticked articles after the current one as read.
9974 If ALL is non-nil, also mark ticked and dormant articles as read."
9975   (interactive "P")
9976   (save-excursion
9977     (gnus-save-hidden-threads
9978       (let ((beg (point)))
9979         ;; We check that there are unread articles.
9980         (when (or all (gnus-summary-find-next))
9981           (gnus-summary-catchup all t beg nil t)))))
9982
9983   (gnus-summary-position-point))
9984 (defun gnus-summary-catchup-all (&optional quietly)
9985   "Mark all articles in this newsgroup as read.
9986 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9987 instead, which marks only unread articles as read."
9988   (interactive "P")
9989   (gnus-summary-catchup t quietly))
9990
9991 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9992   "Mark all unread articles in this group as read, then exit.
9993 If prefix argument ALL is non-nil, all articles are marked as read.
9994 If QUIETLY is non-nil, no questions will be asked."
9995   (interactive "P")
9996   (when (gnus-summary-catchup all quietly nil 'fast)
9997     ;; Select next newsgroup or exit.
9998     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9999              (eq gnus-auto-select-next 'quietly))
10000         (gnus-summary-next-group nil)
10001       (gnus-summary-exit))))
10002
10003 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10004   "Mark all articles in this newsgroup as read, and then exit.
10005 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10006 instead, which marks only unread articles as read."
10007   (interactive "P")
10008   (gnus-summary-catchup-and-exit t quietly))
10009
10010 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10011   "Mark all articles in this group as read and select the next group.
10012 If given a prefix, mark all articles, unread as well as ticked, as
10013 read."
10014   (interactive "P")
10015   (save-excursion
10016     (gnus-summary-catchup all))
10017   (gnus-summary-next-group))
10018
10019 ;;;
10020 ;;; with article
10021 ;;;
10022
10023 (defmacro gnus-with-article (article &rest forms)
10024   "Select ARTICLE and perform FORMS in the original article buffer.
10025 Then replace the article with the result."
10026   `(progn
10027      ;; We don't want the article to be marked as read.
10028      (let (gnus-mark-article-hook)
10029        (gnus-summary-select-article t t nil ,article))
10030      (set-buffer gnus-original-article-buffer)
10031      ,@forms
10032      (if (not (gnus-check-backend-function
10033                'request-replace-article (car gnus-article-current)))
10034          (gnus-message 5 "Read-only group; not replacing")
10035        (unless (gnus-request-replace-article
10036                 ,article (car gnus-article-current)
10037                 (current-buffer) t)
10038          (error "Couldn't replace article")))
10039      ;; The cache and backlog have to be flushed somewhat.
10040      (when gnus-keep-backlog
10041        (gnus-backlog-remove-article
10042         (car gnus-article-current) (cdr gnus-article-current)))
10043      (when gnus-use-cache
10044        (gnus-cache-update-article
10045         (car gnus-article-current) (cdr gnus-article-current)))))
10046
10047 (put 'gnus-with-article 'lisp-indent-function 1)
10048 (put 'gnus-with-article 'edebug-form-spec '(form body))
10049
10050 ;; Thread-based commands.
10051
10052 (defun gnus-summary-articles-in-thread (&optional article)
10053   "Return a list of all articles in the current thread.
10054 If ARTICLE is non-nil, return all articles in the thread that starts
10055 with that article."
10056   (let* ((article (or article (gnus-summary-article-number)))
10057          (data (gnus-data-find-list article))
10058          (top-level (gnus-data-level (car data)))
10059          (top-subject
10060           (cond ((null gnus-thread-operation-ignore-subject)
10061                  (gnus-simplify-subject-re
10062                   (mail-header-subject (gnus-data-header (car data)))))
10063                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10064                  (gnus-simplify-subject-fuzzy
10065                   (mail-header-subject (gnus-data-header (car data)))))
10066                 (t nil)))
10067          (end-point (save-excursion
10068                       (if (gnus-summary-go-to-next-thread)
10069                           (point) (point-max))))
10070          articles)
10071     (while (and data
10072                 (< (gnus-data-pos (car data)) end-point))
10073       (when (or (not top-subject)
10074                 (string= top-subject
10075                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10076                              (gnus-simplify-subject-fuzzy
10077                               (mail-header-subject
10078                                (gnus-data-header (car data))))
10079                            (gnus-simplify-subject-re
10080                             (mail-header-subject
10081                              (gnus-data-header (car data)))))))
10082         (push (gnus-data-number (car data)) articles))
10083       (unless (and (setq data (cdr data))
10084                    (> (gnus-data-level (car data)) top-level))
10085         (setq data nil)))
10086     ;; Return the list of articles.
10087     (nreverse articles)))
10088
10089 (defun gnus-summary-rethread-current ()
10090   "Rethread the thread the current article is part of."
10091   (interactive)
10092   (let* ((gnus-show-threads t)
10093          (article (gnus-summary-article-number))
10094          (id (mail-header-id (gnus-summary-article-header)))
10095          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10096     (unless id
10097       (error "No article on the current line"))
10098     (gnus-rebuild-thread id)
10099     (gnus-summary-goto-subject article)))
10100
10101 (defun gnus-summary-reparent-thread ()
10102   "Make the current article child of the marked (or previous) article.
10103
10104 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10105 is non-nil or the Subject: of both articles are the same."
10106   (interactive)
10107   (unless (not (gnus-group-read-only-p))
10108     (error "The current newsgroup does not support article editing"))
10109   (unless (<= (length gnus-newsgroup-processable) 1)
10110     (error "No more than one article may be marked"))
10111   (save-window-excursion
10112     (let ((gnus-article-buffer " *reparent*")
10113           (current-article (gnus-summary-article-number))
10114           ;; First grab the marked article, otherwise one line up.
10115           (parent-article (if (not (null gnus-newsgroup-processable))
10116                               (car gnus-newsgroup-processable)
10117                             (save-excursion
10118                               (if (eq (forward-line -1) 0)
10119                                   (gnus-summary-article-number)
10120                                 (error "Beginning of summary buffer"))))))
10121       (unless (not (eq current-article parent-article))
10122         (error "An article may not be self-referential"))
10123       (let ((message-id (mail-header-id
10124                          (gnus-summary-article-header parent-article))))
10125         (unless (and message-id (not (equal message-id "")))
10126           (error "No message-id in desired parent"))
10127         (gnus-with-article current-article
10128           (save-restriction
10129             (goto-char (point-min))
10130             (message-narrow-to-head)
10131             (if (re-search-forward "^References: " nil t)
10132                 (progn
10133                   (re-search-forward "^[^ \t]" nil t)
10134                   (forward-line -1)
10135                   (end-of-line)
10136                   (insert " " message-id))
10137               (insert "References: " message-id "\n"))))
10138         (set-buffer gnus-summary-buffer)
10139         (gnus-summary-unmark-all-processable)
10140         (gnus-summary-update-article current-article)
10141         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10142             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10143         (gnus-summary-rethread-current)
10144         (gnus-message 3 "Article %d is now the child of article %d"
10145                       current-article parent-article)))))
10146
10147 (defun gnus-summary-toggle-threads (&optional arg)
10148   "Toggle showing conversation threads.
10149 If ARG is positive number, turn showing conversation threads on."
10150   (interactive "P")
10151   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10152     (setq gnus-show-threads
10153           (if (null arg) (not gnus-show-threads)
10154             (> (prefix-numeric-value arg) 0)))
10155     (gnus-summary-prepare)
10156     (gnus-summary-goto-subject current)
10157     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10158     (gnus-summary-position-point)))
10159
10160 (defun gnus-summary-show-all-threads ()
10161   "Show all threads."
10162   (interactive)
10163   (save-excursion
10164     (let ((buffer-read-only nil))
10165       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10166   (gnus-summary-position-point))
10167
10168 (defun gnus-summary-show-thread ()
10169   "Show thread subtrees.
10170 Returns nil if no thread was there to be shown."
10171   (interactive)
10172   (let ((buffer-read-only nil)
10173         (orig (point))
10174         ;; first goto end then to beg, to have point at beg after let
10175         (end (progn (end-of-line) (point)))
10176         (beg (progn (beginning-of-line) (point))))
10177     (prog1
10178         ;; Any hidden lines here?
10179         (search-forward "\r" end t)
10180       (subst-char-in-region beg end ?\^M ?\n t)
10181       (goto-char orig)
10182       (gnus-summary-position-point))))
10183
10184 (defun gnus-summary-maybe-hide-threads ()
10185   "If requested, hide the threads that should be hidden."
10186   (when (and gnus-show-threads
10187              gnus-thread-hide-subtree)
10188     (gnus-summary-hide-all-threads
10189      (if (or (consp gnus-thread-hide-subtree)
10190              (gnus-functionp gnus-thread-hide-subtree))
10191          (gnus-make-predicate gnus-thread-hide-subtree)
10192        nil))))
10193
10194 ;;; Hiding predicates.
10195
10196 (defun gnus-article-unread-p (header)
10197   (memq (mail-header-number header) gnus-newsgroup-unreads))
10198
10199 (defun gnus-article-unseen-p (header)
10200   (memq (mail-header-number header) gnus-newsgroup-unseen))
10201
10202 (defun gnus-map-articles (predicate articles)
10203   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10204   (apply 'gnus-or (mapcar predicate
10205                           (mapcar 'gnus-summary-article-header articles))))
10206
10207 (defun gnus-summary-hide-all-threads (&optional predicate)
10208   "Hide all thread subtrees.
10209 If PREDICATE is supplied, threads that satisfy this predicate
10210 will not be hidden."
10211   (interactive)
10212   (save-excursion
10213     (goto-char (point-min))
10214     (let ((end nil))
10215       (while (not end)
10216         (when (or (not predicate)
10217                   (gnus-map-articles
10218                    predicate (gnus-summary-article-children)))
10219             (gnus-summary-hide-thread))
10220         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10221   (gnus-summary-position-point))
10222
10223 (defun gnus-summary-hide-thread ()
10224   "Hide thread subtrees.
10225 If PREDICATE is supplied, threads that satisfy this predicate
10226 will not be hidden.
10227 Returns nil if no threads were there to be hidden."
10228   (interactive)
10229   (let ((buffer-read-only nil)
10230         (start (point))
10231         (article (gnus-summary-article-number)))
10232     (goto-char start)
10233     ;; Go forward until either the buffer ends or the subthread
10234     ;; ends.
10235     (when (and (not (eobp))
10236                (or (zerop (gnus-summary-next-thread 1 t))
10237                    (goto-char (point-max))))
10238       (prog1
10239           (if (and (> (point) start)
10240                    (search-backward "\n" start t))
10241               (progn
10242                 (subst-char-in-region start (point) ?\n ?\^M)
10243                 (gnus-summary-goto-subject article))
10244             (goto-char start)
10245             nil)))))
10246
10247 (defun gnus-summary-go-to-next-thread (&optional previous)
10248   "Go to the same level (or less) next thread.
10249 If PREVIOUS is non-nil, go to previous thread instead.
10250 Return the article number moved to, or nil if moving was impossible."
10251   (let ((level (gnus-summary-thread-level))
10252         (way (if previous -1 1))
10253         (beg (point)))
10254     (forward-line way)
10255     (while (and (not (eobp))
10256                 (< level (gnus-summary-thread-level)))
10257       (forward-line way))
10258     (if (eobp)
10259         (progn
10260           (goto-char beg)
10261           nil)
10262       (setq beg (point))
10263       (prog1
10264           (gnus-summary-article-number)
10265         (goto-char beg)))))
10266
10267 (defun gnus-summary-next-thread (n &optional silent)
10268   "Go to the same level next N'th thread.
10269 If N is negative, search backward instead.
10270 Returns the difference between N and the number of skips actually
10271 done.
10272
10273 If SILENT, don't output messages."
10274   (interactive "p")
10275   (let ((backward (< n 0))
10276         (n (abs n)))
10277     (while (and (> n 0)
10278                 (gnus-summary-go-to-next-thread backward))
10279       (decf n))
10280     (unless silent
10281       (gnus-summary-position-point))
10282     (when (and (not silent) (/= 0 n))
10283       (gnus-message 7 "No more threads"))
10284     n))
10285
10286 (defun gnus-summary-prev-thread (n)
10287   "Go to the same level previous N'th thread.
10288 Returns the difference between N and the number of skips actually
10289 done."
10290   (interactive "p")
10291   (gnus-summary-next-thread (- n)))
10292
10293 (defun gnus-summary-go-down-thread ()
10294   "Go down one level in the current thread."
10295   (let ((children (gnus-summary-article-children)))
10296     (when children
10297       (gnus-summary-goto-subject (car children)))))
10298
10299 (defun gnus-summary-go-up-thread ()
10300   "Go up one level in the current thread."
10301   (let ((parent (gnus-summary-article-parent)))
10302     (when parent
10303       (gnus-summary-goto-subject parent))))
10304
10305 (defun gnus-summary-down-thread (n)
10306   "Go down thread N steps.
10307 If N is negative, go up instead.
10308 Returns the difference between N and how many steps down that were
10309 taken."
10310   (interactive "p")
10311   (let ((up (< n 0))
10312         (n (abs n)))
10313     (while (and (> n 0)
10314                 (if up (gnus-summary-go-up-thread)
10315                   (gnus-summary-go-down-thread)))
10316       (setq n (1- n)))
10317     (gnus-summary-position-point)
10318     (when (/= 0 n)
10319       (gnus-message 7 "Can't go further"))
10320     n))
10321
10322 (defun gnus-summary-up-thread (n)
10323   "Go up thread N steps.
10324 If N is negative, go down instead.
10325 Returns the difference between N and how many steps down that were
10326 taken."
10327   (interactive "p")
10328   (gnus-summary-down-thread (- n)))
10329
10330 (defun gnus-summary-top-thread ()
10331   "Go to the top of the thread."
10332   (interactive)
10333   (while (gnus-summary-go-up-thread))
10334   (gnus-summary-article-number))
10335
10336 (defun gnus-summary-kill-thread (&optional unmark)
10337   "Mark articles under current thread as read.
10338 If the prefix argument is positive, remove any kinds of marks.
10339 If the prefix argument is negative, tick articles instead."
10340   (interactive "P")
10341   (when unmark
10342     (setq unmark (prefix-numeric-value unmark)))
10343   (let ((articles (gnus-summary-articles-in-thread)))
10344     (save-excursion
10345       ;; Expand the thread.
10346       (gnus-summary-show-thread)
10347       ;; Mark all the articles.
10348       (while articles
10349         (gnus-summary-goto-subject (car articles))
10350         (cond ((null unmark)
10351                (gnus-summary-mark-article-as-read gnus-killed-mark))
10352               ((> unmark 0)
10353                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10354               (t
10355                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10356         (setq articles (cdr articles))))
10357     ;; Hide killed subtrees.
10358     (and (null unmark)
10359          gnus-thread-hide-killed
10360          (gnus-summary-hide-thread))
10361     ;; If marked as read, go to next unread subject.
10362     (when (null unmark)
10363       ;; Go to next unread subject.
10364       (gnus-summary-next-subject 1 t)))
10365   (gnus-set-mode-line 'summary))
10366
10367 ;; Summary sorting commands
10368
10369 (defun gnus-summary-sort-by-number (&optional reverse)
10370   "Sort the summary buffer by article number.
10371 Argument REVERSE means reverse order."
10372   (interactive "P")
10373   (gnus-summary-sort 'number reverse))
10374
10375 (defun gnus-summary-sort-by-random (&optional reverse)
10376   "Randomize the order in the summary buffer.
10377 Argument REVERSE means to randomize in reverse order."
10378   (interactive "P")
10379   (gnus-summary-sort 'random reverse))
10380
10381 (defun gnus-summary-sort-by-author (&optional reverse)
10382   "Sort the summary buffer by author name alphabetically.
10383 If `case-fold-search' is non-nil, case of letters is ignored.
10384 Argument REVERSE means reverse order."
10385   (interactive "P")
10386   (gnus-summary-sort 'author reverse))
10387
10388 (defun gnus-summary-sort-by-subject (&optional reverse)
10389   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10390 If `case-fold-search' is non-nil, case of letters is ignored.
10391 Argument REVERSE means reverse order."
10392   (interactive "P")
10393   (gnus-summary-sort 'subject reverse))
10394
10395 (defun gnus-summary-sort-by-date (&optional reverse)
10396   "Sort the summary buffer by date.
10397 Argument REVERSE means reverse order."
10398   (interactive "P")
10399   (gnus-summary-sort 'date reverse))
10400
10401 (defun gnus-summary-sort-by-score (&optional reverse)
10402   "Sort the summary buffer by score.
10403 Argument REVERSE means reverse order."
10404   (interactive "P")
10405   (gnus-summary-sort 'score reverse))
10406
10407 (defun gnus-summary-sort-by-lines (&optional reverse)
10408   "Sort the summary buffer by the number of lines.
10409 Argument REVERSE means reverse order."
10410   (interactive "P")
10411   (gnus-summary-sort 'lines reverse))
10412
10413 (defun gnus-summary-sort-by-chars (&optional reverse)
10414   "Sort the summary buffer by article length.
10415 Argument REVERSE means reverse order."
10416   (interactive "P")
10417   (gnus-summary-sort 'chars reverse))
10418
10419 (defun gnus-summary-sort-by-original (&optional reverse)
10420   "Sort the summary buffer using the default sorting method.
10421 Argument REVERSE means reverse order."
10422   (interactive "P")
10423   (let* ((buffer-read-only)
10424          (gnus-summary-prepare-hook nil))
10425     ;; We do the sorting by regenerating the threads.
10426     (gnus-summary-prepare)
10427     ;; Hide subthreads if needed.
10428     (gnus-summary-maybe-hide-threads)))
10429
10430 (defun gnus-summary-sort (predicate reverse)
10431   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10432   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10433          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10434          (gnus-thread-sort-functions
10435           (if (not reverse)
10436               thread
10437             `(lambda (t1 t2)
10438                (,thread t2 t1))))
10439          (gnus-sort-gathered-threads-function
10440           gnus-thread-sort-functions)
10441          (gnus-article-sort-functions
10442           (if (not reverse)
10443               article
10444             `(lambda (t1 t2)
10445                (,article t2 t1))))
10446          (buffer-read-only)
10447          (gnus-summary-prepare-hook nil))
10448     ;; We do the sorting by regenerating the threads.
10449     (gnus-summary-prepare)
10450     ;; Hide subthreads if needed.
10451     (gnus-summary-maybe-hide-threads)))
10452
10453 ;; Summary saving commands.
10454
10455 (defun gnus-summary-save-article (&optional n not-saved)
10456   "Save the current article using the default saver function.
10457 If N is a positive number, save the N next articles.
10458 If N is a negative number, save the N previous articles.
10459 If N is nil and any articles have been marked with the process mark,
10460 save those articles instead.
10461 The variable `gnus-default-article-saver' specifies the saver function."
10462   (interactive "P")
10463   (let* ((articles (gnus-summary-work-articles n))
10464          (save-buffer (save-excursion
10465                         (nnheader-set-temp-buffer " *Gnus Save*")))
10466          (num (length articles))
10467          header file)
10468     (dolist (article articles)
10469       (setq header (gnus-summary-article-header article))
10470       (if (not (vectorp header))
10471           ;; This is a pseudo-article.
10472           (if (assq 'name header)
10473               (gnus-copy-file (cdr (assq 'name header)))
10474             (gnus-message 1 "Article %d is unsaveable" article))
10475         ;; This is a real article.
10476         (save-window-excursion
10477           (let ((gnus-display-mime-function nil)
10478                 (gnus-article-prepare-hook nil))
10479             (gnus-summary-select-article t nil nil article)))
10480         (save-excursion
10481           (set-buffer save-buffer)
10482           (erase-buffer)
10483           (insert-buffer-substring gnus-original-article-buffer))
10484         (setq file (gnus-article-save save-buffer file num))
10485         (gnus-summary-remove-process-mark article)
10486         (unless not-saved
10487           (gnus-summary-set-saved-mark article))))
10488     (gnus-kill-buffer save-buffer)
10489     (gnus-summary-position-point)
10490     (gnus-set-mode-line 'summary)
10491     n))
10492
10493 (defun gnus-summary-pipe-output (&optional arg headers)
10494   "Pipe the current article to a subprocess.
10495 If N is a positive number, pipe the N next articles.
10496 If N is a negative number, pipe the N previous articles.
10497 If N is nil and any articles have been marked with the process mark,
10498 pipe those articles instead.
10499 If HEADERS (the symbolic prefix), include the headers, too."
10500   (interactive (gnus-interactive "P\ny"))
10501   (require 'gnus-art)
10502   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10503         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10504     (gnus-summary-save-article arg t))
10505   (let ((buffer (get-buffer "*Shell Command Output*")))
10506     (when (and buffer
10507                (not (zerop (buffer-size buffer))))
10508       (gnus-configure-windows 'pipe))))
10509
10510 (defun gnus-summary-save-article-mail (&optional arg)
10511   "Append the current article to an mail file.
10512 If N is a positive number, save the N next articles.
10513 If N is a negative number, save the N previous articles.
10514 If N is nil and any articles have been marked with the process mark,
10515 save those articles instead."
10516   (interactive "P")
10517   (require 'gnus-art)
10518   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10519     (gnus-summary-save-article arg)))
10520
10521 (defun gnus-summary-save-article-rmail (&optional arg)
10522   "Append the current article to an rmail file.
10523 If N is a positive number, save the N next articles.
10524 If N is a negative number, save the N previous articles.
10525 If N is nil and any articles have been marked with the process mark,
10526 save those articles instead."
10527   (interactive "P")
10528   (require 'gnus-art)
10529   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10530     (gnus-summary-save-article arg)))
10531
10532 (defun gnus-summary-save-article-file (&optional arg)
10533   "Append the current article to a file.
10534 If N is a positive number, save the N next articles.
10535 If N is a negative number, save the N previous articles.
10536 If N is nil and any articles have been marked with the process mark,
10537 save those articles instead."
10538   (interactive "P")
10539   (require 'gnus-art)
10540   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10541     (gnus-summary-save-article arg)))
10542
10543 (defun gnus-summary-write-article-file (&optional arg)
10544   "Write the current article to a file, deleting the previous file.
10545 If N is a positive number, save the N next articles.
10546 If N is a negative number, save the N previous articles.
10547 If N is nil and any articles have been marked with the process mark,
10548 save those articles instead."
10549   (interactive "P")
10550   (require 'gnus-art)
10551   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10552     (gnus-summary-save-article arg)))
10553
10554 (defun gnus-summary-save-article-body-file (&optional arg)
10555   "Append the current article body to a file.
10556 If N is a positive number, save the N next articles.
10557 If N is a negative number, save the N previous articles.
10558 If N is nil and any articles have been marked with the process mark,
10559 save those articles instead."
10560   (interactive "P")
10561   (require 'gnus-art)
10562   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10563     (gnus-summary-save-article arg)))
10564
10565 (defun gnus-summary-muttprint (&optional arg)
10566   "Print the current article using Muttprint.
10567 If N is a positive number, save the N next articles.
10568 If N is a negative number, save the N previous articles.
10569 If N is nil and any articles have been marked with the process mark,
10570 save those articles instead."
10571   (interactive "P")
10572   (require 'gnus-art)
10573   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10574     (gnus-summary-save-article arg t)))
10575
10576 (defun gnus-summary-pipe-message (program)
10577   "Pipe the current article through PROGRAM."
10578   (interactive "sProgram: ")
10579   (gnus-summary-select-article)
10580   (let ((mail-header-separator ""))
10581     (gnus-eval-in-buffer-window gnus-article-buffer
10582       (save-restriction
10583         (widen)
10584         (let ((start (window-start))
10585               buffer-read-only)
10586           (message-pipe-buffer-body program)
10587           (set-window-start (get-buffer-window (current-buffer)) start))))))
10588
10589 (defun gnus-get-split-value (methods)
10590   "Return a value based on the split METHODS."
10591   (let (split-name method result match)
10592     (when methods
10593       (save-excursion
10594         (set-buffer gnus-original-article-buffer)
10595         (save-restriction
10596           (nnheader-narrow-to-headers)
10597           (while (and methods (not split-name))
10598             (goto-char (point-min))
10599             (setq method (pop methods))
10600             (setq match (car method))
10601             (when (cond
10602                    ((stringp match)
10603                     ;; Regular expression.
10604                     (ignore-errors
10605                       (re-search-forward match nil t)))
10606                    ((gnus-functionp match)
10607                     ;; Function.
10608                     (save-restriction
10609                       (widen)
10610                       (setq result (funcall match gnus-newsgroup-name))))
10611                    ((consp match)
10612                     ;; Form.
10613                     (save-restriction
10614                       (widen)
10615                       (setq result (eval match)))))
10616               (setq split-name (cdr method))
10617               (cond ((stringp result)
10618                      (push (expand-file-name
10619                             result gnus-article-save-directory)
10620                            split-name))
10621                     ((consp result)
10622                      (setq split-name (append result split-name)))))))))
10623     (nreverse split-name)))
10624
10625 (defun gnus-valid-move-group-p (group)
10626   (and (boundp group)
10627        (symbol-name group)
10628        (symbol-value group)
10629        (gnus-get-function (gnus-find-method-for-group
10630                            (symbol-name group)) 'request-accept-article t)))
10631
10632 (defun gnus-read-move-group-name (prompt default articles prefix)
10633   "Read a group name."
10634   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10635          (minibuffer-confirm-incomplete nil) ; XEmacs
10636          (prom
10637           (format "%s %s to:"
10638                   prompt
10639                   (if (> (length articles) 1)
10640                       (format "these %d articles" (length articles))
10641                     "this article")))
10642          (to-newsgroup
10643           (cond
10644            ((null split-name)
10645             (gnus-completing-read-with-default
10646              default prom
10647              gnus-active-hashtb
10648              'gnus-valid-move-group-p
10649              nil prefix
10650              'gnus-group-history))
10651            ((= 1 (length split-name))
10652             (gnus-completing-read-with-default
10653              (car split-name) prom
10654              gnus-active-hashtb
10655              'gnus-valid-move-group-p
10656              nil nil
10657              'gnus-group-history))
10658            (t
10659             (gnus-completing-read-with-default
10660              nil prom
10661              (mapcar (lambda (el) (list el))
10662                      (nreverse split-name))
10663              nil nil nil
10664              'gnus-group-history))))
10665          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10666     (when to-newsgroup
10667       (if (or (string= to-newsgroup "")
10668               (string= to-newsgroup prefix))
10669           (setq to-newsgroup default))
10670       (unless to-newsgroup
10671         (error "No group name entered"))
10672       (or (gnus-active to-newsgroup)
10673           (gnus-activate-group to-newsgroup nil nil to-method)
10674           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10675                                      to-newsgroup))
10676               (or (and (gnus-request-create-group to-newsgroup to-method)
10677                        (gnus-activate-group
10678                         to-newsgroup nil nil to-method)
10679                        (gnus-subscribe-group to-newsgroup))
10680                   (error "Couldn't create group %s" to-newsgroup)))
10681           (error "No such group: %s" to-newsgroup)))
10682     to-newsgroup))
10683
10684 (defun gnus-summary-save-parts (type dir n &optional reverse)
10685   "Save parts matching TYPE to DIR.
10686 If REVERSE, save parts that do not match TYPE."
10687   (interactive
10688    (list (read-string "Save parts of type: "
10689                       (or (car gnus-summary-save-parts-type-history)
10690                           gnus-summary-save-parts-default-mime)
10691                       'gnus-summary-save-parts-type-history)
10692          (setq gnus-summary-save-parts-last-directory
10693                (read-file-name "Save to directory: "
10694                                gnus-summary-save-parts-last-directory
10695                                nil t))
10696          current-prefix-arg))
10697   (gnus-summary-iterate n
10698     (let ((gnus-display-mime-function nil)
10699           (gnus-inhibit-treatment t))
10700       (gnus-summary-select-article))
10701     (save-excursion
10702       (set-buffer gnus-article-buffer)
10703       (let ((handles (or gnus-article-mime-handles
10704                          (mm-dissect-buffer nil gnus-article-loose-mime)
10705                          (mm-uu-dissect))))
10706         (when handles
10707           (gnus-summary-save-parts-1 type dir handles reverse)
10708           (unless gnus-article-mime-handles ;; Don't destroy this case.
10709             (mm-destroy-parts handles)))))))
10710
10711 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10712   (if (stringp (car handle))
10713       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10714               (cdr handle))
10715     (when (if reverse
10716               (not (string-match type (mm-handle-media-type handle)))
10717             (string-match type (mm-handle-media-type handle)))
10718       (let ((file (expand-file-name
10719                    (file-name-nondirectory
10720                     (or
10721                      (mail-content-type-get
10722                       (mm-handle-disposition handle) 'filename)
10723                      (concat gnus-newsgroup-name
10724                              "." (number-to-string
10725                                   (cdr gnus-article-current)))))
10726                    dir)))
10727         (unless (file-exists-p file)
10728           (mm-save-part-to-file handle file))))))
10729
10730 ;; Summary extract commands
10731
10732 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10733   (let ((buffer-read-only nil)
10734         (article (gnus-summary-article-number))
10735         after-article b e)
10736     (unless (gnus-summary-goto-subject article)
10737       (error "No such article: %d" article))
10738     (gnus-summary-position-point)
10739     ;; If all commands are to be bunched up on one line, we collect
10740     ;; them here.
10741     (unless gnus-view-pseudos-separately
10742       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10743             files action)
10744         (while ps
10745           (setq action (cdr (assq 'action (car ps))))
10746           (setq files (list (cdr (assq 'name (car ps)))))
10747           (while (and ps (cdr ps)
10748                       (string= (or action "1")
10749                                (or (cdr (assq 'action (cadr ps))) "2")))
10750             (push (cdr (assq 'name (cadr ps))) files)
10751             (setcdr ps (cddr ps)))
10752           (when files
10753             (when (not (string-match "%s" action))
10754               (push " " files))
10755             (push " " files)
10756             (when (assq 'execute (car ps))
10757               (setcdr (assq 'execute (car ps))
10758                       (funcall (if (string-match "%s" action)
10759                                    'format 'concat)
10760                                action
10761                                (mapconcat
10762                                 (lambda (f)
10763                                   (if (equal f " ")
10764                                       f
10765                                     (mm-quote-arg f)))
10766                                 files " ")))))
10767           (setq ps (cdr ps)))))
10768     (if (and gnus-view-pseudos (not not-view))
10769         (while pslist
10770           (when (assq 'execute (car pslist))
10771             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10772                                   (eq gnus-view-pseudos 'not-confirm)))
10773           (setq pslist (cdr pslist)))
10774       (save-excursion
10775         (while pslist
10776           (setq after-article (or (cdr (assq 'article (car pslist)))
10777                                   (gnus-summary-article-number)))
10778           (gnus-summary-goto-subject after-article)
10779           (forward-line 1)
10780           (setq b (point))
10781           (insert "    " (file-name-nondirectory
10782                           (cdr (assq 'name (car pslist))))
10783                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10784           (setq e (point))
10785           (forward-line -1)             ; back to `b'
10786           (gnus-add-text-properties
10787            b (1- e) (list 'gnus-number gnus-reffed-article-number
10788                           gnus-mouse-face-prop gnus-mouse-face))
10789           (gnus-data-enter
10790            after-article gnus-reffed-article-number
10791            gnus-unread-mark b (car pslist) 0 (- e b))
10792           (setq gnus-newsgroup-unreads
10793                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10794                                          gnus-reffed-article-number))
10795           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10796           (setq pslist (cdr pslist)))))))
10797
10798 (defun gnus-pseudos< (p1 p2)
10799   (let ((c1 (cdr (assq 'action p1)))
10800         (c2 (cdr (assq 'action p2))))
10801     (and c1 c2 (string< c1 c2))))
10802
10803 (defun gnus-request-pseudo-article (props)
10804   (cond ((assq 'execute props)
10805          (gnus-execute-command (cdr (assq 'execute props)))))
10806   (let ((gnus-current-article (gnus-summary-article-number)))
10807     (gnus-run-hooks 'gnus-mark-article-hook)))
10808
10809 (defun gnus-execute-command (command &optional automatic)
10810   (save-excursion
10811     (gnus-article-setup-buffer)
10812     (set-buffer gnus-article-buffer)
10813     (setq buffer-read-only nil)
10814     (let ((command (if automatic command
10815                      (read-string "Command: " (cons command 0)))))
10816       (erase-buffer)
10817       (insert "$ " command "\n\n")
10818       (if gnus-view-pseudo-asynchronously
10819           (start-process "gnus-execute" (current-buffer) shell-file-name
10820                          shell-command-switch command)
10821         (call-process shell-file-name nil t nil
10822                       shell-command-switch command)))))
10823
10824 ;; Summary kill commands.
10825
10826 (defun gnus-summary-edit-global-kill (article)
10827   "Edit the \"global\" kill file."
10828   (interactive (list (gnus-summary-article-number)))
10829   (gnus-group-edit-global-kill article))
10830
10831 (defun gnus-summary-edit-local-kill ()
10832   "Edit a local kill file applied to the current newsgroup."
10833   (interactive)
10834   (setq gnus-current-headers (gnus-summary-article-header))
10835   (gnus-group-edit-local-kill
10836    (gnus-summary-article-number) gnus-newsgroup-name))
10837
10838 ;;; Header reading.
10839
10840 (defun gnus-read-header (id &optional header)
10841   "Read the headers of article ID and enter them into the Gnus system."
10842   (let ((group gnus-newsgroup-name)
10843         (gnus-override-method
10844          (or
10845           gnus-override-method
10846           (and (gnus-news-group-p gnus-newsgroup-name)
10847                (car (gnus-refer-article-methods)))))
10848         where)
10849     ;; First we check to see whether the header in question is already
10850     ;; fetched.
10851     (if (stringp id)
10852         ;; This is a Message-ID.
10853         (setq header (or header (gnus-id-to-header id)))
10854       ;; This is an article number.
10855       (setq header (or header (gnus-summary-article-header id))))
10856     (if (and header
10857              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10858         ;; We have found the header.
10859         header
10860       ;; If this is a sparse article, we have to nix out its
10861       ;; previous entry in the thread hashtb.
10862       (when (and header
10863                  (gnus-summary-article-sparse-p (mail-header-number header)))
10864         (let* ((parent (gnus-parent-id (mail-header-references header)))
10865                (thread (and parent (gnus-id-to-thread parent))))
10866           (when thread
10867             (delq (assq header thread) thread))))
10868       ;; We have to really fetch the header to this article.
10869       (save-excursion
10870         (set-buffer nntp-server-buffer)
10871         (when (setq where (gnus-request-head id group))
10872           (nnheader-fold-continuation-lines)
10873           (goto-char (point-max))
10874           (insert ".\n")
10875           (goto-char (point-min))
10876           (insert "211 ")
10877           (princ (cond
10878                   ((numberp id) id)
10879                   ((cdr where) (cdr where))
10880                   (header (mail-header-number header))
10881                   (t gnus-reffed-article-number))
10882                  (current-buffer))
10883           (insert " Article retrieved.\n"))
10884         (if (or (not where)
10885                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10886             ()                          ; Malformed head.
10887           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10888             (when (and (stringp id)
10889                        (not (string= (gnus-group-real-name group)
10890                                      (car where))))
10891               ;; If we fetched by Message-ID and the article came
10892               ;; from a different group, we fudge some bogus article
10893               ;; numbers for this article.
10894               (mail-header-set-number header gnus-reffed-article-number))
10895             (save-excursion
10896               (set-buffer gnus-summary-buffer)
10897               (decf gnus-reffed-article-number)
10898               (gnus-remove-header (mail-header-number header))
10899               (push header gnus-newsgroup-headers)
10900               (setq gnus-current-headers header)
10901               (push (mail-header-number header) gnus-newsgroup-limit)))
10902           header)))))
10903
10904 (defun gnus-remove-header (number)
10905   "Remove header NUMBER from `gnus-newsgroup-headers'."
10906   (if (and gnus-newsgroup-headers
10907            (= number (mail-header-number (car gnus-newsgroup-headers))))
10908       (pop gnus-newsgroup-headers)
10909     (let ((headers gnus-newsgroup-headers))
10910       (while (and (cdr headers)
10911                   (not (= number (mail-header-number (cadr headers)))))
10912         (pop headers))
10913       (when (cdr headers)
10914         (setcdr headers (cddr headers))))))
10915
10916 ;;;
10917 ;;; summary highlights
10918 ;;;
10919
10920 (defun gnus-highlight-selected-summary ()
10921   "Highlight selected article in summary buffer."
10922   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10923   (when gnus-summary-selected-face
10924     (save-excursion
10925       (let* ((beg (progn (beginning-of-line) (point)))
10926              (end (progn (end-of-line) (point)))
10927              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10928              (from (if (get-text-property beg gnus-mouse-face-prop)
10929                        beg
10930                      (or (next-single-property-change
10931                           beg gnus-mouse-face-prop nil end)
10932                          beg)))
10933              (to
10934               (if (= from end)
10935                   (- from 2)
10936                 (or (next-single-property-change
10937                      from gnus-mouse-face-prop nil end)
10938                     end))))
10939         ;; If no mouse-face prop on line we will have to = from = end,
10940         ;; so we highlight the entire line instead.
10941         (when (= (+ to 2) from)
10942           (setq from beg)
10943           (setq to end))
10944         (if gnus-newsgroup-selected-overlay
10945             ;; Move old overlay.
10946             (gnus-move-overlay
10947              gnus-newsgroup-selected-overlay from to (current-buffer))
10948           ;; Create new overlay.
10949           (gnus-overlay-put
10950            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10951            'face gnus-summary-selected-face))))))
10952
10953 (defvar gnus-summary-highlight-line-cached nil)
10954 (defvar gnus-summary-highlight-line-trigger nil)
10955
10956 (defun gnus-summary-highlight-line-0 ()
10957   (if (and (eq gnus-summary-highlight-line-trigger 
10958                gnus-summary-highlight)
10959            gnus-summary-highlight-line-cached)
10960       gnus-summary-highlight-line-cached
10961     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10962           gnus-summary-highlight-line-cached
10963           (let* ((cond (list 'cond))
10964                  (c cond)
10965                  (list gnus-summary-highlight))
10966             (while list
10967               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10968               (setq c (cdr c)
10969                     list (cdr list)))
10970             (gnus-byte-compile (list 'lambda nil cond))))))
10971
10972 (defun gnus-summary-highlight-line ()
10973   "Highlight current line according to `gnus-summary-highlight'."
10974   (let* ((beg (gnus-point-at-bol))
10975          (article (or (gnus-summary-article-number) gnus-current-article))
10976          (score (or (cdr (assq article
10977                                gnus-newsgroup-scored))
10978                     gnus-summary-default-score 0))
10979          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10980          (inhibit-read-only t)
10981          (default gnus-summary-default-score)
10982          (default-high gnus-summary-default-high-score)
10983          (default-low gnus-summary-default-low-score)
10984          (uncached (memq article gnus-newsgroup-undownloaded))
10985          (downloaded (not uncached)))
10986     (let ((face (funcall (gnus-summary-highlight-line-0))))
10987       (unless (eq face (get-text-property beg 'face))
10988         (gnus-put-text-property-excluding-characters-with-faces
10989          beg (gnus-point-at-eol) 'face
10990          (setq face (if (boundp face) (symbol-value face) face)))
10991         (when gnus-summary-highlight-line-function
10992           (funcall gnus-summary-highlight-line-function article face))))))
10993
10994 (defun gnus-update-read-articles (group unread &optional compute)
10995   "Update the list of read articles in GROUP.
10996 UNREAD is a sorted list."
10997   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10998          (entry (gnus-gethash group gnus-newsrc-hashtb))
10999          (info (nth 2 entry))
11000          (prev 1)
11001          read)
11002     (if (or (not info) (not active))
11003         ;; There is no info on this group if it was, in fact,
11004         ;; killed.  Gnus stores no information on killed groups, so
11005         ;; there's nothing to be done.
11006         ;; One could store the information somewhere temporarily,
11007         ;; perhaps...  Hmmm...
11008         ()
11009       ;; Remove any negative articles numbers.
11010       (while (and unread (< (car unread) 0))
11011         (setq unread (cdr unread)))
11012       ;; Remove any expired article numbers
11013       (while (and unread (< (car unread) (car active)))
11014         (setq unread (cdr unread)))
11015       ;; Compute the ranges of read articles by looking at the list of
11016       ;; unread articles.
11017       (while unread
11018         (when (/= (car unread) prev)
11019           (push (if (= prev (1- (car unread))) prev
11020                   (cons prev (1- (car unread))))
11021                 read))
11022         (setq prev (1+ (car unread)))
11023         (setq unread (cdr unread)))
11024       (when (<= prev (cdr active))
11025         (push (cons prev (cdr active)) read))
11026       (setq read (if (> (length read) 1) (nreverse read) read))
11027       (if compute
11028           read
11029         (save-excursion
11030           (let (setmarkundo)
11031             ;; Propagate the read marks to the backend.
11032             (when (gnus-check-backend-function 'request-set-mark group)
11033               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11034                     (add (gnus-remove-from-range read (gnus-info-read info))))
11035                 (when (or add del)
11036                   (unless (gnus-check-group group)
11037                     (error "Can't open server for %s" group))
11038                   (gnus-request-set-mark
11039                    group (delq nil (list (if add (list add 'add '(read)))
11040                                          (if del (list del 'del '(read))))))
11041                   (setq setmarkundo
11042                         `(gnus-request-set-mark
11043                           ,group
11044                           ',(delq nil (list
11045                                        (if del (list del 'add '(read)))
11046                                        (if add (list add 'del '(read))))))))))
11047             (set-buffer gnus-group-buffer)
11048             (gnus-undo-register
11049               `(progn
11050                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11051                  (gnus-info-set-read ',info ',(gnus-info-read info))
11052                  (gnus-get-unread-articles-in-group ',info
11053                                                     (gnus-active ,group))
11054                  (gnus-group-update-group ,group t)
11055                  ,setmarkundo))))
11056         ;; Enter this list into the group info.
11057         (gnus-info-set-read info read)
11058         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11059         (gnus-get-unread-articles-in-group info (gnus-active group))
11060         t))))
11061
11062 (defun gnus-offer-save-summaries ()
11063   "Offer to save all active summary buffers."
11064   (let (buffers)
11065     ;; Go through all buffers and find all summaries.
11066     (dolist (buffer (buffer-list))
11067       (when (and (setq buffer (buffer-name buffer))
11068                  (string-match "Summary" buffer)
11069                  (save-excursion
11070                    (set-buffer buffer)
11071                    ;; We check that this is, indeed, a summary buffer.
11072                    (and (eq major-mode 'gnus-summary-mode)
11073                         ;; Also make sure this isn't bogus.
11074                         gnus-newsgroup-prepared
11075                         ;; Also make sure that this isn't a
11076                         ;; dead summary buffer.
11077                         (not gnus-dead-summary-mode))))
11078         (push buffer buffers)))
11079     ;; Go through all these summary buffers and offer to save them.
11080     (when buffers
11081       (save-excursion
11082         (map-y-or-n-p
11083          "Update summary buffer %s? "
11084          (lambda (buf)
11085            (switch-to-buffer buf)
11086            (gnus-summary-exit))
11087          buffers)))))
11088
11089 (defun gnus-summary-setup-default-charset ()
11090   "Setup newsgroup default charset."
11091   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11092       (setq gnus-newsgroup-charset nil)
11093     (let* ((ignored-charsets
11094             (or gnus-newsgroup-ephemeral-ignored-charsets
11095                 (append
11096                  (and gnus-newsgroup-name
11097                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11098                  gnus-newsgroup-ignored-charsets))))
11099       (setq gnus-newsgroup-charset
11100             (or gnus-newsgroup-ephemeral-charset
11101                 (and gnus-newsgroup-name
11102                      (gnus-parameter-charset gnus-newsgroup-name))
11103                 gnus-default-charset))
11104       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11105            ignored-charsets))))
11106
11107 ;;;
11108 ;;; Mime Commands
11109 ;;;
11110
11111 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11112   "Display the current article buffer fully MIME-buttonized.
11113 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11114 treated as multipart/mixed."
11115   (interactive "P")
11116   (require 'gnus-art)
11117   (let ((gnus-unbuttonized-mime-types nil)
11118         (gnus-mime-display-multipart-as-mixed show-all-parts))
11119     (gnus-summary-show-article)))
11120
11121 (defun gnus-summary-repair-multipart (article)
11122   "Add a Content-Type header to a multipart article without one."
11123   (interactive (list (gnus-summary-article-number)))
11124   (gnus-with-article article
11125     (message-narrow-to-head)
11126     (message-remove-header "Mime-Version")
11127     (goto-char (point-max))
11128     (insert "Mime-Version: 1.0\n")
11129     (widen)
11130     (when (search-forward "\n--" nil t)
11131       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11132         (message-narrow-to-head)
11133         (message-remove-header "Content-Type")
11134         (goto-char (point-max))
11135         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11136                         separator))
11137         (widen))))
11138   (let (gnus-mark-article-hook)
11139     (gnus-summary-select-article t t nil article)))
11140
11141 (defun gnus-summary-toggle-display-buttonized ()
11142   "Toggle the buttonizing of the article buffer."
11143   (interactive)
11144   (require 'gnus-art)
11145   (if (setq gnus-inhibit-mime-unbuttonizing
11146             (not gnus-inhibit-mime-unbuttonizing))
11147       (let ((gnus-unbuttonized-mime-types nil))
11148         (gnus-summary-show-article))
11149     (gnus-summary-show-article)))
11150
11151 ;;;
11152 ;;; Generic summary marking commands
11153 ;;;
11154
11155 (defvar gnus-summary-marking-alist
11156   '((read gnus-del-mark "d")
11157     (unread gnus-unread-mark "u")
11158     (ticked gnus-ticked-mark "!")
11159     (dormant gnus-dormant-mark "?")
11160     (expirable gnus-expirable-mark "e"))
11161   "An alist of names/marks/keystrokes.")
11162
11163 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11164 (defvar gnus-summary-mark-map)
11165
11166 (defun gnus-summary-make-all-marking-commands ()
11167   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11168   (dolist (elem gnus-summary-marking-alist)
11169     (apply 'gnus-summary-make-marking-command elem)))
11170
11171 (defun gnus-summary-make-marking-command (name mark keystroke)
11172   (let ((map (make-sparse-keymap)))
11173     (define-key gnus-summary-generic-mark-map keystroke map)
11174     (dolist (lway `((next "next" next nil "n")
11175                     (next-unread "next unread" next t "N")
11176                     (prev "previous" prev nil "p")
11177                     (prev-unread "previous unread" prev t "P")
11178                     (nomove "" nil nil ,keystroke)))
11179       (let ((func (gnus-summary-make-marking-command-1
11180                    mark (car lway) lway name)))
11181         (setq func (eval func))
11182         (define-key map (nth 4 lway) func)))))
11183
11184 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11185   `(defun ,(intern
11186             (format "gnus-summary-put-mark-as-%s%s"
11187                     name (if (eq way 'nomove)
11188                              ""
11189                            (concat "-" (symbol-name way)))))
11190      (n)
11191      ,(format
11192        "Mark the current article as %s%s.
11193 If N, the prefix, then repeat N times.
11194 If N is negative, move in reverse order.
11195 The difference between N and the actual number of articles marked is
11196 returned."
11197        name (cadr lway))
11198      (interactive "p")
11199      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11200
11201 (defun gnus-summary-generic-mark (n mark move unread)
11202   "Mark N articles with MARK."
11203   (unless (eq major-mode 'gnus-summary-mode)
11204     (error "This command can only be used in the summary buffer"))
11205   (gnus-summary-show-thread)
11206   (let ((nummove
11207          (cond
11208           ((eq move 'next) 1)
11209           ((eq move 'prev) -1)
11210           (t 0))))
11211     (if (zerop nummove)
11212         (setq n 1)
11213       (when (< n 0)
11214         (setq n (abs n)
11215               nummove (* -1 nummove))))
11216     (while (and (> n 0)
11217                 (gnus-summary-mark-article nil mark)
11218                 (zerop (gnus-summary-next-subject nummove unread t)))
11219       (setq n (1- n)))
11220     (when (/= 0 n)
11221       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11222     (gnus-summary-recenter)
11223     (gnus-summary-position-point)
11224     (gnus-set-mode-line 'summary)
11225     n))
11226
11227 (defun gnus-summary-insert-articles (articles)
11228   (when (setq articles
11229               (gnus-sorted-difference articles
11230                                       (mapcar (lambda (h)
11231                                                 (mail-header-number h))
11232                                               gnus-newsgroup-headers)))
11233     (setq gnus-newsgroup-headers
11234           (merge 'list
11235                  gnus-newsgroup-headers
11236                  (gnus-fetch-headers articles)
11237                  'gnus-article-sort-by-number))
11238     ;; Suppress duplicates?
11239     (when gnus-suppress-duplicates
11240       (gnus-dup-suppress-articles))
11241
11242     ;; We might want to build some more threads first.
11243     (when (and gnus-fetch-old-headers
11244                (eq gnus-headers-retrieved-by 'nov))
11245       (if (eq gnus-fetch-old-headers 'invisible)
11246           (gnus-build-all-threads)
11247         (gnus-build-old-threads)))
11248     ;; Let the Gnus agent mark articles as read.
11249     (when gnus-agent
11250       (gnus-agent-get-undownloaded-list))
11251     ;; Remove list identifiers from subject
11252     (when gnus-list-identifiers
11253       (gnus-summary-remove-list-identifiers))
11254     ;; First and last article in this newsgroup.
11255     (when gnus-newsgroup-headers
11256       (setq gnus-newsgroup-begin
11257             (mail-header-number (car gnus-newsgroup-headers))
11258             gnus-newsgroup-end
11259             (mail-header-number
11260              (gnus-last-element gnus-newsgroup-headers))))
11261     (when gnus-use-scoring
11262       (gnus-possibly-score-headers))))
11263
11264 (defun gnus-summary-insert-old-articles (&optional all)
11265   "Insert all old articles in this group.
11266 If ALL is non-nil, already read articles become readable.
11267 If ALL is a number, fetch this number of articles."
11268   (interactive "P")
11269   (prog1
11270       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11271             older len)
11272         (setq older
11273               ;; Some nntp servers lie about their active range.  When
11274               ;; this happens, the active range can be in the millions.
11275               ;; Use a compressed range to avoid creating a huge list.
11276               (gnus-range-difference (list gnus-newsgroup-active) old))
11277         (setq len (gnus-range-length older))
11278         (cond
11279          ((null older) nil)
11280          ((numberp all)
11281           (if (< all len)
11282               (let ((older-range (nreverse older)))
11283                 (setq older nil)
11284
11285                 (while (> all 0)
11286                   (let* ((r (pop older-range))
11287                          (min (if (numberp r) r (car r)))
11288                          (max (if (numberp r) r (cdr r))))
11289                     (while (and (<= min max)
11290                                 (> all 0))
11291                       (push max older)
11292                       (setq all (1- all)
11293                             max (1- max))))))
11294             (setq older (gnus-uncompress-range older))))
11295          (all
11296           (setq older (gnus-uncompress-range older)))
11297          (t
11298           (when (and (numberp gnus-large-newsgroup)
11299                    (> len gnus-large-newsgroup))
11300               (let* ((cursor-in-echo-area nil)
11301                      (initial (gnus-parameter-large-newsgroup-initial
11302                                gnus-newsgroup-name))
11303                      (input
11304                       (read-string
11305                        (format
11306                         "How many articles from %s (%s %d): "
11307                         (gnus-limit-string
11308                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11309                         (if initial "max" "default")
11310                         len)
11311                        (if initial
11312                            (cons (number-to-string initial)
11313                                  0)))))
11314                 (unless (string-match "^[ \t]*$" input)
11315                   (setq all (string-to-number input))
11316                   (if (< all len)
11317                       (let ((older-range (nreverse older)))
11318                         (setq older nil)
11319
11320                         (while (> all 0)
11321                           (let* ((r (pop older-range))
11322                                  (min (if (numberp r) r (car r)))
11323                                  (max (if (numberp r) r (cdr r))))
11324                             (while (and (<= min max)
11325                                         (> all 0))
11326                               (push max older)
11327                               (setq all (1- all)
11328                                     max (1- max))))))))))
11329           (setq older (gnus-uncompress-range older))))
11330         (if (not older)
11331             (message "No old news.")
11332           (gnus-summary-insert-articles older)
11333           (gnus-summary-limit (gnus-sorted-nunion old older))))
11334     (gnus-summary-position-point)))
11335
11336 (defun gnus-summary-insert-new-articles ()
11337   "Insert all new articles in this group."
11338   (interactive)
11339   (prog1
11340       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11341             (old-active gnus-newsgroup-active)
11342             (nnmail-fetched-sources (list t))
11343             i new)
11344         (setq gnus-newsgroup-active
11345               (gnus-activate-group gnus-newsgroup-name 'scan))
11346         (setq i (cdr gnus-newsgroup-active))
11347         (while (> i (cdr old-active))
11348           (push i new)
11349           (decf i))
11350         (if (not new)
11351             (message "No gnus is bad news.")
11352           (gnus-summary-insert-articles new)
11353           (setq gnus-newsgroup-unreads
11354                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11355           (gnus-summary-limit (gnus-sorted-nunion old new))))
11356     (gnus-summary-position-point)))
11357
11358 (gnus-summary-make-all-marking-commands)
11359
11360 (gnus-ems-redefine)
11361
11362 (provide 'gnus-sum)
11363
11364 (run-hooks 'gnus-sum-load-hook)
11365
11366 ;;; gnus-sum.el ends here