9ea1efc3edf46d37a6daa8522857c355a5269277
[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-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-make-false-root-always t
124   "Always make a false dummy root."
125   :group 'gnus-thread
126   :type 'boolean)
127
128 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
129   "*A regexp to match subjects to be excluded from loose thread gathering.
130 As loose thread gathering is done on subjects only, that means that
131 there can be many false gatherings performed.  By rooting out certain
132 common subjects, gathering might become saner."
133   :group 'gnus-thread
134   :type 'regexp)
135
136 (defcustom gnus-summary-gather-subject-limit nil
137   "*Maximum length of subject comparisons when gathering loose threads.
138 Use nil to compare full subjects.  Setting this variable to a low
139 number will help gather threads that have been corrupted by
140 newsreaders chopping off subject lines, but it might also mean that
141 unrelated articles that have subject that happen to begin with the
142 same few characters will be incorrectly gathered.
143
144 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
145 comparing subjects."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const fuzzy)
149                  (sexp :menu-tag "on" t)))
150
151 (defcustom gnus-simplify-subject-functions nil
152   "List of functions taking a string argument that simplify subjects.
153 The functions are applied recursively.
154
155 Useful functions to put in this list include:
156 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
157 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
158   :group 'gnus-thread
159   :type '(repeat function))
160
161 (defcustom gnus-simplify-ignored-prefixes nil
162   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
163   :group 'gnus-thread
164   :type '(choice (const :tag "off" nil)
165                  regexp))
166
167 (defcustom gnus-build-sparse-threads nil
168   "*If non-nil, fill in the gaps in threads.
169 If `some', only fill in the gaps that are needed to tie loose threads
170 together.  If `more', fill in all leaf nodes that Gnus can find.  If
171 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
172   :group 'gnus-thread
173   :type '(choice (const :tag "off" nil)
174                  (const some)
175                  (const more)
176                  (sexp :menu-tag "all" t)))
177
178 (defcustom gnus-summary-thread-gathering-function
179   'gnus-gather-threads-by-subject
180   "*Function used for gathering loose threads.
181 There are two pre-defined functions: `gnus-gather-threads-by-subject',
182 which only takes Subjects into consideration; and
183 `gnus-gather-threads-by-references', which compared the References
184 headers of the articles to find matches."
185   :group 'gnus-thread
186   :type '(radio (function-item gnus-gather-threads-by-subject)
187                 (function-item gnus-gather-threads-by-references)
188                 (function :tag "other")))
189
190 (defcustom gnus-summary-same-subject ""
191   "*String indicating that the current article has the same subject as the previous.
192 This variable will only be used if the value of
193 `gnus-summary-make-false-root' is `empty'."
194   :group 'gnus-summary-format
195   :type 'string)
196
197 (defcustom gnus-summary-goto-unread t
198   "*If t, many commands will go to the next unread article.
199 This applies to marking commands as well as other commands that
200 \"naturally\" select the next article, like, for instance, `SPC' at
201 the end of an article.
202
203 If nil, the marking commands do NOT go to the next unread article
204 \(they go to the next article instead).  If `never', commands that
205 usually go to the next unread article, will go to the next article,
206 whether it is read or not."
207   :group 'gnus-summary-marks
208   :link '(custom-manual "(gnus)Setting Marks")
209   :type '(choice (const :tag "off" nil)
210                  (const never)
211                  (sexp :menu-tag "on" t)))
212
213 (defcustom gnus-summary-default-score 0
214   "*Default article score level.
215 All scores generated by the score files will be added to this score.
216 If this variable is nil, scoring will be disabled."
217   :group 'gnus-score-default
218   :type '(choice (const :tag "disable")
219                  integer))
220
221 (defcustom gnus-summary-default-high-score 0
222   "*Default threshold for a high scored article.
223 An article will be highlighted as high scored if its score is greater
224 than this score."
225   :group 'gnus-score-default
226   :type 'integer)
227
228 (defcustom gnus-summary-default-low-score 0
229   "*Default threshold for a low scored article.
230 An article will be highlighted as low scored if its score is smaller
231 than this score."
232   :group 'gnus-score-default
233   :type 'integer)
234
235 (defcustom gnus-summary-zcore-fuzz 0
236   "*Fuzziness factor for the zcore in the summary buffer.
237 Articles with scores closer than this to `gnus-summary-default-score'
238 will not be marked."
239   :group 'gnus-summary-format
240   :type 'integer)
241
242 (defcustom gnus-simplify-subject-fuzzy-regexp nil
243   "*Strings to be removed when doing fuzzy matches.
244 This can either be a regular expression or list of regular expressions
245 that will be removed from subject strings if fuzzy subject
246 simplification is selected."
247   :group 'gnus-thread
248   :type '(repeat regexp))
249
250 (defcustom gnus-show-threads t
251   "*If non-nil, display threads in summary mode."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-hide-subtree nil
256   "*If non-nil, hide all threads initially.
257 This can be a predicate specifier which says which threads to hide.
258 If threads are hidden, you have to run the command
259 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
260 to expose hidden threads."
261   :group 'gnus-thread
262   :type 'boolean)
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-dont-select-after-jump-to-other-group nil
330   "If non-nil, don't select the first unread article after entering the
331 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
332 it is depend on the value of `gnus-auto-select-first' whether to select
333 or not."
334   :group 'gnus-group-select
335   :type 'boolean)
336
337 (defcustom gnus-auto-select-next t
338   "*If non-nil, offer to go to the next group from the end of the previous.
339 If the value is t and the next newsgroup is empty, Gnus will exit
340 summary mode and go back to group mode.  If the value is neither nil
341 nor t, Gnus will select the following unread newsgroup.  In
342 particular, if the value is the symbol `quietly', the next unread
343 newsgroup will be selected without any confirmation, and if it is
344 `almost-quietly', the next group will be selected without any
345 confirmation if you are located on the last article in the group.
346 Finally, if this variable is `slightly-quietly', the `Z n' command
347 will go to the next group without confirmation."
348   :group 'gnus-summary-maneuvering
349   :type '(choice (const :tag "off" nil)
350                  (const quietly)
351                  (const almost-quietly)
352                  (const slightly-quietly)
353                  (sexp :menu-tag "on" t)))
354
355 (defcustom gnus-auto-select-same nil
356   "*If non-nil, select the next article with the same subject.
357 If there are no more articles with the same subject, go to
358 the first unread article."
359   :group 'gnus-summary-maneuvering
360   :type 'boolean)
361
362 (defcustom gnus-summary-check-current nil
363   "*If non-nil, consider the current article when moving.
364 The \"unread\" movement commands will stay on the same line if the
365 current article is unread."
366   :group 'gnus-summary-maneuvering
367   :type 'boolean)
368
369 (defcustom gnus-auto-center-summary t
370   "*If non-nil, always center the current summary buffer.
371 In particular, if `vertical' do only vertical recentering.  If non-nil
372 and non-`vertical', do both horizontal and vertical recentering."
373   :group 'gnus-summary-maneuvering
374   :type '(choice (const :tag "none" nil)
375                  (const vertical)
376                  (integer :tag "height")
377                  (sexp :menu-tag "both" t)))
378
379 (defcustom gnus-show-all-headers nil
380   "*If non-nil, don't hide any headers."
381   :group 'gnus-article-hiding
382   :group 'gnus-article-headers
383   :type 'boolean)
384
385 (defcustom gnus-summary-ignore-duplicates nil
386   "*If non-nil, ignore articles with identical Message-ID headers."
387   :group 'gnus-summary
388   :type 'boolean)
389
390 (defcustom gnus-single-article-buffer t
391   "*If non-nil, display all articles in the same buffer.
392 If nil, each group will get its own article buffer."
393   :group 'gnus-article-various
394   :type 'boolean)
395
396 (defcustom gnus-break-pages t
397   "*If non-nil, do page breaking on articles.
398 The page delimiter is specified by the `gnus-page-delimiter'
399 variable."
400   :group 'gnus-article-various
401   :type 'boolean)
402
403 (defcustom gnus-show-mime t
404   "*If non-nil, do mime processing of articles.
405 The articles will simply be fed to the function given by
406 `gnus-article-display-method-for-mime'."
407   :group 'gnus-article-mime
408   :type 'boolean)
409
410 (defcustom gnus-move-split-methods nil
411   "*Variable used to suggest where articles are to be moved to.
412 It uses the same syntax as the `gnus-split-methods' variable.
413 However, whereas `gnus-split-methods' specifies file names as targets,
414 this variable specifies group names."
415   :group 'gnus-summary-mail
416   :type '(repeat (choice (list :value (fun) function)
417                          (cons :value ("" "") regexp (repeat string))
418                          (sexp :value nil))))
419
420 (defcustom gnus-unread-mark ?\ ;;;Whitespace
421   "*Mark used for unread articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ticked-mark ?!
426   "*Mark used for ticked articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-dormant-mark ??
431   "*Mark used for dormant articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-del-mark ?r
436   "*Mark used for del'd articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-read-mark ?R
441   "*Mark used for read articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-expirable-mark ?E
446   "*Mark used for expirable articles."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-killed-mark ?K
451   "*Mark used for killed articles."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-spam-mark ?H
456   "*Mark used for spam articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-souped-mark ?F
461   "*Mark used for souped articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-kill-file-mark ?X
466   "*Mark used for articles killed by kill files."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-low-score-mark ?Y
471   "*Mark used for articles with a low score."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-catchup-mark ?C
476   "*Mark used for articles that are caught up."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-replied-mark ?A
481   "*Mark used for articles that have been replied to."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-forwarded-mark ?F
486   "*Mark used for articles that have been forwarded."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-recent-mark ?N
491   "*Mark used for articles that are recent."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-cached-mark ?*
496   "*Mark used for articles that are in the cache."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-saved-mark ?S
501   "*Mark used for articles that have been saved."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-unseen-mark ?.
506   "*Mark used for articles that haven't been seen."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-no-mark ?\ ;;;Whitespace
511   "*Mark used for articles that have no other secondary mark."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-ancient-mark ?O
516   "*Mark used for ancient articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-sparse-mark ?Q
521   "*Mark used for sparsely reffed articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-canceled-mark ?G
526   "*Mark used for canceled articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-duplicate-mark ?M
531   "*Mark used for duplicate articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-undownloaded-mark ?-
536   "*Mark used for articles that weren't downloaded."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-downloaded-mark ?+
541   "*Mark used for articles that were downloaded."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-downloadable-mark ?%
546   "*Mark used for articles that are to be downloaded."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-unsendable-mark ?=
551   "*Mark used for articles that won't be sent."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-score-over-mark ?+
556   "*Score mark used for articles with high scores."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-score-below-mark ?-
561   "*Score mark used for articles with low scores."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
566   "*There is no thread under the article."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-not-empty-thread-mark ?=
571   "*There is a thread under the article."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-view-pseudo-asynchronously nil
576   "*If non-nil, Gnus will view pseudo-articles asynchronously."
577   :group 'gnus-extract-view
578   :type 'boolean)
579
580 (defcustom gnus-auto-expirable-marks
581   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
582         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
583         gnus-souped-mark gnus-duplicate-mark)
584   "*The list of marks converted into expiration if a group is auto-expirable."
585   :version "21.1"
586   :group 'gnus-summary
587   :type '(repeat character))
588
589 (defcustom gnus-inhibit-user-auto-expire t
590   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
591   :version "21.1"
592   :group 'gnus-summary
593   :type 'boolean)
594
595 (defcustom gnus-view-pseudos nil
596   "*If `automatic', pseudo-articles will be viewed automatically.
597 If `not-confirm', pseudos will be viewed automatically, and the user
598 will not be asked to confirm the command."
599   :group 'gnus-extract-view
600   :type '(choice (const :tag "off" nil)
601                  (const automatic)
602                  (const not-confirm)))
603
604 (defcustom gnus-view-pseudos-separately t
605   "*If non-nil, one pseudo-article will be created for each file to be viewed.
606 If nil, all files that use the same viewing command will be given as a
607 list of parameters to that command."
608   :group 'gnus-extract-view
609   :type 'boolean)
610
611 (defcustom gnus-insert-pseudo-articles t
612   "*If non-nil, insert pseudo-articles when decoding articles."
613   :group 'gnus-extract-view
614   :type 'boolean)
615
616 (defcustom gnus-summary-dummy-line-format
617   "   %(:                             :%) %S\n"
618   "*The format specification for the dummy roots in the summary buffer.
619 It works along the same lines as a normal formatting string,
620 with some simple extensions.
621
622 %S  The subject
623
624 General format specifiers can also be used.
625 See `(gnus)Formatting Variables'."
626   :link '(custom-manual "(gnus)Formatting Variables")
627   :group 'gnus-threading
628   :type 'string)
629
630 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
631   "*The format specification for the summary mode line.
632 It works along the same lines as a normal formatting string,
633 with some simple extensions:
634
635 %G  Group name
636 %p  Unprefixed group name
637 %A  Current article number
638 %z  Current article score
639 %V  Gnus version
640 %U  Number of unread articles in the group
641 %e  Number of unselected articles in the group
642 %Z  A string with unread/unselected article counts
643 %g  Shortish group name
644 %S  Subject of the current article
645 %u  User-defined spec
646 %s  Current score file name
647 %d  Number of dormant articles
648 %r  Number of articles that have been marked as read in this session
649 %E  Number of articles expunged by the score files"
650   :group 'gnus-summary-format
651   :type 'string)
652
653 (defcustom gnus-list-identifiers nil
654   "Regexp that matches list identifiers to be removed from subject.
655 This can also be a list of regexps."
656   :version "21.1"
657   :group 'gnus-summary-format
658   :group 'gnus-article-hiding
659   :type '(choice (const :tag "none" nil)
660                  (regexp :value ".*")
661                  (repeat :value (".*") regexp)))
662
663 (defcustom gnus-summary-mark-below 0
664   "*Mark all articles with a score below this variable as read.
665 This variable is local to each summary buffer and usually set by the
666 score file."
667   :group 'gnus-score-default
668   :type 'integer)
669
670 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
671   "*List of functions used for sorting articles in the summary buffer.
672
673 Each function takes two articles and returns non-nil if the first
674 article should be sorted before the other.  If you use more than one
675 function, the primary sort function should be the last.  You should
676 probably always include `gnus-article-sort-by-number' in the list of
677 sorting functions -- preferably first.  Also note that sorting by date
678 is often much slower than sorting by number, and the sorting order is
679 very similar.  (Sorting by date means sorting by the time the message
680 was sent, sorting by number means sorting by arrival time.)
681
682 Ready-made functions include `gnus-article-sort-by-number',
683 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
684 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
685 and `gnus-article-sort-by-score'.
686
687 When threading is turned on, the variable `gnus-thread-sort-functions'
688 controls how articles are sorted."
689   :group 'gnus-summary-sort
690   :type '(repeat (choice (function-item gnus-article-sort-by-number)
691                          (function-item gnus-article-sort-by-author)
692                          (function-item gnus-article-sort-by-subject)
693                          (function-item gnus-article-sort-by-date)
694                          (function-item gnus-article-sort-by-score)
695                          (function-item gnus-article-sort-by-random)
696                          (function :tag "other"))))
697
698 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
699   "*List of functions used for sorting threads in the summary buffer.
700 By default, threads are sorted by article number.
701
702 Each function takes two threads and returns non-nil if the first
703 thread should be sorted before the other.  If you use more than one
704 function, the primary sort function should be the last.  You should
705 probably always include `gnus-thread-sort-by-number' in the list of
706 sorting functions -- preferably first.  Also note that sorting by date
707 is often much slower than sorting by number, and the sorting order is
708 very similar.  (Sorting by date means sorting by the time the message
709 was sent, sorting by number means sorting by arrival time.)
710
711 Ready-made functions include `gnus-thread-sort-by-number',
712 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
713 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
714 `gnus-thread-sort-by-most-recent-number',
715 `gnus-thread-sort-by-most-recent-date',
716 `gnus-thread-sort-by-random', and
717 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
718
719 When threading is turned off, the variable
720 `gnus-article-sort-functions' controls how articles are sorted."
721   :group 'gnus-summary-sort
722   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
723                          (function-item gnus-thread-sort-by-author)
724                          (function-item gnus-thread-sort-by-subject)
725                          (function-item gnus-thread-sort-by-date)
726                          (function-item gnus-thread-sort-by-score)
727                          (function-item gnus-thread-sort-by-total-score)
728                          (function-item gnus-thread-sort-by-random)
729                          (function :tag "other"))))
730
731 (defcustom gnus-thread-score-function '+
732   "*Function used for calculating the total score of a thread.
733
734 The function is called with the scores of the article and each
735 subthread and should then return the score of the thread.
736
737 Some functions you can use are `+', `max', or `min'."
738   :group 'gnus-summary-sort
739   :type 'function)
740
741 (defcustom gnus-summary-expunge-below nil
742   "All articles that have a score less than this variable will be expunged.
743 This variable is local to the summary buffers."
744   :group 'gnus-score-default
745   :type '(choice (const :tag "off" nil)
746                  integer))
747
748 (defcustom gnus-thread-expunge-below nil
749   "All threads that have a total score less than this variable will be expunged.
750 See `gnus-thread-score-function' for en explanation of what a
751 \"thread score\" is.
752
753 This variable is local to the summary buffers."
754   :group 'gnus-threading
755   :group 'gnus-score-default
756   :type '(choice (const :tag "off" nil)
757                  integer))
758
759 (defcustom gnus-summary-mode-hook nil
760   "*A hook for Gnus summary mode.
761 This hook is run before any variables are set in the summary buffer."
762   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
763   :group 'gnus-summary-various
764   :type 'hook)
765
766 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
767 (when (featurep 'xemacs)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
769   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
770   (add-hook 'gnus-summary-mode-hook
771             'gnus-xmas-switch-horizontal-scrollbar-off))
772
773 (defcustom gnus-summary-menu-hook nil
774   "*Hook run after the creation of the summary mode menu."
775   :group 'gnus-summary-visual
776   :type 'hook)
777
778 (defcustom gnus-summary-exit-hook nil
779   "*A hook called on exit from the summary buffer.
780 It will be called with point in the group buffer."
781   :group 'gnus-summary-exit
782   :type 'hook)
783
784 (defcustom gnus-summary-prepare-hook nil
785   "*A hook called after the summary buffer has been generated.
786 If you want to modify the summary buffer, you can use this hook."
787   :group 'gnus-summary-various
788   :type 'hook)
789
790 (defcustom gnus-summary-prepared-hook nil
791   "*A hook called as the last thing after the summary buffer has been generated."
792   :group 'gnus-summary-various
793   :type 'hook)
794
795 (defcustom gnus-summary-generate-hook nil
796   "*A hook run just before generating the summary buffer.
797 This hook is commonly used to customize threading variables and the
798 like."
799   :group 'gnus-summary-various
800   :type 'hook)
801
802 (defcustom gnus-select-group-hook nil
803   "*A hook called when a newsgroup is selected.
804
805 If you'd like to simplify subjects like the
806 `gnus-summary-next-same-subject' command does, you can use the
807 following hook:
808
809  (add-hook gnus-select-group-hook
810            (lambda ()
811              (mapcar (lambda (header)
812                        (mail-header-set-subject
813                         header
814                         (gnus-simplify-subject
815                          (mail-header-subject header) 're-only)))
816                      gnus-newsgroup-headers)))"
817   :group 'gnus-group-select
818   :type 'hook)
819
820 (defcustom gnus-select-article-hook nil
821   "*A hook called when an article is selected."
822   :group 'gnus-summary-choose
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-display-arrow
873   (and (fboundp 'display-graphic-p)
874        (display-graphic-p))
875   "*If non-nil, display an arrow highlighting the current article."
876   :version "21.1"
877   :group 'gnus-summary
878   :type 'boolean)
879
880 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
881   "Face used for highlighting the current article in the summary buffer."
882   :group 'gnus-summary-visual
883   :type 'face)
884
885 (defvar gnus-tmp-downloaded nil)
886
887 (defcustom gnus-summary-highlight
888   '(((eq mark gnus-canceled-mark)
889      . gnus-summary-cancelled-face)
890     ((and uncached (> score default-high))
891      . gnus-summary-high-uncached-face)
892     ((and uncached (< score default-low))
893      . gnus-summary-low-uncached-face)
894     (uncached
895      . gnus-summary-normal-uncached-face)
896     ((and (> score default-high)
897           (or (eq mark gnus-dormant-mark)
898               (eq mark gnus-ticked-mark)))
899      . gnus-summary-high-ticked-face)
900     ((and (< score default-low)
901           (or (eq mark gnus-dormant-mark)
902               (eq mark gnus-ticked-mark)))
903      . gnus-summary-low-ticked-face)
904     ((or (eq mark gnus-dormant-mark)
905          (eq mark gnus-ticked-mark))
906      . gnus-summary-normal-ticked-face)
907     ((and (> score default-high) (eq mark gnus-ancient-mark))
908      . gnus-summary-high-ancient-face)
909     ((and (< score default-low) (eq mark gnus-ancient-mark))
910      . gnus-summary-low-ancient-face)
911     ((eq mark gnus-ancient-mark)
912      . gnus-summary-normal-ancient-face)
913     ((and (> score default-high) (eq mark gnus-unread-mark))
914      . gnus-summary-high-unread-face)
915     ((and (< score default-low) (eq mark gnus-unread-mark))
916      . gnus-summary-low-unread-face)
917     ((eq mark gnus-unread-mark)
918      . gnus-summary-normal-unread-face)
919     ((> score default-high)
920      . gnus-summary-high-read-face)
921     ((< score default-low)
922      . gnus-summary-low-read-face)
923     (t
924      . gnus-summary-normal-read-face))
925   "*Controls the highlighting of summary buffer lines.
926
927 A list of (FORM . FACE) pairs.  When deciding how a a particular
928 summary line should be displayed, each form is evaluated.  The content
929 of the face field after the first true form is used.  You can change
930 how those summary lines are displayed, by editing the face field.
931
932 You can use the following variables in the FORM field.
933
934 score:        The article's score
935 default:      The default article score.
936 default-high: The default score for high scored articles.
937 default-low:  The default score for low scored articles.
938 below:        The score below which articles are automatically marked as read.
939 mark:         The articles mark."
940   :group 'gnus-summary-visual
941   :type '(repeat (cons (sexp :tag "Form" nil)
942                        face)))
943
944 (defcustom gnus-alter-header-function nil
945   "Function called to allow alteration of article header structures.
946 The function is called with one parameter, the article header vector,
947 which it may alter in any way.")
948
949 (defvar gnus-decode-encoded-word-function
950   (mime-find-field-decoder 'From 'nov)
951   "Variable that says which function should be used to decode a string with encoded words.")
952
953 (defcustom gnus-extra-headers '(To Newsgroups)
954   "*Extra headers to parse."
955   :version "21.1"
956   :group 'gnus-summary
957   :type '(repeat symbol))
958
959 (defcustom gnus-ignored-from-addresses
960   (and user-mail-address (regexp-quote user-mail-address))
961   "*Regexp of From headers that may be suppressed in favor of To headers."
962   :version "21.1"
963   :group 'gnus-summary
964   :type 'regexp)
965
966 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
967   "List of charsets that should be ignored.
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead."
970   :version "21.1"
971   :type '(repeat symbol)
972   :group 'gnus-charset)
973
974 (gnus-define-group-parameter
975  ignored-charsets
976  :type list
977  :function-document
978  "Return the ignored charsets of GROUP."
979  :variable gnus-group-ignored-charsets-alist
980  :variable-default
981  '(("alt\\.chinese\\.text" iso-8859-1))
982  :variable-document
983  "Alist of regexps (to match group names) and charsets that should be ignored.
984 When these charsets are used in the \"charset\" parameter, the
985 default charset will be used instead."
986  :variable-group gnus-charset
987  :variable-type '(repeat (cons (regexp :tag "Group")
988                                (repeat symbol)))
989  :parameter-type '(choice :tag "Ignored charsets"
990                           :value nil
991                           (repeat (symbol)))
992  :parameter-document       "\
993 List of charsets that should be ignored.
994
995 When these charsets are used in the \"charset\" parameter, the
996 default charset will be used instead.")
997
998 (defcustom gnus-group-highlight-words-alist nil
999   "Alist of group regexps and highlight regexps.
1000 This variable uses the same syntax as `gnus-emphasis-alist'."
1001   :version "21.1"
1002   :type '(repeat (cons (regexp :tag "Group")
1003                        (repeat (list (regexp :tag "Highlight regexp")
1004                                      (number :tag "Group for entire word" 0)
1005                                      (number :tag "Group for displayed part" 0)
1006                                      (symbol :tag "Face"
1007                                              gnus-emphasis-highlight-words)))))
1008   :group 'gnus-summary-visual)
1009
1010 (defcustom gnus-use-wheel nil
1011   "Use Intelli-mouse on summary movement"
1012   :type 'boolean
1013   :group 'gnus-summary-maneuvering)
1014
1015 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1016   "Amount to scroll messages by spinning the mouse wheel.
1017 This is actually a cons cell, where the first item is the amount to scroll
1018 on a normal wheel event, and the second is the amount to scroll when the
1019 wheel is moved with the shift key depressed."
1020   :type '(cons (integer :tag "Shift") integer)
1021   :group 'gnus-summary-maneuvering)
1022
1023 (defcustom gnus-wheel-edge-resistance 2
1024   "How hard it should be to change the current article
1025 by moving the mouse over the edge of the article window."
1026   :type 'integer
1027   :group 'gnus-summary-maneuvering)
1028
1029 (defcustom gnus-summary-show-article-charset-alist
1030   nil
1031   "Alist of number and charset.
1032 The article will be shown with the charset corresponding to the
1033 numbered argument.
1034 For example: ((1 . cn-gb-2312) (2 . big5))."
1035   :version "21.1"
1036   :type '(repeat (cons (number :tag "Argument" 1)
1037                        (symbol :tag "Charset")))
1038   :group 'gnus-charset)
1039
1040 (defcustom gnus-preserve-marks t
1041   "Whether marks are preserved when moving, copying and respooling messages."
1042   :version "21.1"
1043   :type 'boolean
1044   :group 'gnus-summary-marks)
1045
1046 (defcustom gnus-alter-articles-to-read-function nil
1047   "Function to be called to alter the list of articles to be selected."
1048   :type '(choice (const nil) function)
1049   :group 'gnus-summary)
1050
1051 (defcustom gnus-orphan-score nil
1052   "*All orphans get this score added.  Set in the score file."
1053   :group 'gnus-score-default
1054   :type '(choice (const nil)
1055                  integer))
1056
1057 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1058   "*A regexp to match MIME parts when saving multiple parts of a message
1059 with gnus-summary-save-parts (X m). This regexp will be used by default
1060 when prompting the user for which type of files to save."
1061   :group 'gnus-summary
1062   :type 'regexp)
1063
1064 (defcustom gnus-read-all-available-headers nil
1065   "Whether Gnus should parse all headers made available to it.
1066 This is mostly relevant for slow backends where the user may
1067 wish to widen the summary buffer to include all headers
1068 that were fetched.  Say, for nnultimate groups."
1069   :group 'gnus-summary
1070   :type '(choice boolean regexp))
1071
1072 (defcustom gnus-summary-muttprint-program "muttprint"
1073   "Command (and optional arguments) used to run Muttprint."
1074   :version "21.3"
1075   :group 'gnus-summary
1076   :type 'string)
1077
1078 (defcustom gnus-article-loose-mime nil
1079   "If non-nil, don't require MIME-Version header.
1080 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1081 supply the MIME-Version header or deliberately strip it From the mail.
1082 Set it to non-nil, Gnus will treat some articles as MIME even if
1083 the MIME-Version header is missed."
1084   :version "21.3"
1085   :type 'boolean
1086   :group 'gnus-article)
1087
1088 ;;; Internal variables
1089
1090 (defvar gnus-summary-display-cache nil)
1091 (defvar gnus-article-mime-handles nil)
1092 (defvar gnus-article-decoded-p nil)
1093 (defvar gnus-article-charset nil)
1094 (defvar gnus-article-ignored-charsets nil)
1095 (defvar gnus-scores-exclude-files nil)
1096 (defvar gnus-page-broken nil)
1097
1098 (defvar gnus-original-article nil)
1099 (defvar gnus-article-internal-prepare-hook nil)
1100 (defvar gnus-newsgroup-process-stack nil)
1101
1102 (defvar gnus-thread-indent-array nil)
1103 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1104 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1105   "Function called to sort the articles within a thread after it has been gathered together.")
1106
1107 (defvar gnus-summary-save-parts-type-history nil)
1108 (defvar gnus-summary-save-parts-last-directory nil)
1109
1110 ;; Avoid highlighting in kill files.
1111 (defvar gnus-summary-inhibit-highlight nil)
1112 (defvar gnus-newsgroup-selected-overlay nil)
1113 (defvar gnus-inhibit-limiting nil)
1114 (defvar gnus-newsgroup-adaptive-score-file nil)
1115 (defvar gnus-current-score-file nil)
1116 (defvar gnus-current-move-group nil)
1117 (defvar gnus-current-copy-group nil)
1118 (defvar gnus-current-crosspost-group nil)
1119 (defvar gnus-newsgroup-display nil)
1120
1121 (defvar gnus-newsgroup-dependencies nil)
1122 (defvar gnus-newsgroup-adaptive nil)
1123 (defvar gnus-summary-display-article-function nil)
1124 (defvar gnus-summary-highlight-line-function nil
1125   "Function called after highlighting a summary line.")
1126
1127 (defvar gnus-summary-line-format-alist
1128   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1129     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1130     (?s gnus-tmp-subject-or-nil ?s)
1131     (?n gnus-tmp-name ?s)
1132     (?A (std11-address-string
1133          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1134     (?a (or (std11-full-name-string
1135              (car (mime-entity-read-field gnus-tmp-header 'From)))
1136             gnus-tmp-from) ?s)
1137     (?F gnus-tmp-from ?s)
1138     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1139     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1140     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1141     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1142     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1143     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1144     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1145     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1146     (?L gnus-tmp-lines ?s)
1147     (?O gnus-tmp-downloaded ?c)
1148     (?I gnus-tmp-indentation ?s)
1149     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1150     (?R gnus-tmp-replied ?c)
1151     (?\[ gnus-tmp-opening-bracket ?c)
1152     (?\] gnus-tmp-closing-bracket ?c)
1153     (?\> (make-string gnus-tmp-level ? ) ?s)
1154     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1155     (?i gnus-tmp-score ?d)
1156     (?z gnus-tmp-score-char ?c)
1157     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1158     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1159     (?U gnus-tmp-unread ?c)
1160     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1161         ?s)
1162     (?t (gnus-summary-number-of-articles-in-thread
1163          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1164         ?d)
1165     (?e (gnus-summary-number-of-articles-in-thread
1166          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1167         ?c)
1168     (?u gnus-tmp-user-defined ?s)
1169     (?P (gnus-pick-line-number) ?d)
1170     (?B gnus-tmp-thread-tree-header-string ?s)
1171     (user-date (gnus-user-date
1172                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1173   "An alist of format specifications that can appear in summary lines.
1174 These are paired with what variables they correspond with, along with
1175 the type of the variable (string, integer, character, etc).")
1176
1177 (defvar gnus-summary-dummy-line-format-alist
1178   `((?S gnus-tmp-subject ?s)
1179     (?N gnus-tmp-number ?d)
1180     (?u gnus-tmp-user-defined ?s)))
1181
1182 (defvar gnus-summary-mode-line-format-alist
1183   `((?G gnus-tmp-group-name ?s)
1184     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1185     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1186     (?A gnus-tmp-article-number ?d)
1187     (?Z gnus-tmp-unread-and-unselected ?s)
1188     (?V gnus-version ?s)
1189     (?U gnus-tmp-unread-and-unticked ?d)
1190     (?S gnus-tmp-subject ?s)
1191     (?e gnus-tmp-unselected ?d)
1192     (?u gnus-tmp-user-defined ?s)
1193     (?d (length gnus-newsgroup-dormant) ?d)
1194     (?t (length gnus-newsgroup-marked) ?d)
1195     (?h (length gnus-newsgroup-spam-marked) ?d)
1196     (?r (length gnus-newsgroup-reads) ?d)
1197     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1198     (?E gnus-newsgroup-expunged-tally ?d)
1199     (?s (gnus-current-score-file-nondirectory) ?s)))
1200
1201 (defvar gnus-last-search-regexp nil
1202   "Default regexp for article search command.")
1203
1204 (defvar gnus-summary-search-article-matched-data nil
1205   "Last matched data of article search command.  It is the local variable
1206 in `gnus-article-buffer' which consists of the list of start position,
1207 end position and text.")
1208
1209 (defvar gnus-last-shell-command nil
1210   "Default shell command on article.")
1211
1212 (defvar gnus-newsgroup-agentized nil
1213   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1214 (defvar gnus-newsgroup-begin nil)
1215 (defvar gnus-newsgroup-end nil)
1216 (defvar gnus-newsgroup-last-rmail nil)
1217 (defvar gnus-newsgroup-last-mail nil)
1218 (defvar gnus-newsgroup-last-folder nil)
1219 (defvar gnus-newsgroup-last-file nil)
1220 (defvar gnus-newsgroup-auto-expire nil)
1221 (defvar gnus-newsgroup-active nil)
1222
1223 (defvar gnus-newsgroup-data nil)
1224 (defvar gnus-newsgroup-data-reverse nil)
1225 (defvar gnus-newsgroup-limit nil)
1226 (defvar gnus-newsgroup-limits nil)
1227
1228 (defvar gnus-newsgroup-unreads nil
1229   "Sorted list of unread articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-unselected nil
1232   "Sorted list of unselected unread articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-reads nil
1235   "Alist of read articles and article marks in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-expunged-tally nil)
1238
1239 (defvar gnus-newsgroup-marked nil
1240   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1241
1242 (defvar gnus-newsgroup-spam-marked nil
1243   "List of ranges of articles that have been marked as spam.")
1244
1245 (defvar gnus-newsgroup-killed nil
1246   "List of ranges of articles that have been through the scoring process.")
1247
1248 (defvar gnus-newsgroup-cached nil
1249   "Sorted list of articles that come from the article cache.")
1250
1251 (defvar gnus-newsgroup-saved nil
1252   "List of articles that have been saved.")
1253
1254 (defvar gnus-newsgroup-kill-headers nil)
1255
1256 (defvar gnus-newsgroup-replied nil
1257   "List of articles that have been replied to in the current newsgroup.")
1258
1259 (defvar gnus-newsgroup-forwarded nil
1260   "List of articles that have been forwarded in the current newsgroup.")
1261
1262 (defvar gnus-newsgroup-recent nil
1263   "List of articles that have are recent in the current newsgroup.")
1264
1265 (defvar gnus-newsgroup-expirable nil
1266   "Sorted list of articles in the current newsgroup that can be expired.")
1267
1268 (defvar gnus-newsgroup-processable nil
1269   "List of articles in the current newsgroup that can be processed.")
1270
1271 (defvar gnus-newsgroup-downloadable nil
1272   "Sorted list of articles in the current newsgroup that can be processed.")
1273
1274 (defvar gnus-newsgroup-undownloaded nil
1275   "List of articles in the current newsgroup that haven't been downloaded..")
1276
1277 (defvar gnus-newsgroup-unsendable nil
1278   "List of articles in the current newsgroup that won't be sent.")
1279
1280 (defvar gnus-newsgroup-bookmarks nil
1281   "List of articles in the current newsgroup that have bookmarks.")
1282
1283 (defvar gnus-newsgroup-dormant nil
1284   "Sorted list of dormant articles in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-unseen nil
1287   "List of unseen articles in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-seen nil
1290   "Range of seen articles in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-articles nil
1293   "List of articles in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-scored nil
1296   "List of scored articles in the current newsgroup.")
1297
1298 (defvar gnus-newsgroup-incorporated nil
1299   "List of incorporated articles in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-headers nil
1302   "List of article headers in the current newsgroup.")
1303
1304 (defvar gnus-newsgroup-threads nil)
1305
1306 (defvar gnus-newsgroup-prepared nil
1307   "Whether the current group has been prepared properly.")
1308
1309 (defvar gnus-newsgroup-ancient nil
1310   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-sparse nil)
1313
1314 (defvar gnus-current-article nil)
1315 (defvar gnus-article-current nil)
1316 (defvar gnus-current-headers nil)
1317 (defvar gnus-have-all-headers nil)
1318 (defvar gnus-last-article nil)
1319 (defvar gnus-newsgroup-history nil)
1320 (defvar gnus-newsgroup-charset nil)
1321 (defvar gnus-newsgroup-ephemeral-charset nil)
1322 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1323
1324 (defvar gnus-article-before-search nil)
1325
1326 (defconst gnus-summary-local-variables
1327   '(gnus-newsgroup-name
1328     gnus-newsgroup-begin gnus-newsgroup-end
1329     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1330     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1331     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1332     gnus-newsgroup-unselected gnus-newsgroup-marked
1333     gnus-newsgroup-spam-marked
1334     gnus-newsgroup-reads gnus-newsgroup-saved
1335     gnus-newsgroup-replied gnus-newsgroup-forwarded
1336     gnus-newsgroup-recent
1337     gnus-newsgroup-expirable
1338     gnus-newsgroup-processable gnus-newsgroup-killed
1339     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1340     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1341     gnus-newsgroup-seen gnus-newsgroup-articles
1342     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1343     gnus-newsgroup-headers gnus-newsgroup-threads
1344     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1345     gnus-current-article gnus-current-headers gnus-have-all-headers
1346     gnus-last-article gnus-article-internal-prepare-hook
1347     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1348     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1349     gnus-thread-expunge-below
1350     gnus-score-alist gnus-current-score-file
1351     (gnus-summary-expunge-below . global)
1352     (gnus-summary-mark-below . global)
1353     (gnus-orphan-score . global)
1354     gnus-newsgroup-active gnus-scores-exclude-files
1355     gnus-newsgroup-history gnus-newsgroup-ancient
1356     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1357     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1358     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1359     (gnus-newsgroup-expunged-tally . 0)
1360     gnus-cache-removable-articles gnus-newsgroup-cached
1361     gnus-newsgroup-data gnus-newsgroup-data-reverse
1362     gnus-newsgroup-limit gnus-newsgroup-limits
1363     gnus-newsgroup-charset gnus-newsgroup-display
1364     gnus-newsgroup-incorporated)
1365   "Variables that are buffer-local to the summary buffers.")
1366
1367 (defvar gnus-newsgroup-variables nil
1368   "A list of variables that have separate values in different newsgroups.
1369 A list of newsgroup (summary buffer) local variables, or cons of
1370 variables and their default values (when the default values are not
1371 nil), that should be made global while the summary buffer is active.
1372 These variables can be used to set variables in the group parameters
1373 while still allowing them to affect operations done in other
1374 buffers. For example:
1375
1376 \(setq gnus-newsgroup-variables
1377      '(message-use-followup-to
1378        (gnus-visible-headers .
1379          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1380 ")
1381
1382 ;; Byte-compiler warning.
1383 (eval-when-compile (defvar gnus-article-mode-map))
1384
1385 ;; Subject simplification.
1386
1387 (defun gnus-simplify-whitespace (str)
1388   "Remove excessive whitespace from STR."
1389   (let ((mystr str))
1390     ;; Multiple spaces.
1391     (while (string-match "[ \t][ \t]+" mystr)
1392       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1393                           " "
1394                           (substring mystr (match-end 0)))))
1395     ;; Leading spaces.
1396     (when (string-match "^[ \t]+" mystr)
1397       (setq mystr (substring mystr (match-end 0))))
1398     ;; Trailing spaces.
1399     (when (string-match "[ \t]+$" mystr)
1400       (setq mystr (substring mystr 0 (match-beginning 0))))
1401     mystr))
1402
1403 (defun gnus-simplify-all-whitespace (str)
1404   "Remove all whitespace from STR."
1405   (let ((mystr str))
1406     (while (string-match "[ \t\n]+" mystr)
1407       (setq mystr (replace-match "" nil nil mystr)))
1408     mystr))
1409
1410 (defsubst gnus-simplify-subject-re (subject)
1411   "Remove \"Re:\" from subject lines."
1412   (if (string-match message-subject-re-regexp subject)
1413       (substring subject (match-end 0))
1414     subject))
1415
1416 (defun gnus-simplify-subject (subject &optional re-only)
1417   "Remove `Re:' and words in parentheses.
1418 If RE-ONLY is non-nil, strip leading `Re:'s only."
1419   (let ((case-fold-search t))           ;Ignore case.
1420     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1421     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1422       (setq subject (substring subject (match-end 0))))
1423     ;; Remove uninteresting prefixes.
1424     (when (and (not re-only)
1425                gnus-simplify-ignored-prefixes
1426                (string-match gnus-simplify-ignored-prefixes subject))
1427       (setq subject (substring subject (match-end 0))))
1428     ;; Remove words in parentheses from end.
1429     (unless re-only
1430       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1431         (setq subject (substring subject 0 (match-beginning 0)))))
1432     ;; Return subject string.
1433     subject))
1434
1435 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1436 ;; all whitespace.
1437 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1438   (goto-char (point-min))
1439   (while (re-search-forward regexp nil t)
1440     (replace-match (or newtext ""))))
1441
1442 (defun gnus-simplify-buffer-fuzzy ()
1443   "Simplify string in the buffer fuzzily.
1444 The string in the accessible portion of the current buffer is simplified.
1445 It is assumed to be a single-line subject.
1446 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1447 matter is removed.  Additional things can be deleted by setting
1448 `gnus-simplify-subject-fuzzy-regexp'."
1449   (let ((case-fold-search t)
1450         (modified-tick))
1451     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1452
1453     (while (not (eq modified-tick (buffer-modified-tick)))
1454       (setq modified-tick (buffer-modified-tick))
1455       (cond
1456        ((listp gnus-simplify-subject-fuzzy-regexp)
1457         (mapcar 'gnus-simplify-buffer-fuzzy-step
1458                 gnus-simplify-subject-fuzzy-regexp))
1459        (gnus-simplify-subject-fuzzy-regexp
1460         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1461       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1462       (gnus-simplify-buffer-fuzzy-step
1463        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1464       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1465
1466     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1467     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1468     (gnus-simplify-buffer-fuzzy-step " $")
1469     (gnus-simplify-buffer-fuzzy-step "^ +")))
1470
1471 (defun gnus-simplify-subject-fuzzy (subject)
1472   "Simplify a subject string fuzzily.
1473 See `gnus-simplify-buffer-fuzzy' for details."
1474   (save-excursion
1475     (gnus-set-work-buffer)
1476     (let ((case-fold-search t))
1477       ;; Remove uninteresting prefixes.
1478       (when (and gnus-simplify-ignored-prefixes
1479                  (string-match gnus-simplify-ignored-prefixes subject))
1480         (setq subject (substring subject (match-end 0))))
1481       (insert subject)
1482       (inline (gnus-simplify-buffer-fuzzy))
1483       (buffer-string))))
1484
1485 (defsubst gnus-simplify-subject-fully (subject)
1486   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1487   (cond
1488    (gnus-simplify-subject-functions
1489     (gnus-map-function gnus-simplify-subject-functions subject))
1490    ((null gnus-summary-gather-subject-limit)
1491     (gnus-simplify-subject-re subject))
1492    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1493     (gnus-simplify-subject-fuzzy subject))
1494    ((numberp gnus-summary-gather-subject-limit)
1495     (gnus-limit-string (gnus-simplify-subject-re subject)
1496                        gnus-summary-gather-subject-limit))
1497    (t
1498     subject)))
1499
1500 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1501   "Check whether two subjects are equal.
1502 If optional argument simple-first is t, first argument is already
1503 simplified."
1504   (cond
1505    ((null simple-first)
1506     (equal (gnus-simplify-subject-fully s1)
1507            (gnus-simplify-subject-fully s2)))
1508    (t
1509     (equal s1
1510            (gnus-simplify-subject-fully s2)))))
1511
1512 (defun gnus-summary-bubble-group ()
1513   "Increase the score of the current group.
1514 This is a handy function to add to `gnus-summary-exit-hook' to
1515 increase the score of each group you read."
1516   (gnus-group-add-score gnus-newsgroup-name))
1517
1518 \f
1519 ;;;
1520 ;;; Gnus summary mode
1521 ;;;
1522
1523 (put 'gnus-summary-mode 'mode-class 'special)
1524
1525 (defvar gnus-article-commands-menu)
1526
1527 (when t
1528   ;; Non-orthogonal keys
1529
1530   (gnus-define-keys gnus-summary-mode-map
1531     " " gnus-summary-next-page
1532     "\177" gnus-summary-prev-page
1533     [delete] gnus-summary-prev-page
1534     [backspace] gnus-summary-prev-page
1535     "\r" gnus-summary-scroll-up
1536     "\M-\r" gnus-summary-scroll-down
1537     "n" gnus-summary-next-unread-article
1538     "p" gnus-summary-prev-unread-article
1539     "N" gnus-summary-next-article
1540     "P" gnus-summary-prev-article
1541     "\M-\C-n" gnus-summary-next-same-subject
1542     "\M-\C-p" gnus-summary-prev-same-subject
1543     "\M-n" gnus-summary-next-unread-subject
1544     "\M-p" gnus-summary-prev-unread-subject
1545     "." gnus-summary-first-unread-article
1546     "," gnus-summary-best-unread-article
1547     "\M-s" gnus-summary-search-article-forward
1548     "\M-r" gnus-summary-search-article-backward
1549     "<" gnus-summary-beginning-of-article
1550     ">" gnus-summary-end-of-article
1551     "j" gnus-summary-goto-article
1552     "^" gnus-summary-refer-parent-article
1553     "\M-^" gnus-summary-refer-article
1554     "u" gnus-summary-tick-article-forward
1555     "!" gnus-summary-tick-article-forward
1556     "U" gnus-summary-tick-article-backward
1557     "d" gnus-summary-mark-as-read-forward
1558     "D" gnus-summary-mark-as-read-backward
1559     "E" gnus-summary-mark-as-expirable
1560     "\M-u" gnus-summary-clear-mark-forward
1561     "\M-U" gnus-summary-clear-mark-backward
1562     "k" gnus-summary-kill-same-subject-and-select
1563     "\C-k" gnus-summary-kill-same-subject
1564     "\M-\C-k" gnus-summary-kill-thread
1565     "\M-\C-l" gnus-summary-lower-thread
1566     "e" gnus-summary-edit-article
1567     "#" gnus-summary-mark-as-processable
1568     "\M-#" gnus-summary-unmark-as-processable
1569     "\M-\C-t" gnus-summary-toggle-threads
1570     "\M-\C-s" gnus-summary-show-thread
1571     "\M-\C-h" gnus-summary-hide-thread
1572     "\M-\C-f" gnus-summary-next-thread
1573     "\M-\C-b" gnus-summary-prev-thread
1574     [(meta down)] gnus-summary-next-thread
1575     [(meta up)] gnus-summary-prev-thread
1576     "\M-\C-u" gnus-summary-up-thread
1577     "\M-\C-d" gnus-summary-down-thread
1578     "&" gnus-summary-execute-command
1579     "c" gnus-summary-catchup-and-exit
1580     "\C-w" gnus-summary-mark-region-as-read
1581     "\C-t" gnus-summary-toggle-truncation
1582     "?" gnus-summary-mark-as-dormant
1583     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1584     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1585     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1586     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1587     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1588     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1589     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1590     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1591     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1592     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1593     "=" gnus-summary-expand-window
1594     "\C-x\C-s" gnus-summary-reselect-current-group
1595     "\M-g" gnus-summary-rescan-group
1596     "w" gnus-summary-stop-page-breaking
1597     "\C-c\C-r" gnus-summary-caesar-message
1598     "\M-t" gnus-summary-toggle-mime
1599     "f" gnus-summary-followup
1600     "F" gnus-summary-followup-with-original
1601     "C" gnus-summary-cancel-article
1602     "r" gnus-summary-reply
1603     "R" gnus-summary-reply-with-original
1604     "\C-c\C-f" gnus-summary-mail-forward
1605     "o" gnus-summary-save-article
1606     "\C-o" gnus-summary-save-article-mail
1607     "|" gnus-summary-pipe-output
1608     "\M-k" gnus-summary-edit-local-kill
1609     "\M-K" gnus-summary-edit-global-kill
1610     ;; "V" gnus-version
1611     "\C-c\C-d" gnus-summary-describe-group
1612     "q" gnus-summary-exit
1613     "Q" gnus-summary-exit-no-update
1614     "\C-c\C-i" gnus-info-find-node
1615     gnus-mouse-2 gnus-mouse-pick-article
1616     "m" gnus-summary-mail-other-window
1617     "a" gnus-summary-post-news
1618     "i" gnus-summary-news-other-window
1619     "x" gnus-summary-limit-to-unread
1620     "s" gnus-summary-isearch-article
1621     "t" gnus-summary-toggle-header
1622     "g" gnus-summary-show-article
1623     "l" gnus-summary-goto-last-article
1624     "v" gnus-summary-preview-mime-message
1625     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1626     "\C-d" gnus-summary-enter-digest-group
1627     "\M-\C-d" gnus-summary-read-document
1628     "\M-\C-e" gnus-summary-edit-parameters
1629     "\M-\C-a" gnus-summary-customize-parameters
1630     "\C-c\C-b" gnus-bug
1631     "\C-c\C-n" gnus-namazu-search
1632     "*" gnus-cache-enter-article
1633     "\M-*" gnus-cache-remove-article
1634     "\M-&" gnus-summary-universal-argument
1635     "\C-l" gnus-recenter
1636     "I" gnus-summary-increase-score
1637     "L" gnus-summary-lower-score
1638     "\M-i" gnus-symbolic-argument
1639     "h" gnus-summary-select-article-buffer
1640
1641     "V" gnus-summary-score-map
1642     "X" gnus-uu-extract-map
1643     "S" gnus-summary-send-map)
1644
1645   ;; Sort of orthogonal keymap
1646   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1647     "t" gnus-summary-tick-article-forward
1648     "!" gnus-summary-tick-article-forward
1649     "d" gnus-summary-mark-as-read-forward
1650     "r" gnus-summary-mark-as-read-forward
1651     "c" gnus-summary-clear-mark-forward
1652     " " gnus-summary-clear-mark-forward
1653     "e" gnus-summary-mark-as-expirable
1654     "x" gnus-summary-mark-as-expirable
1655     "?" gnus-summary-mark-as-dormant
1656     "b" gnus-summary-set-bookmark
1657     "B" gnus-summary-remove-bookmark
1658     "#" gnus-summary-mark-as-processable
1659     "\M-#" gnus-summary-unmark-as-processable
1660     "S" gnus-summary-limit-include-expunged
1661     "C" gnus-summary-catchup
1662     "H" gnus-summary-catchup-to-here
1663     "h" gnus-summary-catchup-from-here
1664     "\C-c" gnus-summary-catchup-all
1665     "k" gnus-summary-kill-same-subject-and-select
1666     "K" gnus-summary-kill-same-subject
1667     "P" gnus-uu-mark-map)
1668
1669   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1670     "c" gnus-summary-clear-above
1671     "u" gnus-summary-tick-above
1672     "m" gnus-summary-mark-above
1673     "k" gnus-summary-kill-below)
1674
1675   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1676     "/" gnus-summary-limit-to-subject
1677     "n" gnus-summary-limit-to-articles
1678     "w" gnus-summary-pop-limit
1679     "s" gnus-summary-limit-to-subject
1680     "a" gnus-summary-limit-to-author
1681     "u" gnus-summary-limit-to-unread
1682     "m" gnus-summary-limit-to-marks
1683     "M" gnus-summary-limit-exclude-marks
1684     "v" gnus-summary-limit-to-score
1685     "*" gnus-summary-limit-include-cached
1686     "D" gnus-summary-limit-include-dormant
1687     "T" gnus-summary-limit-include-thread
1688     "d" gnus-summary-limit-exclude-dormant
1689     "t" gnus-summary-limit-to-age
1690     "." gnus-summary-limit-to-unseen
1691     "x" gnus-summary-limit-to-extra
1692     "p" gnus-summary-limit-to-display-predicate
1693     "E" gnus-summary-limit-include-expunged
1694     "c" gnus-summary-limit-exclude-childless-dormant
1695     "C" gnus-summary-limit-mark-excluded-as-read
1696     "o" gnus-summary-insert-old-articles
1697     "N" gnus-summary-insert-new-articles)
1698
1699   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1700     "n" gnus-summary-next-unread-article
1701     "p" gnus-summary-prev-unread-article
1702     "N" gnus-summary-next-article
1703     "P" gnus-summary-prev-article
1704     "\C-n" gnus-summary-next-same-subject
1705     "\C-p" gnus-summary-prev-same-subject
1706     "\M-n" gnus-summary-next-unread-subject
1707     "\M-p" gnus-summary-prev-unread-subject
1708     "f" gnus-summary-first-unread-article
1709     "b" gnus-summary-best-unread-article
1710     "j" gnus-summary-goto-article
1711     "g" gnus-summary-goto-subject
1712     "l" gnus-summary-goto-last-article
1713     "o" gnus-summary-pop-article)
1714
1715   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1716     "k" gnus-summary-kill-thread
1717     "l" gnus-summary-lower-thread
1718     "i" gnus-summary-raise-thread
1719     "T" gnus-summary-toggle-threads
1720     "t" gnus-summary-rethread-current
1721     "^" gnus-summary-reparent-thread
1722     "s" gnus-summary-show-thread
1723     "S" gnus-summary-show-all-threads
1724     "h" gnus-summary-hide-thread
1725     "H" gnus-summary-hide-all-threads
1726     "n" gnus-summary-next-thread
1727     "p" gnus-summary-prev-thread
1728     "u" gnus-summary-up-thread
1729     "o" gnus-summary-top-thread
1730     "d" gnus-summary-down-thread
1731     "#" gnus-uu-mark-thread
1732     "\M-#" gnus-uu-unmark-thread)
1733
1734   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1735     "g" gnus-summary-prepare
1736     "c" gnus-summary-insert-cached-articles
1737     "d" gnus-summary-insert-dormant-articles)
1738
1739   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1740     "c" gnus-summary-catchup-and-exit
1741     "C" gnus-summary-catchup-all-and-exit
1742     "E" gnus-summary-exit-no-update
1743     "J" gnus-summary-jump-to-other-group
1744     "Q" gnus-summary-exit
1745     "Z" gnus-summary-exit
1746     "n" gnus-summary-catchup-and-goto-next-group
1747     "R" gnus-summary-reselect-current-group
1748     "G" gnus-summary-rescan-group
1749     "N" gnus-summary-next-group
1750     "s" gnus-summary-save-newsrc
1751     "P" gnus-summary-prev-group)
1752
1753   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1754     " " gnus-summary-next-page
1755     "n" gnus-summary-next-page
1756     "\177" gnus-summary-prev-page
1757     [delete] gnus-summary-prev-page
1758     "p" gnus-summary-prev-page
1759     "\r" gnus-summary-scroll-up
1760     "\M-\r" gnus-summary-scroll-down
1761     "<" gnus-summary-beginning-of-article
1762     ">" gnus-summary-end-of-article
1763     "b" gnus-summary-beginning-of-article
1764     "e" gnus-summary-end-of-article
1765     "^" gnus-summary-refer-parent-article
1766     "r" gnus-summary-refer-parent-article
1767     "D" gnus-summary-enter-digest-group
1768     "R" gnus-summary-refer-references
1769     "T" gnus-summary-refer-thread
1770     "g" gnus-summary-show-article
1771     "s" gnus-summary-isearch-article
1772     "P" gnus-summary-print-article
1773     "M" gnus-mailing-list-insinuate
1774     "t" gnus-article-babel)
1775
1776   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1777     "b" gnus-article-add-buttons
1778     "B" gnus-article-add-buttons-to-head
1779     "o" gnus-article-treat-overstrike
1780     "e" gnus-article-emphasize
1781     "w" gnus-article-fill-cited-article
1782     "Q" gnus-article-fill-long-lines
1783     "C" gnus-article-capitalize-sentences
1784     "c" gnus-article-remove-cr
1785     "Z" gnus-article-decode-HZ
1786     "h" gnus-article-wash-html
1787     "u" gnus-article-unsplit-urls
1788     "f" gnus-article-display-x-face
1789     "l" gnus-summary-stop-page-breaking
1790     "r" gnus-summary-caesar-message
1791     "m" gnus-summary-morse-message
1792     "t" gnus-summary-toggle-header
1793     "g" gnus-treat-smiley
1794     "v" gnus-summary-verbose-headers
1795     "m" gnus-summary-toggle-mime
1796     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1797     "p" gnus-article-verify-x-pgp-sig
1798     "d" gnus-article-treat-dumbquotes
1799     "k" gnus-article-outlook-deuglify-article)
1800
1801   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1802     "a" gnus-article-hide
1803     "h" gnus-article-hide-headers
1804     "b" gnus-article-hide-boring-headers
1805     "s" gnus-article-hide-signature
1806     "c" gnus-article-hide-citation
1807     "C" gnus-article-hide-citation-in-followups
1808     "l" gnus-article-hide-list-identifiers
1809     "p" gnus-article-hide-pgp
1810     "B" gnus-article-strip-banner
1811     "P" gnus-article-hide-pem
1812     "\C-c" gnus-article-hide-citation-maybe)
1813
1814   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1815     "a" gnus-article-highlight
1816     "h" gnus-article-highlight-headers
1817     "c" gnus-article-highlight-citation
1818     "s" gnus-article-highlight-signature)
1819
1820   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1821     "f" gnus-article-treat-fold-headers
1822     "u" gnus-article-treat-unfold-headers
1823     "n" gnus-article-treat-fold-newsgroups)
1824
1825   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1826     "x" gnus-article-display-x-face
1827     "s" gnus-treat-smiley
1828     "D" gnus-article-remove-images
1829     "f" gnus-treat-from-picon
1830     "m" gnus-treat-mail-picon
1831     "n" gnus-treat-newsgroups-picon)
1832
1833   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1834     "z" gnus-article-date-ut
1835     "u" gnus-article-date-ut
1836     "l" gnus-article-date-local
1837     "p" gnus-article-date-english
1838     "e" gnus-article-date-lapsed
1839     "o" gnus-article-date-original
1840     "i" gnus-article-date-iso8601
1841     "s" gnus-article-date-user)
1842
1843   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1844     "t" gnus-article-remove-trailing-blank-lines
1845     "l" gnus-article-strip-leading-blank-lines
1846     "m" gnus-article-strip-multiple-blank-lines
1847     "a" gnus-article-strip-blank-lines
1848     "A" gnus-article-strip-all-blank-lines
1849     "s" gnus-article-strip-leading-space
1850     "e" gnus-article-strip-trailing-space
1851     "w" gnus-article-remove-leading-whitespace)
1852
1853   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1854     "v" gnus-version
1855     "f" gnus-summary-fetch-faq
1856     "d" gnus-summary-describe-group
1857     "h" gnus-summary-describe-briefly
1858     "i" gnus-info-find-node
1859     "c" gnus-group-fetch-charter
1860     "C" gnus-group-fetch-control)
1861
1862   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1863     "e" gnus-summary-expire-articles
1864     "\M-\C-e" gnus-summary-expire-articles-now
1865     "\177" gnus-summary-delete-article
1866     [delete] gnus-summary-delete-article
1867     [backspace] gnus-summary-delete-article
1868     "m" gnus-summary-move-article
1869     "r" gnus-summary-respool-article
1870     "w" gnus-summary-edit-article
1871     "c" gnus-summary-copy-article
1872     "B" gnus-summary-crosspost-article
1873     "q" gnus-summary-respool-query
1874     "t" gnus-summary-respool-trace
1875     "i" gnus-summary-import-article
1876     "I" gnus-summary-create-article
1877     "p" gnus-summary-article-posted-p)
1878
1879   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1880     "o" gnus-summary-save-article
1881     "m" gnus-summary-save-article-mail
1882     "F" gnus-summary-write-article-file
1883     "r" gnus-summary-save-article-rmail
1884     "f" gnus-summary-save-article-file
1885     "b" gnus-summary-save-article-body-file
1886     "h" gnus-summary-save-article-folder
1887     "v" gnus-summary-save-article-vm
1888     "p" gnus-summary-pipe-output
1889     "P" gnus-summary-muttprint
1890     "s" gnus-soup-add-article)
1891
1892   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1893     "b" gnus-summary-display-buttonized
1894     "m" gnus-summary-repair-multipart
1895     "v" gnus-article-view-part
1896     "o" gnus-article-save-part
1897     "c" gnus-article-copy-part
1898     "C" gnus-article-view-part-as-charset
1899     "e" gnus-article-view-part-externally
1900     "E" gnus-article-encrypt-body
1901     "i" gnus-article-inline-part
1902     "|" gnus-article-pipe-part)
1903
1904   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1905     "p" gnus-summary-mark-as-processable
1906     "u" gnus-summary-unmark-as-processable
1907     "U" gnus-summary-unmark-all-processable
1908     "v" gnus-uu-mark-over
1909     "s" gnus-uu-mark-series
1910     "r" gnus-uu-mark-region
1911     "g" gnus-uu-unmark-region
1912     "R" gnus-uu-mark-by-regexp
1913     "G" gnus-uu-unmark-by-regexp
1914     "t" gnus-uu-mark-thread
1915     "T" gnus-uu-unmark-thread
1916     "a" gnus-uu-mark-all
1917     "b" gnus-uu-mark-buffer
1918     "S" gnus-uu-mark-sparse
1919     "k" gnus-summary-kill-process-mark
1920     "y" gnus-summary-yank-process-mark
1921     "w" gnus-summary-save-process-mark
1922     "i" gnus-uu-invert-processable)
1923
1924   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1925     ;;"x" gnus-uu-extract-any
1926     "m" gnus-summary-save-parts
1927     "u" gnus-uu-decode-uu
1928     "U" gnus-uu-decode-uu-and-save
1929     "s" gnus-uu-decode-unshar
1930     "S" gnus-uu-decode-unshar-and-save
1931     "o" gnus-uu-decode-save
1932     "O" gnus-uu-decode-save
1933     "b" gnus-uu-decode-binhex
1934     "B" gnus-uu-decode-binhex
1935     "p" gnus-uu-decode-postscript
1936     "P" gnus-uu-decode-postscript-and-save)
1937
1938   (gnus-define-keys
1939       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1940     "u" gnus-uu-decode-uu-view
1941     "U" gnus-uu-decode-uu-and-save-view
1942     "s" gnus-uu-decode-unshar-view
1943     "S" gnus-uu-decode-unshar-and-save-view
1944     "o" gnus-uu-decode-save-view
1945     "O" gnus-uu-decode-save-view
1946     "b" gnus-uu-decode-binhex-view
1947     "B" gnus-uu-decode-binhex-view
1948     "p" gnus-uu-decode-postscript-view
1949     "P" gnus-uu-decode-postscript-and-save-view))
1950
1951 (defvar gnus-article-post-menu nil)
1952
1953 (defconst gnus-summary-menu-maxlen 20)
1954
1955 (defun gnus-summary-menu-split (menu)
1956   ;; If we have lots of elements, divide them into groups of 20
1957   ;; and make a pane (or submenu) for each one.
1958   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1959       (let ((menu menu) sublists next
1960             (i 1))
1961         (while menu
1962           ;; Pull off the next gnus-summary-menu-maxlen elements
1963           ;; and make them the next element of sublist.
1964           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1965           (if next
1966               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1967                       nil))
1968           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1969                                              (aref (car (last menu)) 0)) menu)
1970                                sublists))
1971           (setq i (1+ i))
1972           (setq menu next))
1973         (nreverse sublists))
1974     ;; Few elements--put them all in one pane.
1975     menu))
1976
1977 (defun gnus-summary-make-menu-bar ()
1978   (gnus-turn-off-edit-menu 'summary)
1979
1980   (unless (boundp 'gnus-summary-misc-menu)
1981
1982     (easy-menu-define
1983      gnus-summary-kill-menu gnus-summary-mode-map ""
1984      (cons
1985       "Score"
1986       (nconc
1987        (list
1988         ["Customize" gnus-score-customize t])
1989        (gnus-make-score-map 'increase)
1990        (gnus-make-score-map 'lower)
1991        '(("Mark"
1992           ["Kill below" gnus-summary-kill-below t]
1993           ["Mark above" gnus-summary-mark-above t]
1994           ["Tick above" gnus-summary-tick-above t]
1995           ["Clear above" gnus-summary-clear-above t])
1996          ["Current score" gnus-summary-current-score t]
1997          ["Set score" gnus-summary-set-score t]
1998          ["Switch current score file..." gnus-score-change-score-file t]
1999          ["Set mark below..." gnus-score-set-mark-below t]
2000          ["Set expunge below..." gnus-score-set-expunge-below t]
2001          ["Edit current score file" gnus-score-edit-current-scores t]
2002          ["Edit score file" gnus-score-edit-file t]
2003          ["Trace score" gnus-score-find-trace t]
2004          ["Find words" gnus-score-find-favourite-words t]
2005          ["Rescore buffer" gnus-summary-rescore t]
2006          ["Increase score..." gnus-summary-increase-score t]
2007          ["Lower score..." gnus-summary-lower-score t]))))
2008
2009     ;; Define both the Article menu in the summary buffer and the
2010     ;; equivalent Commands menu in the article buffer here for
2011     ;; consistency.
2012     (let ((innards
2013            `(("Hide"
2014               ["All" gnus-article-hide t]
2015               ["Headers" gnus-article-hide-headers t]
2016               ["Signature" gnus-article-hide-signature t]
2017               ["Citation" gnus-article-hide-citation t]
2018               ["List identifiers" gnus-article-hide-list-identifiers t]
2019               ["PGP" gnus-article-hide-pgp t]
2020               ["Banner" gnus-article-strip-banner t]
2021               ["Boring headers" gnus-article-hide-boring-headers t])
2022              ("Highlight"
2023               ["All" gnus-article-highlight t]
2024               ["Headers" gnus-article-highlight-headers t]
2025               ["Signature" gnus-article-highlight-signature t]
2026               ["Citation" gnus-article-highlight-citation t])
2027              ("Date"
2028               ["Local" gnus-article-date-local t]
2029               ["ISO8601" gnus-article-date-iso8601 t]
2030               ["UT" gnus-article-date-ut t]
2031               ["Original" gnus-article-date-original t]
2032               ["Lapsed" gnus-article-date-lapsed t]
2033               ["User-defined" gnus-article-date-user t])
2034              ("Display"
2035               ["Remove images" gnus-article-remove-images t]
2036               ["Toggle smiley" gnus-treat-smiley t]
2037               ["Show X-Face" gnus-article-display-x-face t]
2038               ["Show picons in From" gnus-treat-from-picon t]
2039               ["Show picons in mail headers" gnus-treat-mail-picon t]
2040               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2041               ("View as different encoding"
2042                ,@(gnus-summary-menu-split
2043                   (mapcar
2044                    (lambda (cs)
2045                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2046                      ;; forms for menu commands, we should provide intern'ed
2047                      ;; function symbols.
2048                      (let ((command (intern (format "\
2049 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2050                        (fset command
2051                              `(lambda ()
2052                                 (interactive)
2053                                 (let ((gnus-summary-show-article-charset-alist
2054                                        '((1 . ,cs))))
2055                                   (gnus-summary-show-article 1))))
2056                        `[,(symbol-name cs) ,command t]))
2057                    (sort (if (fboundp 'coding-system-list)
2058                              (coding-system-list)
2059                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2060                            )
2061                          'string<)))))
2062              ("Washing"
2063               ("Remove Blanks"
2064                ["Leading" gnus-article-strip-leading-blank-lines t]
2065                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2066                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2067                ["All of the above" gnus-article-strip-blank-lines t]
2068                ["All" gnus-article-strip-all-blank-lines t]
2069                ["Leading space" gnus-article-strip-leading-space t]
2070                ["Trailing space" gnus-article-strip-trailing-space t]
2071                ["Leading space in headers"
2072                 gnus-article-remove-leading-whitespace t])
2073               ["Overstrike" gnus-article-treat-overstrike t]
2074               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2075               ["Emphasis" gnus-article-emphasize t]
2076               ["Word wrap" gnus-article-fill-cited-article t]
2077               ["Fill long lines" gnus-article-fill-long-lines t]
2078               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2079               ["CR" gnus-article-remove-cr t]
2080               ["Rot 13" gnus-summary-caesar-message
2081                ,@(if (featurep 'xemacs) '(t)
2082                    '(:help "\"Caesar rotate\" article by 13"))]
2083               ["Morse decode" gnus-summary-morse-message t]
2084               ["Unix pipe..." gnus-summary-pipe-message t]
2085               ["Add buttons" gnus-article-add-buttons t]
2086               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2087               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2088               ["Toggle MIME" gnus-summary-toggle-mime t]
2089               ["Verbose header" gnus-summary-verbose-headers t]
2090               ["Toggle header" gnus-summary-toggle-header t]
2091               ["Unfold headers" gnus-article-treat-unfold-headers t]
2092               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2093               ["Html" gnus-article-wash-html t]
2094               ["URLs" gnus-article-unsplit-urls t]
2095               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2096               ["HZ" gnus-article-decode-HZ t]
2097               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2098               )
2099              ("Output"
2100               ["Save in default format" gnus-summary-save-article
2101                ,@(if (featurep 'xemacs) '(t)
2102                    '(:help "Save article using default method"))]
2103               ["Save in file" gnus-summary-save-article-file
2104                ,@(if (featurep 'xemacs) '(t)
2105                    '(:help "Save article in file"))]
2106               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2107               ["Save in MH folder" gnus-summary-save-article-folder t]
2108               ["Save in VM folder" gnus-summary-save-article-vm t]
2109               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2110               ["Save body in file" gnus-summary-save-article-body-file t]
2111               ["Pipe through a filter" gnus-summary-pipe-output t]
2112               ["Add to SOUP packet" gnus-soup-add-article t]
2113               ["Print with Muttprint" gnus-summary-muttprint t]
2114               ["Print" gnus-summary-print-article t])
2115              ("Backend"
2116               ["Respool article..." gnus-summary-respool-article t]
2117               ["Move article..." gnus-summary-move-article
2118                (gnus-check-backend-function
2119                 'request-move-article gnus-newsgroup-name)]
2120               ["Copy article..." gnus-summary-copy-article t]
2121               ["Crosspost article..." gnus-summary-crosspost-article
2122                (gnus-check-backend-function
2123                 'request-replace-article gnus-newsgroup-name)]
2124               ["Import file..." gnus-summary-import-article t]
2125               ["Create article..." gnus-summary-create-article t]
2126               ["Check if posted" gnus-summary-article-posted-p t]
2127               ["Edit article" gnus-summary-edit-article
2128                (not (gnus-group-read-only-p))]
2129               ["Delete article" gnus-summary-delete-article
2130                (gnus-check-backend-function
2131                 'request-expire-articles gnus-newsgroup-name)]
2132               ["Query respool" gnus-summary-respool-query t]
2133               ["Trace respool" gnus-summary-respool-trace t]
2134               ["Delete expirable articles" gnus-summary-expire-articles-now
2135                (gnus-check-backend-function
2136                 'request-expire-articles gnus-newsgroup-name)])
2137              ("Extract"
2138               ["Uudecode" gnus-uu-decode-uu
2139                ,@(if (featurep 'xemacs) '(t)
2140                    '(:help "Decode uuencoded article(s)"))]
2141               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2142               ["Unshar" gnus-uu-decode-unshar t]
2143               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2144               ["Save" gnus-uu-decode-save t]
2145               ["Binhex" gnus-uu-decode-binhex t]
2146               ["Postscript" gnus-uu-decode-postscript t])
2147              ("Cache"
2148               ["Enter article" gnus-cache-enter-article t]
2149               ["Remove article" gnus-cache-remove-article t])
2150              ["Translate" gnus-article-babel t]
2151              ["Select article buffer" gnus-summary-select-article-buffer t]
2152              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2153              ["Isearch article..." gnus-summary-isearch-article t]
2154              ["Beginning of the article" gnus-summary-beginning-of-article t]
2155              ["End of the article" gnus-summary-end-of-article t]
2156              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2157              ["Fetch referenced articles" gnus-summary-refer-references t]
2158              ["Fetch current thread" gnus-summary-refer-thread t]
2159              ["Fetch article with id..." gnus-summary-refer-article t]
2160              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2161              ["Redisplay" gnus-summary-show-article t]
2162              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2163       (easy-menu-define
2164        gnus-summary-article-menu gnus-summary-mode-map ""
2165        (cons "Article" innards))
2166
2167       (if (not (keymapp gnus-summary-article-menu))
2168           (easy-menu-define
2169            gnus-article-commands-menu gnus-article-mode-map ""
2170            (cons "Commands" innards))
2171         ;; in Emacs, don't share menu.
2172         (setq gnus-article-commands-menu
2173               (copy-keymap gnus-summary-article-menu))
2174         (define-key gnus-article-mode-map [menu-bar commands]
2175           (cons "Commands" gnus-article-commands-menu))))
2176
2177     (easy-menu-define
2178      gnus-summary-thread-menu gnus-summary-mode-map ""
2179      '("Threads"
2180        ["Toggle threading" gnus-summary-toggle-threads t]
2181        ["Hide threads" gnus-summary-hide-all-threads t]
2182        ["Show threads" gnus-summary-show-all-threads t]
2183        ["Hide thread" gnus-summary-hide-thread t]
2184        ["Show thread" gnus-summary-show-thread t]
2185        ["Go to next thread" gnus-summary-next-thread t]
2186        ["Go to previous thread" gnus-summary-prev-thread t]
2187        ["Go down thread" gnus-summary-down-thread t]
2188        ["Go up thread" gnus-summary-up-thread t]
2189        ["Top of thread" gnus-summary-top-thread t]
2190        ["Mark thread as read" gnus-summary-kill-thread t]
2191        ["Lower thread score" gnus-summary-lower-thread t]
2192        ["Raise thread score" gnus-summary-raise-thread t]
2193        ["Rethread current" gnus-summary-rethread-current t]))
2194
2195     (easy-menu-define
2196      gnus-summary-post-menu gnus-summary-mode-map ""
2197      `("Post"
2198        ["Send a message (mail or news)" gnus-summary-post-news
2199         ,@(if (featurep 'xemacs) '(t)
2200             '(:help "Post an article"))]
2201        ["Followup" gnus-summary-followup
2202         ,@(if (featurep 'xemacs) '(t)
2203             '(:help "Post followup to this article"))]
2204        ["Followup and yank" gnus-summary-followup-with-original
2205         ,@(if (featurep 'xemacs) '(t)
2206             '(:help "Post followup to this article, quoting its contents"))]
2207        ["Supersede article" gnus-summary-supersede-article t]
2208        ["Cancel article" gnus-summary-cancel-article
2209         ,@(if (featurep 'xemacs) '(t)
2210             '(:help "Cancel an article you posted"))]
2211        ["Reply" gnus-summary-reply t]
2212        ["Reply and yank" gnus-summary-reply-with-original t]
2213        ["Wide reply" gnus-summary-wide-reply t]
2214        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2215         ,@(if (featurep 'xemacs) '(t)
2216             '(:help "Mail a reply, quoting this article"))]
2217        ["Very wide reply" gnus-summary-very-wide-reply t]
2218        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2219         ,@(if (featurep 'xemacs) '(t)
2220             '(:help "Mail a very wide reply, quoting this article"))]
2221        ["Mail forward" gnus-summary-mail-forward t]
2222        ["Post forward" gnus-summary-post-forward t]
2223        ["Digest and mail" gnus-summary-digest-mail-forward t]
2224        ["Digest and post" gnus-summary-digest-post-forward t]
2225        ["Resend message" gnus-summary-resend-message t]
2226        ["Resend message edit" gnus-summary-resend-message-edit t]
2227        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2228        ["Send a mail" gnus-summary-mail-other-window t]
2229        ["Create a local message" gnus-summary-news-other-window t]
2230        ["Uuencode and post" gnus-uu-post-news
2231         ,@(if (featurep 'xemacs) '(t)
2232             '(:help "Post a uuencoded article"))]
2233        ["Followup via news" gnus-summary-followup-to-mail t]
2234        ["Followup via news and yank"
2235         gnus-summary-followup-to-mail-with-original t]
2236        ;;("Draft"
2237        ;;["Send" gnus-summary-send-draft t]
2238        ;;["Send bounced" gnus-resend-bounced-mail t])
2239        ))
2240
2241     (cond
2242      ((not (keymapp gnus-summary-post-menu))
2243       (setq gnus-article-post-menu gnus-summary-post-menu))
2244      ((not gnus-article-post-menu)
2245       ;; Don't share post menu.
2246       (setq gnus-article-post-menu
2247             (copy-keymap gnus-summary-post-menu))))
2248     (define-key gnus-article-mode-map [menu-bar post]
2249       (cons "Post" gnus-article-post-menu))
2250
2251     (easy-menu-define
2252      gnus-summary-misc-menu gnus-summary-mode-map ""
2253      `("Gnus"
2254        ("Mark Read"
2255         ["Mark as read" gnus-summary-mark-as-read-forward t]
2256         ["Mark same subject and select"
2257          gnus-summary-kill-same-subject-and-select t]
2258         ["Mark same subject" gnus-summary-kill-same-subject t]
2259         ["Catchup" gnus-summary-catchup
2260          ,@(if (featurep 'xemacs) '(t)
2261              '(:help "Mark unread articles in this group as read"))]
2262         ["Catchup all" gnus-summary-catchup-all t]
2263         ["Catchup to here" gnus-summary-catchup-to-here t]
2264         ["Catchup from here" gnus-summary-catchup-from-here t]
2265         ["Catchup region" gnus-summary-mark-region-as-read t]
2266         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2267        ("Mark Various"
2268         ["Tick" gnus-summary-tick-article-forward t]
2269         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2270         ["Remove marks" gnus-summary-clear-mark-forward t]
2271         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2272         ["Set bookmark" gnus-summary-set-bookmark t]
2273         ["Remove bookmark" gnus-summary-remove-bookmark t])
2274        ("Limit to"
2275         ["Marks..." gnus-summary-limit-to-marks t]
2276         ["Subject..." gnus-summary-limit-to-subject t]
2277         ["Author..." gnus-summary-limit-to-author t]
2278         ["Age..." gnus-summary-limit-to-age t]
2279         ["Extra..." gnus-summary-limit-to-extra t]
2280         ["Score..." gnus-summary-limit-to-score t]
2281         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2282         ["Unread" gnus-summary-limit-to-unread t]
2283         ["Unseen" gnus-summary-limit-to-unseen t]
2284         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2285         ["Next articles" gnus-summary-limit-to-articles t]
2286         ["Pop limit" gnus-summary-pop-limit t]
2287         ["Show dormant" gnus-summary-limit-include-dormant t]
2288         ["Hide childless dormant"
2289          gnus-summary-limit-exclude-childless-dormant t]
2290         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2291         ["Hide marked" gnus-summary-limit-exclude-marks t]
2292         ["Show expunged" gnus-summary-limit-include-expunged t])
2293        ("Process Mark"
2294         ["Set mark" gnus-summary-mark-as-processable t]
2295         ["Remove mark" gnus-summary-unmark-as-processable t]
2296         ["Remove all marks" gnus-summary-unmark-all-processable t]
2297         ["Mark above" gnus-uu-mark-over t]
2298         ["Mark series" gnus-uu-mark-series t]
2299         ["Mark region" gnus-uu-mark-region t]
2300         ["Unmark region" gnus-uu-unmark-region t]
2301         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2302         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2303         ["Mark all" gnus-uu-mark-all t]
2304         ["Mark buffer" gnus-uu-mark-buffer t]
2305         ["Mark sparse" gnus-uu-mark-sparse t]
2306         ["Mark thread" gnus-uu-mark-thread t]
2307         ["Unmark thread" gnus-uu-unmark-thread t]
2308         ("Process Mark Sets"
2309          ["Kill" gnus-summary-kill-process-mark t]
2310          ["Yank" gnus-summary-yank-process-mark
2311           gnus-newsgroup-process-stack]
2312          ["Save" gnus-summary-save-process-mark t]))
2313        ("Scroll article"
2314         ["Page forward" gnus-summary-next-page
2315          ,@(if (featurep 'xemacs) '(t)
2316              '(:help "Show next page of article"))]
2317         ["Page backward" gnus-summary-prev-page
2318          ,@(if (featurep 'xemacs) '(t)
2319              '(:help "Show previous page of article"))]
2320         ["Line forward" gnus-summary-scroll-up t])
2321        ("Move"
2322         ["Next unread article" gnus-summary-next-unread-article t]
2323         ["Previous unread article" gnus-summary-prev-unread-article t]
2324         ["Next article" gnus-summary-next-article t]
2325         ["Previous article" gnus-summary-prev-article t]
2326         ["Next unread subject" gnus-summary-next-unread-subject t]
2327         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2328         ["Next article same subject" gnus-summary-next-same-subject t]
2329         ["Previous article same subject" gnus-summary-prev-same-subject t]
2330         ["First unread article" gnus-summary-first-unread-article t]
2331         ["Best unread article" gnus-summary-best-unread-article t]
2332         ["Go to subject number..." gnus-summary-goto-subject t]
2333         ["Go to article number..." gnus-summary-goto-article t]
2334         ["Go to the last article" gnus-summary-goto-last-article t]
2335         ["Pop article off history" gnus-summary-pop-article t])
2336        ("Sort"
2337         ["Sort by number" gnus-summary-sort-by-number t]
2338         ["Sort by author" gnus-summary-sort-by-author t]
2339         ["Sort by subject" gnus-summary-sort-by-subject t]
2340         ["Sort by date" gnus-summary-sort-by-date t]
2341         ["Sort by score" gnus-summary-sort-by-score t]
2342         ["Sort by lines" gnus-summary-sort-by-lines t]
2343         ["Sort by characters" gnus-summary-sort-by-chars t]
2344         ["Randomize" gnus-summary-sort-by-random t]
2345         ["Original sort" gnus-summary-sort-by-original t])
2346        ("Help"
2347         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2348         ["Describe group" gnus-summary-describe-group t]
2349         ["Fetch charter" gnus-group-fetch-charter
2350          ,@(if (featurep 'xemacs) nil
2351              '(:help "Display the charter of the current group"))]
2352         ["Fetch control message" gnus-group-fetch-control
2353          ,@(if (featurep 'xemacs) nil
2354              '(:help "Display the archived control message for the current group"))]
2355         ["Read manual" gnus-info-find-node t])
2356        ("Modes"
2357         ["Pick and read" gnus-pick-mode t]
2358         ["Binary" gnus-binary-mode t])
2359        ("Regeneration"
2360         ["Regenerate" gnus-summary-prepare t]
2361         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2362         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2363         ["Toggle threading" gnus-summary-toggle-threads t])
2364        ["See old articles" gnus-summary-insert-old-articles t]
2365        ["See new articles" gnus-summary-insert-new-articles t]
2366        ["Filter articles..." gnus-summary-execute-command t]
2367        ["Run command on subjects..." gnus-summary-universal-argument t]
2368        ["Search articles forward..." gnus-summary-search-article-forward t]
2369        ["Search articles backward..." gnus-summary-search-article-backward t]
2370        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2371        ["Expand window" gnus-summary-expand-window t]
2372        ["Expire expirable articles" gnus-summary-expire-articles
2373         (gnus-check-backend-function
2374          'request-expire-articles gnus-newsgroup-name)]
2375        ["Edit local kill file" gnus-summary-edit-local-kill t]
2376        ["Edit main kill file" gnus-summary-edit-global-kill t]
2377        ["Edit group parameters" gnus-summary-edit-parameters t]
2378        ["Customize group parameters" gnus-summary-customize-parameters t]
2379        ["Send a bug report" gnus-bug t]
2380        ("Exit"
2381         ["Catchup and exit" gnus-summary-catchup-and-exit
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Mark unread articles in this group as read, then exit"))]
2384         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2385         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2386         ["Exit group" gnus-summary-exit
2387          ,@(if (featurep 'xemacs) '(t)
2388              '(:help "Exit current group, return to group selection mode"))]
2389         ["Exit group without updating" gnus-summary-exit-no-update t]
2390         ["Exit and goto next group" gnus-summary-next-group t]
2391         ["Exit and goto prev group" gnus-summary-prev-group t]
2392         ["Reselect group" gnus-summary-reselect-current-group t]
2393         ["Rescan group" gnus-summary-rescan-group t]
2394         ["Update dribble" gnus-summary-save-newsrc t])))
2395
2396     (gnus-run-hooks 'gnus-summary-menu-hook)))
2397
2398 (defvar gnus-summary-tool-bar-map nil)
2399
2400 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2401 (defun gnus-summary-make-tool-bar ()
2402   (if (and (fboundp 'tool-bar-add-item-from-menu)
2403            (default-value 'tool-bar-mode)
2404            (not gnus-summary-tool-bar-map))
2405       (setq gnus-summary-tool-bar-map
2406             (let ((tool-bar-map (make-sparse-keymap))
2407                   (load-path (mm-image-load-path)))
2408               (tool-bar-add-item-from-menu
2409                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2410               (tool-bar-add-item-from-menu
2411                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2412               (tool-bar-add-item-from-menu
2413                'gnus-summary-post-news "post" gnus-summary-mode-map)
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-followup "followup" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-reply "reply" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2438               tool-bar-map)))
2439   (if gnus-summary-tool-bar-map
2440       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2441
2442 (defun gnus-score-set-default (var value)
2443   "A version of set that updates the GNU Emacs menu-bar."
2444   (set var value)
2445   ;; It is the message that forces the active status to be updated.
2446   (message ""))
2447
2448 (defun gnus-make-score-map (type)
2449   "Make a summary score map of type TYPE."
2450   (if t
2451       nil
2452     (let ((headers '(("author" "from" string)
2453                      ("subject" "subject" string)
2454                      ("article body" "body" string)
2455                      ("article head" "head" string)
2456                      ("xref" "xref" string)
2457                      ("extra header" "extra" string)
2458                      ("lines" "lines" number)
2459                      ("followups to author" "followup" string)))
2460           (types '((number ("less than" <)
2461                            ("greater than" >)
2462                            ("equal" =))
2463                    (string ("substring" s)
2464                            ("exact string" e)
2465                            ("fuzzy string" f)
2466                            ("regexp" r))))
2467           (perms '(("temporary" (current-time-string))
2468                    ("permanent" nil)
2469                    ("immediate" now)))
2470           header)
2471       (list
2472        (apply
2473         'nconc
2474         (list
2475          (if (eq type 'lower)
2476              "Lower score"
2477            "Increase score"))
2478         (let (outh)
2479           (while headers
2480             (setq header (car headers))
2481             (setq outh
2482                   (cons
2483                    (apply
2484                     'nconc
2485                     (list (car header))
2486                     (let ((ts (cdr (assoc (nth 2 header) types)))
2487                           outt)
2488                       (while ts
2489                         (setq outt
2490                               (cons
2491                                (apply
2492                                 'nconc
2493                                 (list (caar ts))
2494                                 (let ((ps perms)
2495                                       outp)
2496                                   (while ps
2497                                     (setq outp
2498                                           (cons
2499                                            (vector
2500                                             (caar ps)
2501                                             (list
2502                                              'gnus-summary-score-entry
2503                                              (nth 1 header)
2504                                              (if (or (string= (nth 1 header)
2505                                                               "head")
2506                                                      (string= (nth 1 header)
2507                                                               "body"))
2508                                                  ""
2509                                                (list 'gnus-summary-header
2510                                                      (nth 1 header)))
2511                                              (list 'quote (nth 1 (car ts)))
2512                                              (list 'gnus-score-delta-default
2513                                                    nil)
2514                                              (nth 1 (car ps))
2515                                              t)
2516                                             t)
2517                                            outp))
2518                                     (setq ps (cdr ps)))
2519                                   (list (nreverse outp))))
2520                                outt))
2521                         (setq ts (cdr ts)))
2522                       (list (nreverse outt))))
2523                    outh))
2524             (setq headers (cdr headers)))
2525           (list (nreverse outh))))))))
2526
2527 \f
2528
2529 (defun gnus-summary-mode (&optional group)
2530   "Major mode for reading articles.
2531
2532 All normal editing commands are switched off.
2533 \\<gnus-summary-mode-map>
2534 Each line in this buffer represents one article.  To read an
2535 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2536 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2537 respectively.
2538
2539 You can also post articles and send mail from this buffer.  To
2540 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2541 of an article, type `\\[gnus-summary-reply]'.
2542
2543 There are approx. one gazillion commands you can execute in this
2544 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2545
2546 The following commands are available:
2547
2548 \\{gnus-summary-mode-map}"
2549   (interactive)
2550   (kill-all-local-variables)
2551   (when (gnus-visual-p 'summary-menu 'menu)
2552     (gnus-summary-make-menu-bar)
2553     (gnus-summary-make-tool-bar))
2554   (gnus-summary-make-local-variables)
2555   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2556     (gnus-summary-make-local-variables))
2557   (gnus-make-thread-indent-array)
2558   (gnus-simplify-mode-line)
2559   (setq major-mode 'gnus-summary-mode)
2560   (setq mode-name "Summary")
2561   (make-local-variable 'minor-mode-alist)
2562   (use-local-map gnus-summary-mode-map)
2563   (buffer-disable-undo)
2564   (setq buffer-read-only t)             ;Disable modification
2565   (setq truncate-lines t)
2566   (setq selective-display t)
2567   (setq selective-display-ellipses t)   ;Display `...'
2568   (gnus-summary-set-display-table)
2569   (gnus-set-default-directory)
2570   (setq gnus-newsgroup-name group)
2571   (unless (gnus-news-group-p group)
2572     (setq gnus-newsgroup-incorporated
2573           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2574   (make-local-variable 'gnus-summary-line-format)
2575   (make-local-variable 'gnus-summary-line-format-spec)
2576   (make-local-variable 'gnus-summary-dummy-line-format)
2577   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2578   (make-local-variable 'gnus-summary-mark-positions)
2579   (make-local-hook 'pre-command-hook)
2580   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2581   (gnus-run-hooks 'gnus-summary-mode-hook)
2582   (turn-on-gnus-mailing-list-mode)
2583   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2584   (gnus-update-summary-mark-positions))
2585
2586 (defun gnus-summary-make-local-variables ()
2587   "Make all the local summary buffer variables."
2588   (let (global)
2589     (dolist (local gnus-summary-local-variables)
2590       (if (consp local)
2591           (progn
2592             (if (eq (cdr local) 'global)
2593                 ;; Copy the global value of the variable.
2594                 (setq global (symbol-value (car local)))
2595               ;; Use the value from the list.
2596               (setq global (eval (cdr local))))
2597             (set (make-local-variable (car local)) global))
2598         ;; Simple nil-valued local variable.
2599         (set (make-local-variable local) nil)))))
2600
2601 (defun gnus-summary-clear-local-variables ()
2602   (let ((locals gnus-summary-local-variables))
2603     (while locals
2604       (if (consp (car locals))
2605           (and (vectorp (caar locals))
2606                (set (caar locals) nil))
2607         (and (vectorp (car locals))
2608              (set (car locals) nil)))
2609       (setq locals (cdr locals)))))
2610
2611 ;; Summary data functions.
2612
2613 (defmacro gnus-data-number (data)
2614   `(car ,data))
2615
2616 (defmacro gnus-data-set-number (data number)
2617   `(setcar ,data ,number))
2618
2619 (defmacro gnus-data-mark (data)
2620   `(nth 1 ,data))
2621
2622 (defmacro gnus-data-set-mark (data mark)
2623   `(setcar (nthcdr 1 ,data) ,mark))
2624
2625 (defmacro gnus-data-pos (data)
2626   `(nth 2 ,data))
2627
2628 (defmacro gnus-data-set-pos (data pos)
2629   `(setcar (nthcdr 2 ,data) ,pos))
2630
2631 (defmacro gnus-data-header (data)
2632   `(nth 3 ,data))
2633
2634 (defmacro gnus-data-set-header (data header)
2635   `(setcar (nthcdr 3 ,data) ,header))
2636
2637 (defmacro gnus-data-level (data)
2638   `(nth 4 ,data))
2639
2640 (defmacro gnus-data-unread-p (data)
2641   `(= (nth 1 ,data) gnus-unread-mark))
2642
2643 (defmacro gnus-data-read-p (data)
2644   `(/= (nth 1 ,data) gnus-unread-mark))
2645
2646 (defmacro gnus-data-pseudo-p (data)
2647   `(consp (nth 3 ,data)))
2648
2649 (defmacro gnus-data-find (number)
2650   `(assq ,number gnus-newsgroup-data))
2651
2652 (defmacro gnus-data-find-list (number &optional data)
2653   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2654      (memq (assq ,number bdata)
2655            bdata)))
2656
2657 (defmacro gnus-data-make (number mark pos header level)
2658   `(list ,number ,mark ,pos ,header ,level))
2659
2660 (defun gnus-data-enter (after-article number mark pos header level offset)
2661   (let ((data (gnus-data-find-list after-article)))
2662     (unless data
2663       (error "No such article: %d" after-article))
2664     (setcdr data (cons (gnus-data-make number mark pos header level)
2665                        (cdr data)))
2666     (setq gnus-newsgroup-data-reverse nil)
2667     (gnus-data-update-list (cddr data) offset)))
2668
2669 (defun gnus-data-enter-list (after-article list &optional offset)
2670   (when list
2671     (let ((data (and after-article (gnus-data-find-list after-article)))
2672           (ilist list))
2673       (if (not (or data
2674                    after-article))
2675           (let ((odata gnus-newsgroup-data))
2676             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2677             (when offset
2678               (gnus-data-update-list odata offset)))
2679         ;; Find the last element in the list to be spliced into the main
2680         ;; list.
2681         (while (cdr list)
2682           (setq list (cdr list)))
2683         (if (not data)
2684             (progn
2685               (setcdr list gnus-newsgroup-data)
2686               (setq gnus-newsgroup-data ilist)
2687               (when offset
2688                 (gnus-data-update-list (cdr list) offset)))
2689           (setcdr list (cdr data))
2690           (setcdr data ilist)
2691           (when offset
2692             (gnus-data-update-list (cdr list) offset))))
2693       (setq gnus-newsgroup-data-reverse nil))))
2694
2695 (defun gnus-data-remove (article &optional offset)
2696   (let ((data gnus-newsgroup-data))
2697     (if (= (gnus-data-number (car data)) article)
2698         (progn
2699           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2700                 gnus-newsgroup-data-reverse nil)
2701           (when offset
2702             (gnus-data-update-list gnus-newsgroup-data offset)))
2703       (while (cdr data)
2704         (when (= (gnus-data-number (cadr data)) article)
2705           (setcdr data (cddr data))
2706           (when offset
2707             (gnus-data-update-list (cdr data) offset))
2708           (setq data nil
2709                 gnus-newsgroup-data-reverse nil))
2710         (setq data (cdr data))))))
2711
2712 (defmacro gnus-data-list (backward)
2713   `(if ,backward
2714        (or gnus-newsgroup-data-reverse
2715            (setq gnus-newsgroup-data-reverse
2716                  (reverse gnus-newsgroup-data)))
2717      gnus-newsgroup-data))
2718
2719 (defun gnus-data-update-list (data offset)
2720   "Add OFFSET to the POS of all data entries in DATA."
2721   (setq gnus-newsgroup-data-reverse nil)
2722   (while data
2723     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2724     (setq data (cdr data))))
2725
2726 (defun gnus-summary-article-pseudo-p (article)
2727   "Say whether this article is a pseudo article or not."
2728   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2729
2730 (defmacro gnus-summary-article-sparse-p (article)
2731   "Say whether this article is a sparse article or not."
2732   `(memq ,article gnus-newsgroup-sparse))
2733
2734 (defmacro gnus-summary-article-ancient-p (article)
2735   "Say whether this article is a sparse article or not."
2736   `(memq ,article gnus-newsgroup-ancient))
2737
2738 (defun gnus-article-parent-p (number)
2739   "Say whether this article is a parent or not."
2740   (let ((data (gnus-data-find-list number)))
2741     (and (cdr data)                     ; There has to be an article after...
2742          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2743             (gnus-data-level (nth 1 data))))))
2744
2745 (defun gnus-article-children (number)
2746   "Return a list of all children to NUMBER."
2747   (let* ((data (gnus-data-find-list number))
2748          (level (gnus-data-level (car data)))
2749          children)
2750     (setq data (cdr data))
2751     (while (and data
2752                 (= (gnus-data-level (car data)) (1+ level)))
2753       (push (gnus-data-number (car data)) children)
2754       (setq data (cdr data)))
2755     children))
2756
2757 (defmacro gnus-summary-skip-intangible ()
2758   "If the current article is intangible, then jump to a different article."
2759   '(let ((to (get-text-property (point) 'gnus-intangible)))
2760      (and to (gnus-summary-goto-subject to))))
2761
2762 (defmacro gnus-summary-article-intangible-p ()
2763   "Say whether this article is intangible or not."
2764   '(get-text-property (point) 'gnus-intangible))
2765
2766 (defun gnus-article-read-p (article)
2767   "Say whether ARTICLE is read or not."
2768   (not (or (memq article gnus-newsgroup-marked)
2769            (memq article gnus-newsgroup-spam-marked)
2770            (memq article gnus-newsgroup-unreads)
2771            (memq article gnus-newsgroup-unselected)
2772            (memq article gnus-newsgroup-dormant))))
2773
2774 ;; Some summary mode macros.
2775
2776 (defmacro gnus-summary-article-number ()
2777   "The article number of the article on the current line.
2778 If there isn't an article number here, then we return the current
2779 article number."
2780   '(progn
2781      (gnus-summary-skip-intangible)
2782      (or (get-text-property (point) 'gnus-number)
2783          (gnus-summary-last-subject))))
2784
2785 (defmacro gnus-summary-article-header (&optional number)
2786   "Return the header of article NUMBER."
2787   `(gnus-data-header (gnus-data-find
2788                       ,(or number '(gnus-summary-article-number)))))
2789
2790 (defmacro gnus-summary-thread-level (&optional number)
2791   "Return the level of thread that starts with article NUMBER."
2792   `(if (and (eq gnus-summary-make-false-root 'dummy)
2793             (get-text-property (point) 'gnus-intangible))
2794        0
2795      (gnus-data-level (gnus-data-find
2796                        ,(or number '(gnus-summary-article-number))))))
2797
2798 (defmacro gnus-summary-article-mark (&optional number)
2799   "Return the mark of article NUMBER."
2800   `(gnus-data-mark (gnus-data-find
2801                     ,(or number '(gnus-summary-article-number)))))
2802
2803 (defmacro gnus-summary-article-pos (&optional number)
2804   "Return the position of the line of article NUMBER."
2805   `(gnus-data-pos (gnus-data-find
2806                    ,(or number '(gnus-summary-article-number)))))
2807
2808 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2809 (defmacro gnus-summary-article-subject (&optional number)
2810   "Return current subject string or nil if nothing."
2811   `(let ((headers
2812           ,(if number
2813                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2814              '(gnus-data-header (assq (gnus-summary-article-number)
2815                                       gnus-newsgroup-data)))))
2816      (and headers
2817           (vectorp headers)
2818           (mail-header-subject headers))))
2819
2820 (defmacro gnus-summary-article-score (&optional number)
2821   "Return current article score."
2822   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2823                   gnus-newsgroup-scored))
2824        gnus-summary-default-score 0))
2825
2826 (defun gnus-summary-article-children (&optional number)
2827   "Return a list of article numbers that are children of article NUMBER."
2828   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2829          (level (gnus-data-level (car data)))
2830          l children)
2831     (while (and (setq data (cdr data))
2832                 (> (setq l (gnus-data-level (car data))) level))
2833       (and (= (1+ level) l)
2834            (push (gnus-data-number (car data))
2835                  children)))
2836     (nreverse children)))
2837
2838 (defun gnus-summary-article-parent (&optional number)
2839   "Return the article number of the parent of article NUMBER."
2840   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2841                                     (gnus-data-list t)))
2842          (level (gnus-data-level (car data))))
2843     (if (zerop level)
2844         ()                              ; This is a root.
2845       ;; We search until we find an article with a level less than
2846       ;; this one.  That function has to be the parent.
2847       (while (and (setq data (cdr data))
2848                   (not (< (gnus-data-level (car data)) level))))
2849       (and data (gnus-data-number (car data))))))
2850
2851 (defun gnus-unread-mark-p (mark)
2852   "Say whether MARK is the unread mark."
2853   (= mark gnus-unread-mark))
2854
2855 (defun gnus-read-mark-p (mark)
2856   "Say whether MARK is one of the marks that mark as read.
2857 This is all marks except unread, ticked, dormant, and expirable."
2858   (not (or (= mark gnus-unread-mark)
2859            (= mark gnus-ticked-mark)
2860            (= mark gnus-dormant-mark)
2861            (= mark gnus-expirable-mark))))
2862
2863 (defmacro gnus-article-mark (number)
2864   "Return the MARK of article NUMBER.
2865 This macro should only be used when computing the mark the \"first\"
2866 time; i.e., when generating the summary lines.  After that,
2867 `gnus-summary-article-mark' should be used to examine the
2868 marks of articles."
2869   `(cond
2870     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2871     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2872     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2873     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2874     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2875     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2876     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2877     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2878            gnus-ancient-mark))))
2879
2880 ;; Saving hidden threads.
2881
2882 (defmacro gnus-save-hidden-threads (&rest forms)
2883   "Save hidden threads, eval FORMS, and restore the hidden threads."
2884   (let ((config (make-symbol "config")))
2885     `(let ((,config (gnus-hidden-threads-configuration)))
2886        (unwind-protect
2887            (save-excursion
2888              ,@forms)
2889          (gnus-restore-hidden-threads-configuration ,config)))))
2890 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2891 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2892
2893 (defun gnus-data-compute-positions ()
2894   "Compute the positions of all articles."
2895   (setq gnus-newsgroup-data-reverse nil)
2896   (let ((data gnus-newsgroup-data))
2897     (save-excursion
2898       (gnus-save-hidden-threads
2899         (gnus-summary-show-all-threads)
2900         (goto-char (point-min))
2901         (while data
2902           (while (get-text-property (point) 'gnus-intangible)
2903             (forward-line 1))
2904           (gnus-data-set-pos (car data) (+ (point) 3))
2905           (setq data (cdr data))
2906           (forward-line 1))))))
2907
2908 (defun gnus-hidden-threads-configuration ()
2909   "Return the current hidden threads configuration."
2910   (save-excursion
2911     (let (config)
2912       (goto-char (point-min))
2913       (while (search-forward "\r" nil t)
2914         (push (1- (point)) config))
2915       config)))
2916
2917 (defun gnus-restore-hidden-threads-configuration (config)
2918   "Restore hidden threads configuration from CONFIG."
2919   (save-excursion
2920     (let (point buffer-read-only)
2921       (while (setq point (pop config))
2922         (when (and (< point (point-max))
2923                    (goto-char point)
2924                    (eq (char-after) ?\n))
2925           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2926
2927 ;; Various summary mode internalish functions.
2928
2929 (defun gnus-mouse-pick-article (e)
2930   (interactive "e")
2931   (mouse-set-point e)
2932   (gnus-summary-next-page nil t))
2933
2934 (defun gnus-summary-set-display-table ()
2935   "Change the display table.
2936 Odd characters have a tendency to mess
2937 up nicely formatted displays - we make all possible glyphs
2938 display only a single character."
2939
2940   ;; We start from the standard display table, if any.
2941   (let ((table (or (copy-sequence standard-display-table)
2942                    (make-display-table)))
2943         (i 32))
2944     ;; Nix out all the control chars...
2945     (while (>= (setq i (1- i)) 0)
2946       (aset table i [??]))
2947     ;; ... but not newline and cr, of course.  (cr is necessary for the
2948     ;; selective display).
2949     (aset table ?\n nil)
2950     (aset table ?\r nil)
2951     ;; We keep TAB as well.
2952     (aset table ?\t nil)
2953     ;; We nix out any glyphs over 126 that are not set already.
2954     (let ((i 256))
2955       (while (>= (setq i (1- i)) 127)
2956         ;; Only modify if the entry is nil.
2957         (unless (aref table i)
2958           (aset table i [??]))))
2959     (setq buffer-display-table table)))
2960
2961 (defun gnus-summary-set-article-display-arrow (pos)
2962   "Update the overlay arrow to point to line at position POS."
2963   (when (and gnus-summary-display-arrow
2964              (boundp 'overlay-arrow-position)
2965              (boundp 'overlay-arrow-string))
2966     (save-excursion
2967       (goto-char pos)
2968       (beginning-of-line)
2969       (unless overlay-arrow-position
2970         (setq overlay-arrow-position (make-marker)))
2971       (setq overlay-arrow-string "=>"
2972             overlay-arrow-position (set-marker overlay-arrow-position
2973                                                (point)
2974                                                (current-buffer))))))
2975
2976 (defun gnus-summary-buffer-name (group)
2977   "Return the summary buffer name of GROUP."
2978   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2979
2980 (defun gnus-summary-setup-buffer (group)
2981   "Initialize summary buffer."
2982   (let ((buffer (gnus-summary-buffer-name group))
2983         (dead-name (concat "*Dead Summary "
2984                            (gnus-group-decoded-name group) "*")))
2985     ;; If a dead summary buffer exists, we kill it.
2986     (when (gnus-buffer-live-p dead-name)
2987       (gnus-kill-buffer dead-name))
2988     (if (get-buffer buffer)
2989         (progn
2990           (set-buffer buffer)
2991           (setq gnus-summary-buffer (current-buffer))
2992           (not gnus-newsgroup-prepared))
2993       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2994       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2995       (gnus-summary-mode group)
2996       (when gnus-carpal
2997         (gnus-carpal-setup-buffer 'summary))
2998       (unless gnus-single-article-buffer
2999         (make-local-variable 'gnus-article-buffer)
3000         (make-local-variable 'gnus-article-current)
3001         (make-local-variable 'gnus-original-article-buffer))
3002       (setq gnus-newsgroup-name group)
3003       ;; Set any local variables in the group parameters.
3004       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3005       t)))
3006
3007 (defun gnus-set-global-variables ()
3008   "Set the global equivalents of the buffer-local variables.
3009 They are set to the latest values they had.  These reflect the summary
3010 buffer that was in action when the last article was fetched."
3011   (when (eq major-mode 'gnus-summary-mode)
3012     (setq gnus-summary-buffer (current-buffer))
3013     (let ((name gnus-newsgroup-name)
3014           (marked gnus-newsgroup-marked)
3015           (spam gnus-newsgroup-spam-marked)
3016           (unread gnus-newsgroup-unreads)
3017           (headers gnus-current-headers)
3018           (data gnus-newsgroup-data)
3019           (summary gnus-summary-buffer)
3020           (article-buffer gnus-article-buffer)
3021           (original gnus-original-article-buffer)
3022           (gac gnus-article-current)
3023           (reffed gnus-reffed-article-number)
3024           (score-file gnus-current-score-file)
3025           (default-charset gnus-newsgroup-charset)
3026           vlist)
3027       (let ((locals gnus-newsgroup-variables))
3028         (while locals
3029           (if (consp (car locals))
3030               (push (eval (caar locals)) vlist)
3031             (push (eval (car locals)) vlist))
3032           (setq locals (cdr locals)))
3033         (setq vlist (nreverse vlist)))
3034       (save-excursion
3035         (set-buffer gnus-group-buffer)
3036         (setq gnus-newsgroup-name name
3037               gnus-newsgroup-marked marked
3038               gnus-newsgroup-spam-marked spam
3039               gnus-newsgroup-unreads unread
3040               gnus-current-headers headers
3041               gnus-newsgroup-data data
3042               gnus-article-current gac
3043               gnus-summary-buffer summary
3044               gnus-article-buffer article-buffer
3045               gnus-original-article-buffer original
3046               gnus-reffed-article-number reffed
3047               gnus-current-score-file score-file
3048               gnus-newsgroup-charset default-charset)
3049         (let ((locals gnus-newsgroup-variables))
3050           (while locals
3051             (if (consp (car locals))
3052                 (set (caar locals) (pop vlist))
3053               (set (car locals) (pop vlist)))
3054             (setq locals (cdr locals))))
3055         ;; The article buffer also has local variables.
3056         (when (gnus-buffer-live-p gnus-article-buffer)
3057           (set-buffer gnus-article-buffer)
3058           (setq gnus-summary-buffer summary))))))
3059
3060 (defun gnus-summary-article-unread-p (article)
3061   "Say whether ARTICLE is unread or not."
3062   (memq article gnus-newsgroup-unreads))
3063
3064 (defun gnus-summary-first-article-p (&optional article)
3065   "Return whether ARTICLE is the first article in the buffer."
3066   (if (not (setq article (or article (gnus-summary-article-number))))
3067       nil
3068     (eq article (caar gnus-newsgroup-data))))
3069
3070 (defun gnus-summary-last-article-p (&optional article)
3071   "Return whether ARTICLE is the last article in the buffer."
3072   (if (not (setq article (or article (gnus-summary-article-number))))
3073       ;; All non-existent numbers are the last article.  :-)
3074       t
3075     (not (cdr (gnus-data-find-list article)))))
3076
3077 (defun gnus-make-thread-indent-array ()
3078   (let ((n 200))
3079     (unless (and gnus-thread-indent-array
3080                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3081       (setq gnus-thread-indent-array (make-vector 201 "")
3082             gnus-thread-indent-array-level gnus-thread-indent-level)
3083       (while (>= n 0)
3084         (aset gnus-thread-indent-array n
3085               (make-string (* n gnus-thread-indent-level) ? ))
3086         (setq n (1- n))))))
3087
3088 (defun gnus-update-summary-mark-positions ()
3089   "Compute where the summary marks are to go."
3090   (save-excursion
3091     (when (gnus-buffer-exists-p gnus-summary-buffer)
3092       (set-buffer gnus-summary-buffer))
3093     (let ((gnus-replied-mark 129)
3094           (gnus-score-below-mark 130)
3095           (gnus-score-over-mark 130)
3096           (gnus-downloaded-mark 131)
3097           (spec gnus-summary-line-format-spec)
3098           gnus-visual pos)
3099       (save-excursion
3100         (gnus-set-work-buffer)
3101         (let ((gnus-summary-line-format-spec spec)
3102               (gnus-newsgroup-downloadable '(0)))
3103           (gnus-summary-insert-line
3104            (make-full-mail-header 0 "" "nobody"
3105                                   "05 Apr 2001 23:33:09 +0400"
3106                                   "" "" 0 0 "" nil)
3107            0 nil nil 128 t nil "" nil 1)
3108           (goto-char (point-min))
3109           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3110                                              (- (point) (point-min) 1)))))
3111           (goto-char (point-min))
3112           (push (cons 'replied (and (search-forward "\201" nil t)
3113                                     (- (point) (point-min) 1)))
3114                 pos)
3115           (goto-char (point-min))
3116           (push (cons 'score (and (search-forward "\202" nil t)
3117                                   (- (point) (point-min) 1)))
3118                 pos)
3119           (goto-char (point-min))
3120           (push (cons 'download
3121                       (and (search-forward "\203" nil t)
3122                            (- (point) (point-min) 1)))
3123                 pos)))
3124       (setq gnus-summary-mark-positions pos))))
3125
3126 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3127   "Insert a dummy root in the summary buffer."
3128   (beginning-of-line)
3129   (gnus-add-text-properties
3130    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3131    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3132
3133 (defun gnus-summary-extract-address-component (from)
3134   (or (car (funcall gnus-extract-address-components from))
3135       from))
3136
3137 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3138   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3139                                 default-mime-charset)))
3140     ;; Is it really necessary to do this next part for each summary line?
3141     ;; Luckily, doesn't seem to slow things down much.
3142     (or
3143      (and gnus-ignored-from-addresses
3144           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3145           (let ((extra-headers (mail-header-extra header))
3146                 to
3147                 newsgroups)
3148             (cond
3149              ((setq to (cdr (assq 'To extra-headers)))
3150               (concat "-> "
3151                       (inline
3152                         (gnus-summary-extract-address-component
3153                          (funcall gnus-decode-encoded-word-function to)))))
3154              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3155               (concat "=> " newsgroups)))))
3156      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3157
3158 (defun gnus-summary-insert-line (gnus-tmp-header
3159                                  gnus-tmp-level gnus-tmp-current
3160                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3161                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3162                                  &optional gnus-tmp-dummy gnus-tmp-score
3163                                  gnus-tmp-process)
3164   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3165          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3166          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3167          (gnus-tmp-score-char
3168           (if (or (null gnus-summary-default-score)
3169                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3170                       gnus-summary-zcore-fuzz))
3171               ?\ ;;;Whitespace
3172             (if (< gnus-tmp-score gnus-summary-default-score)
3173                 gnus-score-below-mark gnus-score-over-mark)))
3174          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3175          (gnus-tmp-replied
3176           (cond (gnus-tmp-process gnus-process-mark)
3177                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3178                  gnus-cached-mark)
3179                 (gnus-tmp-replied gnus-replied-mark)
3180                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3181                  gnus-forwarded-mark)
3182                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3183                  gnus-saved-mark)
3184                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3185                  gnus-recent-mark)
3186                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3187                  gnus-unseen-mark)
3188                 (t gnus-no-mark)))
3189          (gnus-tmp-downloaded
3190           (cond (undownloaded 
3191                  gnus-undownloaded-mark)
3192                 (gnus-newsgroup-agentized
3193                  gnus-downloaded-mark)
3194                 (t
3195                  gnus-no-mark)))
3196          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3197          (gnus-tmp-name
3198           (cond
3199            ((string-match "<[^>]+> *$" gnus-tmp-from)
3200             (let ((beg (match-beginning 0)))
3201               (or (and (string-match "^\".+\"" gnus-tmp-from)
3202                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3203                   (substring gnus-tmp-from 0 beg))))
3204            ((string-match "(.+)" gnus-tmp-from)
3205             (substring gnus-tmp-from
3206                        (1+ (match-beginning 0)) (1- (match-end 0))))
3207            (t gnus-tmp-from)))
3208          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3209          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3210          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3211          (buffer-read-only nil))
3212     (when (string= gnus-tmp-name "")
3213       (setq gnus-tmp-name gnus-tmp-from))
3214     (unless (numberp gnus-tmp-lines)
3215       (setq gnus-tmp-lines -1))
3216     (if (= gnus-tmp-lines -1)
3217         (setq gnus-tmp-lines "?")
3218       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3219     (gnus-put-text-property-excluding-characters-with-faces
3220      (point)
3221      (progn (eval gnus-summary-line-format-spec) (point))
3222      'gnus-number gnus-tmp-number)
3223     (when (gnus-visual-p 'summary-highlight 'highlight)
3224       (forward-line -1)
3225       (gnus-run-hooks 'gnus-summary-update-hook)
3226       (forward-line 1))))
3227
3228 (defun gnus-summary-update-line (&optional dont-update)
3229   "Update summary line after change."
3230   (when (and gnus-summary-default-score
3231              (not gnus-summary-inhibit-highlight))
3232     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3233            (article (gnus-summary-article-number))
3234            (score (gnus-summary-article-score article)))
3235       (unless dont-update
3236         (if (and gnus-summary-mark-below
3237                  (< (gnus-summary-article-score)
3238                     gnus-summary-mark-below))
3239             ;; This article has a low score, so we mark it as read.
3240             (when (memq article gnus-newsgroup-unreads)
3241               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3242           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3243             ;; This article was previously marked as read on account
3244             ;; of a low score, but now it has risen, so we mark it as
3245             ;; unread.
3246             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3247         (gnus-summary-update-mark
3248          (if (or (null gnus-summary-default-score)
3249                  (<= (abs (- score gnus-summary-default-score))
3250                      gnus-summary-zcore-fuzz))
3251              ?\ ;;;Whitespace
3252            (if (< score gnus-summary-default-score)
3253                gnus-score-below-mark gnus-score-over-mark))
3254          'score))
3255       ;; Do visual highlighting.
3256       (when (gnus-visual-p 'summary-highlight 'highlight)
3257         (gnus-run-hooks 'gnus-summary-update-hook)))))
3258
3259 (defvar gnus-tmp-new-adopts nil)
3260
3261 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3262   "Return the number of articles in THREAD.
3263 This may be 0 in some cases -- if none of the articles in
3264 the thread are to be displayed."
3265   (let* ((number
3266           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3267           (cond
3268            ((not (listp thread))
3269             1)
3270            ((and (consp thread) (cdr thread))
3271             (apply
3272              '+ 1 (mapcar
3273                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3274            ((null thread)
3275             1)
3276            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3277             1)
3278            (t 0))))
3279     (when (and level (zerop level) gnus-tmp-new-adopts)
3280       (incf number
3281             (apply '+ (mapcar
3282                        'gnus-summary-number-of-articles-in-thread
3283                        gnus-tmp-new-adopts))))
3284     (if char
3285         (if (> number 1) gnus-not-empty-thread-mark
3286           gnus-empty-thread-mark)
3287       number)))
3288
3289 (defsubst gnus-summary-line-message-size (head)
3290   "Return pretty-printed version of message size.
3291 This function is intended to be used in
3292 `gnus-summary-line-format-alist', which see."
3293   (let ((c (or (mail-header-chars head) -1)))
3294     (cond ((< c 0) "n/a")               ; chars not available
3295           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3296           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3297           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3298           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3299
3300
3301 (defun gnus-summary-set-local-parameters (group)
3302   "Go through the local params of GROUP and set all variable specs in that list."
3303   (let ((params (gnus-group-find-parameter group))
3304         (vars '(quit-config))           ; Ignore quit-config.
3305         elem)
3306     (while params
3307       (setq elem (car params)
3308             params (cdr params))
3309       (and (consp elem)                 ; Has to be a cons.
3310            (consp (cdr elem))           ; The cdr has to be a list.
3311            (symbolp (car elem))         ; Has to be a symbol in there.
3312            (not (memq (car elem) vars))
3313            (ignore-errors               ; So we set it.
3314              (push (car elem) vars)
3315              (make-local-variable (car elem))
3316              (set (car elem) (eval (nth 1 elem))))))))
3317
3318 (defun gnus-summary-read-group (group &optional show-all no-article
3319                                       kill-buffer no-display backward
3320                                       select-articles)
3321   "Start reading news in newsgroup GROUP.
3322 If SHOW-ALL is non-nil, already read articles are also listed.
3323 If NO-ARTICLE is non-nil, no article is selected initially.
3324 If NO-DISPLAY, don't generate a summary buffer."
3325   (let (result)
3326     (while (and group
3327                 (null (setq result
3328                             (let ((gnus-auto-select-next nil))
3329                               (or (gnus-summary-read-group-1
3330                                    group show-all no-article
3331                                    kill-buffer no-display
3332                                    select-articles)
3333                                   (setq show-all nil
3334                                         select-articles nil)))))
3335                 (eq gnus-auto-select-next 'quietly))
3336       (set-buffer gnus-group-buffer)
3337       ;; The entry function called above goes to the next
3338       ;; group automatically, so we go two groups back
3339       ;; if we are searching for the previous group.
3340       (when backward
3341         (gnus-group-prev-unread-group 2))
3342       (if (not (equal group (gnus-group-group-name)))
3343           (setq group (gnus-group-group-name))
3344         (setq group nil)))
3345     result))
3346
3347 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3348   "Directly jump to the other GROUP from summary buffer.
3349 If SHOW-ALL is non-nil, already read articles are also listed."
3350   (interactive
3351    (if (eq gnus-summary-buffer (current-buffer))
3352        (list (completing-read
3353               "Group: " gnus-active-hashtb nil t
3354               (when (and gnus-newsgroup-name
3355                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3356                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3357               'gnus-group-history)
3358              current-prefix-arg)
3359      (error "%s must be invoked from a gnus summary buffer." this-command)))
3360   (unless (or (zerop (length group))
3361               (and gnus-newsgroup-name
3362                    (string-equal gnus-newsgroup-name group)))
3363     (gnus-summary-exit)
3364     (gnus-summary-read-group group show-all
3365                              gnus-dont-select-after-jump-to-other-group)))
3366
3367 (defun gnus-summary-read-group-1 (group show-all no-article
3368                                         kill-buffer no-display
3369                                         &optional select-articles)
3370   ;; Killed foreign groups can't be entered.
3371   ;;  (when (and (not (gnus-group-native-p group))
3372   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3373   ;;    (error "Dead non-native groups can't be entered"))
3374   (gnus-message 5 "Retrieving newsgroup: %s..."
3375                 (gnus-group-decoded-name group))
3376   (let* ((new-group (gnus-summary-setup-buffer group))
3377          (quit-config (gnus-group-quit-config group))
3378          (did-select (and new-group (gnus-select-newsgroup
3379                                      group show-all select-articles))))
3380     (cond
3381      ;; This summary buffer exists already, so we just select it.
3382      ((not new-group)
3383       (gnus-set-global-variables)
3384       (when kill-buffer
3385         (gnus-kill-or-deaden-summary kill-buffer))
3386       (gnus-configure-windows 'summary 'force)
3387       (gnus-set-mode-line 'summary)
3388       (gnus-summary-position-point)
3389       (message "")
3390       t)
3391      ;; We couldn't select this group.
3392      ((null did-select)
3393       (when (and (eq major-mode 'gnus-summary-mode)
3394                  (not (equal (current-buffer) kill-buffer)))
3395         (kill-buffer (current-buffer))
3396         (if (not quit-config)
3397             (progn
3398               ;; Update the info -- marks might need to be removed,
3399               ;; for instance.
3400               (gnus-summary-update-info)
3401               (set-buffer gnus-group-buffer)
3402               (gnus-group-jump-to-group group)
3403               (gnus-group-next-unread-group 1))
3404           (gnus-handle-ephemeral-exit quit-config)))
3405       (let ((grpinfo (gnus-get-info group)))
3406         (if (null (gnus-info-read grpinfo))
3407             (gnus-message 3 "Group %s contains no messages"
3408                           (gnus-group-decoded-name group))
3409           (gnus-message 3 "Can't select group")))
3410       nil)
3411      ;; The user did a `C-g' while prompting for number of articles,
3412      ;; so we exit this group.
3413      ((eq did-select 'quit)
3414       (and (eq major-mode 'gnus-summary-mode)
3415            (not (equal (current-buffer) kill-buffer))
3416            (kill-buffer (current-buffer)))
3417       (when kill-buffer
3418         (gnus-kill-or-deaden-summary kill-buffer))
3419       (if (not quit-config)
3420           (progn
3421             (set-buffer gnus-group-buffer)
3422             (gnus-group-jump-to-group group)
3423             (gnus-group-next-unread-group 1)
3424             (gnus-configure-windows 'group 'force))
3425         (gnus-handle-ephemeral-exit quit-config))
3426       ;; Finally signal the quit.
3427       (signal 'quit nil))
3428      ;; The group was successfully selected.
3429      (t
3430       (gnus-set-global-variables)
3431       ;; Save the active value in effect when the group was entered.
3432       (setq gnus-newsgroup-active
3433             (gnus-copy-sequence
3434              (gnus-active gnus-newsgroup-name)))
3435       ;; You can change the summary buffer in some way with this hook.
3436       (gnus-run-hooks 'gnus-select-group-hook)
3437       (gnus-update-format-specifications
3438        nil 'summary 'summary-mode 'summary-dummy)
3439       (gnus-update-summary-mark-positions)
3440       ;; Do score processing.
3441       (when gnus-use-scoring
3442         (gnus-possibly-score-headers))
3443       ;; Check whether to fill in the gaps in the threads.
3444       (when gnus-build-sparse-threads
3445         (gnus-build-sparse-threads))
3446       ;; Find the initial limit.
3447       (if gnus-show-threads
3448           (if show-all
3449               (let ((gnus-newsgroup-dormant nil))
3450                 (gnus-summary-initial-limit show-all))
3451             (gnus-summary-initial-limit show-all))
3452         ;; When unthreaded, all articles are always shown.
3453         (setq gnus-newsgroup-limit
3454               (mapcar
3455                (lambda (header) (mail-header-number header))
3456                gnus-newsgroup-headers)))
3457       ;; Generate the summary buffer.
3458       (unless no-display
3459         (gnus-summary-prepare))
3460       (when gnus-use-trees
3461         (gnus-tree-open group)
3462         (setq gnus-summary-highlight-line-function
3463               'gnus-tree-highlight-article))
3464       ;; If the summary buffer is empty, but there are some low-scored
3465       ;; articles or some excluded dormants, we include these in the
3466       ;; buffer.
3467       (when (and (zerop (buffer-size))
3468                  (not no-display))
3469         (cond (gnus-newsgroup-dormant
3470                (gnus-summary-limit-include-dormant))
3471               ((and gnus-newsgroup-scored show-all)
3472                (gnus-summary-limit-include-expunged t))))
3473       ;; Function `gnus-apply-kill-file' must be called in this hook.
3474       (gnus-run-hooks 'gnus-apply-kill-hook)
3475       (if (and (zerop (buffer-size))
3476                (not no-display))
3477           (progn
3478             ;; This newsgroup is empty.
3479             (gnus-summary-catchup-and-exit nil t)
3480             (gnus-message 6 "No unread news")
3481             (when kill-buffer
3482               (gnus-kill-or-deaden-summary kill-buffer))
3483             ;; Return nil from this function.
3484             nil)
3485         ;; Hide conversation thread subtrees.  We cannot do this in
3486         ;; gnus-summary-prepare-hook since kill processing may not
3487         ;; work with hidden articles.
3488         (gnus-summary-maybe-hide-threads)
3489         (when kill-buffer
3490           (gnus-kill-or-deaden-summary kill-buffer))
3491         (gnus-summary-auto-select-subject)
3492         ;; Show first unread article if requested.
3493         (if (and (not no-article)
3494                  (not no-display)
3495                  gnus-newsgroup-unreads
3496                  gnus-auto-select-first)
3497             (progn
3498               (gnus-configure-windows 'summary)
3499               (let ((art (gnus-summary-article-number)))
3500                 (unless (or (memq art gnus-newsgroup-undownloaded)
3501                             (memq art gnus-newsgroup-downloadable))
3502                   (gnus-summary-goto-article art))))
3503           ;; Don't select any articles.
3504           (gnus-summary-position-point)
3505           (gnus-configure-windows 'summary 'force)
3506           (gnus-set-mode-line 'summary))
3507         (when (get-buffer-window gnus-group-buffer t)
3508           ;; Gotta use windows, because recenter does weird stuff if
3509           ;; the current buffer ain't the displayed window.
3510           (let ((owin (selected-window)))
3511             (select-window (get-buffer-window gnus-group-buffer t))
3512             (when (gnus-group-goto-group group)
3513               (recenter))
3514             (select-window owin)))
3515         ;; Mark this buffer as "prepared".
3516         (setq gnus-newsgroup-prepared t)
3517         (gnus-run-hooks 'gnus-summary-prepared-hook)
3518         (gnus-group-update-group group)
3519         t)))))
3520
3521 (defun gnus-summary-auto-select-subject ()
3522   "Select the subject line on initial group entry."
3523   (goto-char (point-min))
3524   (cond
3525    ((eq gnus-auto-select-subject 'best)
3526     (gnus-summary-best-unread-subject))
3527    ((eq gnus-auto-select-subject 'unread)
3528     (gnus-summary-first-unread-subject))
3529    ((eq gnus-auto-select-subject 'unseen)
3530     (gnus-summary-first-unseen-subject))
3531    ((eq gnus-auto-select-subject 'unseen-or-unread)
3532     (gnus-summary-first-unseen-or-unread-subject))
3533    ((eq gnus-auto-select-subject 'first)
3534     ;; Do nothing.
3535     )
3536    ((gnus-functionp gnus-auto-select-subject)
3537     (funcall gnus-auto-select-subject))))
3538
3539 (defun gnus-summary-prepare ()
3540   "Generate the summary buffer."
3541   (interactive)
3542   (let ((buffer-read-only nil))
3543     (erase-buffer)
3544     (setq gnus-newsgroup-data nil
3545           gnus-newsgroup-data-reverse nil)
3546     (gnus-run-hooks 'gnus-summary-generate-hook)
3547     ;; Generate the buffer, either with threads or without.
3548     (when gnus-newsgroup-headers
3549       (gnus-summary-prepare-threads
3550        (if gnus-show-threads
3551            (gnus-sort-gathered-threads
3552             (funcall gnus-summary-thread-gathering-function
3553                      (gnus-sort-threads
3554                       (gnus-cut-threads (gnus-make-threads)))))
3555          ;; Unthreaded display.
3556          (gnus-sort-articles gnus-newsgroup-headers))))
3557     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3558     ;; Call hooks for modifying summary buffer.
3559     (goto-char (point-min))
3560     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3561
3562 (defsubst gnus-general-simplify-subject (subject)
3563   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3564   (setq subject
3565         (cond
3566          ;; Truncate the subject.
3567          (gnus-simplify-subject-functions
3568           (gnus-map-function gnus-simplify-subject-functions subject))
3569          ((numberp gnus-summary-gather-subject-limit)
3570           (setq subject (gnus-simplify-subject-re subject))
3571           (if (> (length subject) gnus-summary-gather-subject-limit)
3572               (substring subject 0 gnus-summary-gather-subject-limit)
3573             subject))
3574          ;; Fuzzily simplify it.
3575          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3576           (gnus-simplify-subject-fuzzy subject))
3577          ;; Just remove the leading "Re:".
3578          (t
3579           (gnus-simplify-subject-re subject))))
3580
3581   (if (and gnus-summary-gather-exclude-subject
3582            (string-match gnus-summary-gather-exclude-subject subject))
3583       nil                               ; This article shouldn't be gathered
3584     subject))
3585
3586 (defun gnus-summary-simplify-subject-query ()
3587   "Query where the respool algorithm would put this article."
3588   (interactive)
3589   (gnus-summary-select-article)
3590   (message "%s"
3591            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3592
3593 (defun gnus-gather-threads-by-subject (threads)
3594   "Gather threads by looking at Subject headers."
3595   (if (not gnus-summary-make-false-root)
3596       threads
3597     (let ((hashtb (gnus-make-hashtable 1024))
3598           (prev threads)
3599           (result threads)
3600           subject hthread whole-subject)
3601       (while threads
3602         (setq subject (gnus-general-simplify-subject
3603                        (setq whole-subject (mail-header-subject
3604                                             (caar threads)))))
3605         (when subject
3606           (if (setq hthread (gnus-gethash subject hashtb))
3607               (progn
3608                 ;; We enter a dummy root into the thread, if we
3609                 ;; haven't done that already.
3610                 (unless (stringp (caar hthread))
3611                   (setcar hthread (list whole-subject (car hthread))))
3612                 ;; We add this new gathered thread to this gathered
3613                 ;; thread.
3614                 (setcdr (car hthread)
3615                         (nconc (cdar hthread) (list (car threads))))
3616                 ;; Remove it from the list of threads.
3617                 (setcdr prev (cdr threads))
3618                 (setq threads prev))
3619             ;; Enter this thread into the hash table.
3620             (gnus-sethash subject
3621                           (if gnus-summary-make-false-root-always
3622                               (progn
3623                                 ;; If you want a dummy root above all
3624                                 ;; threads...
3625                                 (setcar threads (list whole-subject
3626                                                       (car threads)))
3627                                 threads)
3628                             threads)
3629                           hashtb)))
3630         (setq prev threads)
3631         (setq threads (cdr threads)))
3632       result)))
3633
3634 (defun gnus-gather-threads-by-references (threads)
3635   "Gather threads by looking at References headers."
3636   (let ((idhashtb (gnus-make-hashtable 1024))
3637         (thhashtb (gnus-make-hashtable 1024))
3638         (prev threads)
3639         (result threads)
3640         ids references id gthread gid entered ref)
3641     (while threads
3642       (when (setq references (mail-header-references (caar threads)))
3643         (setq id (mail-header-id (caar threads))
3644               ids (inline (gnus-split-references references))
3645               entered nil)
3646         (while (setq ref (pop ids))
3647           (setq ids (delete ref ids))
3648           (if (not (setq gid (gnus-gethash ref idhashtb)))
3649               (progn
3650                 (gnus-sethash ref id idhashtb)
3651                 (gnus-sethash id threads thhashtb))
3652             (setq gthread (gnus-gethash gid thhashtb))
3653             (unless entered
3654               ;; We enter a dummy root into the thread, if we
3655               ;; haven't done that already.
3656               (unless (stringp (caar gthread))
3657                 (setcar gthread (list (mail-header-subject (caar gthread))
3658                                       (car gthread))))
3659               ;; We add this new gathered thread to this gathered
3660               ;; thread.
3661               (setcdr (car gthread)
3662                       (nconc (cdar gthread) (list (car threads)))))
3663             ;; Add it into the thread hash table.
3664             (gnus-sethash id gthread thhashtb)
3665             (setq entered t)
3666             ;; Remove it from the list of threads.
3667             (setcdr prev (cdr threads))
3668             (setq threads prev))))
3669       (setq prev threads)
3670       (setq threads (cdr threads)))
3671     result))
3672
3673 (defun gnus-sort-gathered-threads (threads)
3674   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3675   (let ((result threads))
3676     (while threads
3677       (when (stringp (caar threads))
3678         (setcdr (car threads)
3679                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3680       (setq threads (cdr threads)))
3681     result))
3682
3683 (defun gnus-thread-loop-p (root thread)
3684   "Say whether ROOT is in THREAD."
3685   (let ((stack (list thread))
3686         (infloop 0)
3687         th)
3688     (while (setq thread (pop stack))
3689       (setq th (cdr thread))
3690       (while (and th
3691                   (not (eq (caar th) root)))
3692         (pop th))
3693       (if th
3694           ;; We have found a loop.
3695           (let (ref-dep)
3696             (setcdr thread (delq (car th) (cdr thread)))
3697             (if (boundp (setq ref-dep (intern "none"
3698                                               gnus-newsgroup-dependencies)))
3699                 (setcdr (symbol-value ref-dep)
3700                         (nconc (cdr (symbol-value ref-dep))
3701                                (list (car th))))
3702               (set ref-dep (list nil (car th))))
3703             (setq infloop 1
3704                   stack nil))
3705         ;; Push all the subthreads onto the stack.
3706         (push (cdr thread) stack)))
3707     infloop))
3708
3709 (defun gnus-make-threads ()
3710   "Go through the dependency hashtb and find the roots.  Return all threads."
3711   (let (threads)
3712     (while (catch 'infloop
3713              (mapatoms
3714               (lambda (refs)
3715                 ;; Deal with self-referencing References loops.
3716                 (when (and (car (symbol-value refs))
3717                            (not (zerop
3718                                  (apply
3719                                   '+
3720                                   (mapcar
3721                                    (lambda (thread)
3722                                      (gnus-thread-loop-p
3723                                       (car (symbol-value refs)) thread))
3724                                    (cdr (symbol-value refs)))))))
3725                   (setq threads nil)
3726                   (throw 'infloop t))
3727                 (unless (car (symbol-value refs))
3728                   ;; These threads do not refer back to any other articles,
3729                   ;; so they're roots.
3730                   (setq threads (append (cdr (symbol-value refs)) threads))))
3731               gnus-newsgroup-dependencies)))
3732     threads))
3733
3734 ;; Build the thread tree.
3735 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3736   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3737
3738 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3739 if it was already present.
3740
3741 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3742 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3743 Message-IDs will be renamed to a unique Message-ID before being
3744 entered.
3745
3746 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3747   (let* ((id (mail-header-id header))
3748          (id-dep (and id (intern id dependencies)))
3749          parent-id ref ref-dep ref-header replaced)
3750     ;; Enter this `header' in the `dependencies' table.
3751     (cond
3752      ((not id-dep)
3753       (setq header nil))
3754      ;; The first two cases do the normal part: enter a new `header'
3755      ;; in the `dependencies' table.
3756      ((not (boundp id-dep))
3757       (set id-dep (list header)))
3758      ((null (car (symbol-value id-dep)))
3759       (setcar (symbol-value id-dep) header))
3760
3761      ;; From here the `header' was already present in the
3762      ;; `dependencies' table.
3763      (force-new
3764       ;; Overrides an existing entry;
3765       ;; just set the header part of the entry.
3766       (setcar (symbol-value id-dep) header)
3767       (setq replaced t))
3768
3769      ;; Renames the existing `header' to a unique Message-ID.
3770      ((not gnus-summary-ignore-duplicates)
3771       ;; An article with this Message-ID has already been seen.
3772       ;; We rename the Message-ID.
3773       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3774            (list header))
3775       (mail-header-set-id header id))
3776
3777      ;; The last case ignores an existing entry, except it adds any
3778      ;; additional Xrefs (in case the two articles came from different
3779      ;; servers.
3780      ;; Also sets `header' to `nil' meaning that the `dependencies'
3781      ;; table was *not* modified.
3782      (t
3783       (mail-header-set-xref
3784        (car (symbol-value id-dep))
3785        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3786                    "")
3787                (or (mail-header-xref header) "")))
3788       (setq header nil)))
3789
3790     (when (and header (not replaced))
3791       ;; First check that we are not creating a References loop.
3792       (setq parent-id (gnus-parent-id (mail-header-references header)))
3793       (setq ref parent-id)
3794       (while (and ref
3795                   (setq ref-dep (intern-soft ref dependencies))
3796                   (boundp ref-dep)
3797                   (setq ref-header (car (symbol-value ref-dep))))
3798         (if (string= id ref)
3799             ;; Yuk!  This is a reference loop.  Make the article be a
3800             ;; root article.
3801             (progn
3802               (mail-header-set-references (car (symbol-value id-dep)) "none")
3803               (setq ref nil)
3804               (setq parent-id nil))
3805           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3806       (setq ref-dep (intern (or parent-id "none") dependencies))
3807       (if (boundp ref-dep)
3808           (setcdr (symbol-value ref-dep)
3809                   (nconc (cdr (symbol-value ref-dep))
3810                          (list (symbol-value id-dep))))
3811         (set ref-dep (list nil (symbol-value id-dep)))))
3812     header))
3813
3814 (defun gnus-extract-message-id-from-in-reply-to (string)
3815   (if (string-match "<[^>]+>" string)
3816       (substring string (match-beginning 0) (match-end 0))
3817     nil))
3818
3819 (defun gnus-build-sparse-threads ()
3820   (let ((headers gnus-newsgroup-headers)
3821         (mail-parse-charset gnus-newsgroup-charset)
3822         (gnus-summary-ignore-duplicates t)
3823         header references generation relations
3824         subject child end new-child date)
3825     ;; First we create an alist of generations/relations, where
3826     ;; generations is how much we trust the relation, and the relation
3827     ;; is parent/child.
3828     (gnus-message 7 "Making sparse threads...")
3829     (save-excursion
3830       (nnheader-set-temp-buffer " *gnus sparse threads*")
3831       (while (setq header (pop headers))
3832         (when (and (setq references (mail-header-references header))
3833                    (not (string= references "")))
3834           (insert references)
3835           (setq child (mail-header-id header)
3836                 subject (mail-header-subject header)
3837                 date (mail-header-date header)
3838                 generation 0)
3839           (while (search-backward ">" nil t)
3840             (setq end (1+ (point)))
3841             (when (search-backward "<" nil t)
3842               (setq new-child (buffer-substring (point) end))
3843               (push (list (incf generation)
3844                           child (setq child new-child)
3845                           subject date)
3846                     relations)))
3847           (when child
3848             (push (list (1+ generation) child nil subject) relations))
3849           (erase-buffer)))
3850       (kill-buffer (current-buffer)))
3851     ;; Sort over trustworthiness.
3852     (mapcar
3853      (lambda (relation)
3854        (when (gnus-dependencies-add-header
3855               (make-full-mail-header-from-decoded-header
3856                gnus-reffed-article-number
3857                (nth 3 relation) "" (or (nth 4 relation) "")
3858                (nth 1 relation)
3859                (or (nth 2 relation) "") 0 0 "")
3860               gnus-newsgroup-dependencies nil)
3861          (push gnus-reffed-article-number gnus-newsgroup-limit)
3862          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3863          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3864                gnus-newsgroup-reads)
3865          (decf gnus-reffed-article-number)))
3866      (sort relations 'car-less-than-car))
3867     (gnus-message 7 "Making sparse threads...done")))
3868
3869 (defun gnus-build-old-threads ()
3870   ;; Look at all the articles that refer back to old articles, and
3871   ;; fetch the headers for the articles that aren't there.  This will
3872   ;; build complete threads - if the roots haven't been expired by the
3873   ;; server, that is.
3874   (let ((mail-parse-charset gnus-newsgroup-charset)
3875         id heads)
3876     (mapatoms
3877      (lambda (refs)
3878        (when (not (car (symbol-value refs)))
3879          (setq heads (cdr (symbol-value refs)))
3880          (while heads
3881            (if (memq (mail-header-number (caar heads))
3882                      gnus-newsgroup-dormant)
3883                (setq heads (cdr heads))
3884              (setq id (symbol-name refs))
3885              (while (and (setq id (gnus-build-get-header id))
3886                          (not (car (gnus-id-to-thread id)))))
3887              (setq heads nil)))))
3888      gnus-newsgroup-dependencies)))
3889
3890 ;; This function has to be called with point after the article number
3891 ;; on the beginning of the line.
3892 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3893   (let ((eol (gnus-point-at-eol))
3894         (buffer (current-buffer))
3895         header references in-reply-to)
3896
3897     ;; overview: [num subject from date id refs chars lines misc]
3898     (unwind-protect
3899         (progn
3900           (narrow-to-region (point) eol)
3901           (unless (eobp)
3902             (forward-char))
3903
3904           (setq header
3905                 (make-full-mail-header
3906                  number                         ; number
3907                  (nnheader-nov-field)           ; subject
3908                  (nnheader-nov-field)           ; from
3909                  (nnheader-nov-field)           ; date
3910                  (nnheader-nov-read-message-id) ; id
3911                  (nnheader-nov-field)           ; refs
3912                  (nnheader-nov-read-integer)    ; chars
3913                  (nnheader-nov-read-integer)    ; lines
3914                  (unless (eobp)
3915                    (if (looking-at "Xref: ")
3916                        (goto-char (match-end 0)))
3917                    (nnheader-nov-field))        ; Xref
3918                  (nnheader-nov-parse-extra))))  ; extra
3919
3920       (widen))
3921
3922     (when (and (string= references "")
3923                (setq in-reply-to (mail-header-extra header))
3924                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3925       (mail-header-set-references
3926        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3927
3928     (when gnus-alter-header-function
3929       (funcall gnus-alter-header-function header))
3930     (gnus-dependencies-add-header header dependencies force-new)))
3931
3932 (defun gnus-build-get-header (id)
3933   "Look through the buffer of NOV lines and find the header to ID.
3934 Enter this line into the dependencies hash table, and return
3935 the id of the parent article (if any)."
3936   (let ((deps gnus-newsgroup-dependencies)
3937         found header)
3938     (prog1
3939         (save-excursion
3940           (set-buffer nntp-server-buffer)
3941           (let ((case-fold-search nil))
3942             (goto-char (point-min))
3943             (while (and (not found)
3944                         (search-forward id nil t))
3945               (beginning-of-line)
3946               (setq found (looking-at
3947                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3948                                    (regexp-quote id))))
3949               (or found (beginning-of-line 2)))
3950             (when found
3951               (beginning-of-line)
3952               (and
3953                (setq header (gnus-nov-parse-line
3954                              (read (current-buffer)) deps))
3955                (gnus-parent-id (mail-header-references header))))))
3956       (when header
3957         (let ((number (mail-header-number header)))
3958           (push number gnus-newsgroup-limit)
3959           (push header gnus-newsgroup-headers)
3960           (if (memq number gnus-newsgroup-unselected)
3961               (progn
3962                 (setq gnus-newsgroup-unreads
3963                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3964                                                number))
3965                 (setq gnus-newsgroup-unselected
3966                       (delq number gnus-newsgroup-unselected)))
3967             (push number gnus-newsgroup-ancient)))))))
3968
3969 (defun gnus-build-all-threads ()
3970   "Read all the headers."
3971   (let ((gnus-summary-ignore-duplicates t)
3972         (mail-parse-charset gnus-newsgroup-charset)
3973         (dependencies gnus-newsgroup-dependencies)
3974         header article)
3975     (save-excursion
3976       (set-buffer nntp-server-buffer)
3977       (let ((case-fold-search nil))
3978         (goto-char (point-min))
3979         (while (not (eobp))
3980           (ignore-errors
3981             (setq article (read (current-buffer))
3982                   header (gnus-nov-parse-line article dependencies)))
3983           (when header
3984             (save-excursion
3985               (set-buffer gnus-summary-buffer)
3986               (push header gnus-newsgroup-headers)
3987               (if (memq (setq article (mail-header-number header))
3988                         gnus-newsgroup-unselected)
3989                   (progn
3990                     (setq gnus-newsgroup-unreads
3991                           (gnus-add-to-sorted-list
3992                            gnus-newsgroup-unreads article))
3993                     (setq gnus-newsgroup-unselected
3994                           (delq article gnus-newsgroup-unselected)))
3995                 (push article gnus-newsgroup-ancient)))
3996             (forward-line 1)))))))
3997
3998 (defun gnus-summary-update-article-line (article header)
3999   "Update the line for ARTICLE using HEADERS."
4000   (let* ((id (mail-header-id header))
4001          (thread (gnus-id-to-thread id)))
4002     (unless thread
4003       (error "Article in no thread"))
4004     ;; Update the thread.
4005     (setcar thread header)
4006     (gnus-summary-goto-subject article)
4007     (let* ((datal (gnus-data-find-list article))
4008            (data (car datal))
4009            (length (when (cdr datal)
4010                      (- (gnus-data-pos data)
4011                         (gnus-data-pos (cadr datal)))))
4012            (buffer-read-only nil)
4013            (level (gnus-summary-thread-level)))
4014       (gnus-delete-line)
4015       (gnus-summary-insert-line
4016        header level nil 
4017        (memq article gnus-newsgroup-undownloaded)
4018        (gnus-article-mark article)
4019        (memq article gnus-newsgroup-replied)
4020        (memq article gnus-newsgroup-expirable)
4021        ;; Only insert the Subject string when it's different
4022        ;; from the previous Subject string.
4023        (if (and
4024             gnus-show-threads
4025             (gnus-subject-equal
4026              (condition-case ()
4027                  (mail-header-subject
4028                   (gnus-data-header
4029                    (cadr
4030                     (gnus-data-find-list
4031                      article
4032                      (gnus-data-list t)))))
4033                ;; Error on the side of excessive subjects.
4034                (error ""))
4035              (mail-header-subject header)))
4036            ""
4037          (mail-header-subject header))
4038        nil (cdr (assq article gnus-newsgroup-scored))
4039        (memq article gnus-newsgroup-processable))
4040       (when length
4041         (gnus-data-update-list
4042          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4043
4044 (defun gnus-summary-update-article (article &optional iheader)
4045   "Update ARTICLE in the summary buffer."
4046   (set-buffer gnus-summary-buffer)
4047   (let* ((header (gnus-summary-article-header article))
4048          (id (mail-header-id header))
4049          (data (gnus-data-find article))
4050          (thread (gnus-id-to-thread id))
4051          (references (mail-header-references header))
4052          (parent
4053           (gnus-id-to-thread
4054            (or (gnus-parent-id
4055                 (when (and references
4056                            (not (equal "" references)))
4057                   references))
4058                "none")))
4059          (buffer-read-only nil)
4060          (old (car thread)))
4061     (when thread
4062       (unless iheader
4063         (setcar thread nil)
4064         (when parent
4065           (delq thread parent)))
4066       (if (gnus-summary-insert-subject id header)
4067           ;; Set the (possibly) new article number in the data structure.
4068           (gnus-data-set-number data (gnus-id-to-article id))
4069         (setcar thread old)
4070         nil))))
4071
4072 (defun gnus-rebuild-thread (id &optional line)
4073   "Rebuild the thread containing ID.
4074 If LINE, insert the rebuilt thread starting on line LINE."
4075   (let ((buffer-read-only nil)
4076         old-pos current thread data)
4077     (if (not gnus-show-threads)
4078         (setq thread (list (car (gnus-id-to-thread id))))
4079       ;; Get the thread this article is part of.
4080       (setq thread (gnus-remove-thread id)))
4081     (setq old-pos (gnus-point-at-bol))
4082     (setq current (save-excursion
4083                     (and (re-search-backward "[\r\n]" nil t)
4084                          (gnus-summary-article-number))))
4085     ;; If this is a gathered thread, we have to go some re-gathering.
4086     (when (stringp (car thread))
4087       (let ((subject (car thread))
4088             roots thr)
4089         (setq thread (cdr thread))
4090         (while thread
4091           (unless (memq (setq thr (gnus-id-to-thread
4092                                    (gnus-root-id
4093                                     (mail-header-id (caar thread)))))
4094                         roots)
4095             (push thr roots))
4096           (setq thread (cdr thread)))
4097         ;; We now have all (unique) roots.
4098         (if (= (length roots) 1)
4099             ;; All the loose roots are now one solid root.
4100             (setq thread (car roots))
4101           (setq thread (cons subject (gnus-sort-threads roots))))))
4102     (let (threads)
4103       ;; We then insert this thread into the summary buffer.
4104       (when line
4105         (goto-char (point-min))
4106         (forward-line (1- line)))
4107       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4108         (if gnus-show-threads
4109             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4110           (gnus-summary-prepare-unthreaded thread))
4111         (setq data (nreverse gnus-newsgroup-data))
4112         (setq threads gnus-newsgroup-threads))
4113       ;; We splice the new data into the data structure.
4114       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4115       ;;!!! then we want to insert at the beginning of the buffer.
4116       ;;!!! That happens to be true with Gnus now, but that may
4117       ;;!!! change in the future.  Perhaps.
4118       (gnus-data-enter-list
4119        (if line nil current) data (- (point) old-pos))
4120       (setq gnus-newsgroup-threads
4121             (nconc threads gnus-newsgroup-threads))
4122       (gnus-data-compute-positions))))
4123
4124 (defun gnus-number-to-header (number)
4125   "Return the header for article NUMBER."
4126   (let ((headers gnus-newsgroup-headers))
4127     (while (and headers
4128                 (not (= number (mail-header-number (car headers)))))
4129       (pop headers))
4130     (when headers
4131       (car headers))))
4132
4133 (defun gnus-parent-headers (in-headers &optional generation)
4134   "Return the headers of the GENERATIONeth parent of HEADERS."
4135   (unless generation
4136     (setq generation 1))
4137   (let ((parent t)
4138         (headers in-headers)
4139         references)
4140     (while (and parent
4141                 (not (zerop generation))
4142                 (setq references (mail-header-references headers)))
4143       (setq headers (if (and references
4144                              (setq parent (gnus-parent-id references)))
4145                         (car (gnus-id-to-thread parent))
4146                       nil))
4147       (decf generation))
4148     (and (not (eq headers in-headers))
4149          headers)))
4150
4151 (defun gnus-id-to-thread (id)
4152   "Return the (sub-)thread where ID appears."
4153   (gnus-gethash id gnus-newsgroup-dependencies))
4154
4155 (defun gnus-id-to-article (id)
4156   "Return the article number of ID."
4157   (let ((thread (gnus-id-to-thread id)))
4158     (when (and thread
4159                (car thread))
4160       (mail-header-number (car thread)))))
4161
4162 (defun gnus-id-to-header (id)
4163   "Return the article headers of ID."
4164   (car (gnus-id-to-thread id)))
4165
4166 (defun gnus-article-displayed-root-p (article)
4167   "Say whether ARTICLE is a root(ish) article."
4168   (let ((level (gnus-summary-thread-level article))
4169         (refs (mail-header-references  (gnus-summary-article-header article)))
4170         particle)
4171     (cond
4172      ((null level) nil)
4173      ((zerop level) t)
4174      ((null refs) t)
4175      ((null (gnus-parent-id refs)) t)
4176      ((and (= 1 level)
4177            (null (setq particle (gnus-id-to-article
4178                                  (gnus-parent-id refs))))
4179            (null (gnus-summary-thread-level particle)))))))
4180
4181 (defun gnus-root-id (id)
4182   "Return the id of the root of the thread where ID appears."
4183   (let (last-id prev)
4184     (while (and id (setq prev (car (gnus-id-to-thread id))))
4185       (setq last-id id
4186             id (gnus-parent-id (mail-header-references prev))))
4187     last-id))
4188
4189 (defun gnus-articles-in-thread (thread)
4190   "Return the list of articles in THREAD."
4191   (cons (mail-header-number (car thread))
4192         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4193
4194 (defun gnus-remove-thread (id &optional dont-remove)
4195   "Remove the thread that has ID in it."
4196   (let (headers thread last-id)
4197     ;; First go up in this thread until we find the root.
4198     (setq last-id (gnus-root-id id)
4199           headers (message-flatten-list (gnus-id-to-thread last-id)))
4200     ;; We have now found the real root of this thread.  It might have
4201     ;; been gathered into some loose thread, so we have to search
4202     ;; through the threads to find the thread we wanted.
4203     (let ((threads gnus-newsgroup-threads)
4204           sub)
4205       (while threads
4206         (setq sub (car threads))
4207         (if (stringp (car sub))
4208             ;; This is a gathered thread, so we look at the roots
4209             ;; below it to find whether this article is in this
4210             ;; gathered root.
4211             (progn
4212               (setq sub (cdr sub))
4213               (while sub
4214                 (when (member (caar sub) headers)
4215                   (setq thread (car threads)
4216                         threads nil
4217                         sub nil))
4218                 (setq sub (cdr sub))))
4219           ;; It's an ordinary thread, so we check it.
4220           (when (eq (car sub) (car headers))
4221             (setq thread sub
4222                   threads nil)))
4223         (setq threads (cdr threads)))
4224       ;; If this article is in no thread, then it's a root.
4225       (if thread
4226           (unless dont-remove
4227             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4228         (setq thread (gnus-id-to-thread last-id)))
4229       (when thread
4230         (prog1
4231             thread                      ; We return this thread.
4232           (unless dont-remove
4233             (if (stringp (car thread))
4234                 (progn
4235                   ;; If we use dummy roots, then we have to remove the
4236                   ;; dummy root as well.
4237                   (when (eq gnus-summary-make-false-root 'dummy)
4238                     ;; We go to the dummy root by going to
4239                     ;; the first sub-"thread", and then one line up.
4240                     (gnus-summary-goto-article
4241                      (mail-header-number (caadr thread)))
4242                     (forward-line -1)
4243                     (gnus-delete-line)
4244                     (gnus-data-compute-positions))
4245                   (setq thread (cdr thread))
4246                   (while thread
4247                     (gnus-remove-thread-1 (car thread))
4248                     (setq thread (cdr thread))))
4249               (gnus-remove-thread-1 thread))))))))
4250
4251 (defun gnus-remove-thread-1 (thread)
4252   "Remove the thread THREAD recursively."
4253   (let ((number (mail-header-number (pop thread)))
4254         d)
4255     (setq thread (reverse thread))
4256     (while thread
4257       (gnus-remove-thread-1 (pop thread)))
4258     (when (setq d (gnus-data-find number))
4259       (goto-char (gnus-data-pos d))
4260       (gnus-summary-show-thread)
4261       (gnus-data-remove
4262        number
4263        (- (gnus-point-at-bol)
4264           (prog1
4265               (1+ (gnus-point-at-eol))
4266             (gnus-delete-line)))))))
4267
4268 (defun gnus-sort-threads-1 (threads func)
4269   (sort (mapcar (lambda (thread)
4270                   (cons (car thread)
4271                         (and (cdr thread)
4272                              (gnus-sort-threads-1 (cdr thread) func))))
4273                 threads) func))
4274
4275 (defun gnus-sort-threads (threads)
4276   "Sort THREADS."
4277   (if (not gnus-thread-sort-functions)
4278       threads
4279     (gnus-message 8 "Sorting threads...")
4280     (let ((max-lisp-eval-depth 5000))
4281       (prog1 (gnus-sort-threads-1
4282          threads
4283          (gnus-make-sort-function gnus-thread-sort-functions))
4284         (gnus-message 8 "Sorting threads...done")))))
4285
4286 (defun gnus-sort-articles (articles)
4287   "Sort ARTICLES."
4288   (when gnus-article-sort-functions
4289     (gnus-message 7 "Sorting articles...")
4290     (prog1
4291         (setq gnus-newsgroup-headers
4292               (sort articles (gnus-make-sort-function
4293                               gnus-article-sort-functions)))
4294       (gnus-message 7 "Sorting articles...done"))))
4295
4296 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4297 (defmacro gnus-thread-header (thread)
4298   "Return header of first article in THREAD.
4299 Note that THREAD must never, ever be anything else than a variable -
4300 using some other form will lead to serious barfage."
4301   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4302   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4303   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4304         (vector thread) 2))
4305
4306 (defsubst gnus-article-sort-by-number (h1 h2)
4307   "Sort articles by article number."
4308   (< (mail-header-number h1)
4309      (mail-header-number h2)))
4310
4311 (defun gnus-thread-sort-by-number (h1 h2)
4312   "Sort threads by root article number."
4313   (gnus-article-sort-by-number
4314    (gnus-thread-header h1) (gnus-thread-header h2)))
4315
4316 (defsubst gnus-article-sort-by-random (h1 h2)
4317   "Sort articles by article number."
4318   (zerop (random 2)))
4319
4320 (defun gnus-thread-sort-by-random (h1 h2)
4321   "Sort threads by root article number."
4322   (gnus-article-sort-by-random
4323    (gnus-thread-header h1) (gnus-thread-header h2)))
4324
4325 (defsubst gnus-article-sort-by-lines (h1 h2)
4326   "Sort articles by article Lines header."
4327   (< (mail-header-lines h1)
4328      (mail-header-lines h2)))
4329
4330 (defun gnus-thread-sort-by-lines (h1 h2)
4331   "Sort threads by root article Lines header."
4332   (gnus-article-sort-by-lines
4333    (gnus-thread-header h1) (gnus-thread-header h2)))
4334
4335 (defsubst gnus-article-sort-by-chars (h1 h2)
4336   "Sort articles by octet length."
4337   (< (mail-header-chars h1)
4338      (mail-header-chars h2)))
4339
4340 (defun gnus-thread-sort-by-chars (h1 h2)
4341   "Sort threads by root article octet length."
4342   (gnus-article-sort-by-chars
4343    (gnus-thread-header h1) (gnus-thread-header h2)))
4344
4345 (defsubst gnus-article-sort-by-author (h1 h2)
4346   "Sort articles by root author."
4347   (string-lessp
4348    (let ((addr (car (mime-entity-read-field h1 'From))))
4349      (or (std11-full-name-string addr)
4350          (std11-address-string addr)
4351          ""))
4352    (let ((addr (car (mime-entity-read-field h2 'From))))
4353      (or (std11-full-name-string addr)
4354          (std11-address-string addr)
4355          ""))
4356    ))
4357
4358 (defun gnus-thread-sort-by-author (h1 h2)
4359   "Sort threads by root author."
4360   (gnus-article-sort-by-author
4361    (gnus-thread-header h1)  (gnus-thread-header h2)))
4362
4363 (defsubst gnus-article-sort-by-subject (h1 h2)
4364   "Sort articles by root subject."
4365   (string-lessp
4366    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4367    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4368
4369 (defun gnus-thread-sort-by-subject (h1 h2)
4370   "Sort threads by root subject."
4371   (gnus-article-sort-by-subject
4372    (gnus-thread-header h1) (gnus-thread-header h2)))
4373
4374 (defsubst gnus-article-sort-by-date (h1 h2)
4375   "Sort articles by root article date."
4376   (time-less-p
4377    (gnus-date-get-time (mail-header-date h1))
4378    (gnus-date-get-time (mail-header-date h2))))
4379
4380 (defun gnus-thread-sort-by-date (h1 h2)
4381   "Sort threads by root article date."
4382   (gnus-article-sort-by-date
4383    (gnus-thread-header h1) (gnus-thread-header h2)))
4384
4385 (defsubst gnus-article-sort-by-score (h1 h2)
4386   "Sort articles by root article score.
4387 Unscored articles will be counted as having a score of zero."
4388   (> (or (cdr (assq (mail-header-number h1)
4389                     gnus-newsgroup-scored))
4390          gnus-summary-default-score 0)
4391      (or (cdr (assq (mail-header-number h2)
4392                     gnus-newsgroup-scored))
4393          gnus-summary-default-score 0)))
4394
4395 (defun gnus-thread-sort-by-score (h1 h2)
4396   "Sort threads by root article score."
4397   (gnus-article-sort-by-score
4398    (gnus-thread-header h1) (gnus-thread-header h2)))
4399
4400 (defun gnus-thread-sort-by-total-score (h1 h2)
4401   "Sort threads by the sum of all scores in the thread.
4402 Unscored articles will be counted as having a score of zero."
4403   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4404
4405 (defun gnus-thread-total-score (thread)
4406   ;; This function find the total score of THREAD.
4407   (cond
4408    ((null thread)
4409     0)
4410    ((consp thread)
4411     (if (stringp (car thread))
4412         (apply gnus-thread-score-function 0
4413                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4414       (gnus-thread-total-score-1 thread)))
4415    (t
4416     (gnus-thread-total-score-1 (list thread)))))
4417
4418 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4419   "Sort threads such that the thread with the most recently arrived article comes first."
4420   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4421
4422 (defun gnus-thread-highest-number (thread)
4423   "Return the highest article number in THREAD."
4424   (apply 'max (mapcar (lambda (header)
4425                         (mail-header-number header))
4426                       (message-flatten-list thread))))
4427
4428 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4429   "Sort threads such that the thread with the most recently dated article comes first."
4430   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4431
4432 (defun gnus-thread-latest-date (thread)
4433   "Return the highest article date in THREAD."
4434   (let ((previous-time 0))
4435     (apply 'max
4436            (mapcar
4437             (lambda (header)
4438               (setq previous-time
4439                     (time-to-seconds
4440                      (condition-case ()
4441                          (mail-header-parse-date (mail-header-date header))
4442                        (error previous-time)))))
4443             (sort
4444              (message-flatten-list thread)
4445              (lambda (h1 h2)
4446                (< (mail-header-number h1)
4447                   (mail-header-number h2))))))))
4448
4449 (defun gnus-thread-total-score-1 (root)
4450   ;; This function find the total score of the thread below ROOT.
4451   (setq root (car root))
4452   (apply gnus-thread-score-function
4453          (or (append
4454               (mapcar 'gnus-thread-total-score
4455                       (cdr (gnus-id-to-thread (mail-header-id root))))
4456               (when (> (mail-header-number root) 0)
4457                 (list (or (cdr (assq (mail-header-number root)
4458                                      gnus-newsgroup-scored))
4459                           gnus-summary-default-score 0))))
4460              (list gnus-summary-default-score)
4461              '(0))))
4462
4463 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4464 (defvar gnus-tmp-prev-subject nil)
4465 (defvar gnus-tmp-false-parent nil)
4466 (defvar gnus-tmp-root-expunged nil)
4467 (defvar gnus-tmp-dummy-line nil)
4468
4469 (eval-when-compile (defvar gnus-tmp-header))
4470 (defun gnus-extra-header (type &optional header)
4471   "Return the extra header of TYPE."
4472   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4473       ""))
4474
4475 (defvar gnus-tmp-thread-tree-header-string "")
4476
4477 (defcustom gnus-sum-thread-tree-root "> "
4478   "With %B spec, used for the root of a thread.
4479 If nil, use subject instead."
4480   :type 'string
4481   :group 'gnus-thread)
4482 (defcustom gnus-sum-thread-tree-single-indent ""
4483   "With %B spec, used for a thread with just one message.
4484 If nil, use subject instead."
4485   :type 'string
4486   :group 'gnus-thread)
4487 (defcustom gnus-sum-thread-tree-vertical "| "
4488   "With %B spec, used for drawing a vertical line."
4489   :type 'string
4490   :group 'gnus-thread)
4491 (defcustom gnus-sum-thread-tree-indent "  "
4492   "With %B spec, used for indenting."
4493   :type 'string
4494   :group 'gnus-thread)
4495 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4496   "With %B spec, used for a leaf with brothers."
4497   :type 'string
4498   :group 'gnus-thread)
4499 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4500   "With %B spec, used for a leaf without brothers."
4501   :type 'string
4502   :group 'gnus-thread)
4503
4504 (defun gnus-summary-prepare-threads (threads)
4505   "Prepare summary buffer from THREADS and indentation LEVEL.
4506 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4507 or a straight list of headers."
4508   (gnus-message 7 "Generating summary...")
4509
4510   (setq gnus-newsgroup-threads threads)
4511   (beginning-of-line)
4512
4513   (let ((gnus-tmp-level 0)
4514         (default-score (or gnus-summary-default-score 0))
4515         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4516         (building-line-count gnus-summary-display-while-building)
4517         (building-count (integerp gnus-summary-display-while-building))
4518         thread number subject stack state gnus-tmp-gathered beg-match
4519         new-roots gnus-tmp-new-adopts thread-end simp-subject
4520         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4521         gnus-tmp-replied gnus-tmp-subject-or-nil
4522         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4523         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4524         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4525         tree-stack)
4526
4527     (setq gnus-tmp-prev-subject nil)
4528
4529     (if (vectorp (car threads))
4530         ;; If this is a straight (sic) list of headers, then a
4531         ;; threaded summary display isn't required, so we just create
4532         ;; an unthreaded one.
4533         (gnus-summary-prepare-unthreaded threads)
4534
4535       ;; Do the threaded display.
4536
4537       (if gnus-summary-display-while-building
4538           (switch-to-buffer (buffer-name)))
4539       (while (or threads stack gnus-tmp-new-adopts new-roots)
4540
4541         (if (and (= gnus-tmp-level 0)
4542                  (or (not stack)
4543                      (= (caar stack) 0))
4544                  (not gnus-tmp-false-parent)
4545                  (or gnus-tmp-new-adopts new-roots))
4546             (if gnus-tmp-new-adopts
4547                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4548                       thread (list (car gnus-tmp-new-adopts))
4549                       gnus-tmp-header (caar thread)
4550                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4551               (when new-roots
4552                 (setq thread (list (car new-roots))
4553                       gnus-tmp-header (caar thread)
4554                       new-roots (cdr new-roots))))
4555
4556           (if threads
4557               ;; If there are some threads, we do them before the
4558               ;; threads on the stack.
4559               (setq thread threads
4560                     gnus-tmp-header (caar thread))
4561             ;; There were no current threads, so we pop something off
4562             ;; the stack.
4563             (setq state (car stack)
4564                   gnus-tmp-level (car state)
4565                   tree-stack (cadr state)
4566                   thread (caddr state)
4567                   stack (cdr stack)
4568                   gnus-tmp-header (caar thread))))
4569
4570         (setq gnus-tmp-false-parent nil)
4571         (setq gnus-tmp-root-expunged nil)
4572         (setq thread-end nil)
4573
4574         (if (stringp gnus-tmp-header)
4575             ;; The header is a dummy root.
4576             (cond
4577              ((eq gnus-summary-make-false-root 'adopt)
4578               ;; We let the first article adopt the rest.
4579               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4580                                                (cddar thread)))
4581               (setq gnus-tmp-gathered
4582                     (nconc (mapcar
4583                             (lambda (h) (mail-header-number (car h)))
4584                             (cddar thread))
4585                            gnus-tmp-gathered))
4586               (setq thread (cons (list (caar thread)
4587                                        (cadar thread))
4588                                  (cdr thread)))
4589               (setq gnus-tmp-level -1
4590                     gnus-tmp-false-parent t))
4591              ((eq gnus-summary-make-false-root 'empty)
4592               ;; We print adopted articles with empty subject fields.
4593               (setq gnus-tmp-gathered
4594                     (nconc (mapcar
4595                             (lambda (h) (mail-header-number (car h)))
4596                             (cddar thread))
4597                            gnus-tmp-gathered))
4598               (setq gnus-tmp-level -1))
4599              ((eq gnus-summary-make-false-root 'dummy)
4600               ;; We remember that we probably want to output a dummy
4601               ;; root.
4602               (setq gnus-tmp-dummy-line gnus-tmp-header)
4603               (setq gnus-tmp-prev-subject gnus-tmp-header))
4604              (t
4605               ;; We do not make a root for the gathered
4606               ;; sub-threads at all.
4607               (setq gnus-tmp-level -1)))
4608
4609           (setq number (mail-header-number gnus-tmp-header)
4610                 subject (mail-header-subject gnus-tmp-header)
4611                 simp-subject (gnus-simplify-subject-fully subject))
4612
4613           (cond
4614            ;; If the thread has changed subject, we might want to make
4615            ;; this subthread into a root.
4616            ((and (null gnus-thread-ignore-subject)
4617                  (not (zerop gnus-tmp-level))
4618                  gnus-tmp-prev-subject
4619                  (not (string= gnus-tmp-prev-subject simp-subject)))
4620             (setq new-roots (nconc new-roots (list (car thread)))
4621                   thread-end t
4622                   gnus-tmp-header nil))
4623            ;; If the article lies outside the current limit,
4624            ;; then we do not display it.
4625            ((not (memq number gnus-newsgroup-limit))
4626             (setq gnus-tmp-gathered
4627                   (nconc (mapcar
4628                           (lambda (h) (mail-header-number (car h)))
4629                           (cdar thread))
4630                          gnus-tmp-gathered))
4631             (setq gnus-tmp-new-adopts (if (cdar thread)
4632                                           (append gnus-tmp-new-adopts
4633                                                   (cdar thread))
4634                                         gnus-tmp-new-adopts)
4635                   thread-end t
4636                   gnus-tmp-header nil)
4637             (when (zerop gnus-tmp-level)
4638               (setq gnus-tmp-root-expunged t)))
4639            ;; Perhaps this article is to be marked as read?
4640            ((and gnus-summary-mark-below
4641                  (< (or (cdr (assq number gnus-newsgroup-scored))
4642                         default-score)
4643                     gnus-summary-mark-below)
4644                  ;; Don't touch sparse articles.
4645                  (not (gnus-summary-article-sparse-p number))
4646                  (not (gnus-summary-article-ancient-p number)))
4647             (setq gnus-newsgroup-unreads
4648                   (delq number gnus-newsgroup-unreads))
4649             (if gnus-newsgroup-auto-expire
4650                 (setq gnus-newsgroup-expirable
4651                       (gnus-add-to-sorted-list
4652                        gnus-newsgroup-expirable number))
4653               (push (cons number gnus-low-score-mark)
4654                     gnus-newsgroup-reads))))
4655
4656           (when gnus-tmp-header
4657             ;; We may have an old dummy line to output before this
4658             ;; article.
4659             (when (and gnus-tmp-dummy-line
4660                        (gnus-subject-equal
4661                         gnus-tmp-dummy-line
4662                         (mail-header-subject gnus-tmp-header)))
4663               (gnus-summary-insert-dummy-line
4664                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4665               (setq gnus-tmp-dummy-line nil))
4666
4667             ;; Compute the mark.
4668             (setq gnus-tmp-unread (gnus-article-mark number))
4669
4670             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4671                                   gnus-tmp-header gnus-tmp-level)
4672                   gnus-newsgroup-data)
4673
4674             ;; Actually insert the line.
4675             (setq
4676              gnus-tmp-subject-or-nil
4677              (cond
4678               ((and gnus-thread-ignore-subject
4679                     gnus-tmp-prev-subject
4680                     (not (string= gnus-tmp-prev-subject simp-subject)))
4681                subject)
4682               ((zerop gnus-tmp-level)
4683                (if (and (eq gnus-summary-make-false-root 'empty)
4684                         (memq number gnus-tmp-gathered)
4685                         gnus-tmp-prev-subject
4686                         (string= gnus-tmp-prev-subject simp-subject))
4687                    gnus-summary-same-subject
4688                  subject))
4689               (t gnus-summary-same-subject)))
4690             (if (and (eq gnus-summary-make-false-root 'adopt)
4691                      (= gnus-tmp-level 1)
4692                      (memq number gnus-tmp-gathered))
4693                 (setq gnus-tmp-opening-bracket ?\<
4694                       gnus-tmp-closing-bracket ?\>)
4695               (setq gnus-tmp-opening-bracket ?\[
4696                     gnus-tmp-closing-bracket ?\]))
4697             (setq
4698              gnus-tmp-indentation
4699              (aref gnus-thread-indent-array gnus-tmp-level)
4700              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4701              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4702                                 gnus-summary-default-score 0)
4703              gnus-tmp-score-char
4704              (if (or (null gnus-summary-default-score)
4705                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4706                          gnus-summary-zcore-fuzz))
4707                  ?\ ;;;Whitespace
4708                (if (< gnus-tmp-score gnus-summary-default-score)
4709                    gnus-score-below-mark gnus-score-over-mark))
4710              gnus-tmp-replied
4711              (cond ((memq number gnus-newsgroup-processable)
4712                     gnus-process-mark)
4713                    ((memq number gnus-newsgroup-cached)
4714                     gnus-cached-mark)
4715                    ((memq number gnus-newsgroup-replied)
4716                     gnus-replied-mark)
4717                    ((memq number gnus-newsgroup-forwarded)
4718                     gnus-forwarded-mark)
4719                    ((memq number gnus-newsgroup-saved)
4720                     gnus-saved-mark)
4721                    ((memq number gnus-newsgroup-recent)
4722                     gnus-recent-mark)
4723                    ((memq number gnus-newsgroup-unseen)
4724                     gnus-unseen-mark)
4725                    (t gnus-no-mark))
4726              gnus-tmp-downloaded
4727              (cond ((memq number gnus-newsgroup-undownloaded) 
4728                     gnus-undownloaded-mark)
4729                    (gnus-newsgroup-agentized
4730                     gnus-downloaded-mark)
4731                    (t
4732                     gnus-no-mark))
4733              gnus-tmp-from (mail-header-from gnus-tmp-header)
4734              gnus-tmp-name
4735              (cond
4736               ((string-match "<[^>]+> *$" gnus-tmp-from)
4737                (setq beg-match (match-beginning 0))
4738                (or (and (string-match "^\".+\"" gnus-tmp-from)
4739                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4740                    (substring gnus-tmp-from 0 beg-match)))
4741               ((string-match "(.+)" gnus-tmp-from)
4742                (substring gnus-tmp-from
4743                           (1+ (match-beginning 0)) (1- (match-end 0))))
4744               (t gnus-tmp-from))
4745              gnus-tmp-thread-tree-header-string
4746              (cond
4747               ((not gnus-show-threads) "")
4748               ((zerop gnus-tmp-level)
4749                (if (cdar thread)
4750                    (or gnus-sum-thread-tree-root subject)
4751                  (or gnus-sum-thread-tree-single-indent subject)))
4752               (t
4753                (concat (apply 'concat
4754                               (mapcar (lambda (item)
4755                                         (if (= item 1)
4756                                             gnus-sum-thread-tree-vertical
4757                                           gnus-sum-thread-tree-indent))
4758                                       (cdr (reverse tree-stack))))
4759                        (if (nth 1 thread)
4760                            gnus-sum-thread-tree-leaf-with-other
4761                          gnus-sum-thread-tree-single-leaf)))))
4762             (when (string= gnus-tmp-name "")
4763               (setq gnus-tmp-name gnus-tmp-from))
4764             (unless (numberp gnus-tmp-lines)
4765               (setq gnus-tmp-lines -1))
4766             (if (= gnus-tmp-lines -1)
4767                 (setq gnus-tmp-lines "?")
4768               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4769             (gnus-put-text-property
4770              (point)
4771              (progn (eval gnus-summary-line-format-spec) (point))
4772              'gnus-number number)
4773             (when gnus-visual-p
4774               (forward-line -1)
4775               (gnus-run-hooks 'gnus-summary-update-hook)
4776               (forward-line 1))
4777
4778             (setq gnus-tmp-prev-subject simp-subject)))
4779
4780         (when (nth 1 thread)
4781           (push (list (max 0 gnus-tmp-level)
4782                       (copy-list tree-stack)
4783                       (nthcdr 1 thread))
4784                 stack))
4785         (push (if (nth 1 thread) 1 0) tree-stack)
4786         (incf gnus-tmp-level)
4787         (setq threads (if thread-end nil (cdar thread)))
4788         (if gnus-summary-display-while-building
4789             (if building-count
4790                 (progn
4791                   ;; use a set frequency
4792                   (setq building-line-count (1- building-line-count))
4793                   (when (= building-line-count 0)
4794                     (sit-for 0)
4795                     (setq building-line-count
4796                           gnus-summary-display-while-building)))
4797               ;; always
4798               (sit-for 0)))
4799         (unless threads
4800           (setq gnus-tmp-level 0)))))
4801   (gnus-message 7 "Generating summary...done"))
4802
4803 (defun gnus-summary-prepare-unthreaded (headers)
4804   "Generate an unthreaded summary buffer based on HEADERS."
4805   (let (header number mark)
4806
4807     (beginning-of-line)
4808
4809     (while headers
4810       ;; We may have to root out some bad articles...
4811       (when (memq (setq number (mail-header-number
4812                                 (setq header (pop headers))))
4813                   gnus-newsgroup-limit)
4814         ;; Mark article as read when it has a low score.
4815         (when (and gnus-summary-mark-below
4816                    (< (or (cdr (assq number gnus-newsgroup-scored))
4817                           gnus-summary-default-score 0)
4818                       gnus-summary-mark-below)
4819                    (not (gnus-summary-article-ancient-p number)))
4820           (setq gnus-newsgroup-unreads
4821                 (delq number gnus-newsgroup-unreads))
4822           (if gnus-newsgroup-auto-expire
4823               (push number gnus-newsgroup-expirable)
4824             (push (cons number gnus-low-score-mark)
4825                   gnus-newsgroup-reads)))
4826
4827         (setq mark (gnus-article-mark number))
4828         (push (gnus-data-make number mark (1+ (point)) header 0)
4829               gnus-newsgroup-data)
4830         (gnus-summary-insert-line
4831          header 0 number
4832          (memq number gnus-newsgroup-undownloaded)
4833          mark (memq number gnus-newsgroup-replied)
4834          (memq number gnus-newsgroup-expirable)
4835          (mail-header-subject header) nil
4836          (cdr (assq number gnus-newsgroup-scored))
4837          (memq number gnus-newsgroup-processable))))))
4838
4839 (defun gnus-summary-remove-list-identifiers ()
4840   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4841   (let ((regexp (if (consp gnus-list-identifiers)
4842                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4843                   gnus-list-identifiers))
4844         changed subject)
4845     (when regexp
4846       (dolist (header gnus-newsgroup-headers)
4847         (setq subject (mail-header-subject header)
4848               changed nil)
4849         (while (string-match
4850                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4851                 subject)
4852           (setq subject
4853                 (concat (substring subject 0 (match-beginning 2))
4854                         (substring subject (match-end 0)))
4855                 changed t))
4856         (when (and changed
4857                    (string-match
4858                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4859           (setq subject
4860                 (concat (substring subject 0 (match-beginning 1))
4861                         (substring subject (match-end 1)))))
4862         (when changed
4863           (mail-header-set-subject header subject))))))
4864
4865 (defun gnus-fetch-headers (articles)
4866   "Fetch headers of ARTICLES."
4867   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4868     (gnus-message 5 "Fetching headers for %s..." name)
4869     (prog1
4870         (if (eq 'nov
4871                 (setq gnus-headers-retrieved-by
4872                       (gnus-retrieve-headers
4873                        articles gnus-newsgroup-name
4874                        ;; We might want to fetch old headers, but
4875                        ;; not if there is only 1 article.
4876                        (and (or (and
4877                                  (not (eq gnus-fetch-old-headers 'some))
4878                                  (not (numberp gnus-fetch-old-headers)))
4879                                 (> (length articles) 1))
4880                             gnus-fetch-old-headers))))
4881             (gnus-get-newsgroup-headers-xover
4882              articles nil nil gnus-newsgroup-name t)
4883           (gnus-get-newsgroup-headers))
4884       (gnus-message 5 "Fetching headers for %s...done" name))))
4885
4886 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4887   "Select newsgroup GROUP.
4888 If READ-ALL is non-nil, all articles in the group are selected.
4889 If SELECT-ARTICLES, only select those articles from GROUP."
4890   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4891          ;;!!! Dirty hack; should be removed.
4892          (gnus-summary-ignore-duplicates
4893           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4894               t
4895             gnus-summary-ignore-duplicates))
4896          (info (nth 2 entry))
4897          articles fetched-articles cached)
4898
4899     (unless (gnus-check-server
4900              (set (make-local-variable 'gnus-current-select-method)
4901                   (gnus-find-method-for-group group)))
4902       (error "Couldn't open server"))
4903
4904     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4905         (gnus-activate-group group)     ; Or we can activate it...
4906         (progn                          ; Or we bug out.
4907           (when (equal major-mode 'gnus-summary-mode)
4908             (kill-buffer (current-buffer)))
4909           (error "Couldn't activate group %s: %s"
4910                  group (gnus-status-message group))))
4911
4912     (unless (gnus-request-group group t)
4913       (when (equal major-mode 'gnus-summary-mode)
4914         (kill-buffer (current-buffer)))
4915       (error "Couldn't request group %s: %s"
4916              group (gnus-status-message group)))
4917
4918     (setq gnus-newsgroup-name group
4919           gnus-newsgroup-unselected nil
4920           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4921
4922     (let ((display (gnus-group-find-parameter group 'display)))
4923       (setq gnus-newsgroup-display
4924             (cond
4925              ((not (zerop (or (car-safe read-all) 0)))
4926               ;; The user entered the group with C-u SPC/RET, let's show
4927               ;; all articles.
4928               'gnus-not-ignore)
4929              ((eq display 'all)
4930               'gnus-not-ignore)
4931              ((arrayp display)
4932               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4933              ((numberp display)
4934               ;; The following is probably the "correct" solution, but
4935               ;; it makes Gnus fetch all headers and then limit the
4936               ;; articles (which is slow), so instead we hack the
4937               ;; select-articles parameter instead. -- Simon Josefsson
4938               ;; <jas@kth.se>
4939               ;;
4940               ;; (gnus-byte-compile
4941               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4942               ;;                         display)))))
4943               (setq select-articles
4944                     (gnus-uncompress-range
4945                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4946                              (if (> tmp 0)
4947                                  tmp
4948                                1))
4949                            (cdr (gnus-active group)))))
4950               nil)
4951              (t
4952               nil))))
4953
4954     (gnus-summary-setup-default-charset)
4955
4956     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4957     (when (gnus-virtual-group-p group)
4958       (setq cached gnus-newsgroup-cached))
4959
4960     (setq gnus-newsgroup-unreads
4961           (gnus-sorted-ndifference
4962            (gnus-sorted-ndifference gnus-newsgroup-unreads
4963                                     gnus-newsgroup-marked)
4964            gnus-newsgroup-dormant))
4965
4966     (setq gnus-newsgroup-processable nil)
4967
4968     (gnus-update-read-articles group gnus-newsgroup-unreads)
4969
4970     ;; Adjust and set lists of article marks.
4971     (when info
4972       (gnus-adjust-marked-articles info))
4973     (if (setq articles select-articles)
4974         (setq gnus-newsgroup-unselected
4975               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4976       (setq articles (gnus-articles-to-read group read-all)))
4977
4978     (cond
4979      ((null articles)
4980       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4981       'quit)
4982      ((eq articles 0) nil)
4983      (t
4984       ;; Init the dependencies hash table.
4985       (setq gnus-newsgroup-dependencies
4986             (gnus-make-hashtable (length articles)))
4987       (gnus-set-global-variables)
4988       ;; Retrieve the headers and read them in.
4989
4990       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4991
4992       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4993       (when cached
4994         (setq gnus-newsgroup-cached cached))
4995
4996       ;; Suppress duplicates?
4997       (when gnus-suppress-duplicates
4998         (gnus-dup-suppress-articles))
4999
5000       ;; Set the initial limit.
5001       (setq gnus-newsgroup-limit (copy-sequence articles))
5002       ;; Remove canceled articles from the list of unread articles.
5003       (setq fetched-articles
5004             (mapcar (lambda (headers) (mail-header-number headers))
5005                     gnus-newsgroup-headers))
5006       (setq gnus-newsgroup-articles fetched-articles)
5007       (setq gnus-newsgroup-unreads
5008             (gnus-sorted-nintersection
5009              gnus-newsgroup-unreads fetched-articles))
5010       (gnus-compute-unseen-list)
5011
5012       ;; Removed marked articles that do not exist.
5013       (gnus-update-missing-marks
5014        (gnus-sorted-difference articles fetched-articles))
5015       ;; We might want to build some more threads first.
5016       (when (and gnus-fetch-old-headers
5017                  (eq gnus-headers-retrieved-by 'nov))
5018         (if (eq gnus-fetch-old-headers 'invisible)
5019             (gnus-build-all-threads)
5020           (gnus-build-old-threads)))
5021       ;; Let the Gnus agent mark articles as read.
5022       (when gnus-agent
5023         (gnus-agent-get-undownloaded-list))
5024       ;; Remove list identifiers from subject
5025       (when gnus-list-identifiers
5026         (gnus-summary-remove-list-identifiers))
5027       ;; Check whether auto-expire is to be done in this group.
5028       (setq gnus-newsgroup-auto-expire
5029             (gnus-group-auto-expirable-p group))
5030       ;; Set up the article buffer now, if necessary.
5031       (unless gnus-single-article-buffer
5032         (gnus-article-setup-buffer))
5033       ;; First and last article in this newsgroup.
5034       (when gnus-newsgroup-headers
5035         (setq gnus-newsgroup-begin
5036               (mail-header-number (car gnus-newsgroup-headers))
5037               gnus-newsgroup-end
5038               (mail-header-number
5039                (gnus-last-element gnus-newsgroup-headers))))
5040       ;; GROUP is successfully selected.
5041       (or gnus-newsgroup-headers t)))))
5042
5043 (defun gnus-compute-unseen-list ()
5044   ;; The `seen' marks are treated specially.
5045   (if (not gnus-newsgroup-seen)
5046       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5047     (setq gnus-newsgroup-unseen
5048           (gnus-inverse-list-range-intersection
5049            gnus-newsgroup-articles gnus-newsgroup-seen))))
5050
5051 (defun gnus-summary-display-make-predicate (display)
5052   (require 'gnus-agent)
5053   (when (= (length display) 1)
5054     (setq display (car display)))
5055   (unless gnus-summary-display-cache
5056     (dolist (elem (append '((unread . unread)
5057                             (read . read)
5058                             (unseen . unseen))
5059                           gnus-article-mark-lists))
5060       (push (cons (cdr elem)
5061                   (gnus-byte-compile
5062                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5063             gnus-summary-display-cache)))
5064   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5065         (gnus-category-predicate-cache gnus-summary-display-cache))
5066     (gnus-get-predicate display)))
5067
5068 ;; Uses the dynamically bound `number' variable.
5069 (eval-when-compile
5070   (defvar number))
5071 (defun gnus-article-marked-p (type &optional article)
5072   (let ((article (or article number)))
5073     (cond
5074      ((eq type 'tick)
5075       (memq article gnus-newsgroup-marked))
5076      ((eq type 'spam)
5077       (memq article gnus-newsgroup-spam-marked))
5078      ((eq type 'unsend)
5079       (memq article gnus-newsgroup-unsendable))
5080      ((eq type 'undownload)
5081       (memq article gnus-newsgroup-undownloaded))
5082      ((eq type 'download)
5083       (memq article gnus-newsgroup-downloadable))
5084      ((eq type 'unread)
5085       (memq article gnus-newsgroup-unreads))
5086      ((eq type 'read)
5087       (memq article gnus-newsgroup-reads))
5088      ((eq type 'dormant)
5089       (memq article gnus-newsgroup-dormant) )
5090      ((eq type 'expire)
5091       (memq article gnus-newsgroup-expirable))
5092      ((eq type 'reply)
5093       (memq article gnus-newsgroup-replied))
5094      ((eq type 'killed)
5095       (memq article gnus-newsgroup-killed))
5096      ((eq type 'bookmark)
5097       (assq article gnus-newsgroup-bookmarks))
5098      ((eq type 'score)
5099       (assq article gnus-newsgroup-scored))
5100      ((eq type 'save)
5101       (memq article gnus-newsgroup-saved))
5102      ((eq type 'cache)
5103       (memq article gnus-newsgroup-cached))
5104      ((eq type 'forward)
5105       (memq article gnus-newsgroup-forwarded))
5106      ((eq type 'seen)
5107       (not (memq article gnus-newsgroup-unseen)))
5108      ((eq type 'recent)
5109       (memq article gnus-newsgroup-recent))
5110      (t t))))
5111
5112 (defun gnus-articles-to-read (group &optional read-all)
5113   "Find out what articles the user wants to read."
5114   (let* ((display (gnus-group-find-parameter group 'display))
5115          (articles
5116           ;; Select all articles if `read-all' is non-nil, or if there
5117           ;; are no unread articles.
5118           (if (or read-all
5119                   (and (zerop (length gnus-newsgroup-marked))
5120                        (zerop (length gnus-newsgroup-unreads)))
5121                   ;; Fetch all if the predicate is non-nil.
5122                   gnus-newsgroup-display)
5123               ;; We want to select the headers for all the articles in
5124               ;; the group, so we select either all the active
5125               ;; articles in the group, or (if that's nil), the
5126               ;; articles in the cache.
5127               (or
5128                (gnus-uncompress-range (gnus-active group))
5129                (gnus-cache-articles-in-group group))
5130             ;; Select only the "normal" subset of articles.
5131             (gnus-sorted-nunion
5132              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5133              gnus-newsgroup-unreads)))
5134          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5135          (scored (length scored-list))
5136          (number (length articles))
5137          (marked (+ (length gnus-newsgroup-marked)
5138                     (length gnus-newsgroup-dormant)))
5139          (select
5140           (cond
5141            ((numberp read-all)
5142             read-all)
5143            ((numberp gnus-newsgroup-display)
5144             gnus-newsgroup-display)
5145            (t
5146             (condition-case ()
5147                 (cond
5148                  ((and (or (<= scored marked) (= scored number))
5149                        (numberp gnus-large-newsgroup)
5150                        (> number gnus-large-newsgroup))
5151                   (let* ((cursor-in-echo-area nil)
5152                          (initial (gnus-parameter-large-newsgroup-initial
5153                                    gnus-newsgroup-name))
5154                          (input
5155                           (read-string
5156                            (format
5157                             "How many articles from %s (%s %d): "
5158                             (gnus-limit-string
5159                              (gnus-group-decoded-name gnus-newsgroup-name)
5160                              35)
5161                             (if initial "max" "default")
5162                             number)
5163                            (if initial
5164                                (cons (number-to-string initial)
5165                                      0)))))
5166                     (if (string-match "^[ \t]*$" input) number input)))
5167                  ((and (> scored marked) (< scored number)
5168                        (> (- scored number) 20))
5169                   (let ((input
5170                          (read-string
5171                           (format "%s %s (%d scored, %d total): "
5172                                   "How many articles from"
5173                                   (gnus-group-decoded-name group)
5174                                   scored number))))
5175                     (if (string-match "^[ \t]*$" input)
5176                         number input)))
5177                  (t number))
5178               (quit
5179                (message "Quit getting the articles to read")
5180                nil))))))
5181     (setq select (if (stringp select) (string-to-number select) select))
5182     (if (or (null select) (zerop select))
5183         select
5184       (if (and (not (zerop scored)) (<= (abs select) scored))
5185           (progn
5186             (setq articles (sort scored-list '<))
5187             (setq number (length articles)))
5188         (setq articles (copy-sequence articles)))
5189
5190       (when (< (abs select) number)
5191         (if (< select 0)
5192             ;; Select the N oldest articles.
5193             (setcdr (nthcdr (1- (abs select)) articles) nil)
5194           ;; Select the N most recent articles.
5195           (setq articles (nthcdr (- number select) articles))))
5196       (setq gnus-newsgroup-unselected
5197             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5198       (when gnus-alter-articles-to-read-function
5199         (setq gnus-newsgroup-unreads
5200               (sort
5201                (funcall gnus-alter-articles-to-read-function
5202                         gnus-newsgroup-name gnus-newsgroup-unreads)
5203                '<)))
5204       articles)))
5205
5206 (defun gnus-killed-articles (killed articles)
5207   (let (out)
5208     (while articles
5209       (when (inline (gnus-member-of-range (car articles) killed))
5210         (push (car articles) out))
5211       (setq articles (cdr articles)))
5212     out))
5213
5214 (defun gnus-uncompress-marks (marks)
5215   "Uncompress the mark ranges in MARKS."
5216   (let ((uncompressed '(score bookmark))
5217         out)
5218     (while marks
5219       (if (memq (caar marks) uncompressed)
5220           (push (car marks) out)
5221         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5222       (setq marks (cdr marks)))
5223     out))
5224
5225 (defun gnus-article-mark-to-type (mark)
5226   "Return the type of MARK."
5227   (or (cadr (assq mark gnus-article-special-mark-lists))
5228       'list))
5229
5230 (defun gnus-article-unpropagatable-p (mark)
5231   "Return whether MARK should be propagated to backend."
5232   (memq mark gnus-article-unpropagated-mark-lists))
5233
5234 (defun gnus-adjust-marked-articles (info)
5235   "Set all article lists and remove all marks that are no longer valid."
5236   (let* ((marked-lists (gnus-info-marks info))
5237          (active (gnus-active (gnus-info-group info)))
5238          (min (car active))
5239          (max (cdr active))
5240          (types gnus-article-mark-lists)
5241          marks var articles article mark mark-type)
5242
5243     (dolist (marks marked-lists)
5244       (setq mark (car marks)
5245             mark-type (gnus-article-mark-to-type mark)
5246             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5247
5248       ;; We set the variable according to the type of the marks list,
5249       ;; and then adjust the marks to a subset of the active articles.
5250       (cond
5251        ;; Adjust "simple" lists.
5252        ((eq mark-type 'list)
5253         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5254         (when (memq mark '(tick dormant expire reply save))
5255           (while articles
5256             (when (or (< (setq article (pop articles)) min) (> article max))
5257               (set var (delq article (symbol-value var)))))))
5258        ;; Adjust assocs.
5259        ((eq mark-type 'tuple)
5260         (set var (setq articles (cdr marks)))
5261         (when (not (listp (cdr (symbol-value var))))
5262           (set var (list (symbol-value var))))
5263         (when (not (listp (cdr articles)))
5264           (setq articles (list articles)))
5265         (while articles
5266           (when (or (not (consp (setq article (pop articles))))
5267                     (< (car article) min)
5268                     (> (car article) max))
5269             (set var (delq article (symbol-value var))))))
5270        ;; Adjust ranges (sloppily).
5271        ((eq mark-type 'range)
5272         (cond
5273          ((eq mark 'seen)
5274           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5275           ;; It should be (seen (NUM1 . NUM2)).
5276           (when (numberp (cddr marks))
5277             (setcdr marks (list (cdr marks))))
5278           (setq articles (cdr marks))
5279           (while (and articles
5280                       (or (and (consp (car articles))
5281                                (> min (cdar articles)))
5282                           (and (numberp (car articles))
5283                                (> min (car articles)))))
5284             (pop articles))
5285           (set var articles))))))))
5286
5287 (defun gnus-update-missing-marks (missing)
5288   "Go through the list of MISSING articles and remove them from the mark lists."
5289   (when missing
5290     (let (var m)
5291       ;; Go through all types.
5292       (dolist (elem gnus-article-mark-lists)
5293         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5294           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5295           (when (symbol-value var)
5296             ;; This list has articles.  So we delete all missing
5297             ;; articles from it.
5298             (setq m missing)
5299             (while m
5300               (set var (delq (pop m) (symbol-value var))))))))))
5301
5302 (defun gnus-update-marks ()
5303   "Enter the various lists of marked articles into the newsgroup info list."
5304   (let ((types gnus-article-mark-lists)
5305         (info (gnus-get-info gnus-newsgroup-name))
5306         type list newmarked symbol delta-marks)
5307     (when info
5308       ;; Add all marks lists to the list of marks lists.
5309       (while (setq type (pop types))
5310         (setq list (symbol-value
5311                     (setq symbol
5312                           (intern (format "gnus-newsgroup-%s" (car type))))))
5313
5314         (when list
5315           ;; Get rid of the entries of the articles that have the
5316           ;; default score.
5317           (when (and (eq (cdr type) 'score)
5318                      gnus-save-score
5319                      list)
5320             (let* ((arts list)
5321                    (prev (cons nil list))
5322                    (all prev))
5323               (while arts
5324                 (if (or (not (consp (car arts)))
5325                         (= (cdar arts) gnus-summary-default-score))
5326                     (setcdr prev (cdr arts))
5327                   (setq prev arts))
5328                 (setq arts (cdr arts)))
5329               (setq list (cdr all)))))
5330
5331         (when (eq (cdr type) 'seen)
5332           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5333
5334         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5335           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5336
5337         (when (and (gnus-check-backend-function
5338                     'request-set-mark gnus-newsgroup-name)
5339                    (not (gnus-article-unpropagatable-p (cdr type))))
5340           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5341                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5342                  (add (gnus-remove-from-range
5343                        (gnus-copy-sequence list) old)))
5344             (when add
5345               (push (list add 'add (list (cdr type))) delta-marks))
5346             (when del
5347               (push (list del 'del (list (cdr type))) delta-marks))))
5348
5349         (when list
5350           (push (cons (cdr type) list) newmarked)))
5351
5352       (when delta-marks
5353         (unless (gnus-check-group gnus-newsgroup-name)
5354           (error "Can't open server for %s" gnus-newsgroup-name))
5355         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5356
5357       ;; Enter these new marks into the info of the group.
5358       (if (nthcdr 3 info)
5359           (setcar (nthcdr 3 info) newmarked)
5360         ;; Add the marks lists to the end of the info.
5361         (when newmarked
5362           (setcdr (nthcdr 2 info) (list newmarked))))
5363
5364       ;; Cut off the end of the info if there's nothing else there.
5365       (let ((i 5))
5366         (while (and (> i 2)
5367                     (not (nth i info)))
5368           (when (nthcdr (decf i) info)
5369             (setcdr (nthcdr i info) nil)))))))
5370
5371 (defun gnus-set-mode-line (where)
5372   "Set the mode line of the article or summary buffers.
5373 If WHERE is `summary', the summary mode line format will be used."
5374   ;; Is this mode line one we keep updated?
5375   (when (and (memq where gnus-updated-mode-lines)
5376              (symbol-value
5377               (intern (format "gnus-%s-mode-line-format-spec" where))))
5378     (let (mode-string)
5379       (save-excursion
5380         ;; We evaluate this in the summary buffer since these
5381         ;; variables are buffer-local to that buffer.
5382         (set-buffer gnus-summary-buffer)
5383         ;; We bind all these variables that are used in the `eval' form
5384         ;; below.
5385         (let* ((mformat (symbol-value
5386                          (intern
5387                           (format "gnus-%s-mode-line-format-spec" where))))
5388                (gnus-tmp-group-name (gnus-group-decoded-name
5389                                      gnus-newsgroup-name))
5390                (gnus-tmp-article-number (or gnus-current-article 0))
5391                (gnus-tmp-unread gnus-newsgroup-unreads)
5392                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5393                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5394                (gnus-tmp-unread-and-unselected
5395                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5396                             (zerop gnus-tmp-unselected))
5397                        "")
5398                       ((zerop gnus-tmp-unselected)
5399                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5400                       (t (format "{%d(+%d) more}"
5401                                  gnus-tmp-unread-and-unticked
5402                                  gnus-tmp-unselected))))
5403                (gnus-tmp-subject
5404                 (if (and gnus-current-headers
5405                          (vectorp gnus-current-headers))
5406                     (gnus-mode-string-quote
5407                      (mail-header-subject gnus-current-headers))
5408                   ""))
5409                bufname-length max-len
5410                gnus-tmp-header);; passed as argument to any user-format-funcs
5411           (setq mode-string (eval mformat))
5412           (setq bufname-length (if (string-match "%b" mode-string)
5413                                    (- (length
5414                                        (buffer-name
5415                                         (if (eq where 'summary)
5416                                             nil
5417                                           (get-buffer gnus-article-buffer))))
5418                                       2)
5419                                  0))
5420           (setq max-len (max 4 (if gnus-mode-non-string-length
5421                                    (- (window-width)
5422                                       gnus-mode-non-string-length
5423                                       bufname-length)
5424                                  (length mode-string))))
5425           ;; We might have to chop a bit of the string off...
5426           (when (> (length mode-string) max-len)
5427             (setq mode-string
5428                   (concat (gnus-truncate-string mode-string (- max-len 3))
5429                           "...")))
5430           ;; Pad the mode string a bit.
5431           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5432       ;; Update the mode line.
5433       (setq mode-line-buffer-identification
5434             (gnus-mode-line-buffer-identification (list mode-string)))
5435       (set-buffer-modified-p t))))
5436
5437 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5438   "Go through the HEADERS list and add all Xrefs to a hash table.
5439 The resulting hash table is returned, or nil if no Xrefs were found."
5440   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5441          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5442          (xref-hashtb (gnus-make-hashtable))
5443          start group entry number xrefs header)
5444     (while headers
5445       (setq header (pop headers))
5446       (when (and (setq xrefs (mail-header-xref header))
5447                  (not (memq (setq number (mail-header-number header))
5448                             unreads)))
5449         (setq start 0)
5450         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5451           (setq start (match-end 0))
5452           (setq group (if prefix
5453                           (concat prefix (substring xrefs (match-beginning 1)
5454                                                     (match-end 1)))
5455                         (substring xrefs (match-beginning 1) (match-end 1))))
5456           (setq number
5457                 (string-to-int (substring xrefs (match-beginning 2)
5458                                           (match-end 2))))
5459           (if (setq entry (gnus-gethash group xref-hashtb))
5460               (setcdr entry (cons number (cdr entry)))
5461             (gnus-sethash group (cons number nil) xref-hashtb)))))
5462     (and start xref-hashtb)))
5463
5464 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5465   "Look through all the headers and mark the Xrefs as read."
5466   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5467         name entry info xref-hashtb idlist method nth4)
5468     (save-excursion
5469       (set-buffer gnus-group-buffer)
5470       (when (setq xref-hashtb
5471                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5472         (mapatoms
5473          (lambda (group)
5474            (unless (string= from-newsgroup (setq name (symbol-name group)))
5475              (setq idlist (symbol-value group))
5476              ;; Dead groups are not updated.
5477              (and (prog1
5478                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5479                             info (nth 2 entry))
5480                     (when (stringp (setq nth4 (gnus-info-method info)))
5481                       (setq nth4 (gnus-server-to-method nth4))))
5482                   ;; Only do the xrefs if the group has the same
5483                   ;; select method as the group we have just read.
5484                   (or (gnus-methods-equal-p
5485                        nth4 (gnus-find-method-for-group from-newsgroup))
5486                       virtual
5487                       (equal nth4 (setq method (gnus-find-method-for-group
5488                                                 from-newsgroup)))
5489                       (and (equal (car nth4) (car method))
5490                            (equal (nth 1 nth4) (nth 1 method))))
5491                   gnus-use-cross-reference
5492                   (or (not (eq gnus-use-cross-reference t))
5493                       virtual
5494                       ;; Only do cross-references on subscribed
5495                       ;; groups, if that is what is wanted.
5496                       (<= (gnus-info-level info) gnus-level-subscribed))
5497                   (gnus-group-make-articles-read name idlist))))
5498          xref-hashtb)))))
5499
5500 (defun gnus-compute-read-articles (group articles)
5501   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5502          (info (nth 2 entry))
5503          (active (gnus-active group))
5504          ninfo)
5505     (when entry
5506       ;; First peel off all invalid article numbers.
5507       (when active
5508         (let ((ids articles)
5509               id first)
5510           (while (setq id (pop ids))
5511             (when (and first (> id (cdr active)))
5512               ;; We'll end up in this situation in one particular
5513               ;; obscure situation.  If you re-scan a group and get
5514               ;; a new article that is cross-posted to a different
5515               ;; group that has not been re-scanned, you might get
5516               ;; crossposted article that has a higher number than
5517               ;; Gnus believes possible.  So we re-activate this
5518               ;; group as well.  This might mean doing the
5519               ;; crossposting thingy will *increase* the number
5520               ;; of articles in some groups.  Tsk, tsk.
5521               (setq active (or (gnus-activate-group group) active)))
5522             (when (or (> id (cdr active))
5523                       (< id (car active)))
5524               (setq articles (delq id articles))))))
5525       ;; If the read list is nil, we init it.
5526       (if (and active
5527                (null (gnus-info-read info))
5528                (> (car active) 1))
5529           (setq ninfo (cons 1 (1- (car active))))
5530         (setq ninfo (gnus-info-read info)))
5531       ;; Then we add the read articles to the range.
5532       (gnus-add-to-range
5533        ninfo (setq articles (sort articles '<))))))
5534
5535 (defun gnus-group-make-articles-read (group articles)
5536   "Update the info of GROUP to say that ARTICLES are read."
5537   (let* ((num 0)
5538          (entry (gnus-gethash group gnus-newsrc-hashtb))
5539          (info (nth 2 entry))
5540          (active (gnus-active group))
5541          range)
5542     (when entry
5543       (setq range (gnus-compute-read-articles group articles))
5544       (save-excursion
5545         (set-buffer gnus-group-buffer)
5546         (gnus-undo-register
5547           `(progn
5548              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5549              (gnus-info-set-read ',info ',(gnus-info-read info))
5550              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5551              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5552              (gnus-group-update-group ,group t))))
5553       ;; Add the read articles to the range.
5554       (gnus-info-set-read info range)
5555       (gnus-request-set-mark group (list (list range 'add '(read))))
5556       ;; Then we have to re-compute how many unread
5557       ;; articles there are in this group.
5558       (when active
5559         (cond
5560          ((not range)
5561           (setq num (- (1+ (cdr active)) (car active))))
5562          ((not (listp (cdr range)))
5563           (setq num (- (cdr active) (- (1+ (cdr range))
5564                                        (car range)))))
5565          (t
5566           (while range
5567             (if (numberp (car range))
5568                 (setq num (1+ num))
5569               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5570             (setq range (cdr range)))
5571           (setq num (- (cdr active) num))))
5572         ;; Update the number of unread articles.
5573         (setcar entry num)
5574         ;; Update the group buffer.
5575         (gnus-group-update-group group t)))))
5576
5577 (defvar gnus-newsgroup-none-id 0)
5578
5579 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5580   (let ((cur nntp-server-buffer)
5581         (dependencies
5582          (or dependencies
5583              (save-excursion (set-buffer gnus-summary-buffer)
5584                              gnus-newsgroup-dependencies)))
5585         headers id end ref
5586         (mail-parse-charset gnus-newsgroup-charset)
5587         (mail-parse-ignored-charsets
5588          (save-excursion (condition-case nil
5589                              (set-buffer gnus-summary-buffer)
5590                            (error))
5591                          gnus-newsgroup-ignored-charsets)))
5592     (save-excursion
5593       (set-buffer nntp-server-buffer)
5594       ;; Translate all TAB characters into SPACE characters.
5595       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5596       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5597       (gnus-run-hooks 'gnus-parse-headers-hook)
5598       (let ((case-fold-search t)
5599             in-reply-to header p lines chars ctype)
5600         (goto-char (point-min))
5601         ;; Search to the beginning of the next header.  Error messages
5602         ;; do not begin with 2 or 3.
5603         (while (re-search-forward "^[23][0-9]+ " nil t)
5604           (setq id nil
5605                 ref nil)
5606           ;; This implementation of this function, with nine
5607           ;; search-forwards instead of the one re-search-forward and
5608           ;; a case (which basically was the old function) is actually
5609           ;; about twice as fast, even though it looks messier.  You
5610           ;; can't have everything, I guess.  Speed and elegance
5611           ;; doesn't always go hand in hand.
5612           (setq
5613            header
5614            (make-full-mail-header
5615             ;; Number.
5616             (prog1
5617                 (read cur)
5618               (end-of-line)
5619               (setq p (point))
5620               (narrow-to-region (point)
5621                                 (or (and (search-forward "\n.\n" nil t)
5622                                          (- (point) 2))
5623                                     (point))))
5624             ;; Subject.
5625             (progn
5626               (goto-char p)
5627               (if (search-forward "\nsubject:" nil t)
5628                   (nnheader-header-value)
5629                 "(none)"))
5630             ;; From.
5631             (progn
5632               (goto-char p)
5633               (if (search-forward "\nfrom:" nil t)
5634                   (nnheader-header-value)
5635                 "(nobody)"))
5636             ;; Date.
5637             (progn
5638               (goto-char p)
5639               (if (search-forward "\ndate:" nil t)
5640                   (nnheader-header-value) ""))
5641             ;; Message-ID.
5642             (progn
5643               (goto-char p)
5644               (setq id (if (re-search-forward
5645                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5646                            ;; We do it this way to make sure the Message-ID
5647                            ;; is (somewhat) syntactically valid.
5648                            (buffer-substring (match-beginning 1)
5649                                              (match-end 1))
5650                          ;; If there was no message-id, we just fake one
5651                          ;; to make subsequent routines simpler.
5652                          (nnheader-generate-fake-message-id))))
5653             ;; References.
5654             (progn
5655               (goto-char p)
5656               (if (search-forward "\nreferences:" nil t)
5657                   (progn
5658                     (setq end (point))
5659                     (prog1
5660                         (nnheader-header-value)
5661                       (setq ref
5662                             (buffer-substring
5663                              (progn
5664                                ;; (end-of-line)
5665                                (search-backward ">" end t)
5666                                (1+ (point)))
5667                              (progn
5668                                (search-backward "<" end t)
5669                                (point))))))
5670                 ;; Get the references from the in-reply-to header if there
5671                 ;; were no references and the in-reply-to header looks
5672                 ;; promising.
5673                 (if (and (search-forward "\nin-reply-to:" nil t)
5674                          (setq in-reply-to (nnheader-header-value))
5675                          (string-match "<[^>]+>" in-reply-to))
5676                     (let (ref2)
5677                       (setq ref (substring in-reply-to (match-beginning 0)
5678                                            (match-end 0)))
5679                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5680                         (setq ref2 (substring in-reply-to (match-beginning 0)
5681                                               (match-end 0)))
5682                         (when (> (length ref2) (length ref))
5683                           (setq ref ref2)))
5684                       ref)
5685                   (setq ref nil))))
5686             ;; Chars.
5687             (progn
5688               (goto-char p)
5689               (if (search-forward "\nchars: " nil t)
5690                   (if (numberp (setq chars (ignore-errors (read cur))))
5691                       chars -1)
5692                 -1))
5693             ;; Lines.
5694             (progn
5695               (goto-char p)
5696               (if (search-forward "\nlines: " nil t)
5697                   (if (numberp (setq lines (ignore-errors (read cur))))
5698                       lines -1)
5699                 -1))
5700             ;; Xref.
5701             (progn
5702               (goto-char p)
5703               (and (search-forward "\nxref:" nil t)
5704                    (nnheader-header-value)))
5705             ;; Extra.
5706             (when gnus-extra-headers
5707               (let ((extra gnus-extra-headers)
5708                     out)
5709                 (while extra
5710                   (goto-char p)
5711                   (when (search-forward
5712                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5713                     (push (cons (car extra) (nnheader-header-value)) out))
5714                   (pop extra))
5715                 out))))
5716           (goto-char p)
5717           (if (and (search-forward "\ncontent-type: " nil t)
5718                    (setq ctype (nnheader-header-value)))
5719               (mime-entity-set-content-type-internal
5720                header (mime-parse-Content-Type ctype)))
5721           (when (equal id ref)
5722             (setq ref nil))
5723
5724           (when gnus-alter-header-function
5725             (funcall gnus-alter-header-function header)
5726             (setq id (mail-header-id header)
5727                   ref (gnus-parent-id (mail-header-references header))))
5728
5729           (when (setq header
5730                       (gnus-dependencies-add-header
5731                        header dependencies force-new))
5732             (push header headers))
5733           (goto-char (point-max))
5734           (widen))
5735         (nreverse headers)))))
5736
5737 ;; Goes through the xover lines and returns a list of vectors
5738 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5739                                                   force-new dependencies
5740                                                   group also-fetch-heads)
5741   "Parse the news overview data in the server buffer.
5742 Return a list of headers that match SEQUENCE (see
5743 `nntp-retrieve-headers')."
5744   ;; Get the Xref when the users reads the articles since most/some
5745   ;; NNTP servers do not include Xrefs when using XOVER.
5746   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5747   (let ((mail-parse-charset gnus-newsgroup-charset)
5748         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5749         (cur nntp-server-buffer)
5750         (dependencies (or dependencies gnus-newsgroup-dependencies))
5751         (allp (cond
5752                ((eq gnus-read-all-available-headers t)
5753                 t)
5754                ((stringp gnus-read-all-available-headers)
5755                 (string-match gnus-read-all-available-headers group))
5756                (t
5757                 nil)))
5758         number headers header)
5759     (save-excursion
5760       (set-buffer nntp-server-buffer)
5761       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5762       ;; Allow the user to mangle the headers before parsing them.
5763       (gnus-run-hooks 'gnus-parse-headers-hook)
5764       (goto-char (point-min))
5765       (gnus-parse-without-error
5766         (while (and (or sequence allp)
5767                     (not (eobp)))
5768           (setq number (read cur))
5769           (when (not allp)
5770             (while (and sequence
5771                         (< (car sequence) number))
5772               (setq sequence (cdr sequence))))
5773           (when (and (or allp
5774                          (and sequence
5775                               (eq number (car sequence))))
5776                      (progn
5777                        (setq sequence (cdr sequence))
5778                        (setq header (inline
5779                                       (gnus-nov-parse-line
5780                                        number dependencies force-new)))))
5781             (push header headers))
5782           (forward-line 1)))
5783       ;; A common bug in inn is that if you have posted an article and
5784       ;; then retrieves the active file, it will answer correctly --
5785       ;; the new article is included.  However, a NOV entry for the
5786       ;; article may not have been generated yet, so this may fail.
5787       ;; We work around this problem by retrieving the last few
5788       ;; headers using HEAD.
5789       (if (or (not also-fetch-heads)
5790               (not sequence))
5791           ;; We (probably) got all the headers.
5792           (nreverse headers)
5793         (let ((gnus-nov-is-evil t))
5794           (nconc
5795            (nreverse headers)
5796            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5797              (gnus-get-newsgroup-headers))))))))
5798
5799 (defun gnus-article-get-xrefs ()
5800   "Fill in the Xref value in `gnus-current-headers', if necessary.
5801 This is meant to be called in `gnus-article-internal-prepare-hook'."
5802   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5803                                  gnus-current-headers)))
5804     (or (not gnus-use-cross-reference)
5805         (not headers)
5806         (and (mail-header-xref headers)
5807              (not (string= (mail-header-xref headers) "")))
5808         (let ((case-fold-search t)
5809               xref)
5810           (save-restriction
5811             (nnheader-narrow-to-headers)
5812             (goto-char (point-min))
5813             (when (or (and (not (eobp))
5814                            (eq (downcase (char-after)) ?x)
5815                            (looking-at "Xref:"))
5816                       (search-forward "\nXref:" nil t))
5817               (goto-char (1+ (match-end 0)))
5818               (setq xref (buffer-substring (point)
5819                                            (progn (end-of-line) (point))))
5820               (mail-header-set-xref headers xref)))))))
5821
5822 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5823   "Find article ID and insert the summary line for that article.
5824 OLD-HEADER can either be a header or a line number to insert
5825 the subject line on."
5826   (let* ((line (and (numberp old-header) old-header))
5827          (old-header (and (vectorp old-header) old-header))
5828          (header (cond ((and old-header use-old-header)
5829                         old-header)
5830                        ((and (numberp id)
5831                              (gnus-number-to-header id))
5832                         (gnus-number-to-header id))
5833                        (t
5834                         (gnus-read-header id))))
5835          (number (and (numberp id) id))
5836          d)
5837     (when header
5838       ;; Rebuild the thread that this article is part of and go to the
5839       ;; article we have fetched.
5840       (when (and (not gnus-show-threads)
5841                  old-header)
5842         (when (and number
5843                    (setq d (gnus-data-find (mail-header-number old-header))))
5844           (goto-char (gnus-data-pos d))
5845           (gnus-data-remove
5846            number
5847            (- (gnus-point-at-bol)
5848               (prog1
5849                   (1+ (gnus-point-at-eol))
5850                 (gnus-delete-line))))))
5851       (when old-header
5852         (mail-header-set-number header (mail-header-number old-header)))
5853       (setq gnus-newsgroup-sparse
5854             (delq (setq number (mail-header-number header))
5855                   gnus-newsgroup-sparse))
5856       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5857       (push number gnus-newsgroup-limit)
5858       (gnus-rebuild-thread (mail-header-id header) line)
5859       (gnus-summary-goto-subject number nil t))
5860     (when (and (numberp number)
5861                (> number 0))
5862       ;; We have to update the boundaries even if we can't fetch the
5863       ;; article if ID is a number -- so that the next `P' or `N'
5864       ;; command will fetch the previous (or next) article even
5865       ;; if the one we tried to fetch this time has been canceled.
5866       (when (> number gnus-newsgroup-end)
5867         (setq gnus-newsgroup-end number))
5868       (when (< number gnus-newsgroup-begin)
5869         (setq gnus-newsgroup-begin number))
5870       (setq gnus-newsgroup-unselected
5871             (delq number gnus-newsgroup-unselected)))
5872     ;; Report back a success?
5873     (and header (mail-header-number header))))
5874
5875 ;;; Process/prefix in the summary buffer
5876
5877 (defun gnus-summary-work-articles (n)
5878   "Return a list of articles to be worked upon.
5879 The prefix argument, the list of process marked articles, and the
5880 current article will be taken into consideration."
5881   (save-excursion
5882     (set-buffer gnus-summary-buffer)
5883     (cond
5884      (n
5885       ;; A numerical prefix has been given.
5886       (setq n (prefix-numeric-value n))
5887       (let ((backward (< n 0))
5888             (n (abs (prefix-numeric-value n)))
5889             articles article)
5890         (save-excursion
5891           (while
5892               (and (> n 0)
5893                    (push (setq article (gnus-summary-article-number))
5894                          articles)
5895                    (if backward
5896                        (gnus-summary-find-prev nil article)
5897                      (gnus-summary-find-next nil article)))
5898             (decf n)))
5899         (nreverse articles)))
5900      ((and (gnus-region-active-p) (mark))
5901       (message "region active")
5902       ;; Work on the region between point and mark.
5903       (let ((max (max (point) (mark)))
5904             articles article)
5905         (save-excursion
5906           (goto-char (min (point) (mark)))
5907           (while
5908               (and
5909                (push (setq article (gnus-summary-article-number)) articles)
5910                (gnus-summary-find-next nil article)
5911                (< (point) max)))
5912           (nreverse articles))))
5913      (gnus-newsgroup-processable
5914       ;; There are process-marked articles present.
5915       ;; Save current state.
5916       (gnus-summary-save-process-mark)
5917       ;; Return the list.
5918       (reverse gnus-newsgroup-processable))
5919      (t
5920       ;; Just return the current article.
5921       (list (gnus-summary-article-number))))))
5922
5923 (defmacro gnus-summary-iterate (arg &rest forms)
5924   "Iterate over the process/prefixed articles and do FORMS.
5925 ARG is the interactive prefix given to the command.  FORMS will be
5926 executed with point over the summary line of the articles."
5927   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5928     `(let ((,articles (gnus-summary-work-articles ,arg)))
5929        (while ,articles
5930          (gnus-summary-goto-subject (car ,articles))
5931          ,@forms
5932          (pop ,articles)))))
5933
5934 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5935 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5936
5937 (defun gnus-summary-save-process-mark ()
5938   "Push the current set of process marked articles on the stack."
5939   (interactive)
5940   (push (copy-sequence gnus-newsgroup-processable)
5941         gnus-newsgroup-process-stack))
5942
5943 (defun gnus-summary-kill-process-mark ()
5944   "Push the current set of process marked articles on the stack and unmark."
5945   (interactive)
5946   (gnus-summary-save-process-mark)
5947   (gnus-summary-unmark-all-processable))
5948
5949 (defun gnus-summary-yank-process-mark ()
5950   "Pop the last process mark state off the stack and restore it."
5951   (interactive)
5952   (unless gnus-newsgroup-process-stack
5953     (error "Empty mark stack"))
5954   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5955
5956 (defun gnus-summary-process-mark-set (set)
5957   "Make SET into the current process marked articles."
5958   (gnus-summary-unmark-all-processable)
5959   (while set
5960     (gnus-summary-set-process-mark (pop set))))
5961
5962 ;;; Searching and stuff
5963
5964 (defun gnus-summary-search-group (&optional backward use-level)
5965   "Search for next unread newsgroup.
5966 If optional argument BACKWARD is non-nil, search backward instead."
5967   (save-excursion
5968     (set-buffer gnus-group-buffer)
5969     (when (gnus-group-search-forward
5970            backward nil (if use-level (gnus-group-group-level) nil))
5971       (gnus-group-group-name))))
5972
5973 (defun gnus-summary-best-group (&optional exclude-group)
5974   "Find the name of the best unread group.
5975 If EXCLUDE-GROUP, do not go to this group."
5976   (save-excursion
5977     (set-buffer gnus-group-buffer)
5978     (save-excursion
5979       (gnus-group-best-unread-group exclude-group))))
5980
5981 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5982   (if backward (gnus-summary-find-prev)
5983     (let* ((dummy (gnus-summary-article-intangible-p))
5984            (article (or article (gnus-summary-article-number)))
5985            (arts (gnus-data-find-list article))
5986            result)
5987       (when (and (not dummy)
5988                  (or (not gnus-summary-check-current)
5989                      (not unread)
5990                      (not (gnus-data-unread-p (car arts)))))
5991         (setq arts (cdr arts)))
5992       (when (setq result
5993                   (if unread
5994                       (progn
5995                         (while arts
5996                           (when (or (and undownloaded
5997                                          (memq (car arts) gnus-newsgroup-undownloaded))
5998                                     (gnus-data-unread-p (car arts)))
5999                             (setq result (car arts)
6000                                   arts nil))
6001                           (setq arts (cdr arts)))
6002                         result)
6003                     (car arts)))
6004         (goto-char (gnus-data-pos result))
6005         (gnus-data-number result)))))
6006
6007 (defun gnus-summary-find-prev (&optional unread article)
6008   (let* ((eobp (eobp))
6009          (article (or article (gnus-summary-article-number)))
6010          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6011          result)
6012     (when (and (not eobp)
6013                (or (not gnus-summary-check-current)
6014                    (not unread)
6015                    (not (gnus-data-unread-p (car arts)))))
6016       (setq arts (cdr arts)))
6017     (when (setq result
6018                 (if unread
6019                     (progn
6020                       (while arts
6021                         (when (gnus-data-unread-p (car arts))
6022                           (setq result (car arts)
6023                                 arts nil))
6024                         (setq arts (cdr arts)))
6025                       result)
6026                   (car arts)))
6027       (goto-char (gnus-data-pos result))
6028       (gnus-data-number result))))
6029
6030 (defun gnus-summary-find-subject (subject &optional unread backward article)
6031   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6032          (article (or article (gnus-summary-article-number)))
6033          (articles (gnus-data-list backward))
6034          (arts (gnus-data-find-list article articles))
6035          result)
6036     (when (or (not gnus-summary-check-current)
6037               (not unread)
6038               (not (gnus-data-unread-p (car arts))))
6039       (setq arts (cdr arts)))
6040     (while arts
6041       (and (or (not unread)
6042                (gnus-data-unread-p (car arts)))
6043            (vectorp (gnus-data-header (car arts)))
6044            (gnus-subject-equal
6045             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6046            (setq result (car arts)
6047                  arts nil))
6048       (setq arts (cdr arts)))
6049     (and result
6050          (goto-char (gnus-data-pos result))
6051          (gnus-data-number result))))
6052
6053 (defun gnus-summary-search-forward (&optional unread subject backward)
6054   "Search forward for an article.
6055 If UNREAD, look for unread articles.  If SUBJECT, look for
6056 articles with that subject.  If BACKWARD, search backward instead."
6057   (cond (subject (gnus-summary-find-subject subject unread backward))
6058         (backward (gnus-summary-find-prev unread))
6059         (t (gnus-summary-find-next unread))))
6060
6061 (defun gnus-recenter (&optional n)
6062   "Center point in window and redisplay frame.
6063 Also do horizontal recentering."
6064   (interactive "P")
6065   (when (and gnus-auto-center-summary
6066              (not (eq gnus-auto-center-summary 'vertical)))
6067     (gnus-horizontal-recenter))
6068   (recenter n))
6069
6070 (defun gnus-summary-recenter ()
6071   "Center point in the summary window.
6072 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6073 displayed, no centering will be performed."
6074   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6075   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6076   (interactive)
6077   (let* ((top (cond ((< (window-height) 4) 0)
6078                     ((< (window-height) 7) 1)
6079                     (t (if (numberp gnus-auto-center-summary)
6080                            gnus-auto-center-summary
6081                          2))))
6082          (height (1- (window-height)))
6083          (bottom (save-excursion (goto-char (point-max))
6084                                  (forward-line (- height))
6085                                  (point)))
6086          (window (get-buffer-window (current-buffer))))
6087     ;; The user has to want it.
6088     (when gnus-auto-center-summary
6089       (when (get-buffer-window gnus-article-buffer)
6090         ;; Only do recentering when the article buffer is displayed,
6091         ;; Set the window start to either `bottom', which is the biggest
6092         ;; possible valid number, or the second line from the top,
6093         ;; whichever is the least.
6094         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6095           (if (> bottom top-pos)
6096               ;; Keep the second line from the top visible
6097               (set-window-start window top-pos t)
6098             ;; Try to keep the bottom line visible; if it's partially
6099             ;; obscured, either scroll one more line to make it fully
6100             ;; visible, or revert to using TOP-POS.
6101             (save-excursion
6102               (goto-char (point-max))
6103               (forward-line -1)
6104               (let ((last-line-start (point)))
6105                 (goto-char bottom)
6106                 (set-window-start window (point) t)
6107                 (when (not (pos-visible-in-window-p last-line-start window))
6108                   (forward-line 1)
6109                   (set-window-start window (min (point) top-pos) t)))))))
6110       ;; Do horizontal recentering while we're at it.
6111       (when (and (get-buffer-window (current-buffer) t)
6112                  (not (eq gnus-auto-center-summary 'vertical)))
6113         (let ((selected (selected-window)))
6114           (select-window (get-buffer-window (current-buffer) t))
6115           (gnus-summary-position-point)
6116           (gnus-horizontal-recenter)
6117           (select-window selected))))))
6118
6119 (defun gnus-summary-jump-to-group (newsgroup)
6120   "Move point to NEWSGROUP in group mode buffer."
6121   ;; Keep update point of group mode buffer if visible.
6122   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6123       (save-window-excursion
6124         ;; Take care of tree window mode.
6125         (when (get-buffer-window gnus-group-buffer)
6126           (pop-to-buffer gnus-group-buffer))
6127         (gnus-group-jump-to-group newsgroup))
6128     (save-excursion
6129       ;; Take care of tree window mode.
6130       (if (get-buffer-window gnus-group-buffer)
6131           (pop-to-buffer gnus-group-buffer)
6132         (set-buffer gnus-group-buffer))
6133       (gnus-group-jump-to-group newsgroup))))
6134
6135 ;; This function returns a list of article numbers based on the
6136 ;; difference between the ranges of read articles in this group and
6137 ;; the range of active articles.
6138 (defun gnus-list-of-unread-articles (group)
6139   (let* ((read (gnus-info-read (gnus-get-info group)))
6140          (active (or (gnus-active group) (gnus-activate-group group)))
6141          (last (cdr active))
6142          first nlast unread)
6143     ;; If none are read, then all are unread.
6144     (if (not read)
6145         (setq first (car active))
6146       ;; If the range of read articles is a single range, then the
6147       ;; first unread article is the article after the last read
6148       ;; article.  Sounds logical, doesn't it?
6149       (if (and (not (listp (cdr read)))
6150                (or (< (car read) (car active))
6151                    (progn (setq read (list read))
6152                           nil)))
6153           (setq first (max (car active) (1+ (cdr read))))
6154         ;; `read' is a list of ranges.
6155         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6156                                   (caar read)))
6157                   1)
6158           (setq first (car active)))
6159         (while read
6160           (when first
6161             (while (< first nlast)
6162               (push first unread)
6163               (setq first (1+ first))))
6164           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6165           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6166           (setq read (cdr read)))))
6167     ;; And add the last unread articles.
6168     (while (<= first last)
6169       (push first unread)
6170       (setq first (1+ first)))
6171     ;; Return the list of unread articles.
6172     (delq 0 (nreverse unread))))
6173
6174 (defun gnus-list-of-read-articles (group)
6175   "Return a list of unread, unticked and non-dormant articles."
6176   (let* ((info (gnus-get-info group))
6177          (marked (gnus-info-marks info))
6178          (active (gnus-active group)))
6179     (and info active
6180          (gnus-list-range-difference
6181           (gnus-list-range-difference
6182            (gnus-sorted-complement
6183             (gnus-uncompress-range active)
6184             (gnus-list-of-unread-articles group))
6185            (cdr (assq 'dormant marked)))
6186           (cdr (assq 'tick marked))))))
6187
6188 ;; Various summary commands
6189
6190 (defun gnus-summary-select-article-buffer ()
6191   "Reconfigure windows to show article buffer."
6192   (interactive)
6193   (if (not (gnus-buffer-live-p gnus-article-buffer))
6194       (error "There is no article buffer for this summary buffer")
6195     (gnus-configure-windows 'article)
6196     (select-window (get-buffer-window gnus-article-buffer))))
6197
6198 (defun gnus-summary-universal-argument (arg)
6199   "Perform any operation on all articles that are process/prefixed."
6200   (interactive "P")
6201   (let ((articles (gnus-summary-work-articles arg))
6202         func article)
6203     (if (eq
6204          (setq
6205           func
6206           (key-binding
6207            (read-key-sequence
6208             (substitute-command-keys
6209              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6210          'undefined)
6211         (gnus-error 1 "Undefined key")
6212       (save-excursion
6213         (while articles
6214           (gnus-summary-goto-subject (setq article (pop articles)))
6215           (let (gnus-newsgroup-processable)
6216             (command-execute func))
6217           (gnus-summary-remove-process-mark article)))))
6218   (gnus-summary-position-point))
6219
6220 (defun gnus-summary-toggle-truncation (&optional arg)
6221   "Toggle truncation of summary lines.
6222 With arg, turn line truncation on if arg is positive."
6223   (interactive "P")
6224   (setq truncate-lines
6225         (if (null arg) (not truncate-lines)
6226           (> (prefix-numeric-value arg) 0)))
6227   (redraw-display))
6228
6229 (defun gnus-summary-find-uncancelled ()
6230   "Return the number of an uncancelled article.
6231 The current article is considered, then following articles, then previous
6232 articles.  If all articles are cancelled then return a dummy 0."
6233   (let (found)
6234     (dolist (rev '(nil t))
6235       (unless found      ; don't demand the reverse list if we don't need it
6236         (let ((data (gnus-data-find-list
6237                      (gnus-summary-article-number) (gnus-data-list rev))))
6238           (while (and data (not found))
6239             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6240                 (setq found (gnus-data-number (car data))))
6241             (setq data (cdr data))))))
6242     (or found 0)))
6243
6244 (defun gnus-summary-reselect-current-group (&optional all rescan)
6245   "Exit and then reselect the current newsgroup.
6246 The prefix argument ALL means to select all articles."
6247   (interactive "P")
6248   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6249     (error "Ephemeral groups can't be reselected"))
6250   (let ((current-subject (gnus-summary-find-uncancelled))
6251         (group gnus-newsgroup-name))
6252     (setq gnus-newsgroup-begin nil)
6253     (gnus-summary-exit)
6254     ;; We have to adjust the point of group mode buffer because
6255     ;; point was moved to the next unread newsgroup by exiting.
6256     (gnus-summary-jump-to-group group)
6257     (when rescan
6258       (save-excursion
6259         (save-window-excursion
6260           ;; Don't show group contents.
6261           (set-window-start (selected-window) (point-max))
6262           (gnus-group-get-new-news-this-group 1))))
6263     (gnus-group-read-group all t)
6264     (gnus-summary-goto-subject current-subject nil t)))
6265
6266 (defun gnus-summary-rescan-group (&optional all)
6267   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6268   (interactive "P")
6269   (gnus-summary-reselect-current-group all t))
6270
6271 (defun gnus-summary-update-info (&optional non-destructive)
6272   (save-excursion
6273     (let ((group gnus-newsgroup-name))
6274       (when group
6275         (when gnus-newsgroup-kill-headers
6276           (setq gnus-newsgroup-killed
6277                 (gnus-compress-sequence
6278                  (gnus-sorted-union
6279                   (gnus-list-range-intersection
6280                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6281                   gnus-newsgroup-unreads)
6282                  t)))
6283         (unless (listp (cdr gnus-newsgroup-killed))
6284           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6285         (let ((headers gnus-newsgroup-headers))
6286           ;; Set the new ranges of read articles.
6287           (save-excursion
6288             (set-buffer gnus-group-buffer)
6289             (gnus-undo-force-boundary))
6290           (gnus-update-read-articles
6291            group (gnus-sorted-union
6292                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6293           ;; Set the current article marks.
6294           (let ((gnus-newsgroup-scored
6295                  (if (and (not gnus-save-score)
6296                           (not non-destructive))
6297                      nil
6298                    gnus-newsgroup-scored)))
6299             (save-excursion
6300               (gnus-update-marks)))
6301           ;; Do the cross-ref thing.
6302           (when gnus-use-cross-reference
6303             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6304           ;; Do not switch windows but change the buffer to work.
6305           (set-buffer gnus-group-buffer)
6306           (unless (gnus-ephemeral-group-p group)
6307             (gnus-group-update-group group)))))))
6308
6309 (defun gnus-summary-save-newsrc (&optional force)
6310   "Save the current number of read/marked articles in the dribble buffer.
6311 The dribble buffer will then be saved.
6312 If FORCE (the prefix), also save the .newsrc file(s)."
6313   (interactive "P")
6314   (gnus-summary-update-info t)
6315   (if force
6316       (gnus-save-newsrc-file)
6317     (gnus-dribble-save)))
6318
6319 (defun gnus-summary-exit (&optional temporary)
6320   "Exit reading current newsgroup, and then return to group selection mode.
6321 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6322   (interactive)
6323   (gnus-set-global-variables)
6324   (gnus-kill-save-kill-buffer)
6325   (gnus-async-halt-prefetch)
6326   (let* ((group gnus-newsgroup-name)
6327          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6328          (gnus-group-is-exiting-p t)
6329          (mode major-mode)
6330          (group-point nil)
6331          (buf (current-buffer)))
6332     (unless quit-config
6333       ;; Do adaptive scoring, and possibly save score files.
6334       (when gnus-newsgroup-adaptive
6335         (gnus-score-adaptive))
6336       (when gnus-use-scoring
6337         (gnus-score-save)))
6338     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6339     ;; If we have several article buffers, we kill them at exit.
6340     (unless gnus-single-article-buffer
6341       (gnus-kill-buffer gnus-original-article-buffer)
6342       (setq gnus-article-current nil))
6343     (when gnus-use-cache
6344       (gnus-cache-possibly-remove-articles)
6345       (gnus-cache-save-buffers))
6346     (gnus-async-prefetch-remove-group group)
6347     (when gnus-suppress-duplicates
6348       (gnus-dup-enter-articles))
6349     (when gnus-use-trees
6350       (gnus-tree-close group))
6351     (when gnus-use-cache
6352       (gnus-cache-write-active))
6353     ;; Remove entries for this group.
6354     (nnmail-purge-split-history (gnus-group-real-name group))
6355     ;; Make all changes in this group permanent.
6356     (unless quit-config
6357       (gnus-run-hooks 'gnus-exit-group-hook)
6358       (gnus-summary-update-info))
6359     (gnus-close-group group)
6360     ;; Make sure where we were, and go to next newsgroup.
6361     (set-buffer gnus-group-buffer)
6362     (unless quit-config
6363       (gnus-group-jump-to-group group))
6364     (gnus-run-hooks 'gnus-summary-exit-hook)
6365     (unless (or quit-config
6366                 ;; If this group has disappeared from the summary
6367                 ;; buffer, don't skip forwards.
6368                 (not (string= group (gnus-group-group-name))))
6369       (gnus-group-next-unread-group 1))
6370     (setq group-point (point))
6371     (if temporary
6372         nil                             ;Nothing to do.
6373       ;; If we have several article buffers, we kill them at exit.
6374       (unless gnus-single-article-buffer
6375         (gnus-kill-buffer gnus-article-buffer)
6376         (gnus-kill-buffer gnus-original-article-buffer)
6377         (setq gnus-article-current nil))
6378       (set-buffer buf)
6379       (if (not gnus-kill-summary-on-exit)
6380           (progn
6381             (gnus-deaden-summary)
6382             (setq mode nil))
6383         ;; We set all buffer-local variables to nil.  It is unclear why
6384         ;; this is needed, but if we don't, buffer-local variables are
6385         ;; not garbage-collected, it seems.  This would the lead to en
6386         ;; ever-growing Emacs.
6387         (gnus-summary-clear-local-variables)
6388         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6389           (gnus-summary-clear-local-variables))
6390         (when (get-buffer gnus-article-buffer)
6391           (bury-buffer gnus-article-buffer))
6392         ;; We clear the global counterparts of the buffer-local
6393         ;; variables as well, just to be on the safe side.
6394         (set-buffer gnus-group-buffer)
6395         (gnus-summary-clear-local-variables)
6396         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6397           (gnus-summary-clear-local-variables)))
6398       (setq gnus-current-select-method gnus-select-method)
6399       (pop-to-buffer gnus-group-buffer)
6400       (if (not quit-config)
6401           (progn
6402             (goto-char group-point)
6403             (gnus-configure-windows 'group 'force)
6404             (unless (pos-visible-in-window-p)
6405               (forward-line (/ (static-if (featurep 'xemacs)
6406                                    (window-displayed-height)
6407                                  (1- (window-height)))
6408                                -2))
6409               (set-window-start (selected-window) (point))
6410               (goto-char group-point)))
6411         (gnus-handle-ephemeral-exit quit-config))
6412       ;; Return to group mode buffer.
6413       (when (eq mode 'gnus-summary-mode)
6414         (gnus-kill-buffer buf))
6415       ;; Clear the current group name.
6416       (unless quit-config
6417         (setq gnus-newsgroup-name nil)))))
6418
6419 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6420 (defun gnus-summary-exit-no-update (&optional no-questions)
6421   "Quit reading current newsgroup without updating read article info."
6422   (interactive)
6423   (let* ((group gnus-newsgroup-name)
6424          (gnus-group-is-exiting-p t)
6425          (quit-config (gnus-group-quit-config group)))
6426     (when (or no-questions
6427               gnus-expert-user
6428               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6429       (gnus-async-halt-prefetch)
6430       (mapcar 'funcall
6431               (delq 'gnus-summary-expire-articles
6432                     (copy-sequence gnus-summary-prepare-exit-hook)))
6433       ;; If we have several article buffers, we kill them at exit.
6434       (unless gnus-single-article-buffer
6435         (gnus-kill-buffer gnus-article-buffer)
6436         (gnus-kill-buffer gnus-original-article-buffer)
6437         (setq gnus-article-current nil))
6438       (if (not gnus-kill-summary-on-exit)
6439           (gnus-deaden-summary)
6440         (gnus-close-group group)
6441         (gnus-summary-clear-local-variables)
6442         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6443           (gnus-summary-clear-local-variables))
6444         (set-buffer gnus-group-buffer)
6445         (gnus-summary-clear-local-variables)
6446         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6447           (gnus-summary-clear-local-variables))
6448         (when (get-buffer gnus-summary-buffer)
6449           (kill-buffer gnus-summary-buffer)))
6450       (unless gnus-single-article-buffer
6451         (setq gnus-article-current nil))
6452       (when gnus-use-trees
6453         (gnus-tree-close group))
6454       (gnus-async-prefetch-remove-group group)
6455       (when (get-buffer gnus-article-buffer)
6456         (bury-buffer gnus-article-buffer))
6457       ;; Return to the group buffer.
6458       (gnus-configure-windows 'group 'force)
6459       ;; Clear the current group name.
6460       (setq gnus-newsgroup-name nil)
6461       (gnus-group-update-group group)
6462       (when (equal (gnus-group-group-name) group)
6463         (gnus-group-next-unread-group 1))
6464       (when quit-config
6465         (gnus-handle-ephemeral-exit quit-config)))))
6466
6467 (defun gnus-handle-ephemeral-exit (quit-config)
6468   "Handle movement when leaving an ephemeral group.
6469 The state which existed when entering the ephemeral is reset."
6470   (if (not (buffer-name (car quit-config)))
6471       (gnus-configure-windows 'group 'force)
6472     (set-buffer (car quit-config))
6473     (cond ((eq major-mode 'gnus-summary-mode)
6474            (gnus-set-global-variables))
6475           ((eq major-mode 'gnus-article-mode)
6476            (save-excursion
6477              ;; The `gnus-summary-buffer' variable may point
6478              ;; to the old summary buffer when using a single
6479              ;; article buffer.
6480              (unless (gnus-buffer-live-p gnus-summary-buffer)
6481                (set-buffer gnus-group-buffer))
6482              (set-buffer gnus-summary-buffer)
6483              (gnus-set-global-variables))))
6484     (if (or (eq (cdr quit-config) 'article)
6485             (eq (cdr quit-config) 'pick))
6486         (progn
6487           ;; The current article may be from the ephemeral group
6488           ;; thus it is best that we reload this article
6489           (gnus-summary-show-article)
6490           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6491               (gnus-configure-windows 'pick 'force)
6492             (gnus-configure-windows (cdr quit-config) 'force)))
6493       (gnus-configure-windows (cdr quit-config) 'force))
6494     (when (eq major-mode 'gnus-summary-mode)
6495       (gnus-summary-next-subject 1 nil t)
6496       (gnus-summary-recenter)
6497       (gnus-summary-position-point))))
6498
6499 (defun gnus-summary-preview-mime-message ()
6500   "MIME decode and play this message."
6501   (interactive)
6502   (let ((gnus-break-pages nil)
6503         (gnus-show-mime t))
6504     (gnus-summary-select-article gnus-show-all-headers t))
6505   (let ((w (get-buffer-window gnus-article-buffer)))
6506     (when w
6507       (select-window (get-buffer-window gnus-article-buffer)))))
6508
6509 ;;; Dead summaries.
6510
6511 (defvar gnus-dead-summary-mode-map nil)
6512
6513 (unless gnus-dead-summary-mode-map
6514   (setq gnus-dead-summary-mode-map (make-keymap))
6515   (suppress-keymap gnus-dead-summary-mode-map)
6516   (substitute-key-definition
6517    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6518   (dolist (key '("\C-d" "\r" "\177" [delete]))
6519     (define-key gnus-dead-summary-mode-map
6520       key 'gnus-summary-wake-up-the-dead))
6521   (dolist (key '("q" "Q"))
6522     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6523
6524 (defvar gnus-dead-summary-mode nil
6525   "Minor mode for Gnus summary buffers.")
6526
6527 (defun gnus-dead-summary-mode (&optional arg)
6528   "Minor mode for Gnus summary buffers."
6529   (interactive "P")
6530   (when (eq major-mode 'gnus-summary-mode)
6531     (make-local-variable 'gnus-dead-summary-mode)
6532     (setq gnus-dead-summary-mode
6533           (if (null arg) (not gnus-dead-summary-mode)
6534             (> (prefix-numeric-value arg) 0)))
6535     (when gnus-dead-summary-mode
6536       (gnus-add-minor-mode
6537        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6538
6539 (defun gnus-deaden-summary ()
6540   "Make the current summary buffer into a dead summary buffer."
6541   ;; Kill any previous dead summary buffer.
6542   (when (and gnus-dead-summary
6543              (buffer-name gnus-dead-summary))
6544     (save-excursion
6545       (set-buffer gnus-dead-summary)
6546       (when gnus-dead-summary-mode
6547         (kill-buffer (current-buffer)))))
6548   ;; Make this the current dead summary.
6549   (setq gnus-dead-summary (current-buffer))
6550   (gnus-dead-summary-mode 1)
6551   (let ((name (buffer-name)))
6552     (when (string-match "Summary" name)
6553       (rename-buffer
6554        (concat (substring name 0 (match-beginning 0)) "Dead "
6555                (substring name (match-beginning 0)))
6556        t)
6557       (bury-buffer))))
6558
6559 (defun gnus-kill-or-deaden-summary (buffer)
6560   "Kill or deaden the summary BUFFER."
6561   (save-excursion
6562     (when (and (buffer-name buffer)
6563                (not gnus-single-article-buffer))
6564       (save-excursion
6565         (set-buffer buffer)
6566         (gnus-kill-buffer gnus-article-buffer)
6567         (gnus-kill-buffer gnus-original-article-buffer)))
6568     (cond
6569      ;; Kill the buffer.
6570      (gnus-kill-summary-on-exit
6571       (when (and gnus-use-trees
6572                  (gnus-buffer-exists-p buffer))
6573         (save-excursion
6574           (set-buffer buffer)
6575           (gnus-tree-close gnus-newsgroup-name)))
6576       (gnus-kill-buffer buffer))
6577      ;; Deaden the buffer.
6578      ((gnus-buffer-exists-p buffer)
6579       (save-excursion
6580         (set-buffer buffer)
6581         (gnus-deaden-summary))))))
6582
6583 (defun gnus-summary-wake-up-the-dead (&rest args)
6584   "Wake up the dead summary buffer."
6585   (interactive)
6586   (gnus-dead-summary-mode -1)
6587   (let ((name (buffer-name)))
6588     (when (string-match "Dead " name)
6589       (rename-buffer
6590        (concat (substring name 0 (match-beginning 0))
6591                (substring name (match-end 0)))
6592        t)))
6593   (gnus-message 3 "This dead summary is now alive again"))
6594
6595 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6596 (defun gnus-summary-fetch-faq (&optional faq-dir)
6597   "Fetch the FAQ for the current group.
6598 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6599 in."
6600   (interactive
6601    (list
6602     (when current-prefix-arg
6603       (completing-read
6604        "FAQ dir: " (and (listp gnus-group-faq-directory)
6605                         (mapcar (lambda (file) (list file))
6606                                 gnus-group-faq-directory))))))
6607   (let (gnus-faq-buffer)
6608     (when (setq gnus-faq-buffer
6609                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6610       (gnus-configure-windows 'summary-faq))))
6611
6612 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6613 (defun gnus-summary-describe-group (&optional force)
6614   "Describe the current newsgroup."
6615   (interactive "P")
6616   (gnus-group-describe-group force gnus-newsgroup-name))
6617
6618 (defun gnus-summary-describe-briefly ()
6619   "Describe summary mode commands briefly."
6620   (interactive)
6621   (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")))
6622
6623 ;; Walking around group mode buffer from summary mode.
6624
6625 (defun gnus-summary-next-group (&optional no-article target-group backward)
6626   "Exit current newsgroup and then select next unread newsgroup.
6627 If prefix argument NO-ARTICLE is non-nil, no article is selected
6628 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6629 previous group instead."
6630   (interactive "P")
6631   ;; Stop pre-fetching.
6632   (gnus-async-halt-prefetch)
6633   (let ((current-group gnus-newsgroup-name)
6634         (current-buffer (current-buffer))
6635         entered)
6636     (gnus-summary-exit t)
6637     (while (not entered)
6638       ;; Then we find what group we are supposed to enter.
6639       (set-buffer gnus-group-buffer)
6640       (gnus-group-jump-to-group current-group)
6641       (setq target-group
6642             (or target-group
6643                 (if (eq gnus-keep-same-level 'best)
6644                     (gnus-summary-best-group gnus-newsgroup-name)
6645                   (gnus-summary-search-group backward gnus-keep-same-level))))
6646       (if (not target-group)
6647           ;; There are no further groups, so we return to the group
6648           ;; buffer.
6649           (progn
6650             (gnus-message 5 "Returning to the group buffer")
6651             (setq entered t)
6652             (when (gnus-buffer-live-p current-buffer)
6653               (set-buffer current-buffer)
6654               (gnus-summary-exit))
6655             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6656         ;; We try to enter the target group.
6657         (gnus-group-jump-to-group target-group)
6658         (let ((unreads (gnus-group-group-unread)))
6659           (if (and (or (eq t unreads)
6660                        (and unreads (not (zerop unreads))))
6661                    (progn
6662                      ;; Now we semi-exit this group to update Xrefs
6663                      ;; and all variables.  We can't do a real exit,
6664                      ;; because the window conf must remain the same
6665                      ;; in case the user is prompted for info, and we
6666                      ;; don't want the window conf to change before
6667                      ;; that...
6668                      (when (gnus-buffer-live-p current-buffer)
6669                        (set-buffer current-buffer)
6670                        (gnus-summary-exit t))
6671                      (gnus-summary-read-group
6672                       target-group nil no-article
6673                       (and (buffer-name current-buffer) current-buffer)
6674                       nil backward)))
6675               (setq entered t)
6676             (setq current-group target-group
6677                   target-group nil)))))))
6678
6679 (defun gnus-summary-prev-group (&optional no-article)
6680   "Exit current newsgroup and then select previous unread newsgroup.
6681 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6682   (interactive "P")
6683   (gnus-summary-next-group no-article nil t))
6684
6685 ;; Walking around summary lines.
6686
6687 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6688   "Go to the first unread subject.
6689 If UNREAD is non-nil, go to the first unread article.
6690 Returns the article selected or nil if there are no unread articles."
6691   (interactive "P")
6692   (prog1
6693       (cond
6694        ;; Empty summary.
6695        ((null gnus-newsgroup-data)
6696         (gnus-message 3 "No articles in the group")
6697         nil)
6698        ;; Pick the first article.
6699        ((not unread)
6700         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6701         (gnus-data-number (car gnus-newsgroup-data)))
6702        ;; No unread articles.
6703        ((null gnus-newsgroup-unreads)
6704         (gnus-message 3 "No more unread articles")
6705         nil)
6706        ;; Find the first unread article.
6707        (t
6708         (let ((data gnus-newsgroup-data))
6709           (while (and data
6710                       (and (not (and undownloaded
6711                                      (memq (car data) gnus-newsgroup-undownloaded)))
6712                            (if unseen
6713                                (or (not (memq
6714                                          (gnus-data-number (car data))
6715                                          gnus-newsgroup-unseen))
6716                                    (not (gnus-data-unread-p (car data))))
6717                              (not (gnus-data-unread-p (car data))))))
6718             (setq data (cdr data)))
6719           (when data
6720             (goto-char (gnus-data-pos (car data)))
6721             (gnus-data-number (car data))))))
6722     (gnus-summary-position-point)))
6723
6724 (defun gnus-summary-next-subject (n &optional unread dont-display)
6725   "Go to next N'th summary line.
6726 If N is negative, go to the previous N'th subject line.
6727 If UNREAD is non-nil, only unread articles are selected.
6728 The difference between N and the actual number of steps taken is
6729 returned."
6730   (interactive "p")
6731   (let ((backward (< n 0))
6732         (n (abs n)))
6733     (while (and (> n 0)
6734                 (if backward
6735                     (gnus-summary-find-prev unread)
6736                   (gnus-summary-find-next unread)))
6737       (unless (zerop (setq n (1- n)))
6738         (gnus-summary-show-thread)))
6739     (when (/= 0 n)
6740       (gnus-message 7 "No more%s articles"
6741                     (if unread " unread" "")))
6742     (unless dont-display
6743       (gnus-summary-recenter)
6744       (gnus-summary-position-point))
6745     n))
6746
6747 (defun gnus-summary-next-unread-subject (n)
6748   "Go to next N'th unread summary line."
6749   (interactive "p")
6750   (gnus-summary-next-subject n t))
6751
6752 (defun gnus-summary-prev-subject (n &optional unread)
6753   "Go to previous N'th summary line.
6754 If optional argument UNREAD is non-nil, only unread article is selected."
6755   (interactive "p")
6756   (gnus-summary-next-subject (- n) unread))
6757
6758 (defun gnus-summary-prev-unread-subject (n)
6759   "Go to previous N'th unread summary line."
6760   (interactive "p")
6761   (gnus-summary-next-subject (- n) t))
6762
6763 (defun gnus-summary-goto-subjects (articles)
6764   "Insert the subject header for ARTICLES in the current buffer."
6765   (save-excursion
6766     (dolist (article articles)
6767       (gnus-summary-goto-subject article t)))
6768   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6769   (gnus-summary-position-point))
6770   
6771 (defun gnus-summary-goto-subject (article &optional force silent)
6772   "Go the subject line of ARTICLE.
6773 If FORCE, also allow jumping to articles not currently shown."
6774   (interactive "nArticle number: ")
6775   (unless (numberp article)
6776     (error "Article %s is not a number" article))
6777   (let ((b (point))
6778         (data (gnus-data-find article)))
6779     ;; We read in the article if we have to.
6780     (and (not data)
6781          force
6782          (gnus-summary-insert-subject
6783           article
6784           (if (or (numberp force) (vectorp force)) force)
6785           t)
6786          (setq data (gnus-data-find article)))
6787     (goto-char b)
6788     (if (not data)
6789         (progn
6790           (unless silent
6791             (gnus-message 3 "Can't find article %d" article))
6792           nil)
6793       (let ((pt (gnus-data-pos data)))
6794         (goto-char pt)
6795         (gnus-summary-set-article-display-arrow pt))
6796       (gnus-summary-position-point)
6797       article)))
6798
6799 ;; Walking around summary lines with displaying articles.
6800
6801 (defun gnus-summary-expand-window (&optional arg)
6802   "Make the summary buffer take up the entire Emacs frame.
6803 Given a prefix, will force an `article' buffer configuration."
6804   (interactive "P")
6805   (if arg
6806       (gnus-configure-windows 'article 'force)
6807     (gnus-configure-windows 'summary 'force)))
6808
6809 (defun gnus-summary-display-article (article &optional all-header)
6810   "Display ARTICLE in article buffer."
6811   (when (gnus-buffer-live-p gnus-article-buffer)
6812     (with-current-buffer gnus-article-buffer
6813       (set-buffer-multibyte t)))
6814   (gnus-set-global-variables)
6815   (when (gnus-buffer-live-p gnus-article-buffer)
6816     (with-current-buffer gnus-article-buffer
6817       (setq gnus-article-charset gnus-newsgroup-charset)
6818       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6819       (set-buffer-multibyte t)))
6820   (if (null article)
6821       nil
6822     (prog1
6823         (if gnus-summary-display-article-function
6824             (funcall gnus-summary-display-article-function article all-header)
6825           (gnus-article-prepare article all-header))
6826       (with-current-buffer gnus-article-buffer
6827         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6828              nil))
6829       (gnus-run-hooks 'gnus-select-article-hook)
6830       (when (and gnus-current-article
6831                  (not (zerop gnus-current-article)))
6832         (gnus-summary-goto-subject gnus-current-article))
6833       (gnus-summary-recenter)
6834       (when (and gnus-use-trees gnus-show-threads)
6835         (gnus-possibly-generate-tree article)
6836         (gnus-highlight-selected-tree article))
6837       ;; Successfully display article.
6838       (gnus-article-set-window-start
6839        (cdr (assq article gnus-newsgroup-bookmarks))))))
6840
6841 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6842   "Select the current article.
6843 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6844 non-nil, the article will be re-fetched even if it already present in
6845 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6846 be displayed."
6847   ;; Make sure we are in the summary buffer to work around bbdb bug.
6848   (unless (eq major-mode 'gnus-summary-mode)
6849     (set-buffer gnus-summary-buffer))
6850   (let ((article (or article (gnus-summary-article-number)))
6851         (all-headers (not (not all-headers))) ;Must be T or NIL.
6852         gnus-summary-display-article-function)
6853     (and (not pseudo)
6854          (gnus-summary-article-pseudo-p article)
6855          (error "This is a pseudo-article"))
6856     (save-excursion
6857       (set-buffer gnus-summary-buffer)
6858       (if (or (and gnus-single-article-buffer
6859                    (or (null gnus-current-article)
6860                        (null gnus-article-current)
6861                        (null (get-buffer gnus-article-buffer))
6862                        (not (eq article (cdr gnus-article-current)))
6863                        (not (equal (car gnus-article-current)
6864                                    gnus-newsgroup-name))))
6865               (and (not gnus-single-article-buffer)
6866                    (or (null gnus-current-article)
6867                        (not (eq gnus-current-article article))))
6868               force)
6869           ;; The requested article is different from the current article.
6870           (progn
6871             (gnus-summary-display-article article all-headers)
6872             (gnus-article-set-window-start
6873              (cdr (assq article gnus-newsgroup-bookmarks)))
6874             article)
6875         'old))))
6876
6877 (defun gnus-summary-force-verify-and-decrypt ()
6878   (interactive)
6879   (let ((mm-verify-option 'known)
6880         (mm-decrypt-option 'known)
6881         (gnus-buttonized-mime-types (append (list "multipart/signed"
6882                                                   "multipart/encrypted")
6883                                             gnus-buttonized-mime-types)))
6884     (gnus-summary-select-article nil 'force)))
6885
6886 (defun gnus-summary-set-current-mark (&optional current-mark)
6887   "Obsolete function."
6888   nil)
6889
6890 (defun gnus-summary-next-article (&optional unread subject backward push)
6891   "Select the next article.
6892 If UNREAD, only unread articles are selected.
6893 If SUBJECT, only articles with SUBJECT are selected.
6894 If BACKWARD, the previous article is selected instead of the next."
6895   (interactive "P")
6896   (cond
6897    ;; Is there such an article?
6898    ((and (gnus-summary-search-forward unread subject backward)
6899          (or (gnus-summary-display-article (gnus-summary-article-number))
6900              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6901     (gnus-summary-position-point))
6902    ;; If not, we try the first unread, if that is wanted.
6903    ((and subject
6904          gnus-auto-select-same
6905          (gnus-summary-first-unread-article))
6906     (gnus-summary-position-point)
6907     (gnus-message 6 "Wrapped"))
6908    ;; Try to get next/previous article not displayed in this group.
6909    ((and gnus-auto-extend-newsgroup
6910          (not unread) (not subject))
6911     (gnus-summary-goto-article
6912      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6913      nil (count-lines (point-min) (point))))
6914    ;; Go to next/previous group.
6915    (t
6916     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6917       (gnus-summary-jump-to-group gnus-newsgroup-name))
6918     (let ((cmd last-command-char)
6919           (point
6920            (save-excursion
6921              (set-buffer gnus-group-buffer)
6922              (point)))
6923           (group
6924            (if (eq gnus-keep-same-level 'best)
6925                (gnus-summary-best-group gnus-newsgroup-name)
6926              (gnus-summary-search-group backward gnus-keep-same-level))))
6927       ;; For some reason, the group window gets selected.  We change
6928       ;; it back.
6929       (select-window (get-buffer-window (current-buffer)))
6930       ;; Select next unread newsgroup automagically.
6931       (cond
6932        ((or (not gnus-auto-select-next)
6933             (not cmd))
6934         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6935        ((or (eq gnus-auto-select-next 'quietly)
6936             (and (eq gnus-auto-select-next 'slightly-quietly)
6937                  push)
6938             (and (eq gnus-auto-select-next 'almost-quietly)
6939                  (gnus-summary-last-article-p)))
6940         ;; Select quietly.
6941         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6942             (gnus-summary-exit)
6943           (gnus-message 7 "No more%s articles (%s)..."
6944                         (if unread " unread" "")
6945                         (if group (concat "selecting " group)
6946                           "exiting"))
6947           (gnus-summary-next-group nil group backward)))
6948        (t
6949         (when (gnus-key-press-event-p last-input-event)
6950           (gnus-summary-walk-group-buffer
6951            gnus-newsgroup-name cmd unread backward point))))))))
6952
6953 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6954   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6955                       (?\C-p (gnus-group-prev-unread-group 1))))
6956         (cursor-in-echo-area t)
6957         keve key group ended prompt)
6958     (save-excursion
6959       (set-buffer gnus-group-buffer)
6960       (goto-char start)
6961       (setq group
6962             (if (eq gnus-keep-same-level 'best)
6963                 (gnus-summary-best-group gnus-newsgroup-name)
6964               (gnus-summary-search-group backward gnus-keep-same-level))))
6965     (while (not ended)
6966       (setq prompt
6967             (format
6968              "No more%s articles%s " (if unread " unread" "")
6969              (if (and group
6970                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6971                  (format " (Type %s for %s [%s])"
6972                          (single-key-description cmd) group
6973                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6974                (format " (Type %s to exit %s)"
6975                        (single-key-description cmd)
6976                        gnus-newsgroup-name))))
6977       ;; Confirm auto selection.
6978       (setq key (car (setq keve (gnus-read-event-char prompt)))
6979             ended t)
6980       (cond
6981        ((assq key keystrokes)
6982         (let ((obuf (current-buffer)))
6983           (switch-to-buffer gnus-group-buffer)
6984           (when group
6985             (gnus-group-jump-to-group group))
6986           (eval (cadr (assq key keystrokes)))
6987           (setq group (gnus-group-group-name))
6988           (switch-to-buffer obuf))
6989         (setq ended nil))
6990        ((equal key cmd)
6991         (if (or (not group)
6992                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6993             (gnus-summary-exit)
6994           (gnus-summary-next-group nil group backward)))
6995        (t
6996         (push (cdr keve) unread-command-events))))))
6997
6998 (defun gnus-summary-next-unread-article ()
6999   "Select unread article after current one."
7000   (interactive)
7001   (gnus-summary-next-article
7002    (or (not (eq gnus-summary-goto-unread 'never))
7003        (gnus-summary-last-article-p (gnus-summary-article-number)))
7004    (and gnus-auto-select-same
7005         (gnus-summary-article-subject))))
7006
7007 (defun gnus-summary-prev-article (&optional unread subject)
7008   "Select the article after the current one.
7009 If UNREAD is non-nil, only unread articles are selected."
7010   (interactive "P")
7011   (gnus-summary-next-article unread subject t))
7012
7013 (defun gnus-summary-prev-unread-article ()
7014   "Select unread article before current one."
7015   (interactive)
7016   (gnus-summary-prev-article
7017    (or (not (eq gnus-summary-goto-unread 'never))
7018        (gnus-summary-first-article-p (gnus-summary-article-number)))
7019    (and gnus-auto-select-same
7020         (gnus-summary-article-subject))))
7021
7022 (defun gnus-summary-next-page (&optional lines circular stop)
7023   "Show next page of the selected article.
7024 If at the end of the current article, select the next article.
7025 LINES says how many lines should be scrolled up.
7026
7027 If CIRCULAR is non-nil, go to the start of the article instead of
7028 selecting the next article when reaching the end of the current
7029 article.
7030
7031 If STOP is non-nil, just stop when reaching the end of the message."
7032   (interactive "P")
7033   (setq gnus-summary-buffer (current-buffer))
7034   (gnus-set-global-variables)
7035   (let ((article (gnus-summary-article-number))
7036         (article-window (get-buffer-window gnus-article-buffer t))
7037         endp)
7038     ;; If the buffer is empty, we have no article.
7039     (unless article
7040       (error "No article to select"))
7041     (gnus-configure-windows 'article)
7042     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7043         (if (and (eq gnus-summary-goto-unread 'never)
7044                  (not (gnus-summary-last-article-p article)))
7045             (gnus-summary-next-article)
7046           (gnus-summary-next-unread-article))
7047       (if (or (null gnus-current-article)
7048               (null gnus-article-current)
7049               (/= article (cdr gnus-article-current))
7050               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7051           ;; Selected subject is different from current article's.
7052           (gnus-summary-display-article article)
7053         (when article-window
7054           (gnus-eval-in-buffer-window gnus-article-buffer
7055             (setq endp (gnus-article-next-page lines)))
7056           (when endp
7057             (cond (stop
7058                    (gnus-message 3 "End of message"))
7059                   (circular
7060                    (gnus-summary-beginning-of-article))
7061                   (lines
7062                    (gnus-message 3 "End of message"))
7063                   ((null lines)
7064                    (if (and (eq gnus-summary-goto-unread 'never)
7065                             (not (gnus-summary-last-article-p article)))
7066                        (gnus-summary-next-article)
7067                      (gnus-summary-next-unread-article))))))))
7068     (gnus-summary-recenter)
7069     (gnus-summary-position-point)))
7070
7071 (defun gnus-summary-prev-page (&optional lines move)
7072   "Show previous page of selected article.
7073 Argument LINES specifies lines to be scrolled down.
7074 If MOVE, move to the previous unread article if point is at
7075 the beginning of the buffer."
7076   (interactive "P")
7077   (let ((article (gnus-summary-article-number))
7078         (article-window (get-buffer-window gnus-article-buffer t))
7079         endp)
7080     (gnus-configure-windows 'article)
7081     (if (or (null gnus-current-article)
7082             (null gnus-article-current)
7083             (/= article (cdr gnus-article-current))
7084             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7085         ;; Selected subject is different from current article's.
7086         (gnus-summary-display-article article)
7087       (gnus-summary-recenter)
7088       (when article-window
7089         (gnus-eval-in-buffer-window gnus-article-buffer
7090           (setq endp (gnus-article-prev-page lines)))
7091         (when (and move endp)
7092           (cond (lines
7093                  (gnus-message 3 "Beginning of message"))
7094                 ((null lines)
7095                  (if (and (eq gnus-summary-goto-unread 'never)
7096                           (not (gnus-summary-first-article-p article)))
7097                      (gnus-summary-prev-article)
7098                    (gnus-summary-prev-unread-article))))))))
7099   (gnus-summary-position-point))
7100
7101 (defun gnus-summary-prev-page-or-article (&optional lines)
7102   "Show previous page of selected article.
7103 Argument LINES specifies lines to be scrolled down.
7104 If at the beginning of the article, go to the next article."
7105   (interactive "P")
7106   (gnus-summary-prev-page lines t))
7107
7108 (defun gnus-summary-scroll-up (lines)
7109   "Scroll up (or down) one line current article.
7110 Argument LINES specifies lines to be scrolled up (or down if negative)."
7111   (interactive "p")
7112   (gnus-configure-windows 'article)
7113   (gnus-summary-show-thread)
7114   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7115     (gnus-eval-in-buffer-window gnus-article-buffer
7116       (cond ((> lines 0)
7117              (when (gnus-article-next-page lines)
7118                (gnus-message 3 "End of message")))
7119             ((< lines 0)
7120              (gnus-article-prev-page (- lines))))))
7121   (gnus-summary-recenter)
7122   (gnus-summary-position-point))
7123
7124 (defun gnus-summary-scroll-down (lines)
7125   "Scroll down (or up) one line current article.
7126 Argument LINES specifies lines to be scrolled down (or up if negative)."
7127   (interactive "p")
7128   (gnus-summary-scroll-up (- lines)))
7129
7130 (defun gnus-summary-next-same-subject ()
7131   "Select next article which has the same subject as current one."
7132   (interactive)
7133   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7134
7135 (defun gnus-summary-prev-same-subject ()
7136   "Select previous article which has the same subject as current one."
7137   (interactive)
7138   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7139
7140 (defun gnus-summary-next-unread-same-subject ()
7141   "Select next unread article which has the same subject as current one."
7142   (interactive)
7143   (gnus-summary-next-article t (gnus-summary-article-subject)))
7144
7145 (defun gnus-summary-prev-unread-same-subject ()
7146   "Select previous unread article which has the same subject as current one."
7147   (interactive)
7148   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7149
7150 (defun gnus-summary-first-unread-article ()
7151   "Select the first unread article.
7152 Return nil if there are no unread articles."
7153   (interactive)
7154   (prog1
7155       (when (gnus-summary-first-subject t)
7156         (gnus-summary-show-thread)
7157         (gnus-summary-first-subject t)
7158         (gnus-summary-display-article (gnus-summary-article-number)))
7159     (gnus-summary-position-point)))
7160
7161 (defun gnus-summary-first-unread-subject ()
7162   "Place the point on the subject line of the first unread article.
7163 Return nil if there are no unread articles."
7164   (interactive)
7165   (prog1
7166       (when (gnus-summary-first-subject t)
7167         (gnus-summary-show-thread)
7168         (gnus-summary-first-subject t))
7169     (gnus-summary-position-point)))
7170
7171 (defun gnus-summary-first-unseen-subject ()
7172   "Place the point on the subject line of the first unseen article.
7173 Return nil if there are no unseen articles."
7174   (interactive)
7175   (prog1
7176       (when (gnus-summary-first-subject t t t)
7177         (gnus-summary-show-thread)
7178         (gnus-summary-first-subject t t t))
7179     (gnus-summary-position-point)))
7180
7181 (defun gnus-summary-first-unseen-or-unread-subject ()
7182   "Place the point on the subject line of the first unseen article.
7183 Return nil if there are no unseen articles."
7184   (interactive)
7185   (prog1
7186       (unless (when (gnus-summary-first-subject t t t)
7187                 (gnus-summary-show-thread)
7188                 (gnus-summary-first-subject t t t))
7189         (when (gnus-summary-first-subject t)
7190           (gnus-summary-show-thread)
7191           (gnus-summary-first-subject t)))
7192     (gnus-summary-position-point)))
7193
7194 (defun gnus-summary-first-article ()
7195   "Select the first article.
7196 Return nil if there are no articles."
7197   (interactive)
7198   (prog1
7199       (when (gnus-summary-first-subject)
7200         (gnus-summary-show-thread)
7201         (gnus-summary-first-subject)
7202         (gnus-summary-display-article (gnus-summary-article-number)))
7203     (gnus-summary-position-point)))
7204
7205 (defun gnus-summary-best-unread-article (&optional arg)
7206   "Select the unread article with the highest score.
7207 If given a prefix argument, select the next unread article that has a
7208 score higher than the default score."
7209   (interactive "P")
7210   (let ((article (if arg
7211                      (gnus-summary-better-unread-subject)
7212                    (gnus-summary-best-unread-subject))))
7213     (if article
7214         (gnus-summary-goto-article article)
7215       (error "No unread articles"))))
7216
7217 (defun gnus-summary-best-unread-subject ()
7218   "Select the unread subject with the highest score."
7219   (interactive)
7220   (let ((best -1000000)
7221         (data gnus-newsgroup-data)
7222         article score)
7223     (while data
7224       (and (gnus-data-unread-p (car data))
7225            (> (setq score
7226                     (gnus-summary-article-score (gnus-data-number (car data))))
7227               best)
7228            (setq best score
7229                  article (gnus-data-number (car data))))
7230       (setq data (cdr data)))
7231     (when article
7232       (gnus-summary-goto-subject article))
7233     (gnus-summary-position-point)
7234     article))
7235
7236 (defun gnus-summary-better-unread-subject ()
7237   "Select the first unread subject that has a score over the default score."
7238   (interactive)
7239   (let ((data gnus-newsgroup-data)
7240         article score)
7241     (while (and (setq article (gnus-data-number (car data)))
7242                 (or (gnus-data-read-p (car data))
7243                     (not (> (gnus-summary-article-score article)
7244                             gnus-summary-default-score))))
7245       (setq data (cdr data)))
7246     (when article
7247       (gnus-summary-goto-subject article))
7248     (gnus-summary-position-point)
7249     article))
7250
7251 (defun gnus-summary-last-subject ()
7252   "Go to the last displayed subject line in the group."
7253   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7254     (when article
7255       (gnus-summary-goto-subject article))))
7256
7257 (defun gnus-summary-goto-article (article &optional all-headers force)
7258   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7259 If ALL-HEADERS is non-nil, no header lines are hidden.
7260 If FORCE, go to the article even if it isn't displayed.  If FORCE
7261 is a number, it is the line the article is to be displayed on."
7262   (interactive
7263    (list
7264     (completing-read
7265      "Article number or Message-ID: "
7266      (mapcar (lambda (number) (list (int-to-string number)))
7267              gnus-newsgroup-limit))
7268     current-prefix-arg
7269     t))
7270   (prog1
7271       (if (and (stringp article)
7272                (string-match "@" article))
7273           (gnus-summary-refer-article article)
7274         (when (stringp article)
7275           (setq article (string-to-number article)))
7276         (if (gnus-summary-goto-subject article force)
7277             (gnus-summary-display-article article all-headers)
7278           (gnus-message 4 "Couldn't go to article %s" article) nil))
7279     (gnus-summary-position-point)))
7280
7281 (defun gnus-summary-goto-last-article ()
7282   "Go to the previously read article."
7283   (interactive)
7284   (prog1
7285       (when gnus-last-article
7286         (gnus-summary-goto-article gnus-last-article nil t))
7287     (gnus-summary-position-point)))
7288
7289 (defun gnus-summary-pop-article (number)
7290   "Pop one article off the history and go to the previous.
7291 NUMBER articles will be popped off."
7292   (interactive "p")
7293   (let (to)
7294     (setq gnus-newsgroup-history
7295           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7296     (if to
7297         (gnus-summary-goto-article (car to) nil t)
7298       (error "Article history empty")))
7299   (gnus-summary-position-point))
7300
7301 ;; Summary commands and functions for limiting the summary buffer.
7302
7303 (defun gnus-summary-limit-to-articles (n)
7304   "Limit the summary buffer to the next N articles.
7305 If not given a prefix, use the process marked articles instead."
7306   (interactive "P")
7307   (prog1
7308       (let ((articles (gnus-summary-work-articles n)))
7309         (setq gnus-newsgroup-processable nil)
7310         (gnus-summary-limit articles))
7311     (gnus-summary-position-point)))
7312
7313 (defun gnus-summary-pop-limit (&optional total)
7314   "Restore the previous limit.
7315 If given a prefix, remove all limits."
7316   (interactive "P")
7317   (when total
7318     (setq gnus-newsgroup-limits
7319           (list (mapcar (lambda (h) (mail-header-number h))
7320                         gnus-newsgroup-headers))))
7321   (unless gnus-newsgroup-limits
7322     (error "No limit to pop"))
7323   (prog1
7324       (gnus-summary-limit nil 'pop)
7325     (gnus-summary-position-point)))
7326
7327 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7328   "Limit the summary buffer to articles that have subjects that match a regexp.
7329 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7330   (interactive
7331    (list (read-string (if current-prefix-arg
7332                           "Exclude subject (regexp): "
7333                         "Limit to subject (regexp): "))
7334          nil current-prefix-arg))
7335   (unless header
7336     (setq header "subject"))
7337   (when (not (equal "" subject))
7338     (prog1
7339         (let ((articles (gnus-summary-find-matching
7340                          (or header "subject") subject 'all nil nil
7341                          not-matching)))
7342           (unless articles
7343             (error "Found no matches for \"%s\"" subject))
7344           (gnus-summary-limit articles))
7345       (gnus-summary-position-point))))
7346
7347 (defun gnus-summary-limit-to-author (from &optional not-matching)
7348   "Limit the summary buffer to articles that have authors that match a regexp.
7349 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7350   (interactive
7351    (list (read-string (if current-prefix-arg
7352                           "Exclude author (regexp): "
7353                         "Limit to author (regexp): "))
7354          current-prefix-arg))
7355   (gnus-summary-limit-to-subject from "from" not-matching))
7356
7357 (defun gnus-summary-limit-to-age (age &optional younger-p)
7358   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7359 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7360 articles that are younger than AGE days."
7361   (interactive
7362    (let ((younger current-prefix-arg)
7363          (days-got nil)
7364          days)
7365      (while (not days-got)
7366        (setq days (if younger
7367                       (read-string "Limit to articles younger than (in days, older when negative): ")
7368                     (read-string
7369                      "Limit to articles older than (in days, younger when negative): ")))
7370        (when (> (length days) 0)
7371          (setq days (read days)))
7372        (if (numberp days)
7373            (progn
7374              (setq days-got t)
7375              (if (< days 0)
7376                  (progn
7377                    (setq younger (not younger))
7378                    (setq days (* days -1)))))
7379          (message "Please enter a number.")
7380          (sleep-for 1)))
7381      (list days younger)))
7382   (prog1
7383       (let ((data gnus-newsgroup-data)
7384             (cutoff (days-to-time age))
7385             articles d date is-younger)
7386         (while (setq d (pop data))
7387           (when (and (vectorp (gnus-data-header d))
7388                      (setq date (mail-header-date (gnus-data-header d))))
7389             (setq is-younger (time-less-p
7390                               (time-since (condition-case ()
7391                                               (date-to-time date)
7392                                             (error '(0 0))))
7393                               cutoff))
7394             (when (if younger-p
7395                       is-younger
7396                     (not is-younger))
7397               (push (gnus-data-number d) articles))))
7398         (gnus-summary-limit (nreverse articles)))
7399     (gnus-summary-position-point)))
7400
7401 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7402   "Limit the summary buffer to articles that match an 'extra' header."
7403   (interactive
7404    (let ((header
7405           (intern
7406            (gnus-completing-read-with-default
7407             (symbol-name (car gnus-extra-headers))
7408             (if current-prefix-arg
7409                 "Exclude extra header:"
7410               "Limit extra header:")
7411             (mapcar (lambda (x)
7412                       (cons (symbol-name x) x))
7413                     gnus-extra-headers)
7414             nil
7415             t))))
7416      (list header
7417            (read-string (format "%s header %s (regexp): "
7418                                 (if current-prefix-arg "Exclude" "Limit to")
7419                                 header))
7420            current-prefix-arg)))
7421   (when (not (equal "" regexp))
7422     (prog1
7423         (let ((articles (gnus-summary-find-matching
7424                          (cons 'extra header) regexp 'all nil nil
7425                          not-matching)))
7426           (unless articles
7427             (error "Found no matches for \"%s\"" regexp))
7428           (gnus-summary-limit articles))
7429       (gnus-summary-position-point))))
7430
7431 (defun gnus-summary-limit-to-display-predicate ()
7432   "Limit the summary buffer to the predicated in the `display' group parameter."
7433   (interactive)
7434   (unless gnus-newsgroup-display
7435     (error "There is no `display' group parameter"))
7436   (let (articles)
7437     (dolist (number gnus-newsgroup-articles)
7438       (when (funcall gnus-newsgroup-display)
7439         (push number articles)))
7440     (gnus-summary-limit articles))
7441   (gnus-summary-position-point))
7442
7443 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7444 (make-obsolete
7445  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7446
7447 (defun gnus-summary-limit-to-unread (&optional all)
7448   "Limit the summary buffer to articles that are not marked as read.
7449 If ALL is non-nil, limit strictly to unread articles."
7450   (interactive "P")
7451   (if all
7452       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7453     (gnus-summary-limit-to-marks
7454      ;; Concat all the marks that say that an article is read and have
7455      ;; those removed.
7456      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7457            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7458            gnus-low-score-mark gnus-expirable-mark
7459            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7460            gnus-duplicate-mark gnus-souped-mark)
7461      'reverse)))
7462
7463 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7464 (make-obsolete 'gnus-summary-delete-marked-with
7465                'gnus-summary-limit-exclude-marks)
7466
7467 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7468   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7469 If REVERSE, limit the summary buffer to articles that are marked
7470 with MARKS.  MARKS can either be a string of marks or a list of marks.
7471 Returns how many articles were removed."
7472   (interactive "sMarks: ")
7473   (gnus-summary-limit-to-marks marks t))
7474
7475 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7476   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7477 If REVERSE (the prefix), limit the summary buffer to articles that are
7478 not marked with MARKS.  MARKS can either be a string of marks or a
7479 list of marks.
7480 Returns how many articles were removed."
7481   (interactive "sMarks: \nP")
7482   (prog1
7483       (let ((data gnus-newsgroup-data)
7484             (marks (if (listp marks) marks
7485                      (append marks nil))) ; Transform to list.
7486             articles)
7487         (while data
7488           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7489                   (memq (gnus-data-mark (car data)) marks))
7490             (push (gnus-data-number (car data)) articles))
7491           (setq data (cdr data)))
7492         (gnus-summary-limit articles))
7493     (gnus-summary-position-point)))
7494
7495 (defun gnus-summary-limit-to-score (score)
7496   "Limit to articles with score at or above SCORE."
7497   (interactive "NLimit to articles with score of at least: ")
7498   (let ((data gnus-newsgroup-data)
7499         articles)
7500     (while data
7501       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7502                 score)
7503         (push (gnus-data-number (car data)) articles))
7504       (setq data (cdr data)))
7505     (prog1
7506         (gnus-summary-limit articles)
7507       (gnus-summary-position-point))))
7508
7509 (defun gnus-summary-limit-to-unseen ()
7510   "Limit to unseen articles."
7511   (interactive)
7512   (prog1
7513       (gnus-summary-limit gnus-newsgroup-unseen)
7514     (gnus-summary-position-point)))
7515
7516 (defun gnus-summary-limit-include-thread (id)
7517   "Display all the hidden articles that is in the thread with ID in it.
7518 When called interactively, ID is the Message-ID of the current
7519 article."
7520   (interactive (list (mail-header-id (gnus-summary-article-header))))
7521   (let ((articles (gnus-articles-in-thread
7522                    (gnus-id-to-thread (gnus-root-id id)))))
7523     (prog1
7524         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7525       (gnus-summary-limit-include-matching-articles
7526        "subject"
7527        (regexp-quote (gnus-simplify-subject-re
7528                       (mail-header-subject (gnus-id-to-header id)))))
7529       (gnus-summary-position-point))))
7530
7531 (defun gnus-summary-limit-include-matching-articles (header regexp)
7532   "Display all the hidden articles that have HEADERs that match REGEXP."
7533   (interactive (list (read-string "Match on header: ")
7534                      (read-string "Regexp: ")))
7535   (let ((articles (gnus-find-matching-articles header regexp)))
7536     (prog1
7537         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7538       (gnus-summary-position-point))))
7539
7540 (defun gnus-summary-insert-dormant-articles ()
7541   "Insert all the dormat articles for this group into the current buffer."
7542   (interactive)
7543   (let ((gnus-verbose (max 6 gnus-verbose)))
7544     (if (not gnus-newsgroup-dormant)
7545         (gnus-message 3 "No cached articles for this group")
7546       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7547
7548 (defun gnus-summary-limit-include-dormant ()
7549   "Display all the hidden articles that are marked as dormant.
7550 Note that this command only works on a subset of the articles currently
7551 fetched for this group."
7552   (interactive)
7553   (unless gnus-newsgroup-dormant
7554     (error "There are no dormant articles in this group"))
7555   (prog1
7556       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7557     (gnus-summary-position-point)))
7558
7559 (defun gnus-summary-limit-exclude-dormant ()
7560   "Hide all dormant articles."
7561   (interactive)
7562   (prog1
7563       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7564     (gnus-summary-position-point)))
7565
7566 (defun gnus-summary-limit-exclude-childless-dormant ()
7567   "Hide all dormant articles that have no children."
7568   (interactive)
7569   (let ((data (gnus-data-list t))
7570         articles d children)
7571     ;; Find all articles that are either not dormant or have
7572     ;; children.
7573     (while (setq d (pop data))
7574       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7575                 (and (setq children
7576                            (gnus-article-children (gnus-data-number d)))
7577                      (let (found)
7578                        (while children
7579                          (when (memq (car children) articles)
7580                            (setq children nil
7581                                  found t))
7582                          (pop children))
7583                        found)))
7584         (push (gnus-data-number d) articles)))
7585     ;; Do the limiting.
7586     (prog1
7587         (gnus-summary-limit articles)
7588       (gnus-summary-position-point))))
7589
7590 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7591   "Mark all unread excluded articles as read.
7592 If ALL, mark even excluded ticked and dormants as read."
7593   (interactive "P")
7594   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7595   (let ((articles (gnus-sorted-ndifference
7596                    (sort
7597                     (mapcar (lambda (h) (mail-header-number h))
7598                             gnus-newsgroup-headers)
7599                     '<)
7600                    gnus-newsgroup-limit))
7601         article)
7602     (setq gnus-newsgroup-unreads
7603           (gnus-sorted-intersection gnus-newsgroup-unreads
7604                                     gnus-newsgroup-limit))
7605     (if all
7606         (setq gnus-newsgroup-dormant nil
7607               gnus-newsgroup-marked nil
7608               gnus-newsgroup-reads
7609               (nconc
7610                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7611                gnus-newsgroup-reads))
7612       (while (setq article (pop articles))
7613         (unless (or (memq article gnus-newsgroup-dormant)
7614                     (memq article gnus-newsgroup-marked))
7615           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7616
7617 (defun gnus-summary-limit (articles &optional pop)
7618   (if pop
7619       ;; We pop the previous limit off the stack and use that.
7620       (setq articles (car gnus-newsgroup-limits)
7621             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7622     ;; We use the new limit, so we push the old limit on the stack.
7623     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7624   ;; Set the limit.
7625   (setq gnus-newsgroup-limit articles)
7626   (let ((total (length gnus-newsgroup-data))
7627         (data (gnus-data-find-list (gnus-summary-article-number)))
7628         (gnus-summary-mark-below nil)   ; Inhibit this.
7629         found)
7630     ;; This will do all the work of generating the new summary buffer
7631     ;; according to the new limit.
7632     (gnus-summary-prepare)
7633     ;; Hide any threads, possibly.
7634     (gnus-summary-maybe-hide-threads)
7635     ;; Try to return to the article you were at, or one in the
7636     ;; neighborhood.
7637     (when data
7638       ;; We try to find some article after the current one.
7639       (while data
7640         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7641           (setq data nil
7642                 found t))
7643         (setq data (cdr data))))
7644     (unless found
7645       ;; If there is no data, that means that we were after the last
7646       ;; article.  The same goes when we can't find any articles
7647       ;; after the current one.
7648       (goto-char (point-max))
7649       (gnus-summary-find-prev))
7650     (gnus-set-mode-line 'summary)
7651     ;; We return how many articles were removed from the summary
7652     ;; buffer as a result of the new limit.
7653     (- total (length gnus-newsgroup-data))))
7654
7655 (defsubst gnus-invisible-cut-children (threads)
7656   (let ((num 0))
7657     (while threads
7658       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7659         (incf num))
7660       (pop threads))
7661     (< num 2)))
7662
7663 (defsubst gnus-cut-thread (thread)
7664   "Go forwards in the thread until we find an article that we want to display."
7665   (when (or (eq gnus-fetch-old-headers 'some)
7666             (eq gnus-fetch-old-headers 'invisible)
7667             (numberp gnus-fetch-old-headers)
7668             (eq gnus-build-sparse-threads 'some)
7669             (eq gnus-build-sparse-threads 'more))
7670     ;; Deal with old-fetched headers and sparse threads.
7671     (while (and
7672             thread
7673             (or
7674              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7675              (gnus-summary-article-ancient-p
7676               (mail-header-number (car thread))))
7677             (if (or (<= (length (cdr thread)) 1)
7678                     (eq gnus-fetch-old-headers 'invisible))
7679                 (setq gnus-newsgroup-limit
7680                       (delq (mail-header-number (car thread))
7681                             gnus-newsgroup-limit)
7682                       thread (cadr thread))
7683               (when (gnus-invisible-cut-children (cdr thread))
7684                 (let ((th (cdr thread)))
7685                   (while th
7686                     (if (memq (mail-header-number (caar th))
7687                               gnus-newsgroup-limit)
7688                         (setq thread (car th)
7689                               th nil)
7690                       (setq th (cdr th))))))))))
7691   thread)
7692
7693 (defun gnus-cut-threads (threads)
7694   "Cut off all uninteresting articles from the beginning of threads."
7695   (when (or (eq gnus-fetch-old-headers 'some)
7696             (eq gnus-fetch-old-headers 'invisible)
7697             (numberp gnus-fetch-old-headers)
7698             (eq gnus-build-sparse-threads 'some)
7699             (eq gnus-build-sparse-threads 'more))
7700     (let ((th threads))
7701       (while th
7702         (setcar th (gnus-cut-thread (car th)))
7703         (setq th (cdr th)))))
7704   ;; Remove nixed out threads.
7705   (delq nil threads))
7706
7707 (defun gnus-summary-initial-limit (&optional show-if-empty)
7708   "Figure out what the initial limit is supposed to be on group entry.
7709 This entails weeding out unwanted dormants, low-scored articles,
7710 fetch-old-headers verbiage, and so on."
7711   ;; Most groups have nothing to remove.
7712   (if (or gnus-inhibit-limiting
7713           (and (null gnus-newsgroup-dormant)
7714                (eq gnus-newsgroup-display 'gnus-not-ignore)
7715                (not (eq gnus-fetch-old-headers 'some))
7716                (not (numberp gnus-fetch-old-headers))
7717                (not (eq gnus-fetch-old-headers 'invisible))
7718                (null gnus-summary-expunge-below)
7719                (not (eq gnus-build-sparse-threads 'some))
7720                (not (eq gnus-build-sparse-threads 'more))
7721                (null gnus-thread-expunge-below)
7722                (not gnus-use-nocem)))
7723       ()                                ; Do nothing.
7724     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7725     (setq gnus-newsgroup-limit nil)
7726     (mapatoms
7727      (lambda (node)
7728        (unless (car (symbol-value node))
7729          ;; These threads have no parents -- they are roots.
7730          (let ((nodes (cdr (symbol-value node)))
7731                thread)
7732            (while nodes
7733              (if (and gnus-thread-expunge-below
7734                       (< (gnus-thread-total-score (car nodes))
7735                          gnus-thread-expunge-below))
7736                  (gnus-expunge-thread (pop nodes))
7737                (setq thread (pop nodes))
7738                (gnus-summary-limit-children thread))))))
7739      gnus-newsgroup-dependencies)
7740     ;; If this limitation resulted in an empty group, we might
7741     ;; pop the previous limit and use it instead.
7742     (when (and (not gnus-newsgroup-limit)
7743                show-if-empty)
7744       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7745     gnus-newsgroup-limit))
7746
7747 (defun gnus-summary-limit-children (thread)
7748   "Return 1 if this subthread is visible and 0 if it is not."
7749   ;; First we get the number of visible children to this thread.  This
7750   ;; is done by recursing down the thread using this function, so this
7751   ;; will really go down to a leaf article first, before slowly
7752   ;; working its way up towards the root.
7753   (when thread
7754     (let* ((max-lisp-eval-depth 5000)
7755            (children
7756            (if (cdr thread)
7757                (apply '+ (mapcar 'gnus-summary-limit-children
7758                                  (cdr thread)))
7759              0))
7760           (number (mail-header-number (car thread)))
7761           score)
7762       (if (and
7763            (not (memq number gnus-newsgroup-marked))
7764            (or
7765             ;; If this article is dormant and has absolutely no visible
7766             ;; children, then this article isn't visible.
7767             (and (memq number gnus-newsgroup-dormant)
7768                  (zerop children))
7769             ;; If this is "fetch-old-headered" and there is no
7770             ;; visible children, then we don't want this article.
7771             (and (or (eq gnus-fetch-old-headers 'some)
7772                      (numberp gnus-fetch-old-headers))
7773                  (gnus-summary-article-ancient-p number)
7774                  (zerop children))
7775             ;; If this is "fetch-old-headered" and `invisible', then
7776             ;; we don't want this article.
7777             (and (eq gnus-fetch-old-headers 'invisible)
7778                  (gnus-summary-article-ancient-p number))
7779             ;; If this is a sparsely inserted article with no children,
7780             ;; we don't want it.
7781             (and (eq gnus-build-sparse-threads 'some)
7782                  (gnus-summary-article-sparse-p number)
7783                  (zerop children))
7784             ;; If we use expunging, and this article is really
7785             ;; low-scored, then we don't want this article.
7786             (when (and gnus-summary-expunge-below
7787                        (< (setq score
7788                                 (or (cdr (assq number gnus-newsgroup-scored))
7789                                     gnus-summary-default-score))
7790                           gnus-summary-expunge-below))
7791               ;; We increase the expunge-tally here, but that has
7792               ;; nothing to do with the limits, really.
7793               (incf gnus-newsgroup-expunged-tally)
7794               ;; We also mark as read here, if that's wanted.
7795               (when (and gnus-summary-mark-below
7796                          (< score gnus-summary-mark-below))
7797                 (setq gnus-newsgroup-unreads
7798                       (delq number gnus-newsgroup-unreads))
7799                 (if gnus-newsgroup-auto-expire
7800                     (push number gnus-newsgroup-expirable)
7801                   (push (cons number gnus-low-score-mark)
7802                         gnus-newsgroup-reads)))
7803               t)
7804             ;; Do the `display' group parameter.
7805             (and gnus-newsgroup-display
7806                  (not (funcall gnus-newsgroup-display)))
7807             ;; Check NoCeM things.
7808             (if (and gnus-use-nocem
7809                      (gnus-nocem-unwanted-article-p
7810                       (mail-header-id (car thread))))
7811                 (progn
7812                   (setq gnus-newsgroup-unreads
7813                         (delq number gnus-newsgroup-unreads))
7814                   t))))
7815           ;; Nope, invisible article.
7816           0
7817         ;; Ok, this article is to be visible, so we add it to the limit
7818         ;; and return 1.
7819         (push number gnus-newsgroup-limit)
7820         1))))
7821
7822 (defun gnus-expunge-thread (thread)
7823   "Mark all articles in THREAD as read."
7824   (let* ((number (mail-header-number (car thread))))
7825     (incf gnus-newsgroup-expunged-tally)
7826     ;; We also mark as read here, if that's wanted.
7827     (setq gnus-newsgroup-unreads
7828           (delq number gnus-newsgroup-unreads))
7829     (if gnus-newsgroup-auto-expire
7830         (push number gnus-newsgroup-expirable)
7831       (push (cons number gnus-low-score-mark)
7832             gnus-newsgroup-reads)))
7833   ;; Go recursively through all subthreads.
7834   (mapcar 'gnus-expunge-thread (cdr thread)))
7835
7836 ;; Summary article oriented commands
7837
7838 (defun gnus-summary-refer-parent-article (n)
7839   "Refer parent article N times.
7840 If N is negative, go to ancestor -N instead.
7841 The difference between N and the number of articles fetched is returned."
7842   (interactive "p")
7843   (let ((skip 1)
7844         error header ref)
7845     (when (not (natnump n))
7846       (setq skip (abs n)
7847             n 1))
7848     (while (and (> n 0)
7849                 (not error))
7850       (setq header (gnus-summary-article-header))
7851       (if (and (eq (mail-header-number header)
7852                    (cdr gnus-article-current))
7853                (equal gnus-newsgroup-name
7854                       (car gnus-article-current)))
7855           ;; If we try to find the parent of the currently
7856           ;; displayed article, then we take a look at the actual
7857           ;; References header, since this is slightly more
7858           ;; reliable than the References field we got from the
7859           ;; server.
7860           (save-excursion
7861             (set-buffer gnus-original-article-buffer)
7862             (nnheader-narrow-to-headers)
7863             (unless (setq ref (message-fetch-field "references"))
7864               (setq ref (message-fetch-field "in-reply-to")))
7865             (widen))
7866         (setq ref
7867               ;; It's not the current article, so we take a bet on
7868               ;; the value we got from the server.
7869               (mail-header-references header)))
7870       (if (and ref
7871                (not (equal ref "")))
7872           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7873             (gnus-message 1 "Couldn't find parent"))
7874         (gnus-message 1 "No references in article %d"
7875                       (gnus-summary-article-number))
7876         (setq error t))
7877       (decf n))
7878     (gnus-summary-position-point)
7879     n))
7880
7881 (defun gnus-summary-refer-references ()
7882   "Fetch all articles mentioned in the References header.
7883 Return the number of articles fetched."
7884   (interactive)
7885   (let ((ref (mail-header-references (gnus-summary-article-header)))
7886         (current (gnus-summary-article-number))
7887         (n 0))
7888     (if (or (not ref)
7889             (equal ref ""))
7890         (error "No References in the current article")
7891       ;; For each Message-ID in the References header...
7892       (while (string-match "<[^>]*>" ref)
7893         (incf n)
7894         ;; ... fetch that article.
7895         (gnus-summary-refer-article
7896          (prog1 (match-string 0 ref)
7897            (setq ref (substring ref (match-end 0))))))
7898       (gnus-summary-goto-subject current)
7899       (gnus-summary-position-point)
7900       n)))
7901
7902 (defun gnus-summary-refer-thread (&optional limit)
7903   "Fetch all articles in the current thread.
7904 If LIMIT (the numerical prefix), fetch that many old headers instead
7905 of what's specified by the `gnus-refer-thread-limit' variable."
7906   (interactive "P")
7907   (let ((id (mail-header-id (gnus-summary-article-header)))
7908         (limit (if limit (prefix-numeric-value limit)
7909                  gnus-refer-thread-limit)))
7910     ;; We want to fetch LIMIT *old* headers, but we also have to
7911     ;; re-fetch all the headers in the current buffer, because many of
7912     ;; them may be undisplayed.  So we adjust LIMIT.
7913     (when (numberp limit)
7914       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7915     (unless (eq gnus-fetch-old-headers 'invisible)
7916       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7917       ;; Retrieve the headers and read them in.
7918       (if (eq (gnus-retrieve-headers
7919                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7920               'nov)
7921           (gnus-build-all-threads)
7922         (error "Can't fetch thread from backends that don't support NOV"))
7923       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7924     (gnus-summary-limit-include-thread id)))
7925
7926 (defun gnus-summary-refer-article (message-id)
7927   "Fetch an article specified by MESSAGE-ID."
7928   (interactive "sMessage-ID: ")
7929   (when (and (stringp message-id)
7930              (not (zerop (length message-id))))
7931     ;; Construct the correct Message-ID if necessary.
7932     ;; Suggested by tale@pawl.rpi.edu.
7933     (unless (string-match "^<" message-id)
7934       (setq message-id (concat "<" message-id)))
7935     (unless (string-match ">$" message-id)
7936       (setq message-id (concat message-id ">")))
7937     (let* ((header (gnus-id-to-header message-id))
7938            (sparse (and header
7939                         (gnus-summary-article-sparse-p
7940                          (mail-header-number header))
7941                         (memq (mail-header-number header)
7942                               gnus-newsgroup-limit)))
7943            number)
7944       (cond
7945        ;; If the article is present in the buffer we just go to it.
7946        ((and header
7947              (or (not (gnus-summary-article-sparse-p
7948                        (mail-header-number header)))
7949                  sparse))
7950         (prog1
7951             (gnus-summary-goto-article
7952              (mail-header-number header) nil t)
7953           (when sparse
7954             (gnus-summary-update-article (mail-header-number header)))))
7955        (t
7956         ;; We fetch the article.
7957         (catch 'found
7958           (dolist (gnus-override-method (gnus-refer-article-methods))
7959             (gnus-check-server gnus-override-method)
7960             ;; Fetch the header, and display the article.
7961             (when (setq number (gnus-summary-insert-subject message-id))
7962               (gnus-summary-select-article nil nil nil number)
7963               (throw 'found t)))
7964           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7965
7966 (defun gnus-refer-article-methods ()
7967   "Return a list of referable methods."
7968   (cond
7969    ;; No method, so we default to current and native.
7970    ((null gnus-refer-article-method)
7971     (list gnus-current-select-method gnus-select-method))
7972    ;; Current.
7973    ((eq 'current gnus-refer-article-method)
7974     (list gnus-current-select-method))
7975    ;; List of select methods.
7976    ((not (and (symbolp (car gnus-refer-article-method))
7977               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7978     (let (out)
7979       (dolist (method gnus-refer-article-method)
7980         (push (if (eq 'current method)
7981                   gnus-current-select-method
7982                 method)
7983               out))
7984       (nreverse out)))
7985    ;; One single select method.
7986    (t
7987     (list gnus-refer-article-method))))
7988
7989 (defun gnus-summary-edit-parameters ()
7990   "Edit the group parameters of the current group."
7991   (interactive)
7992   (gnus-group-edit-group gnus-newsgroup-name 'params))
7993
7994 (defun gnus-summary-customize-parameters ()
7995   "Customize the group parameters of the current group."
7996   (interactive)
7997   (gnus-group-customize gnus-newsgroup-name))
7998
7999 (defun gnus-summary-enter-digest-group (&optional force)
8000   "Enter an nndoc group based on the current article.
8001 If FORCE, force a digest interpretation.  If not, try
8002 to guess what the document format is."
8003   (interactive "P")
8004   (let ((conf gnus-current-window-configuration))
8005     (save-excursion
8006       (gnus-summary-select-article))
8007     (setq gnus-current-window-configuration conf)
8008     (let* ((name (format "%s-%d"
8009                          (gnus-group-prefixed-name
8010                           gnus-newsgroup-name (list 'nndoc ""))
8011                          (save-excursion
8012                            (set-buffer gnus-summary-buffer)
8013                            gnus-current-article)))
8014            (ogroup gnus-newsgroup-name)
8015            (params (append (gnus-info-params (gnus-get-info ogroup))
8016                            (list (cons 'to-group ogroup))
8017                            (list (cons 'save-article-group ogroup))))
8018            (case-fold-search t)
8019            (buf (current-buffer))
8020            dig to-address)
8021       (save-excursion
8022         (set-buffer gnus-original-article-buffer)
8023         ;; Have the digest group inherit the main mail address of
8024         ;; the parent article.
8025         (when (setq to-address (or (gnus-fetch-field "reply-to")
8026                                    (gnus-fetch-field "from")))
8027           (setq params (append
8028                         (list (cons 'to-address
8029                                     (funcall gnus-decode-encoded-word-function
8030                                              to-address))))))
8031         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8032         (insert-buffer-substring gnus-original-article-buffer)
8033         ;; Remove lines that may lead nndoc to misinterpret the
8034         ;; document type.
8035         (narrow-to-region
8036          (goto-char (point-min))
8037          (or (search-forward "\n\n" nil t) (point)))
8038         (goto-char (point-min))
8039         (delete-matching-lines "^Path:\\|^From ")
8040         (widen))
8041       (unwind-protect
8042           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8043                     (gnus-newsgroup-ephemeral-ignored-charsets
8044                      gnus-newsgroup-ignored-charsets))
8045                 (gnus-group-read-ephemeral-group
8046                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8047                               (nndoc-article-type
8048                                ,(if force 'mbox 'guess)))
8049                  t nil nil nil
8050                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8051                                                         "ADAPT")))))
8052               ;; Make all postings to this group go to the parent group.
8053               (nconc (gnus-info-params (gnus-get-info name))
8054                      params)
8055             ;; Couldn't select this doc group.
8056             (switch-to-buffer buf)
8057             (gnus-set-global-variables)
8058             (gnus-configure-windows 'summary)
8059             (gnus-message 3 "Article couldn't be entered?"))
8060         (kill-buffer dig)))))
8061
8062 (defun gnus-summary-read-document (n)
8063   "Open a new group based on the current article(s).
8064 This will allow you to read digests and other similar
8065 documents as newsgroups.
8066 Obeys the standard process/prefix convention."
8067   (interactive "P")
8068   (let* ((articles (gnus-summary-work-articles n))
8069          (ogroup gnus-newsgroup-name)
8070          (params (append (gnus-info-params (gnus-get-info ogroup))
8071                          (list (cons 'to-group ogroup))))
8072          article group egroup groups vgroup)
8073     (while (setq article (pop articles))
8074       (setq group (format "%s-%d" gnus-newsgroup-name article))
8075       (gnus-summary-remove-process-mark article)
8076       (when (gnus-summary-display-article article)
8077         (save-excursion
8078           (with-temp-buffer
8079             (insert-buffer-substring gnus-original-article-buffer)
8080             ;; Remove some headers that may lead nndoc to make
8081             ;; the wrong guess.
8082             (message-narrow-to-head)
8083             (goto-char (point-min))
8084             (delete-matching-lines "^\\(Path\\):\\|^From ")
8085             (widen)
8086             (if (setq egroup
8087                       (gnus-group-read-ephemeral-group
8088                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8089                                      (nndoc-article-type guess))
8090                        t nil t))
8091                 (progn
8092                   ;; Make all postings to this group go to the parent group.
8093                   (nconc (gnus-info-params (gnus-get-info egroup))
8094                          params)
8095                   (push egroup groups))
8096               ;; Couldn't select this doc group.
8097               (gnus-error 3 "Article couldn't be entered"))))))
8098     ;; Now we have selected all the documents.
8099     (cond
8100      ((not groups)
8101       (error "None of the articles could be interpreted as documents"))
8102      ((gnus-group-read-ephemeral-group
8103        (setq vgroup (format
8104                      "nnvirtual:%s-%s" gnus-newsgroup-name
8105                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8106        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8107        t
8108        (cons (current-buffer) 'summary)))
8109      (t
8110       (error "Couldn't select virtual nndoc group")))))
8111
8112 (defun gnus-summary-isearch-article (&optional regexp-p)
8113   "Do incremental search forward on the current article.
8114 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8115   (interactive "P")
8116   (let* ((gnus-inhibit-treatment t)
8117          (old (gnus-summary-select-article)))
8118     (gnus-configure-windows 'article)
8119     (gnus-eval-in-buffer-window gnus-article-buffer
8120       (save-restriction
8121         (widen)
8122         (when (eq 'old old)
8123           (gnus-article-show-all-headers))
8124         (goto-char (point-min))
8125         (isearch-forward regexp-p)))))
8126
8127 (defun gnus-summary-search-article-forward (regexp &optional backward)
8128   "Search for an article containing REGEXP forward.
8129 If BACKWARD, search backward instead."
8130   (interactive
8131    (list (read-string
8132           (format "Search article %s (regexp%s): "
8133                   (if current-prefix-arg "backward" "forward")
8134                   (if gnus-last-search-regexp
8135                       (concat ", default " gnus-last-search-regexp)
8136                     "")))
8137          current-prefix-arg))
8138   (if (string-equal regexp "")
8139       (setq regexp (or gnus-last-search-regexp ""))
8140     (setq gnus-last-search-regexp regexp)
8141     (setq gnus-article-before-search gnus-current-article))
8142   ;; Intentionally set gnus-last-article.
8143   (setq gnus-last-article gnus-article-before-search)
8144   (let ((gnus-last-article gnus-last-article))
8145     (if (gnus-summary-search-article regexp backward)
8146         (gnus-summary-show-thread)
8147       (error "Search failed: \"%s\"" regexp))))
8148
8149 (defun gnus-summary-search-article-backward (regexp)
8150   "Search for an article containing REGEXP backward."
8151   (interactive
8152    (list (read-string
8153           (format "Search article backward (regexp%s): "
8154                   (if gnus-last-search-regexp
8155                       (concat ", default " gnus-last-search-regexp)
8156                     "")))))
8157   (gnus-summary-search-article-forward regexp 'backward))
8158
8159 (eval-when-compile
8160   (defmacro gnus-summary-search-article-position-point (regexp backward)
8161     "Dehighlight the last matched text and goto the beginning position."
8162     (` (if (and gnus-summary-search-article-matched-data
8163                 (let ((text (caddr gnus-summary-search-article-matched-data))
8164                       (inhibit-read-only t)
8165                       buffer-read-only)
8166                   (delete-region
8167                    (goto-char (car gnus-summary-search-article-matched-data))
8168                    (cadr gnus-summary-search-article-matched-data))
8169                   (insert text)
8170                   (string-match (, regexp) text)))
8171            (if (, backward) (beginning-of-line) (end-of-line))
8172          (goto-char (if (, backward) (point-max) (point-min))))))
8173
8174   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8175     "Place point where X-Face image is displayed."
8176     (if (featurep 'xemacs)
8177         (` (let ((end (if (search-forward "\n\n" nil t)
8178                           (goto-char (1- (point)))
8179                         (point-min)))
8180                  extent)
8181              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8182              (unless (and (re-search-forward "^From:" end t)
8183                           (setq extent (extent-at (point)))
8184                           (extent-begin-glyph extent))
8185                (goto-char (, opoint)))))
8186       (` (let ((end (if (search-forward "\n\n" nil t)
8187                         (goto-char (1- (point)))
8188                       (point-min)))
8189                (start (or (search-backward "\n\n" nil t) (point-min))))
8190            (goto-char
8191             (or (text-property-any start end 'x-face-image t);; x-face-e21
8192                 (text-property-any start end 'x-face-mule-bitmap-image t)
8193                 (, opoint)))))))
8194
8195   (defmacro gnus-summary-search-article-highlight-matched-text
8196     (backward treated x-face)
8197     "Highlight matched text in the function `gnus-summary-search-article'."
8198     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8199              (end (set-marker (make-marker) (match-end 0)))
8200              (inhibit-read-only t)
8201              buffer-read-only)
8202          (unless treated
8203            (let ((,@
8204                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8205                     (mapcar
8206                      (lambda (item) (setq items (delq item items)))
8207                      '(gnus-treat-buttonize
8208                        gnus-treat-fill-article
8209                        gnus-treat-fill-long-lines
8210                        gnus-treat-emphasize
8211                        gnus-treat-highlight-headers
8212                        gnus-treat-highlight-citation
8213                        gnus-treat-highlight-signature
8214                        gnus-treat-overstrike
8215                        gnus-treat-display-xface
8216                        gnus-treat-buttonize-head
8217                        gnus-treat-decode-article-as-default-mime-charset))
8218                     (static-if (featurep 'xemacs)
8219                         items
8220                       (cons '(x-face-mule-delete-x-face-field
8221                               (quote never))
8222                             items))))
8223                  (gnus-treat-display-xface
8224                   (when (, x-face) gnus-treat-display-xface)))
8225              (gnus-article-prepare-mime-display)))
8226          (goto-char (if (, backward) start end))
8227          (when (, x-face)
8228            (gnus-summary-search-article-highlight-goto-x-face (point)))
8229          (setq gnus-summary-search-article-matched-data
8230                (list start end (buffer-substring start end)))
8231          (unless (eq start end);; matched text has been deleted. :-<
8232            (put-text-property start end 'face
8233                               (or (find-face 'isearch)
8234                                   'secondary-selection))))))
8235   )
8236
8237 (defun gnus-summary-search-article (regexp &optional backward)
8238   "Search for an article containing REGEXP.
8239 Optional argument BACKWARD means do search for backward.
8240 `gnus-select-article-hook' is not called during the search."
8241   ;; We have to require this here to make sure that the following
8242   ;; dynamic binding isn't shadowed by autoloading.
8243   (require 'gnus-async)
8244   (require 'gnus-art)
8245   (let ((gnus-select-article-hook nil)  ;Disable hook.
8246         (gnus-article-prepare-hook nil)
8247         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8248         (gnus-use-article-prefetch nil)
8249         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8250         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8251         (sum (current-buffer))
8252         (found nil)
8253         point treated)
8254     (gnus-save-hidden-threads
8255       (static-if (featurep 'xemacs)
8256           (let ((gnus-inhibit-treatment t))
8257             (setq treated (eq 'old (gnus-summary-select-article)))
8258             (when (and treated
8259                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8260                                  (window-live-p (get-buffer-window
8261                                                  gnus-article-buffer t)))))
8262               (gnus-summary-select-article nil t)
8263               (setq treated nil)))
8264         (let ((gnus-inhibit-treatment t)
8265               (x-face-mule-delete-x-face-field 'never))
8266           (setq treated (eq 'old (gnus-summary-select-article)))
8267           (when (and treated
8268                      (not
8269                       (and (gnus-buffer-live-p gnus-article-buffer)
8270                            (window-live-p (get-buffer-window
8271                                            gnus-article-buffer t))
8272                            (or (not (string-match "^\\^X-Face:" regexp))
8273                                (with-current-buffer gnus-article-buffer
8274                                  gnus-summary-search-article-matched-data)))))
8275             (gnus-summary-select-article nil t)
8276             (setq treated nil))))
8277       (set-buffer gnus-article-buffer)
8278       (widen)
8279       (if treated
8280           (progn
8281             (gnus-article-show-all-headers)
8282             (gnus-summary-search-article-position-point regexp backward))
8283         (goto-char (if backward (point-max) (point-min))))
8284       (while (not found)
8285         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8286         (if (if backward
8287                 (re-search-backward regexp nil t)
8288               (re-search-forward regexp nil t))
8289             ;; We found the regexp.
8290             (progn
8291               (gnus-summary-search-article-highlight-matched-text
8292                backward treated (string-match "^\\^X-Face:" regexp))
8293               (setq found 'found)
8294               (forward-line
8295                (/ (- 2 (window-height
8296                         (get-buffer-window gnus-article-buffer t)))
8297                   2))
8298               (set-window-start
8299                (get-buffer-window (current-buffer))
8300                (point))
8301               (set-buffer sum)
8302               (setq point (point)))
8303           ;; We didn't find it, so we go to the next article.
8304           (set-buffer sum)
8305           (setq found 'not)
8306           (while (eq found 'not)
8307             (if (not (if backward (gnus-summary-find-prev)
8308                        (gnus-summary-find-next)))
8309                 ;; No more articles.
8310                 (setq found t)
8311               ;; Select the next article and adjust point.
8312               (unless (gnus-summary-article-sparse-p
8313                        (gnus-summary-article-number))
8314                 (setq found nil)
8315                 (let ((gnus-inhibit-treatment t))
8316                   (gnus-summary-select-article))
8317                 (setq treated nil)
8318                 (set-buffer gnus-article-buffer)
8319                 (widen)
8320                 (goto-char (if backward (point-max) (point-min))))))))
8321       (gnus-message 7 ""))
8322     ;; Return whether we found the regexp.
8323     (when (eq found 'found)
8324       (goto-char point)
8325       (gnus-summary-show-thread)
8326       (gnus-summary-goto-subject gnus-current-article)
8327       (gnus-summary-position-point)
8328       t)))
8329
8330 (defun gnus-find-matching-articles (header regexp)
8331   "Return a list of all articles that match REGEXP on HEADER.
8332 This search includes all articles in the current group that Gnus has
8333 fetched headers for, whether they are displayed or not."
8334   (let ((articles nil)
8335         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8336         (case-fold-search t))
8337     (dolist (header gnus-newsgroup-headers)
8338       (when (string-match regexp (funcall func header))
8339         (push (mail-header-number header) articles)))
8340     (nreverse articles)))
8341
8342 (defun gnus-summary-find-matching (header regexp &optional backward unread
8343                                           not-case-fold not-matching)
8344   "Return a list of all articles that match REGEXP on HEADER.
8345 The search stars on the current article and goes forwards unless
8346 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8347 If UNREAD is non-nil, only unread articles will
8348 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8349 in the comparisons. If NOT-MATCHING, return a list of all articles that
8350 not match REGEXP on HEADER."
8351   (let ((case-fold-search (not not-case-fold))
8352         articles d func)
8353     (if (consp header)
8354         (if (eq (car header) 'extra)
8355             (setq func
8356                   `(lambda (h)
8357                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8358                          "")))
8359           (error "%s is an invalid header" header))
8360       (unless (fboundp (intern (concat "mail-header-" header)))
8361         (error "%s is not a valid header" header))
8362       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8363     (dolist (d (if (eq backward 'all)
8364                    gnus-newsgroup-data
8365                  (gnus-data-find-list
8366                   (gnus-summary-article-number)
8367                   (gnus-data-list backward))))
8368       (when (and (or (not unread)       ; We want all articles...
8369                      (gnus-data-unread-p d)) ; Or just unreads.
8370                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8371                  (if not-matching
8372                      (not (string-match
8373                            regexp
8374                            (funcall func (gnus-data-header d))))
8375                    (string-match regexp
8376                                  (funcall func (gnus-data-header d)))))
8377         (push (gnus-data-number d) articles))) ; Success!
8378     (nreverse articles)))
8379
8380 (defun gnus-summary-execute-command (header regexp command &optional backward)
8381   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8382 If HEADER is an empty string (or nil), the match is done on the entire
8383 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8384   (interactive
8385    (list (let ((completion-ignore-case t))
8386            (completing-read
8387             "Header name: "
8388             (mapcar (lambda (header) (list (format "%s" header)))
8389                     (append
8390                      '("Number" "Subject" "From" "Lines" "Date"
8391                        "Message-ID" "Xref" "References" "Body")
8392                      gnus-extra-headers))
8393             nil 'require-match))
8394          (read-string "Regexp: ")
8395          (read-key-sequence "Command: ")
8396          current-prefix-arg))
8397   (when (equal header "Body")
8398     (setq header ""))
8399   ;; Hidden thread subtrees must be searched as well.
8400   (gnus-summary-show-all-threads)
8401   ;; We don't want to change current point nor window configuration.
8402   (save-excursion
8403     (save-window-excursion
8404       (let (gnus-visual
8405             gnus-treat-strip-trailing-blank-lines
8406             gnus-treat-strip-leading-blank-lines
8407             gnus-treat-strip-multiple-blank-lines
8408             gnus-treat-hide-boring-headers
8409             gnus-treat-fold-newsgroups
8410             gnus-article-prepare-hook)
8411         (gnus-message 6 "Executing %s..." (key-description command))
8412         ;; We'd like to execute COMMAND interactively so as to give arguments.
8413         (gnus-execute header regexp
8414                       `(call-interactively ',(key-binding command))
8415                       backward)
8416         (gnus-message 6 "Executing %s...done" (key-description command))))))
8417
8418 (defun gnus-summary-beginning-of-article ()
8419   "Scroll the article back to the beginning."
8420   (interactive)
8421   (gnus-summary-select-article)
8422   (gnus-configure-windows 'article)
8423   (gnus-eval-in-buffer-window gnus-article-buffer
8424     (widen)
8425     (goto-char (point-min))
8426     (when gnus-page-broken
8427       (gnus-narrow-to-page))))
8428
8429 (defun gnus-summary-end-of-article ()
8430   "Scroll to the end of the article."
8431   (interactive)
8432   (gnus-summary-select-article)
8433   (gnus-configure-windows 'article)
8434   (gnus-eval-in-buffer-window gnus-article-buffer
8435     (widen)
8436     (goto-char (point-max))
8437     (recenter -3)
8438     (when gnus-page-broken
8439       (gnus-narrow-to-page))))
8440
8441 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8442   "Truncate to LEN and quote all \"(\"'s in STRING."
8443   (gnus-replace-in-string (if (and len (> (length string) len))
8444                               (substring string 0 len)
8445                             string)
8446                           "[()]" "\\\\\\&"))
8447
8448 (defun gnus-summary-print-article (&optional filename n)
8449   "Generate and print a PostScript image of the N next (mail) articles.
8450
8451 If N is negative, print the N previous articles.  If N is nil and articles
8452 have been marked with the process mark, print these instead.
8453
8454 If the optional first argument FILENAME is nil, send the image to the
8455 printer.  If FILENAME is a string, save the PostScript image in a file with
8456 that name.  If FILENAME is a number, prompt the user for the name of the file
8457 to save in."
8458   (interactive (list (ps-print-preprint current-prefix-arg)))
8459   (dolist (article (gnus-summary-work-articles n))
8460     (gnus-summary-select-article nil nil 'pseudo article)
8461     (gnus-eval-in-buffer-window gnus-article-buffer
8462       (gnus-print-buffer))
8463     (gnus-summary-remove-process-mark article))
8464   (ps-despool filename))
8465
8466 (defun gnus-print-buffer ()
8467   (let ((buffer (generate-new-buffer " *print*")))
8468     (unwind-protect
8469         (progn
8470           (copy-to-buffer buffer (point-min) (point-max))
8471           (set-buffer buffer)
8472           (gnus-article-delete-invisible-text)
8473           (gnus-remove-text-with-property 'gnus-decoration)
8474           (when (gnus-visual-p 'article-highlight 'highlight)
8475             ;; Copy-to-buffer doesn't copy overlay.  So redo
8476             ;; highlight.
8477             (let ((gnus-article-buffer buffer))
8478               (gnus-article-highlight-citation t)
8479               (gnus-article-highlight-signature)))
8480           (let ((ps-left-header
8481                  (list
8482                   (concat "("
8483                           (gnus-summary-print-truncate-and-quote
8484                            (mail-header-subject gnus-current-headers)
8485                            66) ")")
8486                   (concat "("
8487                           (gnus-summary-print-truncate-and-quote
8488                            (mail-header-from gnus-current-headers)
8489                            45) ")")))
8490                 (ps-right-header
8491                  (list
8492                   "/pagenumberstring load"
8493                   (concat "("
8494                           (mail-header-date gnus-current-headers) ")"))))
8495             (gnus-run-hooks 'gnus-ps-print-hook)
8496             (save-excursion
8497               (if window-system
8498                   (ps-spool-buffer-with-faces)
8499                 (ps-spool-buffer)))))
8500       (kill-buffer buffer))))
8501
8502 (defun gnus-summary-show-article (&optional arg)
8503   "Force redisplaying of the current article.
8504 If ARG (the prefix) is a number, show the article with the charset
8505 defined in `gnus-summary-show-article-charset-alist', or the charset
8506 input.
8507 If ARG (the prefix) is non-nil and not a number, show the raw article
8508 without any article massaging functions being run.  Normally, the key strokes
8509 are `C-u g'."
8510   (interactive "P")
8511   (cond
8512    ((numberp arg)
8513     (gnus-summary-show-article t)
8514     (let* ((gnus-newsgroup-charset
8515             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8516                 (mm-read-coding-system
8517                  "View as charset: " ;; actually it is coding system.
8518                  (save-excursion
8519                    (set-buffer gnus-article-buffer)
8520                    (mm-detect-coding-region (point) (point-max))))))
8521            (default-mime-charset gnus-newsgroup-charset)
8522            (gnus-newsgroup-ignored-charsets 'gnus-all))
8523       (gnus-summary-select-article nil 'force)
8524       (let ((deps gnus-newsgroup-dependencies)
8525             head header lines)
8526         (save-excursion
8527           (set-buffer gnus-original-article-buffer)
8528           (save-restriction
8529             (message-narrow-to-head)
8530             (setq head (buffer-string))
8531             (goto-char (point-min))
8532             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8533               (goto-char (point-max))
8534               (widen)
8535               (setq lines (1- (count-lines (point) (point-max))))))
8536           (with-temp-buffer
8537             (insert (format "211 %d Article retrieved.\n"
8538                             (cdr gnus-article-current)))
8539             (insert head)
8540             (if lines (insert (format "Lines: %d\n" lines)))
8541             (insert ".\n")
8542             (let ((nntp-server-buffer (current-buffer)))
8543               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8544         (gnus-data-set-header
8545          (gnus-data-find (cdr gnus-article-current))
8546          header)
8547         (gnus-summary-update-article-line
8548          (cdr gnus-article-current) header)
8549         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8550           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8551    ((not arg)
8552     ;; Select the article the normal way.
8553     (gnus-summary-select-article nil 'force))
8554    (t
8555     ;; We have to require this here to make sure that the following
8556     ;; dynamic binding isn't shadowed by autoloading.
8557     (require 'gnus-async)
8558     (require 'gnus-art)
8559     ;; Bind the article treatment functions to nil.
8560     (let ((gnus-have-all-headers t)
8561           gnus-article-prepare-hook
8562           gnus-article-decode-hook
8563           gnus-break-pages
8564           gnus-show-mime
8565           (gnus-inhibit-treatment t))
8566       (gnus-summary-select-article nil 'force))))
8567   (gnus-summary-goto-subject gnus-current-article)
8568   (gnus-summary-position-point))
8569
8570 (defun gnus-summary-show-raw-article ()
8571   "Show the raw article without any article massaging functions being run."
8572   (interactive)
8573   (gnus-summary-show-article t))
8574
8575 (defun gnus-summary-verbose-headers (&optional arg)
8576   "Toggle permanent full header display.
8577 If ARG is a positive number, turn header display on.
8578 If ARG is a negative number, turn header display off."
8579   (interactive "P")
8580   (setq gnus-show-all-headers
8581         (cond ((or (not (numberp arg))
8582                    (zerop arg))
8583                (not gnus-show-all-headers))
8584               ((natnump arg)
8585                t)))
8586   (gnus-summary-show-article))
8587
8588 (defun gnus-summary-toggle-header (&optional arg)
8589   "Show the headers if they are hidden, or hide them if they are shown.
8590 If ARG is a positive number, show the entire header.
8591 If ARG is a negative number, hide the unwanted header lines."
8592   (interactive "P")
8593   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8594                      (get-buffer-window gnus-article-buffer t))))
8595     (with-current-buffer gnus-article-buffer
8596       (widen)
8597       (article-narrow-to-head)
8598       (let* ((buffer-read-only nil)
8599              (inhibit-point-motion-hooks t)
8600              (hidden (if (numberp arg)
8601                          (>= arg 0)
8602                        (gnus-article-hidden-text-p 'headers)))
8603              s e)
8604         (delete-region (point-min) (point-max))
8605         (with-current-buffer gnus-original-article-buffer
8606           (goto-char (setq s (point-min)))
8607           (setq e (if (search-forward "\n\n" nil t)
8608                       (1- (point))
8609                     (point-max))))
8610         (insert-buffer-substring gnus-original-article-buffer s e)
8611         (article-decode-encoded-words)
8612         (if hidden
8613             (let ((gnus-treat-hide-headers nil)
8614                   (gnus-treat-hide-boring-headers nil))
8615               (gnus-delete-wash-type 'headers)
8616               (gnus-treat-article 'head))
8617           (gnus-treat-article 'head))
8618         (widen)
8619         (if window
8620             (set-window-start window (goto-char (point-min))))
8621         (setq gnus-page-broken
8622               (when gnus-break-pages
8623                 (gnus-narrow-to-page)
8624                 t))
8625         (gnus-set-mode-line 'article)))))
8626
8627 (defun gnus-summary-show-all-headers ()
8628   "Make all header lines visible."
8629   (interactive)
8630   (gnus-summary-toggle-header 1))
8631
8632 (defun gnus-summary-toggle-mime (&optional arg)
8633   "Toggle MIME processing.
8634 If ARG is a positive number, turn MIME processing on."
8635   (interactive "P")
8636   (setq gnus-show-mime
8637         (if (null arg)
8638             (not gnus-show-mime)
8639           (> (prefix-numeric-value arg) 0)))
8640   (gnus-summary-select-article t 'force))
8641
8642 (defun gnus-summary-caesar-message (&optional arg)
8643   "Caesar rotate the current article by 13.
8644 The numerical prefix specifies how many places to rotate each letter
8645 forward."
8646   (interactive "P")
8647   (gnus-summary-select-article)
8648   (let ((mail-header-separator ""))
8649     (gnus-eval-in-buffer-window gnus-article-buffer
8650       (save-restriction
8651         (widen)
8652         (let ((start (window-start))
8653               buffer-read-only)
8654           (message-caesar-buffer-body arg)
8655           (set-window-start (get-buffer-window (current-buffer)) start))))))
8656
8657 (autoload 'unmorse-region "morse"
8658   "Convert morse coded text in region to ordinary ASCII text."
8659   t)
8660
8661 (defun gnus-summary-morse-message (&optional arg)
8662   "Morse decode the current article."
8663   (interactive "P")
8664   (gnus-summary-select-article)
8665   (let ((mail-header-separator ""))
8666     (gnus-eval-in-buffer-window gnus-article-buffer
8667       (save-excursion
8668         (save-restriction
8669           (widen)
8670           (let ((pos (window-start))
8671                 buffer-read-only)
8672             (goto-char (point-min))
8673             (when (message-goto-body)
8674               (gnus-narrow-to-body))
8675             (goto-char (point-min))
8676             (while (re-search-forward "·" (point-max) t)
8677               (replace-match "."))
8678             (unmorse-region (point-min) (point-max))
8679             (widen)
8680             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8681
8682 (defun gnus-summary-stop-page-breaking ()
8683   "Stop page breaking in the current article."
8684   (interactive)
8685   (gnus-summary-select-article)
8686   (gnus-eval-in-buffer-window gnus-article-buffer
8687     (widen)
8688     (when (gnus-visual-p 'page-marker)
8689       (let ((buffer-read-only nil))
8690         (gnus-remove-text-with-property 'gnus-prev)
8691         (gnus-remove-text-with-property 'gnus-next))
8692       (setq gnus-page-broken nil))))
8693
8694 (defun gnus-summary-move-article (&optional n to-newsgroup
8695                                             select-method action)
8696   "Move the current article to a different newsgroup.
8697 If N is a positive number, move the N next articles.
8698 If N is a negative number, move the N previous articles.
8699 If N is nil and any articles have been marked with the process mark,
8700 move those articles instead.
8701 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8702 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8703 re-spool using this method.
8704
8705 When called interactively with TO-NEWSGROUP being nil, the value of
8706 the variable `gnus-move-split-methods' is used for finding a default
8707 for the target newsgroup.
8708
8709 For this function to work, both the current newsgroup and the
8710 newsgroup that you want to move to have to support the `request-move'
8711 and `request-accept' functions.
8712
8713 ACTION can be either `move' (the default), `crosspost' or `copy'."
8714   (interactive "P")
8715   (unless action
8716     (setq action 'move))
8717   ;; Check whether the source group supports the required functions.
8718   (cond ((and (eq action 'move)
8719               (not (gnus-check-backend-function
8720                     'request-move-article gnus-newsgroup-name)))
8721          (error "The current group does not support article moving"))
8722         ((and (eq action 'crosspost)
8723               (not (gnus-check-backend-function
8724                     'request-replace-article gnus-newsgroup-name)))
8725          (error "The current group does not support article editing")))
8726   (let ((articles (gnus-summary-work-articles n))
8727         (prefix (if (gnus-check-backend-function
8728                      'request-move-article gnus-newsgroup-name)
8729                     (gnus-group-real-prefix gnus-newsgroup-name)
8730                   ""))
8731         (names '((move "Move" "Moving")
8732                  (copy "Copy" "Copying")
8733                  (crosspost "Crosspost" "Crossposting")))
8734         (copy-buf (save-excursion
8735                     (nnheader-set-temp-buffer " *copy article*")))
8736         (default-marks gnus-article-mark-lists)
8737         (no-expire-marks (delete '(expirable . expire)
8738                                  (copy-sequence gnus-article-mark-lists)))
8739         art-group to-method new-xref article to-groups)
8740     (unless (assq action names)
8741       (error "Unknown action %s" action))
8742     ;; Read the newsgroup name.
8743     (when (and (not to-newsgroup)
8744                (not select-method))
8745       (if (and gnus-move-split-methods
8746                (not
8747                 (and (memq gnus-current-article articles)
8748                      (gnus-buffer-live-p gnus-original-article-buffer))))
8749           ;; When `gnus-move-split-methods' is non-nil, we have to
8750           ;; select an article to give `gnus-read-move-group-name' an
8751           ;; opportunity to suggest an appropriate default.  However,
8752           ;; we needn't render or mark the article.
8753           (let ((gnus-display-mime-function nil)
8754                 (gnus-article-prepare-hook nil)
8755                 (gnus-mark-article-hook nil))
8756             (gnus-summary-select-article nil nil nil (car articles))))
8757       (setq to-newsgroup
8758             (gnus-read-move-group-name
8759              (cadr (assq action names))
8760              (symbol-value (intern (format "gnus-current-%s-group" action)))
8761              articles prefix))
8762       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8763     (setq to-method (or select-method
8764                         (gnus-server-to-method
8765                          (gnus-group-method to-newsgroup))))
8766     ;; Check the method we are to move this article to...
8767     (unless (gnus-check-backend-function
8768              'request-accept-article (car to-method))
8769       (error "%s does not support article copying" (car to-method)))
8770     (unless (gnus-check-server to-method)
8771       (error "Can't open server %s" (car to-method)))
8772     (gnus-message 6 "%s to %s: %s..."
8773                   (caddr (assq action names))
8774                   (or (car select-method) to-newsgroup) articles)
8775     (while articles
8776       (setq article (pop articles))
8777       (setq
8778        art-group
8779        (cond
8780         ;; Move the article.
8781         ((eq action 'move)
8782          ;; Remove this article from future suppression.
8783          (gnus-dup-unsuppress-article article)
8784          (gnus-request-move-article
8785           article                       ; Article to move
8786           gnus-newsgroup-name           ; From newsgroup
8787           (nth 1 (gnus-find-method-for-group
8788                   gnus-newsgroup-name)) ; Server
8789           (list 'gnus-request-accept-article
8790                 to-newsgroup (list 'quote select-method)
8791                 (not articles) t)       ; Accept form
8792           (not articles)))              ; Only save nov last time
8793         ;; Copy the article.
8794         ((eq action 'copy)
8795          (save-excursion
8796            (set-buffer copy-buf)
8797            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8798              (gnus-request-accept-article
8799               to-newsgroup select-method (not articles) t))))
8800         ;; Crosspost the article.
8801         ((eq action 'crosspost)
8802          (let ((xref (message-tokenize-header
8803                       (mail-header-xref (gnus-summary-article-header article))
8804                       " ")))
8805            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8806                                   ":" (number-to-string article)))
8807            (unless xref
8808              (setq xref (list (system-name))))
8809            (setq new-xref
8810                  (concat
8811                   (mapconcat 'identity
8812                              (delete "Xref:" (delete new-xref xref))
8813                              " ")
8814                   " " new-xref))
8815            (save-excursion
8816              (set-buffer copy-buf)
8817              ;; First put the article in the destination group.
8818              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8819              (when (consp (setq art-group
8820                                 (gnus-request-accept-article
8821                                  to-newsgroup select-method (not articles))))
8822                (setq new-xref (concat new-xref " " (car art-group)
8823                                       ":"
8824                                       (number-to-string (cdr art-group))))
8825                ;; Now we have the new Xrefs header, so we insert
8826                ;; it and replace the new article.
8827                (nnheader-replace-header "Xref" new-xref)
8828                (gnus-request-replace-article
8829                 (cdr art-group) to-newsgroup (current-buffer))
8830                art-group))))))
8831       (cond
8832        ((not art-group)
8833         (gnus-message 1 "Couldn't %s article %s: %s"
8834                       (cadr (assq action names)) article
8835                       (nnheader-get-report (car to-method))))
8836        ((eq art-group 'junk)
8837         (when (eq action 'move)
8838           (gnus-summary-mark-article article gnus-canceled-mark)
8839           (gnus-message 4 "Deleted article %s" article)))
8840        (t
8841         (let* ((pto-group (gnus-group-prefixed-name
8842                            (car art-group) to-method))
8843                (entry
8844                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8845                (info (nth 2 entry))
8846                (to-group (gnus-info-group info))
8847                to-marks)
8848           ;; Update the group that has been moved to.
8849           (when (and info
8850                      (memq action '(move copy)))
8851             (unless (member to-group to-groups)
8852               (push to-group to-groups))
8853
8854             (unless (memq article gnus-newsgroup-unreads)
8855               (push 'read to-marks)
8856               (gnus-info-set-read
8857                info (gnus-add-to-range (gnus-info-read info)
8858                                        (list (cdr art-group)))))
8859
8860             ;; See whether the article is to be put in the cache.
8861             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8862                              default-marks
8863                            no-expire-marks))
8864                   (to-article (cdr art-group)))
8865
8866               ;; Enter the article into the cache in the new group,
8867               ;; if that is required.
8868               (when gnus-use-cache
8869                 (gnus-cache-possibly-enter-article
8870                  to-group to-article
8871                  (let ((header (copy-sequence
8872                                 (gnus-summary-article-header article))))
8873                    (mail-header-set-number header to-article)
8874                    header)
8875                  (memq article gnus-newsgroup-marked)
8876                  (memq article gnus-newsgroup-dormant)
8877                  (memq article gnus-newsgroup-unreads)))
8878
8879               (when gnus-preserve-marks
8880                 ;; Copy any marks over to the new group.
8881                 (when (and (equal to-group gnus-newsgroup-name)
8882                            (not (memq article gnus-newsgroup-unreads)))
8883                   ;; Mark this article as read in this group.
8884                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8885                   (setcdr (gnus-active to-group) to-article)
8886                   (setcdr gnus-newsgroup-active to-article))
8887
8888                 (while marks
8889                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8890                     (when (memq article (symbol-value
8891                                          (intern (format "gnus-newsgroup-%s"
8892                                                          (caar marks)))))
8893                       (push (cdar marks) to-marks)
8894                       ;; If the other group is the same as this group,
8895                       ;; then we have to add the mark to the list.
8896                       (when (equal to-group gnus-newsgroup-name)
8897                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8898                              (cons to-article
8899                                    (symbol-value
8900                                     (intern (format "gnus-newsgroup-%s"
8901                                                     (caar marks)))))))
8902                       ;; Copy the marks to other group.
8903                       (gnus-add-marked-articles
8904                        to-group (cdar marks) (list to-article) info)))
8905                   (setq marks (cdr marks)))
8906
8907                 (gnus-request-set-mark
8908                  to-group (list (list (list to-article) 'add to-marks))))
8909
8910               (gnus-dribble-enter
8911                (concat "(gnus-group-set-info '"
8912                        (gnus-prin1-to-string (gnus-get-info to-group))
8913                        ")"))))
8914
8915           ;; Update the Xref header in this article to point to
8916           ;; the new crossposted article we have just created.
8917           (when (eq action 'crosspost)
8918             (save-excursion
8919               (set-buffer copy-buf)
8920               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8921               (nnheader-replace-header "Xref" new-xref)
8922               (gnus-request-replace-article
8923                article gnus-newsgroup-name (current-buffer)))))
8924
8925         ;;;!!!Why is this necessary?
8926         (set-buffer gnus-summary-buffer)
8927
8928         (gnus-summary-goto-subject article)
8929         (when (eq action 'move)
8930           (gnus-summary-mark-article article gnus-canceled-mark))))
8931       (gnus-summary-remove-process-mark article))
8932     ;; Re-activate all groups that have been moved to.
8933     (save-excursion
8934       (set-buffer gnus-group-buffer)
8935       (let ((gnus-group-marked to-groups))
8936         (gnus-group-get-new-news-this-group nil t)))
8937
8938     (gnus-kill-buffer copy-buf)
8939     (gnus-summary-position-point)
8940     (gnus-set-mode-line 'summary)))
8941
8942 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8943   "Move the current article to a different newsgroup.
8944 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8945 When called interactively, if TO-NEWSGROUP is nil, use the value of
8946 the variable `gnus-move-split-methods' for finding a default target
8947 newsgroup.
8948 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8949 re-spool using this method."
8950   (interactive "P")
8951   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8952
8953 (defun gnus-summary-crosspost-article (&optional n)
8954   "Crosspost the current article to some other group."
8955   (interactive "P")
8956   (gnus-summary-move-article n nil nil 'crosspost))
8957
8958 (defcustom gnus-summary-respool-default-method nil
8959   "Default method type for respooling an article.
8960 If nil, use to the current newsgroup method."
8961   :type 'symbol
8962   :group 'gnus-summary-mail)
8963
8964 (defcustom gnus-summary-display-while-building nil
8965   "If not-nil, show and update the summary buffer as it's being built.
8966 If the value is t, update the buffer after every line is inserted.  If
8967 the value is an integer (N), update the display every N lines."
8968   :group 'gnus-thread
8969   :type '(choice (const :tag "off" nil)
8970                  number
8971                  (const :tag "frequently" t)))
8972
8973 (defun gnus-summary-respool-article (&optional n method)
8974   "Respool the current article.
8975 The article will be squeezed through the mail spooling process again,
8976 which means that it will be put in some mail newsgroup or other
8977 depending on `nnmail-split-methods'.
8978 If N is a positive number, respool the N next articles.
8979 If N is a negative number, respool the N previous articles.
8980 If N is nil and any articles have been marked with the process mark,
8981 respool those articles instead.
8982
8983 Respooling can be done both from mail groups and \"real\" newsgroups.
8984 In the former case, the articles in question will be moved from the
8985 current group into whatever groups they are destined to.  In the
8986 latter case, they will be copied into the relevant groups."
8987   (interactive
8988    (list current-prefix-arg
8989          (let* ((methods (gnus-methods-using 'respool))
8990                 (methname
8991                  (symbol-name (or gnus-summary-respool-default-method
8992                                   (car (gnus-find-method-for-group
8993                                         gnus-newsgroup-name)))))
8994                 (method
8995                  (gnus-completing-read-with-default
8996                   methname "What backend do you want to use when respooling?"
8997                   methods nil t nil 'gnus-mail-method-history))
8998                 ms)
8999            (cond
9000             ((zerop (length (setq ms (gnus-servers-using-backend
9001                                       (intern method)))))
9002              (list (intern method) ""))
9003             ((= 1 (length ms))
9004              (car ms))
9005             (t
9006              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9007                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9008                            ms-alist))))))))
9009   (unless method
9010     (error "No method given for respooling"))
9011   (if (assoc (symbol-name
9012               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9013              (gnus-methods-using 'respool))
9014       (gnus-summary-move-article n nil method)
9015     (gnus-summary-copy-article n nil method)))
9016
9017 (defun gnus-summary-import-article (file &optional edit)
9018   "Import an arbitrary file into a mail newsgroup."
9019   (interactive "fImport file: \nP")
9020   (let ((group gnus-newsgroup-name)
9021         (now (current-time))
9022         atts lines group-art)
9023     (unless (gnus-check-backend-function 'request-accept-article group)
9024       (error "%s does not support article importing" group))
9025     (or (file-readable-p file)
9026         (not (file-regular-p file))
9027         (error "Can't read %s" file))
9028     (save-excursion
9029       (set-buffer (gnus-get-buffer-create " *import file*"))
9030       (erase-buffer)
9031       (nnheader-insert-file-contents file)
9032       (goto-char (point-min))
9033       (if (nnheader-article-p)
9034           (save-restriction
9035             (goto-char (point-min))
9036             (search-forward "\n\n" nil t)
9037             (narrow-to-region (point-min) (1- (point)))
9038             (goto-char (point-min))
9039             (unless (re-search-forward "^date:" nil t)
9040               (goto-char (point-max))
9041               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9042         ;; This doesn't look like an article, so we fudge some headers.
9043         (setq atts (file-attributes file)
9044               lines (count-lines (point-min) (point-max)))
9045         (insert "From: " (read-string "From: ") "\n"
9046                 "Subject: " (read-string "Subject: ") "\n"
9047                 "Date: " (message-make-date (nth 5 atts)) "\n"
9048                 "Message-ID: " (message-make-message-id) "\n"
9049                 "Lines: " (int-to-string lines) "\n"
9050                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9051       (setq group-art (gnus-request-accept-article group nil t))
9052       (kill-buffer (current-buffer)))
9053     (setq gnus-newsgroup-active (gnus-activate-group group))
9054     (forward-line 1)
9055     (gnus-summary-goto-article (cdr group-art) nil t)
9056     (when edit
9057       (gnus-summary-edit-article))))
9058
9059 (defun gnus-summary-create-article ()
9060   "Create an article in a mail newsgroup."
9061   (interactive)
9062   (let ((group gnus-newsgroup-name)
9063         (now (current-time))
9064         group-art)
9065     (unless (gnus-check-backend-function 'request-accept-article group)
9066       (error "%s does not support article importing" group))
9067     (save-excursion
9068       (set-buffer (gnus-get-buffer-create " *import file*"))
9069       (erase-buffer)
9070       (goto-char (point-min))
9071       ;; This doesn't look like an article, so we fudge some headers.
9072       (insert "From: " (read-string "From: ") "\n"
9073               "Subject: " (read-string "Subject: ") "\n"
9074               "Date: " (message-make-date now) "\n"
9075               "Message-ID: " (message-make-message-id) "\n")
9076       (setq group-art (gnus-request-accept-article group nil t))
9077       (kill-buffer (current-buffer)))
9078     (setq gnus-newsgroup-active (gnus-activate-group group))
9079     (forward-line 1)
9080     (gnus-summary-goto-article (cdr group-art) nil t)
9081     (gnus-summary-edit-article)))
9082
9083 (defun gnus-summary-article-posted-p ()
9084   "Say whether the current (mail) article is available from news as well.
9085 This will be the case if the article has both been mailed and posted."
9086   (interactive)
9087   (let ((id (mail-header-references (gnus-summary-article-header)))
9088         (gnus-override-method (car (gnus-refer-article-methods))))
9089     (if (gnus-request-head id "")
9090         (gnus-message 2 "The current message was found on %s"
9091                       gnus-override-method)
9092       (gnus-message 2 "The current message couldn't be found on %s"
9093                     gnus-override-method)
9094       nil)))
9095
9096 (defun gnus-summary-expire-articles (&optional now)
9097   "Expire all articles that are marked as expirable in the current group."
9098   (interactive)
9099   (when (gnus-check-backend-function
9100          'request-expire-articles gnus-newsgroup-name)
9101     ;; This backend supports expiry.
9102     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9103            (expirable (if total
9104                           (progn
9105                             ;; We need to update the info for
9106                             ;; this group for `gnus-list-of-read-articles'
9107                             ;; to give us the right answer.
9108                             (gnus-run-hooks 'gnus-exit-group-hook)
9109                             (gnus-summary-update-info)
9110                             (gnus-list-of-read-articles gnus-newsgroup-name))
9111                         (setq gnus-newsgroup-expirable
9112                               (sort gnus-newsgroup-expirable '<))))
9113            (expiry-wait (if now 'immediate
9114                           (gnus-group-find-parameter
9115                            gnus-newsgroup-name 'expiry-wait)))
9116            (nnmail-expiry-target
9117             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9118                 nnmail-expiry-target))
9119            es)
9120       (when expirable
9121         ;; There are expirable articles in this group, so we run them
9122         ;; through the expiry process.
9123         (gnus-message 6 "Expiring articles...")
9124         (unless (gnus-check-group gnus-newsgroup-name)
9125           (error "Can't open server for %s" gnus-newsgroup-name))
9126         ;; The list of articles that weren't expired is returned.
9127         (save-excursion
9128           (if expiry-wait
9129               (let ((nnmail-expiry-wait-function nil)
9130                     (nnmail-expiry-wait expiry-wait))
9131                 (setq es (gnus-request-expire-articles
9132                           expirable gnus-newsgroup-name)))
9133             (setq es (gnus-request-expire-articles
9134                       expirable gnus-newsgroup-name)))
9135           (unless total
9136             (setq gnus-newsgroup-expirable es))
9137           ;; We go through the old list of expirable, and mark all
9138           ;; really expired articles as nonexistent.
9139           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9140             (let ((gnus-use-cache nil))
9141               (dolist (article expirable)
9142                 (when (and (not (memq article es))
9143                            (gnus-data-find article))
9144                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9145         (gnus-message 6 "Expiring articles...done")))))
9146
9147 (defun gnus-summary-expire-articles-now ()
9148   "Expunge all expirable articles in the current group.
9149 This means that *all* articles that are marked as expirable will be
9150 deleted forever, right now."
9151   (interactive)
9152   (or gnus-expert-user
9153       (gnus-yes-or-no-p
9154        "Are you really, really, really sure you want to delete all these messages? ")
9155       (error "Phew!"))
9156   (gnus-summary-expire-articles t))
9157
9158 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9159 (defun gnus-summary-delete-article (&optional n)
9160   "Delete the N next (mail) articles.
9161 This command actually deletes articles.  This is not a marking
9162 command.  The article will disappear forever from your life, never to
9163 return.
9164 If N is negative, delete backwards.
9165 If N is nil and articles have been marked with the process mark,
9166 delete these instead."
9167   (interactive "P")
9168   (unless (gnus-check-backend-function 'request-expire-articles
9169                                        gnus-newsgroup-name)
9170     (error "The current newsgroup does not support article deletion"))
9171   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9172     (error "Couldn't open server"))
9173   ;; Compute the list of articles to delete.
9174   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9175         (nnmail-expiry-target 'delete)
9176         not-deleted)
9177     (if (and gnus-novice-user
9178              (not (gnus-yes-or-no-p
9179                    (format "Do you really want to delete %s forever? "
9180                            (if (> (length articles) 1)
9181                                (format "these %s articles" (length articles))
9182                              "this article")))))
9183         ()
9184       ;; Delete the articles.
9185       (setq not-deleted (gnus-request-expire-articles
9186                          articles gnus-newsgroup-name 'force))
9187       (while articles
9188         (gnus-summary-remove-process-mark (car articles))
9189         ;; The backend might not have been able to delete the article
9190         ;; after all.
9191         (unless (memq (car articles) not-deleted)
9192           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9193         (setq articles (cdr articles)))
9194       (when not-deleted
9195         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9196     (gnus-summary-position-point)
9197     (gnus-set-mode-line 'summary)
9198     not-deleted))
9199
9200 (defun gnus-summary-edit-article (&optional force)
9201   "Edit the current article.
9202 This will have permanent effect only in mail groups.
9203 If FORCE is non-nil, allow editing of articles even in read-only
9204 groups."
9205   (interactive "P")
9206   (save-excursion
9207     (set-buffer gnus-summary-buffer)
9208     (let ((mail-parse-charset gnus-newsgroup-charset)
9209           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9210       (gnus-set-global-variables)
9211       (when (and (not force)
9212                  (gnus-group-read-only-p))
9213         (error "The current newsgroup does not support article editing"))
9214       (gnus-summary-show-article t)
9215       (gnus-article-edit-article
9216        'ignore
9217        `(lambda (no-highlight)
9218           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9219                 (message-options message-options)
9220                 (message-options-set-recipient)
9221                 (mail-parse-ignored-charsets
9222                  ',gnus-newsgroup-ignored-charsets))
9223             (gnus-summary-edit-article-done
9224              ,(or (mail-header-references gnus-current-headers) "")
9225              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9226
9227 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9228
9229 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9230                                                  no-highlight)
9231   "Make edits to the current article permanent."
9232   (interactive)
9233   (save-excursion
9234     ;; The buffer restriction contains the entire article if it exists.
9235     (when (article-goto-body)
9236       (let ((lines (count-lines (point) (point-max)))
9237             (length (- (point-max) (point)))
9238             (case-fold-search t)
9239             (body (copy-marker (point))))
9240         (goto-char (point-min))
9241         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9242           (delete-region (match-beginning 1) (match-end 1))
9243           (insert (number-to-string length)))
9244         (goto-char (point-min))
9245         (when (re-search-forward
9246                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9247           (delete-region (match-beginning 1) (match-end 1))
9248           (insert (number-to-string length)))
9249         (goto-char (point-min))
9250         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9251           (delete-region (match-beginning 1) (match-end 1))
9252           (insert (number-to-string lines))))))
9253   ;; Replace the article.
9254   (let ((buf (current-buffer)))
9255     (with-temp-buffer
9256       (insert-buffer-substring buf)
9257
9258       (if (and (not read-only)
9259                (not (gnus-request-replace-article
9260                      (cdr gnus-article-current) (car gnus-article-current)
9261                      (current-buffer) t)))
9262           (error "Couldn't replace article")
9263         ;; Update the summary buffer.
9264         (if (and references
9265                  (equal (message-tokenize-header references " ")
9266                         (message-tokenize-header
9267                          (or (message-fetch-field "references") "") " ")))
9268             ;; We only have to update this line.
9269             (save-excursion
9270               (save-restriction
9271                 (message-narrow-to-head)
9272                 (let ((head (buffer-string))
9273                       header)
9274                   (with-temp-buffer
9275                     (insert (format "211 %d Article retrieved.\n"
9276                                     (cdr gnus-article-current)))
9277                     (insert head)
9278                     (insert ".\n")
9279                     (let ((nntp-server-buffer (current-buffer)))
9280                       (setq header (car (gnus-get-newsgroup-headers
9281                                          nil t))))
9282                     (save-excursion
9283                       (set-buffer gnus-summary-buffer)
9284                       (gnus-data-set-header
9285                        (gnus-data-find (cdr gnus-article-current))
9286                        header)
9287                       (gnus-summary-update-article-line
9288                        (cdr gnus-article-current) header)
9289                       (if (gnus-summary-goto-subject
9290                            (cdr gnus-article-current) nil t)
9291                           (gnus-summary-update-secondary-mark
9292                            (cdr gnus-article-current))))))))
9293           ;; Update threads.
9294           (set-buffer (or buffer gnus-summary-buffer))
9295           (gnus-summary-update-article (cdr gnus-article-current))
9296           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9297               (gnus-summary-update-secondary-mark
9298                (cdr gnus-article-current))))
9299         ;; Prettify the article buffer again.
9300         (unless no-highlight
9301           (save-excursion
9302             (set-buffer gnus-article-buffer)
9303             ;;;!!! Fix this -- article should be rehighlighted.
9304             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9305             (set-buffer gnus-original-article-buffer)
9306             (gnus-request-article
9307              (cdr gnus-article-current)
9308              (car gnus-article-current) (current-buffer))))
9309         ;; Prettify the summary buffer line.
9310         (when (gnus-visual-p 'summary-highlight 'highlight)
9311           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9312
9313 (defun gnus-summary-edit-wash (key)
9314   "Perform editing command KEY in the article buffer."
9315   (interactive
9316    (list
9317     (progn
9318       (message "%s" (concat (this-command-keys) "- "))
9319       (read-char))))
9320   (message "")
9321   (gnus-summary-edit-article)
9322   (execute-kbd-macro (concat (this-command-keys) key))
9323   (gnus-article-edit-done))
9324
9325 ;;; Respooling
9326
9327 (defun gnus-summary-respool-query (&optional silent trace)
9328   "Query where the respool algorithm would put this article."
9329   (interactive)
9330   (let (gnus-mark-article-hook)
9331     (gnus-summary-select-article)
9332     (save-excursion
9333       (set-buffer gnus-original-article-buffer)
9334       (save-restriction
9335         (message-narrow-to-head)
9336         (let ((groups (nnmail-article-group 'identity trace)))
9337           (unless silent
9338             (if groups
9339                 (message "This message would go to %s"
9340                          (mapconcat 'car groups ", "))
9341               (message "This message would go to no groups"))
9342             groups))))))
9343
9344 (defun gnus-summary-respool-trace ()
9345   "Trace where the respool algorithm would put this article.
9346 Display a buffer showing all fancy splitting patterns which matched."
9347   (interactive)
9348   (gnus-summary-respool-query nil t))
9349
9350 ;; Summary marking commands.
9351
9352 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9353   "Mark articles which has the same subject as read, and then select the next.
9354 If UNMARK is positive, remove any kind of mark.
9355 If UNMARK is negative, tick articles."
9356   (interactive "P")
9357   (when unmark
9358     (setq unmark (prefix-numeric-value unmark)))
9359   (let ((count
9360          (gnus-summary-mark-same-subject
9361           (gnus-summary-article-subject) unmark)))
9362     ;; Select next unread article.  If auto-select-same mode, should
9363     ;; select the first unread article.
9364     (gnus-summary-next-article t (and gnus-auto-select-same
9365                                       (gnus-summary-article-subject)))
9366     (gnus-message 7 "%d article%s marked as %s"
9367                   count (if (= count 1) " is" "s are")
9368                   (if unmark "unread" "read"))))
9369
9370 (defun gnus-summary-kill-same-subject (&optional unmark)
9371   "Mark articles which has the same subject as read.
9372 If UNMARK is positive, remove any kind of mark.
9373 If UNMARK is negative, tick articles."
9374   (interactive "P")
9375   (when unmark
9376     (setq unmark (prefix-numeric-value unmark)))
9377   (let ((count
9378          (gnus-summary-mark-same-subject
9379           (gnus-summary-article-subject) unmark)))
9380     ;; If marked as read, go to next unread subject.
9381     (when (null unmark)
9382       ;; Go to next unread subject.
9383       (gnus-summary-next-subject 1 t))
9384     (gnus-message 7 "%d articles are marked as %s"
9385                   count (if unmark "unread" "read"))))
9386
9387 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9388   "Mark articles with same SUBJECT as read, and return marked number.
9389 If optional argument UNMARK is positive, remove any kinds of marks.
9390 If optional argument UNMARK is negative, mark articles as unread instead."
9391   (let ((count 1))
9392     (save-excursion
9393       (cond
9394        ((null unmark)                   ; Mark as read.
9395         (while (and
9396                 (progn
9397                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9398                   (gnus-summary-show-thread) t)
9399                 (gnus-summary-find-subject subject))
9400           (setq count (1+ count))))
9401        ((> unmark 0)                    ; Tick.
9402         (while (and
9403                 (progn
9404                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9405                   (gnus-summary-show-thread) t)
9406                 (gnus-summary-find-subject subject))
9407           (setq count (1+ count))))
9408        (t                               ; Mark as unread.
9409         (while (and
9410                 (progn
9411                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9412                   (gnus-summary-show-thread) t)
9413                 (gnus-summary-find-subject subject))
9414           (setq count (1+ count)))))
9415       (gnus-set-mode-line 'summary)
9416       ;; Return the number of marked articles.
9417       count)))
9418
9419 (defun gnus-summary-mark-as-processable (n &optional unmark)
9420   "Set the process mark on the next N articles.
9421 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9422 the process mark instead.  The difference between N and the actual
9423 number of articles marked is returned."
9424   (interactive "P")
9425   (if (and (null n) (gnus-region-active-p))
9426       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9427     (setq n (prefix-numeric-value n))
9428     (let ((backward (< n 0))
9429           (n (abs n)))
9430       (while (and
9431               (> n 0)
9432               (if unmark
9433                   (gnus-summary-remove-process-mark
9434                    (gnus-summary-article-number))
9435                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9436               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9437         (setq n (1- n)))
9438       (when (/= 0 n)
9439         (gnus-message 7 "No more articles"))
9440       (gnus-summary-recenter)
9441       (gnus-summary-position-point)
9442       n)))
9443
9444 (defun gnus-summary-unmark-as-processable (n)
9445   "Remove the process mark from the next N articles.
9446 If N is negative, unmark backward instead.  The difference between N and
9447 the actual number of articles unmarked is returned."
9448   (interactive "P")
9449   (gnus-summary-mark-as-processable n t))
9450
9451 (defun gnus-summary-unmark-all-processable ()
9452   "Remove the process mark from all articles."
9453   (interactive)
9454   (save-excursion
9455     (while gnus-newsgroup-processable
9456       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9457   (gnus-summary-position-point))
9458
9459 (defun gnus-summary-add-mark (article type)
9460   "Mark ARTICLE with a mark of TYPE."
9461   (let ((vtype (car (assq type gnus-article-mark-lists)))
9462         var)
9463     (if (not vtype)
9464         (error "No such mark type: %s" type)
9465       (setq var (intern (format "gnus-newsgroup-%s" type)))
9466       (set var (cons article (symbol-value var)))
9467       (if (memq type '(processable cached replied forwarded recent saved))
9468           (gnus-summary-update-secondary-mark article)
9469         ;;; !!! This is bogus.  We should find out what primary
9470         ;;; !!! mark we want to set.
9471         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9472
9473 (defun gnus-summary-mark-as-expirable (n)
9474   "Mark N articles forward as expirable.
9475 If N is negative, mark backward instead.  The difference between N and
9476 the actual number of articles marked is returned."
9477   (interactive "p")
9478   (gnus-summary-mark-forward n gnus-expirable-mark))
9479
9480 (defun gnus-summary-mark-as-spam (n)
9481   "Mark N articles forward as spam.
9482 If N is negative, mark backward instead.  The difference between N and
9483 the actual number of articles marked is returned."
9484   (interactive "p")
9485   (gnus-summary-mark-forward n gnus-spam-mark))
9486
9487 (defun gnus-summary-mark-article-as-replied (article)
9488   "Mark ARTICLE as replied to and update the summary line.
9489 ARTICLE can also be a list of articles."
9490   (interactive (list (gnus-summary-article-number)))
9491   (let ((articles (if (listp article) article (list article))))
9492     (dolist (article articles)
9493       (push article gnus-newsgroup-replied)
9494       (let ((buffer-read-only nil))
9495         (when (gnus-summary-goto-subject article nil t)
9496           (gnus-summary-update-secondary-mark article))))))
9497
9498 (defun gnus-summary-mark-article-as-forwarded (article)
9499   "Mark ARTICLE as forwarded and update the summary line.
9500 ARTICLE can also be a list of articles."
9501   (let ((articles (if (listp article) article (list article))))
9502     (dolist (article articles)
9503       (push article gnus-newsgroup-forwarded)
9504       (let ((buffer-read-only nil))
9505         (when (gnus-summary-goto-subject article nil t)
9506           (gnus-summary-update-secondary-mark article))))))
9507
9508 (defun gnus-summary-set-bookmark (article)
9509   "Set a bookmark in current article."
9510   (interactive (list (gnus-summary-article-number)))
9511   (when (or (not (get-buffer gnus-article-buffer))
9512             (not gnus-current-article)
9513             (not gnus-article-current)
9514             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9515     (error "No current article selected"))
9516   ;; Remove old bookmark, if one exists.
9517   (gnus-pull article gnus-newsgroup-bookmarks)
9518   ;; Set the new bookmark, which is on the form
9519   ;; (article-number . line-number-in-body).
9520   (push
9521    (cons article
9522          (save-excursion
9523            (set-buffer gnus-article-buffer)
9524            (count-lines
9525             (min (point)
9526                  (save-excursion
9527                    (article-goto-body)
9528                    (point)))
9529             (point))))
9530    gnus-newsgroup-bookmarks)
9531   (gnus-message 6 "A bookmark has been added to the current article."))
9532
9533 (defun gnus-summary-remove-bookmark (article)
9534   "Remove the bookmark from the current article."
9535   (interactive (list (gnus-summary-article-number)))
9536   ;; Remove old bookmark, if one exists.
9537   (if (not (assq article gnus-newsgroup-bookmarks))
9538       (gnus-message 6 "No bookmark in current article.")
9539     (gnus-pull article gnus-newsgroup-bookmarks)
9540     (gnus-message 6 "Removed bookmark.")))
9541
9542 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9543 (defun gnus-summary-mark-as-dormant (n)
9544   "Mark N articles forward as dormant.
9545 If N is negative, mark backward instead.  The difference between N and
9546 the actual number of articles marked is returned."
9547   (interactive "p")
9548   (gnus-summary-mark-forward n gnus-dormant-mark))
9549
9550 (defun gnus-summary-set-process-mark (article)
9551   "Set the process mark on ARTICLE and update the summary line."
9552   (setq gnus-newsgroup-processable
9553         (cons article
9554               (delq article gnus-newsgroup-processable)))
9555   (when (gnus-summary-goto-subject article)
9556     (gnus-summary-show-thread)
9557     (gnus-summary-goto-subject article)
9558     (gnus-summary-update-secondary-mark article)))
9559
9560 (defun gnus-summary-remove-process-mark (article)
9561   "Remove the process mark from ARTICLE and update the summary line."
9562   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9563   (when (gnus-summary-goto-subject article)
9564     (gnus-summary-show-thread)
9565     (gnus-summary-goto-subject article)
9566     (gnus-summary-update-secondary-mark article)))
9567
9568 (defun gnus-summary-set-saved-mark (article)
9569   "Set the process mark on ARTICLE and update the summary line."
9570   (push article gnus-newsgroup-saved)
9571   (when (gnus-summary-goto-subject article)
9572     (gnus-summary-update-secondary-mark article)))
9573
9574 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9575   "Mark N articles as read forwards.
9576 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9577 The difference between N and the actual number of articles marked is
9578 returned.
9579 If NO-EXPIRE, auto-expiry will be inhibited."
9580   (interactive "p")
9581   (gnus-summary-show-thread)
9582   (let ((backward (< n 0))
9583         (gnus-summary-goto-unread
9584          (and gnus-summary-goto-unread
9585               (not (eq gnus-summary-goto-unread 'never))
9586               (not (memq mark (list gnus-unread-mark
9587                                     gnus-ticked-mark gnus-dormant-mark)))))
9588         (n (abs n))
9589         (mark (or mark gnus-del-mark)))
9590     (while (and (> n 0)
9591                 (gnus-summary-mark-article nil mark no-expire)
9592                 (zerop (gnus-summary-next-subject
9593                         (if backward -1 1)
9594                         (and gnus-summary-goto-unread
9595                              (not (eq gnus-summary-goto-unread 'never)))
9596                         t)))
9597       (setq n (1- n)))
9598     (when (/= 0 n)
9599       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9600     (gnus-summary-recenter)
9601     (gnus-summary-position-point)
9602     (gnus-set-mode-line 'summary)
9603     n))
9604
9605 (defun gnus-summary-mark-article-as-read (mark)
9606   "Mark the current article quickly as read with MARK."
9607   (let ((article (gnus-summary-article-number)))
9608     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9609     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9610     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9611     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9612     (push (cons article mark) gnus-newsgroup-reads)
9613     ;; Possibly remove from cache, if that is used.
9614     (when gnus-use-cache
9615       (gnus-cache-enter-remove-article article))
9616     ;; Allow the backend to change the mark.
9617     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9618     ;; Check for auto-expiry.
9619     (when (and gnus-newsgroup-auto-expire
9620                (memq mark gnus-auto-expirable-marks))
9621       (setq mark gnus-expirable-mark)
9622       ;; Let the backend know about the mark change.
9623       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9624       (push article gnus-newsgroup-expirable))
9625     ;; Set the mark in the buffer.
9626     (gnus-summary-update-mark mark 'unread)
9627     t))
9628
9629 (defun gnus-summary-mark-article-as-unread (mark)
9630   "Mark the current article quickly as unread with MARK."
9631   (let* ((article (gnus-summary-article-number))
9632          (old-mark (gnus-summary-article-mark article)))
9633     ;; Allow the backend to change the mark.
9634     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9635     (if (eq mark old-mark)
9636         t
9637       (if (<= article 0)
9638           (progn
9639             (gnus-error 1 "Can't mark negative article numbers")
9640             nil)
9641         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9642         (setq gnus-newsgroup-spam-marked
9643               (delq article gnus-newsgroup-spam-marked))
9644         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9645         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9646         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9647         (cond ((= mark gnus-ticked-mark)
9648                (setq gnus-newsgroup-marked
9649                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9650                                               article)))
9651               ((= mark gnus-spam-mark)
9652                (setq gnus-newsgroup-spam-marked
9653                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9654                                               article)))
9655               ((= mark gnus-dormant-mark)
9656                (setq gnus-newsgroup-dormant
9657                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9658                                               article)))
9659               (t
9660                (setq gnus-newsgroup-unreads
9661                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9662                                               article))))
9663         (gnus-pull article gnus-newsgroup-reads)
9664
9665         ;; See whether the article is to be put in the cache.
9666         (and gnus-use-cache
9667              (vectorp (gnus-summary-article-header article))
9668              (save-excursion
9669                (gnus-cache-possibly-enter-article
9670                 gnus-newsgroup-name article
9671                 (gnus-summary-article-header article)
9672                 (= mark gnus-ticked-mark)
9673                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9674
9675         ;; Fix the mark.
9676         (gnus-summary-update-mark mark 'unread)
9677         t))))
9678
9679 (defun gnus-summary-mark-article (&optional article mark no-expire)
9680   "Mark ARTICLE with MARK.  MARK can be any character.
9681 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9682 `??' (dormant) and `?E' (expirable).
9683 If MARK is nil, then the default character `?r' is used.
9684 If ARTICLE is nil, then the article on the current line will be
9685 marked.
9686 If NO-EXPIRE, auto-expiry will be inhibited."
9687   ;; The mark might be a string.
9688   (when (stringp mark)
9689     (setq mark (aref mark 0)))
9690   ;; If no mark is given, then we check auto-expiring.
9691   (when (null mark)
9692     (setq mark gnus-del-mark))
9693   (when (and (not no-expire)
9694              gnus-newsgroup-auto-expire
9695              (memq mark gnus-auto-expirable-marks))
9696     (setq mark gnus-expirable-mark))
9697   (let ((article (or article (gnus-summary-article-number)))
9698         (old-mark (gnus-summary-article-mark article)))
9699     ;; Allow the backend to change the mark.
9700     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9701     (if (eq mark old-mark)
9702         t
9703       (unless article
9704         (error "No article on current line"))
9705       (if (not (if (or (= mark gnus-unread-mark)
9706                        (= mark gnus-ticked-mark)
9707                        (= mark gnus-spam-mark)
9708                        (= mark gnus-dormant-mark))
9709                    (gnus-mark-article-as-unread article mark)
9710                  (gnus-mark-article-as-read article mark)))
9711           t
9712         ;; See whether the article is to be put in the cache.
9713         (and gnus-use-cache
9714              (not (= mark gnus-canceled-mark))
9715              (vectorp (gnus-summary-article-header article))
9716              (save-excursion
9717                (gnus-cache-possibly-enter-article
9718                 gnus-newsgroup-name article
9719                 (gnus-summary-article-header article)
9720                 (= mark gnus-ticked-mark)
9721                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9722
9723         (when (gnus-summary-goto-subject article nil t)
9724           (let ((buffer-read-only nil))
9725             (gnus-summary-show-thread)
9726             ;; Fix the mark.
9727             (gnus-summary-update-mark mark 'unread)
9728             t))))))
9729
9730 (defun gnus-summary-update-secondary-mark (article)
9731   "Update the secondary (read, process, cache) mark."
9732   (gnus-summary-update-mark
9733    (cond ((memq article gnus-newsgroup-processable)
9734           gnus-process-mark)
9735          ((memq article gnus-newsgroup-cached)
9736           gnus-cached-mark)
9737          ((memq article gnus-newsgroup-replied)
9738           gnus-replied-mark)
9739          ((memq article gnus-newsgroup-forwarded)
9740           gnus-forwarded-mark)
9741          ((memq article gnus-newsgroup-saved)
9742           gnus-saved-mark)
9743          ((memq article gnus-newsgroup-recent)
9744           gnus-recent-mark)
9745          ((memq article gnus-newsgroup-unseen)
9746           gnus-unseen-mark)
9747          (t gnus-no-mark))
9748    'replied)
9749   (when (gnus-visual-p 'summary-highlight 'highlight)
9750     (gnus-run-hooks 'gnus-summary-update-hook))
9751   t)
9752
9753 (defun gnus-summary-update-download-mark (article)
9754   "Update the secondary (read, process, cache) mark."
9755   (gnus-summary-update-mark
9756    (cond ((memq article gnus-newsgroup-undownloaded) 
9757           gnus-undownloaded-mark)
9758          (gnus-newsgroup-agentized
9759           gnus-downloaded-mark)
9760          (t
9761           gnus-no-mark))
9762    'download)
9763   (gnus-summary-update-line t)
9764   t)
9765
9766 (defun gnus-summary-update-mark (mark type)
9767   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9768         (buffer-read-only nil))
9769     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9770     (when forward
9771       (when (looking-at "\r")
9772         (incf forward))
9773       (when (<= (+ forward (point)) (point-max))
9774         ;; Go to the right position on the line.
9775         (goto-char (+ forward (point)))
9776         ;; Replace the old mark with the new mark.
9777         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9778         ;; Optionally update the marks by some user rule.
9779         (when (eq type 'unread)
9780           (gnus-data-set-mark
9781            (gnus-data-find (gnus-summary-article-number)) mark)
9782           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9783
9784 (defun gnus-mark-article-as-read (article &optional mark)
9785   "Enter ARTICLE in the pertinent lists and remove it from others."
9786   ;; Make the article expirable.
9787   (let ((mark (or mark gnus-del-mark)))
9788     (setq gnus-newsgroup-expirable
9789           (if (= mark gnus-expirable-mark)
9790               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9791             (delq article gnus-newsgroup-expirable)))
9792     ;; Remove from unread and marked lists.
9793     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9794     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9795     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9796     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9797     (push (cons article mark) gnus-newsgroup-reads)
9798     ;; Possibly remove from cache, if that is used.
9799     (when gnus-use-cache
9800       (gnus-cache-enter-remove-article article))
9801     t))
9802
9803 (defun gnus-mark-article-as-unread (article &optional mark)
9804   "Enter ARTICLE in the pertinent lists and remove it from others."
9805   (let ((mark (or mark gnus-ticked-mark)))
9806     (if (<= article 0)
9807         (progn
9808           (gnus-error 1 "Can't mark negative article numbers")
9809           nil)
9810       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9811             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9812             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9813             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9814             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9815
9816       ;; Unsuppress duplicates?
9817       (when gnus-suppress-duplicates
9818         (gnus-dup-unsuppress-article article))
9819
9820       (cond ((= mark gnus-ticked-mark)
9821              (setq gnus-newsgroup-marked
9822                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9823             ((= mark gnus-spam-mark)
9824              (setq gnus-newsgroup-spam-marked
9825                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9826                                             article)))
9827             ((= mark gnus-dormant-mark)
9828              (setq gnus-newsgroup-dormant
9829                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9830             (t
9831              (setq gnus-newsgroup-unreads
9832                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9833       (gnus-pull article gnus-newsgroup-reads)
9834       t)))
9835
9836 (defalias 'gnus-summary-mark-as-unread-forward
9837   'gnus-summary-tick-article-forward)
9838 (make-obsolete 'gnus-summary-mark-as-unread-forward
9839                'gnus-summary-tick-article-forward)
9840 (defun gnus-summary-tick-article-forward (n)
9841   "Tick N articles forwards.
9842 If N is negative, tick backwards instead.
9843 The difference between N and the number of articles ticked is returned."
9844   (interactive "p")
9845   (gnus-summary-mark-forward n gnus-ticked-mark))
9846
9847 (defalias 'gnus-summary-mark-as-unread-backward
9848   'gnus-summary-tick-article-backward)
9849 (make-obsolete 'gnus-summary-mark-as-unread-backward
9850                'gnus-summary-tick-article-backward)
9851 (defun gnus-summary-tick-article-backward (n)
9852   "Tick N articles backwards.
9853 The difference between N and the number of articles ticked is returned."
9854   (interactive "p")
9855   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9856
9857 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9858 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9859 (defun gnus-summary-tick-article (&optional article clear-mark)
9860   "Mark current article as unread.
9861 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9862 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9863   (interactive)
9864   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9865                                        gnus-ticked-mark)))
9866
9867 (defun gnus-summary-mark-as-read-forward (n)
9868   "Mark N articles as read forwards.
9869 If N is negative, mark backwards instead.
9870 The difference between N and the actual number of articles marked is
9871 returned."
9872   (interactive "p")
9873   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9874
9875 (defun gnus-summary-mark-as-read-backward (n)
9876   "Mark the N articles as read backwards.
9877 The difference between N and the actual number of articles marked is
9878 returned."
9879   (interactive "p")
9880   (gnus-summary-mark-forward
9881    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9882
9883 (defun gnus-summary-mark-as-read (&optional article mark)
9884   "Mark current article as read.
9885 ARTICLE specifies the article to be marked as read.
9886 MARK specifies a string to be inserted at the beginning of the line."
9887   (gnus-summary-mark-article article mark))
9888
9889 (defun gnus-summary-clear-mark-forward (n)
9890   "Clear marks from N articles forward.
9891 If N is negative, clear backward instead.
9892 The difference between N and the number of marks cleared is returned."
9893   (interactive "p")
9894   (gnus-summary-mark-forward n gnus-unread-mark))
9895
9896 (defun gnus-summary-clear-mark-backward (n)
9897   "Clear marks from N articles backward.
9898 The difference between N and the number of marks cleared is returned."
9899   (interactive "p")
9900   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9901
9902 (defun gnus-summary-mark-unread-as-read ()
9903   "Intended to be used by `gnus-summary-mark-article-hook'."
9904   (when (memq gnus-current-article gnus-newsgroup-unreads)
9905     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9906
9907 (defun gnus-summary-mark-read-and-unread-as-read ()
9908   "Intended to be used by `gnus-summary-mark-article-hook'."
9909   (let ((mark (gnus-summary-article-mark)))
9910     (when (or (gnus-unread-mark-p mark)
9911               (gnus-read-mark-p mark))
9912       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9913
9914 (defun gnus-summary-mark-unread-as-ticked ()
9915   "Intended to be used by `gnus-summary-mark-article-hook'."
9916   (when (memq gnus-current-article gnus-newsgroup-unreads)
9917     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9918
9919 (defun gnus-summary-mark-region-as-read (point mark all)
9920   "Mark all unread articles between point and mark as read.
9921 If given a prefix, mark all articles between point and mark as read,
9922 even ticked and dormant ones."
9923   (interactive "r\nP")
9924   (save-excursion
9925     (let (article)
9926       (goto-char point)
9927       (beginning-of-line)
9928       (while (and
9929               (< (point) mark)
9930               (progn
9931                 (when (or all
9932                           (memq (setq article (gnus-summary-article-number))
9933                                 gnus-newsgroup-unreads))
9934                   (gnus-summary-mark-article article gnus-del-mark))
9935                 t)
9936               (gnus-summary-find-next))))))
9937
9938 (defun gnus-summary-mark-below (score mark)
9939   "Mark articles with score less than SCORE with MARK."
9940   (interactive "P\ncMark: ")
9941   (setq score (if score
9942                   (prefix-numeric-value score)
9943                 (or gnus-summary-default-score 0)))
9944   (save-excursion
9945     (set-buffer gnus-summary-buffer)
9946     (goto-char (point-min))
9947     (while
9948         (progn
9949           (and (< (gnus-summary-article-score) score)
9950                (gnus-summary-mark-article nil mark))
9951           (gnus-summary-find-next)))))
9952
9953 (defun gnus-summary-kill-below (&optional score)
9954   "Mark articles with score below SCORE as read."
9955   (interactive "P")
9956   (gnus-summary-mark-below score gnus-killed-mark))
9957
9958 (defun gnus-summary-clear-above (&optional score)
9959   "Clear all marks from articles with score above SCORE."
9960   (interactive "P")
9961   (gnus-summary-mark-above score gnus-unread-mark))
9962
9963 (defun gnus-summary-tick-above (&optional score)
9964   "Tick all articles with score above SCORE."
9965   (interactive "P")
9966   (gnus-summary-mark-above score gnus-ticked-mark))
9967
9968 (defun gnus-summary-mark-above (score mark)
9969   "Mark articles with score over SCORE with MARK."
9970   (interactive "P\ncMark: ")
9971   (setq score (if score
9972                   (prefix-numeric-value score)
9973                 (or gnus-summary-default-score 0)))
9974   (save-excursion
9975     (set-buffer gnus-summary-buffer)
9976     (goto-char (point-min))
9977     (while (and (progn
9978                   (when (> (gnus-summary-article-score) score)
9979                     (gnus-summary-mark-article nil mark))
9980                   t)
9981                 (gnus-summary-find-next)))))
9982
9983 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9984 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9985 (defun gnus-summary-limit-include-expunged (&optional no-error)
9986   "Display all the hidden articles that were expunged for low scores."
9987   (interactive)
9988   (let ((buffer-read-only nil))
9989     (let ((scored gnus-newsgroup-scored)
9990           headers h)
9991       (while scored
9992         (unless (gnus-summary-article-header (caar scored))
9993           (and (setq h (gnus-number-to-header (caar scored)))
9994                (< (cdar scored) gnus-summary-expunge-below)
9995                (push h headers)))
9996         (setq scored (cdr scored)))
9997       (if (not headers)
9998           (when (not no-error)
9999             (error "No expunged articles hidden"))
10000         (goto-char (point-min))
10001         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10002         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10003         (mapcar (lambda (x) (push (mail-header-number x)
10004                                   gnus-newsgroup-limit))
10005                 headers)
10006         (gnus-summary-prepare-unthreaded (nreverse headers))
10007         (goto-char (point-min))
10008         (gnus-summary-position-point)
10009         t))))
10010
10011 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10012   "Mark all unread articles in this newsgroup as read.
10013 If prefix argument ALL is non-nil, ticked and dormant articles will
10014 also be marked as read.
10015 If QUIETLY is non-nil, no questions will be asked.
10016 If TO-HERE is non-nil, it should be a point in the buffer.  All
10017 articles before (after, if REVERSE is set) this point will be marked as read.
10018 Note that this function will only catch up the unread article
10019 in the current summary buffer limitation.
10020 The number of articles marked as read is returned."
10021   (interactive "P")
10022   (prog1
10023       (save-excursion
10024         (when (or quietly
10025                   (not gnus-interactive-catchup) ;Without confirmation?
10026                   gnus-expert-user
10027                   (gnus-y-or-n-p
10028                    (if all
10029                        "Mark absolutely all articles as read? "
10030                      "Mark all unread articles as read? ")))
10031           (if (and not-mark
10032                    (not gnus-newsgroup-adaptive)
10033                    (not gnus-newsgroup-auto-expire)
10034                    (not gnus-suppress-duplicates)
10035                    (or (not gnus-use-cache)
10036                        (eq gnus-use-cache 'passive)))
10037               (progn
10038                 (when all
10039                   (setq gnus-newsgroup-marked nil
10040                         gnus-newsgroup-spam-marked nil
10041                         gnus-newsgroup-dormant nil))
10042                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
10043             ;; We actually mark all articles as canceled, which we
10044             ;; have to do when using auto-expiry or adaptive scoring.
10045             (gnus-summary-show-all-threads)
10046             (if (and to-here reverse)
10047                 (progn
10048                   (goto-char to-here)
10049                   (while (and
10050                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10051                           (gnus-summary-find-next (not all) nil nil t))))
10052               (when (gnus-summary-first-subject (not all) t)
10053                 (while (and
10054                         (if to-here (< (point) to-here) t)
10055                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10056                         (gnus-summary-find-next (not all) nil nil t)))))
10057             (gnus-set-mode-line 'summary))
10058           t))
10059     (gnus-summary-position-point)))
10060
10061 (defun gnus-summary-catchup-to-here (&optional all)
10062   "Mark all unticked articles before the current one as read.
10063 If ALL is non-nil, also mark ticked and dormant articles as read."
10064   (interactive "P")
10065   (save-excursion
10066     (gnus-save-hidden-threads
10067       (let ((beg (point)))
10068         ;; We check that there are unread articles.
10069         (when (or all (gnus-summary-find-prev))
10070           (gnus-summary-catchup all t beg)))))
10071   (gnus-summary-position-point))
10072
10073 (defun gnus-summary-catchup-from-here (&optional all)
10074   "Mark all unticked articles after the current one as read.
10075 If ALL is non-nil, also mark ticked and dormant articles as read."
10076   (interactive "P")
10077   (save-excursion
10078     (gnus-save-hidden-threads
10079       (let ((beg (point)))
10080         ;; We check that there are unread articles.
10081         (when (or all (gnus-summary-find-next))
10082           (gnus-summary-catchup all t beg nil t)))))
10083   (gnus-summary-position-point))
10084
10085 (defun gnus-summary-catchup-all (&optional quietly)
10086   "Mark all articles in this newsgroup as read.
10087 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10088 instead, which marks only unread articles as read."
10089   (interactive "P")
10090   (gnus-summary-catchup t quietly))
10091
10092 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10093   "Mark all unread articles in this group as read, then exit.
10094 If prefix argument ALL is non-nil, all articles are marked as read.
10095 If QUIETLY is non-nil, no questions will be asked."
10096   (interactive "P")
10097   (when (gnus-summary-catchup all quietly nil 'fast)
10098     ;; Select next newsgroup or exit.
10099     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10100              (eq gnus-auto-select-next 'quietly))
10101         (gnus-summary-next-group nil)
10102       (gnus-summary-exit))))
10103
10104 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10105   "Mark all articles in this newsgroup as read, and then exit.
10106 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10107 instead, which marks only unread articles as read."
10108   (interactive "P")
10109   (gnus-summary-catchup-and-exit t quietly))
10110
10111 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10112   "Mark all articles in this group as read and select the next group.
10113 If given a prefix, mark all articles, unread as well as ticked, as
10114 read."
10115   (interactive "P")
10116   (save-excursion
10117     (gnus-summary-catchup all))
10118   (gnus-summary-next-group))
10119
10120 ;;;
10121 ;;; with article
10122 ;;;
10123
10124 (defmacro gnus-with-article (article &rest forms)
10125   "Select ARTICLE and perform FORMS in the original article buffer.
10126 Then replace the article with the result."
10127   `(progn
10128      ;; We don't want the article to be marked as read.
10129      (let (gnus-mark-article-hook)
10130        (gnus-summary-select-article t t nil ,article))
10131      (set-buffer gnus-original-article-buffer)
10132      ,@forms
10133      (if (not (gnus-check-backend-function
10134                'request-replace-article (car gnus-article-current)))
10135          (gnus-message 5 "Read-only group; not replacing")
10136        (unless (gnus-request-replace-article
10137                 ,article (car gnus-article-current)
10138                 (current-buffer) t)
10139          (error "Couldn't replace article")))
10140      ;; The cache and backlog have to be flushed somewhat.
10141      (when gnus-keep-backlog
10142        (gnus-backlog-remove-article
10143         (car gnus-article-current) (cdr gnus-article-current)))
10144      (when gnus-use-cache
10145        (gnus-cache-update-article
10146         (car gnus-article-current) (cdr gnus-article-current)))))
10147
10148 (put 'gnus-with-article 'lisp-indent-function 1)
10149 (put 'gnus-with-article 'edebug-form-spec '(form body))
10150
10151 ;; Thread-based commands.
10152
10153 (defun gnus-summary-articles-in-thread (&optional article)
10154   "Return a list of all articles in the current thread.
10155 If ARTICLE is non-nil, return all articles in the thread that starts
10156 with that article."
10157   (let* ((article (or article (gnus-summary-article-number)))
10158          (data (gnus-data-find-list article))
10159          (top-level (gnus-data-level (car data)))
10160          (top-subject
10161           (cond ((null gnus-thread-operation-ignore-subject)
10162                  (gnus-simplify-subject-re
10163                   (mail-header-subject (gnus-data-header (car data)))))
10164                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10165                  (gnus-simplify-subject-fuzzy
10166                   (mail-header-subject (gnus-data-header (car data)))))
10167                 (t nil)))
10168          (end-point (save-excursion
10169                       (if (gnus-summary-go-to-next-thread)
10170                           (point) (point-max))))
10171          articles)
10172     (while (and data
10173                 (< (gnus-data-pos (car data)) end-point))
10174       (when (or (not top-subject)
10175                 (string= top-subject
10176                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10177                              (gnus-simplify-subject-fuzzy
10178                               (mail-header-subject
10179                                (gnus-data-header (car data))))
10180                            (gnus-simplify-subject-re
10181                             (mail-header-subject
10182                              (gnus-data-header (car data)))))))
10183         (push (gnus-data-number (car data)) articles))
10184       (unless (and (setq data (cdr data))
10185                    (> (gnus-data-level (car data)) top-level))
10186         (setq data nil)))
10187     ;; Return the list of articles.
10188     (nreverse articles)))
10189
10190 (defun gnus-summary-rethread-current ()
10191   "Rethread the thread the current article is part of."
10192   (interactive)
10193   (let* ((gnus-show-threads t)
10194          (article (gnus-summary-article-number))
10195          (id (mail-header-id (gnus-summary-article-header)))
10196          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10197     (unless id
10198       (error "No article on the current line"))
10199     (gnus-rebuild-thread id)
10200     (gnus-summary-goto-subject article)))
10201
10202 (defun gnus-summary-reparent-thread ()
10203   "Make the current article child of the marked (or previous) article.
10204
10205 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10206 is non-nil or the Subject: of both articles are the same."
10207   (interactive)
10208   (unless (not (gnus-group-read-only-p))
10209     (error "The current newsgroup does not support article editing"))
10210   (unless (<= (length gnus-newsgroup-processable) 1)
10211     (error "No more than one article may be marked"))
10212   (save-window-excursion
10213     (let ((gnus-article-buffer " *reparent*")
10214           (current-article (gnus-summary-article-number))
10215           ;; First grab the marked article, otherwise one line up.
10216           (parent-article (if (not (null gnus-newsgroup-processable))
10217                               (car gnus-newsgroup-processable)
10218                             (save-excursion
10219                               (if (eq (forward-line -1) 0)
10220                                   (gnus-summary-article-number)
10221                                 (error "Beginning of summary buffer"))))))
10222       (unless (not (eq current-article parent-article))
10223         (error "An article may not be self-referential"))
10224       (let ((message-id (mail-header-id
10225                          (gnus-summary-article-header parent-article))))
10226         (unless (and message-id (not (equal message-id "")))
10227           (error "No message-id in desired parent"))
10228         (gnus-with-article current-article
10229           (save-restriction
10230             (goto-char (point-min))
10231             (message-narrow-to-head)
10232             (if (re-search-forward "^References: " nil t)
10233                 (progn
10234                   (re-search-forward "^[^ \t]" nil t)
10235                   (forward-line -1)
10236                   (end-of-line)
10237                   (insert " " message-id))
10238               (insert "References: " message-id "\n"))))
10239         (set-buffer gnus-summary-buffer)
10240         (gnus-summary-unmark-all-processable)
10241         (gnus-summary-update-article current-article)
10242         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10243             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10244         (gnus-summary-rethread-current)
10245         (gnus-message 3 "Article %d is now the child of article %d"
10246                       current-article parent-article)))))
10247
10248 (defun gnus-summary-toggle-threads (&optional arg)
10249   "Toggle showing conversation threads.
10250 If ARG is positive number, turn showing conversation threads on."
10251   (interactive "P")
10252   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10253     (setq gnus-show-threads
10254           (if (null arg) (not gnus-show-threads)
10255             (> (prefix-numeric-value arg) 0)))
10256     (gnus-summary-prepare)
10257     (gnus-summary-goto-subject current)
10258     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10259     (gnus-summary-position-point)))
10260
10261 (defun gnus-summary-show-all-threads ()
10262   "Show all threads."
10263   (interactive)
10264   (save-excursion
10265     (let ((buffer-read-only nil))
10266       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10267   (gnus-summary-position-point))
10268
10269 (defun gnus-summary-show-thread ()
10270   "Show thread subtrees.
10271 Returns nil if no thread was there to be shown."
10272   (interactive)
10273   (let ((buffer-read-only nil)
10274         (orig (point))
10275         ;; first goto end then to beg, to have point at beg after let
10276         (end (progn (end-of-line) (point)))
10277         (beg (progn (beginning-of-line) (point))))
10278     (prog1
10279         ;; Any hidden lines here?
10280         (search-forward "\r" end t)
10281       (subst-char-in-region beg end ?\^M ?\n t)
10282       (goto-char orig)
10283       (gnus-summary-position-point))))
10284
10285 (defun gnus-summary-maybe-hide-threads ()
10286   "If requested, hide the threads that should be hidden."
10287   (when (and gnus-show-threads
10288              gnus-thread-hide-subtree)
10289     (gnus-summary-hide-all-threads
10290      (if (or (consp gnus-thread-hide-subtree)
10291              (gnus-functionp gnus-thread-hide-subtree))
10292          (gnus-make-predicate gnus-thread-hide-subtree)
10293        nil))))
10294
10295 ;;; Hiding predicates.
10296
10297 (defun gnus-article-unread-p (header)
10298   (memq (mail-header-number header) gnus-newsgroup-unreads))
10299
10300 (defun gnus-article-unseen-p (header)
10301   (memq (mail-header-number header) gnus-newsgroup-unseen))
10302
10303 (defun gnus-map-articles (predicate articles)
10304   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10305   (apply 'gnus-or (mapcar predicate
10306                           (mapcar 'gnus-summary-article-header articles))))
10307
10308 (defun gnus-summary-hide-all-threads (&optional predicate)
10309   "Hide all thread subtrees.
10310 If PREDICATE is supplied, threads that satisfy this predicate
10311 will not be hidden."
10312   (interactive)
10313   (save-excursion
10314     (goto-char (point-min))
10315     (let ((end nil))
10316       (while (not end)
10317         (when (or (not predicate)
10318                   (gnus-map-articles
10319                    predicate (gnus-summary-article-children)))
10320             (gnus-summary-hide-thread))
10321         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10322   (gnus-summary-position-point))
10323
10324 (defun gnus-summary-hide-thread ()
10325   "Hide thread subtrees.
10326 If PREDICATE is supplied, threads that satisfy this predicate
10327 will not be hidden.
10328 Returns nil if no threads were there to be hidden."
10329   (interactive)
10330   (let ((buffer-read-only nil)
10331         (start (point))
10332         (article (gnus-summary-article-number)))
10333     (goto-char start)
10334     ;; Go forward until either the buffer ends or the subthread
10335     ;; ends.
10336     (when (and (not (eobp))
10337                (or (zerop (gnus-summary-next-thread 1 t))
10338                    (goto-char (point-max))))
10339       (prog1
10340           (if (and (> (point) start)
10341                    (search-backward "\n" start t))
10342               (progn
10343                 (subst-char-in-region start (point) ?\n ?\^M)
10344                 (gnus-summary-goto-subject article))
10345             (goto-char start)
10346             nil)))))
10347
10348 (defun gnus-summary-go-to-next-thread (&optional previous)
10349   "Go to the same level (or less) next thread.
10350 If PREVIOUS is non-nil, go to previous thread instead.
10351 Return the article number moved to, or nil if moving was impossible."
10352   (let ((level (gnus-summary-thread-level))
10353         (way (if previous -1 1))
10354         (beg (point)))
10355     (forward-line way)
10356     (while (and (not (eobp))
10357                 (< level (gnus-summary-thread-level)))
10358       (forward-line way))
10359     (if (eobp)
10360         (progn
10361           (goto-char beg)
10362           nil)
10363       (setq beg (point))
10364       (prog1
10365           (gnus-summary-article-number)
10366         (goto-char beg)))))
10367
10368 (defun gnus-summary-next-thread (n &optional silent)
10369   "Go to the same level next N'th thread.
10370 If N is negative, search backward instead.
10371 Returns the difference between N and the number of skips actually
10372 done.
10373
10374 If SILENT, don't output messages."
10375   (interactive "p")
10376   (let ((backward (< n 0))
10377         (n (abs n)))
10378     (while (and (> n 0)
10379                 (gnus-summary-go-to-next-thread backward))
10380       (decf n))
10381     (unless silent
10382       (gnus-summary-position-point))
10383     (when (and (not silent) (/= 0 n))
10384       (gnus-message 7 "No more threads"))
10385     n))
10386
10387 (defun gnus-summary-prev-thread (n)
10388   "Go to the same level previous N'th thread.
10389 Returns the difference between N and the number of skips actually
10390 done."
10391   (interactive "p")
10392   (gnus-summary-next-thread (- n)))
10393
10394 (defun gnus-summary-go-down-thread ()
10395   "Go down one level in the current thread."
10396   (let ((children (gnus-summary-article-children)))
10397     (when children
10398       (gnus-summary-goto-subject (car children)))))
10399
10400 (defun gnus-summary-go-up-thread ()
10401   "Go up one level in the current thread."
10402   (let ((parent (gnus-summary-article-parent)))
10403     (when parent
10404       (gnus-summary-goto-subject parent))))
10405
10406 (defun gnus-summary-down-thread (n)
10407   "Go down thread N steps.
10408 If N is negative, go up instead.
10409 Returns the difference between N and how many steps down that were
10410 taken."
10411   (interactive "p")
10412   (let ((up (< n 0))
10413         (n (abs n)))
10414     (while (and (> n 0)
10415                 (if up (gnus-summary-go-up-thread)
10416                   (gnus-summary-go-down-thread)))
10417       (setq n (1- n)))
10418     (gnus-summary-position-point)
10419     (when (/= 0 n)
10420       (gnus-message 7 "Can't go further"))
10421     n))
10422
10423 (defun gnus-summary-up-thread (n)
10424   "Go up thread N steps.
10425 If N is negative, go down instead.
10426 Returns the difference between N and how many steps down that were
10427 taken."
10428   (interactive "p")
10429   (gnus-summary-down-thread (- n)))
10430
10431 (defun gnus-summary-top-thread ()
10432   "Go to the top of the thread."
10433   (interactive)
10434   (while (gnus-summary-go-up-thread))
10435   (gnus-summary-article-number))
10436
10437 (defun gnus-summary-kill-thread (&optional unmark)
10438   "Mark articles under current thread as read.
10439 If the prefix argument is positive, remove any kinds of marks.
10440 If the prefix argument is negative, tick articles instead."
10441   (interactive "P")
10442   (when unmark
10443     (setq unmark (prefix-numeric-value unmark)))
10444   (let ((articles (gnus-summary-articles-in-thread)))
10445     (save-excursion
10446       ;; Expand the thread.
10447       (gnus-summary-show-thread)
10448       ;; Mark all the articles.
10449       (while articles
10450         (gnus-summary-goto-subject (car articles))
10451         (cond ((null unmark)
10452                (gnus-summary-mark-article-as-read gnus-killed-mark))
10453               ((> unmark 0)
10454                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10455               (t
10456                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10457         (setq articles (cdr articles))))
10458     ;; Hide killed subtrees.
10459     (and (null unmark)
10460          gnus-thread-hide-killed
10461          (gnus-summary-hide-thread))
10462     ;; If marked as read, go to next unread subject.
10463     (when (null unmark)
10464       ;; Go to next unread subject.
10465       (gnus-summary-next-subject 1 t)))
10466   (gnus-set-mode-line 'summary))
10467
10468 ;; Summary sorting commands
10469
10470 (defun gnus-summary-sort-by-number (&optional reverse)
10471   "Sort the summary buffer by article number.
10472 Argument REVERSE means reverse order."
10473   (interactive "P")
10474   (gnus-summary-sort 'number reverse))
10475
10476 (defun gnus-summary-sort-by-random (&optional reverse)
10477   "Randomize the order in the summary buffer.
10478 Argument REVERSE means to randomize in reverse order."
10479   (interactive "P")
10480   (gnus-summary-sort 'random reverse))
10481
10482 (defun gnus-summary-sort-by-author (&optional reverse)
10483   "Sort the summary buffer by author name alphabetically.
10484 If `case-fold-search' is non-nil, case of letters is ignored.
10485 Argument REVERSE means reverse order."
10486   (interactive "P")
10487   (gnus-summary-sort 'author reverse))
10488
10489 (defun gnus-summary-sort-by-subject (&optional reverse)
10490   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10491 If `case-fold-search' is non-nil, case of letters is ignored.
10492 Argument REVERSE means reverse order."
10493   (interactive "P")
10494   (gnus-summary-sort 'subject reverse))
10495
10496 (defun gnus-summary-sort-by-date (&optional reverse)
10497   "Sort the summary buffer by date.
10498 Argument REVERSE means reverse order."
10499   (interactive "P")
10500   (gnus-summary-sort 'date reverse))
10501
10502 (defun gnus-summary-sort-by-score (&optional reverse)
10503   "Sort the summary buffer by score.
10504 Argument REVERSE means reverse order."
10505   (interactive "P")
10506   (gnus-summary-sort 'score reverse))
10507
10508 (defun gnus-summary-sort-by-lines (&optional reverse)
10509   "Sort the summary buffer by the number of lines.
10510 Argument REVERSE means reverse order."
10511   (interactive "P")
10512   (gnus-summary-sort 'lines reverse))
10513
10514 (defun gnus-summary-sort-by-chars (&optional reverse)
10515   "Sort the summary buffer by article length.
10516 Argument REVERSE means reverse order."
10517   (interactive "P")
10518   (gnus-summary-sort 'chars reverse))
10519
10520 (defun gnus-summary-sort-by-original (&optional reverse)
10521   "Sort the summary buffer using the default sorting method.
10522 Argument REVERSE means reverse order."
10523   (interactive "P")
10524   (let* ((buffer-read-only)
10525          (gnus-summary-prepare-hook nil))
10526     ;; We do the sorting by regenerating the threads.
10527     (gnus-summary-prepare)
10528     ;; Hide subthreads if needed.
10529     (gnus-summary-maybe-hide-threads)))
10530
10531 (defun gnus-summary-sort (predicate reverse)
10532   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10533   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10534          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10535          (gnus-thread-sort-functions
10536           (if (not reverse)
10537               thread
10538             `(lambda (t1 t2)
10539                (,thread t2 t1))))
10540          (gnus-sort-gathered-threads-function
10541           gnus-thread-sort-functions)
10542          (gnus-article-sort-functions
10543           (if (not reverse)
10544               article
10545             `(lambda (t1 t2)
10546                (,article t2 t1))))
10547          (buffer-read-only)
10548          (gnus-summary-prepare-hook nil))
10549     ;; We do the sorting by regenerating the threads.
10550     (gnus-summary-prepare)
10551     ;; Hide subthreads if needed.
10552     (gnus-summary-maybe-hide-threads)))
10553
10554 ;; Summary saving commands.
10555
10556 (defun gnus-summary-save-article (&optional n not-saved)
10557   "Save the current article using the default saver function.
10558 If N is a positive number, save the N next articles.
10559 If N is a negative number, save the N previous articles.
10560 If N is nil and any articles have been marked with the process mark,
10561 save those articles instead.
10562 The variable `gnus-default-article-saver' specifies the saver function."
10563   (interactive "P")
10564   (let* ((articles (gnus-summary-work-articles n))
10565          (save-buffer (save-excursion
10566                         (nnheader-set-temp-buffer " *Gnus Save*")))
10567          (num (length articles))
10568          header file)
10569     (dolist (article articles)
10570       (setq header (gnus-summary-article-header article))
10571       (if (not (vectorp header))
10572           ;; This is a pseudo-article.
10573           (if (assq 'name header)
10574               (gnus-copy-file (cdr (assq 'name header)))
10575             (gnus-message 1 "Article %d is unsaveable" article))
10576         ;; This is a real article.
10577         (save-window-excursion
10578           (let ((gnus-display-mime-function nil)
10579                 (gnus-article-prepare-hook nil))
10580             (gnus-summary-select-article t nil nil article)))
10581         (save-excursion
10582           (set-buffer save-buffer)
10583           (erase-buffer)
10584           (insert-buffer-substring gnus-original-article-buffer))
10585         (setq file (gnus-article-save save-buffer file num))
10586         (gnus-summary-remove-process-mark article)
10587         (unless not-saved
10588           (gnus-summary-set-saved-mark article))))
10589     (gnus-kill-buffer save-buffer)
10590     (gnus-summary-position-point)
10591     (gnus-set-mode-line 'summary)
10592     n))
10593
10594 (defun gnus-summary-pipe-output (&optional arg headers)
10595   "Pipe the current article to a subprocess.
10596 If N is a positive number, pipe the N next articles.
10597 If N is a negative number, pipe the N previous articles.
10598 If N is nil and any articles have been marked with the process mark,
10599 pipe those articles instead.
10600 If HEADERS (the symbolic prefix), include the headers, too."
10601   (interactive (gnus-interactive "P\ny"))
10602   (require 'gnus-art)
10603   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10604         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10605     (gnus-summary-save-article arg t))
10606   (let ((buffer (get-buffer "*Shell Command Output*")))
10607     (when (and buffer
10608                (not (zerop (buffer-size buffer))))
10609       (gnus-configure-windows 'pipe))))
10610
10611 (defun gnus-summary-save-article-mail (&optional arg)
10612   "Append the current article to an mail file.
10613 If N is a positive number, save the N next articles.
10614 If N is a negative number, save the N previous articles.
10615 If N is nil and any articles have been marked with the process mark,
10616 save those articles instead."
10617   (interactive "P")
10618   (require 'gnus-art)
10619   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10620     (gnus-summary-save-article arg)))
10621
10622 (defun gnus-summary-save-article-rmail (&optional arg)
10623   "Append the current article to an rmail file.
10624 If N is a positive number, save the N next articles.
10625 If N is a negative number, save the N previous articles.
10626 If N is nil and any articles have been marked with the process mark,
10627 save those articles instead."
10628   (interactive "P")
10629   (require 'gnus-art)
10630   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10631     (gnus-summary-save-article arg)))
10632
10633 (defun gnus-summary-save-article-file (&optional arg)
10634   "Append the current article to a file.
10635 If N is a positive number, save the N next articles.
10636 If N is a negative number, save the N previous articles.
10637 If N is nil and any articles have been marked with the process mark,
10638 save those articles instead."
10639   (interactive "P")
10640   (require 'gnus-art)
10641   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10642     (gnus-summary-save-article arg)))
10643
10644 (defun gnus-summary-write-article-file (&optional arg)
10645   "Write the current article to a file, deleting the previous file.
10646 If N is a positive number, save the N next articles.
10647 If N is a negative number, save the N previous articles.
10648 If N is nil and any articles have been marked with the process mark,
10649 save those articles instead."
10650   (interactive "P")
10651   (require 'gnus-art)
10652   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10653     (gnus-summary-save-article arg)))
10654
10655 (defun gnus-summary-save-article-body-file (&optional arg)
10656   "Append the current article body to a file.
10657 If N is a positive number, save the N next articles.
10658 If N is a negative number, save the N previous articles.
10659 If N is nil and any articles have been marked with the process mark,
10660 save those articles instead."
10661   (interactive "P")
10662   (require 'gnus-art)
10663   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10664     (gnus-summary-save-article arg)))
10665
10666 (defun gnus-summary-muttprint (&optional arg)
10667   "Print the current article using Muttprint.
10668 If N is a positive number, save the N next articles.
10669 If N is a negative number, save the N previous articles.
10670 If N is nil and any articles have been marked with the process mark,
10671 save those articles instead."
10672   (interactive "P")
10673   (require 'gnus-art)
10674   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10675     (gnus-summary-save-article arg t)))
10676
10677 (defun gnus-summary-pipe-message (program)
10678   "Pipe the current article through PROGRAM."
10679   (interactive "sProgram: ")
10680   (gnus-summary-select-article)
10681   (let ((mail-header-separator ""))
10682     (gnus-eval-in-buffer-window gnus-article-buffer
10683       (save-restriction
10684         (widen)
10685         (let ((start (window-start))
10686               buffer-read-only)
10687           (message-pipe-buffer-body program)
10688           (set-window-start (get-buffer-window (current-buffer)) start))))))
10689
10690 (defun gnus-get-split-value (methods)
10691   "Return a value based on the split METHODS."
10692   (let (split-name method result match)
10693     (when methods
10694       (save-excursion
10695         (set-buffer gnus-original-article-buffer)
10696         (save-restriction
10697           (nnheader-narrow-to-headers)
10698           (while (and methods (not split-name))
10699             (goto-char (point-min))
10700             (setq method (pop methods))
10701             (setq match (car method))
10702             (when (cond
10703                    ((stringp match)
10704                     ;; Regular expression.
10705                     (ignore-errors
10706                       (re-search-forward match nil t)))
10707                    ((gnus-functionp match)
10708                     ;; Function.
10709                     (save-restriction
10710                       (widen)
10711                       (setq result (funcall match gnus-newsgroup-name))))
10712                    ((consp match)
10713                     ;; Form.
10714                     (save-restriction
10715                       (widen)
10716                       (setq result (eval match)))))
10717               (setq split-name (cdr method))
10718               (cond ((stringp result)
10719                      (push (expand-file-name
10720                             result gnus-article-save-directory)
10721                            split-name))
10722                     ((consp result)
10723                      (setq split-name (append result split-name)))))))))
10724     (nreverse split-name)))
10725
10726 (defun gnus-valid-move-group-p (group)
10727   (and (boundp group)
10728        (symbol-name group)
10729        (symbol-value group)
10730        (gnus-get-function (gnus-find-method-for-group
10731                            (symbol-name group)) 'request-accept-article t)))
10732
10733 (defun gnus-read-move-group-name (prompt default articles prefix)
10734   "Read a group name."
10735   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10736          (minibuffer-confirm-incomplete nil) ; XEmacs
10737          (prom
10738           (format "%s %s to:"
10739                   prompt
10740                   (if (> (length articles) 1)
10741                       (format "these %d articles" (length articles))
10742                     "this article")))
10743          (to-newsgroup
10744           (cond
10745            ((null split-name)
10746             (gnus-completing-read-with-default
10747              default prom
10748              gnus-active-hashtb
10749              'gnus-valid-move-group-p
10750              nil prefix
10751              'gnus-group-history))
10752            ((= 1 (length split-name))
10753             (gnus-completing-read-with-default
10754              (car split-name) prom
10755              gnus-active-hashtb
10756              'gnus-valid-move-group-p
10757              nil nil
10758              'gnus-group-history))
10759            (t
10760             (gnus-completing-read-with-default
10761              nil prom
10762              (mapcar (lambda (el) (list el))
10763                      (nreverse split-name))
10764              nil nil nil
10765              'gnus-group-history))))
10766          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10767     (when to-newsgroup
10768       (if (or (string= to-newsgroup "")
10769               (string= to-newsgroup prefix))
10770           (setq to-newsgroup default))
10771       (unless to-newsgroup
10772         (error "No group name entered"))
10773       (or (gnus-active to-newsgroup)
10774           (gnus-activate-group to-newsgroup nil nil to-method)
10775           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10776                                      to-newsgroup))
10777               (or (and (gnus-request-create-group to-newsgroup to-method)
10778                        (gnus-activate-group
10779                         to-newsgroup nil nil to-method)
10780                        (gnus-subscribe-group to-newsgroup))
10781                   (error "Couldn't create group %s" to-newsgroup)))
10782           (error "No such group: %s" to-newsgroup)))
10783     to-newsgroup))
10784
10785 (defun gnus-summary-save-parts (type dir n &optional reverse)
10786   "Save parts matching TYPE to DIR.
10787 If REVERSE, save parts that do not match TYPE."
10788   (interactive
10789    (list (read-string "Save parts of type: "
10790                       (or (car gnus-summary-save-parts-type-history)
10791                           gnus-summary-save-parts-default-mime)
10792                       'gnus-summary-save-parts-type-history)
10793          (setq gnus-summary-save-parts-last-directory
10794                (read-file-name "Save to directory: "
10795                                gnus-summary-save-parts-last-directory
10796                                nil t))
10797          current-prefix-arg))
10798   (gnus-summary-iterate n
10799     (let ((gnus-display-mime-function nil)
10800           (gnus-inhibit-treatment t))
10801       (gnus-summary-select-article))
10802     (save-excursion
10803       (set-buffer gnus-article-buffer)
10804       (let ((handles (or gnus-article-mime-handles
10805                          (mm-dissect-buffer nil gnus-article-loose-mime)
10806                          (mm-uu-dissect))))
10807         (when handles
10808           (gnus-summary-save-parts-1 type dir handles reverse)
10809           (unless gnus-article-mime-handles ;; Don't destroy this case.
10810             (mm-destroy-parts handles)))))))
10811
10812 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10813   (if (stringp (car handle))
10814       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10815               (cdr handle))
10816     (when (if reverse
10817               (not (string-match type (mm-handle-media-type handle)))
10818             (string-match type (mm-handle-media-type handle)))
10819       (let ((file (expand-file-name
10820                    (file-name-nondirectory
10821                     (or
10822                      (mail-content-type-get
10823                       (mm-handle-disposition handle) 'filename)
10824                      (concat gnus-newsgroup-name
10825                              "." (number-to-string
10826                                   (cdr gnus-article-current)))))
10827                    dir)))
10828         (unless (file-exists-p file)
10829           (mm-save-part-to-file handle file))))))
10830
10831 ;; Summary extract commands
10832
10833 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10834   (let ((buffer-read-only nil)
10835         (article (gnus-summary-article-number))
10836         after-article b e)
10837     (unless (gnus-summary-goto-subject article)
10838       (error "No such article: %d" article))
10839     (gnus-summary-position-point)
10840     ;; If all commands are to be bunched up on one line, we collect
10841     ;; them here.
10842     (unless gnus-view-pseudos-separately
10843       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10844             files action)
10845         (while ps
10846           (setq action (cdr (assq 'action (car ps))))
10847           (setq files (list (cdr (assq 'name (car ps)))))
10848           (while (and ps (cdr ps)
10849                       (string= (or action "1")
10850                                (or (cdr (assq 'action (cadr ps))) "2")))
10851             (push (cdr (assq 'name (cadr ps))) files)
10852             (setcdr ps (cddr ps)))
10853           (when files
10854             (when (not (string-match "%s" action))
10855               (push " " files))
10856             (push " " files)
10857             (when (assq 'execute (car ps))
10858               (setcdr (assq 'execute (car ps))
10859                       (funcall (if (string-match "%s" action)
10860                                    'format 'concat)
10861                                action
10862                                (mapconcat
10863                                 (lambda (f)
10864                                   (if (equal f " ")
10865                                       f
10866                                     (gnus-quote-arg-for-sh-or-csh f)))
10867                                 files " ")))))
10868           (setq ps (cdr ps)))))
10869     (if (and gnus-view-pseudos (not not-view))
10870         (while pslist
10871           (when (assq 'execute (car pslist))
10872             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10873                                   (eq gnus-view-pseudos 'not-confirm)))
10874           (setq pslist (cdr pslist)))
10875       (save-excursion
10876         (while pslist
10877           (setq after-article (or (cdr (assq 'article (car pslist)))
10878                                   (gnus-summary-article-number)))
10879           (gnus-summary-goto-subject after-article)
10880           (forward-line 1)
10881           (setq b (point))
10882           (insert "    " (file-name-nondirectory
10883                           (cdr (assq 'name (car pslist))))
10884                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10885           (setq e (point))
10886           (forward-line -1)             ; back to `b'
10887           (gnus-add-text-properties
10888            b (1- e) (list 'gnus-number gnus-reffed-article-number
10889                           gnus-mouse-face-prop gnus-mouse-face))
10890           (gnus-data-enter
10891            after-article gnus-reffed-article-number
10892            gnus-unread-mark b (car pslist) 0 (- e b))
10893           (setq gnus-newsgroup-unreads
10894                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10895                                          gnus-reffed-article-number))
10896           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10897           (setq pslist (cdr pslist)))))))
10898
10899 (defun gnus-pseudos< (p1 p2)
10900   (let ((c1 (cdr (assq 'action p1)))
10901         (c2 (cdr (assq 'action p2))))
10902     (and c1 c2 (string< c1 c2))))
10903
10904 (defun gnus-request-pseudo-article (props)
10905   (cond ((assq 'execute props)
10906          (gnus-execute-command (cdr (assq 'execute props)))))
10907   (let ((gnus-current-article (gnus-summary-article-number)))
10908     (gnus-run-hooks 'gnus-mark-article-hook)))
10909
10910 (defun gnus-execute-command (command &optional automatic)
10911   (save-excursion
10912     (gnus-article-setup-buffer)
10913     (set-buffer gnus-article-buffer)
10914     (setq buffer-read-only nil)
10915     (let ((command (if automatic command
10916                      (read-string "Command: " (cons command 0)))))
10917       (erase-buffer)
10918       (insert "$ " command "\n\n")
10919       (if gnus-view-pseudo-asynchronously
10920           (start-process "gnus-execute" (current-buffer) shell-file-name
10921                          shell-command-switch command)
10922         (call-process shell-file-name nil t nil
10923                       shell-command-switch command)))))
10924
10925 ;; Summary kill commands.
10926
10927 (defun gnus-summary-edit-global-kill (article)
10928   "Edit the \"global\" kill file."
10929   (interactive (list (gnus-summary-article-number)))
10930   (gnus-group-edit-global-kill article))
10931
10932 (defun gnus-summary-edit-local-kill ()
10933   "Edit a local kill file applied to the current newsgroup."
10934   (interactive)
10935   (setq gnus-current-headers (gnus-summary-article-header))
10936   (gnus-group-edit-local-kill
10937    (gnus-summary-article-number) gnus-newsgroup-name))
10938
10939 ;;; Header reading.
10940
10941 (defun gnus-read-header (id &optional header)
10942   "Read the headers of article ID and enter them into the Gnus system."
10943   (let ((group gnus-newsgroup-name)
10944         (gnus-override-method
10945          (or
10946           gnus-override-method
10947           (and (gnus-news-group-p gnus-newsgroup-name)
10948                (car (gnus-refer-article-methods)))))
10949         where)
10950     ;; First we check to see whether the header in question is already
10951     ;; fetched.
10952     (if (stringp id)
10953         ;; This is a Message-ID.
10954         (setq header (or header (gnus-id-to-header id)))
10955       ;; This is an article number.
10956       (setq header (or header (gnus-summary-article-header id))))
10957     (if (and header
10958              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10959         ;; We have found the header.
10960         header
10961       ;; If this is a sparse article, we have to nix out its
10962       ;; previous entry in the thread hashtb.
10963       (when (and header
10964                  (gnus-summary-article-sparse-p (mail-header-number header)))
10965         (let* ((parent (gnus-parent-id (mail-header-references header)))
10966                (thread (and parent (gnus-id-to-thread parent))))
10967           (when thread
10968             (delq (assq header thread) thread))))
10969       ;; We have to really fetch the header to this article.
10970       (save-excursion
10971         (set-buffer nntp-server-buffer)
10972         (when (setq where (gnus-request-head id group))
10973           (nnheader-fold-continuation-lines)
10974           (goto-char (point-max))
10975           (insert ".\n")
10976           (goto-char (point-min))
10977           (insert "211 ")
10978           (princ (cond
10979                   ((numberp id) id)
10980                   ((cdr where) (cdr where))
10981                   (header (mail-header-number header))
10982                   (t gnus-reffed-article-number))
10983                  (current-buffer))
10984           (insert " Article retrieved.\n"))
10985         (if (or (not where)
10986                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10987             ()                          ; Malformed head.
10988           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10989             (when (and (stringp id)
10990                        (not (string= (gnus-group-real-name group)
10991                                      (car where))))
10992               ;; If we fetched by Message-ID and the article came
10993               ;; from a different group, we fudge some bogus article
10994               ;; numbers for this article.
10995               (mail-header-set-number header gnus-reffed-article-number))
10996             (save-excursion
10997               (set-buffer gnus-summary-buffer)
10998               (decf gnus-reffed-article-number)
10999               (gnus-remove-header (mail-header-number header))
11000               (push header gnus-newsgroup-headers)
11001               (setq gnus-current-headers header)
11002               (push (mail-header-number header) gnus-newsgroup-limit)))
11003           header)))))
11004
11005 (defun gnus-remove-header (number)
11006   "Remove header NUMBER from `gnus-newsgroup-headers'."
11007   (if (and gnus-newsgroup-headers
11008            (= number (mail-header-number (car gnus-newsgroup-headers))))
11009       (pop gnus-newsgroup-headers)
11010     (let ((headers gnus-newsgroup-headers))
11011       (while (and (cdr headers)
11012                   (not (= number (mail-header-number (cadr headers)))))
11013         (pop headers))
11014       (when (cdr headers)
11015         (setcdr headers (cddr headers))))))
11016
11017 ;;;
11018 ;;; summary highlights
11019 ;;;
11020
11021 (defun gnus-highlight-selected-summary ()
11022   "Highlight selected article in summary buffer."
11023   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11024   (when gnus-summary-selected-face
11025     (save-excursion
11026       (let* ((beg (progn (beginning-of-line) (point)))
11027              (end (progn (end-of-line) (point)))
11028              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11029              (from (if (get-text-property beg gnus-mouse-face-prop)
11030                        beg
11031                      (or (next-single-property-change
11032                           beg gnus-mouse-face-prop nil end)
11033                          beg)))
11034              (to
11035               (if (= from end)
11036                   (- from 2)
11037                 (or (next-single-property-change
11038                      from gnus-mouse-face-prop nil end)
11039                     end))))
11040         ;; If no mouse-face prop on line we will have to = from = end,
11041         ;; so we highlight the entire line instead.
11042         (when (= (+ to 2) from)
11043           (setq from beg)
11044           (setq to end))
11045         (if gnus-newsgroup-selected-overlay
11046             ;; Move old overlay.
11047             (gnus-move-overlay
11048              gnus-newsgroup-selected-overlay from to (current-buffer))
11049           ;; Create new overlay.
11050           (gnus-overlay-put
11051            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11052            'face gnus-summary-selected-face))))))
11053
11054 (defvar gnus-summary-highlight-line-cached nil)
11055 (defvar gnus-summary-highlight-line-trigger nil)
11056
11057 (defun gnus-summary-highlight-line-0 ()
11058   (if (and (eq gnus-summary-highlight-line-trigger 
11059                gnus-summary-highlight)
11060            gnus-summary-highlight-line-cached)
11061       gnus-summary-highlight-line-cached
11062     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11063           gnus-summary-highlight-line-cached
11064           (let* ((cond (list 'cond))
11065                  (c cond)
11066                  (list gnus-summary-highlight))
11067             (while list
11068               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11069               (setq c (cdr c)
11070                     list (cdr list)))
11071             (gnus-byte-compile (list 'lambda nil cond))))))
11072
11073 (defun gnus-summary-highlight-line ()
11074   "Highlight current line according to `gnus-summary-highlight'."
11075   (let* ((beg (gnus-point-at-bol))
11076          (article (or (gnus-summary-article-number) gnus-current-article))
11077          (score (or (cdr (assq article
11078                                gnus-newsgroup-scored))
11079                     gnus-summary-default-score 0))
11080          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11081          (inhibit-read-only t)
11082          (default gnus-summary-default-score)
11083          (default-high gnus-summary-default-high-score)
11084          (default-low gnus-summary-default-low-score)
11085          (uncached (memq article gnus-newsgroup-undownloaded))
11086          (downloaded (not uncached)))
11087     (let ((face (funcall (gnus-summary-highlight-line-0))))
11088       (unless (eq face (get-text-property beg 'face))
11089         (gnus-put-text-property-excluding-characters-with-faces
11090          beg (gnus-point-at-eol) 'face
11091          (setq face (if (boundp face) (symbol-value face) face)))
11092         (when gnus-summary-highlight-line-function
11093           (funcall gnus-summary-highlight-line-function article face))))))
11094
11095 (defun gnus-update-read-articles (group unread &optional compute)
11096   "Update the list of read articles in GROUP.
11097 UNREAD is a sorted list."
11098   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11099          (entry (gnus-gethash group gnus-newsrc-hashtb))
11100          (info (nth 2 entry))
11101          (prev 1)
11102          read)
11103     (if (or (not info) (not active))
11104         ;; There is no info on this group if it was, in fact,
11105         ;; killed.  Gnus stores no information on killed groups, so
11106         ;; there's nothing to be done.
11107         ;; One could store the information somewhere temporarily,
11108         ;; perhaps...  Hmmm...
11109         ()
11110       ;; Remove any negative articles numbers.
11111       (while (and unread (< (car unread) 0))
11112         (setq unread (cdr unread)))
11113       ;; Remove any expired article numbers
11114       (while (and unread (< (car unread) (car active)))
11115         (setq unread (cdr unread)))
11116       ;; Compute the ranges of read articles by looking at the list of
11117       ;; unread articles.
11118       (while unread
11119         (when (/= (car unread) prev)
11120           (push (if (= prev (1- (car unread))) prev
11121                   (cons prev (1- (car unread))))
11122                 read))
11123         (setq prev (1+ (car unread)))
11124         (setq unread (cdr unread)))
11125       (when (<= prev (cdr active))
11126         (push (cons prev (cdr active)) read))
11127       (setq read (if (> (length read) 1) (nreverse read) read))
11128       (if compute
11129           read
11130         (save-excursion
11131           (let (setmarkundo)
11132             ;; Propagate the read marks to the backend.
11133             (when (gnus-check-backend-function 'request-set-mark group)
11134               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11135                     (add (gnus-remove-from-range read (gnus-info-read info))))
11136                 (when (or add del)
11137                   (unless (gnus-check-group group)
11138                     (error "Can't open server for %s" group))
11139                   (gnus-request-set-mark
11140                    group (delq nil (list (if add (list add 'add '(read)))
11141                                          (if del (list del 'del '(read))))))
11142                   (setq setmarkundo
11143                         `(gnus-request-set-mark
11144                           ,group
11145                           ',(delq nil (list
11146                                        (if del (list del 'add '(read)))
11147                                        (if add (list add 'del '(read))))))))))
11148             (set-buffer gnus-group-buffer)
11149             (gnus-undo-register
11150               `(progn
11151                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11152                  (gnus-info-set-read ',info ',(gnus-info-read info))
11153                  (gnus-get-unread-articles-in-group ',info
11154                                                     (gnus-active ,group))
11155                  (gnus-group-update-group ,group t)
11156                  ,setmarkundo))))
11157         ;; Enter this list into the group info.
11158         (gnus-info-set-read info read)
11159         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11160         (gnus-get-unread-articles-in-group info (gnus-active group))
11161         t))))
11162
11163 (defun gnus-offer-save-summaries ()
11164   "Offer to save all active summary buffers."
11165   (let (buffers)
11166     ;; Go through all buffers and find all summaries.
11167     (dolist (buffer (buffer-list))
11168       (when (and (setq buffer (buffer-name buffer))
11169                  (string-match "Summary" buffer)
11170                  (save-excursion
11171                    (set-buffer buffer)
11172                    ;; We check that this is, indeed, a summary buffer.
11173                    (and (eq major-mode 'gnus-summary-mode)
11174                         ;; Also make sure this isn't bogus.
11175                         gnus-newsgroup-prepared
11176                         ;; Also make sure that this isn't a
11177                         ;; dead summary buffer.
11178                         (not gnus-dead-summary-mode))))
11179         (push buffer buffers)))
11180     ;; Go through all these summary buffers and offer to save them.
11181     (when buffers
11182       (save-excursion
11183         (map-y-or-n-p
11184          "Update summary buffer %s? "
11185          (lambda (buf)
11186            (switch-to-buffer buf)
11187            (gnus-summary-exit))
11188          buffers)))))
11189
11190
11191 ;;; @ for mime-partial
11192 ;;;
11193
11194 (defun gnus-request-partial-message ()
11195   (save-excursion
11196     (let ((number (gnus-summary-article-number))
11197           (group gnus-newsgroup-name)
11198           (mother gnus-article-buffer))
11199       (set-buffer (get-buffer-create " *Partial Article*"))
11200       (erase-buffer)
11201       (setq mime-preview-buffer mother)
11202       (gnus-request-article-this-buffer number group)
11203       (mime-parse-buffer)
11204       )))
11205
11206 (autoload 'mime-combine-message/partial-pieces-automatically
11207   "mime-partial"
11208   "Internal method to combine message/partial messages automatically.")
11209
11210 (mime-add-condition
11211  'action '((type . message)(subtype . partial)
11212            (major-mode . gnus-original-article-mode)
11213            (method . mime-combine-message/partial-pieces-automatically)
11214            (summary-buffer-exp . gnus-summary-buffer)
11215            (request-partial-message-method . gnus-request-partial-message)
11216            ))
11217
11218
11219 ;;; @ for message/rfc822
11220 ;;;
11221
11222 (defun gnus-mime-extract-message/rfc822 (entity situation)
11223   "Burst a forwarded article."
11224   (save-excursion
11225     (set-buffer gnus-summary-buffer)
11226     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11227                                    gnus-newsgroup-name 'gnus-group-history))
11228            (gnus-group-marked (list group))
11229            article info)
11230       (with-temp-buffer
11231         (mime-insert-entity-content entity)
11232         (setq article (gnus-request-accept-article group)))
11233       (when (and (consp article)
11234                  (numberp (setq article (cdr article))))
11235         (setq info (gnus-get-info group))
11236         (gnus-info-set-read info
11237                             (gnus-remove-from-range (gnus-info-read info)
11238                                                     (list article)))
11239         (when (string-equal group gnus-newsgroup-name)
11240           (forward-line 1)
11241           (let (gnus-show-threads)
11242             (gnus-summary-goto-subject article t))
11243           (gnus-summary-clear-mark-forward 1))
11244         (set-buffer gnus-group-buffer)
11245         (gnus-group-get-new-news-this-group nil t)))))
11246
11247 (mime-add-condition
11248  'action '((type . message)(subtype . rfc822)
11249            (major-mode . gnus-original-article-mode)
11250            (method . gnus-mime-extract-message/rfc822)
11251            (mode . "extract")
11252            ))
11253
11254 (mime-add-condition
11255  'action '((type . message)(subtype . news)
11256            (major-mode . gnus-original-article-mode)
11257            (method . gnus-mime-extract-message/rfc822)
11258            (mode . "extract")
11259            ))
11260
11261 (defun gnus-mime-extract-multipart (entity situation)
11262   (let ((children (mime-entity-children entity))
11263         mime-acting-situation-to-override
11264         f)
11265     (while children
11266       (mime-play-entity (car children)
11267                         (cons (assq 'mode situation)
11268                               mime-acting-situation-to-override))
11269       (setq children (cdr children)))
11270     (if (setq f (cdr (assq 'after-method
11271                            mime-acting-situation-to-override)))
11272         (eval f)
11273       )))
11274
11275 (mime-add-condition
11276  'action '((type . multipart)
11277            (method . gnus-mime-extract-multipart)
11278            (mode . "extract")
11279            )
11280  'with-default)
11281
11282
11283 ;;; @ end
11284 ;;;
11285
11286 (defun gnus-summary-inherit-default-charset ()
11287   "Import `default-mime-charset' from summary buffer.
11288 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11289 of FLIM is used."
11290   (if (buffer-live-p gnus-summary-buffer)
11291       (let (d-m-c d-m-c-u)
11292         (with-current-buffer gnus-summary-buffer
11293           (setq d-m-c (if (local-variable-p 'default-mime-charset
11294                                             gnus-summary-buffer)
11295                           default-mime-charset
11296                         t)
11297                 ;; LIMIT
11298                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11299                                               gnus-summary-buffer)
11300                             (symbol-value 'default-mime-charset-unlimited)
11301                           t)))
11302         (if (eq t d-m-c)
11303             (kill-local-variable 'default-mime-charset)
11304           (set (make-local-variable 'default-mime-charset) d-m-c))
11305         (if (eq t d-m-c-u)
11306             (kill-local-variable 'default-mime-charset-unlimited)
11307           (set (make-local-variable 'default-mime-charset-unlimited)
11308                d-m-c-u)))))
11309
11310 (defun gnus-summary-setup-default-charset ()
11311   "Setup newsgroup default charset."
11312   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11313       (progn
11314         (setq gnus-newsgroup-charset nil)
11315         (set (make-local-variable 'default-mime-charset) nil)
11316         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11317           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11318     (let ((ignored-charsets
11319            (or gnus-newsgroup-ephemeral-ignored-charsets
11320                (append
11321                 (and gnus-newsgroup-name
11322                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11323                 gnus-newsgroup-ignored-charsets)))
11324           charset)
11325       (setq gnus-newsgroup-charset
11326             (or gnus-newsgroup-ephemeral-charset
11327                 (when (and gnus-newsgroup-name
11328                            (setq charset (gnus-parameter-charset
11329                                           gnus-newsgroup-name)))
11330                   (make-local-variable 'default-mime-charset)
11331                   (setq default-mime-charset charset))
11332                 gnus-default-charset))
11333       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11334            ignored-charsets))))
11335
11336 ;;;
11337 ;;; Mime Commands
11338 ;;;
11339
11340 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11341   "Display the current article buffer fully MIME-buttonized.
11342 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11343 treated as multipart/mixed."
11344   (interactive "P")
11345   (require 'gnus-art)
11346   (let ((gnus-unbuttonized-mime-types nil)
11347         (gnus-mime-display-multipart-as-mixed show-all-parts))
11348     (gnus-summary-show-article)))
11349
11350 (defun gnus-summary-repair-multipart (article)
11351   "Add a Content-Type header to a multipart article without one."
11352   (interactive (list (gnus-summary-article-number)))
11353   (gnus-with-article article
11354     (message-narrow-to-head)
11355     (message-remove-header "Mime-Version")
11356     (goto-char (point-max))
11357     (insert "Mime-Version: 1.0\n")
11358     (widen)
11359     (when (search-forward "\n--" nil t)
11360       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11361         (message-narrow-to-head)
11362         (message-remove-header "Content-Type")
11363         (goto-char (point-max))
11364         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11365                         separator))
11366         (widen))))
11367   (let (gnus-mark-article-hook)
11368     (gnus-summary-select-article t t nil article)))
11369
11370 (defun gnus-summary-toggle-display-buttonized ()
11371   "Toggle the buttonizing of the article buffer."
11372   (interactive)
11373   (require 'gnus-art)
11374   (if (setq gnus-inhibit-mime-unbuttonizing
11375             (not gnus-inhibit-mime-unbuttonizing))
11376       (let ((gnus-unbuttonized-mime-types nil))
11377         (gnus-summary-show-article))
11378     (gnus-summary-show-article)))
11379
11380 ;;;
11381 ;;; Intelli-mouse commmands
11382 ;;;
11383
11384 (defun gnus-wheel-summary-scroll (event)
11385   (interactive "e")
11386   (let ((amount (if (memq 'shift (event-modifiers event))
11387                     (car gnus-wheel-scroll-amount)
11388                   (cdr gnus-wheel-scroll-amount)))
11389         (direction (- (* (static-if (featurep 'xemacs)
11390                              (event-button event)
11391                            (cond ((eq 'mouse-4 (event-basic-type event))
11392                                   4)
11393                                  ((eq 'mouse-5 (event-basic-type event))
11394                                   5)))
11395                          2) 9))
11396         edge)
11397     (gnus-summary-scroll-up (* amount direction))
11398     (when (gnus-eval-in-buffer-window gnus-article-buffer
11399             (save-restriction
11400               (widen)
11401               (and (if (< 0 direction)
11402                        (gnus-article-next-page 0)
11403                      (gnus-article-prev-page 0)
11404                      (bobp))
11405                    (if (setq edge (get-text-property
11406                                    (point-min) 'gnus-wheel-edge))
11407                        (setq edge (* edge direction))
11408                      (setq edge -1))
11409                    (or (plusp edge)
11410                        (let ((buffer-read-only nil)
11411                              (inhibit-read-only t))
11412                          (put-text-property (point-min) (point-max)
11413                                             'gnus-wheel-edge direction)
11414                          nil))
11415                    (or (> edge gnus-wheel-edge-resistance)
11416                        (let ((buffer-read-only nil)
11417                              (inhibit-read-only t))
11418                          (put-text-property (point-min) (point-max)
11419                                             'gnus-wheel-edge
11420                                             (* (1+ edge) direction))
11421                          nil))
11422                    (eq last-command 'gnus-wheel-summary-scroll))))
11423       (gnus-summary-next-article nil nil (minusp direction)))))
11424
11425 (defun gnus-wheel-install ()
11426   "Enable mouse wheel support on summary window."
11427   (when gnus-use-wheel
11428     (let ((keys
11429            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11430       (dolist (key keys)
11431         (define-key gnus-summary-mode-map key
11432           'gnus-wheel-summary-scroll)))))
11433
11434 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11435
11436 ;;;
11437 ;;; Traditional PGP commmands
11438 ;;;
11439
11440 (defun gnus-summary-decrypt-article (&optional force)
11441   "Decrypt the current article in traditional PGP way.
11442 This will have permanent effect only in mail groups.
11443 If FORCE is non-nil, allow editing of articles even in read-only
11444 groups."
11445   (interactive "P")
11446   (gnus-summary-select-article t)
11447   (gnus-eval-in-buffer-window gnus-article-buffer
11448     (save-excursion
11449       (save-restriction
11450         (widen)
11451         (goto-char (point-min))
11452         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11453           (error "Not a traditional PGP message!"))
11454         (let ((armor-start (match-beginning 0)))
11455           (if (and (pgg-decrypt-region armor-start (point-max))
11456                    (or force (not (gnus-group-read-only-p))))
11457               (let ((inhibit-read-only t)
11458                     buffer-read-only)
11459                 (delete-region armor-start
11460                                (progn
11461                                  (re-search-forward "^-+END PGP" nil t)
11462                                  (beginning-of-line 2)
11463                                  (point)))
11464                 (insert-buffer-substring pgg-output-buffer))))))))
11465
11466 (defun gnus-summary-verify-article ()
11467   "Verify the current article in traditional PGP way."
11468   (interactive)
11469   (save-excursion
11470     (set-buffer gnus-original-article-buffer)
11471     (goto-char (point-min))
11472     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11473       (error "Not a traditional PGP message!"))
11474     (re-search-forward "^-+END PGP" nil t)
11475     (beginning-of-line 2)
11476     (call-interactively (function pgg-verify-region))))
11477
11478 ;;;
11479 ;;; Generic summary marking commands
11480 ;;;
11481
11482 (defvar gnus-summary-marking-alist
11483   '((read gnus-del-mark "d")
11484     (unread gnus-unread-mark "u")
11485     (ticked gnus-ticked-mark "!")
11486     (dormant gnus-dormant-mark "?")
11487     (expirable gnus-expirable-mark "e"))
11488   "An alist of names/marks/keystrokes.")
11489
11490 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11491 (defvar gnus-summary-mark-map)
11492
11493 (defun gnus-summary-make-all-marking-commands ()
11494   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11495   (dolist (elem gnus-summary-marking-alist)
11496     (apply 'gnus-summary-make-marking-command elem)))
11497
11498 (defun gnus-summary-make-marking-command (name mark keystroke)
11499   (let ((map (make-sparse-keymap)))
11500     (define-key gnus-summary-generic-mark-map keystroke map)
11501     (dolist (lway `((next "next" next nil "n")
11502                     (next-unread "next unread" next t "N")
11503                     (prev "previous" prev nil "p")
11504                     (prev-unread "previous unread" prev t "P")
11505                     (nomove "" nil nil ,keystroke)))
11506       (let ((func (gnus-summary-make-marking-command-1
11507                    mark (car lway) lway name)))
11508         (setq func (eval func))
11509         (define-key map (nth 4 lway) func)))))
11510
11511 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11512   `(defun ,(intern
11513             (format "gnus-summary-put-mark-as-%s%s"
11514                     name (if (eq way 'nomove)
11515                              ""
11516                            (concat "-" (symbol-name way)))))
11517      (n)
11518      ,(format
11519        "Mark the current article as %s%s.
11520 If N, the prefix, then repeat N times.
11521 If N is negative, move in reverse order.
11522 The difference between N and the actual number of articles marked is
11523 returned."
11524        name (car (cdr lway)))
11525      (interactive "p")
11526      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11527
11528 (defun gnus-summary-generic-mark (n mark move unread)
11529   "Mark N articles with MARK."
11530   (unless (eq major-mode 'gnus-summary-mode)
11531     (error "This command can only be used in the summary buffer"))
11532   (gnus-summary-show-thread)
11533   (let ((nummove
11534          (cond
11535           ((eq move 'next) 1)
11536           ((eq move 'prev) -1)
11537           (t 0))))
11538     (if (zerop nummove)
11539         (setq n 1)
11540       (when (< n 0)
11541         (setq n (abs n)
11542               nummove (* -1 nummove))))
11543     (while (and (> n 0)
11544                 (gnus-summary-mark-article nil mark)
11545                 (zerop (gnus-summary-next-subject nummove unread t)))
11546       (setq n (1- n)))
11547     (when (/= 0 n)
11548       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11549     (gnus-summary-recenter)
11550     (gnus-summary-position-point)
11551     (gnus-set-mode-line 'summary)
11552     n))
11553
11554 (defun gnus-summary-insert-articles (articles)
11555   (when (setq articles
11556               (gnus-sorted-difference articles
11557                                       (mapcar (lambda (h)
11558                                                 (mail-header-number h))
11559                                               gnus-newsgroup-headers)))
11560     (setq gnus-newsgroup-headers
11561           (merge 'list
11562                  gnus-newsgroup-headers
11563                  (gnus-fetch-headers articles)
11564                  'gnus-article-sort-by-number))
11565     ;; Suppress duplicates?
11566     (when gnus-suppress-duplicates
11567       (gnus-dup-suppress-articles))
11568
11569     ;; We might want to build some more threads first.
11570     (when (and gnus-fetch-old-headers
11571                (eq gnus-headers-retrieved-by 'nov))
11572       (if (eq gnus-fetch-old-headers 'invisible)
11573           (gnus-build-all-threads)
11574         (gnus-build-old-threads)))
11575     ;; Let the Gnus agent mark articles as read.
11576     (when gnus-agent
11577       (gnus-agent-get-undownloaded-list))
11578     ;; Remove list identifiers from subject
11579     (when gnus-list-identifiers
11580       (gnus-summary-remove-list-identifiers))
11581     ;; First and last article in this newsgroup.
11582     (when gnus-newsgroup-headers
11583       (setq gnus-newsgroup-begin
11584             (mail-header-number (car gnus-newsgroup-headers))
11585             gnus-newsgroup-end
11586             (mail-header-number
11587              (gnus-last-element gnus-newsgroup-headers))))
11588     (when gnus-use-scoring
11589       (gnus-possibly-score-headers))))
11590
11591 (defun gnus-summary-insert-old-articles (&optional all)
11592   "Insert all old articles in this group.
11593 If ALL is non-nil, already read articles become readable.
11594 If ALL is a number, fetch this number of articles."
11595   (interactive "P")
11596   (prog1
11597       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11598             older len)
11599         (setq older
11600               ;; Some nntp servers lie about their active range.  When
11601               ;; this happens, the active range can be in the millions.
11602               ;; Use a compressed range to avoid creating a huge list.
11603               (gnus-range-difference (list gnus-newsgroup-active) old))
11604         (setq len (gnus-range-length older))
11605         (cond
11606          ((null older) nil)
11607          ((numberp all)
11608           (if (< all len)
11609               (let ((older-range (nreverse older)))
11610                 (setq older nil)
11611
11612                 (while (> all 0)
11613                   (let* ((r (pop older-range))
11614                          (min (if (numberp r) r (car r)))
11615                          (max (if (numberp r) r (cdr r))))
11616                     (while (and (<= min max)
11617                                 (> all 0))
11618                       (push max older)
11619                       (setq all (1- all)
11620                             max (1- max))))))
11621             (setq older (gnus-uncompress-range older))))
11622          (all
11623           (setq older (gnus-uncompress-range older)))
11624          (t
11625           (when (and (numberp gnus-large-newsgroup)
11626                    (> len gnus-large-newsgroup))
11627               (let* ((cursor-in-echo-area nil)
11628                      (initial (gnus-parameter-large-newsgroup-initial
11629                                gnus-newsgroup-name))
11630                      (input
11631                       (read-string
11632                        (format
11633                         "How many articles from %s (%s %d): "
11634                         (gnus-limit-string
11635                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11636                         (if initial "max" "default")
11637                         len)
11638                        (if initial
11639                            (cons (number-to-string initial)
11640                                  0)))))
11641                 (unless (string-match "^[ \t]*$" input)
11642                   (setq all (string-to-number input))
11643                   (if (< all len)
11644                       (let ((older-range (nreverse older)))
11645                         (setq older nil)
11646
11647                         (while (> all 0)
11648                           (let* ((r (pop older-range))
11649                                  (min (if (numberp r) r (car r)))
11650                                  (max (if (numberp r) r (cdr r))))
11651                             (while (and (<= min max)
11652                                         (> all 0))
11653                               (push max older)
11654                               (setq all (1- all)
11655                                     max (1- max))))))))))
11656           (setq older (gnus-uncompress-range older))))
11657         (if (not older)
11658             (message "No old news.")
11659           (gnus-summary-insert-articles older)
11660           (gnus-summary-limit (gnus-sorted-nunion old older))))
11661     (gnus-summary-position-point)))
11662
11663 (defun gnus-summary-insert-new-articles ()
11664   "Insert all new articles in this group."
11665   (interactive)
11666   (prog1
11667       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11668             (old-active gnus-newsgroup-active)
11669             (nnmail-fetched-sources (list t))
11670             i new)
11671         (setq gnus-newsgroup-active
11672               (gnus-activate-group gnus-newsgroup-name 'scan))
11673         (setq i (cdr gnus-newsgroup-active))
11674         (while (> i (cdr old-active))
11675           (push i new)
11676           (decf i))
11677         (if (not new)
11678             (message "No gnus is bad news.")
11679           (gnus-summary-insert-articles new)
11680           (setq gnus-newsgroup-unreads
11681                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11682           (gnus-summary-limit (gnus-sorted-nunion old new))))
11683     (gnus-summary-position-point)))
11684
11685 (gnus-summary-make-all-marking-commands)
11686
11687 (gnus-ems-redefine)
11688
11689 (provide 'gnus-sum)
11690
11691 (run-hooks 'gnus-sum-load-hook)
11692
11693 ;;; gnus-sum.el ends here