0abab9078fc8e598edc64bc213e7438460a788d1
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-make-false-root-always t
113   "Always make a false dummy root."
114   :group 'gnus-thread
115   :type 'boolean)
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include:
145 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
146 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 \(they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-default-high-score 0
211   "*Default threshold for a high scored article.
212 An article will be highlighted as high scored if its score is greater
213 than this score."
214   :group 'gnus-score-default
215   :type 'integer)
216
217 (defcustom gnus-summary-default-low-score 0
218   "*Default threshold for a low scored article.
219 An article will be highlighted as low scored if its score is smaller
220 than this score."
221   :group 'gnus-score-default
222   :type 'integer)
223
224 (defcustom gnus-summary-zcore-fuzz 0
225   "*Fuzziness factor for the zcore in the summary buffer.
226 Articles with scores closer than this to `gnus-summary-default-score'
227 will not be marked."
228   :group 'gnus-summary-format
229   :type 'integer)
230
231 (defcustom gnus-simplify-subject-fuzzy-regexp nil
232   "*Strings to be removed when doing fuzzy matches.
233 This can either be a regular expression or list of regular expressions
234 that will be removed from subject strings if fuzzy subject
235 simplification is selected."
236   :group 'gnus-thread
237   :type '(repeat regexp))
238
239 (defcustom gnus-show-threads t
240   "*If non-nil, display threads in summary mode."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-subtree nil
245   "*If non-nil, hide all threads initially.
246 This can be a predicate specifier which says which threads to hide.
247 If threads are hidden, you have to run the command
248 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
249 to expose hidden threads."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-hide-killed t
254   "*If non-nil, hide killed threads automatically."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-ignore-subject t
259   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
260 If nil, articles that have different subjects from their parents will
261 start separate threads."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-operation-ignore-subject t
266   "*If non-nil, subjects will be ignored when doing thread commands.
267 This affects commands like `gnus-summary-kill-thread' and
268 `gnus-summary-lower-thread'.
269
270 If this variable is nil, articles in the same thread with different
271 subjects will not be included in the operation in question.  If this
272 variable is `fuzzy', only articles that have subjects that are fuzzily
273 equal will be included."
274   :group 'gnus-thread
275   :type '(choice (const :tag "off" nil)
276                  (const fuzzy)
277                  (sexp :tag "on" t)))
278
279 (defcustom gnus-thread-indent-level 4
280   "*Number that says how much each sub-thread should be indented."
281   :group 'gnus-thread
282   :type 'integer)
283
284 (defcustom gnus-auto-extend-newsgroup t
285   "*If non-nil, extend newsgroup forward and backward when requested."
286   :group 'gnus-summary-choose
287   :type 'boolean)
288
289 (defcustom gnus-auto-select-first t
290   "*If non-nil, select the article under point.
291 Which article this is is controlled by the `gnus-auto-select-subject'
292 variable.
293
294 If you want to prevent automatic selection of articles in some
295 newsgroups, set the variable to nil in `gnus-select-group-hook'."
296   :group 'gnus-group-select
297   :type '(choice (const :tag "none" nil)
298                  (sexp :menu-tag "first" t)))
299
300 (defcustom gnus-auto-select-subject 'unread
301   "*Says what subject to place under point when entering a group.
302
303 This variable can either be the symbols `first' (place point on the
304 first subject), `unread' (place point on the subject line of the first
305 unread article), `best' (place point on the subject line of the
306 higest-scored article), `unseen' (place point on the subject line of
307 the first unseen article), 'unseen-or-unread' (place point on the subject
308 line of the first unseen article or, if all article have been seen, on the
309 subject line of the first unread article), or a function to be called to
310 place point on some subject line."
311   :group 'gnus-group-select
312   :type '(choice (const best)
313                  (const unread)
314                  (const first)
315                  (const unseen)
316                  (const unseen-or-unread)))
317
318 (defcustom gnus-auto-select-next t
319   "*If non-nil, offer to go to the next group from the end of the previous.
320 If the value is t and the next newsgroup is empty, Gnus will exit
321 summary mode and go back to group mode.  If the value is neither nil
322 nor t, Gnus will select the following unread newsgroup.  In
323 particular, if the value is the symbol `quietly', the next unread
324 newsgroup will be selected without any confirmation, and if it is
325 `almost-quietly', the next group will be selected without any
326 confirmation if you are located on the last article in the group.
327 Finally, if this variable is `slightly-quietly', the `Z n' command
328 will go to the next group without confirmation."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "off" nil)
331                  (const quietly)
332                  (const almost-quietly)
333                  (const slightly-quietly)
334                  (sexp :menu-tag "on" t)))
335
336 (defcustom gnus-auto-select-same nil
337   "*If non-nil, select the next article with the same subject.
338 If there are no more articles with the same subject, go to
339 the first unread article."
340   :group 'gnus-summary-maneuvering
341   :type 'boolean)
342
343 (defcustom gnus-summary-check-current nil
344   "*If non-nil, consider the current article when moving.
345 The \"unread\" movement commands will stay on the same line if the
346 current article is unread."
347   :group 'gnus-summary-maneuvering
348   :type 'boolean)
349
350 (defcustom gnus-auto-center-summary t
351   "*If non-nil, always center the current summary buffer.
352 In particular, if `vertical' do only vertical recentering.  If non-nil
353 and non-`vertical', do both horizontal and vertical recentering."
354   :group 'gnus-summary-maneuvering
355   :type '(choice (const :tag "none" nil)
356                  (const vertical)
357                  (integer :tag "height")
358                  (sexp :menu-tag "both" t)))
359
360 (defcustom gnus-show-all-headers nil
361   "*If non-nil, don't hide any headers."
362   :group 'gnus-article-hiding
363   :group 'gnus-article-headers
364   :type 'boolean)
365
366 (defcustom gnus-summary-ignore-duplicates nil
367   "*If non-nil, ignore articles with identical Message-ID headers."
368   :group 'gnus-summary
369   :type 'boolean)
370
371 (defcustom gnus-single-article-buffer t
372   "*If non-nil, display all articles in the same buffer.
373 If nil, each group will get its own article buffer."
374   :group 'gnus-article-various
375   :type 'boolean)
376
377 (defcustom gnus-break-pages t
378   "*If non-nil, do page breaking on articles.
379 The page delimiter is specified by the `gnus-page-delimiter'
380 variable."
381   :group 'gnus-article-various
382   :type 'boolean)
383
384 (defcustom gnus-move-split-methods nil
385   "*Variable used to suggest where articles are to be moved to.
386 It uses the same syntax as the `gnus-split-methods' variable.
387 However, whereas `gnus-split-methods' specifies file names as targets,
388 this variable specifies group names."
389   :group 'gnus-summary-mail
390   :type '(repeat (choice (list :value (fun) function)
391                          (cons :value ("" "") regexp (repeat string))
392                          (sexp :value nil))))
393
394 (defcustom gnus-unread-mark ?           ;Whitespace
395   "*Mark used for unread articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-ticked-mark ?!
400   "*Mark used for ticked articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-dormant-mark ??
405   "*Mark used for dormant articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-del-mark ?r
410   "*Mark used for del'd articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-read-mark ?R
415   "*Mark used for read articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-expirable-mark ?E
420   "*Mark used for expirable articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-killed-mark ?K
425   "*Mark used for killed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-spam-mark ?H
430   "*Mark used for spam articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-souped-mark ?F
435   "*Mark used for souped articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-kill-file-mark ?X
440   "*Mark used for articles killed by kill files."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-low-score-mark ?Y
445   "*Mark used for articles with a low score."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-catchup-mark ?C
450   "*Mark used for articles that are caught up."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-replied-mark ?A
455   "*Mark used for articles that have been replied to."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-forwarded-mark ?F
460   "*Mark used for articles that have been forwarded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-recent-mark ?N
465   "*Mark used for articles that are recent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-cached-mark ?*
470   "*Mark used for articles that are in the cache."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-saved-mark ?S
475   "*Mark used for articles that have been saved."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unseen-mark ?.
480   "*Mark used for articles that haven't been seen."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-no-mark ?               ;Whitespace
485   "*Mark used for articles that have no other secondary mark."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-ancient-mark ?O
490   "*Mark used for ancient articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-sparse-mark ?Q
495   "*Mark used for sparsely reffed articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-canceled-mark ?G
500   "*Mark used for canceled articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-duplicate-mark ?M
505   "*Mark used for duplicate articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-undownloaded-mark ?-
510   "*Mark used for articles that weren't downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-downloaded-mark ?+
515   "*Mark used for articles that were downloaded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-downloadable-mark ?%
520   "*Mark used for articles that are to be downloaded."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-unsendable-mark ?=
525   "*Mark used for articles that won't be sent."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-score-over-mark ?+
530   "*Score mark used for articles with high scores."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-score-below-mark ?-
535   "*Score mark used for articles with low scores."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-empty-thread-mark ?     ;Whitespace
540   "*There is no thread under the article."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-not-empty-thread-mark ?=
545   "*There is a thread under the article."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-view-pseudo-asynchronously nil
550   "*If non-nil, Gnus will view pseudo-articles asynchronously."
551   :group 'gnus-extract-view
552   :type 'boolean)
553
554 (defcustom gnus-auto-expirable-marks
555   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
556         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
557         gnus-souped-mark gnus-duplicate-mark)
558   "*The list of marks converted into expiration if a group is auto-expirable."
559   :version "21.1"
560   :group 'gnus-summary
561   :type '(repeat character))
562
563 (defcustom gnus-inhibit-user-auto-expire t
564   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
565   :version "21.1"
566   :group 'gnus-summary
567   :type 'boolean)
568
569 (defcustom gnus-view-pseudos nil
570   "*If `automatic', pseudo-articles will be viewed automatically.
571 If `not-confirm', pseudos will be viewed automatically, and the user
572 will not be asked to confirm the command."
573   :group 'gnus-extract-view
574   :type '(choice (const :tag "off" nil)
575                  (const automatic)
576                  (const not-confirm)))
577
578 (defcustom gnus-view-pseudos-separately t
579   "*If non-nil, one pseudo-article will be created for each file to be viewed.
580 If nil, all files that use the same viewing command will be given as a
581 list of parameters to that command."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-insert-pseudo-articles t
586   "*If non-nil, insert pseudo-articles when decoding articles."
587   :group 'gnus-extract-view
588   :type 'boolean)
589
590 (defcustom gnus-summary-dummy-line-format
591   "   %(:                             :%) %S\n"
592   "*The format specification for the dummy roots in the summary buffer.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions.
595
596 %S  The subject
597
598 General format specifiers can also be used.
599 See `(gnus)Formatting Variables'."
600   :link '(custom-manual "(gnus)Formatting Variables")
601   :group 'gnus-threading
602   :type 'string)
603
604 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
605   "*The format specification for the summary mode line.
606 It works along the same lines as a normal formatting string,
607 with some simple extensions:
608
609 %G  Group name
610 %p  Unprefixed group name
611 %A  Current article number
612 %z  Current article score
613 %V  Gnus version
614 %U  Number of unread articles in the group
615 %e  Number of unselected articles in the group
616 %Z  A string with unread/unselected article counts
617 %g  Shortish group name
618 %S  Subject of the current article
619 %u  User-defined spec
620 %s  Current score file name
621 %d  Number of dormant articles
622 %r  Number of articles that have been marked as read in this session
623 %E  Number of articles expunged by the score files"
624   :group 'gnus-summary-format
625   :type 'string)
626
627 (defcustom gnus-list-identifiers nil
628   "Regexp that matches list identifiers to be removed from subject.
629 This can also be a list of regexps."
630   :version "21.1"
631   :group 'gnus-summary-format
632   :group 'gnus-article-hiding
633   :type '(choice (const :tag "none" nil)
634                  (regexp :value ".*")
635                  (repeat :value (".*") regexp)))
636
637 (defcustom gnus-summary-mark-below 0
638   "*Mark all articles with a score below this variable as read.
639 This variable is local to each summary buffer and usually set by the
640 score file."
641   :group 'gnus-score-default
642   :type 'integer)
643
644 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
645   "*List of functions used for sorting articles in the summary buffer.
646
647 Each function takes two articles and returns non-nil if the first
648 article should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-article-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-article-sort-by-number',
657 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
658 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
659 and `gnus-article-sort-by-score'.
660
661 When threading is turned on, the variable `gnus-thread-sort-functions'
662 controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-article-sort-by-number)
665                          (function-item gnus-article-sort-by-author)
666                          (function-item gnus-article-sort-by-subject)
667                          (function-item gnus-article-sort-by-date)
668                          (function-item gnus-article-sort-by-score)
669                          (function-item gnus-article-sort-by-random)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
673   "*List of functions used for sorting threads in the summary buffer.
674 By default, threads are sorted by article number.
675
676 Each function takes two threads and returns non-nil if the first
677 thread should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-thread-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-thread-sort-by-number',
686 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
687 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
688 `gnus-thread-sort-by-most-recent-number',
689 `gnus-thread-sort-by-most-recent-date',
690 `gnus-thread-sort-by-random', and
691 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
692
693 When threading is turned off, the variable
694 `gnus-article-sort-functions' controls how articles are sorted."
695   :group 'gnus-summary-sort
696   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
697                          (function-item gnus-thread-sort-by-author)
698                          (function-item gnus-thread-sort-by-subject)
699                          (function-item gnus-thread-sort-by-date)
700                          (function-item gnus-thread-sort-by-score)
701                          (function-item gnus-thread-sort-by-total-score)
702                          (function-item gnus-thread-sort-by-random)
703                          (function :tag "other"))))
704
705 (defcustom gnus-thread-score-function '+
706   "*Function used for calculating the total score of a thread.
707
708 The function is called with the scores of the article and each
709 subthread and should then return the score of the thread.
710
711 Some functions you can use are `+', `max', or `min'."
712   :group 'gnus-summary-sort
713   :type 'function)
714
715 (defcustom gnus-summary-expunge-below nil
716   "All articles that have a score less than this variable will be expunged.
717 This variable is local to the summary buffers."
718   :group 'gnus-score-default
719   :type '(choice (const :tag "off" nil)
720                  integer))
721
722 (defcustom gnus-thread-expunge-below nil
723   "All threads that have a total score less than this variable will be expunged.
724 See `gnus-thread-score-function' for en explanation of what a
725 \"thread score\" is.
726
727 This variable is local to the summary buffers."
728   :group 'gnus-threading
729   :group 'gnus-score-default
730   :type '(choice (const :tag "off" nil)
731                  integer))
732
733 (defcustom gnus-summary-mode-hook nil
734   "*A hook for Gnus summary mode.
735 This hook is run before any variables are set in the summary buffer."
736   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
737   :group 'gnus-summary-various
738   :type 'hook)
739
740 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
741 (when (featurep 'xemacs)
742   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
743   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
744   (add-hook 'gnus-summary-mode-hook
745             'gnus-xmas-switch-horizontal-scrollbar-off))
746
747 (defcustom gnus-summary-menu-hook nil
748   "*Hook run after the creation of the summary mode menu."
749   :group 'gnus-summary-visual
750   :type 'hook)
751
752 (defcustom gnus-summary-exit-hook nil
753   "*A hook called on exit from the summary buffer.
754 It will be called with point in the group buffer."
755   :group 'gnus-summary-exit
756   :type 'hook)
757
758 (defcustom gnus-summary-prepare-hook nil
759   "*A hook called after the summary buffer has been generated.
760 If you want to modify the summary buffer, you can use this hook."
761   :group 'gnus-summary-various
762   :type 'hook)
763
764 (defcustom gnus-summary-prepared-hook nil
765   "*A hook called as the last thing after the summary buffer has been generated."
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 (defcustom gnus-summary-generate-hook nil
770   "*A hook run just before generating the summary buffer.
771 This hook is commonly used to customize threading variables and the
772 like."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-select-group-hook nil
777   "*A hook called when a newsgroup is selected.
778
779 If you'd like to simplify subjects like the
780 `gnus-summary-next-same-subject' command does, you can use the
781 following hook:
782
783  (add-hook gnus-select-group-hook
784            (lambda ()
785              (mapcar (lambda (header)
786                        (mail-header-set-subject
787                         header
788                         (gnus-simplify-subject
789                          (mail-header-subject header) 're-only)))
790                      gnus-newsgroup-headers)))"
791   :group 'gnus-group-select
792   :type 'hook)
793
794 (defcustom gnus-select-article-hook nil
795   "*A hook called when an article is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-visual-mark-article-hook
800   (list 'gnus-highlight-selected-summary)
801   "*Hook run after selecting an article in the summary buffer.
802 It is meant to be used for highlighting the article in some way.  It
803 is not run if `gnus-visual' is nil."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-parse-headers-hook nil
808   "*A hook called before parsing the headers."
809   :group 'gnus-various
810   :type 'hook)
811
812 (defcustom gnus-exit-group-hook nil
813   "*A hook called when exiting summary mode.
814 This hook is not called from the non-updating exit commands like `Q'."
815   :group 'gnus-various
816   :type 'hook)
817
818 (defcustom gnus-summary-update-hook
819   (list 'gnus-summary-highlight-line)
820   "*A hook called when a summary line is changed.
821 The hook will not be called if `gnus-visual' is nil.
822
823 The default function `gnus-summary-highlight-line' will
824 highlight the line according to the `gnus-summary-highlight'
825 variable."
826   :group 'gnus-summary-visual
827   :type 'hook)
828
829 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
830   "*A hook called when an article is selected for the first time.
831 The hook is intended to mark an article as read (or unread)
832 automatically when it is selected."
833   :group 'gnus-summary-choose
834   :type 'hook)
835
836 (defcustom gnus-group-no-more-groups-hook nil
837   "*A hook run when returning to group mode having no more (unread) groups."
838   :group 'gnus-group-select
839   :type 'hook)
840
841 (defcustom gnus-ps-print-hook nil
842   "*A hook run before ps-printing something from Gnus."
843   :group 'gnus-summary
844   :type 'hook)
845
846 (defcustom gnus-summary-display-arrow
847   (and (fboundp 'display-graphic-p)
848        (display-graphic-p))
849   "*If non-nil, display an arrow highlighting the current article."
850   :version "21.1"
851   :group 'gnus-summary
852   :type 'boolean)
853
854 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
855   "Face used for highlighting the current article in the summary buffer."
856   :group 'gnus-summary-visual
857   :type 'face)
858
859 (defvar gnus-tmp-downloaded nil)
860
861 (defcustom gnus-summary-highlight
862   '(((eq mark gnus-canceled-mark)
863      . gnus-summary-cancelled-face)
864     ((and uncached (> score default-high))
865      . gnus-summary-high-uncached-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-uncached-face)
868     (uncached
869      . gnus-summary-normal-uncached-face)
870     ((and (> score default-high)
871           (or (eq mark gnus-dormant-mark)
872               (eq mark gnus-ticked-mark)))
873      . gnus-summary-high-ticked-face)
874     ((and (< score default-low)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-low-ticked-face)
878     ((or (eq mark gnus-dormant-mark)
879          (eq mark gnus-ticked-mark))
880      . gnus-summary-normal-ticked-face)
881     ((and (> score default-high) (eq mark gnus-ancient-mark))
882      . gnus-summary-high-ancient-face)
883     ((and (< score default-low) (eq mark gnus-ancient-mark))
884      . gnus-summary-low-ancient-face)
885     ((eq mark gnus-ancient-mark)
886      . gnus-summary-normal-ancient-face)
887     ((and (> score default-high) (eq mark gnus-unread-mark))
888      . gnus-summary-high-unread-face)
889     ((and (< score default-low) (eq mark gnus-unread-mark))
890      . gnus-summary-low-unread-face)
891     ((eq mark gnus-unread-mark)
892      . gnus-summary-normal-unread-face)
893     ((> score default-high)
894      . gnus-summary-high-read-face)
895     ((< score default-low)
896      . gnus-summary-low-read-face)
897     (t
898      . gnus-summary-normal-read-face))
899   "*Controls the highlighting of summary buffer lines.
900
901 A list of (FORM . FACE) pairs.  When deciding how a a particular
902 summary line should be displayed, each form is evaluated.  The content
903 of the face field after the first true form is used.  You can change
904 how those summary lines are displayed, by editing the face field.
905
906 You can use the following variables in the FORM field.
907
908 score:        The article's score
909 default:      The default article score.
910 default-high: The default score for high scored articles.
911 default-low:  The default score for low scored articles.
912 below:        The score below which articles are automatically marked as read.
913 mark:         The articles mark."
914   :group 'gnus-summary-visual
915   :type '(repeat (cons (sexp :tag "Form" nil)
916                        face)))
917
918 (defcustom gnus-alter-header-function nil
919   "Function called to allow alteration of article header structures.
920 The function is called with one parameter, the article header vector,
921 which it may alter in any way.")
922
923 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
924   "Variable that says which function should be used to decode a string with encoded words.")
925
926 (defcustom gnus-extra-headers '(To Newsgroups)
927   "*Extra headers to parse."
928   :version "21.1"
929   :group 'gnus-summary
930   :type '(repeat symbol))
931
932 (defcustom gnus-ignored-from-addresses
933   (and user-mail-address (regexp-quote user-mail-address))
934   "*Regexp of From headers that may be suppressed in favor of To headers."
935   :version "21.1"
936   :group 'gnus-summary
937   :type 'regexp)
938
939 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
940   "List of charsets that should be ignored.
941 When these charsets are used in the \"charset\" parameter, the
942 default charset will be used instead."
943   :version "21.1"
944   :type '(repeat symbol)
945   :group 'gnus-charset)
946
947 (gnus-define-group-parameter
948  ignored-charsets
949  :type list
950  :function-document
951  "Return the ignored charsets of GROUP."
952  :variable gnus-group-ignored-charsets-alist
953  :variable-default
954  '(("alt\\.chinese\\.text" iso-8859-1))
955  :variable-document
956  "Alist of regexps (to match group names) and charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959  :variable-group gnus-charset
960  :variable-type '(repeat (cons (regexp :tag "Group")
961                                (repeat symbol)))
962  :parameter-type '(choice :tag "Ignored charsets"
963                           :value nil
964                           (repeat (symbol)))
965  :parameter-document       "\
966 List of charsets that should be ignored.
967
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead.")
970
971 (defcustom gnus-group-highlight-words-alist nil
972   "Alist of group regexps and highlight regexps.
973 This variable uses the same syntax as `gnus-emphasis-alist'."
974   :version "21.1"
975   :type '(repeat (cons (regexp :tag "Group")
976                        (repeat (list (regexp :tag "Highlight regexp")
977                                      (number :tag "Group for entire word" 0)
978                                      (number :tag "Group for displayed part" 0)
979                                      (symbol :tag "Face"
980                                              gnus-emphasis-highlight-words)))))
981   :group 'gnus-summary-visual)
982
983 (defcustom gnus-summary-show-article-charset-alist
984   nil
985   "Alist of number and charset.
986 The article will be shown with the charset corresponding to the
987 numbered argument.
988 For example: ((1 . cn-gb-2312) (2 . big5))."
989   :version "21.1"
990   :type '(repeat (cons (number :tag "Argument" 1)
991                        (symbol :tag "Charset")))
992   :group 'gnus-charset)
993
994 (defcustom gnus-preserve-marks t
995   "Whether marks are preserved when moving, copying and respooling messages."
996   :version "21.1"
997   :type 'boolean
998   :group 'gnus-summary-marks)
999
1000 (defcustom gnus-alter-articles-to-read-function nil
1001   "Function to be called to alter the list of articles to be selected."
1002   :type '(choice (const nil) function)
1003   :group 'gnus-summary)
1004
1005 (defcustom gnus-orphan-score nil
1006   "*All orphans get this score added.  Set in the score file."
1007   :group 'gnus-score-default
1008   :type '(choice (const nil)
1009                  integer))
1010
1011 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1012   "*A regexp to match MIME parts when saving multiple parts of a message
1013 with gnus-summary-save-parts (X m). This regexp will be used by default
1014 when prompting the user for which type of files to save."
1015   :group 'gnus-summary
1016   :type 'regexp)
1017
1018 (defcustom gnus-read-all-available-headers nil
1019   "Whether Gnus should parse all headers made available to it.
1020 This is mostly relevant for slow backends where the user may
1021 wish to widen the summary buffer to include all headers
1022 that were fetched.  Say, for nnultimate groups."
1023   :group 'gnus-summary
1024   :type '(choice boolean regexp))
1025
1026 (defcustom gnus-summary-muttprint-program "muttprint"
1027   "Command (and optional arguments) used to run Muttprint."
1028   :version "21.3"
1029   :group 'gnus-summary
1030   :type 'string)
1031
1032 (defcustom gnus-article-loose-mime nil
1033   "If non-nil, don't require MIME-Version header.
1034 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1035 supply the MIME-Version header or deliberately strip it From the mail.
1036 Set it to non-nil, Gnus will treat some articles as MIME even if
1037 the MIME-Version header is missed."
1038   :version "21.3"
1039   :type 'boolean
1040   :group 'gnus-article)
1041
1042 ;;; Internal variables
1043
1044 (defvar gnus-summary-display-cache nil)
1045 (defvar gnus-article-mime-handles nil)
1046 (defvar gnus-article-decoded-p nil)
1047 (defvar gnus-article-charset nil)
1048 (defvar gnus-article-ignored-charsets nil)
1049 (defvar gnus-scores-exclude-files nil)
1050 (defvar gnus-page-broken nil)
1051
1052 (defvar gnus-original-article nil)
1053 (defvar gnus-article-internal-prepare-hook nil)
1054 (defvar gnus-newsgroup-process-stack nil)
1055
1056 (defvar gnus-thread-indent-array nil)
1057 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1058 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1059   "Function called to sort the articles within a thread after it has been gathered together.")
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073 (defvar gnus-newsgroup-display nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1087         ?s)
1088     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1099     (?L gnus-tmp-lines ?s)
1100     (?O gnus-tmp-downloaded ?c)
1101     (?I gnus-tmp-indentation ?s)
1102     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1103     (?R gnus-tmp-replied ?c)
1104     (?\[ gnus-tmp-opening-bracket ?c)
1105     (?\] gnus-tmp-closing-bracket ?c)
1106     (?\> (make-string gnus-tmp-level ? ) ?s)
1107     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1108     (?i gnus-tmp-score ?d)
1109     (?z gnus-tmp-score-char ?c)
1110     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1111     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1112     (?U gnus-tmp-unread ?c)
1113     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1114         ?s)
1115     (?t (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1117         ?d)
1118     (?e (gnus-summary-number-of-articles-in-thread
1119          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1120         ?c)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?P (gnus-pick-line-number) ?d)
1123     (?B gnus-tmp-thread-tree-header-string ?s)
1124     (user-date (gnus-user-date
1125                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1126   "An alist of format specifications that can appear in summary lines.
1127 These are paired with what variables they correspond with, along with
1128 the type of the variable (string, integer, character, etc).")
1129
1130 (defvar gnus-summary-dummy-line-format-alist
1131   `((?S gnus-tmp-subject ?s)
1132     (?N gnus-tmp-number ?d)
1133     (?u gnus-tmp-user-defined ?s)))
1134
1135 (defvar gnus-summary-mode-line-format-alist
1136   `((?G gnus-tmp-group-name ?s)
1137     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1138     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1139     (?A gnus-tmp-article-number ?d)
1140     (?Z gnus-tmp-unread-and-unselected ?s)
1141     (?V gnus-version ?s)
1142     (?U gnus-tmp-unread-and-unticked ?d)
1143     (?S gnus-tmp-subject ?s)
1144     (?e gnus-tmp-unselected ?d)
1145     (?u gnus-tmp-user-defined ?s)
1146     (?d (length gnus-newsgroup-dormant) ?d)
1147     (?t (length gnus-newsgroup-marked) ?d)
1148     (?h (length gnus-newsgroup-spam-marked) ?d)
1149     (?r (length gnus-newsgroup-reads) ?d)
1150     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1151     (?E gnus-newsgroup-expunged-tally ?d)
1152     (?s (gnus-current-score-file-nondirectory) ?s)))
1153
1154 (defvar gnus-last-search-regexp nil
1155   "Default regexp for article search command.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-agentized nil
1161   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1162 (defvar gnus-newsgroup-begin nil)
1163 (defvar gnus-newsgroup-end nil)
1164 (defvar gnus-newsgroup-last-rmail nil)
1165 (defvar gnus-newsgroup-last-mail nil)
1166 (defvar gnus-newsgroup-last-folder nil)
1167 (defvar gnus-newsgroup-last-file nil)
1168 (defvar gnus-newsgroup-auto-expire nil)
1169 (defvar gnus-newsgroup-active nil)
1170
1171 (defvar gnus-newsgroup-data nil)
1172 (defvar gnus-newsgroup-data-reverse nil)
1173 (defvar gnus-newsgroup-limit nil)
1174 (defvar gnus-newsgroup-limits nil)
1175
1176 (defvar gnus-newsgroup-unreads nil
1177   "Sorted list of unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-unselected nil
1180   "Sorted list of unselected unread articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-reads nil
1183   "Alist of read articles and article marks in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-expunged-tally nil)
1186
1187 (defvar gnus-newsgroup-marked nil
1188   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1189
1190 (defvar gnus-newsgroup-spam-marked nil
1191   "List of ranges of articles that have been marked as spam.")
1192
1193 (defvar gnus-newsgroup-killed nil
1194   "List of ranges of articles that have been through the scoring process.")
1195
1196 (defvar gnus-newsgroup-cached nil
1197   "Sorted list of articles that come from the article cache.")
1198
1199 (defvar gnus-newsgroup-saved nil
1200   "List of articles that have been saved.")
1201
1202 (defvar gnus-newsgroup-kill-headers nil)
1203
1204 (defvar gnus-newsgroup-replied nil
1205   "List of articles that have been replied to in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-forwarded nil
1208   "List of articles that have been forwarded in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-recent nil
1211   "List of articles that have are recent in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-expirable nil
1214   "Sorted list of articles in the current newsgroup that can be expired.")
1215
1216 (defvar gnus-newsgroup-processable nil
1217   "List of articles in the current newsgroup that can be processed.")
1218
1219 (defvar gnus-newsgroup-downloadable nil
1220   "Sorted list of articles in the current newsgroup that can be processed.")
1221
1222 (defvar gnus-newsgroup-undownloaded nil
1223   "List of articles in the current newsgroup that haven't been downloaded..")
1224
1225 (defvar gnus-newsgroup-unsendable nil
1226   "List of articles in the current newsgroup that won't be sent.")
1227
1228 (defvar gnus-newsgroup-bookmarks nil
1229   "List of articles in the current newsgroup that have bookmarks.")
1230
1231 (defvar gnus-newsgroup-dormant nil
1232   "Sorted list of dormant articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-unseen nil
1235   "List of unseen articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-seen nil
1238   "Range of seen articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-articles nil
1241   "List of articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-scored nil
1244   "List of scored articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-headers nil
1247   "List of article headers in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-threads nil)
1250
1251 (defvar gnus-newsgroup-prepared nil
1252   "Whether the current group has been prepared properly.")
1253
1254 (defvar gnus-newsgroup-ancient nil
1255   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-sparse nil)
1258
1259 (defvar gnus-current-article nil)
1260 (defvar gnus-article-current nil)
1261 (defvar gnus-current-headers nil)
1262 (defvar gnus-have-all-headers nil)
1263 (defvar gnus-last-article nil)
1264 (defvar gnus-newsgroup-history nil)
1265 (defvar gnus-newsgroup-charset nil)
1266 (defvar gnus-newsgroup-ephemeral-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1268
1269 (defvar gnus-article-before-search nil)
1270
1271 (defconst gnus-summary-local-variables
1272   '(gnus-newsgroup-name
1273     gnus-newsgroup-begin gnus-newsgroup-end
1274     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1275     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1276     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1277     gnus-newsgroup-unselected gnus-newsgroup-marked
1278     gnus-newsgroup-spam-marked
1279     gnus-newsgroup-reads gnus-newsgroup-saved
1280     gnus-newsgroup-replied gnus-newsgroup-forwarded
1281     gnus-newsgroup-recent
1282     gnus-newsgroup-expirable
1283     gnus-newsgroup-processable gnus-newsgroup-killed
1284     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1285     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1286     gnus-newsgroup-seen gnus-newsgroup-articles
1287     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1288     gnus-newsgroup-headers gnus-newsgroup-threads
1289     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1290     gnus-current-article gnus-current-headers gnus-have-all-headers
1291     gnus-last-article gnus-article-internal-prepare-hook
1292     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1293     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1294     gnus-thread-expunge-below
1295     gnus-score-alist gnus-current-score-file
1296     (gnus-summary-expunge-below . global)
1297     (gnus-summary-mark-below . global)
1298     (gnus-orphan-score . global)
1299     gnus-newsgroup-active gnus-scores-exclude-files
1300     gnus-newsgroup-history gnus-newsgroup-ancient
1301     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1302     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1303     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1304     (gnus-newsgroup-expunged-tally . 0)
1305     gnus-cache-removable-articles gnus-newsgroup-cached
1306     gnus-newsgroup-data gnus-newsgroup-data-reverse
1307     gnus-newsgroup-limit gnus-newsgroup-limits
1308     gnus-newsgroup-charset gnus-newsgroup-display)
1309   "Variables that are buffer-local to the summary buffers.")
1310
1311 (defvar gnus-newsgroup-variables nil
1312   "A list of variables that have separate values in different newsgroups.
1313 A list of newsgroup (summary buffer) local variables, or cons of
1314 variables and their default values (when the default values are not
1315 nil), that should be made global while the summary buffer is active.
1316 These variables can be used to set variables in the group parameters
1317 while still allowing them to affect operations done in other
1318 buffers. For example:
1319
1320 \(setq gnus-newsgroup-variables
1321      '(message-use-followup-to
1322        (gnus-visible-headers .
1323          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1324 ")
1325
1326 ;; Byte-compiler warning.
1327 (eval-when-compile (defvar gnus-article-mode-map))
1328
1329 ;; MIME stuff.
1330
1331 (defvar gnus-decode-encoded-word-methods
1332   '(mail-decode-encoded-word-string)
1333   "List of methods used to decode encoded words.
1334
1335 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1336 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1337 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1338 whose names match REGEXP.
1339
1340 For example:
1341 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1342  mail-decode-encoded-word-string
1343  (\"chinese\" . rfc1843-decode-string))")
1344
1345 (defvar gnus-decode-encoded-word-methods-cache nil)
1346
1347 (defun gnus-multi-decode-encoded-word-string (string)
1348   "Apply the functions from `gnus-encoded-word-methods' that match."
1349   (unless (and gnus-decode-encoded-word-methods-cache
1350                (eq gnus-newsgroup-name
1351                    (car gnus-decode-encoded-word-methods-cache)))
1352     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1353     (mapcar (lambda (x)
1354               (if (symbolp x)
1355                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1356                 (if (and gnus-newsgroup-name
1357                          (string-match (car x) gnus-newsgroup-name))
1358                     (nconc gnus-decode-encoded-word-methods-cache
1359                            (list (cdr x))))))
1360             gnus-decode-encoded-word-methods))
1361   (let ((xlist gnus-decode-encoded-word-methods-cache))
1362     (pop xlist)
1363     (while xlist
1364       (setq string (funcall (pop xlist) string))))
1365   string)
1366
1367 ;; Subject simplification.
1368
1369 (defun gnus-simplify-whitespace (str)
1370   "Remove excessive whitespace from STR."
1371   (let ((mystr str))
1372     ;; Multiple spaces.
1373     (while (string-match "[ \t][ \t]+" mystr)
1374       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1375                           " "
1376                           (substring mystr (match-end 0)))))
1377     ;; Leading spaces.
1378     (when (string-match "^[ \t]+" mystr)
1379       (setq mystr (substring mystr (match-end 0))))
1380     ;; Trailing spaces.
1381     (when (string-match "[ \t]+$" mystr)
1382       (setq mystr (substring mystr 0 (match-beginning 0))))
1383     mystr))
1384
1385 (defun gnus-simplify-all-whitespace (str)
1386   "Remove all whitespace from STR."
1387   (let ((mystr str))
1388     (while (string-match "[ \t\n]+" mystr)
1389       (setq mystr (replace-match "" nil nil mystr)))
1390     mystr))
1391
1392 (defsubst gnus-simplify-subject-re (subject)
1393   "Remove \"Re:\" from subject lines."
1394   (if (string-match message-subject-re-regexp subject)
1395       (substring subject (match-end 0))
1396     subject))
1397
1398 (defun gnus-simplify-subject (subject &optional re-only)
1399   "Remove `Re:' and words in parentheses.
1400 If RE-ONLY is non-nil, strip leading `Re:'s only."
1401   (let ((case-fold-search t))           ;Ignore case.
1402     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1403     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1404       (setq subject (substring subject (match-end 0))))
1405     ;; Remove uninteresting prefixes.
1406     (when (and (not re-only)
1407                gnus-simplify-ignored-prefixes
1408                (string-match gnus-simplify-ignored-prefixes subject))
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove words in parentheses from end.
1411     (unless re-only
1412       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1413         (setq subject (substring subject 0 (match-beginning 0)))))
1414     ;; Return subject string.
1415     subject))
1416
1417 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1418 ;; all whitespace.
1419 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1420   (goto-char (point-min))
1421   (while (re-search-forward regexp nil t)
1422     (replace-match (or newtext ""))))
1423
1424 (defun gnus-simplify-buffer-fuzzy ()
1425   "Simplify string in the buffer fuzzily.
1426 The string in the accessible portion of the current buffer is simplified.
1427 It is assumed to be a single-line subject.
1428 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1429 matter is removed.  Additional things can be deleted by setting
1430 `gnus-simplify-subject-fuzzy-regexp'."
1431   (let ((case-fold-search t)
1432         (modified-tick))
1433     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1434
1435     (while (not (eq modified-tick (buffer-modified-tick)))
1436       (setq modified-tick (buffer-modified-tick))
1437       (cond
1438        ((listp gnus-simplify-subject-fuzzy-regexp)
1439         (mapcar 'gnus-simplify-buffer-fuzzy-step
1440                 gnus-simplify-subject-fuzzy-regexp))
1441        (gnus-simplify-subject-fuzzy-regexp
1442         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1443       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1444       (gnus-simplify-buffer-fuzzy-step
1445        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1446       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1447
1448     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1449     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1450     (gnus-simplify-buffer-fuzzy-step " $")
1451     (gnus-simplify-buffer-fuzzy-step "^ +")))
1452
1453 (defun gnus-simplify-subject-fuzzy (subject)
1454   "Simplify a subject string fuzzily.
1455 See `gnus-simplify-buffer-fuzzy' for details."
1456   (save-excursion
1457     (gnus-set-work-buffer)
1458     (let ((case-fold-search t))
1459       ;; Remove uninteresting prefixes.
1460       (when (and gnus-simplify-ignored-prefixes
1461                  (string-match gnus-simplify-ignored-prefixes subject))
1462         (setq subject (substring subject (match-end 0))))
1463       (insert subject)
1464       (inline (gnus-simplify-buffer-fuzzy))
1465       (buffer-string))))
1466
1467 (defsubst gnus-simplify-subject-fully (subject)
1468   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1469   (cond
1470    (gnus-simplify-subject-functions
1471     (gnus-map-function gnus-simplify-subject-functions subject))
1472    ((null gnus-summary-gather-subject-limit)
1473     (gnus-simplify-subject-re subject))
1474    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1475     (gnus-simplify-subject-fuzzy subject))
1476    ((numberp gnus-summary-gather-subject-limit)
1477     (gnus-limit-string (gnus-simplify-subject-re subject)
1478                        gnus-summary-gather-subject-limit))
1479    (t
1480     subject)))
1481
1482 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1483   "Check whether two subjects are equal.
1484 If optional argument simple-first is t, first argument is already
1485 simplified."
1486   (cond
1487    ((null simple-first)
1488     (equal (gnus-simplify-subject-fully s1)
1489            (gnus-simplify-subject-fully s2)))
1490    (t
1491     (equal s1
1492            (gnus-simplify-subject-fully s2)))))
1493
1494 (defun gnus-summary-bubble-group ()
1495   "Increase the score of the current group.
1496 This is a handy function to add to `gnus-summary-exit-hook' to
1497 increase the score of each group you read."
1498   (gnus-group-add-score gnus-newsgroup-name))
1499
1500 \f
1501 ;;;
1502 ;;; Gnus summary mode
1503 ;;;
1504
1505 (put 'gnus-summary-mode 'mode-class 'special)
1506
1507 (defvar gnus-article-commands-menu)
1508
1509 (when t
1510   ;; Non-orthogonal keys
1511
1512   (gnus-define-keys gnus-summary-mode-map
1513     " " gnus-summary-next-page
1514     "\177" gnus-summary-prev-page
1515     [delete] gnus-summary-prev-page
1516     [backspace] gnus-summary-prev-page
1517     "\r" gnus-summary-scroll-up
1518     "\M-\r" gnus-summary-scroll-down
1519     "n" gnus-summary-next-unread-article
1520     "p" gnus-summary-prev-unread-article
1521     "N" gnus-summary-next-article
1522     "P" gnus-summary-prev-article
1523     "\M-\C-n" gnus-summary-next-same-subject
1524     "\M-\C-p" gnus-summary-prev-same-subject
1525     "\M-n" gnus-summary-next-unread-subject
1526     "\M-p" gnus-summary-prev-unread-subject
1527     "." gnus-summary-first-unread-article
1528     "," gnus-summary-best-unread-article
1529     "\M-s" gnus-summary-search-article-forward
1530     "\M-r" gnus-summary-search-article-backward
1531     "<" gnus-summary-beginning-of-article
1532     ">" gnus-summary-end-of-article
1533     "j" gnus-summary-goto-article
1534     "^" gnus-summary-refer-parent-article
1535     "\M-^" gnus-summary-refer-article
1536     "u" gnus-summary-tick-article-forward
1537     "!" gnus-summary-tick-article-forward
1538     "U" gnus-summary-tick-article-backward
1539     "d" gnus-summary-mark-as-read-forward
1540     "D" gnus-summary-mark-as-read-backward
1541     "E" gnus-summary-mark-as-expirable
1542     "\M-u" gnus-summary-clear-mark-forward
1543     "\M-U" gnus-summary-clear-mark-backward
1544     "k" gnus-summary-kill-same-subject-and-select
1545     "\C-k" gnus-summary-kill-same-subject
1546     "\M-\C-k" gnus-summary-kill-thread
1547     "\M-\C-l" gnus-summary-lower-thread
1548     "e" gnus-summary-edit-article
1549     "#" gnus-summary-mark-as-processable
1550     "\M-#" gnus-summary-unmark-as-processable
1551     "\M-\C-t" gnus-summary-toggle-threads
1552     "\M-\C-s" gnus-summary-show-thread
1553     "\M-\C-h" gnus-summary-hide-thread
1554     "\M-\C-f" gnus-summary-next-thread
1555     "\M-\C-b" gnus-summary-prev-thread
1556     [(meta down)] gnus-summary-next-thread
1557     [(meta up)] gnus-summary-prev-thread
1558     "\M-\C-u" gnus-summary-up-thread
1559     "\M-\C-d" gnus-summary-down-thread
1560     "&" gnus-summary-execute-command
1561     "c" gnus-summary-catchup-and-exit
1562     "\C-w" gnus-summary-mark-region-as-read
1563     "\C-t" gnus-summary-toggle-truncation
1564     "?" gnus-summary-mark-as-dormant
1565     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1566     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1567     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1568     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1569     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1570     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1571     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1572     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1573     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1574     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1575     "=" gnus-summary-expand-window
1576     "\C-x\C-s" gnus-summary-reselect-current-group
1577     "\M-g" gnus-summary-rescan-group
1578     "w" gnus-summary-stop-page-breaking
1579     "\C-c\C-r" gnus-summary-caesar-message
1580     "f" gnus-summary-followup
1581     "F" gnus-summary-followup-with-original
1582     "C" gnus-summary-cancel-article
1583     "r" gnus-summary-reply
1584     "R" gnus-summary-reply-with-original
1585     "\C-c\C-f" gnus-summary-mail-forward
1586     "o" gnus-summary-save-article
1587     "\C-o" gnus-summary-save-article-mail
1588     "|" gnus-summary-pipe-output
1589     "\M-k" gnus-summary-edit-local-kill
1590     "\M-K" gnus-summary-edit-global-kill
1591     ;; "V" gnus-version
1592     "\C-c\C-d" gnus-summary-describe-group
1593     "q" gnus-summary-exit
1594     "Q" gnus-summary-exit-no-update
1595     "\C-c\C-i" gnus-info-find-node
1596     gnus-mouse-2 gnus-mouse-pick-article
1597     "m" gnus-summary-mail-other-window
1598     "a" gnus-summary-post-news
1599     "i" gnus-summary-news-other-window
1600     "x" gnus-summary-limit-to-unread
1601     "s" gnus-summary-isearch-article
1602     "t" gnus-summary-toggle-header
1603     "g" gnus-summary-show-article
1604     "l" gnus-summary-goto-last-article
1605     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1606     "\C-d" gnus-summary-enter-digest-group
1607     "\M-\C-d" gnus-summary-read-document
1608     "\M-\C-e" gnus-summary-edit-parameters
1609     "\M-\C-a" gnus-summary-customize-parameters
1610     "\C-c\C-b" gnus-bug
1611     "*" gnus-cache-enter-article
1612     "\M-*" gnus-cache-remove-article
1613     "\M-&" gnus-summary-universal-argument
1614     "\C-l" gnus-recenter
1615     "I" gnus-summary-increase-score
1616     "L" gnus-summary-lower-score
1617     "\M-i" gnus-symbolic-argument
1618     "h" gnus-summary-select-article-buffer
1619
1620     "b" gnus-article-view-part
1621     "\M-t" gnus-summary-toggle-display-buttonized
1622
1623     "V" gnus-summary-score-map
1624     "X" gnus-uu-extract-map
1625     "S" gnus-summary-send-map)
1626
1627   ;; Sort of orthogonal keymap
1628   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1629     "t" gnus-summary-tick-article-forward
1630     "!" gnus-summary-tick-article-forward
1631     "d" gnus-summary-mark-as-read-forward
1632     "r" gnus-summary-mark-as-read-forward
1633     "c" gnus-summary-clear-mark-forward
1634     " " gnus-summary-clear-mark-forward
1635     "e" gnus-summary-mark-as-expirable
1636     "x" gnus-summary-mark-as-expirable
1637     "?" gnus-summary-mark-as-dormant
1638     "b" gnus-summary-set-bookmark
1639     "B" gnus-summary-remove-bookmark
1640     "#" gnus-summary-mark-as-processable
1641     "\M-#" gnus-summary-unmark-as-processable
1642     "S" gnus-summary-limit-include-expunged
1643     "C" gnus-summary-catchup
1644     "H" gnus-summary-catchup-to-here
1645     "h" gnus-summary-catchup-from-here
1646     "\C-c" gnus-summary-catchup-all
1647     "k" gnus-summary-kill-same-subject-and-select
1648     "K" gnus-summary-kill-same-subject
1649     "P" gnus-uu-mark-map)
1650
1651   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1652     "c" gnus-summary-clear-above
1653     "u" gnus-summary-tick-above
1654     "m" gnus-summary-mark-above
1655     "k" gnus-summary-kill-below)
1656
1657   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1658     "/" gnus-summary-limit-to-subject
1659     "n" gnus-summary-limit-to-articles
1660     "w" gnus-summary-pop-limit
1661     "s" gnus-summary-limit-to-subject
1662     "a" gnus-summary-limit-to-author
1663     "u" gnus-summary-limit-to-unread
1664     "m" gnus-summary-limit-to-marks
1665     "M" gnus-summary-limit-exclude-marks
1666     "v" gnus-summary-limit-to-score
1667     "*" gnus-summary-limit-include-cached
1668     "D" gnus-summary-limit-include-dormant
1669     "T" gnus-summary-limit-include-thread
1670     "d" gnus-summary-limit-exclude-dormant
1671     "t" gnus-summary-limit-to-age
1672     "." gnus-summary-limit-to-unseen
1673     "x" gnus-summary-limit-to-extra
1674     "p" gnus-summary-limit-to-display-predicate
1675     "E" gnus-summary-limit-include-expunged
1676     "c" gnus-summary-limit-exclude-childless-dormant
1677     "C" gnus-summary-limit-mark-excluded-as-read
1678     "o" gnus-summary-insert-old-articles
1679     "N" gnus-summary-insert-new-articles)
1680
1681   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1682     "n" gnus-summary-next-unread-article
1683     "p" gnus-summary-prev-unread-article
1684     "N" gnus-summary-next-article
1685     "P" gnus-summary-prev-article
1686     "\C-n" gnus-summary-next-same-subject
1687     "\C-p" gnus-summary-prev-same-subject
1688     "\M-n" gnus-summary-next-unread-subject
1689     "\M-p" gnus-summary-prev-unread-subject
1690     "f" gnus-summary-first-unread-article
1691     "b" gnus-summary-best-unread-article
1692     "j" gnus-summary-goto-article
1693     "g" gnus-summary-goto-subject
1694     "l" gnus-summary-goto-last-article
1695     "o" gnus-summary-pop-article)
1696
1697   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1698     "k" gnus-summary-kill-thread
1699     "l" gnus-summary-lower-thread
1700     "i" gnus-summary-raise-thread
1701     "T" gnus-summary-toggle-threads
1702     "t" gnus-summary-rethread-current
1703     "^" gnus-summary-reparent-thread
1704     "s" gnus-summary-show-thread
1705     "S" gnus-summary-show-all-threads
1706     "h" gnus-summary-hide-thread
1707     "H" gnus-summary-hide-all-threads
1708     "n" gnus-summary-next-thread
1709     "p" gnus-summary-prev-thread
1710     "u" gnus-summary-up-thread
1711     "o" gnus-summary-top-thread
1712     "d" gnus-summary-down-thread
1713     "#" gnus-uu-mark-thread
1714     "\M-#" gnus-uu-unmark-thread)
1715
1716   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1717     "g" gnus-summary-prepare
1718     "c" gnus-summary-insert-cached-articles
1719     "d" gnus-summary-insert-dormant-articles)
1720
1721   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1722     "c" gnus-summary-catchup-and-exit
1723     "C" gnus-summary-catchup-all-and-exit
1724     "E" gnus-summary-exit-no-update
1725     "Q" gnus-summary-exit
1726     "Z" gnus-summary-exit
1727     "n" gnus-summary-catchup-and-goto-next-group
1728     "R" gnus-summary-reselect-current-group
1729     "G" gnus-summary-rescan-group
1730     "N" gnus-summary-next-group
1731     "s" gnus-summary-save-newsrc
1732     "P" gnus-summary-prev-group)
1733
1734   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1735     " " gnus-summary-next-page
1736     "n" gnus-summary-next-page
1737     "\177" gnus-summary-prev-page
1738     [delete] gnus-summary-prev-page
1739     "p" gnus-summary-prev-page
1740     "\r" gnus-summary-scroll-up
1741     "\M-\r" gnus-summary-scroll-down
1742     "<" gnus-summary-beginning-of-article
1743     ">" gnus-summary-end-of-article
1744     "b" gnus-summary-beginning-of-article
1745     "e" gnus-summary-end-of-article
1746     "^" gnus-summary-refer-parent-article
1747     "r" gnus-summary-refer-parent-article
1748     "D" gnus-summary-enter-digest-group
1749     "R" gnus-summary-refer-references
1750     "T" gnus-summary-refer-thread
1751     "g" gnus-summary-show-article
1752     "s" gnus-summary-isearch-article
1753     "P" gnus-summary-print-article
1754     "M" gnus-mailing-list-insinuate
1755     "t" gnus-article-babel)
1756
1757   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1758     "b" gnus-article-add-buttons
1759     "B" gnus-article-add-buttons-to-head
1760     "o" gnus-article-treat-overstrike
1761     "e" gnus-article-emphasize
1762     "w" gnus-article-fill-cited-article
1763     "Q" gnus-article-fill-long-lines
1764     "C" gnus-article-capitalize-sentences
1765     "c" gnus-article-remove-cr
1766     "q" gnus-article-de-quoted-unreadable
1767     "6" gnus-article-de-base64-unreadable
1768     "Z" gnus-article-decode-HZ
1769     "h" gnus-article-wash-html
1770     "u" gnus-article-unsplit-urls
1771     "s" gnus-summary-force-verify-and-decrypt
1772     "f" gnus-article-display-x-face
1773     "l" gnus-summary-stop-page-breaking
1774     "r" gnus-summary-caesar-message
1775     "m" gnus-summary-morse-message
1776     "t" gnus-summary-toggle-header
1777     "g" gnus-treat-smiley
1778     "v" gnus-summary-verbose-headers
1779     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1780     "p" gnus-article-verify-x-pgp-sig
1781     "d" gnus-article-treat-dumbquotes
1782     "k" gnus-article-outlook-deuglify-article)
1783
1784   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1785     "a" gnus-article-hide
1786     "h" gnus-article-hide-headers
1787     "b" gnus-article-hide-boring-headers
1788     "s" gnus-article-hide-signature
1789     "c" gnus-article-hide-citation
1790     "C" gnus-article-hide-citation-in-followups
1791     "l" gnus-article-hide-list-identifiers
1792     "p" gnus-article-hide-pgp
1793     "B" gnus-article-strip-banner
1794     "P" gnus-article-hide-pem
1795     "\C-c" gnus-article-hide-citation-maybe)
1796
1797   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1798     "a" gnus-article-highlight
1799     "h" gnus-article-highlight-headers
1800     "c" gnus-article-highlight-citation
1801     "s" gnus-article-highlight-signature)
1802
1803   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1804     "f" gnus-article-treat-fold-headers
1805     "u" gnus-article-treat-unfold-headers
1806     "n" gnus-article-treat-fold-newsgroups)
1807
1808   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1809     "x" gnus-article-display-x-face
1810     "s" gnus-treat-smiley
1811     "D" gnus-article-remove-images
1812     "f" gnus-treat-from-picon
1813     "m" gnus-treat-mail-picon
1814     "n" gnus-treat-newsgroups-picon)
1815
1816   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1817     "w" gnus-article-decode-mime-words
1818     "c" gnus-article-decode-charset
1819     "v" gnus-mime-view-all-parts
1820     "b" gnus-article-view-part)
1821
1822   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1823     "z" gnus-article-date-ut
1824     "u" gnus-article-date-ut
1825     "l" gnus-article-date-local
1826     "p" gnus-article-date-english
1827     "e" gnus-article-date-lapsed
1828     "o" gnus-article-date-original
1829     "i" gnus-article-date-iso8601
1830     "s" gnus-article-date-user)
1831
1832   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1833     "t" gnus-article-remove-trailing-blank-lines
1834     "l" gnus-article-strip-leading-blank-lines
1835     "m" gnus-article-strip-multiple-blank-lines
1836     "a" gnus-article-strip-blank-lines
1837     "A" gnus-article-strip-all-blank-lines
1838     "s" gnus-article-strip-leading-space
1839     "e" gnus-article-strip-trailing-space
1840     "w" gnus-article-remove-leading-whitespace)
1841
1842   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1843     "v" gnus-version
1844     "f" gnus-summary-fetch-faq
1845     "d" gnus-summary-describe-group
1846     "h" gnus-summary-describe-briefly
1847     "i" gnus-info-find-node
1848     "c" gnus-group-fetch-charter
1849     "C" gnus-group-fetch-control)
1850
1851   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1852     "e" gnus-summary-expire-articles
1853     "\M-\C-e" gnus-summary-expire-articles-now
1854     "\177" gnus-summary-delete-article
1855     [delete] gnus-summary-delete-article
1856     [backspace] gnus-summary-delete-article
1857     "m" gnus-summary-move-article
1858     "r" gnus-summary-respool-article
1859     "w" gnus-summary-edit-article
1860     "c" gnus-summary-copy-article
1861     "B" gnus-summary-crosspost-article
1862     "q" gnus-summary-respool-query
1863     "t" gnus-summary-respool-trace
1864     "i" gnus-summary-import-article
1865     "I" gnus-summary-create-article
1866     "p" gnus-summary-article-posted-p)
1867
1868   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1869     "o" gnus-summary-save-article
1870     "m" gnus-summary-save-article-mail
1871     "F" gnus-summary-write-article-file
1872     "r" gnus-summary-save-article-rmail
1873     "f" gnus-summary-save-article-file
1874     "b" gnus-summary-save-article-body-file
1875     "h" gnus-summary-save-article-folder
1876     "v" gnus-summary-save-article-vm
1877     "p" gnus-summary-pipe-output
1878     "P" gnus-summary-muttprint
1879     "s" gnus-soup-add-article)
1880
1881   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1882     "b" gnus-summary-display-buttonized
1883     "m" gnus-summary-repair-multipart
1884     "v" gnus-article-view-part
1885     "o" gnus-article-save-part
1886     "c" gnus-article-copy-part
1887     "C" gnus-article-view-part-as-charset
1888     "e" gnus-article-view-part-externally
1889     "E" gnus-article-encrypt-body
1890     "i" gnus-article-inline-part
1891     "|" gnus-article-pipe-part)
1892
1893   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1894     "p" gnus-summary-mark-as-processable
1895     "u" gnus-summary-unmark-as-processable
1896     "U" gnus-summary-unmark-all-processable
1897     "v" gnus-uu-mark-over
1898     "s" gnus-uu-mark-series
1899     "r" gnus-uu-mark-region
1900     "g" gnus-uu-unmark-region
1901     "R" gnus-uu-mark-by-regexp
1902     "G" gnus-uu-unmark-by-regexp
1903     "t" gnus-uu-mark-thread
1904     "T" gnus-uu-unmark-thread
1905     "a" gnus-uu-mark-all
1906     "b" gnus-uu-mark-buffer
1907     "S" gnus-uu-mark-sparse
1908     "k" gnus-summary-kill-process-mark
1909     "y" gnus-summary-yank-process-mark
1910     "w" gnus-summary-save-process-mark
1911     "i" gnus-uu-invert-processable)
1912
1913   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1914     ;;"x" gnus-uu-extract-any
1915     "m" gnus-summary-save-parts
1916     "u" gnus-uu-decode-uu
1917     "U" gnus-uu-decode-uu-and-save
1918     "s" gnus-uu-decode-unshar
1919     "S" gnus-uu-decode-unshar-and-save
1920     "o" gnus-uu-decode-save
1921     "O" gnus-uu-decode-save
1922     "b" gnus-uu-decode-binhex
1923     "B" gnus-uu-decode-binhex
1924     "p" gnus-uu-decode-postscript
1925     "P" gnus-uu-decode-postscript-and-save)
1926
1927   (gnus-define-keys
1928       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1929     "u" gnus-uu-decode-uu-view
1930     "U" gnus-uu-decode-uu-and-save-view
1931     "s" gnus-uu-decode-unshar-view
1932     "S" gnus-uu-decode-unshar-and-save-view
1933     "o" gnus-uu-decode-save-view
1934     "O" gnus-uu-decode-save-view
1935     "b" gnus-uu-decode-binhex-view
1936     "B" gnus-uu-decode-binhex-view
1937     "p" gnus-uu-decode-postscript-view
1938     "P" gnus-uu-decode-postscript-and-save-view))
1939
1940 (defvar gnus-article-post-menu nil)
1941
1942 (defconst gnus-summary-menu-maxlen 20)
1943
1944 (defun gnus-summary-menu-split (menu)
1945   ;; If we have lots of elements, divide them into groups of 20
1946   ;; and make a pane (or submenu) for each one.
1947   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1948       (let ((menu menu) sublists next
1949             (i 1))
1950         (while menu
1951           ;; Pull off the next gnus-summary-menu-maxlen elements
1952           ;; and make them the next element of sublist.
1953           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1954           (if next
1955               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1956                       nil))
1957           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1958                                              (aref (car (last menu)) 0)) menu)
1959                                sublists))
1960           (setq i (1+ i))
1961           (setq menu next))
1962         (nreverse sublists))
1963     ;; Few elements--put them all in one pane.
1964     menu))
1965
1966 (defun gnus-summary-make-menu-bar ()
1967   (gnus-turn-off-edit-menu 'summary)
1968
1969   (unless (boundp 'gnus-summary-misc-menu)
1970
1971     (easy-menu-define
1972       gnus-summary-kill-menu gnus-summary-mode-map ""
1973       (cons
1974        "Score"
1975        (nconc
1976         (list
1977          ["Customize" gnus-score-customize t])
1978         (gnus-make-score-map 'increase)
1979         (gnus-make-score-map 'lower)
1980         '(("Mark"
1981            ["Kill below" gnus-summary-kill-below t]
1982            ["Mark above" gnus-summary-mark-above t]
1983            ["Tick above" gnus-summary-tick-above t]
1984            ["Clear above" gnus-summary-clear-above t])
1985           ["Current score" gnus-summary-current-score t]
1986           ["Set score" gnus-summary-set-score t]
1987           ["Switch current score file..." gnus-score-change-score-file t]
1988           ["Set mark below..." gnus-score-set-mark-below t]
1989           ["Set expunge below..." gnus-score-set-expunge-below t]
1990           ["Edit current score file" gnus-score-edit-current-scores t]
1991           ["Edit score file" gnus-score-edit-file t]
1992           ["Trace score" gnus-score-find-trace t]
1993           ["Find words" gnus-score-find-favourite-words t]
1994           ["Rescore buffer" gnus-summary-rescore t]
1995           ["Increase score..." gnus-summary-increase-score t]
1996           ["Lower score..." gnus-summary-lower-score t]))))
1997
1998     ;; Define both the Article menu in the summary buffer and the
1999     ;; equivalent Commands menu in the article buffer here for
2000     ;; consistency.
2001     (let ((innards
2002            `(("Hide"
2003               ["All" gnus-article-hide t]
2004               ["Headers" gnus-article-hide-headers t]
2005               ["Signature" gnus-article-hide-signature t]
2006               ["Citation" gnus-article-hide-citation t]
2007               ["List identifiers" gnus-article-hide-list-identifiers t]
2008               ["PGP" gnus-article-hide-pgp t]
2009               ["Banner" gnus-article-strip-banner t]
2010               ["Boring headers" gnus-article-hide-boring-headers t])
2011              ("Highlight"
2012               ["All" gnus-article-highlight t]
2013               ["Headers" gnus-article-highlight-headers t]
2014               ["Signature" gnus-article-highlight-signature t]
2015               ["Citation" gnus-article-highlight-citation t])
2016              ("MIME"
2017               ["Words" gnus-article-decode-mime-words t]
2018               ["Charset" gnus-article-decode-charset t]
2019               ["QP" gnus-article-de-quoted-unreadable t]
2020               ["Base64" gnus-article-de-base64-unreadable t]
2021               ["View MIME buttons" gnus-summary-display-buttonized t]
2022               ["View all" gnus-mime-view-all-parts t]
2023               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2024               ["Encrypt body" gnus-article-encrypt-body t]
2025               ["Extract all parts" gnus-summary-save-parts t])
2026              ("Date"
2027               ["Local" gnus-article-date-local t]
2028               ["ISO8601" gnus-article-date-iso8601 t]
2029               ["UT" gnus-article-date-ut t]
2030               ["Original" gnus-article-date-original t]
2031               ["Lapsed" gnus-article-date-lapsed t]
2032               ["User-defined" gnus-article-date-user t])
2033              ("Display"
2034               ["Remove images" gnus-article-remove-images t]
2035               ["Toggle smiley" gnus-treat-smiley t]
2036               ["Show X-Face" gnus-article-display-x-face t]
2037               ["Show picons in From" gnus-treat-from-picon t]
2038               ["Show picons in mail headers" gnus-treat-mail-picon t]
2039               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2040               ("View as different encoding"
2041                ,@(gnus-summary-menu-split
2042                   (mapcar
2043                    (lambda (cs)
2044                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2045                      ;; forms for menu commands, we should provide intern'ed
2046                      ;; function symbols.
2047                      (let ((command (intern (format "\
2048 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2049                        (fset command
2050                              `(lambda ()
2051                                 (interactive)
2052                                 (let ((gnus-summary-show-article-charset-alist
2053                                        '((1 . ,cs))))
2054                                   (gnus-summary-show-article 1))))
2055                        `[,(symbol-name cs) ,command t]))
2056                    (sort (if (fboundp 'coding-system-list)
2057                              (coding-system-list)
2058                            (mapcar 'car mm-mime-mule-charset-alist))
2059                          'string<)))))
2060              ("Washing"
2061               ("Remove Blanks"
2062                ["Leading" gnus-article-strip-leading-blank-lines t]
2063                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2064                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2065                ["All of the above" gnus-article-strip-blank-lines t]
2066                ["All" gnus-article-strip-all-blank-lines t]
2067                ["Leading space" gnus-article-strip-leading-space t]
2068                ["Trailing space" gnus-article-strip-trailing-space t]
2069                ["Leading space in headers"
2070                 gnus-article-remove-leading-whitespace t])
2071               ["Overstrike" gnus-article-treat-overstrike t]
2072               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2073               ["Emphasis" gnus-article-emphasize t]
2074               ["Word wrap" gnus-article-fill-cited-article t]
2075               ["Fill long lines" gnus-article-fill-long-lines t]
2076               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2077               ["CR" gnus-article-remove-cr t]
2078               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2079               ["Base64" gnus-article-de-base64-unreadable t]
2080               ["Rot 13" gnus-summary-caesar-message
2081                ,@(if (featurep 'xemacs) '(t)
2082                    '(:help "\"Caesar rotate\" article by 13"))]
2083               ["Morse decode" gnus-summary-morse-message t]
2084               ["Unix pipe..." gnus-summary-pipe-message t]
2085               ["Add buttons" gnus-article-add-buttons t]
2086               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2087               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2088               ["Verbose header" gnus-summary-verbose-headers t]
2089               ["Toggle header" gnus-summary-toggle-header t]
2090               ["Unfold headers" gnus-article-treat-unfold-headers t]
2091               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2092               ["Html" gnus-article-wash-html t]
2093               ["URLs" gnus-article-unsplit-urls t]
2094               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2095               ["HZ" gnus-article-decode-HZ t]
2096               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2097               )
2098              ("Output"
2099               ["Save in default format" gnus-summary-save-article
2100                ,@(if (featurep 'xemacs) '(t)
2101                    '(:help "Save article using default method"))]
2102               ["Save in file" gnus-summary-save-article-file
2103                ,@(if (featurep 'xemacs) '(t)
2104                    '(:help "Save article in file"))]
2105               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2106               ["Save in MH folder" gnus-summary-save-article-folder t]
2107               ["Save in VM folder" gnus-summary-save-article-vm t]
2108               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2109               ["Save body in file" gnus-summary-save-article-body-file t]
2110               ["Pipe through a filter" gnus-summary-pipe-output t]
2111               ["Add to SOUP packet" gnus-soup-add-article t]
2112               ["Print with Muttprint" gnus-summary-muttprint t]
2113               ["Print" gnus-summary-print-article t])
2114              ("Backend"
2115               ["Respool article..." gnus-summary-respool-article t]
2116               ["Move article..." gnus-summary-move-article
2117                (gnus-check-backend-function
2118                 'request-move-article gnus-newsgroup-name)]
2119               ["Copy article..." gnus-summary-copy-article t]
2120               ["Crosspost article..." gnus-summary-crosspost-article
2121                (gnus-check-backend-function
2122                 'request-replace-article gnus-newsgroup-name)]
2123               ["Import file..." gnus-summary-import-article t]
2124               ["Create article..." gnus-summary-create-article t]
2125               ["Check if posted" gnus-summary-article-posted-p t]
2126               ["Edit article" gnus-summary-edit-article
2127                (not (gnus-group-read-only-p))]
2128               ["Delete article" gnus-summary-delete-article
2129                (gnus-check-backend-function
2130                 'request-expire-articles gnus-newsgroup-name)]
2131               ["Query respool" gnus-summary-respool-query t]
2132               ["Trace respool" gnus-summary-respool-trace t]
2133               ["Delete expirable articles" gnus-summary-expire-articles-now
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)])
2136              ("Extract"
2137               ["Uudecode" gnus-uu-decode-uu
2138                ,@(if (featurep 'xemacs) '(t)
2139                    '(:help "Decode uuencoded article(s)"))]
2140               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2141               ["Unshar" gnus-uu-decode-unshar t]
2142               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2143               ["Save" gnus-uu-decode-save t]
2144               ["Binhex" gnus-uu-decode-binhex t]
2145               ["Postscript" gnus-uu-decode-postscript t]
2146               ["All MIME parts" gnus-summary-save-parts t])
2147              ("Cache"
2148               ["Enter article" gnus-cache-enter-article t]
2149               ["Remove article" gnus-cache-remove-article t])
2150              ["Translate" gnus-article-babel t]
2151              ["Select article buffer" gnus-summary-select-article-buffer t]
2152              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2153              ["Isearch article..." gnus-summary-isearch-article t]
2154              ["Beginning of the article" gnus-summary-beginning-of-article t]
2155              ["End of the article" gnus-summary-end-of-article t]
2156              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2157              ["Fetch referenced articles" gnus-summary-refer-references t]
2158              ["Fetch current thread" gnus-summary-refer-thread t]
2159              ["Fetch article with id..." gnus-summary-refer-article t]
2160              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2161              ["Redisplay" gnus-summary-show-article t]
2162              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2163       (easy-menu-define
2164         gnus-summary-article-menu gnus-summary-mode-map ""
2165         (cons "Article" innards))
2166
2167       (if (not (keymapp gnus-summary-article-menu))
2168           (easy-menu-define
2169             gnus-article-commands-menu gnus-article-mode-map ""
2170             (cons "Commands" innards))
2171         ;; in Emacs, don't share menu.
2172         (setq gnus-article-commands-menu
2173               (copy-keymap gnus-summary-article-menu))
2174         (define-key gnus-article-mode-map [menu-bar commands]
2175           (cons "Commands" gnus-article-commands-menu))))
2176
2177     (easy-menu-define
2178       gnus-summary-thread-menu gnus-summary-mode-map ""
2179       '("Threads"
2180         ["Toggle threading" gnus-summary-toggle-threads t]
2181         ["Hide threads" gnus-summary-hide-all-threads t]
2182         ["Show threads" gnus-summary-show-all-threads t]
2183         ["Hide thread" gnus-summary-hide-thread t]
2184         ["Show thread" gnus-summary-show-thread t]
2185         ["Go to next thread" gnus-summary-next-thread t]
2186         ["Go to previous thread" gnus-summary-prev-thread t]
2187         ["Go down thread" gnus-summary-down-thread t]
2188         ["Go up thread" gnus-summary-up-thread t]
2189         ["Top of thread" gnus-summary-top-thread t]
2190         ["Mark thread as read" gnus-summary-kill-thread t]
2191         ["Lower thread score" gnus-summary-lower-thread t]
2192         ["Raise thread score" gnus-summary-raise-thread t]
2193         ["Rethread current" gnus-summary-rethread-current t]))
2194
2195     (easy-menu-define
2196       gnus-summary-post-menu gnus-summary-mode-map ""
2197       `("Post"
2198         ["Send a message (mail or news)" gnus-summary-post-news
2199          ,@(if (featurep 'xemacs) '(t)
2200              '(:help "Post an article"))]
2201         ["Followup" gnus-summary-followup
2202          ,@(if (featurep 'xemacs) '(t)
2203              '(:help "Post followup to this article"))]
2204         ["Followup and yank" gnus-summary-followup-with-original
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post followup to this article, quoting its contents"))]
2207         ["Supersede article" gnus-summary-supersede-article t]
2208         ["Cancel article" gnus-summary-cancel-article
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Cancel an article you posted"))]
2211         ["Reply" gnus-summary-reply t]
2212         ["Reply and yank" gnus-summary-reply-with-original t]
2213         ["Wide reply" gnus-summary-wide-reply t]
2214         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Mail a reply, quoting this article"))]
2217         ["Very wide reply" gnus-summary-very-wide-reply t]
2218         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Mail a very wide reply, quoting this article"))]
2221         ["Mail forward" gnus-summary-mail-forward t]
2222         ["Post forward" gnus-summary-post-forward t]
2223         ["Digest and mail" gnus-uu-digest-mail-forward t]
2224         ["Digest and post" gnus-uu-digest-post-forward t]
2225         ["Resend message" gnus-summary-resend-message t]
2226         ["Resend message edit" gnus-summary-resend-message-edit t]
2227         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2228         ["Send a mail" gnus-summary-mail-other-window t]
2229         ["Create a local message" gnus-summary-news-other-window t]
2230         ["Uuencode and post" gnus-uu-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post a uuencoded article"))]
2233         ["Followup via news" gnus-summary-followup-to-mail t]
2234         ["Followup via news and yank"
2235          gnus-summary-followup-to-mail-with-original t]
2236         ;;("Draft"
2237         ;;["Send" gnus-summary-send-draft t]
2238         ;;["Send bounced" gnus-resend-bounced-mail t])
2239         ))
2240
2241     (cond
2242      ((not (keymapp gnus-summary-post-menu))
2243       (setq gnus-article-post-menu gnus-summary-post-menu))
2244      ((not gnus-article-post-menu)
2245       ;; Don't share post menu.
2246       (setq gnus-article-post-menu
2247             (copy-keymap gnus-summary-post-menu))))
2248     (define-key gnus-article-mode-map [menu-bar post]
2249       (cons "Post" gnus-article-post-menu))
2250
2251     (easy-menu-define
2252       gnus-summary-misc-menu gnus-summary-mode-map ""
2253       `("Gnus"
2254         ("Mark Read"
2255          ["Mark as read" gnus-summary-mark-as-read-forward t]
2256          ["Mark same subject and select"
2257           gnus-summary-kill-same-subject-and-select t]
2258          ["Mark same subject" gnus-summary-kill-same-subject t]
2259          ["Catchup" gnus-summary-catchup
2260           ,@(if (featurep 'xemacs) '(t)
2261               '(:help "Mark unread articles in this group as read"))]
2262          ["Catchup all" gnus-summary-catchup-all t]
2263          ["Catchup to here" gnus-summary-catchup-to-here t]
2264          ["Catchup from here" gnus-summary-catchup-from-here t]
2265          ["Catchup region" gnus-summary-mark-region-as-read t]
2266          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2267         ("Mark Various"
2268          ["Tick" gnus-summary-tick-article-forward t]
2269          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2270          ["Remove marks" gnus-summary-clear-mark-forward t]
2271          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2272          ["Set bookmark" gnus-summary-set-bookmark t]
2273          ["Remove bookmark" gnus-summary-remove-bookmark t])
2274         ("Limit to"
2275          ["Marks..." gnus-summary-limit-to-marks t]
2276          ["Subject..." gnus-summary-limit-to-subject t]
2277          ["Author..." gnus-summary-limit-to-author t]
2278          ["Age..." gnus-summary-limit-to-age t]
2279          ["Extra..." gnus-summary-limit-to-extra t]
2280          ["Score..." gnus-summary-limit-to-score t]
2281          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2282          ["Unread" gnus-summary-limit-to-unread t]
2283          ["Unseen" gnus-summary-limit-to-unseen t]
2284          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2285          ["Next articles" gnus-summary-limit-to-articles t]
2286          ["Pop limit" gnus-summary-pop-limit t]
2287          ["Show dormant" gnus-summary-limit-include-dormant t]
2288          ["Hide childless dormant"
2289           gnus-summary-limit-exclude-childless-dormant t]
2290          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2291          ["Hide marked" gnus-summary-limit-exclude-marks t]
2292          ["Show expunged" gnus-summary-limit-include-expunged t])
2293         ("Process Mark"
2294          ["Set mark" gnus-summary-mark-as-processable t]
2295          ["Remove mark" gnus-summary-unmark-as-processable t]
2296          ["Remove all marks" gnus-summary-unmark-all-processable t]
2297          ["Mark above" gnus-uu-mark-over t]
2298          ["Mark series" gnus-uu-mark-series t]
2299          ["Mark region" gnus-uu-mark-region t]
2300          ["Unmark region" gnus-uu-unmark-region t]
2301          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2302          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2303          ["Mark all" gnus-uu-mark-all t]
2304          ["Mark buffer" gnus-uu-mark-buffer t]
2305          ["Mark sparse" gnus-uu-mark-sparse t]
2306          ["Mark thread" gnus-uu-mark-thread t]
2307          ["Unmark thread" gnus-uu-unmark-thread t]
2308          ("Process Mark Sets"
2309           ["Kill" gnus-summary-kill-process-mark t]
2310           ["Yank" gnus-summary-yank-process-mark
2311            gnus-newsgroup-process-stack]
2312           ["Save" gnus-summary-save-process-mark t]))
2313         ("Scroll article"
2314          ["Page forward" gnus-summary-next-page
2315           ,@(if (featurep 'xemacs) '(t)
2316               '(:help "Show next page of article"))]
2317          ["Page backward" gnus-summary-prev-page
2318           ,@(if (featurep 'xemacs) '(t)
2319               '(:help "Show previous page of article"))]
2320          ["Line forward" gnus-summary-scroll-up t])
2321         ("Move"
2322          ["Next unread article" gnus-summary-next-unread-article t]
2323          ["Previous unread article" gnus-summary-prev-unread-article t]
2324          ["Next article" gnus-summary-next-article t]
2325          ["Previous article" gnus-summary-prev-article t]
2326          ["Next unread subject" gnus-summary-next-unread-subject t]
2327          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2328          ["Next article same subject" gnus-summary-next-same-subject t]
2329          ["Previous article same subject" gnus-summary-prev-same-subject t]
2330          ["First unread article" gnus-summary-first-unread-article t]
2331          ["Best unread article" gnus-summary-best-unread-article t]
2332          ["Go to subject number..." gnus-summary-goto-subject t]
2333          ["Go to article number..." gnus-summary-goto-article t]
2334          ["Go to the last article" gnus-summary-goto-last-article t]
2335          ["Pop article off history" gnus-summary-pop-article t])
2336         ("Sort"
2337          ["Sort by number" gnus-summary-sort-by-number t]
2338          ["Sort by author" gnus-summary-sort-by-author t]
2339          ["Sort by subject" gnus-summary-sort-by-subject t]
2340          ["Sort by date" gnus-summary-sort-by-date t]
2341          ["Sort by score" gnus-summary-sort-by-score t]
2342          ["Sort by lines" gnus-summary-sort-by-lines t]
2343          ["Sort by characters" gnus-summary-sort-by-chars t]
2344          ["Randomize" gnus-summary-sort-by-random t]
2345          ["Original sort" gnus-summary-sort-by-original t])
2346         ("Help"
2347          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2348          ["Describe group" gnus-summary-describe-group t]
2349          ["Fetch charter" gnus-group-fetch-charter
2350           ,@(if (featurep 'xemacs) nil
2351               '(:help "Display the charter of the current group"))]
2352          ["Fetch control message" gnus-group-fetch-control
2353           ,@(if (featurep 'xemacs) nil
2354               '(:help "Display the archived control message for the current group"))]
2355          ["Read manual" gnus-info-find-node t])
2356         ("Modes"
2357          ["Pick and read" gnus-pick-mode t]
2358          ["Binary" gnus-binary-mode t])
2359         ("Regeneration"
2360          ["Regenerate" gnus-summary-prepare t]
2361          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2362          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2363          ["Toggle threading" gnus-summary-toggle-threads t])
2364         ["See old articles" gnus-summary-insert-old-articles t]
2365         ["See new articles" gnus-summary-insert-new-articles t]
2366         ["Filter articles..." gnus-summary-execute-command t]
2367         ["Run command on subjects..." gnus-summary-universal-argument t]
2368         ["Search articles forward..." gnus-summary-search-article-forward t]
2369         ["Search articles backward..." gnus-summary-search-article-backward t]
2370         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2371         ["Expand window" gnus-summary-expand-window t]
2372         ["Expire expirable articles" gnus-summary-expire-articles
2373          (gnus-check-backend-function
2374           'request-expire-articles gnus-newsgroup-name)]
2375         ["Edit local kill file" gnus-summary-edit-local-kill t]
2376         ["Edit main kill file" gnus-summary-edit-global-kill t]
2377         ["Edit group parameters" gnus-summary-edit-parameters t]
2378         ["Customize group parameters" gnus-summary-customize-parameters t]
2379         ["Send a bug report" gnus-bug t]
2380         ("Exit"
2381          ["Catchup and exit" gnus-summary-catchup-and-exit
2382           ,@(if (featurep 'xemacs) '(t)
2383               '(:help "Mark unread articles in this group as read, then exit"))]
2384          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2385          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2386          ["Exit group" gnus-summary-exit
2387           ,@(if (featurep 'xemacs) '(t)
2388               '(:help "Exit current group, return to group selection mode"))]
2389          ["Exit group without updating" gnus-summary-exit-no-update t]
2390          ["Exit and goto next group" gnus-summary-next-group t]
2391          ["Exit and goto prev group" gnus-summary-prev-group t]
2392          ["Reselect group" gnus-summary-reselect-current-group t]
2393          ["Rescan group" gnus-summary-rescan-group t]
2394          ["Update dribble" gnus-summary-save-newsrc t])))
2395
2396     (gnus-run-hooks 'gnus-summary-menu-hook)))
2397
2398 (defvar gnus-summary-tool-bar-map nil)
2399
2400 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2401 (defun gnus-summary-make-tool-bar ()
2402   (if (and (fboundp 'tool-bar-add-item-from-menu)
2403            (default-value 'tool-bar-mode)
2404            (not gnus-summary-tool-bar-map))
2405       (setq gnus-summary-tool-bar-map
2406             (let ((tool-bar-map (make-sparse-keymap))
2407                   (load-path (mm-image-load-path)))
2408               (tool-bar-add-item-from-menu
2409                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2410               (tool-bar-add-item-from-menu
2411                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2412               (tool-bar-add-item-from-menu
2413                'gnus-summary-post-news "post" gnus-summary-mode-map)
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-followup "followup" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-reply "reply" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2438               tool-bar-map)))
2439   (if gnus-summary-tool-bar-map
2440       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2441
2442 (defun gnus-score-set-default (var value)
2443   "A version of set that updates the GNU Emacs menu-bar."
2444   (set var value)
2445   ;; It is the message that forces the active status to be updated.
2446   (message ""))
2447
2448 (defun gnus-make-score-map (type)
2449   "Make a summary score map of type TYPE."
2450   (if t
2451       nil
2452     (let ((headers '(("author" "from" string)
2453                      ("subject" "subject" string)
2454                      ("article body" "body" string)
2455                      ("article head" "head" string)
2456                      ("xref" "xref" string)
2457                      ("extra header" "extra" string)
2458                      ("lines" "lines" number)
2459                      ("followups to author" "followup" string)))
2460           (types '((number ("less than" <)
2461                            ("greater than" >)
2462                            ("equal" =))
2463                    (string ("substring" s)
2464                            ("exact string" e)
2465                            ("fuzzy string" f)
2466                            ("regexp" r))))
2467           (perms '(("temporary" (current-time-string))
2468                    ("permanent" nil)
2469                    ("immediate" now)))
2470           header)
2471       (list
2472        (apply
2473         'nconc
2474         (list
2475          (if (eq type 'lower)
2476              "Lower score"
2477            "Increase score"))
2478         (let (outh)
2479           (while headers
2480             (setq header (car headers))
2481             (setq outh
2482                   (cons
2483                    (apply
2484                     'nconc
2485                     (list (car header))
2486                     (let ((ts (cdr (assoc (nth 2 header) types)))
2487                           outt)
2488                       (while ts
2489                         (setq outt
2490                               (cons
2491                                (apply
2492                                 'nconc
2493                                 (list (caar ts))
2494                                 (let ((ps perms)
2495                                       outp)
2496                                   (while ps
2497                                     (setq outp
2498                                           (cons
2499                                            (vector
2500                                             (caar ps)
2501                                             (list
2502                                              'gnus-summary-score-entry
2503                                              (nth 1 header)
2504                                              (if (or (string= (nth 1 header)
2505                                                               "head")
2506                                                      (string= (nth 1 header)
2507                                                               "body"))
2508                                                  ""
2509                                                (list 'gnus-summary-header
2510                                                      (nth 1 header)))
2511                                              (list 'quote (nth 1 (car ts)))
2512                                              (list 'gnus-score-delta-default
2513                                                    nil)
2514                                              (nth 1 (car ps))
2515                                              t)
2516                                             t)
2517                                            outp))
2518                                     (setq ps (cdr ps)))
2519                                   (list (nreverse outp))))
2520                                outt))
2521                         (setq ts (cdr ts)))
2522                       (list (nreverse outt))))
2523                    outh))
2524             (setq headers (cdr headers)))
2525           (list (nreverse outh))))))))
2526
2527 \f
2528
2529 (defun gnus-summary-mode (&optional group)
2530   "Major mode for reading articles.
2531
2532 All normal editing commands are switched off.
2533 \\<gnus-summary-mode-map>
2534 Each line in this buffer represents one article.  To read an
2535 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2536 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2537 respectively.
2538
2539 You can also post articles and send mail from this buffer.  To
2540 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2541 of an article, type `\\[gnus-summary-reply]'.
2542
2543 There are approx. one gazillion commands you can execute in this
2544 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2545
2546 The following commands are available:
2547
2548 \\{gnus-summary-mode-map}"
2549   (interactive)
2550   (kill-all-local-variables)
2551   (when (gnus-visual-p 'summary-menu 'menu)
2552     (gnus-summary-make-menu-bar)
2553     (gnus-summary-make-tool-bar))
2554   (gnus-summary-make-local-variables)
2555   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2556     (gnus-summary-make-local-variables))
2557   (gnus-make-thread-indent-array)
2558   (gnus-simplify-mode-line)
2559   (setq major-mode 'gnus-summary-mode)
2560   (setq mode-name "Summary")
2561   (make-local-variable 'minor-mode-alist)
2562   (use-local-map gnus-summary-mode-map)
2563   (buffer-disable-undo)
2564   (setq buffer-read-only t)             ;Disable modification
2565   (setq truncate-lines t)
2566   (setq selective-display t)
2567   (setq selective-display-ellipses t)   ;Display `...'
2568   (gnus-summary-set-display-table)
2569   (gnus-set-default-directory)
2570   (setq gnus-newsgroup-name group)
2571   (make-local-variable 'gnus-summary-line-format)
2572   (make-local-variable 'gnus-summary-line-format-spec)
2573   (make-local-variable 'gnus-summary-dummy-line-format)
2574   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2575   (make-local-variable 'gnus-summary-mark-positions)
2576   (make-local-hook 'pre-command-hook)
2577   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2578   (gnus-run-hooks 'gnus-summary-mode-hook)
2579   (turn-on-gnus-mailing-list-mode)
2580   (mm-enable-multibyte)
2581   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2582   (gnus-update-summary-mark-positions))
2583
2584 (defun gnus-summary-make-local-variables ()
2585   "Make all the local summary buffer variables."
2586   (let (global)
2587     (dolist (local gnus-summary-local-variables)
2588       (if (consp local)
2589           (progn
2590             (if (eq (cdr local) 'global)
2591                 ;; Copy the global value of the variable.
2592                 (setq global (symbol-value (car local)))
2593               ;; Use the value from the list.
2594               (setq global (eval (cdr local))))
2595             (set (make-local-variable (car local)) global))
2596         ;; Simple nil-valued local variable.
2597         (set (make-local-variable local) nil)))))
2598
2599 (defun gnus-summary-clear-local-variables ()
2600   (let ((locals gnus-summary-local-variables))
2601     (while locals
2602       (if (consp (car locals))
2603           (and (vectorp (caar locals))
2604                (set (caar locals) nil))
2605         (and (vectorp (car locals))
2606              (set (car locals) nil)))
2607       (setq locals (cdr locals)))))
2608
2609 ;; Summary data functions.
2610
2611 (defmacro gnus-data-number (data)
2612   `(car ,data))
2613
2614 (defmacro gnus-data-set-number (data number)
2615   `(setcar ,data ,number))
2616
2617 (defmacro gnus-data-mark (data)
2618   `(nth 1 ,data))
2619
2620 (defmacro gnus-data-set-mark (data mark)
2621   `(setcar (nthcdr 1 ,data) ,mark))
2622
2623 (defmacro gnus-data-pos (data)
2624   `(nth 2 ,data))
2625
2626 (defmacro gnus-data-set-pos (data pos)
2627   `(setcar (nthcdr 2 ,data) ,pos))
2628
2629 (defmacro gnus-data-header (data)
2630   `(nth 3 ,data))
2631
2632 (defmacro gnus-data-set-header (data header)
2633   `(setf (nth 3 ,data) ,header))
2634
2635 (defmacro gnus-data-level (data)
2636   `(nth 4 ,data))
2637
2638 (defmacro gnus-data-unread-p (data)
2639   `(= (nth 1 ,data) gnus-unread-mark))
2640
2641 (defmacro gnus-data-read-p (data)
2642   `(/= (nth 1 ,data) gnus-unread-mark))
2643
2644 (defmacro gnus-data-pseudo-p (data)
2645   `(consp (nth 3 ,data)))
2646
2647 (defmacro gnus-data-find (number)
2648   `(assq ,number gnus-newsgroup-data))
2649
2650 (defmacro gnus-data-find-list (number &optional data)
2651   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2652      (memq (assq ,number bdata)
2653            bdata)))
2654
2655 (defmacro gnus-data-make (number mark pos header level)
2656   `(list ,number ,mark ,pos ,header ,level))
2657
2658 (defun gnus-data-enter (after-article number mark pos header level offset)
2659   (let ((data (gnus-data-find-list after-article)))
2660     (unless data
2661       (error "No such article: %d" after-article))
2662     (setcdr data (cons (gnus-data-make number mark pos header level)
2663                        (cdr data)))
2664     (setq gnus-newsgroup-data-reverse nil)
2665     (gnus-data-update-list (cddr data) offset)))
2666
2667 (defun gnus-data-enter-list (after-article list &optional offset)
2668   (when list
2669     (let ((data (and after-article (gnus-data-find-list after-article)))
2670           (ilist list))
2671       (if (not (or data
2672                    after-article))
2673           (let ((odata gnus-newsgroup-data))
2674             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2675             (when offset
2676               (gnus-data-update-list odata offset)))
2677       ;; Find the last element in the list to be spliced into the main
2678         ;; list.
2679         (while (cdr list)
2680           (setq list (cdr list)))
2681         (if (not data)
2682             (progn
2683               (setcdr list gnus-newsgroup-data)
2684               (setq gnus-newsgroup-data ilist)
2685               (when offset
2686                 (gnus-data-update-list (cdr list) offset)))
2687           (setcdr list (cdr data))
2688           (setcdr data ilist)
2689           (when offset
2690             (gnus-data-update-list (cdr list) offset))))
2691       (setq gnus-newsgroup-data-reverse nil))))
2692
2693 (defun gnus-data-remove (article &optional offset)
2694   (let ((data gnus-newsgroup-data))
2695     (if (= (gnus-data-number (car data)) article)
2696         (progn
2697           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2698                 gnus-newsgroup-data-reverse nil)
2699           (when offset
2700             (gnus-data-update-list gnus-newsgroup-data offset)))
2701       (while (cdr data)
2702         (when (= (gnus-data-number (cadr data)) article)
2703           (setcdr data (cddr data))
2704           (when offset
2705             (gnus-data-update-list (cdr data) offset))
2706           (setq data nil
2707                 gnus-newsgroup-data-reverse nil))
2708         (setq data (cdr data))))))
2709
2710 (defmacro gnus-data-list (backward)
2711   `(if ,backward
2712        (or gnus-newsgroup-data-reverse
2713            (setq gnus-newsgroup-data-reverse
2714                  (reverse gnus-newsgroup-data)))
2715      gnus-newsgroup-data))
2716
2717 (defun gnus-data-update-list (data offset)
2718   "Add OFFSET to the POS of all data entries in DATA."
2719   (setq gnus-newsgroup-data-reverse nil)
2720   (while data
2721     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2722     (setq data (cdr data))))
2723
2724 (defun gnus-summary-article-pseudo-p (article)
2725   "Say whether this article is a pseudo article or not."
2726   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2727
2728 (defmacro gnus-summary-article-sparse-p (article)
2729   "Say whether this article is a sparse article or not."
2730   `(memq ,article gnus-newsgroup-sparse))
2731
2732 (defmacro gnus-summary-article-ancient-p (article)
2733   "Say whether this article is a sparse article or not."
2734   `(memq ,article gnus-newsgroup-ancient))
2735
2736 (defun gnus-article-parent-p (number)
2737   "Say whether this article is a parent or not."
2738   (let ((data (gnus-data-find-list number)))
2739     (and (cdr data)              ; There has to be an article after...
2740          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2741             (gnus-data-level (nth 1 data))))))
2742
2743 (defun gnus-article-children (number)
2744   "Return a list of all children to NUMBER."
2745   (let* ((data (gnus-data-find-list number))
2746          (level (gnus-data-level (car data)))
2747          children)
2748     (setq data (cdr data))
2749     (while (and data
2750                 (= (gnus-data-level (car data)) (1+ level)))
2751       (push (gnus-data-number (car data)) children)
2752       (setq data (cdr data)))
2753     children))
2754
2755 (defmacro gnus-summary-skip-intangible ()
2756   "If the current article is intangible, then jump to a different article."
2757   '(let ((to (get-text-property (point) 'gnus-intangible)))
2758      (and to (gnus-summary-goto-subject to))))
2759
2760 (defmacro gnus-summary-article-intangible-p ()
2761   "Say whether this article is intangible or not."
2762   '(get-text-property (point) 'gnus-intangible))
2763
2764 (defun gnus-article-read-p (article)
2765   "Say whether ARTICLE is read or not."
2766   (not (or (memq article gnus-newsgroup-marked)
2767            (memq article gnus-newsgroup-spam-marked)
2768            (memq article gnus-newsgroup-unreads)
2769            (memq article gnus-newsgroup-unselected)
2770            (memq article gnus-newsgroup-dormant))))
2771
2772 ;; Some summary mode macros.
2773
2774 (defmacro gnus-summary-article-number ()
2775   "The article number of the article on the current line.
2776 If there isn't an article number here, then we return the current
2777 article number."
2778   '(progn
2779      (gnus-summary-skip-intangible)
2780      (or (get-text-property (point) 'gnus-number)
2781          (gnus-summary-last-subject))))
2782
2783 (defmacro gnus-summary-article-header (&optional number)
2784   "Return the header of article NUMBER."
2785   `(gnus-data-header (gnus-data-find
2786                       ,(or number '(gnus-summary-article-number)))))
2787
2788 (defmacro gnus-summary-thread-level (&optional number)
2789   "Return the level of thread that starts with article NUMBER."
2790   `(if (and (eq gnus-summary-make-false-root 'dummy)
2791             (get-text-property (point) 'gnus-intangible))
2792        0
2793      (gnus-data-level (gnus-data-find
2794                        ,(or number '(gnus-summary-article-number))))))
2795
2796 (defmacro gnus-summary-article-mark (&optional number)
2797   "Return the mark of article NUMBER."
2798   `(gnus-data-mark (gnus-data-find
2799                     ,(or number '(gnus-summary-article-number)))))
2800
2801 (defmacro gnus-summary-article-pos (&optional number)
2802   "Return the position of the line of article NUMBER."
2803   `(gnus-data-pos (gnus-data-find
2804                    ,(or number '(gnus-summary-article-number)))))
2805
2806 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2807 (defmacro gnus-summary-article-subject (&optional number)
2808   "Return current subject string or nil if nothing."
2809   `(let ((headers
2810           ,(if number
2811                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2812              '(gnus-data-header (assq (gnus-summary-article-number)
2813                                       gnus-newsgroup-data)))))
2814      (and headers
2815           (vectorp headers)
2816           (mail-header-subject headers))))
2817
2818 (defmacro gnus-summary-article-score (&optional number)
2819   "Return current article score."
2820   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2821                   gnus-newsgroup-scored))
2822        gnus-summary-default-score 0))
2823
2824 (defun gnus-summary-article-children (&optional number)
2825   "Return a list of article numbers that are children of article NUMBER."
2826   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2827          (level (gnus-data-level (car data)))
2828          l children)
2829     (while (and (setq data (cdr data))
2830                 (> (setq l (gnus-data-level (car data))) level))
2831       (and (= (1+ level) l)
2832            (push (gnus-data-number (car data))
2833                  children)))
2834     (nreverse children)))
2835
2836 (defun gnus-summary-article-parent (&optional number)
2837   "Return the article number of the parent of article NUMBER."
2838   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2839                                     (gnus-data-list t)))
2840          (level (gnus-data-level (car data))))
2841     (if (zerop level)
2842         ()                              ; This is a root.
2843       ;; We search until we find an article with a level less than
2844       ;; this one.  That function has to be the parent.
2845       (while (and (setq data (cdr data))
2846                   (not (< (gnus-data-level (car data)) level))))
2847       (and data (gnus-data-number (car data))))))
2848
2849 (defun gnus-unread-mark-p (mark)
2850   "Say whether MARK is the unread mark."
2851   (= mark gnus-unread-mark))
2852
2853 (defun gnus-read-mark-p (mark)
2854   "Say whether MARK is one of the marks that mark as read.
2855 This is all marks except unread, ticked, dormant, and expirable."
2856   (not (or (= mark gnus-unread-mark)
2857            (= mark gnus-ticked-mark)
2858            (= mark gnus-spam-mark)
2859            (= mark gnus-dormant-mark)
2860            (= mark gnus-expirable-mark))))
2861
2862 (defmacro gnus-article-mark (number)
2863   "Return the MARK of article NUMBER.
2864 This macro should only be used when computing the mark the \"first\"
2865 time; i.e., when generating the summary lines.  After that,
2866 `gnus-summary-article-mark' should be used to examine the
2867 marks of articles."
2868   `(cond
2869     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2870     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2871     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2872     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2873     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2874     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2875     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2876     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2877            gnus-ancient-mark))))
2878
2879 ;; Saving hidden threads.
2880
2881 (defmacro gnus-save-hidden-threads (&rest forms)
2882   "Save hidden threads, eval FORMS, and restore the hidden threads."
2883   (let ((config (make-symbol "config")))
2884     `(let ((,config (gnus-hidden-threads-configuration)))
2885        (unwind-protect
2886            (save-excursion
2887              ,@forms)
2888          (gnus-restore-hidden-threads-configuration ,config)))))
2889 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2890 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2891
2892 (defun gnus-data-compute-positions ()
2893   "Compute the positions of all articles."
2894   (setq gnus-newsgroup-data-reverse nil)
2895   (let ((data gnus-newsgroup-data))
2896     (save-excursion
2897       (gnus-save-hidden-threads
2898         (gnus-summary-show-all-threads)
2899         (goto-char (point-min))
2900         (while data
2901           (while (get-text-property (point) 'gnus-intangible)
2902             (forward-line 1))
2903           (gnus-data-set-pos (car data) (+ (point) 3))
2904           (setq data (cdr data))
2905           (forward-line 1))))))
2906
2907 (defun gnus-hidden-threads-configuration ()
2908   "Return the current hidden threads configuration."
2909   (save-excursion
2910     (let (config)
2911       (goto-char (point-min))
2912       (while (search-forward "\r" nil t)
2913         (push (1- (point)) config))
2914       config)))
2915
2916 (defun gnus-restore-hidden-threads-configuration (config)
2917   "Restore hidden threads configuration from CONFIG."
2918   (save-excursion
2919     (let (point buffer-read-only)
2920       (while (setq point (pop config))
2921         (when (and (< point (point-max))
2922                    (goto-char point)
2923                    (eq (char-after) ?\n))
2924           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2925
2926 ;; Various summary mode internalish functions.
2927
2928 (defun gnus-mouse-pick-article (e)
2929   (interactive "e")
2930   (mouse-set-point e)
2931   (gnus-summary-next-page nil t))
2932
2933 (defun gnus-summary-set-display-table ()
2934   "Change the display table.
2935 Odd characters have a tendency to mess
2936 up nicely formatted displays - we make all possible glyphs
2937 display only a single character."
2938
2939   ;; We start from the standard display table, if any.
2940   (let ((table (or (copy-sequence standard-display-table)
2941                    (make-display-table)))
2942         (i 32))
2943     ;; Nix out all the control chars...
2944     (while (>= (setq i (1- i)) 0)
2945       (aset table i [??]))
2946    ;; ... but not newline and cr, of course.  (cr is necessary for the
2947     ;; selective display).
2948     (aset table ?\n nil)
2949     (aset table ?\r nil)
2950     ;; We keep TAB as well.
2951     (aset table ?\t nil)
2952     ;; We nix out any glyphs over 126 that are not set already.
2953     (let ((i 256))
2954       (while (>= (setq i (1- i)) 127)
2955         ;; Only modify if the entry is nil.
2956         (unless (aref table i)
2957           (aset table i [??]))))
2958     (setq buffer-display-table table)))
2959
2960 (defun gnus-summary-set-article-display-arrow (pos)
2961   "Update the overlay arrow to point to line at position POS."
2962   (when (and gnus-summary-display-arrow
2963              (boundp 'overlay-arrow-position)
2964              (boundp 'overlay-arrow-string))
2965     (save-excursion
2966       (goto-char pos)
2967       (beginning-of-line)
2968       (unless overlay-arrow-position
2969         (setq overlay-arrow-position (make-marker)))
2970       (setq overlay-arrow-string "=>"
2971             overlay-arrow-position (set-marker overlay-arrow-position
2972                                                (point)
2973                                                (current-buffer))))))
2974
2975 (defun gnus-summary-buffer-name (group)
2976   "Return the summary buffer name of GROUP."
2977   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2978
2979 (defun gnus-summary-setup-buffer (group)
2980   "Initialize summary buffer."
2981   (let ((buffer (gnus-summary-buffer-name group))
2982         (dead-name (concat "*Dead Summary "
2983                            (gnus-group-decoded-name group) "*")))
2984     ;; If a dead summary buffer exists, we kill it.
2985     (when (gnus-buffer-live-p dead-name)
2986       (gnus-kill-buffer dead-name))
2987     (if (get-buffer buffer)
2988         (progn
2989           (set-buffer buffer)
2990           (setq gnus-summary-buffer (current-buffer))
2991           (not gnus-newsgroup-prepared))
2992       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2993       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2994       (gnus-summary-mode group)
2995       (when gnus-carpal
2996         (gnus-carpal-setup-buffer 'summary))
2997       (unless gnus-single-article-buffer
2998         (make-local-variable 'gnus-article-buffer)
2999         (make-local-variable 'gnus-article-current)
3000         (make-local-variable 'gnus-original-article-buffer))
3001       (setq gnus-newsgroup-name group)
3002       ;; Set any local variables in the group parameters.
3003       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3004       t)))
3005
3006 (defun gnus-set-global-variables ()
3007   "Set the global equivalents of the buffer-local variables.
3008 They are set to the latest values they had.  These reflect the summary
3009 buffer that was in action when the last article was fetched."
3010   (when (eq major-mode 'gnus-summary-mode)
3011     (setq gnus-summary-buffer (current-buffer))
3012     (let ((name gnus-newsgroup-name)
3013           (marked gnus-newsgroup-marked)
3014           (spam gnus-newsgroup-spam-marked)
3015           (unread gnus-newsgroup-unreads)
3016           (headers gnus-current-headers)
3017           (data gnus-newsgroup-data)
3018           (summary gnus-summary-buffer)
3019           (article-buffer gnus-article-buffer)
3020           (original gnus-original-article-buffer)
3021           (gac gnus-article-current)
3022           (reffed gnus-reffed-article-number)
3023           (score-file gnus-current-score-file)
3024           (default-charset gnus-newsgroup-charset)
3025           vlist)
3026       (let ((locals gnus-newsgroup-variables))
3027         (while locals
3028           (if (consp (car locals))
3029               (push (eval (caar locals)) vlist)
3030             (push (eval (car locals)) vlist))
3031           (setq locals (cdr locals)))
3032         (setq vlist (nreverse vlist)))
3033       (save-excursion
3034         (set-buffer gnus-group-buffer)
3035         (setq gnus-newsgroup-name name
3036               gnus-newsgroup-marked marked
3037               gnus-newsgroup-spam-marked spam
3038               gnus-newsgroup-unreads unread
3039               gnus-current-headers headers
3040               gnus-newsgroup-data data
3041               gnus-article-current gac
3042               gnus-summary-buffer summary
3043               gnus-article-buffer article-buffer
3044               gnus-original-article-buffer original
3045               gnus-reffed-article-number reffed
3046               gnus-current-score-file score-file
3047               gnus-newsgroup-charset default-charset)
3048         (let ((locals gnus-newsgroup-variables))
3049           (while locals
3050             (if (consp (car locals))
3051                 (set (caar locals) (pop vlist))
3052               (set (car locals) (pop vlist)))
3053             (setq locals (cdr locals))))
3054         ;; The article buffer also has local variables.
3055         (when (gnus-buffer-live-p gnus-article-buffer)
3056           (set-buffer gnus-article-buffer)
3057           (setq gnus-summary-buffer summary))))))
3058
3059 (defun gnus-summary-article-unread-p (article)
3060   "Say whether ARTICLE is unread or not."
3061   (memq article gnus-newsgroup-unreads))
3062
3063 (defun gnus-summary-first-article-p (&optional article)
3064   "Return whether ARTICLE is the first article in the buffer."
3065   (if (not (setq article (or article (gnus-summary-article-number))))
3066       nil
3067     (eq article (caar gnus-newsgroup-data))))
3068
3069 (defun gnus-summary-last-article-p (&optional article)
3070   "Return whether ARTICLE is the last article in the buffer."
3071   (if (not (setq article (or article (gnus-summary-article-number))))
3072       ;; All non-existent numbers are the last article.  :-)
3073       t
3074     (not (cdr (gnus-data-find-list article)))))
3075
3076 (defun gnus-make-thread-indent-array ()
3077   (let ((n 200))
3078     (unless (and gnus-thread-indent-array
3079                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3080       (setq gnus-thread-indent-array (make-vector 201 "")
3081             gnus-thread-indent-array-level gnus-thread-indent-level)
3082       (while (>= n 0)
3083         (aset gnus-thread-indent-array n
3084               (make-string (* n gnus-thread-indent-level) ? ))
3085         (setq n (1- n))))))
3086
3087 (defun gnus-update-summary-mark-positions ()
3088   "Compute where the summary marks are to go."
3089   (save-excursion
3090     (when (gnus-buffer-exists-p gnus-summary-buffer)
3091       (set-buffer gnus-summary-buffer))
3092     (let ((gnus-replied-mark 129)
3093           (gnus-score-below-mark 130)
3094           (gnus-score-over-mark 130)
3095           (gnus-downloaded-mark 131)
3096           (spec gnus-summary-line-format-spec)
3097           gnus-visual pos)
3098       (save-excursion
3099         (gnus-set-work-buffer)
3100         (let ((gnus-summary-line-format-spec spec)
3101               (gnus-newsgroup-downloadable '(0)))
3102           (gnus-summary-insert-line
3103            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3104            0 nil nil 128 t nil "" nil 1)
3105           (goto-char (point-min))
3106           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3107                                              (- (point) (point-min) 1)))))
3108           (goto-char (point-min))
3109           (push (cons 'replied (and (search-forward "\201" nil t)
3110                                     (- (point) (point-min) 1)))
3111                 pos)
3112           (goto-char (point-min))
3113           (push (cons 'score (and (search-forward "\202" nil t)
3114                                   (- (point) (point-min) 1)))
3115                 pos)
3116           (goto-char (point-min))
3117           (push (cons 'download
3118                       (and (search-forward "\203" nil t)
3119                            (- (point) (point-min) 1)))
3120                 pos)))
3121       (setq gnus-summary-mark-positions pos))))
3122
3123 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3124   "Insert a dummy root in the summary buffer."
3125   (beginning-of-line)
3126   (gnus-add-text-properties
3127    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3128    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3129
3130 (defun gnus-summary-extract-address-component (from)
3131   (or (car (funcall gnus-extract-address-components from))
3132       from))
3133
3134 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3135   (let ((mail-parse-charset gnus-newsgroup-charset)
3136         ; Is it really necessary to do this next part for each summary line?
3137         ; Luckily, doesn't seem to slow things down much.
3138         (mail-parse-ignored-charsets
3139          (save-excursion (set-buffer gnus-summary-buffer)
3140                          gnus-newsgroup-ignored-charsets)))
3141     (or
3142      (and gnus-ignored-from-addresses
3143           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3144           (let ((extra-headers (mail-header-extra header))
3145                 to
3146                 newsgroups)
3147             (cond
3148              ((setq to (cdr (assq 'To extra-headers)))
3149               (concat "-> "
3150                       (inline
3151                         (gnus-summary-extract-address-component
3152                          (funcall gnus-decode-encoded-word-function to)))))
3153              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3154               (concat "=> " newsgroups)))))
3155      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3156
3157 (defun gnus-summary-insert-line (gnus-tmp-header
3158                                  gnus-tmp-level gnus-tmp-current
3159                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3160                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3161                                  &optional gnus-tmp-dummy gnus-tmp-score
3162                                  gnus-tmp-process)
3163   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3164          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3165          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3166          (gnus-tmp-score-char
3167           (if (or (null gnus-summary-default-score)
3168                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3169                       gnus-summary-zcore-fuzz))
3170               ?                         ;Whitespace
3171             (if (< gnus-tmp-score gnus-summary-default-score)
3172                 gnus-score-below-mark gnus-score-over-mark)))
3173          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3174          (gnus-tmp-replied
3175           (cond (gnus-tmp-process gnus-process-mark)
3176                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3177                  gnus-cached-mark)
3178                 (gnus-tmp-replied gnus-replied-mark)
3179                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3180                  gnus-forwarded-mark)
3181                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3182                  gnus-saved-mark)
3183                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3184                  gnus-recent-mark)
3185                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3186                  gnus-unseen-mark)
3187                 (t gnus-no-mark)))
3188          (gnus-tmp-downloaded
3189           (cond (undownloaded 
3190                  gnus-undownloaded-mark)
3191                 (gnus-newsgroup-agentized
3192                  gnus-downloaded-mark)
3193                 (t
3194                  gnus-no-mark)))
3195          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3196          (gnus-tmp-name
3197           (cond
3198            ((string-match "<[^>]+> *$" gnus-tmp-from)
3199             (let ((beg (match-beginning 0)))
3200               (or (and (string-match "^\".+\"" gnus-tmp-from)
3201                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3202                   (substring gnus-tmp-from 0 beg))))
3203            ((string-match "(.+)" gnus-tmp-from)
3204             (substring gnus-tmp-from
3205                        (1+ (match-beginning 0)) (1- (match-end 0))))
3206            (t gnus-tmp-from)))
3207          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3208          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3209          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3210          (buffer-read-only nil))
3211     (when (string= gnus-tmp-name "")
3212       (setq gnus-tmp-name gnus-tmp-from))
3213     (unless (numberp gnus-tmp-lines)
3214       (setq gnus-tmp-lines -1))
3215     (if (= gnus-tmp-lines -1)
3216         (setq gnus-tmp-lines "?")
3217       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3218     (gnus-put-text-property
3219      (point)
3220      (progn (eval gnus-summary-line-format-spec) (point))
3221      'gnus-number gnus-tmp-number)
3222     (when (gnus-visual-p 'summary-highlight 'highlight)
3223       (forward-line -1)
3224       (gnus-run-hooks 'gnus-summary-update-hook)
3225       (forward-line 1))))
3226
3227 (defun gnus-summary-update-line (&optional dont-update)
3228   "Update summary line after change."
3229   (when (and gnus-summary-default-score
3230              (not gnus-summary-inhibit-highlight))
3231     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3232            (article (gnus-summary-article-number))
3233            (score (gnus-summary-article-score article)))
3234       (unless dont-update
3235         (if (and gnus-summary-mark-below
3236                  (< (gnus-summary-article-score)
3237                     gnus-summary-mark-below))
3238             ;; This article has a low score, so we mark it as read.
3239             (when (memq article gnus-newsgroup-unreads)
3240               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3241           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3242             ;; This article was previously marked as read on account
3243             ;; of a low score, but now it has risen, so we mark it as
3244             ;; unread.
3245             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3246         (gnus-summary-update-mark
3247          (if (or (null gnus-summary-default-score)
3248                  (<= (abs (- score gnus-summary-default-score))
3249                      gnus-summary-zcore-fuzz))
3250              ?                          ;Whitespace
3251            (if (< score gnus-summary-default-score)
3252                gnus-score-below-mark gnus-score-over-mark))
3253          'score))
3254       ;; Do visual highlighting.
3255       (when (gnus-visual-p 'summary-highlight 'highlight)
3256         (gnus-run-hooks 'gnus-summary-update-hook)))))
3257
3258 (defvar gnus-tmp-new-adopts nil)
3259
3260 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3261   "Return the number of articles in THREAD.
3262 This may be 0 in some cases -- if none of the articles in
3263 the thread are to be displayed."
3264   (let* ((number
3265          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3266           (cond
3267            ((not (listp thread))
3268             1)
3269            ((and (consp thread) (cdr thread))
3270             (apply
3271              '+ 1 (mapcar
3272                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3273            ((null thread)
3274             1)
3275            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3276             1)
3277            (t 0))))
3278     (when (and level (zerop level) gnus-tmp-new-adopts)
3279       (incf number
3280             (apply '+ (mapcar
3281                        'gnus-summary-number-of-articles-in-thread
3282                        gnus-tmp-new-adopts))))
3283     (if char
3284         (if (> number 1) gnus-not-empty-thread-mark
3285           gnus-empty-thread-mark)
3286       number)))
3287
3288 (defsubst gnus-summary-line-message-size (head)
3289   "Return pretty-printed version of message size.
3290 This function is intended to be used in
3291 `gnus-summary-line-format-alist', which see."
3292   (let ((c (or (mail-header-chars head) -1)))
3293     (cond ((< c 0) "n/a")               ; chars not available
3294           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3295           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3296           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3297           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3298
3299
3300 (defun gnus-summary-set-local-parameters (group)
3301   "Go through the local params of GROUP and set all variable specs in that list."
3302   (let ((params (gnus-group-find-parameter group))
3303         (vars '(quit-config))           ; Ignore quit-config.
3304         elem)
3305     (while params
3306       (setq elem (car params)
3307             params (cdr params))
3308       (and (consp elem)                 ; Has to be a cons.
3309            (consp (cdr elem))           ; The cdr has to be a list.
3310            (symbolp (car elem))         ; Has to be a symbol in there.
3311            (not (memq (car elem) vars))
3312            (ignore-errors               ; So we set it.
3313              (push (car elem) vars)
3314              (make-local-variable (car elem))
3315              (set (car elem) (eval (nth 1 elem))))))))
3316
3317 (defun gnus-summary-read-group (group &optional show-all no-article
3318                                       kill-buffer no-display backward
3319                                       select-articles)
3320   "Start reading news in newsgroup GROUP.
3321 If SHOW-ALL is non-nil, already read articles are also listed.
3322 If NO-ARTICLE is non-nil, no article is selected initially.
3323 If NO-DISPLAY, don't generate a summary buffer."
3324   (let (result)
3325     (while (and group
3326                 (null (setq result
3327                             (let ((gnus-auto-select-next nil))
3328                               (or (gnus-summary-read-group-1
3329                                    group show-all no-article
3330                                    kill-buffer no-display
3331                                    select-articles)
3332                                   (setq show-all nil
3333                                         select-articles nil)))))
3334                 (eq gnus-auto-select-next 'quietly))
3335       (set-buffer gnus-group-buffer)
3336       ;; The entry function called above goes to the next
3337       ;; group automatically, so we go two groups back
3338       ;; if we are searching for the previous group.
3339       (when backward
3340         (gnus-group-prev-unread-group 2))
3341       (if (not (equal group (gnus-group-group-name)))
3342           (setq group (gnus-group-group-name))
3343         (setq group nil)))
3344     result))
3345
3346 (defun gnus-summary-read-group-1 (group show-all no-article
3347                                         kill-buffer no-display
3348                                         &optional select-articles)
3349   ;; Killed foreign groups can't be entered.
3350   ;;  (when (and (not (gnus-group-native-p group))
3351   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3352   ;;    (error "Dead non-native groups can't be entered"))
3353   (gnus-message 5 "Retrieving newsgroup: %s..."
3354                 (gnus-group-decoded-name group))
3355   (let* ((new-group (gnus-summary-setup-buffer group))
3356          (quit-config (gnus-group-quit-config group))
3357          (did-select (and new-group (gnus-select-newsgroup
3358                                      group show-all select-articles))))
3359     (cond
3360      ;; This summary buffer exists already, so we just select it.
3361      ((not new-group)
3362       (gnus-set-global-variables)
3363       (when kill-buffer
3364         (gnus-kill-or-deaden-summary kill-buffer))
3365       (gnus-configure-windows 'summary 'force)
3366       (gnus-set-mode-line 'summary)
3367       (gnus-summary-position-point)
3368       (message "")
3369       t)
3370      ;; We couldn't select this group.
3371      ((null did-select)
3372       (when (and (eq major-mode 'gnus-summary-mode)
3373                  (not (equal (current-buffer) kill-buffer)))
3374         (kill-buffer (current-buffer))
3375         (if (not quit-config)
3376             (progn
3377               ;; Update the info -- marks might need to be removed,
3378               ;; for instance.
3379               (gnus-summary-update-info)
3380               (set-buffer gnus-group-buffer)
3381               (gnus-group-jump-to-group group)
3382               (gnus-group-next-unread-group 1))
3383           (gnus-handle-ephemeral-exit quit-config)))
3384       (let ((grpinfo (gnus-get-info group)))
3385         (if (null (gnus-info-read grpinfo))
3386             (gnus-message 3 "Group %s contains no messages"
3387                           (gnus-group-decoded-name group))
3388           (gnus-message 3 "Can't select group")))
3389       nil)
3390      ;; The user did a `C-g' while prompting for number of articles,
3391      ;; so we exit this group.
3392      ((eq did-select 'quit)
3393       (and (eq major-mode 'gnus-summary-mode)
3394            (not (equal (current-buffer) kill-buffer))
3395            (kill-buffer (current-buffer)))
3396       (when kill-buffer
3397         (gnus-kill-or-deaden-summary kill-buffer))
3398       (if (not quit-config)
3399           (progn
3400             (set-buffer gnus-group-buffer)
3401             (gnus-group-jump-to-group group)
3402             (gnus-group-next-unread-group 1)
3403             (gnus-configure-windows 'group 'force))
3404         (gnus-handle-ephemeral-exit quit-config))
3405       ;; Finally signal the quit.
3406       (signal 'quit nil))
3407      ;; The group was successfully selected.
3408      (t
3409       (gnus-set-global-variables)
3410       ;; Save the active value in effect when the group was entered.
3411       (setq gnus-newsgroup-active
3412             (gnus-copy-sequence
3413              (gnus-active gnus-newsgroup-name)))
3414       ;; You can change the summary buffer in some way with this hook.
3415       (gnus-run-hooks 'gnus-select-group-hook)
3416       (gnus-update-format-specifications
3417        nil 'summary 'summary-mode 'summary-dummy)
3418       (gnus-update-summary-mark-positions)
3419       ;; Do score processing.
3420       (when gnus-use-scoring
3421         (gnus-possibly-score-headers))
3422       ;; Check whether to fill in the gaps in the threads.
3423       (when gnus-build-sparse-threads
3424         (gnus-build-sparse-threads))
3425       ;; Find the initial limit.
3426       (if gnus-show-threads
3427           (if show-all
3428               (let ((gnus-newsgroup-dormant nil))
3429                 (gnus-summary-initial-limit show-all))
3430             (gnus-summary-initial-limit show-all))
3431         ;; When unthreaded, all articles are always shown.
3432         (setq gnus-newsgroup-limit
3433               (mapcar
3434                (lambda (header) (mail-header-number header))
3435                gnus-newsgroup-headers)))
3436       ;; Generate the summary buffer.
3437       (unless no-display
3438         (gnus-summary-prepare))
3439       (when gnus-use-trees
3440         (gnus-tree-open group)
3441         (setq gnus-summary-highlight-line-function
3442               'gnus-tree-highlight-article))
3443       ;; If the summary buffer is empty, but there are some low-scored
3444       ;; articles or some excluded dormants, we include these in the
3445       ;; buffer.
3446       (when (and (zerop (buffer-size))
3447                  (not no-display))
3448         (cond (gnus-newsgroup-dormant
3449                (gnus-summary-limit-include-dormant))
3450               ((and gnus-newsgroup-scored show-all)
3451                (gnus-summary-limit-include-expunged t))))
3452       ;; Function `gnus-apply-kill-file' must be called in this hook.
3453       (gnus-run-hooks 'gnus-apply-kill-hook)
3454       (if (and (zerop (buffer-size))
3455                (not no-display))
3456           (progn
3457             ;; This newsgroup is empty.
3458             (gnus-summary-catchup-and-exit nil t)
3459             (gnus-message 6 "No unread news")
3460             (when kill-buffer
3461               (gnus-kill-or-deaden-summary kill-buffer))
3462             ;; Return nil from this function.
3463             nil)
3464         ;; Hide conversation thread subtrees.  We cannot do this in
3465         ;; gnus-summary-prepare-hook since kill processing may not
3466         ;; work with hidden articles.
3467         (gnus-summary-maybe-hide-threads)
3468         (when kill-buffer
3469           (gnus-kill-or-deaden-summary kill-buffer))
3470         (gnus-summary-auto-select-subject)
3471         ;; Show first unread article if requested.
3472         (if (and (not no-article)
3473                  (not no-display)
3474                  gnus-newsgroup-unreads
3475                  gnus-auto-select-first)
3476             (progn
3477               (gnus-configure-windows 'summary)
3478               (let ((art (gnus-summary-article-number)))
3479                 (unless (or (memq art gnus-newsgroup-undownloaded)
3480                             (memq art gnus-newsgroup-downloadable))
3481                   (gnus-summary-goto-article art))))
3482           ;; Don't select any articles.
3483           (gnus-summary-position-point)
3484           (gnus-configure-windows 'summary 'force)
3485           (gnus-set-mode-line 'summary))
3486         (when (get-buffer-window gnus-group-buffer t)
3487           ;; Gotta use windows, because recenter does weird stuff if
3488           ;; the current buffer ain't the displayed window.
3489           (let ((owin (selected-window)))
3490             (select-window (get-buffer-window gnus-group-buffer t))
3491             (when (gnus-group-goto-group group)
3492               (recenter))
3493             (select-window owin)))
3494         ;; Mark this buffer as "prepared".
3495         (setq gnus-newsgroup-prepared t)
3496         (gnus-run-hooks 'gnus-summary-prepared-hook)
3497         (unless (gnus-ephemeral-group-p group)
3498           (gnus-group-update-group group))
3499         t)))))
3500
3501 (defun gnus-summary-auto-select-subject ()
3502   "Select the subject line on initial group entry."
3503   (goto-char (point-min))
3504   (cond
3505    ((eq gnus-auto-select-subject 'best)
3506     (gnus-summary-best-unread-subject))
3507    ((eq gnus-auto-select-subject 'unread)
3508     (gnus-summary-first-unread-subject))
3509    ((eq gnus-auto-select-subject 'unseen)
3510     (gnus-summary-first-unseen-subject))
3511    ((eq gnus-auto-select-subject 'unseen-or-unread)
3512     (gnus-summary-first-unseen-or-unread-subject))
3513    ((eq gnus-auto-select-subject 'first)
3514     ;; Do nothing.
3515     )
3516    ((gnus-functionp gnus-auto-select-subject)
3517     (funcall gnus-auto-select-subject))))
3518
3519 (defun gnus-summary-prepare ()
3520   "Generate the summary buffer."
3521   (interactive)
3522   (let ((buffer-read-only nil))
3523     (erase-buffer)
3524     (setq gnus-newsgroup-data nil
3525           gnus-newsgroup-data-reverse nil)
3526     (gnus-run-hooks 'gnus-summary-generate-hook)
3527     ;; Generate the buffer, either with threads or without.
3528     (when gnus-newsgroup-headers
3529       (gnus-summary-prepare-threads
3530        (if gnus-show-threads
3531            (gnus-sort-gathered-threads
3532             (funcall gnus-summary-thread-gathering-function
3533                      (gnus-sort-threads
3534                       (gnus-cut-threads (gnus-make-threads)))))
3535          ;; Unthreaded display.
3536          (gnus-sort-articles gnus-newsgroup-headers))))
3537     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3538     ;; Call hooks for modifying summary buffer.
3539     (goto-char (point-min))
3540     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3541
3542 (defsubst gnus-general-simplify-subject (subject)
3543   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3544   (setq subject
3545         (cond
3546          ;; Truncate the subject.
3547          (gnus-simplify-subject-functions
3548           (gnus-map-function gnus-simplify-subject-functions subject))
3549          ((numberp gnus-summary-gather-subject-limit)
3550           (setq subject (gnus-simplify-subject-re subject))
3551           (if (> (length subject) gnus-summary-gather-subject-limit)
3552               (substring subject 0 gnus-summary-gather-subject-limit)
3553             subject))
3554          ;; Fuzzily simplify it.
3555          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3556           (gnus-simplify-subject-fuzzy subject))
3557          ;; Just remove the leading "Re:".
3558          (t
3559           (gnus-simplify-subject-re subject))))
3560
3561   (if (and gnus-summary-gather-exclude-subject
3562            (string-match gnus-summary-gather-exclude-subject subject))
3563       nil                         ; This article shouldn't be gathered
3564     subject))
3565
3566 (defun gnus-summary-simplify-subject-query ()
3567   "Query where the respool algorithm would put this article."
3568   (interactive)
3569   (gnus-summary-select-article)
3570   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3571
3572 (defun gnus-gather-threads-by-subject (threads)
3573   "Gather threads by looking at Subject headers."
3574   (if (not gnus-summary-make-false-root)
3575       threads
3576     (let ((hashtb (gnus-make-hashtable 1024))
3577           (prev threads)
3578           (result threads)
3579           subject hthread whole-subject)
3580       (while threads
3581         (setq subject (gnus-general-simplify-subject
3582                        (setq whole-subject (mail-header-subject
3583                                             (caar threads)))))
3584         (when subject
3585           (if (setq hthread (gnus-gethash subject hashtb))
3586               (progn
3587                 ;; We enter a dummy root into the thread, if we
3588                 ;; haven't done that already.
3589                 (unless (stringp (caar hthread))
3590                   (setcar hthread (list whole-subject (car hthread))))
3591                 ;; We add this new gathered thread to this gathered
3592                 ;; thread.
3593                 (setcdr (car hthread)
3594                         (nconc (cdar hthread) (list (car threads))))
3595                 ;; Remove it from the list of threads.
3596                 (setcdr prev (cdr threads))
3597                 (setq threads prev))
3598             ;; Enter this thread into the hash table.
3599             (gnus-sethash subject
3600                           (if gnus-summary-make-false-root-always
3601                               (progn
3602                                 ;; If you want a dummy root above all
3603                                 ;; threads...
3604                                 (setcar threads (list whole-subject
3605                                                       (car threads)))
3606                                 threads)
3607                             threads)
3608                           hashtb)))
3609         (setq prev threads)
3610         (setq threads (cdr threads)))
3611       result)))
3612
3613 (defun gnus-gather-threads-by-references (threads)
3614   "Gather threads by looking at References headers."
3615   (let ((idhashtb (gnus-make-hashtable 1024))
3616         (thhashtb (gnus-make-hashtable 1024))
3617         (prev threads)
3618         (result threads)
3619         ids references id gthread gid entered ref)
3620     (while threads
3621       (when (setq references (mail-header-references (caar threads)))
3622         (setq id (mail-header-id (caar threads))
3623               ids (inline (gnus-split-references references))
3624               entered nil)
3625         (while (setq ref (pop ids))
3626           (setq ids (delete ref ids))
3627           (if (not (setq gid (gnus-gethash ref idhashtb)))
3628               (progn
3629                 (gnus-sethash ref id idhashtb)
3630                 (gnus-sethash id threads thhashtb))
3631             (setq gthread (gnus-gethash gid thhashtb))
3632             (unless entered
3633               ;; We enter a dummy root into the thread, if we
3634               ;; haven't done that already.
3635               (unless (stringp (caar gthread))
3636                 (setcar gthread (list (mail-header-subject (caar gthread))
3637                                       (car gthread))))
3638               ;; We add this new gathered thread to this gathered
3639               ;; thread.
3640               (setcdr (car gthread)
3641                       (nconc (cdar gthread) (list (car threads)))))
3642             ;; Add it into the thread hash table.
3643             (gnus-sethash id gthread thhashtb)
3644             (setq entered t)
3645             ;; Remove it from the list of threads.
3646             (setcdr prev (cdr threads))
3647             (setq threads prev))))
3648       (setq prev threads)
3649       (setq threads (cdr threads)))
3650     result))
3651
3652 (defun gnus-sort-gathered-threads (threads)
3653   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3654   (let ((result threads))
3655     (while threads
3656       (when (stringp (caar threads))
3657         (setcdr (car threads)
3658                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3659       (setq threads (cdr threads)))
3660     result))
3661
3662 (defun gnus-thread-loop-p (root thread)
3663   "Say whether ROOT is in THREAD."
3664   (let ((stack (list thread))
3665         (infloop 0)
3666         th)
3667     (while (setq thread (pop stack))
3668       (setq th (cdr thread))
3669       (while (and th
3670                   (not (eq (caar th) root)))
3671         (pop th))
3672       (if th
3673           ;; We have found a loop.
3674           (let (ref-dep)
3675             (setcdr thread (delq (car th) (cdr thread)))
3676             (if (boundp (setq ref-dep (intern "none"
3677                                               gnus-newsgroup-dependencies)))
3678                 (setcdr (symbol-value ref-dep)
3679                         (nconc (cdr (symbol-value ref-dep))
3680                                (list (car th))))
3681               (set ref-dep (list nil (car th))))
3682             (setq infloop 1
3683                   stack nil))
3684         ;; Push all the subthreads onto the stack.
3685         (push (cdr thread) stack)))
3686     infloop))
3687
3688 (defun gnus-make-threads ()
3689   "Go through the dependency hashtb and find the roots.  Return all threads."
3690   (let (threads)
3691     (while (catch 'infloop
3692              (mapatoms
3693               (lambda (refs)
3694                 ;; Deal with self-referencing References loops.
3695                 (when (and (car (symbol-value refs))
3696                            (not (zerop
3697                                  (apply
3698                                   '+
3699                                   (mapcar
3700                                    (lambda (thread)
3701                                      (gnus-thread-loop-p
3702                                       (car (symbol-value refs)) thread))
3703                                    (cdr (symbol-value refs)))))))
3704                   (setq threads nil)
3705                   (throw 'infloop t))
3706                 (unless (car (symbol-value refs))
3707                   ;; These threads do not refer back to any other
3708                   ;; articles, so they're roots.
3709                   (setq threads (append (cdr (symbol-value refs)) threads))))
3710               gnus-newsgroup-dependencies)))
3711     threads))
3712
3713 ;; Build the thread tree.
3714 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3715   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3716
3717 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3718 if it was already present.
3719
3720 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3721 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3722 Message-IDs will be renamed to a unique Message-ID before being
3723 entered.
3724
3725 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3726   (let* ((id (mail-header-id header))
3727          (id-dep (and id (intern id dependencies)))
3728          parent-id ref ref-dep ref-header replaced)
3729     ;; Enter this `header' in the `dependencies' table.
3730     (cond
3731      ((not id-dep)
3732       (setq header nil))
3733      ;; The first two cases do the normal part: enter a new `header'
3734      ;; in the `dependencies' table.
3735      ((not (boundp id-dep))
3736       (set id-dep (list header)))
3737      ((null (car (symbol-value id-dep)))
3738       (setcar (symbol-value id-dep) header))
3739
3740      ;; From here the `header' was already present in the
3741      ;; `dependencies' table.
3742      (force-new
3743       ;; Overrides an existing entry;
3744       ;; just set the header part of the entry.
3745       (setcar (symbol-value id-dep) header)
3746       (setq replaced t))
3747
3748      ;; Renames the existing `header' to a unique Message-ID.
3749      ((not gnus-summary-ignore-duplicates)
3750       ;; An article with this Message-ID has already been seen.
3751       ;; We rename the Message-ID.
3752       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3753            (list header))
3754       (mail-header-set-id header id))
3755
3756      ;; The last case ignores an existing entry, except it adds any
3757      ;; additional Xrefs (in case the two articles came from different
3758      ;; servers.
3759      ;; Also sets `header' to `nil' meaning that the `dependencies'
3760      ;; table was *not* modified.
3761      (t
3762       (mail-header-set-xref
3763        (car (symbol-value id-dep))
3764        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3765                    "")
3766                (or (mail-header-xref header) "")))
3767       (setq header nil)))
3768
3769     (when (and header (not replaced))
3770       ;; First check that we are not creating a References loop.
3771       (setq parent-id (gnus-parent-id (mail-header-references header)))
3772       (setq ref parent-id)
3773       (while (and ref
3774                   (setq ref-dep (intern-soft ref dependencies))
3775                   (boundp ref-dep)
3776                   (setq ref-header (car (symbol-value ref-dep))))
3777         (if (string= id ref)
3778             ;; Yuk!  This is a reference loop.  Make the article be a
3779             ;; root article.
3780             (progn
3781               (mail-header-set-references (car (symbol-value id-dep)) "none")
3782               (setq ref nil)
3783               (setq parent-id nil))
3784           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3785       (setq ref-dep (intern (or parent-id "none") dependencies))
3786       (if (boundp ref-dep)
3787           (setcdr (symbol-value ref-dep)
3788                   (nconc (cdr (symbol-value ref-dep))
3789                          (list (symbol-value id-dep))))
3790         (set ref-dep (list nil (symbol-value id-dep)))))
3791     header))
3792
3793 (defun gnus-extract-message-id-from-in-reply-to (string)
3794   (if (string-match "<[^>]+>" string)
3795       (substring string (match-beginning 0) (match-end 0))
3796     nil))
3797
3798 (defun gnus-build-sparse-threads ()
3799   (let ((headers gnus-newsgroup-headers)
3800         (mail-parse-charset gnus-newsgroup-charset)
3801         (gnus-summary-ignore-duplicates t)
3802         header references generation relations
3803         subject child end new-child date)
3804     ;; First we create an alist of generations/relations, where
3805     ;; generations is how much we trust the relation, and the relation
3806     ;; is parent/child.
3807     (gnus-message 7 "Making sparse threads...")
3808     (save-excursion
3809       (nnheader-set-temp-buffer " *gnus sparse threads*")
3810       (while (setq header (pop headers))
3811         (when (and (setq references (mail-header-references header))
3812                    (not (string= references "")))
3813           (insert references)
3814           (setq child (mail-header-id header)
3815                 subject (mail-header-subject header)
3816                 date (mail-header-date header)
3817                 generation 0)
3818           (while (search-backward ">" nil t)
3819             (setq end (1+ (point)))
3820             (when (search-backward "<" nil t)
3821               (setq new-child (buffer-substring (point) end))
3822               (push (list (incf generation)
3823                           child (setq child new-child)
3824                           subject date)
3825                     relations)))
3826           (when child
3827             (push (list (1+ generation) child nil subject) relations))
3828           (erase-buffer)))
3829       (kill-buffer (current-buffer)))
3830     ;; Sort over trustworthiness.
3831     (mapcar
3832      (lambda (relation)
3833        (when (gnus-dependencies-add-header
3834               (make-full-mail-header
3835                gnus-reffed-article-number
3836                (nth 3 relation) "" (or (nth 4 relation) "")
3837                (nth 1 relation)
3838                (or (nth 2 relation) "") 0 0 "")
3839               gnus-newsgroup-dependencies nil)
3840          (push gnus-reffed-article-number gnus-newsgroup-limit)
3841          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3842          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3843                gnus-newsgroup-reads)
3844          (decf gnus-reffed-article-number)))
3845      (sort relations 'car-less-than-car))
3846     (gnus-message 7 "Making sparse threads...done")))
3847
3848 (defun gnus-build-old-threads ()
3849   ;; Look at all the articles that refer back to old articles, and
3850   ;; fetch the headers for the articles that aren't there.  This will
3851   ;; build complete threads - if the roots haven't been expired by the
3852   ;; server, that is.
3853   (let ((mail-parse-charset gnus-newsgroup-charset)
3854         id heads)
3855     (mapatoms
3856      (lambda (refs)
3857        (when (not (car (symbol-value refs)))
3858          (setq heads (cdr (symbol-value refs)))
3859          (while heads
3860            (if (memq (mail-header-number (caar heads))
3861                      gnus-newsgroup-dormant)
3862                (setq heads (cdr heads))
3863              (setq id (symbol-name refs))
3864              (while (and (setq id (gnus-build-get-header id))
3865                          (not (car (gnus-id-to-thread id)))))
3866              (setq heads nil)))))
3867      gnus-newsgroup-dependencies)))
3868
3869 ;; This function has to be called with point after the article number
3870 ;; on the beginning of the line.
3871 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3872   (let ((eol (gnus-point-at-eol))
3873         (buffer (current-buffer))
3874         header references in-reply-to)
3875
3876     ;; overview: [num subject from date id refs chars lines misc]
3877     (unwind-protect
3878         (let (x)
3879           (narrow-to-region (point) eol)
3880           (unless (eobp)
3881             (forward-char))
3882
3883           (setq header
3884                 (make-full-mail-header
3885                  number                 ; number
3886                  (condition-case ()     ; subject
3887                      (funcall gnus-decode-encoded-word-function
3888                               (setq x (nnheader-nov-field)))
3889                    (error x))
3890                  (condition-case ()     ; from
3891                      (funcall gnus-decode-encoded-word-function
3892                               (setq x (nnheader-nov-field)))
3893                    (error x))
3894                  (nnheader-nov-field)   ; date
3895                  (nnheader-nov-read-message-id) ; id
3896                  (setq references (nnheader-nov-field)) ; refs
3897                  (nnheader-nov-read-integer) ; chars
3898                  (nnheader-nov-read-integer) ; lines
3899                  (unless (eobp)
3900                    (if (looking-at "Xref: ")
3901                        (goto-char (match-end 0)))
3902                    (nnheader-nov-field)) ; Xref
3903                  (nnheader-nov-parse-extra)))) ; extra
3904
3905       (widen))
3906
3907     (when (and (string= references "")
3908                (setq in-reply-to (mail-header-extra header))
3909                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3910       (mail-header-set-references
3911        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3912
3913     (when gnus-alter-header-function
3914       (funcall gnus-alter-header-function header))
3915     (gnus-dependencies-add-header header dependencies force-new)))
3916
3917 (defun gnus-build-get-header (id)
3918   "Look through the buffer of NOV lines and find the header to ID.
3919 Enter this line into the dependencies hash table, and return
3920 the id of the parent article (if any)."
3921   (let ((deps gnus-newsgroup-dependencies)
3922         found header)
3923     (prog1
3924         (save-excursion
3925           (set-buffer nntp-server-buffer)
3926           (let ((case-fold-search nil))
3927             (goto-char (point-min))
3928             (while (and (not found)
3929                         (search-forward id nil t))
3930               (beginning-of-line)
3931               (setq found (looking-at
3932                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3933                                    (regexp-quote id))))
3934               (or found (beginning-of-line 2)))
3935             (when found
3936               (beginning-of-line)
3937               (and
3938                (setq header (gnus-nov-parse-line
3939                              (read (current-buffer)) deps))
3940                (gnus-parent-id (mail-header-references header))))))
3941       (when header
3942         (let ((number (mail-header-number header)))
3943           (push number gnus-newsgroup-limit)
3944           (push header gnus-newsgroup-headers)
3945           (if (memq number gnus-newsgroup-unselected)
3946               (progn
3947                 (setq gnus-newsgroup-unreads
3948                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3949                                                number))
3950                 (setq gnus-newsgroup-unselected
3951                       (delq number gnus-newsgroup-unselected)))
3952             (push number gnus-newsgroup-ancient)))))))
3953
3954 (defun gnus-build-all-threads ()
3955   "Read all the headers."
3956   (let ((gnus-summary-ignore-duplicates t)
3957         (mail-parse-charset gnus-newsgroup-charset)
3958         (dependencies gnus-newsgroup-dependencies)
3959         header article)
3960     (save-excursion
3961       (set-buffer nntp-server-buffer)
3962       (let ((case-fold-search nil))
3963         (goto-char (point-min))
3964         (while (not (eobp))
3965           (ignore-errors
3966             (setq article (read (current-buffer))
3967                   header (gnus-nov-parse-line article dependencies)))
3968           (when header
3969             (save-excursion
3970               (set-buffer gnus-summary-buffer)
3971               (push header gnus-newsgroup-headers)
3972               (if (memq (setq article (mail-header-number header))
3973                         gnus-newsgroup-unselected)
3974                   (progn
3975                     (setq gnus-newsgroup-unreads
3976                           (gnus-add-to-sorted-list
3977                            gnus-newsgroup-unreads article))
3978                     (setq gnus-newsgroup-unselected
3979                           (delq article gnus-newsgroup-unselected)))
3980                 (push article gnus-newsgroup-ancient)))
3981             (forward-line 1)))))))
3982
3983 (defun gnus-summary-update-article-line (article header)
3984   "Update the line for ARTICLE using HEADERS."
3985   (let* ((id (mail-header-id header))
3986          (thread (gnus-id-to-thread id)))
3987     (unless thread
3988       (error "Article in no thread"))
3989     ;; Update the thread.
3990     (setcar thread header)
3991     (gnus-summary-goto-subject article)
3992     (let* ((datal (gnus-data-find-list article))
3993            (data (car datal))
3994            (length (when (cdr datal)
3995                      (- (gnus-data-pos data)
3996                         (gnus-data-pos (cadr datal)))))
3997            (buffer-read-only nil)
3998            (level (gnus-summary-thread-level)))
3999       (gnus-delete-line)
4000       (gnus-summary-insert-line
4001        header level nil 
4002        (memq article gnus-newsgroup-undownloaded)
4003        (gnus-article-mark article)
4004        (memq article gnus-newsgroup-replied)
4005        (memq article gnus-newsgroup-expirable)
4006        ;; Only insert the Subject string when it's different
4007        ;; from the previous Subject string.
4008        (if (and
4009             gnus-show-threads
4010             (gnus-subject-equal
4011              (condition-case ()
4012                  (mail-header-subject
4013                   (gnus-data-header
4014                    (cadr
4015                     (gnus-data-find-list
4016                      article
4017                      (gnus-data-list t)))))
4018                ;; Error on the side of excessive subjects.
4019                (error ""))
4020              (mail-header-subject header)))
4021            ""
4022          (mail-header-subject header))
4023        nil (cdr (assq article gnus-newsgroup-scored))
4024        (memq article gnus-newsgroup-processable))
4025       (when length
4026         (gnus-data-update-list
4027          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4028
4029 (defun gnus-summary-update-article (article &optional iheader)
4030   "Update ARTICLE in the summary buffer."
4031   (set-buffer gnus-summary-buffer)
4032   (let* ((header (gnus-summary-article-header article))
4033          (id (mail-header-id header))
4034          (data (gnus-data-find article))
4035          (thread (gnus-id-to-thread id))
4036          (references (mail-header-references header))
4037          (parent
4038           (gnus-id-to-thread
4039            (or (gnus-parent-id
4040                 (when (and references
4041                            (not (equal "" references)))
4042                   references))
4043                "none")))
4044          (buffer-read-only nil)
4045          (old (car thread)))
4046     (when thread
4047       (unless iheader
4048         (setcar thread nil)
4049         (when parent
4050           (delq thread parent)))
4051       (if (gnus-summary-insert-subject id header)
4052           ;; Set the (possibly) new article number in the data structure.
4053           (gnus-data-set-number data (gnus-id-to-article id))
4054         (setcar thread old)
4055         nil))))
4056
4057 (defun gnus-rebuild-thread (id &optional line)
4058   "Rebuild the thread containing ID.
4059 If LINE, insert the rebuilt thread starting on line LINE."
4060   (let ((buffer-read-only nil)
4061         old-pos current thread data)
4062     (if (not gnus-show-threads)
4063         (setq thread (list (car (gnus-id-to-thread id))))
4064       ;; Get the thread this article is part of.
4065       (setq thread (gnus-remove-thread id)))
4066     (setq old-pos (gnus-point-at-bol))
4067     (setq current (save-excursion
4068                     (and (re-search-backward "[\r\n]" nil t)
4069                          (gnus-summary-article-number))))
4070     ;; If this is a gathered thread, we have to go some re-gathering.
4071     (when (stringp (car thread))
4072       (let ((subject (car thread))
4073             roots thr)
4074         (setq thread (cdr thread))
4075         (while thread
4076           (unless (memq (setq thr (gnus-id-to-thread
4077                                    (gnus-root-id
4078                                     (mail-header-id (caar thread)))))
4079                         roots)
4080             (push thr roots))
4081           (setq thread (cdr thread)))
4082         ;; We now have all (unique) roots.
4083         (if (= (length roots) 1)
4084             ;; All the loose roots are now one solid root.
4085             (setq thread (car roots))
4086           (setq thread (cons subject (gnus-sort-threads roots))))))
4087     (let (threads)
4088       ;; We then insert this thread into the summary buffer.
4089       (when line
4090         (goto-char (point-min))
4091         (forward-line (1- line)))
4092       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4093         (if gnus-show-threads
4094             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4095           (gnus-summary-prepare-unthreaded thread))
4096         (setq data (nreverse gnus-newsgroup-data))
4097         (setq threads gnus-newsgroup-threads))
4098       ;; We splice the new data into the data structure.
4099       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4100       ;;!!! then we want to insert at the beginning of the buffer.
4101       ;;!!! That happens to be true with Gnus now, but that may
4102       ;;!!! change in the future.  Perhaps.
4103       (gnus-data-enter-list
4104        (if line nil current) data (- (point) old-pos))
4105       (setq gnus-newsgroup-threads
4106             (nconc threads gnus-newsgroup-threads))
4107       (gnus-data-compute-positions))))
4108
4109 (defun gnus-number-to-header (number)
4110   "Return the header for article NUMBER."
4111   (let ((headers gnus-newsgroup-headers))
4112     (while (and headers
4113                 (not (= number (mail-header-number (car headers)))))
4114       (pop headers))
4115     (when headers
4116       (car headers))))
4117
4118 (defun gnus-parent-headers (in-headers &optional generation)
4119   "Return the headers of the GENERATIONeth parent of HEADERS."
4120   (unless generation
4121     (setq generation 1))
4122   (let ((parent t)
4123         (headers in-headers)
4124         references)
4125     (while (and parent
4126                 (not (zerop generation))
4127                 (setq references (mail-header-references headers)))
4128       (setq headers (if (and references
4129                              (setq parent (gnus-parent-id references)))
4130                         (car (gnus-id-to-thread parent))
4131                       nil))
4132       (decf generation))
4133     (and (not (eq headers in-headers))
4134          headers)))
4135
4136 (defun gnus-id-to-thread (id)
4137   "Return the (sub-)thread where ID appears."
4138   (gnus-gethash id gnus-newsgroup-dependencies))
4139
4140 (defun gnus-id-to-article (id)
4141   "Return the article number of ID."
4142   (let ((thread (gnus-id-to-thread id)))
4143     (when (and thread
4144                (car thread))
4145       (mail-header-number (car thread)))))
4146
4147 (defun gnus-id-to-header (id)
4148   "Return the article headers of ID."
4149   (car (gnus-id-to-thread id)))
4150
4151 (defun gnus-article-displayed-root-p (article)
4152   "Say whether ARTICLE is a root(ish) article."
4153   (let ((level (gnus-summary-thread-level article))
4154         (refs (mail-header-references  (gnus-summary-article-header article)))
4155         particle)
4156     (cond
4157      ((null level) nil)
4158      ((zerop level) t)
4159      ((null refs) t)
4160      ((null (gnus-parent-id refs)) t)
4161      ((and (= 1 level)
4162            (null (setq particle (gnus-id-to-article
4163                                  (gnus-parent-id refs))))
4164            (null (gnus-summary-thread-level particle)))))))
4165
4166 (defun gnus-root-id (id)
4167   "Return the id of the root of the thread where ID appears."
4168   (let (last-id prev)
4169     (while (and id (setq prev (car (gnus-id-to-thread id))))
4170       (setq last-id id
4171             id (gnus-parent-id (mail-header-references prev))))
4172     last-id))
4173
4174 (defun gnus-articles-in-thread (thread)
4175   "Return the list of articles in THREAD."
4176   (cons (mail-header-number (car thread))
4177         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4178
4179 (defun gnus-remove-thread (id &optional dont-remove)
4180   "Remove the thread that has ID in it."
4181   (let (headers thread last-id)
4182     ;; First go up in this thread until we find the root.
4183     (setq last-id (gnus-root-id id)
4184           headers (message-flatten-list (gnus-id-to-thread last-id)))
4185     ;; We have now found the real root of this thread.  It might have
4186     ;; been gathered into some loose thread, so we have to search
4187     ;; through the threads to find the thread we wanted.
4188     (let ((threads gnus-newsgroup-threads)
4189           sub)
4190       (while threads
4191         (setq sub (car threads))
4192         (if (stringp (car sub))
4193             ;; This is a gathered thread, so we look at the roots
4194             ;; below it to find whether this article is in this
4195             ;; gathered root.
4196             (progn
4197               (setq sub (cdr sub))
4198               (while sub
4199                 (when (member (caar sub) headers)
4200                   (setq thread (car threads)
4201                         threads nil
4202                         sub nil))
4203                 (setq sub (cdr sub))))
4204           ;; It's an ordinary thread, so we check it.
4205           (when (eq (car sub) (car headers))
4206             (setq thread sub
4207                   threads nil)))
4208         (setq threads (cdr threads)))
4209       ;; If this article is in no thread, then it's a root.
4210       (if thread
4211           (unless dont-remove
4212             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4213         (setq thread (gnus-id-to-thread last-id)))
4214       (when thread
4215         (prog1
4216             thread                      ; We return this thread.
4217           (unless dont-remove
4218             (if (stringp (car thread))
4219                 (progn
4220                   ;; If we use dummy roots, then we have to remove the
4221                   ;; dummy root as well.
4222                   (when (eq gnus-summary-make-false-root 'dummy)
4223                     ;; We go to the dummy root by going to
4224                     ;; the first sub-"thread", and then one line up.
4225                     (gnus-summary-goto-article
4226                      (mail-header-number (caadr thread)))
4227                     (forward-line -1)
4228                     (gnus-delete-line)
4229                     (gnus-data-compute-positions))
4230                   (setq thread (cdr thread))
4231                   (while thread
4232                     (gnus-remove-thread-1 (car thread))
4233                     (setq thread (cdr thread))))
4234               (gnus-remove-thread-1 thread))))))))
4235
4236 (defun gnus-remove-thread-1 (thread)
4237   "Remove the thread THREAD recursively."
4238   (let ((number (mail-header-number (pop thread)))
4239         d)
4240     (setq thread (reverse thread))
4241     (while thread
4242       (gnus-remove-thread-1 (pop thread)))
4243     (when (setq d (gnus-data-find number))
4244       (goto-char (gnus-data-pos d))
4245       (gnus-summary-show-thread)
4246       (gnus-data-remove
4247        number
4248        (- (gnus-point-at-bol)
4249           (prog1
4250               (1+ (gnus-point-at-eol))
4251             (gnus-delete-line)))))))
4252
4253 (defun gnus-sort-threads-1 (threads func)
4254   (sort (mapcar (lambda (thread)
4255                   (cons (car thread)
4256                         (and (cdr thread)
4257                              (gnus-sort-threads-1 (cdr thread) func))))
4258                 threads) func))
4259
4260 (defun gnus-sort-threads (threads)
4261   "Sort THREADS."
4262   (if (not gnus-thread-sort-functions)
4263       threads
4264     (gnus-message 8 "Sorting threads...")
4265     (let ((max-lisp-eval-depth 5000))
4266       (prog1 (gnus-sort-threads-1
4267          threads
4268          (gnus-make-sort-function gnus-thread-sort-functions))
4269         (gnus-message 8 "Sorting threads...done")))))
4270
4271 (defun gnus-sort-articles (articles)
4272   "Sort ARTICLES."
4273   (when gnus-article-sort-functions
4274     (gnus-message 7 "Sorting articles...")
4275     (prog1
4276         (setq gnus-newsgroup-headers
4277               (sort articles (gnus-make-sort-function
4278                               gnus-article-sort-functions)))
4279       (gnus-message 7 "Sorting articles...done"))))
4280
4281 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4282 (defmacro gnus-thread-header (thread)
4283   "Return header of first article in THREAD.
4284 Note that THREAD must never, ever be anything else than a variable -
4285 using some other form will lead to serious barfage."
4286   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4287   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4288   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4289         (vector thread) 2))
4290
4291 (defsubst gnus-article-sort-by-number (h1 h2)
4292   "Sort articles by article number."
4293   (< (mail-header-number h1)
4294      (mail-header-number h2)))
4295
4296 (defun gnus-thread-sort-by-number (h1 h2)
4297   "Sort threads by root article number."
4298   (gnus-article-sort-by-number
4299    (gnus-thread-header h1) (gnus-thread-header h2)))
4300
4301 (defsubst gnus-article-sort-by-random (h1 h2)
4302   "Sort articles by article number."
4303   (zerop (random 2)))
4304
4305 (defun gnus-thread-sort-by-random (h1 h2)
4306   "Sort threads by root article number."
4307   (gnus-article-sort-by-random
4308    (gnus-thread-header h1) (gnus-thread-header h2)))
4309
4310 (defsubst gnus-article-sort-by-lines (h1 h2)
4311   "Sort articles by article Lines header."
4312   (< (mail-header-lines h1)
4313      (mail-header-lines h2)))
4314
4315 (defun gnus-thread-sort-by-lines (h1 h2)
4316   "Sort threads by root article Lines header."
4317   (gnus-article-sort-by-lines
4318    (gnus-thread-header h1) (gnus-thread-header h2)))
4319
4320 (defsubst gnus-article-sort-by-chars (h1 h2)
4321   "Sort articles by octet length."
4322   (< (mail-header-chars h1)
4323      (mail-header-chars h2)))
4324
4325 (defun gnus-thread-sort-by-chars (h1 h2)
4326   "Sort threads by root article octet length."
4327   (gnus-article-sort-by-chars
4328    (gnus-thread-header h1) (gnus-thread-header h2)))
4329
4330 (defsubst gnus-article-sort-by-author (h1 h2)
4331   "Sort articles by root author."
4332   (string-lessp
4333    (let ((extract (funcall
4334                    gnus-extract-address-components
4335                    (mail-header-from h1))))
4336      (or (car extract) (cadr extract) ""))
4337    (let ((extract (funcall
4338                    gnus-extract-address-components
4339                    (mail-header-from h2))))
4340      (or (car extract) (cadr extract) ""))))
4341
4342 (defun gnus-thread-sort-by-author (h1 h2)
4343   "Sort threads by root author."
4344   (gnus-article-sort-by-author
4345    (gnus-thread-header h1)  (gnus-thread-header h2)))
4346
4347 (defsubst gnus-article-sort-by-subject (h1 h2)
4348   "Sort articles by root subject."
4349   (string-lessp
4350    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4351    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4352
4353 (defun gnus-thread-sort-by-subject (h1 h2)
4354   "Sort threads by root subject."
4355   (gnus-article-sort-by-subject
4356    (gnus-thread-header h1) (gnus-thread-header h2)))
4357
4358 (defsubst gnus-article-sort-by-date (h1 h2)
4359   "Sort articles by root article date."
4360   (time-less-p
4361    (gnus-date-get-time (mail-header-date h1))
4362    (gnus-date-get-time (mail-header-date h2))))
4363
4364 (defun gnus-thread-sort-by-date (h1 h2)
4365   "Sort threads by root article date."
4366   (gnus-article-sort-by-date
4367    (gnus-thread-header h1) (gnus-thread-header h2)))
4368
4369 (defsubst gnus-article-sort-by-score (h1 h2)
4370   "Sort articles by root article score.
4371 Unscored articles will be counted as having a score of zero."
4372   (> (or (cdr (assq (mail-header-number h1)
4373                     gnus-newsgroup-scored))
4374          gnus-summary-default-score 0)
4375      (or (cdr (assq (mail-header-number h2)
4376                     gnus-newsgroup-scored))
4377          gnus-summary-default-score 0)))
4378
4379 (defun gnus-thread-sort-by-score (h1 h2)
4380   "Sort threads by root article score."
4381   (gnus-article-sort-by-score
4382    (gnus-thread-header h1) (gnus-thread-header h2)))
4383
4384 (defun gnus-thread-sort-by-total-score (h1 h2)
4385   "Sort threads by the sum of all scores in the thread.
4386 Unscored articles will be counted as having a score of zero."
4387   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4388
4389 (defun gnus-thread-total-score (thread)
4390   ;; This function find the total score of THREAD.
4391   (cond
4392    ((null thread)
4393     0)
4394    ((consp thread)
4395     (if (stringp (car thread))
4396         (apply gnus-thread-score-function 0
4397                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4398       (gnus-thread-total-score-1 thread)))
4399    (t
4400     (gnus-thread-total-score-1 (list thread)))))
4401
4402 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4403   "Sort threads such that the thread with the most recently arrived article comes first."
4404   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4405
4406 (defun gnus-thread-highest-number (thread)
4407   "Return the highest article number in THREAD."
4408   (apply 'max (mapcar (lambda (header)
4409                         (mail-header-number header))
4410                       (message-flatten-list thread))))
4411
4412 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4413   "Sort threads such that the thread with the most recently dated article comes first."
4414   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4415
4416 (defun gnus-thread-latest-date (thread)
4417   "Return the highest article date in THREAD."
4418   (let ((previous-time 0))
4419     (apply 'max
4420            (mapcar
4421             (lambda (header)
4422               (setq previous-time
4423                     (time-to-seconds
4424                      (condition-case ()
4425                          (mail-header-parse-date (mail-header-date header))
4426                        (error previous-time)))))
4427             (sort
4428              (message-flatten-list thread)
4429              (lambda (h1 h2)
4430                (< (mail-header-number h1)
4431                   (mail-header-number h2))))))))
4432
4433 (defun gnus-thread-total-score-1 (root)
4434   ;; This function find the total score of the thread below ROOT.
4435   (setq root (car root))
4436   (apply gnus-thread-score-function
4437          (or (append
4438               (mapcar 'gnus-thread-total-score
4439                       (cdr (gnus-id-to-thread (mail-header-id root))))
4440               (when (> (mail-header-number root) 0)
4441                 (list (or (cdr (assq (mail-header-number root)
4442                                      gnus-newsgroup-scored))
4443                           gnus-summary-default-score 0))))
4444              (list gnus-summary-default-score)
4445              '(0))))
4446
4447 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4448 (defvar gnus-tmp-prev-subject nil)
4449 (defvar gnus-tmp-false-parent nil)
4450 (defvar gnus-tmp-root-expunged nil)
4451 (defvar gnus-tmp-dummy-line nil)
4452
4453 (eval-when-compile (defvar gnus-tmp-header))
4454 (defun gnus-extra-header (type &optional header)
4455   "Return the extra header of TYPE."
4456   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4457       ""))
4458
4459 (defvar gnus-tmp-thread-tree-header-string "")
4460
4461 (defcustom gnus-sum-thread-tree-root "> "
4462   "With %B spec, used for the root of a thread.
4463 If nil, use subject instead."
4464   :type 'string
4465   :group 'gnus-thread)
4466 (defcustom gnus-sum-thread-tree-single-indent ""
4467   "With %B spec, used for a thread with just one message.
4468 If nil, use subject instead."
4469   :type 'string
4470   :group 'gnus-thread)
4471 (defcustom gnus-sum-thread-tree-vertical "| "
4472   "With %B spec, used for drawing a vertical line."
4473   :type 'string
4474   :group 'gnus-thread)
4475 (defcustom gnus-sum-thread-tree-indent "  "
4476   "With %B spec, used for indenting."
4477   :type 'string
4478   :group 'gnus-thread)
4479 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4480   "With %B spec, used for a leaf with brothers."
4481   :type 'string
4482   :group 'gnus-thread)
4483 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4484   "With %B spec, used for a leaf without brothers."
4485   :type 'string
4486   :group 'gnus-thread)
4487
4488 (defun gnus-summary-prepare-threads (threads)
4489   "Prepare summary buffer from THREADS and indentation LEVEL.
4490 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4491 or a straight list of headers."
4492   (gnus-message 7 "Generating summary...")
4493
4494   (setq gnus-newsgroup-threads threads)
4495   (beginning-of-line)
4496
4497   (let ((gnus-tmp-level 0)
4498         (default-score (or gnus-summary-default-score 0))
4499         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4500         (building-line-count gnus-summary-display-while-building)
4501         (building-count (integerp gnus-summary-display-while-building))
4502         thread number subject stack state gnus-tmp-gathered beg-match
4503         new-roots gnus-tmp-new-adopts thread-end simp-subject
4504         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4505         gnus-tmp-replied gnus-tmp-subject-or-nil
4506         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4507         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4508         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4509         tree-stack)
4510
4511     (setq gnus-tmp-prev-subject nil)
4512
4513     (if (vectorp (car threads))
4514         ;; If this is a straight (sic) list of headers, then a
4515         ;; threaded summary display isn't required, so we just create
4516         ;; an unthreaded one.
4517         (gnus-summary-prepare-unthreaded threads)
4518
4519       ;; Do the threaded display.
4520
4521       (if gnus-summary-display-while-building
4522           (switch-to-buffer (buffer-name)))
4523       (while (or threads stack gnus-tmp-new-adopts new-roots)
4524
4525         (if (and (= gnus-tmp-level 0)
4526                  (or (not stack)
4527                      (= (caar stack) 0))
4528                  (not gnus-tmp-false-parent)
4529                  (or gnus-tmp-new-adopts new-roots))
4530             (if gnus-tmp-new-adopts
4531                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4532                       thread (list (car gnus-tmp-new-adopts))
4533                       gnus-tmp-header (caar thread)
4534                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4535               (when new-roots
4536                 (setq thread (list (car new-roots))
4537                       gnus-tmp-header (caar thread)
4538                       new-roots (cdr new-roots))))
4539
4540           (if threads
4541               ;; If there are some threads, we do them before the
4542               ;; threads on the stack.
4543               (setq thread threads
4544                     gnus-tmp-header (caar thread))
4545             ;; There were no current threads, so we pop something off
4546             ;; the stack.
4547             (setq state (car stack)
4548                   gnus-tmp-level (car state)
4549                   tree-stack (cadr state)
4550                   thread (caddr state)
4551                   stack (cdr stack)
4552                   gnus-tmp-header (caar thread))))
4553
4554         (setq gnus-tmp-false-parent nil)
4555         (setq gnus-tmp-root-expunged nil)
4556         (setq thread-end nil)
4557
4558         (if (stringp gnus-tmp-header)
4559             ;; The header is a dummy root.
4560             (cond
4561              ((eq gnus-summary-make-false-root 'adopt)
4562               ;; We let the first article adopt the rest.
4563               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4564                                                (cddar thread)))
4565               (setq gnus-tmp-gathered
4566                     (nconc (mapcar
4567                             (lambda (h) (mail-header-number (car h)))
4568                             (cddar thread))
4569                            gnus-tmp-gathered))
4570               (setq thread (cons (list (caar thread)
4571                                        (cadar thread))
4572                                  (cdr thread)))
4573               (setq gnus-tmp-level -1
4574                     gnus-tmp-false-parent t))
4575              ((eq gnus-summary-make-false-root 'empty)
4576               ;; We print adopted articles with empty subject fields.
4577               (setq gnus-tmp-gathered
4578                     (nconc (mapcar
4579                             (lambda (h) (mail-header-number (car h)))
4580                             (cddar thread))
4581                            gnus-tmp-gathered))
4582               (setq gnus-tmp-level -1))
4583              ((eq gnus-summary-make-false-root 'dummy)
4584               ;; We remember that we probably want to output a dummy
4585               ;; root.
4586               (setq gnus-tmp-dummy-line gnus-tmp-header)
4587               (setq gnus-tmp-prev-subject gnus-tmp-header))
4588              (t
4589               ;; We do not make a root for the gathered
4590               ;; sub-threads at all.
4591               (setq gnus-tmp-level -1)))
4592
4593           (setq number (mail-header-number gnus-tmp-header)
4594                 subject (mail-header-subject gnus-tmp-header)
4595                 simp-subject (gnus-simplify-subject-fully subject))
4596
4597           (cond
4598            ;; If the thread has changed subject, we might want to make
4599            ;; this subthread into a root.
4600            ((and (null gnus-thread-ignore-subject)
4601                  (not (zerop gnus-tmp-level))
4602                  gnus-tmp-prev-subject
4603                  (not (string= gnus-tmp-prev-subject simp-subject)))
4604             (setq new-roots (nconc new-roots (list (car thread)))
4605                   thread-end t
4606                   gnus-tmp-header nil))
4607            ;; If the article lies outside the current limit,
4608            ;; then we do not display it.
4609            ((not (memq number gnus-newsgroup-limit))
4610             (setq gnus-tmp-gathered
4611                   (nconc (mapcar
4612                           (lambda (h) (mail-header-number (car h)))
4613                           (cdar thread))
4614                          gnus-tmp-gathered))
4615             (setq gnus-tmp-new-adopts (if (cdar thread)
4616                                           (append gnus-tmp-new-adopts
4617                                                   (cdar thread))
4618                                         gnus-tmp-new-adopts)
4619                   thread-end t
4620                   gnus-tmp-header nil)
4621             (when (zerop gnus-tmp-level)
4622               (setq gnus-tmp-root-expunged t)))
4623            ;; Perhaps this article is to be marked as read?
4624            ((and gnus-summary-mark-below
4625                  (< (or (cdr (assq number gnus-newsgroup-scored))
4626                         default-score)
4627                     gnus-summary-mark-below)
4628                  ;; Don't touch sparse articles.
4629                  (not (gnus-summary-article-sparse-p number))
4630                  (not (gnus-summary-article-ancient-p number)))
4631             (setq gnus-newsgroup-unreads
4632                   (delq number gnus-newsgroup-unreads))
4633             (if gnus-newsgroup-auto-expire
4634                 (setq gnus-newsgroup-expirable
4635                       (gnus-add-to-sorted-list
4636                        gnus-newsgroup-expirable number))
4637               (push (cons number gnus-low-score-mark)
4638                     gnus-newsgroup-reads))))
4639
4640           (when gnus-tmp-header
4641             ;; We may have an old dummy line to output before this
4642             ;; article.
4643             (when (and gnus-tmp-dummy-line
4644                        (gnus-subject-equal
4645                         gnus-tmp-dummy-line
4646                         (mail-header-subject gnus-tmp-header)))
4647               (gnus-summary-insert-dummy-line
4648                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4649               (setq gnus-tmp-dummy-line nil))
4650
4651             ;; Compute the mark.
4652             (setq gnus-tmp-unread (gnus-article-mark number))
4653
4654             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4655                                   gnus-tmp-header gnus-tmp-level)
4656                   gnus-newsgroup-data)
4657
4658             ;; Actually insert the line.
4659             (setq
4660              gnus-tmp-subject-or-nil
4661              (cond
4662               ((and gnus-thread-ignore-subject
4663                     gnus-tmp-prev-subject
4664                     (not (string= gnus-tmp-prev-subject simp-subject)))
4665                subject)
4666               ((zerop gnus-tmp-level)
4667                (if (and (eq gnus-summary-make-false-root 'empty)
4668                         (memq number gnus-tmp-gathered)
4669                         gnus-tmp-prev-subject
4670                         (string= gnus-tmp-prev-subject simp-subject))
4671                    gnus-summary-same-subject
4672                  subject))
4673               (t gnus-summary-same-subject)))
4674             (if (and (eq gnus-summary-make-false-root 'adopt)
4675                      (= gnus-tmp-level 1)
4676                      (memq number gnus-tmp-gathered))
4677                 (setq gnus-tmp-opening-bracket ?\<
4678                       gnus-tmp-closing-bracket ?\>)
4679               (setq gnus-tmp-opening-bracket ?\[
4680                     gnus-tmp-closing-bracket ?\]))
4681             (setq
4682              gnus-tmp-indentation
4683              (aref gnus-thread-indent-array gnus-tmp-level)
4684              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4685              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4686                                 gnus-summary-default-score 0)
4687              gnus-tmp-score-char
4688              (if (or (null gnus-summary-default-score)
4689                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4690                          gnus-summary-zcore-fuzz))
4691                  ?                      ;Whitespace
4692                (if (< gnus-tmp-score gnus-summary-default-score)
4693                    gnus-score-below-mark gnus-score-over-mark))
4694              gnus-tmp-replied
4695              (cond ((memq number gnus-newsgroup-processable)
4696                     gnus-process-mark)
4697                    ((memq number gnus-newsgroup-cached)
4698                     gnus-cached-mark)
4699                    ((memq number gnus-newsgroup-replied)
4700                     gnus-replied-mark)
4701                    ((memq number gnus-newsgroup-forwarded)
4702                     gnus-forwarded-mark)
4703                    ((memq number gnus-newsgroup-saved)
4704                     gnus-saved-mark)
4705                    ((memq number gnus-newsgroup-recent)
4706                     gnus-recent-mark)
4707                    ((memq number gnus-newsgroup-unseen)
4708                     gnus-unseen-mark)
4709                    (t gnus-no-mark))
4710              gnus-tmp-downloaded
4711              (cond ((memq number gnus-newsgroup-undownloaded) 
4712                     gnus-undownloaded-mark)
4713                    (gnus-newsgroup-agentized
4714                     gnus-downloaded-mark)
4715                    (t
4716                     gnus-no-mark))
4717              gnus-tmp-from (mail-header-from gnus-tmp-header)
4718              gnus-tmp-name
4719              (cond
4720               ((string-match "<[^>]+> *$" gnus-tmp-from)
4721                (setq beg-match (match-beginning 0))
4722                (or (and (string-match "^\".+\"" gnus-tmp-from)
4723                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4724                    (substring gnus-tmp-from 0 beg-match)))
4725               ((string-match "(.+)" gnus-tmp-from)
4726                (substring gnus-tmp-from
4727                           (1+ (match-beginning 0)) (1- (match-end 0))))
4728               (t gnus-tmp-from))
4729              gnus-tmp-thread-tree-header-string
4730              (cond
4731               ((not gnus-show-threads) "")
4732               ((zerop gnus-tmp-level)
4733                (if (cdar thread)
4734                    (or gnus-sum-thread-tree-root subject)
4735                  (or gnus-sum-thread-tree-single-indent subject)))
4736               (t
4737                (concat (apply 'concat
4738                               (mapcar (lambda (item)
4739                                         (if (= item 1)
4740                                             gnus-sum-thread-tree-vertical
4741                                           gnus-sum-thread-tree-indent))
4742                                       (cdr (reverse tree-stack))))
4743                        (if (nth 1 thread)
4744                            gnus-sum-thread-tree-leaf-with-other
4745                          gnus-sum-thread-tree-single-leaf)))))
4746             (when (string= gnus-tmp-name "")
4747               (setq gnus-tmp-name gnus-tmp-from))
4748             (unless (numberp gnus-tmp-lines)
4749               (setq gnus-tmp-lines -1))
4750             (if (= gnus-tmp-lines -1)
4751                 (setq gnus-tmp-lines "?")
4752               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4753             (gnus-put-text-property
4754              (point)
4755              (progn (eval gnus-summary-line-format-spec) (point))
4756              'gnus-number number)
4757             (when gnus-visual-p
4758               (forward-line -1)
4759               (gnus-run-hooks 'gnus-summary-update-hook)
4760               (forward-line 1))
4761
4762             (setq gnus-tmp-prev-subject simp-subject)))
4763
4764         (when (nth 1 thread)
4765           (push (list (max 0 gnus-tmp-level)
4766                       (copy-list tree-stack)
4767                       (nthcdr 1 thread))
4768                 stack))
4769         (push (if (nth 1 thread) 1 0) tree-stack)
4770         (incf gnus-tmp-level)
4771         (setq threads (if thread-end nil (cdar thread)))
4772         (if gnus-summary-display-while-building
4773             (if building-count
4774                 (progn
4775                   ;; use a set frequency
4776                   (setq building-line-count (1- building-line-count))
4777                   (when (= building-line-count 0)
4778                     (sit-for 0)
4779                     (setq building-line-count
4780                           gnus-summary-display-while-building)))
4781               ;; always
4782               (sit-for 0)))
4783         (unless threads
4784           (setq gnus-tmp-level 0)))))
4785   (gnus-message 7 "Generating summary...done"))
4786
4787 (defun gnus-summary-prepare-unthreaded (headers)
4788   "Generate an unthreaded summary buffer based on HEADERS."
4789   (let (header number mark)
4790
4791     (beginning-of-line)
4792
4793     (while headers
4794       ;; We may have to root out some bad articles...
4795       (when (memq (setq number (mail-header-number
4796                                 (setq header (pop headers))))
4797                   gnus-newsgroup-limit)
4798         ;; Mark article as read when it has a low score.
4799         (when (and gnus-summary-mark-below
4800                    (< (or (cdr (assq number gnus-newsgroup-scored))
4801                           gnus-summary-default-score 0)
4802                       gnus-summary-mark-below)
4803                    (not (gnus-summary-article-ancient-p number)))
4804           (setq gnus-newsgroup-unreads
4805                 (delq number gnus-newsgroup-unreads))
4806           (if gnus-newsgroup-auto-expire
4807               (push number gnus-newsgroup-expirable)
4808             (push (cons number gnus-low-score-mark)
4809                   gnus-newsgroup-reads)))
4810
4811         (setq mark (gnus-article-mark number))
4812         (push (gnus-data-make number mark (1+ (point)) header 0)
4813               gnus-newsgroup-data)
4814         (gnus-summary-insert-line
4815          header 0 number
4816          (memq number gnus-newsgroup-undownloaded)
4817          mark (memq number gnus-newsgroup-replied)
4818          (memq number gnus-newsgroup-expirable)
4819          (mail-header-subject header) nil
4820          (cdr (assq number gnus-newsgroup-scored))
4821          (memq number gnus-newsgroup-processable))))))
4822
4823 (defun gnus-summary-remove-list-identifiers ()
4824   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4825   (let ((regexp (if (consp gnus-list-identifiers)
4826                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4827                   gnus-list-identifiers))
4828         changed subject)
4829     (when regexp
4830       (dolist (header gnus-newsgroup-headers)
4831         (setq subject (mail-header-subject header)
4832               changed nil)
4833         (while (string-match
4834                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4835                 subject)
4836           (setq subject
4837                 (concat (substring subject 0 (match-beginning 2))
4838                         (substring subject (match-end 0)))
4839                 changed t))
4840         (when (and changed
4841                    (string-match
4842                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4843           (setq subject
4844                 (concat (substring subject 0 (match-beginning 1))
4845                         (substring subject (match-end 1)))))
4846         (when changed
4847           (mail-header-set-subject header subject))))))
4848
4849 (defun gnus-fetch-headers (articles)
4850   "Fetch headers of ARTICLES."
4851   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4852     (gnus-message 5 "Fetching headers for %s..." name)
4853     (prog1
4854         (if (eq 'nov
4855                 (setq gnus-headers-retrieved-by
4856                       (gnus-retrieve-headers
4857                        articles gnus-newsgroup-name
4858                        ;; We might want to fetch old headers, but
4859                        ;; not if there is only 1 article.
4860                        (and (or (and
4861                                  (not (eq gnus-fetch-old-headers 'some))
4862                                  (not (numberp gnus-fetch-old-headers)))
4863                                 (> (length articles) 1))
4864                             gnus-fetch-old-headers))))
4865             (gnus-get-newsgroup-headers-xover
4866              articles nil nil gnus-newsgroup-name t)
4867           (gnus-get-newsgroup-headers))
4868       (gnus-message 5 "Fetching headers for %s...done" name))))
4869
4870 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4871   "Select newsgroup GROUP.
4872 If READ-ALL is non-nil, all articles in the group are selected.
4873 If SELECT-ARTICLES, only select those articles from GROUP."
4874   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4875          ;;!!! Dirty hack; should be removed.
4876          (gnus-summary-ignore-duplicates
4877           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4878               t
4879             gnus-summary-ignore-duplicates))
4880          (info (nth 2 entry))
4881          articles fetched-articles cached)
4882
4883     (unless (gnus-check-server
4884              (set (make-local-variable 'gnus-current-select-method)
4885                   (gnus-find-method-for-group group)))
4886       (error "Couldn't open server"))
4887
4888     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4889         (gnus-activate-group group)     ; Or we can activate it...
4890         (progn                          ; Or we bug out.
4891           (when (equal major-mode 'gnus-summary-mode)
4892             (kill-buffer (current-buffer)))
4893           (error "Couldn't activate group %s: %s"
4894                  group (gnus-status-message group))))
4895
4896     (unless (gnus-request-group group t)
4897       (when (equal major-mode 'gnus-summary-mode)
4898         (kill-buffer (current-buffer)))
4899       (error "Couldn't request group %s: %s"
4900              group (gnus-status-message group)))
4901
4902     (setq gnus-newsgroup-name group
4903           gnus-newsgroup-unselected nil
4904           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4905
4906     (let ((display (gnus-group-find-parameter group 'display)))
4907       (setq gnus-newsgroup-display
4908             (cond
4909              ((not (zerop (or (car-safe read-all) 0)))
4910               ;; The user entered the group with C-u SPC/RET, let's show
4911               ;; all articles.
4912               'gnus-not-ignore)
4913              ((eq display 'all)
4914               'gnus-not-ignore)
4915              ((arrayp display)
4916               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4917              ((numberp display)
4918               ;; The following is probably the "correct" solution, but
4919               ;; it makes Gnus fetch all headers and then limit the
4920               ;; articles (which is slow), so instead we hack the
4921               ;; select-articles parameter instead. -- Simon Josefsson
4922               ;; <jas@kth.se>
4923               ;;
4924               ;; (gnus-byte-compile
4925               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4926               ;;                         display)))))
4927               (setq select-articles
4928                     (gnus-uncompress-range
4929                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4930                              (if (> tmp 0)
4931                                  tmp
4932                                1))
4933                            (cdr (gnus-active group)))))
4934               nil)
4935              (t
4936               nil))))
4937
4938     (gnus-summary-setup-default-charset)
4939
4940     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4941     (when (gnus-virtual-group-p group)
4942       (setq cached gnus-newsgroup-cached))
4943
4944     (setq gnus-newsgroup-unreads
4945           (gnus-sorted-ndifference
4946            (gnus-sorted-ndifference gnus-newsgroup-unreads
4947                                     gnus-newsgroup-marked)
4948            gnus-newsgroup-dormant))
4949
4950     (setq gnus-newsgroup-processable nil)
4951
4952     (gnus-update-read-articles group gnus-newsgroup-unreads)
4953
4954     ;; Adjust and set lists of article marks.
4955     (when info
4956       (gnus-adjust-marked-articles info))
4957     (if (setq articles select-articles)
4958         (setq gnus-newsgroup-unselected
4959               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4960       (setq articles (gnus-articles-to-read group read-all)))
4961
4962     (cond
4963      ((null articles)
4964       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4965       'quit)
4966      ((eq articles 0) nil)
4967      (t
4968       ;; Init the dependencies hash table.
4969       (setq gnus-newsgroup-dependencies
4970             (gnus-make-hashtable (length articles)))
4971       (gnus-set-global-variables)
4972       ;; Retrieve the headers and read them in.
4973
4974       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4975
4976       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4977       (when cached
4978         (setq gnus-newsgroup-cached cached))
4979
4980       ;; Suppress duplicates?
4981       (when gnus-suppress-duplicates
4982         (gnus-dup-suppress-articles))
4983
4984       ;; Set the initial limit.
4985       (setq gnus-newsgroup-limit (copy-sequence articles))
4986       ;; Remove canceled articles from the list of unread articles.
4987       (setq fetched-articles
4988             (mapcar (lambda (headers) (mail-header-number headers))
4989                     gnus-newsgroup-headers))
4990       (setq gnus-newsgroup-articles fetched-articles)
4991       (setq gnus-newsgroup-unreads
4992             (gnus-sorted-nintersection
4993              gnus-newsgroup-unreads fetched-articles))
4994       (gnus-compute-unseen-list)
4995
4996       ;; Removed marked articles that do not exist.
4997       (gnus-update-missing-marks
4998        (gnus-sorted-difference articles fetched-articles))
4999       ;; We might want to build some more threads first.
5000       (when (and gnus-fetch-old-headers
5001                  (eq gnus-headers-retrieved-by 'nov))
5002         (if (eq gnus-fetch-old-headers 'invisible)
5003             (gnus-build-all-threads)
5004           (gnus-build-old-threads)))
5005       ;; Let the Gnus agent mark articles as read.
5006       (when gnus-agent
5007         (gnus-agent-get-undownloaded-list))
5008       ;; Remove list identifiers from subject
5009       (when gnus-list-identifiers
5010         (gnus-summary-remove-list-identifiers))
5011       ;; Check whether auto-expire is to be done in this group.
5012       (setq gnus-newsgroup-auto-expire
5013             (gnus-group-auto-expirable-p group))
5014       ;; Set up the article buffer now, if necessary.
5015       (unless gnus-single-article-buffer
5016         (gnus-article-setup-buffer))
5017       ;; First and last article in this newsgroup.
5018       (when gnus-newsgroup-headers
5019         (setq gnus-newsgroup-begin
5020               (mail-header-number (car gnus-newsgroup-headers))
5021               gnus-newsgroup-end
5022               (mail-header-number
5023                (gnus-last-element gnus-newsgroup-headers))))
5024       ;; GROUP is successfully selected.
5025       (or gnus-newsgroup-headers t)))))
5026
5027 (defun gnus-compute-unseen-list ()
5028   ;; The `seen' marks are treated specially.
5029   (if (not gnus-newsgroup-seen)
5030       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5031     (setq gnus-newsgroup-unseen
5032           (gnus-inverse-list-range-intersection
5033            gnus-newsgroup-articles gnus-newsgroup-seen))))
5034
5035 (defun gnus-summary-display-make-predicate (display)
5036   (require 'gnus-agent)
5037   (when (= (length display) 1)
5038     (setq display (car display)))
5039   (unless gnus-summary-display-cache
5040     (dolist (elem (append '((unread . unread)
5041                             (read . read)
5042                             (unseen . unseen))
5043                           gnus-article-mark-lists))
5044       (push (cons (cdr elem)
5045                   (gnus-byte-compile
5046                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5047             gnus-summary-display-cache)))
5048   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5049         (gnus-category-predicate-cache gnus-summary-display-cache))
5050     (gnus-get-predicate display)))
5051
5052 ;; Uses the dynamically bound `number' variable.
5053 (eval-when-compile
5054   (defvar number))
5055 (defun gnus-article-marked-p (type &optional article)
5056   (let ((article (or article number)))
5057     (cond
5058      ((eq type 'tick)
5059       (memq article gnus-newsgroup-marked))
5060      ((eq type 'spam)
5061       (memq article gnus-newsgroup-spam-marked))
5062      ((eq type 'unsend)
5063       (memq article gnus-newsgroup-unsendable))
5064      ((eq type 'undownload)
5065       (memq article gnus-newsgroup-undownloaded))
5066      ((eq type 'download)
5067       (memq article gnus-newsgroup-downloadable))
5068      ((eq type 'unread)
5069       (memq article gnus-newsgroup-unreads))
5070      ((eq type 'read)
5071       (memq article gnus-newsgroup-reads))
5072      ((eq type 'dormant)
5073       (memq article gnus-newsgroup-dormant) )
5074      ((eq type 'expire)
5075       (memq article gnus-newsgroup-expirable))
5076      ((eq type 'reply)
5077       (memq article gnus-newsgroup-replied))
5078      ((eq type 'killed)
5079       (memq article gnus-newsgroup-killed))
5080      ((eq type 'bookmark)
5081       (assq article gnus-newsgroup-bookmarks))
5082      ((eq type 'score)
5083       (assq article gnus-newsgroup-scored))
5084      ((eq type 'save)
5085       (memq article gnus-newsgroup-saved))
5086      ((eq type 'cache)
5087       (memq article gnus-newsgroup-cached))
5088      ((eq type 'forward)
5089       (memq article gnus-newsgroup-forwarded))
5090      ((eq type 'seen)
5091       (not (memq article gnus-newsgroup-unseen)))
5092      ((eq type 'recent)
5093       (memq article gnus-newsgroup-recent))
5094      (t t))))
5095
5096 (defun gnus-articles-to-read (group &optional read-all)
5097   "Find out what articles the user wants to read."
5098   (let* ((display (gnus-group-find-parameter group 'display))
5099          (articles
5100           ;; Select all articles if `read-all' is non-nil, or if there
5101           ;; are no unread articles.
5102           (if (or read-all
5103                   (and (zerop (length gnus-newsgroup-marked))
5104                        (zerop (length gnus-newsgroup-unreads)))
5105                   ;; Fetch all if the predicate is non-nil.
5106                   gnus-newsgroup-display)
5107               ;; We want to select the headers for all the articles in
5108               ;; the group, so we select either all the active
5109               ;; articles in the group, or (if that's nil), the
5110               ;; articles in the cache.
5111               (or
5112                (gnus-uncompress-range (gnus-active group))
5113                (gnus-cache-articles-in-group group))
5114             ;; Select only the "normal" subset of articles.
5115             (gnus-sorted-nunion
5116              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5117              gnus-newsgroup-unreads)))
5118          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5119          (scored (length scored-list))
5120          (number (length articles))
5121          (marked (+ (length gnus-newsgroup-marked)
5122                     (length gnus-newsgroup-dormant)))
5123          (select
5124           (cond
5125            ((numberp read-all)
5126             read-all)
5127            ((numberp gnus-newsgroup-display)
5128             gnus-newsgroup-display)
5129            (t
5130             (condition-case ()
5131                 (cond
5132                  ((and (or (<= scored marked) (= scored number))
5133                        (numberp gnus-large-newsgroup)
5134                        (> number gnus-large-newsgroup))
5135                   (let* ((cursor-in-echo-area nil)
5136                          (initial (gnus-parameter-large-newsgroup-initial
5137                                    gnus-newsgroup-name))
5138                          (input
5139                           (read-string
5140                            (format
5141                             "How many articles from %s (%s %d): "
5142                             (gnus-limit-string
5143                              (gnus-group-decoded-name gnus-newsgroup-name)
5144                              35)
5145                             (if initial "max" "default")
5146                             number)
5147                            (if initial
5148                                (cons (number-to-string initial)
5149                                      0)))))
5150                     (if (string-match "^[ \t]*$" input) number input)))
5151                  ((and (> scored marked) (< scored number)
5152                        (> (- scored number) 20))
5153                   (let ((input
5154                          (read-string
5155                           (format "%s %s (%d scored, %d total): "
5156                                   "How many articles from"
5157                                   (gnus-group-decoded-name group)
5158                                   scored number))))
5159                     (if (string-match "^[ \t]*$" input)
5160                         number input)))
5161                  (t number))
5162               (quit
5163                (message "Quit getting the articles to read")
5164                nil))))))
5165     (setq select (if (stringp select) (string-to-number select) select))
5166     (if (or (null select) (zerop select))
5167         select
5168       (if (and (not (zerop scored)) (<= (abs select) scored))
5169           (progn
5170             (setq articles (sort scored-list '<))
5171             (setq number (length articles)))
5172         (setq articles (copy-sequence articles)))
5173
5174       (when (< (abs select) number)
5175         (if (< select 0)
5176             ;; Select the N oldest articles.
5177             (setcdr (nthcdr (1- (abs select)) articles) nil)
5178           ;; Select the N most recent articles.
5179           (setq articles (nthcdr (- number select) articles))))
5180       (setq gnus-newsgroup-unselected
5181             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5182       (when gnus-alter-articles-to-read-function
5183         (setq gnus-newsgroup-unreads
5184               (sort
5185                (funcall gnus-alter-articles-to-read-function
5186                         gnus-newsgroup-name gnus-newsgroup-unreads)
5187                '<)))
5188       articles)))
5189
5190 (defun gnus-killed-articles (killed articles)
5191   (let (out)
5192     (while articles
5193       (when (inline (gnus-member-of-range (car articles) killed))
5194         (push (car articles) out))
5195       (setq articles (cdr articles)))
5196     out))
5197
5198 (defun gnus-uncompress-marks (marks)
5199   "Uncompress the mark ranges in MARKS."
5200   (let ((uncompressed '(score bookmark))
5201         out)
5202     (while marks
5203       (if (memq (caar marks) uncompressed)
5204           (push (car marks) out)
5205         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5206       (setq marks (cdr marks)))
5207     out))
5208
5209 (defun gnus-article-mark-to-type (mark)
5210   "Return the type of MARK."
5211   (or (cadr (assq mark gnus-article-special-mark-lists))
5212       'list))
5213
5214 (defun gnus-article-unpropagatable-p (mark)
5215   "Return whether MARK should be propagated to backend."
5216   (memq mark gnus-article-unpropagated-mark-lists))
5217
5218 (defun gnus-adjust-marked-articles (info)
5219   "Set all article lists and remove all marks that are no longer valid."
5220   (let* ((marked-lists (gnus-info-marks info))
5221          (active (gnus-active (gnus-info-group info)))
5222          (min (car active))
5223          (max (cdr active))
5224          (types gnus-article-mark-lists)
5225          marks var articles article mark mark-type)
5226
5227     (dolist (marks marked-lists)
5228       (setq mark (car marks)
5229             mark-type (gnus-article-mark-to-type mark)
5230             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5231
5232       ;; We set the variable according to the type of the marks list,
5233       ;; and then adjust the marks to a subset of the active articles.
5234       (cond
5235        ;; Adjust "simple" lists.
5236        ((eq mark-type 'list)
5237         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5238         (when (memq mark '(tick dormant expire reply save))
5239           (while articles
5240             (when (or (< (setq article (pop articles)) min) (> article max))
5241               (set var (delq article (symbol-value var)))))))
5242        ;; Adjust assocs.
5243        ((eq mark-type 'tuple)
5244         (set var (setq articles (cdr marks)))
5245         (when (not (listp (cdr (symbol-value var))))
5246           (set var (list (symbol-value var))))
5247         (when (not (listp (cdr articles)))
5248           (setq articles (list articles)))
5249         (while articles
5250           (when (or (not (consp (setq article (pop articles))))
5251                     (< (car article) min)
5252                     (> (car article) max))
5253             (set var (delq article (symbol-value var))))))
5254        ;; Adjust ranges (sloppily).
5255        ((eq mark-type 'range)
5256         (cond
5257          ((eq mark 'seen)
5258           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5259           ;; It should be (seen (NUM1 . NUM2)).
5260           (when (numberp (cddr marks))
5261             (setcdr marks (list (cdr marks))))
5262           (setq articles (cdr marks))
5263           (while (and articles
5264                       (or (and (consp (car articles))
5265                                (> min (cdar articles)))
5266                           (and (numberp (car articles))
5267                                (> min (car articles)))))
5268             (pop articles))
5269           (set var articles))))))))
5270
5271 (defun gnus-update-missing-marks (missing)
5272   "Go through the list of MISSING articles and remove them from the mark lists."
5273   (when missing
5274     (let (var m)
5275       ;; Go through all types.
5276       (dolist (elem gnus-article-mark-lists)
5277         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5278           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5279           (when (symbol-value var)
5280             ;; This list has articles.  So we delete all missing
5281             ;; articles from it.
5282             (setq m missing)
5283             (while m
5284               (set var (delq (pop m) (symbol-value var))))))))))
5285
5286 (defun gnus-update-marks ()
5287   "Enter the various lists of marked articles into the newsgroup info list."
5288   (let ((types gnus-article-mark-lists)
5289         (info (gnus-get-info gnus-newsgroup-name))
5290         type list newmarked symbol delta-marks)
5291     (when info
5292       ;; Add all marks lists to the list of marks lists.
5293       (while (setq type (pop types))
5294         (setq list (symbol-value
5295                     (setq symbol
5296                           (intern (format "gnus-newsgroup-%s" (car type))))))
5297
5298         (when list
5299           ;; Get rid of the entries of the articles that have the
5300           ;; default score.
5301           (when (and (eq (cdr type) 'score)
5302                      gnus-save-score
5303                      list)
5304             (let* ((arts list)
5305                    (prev (cons nil list))
5306                    (all prev))
5307               (while arts
5308                 (if (or (not (consp (car arts)))
5309                         (= (cdar arts) gnus-summary-default-score))
5310                     (setcdr prev (cdr arts))
5311                   (setq prev arts))
5312                 (setq arts (cdr arts)))
5313               (setq list (cdr all)))))
5314
5315         (when (eq (cdr type) 'seen)
5316           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5317
5318         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5319           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5320
5321         (when (and (gnus-check-backend-function
5322                     'request-set-mark gnus-newsgroup-name)
5323                    (not (gnus-article-unpropagatable-p (cdr type))))
5324           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5325                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5326                  (add (gnus-remove-from-range
5327                        (gnus-copy-sequence list) old)))
5328             (when add
5329               (push (list add 'add (list (cdr type))) delta-marks))
5330             (when del
5331               (push (list del 'del (list (cdr type))) delta-marks))))
5332
5333         (when list
5334           (push (cons (cdr type) list) newmarked)))
5335
5336       (when delta-marks
5337         (unless (gnus-check-group gnus-newsgroup-name)
5338           (error "Can't open server for %s" gnus-newsgroup-name))
5339         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5340
5341       ;; Enter these new marks into the info of the group.
5342       (if (nthcdr 3 info)
5343           (setcar (nthcdr 3 info) newmarked)
5344         ;; Add the marks lists to the end of the info.
5345         (when newmarked
5346           (setcdr (nthcdr 2 info) (list newmarked))))
5347
5348       ;; Cut off the end of the info if there's nothing else there.
5349       (let ((i 5))
5350         (while (and (> i 2)
5351                     (not (nth i info)))
5352           (when (nthcdr (decf i) info)
5353             (setcdr (nthcdr i info) nil)))))))
5354
5355 (defun gnus-set-mode-line (where)
5356   "Set the mode line of the article or summary buffers.
5357 If WHERE is `summary', the summary mode line format will be used."
5358   ;; Is this mode line one we keep updated?
5359   (when (and (memq where gnus-updated-mode-lines)
5360              (symbol-value
5361               (intern (format "gnus-%s-mode-line-format-spec" where))))
5362     (let (mode-string)
5363       (save-excursion
5364         ;; We evaluate this in the summary buffer since these
5365         ;; variables are buffer-local to that buffer.
5366         (set-buffer gnus-summary-buffer)
5367        ;; We bind all these variables that are used in the `eval' form
5368         ;; below.
5369         (let* ((mformat (symbol-value
5370                          (intern
5371                           (format "gnus-%s-mode-line-format-spec" where))))
5372                (gnus-tmp-group-name (gnus-group-decoded-name
5373                                      gnus-newsgroup-name))
5374                (gnus-tmp-article-number (or gnus-current-article 0))
5375                (gnus-tmp-unread gnus-newsgroup-unreads)
5376                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5377                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5378                (gnus-tmp-unread-and-unselected
5379                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5380                             (zerop gnus-tmp-unselected))
5381                        "")
5382                       ((zerop gnus-tmp-unselected)
5383                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5384                       (t (format "{%d(+%d) more}"
5385                                  gnus-tmp-unread-and-unticked
5386                                  gnus-tmp-unselected))))
5387                (gnus-tmp-subject
5388                 (if (and gnus-current-headers
5389                          (vectorp gnus-current-headers))
5390                     (gnus-mode-string-quote
5391                      (mail-header-subject gnus-current-headers))
5392                   ""))
5393                bufname-length max-len
5394                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5395           (setq mode-string (eval mformat))
5396           (setq bufname-length (if (string-match "%b" mode-string)
5397                                    (- (length
5398                                        (buffer-name
5399                                         (if (eq where 'summary)
5400                                             nil
5401                                           (get-buffer gnus-article-buffer))))
5402                                       2)
5403                                  0))
5404           (setq max-len (max 4 (if gnus-mode-non-string-length
5405                                    (- (window-width)
5406                                       gnus-mode-non-string-length
5407                                       bufname-length)
5408                                  (length mode-string))))
5409           ;; We might have to chop a bit of the string off...
5410           (when (> (length mode-string) max-len)
5411             (setq mode-string
5412                   (concat (truncate-string-to-width mode-string (- max-len 3))
5413                           "...")))
5414           ;; Pad the mode string a bit.
5415           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5416       ;; Update the mode line.
5417       (setq mode-line-buffer-identification
5418             (gnus-mode-line-buffer-identification (list mode-string)))
5419       (set-buffer-modified-p t))))
5420
5421 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5422   "Go through the HEADERS list and add all Xrefs to a hash table.
5423 The resulting hash table is returned, or nil if no Xrefs were found."
5424   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5425          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5426          (xref-hashtb (gnus-make-hashtable))
5427          start group entry number xrefs header)
5428     (while headers
5429       (setq header (pop headers))
5430       (when (and (setq xrefs (mail-header-xref header))
5431                  (not (memq (setq number (mail-header-number header))
5432                             unreads)))
5433         (setq start 0)
5434         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5435           (setq start (match-end 0))
5436           (setq group (if prefix
5437                           (concat prefix (substring xrefs (match-beginning 1)
5438                                                     (match-end 1)))
5439                         (substring xrefs (match-beginning 1) (match-end 1))))
5440           (setq number
5441                 (string-to-int (substring xrefs (match-beginning 2)
5442                                           (match-end 2))))
5443           (if (setq entry (gnus-gethash group xref-hashtb))
5444               (setcdr entry (cons number (cdr entry)))
5445             (gnus-sethash group (cons number nil) xref-hashtb)))))
5446     (and start xref-hashtb)))
5447
5448 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5449   "Look through all the headers and mark the Xrefs as read."
5450   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5451         name entry info xref-hashtb idlist method nth4)
5452     (save-excursion
5453       (set-buffer gnus-group-buffer)
5454       (when (setq xref-hashtb
5455                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5456         (mapatoms
5457          (lambda (group)
5458            (unless (string= from-newsgroup (setq name (symbol-name group)))
5459              (setq idlist (symbol-value group))
5460              ;; Dead groups are not updated.
5461              (and (prog1
5462                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5463                             info (nth 2 entry))
5464                     (when (stringp (setq nth4 (gnus-info-method info)))
5465                       (setq nth4 (gnus-server-to-method nth4))))
5466                   ;; Only do the xrefs if the group has the same
5467                   ;; select method as the group we have just read.
5468                   (or (gnus-methods-equal-p
5469                        nth4 (gnus-find-method-for-group from-newsgroup))
5470                       virtual
5471                       (equal nth4 (setq method (gnus-find-method-for-group
5472                                                 from-newsgroup)))
5473                       (and (equal (car nth4) (car method))
5474                            (equal (nth 1 nth4) (nth 1 method))))
5475                   gnus-use-cross-reference
5476                   (or (not (eq gnus-use-cross-reference t))
5477                       virtual
5478                       ;; Only do cross-references on subscribed
5479                       ;; groups, if that is what is wanted.
5480                       (<= (gnus-info-level info) gnus-level-subscribed))
5481                   (gnus-group-make-articles-read name idlist))))
5482          xref-hashtb)))))
5483
5484 (defun gnus-compute-read-articles (group articles)
5485   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5486          (info (nth 2 entry))
5487          (active (gnus-active group))
5488          ninfo)
5489     (when entry
5490       ;; First peel off all invalid article numbers.
5491       (when active
5492         (let ((ids articles)
5493               id first)
5494           (while (setq id (pop ids))
5495             (when (and first (> id (cdr active)))
5496               ;; We'll end up in this situation in one particular
5497               ;; obscure situation.  If you re-scan a group and get
5498               ;; a new article that is cross-posted to a different
5499               ;; group that has not been re-scanned, you might get
5500               ;; crossposted article that has a higher number than
5501               ;; Gnus believes possible.  So we re-activate this
5502               ;; group as well.  This might mean doing the
5503               ;; crossposting thingy will *increase* the number
5504               ;; of articles in some groups.  Tsk, tsk.
5505               (setq active (or (gnus-activate-group group) active)))
5506             (when (or (> id (cdr active))
5507                       (< id (car active)))
5508               (setq articles (delq id articles))))))
5509       ;; If the read list is nil, we init it.
5510       (if (and active
5511                (null (gnus-info-read info))
5512                (> (car active) 1))
5513           (setq ninfo (cons 1 (1- (car active))))
5514         (setq ninfo (gnus-info-read info)))
5515       ;; Then we add the read articles to the range.
5516       (gnus-add-to-range
5517        ninfo (setq articles (sort articles '<))))))
5518
5519 (defun gnus-group-make-articles-read (group articles)
5520   "Update the info of GROUP to say that ARTICLES are read."
5521   (let* ((num 0)
5522          (entry (gnus-gethash group gnus-newsrc-hashtb))
5523          (info (nth 2 entry))
5524          (active (gnus-active group))
5525          range)
5526     (when entry
5527       (setq range (gnus-compute-read-articles group articles))
5528       (save-excursion
5529         (set-buffer gnus-group-buffer)
5530         (gnus-undo-register
5531           `(progn
5532              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5533              (gnus-info-set-read ',info ',(gnus-info-read info))
5534              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5535              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5536              (gnus-group-update-group ,group t))))
5537       ;; Add the read articles to the range.
5538       (gnus-info-set-read info range)
5539       (gnus-request-set-mark group (list (list range 'add '(read))))
5540       ;; Then we have to re-compute how many unread
5541       ;; articles there are in this group.
5542       (when active
5543         (cond
5544          ((not range)
5545           (setq num (- (1+ (cdr active)) (car active))))
5546          ((not (listp (cdr range)))
5547           (setq num (- (cdr active) (- (1+ (cdr range))
5548                                        (car range)))))
5549          (t
5550           (while range
5551             (if (numberp (car range))
5552                 (setq num (1+ num))
5553               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5554             (setq range (cdr range)))
5555           (setq num (- (cdr active) num))))
5556         ;; Update the number of unread articles.
5557         (setcar entry num)
5558         ;; Update the group buffer.
5559         (unless (gnus-ephemeral-group-p group)
5560           (gnus-group-update-group group t))))))
5561
5562 (defvar gnus-newsgroup-none-id 0)
5563
5564 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5565   (let ((cur nntp-server-buffer)
5566         (dependencies
5567          (or dependencies
5568              (save-excursion (set-buffer gnus-summary-buffer)
5569                              gnus-newsgroup-dependencies)))
5570         headers id end ref
5571         (mail-parse-charset gnus-newsgroup-charset)
5572         (mail-parse-ignored-charsets
5573          (save-excursion (condition-case nil
5574                              (set-buffer gnus-summary-buffer)
5575                            (error))
5576                          gnus-newsgroup-ignored-charsets)))
5577     (save-excursion
5578       (set-buffer nntp-server-buffer)
5579       ;; Translate all TAB characters into SPACE characters.
5580       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5581       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5582       (gnus-run-hooks 'gnus-parse-headers-hook)
5583       (let ((case-fold-search t)
5584             in-reply-to header p lines chars)
5585         (goto-char (point-min))
5586         ;; Search to the beginning of the next header.  Error messages
5587         ;; do not begin with 2 or 3.
5588         (while (re-search-forward "^[23][0-9]+ " nil t)
5589           (setq id nil
5590                 ref nil)
5591           ;; This implementation of this function, with nine
5592           ;; search-forwards instead of the one re-search-forward and
5593           ;; a case (which basically was the old function) is actually
5594           ;; about twice as fast, even though it looks messier.  You
5595           ;; can't have everything, I guess.  Speed and elegance
5596           ;; doesn't always go hand in hand.
5597           (setq
5598            header
5599            (vector
5600             ;; Number.
5601             (prog1
5602                 (read cur)
5603               (end-of-line)
5604               (setq p (point))
5605               (narrow-to-region (point)
5606                                 (or (and (search-forward "\n.\n" nil t)
5607                                          (- (point) 2))
5608                                     (point))))
5609             ;; Subject.
5610             (progn
5611               (goto-char p)
5612               (if (search-forward "\nsubject:" nil t)
5613                   (funcall gnus-decode-encoded-word-function
5614                            (nnheader-header-value))
5615                 "(none)"))
5616             ;; From.
5617             (progn
5618               (goto-char p)
5619               (if (search-forward "\nfrom:" nil t)
5620                   (funcall gnus-decode-encoded-word-function
5621                            (nnheader-header-value))
5622                 "(nobody)"))
5623             ;; Date.
5624             (progn
5625               (goto-char p)
5626               (if (search-forward "\ndate:" nil t)
5627                   (nnheader-header-value) ""))
5628             ;; Message-ID.
5629             (progn
5630               (goto-char p)
5631               (setq id (if (re-search-forward
5632                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5633                            ;; We do it this way to make sure the Message-ID
5634                            ;; is (somewhat) syntactically valid.
5635                            (buffer-substring (match-beginning 1)
5636                                              (match-end 1))
5637                          ;; If there was no message-id, we just fake one
5638                          ;; to make subsequent routines simpler.
5639                          (nnheader-generate-fake-message-id))))
5640             ;; References.
5641             (progn
5642               (goto-char p)
5643               (if (search-forward "\nreferences:" nil t)
5644                   (progn
5645                     (setq end (point))
5646                     (prog1
5647                         (nnheader-header-value)
5648                       (setq ref
5649                             (buffer-substring
5650                              (progn
5651                                (end-of-line)
5652                                (search-backward ">" end t)
5653                                (1+ (point)))
5654                              (progn
5655                                (search-backward "<" end t)
5656                                (point))))))
5657                 ;; Get the references from the in-reply-to header if there
5658                 ;; were no references and the in-reply-to header looks
5659                 ;; promising.
5660                 (if (and (search-forward "\nin-reply-to:" nil t)
5661                          (setq in-reply-to (nnheader-header-value))
5662                          (string-match "<[^>]+>" in-reply-to))
5663                     (let (ref2)
5664                       (setq ref (substring in-reply-to (match-beginning 0)
5665                                            (match-end 0)))
5666                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5667                         (setq ref2 (substring in-reply-to (match-beginning 0)
5668                                               (match-end 0)))
5669                         (when (> (length ref2) (length ref))
5670                           (setq ref ref2)))
5671                       ref)
5672                   (setq ref nil))))
5673             ;; Chars.
5674             (progn
5675               (goto-char p)
5676               (if (search-forward "\nchars: " nil t)
5677                   (if (numberp (setq chars (ignore-errors (read cur))))
5678                       chars -1)
5679                 -1))
5680             ;; Lines.
5681             (progn
5682               (goto-char p)
5683               (if (search-forward "\nlines: " nil t)
5684                   (if (numberp (setq lines (ignore-errors (read cur))))
5685                       lines -1)
5686                 -1))
5687             ;; Xref.
5688             (progn
5689               (goto-char p)
5690               (and (search-forward "\nxref:" nil t)
5691                    (nnheader-header-value)))
5692             ;; Extra.
5693             (when gnus-extra-headers
5694               (let ((extra gnus-extra-headers)
5695                     out)
5696                 (while extra
5697                   (goto-char p)
5698                   (when (search-forward
5699                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5700                     (push (cons (car extra) (nnheader-header-value))
5701                           out))
5702                   (pop extra))
5703                 out))))
5704           (when (equal id ref)
5705             (setq ref nil))
5706
5707           (when gnus-alter-header-function
5708             (funcall gnus-alter-header-function header)
5709             (setq id (mail-header-id header)
5710                   ref (gnus-parent-id (mail-header-references header))))
5711
5712           (when (setq header
5713                       (gnus-dependencies-add-header
5714                        header dependencies force-new))
5715             (push header headers))
5716           (goto-char (point-max))
5717           (widen))
5718         (nreverse headers)))))
5719
5720 ;; Goes through the xover lines and returns a list of vectors
5721 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5722                                                   force-new dependencies
5723                                                   group also-fetch-heads)
5724   "Parse the news overview data in the server buffer.
5725 Return a list of headers that match SEQUENCE (see
5726 `nntp-retrieve-headers')."
5727   ;; Get the Xref when the users reads the articles since most/some
5728   ;; NNTP servers do not include Xrefs when using XOVER.
5729   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5730   (let ((mail-parse-charset gnus-newsgroup-charset)
5731         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5732         (cur nntp-server-buffer)
5733         (dependencies (or dependencies gnus-newsgroup-dependencies))
5734         (allp (cond
5735                ((eq gnus-read-all-available-headers t)
5736                 t)
5737                ((stringp gnus-read-all-available-headers)
5738                 (string-match gnus-read-all-available-headers group))
5739                (t
5740                 nil)))
5741         number headers header)
5742     (save-excursion
5743       (set-buffer nntp-server-buffer)
5744       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5745       ;; Allow the user to mangle the headers before parsing them.
5746       (gnus-run-hooks 'gnus-parse-headers-hook)
5747       (goto-char (point-min))
5748       (gnus-parse-without-error
5749         (while (and (or sequence allp)
5750                     (not (eobp)))
5751           (setq number (read cur))
5752           (when (not allp)
5753             (while (and sequence
5754                         (< (car sequence) number))
5755               (setq sequence (cdr sequence))))
5756           (when (and (or allp
5757                          (and sequence
5758                               (eq number (car sequence))))
5759                      (progn
5760                        (setq sequence (cdr sequence))
5761                        (setq header (inline
5762                                       (gnus-nov-parse-line
5763                                        number dependencies force-new)))))
5764             (push header headers))
5765           (forward-line 1)))
5766       ;; A common bug in inn is that if you have posted an article and
5767       ;; then retrieves the active file, it will answer correctly --
5768       ;; the new article is included.  However, a NOV entry for the
5769       ;; article may not have been generated yet, so this may fail.
5770       ;; We work around this problem by retrieving the last few
5771       ;; headers using HEAD.
5772       (if (or (not also-fetch-heads)
5773               (not sequence))
5774           ;; We (probably) got all the headers.
5775           (nreverse headers)
5776         (let ((gnus-nov-is-evil t))
5777           (nconc
5778            (nreverse headers)
5779            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5780              (gnus-get-newsgroup-headers))))))))
5781
5782 (defun gnus-article-get-xrefs ()
5783   "Fill in the Xref value in `gnus-current-headers', if necessary.
5784 This is meant to be called in `gnus-article-internal-prepare-hook'."
5785   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5786                                  gnus-current-headers)))
5787     (or (not gnus-use-cross-reference)
5788         (not headers)
5789         (and (mail-header-xref headers)
5790              (not (string= (mail-header-xref headers) "")))
5791         (let ((case-fold-search t)
5792               xref)
5793           (save-restriction
5794             (nnheader-narrow-to-headers)
5795             (goto-char (point-min))
5796             (when (or (and (not (eobp))
5797                            (eq (downcase (char-after)) ?x)
5798                            (looking-at "Xref:"))
5799                       (search-forward "\nXref:" nil t))
5800               (goto-char (1+ (match-end 0)))
5801               (setq xref (buffer-substring (point)
5802                                            (progn (end-of-line) (point))))
5803               (mail-header-set-xref headers xref)))))))
5804
5805 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5806   "Find article ID and insert the summary line for that article.
5807 OLD-HEADER can either be a header or a line number to insert
5808 the subject line on."
5809   (let* ((line (and (numberp old-header) old-header))
5810          (old-header (and (vectorp old-header) old-header))
5811          (header (cond ((and old-header use-old-header)
5812                         old-header)
5813                        ((and (numberp id)
5814                              (gnus-number-to-header id))
5815                         (gnus-number-to-header id))
5816                        (t
5817                         (gnus-read-header id))))
5818          (number (and (numberp id) id))
5819          d)
5820     (when header
5821       ;; Rebuild the thread that this article is part of and go to the
5822       ;; article we have fetched.
5823       (when (and (not gnus-show-threads)
5824                  old-header)
5825         (when (and number
5826                    (setq d (gnus-data-find (mail-header-number old-header))))
5827           (goto-char (gnus-data-pos d))
5828           (gnus-data-remove
5829            number
5830            (- (gnus-point-at-bol)
5831               (prog1
5832                   (1+ (gnus-point-at-eol))
5833                 (gnus-delete-line))))))
5834       (when old-header
5835         (mail-header-set-number header (mail-header-number old-header)))
5836       (setq gnus-newsgroup-sparse
5837             (delq (setq number (mail-header-number header))
5838                   gnus-newsgroup-sparse))
5839       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5840       (push number gnus-newsgroup-limit)
5841       (gnus-rebuild-thread (mail-header-id header) line)
5842       (gnus-summary-goto-subject number nil t))
5843     (when (and (numberp number)
5844                (> number 0))
5845       ;; We have to update the boundaries even if we can't fetch the
5846       ;; article if ID is a number -- so that the next `P' or `N'
5847       ;; command will fetch the previous (or next) article even
5848       ;; if the one we tried to fetch this time has been canceled.
5849       (when (> number gnus-newsgroup-end)
5850         (setq gnus-newsgroup-end number))
5851       (when (< number gnus-newsgroup-begin)
5852         (setq gnus-newsgroup-begin number))
5853       (setq gnus-newsgroup-unselected
5854             (delq number gnus-newsgroup-unselected)))
5855     ;; Report back a success?
5856     (and header (mail-header-number header))))
5857
5858 ;;; Process/prefix in the summary buffer
5859
5860 (defun gnus-summary-work-articles (n)
5861   "Return a list of articles to be worked upon.
5862 The prefix argument, the list of process marked articles, and the
5863 current article will be taken into consideration."
5864   (save-excursion
5865     (set-buffer gnus-summary-buffer)
5866     (cond
5867      (n
5868       ;; A numerical prefix has been given.
5869       (setq n (prefix-numeric-value n))
5870       (let ((backward (< n 0))
5871             (n (abs (prefix-numeric-value n)))
5872             articles article)
5873         (save-excursion
5874           (while
5875               (and (> n 0)
5876                    (push (setq article (gnus-summary-article-number))
5877                          articles)
5878                    (if backward
5879                        (gnus-summary-find-prev nil article)
5880                      (gnus-summary-find-next nil article)))
5881             (decf n)))
5882         (nreverse articles)))
5883      ((and (gnus-region-active-p) (mark))
5884       (message "region active")
5885       ;; Work on the region between point and mark.
5886       (let ((max (max (point) (mark)))
5887             articles article)
5888         (save-excursion
5889           (goto-char (min (min (point) (mark))))
5890           (while
5891               (and
5892                (push (setq article (gnus-summary-article-number)) articles)
5893                (gnus-summary-find-next nil article)
5894                (< (point) max)))
5895           (nreverse articles))))
5896      (gnus-newsgroup-processable
5897       ;; There are process-marked articles present.
5898       ;; Save current state.
5899       (gnus-summary-save-process-mark)
5900       ;; Return the list.
5901       (reverse gnus-newsgroup-processable))
5902      (t
5903       ;; Just return the current article.
5904       (list (gnus-summary-article-number))))))
5905
5906 (defmacro gnus-summary-iterate (arg &rest forms)
5907   "Iterate over the process/prefixed articles and do FORMS.
5908 ARG is the interactive prefix given to the command.  FORMS will be
5909 executed with point over the summary line of the articles."
5910   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5911     `(let ((,articles (gnus-summary-work-articles ,arg)))
5912        (while ,articles
5913          (gnus-summary-goto-subject (car ,articles))
5914          ,@forms
5915          (pop ,articles)))))
5916
5917 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5918 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5919
5920 (defun gnus-summary-save-process-mark ()
5921   "Push the current set of process marked articles on the stack."
5922   (interactive)
5923   (push (copy-sequence gnus-newsgroup-processable)
5924         gnus-newsgroup-process-stack))
5925
5926 (defun gnus-summary-kill-process-mark ()
5927   "Push the current set of process marked articles on the stack and unmark."
5928   (interactive)
5929   (gnus-summary-save-process-mark)
5930   (gnus-summary-unmark-all-processable))
5931
5932 (defun gnus-summary-yank-process-mark ()
5933   "Pop the last process mark state off the stack and restore it."
5934   (interactive)
5935   (unless gnus-newsgroup-process-stack
5936     (error "Empty mark stack"))
5937   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5938
5939 (defun gnus-summary-process-mark-set (set)
5940   "Make SET into the current process marked articles."
5941   (gnus-summary-unmark-all-processable)
5942   (while set
5943     (gnus-summary-set-process-mark (pop set))))
5944
5945 ;;; Searching and stuff
5946
5947 (defun gnus-summary-search-group (&optional backward use-level)
5948   "Search for next unread newsgroup.
5949 If optional argument BACKWARD is non-nil, search backward instead."
5950   (save-excursion
5951     (set-buffer gnus-group-buffer)
5952     (when (gnus-group-search-forward
5953            backward nil (if use-level (gnus-group-group-level) nil))
5954       (gnus-group-group-name))))
5955
5956 (defun gnus-summary-best-group (&optional exclude-group)
5957   "Find the name of the best unread group.
5958 If EXCLUDE-GROUP, do not go to this group."
5959   (save-excursion
5960     (set-buffer gnus-group-buffer)
5961     (save-excursion
5962       (gnus-group-best-unread-group exclude-group))))
5963
5964 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5965   (if backward (gnus-summary-find-prev)
5966     (let* ((dummy (gnus-summary-article-intangible-p))
5967            (article (or article (gnus-summary-article-number)))
5968            (arts (gnus-data-find-list article))
5969            result)
5970       (when (and (not dummy)
5971                  (or (not gnus-summary-check-current)
5972                      (not unread)
5973                      (not (gnus-data-unread-p (car arts)))))
5974         (setq arts (cdr arts)))
5975       (when (setq result
5976                   (if unread
5977                       (progn
5978                         (while arts
5979                           (when (or (and undownloaded
5980                                          (memq (car arts) gnus-newsgroup-undownloaded))
5981                                     (gnus-data-unread-p (car arts)))
5982                             (setq result (car arts)
5983                                   arts nil))
5984                           (setq arts (cdr arts)))
5985                         result)
5986                     (car arts)))
5987         (goto-char (gnus-data-pos result))
5988         (gnus-data-number result)))))
5989
5990 (defun gnus-summary-find-prev (&optional unread article)
5991   (let* ((eobp (eobp))
5992          (article (or article (gnus-summary-article-number)))
5993          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5994          result)
5995     (when (and (not eobp)
5996                (or (not gnus-summary-check-current)
5997                    (not unread)
5998                    (not (gnus-data-unread-p (car arts)))))
5999       (setq arts (cdr arts)))
6000     (when (setq result
6001                 (if unread
6002                     (progn
6003                       (while arts
6004                         (when (gnus-data-unread-p (car arts))
6005                           (setq result (car arts)
6006                                 arts nil))
6007                         (setq arts (cdr arts)))
6008                       result)
6009                   (car arts)))
6010       (goto-char (gnus-data-pos result))
6011       (gnus-data-number result))))
6012
6013 (defun gnus-summary-find-subject (subject &optional unread backward article)
6014   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6015          (article (or article (gnus-summary-article-number)))
6016          (articles (gnus-data-list backward))
6017          (arts (gnus-data-find-list article articles))
6018          result)
6019     (when (or (not gnus-summary-check-current)
6020               (not unread)
6021               (not (gnus-data-unread-p (car arts))))
6022       (setq arts (cdr arts)))
6023     (while arts
6024       (and (or (not unread)
6025                (gnus-data-unread-p (car arts)))
6026            (vectorp (gnus-data-header (car arts)))
6027            (gnus-subject-equal
6028             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6029            (setq result (car arts)
6030                  arts nil))
6031       (setq arts (cdr arts)))
6032     (and result
6033          (goto-char (gnus-data-pos result))
6034          (gnus-data-number result))))
6035
6036 (defun gnus-summary-search-forward (&optional unread subject backward)
6037   "Search forward for an article.
6038 If UNREAD, look for unread articles.  If SUBJECT, look for
6039 articles with that subject.  If BACKWARD, search backward instead."
6040   (cond (subject (gnus-summary-find-subject subject unread backward))
6041         (backward (gnus-summary-find-prev unread))
6042         (t (gnus-summary-find-next unread))))
6043
6044 (defun gnus-recenter (&optional n)
6045   "Center point in window and redisplay frame.
6046 Also do horizontal recentering."
6047   (interactive "P")
6048   (when (and gnus-auto-center-summary
6049              (not (eq gnus-auto-center-summary 'vertical)))
6050     (gnus-horizontal-recenter))
6051   (recenter n))
6052
6053 (defun gnus-summary-recenter ()
6054   "Center point in the summary window.
6055 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6056 displayed, no centering will be performed."
6057   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6058 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6059   (interactive)
6060   (let* ((top (cond ((< (window-height) 4) 0)
6061                     ((< (window-height) 7) 1)
6062                     (t (if (numberp gnus-auto-center-summary)
6063                            gnus-auto-center-summary
6064                          2))))
6065          (height (1- (window-height)))
6066          (bottom (save-excursion (goto-char (point-max))
6067                                  (forward-line (- height))
6068                                  (point)))
6069          (window (get-buffer-window (current-buffer))))
6070     ;; The user has to want it.
6071     (when gnus-auto-center-summary
6072       (when (get-buffer-window gnus-article-buffer)
6073         ;; Only do recentering when the article buffer is displayed,
6074       ;; Set the window start to either `bottom', which is the biggest
6075         ;; possible valid number, or the second line from the top,
6076         ;; whichever is the least.
6077         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6078           (if (> bottom top-pos)
6079               ;; Keep the second line from the top visible
6080               (set-window-start window top-pos t)
6081             ;; Try to keep the bottom line visible; if it's partially
6082             ;; obscured, either scroll one more line to make it fully
6083             ;; visible, or revert to using TOP-POS.
6084             (save-excursion
6085               (goto-char (point-max))
6086               (forward-line -1)
6087               (let ((last-line-start (point)))
6088                 (goto-char bottom)
6089                 (set-window-start window (point) t)
6090                 (when (not (pos-visible-in-window-p last-line-start window))
6091                   (forward-line 1)
6092                   (set-window-start window (min (point) top-pos) t)))))))
6093       ;; Do horizontal recentering while we're at it.
6094       (when (and (get-buffer-window (current-buffer) t)
6095                  (not (eq gnus-auto-center-summary 'vertical)))
6096         (let ((selected (selected-window)))
6097           (select-window (get-buffer-window (current-buffer) t))
6098           (gnus-summary-position-point)
6099           (gnus-horizontal-recenter)
6100           (select-window selected))))))
6101
6102 (defun gnus-summary-jump-to-group (newsgroup)
6103   "Move point to NEWSGROUP in group mode buffer."
6104   ;; Keep update point of group mode buffer if visible.
6105   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6106       (save-window-excursion
6107         ;; Take care of tree window mode.
6108         (when (get-buffer-window gnus-group-buffer)
6109           (pop-to-buffer gnus-group-buffer))
6110         (gnus-group-jump-to-group newsgroup))
6111     (save-excursion
6112       ;; Take care of tree window mode.
6113       (if (get-buffer-window gnus-group-buffer)
6114           (pop-to-buffer gnus-group-buffer)
6115         (set-buffer gnus-group-buffer))
6116       (gnus-group-jump-to-group newsgroup))))
6117
6118 ;; This function returns a list of article numbers based on the
6119 ;; difference between the ranges of read articles in this group and
6120 ;; the range of active articles.
6121 (defun gnus-list-of-unread-articles (group)
6122   (let* ((read (gnus-info-read (gnus-get-info group)))
6123          (active (or (gnus-active group) (gnus-activate-group group)))
6124          (last (cdr active))
6125          first nlast unread)
6126     ;; If none are read, then all are unread.
6127     (if (not read)
6128         (setq first (car active))
6129       ;; If the range of read articles is a single range, then the
6130       ;; first unread article is the article after the last read
6131       ;; article.  Sounds logical, doesn't it?
6132       (if (and (not (listp (cdr read)))
6133                (or (< (car read) (car active))
6134                    (progn (setq read (list read))
6135                           nil)))
6136           (setq first (max (car active) (1+ (cdr read))))
6137         ;; `read' is a list of ranges.
6138         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6139                                   (caar read)))
6140                   1)
6141           (setq first (car active)))
6142         (while read
6143           (when first
6144             (while (< first nlast)
6145               (push first unread)
6146               (setq first (1+ first))))
6147           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6148           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6149           (setq read (cdr read)))))
6150     ;; And add the last unread articles.
6151     (while (<= first last)
6152       (push first unread)
6153       (setq first (1+ first)))
6154     ;; Return the list of unread articles.
6155     (delq 0 (nreverse unread))))
6156
6157 (defun gnus-list-of-read-articles (group)
6158   "Return a list of unread, unticked and non-dormant articles."
6159   (let* ((info (gnus-get-info group))
6160          (marked (gnus-info-marks info))
6161          (active (gnus-active group)))
6162     (and info active
6163          (gnus-list-range-difference
6164           (gnus-list-range-difference
6165            (gnus-sorted-complement
6166             (gnus-uncompress-range active)
6167             (gnus-list-of-unread-articles group))
6168            (cdr (assq 'dormant marked)))
6169           (cdr (assq 'tick marked))))))
6170
6171 ;; Various summary commands
6172
6173 (defun gnus-summary-select-article-buffer ()
6174   "Reconfigure windows to show article buffer."
6175   (interactive)
6176   (if (not (gnus-buffer-live-p gnus-article-buffer))
6177       (error "There is no article buffer for this summary buffer")
6178     (gnus-configure-windows 'article)
6179     (select-window (get-buffer-window gnus-article-buffer))))
6180
6181 (defun gnus-summary-universal-argument (arg)
6182   "Perform any operation on all articles that are process/prefixed."
6183   (interactive "P")
6184   (let ((articles (gnus-summary-work-articles arg))
6185         func article)
6186     (if (eq
6187          (setq
6188           func
6189           (key-binding
6190            (read-key-sequence
6191             (substitute-command-keys
6192              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6193          'undefined)
6194         (gnus-error 1 "Undefined key")
6195       (save-excursion
6196         (while articles
6197           (gnus-summary-goto-subject (setq article (pop articles)))
6198           (let (gnus-newsgroup-processable)
6199             (command-execute func))
6200           (gnus-summary-remove-process-mark article)))))
6201   (gnus-summary-position-point))
6202
6203 (defun gnus-summary-toggle-truncation (&optional arg)
6204   "Toggle truncation of summary lines.
6205 With arg, turn line truncation on if arg is positive."
6206   (interactive "P")
6207   (setq truncate-lines
6208         (if (null arg) (not truncate-lines)
6209           (> (prefix-numeric-value arg) 0)))
6210   (redraw-display))
6211
6212 (defun gnus-summary-find-uncancelled ()
6213   "Return the number of an uncancelled article.
6214 The current article is considered, then following articles, then previous
6215 articles.  If all articles are cancelled then return a dummy 0."
6216   (let (found)
6217     (dolist (rev '(nil t))
6218       (unless found      ; don't demand the reverse list if we don't need it
6219         (let ((data (gnus-data-find-list
6220                      (gnus-summary-article-number) (gnus-data-list rev))))
6221           (while (and data (not found))
6222             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6223                 (setq found (gnus-data-number (car data))))
6224             (setq data (cdr data))))))
6225     (or found 0)))
6226
6227 (defun gnus-summary-reselect-current-group (&optional all rescan)
6228   "Exit and then reselect the current newsgroup.
6229 The prefix argument ALL means to select all articles."
6230   (interactive "P")
6231   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6232     (error "Ephemeral groups can't be reselected"))
6233   (let ((current-subject (gnus-summary-find-uncancelled))
6234         (group gnus-newsgroup-name))
6235     (setq gnus-newsgroup-begin nil)
6236     (gnus-summary-exit)
6237     ;; We have to adjust the point of group mode buffer because
6238     ;; point was moved to the next unread newsgroup by exiting.
6239     (gnus-summary-jump-to-group group)
6240     (when rescan
6241       (save-excursion
6242         (gnus-group-get-new-news-this-group 1)))
6243     (gnus-group-read-group all t)
6244     (gnus-summary-goto-subject current-subject nil t)))
6245
6246 (defun gnus-summary-rescan-group (&optional all)
6247   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6248   (interactive "P")
6249   (gnus-summary-reselect-current-group all t))
6250
6251 (defun gnus-summary-update-info (&optional non-destructive)
6252   (save-excursion
6253     (let ((group gnus-newsgroup-name))
6254       (when group
6255         (when gnus-newsgroup-kill-headers
6256           (setq gnus-newsgroup-killed
6257                 (gnus-compress-sequence
6258                  (gnus-sorted-union
6259                   (gnus-list-range-intersection
6260                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6261                   gnus-newsgroup-unreads)
6262                  t)))
6263         (unless (listp (cdr gnus-newsgroup-killed))
6264           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6265         (let ((headers gnus-newsgroup-headers))
6266           ;; Set the new ranges of read articles.
6267           (save-excursion
6268             (set-buffer gnus-group-buffer)
6269             (gnus-undo-force-boundary))
6270           (gnus-update-read-articles
6271            group (gnus-sorted-union
6272                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6273           ;; Set the current article marks.
6274           (let ((gnus-newsgroup-scored
6275                  (if (and (not gnus-save-score)
6276                           (not non-destructive))
6277                      nil
6278                    gnus-newsgroup-scored)))
6279             (save-excursion
6280               (gnus-update-marks)))
6281           ;; Do the cross-ref thing.
6282           (when gnus-use-cross-reference
6283             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6284           ;; Do not switch windows but change the buffer to work.
6285           (set-buffer gnus-group-buffer)
6286           (unless (gnus-ephemeral-group-p group)
6287             (gnus-group-update-group group)))))))
6288
6289 (defun gnus-summary-save-newsrc (&optional force)
6290   "Save the current number of read/marked articles in the dribble buffer.
6291 The dribble buffer will then be saved.
6292 If FORCE (the prefix), also save the .newsrc file(s)."
6293   (interactive "P")
6294   (gnus-summary-update-info t)
6295   (if force
6296       (gnus-save-newsrc-file)
6297     (gnus-dribble-save)))
6298
6299 (defun gnus-summary-exit (&optional temporary)
6300   "Exit reading current newsgroup, and then return to group selection mode.
6301 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6302   (interactive)
6303   (gnus-set-global-variables)
6304   (when (gnus-buffer-live-p gnus-article-buffer)
6305     (save-excursion
6306       (set-buffer gnus-article-buffer)
6307       (mm-destroy-parts gnus-article-mime-handles)
6308       ;; Set it to nil for safety reason.
6309       (setq gnus-article-mime-handle-alist nil)
6310       (setq gnus-article-mime-handles nil)))
6311   (gnus-kill-save-kill-buffer)
6312   (gnus-async-halt-prefetch)
6313   (let* ((group gnus-newsgroup-name)
6314          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6315          (gnus-group-is-exiting-p t)
6316          (mode major-mode)
6317          (group-point nil)
6318          (buf (current-buffer)))
6319     (unless quit-config
6320       ;; Do adaptive scoring, and possibly save score files.
6321       (when gnus-newsgroup-adaptive
6322         (gnus-score-adaptive))
6323       (when gnus-use-scoring
6324         (gnus-score-save)))
6325     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6326     ;; If we have several article buffers, we kill them at exit.
6327     (unless gnus-single-article-buffer
6328       (gnus-kill-buffer gnus-original-article-buffer)
6329       (setq gnus-article-current nil))
6330     (when gnus-use-cache
6331       (gnus-cache-possibly-remove-articles)
6332       (gnus-cache-save-buffers))
6333     (gnus-async-prefetch-remove-group group)
6334     (when gnus-suppress-duplicates
6335       (gnus-dup-enter-articles))
6336     (when gnus-use-trees
6337       (gnus-tree-close group))
6338     (when gnus-use-cache
6339       (gnus-cache-write-active))
6340     ;; Remove entries for this group.
6341     (nnmail-purge-split-history (gnus-group-real-name group))
6342     ;; Make all changes in this group permanent.
6343     (unless quit-config
6344       (gnus-run-hooks 'gnus-exit-group-hook)
6345       (gnus-summary-update-info))
6346     (gnus-close-group group)
6347     ;; Make sure where we were, and go to next newsgroup.
6348     (set-buffer gnus-group-buffer)
6349     (unless quit-config
6350       (gnus-group-jump-to-group group))
6351     (gnus-run-hooks 'gnus-summary-exit-hook)
6352     (unless (or quit-config
6353                 ;; If this group has disappeared from the summary
6354                 ;; buffer, don't skip forwards.
6355                 (not (string= group (gnus-group-group-name))))
6356       (gnus-group-next-unread-group 1))
6357     (setq group-point (point))
6358     (if temporary
6359         nil                             ;Nothing to do.
6360       ;; If we have several article buffers, we kill them at exit.
6361       (unless gnus-single-article-buffer
6362         (gnus-kill-buffer gnus-article-buffer)
6363         (gnus-kill-buffer gnus-original-article-buffer)
6364         (setq gnus-article-current nil))
6365       (set-buffer buf)
6366       (if (not gnus-kill-summary-on-exit)
6367           (progn
6368             (gnus-deaden-summary)
6369             (setq mode nil))
6370         ;; We set all buffer-local variables to nil.  It is unclear why
6371         ;; this is needed, but if we don't, buffer-local variables are
6372         ;; not garbage-collected, it seems.  This would the lead to en
6373         ;; ever-growing Emacs.
6374         (gnus-summary-clear-local-variables)
6375         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6376           (gnus-summary-clear-local-variables))
6377         (when (get-buffer gnus-article-buffer)
6378           (bury-buffer gnus-article-buffer))
6379         ;; We clear the global counterparts of the buffer-local
6380         ;; variables as well, just to be on the safe side.
6381         (set-buffer gnus-group-buffer)
6382         (gnus-summary-clear-local-variables)
6383         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6384           (gnus-summary-clear-local-variables)))
6385       (setq gnus-current-select-method gnus-select-method)
6386       (pop-to-buffer gnus-group-buffer)
6387       (if (not quit-config)
6388           (progn
6389             (goto-char group-point)
6390             (gnus-configure-windows 'group 'force))
6391         (gnus-handle-ephemeral-exit quit-config))
6392       ;; Return to group mode buffer.
6393       (when (eq mode 'gnus-summary-mode)
6394         (gnus-kill-buffer buf))
6395       ;; Clear the current group name.
6396       (unless quit-config
6397         (setq gnus-newsgroup-name nil)))))
6398
6399 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6400 (defun gnus-summary-exit-no-update (&optional no-questions)
6401   "Quit reading current newsgroup without updating read article info."
6402   (interactive)
6403   (let* ((group gnus-newsgroup-name)
6404          (gnus-group-is-exiting-p t)
6405          (quit-config (gnus-group-quit-config group)))
6406     (when (or no-questions
6407               gnus-expert-user
6408               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6409       (gnus-async-halt-prefetch)
6410       (mapcar 'funcall
6411               (delq 'gnus-summary-expire-articles
6412                     (copy-sequence gnus-summary-prepare-exit-hook)))
6413       (when (gnus-buffer-live-p gnus-article-buffer)
6414         (save-excursion
6415           (set-buffer gnus-article-buffer)
6416           (mm-destroy-parts gnus-article-mime-handles)
6417           ;; Set it to nil for safety reason.
6418           (setq gnus-article-mime-handle-alist nil)
6419           (setq gnus-article-mime-handles nil)))
6420       ;; If we have several article buffers, we kill them at exit.
6421       (unless gnus-single-article-buffer
6422         (gnus-kill-buffer gnus-article-buffer)
6423         (gnus-kill-buffer gnus-original-article-buffer)
6424         (setq gnus-article-current nil))
6425       (if (not gnus-kill-summary-on-exit)
6426           (gnus-deaden-summary)
6427         (gnus-close-group group)
6428         (gnus-summary-clear-local-variables)
6429         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6430           (gnus-summary-clear-local-variables))
6431         (set-buffer gnus-group-buffer)
6432         (gnus-summary-clear-local-variables)
6433         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6434           (gnus-summary-clear-local-variables))
6435         (when (get-buffer gnus-summary-buffer)
6436           (kill-buffer gnus-summary-buffer)))
6437       (unless gnus-single-article-buffer
6438         (setq gnus-article-current nil))
6439       (when gnus-use-trees
6440         (gnus-tree-close group))
6441       (gnus-async-prefetch-remove-group group)
6442       (when (get-buffer gnus-article-buffer)
6443         (bury-buffer gnus-article-buffer))
6444       ;; Return to the group buffer.
6445       (gnus-configure-windows 'group 'force)
6446       ;; Clear the current group name.
6447       (setq gnus-newsgroup-name nil)
6448       (unless (gnus-ephemeral-group-p group)
6449         (gnus-group-update-group group))
6450       (when (equal (gnus-group-group-name) group)
6451         (gnus-group-next-unread-group 1))
6452       (when quit-config
6453         (gnus-handle-ephemeral-exit quit-config)))))
6454
6455 (defun gnus-handle-ephemeral-exit (quit-config)
6456   "Handle movement when leaving an ephemeral group.
6457 The state which existed when entering the ephemeral is reset."
6458   (if (not (buffer-name (car quit-config)))
6459       (gnus-configure-windows 'group 'force)
6460     (set-buffer (car quit-config))
6461     (cond ((eq major-mode 'gnus-summary-mode)
6462            (gnus-set-global-variables))
6463           ((eq major-mode 'gnus-article-mode)
6464            (save-excursion
6465              ;; The `gnus-summary-buffer' variable may point
6466              ;; to the old summary buffer when using a single
6467              ;; article buffer.
6468              (unless (gnus-buffer-live-p gnus-summary-buffer)
6469                (set-buffer gnus-group-buffer))
6470              (set-buffer gnus-summary-buffer)
6471              (gnus-set-global-variables))))
6472     (if (or (eq (cdr quit-config) 'article)
6473             (eq (cdr quit-config) 'pick))
6474         (progn
6475           ;; The current article may be from the ephemeral group
6476           ;; thus it is best that we reload this article
6477           (gnus-summary-show-article)
6478           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6479               (gnus-configure-windows 'pick 'force)
6480             (gnus-configure-windows (cdr quit-config) 'force)))
6481       (gnus-configure-windows (cdr quit-config) 'force))
6482     (when (eq major-mode 'gnus-summary-mode)
6483       (gnus-summary-next-subject 1 nil t)
6484       (gnus-summary-recenter)
6485       (gnus-summary-position-point))))
6486
6487 ;;; Dead summaries.
6488
6489 (defvar gnus-dead-summary-mode-map nil)
6490
6491 (unless gnus-dead-summary-mode-map
6492   (setq gnus-dead-summary-mode-map (make-keymap))
6493   (suppress-keymap gnus-dead-summary-mode-map)
6494   (substitute-key-definition
6495    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6496   (dolist (key '("\C-d" "\r" "\177" [delete]))
6497     (define-key gnus-dead-summary-mode-map
6498       key 'gnus-summary-wake-up-the-dead))
6499   (dolist (key '("q" "Q"))
6500     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6501
6502 (defvar gnus-dead-summary-mode nil
6503   "Minor mode for Gnus summary buffers.")
6504
6505 (defun gnus-dead-summary-mode (&optional arg)
6506   "Minor mode for Gnus summary buffers."
6507   (interactive "P")
6508   (when (eq major-mode 'gnus-summary-mode)
6509     (make-local-variable 'gnus-dead-summary-mode)
6510     (setq gnus-dead-summary-mode
6511           (if (null arg) (not gnus-dead-summary-mode)
6512             (> (prefix-numeric-value arg) 0)))
6513     (when gnus-dead-summary-mode
6514       (gnus-add-minor-mode
6515        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6516
6517 (defun gnus-deaden-summary ()
6518   "Make the current summary buffer into a dead summary buffer."
6519   ;; Kill any previous dead summary buffer.
6520   (when (and gnus-dead-summary
6521              (buffer-name gnus-dead-summary))
6522     (save-excursion
6523       (set-buffer gnus-dead-summary)
6524       (when gnus-dead-summary-mode
6525         (kill-buffer (current-buffer)))))
6526   ;; Make this the current dead summary.
6527   (setq gnus-dead-summary (current-buffer))
6528   (gnus-dead-summary-mode 1)
6529   (let ((name (buffer-name)))
6530     (when (string-match "Summary" name)
6531       (rename-buffer
6532        (concat (substring name 0 (match-beginning 0)) "Dead "
6533                (substring name (match-beginning 0)))
6534        t)
6535       (bury-buffer))))
6536
6537 (defun gnus-kill-or-deaden-summary (buffer)
6538   "Kill or deaden the summary BUFFER."
6539   (save-excursion
6540     (when (and (buffer-name buffer)
6541                (not gnus-single-article-buffer))
6542       (save-excursion
6543         (set-buffer buffer)
6544         (gnus-kill-buffer gnus-article-buffer)
6545         (gnus-kill-buffer gnus-original-article-buffer)))
6546     (cond
6547      ;; Kill the buffer.
6548      (gnus-kill-summary-on-exit
6549       (when (and gnus-use-trees
6550                  (gnus-buffer-exists-p buffer))
6551         (save-excursion
6552           (set-buffer buffer)
6553           (gnus-tree-close gnus-newsgroup-name)))
6554       (gnus-kill-buffer buffer))
6555      ;; Deaden the buffer.
6556      ((gnus-buffer-exists-p buffer)
6557       (save-excursion
6558         (set-buffer buffer)
6559         (gnus-deaden-summary))))))
6560
6561 (defun gnus-summary-wake-up-the-dead (&rest args)
6562   "Wake up the dead summary buffer."
6563   (interactive)
6564   (gnus-dead-summary-mode -1)
6565   (let ((name (buffer-name)))
6566     (when (string-match "Dead " name)
6567       (rename-buffer
6568        (concat (substring name 0 (match-beginning 0))
6569                (substring name (match-end 0)))
6570        t)))
6571   (gnus-message 3 "This dead summary is now alive again"))
6572
6573 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6574 (defun gnus-summary-fetch-faq (&optional faq-dir)
6575   "Fetch the FAQ for the current group.
6576 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6577 in."
6578   (interactive
6579    (list
6580     (when current-prefix-arg
6581       (completing-read
6582        "FAQ dir: " (and (listp gnus-group-faq-directory)
6583                         (mapcar (lambda (file) (list file))
6584                                 gnus-group-faq-directory))))))
6585   (let (gnus-faq-buffer)
6586     (when (setq gnus-faq-buffer
6587                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6588       (gnus-configure-windows 'summary-faq))))
6589
6590 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6591 (defun gnus-summary-describe-group (&optional force)
6592   "Describe the current newsgroup."
6593   (interactive "P")
6594   (gnus-group-describe-group force gnus-newsgroup-name))
6595
6596 (defun gnus-summary-describe-briefly ()
6597   "Describe summary mode commands briefly."
6598   (interactive)
6599   (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")))
6600
6601 ;; Walking around group mode buffer from summary mode.
6602
6603 (defun gnus-summary-next-group (&optional no-article target-group backward)
6604   "Exit current newsgroup and then select next unread newsgroup.
6605 If prefix argument NO-ARTICLE is non-nil, no article is selected
6606 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6607 previous group instead."
6608   (interactive "P")
6609   ;; Stop pre-fetching.
6610   (gnus-async-halt-prefetch)
6611   (let ((current-group gnus-newsgroup-name)
6612         (current-buffer (current-buffer))
6613         entered)
6614     (while (not entered)
6615       ;; Then we find what group we are supposed to enter.
6616       (set-buffer gnus-group-buffer)
6617       (gnus-group-jump-to-group current-group)
6618       (setq target-group
6619             (or target-group
6620                 (if (eq gnus-keep-same-level 'best)
6621                     (gnus-summary-best-group gnus-newsgroup-name)
6622                   (gnus-summary-search-group backward gnus-keep-same-level))))
6623       (if (not target-group)
6624           ;; There are no further groups, so we return to the group
6625           ;; buffer.
6626           (progn
6627             (gnus-message 5 "Returning to the group buffer")
6628             (setq entered t)
6629             (when (gnus-buffer-live-p current-buffer)
6630               (set-buffer current-buffer)
6631               (gnus-summary-exit))
6632             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6633         ;; We try to enter the target group.
6634         (gnus-group-jump-to-group target-group)
6635         (let ((unreads (gnus-group-group-unread)))
6636           (if (and (or (eq t unreads)
6637                        (and unreads (not (zerop unreads))))
6638                    (progn
6639                      ;; Now we semi-exit this group to update Xrefs
6640                      ;; and all variables.  We can't do a real exit,
6641                      ;; because the window conf must remain the same
6642                      ;; in case the user is prompted for info, and we
6643                      ;; don't want the window conf to change before
6644                      ;; that...
6645                      (when (gnus-buffer-live-p current-buffer)
6646                        (set-buffer current-buffer)
6647                        (gnus-summary-exit t))
6648                      (gnus-summary-read-group
6649                       target-group nil no-article
6650                       (and (buffer-name current-buffer) current-buffer)
6651                       nil backward)))
6652               (setq entered t)
6653             (setq current-group target-group
6654                   target-group nil)))))))
6655
6656 (defun gnus-summary-prev-group (&optional no-article)
6657   "Exit current newsgroup and then select previous unread newsgroup.
6658 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6659   (interactive "P")
6660   (gnus-summary-next-group no-article nil t))
6661
6662 ;; Walking around summary lines.
6663
6664 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6665   "Go to the first unread subject.
6666 If UNREAD is non-nil, go to the first unread article.
6667 Returns the article selected or nil if there are no unread articles."
6668   (interactive "P")
6669   (prog1
6670       (cond
6671        ;; Empty summary.
6672        ((null gnus-newsgroup-data)
6673         (gnus-message 3 "No articles in the group")
6674         nil)
6675        ;; Pick the first article.
6676        ((not unread)
6677         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6678         (gnus-data-number (car gnus-newsgroup-data)))
6679        ;; No unread articles.
6680        ((null gnus-newsgroup-unreads)
6681         (gnus-message 3 "No more unread articles")
6682         nil)
6683        ;; Find the first unread article.
6684        (t
6685         (let ((data gnus-newsgroup-data))
6686           (while (and data
6687                       (and (not (and undownloaded
6688                                      (memq (car data)
6689                                            gnus-newsgroup-undownloaded)))
6690                            (if unseen
6691                                (or (not (memq
6692                                          (gnus-data-number (car data))
6693                                          gnus-newsgroup-unseen))
6694                                    (not (gnus-data-unread-p (car data))))
6695                              (not (gnus-data-unread-p (car data))))))
6696             (setq data (cdr data)))
6697           (when data
6698             (goto-char (gnus-data-pos (car data)))
6699             (gnus-data-number (car data))))))
6700     (gnus-summary-position-point)))
6701
6702 (defun gnus-summary-next-subject (n &optional unread dont-display)
6703   "Go to next N'th summary line.
6704 If N is negative, go to the previous N'th subject line.
6705 If UNREAD is non-nil, only unread articles are selected.
6706 The difference between N and the actual number of steps taken is
6707 returned."
6708   (interactive "p")
6709   (let ((backward (< n 0))
6710         (n (abs n)))
6711     (while (and (> n 0)
6712                 (if backward
6713                     (gnus-summary-find-prev unread)
6714                   (gnus-summary-find-next unread)))
6715       (unless (zerop (setq n (1- n)))
6716         (gnus-summary-show-thread)))
6717     (when (/= 0 n)
6718       (gnus-message 7 "No more%s articles"
6719                     (if unread " unread" "")))
6720     (unless dont-display
6721       (gnus-summary-recenter)
6722       (gnus-summary-position-point))
6723     n))
6724
6725 (defun gnus-summary-next-unread-subject (n)
6726   "Go to next N'th unread summary line."
6727   (interactive "p")
6728   (gnus-summary-next-subject n t))
6729
6730 (defun gnus-summary-prev-subject (n &optional unread)
6731   "Go to previous N'th summary line.
6732 If optional argument UNREAD is non-nil, only unread article is selected."
6733   (interactive "p")
6734   (gnus-summary-next-subject (- n) unread))
6735
6736 (defun gnus-summary-prev-unread-subject (n)
6737   "Go to previous N'th unread summary line."
6738   (interactive "p")
6739   (gnus-summary-next-subject (- n) t))
6740
6741 (defun gnus-summary-goto-subjects (articles)
6742   "Insert the subject header for ARTICLES in the current buffer."
6743   (save-excursion
6744     (dolist (article articles)
6745       (gnus-summary-goto-subject article t)))
6746   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6747   (gnus-summary-position-point))
6748   
6749 (defun gnus-summary-goto-subject (article &optional force silent)
6750   "Go the subject line of ARTICLE.
6751 If FORCE, also allow jumping to articles not currently shown."
6752   (interactive "nArticle number: ")
6753   (unless (numberp article)
6754     (error "Article %s is not a number" article))
6755   (let ((b (point))
6756         (data (gnus-data-find article)))
6757     ;; We read in the article if we have to.
6758     (and (not data)
6759          force
6760          (gnus-summary-insert-subject
6761           article
6762           (if (or (numberp force) (vectorp force)) force)
6763           t)
6764          (setq data (gnus-data-find article)))
6765     (goto-char b)
6766     (if (not data)
6767         (progn
6768           (unless silent
6769             (gnus-message 3 "Can't find article %d" article))
6770           nil)
6771       (let ((pt (gnus-data-pos data)))
6772         (goto-char pt)
6773         (gnus-summary-set-article-display-arrow pt))
6774       (gnus-summary-position-point)
6775       article)))
6776
6777 ;; Walking around summary lines with displaying articles.
6778
6779 (defun gnus-summary-expand-window (&optional arg)
6780   "Make the summary buffer take up the entire Emacs frame.
6781 Given a prefix, will force an `article' buffer configuration."
6782   (interactive "P")
6783   (if arg
6784       (gnus-configure-windows 'article 'force)
6785     (gnus-configure-windows 'summary 'force)))
6786
6787 (defun gnus-summary-display-article (article &optional all-header)
6788   "Display ARTICLE in article buffer."
6789   (when (gnus-buffer-live-p gnus-article-buffer)
6790     (with-current-buffer gnus-article-buffer
6791       (mm-enable-multibyte)))
6792   (gnus-set-global-variables)
6793   (when (gnus-buffer-live-p gnus-article-buffer)
6794     (with-current-buffer gnus-article-buffer
6795       (setq gnus-article-charset gnus-newsgroup-charset)
6796       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6797       (mm-enable-multibyte)))
6798   (if (null article)
6799       nil
6800     (prog1
6801         (if gnus-summary-display-article-function
6802             (funcall gnus-summary-display-article-function article all-header)
6803           (gnus-article-prepare article all-header))
6804       (gnus-run-hooks 'gnus-select-article-hook)
6805       (when (and gnus-current-article
6806                  (not (zerop gnus-current-article)))
6807         (gnus-summary-goto-subject gnus-current-article))
6808       (gnus-summary-recenter)
6809       (when (and gnus-use-trees gnus-show-threads)
6810         (gnus-possibly-generate-tree article)
6811         (gnus-highlight-selected-tree article))
6812       ;; Successfully display article.
6813       (gnus-article-set-window-start
6814        (cdr (assq article gnus-newsgroup-bookmarks))))))
6815
6816 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6817   "Select the current article.
6818 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6819 non-nil, the article will be re-fetched even if it already present in
6820 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6821 be displayed."
6822   ;; Make sure we are in the summary buffer to work around bbdb bug.
6823   (unless (eq major-mode 'gnus-summary-mode)
6824     (set-buffer gnus-summary-buffer))
6825   (let ((article (or article (gnus-summary-article-number)))
6826         (all-headers (not (not all-headers))) ;Must be t or nil.
6827         gnus-summary-display-article-function)
6828     (and (not pseudo)
6829          (gnus-summary-article-pseudo-p article)
6830          (error "This is a pseudo-article"))
6831     (save-excursion
6832       (set-buffer gnus-summary-buffer)
6833       (if (or (and gnus-single-article-buffer
6834                    (or (null gnus-current-article)
6835                        (null gnus-article-current)
6836                        (null (get-buffer gnus-article-buffer))
6837                        (not (eq article (cdr gnus-article-current)))
6838                        (not (equal (car gnus-article-current)
6839                                    gnus-newsgroup-name))))
6840               (and (not gnus-single-article-buffer)
6841                    (or (null gnus-current-article)
6842                        (not (eq gnus-current-article article))))
6843               force)
6844           ;; The requested article is different from the current article.
6845           (progn
6846             (gnus-summary-display-article article all-headers)
6847             (when (gnus-buffer-live-p gnus-article-buffer)
6848               (with-current-buffer gnus-article-buffer
6849                 (if (not gnus-article-decoded-p) ;; a local variable
6850                     (mm-disable-multibyte))))
6851             (gnus-article-set-window-start
6852              (cdr (assq article gnus-newsgroup-bookmarks)))
6853             article)
6854         'old))))
6855
6856 (defun gnus-summary-force-verify-and-decrypt ()
6857   (interactive)
6858   (let ((mm-verify-option 'known)
6859         (mm-decrypt-option 'known)
6860         (gnus-buttonized-mime-types (append (list "multipart/signed"
6861                                                   "multipart/encrypted")
6862                                             gnus-buttonized-mime-types)))
6863     (gnus-summary-select-article nil 'force)))
6864
6865 (defun gnus-summary-set-current-mark (&optional current-mark)
6866   "Obsolete function."
6867   nil)
6868
6869 (defun gnus-summary-next-article (&optional unread subject backward push)
6870   "Select the next article.
6871 If UNREAD, only unread articles are selected.
6872 If SUBJECT, only articles with SUBJECT are selected.
6873 If BACKWARD, the previous article is selected instead of the next."
6874   (interactive "P")
6875   (cond
6876    ;; Is there such an article?
6877    ((and (gnus-summary-search-forward unread subject backward)
6878          (or (gnus-summary-display-article (gnus-summary-article-number))
6879              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6880     (gnus-summary-position-point))
6881    ;; If not, we try the first unread, if that is wanted.
6882    ((and subject
6883          gnus-auto-select-same
6884          (gnus-summary-first-unread-article))
6885     (gnus-summary-position-point)
6886     (gnus-message 6 "Wrapped"))
6887    ;; Try to get next/previous article not displayed in this group.
6888    ((and gnus-auto-extend-newsgroup
6889          (not unread) (not subject))
6890     (gnus-summary-goto-article
6891      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6892      nil (count-lines (point-min) (point))))
6893    ;; Go to next/previous group.
6894    (t
6895     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6896       (gnus-summary-jump-to-group gnus-newsgroup-name))
6897     (let ((cmd last-command-char)
6898           (point
6899            (save-excursion
6900              (set-buffer gnus-group-buffer)
6901              (point)))
6902           (group
6903            (if (eq gnus-keep-same-level 'best)
6904                (gnus-summary-best-group gnus-newsgroup-name)
6905              (gnus-summary-search-group backward gnus-keep-same-level))))
6906       ;; For some reason, the group window gets selected.  We change
6907       ;; it back.
6908       (select-window (get-buffer-window (current-buffer)))
6909       ;; Select next unread newsgroup automagically.
6910       (cond
6911        ((or (not gnus-auto-select-next)
6912             (not cmd))
6913         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6914        ((or (eq gnus-auto-select-next 'quietly)
6915             (and (eq gnus-auto-select-next 'slightly-quietly)
6916                  push)
6917             (and (eq gnus-auto-select-next 'almost-quietly)
6918                  (gnus-summary-last-article-p)))
6919         ;; Select quietly.
6920         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6921             (gnus-summary-exit)
6922           (gnus-message 7 "No more%s articles (%s)..."
6923                         (if unread " unread" "")
6924                         (if group (concat "selecting " group)
6925                           "exiting"))
6926           (gnus-summary-next-group nil group backward)))
6927        (t
6928         (when (gnus-key-press-event-p last-input-event)
6929           (gnus-summary-walk-group-buffer
6930            gnus-newsgroup-name cmd unread backward point))))))))
6931
6932 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6933   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6934                       (?\C-p (gnus-group-prev-unread-group 1))))
6935         (cursor-in-echo-area t)
6936         keve key group ended prompt)
6937     (save-excursion
6938       (set-buffer gnus-group-buffer)
6939       (goto-char start)
6940       (setq group
6941             (if (eq gnus-keep-same-level 'best)
6942                 (gnus-summary-best-group gnus-newsgroup-name)
6943               (gnus-summary-search-group backward gnus-keep-same-level))))
6944     (while (not ended)
6945       (setq prompt
6946             (format
6947              "No more%s articles%s " (if unread " unread" "")
6948              (if (and group
6949                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6950                  (format " (Type %s for %s [%s])"
6951                          (single-key-description cmd) group
6952                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6953                (format " (Type %s to exit %s)"
6954                        (single-key-description cmd)
6955                        gnus-newsgroup-name))))
6956       ;; Confirm auto selection.
6957       (setq key (car (setq keve (gnus-read-event-char prompt)))
6958             ended t)
6959       (cond
6960        ((assq key keystrokes)
6961         (let ((obuf (current-buffer)))
6962           (switch-to-buffer gnus-group-buffer)
6963           (when group
6964             (gnus-group-jump-to-group group))
6965           (eval (cadr (assq key keystrokes)))
6966           (setq group (gnus-group-group-name))
6967           (switch-to-buffer obuf))
6968         (setq ended nil))
6969        ((equal key cmd)
6970         (if (or (not group)
6971                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6972             (gnus-summary-exit)
6973           (gnus-summary-next-group nil group backward)))
6974        (t
6975         (push (cdr keve) unread-command-events))))))
6976
6977 (defun gnus-summary-next-unread-article ()
6978   "Select unread article after current one."
6979   (interactive)
6980   (gnus-summary-next-article
6981    (or (not (eq gnus-summary-goto-unread 'never))
6982        (gnus-summary-last-article-p (gnus-summary-article-number)))
6983    (and gnus-auto-select-same
6984         (gnus-summary-article-subject))))
6985
6986 (defun gnus-summary-prev-article (&optional unread subject)
6987   "Select the article after the current one.
6988 If UNREAD is non-nil, only unread articles are selected."
6989   (interactive "P")
6990   (gnus-summary-next-article unread subject t))
6991
6992 (defun gnus-summary-prev-unread-article ()
6993   "Select unread article before current one."
6994   (interactive)
6995   (gnus-summary-prev-article
6996    (or (not (eq gnus-summary-goto-unread 'never))
6997        (gnus-summary-first-article-p (gnus-summary-article-number)))
6998    (and gnus-auto-select-same
6999         (gnus-summary-article-subject))))
7000
7001 (defun gnus-summary-next-page (&optional lines circular stop)
7002   "Show next page of the selected article.
7003 If at the end of the current article, select the next article.
7004 LINES says how many lines should be scrolled up.
7005
7006 If CIRCULAR is non-nil, go to the start of the article instead of
7007 selecting the next article when reaching the end of the current
7008 article.
7009
7010 If STOP is non-nil, just stop when reaching the end of the message."
7011   (interactive "P")
7012   (setq gnus-summary-buffer (current-buffer))
7013   (gnus-set-global-variables)
7014   (let ((article (gnus-summary-article-number))
7015         (article-window (get-buffer-window gnus-article-buffer t))
7016         endp)
7017     ;; If the buffer is empty, we have no article.
7018     (unless article
7019       (error "No article to select"))
7020     (gnus-configure-windows 'article)
7021     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7022         (if (and (eq gnus-summary-goto-unread 'never)
7023                  (not (gnus-summary-last-article-p article)))
7024             (gnus-summary-next-article)
7025           (gnus-summary-next-unread-article))
7026       (if (or (null gnus-current-article)
7027               (null gnus-article-current)
7028               (/= article (cdr gnus-article-current))
7029               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7030           ;; Selected subject is different from current article's.
7031           (gnus-summary-display-article article)
7032         (when article-window
7033           (gnus-eval-in-buffer-window gnus-article-buffer
7034             (setq endp (gnus-article-next-page lines)))
7035           (when endp
7036             (cond (stop
7037                    (gnus-message 3 "End of message"))
7038                   (circular
7039                    (gnus-summary-beginning-of-article))
7040                   (lines
7041                    (gnus-message 3 "End of message"))
7042                   ((null lines)
7043                    (if (and (eq gnus-summary-goto-unread 'never)
7044                             (not (gnus-summary-last-article-p article)))
7045                        (gnus-summary-next-article)
7046                      (gnus-summary-next-unread-article))))))))
7047     (gnus-summary-recenter)
7048     (gnus-summary-position-point)))
7049
7050 (defun gnus-summary-prev-page (&optional lines move)
7051   "Show previous page of selected article.
7052 Argument LINES specifies lines to be scrolled down.
7053 If MOVE, move to the previous unread article if point is at
7054 the beginning of the buffer."
7055   (interactive "P")
7056   (let ((article (gnus-summary-article-number))
7057         (article-window (get-buffer-window gnus-article-buffer t))
7058         endp)
7059     (gnus-configure-windows 'article)
7060     (if (or (null gnus-current-article)
7061             (null gnus-article-current)
7062             (/= article (cdr gnus-article-current))
7063             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7064         ;; Selected subject is different from current article's.
7065         (gnus-summary-display-article article)
7066       (gnus-summary-recenter)
7067       (when article-window
7068         (gnus-eval-in-buffer-window gnus-article-buffer
7069           (setq endp (gnus-article-prev-page lines)))
7070         (when (and move endp)
7071           (cond (lines
7072                  (gnus-message 3 "Beginning of message"))
7073                 ((null lines)
7074                  (if (and (eq gnus-summary-goto-unread 'never)
7075                           (not (gnus-summary-first-article-p article)))
7076                      (gnus-summary-prev-article)
7077                    (gnus-summary-prev-unread-article))))))))
7078   (gnus-summary-position-point))
7079
7080 (defun gnus-summary-prev-page-or-article (&optional lines)
7081   "Show previous page of selected article.
7082 Argument LINES specifies lines to be scrolled down.
7083 If at the beginning of the article, go to the next article."
7084   (interactive "P")
7085   (gnus-summary-prev-page lines t))
7086
7087 (defun gnus-summary-scroll-up (lines)
7088   "Scroll up (or down) one line current article.
7089 Argument LINES specifies lines to be scrolled up (or down if negative)."
7090   (interactive "p")
7091   (gnus-configure-windows 'article)
7092   (gnus-summary-show-thread)
7093   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7094     (gnus-eval-in-buffer-window gnus-article-buffer
7095       (cond ((> lines 0)
7096              (when (gnus-article-next-page lines)
7097                (gnus-message 3 "End of message")))
7098             ((< lines 0)
7099              (gnus-article-prev-page (- lines))))))
7100   (gnus-summary-recenter)
7101   (gnus-summary-position-point))
7102
7103 (defun gnus-summary-scroll-down (lines)
7104   "Scroll down (or up) one line current article.
7105 Argument LINES specifies lines to be scrolled down (or up if negative)."
7106   (interactive "p")
7107   (gnus-summary-scroll-up (- lines)))
7108
7109 (defun gnus-summary-next-same-subject ()
7110   "Select next article which has the same subject as current one."
7111   (interactive)
7112   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7113
7114 (defun gnus-summary-prev-same-subject ()
7115   "Select previous article which has the same subject as current one."
7116   (interactive)
7117   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7118
7119 (defun gnus-summary-next-unread-same-subject ()
7120   "Select next unread article which has the same subject as current one."
7121   (interactive)
7122   (gnus-summary-next-article t (gnus-summary-article-subject)))
7123
7124 (defun gnus-summary-prev-unread-same-subject ()
7125   "Select previous unread article which has the same subject as current one."
7126   (interactive)
7127   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7128
7129 (defun gnus-summary-first-unread-article ()
7130   "Select the first unread article.
7131 Return nil if there are no unread articles."
7132   (interactive)
7133   (prog1
7134       (when (gnus-summary-first-subject t)
7135         (gnus-summary-show-thread)
7136         (gnus-summary-first-subject t)
7137         (gnus-summary-display-article (gnus-summary-article-number)))
7138     (gnus-summary-position-point)))
7139
7140 (defun gnus-summary-first-unread-subject ()
7141   "Place the point on the subject line of the first unread article.
7142 Return nil if there are no unread articles."
7143   (interactive)
7144   (prog1
7145       (when (gnus-summary-first-subject t)
7146         (gnus-summary-show-thread)
7147         (gnus-summary-first-subject t))
7148     (gnus-summary-position-point)))
7149
7150 (defun gnus-summary-first-unseen-subject ()
7151   "Place the point on the subject line of the first unseen article.
7152 Return nil if there are no unseen articles."
7153   (interactive)
7154   (prog1
7155       (when (gnus-summary-first-subject t t t)
7156         (gnus-summary-show-thread)
7157         (gnus-summary-first-subject t t t))
7158     (gnus-summary-position-point)))
7159
7160 (defun gnus-summary-first-unseen-or-unread-subject ()
7161   "Place the point on the subject line of the first unseen article.
7162 Return nil if there are no unseen articles."
7163   (interactive)
7164   (prog1
7165       (unless (when (gnus-summary-first-subject t t t)
7166                 (gnus-summary-show-thread)
7167                 (gnus-summary-first-subject t t t))
7168         (when (gnus-summary-first-subject t)
7169           (gnus-summary-show-thread)
7170           (gnus-summary-first-subject t)))
7171     (gnus-summary-position-point)))
7172
7173 (defun gnus-summary-first-article ()
7174   "Select the first article.
7175 Return nil if there are no articles."
7176   (interactive)
7177   (prog1
7178       (when (gnus-summary-first-subject)
7179         (gnus-summary-show-thread)
7180         (gnus-summary-first-subject)
7181         (gnus-summary-display-article (gnus-summary-article-number)))
7182     (gnus-summary-position-point)))
7183
7184 (defun gnus-summary-best-unread-article (&optional arg)
7185   "Select the unread article with the highest score.
7186 If given a prefix argument, select the next unread article that has a
7187 score higher than the default score."
7188   (interactive "P")
7189   (let ((article (if arg
7190                      (gnus-summary-better-unread-subject)
7191                    (gnus-summary-best-unread-subject))))
7192     (if article
7193         (gnus-summary-goto-article article)
7194       (error "No unread articles"))))
7195
7196 (defun gnus-summary-best-unread-subject ()
7197   "Select the unread subject with the highest score."
7198   (interactive)
7199   (let ((best -1000000)
7200         (data gnus-newsgroup-data)
7201         article score)
7202     (while data
7203       (and (gnus-data-unread-p (car data))
7204            (> (setq score
7205                     (gnus-summary-article-score (gnus-data-number (car data))))
7206               best)
7207            (setq best score
7208                  article (gnus-data-number (car data))))
7209       (setq data (cdr data)))
7210     (when article
7211       (gnus-summary-goto-subject article))
7212     (gnus-summary-position-point)
7213     article))
7214
7215 (defun gnus-summary-better-unread-subject ()
7216   "Select the first unread subject that has a score over the default score."
7217   (interactive)
7218   (let ((data gnus-newsgroup-data)
7219         article score)
7220     (while (and (setq article (gnus-data-number (car data)))
7221                 (or (gnus-data-read-p (car data))
7222                     (not (> (gnus-summary-article-score article)
7223                             gnus-summary-default-score))))
7224       (setq data (cdr data)))
7225     (when article
7226       (gnus-summary-goto-subject article))
7227     (gnus-summary-position-point)
7228     article))
7229
7230 (defun gnus-summary-last-subject ()
7231   "Go to the last displayed subject line in the group."
7232   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7233     (when article
7234       (gnus-summary-goto-subject article))))
7235
7236 (defun gnus-summary-goto-article (article &optional all-headers force)
7237   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7238 If ALL-HEADERS is non-nil, no header lines are hidden.
7239 If FORCE, go to the article even if it isn't displayed.  If FORCE
7240 is a number, it is the line the article is to be displayed on."
7241   (interactive
7242    (list
7243     (completing-read
7244      "Article number or Message-ID: "
7245      (mapcar (lambda (number) (list (int-to-string number)))
7246              gnus-newsgroup-limit))
7247     current-prefix-arg
7248     t))
7249   (prog1
7250       (if (and (stringp article)
7251                (string-match "@" article))
7252           (gnus-summary-refer-article article)
7253         (when (stringp article)
7254           (setq article (string-to-number article)))
7255         (if (gnus-summary-goto-subject article force)
7256             (gnus-summary-display-article article all-headers)
7257           (gnus-message 4 "Couldn't go to article %s" article) nil))
7258     (gnus-summary-position-point)))
7259
7260 (defun gnus-summary-goto-last-article ()
7261   "Go to the previously read article."
7262   (interactive)
7263   (prog1
7264       (when gnus-last-article
7265         (gnus-summary-goto-article gnus-last-article nil t))
7266     (gnus-summary-position-point)))
7267
7268 (defun gnus-summary-pop-article (number)
7269   "Pop one article off the history and go to the previous.
7270 NUMBER articles will be popped off."
7271   (interactive "p")
7272   (let (to)
7273     (setq gnus-newsgroup-history
7274           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7275     (if to
7276         (gnus-summary-goto-article (car to) nil t)
7277       (error "Article history empty")))
7278   (gnus-summary-position-point))
7279
7280 ;; Summary commands and functions for limiting the summary buffer.
7281
7282 (defun gnus-summary-limit-to-articles (n)
7283   "Limit the summary buffer to the next N articles.
7284 If not given a prefix, use the process marked articles instead."
7285   (interactive "P")
7286   (prog1
7287       (let ((articles (gnus-summary-work-articles n)))
7288         (setq gnus-newsgroup-processable nil)
7289         (gnus-summary-limit articles))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-pop-limit (&optional total)
7293   "Restore the previous limit.
7294 If given a prefix, remove all limits."
7295   (interactive "P")
7296   (when total
7297     (setq gnus-newsgroup-limits
7298           (list (mapcar (lambda (h) (mail-header-number h))
7299                         gnus-newsgroup-headers))))
7300   (unless gnus-newsgroup-limits
7301     (error "No limit to pop"))
7302   (prog1
7303       (gnus-summary-limit nil 'pop)
7304     (gnus-summary-position-point)))
7305
7306 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7307   "Limit the summary buffer to articles that have subjects that match a regexp.
7308 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7309   (interactive
7310    (list (read-string (if current-prefix-arg
7311                           "Exclude subject (regexp): "
7312                         "Limit to subject (regexp): "))
7313          nil current-prefix-arg))
7314   (unless header
7315     (setq header "subject"))
7316   (when (not (equal "" subject))
7317     (prog1
7318         (let ((articles (gnus-summary-find-matching
7319                          (or header "subject") subject 'all nil nil
7320                          not-matching)))
7321           (unless articles
7322             (error "Found no matches for \"%s\"" subject))
7323           (gnus-summary-limit articles))
7324       (gnus-summary-position-point))))
7325
7326 (defun gnus-summary-limit-to-author (from &optional not-matching)
7327   "Limit the summary buffer to articles that have authors that match a regexp.
7328 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7329   (interactive
7330    (list (read-string (if current-prefix-arg
7331                           "Exclude author (regexp): "
7332                         "Limit to author (regexp): "))
7333          current-prefix-arg))
7334   (gnus-summary-limit-to-subject from "from" not-matching))
7335
7336 (defun gnus-summary-limit-to-age (age &optional younger-p)
7337   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7338 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7339 articles that are younger than AGE days."
7340   (interactive
7341    (let ((younger current-prefix-arg)
7342          (days-got nil)
7343          days)
7344      (while (not days-got)
7345        (setq days (if younger
7346                       (read-string "Limit to articles younger than (in days, older when negative): ")
7347                     (read-string
7348                      "Limit to articles older than (in days, younger when negative): ")))
7349        (when (> (length days) 0)
7350          (setq days (read days)))
7351        (if (numberp days)
7352            (progn
7353              (setq days-got t)
7354              (if (< days 0)
7355                  (progn
7356                    (setq younger (not younger))
7357                    (setq days (* days -1)))))
7358          (message "Please enter a number.")
7359          (sleep-for 1)))
7360      (list days younger)))
7361   (prog1
7362       (let ((data gnus-newsgroup-data)
7363             (cutoff (days-to-time age))
7364             articles d date is-younger)
7365         (while (setq d (pop data))
7366           (when (and (vectorp (gnus-data-header d))
7367                      (setq date (mail-header-date (gnus-data-header d))))
7368             (setq is-younger (time-less-p
7369                               (time-since (condition-case ()
7370                                               (date-to-time date)
7371                                             (error '(0 0))))
7372                               cutoff))
7373             (when (if younger-p
7374                       is-younger
7375                     (not is-younger))
7376               (push (gnus-data-number d) articles))))
7377         (gnus-summary-limit (nreverse articles)))
7378     (gnus-summary-position-point)))
7379
7380 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7381   "Limit the summary buffer to articles that match an 'extra' header."
7382   (interactive
7383    (let ((header
7384           (intern
7385            (gnus-completing-read-with-default
7386             (symbol-name (car gnus-extra-headers))
7387             (if current-prefix-arg
7388                 "Exclude extra header:"
7389               "Limit extra header:")
7390             (mapcar (lambda (x)
7391                       (cons (symbol-name x) x))
7392                     gnus-extra-headers)
7393             nil
7394             t))))
7395      (list header
7396            (read-string (format "%s header %s (regexp): "
7397                                 (if current-prefix-arg "Exclude" "Limit to")
7398                                 header))
7399            current-prefix-arg)))
7400   (when (not (equal "" regexp))
7401     (prog1
7402         (let ((articles (gnus-summary-find-matching
7403                          (cons 'extra header) regexp 'all nil nil
7404                          not-matching)))
7405           (unless articles
7406             (error "Found no matches for \"%s\"" regexp))
7407           (gnus-summary-limit articles))
7408       (gnus-summary-position-point))))
7409
7410 (defun gnus-summary-limit-to-display-predicate ()
7411   "Limit the summary buffer to the predicated in the `display' group parameter."
7412   (interactive)
7413   (unless gnus-newsgroup-display
7414     (error "There is no `display' group parameter"))
7415   (let (articles)
7416     (dolist (number gnus-newsgroup-articles)
7417       (when (funcall gnus-newsgroup-display)
7418         (push number articles)))
7419     (gnus-summary-limit articles))
7420   (gnus-summary-position-point))
7421
7422 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7423 (make-obsolete
7424  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7425
7426 (defun gnus-summary-limit-to-unread (&optional all)
7427   "Limit the summary buffer to articles that are not marked as read.
7428 If ALL is non-nil, limit strictly to unread articles."
7429   (interactive "P")
7430   (if all
7431       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7432     (gnus-summary-limit-to-marks
7433      ;; Concat all the marks that say that an article is read and have
7434      ;; those removed.
7435      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7436            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7437            gnus-low-score-mark gnus-expirable-mark
7438            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7439            gnus-duplicate-mark gnus-souped-mark)
7440      'reverse)))
7441
7442 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7443 (make-obsolete 'gnus-summary-delete-marked-with
7444                'gnus-summary-limit-exclude-marks)
7445
7446 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7447   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7448 If REVERSE, limit the summary buffer to articles that are marked
7449 with MARKS.  MARKS can either be a string of marks or a list of marks.
7450 Returns how many articles were removed."
7451   (interactive "sMarks: ")
7452   (gnus-summary-limit-to-marks marks t))
7453
7454 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7455   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7456 If REVERSE (the prefix), limit the summary buffer to articles that are
7457 not marked with MARKS.  MARKS can either be a string of marks or a
7458 list of marks.
7459 Returns how many articles were removed."
7460   (interactive "sMarks: \nP")
7461   (prog1
7462       (let ((data gnus-newsgroup-data)
7463             (marks (if (listp marks) marks
7464                      (append marks nil))) ; Transform to list.
7465             articles)
7466         (while data
7467           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7468                   (memq (gnus-data-mark (car data)) marks))
7469             (push (gnus-data-number (car data)) articles))
7470           (setq data (cdr data)))
7471         (gnus-summary-limit articles))
7472     (gnus-summary-position-point)))
7473
7474 (defun gnus-summary-limit-to-score (score)
7475   "Limit to articles with score at or above SCORE."
7476   (interactive "NLimit to articles with score of at least: ")
7477   (let ((data gnus-newsgroup-data)
7478         articles)
7479     (while data
7480       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7481                 score)
7482         (push (gnus-data-number (car data)) articles))
7483       (setq data (cdr data)))
7484     (prog1
7485         (gnus-summary-limit articles)
7486       (gnus-summary-position-point))))
7487
7488 (defun gnus-summary-limit-to-unseen ()
7489   "Limit to unseen articles."
7490   (interactive)
7491   (prog1
7492       (gnus-summary-limit gnus-newsgroup-unseen)
7493     (gnus-summary-position-point)))
7494
7495 (defun gnus-summary-limit-include-thread (id)
7496   "Display all the hidden articles that is in the thread with ID in it.
7497 When called interactively, ID is the Message-ID of the current
7498 article."
7499   (interactive (list (mail-header-id (gnus-summary-article-header))))
7500   (let ((articles (gnus-articles-in-thread
7501                    (gnus-id-to-thread (gnus-root-id id)))))
7502     (prog1
7503         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7504       (gnus-summary-limit-include-matching-articles
7505        "subject"
7506        (regexp-quote (gnus-simplify-subject-re
7507                       (mail-header-subject (gnus-id-to-header id)))))
7508       (gnus-summary-position-point))))
7509
7510 (defun gnus-summary-limit-include-matching-articles (header regexp)
7511   "Display all the hidden articles that have HEADERs that match REGEXP."
7512   (interactive (list (read-string "Match on header: ")
7513                      (read-string "Regexp: ")))
7514   (let ((articles (gnus-find-matching-articles header regexp)))
7515     (prog1
7516         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7517       (gnus-summary-position-point))))
7518
7519 (defun gnus-summary-insert-dormant-articles ()
7520   "Insert all the dormat articles for this group into the current buffer."
7521   (interactive)
7522   (let ((gnus-verbose (max 6 gnus-verbose)))
7523     (if (not gnus-newsgroup-dormant)
7524         (gnus-message 3 "No cached articles for this group")
7525       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7526
7527 (defun gnus-summary-limit-include-dormant ()
7528   "Display all the hidden articles that are marked as dormant.
7529 Note that this command only works on a subset of the articles currently
7530 fetched for this group."
7531   (interactive)
7532   (unless gnus-newsgroup-dormant
7533     (error "There are no dormant articles in this group"))
7534   (prog1
7535       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7536     (gnus-summary-position-point)))
7537
7538 (defun gnus-summary-limit-exclude-dormant ()
7539   "Hide all dormant articles."
7540   (interactive)
7541   (prog1
7542       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7543     (gnus-summary-position-point)))
7544
7545 (defun gnus-summary-limit-exclude-childless-dormant ()
7546   "Hide all dormant articles that have no children."
7547   (interactive)
7548   (let ((data (gnus-data-list t))
7549         articles d children)
7550     ;; Find all articles that are either not dormant or have
7551     ;; children.
7552     (while (setq d (pop data))
7553       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7554                 (and (setq children
7555                            (gnus-article-children (gnus-data-number d)))
7556                      (let (found)
7557                        (while children
7558                          (when (memq (car children) articles)
7559                            (setq children nil
7560                                  found t))
7561                          (pop children))
7562                        found)))
7563         (push (gnus-data-number d) articles)))
7564     ;; Do the limiting.
7565     (prog1
7566         (gnus-summary-limit articles)
7567       (gnus-summary-position-point))))
7568
7569 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7570   "Mark all unread excluded articles as read.
7571 If ALL, mark even excluded ticked and dormants as read."
7572   (interactive "P")
7573   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7574   (let ((articles (gnus-sorted-ndifference
7575                    (sort
7576                     (mapcar (lambda (h) (mail-header-number h))
7577                             gnus-newsgroup-headers)
7578                     '<)
7579                    gnus-newsgroup-limit))
7580         article)
7581     (setq gnus-newsgroup-unreads
7582           (gnus-sorted-intersection gnus-newsgroup-unreads
7583                                     gnus-newsgroup-limit))
7584     (if all
7585         (setq gnus-newsgroup-dormant nil
7586               gnus-newsgroup-marked nil
7587               gnus-newsgroup-reads
7588               (nconc
7589                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7590                gnus-newsgroup-reads))
7591       (while (setq article (pop articles))
7592         (unless (or (memq article gnus-newsgroup-dormant)
7593                     (memq article gnus-newsgroup-marked))
7594           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7595
7596 (defun gnus-summary-limit (articles &optional pop)
7597   (if pop
7598       ;; We pop the previous limit off the stack and use that.
7599       (setq articles (car gnus-newsgroup-limits)
7600             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7601     ;; We use the new limit, so we push the old limit on the stack.
7602     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7603   ;; Set the limit.
7604   (setq gnus-newsgroup-limit articles)
7605   (let ((total (length gnus-newsgroup-data))
7606         (data (gnus-data-find-list (gnus-summary-article-number)))
7607         (gnus-summary-mark-below nil)   ; Inhibit this.
7608         found)
7609     ;; This will do all the work of generating the new summary buffer
7610     ;; according to the new limit.
7611     (gnus-summary-prepare)
7612     ;; Hide any threads, possibly.
7613     (gnus-summary-maybe-hide-threads)
7614     ;; Try to return to the article you were at, or one in the
7615     ;; neighborhood.
7616     (when data
7617       ;; We try to find some article after the current one.
7618       (while data
7619         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7620           (setq data nil
7621                 found t))
7622         (setq data (cdr data))))
7623     (unless found
7624       ;; If there is no data, that means that we were after the last
7625       ;; article.  The same goes when we can't find any articles
7626       ;; after the current one.
7627       (goto-char (point-max))
7628       (gnus-summary-find-prev))
7629     (gnus-set-mode-line 'summary)
7630     ;; We return how many articles were removed from the summary
7631     ;; buffer as a result of the new limit.
7632     (- total (length gnus-newsgroup-data))))
7633
7634 (defsubst gnus-invisible-cut-children (threads)
7635   (let ((num 0))
7636     (while threads
7637       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7638         (incf num))
7639       (pop threads))
7640     (< num 2)))
7641
7642 (defsubst gnus-cut-thread (thread)
7643   "Go forwards in the thread until we find an article that we want to display."
7644   (when (or (eq gnus-fetch-old-headers 'some)
7645             (eq gnus-fetch-old-headers 'invisible)
7646             (numberp gnus-fetch-old-headers)
7647             (eq gnus-build-sparse-threads 'some)
7648             (eq gnus-build-sparse-threads 'more))
7649     ;; Deal with old-fetched headers and sparse threads.
7650     (while (and
7651             thread
7652             (or
7653              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7654              (gnus-summary-article-ancient-p
7655               (mail-header-number (car thread))))
7656             (if (or (<= (length (cdr thread)) 1)
7657                     (eq gnus-fetch-old-headers 'invisible))
7658                 (setq gnus-newsgroup-limit
7659                       (delq (mail-header-number (car thread))
7660                             gnus-newsgroup-limit)
7661                       thread (cadr thread))
7662               (when (gnus-invisible-cut-children (cdr thread))
7663                 (let ((th (cdr thread)))
7664                   (while th
7665                     (if (memq (mail-header-number (caar th))
7666                               gnus-newsgroup-limit)
7667                         (setq thread (car th)
7668                               th nil)
7669                       (setq th (cdr th))))))))))
7670   thread)
7671
7672 (defun gnus-cut-threads (threads)
7673   "Cut off all uninteresting articles from the beginning of threads."
7674   (when (or (eq gnus-fetch-old-headers 'some)
7675             (eq gnus-fetch-old-headers 'invisible)
7676             (numberp gnus-fetch-old-headers)
7677             (eq gnus-build-sparse-threads 'some)
7678             (eq gnus-build-sparse-threads 'more))
7679     (let ((th threads))
7680       (while th
7681         (setcar th (gnus-cut-thread (car th)))
7682         (setq th (cdr th)))))
7683   ;; Remove nixed out threads.
7684   (delq nil threads))
7685
7686 (defun gnus-summary-initial-limit (&optional show-if-empty)
7687   "Figure out what the initial limit is supposed to be on group entry.
7688 This entails weeding out unwanted dormants, low-scored articles,
7689 fetch-old-headers verbiage, and so on."
7690   ;; Most groups have nothing to remove.
7691   (if (or gnus-inhibit-limiting
7692           (and (null gnus-newsgroup-dormant)
7693                (eq gnus-newsgroup-display 'gnus-not-ignore)
7694                (not (eq gnus-fetch-old-headers 'some))
7695                (not (numberp gnus-fetch-old-headers))
7696                (not (eq gnus-fetch-old-headers 'invisible))
7697                (null gnus-summary-expunge-below)
7698                (not (eq gnus-build-sparse-threads 'some))
7699                (not (eq gnus-build-sparse-threads 'more))
7700                (null gnus-thread-expunge-below)
7701                (not gnus-use-nocem)))
7702       ()                                ; Do nothing.
7703     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7704     (setq gnus-newsgroup-limit nil)
7705     (mapatoms
7706      (lambda (node)
7707        (unless (car (symbol-value node))
7708          ;; These threads have no parents -- they are roots.
7709          (let ((nodes (cdr (symbol-value node)))
7710                thread)
7711            (while nodes
7712              (if (and gnus-thread-expunge-below
7713                       (< (gnus-thread-total-score (car nodes))
7714                          gnus-thread-expunge-below))
7715                  (gnus-expunge-thread (pop nodes))
7716                (setq thread (pop nodes))
7717                (gnus-summary-limit-children thread))))))
7718      gnus-newsgroup-dependencies)
7719     ;; If this limitation resulted in an empty group, we might
7720     ;; pop the previous limit and use it instead.
7721     (when (and (not gnus-newsgroup-limit)
7722                show-if-empty)
7723       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7724     gnus-newsgroup-limit))
7725
7726 (defun gnus-summary-limit-children (thread)
7727   "Return 1 if this subthread is visible and 0 if it is not."
7728   ;; First we get the number of visible children to this thread.  This
7729   ;; is done by recursing down the thread using this function, so this
7730   ;; will really go down to a leaf article first, before slowly
7731   ;; working its way up towards the root.
7732   (when thread
7733     (let* ((max-lisp-eval-depth 5000)
7734            (children
7735            (if (cdr thread)
7736                (apply '+ (mapcar 'gnus-summary-limit-children
7737                                  (cdr thread)))
7738              0))
7739           (number (mail-header-number (car thread)))
7740           score)
7741       (if (and
7742            (not (memq number gnus-newsgroup-marked))
7743            (or
7744             ;; If this article is dormant and has absolutely no visible
7745             ;; children, then this article isn't visible.
7746             (and (memq number gnus-newsgroup-dormant)
7747                  (zerop children))
7748             ;; If this is "fetch-old-headered" and there is no
7749             ;; visible children, then we don't want this article.
7750             (and (or (eq gnus-fetch-old-headers 'some)
7751                      (numberp gnus-fetch-old-headers))
7752                  (gnus-summary-article-ancient-p number)
7753                  (zerop children))
7754             ;; If this is "fetch-old-headered" and `invisible', then
7755             ;; we don't want this article.
7756             (and (eq gnus-fetch-old-headers 'invisible)
7757                  (gnus-summary-article-ancient-p number))
7758             ;; If this is a sparsely inserted article with no children,
7759             ;; we don't want it.
7760             (and (eq gnus-build-sparse-threads 'some)
7761                  (gnus-summary-article-sparse-p number)
7762                  (zerop children))
7763             ;; If we use expunging, and this article is really
7764             ;; low-scored, then we don't want this article.
7765             (when (and gnus-summary-expunge-below
7766                        (< (setq score
7767                                 (or (cdr (assq number gnus-newsgroup-scored))
7768                                     gnus-summary-default-score))
7769                           gnus-summary-expunge-below))
7770               ;; We increase the expunge-tally here, but that has
7771               ;; nothing to do with the limits, really.
7772               (incf gnus-newsgroup-expunged-tally)
7773               ;; We also mark as read here, if that's wanted.
7774               (when (and gnus-summary-mark-below
7775                          (< score gnus-summary-mark-below))
7776                 (setq gnus-newsgroup-unreads
7777                       (delq number gnus-newsgroup-unreads))
7778                 (if gnus-newsgroup-auto-expire
7779                     (push number gnus-newsgroup-expirable)
7780                   (push (cons number gnus-low-score-mark)
7781                         gnus-newsgroup-reads)))
7782               t)
7783             ;; Do the `display' group parameter.
7784             (and gnus-newsgroup-display
7785                  (not (funcall gnus-newsgroup-display)))
7786             ;; Check NoCeM things.
7787             (if (and gnus-use-nocem
7788                      (gnus-nocem-unwanted-article-p
7789                       (mail-header-id (car thread))))
7790                 (progn
7791                   (setq gnus-newsgroup-unreads
7792                         (delq number gnus-newsgroup-unreads))
7793                   t))))
7794           ;; Nope, invisible article.
7795           0
7796         ;; Ok, this article is to be visible, so we add it to the limit
7797         ;; and return 1.
7798         (push number gnus-newsgroup-limit)
7799         1))))
7800
7801 (defun gnus-expunge-thread (thread)
7802   "Mark all articles in THREAD as read."
7803   (let* ((number (mail-header-number (car thread))))
7804     (incf gnus-newsgroup-expunged-tally)
7805     ;; We also mark as read here, if that's wanted.
7806     (setq gnus-newsgroup-unreads
7807           (delq number gnus-newsgroup-unreads))
7808     (if gnus-newsgroup-auto-expire
7809         (push number gnus-newsgroup-expirable)
7810       (push (cons number gnus-low-score-mark)
7811             gnus-newsgroup-reads)))
7812   ;; Go recursively through all subthreads.
7813   (mapcar 'gnus-expunge-thread (cdr thread)))
7814
7815 ;; Summary article oriented commands
7816
7817 (defun gnus-summary-refer-parent-article (n)
7818   "Refer parent article N times.
7819 If N is negative, go to ancestor -N instead.
7820 The difference between N and the number of articles fetched is returned."
7821   (interactive "p")
7822   (let ((skip 1)
7823         error header ref)
7824     (when (not (natnump n))
7825       (setq skip (abs n)
7826             n 1))
7827     (while (and (> n 0)
7828                 (not error))
7829       (setq header (gnus-summary-article-header))
7830       (if (and (eq (mail-header-number header)
7831                    (cdr gnus-article-current))
7832                (equal gnus-newsgroup-name
7833                       (car gnus-article-current)))
7834           ;; If we try to find the parent of the currently
7835           ;; displayed article, then we take a look at the actual
7836           ;; References header, since this is slightly more
7837           ;; reliable than the References field we got from the
7838           ;; server.
7839           (save-excursion
7840             (set-buffer gnus-original-article-buffer)
7841             (nnheader-narrow-to-headers)
7842             (unless (setq ref (message-fetch-field "references"))
7843               (setq ref (message-fetch-field "in-reply-to")))
7844             (widen))
7845         (setq ref
7846               ;; It's not the current article, so we take a bet on
7847               ;; the value we got from the server.
7848               (mail-header-references header)))
7849       (if (and ref
7850                (not (equal ref "")))
7851           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7852             (gnus-message 1 "Couldn't find parent"))
7853         (gnus-message 1 "No references in article %d"
7854                       (gnus-summary-article-number))
7855         (setq error t))
7856       (decf n))
7857     (gnus-summary-position-point)
7858     n))
7859
7860 (defun gnus-summary-refer-references ()
7861   "Fetch all articles mentioned in the References header.
7862 Return the number of articles fetched."
7863   (interactive)
7864   (let ((ref (mail-header-references (gnus-summary-article-header)))
7865         (current (gnus-summary-article-number))
7866         (n 0))
7867     (if (or (not ref)
7868             (equal ref ""))
7869         (error "No References in the current article")
7870       ;; For each Message-ID in the References header...
7871       (while (string-match "<[^>]*>" ref)
7872         (incf n)
7873         ;; ... fetch that article.
7874         (gnus-summary-refer-article
7875          (prog1 (match-string 0 ref)
7876            (setq ref (substring ref (match-end 0))))))
7877       (gnus-summary-goto-subject current)
7878       (gnus-summary-position-point)
7879       n)))
7880
7881 (defun gnus-summary-refer-thread (&optional limit)
7882   "Fetch all articles in the current thread.
7883 If LIMIT (the numerical prefix), fetch that many old headers instead
7884 of what's specified by the `gnus-refer-thread-limit' variable."
7885   (interactive "P")
7886   (let ((id (mail-header-id (gnus-summary-article-header)))
7887         (limit (if limit (prefix-numeric-value limit)
7888                  gnus-refer-thread-limit)))
7889     ;; We want to fetch LIMIT *old* headers, but we also have to
7890     ;; re-fetch all the headers in the current buffer, because many of
7891     ;; them may be undisplayed.  So we adjust LIMIT.
7892     (when (numberp limit)
7893       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7894     (unless (eq gnus-fetch-old-headers 'invisible)
7895       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7896       ;; Retrieve the headers and read them in.
7897       (if (eq (gnus-retrieve-headers
7898                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7899               'nov)
7900           (gnus-build-all-threads)
7901         (error "Can't fetch thread from backends that don't support NOV"))
7902       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7903     (gnus-summary-limit-include-thread id)))
7904
7905 (defun gnus-summary-refer-article (message-id)
7906   "Fetch an article specified by MESSAGE-ID."
7907   (interactive "sMessage-ID: ")
7908   (when (and (stringp message-id)
7909              (not (zerop (length message-id))))
7910     ;; Construct the correct Message-ID if necessary.
7911     ;; Suggested by tale@pawl.rpi.edu.
7912     (unless (string-match "^<" message-id)
7913       (setq message-id (concat "<" message-id)))
7914     (unless (string-match ">$" message-id)
7915       (setq message-id (concat message-id ">")))
7916     (let* ((header (gnus-id-to-header message-id))
7917            (sparse (and header
7918                         (gnus-summary-article-sparse-p
7919                          (mail-header-number header))
7920                         (memq (mail-header-number header)
7921                               gnus-newsgroup-limit)))
7922            number)
7923       (cond
7924        ;; If the article is present in the buffer we just go to it.
7925        ((and header
7926              (or (not (gnus-summary-article-sparse-p
7927                        (mail-header-number header)))
7928                  sparse))
7929         (prog1
7930             (gnus-summary-goto-article
7931              (mail-header-number header) nil t)
7932           (when sparse
7933             (gnus-summary-update-article (mail-header-number header)))))
7934        (t
7935         ;; We fetch the article.
7936         (catch 'found
7937           (dolist (gnus-override-method (gnus-refer-article-methods))
7938             (gnus-check-server gnus-override-method)
7939             ;; Fetch the header, and display the article.
7940             (when (setq number (gnus-summary-insert-subject message-id))
7941               (gnus-summary-select-article nil nil nil number)
7942               (throw 'found t)))
7943           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7944
7945 (defun gnus-refer-article-methods ()
7946   "Return a list of referable methods."
7947   (cond
7948    ;; No method, so we default to current and native.
7949    ((null gnus-refer-article-method)
7950     (list gnus-current-select-method gnus-select-method))
7951    ;; Current.
7952    ((eq 'current gnus-refer-article-method)
7953     (list gnus-current-select-method))
7954    ;; List of select methods.
7955    ((not (and (symbolp (car gnus-refer-article-method))
7956               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7957     (let (out)
7958       (dolist (method gnus-refer-article-method)
7959         (push (if (eq 'current method)
7960                   gnus-current-select-method
7961                 method)
7962               out))
7963       (nreverse out)))
7964    ;; One single select method.
7965    (t
7966     (list gnus-refer-article-method))))
7967
7968 (defun gnus-summary-edit-parameters ()
7969   "Edit the group parameters of the current group."
7970   (interactive)
7971   (gnus-group-edit-group gnus-newsgroup-name 'params))
7972
7973 (defun gnus-summary-customize-parameters ()
7974   "Customize the group parameters of the current group."
7975   (interactive)
7976   (gnus-group-customize gnus-newsgroup-name))
7977
7978 (defun gnus-summary-enter-digest-group (&optional force)
7979   "Enter an nndoc group based on the current article.
7980 If FORCE, force a digest interpretation.  If not, try
7981 to guess what the document format is."
7982   (interactive "P")
7983   (let ((conf gnus-current-window-configuration))
7984     (save-excursion
7985       (gnus-summary-select-article))
7986     (setq gnus-current-window-configuration conf)
7987     (let* ((name (format "%s-%d"
7988                          (gnus-group-prefixed-name
7989                           gnus-newsgroup-name (list 'nndoc ""))
7990                          (save-excursion
7991                            (set-buffer gnus-summary-buffer)
7992                            gnus-current-article)))
7993            (ogroup gnus-newsgroup-name)
7994            (params (append (gnus-info-params (gnus-get-info ogroup))
7995                            (list (cons 'to-group ogroup))
7996                            (list (cons 'save-article-group ogroup))))
7997            (case-fold-search t)
7998            (buf (current-buffer))
7999            dig to-address)
8000       (save-excursion
8001         (set-buffer gnus-original-article-buffer)
8002         ;; Have the digest group inherit the main mail address of
8003         ;; the parent article.
8004         (when (setq to-address (or (gnus-fetch-field "reply-to")
8005                                    (gnus-fetch-field "from")))
8006           (setq params (append
8007                         (list (cons 'to-address
8008                                     (funcall gnus-decode-encoded-word-function
8009                                              to-address))))))
8010         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8011         (insert-buffer-substring gnus-original-article-buffer)
8012         ;; Remove lines that may lead nndoc to misinterpret the
8013         ;; document type.
8014         (narrow-to-region
8015          (goto-char (point-min))
8016          (or (search-forward "\n\n" nil t) (point)))
8017         (goto-char (point-min))
8018         (delete-matching-lines "^Path:\\|^From ")
8019         (widen))
8020       (unwind-protect
8021           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8022                     (gnus-newsgroup-ephemeral-ignored-charsets
8023                      gnus-newsgroup-ignored-charsets))
8024                 (gnus-group-read-ephemeral-group
8025                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8026                               (nndoc-article-type
8027                                ,(if force 'mbox 'guess)))
8028                  t nil nil nil
8029                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8030                                                         "ADAPT")))))
8031               ;; Make all postings to this group go to the parent group.
8032               (nconc (gnus-info-params (gnus-get-info name))
8033                      params)
8034             ;; Couldn't select this doc group.
8035             (switch-to-buffer buf)
8036             (gnus-set-global-variables)
8037             (gnus-configure-windows 'summary)
8038             (gnus-message 3 "Article couldn't be entered?"))
8039         (kill-buffer dig)))))
8040
8041 (defun gnus-summary-read-document (n)
8042   "Open a new group based on the current article(s).
8043 This will allow you to read digests and other similar
8044 documents as newsgroups.
8045 Obeys the standard process/prefix convention."
8046   (interactive "P")
8047   (let* ((articles (gnus-summary-work-articles n))
8048          (ogroup gnus-newsgroup-name)
8049          (params (append (gnus-info-params (gnus-get-info ogroup))
8050                          (list (cons 'to-group ogroup))))
8051          article group egroup groups vgroup)
8052     (while (setq article (pop articles))
8053       (setq group (format "%s-%d" gnus-newsgroup-name article))
8054       (gnus-summary-remove-process-mark article)
8055       (when (gnus-summary-display-article article)
8056         (save-excursion
8057           (with-temp-buffer
8058             (insert-buffer-substring gnus-original-article-buffer)
8059             ;; Remove some headers that may lead nndoc to make
8060             ;; the wrong guess.
8061             (message-narrow-to-head)
8062             (goto-char (point-min))
8063             (delete-matching-lines "^\\(Path\\):\\|^From ")
8064             (widen)
8065             (if (setq egroup
8066                       (gnus-group-read-ephemeral-group
8067                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8068                                      (nndoc-article-type guess))
8069                        t nil t))
8070                 (progn
8071             ;; Make all postings to this group go to the parent group.
8072                   (nconc (gnus-info-params (gnus-get-info egroup))
8073                          params)
8074                   (push egroup groups))
8075               ;; Couldn't select this doc group.
8076               (gnus-error 3 "Article couldn't be entered"))))))
8077     ;; Now we have selected all the documents.
8078     (cond
8079      ((not groups)
8080       (error "None of the articles could be interpreted as documents"))
8081      ((gnus-group-read-ephemeral-group
8082        (setq vgroup (format
8083                      "nnvirtual:%s-%s" gnus-newsgroup-name
8084                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8085        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8086        t
8087        (cons (current-buffer) 'summary)))
8088      (t
8089       (error "Couldn't select virtual nndoc group")))))
8090
8091 (defun gnus-summary-isearch-article (&optional regexp-p)
8092   "Do incremental search forward on the current article.
8093 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8094   (interactive "P")
8095   (gnus-summary-select-article)
8096   (gnus-configure-windows 'article)
8097   (gnus-eval-in-buffer-window gnus-article-buffer
8098     (save-restriction
8099       (widen)
8100       (isearch-forward regexp-p))))
8101
8102 (defun gnus-summary-search-article-forward (regexp &optional backward)
8103   "Search for an article containing REGEXP forward.
8104 If BACKWARD, search backward instead."
8105   (interactive
8106    (list (read-string
8107           (format "Search article %s (regexp%s): "
8108                   (if current-prefix-arg "backward" "forward")
8109                   (if gnus-last-search-regexp
8110                       (concat ", default " gnus-last-search-regexp)
8111                     "")))
8112          current-prefix-arg))
8113   (if (string-equal regexp "")
8114       (setq regexp (or gnus-last-search-regexp ""))
8115     (setq gnus-last-search-regexp regexp)
8116     (setq gnus-article-before-search gnus-current-article))
8117   ;; Intentionally set gnus-last-article.
8118   (setq gnus-last-article gnus-article-before-search)
8119   (let ((gnus-last-article gnus-last-article))
8120     (if (gnus-summary-search-article regexp backward)
8121         (gnus-summary-show-thread)
8122       (error "Search failed: \"%s\"" regexp))))
8123
8124 (defun gnus-summary-search-article-backward (regexp)
8125   "Search for an article containing REGEXP backward."
8126   (interactive
8127    (list (read-string
8128           (format "Search article backward (regexp%s): "
8129                   (if gnus-last-search-regexp
8130                       (concat ", default " gnus-last-search-regexp)
8131                     "")))))
8132   (gnus-summary-search-article-forward regexp 'backward))
8133
8134 (defun gnus-summary-search-article (regexp &optional backward)
8135   "Search for an article containing REGEXP.
8136 Optional argument BACKWARD means do search for backward.
8137 `gnus-select-article-hook' is not called during the search."
8138   ;; We have to require this here to make sure that the following
8139   ;; dynamic binding isn't shadowed by autoloading.
8140   (require 'gnus-async)
8141   (require 'gnus-art)
8142   (let ((gnus-select-article-hook nil)  ;Disable hook.
8143         (gnus-article-prepare-hook nil)
8144         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8145         (gnus-use-article-prefetch nil)
8146         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8147         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8148         (sum (current-buffer))
8149         (gnus-display-mime-function nil)
8150         (found nil)
8151         point)
8152     (gnus-save-hidden-threads
8153       (gnus-summary-select-article)
8154       (set-buffer gnus-article-buffer)
8155       (goto-char (window-point (get-buffer-window (current-buffer))))
8156       (when backward
8157         (forward-line -1))
8158       (while (not found)
8159         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8160         (if (if backward
8161                 (re-search-backward regexp nil t)
8162               (re-search-forward regexp nil t))
8163             ;; We found the regexp.
8164             (progn
8165               (setq found 'found)
8166               (beginning-of-line)
8167               (set-window-start
8168                (get-buffer-window (current-buffer))
8169                (point))
8170               (forward-line 1)
8171               (set-window-point
8172                (get-buffer-window (current-buffer))
8173                (point))
8174               (set-buffer sum)
8175               (setq point (point)))
8176           ;; We didn't find it, so we go to the next article.
8177           (set-buffer sum)
8178           (setq found 'not)
8179           (while (eq found 'not)
8180             (if (not (if backward (gnus-summary-find-prev)
8181                        (gnus-summary-find-next)))
8182                 ;; No more articles.
8183                 (setq found t)
8184               ;; Select the next article and adjust point.
8185               (unless (gnus-summary-article-sparse-p
8186                        (gnus-summary-article-number))
8187                 (setq found nil)
8188                 (gnus-summary-select-article)
8189                 (set-buffer gnus-article-buffer)
8190                 (widen)
8191                 (goto-char (if backward (point-max) (point-min))))))))
8192       (gnus-message 7 ""))
8193     ;; Return whether we found the regexp.
8194     (when (eq found 'found)
8195       (goto-char point)
8196       (gnus-summary-show-thread)
8197       (gnus-summary-goto-subject gnus-current-article)
8198       (gnus-summary-position-point)
8199       t)))
8200
8201 (defun gnus-find-matching-articles (header regexp)
8202   "Return a list of all articles that match REGEXP on HEADER.
8203 This search includes all articles in the current group that Gnus has
8204 fetched headers for, whether they are displayed or not."
8205   (let ((articles nil)
8206         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8207         (case-fold-search t))
8208     (dolist (header gnus-newsgroup-headers)
8209       (when (string-match regexp (funcall func header))
8210         (push (mail-header-number header) articles)))
8211     (nreverse articles)))
8212
8213 (defun gnus-summary-find-matching (header regexp &optional backward unread
8214                                           not-case-fold not-matching)
8215   "Return a list of all articles that match REGEXP on HEADER.
8216 The search stars on the current article and goes forwards unless
8217 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8218 If UNREAD is non-nil, only unread articles will
8219 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8220 in the comparisons. If NOT-MATCHING, return a list of all articles that
8221 not match REGEXP on HEADER."
8222   (let ((case-fold-search (not not-case-fold))
8223         articles d func)
8224     (if (consp header)
8225         (if (eq (car header) 'extra)
8226             (setq func
8227                   `(lambda (h)
8228                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8229                          "")))
8230           (error "%s is an invalid header" header))
8231       (unless (fboundp (intern (concat "mail-header-" header)))
8232         (error "%s is not a valid header" header))
8233       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8234     (dolist (d (if (eq backward 'all)
8235                    gnus-newsgroup-data
8236                  (gnus-data-find-list
8237                   (gnus-summary-article-number)
8238                   (gnus-data-list backward))))
8239       (when (and (or (not unread)       ; We want all articles...
8240                      (gnus-data-unread-p d)) ; Or just unreads.
8241                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8242                  (if not-matching
8243                      (not (string-match
8244                            regexp
8245                            (funcall func (gnus-data-header d))))
8246                    (string-match regexp
8247                                  (funcall func (gnus-data-header d)))))
8248         (push (gnus-data-number d) articles))) ; Success!
8249     (nreverse articles)))
8250
8251 (defun gnus-summary-execute-command (header regexp command &optional backward)
8252   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8253 If HEADER is an empty string (or nil), the match is done on the entire
8254 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8255   (interactive
8256    (list (let ((completion-ignore-case t))
8257            (completing-read
8258             "Header name: "
8259             (mapcar (lambda (header) (list (format "%s" header)))
8260                     (append
8261                      '("Number" "Subject" "From" "Lines" "Date"
8262                        "Message-ID" "Xref" "References" "Body")
8263                      gnus-extra-headers))
8264             nil 'require-match))
8265          (read-string "Regexp: ")
8266          (read-key-sequence "Command: ")
8267          current-prefix-arg))
8268   (when (equal header "Body")
8269     (setq header ""))
8270   ;; Hidden thread subtrees must be searched as well.
8271   (gnus-summary-show-all-threads)
8272   ;; We don't want to change current point nor window configuration.
8273   (save-excursion
8274     (save-window-excursion
8275       (let (gnus-visual
8276             gnus-treat-strip-trailing-blank-lines
8277             gnus-treat-strip-leading-blank-lines
8278             gnus-treat-strip-multiple-blank-lines
8279             gnus-treat-hide-boring-headers
8280             gnus-treat-fold-newsgroups
8281             gnus-article-prepare-hook)
8282         (gnus-message 6 "Executing %s..." (key-description command))
8283         ;; We'd like to execute COMMAND interactively so as to give arguments.
8284         (gnus-execute header regexp
8285                       `(call-interactively ',(key-binding command))
8286                       backward)
8287         (gnus-message 6 "Executing %s...done" (key-description command))))))
8288
8289 (defun gnus-summary-beginning-of-article ()
8290   "Scroll the article back to the beginning."
8291   (interactive)
8292   (gnus-summary-select-article)
8293   (gnus-configure-windows 'article)
8294   (gnus-eval-in-buffer-window gnus-article-buffer
8295     (widen)
8296     (goto-char (point-min))
8297     (when gnus-page-broken
8298       (gnus-narrow-to-page))))
8299
8300 (defun gnus-summary-end-of-article ()
8301   "Scroll to the end of the article."
8302   (interactive)
8303   (gnus-summary-select-article)
8304   (gnus-configure-windows 'article)
8305   (gnus-eval-in-buffer-window gnus-article-buffer
8306     (widen)
8307     (goto-char (point-max))
8308     (recenter -3)
8309     (when gnus-page-broken
8310       (gnus-narrow-to-page))))
8311
8312 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8313   "Truncate to LEN and quote all \"(\"'s in STRING."
8314   (gnus-replace-in-string (if (and len (> (length string) len))
8315                               (substring string 0 len)
8316                             string)
8317                           "[()]" "\\\\\\&"))
8318
8319 (defun gnus-summary-print-article (&optional filename n)
8320   "Generate and print a PostScript image of the N next (mail) articles.
8321
8322 If N is negative, print the N previous articles.  If N is nil and articles
8323 have been marked with the process mark, print these instead.
8324
8325 If the optional first argument FILENAME is nil, send the image to the
8326 printer.  If FILENAME is a string, save the PostScript image in a file with
8327 that name.  If FILENAME is a number, prompt the user for the name of the file
8328 to save in."
8329   (interactive (list (ps-print-preprint current-prefix-arg)))
8330   (dolist (article (gnus-summary-work-articles n))
8331     (gnus-summary-select-article nil nil 'pseudo article)
8332     (gnus-eval-in-buffer-window gnus-article-buffer
8333       (gnus-print-buffer))
8334     (gnus-summary-remove-process-mark article))
8335   (ps-despool filename))
8336
8337 (defun gnus-print-buffer ()
8338   (let ((buffer (generate-new-buffer " *print*")))
8339     (unwind-protect
8340         (progn
8341           (copy-to-buffer buffer (point-min) (point-max))
8342           (set-buffer buffer)
8343           (gnus-article-delete-invisible-text)
8344           (gnus-remove-text-with-property 'gnus-decoration)
8345           (when (gnus-visual-p 'article-highlight 'highlight)
8346             ;; Copy-to-buffer doesn't copy overlay.  So redo
8347             ;; highlight.
8348             (let ((gnus-article-buffer buffer))
8349               (gnus-article-highlight-citation t)
8350               (gnus-article-highlight-signature)))
8351           (let ((ps-left-header
8352                  (list
8353                   (concat "("
8354                           (gnus-summary-print-truncate-and-quote
8355                            (mail-header-subject gnus-current-headers)
8356                            66) ")")
8357                   (concat "("
8358                           (gnus-summary-print-truncate-and-quote
8359                            (mail-header-from gnus-current-headers)
8360                            45) ")")))
8361                 (ps-right-header
8362                  (list
8363                   "/pagenumberstring load"
8364                   (concat "("
8365                           (mail-header-date gnus-current-headers) ")"))))
8366             (gnus-run-hooks 'gnus-ps-print-hook)
8367             (save-excursion
8368               (if window-system
8369                   (ps-spool-buffer-with-faces)
8370                 (ps-spool-buffer)))))
8371       (kill-buffer buffer))))
8372
8373 (defun gnus-summary-show-article (&optional arg)
8374   "Force redisplaying of the current article.
8375 If ARG (the prefix) is a number, show the article with the charset
8376 defined in `gnus-summary-show-article-charset-alist', or the charset
8377 input.
8378 If ARG (the prefix) is non-nil and not a number, show the raw article
8379 without any article massaging functions being run.  Normally, the key strokes
8380 are `C-u g'."
8381   (interactive "P")
8382   (cond
8383    ((numberp arg)
8384     (gnus-summary-show-article t)
8385     (let ((gnus-newsgroup-charset
8386            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8387                (mm-read-coding-system
8388                 "View as charset: " ;; actually it is coding system.
8389                 (save-excursion
8390                   (set-buffer gnus-article-buffer)
8391                   (mm-detect-coding-region (point) (point-max))))))
8392           (gnus-newsgroup-ignored-charsets 'gnus-all))
8393       (gnus-summary-select-article nil 'force)
8394       (let ((deps gnus-newsgroup-dependencies)
8395             head header lines)
8396         (save-excursion
8397           (set-buffer gnus-original-article-buffer)
8398           (save-restriction
8399             (message-narrow-to-head)
8400             (setq head (buffer-string))
8401             (goto-char (point-min))
8402             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8403               (goto-char (point-max))
8404               (widen)
8405               (setq lines (1- (count-lines (point) (point-max))))))
8406           (with-temp-buffer
8407             (insert (format "211 %d Article retrieved.\n"
8408                             (cdr gnus-article-current)))
8409             (insert head)
8410             (if lines (insert (format "Lines: %d\n" lines)))
8411             (insert ".\n")
8412             (let ((nntp-server-buffer (current-buffer)))
8413               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8414         (gnus-data-set-header
8415          (gnus-data-find (cdr gnus-article-current))
8416          header)
8417         (gnus-summary-update-article-line
8418          (cdr gnus-article-current) header)
8419         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8420           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8421    ((not arg)
8422     ;; Select the article the normal way.
8423     (gnus-summary-select-article nil 'force))
8424    (t
8425     ;; We have to require this here to make sure that the following
8426     ;; dynamic binding isn't shadowed by autoloading.
8427     (require 'gnus-async)
8428     (require 'gnus-art)
8429     ;; Bind the article treatment functions to nil.
8430     (let ((gnus-have-all-headers t)
8431           gnus-article-prepare-hook
8432           gnus-article-decode-hook
8433           gnus-display-mime-function
8434           gnus-break-pages)
8435       ;; Destroy any MIME parts.
8436       (when (gnus-buffer-live-p gnus-article-buffer)
8437         (save-excursion
8438           (set-buffer gnus-article-buffer)
8439           (mm-destroy-parts gnus-article-mime-handles)
8440           ;; Set it to nil for safety reason.
8441           (setq gnus-article-mime-handle-alist nil)
8442           (setq gnus-article-mime-handles nil)))
8443       (gnus-summary-select-article nil 'force))))
8444   (gnus-summary-goto-subject gnus-current-article)
8445   (gnus-summary-position-point))
8446
8447 (defun gnus-summary-show-raw-article ()
8448   "Show the raw article without any article massaging functions being run."
8449   (interactive)
8450   (gnus-summary-show-article t))
8451
8452 (defun gnus-summary-verbose-headers (&optional arg)
8453   "Toggle permanent full header display.
8454 If ARG is a positive number, turn header display on.
8455 If ARG is a negative number, turn header display off."
8456   (interactive "P")
8457   (setq gnus-show-all-headers
8458         (cond ((or (not (numberp arg))
8459                    (zerop arg))
8460                (not gnus-show-all-headers))
8461               ((natnump arg)
8462                t)))
8463   (gnus-summary-show-article))
8464
8465 (defun gnus-summary-toggle-header (&optional arg)
8466   "Show the headers if they are hidden, or hide them if they are shown.
8467 If ARG is a positive number, show the entire header.
8468 If ARG is a negative number, hide the unwanted header lines."
8469   (interactive "P")
8470   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8471                      (get-buffer-window gnus-article-buffer t))))
8472     (with-current-buffer gnus-article-buffer
8473       (widen)
8474       (article-narrow-to-head)
8475       (let* ((buffer-read-only nil)
8476              (inhibit-point-motion-hooks t)
8477              (hidden (if (numberp arg)
8478                          (>= arg 0)
8479                        (gnus-article-hidden-text-p 'headers)))
8480              s e)
8481         (delete-region (point-min) (point-max))
8482         (with-current-buffer gnus-original-article-buffer
8483           (goto-char (setq s (point-min)))
8484           (setq e (if (search-forward "\n\n" nil t)
8485                       (1- (point))
8486                     (point-max))))
8487         (insert-buffer-substring gnus-original-article-buffer s e)
8488         (article-decode-encoded-words)
8489         (if hidden
8490             (let ((gnus-treat-hide-headers nil)
8491                   (gnus-treat-hide-boring-headers nil))
8492               (gnus-delete-wash-type 'headers)
8493               (gnus-treat-article 'head))
8494           (gnus-treat-article 'head))
8495         (widen)
8496         (if window
8497             (set-window-start window (goto-char (point-min))))
8498         (setq gnus-page-broken
8499               (when gnus-break-pages
8500                 (gnus-narrow-to-page)
8501                 t))
8502         (gnus-set-mode-line 'article)))))
8503
8504 (defun gnus-summary-show-all-headers ()
8505   "Make all header lines visible."
8506   (interactive)
8507   (gnus-summary-toggle-header 1))
8508
8509 (defun gnus-summary-caesar-message (&optional arg)
8510   "Caesar rotate the current article by 13.
8511 The numerical prefix specifies how many places to rotate each letter
8512 forward."
8513   (interactive "P")
8514   (gnus-summary-select-article)
8515   (let ((mail-header-separator ""))
8516     (gnus-eval-in-buffer-window gnus-article-buffer
8517       (save-restriction
8518         (widen)
8519         (let ((start (window-start))
8520               buffer-read-only)
8521           (message-caesar-buffer-body arg)
8522           (set-window-start (get-buffer-window (current-buffer)) start))))))
8523
8524 (autoload 'unmorse-region "morse"
8525   "Convert morse coded text in region to ordinary ASCII text."
8526   t)
8527
8528 (defun gnus-summary-morse-message (&optional arg)
8529   "Morse decode the current article."
8530   (interactive "P")
8531   (gnus-summary-select-article)
8532   (let ((mail-header-separator ""))
8533     (gnus-eval-in-buffer-window gnus-article-buffer
8534       (save-excursion
8535         (save-restriction
8536           (widen)
8537           (let ((pos (window-start))
8538                 buffer-read-only)
8539             (goto-char (point-min))
8540             (when (message-goto-body)
8541               (gnus-narrow-to-body))
8542             (goto-char (point-min))
8543             (while (re-search-forward "·" (point-max) t)
8544               (replace-match "."))
8545             (unmorse-region (point-min) (point-max))
8546             (widen)
8547             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8548
8549 (defun gnus-summary-stop-page-breaking ()
8550   "Stop page breaking in the current article."
8551   (interactive)
8552   (gnus-summary-select-article)
8553   (gnus-eval-in-buffer-window gnus-article-buffer
8554     (widen)
8555     (when (gnus-visual-p 'page-marker)
8556       (let ((buffer-read-only nil))
8557         (gnus-remove-text-with-property 'gnus-prev)
8558         (gnus-remove-text-with-property 'gnus-next))
8559       (setq gnus-page-broken nil))))
8560
8561 (defun gnus-summary-move-article (&optional n to-newsgroup
8562                                             select-method action)
8563   "Move the current article to a different newsgroup.
8564 If N is a positive number, move the N next articles.
8565 If N is a negative number, move the N previous articles.
8566 If N is nil and any articles have been marked with the process mark,
8567 move those articles instead.
8568 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8569 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8570 re-spool using this method.
8571
8572 When called interactively with TO-NEWSGROUP being nil, the value of
8573 the variable `gnus-move-split-methods' is used for finding a default
8574 for the target newsgroup.
8575
8576 For this function to work, both the current newsgroup and the
8577 newsgroup that you want to move to have to support the `request-move'
8578 and `request-accept' functions.
8579
8580 ACTION can be either `move' (the default), `crosspost' or `copy'."
8581   (interactive "P")
8582   (unless action
8583     (setq action 'move))
8584   ;; Check whether the source group supports the required functions.
8585   (cond ((and (eq action 'move)
8586               (not (gnus-check-backend-function
8587                     'request-move-article gnus-newsgroup-name)))
8588          (error "The current group does not support article moving"))
8589         ((and (eq action 'crosspost)
8590               (not (gnus-check-backend-function
8591                     'request-replace-article gnus-newsgroup-name)))
8592          (error "The current group does not support article editing")))
8593   (let ((articles (gnus-summary-work-articles n))
8594         (prefix (if (gnus-check-backend-function
8595                      'request-move-article gnus-newsgroup-name)
8596                     (gnus-group-real-prefix gnus-newsgroup-name)
8597                   ""))
8598         (names '((move "Move" "Moving")
8599                  (copy "Copy" "Copying")
8600                  (crosspost "Crosspost" "Crossposting")))
8601         (copy-buf (save-excursion
8602                     (nnheader-set-temp-buffer " *copy article*")))
8603         art-group to-method new-xref article to-groups)
8604     (unless (assq action names)
8605       (error "Unknown action %s" action))
8606     ;; Read the newsgroup name.
8607     (when (and (not to-newsgroup)
8608                (not select-method))
8609       (if (and gnus-move-split-methods
8610                (not
8611                 (and (memq gnus-current-article articles)
8612                      (gnus-buffer-live-p gnus-original-article-buffer))))
8613           ;; When `gnus-move-split-methods' is non-nil, we have to
8614           ;; select an article to give `gnus-read-move-group-name' an
8615           ;; opportunity to suggest an appropriate default.  However,
8616           ;; we needn't render or mark the article.
8617           (let ((gnus-display-mime-function nil)
8618                 (gnus-article-prepare-hook nil)
8619                 (gnus-mark-article-hook nil))
8620             (gnus-summary-select-article nil nil nil (car articles))))
8621       (setq to-newsgroup
8622             (gnus-read-move-group-name
8623              (cadr (assq action names))
8624              (symbol-value (intern (format "gnus-current-%s-group" action)))
8625              articles prefix))
8626       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8627     (setq to-method (or select-method
8628                         (gnus-server-to-method
8629                          (gnus-group-method to-newsgroup))))
8630     ;; Check the method we are to move this article to...
8631     (unless (gnus-check-backend-function
8632              'request-accept-article (car to-method))
8633       (error "%s does not support article copying" (car to-method)))
8634     (unless (gnus-check-server to-method)
8635       (error "Can't open server %s" (car to-method)))
8636     (gnus-message 6 "%s to %s: %s..."
8637                   (caddr (assq action names))
8638                   (or (car select-method) to-newsgroup) articles)
8639     (while articles
8640       (setq article (pop articles))
8641       (setq
8642        art-group
8643        (cond
8644         ;; Move the article.
8645         ((eq action 'move)
8646          ;; Remove this article from future suppression.
8647          (gnus-dup-unsuppress-article article)
8648          (gnus-request-move-article
8649           article                       ; Article to move
8650           gnus-newsgroup-name           ; From newsgroup
8651           (nth 1 (gnus-find-method-for-group
8652                   gnus-newsgroup-name)) ; Server
8653           (list 'gnus-request-accept-article
8654                 to-newsgroup (list 'quote select-method)
8655                 (not articles) t)       ; Accept form
8656           (not articles)))              ; Only save nov last time
8657         ;; Copy the article.
8658         ((eq action 'copy)
8659          (save-excursion
8660            (set-buffer copy-buf)
8661            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8662              (gnus-request-accept-article
8663               to-newsgroup select-method (not articles) t))))
8664         ;; Crosspost the article.
8665         ((eq action 'crosspost)
8666          (let ((xref (message-tokenize-header
8667                       (mail-header-xref (gnus-summary-article-header article))
8668                       " ")))
8669            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8670                                   ":" (number-to-string article)))
8671            (unless xref
8672              (setq xref (list (system-name))))
8673            (setq new-xref
8674                  (concat
8675                   (mapconcat 'identity
8676                              (delete "Xref:" (delete new-xref xref))
8677                              " ")
8678                   " " new-xref))
8679            (save-excursion
8680              (set-buffer copy-buf)
8681              ;; First put the article in the destination group.
8682              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8683              (when (consp (setq art-group
8684                                 (gnus-request-accept-article
8685                                  to-newsgroup select-method (not articles))))
8686                (setq new-xref (concat new-xref " " (car art-group)
8687                                       ":"
8688                                       (number-to-string (cdr art-group))))
8689                ;; Now we have the new Xrefs header, so we insert
8690                ;; it and replace the new article.
8691                (nnheader-replace-header "Xref" new-xref)
8692                (gnus-request-replace-article
8693                 (cdr art-group) to-newsgroup (current-buffer))
8694                art-group))))))
8695       (cond
8696        ((not art-group)
8697         (gnus-message 1 "Couldn't %s article %s: %s"
8698                       (cadr (assq action names)) article
8699                       (nnheader-get-report (car to-method))))
8700        ((eq art-group 'junk)
8701         (when (eq action 'move)
8702           (gnus-summary-mark-article article gnus-canceled-mark)
8703           (gnus-message 4 "Deleted article %s" article)))
8704        (t
8705         (let* ((pto-group (gnus-group-prefixed-name
8706                            (car art-group) to-method))
8707                (entry
8708                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8709                (info (nth 2 entry))
8710                (to-group (gnus-info-group info))
8711                to-marks)
8712           ;; Update the group that has been moved to.
8713           (when (and info
8714                      (memq action '(move copy)))
8715             (unless (member to-group to-groups)
8716               (push to-group to-groups))
8717
8718             (unless (memq article gnus-newsgroup-unreads)
8719               (push 'read to-marks)
8720               (gnus-info-set-read
8721                info (gnus-add-to-range (gnus-info-read info)
8722                                        (list (cdr art-group)))))
8723
8724             ;; See whether the article is to be put in the cache.
8725             (let ((marks gnus-article-mark-lists)
8726                   (to-article (cdr art-group)))
8727
8728               ;; Enter the article into the cache in the new group,
8729               ;; if that is required.
8730               (when gnus-use-cache
8731                 (gnus-cache-possibly-enter-article
8732                  to-group to-article
8733                  (memq article gnus-newsgroup-marked)
8734                  (memq article gnus-newsgroup-dormant)
8735                  (memq article gnus-newsgroup-unreads)))
8736
8737               (when gnus-preserve-marks
8738                 ;; Copy any marks over to the new group.
8739                 (when (and (equal to-group gnus-newsgroup-name)
8740                            (not (memq article gnus-newsgroup-unreads)))
8741                   ;; Mark this article as read in this group.
8742                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8743                   (setcdr (gnus-active to-group) to-article)
8744                   (setcdr gnus-newsgroup-active to-article))
8745
8746                 (while marks
8747                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8748                     (when (memq article (symbol-value
8749                                          (intern (format "gnus-newsgroup-%s"
8750                                                          (caar marks)))))
8751                       (push (cdar marks) to-marks)
8752                       ;; If the other group is the same as this group,
8753                       ;; then we have to add the mark to the list.
8754                       (when (equal to-group gnus-newsgroup-name)
8755                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8756                              (cons to-article
8757                                    (symbol-value
8758                                     (intern (format "gnus-newsgroup-%s"
8759                                                     (caar marks)))))))
8760                       ;; Copy the marks to other group.
8761                       (gnus-add-marked-articles
8762                        to-group (cdar marks) (list to-article) info)))
8763                   (setq marks (cdr marks)))
8764
8765                 (gnus-request-set-mark
8766                  to-group (list (list (list to-article) 'add to-marks))))
8767
8768               (gnus-dribble-enter
8769                (concat "(gnus-group-set-info '"
8770                        (gnus-prin1-to-string (gnus-get-info to-group))
8771                        ")"))))
8772
8773           ;; Update the Xref header in this article to point to
8774           ;; the new crossposted article we have just created.
8775           (when (eq action 'crosspost)
8776             (save-excursion
8777               (set-buffer copy-buf)
8778               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8779               (nnheader-replace-header "Xref" new-xref)
8780               (gnus-request-replace-article
8781                article gnus-newsgroup-name (current-buffer)))))
8782
8783         ;;;!!!Why is this necessary?
8784         (set-buffer gnus-summary-buffer)
8785
8786         (gnus-summary-goto-subject article)
8787         (when (eq action 'move)
8788           (gnus-summary-mark-article article gnus-canceled-mark))))
8789       (gnus-summary-remove-process-mark article))
8790     ;; Re-activate all groups that have been moved to.
8791     (save-excursion
8792       (set-buffer gnus-group-buffer)
8793       (let ((gnus-group-marked to-groups))
8794         (gnus-group-get-new-news-this-group nil t)))
8795
8796     (gnus-kill-buffer copy-buf)
8797     (gnus-summary-position-point)
8798     (gnus-set-mode-line 'summary)))
8799
8800 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8801   "Move the current article to a different newsgroup.
8802 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8803 When called interactively, if TO-NEWSGROUP is nil, use the value of
8804 the variable `gnus-move-split-methods' for finding a default target
8805 newsgroup.
8806 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8807 re-spool using this method."
8808   (interactive "P")
8809   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8810
8811 (defun gnus-summary-crosspost-article (&optional n)
8812   "Crosspost the current article to some other group."
8813   (interactive "P")
8814   (gnus-summary-move-article n nil nil 'crosspost))
8815
8816 (defcustom gnus-summary-respool-default-method nil
8817   "Default method type for respooling an article.
8818 If nil, use to the current newsgroup method."
8819   :type 'symbol
8820   :group 'gnus-summary-mail)
8821
8822 (defcustom gnus-summary-display-while-building nil
8823   "If not-nil, show and update the summary buffer as it's being built.
8824 If the value is t, update the buffer after every line is inserted.  If
8825 the value is an integer (N), update the display every N lines."
8826   :group 'gnus-thread
8827   :type '(choice (const :tag "off" nil)
8828                  number
8829                  (const :tag "frequently" t)))
8830
8831 (defun gnus-summary-respool-article (&optional n method)
8832   "Respool the current article.
8833 The article will be squeezed through the mail spooling process again,
8834 which means that it will be put in some mail newsgroup or other
8835 depending on `nnmail-split-methods'.
8836 If N is a positive number, respool the N next articles.
8837 If N is a negative number, respool the N previous articles.
8838 If N is nil and any articles have been marked with the process mark,
8839 respool those articles instead.
8840
8841 Respooling can be done both from mail groups and \"real\" newsgroups.
8842 In the former case, the articles in question will be moved from the
8843 current group into whatever groups they are destined to.  In the
8844 latter case, they will be copied into the relevant groups."
8845   (interactive
8846    (list current-prefix-arg
8847          (let* ((methods (gnus-methods-using 'respool))
8848                 (methname
8849                  (symbol-name (or gnus-summary-respool-default-method
8850                                   (car (gnus-find-method-for-group
8851                                         gnus-newsgroup-name)))))
8852                 (method
8853                  (gnus-completing-read-with-default
8854                   methname "What backend do you want to use when respooling?"
8855                   methods nil t nil 'gnus-mail-method-history))
8856                 ms)
8857            (cond
8858             ((zerop (length (setq ms (gnus-servers-using-backend
8859                                       (intern method)))))
8860              (list (intern method) ""))
8861             ((= 1 (length ms))
8862              (car ms))
8863             (t
8864              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8865                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8866                            ms-alist))))))))
8867   (unless method
8868     (error "No method given for respooling"))
8869   (if (assoc (symbol-name
8870               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8871              (gnus-methods-using 'respool))
8872       (gnus-summary-move-article n nil method)
8873     (gnus-summary-copy-article n nil method)))
8874
8875 (defun gnus-summary-import-article (file &optional edit)
8876   "Import an arbitrary file into a mail newsgroup."
8877   (interactive "fImport file: \nP")
8878   (let ((group gnus-newsgroup-name)
8879         (now (current-time))
8880         atts lines group-art)
8881     (unless (gnus-check-backend-function 'request-accept-article group)
8882       (error "%s does not support article importing" group))
8883     (or (file-readable-p file)
8884         (not (file-regular-p file))
8885         (error "Can't read %s" file))
8886     (save-excursion
8887       (set-buffer (gnus-get-buffer-create " *import file*"))
8888       (erase-buffer)
8889       (nnheader-insert-file-contents file)
8890       (goto-char (point-min))
8891       (if (nnheader-article-p)
8892           (save-restriction
8893             (goto-char (point-min))
8894             (search-forward "\n\n" nil t)
8895             (narrow-to-region (point-min) (1- (point)))
8896             (goto-char (point-min))
8897             (unless (re-search-forward "^date:" nil t)
8898               (goto-char (point-max))
8899               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8900        ;; This doesn't look like an article, so we fudge some headers.
8901         (setq atts (file-attributes file)
8902               lines (count-lines (point-min) (point-max)))
8903         (insert "From: " (read-string "From: ") "\n"
8904                 "Subject: " (read-string "Subject: ") "\n"
8905                 "Date: " (message-make-date (nth 5 atts)) "\n"
8906                 "Message-ID: " (message-make-message-id) "\n"
8907                 "Lines: " (int-to-string lines) "\n"
8908                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8909       (setq group-art (gnus-request-accept-article group nil t))
8910       (kill-buffer (current-buffer)))
8911     (setq gnus-newsgroup-active (gnus-activate-group group))
8912     (forward-line 1)
8913     (gnus-summary-goto-article (cdr group-art) nil t)
8914     (when edit
8915       (gnus-summary-edit-article))))
8916
8917 (defun gnus-summary-create-article ()
8918   "Create an article in a mail newsgroup."
8919   (interactive)
8920   (let ((group gnus-newsgroup-name)
8921         (now (current-time))
8922         group-art)
8923     (unless (gnus-check-backend-function 'request-accept-article group)
8924       (error "%s does not support article importing" group))
8925     (save-excursion
8926       (set-buffer (gnus-get-buffer-create " *import file*"))
8927       (erase-buffer)
8928       (goto-char (point-min))
8929       ;; This doesn't look like an article, so we fudge some headers.
8930       (insert "From: " (read-string "From: ") "\n"
8931               "Subject: " (read-string "Subject: ") "\n"
8932               "Date: " (message-make-date now) "\n"
8933               "Message-ID: " (message-make-message-id) "\n")
8934       (setq group-art (gnus-request-accept-article group nil t))
8935       (kill-buffer (current-buffer)))
8936     (setq gnus-newsgroup-active (gnus-activate-group group))
8937     (forward-line 1)
8938     (gnus-summary-goto-article (cdr group-art) nil t)
8939     (gnus-summary-edit-article)))
8940
8941 (defun gnus-summary-article-posted-p ()
8942   "Say whether the current (mail) article is available from news as well.
8943 This will be the case if the article has both been mailed and posted."
8944   (interactive)
8945   (let ((id (mail-header-references (gnus-summary-article-header)))
8946         (gnus-override-method (car (gnus-refer-article-methods))))
8947     (if (gnus-request-head id "")
8948         (gnus-message 2 "The current message was found on %s"
8949                       gnus-override-method)
8950       (gnus-message 2 "The current message couldn't be found on %s"
8951                     gnus-override-method)
8952       nil)))
8953
8954 (defun gnus-summary-expire-articles (&optional now)
8955   "Expire all articles that are marked as expirable in the current group."
8956   (interactive)
8957   (when (gnus-check-backend-function
8958          'request-expire-articles gnus-newsgroup-name)
8959     ;; This backend supports expiry.
8960     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8961            (expirable (if total
8962                           (progn
8963                             ;; We need to update the info for
8964                             ;; this group for `gnus-list-of-read-articles'
8965                             ;; to give us the right answer.
8966                             (gnus-run-hooks 'gnus-exit-group-hook)
8967                             (gnus-summary-update-info)
8968                             (gnus-list-of-read-articles gnus-newsgroup-name))
8969                         (setq gnus-newsgroup-expirable
8970                               (sort gnus-newsgroup-expirable '<))))
8971            (expiry-wait (if now 'immediate
8972                           (gnus-group-find-parameter
8973                            gnus-newsgroup-name 'expiry-wait)))
8974            (nnmail-expiry-target
8975             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8976                 nnmail-expiry-target))
8977            es)
8978       (when expirable
8979         ;; There are expirable articles in this group, so we run them
8980         ;; through the expiry process.
8981         (gnus-message 6 "Expiring articles...")
8982         (unless (gnus-check-group gnus-newsgroup-name)
8983           (error "Can't open server for %s" gnus-newsgroup-name))
8984         ;; The list of articles that weren't expired is returned.
8985         (save-excursion
8986           (if expiry-wait
8987               (let ((nnmail-expiry-wait-function nil)
8988                     (nnmail-expiry-wait expiry-wait))
8989                 (setq es (gnus-request-expire-articles
8990                           expirable gnus-newsgroup-name)))
8991             (setq es (gnus-request-expire-articles
8992                       expirable gnus-newsgroup-name)))
8993           (unless total
8994             (setq gnus-newsgroup-expirable es))
8995           ;; We go through the old list of expirable, and mark all
8996           ;; really expired articles as nonexistent.
8997           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8998             (let ((gnus-use-cache nil))
8999               (dolist (article expirable)
9000                 (when (and (not (memq article es))
9001                            (gnus-data-find article))
9002                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9003         (gnus-message 6 "Expiring articles...done")))))
9004
9005 (defun gnus-summary-expire-articles-now ()
9006   "Expunge all expirable articles in the current group.
9007 This means that *all* articles that are marked as expirable will be
9008 deleted forever, right now."
9009   (interactive)
9010   (or gnus-expert-user
9011       (gnus-yes-or-no-p
9012        "Are you really, really, really sure you want to delete all these messages? ")
9013       (error "Phew!"))
9014   (gnus-summary-expire-articles t))
9015
9016 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9017 (defun gnus-summary-delete-article (&optional n)
9018   "Delete the N next (mail) articles.
9019 This command actually deletes articles.  This is not a marking
9020 command.  The article will disappear forever from your life, never to
9021 return.
9022 If N is negative, delete backwards.
9023 If N is nil and articles have been marked with the process mark,
9024 delete these instead."
9025   (interactive "P")
9026   (unless (gnus-check-backend-function 'request-expire-articles
9027                                        gnus-newsgroup-name)
9028     (error "The current newsgroup does not support article deletion"))
9029   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9030     (error "Couldn't open server"))
9031   ;; Compute the list of articles to delete.
9032   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9033         (nnmail-expiry-target 'delete)
9034         not-deleted)
9035     (if (and gnus-novice-user
9036              (not (gnus-yes-or-no-p
9037                    (format "Do you really want to delete %s forever? "
9038                            (if (> (length articles) 1)
9039                                (format "these %s articles" (length articles))
9040                              "this article")))))
9041         ()
9042       ;; Delete the articles.
9043       (setq not-deleted (gnus-request-expire-articles
9044                          articles gnus-newsgroup-name 'force))
9045       (while articles
9046         (gnus-summary-remove-process-mark (car articles))
9047         ;; The backend might not have been able to delete the article
9048         ;; after all.
9049         (unless (memq (car articles) not-deleted)
9050           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9051         (setq articles (cdr articles)))
9052       (when not-deleted
9053         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9054     (gnus-summary-position-point)
9055     (gnus-set-mode-line 'summary)
9056     not-deleted))
9057
9058 (defun gnus-summary-edit-article (&optional arg)
9059   "Edit the current article.
9060 This will have permanent effect only in mail groups.
9061 If ARG is nil, edit the decoded articles.
9062 If ARG is 1, edit the raw articles.
9063 If ARG is 2, edit the raw articles even in read-only groups.
9064 If ARG is 3, edit the articles with the current handles.
9065 Otherwise, allow editing of articles even in read-only
9066 groups."
9067   (interactive "P")
9068   (let (force raw current-handles)
9069     (cond
9070      ((null arg))
9071      ((eq arg 1)
9072       (setq raw t))
9073      ((eq arg 2)
9074       (setq raw t
9075             force t))
9076      ((eq arg 3)
9077       (setq current-handles
9078             (and (gnus-buffer-live-p gnus-article-buffer)
9079                  (with-current-buffer gnus-article-buffer
9080                    (prog1
9081                        gnus-article-mime-handles
9082                      (setq gnus-article-mime-handles nil))))))
9083      (t
9084       (setq force t)))
9085     (when (and raw (not force)
9086                (member gnus-newsgroup-name '("nndraft:delayed"
9087                                              "nndraft:drafts"
9088                                              "nndraft:queue")))
9089       (error "Can't edit the raw article in group %s"
9090              gnus-newsgroup-name))
9091     (save-excursion
9092       (set-buffer gnus-summary-buffer)
9093       (let ((mail-parse-charset gnus-newsgroup-charset)
9094             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9095         (gnus-set-global-variables)
9096         (when (and (not force)
9097                    (gnus-group-read-only-p))
9098           (error "The current newsgroup does not support article editing"))
9099         (gnus-summary-show-article t)
9100         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9101           (with-current-buffer gnus-article-buffer
9102             (mm-enable-multibyte)))
9103         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9104             (setq raw t))
9105         (gnus-article-edit-article
9106          (if raw 'ignore
9107            `(lambda ()
9108               (let ((mbl mml-buffer-list))
9109                 (setq mml-buffer-list nil)
9110                 (mime-to-mml ,'current-handles)
9111                 (let ((mbl1 mml-buffer-list))
9112                   (setq mml-buffer-list mbl)
9113                   (set (make-local-variable 'mml-buffer-list) mbl1))
9114                 (make-local-hook 'kill-buffer-hook)
9115                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9116          `(lambda (no-highlight)
9117             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9118                   (message-options message-options)
9119                   (message-options-set-recipient)
9120                   (mail-parse-ignored-charsets
9121                    ',gnus-newsgroup-ignored-charsets))
9122               ,(if (not raw) '(progn
9123                                 (mml-to-mime)
9124                                 (mml-destroy-buffers)
9125                                 (remove-hook 'kill-buffer-hook
9126                                              'mml-destroy-buffers t)
9127                                 (kill-local-variable 'mml-buffer-list)))
9128               (gnus-summary-edit-article-done
9129                ,(or (mail-header-references gnus-current-headers) "")
9130                ,(gnus-group-read-only-p)
9131                ,gnus-summary-buffer no-highlight))))))))
9132
9133 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9134
9135 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9136                                                  no-highlight)
9137   "Make edits to the current article permanent."
9138   (interactive)
9139   (save-excursion
9140    ;; The buffer restriction contains the entire article if it exists.
9141     (when (article-goto-body)
9142       (let ((lines (count-lines (point) (point-max)))
9143             (length (- (point-max) (point)))
9144             (case-fold-search t)
9145             (body (copy-marker (point))))
9146         (goto-char (point-min))
9147         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9148           (delete-region (match-beginning 1) (match-end 1))
9149           (insert (number-to-string length)))
9150         (goto-char (point-min))
9151         (when (re-search-forward
9152                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9153           (delete-region (match-beginning 1) (match-end 1))
9154           (insert (number-to-string length)))
9155         (goto-char (point-min))
9156         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9157           (delete-region (match-beginning 1) (match-end 1))
9158           (insert (number-to-string lines))))))
9159   ;; Replace the article.
9160   (let ((buf (current-buffer)))
9161     (with-temp-buffer
9162       (insert-buffer-substring buf)
9163
9164       (if (and (not read-only)
9165                (not (gnus-request-replace-article
9166                      (cdr gnus-article-current) (car gnus-article-current)
9167                      (current-buffer) t)))
9168           (error "Couldn't replace article")
9169         ;; Update the summary buffer.
9170         (if (and references
9171                  (equal (message-tokenize-header references " ")
9172                         (message-tokenize-header
9173                          (or (message-fetch-field "references") "") " ")))
9174             ;; We only have to update this line.
9175             (save-excursion
9176               (save-restriction
9177                 (message-narrow-to-head)
9178                 (let ((head (buffer-string))
9179                       header)
9180                   (with-temp-buffer
9181                     (insert (format "211 %d Article retrieved.\n"
9182                                     (cdr gnus-article-current)))
9183                     (insert head)
9184                     (insert ".\n")
9185                     (let ((nntp-server-buffer (current-buffer)))
9186                       (setq header (car (gnus-get-newsgroup-headers
9187                                          nil t))))
9188                     (save-excursion
9189                       (set-buffer gnus-summary-buffer)
9190                       (gnus-data-set-header
9191                        (gnus-data-find (cdr gnus-article-current))
9192                        header)
9193                       (gnus-summary-update-article-line
9194                        (cdr gnus-article-current) header)
9195                       (if (gnus-summary-goto-subject
9196                            (cdr gnus-article-current) nil t)
9197                           (gnus-summary-update-secondary-mark
9198                            (cdr gnus-article-current))))))))
9199           ;; Update threads.
9200           (set-buffer (or buffer gnus-summary-buffer))
9201           (gnus-summary-update-article (cdr gnus-article-current))
9202           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9203               (gnus-summary-update-secondary-mark
9204                (cdr gnus-article-current))))
9205         ;; Prettify the article buffer again.
9206         (unless no-highlight
9207           (save-excursion
9208             (set-buffer gnus-article-buffer)
9209             ;;;!!! Fix this -- article should be rehighlighted.
9210             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9211             (set-buffer gnus-original-article-buffer)
9212             (gnus-request-article
9213              (cdr gnus-article-current)
9214              (car gnus-article-current) (current-buffer))))
9215         ;; Prettify the summary buffer line.
9216         (when (gnus-visual-p 'summary-highlight 'highlight)
9217           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9218
9219 (defun gnus-summary-edit-wash (key)
9220   "Perform editing command KEY in the article buffer."
9221   (interactive
9222    (list
9223     (progn
9224       (message "%s" (concat (this-command-keys) "- "))
9225       (read-char))))
9226   (message "")
9227   (gnus-summary-edit-article)
9228   (execute-kbd-macro (concat (this-command-keys) key))
9229   (gnus-article-edit-done))
9230
9231 ;;; Respooling
9232
9233 (defun gnus-summary-respool-query (&optional silent trace)
9234   "Query where the respool algorithm would put this article."
9235   (interactive)
9236   (let (gnus-mark-article-hook)
9237     (gnus-summary-select-article)
9238     (save-excursion
9239       (set-buffer gnus-original-article-buffer)
9240       (save-restriction
9241         (message-narrow-to-head)
9242         (let ((groups (nnmail-article-group 'identity trace)))
9243           (unless silent
9244             (if groups
9245                 (message "This message would go to %s"
9246                          (mapconcat 'car groups ", "))
9247               (message "This message would go to no groups"))
9248             groups))))))
9249
9250 (defun gnus-summary-respool-trace ()
9251   "Trace where the respool algorithm would put this article.
9252 Display a buffer showing all fancy splitting patterns which matched."
9253   (interactive)
9254   (gnus-summary-respool-query nil t))
9255
9256 ;; Summary marking commands.
9257
9258 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9259   "Mark articles which has the same subject as read, and then select the next.
9260 If UNMARK is positive, remove any kind of mark.
9261 If UNMARK is negative, tick articles."
9262   (interactive "P")
9263   (when unmark
9264     (setq unmark (prefix-numeric-value unmark)))
9265   (let ((count
9266          (gnus-summary-mark-same-subject
9267           (gnus-summary-article-subject) unmark)))
9268     ;; Select next unread article.  If auto-select-same mode, should
9269     ;; select the first unread article.
9270     (gnus-summary-next-article t (and gnus-auto-select-same
9271                                       (gnus-summary-article-subject)))
9272     (gnus-message 7 "%d article%s marked as %s"
9273                   count (if (= count 1) " is" "s are")
9274                   (if unmark "unread" "read"))))
9275
9276 (defun gnus-summary-kill-same-subject (&optional unmark)
9277   "Mark articles which has the same subject as read.
9278 If UNMARK is positive, remove any kind of mark.
9279 If UNMARK is negative, tick articles."
9280   (interactive "P")
9281   (when unmark
9282     (setq unmark (prefix-numeric-value unmark)))
9283   (let ((count
9284          (gnus-summary-mark-same-subject
9285           (gnus-summary-article-subject) unmark)))
9286     ;; If marked as read, go to next unread subject.
9287     (when (null unmark)
9288       ;; Go to next unread subject.
9289       (gnus-summary-next-subject 1 t))
9290     (gnus-message 7 "%d articles are marked as %s"
9291                   count (if unmark "unread" "read"))))
9292
9293 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9294   "Mark articles with same SUBJECT as read, and return marked number.
9295 If optional argument UNMARK is positive, remove any kinds of marks.
9296 If optional argument UNMARK is negative, mark articles as unread instead."
9297   (let ((count 1))
9298     (save-excursion
9299       (cond
9300        ((null unmark)                   ; Mark as read.
9301         (while (and
9302                 (progn
9303                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9304                   (gnus-summary-show-thread) t)
9305                 (gnus-summary-find-subject subject))
9306           (setq count (1+ count))))
9307        ((> unmark 0)                    ; Tick.
9308         (while (and
9309                 (progn
9310                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9311                   (gnus-summary-show-thread) t)
9312                 (gnus-summary-find-subject subject))
9313           (setq count (1+ count))))
9314        (t                               ; Mark as unread.
9315         (while (and
9316                 (progn
9317                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9318                   (gnus-summary-show-thread) t)
9319                 (gnus-summary-find-subject subject))
9320           (setq count (1+ count)))))
9321       (gnus-set-mode-line 'summary)
9322       ;; Return the number of marked articles.
9323       count)))
9324
9325 (defun gnus-summary-mark-as-processable (n &optional unmark)
9326   "Set the process mark on the next N articles.
9327 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9328 the process mark instead.  The difference between N and the actual
9329 number of articles marked is returned."
9330   (interactive "P")
9331   (if (and (null n) (gnus-region-active-p))
9332       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9333     (setq n (prefix-numeric-value n))
9334     (let ((backward (< n 0))
9335           (n (abs n)))
9336       (while (and
9337               (> n 0)
9338               (if unmark
9339                   (gnus-summary-remove-process-mark
9340                    (gnus-summary-article-number))
9341                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9342               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9343         (setq n (1- n)))
9344       (when (/= 0 n)
9345         (gnus-message 7 "No more articles"))
9346       (gnus-summary-recenter)
9347       (gnus-summary-position-point)
9348       n)))
9349
9350 (defun gnus-summary-unmark-as-processable (n)
9351   "Remove the process mark from the next N articles.
9352 If N is negative, unmark backward instead.  The difference between N and
9353 the actual number of articles unmarked is returned."
9354   (interactive "P")
9355   (gnus-summary-mark-as-processable n t))
9356
9357 (defun gnus-summary-unmark-all-processable ()
9358   "Remove the process mark from all articles."
9359   (interactive)
9360   (save-excursion
9361     (while gnus-newsgroup-processable
9362       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9363   (gnus-summary-position-point))
9364
9365 (defun gnus-summary-add-mark (article type)
9366   "Mark ARTICLE with a mark of TYPE."
9367   (let ((vtype (car (assq type gnus-article-mark-lists)))
9368         var)
9369     (if (not vtype)
9370         (error "No such mark type: %s" type)
9371       (setq var (intern (format "gnus-newsgroup-%s" type)))
9372       (set var (cons article (symbol-value var)))
9373       (if (memq type '(processable cached replied forwarded recent saved))
9374           (gnus-summary-update-secondary-mark article)
9375         ;;; !!! This is bogus.  We should find out what primary
9376         ;;; !!! mark we want to set.
9377         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9378
9379 (defun gnus-summary-mark-as-expirable (n)
9380   "Mark N articles forward as expirable.
9381 If N is negative, mark backward instead.  The difference between N and
9382 the actual number of articles marked is returned."
9383   (interactive "p")
9384   (gnus-summary-mark-forward n gnus-expirable-mark))
9385
9386 (defun gnus-summary-mark-as-spam (n)
9387   "Mark N articles forward as spam.
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-spam-mark))
9392
9393 (defun gnus-summary-mark-article-as-replied (article)
9394   "Mark ARTICLE as replied to and update the summary line.
9395 ARTICLE can also be a list of articles."
9396   (interactive (list (gnus-summary-article-number)))
9397   (let ((articles (if (listp article) article (list article))))
9398     (dolist (article articles)
9399       (push article gnus-newsgroup-replied)
9400       (let ((buffer-read-only nil))
9401         (when (gnus-summary-goto-subject article nil t)
9402           (gnus-summary-update-secondary-mark article))))))
9403
9404 (defun gnus-summary-mark-article-as-forwarded (article)
9405   "Mark ARTICLE as forwarded and update the summary line.
9406 ARTICLE can also be a list of articles."
9407   (let ((articles (if (listp article) article (list article))))
9408     (dolist (article articles)
9409       (push article gnus-newsgroup-forwarded)
9410       (let ((buffer-read-only nil))
9411         (when (gnus-summary-goto-subject article nil t)
9412           (gnus-summary-update-secondary-mark article))))))
9413
9414 (defun gnus-summary-set-bookmark (article)
9415   "Set a bookmark in current article."
9416   (interactive (list (gnus-summary-article-number)))
9417   (when (or (not (get-buffer gnus-article-buffer))
9418             (not gnus-current-article)
9419             (not gnus-article-current)
9420             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9421     (error "No current article selected"))
9422   ;; Remove old bookmark, if one exists.
9423   (gnus-pull article gnus-newsgroup-bookmarks)
9424   ;; Set the new bookmark, which is on the form
9425   ;; (article-number . line-number-in-body).
9426   (push
9427    (cons article
9428          (save-excursion
9429            (set-buffer gnus-article-buffer)
9430            (count-lines
9431             (min (point)
9432                  (save-excursion
9433                    (article-goto-body)
9434                    (point)))
9435             (point))))
9436    gnus-newsgroup-bookmarks)
9437   (gnus-message 6 "A bookmark has been added to the current article."))
9438
9439 (defun gnus-summary-remove-bookmark (article)
9440   "Remove the bookmark from the current article."
9441   (interactive (list (gnus-summary-article-number)))
9442   ;; Remove old bookmark, if one exists.
9443   (if (not (assq article gnus-newsgroup-bookmarks))
9444       (gnus-message 6 "No bookmark in current article.")
9445     (gnus-pull article gnus-newsgroup-bookmarks)
9446     (gnus-message 6 "Removed bookmark.")))
9447
9448 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9449 (defun gnus-summary-mark-as-dormant (n)
9450   "Mark N articles forward as dormant.
9451 If N is negative, mark backward instead.  The difference between N and
9452 the actual number of articles marked is returned."
9453   (interactive "p")
9454   (gnus-summary-mark-forward n gnus-dormant-mark))
9455
9456 (defun gnus-summary-set-process-mark (article)
9457   "Set the process mark on ARTICLE and update the summary line."
9458   (setq gnus-newsgroup-processable
9459         (cons article
9460               (delq article gnus-newsgroup-processable)))
9461   (when (gnus-summary-goto-subject article)
9462     (gnus-summary-show-thread)
9463     (gnus-summary-goto-subject article)
9464     (gnus-summary-update-secondary-mark article)))
9465
9466 (defun gnus-summary-remove-process-mark (article)
9467   "Remove the process mark from ARTICLE and update the summary line."
9468   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9469   (when (gnus-summary-goto-subject article)
9470     (gnus-summary-show-thread)
9471     (gnus-summary-goto-subject article)
9472     (gnus-summary-update-secondary-mark article)))
9473
9474 (defun gnus-summary-set-saved-mark (article)
9475   "Set the process mark on ARTICLE and update the summary line."
9476   (push article gnus-newsgroup-saved)
9477   (when (gnus-summary-goto-subject article)
9478     (gnus-summary-update-secondary-mark article)))
9479
9480 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9481   "Mark N articles as read forwards.
9482 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9483 The difference between N and the actual number of articles marked is
9484 returned.
9485 If NO-EXPIRE, auto-expiry will be inhibited."
9486   (interactive "p")
9487   (gnus-summary-show-thread)
9488   (let ((backward (< n 0))
9489         (gnus-summary-goto-unread
9490          (and gnus-summary-goto-unread
9491               (not (eq gnus-summary-goto-unread 'never))
9492               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9493                                     gnus-ticked-mark gnus-dormant-mark)))))
9494         (n (abs n))
9495         (mark (or mark gnus-del-mark)))
9496     (while (and (> n 0)
9497                 (gnus-summary-mark-article nil mark no-expire)
9498                 (zerop (gnus-summary-next-subject
9499                         (if backward -1 1)
9500                         (and gnus-summary-goto-unread
9501                              (not (eq gnus-summary-goto-unread 'never)))
9502                         t)))
9503       (setq n (1- n)))
9504     (when (/= 0 n)
9505       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9506     (gnus-summary-recenter)
9507     (gnus-summary-position-point)
9508     (gnus-set-mode-line 'summary)
9509     n))
9510
9511 (defun gnus-summary-mark-article-as-read (mark)
9512   "Mark the current article quickly as read with MARK."
9513   (let ((article (gnus-summary-article-number)))
9514     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9515     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9516     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9517     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9518     (push (cons article mark) gnus-newsgroup-reads)
9519     ;; Possibly remove from cache, if that is used.
9520     (when gnus-use-cache
9521       (gnus-cache-enter-remove-article article))
9522     ;; Allow the backend to change the mark.
9523     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9524     ;; Check for auto-expiry.
9525     (when (and gnus-newsgroup-auto-expire
9526                (memq mark gnus-auto-expirable-marks))
9527       (setq mark gnus-expirable-mark)
9528       ;; Let the backend know about the mark change.
9529       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9530       (push article gnus-newsgroup-expirable))
9531     ;; Set the mark in the buffer.
9532     (gnus-summary-update-mark mark 'unread)
9533     t))
9534
9535 (defun gnus-summary-mark-article-as-unread (mark)
9536   "Mark the current article quickly as unread with MARK."
9537   (let* ((article (gnus-summary-article-number))
9538          (old-mark (gnus-summary-article-mark article)))
9539     ;; Allow the backend to change the mark.
9540     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9541     (if (eq mark old-mark)
9542         t
9543       (if (<= article 0)
9544           (progn
9545             (gnus-error 1 "Can't mark negative article numbers")
9546             nil)
9547         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9548         (setq gnus-newsgroup-spam-marked
9549               (delq article gnus-newsgroup-spam-marked))
9550         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9551         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9552         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9553         (cond ((= mark gnus-ticked-mark)
9554                (setq gnus-newsgroup-marked
9555                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9556                                               article)))
9557               ((= mark gnus-spam-mark)
9558                (setq gnus-newsgroup-spam-marked
9559                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9560                                               article)))
9561               ((= mark gnus-dormant-mark)
9562                (setq gnus-newsgroup-dormant
9563                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9564                                               article)))
9565               (t
9566                (setq gnus-newsgroup-unreads
9567                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9568                                               article))))
9569         (gnus-pull article gnus-newsgroup-reads)
9570
9571         ;; See whether the article is to be put in the cache.
9572         (and gnus-use-cache
9573              (vectorp (gnus-summary-article-header article))
9574              (save-excursion
9575                (gnus-cache-possibly-enter-article
9576                 gnus-newsgroup-name article
9577                 (= mark gnus-ticked-mark)
9578                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9579
9580         ;; Fix the mark.
9581         (gnus-summary-update-mark mark 'unread)
9582         t))))
9583
9584 (defun gnus-summary-mark-article (&optional article mark no-expire)
9585   "Mark ARTICLE with MARK.  MARK can be any character.
9586 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9587 `??' (dormant) and `?E' (expirable).
9588 If MARK is nil, then the default character `?r' is used.
9589 If ARTICLE is nil, then the article on the current line will be
9590 marked.
9591 If NO-EXPIRE, auto-expiry will be inhibited."
9592   ;; The mark might be a string.
9593   (when (stringp mark)
9594     (setq mark (aref mark 0)))
9595   ;; If no mark is given, then we check auto-expiring.
9596   (when (null mark)
9597     (setq mark gnus-del-mark))
9598   (when (and (not no-expire)
9599              gnus-newsgroup-auto-expire
9600              (memq mark gnus-auto-expirable-marks))
9601     (setq mark gnus-expirable-mark))
9602   (let ((article (or article (gnus-summary-article-number)))
9603         (old-mark (gnus-summary-article-mark article)))
9604     ;; Allow the backend to change the mark.
9605     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9606     (if (eq mark old-mark)
9607         t
9608       (unless article
9609         (error "No article on current line"))
9610       (if (not (if (or (= mark gnus-unread-mark)
9611                        (= mark gnus-ticked-mark)
9612                        (= mark gnus-spam-mark)
9613                        (= mark gnus-dormant-mark))
9614                    (gnus-mark-article-as-unread article mark)
9615                  (gnus-mark-article-as-read article mark)))
9616           t
9617         ;; See whether the article is to be put in the cache.
9618         (and gnus-use-cache
9619              (not (= mark gnus-canceled-mark))
9620              (vectorp (gnus-summary-article-header article))
9621              (save-excursion
9622                (gnus-cache-possibly-enter-article
9623                 gnus-newsgroup-name article
9624                 (= mark gnus-ticked-mark)
9625                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9626
9627         (when (gnus-summary-goto-subject article nil t)
9628           (let ((buffer-read-only nil))
9629             (gnus-summary-show-thread)
9630             ;; Fix the mark.
9631             (gnus-summary-update-mark mark 'unread)
9632             t))))))
9633
9634 (defun gnus-summary-update-secondary-mark (article)
9635   "Update the secondary (read, process, cache) mark."
9636   (gnus-summary-update-mark
9637    (cond ((memq article gnus-newsgroup-processable)
9638           gnus-process-mark)
9639          ((memq article gnus-newsgroup-cached)
9640           gnus-cached-mark)
9641          ((memq article gnus-newsgroup-replied)
9642           gnus-replied-mark)
9643          ((memq article gnus-newsgroup-forwarded)
9644           gnus-forwarded-mark)
9645          ((memq article gnus-newsgroup-saved)
9646           gnus-saved-mark)
9647          ((memq article gnus-newsgroup-recent)
9648           gnus-recent-mark)
9649          ((memq article gnus-newsgroup-unseen)
9650           gnus-unseen-mark)
9651          (t gnus-no-mark))
9652    'replied)
9653   (when (gnus-visual-p 'summary-highlight 'highlight)
9654     (gnus-run-hooks 'gnus-summary-update-hook))
9655   t)
9656
9657 (defun gnus-summary-update-download-mark (article)
9658   "Update the secondary (read, process, cache) mark."
9659   (gnus-summary-update-mark
9660    (cond ((memq article gnus-newsgroup-undownloaded) 
9661           gnus-undownloaded-mark)
9662          (gnus-newsgroup-agentized
9663           gnus-downloaded-mark)
9664          (t
9665           gnus-no-mark))
9666    'download)
9667   (gnus-summary-update-line t)
9668   t)
9669
9670 (defun gnus-summary-update-mark (mark type)
9671   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9672         (buffer-read-only nil))
9673     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9674     (when forward
9675       (when (looking-at "\r")
9676         (incf forward))
9677       (when (<= (+ forward (point)) (point-max))
9678         ;; Go to the right position on the line.
9679         (goto-char (+ forward (point)))
9680         ;; Replace the old mark with the new mark.
9681         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9682         ;; Optionally update the marks by some user rule.
9683         (when (eq type 'unread)
9684           (gnus-data-set-mark
9685            (gnus-data-find (gnus-summary-article-number)) mark)
9686           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9687
9688 (defun gnus-mark-article-as-read (article &optional mark)
9689   "Enter ARTICLE in the pertinent lists and remove it from others."
9690   ;; Make the article expirable.
9691   (let ((mark (or mark gnus-del-mark)))
9692     (setq gnus-newsgroup-expirable
9693           (if (= mark gnus-expirable-mark)
9694               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9695             (delq article gnus-newsgroup-expirable)))
9696     ;; Remove from unread and marked lists.
9697     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9698     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9699     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9700     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9701     (push (cons article mark) gnus-newsgroup-reads)
9702     ;; Possibly remove from cache, if that is used.
9703     (when gnus-use-cache
9704       (gnus-cache-enter-remove-article article))
9705     t))
9706
9707 (defun gnus-mark-article-as-unread (article &optional mark)
9708   "Enter ARTICLE in the pertinent lists and remove it from others."
9709   (let ((mark (or mark gnus-ticked-mark)))
9710     (if (<= article 0)
9711         (progn
9712           (gnus-error 1 "Can't mark negative article numbers")
9713           nil)
9714       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9715             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9716             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9717             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9718             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9719
9720       ;; Unsuppress duplicates?
9721       (when gnus-suppress-duplicates
9722         (gnus-dup-unsuppress-article article))
9723
9724       (cond ((= mark gnus-ticked-mark)
9725              (setq gnus-newsgroup-marked
9726                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9727             ((= mark gnus-spam-mark)
9728              (setq gnus-newsgroup-spam-marked
9729                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9730                                             article)))
9731             ((= mark gnus-dormant-mark)
9732              (setq gnus-newsgroup-dormant
9733                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9734             (t
9735              (setq gnus-newsgroup-unreads
9736                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9737       (gnus-pull article gnus-newsgroup-reads)
9738       t)))
9739
9740 (defalias 'gnus-summary-mark-as-unread-forward
9741   'gnus-summary-tick-article-forward)
9742 (make-obsolete 'gnus-summary-mark-as-unread-forward
9743                'gnus-summary-tick-article-forward)
9744 (defun gnus-summary-tick-article-forward (n)
9745   "Tick N articles forwards.
9746 If N is negative, tick backwards instead.
9747 The difference between N and the number of articles ticked is returned."
9748   (interactive "p")
9749   (gnus-summary-mark-forward n gnus-ticked-mark))
9750
9751 (defalias 'gnus-summary-mark-as-unread-backward
9752   'gnus-summary-tick-article-backward)
9753 (make-obsolete 'gnus-summary-mark-as-unread-backward
9754                'gnus-summary-tick-article-backward)
9755 (defun gnus-summary-tick-article-backward (n)
9756   "Tick N articles backwards.
9757 The difference between N and the number of articles ticked is returned."
9758   (interactive "p")
9759   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9760
9761 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9762 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9763 (defun gnus-summary-tick-article (&optional article clear-mark)
9764   "Mark current article as unread.
9765 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9766 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9767   (interactive)
9768   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9769                                        gnus-ticked-mark)))
9770
9771 (defun gnus-summary-mark-as-read-forward (n)
9772   "Mark N articles as read forwards.
9773 If N is negative, mark backwards instead.
9774 The difference between N and the actual number of articles marked is
9775 returned."
9776   (interactive "p")
9777   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9778
9779 (defun gnus-summary-mark-as-read-backward (n)
9780   "Mark the N articles as read backwards.
9781 The difference between N and the actual number of articles marked is
9782 returned."
9783   (interactive "p")
9784   (gnus-summary-mark-forward
9785    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9786
9787 (defun gnus-summary-mark-as-read (&optional article mark)
9788   "Mark current article as read.
9789 ARTICLE specifies the article to be marked as read.
9790 MARK specifies a string to be inserted at the beginning of the line."
9791   (gnus-summary-mark-article article mark))
9792
9793 (defun gnus-summary-clear-mark-forward (n)
9794   "Clear marks from N articles forward.
9795 If N is negative, clear backward instead.
9796 The difference between N and the number of marks cleared is returned."
9797   (interactive "p")
9798   (gnus-summary-mark-forward n gnus-unread-mark))
9799
9800 (defun gnus-summary-clear-mark-backward (n)
9801   "Clear marks from N articles backward.
9802 The difference between N and the number of marks cleared is returned."
9803   (interactive "p")
9804   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9805
9806 (defun gnus-summary-mark-unread-as-read ()
9807   "Intended to be used by `gnus-summary-mark-article-hook'."
9808   (when (memq gnus-current-article gnus-newsgroup-unreads)
9809     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9810
9811 (defun gnus-summary-mark-read-and-unread-as-read ()
9812   "Intended to be used by `gnus-summary-mark-article-hook'."
9813   (let ((mark (gnus-summary-article-mark)))
9814     (when (or (gnus-unread-mark-p mark)
9815               (gnus-read-mark-p mark))
9816       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9817
9818 (defun gnus-summary-mark-unread-as-ticked ()
9819   "Intended to be used by `gnus-summary-mark-article-hook'."
9820   (when (memq gnus-current-article gnus-newsgroup-unreads)
9821     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9822
9823 (defun gnus-summary-mark-region-as-read (point mark all)
9824   "Mark all unread articles between point and mark as read.
9825 If given a prefix, mark all articles between point and mark as read,
9826 even ticked and dormant ones."
9827   (interactive "r\nP")
9828   (save-excursion
9829     (let (article)
9830       (goto-char point)
9831       (beginning-of-line)
9832       (while (and
9833               (< (point) mark)
9834               (progn
9835                 (when (or all
9836                           (memq (setq article (gnus-summary-article-number))
9837                                 gnus-newsgroup-unreads))
9838                   (gnus-summary-mark-article article gnus-del-mark))
9839                 t)
9840               (gnus-summary-find-next))))))
9841
9842 (defun gnus-summary-mark-below (score mark)
9843   "Mark articles with score less than SCORE with MARK."
9844   (interactive "P\ncMark: ")
9845   (setq score (if score
9846                   (prefix-numeric-value score)
9847                 (or gnus-summary-default-score 0)))
9848   (save-excursion
9849     (set-buffer gnus-summary-buffer)
9850     (goto-char (point-min))
9851     (while
9852         (progn
9853           (and (< (gnus-summary-article-score) score)
9854                (gnus-summary-mark-article nil mark))
9855           (gnus-summary-find-next)))))
9856
9857 (defun gnus-summary-kill-below (&optional score)
9858   "Mark articles with score below SCORE as read."
9859   (interactive "P")
9860   (gnus-summary-mark-below score gnus-killed-mark))
9861
9862 (defun gnus-summary-clear-above (&optional score)
9863   "Clear all marks from articles with score above SCORE."
9864   (interactive "P")
9865   (gnus-summary-mark-above score gnus-unread-mark))
9866
9867 (defun gnus-summary-tick-above (&optional score)
9868   "Tick all articles with score above SCORE."
9869   (interactive "P")
9870   (gnus-summary-mark-above score gnus-ticked-mark))
9871
9872 (defun gnus-summary-mark-above (score mark)
9873   "Mark articles with score over SCORE with MARK."
9874   (interactive "P\ncMark: ")
9875   (setq score (if score
9876                   (prefix-numeric-value score)
9877                 (or gnus-summary-default-score 0)))
9878   (save-excursion
9879     (set-buffer gnus-summary-buffer)
9880     (goto-char (point-min))
9881     (while (and (progn
9882                   (when (> (gnus-summary-article-score) score)
9883                     (gnus-summary-mark-article nil mark))
9884                   t)
9885                 (gnus-summary-find-next)))))
9886
9887 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9888 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9889 (defun gnus-summary-limit-include-expunged (&optional no-error)
9890   "Display all the hidden articles that were expunged for low scores."
9891   (interactive)
9892   (let ((buffer-read-only nil))
9893     (let ((scored gnus-newsgroup-scored)
9894           headers h)
9895       (while scored
9896         (unless (gnus-summary-article-header (caar scored))
9897           (and (setq h (gnus-number-to-header (caar scored)))
9898                (< (cdar scored) gnus-summary-expunge-below)
9899                (push h headers)))
9900         (setq scored (cdr scored)))
9901       (if (not headers)
9902           (when (not no-error)
9903             (error "No expunged articles hidden"))
9904         (goto-char (point-min))
9905         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9906         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9907         (mapcar (lambda (x) (push (mail-header-number x)
9908                                   gnus-newsgroup-limit))
9909                 headers)
9910         (gnus-summary-prepare-unthreaded (nreverse headers))
9911         (goto-char (point-min))
9912         (gnus-summary-position-point)
9913         t))))
9914
9915 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9916   "Mark all unread articles in this newsgroup as read.
9917 If prefix argument ALL is non-nil, ticked and dormant articles will
9918 also be marked as read.
9919 If QUIETLY is non-nil, no questions will be asked.
9920 If TO-HERE is non-nil, it should be a point in the buffer.  All
9921 articles before (after, if REVERSE is set) this point will be marked as read.
9922 Note that this function will only catch up the unread article
9923 in the current summary buffer limitation.
9924 The number of articles marked as read is returned."
9925   (interactive "P")
9926   (prog1
9927       (save-excursion
9928         (when (or quietly
9929                   (not gnus-interactive-catchup) ;Without confirmation?
9930                   gnus-expert-user
9931                   (gnus-y-or-n-p
9932                    (if all
9933                        "Mark absolutely all articles as read? "
9934                      "Mark all unread articles as read? ")))
9935           (if (and not-mark
9936                    (not gnus-newsgroup-adaptive)
9937                    (not gnus-newsgroup-auto-expire)
9938                    (not gnus-suppress-duplicates)
9939                    (or (not gnus-use-cache)
9940                        (eq gnus-use-cache 'passive)))
9941               (progn
9942                 (when all
9943                   (setq gnus-newsgroup-marked nil
9944                         gnus-newsgroup-spam-marked nil
9945                         gnus-newsgroup-dormant nil))
9946                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9947             ;; We actually mark all articles as canceled, which we
9948             ;; have to do when using auto-expiry or adaptive scoring.
9949             (gnus-summary-show-all-threads)
9950             (if (and to-here reverse)
9951                 (progn
9952                   (goto-char to-here)
9953                   (while (and
9954                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9955                           (gnus-summary-find-next (not all) nil nil t))))
9956               (when (gnus-summary-first-subject (not all) t)
9957                 (while (and
9958                         (if to-here (< (point) to-here) t)
9959                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9960                         (gnus-summary-find-next (not all) nil nil t)))))
9961             (gnus-set-mode-line 'summary))
9962           t))
9963     (gnus-summary-position-point)))
9964
9965 (defun gnus-summary-catchup-to-here (&optional all)
9966   "Mark all unticked articles before the current one as read.
9967 If ALL is non-nil, also mark ticked and dormant articles as read."
9968   (interactive "P")
9969   (save-excursion
9970     (gnus-save-hidden-threads
9971       (let ((beg (point)))
9972         ;; We check that there are unread articles.
9973         (when (or all (gnus-summary-find-prev))
9974           (gnus-summary-catchup all t beg)))))
9975   (gnus-summary-position-point))
9976
9977 (defun gnus-summary-catchup-from-here (&optional all)
9978   "Mark all unticked articles after the current one as read.
9979 If ALL is non-nil, also mark ticked and dormant articles as read."
9980   (interactive "P")
9981   (save-excursion
9982     (gnus-save-hidden-threads
9983       (let ((beg (point)))
9984         ;; We check that there are unread articles.
9985         (when (or all (gnus-summary-find-next))
9986           (gnus-summary-catchup all t beg nil t)))))
9987
9988   (gnus-summary-position-point))
9989 (defun gnus-summary-catchup-all (&optional quietly)
9990   "Mark all articles in this newsgroup as read.
9991 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9992 instead, which marks only unread articles as read."
9993   (interactive "P")
9994   (gnus-summary-catchup t quietly))
9995
9996 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9997   "Mark all unread articles in this group as read, then exit.
9998 If prefix argument ALL is non-nil, all articles are marked as read.
9999 If QUIETLY is non-nil, no questions will be asked."
10000   (interactive "P")
10001   (when (gnus-summary-catchup all quietly nil 'fast)
10002     ;; Select next newsgroup or exit.
10003     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10004              (eq gnus-auto-select-next 'quietly))
10005         (gnus-summary-next-group nil)
10006       (gnus-summary-exit))))
10007
10008 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10009   "Mark all articles in this newsgroup as read, and then exit.
10010 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10011 instead, which marks only unread articles as read."
10012   (interactive "P")
10013   (gnus-summary-catchup-and-exit t quietly))
10014
10015 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10016   "Mark all articles in this group as read and select the next group.
10017 If given a prefix, mark all articles, unread as well as ticked, as
10018 read."
10019   (interactive "P")
10020   (save-excursion
10021     (gnus-summary-catchup all))
10022   (gnus-summary-next-group))
10023
10024 ;;;
10025 ;;; with article
10026 ;;;
10027
10028 (defmacro gnus-with-article (article &rest forms)
10029   "Select ARTICLE and perform FORMS in the original article buffer.
10030 Then replace the article with the result."
10031   `(progn
10032      ;; We don't want the article to be marked as read.
10033      (let (gnus-mark-article-hook)
10034        (gnus-summary-select-article t t nil ,article))
10035      (set-buffer gnus-original-article-buffer)
10036      ,@forms
10037      (if (not (gnus-check-backend-function
10038                'request-replace-article (car gnus-article-current)))
10039          (gnus-message 5 "Read-only group; not replacing")
10040        (unless (gnus-request-replace-article
10041                 ,article (car gnus-article-current)
10042                 (current-buffer) t)
10043          (error "Couldn't replace article")))
10044      ;; The cache and backlog have to be flushed somewhat.
10045      (when gnus-keep-backlog
10046        (gnus-backlog-remove-article
10047         (car gnus-article-current) (cdr gnus-article-current)))
10048      (when gnus-use-cache
10049        (gnus-cache-update-article
10050         (car gnus-article-current) (cdr gnus-article-current)))))
10051
10052 (put 'gnus-with-article 'lisp-indent-function 1)
10053 (put 'gnus-with-article 'edebug-form-spec '(form body))
10054
10055 ;; Thread-based commands.
10056
10057 (defun gnus-summary-articles-in-thread (&optional article)
10058   "Return a list of all articles in the current thread.
10059 If ARTICLE is non-nil, return all articles in the thread that starts
10060 with that article."
10061   (let* ((article (or article (gnus-summary-article-number)))
10062          (data (gnus-data-find-list article))
10063          (top-level (gnus-data-level (car data)))
10064          (top-subject
10065           (cond ((null gnus-thread-operation-ignore-subject)
10066                  (gnus-simplify-subject-re
10067                   (mail-header-subject (gnus-data-header (car data)))))
10068                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10069                  (gnus-simplify-subject-fuzzy
10070                   (mail-header-subject (gnus-data-header (car data)))))
10071                 (t nil)))
10072          (end-point (save-excursion
10073                       (if (gnus-summary-go-to-next-thread)
10074                           (point) (point-max))))
10075          articles)
10076     (while (and data
10077                 (< (gnus-data-pos (car data)) end-point))
10078       (when (or (not top-subject)
10079                 (string= top-subject
10080                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10081                              (gnus-simplify-subject-fuzzy
10082                               (mail-header-subject
10083                                (gnus-data-header (car data))))
10084                            (gnus-simplify-subject-re
10085                             (mail-header-subject
10086                              (gnus-data-header (car data)))))))
10087         (push (gnus-data-number (car data)) articles))
10088       (unless (and (setq data (cdr data))
10089                    (> (gnus-data-level (car data)) top-level))
10090         (setq data nil)))
10091     ;; Return the list of articles.
10092     (nreverse articles)))
10093
10094 (defun gnus-summary-rethread-current ()
10095   "Rethread the thread the current article is part of."
10096   (interactive)
10097   (let* ((gnus-show-threads t)
10098          (article (gnus-summary-article-number))
10099          (id (mail-header-id (gnus-summary-article-header)))
10100          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10101     (unless id
10102       (error "No article on the current line"))
10103     (gnus-rebuild-thread id)
10104     (gnus-summary-goto-subject article)))
10105
10106 (defun gnus-summary-reparent-thread ()
10107   "Make the current article child of the marked (or previous) article.
10108
10109 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10110 is non-nil or the Subject: of both articles are the same."
10111   (interactive)
10112   (unless (not (gnus-group-read-only-p))
10113     (error "The current newsgroup does not support article editing"))
10114   (unless (<= (length gnus-newsgroup-processable) 1)
10115     (error "No more than one article may be marked"))
10116   (save-window-excursion
10117     (let ((gnus-article-buffer " *reparent*")
10118           (current-article (gnus-summary-article-number))
10119           ;; First grab the marked article, otherwise one line up.
10120           (parent-article (if (not (null gnus-newsgroup-processable))
10121                               (car gnus-newsgroup-processable)
10122                             (save-excursion
10123                               (if (eq (forward-line -1) 0)
10124                                   (gnus-summary-article-number)
10125                                 (error "Beginning of summary buffer"))))))
10126       (unless (not (eq current-article parent-article))
10127         (error "An article may not be self-referential"))
10128       (let ((message-id (mail-header-id
10129                          (gnus-summary-article-header parent-article))))
10130         (unless (and message-id (not (equal message-id "")))
10131           (error "No message-id in desired parent"))
10132         (gnus-with-article current-article
10133           (save-restriction
10134             (goto-char (point-min))
10135             (message-narrow-to-head)
10136             (if (re-search-forward "^References: " nil t)
10137                 (progn
10138                   (re-search-forward "^[^ \t]" nil t)
10139                   (forward-line -1)
10140                   (end-of-line)
10141                   (insert " " message-id))
10142               (insert "References: " message-id "\n"))))
10143         (set-buffer gnus-summary-buffer)
10144         (gnus-summary-unmark-all-processable)
10145         (gnus-summary-update-article current-article)
10146         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10147             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10148         (gnus-summary-rethread-current)
10149         (gnus-message 3 "Article %d is now the child of article %d"
10150                       current-article parent-article)))))
10151
10152 (defun gnus-summary-toggle-threads (&optional arg)
10153   "Toggle showing conversation threads.
10154 If ARG is positive number, turn showing conversation threads on."
10155   (interactive "P")
10156   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10157     (setq gnus-show-threads
10158           (if (null arg) (not gnus-show-threads)
10159             (> (prefix-numeric-value arg) 0)))
10160     (gnus-summary-prepare)
10161     (gnus-summary-goto-subject current)
10162     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10163     (gnus-summary-position-point)))
10164
10165 (defun gnus-summary-show-all-threads ()
10166   "Show all threads."
10167   (interactive)
10168   (save-excursion
10169     (let ((buffer-read-only nil))
10170       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10171   (gnus-summary-position-point))
10172
10173 (defun gnus-summary-show-thread ()
10174   "Show thread subtrees.
10175 Returns nil if no thread was there to be shown."
10176   (interactive)
10177   (let ((buffer-read-only nil)
10178         (orig (point))
10179         ;; first goto end then to beg, to have point at beg after let
10180         (end (progn (end-of-line) (point)))
10181         (beg (progn (beginning-of-line) (point))))
10182     (prog1
10183         ;; Any hidden lines here?
10184         (search-forward "\r" end t)
10185       (subst-char-in-region beg end ?\^M ?\n t)
10186       (goto-char orig)
10187       (gnus-summary-position-point))))
10188
10189 (defun gnus-summary-maybe-hide-threads ()
10190   "If requested, hide the threads that should be hidden."
10191   (when (and gnus-show-threads
10192              gnus-thread-hide-subtree)
10193     (gnus-summary-hide-all-threads
10194      (if (or (consp gnus-thread-hide-subtree)
10195              (gnus-functionp gnus-thread-hide-subtree))
10196          (gnus-make-predicate gnus-thread-hide-subtree)
10197        nil))))
10198
10199 ;;; Hiding predicates.
10200
10201 (defun gnus-article-unread-p (header)
10202   (memq (mail-header-number header) gnus-newsgroup-unreads))
10203
10204 (defun gnus-article-unseen-p (header)
10205   (memq (mail-header-number header) gnus-newsgroup-unseen))
10206
10207 (defun gnus-map-articles (predicate articles)
10208   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10209   (apply 'gnus-or (mapcar predicate
10210                           (mapcar 'gnus-summary-article-header articles))))
10211
10212 (defun gnus-summary-hide-all-threads (&optional predicate)
10213   "Hide all thread subtrees.
10214 If PREDICATE is supplied, threads that satisfy this predicate
10215 will not be hidden."
10216   (interactive)
10217   (save-excursion
10218     (goto-char (point-min))
10219     (let ((end nil))
10220       (while (not end)
10221         (when (or (not predicate)
10222                   (gnus-map-articles
10223                    predicate (gnus-summary-article-children)))
10224             (gnus-summary-hide-thread))
10225         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10226   (gnus-summary-position-point))
10227
10228 (defun gnus-summary-hide-thread ()
10229   "Hide thread subtrees.
10230 If PREDICATE is supplied, threads that satisfy this predicate
10231 will not be hidden.
10232 Returns nil if no threads were there to be hidden."
10233   (interactive)
10234   (let ((buffer-read-only nil)
10235         (start (point))
10236         (article (gnus-summary-article-number)))
10237     (goto-char start)
10238     ;; Go forward until either the buffer ends or the subthread
10239     ;; ends.
10240     (when (and (not (eobp))
10241                (or (zerop (gnus-summary-next-thread 1 t))
10242                    (goto-char (point-max))))
10243       (prog1
10244           (if (and (> (point) start)
10245                    (search-backward "\n" start t))
10246               (progn
10247                 (subst-char-in-region start (point) ?\n ?\^M)
10248                 (gnus-summary-goto-subject article))
10249             (goto-char start)
10250             nil)))))
10251
10252 (defun gnus-summary-go-to-next-thread (&optional previous)
10253   "Go to the same level (or less) next thread.
10254 If PREVIOUS is non-nil, go to previous thread instead.
10255 Return the article number moved to, or nil if moving was impossible."
10256   (let ((level (gnus-summary-thread-level))
10257         (way (if previous -1 1))
10258         (beg (point)))
10259     (forward-line way)
10260     (while (and (not (eobp))
10261                 (< level (gnus-summary-thread-level)))
10262       (forward-line way))
10263     (if (eobp)
10264         (progn
10265           (goto-char beg)
10266           nil)
10267       (setq beg (point))
10268       (prog1
10269           (gnus-summary-article-number)
10270         (goto-char beg)))))
10271
10272 (defun gnus-summary-next-thread (n &optional silent)
10273   "Go to the same level next N'th thread.
10274 If N is negative, search backward instead.
10275 Returns the difference between N and the number of skips actually
10276 done.
10277
10278 If SILENT, don't output messages."
10279   (interactive "p")
10280   (let ((backward (< n 0))
10281         (n (abs n)))
10282     (while (and (> n 0)
10283                 (gnus-summary-go-to-next-thread backward))
10284       (decf n))
10285     (unless silent
10286       (gnus-summary-position-point))
10287     (when (and (not silent) (/= 0 n))
10288       (gnus-message 7 "No more threads"))
10289     n))
10290
10291 (defun gnus-summary-prev-thread (n)
10292   "Go to the same level previous N'th thread.
10293 Returns the difference between N and the number of skips actually
10294 done."
10295   (interactive "p")
10296   (gnus-summary-next-thread (- n)))
10297
10298 (defun gnus-summary-go-down-thread ()
10299   "Go down one level in the current thread."
10300   (let ((children (gnus-summary-article-children)))
10301     (when children
10302       (gnus-summary-goto-subject (car children)))))
10303
10304 (defun gnus-summary-go-up-thread ()
10305   "Go up one level in the current thread."
10306   (let ((parent (gnus-summary-article-parent)))
10307     (when parent
10308       (gnus-summary-goto-subject parent))))
10309
10310 (defun gnus-summary-down-thread (n)
10311   "Go down thread N steps.
10312 If N is negative, go up instead.
10313 Returns the difference between N and how many steps down that were
10314 taken."
10315   (interactive "p")
10316   (let ((up (< n 0))
10317         (n (abs n)))
10318     (while (and (> n 0)
10319                 (if up (gnus-summary-go-up-thread)
10320                   (gnus-summary-go-down-thread)))
10321       (setq n (1- n)))
10322     (gnus-summary-position-point)
10323     (when (/= 0 n)
10324       (gnus-message 7 "Can't go further"))
10325     n))
10326
10327 (defun gnus-summary-up-thread (n)
10328   "Go up thread N steps.
10329 If N is negative, go down instead.
10330 Returns the difference between N and how many steps down that were
10331 taken."
10332   (interactive "p")
10333   (gnus-summary-down-thread (- n)))
10334
10335 (defun gnus-summary-top-thread ()
10336   "Go to the top of the thread."
10337   (interactive)
10338   (while (gnus-summary-go-up-thread))
10339   (gnus-summary-article-number))
10340
10341 (defun gnus-summary-kill-thread (&optional unmark)
10342   "Mark articles under current thread as read.
10343 If the prefix argument is positive, remove any kinds of marks.
10344 If the prefix argument is negative, tick articles instead."
10345   (interactive "P")
10346   (when unmark
10347     (setq unmark (prefix-numeric-value unmark)))
10348   (let ((articles (gnus-summary-articles-in-thread)))
10349     (save-excursion
10350       ;; Expand the thread.
10351       (gnus-summary-show-thread)
10352       ;; Mark all the articles.
10353       (while articles
10354         (gnus-summary-goto-subject (car articles))
10355         (cond ((null unmark)
10356                (gnus-summary-mark-article-as-read gnus-killed-mark))
10357               ((> unmark 0)
10358                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10359               (t
10360                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10361         (setq articles (cdr articles))))
10362     ;; Hide killed subtrees.
10363     (and (null unmark)
10364          gnus-thread-hide-killed
10365          (gnus-summary-hide-thread))
10366     ;; If marked as read, go to next unread subject.
10367     (when (null unmark)
10368       ;; Go to next unread subject.
10369       (gnus-summary-next-subject 1 t)))
10370   (gnus-set-mode-line 'summary))
10371
10372 ;; Summary sorting commands
10373
10374 (defun gnus-summary-sort-by-number (&optional reverse)
10375   "Sort the summary buffer by article number.
10376 Argument REVERSE means reverse order."
10377   (interactive "P")
10378   (gnus-summary-sort 'number reverse))
10379
10380 (defun gnus-summary-sort-by-random (&optional reverse)
10381   "Randomize the order in the summary buffer.
10382 Argument REVERSE means to randomize in reverse order."
10383   (interactive "P")
10384   (gnus-summary-sort 'random reverse))
10385
10386 (defun gnus-summary-sort-by-author (&optional reverse)
10387   "Sort the summary buffer by author name alphabetically.
10388 If `case-fold-search' is non-nil, case of letters is ignored.
10389 Argument REVERSE means reverse order."
10390   (interactive "P")
10391   (gnus-summary-sort 'author reverse))
10392
10393 (defun gnus-summary-sort-by-subject (&optional reverse)
10394   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
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 'subject reverse))
10399
10400 (defun gnus-summary-sort-by-date (&optional reverse)
10401   "Sort the summary buffer by date.
10402 Argument REVERSE means reverse order."
10403   (interactive "P")
10404   (gnus-summary-sort 'date reverse))
10405
10406 (defun gnus-summary-sort-by-score (&optional reverse)
10407   "Sort the summary buffer by score.
10408 Argument REVERSE means reverse order."
10409   (interactive "P")
10410   (gnus-summary-sort 'score reverse))
10411
10412 (defun gnus-summary-sort-by-lines (&optional reverse)
10413   "Sort the summary buffer by the number of lines.
10414 Argument REVERSE means reverse order."
10415   (interactive "P")
10416   (gnus-summary-sort 'lines reverse))
10417
10418 (defun gnus-summary-sort-by-chars (&optional reverse)
10419   "Sort the summary buffer by article length.
10420 Argument REVERSE means reverse order."
10421   (interactive "P")
10422   (gnus-summary-sort 'chars reverse))
10423
10424 (defun gnus-summary-sort-by-original (&optional reverse)
10425   "Sort the summary buffer using the default sorting method.
10426 Argument REVERSE means reverse order."
10427   (interactive "P")
10428   (let* ((buffer-read-only)
10429          (gnus-summary-prepare-hook nil))
10430     ;; We do the sorting by regenerating the threads.
10431     (gnus-summary-prepare)
10432     ;; Hide subthreads if needed.
10433     (gnus-summary-maybe-hide-threads)))
10434
10435 (defun gnus-summary-sort (predicate reverse)
10436   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10437   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10438          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10439          (gnus-thread-sort-functions
10440           (if (not reverse)
10441               thread
10442             `(lambda (t1 t2)
10443                (,thread t2 t1))))
10444          (gnus-sort-gathered-threads-function
10445           gnus-thread-sort-functions)
10446          (gnus-article-sort-functions
10447           (if (not reverse)
10448               article
10449             `(lambda (t1 t2)
10450                (,article t2 t1))))
10451          (buffer-read-only)
10452          (gnus-summary-prepare-hook nil))
10453     ;; We do the sorting by regenerating the threads.
10454     (gnus-summary-prepare)
10455     ;; Hide subthreads if needed.
10456     (gnus-summary-maybe-hide-threads)))
10457
10458 ;; Summary saving commands.
10459
10460 (defun gnus-summary-save-article (&optional n not-saved)
10461   "Save the current article using the default saver function.
10462 If N is a positive number, save the N next articles.
10463 If N is a negative number, save the N previous articles.
10464 If N is nil and any articles have been marked with the process mark,
10465 save those articles instead.
10466 The variable `gnus-default-article-saver' specifies the saver function."
10467   (interactive "P")
10468   (let* ((articles (gnus-summary-work-articles n))
10469          (save-buffer (save-excursion
10470                         (nnheader-set-temp-buffer " *Gnus Save*")))
10471          (num (length articles))
10472          header file)
10473     (dolist (article articles)
10474       (setq header (gnus-summary-article-header article))
10475       (if (not (vectorp header))
10476           ;; This is a pseudo-article.
10477           (if (assq 'name header)
10478               (gnus-copy-file (cdr (assq 'name header)))
10479             (gnus-message 1 "Article %d is unsaveable" article))
10480         ;; This is a real article.
10481         (save-window-excursion
10482           (let ((gnus-display-mime-function nil)
10483                 (gnus-article-prepare-hook nil))
10484             (gnus-summary-select-article t nil nil article)))
10485         (save-excursion
10486           (set-buffer save-buffer)
10487           (erase-buffer)
10488           (insert-buffer-substring gnus-original-article-buffer))
10489         (setq file (gnus-article-save save-buffer file num))
10490         (gnus-summary-remove-process-mark article)
10491         (unless not-saved
10492           (gnus-summary-set-saved-mark article))))
10493     (gnus-kill-buffer save-buffer)
10494     (gnus-summary-position-point)
10495     (gnus-set-mode-line 'summary)
10496     n))
10497
10498 (defun gnus-summary-pipe-output (&optional arg headers)
10499   "Pipe the current article to a subprocess.
10500 If N is a positive number, pipe the N next articles.
10501 If N is a negative number, pipe the N previous articles.
10502 If N is nil and any articles have been marked with the process mark,
10503 pipe those articles instead.
10504 If HEADERS (the symbolic prefix), include the headers, too."
10505   (interactive (gnus-interactive "P\ny"))
10506   (require 'gnus-art)
10507   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10508         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10509     (gnus-summary-save-article arg t))
10510   (let ((buffer (get-buffer "*Shell Command Output*")))
10511     (when (and buffer
10512                (not (zerop (buffer-size buffer))))
10513       (gnus-configure-windows 'pipe))))
10514
10515 (defun gnus-summary-save-article-mail (&optional arg)
10516   "Append the current article to an mail file.
10517 If N is a positive number, save the N next articles.
10518 If N is a negative number, save the N previous articles.
10519 If N is nil and any articles have been marked with the process mark,
10520 save those articles instead."
10521   (interactive "P")
10522   (require 'gnus-art)
10523   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10524     (gnus-summary-save-article arg)))
10525
10526 (defun gnus-summary-save-article-rmail (&optional arg)
10527   "Append the current article to an rmail file.
10528 If N is a positive number, save the N next articles.
10529 If N is a negative number, save the N previous articles.
10530 If N is nil and any articles have been marked with the process mark,
10531 save those articles instead."
10532   (interactive "P")
10533   (require 'gnus-art)
10534   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10535     (gnus-summary-save-article arg)))
10536
10537 (defun gnus-summary-save-article-file (&optional arg)
10538   "Append the current article to a file.
10539 If N is a positive number, save the N next articles.
10540 If N is a negative number, save the N previous articles.
10541 If N is nil and any articles have been marked with the process mark,
10542 save those articles instead."
10543   (interactive "P")
10544   (require 'gnus-art)
10545   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10546     (gnus-summary-save-article arg)))
10547
10548 (defun gnus-summary-write-article-file (&optional arg)
10549   "Write the current article to a file, deleting the previous file.
10550 If N is a positive number, save the N next articles.
10551 If N is a negative number, save the N previous articles.
10552 If N is nil and any articles have been marked with the process mark,
10553 save those articles instead."
10554   (interactive "P")
10555   (require 'gnus-art)
10556   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10557     (gnus-summary-save-article arg)))
10558
10559 (defun gnus-summary-save-article-body-file (&optional arg)
10560   "Append the current article body to a file.
10561 If N is a positive number, save the N next articles.
10562 If N is a negative number, save the N previous articles.
10563 If N is nil and any articles have been marked with the process mark,
10564 save those articles instead."
10565   (interactive "P")
10566   (require 'gnus-art)
10567   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10568     (gnus-summary-save-article arg)))
10569
10570 (defun gnus-summary-muttprint (&optional arg)
10571   "Print the current article using Muttprint.
10572 If N is a positive number, save the N next articles.
10573 If N is a negative number, save the N previous articles.
10574 If N is nil and any articles have been marked with the process mark,
10575 save those articles instead."
10576   (interactive "P")
10577   (require 'gnus-art)
10578   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10579     (gnus-summary-save-article arg t)))
10580
10581 (defun gnus-summary-pipe-message (program)
10582   "Pipe the current article through PROGRAM."
10583   (interactive "sProgram: ")
10584   (gnus-summary-select-article)
10585   (let ((mail-header-separator ""))
10586     (gnus-eval-in-buffer-window gnus-article-buffer
10587       (save-restriction
10588         (widen)
10589         (let ((start (window-start))
10590               buffer-read-only)
10591           (message-pipe-buffer-body program)
10592           (set-window-start (get-buffer-window (current-buffer)) start))))))
10593
10594 (defun gnus-get-split-value (methods)
10595   "Return a value based on the split METHODS."
10596   (let (split-name method result match)
10597     (when methods
10598       (save-excursion
10599         (set-buffer gnus-original-article-buffer)
10600         (save-restriction
10601           (nnheader-narrow-to-headers)
10602           (while (and methods (not split-name))
10603             (goto-char (point-min))
10604             (setq method (pop methods))
10605             (setq match (car method))
10606             (when (cond
10607                    ((stringp match)
10608                     ;; Regular expression.
10609                     (ignore-errors
10610                       (re-search-forward match nil t)))
10611                    ((gnus-functionp match)
10612                     ;; Function.
10613                     (save-restriction
10614                       (widen)
10615                       (setq result (funcall match gnus-newsgroup-name))))
10616                    ((consp match)
10617                     ;; Form.
10618                     (save-restriction
10619                       (widen)
10620                       (setq result (eval match)))))
10621               (setq split-name (cdr method))
10622               (cond ((stringp result)
10623                      (push (expand-file-name
10624                             result gnus-article-save-directory)
10625                            split-name))
10626                     ((consp result)
10627                      (setq split-name (append result split-name)))))))))
10628     (nreverse split-name)))
10629
10630 (defun gnus-valid-move-group-p (group)
10631   (and (boundp group)
10632        (symbol-name group)
10633        (symbol-value group)
10634        (gnus-get-function (gnus-find-method-for-group
10635                            (symbol-name group)) 'request-accept-article t)))
10636
10637 (defun gnus-read-move-group-name (prompt default articles prefix)
10638   "Read a group name."
10639   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10640          (minibuffer-confirm-incomplete nil) ; XEmacs
10641          (prom
10642           (format "%s %s to:"
10643                   prompt
10644                   (if (> (length articles) 1)
10645                       (format "these %d articles" (length articles))
10646                     "this article")))
10647          (to-newsgroup
10648           (cond
10649            ((null split-name)
10650             (gnus-completing-read-with-default
10651              default prom
10652              gnus-active-hashtb
10653              'gnus-valid-move-group-p
10654              nil prefix
10655              'gnus-group-history))
10656            ((= 1 (length split-name))
10657             (gnus-completing-read-with-default
10658              (car split-name) prom
10659              gnus-active-hashtb
10660              'gnus-valid-move-group-p
10661              nil nil
10662              'gnus-group-history))
10663            (t
10664             (gnus-completing-read-with-default
10665              nil prom
10666              (mapcar (lambda (el) (list el))
10667                      (nreverse split-name))
10668              nil nil nil
10669              'gnus-group-history))))
10670          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10671     (when to-newsgroup
10672       (if (or (string= to-newsgroup "")
10673               (string= to-newsgroup prefix))
10674           (setq to-newsgroup default))
10675       (unless to-newsgroup
10676         (error "No group name entered"))
10677       (or (gnus-active to-newsgroup)
10678           (gnus-activate-group to-newsgroup nil nil to-method)
10679           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10680                                      to-newsgroup))
10681               (or (and (gnus-request-create-group to-newsgroup to-method)
10682                        (gnus-activate-group
10683                         to-newsgroup nil nil to-method)
10684                        (gnus-subscribe-group to-newsgroup))
10685                   (error "Couldn't create group %s" to-newsgroup)))
10686           (error "No such group: %s" to-newsgroup)))
10687     to-newsgroup))
10688
10689 (defun gnus-summary-save-parts (type dir n &optional reverse)
10690   "Save parts matching TYPE to DIR.
10691 If REVERSE, save parts that do not match TYPE."
10692   (interactive
10693    (list (read-string "Save parts of type: "
10694                       (or (car gnus-summary-save-parts-type-history)
10695                           gnus-summary-save-parts-default-mime)
10696                       'gnus-summary-save-parts-type-history)
10697          (setq gnus-summary-save-parts-last-directory
10698                (read-file-name "Save to directory: "
10699                                gnus-summary-save-parts-last-directory
10700                                nil t))
10701          current-prefix-arg))
10702   (gnus-summary-iterate n
10703     (let ((gnus-display-mime-function nil)
10704           (gnus-inhibit-treatment t))
10705       (gnus-summary-select-article))
10706     (save-excursion
10707       (set-buffer gnus-article-buffer)
10708       (let ((handles (or gnus-article-mime-handles
10709                          (mm-dissect-buffer nil gnus-article-loose-mime)
10710                          (mm-uu-dissect))))
10711         (when handles
10712           (gnus-summary-save-parts-1 type dir handles reverse)
10713           (unless gnus-article-mime-handles ;; Don't destroy this case.
10714             (mm-destroy-parts handles)))))))
10715
10716 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10717   (if (stringp (car handle))
10718       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10719               (cdr handle))
10720     (when (if reverse
10721               (not (string-match type (mm-handle-media-type handle)))
10722             (string-match type (mm-handle-media-type handle)))
10723       (let ((file (expand-file-name
10724                    (file-name-nondirectory
10725                     (or
10726                      (mail-content-type-get
10727                       (mm-handle-disposition handle) 'filename)
10728                      (concat gnus-newsgroup-name
10729                              "." (number-to-string
10730                                   (cdr gnus-article-current)))))
10731                    dir)))
10732         (unless (file-exists-p file)
10733           (mm-save-part-to-file handle file))))))
10734
10735 ;; Summary extract commands
10736
10737 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10738   (let ((buffer-read-only nil)
10739         (article (gnus-summary-article-number))
10740         after-article b e)
10741     (unless (gnus-summary-goto-subject article)
10742       (error "No such article: %d" article))
10743     (gnus-summary-position-point)
10744     ;; If all commands are to be bunched up on one line, we collect
10745     ;; them here.
10746     (unless gnus-view-pseudos-separately
10747       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10748             files action)
10749         (while ps
10750           (setq action (cdr (assq 'action (car ps))))
10751           (setq files (list (cdr (assq 'name (car ps)))))
10752           (while (and ps (cdr ps)
10753                       (string= (or action "1")
10754                                (or (cdr (assq 'action (cadr ps))) "2")))
10755             (push (cdr (assq 'name (cadr ps))) files)
10756             (setcdr ps (cddr ps)))
10757           (when files
10758             (when (not (string-match "%s" action))
10759               (push " " files))
10760             (push " " files)
10761             (when (assq 'execute (car ps))
10762               (setcdr (assq 'execute (car ps))
10763                       (funcall (if (string-match "%s" action)
10764                                    'format 'concat)
10765                                action
10766                                (mapconcat
10767                                 (lambda (f)
10768                                   (if (equal f " ")
10769                                       f
10770                                     (mm-quote-arg f)))
10771                                 files " ")))))
10772           (setq ps (cdr ps)))))
10773     (if (and gnus-view-pseudos (not not-view))
10774         (while pslist
10775           (when (assq 'execute (car pslist))
10776             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10777                                   (eq gnus-view-pseudos 'not-confirm)))
10778           (setq pslist (cdr pslist)))
10779       (save-excursion
10780         (while pslist
10781           (setq after-article (or (cdr (assq 'article (car pslist)))
10782                                   (gnus-summary-article-number)))
10783           (gnus-summary-goto-subject after-article)
10784           (forward-line 1)
10785           (setq b (point))
10786           (insert "    " (file-name-nondirectory
10787                           (cdr (assq 'name (car pslist))))
10788                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10789           (setq e (point))
10790           (forward-line -1)             ; back to `b'
10791           (gnus-add-text-properties
10792            b (1- e) (list 'gnus-number gnus-reffed-article-number
10793                           gnus-mouse-face-prop gnus-mouse-face))
10794           (gnus-data-enter
10795            after-article gnus-reffed-article-number
10796            gnus-unread-mark b (car pslist) 0 (- e b))
10797           (setq gnus-newsgroup-unreads
10798                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10799                                          gnus-reffed-article-number))
10800           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10801           (setq pslist (cdr pslist)))))))
10802
10803 (defun gnus-pseudos< (p1 p2)
10804   (let ((c1 (cdr (assq 'action p1)))
10805         (c2 (cdr (assq 'action p2))))
10806     (and c1 c2 (string< c1 c2))))
10807
10808 (defun gnus-request-pseudo-article (props)
10809   (cond ((assq 'execute props)
10810          (gnus-execute-command (cdr (assq 'execute props)))))
10811   (let ((gnus-current-article (gnus-summary-article-number)))
10812     (gnus-run-hooks 'gnus-mark-article-hook)))
10813
10814 (defun gnus-execute-command (command &optional automatic)
10815   (save-excursion
10816     (gnus-article-setup-buffer)
10817     (set-buffer gnus-article-buffer)
10818     (setq buffer-read-only nil)
10819     (let ((command (if automatic command
10820                      (read-string "Command: " (cons command 0)))))
10821       (erase-buffer)
10822       (insert "$ " command "\n\n")
10823       (if gnus-view-pseudo-asynchronously
10824           (start-process "gnus-execute" (current-buffer) shell-file-name
10825                          shell-command-switch command)
10826         (call-process shell-file-name nil t nil
10827                       shell-command-switch command)))))
10828
10829 ;; Summary kill commands.
10830
10831 (defun gnus-summary-edit-global-kill (article)
10832   "Edit the \"global\" kill file."
10833   (interactive (list (gnus-summary-article-number)))
10834   (gnus-group-edit-global-kill article))
10835
10836 (defun gnus-summary-edit-local-kill ()
10837   "Edit a local kill file applied to the current newsgroup."
10838   (interactive)
10839   (setq gnus-current-headers (gnus-summary-article-header))
10840   (gnus-group-edit-local-kill
10841    (gnus-summary-article-number) gnus-newsgroup-name))
10842
10843 ;;; Header reading.
10844
10845 (defun gnus-read-header (id &optional header)
10846   "Read the headers of article ID and enter them into the Gnus system."
10847   (let ((group gnus-newsgroup-name)
10848         (gnus-override-method
10849          (or
10850           gnus-override-method
10851           (and (gnus-news-group-p gnus-newsgroup-name)
10852                (car (gnus-refer-article-methods)))))
10853         where)
10854     ;; First we check to see whether the header in question is already
10855     ;; fetched.
10856     (if (stringp id)
10857         ;; This is a Message-ID.
10858         (setq header (or header (gnus-id-to-header id)))
10859       ;; This is an article number.
10860       (setq header (or header (gnus-summary-article-header id))))
10861     (if (and header
10862              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10863         ;; We have found the header.
10864         header
10865       ;; If this is a sparse article, we have to nix out its
10866       ;; previous entry in the thread hashtb.
10867       (when (and header
10868                  (gnus-summary-article-sparse-p (mail-header-number header)))
10869         (let* ((parent (gnus-parent-id (mail-header-references header)))
10870                (thread (and parent (gnus-id-to-thread parent))))
10871           (when thread
10872             (delq (assq header thread) thread))))
10873       ;; We have to really fetch the header to this article.
10874       (save-excursion
10875         (set-buffer nntp-server-buffer)
10876         (when (setq where (gnus-request-head id group))
10877           (nnheader-fold-continuation-lines)
10878           (goto-char (point-max))
10879           (insert ".\n")
10880           (goto-char (point-min))
10881           (insert "211 ")
10882           (princ (cond
10883                   ((numberp id) id)
10884                   ((cdr where) (cdr where))
10885                   (header (mail-header-number header))
10886                   (t gnus-reffed-article-number))
10887                  (current-buffer))
10888           (insert " Article retrieved.\n"))
10889         (if (or (not where)
10890                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10891             ()                          ; Malformed head.
10892           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10893             (when (and (stringp id)
10894                        (not (string= (gnus-group-real-name group)
10895                                      (car where))))
10896               ;; If we fetched by Message-ID and the article came
10897               ;; from a different group, we fudge some bogus article
10898               ;; numbers for this article.
10899               (mail-header-set-number header gnus-reffed-article-number))
10900             (save-excursion
10901               (set-buffer gnus-summary-buffer)
10902               (decf gnus-reffed-article-number)
10903               (gnus-remove-header (mail-header-number header))
10904               (push header gnus-newsgroup-headers)
10905               (setq gnus-current-headers header)
10906               (push (mail-header-number header) gnus-newsgroup-limit)))
10907           header)))))
10908
10909 (defun gnus-remove-header (number)
10910   "Remove header NUMBER from `gnus-newsgroup-headers'."
10911   (if (and gnus-newsgroup-headers
10912            (= number (mail-header-number (car gnus-newsgroup-headers))))
10913       (pop gnus-newsgroup-headers)
10914     (let ((headers gnus-newsgroup-headers))
10915       (while (and (cdr headers)
10916                   (not (= number (mail-header-number (cadr headers)))))
10917         (pop headers))
10918       (when (cdr headers)
10919         (setcdr headers (cddr headers))))))
10920
10921 ;;;
10922 ;;; summary highlights
10923 ;;;
10924
10925 (defun gnus-highlight-selected-summary ()
10926   "Highlight selected article in summary buffer."
10927   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10928   (when gnus-summary-selected-face
10929     (save-excursion
10930       (let* ((beg (progn (beginning-of-line) (point)))
10931              (end (progn (end-of-line) (point)))
10932              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10933              (from (if (get-text-property beg gnus-mouse-face-prop)
10934                        beg
10935                      (or (next-single-property-change
10936                           beg gnus-mouse-face-prop nil end)
10937                          beg)))
10938              (to
10939               (if (= from end)
10940                   (- from 2)
10941                 (or (next-single-property-change
10942                      from gnus-mouse-face-prop nil end)
10943                     end))))
10944         ;; If no mouse-face prop on line we will have to = from = end,
10945         ;; so we highlight the entire line instead.
10946         (when (= (+ to 2) from)
10947           (setq from beg)
10948           (setq to end))
10949         (if gnus-newsgroup-selected-overlay
10950             ;; Move old overlay.
10951             (gnus-move-overlay
10952              gnus-newsgroup-selected-overlay from to (current-buffer))
10953           ;; Create new overlay.
10954           (gnus-overlay-put
10955            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10956            'face gnus-summary-selected-face))))))
10957
10958 (defvar gnus-summary-highlight-line-cached nil)
10959 (defvar gnus-summary-highlight-line-trigger nil)
10960
10961 (defun gnus-summary-highlight-line-0 ()
10962   (if (and (eq gnus-summary-highlight-line-trigger 
10963                gnus-summary-highlight)
10964            gnus-summary-highlight-line-cached)
10965       gnus-summary-highlight-line-cached
10966     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10967           gnus-summary-highlight-line-cached
10968           (let* ((cond (list 'cond))
10969                  (c cond)
10970                  (list gnus-summary-highlight))
10971             (while list
10972               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10973               (setq c (cdr c)
10974                     list (cdr list)))
10975             (gnus-byte-compile (list 'lambda nil cond))))))
10976
10977 (defun gnus-summary-highlight-line ()
10978   "Highlight current line according to `gnus-summary-highlight'."
10979   (let* ((beg (gnus-point-at-bol))
10980          (article (or (gnus-summary-article-number) gnus-current-article))
10981          (score (or (cdr (assq article
10982                                gnus-newsgroup-scored))
10983                     gnus-summary-default-score 0))
10984          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10985          (inhibit-read-only t)
10986          (default gnus-summary-default-score)
10987          (default-high gnus-summary-default-high-score)
10988          (default-low gnus-summary-default-low-score)
10989          (uncached (memq article gnus-newsgroup-undownloaded))
10990          (downloaded (not uncached)))
10991     (let ((face (funcall (gnus-summary-highlight-line-0))))
10992       (unless (eq face (get-text-property beg 'face))
10993         (gnus-put-text-property-excluding-characters-with-faces
10994          beg (gnus-point-at-eol) 'face
10995          (setq face (if (boundp face) (symbol-value face) face)))
10996         (when gnus-summary-highlight-line-function
10997           (funcall gnus-summary-highlight-line-function article face))))))
10998
10999 (defun gnus-update-read-articles (group unread &optional compute)
11000   "Update the list of read articles in GROUP.
11001 UNREAD is a sorted list."
11002   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11003          (entry (gnus-gethash group gnus-newsrc-hashtb))
11004          (info (nth 2 entry))
11005          (prev 1)
11006          read)
11007     (if (or (not info) (not active))
11008         ;; There is no info on this group if it was, in fact,
11009         ;; killed.  Gnus stores no information on killed groups, so
11010         ;; there's nothing to be done.
11011         ;; One could store the information somewhere temporarily,
11012         ;; perhaps...  Hmmm...
11013         ()
11014       ;; Remove any negative articles numbers.
11015       (while (and unread (< (car unread) 0))
11016         (setq unread (cdr unread)))
11017       ;; Remove any expired article numbers
11018       (while (and unread (< (car unread) (car active)))
11019         (setq unread (cdr unread)))
11020       ;; Compute the ranges of read articles by looking at the list of
11021       ;; unread articles.
11022       (while unread
11023         (when (/= (car unread) prev)
11024           (push (if (= prev (1- (car unread))) prev
11025                   (cons prev (1- (car unread))))
11026                 read))
11027         (setq prev (1+ (car unread)))
11028         (setq unread (cdr unread)))
11029       (when (<= prev (cdr active))
11030         (push (cons prev (cdr active)) read))
11031       (setq read (if (> (length read) 1) (nreverse read) read))
11032       (if compute
11033           read
11034         (save-excursion
11035           (let (setmarkundo)
11036             ;; Propagate the read marks to the backend.
11037             (when (gnus-check-backend-function 'request-set-mark group)
11038               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11039                     (add (gnus-remove-from-range read (gnus-info-read info))))
11040                 (when (or add del)
11041                   (unless (gnus-check-group group)
11042                     (error "Can't open server for %s" group))
11043                   (gnus-request-set-mark
11044                    group (delq nil (list (if add (list add 'add '(read)))
11045                                          (if del (list del 'del '(read))))))
11046                   (setq setmarkundo
11047                         `(gnus-request-set-mark
11048                           ,group
11049                           ',(delq nil (list
11050                                        (if del (list del 'add '(read)))
11051                                        (if add (list add 'del '(read))))))))))
11052             (set-buffer gnus-group-buffer)
11053             (gnus-undo-register
11054               `(progn
11055                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11056                  (gnus-info-set-read ',info ',(gnus-info-read info))
11057                  (gnus-get-unread-articles-in-group ',info
11058                                                     (gnus-active ,group))
11059                  (gnus-group-update-group ,group t)
11060                  ,setmarkundo))))
11061         ;; Enter this list into the group info.
11062         (gnus-info-set-read info read)
11063         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11064         (gnus-get-unread-articles-in-group info (gnus-active group))
11065         t))))
11066
11067 (defun gnus-offer-save-summaries ()
11068   "Offer to save all active summary buffers."
11069   (let (buffers)
11070     ;; Go through all buffers and find all summaries.
11071     (dolist (buffer (buffer-list))
11072       (when (and (setq buffer (buffer-name buffer))
11073                  (string-match "Summary" buffer)
11074                  (save-excursion
11075                    (set-buffer buffer)
11076                    ;; We check that this is, indeed, a summary buffer.
11077                    (and (eq major-mode 'gnus-summary-mode)
11078                         ;; Also make sure this isn't bogus.
11079                         gnus-newsgroup-prepared
11080                         ;; Also make sure that this isn't a
11081                         ;; dead summary buffer.
11082                         (not gnus-dead-summary-mode))))
11083         (push buffer buffers)))
11084     ;; Go through all these summary buffers and offer to save them.
11085     (when buffers
11086       (save-excursion
11087         (map-y-or-n-p
11088          "Update summary buffer %s? "
11089          (lambda (buf)
11090            (switch-to-buffer buf)
11091            (gnus-summary-exit))
11092          buffers)))))
11093
11094 (defun gnus-summary-setup-default-charset ()
11095   "Setup newsgroup default charset."
11096   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11097       (setq gnus-newsgroup-charset nil)
11098     (let* ((ignored-charsets
11099             (or gnus-newsgroup-ephemeral-ignored-charsets
11100                 (append
11101                  (and gnus-newsgroup-name
11102                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11103                  gnus-newsgroup-ignored-charsets))))
11104       (setq gnus-newsgroup-charset
11105             (or gnus-newsgroup-ephemeral-charset
11106                 (and gnus-newsgroup-name
11107                      (gnus-parameter-charset gnus-newsgroup-name))
11108                 gnus-default-charset))
11109       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11110            ignored-charsets))))
11111
11112 ;;;
11113 ;;; Mime Commands
11114 ;;;
11115
11116 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11117   "Display the current article buffer fully MIME-buttonized.
11118 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11119 treated as multipart/mixed."
11120   (interactive "P")
11121   (require 'gnus-art)
11122   (let ((gnus-unbuttonized-mime-types nil)
11123         (gnus-mime-display-multipart-as-mixed show-all-parts))
11124     (gnus-summary-show-article)))
11125
11126 (defun gnus-summary-repair-multipart (article)
11127   "Add a Content-Type header to a multipart article without one."
11128   (interactive (list (gnus-summary-article-number)))
11129   (gnus-with-article article
11130     (message-narrow-to-head)
11131     (message-remove-header "Mime-Version")
11132     (goto-char (point-max))
11133     (insert "Mime-Version: 1.0\n")
11134     (widen)
11135     (when (search-forward "\n--" nil t)
11136       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11137         (message-narrow-to-head)
11138         (message-remove-header "Content-Type")
11139         (goto-char (point-max))
11140         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11141                         separator))
11142         (widen))))
11143   (let (gnus-mark-article-hook)
11144     (gnus-summary-select-article t t nil article)))
11145
11146 (defun gnus-summary-toggle-display-buttonized ()
11147   "Toggle the buttonizing of the article buffer."
11148   (interactive)
11149   (require 'gnus-art)
11150   (if (setq gnus-inhibit-mime-unbuttonizing
11151             (not gnus-inhibit-mime-unbuttonizing))
11152       (let ((gnus-unbuttonized-mime-types nil))
11153         (gnus-summary-show-article))
11154     (gnus-summary-show-article)))
11155
11156 ;;;
11157 ;;; Generic summary marking commands
11158 ;;;
11159
11160 (defvar gnus-summary-marking-alist
11161   '((read gnus-del-mark "d")
11162     (unread gnus-unread-mark "u")
11163     (ticked gnus-ticked-mark "!")
11164     (dormant gnus-dormant-mark "?")
11165     (expirable gnus-expirable-mark "e"))
11166   "An alist of names/marks/keystrokes.")
11167
11168 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11169 (defvar gnus-summary-mark-map)
11170
11171 (defun gnus-summary-make-all-marking-commands ()
11172   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11173   (dolist (elem gnus-summary-marking-alist)
11174     (apply 'gnus-summary-make-marking-command elem)))
11175
11176 (defun gnus-summary-make-marking-command (name mark keystroke)
11177   (let ((map (make-sparse-keymap)))
11178     (define-key gnus-summary-generic-mark-map keystroke map)
11179     (dolist (lway `((next "next" next nil "n")
11180                     (next-unread "next unread" next t "N")
11181                     (prev "previous" prev nil "p")
11182                     (prev-unread "previous unread" prev t "P")
11183                     (nomove "" nil nil ,keystroke)))
11184       (let ((func (gnus-summary-make-marking-command-1
11185                    mark (car lway) lway name)))
11186         (setq func (eval func))
11187         (define-key map (nth 4 lway) func)))))
11188
11189 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11190   `(defun ,(intern
11191             (format "gnus-summary-put-mark-as-%s%s"
11192                     name (if (eq way 'nomove)
11193                              ""
11194                            (concat "-" (symbol-name way)))))
11195      (n)
11196      ,(format
11197        "Mark the current article as %s%s.
11198 If N, the prefix, then repeat N times.
11199 If N is negative, move in reverse order.
11200 The difference between N and the actual number of articles marked is
11201 returned."
11202        name (cadr lway))
11203      (interactive "p")
11204      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11205
11206 (defun gnus-summary-generic-mark (n mark move unread)
11207   "Mark N articles with MARK."
11208   (unless (eq major-mode 'gnus-summary-mode)
11209     (error "This command can only be used in the summary buffer"))
11210   (gnus-summary-show-thread)
11211   (let ((nummove
11212          (cond
11213           ((eq move 'next) 1)
11214           ((eq move 'prev) -1)
11215           (t 0))))
11216     (if (zerop nummove)
11217         (setq n 1)
11218       (when (< n 0)
11219         (setq n (abs n)
11220               nummove (* -1 nummove))))
11221     (while (and (> n 0)
11222                 (gnus-summary-mark-article nil mark)
11223                 (zerop (gnus-summary-next-subject nummove unread t)))
11224       (setq n (1- n)))
11225     (when (/= 0 n)
11226       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11227     (gnus-summary-recenter)
11228     (gnus-summary-position-point)
11229     (gnus-set-mode-line 'summary)
11230     n))
11231
11232 (defun gnus-summary-insert-articles (articles)
11233   (when (setq articles
11234               (gnus-sorted-difference articles
11235                                       (mapcar (lambda (h)
11236                                                 (mail-header-number h))
11237                                               gnus-newsgroup-headers)))
11238     (setq gnus-newsgroup-headers
11239           (merge 'list
11240                  gnus-newsgroup-headers
11241                  (gnus-fetch-headers articles)
11242                  'gnus-article-sort-by-number))
11243     ;; Suppress duplicates?
11244     (when gnus-suppress-duplicates
11245       (gnus-dup-suppress-articles))
11246
11247     ;; We might want to build some more threads first.
11248     (when (and gnus-fetch-old-headers
11249                (eq gnus-headers-retrieved-by 'nov))
11250       (if (eq gnus-fetch-old-headers 'invisible)
11251           (gnus-build-all-threads)
11252         (gnus-build-old-threads)))
11253     ;; Let the Gnus agent mark articles as read.
11254     (when gnus-agent
11255       (gnus-agent-get-undownloaded-list))
11256     ;; Remove list identifiers from subject
11257     (when gnus-list-identifiers
11258       (gnus-summary-remove-list-identifiers))
11259     ;; First and last article in this newsgroup.
11260     (when gnus-newsgroup-headers
11261       (setq gnus-newsgroup-begin
11262             (mail-header-number (car gnus-newsgroup-headers))
11263             gnus-newsgroup-end
11264             (mail-header-number
11265              (gnus-last-element gnus-newsgroup-headers))))
11266     (when gnus-use-scoring
11267       (gnus-possibly-score-headers))))
11268
11269 (defun gnus-summary-insert-old-articles (&optional all)
11270   "Insert all old articles in this group.
11271 If ALL is non-nil, already read articles become readable.
11272 If ALL is a number, fetch this number of articles."
11273   (interactive "P")
11274   (prog1
11275       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11276             older len)
11277         (setq older
11278               ;; Some nntp servers lie about their active range.  When
11279               ;; this happens, the active range can be in the millions.
11280               ;; Use a compressed range to avoid creating a huge list.
11281               (gnus-range-difference (list gnus-newsgroup-active) old))
11282         (setq len (gnus-range-length older))
11283         (cond
11284          ((null older) nil)
11285          ((numberp all)
11286           (if (< all len)
11287               (let ((older-range (nreverse older)))
11288                 (setq older nil)
11289
11290                 (while (> all 0)
11291                   (let* ((r (pop older-range))
11292                          (min (if (numberp r) r (car r)))
11293                          (max (if (numberp r) r (cdr r))))
11294                     (while (and (<= min max)
11295                                 (> all 0))
11296                       (push max older)
11297                       (setq all (1- all)
11298                             max (1- max))))))
11299             (setq older (gnus-uncompress-range older))))
11300          (all
11301           (setq older (gnus-uncompress-range older)))
11302          (t
11303           (when (and (numberp gnus-large-newsgroup)
11304                    (> len gnus-large-newsgroup))
11305               (let* ((cursor-in-echo-area nil)
11306                      (initial (gnus-parameter-large-newsgroup-initial
11307                                gnus-newsgroup-name))
11308                      (input
11309                       (read-string
11310                        (format
11311                         "How many articles from %s (%s %d): "
11312                         (gnus-limit-string
11313                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11314                         (if initial "max" "default")
11315                         len)
11316                        (if initial
11317                            (cons (number-to-string initial)
11318                                  0)))))
11319                 (unless (string-match "^[ \t]*$" input)
11320                   (setq all (string-to-number input))
11321                   (if (< all len)
11322                       (let ((older-range (nreverse older)))
11323                         (setq older nil)
11324
11325                         (while (> all 0)
11326                           (let* ((r (pop older-range))
11327                                  (min (if (numberp r) r (car r)))
11328                                  (max (if (numberp r) r (cdr r))))
11329                             (while (and (<= min max)
11330                                         (> all 0))
11331                               (push max older)
11332                               (setq all (1- all)
11333                                     max (1- max))))))))))
11334           (setq older (gnus-uncompress-range older))))
11335         (if (not older)
11336             (message "No old news.")
11337           (gnus-summary-insert-articles older)
11338           (gnus-summary-limit (gnus-sorted-nunion old older))))
11339     (gnus-summary-position-point)))
11340
11341 (defun gnus-summary-insert-new-articles ()
11342   "Insert all new articles in this group."
11343   (interactive)
11344   (prog1
11345       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11346             (old-active gnus-newsgroup-active)
11347             (nnmail-fetched-sources (list t))
11348             i new)
11349         (setq gnus-newsgroup-active
11350               (gnus-activate-group gnus-newsgroup-name 'scan))
11351         (setq i (cdr gnus-newsgroup-active))
11352         (while (> i (cdr old-active))
11353           (push i new)
11354           (decf i))
11355         (if (not new)
11356             (message "No gnus is bad news.")
11357           (gnus-summary-insert-articles new)
11358           (setq gnus-newsgroup-unreads
11359                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11360           (gnus-summary-limit (gnus-sorted-nunion old new))))
11361     (gnus-summary-position-point)))
11362
11363 (gnus-summary-make-all-marking-commands)
11364
11365 (gnus-ems-redefine)
11366
11367 (provide 'gnus-sum)
11368
11369 (run-hooks 'gnus-sum-load-hook)
11370
11371 ;;; gnus-sum.el ends here