(gnus-summary-read-group-1): Don't select first
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-make-false-root-always t
113   "Always make a false dummy root."
114   :group 'gnus-thread
115   :type 'boolean)
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include:
145 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
146 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 \(they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-default-high-score 0
211   "*Default threshold for a high scored article.
212 An article will be highlighted as high scored if its score is greater
213 than this score."
214   :group 'gnus-score-default
215   :type 'integer)
216
217 (defcustom gnus-summary-default-low-score 0
218   "*Default threshold for a low scored article.
219 An article will be highlighted as low scored if its score is smaller
220 than this score."
221   :group 'gnus-score-default
222   :type 'integer)
223
224 (defcustom gnus-summary-zcore-fuzz 0
225   "*Fuzziness factor for the zcore in the summary buffer.
226 Articles with scores closer than this to `gnus-summary-default-score'
227 will not be marked."
228   :group 'gnus-summary-format
229   :type 'integer)
230
231 (defcustom gnus-simplify-subject-fuzzy-regexp nil
232   "*Strings to be removed when doing fuzzy matches.
233 This can either be a regular expression or list of regular expressions
234 that will be removed from subject strings if fuzzy subject
235 simplification is selected."
236   :group 'gnus-thread
237   :type '(repeat regexp))
238
239 (defcustom gnus-show-threads t
240   "*If non-nil, display threads in summary mode."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-subtree nil
245   "*If non-nil, hide all threads initially.
246 This can be a predicate specifier which says which threads to hide.
247 If threads are hidden, you have to run the command
248 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
249 to expose hidden threads."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-hide-killed t
254   "*If non-nil, hide killed threads automatically."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-ignore-subject t
259   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
260 If nil, articles that have different subjects from their parents will
261 start separate threads."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-operation-ignore-subject t
266   "*If non-nil, subjects will be ignored when doing thread commands.
267 This affects commands like `gnus-summary-kill-thread' and
268 `gnus-summary-lower-thread'.
269
270 If this variable is nil, articles in the same thread with different
271 subjects will not be included in the operation in question.  If this
272 variable is `fuzzy', only articles that have subjects that are fuzzily
273 equal will be included."
274   :group 'gnus-thread
275   :type '(choice (const :tag "off" nil)
276                  (const fuzzy)
277                  (sexp :tag "on" t)))
278
279 (defcustom gnus-thread-indent-level 4
280   "*Number that says how much each sub-thread should be indented."
281   :group 'gnus-thread
282   :type 'integer)
283
284 (defcustom gnus-auto-extend-newsgroup t
285   "*If non-nil, extend newsgroup forward and backward when requested."
286   :group 'gnus-summary-choose
287   :type 'boolean)
288
289 (defcustom gnus-auto-select-first t
290   "*If non-nil, select the article under point.
291 Which article this is is controlled by the `gnus-auto-select-subject'
292 variable.
293
294 If you want to prevent automatic selection of articles in some
295 newsgroups, set the variable to nil in `gnus-select-group-hook'."
296   :group 'gnus-group-select
297   :type '(choice (const :tag "none" nil)
298                  (sexp :menu-tag "first" t)))
299
300 (defcustom gnus-auto-select-subject 'unread
301   "*Says what subject to place under point when entering a group.
302
303 This variable can either be the symbols `first' (place point on the
304 first subject), `unread' (place point on the subject line of the first
305 unread article), `best' (place point on the subject line of the
306 higest-scored article), `unseen' (place point on the subject line of
307 the first unseen article), 'unseen-or-unread' (place point on the subject
308 line of the first unseen article or, if all article have been seen, on the
309 subject line of the first unread article), or a function to be called to
310 place point on some subject line."
311   :group 'gnus-group-select
312   :type '(choice (const best)
313                  (const unread)
314                  (const first)
315                  (const unseen)
316                  (const unseen-or-unread)))
317
318 (defcustom gnus-auto-select-next t
319   "*If non-nil, offer to go to the next group from the end of the previous.
320 If the value is t and the next newsgroup is empty, Gnus will exit
321 summary mode and go back to group mode.  If the value is neither nil
322 nor t, Gnus will select the following unread newsgroup.  In
323 particular, if the value is the symbol `quietly', the next unread
324 newsgroup will be selected without any confirmation, and if it is
325 `almost-quietly', the next group will be selected without any
326 confirmation if you are located on the last article in the group.
327 Finally, if this variable is `slightly-quietly', the `Z n' command
328 will go to the next group without confirmation."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "off" nil)
331                  (const quietly)
332                  (const almost-quietly)
333                  (const slightly-quietly)
334                  (sexp :menu-tag "on" t)))
335
336 (defcustom gnus-auto-select-same nil
337   "*If non-nil, select the next article with the same subject.
338 If there are no more articles with the same subject, go to
339 the first unread article."
340   :group 'gnus-summary-maneuvering
341   :type 'boolean)
342
343 (defcustom gnus-summary-check-current nil
344   "*If non-nil, consider the current article when moving.
345 The \"unread\" movement commands will stay on the same line if the
346 current article is unread."
347   :group 'gnus-summary-maneuvering
348   :type 'boolean)
349
350 (defcustom gnus-auto-center-summary t
351   "*If non-nil, always center the current summary buffer.
352 In particular, if `vertical' do only vertical recentering.  If non-nil
353 and non-`vertical', do both horizontal and vertical recentering."
354   :group 'gnus-summary-maneuvering
355   :type '(choice (const :tag "none" nil)
356                  (const vertical)
357                  (integer :tag "height")
358                  (sexp :menu-tag "both" t)))
359
360 (defcustom gnus-show-all-headers nil
361   "*If non-nil, don't hide any headers."
362   :group 'gnus-article-hiding
363   :group 'gnus-article-headers
364   :type 'boolean)
365
366 (defcustom gnus-summary-ignore-duplicates nil
367   "*If non-nil, ignore articles with identical Message-ID headers."
368   :group 'gnus-summary
369   :type 'boolean)
370
371 (defcustom gnus-single-article-buffer t
372   "*If non-nil, display all articles in the same buffer.
373 If nil, each group will get its own article buffer."
374   :group 'gnus-article-various
375   :type 'boolean)
376
377 (defcustom gnus-break-pages t
378   "*If non-nil, do page breaking on articles.
379 The page delimiter is specified by the `gnus-page-delimiter'
380 variable."
381   :group 'gnus-article-various
382   :type 'boolean)
383
384 (defcustom gnus-move-split-methods nil
385   "*Variable used to suggest where articles are to be moved to.
386 It uses the same syntax as the `gnus-split-methods' variable.
387 However, whereas `gnus-split-methods' specifies file names as targets,
388 this variable specifies group names."
389   :group 'gnus-summary-mail
390   :type '(repeat (choice (list :value (fun) function)
391                          (cons :value ("" "") regexp (repeat string))
392                          (sexp :value nil))))
393
394 (defcustom gnus-unread-mark ?           ;Whitespace
395   "*Mark used for unread articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-ticked-mark ?!
400   "*Mark used for ticked articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-dormant-mark ??
405   "*Mark used for dormant articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-del-mark ?r
410   "*Mark used for del'd articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-read-mark ?R
415   "*Mark used for read articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-expirable-mark ?E
420   "*Mark used for expirable articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-killed-mark ?K
425   "*Mark used for killed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-spam-mark ?H
430   "*Mark used for spam articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-souped-mark ?F
435   "*Mark used for souped articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-kill-file-mark ?X
440   "*Mark used for articles killed by kill files."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-low-score-mark ?Y
445   "*Mark used for articles with a low score."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-catchup-mark ?C
450   "*Mark used for articles that are caught up."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-replied-mark ?A
455   "*Mark used for articles that have been replied to."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-forwarded-mark ?F
460   "*Mark used for articles that have been forwarded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-recent-mark ?N
465   "*Mark used for articles that are recent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-cached-mark ?*
470   "*Mark used for articles that are in the cache."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-saved-mark ?S
475   "*Mark used for articles that have been saved."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unseen-mark ?.
480   "*Mark used for articles that haven't been seen."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-no-mark ?               ;Whitespace
485   "*Mark used for articles that have no other secondary mark."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-ancient-mark ?O
490   "*Mark used for ancient articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-sparse-mark ?Q
495   "*Mark used for sparsely reffed articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-canceled-mark ?G
500   "*Mark used for canceled articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-duplicate-mark ?M
505   "*Mark used for duplicate articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-undownloaded-mark ?-
510   "*Mark used for articles that weren't downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-downloaded-mark ?+
515   "*Mark used for articles that were downloaded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-downloadable-mark ?%
520   "*Mark used for articles that are to be downloaded."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-unsendable-mark ?=
525   "*Mark used for articles that won't be sent."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-score-over-mark ?+
530   "*Score mark used for articles with high scores."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-score-below-mark ?-
535   "*Score mark used for articles with low scores."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-empty-thread-mark ?     ;Whitespace
540   "*There is no thread under the article."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-not-empty-thread-mark ?=
545   "*There is a thread under the article."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-view-pseudo-asynchronously nil
550   "*If non-nil, Gnus will view pseudo-articles asynchronously."
551   :group 'gnus-extract-view
552   :type 'boolean)
553
554 (defcustom gnus-auto-expirable-marks
555   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
556         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
557         gnus-souped-mark gnus-duplicate-mark)
558   "*The list of marks converted into expiration if a group is auto-expirable."
559   :version "21.1"
560   :group 'gnus-summary
561   :type '(repeat character))
562
563 (defcustom gnus-inhibit-user-auto-expire t
564   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
565   :version "21.1"
566   :group 'gnus-summary
567   :type 'boolean)
568
569 (defcustom gnus-view-pseudos nil
570   "*If `automatic', pseudo-articles will be viewed automatically.
571 If `not-confirm', pseudos will be viewed automatically, and the user
572 will not be asked to confirm the command."
573   :group 'gnus-extract-view
574   :type '(choice (const :tag "off" nil)
575                  (const automatic)
576                  (const not-confirm)))
577
578 (defcustom gnus-view-pseudos-separately t
579   "*If non-nil, one pseudo-article will be created for each file to be viewed.
580 If nil, all files that use the same viewing command will be given as a
581 list of parameters to that command."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-insert-pseudo-articles t
586   "*If non-nil, insert pseudo-articles when decoding articles."
587   :group 'gnus-extract-view
588   :type 'boolean)
589
590 (defcustom gnus-summary-dummy-line-format
591   "   %(:                             :%) %S\n"
592   "*The format specification for the dummy roots in the summary buffer.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions.
595
596 %S  The subject
597
598 General format specifiers can also be used.
599 See `(gnus)Formatting Variables'."
600   :link '(custom-manual "(gnus)Formatting Variables")
601   :group 'gnus-threading
602   :type 'string)
603
604 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
605   "*The format specification for the summary mode line.
606 It works along the same lines as a normal formatting string,
607 with some simple extensions:
608
609 %G  Group name
610 %p  Unprefixed group name
611 %A  Current article number
612 %z  Current article score
613 %V  Gnus version
614 %U  Number of unread articles in the group
615 %e  Number of unselected articles in the group
616 %Z  A string with unread/unselected article counts
617 %g  Shortish group name
618 %S  Subject of the current article
619 %u  User-defined spec
620 %s  Current score file name
621 %d  Number of dormant articles
622 %r  Number of articles that have been marked as read in this session
623 %E  Number of articles expunged by the score files"
624   :group 'gnus-summary-format
625   :type 'string)
626
627 (defcustom gnus-list-identifiers nil
628   "Regexp that matches list identifiers to be removed from subject.
629 This can also be a list of regexps."
630   :version "21.1"
631   :group 'gnus-summary-format
632   :group 'gnus-article-hiding
633   :type '(choice (const :tag "none" nil)
634                  (regexp :value ".*")
635                  (repeat :value (".*") regexp)))
636
637 (defcustom gnus-summary-mark-below 0
638   "*Mark all articles with a score below this variable as read.
639 This variable is local to each summary buffer and usually set by the
640 score file."
641   :group 'gnus-score-default
642   :type 'integer)
643
644 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
645   "*List of functions used for sorting articles in the summary buffer.
646
647 Each function takes two articles and returns non-nil if the first
648 article should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-article-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-article-sort-by-number',
657 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
658 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
659 and `gnus-article-sort-by-score'.
660
661 When threading is turned on, the variable `gnus-thread-sort-functions'
662 controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-article-sort-by-number)
665                          (function-item gnus-article-sort-by-author)
666                          (function-item gnus-article-sort-by-subject)
667                          (function-item gnus-article-sort-by-date)
668                          (function-item gnus-article-sort-by-score)
669                          (function-item gnus-article-sort-by-random)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
673   "*List of functions used for sorting threads in the summary buffer.
674 By default, threads are sorted by article number.
675
676 Each function takes two threads and returns non-nil if the first
677 thread should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-thread-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-thread-sort-by-number',
686 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
687 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
688 `gnus-thread-sort-by-most-recent-number',
689 `gnus-thread-sort-by-most-recent-date',
690 `gnus-thread-sort-by-random', and
691 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
692
693 When threading is turned off, the variable
694 `gnus-article-sort-functions' controls how articles are sorted."
695   :group 'gnus-summary-sort
696   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
697                          (function-item gnus-thread-sort-by-author)
698                          (function-item gnus-thread-sort-by-subject)
699                          (function-item gnus-thread-sort-by-date)
700                          (function-item gnus-thread-sort-by-score)
701                          (function-item gnus-thread-sort-by-total-score)
702                          (function-item gnus-thread-sort-by-random)
703                          (function :tag "other"))))
704
705 (defcustom gnus-thread-score-function '+
706   "*Function used for calculating the total score of a thread.
707
708 The function is called with the scores of the article and each
709 subthread and should then return the score of the thread.
710
711 Some functions you can use are `+', `max', or `min'."
712   :group 'gnus-summary-sort
713   :type 'function)
714
715 (defcustom gnus-summary-expunge-below nil
716   "All articles that have a score less than this variable will be expunged.
717 This variable is local to the summary buffers."
718   :group 'gnus-score-default
719   :type '(choice (const :tag "off" nil)
720                  integer))
721
722 (defcustom gnus-thread-expunge-below nil
723   "All threads that have a total score less than this variable will be expunged.
724 See `gnus-thread-score-function' for en explanation of what a
725 \"thread score\" is.
726
727 This variable is local to the summary buffers."
728   :group 'gnus-threading
729   :group 'gnus-score-default
730   :type '(choice (const :tag "off" nil)
731                  integer))
732
733 (defcustom gnus-summary-mode-hook nil
734   "*A hook for Gnus summary mode.
735 This hook is run before any variables are set in the summary buffer."
736   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
737   :group 'gnus-summary-various
738   :type 'hook)
739
740 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
741 (when (featurep 'xemacs)
742   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
743   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
744   (add-hook 'gnus-summary-mode-hook
745             'gnus-xmas-switch-horizontal-scrollbar-off))
746
747 (defcustom gnus-summary-menu-hook nil
748   "*Hook run after the creation of the summary mode menu."
749   :group 'gnus-summary-visual
750   :type 'hook)
751
752 (defcustom gnus-summary-exit-hook nil
753   "*A hook called on exit from the summary buffer.
754 It will be called with point in the group buffer."
755   :group 'gnus-summary-exit
756   :type 'hook)
757
758 (defcustom gnus-summary-prepare-hook nil
759   "*A hook called after the summary buffer has been generated.
760 If you want to modify the summary buffer, you can use this hook."
761   :group 'gnus-summary-various
762   :type 'hook)
763
764 (defcustom gnus-summary-prepared-hook nil
765   "*A hook called as the last thing after the summary buffer has been generated."
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 (defcustom gnus-summary-generate-hook nil
770   "*A hook run just before generating the summary buffer.
771 This hook is commonly used to customize threading variables and the
772 like."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-select-group-hook nil
777   "*A hook called when a newsgroup is selected.
778
779 If you'd like to simplify subjects like the
780 `gnus-summary-next-same-subject' command does, you can use the
781 following hook:
782
783  (add-hook gnus-select-group-hook
784            (lambda ()
785              (mapcar (lambda (header)
786                        (mail-header-set-subject
787                         header
788                         (gnus-simplify-subject
789                          (mail-header-subject header) 're-only)))
790                      gnus-newsgroup-headers)))"
791   :group 'gnus-group-select
792   :type 'hook)
793
794 (defcustom gnus-select-article-hook nil
795   "*A hook called when an article is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-visual-mark-article-hook
800   (list 'gnus-highlight-selected-summary)
801   "*Hook run after selecting an article in the summary buffer.
802 It is meant to be used for highlighting the article in some way.  It
803 is not run if `gnus-visual' is nil."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-parse-headers-hook nil
808   "*A hook called before parsing the headers."
809   :group 'gnus-various
810   :type 'hook)
811
812 (defcustom gnus-exit-group-hook nil
813   "*A hook called when exiting summary mode.
814 This hook is not called from the non-updating exit commands like `Q'."
815   :group 'gnus-various
816   :type 'hook)
817
818 (defcustom gnus-summary-update-hook
819   (list 'gnus-summary-highlight-line)
820   "*A hook called when a summary line is changed.
821 The hook will not be called if `gnus-visual' is nil.
822
823 The default function `gnus-summary-highlight-line' will
824 highlight the line according to the `gnus-summary-highlight'
825 variable."
826   :group 'gnus-summary-visual
827   :type 'hook)
828
829 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
830   "*A hook called when an article is selected for the first time.
831 The hook is intended to mark an article as read (or unread)
832 automatically when it is selected."
833   :group 'gnus-summary-choose
834   :type 'hook)
835
836 (defcustom gnus-group-no-more-groups-hook nil
837   "*A hook run when returning to group mode having no more (unread) groups."
838   :group 'gnus-group-select
839   :type 'hook)
840
841 (defcustom gnus-ps-print-hook nil
842   "*A hook run before ps-printing something from Gnus."
843   :group 'gnus-summary
844   :type 'hook)
845
846 (defcustom gnus-summary-display-arrow
847   (and (fboundp 'display-graphic-p)
848        (display-graphic-p))
849   "*If non-nil, display an arrow highlighting the current article."
850   :version "21.1"
851   :group 'gnus-summary
852   :type 'boolean)
853
854 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
855   "Face used for highlighting the current article in the summary buffer."
856   :group 'gnus-summary-visual
857   :type 'face)
858
859 (defvar gnus-tmp-downloaded nil)
860
861 (defcustom gnus-summary-highlight
862   '(((eq mark gnus-canceled-mark)
863      . gnus-summary-cancelled-face)
864     ((and uncached (> score default-high))
865      . gnus-summary-high-undownloaded-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-undownloaded-face)
868     (uncached
869      . gnus-summary-normal-undownloaded-face)
870     ((and (> score default-high)
871           (or (eq mark gnus-dormant-mark)
872               (eq mark gnus-ticked-mark)))
873      . gnus-summary-high-ticked-face)
874     ((and (< score default-low)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-low-ticked-face)
878     ((or (eq mark gnus-dormant-mark)
879          (eq mark gnus-ticked-mark))
880      . gnus-summary-normal-ticked-face)
881     ((and (> score default-high) (eq mark gnus-ancient-mark))
882      . gnus-summary-high-ancient-face)
883     ((and (< score default-low) (eq mark gnus-ancient-mark))
884      . gnus-summary-low-ancient-face)
885     ((eq mark gnus-ancient-mark)
886      . gnus-summary-normal-ancient-face)
887     ((and (> score default-high) (eq mark gnus-unread-mark))
888      . gnus-summary-high-unread-face)
889     ((and (< score default-low) (eq mark gnus-unread-mark))
890      . gnus-summary-low-unread-face)
891     ((eq mark gnus-unread-mark)
892      . gnus-summary-normal-unread-face)
893     ((> score default-high)
894      . gnus-summary-high-read-face)
895     ((< score default-low)
896      . gnus-summary-low-read-face)
897     (t
898      . gnus-summary-normal-read-face))
899   "*Controls the highlighting of summary buffer lines.
900
901 A list of (FORM . FACE) pairs.  When deciding how a a particular
902 summary line should be displayed, each form is evaluated.  The content
903 of the face field after the first true form is used.  You can change
904 how those summary lines are displayed, by editing the face field.
905
906 You can use the following variables in the FORM field.
907
908 score:        The article's score
909 default:      The default article score.
910 default-high: The default score for high scored articles.
911 default-low:  The default score for low scored articles.
912 below:        The score below which articles are automatically marked as read.
913 mark:         The articles mark."
914   :group 'gnus-summary-visual
915   :type '(repeat (cons (sexp :tag "Form" nil)
916                        face)))
917
918 (defcustom gnus-alter-header-function nil
919   "Function called to allow alteration of article header structures.
920 The function is called with one parameter, the article header vector,
921 which it may alter in any way.")
922
923 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
924   "Variable that says which function should be used to decode a string with encoded words.")
925
926 (defcustom gnus-extra-headers '(To Newsgroups)
927   "*Extra headers to parse."
928   :version "21.1"
929   :group 'gnus-summary
930   :type '(repeat symbol))
931
932 (defcustom gnus-ignored-from-addresses
933   (and user-mail-address (regexp-quote user-mail-address))
934   "*Regexp of From headers that may be suppressed in favor of To headers."
935   :version "21.1"
936   :group 'gnus-summary
937   :type 'regexp)
938
939 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
940   "List of charsets that should be ignored.
941 When these charsets are used in the \"charset\" parameter, the
942 default charset will be used instead."
943   :version "21.1"
944   :type '(repeat symbol)
945   :group 'gnus-charset)
946
947 (gnus-define-group-parameter
948  ignored-charsets
949  :type list
950  :function-document
951  "Return the ignored charsets of GROUP."
952  :variable gnus-group-ignored-charsets-alist
953  :variable-default
954  '(("alt\\.chinese\\.text" iso-8859-1))
955  :variable-document
956  "Alist of regexps (to match group names) and charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959  :variable-group gnus-charset
960  :variable-type '(repeat (cons (regexp :tag "Group")
961                                (repeat symbol)))
962  :parameter-type '(choice :tag "Ignored charsets"
963                           :value nil
964                           (repeat (symbol)))
965  :parameter-document       "\
966 List of charsets that should be ignored.
967
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead.")
970
971 (defcustom gnus-group-highlight-words-alist nil
972   "Alist of group regexps and highlight regexps.
973 This variable uses the same syntax as `gnus-emphasis-alist'."
974   :version "21.1"
975   :type '(repeat (cons (regexp :tag "Group")
976                        (repeat (list (regexp :tag "Highlight regexp")
977                                      (number :tag "Group for entire word" 0)
978                                      (number :tag "Group for displayed part" 0)
979                                      (symbol :tag "Face"
980                                              gnus-emphasis-highlight-words)))))
981   :group 'gnus-summary-visual)
982
983 (defcustom gnus-summary-show-article-charset-alist
984   nil
985   "Alist of number and charset.
986 The article will be shown with the charset corresponding to the
987 numbered argument.
988 For example: ((1 . cn-gb-2312) (2 . big5))."
989   :version "21.1"
990   :type '(repeat (cons (number :tag "Argument" 1)
991                        (symbol :tag "Charset")))
992   :group 'gnus-charset)
993
994 (defcustom gnus-preserve-marks t
995   "Whether marks are preserved when moving, copying and respooling messages."
996   :version "21.1"
997   :type 'boolean
998   :group 'gnus-summary-marks)
999
1000 (defcustom gnus-alter-articles-to-read-function nil
1001   "Function to be called to alter the list of articles to be selected."
1002   :type '(choice (const nil) function)
1003   :group 'gnus-summary)
1004
1005 (defcustom gnus-orphan-score nil
1006   "*All orphans get this score added.  Set in the score file."
1007   :group 'gnus-score-default
1008   :type '(choice (const nil)
1009                  integer))
1010
1011 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1012   "*A regexp to match MIME parts when saving multiple parts of a message
1013 with gnus-summary-save-parts (X m). This regexp will be used by default
1014 when prompting the user for which type of files to save."
1015   :group 'gnus-summary
1016   :type 'regexp)
1017
1018 (defcustom gnus-read-all-available-headers nil
1019   "Whether Gnus should parse all headers made available to it.
1020 This is mostly relevant for slow backends where the user may
1021 wish to widen the summary buffer to include all headers
1022 that were fetched.  Say, for nnultimate groups."
1023   :group 'gnus-summary
1024   :type '(choice boolean regexp))
1025
1026 (defcustom gnus-summary-muttprint-program "muttprint"
1027   "Command (and optional arguments) used to run Muttprint."
1028   :version "21.3"
1029   :group 'gnus-summary
1030   :type 'string)
1031
1032 (defcustom gnus-article-loose-mime nil
1033   "If non-nil, don't require MIME-Version header.
1034 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1035 supply the MIME-Version header or deliberately strip it From the mail.
1036 Set it to non-nil, Gnus will treat some articles as MIME even if
1037 the MIME-Version header is missed."
1038   :version "21.3"
1039   :type 'boolean
1040   :group 'gnus-article)
1041
1042 ;;; Internal variables
1043
1044 (defvar gnus-summary-display-cache nil)
1045 (defvar gnus-article-mime-handles nil)
1046 (defvar gnus-article-decoded-p nil)
1047 (defvar gnus-article-charset nil)
1048 (defvar gnus-article-ignored-charsets nil)
1049 (defvar gnus-scores-exclude-files nil)
1050 (defvar gnus-page-broken nil)
1051
1052 (defvar gnus-original-article nil)
1053 (defvar gnus-article-internal-prepare-hook nil)
1054 (defvar gnus-newsgroup-process-stack nil)
1055
1056 (defvar gnus-thread-indent-array nil)
1057 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1058 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1059   "Function called to sort the articles within a thread after it has been gathered together.")
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073 (defvar gnus-newsgroup-display nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1087         ?s)
1088     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1099     (?L gnus-tmp-lines ?s)
1100     (?O gnus-tmp-downloaded ?c)
1101     (?I gnus-tmp-indentation ?s)
1102     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1103     (?R gnus-tmp-replied ?c)
1104     (?\[ gnus-tmp-opening-bracket ?c)
1105     (?\] gnus-tmp-closing-bracket ?c)
1106     (?\> (make-string gnus-tmp-level ? ) ?s)
1107     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1108     (?i gnus-tmp-score ?d)
1109     (?z gnus-tmp-score-char ?c)
1110     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1111     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1112     (?U gnus-tmp-unread ?c)
1113     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1114         ?s)
1115     (?t (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1117         ?d)
1118     (?e (gnus-summary-number-of-articles-in-thread
1119          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1120         ?c)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?P (gnus-pick-line-number) ?d)
1123     (?B gnus-tmp-thread-tree-header-string ?s)
1124     (user-date (gnus-user-date
1125                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1126   "An alist of format specifications that can appear in summary lines.
1127 These are paired with what variables they correspond with, along with
1128 the type of the variable (string, integer, character, etc).")
1129
1130 (defvar gnus-summary-dummy-line-format-alist
1131   `((?S gnus-tmp-subject ?s)
1132     (?N gnus-tmp-number ?d)
1133     (?u gnus-tmp-user-defined ?s)))
1134
1135 (defvar gnus-summary-mode-line-format-alist
1136   `((?G gnus-tmp-group-name ?s)
1137     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1138     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1139     (?A gnus-tmp-article-number ?d)
1140     (?Z gnus-tmp-unread-and-unselected ?s)
1141     (?V gnus-version ?s)
1142     (?U gnus-tmp-unread-and-unticked ?d)
1143     (?S gnus-tmp-subject ?s)
1144     (?e gnus-tmp-unselected ?d)
1145     (?u gnus-tmp-user-defined ?s)
1146     (?d (length gnus-newsgroup-dormant) ?d)
1147     (?t (length gnus-newsgroup-marked) ?d)
1148     (?h (length gnus-newsgroup-spam-marked) ?d)
1149     (?r (length gnus-newsgroup-reads) ?d)
1150     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1151     (?E gnus-newsgroup-expunged-tally ?d)
1152     (?s (gnus-current-score-file-nondirectory) ?s)))
1153
1154 (defvar gnus-last-search-regexp nil
1155   "Default regexp for article search command.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-agentized nil
1161   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1162 (defvar gnus-newsgroup-begin nil)
1163 (defvar gnus-newsgroup-end nil)
1164 (defvar gnus-newsgroup-last-rmail nil)
1165 (defvar gnus-newsgroup-last-mail nil)
1166 (defvar gnus-newsgroup-last-folder nil)
1167 (defvar gnus-newsgroup-last-file nil)
1168 (defvar gnus-newsgroup-auto-expire nil)
1169 (defvar gnus-newsgroup-active nil)
1170
1171 (defvar gnus-newsgroup-data nil)
1172 (defvar gnus-newsgroup-data-reverse nil)
1173 (defvar gnus-newsgroup-limit nil)
1174 (defvar gnus-newsgroup-limits nil)
1175
1176 (defvar gnus-newsgroup-unreads nil
1177   "Sorted list of unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-unselected nil
1180   "Sorted list of unselected unread articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-reads nil
1183   "Alist of read articles and article marks in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-expunged-tally nil)
1186
1187 (defvar gnus-newsgroup-marked nil
1188   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1189
1190 (defvar gnus-newsgroup-spam-marked nil
1191   "List of ranges of articles that have been marked as spam.")
1192
1193 (defvar gnus-newsgroup-killed nil
1194   "List of ranges of articles that have been through the scoring process.")
1195
1196 (defvar gnus-newsgroup-cached nil
1197   "Sorted list of articles that come from the article cache.")
1198
1199 (defvar gnus-newsgroup-saved nil
1200   "List of articles that have been saved.")
1201
1202 (defvar gnus-newsgroup-kill-headers nil)
1203
1204 (defvar gnus-newsgroup-replied nil
1205   "List of articles that have been replied to in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-forwarded nil
1208   "List of articles that have been forwarded in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-recent nil
1211   "List of articles that have are recent in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-expirable nil
1214   "Sorted list of articles in the current newsgroup that can be expired.")
1215
1216 (defvar gnus-newsgroup-processable nil
1217   "List of articles in the current newsgroup that can be processed.")
1218
1219 (defvar gnus-newsgroup-downloadable nil
1220   "Sorted list of articles in the current newsgroup that can be processed.")
1221
1222 (defvar gnus-newsgroup-undownloaded nil
1223   "List of articles in the current newsgroup that haven't been downloaded..")
1224
1225 (defvar gnus-newsgroup-unsendable nil
1226   "List of articles in the current newsgroup that won't be sent.")
1227
1228 (defvar gnus-newsgroup-bookmarks nil
1229   "List of articles in the current newsgroup that have bookmarks.")
1230
1231 (defvar gnus-newsgroup-dormant nil
1232   "Sorted list of dormant articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-unseen nil
1235   "List of unseen articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-seen nil
1238   "Range of seen articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-articles nil
1241   "List of articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-scored nil
1244   "List of scored articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-headers nil
1247   "List of article headers in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-threads nil)
1250
1251 (defvar gnus-newsgroup-prepared nil
1252   "Whether the current group has been prepared properly.")
1253
1254 (defvar gnus-newsgroup-ancient nil
1255   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-sparse nil)
1258
1259 (defvar gnus-current-article nil)
1260 (defvar gnus-article-current nil)
1261 (defvar gnus-current-headers nil)
1262 (defvar gnus-have-all-headers nil)
1263 (defvar gnus-last-article nil)
1264 (defvar gnus-newsgroup-history nil)
1265 (defvar gnus-newsgroup-charset nil)
1266 (defvar gnus-newsgroup-ephemeral-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1268
1269 (defvar gnus-article-before-search nil)
1270
1271 (defconst gnus-summary-local-variables
1272   '(gnus-newsgroup-name
1273     gnus-newsgroup-begin gnus-newsgroup-end
1274     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1275     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1276     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1277     gnus-newsgroup-unselected gnus-newsgroup-marked
1278     gnus-newsgroup-spam-marked
1279     gnus-newsgroup-reads gnus-newsgroup-saved
1280     gnus-newsgroup-replied gnus-newsgroup-forwarded
1281     gnus-newsgroup-recent
1282     gnus-newsgroup-expirable
1283     gnus-newsgroup-processable gnus-newsgroup-killed
1284     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1285     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1286     gnus-newsgroup-seen gnus-newsgroup-articles
1287     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1288     gnus-newsgroup-headers gnus-newsgroup-threads
1289     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1290     gnus-current-article gnus-current-headers gnus-have-all-headers
1291     gnus-last-article gnus-article-internal-prepare-hook
1292     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1293     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1294     gnus-thread-expunge-below
1295     gnus-score-alist gnus-current-score-file
1296     (gnus-summary-expunge-below . global)
1297     (gnus-summary-mark-below . global)
1298     (gnus-orphan-score . global)
1299     gnus-newsgroup-active gnus-scores-exclude-files
1300     gnus-newsgroup-history gnus-newsgroup-ancient
1301     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1302     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1303     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1304     (gnus-newsgroup-expunged-tally . 0)
1305     gnus-cache-removable-articles gnus-newsgroup-cached
1306     gnus-newsgroup-data gnus-newsgroup-data-reverse
1307     gnus-newsgroup-limit gnus-newsgroup-limits
1308     gnus-newsgroup-charset gnus-newsgroup-display)
1309   "Variables that are buffer-local to the summary buffers.")
1310
1311 (defvar gnus-newsgroup-variables nil
1312   "A list of variables that have separate values in different newsgroups.
1313 A list of newsgroup (summary buffer) local variables, or cons of
1314 variables and their default values (when the default values are not
1315 nil), that should be made global while the summary buffer is active.
1316 These variables can be used to set variables in the group parameters
1317 while still allowing them to affect operations done in other
1318 buffers. For example:
1319
1320 \(setq gnus-newsgroup-variables
1321      '(message-use-followup-to
1322        (gnus-visible-headers .
1323          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1324 ")
1325
1326 ;; Byte-compiler warning.
1327 ;(eval-when-compile (defvar gnus-article-mode-map))
1328 (eval-when-compile
1329   (let ((features (cons 'gnus-sum features)))
1330     (require 'gnus)
1331     (require 'gnus-agent)
1332     (require 'gnus-art)))
1333
1334 ;; MIME stuff.
1335
1336 (defvar gnus-decode-encoded-word-methods
1337   '(mail-decode-encoded-word-string)
1338   "List of methods used to decode encoded words.
1339
1340 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1341 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1342 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1343 whose names match REGEXP.
1344
1345 For example:
1346 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1347  mail-decode-encoded-word-string
1348  (\"chinese\" . rfc1843-decode-string))")
1349
1350 (defvar gnus-decode-encoded-word-methods-cache nil)
1351
1352 (defun gnus-multi-decode-encoded-word-string (string)
1353   "Apply the functions from `gnus-encoded-word-methods' that match."
1354   (unless (and gnus-decode-encoded-word-methods-cache
1355                (eq gnus-newsgroup-name
1356                    (car gnus-decode-encoded-word-methods-cache)))
1357     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1358     (mapcar (lambda (x)
1359               (if (symbolp x)
1360                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1361                 (if (and gnus-newsgroup-name
1362                          (string-match (car x) gnus-newsgroup-name))
1363                     (nconc gnus-decode-encoded-word-methods-cache
1364                            (list (cdr x))))))
1365             gnus-decode-encoded-word-methods))
1366   (let ((xlist gnus-decode-encoded-word-methods-cache))
1367     (pop xlist)
1368     (while xlist
1369       (setq string (funcall (pop xlist) string))))
1370   string)
1371
1372 ;; Subject simplification.
1373
1374 (defun gnus-simplify-whitespace (str)
1375   "Remove excessive whitespace from STR."
1376   (let ((mystr str))
1377     ;; Multiple spaces.
1378     (while (string-match "[ \t][ \t]+" mystr)
1379       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1380                           " "
1381                           (substring mystr (match-end 0)))))
1382     ;; Leading spaces.
1383     (when (string-match "^[ \t]+" mystr)
1384       (setq mystr (substring mystr (match-end 0))))
1385     ;; Trailing spaces.
1386     (when (string-match "[ \t]+$" mystr)
1387       (setq mystr (substring mystr 0 (match-beginning 0))))
1388     mystr))
1389
1390 (defun gnus-simplify-all-whitespace (str)
1391   "Remove all whitespace from STR."
1392   (let ((mystr str))
1393     (while (string-match "[ \t\n]+" mystr)
1394       (setq mystr (replace-match "" nil nil mystr)))
1395     mystr))
1396
1397 (defsubst gnus-simplify-subject-re (subject)
1398   "Remove \"Re:\" from subject lines."
1399   (if (string-match message-subject-re-regexp subject)
1400       (substring subject (match-end 0))
1401     subject))
1402
1403 (defun gnus-simplify-subject (subject &optional re-only)
1404   "Remove `Re:' and words in parentheses.
1405 If RE-ONLY is non-nil, strip leading `Re:'s only."
1406   (let ((case-fold-search t))           ;Ignore case.
1407     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1408     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove uninteresting prefixes.
1411     (when (and (not re-only)
1412                gnus-simplify-ignored-prefixes
1413                (string-match gnus-simplify-ignored-prefixes subject))
1414       (setq subject (substring subject (match-end 0))))
1415     ;; Remove words in parentheses from end.
1416     (unless re-only
1417       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1418         (setq subject (substring subject 0 (match-beginning 0)))))
1419     ;; Return subject string.
1420     subject))
1421
1422 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1423 ;; all whitespace.
1424 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1425   (goto-char (point-min))
1426   (while (re-search-forward regexp nil t)
1427     (replace-match (or newtext ""))))
1428
1429 (defun gnus-simplify-buffer-fuzzy ()
1430   "Simplify string in the buffer fuzzily.
1431 The string in the accessible portion of the current buffer is simplified.
1432 It is assumed to be a single-line subject.
1433 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1434 matter is removed.  Additional things can be deleted by setting
1435 `gnus-simplify-subject-fuzzy-regexp'."
1436   (let ((case-fold-search t)
1437         (modified-tick))
1438     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1439
1440     (while (not (eq modified-tick (buffer-modified-tick)))
1441       (setq modified-tick (buffer-modified-tick))
1442       (cond
1443        ((listp gnus-simplify-subject-fuzzy-regexp)
1444         (mapcar 'gnus-simplify-buffer-fuzzy-step
1445                 gnus-simplify-subject-fuzzy-regexp))
1446        (gnus-simplify-subject-fuzzy-regexp
1447         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1448       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1449       (gnus-simplify-buffer-fuzzy-step
1450        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1451       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1452
1453     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1454     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1455     (gnus-simplify-buffer-fuzzy-step " $")
1456     (gnus-simplify-buffer-fuzzy-step "^ +")))
1457
1458 (defun gnus-simplify-subject-fuzzy (subject)
1459   "Simplify a subject string fuzzily.
1460 See `gnus-simplify-buffer-fuzzy' for details."
1461   (save-excursion
1462     (gnus-set-work-buffer)
1463     (let ((case-fold-search t))
1464       ;; Remove uninteresting prefixes.
1465       (when (and gnus-simplify-ignored-prefixes
1466                  (string-match gnus-simplify-ignored-prefixes subject))
1467         (setq subject (substring subject (match-end 0))))
1468       (insert subject)
1469       (inline (gnus-simplify-buffer-fuzzy))
1470       (buffer-string))))
1471
1472 (defsubst gnus-simplify-subject-fully (subject)
1473   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1474   (cond
1475    (gnus-simplify-subject-functions
1476     (gnus-map-function gnus-simplify-subject-functions subject))
1477    ((null gnus-summary-gather-subject-limit)
1478     (gnus-simplify-subject-re subject))
1479    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1480     (gnus-simplify-subject-fuzzy subject))
1481    ((numberp gnus-summary-gather-subject-limit)
1482     (gnus-limit-string (gnus-simplify-subject-re subject)
1483                        gnus-summary-gather-subject-limit))
1484    (t
1485     subject)))
1486
1487 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1488   "Check whether two subjects are equal.
1489 If optional argument simple-first is t, first argument is already
1490 simplified."
1491   (cond
1492    ((null simple-first)
1493     (equal (gnus-simplify-subject-fully s1)
1494            (gnus-simplify-subject-fully s2)))
1495    (t
1496     (equal s1
1497            (gnus-simplify-subject-fully s2)))))
1498
1499 (defun gnus-summary-bubble-group ()
1500   "Increase the score of the current group.
1501 This is a handy function to add to `gnus-summary-exit-hook' to
1502 increase the score of each group you read."
1503   (gnus-group-add-score gnus-newsgroup-name))
1504
1505 \f
1506 ;;;
1507 ;;; Gnus summary mode
1508 ;;;
1509
1510 (put 'gnus-summary-mode 'mode-class 'special)
1511
1512 (defvar gnus-article-commands-menu)
1513
1514 (when t
1515   ;; Non-orthogonal keys
1516
1517   (gnus-define-keys gnus-summary-mode-map
1518     " " gnus-summary-next-page
1519     "\177" gnus-summary-prev-page
1520     [delete] gnus-summary-prev-page
1521     [backspace] gnus-summary-prev-page
1522     "\r" gnus-summary-scroll-up
1523     "\M-\r" gnus-summary-scroll-down
1524     "n" gnus-summary-next-unread-article
1525     "p" gnus-summary-prev-unread-article
1526     "N" gnus-summary-next-article
1527     "P" gnus-summary-prev-article
1528     "\M-\C-n" gnus-summary-next-same-subject
1529     "\M-\C-p" gnus-summary-prev-same-subject
1530     "\M-n" gnus-summary-next-unread-subject
1531     "\M-p" gnus-summary-prev-unread-subject
1532     "." gnus-summary-first-unread-article
1533     "," gnus-summary-best-unread-article
1534     "\M-s" gnus-summary-search-article-forward
1535     "\M-r" gnus-summary-search-article-backward
1536     "<" gnus-summary-beginning-of-article
1537     ">" gnus-summary-end-of-article
1538     "j" gnus-summary-goto-article
1539     "^" gnus-summary-refer-parent-article
1540     "\M-^" gnus-summary-refer-article
1541     "u" gnus-summary-tick-article-forward
1542     "!" gnus-summary-tick-article-forward
1543     "U" gnus-summary-tick-article-backward
1544     "d" gnus-summary-mark-as-read-forward
1545     "D" gnus-summary-mark-as-read-backward
1546     "E" gnus-summary-mark-as-expirable
1547     "\M-u" gnus-summary-clear-mark-forward
1548     "\M-U" gnus-summary-clear-mark-backward
1549     "k" gnus-summary-kill-same-subject-and-select
1550     "\C-k" gnus-summary-kill-same-subject
1551     "\M-\C-k" gnus-summary-kill-thread
1552     "\M-\C-l" gnus-summary-lower-thread
1553     "e" gnus-summary-edit-article
1554     "#" gnus-summary-mark-as-processable
1555     "\M-#" gnus-summary-unmark-as-processable
1556     "\M-\C-t" gnus-summary-toggle-threads
1557     "\M-\C-s" gnus-summary-show-thread
1558     "\M-\C-h" gnus-summary-hide-thread
1559     "\M-\C-f" gnus-summary-next-thread
1560     "\M-\C-b" gnus-summary-prev-thread
1561     [(meta down)] gnus-summary-next-thread
1562     [(meta up)] gnus-summary-prev-thread
1563     "\M-\C-u" gnus-summary-up-thread
1564     "\M-\C-d" gnus-summary-down-thread
1565     "&" gnus-summary-execute-command
1566     "c" gnus-summary-catchup-and-exit
1567     "\C-w" gnus-summary-mark-region-as-read
1568     "\C-t" gnus-summary-toggle-truncation
1569     "?" gnus-summary-mark-as-dormant
1570     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1571     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1572     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1573     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1574     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1575     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1576     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1577     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1578     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1579     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1580     "=" gnus-summary-expand-window
1581     "\C-x\C-s" gnus-summary-reselect-current-group
1582     "\M-g" gnus-summary-rescan-group
1583     "w" gnus-summary-stop-page-breaking
1584     "\C-c\C-r" gnus-summary-caesar-message
1585     "f" gnus-summary-followup
1586     "F" gnus-summary-followup-with-original
1587     "C" gnus-summary-cancel-article
1588     "r" gnus-summary-reply
1589     "R" gnus-summary-reply-with-original
1590     "\C-c\C-f" gnus-summary-mail-forward
1591     "o" gnus-summary-save-article
1592     "\C-o" gnus-summary-save-article-mail
1593     "|" gnus-summary-pipe-output
1594     "\M-k" gnus-summary-edit-local-kill
1595     "\M-K" gnus-summary-edit-global-kill
1596     ;; "V" gnus-version
1597     "\C-c\C-d" gnus-summary-describe-group
1598     "q" gnus-summary-exit
1599     "Q" gnus-summary-exit-no-update
1600     "\C-c\C-i" gnus-info-find-node
1601     gnus-mouse-2 gnus-mouse-pick-article
1602     "m" gnus-summary-mail-other-window
1603     "a" gnus-summary-post-news
1604     "i" gnus-summary-news-other-window
1605     "x" gnus-summary-limit-to-unread
1606     "s" gnus-summary-isearch-article
1607     "t" gnus-summary-toggle-header
1608     "g" gnus-summary-show-article
1609     "l" gnus-summary-goto-last-article
1610     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1611     "\C-d" gnus-summary-enter-digest-group
1612     "\M-\C-d" gnus-summary-read-document
1613     "\M-\C-e" gnus-summary-edit-parameters
1614     "\M-\C-a" gnus-summary-customize-parameters
1615     "\C-c\C-b" gnus-bug
1616     "*" gnus-cache-enter-article
1617     "\M-*" gnus-cache-remove-article
1618     "\M-&" gnus-summary-universal-argument
1619     "\C-l" gnus-recenter
1620     "I" gnus-summary-increase-score
1621     "L" gnus-summary-lower-score
1622     "\M-i" gnus-symbolic-argument
1623     "h" gnus-summary-select-article-buffer
1624
1625     "b" gnus-article-view-part
1626     "\M-t" gnus-summary-toggle-display-buttonized
1627
1628     "V" gnus-summary-score-map
1629     "X" gnus-uu-extract-map
1630     "S" gnus-summary-send-map)
1631
1632   ;; Sort of orthogonal keymap
1633   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1634     "t" gnus-summary-tick-article-forward
1635     "!" gnus-summary-tick-article-forward
1636     "d" gnus-summary-mark-as-read-forward
1637     "r" gnus-summary-mark-as-read-forward
1638     "c" gnus-summary-clear-mark-forward
1639     " " gnus-summary-clear-mark-forward
1640     "e" gnus-summary-mark-as-expirable
1641     "x" gnus-summary-mark-as-expirable
1642     "?" gnus-summary-mark-as-dormant
1643     "b" gnus-summary-set-bookmark
1644     "B" gnus-summary-remove-bookmark
1645     "#" gnus-summary-mark-as-processable
1646     "\M-#" gnus-summary-unmark-as-processable
1647     "S" gnus-summary-limit-include-expunged
1648     "C" gnus-summary-catchup
1649     "H" gnus-summary-catchup-to-here
1650     "h" gnus-summary-catchup-from-here
1651     "\C-c" gnus-summary-catchup-all
1652     "k" gnus-summary-kill-same-subject-and-select
1653     "K" gnus-summary-kill-same-subject
1654     "P" gnus-uu-mark-map)
1655
1656   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1657     "c" gnus-summary-clear-above
1658     "u" gnus-summary-tick-above
1659     "m" gnus-summary-mark-above
1660     "k" gnus-summary-kill-below)
1661
1662   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1663     "/" gnus-summary-limit-to-subject
1664     "n" gnus-summary-limit-to-articles
1665     "w" gnus-summary-pop-limit
1666     "s" gnus-summary-limit-to-subject
1667     "a" gnus-summary-limit-to-author
1668     "u" gnus-summary-limit-to-unread
1669     "m" gnus-summary-limit-to-marks
1670     "M" gnus-summary-limit-exclude-marks
1671     "v" gnus-summary-limit-to-score
1672     "*" gnus-summary-limit-include-cached
1673     "D" gnus-summary-limit-include-dormant
1674     "T" gnus-summary-limit-include-thread
1675     "d" gnus-summary-limit-exclude-dormant
1676     "t" gnus-summary-limit-to-age
1677     "." gnus-summary-limit-to-unseen
1678     "x" gnus-summary-limit-to-extra
1679     "p" gnus-summary-limit-to-display-predicate
1680     "E" gnus-summary-limit-include-expunged
1681     "c" gnus-summary-limit-exclude-childless-dormant
1682     "C" gnus-summary-limit-mark-excluded-as-read
1683     "o" gnus-summary-insert-old-articles
1684     "N" gnus-summary-insert-new-articles)
1685
1686   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1687     "n" gnus-summary-next-unread-article
1688     "p" gnus-summary-prev-unread-article
1689     "N" gnus-summary-next-article
1690     "P" gnus-summary-prev-article
1691     "\C-n" gnus-summary-next-same-subject
1692     "\C-p" gnus-summary-prev-same-subject
1693     "\M-n" gnus-summary-next-unread-subject
1694     "\M-p" gnus-summary-prev-unread-subject
1695     "f" gnus-summary-first-unread-article
1696     "b" gnus-summary-best-unread-article
1697     "j" gnus-summary-goto-article
1698     "g" gnus-summary-goto-subject
1699     "l" gnus-summary-goto-last-article
1700     "o" gnus-summary-pop-article)
1701
1702   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1703     "k" gnus-summary-kill-thread
1704     "l" gnus-summary-lower-thread
1705     "i" gnus-summary-raise-thread
1706     "T" gnus-summary-toggle-threads
1707     "t" gnus-summary-rethread-current
1708     "^" gnus-summary-reparent-thread
1709     "s" gnus-summary-show-thread
1710     "S" gnus-summary-show-all-threads
1711     "h" gnus-summary-hide-thread
1712     "H" gnus-summary-hide-all-threads
1713     "n" gnus-summary-next-thread
1714     "p" gnus-summary-prev-thread
1715     "u" gnus-summary-up-thread
1716     "o" gnus-summary-top-thread
1717     "d" gnus-summary-down-thread
1718     "#" gnus-uu-mark-thread
1719     "\M-#" gnus-uu-unmark-thread)
1720
1721   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1722     "g" gnus-summary-prepare
1723     "c" gnus-summary-insert-cached-articles
1724     "d" gnus-summary-insert-dormant-articles)
1725
1726   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1727     "c" gnus-summary-catchup-and-exit
1728     "C" gnus-summary-catchup-all-and-exit
1729     "E" gnus-summary-exit-no-update
1730     "Q" gnus-summary-exit
1731     "Z" gnus-summary-exit
1732     "n" gnus-summary-catchup-and-goto-next-group
1733     "R" gnus-summary-reselect-current-group
1734     "G" gnus-summary-rescan-group
1735     "N" gnus-summary-next-group
1736     "s" gnus-summary-save-newsrc
1737     "P" gnus-summary-prev-group)
1738
1739   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1740     " " gnus-summary-next-page
1741     "n" gnus-summary-next-page
1742     "\177" gnus-summary-prev-page
1743     [delete] gnus-summary-prev-page
1744     "p" gnus-summary-prev-page
1745     "\r" gnus-summary-scroll-up
1746     "\M-\r" gnus-summary-scroll-down
1747     "<" gnus-summary-beginning-of-article
1748     ">" gnus-summary-end-of-article
1749     "b" gnus-summary-beginning-of-article
1750     "e" gnus-summary-end-of-article
1751     "^" gnus-summary-refer-parent-article
1752     "r" gnus-summary-refer-parent-article
1753     "D" gnus-summary-enter-digest-group
1754     "R" gnus-summary-refer-references
1755     "T" gnus-summary-refer-thread
1756     "g" gnus-summary-show-article
1757     "s" gnus-summary-isearch-article
1758     "P" gnus-summary-print-article
1759     "M" gnus-mailing-list-insinuate
1760     "t" gnus-article-babel)
1761
1762   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1763     "b" gnus-article-add-buttons
1764     "B" gnus-article-add-buttons-to-head
1765     "o" gnus-article-treat-overstrike
1766     "e" gnus-article-emphasize
1767     "w" gnus-article-fill-cited-article
1768     "Q" gnus-article-fill-long-lines
1769     "C" gnus-article-capitalize-sentences
1770     "c" gnus-article-remove-cr
1771     "q" gnus-article-de-quoted-unreadable
1772     "6" gnus-article-de-base64-unreadable
1773     "Z" gnus-article-decode-HZ
1774     "h" gnus-article-wash-html
1775     "u" gnus-article-unsplit-urls
1776     "s" gnus-summary-force-verify-and-decrypt
1777     "f" gnus-article-display-x-face
1778     "l" gnus-summary-stop-page-breaking
1779     "r" gnus-summary-caesar-message
1780     "m" gnus-summary-morse-message
1781     "t" gnus-summary-toggle-header
1782     "g" gnus-treat-smiley
1783     "v" gnus-summary-verbose-headers
1784     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1785     "p" gnus-article-verify-x-pgp-sig
1786     "d" gnus-article-treat-dumbquotes
1787     "k" gnus-article-outlook-deuglify-article)
1788
1789   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1790     "a" gnus-article-hide
1791     "h" gnus-article-hide-headers
1792     "b" gnus-article-hide-boring-headers
1793     "s" gnus-article-hide-signature
1794     "c" gnus-article-hide-citation
1795     "C" gnus-article-hide-citation-in-followups
1796     "l" gnus-article-hide-list-identifiers
1797     "p" gnus-article-hide-pgp
1798     "B" gnus-article-strip-banner
1799     "P" gnus-article-hide-pem
1800     "\C-c" gnus-article-hide-citation-maybe)
1801
1802   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1803     "a" gnus-article-highlight
1804     "h" gnus-article-highlight-headers
1805     "c" gnus-article-highlight-citation
1806     "s" gnus-article-highlight-signature)
1807
1808   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1809     "f" gnus-article-treat-fold-headers
1810     "u" gnus-article-treat-unfold-headers
1811     "n" gnus-article-treat-fold-newsgroups)
1812
1813   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1814     "x" gnus-article-display-x-face
1815     "s" gnus-treat-smiley
1816     "D" gnus-article-remove-images
1817     "f" gnus-treat-from-picon
1818     "m" gnus-treat-mail-picon
1819     "n" gnus-treat-newsgroups-picon)
1820
1821   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1822     "w" gnus-article-decode-mime-words
1823     "c" gnus-article-decode-charset
1824     "v" gnus-mime-view-all-parts
1825     "b" gnus-article-view-part)
1826
1827   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1828     "z" gnus-article-date-ut
1829     "u" gnus-article-date-ut
1830     "l" gnus-article-date-local
1831     "p" gnus-article-date-english
1832     "e" gnus-article-date-lapsed
1833     "o" gnus-article-date-original
1834     "i" gnus-article-date-iso8601
1835     "s" gnus-article-date-user)
1836
1837   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1838     "t" gnus-article-remove-trailing-blank-lines
1839     "l" gnus-article-strip-leading-blank-lines
1840     "m" gnus-article-strip-multiple-blank-lines
1841     "a" gnus-article-strip-blank-lines
1842     "A" gnus-article-strip-all-blank-lines
1843     "s" gnus-article-strip-leading-space
1844     "e" gnus-article-strip-trailing-space
1845     "w" gnus-article-remove-leading-whitespace)
1846
1847   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1848     "v" gnus-version
1849     "f" gnus-summary-fetch-faq
1850     "d" gnus-summary-describe-group
1851     "h" gnus-summary-describe-briefly
1852     "i" gnus-info-find-node
1853     "c" gnus-group-fetch-charter
1854     "C" gnus-group-fetch-control)
1855
1856   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1857     "e" gnus-summary-expire-articles
1858     "\M-\C-e" gnus-summary-expire-articles-now
1859     "\177" gnus-summary-delete-article
1860     [delete] gnus-summary-delete-article
1861     [backspace] gnus-summary-delete-article
1862     "m" gnus-summary-move-article
1863     "r" gnus-summary-respool-article
1864     "w" gnus-summary-edit-article
1865     "c" gnus-summary-copy-article
1866     "B" gnus-summary-crosspost-article
1867     "q" gnus-summary-respool-query
1868     "t" gnus-summary-respool-trace
1869     "i" gnus-summary-import-article
1870     "I" gnus-summary-create-article
1871     "p" gnus-summary-article-posted-p)
1872
1873   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1874     "o" gnus-summary-save-article
1875     "m" gnus-summary-save-article-mail
1876     "F" gnus-summary-write-article-file
1877     "r" gnus-summary-save-article-rmail
1878     "f" gnus-summary-save-article-file
1879     "b" gnus-summary-save-article-body-file
1880     "h" gnus-summary-save-article-folder
1881     "v" gnus-summary-save-article-vm
1882     "p" gnus-summary-pipe-output
1883     "P" gnus-summary-muttprint
1884     "s" gnus-soup-add-article)
1885
1886   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1887     "b" gnus-summary-display-buttonized
1888     "m" gnus-summary-repair-multipart
1889     "v" gnus-article-view-part
1890     "o" gnus-article-save-part
1891     "c" gnus-article-copy-part
1892     "C" gnus-article-view-part-as-charset
1893     "e" gnus-article-view-part-externally
1894     "E" gnus-article-encrypt-body
1895     "i" gnus-article-inline-part
1896     "|" gnus-article-pipe-part)
1897
1898   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1899     "p" gnus-summary-mark-as-processable
1900     "u" gnus-summary-unmark-as-processable
1901     "U" gnus-summary-unmark-all-processable
1902     "v" gnus-uu-mark-over
1903     "s" gnus-uu-mark-series
1904     "r" gnus-uu-mark-region
1905     "g" gnus-uu-unmark-region
1906     "R" gnus-uu-mark-by-regexp
1907     "G" gnus-uu-unmark-by-regexp
1908     "t" gnus-uu-mark-thread
1909     "T" gnus-uu-unmark-thread
1910     "a" gnus-uu-mark-all
1911     "b" gnus-uu-mark-buffer
1912     "S" gnus-uu-mark-sparse
1913     "k" gnus-summary-kill-process-mark
1914     "y" gnus-summary-yank-process-mark
1915     "w" gnus-summary-save-process-mark
1916     "i" gnus-uu-invert-processable)
1917
1918   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1919     ;;"x" gnus-uu-extract-any
1920     "m" gnus-summary-save-parts
1921     "u" gnus-uu-decode-uu
1922     "U" gnus-uu-decode-uu-and-save
1923     "s" gnus-uu-decode-unshar
1924     "S" gnus-uu-decode-unshar-and-save
1925     "o" gnus-uu-decode-save
1926     "O" gnus-uu-decode-save
1927     "b" gnus-uu-decode-binhex
1928     "B" gnus-uu-decode-binhex
1929     "p" gnus-uu-decode-postscript
1930     "P" gnus-uu-decode-postscript-and-save)
1931
1932   (gnus-define-keys
1933       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1934     "u" gnus-uu-decode-uu-view
1935     "U" gnus-uu-decode-uu-and-save-view
1936     "s" gnus-uu-decode-unshar-view
1937     "S" gnus-uu-decode-unshar-and-save-view
1938     "o" gnus-uu-decode-save-view
1939     "O" gnus-uu-decode-save-view
1940     "b" gnus-uu-decode-binhex-view
1941     "B" gnus-uu-decode-binhex-view
1942     "p" gnus-uu-decode-postscript-view
1943     "P" gnus-uu-decode-postscript-and-save-view))
1944
1945 (defvar gnus-article-post-menu nil)
1946
1947 (defconst gnus-summary-menu-maxlen 20)
1948
1949 (defun gnus-summary-menu-split (menu)
1950   ;; If we have lots of elements, divide them into groups of 20
1951   ;; and make a pane (or submenu) for each one.
1952   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1953       (let ((menu menu) sublists next
1954             (i 1))
1955         (while menu
1956           ;; Pull off the next gnus-summary-menu-maxlen elements
1957           ;; and make them the next element of sublist.
1958           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1959           (if next
1960               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1961                       nil))
1962           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1963                                              (aref (car (last menu)) 0)) menu)
1964                                sublists))
1965           (setq i (1+ i))
1966           (setq menu next))
1967         (nreverse sublists))
1968     ;; Few elements--put them all in one pane.
1969     menu))
1970
1971 (defun gnus-summary-make-menu-bar ()
1972   (gnus-turn-off-edit-menu 'summary)
1973
1974   (unless (boundp 'gnus-summary-misc-menu)
1975
1976     (easy-menu-define
1977       gnus-summary-kill-menu gnus-summary-mode-map ""
1978       (cons
1979        "Score"
1980        (nconc
1981         (list
1982          ["Customize" gnus-score-customize t])
1983         (gnus-make-score-map 'increase)
1984         (gnus-make-score-map 'lower)
1985         '(("Mark"
1986            ["Kill below" gnus-summary-kill-below t]
1987            ["Mark above" gnus-summary-mark-above t]
1988            ["Tick above" gnus-summary-tick-above t]
1989            ["Clear above" gnus-summary-clear-above t])
1990           ["Current score" gnus-summary-current-score t]
1991           ["Set score" gnus-summary-set-score t]
1992           ["Switch current score file..." gnus-score-change-score-file t]
1993           ["Set mark below..." gnus-score-set-mark-below t]
1994           ["Set expunge below..." gnus-score-set-expunge-below t]
1995           ["Edit current score file" gnus-score-edit-current-scores t]
1996           ["Edit score file" gnus-score-edit-file t]
1997           ["Trace score" gnus-score-find-trace t]
1998           ["Find words" gnus-score-find-favourite-words t]
1999           ["Rescore buffer" gnus-summary-rescore t]
2000           ["Increase score..." gnus-summary-increase-score t]
2001           ["Lower score..." gnus-summary-lower-score t]))))
2002
2003     ;; Define both the Article menu in the summary buffer and the
2004     ;; equivalent Commands menu in the article buffer here for
2005     ;; consistency.
2006     (let ((innards
2007            `(("Hide"
2008               ["All" gnus-article-hide t]
2009               ["Headers" gnus-article-hide-headers t]
2010               ["Signature" gnus-article-hide-signature t]
2011               ["Citation" gnus-article-hide-citation t]
2012               ["List identifiers" gnus-article-hide-list-identifiers t]
2013               ["PGP" gnus-article-hide-pgp t]
2014               ["Banner" gnus-article-strip-banner t]
2015               ["Boring headers" gnus-article-hide-boring-headers t])
2016              ("Highlight"
2017               ["All" gnus-article-highlight t]
2018               ["Headers" gnus-article-highlight-headers t]
2019               ["Signature" gnus-article-highlight-signature t]
2020               ["Citation" gnus-article-highlight-citation t])
2021              ("MIME"
2022               ["Words" gnus-article-decode-mime-words t]
2023               ["Charset" gnus-article-decode-charset t]
2024               ["QP" gnus-article-de-quoted-unreadable t]
2025               ["Base64" gnus-article-de-base64-unreadable t]
2026               ["View MIME buttons" gnus-summary-display-buttonized t]
2027               ["View all" gnus-mime-view-all-parts t]
2028               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2029               ["Encrypt body" gnus-article-encrypt-body t]
2030               ["Extract all parts" gnus-summary-save-parts t])
2031              ("Date"
2032               ["Local" gnus-article-date-local t]
2033               ["ISO8601" gnus-article-date-iso8601 t]
2034               ["UT" gnus-article-date-ut t]
2035               ["Original" gnus-article-date-original t]
2036               ["Lapsed" gnus-article-date-lapsed t]
2037               ["User-defined" gnus-article-date-user t])
2038              ("Display"
2039               ["Remove images" gnus-article-remove-images t]
2040               ["Toggle smiley" gnus-treat-smiley t]
2041               ["Show X-Face" gnus-article-display-x-face t]
2042               ["Show picons in From" gnus-treat-from-picon t]
2043               ["Show picons in mail headers" gnus-treat-mail-picon t]
2044               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2045               ("View as different encoding"
2046                ,@(gnus-summary-menu-split
2047                   (mapcar
2048                    (lambda (cs)
2049                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2050                      ;; forms for menu commands, we should provide intern'ed
2051                      ;; function symbols.
2052                      (let ((command (intern (format "\
2053 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2054                        (fset command
2055                              `(lambda ()
2056                                 (interactive)
2057                                 (let ((gnus-summary-show-article-charset-alist
2058                                        '((1 . ,cs))))
2059                                   (gnus-summary-show-article 1))))
2060                        `[,(symbol-name cs) ,command t]))
2061                    (sort (if (fboundp 'coding-system-list)
2062                              (coding-system-list)
2063                            (mapcar 'car mm-mime-mule-charset-alist))
2064                          'string<)))))
2065              ("Washing"
2066               ("Remove Blanks"
2067                ["Leading" gnus-article-strip-leading-blank-lines t]
2068                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2069                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2070                ["All of the above" gnus-article-strip-blank-lines t]
2071                ["All" gnus-article-strip-all-blank-lines t]
2072                ["Leading space" gnus-article-strip-leading-space t]
2073                ["Trailing space" gnus-article-strip-trailing-space t]
2074                ["Leading space in headers"
2075                 gnus-article-remove-leading-whitespace t])
2076               ["Overstrike" gnus-article-treat-overstrike t]
2077               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2078               ["Emphasis" gnus-article-emphasize t]
2079               ["Word wrap" gnus-article-fill-cited-article t]
2080               ["Fill long lines" gnus-article-fill-long-lines t]
2081               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2082               ["CR" gnus-article-remove-cr t]
2083               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2084               ["Base64" gnus-article-de-base64-unreadable t]
2085               ["Rot 13" gnus-summary-caesar-message
2086                ,@(if (featurep 'xemacs) '(t)
2087                    '(:help "\"Caesar rotate\" article by 13"))]
2088               ["Morse decode" gnus-summary-morse-message t]
2089               ["Unix pipe..." gnus-summary-pipe-message t]
2090               ["Add buttons" gnus-article-add-buttons t]
2091               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2092               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2093               ["Verbose header" gnus-summary-verbose-headers t]
2094               ["Toggle header" gnus-summary-toggle-header t]
2095               ["Unfold headers" gnus-article-treat-unfold-headers t]
2096               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2097               ["Html" gnus-article-wash-html t]
2098               ["URLs" gnus-article-unsplit-urls t]
2099               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2100               ["HZ" gnus-article-decode-HZ t]
2101               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2102               )
2103              ("Output"
2104               ["Save in default format" gnus-summary-save-article
2105                ,@(if (featurep 'xemacs) '(t)
2106                    '(:help "Save article using default method"))]
2107               ["Save in file" gnus-summary-save-article-file
2108                ,@(if (featurep 'xemacs) '(t)
2109                    '(:help "Save article in file"))]
2110               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2111               ["Save in MH folder" gnus-summary-save-article-folder t]
2112               ["Save in VM folder" gnus-summary-save-article-vm t]
2113               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2114               ["Save body in file" gnus-summary-save-article-body-file t]
2115               ["Pipe through a filter" gnus-summary-pipe-output t]
2116               ["Add to SOUP packet" gnus-soup-add-article t]
2117               ["Print with Muttprint" gnus-summary-muttprint t]
2118               ["Print" gnus-summary-print-article t])
2119              ("Backend"
2120               ["Respool article..." gnus-summary-respool-article t]
2121               ["Move article..." gnus-summary-move-article
2122                (gnus-check-backend-function
2123                 'request-move-article gnus-newsgroup-name)]
2124               ["Copy article..." gnus-summary-copy-article t]
2125               ["Crosspost article..." gnus-summary-crosspost-article
2126                (gnus-check-backend-function
2127                 'request-replace-article gnus-newsgroup-name)]
2128               ["Import file..." gnus-summary-import-article t]
2129               ["Create article..." gnus-summary-create-article t]
2130               ["Check if posted" gnus-summary-article-posted-p t]
2131               ["Edit article" gnus-summary-edit-article
2132                (not (gnus-group-read-only-p))]
2133               ["Delete article" gnus-summary-delete-article
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)]
2136               ["Query respool" gnus-summary-respool-query t]
2137               ["Trace respool" gnus-summary-respool-trace t]
2138               ["Delete expirable articles" gnus-summary-expire-articles-now
2139                (gnus-check-backend-function
2140                 'request-expire-articles gnus-newsgroup-name)])
2141              ("Extract"
2142               ["Uudecode" gnus-uu-decode-uu
2143                ,@(if (featurep 'xemacs) '(t)
2144                    '(:help "Decode uuencoded article(s)"))]
2145               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2146               ["Unshar" gnus-uu-decode-unshar t]
2147               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2148               ["Save" gnus-uu-decode-save t]
2149               ["Binhex" gnus-uu-decode-binhex t]
2150               ["Postscript" gnus-uu-decode-postscript t]
2151               ["All MIME parts" gnus-summary-save-parts t])
2152              ("Cache"
2153               ["Enter article" gnus-cache-enter-article t]
2154               ["Remove article" gnus-cache-remove-article t])
2155              ["Translate" gnus-article-babel t]
2156              ["Select article buffer" gnus-summary-select-article-buffer t]
2157              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2158              ["Isearch article..." gnus-summary-isearch-article t]
2159              ["Beginning of the article" gnus-summary-beginning-of-article t]
2160              ["End of the article" gnus-summary-end-of-article t]
2161              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2162              ["Fetch referenced articles" gnus-summary-refer-references t]
2163              ["Fetch current thread" gnus-summary-refer-thread t]
2164              ["Fetch article with id..." gnus-summary-refer-article t]
2165              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2166              ["Redisplay" gnus-summary-show-article t]
2167              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2168       (easy-menu-define
2169         gnus-summary-article-menu gnus-summary-mode-map ""
2170         (cons "Article" innards))
2171
2172       (if (not (keymapp gnus-summary-article-menu))
2173           (easy-menu-define
2174             gnus-article-commands-menu gnus-article-mode-map ""
2175             (cons "Commands" innards))
2176         ;; in Emacs, don't share menu.
2177         (setq gnus-article-commands-menu
2178               (copy-keymap gnus-summary-article-menu))
2179         (define-key gnus-article-mode-map [menu-bar commands]
2180           (cons "Commands" gnus-article-commands-menu))))
2181
2182     (easy-menu-define
2183       gnus-summary-thread-menu gnus-summary-mode-map ""
2184       '("Threads"
2185         ["Find all messages in thread" gnus-summary-refer-thread t]
2186         ["Toggle threading" gnus-summary-toggle-threads t]
2187         ["Hide threads" gnus-summary-hide-all-threads t]
2188         ["Show threads" gnus-summary-show-all-threads t]
2189         ["Hide thread" gnus-summary-hide-thread t]
2190         ["Show thread" gnus-summary-show-thread t]
2191         ["Go to next thread" gnus-summary-next-thread t]
2192         ["Go to previous thread" gnus-summary-prev-thread t]
2193         ["Go down thread" gnus-summary-down-thread t]
2194         ["Go up thread" gnus-summary-up-thread t]
2195         ["Top of thread" gnus-summary-top-thread t]
2196         ["Mark thread as read" gnus-summary-kill-thread t]
2197         ["Lower thread score" gnus-summary-lower-thread t]
2198         ["Raise thread score" gnus-summary-raise-thread t]
2199         ["Rethread current" gnus-summary-rethread-current t]))
2200
2201     (easy-menu-define
2202       gnus-summary-post-menu gnus-summary-mode-map ""
2203       `("Post"
2204         ["Send a message (mail or news)" gnus-summary-post-news
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post an article"))]
2207         ["Followup" gnus-summary-followup
2208          ,@(if (featurep 'xemacs) '(t)
2209              '(:help "Post followup to this article"))]
2210         ["Followup and yank" gnus-summary-followup-with-original
2211          ,@(if (featurep 'xemacs) '(t)
2212              '(:help "Post followup to this article, quoting its contents"))]
2213         ["Supersede article" gnus-summary-supersede-article t]
2214         ["Cancel article" gnus-summary-cancel-article
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Cancel an article you posted"))]
2217         ["Reply" gnus-summary-reply t]
2218         ["Reply and yank" gnus-summary-reply-with-original t]
2219         ["Wide reply" gnus-summary-wide-reply t]
2220         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2221          ,@(if (featurep 'xemacs) '(t)
2222              '(:help "Mail a reply, quoting this article"))]
2223         ["Very wide reply" gnus-summary-very-wide-reply t]
2224         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2225          ,@(if (featurep 'xemacs) '(t)
2226              '(:help "Mail a very wide reply, quoting this article"))]
2227         ["Mail forward" gnus-summary-mail-forward t]
2228         ["Post forward" gnus-summary-post-forward t]
2229         ["Digest and mail" gnus-uu-digest-mail-forward t]
2230         ["Digest and post" gnus-uu-digest-post-forward t]
2231         ["Resend message" gnus-summary-resend-message t]
2232         ["Resend message edit" gnus-summary-resend-message-edit t]
2233         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2234         ["Send a mail" gnus-summary-mail-other-window t]
2235         ["Create a local message" gnus-summary-news-other-window t]
2236         ["Uuencode and post" gnus-uu-post-news
2237          ,@(if (featurep 'xemacs) '(t)
2238              '(:help "Post a uuencoded article"))]
2239         ["Followup via news" gnus-summary-followup-to-mail t]
2240         ["Followup via news and yank"
2241          gnus-summary-followup-to-mail-with-original t]
2242         ;;("Draft"
2243         ;;["Send" gnus-summary-send-draft t]
2244         ;;["Send bounced" gnus-resend-bounced-mail t])
2245         ))
2246
2247     (cond
2248      ((not (keymapp gnus-summary-post-menu))
2249       (setq gnus-article-post-menu gnus-summary-post-menu))
2250      ((not gnus-article-post-menu)
2251       ;; Don't share post menu.
2252       (setq gnus-article-post-menu
2253             (copy-keymap gnus-summary-post-menu))))
2254     (define-key gnus-article-mode-map [menu-bar post]
2255       (cons "Post" gnus-article-post-menu))
2256
2257     (easy-menu-define
2258       gnus-summary-misc-menu gnus-summary-mode-map ""
2259       `("Gnus"
2260         ("Mark Read"
2261          ["Mark as read" gnus-summary-mark-as-read-forward t]
2262          ["Mark same subject and select"
2263           gnus-summary-kill-same-subject-and-select t]
2264          ["Mark same subject" gnus-summary-kill-same-subject t]
2265          ["Catchup" gnus-summary-catchup
2266           ,@(if (featurep 'xemacs) '(t)
2267               '(:help "Mark unread articles in this group as read"))]
2268          ["Catchup all" gnus-summary-catchup-all t]
2269          ["Catchup to here" gnus-summary-catchup-to-here t]
2270          ["Catchup from here" gnus-summary-catchup-from-here t]
2271          ["Catchup region" gnus-summary-mark-region-as-read t]
2272          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2273         ("Mark Various"
2274          ["Tick" gnus-summary-tick-article-forward t]
2275          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2276          ["Remove marks" gnus-summary-clear-mark-forward t]
2277          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2278          ["Set bookmark" gnus-summary-set-bookmark t]
2279          ["Remove bookmark" gnus-summary-remove-bookmark t])
2280         ("Limit to"
2281          ["Marks..." gnus-summary-limit-to-marks t]
2282          ["Subject..." gnus-summary-limit-to-subject t]
2283          ["Author..." gnus-summary-limit-to-author t]
2284          ["Age..." gnus-summary-limit-to-age t]
2285          ["Extra..." gnus-summary-limit-to-extra t]
2286          ["Score..." gnus-summary-limit-to-score t]
2287          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2288          ["Unread" gnus-summary-limit-to-unread t]
2289          ["Unseen" gnus-summary-limit-to-unseen t]
2290          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2291          ["Next articles" gnus-summary-limit-to-articles t]
2292          ["Pop limit" gnus-summary-pop-limit t]
2293          ["Show dormant" gnus-summary-limit-include-dormant t]
2294          ["Hide childless dormant"
2295           gnus-summary-limit-exclude-childless-dormant t]
2296          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2297          ["Hide marked" gnus-summary-limit-exclude-marks t]
2298          ["Show expunged" gnus-summary-limit-include-expunged t])
2299         ("Process Mark"
2300          ["Set mark" gnus-summary-mark-as-processable t]
2301          ["Remove mark" gnus-summary-unmark-as-processable t]
2302          ["Remove all marks" gnus-summary-unmark-all-processable t]
2303          ["Mark above" gnus-uu-mark-over t]
2304          ["Mark series" gnus-uu-mark-series t]
2305          ["Mark region" gnus-uu-mark-region t]
2306          ["Unmark region" gnus-uu-unmark-region t]
2307          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2308          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2309          ["Mark all" gnus-uu-mark-all t]
2310          ["Mark buffer" gnus-uu-mark-buffer t]
2311          ["Mark sparse" gnus-uu-mark-sparse t]
2312          ["Mark thread" gnus-uu-mark-thread t]
2313          ["Unmark thread" gnus-uu-unmark-thread t]
2314          ("Process Mark Sets"
2315           ["Kill" gnus-summary-kill-process-mark t]
2316           ["Yank" gnus-summary-yank-process-mark
2317            gnus-newsgroup-process-stack]
2318           ["Save" gnus-summary-save-process-mark t]))
2319         ("Scroll article"
2320          ["Page forward" gnus-summary-next-page
2321           ,@(if (featurep 'xemacs) '(t)
2322               '(:help "Show next page of article"))]
2323          ["Page backward" gnus-summary-prev-page
2324           ,@(if (featurep 'xemacs) '(t)
2325               '(:help "Show previous page of article"))]
2326          ["Line forward" gnus-summary-scroll-up t])
2327         ("Move"
2328          ["Next unread article" gnus-summary-next-unread-article t]
2329          ["Previous unread article" gnus-summary-prev-unread-article t]
2330          ["Next article" gnus-summary-next-article t]
2331          ["Previous article" gnus-summary-prev-article t]
2332          ["Next unread subject" gnus-summary-next-unread-subject t]
2333          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2334          ["Next article same subject" gnus-summary-next-same-subject t]
2335          ["Previous article same subject" gnus-summary-prev-same-subject t]
2336          ["First unread article" gnus-summary-first-unread-article t]
2337          ["Best unread article" gnus-summary-best-unread-article t]
2338          ["Go to subject number..." gnus-summary-goto-subject t]
2339          ["Go to article number..." gnus-summary-goto-article t]
2340          ["Go to the last article" gnus-summary-goto-last-article t]
2341          ["Pop article off history" gnus-summary-pop-article t])
2342         ("Sort"
2343          ["Sort by number" gnus-summary-sort-by-number t]
2344          ["Sort by author" gnus-summary-sort-by-author t]
2345          ["Sort by subject" gnus-summary-sort-by-subject t]
2346          ["Sort by date" gnus-summary-sort-by-date t]
2347          ["Sort by score" gnus-summary-sort-by-score t]
2348          ["Sort by lines" gnus-summary-sort-by-lines t]
2349          ["Sort by characters" gnus-summary-sort-by-chars t]
2350          ["Randomize" gnus-summary-sort-by-random t]
2351          ["Original sort" gnus-summary-sort-by-original t])
2352         ("Help"
2353          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2354          ["Describe group" gnus-summary-describe-group t]
2355          ["Fetch charter" gnus-group-fetch-charter
2356           ,@(if (featurep 'xemacs) nil
2357               '(:help "Display the charter of the current group"))]
2358          ["Fetch control message" gnus-group-fetch-control
2359           ,@(if (featurep 'xemacs) nil
2360               '(:help "Display the archived control message for the current group"))]
2361          ["Read manual" gnus-info-find-node t])
2362         ("Modes"
2363          ["Pick and read" gnus-pick-mode t]
2364          ["Binary" gnus-binary-mode t])
2365         ("Regeneration"
2366          ["Regenerate" gnus-summary-prepare t]
2367          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2368          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2369          ["Toggle threading" gnus-summary-toggle-threads t])
2370         ["See old articles" gnus-summary-insert-old-articles t]
2371         ["See new articles" gnus-summary-insert-new-articles t]
2372         ["Filter articles..." gnus-summary-execute-command t]
2373         ["Run command on subjects..." gnus-summary-universal-argument t]
2374         ["Search articles forward..." gnus-summary-search-article-forward t]
2375         ["Search articles backward..." gnus-summary-search-article-backward t]
2376         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2377         ["Expand window" gnus-summary-expand-window t]
2378         ["Expire expirable articles" gnus-summary-expire-articles
2379          (gnus-check-backend-function
2380           'request-expire-articles gnus-newsgroup-name)]
2381         ["Edit local kill file" gnus-summary-edit-local-kill t]
2382         ["Edit main kill file" gnus-summary-edit-global-kill t]
2383         ["Edit group parameters" gnus-summary-edit-parameters t]
2384         ["Customize group parameters" gnus-summary-customize-parameters t]
2385         ["Send a bug report" gnus-bug t]
2386         ("Exit"
2387          ["Catchup and exit" gnus-summary-catchup-and-exit
2388           ,@(if (featurep 'xemacs) '(t)
2389               '(:help "Mark unread articles in this group as read, then exit"))]
2390          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2391          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2392          ["Exit group" gnus-summary-exit
2393           ,@(if (featurep 'xemacs) '(t)
2394               '(:help "Exit current group, return to group selection mode"))]
2395          ["Exit group without updating" gnus-summary-exit-no-update t]
2396          ["Exit and goto next group" gnus-summary-next-group t]
2397          ["Exit and goto prev group" gnus-summary-prev-group t]
2398          ["Reselect group" gnus-summary-reselect-current-group t]
2399          ["Rescan group" gnus-summary-rescan-group t]
2400          ["Update dribble" gnus-summary-save-newsrc t])))
2401
2402     (gnus-run-hooks 'gnus-summary-menu-hook)))
2403
2404 (defvar gnus-summary-tool-bar-map nil)
2405
2406 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2407 (defun gnus-summary-make-tool-bar ()
2408   (if (and (fboundp 'tool-bar-add-item-from-menu)
2409            (default-value 'tool-bar-mode)
2410            (not gnus-summary-tool-bar-map))
2411       (setq gnus-summary-tool-bar-map
2412             (let ((tool-bar-map (make-sparse-keymap))
2413                   (load-path (mm-image-load-path)))
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-post-news "post" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-followup "followup" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-reply "reply" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2438               (tool-bar-add-item-from-menu
2439                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2440               (tool-bar-add-item-from-menu
2441                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2442               (tool-bar-add-item-from-menu
2443                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2444               tool-bar-map)))
2445   (if gnus-summary-tool-bar-map
2446       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2447
2448 (defun gnus-score-set-default (var value)
2449   "A version of set that updates the GNU Emacs menu-bar."
2450   (set var value)
2451   ;; It is the message that forces the active status to be updated.
2452   (message ""))
2453
2454 (defun gnus-make-score-map (type)
2455   "Make a summary score map of type TYPE."
2456   (if t
2457       nil
2458     (let ((headers '(("author" "from" string)
2459                      ("subject" "subject" string)
2460                      ("article body" "body" string)
2461                      ("article head" "head" string)
2462                      ("xref" "xref" string)
2463                      ("extra header" "extra" string)
2464                      ("lines" "lines" number)
2465                      ("followups to author" "followup" string)))
2466           (types '((number ("less than" <)
2467                            ("greater than" >)
2468                            ("equal" =))
2469                    (string ("substring" s)
2470                            ("exact string" e)
2471                            ("fuzzy string" f)
2472                            ("regexp" r))))
2473           (perms '(("temporary" (current-time-string))
2474                    ("permanent" nil)
2475                    ("immediate" now)))
2476           header)
2477       (list
2478        (apply
2479         'nconc
2480         (list
2481          (if (eq type 'lower)
2482              "Lower score"
2483            "Increase score"))
2484         (let (outh)
2485           (while headers
2486             (setq header (car headers))
2487             (setq outh
2488                   (cons
2489                    (apply
2490                     'nconc
2491                     (list (car header))
2492                     (let ((ts (cdr (assoc (nth 2 header) types)))
2493                           outt)
2494                       (while ts
2495                         (setq outt
2496                               (cons
2497                                (apply
2498                                 'nconc
2499                                 (list (caar ts))
2500                                 (let ((ps perms)
2501                                       outp)
2502                                   (while ps
2503                                     (setq outp
2504                                           (cons
2505                                            (vector
2506                                             (caar ps)
2507                                             (list
2508                                              'gnus-summary-score-entry
2509                                              (nth 1 header)
2510                                              (if (or (string= (nth 1 header)
2511                                                               "head")
2512                                                      (string= (nth 1 header)
2513                                                               "body"))
2514                                                  ""
2515                                                (list 'gnus-summary-header
2516                                                      (nth 1 header)))
2517                                              (list 'quote (nth 1 (car ts)))
2518                                              (list 'gnus-score-delta-default
2519                                                    nil)
2520                                              (nth 1 (car ps))
2521                                              t)
2522                                             t)
2523                                            outp))
2524                                     (setq ps (cdr ps)))
2525                                   (list (nreverse outp))))
2526                                outt))
2527                         (setq ts (cdr ts)))
2528                       (list (nreverse outt))))
2529                    outh))
2530             (setq headers (cdr headers)))
2531           (list (nreverse outh))))))))
2532
2533 \f
2534
2535 (defun gnus-summary-mode (&optional group)
2536   "Major mode for reading articles.
2537
2538 All normal editing commands are switched off.
2539 \\<gnus-summary-mode-map>
2540 Each line in this buffer represents one article.  To read an
2541 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2542 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2543 respectively.
2544
2545 You can also post articles and send mail from this buffer.  To
2546 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2547 of an article, type `\\[gnus-summary-reply]'.
2548
2549 There are approx. one gazillion commands you can execute in this
2550 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2551
2552 The following commands are available:
2553
2554 \\{gnus-summary-mode-map}"
2555   (interactive)
2556   (kill-all-local-variables)
2557   (when (gnus-visual-p 'summary-menu 'menu)
2558     (gnus-summary-make-menu-bar)
2559     (gnus-summary-make-tool-bar))
2560   (gnus-summary-make-local-variables)
2561   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2562     (gnus-summary-make-local-variables))
2563   (gnus-make-thread-indent-array)
2564   (gnus-simplify-mode-line)
2565   (setq major-mode 'gnus-summary-mode)
2566   (setq mode-name "Summary")
2567   (make-local-variable 'minor-mode-alist)
2568   (use-local-map gnus-summary-mode-map)
2569   (buffer-disable-undo)
2570   (setq buffer-read-only t)             ;Disable modification
2571   (setq truncate-lines t)
2572   (setq selective-display t)
2573   (setq selective-display-ellipses t)   ;Display `...'
2574   (gnus-summary-set-display-table)
2575   (gnus-set-default-directory)
2576   (setq gnus-newsgroup-name group)
2577   (make-local-variable 'gnus-summary-line-format)
2578   (make-local-variable 'gnus-summary-line-format-spec)
2579   (make-local-variable 'gnus-summary-dummy-line-format)
2580   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2581   (make-local-variable 'gnus-summary-mark-positions)
2582   (make-local-hook 'pre-command-hook)
2583   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2584   (gnus-run-hooks 'gnus-summary-mode-hook)
2585   (turn-on-gnus-mailing-list-mode)
2586   (mm-enable-multibyte)
2587   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2588   (gnus-update-summary-mark-positions))
2589
2590 (defun gnus-summary-make-local-variables ()
2591   "Make all the local summary buffer variables."
2592   (let (global)
2593     (dolist (local gnus-summary-local-variables)
2594       (if (consp local)
2595           (progn
2596             (if (eq (cdr local) 'global)
2597                 ;; Copy the global value of the variable.
2598                 (setq global (symbol-value (car local)))
2599               ;; Use the value from the list.
2600               (setq global (eval (cdr local))))
2601             (set (make-local-variable (car local)) global))
2602         ;; Simple nil-valued local variable.
2603         (set (make-local-variable local) nil)))))
2604
2605 (defun gnus-summary-clear-local-variables ()
2606   (let ((locals gnus-summary-local-variables))
2607     (while locals
2608       (if (consp (car locals))
2609           (and (vectorp (caar locals))
2610                (set (caar locals) nil))
2611         (and (vectorp (car locals))
2612              (set (car locals) nil)))
2613       (setq locals (cdr locals)))))
2614
2615 ;; Summary data functions.
2616
2617 (defmacro gnus-data-number (data)
2618   `(car ,data))
2619
2620 (defmacro gnus-data-set-number (data number)
2621   `(setcar ,data ,number))
2622
2623 (defmacro gnus-data-mark (data)
2624   `(nth 1 ,data))
2625
2626 (defmacro gnus-data-set-mark (data mark)
2627   `(setcar (nthcdr 1 ,data) ,mark))
2628
2629 (defmacro gnus-data-pos (data)
2630   `(nth 2 ,data))
2631
2632 (defmacro gnus-data-set-pos (data pos)
2633   `(setcar (nthcdr 2 ,data) ,pos))
2634
2635 (defmacro gnus-data-header (data)
2636   `(nth 3 ,data))
2637
2638 (defmacro gnus-data-set-header (data header)
2639   `(setf (nth 3 ,data) ,header))
2640
2641 (defmacro gnus-data-level (data)
2642   `(nth 4 ,data))
2643
2644 (defmacro gnus-data-unread-p (data)
2645   `(= (nth 1 ,data) gnus-unread-mark))
2646
2647 (defmacro gnus-data-read-p (data)
2648   `(/= (nth 1 ,data) gnus-unread-mark))
2649
2650 (defmacro gnus-data-pseudo-p (data)
2651   `(consp (nth 3 ,data)))
2652
2653 (defmacro gnus-data-find (number)
2654   `(assq ,number gnus-newsgroup-data))
2655
2656 (defmacro gnus-data-find-list (number &optional data)
2657   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2658      (memq (assq ,number bdata)
2659            bdata)))
2660
2661 (defmacro gnus-data-make (number mark pos header level)
2662   `(list ,number ,mark ,pos ,header ,level))
2663
2664 (defun gnus-data-enter (after-article number mark pos header level offset)
2665   (let ((data (gnus-data-find-list after-article)))
2666     (unless data
2667       (error "No such article: %d" after-article))
2668     (setcdr data (cons (gnus-data-make number mark pos header level)
2669                        (cdr data)))
2670     (setq gnus-newsgroup-data-reverse nil)
2671     (gnus-data-update-list (cddr data) offset)))
2672
2673 (defun gnus-data-enter-list (after-article list &optional offset)
2674   (when list
2675     (let ((data (and after-article (gnus-data-find-list after-article)))
2676           (ilist list))
2677       (if (not (or data
2678                    after-article))
2679           (let ((odata gnus-newsgroup-data))
2680             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2681             (when offset
2682               (gnus-data-update-list odata offset)))
2683       ;; Find the last element in the list to be spliced into the main
2684         ;; list.
2685         (while (cdr list)
2686           (setq list (cdr list)))
2687         (if (not data)
2688             (progn
2689               (setcdr list gnus-newsgroup-data)
2690               (setq gnus-newsgroup-data ilist)
2691               (when offset
2692                 (gnus-data-update-list (cdr list) offset)))
2693           (setcdr list (cdr data))
2694           (setcdr data ilist)
2695           (when offset
2696             (gnus-data-update-list (cdr list) offset))))
2697       (setq gnus-newsgroup-data-reverse nil))))
2698
2699 (defun gnus-data-remove (article &optional offset)
2700   (let ((data gnus-newsgroup-data))
2701     (if (= (gnus-data-number (car data)) article)
2702         (progn
2703           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2704                 gnus-newsgroup-data-reverse nil)
2705           (when offset
2706             (gnus-data-update-list gnus-newsgroup-data offset)))
2707       (while (cdr data)
2708         (when (= (gnus-data-number (cadr data)) article)
2709           (setcdr data (cddr data))
2710           (when offset
2711             (gnus-data-update-list (cdr data) offset))
2712           (setq data nil
2713                 gnus-newsgroup-data-reverse nil))
2714         (setq data (cdr data))))))
2715
2716 (defmacro gnus-data-list (backward)
2717   `(if ,backward
2718        (or gnus-newsgroup-data-reverse
2719            (setq gnus-newsgroup-data-reverse
2720                  (reverse gnus-newsgroup-data)))
2721      gnus-newsgroup-data))
2722
2723 (defun gnus-data-update-list (data offset)
2724   "Add OFFSET to the POS of all data entries in DATA."
2725   (setq gnus-newsgroup-data-reverse nil)
2726   (while data
2727     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2728     (setq data (cdr data))))
2729
2730 (defun gnus-summary-article-pseudo-p (article)
2731   "Say whether this article is a pseudo article or not."
2732   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2733
2734 (defmacro gnus-summary-article-sparse-p (article)
2735   "Say whether this article is a sparse article or not."
2736   `(memq ,article gnus-newsgroup-sparse))
2737
2738 (defmacro gnus-summary-article-ancient-p (article)
2739   "Say whether this article is a sparse article or not."
2740   `(memq ,article gnus-newsgroup-ancient))
2741
2742 (defun gnus-article-parent-p (number)
2743   "Say whether this article is a parent or not."
2744   (let ((data (gnus-data-find-list number)))
2745     (and (cdr data)              ; There has to be an article after...
2746          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2747             (gnus-data-level (nth 1 data))))))
2748
2749 (defun gnus-article-children (number)
2750   "Return a list of all children to NUMBER."
2751   (let* ((data (gnus-data-find-list number))
2752          (level (gnus-data-level (car data)))
2753          children)
2754     (setq data (cdr data))
2755     (while (and data
2756                 (= (gnus-data-level (car data)) (1+ level)))
2757       (push (gnus-data-number (car data)) children)
2758       (setq data (cdr data)))
2759     children))
2760
2761 (defmacro gnus-summary-skip-intangible ()
2762   "If the current article is intangible, then jump to a different article."
2763   '(let ((to (get-text-property (point) 'gnus-intangible)))
2764      (and to (gnus-summary-goto-subject to))))
2765
2766 (defmacro gnus-summary-article-intangible-p ()
2767   "Say whether this article is intangible or not."
2768   '(get-text-property (point) 'gnus-intangible))
2769
2770 (defun gnus-article-read-p (article)
2771   "Say whether ARTICLE is read or not."
2772   (not (or (memq article gnus-newsgroup-marked)
2773            (memq article gnus-newsgroup-spam-marked)
2774            (memq article gnus-newsgroup-unreads)
2775            (memq article gnus-newsgroup-unselected)
2776            (memq article gnus-newsgroup-dormant))))
2777
2778 ;; Some summary mode macros.
2779
2780 (defmacro gnus-summary-article-number ()
2781   "The article number of the article on the current line.
2782 If there isn't an article number here, then we return the current
2783 article number."
2784   '(progn
2785      (gnus-summary-skip-intangible)
2786      (or (get-text-property (point) 'gnus-number)
2787          (gnus-summary-last-subject))))
2788
2789 (defmacro gnus-summary-article-header (&optional number)
2790   "Return the header of article NUMBER."
2791   `(gnus-data-header (gnus-data-find
2792                       ,(or number '(gnus-summary-article-number)))))
2793
2794 (defmacro gnus-summary-thread-level (&optional number)
2795   "Return the level of thread that starts with article NUMBER."
2796   `(if (and (eq gnus-summary-make-false-root 'dummy)
2797             (get-text-property (point) 'gnus-intangible))
2798        0
2799      (gnus-data-level (gnus-data-find
2800                        ,(or number '(gnus-summary-article-number))))))
2801
2802 (defmacro gnus-summary-article-mark (&optional number)
2803   "Return the mark of article NUMBER."
2804   `(gnus-data-mark (gnus-data-find
2805                     ,(or number '(gnus-summary-article-number)))))
2806
2807 (defmacro gnus-summary-article-pos (&optional number)
2808   "Return the position of the line of article NUMBER."
2809   `(gnus-data-pos (gnus-data-find
2810                    ,(or number '(gnus-summary-article-number)))))
2811
2812 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2813 (defmacro gnus-summary-article-subject (&optional number)
2814   "Return current subject string or nil if nothing."
2815   `(let ((headers
2816           ,(if number
2817                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2818              '(gnus-data-header (assq (gnus-summary-article-number)
2819                                       gnus-newsgroup-data)))))
2820      (and headers
2821           (vectorp headers)
2822           (mail-header-subject headers))))
2823
2824 (defmacro gnus-summary-article-score (&optional number)
2825   "Return current article score."
2826   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2827                   gnus-newsgroup-scored))
2828        gnus-summary-default-score 0))
2829
2830 (defun gnus-summary-article-children (&optional number)
2831   "Return a list of article numbers that are children of article NUMBER."
2832   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2833          (level (gnus-data-level (car data)))
2834          l children)
2835     (while (and (setq data (cdr data))
2836                 (> (setq l (gnus-data-level (car data))) level))
2837       (and (= (1+ level) l)
2838            (push (gnus-data-number (car data))
2839                  children)))
2840     (nreverse children)))
2841
2842 (defun gnus-summary-article-parent (&optional number)
2843   "Return the article number of the parent of article NUMBER."
2844   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2845                                     (gnus-data-list t)))
2846          (level (gnus-data-level (car data))))
2847     (if (zerop level)
2848         ()                              ; This is a root.
2849       ;; We search until we find an article with a level less than
2850       ;; this one.  That function has to be the parent.
2851       (while (and (setq data (cdr data))
2852                   (not (< (gnus-data-level (car data)) level))))
2853       (and data (gnus-data-number (car data))))))
2854
2855 (defun gnus-unread-mark-p (mark)
2856   "Say whether MARK is the unread mark."
2857   (= mark gnus-unread-mark))
2858
2859 (defun gnus-read-mark-p (mark)
2860   "Say whether MARK is one of the marks that mark as read.
2861 This is all marks except unread, ticked, dormant, and expirable."
2862   (not (or (= mark gnus-unread-mark)
2863            (= mark gnus-ticked-mark)
2864            (= mark gnus-spam-mark)
2865            (= mark gnus-dormant-mark)
2866            (= mark gnus-expirable-mark))))
2867
2868 (defmacro gnus-article-mark (number)
2869   "Return the MARK of article NUMBER.
2870 This macro should only be used when computing the mark the \"first\"
2871 time; i.e., when generating the summary lines.  After that,
2872 `gnus-summary-article-mark' should be used to examine the
2873 marks of articles."
2874   `(cond
2875     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2876     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2877     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2878     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2879     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2880     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2881     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2882     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2883            gnus-ancient-mark))))
2884
2885 ;; Saving hidden threads.
2886
2887 (defmacro gnus-save-hidden-threads (&rest forms)
2888   "Save hidden threads, eval FORMS, and restore the hidden threads."
2889   (let ((config (make-symbol "config")))
2890     `(let ((,config (gnus-hidden-threads-configuration)))
2891        (unwind-protect
2892            (save-excursion
2893              ,@forms)
2894          (gnus-restore-hidden-threads-configuration ,config)))))
2895 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2896 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2897
2898 (defun gnus-data-compute-positions ()
2899   "Compute the positions of all articles."
2900   (setq gnus-newsgroup-data-reverse nil)
2901   (let ((data gnus-newsgroup-data))
2902     (save-excursion
2903       (gnus-save-hidden-threads
2904         (gnus-summary-show-all-threads)
2905         (goto-char (point-min))
2906         (while data
2907           (while (get-text-property (point) 'gnus-intangible)
2908             (forward-line 1))
2909           (gnus-data-set-pos (car data) (+ (point) 3))
2910           (setq data (cdr data))
2911           (forward-line 1))))))
2912
2913 (defun gnus-hidden-threads-configuration ()
2914   "Return the current hidden threads configuration."
2915   (save-excursion
2916     (let (config)
2917       (goto-char (point-min))
2918       (while (search-forward "\r" nil t)
2919         (push (1- (point)) config))
2920       config)))
2921
2922 (defun gnus-restore-hidden-threads-configuration (config)
2923   "Restore hidden threads configuration from CONFIG."
2924   (save-excursion
2925     (let (point buffer-read-only)
2926       (while (setq point (pop config))
2927         (when (and (< point (point-max))
2928                    (goto-char point)
2929                    (eq (char-after) ?\n))
2930           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2931
2932 ;; Various summary mode internalish functions.
2933
2934 (defun gnus-mouse-pick-article (e)
2935   (interactive "e")
2936   (mouse-set-point e)
2937   (gnus-summary-next-page nil t))
2938
2939 (defun gnus-summary-set-display-table ()
2940   "Change the display table.
2941 Odd characters have a tendency to mess
2942 up nicely formatted displays - we make all possible glyphs
2943 display only a single character."
2944
2945   ;; We start from the standard display table, if any.
2946   (let ((table (or (copy-sequence standard-display-table)
2947                    (make-display-table)))
2948         (i 32))
2949     ;; Nix out all the control chars...
2950     (while (>= (setq i (1- i)) 0)
2951       (aset table i [??]))
2952    ;; ... but not newline and cr, of course.  (cr is necessary for the
2953     ;; selective display).
2954     (aset table ?\n nil)
2955     (aset table ?\r nil)
2956     ;; We keep TAB as well.
2957     (aset table ?\t nil)
2958     ;; We nix out any glyphs over 126 that are not set already.
2959     (let ((i 256))
2960       (while (>= (setq i (1- i)) 127)
2961         ;; Only modify if the entry is nil.
2962         (unless (aref table i)
2963           (aset table i [??]))))
2964     (setq buffer-display-table table)))
2965
2966 (defun gnus-summary-set-article-display-arrow (pos)
2967   "Update the overlay arrow to point to line at position POS."
2968   (when (and gnus-summary-display-arrow
2969              (boundp 'overlay-arrow-position)
2970              (boundp 'overlay-arrow-string))
2971     (save-excursion
2972       (goto-char pos)
2973       (beginning-of-line)
2974       (unless overlay-arrow-position
2975         (setq overlay-arrow-position (make-marker)))
2976       (setq overlay-arrow-string "=>"
2977             overlay-arrow-position (set-marker overlay-arrow-position
2978                                                (point)
2979                                                (current-buffer))))))
2980
2981 (defun gnus-summary-buffer-name (group)
2982   "Return the summary buffer name of GROUP."
2983   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2984
2985 (defun gnus-summary-setup-buffer (group)
2986   "Initialize summary buffer."
2987   (let ((buffer (gnus-summary-buffer-name group))
2988         (dead-name (concat "*Dead Summary "
2989                            (gnus-group-decoded-name group) "*")))
2990     ;; If a dead summary buffer exists, we kill it.
2991     (when (gnus-buffer-live-p dead-name)
2992       (gnus-kill-buffer dead-name))
2993     (if (get-buffer buffer)
2994         (progn
2995           (set-buffer buffer)
2996           (setq gnus-summary-buffer (current-buffer))
2997           (not gnus-newsgroup-prepared))
2998       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2999       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3000       (gnus-summary-mode group)
3001       (when gnus-carpal
3002         (gnus-carpal-setup-buffer 'summary))
3003       (unless gnus-single-article-buffer
3004         (make-local-variable 'gnus-article-buffer)
3005         (make-local-variable 'gnus-article-current)
3006         (make-local-variable 'gnus-original-article-buffer))
3007       (setq gnus-newsgroup-name group)
3008       ;; Set any local variables in the group parameters.
3009       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3010       t)))
3011
3012 (defun gnus-set-global-variables ()
3013   "Set the global equivalents of the buffer-local variables.
3014 They are set to the latest values they had.  These reflect the summary
3015 buffer that was in action when the last article was fetched."
3016   (when (eq major-mode 'gnus-summary-mode)
3017     (setq gnus-summary-buffer (current-buffer))
3018     (let ((name gnus-newsgroup-name)
3019           (marked gnus-newsgroup-marked)
3020           (spam gnus-newsgroup-spam-marked)
3021           (unread gnus-newsgroup-unreads)
3022           (headers gnus-current-headers)
3023           (data gnus-newsgroup-data)
3024           (summary gnus-summary-buffer)
3025           (article-buffer gnus-article-buffer)
3026           (original gnus-original-article-buffer)
3027           (gac gnus-article-current)
3028           (reffed gnus-reffed-article-number)
3029           (score-file gnus-current-score-file)
3030           (default-charset gnus-newsgroup-charset)
3031           vlist)
3032       (let ((locals gnus-newsgroup-variables))
3033         (while locals
3034           (if (consp (car locals))
3035               (push (eval (caar locals)) vlist)
3036             (push (eval (car locals)) vlist))
3037           (setq locals (cdr locals)))
3038         (setq vlist (nreverse vlist)))
3039       (save-excursion
3040         (set-buffer gnus-group-buffer)
3041         (setq gnus-newsgroup-name name
3042               gnus-newsgroup-marked marked
3043               gnus-newsgroup-spam-marked spam
3044               gnus-newsgroup-unreads unread
3045               gnus-current-headers headers
3046               gnus-newsgroup-data data
3047               gnus-article-current gac
3048               gnus-summary-buffer summary
3049               gnus-article-buffer article-buffer
3050               gnus-original-article-buffer original
3051               gnus-reffed-article-number reffed
3052               gnus-current-score-file score-file
3053               gnus-newsgroup-charset default-charset)
3054         (let ((locals gnus-newsgroup-variables))
3055           (while locals
3056             (if (consp (car locals))
3057                 (set (caar locals) (pop vlist))
3058               (set (car locals) (pop vlist)))
3059             (setq locals (cdr locals))))
3060         ;; The article buffer also has local variables.
3061         (when (gnus-buffer-live-p gnus-article-buffer)
3062           (set-buffer gnus-article-buffer)
3063           (setq gnus-summary-buffer summary))))))
3064
3065 (defun gnus-summary-article-unread-p (article)
3066   "Say whether ARTICLE is unread or not."
3067   (memq article gnus-newsgroup-unreads))
3068
3069 (defun gnus-summary-first-article-p (&optional article)
3070   "Return whether ARTICLE is the first article in the buffer."
3071   (if (not (setq article (or article (gnus-summary-article-number))))
3072       nil
3073     (eq article (caar gnus-newsgroup-data))))
3074
3075 (defun gnus-summary-last-article-p (&optional article)
3076   "Return whether ARTICLE is the last article in the buffer."
3077   (if (not (setq article (or article (gnus-summary-article-number))))
3078       ;; All non-existent numbers are the last article.  :-)
3079       t
3080     (not (cdr (gnus-data-find-list article)))))
3081
3082 (defun gnus-make-thread-indent-array ()
3083   (let ((n 200))
3084     (unless (and gnus-thread-indent-array
3085                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3086       (setq gnus-thread-indent-array (make-vector 201 "")
3087             gnus-thread-indent-array-level gnus-thread-indent-level)
3088       (while (>= n 0)
3089         (aset gnus-thread-indent-array n
3090               (make-string (* n gnus-thread-indent-level) ? ))
3091         (setq n (1- n))))))
3092
3093 (defun gnus-update-summary-mark-positions ()
3094   "Compute where the summary marks are to go."
3095   (save-excursion
3096     (when (gnus-buffer-exists-p gnus-summary-buffer)
3097       (set-buffer gnus-summary-buffer))
3098     (let ((gnus-replied-mark 129)
3099           (gnus-score-below-mark 130)
3100           (gnus-score-over-mark 130)
3101           (gnus-undownloaded-mark 131)
3102           (spec gnus-summary-line-format-spec)
3103           gnus-visual pos)
3104       (save-excursion
3105         (gnus-set-work-buffer)
3106         (let ((gnus-summary-line-format-spec spec)
3107               (gnus-newsgroup-downloadable '(0)))
3108           (gnus-summary-insert-line
3109            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3110            0 nil t 128 t nil "" nil 1)
3111           (goto-char (point-min))
3112           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3113                                              (- (point) (point-min) 1)))))
3114           (goto-char (point-min))
3115           (push (cons 'replied (and (search-forward "\201" nil t)
3116                                     (- (point) (point-min) 1)))
3117                 pos)
3118           (goto-char (point-min))
3119           (push (cons 'score (and (search-forward "\202" nil t)
3120                                   (- (point) (point-min) 1)))
3121                 pos)
3122           (goto-char (point-min))
3123           (push (cons 'download
3124                       (and (search-forward "\203" nil t)
3125                            (- (point) (point-min) 1)))
3126                 pos)))
3127       (setq gnus-summary-mark-positions pos))))
3128
3129 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3130   "Insert a dummy root in the summary buffer."
3131   (beginning-of-line)
3132   (gnus-add-text-properties
3133    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3134    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3135
3136 (defun gnus-summary-extract-address-component (from)
3137   (or (car (funcall gnus-extract-address-components from))
3138       from))
3139
3140 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3141   (let ((mail-parse-charset gnus-newsgroup-charset)
3142         ; Is it really necessary to do this next part for each summary line?
3143         ; Luckily, doesn't seem to slow things down much.
3144         (mail-parse-ignored-charsets
3145          (save-excursion (set-buffer gnus-summary-buffer)
3146                          gnus-newsgroup-ignored-charsets)))
3147     (or
3148      (and gnus-ignored-from-addresses
3149           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3150           (let ((extra-headers (mail-header-extra header))
3151                 to
3152                 newsgroups)
3153             (cond
3154              ((setq to (cdr (assq 'To extra-headers)))
3155               (concat "-> "
3156                       (inline
3157                         (gnus-summary-extract-address-component
3158                          (funcall gnus-decode-encoded-word-function to)))))
3159              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3160               (concat "=> " newsgroups)))))
3161      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3162
3163 (defun gnus-summary-insert-line (gnus-tmp-header
3164                                  gnus-tmp-level gnus-tmp-current
3165                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3166                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3167                                  &optional gnus-tmp-dummy gnus-tmp-score
3168                                  gnus-tmp-process)
3169   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3170          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3171          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3172          (gnus-tmp-score-char
3173           (if (or (null gnus-summary-default-score)
3174                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3175                       gnus-summary-zcore-fuzz))
3176               ?                         ;Whitespace
3177             (if (< gnus-tmp-score gnus-summary-default-score)
3178                 gnus-score-below-mark gnus-score-over-mark)))
3179          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3180          (gnus-tmp-replied
3181           (cond (gnus-tmp-process gnus-process-mark)
3182                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3183                  gnus-cached-mark)
3184                 (gnus-tmp-replied gnus-replied-mark)
3185                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3186                  gnus-forwarded-mark)
3187                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3188                  gnus-saved-mark)
3189                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3190                  gnus-recent-mark)
3191                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3192                  gnus-unseen-mark)
3193                 (t gnus-no-mark)))
3194          (gnus-tmp-downloaded
3195           (cond (undownloaded 
3196                  gnus-undownloaded-mark)
3197                 (gnus-newsgroup-agentized
3198                  gnus-downloaded-mark)
3199                 (t
3200                  gnus-no-mark)))
3201          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3202          (gnus-tmp-name
3203           (cond
3204            ((string-match "<[^>]+> *$" gnus-tmp-from)
3205             (let ((beg (match-beginning 0)))
3206               (or (and (string-match "^\".+\"" gnus-tmp-from)
3207                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3208                   (substring gnus-tmp-from 0 beg))))
3209            ((string-match "(.+)" gnus-tmp-from)
3210             (substring gnus-tmp-from
3211                        (1+ (match-beginning 0)) (1- (match-end 0))))
3212            (t gnus-tmp-from)))
3213          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3214          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3215          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3216          (buffer-read-only nil))
3217     (when (string= gnus-tmp-name "")
3218       (setq gnus-tmp-name gnus-tmp-from))
3219     (unless (numberp gnus-tmp-lines)
3220       (setq gnus-tmp-lines -1))
3221     (if (= gnus-tmp-lines -1)
3222         (setq gnus-tmp-lines "?")
3223       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3224     (gnus-put-text-property
3225      (point)
3226      (progn (eval gnus-summary-line-format-spec) (point))
3227      'gnus-number gnus-tmp-number)
3228     (when (gnus-visual-p 'summary-highlight 'highlight)
3229       (forward-line -1)
3230       (gnus-run-hooks 'gnus-summary-update-hook)
3231       (forward-line 1))))
3232
3233 (defun gnus-summary-update-line (&optional dont-update)
3234   "Update summary line after change."
3235   (when (and gnus-summary-default-score
3236              (not gnus-summary-inhibit-highlight))
3237     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3238            (article (gnus-summary-article-number))
3239            (score (gnus-summary-article-score article)))
3240       (unless dont-update
3241         (if (and gnus-summary-mark-below
3242                  (< (gnus-summary-article-score)
3243                     gnus-summary-mark-below))
3244             ;; This article has a low score, so we mark it as read.
3245             (when (memq article gnus-newsgroup-unreads)
3246               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3247           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3248             ;; This article was previously marked as read on account
3249             ;; of a low score, but now it has risen, so we mark it as
3250             ;; unread.
3251             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3252         (gnus-summary-update-mark
3253          (if (or (null gnus-summary-default-score)
3254                  (<= (abs (- score gnus-summary-default-score))
3255                      gnus-summary-zcore-fuzz))
3256              ?                          ;Whitespace
3257            (if (< score gnus-summary-default-score)
3258                gnus-score-below-mark gnus-score-over-mark))
3259          'score))
3260       ;; Do visual highlighting.
3261       (when (gnus-visual-p 'summary-highlight 'highlight)
3262         (gnus-run-hooks 'gnus-summary-update-hook)))))
3263
3264 (defvar gnus-tmp-new-adopts nil)
3265
3266 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3267   "Return the number of articles in THREAD.
3268 This may be 0 in some cases -- if none of the articles in
3269 the thread are to be displayed."
3270   (let* ((number
3271          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3272           (cond
3273            ((not (listp thread))
3274             1)
3275            ((and (consp thread) (cdr thread))
3276             (apply
3277              '+ 1 (mapcar
3278                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3279            ((null thread)
3280             1)
3281            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3282             1)
3283            (t 0))))
3284     (when (and level (zerop level) gnus-tmp-new-adopts)
3285       (incf number
3286             (apply '+ (mapcar
3287                        'gnus-summary-number-of-articles-in-thread
3288                        gnus-tmp-new-adopts))))
3289     (if char
3290         (if (> number 1) gnus-not-empty-thread-mark
3291           gnus-empty-thread-mark)
3292       number)))
3293
3294 (defsubst gnus-summary-line-message-size (head)
3295   "Return pretty-printed version of message size.
3296 This function is intended to be used in
3297 `gnus-summary-line-format-alist', which see."
3298   (let ((c (or (mail-header-chars head) -1)))
3299     (cond ((< c 0) "n/a")               ; chars not available
3300           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3301           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3302           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3303           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3304
3305
3306 (defun gnus-summary-set-local-parameters (group)
3307   "Go through the local params of GROUP and set all variable specs in that list."
3308   (let ((params (gnus-group-find-parameter group))
3309         (vars '(quit-config))           ; Ignore quit-config.
3310         elem)
3311     (while params
3312       (setq elem (car params)
3313             params (cdr params))
3314       (and (consp elem)                 ; Has to be a cons.
3315            (consp (cdr elem))           ; The cdr has to be a list.
3316            (symbolp (car elem))         ; Has to be a symbol in there.
3317            (not (memq (car elem) vars))
3318            (ignore-errors               ; So we set it.
3319              (push (car elem) vars)
3320              (make-local-variable (car elem))
3321              (set (car elem) (eval (nth 1 elem))))))))
3322
3323 (defun gnus-summary-read-group (group &optional show-all no-article
3324                                       kill-buffer no-display backward
3325                                       select-articles)
3326   "Start reading news in newsgroup GROUP.
3327 If SHOW-ALL is non-nil, already read articles are also listed.
3328 If NO-ARTICLE is non-nil, no article is selected initially.
3329 If NO-DISPLAY, don't generate a summary buffer."
3330   (let (result)
3331     (while (and group
3332                 (null (setq result
3333                             (let ((gnus-auto-select-next nil))
3334                               (or (gnus-summary-read-group-1
3335                                    group show-all no-article
3336                                    kill-buffer no-display
3337                                    select-articles)
3338                                   (setq show-all nil
3339                                         select-articles nil)))))
3340                 (eq gnus-auto-select-next 'quietly))
3341       (set-buffer gnus-group-buffer)
3342       ;; The entry function called above goes to the next
3343       ;; group automatically, so we go two groups back
3344       ;; if we are searching for the previous group.
3345       (when backward
3346         (gnus-group-prev-unread-group 2))
3347       (if (not (equal group (gnus-group-group-name)))
3348           (setq group (gnus-group-group-name))
3349         (setq group nil)))
3350     result))
3351
3352 (defun gnus-summary-read-group-1 (group show-all no-article
3353                                         kill-buffer no-display
3354                                         &optional select-articles)
3355   ;; Killed foreign groups can't be entered.
3356   ;;  (when (and (not (gnus-group-native-p group))
3357   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3358   ;;    (error "Dead non-native groups can't be entered"))
3359   (gnus-message 5 "Retrieving newsgroup: %s..."
3360                 (gnus-group-decoded-name group))
3361   (let* ((new-group (gnus-summary-setup-buffer group))
3362          (quit-config (gnus-group-quit-config group))
3363          (did-select (and new-group (gnus-select-newsgroup
3364                                      group show-all select-articles))))
3365     (cond
3366      ;; This summary buffer exists already, so we just select it.
3367      ((not new-group)
3368       (gnus-set-global-variables)
3369       (when kill-buffer
3370         (gnus-kill-or-deaden-summary kill-buffer))
3371       (gnus-configure-windows 'summary 'force)
3372       (gnus-set-mode-line 'summary)
3373       (gnus-summary-position-point)
3374       (message "")
3375       t)
3376      ;; We couldn't select this group.
3377      ((null did-select)
3378       (when (and (eq major-mode 'gnus-summary-mode)
3379                  (not (equal (current-buffer) kill-buffer)))
3380         (kill-buffer (current-buffer))
3381         (if (not quit-config)
3382             (progn
3383               ;; Update the info -- marks might need to be removed,
3384               ;; for instance.
3385               (gnus-summary-update-info)
3386               (set-buffer gnus-group-buffer)
3387               (gnus-group-jump-to-group group)
3388               (gnus-group-next-unread-group 1))
3389           (gnus-handle-ephemeral-exit quit-config)))
3390       (let ((grpinfo (gnus-get-info group)))
3391         (if (null (gnus-info-read grpinfo))
3392             (gnus-message 3 "Group %s contains no messages"
3393                           (gnus-group-decoded-name group))
3394           (gnus-message 3 "Can't select group")))
3395       nil)
3396      ;; The user did a `C-g' while prompting for number of articles,
3397      ;; so we exit this group.
3398      ((eq did-select 'quit)
3399       (and (eq major-mode 'gnus-summary-mode)
3400            (not (equal (current-buffer) kill-buffer))
3401            (kill-buffer (current-buffer)))
3402       (when kill-buffer
3403         (gnus-kill-or-deaden-summary kill-buffer))
3404       (if (not quit-config)
3405           (progn
3406             (set-buffer gnus-group-buffer)
3407             (gnus-group-jump-to-group group)
3408             (gnus-group-next-unread-group 1)
3409             (gnus-configure-windows 'group 'force))
3410         (gnus-handle-ephemeral-exit quit-config))
3411       ;; Finally signal the quit.
3412       (signal 'quit nil))
3413      ;; The group was successfully selected.
3414      (t
3415       (gnus-set-global-variables)
3416       ;; Save the active value in effect when the group was entered.
3417       (setq gnus-newsgroup-active
3418             (gnus-copy-sequence
3419              (gnus-active gnus-newsgroup-name)))
3420       ;; You can change the summary buffer in some way with this hook.
3421       (gnus-run-hooks 'gnus-select-group-hook)
3422       (gnus-update-format-specifications
3423        nil 'summary 'summary-mode 'summary-dummy)
3424       (gnus-update-summary-mark-positions)
3425       ;; Do score processing.
3426       (when gnus-use-scoring
3427         (gnus-possibly-score-headers))
3428       ;; Check whether to fill in the gaps in the threads.
3429       (when gnus-build-sparse-threads
3430         (gnus-build-sparse-threads))
3431       ;; Find the initial limit.
3432       (if gnus-show-threads
3433           (if show-all
3434               (let ((gnus-newsgroup-dormant nil))
3435                 (gnus-summary-initial-limit show-all))
3436             (gnus-summary-initial-limit show-all))
3437         ;; When unthreaded, all articles are always shown.
3438         (setq gnus-newsgroup-limit
3439               (mapcar
3440                (lambda (header) (mail-header-number header))
3441                gnus-newsgroup-headers)))
3442       ;; Generate the summary buffer.
3443       (unless no-display
3444         (gnus-summary-prepare))
3445       (when gnus-use-trees
3446         (gnus-tree-open group)
3447         (setq gnus-summary-highlight-line-function
3448               'gnus-tree-highlight-article))
3449       ;; If the summary buffer is empty, but there are some low-scored
3450       ;; articles or some excluded dormants, we include these in the
3451       ;; buffer.
3452       (when (and (zerop (buffer-size))
3453                  (not no-display))
3454         (cond (gnus-newsgroup-dormant
3455                (gnus-summary-limit-include-dormant))
3456               ((and gnus-newsgroup-scored show-all)
3457                (gnus-summary-limit-include-expunged t))))
3458       ;; Function `gnus-apply-kill-file' must be called in this hook.
3459       (gnus-run-hooks 'gnus-apply-kill-hook)
3460       (if (and (zerop (buffer-size))
3461                (not no-display))
3462           (progn
3463             ;; This newsgroup is empty.
3464             (gnus-summary-catchup-and-exit nil t)
3465             (gnus-message 6 "No unread news")
3466             (when kill-buffer
3467               (gnus-kill-or-deaden-summary kill-buffer))
3468             ;; Return nil from this function.
3469             nil)
3470         ;; Hide conversation thread subtrees.  We cannot do this in
3471         ;; gnus-summary-prepare-hook since kill processing may not
3472         ;; work with hidden articles.
3473         (gnus-summary-maybe-hide-threads)
3474         (when kill-buffer
3475           (gnus-kill-or-deaden-summary kill-buffer))
3476         (gnus-summary-auto-select-subject)
3477         ;; Show first unread article if requested.
3478         (if (and (not no-article)
3479                  (not no-display)
3480                  gnus-newsgroup-unreads
3481                  gnus-auto-select-first)
3482             (progn
3483               (gnus-configure-windows 'summary)
3484               (let ((art (gnus-summary-article-number)))
3485                 (unless (and (not gnus-plugged)
3486                              (or (memq art gnus-newsgroup-undownloaded)
3487                                  (memq art gnus-newsgroup-downloadable)))
3488                   (gnus-summary-goto-article art))))
3489           ;; Don't select any articles.
3490           (gnus-summary-position-point)
3491           (gnus-configure-windows 'summary 'force)
3492           (gnus-set-mode-line 'summary))
3493         (when (get-buffer-window gnus-group-buffer t)
3494           ;; Gotta use windows, because recenter does weird stuff if
3495           ;; the current buffer ain't the displayed window.
3496           (let ((owin (selected-window)))
3497             (select-window (get-buffer-window gnus-group-buffer t))
3498             (when (gnus-group-goto-group group)
3499               (recenter))
3500             (select-window owin)))
3501         ;; Mark this buffer as "prepared".
3502         (setq gnus-newsgroup-prepared t)
3503         (gnus-run-hooks 'gnus-summary-prepared-hook)
3504         (unless (gnus-ephemeral-group-p group)
3505           (gnus-group-update-group group))
3506         t)))))
3507
3508 (defun gnus-summary-auto-select-subject ()
3509   "Select the subject line on initial group entry."
3510   (goto-char (point-min))
3511   (cond
3512    ((eq gnus-auto-select-subject 'best)
3513     (gnus-summary-best-unread-subject))
3514    ((eq gnus-auto-select-subject 'unread)
3515     (gnus-summary-first-unread-subject))
3516    ((eq gnus-auto-select-subject 'unseen)
3517     (gnus-summary-first-unseen-subject))
3518    ((eq gnus-auto-select-subject 'unseen-or-unread)
3519     (gnus-summary-first-unseen-or-unread-subject))
3520    ((eq gnus-auto-select-subject 'first)
3521     ;; Do nothing.
3522     )
3523    ((gnus-functionp gnus-auto-select-subject)
3524     (funcall gnus-auto-select-subject))))
3525
3526 (defun gnus-summary-prepare ()
3527   "Generate the summary buffer."
3528   (interactive)
3529   (let ((buffer-read-only nil))
3530     (erase-buffer)
3531     (setq gnus-newsgroup-data nil
3532           gnus-newsgroup-data-reverse nil)
3533     (gnus-run-hooks 'gnus-summary-generate-hook)
3534     ;; Generate the buffer, either with threads or without.
3535     (when gnus-newsgroup-headers
3536       (gnus-summary-prepare-threads
3537        (if gnus-show-threads
3538            (gnus-sort-gathered-threads
3539             (funcall gnus-summary-thread-gathering-function
3540                      (gnus-sort-threads
3541                       (gnus-cut-threads (gnus-make-threads)))))
3542          ;; Unthreaded display.
3543          (gnus-sort-articles gnus-newsgroup-headers))))
3544     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3545     ;; Call hooks for modifying summary buffer.
3546     (goto-char (point-min))
3547     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3548
3549 (defsubst gnus-general-simplify-subject (subject)
3550   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3551   (setq subject
3552         (cond
3553          ;; Truncate the subject.
3554          (gnus-simplify-subject-functions
3555           (gnus-map-function gnus-simplify-subject-functions subject))
3556          ((numberp gnus-summary-gather-subject-limit)
3557           (setq subject (gnus-simplify-subject-re subject))
3558           (if (> (length subject) gnus-summary-gather-subject-limit)
3559               (substring subject 0 gnus-summary-gather-subject-limit)
3560             subject))
3561          ;; Fuzzily simplify it.
3562          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3563           (gnus-simplify-subject-fuzzy subject))
3564          ;; Just remove the leading "Re:".
3565          (t
3566           (gnus-simplify-subject-re subject))))
3567
3568   (if (and gnus-summary-gather-exclude-subject
3569            (string-match gnus-summary-gather-exclude-subject subject))
3570       nil                         ; This article shouldn't be gathered
3571     subject))
3572
3573 (defun gnus-summary-simplify-subject-query ()
3574   "Query where the respool algorithm would put this article."
3575   (interactive)
3576   (gnus-summary-select-article)
3577   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3578
3579 (defun gnus-gather-threads-by-subject (threads)
3580   "Gather threads by looking at Subject headers."
3581   (if (not gnus-summary-make-false-root)
3582       threads
3583     (let ((hashtb (gnus-make-hashtable 1024))
3584           (prev threads)
3585           (result threads)
3586           subject hthread whole-subject)
3587       (while threads
3588         (setq subject (gnus-general-simplify-subject
3589                        (setq whole-subject (mail-header-subject
3590                                             (caar threads)))))
3591         (when subject
3592           (if (setq hthread (gnus-gethash subject hashtb))
3593               (progn
3594                 ;; We enter a dummy root into the thread, if we
3595                 ;; haven't done that already.
3596                 (unless (stringp (caar hthread))
3597                   (setcar hthread (list whole-subject (car hthread))))
3598                 ;; We add this new gathered thread to this gathered
3599                 ;; thread.
3600                 (setcdr (car hthread)
3601                         (nconc (cdar hthread) (list (car threads))))
3602                 ;; Remove it from the list of threads.
3603                 (setcdr prev (cdr threads))
3604                 (setq threads prev))
3605             ;; Enter this thread into the hash table.
3606             (gnus-sethash subject
3607                           (if gnus-summary-make-false-root-always
3608                               (progn
3609                                 ;; If you want a dummy root above all
3610                                 ;; threads...
3611                                 (setcar threads (list whole-subject
3612                                                       (car threads)))
3613                                 threads)
3614                             threads)
3615                           hashtb)))
3616         (setq prev threads)
3617         (setq threads (cdr threads)))
3618       result)))
3619
3620 (defun gnus-gather-threads-by-references (threads)
3621   "Gather threads by looking at References headers."
3622   (let ((idhashtb (gnus-make-hashtable 1024))
3623         (thhashtb (gnus-make-hashtable 1024))
3624         (prev threads)
3625         (result threads)
3626         ids references id gthread gid entered ref)
3627     (while threads
3628       (when (setq references (mail-header-references (caar threads)))
3629         (setq id (mail-header-id (caar threads))
3630               ids (inline (gnus-split-references references))
3631               entered nil)
3632         (while (setq ref (pop ids))
3633           (setq ids (delete ref ids))
3634           (if (not (setq gid (gnus-gethash ref idhashtb)))
3635               (progn
3636                 (gnus-sethash ref id idhashtb)
3637                 (gnus-sethash id threads thhashtb))
3638             (setq gthread (gnus-gethash gid thhashtb))
3639             (unless entered
3640               ;; We enter a dummy root into the thread, if we
3641               ;; haven't done that already.
3642               (unless (stringp (caar gthread))
3643                 (setcar gthread (list (mail-header-subject (caar gthread))
3644                                       (car gthread))))
3645               ;; We add this new gathered thread to this gathered
3646               ;; thread.
3647               (setcdr (car gthread)
3648                       (nconc (cdar gthread) (list (car threads)))))
3649             ;; Add it into the thread hash table.
3650             (gnus-sethash id gthread thhashtb)
3651             (setq entered t)
3652             ;; Remove it from the list of threads.
3653             (setcdr prev (cdr threads))
3654             (setq threads prev))))
3655       (setq prev threads)
3656       (setq threads (cdr threads)))
3657     result))
3658
3659 (defun gnus-sort-gathered-threads (threads)
3660   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3661   (let ((result threads))
3662     (while threads
3663       (when (stringp (caar threads))
3664         (setcdr (car threads)
3665                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3666       (setq threads (cdr threads)))
3667     result))
3668
3669 (defun gnus-thread-loop-p (root thread)
3670   "Say whether ROOT is in THREAD."
3671   (let ((stack (list thread))
3672         (infloop 0)
3673         th)
3674     (while (setq thread (pop stack))
3675       (setq th (cdr thread))
3676       (while (and th
3677                   (not (eq (caar th) root)))
3678         (pop th))
3679       (if th
3680           ;; We have found a loop.
3681           (let (ref-dep)
3682             (setcdr thread (delq (car th) (cdr thread)))
3683             (if (boundp (setq ref-dep (intern "none"
3684                                               gnus-newsgroup-dependencies)))
3685                 (setcdr (symbol-value ref-dep)
3686                         (nconc (cdr (symbol-value ref-dep))
3687                                (list (car th))))
3688               (set ref-dep (list nil (car th))))
3689             (setq infloop 1
3690                   stack nil))
3691         ;; Push all the subthreads onto the stack.
3692         (push (cdr thread) stack)))
3693     infloop))
3694
3695 (defun gnus-make-threads ()
3696   "Go through the dependency hashtb and find the roots.  Return all threads."
3697   (let (threads)
3698     (while (catch 'infloop
3699              (mapatoms
3700               (lambda (refs)
3701                 ;; Deal with self-referencing References loops.
3702                 (when (and (car (symbol-value refs))
3703                            (not (zerop
3704                                  (apply
3705                                   '+
3706                                   (mapcar
3707                                    (lambda (thread)
3708                                      (gnus-thread-loop-p
3709                                       (car (symbol-value refs)) thread))
3710                                    (cdr (symbol-value refs)))))))
3711                   (setq threads nil)
3712                   (throw 'infloop t))
3713                 (unless (car (symbol-value refs))
3714                   ;; These threads do not refer back to any other
3715                   ;; articles, so they're roots.
3716                   (setq threads (append (cdr (symbol-value refs)) threads))))
3717               gnus-newsgroup-dependencies)))
3718     threads))
3719
3720 ;; Build the thread tree.
3721 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3722   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3723
3724 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3725 if it was already present.
3726
3727 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3728 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3729 Message-IDs will be renamed to a unique Message-ID before being
3730 entered.
3731
3732 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3733   (let* ((id (mail-header-id header))
3734          (id-dep (and id (intern id dependencies)))
3735          parent-id ref ref-dep ref-header replaced)
3736     ;; Enter this `header' in the `dependencies' table.
3737     (cond
3738      ((not id-dep)
3739       (setq header nil))
3740      ;; The first two cases do the normal part: enter a new `header'
3741      ;; in the `dependencies' table.
3742      ((not (boundp id-dep))
3743       (set id-dep (list header)))
3744      ((null (car (symbol-value id-dep)))
3745       (setcar (symbol-value id-dep) header))
3746
3747      ;; From here the `header' was already present in the
3748      ;; `dependencies' table.
3749      (force-new
3750       ;; Overrides an existing entry;
3751       ;; just set the header part of the entry.
3752       (setcar (symbol-value id-dep) header)
3753       (setq replaced t))
3754
3755      ;; Renames the existing `header' to a unique Message-ID.
3756      ((not gnus-summary-ignore-duplicates)
3757       ;; An article with this Message-ID has already been seen.
3758       ;; We rename the Message-ID.
3759       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3760            (list header))
3761       (mail-header-set-id header id))
3762
3763      ;; The last case ignores an existing entry, except it adds any
3764      ;; additional Xrefs (in case the two articles came from different
3765      ;; servers.
3766      ;; Also sets `header' to `nil' meaning that the `dependencies'
3767      ;; table was *not* modified.
3768      (t
3769       (mail-header-set-xref
3770        (car (symbol-value id-dep))
3771        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3772                    "")
3773                (or (mail-header-xref header) "")))
3774       (setq header nil)))
3775
3776     (when (and header (not replaced))
3777       ;; First check that we are not creating a References loop.
3778       (setq parent-id (gnus-parent-id (mail-header-references header)))
3779       (setq ref parent-id)
3780       (while (and ref
3781                   (setq ref-dep (intern-soft ref dependencies))
3782                   (boundp ref-dep)
3783                   (setq ref-header (car (symbol-value ref-dep))))
3784         (if (string= id ref)
3785             ;; Yuk!  This is a reference loop.  Make the article be a
3786             ;; root article.
3787             (progn
3788               (mail-header-set-references (car (symbol-value id-dep)) "none")
3789               (setq ref nil)
3790               (setq parent-id nil))
3791           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3792       (setq ref-dep (intern (or parent-id "none") dependencies))
3793       (if (boundp ref-dep)
3794           (setcdr (symbol-value ref-dep)
3795                   (nconc (cdr (symbol-value ref-dep))
3796                          (list (symbol-value id-dep))))
3797         (set ref-dep (list nil (symbol-value id-dep)))))
3798     header))
3799
3800 (defun gnus-extract-message-id-from-in-reply-to (string)
3801   (if (string-match "<[^>]+>" string)
3802       (substring string (match-beginning 0) (match-end 0))
3803     nil))
3804
3805 (defun gnus-build-sparse-threads ()
3806   (let ((headers gnus-newsgroup-headers)
3807         (mail-parse-charset gnus-newsgroup-charset)
3808         (gnus-summary-ignore-duplicates t)
3809         header references generation relations
3810         subject child end new-child date)
3811     ;; First we create an alist of generations/relations, where
3812     ;; generations is how much we trust the relation, and the relation
3813     ;; is parent/child.
3814     (gnus-message 7 "Making sparse threads...")
3815     (save-excursion
3816       (nnheader-set-temp-buffer " *gnus sparse threads*")
3817       (while (setq header (pop headers))
3818         (when (and (setq references (mail-header-references header))
3819                    (not (string= references "")))
3820           (insert references)
3821           (setq child (mail-header-id header)
3822                 subject (mail-header-subject header)
3823                 date (mail-header-date header)
3824                 generation 0)
3825           (while (search-backward ">" nil t)
3826             (setq end (1+ (point)))
3827             (when (search-backward "<" nil t)
3828               (setq new-child (buffer-substring (point) end))
3829               (push (list (incf generation)
3830                           child (setq child new-child)
3831                           subject date)
3832                     relations)))
3833           (when child
3834             (push (list (1+ generation) child nil subject) relations))
3835           (erase-buffer)))
3836       (kill-buffer (current-buffer)))
3837     ;; Sort over trustworthiness.
3838     (mapcar
3839      (lambda (relation)
3840        (when (gnus-dependencies-add-header
3841               (make-full-mail-header
3842                gnus-reffed-article-number
3843                (nth 3 relation) "" (or (nth 4 relation) "")
3844                (nth 1 relation)
3845                (or (nth 2 relation) "") 0 0 "")
3846               gnus-newsgroup-dependencies nil)
3847          (push gnus-reffed-article-number gnus-newsgroup-limit)
3848          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3849          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3850                gnus-newsgroup-reads)
3851          (decf gnus-reffed-article-number)))
3852      (sort relations 'car-less-than-car))
3853     (gnus-message 7 "Making sparse threads...done")))
3854
3855 (defun gnus-build-old-threads ()
3856   ;; Look at all the articles that refer back to old articles, and
3857   ;; fetch the headers for the articles that aren't there.  This will
3858   ;; build complete threads - if the roots haven't been expired by the
3859   ;; server, that is.
3860   (let ((mail-parse-charset gnus-newsgroup-charset)
3861         id heads)
3862     (mapatoms
3863      (lambda (refs)
3864        (when (not (car (symbol-value refs)))
3865          (setq heads (cdr (symbol-value refs)))
3866          (while heads
3867            (if (memq (mail-header-number (caar heads))
3868                      gnus-newsgroup-dormant)
3869                (setq heads (cdr heads))
3870              (setq id (symbol-name refs))
3871              (while (and (setq id (gnus-build-get-header id))
3872                          (not (car (gnus-id-to-thread id)))))
3873              (setq heads nil)))))
3874      gnus-newsgroup-dependencies)))
3875
3876 ;; This function has to be called with point after the article number
3877 ;; on the beginning of the line.
3878 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3879   (let ((eol (gnus-point-at-eol))
3880         (buffer (current-buffer))
3881         header references in-reply-to)
3882
3883     ;; overview: [num subject from date id refs chars lines misc]
3884     (unwind-protect
3885         (let (x)
3886           (narrow-to-region (point) eol)
3887           (unless (eobp)
3888             (forward-char))
3889
3890           (setq header
3891                 (make-full-mail-header
3892                  number                 ; number
3893                  (condition-case ()     ; subject
3894                      (funcall gnus-decode-encoded-word-function
3895                               (setq x (nnheader-nov-field)))
3896                    (error x))
3897                  (condition-case ()     ; from
3898                      (funcall gnus-decode-encoded-word-function
3899                               (setq x (nnheader-nov-field)))
3900                    (error x))
3901                  (nnheader-nov-field)   ; date
3902                  (nnheader-nov-read-message-id) ; id
3903                  (setq references (nnheader-nov-field)) ; refs
3904                  (nnheader-nov-read-integer) ; chars
3905                  (nnheader-nov-read-integer) ; lines
3906                  (unless (eobp)
3907                    (if (looking-at "Xref: ")
3908                        (goto-char (match-end 0)))
3909                    (nnheader-nov-field)) ; Xref
3910                  (nnheader-nov-parse-extra)))) ; extra
3911
3912       (widen))
3913
3914     (when (and (string= references "")
3915                (setq in-reply-to (mail-header-extra header))
3916                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3917       (mail-header-set-references
3918        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3919
3920     (when gnus-alter-header-function
3921       (funcall gnus-alter-header-function header))
3922     (gnus-dependencies-add-header header dependencies force-new)))
3923
3924 (defun gnus-build-get-header (id)
3925   "Look through the buffer of NOV lines and find the header to ID.
3926 Enter this line into the dependencies hash table, and return
3927 the id of the parent article (if any)."
3928   (let ((deps gnus-newsgroup-dependencies)
3929         found header)
3930     (prog1
3931         (save-excursion
3932           (set-buffer nntp-server-buffer)
3933           (let ((case-fold-search nil))
3934             (goto-char (point-min))
3935             (while (and (not found)
3936                         (search-forward id nil t))
3937               (beginning-of-line)
3938               (setq found (looking-at
3939                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3940                                    (regexp-quote id))))
3941               (or found (beginning-of-line 2)))
3942             (when found
3943               (beginning-of-line)
3944               (and
3945                (setq header (gnus-nov-parse-line
3946                              (read (current-buffer)) deps))
3947                (gnus-parent-id (mail-header-references header))))))
3948       (when header
3949         (let ((number (mail-header-number header)))
3950           (push number gnus-newsgroup-limit)
3951           (push header gnus-newsgroup-headers)
3952           (if (memq number gnus-newsgroup-unselected)
3953               (progn
3954                 (setq gnus-newsgroup-unreads
3955                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3956                                                number))
3957                 (setq gnus-newsgroup-unselected
3958                       (delq number gnus-newsgroup-unselected)))
3959             (push number gnus-newsgroup-ancient)))))))
3960
3961 (defun gnus-build-all-threads ()
3962   "Read all the headers."
3963   (let ((gnus-summary-ignore-duplicates t)
3964         (mail-parse-charset gnus-newsgroup-charset)
3965         (dependencies gnus-newsgroup-dependencies)
3966         header article)
3967     (save-excursion
3968       (set-buffer nntp-server-buffer)
3969       (let ((case-fold-search nil))
3970         (goto-char (point-min))
3971         (while (not (eobp))
3972           (ignore-errors
3973             (setq article (read (current-buffer))
3974                   header (gnus-nov-parse-line article dependencies)))
3975           (when header
3976             (save-excursion
3977               (set-buffer gnus-summary-buffer)
3978               (push header gnus-newsgroup-headers)
3979               (if (memq (setq article (mail-header-number header))
3980                         gnus-newsgroup-unselected)
3981                   (progn
3982                     (setq gnus-newsgroup-unreads
3983                           (gnus-add-to-sorted-list
3984                            gnus-newsgroup-unreads article))
3985                     (setq gnus-newsgroup-unselected
3986                           (delq article gnus-newsgroup-unselected)))
3987                 (push article gnus-newsgroup-ancient)))
3988             (forward-line 1)))))))
3989
3990 (defun gnus-summary-update-article-line (article header)
3991   "Update the line for ARTICLE using HEADERS."
3992   (let* ((id (mail-header-id header))
3993          (thread (gnus-id-to-thread id)))
3994     (unless thread
3995       (error "Article in no thread"))
3996     ;; Update the thread.
3997     (setcar thread header)
3998     (gnus-summary-goto-subject article)
3999     (let* ((datal (gnus-data-find-list article))
4000            (data (car datal))
4001            (length (when (cdr datal)
4002                      (- (gnus-data-pos data)
4003                         (gnus-data-pos (cadr datal)))))
4004            (buffer-read-only nil)
4005            (level (gnus-summary-thread-level)))
4006       (gnus-delete-line)
4007       (gnus-summary-insert-line
4008        header level nil 
4009        (memq article gnus-newsgroup-undownloaded)
4010        (gnus-article-mark article)
4011        (memq article gnus-newsgroup-replied)
4012        (memq article gnus-newsgroup-expirable)
4013        ;; Only insert the Subject string when it's different
4014        ;; from the previous Subject string.
4015        (if (and
4016             gnus-show-threads
4017             (gnus-subject-equal
4018              (condition-case ()
4019                  (mail-header-subject
4020                   (gnus-data-header
4021                    (cadr
4022                     (gnus-data-find-list
4023                      article
4024                      (gnus-data-list t)))))
4025                ;; Error on the side of excessive subjects.
4026                (error ""))
4027              (mail-header-subject header)))
4028            ""
4029          (mail-header-subject header))
4030        nil (cdr (assq article gnus-newsgroup-scored))
4031        (memq article gnus-newsgroup-processable))
4032       (when length
4033         (gnus-data-update-list
4034          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4035
4036 (defun gnus-summary-update-article (article &optional iheader)
4037   "Update ARTICLE in the summary buffer."
4038   (set-buffer gnus-summary-buffer)
4039   (let* ((header (gnus-summary-article-header article))
4040          (id (mail-header-id header))
4041          (data (gnus-data-find article))
4042          (thread (gnus-id-to-thread id))
4043          (references (mail-header-references header))
4044          (parent
4045           (gnus-id-to-thread
4046            (or (gnus-parent-id
4047                 (when (and references
4048                            (not (equal "" references)))
4049                   references))
4050                "none")))
4051          (buffer-read-only nil)
4052          (old (car thread)))
4053     (when thread
4054       (unless iheader
4055         (setcar thread nil)
4056         (when parent
4057           (delq thread parent)))
4058       (if (gnus-summary-insert-subject id header)
4059           ;; Set the (possibly) new article number in the data structure.
4060           (gnus-data-set-number data (gnus-id-to-article id))
4061         (setcar thread old)
4062         nil))))
4063
4064 (defun gnus-rebuild-thread (id &optional line)
4065   "Rebuild the thread containing ID.
4066 If LINE, insert the rebuilt thread starting on line LINE."
4067   (let ((buffer-read-only nil)
4068         old-pos current thread data)
4069     (if (not gnus-show-threads)
4070         (setq thread (list (car (gnus-id-to-thread id))))
4071       ;; Get the thread this article is part of.
4072       (setq thread (gnus-remove-thread id)))
4073     (setq old-pos (gnus-point-at-bol))
4074     (setq current (save-excursion
4075                     (and (re-search-backward "[\r\n]" nil t)
4076                          (gnus-summary-article-number))))
4077     ;; If this is a gathered thread, we have to go some re-gathering.
4078     (when (stringp (car thread))
4079       (let ((subject (car thread))
4080             roots thr)
4081         (setq thread (cdr thread))
4082         (while thread
4083           (unless (memq (setq thr (gnus-id-to-thread
4084                                    (gnus-root-id
4085                                     (mail-header-id (caar thread)))))
4086                         roots)
4087             (push thr roots))
4088           (setq thread (cdr thread)))
4089         ;; We now have all (unique) roots.
4090         (if (= (length roots) 1)
4091             ;; All the loose roots are now one solid root.
4092             (setq thread (car roots))
4093           (setq thread (cons subject (gnus-sort-threads roots))))))
4094     (let (threads)
4095       ;; We then insert this thread into the summary buffer.
4096       (when line
4097         (goto-char (point-min))
4098         (forward-line (1- line)))
4099       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4100         (if gnus-show-threads
4101             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4102           (gnus-summary-prepare-unthreaded thread))
4103         (setq data (nreverse gnus-newsgroup-data))
4104         (setq threads gnus-newsgroup-threads))
4105       ;; We splice the new data into the data structure.
4106       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4107       ;;!!! then we want to insert at the beginning of the buffer.
4108       ;;!!! That happens to be true with Gnus now, but that may
4109       ;;!!! change in the future.  Perhaps.
4110       (gnus-data-enter-list
4111        (if line nil current) data (- (point) old-pos))
4112       (setq gnus-newsgroup-threads
4113             (nconc threads gnus-newsgroup-threads))
4114       (gnus-data-compute-positions))))
4115
4116 (defun gnus-number-to-header (number)
4117   "Return the header for article NUMBER."
4118   (let ((headers gnus-newsgroup-headers))
4119     (while (and headers
4120                 (not (= number (mail-header-number (car headers)))))
4121       (pop headers))
4122     (when headers
4123       (car headers))))
4124
4125 (defun gnus-parent-headers (in-headers &optional generation)
4126   "Return the headers of the GENERATIONeth parent of HEADERS."
4127   (unless generation
4128     (setq generation 1))
4129   (let ((parent t)
4130         (headers in-headers)
4131         references)
4132     (while (and parent
4133                 (not (zerop generation))
4134                 (setq references (mail-header-references headers)))
4135       (setq headers (if (and references
4136                              (setq parent (gnus-parent-id references)))
4137                         (car (gnus-id-to-thread parent))
4138                       nil))
4139       (decf generation))
4140     (and (not (eq headers in-headers))
4141          headers)))
4142
4143 (defun gnus-id-to-thread (id)
4144   "Return the (sub-)thread where ID appears."
4145   (gnus-gethash id gnus-newsgroup-dependencies))
4146
4147 (defun gnus-id-to-article (id)
4148   "Return the article number of ID."
4149   (let ((thread (gnus-id-to-thread id)))
4150     (when (and thread
4151                (car thread))
4152       (mail-header-number (car thread)))))
4153
4154 (defun gnus-id-to-header (id)
4155   "Return the article headers of ID."
4156   (car (gnus-id-to-thread id)))
4157
4158 (defun gnus-article-displayed-root-p (article)
4159   "Say whether ARTICLE is a root(ish) article."
4160   (let ((level (gnus-summary-thread-level article))
4161         (refs (mail-header-references  (gnus-summary-article-header article)))
4162         particle)
4163     (cond
4164      ((null level) nil)
4165      ((zerop level) t)
4166      ((null refs) t)
4167      ((null (gnus-parent-id refs)) t)
4168      ((and (= 1 level)
4169            (null (setq particle (gnus-id-to-article
4170                                  (gnus-parent-id refs))))
4171            (null (gnus-summary-thread-level particle)))))))
4172
4173 (defun gnus-root-id (id)
4174   "Return the id of the root of the thread where ID appears."
4175   (let (last-id prev)
4176     (while (and id (setq prev (car (gnus-id-to-thread id))))
4177       (setq last-id id
4178             id (gnus-parent-id (mail-header-references prev))))
4179     last-id))
4180
4181 (defun gnus-articles-in-thread (thread)
4182   "Return the list of articles in THREAD."
4183   (cons (mail-header-number (car thread))
4184         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4185
4186 (defun gnus-remove-thread (id &optional dont-remove)
4187   "Remove the thread that has ID in it."
4188   (let (headers thread last-id)
4189     ;; First go up in this thread until we find the root.
4190     (setq last-id (gnus-root-id id)
4191           headers (message-flatten-list (gnus-id-to-thread last-id)))
4192     ;; We have now found the real root of this thread.  It might have
4193     ;; been gathered into some loose thread, so we have to search
4194     ;; through the threads to find the thread we wanted.
4195     (let ((threads gnus-newsgroup-threads)
4196           sub)
4197       (while threads
4198         (setq sub (car threads))
4199         (if (stringp (car sub))
4200             ;; This is a gathered thread, so we look at the roots
4201             ;; below it to find whether this article is in this
4202             ;; gathered root.
4203             (progn
4204               (setq sub (cdr sub))
4205               (while sub
4206                 (when (member (caar sub) headers)
4207                   (setq thread (car threads)
4208                         threads nil
4209                         sub nil))
4210                 (setq sub (cdr sub))))
4211           ;; It's an ordinary thread, so we check it.
4212           (when (eq (car sub) (car headers))
4213             (setq thread sub
4214                   threads nil)))
4215         (setq threads (cdr threads)))
4216       ;; If this article is in no thread, then it's a root.
4217       (if thread
4218           (unless dont-remove
4219             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4220         (setq thread (gnus-id-to-thread last-id)))
4221       (when thread
4222         (prog1
4223             thread                      ; We return this thread.
4224           (unless dont-remove
4225             (if (stringp (car thread))
4226                 (progn
4227                   ;; If we use dummy roots, then we have to remove the
4228                   ;; dummy root as well.
4229                   (when (eq gnus-summary-make-false-root 'dummy)
4230                     ;; We go to the dummy root by going to
4231                     ;; the first sub-"thread", and then one line up.
4232                     (gnus-summary-goto-article
4233                      (mail-header-number (caadr thread)))
4234                     (forward-line -1)
4235                     (gnus-delete-line)
4236                     (gnus-data-compute-positions))
4237                   (setq thread (cdr thread))
4238                   (while thread
4239                     (gnus-remove-thread-1 (car thread))
4240                     (setq thread (cdr thread))))
4241               (gnus-remove-thread-1 thread))))))))
4242
4243 (defun gnus-remove-thread-1 (thread)
4244   "Remove the thread THREAD recursively."
4245   (let ((number (mail-header-number (pop thread)))
4246         d)
4247     (setq thread (reverse thread))
4248     (while thread
4249       (gnus-remove-thread-1 (pop thread)))
4250     (when (setq d (gnus-data-find number))
4251       (goto-char (gnus-data-pos d))
4252       (gnus-summary-show-thread)
4253       (gnus-data-remove
4254        number
4255        (- (gnus-point-at-bol)
4256           (prog1
4257               (1+ (gnus-point-at-eol))
4258             (gnus-delete-line)))))))
4259
4260 (defun gnus-sort-threads-1 (threads func)
4261   (sort (mapcar (lambda (thread)
4262                   (cons (car thread)
4263                         (and (cdr thread)
4264                              (gnus-sort-threads-1 (cdr thread) func))))
4265                 threads) func))
4266
4267 (defun gnus-sort-threads (threads)
4268   "Sort THREADS."
4269   (if (not gnus-thread-sort-functions)
4270       threads
4271     (gnus-message 8 "Sorting threads...")
4272     (let ((max-lisp-eval-depth 5000))
4273       (prog1 (gnus-sort-threads-1
4274          threads
4275          (gnus-make-sort-function gnus-thread-sort-functions))
4276         (gnus-message 8 "Sorting threads...done")))))
4277
4278 (defun gnus-sort-articles (articles)
4279   "Sort ARTICLES."
4280   (when gnus-article-sort-functions
4281     (gnus-message 7 "Sorting articles...")
4282     (prog1
4283         (setq gnus-newsgroup-headers
4284               (sort articles (gnus-make-sort-function
4285                               gnus-article-sort-functions)))
4286       (gnus-message 7 "Sorting articles...done"))))
4287
4288 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4289 (defmacro gnus-thread-header (thread)
4290   "Return header of first article in THREAD.
4291 Note that THREAD must never, ever be anything else than a variable -
4292 using some other form will lead to serious barfage."
4293   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4294   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4295   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4296         (vector thread) 2))
4297
4298 (defsubst gnus-article-sort-by-number (h1 h2)
4299   "Sort articles by article number."
4300   (< (mail-header-number h1)
4301      (mail-header-number h2)))
4302
4303 (defun gnus-thread-sort-by-number (h1 h2)
4304   "Sort threads by root article number."
4305   (gnus-article-sort-by-number
4306    (gnus-thread-header h1) (gnus-thread-header h2)))
4307
4308 (defsubst gnus-article-sort-by-random (h1 h2)
4309   "Sort articles by article number."
4310   (zerop (random 2)))
4311
4312 (defun gnus-thread-sort-by-random (h1 h2)
4313   "Sort threads by root article number."
4314   (gnus-article-sort-by-random
4315    (gnus-thread-header h1) (gnus-thread-header h2)))
4316
4317 (defsubst gnus-article-sort-by-lines (h1 h2)
4318   "Sort articles by article Lines header."
4319   (< (mail-header-lines h1)
4320      (mail-header-lines h2)))
4321
4322 (defun gnus-thread-sort-by-lines (h1 h2)
4323   "Sort threads by root article Lines header."
4324   (gnus-article-sort-by-lines
4325    (gnus-thread-header h1) (gnus-thread-header h2)))
4326
4327 (defsubst gnus-article-sort-by-chars (h1 h2)
4328   "Sort articles by octet length."
4329   (< (mail-header-chars h1)
4330      (mail-header-chars h2)))
4331
4332 (defun gnus-thread-sort-by-chars (h1 h2)
4333   "Sort threads by root article octet length."
4334   (gnus-article-sort-by-chars
4335    (gnus-thread-header h1) (gnus-thread-header h2)))
4336
4337 (defsubst gnus-article-sort-by-author (h1 h2)
4338   "Sort articles by root author."
4339   (string-lessp
4340    (let ((extract (funcall
4341                    gnus-extract-address-components
4342                    (mail-header-from h1))))
4343      (or (car extract) (cadr extract) ""))
4344    (let ((extract (funcall
4345                    gnus-extract-address-components
4346                    (mail-header-from h2))))
4347      (or (car extract) (cadr extract) ""))))
4348
4349 (defun gnus-thread-sort-by-author (h1 h2)
4350   "Sort threads by root author."
4351   (gnus-article-sort-by-author
4352    (gnus-thread-header h1)  (gnus-thread-header h2)))
4353
4354 (defsubst gnus-article-sort-by-subject (h1 h2)
4355   "Sort articles by root subject."
4356   (string-lessp
4357    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4358    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4359
4360 (defun gnus-thread-sort-by-subject (h1 h2)
4361   "Sort threads by root subject."
4362   (gnus-article-sort-by-subject
4363    (gnus-thread-header h1) (gnus-thread-header h2)))
4364
4365 (defsubst gnus-article-sort-by-date (h1 h2)
4366   "Sort articles by root article date."
4367   (time-less-p
4368    (gnus-date-get-time (mail-header-date h1))
4369    (gnus-date-get-time (mail-header-date h2))))
4370
4371 (defun gnus-thread-sort-by-date (h1 h2)
4372   "Sort threads by root article date."
4373   (gnus-article-sort-by-date
4374    (gnus-thread-header h1) (gnus-thread-header h2)))
4375
4376 (defsubst gnus-article-sort-by-score (h1 h2)
4377   "Sort articles by root article score.
4378 Unscored articles will be counted as having a score of zero."
4379   (> (or (cdr (assq (mail-header-number h1)
4380                     gnus-newsgroup-scored))
4381          gnus-summary-default-score 0)
4382      (or (cdr (assq (mail-header-number h2)
4383                     gnus-newsgroup-scored))
4384          gnus-summary-default-score 0)))
4385
4386 (defun gnus-thread-sort-by-score (h1 h2)
4387   "Sort threads by root article score."
4388   (gnus-article-sort-by-score
4389    (gnus-thread-header h1) (gnus-thread-header h2)))
4390
4391 (defun gnus-thread-sort-by-total-score (h1 h2)
4392   "Sort threads by the sum of all scores in the thread.
4393 Unscored articles will be counted as having a score of zero."
4394   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4395
4396 (defun gnus-thread-total-score (thread)
4397   ;; This function find the total score of THREAD.
4398   (cond
4399    ((null thread)
4400     0)
4401    ((consp thread)
4402     (if (stringp (car thread))
4403         (apply gnus-thread-score-function 0
4404                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4405       (gnus-thread-total-score-1 thread)))
4406    (t
4407     (gnus-thread-total-score-1 (list thread)))))
4408
4409 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4410   "Sort threads such that the thread with the most recently arrived article comes first."
4411   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4412
4413 (defun gnus-thread-highest-number (thread)
4414   "Return the highest article number in THREAD."
4415   (apply 'max (mapcar (lambda (header)
4416                         (mail-header-number header))
4417                       (message-flatten-list thread))))
4418
4419 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4420   "Sort threads such that the thread with the most recently dated article comes first."
4421   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4422
4423 (defun gnus-thread-latest-date (thread)
4424   "Return the highest article date in THREAD."
4425   (let ((previous-time 0))
4426     (apply 'max
4427            (mapcar
4428             (lambda (header)
4429               (setq previous-time
4430                     (time-to-seconds
4431                      (condition-case ()
4432                          (mail-header-parse-date (mail-header-date header))
4433                        (error previous-time)))))
4434             (sort
4435              (message-flatten-list thread)
4436              (lambda (h1 h2)
4437                (< (mail-header-number h1)
4438                   (mail-header-number h2))))))))
4439
4440 (defun gnus-thread-total-score-1 (root)
4441   ;; This function find the total score of the thread below ROOT.
4442   (setq root (car root))
4443   (apply gnus-thread-score-function
4444          (or (append
4445               (mapcar 'gnus-thread-total-score
4446                       (cdr (gnus-id-to-thread (mail-header-id root))))
4447               (when (> (mail-header-number root) 0)
4448                 (list (or (cdr (assq (mail-header-number root)
4449                                      gnus-newsgroup-scored))
4450                           gnus-summary-default-score 0))))
4451              (list gnus-summary-default-score)
4452              '(0))))
4453
4454 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4455 (defvar gnus-tmp-prev-subject nil)
4456 (defvar gnus-tmp-false-parent nil)
4457 (defvar gnus-tmp-root-expunged nil)
4458 (defvar gnus-tmp-dummy-line nil)
4459
4460 (eval-when-compile (defvar gnus-tmp-header))
4461 (defun gnus-extra-header (type &optional header)
4462   "Return the extra header of TYPE."
4463   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4464       ""))
4465
4466 (defvar gnus-tmp-thread-tree-header-string "")
4467
4468 (defcustom gnus-sum-thread-tree-root "> "
4469   "With %B spec, used for the root of a thread.
4470 If nil, use subject instead."
4471   :type 'string
4472   :group 'gnus-thread)
4473 (defcustom gnus-sum-thread-tree-single-indent ""
4474   "With %B spec, used for a thread with just one message.
4475 If nil, use subject instead."
4476   :type 'string
4477   :group 'gnus-thread)
4478 (defcustom gnus-sum-thread-tree-vertical "| "
4479   "With %B spec, used for drawing a vertical line."
4480   :type 'string
4481   :group 'gnus-thread)
4482 (defcustom gnus-sum-thread-tree-indent "  "
4483   "With %B spec, used for indenting."
4484   :type 'string
4485   :group 'gnus-thread)
4486 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4487   "With %B spec, used for a leaf with brothers."
4488   :type 'string
4489   :group 'gnus-thread)
4490 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4491   "With %B spec, used for a leaf without brothers."
4492   :type 'string
4493   :group 'gnus-thread)
4494
4495 (defun gnus-summary-prepare-threads (threads)
4496   "Prepare summary buffer from THREADS and indentation LEVEL.
4497 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4498 or a straight list of headers."
4499   (gnus-message 7 "Generating summary...")
4500
4501   (setq gnus-newsgroup-threads threads)
4502   (beginning-of-line)
4503
4504   (let ((gnus-tmp-level 0)
4505         (default-score (or gnus-summary-default-score 0))
4506         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4507         (building-line-count gnus-summary-display-while-building)
4508         (building-count (integerp gnus-summary-display-while-building))
4509         thread number subject stack state gnus-tmp-gathered beg-match
4510         new-roots gnus-tmp-new-adopts thread-end simp-subject
4511         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4512         gnus-tmp-replied gnus-tmp-subject-or-nil
4513         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4514         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4515         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4516         tree-stack)
4517
4518     (setq gnus-tmp-prev-subject nil)
4519
4520     (if (vectorp (car threads))
4521         ;; If this is a straight (sic) list of headers, then a
4522         ;; threaded summary display isn't required, so we just create
4523         ;; an unthreaded one.
4524         (gnus-summary-prepare-unthreaded threads)
4525
4526       ;; Do the threaded display.
4527
4528       (if gnus-summary-display-while-building
4529           (switch-to-buffer (buffer-name)))
4530       (while (or threads stack gnus-tmp-new-adopts new-roots)
4531
4532         (if (and (= gnus-tmp-level 0)
4533                  (or (not stack)
4534                      (= (caar stack) 0))
4535                  (not gnus-tmp-false-parent)
4536                  (or gnus-tmp-new-adopts new-roots))
4537             (if gnus-tmp-new-adopts
4538                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4539                       thread (list (car gnus-tmp-new-adopts))
4540                       gnus-tmp-header (caar thread)
4541                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4542               (when new-roots
4543                 (setq thread (list (car new-roots))
4544                       gnus-tmp-header (caar thread)
4545                       new-roots (cdr new-roots))))
4546
4547           (if threads
4548               ;; If there are some threads, we do them before the
4549               ;; threads on the stack.
4550               (setq thread threads
4551                     gnus-tmp-header (caar thread))
4552             ;; There were no current threads, so we pop something off
4553             ;; the stack.
4554             (setq state (car stack)
4555                   gnus-tmp-level (car state)
4556                   tree-stack (cadr state)
4557                   thread (caddr state)
4558                   stack (cdr stack)
4559                   gnus-tmp-header (caar thread))))
4560
4561         (setq gnus-tmp-false-parent nil)
4562         (setq gnus-tmp-root-expunged nil)
4563         (setq thread-end nil)
4564
4565         (if (stringp gnus-tmp-header)
4566             ;; The header is a dummy root.
4567             (cond
4568              ((eq gnus-summary-make-false-root 'adopt)
4569               ;; We let the first article adopt the rest.
4570               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4571                                                (cddar thread)))
4572               (setq gnus-tmp-gathered
4573                     (nconc (mapcar
4574                             (lambda (h) (mail-header-number (car h)))
4575                             (cddar thread))
4576                            gnus-tmp-gathered))
4577               (setq thread (cons (list (caar thread)
4578                                        (cadar thread))
4579                                  (cdr thread)))
4580               (setq gnus-tmp-level -1
4581                     gnus-tmp-false-parent t))
4582              ((eq gnus-summary-make-false-root 'empty)
4583               ;; We print adopted articles with empty subject fields.
4584               (setq gnus-tmp-gathered
4585                     (nconc (mapcar
4586                             (lambda (h) (mail-header-number (car h)))
4587                             (cddar thread))
4588                            gnus-tmp-gathered))
4589               (setq gnus-tmp-level -1))
4590              ((eq gnus-summary-make-false-root 'dummy)
4591               ;; We remember that we probably want to output a dummy
4592               ;; root.
4593               (setq gnus-tmp-dummy-line gnus-tmp-header)
4594               (setq gnus-tmp-prev-subject gnus-tmp-header))
4595              (t
4596               ;; We do not make a root for the gathered
4597               ;; sub-threads at all.
4598               (setq gnus-tmp-level -1)))
4599
4600           (setq number (mail-header-number gnus-tmp-header)
4601                 subject (mail-header-subject gnus-tmp-header)
4602                 simp-subject (gnus-simplify-subject-fully subject))
4603
4604           (cond
4605            ;; If the thread has changed subject, we might want to make
4606            ;; this subthread into a root.
4607            ((and (null gnus-thread-ignore-subject)
4608                  (not (zerop gnus-tmp-level))
4609                  gnus-tmp-prev-subject
4610                  (not (string= gnus-tmp-prev-subject simp-subject)))
4611             (setq new-roots (nconc new-roots (list (car thread)))
4612                   thread-end t
4613                   gnus-tmp-header nil))
4614            ;; If the article lies outside the current limit,
4615            ;; then we do not display it.
4616            ((not (memq number gnus-newsgroup-limit))
4617             (setq gnus-tmp-gathered
4618                   (nconc (mapcar
4619                           (lambda (h) (mail-header-number (car h)))
4620                           (cdar thread))
4621                          gnus-tmp-gathered))
4622             (setq gnus-tmp-new-adopts (if (cdar thread)
4623                                           (append gnus-tmp-new-adopts
4624                                                   (cdar thread))
4625                                         gnus-tmp-new-adopts)
4626                   thread-end t
4627                   gnus-tmp-header nil)
4628             (when (zerop gnus-tmp-level)
4629               (setq gnus-tmp-root-expunged t)))
4630            ;; Perhaps this article is to be marked as read?
4631            ((and gnus-summary-mark-below
4632                  (< (or (cdr (assq number gnus-newsgroup-scored))
4633                         default-score)
4634                     gnus-summary-mark-below)
4635                  ;; Don't touch sparse articles.
4636                  (not (gnus-summary-article-sparse-p number))
4637                  (not (gnus-summary-article-ancient-p number)))
4638             (setq gnus-newsgroup-unreads
4639                   (delq number gnus-newsgroup-unreads))
4640             (if gnus-newsgroup-auto-expire
4641                 (setq gnus-newsgroup-expirable
4642                       (gnus-add-to-sorted-list
4643                        gnus-newsgroup-expirable number))
4644               (push (cons number gnus-low-score-mark)
4645                     gnus-newsgroup-reads))))
4646
4647           (when gnus-tmp-header
4648             ;; We may have an old dummy line to output before this
4649             ;; article.
4650             (when (and gnus-tmp-dummy-line
4651                        (gnus-subject-equal
4652                         gnus-tmp-dummy-line
4653                         (mail-header-subject gnus-tmp-header)))
4654               (gnus-summary-insert-dummy-line
4655                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4656               (setq gnus-tmp-dummy-line nil))
4657
4658             ;; Compute the mark.
4659             (setq gnus-tmp-unread (gnus-article-mark number))
4660
4661             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4662                                   gnus-tmp-header gnus-tmp-level)
4663                   gnus-newsgroup-data)
4664
4665             ;; Actually insert the line.
4666             (setq
4667              gnus-tmp-subject-or-nil
4668              (cond
4669               ((and gnus-thread-ignore-subject
4670                     gnus-tmp-prev-subject
4671                     (not (string= gnus-tmp-prev-subject simp-subject)))
4672                subject)
4673               ((zerop gnus-tmp-level)
4674                (if (and (eq gnus-summary-make-false-root 'empty)
4675                         (memq number gnus-tmp-gathered)
4676                         gnus-tmp-prev-subject
4677                         (string= gnus-tmp-prev-subject simp-subject))
4678                    gnus-summary-same-subject
4679                  subject))
4680               (t gnus-summary-same-subject)))
4681             (if (and (eq gnus-summary-make-false-root 'adopt)
4682                      (= gnus-tmp-level 1)
4683                      (memq number gnus-tmp-gathered))
4684                 (setq gnus-tmp-opening-bracket ?\<
4685                       gnus-tmp-closing-bracket ?\>)
4686               (setq gnus-tmp-opening-bracket ?\[
4687                     gnus-tmp-closing-bracket ?\]))
4688             (setq
4689              gnus-tmp-indentation
4690              (aref gnus-thread-indent-array gnus-tmp-level)
4691              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4692              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4693                                 gnus-summary-default-score 0)
4694              gnus-tmp-score-char
4695              (if (or (null gnus-summary-default-score)
4696                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4697                          gnus-summary-zcore-fuzz))
4698                  ?                      ;Whitespace
4699                (if (< gnus-tmp-score gnus-summary-default-score)
4700                    gnus-score-below-mark gnus-score-over-mark))
4701              gnus-tmp-replied
4702              (cond ((memq number gnus-newsgroup-processable)
4703                     gnus-process-mark)
4704                    ((memq number gnus-newsgroup-cached)
4705                     gnus-cached-mark)
4706                    ((memq number gnus-newsgroup-replied)
4707                     gnus-replied-mark)
4708                    ((memq number gnus-newsgroup-forwarded)
4709                     gnus-forwarded-mark)
4710                    ((memq number gnus-newsgroup-saved)
4711                     gnus-saved-mark)
4712                    ((memq number gnus-newsgroup-recent)
4713                     gnus-recent-mark)
4714                    ((memq number gnus-newsgroup-unseen)
4715                     gnus-unseen-mark)
4716                    (t gnus-no-mark))
4717              gnus-tmp-downloaded
4718              (cond ((memq number gnus-newsgroup-undownloaded) 
4719                     gnus-undownloaded-mark)
4720                    (gnus-newsgroup-agentized
4721                     gnus-downloaded-mark)
4722                    (t
4723                     gnus-no-mark))
4724              gnus-tmp-from (mail-header-from gnus-tmp-header)
4725              gnus-tmp-name
4726              (cond
4727               ((string-match "<[^>]+> *$" gnus-tmp-from)
4728                (setq beg-match (match-beginning 0))
4729                (or (and (string-match "^\".+\"" gnus-tmp-from)
4730                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4731                    (substring gnus-tmp-from 0 beg-match)))
4732               ((string-match "(.+)" gnus-tmp-from)
4733                (substring gnus-tmp-from
4734                           (1+ (match-beginning 0)) (1- (match-end 0))))
4735               (t gnus-tmp-from))
4736              gnus-tmp-thread-tree-header-string
4737              (cond
4738               ((not gnus-show-threads) "")
4739               ((zerop gnus-tmp-level)
4740                (if (cdar thread)
4741                    (or gnus-sum-thread-tree-root subject)
4742                  (or gnus-sum-thread-tree-single-indent subject)))
4743               (t
4744                (concat (apply 'concat
4745                               (mapcar (lambda (item)
4746                                         (if (= item 1)
4747                                             gnus-sum-thread-tree-vertical
4748                                           gnus-sum-thread-tree-indent))
4749                                       (cdr (reverse tree-stack))))
4750                        (if (nth 1 thread)
4751                            gnus-sum-thread-tree-leaf-with-other
4752                          gnus-sum-thread-tree-single-leaf)))))
4753             (when (string= gnus-tmp-name "")
4754               (setq gnus-tmp-name gnus-tmp-from))
4755             (unless (numberp gnus-tmp-lines)
4756               (setq gnus-tmp-lines -1))
4757             (if (= gnus-tmp-lines -1)
4758                 (setq gnus-tmp-lines "?")
4759               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4760             (gnus-put-text-property
4761              (point)
4762              (progn (eval gnus-summary-line-format-spec) (point))
4763              'gnus-number number)
4764             (when gnus-visual-p
4765               (forward-line -1)
4766               (gnus-run-hooks 'gnus-summary-update-hook)
4767               (forward-line 1))
4768
4769             (setq gnus-tmp-prev-subject simp-subject)))
4770
4771         (when (nth 1 thread)
4772           (push (list (max 0 gnus-tmp-level)
4773                       (copy-list tree-stack)
4774                       (nthcdr 1 thread))
4775                 stack))
4776         (push (if (nth 1 thread) 1 0) tree-stack)
4777         (incf gnus-tmp-level)
4778         (setq threads (if thread-end nil (cdar thread)))
4779         (if gnus-summary-display-while-building
4780             (if building-count
4781                 (progn
4782                   ;; use a set frequency
4783                   (setq building-line-count (1- building-line-count))
4784                   (when (= building-line-count 0)
4785                     (sit-for 0)
4786                     (setq building-line-count
4787                           gnus-summary-display-while-building)))
4788               ;; always
4789               (sit-for 0)))
4790         (unless threads
4791           (setq gnus-tmp-level 0)))))
4792   (gnus-message 7 "Generating summary...done"))
4793
4794 (defun gnus-summary-prepare-unthreaded (headers)
4795   "Generate an unthreaded summary buffer based on HEADERS."
4796   (let (header number mark)
4797
4798     (beginning-of-line)
4799
4800     (while headers
4801       ;; We may have to root out some bad articles...
4802       (when (memq (setq number (mail-header-number
4803                                 (setq header (pop headers))))
4804                   gnus-newsgroup-limit)
4805         ;; Mark article as read when it has a low score.
4806         (when (and gnus-summary-mark-below
4807                    (< (or (cdr (assq number gnus-newsgroup-scored))
4808                           gnus-summary-default-score 0)
4809                       gnus-summary-mark-below)
4810                    (not (gnus-summary-article-ancient-p number)))
4811           (setq gnus-newsgroup-unreads
4812                 (delq number gnus-newsgroup-unreads))
4813           (if gnus-newsgroup-auto-expire
4814               (push number gnus-newsgroup-expirable)
4815             (push (cons number gnus-low-score-mark)
4816                   gnus-newsgroup-reads)))
4817
4818         (setq mark (gnus-article-mark number))
4819         (push (gnus-data-make number mark (1+ (point)) header 0)
4820               gnus-newsgroup-data)
4821         (gnus-summary-insert-line
4822          header 0 number
4823          (memq number gnus-newsgroup-undownloaded)
4824          mark (memq number gnus-newsgroup-replied)
4825          (memq number gnus-newsgroup-expirable)
4826          (mail-header-subject header) nil
4827          (cdr (assq number gnus-newsgroup-scored))
4828          (memq number gnus-newsgroup-processable))))))
4829
4830 (defun gnus-summary-remove-list-identifiers ()
4831   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4832   (let ((regexp (if (consp gnus-list-identifiers)
4833                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4834                   gnus-list-identifiers))
4835         changed subject)
4836     (when regexp
4837       (dolist (header gnus-newsgroup-headers)
4838         (setq subject (mail-header-subject header)
4839               changed nil)
4840         (while (string-match
4841                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4842                 subject)
4843           (setq subject
4844                 (concat (substring subject 0 (match-beginning 2))
4845                         (substring subject (match-end 0)))
4846                 changed t))
4847         (when (and changed
4848                    (string-match
4849                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4850           (setq subject
4851                 (concat (substring subject 0 (match-beginning 1))
4852                         (substring subject (match-end 1)))))
4853         (when changed
4854           (mail-header-set-subject header subject))))))
4855
4856 (defun gnus-fetch-headers (articles)
4857   "Fetch headers of ARTICLES."
4858   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4859     (gnus-message 5 "Fetching headers for %s..." name)
4860     (prog1
4861         (if (eq 'nov
4862                 (setq gnus-headers-retrieved-by
4863                       (gnus-retrieve-headers
4864                        articles gnus-newsgroup-name
4865                        ;; We might want to fetch old headers, but
4866                        ;; not if there is only 1 article.
4867                        (and (or (and
4868                                  (not (eq gnus-fetch-old-headers 'some))
4869                                  (not (numberp gnus-fetch-old-headers)))
4870                                 (> (length articles) 1))
4871                             gnus-fetch-old-headers))))
4872             (gnus-get-newsgroup-headers-xover
4873              articles nil nil gnus-newsgroup-name t)
4874           (gnus-get-newsgroup-headers))
4875       (gnus-message 5 "Fetching headers for %s...done" name))))
4876
4877 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4878   "Select newsgroup GROUP.
4879 If READ-ALL is non-nil, all articles in the group are selected.
4880 If SELECT-ARTICLES, only select those articles from GROUP."
4881   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4882          ;;!!! Dirty hack; should be removed.
4883          (gnus-summary-ignore-duplicates
4884           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4885               t
4886             gnus-summary-ignore-duplicates))
4887          (info (nth 2 entry))
4888          articles fetched-articles cached)
4889
4890     (unless (gnus-check-server
4891              (set (make-local-variable 'gnus-current-select-method)
4892                   (gnus-find-method-for-group group)))
4893       (error "Couldn't open server"))
4894
4895     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4896         (gnus-activate-group group)     ; Or we can activate it...
4897         (progn                          ; Or we bug out.
4898           (when (equal major-mode 'gnus-summary-mode)
4899             (kill-buffer (current-buffer)))
4900           (error "Couldn't activate group %s: %s"
4901                  group (gnus-status-message group))))
4902
4903     (unless (gnus-request-group group t)
4904       (when (equal major-mode 'gnus-summary-mode)
4905         (kill-buffer (current-buffer)))
4906       (error "Couldn't request group %s: %s"
4907              group (gnus-status-message group)))
4908
4909     (setq gnus-newsgroup-name group
4910           gnus-newsgroup-unselected nil
4911           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4912
4913     (let ((display (gnus-group-find-parameter group 'display)))
4914       (setq gnus-newsgroup-display
4915             (cond
4916              ((not (zerop (or (car-safe read-all) 0)))
4917               ;; The user entered the group with C-u SPC/RET, let's show
4918               ;; all articles.
4919               'gnus-not-ignore)
4920              ((eq display 'all)
4921               'gnus-not-ignore)
4922              ((arrayp display)
4923               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4924              ((numberp display)
4925               ;; The following is probably the "correct" solution, but
4926               ;; it makes Gnus fetch all headers and then limit the
4927               ;; articles (which is slow), so instead we hack the
4928               ;; select-articles parameter instead. -- Simon Josefsson
4929               ;; <jas@kth.se>
4930               ;;
4931               ;; (gnus-byte-compile
4932               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4933               ;;                         display)))))
4934               (setq select-articles
4935                     (gnus-uncompress-range
4936                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4937                              (if (> tmp 0)
4938                                  tmp
4939                                1))
4940                            (cdr (gnus-active group)))))
4941               nil)
4942              (t
4943               nil))))
4944
4945     (gnus-summary-setup-default-charset)
4946
4947     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4948     (when (gnus-virtual-group-p group)
4949       (setq cached gnus-newsgroup-cached))
4950
4951     (setq gnus-newsgroup-unreads
4952           (gnus-sorted-ndifference
4953            (gnus-sorted-ndifference gnus-newsgroup-unreads
4954                                     gnus-newsgroup-marked)
4955            gnus-newsgroup-dormant))
4956
4957     (setq gnus-newsgroup-processable nil)
4958
4959     (gnus-update-read-articles group gnus-newsgroup-unreads)
4960
4961     ;; Adjust and set lists of article marks.
4962     (when info
4963       (gnus-adjust-marked-articles info))
4964     (if (setq articles select-articles)
4965         (setq gnus-newsgroup-unselected
4966               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4967       (setq articles (gnus-articles-to-read group read-all)))
4968
4969     (cond
4970      ((null articles)
4971       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4972       'quit)
4973      ((eq articles 0) nil)
4974      (t
4975       ;; Init the dependencies hash table.
4976       (setq gnus-newsgroup-dependencies
4977             (gnus-make-hashtable (length articles)))
4978       (gnus-set-global-variables)
4979       ;; Retrieve the headers and read them in.
4980
4981       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4982
4983       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4984       (when cached
4985         (setq gnus-newsgroup-cached cached))
4986
4987       ;; Suppress duplicates?
4988       (when gnus-suppress-duplicates
4989         (gnus-dup-suppress-articles))
4990
4991       ;; Set the initial limit.
4992       (setq gnus-newsgroup-limit (copy-sequence articles))
4993       ;; Remove canceled articles from the list of unread articles.
4994       (setq fetched-articles
4995             (mapcar (lambda (headers) (mail-header-number headers))
4996                     gnus-newsgroup-headers))
4997       (setq gnus-newsgroup-articles fetched-articles)
4998       (setq gnus-newsgroup-unreads
4999             (gnus-sorted-nintersection
5000              gnus-newsgroup-unreads fetched-articles))
5001       (gnus-compute-unseen-list)
5002
5003       ;; Removed marked articles that do not exist.
5004       (gnus-update-missing-marks
5005        (gnus-sorted-difference articles fetched-articles))
5006       ;; We might want to build some more threads first.
5007       (when (and gnus-fetch-old-headers
5008                  (eq gnus-headers-retrieved-by 'nov))
5009         (if (eq gnus-fetch-old-headers 'invisible)
5010             (gnus-build-all-threads)
5011           (gnus-build-old-threads)))
5012       ;; Let the Gnus agent mark articles as read.
5013       (when gnus-agent
5014         (gnus-agent-get-undownloaded-list))
5015       ;; Remove list identifiers from subject
5016       (when gnus-list-identifiers
5017         (gnus-summary-remove-list-identifiers))
5018       ;; Check whether auto-expire is to be done in this group.
5019       (setq gnus-newsgroup-auto-expire
5020             (gnus-group-auto-expirable-p group))
5021       ;; Set up the article buffer now, if necessary.
5022       (unless gnus-single-article-buffer
5023         (gnus-article-setup-buffer))
5024       ;; First and last article in this newsgroup.
5025       (when gnus-newsgroup-headers
5026         (setq gnus-newsgroup-begin
5027               (mail-header-number (car gnus-newsgroup-headers))
5028               gnus-newsgroup-end
5029               (mail-header-number
5030                (gnus-last-element gnus-newsgroup-headers))))
5031       ;; GROUP is successfully selected.
5032       (or gnus-newsgroup-headers t)))))
5033
5034 (defun gnus-compute-unseen-list ()
5035   ;; The `seen' marks are treated specially.
5036   (if (not gnus-newsgroup-seen)
5037       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5038     (setq gnus-newsgroup-unseen
5039           (gnus-inverse-list-range-intersection
5040            gnus-newsgroup-articles gnus-newsgroup-seen))))
5041
5042 (defun gnus-summary-display-make-predicate (display)
5043   (require 'gnus-agent)
5044   (when (= (length display) 1)
5045     (setq display (car display)))
5046   (unless gnus-summary-display-cache
5047     (dolist (elem (append '((unread . unread)
5048                             (read . read)
5049                             (unseen . unseen))
5050                           gnus-article-mark-lists))
5051       (push (cons (cdr elem)
5052                   (gnus-byte-compile
5053                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5054             gnus-summary-display-cache)))
5055   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5056         (gnus-category-predicate-cache gnus-summary-display-cache))
5057     (gnus-get-predicate display)))
5058
5059 ;; Uses the dynamically bound `number' variable.
5060 (eval-when-compile
5061   (defvar number))
5062 (defun gnus-article-marked-p (type &optional article)
5063   (let ((article (or article number)))
5064     (cond
5065      ((eq type 'tick)
5066       (memq article gnus-newsgroup-marked))
5067      ((eq type 'spam)
5068       (memq article gnus-newsgroup-spam-marked))
5069      ((eq type 'unsend)
5070       (memq article gnus-newsgroup-unsendable))
5071      ((eq type 'undownload)
5072       (memq article gnus-newsgroup-undownloaded))
5073      ((eq type 'download)
5074       (memq article gnus-newsgroup-downloadable))
5075      ((eq type 'unread)
5076       (memq article gnus-newsgroup-unreads))
5077      ((eq type 'read)
5078       (memq article gnus-newsgroup-reads))
5079      ((eq type 'dormant)
5080       (memq article gnus-newsgroup-dormant) )
5081      ((eq type 'expire)
5082       (memq article gnus-newsgroup-expirable))
5083      ((eq type 'reply)
5084       (memq article gnus-newsgroup-replied))
5085      ((eq type 'killed)
5086       (memq article gnus-newsgroup-killed))
5087      ((eq type 'bookmark)
5088       (assq article gnus-newsgroup-bookmarks))
5089      ((eq type 'score)
5090       (assq article gnus-newsgroup-scored))
5091      ((eq type 'save)
5092       (memq article gnus-newsgroup-saved))
5093      ((eq type 'cache)
5094       (memq article gnus-newsgroup-cached))
5095      ((eq type 'forward)
5096       (memq article gnus-newsgroup-forwarded))
5097      ((eq type 'seen)
5098       (not (memq article gnus-newsgroup-unseen)))
5099      ((eq type 'recent)
5100       (memq article gnus-newsgroup-recent))
5101      (t t))))
5102
5103 (defun gnus-articles-to-read (group &optional read-all)
5104   "Find out what articles the user wants to read."
5105   (let* ((display (gnus-group-find-parameter group 'display))
5106          (articles
5107           ;; Select all articles if `read-all' is non-nil, or if there
5108           ;; are no unread articles.
5109           (if (or read-all
5110                   (and (zerop (length gnus-newsgroup-marked))
5111                        (zerop (length gnus-newsgroup-unreads)))
5112                   ;; Fetch all if the predicate is non-nil.
5113                   gnus-newsgroup-display)
5114               ;; We want to select the headers for all the articles in
5115               ;; the group, so we select either all the active
5116               ;; articles in the group, or (if that's nil), the
5117               ;; articles in the cache.
5118               (or
5119                (gnus-uncompress-range (gnus-active group))
5120                (gnus-cache-articles-in-group group))
5121             ;; Select only the "normal" subset of articles.
5122             (gnus-sorted-nunion
5123              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5124              gnus-newsgroup-unreads)))
5125          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5126          (scored (length scored-list))
5127          (number (length articles))
5128          (marked (+ (length gnus-newsgroup-marked)
5129                     (length gnus-newsgroup-dormant)))
5130          (select
5131           (cond
5132            ((numberp read-all)
5133             read-all)
5134            ((numberp gnus-newsgroup-display)
5135             gnus-newsgroup-display)
5136            (t
5137             (condition-case ()
5138                 (cond
5139                  ((and (or (<= scored marked) (= scored number))
5140                        (numberp gnus-large-newsgroup)
5141                        (> number gnus-large-newsgroup))
5142                   (let* ((cursor-in-echo-area nil)
5143                          (initial (gnus-parameter-large-newsgroup-initial
5144                                    gnus-newsgroup-name))
5145                          (input
5146                           (read-string
5147                            (format
5148                             "How many articles from %s (%s %d): "
5149                             (gnus-limit-string
5150                              (gnus-group-decoded-name gnus-newsgroup-name)
5151                              35)
5152                             (if initial "max" "default")
5153                             number)
5154                            (if initial
5155                                (cons (number-to-string initial)
5156                                      0)))))
5157                     (if (string-match "^[ \t]*$" input) number input)))
5158                  ((and (> scored marked) (< scored number)
5159                        (> (- scored number) 20))
5160                   (let ((input
5161                          (read-string
5162                           (format "%s %s (%d scored, %d total): "
5163                                   "How many articles from"
5164                                   (gnus-group-decoded-name group)
5165                                   scored number))))
5166                     (if (string-match "^[ \t]*$" input)
5167                         number input)))
5168                  (t number))
5169               (quit
5170                (message "Quit getting the articles to read")
5171                nil))))))
5172     (setq select (if (stringp select) (string-to-number select) select))
5173     (if (or (null select) (zerop select))
5174         select
5175       (if (and (not (zerop scored)) (<= (abs select) scored))
5176           (progn
5177             (setq articles (sort scored-list '<))
5178             (setq number (length articles)))
5179         (setq articles (copy-sequence articles)))
5180
5181       (when (< (abs select) number)
5182         (if (< select 0)
5183             ;; Select the N oldest articles.
5184             (setcdr (nthcdr (1- (abs select)) articles) nil)
5185           ;; Select the N most recent articles.
5186           (setq articles (nthcdr (- number select) articles))))
5187       (setq gnus-newsgroup-unselected
5188             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5189       (when gnus-alter-articles-to-read-function
5190         (setq gnus-newsgroup-unreads
5191               (sort
5192                (funcall gnus-alter-articles-to-read-function
5193                         gnus-newsgroup-name gnus-newsgroup-unreads)
5194                '<)))
5195       articles)))
5196
5197 (defun gnus-killed-articles (killed articles)
5198   (let (out)
5199     (while articles
5200       (when (inline (gnus-member-of-range (car articles) killed))
5201         (push (car articles) out))
5202       (setq articles (cdr articles)))
5203     out))
5204
5205 (defun gnus-uncompress-marks (marks)
5206   "Uncompress the mark ranges in MARKS."
5207   (let ((uncompressed '(score bookmark))
5208         out)
5209     (while marks
5210       (if (memq (caar marks) uncompressed)
5211           (push (car marks) out)
5212         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5213       (setq marks (cdr marks)))
5214     out))
5215
5216 (defun gnus-article-mark-to-type (mark)
5217   "Return the type of MARK."
5218   (or (cadr (assq mark gnus-article-special-mark-lists))
5219       'list))
5220
5221 (defun gnus-article-unpropagatable-p (mark)
5222   "Return whether MARK should be propagated to backend."
5223   (memq mark gnus-article-unpropagated-mark-lists))
5224
5225 (defun gnus-adjust-marked-articles (info)
5226   "Set all article lists and remove all marks that are no longer valid."
5227   (let* ((marked-lists (gnus-info-marks info))
5228          (active (gnus-active (gnus-info-group info)))
5229          (min (car active))
5230          (max (cdr active))
5231          (types gnus-article-mark-lists)
5232          marks var articles article mark mark-type)
5233
5234     (dolist (marks marked-lists)
5235       (setq mark (car marks)
5236             mark-type (gnus-article-mark-to-type mark)
5237             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5238
5239       ;; We set the variable according to the type of the marks list,
5240       ;; and then adjust the marks to a subset of the active articles.
5241       (cond
5242        ;; Adjust "simple" lists.
5243        ((eq mark-type 'list)
5244         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5245         (when (memq mark '(tick dormant expire reply save))
5246           (while articles
5247             (when (or (< (setq article (pop articles)) min) (> article max))
5248               (set var (delq article (symbol-value var)))))))
5249        ;; Adjust assocs.
5250        ((eq mark-type 'tuple)
5251         (set var (setq articles (cdr marks)))
5252         (when (not (listp (cdr (symbol-value var))))
5253           (set var (list (symbol-value var))))
5254         (when (not (listp (cdr articles)))
5255           (setq articles (list articles)))
5256         (while articles
5257           (when (or (not (consp (setq article (pop articles))))
5258                     (< (car article) min)
5259                     (> (car article) max))
5260             (set var (delq article (symbol-value var))))))
5261        ;; Adjust ranges (sloppily).
5262        ((eq mark-type 'range)
5263         (cond
5264          ((eq mark 'seen)
5265           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5266           ;; It should be (seen (NUM1 . NUM2)).
5267           (when (numberp (cddr marks))
5268             (setcdr marks (list (cdr marks))))
5269           (setq articles (cdr marks))
5270           (while (and articles
5271                       (or (and (consp (car articles))
5272                                (> min (cdar articles)))
5273                           (and (numberp (car articles))
5274                                (> min (car articles)))))
5275             (pop articles))
5276           (set var articles))))))))
5277
5278 (defun gnus-update-missing-marks (missing)
5279   "Go through the list of MISSING articles and remove them from the mark lists."
5280   (when missing
5281     (let (var m)
5282       ;; Go through all types.
5283       (dolist (elem gnus-article-mark-lists)
5284         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5285           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5286           (when (symbol-value var)
5287             ;; This list has articles.  So we delete all missing
5288             ;; articles from it.
5289             (setq m missing)
5290             (while m
5291               (set var (delq (pop m) (symbol-value var))))))))))
5292
5293 (defun gnus-update-marks ()
5294   "Enter the various lists of marked articles into the newsgroup info list."
5295   (let ((types gnus-article-mark-lists)
5296         (info (gnus-get-info gnus-newsgroup-name))
5297         type list newmarked symbol delta-marks)
5298     (when info
5299       ;; Add all marks lists to the list of marks lists.
5300       (while (setq type (pop types))
5301         (setq list (symbol-value
5302                     (setq symbol
5303                           (intern (format "gnus-newsgroup-%s" (car type))))))
5304
5305         (when list
5306           ;; Get rid of the entries of the articles that have the
5307           ;; default score.
5308           (when (and (eq (cdr type) 'score)
5309                      gnus-save-score
5310                      list)
5311             (let* ((arts list)
5312                    (prev (cons nil list))
5313                    (all prev))
5314               (while arts
5315                 (if (or (not (consp (car arts)))
5316                         (= (cdar arts) gnus-summary-default-score))
5317                     (setcdr prev (cdr arts))
5318                   (setq prev arts))
5319                 (setq arts (cdr arts)))
5320               (setq list (cdr all)))))
5321
5322         (when (eq (cdr type) 'seen)
5323           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5324
5325         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5326           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5327
5328         (when (and (gnus-check-backend-function
5329                     'request-set-mark gnus-newsgroup-name)
5330                    (not (gnus-article-unpropagatable-p (cdr type))))
5331           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5332                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5333                  (add (gnus-remove-from-range
5334                        (gnus-copy-sequence list) old)))
5335             (when add
5336               (push (list add 'add (list (cdr type))) delta-marks))
5337             (when del
5338               (push (list del 'del (list (cdr type))) delta-marks))))
5339
5340         (when list
5341           (push (cons (cdr type) list) newmarked)))
5342
5343       (when delta-marks
5344         (unless (gnus-check-group gnus-newsgroup-name)
5345           (error "Can't open server for %s" gnus-newsgroup-name))
5346         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5347
5348       ;; Enter these new marks into the info of the group.
5349       (if (nthcdr 3 info)
5350           (setcar (nthcdr 3 info) newmarked)
5351         ;; Add the marks lists to the end of the info.
5352         (when newmarked
5353           (setcdr (nthcdr 2 info) (list newmarked))))
5354
5355       ;; Cut off the end of the info if there's nothing else there.
5356       (let ((i 5))
5357         (while (and (> i 2)
5358                     (not (nth i info)))
5359           (when (nthcdr (decf i) info)
5360             (setcdr (nthcdr i info) nil)))))))
5361
5362 (defun gnus-set-mode-line (where)
5363   "Set the mode line of the article or summary buffers.
5364 If WHERE is `summary', the summary mode line format will be used."
5365   ;; Is this mode line one we keep updated?
5366   (when (and (memq where gnus-updated-mode-lines)
5367              (symbol-value
5368               (intern (format "gnus-%s-mode-line-format-spec" where))))
5369     (let (mode-string)
5370       (save-excursion
5371         ;; We evaluate this in the summary buffer since these
5372         ;; variables are buffer-local to that buffer.
5373         (set-buffer gnus-summary-buffer)
5374        ;; We bind all these variables that are used in the `eval' form
5375         ;; below.
5376         (let* ((mformat (symbol-value
5377                          (intern
5378                           (format "gnus-%s-mode-line-format-spec" where))))
5379                (gnus-tmp-group-name (gnus-group-decoded-name
5380                                      gnus-newsgroup-name))
5381                (gnus-tmp-article-number (or gnus-current-article 0))
5382                (gnus-tmp-unread gnus-newsgroup-unreads)
5383                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5384                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5385                (gnus-tmp-unread-and-unselected
5386                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5387                             (zerop gnus-tmp-unselected))
5388                        "")
5389                       ((zerop gnus-tmp-unselected)
5390                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5391                       (t (format "{%d(+%d) more}"
5392                                  gnus-tmp-unread-and-unticked
5393                                  gnus-tmp-unselected))))
5394                (gnus-tmp-subject
5395                 (if (and gnus-current-headers
5396                          (vectorp gnus-current-headers))
5397                     (gnus-mode-string-quote
5398                      (mail-header-subject gnus-current-headers))
5399                   ""))
5400                bufname-length max-len
5401                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5402           (setq mode-string (eval mformat))
5403           (setq bufname-length (if (string-match "%b" mode-string)
5404                                    (- (length
5405                                        (buffer-name
5406                                         (if (eq where 'summary)
5407                                             nil
5408                                           (get-buffer gnus-article-buffer))))
5409                                       2)
5410                                  0))
5411           (setq max-len (max 4 (if gnus-mode-non-string-length
5412                                    (- (window-width)
5413                                       gnus-mode-non-string-length
5414                                       bufname-length)
5415                                  (length mode-string))))
5416           ;; We might have to chop a bit of the string off...
5417           (when (> (length mode-string) max-len)
5418             (setq mode-string
5419                   (concat (truncate-string-to-width mode-string (- max-len 3))
5420                           "...")))
5421           ;; Pad the mode string a bit.
5422           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5423       ;; Update the mode line.
5424       (setq mode-line-buffer-identification
5425             (gnus-mode-line-buffer-identification (list mode-string)))
5426       (set-buffer-modified-p t))))
5427
5428 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5429   "Go through the HEADERS list and add all Xrefs to a hash table.
5430 The resulting hash table is returned, or nil if no Xrefs were found."
5431   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5432          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5433          (xref-hashtb (gnus-make-hashtable))
5434          start group entry number xrefs header)
5435     (while headers
5436       (setq header (pop headers))
5437       (when (and (setq xrefs (mail-header-xref header))
5438                  (not (memq (setq number (mail-header-number header))
5439                             unreads)))
5440         (setq start 0)
5441         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5442           (setq start (match-end 0))
5443           (setq group (if prefix
5444                           (concat prefix (substring xrefs (match-beginning 1)
5445                                                     (match-end 1)))
5446                         (substring xrefs (match-beginning 1) (match-end 1))))
5447           (setq number
5448                 (string-to-int (substring xrefs (match-beginning 2)
5449                                           (match-end 2))))
5450           (if (setq entry (gnus-gethash group xref-hashtb))
5451               (setcdr entry (cons number (cdr entry)))
5452             (gnus-sethash group (cons number nil) xref-hashtb)))))
5453     (and start xref-hashtb)))
5454
5455 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5456   "Look through all the headers and mark the Xrefs as read."
5457   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5458         name entry info xref-hashtb idlist method nth4)
5459     (save-excursion
5460       (set-buffer gnus-group-buffer)
5461       (when (setq xref-hashtb
5462                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5463         (mapatoms
5464          (lambda (group)
5465            (unless (string= from-newsgroup (setq name (symbol-name group)))
5466              (setq idlist (symbol-value group))
5467              ;; Dead groups are not updated.
5468              (and (prog1
5469                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5470                             info (nth 2 entry))
5471                     (when (stringp (setq nth4 (gnus-info-method info)))
5472                       (setq nth4 (gnus-server-to-method nth4))))
5473                   ;; Only do the xrefs if the group has the same
5474                   ;; select method as the group we have just read.
5475                   (or (gnus-methods-equal-p
5476                        nth4 (gnus-find-method-for-group from-newsgroup))
5477                       virtual
5478                       (equal nth4 (setq method (gnus-find-method-for-group
5479                                                 from-newsgroup)))
5480                       (and (equal (car nth4) (car method))
5481                            (equal (nth 1 nth4) (nth 1 method))))
5482                   gnus-use-cross-reference
5483                   (or (not (eq gnus-use-cross-reference t))
5484                       virtual
5485                       ;; Only do cross-references on subscribed
5486                       ;; groups, if that is what is wanted.
5487                       (<= (gnus-info-level info) gnus-level-subscribed))
5488                   (gnus-group-make-articles-read name idlist))))
5489          xref-hashtb)))))
5490
5491 (defun gnus-compute-read-articles (group articles)
5492   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5493          (info (nth 2 entry))
5494          (active (gnus-active group))
5495          ninfo)
5496     (when entry
5497       ;; First peel off all invalid article numbers.
5498       (when active
5499         (let ((ids articles)
5500               id first)
5501           (while (setq id (pop ids))
5502             (when (and first (> id (cdr active)))
5503               ;; We'll end up in this situation in one particular
5504               ;; obscure situation.  If you re-scan a group and get
5505               ;; a new article that is cross-posted to a different
5506               ;; group that has not been re-scanned, you might get
5507               ;; crossposted article that has a higher number than
5508               ;; Gnus believes possible.  So we re-activate this
5509               ;; group as well.  This might mean doing the
5510               ;; crossposting thingy will *increase* the number
5511               ;; of articles in some groups.  Tsk, tsk.
5512               (setq active (or (gnus-activate-group group) active)))
5513             (when (or (> id (cdr active))
5514                       (< id (car active)))
5515               (setq articles (delq id articles))))))
5516       ;; If the read list is nil, we init it.
5517       (if (and active
5518                (null (gnus-info-read info))
5519                (> (car active) 1))
5520           (setq ninfo (cons 1 (1- (car active))))
5521         (setq ninfo (gnus-info-read info)))
5522       ;; Then we add the read articles to the range.
5523       (gnus-add-to-range
5524        ninfo (setq articles (sort articles '<))))))
5525
5526 (defun gnus-group-make-articles-read (group articles)
5527   "Update the info of GROUP to say that ARTICLES are read."
5528   (let* ((num 0)
5529          (entry (gnus-gethash group gnus-newsrc-hashtb))
5530          (info (nth 2 entry))
5531          (active (gnus-active group))
5532          range)
5533     (when entry
5534       (setq range (gnus-compute-read-articles group articles))
5535       (save-excursion
5536         (set-buffer gnus-group-buffer)
5537         (gnus-undo-register
5538           `(progn
5539              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5540              (gnus-info-set-read ',info ',(gnus-info-read info))
5541              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5542              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5543              (gnus-group-update-group ,group t))))
5544       ;; Add the read articles to the range.
5545       (gnus-info-set-read info range)
5546       (gnus-request-set-mark group (list (list range 'add '(read))))
5547       ;; Then we have to re-compute how many unread
5548       ;; articles there are in this group.
5549       (when active
5550         (cond
5551          ((not range)
5552           (setq num (- (1+ (cdr active)) (car active))))
5553          ((not (listp (cdr range)))
5554           (setq num (- (cdr active) (- (1+ (cdr range))
5555                                        (car range)))))
5556          (t
5557           (while range
5558             (if (numberp (car range))
5559                 (setq num (1+ num))
5560               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5561             (setq range (cdr range)))
5562           (setq num (- (cdr active) num))))
5563         ;; Update the number of unread articles.
5564         (setcar entry num)
5565         ;; Update the group buffer.
5566         (unless (gnus-ephemeral-group-p group)
5567           (gnus-group-update-group group t))))))
5568
5569 (defvar gnus-newsgroup-none-id 0)
5570
5571 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5572   (let ((cur nntp-server-buffer)
5573         (dependencies
5574          (or dependencies
5575              (save-excursion (set-buffer gnus-summary-buffer)
5576                              gnus-newsgroup-dependencies)))
5577         headers id end ref
5578         (mail-parse-charset gnus-newsgroup-charset)
5579         (mail-parse-ignored-charsets
5580          (save-excursion (condition-case nil
5581                              (set-buffer gnus-summary-buffer)
5582                            (error))
5583                          gnus-newsgroup-ignored-charsets)))
5584     (save-excursion
5585       (set-buffer nntp-server-buffer)
5586       ;; Translate all TAB characters into SPACE characters.
5587       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5588       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5589       (gnus-run-hooks 'gnus-parse-headers-hook)
5590       (let ((case-fold-search t)
5591             in-reply-to header p lines chars)
5592         (goto-char (point-min))
5593         ;; Search to the beginning of the next header.  Error messages
5594         ;; do not begin with 2 or 3.
5595         (while (re-search-forward "^[23][0-9]+ " nil t)
5596           (setq id nil
5597                 ref nil)
5598           ;; This implementation of this function, with nine
5599           ;; search-forwards instead of the one re-search-forward and
5600           ;; a case (which basically was the old function) is actually
5601           ;; about twice as fast, even though it looks messier.  You
5602           ;; can't have everything, I guess.  Speed and elegance
5603           ;; doesn't always go hand in hand.
5604           (setq
5605            header
5606            (vector
5607             ;; Number.
5608             (prog1
5609                 (read cur)
5610               (end-of-line)
5611               (setq p (point))
5612               (narrow-to-region (point)
5613                                 (or (and (search-forward "\n.\n" nil t)
5614                                          (- (point) 2))
5615                                     (point))))
5616             ;; Subject.
5617             (progn
5618               (goto-char p)
5619               (if (search-forward "\nsubject:" nil t)
5620                   (funcall gnus-decode-encoded-word-function
5621                            (nnheader-header-value))
5622                 "(none)"))
5623             ;; From.
5624             (progn
5625               (goto-char p)
5626               (if (search-forward "\nfrom:" nil t)
5627                   (funcall gnus-decode-encoded-word-function
5628                            (nnheader-header-value))
5629                 "(nobody)"))
5630             ;; Date.
5631             (progn
5632               (goto-char p)
5633               (if (search-forward "\ndate:" nil t)
5634                   (nnheader-header-value) ""))
5635             ;; Message-ID.
5636             (progn
5637               (goto-char p)
5638               (setq id (if (re-search-forward
5639                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5640                            ;; We do it this way to make sure the Message-ID
5641                            ;; is (somewhat) syntactically valid.
5642                            (buffer-substring (match-beginning 1)
5643                                              (match-end 1))
5644                          ;; If there was no message-id, we just fake one
5645                          ;; to make subsequent routines simpler.
5646                          (nnheader-generate-fake-message-id))))
5647             ;; References.
5648             (progn
5649               (goto-char p)
5650               (if (search-forward "\nreferences:" nil t)
5651                   (progn
5652                     (setq end (point))
5653                     (prog1
5654                         (nnheader-header-value)
5655                       (setq ref
5656                             (buffer-substring
5657                              (progn
5658                                (end-of-line)
5659                                (search-backward ">" end t)
5660                                (1+ (point)))
5661                              (progn
5662                                (search-backward "<" end t)
5663                                (point))))))
5664                 ;; Get the references from the in-reply-to header if there
5665                 ;; were no references and the in-reply-to header looks
5666                 ;; promising.
5667                 (if (and (search-forward "\nin-reply-to:" nil t)
5668                          (setq in-reply-to (nnheader-header-value))
5669                          (string-match "<[^>]+>" in-reply-to))
5670                     (let (ref2)
5671                       (setq ref (substring in-reply-to (match-beginning 0)
5672                                            (match-end 0)))
5673                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5674                         (setq ref2 (substring in-reply-to (match-beginning 0)
5675                                               (match-end 0)))
5676                         (when (> (length ref2) (length ref))
5677                           (setq ref ref2)))
5678                       ref)
5679                   (setq ref nil))))
5680             ;; Chars.
5681             (progn
5682               (goto-char p)
5683               (if (search-forward "\nchars: " nil t)
5684                   (if (numberp (setq chars (ignore-errors (read cur))))
5685                       chars -1)
5686                 -1))
5687             ;; Lines.
5688             (progn
5689               (goto-char p)
5690               (if (search-forward "\nlines: " nil t)
5691                   (if (numberp (setq lines (ignore-errors (read cur))))
5692                       lines -1)
5693                 -1))
5694             ;; Xref.
5695             (progn
5696               (goto-char p)
5697               (and (search-forward "\nxref:" nil t)
5698                    (nnheader-header-value)))
5699             ;; Extra.
5700             (when gnus-extra-headers
5701               (let ((extra gnus-extra-headers)
5702                     out)
5703                 (while extra
5704                   (goto-char p)
5705                   (when (search-forward
5706                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5707                     (push (cons (car extra) (nnheader-header-value))
5708                           out))
5709                   (pop extra))
5710                 out))))
5711           (when (equal id ref)
5712             (setq ref nil))
5713
5714           (when gnus-alter-header-function
5715             (funcall gnus-alter-header-function header)
5716             (setq id (mail-header-id header)
5717                   ref (gnus-parent-id (mail-header-references header))))
5718
5719           (when (setq header
5720                       (gnus-dependencies-add-header
5721                        header dependencies force-new))
5722             (push header headers))
5723           (goto-char (point-max))
5724           (widen))
5725         (nreverse headers)))))
5726
5727 ;; Goes through the xover lines and returns a list of vectors
5728 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5729                                                   force-new dependencies
5730                                                   group also-fetch-heads)
5731   "Parse the news overview data in the server buffer.
5732 Return a list of headers that match SEQUENCE (see
5733 `nntp-retrieve-headers')."
5734   ;; Get the Xref when the users reads the articles since most/some
5735   ;; NNTP servers do not include Xrefs when using XOVER.
5736   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5737   (let ((mail-parse-charset gnus-newsgroup-charset)
5738         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5739         (cur nntp-server-buffer)
5740         (dependencies (or dependencies gnus-newsgroup-dependencies))
5741         (allp (cond
5742                ((eq gnus-read-all-available-headers t)
5743                 t)
5744                ((stringp gnus-read-all-available-headers)
5745                 (string-match gnus-read-all-available-headers group))
5746                (t
5747                 nil)))
5748         number headers header)
5749     (save-excursion
5750       (set-buffer nntp-server-buffer)
5751       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5752       ;; Allow the user to mangle the headers before parsing them.
5753       (gnus-run-hooks 'gnus-parse-headers-hook)
5754       (goto-char (point-min))
5755       (gnus-parse-without-error
5756         (while (and (or sequence allp)
5757                     (not (eobp)))
5758           (setq number (read cur))
5759           (when (not allp)
5760             (while (and sequence
5761                         (< (car sequence) number))
5762               (setq sequence (cdr sequence))))
5763           (when (and (or allp
5764                          (and sequence
5765                               (eq number (car sequence))))
5766                      (progn
5767                        (setq sequence (cdr sequence))
5768                        (setq header (inline
5769                                       (gnus-nov-parse-line
5770                                        number dependencies force-new)))))
5771             (push header headers))
5772           (forward-line 1)))
5773       ;; A common bug in inn is that if you have posted an article and
5774       ;; then retrieves the active file, it will answer correctly --
5775       ;; the new article is included.  However, a NOV entry for the
5776       ;; article may not have been generated yet, so this may fail.
5777       ;; We work around this problem by retrieving the last few
5778       ;; headers using HEAD.
5779       (if (or (not also-fetch-heads)
5780               (not sequence))
5781           ;; We (probably) got all the headers.
5782           (nreverse headers)
5783         (let ((gnus-nov-is-evil t))
5784           (nconc
5785            (nreverse headers)
5786            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5787              (gnus-get-newsgroup-headers))))))))
5788
5789 (defun gnus-article-get-xrefs ()
5790   "Fill in the Xref value in `gnus-current-headers', if necessary.
5791 This is meant to be called in `gnus-article-internal-prepare-hook'."
5792   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5793                                  gnus-current-headers)))
5794     (or (not gnus-use-cross-reference)
5795         (not headers)
5796         (and (mail-header-xref headers)
5797              (not (string= (mail-header-xref headers) "")))
5798         (let ((case-fold-search t)
5799               xref)
5800           (save-restriction
5801             (nnheader-narrow-to-headers)
5802             (goto-char (point-min))
5803             (when (or (and (not (eobp))
5804                            (eq (downcase (char-after)) ?x)
5805                            (looking-at "Xref:"))
5806                       (search-forward "\nXref:" nil t))
5807               (goto-char (1+ (match-end 0)))
5808               (setq xref (buffer-substring (point)
5809                                            (progn (end-of-line) (point))))
5810               (mail-header-set-xref headers xref)))))))
5811
5812 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5813   "Find article ID and insert the summary line for that article.
5814 OLD-HEADER can either be a header or a line number to insert
5815 the subject line on."
5816   (let* ((line (and (numberp old-header) old-header))
5817          (old-header (and (vectorp old-header) old-header))
5818          (header (cond ((and old-header use-old-header)
5819                         old-header)
5820                        ((and (numberp id)
5821                              (gnus-number-to-header id))
5822                         (gnus-number-to-header id))
5823                        (t
5824                         (gnus-read-header id))))
5825          (number (and (numberp id) id))
5826          d)
5827     (when header
5828       ;; Rebuild the thread that this article is part of and go to the
5829       ;; article we have fetched.
5830       (when (and (not gnus-show-threads)
5831                  old-header)
5832         (when (and number
5833                    (setq d (gnus-data-find (mail-header-number old-header))))
5834           (goto-char (gnus-data-pos d))
5835           (gnus-data-remove
5836            number
5837            (- (gnus-point-at-bol)
5838               (prog1
5839                   (1+ (gnus-point-at-eol))
5840                 (gnus-delete-line))))))
5841       (when old-header
5842         (mail-header-set-number header (mail-header-number old-header)))
5843       (setq gnus-newsgroup-sparse
5844             (delq (setq number (mail-header-number header))
5845                   gnus-newsgroup-sparse))
5846       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5847       (push number gnus-newsgroup-limit)
5848       (gnus-rebuild-thread (mail-header-id header) line)
5849       (gnus-summary-goto-subject number nil t))
5850     (when (and (numberp number)
5851                (> number 0))
5852       ;; We have to update the boundaries even if we can't fetch the
5853       ;; article if ID is a number -- so that the next `P' or `N'
5854       ;; command will fetch the previous (or next) article even
5855       ;; if the one we tried to fetch this time has been canceled.
5856       (when (> number gnus-newsgroup-end)
5857         (setq gnus-newsgroup-end number))
5858       (when (< number gnus-newsgroup-begin)
5859         (setq gnus-newsgroup-begin number))
5860       (setq gnus-newsgroup-unselected
5861             (delq number gnus-newsgroup-unselected)))
5862     ;; Report back a success?
5863     (and header (mail-header-number header))))
5864
5865 ;;; Process/prefix in the summary buffer
5866
5867 (defun gnus-summary-work-articles (n)
5868   "Return a list of articles to be worked upon.
5869 The prefix argument, the list of process marked articles, and the
5870 current article will be taken into consideration."
5871   (save-excursion
5872     (set-buffer gnus-summary-buffer)
5873     (cond
5874      (n
5875       ;; A numerical prefix has been given.
5876       (setq n (prefix-numeric-value n))
5877       (let ((backward (< n 0))
5878             (n (abs (prefix-numeric-value n)))
5879             articles article)
5880         (save-excursion
5881           (while
5882               (and (> n 0)
5883                    (push (setq article (gnus-summary-article-number))
5884                          articles)
5885                    (if backward
5886                        (gnus-summary-find-prev nil article)
5887                      (gnus-summary-find-next nil article)))
5888             (decf n)))
5889         (nreverse articles)))
5890      ((and (gnus-region-active-p) (mark))
5891       (message "region active")
5892       ;; Work on the region between point and mark.
5893       (let ((max (max (point) (mark)))
5894             articles article)
5895         (save-excursion
5896           (goto-char (min (min (point) (mark))))
5897           (while
5898               (and
5899                (push (setq article (gnus-summary-article-number)) articles)
5900                (gnus-summary-find-next nil article)
5901                (< (point) max)))
5902           (nreverse articles))))
5903      (gnus-newsgroup-processable
5904       ;; There are process-marked articles present.
5905       ;; Save current state.
5906       (gnus-summary-save-process-mark)
5907       ;; Return the list.
5908       (reverse gnus-newsgroup-processable))
5909      (t
5910       ;; Just return the current article.
5911       (list (gnus-summary-article-number))))))
5912
5913 (defmacro gnus-summary-iterate (arg &rest forms)
5914   "Iterate over the process/prefixed articles and do FORMS.
5915 ARG is the interactive prefix given to the command.  FORMS will be
5916 executed with point over the summary line of the articles."
5917   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5918     `(let ((,articles (gnus-summary-work-articles ,arg)))
5919        (while ,articles
5920          (gnus-summary-goto-subject (car ,articles))
5921          ,@forms
5922          (pop ,articles)))))
5923
5924 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5925 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5926
5927 (defun gnus-summary-save-process-mark ()
5928   "Push the current set of process marked articles on the stack."
5929   (interactive)
5930   (push (copy-sequence gnus-newsgroup-processable)
5931         gnus-newsgroup-process-stack))
5932
5933 (defun gnus-summary-kill-process-mark ()
5934   "Push the current set of process marked articles on the stack and unmark."
5935   (interactive)
5936   (gnus-summary-save-process-mark)
5937   (gnus-summary-unmark-all-processable))
5938
5939 (defun gnus-summary-yank-process-mark ()
5940   "Pop the last process mark state off the stack and restore it."
5941   (interactive)
5942   (unless gnus-newsgroup-process-stack
5943     (error "Empty mark stack"))
5944   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5945
5946 (defun gnus-summary-process-mark-set (set)
5947   "Make SET into the current process marked articles."
5948   (gnus-summary-unmark-all-processable)
5949   (while set
5950     (gnus-summary-set-process-mark (pop set))))
5951
5952 ;;; Searching and stuff
5953
5954 (defun gnus-summary-search-group (&optional backward use-level)
5955   "Search for next unread newsgroup.
5956 If optional argument BACKWARD is non-nil, search backward instead."
5957   (save-excursion
5958     (set-buffer gnus-group-buffer)
5959     (when (gnus-group-search-forward
5960            backward nil (if use-level (gnus-group-group-level) nil))
5961       (gnus-group-group-name))))
5962
5963 (defun gnus-summary-best-group (&optional exclude-group)
5964   "Find the name of the best unread group.
5965 If EXCLUDE-GROUP, do not go to this group."
5966   (save-excursion
5967     (set-buffer gnus-group-buffer)
5968     (save-excursion
5969       (gnus-group-best-unread-group exclude-group))))
5970
5971 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5972   (if backward (gnus-summary-find-prev)
5973     (let* ((dummy (gnus-summary-article-intangible-p))
5974            (article (or article (gnus-summary-article-number)))
5975            (arts (gnus-data-find-list article))
5976            result)
5977       (when (and (not dummy)
5978                  (or (not gnus-summary-check-current)
5979                      (not unread)
5980                      (not (gnus-data-unread-p (car arts)))))
5981         (setq arts (cdr arts)))
5982       (when (setq result
5983                   (if unread
5984                       (progn
5985                         (while arts
5986                           (when (or (and undownloaded
5987                                          (memq (car arts) gnus-newsgroup-undownloaded))
5988                                     (gnus-data-unread-p (car arts)))
5989                             (setq result (car arts)
5990                                   arts nil))
5991                           (setq arts (cdr arts)))
5992                         result)
5993                     (car arts)))
5994         (goto-char (gnus-data-pos result))
5995         (gnus-data-number result)))))
5996
5997 (defun gnus-summary-find-prev (&optional unread article)
5998   (let* ((eobp (eobp))
5999          (article (or article (gnus-summary-article-number)))
6000          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6001          result)
6002     (when (and (not eobp)
6003                (or (not gnus-summary-check-current)
6004                    (not unread)
6005                    (not (gnus-data-unread-p (car arts)))))
6006       (setq arts (cdr arts)))
6007     (when (setq result
6008                 (if unread
6009                     (progn
6010                       (while arts
6011                         (when (gnus-data-unread-p (car arts))
6012                           (setq result (car arts)
6013                                 arts nil))
6014                         (setq arts (cdr arts)))
6015                       result)
6016                   (car arts)))
6017       (goto-char (gnus-data-pos result))
6018       (gnus-data-number result))))
6019
6020 (defun gnus-summary-find-subject (subject &optional unread backward article)
6021   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6022          (article (or article (gnus-summary-article-number)))
6023          (articles (gnus-data-list backward))
6024          (arts (gnus-data-find-list article articles))
6025          result)
6026     (when (or (not gnus-summary-check-current)
6027               (not unread)
6028               (not (gnus-data-unread-p (car arts))))
6029       (setq arts (cdr arts)))
6030     (while arts
6031       (and (or (not unread)
6032                (gnus-data-unread-p (car arts)))
6033            (vectorp (gnus-data-header (car arts)))
6034            (gnus-subject-equal
6035             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6036            (setq result (car arts)
6037                  arts nil))
6038       (setq arts (cdr arts)))
6039     (and result
6040          (goto-char (gnus-data-pos result))
6041          (gnus-data-number result))))
6042
6043 (defun gnus-summary-search-forward (&optional unread subject backward)
6044   "Search forward for an article.
6045 If UNREAD, look for unread articles.  If SUBJECT, look for
6046 articles with that subject.  If BACKWARD, search backward instead."
6047   (cond (subject (gnus-summary-find-subject subject unread backward))
6048         (backward (gnus-summary-find-prev unread))
6049         (t (gnus-summary-find-next unread))))
6050
6051 (defun gnus-recenter (&optional n)
6052   "Center point in window and redisplay frame.
6053 Also do horizontal recentering."
6054   (interactive "P")
6055   (when (and gnus-auto-center-summary
6056              (not (eq gnus-auto-center-summary 'vertical)))
6057     (gnus-horizontal-recenter))
6058   (recenter n))
6059
6060 (defun gnus-summary-recenter ()
6061   "Center point in the summary window.
6062 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6063 displayed, no centering will be performed."
6064   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6065 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6066   (interactive)
6067   (let* ((top (cond ((< (window-height) 4) 0)
6068                     ((< (window-height) 7) 1)
6069                     (t (if (numberp gnus-auto-center-summary)
6070                            gnus-auto-center-summary
6071                          2))))
6072          (height (1- (window-height)))
6073          (bottom (save-excursion (goto-char (point-max))
6074                                  (forward-line (- height))
6075                                  (point)))
6076          (window (get-buffer-window (current-buffer))))
6077     ;; The user has to want it.
6078     (when gnus-auto-center-summary
6079       (when (get-buffer-window gnus-article-buffer)
6080         ;; Only do recentering when the article buffer is displayed,
6081       ;; Set the window start to either `bottom', which is the biggest
6082         ;; possible valid number, or the second line from the top,
6083         ;; whichever is the least.
6084         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6085           (if (> bottom top-pos)
6086               ;; Keep the second line from the top visible
6087               (set-window-start window top-pos t)
6088             ;; Try to keep the bottom line visible; if it's partially
6089             ;; obscured, either scroll one more line to make it fully
6090             ;; visible, or revert to using TOP-POS.
6091             (save-excursion
6092               (goto-char (point-max))
6093               (forward-line -1)
6094               (let ((last-line-start (point)))
6095                 (goto-char bottom)
6096                 (set-window-start window (point) t)
6097                 (when (not (pos-visible-in-window-p last-line-start window))
6098                   (forward-line 1)
6099                   (set-window-start window (min (point) top-pos) t)))))))
6100       ;; Do horizontal recentering while we're at it.
6101       (when (and (get-buffer-window (current-buffer) t)
6102                  (not (eq gnus-auto-center-summary 'vertical)))
6103         (let ((selected (selected-window)))
6104           (select-window (get-buffer-window (current-buffer) t))
6105           (gnus-summary-position-point)
6106           (gnus-horizontal-recenter)
6107           (select-window selected))))))
6108
6109 (defun gnus-summary-jump-to-group (newsgroup)
6110   "Move point to NEWSGROUP in group mode buffer."
6111   ;; Keep update point of group mode buffer if visible.
6112   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6113       (save-window-excursion
6114         ;; Take care of tree window mode.
6115         (when (get-buffer-window gnus-group-buffer)
6116           (pop-to-buffer gnus-group-buffer))
6117         (gnus-group-jump-to-group newsgroup))
6118     (save-excursion
6119       ;; Take care of tree window mode.
6120       (if (get-buffer-window gnus-group-buffer)
6121           (pop-to-buffer gnus-group-buffer)
6122         (set-buffer gnus-group-buffer))
6123       (gnus-group-jump-to-group newsgroup))))
6124
6125 ;; This function returns a list of article numbers based on the
6126 ;; difference between the ranges of read articles in this group and
6127 ;; the range of active articles.
6128 (defun gnus-list-of-unread-articles (group)
6129   (let* ((read (gnus-info-read (gnus-get-info group)))
6130          (active (or (gnus-active group) (gnus-activate-group group)))
6131          (last (cdr active))
6132          first nlast unread)
6133     ;; If none are read, then all are unread.
6134     (if (not read)
6135         (setq first (car active))
6136       ;; If the range of read articles is a single range, then the
6137       ;; first unread article is the article after the last read
6138       ;; article.  Sounds logical, doesn't it?
6139       (if (and (not (listp (cdr read)))
6140                (or (< (car read) (car active))
6141                    (progn (setq read (list read))
6142                           nil)))
6143           (setq first (max (car active) (1+ (cdr read))))
6144         ;; `read' is a list of ranges.
6145         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6146                                   (caar read)))
6147                   1)
6148           (setq first (car active)))
6149         (while read
6150           (when first
6151             (while (< first nlast)
6152               (push first unread)
6153               (setq first (1+ first))))
6154           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6155           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6156           (setq read (cdr read)))))
6157     ;; And add the last unread articles.
6158     (while (<= first last)
6159       (push first unread)
6160       (setq first (1+ first)))
6161     ;; Return the list of unread articles.
6162     (delq 0 (nreverse unread))))
6163
6164 (defun gnus-list-of-read-articles (group)
6165   "Return a list of unread, unticked and non-dormant articles."
6166   (let* ((info (gnus-get-info group))
6167          (marked (gnus-info-marks info))
6168          (active (gnus-active group)))
6169     (and info active
6170          (gnus-list-range-difference
6171           (gnus-list-range-difference
6172            (gnus-sorted-complement
6173             (gnus-uncompress-range active)
6174             (gnus-list-of-unread-articles group))
6175            (cdr (assq 'dormant marked)))
6176           (cdr (assq 'tick marked))))))
6177
6178 ;; Various summary commands
6179
6180 (defun gnus-summary-select-article-buffer ()
6181   "Reconfigure windows to show article buffer."
6182   (interactive)
6183   (if (not (gnus-buffer-live-p gnus-article-buffer))
6184       (error "There is no article buffer for this summary buffer")
6185     (gnus-configure-windows 'article)
6186     (select-window (get-buffer-window gnus-article-buffer))))
6187
6188 (defun gnus-summary-universal-argument (arg)
6189   "Perform any operation on all articles that are process/prefixed."
6190   (interactive "P")
6191   (let ((articles (gnus-summary-work-articles arg))
6192         func article)
6193     (if (eq
6194          (setq
6195           func
6196           (key-binding
6197            (read-key-sequence
6198             (substitute-command-keys
6199              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6200          'undefined)
6201         (gnus-error 1 "Undefined key")
6202       (save-excursion
6203         (while articles
6204           (gnus-summary-goto-subject (setq article (pop articles)))
6205           (let (gnus-newsgroup-processable)
6206             (command-execute func))
6207           (gnus-summary-remove-process-mark article)))))
6208   (gnus-summary-position-point))
6209
6210 (defun gnus-summary-toggle-truncation (&optional arg)
6211   "Toggle truncation of summary lines.
6212 With arg, turn line truncation on if arg is positive."
6213   (interactive "P")
6214   (setq truncate-lines
6215         (if (null arg) (not truncate-lines)
6216           (> (prefix-numeric-value arg) 0)))
6217   (redraw-display))
6218
6219 (defun gnus-summary-find-uncancelled ()
6220   "Return the number of an uncancelled article.
6221 The current article is considered, then following articles, then previous
6222 articles.  If all articles are cancelled then return a dummy 0."
6223   (let (found)
6224     (dolist (rev '(nil t))
6225       (unless found      ; don't demand the reverse list if we don't need it
6226         (let ((data (gnus-data-find-list
6227                      (gnus-summary-article-number) (gnus-data-list rev))))
6228           (while (and data (not found))
6229             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6230                 (setq found (gnus-data-number (car data))))
6231             (setq data (cdr data))))))
6232     (or found 0)))
6233
6234 (defun gnus-summary-reselect-current-group (&optional all rescan)
6235   "Exit and then reselect the current newsgroup.
6236 The prefix argument ALL means to select all articles."
6237   (interactive "P")
6238   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6239     (error "Ephemeral groups can't be reselected"))
6240   (let ((current-subject (gnus-summary-find-uncancelled))
6241         (group gnus-newsgroup-name))
6242     (setq gnus-newsgroup-begin nil)
6243     (gnus-summary-exit)
6244     ;; We have to adjust the point of group mode buffer because
6245     ;; point was moved to the next unread newsgroup by exiting.
6246     (gnus-summary-jump-to-group group)
6247     (when rescan
6248       (save-excursion
6249         (gnus-group-get-new-news-this-group 1)))
6250     (gnus-group-read-group all t)
6251     (gnus-summary-goto-subject current-subject nil t)))
6252
6253 (defun gnus-summary-rescan-group (&optional all)
6254   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6255   (interactive "P")
6256   (gnus-summary-reselect-current-group all t))
6257
6258 (defun gnus-summary-update-info (&optional non-destructive)
6259   (save-excursion
6260     (let ((group gnus-newsgroup-name))
6261       (when group
6262         (when gnus-newsgroup-kill-headers
6263           (setq gnus-newsgroup-killed
6264                 (gnus-compress-sequence
6265                  (gnus-sorted-union
6266                   (gnus-list-range-intersection
6267                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6268                   gnus-newsgroup-unreads)
6269                  t)))
6270         (unless (listp (cdr gnus-newsgroup-killed))
6271           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6272         (let ((headers gnus-newsgroup-headers))
6273           ;; Set the new ranges of read articles.
6274           (save-excursion
6275             (set-buffer gnus-group-buffer)
6276             (gnus-undo-force-boundary))
6277           (gnus-update-read-articles
6278            group (gnus-sorted-union
6279                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6280           ;; Set the current article marks.
6281           (let ((gnus-newsgroup-scored
6282                  (if (and (not gnus-save-score)
6283                           (not non-destructive))
6284                      nil
6285                    gnus-newsgroup-scored)))
6286             (save-excursion
6287               (gnus-update-marks)))
6288           ;; Do the cross-ref thing.
6289           (when gnus-use-cross-reference
6290             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6291           ;; Do not switch windows but change the buffer to work.
6292           (set-buffer gnus-group-buffer)
6293           (unless (gnus-ephemeral-group-p group)
6294             (gnus-group-update-group group)))))))
6295
6296 (defun gnus-summary-save-newsrc (&optional force)
6297   "Save the current number of read/marked articles in the dribble buffer.
6298 The dribble buffer will then be saved.
6299 If FORCE (the prefix), also save the .newsrc file(s)."
6300   (interactive "P")
6301   (gnus-summary-update-info t)
6302   (if force
6303       (gnus-save-newsrc-file)
6304     (gnus-dribble-save)))
6305
6306 (defun gnus-summary-exit (&optional temporary)
6307   "Exit reading current newsgroup, and then return to group selection mode.
6308 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6309   (interactive)
6310   (gnus-set-global-variables)
6311   (when (gnus-buffer-live-p gnus-article-buffer)
6312     (save-excursion
6313       (set-buffer gnus-article-buffer)
6314       (mm-destroy-parts gnus-article-mime-handles)
6315       ;; Set it to nil for safety reason.
6316       (setq gnus-article-mime-handle-alist nil)
6317       (setq gnus-article-mime-handles nil)))
6318   (gnus-kill-save-kill-buffer)
6319   (gnus-async-halt-prefetch)
6320   (let* ((group gnus-newsgroup-name)
6321          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6322          (gnus-group-is-exiting-p t)
6323          (mode major-mode)
6324          (group-point nil)
6325          (buf (current-buffer)))
6326     (unless quit-config
6327       ;; Do adaptive scoring, and possibly save score files.
6328       (when gnus-newsgroup-adaptive
6329         (gnus-score-adaptive))
6330       (when gnus-use-scoring
6331         (gnus-score-save)))
6332     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6333     ;; If we have several article buffers, we kill them at exit.
6334     (unless gnus-single-article-buffer
6335       (gnus-kill-buffer gnus-original-article-buffer)
6336       (setq gnus-article-current nil))
6337     (when gnus-use-cache
6338       (gnus-cache-possibly-remove-articles)
6339       (gnus-cache-save-buffers))
6340     (gnus-async-prefetch-remove-group group)
6341     (when gnus-suppress-duplicates
6342       (gnus-dup-enter-articles))
6343     (when gnus-use-trees
6344       (gnus-tree-close group))
6345     (when gnus-use-cache
6346       (gnus-cache-write-active))
6347     ;; Remove entries for this group.
6348     (nnmail-purge-split-history (gnus-group-real-name group))
6349     ;; Make all changes in this group permanent.
6350     (unless quit-config
6351       (gnus-run-hooks 'gnus-exit-group-hook)
6352       (gnus-summary-update-info))
6353     (gnus-close-group group)
6354     ;; Make sure where we were, and go to next newsgroup.
6355     (set-buffer gnus-group-buffer)
6356     (unless quit-config
6357       (gnus-group-jump-to-group group))
6358     (gnus-run-hooks 'gnus-summary-exit-hook)
6359     (unless (or quit-config
6360                 ;; If this group has disappeared from the summary
6361                 ;; buffer, don't skip forwards.
6362                 (not (string= group (gnus-group-group-name))))
6363       (gnus-group-next-unread-group 1))
6364     (setq group-point (point))
6365     (if temporary
6366         nil                             ;Nothing to do.
6367       ;; If we have several article buffers, we kill them at exit.
6368       (unless gnus-single-article-buffer
6369         (gnus-kill-buffer gnus-article-buffer)
6370         (gnus-kill-buffer gnus-original-article-buffer)
6371         (setq gnus-article-current nil))
6372       (set-buffer buf)
6373       (if (not gnus-kill-summary-on-exit)
6374           (progn
6375             (gnus-deaden-summary)
6376             (setq mode nil))
6377         ;; We set all buffer-local variables to nil.  It is unclear why
6378         ;; this is needed, but if we don't, buffer-local variables are
6379         ;; not garbage-collected, it seems.  This would the lead to en
6380         ;; ever-growing Emacs.
6381         (gnus-summary-clear-local-variables)
6382         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6383           (gnus-summary-clear-local-variables))
6384         (when (get-buffer gnus-article-buffer)
6385           (bury-buffer gnus-article-buffer))
6386         ;; We clear the global counterparts of the buffer-local
6387         ;; variables as well, just to be on the safe side.
6388         (set-buffer gnus-group-buffer)
6389         (gnus-summary-clear-local-variables)
6390         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6391           (gnus-summary-clear-local-variables)))
6392       (setq gnus-current-select-method gnus-select-method)
6393       (pop-to-buffer gnus-group-buffer)
6394       (if (not quit-config)
6395           (progn
6396             (goto-char group-point)
6397             (gnus-configure-windows 'group 'force))
6398         (gnus-handle-ephemeral-exit quit-config))
6399       ;; Return to group mode buffer.
6400       (when (eq mode 'gnus-summary-mode)
6401         (gnus-kill-buffer buf))
6402       ;; Clear the current group name.
6403       (unless quit-config
6404         (setq gnus-newsgroup-name nil)))))
6405
6406 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6407 (defun gnus-summary-exit-no-update (&optional no-questions)
6408   "Quit reading current newsgroup without updating read article info."
6409   (interactive)
6410   (let* ((group gnus-newsgroup-name)
6411          (gnus-group-is-exiting-p t)
6412          (quit-config (gnus-group-quit-config group)))
6413     (when (or no-questions
6414               gnus-expert-user
6415               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6416       (gnus-async-halt-prefetch)
6417       (mapcar 'funcall
6418               (delq 'gnus-summary-expire-articles
6419                     (copy-sequence gnus-summary-prepare-exit-hook)))
6420       (when (gnus-buffer-live-p gnus-article-buffer)
6421         (save-excursion
6422           (set-buffer gnus-article-buffer)
6423           (mm-destroy-parts gnus-article-mime-handles)
6424           ;; Set it to nil for safety reason.
6425           (setq gnus-article-mime-handle-alist nil)
6426           (setq gnus-article-mime-handles nil)))
6427       ;; If we have several article buffers, we kill them at exit.
6428       (unless gnus-single-article-buffer
6429         (gnus-kill-buffer gnus-article-buffer)
6430         (gnus-kill-buffer gnus-original-article-buffer)
6431         (setq gnus-article-current nil))
6432       (if (not gnus-kill-summary-on-exit)
6433           (gnus-deaden-summary)
6434         (gnus-close-group group)
6435         (gnus-summary-clear-local-variables)
6436         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6437           (gnus-summary-clear-local-variables))
6438         (set-buffer gnus-group-buffer)
6439         (gnus-summary-clear-local-variables)
6440         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6441           (gnus-summary-clear-local-variables))
6442         (when (get-buffer gnus-summary-buffer)
6443           (kill-buffer gnus-summary-buffer)))
6444       (unless gnus-single-article-buffer
6445         (setq gnus-article-current nil))
6446       (when gnus-use-trees
6447         (gnus-tree-close group))
6448       (gnus-async-prefetch-remove-group group)
6449       (when (get-buffer gnus-article-buffer)
6450         (bury-buffer gnus-article-buffer))
6451       ;; Return to the group buffer.
6452       (gnus-configure-windows 'group 'force)
6453       ;; Clear the current group name.
6454       (setq gnus-newsgroup-name nil)
6455       (unless (gnus-ephemeral-group-p group)
6456         (gnus-group-update-group group))
6457       (when (equal (gnus-group-group-name) group)
6458         (gnus-group-next-unread-group 1))
6459       (when quit-config
6460         (gnus-handle-ephemeral-exit quit-config)))))
6461
6462 (defun gnus-handle-ephemeral-exit (quit-config)
6463   "Handle movement when leaving an ephemeral group.
6464 The state which existed when entering the ephemeral is reset."
6465   (if (not (buffer-name (car quit-config)))
6466       (gnus-configure-windows 'group 'force)
6467     (set-buffer (car quit-config))
6468     (cond ((eq major-mode 'gnus-summary-mode)
6469            (gnus-set-global-variables))
6470           ((eq major-mode 'gnus-article-mode)
6471            (save-excursion
6472              ;; The `gnus-summary-buffer' variable may point
6473              ;; to the old summary buffer when using a single
6474              ;; article buffer.
6475              (unless (gnus-buffer-live-p gnus-summary-buffer)
6476                (set-buffer gnus-group-buffer))
6477              (set-buffer gnus-summary-buffer)
6478              (gnus-set-global-variables))))
6479     (if (or (eq (cdr quit-config) 'article)
6480             (eq (cdr quit-config) 'pick))
6481         (progn
6482           ;; The current article may be from the ephemeral group
6483           ;; thus it is best that we reload this article
6484           (gnus-summary-show-article)
6485           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6486               (gnus-configure-windows 'pick 'force)
6487             (gnus-configure-windows (cdr quit-config) 'force)))
6488       (gnus-configure-windows (cdr quit-config) 'force))
6489     (when (eq major-mode 'gnus-summary-mode)
6490       (gnus-summary-next-subject 1 nil t)
6491       (gnus-summary-recenter)
6492       (gnus-summary-position-point))))
6493
6494 ;;; Dead summaries.
6495
6496 (defvar gnus-dead-summary-mode-map nil)
6497
6498 (unless gnus-dead-summary-mode-map
6499   (setq gnus-dead-summary-mode-map (make-keymap))
6500   (suppress-keymap gnus-dead-summary-mode-map)
6501   (substitute-key-definition
6502    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6503   (dolist (key '("\C-d" "\r" "\177" [delete]))
6504     (define-key gnus-dead-summary-mode-map
6505       key 'gnus-summary-wake-up-the-dead))
6506   (dolist (key '("q" "Q"))
6507     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6508
6509 (defvar gnus-dead-summary-mode nil
6510   "Minor mode for Gnus summary buffers.")
6511
6512 (defun gnus-dead-summary-mode (&optional arg)
6513   "Minor mode for Gnus summary buffers."
6514   (interactive "P")
6515   (when (eq major-mode 'gnus-summary-mode)
6516     (make-local-variable 'gnus-dead-summary-mode)
6517     (setq gnus-dead-summary-mode
6518           (if (null arg) (not gnus-dead-summary-mode)
6519             (> (prefix-numeric-value arg) 0)))
6520     (when gnus-dead-summary-mode
6521       (gnus-add-minor-mode
6522        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6523
6524 (defun gnus-deaden-summary ()
6525   "Make the current summary buffer into a dead summary buffer."
6526   ;; Kill any previous dead summary buffer.
6527   (when (and gnus-dead-summary
6528              (buffer-name gnus-dead-summary))
6529     (save-excursion
6530       (set-buffer gnus-dead-summary)
6531       (when gnus-dead-summary-mode
6532         (kill-buffer (current-buffer)))))
6533   ;; Make this the current dead summary.
6534   (setq gnus-dead-summary (current-buffer))
6535   (gnus-dead-summary-mode 1)
6536   (let ((name (buffer-name)))
6537     (when (string-match "Summary" name)
6538       (rename-buffer
6539        (concat (substring name 0 (match-beginning 0)) "Dead "
6540                (substring name (match-beginning 0)))
6541        t)
6542       (bury-buffer))))
6543
6544 (defun gnus-kill-or-deaden-summary (buffer)
6545   "Kill or deaden the summary BUFFER."
6546   (save-excursion
6547     (when (and (buffer-name buffer)
6548                (not gnus-single-article-buffer))
6549       (save-excursion
6550         (set-buffer buffer)
6551         (gnus-kill-buffer gnus-article-buffer)
6552         (gnus-kill-buffer gnus-original-article-buffer)))
6553     (cond
6554      ;; Kill the buffer.
6555      (gnus-kill-summary-on-exit
6556       (when (and gnus-use-trees
6557                  (gnus-buffer-exists-p buffer))
6558         (save-excursion
6559           (set-buffer buffer)
6560           (gnus-tree-close gnus-newsgroup-name)))
6561       (gnus-kill-buffer buffer))
6562      ;; Deaden the buffer.
6563      ((gnus-buffer-exists-p buffer)
6564       (save-excursion
6565         (set-buffer buffer)
6566         (gnus-deaden-summary))))))
6567
6568 (defun gnus-summary-wake-up-the-dead (&rest args)
6569   "Wake up the dead summary buffer."
6570   (interactive)
6571   (gnus-dead-summary-mode -1)
6572   (let ((name (buffer-name)))
6573     (when (string-match "Dead " name)
6574       (rename-buffer
6575        (concat (substring name 0 (match-beginning 0))
6576                (substring name (match-end 0)))
6577        t)))
6578   (gnus-message 3 "This dead summary is now alive again"))
6579
6580 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6581 (defun gnus-summary-fetch-faq (&optional faq-dir)
6582   "Fetch the FAQ for the current group.
6583 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6584 in."
6585   (interactive
6586    (list
6587     (when current-prefix-arg
6588       (completing-read
6589        "FAQ dir: " (and (listp gnus-group-faq-directory)
6590                         (mapcar (lambda (file) (list file))
6591                                 gnus-group-faq-directory))))))
6592   (let (gnus-faq-buffer)
6593     (when (setq gnus-faq-buffer
6594                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6595       (gnus-configure-windows 'summary-faq))))
6596
6597 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6598 (defun gnus-summary-describe-group (&optional force)
6599   "Describe the current newsgroup."
6600   (interactive "P")
6601   (gnus-group-describe-group force gnus-newsgroup-name))
6602
6603 (defun gnus-summary-describe-briefly ()
6604   "Describe summary mode commands briefly."
6605   (interactive)
6606   (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")))
6607
6608 ;; Walking around group mode buffer from summary mode.
6609
6610 (defun gnus-summary-next-group (&optional no-article target-group backward)
6611   "Exit current newsgroup and then select next unread newsgroup.
6612 If prefix argument NO-ARTICLE is non-nil, no article is selected
6613 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6614 previous group instead."
6615   (interactive "P")
6616   ;; Stop pre-fetching.
6617   (gnus-async-halt-prefetch)
6618   (let ((current-group gnus-newsgroup-name)
6619         (current-buffer (current-buffer))
6620         entered)
6621     (while (not entered)
6622       ;; Then we find what group we are supposed to enter.
6623       (set-buffer gnus-group-buffer)
6624       (gnus-group-jump-to-group current-group)
6625       (setq target-group
6626             (or target-group
6627                 (if (eq gnus-keep-same-level 'best)
6628                     (gnus-summary-best-group gnus-newsgroup-name)
6629                   (gnus-summary-search-group backward gnus-keep-same-level))))
6630       (if (not target-group)
6631           ;; There are no further groups, so we return to the group
6632           ;; buffer.
6633           (progn
6634             (gnus-message 5 "Returning to the group buffer")
6635             (setq entered t)
6636             (when (gnus-buffer-live-p current-buffer)
6637               (set-buffer current-buffer)
6638               (gnus-summary-exit))
6639             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6640         ;; We try to enter the target group.
6641         (gnus-group-jump-to-group target-group)
6642         (let ((unreads (gnus-group-group-unread)))
6643           (if (and (or (eq t unreads)
6644                        (and unreads (not (zerop unreads))))
6645                    (progn
6646                      ;; Now we semi-exit this group to update Xrefs
6647                      ;; and all variables.  We can't do a real exit,
6648                      ;; because the window conf must remain the same
6649                      ;; in case the user is prompted for info, and we
6650                      ;; don't want the window conf to change before
6651                      ;; that...
6652                      (when (gnus-buffer-live-p current-buffer)
6653                        (set-buffer current-buffer)
6654                        (gnus-summary-exit t))
6655                      (gnus-summary-read-group
6656                       target-group nil no-article
6657                       (and (buffer-name current-buffer) current-buffer)
6658                       nil backward)))
6659               (setq entered t)
6660             (setq current-group target-group
6661                   target-group nil)))))))
6662
6663 (defun gnus-summary-prev-group (&optional no-article)
6664   "Exit current newsgroup and then select previous unread newsgroup.
6665 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6666   (interactive "P")
6667   (gnus-summary-next-group no-article nil t))
6668
6669 ;; Walking around summary lines.
6670
6671 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6672   "Go to the first unread subject.
6673 If UNREAD is non-nil, go to the first unread article.
6674 Returns the article selected or nil if there are no unread articles."
6675   (interactive "P")
6676   (prog1
6677       (cond
6678        ;; Empty summary.
6679        ((null gnus-newsgroup-data)
6680         (gnus-message 3 "No articles in the group")
6681         nil)
6682        ;; Pick the first article.
6683        ((not unread)
6684         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6685         (gnus-data-number (car gnus-newsgroup-data)))
6686        ;; No unread articles.
6687        ((null gnus-newsgroup-unreads)
6688         (gnus-message 3 "No more unread articles")
6689         nil)
6690        ;; Find the first unread article.
6691        (t
6692         (let ((data gnus-newsgroup-data))
6693           (while (and data
6694                       (and (not (and undownloaded
6695                                      (memq (car data)
6696                                            gnus-newsgroup-undownloaded)))
6697                            (if unseen
6698                                (or (not (memq
6699                                          (gnus-data-number (car data))
6700                                          gnus-newsgroup-unseen))
6701                                    (not (gnus-data-unread-p (car data))))
6702                              (not (gnus-data-unread-p (car data))))))
6703             (setq data (cdr data)))
6704           (when data
6705             (goto-char (gnus-data-pos (car data)))
6706             (gnus-data-number (car data))))))
6707     (gnus-summary-position-point)))
6708
6709 (defun gnus-summary-next-subject (n &optional unread dont-display)
6710   "Go to next N'th summary line.
6711 If N is negative, go to the previous N'th subject line.
6712 If UNREAD is non-nil, only unread articles are selected.
6713 The difference between N and the actual number of steps taken is
6714 returned."
6715   (interactive "p")
6716   (let ((backward (< n 0))
6717         (n (abs n)))
6718     (while (and (> n 0)
6719                 (if backward
6720                     (gnus-summary-find-prev unread)
6721                   (gnus-summary-find-next unread)))
6722       (unless (zerop (setq n (1- n)))
6723         (gnus-summary-show-thread)))
6724     (when (/= 0 n)
6725       (gnus-message 7 "No more%s articles"
6726                     (if unread " unread" "")))
6727     (unless dont-display
6728       (gnus-summary-recenter)
6729       (gnus-summary-position-point))
6730     n))
6731
6732 (defun gnus-summary-next-unread-subject (n)
6733   "Go to next N'th unread summary line."
6734   (interactive "p")
6735   (gnus-summary-next-subject n t))
6736
6737 (defun gnus-summary-prev-subject (n &optional unread)
6738   "Go to previous N'th summary line.
6739 If optional argument UNREAD is non-nil, only unread article is selected."
6740   (interactive "p")
6741   (gnus-summary-next-subject (- n) unread))
6742
6743 (defun gnus-summary-prev-unread-subject (n)
6744   "Go to previous N'th unread summary line."
6745   (interactive "p")
6746   (gnus-summary-next-subject (- n) t))
6747
6748 (defun gnus-summary-goto-subjects (articles)
6749   "Insert the subject header for ARTICLES in the current buffer."
6750   (save-excursion
6751     (dolist (article articles)
6752       (gnus-summary-goto-subject article t)))
6753   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6754   (gnus-summary-position-point))
6755   
6756 (defun gnus-summary-goto-subject (article &optional force silent)
6757   "Go the subject line of ARTICLE.
6758 If FORCE, also allow jumping to articles not currently shown."
6759   (interactive "nArticle number: ")
6760   (unless (numberp article)
6761     (error "Article %s is not a number" article))
6762   (let ((b (point))
6763         (data (gnus-data-find article)))
6764     ;; We read in the article if we have to.
6765     (and (not data)
6766          force
6767          (gnus-summary-insert-subject
6768           article
6769           (if (or (numberp force) (vectorp force)) force)
6770           t)
6771          (setq data (gnus-data-find article)))
6772     (goto-char b)
6773     (if (not data)
6774         (progn
6775           (unless silent
6776             (gnus-message 3 "Can't find article %d" article))
6777           nil)
6778       (let ((pt (gnus-data-pos data)))
6779         (goto-char pt)
6780         (gnus-summary-set-article-display-arrow pt))
6781       (gnus-summary-position-point)
6782       article)))
6783
6784 ;; Walking around summary lines with displaying articles.
6785
6786 (defun gnus-summary-expand-window (&optional arg)
6787   "Make the summary buffer take up the entire Emacs frame.
6788 Given a prefix, will force an `article' buffer configuration."
6789   (interactive "P")
6790   (if arg
6791       (gnus-configure-windows 'article 'force)
6792     (gnus-configure-windows 'summary 'force)))
6793
6794 (defun gnus-summary-display-article (article &optional all-header)
6795   "Display ARTICLE in article buffer."
6796   (when (gnus-buffer-live-p gnus-article-buffer)
6797     (with-current-buffer gnus-article-buffer
6798       (mm-enable-multibyte)))
6799   (gnus-set-global-variables)
6800   (when (gnus-buffer-live-p gnus-article-buffer)
6801     (with-current-buffer gnus-article-buffer
6802       (setq gnus-article-charset gnus-newsgroup-charset)
6803       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6804       (mm-enable-multibyte)))
6805   (if (null article)
6806       nil
6807     (prog1
6808         (if gnus-summary-display-article-function
6809             (funcall gnus-summary-display-article-function article all-header)
6810           (gnus-article-prepare article all-header))
6811       (gnus-run-hooks 'gnus-select-article-hook)
6812       (when (and gnus-current-article
6813                  (not (zerop gnus-current-article)))
6814         (gnus-summary-goto-subject gnus-current-article))
6815       (gnus-summary-recenter)
6816       (when (and gnus-use-trees gnus-show-threads)
6817         (gnus-possibly-generate-tree article)
6818         (gnus-highlight-selected-tree article))
6819       ;; Successfully display article.
6820       (gnus-article-set-window-start
6821        (cdr (assq article gnus-newsgroup-bookmarks))))))
6822
6823 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6824   "Select the current article.
6825 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6826 non-nil, the article will be re-fetched even if it already present in
6827 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6828 be displayed."
6829   ;; Make sure we are in the summary buffer to work around bbdb bug.
6830   (unless (eq major-mode 'gnus-summary-mode)
6831     (set-buffer gnus-summary-buffer))
6832   (let ((article (or article (gnus-summary-article-number)))
6833         (all-headers (not (not all-headers))) ;Must be t or nil.
6834         gnus-summary-display-article-function)
6835     (and (not pseudo)
6836          (gnus-summary-article-pseudo-p article)
6837          (error "This is a pseudo-article"))
6838     (save-excursion
6839       (set-buffer gnus-summary-buffer)
6840       (if (or (and gnus-single-article-buffer
6841                    (or (null gnus-current-article)
6842                        (null gnus-article-current)
6843                        (null (get-buffer gnus-article-buffer))
6844                        (not (eq article (cdr gnus-article-current)))
6845                        (not (equal (car gnus-article-current)
6846                                    gnus-newsgroup-name))))
6847               (and (not gnus-single-article-buffer)
6848                    (or (null gnus-current-article)
6849                        (not (eq gnus-current-article article))))
6850               force)
6851           ;; The requested article is different from the current article.
6852           (progn
6853             (gnus-summary-display-article article all-headers)
6854             (when (gnus-buffer-live-p gnus-article-buffer)
6855               (with-current-buffer gnus-article-buffer
6856                 (if (not gnus-article-decoded-p) ;; a local variable
6857                     (mm-disable-multibyte))))
6858             (gnus-article-set-window-start
6859              (cdr (assq article gnus-newsgroup-bookmarks)))
6860             article)
6861         'old))))
6862
6863 (defun gnus-summary-force-verify-and-decrypt ()
6864   (interactive)
6865   (let ((mm-verify-option 'known)
6866         (mm-decrypt-option 'known)
6867         (gnus-buttonized-mime-types (append (list "multipart/signed"
6868                                                   "multipart/encrypted")
6869                                             gnus-buttonized-mime-types)))
6870     (gnus-summary-select-article nil 'force)))
6871
6872 (defun gnus-summary-set-current-mark (&optional current-mark)
6873   "Obsolete function."
6874   nil)
6875
6876 (defun gnus-summary-next-article (&optional unread subject backward push)
6877   "Select the next article.
6878 If UNREAD, only unread articles are selected.
6879 If SUBJECT, only articles with SUBJECT are selected.
6880 If BACKWARD, the previous article is selected instead of the next."
6881   (interactive "P")
6882   (cond
6883    ;; Is there such an article?
6884    ((and (gnus-summary-search-forward unread subject backward)
6885          (or (gnus-summary-display-article (gnus-summary-article-number))
6886              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6887     (gnus-summary-position-point))
6888    ;; If not, we try the first unread, if that is wanted.
6889    ((and subject
6890          gnus-auto-select-same
6891          (gnus-summary-first-unread-article))
6892     (gnus-summary-position-point)
6893     (gnus-message 6 "Wrapped"))
6894    ;; Try to get next/previous article not displayed in this group.
6895    ((and gnus-auto-extend-newsgroup
6896          (not unread) (not subject))
6897     (gnus-summary-goto-article
6898      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6899      nil (count-lines (point-min) (point))))
6900    ;; Go to next/previous group.
6901    (t
6902     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6903       (gnus-summary-jump-to-group gnus-newsgroup-name))
6904     (let ((cmd last-command-char)
6905           (point
6906            (save-excursion
6907              (set-buffer gnus-group-buffer)
6908              (point)))
6909           (group
6910            (if (eq gnus-keep-same-level 'best)
6911                (gnus-summary-best-group gnus-newsgroup-name)
6912              (gnus-summary-search-group backward gnus-keep-same-level))))
6913       ;; For some reason, the group window gets selected.  We change
6914       ;; it back.
6915       (select-window (get-buffer-window (current-buffer)))
6916       ;; Select next unread newsgroup automagically.
6917       (cond
6918        ((or (not gnus-auto-select-next)
6919             (not cmd))
6920         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6921        ((or (eq gnus-auto-select-next 'quietly)
6922             (and (eq gnus-auto-select-next 'slightly-quietly)
6923                  push)
6924             (and (eq gnus-auto-select-next 'almost-quietly)
6925                  (gnus-summary-last-article-p)))
6926         ;; Select quietly.
6927         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6928             (gnus-summary-exit)
6929           (gnus-message 7 "No more%s articles (%s)..."
6930                         (if unread " unread" "")
6931                         (if group (concat "selecting " group)
6932                           "exiting"))
6933           (gnus-summary-next-group nil group backward)))
6934        (t
6935         (when (gnus-key-press-event-p last-input-event)
6936           (gnus-summary-walk-group-buffer
6937            gnus-newsgroup-name cmd unread backward point))))))))
6938
6939 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6940   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6941                       (?\C-p (gnus-group-prev-unread-group 1))))
6942         (cursor-in-echo-area t)
6943         keve key group ended prompt)
6944     (save-excursion
6945       (set-buffer gnus-group-buffer)
6946       (goto-char start)
6947       (setq group
6948             (if (eq gnus-keep-same-level 'best)
6949                 (gnus-summary-best-group gnus-newsgroup-name)
6950               (gnus-summary-search-group backward gnus-keep-same-level))))
6951     (while (not ended)
6952       (setq prompt
6953             (format
6954              "No more%s articles%s " (if unread " unread" "")
6955              (if (and group
6956                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6957                  (format " (Type %s for %s [%s])"
6958                          (single-key-description cmd) group
6959                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6960                (format " (Type %s to exit %s)"
6961                        (single-key-description cmd)
6962                        gnus-newsgroup-name))))
6963       ;; Confirm auto selection.
6964       (setq key (car (setq keve (gnus-read-event-char prompt)))
6965             ended t)
6966       (cond
6967        ((assq key keystrokes)
6968         (let ((obuf (current-buffer)))
6969           (switch-to-buffer gnus-group-buffer)
6970           (when group
6971             (gnus-group-jump-to-group group))
6972           (eval (cadr (assq key keystrokes)))
6973           (setq group (gnus-group-group-name))
6974           (switch-to-buffer obuf))
6975         (setq ended nil))
6976        ((equal key cmd)
6977         (if (or (not group)
6978                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6979             (gnus-summary-exit)
6980           (gnus-summary-next-group nil group backward)))
6981        (t
6982         (push (cdr keve) unread-command-events))))))
6983
6984 (defun gnus-summary-next-unread-article ()
6985   "Select unread article after current one."
6986   (interactive)
6987   (gnus-summary-next-article
6988    (or (not (eq gnus-summary-goto-unread 'never))
6989        (gnus-summary-last-article-p (gnus-summary-article-number)))
6990    (and gnus-auto-select-same
6991         (gnus-summary-article-subject))))
6992
6993 (defun gnus-summary-prev-article (&optional unread subject)
6994   "Select the article after the current one.
6995 If UNREAD is non-nil, only unread articles are selected."
6996   (interactive "P")
6997   (gnus-summary-next-article unread subject t))
6998
6999 (defun gnus-summary-prev-unread-article ()
7000   "Select unread article before current one."
7001   (interactive)
7002   (gnus-summary-prev-article
7003    (or (not (eq gnus-summary-goto-unread 'never))
7004        (gnus-summary-first-article-p (gnus-summary-article-number)))
7005    (and gnus-auto-select-same
7006         (gnus-summary-article-subject))))
7007
7008 (defun gnus-summary-next-page (&optional lines circular stop)
7009   "Show next page of the selected article.
7010 If at the end of the current article, select the next article.
7011 LINES says how many lines should be scrolled up.
7012
7013 If CIRCULAR is non-nil, go to the start of the article instead of
7014 selecting the next article when reaching the end of the current
7015 article.
7016
7017 If STOP is non-nil, just stop when reaching the end of the message."
7018   (interactive "P")
7019   (setq gnus-summary-buffer (current-buffer))
7020   (gnus-set-global-variables)
7021   (let ((article (gnus-summary-article-number))
7022         (article-window (get-buffer-window gnus-article-buffer t))
7023         endp)
7024     ;; If the buffer is empty, we have no article.
7025     (unless article
7026       (error "No article to select"))
7027     (gnus-configure-windows 'article)
7028     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7029         (if (and (eq gnus-summary-goto-unread 'never)
7030                  (not (gnus-summary-last-article-p article)))
7031             (gnus-summary-next-article)
7032           (gnus-summary-next-unread-article))
7033       (if (or (null gnus-current-article)
7034               (null gnus-article-current)
7035               (/= article (cdr gnus-article-current))
7036               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7037           ;; Selected subject is different from current article's.
7038           (gnus-summary-display-article article)
7039         (when article-window
7040           (gnus-eval-in-buffer-window gnus-article-buffer
7041             (setq endp (gnus-article-next-page lines)))
7042           (when endp
7043             (cond (stop
7044                    (gnus-message 3 "End of message"))
7045                   (circular
7046                    (gnus-summary-beginning-of-article))
7047                   (lines
7048                    (gnus-message 3 "End of message"))
7049                   ((null lines)
7050                    (if (and (eq gnus-summary-goto-unread 'never)
7051                             (not (gnus-summary-last-article-p article)))
7052                        (gnus-summary-next-article)
7053                      (gnus-summary-next-unread-article))))))))
7054     (gnus-summary-recenter)
7055     (gnus-summary-position-point)))
7056
7057 (defun gnus-summary-prev-page (&optional lines move)
7058   "Show previous page of selected article.
7059 Argument LINES specifies lines to be scrolled down.
7060 If MOVE, move to the previous unread article if point is at
7061 the beginning of the buffer."
7062   (interactive "P")
7063   (let ((article (gnus-summary-article-number))
7064         (article-window (get-buffer-window gnus-article-buffer t))
7065         endp)
7066     (gnus-configure-windows 'article)
7067     (if (or (null gnus-current-article)
7068             (null gnus-article-current)
7069             (/= article (cdr gnus-article-current))
7070             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7071         ;; Selected subject is different from current article's.
7072         (gnus-summary-display-article article)
7073       (gnus-summary-recenter)
7074       (when article-window
7075         (gnus-eval-in-buffer-window gnus-article-buffer
7076           (setq endp (gnus-article-prev-page lines)))
7077         (when (and move endp)
7078           (cond (lines
7079                  (gnus-message 3 "Beginning of message"))
7080                 ((null lines)
7081                  (if (and (eq gnus-summary-goto-unread 'never)
7082                           (not (gnus-summary-first-article-p article)))
7083                      (gnus-summary-prev-article)
7084                    (gnus-summary-prev-unread-article))))))))
7085   (gnus-summary-position-point))
7086
7087 (defun gnus-summary-prev-page-or-article (&optional lines)
7088   "Show previous page of selected article.
7089 Argument LINES specifies lines to be scrolled down.
7090 If at the beginning of the article, go to the next article."
7091   (interactive "P")
7092   (gnus-summary-prev-page lines t))
7093
7094 (defun gnus-summary-scroll-up (lines)
7095   "Scroll up (or down) one line current article.
7096 Argument LINES specifies lines to be scrolled up (or down if negative)."
7097   (interactive "p")
7098   (gnus-configure-windows 'article)
7099   (gnus-summary-show-thread)
7100   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7101     (gnus-eval-in-buffer-window gnus-article-buffer
7102       (cond ((> lines 0)
7103              (when (gnus-article-next-page lines)
7104                (gnus-message 3 "End of message")))
7105             ((< lines 0)
7106              (gnus-article-prev-page (- lines))))))
7107   (gnus-summary-recenter)
7108   (gnus-summary-position-point))
7109
7110 (defun gnus-summary-scroll-down (lines)
7111   "Scroll down (or up) one line current article.
7112 Argument LINES specifies lines to be scrolled down (or up if negative)."
7113   (interactive "p")
7114   (gnus-summary-scroll-up (- lines)))
7115
7116 (defun gnus-summary-next-same-subject ()
7117   "Select next article which has the same subject as current one."
7118   (interactive)
7119   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7120
7121 (defun gnus-summary-prev-same-subject ()
7122   "Select previous article which has the same subject as current one."
7123   (interactive)
7124   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7125
7126 (defun gnus-summary-next-unread-same-subject ()
7127   "Select next unread article which has the same subject as current one."
7128   (interactive)
7129   (gnus-summary-next-article t (gnus-summary-article-subject)))
7130
7131 (defun gnus-summary-prev-unread-same-subject ()
7132   "Select previous unread article which has the same subject as current one."
7133   (interactive)
7134   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7135
7136 (defun gnus-summary-first-unread-article ()
7137   "Select the first unread article.
7138 Return nil if there are no unread articles."
7139   (interactive)
7140   (prog1
7141       (when (gnus-summary-first-subject t)
7142         (gnus-summary-show-thread)
7143         (gnus-summary-first-subject t)
7144         (gnus-summary-display-article (gnus-summary-article-number)))
7145     (gnus-summary-position-point)))
7146
7147 (defun gnus-summary-first-unread-subject ()
7148   "Place the point on the subject line of the first unread article.
7149 Return nil if there are no unread articles."
7150   (interactive)
7151   (prog1
7152       (when (gnus-summary-first-subject t)
7153         (gnus-summary-show-thread)
7154         (gnus-summary-first-subject t))
7155     (gnus-summary-position-point)))
7156
7157 (defun gnus-summary-first-unseen-subject ()
7158   "Place the point on the subject line of the first unseen article.
7159 Return nil if there are no unseen articles."
7160   (interactive)
7161   (prog1
7162       (when (gnus-summary-first-subject t t t)
7163         (gnus-summary-show-thread)
7164         (gnus-summary-first-subject t t t))
7165     (gnus-summary-position-point)))
7166
7167 (defun gnus-summary-first-unseen-or-unread-subject ()
7168   "Place the point on the subject line of the first unseen article.
7169 Return nil if there are no unseen articles."
7170   (interactive)
7171   (prog1
7172       (unless (when (gnus-summary-first-subject t t t)
7173                 (gnus-summary-show-thread)
7174                 (gnus-summary-first-subject t t t))
7175         (when (gnus-summary-first-subject t)
7176           (gnus-summary-show-thread)
7177           (gnus-summary-first-subject t)))
7178     (gnus-summary-position-point)))
7179
7180 (defun gnus-summary-first-article ()
7181   "Select the first article.
7182 Return nil if there are no articles."
7183   (interactive)
7184   (prog1
7185       (when (gnus-summary-first-subject)
7186         (gnus-summary-show-thread)
7187         (gnus-summary-first-subject)
7188         (gnus-summary-display-article (gnus-summary-article-number)))
7189     (gnus-summary-position-point)))
7190
7191 (defun gnus-summary-best-unread-article (&optional arg)
7192   "Select the unread article with the highest score.
7193 If given a prefix argument, select the next unread article that has a
7194 score higher than the default score."
7195   (interactive "P")
7196   (let ((article (if arg
7197                      (gnus-summary-better-unread-subject)
7198                    (gnus-summary-best-unread-subject))))
7199     (if article
7200         (gnus-summary-goto-article article)
7201       (error "No unread articles"))))
7202
7203 (defun gnus-summary-best-unread-subject ()
7204   "Select the unread subject with the highest score."
7205   (interactive)
7206   (let ((best -1000000)
7207         (data gnus-newsgroup-data)
7208         article score)
7209     (while data
7210       (and (gnus-data-unread-p (car data))
7211            (> (setq score
7212                     (gnus-summary-article-score (gnus-data-number (car data))))
7213               best)
7214            (setq best score
7215                  article (gnus-data-number (car data))))
7216       (setq data (cdr data)))
7217     (when article
7218       (gnus-summary-goto-subject article))
7219     (gnus-summary-position-point)
7220     article))
7221
7222 (defun gnus-summary-better-unread-subject ()
7223   "Select the first unread subject that has a score over the default score."
7224   (interactive)
7225   (let ((data gnus-newsgroup-data)
7226         article score)
7227     (while (and (setq article (gnus-data-number (car data)))
7228                 (or (gnus-data-read-p (car data))
7229                     (not (> (gnus-summary-article-score article)
7230                             gnus-summary-default-score))))
7231       (setq data (cdr data)))
7232     (when article
7233       (gnus-summary-goto-subject article))
7234     (gnus-summary-position-point)
7235     article))
7236
7237 (defun gnus-summary-last-subject ()
7238   "Go to the last displayed subject line in the group."
7239   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7240     (when article
7241       (gnus-summary-goto-subject article))))
7242
7243 (defun gnus-summary-goto-article (article &optional all-headers force)
7244   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7245 If ALL-HEADERS is non-nil, no header lines are hidden.
7246 If FORCE, go to the article even if it isn't displayed.  If FORCE
7247 is a number, it is the line the article is to be displayed on."
7248   (interactive
7249    (list
7250     (completing-read
7251      "Article number or Message-ID: "
7252      (mapcar (lambda (number) (list (int-to-string number)))
7253              gnus-newsgroup-limit))
7254     current-prefix-arg
7255     t))
7256   (prog1
7257       (if (and (stringp article)
7258                (string-match "@" article))
7259           (gnus-summary-refer-article article)
7260         (when (stringp article)
7261           (setq article (string-to-number article)))
7262         (if (gnus-summary-goto-subject article force)
7263             (gnus-summary-display-article article all-headers)
7264           (gnus-message 4 "Couldn't go to article %s" article) nil))
7265     (gnus-summary-position-point)))
7266
7267 (defun gnus-summary-goto-last-article ()
7268   "Go to the previously read article."
7269   (interactive)
7270   (prog1
7271       (when gnus-last-article
7272         (gnus-summary-goto-article gnus-last-article nil t))
7273     (gnus-summary-position-point)))
7274
7275 (defun gnus-summary-pop-article (number)
7276   "Pop one article off the history and go to the previous.
7277 NUMBER articles will be popped off."
7278   (interactive "p")
7279   (let (to)
7280     (setq gnus-newsgroup-history
7281           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7282     (if to
7283         (gnus-summary-goto-article (car to) nil t)
7284       (error "Article history empty")))
7285   (gnus-summary-position-point))
7286
7287 ;; Summary commands and functions for limiting the summary buffer.
7288
7289 (defun gnus-summary-limit-to-articles (n)
7290   "Limit the summary buffer to the next N articles.
7291 If not given a prefix, use the process marked articles instead."
7292   (interactive "P")
7293   (prog1
7294       (let ((articles (gnus-summary-work-articles n)))
7295         (setq gnus-newsgroup-processable nil)
7296         (gnus-summary-limit articles))
7297     (gnus-summary-position-point)))
7298
7299 (defun gnus-summary-pop-limit (&optional total)
7300   "Restore the previous limit.
7301 If given a prefix, remove all limits."
7302   (interactive "P")
7303   (when total
7304     (setq gnus-newsgroup-limits
7305           (list (mapcar (lambda (h) (mail-header-number h))
7306                         gnus-newsgroup-headers))))
7307   (unless gnus-newsgroup-limits
7308     (error "No limit to pop"))
7309   (prog1
7310       (gnus-summary-limit nil 'pop)
7311     (gnus-summary-position-point)))
7312
7313 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7314   "Limit the summary buffer to articles that have subjects that match a regexp.
7315 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7316   (interactive
7317    (list (read-string (if current-prefix-arg
7318                           "Exclude subject (regexp): "
7319                         "Limit to subject (regexp): "))
7320          nil current-prefix-arg))
7321   (unless header
7322     (setq header "subject"))
7323   (when (not (equal "" subject))
7324     (prog1
7325         (let ((articles (gnus-summary-find-matching
7326                          (or header "subject") subject 'all nil nil
7327                          not-matching)))
7328           (unless articles
7329             (error "Found no matches for \"%s\"" subject))
7330           (gnus-summary-limit articles))
7331       (gnus-summary-position-point))))
7332
7333 (defun gnus-summary-limit-to-author (from &optional not-matching)
7334   "Limit the summary buffer to articles that have authors that match a regexp.
7335 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7336   (interactive
7337    (list (read-string (if current-prefix-arg
7338                           "Exclude author (regexp): "
7339                         "Limit to author (regexp): "))
7340          current-prefix-arg))
7341   (gnus-summary-limit-to-subject from "from" not-matching))
7342
7343 (defun gnus-summary-limit-to-age (age &optional younger-p)
7344   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7345 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7346 articles that are younger than AGE days."
7347   (interactive
7348    (let ((younger current-prefix-arg)
7349          (days-got nil)
7350          days)
7351      (while (not days-got)
7352        (setq days (if younger
7353                       (read-string "Limit to articles younger than (in days, older when negative): ")
7354                     (read-string
7355                      "Limit to articles older than (in days, younger when negative): ")))
7356        (when (> (length days) 0)
7357          (setq days (read days)))
7358        (if (numberp days)
7359            (progn
7360              (setq days-got t)
7361              (if (< days 0)
7362                  (progn
7363                    (setq younger (not younger))
7364                    (setq days (* days -1)))))
7365          (message "Please enter a number.")
7366          (sleep-for 1)))
7367      (list days younger)))
7368   (prog1
7369       (let ((data gnus-newsgroup-data)
7370             (cutoff (days-to-time age))
7371             articles d date is-younger)
7372         (while (setq d (pop data))
7373           (when (and (vectorp (gnus-data-header d))
7374                      (setq date (mail-header-date (gnus-data-header d))))
7375             (setq is-younger (time-less-p
7376                               (time-since (condition-case ()
7377                                               (date-to-time date)
7378                                             (error '(0 0))))
7379                               cutoff))
7380             (when (if younger-p
7381                       is-younger
7382                     (not is-younger))
7383               (push (gnus-data-number d) articles))))
7384         (gnus-summary-limit (nreverse articles)))
7385     (gnus-summary-position-point)))
7386
7387 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7388   "Limit the summary buffer to articles that match an 'extra' header."
7389   (interactive
7390    (let ((header
7391           (intern
7392            (gnus-completing-read-with-default
7393             (symbol-name (car gnus-extra-headers))
7394             (if current-prefix-arg
7395                 "Exclude extra header:"
7396               "Limit extra header:")
7397             (mapcar (lambda (x)
7398                       (cons (symbol-name x) x))
7399                     gnus-extra-headers)
7400             nil
7401             t))))
7402      (list header
7403            (read-string (format "%s header %s (regexp): "
7404                                 (if current-prefix-arg "Exclude" "Limit to")
7405                                 header))
7406            current-prefix-arg)))
7407   (when (not (equal "" regexp))
7408     (prog1
7409         (let ((articles (gnus-summary-find-matching
7410                          (cons 'extra header) regexp 'all nil nil
7411                          not-matching)))
7412           (unless articles
7413             (error "Found no matches for \"%s\"" regexp))
7414           (gnus-summary-limit articles))
7415       (gnus-summary-position-point))))
7416
7417 (defun gnus-summary-limit-to-display-predicate ()
7418   "Limit the summary buffer to the predicated in the `display' group parameter."
7419   (interactive)
7420   (unless gnus-newsgroup-display
7421     (error "There is no `display' group parameter"))
7422   (let (articles)
7423     (dolist (number gnus-newsgroup-articles)
7424       (when (funcall gnus-newsgroup-display)
7425         (push number articles)))
7426     (gnus-summary-limit articles))
7427   (gnus-summary-position-point))
7428
7429 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7430 (make-obsolete
7431  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7432
7433 (defun gnus-summary-limit-to-unread (&optional all)
7434   "Limit the summary buffer to articles that are not marked as read.
7435 If ALL is non-nil, limit strictly to unread articles."
7436   (interactive "P")
7437   (if all
7438       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7439     (gnus-summary-limit-to-marks
7440      ;; Concat all the marks that say that an article is read and have
7441      ;; those removed.
7442      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7443            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7444            gnus-low-score-mark gnus-expirable-mark
7445            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7446            gnus-duplicate-mark gnus-souped-mark)
7447      'reverse)))
7448
7449 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7450 (make-obsolete 'gnus-summary-delete-marked-with
7451                'gnus-summary-limit-exclude-marks)
7452
7453 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7454   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7455 If REVERSE, limit the summary buffer to articles that are marked
7456 with MARKS.  MARKS can either be a string of marks or a list of marks.
7457 Returns how many articles were removed."
7458   (interactive "sMarks: ")
7459   (gnus-summary-limit-to-marks marks t))
7460
7461 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7462   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7463 If REVERSE (the prefix), limit the summary buffer to articles that are
7464 not marked with MARKS.  MARKS can either be a string of marks or a
7465 list of marks.
7466 Returns how many articles were removed."
7467   (interactive "sMarks: \nP")
7468   (prog1
7469       (let ((data gnus-newsgroup-data)
7470             (marks (if (listp marks) marks
7471                      (append marks nil))) ; Transform to list.
7472             articles)
7473         (while data
7474           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7475                   (memq (gnus-data-mark (car data)) marks))
7476             (push (gnus-data-number (car data)) articles))
7477           (setq data (cdr data)))
7478         (gnus-summary-limit articles))
7479     (gnus-summary-position-point)))
7480
7481 (defun gnus-summary-limit-to-score (score)
7482   "Limit to articles with score at or above SCORE."
7483   (interactive "NLimit to articles with score of at least: ")
7484   (let ((data gnus-newsgroup-data)
7485         articles)
7486     (while data
7487       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7488                 score)
7489         (push (gnus-data-number (car data)) articles))
7490       (setq data (cdr data)))
7491     (prog1
7492         (gnus-summary-limit articles)
7493       (gnus-summary-position-point))))
7494
7495 (defun gnus-summary-limit-to-unseen ()
7496   "Limit to unseen articles."
7497   (interactive)
7498   (prog1
7499       (gnus-summary-limit gnus-newsgroup-unseen)
7500     (gnus-summary-position-point)))
7501
7502 (defun gnus-summary-limit-include-thread (id)
7503   "Display all the hidden articles that is in the thread with ID in it.
7504 When called interactively, ID is the Message-ID of the current
7505 article."
7506   (interactive (list (mail-header-id (gnus-summary-article-header))))
7507   (let ((articles (gnus-articles-in-thread
7508                    (gnus-id-to-thread (gnus-root-id id)))))
7509     (prog1
7510         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7511       (gnus-summary-limit-include-matching-articles
7512        "subject"
7513        (regexp-quote (gnus-simplify-subject-re
7514                       (mail-header-subject (gnus-id-to-header id)))))
7515       (gnus-summary-position-point))))
7516
7517 (defun gnus-summary-limit-include-matching-articles (header regexp)
7518   "Display all the hidden articles that have HEADERs that match REGEXP."
7519   (interactive (list (read-string "Match on header: ")
7520                      (read-string "Regexp: ")))
7521   (let ((articles (gnus-find-matching-articles header regexp)))
7522     (prog1
7523         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7524       (gnus-summary-position-point))))
7525
7526 (defun gnus-summary-insert-dormant-articles ()
7527   "Insert all the dormat articles for this group into the current buffer."
7528   (interactive)
7529   (let ((gnus-verbose (max 6 gnus-verbose)))
7530     (if (not gnus-newsgroup-dormant)
7531         (gnus-message 3 "No cached articles for this group")
7532       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7533
7534 (defun gnus-summary-limit-include-dormant ()
7535   "Display all the hidden articles that are marked as dormant.
7536 Note that this command only works on a subset of the articles currently
7537 fetched for this group."
7538   (interactive)
7539   (unless gnus-newsgroup-dormant
7540     (error "There are no dormant articles in this group"))
7541   (prog1
7542       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7543     (gnus-summary-position-point)))
7544
7545 (defun gnus-summary-limit-exclude-dormant ()
7546   "Hide all dormant articles."
7547   (interactive)
7548   (prog1
7549       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7550     (gnus-summary-position-point)))
7551
7552 (defun gnus-summary-limit-exclude-childless-dormant ()
7553   "Hide all dormant articles that have no children."
7554   (interactive)
7555   (let ((data (gnus-data-list t))
7556         articles d children)
7557     ;; Find all articles that are either not dormant or have
7558     ;; children.
7559     (while (setq d (pop data))
7560       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7561                 (and (setq children
7562                            (gnus-article-children (gnus-data-number d)))
7563                      (let (found)
7564                        (while children
7565                          (when (memq (car children) articles)
7566                            (setq children nil
7567                                  found t))
7568                          (pop children))
7569                        found)))
7570         (push (gnus-data-number d) articles)))
7571     ;; Do the limiting.
7572     (prog1
7573         (gnus-summary-limit articles)
7574       (gnus-summary-position-point))))
7575
7576 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7577   "Mark all unread excluded articles as read.
7578 If ALL, mark even excluded ticked and dormants as read."
7579   (interactive "P")
7580   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7581   (let ((articles (gnus-sorted-ndifference
7582                    (sort
7583                     (mapcar (lambda (h) (mail-header-number h))
7584                             gnus-newsgroup-headers)
7585                     '<)
7586                    gnus-newsgroup-limit))
7587         article)
7588     (setq gnus-newsgroup-unreads
7589           (gnus-sorted-intersection gnus-newsgroup-unreads
7590                                     gnus-newsgroup-limit))
7591     (if all
7592         (setq gnus-newsgroup-dormant nil
7593               gnus-newsgroup-marked nil
7594               gnus-newsgroup-reads
7595               (nconc
7596                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7597                gnus-newsgroup-reads))
7598       (while (setq article (pop articles))
7599         (unless (or (memq article gnus-newsgroup-dormant)
7600                     (memq article gnus-newsgroup-marked))
7601           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7602
7603 (defun gnus-summary-limit (articles &optional pop)
7604   (if pop
7605       ;; We pop the previous limit off the stack and use that.
7606       (setq articles (car gnus-newsgroup-limits)
7607             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7608     ;; We use the new limit, so we push the old limit on the stack.
7609     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7610   ;; Set the limit.
7611   (setq gnus-newsgroup-limit articles)
7612   (let ((total (length gnus-newsgroup-data))
7613         (data (gnus-data-find-list (gnus-summary-article-number)))
7614         (gnus-summary-mark-below nil)   ; Inhibit this.
7615         found)
7616     ;; This will do all the work of generating the new summary buffer
7617     ;; according to the new limit.
7618     (gnus-summary-prepare)
7619     ;; Hide any threads, possibly.
7620     (gnus-summary-maybe-hide-threads)
7621     ;; Try to return to the article you were at, or one in the
7622     ;; neighborhood.
7623     (when data
7624       ;; We try to find some article after the current one.
7625       (while data
7626         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7627           (setq data nil
7628                 found t))
7629         (setq data (cdr data))))
7630     (unless found
7631       ;; If there is no data, that means that we were after the last
7632       ;; article.  The same goes when we can't find any articles
7633       ;; after the current one.
7634       (goto-char (point-max))
7635       (gnus-summary-find-prev))
7636     (gnus-set-mode-line 'summary)
7637     ;; We return how many articles were removed from the summary
7638     ;; buffer as a result of the new limit.
7639     (- total (length gnus-newsgroup-data))))
7640
7641 (defsubst gnus-invisible-cut-children (threads)
7642   (let ((num 0))
7643     (while threads
7644       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7645         (incf num))
7646       (pop threads))
7647     (< num 2)))
7648
7649 (defsubst gnus-cut-thread (thread)
7650   "Go forwards in the thread until we find an article that we want to display."
7651   (when (or (eq gnus-fetch-old-headers 'some)
7652             (eq gnus-fetch-old-headers 'invisible)
7653             (numberp gnus-fetch-old-headers)
7654             (eq gnus-build-sparse-threads 'some)
7655             (eq gnus-build-sparse-threads 'more))
7656     ;; Deal with old-fetched headers and sparse threads.
7657     (while (and
7658             thread
7659             (or
7660              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7661              (gnus-summary-article-ancient-p
7662               (mail-header-number (car thread))))
7663             (if (or (<= (length (cdr thread)) 1)
7664                     (eq gnus-fetch-old-headers 'invisible))
7665                 (setq gnus-newsgroup-limit
7666                       (delq (mail-header-number (car thread))
7667                             gnus-newsgroup-limit)
7668                       thread (cadr thread))
7669               (when (gnus-invisible-cut-children (cdr thread))
7670                 (let ((th (cdr thread)))
7671                   (while th
7672                     (if (memq (mail-header-number (caar th))
7673                               gnus-newsgroup-limit)
7674                         (setq thread (car th)
7675                               th nil)
7676                       (setq th (cdr th))))))))))
7677   thread)
7678
7679 (defun gnus-cut-threads (threads)
7680   "Cut off all uninteresting articles from the beginning of threads."
7681   (when (or (eq gnus-fetch-old-headers 'some)
7682             (eq gnus-fetch-old-headers 'invisible)
7683             (numberp gnus-fetch-old-headers)
7684             (eq gnus-build-sparse-threads 'some)
7685             (eq gnus-build-sparse-threads 'more))
7686     (let ((th threads))
7687       (while th
7688         (setcar th (gnus-cut-thread (car th)))
7689         (setq th (cdr th)))))
7690   ;; Remove nixed out threads.
7691   (delq nil threads))
7692
7693 (defun gnus-summary-initial-limit (&optional show-if-empty)
7694   "Figure out what the initial limit is supposed to be on group entry.
7695 This entails weeding out unwanted dormants, low-scored articles,
7696 fetch-old-headers verbiage, and so on."
7697   ;; Most groups have nothing to remove.
7698   (if (or gnus-inhibit-limiting
7699           (and (null gnus-newsgroup-dormant)
7700                (eq gnus-newsgroup-display 'gnus-not-ignore)
7701                (not (eq gnus-fetch-old-headers 'some))
7702                (not (numberp gnus-fetch-old-headers))
7703                (not (eq gnus-fetch-old-headers 'invisible))
7704                (null gnus-summary-expunge-below)
7705                (not (eq gnus-build-sparse-threads 'some))
7706                (not (eq gnus-build-sparse-threads 'more))
7707                (null gnus-thread-expunge-below)
7708                (not gnus-use-nocem)))
7709       ()                                ; Do nothing.
7710     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7711     (setq gnus-newsgroup-limit nil)
7712     (mapatoms
7713      (lambda (node)
7714        (unless (car (symbol-value node))
7715          ;; These threads have no parents -- they are roots.
7716          (let ((nodes (cdr (symbol-value node)))
7717                thread)
7718            (while nodes
7719              (if (and gnus-thread-expunge-below
7720                       (< (gnus-thread-total-score (car nodes))
7721                          gnus-thread-expunge-below))
7722                  (gnus-expunge-thread (pop nodes))
7723                (setq thread (pop nodes))
7724                (gnus-summary-limit-children thread))))))
7725      gnus-newsgroup-dependencies)
7726     ;; If this limitation resulted in an empty group, we might
7727     ;; pop the previous limit and use it instead.
7728     (when (and (not gnus-newsgroup-limit)
7729                show-if-empty)
7730       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7731     gnus-newsgroup-limit))
7732
7733 (defun gnus-summary-limit-children (thread)
7734   "Return 1 if this subthread is visible and 0 if it is not."
7735   ;; First we get the number of visible children to this thread.  This
7736   ;; is done by recursing down the thread using this function, so this
7737   ;; will really go down to a leaf article first, before slowly
7738   ;; working its way up towards the root.
7739   (when thread
7740     (let* ((max-lisp-eval-depth 5000)
7741            (children
7742            (if (cdr thread)
7743                (apply '+ (mapcar 'gnus-summary-limit-children
7744                                  (cdr thread)))
7745              0))
7746           (number (mail-header-number (car thread)))
7747           score)
7748       (if (and
7749            (not (memq number gnus-newsgroup-marked))
7750            (or
7751             ;; If this article is dormant and has absolutely no visible
7752             ;; children, then this article isn't visible.
7753             (and (memq number gnus-newsgroup-dormant)
7754                  (zerop children))
7755             ;; If this is "fetch-old-headered" and there is no
7756             ;; visible children, then we don't want this article.
7757             (and (or (eq gnus-fetch-old-headers 'some)
7758                      (numberp gnus-fetch-old-headers))
7759                  (gnus-summary-article-ancient-p number)
7760                  (zerop children))
7761             ;; If this is "fetch-old-headered" and `invisible', then
7762             ;; we don't want this article.
7763             (and (eq gnus-fetch-old-headers 'invisible)
7764                  (gnus-summary-article-ancient-p number))
7765             ;; If this is a sparsely inserted article with no children,
7766             ;; we don't want it.
7767             (and (eq gnus-build-sparse-threads 'some)
7768                  (gnus-summary-article-sparse-p number)
7769                  (zerop children))
7770             ;; If we use expunging, and this article is really
7771             ;; low-scored, then we don't want this article.
7772             (when (and gnus-summary-expunge-below
7773                        (< (setq score
7774                                 (or (cdr (assq number gnus-newsgroup-scored))
7775                                     gnus-summary-default-score))
7776                           gnus-summary-expunge-below))
7777               ;; We increase the expunge-tally here, but that has
7778               ;; nothing to do with the limits, really.
7779               (incf gnus-newsgroup-expunged-tally)
7780               ;; We also mark as read here, if that's wanted.
7781               (when (and gnus-summary-mark-below
7782                          (< score gnus-summary-mark-below))
7783                 (setq gnus-newsgroup-unreads
7784                       (delq number gnus-newsgroup-unreads))
7785                 (if gnus-newsgroup-auto-expire
7786                     (push number gnus-newsgroup-expirable)
7787                   (push (cons number gnus-low-score-mark)
7788                         gnus-newsgroup-reads)))
7789               t)
7790             ;; Do the `display' group parameter.
7791             (and gnus-newsgroup-display
7792                  (not (funcall gnus-newsgroup-display)))
7793             ;; Check NoCeM things.
7794             (if (and gnus-use-nocem
7795                      (gnus-nocem-unwanted-article-p
7796                       (mail-header-id (car thread))))
7797                 (progn
7798                   (setq gnus-newsgroup-unreads
7799                         (delq number gnus-newsgroup-unreads))
7800                   t))))
7801           ;; Nope, invisible article.
7802           0
7803         ;; Ok, this article is to be visible, so we add it to the limit
7804         ;; and return 1.
7805         (push number gnus-newsgroup-limit)
7806         1))))
7807
7808 (defun gnus-expunge-thread (thread)
7809   "Mark all articles in THREAD as read."
7810   (let* ((number (mail-header-number (car thread))))
7811     (incf gnus-newsgroup-expunged-tally)
7812     ;; We also mark as read here, if that's wanted.
7813     (setq gnus-newsgroup-unreads
7814           (delq number gnus-newsgroup-unreads))
7815     (if gnus-newsgroup-auto-expire
7816         (push number gnus-newsgroup-expirable)
7817       (push (cons number gnus-low-score-mark)
7818             gnus-newsgroup-reads)))
7819   ;; Go recursively through all subthreads.
7820   (mapcar 'gnus-expunge-thread (cdr thread)))
7821
7822 ;; Summary article oriented commands
7823
7824 (defun gnus-summary-refer-parent-article (n)
7825   "Refer parent article N times.
7826 If N is negative, go to ancestor -N instead.
7827 The difference between N and the number of articles fetched is returned."
7828   (interactive "p")
7829   (let ((skip 1)
7830         error header ref)
7831     (when (not (natnump n))
7832       (setq skip (abs n)
7833             n 1))
7834     (while (and (> n 0)
7835                 (not error))
7836       (setq header (gnus-summary-article-header))
7837       (if (and (eq (mail-header-number header)
7838                    (cdr gnus-article-current))
7839                (equal gnus-newsgroup-name
7840                       (car gnus-article-current)))
7841           ;; If we try to find the parent of the currently
7842           ;; displayed article, then we take a look at the actual
7843           ;; References header, since this is slightly more
7844           ;; reliable than the References field we got from the
7845           ;; server.
7846           (save-excursion
7847             (set-buffer gnus-original-article-buffer)
7848             (nnheader-narrow-to-headers)
7849             (unless (setq ref (message-fetch-field "references"))
7850               (setq ref (message-fetch-field "in-reply-to")))
7851             (widen))
7852         (setq ref
7853               ;; It's not the current article, so we take a bet on
7854               ;; the value we got from the server.
7855               (mail-header-references header)))
7856       (if (and ref
7857                (not (equal ref "")))
7858           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7859             (gnus-message 1 "Couldn't find parent"))
7860         (gnus-message 1 "No references in article %d"
7861                       (gnus-summary-article-number))
7862         (setq error t))
7863       (decf n))
7864     (gnus-summary-position-point)
7865     n))
7866
7867 (defun gnus-summary-refer-references ()
7868   "Fetch all articles mentioned in the References header.
7869 Return the number of articles fetched."
7870   (interactive)
7871   (let ((ref (mail-header-references (gnus-summary-article-header)))
7872         (current (gnus-summary-article-number))
7873         (n 0))
7874     (if (or (not ref)
7875             (equal ref ""))
7876         (error "No References in the current article")
7877       ;; For each Message-ID in the References header...
7878       (while (string-match "<[^>]*>" ref)
7879         (incf n)
7880         ;; ... fetch that article.
7881         (gnus-summary-refer-article
7882          (prog1 (match-string 0 ref)
7883            (setq ref (substring ref (match-end 0))))))
7884       (gnus-summary-goto-subject current)
7885       (gnus-summary-position-point)
7886       n)))
7887
7888 (defun gnus-summary-refer-thread (&optional limit)
7889   "Fetch all articles in the current thread.
7890 If LIMIT (the numerical prefix), fetch that many old headers instead
7891 of what's specified by the `gnus-refer-thread-limit' variable."
7892   (interactive "P")
7893   (let ((id (mail-header-id (gnus-summary-article-header)))
7894         (limit (if limit (prefix-numeric-value limit)
7895                  gnus-refer-thread-limit)))
7896     ;; We want to fetch LIMIT *old* headers, but we also have to
7897     ;; re-fetch all the headers in the current buffer, because many of
7898     ;; them may be undisplayed.  So we adjust LIMIT.
7899     (when (numberp limit)
7900       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7901     (unless (eq gnus-fetch-old-headers 'invisible)
7902       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7903       ;; Retrieve the headers and read them in.
7904       (if (eq (gnus-retrieve-headers
7905                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7906               'nov)
7907           (gnus-build-all-threads)
7908         (error "Can't fetch thread from backends that don't support NOV"))
7909       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7910     (gnus-summary-limit-include-thread id)))
7911
7912 (defun gnus-summary-refer-article (message-id)
7913   "Fetch an article specified by MESSAGE-ID."
7914   (interactive "sMessage-ID: ")
7915   (when (and (stringp message-id)
7916              (not (zerop (length message-id))))
7917     ;; Construct the correct Message-ID if necessary.
7918     ;; Suggested by tale@pawl.rpi.edu.
7919     (unless (string-match "^<" message-id)
7920       (setq message-id (concat "<" message-id)))
7921     (unless (string-match ">$" message-id)
7922       (setq message-id (concat message-id ">")))
7923     (let* ((header (gnus-id-to-header message-id))
7924            (sparse (and header
7925                         (gnus-summary-article-sparse-p
7926                          (mail-header-number header))
7927                         (memq (mail-header-number header)
7928                               gnus-newsgroup-limit)))
7929            number)
7930       (cond
7931        ;; If the article is present in the buffer we just go to it.
7932        ((and header
7933              (or (not (gnus-summary-article-sparse-p
7934                        (mail-header-number header)))
7935                  sparse))
7936         (prog1
7937             (gnus-summary-goto-article
7938              (mail-header-number header) nil t)
7939           (when sparse
7940             (gnus-summary-update-article (mail-header-number header)))))
7941        (t
7942         ;; We fetch the article.
7943         (catch 'found
7944           (dolist (gnus-override-method (gnus-refer-article-methods))
7945             (gnus-check-server gnus-override-method)
7946             ;; Fetch the header, and display the article.
7947             (when (setq number (gnus-summary-insert-subject message-id))
7948               (gnus-summary-select-article nil nil nil number)
7949               (throw 'found t)))
7950           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7951
7952 (defun gnus-refer-article-methods ()
7953   "Return a list of referable methods."
7954   (cond
7955    ;; No method, so we default to current and native.
7956    ((null gnus-refer-article-method)
7957     (list gnus-current-select-method gnus-select-method))
7958    ;; Current.
7959    ((eq 'current gnus-refer-article-method)
7960     (list gnus-current-select-method))
7961    ;; List of select methods.
7962    ((not (and (symbolp (car gnus-refer-article-method))
7963               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7964     (let (out)
7965       (dolist (method gnus-refer-article-method)
7966         (push (if (eq 'current method)
7967                   gnus-current-select-method
7968                 method)
7969               out))
7970       (nreverse out)))
7971    ;; One single select method.
7972    (t
7973     (list gnus-refer-article-method))))
7974
7975 (defun gnus-summary-edit-parameters ()
7976   "Edit the group parameters of the current group."
7977   (interactive)
7978   (gnus-group-edit-group gnus-newsgroup-name 'params))
7979
7980 (defun gnus-summary-customize-parameters ()
7981   "Customize the group parameters of the current group."
7982   (interactive)
7983   (gnus-group-customize gnus-newsgroup-name))
7984
7985 (defun gnus-summary-enter-digest-group (&optional force)
7986   "Enter an nndoc group based on the current article.
7987 If FORCE, force a digest interpretation.  If not, try
7988 to guess what the document format is."
7989   (interactive "P")
7990   (let ((conf gnus-current-window-configuration))
7991     (save-excursion
7992       (gnus-summary-select-article))
7993     (setq gnus-current-window-configuration conf)
7994     (let* ((name (format "%s-%d"
7995                          (gnus-group-prefixed-name
7996                           gnus-newsgroup-name (list 'nndoc ""))
7997                          (save-excursion
7998                            (set-buffer gnus-summary-buffer)
7999                            gnus-current-article)))
8000            (ogroup gnus-newsgroup-name)
8001            (params (append (gnus-info-params (gnus-get-info ogroup))
8002                            (list (cons 'to-group ogroup))
8003                            (list (cons 'save-article-group ogroup))))
8004            (case-fold-search t)
8005            (buf (current-buffer))
8006            dig to-address)
8007       (save-excursion
8008         (set-buffer gnus-original-article-buffer)
8009         ;; Have the digest group inherit the main mail address of
8010         ;; the parent article.
8011         (when (setq to-address (or (gnus-fetch-field "reply-to")
8012                                    (gnus-fetch-field "from")))
8013           (setq params (append
8014                         (list (cons 'to-address
8015                                     (funcall gnus-decode-encoded-word-function
8016                                              to-address))))))
8017         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8018         (insert-buffer-substring gnus-original-article-buffer)
8019         ;; Remove lines that may lead nndoc to misinterpret the
8020         ;; document type.
8021         (narrow-to-region
8022          (goto-char (point-min))
8023          (or (search-forward "\n\n" nil t) (point)))
8024         (goto-char (point-min))
8025         (delete-matching-lines "^Path:\\|^From ")
8026         (widen))
8027       (unwind-protect
8028           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8029                     (gnus-newsgroup-ephemeral-ignored-charsets
8030                      gnus-newsgroup-ignored-charsets))
8031                 (gnus-group-read-ephemeral-group
8032                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8033                               (nndoc-article-type
8034                                ,(if force 'mbox 'guess)))
8035                  t nil nil nil
8036                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8037                                                         "ADAPT")))))
8038               ;; Make all postings to this group go to the parent group.
8039               (nconc (gnus-info-params (gnus-get-info name))
8040                      params)
8041             ;; Couldn't select this doc group.
8042             (switch-to-buffer buf)
8043             (gnus-set-global-variables)
8044             (gnus-configure-windows 'summary)
8045             (gnus-message 3 "Article couldn't be entered?"))
8046         (kill-buffer dig)))))
8047
8048 (defun gnus-summary-read-document (n)
8049   "Open a new group based on the current article(s).
8050 This will allow you to read digests and other similar
8051 documents as newsgroups.
8052 Obeys the standard process/prefix convention."
8053   (interactive "P")
8054   (let* ((articles (gnus-summary-work-articles n))
8055          (ogroup gnus-newsgroup-name)
8056          (params (append (gnus-info-params (gnus-get-info ogroup))
8057                          (list (cons 'to-group ogroup))))
8058          article group egroup groups vgroup)
8059     (while (setq article (pop articles))
8060       (setq group (format "%s-%d" gnus-newsgroup-name article))
8061       (gnus-summary-remove-process-mark article)
8062       (when (gnus-summary-display-article article)
8063         (save-excursion
8064           (with-temp-buffer
8065             (insert-buffer-substring gnus-original-article-buffer)
8066             ;; Remove some headers that may lead nndoc to make
8067             ;; the wrong guess.
8068             (message-narrow-to-head)
8069             (goto-char (point-min))
8070             (delete-matching-lines "^\\(Path\\):\\|^From ")
8071             (widen)
8072             (if (setq egroup
8073                       (gnus-group-read-ephemeral-group
8074                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8075                                      (nndoc-article-type guess))
8076                        t nil t))
8077                 (progn
8078             ;; Make all postings to this group go to the parent group.
8079                   (nconc (gnus-info-params (gnus-get-info egroup))
8080                          params)
8081                   (push egroup groups))
8082               ;; Couldn't select this doc group.
8083               (gnus-error 3 "Article couldn't be entered"))))))
8084     ;; Now we have selected all the documents.
8085     (cond
8086      ((not groups)
8087       (error "None of the articles could be interpreted as documents"))
8088      ((gnus-group-read-ephemeral-group
8089        (setq vgroup (format
8090                      "nnvirtual:%s-%s" gnus-newsgroup-name
8091                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8092        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8093        t
8094        (cons (current-buffer) 'summary)))
8095      (t
8096       (error "Couldn't select virtual nndoc group")))))
8097
8098 (defun gnus-summary-isearch-article (&optional regexp-p)
8099   "Do incremental search forward on the current article.
8100 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8101   (interactive "P")
8102   (gnus-summary-select-article)
8103   (gnus-configure-windows 'article)
8104   (gnus-eval-in-buffer-window gnus-article-buffer
8105     (save-restriction
8106       (widen)
8107       (isearch-forward regexp-p))))
8108
8109 (defun gnus-summary-search-article-forward (regexp &optional backward)
8110   "Search for an article containing REGEXP forward.
8111 If BACKWARD, search backward instead."
8112   (interactive
8113    (list (read-string
8114           (format "Search article %s (regexp%s): "
8115                   (if current-prefix-arg "backward" "forward")
8116                   (if gnus-last-search-regexp
8117                       (concat ", default " gnus-last-search-regexp)
8118                     "")))
8119          current-prefix-arg))
8120   (if (string-equal regexp "")
8121       (setq regexp (or gnus-last-search-regexp ""))
8122     (setq gnus-last-search-regexp regexp)
8123     (setq gnus-article-before-search gnus-current-article))
8124   ;; Intentionally set gnus-last-article.
8125   (setq gnus-last-article gnus-article-before-search)
8126   (let ((gnus-last-article gnus-last-article))
8127     (if (gnus-summary-search-article regexp backward)
8128         (gnus-summary-show-thread)
8129       (error "Search failed: \"%s\"" regexp))))
8130
8131 (defun gnus-summary-search-article-backward (regexp)
8132   "Search for an article containing REGEXP backward."
8133   (interactive
8134    (list (read-string
8135           (format "Search article backward (regexp%s): "
8136                   (if gnus-last-search-regexp
8137                       (concat ", default " gnus-last-search-regexp)
8138                     "")))))
8139   (gnus-summary-search-article-forward regexp 'backward))
8140
8141 (defun gnus-summary-search-article (regexp &optional backward)
8142   "Search for an article containing REGEXP.
8143 Optional argument BACKWARD means do search for backward.
8144 `gnus-select-article-hook' is not called during the search."
8145   ;; We have to require this here to make sure that the following
8146   ;; dynamic binding isn't shadowed by autoloading.
8147   (require 'gnus-async)
8148   (require 'gnus-art)
8149   (let ((gnus-select-article-hook nil)  ;Disable hook.
8150         (gnus-article-prepare-hook nil)
8151         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8152         (gnus-use-article-prefetch nil)
8153         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8154         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8155         (sum (current-buffer))
8156         (gnus-display-mime-function nil)
8157         (found nil)
8158         point)
8159     (gnus-save-hidden-threads
8160       (gnus-summary-select-article)
8161       (set-buffer gnus-article-buffer)
8162       (goto-char (window-point (get-buffer-window (current-buffer))))
8163       (when backward
8164         (forward-line -1))
8165       (while (not found)
8166         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8167         (if (if backward
8168                 (re-search-backward regexp nil t)
8169               (re-search-forward regexp nil t))
8170             ;; We found the regexp.
8171             (progn
8172               (setq found 'found)
8173               (beginning-of-line)
8174               (set-window-start
8175                (get-buffer-window (current-buffer))
8176                (point))
8177               (forward-line 1)
8178               (set-window-point
8179                (get-buffer-window (current-buffer))
8180                (point))
8181               (set-buffer sum)
8182               (setq point (point)))
8183           ;; We didn't find it, so we go to the next article.
8184           (set-buffer sum)
8185           (setq found 'not)
8186           (while (eq found 'not)
8187             (if (not (if backward (gnus-summary-find-prev)
8188                        (gnus-summary-find-next)))
8189                 ;; No more articles.
8190                 (setq found t)
8191               ;; Select the next article and adjust point.
8192               (unless (gnus-summary-article-sparse-p
8193                        (gnus-summary-article-number))
8194                 (setq found nil)
8195                 (gnus-summary-select-article)
8196                 (set-buffer gnus-article-buffer)
8197                 (widen)
8198                 (goto-char (if backward (point-max) (point-min))))))))
8199       (gnus-message 7 ""))
8200     ;; Return whether we found the regexp.
8201     (when (eq found 'found)
8202       (goto-char point)
8203       (gnus-summary-show-thread)
8204       (gnus-summary-goto-subject gnus-current-article)
8205       (gnus-summary-position-point)
8206       t)))
8207
8208 (defun gnus-find-matching-articles (header regexp)
8209   "Return a list of all articles that match REGEXP on HEADER.
8210 This search includes all articles in the current group that Gnus has
8211 fetched headers for, whether they are displayed or not."
8212   (let ((articles nil)
8213         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8214         (case-fold-search t))
8215     (dolist (header gnus-newsgroup-headers)
8216       (when (string-match regexp (funcall func header))
8217         (push (mail-header-number header) articles)))
8218     (nreverse articles)))
8219
8220 (defun gnus-summary-find-matching (header regexp &optional backward unread
8221                                           not-case-fold not-matching)
8222   "Return a list of all articles that match REGEXP on HEADER.
8223 The search stars on the current article and goes forwards unless
8224 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8225 If UNREAD is non-nil, only unread articles will
8226 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8227 in the comparisons. If NOT-MATCHING, return a list of all articles that
8228 not match REGEXP on HEADER."
8229   (let ((case-fold-search (not not-case-fold))
8230         articles d func)
8231     (if (consp header)
8232         (if (eq (car header) 'extra)
8233             (setq func
8234                   `(lambda (h)
8235                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8236                          "")))
8237           (error "%s is an invalid header" header))
8238       (unless (fboundp (intern (concat "mail-header-" header)))
8239         (error "%s is not a valid header" header))
8240       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8241     (dolist (d (if (eq backward 'all)
8242                    gnus-newsgroup-data
8243                  (gnus-data-find-list
8244                   (gnus-summary-article-number)
8245                   (gnus-data-list backward))))
8246       (when (and (or (not unread)       ; We want all articles...
8247                      (gnus-data-unread-p d)) ; Or just unreads.
8248                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8249                  (if not-matching
8250                      (not (string-match
8251                            regexp
8252                            (funcall func (gnus-data-header d))))
8253                    (string-match regexp
8254                                  (funcall func (gnus-data-header d)))))
8255         (push (gnus-data-number d) articles))) ; Success!
8256     (nreverse articles)))
8257
8258 (defun gnus-summary-execute-command (header regexp command &optional backward)
8259   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8260 If HEADER is an empty string (or nil), the match is done on the entire
8261 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8262   (interactive
8263    (list (let ((completion-ignore-case t))
8264            (completing-read
8265             "Header name: "
8266             (mapcar (lambda (header) (list (format "%s" header)))
8267                     (append
8268                      '("Number" "Subject" "From" "Lines" "Date"
8269                        "Message-ID" "Xref" "References" "Body")
8270                      gnus-extra-headers))
8271             nil 'require-match))
8272          (read-string "Regexp: ")
8273          (read-key-sequence "Command: ")
8274          current-prefix-arg))
8275   (when (equal header "Body")
8276     (setq header ""))
8277   ;; Hidden thread subtrees must be searched as well.
8278   (gnus-summary-show-all-threads)
8279   ;; We don't want to change current point nor window configuration.
8280   (save-excursion
8281     (save-window-excursion
8282       (let (gnus-visual
8283             gnus-treat-strip-trailing-blank-lines
8284             gnus-treat-strip-leading-blank-lines
8285             gnus-treat-strip-multiple-blank-lines
8286             gnus-treat-hide-boring-headers
8287             gnus-treat-fold-newsgroups
8288             gnus-article-prepare-hook)
8289         (gnus-message 6 "Executing %s..." (key-description command))
8290         ;; We'd like to execute COMMAND interactively so as to give arguments.
8291         (gnus-execute header regexp
8292                       `(call-interactively ',(key-binding command))
8293                       backward)
8294         (gnus-message 6 "Executing %s...done" (key-description command))))))
8295
8296 (defun gnus-summary-beginning-of-article ()
8297   "Scroll the article back to the beginning."
8298   (interactive)
8299   (gnus-summary-select-article)
8300   (gnus-configure-windows 'article)
8301   (gnus-eval-in-buffer-window gnus-article-buffer
8302     (widen)
8303     (goto-char (point-min))
8304     (when gnus-page-broken
8305       (gnus-narrow-to-page))))
8306
8307 (defun gnus-summary-end-of-article ()
8308   "Scroll to the end of the article."
8309   (interactive)
8310   (gnus-summary-select-article)
8311   (gnus-configure-windows 'article)
8312   (gnus-eval-in-buffer-window gnus-article-buffer
8313     (widen)
8314     (goto-char (point-max))
8315     (recenter -3)
8316     (when gnus-page-broken
8317       (gnus-narrow-to-page))))
8318
8319 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8320   "Truncate to LEN and quote all \"(\"'s in STRING."
8321   (gnus-replace-in-string (if (and len (> (length string) len))
8322                               (substring string 0 len)
8323                             string)
8324                           "[()]" "\\\\\\&"))
8325
8326 (defun gnus-summary-print-article (&optional filename n)
8327   "Generate and print a PostScript image of the N next (mail) articles.
8328
8329 If N is negative, print the N previous articles.  If N is nil and articles
8330 have been marked with the process mark, print these instead.
8331
8332 If the optional first argument FILENAME is nil, send the image to the
8333 printer.  If FILENAME is a string, save the PostScript image in a file with
8334 that name.  If FILENAME is a number, prompt the user for the name of the file
8335 to save in."
8336   (interactive (list (ps-print-preprint current-prefix-arg)))
8337   (dolist (article (gnus-summary-work-articles n))
8338     (gnus-summary-select-article nil nil 'pseudo article)
8339     (gnus-eval-in-buffer-window gnus-article-buffer
8340       (gnus-print-buffer))
8341     (gnus-summary-remove-process-mark article))
8342   (ps-despool filename))
8343
8344 (defun gnus-print-buffer ()
8345   (let ((buffer (generate-new-buffer " *print*")))
8346     (unwind-protect
8347         (progn
8348           (copy-to-buffer buffer (point-min) (point-max))
8349           (set-buffer buffer)
8350           (gnus-article-delete-invisible-text)
8351           (gnus-remove-text-with-property 'gnus-decoration)
8352           (when (gnus-visual-p 'article-highlight 'highlight)
8353             ;; Copy-to-buffer doesn't copy overlay.  So redo
8354             ;; highlight.
8355             (let ((gnus-article-buffer buffer))
8356               (gnus-article-highlight-citation t)
8357               (gnus-article-highlight-signature)))
8358           (let ((ps-left-header
8359                  (list
8360                   (concat "("
8361                           (gnus-summary-print-truncate-and-quote
8362                            (mail-header-subject gnus-current-headers)
8363                            66) ")")
8364                   (concat "("
8365                           (gnus-summary-print-truncate-and-quote
8366                            (mail-header-from gnus-current-headers)
8367                            45) ")")))
8368                 (ps-right-header
8369                  (list
8370                   "/pagenumberstring load"
8371                   (concat "("
8372                           (mail-header-date gnus-current-headers) ")"))))
8373             (gnus-run-hooks 'gnus-ps-print-hook)
8374             (save-excursion
8375               (if window-system
8376                   (ps-spool-buffer-with-faces)
8377                 (ps-spool-buffer)))))
8378       (kill-buffer buffer))))
8379
8380 (defun gnus-summary-show-article (&optional arg)
8381   "Force redisplaying of the current article.
8382 If ARG (the prefix) is a number, show the article with the charset
8383 defined in `gnus-summary-show-article-charset-alist', or the charset
8384 input.
8385 If ARG (the prefix) is non-nil and not a number, show the raw article
8386 without any article massaging functions being run.  Normally, the key strokes
8387 are `C-u g'."
8388   (interactive "P")
8389   (cond
8390    ((numberp arg)
8391     (gnus-summary-show-article t)
8392     (let ((gnus-newsgroup-charset
8393            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8394                (mm-read-coding-system
8395                 "View as charset: " ;; actually it is coding system.
8396                 (save-excursion
8397                   (set-buffer gnus-article-buffer)
8398                   (mm-detect-coding-region (point) (point-max))))))
8399           (gnus-newsgroup-ignored-charsets 'gnus-all))
8400       (gnus-summary-select-article nil 'force)
8401       (let ((deps gnus-newsgroup-dependencies)
8402             head header lines)
8403         (save-excursion
8404           (set-buffer gnus-original-article-buffer)
8405           (save-restriction
8406             (message-narrow-to-head)
8407             (setq head (buffer-string))
8408             (goto-char (point-min))
8409             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8410               (goto-char (point-max))
8411               (widen)
8412               (setq lines (1- (count-lines (point) (point-max))))))
8413           (with-temp-buffer
8414             (insert (format "211 %d Article retrieved.\n"
8415                             (cdr gnus-article-current)))
8416             (insert head)
8417             (if lines (insert (format "Lines: %d\n" lines)))
8418             (insert ".\n")
8419             (let ((nntp-server-buffer (current-buffer)))
8420               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8421         (gnus-data-set-header
8422          (gnus-data-find (cdr gnus-article-current))
8423          header)
8424         (gnus-summary-update-article-line
8425          (cdr gnus-article-current) header)
8426         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8427           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8428    ((not arg)
8429     ;; Select the article the normal way.
8430     (gnus-summary-select-article nil 'force))
8431    (t
8432     ;; We have to require this here to make sure that the following
8433     ;; dynamic binding isn't shadowed by autoloading.
8434     (require 'gnus-async)
8435     (require 'gnus-art)
8436     ;; Bind the article treatment functions to nil.
8437     (let ((gnus-have-all-headers t)
8438           gnus-article-prepare-hook
8439           gnus-article-decode-hook
8440           gnus-display-mime-function
8441           gnus-break-pages)
8442       ;; Destroy any MIME parts.
8443       (when (gnus-buffer-live-p gnus-article-buffer)
8444         (save-excursion
8445           (set-buffer gnus-article-buffer)
8446           (mm-destroy-parts gnus-article-mime-handles)
8447           ;; Set it to nil for safety reason.
8448           (setq gnus-article-mime-handle-alist nil)
8449           (setq gnus-article-mime-handles nil)))
8450       (gnus-summary-select-article nil 'force))))
8451   (gnus-summary-goto-subject gnus-current-article)
8452   (gnus-summary-position-point))
8453
8454 (defun gnus-summary-show-raw-article ()
8455   "Show the raw article without any article massaging functions being run."
8456   (interactive)
8457   (gnus-summary-show-article t))
8458
8459 (defun gnus-summary-verbose-headers (&optional arg)
8460   "Toggle permanent full header display.
8461 If ARG is a positive number, turn header display on.
8462 If ARG is a negative number, turn header display off."
8463   (interactive "P")
8464   (setq gnus-show-all-headers
8465         (cond ((or (not (numberp arg))
8466                    (zerop arg))
8467                (not gnus-show-all-headers))
8468               ((natnump arg)
8469                t)))
8470   (gnus-summary-show-article))
8471
8472 (defun gnus-summary-toggle-header (&optional arg)
8473   "Show the headers if they are hidden, or hide them if they are shown.
8474 If ARG is a positive number, show the entire header.
8475 If ARG is a negative number, hide the unwanted header lines."
8476   (interactive "P")
8477   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8478                      (get-buffer-window gnus-article-buffer t))))
8479     (with-current-buffer gnus-article-buffer
8480       (widen)
8481       (article-narrow-to-head)
8482       (let* ((buffer-read-only nil)
8483              (inhibit-point-motion-hooks t)
8484              (hidden (if (numberp arg)
8485                          (>= arg 0)
8486                        (gnus-article-hidden-text-p 'headers)))
8487              s e)
8488         (delete-region (point-min) (point-max))
8489         (with-current-buffer gnus-original-article-buffer
8490           (goto-char (setq s (point-min)))
8491           (setq e (if (search-forward "\n\n" nil t)
8492                       (1- (point))
8493                     (point-max))))
8494         (insert-buffer-substring gnus-original-article-buffer s e)
8495         (article-decode-encoded-words)
8496         (if hidden
8497             (let ((gnus-treat-hide-headers nil)
8498                   (gnus-treat-hide-boring-headers nil))
8499               (gnus-delete-wash-type 'headers)
8500               (gnus-treat-article 'head))
8501           (gnus-treat-article 'head))
8502         (widen)
8503         (if window
8504             (set-window-start window (goto-char (point-min))))
8505         (setq gnus-page-broken
8506               (when gnus-break-pages
8507                 (gnus-narrow-to-page)
8508                 t))
8509         (gnus-set-mode-line 'article)))))
8510
8511 (defun gnus-summary-show-all-headers ()
8512   "Make all header lines visible."
8513   (interactive)
8514   (gnus-summary-toggle-header 1))
8515
8516 (defun gnus-summary-caesar-message (&optional arg)
8517   "Caesar rotate the current article by 13.
8518 The numerical prefix specifies how many places to rotate each letter
8519 forward."
8520   (interactive "P")
8521   (gnus-summary-select-article)
8522   (let ((mail-header-separator ""))
8523     (gnus-eval-in-buffer-window gnus-article-buffer
8524       (save-restriction
8525         (widen)
8526         (let ((start (window-start))
8527               buffer-read-only)
8528           (message-caesar-buffer-body arg)
8529           (set-window-start (get-buffer-window (current-buffer)) start))))))
8530
8531 (autoload 'unmorse-region "morse"
8532   "Convert morse coded text in region to ordinary ASCII text."
8533   t)
8534
8535 (defun gnus-summary-morse-message (&optional arg)
8536   "Morse decode the current article."
8537   (interactive "P")
8538   (gnus-summary-select-article)
8539   (let ((mail-header-separator ""))
8540     (gnus-eval-in-buffer-window gnus-article-buffer
8541       (save-excursion
8542         (save-restriction
8543           (widen)
8544           (let ((pos (window-start))
8545                 buffer-read-only)
8546             (goto-char (point-min))
8547             (when (message-goto-body)
8548               (gnus-narrow-to-body))
8549             (goto-char (point-min))
8550             (while (re-search-forward "·" (point-max) t)
8551               (replace-match "."))
8552             (unmorse-region (point-min) (point-max))
8553             (widen)
8554             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8555
8556 (defun gnus-summary-stop-page-breaking ()
8557   "Stop page breaking in the current article."
8558   (interactive)
8559   (gnus-summary-select-article)
8560   (gnus-eval-in-buffer-window gnus-article-buffer
8561     (widen)
8562     (when (gnus-visual-p 'page-marker)
8563       (let ((buffer-read-only nil))
8564         (gnus-remove-text-with-property 'gnus-prev)
8565         (gnus-remove-text-with-property 'gnus-next))
8566       (setq gnus-page-broken nil))))
8567
8568 (defun gnus-summary-move-article (&optional n to-newsgroup
8569                                             select-method action)
8570   "Move the current article to a different newsgroup.
8571 If N is a positive number, move the N next articles.
8572 If N is a negative number, move the N previous articles.
8573 If N is nil and any articles have been marked with the process mark,
8574 move those articles instead.
8575 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8576 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8577 re-spool using this method.
8578
8579 When called interactively with TO-NEWSGROUP being nil, the value of
8580 the variable `gnus-move-split-methods' is used for finding a default
8581 for the target newsgroup.
8582
8583 For this function to work, both the current newsgroup and the
8584 newsgroup that you want to move to have to support the `request-move'
8585 and `request-accept' functions.
8586
8587 ACTION can be either `move' (the default), `crosspost' or `copy'."
8588   (interactive "P")
8589   (unless action
8590     (setq action 'move))
8591   ;; Check whether the source group supports the required functions.
8592   (cond ((and (eq action 'move)
8593               (not (gnus-check-backend-function
8594                     'request-move-article gnus-newsgroup-name)))
8595          (error "The current group does not support article moving"))
8596         ((and (eq action 'crosspost)
8597               (not (gnus-check-backend-function
8598                     'request-replace-article gnus-newsgroup-name)))
8599          (error "The current group does not support article editing")))
8600   (let ((articles (gnus-summary-work-articles n))
8601         (prefix (if (gnus-check-backend-function
8602                      'request-move-article gnus-newsgroup-name)
8603                     (gnus-group-real-prefix gnus-newsgroup-name)
8604                   ""))
8605         (names '((move "Move" "Moving")
8606                  (copy "Copy" "Copying")
8607                  (crosspost "Crosspost" "Crossposting")))
8608         (copy-buf (save-excursion
8609                     (nnheader-set-temp-buffer " *copy article*")))
8610         art-group to-method new-xref article to-groups)
8611     (unless (assq action names)
8612       (error "Unknown action %s" action))
8613     ;; Read the newsgroup name.
8614     (when (and (not to-newsgroup)
8615                (not select-method))
8616       (if (and gnus-move-split-methods
8617                (not
8618                 (and (memq gnus-current-article articles)
8619                      (gnus-buffer-live-p gnus-original-article-buffer))))
8620           ;; When `gnus-move-split-methods' is non-nil, we have to
8621           ;; select an article to give `gnus-read-move-group-name' an
8622           ;; opportunity to suggest an appropriate default.  However,
8623           ;; we needn't render or mark the article.
8624           (let ((gnus-display-mime-function nil)
8625                 (gnus-article-prepare-hook nil)
8626                 (gnus-mark-article-hook nil))
8627             (gnus-summary-select-article nil nil nil (car articles))))
8628       (setq to-newsgroup
8629             (gnus-read-move-group-name
8630              (cadr (assq action names))
8631              (symbol-value (intern (format "gnus-current-%s-group" action)))
8632              articles prefix))
8633       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8634     (setq to-method (or select-method
8635                         (gnus-server-to-method
8636                          (gnus-group-method to-newsgroup))))
8637     ;; Check the method we are to move this article to...
8638     (unless (gnus-check-backend-function
8639              'request-accept-article (car to-method))
8640       (error "%s does not support article copying" (car to-method)))
8641     (unless (gnus-check-server to-method)
8642       (error "Can't open server %s" (car to-method)))
8643     (gnus-message 6 "%s to %s: %s..."
8644                   (caddr (assq action names))
8645                   (or (car select-method) to-newsgroup) articles)
8646     (while articles
8647       (setq article (pop articles))
8648       (setq
8649        art-group
8650        (cond
8651         ;; Move the article.
8652         ((eq action 'move)
8653          ;; Remove this article from future suppression.
8654          (gnus-dup-unsuppress-article article)
8655          (gnus-request-move-article
8656           article                       ; Article to move
8657           gnus-newsgroup-name           ; From newsgroup
8658           (nth 1 (gnus-find-method-for-group
8659                   gnus-newsgroup-name)) ; Server
8660           (list 'gnus-request-accept-article
8661                 to-newsgroup (list 'quote select-method)
8662                 (not articles) t)       ; Accept form
8663           (not articles)))              ; Only save nov last time
8664         ;; Copy the article.
8665         ((eq action 'copy)
8666          (save-excursion
8667            (set-buffer copy-buf)
8668            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8669              (gnus-request-accept-article
8670               to-newsgroup select-method (not articles) t))))
8671         ;; Crosspost the article.
8672         ((eq action 'crosspost)
8673          (let ((xref (message-tokenize-header
8674                       (mail-header-xref (gnus-summary-article-header article))
8675                       " ")))
8676            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8677                                   ":" (number-to-string article)))
8678            (unless xref
8679              (setq xref (list (system-name))))
8680            (setq new-xref
8681                  (concat
8682                   (mapconcat 'identity
8683                              (delete "Xref:" (delete new-xref xref))
8684                              " ")
8685                   " " new-xref))
8686            (save-excursion
8687              (set-buffer copy-buf)
8688              ;; First put the article in the destination group.
8689              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8690              (when (consp (setq art-group
8691                                 (gnus-request-accept-article
8692                                  to-newsgroup select-method (not articles))))
8693                (setq new-xref (concat new-xref " " (car art-group)
8694                                       ":"
8695                                       (number-to-string (cdr art-group))))
8696                ;; Now we have the new Xrefs header, so we insert
8697                ;; it and replace the new article.
8698                (nnheader-replace-header "Xref" new-xref)
8699                (gnus-request-replace-article
8700                 (cdr art-group) to-newsgroup (current-buffer))
8701                art-group))))))
8702       (cond
8703        ((not art-group)
8704         (gnus-message 1 "Couldn't %s article %s: %s"
8705                       (cadr (assq action names)) article
8706                       (nnheader-get-report (car to-method))))
8707        ((eq art-group 'junk)
8708         (when (eq action 'move)
8709           (gnus-summary-mark-article article gnus-canceled-mark)
8710           (gnus-message 4 "Deleted article %s" article)))
8711        (t
8712         (let* ((pto-group (gnus-group-prefixed-name
8713                            (car art-group) to-method))
8714                (entry
8715                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8716                (info (nth 2 entry))
8717                (to-group (gnus-info-group info))
8718                to-marks)
8719           ;; Update the group that has been moved to.
8720           (when (and info
8721                      (memq action '(move copy)))
8722             (unless (member to-group to-groups)
8723               (push to-group to-groups))
8724
8725             (unless (memq article gnus-newsgroup-unreads)
8726               (push 'read to-marks)
8727               (gnus-info-set-read
8728                info (gnus-add-to-range (gnus-info-read info)
8729                                        (list (cdr art-group)))))
8730
8731             ;; See whether the article is to be put in the cache.
8732             (let ((marks gnus-article-mark-lists)
8733                   (to-article (cdr art-group)))
8734
8735               ;; Enter the article into the cache in the new group,
8736               ;; if that is required.
8737               (when gnus-use-cache
8738                 (gnus-cache-possibly-enter-article
8739                  to-group to-article
8740                  (memq article gnus-newsgroup-marked)
8741                  (memq article gnus-newsgroup-dormant)
8742                  (memq article gnus-newsgroup-unreads)))
8743
8744               (when gnus-preserve-marks
8745                 ;; Copy any marks over to the new group.
8746                 (when (and (equal to-group gnus-newsgroup-name)
8747                            (not (memq article gnus-newsgroup-unreads)))
8748                   ;; Mark this article as read in this group.
8749                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8750                   (setcdr (gnus-active to-group) to-article)
8751                   (setcdr gnus-newsgroup-active to-article))
8752
8753                 (while marks
8754                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8755                     (when (memq article (symbol-value
8756                                          (intern (format "gnus-newsgroup-%s"
8757                                                          (caar marks)))))
8758                       (push (cdar marks) to-marks)
8759                       ;; If the other group is the same as this group,
8760                       ;; then we have to add the mark to the list.
8761                       (when (equal to-group gnus-newsgroup-name)
8762                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8763                              (cons to-article
8764                                    (symbol-value
8765                                     (intern (format "gnus-newsgroup-%s"
8766                                                     (caar marks)))))))
8767                       ;; Copy the marks to other group.
8768                       (gnus-add-marked-articles
8769                        to-group (cdar marks) (list to-article) info)))
8770                   (setq marks (cdr marks)))
8771
8772                 (gnus-request-set-mark
8773                  to-group (list (list (list to-article) 'add to-marks))))
8774
8775               (gnus-dribble-enter
8776                (concat "(gnus-group-set-info '"
8777                        (gnus-prin1-to-string (gnus-get-info to-group))
8778                        ")"))))
8779
8780           ;; Update the Xref header in this article to point to
8781           ;; the new crossposted article we have just created.
8782           (when (eq action 'crosspost)
8783             (save-excursion
8784               (set-buffer copy-buf)
8785               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8786               (nnheader-replace-header "Xref" new-xref)
8787               (gnus-request-replace-article
8788                article gnus-newsgroup-name (current-buffer)))))
8789
8790         ;;;!!!Why is this necessary?
8791         (set-buffer gnus-summary-buffer)
8792
8793         (gnus-summary-goto-subject article)
8794         (when (eq action 'move)
8795           (gnus-summary-mark-article article gnus-canceled-mark))))
8796       (gnus-summary-remove-process-mark article))
8797     ;; Re-activate all groups that have been moved to.
8798     (save-excursion
8799       (set-buffer gnus-group-buffer)
8800       (let ((gnus-group-marked to-groups))
8801         (gnus-group-get-new-news-this-group nil t)))
8802
8803     (gnus-kill-buffer copy-buf)
8804     (gnus-summary-position-point)
8805     (gnus-set-mode-line 'summary)))
8806
8807 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8808   "Move the current article to a different newsgroup.
8809 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8810 When called interactively, if TO-NEWSGROUP is nil, use the value of
8811 the variable `gnus-move-split-methods' for finding a default target
8812 newsgroup.
8813 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8814 re-spool using this method."
8815   (interactive "P")
8816   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8817
8818 (defun gnus-summary-crosspost-article (&optional n)
8819   "Crosspost the current article to some other group."
8820   (interactive "P")
8821   (gnus-summary-move-article n nil nil 'crosspost))
8822
8823 (defcustom gnus-summary-respool-default-method nil
8824   "Default method type for respooling an article.
8825 If nil, use to the current newsgroup method."
8826   :type 'symbol
8827   :group 'gnus-summary-mail)
8828
8829 (defcustom gnus-summary-display-while-building nil
8830   "If not-nil, show and update the summary buffer as it's being built.
8831 If the value is t, update the buffer after every line is inserted.  If
8832 the value is an integer (N), update the display every N lines."
8833   :group 'gnus-thread
8834   :type '(choice (const :tag "off" nil)
8835                  number
8836                  (const :tag "frequently" t)))
8837
8838 (defun gnus-summary-respool-article (&optional n method)
8839   "Respool the current article.
8840 The article will be squeezed through the mail spooling process again,
8841 which means that it will be put in some mail newsgroup or other
8842 depending on `nnmail-split-methods'.
8843 If N is a positive number, respool the N next articles.
8844 If N is a negative number, respool the N previous articles.
8845 If N is nil and any articles have been marked with the process mark,
8846 respool those articles instead.
8847
8848 Respooling can be done both from mail groups and \"real\" newsgroups.
8849 In the former case, the articles in question will be moved from the
8850 current group into whatever groups they are destined to.  In the
8851 latter case, they will be copied into the relevant groups."
8852   (interactive
8853    (list current-prefix-arg
8854          (let* ((methods (gnus-methods-using 'respool))
8855                 (methname
8856                  (symbol-name (or gnus-summary-respool-default-method
8857                                   (car (gnus-find-method-for-group
8858                                         gnus-newsgroup-name)))))
8859                 (method
8860                  (gnus-completing-read-with-default
8861                   methname "What backend do you want to use when respooling?"
8862                   methods nil t nil 'gnus-mail-method-history))
8863                 ms)
8864            (cond
8865             ((zerop (length (setq ms (gnus-servers-using-backend
8866                                       (intern method)))))
8867              (list (intern method) ""))
8868             ((= 1 (length ms))
8869              (car ms))
8870             (t
8871              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8872                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8873                            ms-alist))))))))
8874   (unless method
8875     (error "No method given for respooling"))
8876   (if (assoc (symbol-name
8877               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8878              (gnus-methods-using 'respool))
8879       (gnus-summary-move-article n nil method)
8880     (gnus-summary-copy-article n nil method)))
8881
8882 (defun gnus-summary-import-article (file &optional edit)
8883   "Import an arbitrary file into a mail newsgroup."
8884   (interactive "fImport file: \nP")
8885   (let ((group gnus-newsgroup-name)
8886         (now (current-time))
8887         atts lines group-art)
8888     (unless (gnus-check-backend-function 'request-accept-article group)
8889       (error "%s does not support article importing" group))
8890     (or (file-readable-p file)
8891         (not (file-regular-p file))
8892         (error "Can't read %s" file))
8893     (save-excursion
8894       (set-buffer (gnus-get-buffer-create " *import file*"))
8895       (erase-buffer)
8896       (nnheader-insert-file-contents file)
8897       (goto-char (point-min))
8898       (if (nnheader-article-p)
8899           (save-restriction
8900             (goto-char (point-min))
8901             (search-forward "\n\n" nil t)
8902             (narrow-to-region (point-min) (1- (point)))
8903             (goto-char (point-min))
8904             (unless (re-search-forward "^date:" nil t)
8905               (goto-char (point-max))
8906               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8907        ;; This doesn't look like an article, so we fudge some headers.
8908         (setq atts (file-attributes file)
8909               lines (count-lines (point-min) (point-max)))
8910         (insert "From: " (read-string "From: ") "\n"
8911                 "Subject: " (read-string "Subject: ") "\n"
8912                 "Date: " (message-make-date (nth 5 atts)) "\n"
8913                 "Message-ID: " (message-make-message-id) "\n"
8914                 "Lines: " (int-to-string lines) "\n"
8915                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8916       (setq group-art (gnus-request-accept-article group nil t))
8917       (kill-buffer (current-buffer)))
8918     (setq gnus-newsgroup-active (gnus-activate-group group))
8919     (forward-line 1)
8920     (gnus-summary-goto-article (cdr group-art) nil t)
8921     (when edit
8922       (gnus-summary-edit-article))))
8923
8924 (defun gnus-summary-create-article ()
8925   "Create an article in a mail newsgroup."
8926   (interactive)
8927   (let ((group gnus-newsgroup-name)
8928         (now (current-time))
8929         group-art)
8930     (unless (gnus-check-backend-function 'request-accept-article group)
8931       (error "%s does not support article importing" group))
8932     (save-excursion
8933       (set-buffer (gnus-get-buffer-create " *import file*"))
8934       (erase-buffer)
8935       (goto-char (point-min))
8936       ;; This doesn't look like an article, so we fudge some headers.
8937       (insert "From: " (read-string "From: ") "\n"
8938               "Subject: " (read-string "Subject: ") "\n"
8939               "Date: " (message-make-date now) "\n"
8940               "Message-ID: " (message-make-message-id) "\n")
8941       (setq group-art (gnus-request-accept-article group nil t))
8942       (kill-buffer (current-buffer)))
8943     (setq gnus-newsgroup-active (gnus-activate-group group))
8944     (forward-line 1)
8945     (gnus-summary-goto-article (cdr group-art) nil t)
8946     (gnus-summary-edit-article)))
8947
8948 (defun gnus-summary-article-posted-p ()
8949   "Say whether the current (mail) article is available from news as well.
8950 This will be the case if the article has both been mailed and posted."
8951   (interactive)
8952   (let ((id (mail-header-references (gnus-summary-article-header)))
8953         (gnus-override-method (car (gnus-refer-article-methods))))
8954     (if (gnus-request-head id "")
8955         (gnus-message 2 "The current message was found on %s"
8956                       gnus-override-method)
8957       (gnus-message 2 "The current message couldn't be found on %s"
8958                     gnus-override-method)
8959       nil)))
8960
8961 (defun gnus-summary-expire-articles (&optional now)
8962   "Expire all articles that are marked as expirable in the current group."
8963   (interactive)
8964   (when (gnus-check-backend-function
8965          'request-expire-articles gnus-newsgroup-name)
8966     ;; This backend supports expiry.
8967     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8968            (expirable (if total
8969                           (progn
8970                             ;; We need to update the info for
8971                             ;; this group for `gnus-list-of-read-articles'
8972                             ;; to give us the right answer.
8973                             (gnus-run-hooks 'gnus-exit-group-hook)
8974                             (gnus-summary-update-info)
8975                             (gnus-list-of-read-articles gnus-newsgroup-name))
8976                         (setq gnus-newsgroup-expirable
8977                               (sort gnus-newsgroup-expirable '<))))
8978            (expiry-wait (if now 'immediate
8979                           (gnus-group-find-parameter
8980                            gnus-newsgroup-name 'expiry-wait)))
8981            (nnmail-expiry-target
8982             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8983                 nnmail-expiry-target))
8984            es)
8985       (when expirable
8986         ;; There are expirable articles in this group, so we run them
8987         ;; through the expiry process.
8988         (gnus-message 6 "Expiring articles...")
8989         (unless (gnus-check-group gnus-newsgroup-name)
8990           (error "Can't open server for %s" gnus-newsgroup-name))
8991         ;; The list of articles that weren't expired is returned.
8992         (save-excursion
8993           (if expiry-wait
8994               (let ((nnmail-expiry-wait-function nil)
8995                     (nnmail-expiry-wait expiry-wait))
8996                 (setq es (gnus-request-expire-articles
8997                           expirable gnus-newsgroup-name)))
8998             (setq es (gnus-request-expire-articles
8999                       expirable gnus-newsgroup-name)))
9000           (unless total
9001             (setq gnus-newsgroup-expirable es))
9002           ;; We go through the old list of expirable, and mark all
9003           ;; really expired articles as nonexistent.
9004           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9005             (let ((gnus-use-cache nil))
9006               (dolist (article expirable)
9007                 (when (and (not (memq article es))
9008                            (gnus-data-find article))
9009                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9010         (gnus-message 6 "Expiring articles...done")))))
9011
9012 (defun gnus-summary-expire-articles-now ()
9013   "Expunge all expirable articles in the current group.
9014 This means that *all* articles that are marked as expirable will be
9015 deleted forever, right now."
9016   (interactive)
9017   (or gnus-expert-user
9018       (gnus-yes-or-no-p
9019        "Are you really, really, really sure you want to delete all these messages? ")
9020       (error "Phew!"))
9021   (gnus-summary-expire-articles t))
9022
9023 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9024 (defun gnus-summary-delete-article (&optional n)
9025   "Delete the N next (mail) articles.
9026 This command actually deletes articles.  This is not a marking
9027 command.  The article will disappear forever from your life, never to
9028 return.
9029 If N is negative, delete backwards.
9030 If N is nil and articles have been marked with the process mark,
9031 delete these instead."
9032   (interactive "P")
9033   (unless (gnus-check-backend-function 'request-expire-articles
9034                                        gnus-newsgroup-name)
9035     (error "The current newsgroup does not support article deletion"))
9036   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9037     (error "Couldn't open server"))
9038   ;; Compute the list of articles to delete.
9039   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9040         (nnmail-expiry-target 'delete)
9041         not-deleted)
9042     (if (and gnus-novice-user
9043              (not (gnus-yes-or-no-p
9044                    (format "Do you really want to delete %s forever? "
9045                            (if (> (length articles) 1)
9046                                (format "these %s articles" (length articles))
9047                              "this article")))))
9048         ()
9049       ;; Delete the articles.
9050       (setq not-deleted (gnus-request-expire-articles
9051                          articles gnus-newsgroup-name 'force))
9052       (while articles
9053         (gnus-summary-remove-process-mark (car articles))
9054         ;; The backend might not have been able to delete the article
9055         ;; after all.
9056         (unless (memq (car articles) not-deleted)
9057           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9058         (setq articles (cdr articles)))
9059       (when not-deleted
9060         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9061     (gnus-summary-position-point)
9062     (gnus-set-mode-line 'summary)
9063     not-deleted))
9064
9065 (defun gnus-summary-edit-article (&optional arg)
9066   "Edit the current article.
9067 This will have permanent effect only in mail groups.
9068 If ARG is nil, edit the decoded articles.
9069 If ARG is 1, edit the raw articles.
9070 If ARG is 2, edit the raw articles even in read-only groups.
9071 If ARG is 3, edit the articles with the current handles.
9072 Otherwise, allow editing of articles even in read-only
9073 groups."
9074   (interactive "P")
9075   (let (force raw current-handles)
9076     (cond
9077      ((null arg))
9078      ((eq arg 1)
9079       (setq raw t))
9080      ((eq arg 2)
9081       (setq raw t
9082             force t))
9083      ((eq arg 3)
9084       (setq current-handles
9085             (and (gnus-buffer-live-p gnus-article-buffer)
9086                  (with-current-buffer gnus-article-buffer
9087                    (prog1
9088                        gnus-article-mime-handles
9089                      (setq gnus-article-mime-handles nil))))))
9090      (t
9091       (setq force t)))
9092     (when (and raw (not force)
9093                (member gnus-newsgroup-name '("nndraft:delayed"
9094                                              "nndraft:drafts"
9095                                              "nndraft:queue")))
9096       (error "Can't edit the raw article in group %s"
9097              gnus-newsgroup-name))
9098     (save-excursion
9099       (set-buffer gnus-summary-buffer)
9100       (let ((mail-parse-charset gnus-newsgroup-charset)
9101             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9102         (gnus-set-global-variables)
9103         (when (and (not force)
9104                    (gnus-group-read-only-p))
9105           (error "The current newsgroup does not support article editing"))
9106         (gnus-summary-show-article t)
9107         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9108           (with-current-buffer gnus-article-buffer
9109             (mm-enable-multibyte)))
9110         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9111             (setq raw t))
9112         (gnus-article-edit-article
9113          (if raw 'ignore
9114            `(lambda ()
9115               (let ((mbl mml-buffer-list))
9116                 (setq mml-buffer-list nil)
9117                 (mime-to-mml ,'current-handles)
9118                 (let ((mbl1 mml-buffer-list))
9119                   (setq mml-buffer-list mbl)
9120                   (set (make-local-variable 'mml-buffer-list) mbl1))
9121                 (make-local-hook 'kill-buffer-hook)
9122                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9123          `(lambda (no-highlight)
9124             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9125                   (message-options message-options)
9126                   (message-options-set-recipient)
9127                   (mail-parse-ignored-charsets
9128                    ',gnus-newsgroup-ignored-charsets))
9129               ,(if (not raw) '(progn
9130                                 (mml-to-mime)
9131                                 (mml-destroy-buffers)
9132                                 (remove-hook 'kill-buffer-hook
9133                                              'mml-destroy-buffers t)
9134                                 (kill-local-variable 'mml-buffer-list)))
9135               (gnus-summary-edit-article-done
9136                ,(or (mail-header-references gnus-current-headers) "")
9137                ,(gnus-group-read-only-p)
9138                ,gnus-summary-buffer no-highlight))))))))
9139
9140 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9141
9142 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9143                                                  no-highlight)
9144   "Make edits to the current article permanent."
9145   (interactive)
9146   (save-excursion
9147    ;; The buffer restriction contains the entire article if it exists.
9148     (when (article-goto-body)
9149       (let ((lines (count-lines (point) (point-max)))
9150             (length (- (point-max) (point)))
9151             (case-fold-search t)
9152             (body (copy-marker (point))))
9153         (goto-char (point-min))
9154         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9155           (delete-region (match-beginning 1) (match-end 1))
9156           (insert (number-to-string length)))
9157         (goto-char (point-min))
9158         (when (re-search-forward
9159                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9160           (delete-region (match-beginning 1) (match-end 1))
9161           (insert (number-to-string length)))
9162         (goto-char (point-min))
9163         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9164           (delete-region (match-beginning 1) (match-end 1))
9165           (insert (number-to-string lines))))))
9166   ;; Replace the article.
9167   (let ((buf (current-buffer)))
9168     (with-temp-buffer
9169       (insert-buffer-substring buf)
9170
9171       (if (and (not read-only)
9172                (not (gnus-request-replace-article
9173                      (cdr gnus-article-current) (car gnus-article-current)
9174                      (current-buffer) t)))
9175           (error "Couldn't replace article")
9176         ;; Update the summary buffer.
9177         (if (and references
9178                  (equal (message-tokenize-header references " ")
9179                         (message-tokenize-header
9180                          (or (message-fetch-field "references") "") " ")))
9181             ;; We only have to update this line.
9182             (save-excursion
9183               (save-restriction
9184                 (message-narrow-to-head)
9185                 (let ((head (buffer-string))
9186                       header)
9187                   (with-temp-buffer
9188                     (insert (format "211 %d Article retrieved.\n"
9189                                     (cdr gnus-article-current)))
9190                     (insert head)
9191                     (insert ".\n")
9192                     (let ((nntp-server-buffer (current-buffer)))
9193                       (setq header (car (gnus-get-newsgroup-headers
9194                                          nil t))))
9195                     (save-excursion
9196                       (set-buffer gnus-summary-buffer)
9197                       (gnus-data-set-header
9198                        (gnus-data-find (cdr gnus-article-current))
9199                        header)
9200                       (gnus-summary-update-article-line
9201                        (cdr gnus-article-current) header)
9202                       (if (gnus-summary-goto-subject
9203                            (cdr gnus-article-current) nil t)
9204                           (gnus-summary-update-secondary-mark
9205                            (cdr gnus-article-current))))))))
9206           ;; Update threads.
9207           (set-buffer (or buffer gnus-summary-buffer))
9208           (gnus-summary-update-article (cdr gnus-article-current))
9209           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9210               (gnus-summary-update-secondary-mark
9211                (cdr gnus-article-current))))
9212         ;; Prettify the article buffer again.
9213         (unless no-highlight
9214           (save-excursion
9215             (set-buffer gnus-article-buffer)
9216             ;;;!!! Fix this -- article should be rehighlighted.
9217             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9218             (set-buffer gnus-original-article-buffer)
9219             (gnus-request-article
9220              (cdr gnus-article-current)
9221              (car gnus-article-current) (current-buffer))))
9222         ;; Prettify the summary buffer line.
9223         (when (gnus-visual-p 'summary-highlight 'highlight)
9224           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9225
9226 (defun gnus-summary-edit-wash (key)
9227   "Perform editing command KEY in the article buffer."
9228   (interactive
9229    (list
9230     (progn
9231       (message "%s" (concat (this-command-keys) "- "))
9232       (read-char))))
9233   (message "")
9234   (gnus-summary-edit-article)
9235   (execute-kbd-macro (concat (this-command-keys) key))
9236   (gnus-article-edit-done))
9237
9238 ;;; Respooling
9239
9240 (defun gnus-summary-respool-query (&optional silent trace)
9241   "Query where the respool algorithm would put this article."
9242   (interactive)
9243   (let (gnus-mark-article-hook)
9244     (gnus-summary-select-article)
9245     (save-excursion
9246       (set-buffer gnus-original-article-buffer)
9247       (save-restriction
9248         (message-narrow-to-head)
9249         (let ((groups (nnmail-article-group 'identity trace)))
9250           (unless silent
9251             (if groups
9252                 (message "This message would go to %s"
9253                          (mapconcat 'car groups ", "))
9254               (message "This message would go to no groups"))
9255             groups))))))
9256
9257 (defun gnus-summary-respool-trace ()
9258   "Trace where the respool algorithm would put this article.
9259 Display a buffer showing all fancy splitting patterns which matched."
9260   (interactive)
9261   (gnus-summary-respool-query nil t))
9262
9263 ;; Summary marking commands.
9264
9265 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9266   "Mark articles which has the same subject as read, and then select the next.
9267 If UNMARK is positive, remove any kind of mark.
9268 If UNMARK is negative, tick articles."
9269   (interactive "P")
9270   (when unmark
9271     (setq unmark (prefix-numeric-value unmark)))
9272   (let ((count
9273          (gnus-summary-mark-same-subject
9274           (gnus-summary-article-subject) unmark)))
9275     ;; Select next unread article.  If auto-select-same mode, should
9276     ;; select the first unread article.
9277     (gnus-summary-next-article t (and gnus-auto-select-same
9278                                       (gnus-summary-article-subject)))
9279     (gnus-message 7 "%d article%s marked as %s"
9280                   count (if (= count 1) " is" "s are")
9281                   (if unmark "unread" "read"))))
9282
9283 (defun gnus-summary-kill-same-subject (&optional unmark)
9284   "Mark articles which has the same subject as read.
9285 If UNMARK is positive, remove any kind of mark.
9286 If UNMARK is negative, tick articles."
9287   (interactive "P")
9288   (when unmark
9289     (setq unmark (prefix-numeric-value unmark)))
9290   (let ((count
9291          (gnus-summary-mark-same-subject
9292           (gnus-summary-article-subject) unmark)))
9293     ;; If marked as read, go to next unread subject.
9294     (when (null unmark)
9295       ;; Go to next unread subject.
9296       (gnus-summary-next-subject 1 t))
9297     (gnus-message 7 "%d articles are marked as %s"
9298                   count (if unmark "unread" "read"))))
9299
9300 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9301   "Mark articles with same SUBJECT as read, and return marked number.
9302 If optional argument UNMARK is positive, remove any kinds of marks.
9303 If optional argument UNMARK is negative, mark articles as unread instead."
9304   (let ((count 1))
9305     (save-excursion
9306       (cond
9307        ((null unmark)                   ; Mark as read.
9308         (while (and
9309                 (progn
9310                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9311                   (gnus-summary-show-thread) t)
9312                 (gnus-summary-find-subject subject))
9313           (setq count (1+ count))))
9314        ((> unmark 0)                    ; Tick.
9315         (while (and
9316                 (progn
9317                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9318                   (gnus-summary-show-thread) t)
9319                 (gnus-summary-find-subject subject))
9320           (setq count (1+ count))))
9321        (t                               ; Mark as unread.
9322         (while (and
9323                 (progn
9324                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9325                   (gnus-summary-show-thread) t)
9326                 (gnus-summary-find-subject subject))
9327           (setq count (1+ count)))))
9328       (gnus-set-mode-line 'summary)
9329       ;; Return the number of marked articles.
9330       count)))
9331
9332 (defun gnus-summary-mark-as-processable (n &optional unmark)
9333   "Set the process mark on the next N articles.
9334 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9335 the process mark instead.  The difference between N and the actual
9336 number of articles marked is returned."
9337   (interactive "P")
9338   (if (and (null n) (gnus-region-active-p))
9339       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9340     (setq n (prefix-numeric-value n))
9341     (let ((backward (< n 0))
9342           (n (abs n)))
9343       (while (and
9344               (> n 0)
9345               (if unmark
9346                   (gnus-summary-remove-process-mark
9347                    (gnus-summary-article-number))
9348                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9349               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9350         (setq n (1- n)))
9351       (when (/= 0 n)
9352         (gnus-message 7 "No more articles"))
9353       (gnus-summary-recenter)
9354       (gnus-summary-position-point)
9355       n)))
9356
9357 (defun gnus-summary-unmark-as-processable (n)
9358   "Remove the process mark from the next N articles.
9359 If N is negative, unmark backward instead.  The difference between N and
9360 the actual number of articles unmarked is returned."
9361   (interactive "P")
9362   (gnus-summary-mark-as-processable n t))
9363
9364 (defun gnus-summary-unmark-all-processable ()
9365   "Remove the process mark from all articles."
9366   (interactive)
9367   (save-excursion
9368     (while gnus-newsgroup-processable
9369       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9370   (gnus-summary-position-point))
9371
9372 (defun gnus-summary-add-mark (article type)
9373   "Mark ARTICLE with a mark of TYPE."
9374   (let ((vtype (car (assq type gnus-article-mark-lists)))
9375         var)
9376     (if (not vtype)
9377         (error "No such mark type: %s" type)
9378       (setq var (intern (format "gnus-newsgroup-%s" type)))
9379       (set var (cons article (symbol-value var)))
9380       (if (memq type '(processable cached replied forwarded recent saved))
9381           (gnus-summary-update-secondary-mark article)
9382         ;;; !!! This is bogus.  We should find out what primary
9383         ;;; !!! mark we want to set.
9384         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9385
9386 (defun gnus-summary-mark-as-expirable (n)
9387   "Mark N articles forward as expirable.
9388 If N is negative, mark backward instead.  The difference between N and
9389 the actual number of articles marked is returned."
9390   (interactive "p")
9391   (gnus-summary-mark-forward n gnus-expirable-mark))
9392
9393 (defun gnus-summary-mark-as-spam (n)
9394   "Mark N articles forward as spam.
9395 If N is negative, mark backward instead.  The difference between N and
9396 the actual number of articles marked is returned."
9397   (interactive "p")
9398   (gnus-summary-mark-forward n gnus-spam-mark))
9399
9400 (defun gnus-summary-mark-article-as-replied (article)
9401   "Mark ARTICLE as replied to and update the summary line.
9402 ARTICLE can also be a list of articles."
9403   (interactive (list (gnus-summary-article-number)))
9404   (let ((articles (if (listp article) article (list article))))
9405     (dolist (article articles)
9406       (push article gnus-newsgroup-replied)
9407       (let ((buffer-read-only nil))
9408         (when (gnus-summary-goto-subject article nil t)
9409           (gnus-summary-update-secondary-mark article))))))
9410
9411 (defun gnus-summary-mark-article-as-forwarded (article)
9412   "Mark ARTICLE as forwarded and update the summary line.
9413 ARTICLE can also be a list of articles."
9414   (let ((articles (if (listp article) article (list article))))
9415     (dolist (article articles)
9416       (push article gnus-newsgroup-forwarded)
9417       (let ((buffer-read-only nil))
9418         (when (gnus-summary-goto-subject article nil t)
9419           (gnus-summary-update-secondary-mark article))))))
9420
9421 (defun gnus-summary-set-bookmark (article)
9422   "Set a bookmark in current article."
9423   (interactive (list (gnus-summary-article-number)))
9424   (when (or (not (get-buffer gnus-article-buffer))
9425             (not gnus-current-article)
9426             (not gnus-article-current)
9427             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9428     (error "No current article selected"))
9429   ;; Remove old bookmark, if one exists.
9430   (gnus-pull article gnus-newsgroup-bookmarks)
9431   ;; Set the new bookmark, which is on the form
9432   ;; (article-number . line-number-in-body).
9433   (push
9434    (cons article
9435          (save-excursion
9436            (set-buffer gnus-article-buffer)
9437            (count-lines
9438             (min (point)
9439                  (save-excursion
9440                    (article-goto-body)
9441                    (point)))
9442             (point))))
9443    gnus-newsgroup-bookmarks)
9444   (gnus-message 6 "A bookmark has been added to the current article."))
9445
9446 (defun gnus-summary-remove-bookmark (article)
9447   "Remove the bookmark from the current article."
9448   (interactive (list (gnus-summary-article-number)))
9449   ;; Remove old bookmark, if one exists.
9450   (if (not (assq article gnus-newsgroup-bookmarks))
9451       (gnus-message 6 "No bookmark in current article.")
9452     (gnus-pull article gnus-newsgroup-bookmarks)
9453     (gnus-message 6 "Removed bookmark.")))
9454
9455 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9456 (defun gnus-summary-mark-as-dormant (n)
9457   "Mark N articles forward as dormant.
9458 If N is negative, mark backward instead.  The difference between N and
9459 the actual number of articles marked is returned."
9460   (interactive "p")
9461   (gnus-summary-mark-forward n gnus-dormant-mark))
9462
9463 (defun gnus-summary-set-process-mark (article)
9464   "Set the process mark on ARTICLE and update the summary line."
9465   (setq gnus-newsgroup-processable
9466         (cons article
9467               (delq article gnus-newsgroup-processable)))
9468   (when (gnus-summary-goto-subject article)
9469     (gnus-summary-show-thread)
9470     (gnus-summary-goto-subject article)
9471     (gnus-summary-update-secondary-mark article)))
9472
9473 (defun gnus-summary-remove-process-mark (article)
9474   "Remove the process mark from ARTICLE and update the summary line."
9475   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9476   (when (gnus-summary-goto-subject article)
9477     (gnus-summary-show-thread)
9478     (gnus-summary-goto-subject article)
9479     (gnus-summary-update-secondary-mark article)))
9480
9481 (defun gnus-summary-set-saved-mark (article)
9482   "Set the process mark on ARTICLE and update the summary line."
9483   (push article gnus-newsgroup-saved)
9484   (when (gnus-summary-goto-subject article)
9485     (gnus-summary-update-secondary-mark article)))
9486
9487 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9488   "Mark N articles as read forwards.
9489 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9490 The difference between N and the actual number of articles marked is
9491 returned.
9492 If NO-EXPIRE, auto-expiry will be inhibited."
9493   (interactive "p")
9494   (gnus-summary-show-thread)
9495   (let ((backward (< n 0))
9496         (gnus-summary-goto-unread
9497          (and gnus-summary-goto-unread
9498               (not (eq gnus-summary-goto-unread 'never))
9499               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9500                                     gnus-ticked-mark gnus-dormant-mark)))))
9501         (n (abs n))
9502         (mark (or mark gnus-del-mark)))
9503     (while (and (> n 0)
9504                 (gnus-summary-mark-article nil mark no-expire)
9505                 (zerop (gnus-summary-next-subject
9506                         (if backward -1 1)
9507                         (and gnus-summary-goto-unread
9508                              (not (eq gnus-summary-goto-unread 'never)))
9509                         t)))
9510       (setq n (1- n)))
9511     (when (/= 0 n)
9512       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9513     (gnus-summary-recenter)
9514     (gnus-summary-position-point)
9515     (gnus-set-mode-line 'summary)
9516     n))
9517
9518 (defun gnus-summary-mark-article-as-read (mark)
9519   "Mark the current article quickly as read with MARK."
9520   (let ((article (gnus-summary-article-number)))
9521     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9522     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9523     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9524     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9525     (push (cons article mark) gnus-newsgroup-reads)
9526     ;; Possibly remove from cache, if that is used.
9527     (when gnus-use-cache
9528       (gnus-cache-enter-remove-article article))
9529     ;; Allow the backend to change the mark.
9530     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9531     ;; Check for auto-expiry.
9532     (when (and gnus-newsgroup-auto-expire
9533                (memq mark gnus-auto-expirable-marks))
9534       (setq mark gnus-expirable-mark)
9535       ;; Let the backend know about the mark change.
9536       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9537       (push article gnus-newsgroup-expirable))
9538     ;; Set the mark in the buffer.
9539     (gnus-summary-update-mark mark 'unread)
9540     t))
9541
9542 (defun gnus-summary-mark-article-as-unread (mark)
9543   "Mark the current article quickly as unread with MARK."
9544   (let* ((article (gnus-summary-article-number))
9545          (old-mark (gnus-summary-article-mark article)))
9546     ;; Allow the backend to change the mark.
9547     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9548     (if (eq mark old-mark)
9549         t
9550       (if (<= article 0)
9551           (progn
9552             (gnus-error 1 "Can't mark negative article numbers")
9553             nil)
9554         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9555         (setq gnus-newsgroup-spam-marked
9556               (delq article gnus-newsgroup-spam-marked))
9557         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9558         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9559         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9560         (cond ((= mark gnus-ticked-mark)
9561                (setq gnus-newsgroup-marked
9562                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9563                                               article)))
9564               ((= mark gnus-spam-mark)
9565                (setq gnus-newsgroup-spam-marked
9566                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9567                                               article)))
9568               ((= mark gnus-dormant-mark)
9569                (setq gnus-newsgroup-dormant
9570                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9571                                               article)))
9572               (t
9573                (setq gnus-newsgroup-unreads
9574                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9575                                               article))))
9576         (gnus-pull article gnus-newsgroup-reads)
9577
9578         ;; See whether the article is to be put in the cache.
9579         (and gnus-use-cache
9580              (vectorp (gnus-summary-article-header article))
9581              (save-excursion
9582                (gnus-cache-possibly-enter-article
9583                 gnus-newsgroup-name article
9584                 (= mark gnus-ticked-mark)
9585                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9586
9587         ;; Fix the mark.
9588         (gnus-summary-update-mark mark 'unread)
9589         t))))
9590
9591 (defun gnus-summary-mark-article (&optional article mark no-expire)
9592   "Mark ARTICLE with MARK.  MARK can be any character.
9593 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9594 `??' (dormant) and `?E' (expirable).
9595 If MARK is nil, then the default character `?r' is used.
9596 If ARTICLE is nil, then the article on the current line will be
9597 marked.
9598 If NO-EXPIRE, auto-expiry will be inhibited."
9599   ;; The mark might be a string.
9600   (when (stringp mark)
9601     (setq mark (aref mark 0)))
9602   ;; If no mark is given, then we check auto-expiring.
9603   (when (null mark)
9604     (setq mark gnus-del-mark))
9605   (when (and (not no-expire)
9606              gnus-newsgroup-auto-expire
9607              (memq mark gnus-auto-expirable-marks))
9608     (setq mark gnus-expirable-mark))
9609   (let ((article (or article (gnus-summary-article-number)))
9610         (old-mark (gnus-summary-article-mark article)))
9611     ;; Allow the backend to change the mark.
9612     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9613     (if (eq mark old-mark)
9614         t
9615       (unless article
9616         (error "No article on current line"))
9617       (if (not (if (or (= mark gnus-unread-mark)
9618                        (= mark gnus-ticked-mark)
9619                        (= mark gnus-spam-mark)
9620                        (= mark gnus-dormant-mark))
9621                    (gnus-mark-article-as-unread article mark)
9622                  (gnus-mark-article-as-read article mark)))
9623           t
9624         ;; See whether the article is to be put in the cache.
9625         (and gnus-use-cache
9626              (not (= mark gnus-canceled-mark))
9627              (vectorp (gnus-summary-article-header article))
9628              (save-excursion
9629                (gnus-cache-possibly-enter-article
9630                 gnus-newsgroup-name article
9631                 (= mark gnus-ticked-mark)
9632                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9633
9634         (when (gnus-summary-goto-subject article nil t)
9635           (let ((buffer-read-only nil))
9636             (gnus-summary-show-thread)
9637             ;; Fix the mark.
9638             (gnus-summary-update-mark mark 'unread)
9639             t))))))
9640
9641 (defun gnus-summary-update-secondary-mark (article)
9642   "Update the secondary (read, process, cache) mark."
9643   (gnus-summary-update-mark
9644    (cond ((memq article gnus-newsgroup-processable)
9645           gnus-process-mark)
9646          ((memq article gnus-newsgroup-cached)
9647           gnus-cached-mark)
9648          ((memq article gnus-newsgroup-replied)
9649           gnus-replied-mark)
9650          ((memq article gnus-newsgroup-forwarded)
9651           gnus-forwarded-mark)
9652          ((memq article gnus-newsgroup-saved)
9653           gnus-saved-mark)
9654          ((memq article gnus-newsgroup-recent)
9655           gnus-recent-mark)
9656          ((memq article gnus-newsgroup-unseen)
9657           gnus-unseen-mark)
9658          (t gnus-no-mark))
9659    'replied)
9660   (when (gnus-visual-p 'summary-highlight 'highlight)
9661     (gnus-run-hooks 'gnus-summary-update-hook))
9662   t)
9663
9664 (defun gnus-summary-update-download-mark (article)
9665   "Update the secondary (read, process, cache) mark."
9666   (gnus-summary-update-mark
9667    (cond ((memq article gnus-newsgroup-undownloaded) 
9668           gnus-undownloaded-mark)
9669          (gnus-newsgroup-agentized
9670           gnus-downloaded-mark)
9671          (t
9672           gnus-no-mark))
9673    'download)
9674   (gnus-summary-update-line t)
9675   t)
9676
9677 (defun gnus-summary-update-mark (mark type)
9678   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9679         (buffer-read-only nil))
9680     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9681     (when forward
9682       (when (looking-at "\r")
9683         (incf forward))
9684       (when (<= (+ forward (point)) (point-max))
9685         ;; Go to the right position on the line.
9686         (goto-char (+ forward (point)))
9687         ;; Replace the old mark with the new mark.
9688         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9689         ;; Optionally update the marks by some user rule.
9690         (when (eq type 'unread)
9691           (gnus-data-set-mark
9692            (gnus-data-find (gnus-summary-article-number)) mark)
9693           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9694
9695 (defun gnus-mark-article-as-read (article &optional mark)
9696   "Enter ARTICLE in the pertinent lists and remove it from others."
9697   ;; Make the article expirable.
9698   (let ((mark (or mark gnus-del-mark)))
9699     (setq gnus-newsgroup-expirable
9700           (if (= mark gnus-expirable-mark)
9701               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9702             (delq article gnus-newsgroup-expirable)))
9703     ;; Remove from unread and marked lists.
9704     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9705     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9706     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9707     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9708     (push (cons article mark) gnus-newsgroup-reads)
9709     ;; Possibly remove from cache, if that is used.
9710     (when gnus-use-cache
9711       (gnus-cache-enter-remove-article article))
9712     t))
9713
9714 (defun gnus-mark-article-as-unread (article &optional mark)
9715   "Enter ARTICLE in the pertinent lists and remove it from others."
9716   (let ((mark (or mark gnus-ticked-mark)))
9717     (if (<= article 0)
9718         (progn
9719           (gnus-error 1 "Can't mark negative article numbers")
9720           nil)
9721       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9722             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9723             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9724             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9725             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9726
9727       ;; Unsuppress duplicates?
9728       (when gnus-suppress-duplicates
9729         (gnus-dup-unsuppress-article article))
9730
9731       (cond ((= mark gnus-ticked-mark)
9732              (setq gnus-newsgroup-marked
9733                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9734             ((= mark gnus-spam-mark)
9735              (setq gnus-newsgroup-spam-marked
9736                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9737                                             article)))
9738             ((= mark gnus-dormant-mark)
9739              (setq gnus-newsgroup-dormant
9740                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9741             (t
9742              (setq gnus-newsgroup-unreads
9743                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9744       (gnus-pull article gnus-newsgroup-reads)
9745       t)))
9746
9747 (defalias 'gnus-summary-mark-as-unread-forward
9748   'gnus-summary-tick-article-forward)
9749 (make-obsolete 'gnus-summary-mark-as-unread-forward
9750                'gnus-summary-tick-article-forward)
9751 (defun gnus-summary-tick-article-forward (n)
9752   "Tick N articles forwards.
9753 If N is negative, tick backwards instead.
9754 The difference between N and the number of articles ticked is returned."
9755   (interactive "p")
9756   (gnus-summary-mark-forward n gnus-ticked-mark))
9757
9758 (defalias 'gnus-summary-mark-as-unread-backward
9759   'gnus-summary-tick-article-backward)
9760 (make-obsolete 'gnus-summary-mark-as-unread-backward
9761                'gnus-summary-tick-article-backward)
9762 (defun gnus-summary-tick-article-backward (n)
9763   "Tick N articles backwards.
9764 The difference between N and the number of articles ticked is returned."
9765   (interactive "p")
9766   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9767
9768 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9769 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9770 (defun gnus-summary-tick-article (&optional article clear-mark)
9771   "Mark current article as unread.
9772 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9773 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9774   (interactive)
9775   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9776                                        gnus-ticked-mark)))
9777
9778 (defun gnus-summary-mark-as-read-forward (n)
9779   "Mark N articles as read forwards.
9780 If N is negative, mark backwards instead.
9781 The difference between N and the actual number of articles marked is
9782 returned."
9783   (interactive "p")
9784   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9785
9786 (defun gnus-summary-mark-as-read-backward (n)
9787   "Mark the N articles as read backwards.
9788 The difference between N and the actual number of articles marked is
9789 returned."
9790   (interactive "p")
9791   (gnus-summary-mark-forward
9792    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9793
9794 (defun gnus-summary-mark-as-read (&optional article mark)
9795   "Mark current article as read.
9796 ARTICLE specifies the article to be marked as read.
9797 MARK specifies a string to be inserted at the beginning of the line."
9798   (gnus-summary-mark-article article mark))
9799
9800 (defun gnus-summary-clear-mark-forward (n)
9801   "Clear marks from N articles forward.
9802 If N is negative, clear backward instead.
9803 The difference between N and the number of marks cleared is returned."
9804   (interactive "p")
9805   (gnus-summary-mark-forward n gnus-unread-mark))
9806
9807 (defun gnus-summary-clear-mark-backward (n)
9808   "Clear marks from N articles backward.
9809 The difference between N and the number of marks cleared is returned."
9810   (interactive "p")
9811   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9812
9813 (defun gnus-summary-mark-unread-as-read ()
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-read-mark)))
9817
9818 (defun gnus-summary-mark-read-and-unread-as-read ()
9819   "Intended to be used by `gnus-summary-mark-article-hook'."
9820   (let ((mark (gnus-summary-article-mark)))
9821     (when (or (gnus-unread-mark-p mark)
9822               (gnus-read-mark-p mark))
9823       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9824
9825 (defun gnus-summary-mark-unread-as-ticked ()
9826   "Intended to be used by `gnus-summary-mark-article-hook'."
9827   (when (memq gnus-current-article gnus-newsgroup-unreads)
9828     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9829
9830 (defun gnus-summary-mark-region-as-read (point mark all)
9831   "Mark all unread articles between point and mark as read.
9832 If given a prefix, mark all articles between point and mark as read,
9833 even ticked and dormant ones."
9834   (interactive "r\nP")
9835   (save-excursion
9836     (let (article)
9837       (goto-char point)
9838       (beginning-of-line)
9839       (while (and
9840               (< (point) mark)
9841               (progn
9842                 (when (or all
9843                           (memq (setq article (gnus-summary-article-number))
9844                                 gnus-newsgroup-unreads))
9845                   (gnus-summary-mark-article article gnus-del-mark))
9846                 t)
9847               (gnus-summary-find-next))))))
9848
9849 (defun gnus-summary-mark-below (score mark)
9850   "Mark articles with score less than SCORE with MARK."
9851   (interactive "P\ncMark: ")
9852   (setq score (if score
9853                   (prefix-numeric-value score)
9854                 (or gnus-summary-default-score 0)))
9855   (save-excursion
9856     (set-buffer gnus-summary-buffer)
9857     (goto-char (point-min))
9858     (while
9859         (progn
9860           (and (< (gnus-summary-article-score) score)
9861                (gnus-summary-mark-article nil mark))
9862           (gnus-summary-find-next)))))
9863
9864 (defun gnus-summary-kill-below (&optional score)
9865   "Mark articles with score below SCORE as read."
9866   (interactive "P")
9867   (gnus-summary-mark-below score gnus-killed-mark))
9868
9869 (defun gnus-summary-clear-above (&optional score)
9870   "Clear all marks from articles with score above SCORE."
9871   (interactive "P")
9872   (gnus-summary-mark-above score gnus-unread-mark))
9873
9874 (defun gnus-summary-tick-above (&optional score)
9875   "Tick all articles with score above SCORE."
9876   (interactive "P")
9877   (gnus-summary-mark-above score gnus-ticked-mark))
9878
9879 (defun gnus-summary-mark-above (score mark)
9880   "Mark articles with score over SCORE with MARK."
9881   (interactive "P\ncMark: ")
9882   (setq score (if score
9883                   (prefix-numeric-value score)
9884                 (or gnus-summary-default-score 0)))
9885   (save-excursion
9886     (set-buffer gnus-summary-buffer)
9887     (goto-char (point-min))
9888     (while (and (progn
9889                   (when (> (gnus-summary-article-score) score)
9890                     (gnus-summary-mark-article nil mark))
9891                   t)
9892                 (gnus-summary-find-next)))))
9893
9894 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9895 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9896 (defun gnus-summary-limit-include-expunged (&optional no-error)
9897   "Display all the hidden articles that were expunged for low scores."
9898   (interactive)
9899   (let ((buffer-read-only nil))
9900     (let ((scored gnus-newsgroup-scored)
9901           headers h)
9902       (while scored
9903         (unless (gnus-summary-article-header (caar scored))
9904           (and (setq h (gnus-number-to-header (caar scored)))
9905                (< (cdar scored) gnus-summary-expunge-below)
9906                (push h headers)))
9907         (setq scored (cdr scored)))
9908       (if (not headers)
9909           (when (not no-error)
9910             (error "No expunged articles hidden"))
9911         (goto-char (point-min))
9912         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9913         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9914         (mapcar (lambda (x) (push (mail-header-number x)
9915                                   gnus-newsgroup-limit))
9916                 headers)
9917         (gnus-summary-prepare-unthreaded (nreverse headers))
9918         (goto-char (point-min))
9919         (gnus-summary-position-point)
9920         t))))
9921
9922 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9923   "Mark all unread articles in this newsgroup as read.
9924 If prefix argument ALL is non-nil, ticked and dormant articles will
9925 also be marked as read.
9926 If QUIETLY is non-nil, no questions will be asked.
9927 If TO-HERE is non-nil, it should be a point in the buffer.  All
9928 articles before (after, if REVERSE is set) this point will be marked as read.
9929 Note that this function will only catch up the unread article
9930 in the current summary buffer limitation.
9931 The number of articles marked as read is returned."
9932   (interactive "P")
9933   (prog1
9934       (save-excursion
9935         (when (or quietly
9936                   (not gnus-interactive-catchup) ;Without confirmation?
9937                   gnus-expert-user
9938                   (gnus-y-or-n-p
9939                    (if all
9940                        "Mark absolutely all articles as read? "
9941                      "Mark all unread articles as read? ")))
9942           (if (and not-mark
9943                    (not gnus-newsgroup-adaptive)
9944                    (not gnus-newsgroup-auto-expire)
9945                    (not gnus-suppress-duplicates)
9946                    (or (not gnus-use-cache)
9947                        (eq gnus-use-cache 'passive)))
9948               (progn
9949                 (when all
9950                   (setq gnus-newsgroup-marked nil
9951                         gnus-newsgroup-spam-marked nil
9952                         gnus-newsgroup-dormant nil))
9953                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9954             ;; We actually mark all articles as canceled, which we
9955             ;; have to do when using auto-expiry or adaptive scoring.
9956             (gnus-summary-show-all-threads)
9957             (if (and to-here reverse)
9958                 (progn
9959                   (goto-char to-here)
9960                   (while (and
9961                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9962                           (gnus-summary-find-next (not all) nil nil t))))
9963               (when (gnus-summary-first-subject (not all) t)
9964                 (while (and
9965                         (if to-here (< (point) to-here) t)
9966                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9967                         (gnus-summary-find-next (not all) nil nil t)))))
9968             (gnus-set-mode-line 'summary))
9969           t))
9970     (gnus-summary-position-point)))
9971
9972 (defun gnus-summary-catchup-to-here (&optional all)
9973   "Mark all unticked articles before 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-prev))
9981           (gnus-summary-catchup all t beg)))))
9982   (gnus-summary-position-point))
9983
9984 (defun gnus-summary-catchup-from-here (&optional all)
9985   "Mark all unticked articles after the current one as read.
9986 If ALL is non-nil, also mark ticked and dormant articles as read."
9987   (interactive "P")
9988   (save-excursion
9989     (gnus-save-hidden-threads
9990       (let ((beg (point)))
9991         ;; We check that there are unread articles.
9992         (when (or all (gnus-summary-find-next))
9993           (gnus-summary-catchup all t beg nil t)))))
9994
9995   (gnus-summary-position-point))
9996 (defun gnus-summary-catchup-all (&optional quietly)
9997   "Mark all articles in this newsgroup as read.
9998 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9999 instead, which marks only unread articles as read."
10000   (interactive "P")
10001   (gnus-summary-catchup t quietly))
10002
10003 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10004   "Mark all unread articles in this group as read, then exit.
10005 If prefix argument ALL is non-nil, all articles are marked as read.
10006 If QUIETLY is non-nil, no questions will be asked."
10007   (interactive "P")
10008   (when (gnus-summary-catchup all quietly nil 'fast)
10009     ;; Select next newsgroup or exit.
10010     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10011              (eq gnus-auto-select-next 'quietly))
10012         (gnus-summary-next-group nil)
10013       (gnus-summary-exit))))
10014
10015 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10016   "Mark all articles in this newsgroup as read, and then exit.
10017 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10018 instead, which marks only unread articles as read."
10019   (interactive "P")
10020   (gnus-summary-catchup-and-exit t quietly))
10021
10022 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10023   "Mark all articles in this group as read and select the next group.
10024 If given a prefix, mark all articles, unread as well as ticked, as
10025 read."
10026   (interactive "P")
10027   (save-excursion
10028     (gnus-summary-catchup all))
10029   (gnus-summary-next-group))
10030
10031 ;;;
10032 ;;; with article
10033 ;;;
10034
10035 (defmacro gnus-with-article (article &rest forms)
10036   "Select ARTICLE and perform FORMS in the original article buffer.
10037 Then replace the article with the result."
10038   `(progn
10039      ;; We don't want the article to be marked as read.
10040      (let (gnus-mark-article-hook)
10041        (gnus-summary-select-article t t nil ,article))
10042      (set-buffer gnus-original-article-buffer)
10043      ,@forms
10044      (if (not (gnus-check-backend-function
10045                'request-replace-article (car gnus-article-current)))
10046          (gnus-message 5 "Read-only group; not replacing")
10047        (unless (gnus-request-replace-article
10048                 ,article (car gnus-article-current)
10049                 (current-buffer) t)
10050          (error "Couldn't replace article")))
10051      ;; The cache and backlog have to be flushed somewhat.
10052      (when gnus-keep-backlog
10053        (gnus-backlog-remove-article
10054         (car gnus-article-current) (cdr gnus-article-current)))
10055      (when gnus-use-cache
10056        (gnus-cache-update-article
10057         (car gnus-article-current) (cdr gnus-article-current)))))
10058
10059 (put 'gnus-with-article 'lisp-indent-function 1)
10060 (put 'gnus-with-article 'edebug-form-spec '(form body))
10061
10062 ;; Thread-based commands.
10063
10064 (defun gnus-summary-articles-in-thread (&optional article)
10065   "Return a list of all articles in the current thread.
10066 If ARTICLE is non-nil, return all articles in the thread that starts
10067 with that article."
10068   (let* ((article (or article (gnus-summary-article-number)))
10069          (data (gnus-data-find-list article))
10070          (top-level (gnus-data-level (car data)))
10071          (top-subject
10072           (cond ((null gnus-thread-operation-ignore-subject)
10073                  (gnus-simplify-subject-re
10074                   (mail-header-subject (gnus-data-header (car data)))))
10075                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10076                  (gnus-simplify-subject-fuzzy
10077                   (mail-header-subject (gnus-data-header (car data)))))
10078                 (t nil)))
10079          (end-point (save-excursion
10080                       (if (gnus-summary-go-to-next-thread)
10081                           (point) (point-max))))
10082          articles)
10083     (while (and data
10084                 (< (gnus-data-pos (car data)) end-point))
10085       (when (or (not top-subject)
10086                 (string= top-subject
10087                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10088                              (gnus-simplify-subject-fuzzy
10089                               (mail-header-subject
10090                                (gnus-data-header (car data))))
10091                            (gnus-simplify-subject-re
10092                             (mail-header-subject
10093                              (gnus-data-header (car data)))))))
10094         (push (gnus-data-number (car data)) articles))
10095       (unless (and (setq data (cdr data))
10096                    (> (gnus-data-level (car data)) top-level))
10097         (setq data nil)))
10098     ;; Return the list of articles.
10099     (nreverse articles)))
10100
10101 (defun gnus-summary-rethread-current ()
10102   "Rethread the thread the current article is part of."
10103   (interactive)
10104   (let* ((gnus-show-threads t)
10105          (article (gnus-summary-article-number))
10106          (id (mail-header-id (gnus-summary-article-header)))
10107          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10108     (unless id
10109       (error "No article on the current line"))
10110     (gnus-rebuild-thread id)
10111     (gnus-summary-goto-subject article)))
10112
10113 (defun gnus-summary-reparent-thread ()
10114   "Make the current article child of the marked (or previous) article.
10115
10116 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10117 is non-nil or the Subject: of both articles are the same."
10118   (interactive)
10119   (unless (not (gnus-group-read-only-p))
10120     (error "The current newsgroup does not support article editing"))
10121   (unless (<= (length gnus-newsgroup-processable) 1)
10122     (error "No more than one article may be marked"))
10123   (save-window-excursion
10124     (let ((gnus-article-buffer " *reparent*")
10125           (current-article (gnus-summary-article-number))
10126           ;; First grab the marked article, otherwise one line up.
10127           (parent-article (if (not (null gnus-newsgroup-processable))
10128                               (car gnus-newsgroup-processable)
10129                             (save-excursion
10130                               (if (eq (forward-line -1) 0)
10131                                   (gnus-summary-article-number)
10132                                 (error "Beginning of summary buffer"))))))
10133       (unless (not (eq current-article parent-article))
10134         (error "An article may not be self-referential"))
10135       (let ((message-id (mail-header-id
10136                          (gnus-summary-article-header parent-article))))
10137         (unless (and message-id (not (equal message-id "")))
10138           (error "No message-id in desired parent"))
10139         (gnus-with-article current-article
10140           (save-restriction
10141             (goto-char (point-min))
10142             (message-narrow-to-head)
10143             (if (re-search-forward "^References: " nil t)
10144                 (progn
10145                   (re-search-forward "^[^ \t]" nil t)
10146                   (forward-line -1)
10147                   (end-of-line)
10148                   (insert " " message-id))
10149               (insert "References: " message-id "\n"))))
10150         (set-buffer gnus-summary-buffer)
10151         (gnus-summary-unmark-all-processable)
10152         (gnus-summary-update-article current-article)
10153         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10154             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10155         (gnus-summary-rethread-current)
10156         (gnus-message 3 "Article %d is now the child of article %d"
10157                       current-article parent-article)))))
10158
10159 (defun gnus-summary-toggle-threads (&optional arg)
10160   "Toggle showing conversation threads.
10161 If ARG is positive number, turn showing conversation threads on."
10162   (interactive "P")
10163   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10164     (setq gnus-show-threads
10165           (if (null arg) (not gnus-show-threads)
10166             (> (prefix-numeric-value arg) 0)))
10167     (gnus-summary-prepare)
10168     (gnus-summary-goto-subject current)
10169     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10170     (gnus-summary-position-point)))
10171
10172 (defun gnus-summary-show-all-threads ()
10173   "Show all threads."
10174   (interactive)
10175   (save-excursion
10176     (let ((buffer-read-only nil))
10177       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10178   (gnus-summary-position-point))
10179
10180 (defun gnus-summary-show-thread ()
10181   "Show thread subtrees.
10182 Returns nil if no thread was there to be shown."
10183   (interactive)
10184   (let ((buffer-read-only nil)
10185         (orig (point))
10186         ;; first goto end then to beg, to have point at beg after let
10187         (end (progn (end-of-line) (point)))
10188         (beg (progn (beginning-of-line) (point))))
10189     (prog1
10190         ;; Any hidden lines here?
10191         (search-forward "\r" end t)
10192       (subst-char-in-region beg end ?\^M ?\n t)
10193       (goto-char orig)
10194       (gnus-summary-position-point))))
10195
10196 (defun gnus-summary-maybe-hide-threads ()
10197   "If requested, hide the threads that should be hidden."
10198   (when (and gnus-show-threads
10199              gnus-thread-hide-subtree)
10200     (gnus-summary-hide-all-threads
10201      (if (or (consp gnus-thread-hide-subtree)
10202              (gnus-functionp gnus-thread-hide-subtree))
10203          (gnus-make-predicate gnus-thread-hide-subtree)
10204        nil))))
10205
10206 ;;; Hiding predicates.
10207
10208 (defun gnus-article-unread-p (header)
10209   (memq (mail-header-number header) gnus-newsgroup-unreads))
10210
10211 (defun gnus-article-unseen-p (header)
10212   (memq (mail-header-number header) gnus-newsgroup-unseen))
10213
10214 (defun gnus-map-articles (predicate articles)
10215   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10216   (apply 'gnus-or (mapcar predicate
10217                           (mapcar 'gnus-summary-article-header articles))))
10218
10219 (defun gnus-summary-hide-all-threads (&optional predicate)
10220   "Hide all thread subtrees.
10221 If PREDICATE is supplied, threads that satisfy this predicate
10222 will not be hidden."
10223   (interactive)
10224   (save-excursion
10225     (goto-char (point-min))
10226     (let ((end nil))
10227       (while (not end)
10228         (when (or (not predicate)
10229                   (gnus-map-articles
10230                    predicate (gnus-summary-article-children)))
10231             (gnus-summary-hide-thread))
10232         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10233   (gnus-summary-position-point))
10234
10235 (defun gnus-summary-hide-thread ()
10236   "Hide thread subtrees.
10237 If PREDICATE is supplied, threads that satisfy this predicate
10238 will not be hidden.
10239 Returns nil if no threads were there to be hidden."
10240   (interactive)
10241   (let ((buffer-read-only nil)
10242         (start (point))
10243         (article (gnus-summary-article-number)))
10244     (goto-char start)
10245     ;; Go forward until either the buffer ends or the subthread
10246     ;; ends.
10247     (when (and (not (eobp))
10248                (or (zerop (gnus-summary-next-thread 1 t))
10249                    (goto-char (point-max))))
10250       (prog1
10251           (if (and (> (point) start)
10252                    (search-backward "\n" start t))
10253               (progn
10254                 (subst-char-in-region start (point) ?\n ?\^M)
10255                 (gnus-summary-goto-subject article))
10256             (goto-char start)
10257             nil)))))
10258
10259 (defun gnus-summary-go-to-next-thread (&optional previous)
10260   "Go to the same level (or less) next thread.
10261 If PREVIOUS is non-nil, go to previous thread instead.
10262 Return the article number moved to, or nil if moving was impossible."
10263   (let ((level (gnus-summary-thread-level))
10264         (way (if previous -1 1))
10265         (beg (point)))
10266     (forward-line way)
10267     (while (and (not (eobp))
10268                 (< level (gnus-summary-thread-level)))
10269       (forward-line way))
10270     (if (eobp)
10271         (progn
10272           (goto-char beg)
10273           nil)
10274       (setq beg (point))
10275       (prog1
10276           (gnus-summary-article-number)
10277         (goto-char beg)))))
10278
10279 (defun gnus-summary-next-thread (n &optional silent)
10280   "Go to the same level next N'th thread.
10281 If N is negative, search backward instead.
10282 Returns the difference between N and the number of skips actually
10283 done.
10284
10285 If SILENT, don't output messages."
10286   (interactive "p")
10287   (let ((backward (< n 0))
10288         (n (abs n)))
10289     (while (and (> n 0)
10290                 (gnus-summary-go-to-next-thread backward))
10291       (decf n))
10292     (unless silent
10293       (gnus-summary-position-point))
10294     (when (and (not silent) (/= 0 n))
10295       (gnus-message 7 "No more threads"))
10296     n))
10297
10298 (defun gnus-summary-prev-thread (n)
10299   "Go to the same level previous N'th thread.
10300 Returns the difference between N and the number of skips actually
10301 done."
10302   (interactive "p")
10303   (gnus-summary-next-thread (- n)))
10304
10305 (defun gnus-summary-go-down-thread ()
10306   "Go down one level in the current thread."
10307   (let ((children (gnus-summary-article-children)))
10308     (when children
10309       (gnus-summary-goto-subject (car children)))))
10310
10311 (defun gnus-summary-go-up-thread ()
10312   "Go up one level in the current thread."
10313   (let ((parent (gnus-summary-article-parent)))
10314     (when parent
10315       (gnus-summary-goto-subject parent))))
10316
10317 (defun gnus-summary-down-thread (n)
10318   "Go down thread N steps.
10319 If N is negative, go up instead.
10320 Returns the difference between N and how many steps down that were
10321 taken."
10322   (interactive "p")
10323   (let ((up (< n 0))
10324         (n (abs n)))
10325     (while (and (> n 0)
10326                 (if up (gnus-summary-go-up-thread)
10327                   (gnus-summary-go-down-thread)))
10328       (setq n (1- n)))
10329     (gnus-summary-position-point)
10330     (when (/= 0 n)
10331       (gnus-message 7 "Can't go further"))
10332     n))
10333
10334 (defun gnus-summary-up-thread (n)
10335   "Go up thread N steps.
10336 If N is negative, go down instead.
10337 Returns the difference between N and how many steps down that were
10338 taken."
10339   (interactive "p")
10340   (gnus-summary-down-thread (- n)))
10341
10342 (defun gnus-summary-top-thread ()
10343   "Go to the top of the thread."
10344   (interactive)
10345   (while (gnus-summary-go-up-thread))
10346   (gnus-summary-article-number))
10347
10348 (defun gnus-summary-kill-thread (&optional unmark)
10349   "Mark articles under current thread as read.
10350 If the prefix argument is positive, remove any kinds of marks.
10351 If the prefix argument is negative, tick articles instead."
10352   (interactive "P")
10353   (when unmark
10354     (setq unmark (prefix-numeric-value unmark)))
10355   (let ((articles (gnus-summary-articles-in-thread)))
10356     (save-excursion
10357       ;; Expand the thread.
10358       (gnus-summary-show-thread)
10359       ;; Mark all the articles.
10360       (while articles
10361         (gnus-summary-goto-subject (car articles))
10362         (cond ((null unmark)
10363                (gnus-summary-mark-article-as-read gnus-killed-mark))
10364               ((> unmark 0)
10365                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10366               (t
10367                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10368         (setq articles (cdr articles))))
10369     ;; Hide killed subtrees.
10370     (and (null unmark)
10371          gnus-thread-hide-killed
10372          (gnus-summary-hide-thread))
10373     ;; If marked as read, go to next unread subject.
10374     (when (null unmark)
10375       ;; Go to next unread subject.
10376       (gnus-summary-next-subject 1 t)))
10377   (gnus-set-mode-line 'summary))
10378
10379 ;; Summary sorting commands
10380
10381 (defun gnus-summary-sort-by-number (&optional reverse)
10382   "Sort the summary buffer by article number.
10383 Argument REVERSE means reverse order."
10384   (interactive "P")
10385   (gnus-summary-sort 'number reverse))
10386
10387 (defun gnus-summary-sort-by-random (&optional reverse)
10388   "Randomize the order in the summary buffer.
10389 Argument REVERSE means to randomize in reverse order."
10390   (interactive "P")
10391   (gnus-summary-sort 'random reverse))
10392
10393 (defun gnus-summary-sort-by-author (&optional reverse)
10394   "Sort the summary buffer by author name alphabetically.
10395 If `case-fold-search' is non-nil, case of letters is ignored.
10396 Argument REVERSE means reverse order."
10397   (interactive "P")
10398   (gnus-summary-sort 'author reverse))
10399
10400 (defun gnus-summary-sort-by-subject (&optional reverse)
10401   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10402 If `case-fold-search' is non-nil, case of letters is ignored.
10403 Argument REVERSE means reverse order."
10404   (interactive "P")
10405   (gnus-summary-sort 'subject reverse))
10406
10407 (defun gnus-summary-sort-by-date (&optional reverse)
10408   "Sort the summary buffer by date.
10409 Argument REVERSE means reverse order."
10410   (interactive "P")
10411   (gnus-summary-sort 'date reverse))
10412
10413 (defun gnus-summary-sort-by-score (&optional reverse)
10414   "Sort the summary buffer by score.
10415 Argument REVERSE means reverse order."
10416   (interactive "P")
10417   (gnus-summary-sort 'score reverse))
10418
10419 (defun gnus-summary-sort-by-lines (&optional reverse)
10420   "Sort the summary buffer by the number of lines.
10421 Argument REVERSE means reverse order."
10422   (interactive "P")
10423   (gnus-summary-sort 'lines reverse))
10424
10425 (defun gnus-summary-sort-by-chars (&optional reverse)
10426   "Sort the summary buffer by article length.
10427 Argument REVERSE means reverse order."
10428   (interactive "P")
10429   (gnus-summary-sort 'chars reverse))
10430
10431 (defun gnus-summary-sort-by-original (&optional reverse)
10432   "Sort the summary buffer using the default sorting method.
10433 Argument REVERSE means reverse order."
10434   (interactive "P")
10435   (let* ((buffer-read-only)
10436          (gnus-summary-prepare-hook nil))
10437     ;; We do the sorting by regenerating the threads.
10438     (gnus-summary-prepare)
10439     ;; Hide subthreads if needed.
10440     (gnus-summary-maybe-hide-threads)))
10441
10442 (defun gnus-summary-sort (predicate reverse)
10443   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10444   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10445          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10446          (gnus-thread-sort-functions
10447           (if (not reverse)
10448               thread
10449             `(lambda (t1 t2)
10450                (,thread t2 t1))))
10451          (gnus-sort-gathered-threads-function
10452           gnus-thread-sort-functions)
10453          (gnus-article-sort-functions
10454           (if (not reverse)
10455               article
10456             `(lambda (t1 t2)
10457                (,article t2 t1))))
10458          (buffer-read-only)
10459          (gnus-summary-prepare-hook nil))
10460     ;; We do the sorting by regenerating the threads.
10461     (gnus-summary-prepare)
10462     ;; Hide subthreads if needed.
10463     (gnus-summary-maybe-hide-threads)))
10464
10465 ;; Summary saving commands.
10466
10467 (defun gnus-summary-save-article (&optional n not-saved)
10468   "Save the current article using the default saver function.
10469 If N is a positive number, save the N next articles.
10470 If N is a negative number, save the N previous articles.
10471 If N is nil and any articles have been marked with the process mark,
10472 save those articles instead.
10473 The variable `gnus-default-article-saver' specifies the saver function."
10474   (interactive "P")
10475   (let* ((articles (gnus-summary-work-articles n))
10476          (save-buffer (save-excursion
10477                         (nnheader-set-temp-buffer " *Gnus Save*")))
10478          (num (length articles))
10479          header file)
10480     (dolist (article articles)
10481       (setq header (gnus-summary-article-header article))
10482       (if (not (vectorp header))
10483           ;; This is a pseudo-article.
10484           (if (assq 'name header)
10485               (gnus-copy-file (cdr (assq 'name header)))
10486             (gnus-message 1 "Article %d is unsaveable" article))
10487         ;; This is a real article.
10488         (save-window-excursion
10489           (let ((gnus-display-mime-function nil)
10490                 (gnus-article-prepare-hook nil))
10491             (gnus-summary-select-article t nil nil article)))
10492         (save-excursion
10493           (set-buffer save-buffer)
10494           (erase-buffer)
10495           (insert-buffer-substring gnus-original-article-buffer))
10496         (setq file (gnus-article-save save-buffer file num))
10497         (gnus-summary-remove-process-mark article)
10498         (unless not-saved
10499           (gnus-summary-set-saved-mark article))))
10500     (gnus-kill-buffer save-buffer)
10501     (gnus-summary-position-point)
10502     (gnus-set-mode-line 'summary)
10503     n))
10504
10505 (defun gnus-summary-pipe-output (&optional arg headers)
10506   "Pipe the current article to a subprocess.
10507 If N is a positive number, pipe the N next articles.
10508 If N is a negative number, pipe the N previous articles.
10509 If N is nil and any articles have been marked with the process mark,
10510 pipe those articles instead.
10511 If HEADERS (the symbolic prefix), include the headers, too."
10512   (interactive (gnus-interactive "P\ny"))
10513   (require 'gnus-art)
10514   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10515         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10516     (gnus-summary-save-article arg t))
10517   (let ((buffer (get-buffer "*Shell Command Output*")))
10518     (when (and buffer
10519                (not (zerop (buffer-size buffer))))
10520       (gnus-configure-windows 'pipe))))
10521
10522 (defun gnus-summary-save-article-mail (&optional arg)
10523   "Append the current article to an mail file.
10524 If N is a positive number, save the N next articles.
10525 If N is a negative number, save the N previous articles.
10526 If N is nil and any articles have been marked with the process mark,
10527 save those articles instead."
10528   (interactive "P")
10529   (require 'gnus-art)
10530   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10531     (gnus-summary-save-article arg)))
10532
10533 (defun gnus-summary-save-article-rmail (&optional arg)
10534   "Append the current article to an rmail file.
10535 If N is a positive number, save the N next articles.
10536 If N is a negative number, save the N previous articles.
10537 If N is nil and any articles have been marked with the process mark,
10538 save those articles instead."
10539   (interactive "P")
10540   (require 'gnus-art)
10541   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10542     (gnus-summary-save-article arg)))
10543
10544 (defun gnus-summary-save-article-file (&optional arg)
10545   "Append the current article to a file.
10546 If N is a positive number, save the N next articles.
10547 If N is a negative number, save the N previous articles.
10548 If N is nil and any articles have been marked with the process mark,
10549 save those articles instead."
10550   (interactive "P")
10551   (require 'gnus-art)
10552   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10553     (gnus-summary-save-article arg)))
10554
10555 (defun gnus-summary-write-article-file (&optional arg)
10556   "Write the current article to a file, deleting the previous file.
10557 If N is a positive number, save the N next articles.
10558 If N is a negative number, save the N previous articles.
10559 If N is nil and any articles have been marked with the process mark,
10560 save those articles instead."
10561   (interactive "P")
10562   (require 'gnus-art)
10563   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10564     (gnus-summary-save-article arg)))
10565
10566 (defun gnus-summary-save-article-body-file (&optional arg)
10567   "Append the current article body to a file.
10568 If N is a positive number, save the N next articles.
10569 If N is a negative number, save the N previous articles.
10570 If N is nil and any articles have been marked with the process mark,
10571 save those articles instead."
10572   (interactive "P")
10573   (require 'gnus-art)
10574   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10575     (gnus-summary-save-article arg)))
10576
10577 (defun gnus-summary-muttprint (&optional arg)
10578   "Print the current article using Muttprint.
10579 If N is a positive number, save the N next articles.
10580 If N is a negative number, save the N previous articles.
10581 If N is nil and any articles have been marked with the process mark,
10582 save those articles instead."
10583   (interactive "P")
10584   (require 'gnus-art)
10585   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10586     (gnus-summary-save-article arg t)))
10587
10588 (defun gnus-summary-pipe-message (program)
10589   "Pipe the current article through PROGRAM."
10590   (interactive "sProgram: ")
10591   (gnus-summary-select-article)
10592   (let ((mail-header-separator ""))
10593     (gnus-eval-in-buffer-window gnus-article-buffer
10594       (save-restriction
10595         (widen)
10596         (let ((start (window-start))
10597               buffer-read-only)
10598           (message-pipe-buffer-body program)
10599           (set-window-start (get-buffer-window (current-buffer)) start))))))
10600
10601 (defun gnus-get-split-value (methods)
10602   "Return a value based on the split METHODS."
10603   (let (split-name method result match)
10604     (when methods
10605       (save-excursion
10606         (set-buffer gnus-original-article-buffer)
10607         (save-restriction
10608           (nnheader-narrow-to-headers)
10609           (while (and methods (not split-name))
10610             (goto-char (point-min))
10611             (setq method (pop methods))
10612             (setq match (car method))
10613             (when (cond
10614                    ((stringp match)
10615                     ;; Regular expression.
10616                     (ignore-errors
10617                       (re-search-forward match nil t)))
10618                    ((gnus-functionp match)
10619                     ;; Function.
10620                     (save-restriction
10621                       (widen)
10622                       (setq result (funcall match gnus-newsgroup-name))))
10623                    ((consp match)
10624                     ;; Form.
10625                     (save-restriction
10626                       (widen)
10627                       (setq result (eval match)))))
10628               (setq split-name (cdr method))
10629               (cond ((stringp result)
10630                      (push (expand-file-name
10631                             result gnus-article-save-directory)
10632                            split-name))
10633                     ((consp result)
10634                      (setq split-name (append result split-name)))))))))
10635     (nreverse split-name)))
10636
10637 (defun gnus-valid-move-group-p (group)
10638   (and (boundp group)
10639        (symbol-name group)
10640        (symbol-value group)
10641        (gnus-get-function (gnus-find-method-for-group
10642                            (symbol-name group)) 'request-accept-article t)))
10643
10644 (defun gnus-read-move-group-name (prompt default articles prefix)
10645   "Read a group name."
10646   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10647          (minibuffer-confirm-incomplete nil) ; XEmacs
10648          (prom
10649           (format "%s %s to:"
10650                   prompt
10651                   (if (> (length articles) 1)
10652                       (format "these %d articles" (length articles))
10653                     "this article")))
10654          (to-newsgroup
10655           (cond
10656            ((null split-name)
10657             (gnus-completing-read-with-default
10658              default prom
10659              gnus-active-hashtb
10660              'gnus-valid-move-group-p
10661              nil prefix
10662              'gnus-group-history))
10663            ((= 1 (length split-name))
10664             (gnus-completing-read-with-default
10665              (car split-name) prom
10666              gnus-active-hashtb
10667              'gnus-valid-move-group-p
10668              nil nil
10669              'gnus-group-history))
10670            (t
10671             (gnus-completing-read-with-default
10672              nil prom
10673              (mapcar (lambda (el) (list el))
10674                      (nreverse split-name))
10675              nil nil nil
10676              'gnus-group-history))))
10677          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10678     (when to-newsgroup
10679       (if (or (string= to-newsgroup "")
10680               (string= to-newsgroup prefix))
10681           (setq to-newsgroup default))
10682       (unless to-newsgroup
10683         (error "No group name entered"))
10684       (or (gnus-active to-newsgroup)
10685           (gnus-activate-group to-newsgroup nil nil to-method)
10686           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10687                                      to-newsgroup))
10688               (or (and (gnus-request-create-group to-newsgroup to-method)
10689                        (gnus-activate-group
10690                         to-newsgroup nil nil to-method)
10691                        (gnus-subscribe-group to-newsgroup))
10692                   (error "Couldn't create group %s" to-newsgroup)))
10693           (error "No such group: %s" to-newsgroup)))
10694     to-newsgroup))
10695
10696 (defun gnus-summary-save-parts (type dir n &optional reverse)
10697   "Save parts matching TYPE to DIR.
10698 If REVERSE, save parts that do not match TYPE."
10699   (interactive
10700    (list (read-string "Save parts of type: "
10701                       (or (car gnus-summary-save-parts-type-history)
10702                           gnus-summary-save-parts-default-mime)
10703                       'gnus-summary-save-parts-type-history)
10704          (setq gnus-summary-save-parts-last-directory
10705                (read-file-name "Save to directory: "
10706                                gnus-summary-save-parts-last-directory
10707                                nil t))
10708          current-prefix-arg))
10709   (gnus-summary-iterate n
10710     (let ((gnus-display-mime-function nil)
10711           (gnus-inhibit-treatment t))
10712       (gnus-summary-select-article))
10713     (save-excursion
10714       (set-buffer gnus-article-buffer)
10715       (let ((handles (or gnus-article-mime-handles
10716                          (mm-dissect-buffer nil gnus-article-loose-mime)
10717                          (mm-uu-dissect))))
10718         (when handles
10719           (gnus-summary-save-parts-1 type dir handles reverse)
10720           (unless gnus-article-mime-handles ;; Don't destroy this case.
10721             (mm-destroy-parts handles)))))))
10722
10723 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10724   (if (stringp (car handle))
10725       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10726               (cdr handle))
10727     (when (if reverse
10728               (not (string-match type (mm-handle-media-type handle)))
10729             (string-match type (mm-handle-media-type handle)))
10730       (let ((file (expand-file-name
10731                    (file-name-nondirectory
10732                     (or
10733                      (mail-content-type-get
10734                       (mm-handle-disposition handle) 'filename)
10735                      (concat gnus-newsgroup-name
10736                              "." (number-to-string
10737                                   (cdr gnus-article-current)))))
10738                    dir)))
10739         (unless (file-exists-p file)
10740           (mm-save-part-to-file handle file))))))
10741
10742 ;; Summary extract commands
10743
10744 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10745   (let ((buffer-read-only nil)
10746         (article (gnus-summary-article-number))
10747         after-article b e)
10748     (unless (gnus-summary-goto-subject article)
10749       (error "No such article: %d" article))
10750     (gnus-summary-position-point)
10751     ;; If all commands are to be bunched up on one line, we collect
10752     ;; them here.
10753     (unless gnus-view-pseudos-separately
10754       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10755             files action)
10756         (while ps
10757           (setq action (cdr (assq 'action (car ps))))
10758           (setq files (list (cdr (assq 'name (car ps)))))
10759           (while (and ps (cdr ps)
10760                       (string= (or action "1")
10761                                (or (cdr (assq 'action (cadr ps))) "2")))
10762             (push (cdr (assq 'name (cadr ps))) files)
10763             (setcdr ps (cddr ps)))
10764           (when files
10765             (when (not (string-match "%s" action))
10766               (push " " files))
10767             (push " " files)
10768             (when (assq 'execute (car ps))
10769               (setcdr (assq 'execute (car ps))
10770                       (funcall (if (string-match "%s" action)
10771                                    'format 'concat)
10772                                action
10773                                (mapconcat
10774                                 (lambda (f)
10775                                   (if (equal f " ")
10776                                       f
10777                                     (mm-quote-arg f)))
10778                                 files " ")))))
10779           (setq ps (cdr ps)))))
10780     (if (and gnus-view-pseudos (not not-view))
10781         (while pslist
10782           (when (assq 'execute (car pslist))
10783             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10784                                   (eq gnus-view-pseudos 'not-confirm)))
10785           (setq pslist (cdr pslist)))
10786       (save-excursion
10787         (while pslist
10788           (setq after-article (or (cdr (assq 'article (car pslist)))
10789                                   (gnus-summary-article-number)))
10790           (gnus-summary-goto-subject after-article)
10791           (forward-line 1)
10792           (setq b (point))
10793           (insert "    " (file-name-nondirectory
10794                           (cdr (assq 'name (car pslist))))
10795                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10796           (setq e (point))
10797           (forward-line -1)             ; back to `b'
10798           (gnus-add-text-properties
10799            b (1- e) (list 'gnus-number gnus-reffed-article-number
10800                           gnus-mouse-face-prop gnus-mouse-face))
10801           (gnus-data-enter
10802            after-article gnus-reffed-article-number
10803            gnus-unread-mark b (car pslist) 0 (- e b))
10804           (setq gnus-newsgroup-unreads
10805                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10806                                          gnus-reffed-article-number))
10807           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10808           (setq pslist (cdr pslist)))))))
10809
10810 (defun gnus-pseudos< (p1 p2)
10811   (let ((c1 (cdr (assq 'action p1)))
10812         (c2 (cdr (assq 'action p2))))
10813     (and c1 c2 (string< c1 c2))))
10814
10815 (defun gnus-request-pseudo-article (props)
10816   (cond ((assq 'execute props)
10817          (gnus-execute-command (cdr (assq 'execute props)))))
10818   (let ((gnus-current-article (gnus-summary-article-number)))
10819     (gnus-run-hooks 'gnus-mark-article-hook)))
10820
10821 (defun gnus-execute-command (command &optional automatic)
10822   (save-excursion
10823     (gnus-article-setup-buffer)
10824     (set-buffer gnus-article-buffer)
10825     (setq buffer-read-only nil)
10826     (let ((command (if automatic command
10827                      (read-string "Command: " (cons command 0)))))
10828       (erase-buffer)
10829       (insert "$ " command "\n\n")
10830       (if gnus-view-pseudo-asynchronously
10831           (start-process "gnus-execute" (current-buffer) shell-file-name
10832                          shell-command-switch command)
10833         (call-process shell-file-name nil t nil
10834                       shell-command-switch command)))))
10835
10836 ;; Summary kill commands.
10837
10838 (defun gnus-summary-edit-global-kill (article)
10839   "Edit the \"global\" kill file."
10840   (interactive (list (gnus-summary-article-number)))
10841   (gnus-group-edit-global-kill article))
10842
10843 (defun gnus-summary-edit-local-kill ()
10844   "Edit a local kill file applied to the current newsgroup."
10845   (interactive)
10846   (setq gnus-current-headers (gnus-summary-article-header))
10847   (gnus-group-edit-local-kill
10848    (gnus-summary-article-number) gnus-newsgroup-name))
10849
10850 ;;; Header reading.
10851
10852 (defun gnus-read-header (id &optional header)
10853   "Read the headers of article ID and enter them into the Gnus system."
10854   (let ((group gnus-newsgroup-name)
10855         (gnus-override-method
10856          (or
10857           gnus-override-method
10858           (and (gnus-news-group-p gnus-newsgroup-name)
10859                (car (gnus-refer-article-methods)))))
10860         where)
10861     ;; First we check to see whether the header in question is already
10862     ;; fetched.
10863     (if (stringp id)
10864         ;; This is a Message-ID.
10865         (setq header (or header (gnus-id-to-header id)))
10866       ;; This is an article number.
10867       (setq header (or header (gnus-summary-article-header id))))
10868     (if (and header
10869              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10870         ;; We have found the header.
10871         header
10872       ;; If this is a sparse article, we have to nix out its
10873       ;; previous entry in the thread hashtb.
10874       (when (and header
10875                  (gnus-summary-article-sparse-p (mail-header-number header)))
10876         (let* ((parent (gnus-parent-id (mail-header-references header)))
10877                (thread (and parent (gnus-id-to-thread parent))))
10878           (when thread
10879             (delq (assq header thread) thread))))
10880       ;; We have to really fetch the header to this article.
10881       (save-excursion
10882         (set-buffer nntp-server-buffer)
10883         (when (setq where (gnus-request-head id group))
10884           (nnheader-fold-continuation-lines)
10885           (goto-char (point-max))
10886           (insert ".\n")
10887           (goto-char (point-min))
10888           (insert "211 ")
10889           (princ (cond
10890                   ((numberp id) id)
10891                   ((cdr where) (cdr where))
10892                   (header (mail-header-number header))
10893                   (t gnus-reffed-article-number))
10894                  (current-buffer))
10895           (insert " Article retrieved.\n"))
10896         (if (or (not where)
10897                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10898             ()                          ; Malformed head.
10899           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10900             (when (and (stringp id)
10901                        (not (string= (gnus-group-real-name group)
10902                                      (car where))))
10903               ;; If we fetched by Message-ID and the article came
10904               ;; from a different group, we fudge some bogus article
10905               ;; numbers for this article.
10906               (mail-header-set-number header gnus-reffed-article-number))
10907             (save-excursion
10908               (set-buffer gnus-summary-buffer)
10909               (decf gnus-reffed-article-number)
10910               (gnus-remove-header (mail-header-number header))
10911               (push header gnus-newsgroup-headers)
10912               (setq gnus-current-headers header)
10913               (push (mail-header-number header) gnus-newsgroup-limit)))
10914           header)))))
10915
10916 (defun gnus-remove-header (number)
10917   "Remove header NUMBER from `gnus-newsgroup-headers'."
10918   (if (and gnus-newsgroup-headers
10919            (= number (mail-header-number (car gnus-newsgroup-headers))))
10920       (pop gnus-newsgroup-headers)
10921     (let ((headers gnus-newsgroup-headers))
10922       (while (and (cdr headers)
10923                   (not (= number (mail-header-number (cadr headers)))))
10924         (pop headers))
10925       (when (cdr headers)
10926         (setcdr headers (cddr headers))))))
10927
10928 ;;;
10929 ;;; summary highlights
10930 ;;;
10931
10932 (defun gnus-highlight-selected-summary ()
10933   "Highlight selected article in summary buffer."
10934   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10935   (when gnus-summary-selected-face
10936     (save-excursion
10937       (let* ((beg (progn (beginning-of-line) (point)))
10938              (end (progn (end-of-line) (point)))
10939              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10940              (from (if (get-text-property beg gnus-mouse-face-prop)
10941                        beg
10942                      (or (next-single-property-change
10943                           beg gnus-mouse-face-prop nil end)
10944                          beg)))
10945              (to
10946               (if (= from end)
10947                   (- from 2)
10948                 (or (next-single-property-change
10949                      from gnus-mouse-face-prop nil end)
10950                     end))))
10951         ;; If no mouse-face prop on line we will have to = from = end,
10952         ;; so we highlight the entire line instead.
10953         (when (= (+ to 2) from)
10954           (setq from beg)
10955           (setq to end))
10956         (if gnus-newsgroup-selected-overlay
10957             ;; Move old overlay.
10958             (gnus-move-overlay
10959              gnus-newsgroup-selected-overlay from to (current-buffer))
10960           ;; Create new overlay.
10961           (gnus-overlay-put
10962            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10963            'face gnus-summary-selected-face))))))
10964
10965 (defvar gnus-summary-highlight-line-cached nil)
10966 (defvar gnus-summary-highlight-line-trigger nil)
10967
10968 (defun gnus-summary-highlight-line-0 ()
10969   (if (and (eq gnus-summary-highlight-line-trigger 
10970                gnus-summary-highlight)
10971            gnus-summary-highlight-line-cached)
10972       gnus-summary-highlight-line-cached
10973     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10974           gnus-summary-highlight-line-cached
10975           (let* ((cond (list 'cond))
10976                  (c cond)
10977                  (list gnus-summary-highlight))
10978             (while list
10979               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10980               (setq c (cdr c)
10981                     list (cdr list)))
10982             (gnus-byte-compile (list 'lambda nil cond))))))
10983
10984 (defun gnus-summary-highlight-line ()
10985   "Highlight current line according to `gnus-summary-highlight'."
10986   (let* ((beg (gnus-point-at-bol))
10987          (article (or (gnus-summary-article-number) gnus-current-article))
10988          (score (or (cdr (assq article
10989                                gnus-newsgroup-scored))
10990                     gnus-summary-default-score 0))
10991          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10992          (inhibit-read-only t)
10993          (default gnus-summary-default-score)
10994          (default-high gnus-summary-default-high-score)
10995          (default-low gnus-summary-default-low-score)
10996          (uncached (memq article gnus-newsgroup-undownloaded))
10997          (downloaded (not uncached)))
10998     (let ((face (funcall (gnus-summary-highlight-line-0))))
10999       (unless (eq face (get-text-property beg 'face))
11000         (gnus-put-text-property-excluding-characters-with-faces
11001          beg (gnus-point-at-eol) 'face
11002          (setq face (if (boundp face) (symbol-value face) face)))
11003         (when gnus-summary-highlight-line-function
11004           (funcall gnus-summary-highlight-line-function article face))))))
11005
11006 (defun gnus-update-read-articles (group unread &optional compute)
11007   "Update the list of read articles in GROUP.
11008 UNREAD is a sorted list."
11009   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11010          (entry (gnus-gethash group gnus-newsrc-hashtb))
11011          (info (nth 2 entry))
11012          (prev 1)
11013          read)
11014     (if (or (not info) (not active))
11015         ;; There is no info on this group if it was, in fact,
11016         ;; killed.  Gnus stores no information on killed groups, so
11017         ;; there's nothing to be done.
11018         ;; One could store the information somewhere temporarily,
11019         ;; perhaps...  Hmmm...
11020         ()
11021       ;; Remove any negative articles numbers.
11022       (while (and unread (< (car unread) 0))
11023         (setq unread (cdr unread)))
11024       ;; Remove any expired article numbers
11025       (while (and unread (< (car unread) (car active)))
11026         (setq unread (cdr unread)))
11027       ;; Compute the ranges of read articles by looking at the list of
11028       ;; unread articles.
11029       (while unread
11030         (when (/= (car unread) prev)
11031           (push (if (= prev (1- (car unread))) prev
11032                   (cons prev (1- (car unread))))
11033                 read))
11034         (setq prev (1+ (car unread)))
11035         (setq unread (cdr unread)))
11036       (when (<= prev (cdr active))
11037         (push (cons prev (cdr active)) read))
11038       (setq read (if (> (length read) 1) (nreverse read) read))
11039       (if compute
11040           read
11041         (save-excursion
11042           (let (setmarkundo)
11043             ;; Propagate the read marks to the backend.
11044             (when (gnus-check-backend-function 'request-set-mark group)
11045               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11046                     (add (gnus-remove-from-range read (gnus-info-read info))))
11047                 (when (or add del)
11048                   (unless (gnus-check-group group)
11049                     (error "Can't open server for %s" group))
11050                   (gnus-request-set-mark
11051                    group (delq nil (list (if add (list add 'add '(read)))
11052                                          (if del (list del 'del '(read))))))
11053                   (setq setmarkundo
11054                         `(gnus-request-set-mark
11055                           ,group
11056                           ',(delq nil (list
11057                                        (if del (list del 'add '(read)))
11058                                        (if add (list add 'del '(read))))))))))
11059             (set-buffer gnus-group-buffer)
11060             (gnus-undo-register
11061               `(progn
11062                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11063                  (gnus-info-set-read ',info ',(gnus-info-read info))
11064                  (gnus-get-unread-articles-in-group ',info
11065                                                     (gnus-active ,group))
11066                  (gnus-group-update-group ,group t)
11067                  ,setmarkundo))))
11068         ;; Enter this list into the group info.
11069         (gnus-info-set-read info read)
11070         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11071         (gnus-get-unread-articles-in-group info (gnus-active group))
11072         t))))
11073
11074 (defun gnus-offer-save-summaries ()
11075   "Offer to save all active summary buffers."
11076   (let (buffers)
11077     ;; Go through all buffers and find all summaries.
11078     (dolist (buffer (buffer-list))
11079       (when (and (setq buffer (buffer-name buffer))
11080                  (string-match "Summary" buffer)
11081                  (save-excursion
11082                    (set-buffer buffer)
11083                    ;; We check that this is, indeed, a summary buffer.
11084                    (and (eq major-mode 'gnus-summary-mode)
11085                         ;; Also make sure this isn't bogus.
11086                         gnus-newsgroup-prepared
11087                         ;; Also make sure that this isn't a
11088                         ;; dead summary buffer.
11089                         (not gnus-dead-summary-mode))))
11090         (push buffer buffers)))
11091     ;; Go through all these summary buffers and offer to save them.
11092     (when buffers
11093       (save-excursion
11094         (map-y-or-n-p
11095          "Update summary buffer %s? "
11096          (lambda (buf)
11097            (switch-to-buffer buf)
11098            (gnus-summary-exit))
11099          buffers)))))
11100
11101 (defun gnus-summary-setup-default-charset ()
11102   "Setup newsgroup default charset."
11103   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11104       (setq gnus-newsgroup-charset nil)
11105     (let* ((ignored-charsets
11106             (or gnus-newsgroup-ephemeral-ignored-charsets
11107                 (append
11108                  (and gnus-newsgroup-name
11109                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11110                  gnus-newsgroup-ignored-charsets))))
11111       (setq gnus-newsgroup-charset
11112             (or gnus-newsgroup-ephemeral-charset
11113                 (and gnus-newsgroup-name
11114                      (gnus-parameter-charset gnus-newsgroup-name))
11115                 gnus-default-charset))
11116       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11117            ignored-charsets))))
11118
11119 ;;;
11120 ;;; Mime Commands
11121 ;;;
11122
11123 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11124   "Display the current article buffer fully MIME-buttonized.
11125 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11126 treated as multipart/mixed."
11127   (interactive "P")
11128   (require 'gnus-art)
11129   (let ((gnus-unbuttonized-mime-types nil)
11130         (gnus-mime-display-multipart-as-mixed show-all-parts))
11131     (gnus-summary-show-article)))
11132
11133 (defun gnus-summary-repair-multipart (article)
11134   "Add a Content-Type header to a multipart article without one."
11135   (interactive (list (gnus-summary-article-number)))
11136   (gnus-with-article article
11137     (message-narrow-to-head)
11138     (message-remove-header "Mime-Version")
11139     (goto-char (point-max))
11140     (insert "Mime-Version: 1.0\n")
11141     (widen)
11142     (when (search-forward "\n--" nil t)
11143       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11144         (message-narrow-to-head)
11145         (message-remove-header "Content-Type")
11146         (goto-char (point-max))
11147         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11148                         separator))
11149         (widen))))
11150   (let (gnus-mark-article-hook)
11151     (gnus-summary-select-article t t nil article)))
11152
11153 (defun gnus-summary-toggle-display-buttonized ()
11154   "Toggle the buttonizing of the article buffer."
11155   (interactive)
11156   (require 'gnus-art)
11157   (if (setq gnus-inhibit-mime-unbuttonizing
11158             (not gnus-inhibit-mime-unbuttonizing))
11159       (let ((gnus-unbuttonized-mime-types nil))
11160         (gnus-summary-show-article))
11161     (gnus-summary-show-article)))
11162
11163 ;;;
11164 ;;; Generic summary marking commands
11165 ;;;
11166
11167 (defvar gnus-summary-marking-alist
11168   '((read gnus-del-mark "d")
11169     (unread gnus-unread-mark "u")
11170     (ticked gnus-ticked-mark "!")
11171     (dormant gnus-dormant-mark "?")
11172     (expirable gnus-expirable-mark "e"))
11173   "An alist of names/marks/keystrokes.")
11174
11175 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11176 (defvar gnus-summary-mark-map)
11177
11178 (defun gnus-summary-make-all-marking-commands ()
11179   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11180   (dolist (elem gnus-summary-marking-alist)
11181     (apply 'gnus-summary-make-marking-command elem)))
11182
11183 (defun gnus-summary-make-marking-command (name mark keystroke)
11184   (let ((map (make-sparse-keymap)))
11185     (define-key gnus-summary-generic-mark-map keystroke map)
11186     (dolist (lway `((next "next" next nil "n")
11187                     (next-unread "next unread" next t "N")
11188                     (prev "previous" prev nil "p")
11189                     (prev-unread "previous unread" prev t "P")
11190                     (nomove "" nil nil ,keystroke)))
11191       (let ((func (gnus-summary-make-marking-command-1
11192                    mark (car lway) lway name)))
11193         (setq func (eval func))
11194         (define-key map (nth 4 lway) func)))))
11195
11196 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11197   `(defun ,(intern
11198             (format "gnus-summary-put-mark-as-%s%s"
11199                     name (if (eq way 'nomove)
11200                              ""
11201                            (concat "-" (symbol-name way)))))
11202      (n)
11203      ,(format
11204        "Mark the current article as %s%s.
11205 If N, the prefix, then repeat N times.
11206 If N is negative, move in reverse order.
11207 The difference between N and the actual number of articles marked is
11208 returned."
11209        name (cadr lway))
11210      (interactive "p")
11211      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11212
11213 (defun gnus-summary-generic-mark (n mark move unread)
11214   "Mark N articles with MARK."
11215   (unless (eq major-mode 'gnus-summary-mode)
11216     (error "This command can only be used in the summary buffer"))
11217   (gnus-summary-show-thread)
11218   (let ((nummove
11219          (cond
11220           ((eq move 'next) 1)
11221           ((eq move 'prev) -1)
11222           (t 0))))
11223     (if (zerop nummove)
11224         (setq n 1)
11225       (when (< n 0)
11226         (setq n (abs n)
11227               nummove (* -1 nummove))))
11228     (while (and (> n 0)
11229                 (gnus-summary-mark-article nil mark)
11230                 (zerop (gnus-summary-next-subject nummove unread t)))
11231       (setq n (1- n)))
11232     (when (/= 0 n)
11233       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11234     (gnus-summary-recenter)
11235     (gnus-summary-position-point)
11236     (gnus-set-mode-line 'summary)
11237     n))
11238
11239 (defun gnus-summary-insert-articles (articles)
11240   (when (setq articles
11241               (gnus-sorted-difference articles
11242                                       (mapcar (lambda (h)
11243                                                 (mail-header-number h))
11244                                               gnus-newsgroup-headers)))
11245     (setq gnus-newsgroup-headers
11246           (merge 'list
11247                  gnus-newsgroup-headers
11248                  (gnus-fetch-headers articles)
11249                  'gnus-article-sort-by-number))
11250     ;; Suppress duplicates?
11251     (when gnus-suppress-duplicates
11252       (gnus-dup-suppress-articles))
11253
11254     ;; We might want to build some more threads first.
11255     (when (and gnus-fetch-old-headers
11256                (eq gnus-headers-retrieved-by 'nov))
11257       (if (eq gnus-fetch-old-headers 'invisible)
11258           (gnus-build-all-threads)
11259         (gnus-build-old-threads)))
11260     ;; Let the Gnus agent mark articles as read.
11261     (when gnus-agent
11262       (gnus-agent-get-undownloaded-list))
11263     ;; Remove list identifiers from subject
11264     (when gnus-list-identifiers
11265       (gnus-summary-remove-list-identifiers))
11266     ;; First and last article in this newsgroup.
11267     (when gnus-newsgroup-headers
11268       (setq gnus-newsgroup-begin
11269             (mail-header-number (car gnus-newsgroup-headers))
11270             gnus-newsgroup-end
11271             (mail-header-number
11272              (gnus-last-element gnus-newsgroup-headers))))
11273     (when gnus-use-scoring
11274       (gnus-possibly-score-headers))))
11275
11276 (defun gnus-summary-insert-old-articles (&optional all)
11277   "Insert all old articles in this group.
11278 If ALL is non-nil, already read articles become readable.
11279 If ALL is a number, fetch this number of articles."
11280   (interactive "P")
11281   (prog1
11282       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11283             older len)
11284         (setq older
11285               ;; Some nntp servers lie about their active range.  When
11286               ;; this happens, the active range can be in the millions.
11287               ;; Use a compressed range to avoid creating a huge list.
11288               (gnus-range-difference (list gnus-newsgroup-active) old))
11289         (setq len (gnus-range-length older))
11290         (cond
11291          ((null older) nil)
11292          ((numberp all)
11293           (if (< all len)
11294               (let ((older-range (nreverse older)))
11295                 (setq older nil)
11296
11297                 (while (> all 0)
11298                   (let* ((r (pop older-range))
11299                          (min (if (numberp r) r (car r)))
11300                          (max (if (numberp r) r (cdr r))))
11301                     (while (and (<= min max)
11302                                 (> all 0))
11303                       (push max older)
11304                       (setq all (1- all)
11305                             max (1- max))))))
11306             (setq older (gnus-uncompress-range older))))
11307          (all
11308           (setq older (gnus-uncompress-range older)))
11309          (t
11310           (when (and (numberp gnus-large-newsgroup)
11311                    (> len gnus-large-newsgroup))
11312               (let* ((cursor-in-echo-area nil)
11313                      (initial (gnus-parameter-large-newsgroup-initial
11314                                gnus-newsgroup-name))
11315                      (input
11316                       (read-string
11317                        (format
11318                         "How many articles from %s (%s %d): "
11319                         (gnus-limit-string
11320                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11321                         (if initial "max" "default")
11322                         len)
11323                        (if initial
11324                            (cons (number-to-string initial)
11325                                  0)))))
11326                 (unless (string-match "^[ \t]*$" input)
11327                   (setq all (string-to-number input))
11328                   (if (< all len)
11329                       (let ((older-range (nreverse older)))
11330                         (setq older nil)
11331
11332                         (while (> all 0)
11333                           (let* ((r (pop older-range))
11334                                  (min (if (numberp r) r (car r)))
11335                                  (max (if (numberp r) r (cdr r))))
11336                             (while (and (<= min max)
11337                                         (> all 0))
11338                               (push max older)
11339                               (setq all (1- all)
11340                                     max (1- max))))))))))
11341           (setq older (gnus-uncompress-range older))))
11342         (if (not older)
11343             (message "No old news.")
11344           (gnus-summary-insert-articles older)
11345           (gnus-summary-limit (gnus-sorted-nunion old older))))
11346     (gnus-summary-position-point)))
11347
11348 (defun gnus-summary-insert-new-articles ()
11349   "Insert all new articles in this group."
11350   (interactive)
11351   (prog1
11352       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11353             (old-active gnus-newsgroup-active)
11354             (nnmail-fetched-sources (list t))
11355             i new)
11356         (setq gnus-newsgroup-active
11357               (gnus-activate-group gnus-newsgroup-name 'scan))
11358         (setq i (cdr gnus-newsgroup-active))
11359         (while (> i (cdr old-active))
11360           (push i new)
11361           (decf i))
11362         (if (not new)
11363             (message "No gnus is bad news.")
11364           (gnus-summary-insert-articles new)
11365           (setq gnus-newsgroup-unreads
11366                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11367           (gnus-summary-limit (gnus-sorted-nunion old new))))
11368     (gnus-summary-position-point)))
11369
11370 (gnus-summary-make-all-marking-commands)
11371
11372 (gnus-ems-redefine)
11373
11374 (provide 'gnus-sum)
11375
11376 (run-hooks 'gnus-sum-load-hook)
11377
11378 ;;; gnus-sum.el ends here