Synch with latest snapshot of Oort Gnus (the CVS server is currently down).
[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   :version "21.3"
1064   :group 'gnus-summary
1065   :type 'string)
1066
1067 (defcustom gnus-article-no-strict-mime nil
1068   "If non-nil, don't require strict MIME.
1069 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1070 supply the MIME-Version header or deliberately strip it From the mail.
1071 Set it to non-nil, Gnus will treat some articles as MIME even if
1072 the MIME-Version header is missed."
1073   :version "21.3"
1074   :type 'boolean
1075   :group 'gnus-article)
1076
1077 ;;; Internal variables
1078
1079 (defvar gnus-summary-display-cache nil)
1080 (defvar gnus-article-mime-handles nil)
1081 (defvar gnus-article-decoded-p nil)
1082 (defvar gnus-article-charset nil)
1083 (defvar gnus-article-ignored-charsets nil)
1084 (defvar gnus-scores-exclude-files nil)
1085 (defvar gnus-page-broken nil)
1086 (defvar gnus-inhibit-mime-unbuttonizing nil)
1087
1088 (defvar gnus-original-article nil)
1089 (defvar gnus-article-internal-prepare-hook nil)
1090 (defvar gnus-newsgroup-process-stack nil)
1091
1092 (defvar gnus-thread-indent-array nil)
1093 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1094 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1095   "Function called to sort the articles within a thread after it has been gathered together.")
1096
1097 (defvar gnus-summary-save-parts-type-history nil)
1098 (defvar gnus-summary-save-parts-last-directory nil)
1099
1100 (defvar gnus-summary-save-parts-type-history nil)
1101 (defvar gnus-summary-save-parts-last-directory nil)
1102
1103 ;; Avoid highlighting in kill files.
1104 (defvar gnus-summary-inhibit-highlight nil)
1105 (defvar gnus-newsgroup-selected-overlay nil)
1106 (defvar gnus-inhibit-limiting nil)
1107 (defvar gnus-newsgroup-adaptive-score-file nil)
1108 (defvar gnus-current-score-file nil)
1109 (defvar gnus-current-move-group nil)
1110 (defvar gnus-current-copy-group nil)
1111 (defvar gnus-current-crosspost-group nil)
1112 (defvar gnus-newsgroup-display nil)
1113
1114 (defvar gnus-newsgroup-dependencies nil)
1115 (defvar gnus-newsgroup-adaptive nil)
1116 (defvar gnus-summary-display-article-function nil)
1117 (defvar gnus-summary-highlight-line-function nil
1118   "Function called after highlighting a summary line.")
1119
1120 (defvar gnus-summary-line-format-alist
1121   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1122     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1123     (?s gnus-tmp-subject-or-nil ?s)
1124     (?n gnus-tmp-name ?s)
1125     (?A (std11-address-string
1126          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1127     (?a (or (std11-full-name-string
1128              (car (mime-entity-read-field gnus-tmp-header 'From)))
1129             gnus-tmp-from) ?s)
1130     (?F gnus-tmp-from ?s)
1131     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1132     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1133     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1134     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1135     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1136     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1137     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1138     (?L gnus-tmp-lines ?s)
1139     (?I gnus-tmp-indentation ?s)
1140     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1141     (?R gnus-tmp-replied ?c)
1142     (?\[ gnus-tmp-opening-bracket ?c)
1143     (?\] gnus-tmp-closing-bracket ?c)
1144     (?\> (make-string gnus-tmp-level ? ) ?s)
1145     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1146     (?i gnus-tmp-score ?d)
1147     (?z gnus-tmp-score-char ?c)
1148     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1149     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1150     (?U gnus-tmp-unread ?c)
1151     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1152         ?s)
1153     (?t (gnus-summary-number-of-articles-in-thread
1154          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1155         ?d)
1156     (?e (gnus-summary-number-of-articles-in-thread
1157          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1158         ?c)
1159     (?u gnus-tmp-user-defined ?s)
1160     (?P (gnus-pick-line-number) ?d)
1161     (?B gnus-tmp-thread-tree-header-string ?s)
1162     (user-date (gnus-user-date
1163                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1164   "An alist of format specifications that can appear in summary lines.
1165 These are paired with what variables they correspond with, along with
1166 the type of the variable (string, integer, character, etc).")
1167
1168 (defvar gnus-summary-dummy-line-format-alist
1169   `((?S gnus-tmp-subject ?s)
1170     (?N gnus-tmp-number ?d)
1171     (?u gnus-tmp-user-defined ?s)))
1172
1173 (defvar gnus-summary-mode-line-format-alist
1174   `((?G gnus-tmp-group-name ?s)
1175     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1176     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1177     (?A gnus-tmp-article-number ?d)
1178     (?Z gnus-tmp-unread-and-unselected ?s)
1179     (?V gnus-version ?s)
1180     (?U gnus-tmp-unread-and-unticked ?d)
1181     (?S gnus-tmp-subject ?s)
1182     (?e gnus-tmp-unselected ?d)
1183     (?u gnus-tmp-user-defined ?s)
1184     (?d (length gnus-newsgroup-dormant) ?d)
1185     (?t (length gnus-newsgroup-marked) ?d)
1186     (?r (length gnus-newsgroup-reads) ?d)
1187     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1188     (?E gnus-newsgroup-expunged-tally ?d)
1189     (?s (gnus-current-score-file-nondirectory) ?s)))
1190
1191 (defvar gnus-last-search-regexp nil
1192   "Default regexp for article search command.")
1193
1194 (defvar gnus-summary-search-article-matched-data nil
1195   "Last matched data of article search command.  It is the local variable
1196 in `gnus-article-buffer' which consists of the list of start position,
1197 end position and text.")
1198
1199 (defvar gnus-last-shell-command nil
1200   "Default shell command on article.")
1201
1202 (defvar gnus-newsgroup-begin nil)
1203 (defvar gnus-newsgroup-end nil)
1204 (defvar gnus-newsgroup-last-rmail nil)
1205 (defvar gnus-newsgroup-last-mail nil)
1206 (defvar gnus-newsgroup-last-folder nil)
1207 (defvar gnus-newsgroup-last-file nil)
1208 (defvar gnus-newsgroup-auto-expire nil)
1209 (defvar gnus-newsgroup-active nil)
1210
1211 (defvar gnus-newsgroup-data nil)
1212 (defvar gnus-newsgroup-data-reverse nil)
1213 (defvar gnus-newsgroup-limit nil)
1214 (defvar gnus-newsgroup-limits nil)
1215
1216 (defvar gnus-newsgroup-unreads nil
1217   "Sorted list of unread articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-unselected nil
1220   "Sorted list of unselected unread articles in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-reads nil
1223   "Alist of read articles and article marks in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-expunged-tally nil)
1226
1227 (defvar gnus-newsgroup-marked nil
1228   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1229
1230 (defvar gnus-newsgroup-killed nil
1231   "List of ranges of articles that have been through the scoring process.")
1232
1233 (defvar gnus-newsgroup-cached nil
1234   "Sorted list of articles that come from the article cache.")
1235
1236 (defvar gnus-newsgroup-saved nil
1237   "List of articles that have been saved.")
1238
1239 (defvar gnus-newsgroup-kill-headers nil)
1240
1241 (defvar gnus-newsgroup-replied nil
1242   "List of articles that have been replied to in the current newsgroup.")
1243
1244 (defvar gnus-newsgroup-forwarded nil
1245   "List of articles that have been forwarded in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-recent nil
1248   "List of articles that have are recent in the current newsgroup.")
1249
1250 (defvar gnus-newsgroup-expirable nil
1251   "Sorted list of articles in the current newsgroup that can be expired.")
1252
1253 (defvar gnus-newsgroup-processable nil
1254   "List of articles in the current newsgroup that can be processed.")
1255
1256 (defvar gnus-newsgroup-downloadable nil
1257   "Sorted list of articles in the current newsgroup that can be processed.")
1258
1259 (defvar gnus-newsgroup-undownloaded nil
1260   "List of articles in the current newsgroup that haven't been downloaded..")
1261
1262 (defvar gnus-newsgroup-unsendable nil
1263   "List of articles in the current newsgroup that won't be sent.")
1264
1265 (defvar gnus-newsgroup-bookmarks nil
1266   "List of articles in the current newsgroup that have bookmarks.")
1267
1268 (defvar gnus-newsgroup-dormant nil
1269   "Sorted list of dormant articles in the current newsgroup.")
1270
1271 (defvar gnus-newsgroup-unseen nil
1272   "List of unseen articles in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-seen nil
1275   "Range of seen articles in the current newsgroup.")
1276
1277 (defvar gnus-newsgroup-articles nil
1278   "List of articles in the current newsgroup.")
1279
1280 (defvar gnus-newsgroup-scored nil
1281   "List of scored articles in the current newsgroup.")
1282
1283 (defvar gnus-newsgroup-incorporated nil
1284   "List of incorporated articles in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-headers nil
1287   "List of article headers in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-threads nil)
1290
1291 (defvar gnus-newsgroup-prepared nil
1292   "Whether the current group has been prepared properly.")
1293
1294 (defvar gnus-newsgroup-ancient nil
1295   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-sparse nil)
1298
1299 (defvar gnus-current-article nil)
1300 (defvar gnus-article-current nil)
1301 (defvar gnus-current-headers nil)
1302 (defvar gnus-have-all-headers nil)
1303 (defvar gnus-last-article nil)
1304 (defvar gnus-newsgroup-history nil)
1305 (defvar gnus-newsgroup-charset nil)
1306 (defvar gnus-newsgroup-ephemeral-charset nil)
1307 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1308
1309 (defvar gnus-article-before-search nil)
1310
1311 (defconst gnus-summary-local-variables
1312   '(gnus-newsgroup-name
1313     gnus-newsgroup-begin gnus-newsgroup-end
1314     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1315     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1316     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1317     gnus-newsgroup-unselected gnus-newsgroup-marked
1318     gnus-newsgroup-reads gnus-newsgroup-saved
1319     gnus-newsgroup-replied gnus-newsgroup-forwarded
1320     gnus-newsgroup-recent
1321     gnus-newsgroup-expirable
1322     gnus-newsgroup-processable gnus-newsgroup-killed
1323     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1324     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1325     gnus-newsgroup-seen gnus-newsgroup-articles
1326     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1327     gnus-newsgroup-headers gnus-newsgroup-threads
1328     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1329     gnus-current-article gnus-current-headers gnus-have-all-headers
1330     gnus-last-article gnus-article-internal-prepare-hook
1331     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1332     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1333     gnus-thread-expunge-below
1334     gnus-score-alist gnus-current-score-file
1335     (gnus-summary-expunge-below . global)
1336     (gnus-summary-mark-below . global)
1337     (gnus-orphan-score . global)
1338     gnus-newsgroup-active gnus-scores-exclude-files
1339     gnus-newsgroup-history gnus-newsgroup-ancient
1340     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1341     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1342     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1343     (gnus-newsgroup-expunged-tally . 0)
1344     gnus-cache-removable-articles gnus-newsgroup-cached
1345     gnus-newsgroup-data gnus-newsgroup-data-reverse
1346     gnus-newsgroup-limit gnus-newsgroup-limits
1347     gnus-newsgroup-charset gnus-newsgroup-display
1348     gnus-newsgroup-incorporated)
1349   "Variables that are buffer-local to the summary buffers.")
1350
1351 (defvar gnus-newsgroup-variables nil
1352   "A list of variables that have separate values in different newsgroups.
1353 A list of newsgroup (summary buffer) local variables, or cons of
1354 variables and their default values (when the default values are not
1355 nil), that should be made global while the summary buffer is active.
1356 These variables can be used to set variables in the group parameters
1357 while still allowing them to affect operations done in other
1358 buffers. For example:
1359
1360 \(setq gnus-newsgroup-variables
1361      '(message-use-followup-to
1362        (gnus-visible-headers .
1363          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1364 ")
1365
1366 ;; Byte-compiler warning.
1367 (eval-when-compile (defvar gnus-article-mode-map))
1368
1369 ;; Subject simplification.
1370
1371 (defun gnus-simplify-whitespace (str)
1372   "Remove excessive whitespace from STR."
1373   (let ((mystr str))
1374     ;; Multiple spaces.
1375     (while (string-match "[ \t][ \t]+" mystr)
1376       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1377                           " "
1378                           (substring mystr (match-end 0)))))
1379     ;; Leading spaces.
1380     (when (string-match "^[ \t]+" mystr)
1381       (setq mystr (substring mystr (match-end 0))))
1382     ;; Trailing spaces.
1383     (when (string-match "[ \t]+$" mystr)
1384       (setq mystr (substring mystr 0 (match-beginning 0))))
1385     mystr))
1386
1387 (defsubst gnus-simplify-subject-re (subject)
1388   "Remove \"Re:\" from subject lines."
1389   (if (string-match message-subject-re-regexp subject)
1390       (substring subject (match-end 0))
1391     subject))
1392
1393 (defun gnus-simplify-subject (subject &optional re-only)
1394   "Remove `Re:' and words in parentheses.
1395 If RE-ONLY is non-nil, strip leading `Re:'s only."
1396   (let ((case-fold-search t))           ;Ignore case.
1397     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1398     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1399       (setq subject (substring subject (match-end 0))))
1400     ;; Remove uninteresting prefixes.
1401     (when (and (not re-only)
1402                gnus-simplify-ignored-prefixes
1403                (string-match gnus-simplify-ignored-prefixes subject))
1404       (setq subject (substring subject (match-end 0))))
1405     ;; Remove words in parentheses from end.
1406     (unless re-only
1407       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1408         (setq subject (substring subject 0 (match-beginning 0)))))
1409     ;; Return subject string.
1410     subject))
1411
1412 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1413 ;; all whitespace.
1414 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1415   (goto-char (point-min))
1416   (while (re-search-forward regexp nil t)
1417     (replace-match (or newtext ""))))
1418
1419 (defun gnus-simplify-buffer-fuzzy ()
1420   "Simplify string in the buffer fuzzily.
1421 The string in the accessible portion of the current buffer is simplified.
1422 It is assumed to be a single-line subject.
1423 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1424 matter is removed.  Additional things can be deleted by setting
1425 `gnus-simplify-subject-fuzzy-regexp'."
1426   (let ((case-fold-search t)
1427         (modified-tick))
1428     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1429
1430     (while (not (eq modified-tick (buffer-modified-tick)))
1431       (setq modified-tick (buffer-modified-tick))
1432       (cond
1433        ((listp gnus-simplify-subject-fuzzy-regexp)
1434         (mapcar 'gnus-simplify-buffer-fuzzy-step
1435                 gnus-simplify-subject-fuzzy-regexp))
1436        (gnus-simplify-subject-fuzzy-regexp
1437         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1438       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1439       (gnus-simplify-buffer-fuzzy-step
1440        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1441       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1442
1443     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1444     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1445     (gnus-simplify-buffer-fuzzy-step " $")
1446     (gnus-simplify-buffer-fuzzy-step "^ +")))
1447
1448 (defun gnus-simplify-subject-fuzzy (subject)
1449   "Simplify a subject string fuzzily.
1450 See `gnus-simplify-buffer-fuzzy' for details."
1451   (save-excursion
1452     (gnus-set-work-buffer)
1453     (let ((case-fold-search t))
1454       ;; Remove uninteresting prefixes.
1455       (when (and gnus-simplify-ignored-prefixes
1456                  (string-match gnus-simplify-ignored-prefixes subject))
1457         (setq subject (substring subject (match-end 0))))
1458       (insert subject)
1459       (inline (gnus-simplify-buffer-fuzzy))
1460       (buffer-string))))
1461
1462 (defsubst gnus-simplify-subject-fully (subject)
1463   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1464   (cond
1465    (gnus-simplify-subject-functions
1466     (gnus-map-function gnus-simplify-subject-functions subject))
1467    ((null gnus-summary-gather-subject-limit)
1468     (gnus-simplify-subject-re subject))
1469    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1470     (gnus-simplify-subject-fuzzy subject))
1471    ((numberp gnus-summary-gather-subject-limit)
1472     (gnus-limit-string (gnus-simplify-subject-re subject)
1473                        gnus-summary-gather-subject-limit))
1474    (t
1475     subject)))
1476
1477 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1478   "Check whether two subjects are equal.
1479 If optional argument simple-first is t, first argument is already
1480 simplified."
1481   (cond
1482    ((null simple-first)
1483     (equal (gnus-simplify-subject-fully s1)
1484            (gnus-simplify-subject-fully s2)))
1485    (t
1486     (equal s1
1487            (gnus-simplify-subject-fully s2)))))
1488
1489 (defun gnus-summary-bubble-group ()
1490   "Increase the score of the current group.
1491 This is a handy function to add to `gnus-summary-exit-hook' to
1492 increase the score of each group you read."
1493   (gnus-group-add-score gnus-newsgroup-name))
1494
1495 \f
1496 ;;;
1497 ;;; Gnus summary mode
1498 ;;;
1499
1500 (put 'gnus-summary-mode 'mode-class 'special)
1501
1502 (defvar gnus-article-commands-menu)
1503
1504 (when t
1505   ;; Non-orthogonal keys
1506
1507   (gnus-define-keys gnus-summary-mode-map
1508     " " gnus-summary-next-page
1509     "\177" gnus-summary-prev-page
1510     [delete] gnus-summary-prev-page
1511     [backspace] gnus-summary-prev-page
1512     "\r" gnus-summary-scroll-up
1513     "\M-\r" gnus-summary-scroll-down
1514     "n" gnus-summary-next-unread-article
1515     "p" gnus-summary-prev-unread-article
1516     "N" gnus-summary-next-article
1517     "P" gnus-summary-prev-article
1518     "\M-\C-n" gnus-summary-next-same-subject
1519     "\M-\C-p" gnus-summary-prev-same-subject
1520     "\M-n" gnus-summary-next-unread-subject
1521     "\M-p" gnus-summary-prev-unread-subject
1522     "." gnus-summary-first-unread-article
1523     "," gnus-summary-best-unread-article
1524     "\M-s" gnus-summary-search-article-forward
1525     "\M-r" gnus-summary-search-article-backward
1526     "<" gnus-summary-beginning-of-article
1527     ">" gnus-summary-end-of-article
1528     "j" gnus-summary-goto-article
1529     "^" gnus-summary-refer-parent-article
1530     "\M-^" gnus-summary-refer-article
1531     "u" gnus-summary-tick-article-forward
1532     "!" gnus-summary-tick-article-forward
1533     "U" gnus-summary-tick-article-backward
1534     "d" gnus-summary-mark-as-read-forward
1535     "D" gnus-summary-mark-as-read-backward
1536     "E" gnus-summary-mark-as-expirable
1537     "\M-u" gnus-summary-clear-mark-forward
1538     "\M-U" gnus-summary-clear-mark-backward
1539     "k" gnus-summary-kill-same-subject-and-select
1540     "\C-k" gnus-summary-kill-same-subject
1541     "\M-\C-k" gnus-summary-kill-thread
1542     "\M-\C-l" gnus-summary-lower-thread
1543     "e" gnus-summary-edit-article
1544     "#" gnus-summary-mark-as-processable
1545     "\M-#" gnus-summary-unmark-as-processable
1546     "\M-\C-t" gnus-summary-toggle-threads
1547     "\M-\C-s" gnus-summary-show-thread
1548     "\M-\C-h" gnus-summary-hide-thread
1549     "\M-\C-f" gnus-summary-next-thread
1550     "\M-\C-b" gnus-summary-prev-thread
1551     [(meta down)] gnus-summary-next-thread
1552     [(meta up)] gnus-summary-prev-thread
1553     "\M-\C-u" gnus-summary-up-thread
1554     "\M-\C-d" gnus-summary-down-thread
1555     "&" gnus-summary-execute-command
1556     "c" gnus-summary-catchup-and-exit
1557     "\C-w" gnus-summary-mark-region-as-read
1558     "\C-t" gnus-summary-toggle-truncation
1559     "?" gnus-summary-mark-as-dormant
1560     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1561     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1562     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1563     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1564     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1565     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1566     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1567     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1568     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1569     "=" gnus-summary-expand-window
1570     "\C-x\C-s" gnus-summary-reselect-current-group
1571     "\M-g" gnus-summary-rescan-group
1572     "w" gnus-summary-stop-page-breaking
1573     "\C-c\C-r" gnus-summary-caesar-message
1574     "\M-t" gnus-summary-toggle-mime
1575     "f" gnus-summary-followup
1576     "F" gnus-summary-followup-with-original
1577     "C" gnus-summary-cancel-article
1578     "r" gnus-summary-reply
1579     "R" gnus-summary-reply-with-original
1580     "\C-c\C-f" gnus-summary-mail-forward
1581     "o" gnus-summary-save-article
1582     "\C-o" gnus-summary-save-article-mail
1583     "|" gnus-summary-pipe-output
1584     "\M-k" gnus-summary-edit-local-kill
1585     "\M-K" gnus-summary-edit-global-kill
1586     ;; "V" gnus-version
1587     "\C-c\C-d" gnus-summary-describe-group
1588     "q" gnus-summary-exit
1589     "Q" gnus-summary-exit-no-update
1590     "\C-c\C-i" gnus-info-find-node
1591     gnus-mouse-2 gnus-mouse-pick-article
1592     "m" gnus-summary-mail-other-window
1593     "a" gnus-summary-post-news
1594     "i" gnus-summary-news-other-window
1595     "x" gnus-summary-limit-to-unread
1596     "s" gnus-summary-isearch-article
1597     "t" gnus-article-toggle-headers
1598     "g" gnus-summary-show-article
1599     "l" gnus-summary-goto-last-article
1600     "v" gnus-summary-preview-mime-message
1601     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1602     "\C-d" gnus-summary-enter-digest-group
1603     "\M-\C-d" gnus-summary-read-document
1604     "\M-\C-e" gnus-summary-edit-parameters
1605     "\M-\C-a" gnus-summary-customize-parameters
1606     "\C-c\C-b" gnus-bug
1607     "\C-c\C-n" gnus-namazu-search
1608     "*" gnus-cache-enter-article
1609     "\M-*" gnus-cache-remove-article
1610     "\M-&" gnus-summary-universal-argument
1611     "\C-l" gnus-recenter
1612     "I" gnus-summary-increase-score
1613     "L" gnus-summary-lower-score
1614     "\M-i" gnus-symbolic-argument
1615     "h" gnus-summary-select-article-buffer
1616
1617     "V" gnus-summary-score-map
1618     "X" gnus-uu-extract-map
1619     "S" gnus-summary-send-map)
1620
1621   ;; Sort of orthogonal keymap
1622   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1623     "t" gnus-summary-tick-article-forward
1624     "!" gnus-summary-tick-article-forward
1625     "d" gnus-summary-mark-as-read-forward
1626     "r" gnus-summary-mark-as-read-forward
1627     "c" gnus-summary-clear-mark-forward
1628     " " gnus-summary-clear-mark-forward
1629     "e" gnus-summary-mark-as-expirable
1630     "x" gnus-summary-mark-as-expirable
1631     "?" gnus-summary-mark-as-dormant
1632     "b" gnus-summary-set-bookmark
1633     "B" gnus-summary-remove-bookmark
1634     "#" gnus-summary-mark-as-processable
1635     "\M-#" gnus-summary-unmark-as-processable
1636     "S" gnus-summary-limit-include-expunged
1637     "C" gnus-summary-catchup
1638     "H" gnus-summary-catchup-to-here
1639     "h" gnus-summary-catchup-from-here
1640     "\C-c" gnus-summary-catchup-all
1641     "k" gnus-summary-kill-same-subject-and-select
1642     "K" gnus-summary-kill-same-subject
1643     "P" gnus-uu-mark-map)
1644
1645   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1646     "c" gnus-summary-clear-above
1647     "u" gnus-summary-tick-above
1648     "m" gnus-summary-mark-above
1649     "k" gnus-summary-kill-below)
1650
1651   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1652     "/" gnus-summary-limit-to-subject
1653     "n" gnus-summary-limit-to-articles
1654     "w" gnus-summary-pop-limit
1655     "s" gnus-summary-limit-to-subject
1656     "a" gnus-summary-limit-to-author
1657     "u" gnus-summary-limit-to-unread
1658     "m" gnus-summary-limit-to-marks
1659     "M" gnus-summary-limit-exclude-marks
1660     "v" gnus-summary-limit-to-score
1661     "*" gnus-summary-limit-include-cached
1662     "D" gnus-summary-limit-include-dormant
1663     "T" gnus-summary-limit-include-thread
1664     "d" gnus-summary-limit-exclude-dormant
1665     "t" gnus-summary-limit-to-age
1666     "x" gnus-summary-limit-to-extra
1667     "p" gnus-summary-limit-to-display-predicate
1668     "E" gnus-summary-limit-include-expunged
1669     "c" gnus-summary-limit-exclude-childless-dormant
1670     "C" gnus-summary-limit-mark-excluded-as-read
1671     "o" gnus-summary-insert-old-articles
1672     "N" gnus-summary-insert-new-articles)
1673
1674   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1675     "n" gnus-summary-next-unread-article
1676     "p" gnus-summary-prev-unread-article
1677     "N" gnus-summary-next-article
1678     "P" gnus-summary-prev-article
1679     "\C-n" gnus-summary-next-same-subject
1680     "\C-p" gnus-summary-prev-same-subject
1681     "\M-n" gnus-summary-next-unread-subject
1682     "\M-p" gnus-summary-prev-unread-subject
1683     "f" gnus-summary-first-unread-article
1684     "b" gnus-summary-best-unread-article
1685     "j" gnus-summary-goto-article
1686     "g" gnus-summary-goto-subject
1687     "l" gnus-summary-goto-last-article
1688     "o" gnus-summary-pop-article)
1689
1690   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1691     "k" gnus-summary-kill-thread
1692     "l" gnus-summary-lower-thread
1693     "i" gnus-summary-raise-thread
1694     "T" gnus-summary-toggle-threads
1695     "t" gnus-summary-rethread-current
1696     "^" gnus-summary-reparent-thread
1697     "s" gnus-summary-show-thread
1698     "S" gnus-summary-show-all-threads
1699     "h" gnus-summary-hide-thread
1700     "H" gnus-summary-hide-all-threads
1701     "n" gnus-summary-next-thread
1702     "p" gnus-summary-prev-thread
1703     "u" gnus-summary-up-thread
1704     "o" gnus-summary-top-thread
1705     "d" gnus-summary-down-thread
1706     "#" gnus-uu-mark-thread
1707     "\M-#" gnus-uu-unmark-thread)
1708
1709   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1710     "g" gnus-summary-prepare
1711     "c" gnus-summary-insert-cached-articles)
1712
1713   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1714     "c" gnus-summary-catchup-and-exit
1715     "C" gnus-summary-catchup-all-and-exit
1716     "E" gnus-summary-exit-no-update
1717     "J" gnus-summary-jump-to-other-group
1718     "Q" gnus-summary-exit
1719     "Z" gnus-summary-exit
1720     "n" gnus-summary-catchup-and-goto-next-group
1721     "R" gnus-summary-reselect-current-group
1722     "G" gnus-summary-rescan-group
1723     "N" gnus-summary-next-group
1724     "s" gnus-summary-save-newsrc
1725     "P" gnus-summary-prev-group)
1726
1727   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1728     " " gnus-summary-next-page
1729     "n" gnus-summary-next-page
1730     "\177" gnus-summary-prev-page
1731     [delete] gnus-summary-prev-page
1732     "p" gnus-summary-prev-page
1733     "\r" gnus-summary-scroll-up
1734     "\M-\r" gnus-summary-scroll-down
1735     "<" gnus-summary-beginning-of-article
1736     ">" gnus-summary-end-of-article
1737     "b" gnus-summary-beginning-of-article
1738     "e" gnus-summary-end-of-article
1739     "^" gnus-summary-refer-parent-article
1740     "r" gnus-summary-refer-parent-article
1741     "D" gnus-summary-enter-digest-group
1742     "R" gnus-summary-refer-references
1743     "T" gnus-summary-refer-thread
1744     "g" gnus-summary-show-article
1745     "s" gnus-summary-isearch-article
1746     "P" gnus-summary-print-article
1747     "M" gnus-mailing-list-insinuate
1748     "t" gnus-article-babel)
1749
1750   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1751     "b" gnus-article-add-buttons
1752     "B" gnus-article-add-buttons-to-head
1753     "o" gnus-article-treat-overstrike
1754     "e" gnus-article-emphasize
1755     "w" gnus-article-fill-cited-article
1756     "Q" gnus-article-fill-long-lines
1757     "C" gnus-article-capitalize-sentences
1758     "c" gnus-article-remove-cr
1759     "Z" gnus-article-decode-HZ
1760     "h" gnus-article-wash-html
1761     "u" gnus-article-unsplit-urls
1762     "f" gnus-article-display-x-face
1763     "l" gnus-summary-stop-page-breaking
1764     "r" gnus-summary-caesar-message
1765     "t" gnus-article-toggle-headers
1766     "g" gnus-treat-smiley
1767     "v" gnus-summary-verbose-headers
1768     "m" gnus-summary-toggle-mime
1769     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1770     "p" gnus-article-verify-x-pgp-sig
1771     "d" gnus-article-treat-dumbquotes)
1772
1773   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1774     "a" gnus-article-hide
1775     "h" gnus-article-toggle-headers
1776     "b" gnus-article-hide-boring-headers
1777     "s" gnus-article-hide-signature
1778     "c" gnus-article-hide-citation
1779     "C" gnus-article-hide-citation-in-followups
1780     "l" gnus-article-hide-list-identifiers
1781     "p" gnus-article-hide-pgp
1782     "B" gnus-article-strip-banner
1783     "P" gnus-article-hide-pem
1784     "\C-c" gnus-article-hide-citation-maybe)
1785
1786   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1787     "a" gnus-article-highlight
1788     "h" gnus-article-highlight-headers
1789     "c" gnus-article-highlight-citation
1790     "s" gnus-article-highlight-signature)
1791
1792   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1793     "f" gnus-article-treat-fold-headers
1794     "u" gnus-article-treat-unfold-headers
1795     "n" gnus-article-treat-fold-newsgroups)
1796
1797   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1798     "x" gnus-article-display-x-face
1799     "s" gnus-treat-smiley
1800     "D" gnus-article-remove-images
1801     "f" gnus-treat-from-picon
1802     "m" gnus-treat-mail-picon
1803     "n" gnus-treat-newsgroups-picon)
1804
1805   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1806     "z" gnus-article-date-ut
1807     "u" gnus-article-date-ut
1808     "l" gnus-article-date-local
1809     "p" gnus-article-date-english
1810     "e" gnus-article-date-lapsed
1811     "o" gnus-article-date-original
1812     "i" gnus-article-date-iso8601
1813     "s" gnus-article-date-user)
1814
1815   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1816     "t" gnus-article-remove-trailing-blank-lines
1817     "l" gnus-article-strip-leading-blank-lines
1818     "m" gnus-article-strip-multiple-blank-lines
1819     "a" gnus-article-strip-blank-lines
1820     "A" gnus-article-strip-all-blank-lines
1821     "s" gnus-article-strip-leading-space
1822     "e" gnus-article-strip-trailing-space
1823     "w" gnus-article-remove-leading-whitespace)
1824
1825   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1826     "v" gnus-version
1827     "f" gnus-summary-fetch-faq
1828     "d" gnus-summary-describe-group
1829     "h" gnus-summary-describe-briefly
1830     "i" gnus-info-find-node)
1831
1832   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1833     "e" gnus-summary-expire-articles
1834     "\M-\C-e" gnus-summary-expire-articles-now
1835     "\177" gnus-summary-delete-article
1836     [delete] gnus-summary-delete-article
1837     [backspace] gnus-summary-delete-article
1838     "m" gnus-summary-move-article
1839     "r" gnus-summary-respool-article
1840     "w" gnus-summary-edit-article
1841     "c" gnus-summary-copy-article
1842     "B" gnus-summary-crosspost-article
1843     "q" gnus-summary-respool-query
1844     "t" gnus-summary-respool-trace
1845     "i" gnus-summary-import-article
1846     "I" gnus-summary-create-article
1847     "p" gnus-summary-article-posted-p)
1848
1849   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1850     "o" gnus-summary-save-article
1851     "m" gnus-summary-save-article-mail
1852     "F" gnus-summary-write-article-file
1853     "r" gnus-summary-save-article-rmail
1854     "f" gnus-summary-save-article-file
1855     "b" gnus-summary-save-article-body-file
1856     "h" gnus-summary-save-article-folder
1857     "v" gnus-summary-save-article-vm
1858     "p" gnus-summary-pipe-output
1859     "P" gnus-summary-muttprint
1860     "s" gnus-soup-add-article)
1861
1862   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1863     "b" gnus-summary-display-buttonized
1864     "m" gnus-summary-repair-multipart
1865     "v" gnus-article-view-part
1866     "o" gnus-article-save-part
1867     "c" gnus-article-copy-part
1868     "C" gnus-article-view-part-as-charset
1869     "e" gnus-article-externalize-part
1870     "E" gnus-article-encrypt-body
1871     "i" gnus-article-inline-part
1872     "|" gnus-article-pipe-part))
1873
1874 (defvar gnus-article-post-menu nil)
1875
1876 (defun gnus-summary-make-menu-bar ()
1877   (gnus-turn-off-edit-menu 'summary)
1878
1879   (unless (boundp 'gnus-summary-misc-menu)
1880
1881     (easy-menu-define
1882      gnus-summary-kill-menu gnus-summary-mode-map ""
1883      (cons
1884       "Score"
1885       (nconc
1886        (list
1887         ["Customize" gnus-score-customize t])
1888        (gnus-make-score-map 'increase)
1889        (gnus-make-score-map 'lower)
1890        '(("Mark"
1891           ["Kill below" gnus-summary-kill-below t]
1892           ["Mark above" gnus-summary-mark-above t]
1893           ["Tick above" gnus-summary-tick-above t]
1894           ["Clear above" gnus-summary-clear-above t])
1895          ["Current score" gnus-summary-current-score t]
1896          ["Set score" gnus-summary-set-score t]
1897          ["Switch current score file..." gnus-score-change-score-file t]
1898          ["Set mark below..." gnus-score-set-mark-below t]
1899          ["Set expunge below..." gnus-score-set-expunge-below t]
1900          ["Edit current score file" gnus-score-edit-current-scores t]
1901          ["Edit score file" gnus-score-edit-file t]
1902          ["Trace score" gnus-score-find-trace t]
1903          ["Find words" gnus-score-find-favourite-words t]
1904          ["Rescore buffer" gnus-summary-rescore t]
1905          ["Increase score..." gnus-summary-increase-score t]
1906          ["Lower score..." gnus-summary-lower-score t]))))
1907
1908     ;; Define both the Article menu in the summary buffer and the
1909     ;; equivalent Commands menu in the article buffer here for
1910     ;; consistency.
1911     (let ((innards
1912            `(("Hide"
1913               ["All" gnus-article-hide t]
1914               ["Headers" gnus-article-toggle-headers t]
1915               ["Signature" gnus-article-hide-signature t]
1916               ["Citation" gnus-article-hide-citation t]
1917               ["List identifiers" gnus-article-hide-list-identifiers t]
1918               ["PGP" gnus-article-hide-pgp t]
1919               ["Banner" gnus-article-strip-banner t]
1920               ["Boring headers" gnus-article-hide-boring-headers t])
1921              ("Highlight"
1922               ["All" gnus-article-highlight t]
1923               ["Headers" gnus-article-highlight-headers t]
1924               ["Signature" gnus-article-highlight-signature t]
1925               ["Citation" gnus-article-highlight-citation t])
1926              ("Date"
1927               ["Local" gnus-article-date-local t]
1928               ["ISO8601" gnus-article-date-iso8601 t]
1929               ["UT" gnus-article-date-ut t]
1930               ["Original" gnus-article-date-original t]
1931               ["Lapsed" gnus-article-date-lapsed t]
1932               ["User-defined" gnus-article-date-user t])
1933              ("Display"
1934               ["Remove images" gnus-article-remove-images t]
1935               ["Toggle smiley" gnus-treat-smiley t]
1936               ["Show X-Face" gnus-article-display-x-face t]
1937               ["Show picons in From" gnus-treat-from-picon t]
1938               ["Show picons in mail headers" gnus-treat-mail-picon t]
1939               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1940              ("Washing"
1941               ("Remove Blanks"
1942                ["Leading" gnus-article-strip-leading-blank-lines t]
1943                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1944                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1945                ["All of the above" gnus-article-strip-blank-lines t]
1946                ["All" gnus-article-strip-all-blank-lines t]
1947                ["Leading space" gnus-article-strip-leading-space t]
1948                ["Trailing space" gnus-article-strip-trailing-space t]
1949                ["Leading space in headers"
1950                 gnus-article-remove-leading-whitespace t])
1951               ["Overstrike" gnus-article-treat-overstrike t]
1952               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1953               ["Emphasis" gnus-article-emphasize t]
1954               ["Word wrap" gnus-article-fill-cited-article t]
1955               ["Fill long lines" gnus-article-fill-long-lines t]
1956               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1957               ["CR" gnus-article-remove-cr t]
1958               ["Rot 13" gnus-summary-caesar-message
1959                ,@(if (featurep 'xemacs) '(t)
1960                    '(:help "\"Caesar rotate\" article by 13"))]
1961               ["Unix pipe" gnus-summary-pipe-message t]
1962               ["Add buttons" gnus-article-add-buttons t]
1963               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1964               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1965               ["Toggle MIME" gnus-summary-toggle-mime t]
1966               ["Verbose header" gnus-summary-verbose-headers t]
1967               ["Toggle header" gnus-summary-toggle-header t]
1968               ["Unfold headers" gnus-article-treat-unfold-headers t]
1969               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1970               ["Html" gnus-article-wash-html t]
1971               ["URLs" gnus-article-unsplit-urls t]
1972               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1973               ["HZ" gnus-article-decode-HZ t])
1974              ("Output"
1975               ["Save in default format" gnus-summary-save-article
1976                ,@(if (featurep 'xemacs) '(t)
1977                    '(:help "Save article using default method"))]
1978               ["Save in file" gnus-summary-save-article-file
1979                ,@(if (featurep 'xemacs) '(t)
1980                    '(:help "Save article in file"))]
1981               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1982               ["Save in MH folder" gnus-summary-save-article-folder t]
1983               ["Save in VM folder" gnus-summary-save-article-vm t]
1984               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1985               ["Save body in file" gnus-summary-save-article-body-file t]
1986               ["Pipe through a filter" gnus-summary-pipe-output t]
1987               ["Add to SOUP packet" gnus-soup-add-article t]
1988               ["Print with Muttprint" gnus-summary-muttprint t]
1989               ["Print" gnus-summary-print-article t])
1990              ("Backend"
1991               ["Respool article..." gnus-summary-respool-article t]
1992               ["Move article..." gnus-summary-move-article
1993                (gnus-check-backend-function
1994                 'request-move-article gnus-newsgroup-name)]
1995               ["Copy article..." gnus-summary-copy-article t]
1996               ["Crosspost article..." gnus-summary-crosspost-article
1997                (gnus-check-backend-function
1998                 'request-replace-article gnus-newsgroup-name)]
1999               ["Import file..." gnus-summary-import-article t]
2000               ["Create article..." gnus-summary-create-article t]
2001               ["Check if posted" gnus-summary-article-posted-p t]
2002               ["Edit article" gnus-summary-edit-article
2003                (not (gnus-group-read-only-p))]
2004               ["Delete article" gnus-summary-delete-article
2005                (gnus-check-backend-function
2006                 'request-expire-articles gnus-newsgroup-name)]
2007               ["Query respool" gnus-summary-respool-query t]
2008               ["Trace respool" gnus-summary-respool-trace t]
2009               ["Delete expirable articles" gnus-summary-expire-articles-now
2010                (gnus-check-backend-function
2011                 'request-expire-articles gnus-newsgroup-name)])
2012              ("Extract"
2013               ["Uudecode" gnus-uu-decode-uu
2014                ,@(if (featurep 'xemacs) '(t)
2015                    '(:help "Decode uuencoded article(s)"))]
2016               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2017               ["Unshar" gnus-uu-decode-unshar t]
2018               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2019               ["Save" gnus-uu-decode-save t]
2020               ["Binhex" gnus-uu-decode-binhex t]
2021               ["Postscript" gnus-uu-decode-postscript t])
2022              ("Cache"
2023               ["Enter article" gnus-cache-enter-article t]
2024               ["Remove article" gnus-cache-remove-article t])
2025              ["Translate" gnus-article-babel t]
2026              ["Select article buffer" gnus-summary-select-article-buffer t]
2027              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2028              ["Isearch article..." gnus-summary-isearch-article t]
2029              ["Beginning of the article" gnus-summary-beginning-of-article t]
2030              ["End of the article" gnus-summary-end-of-article t]
2031              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2032              ["Fetch referenced articles" gnus-summary-refer-references t]
2033              ["Fetch current thread" gnus-summary-refer-thread t]
2034              ["Fetch article with id..." gnus-summary-refer-article t]
2035              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2036              ["Redisplay" gnus-summary-show-article t]
2037              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2038       (easy-menu-define
2039        gnus-summary-article-menu gnus-summary-mode-map ""
2040        (cons "Article" innards))
2041
2042       (if (not (keymapp gnus-summary-article-menu))
2043           (easy-menu-define
2044            gnus-article-commands-menu gnus-article-mode-map ""
2045            (cons "Commands" innards))
2046         ;; in Emacs, don't share menu.
2047         (setq gnus-article-commands-menu
2048               (copy-keymap gnus-summary-article-menu))
2049         (define-key gnus-article-mode-map [menu-bar commands]
2050           (cons "Commands" gnus-article-commands-menu))))
2051
2052     (easy-menu-define
2053      gnus-summary-thread-menu gnus-summary-mode-map ""
2054      '("Threads"
2055        ["Toggle threading" gnus-summary-toggle-threads t]
2056        ["Hide threads" gnus-summary-hide-all-threads t]
2057        ["Show threads" gnus-summary-show-all-threads t]
2058        ["Hide thread" gnus-summary-hide-thread t]
2059        ["Show thread" gnus-summary-show-thread t]
2060        ["Go to next thread" gnus-summary-next-thread t]
2061        ["Go to previous thread" gnus-summary-prev-thread t]
2062        ["Go down thread" gnus-summary-down-thread t]
2063        ["Go up thread" gnus-summary-up-thread t]
2064        ["Top of thread" gnus-summary-top-thread t]
2065        ["Mark thread as read" gnus-summary-kill-thread t]
2066        ["Lower thread score" gnus-summary-lower-thread t]
2067        ["Raise thread score" gnus-summary-raise-thread t]
2068        ["Rethread current" gnus-summary-rethread-current t]))
2069
2070     (easy-menu-define
2071      gnus-summary-post-menu gnus-summary-mode-map ""
2072      `("Post"
2073        ["Send a message (mail or news)" gnus-summary-post-news
2074         ,@(if (featurep 'xemacs) '(t)
2075             '(:help "Post an article"))]
2076        ["Followup" gnus-summary-followup
2077         ,@(if (featurep 'xemacs) '(t)
2078             '(:help "Post followup to this article"))]
2079        ["Followup and yank" gnus-summary-followup-with-original
2080         ,@(if (featurep 'xemacs) '(t)
2081             '(:help "Post followup to this article, quoting its contents"))]
2082        ["Supersede article" gnus-summary-supersede-article t]
2083        ["Cancel article" gnus-summary-cancel-article
2084         ,@(if (featurep 'xemacs) '(t)
2085             '(:help "Cancel an article you posted"))]
2086        ["Reply" gnus-summary-reply t]
2087        ["Reply and yank" gnus-summary-reply-with-original t]
2088        ["Wide reply" gnus-summary-wide-reply t]
2089        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2090         ,@(if (featurep 'xemacs) '(t)
2091             '(:help "Mail a reply, quoting this article"))]
2092        ["Very wide reply" gnus-summary-very-wide-reply t]
2093        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2094         ,@(if (featurep 'xemacs) '(t)
2095             '(:help "Mail a very wide reply, quoting this article"))]
2096        ["Mail forward" gnus-summary-mail-forward t]
2097        ["Post forward" gnus-summary-post-forward t]
2098        ["Digest and mail" gnus-summary-digest-mail-forward t]
2099        ["Digest and post" gnus-summary-digest-post-forward t]
2100        ["Resend message" gnus-summary-resend-message t]
2101        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2102        ["Send a mail" gnus-summary-mail-other-window t]
2103        ["Create a local message" gnus-summary-news-other-window t]
2104        ["Uuencode and post" gnus-uu-post-news
2105         ,@(if (featurep 'xemacs) '(t)
2106             '(:help "Post a uuencoded article"))]
2107        ["Followup via news" gnus-summary-followup-to-mail t]
2108        ["Followup via news and yank"
2109         gnus-summary-followup-to-mail-with-original t]
2110        ;;("Draft"
2111        ;;["Send" gnus-summary-send-draft t]
2112        ;;["Send bounced" gnus-resend-bounced-mail t])
2113        ))
2114
2115     (cond
2116      ((not (keymapp gnus-summary-post-menu))
2117       (setq gnus-article-post-menu gnus-summary-post-menu))
2118      ((not gnus-article-post-menu)
2119       ;; Don't share post menu.
2120       (setq gnus-article-post-menu
2121             (copy-keymap gnus-summary-post-menu))))
2122     (define-key gnus-article-mode-map [menu-bar post]
2123       (cons "Post" gnus-article-post-menu))
2124
2125     (easy-menu-define
2126      gnus-summary-misc-menu gnus-summary-mode-map ""
2127      `("Gnus"
2128        ("Mark Read"
2129         ["Mark as read" gnus-summary-mark-as-read-forward t]
2130         ["Mark same subject and select"
2131          gnus-summary-kill-same-subject-and-select t]
2132         ["Mark same subject" gnus-summary-kill-same-subject t]
2133         ["Catchup" gnus-summary-catchup
2134          ,@(if (featurep 'xemacs) '(t)
2135              '(:help "Mark unread articles in this group as read"))]
2136         ["Catchup all" gnus-summary-catchup-all t]
2137         ["Catchup to here" gnus-summary-catchup-to-here t]
2138         ["Catchup from here" gnus-summary-catchup-from-here t]
2139         ["Catchup region" gnus-summary-mark-region-as-read t]
2140         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2141        ("Mark Various"
2142         ["Tick" gnus-summary-tick-article-forward t]
2143         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2144         ["Remove marks" gnus-summary-clear-mark-forward t]
2145         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2146         ["Set bookmark" gnus-summary-set-bookmark t]
2147         ["Remove bookmark" gnus-summary-remove-bookmark t])
2148        ("Limit to"
2149         ["Marks..." gnus-summary-limit-to-marks t]
2150         ["Subject..." gnus-summary-limit-to-subject t]
2151         ["Author..." gnus-summary-limit-to-author t]
2152         ["Age..." gnus-summary-limit-to-age t]
2153         ["Extra..." gnus-summary-limit-to-extra t]
2154         ["Score" gnus-summary-limit-to-score t]
2155         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2156         ["Unread" gnus-summary-limit-to-unread t]
2157         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2158         ["Articles" gnus-summary-limit-to-articles t]
2159         ["Pop limit" gnus-summary-pop-limit t]
2160         ["Show dormant" gnus-summary-limit-include-dormant t]
2161         ["Hide childless dormant"
2162          gnus-summary-limit-exclude-childless-dormant t]
2163         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2164         ["Hide marked" gnus-summary-limit-exclude-marks t]
2165         ["Show expunged" gnus-summary-limit-include-expunged t])
2166        ("Process Mark"
2167         ["Set mark" gnus-summary-mark-as-processable t]
2168         ["Remove mark" gnus-summary-unmark-as-processable t]
2169         ["Remove all marks" gnus-summary-unmark-all-processable t]
2170         ["Mark above" gnus-uu-mark-over t]
2171         ["Mark series" gnus-uu-mark-series t]
2172         ["Mark region" gnus-uu-mark-region t]
2173         ["Unmark region" gnus-uu-unmark-region t]
2174         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2175         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2176         ["Mark all" gnus-uu-mark-all t]
2177         ["Mark buffer" gnus-uu-mark-buffer t]
2178         ["Mark sparse" gnus-uu-mark-sparse t]
2179         ["Mark thread" gnus-uu-mark-thread t]
2180         ["Unmark thread" gnus-uu-unmark-thread t]
2181         ("Process Mark Sets"
2182          ["Kill" gnus-summary-kill-process-mark t]
2183          ["Yank" gnus-summary-yank-process-mark
2184           gnus-newsgroup-process-stack]
2185          ["Save" gnus-summary-save-process-mark t]))
2186        ("Scroll article"
2187         ["Page forward" gnus-summary-next-page
2188          ,@(if (featurep 'xemacs) '(t)
2189              '(:help "Show next page of article"))]
2190         ["Page backward" gnus-summary-prev-page
2191          ,@(if (featurep 'xemacs) '(t)
2192              '(:help "Show previous page of article"))]
2193         ["Line forward" gnus-summary-scroll-up t])
2194        ("Move"
2195         ["Next unread article" gnus-summary-next-unread-article t]
2196         ["Previous unread article" gnus-summary-prev-unread-article t]
2197         ["Next article" gnus-summary-next-article t]
2198         ["Previous article" gnus-summary-prev-article t]
2199         ["Next unread subject" gnus-summary-next-unread-subject t]
2200         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2201         ["Next article same subject" gnus-summary-next-same-subject t]
2202         ["Previous article same subject" gnus-summary-prev-same-subject t]
2203         ["First unread article" gnus-summary-first-unread-article t]
2204         ["Best unread article" gnus-summary-best-unread-article t]
2205         ["Go to subject number..." gnus-summary-goto-subject t]
2206         ["Go to article number..." gnus-summary-goto-article t]
2207         ["Go to the last article" gnus-summary-goto-last-article t]
2208         ["Pop article off history" gnus-summary-pop-article t])
2209        ("Sort"
2210         ["Sort by number" gnus-summary-sort-by-number t]
2211         ["Sort by author" gnus-summary-sort-by-author t]
2212         ["Sort by subject" gnus-summary-sort-by-subject t]
2213         ["Sort by date" gnus-summary-sort-by-date t]
2214         ["Sort by score" gnus-summary-sort-by-score t]
2215         ["Sort by lines" gnus-summary-sort-by-lines t]
2216         ["Sort by characters" gnus-summary-sort-by-chars t]
2217         ["Original sort" gnus-summary-sort-by-original t])
2218        ("Help"
2219         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2220         ["Describe group" gnus-summary-describe-group t]
2221         ["Read manual" gnus-info-find-node t])
2222        ("Modes"
2223         ["Pick and read" gnus-pick-mode t]
2224         ["Binary" gnus-binary-mode t])
2225        ("Regeneration"
2226         ["Regenerate" gnus-summary-prepare t]
2227         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2228         ["Toggle threading" gnus-summary-toggle-threads t])
2229        ["See old articles" gnus-summary-insert-old-articles t]
2230        ["See new articles" gnus-summary-insert-new-articles t]
2231        ["Filter articles..." gnus-summary-execute-command t]
2232        ["Run command on subjects..." gnus-summary-universal-argument t]
2233        ["Search articles forward..." gnus-summary-search-article-forward t]
2234        ["Search articles backward..." gnus-summary-search-article-backward t]
2235        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2236        ["Expand window" gnus-summary-expand-window t]
2237        ["Expire expirable articles" gnus-summary-expire-articles
2238         (gnus-check-backend-function
2239          'request-expire-articles gnus-newsgroup-name)]
2240        ["Edit local kill file" gnus-summary-edit-local-kill t]
2241        ["Edit main kill file" gnus-summary-edit-global-kill t]
2242        ["Edit group parameters" gnus-summary-edit-parameters t]
2243        ["Customize group parameters" gnus-summary-customize-parameters t]
2244        ["Send a bug report" gnus-bug t]
2245        ("Exit"
2246         ["Catchup and exit" gnus-summary-catchup-and-exit
2247          ,@(if (featurep 'xemacs) '(t)
2248              '(:help "Mark unread articles in this group as read, then exit"))]
2249         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2250         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2251         ["Exit group" gnus-summary-exit
2252          ,@(if (featurep 'xemacs) '(t)
2253              '(:help "Exit current group, return to group selection mode"))]
2254         ["Exit group without updating" gnus-summary-exit-no-update t]
2255         ["Exit and goto next group" gnus-summary-next-group t]
2256         ["Exit and goto prev group" gnus-summary-prev-group t]
2257         ["Reselect group" gnus-summary-reselect-current-group t]
2258         ["Rescan group" gnus-summary-rescan-group t]
2259         ["Update dribble" gnus-summary-save-newsrc t])))
2260
2261     (gnus-run-hooks 'gnus-summary-menu-hook)))
2262
2263 (defvar gnus-summary-tool-bar-map nil)
2264
2265 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2266 (defun gnus-summary-make-tool-bar ()
2267   (if (and (fboundp 'tool-bar-add-item-from-menu)
2268            (default-value 'tool-bar-mode)
2269            (not gnus-summary-tool-bar-map))
2270       (setq gnus-summary-tool-bar-map
2271             (let ((tool-bar-map (make-sparse-keymap))
2272                   (load-path (mm-image-load-path)))
2273               (tool-bar-add-item-from-menu
2274                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2275               (tool-bar-add-item-from-menu
2276                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2277               (tool-bar-add-item-from-menu
2278                'gnus-summary-post-news "post" gnus-summary-mode-map)
2279               (tool-bar-add-item-from-menu
2280                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2281               (tool-bar-add-item-from-menu
2282                'gnus-summary-followup "followup" gnus-summary-mode-map)
2283               (tool-bar-add-item-from-menu
2284                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2285               (tool-bar-add-item-from-menu
2286                'gnus-summary-reply "reply" gnus-summary-mode-map)
2287               (tool-bar-add-item-from-menu
2288                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2289               (tool-bar-add-item-from-menu
2290                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2291               (tool-bar-add-item-from-menu
2292                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2293               (tool-bar-add-item-from-menu
2294                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2295               (tool-bar-add-item-from-menu
2296                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2297               (tool-bar-add-item-from-menu
2298                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2299               (tool-bar-add-item-from-menu
2300                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2301               (tool-bar-add-item-from-menu
2302                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2303               tool-bar-map)))
2304   (if gnus-summary-tool-bar-map
2305       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2306
2307 (defun gnus-score-set-default (var value)
2308   "A version of set that updates the GNU Emacs menu-bar."
2309   (set var value)
2310   ;; It is the message that forces the active status to be updated.
2311   (message ""))
2312
2313 (defun gnus-make-score-map (type)
2314   "Make a summary score map of type TYPE."
2315   (if t
2316       nil
2317     (let ((headers '(("author" "from" string)
2318                      ("subject" "subject" string)
2319                      ("article body" "body" string)
2320                      ("article head" "head" string)
2321                      ("xref" "xref" string)
2322                      ("extra header" "extra" string)
2323                      ("lines" "lines" number)
2324                      ("followups to author" "followup" string)))
2325           (types '((number ("less than" <)
2326                            ("greater than" >)
2327                            ("equal" =))
2328                    (string ("substring" s)
2329                            ("exact string" e)
2330                            ("fuzzy string" f)
2331                            ("regexp" r))))
2332           (perms '(("temporary" (current-time-string))
2333                    ("permanent" nil)
2334                    ("immediate" now)))
2335           header)
2336       (list
2337        (apply
2338         'nconc
2339         (list
2340          (if (eq type 'lower)
2341              "Lower score"
2342            "Increase score"))
2343         (let (outh)
2344           (while headers
2345             (setq header (car headers))
2346             (setq outh
2347                   (cons
2348                    (apply
2349                     'nconc
2350                     (list (car header))
2351                     (let ((ts (cdr (assoc (nth 2 header) types)))
2352                           outt)
2353                       (while ts
2354                         (setq outt
2355                               (cons
2356                                (apply
2357                                 'nconc
2358                                 (list (caar ts))
2359                                 (let ((ps perms)
2360                                       outp)
2361                                   (while ps
2362                                     (setq outp
2363                                           (cons
2364                                            (vector
2365                                             (caar ps)
2366                                             (list
2367                                              'gnus-summary-score-entry
2368                                              (nth 1 header)
2369                                              (if (or (string= (nth 1 header)
2370                                                               "head")
2371                                                      (string= (nth 1 header)
2372                                                               "body"))
2373                                                  ""
2374                                                (list 'gnus-summary-header
2375                                                      (nth 1 header)))
2376                                              (list 'quote (nth 1 (car ts)))
2377                                              (list 'gnus-score-delta-default
2378                                                    nil)
2379                                              (nth 1 (car ps))
2380                                              t)
2381                                             t)
2382                                            outp))
2383                                     (setq ps (cdr ps)))
2384                                   (list (nreverse outp))))
2385                                outt))
2386                         (setq ts (cdr ts)))
2387                       (list (nreverse outt))))
2388                    outh))
2389             (setq headers (cdr headers)))
2390           (list (nreverse outh))))))))
2391
2392 \f
2393
2394 (defun gnus-summary-mode (&optional group)
2395   "Major mode for reading articles.
2396
2397 All normal editing commands are switched off.
2398 \\<gnus-summary-mode-map>
2399 Each line in this buffer represents one article.  To read an
2400 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2401 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2402 respectively.
2403
2404 You can also post articles and send mail from this buffer.  To
2405 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2406 of an article, type `\\[gnus-summary-reply]'.
2407
2408 There are approx. one gazillion commands you can execute in this
2409 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2410
2411 The following commands are available:
2412
2413 \\{gnus-summary-mode-map}"
2414   (interactive)
2415   (kill-all-local-variables)
2416   (when (gnus-visual-p 'summary-menu 'menu)
2417     (gnus-summary-make-menu-bar)
2418     (gnus-summary-make-tool-bar))
2419   (gnus-summary-make-local-variables)
2420   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2421     (gnus-summary-make-local-variables))
2422   (gnus-make-thread-indent-array)
2423   (gnus-simplify-mode-line)
2424   (setq major-mode 'gnus-summary-mode)
2425   (setq mode-name "Summary")
2426   (make-local-variable 'minor-mode-alist)
2427   (use-local-map gnus-summary-mode-map)
2428   (buffer-disable-undo)
2429   (setq buffer-read-only t)             ;Disable modification
2430   (setq truncate-lines t)
2431   (setq selective-display t)
2432   (setq selective-display-ellipses t)   ;Display `...'
2433   (gnus-summary-set-display-table)
2434   (gnus-set-default-directory)
2435   (setq gnus-newsgroup-name group)
2436   (unless (gnus-news-group-p group)
2437     (setq gnus-newsgroup-incorporated
2438           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2439   (make-local-variable 'gnus-summary-line-format)
2440   (make-local-variable 'gnus-summary-line-format-spec)
2441   (make-local-variable 'gnus-summary-dummy-line-format)
2442   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2443   (make-local-variable 'gnus-summary-mark-positions)
2444   (make-local-hook 'pre-command-hook)
2445   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2446   (gnus-run-hooks 'gnus-summary-mode-hook)
2447   (turn-on-gnus-mailing-list-mode)
2448   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2449   (gnus-update-summary-mark-positions))
2450
2451 (defun gnus-summary-make-local-variables ()
2452   "Make all the local summary buffer variables."
2453   (let (global)
2454     (dolist (local gnus-summary-local-variables)
2455       (if (consp local)
2456           (progn
2457             (if (eq (cdr local) 'global)
2458                 ;; Copy the global value of the variable.
2459                 (setq global (symbol-value (car local)))
2460               ;; Use the value from the list.
2461               (setq global (eval (cdr local))))
2462             (set (make-local-variable (car local)) global))
2463         ;; Simple nil-valued local variable.
2464         (set (make-local-variable local) nil)))))
2465
2466 (defun gnus-summary-clear-local-variables ()
2467   (let ((locals gnus-summary-local-variables))
2468     (while locals
2469       (if (consp (car locals))
2470           (and (vectorp (caar locals))
2471                (set (caar locals) nil))
2472         (and (vectorp (car locals))
2473              (set (car locals) nil)))
2474       (setq locals (cdr locals)))))
2475
2476 ;; Summary data functions.
2477
2478 (defmacro gnus-data-number (data)
2479   `(car ,data))
2480
2481 (defmacro gnus-data-set-number (data number)
2482   `(setcar ,data ,number))
2483
2484 (defmacro gnus-data-mark (data)
2485   `(nth 1 ,data))
2486
2487 (defmacro gnus-data-set-mark (data mark)
2488   `(setcar (nthcdr 1 ,data) ,mark))
2489
2490 (defmacro gnus-data-pos (data)
2491   `(nth 2 ,data))
2492
2493 (defmacro gnus-data-set-pos (data pos)
2494   `(setcar (nthcdr 2 ,data) ,pos))
2495
2496 (defmacro gnus-data-header (data)
2497   `(nth 3 ,data))
2498
2499 (defmacro gnus-data-set-header (data header)
2500   `(setcar (nthcdr 3 ,data) ,header))
2501
2502 (defmacro gnus-data-level (data)
2503   `(nth 4 ,data))
2504
2505 (defmacro gnus-data-unread-p (data)
2506   `(= (nth 1 ,data) gnus-unread-mark))
2507
2508 (defmacro gnus-data-read-p (data)
2509   `(/= (nth 1 ,data) gnus-unread-mark))
2510
2511 (defmacro gnus-data-pseudo-p (data)
2512   `(consp (nth 3 ,data)))
2513
2514 (defmacro gnus-data-find (number)
2515   `(assq ,number gnus-newsgroup-data))
2516
2517 (defmacro gnus-data-find-list (number &optional data)
2518   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2519      (memq (assq ,number bdata)
2520            bdata)))
2521
2522 (defmacro gnus-data-make (number mark pos header level)
2523   `(list ,number ,mark ,pos ,header ,level))
2524
2525 (defun gnus-data-enter (after-article number mark pos header level offset)
2526   (let ((data (gnus-data-find-list after-article)))
2527     (unless data
2528       (error "No such article: %d" after-article))
2529     (setcdr data (cons (gnus-data-make number mark pos header level)
2530                        (cdr data)))
2531     (setq gnus-newsgroup-data-reverse nil)
2532     (gnus-data-update-list (cddr data) offset)))
2533
2534 (defun gnus-data-enter-list (after-article list &optional offset)
2535   (when list
2536     (let ((data (and after-article (gnus-data-find-list after-article)))
2537           (ilist list))
2538       (if (not (or data
2539                    after-article))
2540           (let ((odata gnus-newsgroup-data))
2541             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2542             (when offset
2543               (gnus-data-update-list odata offset)))
2544         ;; Find the last element in the list to be spliced into the main
2545         ;; list.
2546         (while (cdr list)
2547           (setq list (cdr list)))
2548         (if (not data)
2549             (progn
2550               (setcdr list gnus-newsgroup-data)
2551               (setq gnus-newsgroup-data ilist)
2552               (when offset
2553                 (gnus-data-update-list (cdr list) offset)))
2554           (setcdr list (cdr data))
2555           (setcdr data ilist)
2556           (when offset
2557             (gnus-data-update-list (cdr list) offset))))
2558       (setq gnus-newsgroup-data-reverse nil))))
2559
2560 (defun gnus-data-remove (article &optional offset)
2561   (let ((data gnus-newsgroup-data))
2562     (if (= (gnus-data-number (car data)) article)
2563         (progn
2564           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2565                 gnus-newsgroup-data-reverse nil)
2566           (when offset
2567             (gnus-data-update-list gnus-newsgroup-data offset)))
2568       (while (cdr data)
2569         (when (= (gnus-data-number (cadr data)) article)
2570           (setcdr data (cddr data))
2571           (when offset
2572             (gnus-data-update-list (cdr data) offset))
2573           (setq data nil
2574                 gnus-newsgroup-data-reverse nil))
2575         (setq data (cdr data))))))
2576
2577 (defmacro gnus-data-list (backward)
2578   `(if ,backward
2579        (or gnus-newsgroup-data-reverse
2580            (setq gnus-newsgroup-data-reverse
2581                  (reverse gnus-newsgroup-data)))
2582      gnus-newsgroup-data))
2583
2584 (defun gnus-data-update-list (data offset)
2585   "Add OFFSET to the POS of all data entries in DATA."
2586   (setq gnus-newsgroup-data-reverse nil)
2587   (while data
2588     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2589     (setq data (cdr data))))
2590
2591 (defun gnus-summary-article-pseudo-p (article)
2592   "Say whether this article is a pseudo article or not."
2593   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2594
2595 (defmacro gnus-summary-article-sparse-p (article)
2596   "Say whether this article is a sparse article or not."
2597   `(memq ,article gnus-newsgroup-sparse))
2598
2599 (defmacro gnus-summary-article-ancient-p (article)
2600   "Say whether this article is a sparse article or not."
2601   `(memq ,article gnus-newsgroup-ancient))
2602
2603 (defun gnus-article-parent-p (number)
2604   "Say whether this article is a parent or not."
2605   (let ((data (gnus-data-find-list number)))
2606     (and (cdr data)                     ; There has to be an article after...
2607          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2608             (gnus-data-level (nth 1 data))))))
2609
2610 (defun gnus-article-children (number)
2611   "Return a list of all children to NUMBER."
2612   (let* ((data (gnus-data-find-list number))
2613          (level (gnus-data-level (car data)))
2614          children)
2615     (setq data (cdr data))
2616     (while (and data
2617                 (= (gnus-data-level (car data)) (1+ level)))
2618       (push (gnus-data-number (car data)) children)
2619       (setq data (cdr data)))
2620     children))
2621
2622 (defmacro gnus-summary-skip-intangible ()
2623   "If the current article is intangible, then jump to a different article."
2624   '(let ((to (get-text-property (point) 'gnus-intangible)))
2625      (and to (gnus-summary-goto-subject to))))
2626
2627 (defmacro gnus-summary-article-intangible-p ()
2628   "Say whether this article is intangible or not."
2629   '(get-text-property (point) 'gnus-intangible))
2630
2631 (defun gnus-article-read-p (article)
2632   "Say whether ARTICLE is read or not."
2633   (not (or (memq article gnus-newsgroup-marked)
2634            (memq article gnus-newsgroup-unreads)
2635            (memq article gnus-newsgroup-unselected)
2636            (memq article gnus-newsgroup-dormant))))
2637
2638 ;; Some summary mode macros.
2639
2640 (defmacro gnus-summary-article-number ()
2641   "The article number of the article on the current line.
2642 If there isn's an article number here, then we return the current
2643 article number."
2644   '(progn
2645      (gnus-summary-skip-intangible)
2646      (or (get-text-property (point) 'gnus-number)
2647          (gnus-summary-last-subject))))
2648
2649 (defmacro gnus-summary-article-header (&optional number)
2650   "Return the header of article NUMBER."
2651   `(gnus-data-header (gnus-data-find
2652                       ,(or number '(gnus-summary-article-number)))))
2653
2654 (defmacro gnus-summary-thread-level (&optional number)
2655   "Return the level of thread that starts with article NUMBER."
2656   `(if (and (eq gnus-summary-make-false-root 'dummy)
2657             (get-text-property (point) 'gnus-intangible))
2658        0
2659      (gnus-data-level (gnus-data-find
2660                        ,(or number '(gnus-summary-article-number))))))
2661
2662 (defmacro gnus-summary-article-mark (&optional number)
2663   "Return the mark of article NUMBER."
2664   `(gnus-data-mark (gnus-data-find
2665                     ,(or number '(gnus-summary-article-number)))))
2666
2667 (defmacro gnus-summary-article-pos (&optional number)
2668   "Return the position of the line of article NUMBER."
2669   `(gnus-data-pos (gnus-data-find
2670                    ,(or number '(gnus-summary-article-number)))))
2671
2672 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2673 (defmacro gnus-summary-article-subject (&optional number)
2674   "Return current subject string or nil if nothing."
2675   `(let ((headers
2676           ,(if number
2677                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2678              '(gnus-data-header (assq (gnus-summary-article-number)
2679                                       gnus-newsgroup-data)))))
2680      (and headers
2681           (vectorp headers)
2682           (mail-header-subject headers))))
2683
2684 (defmacro gnus-summary-article-score (&optional number)
2685   "Return current article score."
2686   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2687                   gnus-newsgroup-scored))
2688        gnus-summary-default-score 0))
2689
2690 (defun gnus-summary-article-children (&optional number)
2691   "Return a list of article numbers that are children of article NUMBER."
2692   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2693          (level (gnus-data-level (car data)))
2694          l children)
2695     (while (and (setq data (cdr data))
2696                 (> (setq l (gnus-data-level (car data))) level))
2697       (and (= (1+ level) l)
2698            (push (gnus-data-number (car data))
2699                  children)))
2700     (nreverse children)))
2701
2702 (defun gnus-summary-article-parent (&optional number)
2703   "Return the article number of the parent of article NUMBER."
2704   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2705                                     (gnus-data-list t)))
2706          (level (gnus-data-level (car data))))
2707     (if (zerop level)
2708         ()                              ; This is a root.
2709       ;; We search until we find an article with a level less than
2710       ;; this one.  That function has to be the parent.
2711       (while (and (setq data (cdr data))
2712                   (not (< (gnus-data-level (car data)) level))))
2713       (and data (gnus-data-number (car data))))))
2714
2715 (defun gnus-unread-mark-p (mark)
2716   "Say whether MARK is the unread mark."
2717   (= mark gnus-unread-mark))
2718
2719 (defun gnus-read-mark-p (mark)
2720   "Say whether MARK is one of the marks that mark as read.
2721 This is all marks except unread, ticked, dormant, and expirable."
2722   (not (or (= mark gnus-unread-mark)
2723            (= mark gnus-ticked-mark)
2724            (= mark gnus-dormant-mark)
2725            (= mark gnus-expirable-mark))))
2726
2727 (defmacro gnus-article-mark (number)
2728   "Return the MARK of article NUMBER.
2729 This macro should only be used when computing the mark the \"first\"
2730 time; i.e., when generating the summary lines.  After that,
2731 `gnus-summary-article-mark' should be used to examine the
2732 marks of articles."
2733   `(cond
2734     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2735     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2736     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2737     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2738     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2739     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2740     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2741     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2742            gnus-ancient-mark))))
2743
2744 ;; Saving hidden threads.
2745
2746 (defmacro gnus-save-hidden-threads (&rest forms)
2747   "Save hidden threads, eval FORMS, and restore the hidden threads."
2748   (let ((config (make-symbol "config")))
2749     `(let ((,config (gnus-hidden-threads-configuration)))
2750        (unwind-protect
2751            (save-excursion
2752              ,@forms)
2753          (gnus-restore-hidden-threads-configuration ,config)))))
2754 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2755 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2756
2757 (defun gnus-data-compute-positions ()
2758   "Compute the positions of all articles."
2759   (setq gnus-newsgroup-data-reverse nil)
2760   (let ((data gnus-newsgroup-data))
2761     (save-excursion
2762       (gnus-save-hidden-threads
2763         (gnus-summary-show-all-threads)
2764         (goto-char (point-min))
2765         (while data
2766           (while (get-text-property (point) 'gnus-intangible)
2767             (forward-line 1))
2768           (gnus-data-set-pos (car data) (+ (point) 3))
2769           (setq data (cdr data))
2770           (forward-line 1))))))
2771
2772 (defun gnus-hidden-threads-configuration ()
2773   "Return the current hidden threads configuration."
2774   (save-excursion
2775     (let (config)
2776       (goto-char (point-min))
2777       (while (search-forward "\r" nil t)
2778         (push (1- (point)) config))
2779       config)))
2780
2781 (defun gnus-restore-hidden-threads-configuration (config)
2782   "Restore hidden threads configuration from CONFIG."
2783   (save-excursion
2784     (let (point buffer-read-only)
2785       (while (setq point (pop config))
2786         (when (and (< point (point-max))
2787                    (goto-char point)
2788                    (eq (char-after) ?\n))
2789           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2790
2791 ;; Various summary mode internalish functions.
2792
2793 (defun gnus-mouse-pick-article (e)
2794   (interactive "e")
2795   (mouse-set-point e)
2796   (gnus-summary-next-page nil t))
2797
2798 (defun gnus-summary-set-display-table ()
2799   "Change the display table.
2800 Odd characters have a tendency to mess
2801 up nicely formatted displays - we make all possible glyphs
2802 display only a single character."
2803
2804   ;; We start from the standard display table, if any.
2805   (let ((table (or (copy-sequence standard-display-table)
2806                    (make-display-table)))
2807         (i 32))
2808     ;; Nix out all the control chars...
2809     (while (>= (setq i (1- i)) 0)
2810       (aset table i [??]))
2811     ;; ... but not newline and cr, of course.  (cr is necessary for the
2812     ;; selective display).
2813     (aset table ?\n nil)
2814     (aset table ?\r nil)
2815     ;; We keep TAB as well.
2816     (aset table ?\t nil)
2817     ;; We nix out any glyphs over 126 that are not set already.
2818     (let ((i 256))
2819       (while (>= (setq i (1- i)) 127)
2820         ;; Only modify if the entry is nil.
2821         (unless (aref table i)
2822           (aset table i [??]))))
2823     (setq buffer-display-table table)))
2824
2825 (defun gnus-summary-set-article-display-arrow (pos)
2826   "Update the overlay arrow to point to line at position POS."
2827   (when (and gnus-summary-display-arrow
2828              (boundp 'overlay-arrow-position)
2829              (boundp 'overlay-arrow-string))
2830     (save-excursion
2831       (goto-char pos)
2832       (beginning-of-line)
2833       (unless overlay-arrow-position
2834         (setq overlay-arrow-position (make-marker)))
2835       (setq overlay-arrow-string "=>"
2836             overlay-arrow-position (set-marker overlay-arrow-position
2837                                                (point)
2838                                                (current-buffer))))))
2839
2840 (defun gnus-summary-buffer-name (group)
2841   "Return the summary buffer name of GROUP."
2842   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2843
2844 (defun gnus-summary-setup-buffer (group)
2845   "Initialize summary buffer."
2846   (let ((buffer (gnus-summary-buffer-name group))
2847         (dead-name (concat "*Dead Summary "
2848                            (gnus-group-decoded-name group) "*")))
2849     ;; If a dead summary buffer exists, we kill it.
2850     (when (gnus-buffer-live-p dead-name)
2851       (gnus-kill-buffer dead-name))
2852     (if (get-buffer buffer)
2853         (progn
2854           (set-buffer buffer)
2855           (setq gnus-summary-buffer (current-buffer))
2856           (not gnus-newsgroup-prepared))
2857       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2858       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2859       (gnus-summary-mode group)
2860       (when gnus-carpal
2861         (gnus-carpal-setup-buffer 'summary))
2862       (unless gnus-single-article-buffer
2863         (make-local-variable 'gnus-article-buffer)
2864         (make-local-variable 'gnus-article-current)
2865         (make-local-variable 'gnus-original-article-buffer))
2866       (setq gnus-newsgroup-name group)
2867       ;; Set any local variables in the group parameters.
2868       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2869       t)))
2870
2871 (defun gnus-set-global-variables ()
2872   "Set the global equivalents of the buffer-local variables.
2873 They are set to the latest values they had.  These reflect the summary
2874 buffer that was in action when the last article was fetched."
2875   (when (eq major-mode 'gnus-summary-mode)
2876     (setq gnus-summary-buffer (current-buffer))
2877     (let ((name gnus-newsgroup-name)
2878           (marked gnus-newsgroup-marked)
2879           (unread gnus-newsgroup-unreads)
2880           (headers gnus-current-headers)
2881           (data gnus-newsgroup-data)
2882           (summary gnus-summary-buffer)
2883           (article-buffer gnus-article-buffer)
2884           (original gnus-original-article-buffer)
2885           (gac gnus-article-current)
2886           (reffed gnus-reffed-article-number)
2887           (score-file gnus-current-score-file)
2888           (default-charset gnus-newsgroup-charset)
2889           vlist)
2890       (let ((locals gnus-newsgroup-variables))
2891         (while locals
2892           (if (consp (car locals))
2893               (push (eval (caar locals)) vlist)
2894             (push (eval (car locals)) vlist))
2895           (setq locals (cdr locals)))
2896         (setq vlist (nreverse vlist)))
2897       (save-excursion
2898         (set-buffer gnus-group-buffer)
2899         (setq gnus-newsgroup-name name
2900               gnus-newsgroup-marked marked
2901               gnus-newsgroup-unreads unread
2902               gnus-current-headers headers
2903               gnus-newsgroup-data data
2904               gnus-article-current gac
2905               gnus-summary-buffer summary
2906               gnus-article-buffer article-buffer
2907               gnus-original-article-buffer original
2908               gnus-reffed-article-number reffed
2909               gnus-current-score-file score-file
2910               gnus-newsgroup-charset default-charset)
2911         (let ((locals gnus-newsgroup-variables))
2912           (while locals
2913             (if (consp (car locals))
2914                 (set (caar locals) (pop vlist))
2915               (set (car locals) (pop vlist)))
2916             (setq locals (cdr locals))))
2917         ;; The article buffer also has local variables.
2918         (when (gnus-buffer-live-p gnus-article-buffer)
2919           (set-buffer gnus-article-buffer)
2920           (setq gnus-summary-buffer summary))))))
2921
2922 (defun gnus-summary-article-unread-p (article)
2923   "Say whether ARTICLE is unread or not."
2924   (memq article gnus-newsgroup-unreads))
2925
2926 (defun gnus-summary-first-article-p (&optional article)
2927   "Return whether ARTICLE is the first article in the buffer."
2928   (if (not (setq article (or article (gnus-summary-article-number))))
2929       nil
2930     (eq article (caar gnus-newsgroup-data))))
2931
2932 (defun gnus-summary-last-article-p (&optional article)
2933   "Return whether ARTICLE is the last article in the buffer."
2934   (if (not (setq article (or article (gnus-summary-article-number))))
2935       ;; All non-existent numbers are the last article.  :-)
2936       t
2937     (not (cdr (gnus-data-find-list article)))))
2938
2939 (defun gnus-make-thread-indent-array ()
2940   (let ((n 200))
2941     (unless (and gnus-thread-indent-array
2942                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2943       (setq gnus-thread-indent-array (make-vector 201 "")
2944             gnus-thread-indent-array-level gnus-thread-indent-level)
2945       (while (>= n 0)
2946         (aset gnus-thread-indent-array n
2947               (make-string (* n gnus-thread-indent-level) ? ))
2948         (setq n (1- n))))))
2949
2950 (defun gnus-update-summary-mark-positions ()
2951   "Compute where the summary marks are to go."
2952   (save-excursion
2953     (when (gnus-buffer-exists-p gnus-summary-buffer)
2954       (set-buffer gnus-summary-buffer))
2955     (let ((gnus-replied-mark 129)
2956           (gnus-score-below-mark 130)
2957           (gnus-score-over-mark 130)
2958           (gnus-download-mark 131)
2959           (spec gnus-summary-line-format-spec)
2960           gnus-visual pos)
2961       (save-excursion
2962         (gnus-set-work-buffer)
2963         (let ((gnus-summary-line-format-spec spec)
2964               (gnus-newsgroup-downloadable '((0 . t))))
2965           (gnus-summary-insert-line
2966            (make-full-mail-header 0 "" "nobody"
2967                                   "05 Apr 2001 23:33:09 +0400"
2968                                   "" "" 0 0 "" nil)
2969            0 nil 128 t nil "" nil 1)
2970           (goto-char (point-min))
2971           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2972                                              (- (point) 2)))))
2973           (goto-char (point-min))
2974           (push (cons 'replied (and (search-forward "\201" nil t)
2975                                     (- (point) 2)))
2976                 pos)
2977           (goto-char (point-min))
2978           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2979                 pos)
2980           (goto-char (point-min))
2981           (push (cons 'download
2982                       (and (search-forward "\203" nil t) (- (point) 2)))
2983                 pos)))
2984       (setq gnus-summary-mark-positions pos))))
2985
2986 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2987   "Insert a dummy root in the summary buffer."
2988   (beginning-of-line)
2989   (gnus-add-text-properties
2990    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2991    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2992
2993 (defun gnus-summary-extract-address-component (from)
2994   (or (car (funcall gnus-extract-address-components from))
2995       from))
2996
2997 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2998   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2999                                 default-mime-charset)))
3000     ;; Is it really necessary to do this next part for each summary line?
3001     ;; Luckily, doesn't seem to slow things down much.
3002     (or
3003      (and gnus-ignored-from-addresses
3004           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3005           (let ((extra-headers (mail-header-extra header))
3006                 to
3007                 newsgroups)
3008             (cond
3009              ((setq to (cdr (assq 'To extra-headers)))
3010               (concat "-> "
3011                       (inline
3012                         (gnus-summary-extract-address-component
3013                          (funcall gnus-decode-encoded-word-function to)))))
3014              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3015               (concat "=> " newsgroups)))))
3016      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3017
3018 (defun gnus-summary-insert-line (gnus-tmp-header
3019                                  gnus-tmp-level gnus-tmp-current
3020                                  gnus-tmp-unread gnus-tmp-replied
3021                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3022                                  &optional gnus-tmp-dummy gnus-tmp-score
3023                                  gnus-tmp-process)
3024   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3025          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3026          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3027          (gnus-tmp-score-char
3028           (if (or (null gnus-summary-default-score)
3029                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3030                       gnus-summary-zcore-fuzz))
3031               ?\ ;;;Whitespace
3032             (if (< gnus-tmp-score gnus-summary-default-score)
3033                 gnus-score-below-mark gnus-score-over-mark)))
3034          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3035          (gnus-tmp-replied
3036           (cond (gnus-tmp-process gnus-process-mark)
3037                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3038                  gnus-cached-mark)
3039                 (gnus-tmp-replied gnus-replied-mark)
3040                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3041                  gnus-forwarded-mark)
3042                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3043                  gnus-saved-mark)
3044                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3045                  gnus-recent-mark)
3046                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3047                  gnus-unseen-mark)
3048                 (t gnus-no-mark)))
3049          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3050          (gnus-tmp-name
3051           (cond
3052            ((string-match "<[^>]+> *$" gnus-tmp-from)
3053             (let ((beg (match-beginning 0)))
3054               (or (and (string-match "^\".+\"" gnus-tmp-from)
3055                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3056                   (substring gnus-tmp-from 0 beg))))
3057            ((string-match "(.+)" gnus-tmp-from)
3058             (substring gnus-tmp-from
3059                        (1+ (match-beginning 0)) (1- (match-end 0))))
3060            (t gnus-tmp-from)))
3061          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3062          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3063          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3064          (buffer-read-only nil))
3065     (when (string= gnus-tmp-name "")
3066       (setq gnus-tmp-name gnus-tmp-from))
3067     (unless (numberp gnus-tmp-lines)
3068       (setq gnus-tmp-lines -1))
3069     (if (= gnus-tmp-lines -1)
3070         (setq gnus-tmp-lines "?")
3071       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3072     (gnus-put-text-property-excluding-characters-with-faces
3073      (point)
3074      (progn (eval gnus-summary-line-format-spec) (point))
3075      'gnus-number gnus-tmp-number)
3076     (when (gnus-visual-p 'summary-highlight 'highlight)
3077       (forward-line -1)
3078       (gnus-run-hooks 'gnus-summary-update-hook)
3079       (forward-line 1))))
3080
3081 (defun gnus-summary-update-line (&optional dont-update)
3082   "Update summary line after change."
3083   (when (and gnus-summary-default-score
3084              (not gnus-summary-inhibit-highlight))
3085     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3086            (article (gnus-summary-article-number))
3087            (score (gnus-summary-article-score article)))
3088       (unless dont-update
3089         (if (and gnus-summary-mark-below
3090                  (< (gnus-summary-article-score)
3091                     gnus-summary-mark-below))
3092             ;; This article has a low score, so we mark it as read.
3093             (when (memq article gnus-newsgroup-unreads)
3094               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3095           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3096             ;; This article was previously marked as read on account
3097             ;; of a low score, but now it has risen, so we mark it as
3098             ;; unread.
3099             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3100         (gnus-summary-update-mark
3101          (if (or (null gnus-summary-default-score)
3102                  (<= (abs (- score gnus-summary-default-score))
3103                      gnus-summary-zcore-fuzz))
3104              ?\ ;;;Whitespace
3105            (if (< score gnus-summary-default-score)
3106                gnus-score-below-mark gnus-score-over-mark))
3107          'score))
3108       ;; Do visual highlighting.
3109       (when (gnus-visual-p 'summary-highlight 'highlight)
3110         (gnus-run-hooks 'gnus-summary-update-hook)))))
3111
3112 (defvar gnus-tmp-new-adopts nil)
3113
3114 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3115   "Return the number of articles in THREAD.
3116 This may be 0 in some cases -- if none of the articles in
3117 the thread are to be displayed."
3118   (let* ((number
3119           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3120           (cond
3121            ((not (listp thread))
3122             1)
3123            ((and (consp thread) (cdr thread))
3124             (apply
3125              '+ 1 (mapcar
3126                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3127            ((null thread)
3128             1)
3129            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3130             1)
3131            (t 0))))
3132     (when (and level (zerop level) gnus-tmp-new-adopts)
3133       (incf number
3134             (apply '+ (mapcar
3135                        'gnus-summary-number-of-articles-in-thread
3136                        gnus-tmp-new-adopts))))
3137     (if char
3138         (if (> number 1) gnus-not-empty-thread-mark
3139           gnus-empty-thread-mark)
3140       number)))
3141
3142 (defun gnus-summary-set-local-parameters (group)
3143   "Go through the local params of GROUP and set all variable specs in that list."
3144   (let ((params (gnus-group-find-parameter group))
3145         (vars '(quit-config))           ; Ignore quit-config.
3146         elem)
3147     (while params
3148       (setq elem (car params)
3149             params (cdr params))
3150       (and (consp elem)                 ; Has to be a cons.
3151            (consp (cdr elem))           ; The cdr has to be a list.
3152            (symbolp (car elem))         ; Has to be a symbol in there.
3153            (not (memq (car elem) vars))
3154            (ignore-errors               ; So we set it.
3155              (push (car elem) vars)
3156              (make-local-variable (car elem))
3157              (set (car elem) (eval (nth 1 elem))))))))
3158
3159 (defun gnus-summary-read-group (group &optional show-all no-article
3160                                       kill-buffer no-display backward
3161                                       select-articles)
3162   "Start reading news in newsgroup GROUP.
3163 If SHOW-ALL is non-nil, already read articles are also listed.
3164 If NO-ARTICLE is non-nil, no article is selected initially.
3165 If NO-DISPLAY, don't generate a summary buffer."
3166   (let (result)
3167     (while (and group
3168                 (null (setq result
3169                             (let ((gnus-auto-select-next nil))
3170                               (or (gnus-summary-read-group-1
3171                                    group show-all no-article
3172                                    kill-buffer no-display
3173                                    select-articles)
3174                                   (setq show-all nil
3175                                         select-articles nil)))))
3176                 (eq gnus-auto-select-next 'quietly))
3177       (set-buffer gnus-group-buffer)
3178       ;; The entry function called above goes to the next
3179       ;; group automatically, so we go two groups back
3180       ;; if we are searching for the previous group.
3181       (when backward
3182         (gnus-group-prev-unread-group 2))
3183       (if (not (equal group (gnus-group-group-name)))
3184           (setq group (gnus-group-group-name))
3185         (setq group nil)))
3186     result))
3187
3188 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3189   "Directly jump to the other GROUP from summary buffer.
3190 If SHOW-ALL is non-nil, already read articles are also listed."
3191   (interactive
3192    (if (eq gnus-summary-buffer (current-buffer))
3193        (list (completing-read
3194               "Group: " gnus-active-hashtb nil t
3195               (when (and gnus-newsgroup-name
3196                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3197                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3198               'gnus-group-history)
3199              current-prefix-arg)
3200      (error "%s must be invoked from a gnus summary buffer." this-command)))
3201   (unless (or (zerop (length group))
3202               (and gnus-newsgroup-name
3203                    (string-equal gnus-newsgroup-name group)))
3204     (gnus-summary-exit)
3205     (gnus-summary-read-group group show-all
3206                              gnus-dont-select-after-jump-to-other-group)))
3207
3208 (defun gnus-summary-read-group-1 (group show-all no-article
3209                                         kill-buffer no-display
3210                                         &optional select-articles)
3211   ;; Killed foreign groups can't be entered.
3212   ;;  (when (and (not (gnus-group-native-p group))
3213   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3214   ;;    (error "Dead non-native groups can't be entered"))
3215   (gnus-message 5 "Retrieving newsgroup: %s..."
3216                 (gnus-group-decoded-name group))
3217   (let* ((new-group (gnus-summary-setup-buffer group))
3218          (quit-config (gnus-group-quit-config group))
3219          (did-select (and new-group (gnus-select-newsgroup
3220                                      group show-all select-articles))))
3221     (cond
3222      ;; This summary buffer exists already, so we just select it.
3223      ((not new-group)
3224       (gnus-set-global-variables)
3225       (when kill-buffer
3226         (gnus-kill-or-deaden-summary kill-buffer))
3227       (gnus-configure-windows 'summary 'force)
3228       (gnus-set-mode-line 'summary)
3229       (gnus-summary-position-point)
3230       (message "")
3231       t)
3232      ;; We couldn't select this group.
3233      ((null did-select)
3234       (when (and (eq major-mode 'gnus-summary-mode)
3235                  (not (equal (current-buffer) kill-buffer)))
3236         (kill-buffer (current-buffer))
3237         (if (not quit-config)
3238             (progn
3239               ;; Update the info -- marks might need to be removed,
3240               ;; for instance.
3241               (gnus-summary-update-info)
3242               (set-buffer gnus-group-buffer)
3243               (gnus-group-jump-to-group group)
3244               (gnus-group-next-unread-group 1))
3245           (gnus-handle-ephemeral-exit quit-config)))
3246       (let ((grpinfo (gnus-get-info group)))
3247         (if (null (gnus-info-read grpinfo))
3248             (gnus-message 3 "Group %s contains no messages"
3249                           (gnus-group-decoded-name group))
3250           (gnus-message 3 "Can't select group")))
3251       nil)
3252      ;; The user did a `C-g' while prompting for number of articles,
3253      ;; so we exit this group.
3254      ((eq did-select 'quit)
3255       (and (eq major-mode 'gnus-summary-mode)
3256            (not (equal (current-buffer) kill-buffer))
3257            (kill-buffer (current-buffer)))
3258       (when kill-buffer
3259         (gnus-kill-or-deaden-summary kill-buffer))
3260       (if (not quit-config)
3261           (progn
3262             (set-buffer gnus-group-buffer)
3263             (gnus-group-jump-to-group group)
3264             (gnus-group-next-unread-group 1)
3265             (gnus-configure-windows 'group 'force))
3266         (gnus-handle-ephemeral-exit quit-config))
3267       ;; Finally signal the quit.
3268       (signal 'quit nil))
3269      ;; The group was successfully selected.
3270      (t
3271       (gnus-set-global-variables)
3272       ;; Save the active value in effect when the group was entered.
3273       (setq gnus-newsgroup-active
3274             (gnus-copy-sequence
3275              (gnus-active gnus-newsgroup-name)))
3276       ;; You can change the summary buffer in some way with this hook.
3277       (gnus-run-hooks 'gnus-select-group-hook)
3278       (gnus-update-format-specifications
3279        nil 'summary 'summary-mode 'summary-dummy)
3280       (gnus-update-summary-mark-positions)
3281       ;; Do score processing.
3282       (when gnus-use-scoring
3283         (gnus-possibly-score-headers))
3284       ;; Check whether to fill in the gaps in the threads.
3285       (when gnus-build-sparse-threads
3286         (gnus-build-sparse-threads))
3287       ;; Find the initial limit.
3288       (if gnus-show-threads
3289           (if show-all
3290               (let ((gnus-newsgroup-dormant nil))
3291                 (gnus-summary-initial-limit show-all))
3292             (gnus-summary-initial-limit show-all))
3293         ;; When untreaded, all articles are always shown.
3294         (setq gnus-newsgroup-limit
3295               (mapcar
3296                (lambda (header) (mail-header-number header))
3297                gnus-newsgroup-headers)))
3298       ;; Generate the summary buffer.
3299       (unless no-display
3300         (gnus-summary-prepare))
3301       (when gnus-use-trees
3302         (gnus-tree-open group)
3303         (setq gnus-summary-highlight-line-function
3304               'gnus-tree-highlight-article))
3305       ;; If the summary buffer is empty, but there are some low-scored
3306       ;; articles or some excluded dormants, we include these in the
3307       ;; buffer.
3308       (when (and (zerop (buffer-size))
3309                  (not no-display))
3310         (cond (gnus-newsgroup-dormant
3311                (gnus-summary-limit-include-dormant))
3312               ((and gnus-newsgroup-scored show-all)
3313                (gnus-summary-limit-include-expunged t))))
3314       ;; Function `gnus-apply-kill-file' must be called in this hook.
3315       (gnus-run-hooks 'gnus-apply-kill-hook)
3316       (if (and (zerop (buffer-size))
3317                (not no-display))
3318           (progn
3319             ;; This newsgroup is empty.
3320             (gnus-summary-catchup-and-exit nil t)
3321             (gnus-message 6 "No unread news")
3322             (when kill-buffer
3323               (gnus-kill-or-deaden-summary kill-buffer))
3324             ;; Return nil from this function.
3325             nil)
3326         ;; Hide conversation thread subtrees.  We cannot do this in
3327         ;; gnus-summary-prepare-hook since kill processing may not
3328         ;; work with hidden articles.
3329         (gnus-summary-maybe-hide-threads)
3330         (when kill-buffer
3331           (gnus-kill-or-deaden-summary kill-buffer))
3332         (gnus-summary-auto-select-subject)
3333         ;; Show first unread article if requested.
3334         (if (and (not no-article)
3335                  (not no-display)
3336                  gnus-newsgroup-unreads
3337                  gnus-auto-select-first)
3338             (progn
3339               (gnus-configure-windows 'summary)
3340               (let ((art (gnus-summary-article-number)))
3341                 (unless (or (memq art gnus-newsgroup-undownloaded)
3342                             (memq art gnus-newsgroup-downloadable))
3343                   (gnus-summary-goto-article art))))
3344           ;; Don't select any articles.
3345           (gnus-summary-position-point)
3346           (gnus-configure-windows 'summary 'force)
3347           (gnus-set-mode-line 'summary))
3348         (when (get-buffer-window gnus-group-buffer t)
3349           ;; Gotta use windows, because recenter does weird stuff if
3350           ;; the current buffer ain't the displayed window.
3351           (let ((owin (selected-window)))
3352             (select-window (get-buffer-window gnus-group-buffer t))
3353             (when (gnus-group-goto-group group)
3354               (recenter))
3355             (select-window owin)))
3356         ;; Mark this buffer as "prepared".
3357         (setq gnus-newsgroup-prepared t)
3358         (gnus-run-hooks 'gnus-summary-prepared-hook)
3359         t)))))
3360
3361 (defun gnus-summary-auto-select-subject ()
3362   "Select the subject line on initial group entry."
3363   (goto-char (point-min))
3364   (cond
3365    ((eq gnus-auto-select-subject 'best)
3366     (gnus-summary-best-unread-subject))
3367    ((eq gnus-auto-select-subject 'unread)
3368     (gnus-summary-first-unread-subject))
3369    ((eq gnus-auto-select-subject 'unseen)
3370     (gnus-summary-first-unseen-subject))
3371    ((eq gnus-auto-select-subject 'unseen-or-unread)
3372     (gnus-summary-first-unseen-or-unread-subject))
3373    ((eq gnus-auto-select-subject 'first)
3374     ;; Do nothing.
3375     )
3376    ((gnus-functionp gnus-auto-select-subject)
3377     (funcall gnus-auto-select-subject))))
3378
3379 (defun gnus-summary-prepare ()
3380   "Generate the summary buffer."
3381   (interactive)
3382   (let ((buffer-read-only nil))
3383     (erase-buffer)
3384     (setq gnus-newsgroup-data nil
3385           gnus-newsgroup-data-reverse nil)
3386     (gnus-run-hooks 'gnus-summary-generate-hook)
3387     ;; Generate the buffer, either with threads or without.
3388     (when gnus-newsgroup-headers
3389       (gnus-summary-prepare-threads
3390        (if gnus-show-threads
3391            (gnus-sort-gathered-threads
3392             (funcall gnus-summary-thread-gathering-function
3393                      (gnus-sort-threads
3394                       (gnus-cut-threads (gnus-make-threads)))))
3395          ;; Unthreaded display.
3396          (gnus-sort-articles gnus-newsgroup-headers))))
3397     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3398     ;; Call hooks for modifying summary buffer.
3399     (goto-char (point-min))
3400     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3401
3402 (defsubst gnus-general-simplify-subject (subject)
3403   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3404   (setq subject
3405         (cond
3406          ;; Truncate the subject.
3407          (gnus-simplify-subject-functions
3408           (gnus-map-function gnus-simplify-subject-functions subject))
3409          ((numberp gnus-summary-gather-subject-limit)
3410           (setq subject (gnus-simplify-subject-re subject))
3411           (if (> (length subject) gnus-summary-gather-subject-limit)
3412               (substring subject 0 gnus-summary-gather-subject-limit)
3413             subject))
3414          ;; Fuzzily simplify it.
3415          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3416           (gnus-simplify-subject-fuzzy subject))
3417          ;; Just remove the leading "Re:".
3418          (t
3419           (gnus-simplify-subject-re subject))))
3420
3421   (if (and gnus-summary-gather-exclude-subject
3422            (string-match gnus-summary-gather-exclude-subject subject))
3423       nil                               ; This article shouldn't be gathered
3424     subject))
3425
3426 (defun gnus-summary-simplify-subject-query ()
3427   "Query where the respool algorithm would put this article."
3428   (interactive)
3429   (gnus-summary-select-article)
3430   (message "%s"
3431            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3432
3433 (defun gnus-gather-threads-by-subject (threads)
3434   "Gather threads by looking at Subject headers."
3435   (if (not gnus-summary-make-false-root)
3436       threads
3437     (let ((hashtb (gnus-make-hashtable 1024))
3438           (prev threads)
3439           (result threads)
3440           subject hthread whole-subject)
3441       (while threads
3442         (setq subject (gnus-general-simplify-subject
3443                        (setq whole-subject (mail-header-subject
3444                                             (caar threads)))))
3445         (when subject
3446           (if (setq hthread (gnus-gethash subject hashtb))
3447               (progn
3448                 ;; We enter a dummy root into the thread, if we
3449                 ;; haven't done that already.
3450                 (unless (stringp (caar hthread))
3451                   (setcar hthread (list whole-subject (car hthread))))
3452                 ;; We add this new gathered thread to this gathered
3453                 ;; thread.
3454                 (setcdr (car hthread)
3455                         (nconc (cdar hthread) (list (car threads))))
3456                 ;; Remove it from the list of threads.
3457                 (setcdr prev (cdr threads))
3458                 (setq threads prev))
3459             ;; Enter this thread into the hash table.
3460             (gnus-sethash subject threads hashtb)))
3461         (setq prev threads)
3462         (setq threads (cdr threads)))
3463       result)))
3464
3465 (defun gnus-gather-threads-by-references (threads)
3466   "Gather threads by looking at References headers."
3467   (let ((idhashtb (gnus-make-hashtable 1024))
3468         (thhashtb (gnus-make-hashtable 1024))
3469         (prev threads)
3470         (result threads)
3471         ids references id gthread gid entered ref)
3472     (while threads
3473       (when (setq references (mail-header-references (caar threads)))
3474         (setq id (mail-header-id (caar threads))
3475               ids (inline (gnus-split-references references))
3476               entered nil)
3477         (while (setq ref (pop ids))
3478           (setq ids (delete ref ids))
3479           (if (not (setq gid (gnus-gethash ref idhashtb)))
3480               (progn
3481                 (gnus-sethash ref id idhashtb)
3482                 (gnus-sethash id threads thhashtb))
3483             (setq gthread (gnus-gethash gid thhashtb))
3484             (unless entered
3485               ;; We enter a dummy root into the thread, if we
3486               ;; haven't done that already.
3487               (unless (stringp (caar gthread))
3488                 (setcar gthread (list (mail-header-subject (caar gthread))
3489                                       (car gthread))))
3490               ;; We add this new gathered thread to this gathered
3491               ;; thread.
3492               (setcdr (car gthread)
3493                       (nconc (cdar gthread) (list (car threads)))))
3494             ;; Add it into the thread hash table.
3495             (gnus-sethash id gthread thhashtb)
3496             (setq entered t)
3497             ;; Remove it from the list of threads.
3498             (setcdr prev (cdr threads))
3499             (setq threads prev))))
3500       (setq prev threads)
3501       (setq threads (cdr threads)))
3502     result))
3503
3504 (defun gnus-sort-gathered-threads (threads)
3505   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3506   (let ((result threads))
3507     (while threads
3508       (when (stringp (caar threads))
3509         (setcdr (car threads)
3510                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3511       (setq threads (cdr threads)))
3512     result))
3513
3514 (defun gnus-thread-loop-p (root thread)
3515   "Say whether ROOT is in THREAD."
3516   (let ((stack (list thread))
3517         (infloop 0)
3518         th)
3519     (while (setq thread (pop stack))
3520       (setq th (cdr thread))
3521       (while (and th
3522                   (not (eq (caar th) root)))
3523         (pop th))
3524       (if th
3525           ;; We have found a loop.
3526           (let (ref-dep)
3527             (setcdr thread (delq (car th) (cdr thread)))
3528             (if (boundp (setq ref-dep (intern "none"
3529                                               gnus-newsgroup-dependencies)))
3530                 (setcdr (symbol-value ref-dep)
3531                         (nconc (cdr (symbol-value ref-dep))
3532                                (list (car th))))
3533               (set ref-dep (list nil (car th))))
3534             (setq infloop 1
3535                   stack nil))
3536         ;; Push all the subthreads onto the stack.
3537         (push (cdr thread) stack)))
3538     infloop))
3539
3540 (defun gnus-make-threads ()
3541   "Go through the dependency hashtb and find the roots.  Return all threads."
3542   (let (threads)
3543     (while (catch 'infloop
3544              (mapatoms
3545               (lambda (refs)
3546                 ;; Deal with self-referencing References loops.
3547                 (when (and (car (symbol-value refs))
3548                            (not (zerop
3549                                  (apply
3550                                   '+
3551                                   (mapcar
3552                                    (lambda (thread)
3553                                      (gnus-thread-loop-p
3554                                       (car (symbol-value refs)) thread))
3555                                    (cdr (symbol-value refs)))))))
3556                   (setq threads nil)
3557                   (throw 'infloop t))
3558                 (unless (car (symbol-value refs))
3559                   ;; These threads do not refer back to any other articles,
3560                   ;; so they're roots.
3561                   (setq threads (append (cdr (symbol-value refs)) threads))))
3562               gnus-newsgroup-dependencies)))
3563     threads))
3564
3565 ;; Build the thread tree.
3566 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3567   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3568
3569 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3570 if it was already present.
3571
3572 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3573 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3574 Message-IDs will be renamed to a unique Message-ID before being
3575 entered.
3576
3577 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3578   (let* ((id (mail-header-id header))
3579          (id-dep (and id (intern id dependencies)))
3580          ref ref-dep ref-header)
3581     ;; Enter this `header' in the `dependencies' table.
3582     (cond
3583      ((not id-dep)
3584       (setq header nil))
3585      ;; The first two cases do the normal part: enter a new `header'
3586      ;; in the `dependencies' table.
3587      ((not (boundp id-dep))
3588       (set id-dep (list header)))
3589      ((null (car (symbol-value id-dep)))
3590       (setcar (symbol-value id-dep) header))
3591
3592      ;; From here the `header' was already present in the
3593      ;; `dependencies' table.
3594      (force-new
3595       ;; Overrides an existing entry;
3596       ;; just set the header part of the entry.
3597       (setcar (symbol-value id-dep) header))
3598
3599      ;; Renames the existing `header' to a unique Message-ID.
3600      ((not gnus-summary-ignore-duplicates)
3601       ;; An article with this Message-ID has already been seen.
3602       ;; We rename the Message-ID.
3603       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3604            (list header))
3605       (mail-header-set-id header id))
3606
3607      ;; The last case ignores an existing entry, except it adds any
3608      ;; additional Xrefs (in case the two articles came from different
3609      ;; servers.
3610      ;; Also sets `header' to `nil' meaning that the `dependencies'
3611      ;; table was *not* modified.
3612      (t
3613       (mail-header-set-xref
3614        (car (symbol-value id-dep))
3615        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3616                    "")
3617                (or (mail-header-xref header) "")))
3618       (setq header nil)))
3619
3620     (when header
3621       ;; First check that we are not creating a References loop.
3622       (setq ref (gnus-parent-id (mail-header-references header)))
3623       (while (and ref
3624                   (setq ref-dep (intern-soft ref dependencies))
3625                   (boundp ref-dep)
3626                   (setq ref-header (car (symbol-value ref-dep))))
3627         (if (string= id ref)
3628             ;; Yuk!  This is a reference loop.  Make the article be a
3629             ;; root article.
3630             (progn
3631               (mail-header-set-references (car (symbol-value id-dep)) "none")
3632               (setq ref nil))
3633           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3634       (setq ref (gnus-parent-id (mail-header-references header)))
3635       (setq ref-dep (intern (or ref "none") dependencies))
3636       (if (boundp ref-dep)
3637           (setcdr (symbol-value ref-dep)
3638                   (nconc (cdr (symbol-value ref-dep))
3639                          (list (symbol-value id-dep))))
3640         (set ref-dep (list nil (symbol-value id-dep)))))
3641     header))
3642
3643 (defun gnus-extract-message-id-from-in-reply-to (string)
3644   (if (string-match "<[^>]+>" string)
3645       (substring string (match-beginning 0) (match-end 0))
3646     nil))
3647
3648 (defun gnus-build-sparse-threads ()
3649   (let ((headers gnus-newsgroup-headers)
3650         (mail-parse-charset gnus-newsgroup-charset)
3651         (gnus-summary-ignore-duplicates t)
3652         header references generation relations
3653         subject child end new-child date)
3654     ;; First we create an alist of generations/relations, where
3655     ;; generations is how much we trust the relation, and the relation
3656     ;; is parent/child.
3657     (gnus-message 7 "Making sparse threads...")
3658     (save-excursion
3659       (nnheader-set-temp-buffer " *gnus sparse threads*")
3660       (while (setq header (pop headers))
3661         (when (and (setq references (mail-header-references header))
3662                    (not (string= references "")))
3663           (insert references)
3664           (setq child (mail-header-id header)
3665                 subject (mail-header-subject header)
3666                 date (mail-header-date header)
3667                 generation 0)
3668           (while (search-backward ">" nil t)
3669             (setq end (1+ (point)))
3670             (when (search-backward "<" nil t)
3671               (setq new-child (buffer-substring (point) end))
3672               (push (list (incf generation)
3673                           child (setq child new-child)
3674                           subject date)
3675                     relations)))
3676           (when child
3677             (push (list (1+ generation) child nil subject) relations))
3678           (erase-buffer)))
3679       (kill-buffer (current-buffer)))
3680     ;; Sort over trustworthiness.
3681     (mapcar
3682      (lambda (relation)
3683        (when (gnus-dependencies-add-header
3684               (make-full-mail-header-from-decoded-header
3685                gnus-reffed-article-number
3686                (nth 3 relation) "" (or (nth 4 relation) "")
3687                (nth 1 relation)
3688                (or (nth 2 relation) "") 0 0 "")
3689               gnus-newsgroup-dependencies nil)
3690          (push gnus-reffed-article-number gnus-newsgroup-limit)
3691          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3692          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3693                gnus-newsgroup-reads)
3694          (decf gnus-reffed-article-number)))
3695      (sort relations 'car-less-than-car))
3696     (gnus-message 7 "Making sparse threads...done")))
3697
3698 (defun gnus-build-old-threads ()
3699   ;; Look at all the articles that refer back to old articles, and
3700   ;; fetch the headers for the articles that aren't there.  This will
3701   ;; build complete threads - if the roots haven't been expired by the
3702   ;; server, that is.
3703   (let ((mail-parse-charset gnus-newsgroup-charset)
3704         id heads)
3705     (mapatoms
3706      (lambda (refs)
3707        (when (not (car (symbol-value refs)))
3708          (setq heads (cdr (symbol-value refs)))
3709          (while heads
3710            (if (memq (mail-header-number (caar heads))
3711                      gnus-newsgroup-dormant)
3712                (setq heads (cdr heads))
3713              (setq id (symbol-name refs))
3714              (while (and (setq id (gnus-build-get-header id))
3715                          (not (car (gnus-id-to-thread id)))))
3716              (setq heads nil)))))
3717      gnus-newsgroup-dependencies)))
3718
3719 ;; This function has to be called with point after the article number
3720 ;; on the beginning of the line.
3721 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3722   (let ((eol (gnus-point-at-eol))
3723         (buffer (current-buffer))
3724         header references in-reply-to)
3725
3726     ;; overview: [num subject from date id refs chars lines misc]
3727     (unwind-protect
3728         (progn
3729           (narrow-to-region (point) eol)
3730           (unless (eobp)
3731             (forward-char))
3732
3733           (setq header
3734                 (make-full-mail-header
3735                  number                         ; number
3736                  (nnheader-nov-field)           ; subject
3737                  (nnheader-nov-field)           ; from
3738                  (nnheader-nov-field)           ; date
3739                  (nnheader-nov-read-message-id) ; id
3740                  (nnheader-nov-field)           ; refs
3741                  (nnheader-nov-read-integer)    ; chars
3742                  (nnheader-nov-read-integer)    ; lines
3743                  (unless (eobp)
3744                    (if (looking-at "Xref: ")
3745                        (goto-char (match-end 0)))
3746                    (nnheader-nov-field))        ; Xref
3747                  (nnheader-nov-parse-extra))))  ; extra
3748
3749       (widen))
3750
3751     (when (and (string= references "")
3752                (setq in-reply-to (mail-header-extra header))
3753                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3754       (mail-header-set-references
3755        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3756
3757     (when gnus-alter-header-function
3758       (funcall gnus-alter-header-function header))
3759     (gnus-dependencies-add-header header dependencies force-new)))
3760
3761 (defun gnus-build-get-header (id)
3762   "Look through the buffer of NOV lines and find the header to ID.
3763 Enter this line into the dependencies hash table, and return
3764 the id of the parent article (if any)."
3765   (let ((deps gnus-newsgroup-dependencies)
3766         found header)
3767     (prog1
3768         (save-excursion
3769           (set-buffer nntp-server-buffer)
3770           (let ((case-fold-search nil))
3771             (goto-char (point-min))
3772             (while (and (not found)
3773                         (search-forward id nil t))
3774               (beginning-of-line)
3775               (setq found (looking-at
3776                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3777                                    (regexp-quote id))))
3778               (or found (beginning-of-line 2)))
3779             (when found
3780               (beginning-of-line)
3781               (and
3782                (setq header (gnus-nov-parse-line
3783                              (read (current-buffer)) deps))
3784                (gnus-parent-id (mail-header-references header))))))
3785       (when header
3786         (let ((number (mail-header-number header)))
3787           (push number gnus-newsgroup-limit)
3788           (push header gnus-newsgroup-headers)
3789           (if (memq number gnus-newsgroup-unselected)
3790               (progn
3791                 (setq gnus-newsgroup-unreads
3792                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3793                                                number))
3794                 (setq gnus-newsgroup-unselected
3795                       (delq number gnus-newsgroup-unselected)))
3796             (push number gnus-newsgroup-ancient)))))))
3797
3798 (defun gnus-build-all-threads ()
3799   "Read all the headers."
3800   (let ((gnus-summary-ignore-duplicates t)
3801         (mail-parse-charset gnus-newsgroup-charset)
3802         (dependencies gnus-newsgroup-dependencies)
3803         header article)
3804     (save-excursion
3805       (set-buffer nntp-server-buffer)
3806       (let ((case-fold-search nil))
3807         (goto-char (point-min))
3808         (while (not (eobp))
3809           (ignore-errors
3810             (setq article (read (current-buffer))
3811                   header (gnus-nov-parse-line article dependencies)))
3812           (when header
3813             (save-excursion
3814               (set-buffer gnus-summary-buffer)
3815               (push header gnus-newsgroup-headers)
3816               (if (memq (setq article (mail-header-number header))
3817                         gnus-newsgroup-unselected)
3818                   (progn
3819                     (setq gnus-newsgroup-unreads
3820                           (gnus-add-to-sorted-list
3821                            gnus-newsgroup-unreads article))
3822                     (setq gnus-newsgroup-unselected
3823                           (delq article gnus-newsgroup-unselected)))
3824                 (push article gnus-newsgroup-ancient)))
3825             (forward-line 1)))))))
3826
3827 (defun gnus-summary-update-article-line (article header)
3828   "Update the line for ARTICLE using HEADERS."
3829   (let* ((id (mail-header-id header))
3830          (thread (gnus-id-to-thread id)))
3831     (unless thread
3832       (error "Article in no thread"))
3833     ;; Update the thread.
3834     (setcar thread header)
3835     (gnus-summary-goto-subject article)
3836     (let* ((datal (gnus-data-find-list article))
3837            (data (car datal))
3838            (length (when (cdr datal)
3839                      (- (gnus-data-pos data)
3840                         (gnus-data-pos (cadr datal)))))
3841            (buffer-read-only nil)
3842            (level (gnus-summary-thread-level)))
3843       (gnus-delete-line)
3844       (gnus-summary-insert-line
3845        header level nil (gnus-article-mark article)
3846        (memq article gnus-newsgroup-replied)
3847        (memq article gnus-newsgroup-expirable)
3848        ;; Only insert the Subject string when it's different
3849        ;; from the previous Subject string.
3850        (if (and
3851             gnus-show-threads
3852             (gnus-subject-equal
3853              (condition-case ()
3854                  (mail-header-subject
3855                   (gnus-data-header
3856                    (cadr
3857                     (gnus-data-find-list
3858                      article
3859                      (gnus-data-list t)))))
3860                ;; Error on the side of excessive subjects.
3861                (error ""))
3862              (mail-header-subject header)))
3863            ""
3864          (mail-header-subject header))
3865        nil (cdr (assq article gnus-newsgroup-scored))
3866        (memq article gnus-newsgroup-processable))
3867       (when length
3868         (gnus-data-update-list
3869          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3870
3871 (defun gnus-summary-update-article (article &optional iheader)
3872   "Update ARTICLE in the summary buffer."
3873   (set-buffer gnus-summary-buffer)
3874   (let* ((header (gnus-summary-article-header article))
3875          (id (mail-header-id header))
3876          (data (gnus-data-find article))
3877          (thread (gnus-id-to-thread id))
3878          (references (mail-header-references header))
3879          (parent
3880           (gnus-id-to-thread
3881            (or (gnus-parent-id
3882                 (when (and references
3883                            (not (equal "" references)))
3884                   references))
3885                "none")))
3886          (buffer-read-only nil)
3887          (old (car thread)))
3888     (when thread
3889       (unless iheader
3890         (setcar thread nil)
3891         (when parent
3892           (delq thread parent)))
3893       (if (gnus-summary-insert-subject id header)
3894           ;; Set the (possibly) new article number in the data structure.
3895           (gnus-data-set-number data (gnus-id-to-article id))
3896         (setcar thread old)
3897         nil))))
3898
3899 (defun gnus-rebuild-thread (id &optional line)
3900   "Rebuild the thread containing ID.
3901 If LINE, insert the rebuilt thread starting on line LINE."
3902   (let ((buffer-read-only nil)
3903         old-pos current thread data)
3904     (if (not gnus-show-threads)
3905         (setq thread (list (car (gnus-id-to-thread id))))
3906       ;; Get the thread this article is part of.
3907       (setq thread (gnus-remove-thread id)))
3908     (setq old-pos (gnus-point-at-bol))
3909     (setq current (save-excursion
3910                     (and (re-search-backward "[\r\n]" nil t)
3911                          (gnus-summary-article-number))))
3912     ;; If this is a gathered thread, we have to go some re-gathering.
3913     (when (stringp (car thread))
3914       (let ((subject (car thread))
3915             roots thr)
3916         (setq thread (cdr thread))
3917         (while thread
3918           (unless (memq (setq thr (gnus-id-to-thread
3919                                    (gnus-root-id
3920                                     (mail-header-id (caar thread)))))
3921                         roots)
3922             (push thr roots))
3923           (setq thread (cdr thread)))
3924         ;; We now have all (unique) roots.
3925         (if (= (length roots) 1)
3926             ;; All the loose roots are now one solid root.
3927             (setq thread (car roots))
3928           (setq thread (cons subject (gnus-sort-threads roots))))))
3929     (let (threads)
3930       ;; We then insert this thread into the summary buffer.
3931       (when line
3932         (goto-char (point-min))
3933         (forward-line (1- line)))
3934       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3935         (if gnus-show-threads
3936             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3937           (gnus-summary-prepare-unthreaded thread))
3938         (setq data (nreverse gnus-newsgroup-data))
3939         (setq threads gnus-newsgroup-threads))
3940       ;; We splice the new data into the data structure.
3941       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3942       ;;!!! then we want to insert at the beginning of the buffer.
3943       ;;!!! That happens to be true with Gnus now, but that may
3944       ;;!!! change in the future.  Perhaps.
3945       (gnus-data-enter-list
3946        (if line nil current) data (- (point) old-pos))
3947       (setq gnus-newsgroup-threads
3948             (nconc threads gnus-newsgroup-threads))
3949       (gnus-data-compute-positions))))
3950
3951 (defun gnus-number-to-header (number)
3952   "Return the header for article NUMBER."
3953   (let ((headers gnus-newsgroup-headers))
3954     (while (and headers
3955                 (not (= number (mail-header-number (car headers)))))
3956       (pop headers))
3957     (when headers
3958       (car headers))))
3959
3960 (defun gnus-parent-headers (in-headers &optional generation)
3961   "Return the headers of the GENERATIONeth parent of HEADERS."
3962   (unless generation
3963     (setq generation 1))
3964   (let ((parent t)
3965         (headers in-headers)
3966         references)
3967     (while (and parent
3968                 (not (zerop generation))
3969                 (setq references (mail-header-references headers)))
3970       (setq headers (if (and references
3971                              (setq parent (gnus-parent-id references)))
3972                         (car (gnus-id-to-thread parent))
3973                       nil))
3974       (decf generation))
3975     (and (not (eq headers in-headers))
3976          headers)))
3977
3978 (defun gnus-id-to-thread (id)
3979   "Return the (sub-)thread where ID appears."
3980   (gnus-gethash id gnus-newsgroup-dependencies))
3981
3982 (defun gnus-id-to-article (id)
3983   "Return the article number of ID."
3984   (let ((thread (gnus-id-to-thread id)))
3985     (when (and thread
3986                (car thread))
3987       (mail-header-number (car thread)))))
3988
3989 (defun gnus-id-to-header (id)
3990   "Return the article headers of ID."
3991   (car (gnus-id-to-thread id)))
3992
3993 (defun gnus-article-displayed-root-p (article)
3994   "Say whether ARTICLE is a root(ish) article."
3995   (let ((level (gnus-summary-thread-level article))
3996         (refs (mail-header-references  (gnus-summary-article-header article)))
3997         particle)
3998     (cond
3999      ((null level) nil)
4000      ((zerop level) t)
4001      ((null refs) t)
4002      ((null (gnus-parent-id refs)) t)
4003      ((and (= 1 level)
4004            (null (setq particle (gnus-id-to-article
4005                                  (gnus-parent-id refs))))
4006            (null (gnus-summary-thread-level particle)))))))
4007
4008 (defun gnus-root-id (id)
4009   "Return the id of the root of the thread where ID appears."
4010   (let (last-id prev)
4011     (while (and id (setq prev (car (gnus-id-to-thread id))))
4012       (setq last-id id
4013             id (gnus-parent-id (mail-header-references prev))))
4014     last-id))
4015
4016 (defun gnus-articles-in-thread (thread)
4017   "Return the list of articles in THREAD."
4018   (cons (mail-header-number (car thread))
4019         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4020
4021 (defun gnus-remove-thread (id &optional dont-remove)
4022   "Remove the thread that has ID in it."
4023   (let (headers thread last-id)
4024     ;; First go up in this thread until we find the root.
4025     (setq last-id (gnus-root-id id)
4026           headers (message-flatten-list (gnus-id-to-thread last-id)))
4027     ;; We have now found the real root of this thread.  It might have
4028     ;; been gathered into some loose thread, so we have to search
4029     ;; through the threads to find the thread we wanted.
4030     (let ((threads gnus-newsgroup-threads)
4031           sub)
4032       (while threads
4033         (setq sub (car threads))
4034         (if (stringp (car sub))
4035             ;; This is a gathered thread, so we look at the roots
4036             ;; below it to find whether this article is in this
4037             ;; gathered root.
4038             (progn
4039               (setq sub (cdr sub))
4040               (while sub
4041                 (when (member (caar sub) headers)
4042                   (setq thread (car threads)
4043                         threads nil
4044                         sub nil))
4045                 (setq sub (cdr sub))))
4046           ;; It's an ordinary thread, so we check it.
4047           (when (eq (car sub) (car headers))
4048             (setq thread sub
4049                   threads nil)))
4050         (setq threads (cdr threads)))
4051       ;; If this article is in no thread, then it's a root.
4052       (if thread
4053           (unless dont-remove
4054             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4055         (setq thread (gnus-id-to-thread last-id)))
4056       (when thread
4057         (prog1
4058             thread                      ; We return this thread.
4059           (unless dont-remove
4060             (if (stringp (car thread))
4061                 (progn
4062                   ;; If we use dummy roots, then we have to remove the
4063                   ;; dummy root as well.
4064                   (when (eq gnus-summary-make-false-root 'dummy)
4065                     ;; We go to the dummy root by going to
4066                     ;; the first sub-"thread", and then one line up.
4067                     (gnus-summary-goto-article
4068                      (mail-header-number (caadr thread)))
4069                     (forward-line -1)
4070                     (gnus-delete-line)
4071                     (gnus-data-compute-positions))
4072                   (setq thread (cdr thread))
4073                   (while thread
4074                     (gnus-remove-thread-1 (car thread))
4075                     (setq thread (cdr thread))))
4076               (gnus-remove-thread-1 thread))))))))
4077
4078 (defun gnus-remove-thread-1 (thread)
4079   "Remove the thread THREAD recursively."
4080   (let ((number (mail-header-number (pop thread)))
4081         d)
4082     (setq thread (reverse thread))
4083     (while thread
4084       (gnus-remove-thread-1 (pop thread)))
4085     (when (setq d (gnus-data-find number))
4086       (goto-char (gnus-data-pos d))
4087       (gnus-summary-show-thread)
4088       (gnus-data-remove
4089        number
4090        (- (gnus-point-at-bol)
4091           (prog1
4092               (1+ (gnus-point-at-eol))
4093             (gnus-delete-line)))))))
4094
4095 (defun gnus-sort-threads-1 (threads func)
4096   (sort (mapcar (lambda (thread)
4097                   (cons (car thread)
4098                         (and (cdr thread)
4099                              (gnus-sort-threads-1 (cdr thread) func))))
4100                 threads) func))
4101
4102 (defun gnus-sort-threads (threads)
4103   "Sort THREADS."
4104   (if (not gnus-thread-sort-functions)
4105       threads
4106     (gnus-message 8 "Sorting threads...")
4107     (prog1
4108         (gnus-sort-threads-1
4109          threads
4110          (gnus-make-sort-function gnus-thread-sort-functions))
4111       (gnus-message 8 "Sorting threads...done"))))
4112
4113 (defun gnus-sort-articles (articles)
4114   "Sort ARTICLES."
4115   (when gnus-article-sort-functions
4116     (gnus-message 7 "Sorting articles...")
4117     (prog1
4118         (setq gnus-newsgroup-headers
4119               (sort articles (gnus-make-sort-function
4120                               gnus-article-sort-functions)))
4121       (gnus-message 7 "Sorting articles...done"))))
4122
4123 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4124 (defmacro gnus-thread-header (thread)
4125   "Return header of first article in THREAD.
4126 Note that THREAD must never, ever be anything else than a variable -
4127 using some other form will lead to serious barfage."
4128   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4129   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4130   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4131         (vector thread) 2))
4132
4133 (defsubst gnus-article-sort-by-number (h1 h2)
4134   "Sort articles by article number."
4135   (< (mail-header-number h1)
4136      (mail-header-number h2)))
4137
4138 (defun gnus-thread-sort-by-number (h1 h2)
4139   "Sort threads by root article number."
4140   (gnus-article-sort-by-number
4141    (gnus-thread-header h1) (gnus-thread-header h2)))
4142
4143 (defsubst gnus-article-sort-by-lines (h1 h2)
4144   "Sort articles by article Lines header."
4145   (< (mail-header-lines h1)
4146      (mail-header-lines h2)))
4147
4148 (defun gnus-thread-sort-by-lines (h1 h2)
4149   "Sort threads by root article Lines header."
4150   (gnus-article-sort-by-lines
4151    (gnus-thread-header h1) (gnus-thread-header h2)))
4152
4153 (defsubst gnus-article-sort-by-chars (h1 h2)
4154   "Sort articles by octet length."
4155   (< (mail-header-chars h1)
4156      (mail-header-chars h2)))
4157
4158 (defun gnus-thread-sort-by-chars (h1 h2)
4159   "Sort threads by root article octet length."
4160   (gnus-article-sort-by-chars
4161    (gnus-thread-header h1) (gnus-thread-header h2)))
4162
4163 (defsubst gnus-article-sort-by-author (h1 h2)
4164   "Sort articles by root author."
4165   (string-lessp
4166    (let ((addr (car (mime-entity-read-field h1 'From))))
4167      (or (std11-full-name-string addr)
4168          (std11-address-string addr)
4169          ""))
4170    (let ((addr (car (mime-entity-read-field h2 'From))))
4171      (or (std11-full-name-string addr)
4172          (std11-address-string addr)
4173          ""))
4174    ))
4175
4176 (defun gnus-thread-sort-by-author (h1 h2)
4177   "Sort threads by root author."
4178   (gnus-article-sort-by-author
4179    (gnus-thread-header h1)  (gnus-thread-header h2)))
4180
4181 (defsubst gnus-article-sort-by-subject (h1 h2)
4182   "Sort articles by root subject."
4183   (string-lessp
4184    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4185    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4186
4187 (defun gnus-thread-sort-by-subject (h1 h2)
4188   "Sort threads by root subject."
4189   (gnus-article-sort-by-subject
4190    (gnus-thread-header h1) (gnus-thread-header h2)))
4191
4192 (defsubst gnus-article-sort-by-date (h1 h2)
4193   "Sort articles by root article date."
4194   (time-less-p
4195    (gnus-date-get-time (mail-header-date h1))
4196    (gnus-date-get-time (mail-header-date h2))))
4197
4198 (defun gnus-thread-sort-by-date (h1 h2)
4199   "Sort threads by root article date."
4200   (gnus-article-sort-by-date
4201    (gnus-thread-header h1) (gnus-thread-header h2)))
4202
4203 (defsubst gnus-article-sort-by-score (h1 h2)
4204   "Sort articles by root article score.
4205 Unscored articles will be counted as having a score of zero."
4206   (> (or (cdr (assq (mail-header-number h1)
4207                     gnus-newsgroup-scored))
4208          gnus-summary-default-score 0)
4209      (or (cdr (assq (mail-header-number h2)
4210                     gnus-newsgroup-scored))
4211          gnus-summary-default-score 0)))
4212
4213 (defun gnus-thread-sort-by-score (h1 h2)
4214   "Sort threads by root article score."
4215   (gnus-article-sort-by-score
4216    (gnus-thread-header h1) (gnus-thread-header h2)))
4217
4218 (defun gnus-thread-sort-by-total-score (h1 h2)
4219   "Sort threads by the sum of all scores in the thread.
4220 Unscored articles will be counted as having a score of zero."
4221   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4222
4223 (defun gnus-thread-total-score (thread)
4224   ;; This function find the total score of THREAD.
4225   (cond
4226    ((null thread)
4227     0)
4228    ((consp thread)
4229     (if (stringp (car thread))
4230         (apply gnus-thread-score-function 0
4231                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4232       (gnus-thread-total-score-1 thread)))
4233    (t
4234     (gnus-thread-total-score-1 (list thread)))))
4235
4236 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4237   "Sort threads such that the thread with the most recently arrived article comes first."
4238   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4239
4240 (defun gnus-thread-highest-number (thread)
4241   "Return the highest article number in THREAD."
4242   (apply 'max (mapcar (lambda (header)
4243                         (mail-header-number header))
4244                       (message-flatten-list thread))))
4245
4246 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4247   "Sort threads such that the thread with the most recently dated article comes first."
4248   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4249
4250 (defun gnus-thread-latest-date (thread)
4251   "Return the highest article date in THREAD."
4252   (let ((previous-time 0))
4253     (apply 'max (mapcar
4254                  (lambda (header)
4255                    (setq previous-time
4256                          (time-to-seconds
4257                           (mail-header-parse-date
4258                            (condition-case ()
4259                                (mail-header-date header)
4260                              (error previous-time))))))
4261                  (sort
4262                   (message-flatten-list thread)
4263                   (lambda (h1 h2)
4264                     (< (mail-header-number h1)
4265                        (mail-header-number h2))))))))
4266
4267 (defun gnus-thread-total-score-1 (root)
4268   ;; This function find the total score of the thread below ROOT.
4269   (setq root (car root))
4270   (apply gnus-thread-score-function
4271          (or (append
4272               (mapcar 'gnus-thread-total-score
4273                       (cdr (gnus-id-to-thread (mail-header-id root))))
4274               (when (> (mail-header-number root) 0)
4275                 (list (or (cdr (assq (mail-header-number root)
4276                                      gnus-newsgroup-scored))
4277                           gnus-summary-default-score 0))))
4278              (list gnus-summary-default-score)
4279              '(0))))
4280
4281 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4282 (defvar gnus-tmp-prev-subject nil)
4283 (defvar gnus-tmp-false-parent nil)
4284 (defvar gnus-tmp-root-expunged nil)
4285 (defvar gnus-tmp-dummy-line nil)
4286
4287 (eval-when-compile (defvar gnus-tmp-header))
4288 (defun gnus-extra-header (type &optional header)
4289   "Return the extra header of TYPE."
4290   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4291       ""))
4292
4293 (defvar gnus-tmp-thread-tree-header-string "")
4294
4295 (defvar gnus-sum-thread-tree-root "> "
4296   "With %B spec, used for the root of a thread.
4297 If nil, use subject instead.")
4298 (defvar gnus-sum-thread-tree-single-indent ""
4299   "With %B spec, used for a thread with just one message.
4300 If nil, use subject instead.")
4301 (defvar gnus-sum-thread-tree-vertical "| "
4302   "With %B spec, used for drawing a vertical line.")
4303 (defvar gnus-sum-thread-tree-indent "  "
4304   "With %B spec, used for indenting.")
4305 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4306   "With %B spec, used for a leaf with brothers.")
4307 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4308   "With %B spec, used for a leaf without brothers.")
4309
4310 (defun gnus-summary-prepare-threads (threads)
4311   "Prepare summary buffer from THREADS and indentation LEVEL.
4312 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4313 or a straight list of headers."
4314   (gnus-message 7 "Generating summary...")
4315
4316   (setq gnus-newsgroup-threads threads)
4317   (beginning-of-line)
4318
4319   (let ((gnus-tmp-level 0)
4320         (default-score (or gnus-summary-default-score 0))
4321         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4322         thread number subject stack state gnus-tmp-gathered beg-match
4323         new-roots gnus-tmp-new-adopts thread-end
4324         gnus-tmp-header gnus-tmp-unread
4325         gnus-tmp-replied gnus-tmp-subject-or-nil
4326         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4327         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4328         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4329         tree-stack)
4330
4331     (setq gnus-tmp-prev-subject nil)
4332
4333     (if (vectorp (car threads))
4334         ;; If this is a straight (sic) list of headers, then a
4335         ;; threaded summary display isn't required, so we just create
4336         ;; an unthreaded one.
4337         (gnus-summary-prepare-unthreaded threads)
4338
4339       ;; Do the threaded display.
4340
4341       (while (or threads stack gnus-tmp-new-adopts new-roots)
4342
4343         (if (and (= gnus-tmp-level 0)
4344                  (or (not stack)
4345                      (= (caar stack) 0))
4346                  (not gnus-tmp-false-parent)
4347                  (or gnus-tmp-new-adopts new-roots))
4348             (if gnus-tmp-new-adopts
4349                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4350                       thread (list (car gnus-tmp-new-adopts))
4351                       gnus-tmp-header (caar thread)
4352                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4353               (when new-roots
4354                 (setq thread (list (car new-roots))
4355                       gnus-tmp-header (caar thread)
4356                       new-roots (cdr new-roots))))
4357
4358           (if threads
4359               ;; If there are some threads, we do them before the
4360               ;; threads on the stack.
4361               (setq thread threads
4362                     gnus-tmp-header (caar thread))
4363             ;; There were no current threads, so we pop something off
4364             ;; the stack.
4365             (setq state (car stack)
4366                   gnus-tmp-level (car state)
4367                   tree-stack (cadr state)
4368                   thread (caddr state)
4369                   stack (cdr stack)
4370                   gnus-tmp-header (caar thread))))
4371
4372         (setq gnus-tmp-false-parent nil)
4373         (setq gnus-tmp-root-expunged nil)
4374         (setq thread-end nil)
4375
4376         (if (stringp gnus-tmp-header)
4377             ;; The header is a dummy root.
4378             (cond
4379              ((eq gnus-summary-make-false-root 'adopt)
4380               ;; We let the first article adopt the rest.
4381               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4382                                                (cddar thread)))
4383               (setq gnus-tmp-gathered
4384                     (nconc (mapcar
4385                             (lambda (h) (mail-header-number (car h)))
4386                             (cddar thread))
4387                            gnus-tmp-gathered))
4388               (setq thread (cons (list (caar thread)
4389                                        (cadar thread))
4390                                  (cdr thread)))
4391               (setq gnus-tmp-level -1
4392                     gnus-tmp-false-parent t))
4393              ((eq gnus-summary-make-false-root 'empty)
4394               ;; We print adopted articles with empty subject fields.
4395               (setq gnus-tmp-gathered
4396                     (nconc (mapcar
4397                             (lambda (h) (mail-header-number (car h)))
4398                             (cddar thread))
4399                            gnus-tmp-gathered))
4400               (setq gnus-tmp-level -1))
4401              ((eq gnus-summary-make-false-root 'dummy)
4402               ;; We remember that we probably want to output a dummy
4403               ;; root.
4404               (setq gnus-tmp-dummy-line gnus-tmp-header)
4405               (setq gnus-tmp-prev-subject gnus-tmp-header))
4406              (t
4407               ;; We do not make a root for the gathered
4408               ;; sub-threads at all.
4409               (setq gnus-tmp-level -1)))
4410
4411           (setq number (mail-header-number gnus-tmp-header)
4412                 subject (mail-header-subject gnus-tmp-header))
4413
4414           (cond
4415            ;; If the thread has changed subject, we might want to make
4416            ;; this subthread into a root.
4417            ((and (null gnus-thread-ignore-subject)
4418                  (not (zerop gnus-tmp-level))
4419                  gnus-tmp-prev-subject
4420                  (not (inline
4421                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4422             (setq new-roots (nconc new-roots (list (car thread)))
4423                   thread-end t
4424                   gnus-tmp-header nil))
4425            ;; If the article lies outside the current limit,
4426            ;; then we do not display it.
4427            ((not (memq number gnus-newsgroup-limit))
4428             (setq gnus-tmp-gathered
4429                   (nconc (mapcar
4430                           (lambda (h) (mail-header-number (car h)))
4431                           (cdar thread))
4432                          gnus-tmp-gathered))
4433             (setq gnus-tmp-new-adopts (if (cdar thread)
4434                                           (append gnus-tmp-new-adopts
4435                                                   (cdar thread))
4436                                         gnus-tmp-new-adopts)
4437                   thread-end t
4438                   gnus-tmp-header nil)
4439             (when (zerop gnus-tmp-level)
4440               (setq gnus-tmp-root-expunged t)))
4441            ;; Perhaps this article is to be marked as read?
4442            ((and gnus-summary-mark-below
4443                  (< (or (cdr (assq number gnus-newsgroup-scored))
4444                         default-score)
4445                     gnus-summary-mark-below)
4446                  ;; Don't touch sparse articles.
4447                  (not (gnus-summary-article-sparse-p number))
4448                  (not (gnus-summary-article-ancient-p number)))
4449             (setq gnus-newsgroup-unreads
4450                   (delq number gnus-newsgroup-unreads))
4451             (if gnus-newsgroup-auto-expire
4452                 (setq gnus-newsgroup-expirable
4453                       (gnus-add-to-sorted-list
4454                        gnus-newsgroup-expirable number))
4455               (push (cons number gnus-low-score-mark)
4456                     gnus-newsgroup-reads))))
4457
4458           (when gnus-tmp-header
4459             ;; We may have an old dummy line to output before this
4460             ;; article.
4461             (when (and gnus-tmp-dummy-line
4462                        (gnus-subject-equal
4463                         gnus-tmp-dummy-line
4464                         (mail-header-subject gnus-tmp-header)))
4465               (gnus-summary-insert-dummy-line
4466                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4467               (setq gnus-tmp-dummy-line nil))
4468
4469             ;; Compute the mark.
4470             (setq gnus-tmp-unread (gnus-article-mark number))
4471
4472             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4473                                   gnus-tmp-header gnus-tmp-level)
4474                   gnus-newsgroup-data)
4475
4476             ;; Actually insert the line.
4477             (setq
4478              gnus-tmp-subject-or-nil
4479              (cond
4480               ((and gnus-thread-ignore-subject
4481                     gnus-tmp-prev-subject
4482                     (not (inline (gnus-subject-equal
4483                                   gnus-tmp-prev-subject subject))))
4484                subject)
4485               ((zerop gnus-tmp-level)
4486                (if (and (eq gnus-summary-make-false-root 'empty)
4487                         (memq number gnus-tmp-gathered)
4488                         gnus-tmp-prev-subject
4489                         (inline (gnus-subject-equal
4490                                  gnus-tmp-prev-subject subject)))
4491                    gnus-summary-same-subject
4492                  subject))
4493               (t gnus-summary-same-subject)))
4494             (if (and (eq gnus-summary-make-false-root 'adopt)
4495                      (= gnus-tmp-level 1)
4496                      (memq number gnus-tmp-gathered))
4497                 (setq gnus-tmp-opening-bracket ?\<
4498                       gnus-tmp-closing-bracket ?\>)
4499               (setq gnus-tmp-opening-bracket ?\[
4500                     gnus-tmp-closing-bracket ?\]))
4501             (setq
4502              gnus-tmp-indentation
4503              (aref gnus-thread-indent-array gnus-tmp-level)
4504              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4505              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4506                                 gnus-summary-default-score 0)
4507              gnus-tmp-score-char
4508              (if (or (null gnus-summary-default-score)
4509                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4510                          gnus-summary-zcore-fuzz))
4511                  ?\ ;;;Whitespace
4512                (if (< gnus-tmp-score gnus-summary-default-score)
4513                    gnus-score-below-mark gnus-score-over-mark))
4514              gnus-tmp-replied
4515              (cond ((memq number gnus-newsgroup-processable)
4516                     gnus-process-mark)
4517                    ((memq number gnus-newsgroup-cached)
4518                     gnus-cached-mark)
4519                    ((memq number gnus-newsgroup-replied)
4520                     gnus-replied-mark)
4521                    ((memq number gnus-newsgroup-forwarded)
4522                     gnus-forwarded-mark)
4523                    ((memq number gnus-newsgroup-saved)
4524                     gnus-saved-mark)
4525                    ((memq number gnus-newsgroup-recent)
4526                     gnus-recent-mark)
4527                    ((memq number gnus-newsgroup-unseen)
4528                     gnus-unseen-mark)
4529                    (t gnus-no-mark))
4530              gnus-tmp-from (mail-header-from gnus-tmp-header)
4531              gnus-tmp-name
4532              (cond
4533               ((string-match "<[^>]+> *$" gnus-tmp-from)
4534                (setq beg-match (match-beginning 0))
4535                (or (and (string-match "^\".+\"" gnus-tmp-from)
4536                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4537                    (substring gnus-tmp-from 0 beg-match)))
4538               ((string-match "(.+)" gnus-tmp-from)
4539                (substring gnus-tmp-from
4540                           (1+ (match-beginning 0)) (1- (match-end 0))))
4541               (t gnus-tmp-from))
4542              gnus-tmp-thread-tree-header-string
4543              (cond
4544               ((not gnus-show-threads) "")
4545               ((zerop gnus-tmp-level)
4546                (if (cdar thread)
4547                    (or gnus-sum-thread-tree-root subject)
4548                  (or gnus-sum-thread-tree-single-indent subject)))
4549               (t
4550                (concat (apply 'concat
4551                               (mapcar (lambda (item)
4552                                         (if (= item 1)
4553                                             gnus-sum-thread-tree-vertical
4554                                           gnus-sum-thread-tree-indent))
4555                                       (cdr (reverse tree-stack))))
4556                        (if (nth 1 thread)
4557                            gnus-sum-thread-tree-leaf-with-other
4558                          gnus-sum-thread-tree-single-leaf)))))
4559             (when (string= gnus-tmp-name "")
4560               (setq gnus-tmp-name gnus-tmp-from))
4561             (unless (numberp gnus-tmp-lines)
4562               (setq gnus-tmp-lines -1))
4563             (if (= gnus-tmp-lines -1)
4564                 (setq gnus-tmp-lines "?")
4565               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4566             (gnus-put-text-property
4567              (point)
4568              (progn (eval gnus-summary-line-format-spec) (point))
4569              'gnus-number number)
4570             (when gnus-visual-p
4571               (forward-line -1)
4572               (gnus-run-hooks 'gnus-summary-update-hook)
4573               (forward-line 1))
4574
4575             (setq gnus-tmp-prev-subject subject)))
4576
4577         (when (nth 1 thread)
4578           (push (list (max 0 gnus-tmp-level)
4579                       (copy-list tree-stack)
4580                       (nthcdr 1 thread))
4581                 stack))
4582         (push (if (nth 1 thread) 1 0) tree-stack)
4583         (incf gnus-tmp-level)
4584         (setq threads (if thread-end nil (cdar thread)))
4585         (unless threads
4586           (setq gnus-tmp-level 0)))))
4587   (gnus-message 7 "Generating summary...done"))
4588
4589 (defun gnus-summary-prepare-unthreaded (headers)
4590   "Generate an unthreaded summary buffer based on HEADERS."
4591   (let (header number mark)
4592
4593     (beginning-of-line)
4594
4595     (while headers
4596       ;; We may have to root out some bad articles...
4597       (when (memq (setq number (mail-header-number
4598                                 (setq header (pop headers))))
4599                   gnus-newsgroup-limit)
4600         ;; Mark article as read when it has a low score.
4601         (when (and gnus-summary-mark-below
4602                    (< (or (cdr (assq number gnus-newsgroup-scored))
4603                           gnus-summary-default-score 0)
4604                       gnus-summary-mark-below)
4605                    (not (gnus-summary-article-ancient-p number)))
4606           (setq gnus-newsgroup-unreads
4607                 (delq number gnus-newsgroup-unreads))
4608           (if gnus-newsgroup-auto-expire
4609               (push number gnus-newsgroup-expirable)
4610             (push (cons number gnus-low-score-mark)
4611                   gnus-newsgroup-reads)))
4612
4613         (setq mark (gnus-article-mark number))
4614         (push (gnus-data-make number mark (1+ (point)) header 0)
4615               gnus-newsgroup-data)
4616         (gnus-summary-insert-line
4617          header 0 number
4618          mark (memq number gnus-newsgroup-replied)
4619          (memq number gnus-newsgroup-expirable)
4620          (mail-header-subject header) nil
4621          (cdr (assq number gnus-newsgroup-scored))
4622          (memq number gnus-newsgroup-processable))))))
4623
4624 (defun gnus-summary-remove-list-identifiers ()
4625   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4626   (let ((regexp (if (consp gnus-list-identifiers)
4627                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4628                   gnus-list-identifiers))
4629         changed subject)
4630     (when regexp
4631       (dolist (header gnus-newsgroup-headers)
4632         (setq subject (mail-header-subject header)
4633               changed nil)
4634         (while (string-match
4635                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4636                 subject)
4637           (setq subject
4638                 (concat (substring subject 0 (match-beginning 2))
4639                         (substring subject (match-end 0)))
4640                 changed t))
4641         (when (and changed
4642                    (string-match
4643                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4644           (setq subject
4645                 (concat (substring subject 0 (match-beginning 1))
4646                         (substring subject (match-end 1)))))
4647         (when changed
4648           (mail-header-set-subject header subject))))))
4649
4650 (defun gnus-fetch-headers (articles)
4651   "Fetch headers of ARTICLES."
4652   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4653     (gnus-message 5 "Fetching headers for %s..." name)
4654     (prog1
4655         (if (eq 'nov
4656                 (setq gnus-headers-retrieved-by
4657                       (gnus-retrieve-headers
4658                        articles gnus-newsgroup-name
4659                        ;; We might want to fetch old headers, but
4660                        ;; not if there is only 1 article.
4661                        (and (or (and
4662                                  (not (eq gnus-fetch-old-headers 'some))
4663                                  (not (numberp gnus-fetch-old-headers)))
4664                                 (> (length articles) 1))
4665                             gnus-fetch-old-headers))))
4666             (gnus-get-newsgroup-headers-xover
4667              articles nil nil gnus-newsgroup-name t)
4668           (gnus-get-newsgroup-headers))
4669       (gnus-message 5 "Fetching headers for %s...done" name))))
4670
4671 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4672   "Select newsgroup GROUP.
4673 If READ-ALL is non-nil, all articles in the group are selected.
4674 If SELECT-ARTICLES, only select those articles from GROUP."
4675   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4676          ;;!!! Dirty hack; should be removed.
4677          (gnus-summary-ignore-duplicates
4678           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4679               t
4680             gnus-summary-ignore-duplicates))
4681          (info (nth 2 entry))
4682          articles fetched-articles cached)
4683
4684     (unless (gnus-check-server
4685              (set (make-local-variable 'gnus-current-select-method)
4686                   (gnus-find-method-for-group group)))
4687       (error "Couldn't open server"))
4688
4689     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4690         (gnus-activate-group group)     ; Or we can activate it...
4691         (progn                          ; Or we bug out.
4692           (when (equal major-mode 'gnus-summary-mode)
4693             (kill-buffer (current-buffer)))
4694           (error "Couldn't activate group %s: %s"
4695                  group (gnus-status-message group))))
4696
4697     (unless (gnus-request-group group t)
4698       (when (equal major-mode 'gnus-summary-mode)
4699         (kill-buffer (current-buffer)))
4700       (error "Couldn't request group %s: %s"
4701              group (gnus-status-message group)))
4702
4703     (setq gnus-newsgroup-name group
4704           gnus-newsgroup-unselected nil
4705           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4706
4707     (let ((display (gnus-group-find-parameter group 'display)))
4708       (setq gnus-newsgroup-display
4709             (cond
4710              ((not (zerop (or (car-safe read-all) 0)))
4711               ;; The user entered the group with C-u SPC/RET, let's show
4712               ;; all articles.
4713               'gnus-not-ignore)
4714              ((eq display 'all)
4715               'gnus-not-ignore)
4716              ((arrayp display)
4717               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4718              ((numberp display)
4719               ;; The following is probably the "correct" solution, but
4720               ;; it makes Gnus fetch all headers and then limit the
4721               ;; articles (which is slow), so instead we hack the
4722               ;; select-articles parameter instead. -- Simon Josefsson
4723               ;; <jas@kth.se>
4724               ;;
4725               ;; (gnus-byte-compile
4726               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4727               ;;                         display)))))
4728               (setq select-articles
4729                     (gnus-uncompress-range
4730                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4731                              (if (> tmp 0)
4732                                  tmp
4733                                1))
4734                            (cdr (gnus-active group)))))
4735               nil)
4736              (t
4737               nil))))
4738
4739     (gnus-summary-setup-default-charset)
4740
4741     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4742     (when (gnus-virtual-group-p group)
4743       (setq cached gnus-newsgroup-cached))
4744
4745     (setq gnus-newsgroup-unreads
4746           (gnus-sorted-ndifference
4747            (gnus-sorted-ndifference gnus-newsgroup-unreads
4748                                     gnus-newsgroup-marked)
4749            gnus-newsgroup-dormant))
4750
4751     (setq gnus-newsgroup-processable nil)
4752
4753     (gnus-update-read-articles group gnus-newsgroup-unreads)
4754
4755     ;; Adjust and set lists of article marks.
4756     (when info
4757       (gnus-adjust-marked-articles info))
4758
4759     (if (setq articles select-articles)
4760         (setq gnus-newsgroup-unselected
4761               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4762       (setq articles (gnus-articles-to-read group read-all)))
4763
4764     (cond
4765      ((null articles)
4766       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4767       'quit)
4768      ((eq articles 0) nil)
4769      (t
4770       ;; Init the dependencies hash table.
4771       (setq gnus-newsgroup-dependencies
4772             (gnus-make-hashtable (length articles)))
4773       (gnus-set-global-variables)
4774       ;; Retrieve the headers and read them in.
4775       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4776
4777       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4778       (when cached
4779         (setq gnus-newsgroup-cached cached))
4780
4781       ;; Suppress duplicates?
4782       (when gnus-suppress-duplicates
4783         (gnus-dup-suppress-articles))
4784
4785       ;; Set the initial limit.
4786       (setq gnus-newsgroup-limit (copy-sequence articles))
4787       ;; Remove canceled articles from the list of unread articles.
4788       (setq fetched-articles
4789             (mapcar (lambda (headers) (mail-header-number headers))
4790                     gnus-newsgroup-headers))
4791       (setq gnus-newsgroup-articles fetched-articles)
4792       (setq gnus-newsgroup-unreads
4793             (gnus-sorted-nintersection
4794              gnus-newsgroup-unreads fetched-articles))
4795       (gnus-compute-unseen-list)
4796
4797       ;; Removed marked articles that do not exist.
4798       (gnus-update-missing-marks
4799        (gnus-sorted-difference articles fetched-articles))
4800       ;; We might want to build some more threads first.
4801       (when (and gnus-fetch-old-headers
4802                  (eq gnus-headers-retrieved-by 'nov))
4803         (if (eq gnus-fetch-old-headers 'invisible)
4804             (gnus-build-all-threads)
4805           (gnus-build-old-threads)))
4806       ;; Let the Gnus agent mark articles as read.
4807       (when gnus-agent
4808         (gnus-agent-get-undownloaded-list))
4809       ;; Remove list identifiers from subject
4810       (when gnus-list-identifiers
4811         (gnus-summary-remove-list-identifiers))
4812       ;; Check whether auto-expire is to be done in this group.
4813       (setq gnus-newsgroup-auto-expire
4814             (gnus-group-auto-expirable-p group))
4815       ;; Set up the article buffer now, if necessary.
4816       (unless gnus-single-article-buffer
4817         (gnus-article-setup-buffer))
4818       ;; First and last article in this newsgroup.
4819       (when gnus-newsgroup-headers
4820         (setq gnus-newsgroup-begin
4821               (mail-header-number (car gnus-newsgroup-headers))
4822               gnus-newsgroup-end
4823               (mail-header-number
4824                (gnus-last-element gnus-newsgroup-headers))))
4825       ;; GROUP is successfully selected.
4826       (or gnus-newsgroup-headers t)))))
4827
4828 (defun gnus-compute-unseen-list ()
4829   ;; The `seen' marks are treated specially.
4830   (if (not gnus-newsgroup-seen)
4831       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4832     (setq gnus-newsgroup-unseen
4833           (gnus-inverse-list-range-intersection
4834            gnus-newsgroup-articles gnus-newsgroup-seen))))
4835
4836 (defun gnus-summary-display-make-predicate (display)
4837   (require 'gnus-agent)
4838   (when (= (length display) 1)
4839     (setq display (car display)))
4840   (unless gnus-summary-display-cache
4841     (dolist (elem (append (list (cons 'read 'read)
4842                                 (cons 'unseen 'unseen))
4843                           gnus-article-mark-lists))
4844       (push (cons (cdr elem)
4845                   (gnus-byte-compile
4846                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4847             gnus-summary-display-cache)))
4848   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4849     (gnus-get-predicate display)))
4850
4851 ;; Uses the dynamically bound `number' variable.
4852 (defvar number)
4853 (defun gnus-article-marked-p (type &optional article)
4854   (let ((article (or article number)))
4855     (cond
4856      ((eq type 'tick)
4857       (memq article gnus-newsgroup-marked))
4858      ((eq type 'unsend)
4859       (memq article gnus-newsgroup-unsendable))
4860      ((eq type 'undownload)
4861       (memq article gnus-newsgroup-undownloaded))
4862      ((eq type 'download)
4863       (memq article gnus-newsgroup-downloadable))
4864      ((eq type 'unread)
4865       (memq article gnus-newsgroup-unreads))
4866      ((eq type 'read)
4867       (memq article gnus-newsgroup-reads))
4868      ((eq type 'dormant)
4869       (memq article gnus-newsgroup-dormant) )
4870      ((eq type 'expire)
4871       (memq article gnus-newsgroup-expirable))
4872      ((eq type 'reply)
4873       (memq article gnus-newsgroup-replied))
4874      ((eq type 'killed)
4875       (memq article gnus-newsgroup-killed))
4876      ((eq type 'bookmark)
4877       (assq article gnus-newsgroup-bookmarks))
4878      ((eq type 'score)
4879       (assq article gnus-newsgroup-scored))
4880      ((eq type 'save)
4881       (memq article gnus-newsgroup-saved))
4882      ((eq type 'cache)
4883       (memq article gnus-newsgroup-cached))
4884      ((eq type 'forward)
4885       (memq article gnus-newsgroup-forwarded))
4886      ((eq type 'seen)
4887       (not (memq article gnus-newsgroup-unseen)))
4888      ((eq type 'recent)
4889       (memq article gnus-newsgroup-recent))
4890      (t t))))
4891
4892 (defun gnus-articles-to-read (group &optional read-all)
4893   "Find out what articles the user wants to read."
4894   (let* ((articles
4895           ;; Select all articles if `read-all' is non-nil, or if there
4896           ;; are no unread articles.
4897           (if (or read-all
4898                   (and (zerop (length gnus-newsgroup-marked))
4899                        (zerop (length gnus-newsgroup-unreads)))
4900                   ;; Fetch all if the predicate is non-nil.
4901                   gnus-newsgroup-display)
4902               ;; We want to select the headers for all the articles in
4903               ;; the group, so we select either all the active
4904               ;; articles in the group, or (if that's nil), the
4905               ;; articles in the cache.
4906               (or
4907                (gnus-uncompress-range (gnus-active group))
4908                (gnus-cache-articles-in-group group))
4909             ;; Select only the "normal" subset of articles.
4910             (gnus-sorted-nunion
4911              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4912              gnus-newsgroup-unreads)))
4913          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4914          (scored (length scored-list))
4915          (number (length articles))
4916          (marked (+ (length gnus-newsgroup-marked)
4917                     (length gnus-newsgroup-dormant)))
4918          (select
4919           (cond
4920            ((numberp read-all)
4921             read-all)
4922            ((numberp gnus-newsgroup-display)
4923             gnus-newsgroup-display)
4924            (t
4925             (condition-case ()
4926                 (cond
4927                  ((and (or (<= scored marked) (= scored number))
4928                        (natnump gnus-large-newsgroup)
4929                        (> number gnus-large-newsgroup))
4930                   (let* ((cursor-in-echo-area nil)
4931                          (input
4932                           (read-from-minibuffer
4933                            (format
4934                             "How many articles from %s (max %d): "
4935                             (gnus-limit-string
4936                              (gnus-group-decoded-name gnus-newsgroup-name)
4937                              35)
4938                             number)
4939                            (cons (number-to-string gnus-large-newsgroup)
4940                                  0))))
4941                     (if (string-match "^[ \t]*$" input)
4942                         number
4943                       input)))
4944                  ((and (> scored marked) (< scored number)
4945                        (> (- scored number) 20))
4946                   (let ((input
4947                          (read-string
4948                           (format "%s %s (%d scored, %d total): "
4949                                   "How many articles from"
4950                                   (gnus-group-decoded-name group)
4951                                   scored number))))
4952                     (if (string-match "^[ \t]*$" input)
4953                         number input)))
4954                  (t number))
4955               (quit
4956                (message "Quit getting the articles to read")
4957                nil))))))
4958     (setq select (if (stringp select) (string-to-number select) select))
4959     (if (or (null select) (zerop select))
4960         select
4961       (if (and (not (zerop scored)) (<= (abs select) scored))
4962           (progn
4963             (setq articles (sort scored-list '<))
4964             (setq number (length articles)))
4965         (setq articles (copy-sequence articles)))
4966
4967       (when (< (abs select) number)
4968         (if (< select 0)
4969             ;; Select the N oldest articles.
4970             (setcdr (nthcdr (1- (abs select)) articles) nil)
4971           ;; Select the N most recent articles.
4972           (setq articles (nthcdr (- number select) articles))))
4973       (setq gnus-newsgroup-unselected
4974             (gnus-sorted-difference gnus-newsgroup-unreads articles))
4975       (when gnus-alter-articles-to-read-function
4976         (setq gnus-newsgroup-unreads
4977               (sort
4978                (funcall gnus-alter-articles-to-read-function
4979                         gnus-newsgroup-name gnus-newsgroup-unreads)
4980                '<)))
4981       articles)))
4982
4983 (defun gnus-killed-articles (killed articles)
4984   (let (out)
4985     (while articles
4986       (when (inline (gnus-member-of-range (car articles) killed))
4987         (push (car articles) out))
4988       (setq articles (cdr articles)))
4989     out))
4990
4991 (defun gnus-uncompress-marks (marks)
4992   "Uncompress the mark ranges in MARKS."
4993   (let ((uncompressed '(score bookmark))
4994         out)
4995     (while marks
4996       (if (memq (caar marks) uncompressed)
4997           (push (car marks) out)
4998         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4999       (setq marks (cdr marks)))
5000     out))
5001
5002 (defun gnus-article-mark-to-type (mark)
5003   "Return the type of MARK."
5004   (or (cadr (assq mark gnus-article-special-mark-lists))
5005       'list))
5006
5007 (defun gnus-article-unpropagatable-p (mark)
5008   "Return whether MARK should be propagated to backend."
5009   (memq mark gnus-article-unpropagated-mark-lists))
5010
5011 (defun gnus-adjust-marked-articles (info)
5012   "Set all article lists and remove all marks that are no longer valid."
5013   (let* ((marked-lists (gnus-info-marks info))
5014          (active (gnus-active (gnus-info-group info)))
5015          (min (car active))
5016          (max (cdr active))
5017          (types gnus-article-mark-lists)
5018          marks var articles article mark mark-type)
5019
5020     (dolist (marks marked-lists)
5021       (setq mark (car marks)
5022             mark-type (gnus-article-mark-to-type mark)
5023             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5024
5025       ;; We set the variable according to the type of the marks list,
5026       ;; and then adjust the marks to a subset of the active articles.
5027       (cond
5028        ;; Adjust "simple" lists.
5029        ((eq mark-type 'list)
5030         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5031         (when (memq mark '(tick dormant expire reply save))
5032           (while articles
5033             (when (or (< (setq article (pop articles)) min) (> article max))
5034               (set var (delq article (symbol-value var)))))))
5035        ;; Adjust assocs.
5036        ((eq mark-type 'tuple)
5037         (set var (setq articles (cdr marks)))
5038         (when (not (listp (cdr (symbol-value var))))
5039           (set var (list (symbol-value var))))
5040         (when (not (listp (cdr articles)))
5041           (setq articles (list articles)))
5042         (while articles
5043           (when (or (not (consp (setq article (pop articles))))
5044                     (< (car article) min)
5045                     (> (car article) max))
5046             (set var (delq article (symbol-value var))))))
5047        ;; Adjust ranges (sloppily).
5048        ((eq mark-type 'range)
5049         (cond
5050          ((eq mark 'seen)
5051           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5052           ;; It should be (seen (NUM1 . NUM2)).
5053           (when (numberp (cddr marks))
5054             (setcdr marks (list (cdr marks))))
5055           (setq articles (cdr marks))
5056           (while (and articles
5057                       (or (and (consp (car articles))
5058                                (> min (cdar articles)))
5059                           (and (numberp (car articles))
5060                                (> min (car articles)))))
5061             (pop articles))
5062           (set var articles))))))))
5063
5064 (defun gnus-update-missing-marks (missing)
5065   "Go through the list of MISSING articles and remove them from the mark lists."
5066   (when missing
5067     (let (var m)
5068       ;; Go through all types.
5069       (dolist (elem gnus-article-mark-lists)
5070         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5071           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5072           (when (symbol-value var)
5073             ;; This list has articles.  So we delete all missing
5074             ;; articles from it.
5075             (setq m missing)
5076             (while m
5077               (set var (delq (pop m) (symbol-value var))))))))))
5078
5079 (defun gnus-update-marks ()
5080   "Enter the various lists of marked articles into the newsgroup info list."
5081   (let ((types gnus-article-mark-lists)
5082         (info (gnus-get-info gnus-newsgroup-name))
5083         type list newmarked symbol delta-marks)
5084     (when info
5085       ;; Add all marks lists to the list of marks lists.
5086       (while (setq type (pop types))
5087         (setq list (symbol-value
5088                     (setq symbol
5089                           (intern (format "gnus-newsgroup-%s" (car type))))))
5090
5091         (when list
5092           ;; Get rid of the entries of the articles that have the
5093           ;; default score.
5094           (when (and (eq (cdr type) 'score)
5095                      gnus-save-score
5096                      list)
5097             (let* ((arts list)
5098                    (prev (cons nil list))
5099                    (all prev))
5100               (while arts
5101                 (if (or (not (consp (car arts)))
5102                         (= (cdar arts) gnus-summary-default-score))
5103                     (setcdr prev (cdr arts))
5104                   (setq prev arts))
5105                 (setq arts (cdr arts)))
5106               (setq list (cdr all)))))
5107
5108         (when (eq (cdr type) 'seen)
5109           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5110
5111         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5112           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5113
5114         (when (and (gnus-check-backend-function
5115                     'request-set-mark gnus-newsgroup-name)
5116                    (not (gnus-article-unpropagatable-p (cdr type))))
5117           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5118                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5119                  (add (gnus-remove-from-range
5120                        (gnus-copy-sequence list) old)))
5121             (when add
5122               (push (list add 'add (list (cdr type))) delta-marks))
5123             (when del
5124               (push (list del 'del (list (cdr type))) delta-marks))))
5125
5126         (when list
5127           (push (cons (cdr type) list) newmarked)))
5128
5129       (when delta-marks
5130         (unless (gnus-check-group gnus-newsgroup-name)
5131           (error "Can't open server for %s" gnus-newsgroup-name))
5132         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5133
5134       ;; Enter these new marks into the info of the group.
5135       (if (nthcdr 3 info)
5136           (setcar (nthcdr 3 info) newmarked)
5137         ;; Add the marks lists to the end of the info.
5138         (when newmarked
5139           (setcdr (nthcdr 2 info) (list newmarked))))
5140
5141       ;; Cut off the end of the info if there's nothing else there.
5142       (let ((i 5))
5143         (while (and (> i 2)
5144                     (not (nth i info)))
5145           (when (nthcdr (decf i) info)
5146             (setcdr (nthcdr i info) nil)))))))
5147
5148 (defun gnus-set-mode-line (where)
5149   "Set the mode line of the article or summary buffers.
5150 If WHERE is `summary', the summary mode line format will be used."
5151   ;; Is this mode line one we keep updated?
5152   (when (and (memq where gnus-updated-mode-lines)
5153              (symbol-value
5154               (intern (format "gnus-%s-mode-line-format-spec" where))))
5155     (let (mode-string)
5156       (save-excursion
5157         ;; We evaluate this in the summary buffer since these
5158         ;; variables are buffer-local to that buffer.
5159         (set-buffer gnus-summary-buffer)
5160         ;; We bind all these variables that are used in the `eval' form
5161         ;; below.
5162         (let* ((mformat (symbol-value
5163                          (intern
5164                           (format "gnus-%s-mode-line-format-spec" where))))
5165                (gnus-tmp-group-name (gnus-group-decoded-name
5166                                      gnus-newsgroup-name))
5167                (gnus-tmp-article-number (or gnus-current-article 0))
5168                (gnus-tmp-unread gnus-newsgroup-unreads)
5169                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5170                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5171                (gnus-tmp-unread-and-unselected
5172                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5173                             (zerop gnus-tmp-unselected))
5174                        "")
5175                       ((zerop gnus-tmp-unselected)
5176                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5177                       (t (format "{%d(+%d) more}"
5178                                  gnus-tmp-unread-and-unticked
5179                                  gnus-tmp-unselected))))
5180                (gnus-tmp-subject
5181                 (if (and gnus-current-headers
5182                          (vectorp gnus-current-headers))
5183                     (gnus-mode-string-quote
5184                      (mail-header-subject gnus-current-headers))
5185                   ""))
5186                bufname-length max-len
5187                gnus-tmp-header);; passed as argument to any user-format-funcs
5188           (setq mode-string (eval mformat))
5189           (setq bufname-length (if (string-match "%b" mode-string)
5190                                    (- (length
5191                                        (buffer-name
5192                                         (if (eq where 'summary)
5193                                             nil
5194                                           (get-buffer gnus-article-buffer))))
5195                                       2)
5196                                  0))
5197           (setq max-len (max 4 (if gnus-mode-non-string-length
5198                                    (- (window-width)
5199                                       gnus-mode-non-string-length
5200                                       bufname-length)
5201                                  (length mode-string))))
5202           ;; We might have to chop a bit of the string off...
5203           (when (> (length mode-string) max-len)
5204             (setq mode-string
5205                   (concat (gnus-truncate-string mode-string (- max-len 3))
5206                           "...")))
5207           ;; Pad the mode string a bit.
5208           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5209       ;; Update the mode line.
5210       (setq mode-line-buffer-identification
5211             (gnus-mode-line-buffer-identification (list mode-string)))
5212       (set-buffer-modified-p t))))
5213
5214 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5215   "Go through the HEADERS list and add all Xrefs to a hash table.
5216 The resulting hash table is returned, or nil if no Xrefs were found."
5217   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5218          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5219          (xref-hashtb (gnus-make-hashtable))
5220          start group entry number xrefs header)
5221     (while headers
5222       (setq header (pop headers))
5223       (when (and (setq xrefs (mail-header-xref header))
5224                  (not (memq (setq number (mail-header-number header))
5225                             unreads)))
5226         (setq start 0)
5227         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5228           (setq start (match-end 0))
5229           (setq group (if prefix
5230                           (concat prefix (substring xrefs (match-beginning 1)
5231                                                     (match-end 1)))
5232                         (substring xrefs (match-beginning 1) (match-end 1))))
5233           (setq number
5234                 (string-to-int (substring xrefs (match-beginning 2)
5235                                           (match-end 2))))
5236           (if (setq entry (gnus-gethash group xref-hashtb))
5237               (setcdr entry (cons number (cdr entry)))
5238             (gnus-sethash group (cons number nil) xref-hashtb)))))
5239     (and start xref-hashtb)))
5240
5241 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5242   "Look through all the headers and mark the Xrefs as read."
5243   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5244         name entry info xref-hashtb idlist method nth4)
5245     (save-excursion
5246       (set-buffer gnus-group-buffer)
5247       (when (setq xref-hashtb
5248                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5249         (mapatoms
5250          (lambda (group)
5251            (unless (string= from-newsgroup (setq name (symbol-name group)))
5252              (setq idlist (symbol-value group))
5253              ;; Dead groups are not updated.
5254              (and (prog1
5255                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5256                             info (nth 2 entry))
5257                     (when (stringp (setq nth4 (gnus-info-method info)))
5258                       (setq nth4 (gnus-server-to-method nth4))))
5259                   ;; Only do the xrefs if the group has the same
5260                   ;; select method as the group we have just read.
5261                   (or (gnus-methods-equal-p
5262                        nth4 (gnus-find-method-for-group from-newsgroup))
5263                       virtual
5264                       (equal nth4 (setq method (gnus-find-method-for-group
5265                                                 from-newsgroup)))
5266                       (and (equal (car nth4) (car method))
5267                            (equal (nth 1 nth4) (nth 1 method))))
5268                   gnus-use-cross-reference
5269                   (or (not (eq gnus-use-cross-reference t))
5270                       virtual
5271                       ;; Only do cross-references on subscribed
5272                       ;; groups, if that is what is wanted.
5273                       (<= (gnus-info-level info) gnus-level-subscribed))
5274                   (gnus-group-make-articles-read name idlist))))
5275          xref-hashtb)))))
5276
5277 (defun gnus-compute-read-articles (group articles)
5278   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5279          (info (nth 2 entry))
5280          (active (gnus-active group))
5281          ninfo)
5282     (when entry
5283       ;; First peel off all invalid article numbers.
5284       (when active
5285         (let ((ids articles)
5286               id first)
5287           (while (setq id (pop ids))
5288             (when (and first (> id (cdr active)))
5289               ;; We'll end up in this situation in one particular
5290               ;; obscure situation.  If you re-scan a group and get
5291               ;; a new article that is cross-posted to a different
5292               ;; group that has not been re-scanned, you might get
5293               ;; crossposted article that has a higher number than
5294               ;; Gnus believes possible.  So we re-activate this
5295               ;; group as well.  This might mean doing the
5296               ;; crossposting thingy will *increase* the number
5297               ;; of articles in some groups.  Tsk, tsk.
5298               (setq active (or (gnus-activate-group group) active)))
5299             (when (or (> id (cdr active))
5300                       (< id (car active)))
5301               (setq articles (delq id articles))))))
5302       ;; If the read list is nil, we init it.
5303       (if (and active
5304                (null (gnus-info-read info))
5305                (> (car active) 1))
5306           (setq ninfo (cons 1 (1- (car active))))
5307         (setq ninfo (gnus-info-read info)))
5308       ;; Then we add the read articles to the range.
5309       (gnus-add-to-range
5310        ninfo (setq articles (sort articles '<))))))
5311
5312 (defun gnus-group-make-articles-read (group articles)
5313   "Update the info of GROUP to say that ARTICLES are read."
5314   (let* ((num 0)
5315          (entry (gnus-gethash group gnus-newsrc-hashtb))
5316          (info (nth 2 entry))
5317          (active (gnus-active group))
5318          range)
5319     (when entry
5320       (setq range (gnus-compute-read-articles group articles))
5321       (save-excursion
5322         (set-buffer gnus-group-buffer)
5323         (gnus-undo-register
5324           `(progn
5325              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5326              (gnus-info-set-read ',info ',(gnus-info-read info))
5327              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5328              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5329              (gnus-group-update-group ,group t))))
5330       ;; Add the read articles to the range.
5331       (gnus-info-set-read info range)
5332       (gnus-request-set-mark group (list (list range 'add '(read))))
5333       ;; Then we have to re-compute how many unread
5334       ;; articles there are in this group.
5335       (when active
5336         (cond
5337          ((not range)
5338           (setq num (- (1+ (cdr active)) (car active))))
5339          ((not (listp (cdr range)))
5340           (setq num (- (cdr active) (- (1+ (cdr range))
5341                                        (car range)))))
5342          (t
5343           (while range
5344             (if (numberp (car range))
5345                 (setq num (1+ num))
5346               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5347             (setq range (cdr range)))
5348           (setq num (- (cdr active) num))))
5349         ;; Update the number of unread articles.
5350         (setcar entry num)
5351         ;; Update the group buffer.
5352         (gnus-group-update-group group t)))))
5353
5354 (defvar gnus-newsgroup-none-id 0)
5355
5356 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5357   (let ((cur nntp-server-buffer)
5358         (dependencies
5359          (or dependencies
5360              (save-excursion (set-buffer gnus-summary-buffer)
5361                              gnus-newsgroup-dependencies)))
5362         headers id end ref
5363         (mail-parse-charset gnus-newsgroup-charset)
5364         (mail-parse-ignored-charsets
5365          (save-excursion (condition-case nil
5366                              (set-buffer gnus-summary-buffer)
5367                            (error))
5368                          gnus-newsgroup-ignored-charsets)))
5369     (save-excursion
5370       (set-buffer nntp-server-buffer)
5371       ;; Translate all TAB characters into SPACE characters.
5372       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5373       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5374       (gnus-run-hooks 'gnus-parse-headers-hook)
5375       (let ((case-fold-search t)
5376             in-reply-to header p lines chars ctype)
5377         (goto-char (point-min))
5378         ;; Search to the beginning of the next header.  Error messages
5379         ;; do not begin with 2 or 3.
5380         (while (re-search-forward "^[23][0-9]+ " nil t)
5381           (setq id nil
5382                 ref nil)
5383           ;; This implementation of this function, with nine
5384           ;; search-forwards instead of the one re-search-forward and
5385           ;; a case (which basically was the old function) is actually
5386           ;; about twice as fast, even though it looks messier.  You
5387           ;; can't have everything, I guess.  Speed and elegance
5388           ;; doesn't always go hand in hand.
5389           (setq
5390            header
5391            (make-full-mail-header
5392             ;; Number.
5393             (prog1
5394                 (read cur)
5395               (end-of-line)
5396               (setq p (point))
5397               (narrow-to-region (point)
5398                                 (or (and (search-forward "\n.\n" nil t)
5399                                          (- (point) 2))
5400                                     (point))))
5401             ;; Subject.
5402             (progn
5403               (goto-char p)
5404               (if (search-forward "\nsubject:" nil t)
5405                   (nnheader-header-value)
5406                 "(none)"))
5407             ;; From.
5408             (progn
5409               (goto-char p)
5410               (if (search-forward "\nfrom:" nil t)
5411                   (nnheader-header-value)
5412                 "(nobody)"))
5413             ;; Date.
5414             (progn
5415               (goto-char p)
5416               (if (search-forward "\ndate:" nil t)
5417                   (nnheader-header-value) ""))
5418             ;; Message-ID.
5419             (progn
5420               (goto-char p)
5421               (setq id (if (re-search-forward
5422                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5423                            ;; We do it this way to make sure the Message-ID
5424                            ;; is (somewhat) syntactically valid.
5425                            (buffer-substring (match-beginning 1)
5426                                              (match-end 1))
5427                          ;; If there was no message-id, we just fake one
5428                          ;; to make subsequent routines simpler.
5429                          (nnheader-generate-fake-message-id))))
5430             ;; References.
5431             (progn
5432               (goto-char p)
5433               (if (search-forward "\nreferences:" nil t)
5434                   (progn
5435                     (setq end (point))
5436                     (prog1
5437                         (nnheader-header-value)
5438                       (setq ref
5439                             (buffer-substring
5440                              (progn
5441                                ;; (end-of-line)
5442                                (search-backward ">" end t)
5443                                (1+ (point)))
5444                              (progn
5445                                (search-backward "<" end t)
5446                                (point))))))
5447                 ;; Get the references from the in-reply-to header if there
5448                 ;; were no references and the in-reply-to header looks
5449                 ;; promising.
5450                 (if (and (search-forward "\nin-reply-to:" nil t)
5451                          (setq in-reply-to (nnheader-header-value))
5452                          (string-match "<[^>]+>" in-reply-to))
5453                     (let (ref2)
5454                       (setq ref (substring in-reply-to (match-beginning 0)
5455                                            (match-end 0)))
5456                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5457                         (setq ref2 (substring in-reply-to (match-beginning 0)
5458                                               (match-end 0)))
5459                         (when (> (length ref2) (length ref))
5460                           (setq ref ref2)))
5461                       ref)
5462                   (setq ref nil))))
5463             ;; Chars.
5464             (progn
5465               (goto-char p)
5466               (if (search-forward "\nchars: " nil t)
5467                   (if (numberp (setq chars (ignore-errors (read cur))))
5468                       chars -1)
5469                 -1))
5470             ;; Lines.
5471             (progn
5472               (goto-char p)
5473               (if (search-forward "\nlines: " nil t)
5474                   (if (numberp (setq lines (ignore-errors (read cur))))
5475                       lines -1)
5476                 -1))
5477             ;; Xref.
5478             (progn
5479               (goto-char p)
5480               (and (search-forward "\nxref:" nil t)
5481                    (nnheader-header-value)))
5482             ;; Extra.
5483             (when gnus-extra-headers
5484               (let ((extra gnus-extra-headers)
5485                     out)
5486                 (while extra
5487                   (goto-char p)
5488                   (when (search-forward
5489                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5490                     (push (cons (car extra) (nnheader-header-value)) out))
5491                   (pop extra))
5492                 out))))
5493           (goto-char p)
5494           (if (and (search-forward "\ncontent-type: " nil t)
5495                    (setq ctype (nnheader-header-value)))
5496               (mime-entity-set-content-type-internal
5497                header (mime-parse-Content-Type ctype)))
5498           (when (equal id ref)
5499             (setq ref nil))
5500
5501           (when gnus-alter-header-function
5502             (funcall gnus-alter-header-function header)
5503             (setq id (mail-header-id header)
5504                   ref (gnus-parent-id (mail-header-references header))))
5505
5506           (when (setq header
5507                       (gnus-dependencies-add-header
5508                        header dependencies force-new))
5509             (push header headers))
5510           (goto-char (point-max))
5511           (widen))
5512         (nreverse headers)))))
5513
5514 ;; Goes through the xover lines and returns a list of vectors
5515 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5516                                                   force-new dependencies
5517                                                   group also-fetch-heads)
5518   "Parse the news overview data in the server buffer.
5519 Return a list of headers that match SEQUENCE (see
5520 `nntp-retrieve-headers')."
5521   ;; Get the Xref when the users reads the articles since most/some
5522   ;; NNTP servers do not include Xrefs when using XOVER.
5523   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5524   (let ((mail-parse-charset gnus-newsgroup-charset)
5525         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5526         (cur nntp-server-buffer)
5527         (dependencies (or dependencies gnus-newsgroup-dependencies))
5528         (allp (cond
5529                ((eq gnus-read-all-available-headers t)
5530                 t)
5531                ((stringp gnus-read-all-available-headers)
5532                 (string-match gnus-read-all-available-headers group))
5533                (t
5534                 nil)))
5535         number headers header)
5536     (save-excursion
5537       (set-buffer nntp-server-buffer)
5538       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5539       ;; Allow the user to mangle the headers before parsing them.
5540       (gnus-run-hooks 'gnus-parse-headers-hook)
5541       (goto-char (point-min))
5542       (gnus-parse-without-error
5543         (while (and (or sequence allp)
5544                     (not (eobp)))
5545           (setq number (read cur))
5546           (when (not allp)
5547             (while (and sequence
5548                         (< (car sequence) number))
5549               (setq sequence (cdr sequence))))
5550           (when (and (or allp
5551                          (and sequence
5552                               (eq number (car sequence))))
5553                      (progn
5554                        (setq sequence (cdr sequence))
5555                        (setq header (inline
5556                                       (gnus-nov-parse-line
5557                                        number dependencies force-new)))))
5558             (push header headers))
5559           (forward-line 1)))
5560       ;; A common bug in inn is that if you have posted an article and
5561       ;; then retrieves the active file, it will answer correctly --
5562       ;; the new article is included.  However, a NOV entry for the
5563       ;; article may not have been generated yet, so this may fail.
5564       ;; We work around this problem by retrieving the last few
5565       ;; headers using HEAD.
5566       (if (or (not also-fetch-heads)
5567               (not sequence))
5568           ;; We (probably) got all the headers.
5569           (nreverse headers)
5570         (let ((gnus-nov-is-evil t))
5571           (nconc
5572            (nreverse headers)
5573            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5574              (gnus-get-newsgroup-headers))))))))
5575
5576 (defun gnus-article-get-xrefs ()
5577   "Fill in the Xref value in `gnus-current-headers', if necessary.
5578 This is meant to be called in `gnus-article-internal-prepare-hook'."
5579   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5580                                  gnus-current-headers)))
5581     (or (not gnus-use-cross-reference)
5582         (not headers)
5583         (and (mail-header-xref headers)
5584              (not (string= (mail-header-xref headers) "")))
5585         (let ((case-fold-search t)
5586               xref)
5587           (save-restriction
5588             (nnheader-narrow-to-headers)
5589             (goto-char (point-min))
5590             (when (or (and (not (eobp))
5591                            (eq (downcase (char-after)) ?x)
5592                            (looking-at "Xref:"))
5593                       (search-forward "\nXref:" nil t))
5594               (goto-char (1+ (match-end 0)))
5595               (setq xref (buffer-substring (point)
5596                                            (progn (end-of-line) (point))))
5597               (mail-header-set-xref headers xref)))))))
5598
5599 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5600   "Find article ID and insert the summary line for that article.
5601 OLD-HEADER can either be a header or a line number to insert
5602 the subject line on."
5603   (let* ((line (and (numberp old-header) old-header))
5604          (old-header (and (vectorp old-header) old-header))
5605          (header (cond ((and old-header use-old-header)
5606                         old-header)
5607                        ((and (numberp id)
5608                              (gnus-number-to-header id))
5609                         (gnus-number-to-header id))
5610                        (t
5611                         (gnus-read-header id))))
5612          (number (and (numberp id) id))
5613          d)
5614     (when header
5615       ;; Rebuild the thread that this article is part of and go to the
5616       ;; article we have fetched.
5617       (when (and (not gnus-show-threads)
5618                  old-header)
5619         (when (and number
5620                    (setq d (gnus-data-find (mail-header-number old-header))))
5621           (goto-char (gnus-data-pos d))
5622           (gnus-data-remove
5623            number
5624            (- (gnus-point-at-bol)
5625               (prog1
5626                   (1+ (gnus-point-at-eol))
5627                 (gnus-delete-line))))))
5628       (when old-header
5629         (mail-header-set-number header (mail-header-number old-header)))
5630       (setq gnus-newsgroup-sparse
5631             (delq (setq number (mail-header-number header))
5632                   gnus-newsgroup-sparse))
5633       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5634       (push number gnus-newsgroup-limit)
5635       (gnus-rebuild-thread (mail-header-id header) line)
5636       (gnus-summary-goto-subject number nil t))
5637     (when (and (numberp number)
5638                (> number 0))
5639       ;; We have to update the boundaries even if we can't fetch the
5640       ;; article if ID is a number -- so that the next `P' or `N'
5641       ;; command will fetch the previous (or next) article even
5642       ;; if the one we tried to fetch this time has been canceled.
5643       (when (> number gnus-newsgroup-end)
5644         (setq gnus-newsgroup-end number))
5645       (when (< number gnus-newsgroup-begin)
5646         (setq gnus-newsgroup-begin number))
5647       (setq gnus-newsgroup-unselected
5648             (delq number gnus-newsgroup-unselected)))
5649     ;; Report back a success?
5650     (and header (mail-header-number header))))
5651
5652 ;;; Process/prefix in the summary buffer
5653
5654 (defun gnus-summary-work-articles (n)
5655   "Return a list of articles to be worked upon.
5656 The prefix argument, the list of process marked articles, and the
5657 current article will be taken into consideration."
5658   (save-excursion
5659     (set-buffer gnus-summary-buffer)
5660     (cond
5661      (n
5662       ;; A numerical prefix has been given.
5663       (setq n (prefix-numeric-value n))
5664       (let ((backward (< n 0))
5665             (n (abs (prefix-numeric-value n)))
5666             articles article)
5667         (save-excursion
5668           (while
5669               (and (> n 0)
5670                    (push (setq article (gnus-summary-article-number))
5671                          articles)
5672                    (if backward
5673                        (gnus-summary-find-prev nil article)
5674                      (gnus-summary-find-next nil article)))
5675             (decf n)))
5676         (nreverse articles)))
5677      ((and (gnus-region-active-p) (mark))
5678       (message "region active")
5679       ;; Work on the region between point and mark.
5680       (let ((max (max (point) (mark)))
5681             articles article)
5682         (save-excursion
5683           (goto-char (min (point) (mark)))
5684           (while
5685               (and
5686                (push (setq article (gnus-summary-article-number)) articles)
5687                (gnus-summary-find-next nil article)
5688                (< (point) max)))
5689           (nreverse articles))))
5690      (gnus-newsgroup-processable
5691       ;; There are process-marked articles present.
5692       ;; Save current state.
5693       (gnus-summary-save-process-mark)
5694       ;; Return the list.
5695       (reverse gnus-newsgroup-processable))
5696      (t
5697       ;; Just return the current article.
5698       (list (gnus-summary-article-number))))))
5699
5700 (defmacro gnus-summary-iterate (arg &rest forms)
5701   "Iterate over the process/prefixed articles and do FORMS.
5702 ARG is the interactive prefix given to the command.  FORMS will be
5703 executed with point over the summary line of the articles."
5704   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5705     `(let ((,articles (gnus-summary-work-articles ,arg)))
5706        (while ,articles
5707          (gnus-summary-goto-subject (car ,articles))
5708          ,@forms
5709          (pop ,articles)))))
5710
5711 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5712 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5713
5714 (defun gnus-summary-save-process-mark ()
5715   "Push the current set of process marked articles on the stack."
5716   (interactive)
5717   (push (copy-sequence gnus-newsgroup-processable)
5718         gnus-newsgroup-process-stack))
5719
5720 (defun gnus-summary-kill-process-mark ()
5721   "Push the current set of process marked articles on the stack and unmark."
5722   (interactive)
5723   (gnus-summary-save-process-mark)
5724   (gnus-summary-unmark-all-processable))
5725
5726 (defun gnus-summary-yank-process-mark ()
5727   "Pop the last process mark state off the stack and restore it."
5728   (interactive)
5729   (unless gnus-newsgroup-process-stack
5730     (error "Empty mark stack"))
5731   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5732
5733 (defun gnus-summary-process-mark-set (set)
5734   "Make SET into the current process marked articles."
5735   (gnus-summary-unmark-all-processable)
5736   (while set
5737     (gnus-summary-set-process-mark (pop set))))
5738
5739 ;;; Searching and stuff
5740
5741 (defun gnus-summary-search-group (&optional backward use-level)
5742   "Search for next unread newsgroup.
5743 If optional argument BACKWARD is non-nil, search backward instead."
5744   (save-excursion
5745     (set-buffer gnus-group-buffer)
5746     (when (gnus-group-search-forward
5747            backward nil (if use-level (gnus-group-group-level) nil))
5748       (gnus-group-group-name))))
5749
5750 (defun gnus-summary-best-group (&optional exclude-group)
5751   "Find the name of the best unread group.
5752 If EXCLUDE-GROUP, do not go to this group."
5753   (save-excursion
5754     (set-buffer gnus-group-buffer)
5755     (save-excursion
5756       (gnus-group-best-unread-group exclude-group))))
5757
5758 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5759   (if backward (gnus-summary-find-prev)
5760     (let* ((dummy (gnus-summary-article-intangible-p))
5761            (article (or article (gnus-summary-article-number)))
5762            (arts (gnus-data-find-list article))
5763            result)
5764       (when (and (not dummy)
5765                  (or (not gnus-summary-check-current)
5766                      (not unread)
5767                      (not (gnus-data-unread-p (car arts)))))
5768         (setq arts (cdr arts)))
5769       (when (setq result
5770                   (if unread
5771                       (progn
5772                         (while arts
5773                           (when (or (and undownloaded
5774                                          (eq gnus-undownloaded-mark
5775                                              (gnus-data-mark (car arts))))
5776                                     (gnus-data-unread-p (car arts)))
5777                             (setq result (car arts)
5778                                   arts nil))
5779                           (setq arts (cdr arts)))
5780                         result)
5781                     (car arts)))
5782         (goto-char (gnus-data-pos result))
5783         (gnus-data-number result)))))
5784
5785 (defun gnus-summary-find-prev (&optional unread article)
5786   (let* ((eobp (eobp))
5787          (article (or article (gnus-summary-article-number)))
5788          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5789          result)
5790     (when (and (not eobp)
5791                (or (not gnus-summary-check-current)
5792                    (not unread)
5793                    (not (gnus-data-unread-p (car arts)))))
5794       (setq arts (cdr arts)))
5795     (when (setq result
5796                 (if unread
5797                     (progn
5798                       (while arts
5799                         (when (gnus-data-unread-p (car arts))
5800                           (setq result (car arts)
5801                                 arts nil))
5802                         (setq arts (cdr arts)))
5803                       result)
5804                   (car arts)))
5805       (goto-char (gnus-data-pos result))
5806       (gnus-data-number result))))
5807
5808 (defun gnus-summary-find-subject (subject &optional unread backward article)
5809   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5810          (article (or article (gnus-summary-article-number)))
5811          (articles (gnus-data-list backward))
5812          (arts (gnus-data-find-list article articles))
5813          result)
5814     (when (or (not gnus-summary-check-current)
5815               (not unread)
5816               (not (gnus-data-unread-p (car arts))))
5817       (setq arts (cdr arts)))
5818     (while arts
5819       (and (or (not unread)
5820                (gnus-data-unread-p (car arts)))
5821            (vectorp (gnus-data-header (car arts)))
5822            (gnus-subject-equal
5823             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5824            (setq result (car arts)
5825                  arts nil))
5826       (setq arts (cdr arts)))
5827     (and result
5828          (goto-char (gnus-data-pos result))
5829          (gnus-data-number result))))
5830
5831 (defun gnus-summary-search-forward (&optional unread subject backward)
5832   "Search forward for an article.
5833 If UNREAD, look for unread articles.  If SUBJECT, look for
5834 articles with that subject.  If BACKWARD, search backward instead."
5835   (cond (subject (gnus-summary-find-subject subject unread backward))
5836         (backward (gnus-summary-find-prev unread))
5837         (t (gnus-summary-find-next unread))))
5838
5839 (defun gnus-recenter (&optional n)
5840   "Center point in window and redisplay frame.
5841 Also do horizontal recentering."
5842   (interactive "P")
5843   (when (and gnus-auto-center-summary
5844              (not (eq gnus-auto-center-summary 'vertical)))
5845     (gnus-horizontal-recenter))
5846   (recenter n))
5847
5848 (defun gnus-summary-recenter ()
5849   "Center point in the summary window.
5850 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5851 displayed, no centering will be performed."
5852   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5853   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5854   (interactive)
5855   (let* ((top (cond ((< (window-height) 4) 0)
5856                     ((< (window-height) 7) 1)
5857                     (t (if (numberp gnus-auto-center-summary)
5858                            gnus-auto-center-summary
5859                          2))))
5860          (height (1- (window-height)))
5861          (bottom (save-excursion (goto-char (point-max))
5862                                  (forward-line (- height))
5863                                  (point)))
5864          (window (get-buffer-window (current-buffer))))
5865     ;; The user has to want it.
5866     (when gnus-auto-center-summary
5867       (when (get-buffer-window gnus-article-buffer)
5868         ;; Only do recentering when the article buffer is displayed,
5869         ;; Set the window start to either `bottom', which is the biggest
5870         ;; possible valid number, or the second line from the top,
5871         ;; whichever is the least.
5872         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5873           (if (> bottom top-pos)
5874               ;; Keep the second line from the top visible
5875               (set-window-start window top-pos t)
5876             ;; Try to keep the bottom line visible; if it's partially
5877             ;; obscured, either scroll one more line to make it fully
5878             ;; visible, or revert to using TOP-POS.
5879             (save-excursion
5880               (goto-char (point-max))
5881               (forward-line -1)
5882               (let ((last-line-start (point)))
5883                 (goto-char bottom)
5884                 (set-window-start window (point) t)
5885                 (when (not (pos-visible-in-window-p last-line-start window))
5886                   (forward-line 1)
5887                   (set-window-start window (min (point) top-pos) t)))))))
5888       ;; Do horizontal recentering while we're at it.
5889       (when (and (get-buffer-window (current-buffer) t)
5890                  (not (eq gnus-auto-center-summary 'vertical)))
5891         (let ((selected (selected-window)))
5892           (select-window (get-buffer-window (current-buffer) t))
5893           (gnus-summary-position-point)
5894           (gnus-horizontal-recenter)
5895           (select-window selected))))))
5896
5897 (defun gnus-summary-jump-to-group (newsgroup)
5898   "Move point to NEWSGROUP in group mode buffer."
5899   ;; Keep update point of group mode buffer if visible.
5900   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5901       (save-window-excursion
5902         ;; Take care of tree window mode.
5903         (when (get-buffer-window gnus-group-buffer)
5904           (pop-to-buffer gnus-group-buffer))
5905         (gnus-group-jump-to-group newsgroup))
5906     (save-excursion
5907       ;; Take care of tree window mode.
5908       (if (get-buffer-window gnus-group-buffer)
5909           (pop-to-buffer gnus-group-buffer)
5910         (set-buffer gnus-group-buffer))
5911       (gnus-group-jump-to-group newsgroup))))
5912
5913 ;; This function returns a list of article numbers based on the
5914 ;; difference between the ranges of read articles in this group and
5915 ;; the range of active articles.
5916 (defun gnus-list-of-unread-articles (group)
5917   (let* ((read (gnus-info-read (gnus-get-info group)))
5918          (active (or (gnus-active group) (gnus-activate-group group)))
5919          (last (cdr active))
5920          first nlast unread)
5921     ;; If none are read, then all are unread.
5922     (if (not read)
5923         (setq first (car active))
5924       ;; If the range of read articles is a single range, then the
5925       ;; first unread article is the article after the last read
5926       ;; article.  Sounds logical, doesn't it?
5927       (if (and (not (listp (cdr read)))
5928                (or (< (car read) (car active))
5929                    (progn (setq read (list read))
5930                           nil)))
5931           (setq first (max (car active) (1+ (cdr read))))
5932         ;; `read' is a list of ranges.
5933         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5934                                   (caar read)))
5935                   1)
5936           (setq first (car active)))
5937         (while read
5938           (when first
5939             (while (< first nlast)
5940               (push first unread)
5941               (setq first (1+ first))))
5942           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5943           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5944           (setq read (cdr read)))))
5945     ;; And add the last unread articles.
5946     (while (<= first last)
5947       (push first unread)
5948       (setq first (1+ first)))
5949     ;; Return the list of unread articles.
5950     (delq 0 (nreverse unread))))
5951
5952 (defun gnus-list-of-read-articles (group)
5953   "Return a list of unread, unticked and non-dormant articles."
5954   (let* ((info (gnus-get-info group))
5955          (marked (gnus-info-marks info))
5956          (active (gnus-active group)))
5957     (and info active
5958          (gnus-list-range-difference
5959           (gnus-list-range-difference
5960            (gnus-sorted-complement
5961             (gnus-uncompress-range active)
5962             (gnus-list-of-unread-articles group))
5963            (cdr (assq 'dormant marked)))
5964           (cdr (assq 'tick marked))))))
5965
5966 ;; Various summary commands
5967
5968 (defun gnus-summary-select-article-buffer ()
5969   "Reconfigure windows to show article buffer."
5970   (interactive)
5971   (if (not (gnus-buffer-live-p gnus-article-buffer))
5972       (error "There is no article buffer for this summary buffer")
5973     (gnus-configure-windows 'article)
5974     (select-window (get-buffer-window gnus-article-buffer))))
5975
5976 (defun gnus-summary-universal-argument (arg)
5977   "Perform any operation on all articles that are process/prefixed."
5978   (interactive "P")
5979   (let ((articles (gnus-summary-work-articles arg))
5980         func article)
5981     (if (eq
5982          (setq
5983           func
5984           (key-binding
5985            (read-key-sequence
5986             (substitute-command-keys
5987              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5988          'undefined)
5989         (gnus-error 1 "Undefined key")
5990       (save-excursion
5991         (while articles
5992           (gnus-summary-goto-subject (setq article (pop articles)))
5993           (let (gnus-newsgroup-processable)
5994             (command-execute func))
5995           (gnus-summary-remove-process-mark article)))))
5996   (gnus-summary-position-point))
5997
5998 (defun gnus-summary-toggle-truncation (&optional arg)
5999   "Toggle truncation of summary lines.
6000 With arg, turn line truncation on iff arg is positive."
6001   (interactive "P")
6002   (setq truncate-lines
6003         (if (null arg) (not truncate-lines)
6004           (> (prefix-numeric-value arg) 0)))
6005   (redraw-display))
6006
6007 (defun gnus-summary-reselect-current-group (&optional all rescan)
6008   "Exit and then reselect the current newsgroup.
6009 The prefix argument ALL means to select all articles."
6010   (interactive "P")
6011   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6012     (error "Ephemeral groups can't be reselected"))
6013   (let ((current-subject (gnus-summary-article-number))
6014         (group gnus-newsgroup-name))
6015     (setq gnus-newsgroup-begin nil)
6016     (gnus-summary-exit)
6017     ;; We have to adjust the point of group mode buffer because
6018     ;; point was moved to the next unread newsgroup by exiting.
6019     (gnus-summary-jump-to-group group)
6020     (when rescan
6021       (save-excursion
6022         (save-window-excursion
6023           ;; Don't show group contents.
6024           (set-window-start (selected-window) (point-max))
6025           (gnus-group-get-new-news-this-group 1))))
6026     (gnus-group-read-group all t)
6027     (gnus-summary-goto-subject current-subject nil t)))
6028
6029 (defun gnus-summary-rescan-group (&optional all)
6030   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6031   (interactive "P")
6032   (gnus-summary-reselect-current-group all t))
6033
6034 (defun gnus-summary-update-info (&optional non-destructive)
6035   (save-excursion
6036     (let ((group gnus-newsgroup-name))
6037       (when group
6038         (when gnus-newsgroup-kill-headers
6039           (setq gnus-newsgroup-killed
6040                 (gnus-compress-sequence
6041                  (gnus-sorted-union
6042                   (gnus-list-range-intersection
6043                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6044                   gnus-newsgroup-unreads)
6045                  t)))
6046         (unless (listp (cdr gnus-newsgroup-killed))
6047           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6048         (let ((headers gnus-newsgroup-headers))
6049           ;; Set the new ranges of read articles.
6050           (save-excursion
6051             (set-buffer gnus-group-buffer)
6052             (gnus-undo-force-boundary))
6053           (gnus-update-read-articles
6054            group (gnus-sorted-union
6055                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6056           ;; Set the current article marks.
6057           (let ((gnus-newsgroup-scored
6058                  (if (and (not gnus-save-score)
6059                           (not non-destructive))
6060                      nil
6061                    gnus-newsgroup-scored)))
6062             (save-excursion
6063               (gnus-update-marks)))
6064           ;; Do the cross-ref thing.
6065           (when gnus-use-cross-reference
6066             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6067           ;; Do not switch windows but change the buffer to work.
6068           (set-buffer gnus-group-buffer)
6069           (unless (gnus-ephemeral-group-p group)
6070             (gnus-group-update-group group)))))))
6071
6072 (defun gnus-summary-save-newsrc (&optional force)
6073   "Save the current number of read/marked articles in the dribble buffer.
6074 The dribble buffer will then be saved.
6075 If FORCE (the prefix), also save the .newsrc file(s)."
6076   (interactive "P")
6077   (gnus-summary-update-info t)
6078   (if force
6079       (gnus-save-newsrc-file)
6080     (gnus-dribble-save)))
6081
6082 (defun gnus-summary-exit (&optional temporary)
6083   "Exit reading current newsgroup, and then return to group selection mode.
6084 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6085   (interactive)
6086   (gnus-set-global-variables)
6087   (gnus-kill-save-kill-buffer)
6088   (gnus-async-halt-prefetch)
6089   (let* ((group gnus-newsgroup-name)
6090          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6091          (mode major-mode)
6092          (group-point nil)
6093          (buf (current-buffer)))
6094     (unless quit-config
6095       ;; Do adaptive scoring, and possibly save score files.
6096       (when gnus-newsgroup-adaptive
6097         (gnus-score-adaptive))
6098       (when gnus-use-scoring
6099         (gnus-score-save)))
6100     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6101     ;; If we have several article buffers, we kill them at exit.
6102     (unless gnus-single-article-buffer
6103       (gnus-kill-buffer gnus-original-article-buffer)
6104       (setq gnus-article-current nil))
6105     (when gnus-use-cache
6106       (gnus-cache-possibly-remove-articles)
6107       (gnus-cache-save-buffers))
6108     (gnus-async-prefetch-remove-group group)
6109     (when gnus-suppress-duplicates
6110       (gnus-dup-enter-articles))
6111     (when gnus-use-trees
6112       (gnus-tree-close group))
6113     (when gnus-use-cache
6114       (gnus-cache-write-active))
6115     ;; Remove entries for this group.
6116     (nnmail-purge-split-history (gnus-group-real-name group))
6117     ;; Make all changes in this group permanent.
6118     (unless quit-config
6119       (gnus-run-hooks 'gnus-exit-group-hook)
6120       (gnus-summary-update-info))
6121     (gnus-close-group group)
6122     ;; Make sure where we were, and go to next newsgroup.
6123     (set-buffer gnus-group-buffer)
6124     (unless quit-config
6125       (gnus-group-jump-to-group group))
6126     (gnus-run-hooks 'gnus-summary-exit-hook)
6127     (unless (or quit-config
6128                 ;; If this group has disappeared from the summary
6129                 ;; buffer, don't skip forwards.
6130                 (not (string= group (gnus-group-group-name))))
6131       (gnus-group-next-unread-group 1))
6132     (setq group-point (point))
6133     (if temporary
6134         nil                             ;Nothing to do.
6135       ;; If we have several article buffers, we kill them at exit.
6136       (unless gnus-single-article-buffer
6137         (gnus-kill-buffer gnus-article-buffer)
6138         (gnus-kill-buffer gnus-original-article-buffer)
6139         (setq gnus-article-current nil))
6140       (set-buffer buf)
6141       (if (not gnus-kill-summary-on-exit)
6142           (progn
6143             (gnus-deaden-summary)
6144             (setq mode nil))
6145         ;; We set all buffer-local variables to nil.  It is unclear why
6146         ;; this is needed, but if we don't, buffer-local variables are
6147         ;; not garbage-collected, it seems.  This would the lead to en
6148         ;; ever-growing Emacs.
6149         (gnus-summary-clear-local-variables)
6150         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6151           (gnus-summary-clear-local-variables))
6152         (when (get-buffer gnus-article-buffer)
6153           (bury-buffer gnus-article-buffer))
6154         ;; We clear the global counterparts of the buffer-local
6155         ;; variables as well, just to be on the safe side.
6156         (set-buffer gnus-group-buffer)
6157         (gnus-summary-clear-local-variables)
6158         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6159           (gnus-summary-clear-local-variables)))
6160       (setq gnus-current-select-method gnus-select-method)
6161       (pop-to-buffer gnus-group-buffer)
6162       (if (not quit-config)
6163           (progn
6164             (goto-char group-point)
6165             (gnus-configure-windows 'group 'force)
6166             (unless (pos-visible-in-window-p)
6167               (forward-line (/ (static-if (featurep 'xemacs)
6168                                    (window-displayed-height)
6169                                  (1- (window-height)))
6170                                -2))
6171               (set-window-start (selected-window) (point))
6172               (goto-char group-point)))
6173         (gnus-handle-ephemeral-exit quit-config))
6174       ;; Return to group mode buffer.
6175       (when (eq mode 'gnus-summary-mode)
6176         (gnus-kill-buffer buf))
6177       ;; Clear the current group name.
6178       (unless quit-config
6179         (setq gnus-newsgroup-name nil)))))
6180
6181 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6182 (defun gnus-summary-exit-no-update (&optional no-questions)
6183   "Quit reading current newsgroup without updating read article info."
6184   (interactive)
6185   (let* ((group gnus-newsgroup-name)
6186          (quit-config (gnus-group-quit-config group)))
6187     (when (or no-questions
6188               gnus-expert-user
6189               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6190       (gnus-async-halt-prefetch)
6191       (mapcar 'funcall
6192               (delq 'gnus-summary-expire-articles
6193                     (copy-sequence gnus-summary-prepare-exit-hook)))
6194       ;; If we have several article buffers, we kill them at exit.
6195       (unless gnus-single-article-buffer
6196         (gnus-kill-buffer gnus-article-buffer)
6197         (gnus-kill-buffer gnus-original-article-buffer)
6198         (setq gnus-article-current nil))
6199       (if (not gnus-kill-summary-on-exit)
6200           (gnus-deaden-summary)
6201         (gnus-close-group group)
6202         (gnus-summary-clear-local-variables)
6203         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6204           (gnus-summary-clear-local-variables))
6205         (set-buffer gnus-group-buffer)
6206         (gnus-summary-clear-local-variables)
6207         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6208           (gnus-summary-clear-local-variables))
6209         (when (get-buffer gnus-summary-buffer)
6210           (kill-buffer gnus-summary-buffer)))
6211       (unless gnus-single-article-buffer
6212         (setq gnus-article-current nil))
6213       (when gnus-use-trees
6214         (gnus-tree-close group))
6215       (gnus-async-prefetch-remove-group group)
6216       (when (get-buffer gnus-article-buffer)
6217         (bury-buffer gnus-article-buffer))
6218       ;; Return to the group buffer.
6219       (gnus-configure-windows 'group 'force)
6220       ;; Clear the current group name.
6221       (setq gnus-newsgroup-name nil)
6222       (when (equal (gnus-group-group-name) group)
6223         (gnus-group-next-unread-group 1))
6224       (when quit-config
6225         (gnus-handle-ephemeral-exit quit-config)))))
6226
6227 (defun gnus-handle-ephemeral-exit (quit-config)
6228   "Handle movement when leaving an ephemeral group.
6229 The state which existed when entering the ephemeral is reset."
6230   (if (not (buffer-name (car quit-config)))
6231       (gnus-configure-windows 'group 'force)
6232     (set-buffer (car quit-config))
6233     (cond ((eq major-mode 'gnus-summary-mode)
6234            (gnus-set-global-variables))
6235           ((eq major-mode 'gnus-article-mode)
6236            (save-excursion
6237              ;; The `gnus-summary-buffer' variable may point
6238              ;; to the old summary buffer when using a single
6239              ;; article buffer.
6240              (unless (gnus-buffer-live-p gnus-summary-buffer)
6241                (set-buffer gnus-group-buffer))
6242              (set-buffer gnus-summary-buffer)
6243              (gnus-set-global-variables))))
6244     (if (or (eq (cdr quit-config) 'article)
6245             (eq (cdr quit-config) 'pick))
6246         (progn
6247           ;; The current article may be from the ephemeral group
6248           ;; thus it is best that we reload this article
6249           (gnus-summary-show-article)
6250           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6251               (gnus-configure-windows 'pick 'force)
6252             (gnus-configure-windows (cdr quit-config) 'force)))
6253       (gnus-configure-windows (cdr quit-config) 'force))
6254     (when (eq major-mode 'gnus-summary-mode)
6255       (gnus-summary-next-subject 1 nil t)
6256       (gnus-summary-recenter)
6257       (gnus-summary-position-point))))
6258
6259 (defun gnus-summary-preview-mime-message ()
6260   "MIME decode and play this message."
6261   (interactive)
6262   (let ((gnus-break-pages nil)
6263         (gnus-show-mime t))
6264     (gnus-summary-select-article gnus-show-all-headers t))
6265   (let ((w (get-buffer-window gnus-article-buffer)))
6266     (when w
6267       (select-window (get-buffer-window gnus-article-buffer)))))
6268
6269 ;;; Dead summaries.
6270
6271 (defvar gnus-dead-summary-mode-map nil)
6272
6273 (unless gnus-dead-summary-mode-map
6274   (setq gnus-dead-summary-mode-map (make-keymap))
6275   (suppress-keymap gnus-dead-summary-mode-map)
6276   (substitute-key-definition
6277    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6278   (dolist (key '("\C-d" "\r" "\177" [delete]))
6279     (define-key gnus-dead-summary-mode-map
6280       key 'gnus-summary-wake-up-the-dead))
6281   (dolist (key '("q" "Q"))
6282     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6283
6284 (defvar gnus-dead-summary-mode nil
6285   "Minor mode for Gnus summary buffers.")
6286
6287 (defun gnus-dead-summary-mode (&optional arg)
6288   "Minor mode for Gnus summary buffers."
6289   (interactive "P")
6290   (when (eq major-mode 'gnus-summary-mode)
6291     (make-local-variable 'gnus-dead-summary-mode)
6292     (setq gnus-dead-summary-mode
6293           (if (null arg) (not gnus-dead-summary-mode)
6294             (> (prefix-numeric-value arg) 0)))
6295     (when gnus-dead-summary-mode
6296       (gnus-add-minor-mode
6297        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6298
6299 (defun gnus-deaden-summary ()
6300   "Make the current summary buffer into a dead summary buffer."
6301   ;; Kill any previous dead summary buffer.
6302   (when (and gnus-dead-summary
6303              (buffer-name gnus-dead-summary))
6304     (save-excursion
6305       (set-buffer gnus-dead-summary)
6306       (when gnus-dead-summary-mode
6307         (kill-buffer (current-buffer)))))
6308   ;; Make this the current dead summary.
6309   (setq gnus-dead-summary (current-buffer))
6310   (gnus-dead-summary-mode 1)
6311   (let ((name (buffer-name)))
6312     (when (string-match "Summary" name)
6313       (rename-buffer
6314        (concat (substring name 0 (match-beginning 0)) "Dead "
6315                (substring name (match-beginning 0)))
6316        t)
6317       (bury-buffer))))
6318
6319 (defun gnus-kill-or-deaden-summary (buffer)
6320   "Kill or deaden the summary BUFFER."
6321   (save-excursion
6322     (when (and (buffer-name buffer)
6323                (not gnus-single-article-buffer))
6324       (save-excursion
6325         (set-buffer buffer)
6326         (gnus-kill-buffer gnus-article-buffer)
6327         (gnus-kill-buffer gnus-original-article-buffer)))
6328     (cond
6329      ;; Kill the buffer.
6330      (gnus-kill-summary-on-exit
6331       (when (and gnus-use-trees
6332                  (gnus-buffer-exists-p buffer))
6333         (save-excursion
6334           (set-buffer buffer)
6335           (gnus-tree-close gnus-newsgroup-name)))
6336       (gnus-kill-buffer buffer))
6337      ;; Deaden the buffer.
6338      ((gnus-buffer-exists-p buffer)
6339       (save-excursion
6340         (set-buffer buffer)
6341         (gnus-deaden-summary))))))
6342
6343 (defun gnus-summary-wake-up-the-dead (&rest args)
6344   "Wake up the dead summary buffer."
6345   (interactive)
6346   (gnus-dead-summary-mode -1)
6347   (let ((name (buffer-name)))
6348     (when (string-match "Dead " name)
6349       (rename-buffer
6350        (concat (substring name 0 (match-beginning 0))
6351                (substring name (match-end 0)))
6352        t)))
6353   (gnus-message 3 "This dead summary is now alive again"))
6354
6355 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6356 (defun gnus-summary-fetch-faq (&optional faq-dir)
6357   "Fetch the FAQ for the current group.
6358 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6359 in."
6360   (interactive
6361    (list
6362     (when current-prefix-arg
6363       (completing-read
6364        "Faq dir: " (and (listp gnus-group-faq-directory)
6365                         (mapcar (lambda (file) (list file))
6366                                 gnus-group-faq-directory))))))
6367   (let (gnus-faq-buffer)
6368     (when (setq gnus-faq-buffer
6369                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6370       (gnus-configure-windows 'summary-faq))))
6371
6372 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6373 (defun gnus-summary-describe-group (&optional force)
6374   "Describe the current newsgroup."
6375   (interactive "P")
6376   (gnus-group-describe-group force gnus-newsgroup-name))
6377
6378 (defun gnus-summary-describe-briefly ()
6379   "Describe summary mode commands briefly."
6380   (interactive)
6381   (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")))
6382
6383 ;; Walking around group mode buffer from summary mode.
6384
6385 (defun gnus-summary-next-group (&optional no-article target-group backward)
6386   "Exit current newsgroup and then select next unread newsgroup.
6387 If prefix argument NO-ARTICLE is non-nil, no article is selected
6388 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6389 previous group instead."
6390   (interactive "P")
6391   ;; Stop pre-fetching.
6392   (gnus-async-halt-prefetch)
6393   (let ((current-group gnus-newsgroup-name)
6394         (current-buffer (current-buffer))
6395         entered)
6396     ;; First we semi-exit this group to update Xrefs and all variables.
6397     ;; We can't do a real exit, because the window conf must remain
6398     ;; the same in case the user is prompted for info, and we don't
6399     ;; want the window conf to change before that...
6400     (gnus-summary-exit t)
6401     (while (not entered)
6402       ;; Then we find what group we are supposed to enter.
6403       (set-buffer gnus-group-buffer)
6404       (gnus-group-jump-to-group current-group)
6405       (setq target-group
6406             (or target-group
6407                 (if (eq gnus-keep-same-level 'best)
6408                     (gnus-summary-best-group gnus-newsgroup-name)
6409                   (gnus-summary-search-group backward gnus-keep-same-level))))
6410       (if (not target-group)
6411           ;; There are no further groups, so we return to the group
6412           ;; buffer.
6413           (progn
6414             (gnus-message 5 "Returning to the group buffer")
6415             (setq entered t)
6416             (when (gnus-buffer-live-p current-buffer)
6417               (set-buffer current-buffer)
6418               (gnus-summary-exit))
6419             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6420         ;; We try to enter the target group.
6421         (gnus-group-jump-to-group target-group)
6422         (let ((unreads (gnus-group-group-unread)))
6423           (if (and (or (eq t unreads)
6424                        (and unreads (not (zerop unreads))))
6425                    (gnus-summary-read-group
6426                     target-group nil no-article
6427                     (and (buffer-name current-buffer) current-buffer)
6428                     nil backward))
6429               (setq entered t)
6430             (setq current-group target-group
6431                   target-group nil)))))))
6432
6433 (defun gnus-summary-prev-group (&optional no-article)
6434   "Exit current newsgroup and then select previous unread newsgroup.
6435 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6436   (interactive "P")
6437   (gnus-summary-next-group no-article nil t))
6438
6439 ;; Walking around summary lines.
6440
6441 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6442   "Go to the first unread subject.
6443 If UNREAD is non-nil, go to the first unread article.
6444 Returns the article selected or nil if there are no unread articles."
6445   (interactive "P")
6446   (prog1
6447       (cond
6448        ;; Empty summary.
6449        ((null gnus-newsgroup-data)
6450         (gnus-message 3 "No articles in the group")
6451         nil)
6452        ;; Pick the first article.
6453        ((not unread)
6454         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6455         (gnus-data-number (car gnus-newsgroup-data)))
6456        ;; No unread articles.
6457        ((null gnus-newsgroup-unreads)
6458         (gnus-message 3 "No more unread articles")
6459         nil)
6460        ;; Find the first unread article.
6461        (t
6462         (let ((data gnus-newsgroup-data))
6463           (while (and data
6464                       (and (not (and undownloaded
6465                                      (eq gnus-undownloaded-mark
6466                                          (gnus-data-mark (car data)))))
6467                            (if unseen
6468                                (or (not (memq
6469                                          (gnus-data-number (car data))
6470                                          gnus-newsgroup-unseen))
6471                                    (not (gnus-data-unread-p (car data))))
6472                              (not (gnus-data-unread-p (car data))))))
6473             (setq data (cdr data)))
6474           (when data
6475             (goto-char (gnus-data-pos (car data)))
6476             (gnus-data-number (car data))))))
6477     (gnus-summary-position-point)))
6478
6479 (defun gnus-summary-next-subject (n &optional unread dont-display)
6480   "Go to next N'th summary line.
6481 If N is negative, go to the previous N'th subject line.
6482 If UNREAD is non-nil, only unread articles are selected.
6483 The difference between N and the actual number of steps taken is
6484 returned."
6485   (interactive "p")
6486   (let ((backward (< n 0))
6487         (n (abs n)))
6488     (while (and (> n 0)
6489                 (if backward
6490                     (gnus-summary-find-prev unread)
6491                   (gnus-summary-find-next unread)))
6492       (unless (zerop (setq n (1- n)))
6493         (gnus-summary-show-thread)))
6494     (when (/= 0 n)
6495       (gnus-message 7 "No more%s articles"
6496                     (if unread " unread" "")))
6497     (unless dont-display
6498       (gnus-summary-recenter)
6499       (gnus-summary-position-point))
6500     n))
6501
6502 (defun gnus-summary-next-unread-subject (n)
6503   "Go to next N'th unread summary line."
6504   (interactive "p")
6505   (gnus-summary-next-subject n t))
6506
6507 (defun gnus-summary-prev-subject (n &optional unread)
6508   "Go to previous N'th summary line.
6509 If optional argument UNREAD is non-nil, only unread article is selected."
6510   (interactive "p")
6511   (gnus-summary-next-subject (- n) unread))
6512
6513 (defun gnus-summary-prev-unread-subject (n)
6514   "Go to previous N'th unread summary line."
6515   (interactive "p")
6516   (gnus-summary-next-subject (- n) t))
6517
6518 (defun gnus-summary-goto-subject (article &optional force silent)
6519   "Go the subject line of ARTICLE.
6520 If FORCE, also allow jumping to articles not currently shown."
6521   (interactive "nArticle number: ")
6522   (unless (numberp article)
6523     (error "Article %s is not a number" article))
6524   (let ((b (point))
6525         (data (gnus-data-find article)))
6526     ;; We read in the article if we have to.
6527     (and (not data)
6528          force
6529          (gnus-summary-insert-subject
6530           article
6531           (if (or (numberp force) (vectorp force)) force)
6532           t)
6533          (setq data (gnus-data-find article)))
6534     (goto-char b)
6535     (if (not data)
6536         (progn
6537           (unless silent
6538             (gnus-message 3 "Can't find article %d" article))
6539           nil)
6540       (let ((pt (gnus-data-pos data)))
6541         (goto-char pt)
6542         (gnus-summary-set-article-display-arrow pt))
6543       (gnus-summary-position-point)
6544       article)))
6545
6546 ;; Walking around summary lines with displaying articles.
6547
6548 (defun gnus-summary-expand-window (&optional arg)
6549   "Make the summary buffer take up the entire Emacs frame.
6550 Given a prefix, will force an `article' buffer configuration."
6551   (interactive "P")
6552   (if arg
6553       (gnus-configure-windows 'article 'force)
6554     (gnus-configure-windows 'summary 'force)))
6555
6556 (defun gnus-summary-display-article (article &optional all-header)
6557   "Display ARTICLE in article buffer."
6558   (when (gnus-buffer-live-p gnus-article-buffer)
6559     (with-current-buffer gnus-article-buffer
6560       (set-buffer-multibyte t)))
6561   (gnus-set-global-variables)
6562   (when (gnus-buffer-live-p gnus-article-buffer)
6563     (with-current-buffer gnus-article-buffer
6564       (setq gnus-article-charset gnus-newsgroup-charset)
6565       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6566       (set-buffer-multibyte t)))
6567   (if (null article)
6568       nil
6569     (prog1
6570         (if gnus-summary-display-article-function
6571             (funcall gnus-summary-display-article-function article all-header)
6572           (gnus-article-prepare article all-header))
6573       (with-current-buffer gnus-article-buffer
6574         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6575              nil))
6576       (gnus-run-hooks 'gnus-select-article-hook)
6577       (when (and gnus-current-article
6578                  (not (zerop gnus-current-article)))
6579         (gnus-summary-goto-subject gnus-current-article))
6580       (gnus-summary-recenter)
6581       (when (and gnus-use-trees gnus-show-threads)
6582         (gnus-possibly-generate-tree article)
6583         (gnus-highlight-selected-tree article))
6584       ;; Successfully display article.
6585       (gnus-article-set-window-start
6586        (cdr (assq article gnus-newsgroup-bookmarks))))))
6587
6588 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6589   "Select the current article.
6590 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6591 non-nil, the article will be re-fetched even if it already present in
6592 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6593 be displayed."
6594   ;; Make sure we are in the summary buffer to work around bbdb bug.
6595   (unless (eq major-mode 'gnus-summary-mode)
6596     (set-buffer gnus-summary-buffer))
6597   (let ((article (or article (gnus-summary-article-number)))
6598         (all-headers (not (not all-headers))) ;Must be T or NIL.
6599         gnus-summary-display-article-function)
6600     (and (not pseudo)
6601          (gnus-summary-article-pseudo-p article)
6602          (error "This is a pseudo-article"))
6603     (save-excursion
6604       (set-buffer gnus-summary-buffer)
6605       (if (or (and gnus-single-article-buffer
6606                    (or (null gnus-current-article)
6607                        (null gnus-article-current)
6608                        (null (get-buffer gnus-article-buffer))
6609                        (not (eq article (cdr gnus-article-current)))
6610                        (not (equal (car gnus-article-current)
6611                                    gnus-newsgroup-name))))
6612               (and (not gnus-single-article-buffer)
6613                    (or (null gnus-current-article)
6614                        (not (eq gnus-current-article article))))
6615               force)
6616           ;; The requested article is different from the current article.
6617           (progn
6618             (gnus-summary-display-article article all-headers)
6619 ;;; Hidden headers are not hidden text any more.
6620 ;;          (when (or all-headers gnus-show-all-headers)
6621 ;;            (gnus-article-show-all-headers))
6622             (gnus-article-set-window-start
6623              (cdr (assq article gnus-newsgroup-bookmarks)))
6624             article)
6625 ;;      (when (or all-headers gnus-show-all-headers)
6626 ;;        (gnus-article-show-all-headers))
6627         'old))))
6628
6629 (defun gnus-summary-force-verify-and-decrypt ()
6630   (interactive)
6631   (let ((mm-verify-option 'known)
6632         (mm-decrypt-option 'known))
6633     (gnus-summary-select-article nil 'force)))
6634
6635 (defun gnus-summary-set-current-mark (&optional current-mark)
6636   "Obsolete function."
6637   nil)
6638
6639 (defun gnus-summary-next-article (&optional unread subject backward push)
6640   "Select the next article.
6641 If UNREAD, only unread articles are selected.
6642 If SUBJECT, only articles with SUBJECT are selected.
6643 If BACKWARD, the previous article is selected instead of the next."
6644   (interactive "P")
6645   (cond
6646    ;; Is there such an article?
6647    ((and (gnus-summary-search-forward unread subject backward)
6648          (or (gnus-summary-display-article (gnus-summary-article-number))
6649              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6650     (gnus-summary-position-point))
6651    ;; If not, we try the first unread, if that is wanted.
6652    ((and subject
6653          gnus-auto-select-same
6654          (gnus-summary-first-unread-article))
6655     (gnus-summary-position-point)
6656     (gnus-message 6 "Wrapped"))
6657    ;; Try to get next/previous article not displayed in this group.
6658    ((and gnus-auto-extend-newsgroup
6659          (not unread) (not subject))
6660     (gnus-summary-goto-article
6661      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6662      nil (count-lines (point-min) (point))))
6663    ;; Go to next/previous group.
6664    (t
6665     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6666       (gnus-summary-jump-to-group gnus-newsgroup-name))
6667     (let ((cmd last-command-char)
6668           (point
6669            (save-excursion
6670              (set-buffer gnus-group-buffer)
6671              (point)))
6672           (group
6673            (if (eq gnus-keep-same-level 'best)
6674                (gnus-summary-best-group gnus-newsgroup-name)
6675              (gnus-summary-search-group backward gnus-keep-same-level))))
6676       ;; For some reason, the group window gets selected.  We change
6677       ;; it back.
6678       (select-window (get-buffer-window (current-buffer)))
6679       ;; Select next unread newsgroup automagically.
6680       (cond
6681        ((or (not gnus-auto-select-next)
6682             (not cmd))
6683         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6684        ((or (eq gnus-auto-select-next 'quietly)
6685             (and (eq gnus-auto-select-next 'slightly-quietly)
6686                  push)
6687             (and (eq gnus-auto-select-next 'almost-quietly)
6688                  (gnus-summary-last-article-p)))
6689         ;; Select quietly.
6690         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6691             (gnus-summary-exit)
6692           (gnus-message 7 "No more%s articles (%s)..."
6693                         (if unread " unread" "")
6694                         (if group (concat "selecting " group)
6695                           "exiting"))
6696           (gnus-summary-next-group nil group backward)))
6697        (t
6698         (when (gnus-key-press-event-p last-input-event)
6699           (gnus-summary-walk-group-buffer
6700            gnus-newsgroup-name cmd unread backward point))))))))
6701
6702 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6703   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6704                       (?\C-p (gnus-group-prev-unread-group 1))))
6705         (cursor-in-echo-area t)
6706         keve key group ended)
6707     (save-excursion
6708       (set-buffer gnus-group-buffer)
6709       (goto-char start)
6710       (setq group
6711             (if (eq gnus-keep-same-level 'best)
6712                 (gnus-summary-best-group gnus-newsgroup-name)
6713               (gnus-summary-search-group backward gnus-keep-same-level))))
6714     (while (not ended)
6715       (gnus-message
6716        5 "No more%s articles%s" (if unread " unread" "")
6717        (if (and group
6718                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6719            (format " (Type %s for %s [%s])"
6720                    (single-key-description cmd) group
6721                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6722          (format " (Type %s to exit %s)"
6723                  (single-key-description cmd)
6724                  gnus-newsgroup-name)))
6725       ;; Confirm auto selection.
6726       (setq key (car (setq keve (gnus-read-event-char))))
6727       (setq ended t)
6728       (cond
6729        ((assq key keystrokes)
6730         (let ((obuf (current-buffer)))
6731           (switch-to-buffer gnus-group-buffer)
6732           (when group
6733             (gnus-group-jump-to-group group))
6734           (eval (cadr (assq key keystrokes)))
6735           (setq group (gnus-group-group-name))
6736           (switch-to-buffer obuf))
6737         (setq ended nil))
6738        ((equal key cmd)
6739         (if (or (not group)
6740                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6741             (gnus-summary-exit)
6742           (gnus-summary-next-group nil group backward)))
6743        (t
6744         (push (cdr keve) unread-command-events))))))
6745
6746 (defun gnus-summary-next-unread-article ()
6747   "Select unread article after current one."
6748   (interactive)
6749   (gnus-summary-next-article
6750    (or (not (eq gnus-summary-goto-unread 'never))
6751        (gnus-summary-last-article-p (gnus-summary-article-number)))
6752    (and gnus-auto-select-same
6753         (gnus-summary-article-subject))))
6754
6755 (defun gnus-summary-prev-article (&optional unread subject)
6756   "Select the article after the current one.
6757 If UNREAD is non-nil, only unread articles are selected."
6758   (interactive "P")
6759   (gnus-summary-next-article unread subject t))
6760
6761 (defun gnus-summary-prev-unread-article ()
6762   "Select unread article before current one."
6763   (interactive)
6764   (gnus-summary-prev-article
6765    (or (not (eq gnus-summary-goto-unread 'never))
6766        (gnus-summary-first-article-p (gnus-summary-article-number)))
6767    (and gnus-auto-select-same
6768         (gnus-summary-article-subject))))
6769
6770 (defun gnus-summary-next-page (&optional lines circular)
6771   "Show next page of the selected article.
6772 If at the end of the current article, select the next article.
6773 LINES says how many lines should be scrolled up.
6774
6775 If CIRCULAR is non-nil, go to the start of the article instead of
6776 selecting the next article when reaching the end of the current
6777 article."
6778   (interactive "P")
6779   (setq gnus-summary-buffer (current-buffer))
6780   (gnus-set-global-variables)
6781   (let ((article (gnus-summary-article-number))
6782         (article-window (get-buffer-window gnus-article-buffer t))
6783         endp)
6784     ;; If the buffer is empty, we have no article.
6785     (unless article
6786       (error "No article to select"))
6787     (gnus-configure-windows 'article)
6788     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6789         (if (and (eq gnus-summary-goto-unread 'never)
6790                  (not (gnus-summary-last-article-p article)))
6791             (gnus-summary-next-article)
6792           (gnus-summary-next-unread-article))
6793       (if (or (null gnus-current-article)
6794               (null gnus-article-current)
6795               (/= article (cdr gnus-article-current))
6796               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6797           ;; Selected subject is different from current article's.
6798           (gnus-summary-display-article article)
6799         (when article-window
6800           (gnus-eval-in-buffer-window gnus-article-buffer
6801             (setq endp (gnus-article-next-page lines)))
6802           (when endp
6803             (cond (circular
6804                    (gnus-summary-beginning-of-article))
6805                   (lines
6806                    (gnus-message 3 "End of message"))
6807                   ((null lines)
6808                    (if (and (eq gnus-summary-goto-unread 'never)
6809                             (not (gnus-summary-last-article-p article)))
6810                        (gnus-summary-next-article)
6811                      (gnus-summary-next-unread-article))))))))
6812     (gnus-summary-recenter)
6813     (gnus-summary-position-point)))
6814
6815 (defun gnus-summary-prev-page (&optional lines move)
6816   "Show previous page of selected article.
6817 Argument LINES specifies lines to be scrolled down.
6818 If MOVE, move to the previous unread article if point is at
6819 the beginning of the buffer."
6820   (interactive "P")
6821   (let ((article (gnus-summary-article-number))
6822         (article-window (get-buffer-window gnus-article-buffer t))
6823         endp)
6824     (gnus-configure-windows 'article)
6825     (if (or (null gnus-current-article)
6826             (null gnus-article-current)
6827             (/= article (cdr gnus-article-current))
6828             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6829         ;; Selected subject is different from current article's.
6830         (gnus-summary-display-article article)
6831       (gnus-summary-recenter)
6832       (when article-window
6833         (gnus-eval-in-buffer-window gnus-article-buffer
6834           (setq endp (gnus-article-prev-page lines)))
6835         (when (and move endp)
6836           (cond (lines
6837                  (gnus-message 3 "Beginning of message"))
6838                 ((null lines)
6839                  (if (and (eq gnus-summary-goto-unread 'never)
6840                           (not (gnus-summary-first-article-p article)))
6841                      (gnus-summary-prev-article)
6842                    (gnus-summary-prev-unread-article))))))))
6843   (gnus-summary-position-point))
6844
6845 (defun gnus-summary-prev-page-or-article (&optional lines)
6846   "Show previous page of selected article.
6847 Argument LINES specifies lines to be scrolled down.
6848 If at the beginning of the article, go to the next article."
6849   (interactive "P")
6850   (gnus-summary-prev-page lines t))
6851
6852 (defun gnus-summary-scroll-up (lines)
6853   "Scroll up (or down) one line current article.
6854 Argument LINES specifies lines to be scrolled up (or down if negative)."
6855   (interactive "p")
6856   (gnus-configure-windows 'article)
6857   (gnus-summary-show-thread)
6858   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6859     (gnus-eval-in-buffer-window gnus-article-buffer
6860       (cond ((> lines 0)
6861              (when (gnus-article-next-page lines)
6862                (gnus-message 3 "End of message")))
6863             ((< lines 0)
6864              (gnus-article-prev-page (- lines))))))
6865   (gnus-summary-recenter)
6866   (gnus-summary-position-point))
6867
6868 (defun gnus-summary-scroll-down (lines)
6869   "Scroll down (or up) one line current article.
6870 Argument LINES specifies lines to be scrolled down (or up if negative)."
6871   (interactive "p")
6872   (gnus-summary-scroll-up (- lines)))
6873
6874 (defun gnus-summary-next-same-subject ()
6875   "Select next article which has the same subject as current one."
6876   (interactive)
6877   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6878
6879 (defun gnus-summary-prev-same-subject ()
6880   "Select previous article which has the same subject as current one."
6881   (interactive)
6882   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6883
6884 (defun gnus-summary-next-unread-same-subject ()
6885   "Select next unread article which has the same subject as current one."
6886   (interactive)
6887   (gnus-summary-next-article t (gnus-summary-article-subject)))
6888
6889 (defun gnus-summary-prev-unread-same-subject ()
6890   "Select previous unread article which has the same subject as current one."
6891   (interactive)
6892   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6893
6894 (defun gnus-summary-first-unread-article ()
6895   "Select the first unread article.
6896 Return nil if there are no unread articles."
6897   (interactive)
6898   (prog1
6899       (when (gnus-summary-first-subject t)
6900         (gnus-summary-show-thread)
6901         (gnus-summary-first-subject t)
6902         (gnus-summary-display-article (gnus-summary-article-number)))
6903     (gnus-summary-position-point)))
6904
6905 (defun gnus-summary-first-unread-subject ()
6906   "Place the point on the subject line of the first unread article.
6907 Return nil if there are no unread articles."
6908   (interactive)
6909   (prog1
6910       (when (gnus-summary-first-subject t)
6911         (gnus-summary-show-thread)
6912         (gnus-summary-first-subject t))
6913     (gnus-summary-position-point)))
6914
6915 (defun gnus-summary-first-unseen-subject ()
6916   "Place the point on the subject line of the first unseen article.
6917 Return nil if there are no unseen articles."
6918   (interactive)
6919   (prog1
6920       (when (gnus-summary-first-subject t t t)
6921         (gnus-summary-show-thread)
6922         (gnus-summary-first-subject t t t))
6923     (gnus-summary-position-point)))
6924
6925 (defun gnus-summary-first-unseen-or-unread-subject ()
6926   "Place the point on the subject line of the first unseen article.
6927 Return nil if there are no unseen articles."
6928   (interactive)
6929   (prog1
6930       (unless (when (gnus-summary-first-subject t t t)
6931                 (gnus-summary-show-thread)
6932                 (gnus-summary-first-subject t t t))
6933         (when (gnus-summary-first-subject t)
6934           (gnus-summary-show-thread)
6935           (gnus-summary-first-subject t)))
6936     (gnus-summary-position-point)))
6937
6938 (defun gnus-summary-first-article ()
6939   "Select the first article.
6940 Return nil if there are no articles."
6941   (interactive)
6942   (prog1
6943       (when (gnus-summary-first-subject)
6944         (gnus-summary-show-thread)
6945         (gnus-summary-first-subject)
6946         (gnus-summary-display-article (gnus-summary-article-number)))
6947     (gnus-summary-position-point)))
6948
6949 (defun gnus-summary-best-unread-article (&optional arg)
6950   "Select the unread article with the highest score.
6951 If given a prefix argument, select the next unread article that has a
6952 score higher than the default score."
6953   (interactive "P")
6954   (let ((article (if arg
6955                      (gnus-summary-better-unread-subject)
6956                    (gnus-summary-best-unread-subject))))
6957     (if article
6958         (gnus-summary-goto-article article)
6959       (error "No unread articles"))))
6960
6961 (defun gnus-summary-best-unread-subject ()
6962   "Select the unread subject with the highest score."
6963   (interactive)
6964   (let ((best -1000000)
6965         (data gnus-newsgroup-data)
6966         article score)
6967     (while data
6968       (and (gnus-data-unread-p (car data))
6969            (> (setq score
6970                     (gnus-summary-article-score (gnus-data-number (car data))))
6971               best)
6972            (setq best score
6973                  article (gnus-data-number (car data))))
6974       (setq data (cdr data)))
6975     (when article
6976       (gnus-summary-goto-subject article))
6977     (gnus-summary-position-point)
6978     article))
6979
6980 (defun gnus-summary-better-unread-subject ()
6981   "Select the first unread subject that has a score over the default score."
6982   (interactive)
6983   (let ((data gnus-newsgroup-data)
6984         article score)
6985     (while (and (setq article (gnus-data-number (car data)))
6986                 (or (gnus-data-read-p (car data))
6987                     (not (> (gnus-summary-article-score article)
6988                             gnus-summary-default-score))))
6989       (setq data (cdr data)))
6990     (when article
6991       (gnus-summary-goto-subject article))
6992     (gnus-summary-position-point)
6993     article))
6994
6995 (defun gnus-summary-last-subject ()
6996   "Go to the last displayed subject line in the group."
6997   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6998     (when article
6999       (gnus-summary-goto-subject article))))
7000
7001 (defun gnus-summary-goto-article (article &optional all-headers force)
7002   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7003 If ALL-HEADERS is non-nil, no header lines are hidden.
7004 If FORCE, go to the article even if it isn't displayed.  If FORCE
7005 is a number, it is the line the article is to be displayed on."
7006   (interactive
7007    (list
7008     (completing-read
7009      "Article number or Message-ID: "
7010      (mapcar (lambda (number) (list (int-to-string number)))
7011              gnus-newsgroup-limit))
7012     current-prefix-arg
7013     t))
7014   (prog1
7015       (if (and (stringp article)
7016                (string-match "@" article))
7017           (gnus-summary-refer-article article)
7018         (when (stringp article)
7019           (setq article (string-to-number article)))
7020         (if (gnus-summary-goto-subject article force)
7021             (gnus-summary-display-article article all-headers)
7022           (gnus-message 4 "Couldn't go to article %s" article) nil))
7023     (gnus-summary-position-point)))
7024
7025 (defun gnus-summary-goto-last-article ()
7026   "Go to the previously read article."
7027   (interactive)
7028   (prog1
7029       (when gnus-last-article
7030         (gnus-summary-goto-article gnus-last-article nil t))
7031     (gnus-summary-position-point)))
7032
7033 (defun gnus-summary-pop-article (number)
7034   "Pop one article off the history and go to the previous.
7035 NUMBER articles will be popped off."
7036   (interactive "p")
7037   (let (to)
7038     (setq gnus-newsgroup-history
7039           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7040     (if to
7041         (gnus-summary-goto-article (car to) nil t)
7042       (error "Article history empty")))
7043   (gnus-summary-position-point))
7044
7045 ;; Summary commands and functions for limiting the summary buffer.
7046
7047 (defun gnus-summary-limit-to-articles (n)
7048   "Limit the summary buffer to the next N articles.
7049 If not given a prefix, use the process marked articles instead."
7050   (interactive "P")
7051   (prog1
7052       (let ((articles (gnus-summary-work-articles n)))
7053         (setq gnus-newsgroup-processable nil)
7054         (gnus-summary-limit articles))
7055     (gnus-summary-position-point)))
7056
7057 (defun gnus-summary-pop-limit (&optional total)
7058   "Restore the previous limit.
7059 If given a prefix, remove all limits."
7060   (interactive "P")
7061   (when total
7062     (setq gnus-newsgroup-limits
7063           (list (mapcar (lambda (h) (mail-header-number h))
7064                         gnus-newsgroup-headers))))
7065   (unless gnus-newsgroup-limits
7066     (error "No limit to pop"))
7067   (prog1
7068       (gnus-summary-limit nil 'pop)
7069     (gnus-summary-position-point)))
7070
7071 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7072   "Limit the summary buffer to articles that have subjects that match a regexp.
7073 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7074   (interactive
7075    (list (read-string (if current-prefix-arg
7076                           "Exclude subject (regexp): "
7077                         "Limit to subject (regexp): "))
7078          nil current-prefix-arg))
7079   (unless header
7080     (setq header "subject"))
7081   (when (not (equal "" subject))
7082     (prog1
7083         (let ((articles (gnus-summary-find-matching
7084                          (or header "subject") subject 'all nil nil
7085                          not-matching)))
7086           (unless articles
7087             (error "Found no matches for \"%s\"" subject))
7088           (gnus-summary-limit articles))
7089       (gnus-summary-position-point))))
7090
7091 (defun gnus-summary-limit-to-author (from &optional not-matching)
7092   "Limit the summary buffer to articles that have authors that match a regexp.
7093 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7094   (interactive
7095    (list (read-string (if current-prefix-arg
7096                           "Exclude author (regexp): "
7097                         "Limit to author (regexp): "))
7098          current-prefix-arg))
7099   (gnus-summary-limit-to-subject from "from" not-matching))
7100
7101 (defun gnus-summary-limit-to-age (age &optional younger-p)
7102   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7103 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7104 articles that are younger than AGE days."
7105   (interactive
7106    (let ((younger current-prefix-arg)
7107          (days-got nil)
7108          days)
7109      (while (not days-got)
7110        (setq days (if younger
7111                       (read-string "Limit to articles within (in days): ")
7112                     (read-string "Limit to articles older than (in days): ")))
7113        (when (> (length days) 0)
7114          (setq days (read days)))
7115        (if (numberp days)
7116            (progn
7117              (setq days-got t)
7118              (if (< days 0)
7119                  (progn
7120                    (setq younger (not younger))
7121                    (setq days (* days -1)))))
7122          (message "Please enter a number.")
7123          (sleep-for 1)))
7124      (list days younger)))
7125   (prog1
7126       (let ((data gnus-newsgroup-data)
7127             (cutoff (days-to-time age))
7128             articles d date is-younger)
7129         (while (setq d (pop data))
7130           (when (and (vectorp (gnus-data-header d))
7131                      (setq date (mail-header-date (gnus-data-header d))))
7132             (setq is-younger (time-less-p
7133                               (time-since (condition-case ()
7134                                               (date-to-time date)
7135                                             (error '(0 0))))
7136                               cutoff))
7137             (when (if younger-p
7138                       is-younger
7139                     (not is-younger))
7140               (push (gnus-data-number d) articles))))
7141         (gnus-summary-limit (nreverse articles)))
7142     (gnus-summary-position-point)))
7143
7144 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7145   "Limit the summary buffer to articles that match an 'extra' header."
7146   (interactive
7147    (let ((header
7148           (intern
7149            (gnus-completing-read-with-default
7150             (symbol-name (car gnus-extra-headers))
7151             (if current-prefix-arg
7152                 "Exclude extra header:"
7153               "Limit extra header:")
7154             (mapcar (lambda (x)
7155                       (cons (symbol-name x) x))
7156                     gnus-extra-headers)
7157             nil
7158             t))))
7159      (list header
7160            (read-string (format "%s header %s (regexp): "
7161                                 (if current-prefix-arg "Exclude" "Limit to")
7162                                 header))
7163            current-prefix-arg)))
7164   (when (not (equal "" regexp))
7165     (prog1
7166         (let ((articles (gnus-summary-find-matching
7167                          (cons 'extra header) regexp 'all nil nil
7168                          not-matching)))
7169           (unless articles
7170             (error "Found no matches for \"%s\"" regexp))
7171           (gnus-summary-limit articles))
7172       (gnus-summary-position-point))))
7173
7174 (defun gnus-summary-limit-to-display-predicate ()
7175   "Limit the summary buffer to the predicated in the `display' group parameter."
7176   (interactive)
7177   (unless gnus-newsgroup-display
7178     (error "There is no `display' group parameter"))
7179   (let (articles)
7180     (dolist (number gnus-newsgroup-articles)
7181       (when (funcall gnus-newsgroup-display)
7182         (push number articles)))
7183     (gnus-summary-limit articles))
7184   (gnus-summary-position-point))
7185
7186 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7187 (make-obsolete
7188  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7189
7190 (defun gnus-summary-limit-to-unread (&optional all)
7191   "Limit the summary buffer to articles that are not marked as read.
7192 If ALL is non-nil, limit strictly to unread articles."
7193   (interactive "P")
7194   (if all
7195       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7196     (gnus-summary-limit-to-marks
7197      ;; Concat all the marks that say that an article is read and have
7198      ;; those removed.
7199      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7200            gnus-killed-mark gnus-kill-file-mark
7201            gnus-low-score-mark gnus-expirable-mark
7202            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7203            gnus-duplicate-mark gnus-souped-mark)
7204      'reverse)))
7205
7206 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7207 (make-obsolete 'gnus-summary-delete-marked-with
7208                'gnus-summary-limit-exlude-marks)
7209
7210 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7211   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7212 If REVERSE, limit the summary buffer to articles that are marked
7213 with MARKS.  MARKS can either be a string of marks or a list of marks.
7214 Returns how many articles were removed."
7215   (interactive "sMarks: ")
7216   (gnus-summary-limit-to-marks marks t))
7217
7218 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7219   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7220 If REVERSE (the prefix), limit the summary buffer to articles that are
7221 not marked with MARKS.  MARKS can either be a string of marks or a
7222 list of marks.
7223 Returns how many articles were removed."
7224   (interactive "sMarks: \nP")
7225   (prog1
7226       (let ((data gnus-newsgroup-data)
7227             (marks (if (listp marks) marks
7228                      (append marks nil))) ; Transform to list.
7229             articles)
7230         (while data
7231           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7232                   (memq (gnus-data-mark (car data)) marks))
7233             (push (gnus-data-number (car data)) articles))
7234           (setq data (cdr data)))
7235         (gnus-summary-limit articles))
7236     (gnus-summary-position-point)))
7237
7238 (defun gnus-summary-limit-to-score (score)
7239   "Limit to articles with score at or above SCORE."
7240   (interactive "NLimit to articles with score of at least: ")
7241   (let ((data gnus-newsgroup-data)
7242         articles)
7243     (while data
7244       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7245                 score)
7246         (push (gnus-data-number (car data)) articles))
7247       (setq data (cdr data)))
7248     (prog1
7249         (gnus-summary-limit articles)
7250       (gnus-summary-position-point))))
7251
7252 (defun gnus-summary-limit-include-thread (id)
7253   "Display all the hidden articles that is in the thread with ID in it.
7254 When called interactively, ID is the Message-ID of the current
7255 article."
7256   (interactive (list (mail-header-id (gnus-summary-article-header))))
7257   (let ((articles (gnus-articles-in-thread
7258                    (gnus-id-to-thread (gnus-root-id id)))))
7259     (prog1
7260         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7261       (gnus-summary-limit-include-matching-articles
7262        "subject"
7263        (regexp-quote (gnus-simplify-subject-re
7264                       (mail-header-subject (gnus-id-to-header id)))))
7265       (gnus-summary-position-point))))
7266
7267 (defun gnus-summary-limit-include-matching-articles (header regexp)
7268   "Display all the hidden articles that have HEADERs that match REGEXP."
7269   (interactive (list (read-string "Match on header: ")
7270                      (read-string "Regexp: ")))
7271   (let ((articles (gnus-find-matching-articles header regexp)))
7272     (prog1
7273         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7274       (gnus-summary-position-point))))
7275
7276 (defun gnus-summary-limit-include-dormant ()
7277   "Display all the hidden articles that are marked as dormant.
7278 Note that this command only works on a subset of the articles currently
7279 fetched for this group."
7280   (interactive)
7281   (unless gnus-newsgroup-dormant
7282     (error "There are no dormant articles in this group"))
7283   (prog1
7284       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7285     (gnus-summary-position-point)))
7286
7287 (defun gnus-summary-limit-exclude-dormant ()
7288   "Hide all dormant articles."
7289   (interactive)
7290   (prog1
7291       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7292     (gnus-summary-position-point)))
7293
7294 (defun gnus-summary-limit-exclude-childless-dormant ()
7295   "Hide all dormant articles that have no children."
7296   (interactive)
7297   (let ((data (gnus-data-list t))
7298         articles d children)
7299     ;; Find all articles that are either not dormant or have
7300     ;; children.
7301     (while (setq d (pop data))
7302       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7303                 (and (setq children
7304                            (gnus-article-children (gnus-data-number d)))
7305                      (let (found)
7306                        (while children
7307                          (when (memq (car children) articles)
7308                            (setq children nil
7309                                  found t))
7310                          (pop children))
7311                        found)))
7312         (push (gnus-data-number d) articles)))
7313     ;; Do the limiting.
7314     (prog1
7315         (gnus-summary-limit articles)
7316       (gnus-summary-position-point))))
7317
7318 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7319   "Mark all unread excluded articles as read.
7320 If ALL, mark even excluded ticked and dormants as read."
7321   (interactive "P")
7322   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7323   (let ((articles (gnus-sorted-ndifference
7324                    (sort
7325                     (mapcar (lambda (h) (mail-header-number h))
7326                             gnus-newsgroup-headers)
7327                     '<)
7328                    gnus-newsgroup-limit))
7329         article)
7330     (setq gnus-newsgroup-unreads
7331           (gnus-sorted-intersection gnus-newsgroup-unreads
7332                                     gnus-newsgroup-limit))
7333     (if all
7334         (setq gnus-newsgroup-dormant nil
7335               gnus-newsgroup-marked nil
7336               gnus-newsgroup-reads
7337               (nconc
7338                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7339                gnus-newsgroup-reads))
7340       (while (setq article (pop articles))
7341         (unless (or (memq article gnus-newsgroup-dormant)
7342                     (memq article gnus-newsgroup-marked))
7343           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7344
7345 (defun gnus-summary-limit (articles &optional pop)
7346   (if pop
7347       ;; We pop the previous limit off the stack and use that.
7348       (setq articles (car gnus-newsgroup-limits)
7349             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7350     ;; We use the new limit, so we push the old limit on the stack.
7351     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7352   ;; Set the limit.
7353   (setq gnus-newsgroup-limit articles)
7354   (let ((total (length gnus-newsgroup-data))
7355         (data (gnus-data-find-list (gnus-summary-article-number)))
7356         (gnus-summary-mark-below nil)   ; Inhibit this.
7357         found)
7358     ;; This will do all the work of generating the new summary buffer
7359     ;; according to the new limit.
7360     (gnus-summary-prepare)
7361     ;; Hide any threads, possibly.
7362     (gnus-summary-maybe-hide-threads)
7363     ;; Try to return to the article you were at, or one in the
7364     ;; neighborhood.
7365     (when data
7366       ;; We try to find some article after the current one.
7367       (while data
7368         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7369           (setq data nil
7370                 found t))
7371         (setq data (cdr data))))
7372     (unless found
7373       ;; If there is no data, that means that we were after the last
7374       ;; article.  The same goes when we can't find any articles
7375       ;; after the current one.
7376       (goto-char (point-max))
7377       (gnus-summary-find-prev))
7378     (gnus-set-mode-line 'summary)
7379     ;; We return how many articles were removed from the summary
7380     ;; buffer as a result of the new limit.
7381     (- total (length gnus-newsgroup-data))))
7382
7383 (defsubst gnus-invisible-cut-children (threads)
7384   (let ((num 0))
7385     (while threads
7386       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7387         (incf num))
7388       (pop threads))
7389     (< num 2)))
7390
7391 (defsubst gnus-cut-thread (thread)
7392   "Go forwards in the thread until we find an article that we want to display."
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     ;; Deal with old-fetched headers and sparse threads.
7399     (while (and
7400             thread
7401             (or
7402              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7403              (gnus-summary-article-ancient-p
7404               (mail-header-number (car thread))))
7405             (if (or (<= (length (cdr thread)) 1)
7406                     (eq gnus-fetch-old-headers 'invisible))
7407                 (setq gnus-newsgroup-limit
7408                       (delq (mail-header-number (car thread))
7409                             gnus-newsgroup-limit)
7410                       thread (cadr thread))
7411               (when (gnus-invisible-cut-children (cdr thread))
7412                 (let ((th (cdr thread)))
7413                   (while th
7414                     (if (memq (mail-header-number (caar th))
7415                               gnus-newsgroup-limit)
7416                         (setq thread (car th)
7417                               th nil)
7418                       (setq th (cdr th))))))))))
7419   thread)
7420
7421 (defun gnus-cut-threads (threads)
7422   "Cut off all uninteresting articles from the beginning of threads."
7423   (when (or (eq gnus-fetch-old-headers 'some)
7424             (eq gnus-fetch-old-headers 'invisible)
7425             (numberp gnus-fetch-old-headers)
7426             (eq gnus-build-sparse-threads 'some)
7427             (eq gnus-build-sparse-threads 'more))
7428     (let ((th threads))
7429       (while th
7430         (setcar th (gnus-cut-thread (car th)))
7431         (setq th (cdr th)))))
7432   ;; Remove nixed out threads.
7433   (delq nil threads))
7434
7435 (defun gnus-summary-initial-limit (&optional show-if-empty)
7436   "Figure out what the initial limit is supposed to be on group entry.
7437 This entails weeding out unwanted dormants, low-scored articles,
7438 fetch-old-headers verbiage, and so on."
7439   ;; Most groups have nothing to remove.
7440   (if (or gnus-inhibit-limiting
7441           (and (null gnus-newsgroup-dormant)
7442                (eq gnus-newsgroup-display 'gnus-not-ignore)
7443                (not (eq gnus-fetch-old-headers 'some))
7444                (not (numberp gnus-fetch-old-headers))
7445                (not (eq gnus-fetch-old-headers 'invisible))
7446                (null gnus-summary-expunge-below)
7447                (not (eq gnus-build-sparse-threads 'some))
7448                (not (eq gnus-build-sparse-threads 'more))
7449                (null gnus-thread-expunge-below)
7450                (not gnus-use-nocem)))
7451       ()                                ; Do nothing.
7452     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7453     (setq gnus-newsgroup-limit nil)
7454     (mapatoms
7455      (lambda (node)
7456        (unless (car (symbol-value node))
7457          ;; These threads have no parents -- they are roots.
7458          (let ((nodes (cdr (symbol-value node)))
7459                thread)
7460            (while nodes
7461              (if (and gnus-thread-expunge-below
7462                       (< (gnus-thread-total-score (car nodes))
7463                          gnus-thread-expunge-below))
7464                  (gnus-expunge-thread (pop nodes))
7465                (setq thread (pop nodes))
7466                (gnus-summary-limit-children thread))))))
7467      gnus-newsgroup-dependencies)
7468     ;; If this limitation resulted in an empty group, we might
7469     ;; pop the previous limit and use it instead.
7470     (when (and (not gnus-newsgroup-limit)
7471                show-if-empty)
7472       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7473     gnus-newsgroup-limit))
7474
7475 (defun gnus-summary-limit-children (thread)
7476   "Return 1 if this subthread is visible and 0 if it is not."
7477   ;; First we get the number of visible children to this thread.  This
7478   ;; is done by recursing down the thread using this function, so this
7479   ;; will really go down to a leaf article first, before slowly
7480   ;; working its way up towards the root.
7481   (when thread
7482     (let ((children
7483            (if (cdr thread)
7484                (apply '+ (mapcar 'gnus-summary-limit-children
7485                                  (cdr thread)))
7486              0))
7487           (number (mail-header-number (car thread)))
7488           score)
7489       (if (and
7490            (not (memq number gnus-newsgroup-marked))
7491            (or
7492             ;; If this article is dormant and has absolutely no visible
7493             ;; children, then this article isn't visible.
7494             (and (memq number gnus-newsgroup-dormant)
7495                  (zerop children))
7496             ;; If this is "fetch-old-headered" and there is no
7497             ;; visible children, then we don't want this article.
7498             (and (or (eq gnus-fetch-old-headers 'some)
7499                      (numberp gnus-fetch-old-headers))
7500                  (gnus-summary-article-ancient-p number)
7501                  (zerop children))
7502             ;; If this is "fetch-old-headered" and `invisible', then
7503             ;; we don't want this article.
7504             (and (eq gnus-fetch-old-headers 'invisible)
7505                  (gnus-summary-article-ancient-p number))
7506             ;; If this is a sparsely inserted article with no children,
7507             ;; we don't want it.
7508             (and (eq gnus-build-sparse-threads 'some)
7509                  (gnus-summary-article-sparse-p number)
7510                  (zerop children))
7511             ;; If we use expunging, and this article is really
7512             ;; low-scored, then we don't want this article.
7513             (when (and gnus-summary-expunge-below
7514                        (< (setq score
7515                                 (or (cdr (assq number gnus-newsgroup-scored))
7516                                     gnus-summary-default-score))
7517                           gnus-summary-expunge-below))
7518               ;; We increase the expunge-tally here, but that has
7519               ;; nothing to do with the limits, really.
7520               (incf gnus-newsgroup-expunged-tally)
7521               ;; We also mark as read here, if that's wanted.
7522               (when (and gnus-summary-mark-below
7523                          (< score gnus-summary-mark-below))
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               t)
7531             ;; Do the `display' group parameter.
7532             (and gnus-newsgroup-display
7533                  (not (funcall gnus-newsgroup-display)))
7534             ;; Check NoCeM things.
7535             (if (and gnus-use-nocem
7536                      (gnus-nocem-unwanted-article-p
7537                       (mail-header-id (car thread))))
7538                 (progn
7539                   (setq gnus-newsgroup-unreads
7540                         (delq number gnus-newsgroup-unreads))
7541                   t))))
7542           ;; Nope, invisible article.
7543           0
7544         ;; Ok, this article is to be visible, so we add it to the limit
7545         ;; and return 1.
7546         (push number gnus-newsgroup-limit)
7547         1))))
7548
7549 (defun gnus-expunge-thread (thread)
7550   "Mark all articles in THREAD as read."
7551   (let* ((number (mail-header-number (car thread))))
7552     (incf gnus-newsgroup-expunged-tally)
7553     ;; We also mark as read here, if that's wanted.
7554     (setq gnus-newsgroup-unreads
7555           (delq number gnus-newsgroup-unreads))
7556     (if gnus-newsgroup-auto-expire
7557         (push number gnus-newsgroup-expirable)
7558       (push (cons number gnus-low-score-mark)
7559             gnus-newsgroup-reads)))
7560   ;; Go recursively through all subthreads.
7561   (mapcar 'gnus-expunge-thread (cdr thread)))
7562
7563 ;; Summary article oriented commands
7564
7565 (defun gnus-summary-refer-parent-article (n)
7566   "Refer parent article N times.
7567 If N is negative, go to ancestor -N instead.
7568 The difference between N and the number of articles fetched is returned."
7569   (interactive "p")
7570   (let ((skip 1)
7571         error header ref)
7572     (when (not (natnump n))
7573       (setq skip (abs n)
7574             n 1))
7575     (while (and (> n 0)
7576                 (not error))
7577       (setq header (gnus-summary-article-header))
7578       (if (and (eq (mail-header-number header)
7579                    (cdr gnus-article-current))
7580                (equal gnus-newsgroup-name
7581                       (car gnus-article-current)))
7582           ;; If we try to find the parent of the currently
7583           ;; displayed article, then we take a look at the actual
7584           ;; References header, since this is slightly more
7585           ;; reliable than the References field we got from the
7586           ;; server.
7587           (save-excursion
7588             (set-buffer gnus-original-article-buffer)
7589             (nnheader-narrow-to-headers)
7590             (unless (setq ref (message-fetch-field "references"))
7591               (setq ref (message-fetch-field "in-reply-to")))
7592             (widen))
7593         (setq ref
7594               ;; It's not the current article, so we take a bet on
7595               ;; the value we got from the server.
7596               (mail-header-references header)))
7597       (if (and ref
7598                (not (equal ref "")))
7599           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7600             (gnus-message 1 "Couldn't find parent"))
7601         (gnus-message 1 "No references in article %d"
7602                       (gnus-summary-article-number))
7603         (setq error t))
7604       (decf n))
7605     (gnus-summary-position-point)
7606     n))
7607
7608 (defun gnus-summary-refer-references ()
7609   "Fetch all articles mentioned in the References header.
7610 Return the number of articles fetched."
7611   (interactive)
7612   (let ((ref (mail-header-references (gnus-summary-article-header)))
7613         (current (gnus-summary-article-number))
7614         (n 0))
7615     (if (or (not ref)
7616             (equal ref ""))
7617         (error "No References in the current article")
7618       ;; For each Message-ID in the References header...
7619       (while (string-match "<[^>]*>" ref)
7620         (incf n)
7621         ;; ... fetch that article.
7622         (gnus-summary-refer-article
7623          (prog1 (match-string 0 ref)
7624            (setq ref (substring ref (match-end 0))))))
7625       (gnus-summary-goto-subject current)
7626       (gnus-summary-position-point)
7627       n)))
7628
7629 (defun gnus-summary-refer-thread (&optional limit)
7630   "Fetch all articles in the current thread.
7631 If LIMIT (the numerical prefix), fetch that many old headers instead
7632 of what's specified by the `gnus-refer-thread-limit' variable."
7633   (interactive "P")
7634   (let ((id (mail-header-id (gnus-summary-article-header)))
7635         (limit (if limit (prefix-numeric-value limit)
7636                  gnus-refer-thread-limit)))
7637     ;; We want to fetch LIMIT *old* headers, but we also have to
7638     ;; re-fetch all the headers in the current buffer, because many of
7639     ;; them may be undisplayed.  So we adjust LIMIT.
7640     (when (numberp limit)
7641       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7642     (unless (eq gnus-fetch-old-headers 'invisible)
7643       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7644       ;; Retrieve the headers and read them in.
7645       (if (eq (gnus-retrieve-headers
7646                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7647               'nov)
7648           (gnus-build-all-threads)
7649         (error "Can't fetch thread from backends that don't support NOV"))
7650       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7651     (gnus-summary-limit-include-thread id)))
7652
7653 (defun gnus-summary-refer-article (message-id)
7654   "Fetch an article specified by MESSAGE-ID."
7655   (interactive "sMessage-ID: ")
7656   (when (and (stringp message-id)
7657              (not (zerop (length message-id))))
7658     ;; Construct the correct Message-ID if necessary.
7659     ;; Suggested by tale@pawl.rpi.edu.
7660     (unless (string-match "^<" message-id)
7661       (setq message-id (concat "<" message-id)))
7662     (unless (string-match ">$" message-id)
7663       (setq message-id (concat message-id ">")))
7664     (let* ((header (gnus-id-to-header message-id))
7665            (sparse (and header
7666                         (gnus-summary-article-sparse-p
7667                          (mail-header-number header))
7668                         (memq (mail-header-number header)
7669                               gnus-newsgroup-limit)))
7670            number)
7671       (cond
7672        ;; If the article is present in the buffer we just go to it.
7673        ((and header
7674              (or (not (gnus-summary-article-sparse-p
7675                        (mail-header-number header)))
7676                  sparse))
7677         (prog1
7678             (gnus-summary-goto-article
7679              (mail-header-number header) nil t)
7680           (when sparse
7681             (gnus-summary-update-article (mail-header-number header)))))
7682        (t
7683         ;; We fetch the article.
7684         (catch 'found
7685           (dolist (gnus-override-method (gnus-refer-article-methods))
7686             (gnus-check-server gnus-override-method)
7687             ;; Fetch the header, and display the article.
7688             (when (setq number (gnus-summary-insert-subject message-id))
7689               (gnus-summary-select-article nil nil nil number)
7690               (throw 'found t)))
7691           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7692
7693 (defun gnus-refer-article-methods ()
7694   "Return a list of referrable methods."
7695   (cond
7696    ;; No method, so we default to current and native.
7697    ((null gnus-refer-article-method)
7698     (list gnus-current-select-method gnus-select-method))
7699    ;; Current.
7700    ((eq 'current gnus-refer-article-method)
7701     (list gnus-current-select-method))
7702    ;; List of select methods.
7703    ((not (and (symbolp (car gnus-refer-article-method))
7704               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7705     (let (out)
7706       (dolist (method gnus-refer-article-method)
7707         (push (if (eq 'current method)
7708                   gnus-current-select-method
7709                 method)
7710               out))
7711       (nreverse out)))
7712    ;; One single select method.
7713    (t
7714     (list gnus-refer-article-method))))
7715
7716 (defun gnus-summary-edit-parameters ()
7717   "Edit the group parameters of the current group."
7718   (interactive)
7719   (gnus-group-edit-group gnus-newsgroup-name 'params))
7720
7721 (defun gnus-summary-customize-parameters ()
7722   "Customize the group parameters of the current group."
7723   (interactive)
7724   (gnus-group-customize gnus-newsgroup-name))
7725
7726 (defun gnus-summary-enter-digest-group (&optional force)
7727   "Enter an nndoc group based on the current article.
7728 If FORCE, force a digest interpretation.  If not, try
7729 to guess what the document format is."
7730   (interactive "P")
7731   (let ((conf gnus-current-window-configuration))
7732     (save-excursion
7733       (gnus-summary-select-article))
7734     (setq gnus-current-window-configuration conf)
7735     (let* ((name (format "%s-%d"
7736                          (gnus-group-prefixed-name
7737                           gnus-newsgroup-name (list 'nndoc ""))
7738                          (save-excursion
7739                            (set-buffer gnus-summary-buffer)
7740                            gnus-current-article)))
7741            (ogroup gnus-newsgroup-name)
7742            (params (append (gnus-info-params (gnus-get-info ogroup))
7743                            (list (cons 'to-group ogroup))
7744                            (list (cons 'save-article-group ogroup))))
7745            (case-fold-search t)
7746            (buf (current-buffer))
7747            dig to-address)
7748       (save-excursion
7749         (set-buffer gnus-original-article-buffer)
7750         ;; Have the digest group inherit the main mail address of
7751         ;; the parent article.
7752         (when (setq to-address (or (message-fetch-field "reply-to")
7753                                    (message-fetch-field "from")))
7754           (setq params (append
7755                         (list (cons 'to-address
7756                                     (funcall gnus-decode-encoded-word-function
7757                                              to-address))))))
7758         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7759         (insert-buffer-substring gnus-original-article-buffer)
7760         ;; Remove lines that may lead nndoc to misinterpret the
7761         ;; document type.
7762         (narrow-to-region
7763          (goto-char (point-min))
7764          (or (search-forward "\n\n" nil t) (point)))
7765         (goto-char (point-min))
7766         (delete-matching-lines "^Path:\\|^From ")
7767         (widen))
7768       (unwind-protect
7769           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7770                     (gnus-newsgroup-ephemeral-ignored-charsets
7771                      gnus-newsgroup-ignored-charsets))
7772                 (gnus-group-read-ephemeral-group
7773                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7774                               (nndoc-article-type
7775                                ,(if force 'mbox 'guess)))
7776                  t nil nil nil
7777                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7778                                                         "ADAPT")))))
7779               ;; Make all postings to this group go to the parent group.
7780               (nconc (gnus-info-params (gnus-get-info name))
7781                      params)
7782             ;; Couldn't select this doc group.
7783             (switch-to-buffer buf)
7784             (gnus-set-global-variables)
7785             (gnus-configure-windows 'summary)
7786             (gnus-message 3 "Article couldn't be entered?"))
7787         (kill-buffer dig)))))
7788
7789 (defun gnus-summary-read-document (n)
7790   "Open a new group based on the current article(s).
7791 This will allow you to read digests and other similar
7792 documents as newsgroups.
7793 Obeys the standard process/prefix convention."
7794   (interactive "P")
7795   (let* ((articles (gnus-summary-work-articles n))
7796          (ogroup gnus-newsgroup-name)
7797          (params (append (gnus-info-params (gnus-get-info ogroup))
7798                          (list (cons 'to-group ogroup))))
7799          article group egroup groups vgroup)
7800     (while (setq article (pop articles))
7801       (setq group (format "%s-%d" gnus-newsgroup-name article))
7802       (gnus-summary-remove-process-mark article)
7803       (when (gnus-summary-display-article article)
7804         (save-excursion
7805           (with-temp-buffer
7806             (insert-buffer-substring gnus-original-article-buffer)
7807             ;; Remove some headers that may lead nndoc to make
7808             ;; the wrong guess.
7809             (message-narrow-to-head)
7810             (goto-char (point-min))
7811             (delete-matching-lines "^\\(Path\\):\\|^From ")
7812             (widen)
7813             (if (setq egroup
7814                       (gnus-group-read-ephemeral-group
7815                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7816                                      (nndoc-article-type guess))
7817                        t nil t))
7818                 (progn
7819                   ;; Make all postings to this group go to the parent group.
7820                   (nconc (gnus-info-params (gnus-get-info egroup))
7821                          params)
7822                   (push egroup groups))
7823               ;; Couldn't select this doc group.
7824               (gnus-error 3 "Article couldn't be entered"))))))
7825     ;; Now we have selected all the documents.
7826     (cond
7827      ((not groups)
7828       (error "None of the articles could be interpreted as documents"))
7829      ((gnus-group-read-ephemeral-group
7830        (setq vgroup (format
7831                      "nnvirtual:%s-%s" gnus-newsgroup-name
7832                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7833        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7834        t
7835        (cons (current-buffer) 'summary)))
7836      (t
7837       (error "Couldn't select virtual nndoc group")))))
7838
7839 (defun gnus-summary-isearch-article (&optional regexp-p)
7840   "Do incremental search forward on the current article.
7841 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7842   (interactive "P")
7843   (let* ((gnus-inhibit-treatment t)
7844          (old (gnus-summary-select-article)))
7845     (gnus-configure-windows 'article)
7846     (gnus-eval-in-buffer-window gnus-article-buffer
7847       (save-restriction
7848         (widen)
7849         (when (eq 'old old)
7850           (gnus-article-show-all-headers))
7851         (goto-char (point-min))
7852         (isearch-forward regexp-p)))))
7853
7854 (defun gnus-summary-search-article-forward (regexp &optional backward)
7855   "Search for an article containing REGEXP forward.
7856 If BACKWARD, search backward instead."
7857   (interactive
7858    (list (read-string
7859           (format "Search article %s (regexp%s): "
7860                   (if current-prefix-arg "backward" "forward")
7861                   (if gnus-last-search-regexp
7862                       (concat ", default " gnus-last-search-regexp)
7863                     "")))
7864          current-prefix-arg))
7865   (if (string-equal regexp "")
7866       (setq regexp (or gnus-last-search-regexp ""))
7867     (setq gnus-last-search-regexp regexp)
7868     (setq gnus-article-before-search gnus-current-article))
7869   ;; Intentionally set gnus-last-article.
7870   (setq gnus-last-article gnus-article-before-search)
7871   (let ((gnus-last-article gnus-last-article))
7872     (if (gnus-summary-search-article regexp backward)
7873         (gnus-summary-show-thread)
7874       (error "Search failed: \"%s\"" regexp))))
7875
7876 (defun gnus-summary-search-article-backward (regexp)
7877   "Search for an article containing REGEXP backward."
7878   (interactive
7879    (list (read-string
7880           (format "Search article backward (regexp%s): "
7881                   (if gnus-last-search-regexp
7882                       (concat ", default " gnus-last-search-regexp)
7883                     "")))))
7884   (gnus-summary-search-article-forward regexp 'backward))
7885
7886 (eval-when-compile
7887   (defmacro gnus-summary-search-article-position-point (regexp backward)
7888     "Dehighlight the last matched text and goto the beginning position."
7889     (` (if (and gnus-summary-search-article-matched-data
7890                 (let ((text (caddr gnus-summary-search-article-matched-data))
7891                       (inhibit-read-only t)
7892                       buffer-read-only)
7893                   (delete-region
7894                    (goto-char (car gnus-summary-search-article-matched-data))
7895                    (cadr gnus-summary-search-article-matched-data))
7896                   (insert text)
7897                   (string-match (, regexp) text)))
7898            (if (, backward) (beginning-of-line) (end-of-line))
7899          (goto-char (if (, backward) (point-max) (point-min))))))
7900
7901   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7902     "Place point where X-Face image is displayed."
7903     (if (featurep 'xemacs)
7904         (` (let ((end (if (search-forward "\n\n" nil t)
7905                           (goto-char (1- (point)))
7906                         (point-min)))
7907                  extent)
7908              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7909              (unless (and (re-search-forward "^From:" end t)
7910                           (setq extent (extent-at (point)))
7911                           (extent-begin-glyph extent))
7912                (goto-char (, opoint)))))
7913       (` (let ((end (if (search-forward "\n\n" nil t)
7914                         (goto-char (1- (point)))
7915                       (point-min)))
7916                (start (or (search-backward "\n\n" nil t) (point-min))))
7917            (goto-char
7918             (or (text-property-any start end 'x-face-image t);; x-face-e21
7919                 (text-property-any start end 'x-face-mule-bitmap-image t)
7920                 (, opoint)))))))
7921
7922   (defmacro gnus-summary-search-article-highlight-matched-text
7923     (backward treated x-face)
7924     "Highlight matched text in the function `gnus-summary-search-article'."
7925     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7926              (end (set-marker (make-marker) (match-end 0)))
7927              (inhibit-read-only t)
7928              buffer-read-only)
7929          (unless treated
7930            (let ((,@
7931                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7932                     (mapcar
7933                      (lambda (item) (setq items (delq item items)))
7934                      '(gnus-treat-buttonize
7935                        gnus-treat-fill-article
7936                        gnus-treat-fill-long-lines
7937                        gnus-treat-emphasize
7938                        gnus-treat-highlight-headers
7939                        gnus-treat-highlight-citation
7940                        gnus-treat-highlight-signature
7941                        gnus-treat-overstrike
7942                        gnus-treat-display-xface
7943                        gnus-treat-buttonize-head
7944                        gnus-treat-decode-article-as-default-mime-charset))
7945                     (static-if (featurep 'xemacs)
7946                         items
7947                       (cons '(x-face-mule-delete-x-face-field
7948                               (quote never))
7949                             items))))
7950                  (gnus-treat-display-xface
7951                   (when (, x-face) gnus-treat-display-xface)))
7952              (gnus-article-prepare-mime-display)))
7953          (goto-char (if (, backward) start end))
7954          (when (, x-face)
7955            (gnus-summary-search-article-highlight-goto-x-face (point)))
7956          (setq gnus-summary-search-article-matched-data
7957                (list start end (buffer-substring start end)))
7958          (unless (eq start end);; matched text has been deleted. :-<
7959            (put-text-property start end 'face
7960                               (or (find-face 'isearch)
7961                                   'secondary-selection))))))
7962   )
7963
7964 (defun gnus-summary-search-article (regexp &optional backward)
7965   "Search for an article containing REGEXP.
7966 Optional argument BACKWARD means do search for backward.
7967 `gnus-select-article-hook' is not called during the search."
7968   ;; We have to require this here to make sure that the following
7969   ;; dynamic binding isn't shadowed by autoloading.
7970   (require 'gnus-async)
7971   (require 'gnus-art)
7972   (let ((gnus-select-article-hook nil)  ;Disable hook.
7973         (gnus-article-prepare-hook nil)
7974         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7975         (gnus-use-article-prefetch nil)
7976         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7977         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7978         (sum (current-buffer))
7979         (found nil)
7980         point treated)
7981     (gnus-save-hidden-threads
7982       (static-if (featurep 'xemacs)
7983           (let ((gnus-inhibit-treatment t))
7984             (setq treated (eq 'old (gnus-summary-select-article)))
7985             (when (and treated
7986                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7987                                  (window-live-p (get-buffer-window
7988                                                  gnus-article-buffer t)))))
7989               (gnus-summary-select-article nil t)
7990               (setq treated nil)))
7991         (let ((gnus-inhibit-treatment t)
7992               (x-face-mule-delete-x-face-field 'never))
7993           (setq treated (eq 'old (gnus-summary-select-article)))
7994           (when (and treated
7995                      (not
7996                       (and (gnus-buffer-live-p gnus-article-buffer)
7997                            (window-live-p (get-buffer-window
7998                                            gnus-article-buffer t))
7999                            (or (not (string-match "^\\^X-Face:" regexp))
8000                                (with-current-buffer gnus-article-buffer
8001                                  gnus-summary-search-article-matched-data)))))
8002             (gnus-summary-select-article nil t)
8003             (setq treated nil))))
8004       (set-buffer gnus-article-buffer)
8005       (widen)
8006       (if treated
8007           (progn
8008             (gnus-article-show-all-headers)
8009             (gnus-summary-search-article-position-point regexp backward))
8010         (goto-char (if backward (point-max) (point-min))))
8011       (while (not found)
8012         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8013         (if (if backward
8014                 (re-search-backward regexp nil t)
8015               (re-search-forward regexp nil t))
8016             ;; We found the regexp.
8017             (progn
8018               (gnus-summary-search-article-highlight-matched-text
8019                backward treated (string-match "^\\^X-Face:" regexp))
8020               (setq found 'found)
8021               (forward-line
8022                (/ (- 2 (window-height
8023                         (get-buffer-window gnus-article-buffer t)))
8024                   2))
8025               (set-window-start
8026                (get-buffer-window (current-buffer))
8027                (point))
8028               (set-buffer sum)
8029               (setq point (point)))
8030           ;; We didn't find it, so we go to the next article.
8031           (set-buffer sum)
8032           (setq found 'not)
8033           (while (eq found 'not)
8034             (if (not (if backward (gnus-summary-find-prev)
8035                        (gnus-summary-find-next)))
8036                 ;; No more articles.
8037                 (setq found t)
8038               ;; Select the next article and adjust point.
8039               (unless (gnus-summary-article-sparse-p
8040                        (gnus-summary-article-number))
8041                 (setq found nil)
8042                 (let ((gnus-inhibit-treatment t))
8043                   (gnus-summary-select-article))
8044                 (setq treated nil)
8045                 (set-buffer gnus-article-buffer)
8046                 (widen)
8047                 (goto-char (if backward (point-max) (point-min))))))))
8048       (gnus-message 7 ""))
8049     ;; Return whether we found the regexp.
8050     (when (eq found 'found)
8051       (goto-char point)
8052       (gnus-summary-show-thread)
8053       (gnus-summary-goto-subject gnus-current-article)
8054       (gnus-summary-position-point)
8055       t)))
8056
8057 (defun gnus-find-matching-articles (header regexp)
8058   "Return a list of all articles that match REGEXP on HEADER.
8059 This search includes all articles in the current group that Gnus has
8060 fetched headers for, whether they are displayed or not."
8061   (let ((articles nil)
8062         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8063         (case-fold-search t))
8064     (dolist (header gnus-newsgroup-headers)
8065       (when (string-match regexp (funcall func header))
8066         (push (mail-header-number header) articles)))
8067     (nreverse articles)))
8068
8069 (defun gnus-summary-find-matching (header regexp &optional backward unread
8070                                           not-case-fold not-matching)
8071   "Return a list of all articles that match REGEXP on HEADER.
8072 The search stars on the current article and goes forwards unless
8073 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8074 If UNREAD is non-nil, only unread articles will
8075 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8076 in the comparisons. If NOT-MATCHING, return a list of all articles that
8077 not match REGEXP on HEADER."
8078   (let ((case-fold-search (not not-case-fold))
8079         articles d func)
8080     (if (consp header)
8081         (if (eq (car header) 'extra)
8082             (setq func
8083                   `(lambda (h)
8084                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8085                          "")))
8086           (error "%s is an invalid header" header))
8087       (unless (fboundp (intern (concat "mail-header-" header)))
8088         (error "%s is not a valid header" header))
8089       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8090     (dolist (d (if (eq backward 'all)
8091                    gnus-newsgroup-data
8092                  (gnus-data-find-list
8093                   (gnus-summary-article-number)
8094                   (gnus-data-list backward))))
8095       (when (and (or (not unread)       ; We want all articles...
8096                      (gnus-data-unread-p d)) ; Or just unreads.
8097                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8098                  (if not-matching
8099                      (not (string-match
8100                            regexp
8101                            (funcall func (gnus-data-header d))))
8102                    (string-match regexp
8103                                  (funcall func (gnus-data-header d)))))
8104         (push (gnus-data-number d) articles))) ; Success!
8105     (nreverse articles)))
8106
8107 (defun gnus-summary-execute-command (header regexp command &optional backward)
8108   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8109 If HEADER is an empty string (or nil), the match is done on the entire
8110 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8111   (interactive
8112    (list (let ((completion-ignore-case t))
8113            (completing-read
8114             "Header name: "
8115             (mapcar (lambda (header) (list (format "%s" header)))
8116                     (append
8117                      '("Number" "Subject" "From" "Lines" "Date"
8118                        "Message-ID" "Xref" "References" "Body")
8119                      gnus-extra-headers))
8120             nil 'require-match))
8121          (read-string "Regexp: ")
8122          (read-key-sequence "Command: ")
8123          current-prefix-arg))
8124   (when (equal header "Body")
8125     (setq header ""))
8126   ;; Hidden thread subtrees must be searched as well.
8127   (gnus-summary-show-all-threads)
8128   ;; We don't want to change current point nor window configuration.
8129   (save-excursion
8130     (save-window-excursion
8131       (gnus-message 6 "Executing %s..." (key-description command))
8132       ;; We'd like to execute COMMAND interactively so as to give arguments.
8133       (gnus-execute header regexp
8134                     `(call-interactively ',(key-binding command))
8135                     backward)
8136       (gnus-message 6 "Executing %s...done" (key-description command)))))
8137
8138 (defun gnus-summary-beginning-of-article ()
8139   "Scroll the article back to the beginning."
8140   (interactive)
8141   (gnus-summary-select-article)
8142   (gnus-configure-windows 'article)
8143   (gnus-eval-in-buffer-window gnus-article-buffer
8144     (widen)
8145     (goto-char (point-min))
8146     (when gnus-page-broken
8147       (gnus-narrow-to-page))))
8148
8149 (defun gnus-summary-end-of-article ()
8150   "Scroll to the end of the article."
8151   (interactive)
8152   (gnus-summary-select-article)
8153   (gnus-configure-windows 'article)
8154   (gnus-eval-in-buffer-window gnus-article-buffer
8155     (widen)
8156     (goto-char (point-max))
8157     (recenter -3)
8158     (when gnus-page-broken
8159       (gnus-narrow-to-page))))
8160
8161 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8162   "Truncate to LEN and quote all \"(\"'s in STRING."
8163   (gnus-replace-in-string (if (and len (> (length string) len))
8164                               (substring string 0 len)
8165                             string)
8166                           "[()]" "\\\\\\&"))
8167
8168 (defun gnus-summary-print-article (&optional filename n)
8169   "Generate and print a PostScript image of the N next (mail) articles.
8170
8171 If N is negative, print the N previous articles.  If N is nil and articles
8172 have been marked with the process mark, print these instead.
8173
8174 If the optional first argument FILENAME is nil, send the image to the
8175 printer.  If FILENAME is a string, save the PostScript image in a file with
8176 that name.  If FILENAME is a number, prompt the user for the name of the file
8177 to save in."
8178   (interactive (list (ps-print-preprint current-prefix-arg)))
8179   (dolist (article (gnus-summary-work-articles n))
8180     (gnus-summary-select-article nil nil 'pseudo article)
8181     (gnus-eval-in-buffer-window gnus-article-buffer
8182       (gnus-print-buffer))
8183     (gnus-summary-remove-process-mark article))
8184   (ps-despool filename))
8185
8186 (defun gnus-print-buffer ()
8187   (let ((buffer (generate-new-buffer " *print*")))
8188     (unwind-protect
8189         (progn
8190           (copy-to-buffer buffer (point-min) (point-max))
8191           (set-buffer buffer)
8192           (gnus-article-delete-invisible-text)
8193           (when (gnus-visual-p 'article-highlight 'highlight)
8194             ;; Copy-to-buffer doesn't copy overlay.  So redo
8195             ;; highlight.
8196             (let ((gnus-article-buffer buffer))
8197               (gnus-article-highlight-citation t)
8198               (gnus-article-highlight-signature)))
8199           (let ((ps-left-header
8200                  (list
8201                   (concat "("
8202                           (gnus-summary-print-truncate-and-quote
8203                            (mail-header-subject gnus-current-headers)
8204                            66) ")")
8205                   (concat "("
8206                           (gnus-summary-print-truncate-and-quote
8207                            (mail-header-from gnus-current-headers)
8208                            45) ")")))
8209                 (ps-right-header
8210                  (list
8211                   "/pagenumberstring load"
8212                   (concat "("
8213                           (mail-header-date gnus-current-headers) ")"))))
8214             (gnus-run-hooks 'gnus-ps-print-hook)
8215             (save-excursion
8216               (if window-system
8217                   (ps-spool-buffer-with-faces)
8218                 (ps-spool-buffer)))))
8219       (kill-buffer buffer))))
8220
8221 (defun gnus-summary-show-article (&optional arg)
8222   "Force redisplaying of the current article.
8223 If ARG (the prefix) is a number, show the article with the charset
8224 defined in `gnus-summary-show-article-charset-alist', or the charset
8225 input.
8226 If ARG (the prefix) is non-nil and not a number, show the raw article
8227 without any article massaging functions being run.  Normally, the key strokes
8228 are `C-u g'."
8229   (interactive "P")
8230   (cond
8231    ((numberp arg)
8232     (gnus-summary-show-article t)
8233     (let* ((gnus-newsgroup-charset
8234             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8235                 (mm-read-coding-system
8236                  "View as charset: " ;; actually it is coding system.
8237                  (save-excursion
8238                    (set-buffer gnus-article-buffer)
8239                    (mm-detect-coding-region (point) (point-max))))))
8240            (default-mime-charset gnus-newsgroup-charset)
8241            (gnus-newsgroup-ignored-charsets 'gnus-all))
8242       (gnus-summary-select-article nil 'force)
8243       (let ((deps gnus-newsgroup-dependencies)
8244             head header lines)
8245         (save-excursion
8246           (set-buffer gnus-original-article-buffer)
8247           (save-restriction
8248             (message-narrow-to-head)
8249             (setq head (buffer-string))
8250             (goto-char (point-min))
8251             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8252               (goto-char (point-max))
8253               (widen)
8254               (setq lines (1- (count-lines (point) (point-max))))))
8255           (with-temp-buffer
8256             (insert (format "211 %d Article retrieved.\n"
8257                             (cdr gnus-article-current)))
8258             (insert head)
8259             (if lines (insert (format "Lines: %d\n" lines)))
8260             (insert ".\n")
8261             (let ((nntp-server-buffer (current-buffer)))
8262               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8263         (gnus-data-set-header
8264          (gnus-data-find (cdr gnus-article-current))
8265          header)
8266         (gnus-summary-update-article-line
8267          (cdr gnus-article-current) header)
8268         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8269           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8270    ((not arg)
8271     ;; Select the article the normal way.
8272     (gnus-summary-select-article nil 'force))
8273    (t
8274     ;; We have to require this here to make sure that the following
8275     ;; dynamic binding isn't shadowed by autoloading.
8276     (require 'gnus-async)
8277     (require 'gnus-art)
8278     ;; Bind the article treatment functions to nil.
8279     (let ((gnus-have-all-headers t)
8280           gnus-article-prepare-hook
8281           gnus-article-decode-hook
8282           gnus-break-pages
8283           gnus-show-mime
8284           (gnus-inhibit-treatment t))
8285       (gnus-summary-select-article nil 'force))))
8286   (gnus-summary-goto-subject gnus-current-article)
8287   (gnus-summary-position-point))
8288
8289 (defun gnus-summary-show-raw-article ()
8290   "Show the raw article without any article massaging functions being run."
8291   (interactive)
8292   (gnus-summary-show-article t))
8293
8294 (defun gnus-summary-verbose-headers (&optional arg)
8295   "Toggle permanent full header display.
8296 If ARG is a positive number, turn header display on.
8297 If ARG is a negative number, turn header display off."
8298   (interactive "P")
8299   (setq gnus-show-all-headers
8300         (cond ((or (not (numberp arg))
8301                    (zerop arg))
8302                (not gnus-show-all-headers))
8303               ((natnump arg)
8304                t)))
8305   (gnus-summary-show-article))
8306
8307 (defun gnus-summary-toggle-header (&optional arg)
8308   "Show the headers if they are hidden, or hide them if they are shown.
8309 If ARG is a positive number, show the entire header.
8310 If ARG is a negative number, hide the unwanted header lines."
8311   (interactive "P")
8312   (save-excursion
8313     (set-buffer gnus-article-buffer)
8314     (save-restriction
8315       (let* ((buffer-read-only nil)
8316              (inhibit-point-motion-hooks t)
8317              hidden e)
8318         (setq hidden
8319               (if (numberp arg)
8320                   (>= arg 0)
8321                 (save-restriction
8322                   (article-narrow-to-head)
8323                   (gnus-article-hidden-text-p 'headers))))
8324         (goto-char (point-min))
8325         (when (search-forward "\n\n" nil t)
8326           (delete-region (point-min) (1- (point))))
8327         (goto-char (point-min))
8328         (save-excursion
8329           (set-buffer gnus-original-article-buffer)
8330           (goto-char (point-min))
8331           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8332         (insert-buffer-substring gnus-original-article-buffer 1 e)
8333         (save-restriction
8334           (narrow-to-region (point-min) (point))
8335           (article-decode-encoded-words)
8336           (if  hidden
8337               (let ((gnus-treat-hide-headers nil)
8338                     (gnus-treat-hide-boring-headers nil))
8339                 (gnus-delete-wash-type 'headers)
8340                 (gnus-treat-article 'head))
8341             (gnus-treat-article 'head)))
8342         (gnus-set-mode-line 'article)))))
8343
8344 (defun gnus-summary-show-all-headers ()
8345   "Make all header lines visible."
8346   (interactive)
8347   (gnus-summary-toggle-header 1))
8348
8349 (defun gnus-summary-toggle-mime (&optional arg)
8350   "Toggle MIME processing.
8351 If ARG is a positive number, turn MIME processing on."
8352   (interactive "P")
8353   (setq gnus-show-mime
8354         (if (null arg)
8355             (not gnus-show-mime)
8356           (> (prefix-numeric-value arg) 0)))
8357   (gnus-summary-select-article t 'force))
8358
8359 (defun gnus-summary-caesar-message (&optional arg)
8360   "Caesar rotate the current article by 13.
8361 The numerical prefix specifies how many places to rotate each letter
8362 forward."
8363   (interactive "P")
8364   (gnus-summary-select-article)
8365   (let ((mail-header-separator ""))
8366     (gnus-eval-in-buffer-window gnus-article-buffer
8367       (save-restriction
8368         (widen)
8369         (let ((start (window-start))
8370               buffer-read-only)
8371           (message-caesar-buffer-body arg)
8372           (set-window-start (get-buffer-window (current-buffer)) start))))))
8373
8374 (defun gnus-summary-stop-page-breaking ()
8375   "Stop page breaking in the current article."
8376   (interactive)
8377   (gnus-summary-select-article)
8378   (gnus-eval-in-buffer-window gnus-article-buffer
8379     (widen)
8380     (when (gnus-visual-p 'page-marker)
8381       (let ((buffer-read-only nil))
8382         (gnus-remove-text-with-property 'gnus-prev)
8383         (gnus-remove-text-with-property 'gnus-next))
8384       (setq gnus-page-broken nil))))
8385
8386 (defun gnus-summary-move-article (&optional n to-newsgroup
8387                                             select-method action)
8388   "Move the current article to a different newsgroup.
8389 If N is a positive number, move the N next articles.
8390 If N is a negative number, move the N previous articles.
8391 If N is nil and any articles have been marked with the process mark,
8392 move those articles instead.
8393 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8394 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8395 re-spool using this method.
8396
8397 For this function to work, both the current newsgroup and the
8398 newsgroup that you want to move to have to support the `request-move'
8399 and `request-accept' functions.
8400
8401 ACTION can be either `move' (the default), `crosspost' or `copy'."
8402   (interactive "P")
8403   (unless action
8404     (setq action 'move))
8405   ;; Check whether the source group supports the required functions.
8406   (cond ((and (eq action 'move)
8407               (not (gnus-check-backend-function
8408                     'request-move-article gnus-newsgroup-name)))
8409          (error "The current group does not support article moving"))
8410         ((and (eq action 'crosspost)
8411               (not (gnus-check-backend-function
8412                     'request-replace-article gnus-newsgroup-name)))
8413          (error "The current group does not support article editing")))
8414   (let ((articles (gnus-summary-work-articles n))
8415         (prefix (if (gnus-check-backend-function
8416                      'request-move-article gnus-newsgroup-name)
8417                     (gnus-group-real-prefix gnus-newsgroup-name)
8418                   ""))
8419         (names '((move "Move" "Moving")
8420                  (copy "Copy" "Copying")
8421                  (crosspost "Crosspost" "Crossposting")))
8422         (copy-buf (save-excursion
8423                     (nnheader-set-temp-buffer " *copy article*")))
8424         (default-marks gnus-article-mark-lists)
8425         (no-expire-marks (delete '(expirable . expire)
8426                                  (copy-sequence gnus-article-mark-lists)))
8427         art-group to-method new-xref article to-groups)
8428     (unless (assq action names)
8429       (error "Unknown action %s" action))
8430     ;; We have to select an article to give
8431     ;; `gnus-read-move-group-name' an opportunity to suggest an
8432     ;; appropriate default.
8433     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8434       (let ((gnus-display-mime-function nil)
8435             (gnus-article-prepare-hook nil))
8436         (gnus-summary-select-article nil nil nil (car articles))))
8437     ;; Read the newsgroup name.
8438     (when (and (not to-newsgroup)
8439                (not select-method))
8440       (setq to-newsgroup
8441             (gnus-read-move-group-name
8442              (cadr (assq action names))
8443              (symbol-value (intern (format "gnus-current-%s-group" action)))
8444              articles prefix))
8445       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8446     (setq to-method (or select-method
8447                         (gnus-server-to-method
8448                          (gnus-group-method to-newsgroup))))
8449     ;; Check the method we are to move this article to...
8450     (unless (gnus-check-backend-function
8451              'request-accept-article (car to-method))
8452       (error "%s does not support article copying" (car to-method)))
8453     (unless (gnus-check-server to-method)
8454       (error "Can't open server %s" (car to-method)))
8455     (gnus-message 6 "%s to %s: %s..."
8456                   (caddr (assq action names))
8457                   (or (car select-method) to-newsgroup) articles)
8458     (while articles
8459       (setq article (pop articles))
8460       (setq
8461        art-group
8462        (cond
8463         ;; Move the article.
8464         ((eq action 'move)
8465          ;; Remove this article from future suppression.
8466          (gnus-dup-unsuppress-article article)
8467          (gnus-request-move-article
8468           article                       ; Article to move
8469           gnus-newsgroup-name           ; From newsgroup
8470           (nth 1 (gnus-find-method-for-group
8471                   gnus-newsgroup-name)) ; Server
8472           (list 'gnus-request-accept-article
8473                 to-newsgroup (list 'quote select-method)
8474                 (not articles) t)       ; Accept form
8475           (not articles)))              ; Only save nov last time
8476         ;; Copy the article.
8477         ((eq action 'copy)
8478          (save-excursion
8479            (set-buffer copy-buf)
8480            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8481              (gnus-request-accept-article
8482               to-newsgroup select-method (not articles) t))))
8483         ;; Crosspost the article.
8484         ((eq action 'crosspost)
8485          (let ((xref (message-tokenize-header
8486                       (mail-header-xref (gnus-summary-article-header article))
8487                       " ")))
8488            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8489                                   ":" (number-to-string article)))
8490            (unless xref
8491              (setq xref (list (system-name))))
8492            (setq new-xref
8493                  (concat
8494                   (mapconcat 'identity
8495                              (delete "Xref:" (delete new-xref xref))
8496                              " ")
8497                   " " new-xref))
8498            (save-excursion
8499              (set-buffer copy-buf)
8500              ;; First put the article in the destination group.
8501              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8502              (when (consp (setq art-group
8503                                 (gnus-request-accept-article
8504                                  to-newsgroup select-method (not articles))))
8505                (setq new-xref (concat new-xref " " (car art-group)
8506                                       ":"
8507                                       (number-to-string (cdr art-group))))
8508                ;; Now we have the new Xrefs header, so we insert
8509                ;; it and replace the new article.
8510                (nnheader-replace-header "Xref" new-xref)
8511                (gnus-request-replace-article
8512                 (cdr art-group) to-newsgroup (current-buffer))
8513                art-group))))))
8514       (cond
8515        ((not art-group)
8516         (gnus-message 1 "Couldn't %s article %s: %s"
8517                       (cadr (assq action names)) article
8518                       (nnheader-get-report (car to-method))))
8519        ((eq art-group 'junk)
8520         (when (eq action 'move)
8521           (gnus-summary-mark-article article gnus-canceled-mark)
8522           (gnus-message 4 "Deleted article %s" article)))
8523        (t
8524         (let* ((pto-group (gnus-group-prefixed-name
8525                            (car art-group) to-method))
8526                (entry
8527                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8528                (info (nth 2 entry))
8529                (to-group (gnus-info-group info))
8530                to-marks)
8531           ;; Update the group that has been moved to.
8532           (when (and info
8533                      (memq action '(move copy)))
8534             (unless (member to-group to-groups)
8535               (push to-group to-groups))
8536
8537             (unless (memq article gnus-newsgroup-unreads)
8538               (push 'read to-marks)
8539               (gnus-info-set-read
8540                info (gnus-add-to-range (gnus-info-read info)
8541                                        (list (cdr art-group)))))
8542
8543             ;; See whether the article is to be put in the cache.
8544             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8545                              default-marks
8546                            no-expire-marks))
8547                   (to-article (cdr art-group)))
8548
8549               ;; Enter the article into the cache in the new group,
8550               ;; if that is required.
8551               (when gnus-use-cache
8552                 (gnus-cache-possibly-enter-article
8553                  to-group to-article
8554                  (let ((header (copy-sequence
8555                                 (gnus-summary-article-header article))))
8556                    (mail-header-set-number header to-article)
8557                    header)
8558                  (memq article gnus-newsgroup-marked)
8559                  (memq article gnus-newsgroup-dormant)
8560                  (memq article gnus-newsgroup-unreads)))
8561
8562               (when gnus-preserve-marks
8563                 ;; Copy any marks over to the new group.
8564                 (when (and (equal to-group gnus-newsgroup-name)
8565                            (not (memq article gnus-newsgroup-unreads)))
8566                   ;; Mark this article as read in this group.
8567                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8568                   (setcdr (gnus-active to-group) to-article)
8569                   (setcdr gnus-newsgroup-active to-article))
8570
8571                 (while marks
8572                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8573                     (when (memq article (symbol-value
8574                                          (intern (format "gnus-newsgroup-%s"
8575                                                          (caar marks)))))
8576                       (push (cdar marks) to-marks)
8577                       ;; If the other group is the same as this group,
8578                       ;; then we have to add the mark to the list.
8579                       (when (equal to-group gnus-newsgroup-name)
8580                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8581                              (cons to-article
8582                                    (symbol-value
8583                                     (intern (format "gnus-newsgroup-%s"
8584                                                     (caar marks)))))))
8585                       ;; Copy the marks to other group.
8586                       (gnus-add-marked-articles
8587                        to-group (cdar marks) (list to-article) info)))
8588                   (setq marks (cdr marks)))
8589
8590                 (gnus-request-set-mark to-group (list (list (list to-article)
8591                                                             'add
8592                                                             to-marks))))
8593
8594               (gnus-dribble-enter
8595                (concat "(gnus-group-set-info '"
8596                        (gnus-prin1-to-string (gnus-get-info to-group))
8597                        ")"))))
8598
8599           ;; Update the Xref header in this article to point to
8600           ;; the new crossposted article we have just created.
8601           (when (eq action 'crosspost)
8602             (save-excursion
8603               (set-buffer copy-buf)
8604               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8605               (nnheader-replace-header "Xref" new-xref)
8606               (gnus-request-replace-article
8607                article gnus-newsgroup-name (current-buffer)))))
8608
8609         ;;;!!!Why is this necessary?
8610         (set-buffer gnus-summary-buffer)
8611
8612         (gnus-summary-goto-subject article)
8613         (when (eq action 'move)
8614           (gnus-summary-mark-article article gnus-canceled-mark))))
8615       (gnus-summary-remove-process-mark article))
8616     ;; Re-activate all groups that have been moved to.
8617     (save-excursion
8618       (set-buffer gnus-group-buffer)
8619       (let ((gnus-group-marked to-groups))
8620         (gnus-group-get-new-news-this-group nil t)))
8621
8622     (gnus-kill-buffer copy-buf)
8623     (gnus-summary-position-point)
8624     (gnus-set-mode-line 'summary)))
8625
8626 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8627   "Move the current article to a different newsgroup.
8628 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8629 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8630 re-spool using this method."
8631   (interactive "P")
8632   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8633
8634 (defun gnus-summary-crosspost-article (&optional n)
8635   "Crosspost the current article to some other group."
8636   (interactive "P")
8637   (gnus-summary-move-article n nil nil 'crosspost))
8638
8639 (defcustom gnus-summary-respool-default-method nil
8640   "Default method type for respooling an article.
8641 If nil, use to the current newsgroup method."
8642   :type 'symbol
8643   :group 'gnus-summary-mail)
8644
8645 (defun gnus-summary-respool-article (&optional n method)
8646   "Respool the current article.
8647 The article will be squeezed through the mail spooling process again,
8648 which means that it will be put in some mail newsgroup or other
8649 depending on `nnmail-split-methods'.
8650 If N is a positive number, respool the N next articles.
8651 If N is a negative number, respool the N previous articles.
8652 If N is nil and any articles have been marked with the process mark,
8653 respool those articles instead.
8654
8655 Respooling can be done both from mail groups and \"real\" newsgroups.
8656 In the former case, the articles in question will be moved from the
8657 current group into whatever groups they are destined to.  In the
8658 latter case, they will be copied into the relevant groups."
8659   (interactive
8660    (list current-prefix-arg
8661          (let* ((methods (gnus-methods-using 'respool))
8662                 (methname
8663                  (symbol-name (or gnus-summary-respool-default-method
8664                                   (car (gnus-find-method-for-group
8665                                         gnus-newsgroup-name)))))
8666                 (method
8667                  (gnus-completing-read-with-default
8668                   methname "What backend do you want to use when respooling?"
8669                   methods nil t nil 'gnus-mail-method-history))
8670                 ms)
8671            (cond
8672             ((zerop (length (setq ms (gnus-servers-using-backend
8673                                       (intern method)))))
8674              (list (intern method) ""))
8675             ((= 1 (length ms))
8676              (car ms))
8677             (t
8678              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8679                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8680                            ms-alist))))))))
8681   (unless method
8682     (error "No method given for respooling"))
8683   (if (assoc (symbol-name
8684               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8685              (gnus-methods-using 'respool))
8686       (gnus-summary-move-article n nil method)
8687     (gnus-summary-copy-article n nil method)))
8688
8689 (defun gnus-summary-import-article (file &optional edit)
8690   "Import an arbitrary file into a mail newsgroup."
8691   (interactive "fImport file: \nP")
8692   (let ((group gnus-newsgroup-name)
8693         (now (current-time))
8694         atts lines group-art)
8695     (unless (gnus-check-backend-function 'request-accept-article group)
8696       (error "%s does not support article importing" group))
8697     (or (file-readable-p file)
8698         (not (file-regular-p file))
8699         (error "Can't read %s" file))
8700     (save-excursion
8701       (set-buffer (gnus-get-buffer-create " *import file*"))
8702       (erase-buffer)
8703       (nnheader-insert-file-contents file)
8704       (goto-char (point-min))
8705       (if (nnheader-article-p)
8706           (save-restriction
8707             (goto-char (point-min))
8708             (search-forward "\n\n" nil t)
8709             (narrow-to-region (point-min) (1- (point)))
8710             (goto-char (point-min))
8711             (unless (re-search-forward "^date:" nil t)
8712               (goto-char (point-max))
8713               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8714         ;; This doesn't look like an article, so we fudge some headers.
8715         (setq atts (file-attributes file)
8716               lines (count-lines (point-min) (point-max)))
8717         (insert "From: " (read-string "From: ") "\n"
8718                 "Subject: " (read-string "Subject: ") "\n"
8719                 "Date: " (message-make-date (nth 5 atts)) "\n"
8720                 "Message-ID: " (message-make-message-id) "\n"
8721                 "Lines: " (int-to-string lines) "\n"
8722                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8723       (setq group-art (gnus-request-accept-article group nil t))
8724       (kill-buffer (current-buffer)))
8725     (setq gnus-newsgroup-active (gnus-activate-group group))
8726     (forward-line 1)
8727     (gnus-summary-goto-article (cdr group-art) nil t)
8728     (when edit
8729       (gnus-summary-edit-article))))
8730
8731 (defun gnus-summary-create-article ()
8732   "Create an article in a mail newsgroup."
8733   (interactive)
8734   (let ((group gnus-newsgroup-name)
8735         (now (current-time))
8736         group-art)
8737     (unless (gnus-check-backend-function 'request-accept-article group)
8738       (error "%s does not support article importing" group))
8739     (save-excursion
8740       (set-buffer (gnus-get-buffer-create " *import file*"))
8741       (erase-buffer)
8742       (goto-char (point-min))
8743       ;; This doesn't look like an article, so we fudge some headers.
8744       (insert "From: " (read-string "From: ") "\n"
8745               "Subject: " (read-string "Subject: ") "\n"
8746               "Date: " (message-make-date now) "\n"
8747               "Message-ID: " (message-make-message-id) "\n")
8748       (setq group-art (gnus-request-accept-article group nil t))
8749       (kill-buffer (current-buffer)))
8750     (setq gnus-newsgroup-active (gnus-activate-group group))
8751     (forward-line 1)
8752     (gnus-summary-goto-article (cdr group-art) nil t)
8753     (gnus-summary-edit-article)))
8754
8755 (defun gnus-summary-article-posted-p ()
8756   "Say whether the current (mail) article is available from news as well.
8757 This will be the case if the article has both been mailed and posted."
8758   (interactive)
8759   (let ((id (mail-header-references (gnus-summary-article-header)))
8760         (gnus-override-method (car (gnus-refer-article-methods))))
8761     (if (gnus-request-head id "")
8762         (gnus-message 2 "The current message was found on %s"
8763                       gnus-override-method)
8764       (gnus-message 2 "The current message couldn't be found on %s"
8765                     gnus-override-method)
8766       nil)))
8767
8768 (defun gnus-summary-expire-articles (&optional now)
8769   "Expire all articles that are marked as expirable in the current group."
8770   (interactive)
8771   (when (gnus-check-backend-function
8772          'request-expire-articles gnus-newsgroup-name)
8773     ;; This backend supports expiry.
8774     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8775            (expirable (if total
8776                           (progn
8777                             ;; We need to update the info for
8778                             ;; this group for `gnus-list-of-read-articles'
8779                             ;; to give us the right answer.
8780                             (gnus-run-hooks 'gnus-exit-group-hook)
8781                             (gnus-summary-update-info)
8782                             (gnus-list-of-read-articles gnus-newsgroup-name))
8783                         (setq gnus-newsgroup-expirable
8784                               (sort gnus-newsgroup-expirable '<))))
8785            (expiry-wait (if now 'immediate
8786                           (gnus-group-find-parameter
8787                            gnus-newsgroup-name 'expiry-wait)))
8788            (nnmail-expiry-target
8789             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8790                 nnmail-expiry-target))
8791            es)
8792       (when expirable
8793         ;; There are expirable articles in this group, so we run them
8794         ;; through the expiry process.
8795         (gnus-message 6 "Expiring articles...")
8796         (unless (gnus-check-group gnus-newsgroup-name)
8797           (error "Can't open server for %s" gnus-newsgroup-name))
8798         ;; The list of articles that weren't expired is returned.
8799         (save-excursion
8800           (if expiry-wait
8801               (let ((nnmail-expiry-wait-function nil)
8802                     (nnmail-expiry-wait expiry-wait))
8803                 (setq es (gnus-request-expire-articles
8804                           expirable gnus-newsgroup-name)))
8805             (setq es (gnus-request-expire-articles
8806                       expirable gnus-newsgroup-name)))
8807           (unless total
8808             (setq gnus-newsgroup-expirable es))
8809           ;; We go through the old list of expirable, and mark all
8810           ;; really expired articles as nonexistent.
8811           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8812             (let ((gnus-use-cache nil))
8813               (dolist (article expirable)
8814                 (when (and (not (memq article es))
8815                            (gnus-data-find article))
8816                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8817         (gnus-message 6 "Expiring articles...done")))))
8818
8819 (defun gnus-summary-expire-articles-now ()
8820   "Expunge all expirable articles in the current group.
8821 This means that *all* articles that are marked as expirable will be
8822 deleted forever, right now."
8823   (interactive)
8824   (or gnus-expert-user
8825       (gnus-yes-or-no-p
8826        "Are you really, really, really sure you want to delete all these messages? ")
8827       (error "Phew!"))
8828   (gnus-summary-expire-articles t))
8829
8830 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8831 (defun gnus-summary-delete-article (&optional n)
8832   "Delete the N next (mail) articles.
8833 This command actually deletes articles.  This is not a marking
8834 command.  The article will disappear forever from your life, never to
8835 return.
8836 If N is negative, delete backwards.
8837 If N is nil and articles have been marked with the process mark,
8838 delete these instead."
8839   (interactive "P")
8840   (unless (gnus-check-backend-function 'request-expire-articles
8841                                        gnus-newsgroup-name)
8842     (error "The current newsgroup does not support article deletion"))
8843   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8844     (error "Couldn't open server"))
8845   ;; Compute the list of articles to delete.
8846   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8847         not-deleted)
8848     (if (and gnus-novice-user
8849              (not (gnus-yes-or-no-p
8850                    (format "Do you really want to delete %s forever? "
8851                            (if (> (length articles) 1)
8852                                (format "these %s articles" (length articles))
8853                              "this article")))))
8854         ()
8855       ;; Delete the articles.
8856       (setq not-deleted (gnus-request-expire-articles
8857                          articles gnus-newsgroup-name 'force))
8858       (while articles
8859         (gnus-summary-remove-process-mark (car articles))
8860         ;; The backend might not have been able to delete the article
8861         ;; after all.
8862         (unless (memq (car articles) not-deleted)
8863           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8864         (setq articles (cdr articles)))
8865       (when not-deleted
8866         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8867     (gnus-summary-position-point)
8868     (gnus-set-mode-line 'summary)
8869     not-deleted))
8870
8871 (defun gnus-summary-edit-article (&optional force)
8872   "Edit the current article.
8873 This will have permanent effect only in mail groups.
8874 If FORCE is non-nil, allow editing of articles even in read-only
8875 groups."
8876   (interactive "P")
8877   (save-excursion
8878     (set-buffer gnus-summary-buffer)
8879     (let ((mail-parse-charset gnus-newsgroup-charset)
8880           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8881       (gnus-set-global-variables)
8882       (when (and (not force)
8883                  (gnus-group-read-only-p))
8884         (error "The current newsgroup does not support article editing"))
8885       (gnus-summary-show-article t)
8886       (gnus-article-edit-article
8887        'ignore
8888        `(lambda (no-highlight)
8889           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8890                 (message-options message-options)
8891                 (message-options-set-recipient)
8892                 (mail-parse-ignored-charsets
8893                  ',gnus-newsgroup-ignored-charsets))
8894             (gnus-summary-edit-article-done
8895              ,(or (mail-header-references gnus-current-headers) "")
8896              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8897
8898 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8899
8900 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8901                                                  no-highlight)
8902   "Make edits to the current article permanent."
8903   (interactive)
8904   (save-excursion
8905     ;; The buffer restriction contains the entire article if it exists.
8906     (when (article-goto-body)
8907       (let ((lines (count-lines (point) (point-max)))
8908             (length (- (point-max) (point)))
8909             (case-fold-search t)
8910             (body (copy-marker (point))))
8911         (goto-char (point-min))
8912         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8913           (delete-region (match-beginning 1) (match-end 1))
8914           (insert (number-to-string length)))
8915         (goto-char (point-min))
8916         (when (re-search-forward
8917                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8918           (delete-region (match-beginning 1) (match-end 1))
8919           (insert (number-to-string length)))
8920         (goto-char (point-min))
8921         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8922           (delete-region (match-beginning 1) (match-end 1))
8923           (insert (number-to-string lines))))))
8924   ;; Replace the article.
8925   (let ((buf (current-buffer)))
8926     (with-temp-buffer
8927       (insert-buffer-substring buf)
8928
8929       (if (and (not read-only)
8930                (not (gnus-request-replace-article
8931                      (cdr gnus-article-current) (car gnus-article-current)
8932                      (current-buffer) t)))
8933           (error "Couldn't replace article")
8934         ;; Update the summary buffer.
8935         (if (and references
8936                  (equal (message-tokenize-header references " ")
8937                         (message-tokenize-header
8938                          (or (message-fetch-field "references") "") " ")))
8939             ;; We only have to update this line.
8940             (save-excursion
8941               (save-restriction
8942                 (message-narrow-to-head)
8943                 (let ((head (buffer-string))
8944                       header)
8945                   (with-temp-buffer
8946                     (insert (format "211 %d Article retrieved.\n"
8947                                     (cdr gnus-article-current)))
8948                     (insert head)
8949                     (insert ".\n")
8950                     (let ((nntp-server-buffer (current-buffer)))
8951                       (setq header (car (gnus-get-newsgroup-headers
8952                                          (save-excursion
8953                                            (set-buffer gnus-summary-buffer)
8954                                            gnus-newsgroup-dependencies)
8955                                          t))))
8956                     (save-excursion
8957                       (set-buffer gnus-summary-buffer)
8958                       (gnus-data-set-header
8959                        (gnus-data-find (cdr gnus-article-current))
8960                        header)
8961                       (gnus-summary-update-article-line
8962                        (cdr gnus-article-current) header)
8963                       (if (gnus-summary-goto-subject
8964                            (cdr gnus-article-current) nil t)
8965                           (gnus-summary-update-secondary-mark
8966                            (cdr gnus-article-current))))))))
8967           ;; Update threads.
8968           (set-buffer (or buffer gnus-summary-buffer))
8969           (gnus-summary-update-article (cdr gnus-article-current))
8970           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8971               (gnus-summary-update-secondary-mark
8972                (cdr gnus-article-current))))
8973         ;; Prettify the article buffer again.
8974         (unless no-highlight
8975           (save-excursion
8976             (set-buffer gnus-article-buffer)
8977             ;;;!!! Fix this -- article should be rehighlighted.
8978             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8979             (set-buffer gnus-original-article-buffer)
8980             (gnus-request-article
8981              (cdr gnus-article-current)
8982              (car gnus-article-current) (current-buffer))))
8983         ;; Prettify the summary buffer line.
8984         (when (gnus-visual-p 'summary-highlight 'highlight)
8985           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8986
8987 (defun gnus-summary-edit-wash (key)
8988   "Perform editing command KEY in the article buffer."
8989   (interactive
8990    (list
8991     (progn
8992       (message "%s" (concat (this-command-keys) "- "))
8993       (read-char))))
8994   (message "")
8995   (gnus-summary-edit-article)
8996   (execute-kbd-macro (concat (this-command-keys) key))
8997   (gnus-article-edit-done))
8998
8999 ;;; Respooling
9000
9001 (defun gnus-summary-respool-query (&optional silent trace)
9002   "Query where the respool algorithm would put this article."
9003   (interactive)
9004   (let (gnus-mark-article-hook)
9005     (gnus-summary-select-article)
9006     (save-excursion
9007       (set-buffer gnus-original-article-buffer)
9008       (save-restriction
9009         (message-narrow-to-head)
9010         (let ((groups (nnmail-article-group 'identity trace)))
9011           (unless silent
9012             (if groups
9013                 (message "This message would go to %s"
9014                          (mapconcat 'car groups ", "))
9015               (message "This message would go to no groups"))
9016             groups))))))
9017
9018 (defun gnus-summary-respool-trace ()
9019   "Trace where the respool algorithm would put this article.
9020 Display a buffer showing all fancy splitting patterns which matched."
9021   (interactive)
9022   (gnus-summary-respool-query nil t))
9023
9024 ;; Summary marking commands.
9025
9026 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9027   "Mark articles which has the same subject as read, and then select the next.
9028 If UNMARK is positive, remove any kind of mark.
9029 If UNMARK is negative, tick articles."
9030   (interactive "P")
9031   (when unmark
9032     (setq unmark (prefix-numeric-value unmark)))
9033   (let ((count
9034          (gnus-summary-mark-same-subject
9035           (gnus-summary-article-subject) unmark)))
9036     ;; Select next unread article.  If auto-select-same mode, should
9037     ;; select the first unread article.
9038     (gnus-summary-next-article t (and gnus-auto-select-same
9039                                       (gnus-summary-article-subject)))
9040     (gnus-message 7 "%d article%s marked as %s"
9041                   count (if (= count 1) " is" "s are")
9042                   (if unmark "unread" "read"))))
9043
9044 (defun gnus-summary-kill-same-subject (&optional unmark)
9045   "Mark articles which has the same subject as read.
9046 If UNMARK is positive, remove any kind of mark.
9047 If UNMARK is negative, tick articles."
9048   (interactive "P")
9049   (when unmark
9050     (setq unmark (prefix-numeric-value unmark)))
9051   (let ((count
9052          (gnus-summary-mark-same-subject
9053           (gnus-summary-article-subject) unmark)))
9054     ;; If marked as read, go to next unread subject.
9055     (when (null unmark)
9056       ;; Go to next unread subject.
9057       (gnus-summary-next-subject 1 t))
9058     (gnus-message 7 "%d articles are marked as %s"
9059                   count (if unmark "unread" "read"))))
9060
9061 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9062   "Mark articles with same SUBJECT as read, and return marked number.
9063 If optional argument UNMARK is positive, remove any kinds of marks.
9064 If optional argument UNMARK is negative, mark articles as unread instead."
9065   (let ((count 1))
9066     (save-excursion
9067       (cond
9068        ((null unmark)                   ; Mark as read.
9069         (while (and
9070                 (progn
9071                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9072                   (gnus-summary-show-thread) t)
9073                 (gnus-summary-find-subject subject))
9074           (setq count (1+ count))))
9075        ((> unmark 0)                    ; Tick.
9076         (while (and
9077                 (progn
9078                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9079                   (gnus-summary-show-thread) t)
9080                 (gnus-summary-find-subject subject))
9081           (setq count (1+ count))))
9082        (t                               ; Mark as unread.
9083         (while (and
9084                 (progn
9085                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9086                   (gnus-summary-show-thread) t)
9087                 (gnus-summary-find-subject subject))
9088           (setq count (1+ count)))))
9089       (gnus-set-mode-line 'summary)
9090       ;; Return the number of marked articles.
9091       count)))
9092
9093 (defun gnus-summary-mark-as-processable (n &optional unmark)
9094   "Set the process mark on the next N articles.
9095 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9096 the process mark instead.  The difference between N and the actual
9097 number of articles marked is returned."
9098   (interactive "P")
9099   (if (and (null n) (gnus-region-active-p))
9100       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9101     (setq n (prefix-numeric-value n))
9102     (let ((backward (< n 0))
9103           (n (abs n)))
9104       (while (and
9105               (> n 0)
9106               (if unmark
9107                   (gnus-summary-remove-process-mark
9108                    (gnus-summary-article-number))
9109                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9110               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9111         (setq n (1- n)))
9112       (when (/= 0 n)
9113         (gnus-message 7 "No more articles"))
9114       (gnus-summary-recenter)
9115       (gnus-summary-position-point)
9116       n)))
9117
9118 (defun gnus-summary-unmark-as-processable (n)
9119   "Remove the process mark from the next N articles.
9120 If N is negative, unmark backward instead.  The difference between N and
9121 the actual number of articles unmarked is returned."
9122   (interactive "P")
9123   (gnus-summary-mark-as-processable n t))
9124
9125 (defun gnus-summary-unmark-all-processable ()
9126   "Remove the process mark from all articles."
9127   (interactive)
9128   (save-excursion
9129     (while gnus-newsgroup-processable
9130       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9131   (gnus-summary-position-point))
9132
9133 (defun gnus-summary-add-mark (article type)
9134   "Mark ARTICLE with a mark of TYPE."
9135   (let ((vtype (car (assq type gnus-article-mark-lists)))
9136         var)
9137     (if (not vtype)
9138         (error "No such mark type: %s" type)
9139       (setq var (intern (format "gnus-newsgroup-%s" type)))
9140       (set var (cons article (symbol-value var)))
9141       (if (memq type '(processable cached replied forwarded recent saved))
9142           (gnus-summary-update-secondary-mark article)
9143         ;;; !!! This is bogus.  We should find out what primary
9144         ;;; !!! mark we want to set.
9145         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9146
9147 (defun gnus-summary-mark-as-expirable (n)
9148   "Mark N articles forward as expirable.
9149 If N is negative, mark backward instead.  The difference between N and
9150 the actual number of articles marked is returned."
9151   (interactive "p")
9152   (gnus-summary-mark-forward n gnus-expirable-mark))
9153
9154 (defun gnus-summary-mark-article-as-replied (article)
9155   "Mark ARTICLE as replied to and update the summary line.
9156 ARTICLE can also be a list of articles."
9157   (interactive (list (gnus-summary-article-number)))
9158   (let ((articles (if (listp article) article (list article))))
9159     (dolist (article articles)
9160       (push article gnus-newsgroup-replied)
9161       (let ((buffer-read-only nil))
9162         (when (gnus-summary-goto-subject article nil t)
9163           (gnus-summary-update-secondary-mark article))))))
9164
9165 (defun gnus-summary-mark-article-as-forwarded (article)
9166   "Mark ARTICLE as forwarded and update the summary line.
9167 ARTICLE can also be a list of articles."
9168   (let ((articles (if (listp article) article (list article))))
9169     (dolist (article articles)
9170       (push article gnus-newsgroup-forwarded)
9171       (let ((buffer-read-only nil))
9172         (when (gnus-summary-goto-subject article nil t)
9173           (gnus-summary-update-secondary-mark article))))))
9174
9175 (defun gnus-summary-set-bookmark (article)
9176   "Set a bookmark in current article."
9177   (interactive (list (gnus-summary-article-number)))
9178   (when (or (not (get-buffer gnus-article-buffer))
9179             (not gnus-current-article)
9180             (not gnus-article-current)
9181             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9182     (error "No current article selected"))
9183   ;; Remove old bookmark, if one exists.
9184   (let ((old (assq article gnus-newsgroup-bookmarks)))
9185     (when old
9186       (setq gnus-newsgroup-bookmarks
9187             (delq old gnus-newsgroup-bookmarks))))
9188   ;; Set the new bookmark, which is on the form
9189   ;; (article-number . line-number-in-body).
9190   (push
9191    (cons article
9192          (save-excursion
9193            (set-buffer gnus-article-buffer)
9194            (count-lines
9195             (min (point)
9196                  (save-excursion
9197                    (goto-char (point-min))
9198                    (search-forward "\n\n" nil t)
9199                    (point)))
9200             (point))))
9201    gnus-newsgroup-bookmarks)
9202   (gnus-message 6 "A bookmark has been added to the current article."))
9203
9204 (defun gnus-summary-remove-bookmark (article)
9205   "Remove the bookmark from the current article."
9206   (interactive (list (gnus-summary-article-number)))
9207   ;; Remove old bookmark, if one exists.
9208   (let ((old (assq article gnus-newsgroup-bookmarks)))
9209     (if old
9210         (progn
9211           (setq gnus-newsgroup-bookmarks
9212                 (delq old gnus-newsgroup-bookmarks))
9213           (gnus-message 6 "Removed bookmark."))
9214       (gnus-message 6 "No bookmark in current article."))))
9215
9216 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9217 (defun gnus-summary-mark-as-dormant (n)
9218   "Mark N articles forward as dormant.
9219 If N is negative, mark backward instead.  The difference between N and
9220 the actual number of articles marked is returned."
9221   (interactive "p")
9222   (gnus-summary-mark-forward n gnus-dormant-mark))
9223
9224 (defun gnus-summary-set-process-mark (article)
9225   "Set the process mark on ARTICLE and update the summary line."
9226   (setq gnus-newsgroup-processable
9227         (cons article
9228               (delq article gnus-newsgroup-processable)))
9229   (when (gnus-summary-goto-subject article)
9230     (gnus-summary-show-thread)
9231     (gnus-summary-goto-subject article)
9232     (gnus-summary-update-secondary-mark article)))
9233
9234 (defun gnus-summary-remove-process-mark (article)
9235   "Remove the process mark from ARTICLE and update the summary line."
9236   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9237   (when (gnus-summary-goto-subject article)
9238     (gnus-summary-show-thread)
9239     (gnus-summary-goto-subject article)
9240     (gnus-summary-update-secondary-mark article)))
9241
9242 (defun gnus-summary-set-saved-mark (article)
9243   "Set the process mark on ARTICLE and update the summary line."
9244   (push article gnus-newsgroup-saved)
9245   (when (gnus-summary-goto-subject article)
9246     (gnus-summary-update-secondary-mark article)))
9247
9248 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9249   "Mark N articles as read forwards.
9250 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9251 The difference between N and the actual number of articles marked is
9252 returned.
9253 Iff NO-EXPIRE, auto-expiry will be inhibited."
9254   (interactive "p")
9255   (gnus-summary-show-thread)
9256   (let ((backward (< n 0))
9257         (gnus-summary-goto-unread
9258          (and gnus-summary-goto-unread
9259               (not (eq gnus-summary-goto-unread 'never))
9260               (not (memq mark (list gnus-unread-mark
9261                                     gnus-ticked-mark gnus-dormant-mark)))))
9262         (n (abs n))
9263         (mark (or mark gnus-del-mark)))
9264     (while (and (> n 0)
9265                 (gnus-summary-mark-article nil mark no-expire)
9266                 (zerop (gnus-summary-next-subject
9267                         (if backward -1 1)
9268                         (and gnus-summary-goto-unread
9269                              (not (eq gnus-summary-goto-unread 'never)))
9270                         t)))
9271       (setq n (1- n)))
9272     (when (/= 0 n)
9273       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9274     (gnus-summary-recenter)
9275     (gnus-summary-position-point)
9276     (gnus-set-mode-line 'summary)
9277     n))
9278
9279 (defun gnus-summary-mark-article-as-read (mark)
9280   "Mark the current article quickly as read with MARK."
9281   (let ((article (gnus-summary-article-number)))
9282     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9283     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9284     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9285     (push (cons article mark) gnus-newsgroup-reads)
9286     ;; Possibly remove from cache, if that is used.
9287     (when gnus-use-cache
9288       (gnus-cache-enter-remove-article article))
9289     ;; Allow the backend to change the mark.
9290     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9291     ;; Check for auto-expiry.
9292     (when (and gnus-newsgroup-auto-expire
9293                (memq mark gnus-auto-expirable-marks))
9294       (setq mark gnus-expirable-mark)
9295       ;; Let the backend know about the mark change.
9296       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9297       (push article gnus-newsgroup-expirable))
9298     ;; Set the mark in the buffer.
9299     (gnus-summary-update-mark mark 'unread)
9300     t))
9301
9302 (defun gnus-summary-mark-article-as-unread (mark)
9303   "Mark the current article quickly as unread with MARK."
9304   (let* ((article (gnus-summary-article-number))
9305          (old-mark (gnus-summary-article-mark article)))
9306     ;; Allow the backend to change the mark.
9307     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9308     (if (eq mark old-mark)
9309         t
9310       (if (<= article 0)
9311           (progn
9312             (gnus-error 1 "Can't mark negative article numbers")
9313             nil)
9314         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9315         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9316         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9317         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9318         (cond ((= mark gnus-ticked-mark)
9319                (setq gnus-newsgroup-marked
9320                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9321                                               article)))
9322               ((= mark gnus-dormant-mark)
9323                (setq gnus-newsgroup-dormant
9324                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9325                                               article)))
9326               (t
9327                (setq gnus-newsgroup-unreads
9328                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9329                                               article))))
9330         (gnus-pull article gnus-newsgroup-reads)
9331
9332         ;; See whether the article is to be put in the cache.
9333         (and gnus-use-cache
9334              (vectorp (gnus-summary-article-header article))
9335              (save-excursion
9336                (gnus-cache-possibly-enter-article
9337                 gnus-newsgroup-name article
9338                 (gnus-summary-article-header article)
9339                 (= mark gnus-ticked-mark)
9340                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9341
9342         ;; Fix the mark.
9343         (gnus-summary-update-mark mark 'unread)
9344         t))))
9345
9346 (defun gnus-summary-mark-article (&optional article mark no-expire)
9347   "Mark ARTICLE with MARK.  MARK can be any character.
9348 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9349 `??' (dormant) and `?E' (expirable).
9350 If MARK is nil, then the default character `?r' is used.
9351 If ARTICLE is nil, then the article on the current line will be
9352 marked.
9353 Iff NO-EXPIRE, auto-expiry will be inhibited."
9354   ;; The mark might be a string.
9355   (when (stringp mark)
9356     (setq mark (aref mark 0)))
9357   ;; If no mark is given, then we check auto-expiring.
9358   (when (null mark)
9359     (setq mark gnus-del-mark))
9360   (when (and (not no-expire)
9361              gnus-newsgroup-auto-expire
9362              (memq mark gnus-auto-expirable-marks))
9363     (setq mark gnus-expirable-mark))
9364   (let ((article (or article (gnus-summary-article-number)))
9365         (old-mark (gnus-summary-article-mark article)))
9366     ;; Allow the backend to change the mark.
9367     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9368     (if (eq mark old-mark)
9369         t
9370       (unless article
9371         (error "No article on current line"))
9372       (if (not (if (or (= mark gnus-unread-mark)
9373                        (= mark gnus-ticked-mark)
9374                        (= mark gnus-dormant-mark))
9375                    (gnus-mark-article-as-unread article mark)
9376                  (gnus-mark-article-as-read article mark)))
9377           t
9378         ;; See whether the article is to be put in the cache.
9379         (and gnus-use-cache
9380              (not (= mark gnus-canceled-mark))
9381              (vectorp (gnus-summary-article-header article))
9382              (save-excursion
9383                (gnus-cache-possibly-enter-article
9384                 gnus-newsgroup-name article
9385                 (gnus-summary-article-header article)
9386                 (= mark gnus-ticked-mark)
9387                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9388
9389         (when (gnus-summary-goto-subject article nil t)
9390           (let ((buffer-read-only nil))
9391             (gnus-summary-show-thread)
9392             ;; Fix the mark.
9393             (gnus-summary-update-mark mark 'unread)
9394             t))))))
9395
9396 (defun gnus-summary-update-secondary-mark (article)
9397   "Update the secondary (read, process, cache) mark."
9398   (gnus-summary-update-mark
9399    (cond ((memq article gnus-newsgroup-processable)
9400           gnus-process-mark)
9401          ((memq article gnus-newsgroup-cached)
9402           gnus-cached-mark)
9403          ((memq article gnus-newsgroup-replied)
9404           gnus-replied-mark)
9405          ((memq article gnus-newsgroup-forwarded)
9406           gnus-forwarded-mark)
9407          ((memq article gnus-newsgroup-saved)
9408           gnus-saved-mark)
9409          ((memq article gnus-newsgroup-recent)
9410           gnus-recent-mark)
9411          ((memq article gnus-newsgroup-unseen)
9412           gnus-unseen-mark)
9413          (t gnus-no-mark))
9414    'replied)
9415   (when (gnus-visual-p 'summary-highlight 'highlight)
9416     (gnus-run-hooks 'gnus-summary-update-hook))
9417   t)
9418
9419 (defun gnus-summary-update-mark (mark type)
9420   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9421         (buffer-read-only nil))
9422     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9423     (when forward
9424       (when (looking-at "\r")
9425         (incf forward))
9426       (when (<= (+ forward (point)) (point-max))
9427         ;; Go to the right position on the line.
9428         (goto-char (+ forward (point)))
9429         ;; Replace the old mark with the new mark.
9430         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9431         ;; Optionally update the marks by some user rule.
9432         (when (eq type 'unread)
9433           (gnus-data-set-mark
9434            (gnus-data-find (gnus-summary-article-number)) mark)
9435           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9436
9437 (defun gnus-mark-article-as-read (article &optional mark)
9438   "Enter ARTICLE in the pertinent lists and remove it from others."
9439   ;; Make the article expirable.
9440   (let ((mark (or mark gnus-del-mark)))
9441     (setq gnus-newsgroup-expirable
9442           (if (= mark gnus-expirable-mark)
9443               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9444             (delq article gnus-newsgroup-expirable)))
9445     ;; Remove from unread and marked lists.
9446     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9447     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9448     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9449     (push (cons article mark) gnus-newsgroup-reads)
9450     ;; Possibly remove from cache, if that is used.
9451     (when gnus-use-cache
9452       (gnus-cache-enter-remove-article article))
9453     t))
9454
9455 (defun gnus-mark-article-as-unread (article &optional mark)
9456   "Enter ARTICLE in the pertinent lists and remove it from others."
9457   (let ((mark (or mark gnus-ticked-mark)))
9458     (if (<= article 0)
9459         (progn
9460           (gnus-error 1 "Can't mark negative article numbers")
9461           nil)
9462       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9463             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9464             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9465             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9466
9467       ;; Unsuppress duplicates?
9468       (when gnus-suppress-duplicates
9469         (gnus-dup-unsuppress-article article))
9470
9471       (cond ((= mark gnus-ticked-mark)
9472              (setq gnus-newsgroup-marked
9473                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9474             ((= mark gnus-dormant-mark)
9475              (setq gnus-newsgroup-dormant
9476                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9477             (t
9478              (setq gnus-newsgroup-unreads
9479                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9480       (gnus-pull article gnus-newsgroup-reads)
9481       t)))
9482
9483 (defalias 'gnus-summary-mark-as-unread-forward
9484   'gnus-summary-tick-article-forward)
9485 (make-obsolete 'gnus-summary-mark-as-unread-forward
9486                'gnus-summary-tick-article-forward)
9487 (defun gnus-summary-tick-article-forward (n)
9488   "Tick N articles forwards.
9489 If N is negative, tick backwards instead.
9490 The difference between N and the number of articles ticked is returned."
9491   (interactive "p")
9492   (gnus-summary-mark-forward n gnus-ticked-mark))
9493
9494 (defalias 'gnus-summary-mark-as-unread-backward
9495   'gnus-summary-tick-article-backward)
9496 (make-obsolete 'gnus-summary-mark-as-unread-backward
9497                'gnus-summary-tick-article-backward)
9498 (defun gnus-summary-tick-article-backward (n)
9499   "Tick N articles backwards.
9500 The difference between N and the number of articles ticked is returned."
9501   (interactive "p")
9502   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9503
9504 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9505 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9506 (defun gnus-summary-tick-article (&optional article clear-mark)
9507   "Mark current article as unread.
9508 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9509 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9510   (interactive)
9511   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9512                                        gnus-ticked-mark)))
9513
9514 (defun gnus-summary-mark-as-read-forward (n)
9515   "Mark N articles as read forwards.
9516 If N is negative, mark backwards instead.
9517 The difference between N and the actual number of articles marked is
9518 returned."
9519   (interactive "p")
9520   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9521
9522 (defun gnus-summary-mark-as-read-backward (n)
9523   "Mark the N articles as read backwards.
9524 The difference between N and the actual number of articles marked is
9525 returned."
9526   (interactive "p")
9527   (gnus-summary-mark-forward
9528    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9529
9530 (defun gnus-summary-mark-as-read (&optional article mark)
9531   "Mark current article as read.
9532 ARTICLE specifies the article to be marked as read.
9533 MARK specifies a string to be inserted at the beginning of the line."
9534   (gnus-summary-mark-article article mark))
9535
9536 (defun gnus-summary-clear-mark-forward (n)
9537   "Clear marks from N articles forward.
9538 If N is negative, clear backward instead.
9539 The difference between N and the number of marks cleared is returned."
9540   (interactive "p")
9541   (gnus-summary-mark-forward n gnus-unread-mark))
9542
9543 (defun gnus-summary-clear-mark-backward (n)
9544   "Clear marks from N articles backward.
9545 The difference between N and the number of marks cleared is returned."
9546   (interactive "p")
9547   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9548
9549 (defun gnus-summary-mark-unread-as-read ()
9550   "Intended to be used by `gnus-summary-mark-article-hook'."
9551   (when (memq gnus-current-article gnus-newsgroup-unreads)
9552     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9553
9554 (defun gnus-summary-mark-read-and-unread-as-read ()
9555   "Intended to be used by `gnus-summary-mark-article-hook'."
9556   (let ((mark (gnus-summary-article-mark)))
9557     (when (or (gnus-unread-mark-p mark)
9558               (gnus-read-mark-p mark))
9559       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9560
9561 (defun gnus-summary-mark-unread-as-ticked ()
9562   "Intended to be used by `gnus-summary-mark-article-hook'."
9563   (when (memq gnus-current-article gnus-newsgroup-unreads)
9564     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9565
9566 (defun gnus-summary-mark-region-as-read (point mark all)
9567   "Mark all unread articles between point and mark as read.
9568 If given a prefix, mark all articles between point and mark as read,
9569 even ticked and dormant ones."
9570   (interactive "r\nP")
9571   (save-excursion
9572     (let (article)
9573       (goto-char point)
9574       (beginning-of-line)
9575       (while (and
9576               (< (point) mark)
9577               (progn
9578                 (when (or all
9579                           (memq (setq article (gnus-summary-article-number))
9580                                 gnus-newsgroup-unreads))
9581                   (gnus-summary-mark-article article gnus-del-mark))
9582                 t)
9583               (gnus-summary-find-next))))))
9584
9585 (defun gnus-summary-mark-below (score mark)
9586   "Mark articles with score less than SCORE with MARK."
9587   (interactive "P\ncMark: ")
9588   (setq score (if score
9589                   (prefix-numeric-value score)
9590                 (or gnus-summary-default-score 0)))
9591   (save-excursion
9592     (set-buffer gnus-summary-buffer)
9593     (goto-char (point-min))
9594     (while
9595         (progn
9596           (and (< (gnus-summary-article-score) score)
9597                (gnus-summary-mark-article nil mark))
9598           (gnus-summary-find-next)))))
9599
9600 (defun gnus-summary-kill-below (&optional score)
9601   "Mark articles with score below SCORE as read."
9602   (interactive "P")
9603   (gnus-summary-mark-below score gnus-killed-mark))
9604
9605 (defun gnus-summary-clear-above (&optional score)
9606   "Clear all marks from articles with score above SCORE."
9607   (interactive "P")
9608   (gnus-summary-mark-above score gnus-unread-mark))
9609
9610 (defun gnus-summary-tick-above (&optional score)
9611   "Tick all articles with score above SCORE."
9612   (interactive "P")
9613   (gnus-summary-mark-above score gnus-ticked-mark))
9614
9615 (defun gnus-summary-mark-above (score mark)
9616   "Mark articles with score over SCORE with MARK."
9617   (interactive "P\ncMark: ")
9618   (setq score (if score
9619                   (prefix-numeric-value score)
9620                 (or gnus-summary-default-score 0)))
9621   (save-excursion
9622     (set-buffer gnus-summary-buffer)
9623     (goto-char (point-min))
9624     (while (and (progn
9625                   (when (> (gnus-summary-article-score) score)
9626                     (gnus-summary-mark-article nil mark))
9627                   t)
9628                 (gnus-summary-find-next)))))
9629
9630 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9631 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9632 (defun gnus-summary-limit-include-expunged (&optional no-error)
9633   "Display all the hidden articles that were expunged for low scores."
9634   (interactive)
9635   (let ((buffer-read-only nil))
9636     (let ((scored gnus-newsgroup-scored)
9637           headers h)
9638       (while scored
9639         (unless (gnus-summary-article-header (caar scored))
9640           (and (setq h (gnus-number-to-header (caar scored)))
9641                (< (cdar scored) gnus-summary-expunge-below)
9642                (push h headers)))
9643         (setq scored (cdr scored)))
9644       (if (not headers)
9645           (when (not no-error)
9646             (error "No expunged articles hidden"))
9647         (goto-char (point-min))
9648         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9649         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9650         (mapcar (lambda (x) (push (mail-header-number x)
9651                                   gnus-newsgroup-limit))
9652                 headers)
9653         (gnus-summary-prepare-unthreaded (nreverse headers))
9654         (goto-char (point-min))
9655         (gnus-summary-position-point)
9656         t))))
9657
9658 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9659   "Mark all unread articles in this newsgroup as read.
9660 If prefix argument ALL is non-nil, ticked and dormant articles will
9661 also be marked as read.
9662 If QUIETLY is non-nil, no questions will be asked.
9663 If TO-HERE is non-nil, it should be a point in the buffer.  All
9664 articles before (after, if REVERSE is set) this point will be marked as read.
9665 Note that this function will only catch up the unread article
9666 in the current summary buffer limitation.
9667 The number of articles marked as read is returned."
9668   (interactive "P")
9669   (prog1
9670       (save-excursion
9671         (when (or quietly
9672                   (not gnus-interactive-catchup) ;Without confirmation?
9673                   gnus-expert-user
9674                   (gnus-y-or-n-p
9675                    (if all
9676                        "Mark absolutely all articles as read? "
9677                      "Mark all unread articles as read? ")))
9678           (if (and not-mark
9679                    (not gnus-newsgroup-adaptive)
9680                    (not gnus-newsgroup-auto-expire)
9681                    (not gnus-suppress-duplicates)
9682                    (or (not gnus-use-cache)
9683                        (eq gnus-use-cache 'passive)))
9684               (progn
9685                 (when all
9686                   (setq gnus-newsgroup-marked nil
9687                         gnus-newsgroup-dormant nil))
9688                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9689             ;; We actually mark all articles as canceled, which we
9690             ;; have to do when using auto-expiry or adaptive scoring.
9691             (gnus-summary-show-all-threads)
9692             (if (and to-here reverse)
9693                 (progn
9694                   (goto-char to-here)
9695                   (while (and
9696                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9697                           (gnus-summary-find-next (not all) nil nil t))))
9698               (when (gnus-summary-first-subject (not all) t)
9699                 (while (and
9700                         (if to-here (< (point) to-here) t)
9701                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9702                         (gnus-summary-find-next (not all) nil nil t)))))
9703             (gnus-set-mode-line 'summary))
9704           t))
9705     (gnus-summary-position-point)))
9706
9707 (defun gnus-summary-catchup-to-here (&optional all)
9708   "Mark all unticked articles before the current one as read.
9709 If ALL is non-nil, also mark ticked and dormant articles as read."
9710   (interactive "P")
9711   (save-excursion
9712     (gnus-save-hidden-threads
9713       (let ((beg (point)))
9714         ;; We check that there are unread articles.
9715         (when (or all (gnus-summary-find-prev))
9716           (gnus-summary-catchup all t beg)))))
9717   (gnus-summary-position-point))
9718
9719 (defun gnus-summary-catchup-from-here (&optional all)
9720   "Mark all unticked articles after the current one as read.
9721 If ALL is non-nil, also mark ticked and dormant articles as read."
9722   (interactive "P")
9723   (save-excursion
9724     (gnus-save-hidden-threads
9725       (let ((beg (point)))
9726         ;; We check that there are unread articles.
9727         (when (or all (gnus-summary-find-next))
9728           (gnus-summary-catchup all t beg nil t)))))
9729   (gnus-summary-position-point))
9730
9731 (defun gnus-summary-catchup-all (&optional quietly)
9732   "Mark all articles in this newsgroup as read."
9733   (interactive "P")
9734   (gnus-summary-catchup t quietly))
9735
9736 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9737   "Mark all unread articles in this group as read, then exit.
9738 If prefix argument ALL is non-nil, all articles are marked as read.
9739 If QUIETLY is non-nil, no questions will be asked."
9740   (interactive "P")
9741   (when (gnus-summary-catchup all quietly nil 'fast)
9742     ;; Select next newsgroup or exit.
9743     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9744              (eq gnus-auto-select-next 'quietly))
9745         (gnus-summary-next-group nil)
9746       (gnus-summary-exit))))
9747
9748 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9749   "Mark all articles in this newsgroup as read, and then exit."
9750   (interactive "P")
9751   (gnus-summary-catchup-and-exit t quietly))
9752
9753 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9754   "Mark all articles in this group as read and select the next group.
9755 If given a prefix, mark all articles, unread as well as ticked, as
9756 read."
9757   (interactive "P")
9758   (save-excursion
9759     (gnus-summary-catchup all))
9760   (gnus-summary-next-group))
9761
9762 ;;;
9763 ;;; with article
9764 ;;;
9765
9766 (defmacro gnus-with-article (article &rest forms)
9767   "Select ARTICLE and perform FORMS in the original article buffer.
9768 Then replace the article with the result."
9769   `(progn
9770      ;; We don't want the article to be marked as read.
9771      (let (gnus-mark-article-hook)
9772        (gnus-summary-select-article t t nil ,article))
9773      (set-buffer gnus-original-article-buffer)
9774      ,@forms
9775      (if (not (gnus-check-backend-function
9776                'request-replace-article (car gnus-article-current)))
9777          (gnus-message 5 "Read-only group; not replacing")
9778        (unless (gnus-request-replace-article
9779                 ,article (car gnus-article-current)
9780                 (current-buffer) t)
9781          (error "Couldn't replace article")))
9782      ;; The cache and backlog have to be flushed somewhat.
9783      (when gnus-keep-backlog
9784        (gnus-backlog-remove-article
9785         (car gnus-article-current) (cdr gnus-article-current)))
9786      (when gnus-use-cache
9787        (gnus-cache-update-article
9788         (car gnus-article-current) (cdr gnus-article-current)))))
9789
9790 (put 'gnus-with-article 'lisp-indent-function 1)
9791 (put 'gnus-with-article 'edebug-form-spec '(form body))
9792
9793 ;; Thread-based commands.
9794
9795 (defun gnus-summary-articles-in-thread (&optional article)
9796   "Return a list of all articles in the current thread.
9797 If ARTICLE is non-nil, return all articles in the thread that starts
9798 with that article."
9799   (let* ((article (or article (gnus-summary-article-number)))
9800          (data (gnus-data-find-list article))
9801          (top-level (gnus-data-level (car data)))
9802          (top-subject
9803           (cond ((null gnus-thread-operation-ignore-subject)
9804                  (gnus-simplify-subject-re
9805                   (mail-header-subject (gnus-data-header (car data)))))
9806                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9807                  (gnus-simplify-subject-fuzzy
9808                   (mail-header-subject (gnus-data-header (car data)))))
9809                 (t nil)))
9810          (end-point (save-excursion
9811                       (if (gnus-summary-go-to-next-thread)
9812                           (point) (point-max))))
9813          articles)
9814     (while (and data
9815                 (< (gnus-data-pos (car data)) end-point))
9816       (when (or (not top-subject)
9817                 (string= top-subject
9818                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9819                              (gnus-simplify-subject-fuzzy
9820                               (mail-header-subject
9821                                (gnus-data-header (car data))))
9822                            (gnus-simplify-subject-re
9823                             (mail-header-subject
9824                              (gnus-data-header (car data)))))))
9825         (push (gnus-data-number (car data)) articles))
9826       (unless (and (setq data (cdr data))
9827                    (> (gnus-data-level (car data)) top-level))
9828         (setq data nil)))
9829     ;; Return the list of articles.
9830     (nreverse articles)))
9831
9832 (defun gnus-summary-rethread-current ()
9833   "Rethread the thread the current article is part of."
9834   (interactive)
9835   (let* ((gnus-show-threads t)
9836          (article (gnus-summary-article-number))
9837          (id (mail-header-id (gnus-summary-article-header)))
9838          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9839     (unless id
9840       (error "No article on the current line"))
9841     (gnus-rebuild-thread id)
9842     (gnus-summary-goto-subject article)))
9843
9844 (defun gnus-summary-reparent-thread ()
9845   "Make the current article child of the marked (or previous) article.
9846
9847 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9848 is non-nil or the Subject: of both articles are the same."
9849   (interactive)
9850   (unless (not (gnus-group-read-only-p))
9851     (error "The current newsgroup does not support article editing"))
9852   (unless (<= (length gnus-newsgroup-processable) 1)
9853     (error "No more than one article may be marked"))
9854   (save-window-excursion
9855     (let ((gnus-article-buffer " *reparent*")
9856           (current-article (gnus-summary-article-number))
9857           ;; First grab the marked article, otherwise one line up.
9858           (parent-article (if (not (null gnus-newsgroup-processable))
9859                               (car gnus-newsgroup-processable)
9860                             (save-excursion
9861                               (if (eq (forward-line -1) 0)
9862                                   (gnus-summary-article-number)
9863                                 (error "Beginning of summary buffer"))))))
9864       (unless (not (eq current-article parent-article))
9865         (error "An article may not be self-referential"))
9866       (let ((message-id (mail-header-id
9867                          (gnus-summary-article-header parent-article))))
9868         (unless (and message-id (not (equal message-id "")))
9869           (error "No message-id in desired parent"))
9870         (gnus-with-article current-article
9871           (save-restriction
9872             (goto-char (point-min))
9873             (message-narrow-to-head)
9874             (if (re-search-forward "^References: " nil t)
9875                 (progn
9876                   (re-search-forward "^[^ \t]" nil t)
9877                   (forward-line -1)
9878                   (end-of-line)
9879                   (insert " " message-id))
9880               (insert "References: " message-id "\n"))))
9881         (set-buffer gnus-summary-buffer)
9882         (gnus-summary-unmark-all-processable)
9883         (gnus-summary-update-article current-article)
9884         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9885             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9886         (gnus-summary-rethread-current)
9887         (gnus-message 3 "Article %d is now the child of article %d"
9888                       current-article parent-article)))))
9889
9890 (defun gnus-summary-toggle-threads (&optional arg)
9891   "Toggle showing conversation threads.
9892 If ARG is positive number, turn showing conversation threads on."
9893   (interactive "P")
9894   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9895     (setq gnus-show-threads
9896           (if (null arg) (not gnus-show-threads)
9897             (> (prefix-numeric-value arg) 0)))
9898     (gnus-summary-prepare)
9899     (gnus-summary-goto-subject current)
9900     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9901     (gnus-summary-position-point)))
9902
9903 (defun gnus-summary-show-all-threads ()
9904   "Show all threads."
9905   (interactive)
9906   (save-excursion
9907     (let ((buffer-read-only nil))
9908       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9909   (gnus-summary-position-point))
9910
9911 (defun gnus-summary-show-thread ()
9912   "Show thread subtrees.
9913 Returns nil if no thread was there to be shown."
9914   (interactive)
9915   (let ((buffer-read-only nil)
9916         (orig (point))
9917         ;; first goto end then to beg, to have point at beg after let
9918         (end (progn (end-of-line) (point)))
9919         (beg (progn (beginning-of-line) (point))))
9920     (prog1
9921         ;; Any hidden lines here?
9922         (search-forward "\r" end t)
9923       (subst-char-in-region beg end ?\^M ?\n t)
9924       (goto-char orig)
9925       (gnus-summary-position-point))))
9926
9927 (defun gnus-summary-maybe-hide-threads ()
9928   "If requested, hide the threads that should be hidden."
9929   (when (and gnus-show-threads
9930              gnus-thread-hide-subtree)
9931     (gnus-summary-hide-all-threads
9932      (if (or (consp gnus-thread-hide-subtree)
9933              (gnus-functionp gnus-thread-hide-subtree))
9934          (gnus-make-predicate gnus-thread-hide-subtree)
9935        nil))))
9936
9937 ;;; Hiding predicates.
9938
9939 (defun gnus-article-unread-p (header)
9940   (memq (mail-header-number header) gnus-newsgroup-unreads))
9941
9942 (defun gnus-article-unseen-p (header)
9943   (memq (mail-header-number header) gnus-newsgroup-unseen))
9944
9945 (defun gnus-map-articles (predicate articles)
9946   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9947   (apply 'gnus-or (mapcar predicate
9948                           (mapcar 'gnus-summary-article-header articles))))
9949
9950 (defun gnus-summary-hide-all-threads (&optional predicate)
9951   "Hide all thread subtrees.
9952 If PREDICATE is supplied, threads that satisfy this predicate
9953 will not be hidden."
9954   (interactive)
9955   (save-excursion
9956     (goto-char (point-min))
9957     (let ((end nil))
9958       (while (not end)
9959         (when (or (not predicate)
9960                   (gnus-map-articles
9961                    predicate (gnus-summary-article-children)))
9962             (gnus-summary-hide-thread))
9963         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9964   (gnus-summary-position-point))
9965
9966 (defun gnus-summary-hide-thread ()
9967   "Hide thread subtrees.
9968 If PREDICATE is supplied, threads that satisfy this predicate
9969 will not be hidden.
9970 Returns nil if no threads were there to be hidden."
9971   (interactive)
9972   (let ((buffer-read-only nil)
9973         (start (point))
9974         (article (gnus-summary-article-number)))
9975     (goto-char start)
9976     ;; Go forward until either the buffer ends or the subthread
9977     ;; ends.
9978     (when (and (not (eobp))
9979                (or (zerop (gnus-summary-next-thread 1 t))
9980                    (goto-char (point-max))))
9981       (prog1
9982           (if (and (> (point) start)
9983                    (search-backward "\n" start t))
9984               (progn
9985                 (subst-char-in-region start (point) ?\n ?\^M)
9986                 (gnus-summary-goto-subject article))
9987             (goto-char start)
9988             nil)))))
9989
9990 (defun gnus-summary-go-to-next-thread (&optional previous)
9991   "Go to the same level (or less) next thread.
9992 If PREVIOUS is non-nil, go to previous thread instead.
9993 Return the article number moved to, or nil if moving was impossible."
9994   (let ((level (gnus-summary-thread-level))
9995         (way (if previous -1 1))
9996         (beg (point)))
9997     (forward-line way)
9998     (while (and (not (eobp))
9999                 (< level (gnus-summary-thread-level)))
10000       (forward-line way))
10001     (if (eobp)
10002         (progn
10003           (goto-char beg)
10004           nil)
10005       (setq beg (point))
10006       (prog1
10007           (gnus-summary-article-number)
10008         (goto-char beg)))))
10009
10010 (defun gnus-summary-next-thread (n &optional silent)
10011   "Go to the same level next N'th thread.
10012 If N is negative, search backward instead.
10013 Returns the difference between N and the number of skips actually
10014 done.
10015
10016 If SILENT, don't output messages."
10017   (interactive "p")
10018   (let ((backward (< n 0))
10019         (n (abs n)))
10020     (while (and (> n 0)
10021                 (gnus-summary-go-to-next-thread backward))
10022       (decf n))
10023     (unless silent
10024       (gnus-summary-position-point))
10025     (when (and (not silent) (/= 0 n))
10026       (gnus-message 7 "No more threads"))
10027     n))
10028
10029 (defun gnus-summary-prev-thread (n)
10030   "Go to the same level previous N'th thread.
10031 Returns the difference between N and the number of skips actually
10032 done."
10033   (interactive "p")
10034   (gnus-summary-next-thread (- n)))
10035
10036 (defun gnus-summary-go-down-thread ()
10037   "Go down one level in the current thread."
10038   (let ((children (gnus-summary-article-children)))
10039     (when children
10040       (gnus-summary-goto-subject (car children)))))
10041
10042 (defun gnus-summary-go-up-thread ()
10043   "Go up one level in the current thread."
10044   (let ((parent (gnus-summary-article-parent)))
10045     (when parent
10046       (gnus-summary-goto-subject parent))))
10047
10048 (defun gnus-summary-down-thread (n)
10049   "Go down thread N steps.
10050 If N is negative, go up instead.
10051 Returns the difference between N and how many steps down that were
10052 taken."
10053   (interactive "p")
10054   (let ((up (< n 0))
10055         (n (abs n)))
10056     (while (and (> n 0)
10057                 (if up (gnus-summary-go-up-thread)
10058                   (gnus-summary-go-down-thread)))
10059       (setq n (1- n)))
10060     (gnus-summary-position-point)
10061     (when (/= 0 n)
10062       (gnus-message 7 "Can't go further"))
10063     n))
10064
10065 (defun gnus-summary-up-thread (n)
10066   "Go up thread N steps.
10067 If N is negative, go down instead.
10068 Returns the difference between N and how many steps down that were
10069 taken."
10070   (interactive "p")
10071   (gnus-summary-down-thread (- n)))
10072
10073 (defun gnus-summary-top-thread ()
10074   "Go to the top of the thread."
10075   (interactive)
10076   (while (gnus-summary-go-up-thread))
10077   (gnus-summary-article-number))
10078
10079 (defun gnus-summary-kill-thread (&optional unmark)
10080   "Mark articles under current thread as read.
10081 If the prefix argument is positive, remove any kinds of marks.
10082 If the prefix argument is negative, tick articles instead."
10083   (interactive "P")
10084   (when unmark
10085     (setq unmark (prefix-numeric-value unmark)))
10086   (let ((articles (gnus-summary-articles-in-thread)))
10087     (save-excursion
10088       ;; Expand the thread.
10089       (gnus-summary-show-thread)
10090       ;; Mark all the articles.
10091       (while articles
10092         (gnus-summary-goto-subject (car articles))
10093         (cond ((null unmark)
10094                (gnus-summary-mark-article-as-read gnus-killed-mark))
10095               ((> unmark 0)
10096                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10097               (t
10098                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10099         (setq articles (cdr articles))))
10100     ;; Hide killed subtrees.
10101     (and (null unmark)
10102          gnus-thread-hide-killed
10103          (gnus-summary-hide-thread))
10104     ;; If marked as read, go to next unread subject.
10105     (when (null unmark)
10106       ;; Go to next unread subject.
10107       (gnus-summary-next-subject 1 t)))
10108   (gnus-set-mode-line 'summary))
10109
10110 ;; Summary sorting commands
10111
10112 (defun gnus-summary-sort-by-number (&optional reverse)
10113   "Sort the summary buffer by article number.
10114 Argument REVERSE means reverse order."
10115   (interactive "P")
10116   (gnus-summary-sort 'number reverse))
10117
10118 (defun gnus-summary-sort-by-author (&optional reverse)
10119   "Sort the summary buffer by author name alphabetically.
10120 If `case-fold-search' is non-nil, case of letters is ignored.
10121 Argument REVERSE means reverse order."
10122   (interactive "P")
10123   (gnus-summary-sort 'author reverse))
10124
10125 (defun gnus-summary-sort-by-subject (&optional reverse)
10126   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10127 If `case-fold-search' is non-nil, case of letters is ignored.
10128 Argument REVERSE means reverse order."
10129   (interactive "P")
10130   (gnus-summary-sort 'subject reverse))
10131
10132 (defun gnus-summary-sort-by-date (&optional reverse)
10133   "Sort the summary buffer by date.
10134 Argument REVERSE means reverse order."
10135   (interactive "P")
10136   (gnus-summary-sort 'date reverse))
10137
10138 (defun gnus-summary-sort-by-score (&optional reverse)
10139   "Sort the summary buffer by score.
10140 Argument REVERSE means reverse order."
10141   (interactive "P")
10142   (gnus-summary-sort 'score reverse))
10143
10144 (defun gnus-summary-sort-by-lines (&optional reverse)
10145   "Sort the summary buffer by the number of lines.
10146 Argument REVERSE means reverse order."
10147   (interactive "P")
10148   (gnus-summary-sort 'lines reverse))
10149
10150 (defun gnus-summary-sort-by-chars (&optional reverse)
10151   "Sort the summary buffer by article length.
10152 Argument REVERSE means reverse order."
10153   (interactive "P")
10154   (gnus-summary-sort 'chars reverse))
10155
10156 (defun gnus-summary-sort-by-original (&optional reverse)
10157   "Sort the summary buffer using the default sorting method.
10158 Argument REVERSE means reverse order."
10159   (interactive "P")
10160   (let* ((buffer-read-only)
10161          (gnus-summary-prepare-hook nil))
10162     ;; We do the sorting by regenerating the threads.
10163     (gnus-summary-prepare)
10164     ;; Hide subthreads if needed.
10165     (gnus-summary-maybe-hide-threads)))
10166
10167 (defun gnus-summary-sort (predicate reverse)
10168   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10169   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10170          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10171          (gnus-thread-sort-functions
10172           (if (not reverse)
10173               thread
10174             `(lambda (t1 t2)
10175                (,thread t2 t1))))
10176          (gnus-sort-gathered-threads-function
10177           gnus-thread-sort-functions)
10178          (gnus-article-sort-functions
10179           (if (not reverse)
10180               article
10181             `(lambda (t1 t2)
10182                (,article t2 t1))))
10183          (buffer-read-only)
10184          (gnus-summary-prepare-hook nil))
10185     ;; We do the sorting by regenerating the threads.
10186     (gnus-summary-prepare)
10187     ;; Hide subthreads if needed.
10188     (gnus-summary-maybe-hide-threads)))
10189
10190 ;; Summary saving commands.
10191
10192 (defun gnus-summary-save-article (&optional n not-saved)
10193   "Save the current article using the default saver function.
10194 If N is a positive number, save the N next articles.
10195 If N is a negative number, save the N previous articles.
10196 If N is nil and any articles have been marked with the process mark,
10197 save those articles instead.
10198 The variable `gnus-default-article-saver' specifies the saver function."
10199   (interactive "P")
10200   (let* ((articles (gnus-summary-work-articles n))
10201          (save-buffer (save-excursion
10202                         (nnheader-set-temp-buffer " *Gnus Save*")))
10203          (num (length articles))
10204          header file)
10205     (dolist (article articles)
10206       (setq header (gnus-summary-article-header article))
10207       (if (not (vectorp header))
10208           ;; This is a pseudo-article.
10209           (if (assq 'name header)
10210               (gnus-copy-file (cdr (assq 'name header)))
10211             (gnus-message 1 "Article %d is unsaveable" article))
10212         ;; This is a real article.
10213         (save-window-excursion
10214           (let ((gnus-display-mime-function nil)
10215                 (gnus-article-prepare-hook nil))
10216             (gnus-summary-select-article t nil nil article)))
10217         (save-excursion
10218           (set-buffer save-buffer)
10219           (erase-buffer)
10220           (insert-buffer-substring gnus-original-article-buffer))
10221         (setq file (gnus-article-save save-buffer file num))
10222         (gnus-summary-remove-process-mark article)
10223         (unless not-saved
10224           (gnus-summary-set-saved-mark article))))
10225     (gnus-kill-buffer save-buffer)
10226     (gnus-summary-position-point)
10227     (gnus-set-mode-line 'summary)
10228     n))
10229
10230 (defun gnus-summary-pipe-output (&optional arg)
10231   "Pipe the current article to a subprocess.
10232 If N is a positive number, pipe the N next articles.
10233 If N is a negative number, pipe the N previous articles.
10234 If N is nil and any articles have been marked with the process mark,
10235 pipe those articles instead."
10236   (interactive "P")
10237   (require 'gnus-art)
10238   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10239     (gnus-summary-save-article arg t))
10240   (let ((buffer (get-buffer "*Shell Command Output*")))
10241     (if (and buffer
10242              (with-current-buffer buffer (> (point-max) (point-min))))
10243         (gnus-configure-windows 'pipe))))
10244
10245 (defun gnus-summary-save-article-mail (&optional arg)
10246   "Append the current article to an mail file.
10247 If N is a positive number, save the N next articles.
10248 If N is a negative number, save the N previous articles.
10249 If N is nil and any articles have been marked with the process mark,
10250 save those articles instead."
10251   (interactive "P")
10252   (require 'gnus-art)
10253   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10254     (gnus-summary-save-article arg)))
10255
10256 (defun gnus-summary-save-article-rmail (&optional arg)
10257   "Append the current article to an rmail file.
10258 If N is a positive number, save the N next articles.
10259 If N is a negative number, save the N previous articles.
10260 If N is nil and any articles have been marked with the process mark,
10261 save those articles instead."
10262   (interactive "P")
10263   (require 'gnus-art)
10264   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10265     (gnus-summary-save-article arg)))
10266
10267 (defun gnus-summary-save-article-file (&optional arg)
10268   "Append the current article to a file.
10269 If N is a positive number, save the N next articles.
10270 If N is a negative number, save the N previous articles.
10271 If N is nil and any articles have been marked with the process mark,
10272 save those articles instead."
10273   (interactive "P")
10274   (require 'gnus-art)
10275   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10276     (gnus-summary-save-article arg)))
10277
10278 (defun gnus-summary-write-article-file (&optional arg)
10279   "Write the current article to a file, deleting the previous file.
10280 If N is a positive number, save the N next articles.
10281 If N is a negative number, save the N previous articles.
10282 If N is nil and any articles have been marked with the process mark,
10283 save those articles instead."
10284   (interactive "P")
10285   (require 'gnus-art)
10286   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10287     (gnus-summary-save-article arg)))
10288
10289 (defun gnus-summary-save-article-body-file (&optional arg)
10290   "Append the current article body to a file.
10291 If N is a positive number, save the N next articles.
10292 If N is a negative number, save the N previous articles.
10293 If N is nil and any articles have been marked with the process mark,
10294 save those articles instead."
10295   (interactive "P")
10296   (require 'gnus-art)
10297   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10298     (gnus-summary-save-article arg)))
10299
10300 (defun gnus-summary-muttprint (&optional arg)
10301   "Print the current article using Muttprint.
10302 If N is a positive number, save the N next articles.
10303 If N is a negative number, save the N previous articles.
10304 If N is nil and any articles have been marked with the process mark,
10305 save those articles instead."
10306   (interactive "P")
10307   (require 'gnus-art)
10308   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10309     (gnus-summary-save-article arg t)))
10310
10311 (defun gnus-summary-pipe-message (program)
10312   "Pipe the current article through PROGRAM."
10313   (interactive "sProgram: ")
10314   (gnus-summary-select-article)
10315   (let ((mail-header-separator ""))
10316     (gnus-eval-in-buffer-window gnus-article-buffer
10317       (save-restriction
10318         (widen)
10319         (let ((start (window-start))
10320               buffer-read-only)
10321           (message-pipe-buffer-body program)
10322           (set-window-start (get-buffer-window (current-buffer)) start))))))
10323
10324 (defun gnus-get-split-value (methods)
10325   "Return a value based on the split METHODS."
10326   (let (split-name method result match)
10327     (when methods
10328       (save-excursion
10329         (set-buffer gnus-original-article-buffer)
10330         (save-restriction
10331           (nnheader-narrow-to-headers)
10332           (while (and methods (not split-name))
10333             (goto-char (point-min))
10334             (setq method (pop methods))
10335             (setq match (car method))
10336             (when (cond
10337                    ((stringp match)
10338                     ;; Regular expression.
10339                     (ignore-errors
10340                       (re-search-forward match nil t)))
10341                    ((gnus-functionp match)
10342                     ;; Function.
10343                     (save-restriction
10344                       (widen)
10345                       (setq result (funcall match gnus-newsgroup-name))))
10346                    ((consp match)
10347                     ;; Form.
10348                     (save-restriction
10349                       (widen)
10350                       (setq result (eval match)))))
10351               (setq split-name (cdr method))
10352               (cond ((stringp result)
10353                      (push (expand-file-name
10354                             result gnus-article-save-directory)
10355                            split-name))
10356                     ((consp result)
10357                      (setq split-name (append result split-name)))))))))
10358     (nreverse split-name)))
10359
10360 (defun gnus-valid-move-group-p (group)
10361   (and (boundp group)
10362        (symbol-name group)
10363        (symbol-value group)
10364        (gnus-get-function (gnus-find-method-for-group
10365                            (symbol-name group)) 'request-accept-article t)))
10366
10367 (defun gnus-read-move-group-name (prompt default articles prefix)
10368   "Read a group name."
10369   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10370          (minibuffer-confirm-incomplete nil) ; XEmacs
10371          (prom
10372           (format "%s %s to:"
10373                   prompt
10374                   (if (> (length articles) 1)
10375                       (format "these %d articles" (length articles))
10376                     "this article")))
10377          (to-newsgroup
10378           (cond
10379            ((null split-name)
10380             (gnus-completing-read-with-default
10381              default prom
10382              gnus-active-hashtb
10383              'gnus-valid-move-group-p
10384              nil prefix
10385              'gnus-group-history))
10386            ((= 1 (length split-name))
10387             (gnus-completing-read-with-default
10388              (car split-name) prom
10389              gnus-active-hashtb
10390              'gnus-valid-move-group-p
10391              nil nil
10392              'gnus-group-history))
10393            (t
10394             (gnus-completing-read-with-default
10395              nil prom
10396              (mapcar (lambda (el) (list el))
10397                      (nreverse split-name))
10398              nil nil nil
10399              'gnus-group-history))))
10400          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10401     (when to-newsgroup
10402       (if (or (string= to-newsgroup "")
10403               (string= to-newsgroup prefix))
10404           (setq to-newsgroup default))
10405       (unless to-newsgroup
10406         (error "No group name entered"))
10407       (or (gnus-active to-newsgroup)
10408           (gnus-activate-group to-newsgroup nil nil to-method)
10409           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10410                                      to-newsgroup))
10411               (or (and (gnus-request-create-group to-newsgroup to-method)
10412                        (gnus-activate-group
10413                         to-newsgroup nil nil to-method)
10414                        (gnus-subscribe-group to-newsgroup))
10415                   (error "Couldn't create group %s" to-newsgroup)))
10416           (error "No such group: %s" to-newsgroup)))
10417     to-newsgroup))
10418
10419 (defun gnus-summary-save-parts (type dir n &optional reverse)
10420   "Save parts matching TYPE to DIR.
10421 If REVERSE, save parts that do not match TYPE."
10422   (interactive
10423    (list (read-string "Save parts of type: "
10424                       (or (car gnus-summary-save-parts-type-history)
10425                           gnus-summary-save-parts-default-mime)
10426                       'gnus-summary-save-parts-type-history)
10427          (setq gnus-summary-save-parts-last-directory
10428                (read-file-name "Save to directory: "
10429                                gnus-summary-save-parts-last-directory
10430                                nil t))
10431          current-prefix-arg))
10432   (gnus-summary-iterate n
10433     (let ((gnus-display-mime-function nil)
10434           (gnus-inhibit-treatment t))
10435       (gnus-summary-select-article))
10436     (save-excursion
10437       (set-buffer gnus-article-buffer)
10438       (let ((handles (or gnus-article-mime-handles
10439                          (mm-dissect-buffer
10440                           gnus-article-no-strict-mime)
10441                          (mm-uu-dissect))))
10442         (when handles
10443           (gnus-summary-save-parts-1 type dir handles reverse)
10444           (unless gnus-article-mime-handles ;; Don't destroy this case.
10445             (mm-destroy-parts handles)))))))
10446
10447 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10448   (if (stringp (car handle))
10449       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10450               (cdr handle))
10451     (when (if reverse
10452               (not (string-match type (mm-handle-media-type handle)))
10453             (string-match type (mm-handle-media-type handle)))
10454       (let ((file (expand-file-name
10455                    (file-name-nondirectory
10456                     (or
10457                      (mail-content-type-get
10458                       (mm-handle-disposition handle) 'filename)
10459                      (concat gnus-newsgroup-name
10460                              "." (number-to-string
10461                                   (cdr gnus-article-current)))))
10462                    dir)))
10463         (unless (file-exists-p file)
10464           (mm-save-part-to-file handle file))))))
10465
10466 ;; Summary extract commands
10467
10468 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10469   (let ((buffer-read-only nil)
10470         (article (gnus-summary-article-number))
10471         after-article b e)
10472     (unless (gnus-summary-goto-subject article)
10473       (error "No such article: %d" article))
10474     (gnus-summary-position-point)
10475     ;; If all commands are to be bunched up on one line, we collect
10476     ;; them here.
10477     (unless gnus-view-pseudos-separately
10478       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10479             files action)
10480         (while ps
10481           (setq action (cdr (assq 'action (car ps))))
10482           (setq files (list (cdr (assq 'name (car ps)))))
10483           (while (and ps (cdr ps)
10484                       (string= (or action "1")
10485                                (or (cdr (assq 'action (cadr ps))) "2")))
10486             (push (cdr (assq 'name (cadr ps))) files)
10487             (setcdr ps (cddr ps)))
10488           (when files
10489             (when (not (string-match "%s" action))
10490               (push " " files))
10491             (push " " files)
10492             (when (assq 'execute (car ps))
10493               (setcdr (assq 'execute (car ps))
10494                       (funcall (if (string-match "%s" action)
10495                                    'format 'concat)
10496                                action
10497                                (mapconcat
10498                                 (lambda (f)
10499                                   (if (equal f " ")
10500                                       f
10501                                     (gnus-quote-arg-for-sh-or-csh f)))
10502                                 files " ")))))
10503           (setq ps (cdr ps)))))
10504     (if (and gnus-view-pseudos (not not-view))
10505         (while pslist
10506           (when (assq 'execute (car pslist))
10507             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10508                                   (eq gnus-view-pseudos 'not-confirm)))
10509           (setq pslist (cdr pslist)))
10510       (save-excursion
10511         (while pslist
10512           (setq after-article (or (cdr (assq 'article (car pslist)))
10513                                   (gnus-summary-article-number)))
10514           (gnus-summary-goto-subject after-article)
10515           (forward-line 1)
10516           (setq b (point))
10517           (insert "    " (file-name-nondirectory
10518                           (cdr (assq 'name (car pslist))))
10519                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10520           (setq e (point))
10521           (forward-line -1)             ; back to `b'
10522           (gnus-add-text-properties
10523            b (1- e) (list 'gnus-number gnus-reffed-article-number
10524                           gnus-mouse-face-prop gnus-mouse-face))
10525           (gnus-data-enter
10526            after-article gnus-reffed-article-number
10527            gnus-unread-mark b (car pslist) 0 (- e b))
10528           (setq gnus-newsgroup-unreads
10529                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10530                                          gnus-reffed-article-number))
10531           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10532           (setq pslist (cdr pslist)))))))
10533
10534 (defun gnus-pseudos< (p1 p2)
10535   (let ((c1 (cdr (assq 'action p1)))
10536         (c2 (cdr (assq 'action p2))))
10537     (and c1 c2 (string< c1 c2))))
10538
10539 (defun gnus-request-pseudo-article (props)
10540   (cond ((assq 'execute props)
10541          (gnus-execute-command (cdr (assq 'execute props)))))
10542   (let ((gnus-current-article (gnus-summary-article-number)))
10543     (gnus-run-hooks 'gnus-mark-article-hook)))
10544
10545 (defun gnus-execute-command (command &optional automatic)
10546   (save-excursion
10547     (gnus-article-setup-buffer)
10548     (set-buffer gnus-article-buffer)
10549     (setq buffer-read-only nil)
10550     (let ((command (if automatic command
10551                      (read-string "Command: " (cons command 0)))))
10552       (erase-buffer)
10553       (insert "$ " command "\n\n")
10554       (if gnus-view-pseudo-asynchronously
10555           (start-process "gnus-execute" (current-buffer) shell-file-name
10556                          shell-command-switch command)
10557         (call-process shell-file-name nil t nil
10558                       shell-command-switch command)))))
10559
10560 ;; Summary kill commands.
10561
10562 (defun gnus-summary-edit-global-kill (article)
10563   "Edit the \"global\" kill file."
10564   (interactive (list (gnus-summary-article-number)))
10565   (gnus-group-edit-global-kill article))
10566
10567 (defun gnus-summary-edit-local-kill ()
10568   "Edit a local kill file applied to the current newsgroup."
10569   (interactive)
10570   (setq gnus-current-headers (gnus-summary-article-header))
10571   (gnus-group-edit-local-kill
10572    (gnus-summary-article-number) gnus-newsgroup-name))
10573
10574 ;;; Header reading.
10575
10576 (defun gnus-read-header (id &optional header)
10577   "Read the headers of article ID and enter them into the Gnus system."
10578   (let ((group gnus-newsgroup-name)
10579         (gnus-override-method
10580          (or
10581           gnus-override-method
10582           (and (gnus-news-group-p gnus-newsgroup-name)
10583                (car (gnus-refer-article-methods)))))
10584         where)
10585     ;; First we check to see whether the header in question is already
10586     ;; fetched.
10587     (if (stringp id)
10588         ;; This is a Message-ID.
10589         (setq header (or header (gnus-id-to-header id)))
10590       ;; This is an article number.
10591       (setq header (or header (gnus-summary-article-header id))))
10592     (if (and header
10593              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10594         ;; We have found the header.
10595         header
10596       ;; If this is a sparse article, we have to nix out its
10597       ;; previous entry in the thread hashtb.
10598       (when (and header
10599                  (gnus-summary-article-sparse-p (mail-header-number header)))
10600         (let* ((parent (gnus-parent-id (mail-header-references header)))
10601                (thread (and parent (gnus-id-to-thread parent))))
10602           (when thread
10603             (delq (assq header thread) thread))))
10604       ;; We have to really fetch the header to this article.
10605       (save-excursion
10606         (set-buffer nntp-server-buffer)
10607         (when (setq where (gnus-request-head id group))
10608           (nnheader-fold-continuation-lines)
10609           (goto-char (point-max))
10610           (insert ".\n")
10611           (goto-char (point-min))
10612           (insert "211 ")
10613           (princ (cond
10614                   ((numberp id) id)
10615                   ((cdr where) (cdr where))
10616                   (header (mail-header-number header))
10617                   (t gnus-reffed-article-number))
10618                  (current-buffer))
10619           (insert " Article retrieved.\n"))
10620         (if (or (not where)
10621                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10622             ()                          ; Malformed head.
10623           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10624             (when (and (stringp id)
10625                        (not (string= (gnus-group-real-name group)
10626                                      (car where))))
10627               ;; If we fetched by Message-ID and the article came
10628               ;; from a different group, we fudge some bogus article
10629               ;; numbers for this article.
10630               (mail-header-set-number header gnus-reffed-article-number))
10631             (save-excursion
10632               (set-buffer gnus-summary-buffer)
10633               (decf gnus-reffed-article-number)
10634               (gnus-remove-header (mail-header-number header))
10635               (push header gnus-newsgroup-headers)
10636               (setq gnus-current-headers header)
10637               (push (mail-header-number header) gnus-newsgroup-limit)))
10638           header)))))
10639
10640 (defun gnus-remove-header (number)
10641   "Remove header NUMBER from `gnus-newsgroup-headers'."
10642   (if (and gnus-newsgroup-headers
10643            (= number (mail-header-number (car gnus-newsgroup-headers))))
10644       (pop gnus-newsgroup-headers)
10645     (let ((headers gnus-newsgroup-headers))
10646       (while (and (cdr headers)
10647                   (not (= number (mail-header-number (cadr headers)))))
10648         (pop headers))
10649       (when (cdr headers)
10650         (setcdr headers (cddr headers))))))
10651
10652 ;;;
10653 ;;; summary highlights
10654 ;;;
10655
10656 (defun gnus-highlight-selected-summary ()
10657   "Highlight selected article in summary buffer."
10658   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10659   (when gnus-summary-selected-face
10660     (save-excursion
10661       (let* ((beg (progn (beginning-of-line) (point)))
10662              (end (progn (end-of-line) (point)))
10663              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10664              (from (if (get-text-property beg gnus-mouse-face-prop)
10665                        beg
10666                      (or (next-single-property-change
10667                           beg gnus-mouse-face-prop nil end)
10668                          beg)))
10669              (to
10670               (if (= from end)
10671                   (- from 2)
10672                 (or (next-single-property-change
10673                      from gnus-mouse-face-prop nil end)
10674                     end))))
10675         ;; If no mouse-face prop on line we will have to = from = end,
10676         ;; so we highlight the entire line instead.
10677         (when (= (+ to 2) from)
10678           (setq from beg)
10679           (setq to end))
10680         (if gnus-newsgroup-selected-overlay
10681             ;; Move old overlay.
10682             (gnus-move-overlay
10683              gnus-newsgroup-selected-overlay from to (current-buffer))
10684           ;; Create new overlay.
10685           (gnus-overlay-put
10686            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10687            'face gnus-summary-selected-face))))))
10688
10689 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10690 (defun gnus-summary-highlight-line ()
10691   "Highlight current line according to `gnus-summary-highlight'."
10692   (let* ((list gnus-summary-highlight)
10693          (p (point))
10694          (end (progn (end-of-line) (point)))
10695          ;; now find out where the line starts and leave point there.
10696          (beg (progn (beginning-of-line) (point)))
10697          (article (gnus-summary-article-number))
10698          (score (or (cdr (assq (or article gnus-current-article)
10699                                gnus-newsgroup-scored))
10700                     gnus-summary-default-score 0))
10701          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10702          (inhibit-read-only t))
10703     ;; Eval the cars of the lists until we find a match.
10704     (let ((default gnus-summary-default-score)
10705           (default-high gnus-summary-default-high-score)
10706           (default-low gnus-summary-default-low-score))
10707       (while (and list
10708                   (not (eval (caar list))))
10709         (setq list (cdr list))))
10710     (let ((face (cdar list)))
10711       (unless (eq face (get-text-property beg 'face))
10712         (gnus-put-text-property-excluding-characters-with-faces
10713          beg end 'face
10714          (setq face (if (boundp face) (symbol-value face) face)))
10715         (when gnus-summary-highlight-line-function
10716           (funcall gnus-summary-highlight-line-function article face))))
10717     (goto-char p)))
10718
10719 (defun gnus-update-read-articles (group unread &optional compute)
10720   "Update the list of read articles in GROUP.
10721 UNREAD is a sorted list."
10722   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10723          (entry (gnus-gethash group gnus-newsrc-hashtb))
10724          (info (nth 2 entry))
10725          (prev 1)
10726          read)
10727     (if (or (not info) (not active))
10728         ;; There is no info on this group if it was, in fact,
10729         ;; killed.  Gnus stores no information on killed groups, so
10730         ;; there's nothing to be done.
10731         ;; One could store the information somewhere temporarily,
10732         ;; perhaps...  Hmmm...
10733         ()
10734       ;; Remove any negative articles numbers.
10735       (while (and unread (< (car unread) 0))
10736         (setq unread (cdr unread)))
10737       ;; Remove any expired article numbers
10738       (while (and unread (< (car unread) (car active)))
10739         (setq unread (cdr unread)))
10740       ;; Compute the ranges of read articles by looking at the list of
10741       ;; unread articles.
10742       (while unread
10743         (when (/= (car unread) prev)
10744           (push (if (= prev (1- (car unread))) prev
10745                   (cons prev (1- (car unread))))
10746                 read))
10747         (setq prev (1+ (car unread)))
10748         (setq unread (cdr unread)))
10749       (when (<= prev (cdr active))
10750         (push (cons prev (cdr active)) read))
10751       (setq read (if (> (length read) 1) (nreverse read) read))
10752       (if compute
10753           read
10754         (save-excursion
10755           (let (setmarkundo)
10756             ;; Propagate the read marks to the backend.
10757             (when (gnus-check-backend-function 'request-set-mark group)
10758               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10759                     (add (gnus-remove-from-range read (gnus-info-read info))))
10760                 (when (or add del)
10761                   (unless (gnus-check-group group)
10762                     (error "Can't open server for %s" group))
10763                   (gnus-request-set-mark
10764                    group (delq nil (list (if add (list add 'add '(read)))
10765                                          (if del (list del 'del '(read))))))
10766                   (setq setmarkundo
10767                         `(gnus-request-set-mark
10768                           ,group
10769                           ',(delq nil (list
10770                                        (if del (list del 'add '(read)))
10771                                        (if add (list add 'del '(read))))))))))
10772             (set-buffer gnus-group-buffer)
10773             (gnus-undo-register
10774               `(progn
10775                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10776                  (gnus-info-set-read ',info ',(gnus-info-read info))
10777                  (gnus-get-unread-articles-in-group ',info
10778                                                     (gnus-active ,group))
10779                  (gnus-group-update-group ,group t)
10780                  ,setmarkundo))))
10781         ;; Enter this list into the group info.
10782         (gnus-info-set-read info read)
10783         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10784         (gnus-get-unread-articles-in-group info (gnus-active group))
10785         t))))
10786
10787 (defun gnus-offer-save-summaries ()
10788   "Offer to save all active summary buffers."
10789   (let (buffers)
10790     ;; Go through all buffers and find all summaries.
10791     (dolist (buffer (buffer-list))
10792       (when (and (setq buffer (buffer-name buffer))
10793                  (string-match "Summary" buffer)
10794                  (save-excursion
10795                    (set-buffer buffer)
10796                    ;; We check that this is, indeed, a summary buffer.
10797                    (and (eq major-mode 'gnus-summary-mode)
10798                         ;; Also make sure this isn't bogus.
10799                         gnus-newsgroup-prepared
10800                         ;; Also make sure that this isn't a
10801                         ;; dead summary buffer.
10802                         (not gnus-dead-summary-mode))))
10803         (push buffer buffers)))
10804     ;; Go through all these summary buffers and offer to save them.
10805     (when buffers
10806       (save-excursion
10807         (map-y-or-n-p
10808          "Update summary buffer %s? "
10809          (lambda (buf)
10810            (switch-to-buffer buf)
10811            (gnus-summary-exit))
10812          buffers)))))
10813
10814
10815 ;;; @ for mime-partial
10816 ;;;
10817
10818 (defun gnus-request-partial-message ()
10819   (save-excursion
10820     (let ((number (gnus-summary-article-number))
10821           (group gnus-newsgroup-name)
10822           (mother gnus-article-buffer))
10823       (set-buffer (get-buffer-create " *Partial Article*"))
10824       (erase-buffer)
10825       (setq mime-preview-buffer mother)
10826       (gnus-request-article-this-buffer number group)
10827       (mime-parse-buffer)
10828       )))
10829
10830 (autoload 'mime-combine-message/partial-pieces-automatically
10831   "mime-partial"
10832   "Internal method to combine message/partial messages automatically.")
10833
10834 (mime-add-condition
10835  'action '((type . message)(subtype . partial)
10836            (major-mode . gnus-original-article-mode)
10837            (method . mime-combine-message/partial-pieces-automatically)
10838            (summary-buffer-exp . gnus-summary-buffer)
10839            (request-partial-message-method . gnus-request-partial-message)
10840            ))
10841
10842
10843 ;;; @ for message/rfc822
10844 ;;;
10845
10846 (defun gnus-mime-extract-message/rfc822 (entity situation)
10847   "Burst a forwarded article."
10848   (save-excursion
10849     (set-buffer gnus-summary-buffer)
10850     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
10851                                    gnus-newsgroup-name 'gnus-group-history))
10852            (gnus-group-marked (list group))
10853            article info)
10854       (with-temp-buffer
10855         (mime-insert-entity-content entity)
10856         (setq article (gnus-request-accept-article group)))
10857       (when (and (consp article)
10858                  (numberp (setq article (cdr article))))
10859         (setq info (gnus-get-info group))
10860         (gnus-info-set-read info
10861                             (gnus-remove-from-range (gnus-info-read info)
10862                                                     (list article)))
10863         (when (string-equal group gnus-newsgroup-name)
10864           (forward-line 1)
10865           (let (gnus-show-threads)
10866             (gnus-summary-goto-subject article t))
10867           (gnus-summary-clear-mark-forward 1))
10868         (set-buffer gnus-group-buffer)
10869         (gnus-group-get-new-news-this-group nil t)))))
10870
10871 (mime-add-condition
10872  'action '((type . message)(subtype . rfc822)
10873            (major-mode . gnus-original-article-mode)
10874            (method . gnus-mime-extract-message/rfc822)
10875            (mode . "extract")
10876            ))
10877
10878 (mime-add-condition
10879  'action '((type . message)(subtype . news)
10880            (major-mode . gnus-original-article-mode)
10881            (method . gnus-mime-extract-message/rfc822)
10882            (mode . "extract")
10883            ))
10884
10885 (defun gnus-mime-extract-multipart (entity situation)
10886   (let ((children (mime-entity-children entity))
10887         mime-acting-situation-to-override
10888         f)
10889     (while children
10890       (mime-play-entity (car children)
10891                         (cons (assq 'mode situation)
10892                               mime-acting-situation-to-override))
10893       (setq children (cdr children)))
10894     (if (setq f (cdr (assq 'after-method
10895                            mime-acting-situation-to-override)))
10896         (eval f)
10897       )))
10898
10899 (mime-add-condition
10900  'action '((type . multipart)
10901            (method . gnus-mime-extract-multipart)
10902            (mode . "extract")
10903            )
10904  'with-default)
10905
10906
10907 ;;; @ end
10908 ;;;
10909
10910 (defun gnus-summary-setup-default-charset ()
10911   "Setup newsgroup default charset."
10912   (if (equal gnus-newsgroup-name "nndraft:drafts")
10913       (setq gnus-newsgroup-charset nil)
10914     (let* ((ignored-charsets
10915             (or gnus-newsgroup-ephemeral-ignored-charsets
10916                 (append
10917                  (and gnus-newsgroup-name
10918                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10919                  gnus-newsgroup-ignored-charsets))))
10920       (setq gnus-newsgroup-charset
10921             (or gnus-newsgroup-ephemeral-charset
10922                 (and gnus-newsgroup-name
10923                      (gnus-parameter-charset gnus-newsgroup-name))
10924                 gnus-default-charset))
10925       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10926            ignored-charsets))))
10927
10928 ;;;
10929 ;;; Mime Commands
10930 ;;;
10931
10932 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10933   "Display the current article buffer fully MIME-buttonized.
10934 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10935 treated as multipart/mixed."
10936   (interactive "P")
10937   (require 'gnus-art)
10938   (let ((gnus-unbuttonized-mime-types nil)
10939         (gnus-mime-display-multipart-as-mixed show-all-parts))
10940     (gnus-summary-show-article)))
10941
10942 (defun gnus-summary-repair-multipart (article)
10943   "Add a Content-Type header to a multipart article without one."
10944   (interactive (list (gnus-summary-article-number)))
10945   (gnus-with-article article
10946     (message-narrow-to-head)
10947     (message-remove-header "Mime-Version")
10948     (goto-char (point-max))
10949     (insert "Mime-Version: 1.0\n")
10950     (widen)
10951     (when (search-forward "\n--" nil t)
10952       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10953         (message-narrow-to-head)
10954         (message-remove-header "Content-Type")
10955         (goto-char (point-max))
10956         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10957                         separator))
10958         (widen))))
10959   (let (gnus-mark-article-hook)
10960     (gnus-summary-select-article t t nil article)))
10961
10962 (defun gnus-summary-toggle-display-buttonized ()
10963   "Toggle the buttonizing of the article buffer."
10964   (interactive)
10965   (require 'gnus-art)
10966   (if (setq gnus-inhibit-mime-unbuttonizing
10967             (not gnus-inhibit-mime-unbuttonizing))
10968       (let ((gnus-unbuttonized-mime-types nil))
10969         (gnus-summary-show-article))
10970     (gnus-summary-show-article)))
10971
10972 ;;;
10973 ;;; Intelli-mouse commmands
10974 ;;;
10975
10976 (defun gnus-wheel-summary-scroll (event)
10977   (interactive "e")
10978   (let ((amount (if (memq 'shift (event-modifiers event))
10979                     (car gnus-wheel-scroll-amount)
10980                   (cdr gnus-wheel-scroll-amount)))
10981         (direction (- (* (static-if (featurep 'xemacs)
10982                              (event-button event)
10983                            (cond ((eq 'mouse-4 (event-basic-type event))
10984                                   4)
10985                                  ((eq 'mouse-5 (event-basic-type event))
10986                                   5)))
10987                          2) 9))
10988         edge)
10989     (gnus-summary-scroll-up (* amount direction))
10990     (when (gnus-eval-in-buffer-window gnus-article-buffer
10991             (save-restriction
10992               (widen)
10993               (and (if (< 0 direction)
10994                        (gnus-article-next-page 0)
10995                      (gnus-article-prev-page 0)
10996                      (bobp))
10997                    (if (setq edge (get-text-property
10998                                    (point-min) 'gnus-wheel-edge))
10999                        (setq edge (* edge direction))
11000                      (setq edge -1))
11001                    (or (plusp edge)
11002                        (let ((buffer-read-only nil)
11003                              (inhibit-read-only t))
11004                          (put-text-property (point-min) (point-max)
11005                                             'gnus-wheel-edge direction)
11006                          nil))
11007                    (or (> edge gnus-wheel-edge-resistance)
11008                        (let ((buffer-read-only nil)
11009                              (inhibit-read-only t))
11010                          (put-text-property (point-min) (point-max)
11011                                             'gnus-wheel-edge
11012                                             (* (1+ edge) direction))
11013                          nil))
11014                    (eq last-command 'gnus-wheel-summary-scroll))))
11015       (gnus-summary-next-article nil nil (minusp direction)))))
11016
11017 (defun gnus-wheel-install ()
11018   "Enable mouse wheel support on summary window."
11019   (when gnus-use-wheel
11020     (let ((keys
11021            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11022       (dolist (key keys)
11023         (define-key gnus-summary-mode-map key
11024           'gnus-wheel-summary-scroll)))))
11025
11026 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11027
11028 ;;;
11029 ;;; Traditional PGP commmands
11030 ;;;
11031
11032 (defun gnus-summary-decrypt-article (&optional force)
11033   "Decrypt the current article in traditional PGP way.
11034 This will have permanent effect only in mail groups.
11035 If FORCE is non-nil, allow editing of articles even in read-only
11036 groups."
11037   (interactive "P")
11038   (gnus-summary-select-article t)
11039   (gnus-eval-in-buffer-window gnus-article-buffer
11040     (save-excursion
11041       (save-restriction
11042         (widen)
11043         (goto-char (point-min))
11044         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11045           (error "Not a traditional PGP message!"))
11046         (let ((armor-start (match-beginning 0)))
11047           (if (and (pgg-decrypt-region armor-start (point-max))
11048                    (or force (not (gnus-group-read-only-p))))
11049               (let ((inhibit-read-only t)
11050                     buffer-read-only)
11051                 (delete-region armor-start
11052                                (progn
11053                                  (re-search-forward "^-+END PGP" nil t)
11054                                  (beginning-of-line 2)
11055                                  (point)))
11056                 (insert-buffer-substring pgg-output-buffer))))))))
11057
11058 (defun gnus-summary-verify-article ()
11059   "Verify the current article in traditional PGP way."
11060   (interactive)
11061   (save-excursion
11062     (set-buffer gnus-original-article-buffer)
11063     (goto-char (point-min))
11064     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11065       (error "Not a traditional PGP message!"))
11066     (re-search-forward "^-+END PGP" nil t)
11067     (beginning-of-line 2)
11068     (call-interactively (function pgg-verify-region))))
11069
11070 ;;;
11071 ;;; Generic summary marking commands
11072 ;;;
11073
11074 (defvar gnus-summary-marking-alist
11075   '((read gnus-del-mark "d")
11076     (unread gnus-unread-mark "u")
11077     (ticked gnus-ticked-mark "!")
11078     (dormant gnus-dormant-mark "?")
11079     (expirable gnus-expirable-mark "e"))
11080   "An alist of names/marks/keystrokes.")
11081
11082 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11083 (defvar gnus-summary-mark-map)
11084
11085 (defun gnus-summary-make-all-marking-commands ()
11086   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11087   (dolist (elem gnus-summary-marking-alist)
11088     (apply 'gnus-summary-make-marking-command elem)))
11089
11090 (defun gnus-summary-make-marking-command (name mark keystroke)
11091   (let ((map (make-sparse-keymap)))
11092     (define-key gnus-summary-generic-mark-map keystroke map)
11093     (dolist (lway `((next "next" next nil "n")
11094                     (next-unread "next unread" next t "N")
11095                     (prev "previous" prev nil "p")
11096                     (prev-unread "previous unread" prev t "P")
11097                     (nomove "" nil nil ,keystroke)))
11098       (let ((func (gnus-summary-make-marking-command-1
11099                    mark (car lway) lway name)))
11100         (setq func (eval func))
11101         (define-key map (nth 4 lway) func)))))
11102
11103 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11104   `(defun ,(intern
11105             (format "gnus-summary-put-mark-as-%s%s"
11106                     name (if (eq way 'nomove)
11107                              ""
11108                            (concat "-" (symbol-name way)))))
11109      (n)
11110      ,(format
11111        "Mark the current article as %s%s.
11112 If N, the prefix, then repeat N times.
11113 If N is negative, move in reverse order.
11114 The difference between N and the actual number of articles marked is
11115 returned."
11116        name (car (cdr lway)))
11117      (interactive "p")
11118      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11119
11120 (defun gnus-summary-generic-mark (n mark move unread)
11121   "Mark N articles with MARK."
11122   (unless (eq major-mode 'gnus-summary-mode)
11123     (error "This command can only be used in the summary buffer"))
11124   (gnus-summary-show-thread)
11125   (let ((nummove
11126          (cond
11127           ((eq move 'next) 1)
11128           ((eq move 'prev) -1)
11129           (t 0))))
11130     (if (zerop nummove)
11131         (setq n 1)
11132       (when (< n 0)
11133         (setq n (abs n)
11134               nummove (* -1 nummove))))
11135     (while (and (> n 0)
11136                 (gnus-summary-mark-article nil mark)
11137                 (zerop (gnus-summary-next-subject nummove unread t)))
11138       (setq n (1- n)))
11139     (when (/= 0 n)
11140       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11141     (gnus-summary-recenter)
11142     (gnus-summary-position-point)
11143     (gnus-set-mode-line 'summary)
11144     n))
11145
11146 (defun gnus-summary-insert-articles (articles)
11147   (when (setq articles
11148               (gnus-sorted-difference articles
11149                                       (mapcar (lambda (h)
11150                                                 (mail-header-number h))
11151                                               gnus-newsgroup-headers)))
11152     (setq gnus-newsgroup-headers
11153           (merge 'list
11154                  gnus-newsgroup-headers
11155                  (gnus-fetch-headers articles)
11156                  'gnus-article-sort-by-number))
11157     ;; Suppress duplicates?
11158     (when gnus-suppress-duplicates
11159       (gnus-dup-suppress-articles))
11160
11161     ;; We might want to build some more threads first.
11162     (when (and gnus-fetch-old-headers
11163                (eq gnus-headers-retrieved-by 'nov))
11164       (if (eq gnus-fetch-old-headers 'invisible)
11165           (gnus-build-all-threads)
11166         (gnus-build-old-threads)))
11167     ;; Let the Gnus agent mark articles as read.
11168     (when gnus-agent
11169       (gnus-agent-get-undownloaded-list))
11170     ;; Remove list identifiers from subject
11171     (when gnus-list-identifiers
11172       (gnus-summary-remove-list-identifiers))
11173     ;; First and last article in this newsgroup.
11174     (when gnus-newsgroup-headers
11175       (setq gnus-newsgroup-begin
11176             (mail-header-number (car gnus-newsgroup-headers))
11177             gnus-newsgroup-end
11178             (mail-header-number
11179              (gnus-last-element gnus-newsgroup-headers))))
11180     (when gnus-use-scoring
11181       (gnus-possibly-score-headers))))
11182
11183 (defun gnus-summary-insert-old-articles (&optional all)
11184   "Insert all old articles in this group.
11185 If ALL is non-nil, already read articles become readable.
11186 If ALL is a number, fetch this number of articles."
11187   (interactive "P")
11188   (prog1
11189       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11190             older len)
11191         (setq older
11192               (gnus-sorted-difference
11193                (gnus-uncompress-range (list gnus-newsgroup-active))
11194                old))
11195         (setq len (length older))
11196         (cond
11197          ((null older) nil)
11198          ((numberp all)
11199           (if (< all len)
11200               (setq older (last older all))))
11201          (all nil)
11202          (t
11203           (if (and (numberp gnus-large-newsgroup)
11204                    (> len gnus-large-newsgroup))
11205               (let ((input
11206                      (read-string
11207                       (format
11208                        "How many articles from %s (default %d): "
11209                        (gnus-limit-string
11210                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
11211                        len))))
11212                 (unless (string-match "^[ \t]*$" input)
11213                   (setq all (string-to-number input))
11214                   (if (< all len)
11215                       (setq older (last older all))))))))
11216         (if (not older)
11217             (message "No old news.")
11218           (gnus-summary-insert-articles older)
11219           (gnus-summary-limit (gnus-sorted-nunion old older))))
11220     (gnus-summary-position-point)))
11221
11222 (defun gnus-summary-insert-new-articles ()
11223   "Insert all new articles in this group."
11224   (interactive)
11225   (prog1
11226       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11227             (old-active gnus-newsgroup-active)
11228             (nnmail-fetched-sources (list t))
11229             i new)
11230         (setq gnus-newsgroup-active
11231               (gnus-activate-group gnus-newsgroup-name 'scan))
11232         (setq i (1+ (cdr old-active)))
11233         (while (<= i (cdr gnus-newsgroup-active))
11234           (push i new)
11235           (incf i))
11236         (if (not new)
11237             (message "No gnus is bad news.")
11238           (setq new (nreverse new))
11239           (gnus-summary-insert-articles new)
11240           (setq gnus-newsgroup-unreads
11241                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11242           (gnus-summary-limit (gnus-sorted-nunion old new))))
11243     (gnus-summary-position-point)))
11244
11245 (gnus-summary-make-all-marking-commands)
11246
11247 (gnus-ems-redefine)
11248
11249 (provide 'gnus-sum)
11250
11251 (run-hooks 'gnus-sum-load-hook)
11252
11253 ;;; gnus-sum.el ends here