* gnus-sum.el (gnus-mime-extract-message/rfc822): Rewrote again.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-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 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'nnoo)
42 (require 'mime-view)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
55 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
56 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58
59 (defcustom gnus-kill-summary-on-exit t
60   "*If non-nil, kill the summary buffer when you exit from it.
61 If nil, the summary will become a \"*Dead Summary*\" buffer, and
62 it will be killed sometime later."
63   :group 'gnus-summary-exit
64   :type 'boolean)
65
66 (defcustom gnus-fetch-old-headers nil
67   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
68 If an unread article in the group refers to an older, already read (or
69 just marked as read) article, the old article will not normally be
70 displayed in the Summary buffer.  If this variable is non-nil, Gnus
71 will attempt to grab the headers to the old articles, and thereby
72 build complete threads.  If it has the value `some', only enough
73 headers to connect otherwise loose threads will be displayed.  This
74 variable can also be a number.  In that case, no more than that number
75 of old headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work."
79   :group 'gnus-thread
80   :type '(choice (const :tag "off" nil)
81                  (const some)
82                  number
83                  (sexp :menu-tag "other" t)))
84
85 (defcustom gnus-refer-thread-limit 200
86   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
87 If t, fetch all the available old headers."
88   :group 'gnus-thread
89   :type '(choice number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-summary-make-false-root 'adopt
93   "*nil means that Gnus won't gather loose threads.
94 If the root of a thread has expired or been read in a previous
95 session, the information necessary to build a complete thread has been
96 lost.  Instead of having many small sub-threads from this original thread
97 scattered all over the summary buffer, Gnus can gather them.
98
99 If non-nil, Gnus will try to gather all loose sub-threads from an
100 original thread into one large thread.
101
102 If this variable is non-nil, it should be one of `none', `adopt',
103 `dummy' or `empty'.
104
105 If this variable is `none', Gnus will not make a false root, but just
106 present the sub-threads after another.
107 If this variable is `dummy', Gnus will create a dummy root that will
108 have all the sub-threads as children.
109 If this variable is `adopt', Gnus will make one of the \"children\"
110 the parent and mark all the step-children as such.
111 If this variable is `empty', the \"children\" are printed with empty
112 subject fields.  (Or rather, they will be printed with a string
113 given by the `gnus-summary-same-subject' variable.)"
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  (const none)
117                  (const dummy)
118                  (const adopt)
119                  (const empty)))
120
121 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
122   "*A regexp to match subjects to be excluded from loose thread gathering.
123 As loose thread gathering is done on subjects only, that means that
124 there can be many false gatherings performed.  By rooting out certain
125 common subjects, gathering might become saner."
126   :group 'gnus-thread
127   :type 'regexp)
128
129 (defcustom gnus-summary-gather-subject-limit nil
130   "*Maximum length of subject comparisons when gathering loose threads.
131 Use nil to compare full subjects.  Setting this variable to a low
132 number will help gather threads that have been corrupted by
133 newsreaders chopping off subject lines, but it might also mean that
134 unrelated articles that have subject that happen to begin with the
135 same few characters will be incorrectly gathered.
136
137 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
138 comparing subjects."
139   :group 'gnus-thread
140   :type '(choice (const :tag "off" nil)
141                  (const fuzzy)
142                  (sexp :menu-tag "on" t)))
143
144 (defcustom gnus-simplify-subject-functions nil
145   "List of functions taking a string argument that simplify subjects.
146 The functions are applied recursively.
147
148 Useful functions to put in this list include: `gnus-simplify-subject-re',
149 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 (they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line.."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-dont-select-after-jump-to-other-group nil
322   "If non-nil, don't select the first unread article after entering the
323 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
324 it is depend on the value of `gnus-auto-select-first' whether to select
325 or not."
326   :group 'gnus-group-select
327   :type 'boolean)
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `Z n' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-summary-check-current nil
355   "*If non-nil, consider the current article when moving.
356 The \"unread\" movement commands will stay on the same line if the
357 current article is unread."
358   :group 'gnus-summary-maneuvering
359   :type 'boolean)
360
361 (defcustom gnus-auto-center-summary t
362   "*If non-nil, always center the current summary buffer.
363 In particular, if `vertical' do only vertical recentering.  If non-nil
364 and non-`vertical', do both horizontal and vertical recentering."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "none" nil)
367                  (const vertical)
368                  (integer :tag "height")
369                  (sexp :menu-tag "both" t)))
370
371 (defcustom gnus-show-all-headers nil
372   "*If non-nil, don't hide any headers."
373   :group 'gnus-article-hiding
374   :group 'gnus-article-headers
375   :type 'boolean)
376
377 (defcustom gnus-summary-ignore-duplicates nil
378   "*If non-nil, ignore articles with identical Message-ID headers."
379   :group 'gnus-summary
380   :type 'boolean)
381
382 (defcustom gnus-single-article-buffer t
383   "*If non-nil, display all articles in the same buffer.
384 If nil, each group will get its own article buffer."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-break-pages t
389   "*If non-nil, do page breaking on articles.
390 The page delimiter is specified by the `gnus-page-delimiter'
391 variable."
392   :group 'gnus-article-various
393   :type 'boolean)
394
395 (defcustom gnus-show-mime t
396   "*If non-nil, do mime processing of articles.
397 The articles will simply be fed to the function given by
398 `gnus-article-display-method-for-mime'."
399   :group 'gnus-article-mime
400   :type 'boolean)
401
402 (defcustom gnus-move-split-methods nil
403   "*Variable used to suggest where articles are to be moved to.
404 It uses the same syntax as the `gnus-split-methods' variable.
405 However, whereas `gnus-split-methods' specifies file names as targets,
406 this variable specifies group names."
407   :group 'gnus-summary-mail
408   :type '(repeat (choice (list :value (fun) function)
409                          (cons :value ("" "") regexp (repeat string))
410                          (sexp :value nil))))
411
412 (defcustom gnus-unread-mark ?\ ;;;Whitespace
413   "*Mark used for unread articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-ticked-mark ?!
418   "*Mark used for ticked articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-dormant-mark ??
423   "*Mark used for dormant articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-del-mark ?r
428   "*Mark used for del'd articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-read-mark ?R
433   "*Mark used for read articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-expirable-mark ?E
438   "*Mark used for expirable articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-killed-mark ?K
443   "*Mark used for killed articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-souped-mark ?F
448   "*Mark used for souped articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-kill-file-mark ?X
453   "*Mark used for articles killed by kill files."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-low-score-mark ?Y
458   "*Mark used for articles with a low score."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-catchup-mark ?C
463   "*Mark used for articles that are caught up."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-replied-mark ?A
468   "*Mark used for articles that have been replied to."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-forwarded-mark ?F
473   "*Mark used for articles that have been forwarded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-recent-mark ?N
478   "*Mark used for articles that are recent."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-cached-mark ?*
483   "*Mark used for articles that are in the cache."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-saved-mark ?S
488   "*Mark used for articles that have been saved."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-unseen-mark ?.
493   "*Mark used for articles that haven't been seen."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-no-mark ?\ ;;;Whitespace
498   "*Mark used for articles that have no other secondary mark."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-ancient-mark ?O
503   "*Mark used for ancient articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-sparse-mark ?Q
508   "*Mark used for sparsely reffed articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-canceled-mark ?G
513   "*Mark used for canceled articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-duplicate-mark ?M
518   "*Mark used for duplicate articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-undownloaded-mark ?@
523   "*Mark used for articles that weren't downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-downloadable-mark ?%
528   "*Mark used for articles that are to be downloaded."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-unsendable-mark ?=
533   "*Mark used for articles that won't be sent."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-over-mark ?+
538   "*Score mark used for articles with high scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-score-below-mark ?-
543   "*Score mark used for articles with low scores."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
548   "*There is no thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-not-empty-thread-mark ?=
553   "*There is a thread under the article."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-view-pseudo-asynchronously nil
558   "*If non-nil, Gnus will view pseudo-articles asynchronously."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-auto-expirable-marks
563   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
564         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
565         gnus-souped-mark gnus-duplicate-mark)
566   "*The list of marks converted into expiration if a group is auto-expirable."
567   :version "21.1"
568   :group 'gnus-summary
569   :type '(repeat character))
570
571 (defcustom gnus-inhibit-user-auto-expire t
572   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
573   :version "21.1"
574   :group 'gnus-summary
575   :type 'boolean)
576
577 (defcustom gnus-view-pseudos nil
578   "*If `automatic', pseudo-articles will be viewed automatically.
579 If `not-confirm', pseudos will be viewed automatically, and the user
580 will not be asked to confirm the command."
581   :group 'gnus-extract-view
582   :type '(choice (const :tag "off" nil)
583                  (const automatic)
584                  (const not-confirm)))
585
586 (defcustom gnus-view-pseudos-separately t
587   "*If non-nil, one pseudo-article will be created for each file to be viewed.
588 If nil, all files that use the same viewing command will be given as a
589 list of parameters to that command."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-insert-pseudo-articles t
594   "*If non-nil, insert pseudo-articles when decoding articles."
595   :group 'gnus-extract-view
596   :type 'boolean)
597
598 (defcustom gnus-summary-dummy-line-format
599   "  %(:                          :%) %S\n"
600   "*The format specification for the dummy roots in the summary buffer.
601 It works along the same lines as a normal formatting string,
602 with some simple extensions.
603
604 %S  The subject
605
606 General format specifiers can also be used.
607 See (gnus)Formatting Variables."
608   :link '(custom-manual "(gnus)Formatting Variables")
609   :group 'gnus-threading
610   :type 'string)
611
612 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
613   "*The format specification for the summary mode line.
614 It works along the same lines as a normal formatting string,
615 with some simple extensions:
616
617 %G  Group name
618 %p  Unprefixed group name
619 %A  Current article number
620 %z  Current article score
621 %V  Gnus version
622 %U  Number of unread articles in the group
623 %e  Number of unselected articles in the group
624 %Z  A string with unread/unselected article counts
625 %g  Shortish group name
626 %S  Subject of the current article
627 %u  User-defined spec
628 %s  Current score file name
629 %d  Number of dormant articles
630 %r  Number of articles that have been marked as read in this session
631 %E  Number of articles expunged by the score files"
632   :group 'gnus-summary-format
633   :type 'string)
634
635 (defcustom gnus-list-identifiers nil
636   "Regexp that matches list identifiers to be removed from subject.
637 This can also be a list of regexps."
638   :version "21.1"
639   :group 'gnus-summary-format
640   :group 'gnus-article-hiding
641   :type '(choice (const :tag "none" nil)
642                  (regexp :value ".*")
643                  (repeat :value (".*") regexp)))
644
645 (defcustom gnus-summary-mark-below 0
646   "*Mark all articles with a score below this variable as read.
647 This variable is local to each summary buffer and usually set by the
648 score file."
649   :group 'gnus-score-default
650   :type 'integer)
651
652 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
653   "*List of functions used for sorting articles in the summary buffer.
654
655 Each function takes two articles and returns non-nil if the first
656 article should be sorted before the other.  If you use more than one
657 function, the primary sort function should be the last.  You should
658 probably always include `gnus-article-sort-by-number' in the list of
659 sorting functions -- preferably first.  Also note that sorting by date
660 is often much slower than sorting by number, and the sorting order is
661 very similar.  (Sorting by date means sorting by the time the message
662 was sent, sorting by number means sorting by arrival time.)
663
664 Ready-made functions include `gnus-article-sort-by-number',
665 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
666 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
667
668 When threading is turned on, the variable `gnus-thread-sort-functions'
669 controls how articles are sorted."
670   :group 'gnus-summary-sort
671   :type '(repeat (choice (function-item gnus-article-sort-by-number)
672                          (function-item gnus-article-sort-by-author)
673                          (function-item gnus-article-sort-by-subject)
674                          (function-item gnus-article-sort-by-date)
675                          (function-item gnus-article-sort-by-score)
676                          (function :tag "other"))))
677
678 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
679   "*List of functions used for sorting threads in the summary buffer.
680 By default, threads are sorted by article number.
681
682 Each function takes two threads and returns non-nil if the first
683 thread should be sorted before the other.  If you use more than one
684 function, the primary sort function should be the last.  You should
685 probably always include `gnus-thread-sort-by-number' in the list of
686 sorting functions -- preferably first.  Also note that sorting by date
687 is often much slower than sorting by number, and the sorting order is
688 very similar.  (Sorting by date means sorting by the time the message
689 was sent, sorting by number means sorting by arrival time.)
690
691 Ready-made functions include `gnus-thread-sort-by-number',
692 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
693 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
694 `gnus-thread-sort-by-most-recent-number',
695 `gnus-thread-sort-by-most-recent-date', and
696 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
697
698 When threading is turned off, the variable
699 `gnus-article-sort-functions' controls how articles are sorted."
700   :group 'gnus-summary-sort
701   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
702                          (function-item gnus-thread-sort-by-author)
703                          (function-item gnus-thread-sort-by-subject)
704                          (function-item gnus-thread-sort-by-date)
705                          (function-item gnus-thread-sort-by-score)
706                          (function-item gnus-thread-sort-by-total-score)
707                          (function :tag "other"))))
708
709 (defcustom gnus-thread-score-function '+
710   "*Function used for calculating the total score of a thread.
711
712 The function is called with the scores of the article and each
713 subthread and should then return the score of the thread.
714
715 Some functions you can use are `+', `max', or `min'."
716   :group 'gnus-summary-sort
717   :type 'function)
718
719 (defcustom gnus-summary-expunge-below nil
720   "All articles that have a score less than this variable will be expunged.
721 This variable is local to the summary buffers."
722   :group 'gnus-score-default
723   :type '(choice (const :tag "off" nil)
724                  integer))
725
726 (defcustom gnus-thread-expunge-below nil
727   "All threads that have a total score less than this variable will be expunged.
728 See `gnus-thread-score-function' for en explanation of what a
729 \"thread score\" is.
730
731 This variable is local to the summary buffers."
732   :group 'gnus-threading
733   :group 'gnus-score-default
734   :type '(choice (const :tag "off" nil)
735                  integer))
736
737 (defcustom gnus-summary-mode-hook nil
738   "*A hook for Gnus summary mode.
739 This hook is run before any variables are set in the summary buffer."
740   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
745 (when (featurep 'xemacs)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
747   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
748   (add-hook 'gnus-summary-mode-hook
749             'gnus-xmas-switch-horizontal-scrollbar-off))
750
751 (defcustom gnus-summary-menu-hook nil
752   "*Hook run after the creation of the summary mode menu."
753   :group 'gnus-summary-visual
754   :type 'hook)
755
756 (defcustom gnus-summary-exit-hook nil
757   "*A hook called on exit from the summary buffer.
758 It will be called with point in the group buffer."
759   :group 'gnus-summary-exit
760   :type 'hook)
761
762 (defcustom gnus-summary-prepare-hook nil
763   "*A hook called after the summary buffer has been generated.
764 If you want to modify the summary buffer, you can use this hook."
765   :group 'gnus-summary-various
766   :type 'hook)
767
768 (defcustom gnus-summary-prepared-hook nil
769   "*A hook called as the last thing after the summary buffer has been generated."
770   :group 'gnus-summary-various
771   :type 'hook)
772
773 (defcustom gnus-summary-generate-hook nil
774   "*A hook run just before generating the summary buffer.
775 This hook is commonly used to customize threading variables and the
776 like."
777   :group 'gnus-summary-various
778   :type 'hook)
779
780 (defcustom gnus-select-group-hook nil
781   "*A hook called when a newsgroup is selected.
782
783 If you'd like to simplify subjects like the
784 `gnus-summary-next-same-subject' command does, you can use the
785 following hook:
786
787  (add-hook gnus-select-group-hook
788            (lambda ()
789              (mapcar (lambda (header)
790                        (mail-header-set-subject
791                         header
792                         (gnus-simplify-subject
793                          (mail-header-subject header) 're-only)))
794                      gnus-newsgroup-headers)))"
795   :group 'gnus-group-select
796   :type 'hook)
797
798 (defcustom gnus-select-article-hook nil
799   "*A hook called when an article is selected."
800   :group 'gnus-summary-choose
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defcustom gnus-summary-highlight
864   '(((= mark gnus-canceled-mark)
865      . gnus-summary-cancelled-face)
866     ((and (> score default-high)
867           (or (= mark gnus-dormant-mark)
868               (= mark gnus-ticked-mark)))
869      . gnus-summary-high-ticked-face)
870     ((and (< score default-low)
871           (or (= mark gnus-dormant-mark)
872               (= mark gnus-ticked-mark)))
873      . gnus-summary-low-ticked-face)
874     ((or (= mark gnus-dormant-mark)
875          (= mark gnus-ticked-mark))
876      . gnus-summary-normal-ticked-face)
877     ((and (> score default-high) (= mark gnus-ancient-mark))
878      . gnus-summary-high-ancient-face)
879     ((and (< score default-low) (= mark gnus-ancient-mark))
880      . gnus-summary-low-ancient-face)
881     ((= mark gnus-ancient-mark)
882      . gnus-summary-normal-ancient-face)
883     ((and (> score default-high) (= mark gnus-unread-mark))
884      . gnus-summary-high-unread-face)
885     ((and (< score default-low) (= mark gnus-unread-mark))
886      . gnus-summary-low-unread-face)
887     ((= mark gnus-unread-mark)
888      . gnus-summary-normal-unread-face)
889     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
890                                                   gnus-undownloaded-mark)))
891      . gnus-summary-high-unread-face)
892     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
893                                                  gnus-undownloaded-mark)))
894      . gnus-summary-low-unread-face)
895     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
896           (memq article gnus-newsgroup-unreads))
897      . gnus-summary-normal-unread-face)
898     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
899      . gnus-summary-normal-read-face)
900     ((> score default-high)
901      . gnus-summary-high-read-face)
902     ((< score default-low)
903      . gnus-summary-low-read-face)
904     (t
905      . gnus-summary-normal-read-face))
906   "*Controls the highlighting of summary buffer lines.
907
908 A list of (FORM . FACE) pairs.  When deciding how a a particular
909 summary line should be displayed, each form is evaluated.  The content
910 of the face field after the first true form is used.  You can change
911 how those summary lines are displayed, by editing the face field.
912
913 You can use the following variables in the FORM field.
914
915 score:        The article's score
916 default:      The default article score.
917 default-high: The default score for high scored articles.
918 default-low:  The default score for low scored articles.
919 below:        The score below which articles are automatically marked as read.
920 mark:         The articles mark."
921   :group 'gnus-summary-visual
922   :type '(repeat (cons (sexp :tag "Form" nil)
923                        face)))
924
925 (defcustom gnus-alter-header-function nil
926   "Function called to allow alteration of article header structures.
927 The function is called with one parameter, the article header vector,
928 which it may alter in any way.")
929
930 (defvar gnus-decode-encoded-word-function
931   (mime-find-field-decoder 'From 'nov)
932   "Variable that says which function should be used to decode a string with encoded words.")
933
934 (defcustom gnus-extra-headers '(To Newsgroups)
935   "*Extra headers to parse."
936   :version "21.1"
937   :group 'gnus-summary
938   :type '(repeat symbol))
939
940 (defcustom gnus-ignored-from-addresses
941   (and user-mail-address (regexp-quote user-mail-address))
942   "*Regexp of From headers that may be suppressed in favor of To headers."
943   :version "21.1"
944   :group 'gnus-summary
945   :type 'regexp)
946
947 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
948   "List of charsets that should be ignored.
949 When these charsets are used in the \"charset\" parameter, the
950 default charset will be used instead."
951   :version "21.1"
952   :type '(repeat symbol)
953   :group 'gnus-charset)
954
955 (gnus-define-group-parameter
956  ignored-charsets
957  :type list
958  :function-document
959  "Return the ignored charsets of GROUP."
960  :variable gnus-group-ignored-charsets-alist
961  :variable-default
962  '(("alt\\.chinese\\.text" iso-8859-1))
963  :variable-document
964  "Alist of regexps (to match group names) and charsets that should be ignored.
965 When these charsets are used in the \"charset\" parameter, the
966 default charset will be used instead."
967  :variable-group gnus-charset
968  :variable-type '(repeat (cons (regexp :tag "Group")
969                                (repeat symbol)))
970  :parameter-type '(choice :tag "Ignored charsets"
971                           :value nil
972                           (repeat (symbol)))
973  :parameter-document       "\
974 List of charsets that should be ignored.
975
976 When these charsets are used in the \"charset\" parameter, the
977 default charset will be used instead.")
978
979 (defcustom gnus-group-highlight-words-alist nil
980   "Alist of group regexps and highlight regexps.
981 This variable uses the same syntax as `gnus-emphasis-alist'."
982   :version "21.1"
983   :type '(repeat (cons (regexp :tag "Group")
984                        (repeat (list (regexp :tag "Highlight regexp")
985                                      (number :tag "Group for entire word" 0)
986                                      (number :tag "Group for displayed part" 0)
987                                      (symbol :tag "Face"
988                                              gnus-emphasis-highlight-words)))))
989   :group 'gnus-summary-visual)
990
991 (defcustom gnus-use-wheel nil
992   "Use Intelli-mouse on summary movement"
993   :type 'boolean
994   :group 'gnus-summary-maneuvering)
995
996 (defcustom gnus-wheel-scroll-amount '(5 . 1)
997   "Amount to scroll messages by spinning the mouse wheel.
998 This is actually a cons cell, where the first item is the amount to scroll
999 on a normal wheel event, and the second is the amount to scroll when the
1000 wheel is moved with the shift key depressed."
1001   :type '(cons (integer :tag "Shift") integer)
1002   :group 'gnus-summary-maneuvering)
1003
1004 (defcustom gnus-wheel-edge-resistance 2
1005   "How hard it should be to change the current article
1006 by moving the mouse over the edge of the article window."
1007   :type 'integer
1008   :group 'gnus-summary-maneuvering)
1009
1010 (defcustom gnus-summary-show-article-charset-alist
1011   nil
1012   "Alist of number and charset.
1013 The article will be shown with the charset corresponding to the
1014 numbered argument.
1015 For example: ((1 . cn-gb-2312) (2 . big5))."
1016   :version "21.1"
1017   :type '(repeat (cons (number :tag "Argument" 1)
1018                        (symbol :tag "Charset")))
1019   :group 'gnus-charset)
1020
1021 (defcustom gnus-preserve-marks t
1022   "Whether marks are preserved when moving, copying and respooling messages."
1023   :version "21.1"
1024   :type 'boolean
1025   :group 'gnus-summary-marks)
1026
1027 (defcustom gnus-alter-articles-to-read-function nil
1028   "Function to be called to alter the list of articles to be selected."
1029   :type '(choice (const nil) function)
1030   :group 'gnus-summary)
1031
1032 (defcustom gnus-orphan-score nil
1033   "*All orphans get this score added.  Set in the score file."
1034   :group 'gnus-score-default
1035   :type '(choice (const nil)
1036                  integer))
1037
1038 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1039   "*A regexp to match MIME parts when saving multiple parts of a message
1040 with gnus-summary-save-parts (X m). This regexp will be used by default
1041 when prompting the user for which type of files to save."
1042   :group 'gnus-summary
1043   :type 'regexp)
1044
1045
1046 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1047   "*A regexp to match MIME parts when saving multiple parts of a message
1048 with gnus-summary-save-parts (X m). This regexp will be used by default
1049 when prompting the user for which type of files to save."
1050   :group 'gnus-summary
1051   :type 'regexp)
1052
1053 (defcustom gnus-read-all-available-headers nil
1054   "Whether Gnus should parse all headers made available to it.
1055 This is mostly relevant for slow backends where the user may
1056 wish to widen the summary buffer to include all headers
1057 that were fetched.  Say, for nnultimate groups."
1058   :group 'gnus-summary
1059   :type '(choice boolean regexp))
1060
1061 (defcustom gnus-summary-muttprint-program "muttprint"
1062   "Command (and optional arguments) used to run Muttprint."
1063   :group 'gnus-summary
1064   :type 'string)
1065
1066 ;;; Internal variables
1067
1068 (defvar gnus-summary-display-cache nil)
1069 (defvar gnus-article-mime-handles nil)
1070 (defvar gnus-article-decoded-p nil)
1071 (defvar gnus-article-charset nil)
1072 (defvar gnus-article-ignored-charsets nil)
1073 (defvar gnus-scores-exclude-files nil)
1074 (defvar gnus-page-broken nil)
1075 (defvar gnus-inhibit-mime-unbuttonizing nil)
1076
1077 (defvar gnus-original-article nil)
1078 (defvar gnus-article-internal-prepare-hook nil)
1079 (defvar gnus-newsgroup-process-stack nil)
1080
1081 (defvar gnus-thread-indent-array nil)
1082 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1083 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1084   "Function called to sort the articles within a thread after it has been gathered together.")
1085
1086 (defvar gnus-summary-save-parts-type-history nil)
1087 (defvar gnus-summary-save-parts-last-directory nil)
1088
1089 (defvar gnus-summary-save-parts-type-history nil)
1090 (defvar gnus-summary-save-parts-last-directory nil)
1091
1092 ;; Avoid highlighting in kill files.
1093 (defvar gnus-summary-inhibit-highlight nil)
1094 (defvar gnus-newsgroup-selected-overlay nil)
1095 (defvar gnus-inhibit-limiting nil)
1096 (defvar gnus-newsgroup-adaptive-score-file nil)
1097 (defvar gnus-current-score-file nil)
1098 (defvar gnus-current-move-group nil)
1099 (defvar gnus-current-copy-group nil)
1100 (defvar gnus-current-crosspost-group nil)
1101 (defvar gnus-newsgroup-display nil)
1102
1103 (defvar gnus-newsgroup-dependencies nil)
1104 (defvar gnus-newsgroup-adaptive nil)
1105 (defvar gnus-summary-display-article-function nil)
1106 (defvar gnus-summary-highlight-line-function nil
1107   "Function called after highlighting a summary line.")
1108
1109 (defvar gnus-summary-line-format-alist
1110   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1111     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1112     (?s gnus-tmp-subject-or-nil ?s)
1113     (?n gnus-tmp-name ?s)
1114     (?A (std11-address-string
1115          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1116     (?a (or (std11-full-name-string
1117              (car (mime-entity-read-field gnus-tmp-header 'From)))
1118             gnus-tmp-from) ?s)
1119     (?F gnus-tmp-from ?s)
1120     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1121     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1122     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1123     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1124     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1125     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1126     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1127     (?L gnus-tmp-lines ?s)
1128     (?I gnus-tmp-indentation ?s)
1129     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1130     (?R gnus-tmp-replied ?c)
1131     (?\[ gnus-tmp-opening-bracket ?c)
1132     (?\] gnus-tmp-closing-bracket ?c)
1133     (?\> (make-string gnus-tmp-level ? ) ?s)
1134     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1135     (?i gnus-tmp-score ?d)
1136     (?z gnus-tmp-score-char ?c)
1137     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1138     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1139     (?U gnus-tmp-unread ?c)
1140     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1141         ?s)
1142     (?t (gnus-summary-number-of-articles-in-thread
1143          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1144         ?d)
1145     (?e (gnus-summary-number-of-articles-in-thread
1146          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1147         ?c)
1148     (?u gnus-tmp-user-defined ?s)
1149     (?P (gnus-pick-line-number) ?d)
1150     (?B gnus-tmp-thread-tree-header-string ?s)
1151     (user-date (gnus-user-date
1152                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1153   "An alist of format specifications that can appear in summary lines.
1154 These are paired with what variables they correspond with, along with
1155 the type of the variable (string, integer, character, etc).")
1156
1157 (defvar gnus-summary-dummy-line-format-alist
1158   `((?S gnus-tmp-subject ?s)
1159     (?N gnus-tmp-number ?d)
1160     (?u gnus-tmp-user-defined ?s)))
1161
1162 (defvar gnus-summary-mode-line-format-alist
1163   `((?G gnus-tmp-group-name ?s)
1164     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1165     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1166     (?A gnus-tmp-article-number ?d)
1167     (?Z gnus-tmp-unread-and-unselected ?s)
1168     (?V gnus-version ?s)
1169     (?U gnus-tmp-unread-and-unticked ?d)
1170     (?S gnus-tmp-subject ?s)
1171     (?e gnus-tmp-unselected ?d)
1172     (?u gnus-tmp-user-defined ?s)
1173     (?d (length gnus-newsgroup-dormant) ?d)
1174     (?t (length gnus-newsgroup-marked) ?d)
1175     (?r (length gnus-newsgroup-reads) ?d)
1176     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1177     (?E gnus-newsgroup-expunged-tally ?d)
1178     (?s (gnus-current-score-file-nondirectory) ?s)))
1179
1180 (defvar gnus-last-search-regexp nil
1181   "Default regexp for article search command.")
1182
1183 (defvar gnus-summary-search-article-matched-data nil
1184   "Last matched data of article search command.  It is the local variable
1185 in `gnus-article-buffer' which consists of the list of start position,
1186 end position and text.")
1187
1188 (defvar gnus-last-shell-command nil
1189   "Default shell command on article.")
1190
1191 (defvar gnus-newsgroup-begin nil)
1192 (defvar gnus-newsgroup-end nil)
1193 (defvar gnus-newsgroup-last-rmail nil)
1194 (defvar gnus-newsgroup-last-mail nil)
1195 (defvar gnus-newsgroup-last-folder nil)
1196 (defvar gnus-newsgroup-last-file nil)
1197 (defvar gnus-newsgroup-auto-expire nil)
1198 (defvar gnus-newsgroup-active nil)
1199
1200 (defvar gnus-newsgroup-data nil)
1201 (defvar gnus-newsgroup-data-reverse nil)
1202 (defvar gnus-newsgroup-limit nil)
1203 (defvar gnus-newsgroup-limits nil)
1204
1205 (defvar gnus-newsgroup-unreads nil
1206   "List of unread articles in the current newsgroup.")
1207
1208 (defvar gnus-newsgroup-unselected nil
1209   "List of unselected unread articles in the current newsgroup.")
1210
1211 (defvar gnus-newsgroup-reads nil
1212   "Alist of read articles and article marks in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-expunged-tally nil)
1215
1216 (defvar gnus-newsgroup-marked nil
1217   "List of ticked articles in the current newsgroup (a subset of unread art).")
1218
1219 (defvar gnus-newsgroup-killed nil
1220   "List of ranges of articles that have been through the scoring process.")
1221
1222 (defvar gnus-newsgroup-cached nil
1223   "List of articles that come from the article cache.")
1224
1225 (defvar gnus-newsgroup-saved nil
1226   "List of articles that have been saved.")
1227
1228 (defvar gnus-newsgroup-kill-headers nil)
1229
1230 (defvar gnus-newsgroup-replied nil
1231   "List of articles that have been replied to in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-forwarded nil
1234   "List of articles that have been forwarded in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-recent nil
1237   "List of articles that have are recent in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-expirable nil
1240   "List of articles in the current newsgroup that can be expired.")
1241
1242 (defvar gnus-newsgroup-processable nil
1243   "List of articles in the current newsgroup that can be processed.")
1244
1245 (defvar gnus-newsgroup-downloadable nil
1246   "List of articles in the current newsgroup that can be processed.")
1247
1248 (defvar gnus-newsgroup-undownloaded nil
1249   "List of articles in the current newsgroup that haven't been downloaded..")
1250
1251 (defvar gnus-newsgroup-unsendable nil
1252   "List of articles in the current newsgroup that won't be sent.")
1253
1254 (defvar gnus-newsgroup-bookmarks nil
1255   "List of articles in the current newsgroup that have bookmarks.")
1256
1257 (defvar gnus-newsgroup-dormant nil
1258   "List of dormant articles in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-unseen nil
1261   "List of unseen articles in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-seen nil
1264   "Range of seen articles in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-articles nil
1267   "List of articles in the current newsgroup.")
1268
1269 (defvar gnus-newsgroup-scored nil
1270   "List of scored articles in the current newsgroup.")
1271
1272 (defvar gnus-newsgroup-incorporated nil
1273   "List of incorporated articles in the current newsgroup.")
1274
1275 (defvar gnus-newsgroup-headers nil
1276   "List of article headers in the current newsgroup.")
1277
1278 (defvar gnus-newsgroup-threads nil)
1279
1280 (defvar gnus-newsgroup-prepared nil
1281   "Whether the current group has been prepared properly.")
1282
1283 (defvar gnus-newsgroup-ancient nil
1284   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-sparse nil)
1287
1288 (defvar gnus-current-article nil)
1289 (defvar gnus-article-current nil)
1290 (defvar gnus-current-headers nil)
1291 (defvar gnus-have-all-headers nil)
1292 (defvar gnus-last-article nil)
1293 (defvar gnus-newsgroup-history nil)
1294 (defvar gnus-newsgroup-charset nil)
1295 (defvar gnus-newsgroup-ephemeral-charset nil)
1296 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1297
1298 (defvar gnus-article-before-search nil)
1299
1300 (defconst gnus-summary-local-variables
1301   '(gnus-newsgroup-name
1302     gnus-newsgroup-begin gnus-newsgroup-end
1303     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1304     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1305     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1306     gnus-newsgroup-unselected gnus-newsgroup-marked
1307     gnus-newsgroup-reads gnus-newsgroup-saved
1308     gnus-newsgroup-replied gnus-newsgroup-forwarded
1309     gnus-newsgroup-recent
1310     gnus-newsgroup-expirable
1311     gnus-newsgroup-processable gnus-newsgroup-killed
1312     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1313     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1314     gnus-newsgroup-seen gnus-newsgroup-articles
1315     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1316     gnus-newsgroup-headers gnus-newsgroup-threads
1317     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1318     gnus-current-article gnus-current-headers gnus-have-all-headers
1319     gnus-last-article gnus-article-internal-prepare-hook
1320     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1321     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1322     gnus-thread-expunge-below
1323     gnus-score-alist gnus-current-score-file
1324     (gnus-summary-expunge-below . global)
1325     (gnus-summary-mark-below . global)
1326     (gnus-orphan-score . global)
1327     gnus-newsgroup-active gnus-scores-exclude-files
1328     gnus-newsgroup-history gnus-newsgroup-ancient
1329     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1330     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1331     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1332     (gnus-newsgroup-expunged-tally . 0)
1333     gnus-cache-removable-articles gnus-newsgroup-cached
1334     gnus-newsgroup-data gnus-newsgroup-data-reverse
1335     gnus-newsgroup-limit gnus-newsgroup-limits
1336     gnus-newsgroup-charset gnus-newsgroup-display
1337     gnus-newsgroup-incorporated)
1338   "Variables that are buffer-local to the summary buffers.")
1339
1340 (defvar gnus-newsgroup-variables nil
1341   "A list of variables that have separate values in different newsgroups.
1342 A list of newsgroup (summary buffer) local variables, or cons of
1343 variables and their default values (when the default values are not
1344 nil), that should be made global while the summary buffer is active.
1345 These variables can be used to set variables in the group parameters
1346 while still allowing them to affect operations done in other
1347 buffers. For example:
1348
1349 \(setq gnus-newsgroup-variables
1350      '(message-use-followup-to
1351        (gnus-visible-headers .
1352          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1353 ")
1354
1355 ;; Byte-compiler warning.
1356 (eval-when-compile (defvar gnus-article-mode-map))
1357
1358 ;; Subject simplification.
1359
1360 (defun gnus-simplify-whitespace (str)
1361   "Remove excessive whitespace from STR."
1362   (let ((mystr str))
1363     ;; Multiple spaces.
1364     (while (string-match "[ \t][ \t]+" mystr)
1365       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1366                           " "
1367                           (substring mystr (match-end 0)))))
1368     ;; Leading spaces.
1369     (when (string-match "^[ \t]+" mystr)
1370       (setq mystr (substring mystr (match-end 0))))
1371     ;; Trailing spaces.
1372     (when (string-match "[ \t]+$" mystr)
1373       (setq mystr (substring mystr 0 (match-beginning 0))))
1374     mystr))
1375
1376 (defsubst gnus-simplify-subject-re (subject)
1377   "Remove \"Re:\" from subject lines."
1378   (if (string-match message-subject-re-regexp subject)
1379       (substring subject (match-end 0))
1380     subject))
1381
1382 (defun gnus-simplify-subject (subject &optional re-only)
1383   "Remove `Re:' and words in parentheses.
1384 If RE-ONLY is non-nil, strip leading `Re:'s only."
1385   (let ((case-fold-search t))           ;Ignore case.
1386     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1387     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1388       (setq subject (substring subject (match-end 0))))
1389     ;; Remove uninteresting prefixes.
1390     (when (and (not re-only)
1391                gnus-simplify-ignored-prefixes
1392                (string-match gnus-simplify-ignored-prefixes subject))
1393       (setq subject (substring subject (match-end 0))))
1394     ;; Remove words in parentheses from end.
1395     (unless re-only
1396       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1397         (setq subject (substring subject 0 (match-beginning 0)))))
1398     ;; Return subject string.
1399     subject))
1400
1401 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1402 ;; all whitespace.
1403 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1404   (goto-char (point-min))
1405   (while (re-search-forward regexp nil t)
1406     (replace-match (or newtext ""))))
1407
1408 (defun gnus-simplify-buffer-fuzzy ()
1409   "Simplify string in the buffer fuzzily.
1410 The string in the accessible portion of the current buffer is simplified.
1411 It is assumed to be a single-line subject.
1412 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1413 matter is removed.  Additional things can be deleted by setting
1414 `gnus-simplify-subject-fuzzy-regexp'."
1415   (let ((case-fold-search t)
1416         (modified-tick))
1417     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1418
1419     (while (not (eq modified-tick (buffer-modified-tick)))
1420       (setq modified-tick (buffer-modified-tick))
1421       (cond
1422        ((listp gnus-simplify-subject-fuzzy-regexp)
1423         (mapcar 'gnus-simplify-buffer-fuzzy-step
1424                 gnus-simplify-subject-fuzzy-regexp))
1425        (gnus-simplify-subject-fuzzy-regexp
1426         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1427       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1428       (gnus-simplify-buffer-fuzzy-step
1429        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1430       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1431
1432     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1433     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1434     (gnus-simplify-buffer-fuzzy-step " $")
1435     (gnus-simplify-buffer-fuzzy-step "^ +")))
1436
1437 (defun gnus-simplify-subject-fuzzy (subject)
1438   "Simplify a subject string fuzzily.
1439 See `gnus-simplify-buffer-fuzzy' for details."
1440   (save-excursion
1441     (gnus-set-work-buffer)
1442     (let ((case-fold-search t))
1443       ;; Remove uninteresting prefixes.
1444       (when (and gnus-simplify-ignored-prefixes
1445                  (string-match gnus-simplify-ignored-prefixes subject))
1446         (setq subject (substring subject (match-end 0))))
1447       (insert subject)
1448       (inline (gnus-simplify-buffer-fuzzy))
1449       (buffer-string))))
1450
1451 (defsubst gnus-simplify-subject-fully (subject)
1452   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1453   (cond
1454    (gnus-simplify-subject-functions
1455     (gnus-map-function gnus-simplify-subject-functions subject))
1456    ((null gnus-summary-gather-subject-limit)
1457     (gnus-simplify-subject-re subject))
1458    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1459     (gnus-simplify-subject-fuzzy subject))
1460    ((numberp gnus-summary-gather-subject-limit)
1461     (gnus-limit-string (gnus-simplify-subject-re subject)
1462                        gnus-summary-gather-subject-limit))
1463    (t
1464     subject)))
1465
1466 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1467   "Check whether two subjects are equal.
1468 If optional argument simple-first is t, first argument is already
1469 simplified."
1470   (cond
1471    ((null simple-first)
1472     (equal (gnus-simplify-subject-fully s1)
1473            (gnus-simplify-subject-fully s2)))
1474    (t
1475     (equal s1
1476            (gnus-simplify-subject-fully s2)))))
1477
1478 (defun gnus-summary-bubble-group ()
1479   "Increase the score of the current group.
1480 This is a handy function to add to `gnus-summary-exit-hook' to
1481 increase the score of each group you read."
1482   (gnus-group-add-score gnus-newsgroup-name))
1483
1484 \f
1485 ;;;
1486 ;;; Gnus summary mode
1487 ;;;
1488
1489 (put 'gnus-summary-mode 'mode-class 'special)
1490
1491 (defvar gnus-article-commands-menu)
1492
1493 (when t
1494   ;; Non-orthogonal keys
1495
1496   (gnus-define-keys gnus-summary-mode-map
1497     " " gnus-summary-next-page
1498     "\177" gnus-summary-prev-page
1499     [delete] gnus-summary-prev-page
1500     [backspace] gnus-summary-prev-page
1501     "\r" gnus-summary-scroll-up
1502     "\M-\r" gnus-summary-scroll-down
1503     "n" gnus-summary-next-unread-article
1504     "p" gnus-summary-prev-unread-article
1505     "N" gnus-summary-next-article
1506     "P" gnus-summary-prev-article
1507     "\M-\C-n" gnus-summary-next-same-subject
1508     "\M-\C-p" gnus-summary-prev-same-subject
1509     "\M-n" gnus-summary-next-unread-subject
1510     "\M-p" gnus-summary-prev-unread-subject
1511     "." gnus-summary-first-unread-article
1512     "," gnus-summary-best-unread-article
1513     "\M-s" gnus-summary-search-article-forward
1514     "\M-r" gnus-summary-search-article-backward
1515     "<" gnus-summary-beginning-of-article
1516     ">" gnus-summary-end-of-article
1517     "j" gnus-summary-goto-article
1518     "^" gnus-summary-refer-parent-article
1519     "\M-^" gnus-summary-refer-article
1520     "u" gnus-summary-tick-article-forward
1521     "!" gnus-summary-tick-article-forward
1522     "U" gnus-summary-tick-article-backward
1523     "d" gnus-summary-mark-as-read-forward
1524     "D" gnus-summary-mark-as-read-backward
1525     "E" gnus-summary-mark-as-expirable
1526     "\M-u" gnus-summary-clear-mark-forward
1527     "\M-U" gnus-summary-clear-mark-backward
1528     "k" gnus-summary-kill-same-subject-and-select
1529     "\C-k" gnus-summary-kill-same-subject
1530     "\M-\C-k" gnus-summary-kill-thread
1531     "\M-\C-l" gnus-summary-lower-thread
1532     "e" gnus-summary-edit-article
1533     "#" gnus-summary-mark-as-processable
1534     "\M-#" gnus-summary-unmark-as-processable
1535     "\M-\C-t" gnus-summary-toggle-threads
1536     "\M-\C-s" gnus-summary-show-thread
1537     "\M-\C-h" gnus-summary-hide-thread
1538     "\M-\C-f" gnus-summary-next-thread
1539     "\M-\C-b" gnus-summary-prev-thread
1540     [(meta down)] gnus-summary-next-thread
1541     [(meta up)] gnus-summary-prev-thread
1542     "\M-\C-u" gnus-summary-up-thread
1543     "\M-\C-d" gnus-summary-down-thread
1544     "&" gnus-summary-execute-command
1545     "c" gnus-summary-catchup-and-exit
1546     "\C-w" gnus-summary-mark-region-as-read
1547     "\C-t" gnus-summary-toggle-truncation
1548     "?" gnus-summary-mark-as-dormant
1549     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1550     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1551     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1552     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1553     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1554     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1555     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1556     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1557     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1558     "=" gnus-summary-expand-window
1559     "\C-x\C-s" gnus-summary-reselect-current-group
1560     "\M-g" gnus-summary-rescan-group
1561     "w" gnus-summary-stop-page-breaking
1562     "\C-c\C-r" gnus-summary-caesar-message
1563     "\M-t" gnus-summary-toggle-mime
1564     "f" gnus-summary-followup
1565     "F" gnus-summary-followup-with-original
1566     "C" gnus-summary-cancel-article
1567     "r" gnus-summary-reply
1568     "R" gnus-summary-reply-with-original
1569     "\C-c\C-f" gnus-summary-mail-forward
1570     "o" gnus-summary-save-article
1571     "\C-o" gnus-summary-save-article-mail
1572     "|" gnus-summary-pipe-output
1573     "\M-k" gnus-summary-edit-local-kill
1574     "\M-K" gnus-summary-edit-global-kill
1575     ;; "V" gnus-version
1576     "\C-c\C-d" gnus-summary-describe-group
1577     "q" gnus-summary-exit
1578     "Q" gnus-summary-exit-no-update
1579     "\C-c\C-i" gnus-info-find-node
1580     gnus-mouse-2 gnus-mouse-pick-article
1581     "m" gnus-summary-mail-other-window
1582     "a" gnus-summary-post-news
1583     "i" gnus-summary-news-other-window
1584     "x" gnus-summary-limit-to-unread
1585     "s" gnus-summary-isearch-article
1586     "t" gnus-article-toggle-headers
1587     "g" gnus-summary-show-article
1588     "l" gnus-summary-goto-last-article
1589     "v" gnus-summary-preview-mime-message
1590     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1591     "\C-d" gnus-summary-enter-digest-group
1592     "\M-\C-d" gnus-summary-read-document
1593     "\M-\C-e" gnus-summary-edit-parameters
1594     "\M-\C-a" gnus-summary-customize-parameters
1595     "\C-c\C-b" gnus-bug
1596     "\C-c\C-n" gnus-namazu-search
1597     "*" gnus-cache-enter-article
1598     "\M-*" gnus-cache-remove-article
1599     "\M-&" gnus-summary-universal-argument
1600     "\C-l" gnus-recenter
1601     "I" gnus-summary-increase-score
1602     "L" gnus-summary-lower-score
1603     "\M-i" gnus-symbolic-argument
1604     "h" gnus-summary-select-article-buffer
1605
1606     "V" gnus-summary-score-map
1607     "X" gnus-uu-extract-map
1608     "S" gnus-summary-send-map)
1609
1610   ;; Sort of orthogonal keymap
1611   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1612     "t" gnus-summary-tick-article-forward
1613     "!" gnus-summary-tick-article-forward
1614     "d" gnus-summary-mark-as-read-forward
1615     "r" gnus-summary-mark-as-read-forward
1616     "c" gnus-summary-clear-mark-forward
1617     " " gnus-summary-clear-mark-forward
1618     "e" gnus-summary-mark-as-expirable
1619     "x" gnus-summary-mark-as-expirable
1620     "?" gnus-summary-mark-as-dormant
1621     "b" gnus-summary-set-bookmark
1622     "B" gnus-summary-remove-bookmark
1623     "#" gnus-summary-mark-as-processable
1624     "\M-#" gnus-summary-unmark-as-processable
1625     "S" gnus-summary-limit-include-expunged
1626     "C" gnus-summary-catchup
1627     "H" gnus-summary-catchup-to-here
1628     "h" gnus-summary-catchup-from-here
1629     "\C-c" gnus-summary-catchup-all
1630     "k" gnus-summary-kill-same-subject-and-select
1631     "K" gnus-summary-kill-same-subject
1632     "P" gnus-uu-mark-map)
1633
1634   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1635     "c" gnus-summary-clear-above
1636     "u" gnus-summary-tick-above
1637     "m" gnus-summary-mark-above
1638     "k" gnus-summary-kill-below)
1639
1640   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1641     "/" gnus-summary-limit-to-subject
1642     "n" gnus-summary-limit-to-articles
1643     "w" gnus-summary-pop-limit
1644     "s" gnus-summary-limit-to-subject
1645     "a" gnus-summary-limit-to-author
1646     "u" gnus-summary-limit-to-unread
1647     "m" gnus-summary-limit-to-marks
1648     "M" gnus-summary-limit-exclude-marks
1649     "v" gnus-summary-limit-to-score
1650     "*" gnus-summary-limit-include-cached
1651     "D" gnus-summary-limit-include-dormant
1652     "T" gnus-summary-limit-include-thread
1653     "d" gnus-summary-limit-exclude-dormant
1654     "t" gnus-summary-limit-to-age
1655     "x" gnus-summary-limit-to-extra
1656     "p" gnus-summary-limit-to-display-predicate
1657     "E" gnus-summary-limit-include-expunged
1658     "c" gnus-summary-limit-exclude-childless-dormant
1659     "C" gnus-summary-limit-mark-excluded-as-read
1660     "o" gnus-summary-insert-old-articles
1661     "N" gnus-summary-insert-new-articles)
1662
1663   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1664     "n" gnus-summary-next-unread-article
1665     "p" gnus-summary-prev-unread-article
1666     "N" gnus-summary-next-article
1667     "P" gnus-summary-prev-article
1668     "\C-n" gnus-summary-next-same-subject
1669     "\C-p" gnus-summary-prev-same-subject
1670     "\M-n" gnus-summary-next-unread-subject
1671     "\M-p" gnus-summary-prev-unread-subject
1672     "f" gnus-summary-first-unread-article
1673     "b" gnus-summary-best-unread-article
1674     "j" gnus-summary-goto-article
1675     "g" gnus-summary-goto-subject
1676     "l" gnus-summary-goto-last-article
1677     "o" gnus-summary-pop-article)
1678
1679   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1680     "k" gnus-summary-kill-thread
1681     "l" gnus-summary-lower-thread
1682     "i" gnus-summary-raise-thread
1683     "T" gnus-summary-toggle-threads
1684     "t" gnus-summary-rethread-current
1685     "^" gnus-summary-reparent-thread
1686     "s" gnus-summary-show-thread
1687     "S" gnus-summary-show-all-threads
1688     "h" gnus-summary-hide-thread
1689     "H" gnus-summary-hide-all-threads
1690     "n" gnus-summary-next-thread
1691     "p" gnus-summary-prev-thread
1692     "u" gnus-summary-up-thread
1693     "o" gnus-summary-top-thread
1694     "d" gnus-summary-down-thread
1695     "#" gnus-uu-mark-thread
1696     "\M-#" gnus-uu-unmark-thread)
1697
1698   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1699     "g" gnus-summary-prepare
1700     "c" gnus-summary-insert-cached-articles)
1701
1702   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1703     "c" gnus-summary-catchup-and-exit
1704     "C" gnus-summary-catchup-all-and-exit
1705     "E" gnus-summary-exit-no-update
1706     "J" gnus-summary-jump-to-other-group
1707     "Q" gnus-summary-exit
1708     "Z" gnus-summary-exit
1709     "n" gnus-summary-catchup-and-goto-next-group
1710     "R" gnus-summary-reselect-current-group
1711     "G" gnus-summary-rescan-group
1712     "N" gnus-summary-next-group
1713     "s" gnus-summary-save-newsrc
1714     "P" gnus-summary-prev-group)
1715
1716   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1717     " " gnus-summary-next-page
1718     "n" gnus-summary-next-page
1719     "\177" gnus-summary-prev-page
1720     [delete] gnus-summary-prev-page
1721     "p" gnus-summary-prev-page
1722     "\r" gnus-summary-scroll-up
1723     "\M-\r" gnus-summary-scroll-down
1724     "<" gnus-summary-beginning-of-article
1725     ">" gnus-summary-end-of-article
1726     "b" gnus-summary-beginning-of-article
1727     "e" gnus-summary-end-of-article
1728     "^" gnus-summary-refer-parent-article
1729     "r" gnus-summary-refer-parent-article
1730     "D" gnus-summary-enter-digest-group
1731     "R" gnus-summary-refer-references
1732     "T" gnus-summary-refer-thread
1733     "g" gnus-summary-show-article
1734     "s" gnus-summary-isearch-article
1735     "P" gnus-summary-print-article
1736     "M" gnus-mailing-list-insinuate
1737     "t" gnus-article-babel)
1738
1739   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1740     "b" gnus-article-add-buttons
1741     "B" gnus-article-add-buttons-to-head
1742     "o" gnus-article-treat-overstrike
1743     "e" gnus-article-emphasize
1744     "w" gnus-article-fill-cited-article
1745     "Q" gnus-article-fill-long-lines
1746     "C" gnus-article-capitalize-sentences
1747     "c" gnus-article-remove-cr
1748     "Z" gnus-article-decode-HZ
1749     "f" gnus-article-display-x-face
1750     "l" gnus-summary-stop-page-breaking
1751     "r" gnus-summary-caesar-message
1752     "t" gnus-article-toggle-headers
1753     "g" gnus-treat-smiley
1754     "v" gnus-summary-verbose-headers
1755     "m" gnus-summary-toggle-mime
1756     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1757     "p" gnus-article-verify-x-pgp-sig
1758     "d" gnus-article-treat-dumbquotes)
1759
1760   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1761     "a" gnus-article-hide
1762     "h" gnus-article-toggle-headers
1763     "b" gnus-article-hide-boring-headers
1764     "s" gnus-article-hide-signature
1765     "c" gnus-article-hide-citation
1766     "C" gnus-article-hide-citation-in-followups
1767     "l" gnus-article-hide-list-identifiers
1768     "p" gnus-article-hide-pgp
1769     "B" gnus-article-strip-banner
1770     "P" gnus-article-hide-pem
1771     "\C-c" gnus-article-hide-citation-maybe)
1772
1773   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1774     "a" gnus-article-highlight
1775     "h" gnus-article-highlight-headers
1776     "c" gnus-article-highlight-citation
1777     "s" gnus-article-highlight-signature)
1778
1779   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1780     "f" gnus-article-treat-fold-headers
1781     "u" gnus-article-treat-unfold-headers
1782     "n" gnus-article-treat-fold-newsgroups)
1783
1784   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1785     "x" gnus-article-display-x-face
1786     "s" gnus-treat-smiley
1787     "D" gnus-article-remove-images
1788     "f" gnus-treat-from-picon
1789     "m" gnus-treat-mail-picon
1790     "n" gnus-treat-newsgroups-picon)
1791
1792   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1793     "z" gnus-article-date-ut
1794     "u" gnus-article-date-ut
1795     "l" gnus-article-date-local
1796     "p" gnus-article-date-english
1797     "e" gnus-article-date-lapsed
1798     "o" gnus-article-date-original
1799     "i" gnus-article-date-iso8601
1800     "s" gnus-article-date-user)
1801
1802   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1803     "t" gnus-article-remove-trailing-blank-lines
1804     "l" gnus-article-strip-leading-blank-lines
1805     "m" gnus-article-strip-multiple-blank-lines
1806     "a" gnus-article-strip-blank-lines
1807     "A" gnus-article-strip-all-blank-lines
1808     "s" gnus-article-strip-leading-space
1809     "e" gnus-article-strip-trailing-space
1810     "w" gnus-article-remove-leading-whitespace)
1811
1812   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1813     "v" gnus-version
1814     "f" gnus-summary-fetch-faq
1815     "d" gnus-summary-describe-group
1816     "h" gnus-summary-describe-briefly
1817     "i" gnus-info-find-node)
1818
1819   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1820     "e" gnus-summary-expire-articles
1821     "\M-\C-e" gnus-summary-expire-articles-now
1822     "\177" gnus-summary-delete-article
1823     [delete] gnus-summary-delete-article
1824     [backspace] gnus-summary-delete-article
1825     "m" gnus-summary-move-article
1826     "r" gnus-summary-respool-article
1827     "w" gnus-summary-edit-article
1828     "c" gnus-summary-copy-article
1829     "B" gnus-summary-crosspost-article
1830     "q" gnus-summary-respool-query
1831     "t" gnus-summary-respool-trace
1832     "i" gnus-summary-import-article
1833     "I" gnus-summary-create-article
1834     "p" gnus-summary-article-posted-p)
1835
1836   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1837     "o" gnus-summary-save-article
1838     "m" gnus-summary-save-article-mail
1839     "F" gnus-summary-write-article-file
1840     "r" gnus-summary-save-article-rmail
1841     "f" gnus-summary-save-article-file
1842     "b" gnus-summary-save-article-body-file
1843     "h" gnus-summary-save-article-folder
1844     "v" gnus-summary-save-article-vm
1845     "p" gnus-summary-pipe-output
1846     "P" gnus-summary-muttprint
1847     "s" gnus-soup-add-article)
1848
1849   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1850     "b" gnus-summary-display-buttonized
1851     "m" gnus-summary-repair-multipart
1852     "v" gnus-article-view-part
1853     "o" gnus-article-save-part
1854     "c" gnus-article-copy-part
1855     "C" gnus-article-view-part-as-charset
1856     "e" gnus-article-externalize-part
1857     "E" gnus-article-encrypt-body
1858     "i" gnus-article-inline-part
1859     "|" gnus-article-pipe-part))
1860
1861 (defvar gnus-article-post-menu nil)
1862
1863 (defun gnus-summary-make-menu-bar ()
1864   (gnus-turn-off-edit-menu 'summary)
1865
1866   (unless (boundp 'gnus-summary-misc-menu)
1867
1868     (easy-menu-define
1869      gnus-summary-kill-menu gnus-summary-mode-map ""
1870      (cons
1871       "Score"
1872       (nconc
1873        (list
1874         ["Customize" gnus-score-customize t])
1875        (gnus-make-score-map 'increase)
1876        (gnus-make-score-map 'lower)
1877        '(("Mark"
1878           ["Kill below" gnus-summary-kill-below t]
1879           ["Mark above" gnus-summary-mark-above t]
1880           ["Tick above" gnus-summary-tick-above t]
1881           ["Clear above" gnus-summary-clear-above t])
1882          ["Current score" gnus-summary-current-score t]
1883          ["Set score" gnus-summary-set-score t]
1884          ["Switch current score file..." gnus-score-change-score-file t]
1885          ["Set mark below..." gnus-score-set-mark-below t]
1886          ["Set expunge below..." gnus-score-set-expunge-below t]
1887          ["Edit current score file" gnus-score-edit-current-scores t]
1888          ["Edit score file" gnus-score-edit-file t]
1889          ["Trace score" gnus-score-find-trace t]
1890          ["Find words" gnus-score-find-favourite-words t]
1891          ["Rescore buffer" gnus-summary-rescore t]
1892          ["Increase score..." gnus-summary-increase-score t]
1893          ["Lower score..." gnus-summary-lower-score t]))))
1894
1895     ;; Define both the Article menu in the summary buffer and the
1896     ;; equivalent Commands menu in the article buffer here for
1897     ;; consistency.
1898     (let ((innards
1899            `(("Hide"
1900               ["All" gnus-article-hide t]
1901               ["Headers" gnus-article-toggle-headers t]
1902               ["Signature" gnus-article-hide-signature t]
1903               ["Citation" gnus-article-hide-citation t]
1904               ["List identifiers" gnus-article-hide-list-identifiers t]
1905               ["PGP" gnus-article-hide-pgp t]
1906               ["Banner" gnus-article-strip-banner t]
1907               ["Boring headers" gnus-article-hide-boring-headers t])
1908              ("Highlight"
1909               ["All" gnus-article-highlight t]
1910               ["Headers" gnus-article-highlight-headers t]
1911               ["Signature" gnus-article-highlight-signature t]
1912               ["Citation" gnus-article-highlight-citation t])
1913              ("Date"
1914               ["Local" gnus-article-date-local t]
1915               ["ISO8601" gnus-article-date-iso8601 t]
1916               ["UT" gnus-article-date-ut t]
1917               ["Original" gnus-article-date-original t]
1918               ["Lapsed" gnus-article-date-lapsed t]
1919               ["User-defined" gnus-article-date-user t])
1920              ("Display"
1921               ["Remove images" gnus-article-remove-images t]
1922               ["Toggle smiley" gnus-treat-smiley t]
1923               ["Show X-Face" gnus-article-display-x-face t]
1924               ["Show picons in From" gnus-treat-from-picon t]
1925               ["Show picons in mail headers" gnus-treat-mail-picon t]
1926               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1927              ("Washing"
1928               ("Remove Blanks"
1929                ["Leading" gnus-article-strip-leading-blank-lines t]
1930                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1931                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1932                ["All of the above" gnus-article-strip-blank-lines t]
1933                ["All" gnus-article-strip-all-blank-lines t]
1934                ["Leading space" gnus-article-strip-leading-space t]
1935                ["Trailing space" gnus-article-strip-trailing-space t]
1936                ["Leading space in headers"
1937                 gnus-article-remove-leading-whitespace t])
1938               ["Overstrike" gnus-article-treat-overstrike t]
1939               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1940               ["Emphasis" gnus-article-emphasize t]
1941               ["Word wrap" gnus-article-fill-cited-article t]
1942               ["Fill long lines" gnus-article-fill-long-lines t]
1943               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1944               ["CR" gnus-article-remove-cr t]
1945               ["Rot 13" gnus-summary-caesar-message
1946                ,@(if (featurep 'xemacs) '(t)
1947                    '(:help "\"Caesar rotate\" article by 13"))]
1948               ["Unix pipe" gnus-summary-pipe-message t]
1949               ["Add buttons" gnus-article-add-buttons t]
1950               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1951               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1952               ["Toggle MIME" gnus-summary-toggle-mime t]
1953               ["Verbose header" gnus-summary-verbose-headers t]
1954               ["Toggle header" gnus-summary-toggle-header t]
1955               ["Unfold headers" gnus-article-treat-unfold-headers t]
1956               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1957               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1958               ["HZ" gnus-article-decode-HZ t])
1959              ("Output"
1960               ["Save in default format" gnus-summary-save-article
1961                ,@(if (featurep 'xemacs) '(t)
1962                    '(:help "Save article using default method"))]
1963               ["Save in file" gnus-summary-save-article-file
1964                ,@(if (featurep 'xemacs) '(t)
1965                    '(:help "Save article in file"))]
1966               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1967               ["Save in MH folder" gnus-summary-save-article-folder t]
1968               ["Save in VM folder" gnus-summary-save-article-vm t]
1969               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1970               ["Save body in file" gnus-summary-save-article-body-file t]
1971               ["Pipe through a filter" gnus-summary-pipe-output t]
1972               ["Add to SOUP packet" gnus-soup-add-article t]
1973               ["Print with Muttprint" gnus-summary-muttprint t]
1974               ["Print" gnus-summary-print-article t])
1975              ("Backend"
1976               ["Respool article..." gnus-summary-respool-article t]
1977               ["Move article..." gnus-summary-move-article
1978                (gnus-check-backend-function
1979                 'request-move-article gnus-newsgroup-name)]
1980               ["Copy article..." gnus-summary-copy-article t]
1981               ["Crosspost article..." gnus-summary-crosspost-article
1982                (gnus-check-backend-function
1983                 'request-replace-article gnus-newsgroup-name)]
1984               ["Import file..." gnus-summary-import-article t]
1985               ["Create article..." gnus-summary-create-article t]
1986               ["Check if posted" gnus-summary-article-posted-p t]
1987               ["Edit article" gnus-summary-edit-article
1988                (not (gnus-group-read-only-p))]
1989               ["Delete article" gnus-summary-delete-article
1990                (gnus-check-backend-function
1991                 'request-expire-articles gnus-newsgroup-name)]
1992               ["Query respool" gnus-summary-respool-query t]
1993               ["Trace respool" gnus-summary-respool-trace t]
1994               ["Delete expirable articles" gnus-summary-expire-articles-now
1995                (gnus-check-backend-function
1996                 'request-expire-articles gnus-newsgroup-name)])
1997              ("Extract"
1998               ["Uudecode" gnus-uu-decode-uu
1999                ,@(if (featurep 'xemacs) '(t)
2000                    '(:help "Decode uuencoded article(s)"))]
2001               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2002               ["Unshar" gnus-uu-decode-unshar t]
2003               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2004               ["Save" gnus-uu-decode-save t]
2005               ["Binhex" gnus-uu-decode-binhex t]
2006               ["Postscript" gnus-uu-decode-postscript t])
2007              ("Cache"
2008               ["Enter article" gnus-cache-enter-article t]
2009               ["Remove article" gnus-cache-remove-article t])
2010              ["Translate" gnus-article-babel t]
2011              ["Select article buffer" gnus-summary-select-article-buffer t]
2012              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2013              ["Isearch article..." gnus-summary-isearch-article t]
2014              ["Beginning of the article" gnus-summary-beginning-of-article t]
2015              ["End of the article" gnus-summary-end-of-article t]
2016              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2017              ["Fetch referenced articles" gnus-summary-refer-references t]
2018              ["Fetch current thread" gnus-summary-refer-thread t]
2019              ["Fetch article with id..." gnus-summary-refer-article t]
2020              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2021              ["Redisplay" gnus-summary-show-article t]
2022              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2023       (easy-menu-define
2024        gnus-summary-article-menu gnus-summary-mode-map ""
2025        (cons "Article" innards))
2026
2027       (if (not (keymapp gnus-summary-article-menu))
2028           (easy-menu-define
2029            gnus-article-commands-menu gnus-article-mode-map ""
2030            (cons "Commands" innards))
2031         ;; in Emacs, don't share menu.
2032         (setq gnus-article-commands-menu
2033               (copy-keymap gnus-summary-article-menu))
2034         (define-key gnus-article-mode-map [menu-bar commands]
2035           (cons "Commands" gnus-article-commands-menu))))
2036
2037     (easy-menu-define
2038      gnus-summary-thread-menu gnus-summary-mode-map ""
2039      '("Threads"
2040        ["Toggle threading" gnus-summary-toggle-threads t]
2041        ["Hide threads" gnus-summary-hide-all-threads t]
2042        ["Show threads" gnus-summary-show-all-threads t]
2043        ["Hide thread" gnus-summary-hide-thread t]
2044        ["Show thread" gnus-summary-show-thread t]
2045        ["Go to next thread" gnus-summary-next-thread t]
2046        ["Go to previous thread" gnus-summary-prev-thread t]
2047        ["Go down thread" gnus-summary-down-thread t]
2048        ["Go up thread" gnus-summary-up-thread t]
2049        ["Top of thread" gnus-summary-top-thread t]
2050        ["Mark thread as read" gnus-summary-kill-thread t]
2051        ["Lower thread score" gnus-summary-lower-thread t]
2052        ["Raise thread score" gnus-summary-raise-thread t]
2053        ["Rethread current" gnus-summary-rethread-current t]))
2054
2055     (easy-menu-define
2056      gnus-summary-post-menu gnus-summary-mode-map ""
2057      `("Post"
2058        ["Send a message (mail or news)" gnus-summary-post-news
2059         ,@(if (featurep 'xemacs) '(t)
2060             '(:help "Post an article"))]
2061        ["Followup" gnus-summary-followup
2062         ,@(if (featurep 'xemacs) '(t)
2063             '(:help "Post followup to this article"))]
2064        ["Followup and yank" gnus-summary-followup-with-original
2065         ,@(if (featurep 'xemacs) '(t)
2066             '(:help "Post followup to this article, quoting its contents"))]
2067        ["Supersede article" gnus-summary-supersede-article t]
2068        ["Cancel article" gnus-summary-cancel-article
2069         ,@(if (featurep 'xemacs) '(t)
2070             '(:help "Cancel an article you posted"))]
2071        ["Reply" gnus-summary-reply t]
2072        ["Reply and yank" gnus-summary-reply-with-original t]
2073        ["Wide reply" gnus-summary-wide-reply t]
2074        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2075         ,@(if (featurep 'xemacs) '(t)
2076             '(:help "Mail a reply, quoting this article"))]
2077        ["Very wide reply" gnus-summary-very-wide-reply t]
2078        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2079         ,@(if (featurep 'xemacs) '(t)
2080             '(:help "Mail a very wide reply, quoting this article"))]
2081        ["Mail forward" gnus-summary-mail-forward t]
2082        ["Post forward" gnus-summary-post-forward t]
2083        ["Digest and mail" gnus-summary-digest-mail-forward t]
2084        ["Digest and post" gnus-summary-digest-post-forward t]
2085        ["Resend message" gnus-summary-resend-message t]
2086        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2087        ["Send a mail" gnus-summary-mail-other-window t]
2088        ["Create a local message" gnus-summary-news-other-window t]
2089        ["Uuencode and post" gnus-uu-post-news
2090         ,@(if (featurep 'xemacs) '(t)
2091             '(:help "Post a uuencoded article"))]
2092        ["Followup via news" gnus-summary-followup-to-mail t]
2093        ["Followup via news and yank"
2094         gnus-summary-followup-to-mail-with-original t]
2095        ;;("Draft"
2096        ;;["Send" gnus-summary-send-draft t]
2097        ;;["Send bounced" gnus-resend-bounced-mail t])
2098        ))
2099
2100     (cond
2101      ((not (keymapp gnus-summary-post-menu))
2102       (setq gnus-article-post-menu gnus-summary-post-menu))
2103      ((not gnus-article-post-menu)
2104       ;; Don't share post menu.
2105       (setq gnus-article-post-menu
2106             (copy-keymap gnus-summary-post-menu))))
2107     (define-key gnus-article-mode-map [menu-bar post]
2108       (cons "Post" gnus-article-post-menu))
2109
2110     (easy-menu-define
2111      gnus-summary-misc-menu gnus-summary-mode-map ""
2112      `("Gnus"
2113        ("Mark Read"
2114         ["Mark as read" gnus-summary-mark-as-read-forward t]
2115         ["Mark same subject and select"
2116          gnus-summary-kill-same-subject-and-select t]
2117         ["Mark same subject" gnus-summary-kill-same-subject t]
2118         ["Catchup" gnus-summary-catchup
2119          ,@(if (featurep 'xemacs) '(t)
2120              '(:help "Mark unread articles in this group as read"))]
2121         ["Catchup all" gnus-summary-catchup-all t]
2122         ["Catchup to here" gnus-summary-catchup-to-here t]
2123         ["Catchup from here" gnus-summary-catchup-from-here t]
2124         ["Catchup region" gnus-summary-mark-region-as-read t]
2125         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2126        ("Mark Various"
2127         ["Tick" gnus-summary-tick-article-forward t]
2128         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2129         ["Remove marks" gnus-summary-clear-mark-forward t]
2130         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2131         ["Set bookmark" gnus-summary-set-bookmark t]
2132         ["Remove bookmark" gnus-summary-remove-bookmark t])
2133        ("Limit to"
2134         ["Marks..." gnus-summary-limit-to-marks t]
2135         ["Subject..." gnus-summary-limit-to-subject t]
2136         ["Author..." gnus-summary-limit-to-author t]
2137         ["Age..." gnus-summary-limit-to-age t]
2138         ["Extra..." gnus-summary-limit-to-extra t]
2139         ["Score" gnus-summary-limit-to-score t]
2140         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2141         ["Unread" gnus-summary-limit-to-unread t]
2142         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2143         ["Articles" gnus-summary-limit-to-articles t]
2144         ["Pop limit" gnus-summary-pop-limit t]
2145         ["Show dormant" gnus-summary-limit-include-dormant t]
2146         ["Hide childless dormant"
2147          gnus-summary-limit-exclude-childless-dormant t]
2148         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2149         ["Hide marked" gnus-summary-limit-exclude-marks t]
2150         ["Show expunged" gnus-summary-limit-include-expunged t])
2151        ("Process Mark"
2152         ["Set mark" gnus-summary-mark-as-processable t]
2153         ["Remove mark" gnus-summary-unmark-as-processable t]
2154         ["Remove all marks" gnus-summary-unmark-all-processable t]
2155         ["Mark above" gnus-uu-mark-over t]
2156         ["Mark series" gnus-uu-mark-series t]
2157         ["Mark region" gnus-uu-mark-region t]
2158         ["Unmark region" gnus-uu-unmark-region t]
2159         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2160         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2161         ["Mark all" gnus-uu-mark-all t]
2162         ["Mark buffer" gnus-uu-mark-buffer t]
2163         ["Mark sparse" gnus-uu-mark-sparse t]
2164         ["Mark thread" gnus-uu-mark-thread t]
2165         ["Unmark thread" gnus-uu-unmark-thread t]
2166         ("Process Mark Sets"
2167          ["Kill" gnus-summary-kill-process-mark t]
2168          ["Yank" gnus-summary-yank-process-mark
2169           gnus-newsgroup-process-stack]
2170          ["Save" gnus-summary-save-process-mark t]))
2171        ("Scroll article"
2172         ["Page forward" gnus-summary-next-page
2173          ,@(if (featurep 'xemacs) '(t)
2174              '(:help "Show next page of article"))]
2175         ["Page backward" gnus-summary-prev-page
2176          ,@(if (featurep 'xemacs) '(t)
2177              '(:help "Show previous page of article"))]
2178         ["Line forward" gnus-summary-scroll-up t])
2179        ("Move"
2180         ["Next unread article" gnus-summary-next-unread-article t]
2181         ["Previous unread article" gnus-summary-prev-unread-article t]
2182         ["Next article" gnus-summary-next-article t]
2183         ["Previous article" gnus-summary-prev-article t]
2184         ["Next unread subject" gnus-summary-next-unread-subject t]
2185         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2186         ["Next article same subject" gnus-summary-next-same-subject t]
2187         ["Previous article same subject" gnus-summary-prev-same-subject t]
2188         ["First unread article" gnus-summary-first-unread-article t]
2189         ["Best unread article" gnus-summary-best-unread-article t]
2190         ["Go to subject number..." gnus-summary-goto-subject t]
2191         ["Go to article number..." gnus-summary-goto-article t]
2192         ["Go to the last article" gnus-summary-goto-last-article t]
2193         ["Pop article off history" gnus-summary-pop-article t])
2194        ("Sort"
2195         ["Sort by number" gnus-summary-sort-by-number t]
2196         ["Sort by author" gnus-summary-sort-by-author t]
2197         ["Sort by subject" gnus-summary-sort-by-subject t]
2198         ["Sort by date" gnus-summary-sort-by-date t]
2199         ["Sort by score" gnus-summary-sort-by-score t]
2200         ["Sort by lines" gnus-summary-sort-by-lines t]
2201         ["Sort by characters" gnus-summary-sort-by-chars t]
2202         ["Original sort" gnus-summary-sort-by-original t])
2203        ("Help"
2204         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2205         ["Describe group" gnus-summary-describe-group t]
2206         ["Read manual" gnus-info-find-node t])
2207        ("Modes"
2208         ["Pick and read" gnus-pick-mode t]
2209         ["Binary" gnus-binary-mode t])
2210        ("Regeneration"
2211         ["Regenerate" gnus-summary-prepare t]
2212         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2213         ["Toggle threading" gnus-summary-toggle-threads t])
2214        ["See old articles" gnus-summary-insert-old-articles t]
2215        ["See new articles" gnus-summary-insert-new-articles t]
2216        ["Filter articles..." gnus-summary-execute-command t]
2217        ["Run command on subjects..." gnus-summary-universal-argument t]
2218        ["Search articles forward..." gnus-summary-search-article-forward t]
2219        ["Search articles backward..." gnus-summary-search-article-backward t]
2220        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2221        ["Expand window" gnus-summary-expand-window t]
2222        ["Expire expirable articles" gnus-summary-expire-articles
2223         (gnus-check-backend-function
2224          'request-expire-articles gnus-newsgroup-name)]
2225        ["Edit local kill file" gnus-summary-edit-local-kill t]
2226        ["Edit main kill file" gnus-summary-edit-global-kill t]
2227        ["Edit group parameters" gnus-summary-edit-parameters t]
2228        ["Customize group parameters" gnus-summary-customize-parameters t]
2229        ["Send a bug report" gnus-bug t]
2230        ("Exit"
2231         ["Catchup and exit" gnus-summary-catchup-and-exit
2232          ,@(if (featurep 'xemacs) '(t)
2233              '(:help "Mark unread articles in this group as read, then exit"))]
2234         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2235         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2236         ["Exit group" gnus-summary-exit
2237          ,@(if (featurep 'xemacs) '(t)
2238              '(:help "Exit current group, return to group selection mode"))]
2239         ["Exit group without updating" gnus-summary-exit-no-update t]
2240         ["Exit and goto next group" gnus-summary-next-group t]
2241         ["Exit and goto prev group" gnus-summary-prev-group t]
2242         ["Reselect group" gnus-summary-reselect-current-group t]
2243         ["Rescan group" gnus-summary-rescan-group t]
2244         ["Update dribble" gnus-summary-save-newsrc t])))
2245
2246     (gnus-run-hooks 'gnus-summary-menu-hook)))
2247
2248 (defvar gnus-summary-tool-bar-map nil)
2249
2250 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2251 (defun gnus-summary-make-tool-bar ()
2252   (if (and (fboundp 'tool-bar-add-item-from-menu)
2253            (default-value 'tool-bar-mode)
2254            (not gnus-summary-tool-bar-map))
2255       (setq gnus-summary-tool-bar-map
2256             (let ((tool-bar-map (make-sparse-keymap))
2257                   (load-path (mm-image-load-path)))
2258               (tool-bar-add-item-from-menu
2259                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2260               (tool-bar-add-item-from-menu
2261                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2262               (tool-bar-add-item-from-menu
2263                'gnus-summary-post-news "post" gnus-summary-mode-map)
2264               (tool-bar-add-item-from-menu
2265                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2266               (tool-bar-add-item-from-menu
2267                'gnus-summary-followup "followup" gnus-summary-mode-map)
2268               (tool-bar-add-item-from-menu
2269                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2270               (tool-bar-add-item-from-menu
2271                'gnus-summary-reply "reply" gnus-summary-mode-map)
2272               (tool-bar-add-item-from-menu
2273                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2274               (tool-bar-add-item-from-menu
2275                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2276               (tool-bar-add-item-from-menu
2277                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2278               (tool-bar-add-item-from-menu
2279                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2280               (tool-bar-add-item-from-menu
2281                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2282               (tool-bar-add-item-from-menu
2283                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2284               (tool-bar-add-item-from-menu
2285                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2286               (tool-bar-add-item-from-menu
2287                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2288               tool-bar-map)))
2289   (if gnus-summary-tool-bar-map
2290       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2291
2292 (defun gnus-score-set-default (var value)
2293   "A version of set that updates the GNU Emacs menu-bar."
2294   (set var value)
2295   ;; It is the message that forces the active status to be updated.
2296   (message ""))
2297
2298 (defun gnus-make-score-map (type)
2299   "Make a summary score map of type TYPE."
2300   (if t
2301       nil
2302     (let ((headers '(("author" "from" string)
2303                      ("subject" "subject" string)
2304                      ("article body" "body" string)
2305                      ("article head" "head" string)
2306                      ("xref" "xref" string)
2307                      ("extra header" "extra" string)
2308                      ("lines" "lines" number)
2309                      ("followups to author" "followup" string)))
2310           (types '((number ("less than" <)
2311                            ("greater than" >)
2312                            ("equal" =))
2313                    (string ("substring" s)
2314                            ("exact string" e)
2315                            ("fuzzy string" f)
2316                            ("regexp" r))))
2317           (perms '(("temporary" (current-time-string))
2318                    ("permanent" nil)
2319                    ("immediate" now)))
2320           header)
2321       (list
2322        (apply
2323         'nconc
2324         (list
2325          (if (eq type 'lower)
2326              "Lower score"
2327            "Increase score"))
2328         (let (outh)
2329           (while headers
2330             (setq header (car headers))
2331             (setq outh
2332                   (cons
2333                    (apply
2334                     'nconc
2335                     (list (car header))
2336                     (let ((ts (cdr (assoc (nth 2 header) types)))
2337                           outt)
2338                       (while ts
2339                         (setq outt
2340                               (cons
2341                                (apply
2342                                 'nconc
2343                                 (list (caar ts))
2344                                 (let ((ps perms)
2345                                       outp)
2346                                   (while ps
2347                                     (setq outp
2348                                           (cons
2349                                            (vector
2350                                             (caar ps)
2351                                             (list
2352                                              'gnus-summary-score-entry
2353                                              (nth 1 header)
2354                                              (if (or (string= (nth 1 header)
2355                                                               "head")
2356                                                      (string= (nth 1 header)
2357                                                               "body"))
2358                                                  ""
2359                                                (list 'gnus-summary-header
2360                                                      (nth 1 header)))
2361                                              (list 'quote (nth 1 (car ts)))
2362                                              (list 'gnus-score-delta-default
2363                                                    nil)
2364                                              (nth 1 (car ps))
2365                                              t)
2366                                             t)
2367                                            outp))
2368                                     (setq ps (cdr ps)))
2369                                   (list (nreverse outp))))
2370                                outt))
2371                         (setq ts (cdr ts)))
2372                       (list (nreverse outt))))
2373                    outh))
2374             (setq headers (cdr headers)))
2375           (list (nreverse outh))))))))
2376
2377 \f
2378
2379 (defun gnus-summary-mode (&optional group)
2380   "Major mode for reading articles.
2381
2382 All normal editing commands are switched off.
2383 \\<gnus-summary-mode-map>
2384 Each line in this buffer represents one article.  To read an
2385 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2386 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2387 respectively.
2388
2389 You can also post articles and send mail from this buffer.  To
2390 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2391 of an article, type `\\[gnus-summary-reply]'.
2392
2393 There are approx. one gazillion commands you can execute in this
2394 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2395
2396 The following commands are available:
2397
2398 \\{gnus-summary-mode-map}"
2399   (interactive)
2400   (kill-all-local-variables)
2401   (when (gnus-visual-p 'summary-menu 'menu)
2402     (gnus-summary-make-menu-bar)
2403     (gnus-summary-make-tool-bar))
2404   (gnus-summary-make-local-variables)
2405   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2406     (gnus-summary-make-local-variables))
2407   (gnus-make-thread-indent-array)
2408   (gnus-simplify-mode-line)
2409   (setq major-mode 'gnus-summary-mode)
2410   (setq mode-name "Summary")
2411   (make-local-variable 'minor-mode-alist)
2412   (use-local-map gnus-summary-mode-map)
2413   (buffer-disable-undo)
2414   (setq buffer-read-only t)             ;Disable modification
2415   (setq truncate-lines t)
2416   (setq selective-display t)
2417   (setq selective-display-ellipses t)   ;Display `...'
2418   (gnus-summary-set-display-table)
2419   (gnus-set-default-directory)
2420   (setq gnus-newsgroup-name group)
2421   (unless (gnus-news-group-p group)
2422     (setq gnus-newsgroup-incorporated
2423           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2424   (make-local-variable 'gnus-summary-line-format)
2425   (make-local-variable 'gnus-summary-line-format-spec)
2426   (make-local-variable 'gnus-summary-dummy-line-format)
2427   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2428   (make-local-variable 'gnus-summary-mark-positions)
2429   (make-local-hook 'pre-command-hook)
2430   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2431   (gnus-run-hooks 'gnus-summary-mode-hook)
2432   (turn-on-gnus-mailing-list-mode)
2433   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2434   (gnus-update-summary-mark-positions))
2435
2436 (defun gnus-summary-make-local-variables ()
2437   "Make all the local summary buffer variables."
2438   (let (global)
2439     (dolist (local gnus-summary-local-variables)
2440       (if (consp local)
2441           (progn
2442             (if (eq (cdr local) 'global)
2443                 ;; Copy the global value of the variable.
2444                 (setq global (symbol-value (car local)))
2445               ;; Use the value from the list.
2446               (setq global (eval (cdr local))))
2447             (set (make-local-variable (car local)) global))
2448         ;; Simple nil-valued local variable.
2449         (set (make-local-variable local) nil)))))
2450
2451 (defun gnus-summary-clear-local-variables ()
2452   (let ((locals gnus-summary-local-variables))
2453     (while locals
2454       (if (consp (car locals))
2455           (and (vectorp (caar locals))
2456                (set (caar locals) nil))
2457         (and (vectorp (car locals))
2458              (set (car locals) nil)))
2459       (setq locals (cdr locals)))))
2460
2461 ;; Summary data functions.
2462
2463 (defmacro gnus-data-number (data)
2464   `(car ,data))
2465
2466 (defmacro gnus-data-set-number (data number)
2467   `(setcar ,data ,number))
2468
2469 (defmacro gnus-data-mark (data)
2470   `(nth 1 ,data))
2471
2472 (defmacro gnus-data-set-mark (data mark)
2473   `(setcar (nthcdr 1 ,data) ,mark))
2474
2475 (defmacro gnus-data-pos (data)
2476   `(nth 2 ,data))
2477
2478 (defmacro gnus-data-set-pos (data pos)
2479   `(setcar (nthcdr 2 ,data) ,pos))
2480
2481 (defmacro gnus-data-header (data)
2482   `(nth 3 ,data))
2483
2484 (defmacro gnus-data-set-header (data header)
2485   `(setcar (nthcdr 3 ,data) ,header))
2486
2487 (defmacro gnus-data-level (data)
2488   `(nth 4 ,data))
2489
2490 (defmacro gnus-data-unread-p (data)
2491   `(= (nth 1 ,data) gnus-unread-mark))
2492
2493 (defmacro gnus-data-read-p (data)
2494   `(/= (nth 1 ,data) gnus-unread-mark))
2495
2496 (defmacro gnus-data-pseudo-p (data)
2497   `(consp (nth 3 ,data)))
2498
2499 (defmacro gnus-data-find (number)
2500   `(assq ,number gnus-newsgroup-data))
2501
2502 (defmacro gnus-data-find-list (number &optional data)
2503   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2504      (memq (assq ,number bdata)
2505            bdata)))
2506
2507 (defmacro gnus-data-make (number mark pos header level)
2508   `(list ,number ,mark ,pos ,header ,level))
2509
2510 (defun gnus-data-enter (after-article number mark pos header level offset)
2511   (let ((data (gnus-data-find-list after-article)))
2512     (unless data
2513       (error "No such article: %d" after-article))
2514     (setcdr data (cons (gnus-data-make number mark pos header level)
2515                        (cdr data)))
2516     (setq gnus-newsgroup-data-reverse nil)
2517     (gnus-data-update-list (cddr data) offset)))
2518
2519 (defun gnus-data-enter-list (after-article list &optional offset)
2520   (when list
2521     (let ((data (and after-article (gnus-data-find-list after-article)))
2522           (ilist list))
2523       (if (not (or data
2524                    after-article))
2525           (let ((odata gnus-newsgroup-data))
2526             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2527             (when offset
2528               (gnus-data-update-list odata offset)))
2529         ;; Find the last element in the list to be spliced into the main
2530         ;; list.
2531         (while (cdr list)
2532           (setq list (cdr list)))
2533         (if (not data)
2534             (progn
2535               (setcdr list gnus-newsgroup-data)
2536               (setq gnus-newsgroup-data ilist)
2537               (when offset
2538                 (gnus-data-update-list (cdr list) offset)))
2539           (setcdr list (cdr data))
2540           (setcdr data ilist)
2541           (when offset
2542             (gnus-data-update-list (cdr list) offset))))
2543       (setq gnus-newsgroup-data-reverse nil))))
2544
2545 (defun gnus-data-remove (article &optional offset)
2546   (let ((data gnus-newsgroup-data))
2547     (if (= (gnus-data-number (car data)) article)
2548         (progn
2549           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2550                 gnus-newsgroup-data-reverse nil)
2551           (when offset
2552             (gnus-data-update-list gnus-newsgroup-data offset)))
2553       (while (cdr data)
2554         (when (= (gnus-data-number (cadr data)) article)
2555           (setcdr data (cddr data))
2556           (when offset
2557             (gnus-data-update-list (cdr data) offset))
2558           (setq data nil
2559                 gnus-newsgroup-data-reverse nil))
2560         (setq data (cdr data))))))
2561
2562 (defmacro gnus-data-list (backward)
2563   `(if ,backward
2564        (or gnus-newsgroup-data-reverse
2565            (setq gnus-newsgroup-data-reverse
2566                  (reverse gnus-newsgroup-data)))
2567      gnus-newsgroup-data))
2568
2569 (defun gnus-data-update-list (data offset)
2570   "Add OFFSET to the POS of all data entries in DATA."
2571   (setq gnus-newsgroup-data-reverse nil)
2572   (while data
2573     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2574     (setq data (cdr data))))
2575
2576 (defun gnus-summary-article-pseudo-p (article)
2577   "Say whether this article is a pseudo article or not."
2578   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2579
2580 (defmacro gnus-summary-article-sparse-p (article)
2581   "Say whether this article is a sparse article or not."
2582   `(memq ,article gnus-newsgroup-sparse))
2583
2584 (defmacro gnus-summary-article-ancient-p (article)
2585   "Say whether this article is a sparse article or not."
2586   `(memq ,article gnus-newsgroup-ancient))
2587
2588 (defun gnus-article-parent-p (number)
2589   "Say whether this article is a parent or not."
2590   (let ((data (gnus-data-find-list number)))
2591     (and (cdr data)                     ; There has to be an article after...
2592          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2593             (gnus-data-level (nth 1 data))))))
2594
2595 (defun gnus-article-children (number)
2596   "Return a list of all children to NUMBER."
2597   (let* ((data (gnus-data-find-list number))
2598          (level (gnus-data-level (car data)))
2599          children)
2600     (setq data (cdr data))
2601     (while (and data
2602                 (= (gnus-data-level (car data)) (1+ level)))
2603       (push (gnus-data-number (car data)) children)
2604       (setq data (cdr data)))
2605     children))
2606
2607 (defmacro gnus-summary-skip-intangible ()
2608   "If the current article is intangible, then jump to a different article."
2609   '(let ((to (get-text-property (point) 'gnus-intangible)))
2610      (and to (gnus-summary-goto-subject to))))
2611
2612 (defmacro gnus-summary-article-intangible-p ()
2613   "Say whether this article is intangible or not."
2614   '(get-text-property (point) 'gnus-intangible))
2615
2616 (defun gnus-article-read-p (article)
2617   "Say whether ARTICLE is read or not."
2618   (not (or (memq article gnus-newsgroup-marked)
2619            (memq article gnus-newsgroup-unreads)
2620            (memq article gnus-newsgroup-unselected)
2621            (memq article gnus-newsgroup-dormant))))
2622
2623 ;; Some summary mode macros.
2624
2625 (defmacro gnus-summary-article-number ()
2626   "The article number of the article on the current line.
2627 If there isn's an article number here, then we return the current
2628 article number."
2629   '(progn
2630      (gnus-summary-skip-intangible)
2631      (or (get-text-property (point) 'gnus-number)
2632          (gnus-summary-last-subject))))
2633
2634 (defmacro gnus-summary-article-header (&optional number)
2635   "Return the header of article NUMBER."
2636   `(gnus-data-header (gnus-data-find
2637                       ,(or number '(gnus-summary-article-number)))))
2638
2639 (defmacro gnus-summary-thread-level (&optional number)
2640   "Return the level of thread that starts with article NUMBER."
2641   `(if (and (eq gnus-summary-make-false-root 'dummy)
2642             (get-text-property (point) 'gnus-intangible))
2643        0
2644      (gnus-data-level (gnus-data-find
2645                        ,(or number '(gnus-summary-article-number))))))
2646
2647 (defmacro gnus-summary-article-mark (&optional number)
2648   "Return the mark of article NUMBER."
2649   `(gnus-data-mark (gnus-data-find
2650                     ,(or number '(gnus-summary-article-number)))))
2651
2652 (defmacro gnus-summary-article-pos (&optional number)
2653   "Return the position of the line of article NUMBER."
2654   `(gnus-data-pos (gnus-data-find
2655                    ,(or number '(gnus-summary-article-number)))))
2656
2657 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2658 (defmacro gnus-summary-article-subject (&optional number)
2659   "Return current subject string or nil if nothing."
2660   `(let ((headers
2661           ,(if number
2662                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2663              '(gnus-data-header (assq (gnus-summary-article-number)
2664                                       gnus-newsgroup-data)))))
2665      (and headers
2666           (vectorp headers)
2667           (mail-header-subject headers))))
2668
2669 (defmacro gnus-summary-article-score (&optional number)
2670   "Return current article score."
2671   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2672                   gnus-newsgroup-scored))
2673        gnus-summary-default-score 0))
2674
2675 (defun gnus-summary-article-children (&optional number)
2676   "Return a list of article numbers that are children of article NUMBER."
2677   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2678          (level (gnus-data-level (car data)))
2679          l children)
2680     (while (and (setq data (cdr data))
2681                 (> (setq l (gnus-data-level (car data))) level))
2682       (and (= (1+ level) l)
2683            (push (gnus-data-number (car data))
2684                  children)))
2685     (nreverse children)))
2686
2687 (defun gnus-summary-article-parent (&optional number)
2688   "Return the article number of the parent of article NUMBER."
2689   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2690                                     (gnus-data-list t)))
2691          (level (gnus-data-level (car data))))
2692     (if (zerop level)
2693         ()                              ; This is a root.
2694       ;; We search until we find an article with a level less than
2695       ;; this one.  That function has to be the parent.
2696       (while (and (setq data (cdr data))
2697                   (not (< (gnus-data-level (car data)) level))))
2698       (and data (gnus-data-number (car data))))))
2699
2700 (defun gnus-unread-mark-p (mark)
2701   "Say whether MARK is the unread mark."
2702   (= mark gnus-unread-mark))
2703
2704 (defun gnus-read-mark-p (mark)
2705   "Say whether MARK is one of the marks that mark as read.
2706 This is all marks except unread, ticked, dormant, and expirable."
2707   (not (or (= mark gnus-unread-mark)
2708            (= mark gnus-ticked-mark)
2709            (= mark gnus-dormant-mark)
2710            (= mark gnus-expirable-mark))))
2711
2712 (defmacro gnus-article-mark (number)
2713   "Return the MARK of article NUMBER.
2714 This macro should only be used when computing the mark the \"first\"
2715 time; i.e., when generating the summary lines.  After that,
2716 `gnus-summary-article-mark' should be used to examine the
2717 marks of articles."
2718   `(cond
2719     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2720     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2721     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2722     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2723     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2724     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2725     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2726     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2727            gnus-ancient-mark))))
2728
2729 ;; Saving hidden threads.
2730
2731 (defmacro gnus-save-hidden-threads (&rest forms)
2732   "Save hidden threads, eval FORMS, and restore the hidden threads."
2733   (let ((config (make-symbol "config")))
2734     `(let ((,config (gnus-hidden-threads-configuration)))
2735        (unwind-protect
2736            (save-excursion
2737              ,@forms)
2738          (gnus-restore-hidden-threads-configuration ,config)))))
2739 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2740 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2741
2742 (defun gnus-data-compute-positions ()
2743   "Compute the positions of all articles."
2744   (setq gnus-newsgroup-data-reverse nil)
2745   (let ((data gnus-newsgroup-data))
2746     (save-excursion
2747       (gnus-save-hidden-threads
2748         (gnus-summary-show-all-threads)
2749         (goto-char (point-min))
2750         (while data
2751           (while (get-text-property (point) 'gnus-intangible)
2752             (forward-line 1))
2753           (gnus-data-set-pos (car data) (+ (point) 3))
2754           (setq data (cdr data))
2755           (forward-line 1))))))
2756
2757 (defun gnus-hidden-threads-configuration ()
2758   "Return the current hidden threads configuration."
2759   (save-excursion
2760     (let (config)
2761       (goto-char (point-min))
2762       (while (search-forward "\r" nil t)
2763         (push (1- (point)) config))
2764       config)))
2765
2766 (defun gnus-restore-hidden-threads-configuration (config)
2767   "Restore hidden threads configuration from CONFIG."
2768   (save-excursion
2769     (let (point buffer-read-only)
2770       (while (setq point (pop config))
2771         (when (and (< point (point-max))
2772                    (goto-char point)
2773                    (eq (char-after) ?\n))
2774           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2775
2776 ;; Various summary mode internalish functions.
2777
2778 (defun gnus-mouse-pick-article (e)
2779   (interactive "e")
2780   (mouse-set-point e)
2781   (gnus-summary-next-page nil t))
2782
2783 (defun gnus-summary-set-display-table ()
2784   "Change the display table.
2785 Odd characters have a tendency to mess
2786 up nicely formatted displays - we make all possible glyphs
2787 display only a single character."
2788
2789   ;; We start from the standard display table, if any.
2790   (let ((table (or (copy-sequence standard-display-table)
2791                    (make-display-table)))
2792         (i 32))
2793     ;; Nix out all the control chars...
2794     (while (>= (setq i (1- i)) 0)
2795       (aset table i [??]))
2796     ;; ... but not newline and cr, of course.  (cr is necessary for the
2797     ;; selective display).
2798     (aset table ?\n nil)
2799     (aset table ?\r nil)
2800     ;; We keep TAB as well.
2801     (aset table ?\t nil)
2802     ;; We nix out any glyphs over 126 that are not set already.
2803     (let ((i 256))
2804       (while (>= (setq i (1- i)) 127)
2805         ;; Only modify if the entry is nil.
2806         (unless (aref table i)
2807           (aset table i [??]))))
2808     (setq buffer-display-table table)))
2809
2810 (defun gnus-summary-set-article-display-arrow (pos)
2811   "Update the overlay arrow to point to line at position POS."
2812   (when (and gnus-summary-display-arrow
2813              (boundp 'overlay-arrow-position)
2814              (boundp 'overlay-arrow-string))
2815     (save-excursion
2816       (goto-char pos)
2817       (beginning-of-line)
2818       (unless overlay-arrow-position
2819         (setq overlay-arrow-position (make-marker)))
2820       (setq overlay-arrow-string "=>"
2821             overlay-arrow-position (set-marker overlay-arrow-position
2822                                                (point)
2823                                                (current-buffer))))))
2824
2825 (defun gnus-summary-buffer-name (group)
2826   "Return the summary buffer name of GROUP."
2827   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2828
2829 (defun gnus-summary-setup-buffer (group)
2830   "Initialize summary buffer."
2831   (let ((buffer (gnus-summary-buffer-name group))
2832         (dead-name (concat "*Dead Summary "
2833                            (gnus-group-decoded-name group) "*")))
2834     ;; If a dead summary buffer exists, we kill it.
2835     (when (gnus-buffer-live-p dead-name)
2836       (gnus-kill-buffer dead-name))
2837     (if (get-buffer buffer)
2838         (progn
2839           (set-buffer buffer)
2840           (setq gnus-summary-buffer (current-buffer))
2841           (not gnus-newsgroup-prepared))
2842       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2843       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2844       (gnus-summary-mode group)
2845       (when gnus-carpal
2846         (gnus-carpal-setup-buffer 'summary))
2847       (unless gnus-single-article-buffer
2848         (make-local-variable 'gnus-article-buffer)
2849         (make-local-variable 'gnus-article-current)
2850         (make-local-variable 'gnus-original-article-buffer))
2851       (setq gnus-newsgroup-name group)
2852       ;; Set any local variables in the group parameters.
2853       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2854       t)))
2855
2856 (defun gnus-set-global-variables ()
2857   "Set the global equivalents of the buffer-local variables.
2858 They are set to the latest values they had.  These reflect the summary
2859 buffer that was in action when the last article was fetched."
2860   (when (eq major-mode 'gnus-summary-mode)
2861     (setq gnus-summary-buffer (current-buffer))
2862     (let ((name gnus-newsgroup-name)
2863           (marked gnus-newsgroup-marked)
2864           (unread gnus-newsgroup-unreads)
2865           (headers gnus-current-headers)
2866           (data gnus-newsgroup-data)
2867           (summary gnus-summary-buffer)
2868           (article-buffer gnus-article-buffer)
2869           (original gnus-original-article-buffer)
2870           (gac gnus-article-current)
2871           (reffed gnus-reffed-article-number)
2872           (score-file gnus-current-score-file)
2873           (default-charset gnus-newsgroup-charset)
2874           vlist)
2875       (let ((locals gnus-newsgroup-variables))
2876         (while locals
2877           (if (consp (car locals))
2878               (push (eval (caar locals)) vlist)
2879             (push (eval (car locals)) vlist))
2880           (setq locals (cdr locals)))
2881         (setq vlist (nreverse vlist)))
2882       (save-excursion
2883         (set-buffer gnus-group-buffer)
2884         (setq gnus-newsgroup-name name
2885               gnus-newsgroup-marked marked
2886               gnus-newsgroup-unreads unread
2887               gnus-current-headers headers
2888               gnus-newsgroup-data data
2889               gnus-article-current gac
2890               gnus-summary-buffer summary
2891               gnus-article-buffer article-buffer
2892               gnus-original-article-buffer original
2893               gnus-reffed-article-number reffed
2894               gnus-current-score-file score-file
2895               gnus-newsgroup-charset default-charset)
2896         (let ((locals gnus-newsgroup-variables))
2897           (while locals
2898             (if (consp (car locals))
2899                 (set (caar locals) (pop vlist))
2900               (set (car locals) (pop vlist)))
2901             (setq locals (cdr locals))))
2902         ;; The article buffer also has local variables.
2903         (when (gnus-buffer-live-p gnus-article-buffer)
2904           (set-buffer gnus-article-buffer)
2905           (setq gnus-summary-buffer summary))))))
2906
2907 (defun gnus-summary-article-unread-p (article)
2908   "Say whether ARTICLE is unread or not."
2909   (memq article gnus-newsgroup-unreads))
2910
2911 (defun gnus-summary-first-article-p (&optional article)
2912   "Return whether ARTICLE is the first article in the buffer."
2913   (if (not (setq article (or article (gnus-summary-article-number))))
2914       nil
2915     (eq article (caar gnus-newsgroup-data))))
2916
2917 (defun gnus-summary-last-article-p (&optional article)
2918   "Return whether ARTICLE is the last article in the buffer."
2919   (if (not (setq article (or article (gnus-summary-article-number))))
2920       ;; All non-existent numbers are the last article.  :-)
2921       t
2922     (not (cdr (gnus-data-find-list article)))))
2923
2924 (defun gnus-make-thread-indent-array ()
2925   (let ((n 200))
2926     (unless (and gnus-thread-indent-array
2927                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2928       (setq gnus-thread-indent-array (make-vector 201 "")
2929             gnus-thread-indent-array-level gnus-thread-indent-level)
2930       (while (>= n 0)
2931         (aset gnus-thread-indent-array n
2932               (make-string (* n gnus-thread-indent-level) ? ))
2933         (setq n (1- n))))))
2934
2935 (defun gnus-update-summary-mark-positions ()
2936   "Compute where the summary marks are to go."
2937   (save-excursion
2938     (when (gnus-buffer-exists-p gnus-summary-buffer)
2939       (set-buffer gnus-summary-buffer))
2940     (let ((gnus-replied-mark 129)
2941           (gnus-score-below-mark 130)
2942           (gnus-score-over-mark 130)
2943           (gnus-download-mark 131)
2944           (spec gnus-summary-line-format-spec)
2945           gnus-visual pos)
2946       (save-excursion
2947         (gnus-set-work-buffer)
2948         (let ((gnus-summary-line-format-spec spec)
2949               (gnus-newsgroup-downloadable '((0 . t))))
2950           (gnus-summary-insert-line
2951            (make-full-mail-header 0 "" "nobody"
2952                                   "05 Apr 2001 23:33:09 +0400"
2953                                   "" "" 0 0 "" nil)
2954            0 nil 128 t nil "" nil 1)
2955           (goto-char (point-min))
2956           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2957                                              (- (point) 2)))))
2958           (goto-char (point-min))
2959           (push (cons 'replied (and (search-forward "\201" nil t)
2960                                     (- (point) 2)))
2961                 pos)
2962           (goto-char (point-min))
2963           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2964                 pos)
2965           (goto-char (point-min))
2966           (push (cons 'download
2967                       (and (search-forward "\203" nil t) (- (point) 2)))
2968                 pos)))
2969       (setq gnus-summary-mark-positions pos))))
2970
2971 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2972   "Insert a dummy root in the summary buffer."
2973   (beginning-of-line)
2974   (gnus-add-text-properties
2975    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2976    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2977
2978 (defun gnus-summary-extract-address-component (from)
2979   (or (car (funcall gnus-extract-address-components from))
2980       from))
2981
2982 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2983   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2984                                 default-mime-charset)))
2985     ;; Is it really necessary to do this next part for each summary line?
2986     ;; Luckily, doesn't seem to slow things down much.
2987     (or
2988      (and gnus-ignored-from-addresses
2989           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2990           (let ((extra-headers (mail-header-extra header))
2991                 to
2992                 newsgroups)
2993             (cond
2994              ((setq to (cdr (assq 'To extra-headers)))
2995               (concat "-> "
2996                       (inline
2997                         (gnus-summary-extract-address-component
2998                          (funcall gnus-decode-encoded-word-function to)))))
2999              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3000               (concat "=> " newsgroups)))))
3001      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3002
3003 (defun gnus-summary-insert-line (gnus-tmp-header
3004                                  gnus-tmp-level gnus-tmp-current
3005                                  gnus-tmp-unread gnus-tmp-replied
3006                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3007                                  &optional gnus-tmp-dummy gnus-tmp-score
3008                                  gnus-tmp-process)
3009   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3010          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3011          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3012          (gnus-tmp-score-char
3013           (if (or (null gnus-summary-default-score)
3014                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3015                       gnus-summary-zcore-fuzz))
3016               ?\ ;;;Whitespace
3017             (if (< gnus-tmp-score gnus-summary-default-score)
3018                 gnus-score-below-mark gnus-score-over-mark)))
3019          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3020          (gnus-tmp-replied
3021           (cond (gnus-tmp-process gnus-process-mark)
3022                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3023                  gnus-cached-mark)
3024                 (gnus-tmp-replied gnus-replied-mark)
3025                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3026                  gnus-forwarded-mark)
3027                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3028                  gnus-saved-mark)
3029                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3030                  gnus-recent-mark)
3031                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3032                  gnus-unseen-mark)
3033                 (t gnus-no-mark)))
3034          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3035          (gnus-tmp-name
3036           (cond
3037            ((string-match "<[^>]+> *$" gnus-tmp-from)
3038             (let ((beg (match-beginning 0)))
3039               (or (and (string-match "^\".+\"" gnus-tmp-from)
3040                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3041                   (substring gnus-tmp-from 0 beg))))
3042            ((string-match "(.+)" gnus-tmp-from)
3043             (substring gnus-tmp-from
3044                        (1+ (match-beginning 0)) (1- (match-end 0))))
3045            (t gnus-tmp-from)))
3046          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3047          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3048          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3049          (buffer-read-only nil))
3050     (when (string= gnus-tmp-name "")
3051       (setq gnus-tmp-name gnus-tmp-from))
3052     (unless (numberp gnus-tmp-lines)
3053       (setq gnus-tmp-lines -1))
3054     (if (= gnus-tmp-lines -1)
3055         (setq gnus-tmp-lines "?")
3056       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3057     (gnus-put-text-property-excluding-characters-with-faces
3058      (point)
3059      (progn (eval gnus-summary-line-format-spec) (point))
3060      'gnus-number gnus-tmp-number)
3061     (when (gnus-visual-p 'summary-highlight 'highlight)
3062       (forward-line -1)
3063       (gnus-run-hooks 'gnus-summary-update-hook)
3064       (forward-line 1))))
3065
3066 (defun gnus-summary-update-line (&optional dont-update)
3067   "Update summary line after change."
3068   (when (and gnus-summary-default-score
3069              (not gnus-summary-inhibit-highlight))
3070     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3071            (article (gnus-summary-article-number))
3072            (score (gnus-summary-article-score article)))
3073       (unless dont-update
3074         (if (and gnus-summary-mark-below
3075                  (< (gnus-summary-article-score)
3076                     gnus-summary-mark-below))
3077             ;; This article has a low score, so we mark it as read.
3078             (when (memq article gnus-newsgroup-unreads)
3079               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3080           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3081             ;; This article was previously marked as read on account
3082             ;; of a low score, but now it has risen, so we mark it as
3083             ;; unread.
3084             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3085         (gnus-summary-update-mark
3086          (if (or (null gnus-summary-default-score)
3087                  (<= (abs (- score gnus-summary-default-score))
3088                      gnus-summary-zcore-fuzz))
3089              ?\ ;;;Whitespace
3090            (if (< score gnus-summary-default-score)
3091                gnus-score-below-mark gnus-score-over-mark))
3092          'score))
3093       ;; Do visual highlighting.
3094       (when (gnus-visual-p 'summary-highlight 'highlight)
3095         (gnus-run-hooks 'gnus-summary-update-hook)))))
3096
3097 (defvar gnus-tmp-new-adopts nil)
3098
3099 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3100   "Return the number of articles in THREAD.
3101 This may be 0 in some cases -- if none of the articles in
3102 the thread are to be displayed."
3103   (let* ((number
3104           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3105           (cond
3106            ((not (listp thread))
3107             1)
3108            ((and (consp thread) (cdr thread))
3109             (apply
3110              '+ 1 (mapcar
3111                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3112            ((null thread)
3113             1)
3114            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3115             1)
3116            (t 0))))
3117     (when (and level (zerop level) gnus-tmp-new-adopts)
3118       (incf number
3119             (apply '+ (mapcar
3120                        'gnus-summary-number-of-articles-in-thread
3121                        gnus-tmp-new-adopts))))
3122     (if char
3123         (if (> number 1) gnus-not-empty-thread-mark
3124           gnus-empty-thread-mark)
3125       number)))
3126
3127 (defun gnus-summary-set-local-parameters (group)
3128   "Go through the local params of GROUP and set all variable specs in that list."
3129   (let ((params (gnus-group-find-parameter group))
3130         (vars '(quit-config))           ; Ignore quit-config.
3131         elem)
3132     (while params
3133       (setq elem (car params)
3134             params (cdr params))
3135       (and (consp elem)                 ; Has to be a cons.
3136            (consp (cdr elem))           ; The cdr has to be a list.
3137            (symbolp (car elem))         ; Has to be a symbol in there.
3138            (not (memq (car elem) vars))
3139            (ignore-errors               ; So we set it.
3140              (push (car elem) vars)
3141              (make-local-variable (car elem))
3142              (set (car elem) (eval (nth 1 elem))))))))
3143
3144 (defun gnus-summary-read-group (group &optional show-all no-article
3145                                       kill-buffer no-display backward
3146                                       select-articles)
3147   "Start reading news in newsgroup GROUP.
3148 If SHOW-ALL is non-nil, already read articles are also listed.
3149 If NO-ARTICLE is non-nil, no article is selected initially.
3150 If NO-DISPLAY, don't generate a summary buffer."
3151   (let (result)
3152     (while (and group
3153                 (null (setq result
3154                             (let ((gnus-auto-select-next nil))
3155                               (or (gnus-summary-read-group-1
3156                                    group show-all no-article
3157                                    kill-buffer no-display
3158                                    select-articles)
3159                                   (setq show-all nil
3160                                         select-articles nil)))))
3161                 (eq gnus-auto-select-next 'quietly))
3162       (set-buffer gnus-group-buffer)
3163       ;; The entry function called above goes to the next
3164       ;; group automatically, so we go two groups back
3165       ;; if we are searching for the previous group.
3166       (when backward
3167         (gnus-group-prev-unread-group 2))
3168       (if (not (equal group (gnus-group-group-name)))
3169           (setq group (gnus-group-group-name))
3170         (setq group nil)))
3171     result))
3172
3173 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3174   "Directly jump to the other GROUP from summary buffer.
3175 If SHOW-ALL is non-nil, already read articles are also listed."
3176   (interactive
3177    (if (eq gnus-summary-buffer (current-buffer))
3178        (list (completing-read
3179               "Group: " gnus-active-hashtb nil t
3180               (when (and gnus-newsgroup-name
3181                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3182                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3183               'gnus-group-history)
3184              current-prefix-arg)
3185      (error "%s must be invoked from a gnus summary buffer." this-command)))
3186   (unless (or (zerop (length group))
3187               (and gnus-newsgroup-name
3188                    (string-equal gnus-newsgroup-name group)))
3189     (gnus-summary-exit)
3190     (gnus-summary-read-group group show-all
3191                              gnus-dont-select-after-jump-to-other-group)))
3192
3193 (defun gnus-summary-read-group-1 (group show-all no-article
3194                                         kill-buffer no-display
3195                                         &optional select-articles)
3196   ;; Killed foreign groups can't be entered.
3197   ;;  (when (and (not (gnus-group-native-p group))
3198   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3199   ;;    (error "Dead non-native groups can't be entered"))
3200   (gnus-message 5 "Retrieving newsgroup: %s..."
3201                 (gnus-group-decoded-name group))
3202   (let* ((new-group (gnus-summary-setup-buffer group))
3203          (quit-config (gnus-group-quit-config group))
3204          (did-select (and new-group (gnus-select-newsgroup
3205                                      group show-all select-articles))))
3206     (cond
3207      ;; This summary buffer exists already, so we just select it.
3208      ((not new-group)
3209       (gnus-set-global-variables)
3210       (when kill-buffer
3211         (gnus-kill-or-deaden-summary kill-buffer))
3212       (gnus-configure-windows 'summary 'force)
3213       (gnus-set-mode-line 'summary)
3214       (gnus-summary-position-point)
3215       (message "")
3216       t)
3217      ;; We couldn't select this group.
3218      ((null did-select)
3219       (when (and (eq major-mode 'gnus-summary-mode)
3220                  (not (equal (current-buffer) kill-buffer)))
3221         (kill-buffer (current-buffer))
3222         (if (not quit-config)
3223             (progn
3224               ;; Update the info -- marks might need to be removed,
3225               ;; for instance.
3226               (gnus-summary-update-info)
3227               (set-buffer gnus-group-buffer)
3228               (gnus-group-jump-to-group group)
3229               (gnus-group-next-unread-group 1))
3230           (gnus-handle-ephemeral-exit quit-config)))
3231       (let ((grpinfo (gnus-get-info group)))
3232         (if (null (gnus-info-read grpinfo))
3233             (gnus-message 3 "Group %s contains no messages"
3234                           (gnus-group-decoded-name group))
3235           (gnus-message 3 "Can't select group")))
3236       nil)
3237      ;; The user did a `C-g' while prompting for number of articles,
3238      ;; so we exit this group.
3239      ((eq did-select 'quit)
3240       (and (eq major-mode 'gnus-summary-mode)
3241            (not (equal (current-buffer) kill-buffer))
3242            (kill-buffer (current-buffer)))
3243       (when kill-buffer
3244         (gnus-kill-or-deaden-summary kill-buffer))
3245       (if (not quit-config)
3246           (progn
3247             (set-buffer gnus-group-buffer)
3248             (gnus-group-jump-to-group group)
3249             (gnus-group-next-unread-group 1)
3250             (gnus-configure-windows 'group 'force))
3251         (gnus-handle-ephemeral-exit quit-config))
3252       ;; Finally signal the quit.
3253       (signal 'quit nil))
3254      ;; The group was successfully selected.
3255      (t
3256       (gnus-set-global-variables)
3257       ;; Save the active value in effect when the group was entered.
3258       (setq gnus-newsgroup-active
3259             (gnus-copy-sequence
3260              (gnus-active gnus-newsgroup-name)))
3261       ;; You can change the summary buffer in some way with this hook.
3262       (gnus-run-hooks 'gnus-select-group-hook)
3263       (gnus-update-format-specifications
3264        nil 'summary 'summary-mode 'summary-dummy)
3265       (gnus-update-summary-mark-positions)
3266       ;; Do score processing.
3267       (when gnus-use-scoring
3268         (gnus-possibly-score-headers))
3269       ;; Check whether to fill in the gaps in the threads.
3270       (when gnus-build-sparse-threads
3271         (gnus-build-sparse-threads))
3272       ;; Find the initial limit.
3273       (if gnus-show-threads
3274           (if show-all
3275               (let ((gnus-newsgroup-dormant nil))
3276                 (gnus-summary-initial-limit show-all))
3277             (gnus-summary-initial-limit show-all))
3278         ;; When untreaded, all articles are always shown.
3279         (setq gnus-newsgroup-limit
3280               (mapcar
3281                (lambda (header) (mail-header-number header))
3282                gnus-newsgroup-headers)))
3283       ;; Generate the summary buffer.
3284       (unless no-display
3285         (gnus-summary-prepare))
3286       (when gnus-use-trees
3287         (gnus-tree-open group)
3288         (setq gnus-summary-highlight-line-function
3289               'gnus-tree-highlight-article))
3290       ;; If the summary buffer is empty, but there are some low-scored
3291       ;; articles or some excluded dormants, we include these in the
3292       ;; buffer.
3293       (when (and (zerop (buffer-size))
3294                  (not no-display))
3295         (cond (gnus-newsgroup-dormant
3296                (gnus-summary-limit-include-dormant))
3297               ((and gnus-newsgroup-scored show-all)
3298                (gnus-summary-limit-include-expunged t))))
3299       ;; Function `gnus-apply-kill-file' must be called in this hook.
3300       (gnus-run-hooks 'gnus-apply-kill-hook)
3301       (if (and (zerop (buffer-size))
3302                (not no-display))
3303           (progn
3304             ;; This newsgroup is empty.
3305             (gnus-summary-catchup-and-exit nil t)
3306             (gnus-message 6 "No unread news")
3307             (when kill-buffer
3308               (gnus-kill-or-deaden-summary kill-buffer))
3309             ;; Return nil from this function.
3310             nil)
3311         ;; Hide conversation thread subtrees.  We cannot do this in
3312         ;; gnus-summary-prepare-hook since kill processing may not
3313         ;; work with hidden articles.
3314         (gnus-summary-maybe-hide-threads)
3315         (when kill-buffer
3316           (gnus-kill-or-deaden-summary kill-buffer))
3317         (gnus-summary-auto-select-subject)
3318         ;; Show first unread article if requested.
3319         (if (and (not no-article)
3320                  (not no-display)
3321                  gnus-newsgroup-unreads
3322                  gnus-auto-select-first)
3323             (progn
3324               (gnus-configure-windows 'summary)
3325               (let ((art (gnus-summary-article-number)))
3326                 (unless (or (memq art gnus-newsgroup-undownloaded)
3327                             (memq art gnus-newsgroup-downloadable))
3328                   (gnus-summary-goto-article art))))
3329           ;; Don't select any articles.
3330           (gnus-summary-position-point)
3331           (gnus-configure-windows 'summary 'force)
3332           (gnus-set-mode-line 'summary))
3333         (when (get-buffer-window gnus-group-buffer t)
3334           ;; Gotta use windows, because recenter does weird stuff if
3335           ;; the current buffer ain't the displayed window.
3336           (let ((owin (selected-window)))
3337             (select-window (get-buffer-window gnus-group-buffer t))
3338             (when (gnus-group-goto-group group)
3339               (recenter))
3340             (select-window owin)))
3341         ;; Mark this buffer as "prepared".
3342         (setq gnus-newsgroup-prepared t)
3343         (gnus-run-hooks 'gnus-summary-prepared-hook)
3344         t)))))
3345
3346 (defun gnus-summary-auto-select-subject ()
3347   "Select the subject line on initial group entry."
3348   (goto-char (point-min))
3349   (cond
3350    ((eq gnus-auto-select-subject 'best)
3351     (gnus-summary-best-unread-subject))
3352    ((eq gnus-auto-select-subject 'unread)
3353     (gnus-summary-first-unread-subject))
3354    ((eq gnus-auto-select-subject 'unseen)
3355     (gnus-summary-first-unseen-subject))
3356    ((eq gnus-auto-select-subject 'unseen-or-unread)
3357     (gnus-summary-first-unseen-or-unread-subject))
3358    ((eq gnus-auto-select-subject 'first)
3359     ;; Do nothing.
3360     )
3361    ((gnus-functionp gnus-auto-select-subject)
3362     (funcall gnus-auto-select-subject))))
3363
3364 (defun gnus-summary-prepare ()
3365   "Generate the summary buffer."
3366   (interactive)
3367   (let ((buffer-read-only nil))
3368     (erase-buffer)
3369     (setq gnus-newsgroup-data nil
3370           gnus-newsgroup-data-reverse nil)
3371     (gnus-run-hooks 'gnus-summary-generate-hook)
3372     ;; Generate the buffer, either with threads or without.
3373     (when gnus-newsgroup-headers
3374       (gnus-summary-prepare-threads
3375        (if gnus-show-threads
3376            (gnus-sort-gathered-threads
3377             (funcall gnus-summary-thread-gathering-function
3378                      (gnus-sort-threads
3379                       (gnus-cut-threads (gnus-make-threads)))))
3380          ;; Unthreaded display.
3381          (gnus-sort-articles gnus-newsgroup-headers))))
3382     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3383     ;; Call hooks for modifying summary buffer.
3384     (goto-char (point-min))
3385     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3386
3387 (defsubst gnus-general-simplify-subject (subject)
3388   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3389   (setq subject
3390         (cond
3391          ;; Truncate the subject.
3392          (gnus-simplify-subject-functions
3393           (gnus-map-function gnus-simplify-subject-functions subject))
3394          ((numberp gnus-summary-gather-subject-limit)
3395           (setq subject (gnus-simplify-subject-re subject))
3396           (if (> (length subject) gnus-summary-gather-subject-limit)
3397               (substring subject 0 gnus-summary-gather-subject-limit)
3398             subject))
3399          ;; Fuzzily simplify it.
3400          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3401           (gnus-simplify-subject-fuzzy subject))
3402          ;; Just remove the leading "Re:".
3403          (t
3404           (gnus-simplify-subject-re subject))))
3405
3406   (if (and gnus-summary-gather-exclude-subject
3407            (string-match gnus-summary-gather-exclude-subject subject))
3408       nil                               ; This article shouldn't be gathered
3409     subject))
3410
3411 (defun gnus-summary-simplify-subject-query ()
3412   "Query where the respool algorithm would put this article."
3413   (interactive)
3414   (gnus-summary-select-article)
3415   (message "%s"
3416            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3417
3418 (defun gnus-gather-threads-by-subject (threads)
3419   "Gather threads by looking at Subject headers."
3420   (if (not gnus-summary-make-false-root)
3421       threads
3422     (let ((hashtb (gnus-make-hashtable 1024))
3423           (prev threads)
3424           (result threads)
3425           subject hthread whole-subject)
3426       (while threads
3427         (setq subject (gnus-general-simplify-subject
3428                        (setq whole-subject (mail-header-subject
3429                                             (caar threads)))))
3430         (when subject
3431           (if (setq hthread (gnus-gethash subject hashtb))
3432               (progn
3433                 ;; We enter a dummy root into the thread, if we
3434                 ;; haven't done that already.
3435                 (unless (stringp (caar hthread))
3436                   (setcar hthread (list whole-subject (car hthread))))
3437                 ;; We add this new gathered thread to this gathered
3438                 ;; thread.
3439                 (setcdr (car hthread)
3440                         (nconc (cdar hthread) (list (car threads))))
3441                 ;; Remove it from the list of threads.
3442                 (setcdr prev (cdr threads))
3443                 (setq threads prev))
3444             ;; Enter this thread into the hash table.
3445             (gnus-sethash subject threads hashtb)))
3446         (setq prev threads)
3447         (setq threads (cdr threads)))
3448       result)))
3449
3450 (defun gnus-gather-threads-by-references (threads)
3451   "Gather threads by looking at References headers."
3452   (let ((idhashtb (gnus-make-hashtable 1024))
3453         (thhashtb (gnus-make-hashtable 1024))
3454         (prev threads)
3455         (result threads)
3456         ids references id gthread gid entered ref)
3457     (while threads
3458       (when (setq references (mail-header-references (caar threads)))
3459         (setq id (mail-header-id (caar threads))
3460               ids (inline (gnus-split-references references))
3461               entered nil)
3462         (while (setq ref (pop ids))
3463           (setq ids (delete ref ids))
3464           (if (not (setq gid (gnus-gethash ref idhashtb)))
3465               (progn
3466                 (gnus-sethash ref id idhashtb)
3467                 (gnus-sethash id threads thhashtb))
3468             (setq gthread (gnus-gethash gid thhashtb))
3469             (unless entered
3470               ;; We enter a dummy root into the thread, if we
3471               ;; haven't done that already.
3472               (unless (stringp (caar gthread))
3473                 (setcar gthread (list (mail-header-subject (caar gthread))
3474                                       (car gthread))))
3475               ;; We add this new gathered thread to this gathered
3476               ;; thread.
3477               (setcdr (car gthread)
3478                       (nconc (cdar gthread) (list (car threads)))))
3479             ;; Add it into the thread hash table.
3480             (gnus-sethash id gthread thhashtb)
3481             (setq entered t)
3482             ;; Remove it from the list of threads.
3483             (setcdr prev (cdr threads))
3484             (setq threads prev))))
3485       (setq prev threads)
3486       (setq threads (cdr threads)))
3487     result))
3488
3489 (defun gnus-sort-gathered-threads (threads)
3490   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3491   (let ((result threads))
3492     (while threads
3493       (when (stringp (caar threads))
3494         (setcdr (car threads)
3495                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3496       (setq threads (cdr threads)))
3497     result))
3498
3499 (defun gnus-thread-loop-p (root thread)
3500   "Say whether ROOT is in THREAD."
3501   (let ((stack (list thread))
3502         (infloop 0)
3503         th)
3504     (while (setq thread (pop stack))
3505       (setq th (cdr thread))
3506       (while (and th
3507                   (not (eq (caar th) root)))
3508         (pop th))
3509       (if th
3510           ;; We have found a loop.
3511           (let (ref-dep)
3512             (setcdr thread (delq (car th) (cdr thread)))
3513             (if (boundp (setq ref-dep (intern "none"
3514                                               gnus-newsgroup-dependencies)))
3515                 (setcdr (symbol-value ref-dep)
3516                         (nconc (cdr (symbol-value ref-dep))
3517                                (list (car th))))
3518               (set ref-dep (list nil (car th))))
3519             (setq infloop 1
3520                   stack nil))
3521         ;; Push all the subthreads onto the stack.
3522         (push (cdr thread) stack)))
3523     infloop))
3524
3525 (defun gnus-make-threads ()
3526   "Go through the dependency hashtb and find the roots.  Return all threads."
3527   (let (threads)
3528     (while (catch 'infloop
3529              (mapatoms
3530               (lambda (refs)
3531                 ;; Deal with self-referencing References loops.
3532                 (when (and (car (symbol-value refs))
3533                            (not (zerop
3534                                  (apply
3535                                   '+
3536                                   (mapcar
3537                                    (lambda (thread)
3538                                      (gnus-thread-loop-p
3539                                       (car (symbol-value refs)) thread))
3540                                    (cdr (symbol-value refs)))))))
3541                   (setq threads nil)
3542                   (throw 'infloop t))
3543                 (unless (car (symbol-value refs))
3544                   ;; These threads do not refer back to any other articles,
3545                   ;; so they're roots.
3546                   (setq threads (append (cdr (symbol-value refs)) threads))))
3547               gnus-newsgroup-dependencies)))
3548     threads))
3549
3550 ;; Build the thread tree.
3551 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3552   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3553
3554 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3555 if it was already present.
3556
3557 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3558 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3559 Message-IDs will be renamed be renamed to a unique Message-ID before
3560 being entered.
3561
3562 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3563   (let* ((id (mail-header-id header))
3564          (id-dep (and id (intern id dependencies)))
3565          ref ref-dep ref-header)
3566     ;; Enter this `header' in the `dependencies' table.
3567     (cond
3568      ((not id-dep)
3569       (setq header nil))
3570      ;; The first two cases do the normal part: enter a new `header'
3571      ;; in the `dependencies' table.
3572      ((not (boundp id-dep))
3573       (set id-dep (list header)))
3574      ((null (car (symbol-value id-dep)))
3575       (setcar (symbol-value id-dep) header))
3576
3577      ;; From here the `header' was already present in the
3578      ;; `dependencies' table.
3579      (force-new
3580       ;; Overrides an existing entry;
3581       ;; just set the header part of the entry.
3582       (setcar (symbol-value id-dep) header))
3583
3584      ;; Renames the existing `header' to a unique Message-ID.
3585      ((not gnus-summary-ignore-duplicates)
3586       ;; An article with this Message-ID has already been seen.
3587       ;; We rename the Message-ID.
3588       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3589            (list header))
3590       (mail-header-set-id header id))
3591
3592      ;; The last case ignores an existing entry, except it adds any
3593      ;; additional Xrefs (in case the two articles came from different
3594      ;; servers.
3595      ;; Also sets `header' to `nil' meaning that the `dependencies'
3596      ;; table was *not* modified.
3597      (t
3598       (mail-header-set-xref
3599        (car (symbol-value id-dep))
3600        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3601                    "")
3602                (or (mail-header-xref header) "")))
3603       (setq header nil)))
3604
3605     (when header
3606       ;; First check if that we are not creating a References loop.
3607       (setq ref (gnus-parent-id (mail-header-references header)))
3608       (while (and ref
3609                   (setq ref-dep (intern-soft ref dependencies))
3610                   (boundp ref-dep)
3611                   (setq ref-header (car (symbol-value ref-dep))))
3612         (if (string= id ref)
3613             ;; Yuk!  This is a reference loop.  Make the article be a
3614             ;; root article.
3615             (progn
3616               (mail-header-set-references (car (symbol-value id-dep)) "none")
3617               (setq ref nil))
3618           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3619       (setq ref (gnus-parent-id (mail-header-references header)))
3620       (setq ref-dep (intern (or ref "none") dependencies))
3621       (if (boundp ref-dep)
3622           (setcdr (symbol-value ref-dep)
3623                   (nconc (cdr (symbol-value ref-dep))
3624                          (list (symbol-value id-dep))))
3625         (set ref-dep (list nil (symbol-value id-dep)))))
3626     header))
3627
3628 (defun gnus-build-sparse-threads ()
3629   (let ((headers gnus-newsgroup-headers)
3630         (mail-parse-charset gnus-newsgroup-charset)
3631         (gnus-summary-ignore-duplicates t)
3632         header references generation relations
3633         subject child end new-child date)
3634     ;; First we create an alist of generations/relations, where
3635     ;; generations is how much we trust the relation, and the relation
3636     ;; is parent/child.
3637     (gnus-message 7 "Making sparse threads...")
3638     (save-excursion
3639       (nnheader-set-temp-buffer " *gnus sparse threads*")
3640       (while (setq header (pop headers))
3641         (when (and (setq references (mail-header-references header))
3642                    (not (string= references "")))
3643           (insert references)
3644           (setq child (mail-header-id header)
3645                 subject (mail-header-subject header)
3646                 date (mail-header-date header)
3647                 generation 0)
3648           (while (search-backward ">" nil t)
3649             (setq end (1+ (point)))
3650             (when (search-backward "<" nil t)
3651               (setq new-child (buffer-substring (point) end))
3652               (push (list (incf generation)
3653                           child (setq child new-child)
3654                           subject date)
3655                     relations)))
3656           (when child
3657             (push (list (1+ generation) child nil subject) relations))
3658           (erase-buffer)))
3659       (kill-buffer (current-buffer)))
3660     ;; Sort over trustworthiness.
3661     (mapcar
3662      (lambda (relation)
3663        (when (gnus-dependencies-add-header
3664               (make-full-mail-header-from-decoded-header
3665                gnus-reffed-article-number
3666                (nth 3 relation) "" (or (nth 4 relation) "")
3667                (nth 1 relation)
3668                (or (nth 2 relation) "") 0 0 "")
3669               gnus-newsgroup-dependencies nil)
3670          (push gnus-reffed-article-number gnus-newsgroup-limit)
3671          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3672          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3673                gnus-newsgroup-reads)
3674          (decf gnus-reffed-article-number)))
3675      (sort relations 'car-less-than-car))
3676     (gnus-message 7 "Making sparse threads...done")))
3677
3678 (defun gnus-build-old-threads ()
3679   ;; Look at all the articles that refer back to old articles, and
3680   ;; fetch the headers for the articles that aren't there.  This will
3681   ;; build complete threads - if the roots haven't been expired by the
3682   ;; server, that is.
3683   (let ((mail-parse-charset gnus-newsgroup-charset)
3684         id heads)
3685     (mapatoms
3686      (lambda (refs)
3687        (when (not (car (symbol-value refs)))
3688          (setq heads (cdr (symbol-value refs)))
3689          (while heads
3690            (if (memq (mail-header-number (caar heads))
3691                      gnus-newsgroup-dormant)
3692                (setq heads (cdr heads))
3693              (setq id (symbol-name refs))
3694              (while (and (setq id (gnus-build-get-header id))
3695                          (not (car (gnus-id-to-thread id)))))
3696              (setq heads nil)))))
3697      gnus-newsgroup-dependencies)))
3698
3699 ;; This function has to be called with point after the article number
3700 ;; on the beginning of the line.
3701 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3702   (let ((eol (gnus-point-at-eol))
3703         (buffer (current-buffer))
3704         header)
3705
3706     ;; overview: [num subject from date id refs chars lines misc]
3707     (unwind-protect
3708         (progn
3709           (narrow-to-region (point) eol)
3710           (unless (eobp)
3711             (forward-char))
3712
3713           (setq header
3714                 (make-full-mail-header
3715                  number                         ; number
3716                  (nnheader-nov-field)           ; subject
3717                  (nnheader-nov-field)           ; from
3718                  (nnheader-nov-field)           ; date
3719                  (nnheader-nov-read-message-id) ; id
3720                  (nnheader-nov-field)           ; refs
3721                  (nnheader-nov-read-integer)    ; chars
3722                  (nnheader-nov-read-integer)    ; lines
3723                  (unless (eobp)
3724                    (if (looking-at "Xref: ")
3725                        (goto-char (match-end 0)))
3726                    (nnheader-nov-field))        ; Xref
3727                  (nnheader-nov-parse-extra))))  ; extra
3728
3729       (widen))
3730
3731     (when gnus-alter-header-function
3732       (funcall gnus-alter-header-function header))
3733     (gnus-dependencies-add-header header dependencies force-new)))
3734
3735 (defun gnus-build-get-header (id)
3736   "Look through the buffer of NOV lines and find the header to ID.
3737 Enter this line into the dependencies hash table, and return
3738 the id of the parent article (if any)."
3739   (let ((deps gnus-newsgroup-dependencies)
3740         found header)
3741     (prog1
3742         (save-excursion
3743           (set-buffer nntp-server-buffer)
3744           (let ((case-fold-search nil))
3745             (goto-char (point-min))
3746             (while (and (not found)
3747                         (search-forward id nil t))
3748               (beginning-of-line)
3749               (setq found (looking-at
3750                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3751                                    (regexp-quote id))))
3752               (or found (beginning-of-line 2)))
3753             (when found
3754               (beginning-of-line)
3755               (and
3756                (setq header (gnus-nov-parse-line
3757                              (read (current-buffer)) deps))
3758                (gnus-parent-id (mail-header-references header))))))
3759       (when header
3760         (let ((number (mail-header-number header)))
3761           (push number gnus-newsgroup-limit)
3762           (push header gnus-newsgroup-headers)
3763           (if (memq number gnus-newsgroup-unselected)
3764               (progn
3765                 (push number gnus-newsgroup-unreads)
3766                 (setq gnus-newsgroup-unselected
3767                       (delq number gnus-newsgroup-unselected)))
3768             (push number gnus-newsgroup-ancient)))))))
3769
3770 (defun gnus-build-all-threads ()
3771   "Read all the headers."
3772   (let ((gnus-summary-ignore-duplicates t)
3773         (mail-parse-charset gnus-newsgroup-charset)
3774         (dependencies gnus-newsgroup-dependencies)
3775         header article)
3776     (save-excursion
3777       (set-buffer nntp-server-buffer)
3778       (let ((case-fold-search nil))
3779         (goto-char (point-min))
3780         (while (not (eobp))
3781           (ignore-errors
3782             (setq article (read (current-buffer))
3783                   header (gnus-nov-parse-line article dependencies)))
3784           (when header
3785             (save-excursion
3786               (set-buffer gnus-summary-buffer)
3787               (push header gnus-newsgroup-headers)
3788               (if (memq (setq article (mail-header-number header))
3789                         gnus-newsgroup-unselected)
3790                   (progn
3791                     (push article gnus-newsgroup-unreads)
3792                     (setq gnus-newsgroup-unselected
3793                           (delq article gnus-newsgroup-unselected)))
3794                 (push article gnus-newsgroup-ancient)))
3795             (forward-line 1)))))))
3796
3797 (defun gnus-summary-update-article-line (article header)
3798   "Update the line for ARTICLE using HEADERS."
3799   (let* ((id (mail-header-id header))
3800          (thread (gnus-id-to-thread id)))
3801     (unless thread
3802       (error "Article in no thread"))
3803     ;; Update the thread.
3804     (setcar thread header)
3805     (gnus-summary-goto-subject article)
3806     (let* ((datal (gnus-data-find-list article))
3807            (data (car datal))
3808            (length (when (cdr datal)
3809                      (- (gnus-data-pos data)
3810                         (gnus-data-pos (cadr datal)))))
3811            (buffer-read-only nil)
3812            (level (gnus-summary-thread-level)))
3813       (gnus-delete-line)
3814       (gnus-summary-insert-line
3815        header level nil (gnus-article-mark article)
3816        (memq article gnus-newsgroup-replied)
3817        (memq article gnus-newsgroup-expirable)
3818        ;; Only insert the Subject string when it's different
3819        ;; from the previous Subject string.
3820        (if (and
3821             gnus-show-threads
3822             (gnus-subject-equal
3823              (condition-case ()
3824                  (mail-header-subject
3825                   (gnus-data-header
3826                    (cadr
3827                     (gnus-data-find-list
3828                      article
3829                      (gnus-data-list t)))))
3830                ;; Error on the side of excessive subjects.
3831                (error ""))
3832              (mail-header-subject header)))
3833            ""
3834          (mail-header-subject header))
3835        nil (cdr (assq article gnus-newsgroup-scored))
3836        (memq article gnus-newsgroup-processable))
3837       (when length
3838         (gnus-data-update-list
3839          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3840
3841 (defun gnus-summary-update-article (article &optional iheader)
3842   "Update ARTICLE in the summary buffer."
3843   (set-buffer gnus-summary-buffer)
3844   (let* ((header (gnus-summary-article-header article))
3845          (id (mail-header-id header))
3846          (data (gnus-data-find article))
3847          (thread (gnus-id-to-thread id))
3848          (references (mail-header-references header))
3849          (parent
3850           (gnus-id-to-thread
3851            (or (gnus-parent-id
3852                 (when (and references
3853                            (not (equal "" references)))
3854                   references))
3855                "none")))
3856          (buffer-read-only nil)
3857          (old (car thread)))
3858     (when thread
3859       (unless iheader
3860         (setcar thread nil)
3861         (when parent
3862           (delq thread parent)))
3863       (if (gnus-summary-insert-subject id header)
3864           ;; Set the (possibly) new article number in the data structure.
3865           (gnus-data-set-number data (gnus-id-to-article id))
3866         (setcar thread old)
3867         nil))))
3868
3869 (defun gnus-rebuild-thread (id &optional line)
3870   "Rebuild the thread containing ID.
3871 If LINE, insert the rebuilt thread starting on line LINE."
3872   (let ((buffer-read-only nil)
3873         old-pos current thread data)
3874     (if (not gnus-show-threads)
3875         (setq thread (list (car (gnus-id-to-thread id))))
3876       ;; Get the thread this article is part of.
3877       (setq thread (gnus-remove-thread id)))
3878     (setq old-pos (gnus-point-at-bol))
3879     (setq current (save-excursion
3880                     (and (re-search-backward "[\r\n]" nil t)
3881                          (gnus-summary-article-number))))
3882     ;; If this is a gathered thread, we have to go some re-gathering.
3883     (when (stringp (car thread))
3884       (let ((subject (car thread))
3885             roots thr)
3886         (setq thread (cdr thread))
3887         (while thread
3888           (unless (memq (setq thr (gnus-id-to-thread
3889                                    (gnus-root-id
3890                                     (mail-header-id (caar thread)))))
3891                         roots)
3892             (push thr roots))
3893           (setq thread (cdr thread)))
3894         ;; We now have all (unique) roots.
3895         (if (= (length roots) 1)
3896             ;; All the loose roots are now one solid root.
3897             (setq thread (car roots))
3898           (setq thread (cons subject (gnus-sort-threads roots))))))
3899     (let (threads)
3900       ;; We then insert this thread into the summary buffer.
3901       (when line
3902         (goto-char (point-min))
3903         (forward-line (1- line)))
3904       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3905         (if gnus-show-threads
3906             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3907           (gnus-summary-prepare-unthreaded thread))
3908         (setq data (nreverse gnus-newsgroup-data))
3909         (setq threads gnus-newsgroup-threads))
3910       ;; We splice the new data into the data structure.
3911       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3912       ;;!!! then we want to insert at the beginning of the buffer.
3913       ;;!!! That happens to be true with Gnus now, but that may
3914       ;;!!! change in the future.  Perhaps.
3915       (gnus-data-enter-list
3916        (if line nil current) data (- (point) old-pos))
3917       (setq gnus-newsgroup-threads
3918             (nconc threads gnus-newsgroup-threads))
3919       (gnus-data-compute-positions))))
3920
3921 (defun gnus-number-to-header (number)
3922   "Return the header for article NUMBER."
3923   (let ((headers gnus-newsgroup-headers))
3924     (while (and headers
3925                 (not (= number (mail-header-number (car headers)))))
3926       (pop headers))
3927     (when headers
3928       (car headers))))
3929
3930 (defun gnus-parent-headers (in-headers &optional generation)
3931   "Return the headers of the GENERATIONeth parent of HEADERS."
3932   (unless generation
3933     (setq generation 1))
3934   (let ((parent t)
3935         (headers in-headers)
3936         references)
3937     (while (and parent
3938                 (not (zerop generation))
3939                 (setq references (mail-header-references headers)))
3940       (setq headers (if (and references
3941                              (setq parent (gnus-parent-id references)))
3942                         (car (gnus-id-to-thread parent))
3943                       nil))
3944       (decf generation))
3945     (and (not (eq headers in-headers))
3946          headers)))
3947
3948 (defun gnus-id-to-thread (id)
3949   "Return the (sub-)thread where ID appears."
3950   (gnus-gethash id gnus-newsgroup-dependencies))
3951
3952 (defun gnus-id-to-article (id)
3953   "Return the article number of ID."
3954   (let ((thread (gnus-id-to-thread id)))
3955     (when (and thread
3956                (car thread))
3957       (mail-header-number (car thread)))))
3958
3959 (defun gnus-id-to-header (id)
3960   "Return the article headers of ID."
3961   (car (gnus-id-to-thread id)))
3962
3963 (defun gnus-article-displayed-root-p (article)
3964   "Say whether ARTICLE is a root(ish) article."
3965   (let ((level (gnus-summary-thread-level article))
3966         (refs (mail-header-references  (gnus-summary-article-header article)))
3967         particle)
3968     (cond
3969      ((null level) nil)
3970      ((zerop level) t)
3971      ((null refs) t)
3972      ((null (gnus-parent-id refs)) t)
3973      ((and (= 1 level)
3974            (null (setq particle (gnus-id-to-article
3975                                  (gnus-parent-id refs))))
3976            (null (gnus-summary-thread-level particle)))))))
3977
3978 (defun gnus-root-id (id)
3979   "Return the id of the root of the thread where ID appears."
3980   (let (last-id prev)
3981     (while (and id (setq prev (car (gnus-id-to-thread id))))
3982       (setq last-id id
3983             id (gnus-parent-id (mail-header-references prev))))
3984     last-id))
3985
3986 (defun gnus-articles-in-thread (thread)
3987   "Return the list of articles in THREAD."
3988   (cons (mail-header-number (car thread))
3989         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3990
3991 (defun gnus-remove-thread (id &optional dont-remove)
3992   "Remove the thread that has ID in it."
3993   (let (headers thread last-id)
3994     ;; First go up in this thread until we find the root.
3995     (setq last-id (gnus-root-id id)
3996           headers (message-flatten-list (gnus-id-to-thread last-id)))
3997     ;; We have now found the real root of this thread.  It might have
3998     ;; been gathered into some loose thread, so we have to search
3999     ;; through the threads to find the thread we wanted.
4000     (let ((threads gnus-newsgroup-threads)
4001           sub)
4002       (while threads
4003         (setq sub (car threads))
4004         (if (stringp (car sub))
4005             ;; This is a gathered thread, so we look at the roots
4006             ;; below it to find whether this article is in this
4007             ;; gathered root.
4008             (progn
4009               (setq sub (cdr sub))
4010               (while sub
4011                 (when (member (caar sub) headers)
4012                   (setq thread (car threads)
4013                         threads nil
4014                         sub nil))
4015                 (setq sub (cdr sub))))
4016           ;; It's an ordinary thread, so we check it.
4017           (when (eq (car sub) (car headers))
4018             (setq thread sub
4019                   threads nil)))
4020         (setq threads (cdr threads)))
4021       ;; If this article is in no thread, then it's a root.
4022       (if thread
4023           (unless dont-remove
4024             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4025         (setq thread (gnus-id-to-thread last-id)))
4026       (when thread
4027         (prog1
4028             thread                      ; We return this thread.
4029           (unless dont-remove
4030             (if (stringp (car thread))
4031                 (progn
4032                   ;; If we use dummy roots, then we have to remove the
4033                   ;; dummy root as well.
4034                   (when (eq gnus-summary-make-false-root 'dummy)
4035                     ;; We go to the dummy root by going to
4036                     ;; the first sub-"thread", and then one line up.
4037                     (gnus-summary-goto-article
4038                      (mail-header-number (caadr thread)))
4039                     (forward-line -1)
4040                     (gnus-delete-line)
4041                     (gnus-data-compute-positions))
4042                   (setq thread (cdr thread))
4043                   (while thread
4044                     (gnus-remove-thread-1 (car thread))
4045                     (setq thread (cdr thread))))
4046               (gnus-remove-thread-1 thread))))))))
4047
4048 (defun gnus-remove-thread-1 (thread)
4049   "Remove the thread THREAD recursively."
4050   (let ((number (mail-header-number (pop thread)))
4051         d)
4052     (setq thread (reverse thread))
4053     (while thread
4054       (gnus-remove-thread-1 (pop thread)))
4055     (when (setq d (gnus-data-find number))
4056       (goto-char (gnus-data-pos d))
4057       (gnus-summary-show-thread)
4058       (gnus-data-remove
4059        number
4060        (- (gnus-point-at-bol)
4061           (prog1
4062               (1+ (gnus-point-at-eol))
4063             (gnus-delete-line)))))))
4064
4065 (defun gnus-sort-threads-1 (threads func)
4066   (sort (mapcar (lambda (thread)
4067                   (cons (car thread)
4068                         (and (cdr thread)
4069                              (gnus-sort-threads-1 (cdr thread) func))))
4070                 threads) func))
4071
4072 (defun gnus-sort-threads (threads)
4073   "Sort THREADS."
4074   (if (not gnus-thread-sort-functions)
4075       threads
4076     (gnus-message 8 "Sorting threads...")
4077     (prog1
4078         (gnus-sort-threads-1
4079          threads
4080          (gnus-make-sort-function gnus-thread-sort-functions))
4081       (gnus-message 8 "Sorting threads...done"))))
4082
4083 (defun gnus-sort-articles (articles)
4084   "Sort ARTICLES."
4085   (when gnus-article-sort-functions
4086     (gnus-message 7 "Sorting articles...")
4087     (prog1
4088         (setq gnus-newsgroup-headers
4089               (sort articles (gnus-make-sort-function
4090                               gnus-article-sort-functions)))
4091       (gnus-message 7 "Sorting articles...done"))))
4092
4093 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4094 (defmacro gnus-thread-header (thread)
4095   "Return header of first article in THREAD.
4096 Note that THREAD must never, ever be anything else than a variable -
4097 using some other form will lead to serious barfage."
4098   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4099   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4100   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4101         (vector thread) 2))
4102
4103 (defsubst gnus-article-sort-by-number (h1 h2)
4104   "Sort articles by article number."
4105   (< (mail-header-number h1)
4106      (mail-header-number h2)))
4107
4108 (defun gnus-thread-sort-by-number (h1 h2)
4109   "Sort threads by root article number."
4110   (gnus-article-sort-by-number
4111    (gnus-thread-header h1) (gnus-thread-header h2)))
4112
4113 (defsubst gnus-article-sort-by-lines (h1 h2)
4114   "Sort articles by article Lines header."
4115   (< (mail-header-lines h1)
4116      (mail-header-lines h2)))
4117
4118 (defun gnus-thread-sort-by-lines (h1 h2)
4119   "Sort threads by root article Lines header."
4120   (gnus-article-sort-by-lines
4121    (gnus-thread-header h1) (gnus-thread-header h2)))
4122
4123 (defsubst gnus-article-sort-by-chars (h1 h2)
4124   "Sort articles by octet length."
4125   (< (mail-header-chars h1)
4126      (mail-header-chars h2)))
4127
4128 (defun gnus-thread-sort-by-chars (h1 h2)
4129   "Sort threads by root article octet length."
4130   (gnus-article-sort-by-chars
4131    (gnus-thread-header h1) (gnus-thread-header h2)))
4132
4133 (defsubst gnus-article-sort-by-author (h1 h2)
4134   "Sort articles by root author."
4135   (string-lessp
4136    (let ((addr (car (mime-entity-read-field h1 'From))))
4137      (or (std11-full-name-string addr)
4138          (std11-address-string addr)
4139          ""))
4140    (let ((addr (car (mime-entity-read-field h2 'From))))
4141      (or (std11-full-name-string addr)
4142          (std11-address-string addr)
4143          ""))
4144    ))
4145
4146 (defun gnus-thread-sort-by-author (h1 h2)
4147   "Sort threads by root author."
4148   (gnus-article-sort-by-author
4149    (gnus-thread-header h1)  (gnus-thread-header h2)))
4150
4151 (defsubst gnus-article-sort-by-subject (h1 h2)
4152   "Sort articles by root subject."
4153   (string-lessp
4154    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4155    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4156
4157 (defun gnus-thread-sort-by-subject (h1 h2)
4158   "Sort threads by root subject."
4159   (gnus-article-sort-by-subject
4160    (gnus-thread-header h1) (gnus-thread-header h2)))
4161
4162 (defsubst gnus-article-sort-by-date (h1 h2)
4163   "Sort articles by root article date."
4164   (time-less-p
4165    (gnus-date-get-time (mail-header-date h1))
4166    (gnus-date-get-time (mail-header-date h2))))
4167
4168 (defun gnus-thread-sort-by-date (h1 h2)
4169   "Sort threads by root article date."
4170   (gnus-article-sort-by-date
4171    (gnus-thread-header h1) (gnus-thread-header h2)))
4172
4173 (defsubst gnus-article-sort-by-score (h1 h2)
4174   "Sort articles by root article score.
4175 Unscored articles will be counted as having a score of zero."
4176   (> (or (cdr (assq (mail-header-number h1)
4177                     gnus-newsgroup-scored))
4178          gnus-summary-default-score 0)
4179      (or (cdr (assq (mail-header-number h2)
4180                     gnus-newsgroup-scored))
4181          gnus-summary-default-score 0)))
4182
4183 (defun gnus-thread-sort-by-score (h1 h2)
4184   "Sort threads by root article score."
4185   (gnus-article-sort-by-score
4186    (gnus-thread-header h1) (gnus-thread-header h2)))
4187
4188 (defun gnus-thread-sort-by-total-score (h1 h2)
4189   "Sort threads by the sum of all scores in the thread.
4190 Unscored articles will be counted as having a score of zero."
4191   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4192
4193 (defun gnus-thread-total-score (thread)
4194   ;; This function find the total score of THREAD.
4195   (cond
4196    ((null thread)
4197     0)
4198    ((consp thread)
4199     (if (stringp (car thread))
4200         (apply gnus-thread-score-function 0
4201                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4202       (gnus-thread-total-score-1 thread)))
4203    (t
4204     (gnus-thread-total-score-1 (list thread)))))
4205
4206 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4207   "Sort threads such that the thread with the most recently arrived article comes first."
4208   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4209
4210 (defun gnus-thread-highest-number (thread)
4211   "Return the highest article number in THREAD."
4212   (apply 'max (mapcar (lambda (header)
4213                         (mail-header-number header))
4214                       (message-flatten-list thread))))
4215
4216 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4217   "Sort threads such that the thread with the most recently dated article comes first."
4218   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4219
4220 (defun gnus-thread-latest-date (thread)
4221   "Return the highest article date in THREAD."
4222   (let ((previous-time 0))
4223     (apply 'max (mapcar
4224                  (lambda (header)
4225                    (setq previous-time
4226                          (time-to-seconds
4227                           (mail-header-parse-date
4228                            (condition-case ()
4229                                (mail-header-date header)
4230                              (error previous-time))))))
4231                  (sort
4232                   (message-flatten-list thread)
4233                   (lambda (h1 h2)
4234                     (< (mail-header-number h1)
4235                        (mail-header-number h2))))))))
4236
4237 (defun gnus-thread-total-score-1 (root)
4238   ;; This function find the total score of the thread below ROOT.
4239   (setq root (car root))
4240   (apply gnus-thread-score-function
4241          (or (append
4242               (mapcar 'gnus-thread-total-score
4243                       (cdr (gnus-id-to-thread (mail-header-id root))))
4244               (when (> (mail-header-number root) 0)
4245                 (list (or (cdr (assq (mail-header-number root)
4246                                      gnus-newsgroup-scored))
4247                           gnus-summary-default-score 0))))
4248              (list gnus-summary-default-score)
4249              '(0))))
4250
4251 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4252 (defvar gnus-tmp-prev-subject nil)
4253 (defvar gnus-tmp-false-parent nil)
4254 (defvar gnus-tmp-root-expunged nil)
4255 (defvar gnus-tmp-dummy-line nil)
4256
4257 (eval-when-compile (defvar gnus-tmp-header))
4258 (defun gnus-extra-header (type &optional header)
4259   "Return the extra header of TYPE."
4260   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4261       ""))
4262
4263 (defvar gnus-tmp-thread-tree-header-string "")
4264
4265 (defvar gnus-sum-thread-tree-root "> "
4266   "With %B spec, used for the root of a thread.
4267 If nil, use subject instead.")
4268 (defvar gnus-sum-thread-tree-single-indent ""
4269   "With %B spec, used for a thread with just one message.
4270 If nil, use subject instead.")
4271 (defvar gnus-sum-thread-tree-vertical "| "
4272   "With %B spec, used for drawing a vertical line.")
4273 (defvar gnus-sum-thread-tree-indent "  "
4274   "With %B spec, used for indenting.")
4275 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4276   "With %B spec, used for a leaf with brothers.")
4277 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4278   "With %B spec, used for a leaf without brothers.")
4279
4280 (defun gnus-summary-prepare-threads (threads)
4281   "Prepare summary buffer from THREADS and indentation LEVEL.
4282 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4283 or a straight list of headers."
4284   (gnus-message 7 "Generating summary...")
4285
4286   (setq gnus-newsgroup-threads threads)
4287   (beginning-of-line)
4288
4289   (let ((gnus-tmp-level 0)
4290         (default-score (or gnus-summary-default-score 0))
4291         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4292         thread number subject stack state gnus-tmp-gathered beg-match
4293         new-roots gnus-tmp-new-adopts thread-end
4294         gnus-tmp-header gnus-tmp-unread
4295         gnus-tmp-replied gnus-tmp-subject-or-nil
4296         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4297         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4298         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4299         tree-stack)
4300
4301     (setq gnus-tmp-prev-subject nil)
4302
4303     (if (vectorp (car threads))
4304         ;; If this is a straight (sic) list of headers, then a
4305         ;; threaded summary display isn't required, so we just create
4306         ;; an unthreaded one.
4307         (gnus-summary-prepare-unthreaded threads)
4308
4309       ;; Do the threaded display.
4310
4311       (while (or threads stack gnus-tmp-new-adopts new-roots)
4312
4313         (if (and (= gnus-tmp-level 0)
4314                  (or (not stack)
4315                      (= (caar stack) 0))
4316                  (not gnus-tmp-false-parent)
4317                  (or gnus-tmp-new-adopts new-roots))
4318             (if gnus-tmp-new-adopts
4319                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4320                       thread (list (car gnus-tmp-new-adopts))
4321                       gnus-tmp-header (caar thread)
4322                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4323               (when new-roots
4324                 (setq thread (list (car new-roots))
4325                       gnus-tmp-header (caar thread)
4326                       new-roots (cdr new-roots))))
4327
4328           (if threads
4329               ;; If there are some threads, we do them before the
4330               ;; threads on the stack.
4331               (setq thread threads
4332                     gnus-tmp-header (caar thread))
4333             ;; There were no current threads, so we pop something off
4334             ;; the stack.
4335             (setq state (car stack)
4336                   gnus-tmp-level (car state)
4337                   tree-stack (cadr state)
4338                   thread (caddr state)
4339                   stack (cdr stack)
4340                   gnus-tmp-header (caar thread))))
4341
4342         (setq gnus-tmp-false-parent nil)
4343         (setq gnus-tmp-root-expunged nil)
4344         (setq thread-end nil)
4345
4346         (if (stringp gnus-tmp-header)
4347             ;; The header is a dummy root.
4348             (cond
4349              ((eq gnus-summary-make-false-root 'adopt)
4350               ;; We let the first article adopt the rest.
4351               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4352                                                (cddar thread)))
4353               (setq gnus-tmp-gathered
4354                     (nconc (mapcar
4355                             (lambda (h) (mail-header-number (car h)))
4356                             (cddar thread))
4357                            gnus-tmp-gathered))
4358               (setq thread (cons (list (caar thread)
4359                                        (cadar thread))
4360                                  (cdr thread)))
4361               (setq gnus-tmp-level -1
4362                     gnus-tmp-false-parent t))
4363              ((eq gnus-summary-make-false-root 'empty)
4364               ;; We print adopted articles with empty subject fields.
4365               (setq gnus-tmp-gathered
4366                     (nconc (mapcar
4367                             (lambda (h) (mail-header-number (car h)))
4368                             (cddar thread))
4369                            gnus-tmp-gathered))
4370               (setq gnus-tmp-level -1))
4371              ((eq gnus-summary-make-false-root 'dummy)
4372               ;; We remember that we probably want to output a dummy
4373               ;; root.
4374               (setq gnus-tmp-dummy-line gnus-tmp-header)
4375               (setq gnus-tmp-prev-subject gnus-tmp-header))
4376              (t
4377               ;; We do not make a root for the gathered
4378               ;; sub-threads at all.
4379               (setq gnus-tmp-level -1)))
4380
4381           (setq number (mail-header-number gnus-tmp-header)
4382                 subject (mail-header-subject gnus-tmp-header))
4383
4384           (cond
4385            ;; If the thread has changed subject, we might want to make
4386            ;; this subthread into a root.
4387            ((and (null gnus-thread-ignore-subject)
4388                  (not (zerop gnus-tmp-level))
4389                  gnus-tmp-prev-subject
4390                  (not (inline
4391                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4392             (setq new-roots (nconc new-roots (list (car thread)))
4393                   thread-end t
4394                   gnus-tmp-header nil))
4395            ;; If the article lies outside the current limit,
4396            ;; then we do not display it.
4397            ((not (memq number gnus-newsgroup-limit))
4398             (setq gnus-tmp-gathered
4399                   (nconc (mapcar
4400                           (lambda (h) (mail-header-number (car h)))
4401                           (cdar thread))
4402                          gnus-tmp-gathered))
4403             (setq gnus-tmp-new-adopts (if (cdar thread)
4404                                           (append gnus-tmp-new-adopts
4405                                                   (cdar thread))
4406                                         gnus-tmp-new-adopts)
4407                   thread-end t
4408                   gnus-tmp-header nil)
4409             (when (zerop gnus-tmp-level)
4410               (setq gnus-tmp-root-expunged t)))
4411            ;; Perhaps this article is to be marked as read?
4412            ((and gnus-summary-mark-below
4413                  (< (or (cdr (assq number gnus-newsgroup-scored))
4414                         default-score)
4415                     gnus-summary-mark-below)
4416                  ;; Don't touch sparse articles.
4417                  (not (gnus-summary-article-sparse-p number))
4418                  (not (gnus-summary-article-ancient-p number)))
4419             (setq gnus-newsgroup-unreads
4420                   (delq number gnus-newsgroup-unreads))
4421             (if gnus-newsgroup-auto-expire
4422                 (push number gnus-newsgroup-expirable)
4423               (push (cons number gnus-low-score-mark)
4424                     gnus-newsgroup-reads))))
4425
4426           (when gnus-tmp-header
4427             ;; We may have an old dummy line to output before this
4428             ;; article.
4429             (when (and gnus-tmp-dummy-line
4430                        (gnus-subject-equal
4431                         gnus-tmp-dummy-line
4432                         (mail-header-subject gnus-tmp-header)))
4433               (gnus-summary-insert-dummy-line
4434                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4435               (setq gnus-tmp-dummy-line nil))
4436
4437             ;; Compute the mark.
4438             (setq gnus-tmp-unread (gnus-article-mark number))
4439
4440             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4441                                   gnus-tmp-header gnus-tmp-level)
4442                   gnus-newsgroup-data)
4443
4444             ;; Actually insert the line.
4445             (setq
4446              gnus-tmp-subject-or-nil
4447              (cond
4448               ((and gnus-thread-ignore-subject
4449                     gnus-tmp-prev-subject
4450                     (not (inline (gnus-subject-equal
4451                                   gnus-tmp-prev-subject subject))))
4452                subject)
4453               ((zerop gnus-tmp-level)
4454                (if (and (eq gnus-summary-make-false-root 'empty)
4455                         (memq number gnus-tmp-gathered)
4456                         gnus-tmp-prev-subject
4457                         (inline (gnus-subject-equal
4458                                  gnus-tmp-prev-subject subject)))
4459                    gnus-summary-same-subject
4460                  subject))
4461               (t gnus-summary-same-subject)))
4462             (if (and (eq gnus-summary-make-false-root 'adopt)
4463                      (= gnus-tmp-level 1)
4464                      (memq number gnus-tmp-gathered))
4465                 (setq gnus-tmp-opening-bracket ?\<
4466                       gnus-tmp-closing-bracket ?\>)
4467               (setq gnus-tmp-opening-bracket ?\[
4468                     gnus-tmp-closing-bracket ?\]))
4469             (setq
4470              gnus-tmp-indentation
4471              (aref gnus-thread-indent-array gnus-tmp-level)
4472              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4473              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4474                                 gnus-summary-default-score 0)
4475              gnus-tmp-score-char
4476              (if (or (null gnus-summary-default-score)
4477                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4478                          gnus-summary-zcore-fuzz))
4479                  ?\ ;;;Whitespace
4480                (if (< gnus-tmp-score gnus-summary-default-score)
4481                    gnus-score-below-mark gnus-score-over-mark))
4482              gnus-tmp-replied
4483              (cond ((memq number gnus-newsgroup-processable)
4484                     gnus-process-mark)
4485                    ((memq number gnus-newsgroup-cached)
4486                     gnus-cached-mark)
4487                    ((memq number gnus-newsgroup-replied)
4488                     gnus-replied-mark)
4489                    ((memq number gnus-newsgroup-forwarded)
4490                     gnus-forwarded-mark)
4491                    ((memq number gnus-newsgroup-saved)
4492                     gnus-saved-mark)
4493                    ((memq number gnus-newsgroup-recent)
4494                     gnus-recent-mark)
4495                    ((memq number gnus-newsgroup-unseen)
4496                     gnus-unseen-mark)
4497                    (t gnus-no-mark))
4498              gnus-tmp-from (mail-header-from gnus-tmp-header)
4499              gnus-tmp-name
4500              (cond
4501               ((string-match "<[^>]+> *$" gnus-tmp-from)
4502                (setq beg-match (match-beginning 0))
4503                (or (and (string-match "^\".+\"" gnus-tmp-from)
4504                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4505                    (substring gnus-tmp-from 0 beg-match)))
4506               ((string-match "(.+)" gnus-tmp-from)
4507                (substring gnus-tmp-from
4508                           (1+ (match-beginning 0)) (1- (match-end 0))))
4509               (t gnus-tmp-from))
4510              gnus-tmp-thread-tree-header-string
4511              (cond
4512               ((not gnus-show-threads) "")
4513               ((zerop gnus-tmp-level)
4514                (if (cdar thread)
4515                    (or gnus-sum-thread-tree-root subject)
4516                  (or gnus-sum-thread-tree-single-indent subject)))
4517               (t
4518                (concat (apply 'concat
4519                               (mapcar (lambda (item)
4520                                         (if (= item 1)
4521                                             gnus-sum-thread-tree-vertical
4522                                           gnus-sum-thread-tree-indent))
4523                                       (cdr (reverse tree-stack))))
4524                        (if (nth 1 thread)
4525                            gnus-sum-thread-tree-leaf-with-other
4526                          gnus-sum-thread-tree-single-leaf)))))
4527             (when (string= gnus-tmp-name "")
4528               (setq gnus-tmp-name gnus-tmp-from))
4529             (unless (numberp gnus-tmp-lines)
4530               (setq gnus-tmp-lines -1))
4531             (if (= gnus-tmp-lines -1)
4532                 (setq gnus-tmp-lines "?")
4533               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4534             (gnus-put-text-property
4535              (point)
4536              (progn (eval gnus-summary-line-format-spec) (point))
4537              'gnus-number number)
4538             (when gnus-visual-p
4539               (forward-line -1)
4540               (gnus-run-hooks 'gnus-summary-update-hook)
4541               (forward-line 1))
4542
4543             (setq gnus-tmp-prev-subject subject)))
4544
4545         (when (nth 1 thread)
4546           (push (list (max 0 gnus-tmp-level)
4547                       (copy-list tree-stack)
4548                       (nthcdr 1 thread))
4549                 stack))
4550         (push (if (nth 1 thread) 1 0) tree-stack)
4551         (incf gnus-tmp-level)
4552         (setq threads (if thread-end nil (cdar thread)))
4553         (unless threads
4554           (setq gnus-tmp-level 0)))))
4555   (gnus-message 7 "Generating summary...done"))
4556
4557 (defun gnus-summary-prepare-unthreaded (headers)
4558   "Generate an unthreaded summary buffer based on HEADERS."
4559   (let (header number mark)
4560
4561     (beginning-of-line)
4562
4563     (while headers
4564       ;; We may have to root out some bad articles...
4565       (when (memq (setq number (mail-header-number
4566                                 (setq header (pop headers))))
4567                   gnus-newsgroup-limit)
4568         ;; Mark article as read when it has a low score.
4569         (when (and gnus-summary-mark-below
4570                    (< (or (cdr (assq number gnus-newsgroup-scored))
4571                           gnus-summary-default-score 0)
4572                       gnus-summary-mark-below)
4573                    (not (gnus-summary-article-ancient-p number)))
4574           (setq gnus-newsgroup-unreads
4575                 (delq number gnus-newsgroup-unreads))
4576           (if gnus-newsgroup-auto-expire
4577               (push number gnus-newsgroup-expirable)
4578             (push (cons number gnus-low-score-mark)
4579                   gnus-newsgroup-reads)))
4580
4581         (setq mark (gnus-article-mark number))
4582         (push (gnus-data-make number mark (1+ (point)) header 0)
4583               gnus-newsgroup-data)
4584         (gnus-summary-insert-line
4585          header 0 number
4586          mark (memq number gnus-newsgroup-replied)
4587          (memq number gnus-newsgroup-expirable)
4588          (mail-header-subject header) nil
4589          (cdr (assq number gnus-newsgroup-scored))
4590          (memq number gnus-newsgroup-processable))))))
4591
4592 (defun gnus-summary-remove-list-identifiers ()
4593   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4594   (let ((regexp (if (consp gnus-list-identifiers)
4595                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4596                   gnus-list-identifiers))
4597         changed subject)
4598     (when regexp
4599       (dolist (header gnus-newsgroup-headers)
4600         (setq subject (mail-header-subject header)
4601               changed nil)
4602         (while (string-match
4603                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4604                 subject)
4605           (setq subject
4606                 (concat (substring subject 0 (match-beginning 2))
4607                         (substring subject (match-end 0)))
4608                 changed t))
4609         (when (and changed
4610                    (string-match
4611                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4612           (setq subject
4613                 (concat (substring subject 0 (match-beginning 1))
4614                         (substring subject (match-end 1)))))
4615         (when changed
4616           (mail-header-set-subject header subject))))))
4617
4618 (defun gnus-fetch-headers (articles)
4619   "Fetch headers of ARTICLES."
4620   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4621     (gnus-message 5 "Fetching headers for %s..." name)
4622     (prog1
4623         (if (eq 'nov
4624                 (setq gnus-headers-retrieved-by
4625                       (gnus-retrieve-headers
4626                        articles gnus-newsgroup-name
4627                        ;; We might want to fetch old headers, but
4628                        ;; not if there is only 1 article.
4629                        (and (or (and
4630                                  (not (eq gnus-fetch-old-headers 'some))
4631                                  (not (numberp gnus-fetch-old-headers)))
4632                                 (> (length articles) 1))
4633                             gnus-fetch-old-headers))))
4634             (gnus-get-newsgroup-headers-xover
4635              articles nil nil gnus-newsgroup-name t)
4636           (gnus-get-newsgroup-headers))
4637       (gnus-message 5 "Fetching headers for %s...done" name))))
4638
4639 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4640   "Select newsgroup GROUP.
4641 If READ-ALL is non-nil, all articles in the group are selected.
4642 If SELECT-ARTICLES, only select those articles from GROUP."
4643   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4644          ;;!!! Dirty hack; should be removed.
4645          (gnus-summary-ignore-duplicates
4646           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4647               t
4648             gnus-summary-ignore-duplicates))
4649          (info (nth 2 entry))
4650          articles fetched-articles cached)
4651
4652     (unless (gnus-check-server
4653              (set (make-local-variable 'gnus-current-select-method)
4654                   (gnus-find-method-for-group group)))
4655       (error "Couldn't open server"))
4656
4657     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4658         (gnus-activate-group group)     ; Or we can activate it...
4659         (progn                          ; Or we bug out.
4660           (when (equal major-mode 'gnus-summary-mode)
4661             (kill-buffer (current-buffer)))
4662           (error "Couldn't activate group %s: %s"
4663                  group (gnus-status-message group))))
4664
4665     (unless (gnus-request-group group t)
4666       (when (equal major-mode 'gnus-summary-mode)
4667         (kill-buffer (current-buffer)))
4668       (error "Couldn't request group %s: %s"
4669              group (gnus-status-message group)))
4670
4671     (setq gnus-newsgroup-name group
4672           gnus-newsgroup-unselected nil
4673           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4674
4675     (let ((display (gnus-group-find-parameter group 'display)))
4676       (setq gnus-newsgroup-display
4677             (cond
4678              ((not (zerop (or (car-safe read-all) 0)))
4679               ;; The user entered the group with C-u SPC/RET, let's show
4680               ;; all articles.
4681               'gnus-not-ignore)
4682              ((eq display 'all)
4683               'gnus-not-ignore)
4684              ((arrayp display)
4685               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4686              ((numberp display)
4687               ;; The following is probably the "correct" solution, but
4688               ;; it makes Gnus fetch all headers and then limit the
4689               ;; articles (which is slow), so instead we hack the
4690               ;; select-articles parameter instead. -- Simon Josefsson
4691               ;; <jas@kth.se>
4692               ;;
4693               ;; (gnus-byte-compile
4694               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4695               ;;                         display)))))
4696               (setq select-articles
4697                     (gnus-uncompress-range
4698                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4699                              (if (> tmp 0)
4700                                  tmp
4701                                1))
4702                            (cdr (gnus-active group)))))
4703               nil)
4704              (t
4705               nil))))
4706
4707     (gnus-summary-setup-default-charset)
4708
4709     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4710     (when (gnus-virtual-group-p group)
4711       (setq cached gnus-newsgroup-cached))
4712
4713     (setq gnus-newsgroup-unreads
4714           (gnus-set-difference
4715            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4716            gnus-newsgroup-dormant))
4717
4718     (setq gnus-newsgroup-processable nil)
4719
4720     (gnus-update-read-articles group gnus-newsgroup-unreads)
4721
4722     ;; Adjust and set lists of article marks.
4723     (when info
4724       (gnus-adjust-marked-articles info))
4725
4726     (if (setq articles select-articles)
4727         (setq gnus-newsgroup-unselected
4728               (gnus-sorted-intersection
4729                gnus-newsgroup-unreads
4730                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4731       (setq articles (gnus-articles-to-read group read-all)))
4732
4733     (cond
4734      ((null articles)
4735       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4736       'quit)
4737      ((eq articles 0) nil)
4738      (t
4739       ;; Init the dependencies hash table.
4740       (setq gnus-newsgroup-dependencies
4741             (gnus-make-hashtable (length articles)))
4742       (gnus-set-global-variables)
4743       ;; Retrieve the headers and read them in.
4744       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4745
4746       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4747       (when cached
4748         (setq gnus-newsgroup-cached cached))
4749
4750       ;; Suppress duplicates?
4751       (when gnus-suppress-duplicates
4752         (gnus-dup-suppress-articles))
4753
4754       ;; Set the initial limit.
4755       (setq gnus-newsgroup-limit (copy-sequence articles))
4756       ;; Remove canceled articles from the list of unread articles.
4757       (setq fetched-articles
4758             (mapcar (lambda (headers) (mail-header-number headers))
4759                     gnus-newsgroup-headers))
4760       (setq gnus-newsgroup-articles fetched-articles)
4761       (setq gnus-newsgroup-unreads
4762             (gnus-set-sorted-intersection
4763              gnus-newsgroup-unreads fetched-articles))
4764
4765       ;; The `seen' marks are treated specially.
4766       (if (not gnus-newsgroup-seen)
4767           (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4768         (dolist (article gnus-newsgroup-articles)
4769           (unless (gnus-member-of-range article gnus-newsgroup-seen)
4770             (push article gnus-newsgroup-unseen)))
4771         (setq gnus-newsgroup-unseen (nreverse gnus-newsgroup-unseen)))
4772
4773       ;; Removed marked articles that do not exist.
4774       (gnus-update-missing-marks
4775        (gnus-sorted-complement fetched-articles articles))
4776       ;; We might want to build some more threads first.
4777       (when (and gnus-fetch-old-headers
4778                  (eq gnus-headers-retrieved-by 'nov))
4779         (if (eq gnus-fetch-old-headers 'invisible)
4780             (gnus-build-all-threads)
4781           (gnus-build-old-threads)))
4782       ;; Let the Gnus agent mark articles as read.
4783       (when gnus-agent
4784         (gnus-agent-get-undownloaded-list))
4785       ;; Remove list identifiers from subject
4786       (when gnus-list-identifiers
4787         (gnus-summary-remove-list-identifiers))
4788       ;; Check whether auto-expire is to be done in this group.
4789       (setq gnus-newsgroup-auto-expire
4790             (gnus-group-auto-expirable-p group))
4791       ;; Set up the article buffer now, if necessary.
4792       (unless gnus-single-article-buffer
4793         (gnus-article-setup-buffer))
4794       ;; First and last article in this newsgroup.
4795       (when gnus-newsgroup-headers
4796         (setq gnus-newsgroup-begin
4797               (mail-header-number (car gnus-newsgroup-headers))
4798               gnus-newsgroup-end
4799               (mail-header-number
4800                (gnus-last-element gnus-newsgroup-headers))))
4801       ;; GROUP is successfully selected.
4802       (or gnus-newsgroup-headers t)))))
4803
4804 (defun gnus-summary-display-make-predicate (display)
4805   (require 'gnus-agent)
4806   (when (= (length display) 1)
4807     (setq display (car display)))
4808   (unless gnus-summary-display-cache
4809     (dolist (elem (append (list (cons 'read 'read)
4810                                 (cons 'unseen 'unseen))
4811                           gnus-article-mark-lists))
4812       (push (cons (cdr elem)
4813                   (gnus-byte-compile
4814                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4815             gnus-summary-display-cache)))
4816   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4817     (gnus-get-predicate display)))
4818
4819 ;; Uses the dynamically bound `number' variable.
4820 (defvar number)
4821 (defun gnus-article-marked-p (type &optional article)
4822   (let ((article (or article number)))
4823     (cond
4824      ((eq type 'tick)
4825       (memq article gnus-newsgroup-marked))
4826      ((eq type 'unsend)
4827       (memq article gnus-newsgroup-unsendable))
4828      ((eq type 'undownload)
4829       (memq article gnus-newsgroup-undownloaded))
4830      ((eq type 'download)
4831       (memq article gnus-newsgroup-downloadable))
4832      ((eq type 'unread)
4833       (memq article gnus-newsgroup-unreads))
4834      ((eq type 'read)
4835       (memq article gnus-newsgroup-reads))
4836      ((eq type 'dormant)
4837       (memq article gnus-newsgroup-dormant) )
4838      ((eq type 'expire)
4839       (memq article gnus-newsgroup-expirable))
4840      ((eq type 'reply)
4841       (memq article gnus-newsgroup-replied))
4842      ((eq type 'killed)
4843       (memq article gnus-newsgroup-killed))
4844      ((eq type 'bookmark)
4845       (assq article gnus-newsgroup-bookmarks))
4846      ((eq type 'score)
4847       (assq article gnus-newsgroup-scored))
4848      ((eq type 'save)
4849       (memq article gnus-newsgroup-saved))
4850      ((eq type 'cache)
4851       (memq article gnus-newsgroup-cached))
4852      ((eq type 'forward)
4853       (memq article gnus-newsgroup-forwarded))
4854      ((eq type 'seen)
4855       (not (memq article gnus-newsgroup-unseen)))
4856      ((eq type 'recent)
4857       (memq article gnus-newsgroup-recent))
4858      (t t))))
4859
4860 (defun gnus-articles-to-read (group &optional read-all)
4861   "Find out what articles the user wants to read."
4862   (let* ((articles
4863           ;; Select all articles if `read-all' is non-nil, or if there
4864           ;; are no unread articles.
4865           (if (or read-all
4866                   (and (zerop (length gnus-newsgroup-marked))
4867                        (zerop (length gnus-newsgroup-unreads)))
4868                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4869               ;; We want to select the headers for all the articles in
4870               ;; the group, so we select either all the active
4871               ;; articles in the group, or (if that's nil), the
4872               ;; articles in the cache.
4873               (or
4874                (gnus-uncompress-range (gnus-active group))
4875                (gnus-cache-articles-in-group group))
4876             ;; Select only the "normal" subset of articles.
4877             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4878                           (copy-sequence gnus-newsgroup-unreads))
4879                   '<)))
4880          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4881          (scored (length scored-list))
4882          (number (length articles))
4883          (marked (+ (length gnus-newsgroup-marked)
4884                     (length gnus-newsgroup-dormant)))
4885          (select
4886           (cond
4887            ((numberp read-all)
4888             read-all)
4889            (t
4890             (condition-case ()
4891                 (cond
4892                  ((and (or (<= scored marked) (= scored number))
4893                        (natnump gnus-large-newsgroup)
4894                        (> number gnus-large-newsgroup))
4895                   (let* ((cursor-in-echo-area nil)
4896                          (input
4897                           (read-from-minibuffer
4898                            (format
4899                             "How many articles from %s (max %d): "
4900                             (gnus-limit-string
4901                              (gnus-group-decoded-name gnus-newsgroup-name)
4902                              35)
4903                             number)
4904                            (cons (number-to-string gnus-large-newsgroup)
4905                                  0))))
4906                     (if (string-match "^[ \t]*$" input)
4907                         number
4908                       input)))
4909                  ((and (> scored marked) (< scored number)
4910                        (> (- scored number) 20))
4911                   (let ((input
4912                          (read-string
4913                           (format "%s %s (%d scored, %d total): "
4914                                   "How many articles from"
4915                                   (gnus-group-decoded-name group)
4916                                   scored number))))
4917                     (if (string-match "^[ \t]*$" input)
4918                         number input)))
4919                  (t number))
4920               (quit
4921                (message "Quit getting the articles to read")
4922                nil))))))
4923     (setq select (if (stringp select) (string-to-number select) select))
4924     (if (or (null select) (zerop select))
4925         select
4926       (if (and (not (zerop scored)) (<= (abs select) scored))
4927           (progn
4928             (setq articles (sort scored-list '<))
4929             (setq number (length articles)))
4930         (setq articles (copy-sequence articles)))
4931
4932       (when (< (abs select) number)
4933         (if (< select 0)
4934             ;; Select the N oldest articles.
4935             (setcdr (nthcdr (1- (abs select)) articles) nil)
4936           ;; Select the N most recent articles.
4937           (setq articles (nthcdr (- number select) articles))))
4938       (setq gnus-newsgroup-unselected
4939             (gnus-sorted-intersection
4940              gnus-newsgroup-unreads
4941              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4942       (when gnus-alter-articles-to-read-function
4943         (setq gnus-newsgroup-unreads
4944               (sort
4945                (funcall gnus-alter-articles-to-read-function
4946                         gnus-newsgroup-name gnus-newsgroup-unreads)
4947                '<)))
4948       articles)))
4949
4950 (defun gnus-killed-articles (killed articles)
4951   (let (out)
4952     (while articles
4953       (when (inline (gnus-member-of-range (car articles) killed))
4954         (push (car articles) out))
4955       (setq articles (cdr articles)))
4956     out))
4957
4958 (defun gnus-uncompress-marks (marks)
4959   "Uncompress the mark ranges in MARKS."
4960   (let ((uncompressed '(score bookmark))
4961         out)
4962     (while marks
4963       (if (memq (caar marks) uncompressed)
4964           (push (car marks) out)
4965         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4966       (setq marks (cdr marks)))
4967     out))
4968
4969 (defun gnus-article-mark-to-type (mark)
4970   "Return the type of MARK."
4971   (or (cadr (assq mark gnus-article-special-mark-lists))
4972       'list))
4973
4974 (defun gnus-article-unpropagatable-p (mark)
4975   "Return whether MARK should be propagated to backend."
4976   (memq mark gnus-article-unpropagated-mark-lists))
4977
4978 (defun gnus-adjust-marked-articles (info)
4979   "Set all article lists and remove all marks that are no longer valid."
4980   (let* ((marked-lists (gnus-info-marks info))
4981          (active (gnus-active (gnus-info-group info)))
4982          (min (car active))
4983          (max (cdr active))
4984          (types gnus-article-mark-lists)
4985          marks var articles article mark mark-type)
4986
4987     (dolist (marks marked-lists)
4988       (setq mark (car marks)
4989             mark-type (gnus-article-mark-to-type mark)
4990             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4991
4992       ;; We set the variable according to the type of the marks list,
4993       ;; and then adjust the marks to a subset of the active articles.
4994       (cond
4995        ;; Adjust "simple" lists.
4996        ((eq mark-type 'list)
4997         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4998         (when (memq mark '(tick dormant expire reply save))
4999           (while articles
5000             (when (or (< (setq article (pop articles)) min) (> article max))
5001               (set var (delq article (symbol-value var)))))))
5002        ;; Adjust assocs.
5003        ((eq mark-type 'tuple)
5004         (set var (setq articles (cdr marks)))
5005         (when (not (listp (cdr (symbol-value var))))
5006           (set var (list (symbol-value var))))
5007         (when (not (listp (cdr articles)))
5008           (setq articles (list articles)))
5009         (while articles
5010           (when (or (not (consp (setq article (pop articles))))
5011                     (< (car article) min)
5012                     (> (car article) max))
5013             (set var (delq article (symbol-value var))))))
5014        ;; Adjust ranges (sloppily).
5015        ((eq mark-type 'range)
5016         (cond
5017          ((eq mark 'seen)
5018           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5019           ;; It should be (seen (NUM1 . NUM2)).
5020           (when (numberp (cddr marks))
5021             (setcdr marks (list (cdr marks))))
5022           (setq articles (cdr marks))
5023           (while (and articles
5024                       (or (and (consp (car articles))
5025                                (> min (cdar articles)))
5026                           (and (numberp (car articles))
5027                                (> min (car articles)))))
5028             (pop articles))
5029           (set var articles))))))))
5030
5031 (defun gnus-update-missing-marks (missing)
5032   "Go through the list of MISSING articles and remove them from the mark lists."
5033   (when missing
5034     (let (var m)
5035       ;; Go through all types.
5036       (dolist (elem gnus-article-mark-lists)
5037         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5038           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5039           (when (symbol-value var)
5040             ;; This list has articles.  So we delete all missing
5041             ;; articles from it.
5042             (setq m missing)
5043             (while m
5044               (set var (delq (pop m) (symbol-value var))))))))))
5045
5046 (defun gnus-update-marks ()
5047   "Enter the various lists of marked articles into the newsgroup info list."
5048   (let ((types gnus-article-mark-lists)
5049         (info (gnus-get-info gnus-newsgroup-name))
5050         type list newmarked symbol delta-marks)
5051     (when info
5052       ;; Add all marks lists to the list of marks lists.
5053       (while (setq type (pop types))
5054         (setq list (symbol-value
5055                     (setq symbol
5056                           (intern (format "gnus-newsgroup-%s" (car type))))))
5057
5058         (when list
5059           ;; Get rid of the entries of the articles that have the
5060           ;; default score.
5061           (when (and (eq (cdr type) 'score)
5062                      gnus-save-score
5063                      list)
5064             (let* ((arts list)
5065                    (prev (cons nil list))
5066                    (all prev))
5067               (while arts
5068                 (if (or (not (consp (car arts)))
5069                         (= (cdar arts) gnus-summary-default-score))
5070                     (setcdr prev (cdr arts))
5071                   (setq prev arts))
5072                 (setq arts (cdr arts)))
5073               (setq list (cdr all)))))
5074
5075         (when (eq (cdr type) 'seen)
5076           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5077
5078         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5079           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5080
5081         (when (and (gnus-check-backend-function
5082                     'request-set-mark gnus-newsgroup-name)
5083                    (not (gnus-article-unpropagatable-p (cdr type))))
5084           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5085                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5086                  (add (gnus-remove-from-range
5087                        (gnus-copy-sequence list) old)))
5088             (when add
5089               (push (list add 'add (list (cdr type))) delta-marks))
5090             (when del
5091               (push (list del 'del (list (cdr type))) delta-marks))))
5092
5093         (when list
5094           (push (cons (cdr type) list) newmarked)))
5095
5096       (when delta-marks
5097         (unless (gnus-check-group gnus-newsgroup-name)
5098           (error "Can't open server for %s" gnus-newsgroup-name))
5099         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5100
5101       ;; Enter these new marks into the info of the group.
5102       (if (nthcdr 3 info)
5103           (setcar (nthcdr 3 info) newmarked)
5104         ;; Add the marks lists to the end of the info.
5105         (when newmarked
5106           (setcdr (nthcdr 2 info) (list newmarked))))
5107
5108       ;; Cut off the end of the info if there's nothing else there.
5109       (let ((i 5))
5110         (while (and (> i 2)
5111                     (not (nth i info)))
5112           (when (nthcdr (decf i) info)
5113             (setcdr (nthcdr i info) nil)))))))
5114
5115 (defun gnus-set-mode-line (where)
5116   "Set the mode line of the article or summary buffers.
5117 If WHERE is `summary', the summary mode line format will be used."
5118   ;; Is this mode line one we keep updated?
5119   (when (and (memq where gnus-updated-mode-lines)
5120              (symbol-value
5121               (intern (format "gnus-%s-mode-line-format-spec" where))))
5122     (let (mode-string)
5123       (save-excursion
5124         ;; We evaluate this in the summary buffer since these
5125         ;; variables are buffer-local to that buffer.
5126         (set-buffer gnus-summary-buffer)
5127         ;; We bind all these variables that are used in the `eval' form
5128         ;; below.
5129         (let* ((mformat (symbol-value
5130                          (intern
5131                           (format "gnus-%s-mode-line-format-spec" where))))
5132                (gnus-tmp-group-name (gnus-group-decoded-name
5133                                      gnus-newsgroup-name))
5134                (gnus-tmp-article-number (or gnus-current-article 0))
5135                (gnus-tmp-unread gnus-newsgroup-unreads)
5136                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5137                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5138                (gnus-tmp-unread-and-unselected
5139                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5140                             (zerop gnus-tmp-unselected))
5141                        "")
5142                       ((zerop gnus-tmp-unselected)
5143                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5144                       (t (format "{%d(+%d) more}"
5145                                  gnus-tmp-unread-and-unticked
5146                                  gnus-tmp-unselected))))
5147                (gnus-tmp-subject
5148                 (if (and gnus-current-headers
5149                          (vectorp gnus-current-headers))
5150                     (gnus-mode-string-quote
5151                      (mail-header-subject gnus-current-headers))
5152                   ""))
5153                bufname-length max-len
5154                gnus-tmp-header);; passed as argument to any user-format-funcs
5155           (setq mode-string (eval mformat))
5156           (setq bufname-length (if (string-match "%b" mode-string)
5157                                    (- (length
5158                                        (buffer-name
5159                                         (if (eq where 'summary)
5160                                             nil
5161                                           (get-buffer gnus-article-buffer))))
5162                                       2)
5163                                  0))
5164           (setq max-len (max 4 (if gnus-mode-non-string-length
5165                                    (- (window-width)
5166                                       gnus-mode-non-string-length
5167                                       bufname-length)
5168                                  (length mode-string))))
5169           ;; We might have to chop a bit of the string off...
5170           (when (> (length mode-string) max-len)
5171             (setq mode-string
5172                   (concat (gnus-truncate-string mode-string (- max-len 3))
5173                           "...")))
5174           ;; Pad the mode string a bit.
5175           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5176       ;; Update the mode line.
5177       (setq mode-line-buffer-identification
5178             (gnus-mode-line-buffer-identification (list mode-string)))
5179       (set-buffer-modified-p t))))
5180
5181 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5182   "Go through the HEADERS list and add all Xrefs to a hash table.
5183 The resulting hash table is returned, or nil if no Xrefs were found."
5184   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5185          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5186          (xref-hashtb (gnus-make-hashtable))
5187          start group entry number xrefs header)
5188     (while headers
5189       (setq header (pop headers))
5190       (when (and (setq xrefs (mail-header-xref header))
5191                  (not (memq (setq number (mail-header-number header))
5192                             unreads)))
5193         (setq start 0)
5194         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5195           (setq start (match-end 0))
5196           (setq group (if prefix
5197                           (concat prefix (substring xrefs (match-beginning 1)
5198                                                     (match-end 1)))
5199                         (substring xrefs (match-beginning 1) (match-end 1))))
5200           (setq number
5201                 (string-to-int (substring xrefs (match-beginning 2)
5202                                           (match-end 2))))
5203           (if (setq entry (gnus-gethash group xref-hashtb))
5204               (setcdr entry (cons number (cdr entry)))
5205             (gnus-sethash group (cons number nil) xref-hashtb)))))
5206     (and start xref-hashtb)))
5207
5208 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5209   "Look through all the headers and mark the Xrefs as read."
5210   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5211         name entry info xref-hashtb idlist method nth4)
5212     (save-excursion
5213       (set-buffer gnus-group-buffer)
5214       (when (setq xref-hashtb
5215                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5216         (mapatoms
5217          (lambda (group)
5218            (unless (string= from-newsgroup (setq name (symbol-name group)))
5219              (setq idlist (symbol-value group))
5220              ;; Dead groups are not updated.
5221              (and (prog1
5222                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5223                             info (nth 2 entry))
5224                     (when (stringp (setq nth4 (gnus-info-method info)))
5225                       (setq nth4 (gnus-server-to-method nth4))))
5226                   ;; Only do the xrefs if the group has the same
5227                   ;; select method as the group we have just read.
5228                   (or (gnus-methods-equal-p
5229                        nth4 (gnus-find-method-for-group from-newsgroup))
5230                       virtual
5231                       (equal nth4 (setq method (gnus-find-method-for-group
5232                                                 from-newsgroup)))
5233                       (and (equal (car nth4) (car method))
5234                            (equal (nth 1 nth4) (nth 1 method))))
5235                   gnus-use-cross-reference
5236                   (or (not (eq gnus-use-cross-reference t))
5237                       virtual
5238                       ;; Only do cross-references on subscribed
5239                       ;; groups, if that is what is wanted.
5240                       (<= (gnus-info-level info) gnus-level-subscribed))
5241                   (gnus-group-make-articles-read name idlist))))
5242          xref-hashtb)))))
5243
5244 (defun gnus-compute-read-articles (group articles)
5245   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5246          (info (nth 2 entry))
5247          (active (gnus-active group))
5248          ninfo)
5249     (when entry
5250       ;; First peel off all invalid article numbers.
5251       (when active
5252         (let ((ids articles)
5253               id first)
5254           (while (setq id (pop ids))
5255             (when (and first (> id (cdr active)))
5256               ;; We'll end up in this situation in one particular
5257               ;; obscure situation.  If you re-scan a group and get
5258               ;; a new article that is cross-posted to a different
5259               ;; group that has not been re-scanned, you might get
5260               ;; crossposted article that has a higher number than
5261               ;; Gnus believes possible.  So we re-activate this
5262               ;; group as well.  This might mean doing the
5263               ;; crossposting thingy will *increase* the number
5264               ;; of articles in some groups.  Tsk, tsk.
5265               (setq active (or (gnus-activate-group group) active)))
5266             (when (or (> id (cdr active))
5267                       (< id (car active)))
5268               (setq articles (delq id articles))))))
5269       ;; If the read list is nil, we init it.
5270       (if (and active
5271                (null (gnus-info-read info))
5272                (> (car active) 1))
5273           (setq ninfo (cons 1 (1- (car active))))
5274         (setq ninfo (gnus-info-read info)))
5275       ;; Then we add the read articles to the range.
5276       (gnus-add-to-range
5277        ninfo (setq articles (sort articles '<))))))
5278
5279 (defun gnus-group-make-articles-read (group articles)
5280   "Update the info of GROUP to say that ARTICLES are read."
5281   (let* ((num 0)
5282          (entry (gnus-gethash group gnus-newsrc-hashtb))
5283          (info (nth 2 entry))
5284          (active (gnus-active group))
5285          range)
5286     (when entry
5287       (setq range (gnus-compute-read-articles group articles))
5288       (save-excursion
5289         (set-buffer gnus-group-buffer)
5290         (gnus-undo-register
5291           `(progn
5292              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5293              (gnus-info-set-read ',info ',(gnus-info-read info))
5294              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5295              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5296              (gnus-group-update-group ,group t))))
5297       ;; Add the read articles to the range.
5298       (gnus-info-set-read info range)
5299       (gnus-request-set-mark group (list (list range 'add '(read))))
5300       ;; Then we have to re-compute how many unread
5301       ;; articles there are in this group.
5302       (when active
5303         (cond
5304          ((not range)
5305           (setq num (- (1+ (cdr active)) (car active))))
5306          ((not (listp (cdr range)))
5307           (setq num (- (cdr active) (- (1+ (cdr range))
5308                                        (car range)))))
5309          (t
5310           (while range
5311             (if (numberp (car range))
5312                 (setq num (1+ num))
5313               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5314             (setq range (cdr range)))
5315           (setq num (- (cdr active) num))))
5316         ;; Update the number of unread articles.
5317         (setcar entry num)
5318         ;; Update the group buffer.
5319         (gnus-group-update-group group t)))))
5320
5321 (defvar gnus-newsgroup-none-id 0)
5322
5323 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5324   (let ((cur nntp-server-buffer)
5325         (dependencies
5326          (or dependencies
5327              (save-excursion (set-buffer gnus-summary-buffer)
5328                              gnus-newsgroup-dependencies)))
5329         headers id end ref
5330         (mail-parse-charset gnus-newsgroup-charset)
5331         (mail-parse-ignored-charsets
5332          (save-excursion (condition-case nil
5333                              (set-buffer gnus-summary-buffer)
5334                            (error))
5335                          gnus-newsgroup-ignored-charsets)))
5336     (save-excursion
5337       (set-buffer nntp-server-buffer)
5338       ;; Translate all TAB characters into SPACE characters.
5339       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5340       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5341       (gnus-run-hooks 'gnus-parse-headers-hook)
5342       (let ((case-fold-search t)
5343             in-reply-to header p lines chars ctype)
5344         (goto-char (point-min))
5345         ;; Search to the beginning of the next header.  Error messages
5346         ;; do not begin with 2 or 3.
5347         (while (re-search-forward "^[23][0-9]+ " nil t)
5348           (setq id nil
5349                 ref nil)
5350           ;; This implementation of this function, with nine
5351           ;; search-forwards instead of the one re-search-forward and
5352           ;; a case (which basically was the old function) is actually
5353           ;; about twice as fast, even though it looks messier.  You
5354           ;; can't have everything, I guess.  Speed and elegance
5355           ;; doesn't always go hand in hand.
5356           (setq
5357            header
5358            (make-full-mail-header
5359             ;; Number.
5360             (prog1
5361                 (read cur)
5362               (end-of-line)
5363               (setq p (point))
5364               (narrow-to-region (point)
5365                                 (or (and (search-forward "\n.\n" nil t)
5366                                          (- (point) 2))
5367                                     (point))))
5368             ;; Subject.
5369             (progn
5370               (goto-char p)
5371               (if (search-forward "\nsubject:" nil t)
5372                   (nnheader-header-value)
5373                 "(none)"))
5374             ;; From.
5375             (progn
5376               (goto-char p)
5377               (if (search-forward "\nfrom:" nil t)
5378                   (nnheader-header-value)
5379                 "(nobody)"))
5380             ;; Date.
5381             (progn
5382               (goto-char p)
5383               (if (search-forward "\ndate:" nil t)
5384                   (nnheader-header-value) ""))
5385             ;; Message-ID.
5386             (progn
5387               (goto-char p)
5388               (setq id (if (re-search-forward
5389                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5390                            ;; We do it this way to make sure the Message-ID
5391                            ;; is (somewhat) syntactically valid.
5392                            (buffer-substring (match-beginning 1)
5393                                              (match-end 1))
5394                          ;; If there was no message-id, we just fake one
5395                          ;; to make subsequent routines simpler.
5396                          (nnheader-generate-fake-message-id))))
5397             ;; References.
5398             (progn
5399               (goto-char p)
5400               (if (search-forward "\nreferences:" nil t)
5401                   (progn
5402                     (setq end (point))
5403                     (prog1
5404                         (nnheader-header-value)
5405                       (setq ref
5406                             (buffer-substring
5407                              (progn
5408                                ;; (end-of-line)
5409                                (search-backward ">" end t)
5410                                (1+ (point)))
5411                              (progn
5412                                (search-backward "<" end t)
5413                                (point))))))
5414                 ;; Get the references from the in-reply-to header if there
5415                 ;; were no references and the in-reply-to header looks
5416                 ;; promising.
5417                 (if (and (search-forward "\nin-reply-to:" nil t)
5418                          (setq in-reply-to (nnheader-header-value))
5419                          (string-match "<[^>]+>" in-reply-to))
5420                     (let (ref2)
5421                       (setq ref (substring in-reply-to (match-beginning 0)
5422                                            (match-end 0)))
5423                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5424                         (setq ref2 (substring in-reply-to (match-beginning 0)
5425                                               (match-end 0)))
5426                         (when (> (length ref2) (length ref))
5427                           (setq ref ref2)))
5428                       ref)
5429                   (setq ref nil))))
5430             ;; Chars.
5431             (progn
5432               (goto-char p)
5433               (if (search-forward "\nchars: " nil t)
5434                   (if (numberp (setq chars (ignore-errors (read cur))))
5435                       chars -1)
5436                 -1))
5437             ;; Lines.
5438             (progn
5439               (goto-char p)
5440               (if (search-forward "\nlines: " nil t)
5441                   (if (numberp (setq lines (ignore-errors (read cur))))
5442                       lines -1)
5443                 -1))
5444             ;; Xref.
5445             (progn
5446               (goto-char p)
5447               (and (search-forward "\nxref:" nil t)
5448                    (nnheader-header-value)))
5449             ;; Extra.
5450             (when gnus-extra-headers
5451               (let ((extra gnus-extra-headers)
5452                     out)
5453                 (while extra
5454                   (goto-char p)
5455                   (when (search-forward
5456                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5457                     (push (cons (car extra) (nnheader-header-value)) out))
5458                   (pop extra))
5459                 out))))
5460           (goto-char p)
5461           (if (and (search-forward "\ncontent-type: " nil t)
5462                    (setq ctype (nnheader-header-value)))
5463               (mime-entity-set-content-type-internal
5464                header (mime-parse-Content-Type ctype)))
5465           (when (equal id ref)
5466             (setq ref nil))
5467
5468           (when gnus-alter-header-function
5469             (funcall gnus-alter-header-function header)
5470             (setq id (mail-header-id header)
5471                   ref (gnus-parent-id (mail-header-references header))))
5472
5473           (when (setq header
5474                       (gnus-dependencies-add-header
5475                        header dependencies force-new))
5476             (push header headers))
5477           (goto-char (point-max))
5478           (widen))
5479         (nreverse headers)))))
5480
5481 ;; Goes through the xover lines and returns a list of vectors
5482 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5483                                                   force-new dependencies
5484                                                   group also-fetch-heads)
5485   "Parse the news overview data in the server buffer.
5486 Return a list of headers that match SEQUENCE (see
5487 `nntp-retrieve-headers')."
5488   ;; Get the Xref when the users reads the articles since most/some
5489   ;; NNTP servers do not include Xrefs when using XOVER.
5490   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5491   (let ((mail-parse-charset gnus-newsgroup-charset)
5492         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5493         (cur nntp-server-buffer)
5494         (dependencies (or dependencies gnus-newsgroup-dependencies))
5495         (allp (cond
5496                ((eq gnus-read-all-available-headers t)
5497                 t)
5498                ((stringp gnus-read-all-available-headers)
5499                 (string-match gnus-read-all-available-headers group))
5500                (t
5501                 nil)))
5502         number headers header)
5503     (save-excursion
5504       (set-buffer nntp-server-buffer)
5505       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5506       ;; Allow the user to mangle the headers before parsing them.
5507       (gnus-run-hooks 'gnus-parse-headers-hook)
5508       (goto-char (point-min))
5509       (while (not (eobp))
5510         (condition-case ()
5511             (while (and (or sequence allp)
5512                         (not (eobp)))
5513               (setq number (read cur))
5514               (when (not allp)
5515                 (while (and sequence
5516                             (< (car sequence) number))
5517                   (setq sequence (cdr sequence))))
5518               (when (and (or allp
5519                              (and sequence
5520                                   (eq number (car sequence))))
5521                          (progn
5522                            (setq sequence (cdr sequence))
5523                            (setq header (inline
5524                                           (gnus-nov-parse-line
5525                                            number dependencies force-new)))))
5526                 (push header headers))
5527               (forward-line 1))
5528           (error
5529            (gnus-error 4 "Strange nov line (%d)"
5530                        (count-lines (point-min) (point)))))
5531         (forward-line 1))
5532       ;; A common bug in inn is that if you have posted an article and
5533       ;; then retrieves the active file, it will answer correctly --
5534       ;; the new article is included.  However, a NOV entry for the
5535       ;; article may not have been generated yet, so this may fail.
5536       ;; We work around this problem by retrieving the last few
5537       ;; headers using HEAD.
5538       (if (or (not also-fetch-heads)
5539               (not sequence))
5540           ;; We (probably) got all the headers.
5541           (nreverse headers)
5542         (let ((gnus-nov-is-evil t))
5543           (nconc
5544            (nreverse headers)
5545            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5546              (gnus-get-newsgroup-headers))))))))
5547
5548 (defun gnus-article-get-xrefs ()
5549   "Fill in the Xref value in `gnus-current-headers', if necessary.
5550 This is meant to be called in `gnus-article-internal-prepare-hook'."
5551   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5552                                  gnus-current-headers)))
5553     (or (not gnus-use-cross-reference)
5554         (not headers)
5555         (and (mail-header-xref headers)
5556              (not (string= (mail-header-xref headers) "")))
5557         (let ((case-fold-search t)
5558               xref)
5559           (save-restriction
5560             (nnheader-narrow-to-headers)
5561             (goto-char (point-min))
5562             (when (or (and (not (eobp))
5563                            (eq (downcase (char-after)) ?x)
5564                            (looking-at "Xref:"))
5565                       (search-forward "\nXref:" nil t))
5566               (goto-char (1+ (match-end 0)))
5567               (setq xref (buffer-substring (point)
5568                                            (progn (end-of-line) (point))))
5569               (mail-header-set-xref headers xref)))))))
5570
5571 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5572   "Find article ID and insert the summary line for that article.
5573 OLD-HEADER can either be a header or a line number to insert
5574 the subject line on."
5575   (let* ((line (and (numberp old-header) old-header))
5576          (old-header (and (vectorp old-header) old-header))
5577          (header (cond ((and old-header use-old-header)
5578                         old-header)
5579                        ((and (numberp id)
5580                              (gnus-number-to-header id))
5581                         (gnus-number-to-header id))
5582                        (t
5583                         (gnus-read-header id))))
5584          (number (and (numberp id) id))
5585          d)
5586     (when header
5587       ;; Rebuild the thread that this article is part of and go to the
5588       ;; article we have fetched.
5589       (when (and (not gnus-show-threads)
5590                  old-header)
5591         (when (and number
5592                    (setq d (gnus-data-find (mail-header-number old-header))))
5593           (goto-char (gnus-data-pos d))
5594           (gnus-data-remove
5595            number
5596            (- (gnus-point-at-bol)
5597               (prog1
5598                   (1+ (gnus-point-at-eol))
5599                 (gnus-delete-line))))))
5600       (when old-header
5601         (mail-header-set-number header (mail-header-number old-header)))
5602       (setq gnus-newsgroup-sparse
5603             (delq (setq number (mail-header-number header))
5604                   gnus-newsgroup-sparse))
5605       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5606       (push number gnus-newsgroup-limit)
5607       (gnus-rebuild-thread (mail-header-id header) line)
5608       (gnus-summary-goto-subject number nil t))
5609     (when (and (numberp number)
5610                (> number 0))
5611       ;; We have to update the boundaries even if we can't fetch the
5612       ;; article if ID is a number -- so that the next `P' or `N'
5613       ;; command will fetch the previous (or next) article even
5614       ;; if the one we tried to fetch this time has been canceled.
5615       (when (> number gnus-newsgroup-end)
5616         (setq gnus-newsgroup-end number))
5617       (when (< number gnus-newsgroup-begin)
5618         (setq gnus-newsgroup-begin number))
5619       (setq gnus-newsgroup-unselected
5620             (delq number gnus-newsgroup-unselected)))
5621     ;; Report back a success?
5622     (and header (mail-header-number header))))
5623
5624 ;;; Process/prefix in the summary buffer
5625
5626 (defun gnus-summary-work-articles (n)
5627   "Return a list of articles to be worked upon.
5628 The prefix argument, the list of process marked articles, and the
5629 current article will be taken into consideration."
5630   (save-excursion
5631     (set-buffer gnus-summary-buffer)
5632     (cond
5633      (n
5634       ;; A numerical prefix has been given.
5635       (setq n (prefix-numeric-value n))
5636       (let ((backward (< n 0))
5637             (n (abs (prefix-numeric-value n)))
5638             articles article)
5639         (save-excursion
5640           (while
5641               (and (> n 0)
5642                    (push (setq article (gnus-summary-article-number))
5643                          articles)
5644                    (if backward
5645                        (gnus-summary-find-prev nil article)
5646                      (gnus-summary-find-next nil article)))
5647             (decf n)))
5648         (nreverse articles)))
5649      ((and (gnus-region-active-p) (mark))
5650       (message "region active")
5651       ;; Work on the region between point and mark.
5652       (let ((max (max (point) (mark)))
5653             articles article)
5654         (save-excursion
5655           (goto-char (min (point) (mark)))
5656           (while
5657               (and
5658                (push (setq article (gnus-summary-article-number)) articles)
5659                (gnus-summary-find-next nil article)
5660                (< (point) max)))
5661           (nreverse articles))))
5662      (gnus-newsgroup-processable
5663       ;; There are process-marked articles present.
5664       ;; Save current state.
5665       (gnus-summary-save-process-mark)
5666       ;; Return the list.
5667       (reverse gnus-newsgroup-processable))
5668      (t
5669       ;; Just return the current article.
5670       (list (gnus-summary-article-number))))))
5671
5672 (defmacro gnus-summary-iterate (arg &rest forms)
5673   "Iterate over the process/prefixed articles and do FORMS.
5674 ARG is the interactive prefix given to the command.  FORMS will be
5675 executed with point over the summary line of the articles."
5676   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5677     `(let ((,articles (gnus-summary-work-articles ,arg)))
5678        (while ,articles
5679          (gnus-summary-goto-subject (car ,articles))
5680          ,@forms
5681          (pop ,articles)))))
5682
5683 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5684 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5685
5686 (defun gnus-summary-save-process-mark ()
5687   "Push the current set of process marked articles on the stack."
5688   (interactive)
5689   (push (copy-sequence gnus-newsgroup-processable)
5690         gnus-newsgroup-process-stack))
5691
5692 (defun gnus-summary-kill-process-mark ()
5693   "Push the current set of process marked articles on the stack and unmark."
5694   (interactive)
5695   (gnus-summary-save-process-mark)
5696   (gnus-summary-unmark-all-processable))
5697
5698 (defun gnus-summary-yank-process-mark ()
5699   "Pop the last process mark state off the stack and restore it."
5700   (interactive)
5701   (unless gnus-newsgroup-process-stack
5702     (error "Empty mark stack"))
5703   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5704
5705 (defun gnus-summary-process-mark-set (set)
5706   "Make SET into the current process marked articles."
5707   (gnus-summary-unmark-all-processable)
5708   (while set
5709     (gnus-summary-set-process-mark (pop set))))
5710
5711 ;;; Searching and stuff
5712
5713 (defun gnus-summary-search-group (&optional backward use-level)
5714   "Search for next unread newsgroup.
5715 If optional argument BACKWARD is non-nil, search backward instead."
5716   (save-excursion
5717     (set-buffer gnus-group-buffer)
5718     (when (gnus-group-search-forward
5719            backward nil (if use-level (gnus-group-group-level) nil))
5720       (gnus-group-group-name))))
5721
5722 (defun gnus-summary-best-group (&optional exclude-group)
5723   "Find the name of the best unread group.
5724 If EXCLUDE-GROUP, do not go to this group."
5725   (save-excursion
5726     (set-buffer gnus-group-buffer)
5727     (save-excursion
5728       (gnus-group-best-unread-group exclude-group))))
5729
5730 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5731   (if backward (gnus-summary-find-prev)
5732     (let* ((dummy (gnus-summary-article-intangible-p))
5733            (article (or article (gnus-summary-article-number)))
5734            (arts (gnus-data-find-list article))
5735            result)
5736       (when (and (not dummy)
5737                  (or (not gnus-summary-check-current)
5738                      (not unread)
5739                      (not (gnus-data-unread-p (car arts)))))
5740         (setq arts (cdr arts)))
5741       (when (setq result
5742                   (if unread
5743                       (progn
5744                         (while arts
5745                           (when (or (and undownloaded
5746                                          (eq gnus-undownloaded-mark
5747                                              (gnus-data-mark (car arts))))
5748                                     (gnus-data-unread-p (car arts)))
5749                             (setq result (car arts)
5750                                   arts nil))
5751                           (setq arts (cdr arts)))
5752                         result)
5753                     (car arts)))
5754         (goto-char (gnus-data-pos result))
5755         (gnus-data-number result)))))
5756
5757 (defun gnus-summary-find-prev (&optional unread article)
5758   (let* ((eobp (eobp))
5759          (article (or article (gnus-summary-article-number)))
5760          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5761          result)
5762     (when (and (not eobp)
5763                (or (not gnus-summary-check-current)
5764                    (not unread)
5765                    (not (gnus-data-unread-p (car arts)))))
5766       (setq arts (cdr arts)))
5767     (when (setq result
5768                 (if unread
5769                     (progn
5770                       (while arts
5771                         (when (gnus-data-unread-p (car arts))
5772                           (setq result (car arts)
5773                                 arts nil))
5774                         (setq arts (cdr arts)))
5775                       result)
5776                   (car arts)))
5777       (goto-char (gnus-data-pos result))
5778       (gnus-data-number result))))
5779
5780 (defun gnus-summary-find-subject (subject &optional unread backward article)
5781   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5782          (article (or article (gnus-summary-article-number)))
5783          (articles (gnus-data-list backward))
5784          (arts (gnus-data-find-list article articles))
5785          result)
5786     (when (or (not gnus-summary-check-current)
5787               (not unread)
5788               (not (gnus-data-unread-p (car arts))))
5789       (setq arts (cdr arts)))
5790     (while arts
5791       (and (or (not unread)
5792                (gnus-data-unread-p (car arts)))
5793            (vectorp (gnus-data-header (car arts)))
5794            (gnus-subject-equal
5795             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5796            (setq result (car arts)
5797                  arts nil))
5798       (setq arts (cdr arts)))
5799     (and result
5800          (goto-char (gnus-data-pos result))
5801          (gnus-data-number result))))
5802
5803 (defun gnus-summary-search-forward (&optional unread subject backward)
5804   "Search forward for an article.
5805 If UNREAD, look for unread articles.  If SUBJECT, look for
5806 articles with that subject.  If BACKWARD, search backward instead."
5807   (cond (subject (gnus-summary-find-subject subject unread backward))
5808         (backward (gnus-summary-find-prev unread))
5809         (t (gnus-summary-find-next unread))))
5810
5811 (defun gnus-recenter (&optional n)
5812   "Center point in window and redisplay frame.
5813 Also do horizontal recentering."
5814   (interactive "P")
5815   (when (and gnus-auto-center-summary
5816              (not (eq gnus-auto-center-summary 'vertical)))
5817     (gnus-horizontal-recenter))
5818   (recenter n))
5819
5820 (defun gnus-summary-recenter ()
5821   "Center point in the summary window.
5822 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5823 displayed, no centering will be performed."
5824   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5825   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5826   (interactive)
5827   (let* ((top (cond ((< (window-height) 4) 0)
5828                     ((< (window-height) 7) 1)
5829                     (t (if (numberp gnus-auto-center-summary)
5830                            gnus-auto-center-summary
5831                          2))))
5832          (height (1- (window-height)))
5833          (bottom (save-excursion (goto-char (point-max))
5834                                  (forward-line (- height))
5835                                  (point)))
5836          (window (get-buffer-window (current-buffer))))
5837     ;; The user has to want it.
5838     (when gnus-auto-center-summary
5839       (when (get-buffer-window gnus-article-buffer)
5840         ;; Only do recentering when the article buffer is displayed,
5841         ;; Set the window start to either `bottom', which is the biggest
5842         ;; possible valid number, or the second line from the top,
5843         ;; whichever is the least.
5844         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5845           (if (> bottom top-pos)
5846               ;; Keep the second line from the top visible
5847               (set-window-start window top-pos t)
5848             ;; Try to keep the bottom line visible; if it's partially
5849             ;; obscured, either scroll one more line to make it fully
5850             ;; visible, or revert to using TOP-POS.
5851             (save-excursion
5852               (goto-char (point-max))
5853               (forward-line -1)
5854               (let ((last-line-start (point)))
5855                 (goto-char bottom)
5856                 (set-window-start window (point) t)
5857                 (when (not (pos-visible-in-window-p last-line-start window))
5858                   (forward-line 1)
5859                   (set-window-start window (min (point) top-pos) t)))))))
5860       ;; Do horizontal recentering while we're at it.
5861       (when (and (get-buffer-window (current-buffer) t)
5862                  (not (eq gnus-auto-center-summary 'vertical)))
5863         (let ((selected (selected-window)))
5864           (select-window (get-buffer-window (current-buffer) t))
5865           (gnus-summary-position-point)
5866           (gnus-horizontal-recenter)
5867           (select-window selected))))))
5868
5869 (defun gnus-summary-jump-to-group (newsgroup)
5870   "Move point to NEWSGROUP in group mode buffer."
5871   ;; Keep update point of group mode buffer if visible.
5872   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5873       (save-window-excursion
5874         ;; Take care of tree window mode.
5875         (when (get-buffer-window gnus-group-buffer)
5876           (pop-to-buffer gnus-group-buffer))
5877         (gnus-group-jump-to-group newsgroup))
5878     (save-excursion
5879       ;; Take care of tree window mode.
5880       (if (get-buffer-window gnus-group-buffer)
5881           (pop-to-buffer gnus-group-buffer)
5882         (set-buffer gnus-group-buffer))
5883       (gnus-group-jump-to-group newsgroup))))
5884
5885 ;; This function returns a list of article numbers based on the
5886 ;; difference between the ranges of read articles in this group and
5887 ;; the range of active articles.
5888 (defun gnus-list-of-unread-articles (group)
5889   (let* ((read (gnus-info-read (gnus-get-info group)))
5890          (active (or (gnus-active group) (gnus-activate-group group)))
5891          (last (cdr active))
5892          first nlast unread)
5893     ;; If none are read, then all are unread.
5894     (if (not read)
5895         (setq first (car active))
5896       ;; If the range of read articles is a single range, then the
5897       ;; first unread article is the article after the last read
5898       ;; article.  Sounds logical, doesn't it?
5899       (if (and (not (listp (cdr read)))
5900                (or (< (car read) (car active))
5901                    (progn (setq read (list read))
5902                           nil)))
5903           (setq first (max (car active) (1+ (cdr read))))
5904         ;; `read' is a list of ranges.
5905         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5906                                   (caar read)))
5907                   1)
5908           (setq first (car active)))
5909         (while read
5910           (when first
5911             (while (< first nlast)
5912               (push first unread)
5913               (setq first (1+ first))))
5914           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5915           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5916           (setq read (cdr read)))))
5917     ;; And add the last unread articles.
5918     (while (<= first last)
5919       (push first unread)
5920       (setq first (1+ first)))
5921     ;; Return the list of unread articles.
5922     (delq 0 (nreverse unread))))
5923
5924 (defun gnus-list-of-read-articles (group)
5925   "Return a list of unread, unticked and non-dormant articles."
5926   (let* ((info (gnus-get-info group))
5927          (marked (gnus-info-marks info))
5928          (active (gnus-active group)))
5929     (and info active
5930          (gnus-set-difference
5931           (gnus-sorted-complement
5932            (gnus-uncompress-range active)
5933            (gnus-list-of-unread-articles group))
5934           (append
5935            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5936            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5937
5938 ;; Various summary commands
5939
5940 (defun gnus-summary-select-article-buffer ()
5941   "Reconfigure windows to show article buffer."
5942   (interactive)
5943   (if (not (gnus-buffer-live-p gnus-article-buffer))
5944       (error "There is no article buffer for this summary buffer")
5945     (gnus-configure-windows 'article)
5946     (select-window (get-buffer-window gnus-article-buffer))))
5947
5948 (defun gnus-summary-universal-argument (arg)
5949   "Perform any operation on all articles that are process/prefixed."
5950   (interactive "P")
5951   (let ((articles (gnus-summary-work-articles arg))
5952         func article)
5953     (if (eq
5954          (setq
5955           func
5956           (key-binding
5957            (read-key-sequence
5958             (substitute-command-keys
5959              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5960          'undefined)
5961         (gnus-error 1 "Undefined key")
5962       (save-excursion
5963         (while articles
5964           (gnus-summary-goto-subject (setq article (pop articles)))
5965           (let (gnus-newsgroup-processable)
5966             (command-execute func))
5967           (gnus-summary-remove-process-mark article)))))
5968   (gnus-summary-position-point))
5969
5970 (defun gnus-summary-toggle-truncation (&optional arg)
5971   "Toggle truncation of summary lines.
5972 With arg, turn line truncation on iff arg is positive."
5973   (interactive "P")
5974   (setq truncate-lines
5975         (if (null arg) (not truncate-lines)
5976           (> (prefix-numeric-value arg) 0)))
5977   (redraw-display))
5978
5979 (defun gnus-summary-reselect-current-group (&optional all rescan)
5980   "Exit and then reselect the current newsgroup.
5981 The prefix argument ALL means to select all articles."
5982   (interactive "P")
5983   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5984     (error "Ephemeral groups can't be reselected"))
5985   (let ((current-subject (gnus-summary-article-number))
5986         (group gnus-newsgroup-name))
5987     (setq gnus-newsgroup-begin nil)
5988     (gnus-summary-exit)
5989     ;; We have to adjust the point of group mode buffer because
5990     ;; point was moved to the next unread newsgroup by exiting.
5991     (gnus-summary-jump-to-group group)
5992     (when rescan
5993       (save-excursion
5994         (save-window-excursion
5995           ;; Don't show group contents.
5996           (set-window-start (selected-window) (point-max))
5997           (gnus-group-get-new-news-this-group 1))))
5998     (gnus-group-read-group all t)
5999     (gnus-summary-goto-subject current-subject nil t)))
6000
6001 (defun gnus-summary-rescan-group (&optional all)
6002   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6003   (interactive "P")
6004   (gnus-summary-reselect-current-group all t))
6005
6006 (defun gnus-summary-update-info (&optional non-destructive)
6007   (save-excursion
6008     (let ((group gnus-newsgroup-name))
6009       (when group
6010         (when gnus-newsgroup-kill-headers
6011           (setq gnus-newsgroup-killed
6012                 (gnus-compress-sequence
6013                  (nconc
6014                   (gnus-set-sorted-intersection
6015                    (gnus-uncompress-range gnus-newsgroup-killed)
6016                    (setq gnus-newsgroup-unselected
6017                          (sort gnus-newsgroup-unselected '<)))
6018                   (setq gnus-newsgroup-unreads
6019                         (sort gnus-newsgroup-unreads '<)))
6020                  t)))
6021         (unless (listp (cdr gnus-newsgroup-killed))
6022           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6023         (let ((headers gnus-newsgroup-headers))
6024           ;; Set the new ranges of read articles.
6025           (save-excursion
6026             (set-buffer gnus-group-buffer)
6027             (gnus-undo-force-boundary))
6028           (gnus-update-read-articles
6029            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
6030           ;; Set the current article marks.
6031           (let ((gnus-newsgroup-scored
6032                  (if (and (not gnus-save-score)
6033                           (not non-destructive))
6034                      nil
6035                    gnus-newsgroup-scored)))
6036             (save-excursion
6037               (gnus-update-marks)))
6038           ;; Do the cross-ref thing.
6039           (when gnus-use-cross-reference
6040             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6041           ;; Do not switch windows but change the buffer to work.
6042           (set-buffer gnus-group-buffer)
6043           (unless (gnus-ephemeral-group-p group)
6044             (gnus-group-update-group group)))))))
6045
6046 (defun gnus-summary-save-newsrc (&optional force)
6047   "Save the current number of read/marked articles in the dribble buffer.
6048 The dribble buffer will then be saved.
6049 If FORCE (the prefix), also save the .newsrc file(s)."
6050   (interactive "P")
6051   (gnus-summary-update-info t)
6052   (if force
6053       (gnus-save-newsrc-file)
6054     (gnus-dribble-save)))
6055
6056 (defun gnus-summary-exit (&optional temporary)
6057   "Exit reading current newsgroup, and then return to group selection mode.
6058 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6059   (interactive)
6060   (gnus-set-global-variables)
6061   (gnus-kill-save-kill-buffer)
6062   (gnus-async-halt-prefetch)
6063   (let* ((group gnus-newsgroup-name)
6064          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6065          (mode major-mode)
6066          (group-point nil)
6067          (buf (current-buffer)))
6068     (unless quit-config
6069       ;; Do adaptive scoring, and possibly save score files.
6070       (when gnus-newsgroup-adaptive
6071         (gnus-score-adaptive))
6072       (when gnus-use-scoring
6073         (gnus-score-save)))
6074     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6075     ;; If we have several article buffers, we kill them at exit.
6076     (unless gnus-single-article-buffer
6077       (gnus-kill-buffer gnus-original-article-buffer)
6078       (setq gnus-article-current nil))
6079     (when gnus-use-cache
6080       (gnus-cache-possibly-remove-articles)
6081       (gnus-cache-save-buffers))
6082     (gnus-async-prefetch-remove-group group)
6083     (when gnus-suppress-duplicates
6084       (gnus-dup-enter-articles))
6085     (when gnus-use-trees
6086       (gnus-tree-close group))
6087     (when gnus-use-cache
6088       (gnus-cache-write-active))
6089     ;; Remove entries for this group.
6090     (nnmail-purge-split-history (gnus-group-real-name group))
6091     ;; Make all changes in this group permanent.
6092     (unless quit-config
6093       (gnus-run-hooks 'gnus-exit-group-hook)
6094       (gnus-summary-update-info))
6095     (gnus-close-group group)
6096     ;; Make sure where we were, and go to next newsgroup.
6097     (set-buffer gnus-group-buffer)
6098     (unless quit-config
6099       (gnus-group-jump-to-group group))
6100     (gnus-run-hooks 'gnus-summary-exit-hook)
6101     (unless (or quit-config
6102                 ;; If this group has disappeared from the summary
6103                 ;; buffer, don't skip forwards.
6104                 (not (string= group (gnus-group-group-name))))
6105       (gnus-group-next-unread-group 1))
6106     (setq group-point (point))
6107     (if temporary
6108         nil                             ;Nothing to do.
6109       ;; If we have several article buffers, we kill them at exit.
6110       (unless gnus-single-article-buffer
6111         (gnus-kill-buffer gnus-article-buffer)
6112         (gnus-kill-buffer gnus-original-article-buffer)
6113         (setq gnus-article-current nil))
6114       (set-buffer buf)
6115       (if (not gnus-kill-summary-on-exit)
6116           (progn
6117             (gnus-deaden-summary)
6118             (setq mode nil))
6119         ;; We set all buffer-local variables to nil.  It is unclear why
6120         ;; this is needed, but if we don't, buffer-local variables are
6121         ;; not garbage-collected, it seems.  This would the lead to en
6122         ;; ever-growing Emacs.
6123         (gnus-summary-clear-local-variables)
6124         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6125           (gnus-summary-clear-local-variables))
6126         (when (get-buffer gnus-article-buffer)
6127           (bury-buffer gnus-article-buffer))
6128         ;; We clear the global counterparts of the buffer-local
6129         ;; variables as well, just to be on the safe side.
6130         (set-buffer gnus-group-buffer)
6131         (gnus-summary-clear-local-variables)
6132         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6133           (gnus-summary-clear-local-variables)))
6134       (setq gnus-current-select-method gnus-select-method)
6135       (pop-to-buffer gnus-group-buffer)
6136       (if (not quit-config)
6137           (progn
6138             (goto-char group-point)
6139             (gnus-configure-windows 'group 'force)
6140             (unless (pos-visible-in-window-p)
6141               (forward-line (/ (static-if (featurep 'xemacs)
6142                                    (window-displayed-height)
6143                                  (1- (window-height)))
6144                                -2))
6145               (set-window-start (selected-window) (point))
6146               (goto-char group-point)))
6147         (gnus-handle-ephemeral-exit quit-config))
6148       ;; Return to group mode buffer.
6149       (when (eq mode 'gnus-summary-mode)
6150         (gnus-kill-buffer buf))
6151       ;; Clear the current group name.
6152       (unless quit-config
6153         (setq gnus-newsgroup-name nil)))))
6154
6155 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6156 (defun gnus-summary-exit-no-update (&optional no-questions)
6157   "Quit reading current newsgroup without updating read article info."
6158   (interactive)
6159   (let* ((group gnus-newsgroup-name)
6160          (quit-config (gnus-group-quit-config group)))
6161     (when (or no-questions
6162               gnus-expert-user
6163               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6164       (gnus-async-halt-prefetch)
6165       (mapcar 'funcall
6166               (delq 'gnus-summary-expire-articles
6167                     (copy-sequence gnus-summary-prepare-exit-hook)))
6168       ;; If we have several article buffers, we kill them at exit.
6169       (unless gnus-single-article-buffer
6170         (gnus-kill-buffer gnus-article-buffer)
6171         (gnus-kill-buffer gnus-original-article-buffer)
6172         (setq gnus-article-current nil))
6173       (if (not gnus-kill-summary-on-exit)
6174           (gnus-deaden-summary)
6175         (gnus-close-group group)
6176         (gnus-summary-clear-local-variables)
6177         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6178           (gnus-summary-clear-local-variables))
6179         (set-buffer gnus-group-buffer)
6180         (gnus-summary-clear-local-variables)
6181         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6182           (gnus-summary-clear-local-variables))
6183         (when (get-buffer gnus-summary-buffer)
6184           (kill-buffer gnus-summary-buffer)))
6185       (unless gnus-single-article-buffer
6186         (setq gnus-article-current nil))
6187       (when gnus-use-trees
6188         (gnus-tree-close group))
6189       (gnus-async-prefetch-remove-group group)
6190       (when (get-buffer gnus-article-buffer)
6191         (bury-buffer gnus-article-buffer))
6192       ;; Return to the group buffer.
6193       (gnus-configure-windows 'group 'force)
6194       ;; Clear the current group name.
6195       (setq gnus-newsgroup-name nil)
6196       (when (equal (gnus-group-group-name) group)
6197         (gnus-group-next-unread-group 1))
6198       (when quit-config
6199         (gnus-handle-ephemeral-exit quit-config)))))
6200
6201 (defun gnus-handle-ephemeral-exit (quit-config)
6202   "Handle movement when leaving an ephemeral group.
6203 The state which existed when entering the ephemeral is reset."
6204   (if (not (buffer-name (car quit-config)))
6205       (gnus-configure-windows 'group 'force)
6206     (set-buffer (car quit-config))
6207     (cond ((eq major-mode 'gnus-summary-mode)
6208            (gnus-set-global-variables))
6209           ((eq major-mode 'gnus-article-mode)
6210            (save-excursion
6211              ;; The `gnus-summary-buffer' variable may point
6212              ;; to the old summary buffer when using a single
6213              ;; article buffer.
6214              (unless (gnus-buffer-live-p gnus-summary-buffer)
6215                (set-buffer gnus-group-buffer))
6216              (set-buffer gnus-summary-buffer)
6217              (gnus-set-global-variables))))
6218     (if (or (eq (cdr quit-config) 'article)
6219             (eq (cdr quit-config) 'pick))
6220         (progn
6221           ;; The current article may be from the ephemeral group
6222           ;; thus it is best that we reload this article
6223           (gnus-summary-show-article)
6224           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6225               (gnus-configure-windows 'pick 'force)
6226             (gnus-configure-windows (cdr quit-config) 'force)))
6227       (gnus-configure-windows (cdr quit-config) 'force))
6228     (when (eq major-mode 'gnus-summary-mode)
6229       (gnus-summary-next-subject 1 nil t)
6230       (gnus-summary-recenter)
6231       (gnus-summary-position-point))))
6232
6233 (defun gnus-summary-preview-mime-message ()
6234   "MIME decode and play this message."
6235   (interactive)
6236   (let ((gnus-break-pages nil)
6237         (gnus-show-mime t))
6238     (gnus-summary-select-article gnus-show-all-headers t))
6239   (select-window (get-buffer-window gnus-article-buffer)))
6240
6241 ;;; Dead summaries.
6242
6243 (defvar gnus-dead-summary-mode-map nil)
6244
6245 (unless gnus-dead-summary-mode-map
6246   (setq gnus-dead-summary-mode-map (make-keymap))
6247   (suppress-keymap gnus-dead-summary-mode-map)
6248   (substitute-key-definition
6249    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6250   (dolist (key '("\C-d" "\r" "\177" [delete]))
6251     (define-key gnus-dead-summary-mode-map
6252       key 'gnus-summary-wake-up-the-dead))
6253   (dolist (key '("q" "Q"))
6254     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6255
6256 (defvar gnus-dead-summary-mode nil
6257   "Minor mode for Gnus summary buffers.")
6258
6259 (defun gnus-dead-summary-mode (&optional arg)
6260   "Minor mode for Gnus summary buffers."
6261   (interactive "P")
6262   (when (eq major-mode 'gnus-summary-mode)
6263     (make-local-variable 'gnus-dead-summary-mode)
6264     (setq gnus-dead-summary-mode
6265           (if (null arg) (not gnus-dead-summary-mode)
6266             (> (prefix-numeric-value arg) 0)))
6267     (when gnus-dead-summary-mode
6268       (gnus-add-minor-mode
6269        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6270
6271 (defun gnus-deaden-summary ()
6272   "Make the current summary buffer into a dead summary buffer."
6273   ;; Kill any previous dead summary buffer.
6274   (when (and gnus-dead-summary
6275              (buffer-name gnus-dead-summary))
6276     (save-excursion
6277       (set-buffer gnus-dead-summary)
6278       (when gnus-dead-summary-mode
6279         (kill-buffer (current-buffer)))))
6280   ;; Make this the current dead summary.
6281   (setq gnus-dead-summary (current-buffer))
6282   (gnus-dead-summary-mode 1)
6283   (let ((name (buffer-name)))
6284     (when (string-match "Summary" name)
6285       (rename-buffer
6286        (concat (substring name 0 (match-beginning 0)) "Dead "
6287                (substring name (match-beginning 0)))
6288        t)
6289       (bury-buffer))))
6290
6291 (defun gnus-kill-or-deaden-summary (buffer)
6292   "Kill or deaden the summary BUFFER."
6293   (save-excursion
6294     (when (and (buffer-name buffer)
6295                (not gnus-single-article-buffer))
6296       (save-excursion
6297         (set-buffer buffer)
6298         (gnus-kill-buffer gnus-article-buffer)
6299         (gnus-kill-buffer gnus-original-article-buffer)))
6300     (cond
6301      ;; Kill the buffer.
6302      (gnus-kill-summary-on-exit
6303       (when (and gnus-use-trees
6304                  (gnus-buffer-exists-p buffer))
6305         (save-excursion
6306           (set-buffer buffer)
6307           (gnus-tree-close gnus-newsgroup-name)))
6308       (gnus-kill-buffer buffer))
6309      ;; Deaden the buffer.
6310      ((gnus-buffer-exists-p buffer)
6311       (save-excursion
6312         (set-buffer buffer)
6313         (gnus-deaden-summary))))))
6314
6315 (defun gnus-summary-wake-up-the-dead (&rest args)
6316   "Wake up the dead summary buffer."
6317   (interactive)
6318   (gnus-dead-summary-mode -1)
6319   (let ((name (buffer-name)))
6320     (when (string-match "Dead " name)
6321       (rename-buffer
6322        (concat (substring name 0 (match-beginning 0))
6323                (substring name (match-end 0)))
6324        t)))
6325   (gnus-message 3 "This dead summary is now alive again"))
6326
6327 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6328 (defun gnus-summary-fetch-faq (&optional faq-dir)
6329   "Fetch the FAQ for the current group.
6330 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6331 in."
6332   (interactive
6333    (list
6334     (when current-prefix-arg
6335       (completing-read
6336        "Faq dir: " (and (listp gnus-group-faq-directory)
6337                         (mapcar (lambda (file) (list file))
6338                                 gnus-group-faq-directory))))))
6339   (let (gnus-faq-buffer)
6340     (when (setq gnus-faq-buffer
6341                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6342       (gnus-configure-windows 'summary-faq))))
6343
6344 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6345 (defun gnus-summary-describe-group (&optional force)
6346   "Describe the current newsgroup."
6347   (interactive "P")
6348   (gnus-group-describe-group force gnus-newsgroup-name))
6349
6350 (defun gnus-summary-describe-briefly ()
6351   "Describe summary mode commands briefly."
6352   (interactive)
6353   (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")))
6354
6355 ;; Walking around group mode buffer from summary mode.
6356
6357 (defun gnus-summary-next-group (&optional no-article target-group backward)
6358   "Exit current newsgroup and then select next unread newsgroup.
6359 If prefix argument NO-ARTICLE is non-nil, no article is selected
6360 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6361 previous group instead."
6362   (interactive "P")
6363   ;; Stop pre-fetching.
6364   (gnus-async-halt-prefetch)
6365   (let ((current-group gnus-newsgroup-name)
6366         (current-buffer (current-buffer))
6367         entered)
6368     ;; First we semi-exit this group to update Xrefs and all variables.
6369     ;; We can't do a real exit, because the window conf must remain
6370     ;; the same in case the user is prompted for info, and we don't
6371     ;; want the window conf to change before that...
6372     (gnus-summary-exit t)
6373     (while (not entered)
6374       ;; Then we find what group we are supposed to enter.
6375       (set-buffer gnus-group-buffer)
6376       (gnus-group-jump-to-group current-group)
6377       (setq target-group
6378             (or target-group
6379                 (if (eq gnus-keep-same-level 'best)
6380                     (gnus-summary-best-group gnus-newsgroup-name)
6381                   (gnus-summary-search-group backward gnus-keep-same-level))))
6382       (if (not target-group)
6383           ;; There are no further groups, so we return to the group
6384           ;; buffer.
6385           (progn
6386             (gnus-message 5 "Returning to the group buffer")
6387             (setq entered t)
6388             (when (gnus-buffer-live-p current-buffer)
6389               (set-buffer current-buffer)
6390               (gnus-summary-exit))
6391             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6392         ;; We try to enter the target group.
6393         (gnus-group-jump-to-group target-group)
6394         (let ((unreads (gnus-group-group-unread)))
6395           (if (and (or (eq t unreads)
6396                        (and unreads (not (zerop unreads))))
6397                    (gnus-summary-read-group
6398                     target-group nil no-article
6399                     (and (buffer-name current-buffer) current-buffer)
6400                     nil backward))
6401               (setq entered t)
6402             (setq current-group target-group
6403                   target-group nil)))))))
6404
6405 (defun gnus-summary-prev-group (&optional no-article)
6406   "Exit current newsgroup and then select previous unread newsgroup.
6407 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6408   (interactive "P")
6409   (gnus-summary-next-group no-article nil t))
6410
6411 ;; Walking around summary lines.
6412
6413 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6414   "Go to the first unread subject.
6415 If UNREAD is non-nil, go to the first unread article.
6416 Returns the article selected or nil if there are no unread articles."
6417   (interactive "P")
6418   (prog1
6419       (cond
6420        ;; Empty summary.
6421        ((null gnus-newsgroup-data)
6422         (gnus-message 3 "No articles in the group")
6423         nil)
6424        ;; Pick the first article.
6425        ((not unread)
6426         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6427         (gnus-data-number (car gnus-newsgroup-data)))
6428        ;; No unread articles.
6429        ((null gnus-newsgroup-unreads)
6430         (gnus-message 3 "No more unread articles")
6431         nil)
6432        ;; Find the first unread article.
6433        (t
6434         (let ((data gnus-newsgroup-data))
6435           (while (and data
6436                       (and (not (and undownloaded
6437                                      (eq gnus-undownloaded-mark
6438                                          (gnus-data-mark (car data)))))
6439                            (if unseen
6440                                (or (not (memq
6441                                          (gnus-data-number (car data))
6442                                          gnus-newsgroup-unseen))
6443                                    (not (gnus-data-unread-p (car data))))
6444                              (not (gnus-data-unread-p (car data))))))
6445             (setq data (cdr data)))
6446           (when data
6447             (goto-char (gnus-data-pos (car data)))
6448             (gnus-data-number (car data))))))
6449     (gnus-summary-position-point)))
6450
6451 (defun gnus-summary-next-subject (n &optional unread dont-display)
6452   "Go to next N'th summary line.
6453 If N is negative, go to the previous N'th subject line.
6454 If UNREAD is non-nil, only unread articles are selected.
6455 The difference between N and the actual number of steps taken is
6456 returned."
6457   (interactive "p")
6458   (let ((backward (< n 0))
6459         (n (abs n)))
6460     (while (and (> n 0)
6461                 (if backward
6462                     (gnus-summary-find-prev unread)
6463                   (gnus-summary-find-next unread)))
6464       (unless (zerop (setq n (1- n)))
6465         (gnus-summary-show-thread)))
6466     (when (/= 0 n)
6467       (gnus-message 7 "No more%s articles"
6468                     (if unread " unread" "")))
6469     (unless dont-display
6470       (gnus-summary-recenter)
6471       (gnus-summary-position-point))
6472     n))
6473
6474 (defun gnus-summary-next-unread-subject (n)
6475   "Go to next N'th unread summary line."
6476   (interactive "p")
6477   (gnus-summary-next-subject n t))
6478
6479 (defun gnus-summary-prev-subject (n &optional unread)
6480   "Go to previous N'th summary line.
6481 If optional argument UNREAD is non-nil, only unread article is selected."
6482   (interactive "p")
6483   (gnus-summary-next-subject (- n) unread))
6484
6485 (defun gnus-summary-prev-unread-subject (n)
6486   "Go to previous N'th unread summary line."
6487   (interactive "p")
6488   (gnus-summary-next-subject (- n) t))
6489
6490 (defun gnus-summary-goto-subject (article &optional force silent)
6491   "Go the subject line of ARTICLE.
6492 If FORCE, also allow jumping to articles not currently shown."
6493   (interactive "nArticle number: ")
6494   (unless (numberp article)
6495     (error "Article %s is not a number" article))
6496   (let ((b (point))
6497         (data (gnus-data-find article)))
6498     ;; We read in the article if we have to.
6499     (and (not data)
6500          force
6501          (gnus-summary-insert-subject
6502           article
6503           (if (or (numberp force) (vectorp force)) force)
6504           t)
6505          (setq data (gnus-data-find article)))
6506     (goto-char b)
6507     (if (not data)
6508         (progn
6509           (unless silent
6510             (gnus-message 3 "Can't find article %d" article))
6511           nil)
6512       (let ((pt (gnus-data-pos data)))
6513         (goto-char pt)
6514         (gnus-summary-set-article-display-arrow pt))
6515       (gnus-summary-position-point)
6516       article)))
6517
6518 ;; Walking around summary lines with displaying articles.
6519
6520 (defun gnus-summary-expand-window (&optional arg)
6521   "Make the summary buffer take up the entire Emacs frame.
6522 Given a prefix, will force an `article' buffer configuration."
6523   (interactive "P")
6524   (if arg
6525       (gnus-configure-windows 'article 'force)
6526     (gnus-configure-windows 'summary 'force)))
6527
6528 (defun gnus-summary-display-article (article &optional all-header)
6529   "Display ARTICLE in article buffer."
6530   (when (gnus-buffer-live-p gnus-article-buffer)
6531     (with-current-buffer gnus-article-buffer
6532       (set-buffer-multibyte t)))
6533   (gnus-set-global-variables)
6534   (when (gnus-buffer-live-p gnus-article-buffer)
6535     (with-current-buffer gnus-article-buffer
6536       (setq gnus-article-charset gnus-newsgroup-charset)
6537       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6538       (set-buffer-multibyte t)))
6539   (if (null article)
6540       nil
6541     (prog1
6542         (if gnus-summary-display-article-function
6543             (funcall gnus-summary-display-article-function article all-header)
6544           (gnus-article-prepare article all-header))
6545       (with-current-buffer gnus-article-buffer
6546         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6547              nil))
6548       (gnus-run-hooks 'gnus-select-article-hook)
6549       (when (and gnus-current-article
6550                  (not (zerop gnus-current-article)))
6551         (gnus-summary-goto-subject gnus-current-article))
6552       (gnus-summary-recenter)
6553       (when (and gnus-use-trees gnus-show-threads)
6554         (gnus-possibly-generate-tree article)
6555         (gnus-highlight-selected-tree article))
6556       ;; Successfully display article.
6557       (gnus-article-set-window-start
6558        (cdr (assq article gnus-newsgroup-bookmarks))))))
6559
6560 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6561   "Select the current article.
6562 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6563 non-nil, the article will be re-fetched even if it already present in
6564 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6565 be displayed."
6566   ;; Make sure we are in the summary buffer to work around bbdb bug.
6567   (unless (eq major-mode 'gnus-summary-mode)
6568     (set-buffer gnus-summary-buffer))
6569   (let ((article (or article (gnus-summary-article-number)))
6570         (all-headers (not (not all-headers))) ;Must be T or NIL.
6571         gnus-summary-display-article-function)
6572     (and (not pseudo)
6573          (gnus-summary-article-pseudo-p article)
6574          (error "This is a pseudo-article"))
6575     (save-excursion
6576       (set-buffer gnus-summary-buffer)
6577       (if (or (and gnus-single-article-buffer
6578                    (or (null gnus-current-article)
6579                        (null gnus-article-current)
6580                        (null (get-buffer gnus-article-buffer))
6581                        (not (eq article (cdr gnus-article-current)))
6582                        (not (equal (car gnus-article-current)
6583                                    gnus-newsgroup-name))))
6584               (and (not gnus-single-article-buffer)
6585                    (or (null gnus-current-article)
6586                        (not (eq gnus-current-article article))))
6587               force)
6588           ;; The requested article is different from the current article.
6589           (progn
6590             (gnus-summary-display-article article all-headers)
6591 ;;; Hidden headers are not hidden text any more.
6592 ;;          (when (or all-headers gnus-show-all-headers)
6593 ;;            (gnus-article-show-all-headers))
6594             (gnus-article-set-window-start
6595              (cdr (assq article gnus-newsgroup-bookmarks)))
6596             article)
6597 ;;      (when (or all-headers gnus-show-all-headers)
6598 ;;        (gnus-article-show-all-headers))
6599         'old))))
6600
6601 (defun gnus-summary-force-verify-and-decrypt ()
6602   (interactive)
6603   (let ((mm-verify-option 'known)
6604         (mm-decrypt-option 'known))
6605     (gnus-summary-select-article nil 'force)))
6606
6607 (defun gnus-summary-set-current-mark (&optional current-mark)
6608   "Obsolete function."
6609   nil)
6610
6611 (defun gnus-summary-next-article (&optional unread subject backward push)
6612   "Select the next article.
6613 If UNREAD, only unread articles are selected.
6614 If SUBJECT, only articles with SUBJECT are selected.
6615 If BACKWARD, the previous article is selected instead of the next."
6616   (interactive "P")
6617   (cond
6618    ;; Is there such an article?
6619    ((and (gnus-summary-search-forward unread subject backward)
6620          (or (gnus-summary-display-article (gnus-summary-article-number))
6621              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6622     (gnus-summary-position-point))
6623    ;; If not, we try the first unread, if that is wanted.
6624    ((and subject
6625          gnus-auto-select-same
6626          (gnus-summary-first-unread-article))
6627     (gnus-summary-position-point)
6628     (gnus-message 6 "Wrapped"))
6629    ;; Try to get next/previous article not displayed in this group.
6630    ((and gnus-auto-extend-newsgroup
6631          (not unread) (not subject))
6632     (gnus-summary-goto-article
6633      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6634      nil (count-lines (point-min) (point))))
6635    ;; Go to next/previous group.
6636    (t
6637     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6638       (gnus-summary-jump-to-group gnus-newsgroup-name))
6639     (let ((cmd last-command-char)
6640           (point
6641            (save-excursion
6642              (set-buffer gnus-group-buffer)
6643              (point)))
6644           (group
6645            (if (eq gnus-keep-same-level 'best)
6646                (gnus-summary-best-group gnus-newsgroup-name)
6647              (gnus-summary-search-group backward gnus-keep-same-level))))
6648       ;; For some reason, the group window gets selected.  We change
6649       ;; it back.
6650       (select-window (get-buffer-window (current-buffer)))
6651       ;; Select next unread newsgroup automagically.
6652       (cond
6653        ((or (not gnus-auto-select-next)
6654             (not cmd))
6655         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6656        ((or (eq gnus-auto-select-next 'quietly)
6657             (and (eq gnus-auto-select-next 'slightly-quietly)
6658                  push)
6659             (and (eq gnus-auto-select-next 'almost-quietly)
6660                  (gnus-summary-last-article-p)))
6661         ;; Select quietly.
6662         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6663             (gnus-summary-exit)
6664           (gnus-message 7 "No more%s articles (%s)..."
6665                         (if unread " unread" "")
6666                         (if group (concat "selecting " group)
6667                           "exiting"))
6668           (gnus-summary-next-group nil group backward)))
6669        (t
6670         (when (gnus-key-press-event-p last-input-event)
6671           (gnus-summary-walk-group-buffer
6672            gnus-newsgroup-name cmd unread backward point))))))))
6673
6674 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6675   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6676                       (?\C-p (gnus-group-prev-unread-group 1))))
6677         (cursor-in-echo-area t)
6678         keve key group ended)
6679     (save-excursion
6680       (set-buffer gnus-group-buffer)
6681       (goto-char start)
6682       (setq group
6683             (if (eq gnus-keep-same-level 'best)
6684                 (gnus-summary-best-group gnus-newsgroup-name)
6685               (gnus-summary-search-group backward gnus-keep-same-level))))
6686     (while (not ended)
6687       (gnus-message
6688        5 "No more%s articles%s" (if unread " unread" "")
6689        (if (and group
6690                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6691            (format " (Type %s for %s [%s])"
6692                    (single-key-description cmd) group
6693                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6694          (format " (Type %s to exit %s)"
6695                  (single-key-description cmd)
6696                  gnus-newsgroup-name)))
6697       ;; Confirm auto selection.
6698       (setq key (car (setq keve (gnus-read-event-char))))
6699       (setq ended t)
6700       (cond
6701        ((assq key keystrokes)
6702         (let ((obuf (current-buffer)))
6703           (switch-to-buffer gnus-group-buffer)
6704           (when group
6705             (gnus-group-jump-to-group group))
6706           (eval (cadr (assq key keystrokes)))
6707           (setq group (gnus-group-group-name))
6708           (switch-to-buffer obuf))
6709         (setq ended nil))
6710        ((equal key cmd)
6711         (if (or (not group)
6712                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6713             (gnus-summary-exit)
6714           (gnus-summary-next-group nil group backward)))
6715        (t
6716         (push (cdr keve) unread-command-events))))))
6717
6718 (defun gnus-summary-next-unread-article ()
6719   "Select unread article after current one."
6720   (interactive)
6721   (gnus-summary-next-article
6722    (or (not (eq gnus-summary-goto-unread 'never))
6723        (gnus-summary-last-article-p (gnus-summary-article-number)))
6724    (and gnus-auto-select-same
6725         (gnus-summary-article-subject))))
6726
6727 (defun gnus-summary-prev-article (&optional unread subject)
6728   "Select the article after the current one.
6729 If UNREAD is non-nil, only unread articles are selected."
6730   (interactive "P")
6731   (gnus-summary-next-article unread subject t))
6732
6733 (defun gnus-summary-prev-unread-article ()
6734   "Select unread article before current one."
6735   (interactive)
6736   (gnus-summary-prev-article
6737    (or (not (eq gnus-summary-goto-unread 'never))
6738        (gnus-summary-first-article-p (gnus-summary-article-number)))
6739    (and gnus-auto-select-same
6740         (gnus-summary-article-subject))))
6741
6742 (defun gnus-summary-next-page (&optional lines circular)
6743   "Show next page of the selected article.
6744 If at the end of the current article, select the next article.
6745 LINES says how many lines should be scrolled up.
6746
6747 If CIRCULAR is non-nil, go to the start of the article instead of
6748 selecting the next article when reaching the end of the current
6749 article."
6750   (interactive "P")
6751   (setq gnus-summary-buffer (current-buffer))
6752   (gnus-set-global-variables)
6753   (let ((article (gnus-summary-article-number))
6754         (article-window (get-buffer-window gnus-article-buffer t))
6755         endp)
6756     ;; If the buffer is empty, we have no article.
6757     (unless article
6758       (error "No article to select"))
6759     (gnus-configure-windows 'article)
6760     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6761         (if (and (eq gnus-summary-goto-unread 'never)
6762                  (not (gnus-summary-last-article-p article)))
6763             (gnus-summary-next-article)
6764           (gnus-summary-next-unread-article))
6765       (if (or (null gnus-current-article)
6766               (null gnus-article-current)
6767               (/= article (cdr gnus-article-current))
6768               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6769           ;; Selected subject is different from current article's.
6770           (gnus-summary-display-article article)
6771         (when article-window
6772           (gnus-eval-in-buffer-window gnus-article-buffer
6773             (setq endp (gnus-article-next-page lines)))
6774           (when endp
6775             (cond (circular
6776                    (gnus-summary-beginning-of-article))
6777                   (lines
6778                    (gnus-message 3 "End of message"))
6779                   ((null lines)
6780                    (if (and (eq gnus-summary-goto-unread 'never)
6781                             (not (gnus-summary-last-article-p article)))
6782                        (gnus-summary-next-article)
6783                      (gnus-summary-next-unread-article))))))))
6784     (gnus-summary-recenter)
6785     (gnus-summary-position-point)))
6786
6787 (defun gnus-summary-prev-page (&optional lines move)
6788   "Show previous page of selected article.
6789 Argument LINES specifies lines to be scrolled down.
6790 If MOVE, move to the previous unread article if point is at
6791 the beginning of the buffer."
6792   (interactive "P")
6793   (let ((article (gnus-summary-article-number))
6794         (article-window (get-buffer-window gnus-article-buffer t))
6795         endp)
6796     (gnus-configure-windows 'article)
6797     (if (or (null gnus-current-article)
6798             (null gnus-article-current)
6799             (/= article (cdr gnus-article-current))
6800             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6801         ;; Selected subject is different from current article's.
6802         (gnus-summary-display-article article)
6803       (gnus-summary-recenter)
6804       (when article-window
6805         (gnus-eval-in-buffer-window gnus-article-buffer
6806           (setq endp (gnus-article-prev-page lines)))
6807         (when (and move endp)
6808           (cond (lines
6809                  (gnus-message 3 "Beginning of message"))
6810                 ((null lines)
6811                  (if (and (eq gnus-summary-goto-unread 'never)
6812                           (not (gnus-summary-first-article-p article)))
6813                      (gnus-summary-prev-article)
6814                    (gnus-summary-prev-unread-article))))))))
6815   (gnus-summary-position-point))
6816
6817 (defun gnus-summary-prev-page-or-article (&optional lines)
6818   "Show previous page of selected article.
6819 Argument LINES specifies lines to be scrolled down.
6820 If at the beginning of the article, go to the next article."
6821   (interactive "P")
6822   (gnus-summary-prev-page lines t))
6823
6824 (defun gnus-summary-scroll-up (lines)
6825   "Scroll up (or down) one line current article.
6826 Argument LINES specifies lines to be scrolled up (or down if negative)."
6827   (interactive "p")
6828   (gnus-configure-windows 'article)
6829   (gnus-summary-show-thread)
6830   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6831     (gnus-eval-in-buffer-window gnus-article-buffer
6832       (cond ((> lines 0)
6833              (when (gnus-article-next-page lines)
6834                (gnus-message 3 "End of message")))
6835             ((< lines 0)
6836              (gnus-article-prev-page (- lines))))))
6837   (gnus-summary-recenter)
6838   (gnus-summary-position-point))
6839
6840 (defun gnus-summary-scroll-down (lines)
6841   "Scroll down (or up) one line current article.
6842 Argument LINES specifies lines to be scrolled down (or up if negative)."
6843   (interactive "p")
6844   (gnus-summary-scroll-up (- lines)))
6845
6846 (defun gnus-summary-next-same-subject ()
6847   "Select next article which has the same subject as current one."
6848   (interactive)
6849   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6850
6851 (defun gnus-summary-prev-same-subject ()
6852   "Select previous article which has the same subject as current one."
6853   (interactive)
6854   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6855
6856 (defun gnus-summary-next-unread-same-subject ()
6857   "Select next unread article which has the same subject as current one."
6858   (interactive)
6859   (gnus-summary-next-article t (gnus-summary-article-subject)))
6860
6861 (defun gnus-summary-prev-unread-same-subject ()
6862   "Select previous unread article which has the same subject as current one."
6863   (interactive)
6864   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6865
6866 (defun gnus-summary-first-unread-article ()
6867   "Select the first unread article.
6868 Return nil if there are no unread articles."
6869   (interactive)
6870   (prog1
6871       (when (gnus-summary-first-subject t)
6872         (gnus-summary-show-thread)
6873         (gnus-summary-first-subject t)
6874         (gnus-summary-display-article (gnus-summary-article-number)))
6875     (gnus-summary-position-point)))
6876
6877 (defun gnus-summary-first-unread-subject ()
6878   "Place the point on the subject line of the first unread article.
6879 Return nil if there are no unread articles."
6880   (interactive)
6881   (prog1
6882       (when (gnus-summary-first-subject t)
6883         (gnus-summary-show-thread)
6884         (gnus-summary-first-subject t))
6885     (gnus-summary-position-point)))
6886
6887 (defun gnus-summary-first-unseen-subject ()
6888   "Place the point on the subject line of the first unseen article.
6889 Return nil if there are no unseen articles."
6890   (interactive)
6891   (prog1
6892       (when (gnus-summary-first-subject t t t)
6893         (gnus-summary-show-thread)
6894         (gnus-summary-first-subject t t t))
6895     (gnus-summary-position-point)))
6896
6897 (defun gnus-summary-first-unseen-or-unread-subject ()
6898   "Place the point on the subject line of the first unseen article.
6899 Return nil if there are no unseen articles."
6900   (interactive)
6901   (prog1
6902       (unless (when (gnus-summary-first-subject t t t)
6903                 (gnus-summary-show-thread)
6904                 (gnus-summary-first-subject t t t))
6905         (when (gnus-summary-first-subject t)
6906           (gnus-summary-show-thread)
6907           (gnus-summary-first-subject t)))
6908     (gnus-summary-position-point)))
6909
6910 (defun gnus-summary-first-article ()
6911   "Select the first article.
6912 Return nil if there are no articles."
6913   (interactive)
6914   (prog1
6915       (when (gnus-summary-first-subject)
6916         (gnus-summary-show-thread)
6917         (gnus-summary-first-subject)
6918         (gnus-summary-display-article (gnus-summary-article-number)))
6919     (gnus-summary-position-point)))
6920
6921 (defun gnus-summary-best-unread-article (&optional arg)
6922   "Select the unread article with the highest score.
6923 If given a prefix argument, select the next unread article that has a
6924 score higher than the default score."
6925   (interactive "P")
6926   (let ((article (if arg
6927                      (gnus-summary-better-unread-subject)
6928                    (gnus-summary-best-unread-subject))))
6929     (if article
6930         (gnus-summary-goto-article article)
6931       (error "No unread articles"))))
6932
6933 (defun gnus-summary-best-unread-subject ()
6934   "Select the unread subject with the highest score."
6935   (interactive)
6936   (let ((best -1000000)
6937         (data gnus-newsgroup-data)
6938         article score)
6939     (while data
6940       (and (gnus-data-unread-p (car data))
6941            (> (setq score
6942                     (gnus-summary-article-score (gnus-data-number (car data))))
6943               best)
6944            (setq best score
6945                  article (gnus-data-number (car data))))
6946       (setq data (cdr data)))
6947     (when article
6948       (gnus-summary-goto-subject article))
6949     (gnus-summary-position-point)
6950     article))
6951
6952 (defun gnus-summary-better-unread-subject ()
6953   "Select the first unread subject that has a score over the default score."
6954   (interactive)
6955   (let ((data gnus-newsgroup-data)
6956         article score)
6957     (while (and (setq article (gnus-data-number (car data)))
6958                 (or (gnus-data-read-p (car data))
6959                     (not (> (gnus-summary-article-score article)
6960                             gnus-summary-default-score))))
6961       (setq data (cdr data)))
6962     (when article
6963       (gnus-summary-goto-subject article))
6964     (gnus-summary-position-point)
6965     article))
6966
6967 (defun gnus-summary-last-subject ()
6968   "Go to the last displayed subject line in the group."
6969   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6970     (when article
6971       (gnus-summary-goto-subject article))))
6972
6973 (defun gnus-summary-goto-article (article &optional all-headers force)
6974   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6975 If ALL-HEADERS is non-nil, no header lines are hidden.
6976 If FORCE, go to the article even if it isn't displayed.  If FORCE
6977 is a number, it is the line the article is to be displayed on."
6978   (interactive
6979    (list
6980     (completing-read
6981      "Article number or Message-ID: "
6982      (mapcar (lambda (number) (list (int-to-string number)))
6983              gnus-newsgroup-limit))
6984     current-prefix-arg
6985     t))
6986   (prog1
6987       (if (and (stringp article)
6988                (string-match "@" article))
6989           (gnus-summary-refer-article article)
6990         (when (stringp article)
6991           (setq article (string-to-number article)))
6992         (if (gnus-summary-goto-subject article force)
6993             (gnus-summary-display-article article all-headers)
6994           (gnus-message 4 "Couldn't go to article %s" article) nil))
6995     (gnus-summary-position-point)))
6996
6997 (defun gnus-summary-goto-last-article ()
6998   "Go to the previously read article."
6999   (interactive)
7000   (prog1
7001       (when gnus-last-article
7002         (gnus-summary-goto-article gnus-last-article nil t))
7003     (gnus-summary-position-point)))
7004
7005 (defun gnus-summary-pop-article (number)
7006   "Pop one article off the history and go to the previous.
7007 NUMBER articles will be popped off."
7008   (interactive "p")
7009   (let (to)
7010     (setq gnus-newsgroup-history
7011           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7012     (if to
7013         (gnus-summary-goto-article (car to) nil t)
7014       (error "Article history empty")))
7015   (gnus-summary-position-point))
7016
7017 ;; Summary commands and functions for limiting the summary buffer.
7018
7019 (defun gnus-summary-limit-to-articles (n)
7020   "Limit the summary buffer to the next N articles.
7021 If not given a prefix, use the process marked articles instead."
7022   (interactive "P")
7023   (prog1
7024       (let ((articles (gnus-summary-work-articles n)))
7025         (setq gnus-newsgroup-processable nil)
7026         (gnus-summary-limit articles))
7027     (gnus-summary-position-point)))
7028
7029 (defun gnus-summary-pop-limit (&optional total)
7030   "Restore the previous limit.
7031 If given a prefix, remove all limits."
7032   (interactive "P")
7033   (when total
7034     (setq gnus-newsgroup-limits
7035           (list (mapcar (lambda (h) (mail-header-number h))
7036                         gnus-newsgroup-headers))))
7037   (unless gnus-newsgroup-limits
7038     (error "No limit to pop"))
7039   (prog1
7040       (gnus-summary-limit nil 'pop)
7041     (gnus-summary-position-point)))
7042
7043 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7044   "Limit the summary buffer to articles that have subjects that match a regexp.
7045 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7046   (interactive
7047    (list (read-string (if current-prefix-arg
7048                           "Exclude subject (regexp): "
7049                         "Limit to subject (regexp): "))
7050          nil current-prefix-arg))
7051   (unless header
7052     (setq header "subject"))
7053   (when (not (equal "" subject))
7054     (prog1
7055         (let ((articles (gnus-summary-find-matching
7056                          (or header "subject") subject 'all nil nil
7057                          not-matching)))
7058           (unless articles
7059             (error "Found no matches for \"%s\"" subject))
7060           (gnus-summary-limit articles))
7061       (gnus-summary-position-point))))
7062
7063 (defun gnus-summary-limit-to-author (from &optional not-matching)
7064   "Limit the summary buffer to articles that have authors that match a regexp.
7065 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7066   (interactive
7067    (list (read-string (if current-prefix-arg
7068                           "Exclude author (regexp): "
7069                         "Limit to author (regexp): "))
7070          current-prefix-arg))
7071   (gnus-summary-limit-to-subject from "from" not-matching))
7072
7073 (defun gnus-summary-limit-to-age (age &optional younger-p)
7074   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7075 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7076 articles that are younger than AGE days."
7077   (interactive
7078    (let ((younger current-prefix-arg)
7079          (days-got nil)
7080          days)
7081      (while (not days-got)
7082        (setq days (if younger
7083                       (read-string "Limit to articles within (in days): ")
7084                     (read-string "Limit to articles older than (in days): ")))
7085        (when (> (length days) 0)
7086          (setq days (read days)))
7087        (if (numberp days)
7088            (progn
7089              (setq days-got t)
7090              (if (< days 0)
7091                  (progn
7092                    (setq younger (not younger))
7093                    (setq days (* days -1)))))
7094          (message "Please enter a number.")
7095          (sleep-for 1)))
7096      (list days younger)))
7097   (prog1
7098       (let ((data gnus-newsgroup-data)
7099             (cutoff (days-to-time age))
7100             articles d date is-younger)
7101         (while (setq d (pop data))
7102           (when (and (vectorp (gnus-data-header d))
7103                      (setq date (mail-header-date (gnus-data-header d))))
7104             (setq is-younger (time-less-p
7105                               (time-since (condition-case ()
7106                                               (date-to-time date)
7107                                             (error '(0 0))))
7108                               cutoff))
7109             (when (if younger-p
7110                       is-younger
7111                     (not is-younger))
7112               (push (gnus-data-number d) articles))))
7113         (gnus-summary-limit (nreverse articles)))
7114     (gnus-summary-position-point)))
7115
7116 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7117   "Limit the summary buffer to articles that match an 'extra' header."
7118   (interactive
7119    (let ((header
7120           (intern
7121            (gnus-completing-read-with-default
7122             (symbol-name (car gnus-extra-headers))
7123             (if current-prefix-arg
7124                 "Exclude extra header:"
7125               "Limit extra header:")
7126             (mapcar (lambda (x)
7127                       (cons (symbol-name x) x))
7128                     gnus-extra-headers)
7129             nil
7130             t))))
7131      (list header
7132            (read-string (format "%s header %s (regexp): "
7133                                 (if current-prefix-arg "Exclude" "Limit to")
7134                                 header))
7135            current-prefix-arg)))
7136   (when (not (equal "" regexp))
7137     (prog1
7138         (let ((articles (gnus-summary-find-matching
7139                          (cons 'extra header) regexp 'all nil nil
7140                          not-matching)))
7141           (unless articles
7142             (error "Found no matches for \"%s\"" regexp))
7143           (gnus-summary-limit articles))
7144       (gnus-summary-position-point))))
7145
7146 (defun gnus-summary-limit-to-display-predicate ()
7147   "Limit the summary buffer to the predicated in the `display' group parameter."
7148   (interactive)
7149   (unless gnus-newsgroup-display
7150     (error "There is no `display' group parameter"))
7151   (let (articles)
7152     (dolist (number gnus-newsgroup-articles)
7153       (when (funcall gnus-newsgroup-display)
7154         (push number articles)))
7155     (gnus-summary-limit articles))
7156   (gnus-summary-position-point))
7157
7158 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7159 (make-obsolete
7160  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7161
7162 (defun gnus-summary-limit-to-unread (&optional all)
7163   "Limit the summary buffer to articles that are not marked as read.
7164 If ALL is non-nil, limit strictly to unread articles."
7165   (interactive "P")
7166   (if all
7167       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7168     (gnus-summary-limit-to-marks
7169      ;; Concat all the marks that say that an article is read and have
7170      ;; those removed.
7171      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7172            gnus-killed-mark gnus-kill-file-mark
7173            gnus-low-score-mark gnus-expirable-mark
7174            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7175            gnus-duplicate-mark gnus-souped-mark)
7176      'reverse)))
7177
7178 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7179 (make-obsolete 'gnus-summary-delete-marked-with
7180                'gnus-summary-limit-exlude-marks)
7181
7182 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7183   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7184 If REVERSE, limit the summary buffer to articles that are marked
7185 with MARKS.  MARKS can either be a string of marks or a list of marks.
7186 Returns how many articles were removed."
7187   (interactive "sMarks: ")
7188   (gnus-summary-limit-to-marks marks t))
7189
7190 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7191   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7192 If REVERSE (the prefix), limit the summary buffer to articles that are
7193 not marked with MARKS.  MARKS can either be a string of marks or a
7194 list of marks.
7195 Returns how many articles were removed."
7196   (interactive "sMarks: \nP")
7197   (prog1
7198       (let ((data gnus-newsgroup-data)
7199             (marks (if (listp marks) marks
7200                      (append marks nil))) ; Transform to list.
7201             articles)
7202         (while data
7203           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7204                   (memq (gnus-data-mark (car data)) marks))
7205             (push (gnus-data-number (car data)) articles))
7206           (setq data (cdr data)))
7207         (gnus-summary-limit articles))
7208     (gnus-summary-position-point)))
7209
7210 (defun gnus-summary-limit-to-score (score)
7211   "Limit to articles with score at or above SCORE."
7212   (interactive "NLimit to articles with score of at least: ")
7213   (let ((data gnus-newsgroup-data)
7214         articles)
7215     (while data
7216       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7217                 score)
7218         (push (gnus-data-number (car data)) articles))
7219       (setq data (cdr data)))
7220     (prog1
7221         (gnus-summary-limit articles)
7222       (gnus-summary-position-point))))
7223
7224 (defun gnus-summary-limit-include-thread (id)
7225   "Display all the hidden articles that is in the thread with ID in it.
7226 When called interactively, ID is the Message-ID of the current
7227 article."
7228   (interactive (list (mail-header-id (gnus-summary-article-header))))
7229   (let ((articles (gnus-articles-in-thread
7230                    (gnus-id-to-thread (gnus-root-id id)))))
7231     (prog1
7232         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7233       (gnus-summary-limit-include-matching-articles
7234        "subject"
7235        (regexp-quote (gnus-simplify-subject-re
7236                       (mail-header-subject (gnus-id-to-header id)))))
7237       (gnus-summary-position-point))))
7238
7239 (defun gnus-summary-limit-include-matching-articles (header regexp)
7240   "Display all the hidden articles that have HEADERs that match REGEXP."
7241   (interactive (list (read-string "Match on header: ")
7242                      (read-string "Regexp: ")))
7243   (let ((articles (gnus-find-matching-articles header regexp)))
7244     (prog1
7245         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7246       (gnus-summary-position-point))))
7247
7248 (defun gnus-summary-limit-include-dormant ()
7249   "Display all the hidden articles that are marked as dormant.
7250 Note that this command only works on a subset of the articles currently
7251 fetched for this group."
7252   (interactive)
7253   (unless gnus-newsgroup-dormant
7254     (error "There are no dormant articles in this group"))
7255   (prog1
7256       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7257     (gnus-summary-position-point)))
7258
7259 (defun gnus-summary-limit-exclude-dormant ()
7260   "Hide all dormant articles."
7261   (interactive)
7262   (prog1
7263       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7264     (gnus-summary-position-point)))
7265
7266 (defun gnus-summary-limit-exclude-childless-dormant ()
7267   "Hide all dormant articles that have no children."
7268   (interactive)
7269   (let ((data (gnus-data-list t))
7270         articles d children)
7271     ;; Find all articles that are either not dormant or have
7272     ;; children.
7273     (while (setq d (pop data))
7274       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7275                 (and (setq children
7276                            (gnus-article-children (gnus-data-number d)))
7277                      (let (found)
7278                        (while children
7279                          (when (memq (car children) articles)
7280                            (setq children nil
7281                                  found t))
7282                          (pop children))
7283                        found)))
7284         (push (gnus-data-number d) articles)))
7285     ;; Do the limiting.
7286     (prog1
7287         (gnus-summary-limit articles)
7288       (gnus-summary-position-point))))
7289
7290 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7291   "Mark all unread excluded articles as read.
7292 If ALL, mark even excluded ticked and dormants as read."
7293   (interactive "P")
7294   (let ((articles (gnus-sorted-complement
7295                    (sort
7296                     (mapcar (lambda (h) (mail-header-number h))
7297                             gnus-newsgroup-headers)
7298                     '<)
7299                    (sort gnus-newsgroup-limit '<)))
7300         article)
7301     (setq gnus-newsgroup-unreads
7302           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7303     (if all
7304         (setq gnus-newsgroup-dormant nil
7305               gnus-newsgroup-marked nil
7306               gnus-newsgroup-reads
7307               (nconc
7308                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7309                gnus-newsgroup-reads))
7310       (while (setq article (pop articles))
7311         (unless (or (memq article gnus-newsgroup-dormant)
7312                     (memq article gnus-newsgroup-marked))
7313           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7314
7315 (defun gnus-summary-limit (articles &optional pop)
7316   (if pop
7317       ;; We pop the previous limit off the stack and use that.
7318       (setq articles (car gnus-newsgroup-limits)
7319             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7320     ;; We use the new limit, so we push the old limit on the stack.
7321     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7322   ;; Set the limit.
7323   (setq gnus-newsgroup-limit articles)
7324   (let ((total (length gnus-newsgroup-data))
7325         (data (gnus-data-find-list (gnus-summary-article-number)))
7326         (gnus-summary-mark-below nil)   ; Inhibit this.
7327         found)
7328     ;; This will do all the work of generating the new summary buffer
7329     ;; according to the new limit.
7330     (gnus-summary-prepare)
7331     ;; Hide any threads, possibly.
7332     (gnus-summary-maybe-hide-threads)
7333     ;; Try to return to the article you were at, or one in the
7334     ;; neighborhood.
7335     (when data
7336       ;; We try to find some article after the current one.
7337       (while data
7338         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7339           (setq data nil
7340                 found t))
7341         (setq data (cdr data))))
7342     (unless found
7343       ;; If there is no data, that means that we were after the last
7344       ;; article.  The same goes when we can't find any articles
7345       ;; after the current one.
7346       (goto-char (point-max))
7347       (gnus-summary-find-prev))
7348     (gnus-set-mode-line 'summary)
7349     ;; We return how many articles were removed from the summary
7350     ;; buffer as a result of the new limit.
7351     (- total (length gnus-newsgroup-data))))
7352
7353 (defsubst gnus-invisible-cut-children (threads)
7354   (let ((num 0))
7355     (while threads
7356       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7357         (incf num))
7358       (pop threads))
7359     (< num 2)))
7360
7361 (defsubst gnus-cut-thread (thread)
7362   "Go forwards in the thread until we find an article that we want to display."
7363   (when (or (eq gnus-fetch-old-headers 'some)
7364             (eq gnus-fetch-old-headers 'invisible)
7365             (numberp gnus-fetch-old-headers)
7366             (eq gnus-build-sparse-threads 'some)
7367             (eq gnus-build-sparse-threads 'more))
7368     ;; Deal with old-fetched headers and sparse threads.
7369     (while (and
7370             thread
7371             (or
7372              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7373              (gnus-summary-article-ancient-p
7374               (mail-header-number (car thread))))
7375             (if (or (<= (length (cdr thread)) 1)
7376                     (eq gnus-fetch-old-headers 'invisible))
7377                 (setq gnus-newsgroup-limit
7378                       (delq (mail-header-number (car thread))
7379                             gnus-newsgroup-limit)
7380                       thread (cadr thread))
7381               (when (gnus-invisible-cut-children (cdr thread))
7382                 (let ((th (cdr thread)))
7383                   (while th
7384                     (if (memq (mail-header-number (caar th))
7385                               gnus-newsgroup-limit)
7386                         (setq thread (car th)
7387                               th nil)
7388                       (setq th (cdr th))))))))))
7389   thread)
7390
7391 (defun gnus-cut-threads (threads)
7392   "Cut off all uninteresting articles from the beginning of threads."
7393   (when (or (eq gnus-fetch-old-headers 'some)
7394             (eq gnus-fetch-old-headers 'invisible)
7395             (numberp gnus-fetch-old-headers)
7396             (eq gnus-build-sparse-threads 'some)
7397             (eq gnus-build-sparse-threads 'more))
7398     (let ((th threads))
7399       (while th
7400         (setcar th (gnus-cut-thread (car th)))
7401         (setq th (cdr th)))))
7402   ;; Remove nixed out threads.
7403   (delq nil threads))
7404
7405 (defun gnus-summary-initial-limit (&optional show-if-empty)
7406   "Figure out what the initial limit is supposed to be on group entry.
7407 This entails weeding out unwanted dormants, low-scored articles,
7408 fetch-old-headers verbiage, and so on."
7409   ;; Most groups have nothing to remove.
7410   (if (or gnus-inhibit-limiting
7411           (and (null gnus-newsgroup-dormant)
7412                (eq gnus-newsgroup-display 'gnus-not-ignore)
7413                (not (eq gnus-fetch-old-headers 'some))
7414                (not (numberp gnus-fetch-old-headers))
7415                (not (eq gnus-fetch-old-headers 'invisible))
7416                (null gnus-summary-expunge-below)
7417                (not (eq gnus-build-sparse-threads 'some))
7418                (not (eq gnus-build-sparse-threads 'more))
7419                (null gnus-thread-expunge-below)
7420                (not gnus-use-nocem)))
7421       ()                                ; Do nothing.
7422     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7423     (setq gnus-newsgroup-limit nil)
7424     (mapatoms
7425      (lambda (node)
7426        (unless (car (symbol-value node))
7427          ;; These threads have no parents -- they are roots.
7428          (let ((nodes (cdr (symbol-value node)))
7429                thread)
7430            (while nodes
7431              (if (and gnus-thread-expunge-below
7432                       (< (gnus-thread-total-score (car nodes))
7433                          gnus-thread-expunge-below))
7434                  (gnus-expunge-thread (pop nodes))
7435                (setq thread (pop nodes))
7436                (gnus-summary-limit-children thread))))))
7437      gnus-newsgroup-dependencies)
7438     ;; If this limitation resulted in an empty group, we might
7439     ;; pop the previous limit and use it instead.
7440     (when (and (not gnus-newsgroup-limit)
7441                show-if-empty)
7442       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7443     gnus-newsgroup-limit))
7444
7445 (defun gnus-summary-limit-children (thread)
7446   "Return 1 if this subthread is visible and 0 if it is not."
7447   ;; First we get the number of visible children to this thread.  This
7448   ;; is done by recursing down the thread using this function, so this
7449   ;; will really go down to a leaf article first, before slowly
7450   ;; working its way up towards the root.
7451   (when thread
7452     (let ((children
7453            (if (cdr thread)
7454                (apply '+ (mapcar 'gnus-summary-limit-children
7455                                  (cdr thread)))
7456              0))
7457           (number (mail-header-number (car thread)))
7458           score)
7459       (if (and
7460            (not (memq number gnus-newsgroup-marked))
7461            (or
7462             ;; If this article is dormant and has absolutely no visible
7463             ;; children, then this article isn't visible.
7464             (and (memq number gnus-newsgroup-dormant)
7465                  (zerop children))
7466             ;; If this is "fetch-old-headered" and there is no
7467             ;; visible children, then we don't want this article.
7468             (and (or (eq gnus-fetch-old-headers 'some)
7469                      (numberp gnus-fetch-old-headers))
7470                  (gnus-summary-article-ancient-p number)
7471                  (zerop children))
7472             ;; If this is "fetch-old-headered" and `invisible', then
7473             ;; we don't want this article.
7474             (and (eq gnus-fetch-old-headers 'invisible)
7475                  (gnus-summary-article-ancient-p number))
7476             ;; If this is a sparsely inserted article with no children,
7477             ;; we don't want it.
7478             (and (eq gnus-build-sparse-threads 'some)
7479                  (gnus-summary-article-sparse-p number)
7480                  (zerop children))
7481             ;; If we use expunging, and this article is really
7482             ;; low-scored, then we don't want this article.
7483             (when (and gnus-summary-expunge-below
7484                        (< (setq score
7485                                 (or (cdr (assq number gnus-newsgroup-scored))
7486                                     gnus-summary-default-score))
7487                           gnus-summary-expunge-below))
7488               ;; We increase the expunge-tally here, but that has
7489               ;; nothing to do with the limits, really.
7490               (incf gnus-newsgroup-expunged-tally)
7491               ;; We also mark as read here, if that's wanted.
7492               (when (and gnus-summary-mark-below
7493                          (< score gnus-summary-mark-below))
7494                 (setq gnus-newsgroup-unreads
7495                       (delq number gnus-newsgroup-unreads))
7496                 (if gnus-newsgroup-auto-expire
7497                     (push number gnus-newsgroup-expirable)
7498                   (push (cons number gnus-low-score-mark)
7499                         gnus-newsgroup-reads)))
7500               t)
7501             ;; Do the `display' group parameter.
7502             (and gnus-newsgroup-display
7503                  (not (funcall gnus-newsgroup-display)))
7504             ;; Check NoCeM things.
7505             (if (and gnus-use-nocem
7506                      (gnus-nocem-unwanted-article-p
7507                       (mail-header-id (car thread))))
7508                 (progn
7509                   (setq gnus-newsgroup-unreads
7510                         (delq number gnus-newsgroup-unreads))
7511                   t))))
7512           ;; Nope, invisible article.
7513           0
7514         ;; Ok, this article is to be visible, so we add it to the limit
7515         ;; and return 1.
7516         (push number gnus-newsgroup-limit)
7517         1))))
7518
7519 (defun gnus-expunge-thread (thread)
7520   "Mark all articles in THREAD as read."
7521   (let* ((number (mail-header-number (car thread))))
7522     (incf gnus-newsgroup-expunged-tally)
7523     ;; We also mark as read here, if that's wanted.
7524     (setq gnus-newsgroup-unreads
7525           (delq number gnus-newsgroup-unreads))
7526     (if gnus-newsgroup-auto-expire
7527         (push number gnus-newsgroup-expirable)
7528       (push (cons number gnus-low-score-mark)
7529             gnus-newsgroup-reads)))
7530   ;; Go recursively through all subthreads.
7531   (mapcar 'gnus-expunge-thread (cdr thread)))
7532
7533 ;; Summary article oriented commands
7534
7535 (defun gnus-summary-refer-parent-article (n)
7536   "Refer parent article N times.
7537 If N is negative, go to ancestor -N instead.
7538 The difference between N and the number of articles fetched is returned."
7539   (interactive "p")
7540   (let ((skip 1)
7541         error header ref)
7542     (when (not (natnump n))
7543       (setq skip (abs n)
7544             n 1))
7545     (while (and (> n 0)
7546                 (not error))
7547       (setq header (gnus-summary-article-header))
7548       (if (and (eq (mail-header-number header)
7549                    (cdr gnus-article-current))
7550                (equal gnus-newsgroup-name
7551                       (car gnus-article-current)))
7552           ;; If we try to find the parent of the currently
7553           ;; displayed article, then we take a look at the actual
7554           ;; References header, since this is slightly more
7555           ;; reliable than the References field we got from the
7556           ;; server.
7557           (save-excursion
7558             (set-buffer gnus-original-article-buffer)
7559             (nnheader-narrow-to-headers)
7560             (unless (setq ref (message-fetch-field "references"))
7561               (setq ref (message-fetch-field "in-reply-to")))
7562             (widen))
7563         (setq ref
7564               ;; It's not the current article, so we take a bet on
7565               ;; the value we got from the server.
7566               (mail-header-references header)))
7567       (if (and ref
7568                (not (equal ref "")))
7569           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7570             (gnus-message 1 "Couldn't find parent"))
7571         (gnus-message 1 "No references in article %d"
7572                       (gnus-summary-article-number))
7573         (setq error t))
7574       (decf n))
7575     (gnus-summary-position-point)
7576     n))
7577
7578 (defun gnus-summary-refer-references ()
7579   "Fetch all articles mentioned in the References header.
7580 Return the number of articles fetched."
7581   (interactive)
7582   (let ((ref (mail-header-references (gnus-summary-article-header)))
7583         (current (gnus-summary-article-number))
7584         (n 0))
7585     (if (or (not ref)
7586             (equal ref ""))
7587         (error "No References in the current article")
7588       ;; For each Message-ID in the References header...
7589       (while (string-match "<[^>]*>" ref)
7590         (incf n)
7591         ;; ... fetch that article.
7592         (gnus-summary-refer-article
7593          (prog1 (match-string 0 ref)
7594            (setq ref (substring ref (match-end 0))))))
7595       (gnus-summary-goto-subject current)
7596       (gnus-summary-position-point)
7597       n)))
7598
7599 (defun gnus-summary-refer-thread (&optional limit)
7600   "Fetch all articles in the current thread.
7601 If LIMIT (the numerical prefix), fetch that many old headers instead
7602 of what's specified by the `gnus-refer-thread-limit' variable."
7603   (interactive "P")
7604   (let ((id (mail-header-id (gnus-summary-article-header)))
7605         (limit (if limit (prefix-numeric-value limit)
7606                  gnus-refer-thread-limit)))
7607     ;; We want to fetch LIMIT *old* headers, but we also have to
7608     ;; re-fetch all the headers in the current buffer, because many of
7609     ;; them may be undisplayed.  So we adjust LIMIT.
7610     (when (numberp limit)
7611       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7612     (unless (eq gnus-fetch-old-headers 'invisible)
7613       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7614       ;; Retrieve the headers and read them in.
7615       (if (eq (gnus-retrieve-headers
7616                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7617               'nov)
7618           (gnus-build-all-threads)
7619         (error "Can't fetch thread from backends that don't support NOV"))
7620       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7621     (gnus-summary-limit-include-thread id)))
7622
7623 (defun gnus-summary-refer-article (message-id)
7624   "Fetch an article specified by MESSAGE-ID."
7625   (interactive "sMessage-ID: ")
7626   (when (and (stringp message-id)
7627              (not (zerop (length message-id))))
7628     ;; Construct the correct Message-ID if necessary.
7629     ;; Suggested by tale@pawl.rpi.edu.
7630     (unless (string-match "^<" message-id)
7631       (setq message-id (concat "<" message-id)))
7632     (unless (string-match ">$" message-id)
7633       (setq message-id (concat message-id ">")))
7634     (let* ((header (gnus-id-to-header message-id))
7635            (sparse (and header
7636                         (gnus-summary-article-sparse-p
7637                          (mail-header-number header))
7638                         (memq (mail-header-number header)
7639                               gnus-newsgroup-limit)))
7640            number)
7641       (cond
7642        ;; If the article is present in the buffer we just go to it.
7643        ((and header
7644              (or (not (gnus-summary-article-sparse-p
7645                        (mail-header-number header)))
7646                  sparse))
7647         (prog1
7648             (gnus-summary-goto-article
7649              (mail-header-number header) nil t)
7650           (when sparse
7651             (gnus-summary-update-article (mail-header-number header)))))
7652        (t
7653         ;; We fetch the article.
7654         (catch 'found
7655           (dolist (gnus-override-method (gnus-refer-article-methods))
7656             (gnus-check-server gnus-override-method)
7657             ;; Fetch the header, and display the article.
7658             (when (setq number (gnus-summary-insert-subject message-id))
7659               (gnus-summary-select-article nil nil nil number)
7660               (throw 'found t)))
7661           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7662
7663 (defun gnus-refer-article-methods ()
7664   "Return a list of referrable methods."
7665   (cond
7666    ;; No method, so we default to current and native.
7667    ((null gnus-refer-article-method)
7668     (list gnus-current-select-method gnus-select-method))
7669    ;; Current.
7670    ((eq 'current gnus-refer-article-method)
7671     (list gnus-current-select-method))
7672    ;; List of select methods.
7673    ((not (and (symbolp (car gnus-refer-article-method))
7674               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7675     (let (out)
7676       (dolist (method gnus-refer-article-method)
7677         (push (if (eq 'current method)
7678                   gnus-current-select-method
7679                 method)
7680               out))
7681       (nreverse out)))
7682    ;; One single select method.
7683    (t
7684     (list gnus-refer-article-method))))
7685
7686 (defun gnus-summary-edit-parameters ()
7687   "Edit the group parameters of the current group."
7688   (interactive)
7689   (gnus-group-edit-group gnus-newsgroup-name 'params))
7690
7691 (defun gnus-summary-customize-parameters ()
7692   "Customize the group parameters of the current group."
7693   (interactive)
7694   (gnus-group-customize gnus-newsgroup-name))
7695
7696 (defun gnus-summary-enter-digest-group (&optional force)
7697   "Enter an nndoc group based on the current article.
7698 If FORCE, force a digest interpretation.  If not, try
7699 to guess what the document format is."
7700   (interactive "P")
7701   (let ((conf gnus-current-window-configuration))
7702     (save-excursion
7703       (gnus-summary-select-article))
7704     (setq gnus-current-window-configuration conf)
7705     (let* ((name (format "%s-%d"
7706                          (gnus-group-prefixed-name
7707                           gnus-newsgroup-name (list 'nndoc ""))
7708                          (save-excursion
7709                            (set-buffer gnus-summary-buffer)
7710                            gnus-current-article)))
7711            (ogroup gnus-newsgroup-name)
7712            (params (append (gnus-info-params (gnus-get-info ogroup))
7713                            (list (cons 'to-group ogroup))
7714                            (list (cons 'save-article-group ogroup))))
7715            (case-fold-search t)
7716            (buf (current-buffer))
7717            dig to-address)
7718       (save-excursion
7719         (set-buffer gnus-original-article-buffer)
7720         ;; Have the digest group inherit the main mail address of
7721         ;; the parent article.
7722         (when (setq to-address (or (message-fetch-field "reply-to")
7723                                    (message-fetch-field "from")))
7724           (setq params (append
7725                         (list (cons 'to-address
7726                                     (funcall gnus-decode-encoded-word-function
7727                                              to-address))))))
7728         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7729         (insert-buffer-substring gnus-original-article-buffer)
7730         ;; Remove lines that may lead nndoc to misinterpret the
7731         ;; document type.
7732         (narrow-to-region
7733          (goto-char (point-min))
7734          (or (search-forward "\n\n" nil t) (point)))
7735         (goto-char (point-min))
7736         (delete-matching-lines "^Path:\\|^From ")
7737         (widen))
7738       (unwind-protect
7739           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7740                     (gnus-newsgroup-ephemeral-ignored-charsets
7741                      gnus-newsgroup-ignored-charsets))
7742                 (gnus-group-read-ephemeral-group
7743                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7744                               (nndoc-article-type
7745                                ,(if force 'mbox 'guess)))
7746                  t nil nil nil
7747                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7748                                                         "ADAPT")))))
7749               ;; Make all postings to this group go to the parent group.
7750               (nconc (gnus-info-params (gnus-get-info name))
7751                      params)
7752             ;; Couldn't select this doc group.
7753             (switch-to-buffer buf)
7754             (gnus-set-global-variables)
7755             (gnus-configure-windows 'summary)
7756             (gnus-message 3 "Article couldn't be entered?"))
7757         (kill-buffer dig)))))
7758
7759 (defun gnus-summary-read-document (n)
7760   "Open a new group based on the current article(s).
7761 This will allow you to read digests and other similar
7762 documents as newsgroups.
7763 Obeys the standard process/prefix convention."
7764   (interactive "P")
7765   (let* ((articles (gnus-summary-work-articles n))
7766          (ogroup gnus-newsgroup-name)
7767          (params (append (gnus-info-params (gnus-get-info ogroup))
7768                          (list (cons 'to-group ogroup))))
7769          article group egroup groups vgroup)
7770     (while (setq article (pop articles))
7771       (setq group (format "%s-%d" gnus-newsgroup-name article))
7772       (gnus-summary-remove-process-mark article)
7773       (when (gnus-summary-display-article article)
7774         (save-excursion
7775           (with-temp-buffer
7776             (insert-buffer-substring gnus-original-article-buffer)
7777             ;; Remove some headers that may lead nndoc to make
7778             ;; the wrong guess.
7779             (message-narrow-to-head)
7780             (goto-char (point-min))
7781             (delete-matching-lines "^\\(Path\\):\\|^From ")
7782             (widen)
7783             (if (setq egroup
7784                       (gnus-group-read-ephemeral-group
7785                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7786                                      (nndoc-article-type guess))
7787                        t nil t))
7788                 (progn
7789                   ;; Make all postings to this group go to the parent group.
7790                   (nconc (gnus-info-params (gnus-get-info egroup))
7791                          params)
7792                   (push egroup groups))
7793               ;; Couldn't select this doc group.
7794               (gnus-error 3 "Article couldn't be entered"))))))
7795     ;; Now we have selected all the documents.
7796     (cond
7797      ((not groups)
7798       (error "None of the articles could be interpreted as documents"))
7799      ((gnus-group-read-ephemeral-group
7800        (setq vgroup (format
7801                      "nnvirtual:%s-%s" gnus-newsgroup-name
7802                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7803        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7804        t
7805        (cons (current-buffer) 'summary)))
7806      (t
7807       (error "Couldn't select virtual nndoc group")))))
7808
7809 (defun gnus-summary-isearch-article (&optional regexp-p)
7810   "Do incremental search forward on the current article.
7811 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7812   (interactive "P")
7813   (let* ((gnus-inhibit-treatment t)
7814          (old (gnus-summary-select-article)))
7815     (gnus-configure-windows 'article)
7816     (gnus-eval-in-buffer-window gnus-article-buffer
7817       (save-restriction
7818         (widen)
7819         (when (eq 'old old)
7820           (gnus-article-show-all-headers))
7821         (goto-char (point-min))
7822         (isearch-forward regexp-p)))))
7823
7824 (defun gnus-summary-search-article-forward (regexp &optional backward)
7825   "Search for an article containing REGEXP forward.
7826 If BACKWARD, search backward instead."
7827   (interactive
7828    (list (read-string
7829           (format "Search article %s (regexp%s): "
7830                   (if current-prefix-arg "backward" "forward")
7831                   (if gnus-last-search-regexp
7832                       (concat ", default " gnus-last-search-regexp)
7833                     "")))
7834          current-prefix-arg))
7835   (if (string-equal regexp "")
7836       (setq regexp (or gnus-last-search-regexp ""))
7837     (setq gnus-last-search-regexp regexp)
7838     (setq gnus-article-before-search gnus-current-article))
7839   ;; Intentionally set gnus-last-article.
7840   (setq gnus-last-article gnus-article-before-search)
7841   (let ((gnus-last-article gnus-last-article))
7842     (if (gnus-summary-search-article regexp backward)
7843         (gnus-summary-show-thread)
7844       (error "Search failed: \"%s\"" regexp))))
7845
7846 (defun gnus-summary-search-article-backward (regexp)
7847   "Search for an article containing REGEXP backward."
7848   (interactive
7849    (list (read-string
7850           (format "Search article backward (regexp%s): "
7851                   (if gnus-last-search-regexp
7852                       (concat ", default " gnus-last-search-regexp)
7853                     "")))))
7854   (gnus-summary-search-article-forward regexp 'backward))
7855
7856 (eval-when-compile
7857   (defmacro gnus-summary-search-article-position-point (regexp backward)
7858     "Dehighlight the last matched text and goto the beginning position."
7859     (` (if (and gnus-summary-search-article-matched-data
7860                 (let ((text (caddr gnus-summary-search-article-matched-data))
7861                       (inhibit-read-only t)
7862                       buffer-read-only)
7863                   (delete-region
7864                    (goto-char (car gnus-summary-search-article-matched-data))
7865                    (cadr gnus-summary-search-article-matched-data))
7866                   (insert text)
7867                   (string-match (, regexp) text)))
7868            (if (, backward) (beginning-of-line) (end-of-line))
7869          (goto-char (if (, backward) (point-max) (point-min))))))
7870
7871   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7872     "Place point where X-Face image is displayed."
7873     (if (featurep 'xemacs)
7874         (` (let ((end (if (search-forward "\n\n" nil t)
7875                           (goto-char (1- (point)))
7876                         (point-min)))
7877                  extent)
7878              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7879              (unless (and (re-search-forward "^From:" end t)
7880                           (setq extent (extent-at (point)))
7881                           (extent-begin-glyph extent))
7882                (goto-char (, opoint)))))
7883       (` (let ((end (if (search-forward "\n\n" nil t)
7884                         (goto-char (1- (point)))
7885                       (point-min)))
7886                (start (or (search-backward "\n\n" nil t) (point-min))))
7887            (goto-char
7888             (or (text-property-any start end 'x-face-image t);; x-face-e21
7889                 (text-property-any start end 'x-face-mule-bitmap-image t)
7890                 (, opoint)))))))
7891
7892   (defmacro gnus-summary-search-article-highlight-matched-text
7893     (backward treated x-face)
7894     "Highlight matched text in the function `gnus-summary-search-article'."
7895     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7896              (end (set-marker (make-marker) (match-end 0)))
7897              (inhibit-read-only t)
7898              buffer-read-only)
7899          (unless treated
7900            (let ((,@
7901                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7902                     (mapcar
7903                      (lambda (item) (setq items (delq item items)))
7904                      '(gnus-treat-buttonize
7905                        gnus-treat-fill-article
7906                        gnus-treat-fill-long-lines
7907                        gnus-treat-emphasize
7908                        gnus-treat-highlight-headers
7909                        gnus-treat-highlight-citation
7910                        gnus-treat-highlight-signature
7911                        gnus-treat-overstrike
7912                        gnus-treat-display-xface
7913                        gnus-treat-buttonize-head
7914                        gnus-treat-decode-article-as-default-mime-charset))
7915                     (static-if (featurep 'xemacs)
7916                         items
7917                       (cons '(x-face-mule-delete-x-face-field
7918                               (quote never))
7919                             items))))
7920                  (gnus-treat-display-xface
7921                   (when (, x-face) gnus-treat-display-xface)))
7922              (gnus-article-prepare-mime-display)))
7923          (goto-char (if (, backward) start end))
7924          (when (, x-face)
7925            (gnus-summary-search-article-highlight-goto-x-face (point)))
7926          (setq gnus-summary-search-article-matched-data
7927                (list start end (buffer-substring start end)))
7928          (unless (eq start end);; matched text has been deleted. :-<
7929            (put-text-property start end 'face
7930                               (or (find-face 'isearch)
7931                                   'secondary-selection))))))
7932   )
7933
7934 (defun gnus-summary-search-article (regexp &optional backward)
7935   "Search for an article containing REGEXP.
7936 Optional argument BACKWARD means do search for backward.
7937 `gnus-select-article-hook' is not called during the search."
7938   ;; We have to require this here to make sure that the following
7939   ;; dynamic binding isn't shadowed by autoloading.
7940   (require 'gnus-async)
7941   (require 'gnus-art)
7942   (let ((gnus-select-article-hook nil)  ;Disable hook.
7943         (gnus-article-prepare-hook nil)
7944         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7945         (gnus-use-article-prefetch nil)
7946         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7947         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7948         (sum (current-buffer))
7949         (found nil)
7950         point treated)
7951     (gnus-save-hidden-threads
7952       (static-if (featurep 'xemacs)
7953           (let ((gnus-inhibit-treatment t))
7954             (setq treated (eq 'old (gnus-summary-select-article)))
7955             (when (and treated
7956                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7957                                  (window-live-p (get-buffer-window
7958                                                  gnus-article-buffer t)))))
7959               (gnus-summary-select-article nil t)
7960               (setq treated nil)))
7961         (let ((gnus-inhibit-treatment t)
7962               (x-face-mule-delete-x-face-field 'never))
7963           (setq treated (eq 'old (gnus-summary-select-article)))
7964           (when (and treated
7965                      (not
7966                       (and (gnus-buffer-live-p gnus-article-buffer)
7967                            (window-live-p (get-buffer-window
7968                                            gnus-article-buffer t))
7969                            (or (not (string-match "^\\^X-Face:" regexp))
7970                                (with-current-buffer gnus-article-buffer
7971                                  gnus-summary-search-article-matched-data)))))
7972             (gnus-summary-select-article nil t)
7973             (setq treated nil))))
7974       (set-buffer gnus-article-buffer)
7975       (widen)
7976       (if treated
7977           (progn
7978             (gnus-article-show-all-headers)
7979             (gnus-summary-search-article-position-point regexp backward))
7980         (goto-char (if backward (point-max) (point-min))))
7981       (while (not found)
7982         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7983         (if (if backward
7984                 (re-search-backward regexp nil t)
7985               (re-search-forward regexp nil t))
7986             ;; We found the regexp.
7987             (progn
7988               (gnus-summary-search-article-highlight-matched-text
7989                backward treated (string-match "^\\^X-Face:" regexp))
7990               (setq found 'found)
7991               (forward-line
7992                (/ (- 2 (window-height
7993                         (get-buffer-window gnus-article-buffer t)))
7994                   2))
7995               (set-window-start
7996                (get-buffer-window (current-buffer))
7997                (point))
7998               (set-buffer sum)
7999               (setq point (point)))
8000           ;; We didn't find it, so we go to the next article.
8001           (set-buffer sum)
8002           (setq found 'not)
8003           (while (eq found 'not)
8004             (if (not (if backward (gnus-summary-find-prev)
8005                        (gnus-summary-find-next)))
8006                 ;; No more articles.
8007                 (setq found t)
8008               ;; Select the next article and adjust point.
8009               (unless (gnus-summary-article-sparse-p
8010                        (gnus-summary-article-number))
8011                 (setq found nil)
8012                 (let ((gnus-inhibit-treatment t))
8013                   (gnus-summary-select-article))
8014                 (setq treated nil)
8015                 (set-buffer gnus-article-buffer)
8016                 (widen)
8017                 (goto-char (if backward (point-max) (point-min))))))))
8018       (gnus-message 7 ""))
8019     ;; Return whether we found the regexp.
8020     (when (eq found 'found)
8021       (goto-char point)
8022       (gnus-summary-show-thread)
8023       (gnus-summary-goto-subject gnus-current-article)
8024       (gnus-summary-position-point)
8025       t)))
8026
8027 (defun gnus-find-matching-articles (header regexp)
8028   "Return a list of all articles that match REGEXP on HEADER.
8029 This search includes all articles in the current group that Gnus has
8030 fetched headers for, whether they are displayed or not."
8031   (let ((articles nil)
8032         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8033         (case-fold-search t))
8034     (dolist (header gnus-newsgroup-headers)
8035       (when (string-match regexp (funcall func header))
8036         (push (mail-header-number header) articles)))
8037     (nreverse articles)))
8038
8039 (defun gnus-summary-find-matching (header regexp &optional backward unread
8040                                           not-case-fold not-matching)
8041   "Return a list of all articles that match REGEXP on HEADER.
8042 The search stars on the current article and goes forwards unless
8043 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8044 If UNREAD is non-nil, only unread articles will
8045 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8046 in the comparisons. If NOT-MATCHING, return a list of all articles that
8047 not match REGEXP on HEADER."
8048   (let ((case-fold-search (not not-case-fold))
8049         articles d func)
8050     (if (consp header)
8051         (if (eq (car header) 'extra)
8052             (setq func
8053                   `(lambda (h)
8054                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8055                          "")))
8056           (error "%s is an invalid header" header))
8057       (unless (fboundp (intern (concat "mail-header-" header)))
8058         (error "%s is not a valid header" header))
8059       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8060     (dolist (d (if (eq backward 'all)
8061                    gnus-newsgroup-data
8062                  (gnus-data-find-list
8063                   (gnus-summary-article-number)
8064                   (gnus-data-list backward))))
8065       (when (and (or (not unread)       ; We want all articles...
8066                      (gnus-data-unread-p d)) ; Or just unreads.
8067                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8068                  (if not-matching
8069                      (not (string-match
8070                            regexp
8071                            (funcall func (gnus-data-header d))))
8072                    (string-match regexp
8073                                  (funcall func (gnus-data-header d)))))
8074         (push (gnus-data-number d) articles))) ; Success!
8075     (nreverse articles)))
8076
8077 (defun gnus-summary-execute-command (header regexp command &optional backward)
8078   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8079 If HEADER is an empty string (or nil), the match is done on the entire
8080 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8081   (interactive
8082    (list (let ((completion-ignore-case t))
8083            (completing-read
8084             "Header name: "
8085             (mapcar (lambda (header) (list (format "%s" header)))
8086                     (append
8087                      '("Number" "Subject" "From" "Lines" "Date"
8088                        "Message-ID" "Xref" "References" "Body")
8089                      gnus-extra-headers))
8090             nil 'require-match))
8091          (read-string "Regexp: ")
8092          (read-key-sequence "Command: ")
8093          current-prefix-arg))
8094   (when (equal header "Body")
8095     (setq header ""))
8096   ;; Hidden thread subtrees must be searched as well.
8097   (gnus-summary-show-all-threads)
8098   ;; We don't want to change current point nor window configuration.
8099   (save-excursion
8100     (save-window-excursion
8101       (gnus-message 6 "Executing %s..." (key-description command))
8102       ;; We'd like to execute COMMAND interactively so as to give arguments.
8103       (gnus-execute header regexp
8104                     `(call-interactively ',(key-binding command))
8105                     backward)
8106       (gnus-message 6 "Executing %s...done" (key-description command)))))
8107
8108 (defun gnus-summary-beginning-of-article ()
8109   "Scroll the article back to the beginning."
8110   (interactive)
8111   (gnus-summary-select-article)
8112   (gnus-configure-windows 'article)
8113   (gnus-eval-in-buffer-window gnus-article-buffer
8114     (widen)
8115     (goto-char (point-min))
8116     (when gnus-page-broken
8117       (gnus-narrow-to-page))))
8118
8119 (defun gnus-summary-end-of-article ()
8120   "Scroll to the end of the article."
8121   (interactive)
8122   (gnus-summary-select-article)
8123   (gnus-configure-windows 'article)
8124   (gnus-eval-in-buffer-window gnus-article-buffer
8125     (widen)
8126     (goto-char (point-max))
8127     (recenter -3)
8128     (when gnus-page-broken
8129       (gnus-narrow-to-page))))
8130
8131 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8132   "Truncate to LEN and quote all \"(\"'s in STRING."
8133   (gnus-replace-in-string (if (and len (> (length string) len))
8134                               (substring string 0 len)
8135                             string)
8136                           "[()]" "\\\\\\&"))
8137
8138 (defun gnus-summary-print-article (&optional filename n)
8139   "Generate and print a PostScript image of the N next (mail) articles.
8140
8141 If N is negative, print the N previous articles.  If N is nil and articles
8142 have been marked with the process mark, print these instead.
8143
8144 If the optional first argument FILENAME is nil, send the image to the
8145 printer.  If FILENAME is a string, save the PostScript image in a file with
8146 that name.  If FILENAME is a number, prompt the user for the name of the file
8147 to save in."
8148   (interactive (list (ps-print-preprint current-prefix-arg)))
8149   (dolist (article (gnus-summary-work-articles n))
8150     (gnus-summary-select-article nil nil 'pseudo article)
8151     (gnus-eval-in-buffer-window gnus-article-buffer
8152       (gnus-print-buffer))
8153     (gnus-summary-remove-process-mark article))
8154   (ps-despool filename))
8155
8156 (defun gnus-print-buffer ()
8157   (let ((buffer (generate-new-buffer " *print*")))
8158     (unwind-protect
8159         (progn
8160           (copy-to-buffer buffer (point-min) (point-max))
8161           (set-buffer buffer)
8162           (gnus-article-delete-invisible-text)
8163           (when (gnus-visual-p 'article-highlight 'highlight)
8164             ;; Copy-to-buffer doesn't copy overlay.  So redo
8165             ;; highlight.
8166             (let ((gnus-article-buffer buffer))
8167               (gnus-article-highlight-citation t)
8168               (gnus-article-highlight-signature)))
8169           (let ((ps-left-header
8170                  (list
8171                   (concat "("
8172                           (gnus-summary-print-truncate-and-quote
8173                            (mail-header-subject gnus-current-headers)
8174                            66) ")")
8175                   (concat "("
8176                           (gnus-summary-print-truncate-and-quote
8177                            (mail-header-from gnus-current-headers)
8178                            45) ")")))
8179                 (ps-right-header
8180                  (list
8181                   "/pagenumberstring load"
8182                   (concat "("
8183                           (mail-header-date gnus-current-headers) ")"))))
8184             (gnus-run-hooks 'gnus-ps-print-hook)
8185             (save-excursion
8186               (if window-system
8187                   (ps-spool-buffer-with-faces)
8188                 (ps-spool-buffer)))))
8189       (kill-buffer buffer))))
8190
8191 (defun gnus-summary-show-article (&optional arg)
8192   "Force redisplaying of the current article.
8193 If ARG (the prefix) is a number, show the article with the charset
8194 defined in `gnus-summary-show-article-charset-alist', or the charset
8195 input.
8196 If ARG (the prefix) is non-nil and not a number, show the raw article
8197 without any article massaging functions being run.  Normally, the key strokes
8198 are `C-u g'."
8199   (interactive "P")
8200   (cond
8201    ((numberp arg)
8202     (gnus-summary-show-article t)
8203     (let* ((gnus-newsgroup-charset
8204             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8205                 (mm-read-coding-system
8206                  "View as charset: " ;; actually it is coding system.
8207                  (save-excursion
8208                    (set-buffer gnus-article-buffer)
8209                    (mm-detect-coding-region (point) (point-max))))))
8210            (default-mime-charset gnus-newsgroup-charset)
8211            (gnus-newsgroup-ignored-charsets 'gnus-all))
8212       (gnus-summary-select-article nil 'force)
8213       (let ((deps gnus-newsgroup-dependencies)
8214             head header lines)
8215         (save-excursion
8216           (set-buffer gnus-original-article-buffer)
8217           (save-restriction
8218             (message-narrow-to-head)
8219             (setq head (buffer-string))
8220             (goto-char (point-min))
8221             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8222               (goto-char (point-max))
8223               (widen)
8224               (setq lines (1- (count-lines (point) (point-max))))))
8225           (with-temp-buffer
8226             (insert (format "211 %d Article retrieved.\n"
8227                             (cdr gnus-article-current)))
8228             (insert head)
8229             (if lines (insert (format "Lines: %d\n" lines)))
8230             (insert ".\n")
8231             (let ((nntp-server-buffer (current-buffer)))
8232               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8233         (gnus-data-set-header
8234          (gnus-data-find (cdr gnus-article-current))
8235          header)
8236         (gnus-summary-update-article-line
8237          (cdr gnus-article-current) header)
8238         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8239           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8240    ((not arg)
8241     ;; Select the article the normal way.
8242     (gnus-summary-select-article nil 'force))
8243    (t
8244     ;; We have to require this here to make sure that the following
8245     ;; dynamic binding isn't shadowed by autoloading.
8246     (require 'gnus-async)
8247     (require 'gnus-art)
8248     ;; Bind the article treatment functions to nil.
8249     (let ((gnus-have-all-headers t)
8250           gnus-article-prepare-hook
8251           gnus-article-decode-hook
8252           gnus-break-pages
8253           gnus-show-mime
8254           (gnus-inhibit-treatment t))
8255       (gnus-summary-select-article nil 'force))))
8256   (gnus-summary-goto-subject gnus-current-article)
8257   (gnus-summary-position-point))
8258
8259 (defun gnus-summary-show-raw-article ()
8260   "Show the raw article without any article massaging functions being run."
8261   (interactive)
8262   (gnus-summary-show-article t))
8263
8264 (defun gnus-summary-verbose-headers (&optional arg)
8265   "Toggle permanent full header display.
8266 If ARG is a positive number, turn header display on.
8267 If ARG is a negative number, turn header display off."
8268   (interactive "P")
8269   (setq gnus-show-all-headers
8270         (cond ((or (not (numberp arg))
8271                    (zerop arg))
8272                (not gnus-show-all-headers))
8273               ((natnump arg)
8274                t)))
8275   (gnus-summary-show-article))
8276
8277 (defun gnus-summary-toggle-header (&optional arg)
8278   "Show the headers if they are hidden, or hide them if they are shown.
8279 If ARG is a positive number, show the entire header.
8280 If ARG is a negative number, hide the unwanted header lines."
8281   (interactive "P")
8282   (save-excursion
8283     (set-buffer gnus-article-buffer)
8284     (save-restriction
8285       (let* ((buffer-read-only nil)
8286              (inhibit-point-motion-hooks t)
8287              hidden e)
8288         (setq hidden
8289               (if (numberp arg)
8290                   (>= arg 0)
8291                 (save-restriction
8292                   (article-narrow-to-head)
8293                   (gnus-article-hidden-text-p 'headers))))
8294         (goto-char (point-min))
8295         (when (search-forward "\n\n" nil t)
8296           (delete-region (point-min) (1- (point))))
8297         (goto-char (point-min))
8298         (save-excursion
8299           (set-buffer gnus-original-article-buffer)
8300           (goto-char (point-min))
8301           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8302         (insert-buffer-substring gnus-original-article-buffer 1 e)
8303         (save-restriction
8304           (narrow-to-region (point-min) (point))
8305           (article-decode-encoded-words)
8306           (if  hidden
8307               (let ((gnus-treat-hide-headers nil)
8308                     (gnus-treat-hide-boring-headers nil))
8309                 (gnus-delete-wash-type 'headers)
8310                 (gnus-treat-article 'head))
8311             (gnus-treat-article 'head)))
8312         (gnus-set-mode-line 'article)))))
8313
8314 (defun gnus-summary-show-all-headers ()
8315   "Make all header lines visible."
8316   (interactive)
8317   (gnus-summary-toggle-header 1))
8318
8319 (defun gnus-summary-toggle-mime (&optional arg)
8320   "Toggle MIME processing.
8321 If ARG is a positive number, turn MIME processing on."
8322   (interactive "P")
8323   (setq gnus-show-mime
8324         (if (null arg)
8325             (not gnus-show-mime)
8326           (> (prefix-numeric-value arg) 0)))
8327   (gnus-summary-select-article t 'force))
8328
8329 (defun gnus-summary-caesar-message (&optional arg)
8330   "Caesar rotate the current article by 13.
8331 The numerical prefix specifies how many places to rotate each letter
8332 forward."
8333   (interactive "P")
8334   (gnus-summary-select-article)
8335   (let ((mail-header-separator ""))
8336     (gnus-eval-in-buffer-window gnus-article-buffer
8337       (save-restriction
8338         (widen)
8339         (let ((start (window-start))
8340               buffer-read-only)
8341           (message-caesar-buffer-body arg)
8342           (set-window-start (get-buffer-window (current-buffer)) start))))))
8343
8344 (defun gnus-summary-stop-page-breaking ()
8345   "Stop page breaking in the current article."
8346   (interactive)
8347   (gnus-summary-select-article)
8348   (gnus-eval-in-buffer-window gnus-article-buffer
8349     (widen)
8350     (when (gnus-visual-p 'page-marker)
8351       (let ((buffer-read-only nil))
8352         (gnus-remove-text-with-property 'gnus-prev)
8353         (gnus-remove-text-with-property 'gnus-next))
8354       (setq gnus-page-broken nil))))
8355
8356 (defun gnus-summary-move-article (&optional n to-newsgroup
8357                                             select-method action)
8358   "Move the current article to a different newsgroup.
8359 If N is a positive number, move the N next articles.
8360 If N is a negative number, move the N previous articles.
8361 If N is nil and any articles have been marked with the process mark,
8362 move those articles instead.
8363 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8364 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8365 re-spool using this method.
8366
8367 For this function to work, both the current newsgroup and the
8368 newsgroup that you want to move to have to support the `request-move'
8369 and `request-accept' functions.
8370
8371 ACTION can be either `move' (the default), `crosspost' or `copy'."
8372   (interactive "P")
8373   (unless action
8374     (setq action 'move))
8375   ;; Check whether the source group supports the required functions.
8376   (cond ((and (eq action 'move)
8377               (not (gnus-check-backend-function
8378                     'request-move-article gnus-newsgroup-name)))
8379          (error "The current group does not support article moving"))
8380         ((and (eq action 'crosspost)
8381               (not (gnus-check-backend-function
8382                     'request-replace-article gnus-newsgroup-name)))
8383          (error "The current group does not support article editing")))
8384   (let ((articles (gnus-summary-work-articles n))
8385         (prefix (if (gnus-check-backend-function
8386                      'request-move-article gnus-newsgroup-name)
8387                     (gnus-group-real-prefix gnus-newsgroup-name)
8388                   ""))
8389         (names '((move "Move" "Moving")
8390                  (copy "Copy" "Copying")
8391                  (crosspost "Crosspost" "Crossposting")))
8392         (copy-buf (save-excursion
8393                     (nnheader-set-temp-buffer " *copy article*")))
8394         (default-marks gnus-article-mark-lists)
8395         (no-expire-marks (delete '(expirable . expire)
8396                                  (copy-sequence gnus-article-mark-lists)))
8397         art-group to-method new-xref article to-groups)
8398     (unless (assq action names)
8399       (error "Unknown action %s" action))
8400     ;; We have to select an article to give
8401     ;; `gnus-read-move-group-name' an opportunity to suggest an
8402     ;; appropriate default.
8403     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8404       (let ((gnus-display-mime-function nil)
8405             (gnus-article-prepare-hook nil))
8406         (gnus-summary-select-article nil nil nil (car articles))))
8407     ;; Read the newsgroup name.
8408     (when (and (not to-newsgroup)
8409                (not select-method))
8410       (setq to-newsgroup
8411             (gnus-read-move-group-name
8412              (cadr (assq action names))
8413              (symbol-value (intern (format "gnus-current-%s-group" action)))
8414              articles prefix))
8415       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8416     (setq to-method (or select-method
8417                         (gnus-server-to-method
8418                          (gnus-group-method to-newsgroup))))
8419     ;; Check the method we are to move this article to...
8420     (unless (gnus-check-backend-function
8421              'request-accept-article (car to-method))
8422       (error "%s does not support article copying" (car to-method)))
8423     (unless (gnus-check-server to-method)
8424       (error "Can't open server %s" (car to-method)))
8425     (gnus-message 6 "%s to %s: %s..."
8426                   (caddr (assq action names))
8427                   (or (car select-method) to-newsgroup) articles)
8428     (while articles
8429       (setq article (pop articles))
8430       (setq
8431        art-group
8432        (cond
8433         ;; Move the article.
8434         ((eq action 'move)
8435          ;; Remove this article from future suppression.
8436          (gnus-dup-unsuppress-article article)
8437          (gnus-request-move-article
8438           article                       ; Article to move
8439           gnus-newsgroup-name           ; From newsgroup
8440           (nth 1 (gnus-find-method-for-group
8441                   gnus-newsgroup-name)) ; Server
8442           (list 'gnus-request-accept-article
8443                 to-newsgroup (list 'quote select-method)
8444                 (not articles) t)       ; Accept form
8445           (not articles)))              ; Only save nov last time
8446         ;; Copy the article.
8447         ((eq action 'copy)
8448          (save-excursion
8449            (set-buffer copy-buf)
8450            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8451              (gnus-request-accept-article
8452               to-newsgroup select-method (not articles) t))))
8453         ;; Crosspost the article.
8454         ((eq action 'crosspost)
8455          (let ((xref (message-tokenize-header
8456                       (mail-header-xref (gnus-summary-article-header article))
8457                       " ")))
8458            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8459                                   ":" (number-to-string article)))
8460            (unless xref
8461              (setq xref (list (system-name))))
8462            (setq new-xref
8463                  (concat
8464                   (mapconcat 'identity
8465                              (delete "Xref:" (delete new-xref xref))
8466                              " ")
8467                   " " new-xref))
8468            (save-excursion
8469              (set-buffer copy-buf)
8470              ;; First put the article in the destination group.
8471              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8472              (when (consp (setq art-group
8473                                 (gnus-request-accept-article
8474                                  to-newsgroup select-method (not articles))))
8475                (setq new-xref (concat new-xref " " (car art-group)
8476                                       ":"
8477                                       (number-to-string (cdr art-group))))
8478                ;; Now we have the new Xrefs header, so we insert
8479                ;; it and replace the new article.
8480                (nnheader-replace-header "Xref" new-xref)
8481                (gnus-request-replace-article
8482                 (cdr art-group) to-newsgroup (current-buffer))
8483                art-group))))))
8484       (cond
8485        ((not art-group)
8486         (gnus-message 1 "Couldn't %s article %s: %s"
8487                       (cadr (assq action names)) article
8488                       (nnheader-get-report (car to-method))))
8489        ((eq art-group 'junk)
8490         (when (eq action 'move)
8491           (gnus-summary-mark-article article gnus-canceled-mark)
8492           (gnus-message 4 "Deleted article %s" article)))
8493        (t
8494         (let* ((pto-group (gnus-group-prefixed-name
8495                            (car art-group) to-method))
8496                (entry
8497                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8498                (info (nth 2 entry))
8499                (to-group (gnus-info-group info))
8500                to-marks)
8501           ;; Update the group that has been moved to.
8502           (when (and info
8503                      (memq action '(move copy)))
8504             (unless (member to-group to-groups)
8505               (push to-group to-groups))
8506
8507             (unless (memq article gnus-newsgroup-unreads)
8508               (push 'read to-marks)
8509               (gnus-info-set-read
8510                info (gnus-add-to-range (gnus-info-read info)
8511                                        (list (cdr art-group)))))
8512
8513             ;; See whether the article is to be put in the cache.
8514             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8515                              default-marks
8516                            no-expire-marks))
8517                   (to-article (cdr art-group)))
8518
8519               ;; Enter the article into the cache in the new group,
8520               ;; if that is required.
8521               (when gnus-use-cache
8522                 (gnus-cache-possibly-enter-article
8523                  to-group to-article
8524                  (let ((header (copy-sequence
8525                                 (gnus-summary-article-header article))))
8526                    (mail-header-set-number header to-article)
8527                    header)
8528                  (memq article gnus-newsgroup-marked)
8529                  (memq article gnus-newsgroup-dormant)
8530                  (memq article gnus-newsgroup-unreads)))
8531
8532               (when gnus-preserve-marks
8533                 ;; Copy any marks over to the new group.
8534                 (when (and (equal to-group gnus-newsgroup-name)
8535                            (not (memq article gnus-newsgroup-unreads)))
8536                   ;; Mark this article as read in this group.
8537                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8538                   (setcdr (gnus-active to-group) to-article)
8539                   (setcdr gnus-newsgroup-active to-article))
8540
8541                 (while marks
8542                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8543                     (when (memq article (symbol-value
8544                                          (intern (format "gnus-newsgroup-%s"
8545                                                          (caar marks)))))
8546                       (push (cdar marks) to-marks)
8547                       ;; If the other group is the same as this group,
8548                       ;; then we have to add the mark to the list.
8549                       (when (equal to-group gnus-newsgroup-name)
8550                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8551                              (cons to-article
8552                                    (symbol-value
8553                                     (intern (format "gnus-newsgroup-%s"
8554                                                     (caar marks)))))))
8555                       ;; Copy the marks to other group.
8556                       (gnus-add-marked-articles
8557                        to-group (cdar marks) (list to-article) info)))
8558                   (setq marks (cdr marks)))
8559
8560                 (gnus-request-set-mark to-group (list (list (list to-article)
8561                                                             'add
8562                                                             to-marks))))
8563
8564               (gnus-dribble-enter
8565                (concat "(gnus-group-set-info '"
8566                        (gnus-prin1-to-string (gnus-get-info to-group))
8567                        ")"))))
8568
8569           ;; Update the Xref header in this article to point to
8570           ;; the new crossposted article we have just created.
8571           (when (eq action 'crosspost)
8572             (save-excursion
8573               (set-buffer copy-buf)
8574               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8575               (nnheader-replace-header "Xref" new-xref)
8576               (gnus-request-replace-article
8577                article gnus-newsgroup-name (current-buffer)))))
8578
8579         ;;;!!!Why is this necessary?
8580         (set-buffer gnus-summary-buffer)
8581
8582         (gnus-summary-goto-subject article)
8583         (when (eq action 'move)
8584           (gnus-summary-mark-article article gnus-canceled-mark))))
8585       (gnus-summary-remove-process-mark article))
8586     ;; Re-activate all groups that have been moved to.
8587     (save-excursion
8588       (set-buffer gnus-group-buffer)
8589       (let ((gnus-group-marked to-groups))
8590         (gnus-group-get-new-news-this-group nil t)))
8591
8592     (gnus-kill-buffer copy-buf)
8593     (gnus-summary-position-point)
8594     (gnus-set-mode-line 'summary)))
8595
8596 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8597   "Move the current article to a different newsgroup.
8598 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8599 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8600 re-spool using this method."
8601   (interactive "P")
8602   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8603
8604 (defun gnus-summary-crosspost-article (&optional n)
8605   "Crosspost the current article to some other group."
8606   (interactive "P")
8607   (gnus-summary-move-article n nil nil 'crosspost))
8608
8609 (defcustom gnus-summary-respool-default-method nil
8610   "Default method type for respooling an article.
8611 If nil, use to the current newsgroup method."
8612   :type 'symbol
8613   :group 'gnus-summary-mail)
8614
8615 (defun gnus-summary-respool-article (&optional n method)
8616   "Respool the current article.
8617 The article will be squeezed through the mail spooling process again,
8618 which means that it will be put in some mail newsgroup or other
8619 depending on `nnmail-split-methods'.
8620 If N is a positive number, respool the N next articles.
8621 If N is a negative number, respool the N previous articles.
8622 If N is nil and any articles have been marked with the process mark,
8623 respool those articles instead.
8624
8625 Respooling can be done both from mail groups and \"real\" newsgroups.
8626 In the former case, the articles in question will be moved from the
8627 current group into whatever groups they are destined to.  In the
8628 latter case, they will be copied into the relevant groups."
8629   (interactive
8630    (list current-prefix-arg
8631          (let* ((methods (gnus-methods-using 'respool))
8632                 (methname
8633                  (symbol-name (or gnus-summary-respool-default-method
8634                                   (car (gnus-find-method-for-group
8635                                         gnus-newsgroup-name)))))
8636                 (method
8637                  (gnus-completing-read-with-default
8638                   methname "What backend do you want to use when respooling?"
8639                   methods nil t nil 'gnus-mail-method-history))
8640                 ms)
8641            (cond
8642             ((zerop (length (setq ms (gnus-servers-using-backend
8643                                       (intern method)))))
8644              (list (intern method) ""))
8645             ((= 1 (length ms))
8646              (car ms))
8647             (t
8648              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8649                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8650                            ms-alist))))))))
8651   (unless method
8652     (error "No method given for respooling"))
8653   (if (assoc (symbol-name
8654               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8655              (gnus-methods-using 'respool))
8656       (gnus-summary-move-article n nil method)
8657     (gnus-summary-copy-article n nil method)))
8658
8659 (defun gnus-summary-import-article (file &optional edit)
8660   "Import an arbitrary file into a mail newsgroup."
8661   (interactive "fImport file: \nP")
8662   (let ((group gnus-newsgroup-name)
8663         (now (current-time))
8664         atts lines group-art)
8665     (unless (gnus-check-backend-function 'request-accept-article group)
8666       (error "%s does not support article importing" group))
8667     (or (file-readable-p file)
8668         (not (file-regular-p file))
8669         (error "Can't read %s" file))
8670     (save-excursion
8671       (set-buffer (gnus-get-buffer-create " *import file*"))
8672       (erase-buffer)
8673       (nnheader-insert-file-contents file)
8674       (goto-char (point-min))
8675       (if (nnheader-article-p)
8676           (save-restriction
8677             (goto-char (point-min))
8678             (search-forward "\n\n" nil t)
8679             (narrow-to-region (point-min) (1- (point)))
8680             (goto-char (point-min))
8681             (unless (re-search-forward "^date:" nil t)
8682               (goto-char (point-max))
8683               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8684         ;; This doesn't look like an article, so we fudge some headers.
8685         (setq atts (file-attributes file)
8686               lines (count-lines (point-min) (point-max)))
8687         (insert "From: " (read-string "From: ") "\n"
8688                 "Subject: " (read-string "Subject: ") "\n"
8689                 "Date: " (message-make-date (nth 5 atts)) "\n"
8690                 "Message-ID: " (message-make-message-id) "\n"
8691                 "Lines: " (int-to-string lines) "\n"
8692                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8693       (setq group-art (gnus-request-accept-article group nil t))
8694       (kill-buffer (current-buffer)))
8695     (setq gnus-newsgroup-active (gnus-activate-group group))
8696     (forward-line 1)
8697     (gnus-summary-goto-article (cdr group-art) nil t)
8698     (when edit
8699       (gnus-summary-edit-article))))
8700
8701 (defun gnus-summary-create-article ()
8702   "Create an article in a mail newsgroup."
8703   (interactive)
8704   (let ((group gnus-newsgroup-name)
8705         (now (current-time))
8706         group-art)
8707     (unless (gnus-check-backend-function 'request-accept-article group)
8708       (error "%s does not support article importing" group))
8709     (save-excursion
8710       (set-buffer (gnus-get-buffer-create " *import file*"))
8711       (erase-buffer)
8712       (goto-char (point-min))
8713       ;; This doesn't look like an article, so we fudge some headers.
8714       (insert "From: " (read-string "From: ") "\n"
8715               "Subject: " (read-string "Subject: ") "\n"
8716               "Date: " (message-make-date now) "\n"
8717               "Message-ID: " (message-make-message-id) "\n")
8718       (setq group-art (gnus-request-accept-article group nil t))
8719       (kill-buffer (current-buffer)))
8720     (setq gnus-newsgroup-active (gnus-activate-group group))
8721     (forward-line 1)
8722     (gnus-summary-goto-article (cdr group-art) nil t)
8723     (gnus-summary-edit-article)))
8724
8725 (defun gnus-summary-article-posted-p ()
8726   "Say whether the current (mail) article is available from news as well.
8727 This will be the case if the article has both been mailed and posted."
8728   (interactive)
8729   (let ((id (mail-header-references (gnus-summary-article-header)))
8730         (gnus-override-method (car (gnus-refer-article-methods))))
8731     (if (gnus-request-head id "")
8732         (gnus-message 2 "The current message was found on %s"
8733                       gnus-override-method)
8734       (gnus-message 2 "The current message couldn't be found on %s"
8735                     gnus-override-method)
8736       nil)))
8737
8738 (defun gnus-summary-expire-articles (&optional now)
8739   "Expire all articles that are marked as expirable in the current group."
8740   (interactive)
8741   (when (gnus-check-backend-function
8742          'request-expire-articles gnus-newsgroup-name)
8743     ;; This backend supports expiry.
8744     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8745            (expirable (if total
8746                           (progn
8747                             ;; We need to update the info for
8748                             ;; this group for `gnus-list-of-read-articles'
8749                             ;; to give us the right answer.
8750                             (gnus-run-hooks 'gnus-exit-group-hook)
8751                             (gnus-summary-update-info)
8752                             (gnus-list-of-read-articles gnus-newsgroup-name))
8753                         (setq gnus-newsgroup-expirable
8754                               (sort gnus-newsgroup-expirable '<))))
8755            (expiry-wait (if now 'immediate
8756                           (gnus-group-find-parameter
8757                            gnus-newsgroup-name 'expiry-wait)))
8758            (nnmail-expiry-target
8759             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8760                 nnmail-expiry-target))
8761            es)
8762       (when expirable
8763         ;; There are expirable articles in this group, so we run them
8764         ;; through the expiry process.
8765         (gnus-message 6 "Expiring articles...")
8766         (unless (gnus-check-group gnus-newsgroup-name)
8767           (error "Can't open server for %s" gnus-newsgroup-name))
8768         ;; The list of articles that weren't expired is returned.
8769         (save-excursion
8770           (if expiry-wait
8771               (let ((nnmail-expiry-wait-function nil)
8772                     (nnmail-expiry-wait expiry-wait))
8773                 (setq es (gnus-request-expire-articles
8774                           expirable gnus-newsgroup-name)))
8775             (setq es (gnus-request-expire-articles
8776                       expirable gnus-newsgroup-name)))
8777           (unless total
8778             (setq gnus-newsgroup-expirable es))
8779           ;; We go through the old list of expirable, and mark all
8780           ;; really expired articles as nonexistent.
8781           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8782             (let ((gnus-use-cache nil))
8783               (while expirable
8784                 (unless (memq (car expirable) es)
8785                   (when (gnus-data-find (car expirable))
8786                     (gnus-summary-mark-article
8787                      (car expirable) gnus-canceled-mark)))
8788                 (setq expirable (cdr expirable))))))
8789         (gnus-message 6 "Expiring articles...done")))))
8790
8791 (defun gnus-summary-expire-articles-now ()
8792   "Expunge all expirable articles in the current group.
8793 This means that *all* articles that are marked as expirable will be
8794 deleted forever, right now."
8795   (interactive)
8796   (or gnus-expert-user
8797       (gnus-yes-or-no-p
8798        "Are you really, really, really sure you want to delete all these messages? ")
8799       (error "Phew!"))
8800   (gnus-summary-expire-articles t))
8801
8802 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8803 (defun gnus-summary-delete-article (&optional n)
8804   "Delete the N next (mail) articles.
8805 This command actually deletes articles.  This is not a marking
8806 command.  The article will disappear forever from your life, never to
8807 return.
8808 If N is negative, delete backwards.
8809 If N is nil and articles have been marked with the process mark,
8810 delete these instead."
8811   (interactive "P")
8812   (unless (gnus-check-backend-function 'request-expire-articles
8813                                        gnus-newsgroup-name)
8814     (error "The current newsgroup does not support article deletion"))
8815   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8816     (error "Couldn't open server"))
8817   ;; Compute the list of articles to delete.
8818   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8819         not-deleted)
8820     (if (and gnus-novice-user
8821              (not (gnus-yes-or-no-p
8822                    (format "Do you really want to delete %s forever? "
8823                            (if (> (length articles) 1)
8824                                (format "these %s articles" (length articles))
8825                              "this article")))))
8826         ()
8827       ;; Delete the articles.
8828       (setq not-deleted (gnus-request-expire-articles
8829                          articles gnus-newsgroup-name 'force))
8830       (while articles
8831         (gnus-summary-remove-process-mark (car articles))
8832         ;; The backend might not have been able to delete the article
8833         ;; after all.
8834         (unless (memq (car articles) not-deleted)
8835           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8836         (setq articles (cdr articles)))
8837       (when not-deleted
8838         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8839     (gnus-summary-position-point)
8840     (gnus-set-mode-line 'summary)
8841     not-deleted))
8842
8843 (defun gnus-summary-edit-article (&optional force)
8844   "Edit the current article.
8845 This will have permanent effect only in mail groups.
8846 If FORCE is non-nil, allow editing of articles even in read-only
8847 groups."
8848   (interactive "P")
8849   (save-excursion
8850     (set-buffer gnus-summary-buffer)
8851     (let ((mail-parse-charset gnus-newsgroup-charset)
8852           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8853       (gnus-set-global-variables)
8854       (when (and (not force)
8855                  (gnus-group-read-only-p))
8856         (error "The current newsgroup does not support article editing"))
8857       (gnus-summary-show-article t)
8858       (gnus-article-edit-article
8859        'ignore
8860        `(lambda (no-highlight)
8861           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8862                 (message-options message-options)
8863                 (message-options-set-recipient)
8864                 (mail-parse-ignored-charsets
8865                  ',gnus-newsgroup-ignored-charsets))
8866             (gnus-summary-edit-article-done
8867              ,(or (mail-header-references gnus-current-headers) "")
8868              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8869
8870 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8871
8872 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8873                                                  no-highlight)
8874   "Make edits to the current article permanent."
8875   (interactive)
8876   (save-excursion
8877     ;; The buffer restriction contains the entire article if it exists.
8878     (when (article-goto-body)
8879       (let ((lines (count-lines (point) (point-max)))
8880             (length (- (point-max) (point)))
8881             (case-fold-search t)
8882             (body (copy-marker (point))))
8883         (goto-char (point-min))
8884         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8885           (delete-region (match-beginning 1) (match-end 1))
8886           (insert (number-to-string length)))
8887         (goto-char (point-min))
8888         (when (re-search-forward
8889                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8890           (delete-region (match-beginning 1) (match-end 1))
8891           (insert (number-to-string length)))
8892         (goto-char (point-min))
8893         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8894           (delete-region (match-beginning 1) (match-end 1))
8895           (insert (number-to-string lines))))))
8896   ;; Replace the article.
8897   (let ((buf (current-buffer)))
8898     (with-temp-buffer
8899       (insert-buffer-substring buf)
8900
8901       (if (and (not read-only)
8902                (not (gnus-request-replace-article
8903                      (cdr gnus-article-current) (car gnus-article-current)
8904                      (current-buffer) t)))
8905           (error "Couldn't replace article")
8906         ;; Update the summary buffer.
8907         (if (and references
8908                  (equal (message-tokenize-header references " ")
8909                         (message-tokenize-header
8910                          (or (message-fetch-field "references") "") " ")))
8911             ;; We only have to update this line.
8912             (save-excursion
8913               (save-restriction
8914                 (message-narrow-to-head)
8915                 (let ((head (buffer-string))
8916                       header)
8917                   (with-temp-buffer
8918                     (insert (format "211 %d Article retrieved.\n"
8919                                     (cdr gnus-article-current)))
8920                     (insert head)
8921                     (insert ".\n")
8922                     (let ((nntp-server-buffer (current-buffer)))
8923                       (setq header (car (gnus-get-newsgroup-headers
8924                                          (save-excursion
8925                                            (set-buffer gnus-summary-buffer)
8926                                            gnus-newsgroup-dependencies)
8927                                          t))))
8928                     (save-excursion
8929                       (set-buffer gnus-summary-buffer)
8930                       (gnus-data-set-header
8931                        (gnus-data-find (cdr gnus-article-current))
8932                        header)
8933                       (gnus-summary-update-article-line
8934                        (cdr gnus-article-current) header)
8935                       (if (gnus-summary-goto-subject
8936                            (cdr gnus-article-current) nil t)
8937                           (gnus-summary-update-secondary-mark
8938                            (cdr gnus-article-current))))))))
8939           ;; Update threads.
8940           (set-buffer (or buffer gnus-summary-buffer))
8941           (gnus-summary-update-article (cdr gnus-article-current))
8942           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8943               (gnus-summary-update-secondary-mark
8944                (cdr gnus-article-current))))
8945         ;; Prettify the article buffer again.
8946         (unless no-highlight
8947           (save-excursion
8948             (set-buffer gnus-article-buffer)
8949             ;;;!!! Fix this -- article should be rehighlighted.
8950             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8951             (set-buffer gnus-original-article-buffer)
8952             (gnus-request-article
8953              (cdr gnus-article-current)
8954              (car gnus-article-current) (current-buffer))))
8955         ;; Prettify the summary buffer line.
8956         (when (gnus-visual-p 'summary-highlight 'highlight)
8957           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8958
8959 (defun gnus-summary-edit-wash (key)
8960   "Perform editing command KEY in the article buffer."
8961   (interactive
8962    (list
8963     (progn
8964       (message "%s" (concat (this-command-keys) "- "))
8965       (read-char))))
8966   (message "")
8967   (gnus-summary-edit-article)
8968   (execute-kbd-macro (concat (this-command-keys) key))
8969   (gnus-article-edit-done))
8970
8971 ;;; Respooling
8972
8973 (defun gnus-summary-respool-query (&optional silent trace)
8974   "Query where the respool algorithm would put this article."
8975   (interactive)
8976   (let (gnus-mark-article-hook)
8977     (gnus-summary-select-article)
8978     (save-excursion
8979       (set-buffer gnus-original-article-buffer)
8980       (save-restriction
8981         (message-narrow-to-head)
8982         (let ((groups (nnmail-article-group 'identity trace)))
8983           (unless silent
8984             (if groups
8985                 (message "This message would go to %s"
8986                          (mapconcat 'car groups ", "))
8987               (message "This message would go to no groups"))
8988             groups))))))
8989
8990 (defun gnus-summary-respool-trace ()
8991   "Trace where the respool algorithm would put this article.
8992 Display a buffer showing all fancy splitting patterns which matched."
8993   (interactive)
8994   (gnus-summary-respool-query nil t))
8995
8996 ;; Summary marking commands.
8997
8998 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8999   "Mark articles which has the same subject as read, and then select the next.
9000 If UNMARK is positive, remove any kind of mark.
9001 If UNMARK is negative, tick articles."
9002   (interactive "P")
9003   (when unmark
9004     (setq unmark (prefix-numeric-value unmark)))
9005   (let ((count
9006          (gnus-summary-mark-same-subject
9007           (gnus-summary-article-subject) unmark)))
9008     ;; Select next unread article.  If auto-select-same mode, should
9009     ;; select the first unread article.
9010     (gnus-summary-next-article t (and gnus-auto-select-same
9011                                       (gnus-summary-article-subject)))
9012     (gnus-message 7 "%d article%s marked as %s"
9013                   count (if (= count 1) " is" "s are")
9014                   (if unmark "unread" "read"))))
9015
9016 (defun gnus-summary-kill-same-subject (&optional unmark)
9017   "Mark articles which has the same subject as read.
9018 If UNMARK is positive, remove any kind of mark.
9019 If UNMARK is negative, tick articles."
9020   (interactive "P")
9021   (when unmark
9022     (setq unmark (prefix-numeric-value unmark)))
9023   (let ((count
9024          (gnus-summary-mark-same-subject
9025           (gnus-summary-article-subject) unmark)))
9026     ;; If marked as read, go to next unread subject.
9027     (when (null unmark)
9028       ;; Go to next unread subject.
9029       (gnus-summary-next-subject 1 t))
9030     (gnus-message 7 "%d articles are marked as %s"
9031                   count (if unmark "unread" "read"))))
9032
9033 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9034   "Mark articles with same SUBJECT as read, and return marked number.
9035 If optional argument UNMARK is positive, remove any kinds of marks.
9036 If optional argument UNMARK is negative, mark articles as unread instead."
9037   (let ((count 1))
9038     (save-excursion
9039       (cond
9040        ((null unmark)                   ; Mark as read.
9041         (while (and
9042                 (progn
9043                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9044                   (gnus-summary-show-thread) t)
9045                 (gnus-summary-find-subject subject))
9046           (setq count (1+ count))))
9047        ((> unmark 0)                    ; Tick.
9048         (while (and
9049                 (progn
9050                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9051                   (gnus-summary-show-thread) t)
9052                 (gnus-summary-find-subject subject))
9053           (setq count (1+ count))))
9054        (t                               ; Mark as unread.
9055         (while (and
9056                 (progn
9057                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9058                   (gnus-summary-show-thread) t)
9059                 (gnus-summary-find-subject subject))
9060           (setq count (1+ count)))))
9061       (gnus-set-mode-line 'summary)
9062       ;; Return the number of marked articles.
9063       count)))
9064
9065 (defun gnus-summary-mark-as-processable (n &optional unmark)
9066   "Set the process mark on the next N articles.
9067 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9068 the process mark instead.  The difference between N and the actual
9069 number of articles marked is returned."
9070   (interactive "P")
9071   (if (and (null n) (gnus-region-active-p))
9072       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9073     (setq n (prefix-numeric-value n))
9074     (let ((backward (< n 0))
9075           (n (abs n)))
9076       (while (and
9077               (> n 0)
9078               (if unmark
9079                   (gnus-summary-remove-process-mark
9080                    (gnus-summary-article-number))
9081                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9082               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9083         (setq n (1- n)))
9084       (when (/= 0 n)
9085         (gnus-message 7 "No more articles"))
9086       (gnus-summary-recenter)
9087       (gnus-summary-position-point)
9088       n)))
9089
9090 (defun gnus-summary-unmark-as-processable (n)
9091   "Remove the process mark from the next N articles.
9092 If N is negative, unmark backward instead.  The difference between N and
9093 the actual number of articles unmarked is returned."
9094   (interactive "P")
9095   (gnus-summary-mark-as-processable n t))
9096
9097 (defun gnus-summary-unmark-all-processable ()
9098   "Remove the process mark from all articles."
9099   (interactive)
9100   (save-excursion
9101     (while gnus-newsgroup-processable
9102       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9103   (gnus-summary-position-point))
9104
9105 (defun gnus-summary-add-mark (article type)
9106   "Mark ARTICLE with a mark of TYPE."
9107   (let ((vtype (car (assq type gnus-article-mark-lists)))
9108         var)
9109     (if (not vtype)
9110         (error "No such mark type: %s" type)
9111       (setq var (intern (format "gnus-newsgroup-%s" type)))
9112       (set var (cons article (symbol-value var)))
9113       (if (memq type '(processable cached replied forwarded recent saved))
9114           (gnus-summary-update-secondary-mark article)
9115         ;;; !!! This is bogus.  We should find out what primary
9116         ;;; !!! mark we want to set.
9117         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9118
9119 (defun gnus-summary-mark-as-expirable (n)
9120   "Mark N articles forward as expirable.
9121 If N is negative, mark backward instead.  The difference between N and
9122 the actual number of articles marked is returned."
9123   (interactive "p")
9124   (gnus-summary-mark-forward n gnus-expirable-mark))
9125
9126 (defun gnus-summary-mark-article-as-replied (article)
9127   "Mark ARTICLE as replied to and update the summary line.
9128 ARTICLE can also be a list of articles."
9129   (interactive (list (gnus-summary-article-number)))
9130   (let ((articles (if (listp article) article (list article))))
9131     (dolist (article articles)
9132       (push article gnus-newsgroup-replied)
9133       (let ((buffer-read-only nil))
9134         (when (gnus-summary-goto-subject article nil t)
9135           (gnus-summary-update-secondary-mark article))))))
9136
9137 (defun gnus-summary-mark-article-as-forwarded (article)
9138   "Mark ARTICLE as forwarded and update the summary line.
9139 ARTICLE can also be a list of articles."
9140   (let ((articles (if (listp article) article (list article))))
9141     (dolist (article articles)
9142       (push article gnus-newsgroup-forwarded)
9143       (let ((buffer-read-only nil))
9144         (when (gnus-summary-goto-subject article nil t)
9145           (gnus-summary-update-secondary-mark article))))))
9146
9147 (defun gnus-summary-set-bookmark (article)
9148   "Set a bookmark in current article."
9149   (interactive (list (gnus-summary-article-number)))
9150   (when (or (not (get-buffer gnus-article-buffer))
9151             (not gnus-current-article)
9152             (not gnus-article-current)
9153             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9154     (error "No current article selected"))
9155   ;; Remove old bookmark, if one exists.
9156   (let ((old (assq article gnus-newsgroup-bookmarks)))
9157     (when old
9158       (setq gnus-newsgroup-bookmarks
9159             (delq old gnus-newsgroup-bookmarks))))
9160   ;; Set the new bookmark, which is on the form
9161   ;; (article-number . line-number-in-body).
9162   (push
9163    (cons article
9164          (save-excursion
9165            (set-buffer gnus-article-buffer)
9166            (count-lines
9167             (min (point)
9168                  (save-excursion
9169                    (goto-char (point-min))
9170                    (search-forward "\n\n" nil t)
9171                    (point)))
9172             (point))))
9173    gnus-newsgroup-bookmarks)
9174   (gnus-message 6 "A bookmark has been added to the current article."))
9175
9176 (defun gnus-summary-remove-bookmark (article)
9177   "Remove the bookmark from the current article."
9178   (interactive (list (gnus-summary-article-number)))
9179   ;; Remove old bookmark, if one exists.
9180   (let ((old (assq article gnus-newsgroup-bookmarks)))
9181     (if old
9182         (progn
9183           (setq gnus-newsgroup-bookmarks
9184                 (delq old gnus-newsgroup-bookmarks))
9185           (gnus-message 6 "Removed bookmark."))
9186       (gnus-message 6 "No bookmark in current article."))))
9187
9188 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9189 (defun gnus-summary-mark-as-dormant (n)
9190   "Mark N articles forward as dormant.
9191 If N is negative, mark backward instead.  The difference between N and
9192 the actual number of articles marked is returned."
9193   (interactive "p")
9194   (gnus-summary-mark-forward n gnus-dormant-mark))
9195
9196 (defun gnus-summary-set-process-mark (article)
9197   "Set the process mark on ARTICLE and update the summary line."
9198   (setq gnus-newsgroup-processable
9199         (cons article
9200               (delq article gnus-newsgroup-processable)))
9201   (when (gnus-summary-goto-subject article)
9202     (gnus-summary-show-thread)
9203     (gnus-summary-goto-subject article)
9204     (gnus-summary-update-secondary-mark article)))
9205
9206 (defun gnus-summary-remove-process-mark (article)
9207   "Remove the process mark from ARTICLE and update the summary line."
9208   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9209   (when (gnus-summary-goto-subject article)
9210     (gnus-summary-show-thread)
9211     (gnus-summary-goto-subject article)
9212     (gnus-summary-update-secondary-mark article)))
9213
9214 (defun gnus-summary-set-saved-mark (article)
9215   "Set the process mark on ARTICLE and update the summary line."
9216   (push article gnus-newsgroup-saved)
9217   (when (gnus-summary-goto-subject article)
9218     (gnus-summary-update-secondary-mark article)))
9219
9220 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9221   "Mark N articles as read forwards.
9222 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9223 The difference between N and the actual number of articles marked is
9224 returned.
9225 Iff NO-EXPIRE, auto-expiry will be inhibited."
9226   (interactive "p")
9227   (gnus-summary-show-thread)
9228   (let ((backward (< n 0))
9229         (gnus-summary-goto-unread
9230          (and gnus-summary-goto-unread
9231               (not (eq gnus-summary-goto-unread 'never))
9232               (not (memq mark (list gnus-unread-mark
9233                                     gnus-ticked-mark gnus-dormant-mark)))))
9234         (n (abs n))
9235         (mark (or mark gnus-del-mark)))
9236     (while (and (> n 0)
9237                 (gnus-summary-mark-article nil mark no-expire)
9238                 (zerop (gnus-summary-next-subject
9239                         (if backward -1 1)
9240                         (and gnus-summary-goto-unread
9241                              (not (eq gnus-summary-goto-unread 'never)))
9242                         t)))
9243       (setq n (1- n)))
9244     (when (/= 0 n)
9245       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9246     (gnus-summary-recenter)
9247     (gnus-summary-position-point)
9248     (gnus-set-mode-line 'summary)
9249     n))
9250
9251 (defun gnus-summary-mark-article-as-read (mark)
9252   "Mark the current article quickly as read with MARK."
9253   (let ((article (gnus-summary-article-number)))
9254     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9255     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9256     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9257     (push (cons article mark) gnus-newsgroup-reads)
9258     ;; Possibly remove from cache, if that is used.
9259     (when gnus-use-cache
9260       (gnus-cache-enter-remove-article article))
9261     ;; Allow the backend to change the mark.
9262     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9263     ;; Check for auto-expiry.
9264     (when (and gnus-newsgroup-auto-expire
9265                (memq mark gnus-auto-expirable-marks))
9266       (setq mark gnus-expirable-mark)
9267       ;; Let the backend know about the mark change.
9268       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9269       (push article gnus-newsgroup-expirable))
9270     ;; Set the mark in the buffer.
9271     (gnus-summary-update-mark mark 'unread)
9272     t))
9273
9274 (defun gnus-summary-mark-article-as-unread (mark)
9275   "Mark the current article quickly as unread with MARK."
9276   (let* ((article (gnus-summary-article-number))
9277          (old-mark (gnus-summary-article-mark article)))
9278     ;; Allow the backend to change the mark.
9279     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9280     (if (eq mark old-mark)
9281         t
9282       (if (<= article 0)
9283           (progn
9284             (gnus-error 1 "Can't mark negative article numbers")
9285             nil)
9286         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9287         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9288         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9289         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9290         (cond ((= mark gnus-ticked-mark)
9291                (push article gnus-newsgroup-marked))
9292               ((= mark gnus-dormant-mark)
9293                (push article gnus-newsgroup-dormant))
9294               (t
9295                (push article gnus-newsgroup-unreads)))
9296         (gnus-pull article gnus-newsgroup-reads)
9297
9298         ;; See whether the article is to be put in the cache.
9299         (and gnus-use-cache
9300              (vectorp (gnus-summary-article-header article))
9301              (save-excursion
9302                (gnus-cache-possibly-enter-article
9303                 gnus-newsgroup-name article
9304                 (gnus-summary-article-header article)
9305                 (= mark gnus-ticked-mark)
9306                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9307
9308         ;; Fix the mark.
9309         (gnus-summary-update-mark mark 'unread)
9310         t))))
9311
9312 (defun gnus-summary-mark-article (&optional article mark no-expire)
9313   "Mark ARTICLE with MARK.  MARK can be any character.
9314 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9315 `??' (dormant) and `?E' (expirable).
9316 If MARK is nil, then the default character `?r' is used.
9317 If ARTICLE is nil, then the article on the current line will be
9318 marked.
9319 Iff NO-EXPIRE, auto-expiry will be inhibited."
9320   ;; The mark might be a string.
9321   (when (stringp mark)
9322     (setq mark (aref mark 0)))
9323   ;; If no mark is given, then we check auto-expiring.
9324   (when (null mark)
9325     (setq mark gnus-del-mark))
9326   (when (and (not no-expire)
9327              gnus-newsgroup-auto-expire
9328              (memq mark gnus-auto-expirable-marks))
9329     (setq mark gnus-expirable-mark))
9330   (let ((article (or article (gnus-summary-article-number)))
9331         (old-mark (gnus-summary-article-mark article)))
9332     ;; Allow the backend to change the mark.
9333     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9334     (if (eq mark old-mark)
9335         t
9336       (unless article
9337         (error "No article on current line"))
9338       (if (not (if (or (= mark gnus-unread-mark)
9339                        (= mark gnus-ticked-mark)
9340                        (= mark gnus-dormant-mark))
9341                    (gnus-mark-article-as-unread article mark)
9342                  (gnus-mark-article-as-read article mark)))
9343           t
9344         ;; See whether the article is to be put in the cache.
9345         (and gnus-use-cache
9346              (not (= mark gnus-canceled-mark))
9347              (vectorp (gnus-summary-article-header article))
9348              (save-excursion
9349                (gnus-cache-possibly-enter-article
9350                 gnus-newsgroup-name article
9351                 (gnus-summary-article-header article)
9352                 (= mark gnus-ticked-mark)
9353                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9354
9355         (when (gnus-summary-goto-subject article nil t)
9356           (let ((buffer-read-only nil))
9357             (gnus-summary-show-thread)
9358             ;; Fix the mark.
9359             (gnus-summary-update-mark mark 'unread)
9360             t))))))
9361
9362 (defun gnus-summary-update-secondary-mark (article)
9363   "Update the secondary (read, process, cache) mark."
9364   (gnus-summary-update-mark
9365    (cond ((memq article gnus-newsgroup-processable)
9366           gnus-process-mark)
9367          ((memq article gnus-newsgroup-cached)
9368           gnus-cached-mark)
9369          ((memq article gnus-newsgroup-replied)
9370           gnus-replied-mark)
9371          ((memq article gnus-newsgroup-forwarded)
9372           gnus-forwarded-mark)
9373          ((memq article gnus-newsgroup-saved)
9374           gnus-saved-mark)
9375          ((memq article gnus-newsgroup-recent)
9376           gnus-recent-mark)
9377          ((memq article gnus-newsgroup-unseen)
9378           gnus-unseen-mark)
9379          (t gnus-no-mark))
9380    'replied)
9381   (when (gnus-visual-p 'summary-highlight 'highlight)
9382     (gnus-run-hooks 'gnus-summary-update-hook))
9383   t)
9384
9385 (defun gnus-summary-update-mark (mark type)
9386   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9387         (buffer-read-only nil))
9388     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9389     (when forward
9390       (when (looking-at "\r")
9391         (incf forward))
9392       (when (<= (+ forward (point)) (point-max))
9393         ;; Go to the right position on the line.
9394         (goto-char (+ forward (point)))
9395         ;; Replace the old mark with the new mark.
9396         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9397         ;; Optionally update the marks by some user rule.
9398         (when (eq type 'unread)
9399           (gnus-data-set-mark
9400            (gnus-data-find (gnus-summary-article-number)) mark)
9401           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9402
9403 (defun gnus-mark-article-as-read (article &optional mark)
9404   "Enter ARTICLE in the pertinent lists and remove it from others."
9405   ;; Make the article expirable.
9406   (let ((mark (or mark gnus-del-mark)))
9407     (if (= mark gnus-expirable-mark)
9408         (push article gnus-newsgroup-expirable)
9409       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9410     ;; Remove from unread and marked lists.
9411     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9412     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9413     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9414     (push (cons article mark) gnus-newsgroup-reads)
9415     ;; Possibly remove from cache, if that is used.
9416     (when gnus-use-cache
9417       (gnus-cache-enter-remove-article article))
9418     t))
9419
9420 (defun gnus-mark-article-as-unread (article &optional mark)
9421   "Enter ARTICLE in the pertinent lists and remove it from others."
9422   (let ((mark (or mark gnus-ticked-mark)))
9423     (if (<= article 0)
9424         (progn
9425           (gnus-error 1 "Can't mark negative article numbers")
9426           nil)
9427       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9428             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9429             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9430             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9431
9432       ;; Unsuppress duplicates?
9433       (when gnus-suppress-duplicates
9434         (gnus-dup-unsuppress-article article))
9435
9436       (cond ((= mark gnus-ticked-mark)
9437              (push article gnus-newsgroup-marked))
9438             ((= mark gnus-dormant-mark)
9439              (push article gnus-newsgroup-dormant))
9440             (t
9441              (push article gnus-newsgroup-unreads)))
9442       (gnus-pull article gnus-newsgroup-reads)
9443       t)))
9444
9445 (defalias 'gnus-summary-mark-as-unread-forward
9446   'gnus-summary-tick-article-forward)
9447 (make-obsolete 'gnus-summary-mark-as-unread-forward
9448                'gnus-summary-tick-article-forward)
9449 (defun gnus-summary-tick-article-forward (n)
9450   "Tick N articles forwards.
9451 If N is negative, tick backwards instead.
9452 The difference between N and the number of articles ticked is returned."
9453   (interactive "p")
9454   (gnus-summary-mark-forward n gnus-ticked-mark))
9455
9456 (defalias 'gnus-summary-mark-as-unread-backward
9457   'gnus-summary-tick-article-backward)
9458 (make-obsolete 'gnus-summary-mark-as-unread-backward
9459                'gnus-summary-tick-article-backward)
9460 (defun gnus-summary-tick-article-backward (n)
9461   "Tick N articles backwards.
9462 The difference between N and the number of articles ticked is returned."
9463   (interactive "p")
9464   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9465
9466 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9467 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9468 (defun gnus-summary-tick-article (&optional article clear-mark)
9469   "Mark current article as unread.
9470 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9471 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9472   (interactive)
9473   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9474                                        gnus-ticked-mark)))
9475
9476 (defun gnus-summary-mark-as-read-forward (n)
9477   "Mark N articles as read forwards.
9478 If N is negative, mark backwards instead.
9479 The difference between N and the actual number of articles marked is
9480 returned."
9481   (interactive "p")
9482   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9483
9484 (defun gnus-summary-mark-as-read-backward (n)
9485   "Mark the N articles as read backwards.
9486 The difference between N and the actual number of articles marked is
9487 returned."
9488   (interactive "p")
9489   (gnus-summary-mark-forward
9490    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9491
9492 (defun gnus-summary-mark-as-read (&optional article mark)
9493   "Mark current article as read.
9494 ARTICLE specifies the article to be marked as read.
9495 MARK specifies a string to be inserted at the beginning of the line."
9496   (gnus-summary-mark-article article mark))
9497
9498 (defun gnus-summary-clear-mark-forward (n)
9499   "Clear marks from N articles forward.
9500 If N is negative, clear backward instead.
9501 The difference between N and the number of marks cleared is returned."
9502   (interactive "p")
9503   (gnus-summary-mark-forward n gnus-unread-mark))
9504
9505 (defun gnus-summary-clear-mark-backward (n)
9506   "Clear marks from N articles backward.
9507 The difference between N and the number of marks cleared is returned."
9508   (interactive "p")
9509   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9510
9511 (defun gnus-summary-mark-unread-as-read ()
9512   "Intended to be used by `gnus-summary-mark-article-hook'."
9513   (when (memq gnus-current-article gnus-newsgroup-unreads)
9514     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9515
9516 (defun gnus-summary-mark-read-and-unread-as-read ()
9517   "Intended to be used by `gnus-summary-mark-article-hook'."
9518   (let ((mark (gnus-summary-article-mark)))
9519     (when (or (gnus-unread-mark-p mark)
9520               (gnus-read-mark-p mark))
9521       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9522
9523 (defun gnus-summary-mark-unread-as-ticked ()
9524   "Intended to be used by `gnus-summary-mark-article-hook'."
9525   (when (memq gnus-current-article gnus-newsgroup-unreads)
9526     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9527
9528 (defun gnus-summary-mark-region-as-read (point mark all)
9529   "Mark all unread articles between point and mark as read.
9530 If given a prefix, mark all articles between point and mark as read,
9531 even ticked and dormant ones."
9532   (interactive "r\nP")
9533   (save-excursion
9534     (let (article)
9535       (goto-char point)
9536       (beginning-of-line)
9537       (while (and
9538               (< (point) mark)
9539               (progn
9540                 (when (or all
9541                           (memq (setq article (gnus-summary-article-number))
9542                                 gnus-newsgroup-unreads))
9543                   (gnus-summary-mark-article article gnus-del-mark))
9544                 t)
9545               (gnus-summary-find-next))))))
9546
9547 (defun gnus-summary-mark-below (score mark)
9548   "Mark articles with score less than SCORE with MARK."
9549   (interactive "P\ncMark: ")
9550   (setq score (if score
9551                   (prefix-numeric-value score)
9552                 (or gnus-summary-default-score 0)))
9553   (save-excursion
9554     (set-buffer gnus-summary-buffer)
9555     (goto-char (point-min))
9556     (while
9557         (progn
9558           (and (< (gnus-summary-article-score) score)
9559                (gnus-summary-mark-article nil mark))
9560           (gnus-summary-find-next)))))
9561
9562 (defun gnus-summary-kill-below (&optional score)
9563   "Mark articles with score below SCORE as read."
9564   (interactive "P")
9565   (gnus-summary-mark-below score gnus-killed-mark))
9566
9567 (defun gnus-summary-clear-above (&optional score)
9568   "Clear all marks from articles with score above SCORE."
9569   (interactive "P")
9570   (gnus-summary-mark-above score gnus-unread-mark))
9571
9572 (defun gnus-summary-tick-above (&optional score)
9573   "Tick all articles with score above SCORE."
9574   (interactive "P")
9575   (gnus-summary-mark-above score gnus-ticked-mark))
9576
9577 (defun gnus-summary-mark-above (score mark)
9578   "Mark articles with score over SCORE with MARK."
9579   (interactive "P\ncMark: ")
9580   (setq score (if score
9581                   (prefix-numeric-value score)
9582                 (or gnus-summary-default-score 0)))
9583   (save-excursion
9584     (set-buffer gnus-summary-buffer)
9585     (goto-char (point-min))
9586     (while (and (progn
9587                   (when (> (gnus-summary-article-score) score)
9588                     (gnus-summary-mark-article nil mark))
9589                   t)
9590                 (gnus-summary-find-next)))))
9591
9592 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9593 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9594 (defun gnus-summary-limit-include-expunged (&optional no-error)
9595   "Display all the hidden articles that were expunged for low scores."
9596   (interactive)
9597   (let ((buffer-read-only nil))
9598     (let ((scored gnus-newsgroup-scored)
9599           headers h)
9600       (while scored
9601         (unless (gnus-summary-article-header (caar scored))
9602           (and (setq h (gnus-number-to-header (caar scored)))
9603                (< (cdar scored) gnus-summary-expunge-below)
9604                (push h headers)))
9605         (setq scored (cdr scored)))
9606       (if (not headers)
9607           (when (not no-error)
9608             (error "No expunged articles hidden"))
9609         (goto-char (point-min))
9610         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9611         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9612         (mapcar (lambda (x) (push (mail-header-number x)
9613                                   gnus-newsgroup-limit))
9614                 headers)
9615         (gnus-summary-prepare-unthreaded (nreverse headers))
9616         (goto-char (point-min))
9617         (gnus-summary-position-point)
9618         t))))
9619
9620 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9621   "Mark all unread articles in this newsgroup as read.
9622 If prefix argument ALL is non-nil, ticked and dormant articles will
9623 also be marked as read.
9624 If QUIETLY is non-nil, no questions will be asked.
9625 If TO-HERE is non-nil, it should be a point in the buffer.  All
9626 articles before (after, if REVERSE is set) this point will be marked as read.
9627 Note that this function will only catch up the unread article
9628 in the current summary buffer limitation.
9629 The number of articles marked as read is returned."
9630   (interactive "P")
9631   (prog1
9632       (save-excursion
9633         (when (or quietly
9634                   (not gnus-interactive-catchup) ;Without confirmation?
9635                   gnus-expert-user
9636                   (gnus-y-or-n-p
9637                    (if all
9638                        "Mark absolutely all articles as read? "
9639                      "Mark all unread articles as read? ")))
9640           (if (and not-mark
9641                    (not gnus-newsgroup-adaptive)
9642                    (not gnus-newsgroup-auto-expire)
9643                    (not gnus-suppress-duplicates)
9644                    (or (not gnus-use-cache)
9645                        (eq gnus-use-cache 'passive)))
9646               (progn
9647                 (when all
9648                   (setq gnus-newsgroup-marked nil
9649                         gnus-newsgroup-dormant nil))
9650                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9651             ;; We actually mark all articles as canceled, which we
9652             ;; have to do when using auto-expiry or adaptive scoring.
9653             (gnus-summary-show-all-threads)
9654             (if (and to-here reverse)
9655                 (progn
9656                   (goto-char to-here)
9657                   (while (and
9658                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9659                           (gnus-summary-find-next (not all) nil nil t))))
9660               (when (gnus-summary-first-subject (not all) t)
9661                 (while (and
9662                         (if to-here (< (point) to-here) t)
9663                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9664                         (gnus-summary-find-next (not all) nil nil t)))))
9665             (gnus-set-mode-line 'summary))
9666           t))
9667     (gnus-summary-position-point)))
9668
9669 (defun gnus-summary-catchup-to-here (&optional all)
9670   "Mark all unticked articles before the current one as read.
9671 If ALL is non-nil, also mark ticked and dormant articles as read."
9672   (interactive "P")
9673   (save-excursion
9674     (gnus-save-hidden-threads
9675       (let ((beg (point)))
9676         ;; We check that there are unread articles.
9677         (when (or all (gnus-summary-find-prev))
9678           (gnus-summary-catchup all t beg)))))
9679   (gnus-summary-position-point))
9680
9681 (defun gnus-summary-catchup-from-here (&optional all)
9682   "Mark all unticked articles after the current one as read.
9683 If ALL is non-nil, also mark ticked and dormant articles as read."
9684   (interactive "P")
9685   (save-excursion
9686     (gnus-save-hidden-threads
9687       (let ((beg (point)))
9688         ;; We check that there are unread articles.
9689         (when (or all (gnus-summary-find-next))
9690           (gnus-summary-catchup all t beg nil t)))))
9691   (gnus-summary-position-point))
9692
9693 (defun gnus-summary-catchup-all (&optional quietly)
9694   "Mark all articles in this newsgroup as read."
9695   (interactive "P")
9696   (gnus-summary-catchup t quietly))
9697
9698 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9699   "Mark all unread articles in this group as read, then exit.
9700 If prefix argument ALL is non-nil, all articles are marked as read.
9701 If QUIETLY is non-nil, no questions will be asked."
9702   (interactive "P")
9703   (when (gnus-summary-catchup all quietly nil 'fast)
9704     ;; Select next newsgroup or exit.
9705     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9706              (eq gnus-auto-select-next 'quietly))
9707         (gnus-summary-next-group nil)
9708       (gnus-summary-exit))))
9709
9710 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9711   "Mark all articles in this newsgroup as read, and then exit."
9712   (interactive "P")
9713   (gnus-summary-catchup-and-exit t quietly))
9714
9715 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9716   "Mark all articles in this group as read and select the next group.
9717 If given a prefix, mark all articles, unread as well as ticked, as
9718 read."
9719   (interactive "P")
9720   (save-excursion
9721     (gnus-summary-catchup all))
9722   (gnus-summary-next-group))
9723
9724 ;;;
9725 ;;; with article
9726 ;;;
9727
9728 (defmacro gnus-with-article (article &rest forms)
9729   "Select ARTICLE and perform FORMS in the original article buffer.
9730 Then replace the article with the result."
9731   `(progn
9732      ;; We don't want the article to be marked as read.
9733      (let (gnus-mark-article-hook)
9734        (gnus-summary-select-article t t nil ,article))
9735      (set-buffer gnus-original-article-buffer)
9736      ,@forms
9737      (if (not (gnus-check-backend-function
9738                'request-replace-article (car gnus-article-current)))
9739          (gnus-message 5 "Read-only group; not replacing")
9740        (unless (gnus-request-replace-article
9741                 ,article (car gnus-article-current)
9742                 (current-buffer) t)
9743          (error "Couldn't replace article")))
9744      ;; The cache and backlog have to be flushed somewhat.
9745      (when gnus-keep-backlog
9746        (gnus-backlog-remove-article
9747         (car gnus-article-current) (cdr gnus-article-current)))
9748      (when gnus-use-cache
9749        (gnus-cache-update-article
9750         (car gnus-article-current) (cdr gnus-article-current)))))
9751
9752 (put 'gnus-with-article 'lisp-indent-function 1)
9753 (put 'gnus-with-article 'edebug-form-spec '(form body))
9754
9755 ;; Thread-based commands.
9756
9757 (defun gnus-summary-articles-in-thread (&optional article)
9758   "Return a list of all articles in the current thread.
9759 If ARTICLE is non-nil, return all articles in the thread that starts
9760 with that article."
9761   (let* ((article (or article (gnus-summary-article-number)))
9762          (data (gnus-data-find-list article))
9763          (top-level (gnus-data-level (car data)))
9764          (top-subject
9765           (cond ((null gnus-thread-operation-ignore-subject)
9766                  (gnus-simplify-subject-re
9767                   (mail-header-subject (gnus-data-header (car data)))))
9768                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9769                  (gnus-simplify-subject-fuzzy
9770                   (mail-header-subject (gnus-data-header (car data)))))
9771                 (t nil)))
9772          (end-point (save-excursion
9773                       (if (gnus-summary-go-to-next-thread)
9774                           (point) (point-max))))
9775          articles)
9776     (while (and data
9777                 (< (gnus-data-pos (car data)) end-point))
9778       (when (or (not top-subject)
9779                 (string= top-subject
9780                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9781                              (gnus-simplify-subject-fuzzy
9782                               (mail-header-subject
9783                                (gnus-data-header (car data))))
9784                            (gnus-simplify-subject-re
9785                             (mail-header-subject
9786                              (gnus-data-header (car data)))))))
9787         (push (gnus-data-number (car data)) articles))
9788       (unless (and (setq data (cdr data))
9789                    (> (gnus-data-level (car data)) top-level))
9790         (setq data nil)))
9791     ;; Return the list of articles.
9792     (nreverse articles)))
9793
9794 (defun gnus-summary-rethread-current ()
9795   "Rethread the thread the current article is part of."
9796   (interactive)
9797   (let* ((gnus-show-threads t)
9798          (article (gnus-summary-article-number))
9799          (id (mail-header-id (gnus-summary-article-header)))
9800          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9801     (unless id
9802       (error "No article on the current line"))
9803     (gnus-rebuild-thread id)
9804     (gnus-summary-goto-subject article)))
9805
9806 (defun gnus-summary-reparent-thread ()
9807   "Make the current article child of the marked (or previous) article.
9808
9809 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9810 is non-nil or the Subject: of both articles are the same."
9811   (interactive)
9812   (unless (not (gnus-group-read-only-p))
9813     (error "The current newsgroup does not support article editing"))
9814   (unless (<= (length gnus-newsgroup-processable) 1)
9815     (error "No more than one article may be marked"))
9816   (save-window-excursion
9817     (let ((gnus-article-buffer " *reparent*")
9818           (current-article (gnus-summary-article-number))
9819           ;; First grab the marked article, otherwise one line up.
9820           (parent-article (if (not (null gnus-newsgroup-processable))
9821                               (car gnus-newsgroup-processable)
9822                             (save-excursion
9823                               (if (eq (forward-line -1) 0)
9824                                   (gnus-summary-article-number)
9825                                 (error "Beginning of summary buffer"))))))
9826       (unless (not (eq current-article parent-article))
9827         (error "An article may not be self-referential"))
9828       (let ((message-id (mail-header-id
9829                          (gnus-summary-article-header parent-article))))
9830         (unless (and message-id (not (equal message-id "")))
9831           (error "No message-id in desired parent"))
9832         (gnus-with-article current-article
9833           (save-restriction
9834             (goto-char (point-min))
9835             (message-narrow-to-head)
9836             (if (re-search-forward "^References: " nil t)
9837                 (progn
9838                   (re-search-forward "^[^ \t]" nil t)
9839                   (forward-line -1)
9840                   (end-of-line)
9841                   (insert " " message-id))
9842               (insert "References: " message-id "\n"))))
9843         (set-buffer gnus-summary-buffer)
9844         (gnus-summary-unmark-all-processable)
9845         (gnus-summary-update-article current-article)
9846         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9847             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9848         (gnus-summary-rethread-current)
9849         (gnus-message 3 "Article %d is now the child of article %d"
9850                       current-article parent-article)))))
9851
9852 (defun gnus-summary-toggle-threads (&optional arg)
9853   "Toggle showing conversation threads.
9854 If ARG is positive number, turn showing conversation threads on."
9855   (interactive "P")
9856   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9857     (setq gnus-show-threads
9858           (if (null arg) (not gnus-show-threads)
9859             (> (prefix-numeric-value arg) 0)))
9860     (gnus-summary-prepare)
9861     (gnus-summary-goto-subject current)
9862     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9863     (gnus-summary-position-point)))
9864
9865 (defun gnus-summary-show-all-threads ()
9866   "Show all threads."
9867   (interactive)
9868   (save-excursion
9869     (let ((buffer-read-only nil))
9870       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9871   (gnus-summary-position-point))
9872
9873 (defun gnus-summary-show-thread ()
9874   "Show thread subtrees.
9875 Returns nil if no thread was there to be shown."
9876   (interactive)
9877   (let ((buffer-read-only nil)
9878         (orig (point))
9879         ;; first goto end then to beg, to have point at beg after let
9880         (end (progn (end-of-line) (point)))
9881         (beg (progn (beginning-of-line) (point))))
9882     (prog1
9883         ;; Any hidden lines here?
9884         (search-forward "\r" end t)
9885       (subst-char-in-region beg end ?\^M ?\n t)
9886       (goto-char orig)
9887       (gnus-summary-position-point))))
9888
9889 (defun gnus-summary-maybe-hide-threads ()
9890   "If requested, hide the threads that should be hidden."
9891   (when (and gnus-show-threads
9892              gnus-thread-hide-subtree)
9893     (gnus-summary-hide-all-threads
9894      (if (or (consp gnus-thread-hide-subtree)
9895              (gnus-functionp gnus-thread-hide-subtree))
9896          (gnus-make-predicate gnus-thread-hide-subtree)
9897        nil))))
9898
9899 ;;; Hiding predicates.
9900
9901 (defun gnus-article-unread-p (header)
9902   (memq (mail-header-number header) gnus-newsgroup-unreads))
9903
9904 (defun gnus-article-unseen-p (header)
9905   (memq (mail-header-number header) gnus-newsgroup-unseen))
9906
9907 (defun gnus-map-articles (predicate articles)
9908   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9909   (apply 'gnus-or (mapcar predicate
9910                           (mapcar 'gnus-summary-article-header articles))))
9911
9912 (defun gnus-summary-hide-all-threads (&optional predicate)
9913   "Hide all thread subtrees.
9914 If PREDICATE is supplied, threads that satisfy this predicate
9915 will not be hidden."
9916   (interactive)
9917   (save-excursion
9918     (goto-char (point-min))
9919     (let ((end nil))
9920       (while (not end)
9921         (when (or (not predicate)
9922                   (gnus-map-articles
9923                    predicate (gnus-summary-article-children)))
9924             (gnus-summary-hide-thread))
9925         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9926   (gnus-summary-position-point))
9927
9928 (defun gnus-summary-hide-thread ()
9929   "Hide thread subtrees.
9930 If PREDICATE is supplied, threads that satisfy this predicate
9931 will not be hidden.
9932 Returns nil if no threads were there to be hidden."
9933   (interactive)
9934   (let ((buffer-read-only nil)
9935         (start (point))
9936         (article (gnus-summary-article-number)))
9937     (goto-char start)
9938     ;; Go forward until either the buffer ends or the subthread
9939     ;; ends.
9940     (when (and (not (eobp))
9941                (or (zerop (gnus-summary-next-thread 1 t))
9942                    (goto-char (point-max))))
9943       (prog1
9944           (if (and (> (point) start)
9945                    (search-backward "\n" start t))
9946               (progn
9947                 (subst-char-in-region start (point) ?\n ?\^M)
9948                 (gnus-summary-goto-subject article))
9949             (goto-char start)
9950             nil)))))
9951
9952 (defun gnus-summary-go-to-next-thread (&optional previous)
9953   "Go to the same level (or less) next thread.
9954 If PREVIOUS is non-nil, go to previous thread instead.
9955 Return the article number moved to, or nil if moving was impossible."
9956   (let ((level (gnus-summary-thread-level))
9957         (way (if previous -1 1))
9958         (beg (point)))
9959     (forward-line way)
9960     (while (and (not (eobp))
9961                 (< level (gnus-summary-thread-level)))
9962       (forward-line way))
9963     (if (eobp)
9964         (progn
9965           (goto-char beg)
9966           nil)
9967       (setq beg (point))
9968       (prog1
9969           (gnus-summary-article-number)
9970         (goto-char beg)))))
9971
9972 (defun gnus-summary-next-thread (n &optional silent)
9973   "Go to the same level next N'th thread.
9974 If N is negative, search backward instead.
9975 Returns the difference between N and the number of skips actually
9976 done.
9977
9978 If SILENT, don't output messages."
9979   (interactive "p")
9980   (let ((backward (< n 0))
9981         (n (abs n)))
9982     (while (and (> n 0)
9983                 (gnus-summary-go-to-next-thread backward))
9984       (decf n))
9985     (unless silent
9986       (gnus-summary-position-point))
9987     (when (and (not silent) (/= 0 n))
9988       (gnus-message 7 "No more threads"))
9989     n))
9990
9991 (defun gnus-summary-prev-thread (n)
9992   "Go to the same level previous N'th thread.
9993 Returns the difference between N and the number of skips actually
9994 done."
9995   (interactive "p")
9996   (gnus-summary-next-thread (- n)))
9997
9998 (defun gnus-summary-go-down-thread ()
9999   "Go down one level in the current thread."
10000   (let ((children (gnus-summary-article-children)))
10001     (when children
10002       (gnus-summary-goto-subject (car children)))))
10003
10004 (defun gnus-summary-go-up-thread ()
10005   "Go up one level in the current thread."
10006   (let ((parent (gnus-summary-article-parent)))
10007     (when parent
10008       (gnus-summary-goto-subject parent))))
10009
10010 (defun gnus-summary-down-thread (n)
10011   "Go down thread N steps.
10012 If N is negative, go up instead.
10013 Returns the difference between N and how many steps down that were
10014 taken."
10015   (interactive "p")
10016   (let ((up (< n 0))
10017         (n (abs n)))
10018     (while (and (> n 0)
10019                 (if up (gnus-summary-go-up-thread)
10020                   (gnus-summary-go-down-thread)))
10021       (setq n (1- n)))
10022     (gnus-summary-position-point)
10023     (when (/= 0 n)
10024       (gnus-message 7 "Can't go further"))
10025     n))
10026
10027 (defun gnus-summary-up-thread (n)
10028   "Go up thread N steps.
10029 If N is negative, go down instead.
10030 Returns the difference between N and how many steps down that were
10031 taken."
10032   (interactive "p")
10033   (gnus-summary-down-thread (- n)))
10034
10035 (defun gnus-summary-top-thread ()
10036   "Go to the top of the thread."
10037   (interactive)
10038   (while (gnus-summary-go-up-thread))
10039   (gnus-summary-article-number))
10040
10041 (defun gnus-summary-kill-thread (&optional unmark)
10042   "Mark articles under current thread as read.
10043 If the prefix argument is positive, remove any kinds of marks.
10044 If the prefix argument is negative, tick articles instead."
10045   (interactive "P")
10046   (when unmark
10047     (setq unmark (prefix-numeric-value unmark)))
10048   (let ((articles (gnus-summary-articles-in-thread)))
10049     (save-excursion
10050       ;; Expand the thread.
10051       (gnus-summary-show-thread)
10052       ;; Mark all the articles.
10053       (while articles
10054         (gnus-summary-goto-subject (car articles))
10055         (cond ((null unmark)
10056                (gnus-summary-mark-article-as-read gnus-killed-mark))
10057               ((> unmark 0)
10058                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10059               (t
10060                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10061         (setq articles (cdr articles))))
10062     ;; Hide killed subtrees.
10063     (and (null unmark)
10064          gnus-thread-hide-killed
10065          (gnus-summary-hide-thread))
10066     ;; If marked as read, go to next unread subject.
10067     (when (null unmark)
10068       ;; Go to next unread subject.
10069       (gnus-summary-next-subject 1 t)))
10070   (gnus-set-mode-line 'summary))
10071
10072 ;; Summary sorting commands
10073
10074 (defun gnus-summary-sort-by-number (&optional reverse)
10075   "Sort the summary buffer by article number.
10076 Argument REVERSE means reverse order."
10077   (interactive "P")
10078   (gnus-summary-sort 'number reverse))
10079
10080 (defun gnus-summary-sort-by-author (&optional reverse)
10081   "Sort the summary buffer by author name alphabetically.
10082 If `case-fold-search' is non-nil, case of letters is ignored.
10083 Argument REVERSE means reverse order."
10084   (interactive "P")
10085   (gnus-summary-sort 'author reverse))
10086
10087 (defun gnus-summary-sort-by-subject (&optional reverse)
10088   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10089 If `case-fold-search' is non-nil, case of letters is ignored.
10090 Argument REVERSE means reverse order."
10091   (interactive "P")
10092   (gnus-summary-sort 'subject reverse))
10093
10094 (defun gnus-summary-sort-by-date (&optional reverse)
10095   "Sort the summary buffer by date.
10096 Argument REVERSE means reverse order."
10097   (interactive "P")
10098   (gnus-summary-sort 'date reverse))
10099
10100 (defun gnus-summary-sort-by-score (&optional reverse)
10101   "Sort the summary buffer by score.
10102 Argument REVERSE means reverse order."
10103   (interactive "P")
10104   (gnus-summary-sort 'score reverse))
10105
10106 (defun gnus-summary-sort-by-lines (&optional reverse)
10107   "Sort the summary buffer by the number of lines.
10108 Argument REVERSE means reverse order."
10109   (interactive "P")
10110   (gnus-summary-sort 'lines reverse))
10111
10112 (defun gnus-summary-sort-by-chars (&optional reverse)
10113   "Sort the summary buffer by article length.
10114 Argument REVERSE means reverse order."
10115   (interactive "P")
10116   (gnus-summary-sort 'chars reverse))
10117
10118 (defun gnus-summary-sort-by-original (&optional reverse)
10119   "Sort the summary buffer using the default sorting method.
10120 Argument REVERSE means reverse order."
10121   (interactive "P")
10122   (let* ((buffer-read-only)
10123          (gnus-summary-prepare-hook nil))
10124     ;; We do the sorting by regenerating the threads.
10125     (gnus-summary-prepare)
10126     ;; Hide subthreads if needed.
10127     (gnus-summary-maybe-hide-threads)))
10128
10129 (defun gnus-summary-sort (predicate reverse)
10130   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10131   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10132          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10133          (gnus-thread-sort-functions
10134           (if (not reverse)
10135               thread
10136             `(lambda (t1 t2)
10137                (,thread t2 t1))))
10138          (gnus-sort-gathered-threads-function
10139           gnus-thread-sort-functions)
10140          (gnus-article-sort-functions
10141           (if (not reverse)
10142               article
10143             `(lambda (t1 t2)
10144                (,article t2 t1))))
10145          (buffer-read-only)
10146          (gnus-summary-prepare-hook nil))
10147     ;; We do the sorting by regenerating the threads.
10148     (gnus-summary-prepare)
10149     ;; Hide subthreads if needed.
10150     (gnus-summary-maybe-hide-threads)))
10151
10152 ;; Summary saving commands.
10153
10154 (defun gnus-summary-save-article (&optional n not-saved)
10155   "Save the current article using the default saver function.
10156 If N is a positive number, save the N next articles.
10157 If N is a negative number, save the N previous articles.
10158 If N is nil and any articles have been marked with the process mark,
10159 save those articles instead.
10160 The variable `gnus-default-article-saver' specifies the saver function."
10161   (interactive "P")
10162   (let* ((articles (gnus-summary-work-articles n))
10163          (save-buffer (save-excursion
10164                         (nnheader-set-temp-buffer " *Gnus Save*")))
10165          (num (length articles))
10166          header file)
10167     (dolist (article articles)
10168       (setq header (gnus-summary-article-header article))
10169       (if (not (vectorp header))
10170           ;; This is a pseudo-article.
10171           (if (assq 'name header)
10172               (gnus-copy-file (cdr (assq 'name header)))
10173             (gnus-message 1 "Article %d is unsaveable" article))
10174         ;; This is a real article.
10175         (save-window-excursion
10176           (let ((gnus-display-mime-function nil)
10177                 (gnus-article-prepare-hook nil))
10178             (gnus-summary-select-article t nil nil article)))
10179         (save-excursion
10180           (set-buffer save-buffer)
10181           (erase-buffer)
10182           (insert-buffer-substring gnus-original-article-buffer))
10183         (setq file (gnus-article-save save-buffer file num))
10184         (gnus-summary-remove-process-mark article)
10185         (unless not-saved
10186           (gnus-summary-set-saved-mark article))))
10187     (gnus-kill-buffer save-buffer)
10188     (gnus-summary-position-point)
10189     (gnus-set-mode-line 'summary)
10190     n))
10191
10192 (defun gnus-summary-pipe-output (&optional arg)
10193   "Pipe the current article to a subprocess.
10194 If N is a positive number, pipe the N next articles.
10195 If N is a negative number, pipe the N previous articles.
10196 If N is nil and any articles have been marked with the process mark,
10197 pipe those articles instead."
10198   (interactive "P")
10199   (require 'gnus-art)
10200   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10201     (gnus-summary-save-article arg t))
10202   (let ((buffer (get-buffer "*Shell Command Output*")))
10203     (if (and buffer
10204              (with-current-buffer buffer (> (point-max) (point-min))))
10205         (gnus-configure-windows 'pipe))))
10206
10207 (defun gnus-summary-save-article-mail (&optional arg)
10208   "Append the current article to an mail file.
10209 If N is a positive number, save the N next articles.
10210 If N is a negative number, save the N previous articles.
10211 If N is nil and any articles have been marked with the process mark,
10212 save those articles instead."
10213   (interactive "P")
10214   (require 'gnus-art)
10215   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10216     (gnus-summary-save-article arg)))
10217
10218 (defun gnus-summary-save-article-rmail (&optional arg)
10219   "Append the current article to an rmail file.
10220 If N is a positive number, save the N next articles.
10221 If N is a negative number, save the N previous articles.
10222 If N is nil and any articles have been marked with the process mark,
10223 save those articles instead."
10224   (interactive "P")
10225   (require 'gnus-art)
10226   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10227     (gnus-summary-save-article arg)))
10228
10229 (defun gnus-summary-save-article-file (&optional arg)
10230   "Append the current article to a file.
10231 If N is a positive number, save the N next articles.
10232 If N is a negative number, save the N previous articles.
10233 If N is nil and any articles have been marked with the process mark,
10234 save those articles instead."
10235   (interactive "P")
10236   (require 'gnus-art)
10237   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10238     (gnus-summary-save-article arg)))
10239
10240 (defun gnus-summary-write-article-file (&optional arg)
10241   "Write the current article to a file, deleting the previous file.
10242 If N is a positive number, save the N next articles.
10243 If N is a negative number, save the N previous articles.
10244 If N is nil and any articles have been marked with the process mark,
10245 save those articles instead."
10246   (interactive "P")
10247   (require 'gnus-art)
10248   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10249     (gnus-summary-save-article arg)))
10250
10251 (defun gnus-summary-save-article-body-file (&optional arg)
10252   "Append the current article body to a file.
10253 If N is a positive number, save the N next articles.
10254 If N is a negative number, save the N previous articles.
10255 If N is nil and any articles have been marked with the process mark,
10256 save those articles instead."
10257   (interactive "P")
10258   (require 'gnus-art)
10259   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10260     (gnus-summary-save-article arg)))
10261
10262 (defun gnus-summary-muttprint (&optional arg)
10263   "Print the current article using Muttprint.
10264 If N is a positive number, save the N next articles.
10265 If N is a negative number, save the N previous articles.
10266 If N is nil and any articles have been marked with the process mark,
10267 save those articles instead."
10268   (interactive "P")
10269   (require 'gnus-art)
10270   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10271     (gnus-summary-save-article arg t)))
10272
10273 (defun gnus-summary-pipe-message (program)
10274   "Pipe the current article through PROGRAM."
10275   (interactive "sProgram: ")
10276   (gnus-summary-select-article)
10277   (let ((mail-header-separator ""))
10278     (gnus-eval-in-buffer-window gnus-article-buffer
10279       (save-restriction
10280         (widen)
10281         (let ((start (window-start))
10282               buffer-read-only)
10283           (message-pipe-buffer-body program)
10284           (set-window-start (get-buffer-window (current-buffer)) start))))))
10285
10286 (defun gnus-get-split-value (methods)
10287   "Return a value based on the split METHODS."
10288   (let (split-name method result match)
10289     (when methods
10290       (save-excursion
10291         (set-buffer gnus-original-article-buffer)
10292         (save-restriction
10293           (nnheader-narrow-to-headers)
10294           (while (and methods (not split-name))
10295             (goto-char (point-min))
10296             (setq method (pop methods))
10297             (setq match (car method))
10298             (when (cond
10299                    ((stringp match)
10300                     ;; Regular expression.
10301                     (ignore-errors
10302                       (re-search-forward match nil t)))
10303                    ((gnus-functionp match)
10304                     ;; Function.
10305                     (save-restriction
10306                       (widen)
10307                       (setq result (funcall match gnus-newsgroup-name))))
10308                    ((consp match)
10309                     ;; Form.
10310                     (save-restriction
10311                       (widen)
10312                       (setq result (eval match)))))
10313               (setq split-name (cdr method))
10314               (cond ((stringp result)
10315                      (push (expand-file-name
10316                             result gnus-article-save-directory)
10317                            split-name))
10318                     ((consp result)
10319                      (setq split-name (append result split-name)))))))))
10320     (nreverse split-name)))
10321
10322 (defun gnus-valid-move-group-p (group)
10323   (and (boundp group)
10324        (symbol-name group)
10325        (symbol-value group)
10326        (gnus-get-function (gnus-find-method-for-group
10327                            (symbol-name group)) 'request-accept-article t)))
10328
10329 (defun gnus-read-move-group-name (prompt default articles prefix)
10330   "Read a group name."
10331   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10332          (minibuffer-confirm-incomplete nil) ; XEmacs
10333          (prom
10334           (format "%s %s to:"
10335                   prompt
10336                   (if (> (length articles) 1)
10337                       (format "these %d articles" (length articles))
10338                     "this article")))
10339          (to-newsgroup
10340           (cond
10341            ((null split-name)
10342             (gnus-completing-read-with-default
10343              default prom
10344              gnus-active-hashtb
10345              'gnus-valid-move-group-p
10346              nil prefix
10347              'gnus-group-history))
10348            ((= 1 (length split-name))
10349             (gnus-completing-read-with-default
10350              (car split-name) prom
10351              gnus-active-hashtb
10352              'gnus-valid-move-group-p
10353              nil nil
10354              'gnus-group-history))
10355            (t
10356             (gnus-completing-read-with-default
10357              nil prom
10358              (mapcar (lambda (el) (list el))
10359                      (nreverse split-name))
10360              nil nil nil
10361              'gnus-group-history))))
10362          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10363     (when to-newsgroup
10364       (if (or (string= to-newsgroup "")
10365               (string= to-newsgroup prefix))
10366           (setq to-newsgroup default))
10367       (unless to-newsgroup
10368         (error "No group name entered"))
10369       (or (gnus-active to-newsgroup)
10370           (gnus-activate-group to-newsgroup nil nil to-method)
10371           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10372                                      to-newsgroup))
10373               (or (and (gnus-request-create-group to-newsgroup to-method)
10374                        (gnus-activate-group
10375                         to-newsgroup nil nil to-method)
10376                        (gnus-subscribe-group to-newsgroup))
10377                   (error "Couldn't create group %s" to-newsgroup)))
10378           (error "No such group: %s" to-newsgroup)))
10379     to-newsgroup))
10380
10381 (defun gnus-summary-save-parts (type dir n &optional reverse)
10382   "Save parts matching TYPE to DIR.
10383 If REVERSE, save parts that do not match TYPE."
10384   (interactive
10385    (list (read-string "Save parts of type: "
10386                       (or (car gnus-summary-save-parts-type-history)
10387                           gnus-summary-save-parts-default-mime)
10388                       'gnus-summary-save-parts-type-history)
10389          (setq gnus-summary-save-parts-last-directory
10390                (read-file-name "Save to directory: "
10391                                gnus-summary-save-parts-last-directory
10392                                nil t))
10393          current-prefix-arg))
10394   (gnus-summary-iterate n
10395     (let ((gnus-display-mime-function nil)
10396           (gnus-inhibit-treatment t))
10397       (gnus-summary-select-article))
10398     (save-excursion
10399       (set-buffer gnus-article-buffer)
10400       (let ((handles (or gnus-article-mime-handles
10401                          (mm-dissect-buffer) (mm-uu-dissect))))
10402         (when handles
10403           (gnus-summary-save-parts-1 type dir handles reverse)
10404           (unless gnus-article-mime-handles ;; Don't destroy this case.
10405             (mm-destroy-parts handles)))))))
10406
10407 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10408   (if (stringp (car handle))
10409       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10410               (cdr handle))
10411     (when (if reverse
10412               (not (string-match type (mm-handle-media-type handle)))
10413             (string-match type (mm-handle-media-type handle)))
10414       (let ((file (expand-file-name
10415                    (file-name-nondirectory
10416                     (or
10417                      (mail-content-type-get
10418                       (mm-handle-disposition handle) 'filename)
10419                      (concat gnus-newsgroup-name
10420                              "." (number-to-string
10421                                   (cdr gnus-article-current)))))
10422                    dir)))
10423         (unless (file-exists-p file)
10424           (mm-save-part-to-file handle file))))))
10425
10426 ;; Summary extract commands
10427
10428 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10429   (let ((buffer-read-only nil)
10430         (article (gnus-summary-article-number))
10431         after-article b e)
10432     (unless (gnus-summary-goto-subject article)
10433       (error "No such article: %d" article))
10434     (gnus-summary-position-point)
10435     ;; If all commands are to be bunched up on one line, we collect
10436     ;; them here.
10437     (unless gnus-view-pseudos-separately
10438       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10439             files action)
10440         (while ps
10441           (setq action (cdr (assq 'action (car ps))))
10442           (setq files (list (cdr (assq 'name (car ps)))))
10443           (while (and ps (cdr ps)
10444                       (string= (or action "1")
10445                                (or (cdr (assq 'action (cadr ps))) "2")))
10446             (push (cdr (assq 'name (cadr ps))) files)
10447             (setcdr ps (cddr ps)))
10448           (when files
10449             (when (not (string-match "%s" action))
10450               (push " " files))
10451             (push " " files)
10452             (when (assq 'execute (car ps))
10453               (setcdr (assq 'execute (car ps))
10454                       (funcall (if (string-match "%s" action)
10455                                    'format 'concat)
10456                                action
10457                                (mapconcat
10458                                 (lambda (f)
10459                                   (if (equal f " ")
10460                                       f
10461                                     (gnus-quote-arg-for-sh-or-csh f)))
10462                                 files " ")))))
10463           (setq ps (cdr ps)))))
10464     (if (and gnus-view-pseudos (not not-view))
10465         (while pslist
10466           (when (assq 'execute (car pslist))
10467             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10468                                   (eq gnus-view-pseudos 'not-confirm)))
10469           (setq pslist (cdr pslist)))
10470       (save-excursion
10471         (while pslist
10472           (setq after-article (or (cdr (assq 'article (car pslist)))
10473                                   (gnus-summary-article-number)))
10474           (gnus-summary-goto-subject after-article)
10475           (forward-line 1)
10476           (setq b (point))
10477           (insert "    " (file-name-nondirectory
10478                           (cdr (assq 'name (car pslist))))
10479                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10480           (setq e (point))
10481           (forward-line -1)             ; back to `b'
10482           (gnus-add-text-properties
10483            b (1- e) (list 'gnus-number gnus-reffed-article-number
10484                           gnus-mouse-face-prop gnus-mouse-face))
10485           (gnus-data-enter
10486            after-article gnus-reffed-article-number
10487            gnus-unread-mark b (car pslist) 0 (- e b))
10488           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10489           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10490           (setq pslist (cdr pslist)))))))
10491
10492 (defun gnus-pseudos< (p1 p2)
10493   (let ((c1 (cdr (assq 'action p1)))
10494         (c2 (cdr (assq 'action p2))))
10495     (and c1 c2 (string< c1 c2))))
10496
10497 (defun gnus-request-pseudo-article (props)
10498   (cond ((assq 'execute props)
10499          (gnus-execute-command (cdr (assq 'execute props)))))
10500   (let ((gnus-current-article (gnus-summary-article-number)))
10501     (gnus-run-hooks 'gnus-mark-article-hook)))
10502
10503 (defun gnus-execute-command (command &optional automatic)
10504   (save-excursion
10505     (gnus-article-setup-buffer)
10506     (set-buffer gnus-article-buffer)
10507     (setq buffer-read-only nil)
10508     (let ((command (if automatic command
10509                      (read-string "Command: " (cons command 0)))))
10510       (erase-buffer)
10511       (insert "$ " command "\n\n")
10512       (if gnus-view-pseudo-asynchronously
10513           (start-process "gnus-execute" (current-buffer) shell-file-name
10514                          shell-command-switch command)
10515         (call-process shell-file-name nil t nil
10516                       shell-command-switch command)))))
10517
10518 ;; Summary kill commands.
10519
10520 (defun gnus-summary-edit-global-kill (article)
10521   "Edit the \"global\" kill file."
10522   (interactive (list (gnus-summary-article-number)))
10523   (gnus-group-edit-global-kill article))
10524
10525 (defun gnus-summary-edit-local-kill ()
10526   "Edit a local kill file applied to the current newsgroup."
10527   (interactive)
10528   (setq gnus-current-headers (gnus-summary-article-header))
10529   (gnus-group-edit-local-kill
10530    (gnus-summary-article-number) gnus-newsgroup-name))
10531
10532 ;;; Header reading.
10533
10534 (defun gnus-read-header (id &optional header)
10535   "Read the headers of article ID and enter them into the Gnus system."
10536   (let ((group gnus-newsgroup-name)
10537         (gnus-override-method
10538          (or
10539           gnus-override-method
10540           (and (gnus-news-group-p gnus-newsgroup-name)
10541                (car (gnus-refer-article-methods)))))
10542         where)
10543     ;; First we check to see whether the header in question is already
10544     ;; fetched.
10545     (if (stringp id)
10546         ;; This is a Message-ID.
10547         (setq header (or header (gnus-id-to-header id)))
10548       ;; This is an article number.
10549       (setq header (or header (gnus-summary-article-header id))))
10550     (if (and header
10551              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10552         ;; We have found the header.
10553         header
10554       ;; If this is a sparse article, we have to nix out its
10555       ;; previous entry in the thread hashtb.
10556       (when (and header
10557                  (gnus-summary-article-sparse-p (mail-header-number header)))
10558         (let* ((parent (gnus-parent-id (mail-header-references header)))
10559                (thread (and parent (gnus-id-to-thread parent))))
10560           (when thread
10561             (delq (assq header thread) thread))))
10562       ;; We have to really fetch the header to this article.
10563       (save-excursion
10564         (set-buffer nntp-server-buffer)
10565         (when (setq where (gnus-request-head id group))
10566           (nnheader-fold-continuation-lines)
10567           (goto-char (point-max))
10568           (insert ".\n")
10569           (goto-char (point-min))
10570           (insert "211 ")
10571           (princ (cond
10572                   ((numberp id) id)
10573                   ((cdr where) (cdr where))
10574                   (header (mail-header-number header))
10575                   (t gnus-reffed-article-number))
10576                  (current-buffer))
10577           (insert " Article retrieved.\n"))
10578         (if (or (not where)
10579                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10580             ()                          ; Malformed head.
10581           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10582             (when (and (stringp id)
10583                        (not (string= (gnus-group-real-name group)
10584                                      (car where))))
10585               ;; If we fetched by Message-ID and the article came
10586               ;; from a different group, we fudge some bogus article
10587               ;; numbers for this article.
10588               (mail-header-set-number header gnus-reffed-article-number))
10589             (save-excursion
10590               (set-buffer gnus-summary-buffer)
10591               (decf gnus-reffed-article-number)
10592               (gnus-remove-header (mail-header-number header))
10593               (push header gnus-newsgroup-headers)
10594               (setq gnus-current-headers header)
10595               (push (mail-header-number header) gnus-newsgroup-limit)))
10596           header)))))
10597
10598 (defun gnus-remove-header (number)
10599   "Remove header NUMBER from `gnus-newsgroup-headers'."
10600   (if (and gnus-newsgroup-headers
10601            (= number (mail-header-number (car gnus-newsgroup-headers))))
10602       (pop gnus-newsgroup-headers)
10603     (let ((headers gnus-newsgroup-headers))
10604       (while (and (cdr headers)
10605                   (not (= number (mail-header-number (cadr headers)))))
10606         (pop headers))
10607       (when (cdr headers)
10608         (setcdr headers (cddr headers))))))
10609
10610 ;;;
10611 ;;; summary highlights
10612 ;;;
10613
10614 (defun gnus-highlight-selected-summary ()
10615   "Highlight selected article in summary buffer."
10616   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10617   (when gnus-summary-selected-face
10618     (save-excursion
10619       (let* ((beg (progn (beginning-of-line) (point)))
10620              (end (progn (end-of-line) (point)))
10621              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10622              (from (if (get-text-property beg gnus-mouse-face-prop)
10623                        beg
10624                      (or (next-single-property-change
10625                           beg gnus-mouse-face-prop nil end)
10626                          beg)))
10627              (to
10628               (if (= from end)
10629                   (- from 2)
10630                 (or (next-single-property-change
10631                      from gnus-mouse-face-prop nil end)
10632                     end))))
10633         ;; If no mouse-face prop on line we will have to = from = end,
10634         ;; so we highlight the entire line instead.
10635         (when (= (+ to 2) from)
10636           (setq from beg)
10637           (setq to end))
10638         (if gnus-newsgroup-selected-overlay
10639             ;; Move old overlay.
10640             (gnus-move-overlay
10641              gnus-newsgroup-selected-overlay from to (current-buffer))
10642           ;; Create new overlay.
10643           (gnus-overlay-put
10644            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10645            'face gnus-summary-selected-face))))))
10646
10647 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10648 (defun gnus-summary-highlight-line ()
10649   "Highlight current line according to `gnus-summary-highlight'."
10650   (let* ((list gnus-summary-highlight)
10651          (p (point))
10652          (end (progn (end-of-line) (point)))
10653          ;; now find out where the line starts and leave point there.
10654          (beg (progn (beginning-of-line) (point)))
10655          (article (gnus-summary-article-number))
10656          (score (or (cdr (assq (or article gnus-current-article)
10657                                gnus-newsgroup-scored))
10658                     gnus-summary-default-score 0))
10659          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10660          (inhibit-read-only t))
10661     ;; Eval the cars of the lists until we find a match.
10662     (let ((default gnus-summary-default-score)
10663           (default-high gnus-summary-default-high-score)
10664           (default-low gnus-summary-default-low-score))
10665       (while (and list
10666                   (not (eval (caar list))))
10667         (setq list (cdr list))))
10668     (let ((face (cdar list)))
10669       (unless (eq face (get-text-property beg 'face))
10670         (gnus-put-text-property-excluding-characters-with-faces
10671          beg end 'face
10672          (setq face (if (boundp face) (symbol-value face) face)))
10673         (when gnus-summary-highlight-line-function
10674           (funcall gnus-summary-highlight-line-function article face))))
10675     (goto-char p)))
10676
10677 (defun gnus-update-read-articles (group unread &optional compute)
10678   "Update the list of read articles in GROUP."
10679   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10680          (entry (gnus-gethash group gnus-newsrc-hashtb))
10681          (info (nth 2 entry))
10682          (prev 1)
10683          (unread (sort (copy-sequence unread) '<))
10684          read)
10685     (if (or (not info) (not active))
10686         ;; There is no info on this group if it was, in fact,
10687         ;; killed.  Gnus stores no information on killed groups, so
10688         ;; there's nothing to be done.
10689         ;; One could store the information somewhere temporarily,
10690         ;; perhaps...  Hmmm...
10691         ()
10692       ;; Remove any negative articles numbers.
10693       (while (and unread (< (car unread) 0))
10694         (setq unread (cdr unread)))
10695       ;; Remove any expired article numbers
10696       (while (and unread (< (car unread) (car active)))
10697         (setq unread (cdr unread)))
10698       ;; Compute the ranges of read articles by looking at the list of
10699       ;; unread articles.
10700       (while unread
10701         (when (/= (car unread) prev)
10702           (push (if (= prev (1- (car unread))) prev
10703                   (cons prev (1- (car unread))))
10704                 read))
10705         (setq prev (1+ (car unread)))
10706         (setq unread (cdr unread)))
10707       (when (<= prev (cdr active))
10708         (push (cons prev (cdr active)) read))
10709       (setq read (if (> (length read) 1) (nreverse read) read))
10710       (if compute
10711           read
10712         (save-excursion
10713           (let (setmarkundo)
10714             ;; Propagate the read marks to the backend.
10715             (when (gnus-check-backend-function 'request-set-mark group)
10716               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10717                     (add (gnus-remove-from-range read (gnus-info-read info))))
10718                 (when (or add del)
10719                   (unless (gnus-check-group group)
10720                     (error "Can't open server for %s" group))
10721                   (gnus-request-set-mark
10722                    group (delq nil (list (if add (list add 'add '(read)))
10723                                          (if del (list del 'del '(read))))))
10724                   (setq setmarkundo
10725                         `(gnus-request-set-mark
10726                           ,group
10727                           ',(delq nil (list
10728                                        (if del (list del 'add '(read)))
10729                                        (if add (list add 'del '(read))))))))))
10730             (set-buffer gnus-group-buffer)
10731             (gnus-undo-register
10732               `(progn
10733                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10734                  (gnus-info-set-read ',info ',(gnus-info-read info))
10735                  (gnus-get-unread-articles-in-group ',info
10736                                                     (gnus-active ,group))
10737                  (gnus-group-update-group ,group t)
10738                  ,setmarkundo))))
10739         ;; Enter this list into the group info.
10740         (gnus-info-set-read info read)
10741         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10742         (gnus-get-unread-articles-in-group info (gnus-active group))
10743         t))))
10744
10745 (defun gnus-offer-save-summaries ()
10746   "Offer to save all active summary buffers."
10747   (let (buffers)
10748     ;; Go through all buffers and find all summaries.
10749     (dolist (buffer (buffer-list))
10750       (when (and (setq buffer (buffer-name buffer))
10751                  (string-match "Summary" buffer)
10752                  (save-excursion
10753                    (set-buffer buffer)
10754                    ;; We check that this is, indeed, a summary buffer.
10755                    (and (eq major-mode 'gnus-summary-mode)
10756                         ;; Also make sure this isn't bogus.
10757                         gnus-newsgroup-prepared
10758                         ;; Also make sure that this isn't a
10759                         ;; dead summary buffer.
10760                         (not gnus-dead-summary-mode))))
10761         (push buffer buffers)))
10762     ;; Go through all these summary buffers and offer to save them.
10763     (when buffers
10764       (save-excursion
10765         (map-y-or-n-p
10766          "Update summary buffer %s? "
10767          (lambda (buf)
10768            (switch-to-buffer buf)
10769            (gnus-summary-exit))
10770          buffers)))))
10771
10772
10773 ;;; @ for mime-partial
10774 ;;;
10775
10776 (defun gnus-request-partial-message ()
10777   (save-excursion
10778     (let ((number (gnus-summary-article-number))
10779           (group gnus-newsgroup-name)
10780           (mother gnus-article-buffer))
10781       (set-buffer (get-buffer-create " *Partial Article*"))
10782       (erase-buffer)
10783       (setq mime-preview-buffer mother)
10784       (gnus-request-article-this-buffer number group)
10785       (mime-parse-buffer)
10786       )))
10787
10788 (autoload 'mime-combine-message/partial-pieces-automatically
10789   "mime-partial"
10790   "Internal method to combine message/partial messages automatically.")
10791
10792 (mime-add-condition
10793  'action '((type . message)(subtype . partial)
10794            (major-mode . gnus-original-article-mode)
10795            (method . mime-combine-message/partial-pieces-automatically)
10796            (summary-buffer-exp . gnus-summary-buffer)
10797            (request-partial-message-method . gnus-request-partial-message)
10798            ))
10799
10800
10801 ;;; @ for message/rfc822
10802 ;;;
10803
10804 (defun gnus-mime-extract-message/rfc822 (entity situation)
10805   "Burst a forwarded article."
10806   (save-excursion
10807     (set-buffer gnus-summary-buffer)
10808     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
10809                                    gnus-newsgroup-name 'gnus-group-history))
10810            (gnus-group-marked (list group))
10811            article info)
10812       (with-temp-buffer
10813         (mime-insert-entity-content entity)
10814         (setq article (gnus-request-accept-article group)))
10815       (when (and (consp article)
10816                  (numberp (setq article (cdr article))))
10817         (setq info (gnus-get-info group))
10818         (gnus-info-set-read info
10819                             (gnus-remove-from-range (gnus-info-read info)
10820                                                     (list article)))
10821         (when (string-equal group gnus-newsgroup-name)
10822           (forward-line 1)
10823           (let (gnus-show-threads)
10824             (gnus-summary-goto-subject article t))
10825           (gnus-summary-clear-mark-forward 1))
10826         (set-buffer gnus-group-buffer)
10827         (gnus-group-get-new-news-this-group nil t)))))
10828
10829 (mime-add-condition
10830  'action '((type . message)(subtype . rfc822)
10831            (major-mode . gnus-original-article-mode)
10832            (method . gnus-mime-extract-message/rfc822)
10833            (mode . "extract")
10834            ))
10835
10836 (mime-add-condition
10837  'action '((type . message)(subtype . news)
10838            (major-mode . gnus-original-article-mode)
10839            (method . gnus-mime-extract-message/rfc822)
10840            (mode . "extract")
10841            ))
10842
10843 (defun gnus-mime-extract-multipart (entity situation)
10844   (let ((children (mime-entity-children entity))
10845         mime-acting-situation-to-override
10846         f)
10847     (while children
10848       (mime-play-entity (car children)
10849                         (cons (assq 'mode situation)
10850                               mime-acting-situation-to-override))
10851       (setq children (cdr children)))
10852     (if (setq f (cdr (assq 'after-method
10853                            mime-acting-situation-to-override)))
10854         (eval f)
10855       )))
10856
10857 (mime-add-condition
10858  'action '((type . multipart)
10859            (method . gnus-mime-extract-multipart)
10860            (mode . "extract")
10861            )
10862  'with-default)
10863
10864
10865 ;;; @ end
10866 ;;;
10867
10868 (defun gnus-summary-setup-default-charset ()
10869   "Setup newsgroup default charset."
10870   (if (equal gnus-newsgroup-name "nndraft:drafts")
10871       (setq gnus-newsgroup-charset nil)
10872     (let* ((ignored-charsets
10873             (or gnus-newsgroup-ephemeral-ignored-charsets
10874                 (append
10875                  (and gnus-newsgroup-name
10876                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10877                  gnus-newsgroup-ignored-charsets))))
10878       (setq gnus-newsgroup-charset
10879             (or gnus-newsgroup-ephemeral-charset
10880                 (and gnus-newsgroup-name
10881                      (gnus-parameter-charset gnus-newsgroup-name))
10882                 gnus-default-charset))
10883       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10884            ignored-charsets))))
10885
10886 ;;;
10887 ;;; Mime Commands
10888 ;;;
10889
10890 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10891   "Display the current article buffer fully MIME-buttonized.
10892 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10893 treated as multipart/mixed."
10894   (interactive "P")
10895   (require 'gnus-art)
10896   (let ((gnus-unbuttonized-mime-types nil)
10897         (gnus-mime-display-multipart-as-mixed show-all-parts))
10898     (gnus-summary-show-article)))
10899
10900 (defun gnus-summary-repair-multipart (article)
10901   "Add a Content-Type header to a multipart article without one."
10902   (interactive (list (gnus-summary-article-number)))
10903   (gnus-with-article article
10904     (message-narrow-to-head)
10905     (message-remove-header "Mime-Version")
10906     (goto-char (point-max))
10907     (insert "Mime-Version: 1.0\n")
10908     (widen)
10909     (when (search-forward "\n--" nil t)
10910       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10911         (message-narrow-to-head)
10912         (message-remove-header "Content-Type")
10913         (goto-char (point-max))
10914         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10915                         separator))
10916         (widen))))
10917   (let (gnus-mark-article-hook)
10918     (gnus-summary-select-article t t nil article)))
10919
10920 (defun gnus-summary-toggle-display-buttonized ()
10921   "Toggle the buttonizing of the article buffer."
10922   (interactive)
10923   (require 'gnus-art)
10924   (if (setq gnus-inhibit-mime-unbuttonizing
10925             (not gnus-inhibit-mime-unbuttonizing))
10926       (let ((gnus-unbuttonized-mime-types nil))
10927         (gnus-summary-show-article))
10928     (gnus-summary-show-article)))
10929
10930 ;;;
10931 ;;; Intelli-mouse commmands
10932 ;;;
10933
10934 (defun gnus-wheel-summary-scroll (event)
10935   (interactive "e")
10936   (let ((amount (if (memq 'shift (event-modifiers event))
10937                     (car gnus-wheel-scroll-amount)
10938                   (cdr gnus-wheel-scroll-amount)))
10939         (direction (- (* (static-if (featurep 'xemacs)
10940                              (event-button event)
10941                            (cond ((eq 'mouse-4 (event-basic-type event))
10942                                   4)
10943                                  ((eq 'mouse-5 (event-basic-type event))
10944                                   5)))
10945                          2) 9))
10946         edge)
10947     (gnus-summary-scroll-up (* amount direction))
10948     (when (gnus-eval-in-buffer-window gnus-article-buffer
10949             (save-restriction
10950               (widen)
10951               (and (if (< 0 direction)
10952                        (gnus-article-next-page 0)
10953                      (gnus-article-prev-page 0)
10954                      (bobp))
10955                    (if (setq edge (get-text-property
10956                                    (point-min) 'gnus-wheel-edge))
10957                        (setq edge (* edge direction))
10958                      (setq edge -1))
10959                    (or (plusp edge)
10960                        (let ((buffer-read-only nil)
10961                              (inhibit-read-only t))
10962                          (put-text-property (point-min) (point-max)
10963                                             'gnus-wheel-edge direction)
10964                          nil))
10965                    (or (> edge gnus-wheel-edge-resistance)
10966                        (let ((buffer-read-only nil)
10967                              (inhibit-read-only t))
10968                          (put-text-property (point-min) (point-max)
10969                                             'gnus-wheel-edge
10970                                             (* (1+ edge) direction))
10971                          nil))
10972                    (eq last-command 'gnus-wheel-summary-scroll))))
10973       (gnus-summary-next-article nil nil (minusp direction)))))
10974
10975 (defun gnus-wheel-install ()
10976   "Enable mouse wheel support on summary window."
10977   (when gnus-use-wheel
10978     (let ((keys
10979            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10980       (dolist (key keys)
10981         (define-key gnus-summary-mode-map key
10982           'gnus-wheel-summary-scroll)))))
10983
10984 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10985
10986 ;;;
10987 ;;; Traditional PGP commmands
10988 ;;;
10989
10990 (defun gnus-summary-decrypt-article (&optional force)
10991   "Decrypt the current article in traditional PGP way.
10992 This will have permanent effect only in mail groups.
10993 If FORCE is non-nil, allow editing of articles even in read-only
10994 groups."
10995   (interactive "P")
10996   (gnus-summary-select-article t)
10997   (gnus-eval-in-buffer-window gnus-article-buffer
10998     (save-excursion
10999       (save-restriction
11000         (widen)
11001         (goto-char (point-min))
11002         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11003           (error "Not a traditional PGP message!"))
11004         (let ((armor-start (match-beginning 0)))
11005           (if (and (pgg-decrypt-region armor-start (point-max))
11006                    (or force (not (gnus-group-read-only-p))))
11007               (let ((inhibit-read-only t)
11008                     buffer-read-only)
11009                 (delete-region armor-start
11010                                (progn
11011                                  (re-search-forward "^-+END PGP" nil t)
11012                                  (beginning-of-line 2)
11013                                  (point)))
11014                 (insert-buffer-substring pgg-output-buffer))))))))
11015
11016 (defun gnus-summary-verify-article ()
11017   "Verify the current article in traditional PGP way."
11018   (interactive)
11019   (save-excursion
11020     (set-buffer gnus-original-article-buffer)
11021     (goto-char (point-min))
11022     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11023       (error "Not a traditional PGP message!"))
11024     (re-search-forward "^-+END PGP" nil t)
11025     (beginning-of-line 2)
11026     (call-interactively (function pgg-verify-region))))
11027
11028 ;;;
11029 ;;; Generic summary marking commands
11030 ;;;
11031
11032 (defvar gnus-summary-marking-alist
11033   '((read gnus-del-mark "d")
11034     (unread gnus-unread-mark "u")
11035     (ticked gnus-ticked-mark "!")
11036     (dormant gnus-dormant-mark "?")
11037     (expirable gnus-expirable-mark "e"))
11038   "An alist of names/marks/keystrokes.")
11039
11040 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11041 (defvar gnus-summary-mark-map)
11042
11043 (defun gnus-summary-make-all-marking-commands ()
11044   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11045   (dolist (elem gnus-summary-marking-alist)
11046     (apply 'gnus-summary-make-marking-command elem)))
11047
11048 (defun gnus-summary-make-marking-command (name mark keystroke)
11049   (let ((map (make-sparse-keymap)))
11050     (define-key gnus-summary-generic-mark-map keystroke map)
11051     (dolist (lway `((next "next" next nil "n")
11052                     (next-unread "next unread" next t "N")
11053                     (prev "previous" prev nil "p")
11054                     (prev-unread "previous unread" prev t "P")
11055                     (nomove "" nil nil ,keystroke)))
11056       (let ((func (gnus-summary-make-marking-command-1
11057                    mark (car lway) lway name)))
11058         (setq func (eval func))
11059         (define-key map (nth 4 lway) func)))))
11060
11061 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11062   `(defun ,(intern
11063             (format "gnus-summary-put-mark-as-%s%s"
11064                     name (if (eq way 'nomove)
11065                              ""
11066                            (concat "-" (symbol-name way)))))
11067      (n)
11068      ,(format
11069        "Mark the current article as %s%s.
11070 If N, the prefix, then repeat N times.
11071 If N is negative, move in reverse order.
11072 The difference between N and the actual number of articles marked is
11073 returned."
11074        name (car (cdr lway)))
11075      (interactive "p")
11076      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11077
11078 (defun gnus-summary-generic-mark (n mark move unread)
11079   "Mark N articles with MARK."
11080   (unless (eq major-mode 'gnus-summary-mode)
11081     (error "This command can only be used in the summary buffer"))
11082   (gnus-summary-show-thread)
11083   (let ((nummove
11084          (cond
11085           ((eq move 'next) 1)
11086           ((eq move 'prev) -1)
11087           (t 0))))
11088     (if (zerop nummove)
11089         (setq n 1)
11090       (when (< n 0)
11091         (setq n (abs n)
11092               nummove (* -1 nummove))))
11093     (while (and (> n 0)
11094                 (gnus-summary-mark-article nil mark)
11095                 (zerop (gnus-summary-next-subject nummove unread t)))
11096       (setq n (1- n)))
11097     (when (/= 0 n)
11098       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11099     (gnus-summary-recenter)
11100     (gnus-summary-position-point)
11101     (gnus-set-mode-line 'summary)
11102     n))
11103
11104 (defun gnus-summary-insert-articles (articles)
11105   (when (setq articles
11106               (gnus-set-difference articles
11107                                    (mapcar (lambda (h) (mail-header-number h))
11108                                            gnus-newsgroup-headers)))
11109     (setq gnus-newsgroup-headers
11110           (merge 'list
11111                  gnus-newsgroup-headers
11112                  (gnus-fetch-headers articles)
11113                  'gnus-article-sort-by-number))
11114     ;; Suppress duplicates?
11115     (when gnus-suppress-duplicates
11116       (gnus-dup-suppress-articles))
11117
11118     ;; We might want to build some more threads first.
11119     (when (and gnus-fetch-old-headers
11120                (eq gnus-headers-retrieved-by 'nov))
11121       (if (eq gnus-fetch-old-headers 'invisible)
11122           (gnus-build-all-threads)
11123         (gnus-build-old-threads)))
11124     ;; Let the Gnus agent mark articles as read.
11125     (when gnus-agent
11126       (gnus-agent-get-undownloaded-list))
11127     ;; Remove list identifiers from subject
11128     (when gnus-list-identifiers
11129       (gnus-summary-remove-list-identifiers))
11130     ;; First and last article in this newsgroup.
11131     (when gnus-newsgroup-headers
11132       (setq gnus-newsgroup-begin
11133             (mail-header-number (car gnus-newsgroup-headers))
11134             gnus-newsgroup-end
11135             (mail-header-number
11136              (gnus-last-element gnus-newsgroup-headers))))
11137     (when gnus-use-scoring
11138       (gnus-possibly-score-headers))))
11139
11140 (defun gnus-summary-insert-old-articles (&optional all)
11141   "Insert all old articles in this group.
11142 If ALL is non-nil, already read articles become readable.
11143 If ALL is a number, fetch this number of articles."
11144   (interactive "P")
11145   (prog1
11146       (let ((old (mapcar 'car gnus-newsgroup-data))
11147             (i (car gnus-newsgroup-active))
11148             older len)
11149         (while (<= i (cdr gnus-newsgroup-active))
11150           (or (memq i old) (push i older))
11151           (incf i))
11152         (setq len (length older))
11153         (cond
11154          ((null older) nil)
11155          ((numberp all)
11156           (if (< all len)
11157               (setq older (subseq older 0 all))))
11158          (all nil)
11159          (t
11160           (if (and (numberp gnus-large-newsgroup)
11161                    (> len gnus-large-newsgroup))
11162               (let ((input
11163                      (read-string
11164                       (format
11165                        "How many articles from %s (default %d): "
11166                        (gnus-limit-string
11167                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
11168                        len))))
11169                 (unless (string-match "^[ \t]*$" input)
11170                   (setq all (string-to-number input))
11171                   (if (< all len)
11172                       (setq older (subseq older 0 all))))))))
11173         (if (not older)
11174             (message "No old news.")
11175           (let ((gnus-fetch-old-headers t))
11176             (gnus-summary-insert-articles older))
11177           (gnus-summary-limit (gnus-union older old))))
11178     (gnus-summary-position-point)))
11179
11180 (defun gnus-summary-insert-new-articles ()
11181   "Insert all new articles in this group."
11182   (interactive)
11183   (prog1
11184       (let ((old (mapcar 'car gnus-newsgroup-data))
11185             (old-active gnus-newsgroup-active)
11186             (nnmail-fetched-sources (list t))
11187             i new)
11188         (setq gnus-newsgroup-active
11189               (gnus-activate-group gnus-newsgroup-name 'scan))
11190         (setq i (1+ (cdr old-active)))
11191         (while (<= i (cdr gnus-newsgroup-active))
11192           (push i new)
11193           (incf i))
11194         (if (not new)
11195             (message "No gnus is bad news.")
11196           (setq new (nreverse new))
11197           (gnus-summary-insert-articles new)
11198           (setq gnus-newsgroup-unreads
11199                 (append gnus-newsgroup-unreads new))
11200           (gnus-summary-limit (gnus-union old new))))
11201     (gnus-summary-position-point)))
11202
11203 (gnus-summary-make-all-marking-commands)
11204
11205 (gnus-ems-redefine)
11206
11207 (provide 'gnus-sum)
11208
11209 (run-hooks 'gnus-sum-load-hook)
11210
11211 ;;; gnus-sum.el ends here