Synch with Oort Gnus.
[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, 2003
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 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-fetch-old-headers nil
72   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
73 If an unread article in the group refers to an older, already read (or
74 just marked as read) article, the old article will not normally be
75 displayed in the Summary buffer.  If this variable is non-nil, Gnus
76 will attempt to grab the headers to the old articles, and thereby
77 build complete threads.  If it has the value `some', only enough
78 headers to connect otherwise loose threads will be displayed.  This
79 variable can also be a number.  In that case, no more than that number
80 of old headers will be fetched.  If it has the value `invisible', all
81 old headers will be fetched, but none will be displayed.
82
83 The server has to support NOV for any of this to work."
84   :group 'gnus-thread
85   :type '(choice (const :tag "off" nil)
86                  (const some)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 200
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :group 'gnus-thread
129   :type 'boolean)
130
131 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
132   "*A regexp to match subjects to be excluded from loose thread gathering.
133 As loose thread gathering is done on subjects only, that means that
134 there can be many false gatherings performed.  By rooting out certain
135 common subjects, gathering might become saner."
136   :group 'gnus-thread
137   :type 'regexp)
138
139 (defcustom gnus-summary-gather-subject-limit nil
140   "*Maximum length of subject comparisons when gathering loose threads.
141 Use nil to compare full subjects.  Setting this variable to a low
142 number will help gather threads that have been corrupted by
143 newsreaders chopping off subject lines, but it might also mean that
144 unrelated articles that have subject that happen to begin with the
145 same few characters will be incorrectly gathered.
146
147 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
148 comparing subjects."
149   :group 'gnus-thread
150   :type '(choice (const :tag "off" nil)
151                  (const fuzzy)
152                  (sexp :menu-tag "on" t)))
153
154 (defcustom gnus-simplify-subject-functions nil
155   "List of functions taking a string argument that simplify subjects.
156 The functions are applied recursively.
157
158 Useful functions to put in this list include:
159 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
160 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
161   :group 'gnus-thread
162   :type '(repeat function))
163
164 (defcustom gnus-simplify-ignored-prefixes nil
165   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  regexp))
169
170 (defcustom gnus-build-sparse-threads nil
171   "*If non-nil, fill in the gaps in threads.
172 If `some', only fill in the gaps that are needed to tie loose threads
173 together.  If `more', fill in all leaf nodes that Gnus can find.  If
174 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  (const some)
178                  (const more)
179                  (sexp :menu-tag "all" t)))
180
181 (defcustom gnus-summary-thread-gathering-function
182   'gnus-gather-threads-by-subject
183   "*Function used for gathering loose threads.
184 There are two pre-defined functions: `gnus-gather-threads-by-subject',
185 which only takes Subjects into consideration; and
186 `gnus-gather-threads-by-references', which compared the References
187 headers of the articles to find matches."
188   :group 'gnus-thread
189   :type '(radio (function-item gnus-gather-threads-by-subject)
190                 (function-item gnus-gather-threads-by-references)
191                 (function :tag "other")))
192
193 (defcustom gnus-summary-same-subject ""
194   "*String indicating that the current article has the same subject as the previous.
195 This variable will only be used if the value of
196 `gnus-summary-make-false-root' is `empty'."
197   :group 'gnus-summary-format
198   :type 'string)
199
200 (defcustom gnus-summary-goto-unread t
201   "*If t, many commands will go to the next unread article.
202 This applies to marking commands as well as other commands that
203 \"naturally\" select the next article, like, for instance, `SPC' at
204 the end of an article.
205
206 If nil, the marking commands do NOT go to the next unread article
207 \(they go to the next article instead).  If `never', commands that
208 usually go to the next unread article, will go to the next article,
209 whether it is read or not."
210   :group 'gnus-summary-marks
211   :link '(custom-manual "(gnus)Setting Marks")
212   :type '(choice (const :tag "off" nil)
213                  (const never)
214                  (sexp :menu-tag "on" t)))
215
216 (defcustom gnus-summary-default-score 0
217   "*Default article score level.
218 All scores generated by the score files will be added to this score.
219 If this variable is nil, scoring will be disabled."
220   :group 'gnus-score-default
221   :type '(choice (const :tag "disable")
222                  integer))
223
224 (defcustom gnus-summary-default-high-score 0
225   "*Default threshold for a high scored article.
226 An article will be highlighted as high scored if its score is greater
227 than this score."
228   :group 'gnus-score-default
229   :type 'integer)
230
231 (defcustom gnus-summary-default-low-score 0
232   "*Default threshold for a low scored article.
233 An article will be highlighted as low scored if its score is smaller
234 than this score."
235   :group 'gnus-score-default
236   :type 'integer)
237
238 (defcustom gnus-summary-zcore-fuzz 0
239   "*Fuzziness factor for the zcore in the summary buffer.
240 Articles with scores closer than this to `gnus-summary-default-score'
241 will not be marked."
242   :group 'gnus-summary-format
243   :type 'integer)
244
245 (defcustom gnus-simplify-subject-fuzzy-regexp nil
246   "*Strings to be removed when doing fuzzy matches.
247 This can either be a regular expression or list of regular expressions
248 that will be removed from subject strings if fuzzy subject
249 simplification is selected."
250   :group 'gnus-thread
251   :type '(repeat regexp))
252
253 (defcustom gnus-show-threads t
254   "*If non-nil, display threads in summary mode."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-subtree nil
259   "*If non-nil, hide all threads initially.
260 This can be a predicate specifier which says which threads to hide.
261 If threads are hidden, you have to run the command
262 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
263 to expose hidden threads."
264   :group 'gnus-thread
265   :type 'boolean)
266
267 (defcustom gnus-thread-hide-killed t
268   "*If non-nil, hide killed threads automatically."
269   :group 'gnus-thread
270   :type 'boolean)
271
272 (defcustom gnus-thread-ignore-subject t
273   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
274 If nil, articles that have different subjects from their parents will
275 start separate threads."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-operation-ignore-subject t
280   "*If non-nil, subjects will be ignored when doing thread commands.
281 This affects commands like `gnus-summary-kill-thread' and
282 `gnus-summary-lower-thread'.
283
284 If this variable is nil, articles in the same thread with different
285 subjects will not be included in the operation in question.  If this
286 variable is `fuzzy', only articles that have subjects that are fuzzily
287 equal will be included."
288   :group 'gnus-thread
289   :type '(choice (const :tag "off" nil)
290                  (const fuzzy)
291                  (sexp :tag "on" t)))
292
293 (defcustom gnus-thread-indent-level 4
294   "*Number that says how much each sub-thread should be indented."
295   :group 'gnus-thread
296   :type 'integer)
297
298 (defcustom gnus-auto-extend-newsgroup t
299   "*If non-nil, extend newsgroup forward and backward when requested."
300   :group 'gnus-summary-choose
301   :type 'boolean)
302
303 (defcustom gnus-auto-select-first t
304   "*If non-nil, select the article under point.
305 Which article this is is controlled by the `gnus-auto-select-subject'
306 variable.
307
308 If you want to prevent automatic selection of articles in some
309 newsgroups, set the variable to nil in `gnus-select-group-hook'."
310   :group 'gnus-group-select
311   :type '(choice (const :tag "none" nil)
312                  (sexp :menu-tag "first" t)))
313
314 (defcustom gnus-auto-select-subject 'unread
315   "*Says what subject to place under point when entering a group.
316
317 This variable can either be the symbols `first' (place point on the
318 first subject), `unread' (place point on the subject line of the first
319 unread article), `best' (place point on the subject line of the
320 higest-scored article), `unseen' (place point on the subject line of
321 the first unseen article), 'unseen-or-unread' (place point on the subject
322 line of the first unseen article or, if all article have been seen, on the
323 subject line of the first unread article), or a function to be called to
324 place point on some subject line."
325   :group 'gnus-group-select
326   :type '(choice (const best)
327                  (const unread)
328                  (const first)
329                  (const unseen)
330                  (const unseen-or-unread)))
331
332 (defcustom gnus-dont-select-after-jump-to-other-group nil
333   "If non-nil, don't select the first unread article after entering the
334 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
335 it is depend on the value of `gnus-auto-select-first' whether to select
336 or not."
337   :group 'gnus-group-select
338   :type 'boolean)
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `Z n' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defcustom gnus-show-all-headers nil
383   "*If non-nil, don't hide any headers."
384   :group 'gnus-article-hiding
385   :group 'gnus-article-headers
386   :type 'boolean)
387
388 (defcustom gnus-summary-ignore-duplicates nil
389   "*If non-nil, ignore articles with identical Message-ID headers."
390   :group 'gnus-summary
391   :type 'boolean)
392
393 (defcustom gnus-single-article-buffer t
394   "*If non-nil, display all articles in the same buffer.
395 If nil, each group will get its own article buffer."
396   :group 'gnus-article-various
397   :type 'boolean)
398
399 (defcustom gnus-break-pages t
400   "*If non-nil, do page breaking on articles.
401 The page delimiter is specified by the `gnus-page-delimiter'
402 variable."
403   :group 'gnus-article-various
404   :type 'boolean)
405
406 (defcustom gnus-show-mime t
407   "*If non-nil, do mime processing of articles.
408 The articles will simply be fed to the function given by
409 `gnus-article-display-method-for-mime'."
410   :group 'gnus-article-mime
411   :type 'boolean)
412
413 (defcustom gnus-move-split-methods nil
414   "*Variable used to suggest where articles are to be moved to.
415 It uses the same syntax as the `gnus-split-methods' variable.
416 However, whereas `gnus-split-methods' specifies file names as targets,
417 this variable specifies group names."
418   :group 'gnus-summary-mail
419   :type '(repeat (choice (list :value (fun) function)
420                          (cons :value ("" "") regexp (repeat string))
421                          (sexp :value nil))))
422
423 (defcustom gnus-unread-mark ?\ ;;;Whitespace
424   "*Mark used for unread articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-ticked-mark ?!
429   "*Mark used for ticked articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-dormant-mark ??
434   "*Mark used for dormant articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-del-mark ?r
439   "*Mark used for del'd articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-read-mark ?R
444   "*Mark used for read articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-expirable-mark ?E
449   "*Mark used for expirable articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-killed-mark ?K
454   "*Mark used for killed articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-spam-mark ?H
459   "*Mark used for spam articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-souped-mark ?F
464   "*Mark used for souped articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-kill-file-mark ?X
469   "*Mark used for articles killed by kill files."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-low-score-mark ?Y
474   "*Mark used for articles with a low score."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-catchup-mark ?C
479   "*Mark used for articles that are caught up."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-replied-mark ?A
484   "*Mark used for articles that have been replied to."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-forwarded-mark ?F
489   "*Mark used for articles that have been forwarded."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-recent-mark ?N
494   "*Mark used for articles that are recent."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-cached-mark ?*
499   "*Mark used for articles that are in the cache."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-saved-mark ?S
504   "*Mark used for articles that have been saved."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-unseen-mark ?.
509   "*Mark used for articles that haven't been seen."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-no-mark ?\ ;;;Whitespace
514   "*Mark used for articles that have no other secondary mark."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-ancient-mark ?O
519   "*Mark used for ancient articles."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-sparse-mark ?Q
524   "*Mark used for sparsely reffed articles."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-canceled-mark ?G
529   "*Mark used for canceled articles."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-duplicate-mark ?M
534   "*Mark used for duplicate articles."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-undownloaded-mark ?-
539   "*Mark used for articles that weren't downloaded."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-downloaded-mark ?+
544   "*Mark used for articles that were downloaded."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-downloadable-mark ?%
549   "*Mark used for articles that are to be downloaded."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-unsendable-mark ?=
554   "*Mark used for articles that won't be sent."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-score-over-mark ?+
559   "*Score mark used for articles with high scores."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-score-below-mark ?-
564   "*Score mark used for articles with low scores."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
569   "*There is no thread under the article."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-not-empty-thread-mark ?=
574   "*There is a thread under the article."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-view-pseudo-asynchronously nil
579   "*If non-nil, Gnus will view pseudo-articles asynchronously."
580   :group 'gnus-extract-view
581   :type 'boolean)
582
583 (defcustom gnus-auto-expirable-marks
584   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
585         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
586         gnus-souped-mark gnus-duplicate-mark)
587   "*The list of marks converted into expiration if a group is auto-expirable."
588   :version "21.1"
589   :group 'gnus-summary
590   :type '(repeat character))
591
592 (defcustom gnus-inhibit-user-auto-expire t
593   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
594   :version "21.1"
595   :group 'gnus-summary
596   :type 'boolean)
597
598 (defcustom gnus-view-pseudos nil
599   "*If `automatic', pseudo-articles will be viewed automatically.
600 If `not-confirm', pseudos will be viewed automatically, and the user
601 will not be asked to confirm the command."
602   :group 'gnus-extract-view
603   :type '(choice (const :tag "off" nil)
604                  (const automatic)
605                  (const not-confirm)))
606
607 (defcustom gnus-view-pseudos-separately t
608   "*If non-nil, one pseudo-article will be created for each file to be viewed.
609 If nil, all files that use the same viewing command will be given as a
610 list of parameters to that command."
611   :group 'gnus-extract-view
612   :type 'boolean)
613
614 (defcustom gnus-insert-pseudo-articles t
615   "*If non-nil, insert pseudo-articles when decoding articles."
616   :group 'gnus-extract-view
617   :type 'boolean)
618
619 (defcustom gnus-summary-dummy-line-format
620   "   %(:                             :%) %S\n"
621   "*The format specification for the dummy roots in the summary buffer.
622 It works along the same lines as a normal formatting string,
623 with some simple extensions.
624
625 %S  The subject
626
627 General format specifiers can also be used.
628 See `(gnus)Formatting Variables'."
629   :link '(custom-manual "(gnus)Formatting Variables")
630   :group 'gnus-threading
631   :type 'string)
632
633 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
634   "*The format specification for the summary mode line.
635 It works along the same lines as a normal formatting string,
636 with some simple extensions:
637
638 %G  Group name
639 %p  Unprefixed group name
640 %A  Current article number
641 %z  Current article score
642 %V  Gnus version
643 %U  Number of unread articles in the group
644 %e  Number of unselected articles in the group
645 %Z  A string with unread/unselected article counts
646 %g  Shortish group name
647 %S  Subject of the current article
648 %u  User-defined spec
649 %s  Current score file name
650 %d  Number of dormant articles
651 %r  Number of articles that have been marked as read in this session
652 %E  Number of articles expunged by the score files"
653   :group 'gnus-summary-format
654   :type 'string)
655
656 (defcustom gnus-list-identifiers nil
657   "Regexp that matches list identifiers to be removed from subject.
658 This can also be a list of regexps."
659   :version "21.1"
660   :group 'gnus-summary-format
661   :group 'gnus-article-hiding
662   :type '(choice (const :tag "none" nil)
663                  (regexp :value ".*")
664                  (repeat :value (".*") regexp)))
665
666 (defcustom gnus-summary-mark-below 0
667   "*Mark all articles with a score below this variable as read.
668 This variable is local to each summary buffer and usually set by the
669 score file."
670   :group 'gnus-score-default
671   :type 'integer)
672
673 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
674   "*List of functions used for sorting articles in the summary buffer.
675
676 Each function takes two articles and returns non-nil if the first
677 article should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-article-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-article-sort-by-number',
686 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
687 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
688 and `gnus-article-sort-by-score'.
689
690 When threading is turned on, the variable `gnus-thread-sort-functions'
691 controls how articles are sorted."
692   :group 'gnus-summary-sort
693   :type '(repeat (choice (function-item gnus-article-sort-by-number)
694                          (function-item gnus-article-sort-by-author)
695                          (function-item gnus-article-sort-by-subject)
696                          (function-item gnus-article-sort-by-date)
697                          (function-item gnus-article-sort-by-score)
698                          (function-item gnus-article-sort-by-random)
699                          (function :tag "other"))))
700
701 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
702   "*List of functions used for sorting threads in the summary buffer.
703 By default, threads are sorted by article number.
704
705 Each function takes two threads and returns non-nil if the first
706 thread should be sorted before the other.  If you use more than one
707 function, the primary sort function should be the last.  You should
708 probably always include `gnus-thread-sort-by-number' in the list of
709 sorting functions -- preferably first.  Also note that sorting by date
710 is often much slower than sorting by number, and the sorting order is
711 very similar.  (Sorting by date means sorting by the time the message
712 was sent, sorting by number means sorting by arrival time.)
713
714 Ready-made functions include `gnus-thread-sort-by-number',
715 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
716 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
717 `gnus-thread-sort-by-most-recent-number',
718 `gnus-thread-sort-by-most-recent-date',
719 `gnus-thread-sort-by-random', and
720 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
721
722 When threading is turned off, the variable
723 `gnus-article-sort-functions' controls how articles are sorted."
724   :group 'gnus-summary-sort
725   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
726                          (function-item gnus-thread-sort-by-author)
727                          (function-item gnus-thread-sort-by-subject)
728                          (function-item gnus-thread-sort-by-date)
729                          (function-item gnus-thread-sort-by-score)
730                          (function-item gnus-thread-sort-by-total-score)
731                          (function-item gnus-thread-sort-by-random)
732                          (function :tag "other"))))
733
734 (defcustom gnus-thread-score-function '+
735   "*Function used for calculating the total score of a thread.
736
737 The function is called with the scores of the article and each
738 subthread and should then return the score of the thread.
739
740 Some functions you can use are `+', `max', or `min'."
741   :group 'gnus-summary-sort
742   :type 'function)
743
744 (defcustom gnus-summary-expunge-below nil
745   "All articles that have a score less than this variable will be expunged.
746 This variable is local to the summary buffers."
747   :group 'gnus-score-default
748   :type '(choice (const :tag "off" nil)
749                  integer))
750
751 (defcustom gnus-thread-expunge-below nil
752   "All threads that have a total score less than this variable will be expunged.
753 See `gnus-thread-score-function' for en explanation of what a
754 \"thread score\" is.
755
756 This variable is local to the summary buffers."
757   :group 'gnus-threading
758   :group 'gnus-score-default
759   :type '(choice (const :tag "off" nil)
760                  integer))
761
762 (defcustom gnus-summary-mode-hook nil
763   "*A hook for Gnus summary mode.
764 This hook is run before any variables are set in the summary buffer."
765   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
770 (when (featurep 'xemacs)
771   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
772   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
773   (add-hook 'gnus-summary-mode-hook
774             'gnus-xmas-switch-horizontal-scrollbar-off))
775
776 (defcustom gnus-summary-menu-hook nil
777   "*Hook run after the creation of the summary mode menu."
778   :group 'gnus-summary-visual
779   :type 'hook)
780
781 (defcustom gnus-summary-exit-hook nil
782   "*A hook called on exit from the summary buffer.
783 It will be called with point in the group buffer."
784   :group 'gnus-summary-exit
785   :type 'hook)
786
787 (defcustom gnus-summary-prepare-hook nil
788   "*A hook called after the summary buffer has been generated.
789 If you want to modify the summary buffer, you can use this hook."
790   :group 'gnus-summary-various
791   :type 'hook)
792
793 (defcustom gnus-summary-prepared-hook nil
794   "*A hook called as the last thing after the summary buffer has been generated."
795   :group 'gnus-summary-various
796   :type 'hook)
797
798 (defcustom gnus-summary-generate-hook nil
799   "*A hook run just before generating the summary buffer.
800 This hook is commonly used to customize threading variables and the
801 like."
802   :group 'gnus-summary-various
803   :type 'hook)
804
805 (defcustom gnus-select-group-hook nil
806   "*A hook called when a newsgroup is selected.
807
808 If you'd like to simplify subjects like the
809 `gnus-summary-next-same-subject' command does, you can use the
810 following hook:
811
812  (add-hook gnus-select-group-hook
813            (lambda ()
814              (mapcar (lambda (header)
815                        (mail-header-set-subject
816                         header
817                         (gnus-simplify-subject
818                          (mail-header-subject header) 're-only)))
819                      gnus-newsgroup-headers)))"
820   :group 'gnus-group-select
821   :type 'hook)
822
823 (defcustom gnus-select-article-hook nil
824   "*A hook called when an article is selected."
825   :group 'gnus-summary-choose
826   :options '(gnus-agent-fetch-selected-article)
827   :type 'hook)
828
829 (defcustom gnus-visual-mark-article-hook
830   (list 'gnus-highlight-selected-summary)
831   "*Hook run after selecting an article in the summary buffer.
832 It is meant to be used for highlighting the article in some way.  It
833 is not run if `gnus-visual' is nil."
834   :group 'gnus-summary-visual
835   :type 'hook)
836
837 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
838   "*A hook called before parsing the headers."
839   :group 'gnus-various
840   :type 'hook)
841
842 (defcustom gnus-exit-group-hook nil
843   "*A hook called when exiting summary mode.
844 This hook is not called from the non-updating exit commands like `Q'."
845   :group 'gnus-various
846   :type 'hook)
847
848 (defcustom gnus-summary-update-hook
849   (list 'gnus-summary-highlight-line)
850   "*A hook called when a summary line is changed.
851 The hook will not be called if `gnus-visual' is nil.
852
853 The default function `gnus-summary-highlight-line' will
854 highlight the line according to the `gnus-summary-highlight'
855 variable."
856   :group 'gnus-summary-visual
857   :type 'hook)
858
859 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
860   "*A hook called when an article is selected for the first time.
861 The hook is intended to mark an article as read (or unread)
862 automatically when it is selected."
863   :group 'gnus-summary-choose
864   :type 'hook)
865
866 (defcustom gnus-group-no-more-groups-hook nil
867   "*A hook run when returning to group mode having no more (unread) groups."
868   :group 'gnus-group-select
869   :type 'hook)
870
871 (defcustom gnus-ps-print-hook nil
872   "*A hook run before ps-printing something from Gnus."
873   :group 'gnus-summary
874   :type 'hook)
875
876 (defcustom gnus-summary-display-arrow
877   (and (fboundp 'display-graphic-p)
878        (display-graphic-p))
879   "*If non-nil, display an arrow highlighting the current article."
880   :version "21.1"
881   :group 'gnus-summary
882   :type 'boolean)
883
884 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
885   "Face used for highlighting the current article in the summary buffer."
886   :group 'gnus-summary-visual
887   :type 'face)
888
889 (defvar gnus-tmp-downloaded nil)
890
891 (defcustom gnus-summary-highlight
892   '(((eq mark gnus-canceled-mark)
893      . gnus-summary-cancelled-face)
894     ((and uncached (> score default-high))
895      . gnus-summary-high-undownloaded-face)
896     ((and uncached (< score default-low))
897      . gnus-summary-low-undownloaded-face)
898     (uncached
899      . gnus-summary-normal-undownloaded-face)
900     ((and (> score default-high)
901           (or (eq mark gnus-dormant-mark)
902               (eq mark gnus-ticked-mark)))
903      . gnus-summary-high-ticked-face)
904     ((and (< score default-low)
905           (or (eq mark gnus-dormant-mark)
906               (eq mark gnus-ticked-mark)))
907      . gnus-summary-low-ticked-face)
908     ((or (eq mark gnus-dormant-mark)
909          (eq mark gnus-ticked-mark))
910      . gnus-summary-normal-ticked-face)
911     ((and (> score default-high) (eq mark gnus-ancient-mark))
912      . gnus-summary-high-ancient-face)
913     ((and (< score default-low) (eq mark gnus-ancient-mark))
914      . gnus-summary-low-ancient-face)
915     ((eq mark gnus-ancient-mark)
916      . gnus-summary-normal-ancient-face)
917     ((and (> score default-high) (eq mark gnus-unread-mark))
918      . gnus-summary-high-unread-face)
919     ((and (< score default-low) (eq mark gnus-unread-mark))
920      . gnus-summary-low-unread-face)
921     ((eq mark gnus-unread-mark)
922      . gnus-summary-normal-unread-face)
923     ((> score default-high)
924      . gnus-summary-high-read-face)
925     ((< score default-low)
926      . gnus-summary-low-read-face)
927     (t
928      . gnus-summary-normal-read-face))
929   "*Controls the highlighting of summary buffer lines.
930
931 A list of (FORM . FACE) pairs.  When deciding how a a particular
932 summary line should be displayed, each form is evaluated.  The content
933 of the face field after the first true form is used.  You can change
934 how those summary lines are displayed, by editing the face field.
935
936 You can use the following variables in the FORM field.
937
938 score:        The article's score
939 default:      The default article score.
940 default-high: The default score for high scored articles.
941 default-low:  The default score for low scored articles.
942 below:        The score below which articles are automatically marked as read.
943 mark:         The articles mark."
944   :group 'gnus-summary-visual
945   :type '(repeat (cons (sexp :tag "Form" nil)
946                        face)))
947
948 (defcustom gnus-alter-header-function nil
949   "Function called to allow alteration of article header structures.
950 The function is called with one parameter, the article header vector,
951 which it may alter in any way."
952   :type '(choice (const :tag "None" nil)
953                  function)
954   :group 'gnus-summary)
955
956 (defvar gnus-decode-encoded-word-function
957   (mime-find-field-decoder 'From 'nov)
958   "Variable that says which function should be used to decode a string with encoded words.")
959
960 (defcustom gnus-extra-headers '(To Newsgroups)
961   "*Extra headers to parse."
962   :version "21.1"
963   :group 'gnus-summary
964   :type '(repeat symbol))
965
966 (defcustom gnus-ignored-from-addresses
967   (and user-mail-address (regexp-quote user-mail-address))
968   "*Regexp of From headers that may be suppressed in favor of To headers."
969   :version "21.1"
970   :group 'gnus-summary
971   :type 'regexp)
972
973 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
974   "List of charsets that should be ignored.
975 When these charsets are used in the \"charset\" parameter, the
976 default charset will be used instead."
977   :version "21.1"
978   :type '(repeat symbol)
979   :group 'gnus-charset)
980
981 (gnus-define-group-parameter
982  ignored-charsets
983  :type list
984  :function-document
985  "Return the ignored charsets of GROUP."
986  :variable gnus-group-ignored-charsets-alist
987  :variable-default
988  '(("alt\\.chinese\\.text" iso-8859-1))
989  :variable-document
990  "Alist of regexps (to match group names) and charsets that should be ignored.
991 When these charsets are used in the \"charset\" parameter, the
992 default charset will be used instead."
993  :variable-group gnus-charset
994  :variable-type '(repeat (cons (regexp :tag "Group")
995                                (repeat symbol)))
996  :parameter-type '(choice :tag "Ignored charsets"
997                           :value nil
998                           (repeat (symbol)))
999  :parameter-document       "\
1000 List of charsets that should be ignored.
1001
1002 When these charsets are used in the \"charset\" parameter, the
1003 default charset will be used instead.")
1004
1005 (defcustom gnus-group-highlight-words-alist nil
1006   "Alist of group regexps and highlight regexps.
1007 This variable uses the same syntax as `gnus-emphasis-alist'."
1008   :version "21.1"
1009   :type '(repeat (cons (regexp :tag "Group")
1010                        (repeat (list (regexp :tag "Highlight regexp")
1011                                      (number :tag "Group for entire word" 0)
1012                                      (number :tag "Group for displayed part" 0)
1013                                      (symbol :tag "Face"
1014                                              gnus-emphasis-highlight-words)))))
1015   :group 'gnus-summary-visual)
1016
1017 (defcustom gnus-use-wheel nil
1018   "Use Intelli-mouse on summary movement"
1019   :type 'boolean
1020   :group 'gnus-summary-maneuvering)
1021
1022 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1023   "Amount to scroll messages by spinning the mouse wheel.
1024 This is actually a cons cell, where the first item is the amount to scroll
1025 on a normal wheel event, and the second is the amount to scroll when the
1026 wheel is moved with the shift key depressed."
1027   :type '(cons (integer :tag "Shift") integer)
1028   :group 'gnus-summary-maneuvering)
1029
1030 (defcustom gnus-wheel-edge-resistance 2
1031   "How hard it should be to change the current article
1032 by moving the mouse over the edge of the article window."
1033   :type 'integer
1034   :group 'gnus-summary-maneuvering)
1035
1036 (defcustom gnus-summary-show-article-charset-alist
1037   nil
1038   "Alist of number and charset.
1039 The article will be shown with the charset corresponding to the
1040 numbered argument.
1041 For example: ((1 . cn-gb-2312) (2 . big5))."
1042   :version "21.1"
1043   :type '(repeat (cons (number :tag "Argument" 1)
1044                        (symbol :tag "Charset")))
1045   :group 'gnus-charset)
1046
1047 (defcustom gnus-preserve-marks t
1048   "Whether marks are preserved when moving, copying and respooling messages."
1049   :version "21.1"
1050   :type 'boolean
1051   :group 'gnus-summary-marks)
1052
1053 (defcustom gnus-alter-articles-to-read-function nil
1054   "Function to be called to alter the list of articles to be selected."
1055   :type '(choice (const nil) function)
1056   :group 'gnus-summary)
1057
1058 (defcustom gnus-orphan-score nil
1059   "*All orphans get this score added.  Set in the score file."
1060   :group 'gnus-score-default
1061   :type '(choice (const nil)
1062                  integer))
1063
1064 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1065   "*A regexp to match MIME parts when saving multiple parts of a message
1066 with gnus-summary-save-parts (X m). This regexp will be used by default
1067 when prompting the user for which type of files to save."
1068   :group 'gnus-summary
1069   :type 'regexp)
1070
1071 (defcustom gnus-read-all-available-headers nil
1072   "Whether Gnus should parse all headers made available to it.
1073 This is mostly relevant for slow backends where the user may
1074 wish to widen the summary buffer to include all headers
1075 that were fetched.  Say, for nnultimate groups."
1076   :group 'gnus-summary
1077   :type '(choice boolean regexp))
1078
1079 (defcustom gnus-summary-muttprint-program "muttprint"
1080   "Command (and optional arguments) used to run Muttprint."
1081   :version "21.3"
1082   :group 'gnus-summary
1083   :type 'string)
1084
1085 (defcustom gnus-article-loose-mime nil
1086   "If non-nil, don't require MIME-Version header.
1087 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1088 supply the MIME-Version header or deliberately strip it From the mail.
1089 Set it to non-nil, Gnus will treat some articles as MIME even if
1090 the MIME-Version header is missed."
1091   :version "21.3"
1092   :type 'boolean
1093   :group 'gnus-article)
1094
1095 (defcustom gnus-article-emulate-mime t
1096   "If non-nil, use MIME emulation for uuencode and the like.
1097 This means that Gnus will search message bodies for text that look
1098 like uuencoded bits, yEncoded bits, and so on, and present that using
1099 the normal Gnus MIME machinery."
1100   :type 'boolean
1101   :group 'gnus-article)
1102
1103 ;;; Internal variables
1104
1105 (defvar gnus-summary-display-cache nil)
1106 (defvar gnus-article-mime-handles nil)
1107 (defvar gnus-article-decoded-p nil)
1108 (defvar gnus-article-charset nil)
1109 (defvar gnus-article-ignored-charsets nil)
1110 (defvar gnus-scores-exclude-files nil)
1111 (defvar gnus-page-broken nil)
1112
1113 (defvar gnus-original-article nil)
1114 (defvar gnus-article-internal-prepare-hook nil)
1115 (defvar gnus-newsgroup-process-stack nil)
1116
1117 (defvar gnus-thread-indent-array nil)
1118 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1119 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1120   "Function called to sort the articles within a thread after it has been gathered together.")
1121
1122 (defvar gnus-summary-save-parts-type-history nil)
1123 (defvar gnus-summary-save-parts-last-directory nil)
1124
1125 ;; Avoid highlighting in kill files.
1126 (defvar gnus-summary-inhibit-highlight nil)
1127 (defvar gnus-newsgroup-selected-overlay nil)
1128 (defvar gnus-inhibit-limiting nil)
1129 (defvar gnus-newsgroup-adaptive-score-file nil)
1130 (defvar gnus-current-score-file nil)
1131 (defvar gnus-current-move-group nil)
1132 (defvar gnus-current-copy-group nil)
1133 (defvar gnus-current-crosspost-group nil)
1134 (defvar gnus-newsgroup-display nil)
1135
1136 (defvar gnus-newsgroup-dependencies nil)
1137 (defvar gnus-newsgroup-adaptive nil)
1138 (defvar gnus-summary-display-article-function nil)
1139 (defvar gnus-summary-highlight-line-function nil
1140   "Function called after highlighting a summary line.")
1141
1142 (defvar gnus-summary-line-format-alist
1143   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1144     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1145     (?s gnus-tmp-subject-or-nil ?s)
1146     (?n gnus-tmp-name ?s)
1147     (?A (std11-address-string
1148          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1149     (?a (or (std11-full-name-string
1150              (car (mime-entity-read-field gnus-tmp-header 'From)))
1151             gnus-tmp-from) ?s)
1152     (?F gnus-tmp-from ?s)
1153     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1154     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1155     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1156     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1157     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1158     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1159     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1160     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1161     (?L gnus-tmp-lines ?s)
1162     (?O gnus-tmp-downloaded ?c)
1163     (?I gnus-tmp-indentation ?s)
1164     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1165     (?R gnus-tmp-replied ?c)
1166     (?\[ gnus-tmp-opening-bracket ?c)
1167     (?\] gnus-tmp-closing-bracket ?c)
1168     (?\> (make-string gnus-tmp-level ? ) ?s)
1169     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1170     (?i gnus-tmp-score ?d)
1171     (?z gnus-tmp-score-char ?c)
1172     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1173     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1174     (?U gnus-tmp-unread ?c)
1175     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1176         ?s)
1177     (?t (gnus-summary-number-of-articles-in-thread
1178          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1179         ?d)
1180     (?e (gnus-summary-number-of-articles-in-thread
1181          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1182         ?c)
1183     (?u gnus-tmp-user-defined ?s)
1184     (?P (gnus-pick-line-number) ?d)
1185     (?B gnus-tmp-thread-tree-header-string ?s)
1186     (user-date (gnus-user-date
1187                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1188   "An alist of format specifications that can appear in summary lines.
1189 These are paired with what variables they correspond with, along with
1190 the type of the variable (string, integer, character, etc).")
1191
1192 (defvar gnus-summary-dummy-line-format-alist
1193   `((?S gnus-tmp-subject ?s)
1194     (?N gnus-tmp-number ?d)
1195     (?u gnus-tmp-user-defined ?s)))
1196
1197 (defvar gnus-summary-mode-line-format-alist
1198   `((?G gnus-tmp-group-name ?s)
1199     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1200     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1201     (?A gnus-tmp-article-number ?d)
1202     (?Z gnus-tmp-unread-and-unselected ?s)
1203     (?V gnus-version ?s)
1204     (?U gnus-tmp-unread-and-unticked ?d)
1205     (?S gnus-tmp-subject ?s)
1206     (?e gnus-tmp-unselected ?d)
1207     (?u gnus-tmp-user-defined ?s)
1208     (?d (length gnus-newsgroup-dormant) ?d)
1209     (?t (length gnus-newsgroup-marked) ?d)
1210     (?h (length gnus-newsgroup-spam-marked) ?d)
1211     (?r (length gnus-newsgroup-reads) ?d)
1212     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1213     (?E gnus-newsgroup-expunged-tally ?d)
1214     (?s (gnus-current-score-file-nondirectory) ?s)))
1215
1216 (defvar gnus-last-search-regexp nil
1217   "Default regexp for article search command.")
1218
1219 (defvar gnus-summary-search-article-matched-data nil
1220   "Last matched data of article search command.  It is the local variable
1221 in `gnus-article-buffer' which consists of the list of start position,
1222 end position and text.")
1223
1224 (defvar gnus-last-shell-command nil
1225   "Default shell command on article.")
1226
1227 (defvar gnus-newsgroup-agentized nil
1228   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1229 (defvar gnus-newsgroup-begin nil)
1230 (defvar gnus-newsgroup-end nil)
1231 (defvar gnus-newsgroup-last-rmail nil)
1232 (defvar gnus-newsgroup-last-mail nil)
1233 (defvar gnus-newsgroup-last-folder nil)
1234 (defvar gnus-newsgroup-last-file nil)
1235 (defvar gnus-newsgroup-auto-expire nil)
1236 (defvar gnus-newsgroup-active nil)
1237
1238 (defvar gnus-newsgroup-data nil)
1239 (defvar gnus-newsgroup-data-reverse nil)
1240 (defvar gnus-newsgroup-limit nil)
1241 (defvar gnus-newsgroup-limits nil)
1242
1243 (defvar gnus-newsgroup-unreads nil
1244   "Sorted list of unread articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-unselected nil
1247   "Sorted list of unselected unread articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-reads nil
1250   "Alist of read articles and article marks in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-expunged-tally nil)
1253
1254 (defvar gnus-newsgroup-marked nil
1255   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1256
1257 (defvar gnus-newsgroup-spam-marked nil
1258   "List of ranges of articles that have been marked as spam.")
1259
1260 (defvar gnus-newsgroup-killed nil
1261   "List of ranges of articles that have been through the scoring process.")
1262
1263 (defvar gnus-newsgroup-cached nil
1264   "Sorted list of articles that come from the article cache.")
1265
1266 (defvar gnus-newsgroup-saved nil
1267   "List of articles that have been saved.")
1268
1269 (defvar gnus-newsgroup-kill-headers nil)
1270
1271 (defvar gnus-newsgroup-replied nil
1272   "List of articles that have been replied to in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-forwarded nil
1275   "List of articles that have been forwarded in the current newsgroup.")
1276
1277 (defvar gnus-newsgroup-recent nil
1278   "List of articles that have are recent in the current newsgroup.")
1279
1280 (defvar gnus-newsgroup-expirable nil
1281   "Sorted list of articles in the current newsgroup that can be expired.")
1282
1283 (defvar gnus-newsgroup-processable nil
1284   "List of articles in the current newsgroup that can be processed.")
1285
1286 (defvar gnus-newsgroup-downloadable nil
1287   "Sorted list of articles in the current newsgroup that can be processed.")
1288
1289 (defvar gnus-newsgroup-undownloaded nil
1290   "List of articles in the current newsgroup that haven't been downloaded..")
1291
1292 (defvar gnus-newsgroup-unsendable nil
1293   "List of articles in the current newsgroup that won't be sent.")
1294
1295 (defvar gnus-newsgroup-bookmarks nil
1296   "List of articles in the current newsgroup that have bookmarks.")
1297
1298 (defvar gnus-newsgroup-dormant nil
1299   "Sorted list of dormant articles in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-unseen nil
1302   "List of unseen articles in the current newsgroup.")
1303
1304 (defvar gnus-newsgroup-seen nil
1305   "Range of seen articles in the current newsgroup.")
1306
1307 (defvar gnus-newsgroup-articles nil
1308   "List of articles in the current newsgroup.")
1309
1310 (defvar gnus-newsgroup-scored nil
1311   "List of scored articles in the current newsgroup.")
1312
1313 (defvar gnus-newsgroup-incorporated nil
1314   "List of incorporated articles in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-headers nil
1317   "List of article headers in the current newsgroup.")
1318
1319 (defvar gnus-newsgroup-threads nil)
1320
1321 (defvar gnus-newsgroup-prepared nil
1322   "Whether the current group has been prepared properly.")
1323
1324 (defvar gnus-newsgroup-ancient nil
1325   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1326
1327 (defvar gnus-newsgroup-sparse nil)
1328
1329 (defvar gnus-current-article nil)
1330 (defvar gnus-article-current nil)
1331 (defvar gnus-current-headers nil)
1332 (defvar gnus-have-all-headers nil)
1333 (defvar gnus-last-article nil)
1334 (defvar gnus-newsgroup-history nil)
1335 (defvar gnus-newsgroup-charset nil)
1336 (defvar gnus-newsgroup-ephemeral-charset nil)
1337 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1338
1339 (defvar gnus-article-before-search nil)
1340
1341 (defconst gnus-summary-local-variables
1342   '(gnus-newsgroup-name
1343     gnus-newsgroup-begin gnus-newsgroup-end
1344     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1345     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1346     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1347     gnus-newsgroup-unselected gnus-newsgroup-marked
1348     gnus-newsgroup-spam-marked
1349     gnus-newsgroup-reads gnus-newsgroup-saved
1350     gnus-newsgroup-replied gnus-newsgroup-forwarded
1351     gnus-newsgroup-recent
1352     gnus-newsgroup-expirable
1353     gnus-newsgroup-processable gnus-newsgroup-killed
1354     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1355     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1356     gnus-newsgroup-seen gnus-newsgroup-articles
1357     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1358     gnus-newsgroup-headers gnus-newsgroup-threads
1359     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1360     gnus-current-article gnus-current-headers gnus-have-all-headers
1361     gnus-last-article gnus-article-internal-prepare-hook
1362     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1363     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1364     gnus-thread-expunge-below
1365     gnus-score-alist gnus-current-score-file
1366     (gnus-summary-expunge-below . global)
1367     (gnus-summary-mark-below . global)
1368     (gnus-orphan-score . global)
1369     gnus-newsgroup-active gnus-scores-exclude-files
1370     gnus-newsgroup-history gnus-newsgroup-ancient
1371     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1372     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1373     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1374     (gnus-newsgroup-expunged-tally . 0)
1375     gnus-cache-removable-articles gnus-newsgroup-cached
1376     gnus-newsgroup-data gnus-newsgroup-data-reverse
1377     gnus-newsgroup-limit gnus-newsgroup-limits
1378     gnus-newsgroup-charset gnus-newsgroup-display
1379     gnus-newsgroup-incorporated)
1380   "Variables that are buffer-local to the summary buffers.")
1381
1382 (defvar gnus-newsgroup-variables nil
1383   "A list of variables that have separate values in different newsgroups.
1384 A list of newsgroup (summary buffer) local variables, or cons of
1385 variables and their default values (when the default values are not
1386 nil), that should be made global while the summary buffer is active.
1387 These variables can be used to set variables in the group parameters
1388 while still allowing them to affect operations done in other
1389 buffers. For example:
1390
1391 \(setq gnus-newsgroup-variables
1392      '(message-use-followup-to
1393        (gnus-visible-headers .
1394          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1395 ")
1396
1397 ;; Byte-compiler warning.
1398 ;(eval-when-compile (defvar gnus-article-mode-map))
1399 (eval-when-compile
1400   (let ((features (cons 'gnus-sum features)))
1401     (require 'gnus)
1402     (require 'gnus-agent)
1403     (require 'gnus-art)))
1404
1405 ;; Subject simplification.
1406
1407 (defun gnus-simplify-whitespace (str)
1408   "Remove excessive whitespace from STR."
1409   (let ((mystr str))
1410     ;; Multiple spaces.
1411     (while (string-match "[ \t][ \t]+" mystr)
1412       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1413                           " "
1414                           (substring mystr (match-end 0)))))
1415     ;; Leading spaces.
1416     (when (string-match "^[ \t]+" mystr)
1417       (setq mystr (substring mystr (match-end 0))))
1418     ;; Trailing spaces.
1419     (when (string-match "[ \t]+$" mystr)
1420       (setq mystr (substring mystr 0 (match-beginning 0))))
1421     mystr))
1422
1423 (defun gnus-simplify-all-whitespace (str)
1424   "Remove all whitespace from STR."
1425   (let ((mystr str))
1426     (while (string-match "[ \t\n]+" mystr)
1427       (setq mystr (replace-match "" nil nil mystr)))
1428     mystr))
1429
1430 (defsubst gnus-simplify-subject-re (subject)
1431   "Remove \"Re:\" from subject lines."
1432   (if (string-match message-subject-re-regexp subject)
1433       (substring subject (match-end 0))
1434     subject))
1435
1436 (defun gnus-simplify-subject (subject &optional re-only)
1437   "Remove `Re:' and words in parentheses.
1438 If RE-ONLY is non-nil, strip leading `Re:'s only."
1439   (let ((case-fold-search t))           ;Ignore case.
1440     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1441     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1442       (setq subject (substring subject (match-end 0))))
1443     ;; Remove uninteresting prefixes.
1444     (when (and (not re-only)
1445                gnus-simplify-ignored-prefixes
1446                (string-match gnus-simplify-ignored-prefixes subject))
1447       (setq subject (substring subject (match-end 0))))
1448     ;; Remove words in parentheses from end.
1449     (unless re-only
1450       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1451         (setq subject (substring subject 0 (match-beginning 0)))))
1452     ;; Return subject string.
1453     subject))
1454
1455 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1456 ;; all whitespace.
1457 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1458   (goto-char (point-min))
1459   (while (re-search-forward regexp nil t)
1460     (replace-match (or newtext ""))))
1461
1462 (defun gnus-simplify-buffer-fuzzy ()
1463   "Simplify string in the buffer fuzzily.
1464 The string in the accessible portion of the current buffer is simplified.
1465 It is assumed to be a single-line subject.
1466 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1467 matter is removed.  Additional things can be deleted by setting
1468 `gnus-simplify-subject-fuzzy-regexp'."
1469   (let ((case-fold-search t)
1470         (modified-tick))
1471     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1472
1473     (while (not (eq modified-tick (buffer-modified-tick)))
1474       (setq modified-tick (buffer-modified-tick))
1475       (cond
1476        ((listp gnus-simplify-subject-fuzzy-regexp)
1477         (mapcar 'gnus-simplify-buffer-fuzzy-step
1478                 gnus-simplify-subject-fuzzy-regexp))
1479        (gnus-simplify-subject-fuzzy-regexp
1480         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1481       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1482       (gnus-simplify-buffer-fuzzy-step
1483        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1484       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1485
1486     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1487     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1488     (gnus-simplify-buffer-fuzzy-step " $")
1489     (gnus-simplify-buffer-fuzzy-step "^ +")))
1490
1491 (defun gnus-simplify-subject-fuzzy (subject)
1492   "Simplify a subject string fuzzily.
1493 See `gnus-simplify-buffer-fuzzy' for details."
1494   (save-excursion
1495     (gnus-set-work-buffer)
1496     (let ((case-fold-search t))
1497       ;; Remove uninteresting prefixes.
1498       (when (and gnus-simplify-ignored-prefixes
1499                  (string-match gnus-simplify-ignored-prefixes subject))
1500         (setq subject (substring subject (match-end 0))))
1501       (insert subject)
1502       (inline (gnus-simplify-buffer-fuzzy))
1503       (buffer-string))))
1504
1505 (defsubst gnus-simplify-subject-fully (subject)
1506   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1507   (cond
1508    (gnus-simplify-subject-functions
1509     (gnus-map-function gnus-simplify-subject-functions subject))
1510    ((null gnus-summary-gather-subject-limit)
1511     (gnus-simplify-subject-re subject))
1512    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1513     (gnus-simplify-subject-fuzzy subject))
1514    ((numberp gnus-summary-gather-subject-limit)
1515     (gnus-limit-string (gnus-simplify-subject-re subject)
1516                        gnus-summary-gather-subject-limit))
1517    (t
1518     subject)))
1519
1520 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1521   "Check whether two subjects are equal.
1522 If optional argument simple-first is t, first argument is already
1523 simplified."
1524   (cond
1525    ((null simple-first)
1526     (equal (gnus-simplify-subject-fully s1)
1527            (gnus-simplify-subject-fully s2)))
1528    (t
1529     (equal s1
1530            (gnus-simplify-subject-fully s2)))))
1531
1532 (defun gnus-summary-bubble-group ()
1533   "Increase the score of the current group.
1534 This is a handy function to add to `gnus-summary-exit-hook' to
1535 increase the score of each group you read."
1536   (gnus-group-add-score gnus-newsgroup-name))
1537
1538 \f
1539 ;;;
1540 ;;; Gnus summary mode
1541 ;;;
1542
1543 (put 'gnus-summary-mode 'mode-class 'special)
1544
1545 (defvar gnus-article-commands-menu)
1546
1547 ;; Non-orthogonal keys
1548
1549 (gnus-define-keys gnus-summary-mode-map
1550   " " gnus-summary-next-page
1551   "\177" gnus-summary-prev-page
1552   [delete] gnus-summary-prev-page
1553   [backspace] gnus-summary-prev-page
1554   "\r" gnus-summary-scroll-up
1555   "\M-\r" gnus-summary-scroll-down
1556   "n" gnus-summary-next-unread-article
1557   "p" gnus-summary-prev-unread-article
1558   "N" gnus-summary-next-article
1559   "P" gnus-summary-prev-article
1560   "\M-\C-n" gnus-summary-next-same-subject
1561   "\M-\C-p" gnus-summary-prev-same-subject
1562   "\M-n" gnus-summary-next-unread-subject
1563   "\M-p" gnus-summary-prev-unread-subject
1564   "." gnus-summary-first-unread-article
1565   "," gnus-summary-best-unread-article
1566   "\M-s" gnus-summary-search-article-forward
1567   "\M-r" gnus-summary-search-article-backward
1568   "<" gnus-summary-beginning-of-article
1569   ">" gnus-summary-end-of-article
1570   "j" gnus-summary-goto-article
1571   "^" gnus-summary-refer-parent-article
1572   "\M-^" gnus-summary-refer-article
1573   "u" gnus-summary-tick-article-forward
1574   "!" gnus-summary-tick-article-forward
1575   "U" gnus-summary-tick-article-backward
1576   "d" gnus-summary-mark-as-read-forward
1577   "D" gnus-summary-mark-as-read-backward
1578   "E" gnus-summary-mark-as-expirable
1579   "\M-u" gnus-summary-clear-mark-forward
1580   "\M-U" gnus-summary-clear-mark-backward
1581   "k" gnus-summary-kill-same-subject-and-select
1582   "\C-k" gnus-summary-kill-same-subject
1583   "\M-\C-k" gnus-summary-kill-thread
1584   "\M-\C-l" gnus-summary-lower-thread
1585   "e" gnus-summary-edit-article
1586   "#" gnus-summary-mark-as-processable
1587   "\M-#" gnus-summary-unmark-as-processable
1588   "\M-\C-t" gnus-summary-toggle-threads
1589   "\M-\C-s" gnus-summary-show-thread
1590   "\M-\C-h" gnus-summary-hide-thread
1591   "\M-\C-f" gnus-summary-next-thread
1592   "\M-\C-b" gnus-summary-prev-thread
1593   [(meta down)] gnus-summary-next-thread
1594   [(meta up)] gnus-summary-prev-thread
1595   "\M-\C-u" gnus-summary-up-thread
1596   "\M-\C-d" gnus-summary-down-thread
1597   "&" gnus-summary-execute-command
1598   "c" gnus-summary-catchup-and-exit
1599   "\C-w" gnus-summary-mark-region-as-read
1600   "\C-t" gnus-summary-toggle-truncation
1601   "?" gnus-summary-mark-as-dormant
1602   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1603   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1604   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1605   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1606   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1607   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1608   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1609   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1610   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1611   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1612   "=" gnus-summary-expand-window
1613   "\C-x\C-s" gnus-summary-reselect-current-group
1614   "\M-g" gnus-summary-rescan-group
1615   "w" gnus-summary-stop-page-breaking
1616   "\C-c\C-r" gnus-summary-caesar-message
1617   "\M-t" gnus-summary-toggle-mime
1618   "f" gnus-summary-followup
1619   "F" gnus-summary-followup-with-original
1620   "C" gnus-summary-cancel-article
1621   "r" gnus-summary-reply
1622   "R" gnus-summary-reply-with-original
1623   "\C-c\C-f" gnus-summary-mail-forward
1624   "o" gnus-summary-save-article
1625   "\C-o" gnus-summary-save-article-mail
1626   "|" gnus-summary-pipe-output
1627   "\M-k" gnus-summary-edit-local-kill
1628   "\M-K" gnus-summary-edit-global-kill
1629   ;; "V" gnus-version
1630   "\C-c\C-d" gnus-summary-describe-group
1631   "q" gnus-summary-exit
1632   "Q" gnus-summary-exit-no-update
1633   "\C-c\C-i" gnus-info-find-node
1634   gnus-mouse-2 gnus-mouse-pick-article
1635   "m" gnus-summary-mail-other-window
1636   "a" gnus-summary-post-news
1637   "i" gnus-summary-news-other-window
1638   "x" gnus-summary-limit-to-unread
1639   "s" gnus-summary-isearch-article
1640   "t" gnus-summary-toggle-header
1641   "g" gnus-summary-show-article
1642   "l" gnus-summary-goto-last-article
1643   "v" gnus-summary-preview-mime-message
1644   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1645   "\C-d" gnus-summary-enter-digest-group
1646   "\M-\C-d" gnus-summary-read-document
1647   "\M-\C-e" gnus-summary-edit-parameters
1648   "\M-\C-a" gnus-summary-customize-parameters
1649   "\C-c\C-b" gnus-bug
1650   "\C-c\C-n" gnus-namazu-search
1651   "*" gnus-cache-enter-article
1652   "\M-*" gnus-cache-remove-article
1653   "\M-&" gnus-summary-universal-argument
1654   "\C-l" gnus-recenter
1655   "I" gnus-summary-increase-score
1656   "L" gnus-summary-lower-score
1657   "\M-i" gnus-symbolic-argument
1658   "h" gnus-summary-select-article-buffer
1659
1660   "V" gnus-summary-score-map
1661   "X" gnus-uu-extract-map
1662   "S" gnus-summary-send-map)
1663
1664   ;; Sort of orthogonal keymap
1665 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1666   "t" gnus-summary-tick-article-forward
1667   "!" gnus-summary-tick-article-forward
1668   "d" gnus-summary-mark-as-read-forward
1669   "r" gnus-summary-mark-as-read-forward
1670   "c" gnus-summary-clear-mark-forward
1671   " " gnus-summary-clear-mark-forward
1672   "e" gnus-summary-mark-as-expirable
1673   "x" gnus-summary-mark-as-expirable
1674   "?" gnus-summary-mark-as-dormant
1675   "b" gnus-summary-set-bookmark
1676   "B" gnus-summary-remove-bookmark
1677   "#" gnus-summary-mark-as-processable
1678   "\M-#" gnus-summary-unmark-as-processable
1679   "S" gnus-summary-limit-include-expunged
1680   "C" gnus-summary-catchup
1681   "H" gnus-summary-catchup-to-here
1682   "h" gnus-summary-catchup-from-here
1683   "\C-c" gnus-summary-catchup-all
1684   "k" gnus-summary-kill-same-subject-and-select
1685   "K" gnus-summary-kill-same-subject
1686   "P" gnus-uu-mark-map)
1687
1688 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1689   "c" gnus-summary-clear-above
1690   "u" gnus-summary-tick-above
1691   "m" gnus-summary-mark-above
1692   "k" gnus-summary-kill-below)
1693
1694 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1695   "/" gnus-summary-limit-to-subject
1696   "n" gnus-summary-limit-to-articles
1697   "w" gnus-summary-pop-limit
1698   "s" gnus-summary-limit-to-subject
1699   "a" gnus-summary-limit-to-author
1700   "u" gnus-summary-limit-to-unread
1701   "m" gnus-summary-limit-to-marks
1702   "M" gnus-summary-limit-exclude-marks
1703   "v" gnus-summary-limit-to-score
1704   "*" gnus-summary-limit-include-cached
1705   "D" gnus-summary-limit-include-dormant
1706   "T" gnus-summary-limit-include-thread
1707   "d" gnus-summary-limit-exclude-dormant
1708   "t" gnus-summary-limit-to-age
1709   "." gnus-summary-limit-to-unseen
1710   "x" gnus-summary-limit-to-extra
1711   "p" gnus-summary-limit-to-display-predicate
1712   "E" gnus-summary-limit-include-expunged
1713   "c" gnus-summary-limit-exclude-childless-dormant
1714   "C" gnus-summary-limit-mark-excluded-as-read
1715   "o" gnus-summary-insert-old-articles
1716   "N" gnus-summary-insert-new-articles)
1717
1718 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1719   "n" gnus-summary-next-unread-article
1720   "p" gnus-summary-prev-unread-article
1721   "N" gnus-summary-next-article
1722   "P" gnus-summary-prev-article
1723   "\C-n" gnus-summary-next-same-subject
1724   "\C-p" gnus-summary-prev-same-subject
1725   "\M-n" gnus-summary-next-unread-subject
1726   "\M-p" gnus-summary-prev-unread-subject
1727   "f" gnus-summary-first-unread-article
1728   "b" gnus-summary-best-unread-article
1729   "j" gnus-summary-goto-article
1730   "g" gnus-summary-goto-subject
1731   "l" gnus-summary-goto-last-article
1732   "o" gnus-summary-pop-article)
1733
1734 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1735   "k" gnus-summary-kill-thread
1736   "l" gnus-summary-lower-thread
1737   "i" gnus-summary-raise-thread
1738   "T" gnus-summary-toggle-threads
1739   "t" gnus-summary-rethread-current
1740   "^" gnus-summary-reparent-thread
1741   "s" gnus-summary-show-thread
1742   "S" gnus-summary-show-all-threads
1743   "h" gnus-summary-hide-thread
1744   "H" gnus-summary-hide-all-threads
1745   "n" gnus-summary-next-thread
1746   "p" gnus-summary-prev-thread
1747   "u" gnus-summary-up-thread
1748   "o" gnus-summary-top-thread
1749   "d" gnus-summary-down-thread
1750   "#" gnus-uu-mark-thread
1751   "\M-#" gnus-uu-unmark-thread)
1752
1753 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1754   "g" gnus-summary-prepare
1755   "c" gnus-summary-insert-cached-articles
1756   "d" gnus-summary-insert-dormant-articles)
1757
1758 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1759   "c" gnus-summary-catchup-and-exit
1760   "C" gnus-summary-catchup-all-and-exit
1761   "E" gnus-summary-exit-no-update
1762   "J" gnus-summary-jump-to-other-group
1763   "Q" gnus-summary-exit
1764   "Z" gnus-summary-exit
1765   "n" gnus-summary-catchup-and-goto-next-group
1766   "R" gnus-summary-reselect-current-group
1767   "G" gnus-summary-rescan-group
1768   "N" gnus-summary-next-group
1769   "s" gnus-summary-save-newsrc
1770   "P" gnus-summary-prev-group)
1771
1772 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1773   " " gnus-summary-next-page
1774   "n" gnus-summary-next-page
1775   "\177" gnus-summary-prev-page
1776   [delete] gnus-summary-prev-page
1777   "p" gnus-summary-prev-page
1778   "\r" gnus-summary-scroll-up
1779   "\M-\r" gnus-summary-scroll-down
1780   "<" gnus-summary-beginning-of-article
1781   ">" gnus-summary-end-of-article
1782   "b" gnus-summary-beginning-of-article
1783   "e" gnus-summary-end-of-article
1784   "^" gnus-summary-refer-parent-article
1785   "r" gnus-summary-refer-parent-article
1786   "D" gnus-summary-enter-digest-group
1787   "R" gnus-summary-refer-references
1788   "T" gnus-summary-refer-thread
1789   "g" gnus-summary-show-article
1790   "s" gnus-summary-isearch-article
1791   "P" gnus-summary-print-article
1792   "M" gnus-mailing-list-insinuate
1793   "t" gnus-article-babel)
1794
1795 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1796   "b" gnus-article-add-buttons
1797   "B" gnus-article-add-buttons-to-head
1798   "o" gnus-article-treat-overstrike
1799   "e" gnus-article-emphasize
1800   "w" gnus-article-fill-cited-article
1801   "Q" gnus-article-fill-long-lines
1802   "C" gnus-article-capitalize-sentences
1803   "c" gnus-article-remove-cr
1804   "Z" gnus-article-decode-HZ
1805   "h" gnus-article-wash-html
1806   "u" gnus-article-unsplit-urls
1807   "f" gnus-article-display-x-face
1808   "l" gnus-summary-stop-page-breaking
1809   "r" gnus-summary-caesar-message
1810   "m" gnus-summary-morse-message
1811   "t" gnus-summary-toggle-header
1812   "g" gnus-treat-smiley
1813   "v" gnus-summary-verbose-headers
1814   "m" gnus-summary-toggle-mime
1815   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1816   "p" gnus-article-verify-x-pgp-sig
1817   "d" gnus-article-treat-dumbquotes)
1818
1819 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1820   ;; mnemonic: deuglif*Y*
1821   "u" gnus-article-outlook-unwrap-lines
1822   "a" gnus-article-outlook-repair-attribution
1823   "c" gnus-article-outlook-rearrange-citation
1824   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1825
1826 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1827   "a" gnus-article-hide
1828   "h" gnus-article-hide-headers
1829   "b" gnus-article-hide-boring-headers
1830   "s" gnus-article-hide-signature
1831   "c" gnus-article-hide-citation
1832   "C" gnus-article-hide-citation-in-followups
1833   "l" gnus-article-hide-list-identifiers
1834   "p" gnus-article-hide-pgp
1835   "B" gnus-article-strip-banner
1836   "P" gnus-article-hide-pem
1837   "\C-c" gnus-article-hide-citation-maybe)
1838
1839 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1840   "a" gnus-article-highlight
1841   "h" gnus-article-highlight-headers
1842   "c" gnus-article-highlight-citation
1843   "s" gnus-article-highlight-signature)
1844
1845 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1846   "f" gnus-article-treat-fold-headers
1847   "u" gnus-article-treat-unfold-headers
1848   "n" gnus-article-treat-fold-newsgroups)
1849
1850 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1851   "x" gnus-article-display-x-face
1852   "d" gnus-article-display-face
1853   "s" gnus-treat-smiley
1854   "D" gnus-article-remove-images
1855   "f" gnus-treat-from-picon
1856   "m" gnus-treat-mail-picon
1857   "n" gnus-treat-newsgroups-picon)
1858
1859 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1860   "z" gnus-article-date-ut
1861   "u" gnus-article-date-ut
1862   "l" gnus-article-date-local
1863   "p" gnus-article-date-english
1864   "e" gnus-article-date-lapsed
1865   "o" gnus-article-date-original
1866   "i" gnus-article-date-iso8601
1867   "s" gnus-article-date-user)
1868
1869 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1870   "t" gnus-article-remove-trailing-blank-lines
1871   "l" gnus-article-strip-leading-blank-lines
1872   "m" gnus-article-strip-multiple-blank-lines
1873   "a" gnus-article-strip-blank-lines
1874   "A" gnus-article-strip-all-blank-lines
1875   "s" gnus-article-strip-leading-space
1876   "e" gnus-article-strip-trailing-space
1877   "w" gnus-article-remove-leading-whitespace)
1878
1879 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1880   "v" gnus-version
1881   "f" gnus-summary-fetch-faq
1882   "d" gnus-summary-describe-group
1883   "h" gnus-summary-describe-briefly
1884   "i" gnus-info-find-node
1885   "c" gnus-group-fetch-charter
1886   "C" gnus-group-fetch-control)
1887
1888 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1889   "e" gnus-summary-expire-articles
1890   "\M-\C-e" gnus-summary-expire-articles-now
1891   "\177" gnus-summary-delete-article
1892   [delete] gnus-summary-delete-article
1893   [backspace] gnus-summary-delete-article
1894   "m" gnus-summary-move-article
1895   "r" gnus-summary-respool-article
1896   "w" gnus-summary-edit-article
1897   "c" gnus-summary-copy-article
1898   "B" gnus-summary-crosspost-article
1899   "q" gnus-summary-respool-query
1900   "t" gnus-summary-respool-trace
1901   "i" gnus-summary-import-article
1902   "I" gnus-summary-create-article
1903   "p" gnus-summary-article-posted-p)
1904
1905 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1906   "o" gnus-summary-save-article
1907   "m" gnus-summary-save-article-mail
1908   "F" gnus-summary-write-article-file
1909   "r" gnus-summary-save-article-rmail
1910   "f" gnus-summary-save-article-file
1911   "b" gnus-summary-save-article-body-file
1912   "h" gnus-summary-save-article-folder
1913   "v" gnus-summary-save-article-vm
1914   "p" gnus-summary-pipe-output
1915   "P" gnus-summary-muttprint
1916   "s" gnus-soup-add-article)
1917
1918 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1919   "b" gnus-summary-display-buttonized
1920   "m" gnus-summary-repair-multipart
1921   "v" gnus-article-view-part
1922   "o" gnus-article-save-part
1923   "c" gnus-article-copy-part
1924   "C" gnus-article-view-part-as-charset
1925   "e" gnus-article-view-part-externally
1926   "E" gnus-article-encrypt-body
1927   "i" gnus-article-inline-part
1928   "|" gnus-article-pipe-part)
1929
1930 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1931   "p" gnus-summary-mark-as-processable
1932   "u" gnus-summary-unmark-as-processable
1933   "U" gnus-summary-unmark-all-processable
1934   "v" gnus-uu-mark-over
1935   "s" gnus-uu-mark-series
1936   "r" gnus-uu-mark-region
1937   "g" gnus-uu-unmark-region
1938   "R" gnus-uu-mark-by-regexp
1939   "G" gnus-uu-unmark-by-regexp
1940   "t" gnus-uu-mark-thread
1941   "T" gnus-uu-unmark-thread
1942   "a" gnus-uu-mark-all
1943   "b" gnus-uu-mark-buffer
1944   "S" gnus-uu-mark-sparse
1945   "k" gnus-summary-kill-process-mark
1946   "y" gnus-summary-yank-process-mark
1947   "w" gnus-summary-save-process-mark
1948   "i" gnus-uu-invert-processable)
1949
1950 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1951   ;;"x" gnus-uu-extract-any
1952   "m" gnus-summary-save-parts
1953   "u" gnus-uu-decode-uu
1954   "U" gnus-uu-decode-uu-and-save
1955   "s" gnus-uu-decode-unshar
1956   "S" gnus-uu-decode-unshar-and-save
1957   "o" gnus-uu-decode-save
1958   "O" gnus-uu-decode-save
1959   "b" gnus-uu-decode-binhex
1960   "B" gnus-uu-decode-binhex
1961   "p" gnus-uu-decode-postscript
1962   "P" gnus-uu-decode-postscript-and-save)
1963
1964 (gnus-define-keys
1965     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1966   "u" gnus-uu-decode-uu-view
1967   "U" gnus-uu-decode-uu-and-save-view
1968   "s" gnus-uu-decode-unshar-view
1969   "S" gnus-uu-decode-unshar-and-save-view
1970   "o" gnus-uu-decode-save-view
1971   "O" gnus-uu-decode-save-view
1972   "b" gnus-uu-decode-binhex-view
1973   "B" gnus-uu-decode-binhex-view
1974   "p" gnus-uu-decode-postscript-view
1975   "P" gnus-uu-decode-postscript-and-save-view)
1976
1977 (defvar gnus-article-post-menu nil)
1978
1979 (defconst gnus-summary-menu-maxlen 20)
1980
1981 (defun gnus-summary-menu-split (menu)
1982   ;; If we have lots of elements, divide them into groups of 20
1983   ;; and make a pane (or submenu) for each one.
1984   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1985       (let ((menu menu) sublists next
1986             (i 1))
1987         (while menu
1988           ;; Pull off the next gnus-summary-menu-maxlen elements
1989           ;; and make them the next element of sublist.
1990           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1991           (if next
1992               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1993                       nil))
1994           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1995                                              (aref (car (last menu)) 0)) menu)
1996                                sublists))
1997           (setq i (1+ i))
1998           (setq menu next))
1999         (nreverse sublists))
2000     ;; Few elements--put them all in one pane.
2001     menu))
2002
2003 (defun gnus-summary-make-menu-bar ()
2004   (gnus-turn-off-edit-menu 'summary)
2005
2006   (unless (boundp 'gnus-summary-misc-menu)
2007
2008     (easy-menu-define
2009      gnus-summary-kill-menu gnus-summary-mode-map ""
2010      (cons
2011       "Score"
2012       (nconc
2013        (list
2014         ["Customize" gnus-score-customize t])
2015        (gnus-make-score-map 'increase)
2016        (gnus-make-score-map 'lower)
2017        '(("Mark"
2018           ["Kill below" gnus-summary-kill-below t]
2019           ["Mark above" gnus-summary-mark-above t]
2020           ["Tick above" gnus-summary-tick-above t]
2021           ["Clear above" gnus-summary-clear-above t])
2022          ["Current score" gnus-summary-current-score t]
2023          ["Set score" gnus-summary-set-score t]
2024          ["Switch current score file..." gnus-score-change-score-file t]
2025          ["Set mark below..." gnus-score-set-mark-below t]
2026          ["Set expunge below..." gnus-score-set-expunge-below t]
2027          ["Edit current score file" gnus-score-edit-current-scores t]
2028          ["Edit score file" gnus-score-edit-file t]
2029          ["Trace score" gnus-score-find-trace t]
2030          ["Find words" gnus-score-find-favourite-words t]
2031          ["Rescore buffer" gnus-summary-rescore t]
2032          ["Increase score..." gnus-summary-increase-score t]
2033          ["Lower score..." gnus-summary-lower-score t]))))
2034
2035     ;; Define both the Article menu in the summary buffer and the
2036     ;; equivalent Commands menu in the article buffer here for
2037     ;; consistency.
2038     (let ((innards
2039            `(("Hide"
2040               ["All" gnus-article-hide t]
2041               ["Headers" gnus-article-hide-headers t]
2042               ["Signature" gnus-article-hide-signature t]
2043               ["Citation" gnus-article-hide-citation t]
2044               ["List identifiers" gnus-article-hide-list-identifiers t]
2045               ["PGP" gnus-article-hide-pgp t]
2046               ["Banner" gnus-article-strip-banner t]
2047               ["Boring headers" gnus-article-hide-boring-headers t])
2048              ("Highlight"
2049               ["All" gnus-article-highlight t]
2050               ["Headers" gnus-article-highlight-headers t]
2051               ["Signature" gnus-article-highlight-signature t]
2052               ["Citation" gnus-article-highlight-citation t])
2053              ("Date"
2054               ["Local" gnus-article-date-local t]
2055               ["ISO8601" gnus-article-date-iso8601 t]
2056               ["UT" gnus-article-date-ut t]
2057               ["Original" gnus-article-date-original t]
2058               ["Lapsed" gnus-article-date-lapsed t]
2059               ["User-defined" gnus-article-date-user t])
2060              ("Display"
2061               ["Remove images" gnus-article-remove-images t]
2062               ["Toggle smiley" gnus-treat-smiley t]
2063               ["Show X-Face" gnus-article-display-x-face t]
2064               ["Show picons in From" gnus-treat-from-picon t]
2065               ["Show picons in mail headers" gnus-treat-mail-picon t]
2066               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2067               ("View as different encoding"
2068                ,@(gnus-summary-menu-split
2069                   (mapcar
2070                    (lambda (cs)
2071                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2072                      ;; forms for menu commands, we should provide intern'ed
2073                      ;; function symbols.
2074                      (let ((command (intern (format "\
2075 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2076                        (fset command
2077                              `(lambda ()
2078                                 (interactive)
2079                                 (let ((gnus-summary-show-article-charset-alist
2080                                        '((1 . ,cs))))
2081                                   (gnus-summary-show-article 1))))
2082                        `[,(symbol-name cs) ,command t]))
2083                    (sort (if (fboundp 'coding-system-list)
2084                              (coding-system-list)
2085                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2086                            )
2087                          'string<)))))
2088              ("Washing"
2089               ("Remove Blanks"
2090                ["Leading" gnus-article-strip-leading-blank-lines t]
2091                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2092                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2093                ["All of the above" gnus-article-strip-blank-lines t]
2094                ["All" gnus-article-strip-all-blank-lines t]
2095                ["Leading space" gnus-article-strip-leading-space t]
2096                ["Trailing space" gnus-article-strip-trailing-space t]
2097                ["Leading space in headers"
2098                 gnus-article-remove-leading-whitespace t])
2099               ["Overstrike" gnus-article-treat-overstrike t]
2100               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2101               ["Emphasis" gnus-article-emphasize t]
2102               ["Word wrap" gnus-article-fill-cited-article t]
2103               ["Fill long lines" gnus-article-fill-long-lines t]
2104               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2105               ["CR" gnus-article-remove-cr t]
2106               ["Rot 13" gnus-summary-caesar-message
2107                ,@(if (featurep 'xemacs) '(t)
2108                    '(:help "\"Caesar rotate\" article by 13"))]
2109               ["Morse decode" gnus-summary-morse-message t]
2110               ["Unix pipe..." gnus-summary-pipe-message t]
2111               ["Add buttons" gnus-article-add-buttons t]
2112               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2113               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2114               ["Toggle MIME" gnus-summary-toggle-mime t]
2115               ["Verbose header" gnus-summary-verbose-headers t]
2116               ["Toggle header" gnus-summary-toggle-header t]
2117               ["Unfold headers" gnus-article-treat-unfold-headers t]
2118               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2119               ["Html" gnus-article-wash-html t]
2120               ["URLs" gnus-article-unsplit-urls t]
2121               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2122               ["HZ" gnus-article-decode-HZ t]
2123               ("(Outlook) Deuglify"
2124                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2125                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2126                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2127                ["Full (Outlook) deuglify"
2128                 gnus-article-outlook-deuglify-article t])
2129               )
2130              ("Output"
2131               ["Save in default format" gnus-summary-save-article
2132                ,@(if (featurep 'xemacs) '(t)
2133                    '(:help "Save article using default method"))]
2134               ["Save in file" gnus-summary-save-article-file
2135                ,@(if (featurep 'xemacs) '(t)
2136                    '(:help "Save article in file"))]
2137               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2138               ["Save in MH folder" gnus-summary-save-article-folder t]
2139               ["Save in VM folder" gnus-summary-save-article-vm t]
2140               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2141               ["Save body in file" gnus-summary-save-article-body-file t]
2142               ["Pipe through a filter" gnus-summary-pipe-output t]
2143               ["Add to SOUP packet" gnus-soup-add-article t]
2144               ["Print with Muttprint" gnus-summary-muttprint t]
2145               ["Print" gnus-summary-print-article t])
2146              ("Backend"
2147               ["Respool article..." gnus-summary-respool-article t]
2148               ["Move article..." gnus-summary-move-article
2149                (gnus-check-backend-function
2150                 'request-move-article gnus-newsgroup-name)]
2151               ["Copy article..." gnus-summary-copy-article t]
2152               ["Crosspost article..." gnus-summary-crosspost-article
2153                (gnus-check-backend-function
2154                 'request-replace-article gnus-newsgroup-name)]
2155               ["Import file..." gnus-summary-import-article t]
2156               ["Create article..." gnus-summary-create-article t]
2157               ["Check if posted" gnus-summary-article-posted-p t]
2158               ["Edit article" gnus-summary-edit-article
2159                (not (gnus-group-read-only-p))]
2160               ["Delete article" gnus-summary-delete-article
2161                (gnus-check-backend-function
2162                 'request-expire-articles gnus-newsgroup-name)]
2163               ["Query respool" gnus-summary-respool-query t]
2164               ["Trace respool" gnus-summary-respool-trace t]
2165               ["Delete expirable articles" gnus-summary-expire-articles-now
2166                (gnus-check-backend-function
2167                 'request-expire-articles gnus-newsgroup-name)])
2168              ("Extract"
2169               ["Uudecode" gnus-uu-decode-uu
2170                ,@(if (featurep 'xemacs) '(t)
2171                    '(:help "Decode uuencoded article(s)"))]
2172               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2173               ["Unshar" gnus-uu-decode-unshar t]
2174               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2175               ["Save" gnus-uu-decode-save t]
2176               ["Binhex" gnus-uu-decode-binhex t]
2177               ["Postscript" gnus-uu-decode-postscript t])
2178              ("Cache"
2179               ["Enter article" gnus-cache-enter-article t]
2180               ["Remove article" gnus-cache-remove-article t])
2181              ["Translate" gnus-article-babel t]
2182              ["Select article buffer" gnus-summary-select-article-buffer t]
2183              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2184              ["Isearch article..." gnus-summary-isearch-article t]
2185              ["Beginning of the article" gnus-summary-beginning-of-article t]
2186              ["End of the article" gnus-summary-end-of-article t]
2187              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2188              ["Fetch referenced articles" gnus-summary-refer-references t]
2189              ["Fetch current thread" gnus-summary-refer-thread t]
2190              ["Fetch article with id..." gnus-summary-refer-article t]
2191              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2192              ["Redisplay" gnus-summary-show-article t]
2193              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2194       (easy-menu-define
2195        gnus-summary-article-menu gnus-summary-mode-map ""
2196        (cons "Article" innards))
2197
2198       (if (not (keymapp gnus-summary-article-menu))
2199           (easy-menu-define
2200            gnus-article-commands-menu gnus-article-mode-map ""
2201            (cons "Commands" innards))
2202         ;; in Emacs, don't share menu.
2203         (setq gnus-article-commands-menu
2204               (copy-keymap gnus-summary-article-menu))
2205         (define-key gnus-article-mode-map [menu-bar commands]
2206           (cons "Commands" gnus-article-commands-menu))))
2207
2208     (easy-menu-define
2209      gnus-summary-thread-menu gnus-summary-mode-map ""
2210      '("Threads"
2211        ["Find all messages in thread" gnus-summary-refer-thread t]
2212        ["Toggle threading" gnus-summary-toggle-threads t]
2213        ["Hide threads" gnus-summary-hide-all-threads t]
2214        ["Show threads" gnus-summary-show-all-threads t]
2215        ["Hide thread" gnus-summary-hide-thread t]
2216        ["Show thread" gnus-summary-show-thread t]
2217        ["Go to next thread" gnus-summary-next-thread t]
2218        ["Go to previous thread" gnus-summary-prev-thread t]
2219        ["Go down thread" gnus-summary-down-thread t]
2220        ["Go up thread" gnus-summary-up-thread t]
2221        ["Top of thread" gnus-summary-top-thread t]
2222        ["Mark thread as read" gnus-summary-kill-thread t]
2223        ["Lower thread score" gnus-summary-lower-thread t]
2224        ["Raise thread score" gnus-summary-raise-thread t]
2225        ["Rethread current" gnus-summary-rethread-current t]))
2226
2227     (easy-menu-define
2228      gnus-summary-post-menu gnus-summary-mode-map ""
2229      `("Post"
2230        ["Send a message (mail or news)" gnus-summary-post-news
2231         ,@(if (featurep 'xemacs) '(t)
2232             '(:help "Post an article"))]
2233        ["Followup" gnus-summary-followup
2234         ,@(if (featurep 'xemacs) '(t)
2235             '(:help "Post followup to this article"))]
2236        ["Followup and yank" gnus-summary-followup-with-original
2237         ,@(if (featurep 'xemacs) '(t)
2238             '(:help "Post followup to this article, quoting its contents"))]
2239        ["Supersede article" gnus-summary-supersede-article t]
2240        ["Cancel article" gnus-summary-cancel-article
2241         ,@(if (featurep 'xemacs) '(t)
2242             '(:help "Cancel an article you posted"))]
2243        ["Reply" gnus-summary-reply t]
2244        ["Reply and yank" gnus-summary-reply-with-original t]
2245        ["Wide reply" gnus-summary-wide-reply t]
2246        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2247         ,@(if (featurep 'xemacs) '(t)
2248             '(:help "Mail a reply, quoting this article"))]
2249        ["Very wide reply" gnus-summary-very-wide-reply t]
2250        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2251         ,@(if (featurep 'xemacs) '(t)
2252             '(:help "Mail a very wide reply, quoting this article"))]
2253        ["Mail forward" gnus-summary-mail-forward t]
2254        ["Post forward" gnus-summary-post-forward t]
2255        ["Digest and mail" gnus-summary-digest-mail-forward t]
2256        ["Digest and post" gnus-summary-digest-post-forward t]
2257        ["Resend message" gnus-summary-resend-message t]
2258        ["Resend message edit" gnus-summary-resend-message-edit t]
2259        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2260        ["Send a mail" gnus-summary-mail-other-window t]
2261        ["Create a local message" gnus-summary-news-other-window t]
2262        ["Uuencode and post" gnus-uu-post-news
2263         ,@(if (featurep 'xemacs) '(t)
2264             '(:help "Post a uuencoded article"))]
2265        ["Followup via news" gnus-summary-followup-to-mail t]
2266        ["Followup via news and yank"
2267         gnus-summary-followup-to-mail-with-original t]
2268        ;;("Draft"
2269        ;;["Send" gnus-summary-send-draft t]
2270        ;;["Send bounced" gnus-resend-bounced-mail t])
2271        ))
2272
2273     (cond
2274      ((not (keymapp gnus-summary-post-menu))
2275       (setq gnus-article-post-menu gnus-summary-post-menu))
2276      ((not gnus-article-post-menu)
2277       ;; Don't share post menu.
2278       (setq gnus-article-post-menu
2279             (copy-keymap gnus-summary-post-menu))))
2280     (define-key gnus-article-mode-map [menu-bar post]
2281       (cons "Post" gnus-article-post-menu))
2282
2283     (easy-menu-define
2284      gnus-summary-misc-menu gnus-summary-mode-map ""
2285      `("Gnus"
2286        ("Mark Read"
2287         ["Mark as read" gnus-summary-mark-as-read-forward t]
2288         ["Mark same subject and select"
2289          gnus-summary-kill-same-subject-and-select t]
2290         ["Mark same subject" gnus-summary-kill-same-subject t]
2291         ["Catchup" gnus-summary-catchup
2292          ,@(if (featurep 'xemacs) '(t)
2293              '(:help "Mark unread articles in this group as read"))]
2294         ["Catchup all" gnus-summary-catchup-all t]
2295         ["Catchup to here" gnus-summary-catchup-to-here t]
2296         ["Catchup from here" gnus-summary-catchup-from-here t]
2297         ["Catchup region" gnus-summary-mark-region-as-read
2298          (gnus-mark-active-p)]
2299         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2300        ("Mark Various"
2301         ["Tick" gnus-summary-tick-article-forward t]
2302         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2303         ["Remove marks" gnus-summary-clear-mark-forward t]
2304         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2305         ["Set bookmark" gnus-summary-set-bookmark t]
2306         ["Remove bookmark" gnus-summary-remove-bookmark t])
2307        ("Limit to"
2308         ["Marks..." gnus-summary-limit-to-marks t]
2309         ["Subject..." gnus-summary-limit-to-subject t]
2310         ["Author..." gnus-summary-limit-to-author t]
2311         ["Age..." gnus-summary-limit-to-age t]
2312         ["Extra..." gnus-summary-limit-to-extra t]
2313         ["Score..." gnus-summary-limit-to-score t]
2314         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2315         ["Unread" gnus-summary-limit-to-unread t]
2316         ["Unseen" gnus-summary-limit-to-unseen t]
2317         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2318         ["Next articles" gnus-summary-limit-to-articles t]
2319         ["Pop limit" gnus-summary-pop-limit t]
2320         ["Show dormant" gnus-summary-limit-include-dormant t]
2321         ["Hide childless dormant"
2322          gnus-summary-limit-exclude-childless-dormant t]
2323         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2324         ["Hide marked" gnus-summary-limit-exclude-marks t]
2325         ["Show expunged" gnus-summary-limit-include-expunged t])
2326        ("Process Mark"
2327         ["Set mark" gnus-summary-mark-as-processable t]
2328         ["Remove mark" gnus-summary-unmark-as-processable t]
2329         ["Remove all marks" gnus-summary-unmark-all-processable t]
2330         ["Mark above" gnus-uu-mark-over t]
2331         ["Mark series" gnus-uu-mark-series t]
2332         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2333         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2334         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2335         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2336         ["Mark all" gnus-uu-mark-all t]
2337         ["Mark buffer" gnus-uu-mark-buffer t]
2338         ["Mark sparse" gnus-uu-mark-sparse t]
2339         ["Mark thread" gnus-uu-mark-thread t]
2340         ["Unmark thread" gnus-uu-unmark-thread t]
2341         ("Process Mark Sets"
2342          ["Kill" gnus-summary-kill-process-mark t]
2343          ["Yank" gnus-summary-yank-process-mark
2344           gnus-newsgroup-process-stack]
2345          ["Save" gnus-summary-save-process-mark t]))
2346        ("Scroll article"
2347         ["Page forward" gnus-summary-next-page
2348          ,@(if (featurep 'xemacs) '(t)
2349              '(:help "Show next page of article"))]
2350         ["Page backward" gnus-summary-prev-page
2351          ,@(if (featurep 'xemacs) '(t)
2352              '(:help "Show previous page of article"))]
2353         ["Line forward" gnus-summary-scroll-up t])
2354        ("Move"
2355         ["Next unread article" gnus-summary-next-unread-article t]
2356         ["Previous unread article" gnus-summary-prev-unread-article t]
2357         ["Next article" gnus-summary-next-article t]
2358         ["Previous article" gnus-summary-prev-article t]
2359         ["Next unread subject" gnus-summary-next-unread-subject t]
2360         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2361         ["Next article same subject" gnus-summary-next-same-subject t]
2362         ["Previous article same subject" gnus-summary-prev-same-subject t]
2363         ["First unread article" gnus-summary-first-unread-article t]
2364         ["Best unread article" gnus-summary-best-unread-article t]
2365         ["Go to subject number..." gnus-summary-goto-subject t]
2366         ["Go to article number..." gnus-summary-goto-article t]
2367         ["Go to the last article" gnus-summary-goto-last-article t]
2368         ["Pop article off history" gnus-summary-pop-article t])
2369        ("Sort"
2370         ["Sort by number" gnus-summary-sort-by-number t]
2371         ["Sort by author" gnus-summary-sort-by-author t]
2372         ["Sort by subject" gnus-summary-sort-by-subject t]
2373         ["Sort by date" gnus-summary-sort-by-date t]
2374         ["Sort by score" gnus-summary-sort-by-score t]
2375         ["Sort by lines" gnus-summary-sort-by-lines t]
2376         ["Sort by characters" gnus-summary-sort-by-chars t]
2377         ["Randomize" gnus-summary-sort-by-random t]
2378         ["Original sort" gnus-summary-sort-by-original t])
2379        ("Help"
2380         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2381         ["Describe group" gnus-summary-describe-group t]
2382         ["Fetch charter" gnus-group-fetch-charter
2383          ,@(if (featurep 'xemacs) nil
2384              '(:help "Display the charter of the current group"))]
2385         ["Fetch control message" gnus-group-fetch-control
2386          ,@(if (featurep 'xemacs) nil
2387              '(:help "Display the archived control message for the current group"))]
2388         ["Read manual" gnus-info-find-node t])
2389        ("Modes"
2390         ["Pick and read" gnus-pick-mode t]
2391         ["Binary" gnus-binary-mode t])
2392        ("Regeneration"
2393         ["Regenerate" gnus-summary-prepare t]
2394         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2395         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2396         ["Toggle threading" gnus-summary-toggle-threads t])
2397        ["See old articles" gnus-summary-insert-old-articles t]
2398        ["See new articles" gnus-summary-insert-new-articles t]
2399        ["Filter articles..." gnus-summary-execute-command t]
2400        ["Run command on subjects..." gnus-summary-universal-argument t]
2401        ["Search articles forward..." gnus-summary-search-article-forward t]
2402        ["Search articles backward..." gnus-summary-search-article-backward t]
2403        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2404        ["Expand window" gnus-summary-expand-window t]
2405        ["Expire expirable articles" gnus-summary-expire-articles
2406         (gnus-check-backend-function
2407          'request-expire-articles gnus-newsgroup-name)]
2408        ["Edit local kill file" gnus-summary-edit-local-kill t]
2409        ["Edit main kill file" gnus-summary-edit-global-kill t]
2410        ["Edit group parameters" gnus-summary-edit-parameters t]
2411        ["Customize group parameters" gnus-summary-customize-parameters t]
2412        ["Send a bug report" gnus-bug t]
2413        ("Exit"
2414         ["Catchup and exit" gnus-summary-catchup-and-exit
2415          ,@(if (featurep 'xemacs) '(t)
2416              '(:help "Mark unread articles in this group as read, then exit"))]
2417         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2418         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2419         ["Exit group" gnus-summary-exit
2420          ,@(if (featurep 'xemacs) '(t)
2421              '(:help "Exit current group, return to group selection mode"))]
2422         ["Exit group without updating" gnus-summary-exit-no-update t]
2423         ["Exit and goto next group" gnus-summary-next-group t]
2424         ["Exit and goto prev group" gnus-summary-prev-group t]
2425         ["Reselect group" gnus-summary-reselect-current-group t]
2426         ["Rescan group" gnus-summary-rescan-group t]
2427         ["Update dribble" gnus-summary-save-newsrc t])))
2428
2429     (gnus-run-hooks 'gnus-summary-menu-hook)))
2430
2431 (defvar gnus-summary-tool-bar-map nil)
2432
2433 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2434 (defun gnus-summary-make-tool-bar ()
2435   (if (and (fboundp 'tool-bar-add-item-from-menu)
2436            (default-value 'tool-bar-mode)
2437            (not gnus-summary-tool-bar-map))
2438       (setq gnus-summary-tool-bar-map
2439             (let ((tool-bar-map (make-sparse-keymap))
2440                   (load-path (mm-image-load-path)))
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-post-news "post" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-followup "followup" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-reply "reply" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2471               tool-bar-map)))
2472   (if gnus-summary-tool-bar-map
2473       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2474
2475 (defun gnus-score-set-default (var value)
2476   "A version of set that updates the GNU Emacs menu-bar."
2477   (set var value)
2478   ;; It is the message that forces the active status to be updated.
2479   (message ""))
2480
2481 (defun gnus-make-score-map (type)
2482   "Make a summary score map of type TYPE."
2483   (if t
2484       nil
2485     (let ((headers '(("author" "from" string)
2486                      ("subject" "subject" string)
2487                      ("article body" "body" string)
2488                      ("article head" "head" string)
2489                      ("xref" "xref" string)
2490                      ("extra header" "extra" string)
2491                      ("lines" "lines" number)
2492                      ("followups to author" "followup" string)))
2493           (types '((number ("less than" <)
2494                            ("greater than" >)
2495                            ("equal" =))
2496                    (string ("substring" s)
2497                            ("exact string" e)
2498                            ("fuzzy string" f)
2499                            ("regexp" r))))
2500           (perms '(("temporary" (current-time-string))
2501                    ("permanent" nil)
2502                    ("immediate" now)))
2503           header)
2504       (list
2505        (apply
2506         'nconc
2507         (list
2508          (if (eq type 'lower)
2509              "Lower score"
2510            "Increase score"))
2511         (let (outh)
2512           (while headers
2513             (setq header (car headers))
2514             (setq outh
2515                   (cons
2516                    (apply
2517                     'nconc
2518                     (list (car header))
2519                     (let ((ts (cdr (assoc (nth 2 header) types)))
2520                           outt)
2521                       (while ts
2522                         (setq outt
2523                               (cons
2524                                (apply
2525                                 'nconc
2526                                 (list (caar ts))
2527                                 (let ((ps perms)
2528                                       outp)
2529                                   (while ps
2530                                     (setq outp
2531                                           (cons
2532                                            (vector
2533                                             (caar ps)
2534                                             (list
2535                                              'gnus-summary-score-entry
2536                                              (nth 1 header)
2537                                              (if (or (string= (nth 1 header)
2538                                                               "head")
2539                                                      (string= (nth 1 header)
2540                                                               "body"))
2541                                                  ""
2542                                                (list 'gnus-summary-header
2543                                                      (nth 1 header)))
2544                                              (list 'quote (nth 1 (car ts)))
2545                                              (list 'gnus-score-delta-default
2546                                                    nil)
2547                                              (nth 1 (car ps))
2548                                              t)
2549                                             t)
2550                                            outp))
2551                                     (setq ps (cdr ps)))
2552                                   (list (nreverse outp))))
2553                                outt))
2554                         (setq ts (cdr ts)))
2555                       (list (nreverse outt))))
2556                    outh))
2557             (setq headers (cdr headers)))
2558           (list (nreverse outh))))))))
2559
2560 \f
2561
2562 (defun gnus-summary-mode (&optional group)
2563   "Major mode for reading articles.
2564
2565 All normal editing commands are switched off.
2566 \\<gnus-summary-mode-map>
2567 Each line in this buffer represents one article.  To read an
2568 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2569 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2570 respectively.
2571
2572 You can also post articles and send mail from this buffer.  To
2573 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2574 of an article, type `\\[gnus-summary-reply]'.
2575
2576 There are approx. one gazillion commands you can execute in this
2577 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2578
2579 The following commands are available:
2580
2581 \\{gnus-summary-mode-map}"
2582   (interactive)
2583   (kill-all-local-variables)
2584   (when (gnus-visual-p 'summary-menu 'menu)
2585     (gnus-summary-make-menu-bar)
2586     (gnus-summary-make-tool-bar))
2587   (gnus-summary-make-local-variables)
2588   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2589     (gnus-summary-make-local-variables))
2590   (gnus-make-thread-indent-array)
2591   (gnus-simplify-mode-line)
2592   (setq major-mode 'gnus-summary-mode)
2593   (setq mode-name "Summary")
2594   (make-local-variable 'minor-mode-alist)
2595   (use-local-map gnus-summary-mode-map)
2596   (buffer-disable-undo)
2597   (setq buffer-read-only t)             ;Disable modification
2598   (setq truncate-lines t)
2599   (setq selective-display t)
2600   (setq selective-display-ellipses t)   ;Display `...'
2601   (gnus-summary-set-display-table)
2602   (gnus-set-default-directory)
2603   (setq gnus-newsgroup-name group)
2604   (unless (gnus-news-group-p group)
2605     (setq gnus-newsgroup-incorporated
2606           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2607   (make-local-variable 'gnus-summary-line-format)
2608   (make-local-variable 'gnus-summary-line-format-spec)
2609   (make-local-variable 'gnus-summary-dummy-line-format)
2610   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2611   (make-local-variable 'gnus-summary-mark-positions)
2612   (make-local-hook 'pre-command-hook)
2613   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2614   (gnus-run-hooks 'gnus-summary-mode-hook)
2615   (turn-on-gnus-mailing-list-mode)
2616   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2617   (gnus-update-summary-mark-positions))
2618
2619 (defun gnus-summary-make-local-variables ()
2620   "Make all the local summary buffer variables."
2621   (let (global)
2622     (dolist (local gnus-summary-local-variables)
2623       (if (consp local)
2624           (progn
2625             (if (eq (cdr local) 'global)
2626                 ;; Copy the global value of the variable.
2627                 (setq global (symbol-value (car local)))
2628               ;; Use the value from the list.
2629               (setq global (eval (cdr local))))
2630             (set (make-local-variable (car local)) global))
2631         ;; Simple nil-valued local variable.
2632         (set (make-local-variable local) nil)))))
2633
2634 (defun gnus-summary-clear-local-variables ()
2635   (let ((locals gnus-summary-local-variables))
2636     (while locals
2637       (if (consp (car locals))
2638           (and (vectorp (caar locals))
2639                (set (caar locals) nil))
2640         (and (vectorp (car locals))
2641              (set (car locals) nil)))
2642       (setq locals (cdr locals)))))
2643
2644 ;; Summary data functions.
2645
2646 (defmacro gnus-data-number (data)
2647   `(car ,data))
2648
2649 (defmacro gnus-data-set-number (data number)
2650   `(setcar ,data ,number))
2651
2652 (defmacro gnus-data-mark (data)
2653   `(nth 1 ,data))
2654
2655 (defmacro gnus-data-set-mark (data mark)
2656   `(setcar (nthcdr 1 ,data) ,mark))
2657
2658 (defmacro gnus-data-pos (data)
2659   `(nth 2 ,data))
2660
2661 (defmacro gnus-data-set-pos (data pos)
2662   `(setcar (nthcdr 2 ,data) ,pos))
2663
2664 (defmacro gnus-data-header (data)
2665   `(nth 3 ,data))
2666
2667 (defmacro gnus-data-set-header (data header)
2668   `(setcar (nthcdr 3 ,data) ,header))
2669
2670 (defmacro gnus-data-level (data)
2671   `(nth 4 ,data))
2672
2673 (defmacro gnus-data-unread-p (data)
2674   `(= (nth 1 ,data) gnus-unread-mark))
2675
2676 (defmacro gnus-data-read-p (data)
2677   `(/= (nth 1 ,data) gnus-unread-mark))
2678
2679 (defmacro gnus-data-pseudo-p (data)
2680   `(consp (nth 3 ,data)))
2681
2682 (defmacro gnus-data-find (number)
2683   `(assq ,number gnus-newsgroup-data))
2684
2685 (defmacro gnus-data-find-list (number &optional data)
2686   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2687      (memq (assq ,number bdata)
2688            bdata)))
2689
2690 (defmacro gnus-data-make (number mark pos header level)
2691   `(list ,number ,mark ,pos ,header ,level))
2692
2693 (defun gnus-data-enter (after-article number mark pos header level offset)
2694   (let ((data (gnus-data-find-list after-article)))
2695     (unless data
2696       (error "No such article: %d" after-article))
2697     (setcdr data (cons (gnus-data-make number mark pos header level)
2698                        (cdr data)))
2699     (setq gnus-newsgroup-data-reverse nil)
2700     (gnus-data-update-list (cddr data) offset)))
2701
2702 (defun gnus-data-enter-list (after-article list &optional offset)
2703   (when list
2704     (let ((data (and after-article (gnus-data-find-list after-article)))
2705           (ilist list))
2706       (if (not (or data
2707                    after-article))
2708           (let ((odata gnus-newsgroup-data))
2709             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2710             (when offset
2711               (gnus-data-update-list odata offset)))
2712         ;; Find the last element in the list to be spliced into the main
2713         ;; list.
2714         (while (cdr list)
2715           (setq list (cdr list)))
2716         (if (not data)
2717             (progn
2718               (setcdr list gnus-newsgroup-data)
2719               (setq gnus-newsgroup-data ilist)
2720               (when offset
2721                 (gnus-data-update-list (cdr list) offset)))
2722           (setcdr list (cdr data))
2723           (setcdr data ilist)
2724           (when offset
2725             (gnus-data-update-list (cdr list) offset))))
2726       (setq gnus-newsgroup-data-reverse nil))))
2727
2728 (defun gnus-data-remove (article &optional offset)
2729   (let ((data gnus-newsgroup-data))
2730     (if (= (gnus-data-number (car data)) article)
2731         (progn
2732           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2733                 gnus-newsgroup-data-reverse nil)
2734           (when offset
2735             (gnus-data-update-list gnus-newsgroup-data offset)))
2736       (while (cdr data)
2737         (when (= (gnus-data-number (cadr data)) article)
2738           (setcdr data (cddr data))
2739           (when offset
2740             (gnus-data-update-list (cdr data) offset))
2741           (setq data nil
2742                 gnus-newsgroup-data-reverse nil))
2743         (setq data (cdr data))))))
2744
2745 (defmacro gnus-data-list (backward)
2746   `(if ,backward
2747        (or gnus-newsgroup-data-reverse
2748            (setq gnus-newsgroup-data-reverse
2749                  (reverse gnus-newsgroup-data)))
2750      gnus-newsgroup-data))
2751
2752 (defun gnus-data-update-list (data offset)
2753   "Add OFFSET to the POS of all data entries in DATA."
2754   (setq gnus-newsgroup-data-reverse nil)
2755   (while data
2756     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2757     (setq data (cdr data))))
2758
2759 (defun gnus-summary-article-pseudo-p (article)
2760   "Say whether this article is a pseudo article or not."
2761   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2762
2763 (defmacro gnus-summary-article-sparse-p (article)
2764   "Say whether this article is a sparse article or not."
2765   `(memq ,article gnus-newsgroup-sparse))
2766
2767 (defmacro gnus-summary-article-ancient-p (article)
2768   "Say whether this article is a sparse article or not."
2769   `(memq ,article gnus-newsgroup-ancient))
2770
2771 (defun gnus-article-parent-p (number)
2772   "Say whether this article is a parent or not."
2773   (let ((data (gnus-data-find-list number)))
2774     (and (cdr data)                     ; There has to be an article after...
2775          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2776             (gnus-data-level (nth 1 data))))))
2777
2778 (defun gnus-article-children (number)
2779   "Return a list of all children to NUMBER."
2780   (let* ((data (gnus-data-find-list number))
2781          (level (gnus-data-level (car data)))
2782          children)
2783     (setq data (cdr data))
2784     (while (and data
2785                 (= (gnus-data-level (car data)) (1+ level)))
2786       (push (gnus-data-number (car data)) children)
2787       (setq data (cdr data)))
2788     children))
2789
2790 (defmacro gnus-summary-skip-intangible ()
2791   "If the current article is intangible, then jump to a different article."
2792   '(let ((to (get-text-property (point) 'gnus-intangible)))
2793      (and to (gnus-summary-goto-subject to))))
2794
2795 (defmacro gnus-summary-article-intangible-p ()
2796   "Say whether this article is intangible or not."
2797   '(get-text-property (point) 'gnus-intangible))
2798
2799 (defun gnus-article-read-p (article)
2800   "Say whether ARTICLE is read or not."
2801   (not (or (memq article gnus-newsgroup-marked)
2802            (memq article gnus-newsgroup-spam-marked)
2803            (memq article gnus-newsgroup-unreads)
2804            (memq article gnus-newsgroup-unselected)
2805            (memq article gnus-newsgroup-dormant))))
2806
2807 ;; Some summary mode macros.
2808
2809 (defmacro gnus-summary-article-number ()
2810   "The article number of the article on the current line.
2811 If there isn't an article number here, then we return the current
2812 article number."
2813   '(progn
2814      (gnus-summary-skip-intangible)
2815      (or (get-text-property (point) 'gnus-number)
2816          (gnus-summary-last-subject))))
2817
2818 (defmacro gnus-summary-article-header (&optional number)
2819   "Return the header of article NUMBER."
2820   `(gnus-data-header (gnus-data-find
2821                       ,(or number '(gnus-summary-article-number)))))
2822
2823 (defmacro gnus-summary-thread-level (&optional number)
2824   "Return the level of thread that starts with article NUMBER."
2825   `(if (and (eq gnus-summary-make-false-root 'dummy)
2826             (get-text-property (point) 'gnus-intangible))
2827        0
2828      (gnus-data-level (gnus-data-find
2829                        ,(or number '(gnus-summary-article-number))))))
2830
2831 (defmacro gnus-summary-article-mark (&optional number)
2832   "Return the mark of article NUMBER."
2833   `(gnus-data-mark (gnus-data-find
2834                     ,(or number '(gnus-summary-article-number)))))
2835
2836 (defmacro gnus-summary-article-pos (&optional number)
2837   "Return the position of the line of article NUMBER."
2838   `(gnus-data-pos (gnus-data-find
2839                    ,(or number '(gnus-summary-article-number)))))
2840
2841 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2842 (defmacro gnus-summary-article-subject (&optional number)
2843   "Return current subject string or nil if nothing."
2844   `(let ((headers
2845           ,(if number
2846                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2847              '(gnus-data-header (assq (gnus-summary-article-number)
2848                                       gnus-newsgroup-data)))))
2849      (and headers
2850           (vectorp headers)
2851           (mail-header-subject headers))))
2852
2853 (defmacro gnus-summary-article-score (&optional number)
2854   "Return current article score."
2855   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2856                   gnus-newsgroup-scored))
2857        gnus-summary-default-score 0))
2858
2859 (defun gnus-summary-article-children (&optional number)
2860   "Return a list of article numbers that are children of article NUMBER."
2861   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2862          (level (gnus-data-level (car data)))
2863          l children)
2864     (while (and (setq data (cdr data))
2865                 (> (setq l (gnus-data-level (car data))) level))
2866       (and (= (1+ level) l)
2867            (push (gnus-data-number (car data))
2868                  children)))
2869     (nreverse children)))
2870
2871 (defun gnus-summary-article-parent (&optional number)
2872   "Return the article number of the parent of article NUMBER."
2873   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2874                                     (gnus-data-list t)))
2875          (level (gnus-data-level (car data))))
2876     (if (zerop level)
2877         ()                              ; This is a root.
2878       ;; We search until we find an article with a level less than
2879       ;; this one.  That function has to be the parent.
2880       (while (and (setq data (cdr data))
2881                   (not (< (gnus-data-level (car data)) level))))
2882       (and data (gnus-data-number (car data))))))
2883
2884 (defun gnus-unread-mark-p (mark)
2885   "Say whether MARK is the unread mark."
2886   (= mark gnus-unread-mark))
2887
2888 (defun gnus-read-mark-p (mark)
2889   "Say whether MARK is one of the marks that mark as read.
2890 This is all marks except unread, ticked, dormant, and expirable."
2891   (not (or (= mark gnus-unread-mark)
2892            (= mark gnus-ticked-mark)
2893            (= mark gnus-spam-mark)
2894            (= mark gnus-dormant-mark)
2895            (= mark gnus-expirable-mark))))
2896
2897 (defmacro gnus-article-mark (number)
2898   "Return the MARK of article NUMBER.
2899 This macro should only be used when computing the mark the \"first\"
2900 time; i.e., when generating the summary lines.  After that,
2901 `gnus-summary-article-mark' should be used to examine the
2902 marks of articles."
2903   `(cond
2904     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2905     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2906     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2907     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2908     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2909     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2910     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2911     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2912            gnus-ancient-mark))))
2913
2914 ;; Saving hidden threads.
2915
2916 (defmacro gnus-save-hidden-threads (&rest forms)
2917   "Save hidden threads, eval FORMS, and restore the hidden threads."
2918   (let ((config (make-symbol "config")))
2919     `(let ((,config (gnus-hidden-threads-configuration)))
2920        (unwind-protect
2921            (save-excursion
2922              ,@forms)
2923          (gnus-restore-hidden-threads-configuration ,config)))))
2924 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2925 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2926
2927 (defun gnus-data-compute-positions ()
2928   "Compute the positions of all articles."
2929   (setq gnus-newsgroup-data-reverse nil)
2930   (let ((data gnus-newsgroup-data))
2931     (save-excursion
2932       (gnus-save-hidden-threads
2933         (gnus-summary-show-all-threads)
2934         (goto-char (point-min))
2935         (while data
2936           (while (get-text-property (point) 'gnus-intangible)
2937             (forward-line 1))
2938           (gnus-data-set-pos (car data) (+ (point) 3))
2939           (setq data (cdr data))
2940           (forward-line 1))))))
2941
2942 (defun gnus-hidden-threads-configuration ()
2943   "Return the current hidden threads configuration."
2944   (save-excursion
2945     (let (config)
2946       (goto-char (point-min))
2947       (while (search-forward "\r" nil t)
2948         (push (1- (point)) config))
2949       config)))
2950
2951 (defun gnus-restore-hidden-threads-configuration (config)
2952   "Restore hidden threads configuration from CONFIG."
2953   (save-excursion
2954     (let (point buffer-read-only)
2955       (while (setq point (pop config))
2956         (when (and (< point (point-max))
2957                    (goto-char point)
2958                    (eq (char-after) ?\n))
2959           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2960
2961 ;; Various summary mode internalish functions.
2962
2963 (defun gnus-mouse-pick-article (e)
2964   (interactive "e")
2965   (mouse-set-point e)
2966   (gnus-summary-next-page nil t))
2967
2968 (defun gnus-summary-set-display-table ()
2969   "Change the display table.
2970 Odd characters have a tendency to mess
2971 up nicely formatted displays - we make all possible glyphs
2972 display only a single character."
2973
2974   ;; We start from the standard display table, if any.
2975   (let ((table (or (copy-sequence standard-display-table)
2976                    (make-display-table)))
2977         (i 32))
2978     ;; Nix out all the control chars...
2979     (while (>= (setq i (1- i)) 0)
2980       (aset table i [??]))
2981     ;; ... but not newline and cr, of course.  (cr is necessary for the
2982     ;; selective display).
2983     (aset table ?\n nil)
2984     (aset table ?\r nil)
2985     ;; We keep TAB as well.
2986     (aset table ?\t nil)
2987     ;; We nix out any glyphs over 126 that are not set already.
2988     (let ((i 256))
2989       (while (>= (setq i (1- i)) 127)
2990         ;; Only modify if the entry is nil.
2991         (unless (aref table i)
2992           (aset table i [??]))))
2993     (setq buffer-display-table table)))
2994
2995 (defun gnus-summary-set-article-display-arrow (pos)
2996   "Update the overlay arrow to point to line at position POS."
2997   (when (and gnus-summary-display-arrow
2998              (boundp 'overlay-arrow-position)
2999              (boundp 'overlay-arrow-string))
3000     (save-excursion
3001       (goto-char pos)
3002       (beginning-of-line)
3003       (unless overlay-arrow-position
3004         (setq overlay-arrow-position (make-marker)))
3005       (setq overlay-arrow-string "=>"
3006             overlay-arrow-position (set-marker overlay-arrow-position
3007                                                (point)
3008                                                (current-buffer))))))
3009
3010 (defun gnus-summary-buffer-name (group)
3011   "Return the summary buffer name of GROUP."
3012   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3013
3014 (defun gnus-summary-setup-buffer (group)
3015   "Initialize summary buffer."
3016   (let ((buffer (gnus-summary-buffer-name group))
3017         (dead-name (concat "*Dead Summary "
3018                            (gnus-group-decoded-name group) "*")))
3019     ;; If a dead summary buffer exists, we kill it.
3020     (when (gnus-buffer-live-p dead-name)
3021       (gnus-kill-buffer dead-name))
3022     (if (get-buffer buffer)
3023         (progn
3024           (set-buffer buffer)
3025           (setq gnus-summary-buffer (current-buffer))
3026           (not gnus-newsgroup-prepared))
3027       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3028       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3029       (gnus-summary-mode group)
3030       (when gnus-carpal
3031         (gnus-carpal-setup-buffer 'summary))
3032       (unless gnus-single-article-buffer
3033         (make-local-variable 'gnus-article-buffer)
3034         (make-local-variable 'gnus-article-current)
3035         (make-local-variable 'gnus-original-article-buffer))
3036       (setq gnus-newsgroup-name group)
3037       ;; Set any local variables in the group parameters.
3038       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3039       t)))
3040
3041 (defun gnus-set-global-variables ()
3042   "Set the global equivalents of the buffer-local variables.
3043 They are set to the latest values they had.  These reflect the summary
3044 buffer that was in action when the last article was fetched."
3045   (when (eq major-mode 'gnus-summary-mode)
3046     (setq gnus-summary-buffer (current-buffer))
3047     (let ((name gnus-newsgroup-name)
3048           (marked gnus-newsgroup-marked)
3049           (spam gnus-newsgroup-spam-marked)
3050           (unread gnus-newsgroup-unreads)
3051           (headers gnus-current-headers)
3052           (data gnus-newsgroup-data)
3053           (summary gnus-summary-buffer)
3054           (article-buffer gnus-article-buffer)
3055           (original gnus-original-article-buffer)
3056           (gac gnus-article-current)
3057           (reffed gnus-reffed-article-number)
3058           (score-file gnus-current-score-file)
3059           (default-charset gnus-newsgroup-charset)
3060           vlist)
3061       (let ((locals gnus-newsgroup-variables))
3062         (while locals
3063           (if (consp (car locals))
3064               (push (eval (caar locals)) vlist)
3065             (push (eval (car locals)) vlist))
3066           (setq locals (cdr locals)))
3067         (setq vlist (nreverse vlist)))
3068       (save-excursion
3069         (set-buffer gnus-group-buffer)
3070         (setq gnus-newsgroup-name name
3071               gnus-newsgroup-marked marked
3072               gnus-newsgroup-spam-marked spam
3073               gnus-newsgroup-unreads unread
3074               gnus-current-headers headers
3075               gnus-newsgroup-data data
3076               gnus-article-current gac
3077               gnus-summary-buffer summary
3078               gnus-article-buffer article-buffer
3079               gnus-original-article-buffer original
3080               gnus-reffed-article-number reffed
3081               gnus-current-score-file score-file
3082               gnus-newsgroup-charset default-charset)
3083         (let ((locals gnus-newsgroup-variables))
3084           (while locals
3085             (if (consp (car locals))
3086                 (set (caar locals) (pop vlist))
3087               (set (car locals) (pop vlist)))
3088             (setq locals (cdr locals))))
3089         ;; The article buffer also has local variables.
3090         (when (gnus-buffer-live-p gnus-article-buffer)
3091           (set-buffer gnus-article-buffer)
3092           (setq gnus-summary-buffer summary))))))
3093
3094 (defun gnus-summary-article-unread-p (article)
3095   "Say whether ARTICLE is unread or not."
3096   (memq article gnus-newsgroup-unreads))
3097
3098 (defun gnus-summary-first-article-p (&optional article)
3099   "Return whether ARTICLE is the first article in the buffer."
3100   (if (not (setq article (or article (gnus-summary-article-number))))
3101       nil
3102     (eq article (caar gnus-newsgroup-data))))
3103
3104 (defun gnus-summary-last-article-p (&optional article)
3105   "Return whether ARTICLE is the last article in the buffer."
3106   (if (not (setq article (or article (gnus-summary-article-number))))
3107       ;; All non-existent numbers are the last article.  :-)
3108       t
3109     (not (cdr (gnus-data-find-list article)))))
3110
3111 (defun gnus-make-thread-indent-array ()
3112   (let ((n 200))
3113     (unless (and gnus-thread-indent-array
3114                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3115       (setq gnus-thread-indent-array (make-vector 201 "")
3116             gnus-thread-indent-array-level gnus-thread-indent-level)
3117       (while (>= n 0)
3118         (aset gnus-thread-indent-array n
3119               (make-string (* n gnus-thread-indent-level) ? ))
3120         (setq n (1- n))))))
3121
3122 (defun gnus-update-summary-mark-positions ()
3123   "Compute where the summary marks are to go."
3124   (save-excursion
3125     (when (gnus-buffer-exists-p gnus-summary-buffer)
3126       (set-buffer gnus-summary-buffer))
3127     (let ((gnus-replied-mark 129)
3128           (gnus-score-below-mark 130)
3129           (gnus-score-over-mark 130)
3130           (gnus-undownloaded-mark 131)
3131           (spec gnus-summary-line-format-spec)
3132           gnus-visual pos)
3133       (save-excursion
3134         (gnus-set-work-buffer)
3135         (let ((gnus-summary-line-format-spec spec)
3136               (gnus-newsgroup-downloadable '(0)))
3137           (gnus-summary-insert-line
3138            (make-full-mail-header 0 "" "nobody"
3139                                   "05 Apr 2001 23:33:09 +0400"
3140                                   "" "" 0 0 "" nil)
3141            0 nil t 128 t nil "" nil 1)
3142           (goto-char (point-min))
3143           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3144                                              (- (point) (point-min) 1)))))
3145           (goto-char (point-min))
3146           (push (cons 'replied (and (search-forward "\201" nil t)
3147                                     (- (point) (point-min) 1)))
3148                 pos)
3149           (goto-char (point-min))
3150           (push (cons 'score (and (search-forward "\202" nil t)
3151                                   (- (point) (point-min) 1)))
3152                 pos)
3153           (goto-char (point-min))
3154           (push (cons 'download
3155                       (and (search-forward "\203" nil t)
3156                            (- (point) (point-min) 1)))
3157                 pos)))
3158       (setq gnus-summary-mark-positions pos))))
3159
3160 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3161   "Insert a dummy root in the summary buffer."
3162   (beginning-of-line)
3163   (gnus-add-text-properties
3164    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3165    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3166
3167 (defun gnus-summary-extract-address-component (from)
3168   (or (car (funcall gnus-extract-address-components from))
3169       from))
3170
3171 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3172   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3173                                 default-mime-charset)))
3174     ;; Is it really necessary to do this next part for each summary line?
3175     ;; Luckily, doesn't seem to slow things down much.
3176     (or
3177      (and gnus-ignored-from-addresses
3178           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3179           (let ((extra-headers (mail-header-extra header))
3180                 to
3181                 newsgroups)
3182             (cond
3183              ((setq to (cdr (assq 'To extra-headers)))
3184               (concat "-> "
3185                       (inline
3186                         (gnus-summary-extract-address-component
3187                          (funcall gnus-decode-encoded-word-function to)))))
3188              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3189               (concat "=> " newsgroups)))))
3190      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3191
3192 (defun gnus-summary-insert-line (gnus-tmp-header
3193                                  gnus-tmp-level gnus-tmp-current
3194                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3195                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3196                                  &optional gnus-tmp-dummy gnus-tmp-score
3197                                  gnus-tmp-process)
3198   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3199          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3200          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3201          (gnus-tmp-score-char
3202           (if (or (null gnus-summary-default-score)
3203                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3204                       gnus-summary-zcore-fuzz))
3205               ?\ ;;;Whitespace
3206             (if (< gnus-tmp-score gnus-summary-default-score)
3207                 gnus-score-below-mark gnus-score-over-mark)))
3208          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3209          (gnus-tmp-replied
3210           (cond (gnus-tmp-process gnus-process-mark)
3211                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3212                  gnus-cached-mark)
3213                 (gnus-tmp-replied gnus-replied-mark)
3214                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3215                  gnus-forwarded-mark)
3216                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3217                  gnus-saved-mark)
3218                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3219                  gnus-recent-mark)
3220                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3221                  gnus-unseen-mark)
3222                 (t gnus-no-mark)))
3223          (gnus-tmp-downloaded
3224           (cond (undownloaded 
3225                  gnus-undownloaded-mark)
3226                 (gnus-newsgroup-agentized
3227                  gnus-downloaded-mark)
3228                 (t
3229                  gnus-no-mark)))
3230          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3231          (gnus-tmp-name
3232           (cond
3233            ((string-match "<[^>]+> *$" gnus-tmp-from)
3234             (let ((beg (match-beginning 0)))
3235               (or (and (string-match "^\".+\"" gnus-tmp-from)
3236                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3237                   (substring gnus-tmp-from 0 beg))))
3238            ((string-match "(.+)" gnus-tmp-from)
3239             (substring gnus-tmp-from
3240                        (1+ (match-beginning 0)) (1- (match-end 0))))
3241            (t gnus-tmp-from)))
3242          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3243          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3244          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3245          (buffer-read-only nil))
3246     (when (string= gnus-tmp-name "")
3247       (setq gnus-tmp-name gnus-tmp-from))
3248     (unless (numberp gnus-tmp-lines)
3249       (setq gnus-tmp-lines -1))
3250     (if (= gnus-tmp-lines -1)
3251         (setq gnus-tmp-lines "?")
3252       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3253     (gnus-put-text-property-excluding-characters-with-faces
3254      (point)
3255      (progn (eval gnus-summary-line-format-spec) (point))
3256      'gnus-number gnus-tmp-number)
3257     (when (gnus-visual-p 'summary-highlight 'highlight)
3258       (forward-line -1)
3259       (gnus-run-hooks 'gnus-summary-update-hook)
3260       (forward-line 1))))
3261
3262 (defun gnus-summary-update-line (&optional dont-update)
3263   "Update summary line after change."
3264   (when (and gnus-summary-default-score
3265              (not gnus-summary-inhibit-highlight))
3266     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3267            (article (gnus-summary-article-number))
3268            (score (gnus-summary-article-score article)))
3269       (unless dont-update
3270         (if (and gnus-summary-mark-below
3271                  (< (gnus-summary-article-score)
3272                     gnus-summary-mark-below))
3273             ;; This article has a low score, so we mark it as read.
3274             (when (memq article gnus-newsgroup-unreads)
3275               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3276           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3277             ;; This article was previously marked as read on account
3278             ;; of a low score, but now it has risen, so we mark it as
3279             ;; unread.
3280             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3281         (gnus-summary-update-mark
3282          (if (or (null gnus-summary-default-score)
3283                  (<= (abs (- score gnus-summary-default-score))
3284                      gnus-summary-zcore-fuzz))
3285              ?\ ;;;Whitespace
3286            (if (< score gnus-summary-default-score)
3287                gnus-score-below-mark gnus-score-over-mark))
3288          'score))
3289       ;; Do visual highlighting.
3290       (when (gnus-visual-p 'summary-highlight 'highlight)
3291         (gnus-run-hooks 'gnus-summary-update-hook)))))
3292
3293 (defvar gnus-tmp-new-adopts nil)
3294
3295 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3296   "Return the number of articles in THREAD.
3297 This may be 0 in some cases -- if none of the articles in
3298 the thread are to be displayed."
3299   (let* ((number
3300           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3301           (cond
3302            ((not (listp thread))
3303             1)
3304            ((and (consp thread) (cdr thread))
3305             (apply
3306              '+ 1 (mapcar
3307                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3308            ((null thread)
3309             1)
3310            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3311             1)
3312            (t 0))))
3313     (when (and level (zerop level) gnus-tmp-new-adopts)
3314       (incf number
3315             (apply '+ (mapcar
3316                        'gnus-summary-number-of-articles-in-thread
3317                        gnus-tmp-new-adopts))))
3318     (if char
3319         (if (> number 1) gnus-not-empty-thread-mark
3320           gnus-empty-thread-mark)
3321       number)))
3322
3323 (defsubst gnus-summary-line-message-size (head)
3324   "Return pretty-printed version of message size.
3325 This function is intended to be used in
3326 `gnus-summary-line-format-alist', which see."
3327   (let ((c (or (mail-header-chars head) -1)))
3328     (cond ((< c 0) "n/a")               ; chars not available
3329           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3330           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3331           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3332           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3333
3334
3335 (defun gnus-summary-set-local-parameters (group)
3336   "Go through the local params of GROUP and set all variable specs in that list."
3337   (let ((params (gnus-group-find-parameter group))
3338         (vars '(quit-config))           ; Ignore quit-config.
3339         elem)
3340     (while params
3341       (setq elem (car params)
3342             params (cdr params))
3343       (and (consp elem)                 ; Has to be a cons.
3344            (consp (cdr elem))           ; The cdr has to be a list.
3345            (symbolp (car elem))         ; Has to be a symbol in there.
3346            (not (memq (car elem) vars))
3347            (ignore-errors               ; So we set it.
3348              (push (car elem) vars)
3349              (make-local-variable (car elem))
3350              (set (car elem) (eval (nth 1 elem))))))))
3351
3352 (defun gnus-summary-read-group (group &optional show-all no-article
3353                                       kill-buffer no-display backward
3354                                       select-articles)
3355   "Start reading news in newsgroup GROUP.
3356 If SHOW-ALL is non-nil, already read articles are also listed.
3357 If NO-ARTICLE is non-nil, no article is selected initially.
3358 If NO-DISPLAY, don't generate a summary buffer."
3359   (let (result)
3360     (while (and group
3361                 (null (setq result
3362                             (let ((gnus-auto-select-next nil))
3363                               (or (gnus-summary-read-group-1
3364                                    group show-all no-article
3365                                    kill-buffer no-display
3366                                    select-articles)
3367                                   (setq show-all nil
3368                                         select-articles nil)))))
3369                 (eq gnus-auto-select-next 'quietly))
3370       (set-buffer gnus-group-buffer)
3371       ;; The entry function called above goes to the next
3372       ;; group automatically, so we go two groups back
3373       ;; if we are searching for the previous group.
3374       (when backward
3375         (gnus-group-prev-unread-group 2))
3376       (if (not (equal group (gnus-group-group-name)))
3377           (setq group (gnus-group-group-name))
3378         (setq group nil)))
3379     result))
3380
3381 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3382   "Directly jump to the other GROUP from summary buffer.
3383 If SHOW-ALL is non-nil, already read articles are also listed."
3384   (interactive
3385    (if (eq gnus-summary-buffer (current-buffer))
3386        (list (completing-read
3387               "Group: " gnus-active-hashtb nil t
3388               (when (and gnus-newsgroup-name
3389                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3390                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3391               'gnus-group-history)
3392              current-prefix-arg)
3393      (error "%s must be invoked from a gnus summary buffer." this-command)))
3394   (unless (or (zerop (length group))
3395               (and gnus-newsgroup-name
3396                    (string-equal gnus-newsgroup-name group)))
3397     (gnus-summary-exit)
3398     (gnus-summary-read-group group show-all
3399                              gnus-dont-select-after-jump-to-other-group)))
3400
3401 (defun gnus-summary-read-group-1 (group show-all no-article
3402                                         kill-buffer no-display
3403                                         &optional select-articles)
3404   ;; Killed foreign groups can't be entered.
3405   ;;  (when (and (not (gnus-group-native-p group))
3406   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3407   ;;    (error "Dead non-native groups can't be entered"))
3408   (gnus-message 5 "Retrieving newsgroup: %s..."
3409                 (gnus-group-decoded-name group))
3410   (let* ((new-group (gnus-summary-setup-buffer group))
3411          (quit-config (gnus-group-quit-config group))
3412          (did-select (and new-group (gnus-select-newsgroup
3413                                      group show-all select-articles))))
3414     (cond
3415      ;; This summary buffer exists already, so we just select it.
3416      ((not new-group)
3417       (gnus-set-global-variables)
3418       (when kill-buffer
3419         (gnus-kill-or-deaden-summary kill-buffer))
3420       (gnus-configure-windows 'summary 'force)
3421       (gnus-set-mode-line 'summary)
3422       (gnus-summary-position-point)
3423       (message "")
3424       t)
3425      ;; We couldn't select this group.
3426      ((null did-select)
3427       (when (and (eq major-mode 'gnus-summary-mode)
3428                  (not (equal (current-buffer) kill-buffer)))
3429         (kill-buffer (current-buffer))
3430         (if (not quit-config)
3431             (progn
3432               ;; Update the info -- marks might need to be removed,
3433               ;; for instance.
3434               (gnus-summary-update-info)
3435               (set-buffer gnus-group-buffer)
3436               (gnus-group-jump-to-group group)
3437               (gnus-group-next-unread-group 1))
3438           (gnus-handle-ephemeral-exit quit-config)))
3439       (let ((grpinfo (gnus-get-info group)))
3440         (if (null (gnus-info-read grpinfo))
3441             (gnus-message 3 "Group %s contains no messages"
3442                           (gnus-group-decoded-name group))
3443           (gnus-message 3 "Can't select group")))
3444       nil)
3445      ;; The user did a `C-g' while prompting for number of articles,
3446      ;; so we exit this group.
3447      ((eq did-select 'quit)
3448       (and (eq major-mode 'gnus-summary-mode)
3449            (not (equal (current-buffer) kill-buffer))
3450            (kill-buffer (current-buffer)))
3451       (when kill-buffer
3452         (gnus-kill-or-deaden-summary kill-buffer))
3453       (if (not quit-config)
3454           (progn
3455             (set-buffer gnus-group-buffer)
3456             (gnus-group-jump-to-group group)
3457             (gnus-group-next-unread-group 1)
3458             (gnus-configure-windows 'group 'force))
3459         (gnus-handle-ephemeral-exit quit-config))
3460       ;; Finally signal the quit.
3461       (signal 'quit nil))
3462      ;; The group was successfully selected.
3463      (t
3464       (gnus-set-global-variables)
3465       ;; Save the active value in effect when the group was entered.
3466       (setq gnus-newsgroup-active
3467             (gnus-copy-sequence
3468              (gnus-active gnus-newsgroup-name)))
3469       ;; You can change the summary buffer in some way with this hook.
3470       (gnus-run-hooks 'gnus-select-group-hook)
3471       (gnus-update-format-specifications
3472        nil 'summary 'summary-mode 'summary-dummy)
3473       (gnus-update-summary-mark-positions)
3474       ;; Do score processing.
3475       (when gnus-use-scoring
3476         (gnus-possibly-score-headers))
3477       ;; Check whether to fill in the gaps in the threads.
3478       (when gnus-build-sparse-threads
3479         (gnus-build-sparse-threads))
3480       ;; Find the initial limit.
3481       (if gnus-show-threads
3482           (if show-all
3483               (let ((gnus-newsgroup-dormant nil))
3484                 (gnus-summary-initial-limit show-all))
3485             (gnus-summary-initial-limit show-all))
3486         ;; When unthreaded, all articles are always shown.
3487         (setq gnus-newsgroup-limit
3488               (mapcar
3489                (lambda (header) (mail-header-number header))
3490                gnus-newsgroup-headers)))
3491       ;; Generate the summary buffer.
3492       (unless no-display
3493         (gnus-summary-prepare))
3494       (when gnus-use-trees
3495         (gnus-tree-open group)
3496         (setq gnus-summary-highlight-line-function
3497               'gnus-tree-highlight-article))
3498       ;; If the summary buffer is empty, but there are some low-scored
3499       ;; articles or some excluded dormants, we include these in the
3500       ;; buffer.
3501       (when (and (zerop (buffer-size))
3502                  (not no-display))
3503         (cond (gnus-newsgroup-dormant
3504                (gnus-summary-limit-include-dormant))
3505               ((and gnus-newsgroup-scored show-all)
3506                (gnus-summary-limit-include-expunged t))))
3507       ;; Function `gnus-apply-kill-file' must be called in this hook.
3508       (gnus-run-hooks 'gnus-apply-kill-hook)
3509       (if (and (zerop (buffer-size))
3510                (not no-display))
3511           (progn
3512             ;; This newsgroup is empty.
3513             (gnus-summary-catchup-and-exit nil t)
3514             (gnus-message 6 "No unread news")
3515             (when kill-buffer
3516               (gnus-kill-or-deaden-summary kill-buffer))
3517             ;; Return nil from this function.
3518             nil)
3519         ;; Hide conversation thread subtrees.  We cannot do this in
3520         ;; gnus-summary-prepare-hook since kill processing may not
3521         ;; work with hidden articles.
3522         (gnus-summary-maybe-hide-threads)
3523         (when kill-buffer
3524           (gnus-kill-or-deaden-summary kill-buffer))
3525         (gnus-summary-auto-select-subject)
3526         ;; Show first unread article if requested.
3527         (if (and (not no-article)
3528                  (not no-display)
3529                  gnus-newsgroup-unreads
3530                  gnus-auto-select-first)
3531             (progn
3532               (gnus-configure-windows 'summary)
3533               (let ((art (gnus-summary-article-number)))
3534                 (unless (and (not gnus-plugged)
3535                              (or (memq art gnus-newsgroup-undownloaded)
3536                                  (memq art gnus-newsgroup-downloadable)))
3537                   (gnus-summary-goto-article art))))
3538           ;; Don't select any articles.
3539           (gnus-summary-position-point)
3540           (gnus-configure-windows 'summary 'force)
3541           (gnus-set-mode-line 'summary))
3542         (when (get-buffer-window gnus-group-buffer t)
3543           ;; Gotta use windows, because recenter does weird stuff if
3544           ;; the current buffer ain't the displayed window.
3545           (let ((owin (selected-window)))
3546             (select-window (get-buffer-window gnus-group-buffer t))
3547             (when (gnus-group-goto-group group)
3548               (recenter))
3549             (select-window owin)))
3550         ;; Mark this buffer as "prepared".
3551         (setq gnus-newsgroup-prepared t)
3552         (gnus-run-hooks 'gnus-summary-prepared-hook)
3553         (unless (gnus-ephemeral-group-p group)
3554           (gnus-group-update-group group))
3555         t)))))
3556
3557 (defun gnus-summary-auto-select-subject ()
3558   "Select the subject line on initial group entry."
3559   (goto-char (point-min))
3560   (cond
3561    ((eq gnus-auto-select-subject 'best)
3562     (gnus-summary-best-unread-subject))
3563    ((eq gnus-auto-select-subject 'unread)
3564     (gnus-summary-first-unread-subject))
3565    ((eq gnus-auto-select-subject 'unseen)
3566     (gnus-summary-first-unseen-subject))
3567    ((eq gnus-auto-select-subject 'unseen-or-unread)
3568     (gnus-summary-first-unseen-or-unread-subject))
3569    ((eq gnus-auto-select-subject 'first)
3570     ;; Do nothing.
3571     )
3572    ((gnus-functionp gnus-auto-select-subject)
3573     (funcall gnus-auto-select-subject))))
3574
3575 (defun gnus-summary-prepare ()
3576   "Generate the summary buffer."
3577   (interactive)
3578   (let ((buffer-read-only nil))
3579     (erase-buffer)
3580     (setq gnus-newsgroup-data nil
3581           gnus-newsgroup-data-reverse nil)
3582     (gnus-run-hooks 'gnus-summary-generate-hook)
3583     ;; Generate the buffer, either with threads or without.
3584     (when gnus-newsgroup-headers
3585       (gnus-summary-prepare-threads
3586        (if gnus-show-threads
3587            (gnus-sort-gathered-threads
3588             (funcall gnus-summary-thread-gathering-function
3589                      (gnus-sort-threads
3590                       (gnus-cut-threads (gnus-make-threads)))))
3591          ;; Unthreaded display.
3592          (gnus-sort-articles gnus-newsgroup-headers))))
3593     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3594     ;; Call hooks for modifying summary buffer.
3595     (goto-char (point-min))
3596     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3597
3598 (defsubst gnus-general-simplify-subject (subject)
3599   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3600   (setq subject
3601         (cond
3602          ;; Truncate the subject.
3603          (gnus-simplify-subject-functions
3604           (gnus-map-function gnus-simplify-subject-functions subject))
3605          ((numberp gnus-summary-gather-subject-limit)
3606           (setq subject (gnus-simplify-subject-re subject))
3607           (if (> (length subject) gnus-summary-gather-subject-limit)
3608               (substring subject 0 gnus-summary-gather-subject-limit)
3609             subject))
3610          ;; Fuzzily simplify it.
3611          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3612           (gnus-simplify-subject-fuzzy subject))
3613          ;; Just remove the leading "Re:".
3614          (t
3615           (gnus-simplify-subject-re subject))))
3616
3617   (if (and gnus-summary-gather-exclude-subject
3618            (string-match gnus-summary-gather-exclude-subject subject))
3619       nil                               ; This article shouldn't be gathered
3620     subject))
3621
3622 (defun gnus-summary-simplify-subject-query ()
3623   "Query where the respool algorithm would put this article."
3624   (interactive)
3625   (gnus-summary-select-article)
3626   (message "%s"
3627            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3628
3629 (defun gnus-gather-threads-by-subject (threads)
3630   "Gather threads by looking at Subject headers."
3631   (if (not gnus-summary-make-false-root)
3632       threads
3633     (let ((hashtb (gnus-make-hashtable 1024))
3634           (prev threads)
3635           (result threads)
3636           subject hthread whole-subject)
3637       (while threads
3638         (setq subject (gnus-general-simplify-subject
3639                        (setq whole-subject (mail-header-subject
3640                                             (caar threads)))))
3641         (when subject
3642           (if (setq hthread (gnus-gethash subject hashtb))
3643               (progn
3644                 ;; We enter a dummy root into the thread, if we
3645                 ;; haven't done that already.
3646                 (unless (stringp (caar hthread))
3647                   (setcar hthread (list whole-subject (car hthread))))
3648                 ;; We add this new gathered thread to this gathered
3649                 ;; thread.
3650                 (setcdr (car hthread)
3651                         (nconc (cdar hthread) (list (car threads))))
3652                 ;; Remove it from the list of threads.
3653                 (setcdr prev (cdr threads))
3654                 (setq threads prev))
3655             ;; Enter this thread into the hash table.
3656             (gnus-sethash subject
3657                           (if gnus-summary-make-false-root-always
3658                               (progn
3659                                 ;; If you want a dummy root above all
3660                                 ;; threads...
3661                                 (setcar threads (list whole-subject
3662                                                       (car threads)))
3663                                 threads)
3664                             threads)
3665                           hashtb)))
3666         (setq prev threads)
3667         (setq threads (cdr threads)))
3668       result)))
3669
3670 (defun gnus-gather-threads-by-references (threads)
3671   "Gather threads by looking at References headers."
3672   (let ((idhashtb (gnus-make-hashtable 1024))
3673         (thhashtb (gnus-make-hashtable 1024))
3674         (prev threads)
3675         (result threads)
3676         ids references id gthread gid entered ref)
3677     (while threads
3678       (when (setq references (mail-header-references (caar threads)))
3679         (setq id (mail-header-id (caar threads))
3680               ids (inline (gnus-split-references references))
3681               entered nil)
3682         (while (setq ref (pop ids))
3683           (setq ids (delete ref ids))
3684           (if (not (setq gid (gnus-gethash ref idhashtb)))
3685               (progn
3686                 (gnus-sethash ref id idhashtb)
3687                 (gnus-sethash id threads thhashtb))
3688             (setq gthread (gnus-gethash gid thhashtb))
3689             (unless entered
3690               ;; We enter a dummy root into the thread, if we
3691               ;; haven't done that already.
3692               (unless (stringp (caar gthread))
3693                 (setcar gthread (list (mail-header-subject (caar gthread))
3694                                       (car gthread))))
3695               ;; We add this new gathered thread to this gathered
3696               ;; thread.
3697               (setcdr (car gthread)
3698                       (nconc (cdar gthread) (list (car threads)))))
3699             ;; Add it into the thread hash table.
3700             (gnus-sethash id gthread thhashtb)
3701             (setq entered t)
3702             ;; Remove it from the list of threads.
3703             (setcdr prev (cdr threads))
3704             (setq threads prev))))
3705       (setq prev threads)
3706       (setq threads (cdr threads)))
3707     result))
3708
3709 (defun gnus-sort-gathered-threads (threads)
3710   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3711   (let ((result threads))
3712     (while threads
3713       (when (stringp (caar threads))
3714         (setcdr (car threads)
3715                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3716       (setq threads (cdr threads)))
3717     result))
3718
3719 (defun gnus-thread-loop-p (root thread)
3720   "Say whether ROOT is in THREAD."
3721   (let ((stack (list thread))
3722         (infloop 0)
3723         th)
3724     (while (setq thread (pop stack))
3725       (setq th (cdr thread))
3726       (while (and th
3727                   (not (eq (caar th) root)))
3728         (pop th))
3729       (if th
3730           ;; We have found a loop.
3731           (let (ref-dep)
3732             (setcdr thread (delq (car th) (cdr thread)))
3733             (if (boundp (setq ref-dep (intern "none"
3734                                               gnus-newsgroup-dependencies)))
3735                 (setcdr (symbol-value ref-dep)
3736                         (nconc (cdr (symbol-value ref-dep))
3737                                (list (car th))))
3738               (set ref-dep (list nil (car th))))
3739             (setq infloop 1
3740                   stack nil))
3741         ;; Push all the subthreads onto the stack.
3742         (push (cdr thread) stack)))
3743     infloop))
3744
3745 (defun gnus-make-threads ()
3746   "Go through the dependency hashtb and find the roots.  Return all threads."
3747   (let (threads)
3748     (while (catch 'infloop
3749              (mapatoms
3750               (lambda (refs)
3751                 ;; Deal with self-referencing References loops.
3752                 (when (and (car (symbol-value refs))
3753                            (not (zerop
3754                                  (apply
3755                                   '+
3756                                   (mapcar
3757                                    (lambda (thread)
3758                                      (gnus-thread-loop-p
3759                                       (car (symbol-value refs)) thread))
3760                                    (cdr (symbol-value refs)))))))
3761                   (setq threads nil)
3762                   (throw 'infloop t))
3763                 (unless (car (symbol-value refs))
3764                   ;; These threads do not refer back to any other articles,
3765                   ;; so they're roots.
3766                   (setq threads (append (cdr (symbol-value refs)) threads))))
3767               gnus-newsgroup-dependencies)))
3768     threads))
3769
3770 ;; Build the thread tree.
3771 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3772   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3773
3774 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3775 if it was already present.
3776
3777 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3778 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3779 Message-IDs will be renamed to a unique Message-ID before being
3780 entered.
3781
3782 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3783   (let* ((id (mail-header-id header))
3784          (id-dep (and id (intern id dependencies)))
3785          parent-id ref ref-dep ref-header replaced)
3786     ;; Enter this `header' in the `dependencies' table.
3787     (cond
3788      ((not id-dep)
3789       (setq header nil))
3790      ;; The first two cases do the normal part: enter a new `header'
3791      ;; in the `dependencies' table.
3792      ((not (boundp id-dep))
3793       (set id-dep (list header)))
3794      ((null (car (symbol-value id-dep)))
3795       (setcar (symbol-value id-dep) header))
3796
3797      ;; From here the `header' was already present in the
3798      ;; `dependencies' table.
3799      (force-new
3800       ;; Overrides an existing entry;
3801       ;; just set the header part of the entry.
3802       (setcar (symbol-value id-dep) header)
3803       (setq replaced t))
3804
3805      ;; Renames the existing `header' to a unique Message-ID.
3806      ((not gnus-summary-ignore-duplicates)
3807       ;; An article with this Message-ID has already been seen.
3808       ;; We rename the Message-ID.
3809       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3810            (list header))
3811       (mail-header-set-id header id))
3812
3813      ;; The last case ignores an existing entry, except it adds any
3814      ;; additional Xrefs (in case the two articles came from different
3815      ;; servers.
3816      ;; Also sets `header' to `nil' meaning that the `dependencies'
3817      ;; table was *not* modified.
3818      (t
3819       (mail-header-set-xref
3820        (car (symbol-value id-dep))
3821        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3822                    "")
3823                (or (mail-header-xref header) "")))
3824       (setq header nil)))
3825
3826     (when (and header (not replaced))
3827       ;; First check that we are not creating a References loop.
3828       (setq parent-id (gnus-parent-id (mail-header-references header)))
3829       (setq ref parent-id)
3830       (while (and ref
3831                   (setq ref-dep (intern-soft ref dependencies))
3832                   (boundp ref-dep)
3833                   (setq ref-header (car (symbol-value ref-dep))))
3834         (if (string= id ref)
3835             ;; Yuk!  This is a reference loop.  Make the article be a
3836             ;; root article.
3837             (progn
3838               (mail-header-set-references (car (symbol-value id-dep)) "none")
3839               (setq ref nil)
3840               (setq parent-id nil))
3841           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3842       (setq ref-dep (intern (or parent-id "none") dependencies))
3843       (if (boundp ref-dep)
3844           (setcdr (symbol-value ref-dep)
3845                   (nconc (cdr (symbol-value ref-dep))
3846                          (list (symbol-value id-dep))))
3847         (set ref-dep (list nil (symbol-value id-dep)))))
3848     header))
3849
3850 (defun gnus-extract-message-id-from-in-reply-to (string)
3851   (if (string-match "<[^>]+>" string)
3852       (substring string (match-beginning 0) (match-end 0))
3853     nil))
3854
3855 (defun gnus-build-sparse-threads ()
3856   (let ((headers gnus-newsgroup-headers)
3857         (mail-parse-charset gnus-newsgroup-charset)
3858         (gnus-summary-ignore-duplicates t)
3859         header references generation relations
3860         subject child end new-child date)
3861     ;; First we create an alist of generations/relations, where
3862     ;; generations is how much we trust the relation, and the relation
3863     ;; is parent/child.
3864     (gnus-message 7 "Making sparse threads...")
3865     (save-excursion
3866       (nnheader-set-temp-buffer " *gnus sparse threads*")
3867       (while (setq header (pop headers))
3868         (when (and (setq references (mail-header-references header))
3869                    (not (string= references "")))
3870           (insert references)
3871           (setq child (mail-header-id header)
3872                 subject (mail-header-subject header)
3873                 date (mail-header-date header)
3874                 generation 0)
3875           (while (search-backward ">" nil t)
3876             (setq end (1+ (point)))
3877             (when (search-backward "<" nil t)
3878               (setq new-child (buffer-substring (point) end))
3879               (push (list (incf generation)
3880                           child (setq child new-child)
3881                           subject date)
3882                     relations)))
3883           (when child
3884             (push (list (1+ generation) child nil subject) relations))
3885           (erase-buffer)))
3886       (kill-buffer (current-buffer)))
3887     ;; Sort over trustworthiness.
3888     (mapcar
3889      (lambda (relation)
3890        (when (gnus-dependencies-add-header
3891               (make-full-mail-header-from-decoded-header
3892                gnus-reffed-article-number
3893                (nth 3 relation) "" (or (nth 4 relation) "")
3894                (nth 1 relation)
3895                (or (nth 2 relation) "") 0 0 "")
3896               gnus-newsgroup-dependencies nil)
3897          (push gnus-reffed-article-number gnus-newsgroup-limit)
3898          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3899          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3900                gnus-newsgroup-reads)
3901          (decf gnus-reffed-article-number)))
3902      (sort relations 'car-less-than-car))
3903     (gnus-message 7 "Making sparse threads...done")))
3904
3905 (defun gnus-build-old-threads ()
3906   ;; Look at all the articles that refer back to old articles, and
3907   ;; fetch the headers for the articles that aren't there.  This will
3908   ;; build complete threads - if the roots haven't been expired by the
3909   ;; server, that is.
3910   (let ((mail-parse-charset gnus-newsgroup-charset)
3911         id heads)
3912     (mapatoms
3913      (lambda (refs)
3914        (when (not (car (symbol-value refs)))
3915          (setq heads (cdr (symbol-value refs)))
3916          (while heads
3917            (if (memq (mail-header-number (caar heads))
3918                      gnus-newsgroup-dormant)
3919                (setq heads (cdr heads))
3920              (setq id (symbol-name refs))
3921              (while (and (setq id (gnus-build-get-header id))
3922                          (not (car (gnus-id-to-thread id)))))
3923              (setq heads nil)))))
3924      gnus-newsgroup-dependencies)))
3925
3926 ;; This function has to be called with point after the article number
3927 ;; on the beginning of the line.
3928 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3929   (let ((eol (gnus-point-at-eol))
3930         (buffer (current-buffer))
3931         header references in-reply-to)
3932
3933     ;; overview: [num subject from date id refs chars lines misc]
3934     (unwind-protect
3935         (progn
3936           (narrow-to-region (point) eol)
3937           (unless (eobp)
3938             (forward-char))
3939
3940           (setq header
3941                 (make-full-mail-header
3942                  number                         ; number
3943                  (nnheader-nov-field)           ; subject
3944                  (nnheader-nov-field)           ; from
3945                  (nnheader-nov-field)           ; date
3946                  (nnheader-nov-read-message-id) ; id
3947                  (nnheader-nov-field)           ; refs
3948                  (nnheader-nov-read-integer)    ; chars
3949                  (nnheader-nov-read-integer)    ; lines
3950                  (unless (eobp)
3951                    (if (looking-at "Xref: ")
3952                        (goto-char (match-end 0)))
3953                    (nnheader-nov-field))        ; Xref
3954                  (nnheader-nov-parse-extra))))  ; extra
3955
3956       (widen))
3957
3958     (when (and (string= references "")
3959                (setq in-reply-to (mail-header-extra header))
3960                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3961       (mail-header-set-references
3962        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3963
3964     (when gnus-alter-header-function
3965       (funcall gnus-alter-header-function header))
3966     (gnus-dependencies-add-header header dependencies force-new)))
3967
3968 (defun gnus-build-get-header (id)
3969   "Look through the buffer of NOV lines and find the header to ID.
3970 Enter this line into the dependencies hash table, and return
3971 the id of the parent article (if any)."
3972   (let ((deps gnus-newsgroup-dependencies)
3973         found header)
3974     (prog1
3975         (save-excursion
3976           (set-buffer nntp-server-buffer)
3977           (let ((case-fold-search nil))
3978             (goto-char (point-min))
3979             (while (and (not found)
3980                         (search-forward id nil t))
3981               (beginning-of-line)
3982               (setq found (looking-at
3983                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3984                                    (regexp-quote id))))
3985               (or found (beginning-of-line 2)))
3986             (when found
3987               (beginning-of-line)
3988               (and
3989                (setq header (gnus-nov-parse-line
3990                              (read (current-buffer)) deps))
3991                (gnus-parent-id (mail-header-references header))))))
3992       (when header
3993         (let ((number (mail-header-number header)))
3994           (push number gnus-newsgroup-limit)
3995           (push header gnus-newsgroup-headers)
3996           (if (memq number gnus-newsgroup-unselected)
3997               (progn
3998                 (setq gnus-newsgroup-unreads
3999                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4000                                                number))
4001                 (setq gnus-newsgroup-unselected
4002                       (delq number gnus-newsgroup-unselected)))
4003             (push number gnus-newsgroup-ancient)))))))
4004
4005 (defun gnus-build-all-threads ()
4006   "Read all the headers."
4007   (let ((gnus-summary-ignore-duplicates t)
4008         (mail-parse-charset gnus-newsgroup-charset)
4009         (dependencies gnus-newsgroup-dependencies)
4010         header article)
4011     (save-excursion
4012       (set-buffer nntp-server-buffer)
4013       (let ((case-fold-search nil))
4014         (goto-char (point-min))
4015         (while (not (eobp))
4016           (ignore-errors
4017             (setq article (read (current-buffer))
4018                   header (gnus-nov-parse-line article dependencies)))
4019           (when header
4020             (save-excursion
4021               (set-buffer gnus-summary-buffer)
4022               (push header gnus-newsgroup-headers)
4023               (if (memq (setq article (mail-header-number header))
4024                         gnus-newsgroup-unselected)
4025                   (progn
4026                     (setq gnus-newsgroup-unreads
4027                           (gnus-add-to-sorted-list
4028                            gnus-newsgroup-unreads article))
4029                     (setq gnus-newsgroup-unselected
4030                           (delq article gnus-newsgroup-unselected)))
4031                 (push article gnus-newsgroup-ancient)))
4032             (forward-line 1)))))))
4033
4034 (defun gnus-summary-update-article-line (article header)
4035   "Update the line for ARTICLE using HEADERS."
4036   (let* ((id (mail-header-id header))
4037          (thread (gnus-id-to-thread id)))
4038     (unless thread
4039       (error "Article in no thread"))
4040     ;; Update the thread.
4041     (setcar thread header)
4042     (gnus-summary-goto-subject article)
4043     (let* ((datal (gnus-data-find-list article))
4044            (data (car datal))
4045            (length (when (cdr datal)
4046                      (- (gnus-data-pos data)
4047                         (gnus-data-pos (cadr datal)))))
4048            (buffer-read-only nil)
4049            (level (gnus-summary-thread-level)))
4050       (gnus-delete-line)
4051       (gnus-summary-insert-line
4052        header level nil 
4053        (memq article gnus-newsgroup-undownloaded)
4054        (gnus-article-mark article)
4055        (memq article gnus-newsgroup-replied)
4056        (memq article gnus-newsgroup-expirable)
4057        ;; Only insert the Subject string when it's different
4058        ;; from the previous Subject string.
4059        (if (and
4060             gnus-show-threads
4061             (gnus-subject-equal
4062              (condition-case ()
4063                  (mail-header-subject
4064                   (gnus-data-header
4065                    (cadr
4066                     (gnus-data-find-list
4067                      article
4068                      (gnus-data-list t)))))
4069                ;; Error on the side of excessive subjects.
4070                (error ""))
4071              (mail-header-subject header)))
4072            ""
4073          (mail-header-subject header))
4074        nil (cdr (assq article gnus-newsgroup-scored))
4075        (memq article gnus-newsgroup-processable))
4076       (when length
4077         (gnus-data-update-list
4078          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4079
4080 (defun gnus-summary-update-article (article &optional iheader)
4081   "Update ARTICLE in the summary buffer."
4082   (set-buffer gnus-summary-buffer)
4083   (let* ((header (gnus-summary-article-header article))
4084          (id (mail-header-id header))
4085          (data (gnus-data-find article))
4086          (thread (gnus-id-to-thread id))
4087          (references (mail-header-references header))
4088          (parent
4089           (gnus-id-to-thread
4090            (or (gnus-parent-id
4091                 (when (and references
4092                            (not (equal "" references)))
4093                   references))
4094                "none")))
4095          (buffer-read-only nil)
4096          (old (car thread)))
4097     (when thread
4098       (unless iheader
4099         (setcar thread nil)
4100         (when parent
4101           (delq thread parent)))
4102       (if (gnus-summary-insert-subject id header)
4103           ;; Set the (possibly) new article number in the data structure.
4104           (gnus-data-set-number data (gnus-id-to-article id))
4105         (setcar thread old)
4106         nil))))
4107
4108 (defun gnus-rebuild-thread (id &optional line)
4109   "Rebuild the thread containing ID.
4110 If LINE, insert the rebuilt thread starting on line LINE."
4111   (let ((buffer-read-only nil)
4112         old-pos current thread data)
4113     (if (not gnus-show-threads)
4114         (setq thread (list (car (gnus-id-to-thread id))))
4115       ;; Get the thread this article is part of.
4116       (setq thread (gnus-remove-thread id)))
4117     (setq old-pos (gnus-point-at-bol))
4118     (setq current (save-excursion
4119                     (and (re-search-backward "[\r\n]" nil t)
4120                          (gnus-summary-article-number))))
4121     ;; If this is a gathered thread, we have to go some re-gathering.
4122     (when (stringp (car thread))
4123       (let ((subject (car thread))
4124             roots thr)
4125         (setq thread (cdr thread))
4126         (while thread
4127           (unless (memq (setq thr (gnus-id-to-thread
4128                                    (gnus-root-id
4129                                     (mail-header-id (caar thread)))))
4130                         roots)
4131             (push thr roots))
4132           (setq thread (cdr thread)))
4133         ;; We now have all (unique) roots.
4134         (if (= (length roots) 1)
4135             ;; All the loose roots are now one solid root.
4136             (setq thread (car roots))
4137           (setq thread (cons subject (gnus-sort-threads roots))))))
4138     (let (threads)
4139       ;; We then insert this thread into the summary buffer.
4140       (when line
4141         (goto-char (point-min))
4142         (forward-line (1- line)))
4143       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4144         (if gnus-show-threads
4145             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4146           (gnus-summary-prepare-unthreaded thread))
4147         (setq data (nreverse gnus-newsgroup-data))
4148         (setq threads gnus-newsgroup-threads))
4149       ;; We splice the new data into the data structure.
4150       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4151       ;;!!! then we want to insert at the beginning of the buffer.
4152       ;;!!! That happens to be true with Gnus now, but that may
4153       ;;!!! change in the future.  Perhaps.
4154       (gnus-data-enter-list
4155        (if line nil current) data (- (point) old-pos))
4156       (setq gnus-newsgroup-threads
4157             (nconc threads gnus-newsgroup-threads))
4158       (gnus-data-compute-positions))))
4159
4160 (defun gnus-number-to-header (number)
4161   "Return the header for article NUMBER."
4162   (let ((headers gnus-newsgroup-headers))
4163     (while (and headers
4164                 (not (= number (mail-header-number (car headers)))))
4165       (pop headers))
4166     (when headers
4167       (car headers))))
4168
4169 (defun gnus-parent-headers (in-headers &optional generation)
4170   "Return the headers of the GENERATIONeth parent of HEADERS."
4171   (unless generation
4172     (setq generation 1))
4173   (let ((parent t)
4174         (headers in-headers)
4175         references)
4176     (while (and parent
4177                 (not (zerop generation))
4178                 (setq references (mail-header-references headers)))
4179       (setq headers (if (and references
4180                              (setq parent (gnus-parent-id references)))
4181                         (car (gnus-id-to-thread parent))
4182                       nil))
4183       (decf generation))
4184     (and (not (eq headers in-headers))
4185          headers)))
4186
4187 (defun gnus-id-to-thread (id)
4188   "Return the (sub-)thread where ID appears."
4189   (gnus-gethash id gnus-newsgroup-dependencies))
4190
4191 (defun gnus-id-to-article (id)
4192   "Return the article number of ID."
4193   (let ((thread (gnus-id-to-thread id)))
4194     (when (and thread
4195                (car thread))
4196       (mail-header-number (car thread)))))
4197
4198 (defun gnus-id-to-header (id)
4199   "Return the article headers of ID."
4200   (car (gnus-id-to-thread id)))
4201
4202 (defun gnus-article-displayed-root-p (article)
4203   "Say whether ARTICLE is a root(ish) article."
4204   (let ((level (gnus-summary-thread-level article))
4205         (refs (mail-header-references  (gnus-summary-article-header article)))
4206         particle)
4207     (cond
4208      ((null level) nil)
4209      ((zerop level) t)
4210      ((null refs) t)
4211      ((null (gnus-parent-id refs)) t)
4212      ((and (= 1 level)
4213            (null (setq particle (gnus-id-to-article
4214                                  (gnus-parent-id refs))))
4215            (null (gnus-summary-thread-level particle)))))))
4216
4217 (defun gnus-root-id (id)
4218   "Return the id of the root of the thread where ID appears."
4219   (let (last-id prev)
4220     (while (and id (setq prev (car (gnus-id-to-thread id))))
4221       (setq last-id id
4222             id (gnus-parent-id (mail-header-references prev))))
4223     last-id))
4224
4225 (defun gnus-articles-in-thread (thread)
4226   "Return the list of articles in THREAD."
4227   (cons (mail-header-number (car thread))
4228         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4229
4230 (defun gnus-remove-thread (id &optional dont-remove)
4231   "Remove the thread that has ID in it."
4232   (let (headers thread last-id)
4233     ;; First go up in this thread until we find the root.
4234     (setq last-id (gnus-root-id id)
4235           headers (message-flatten-list (gnus-id-to-thread last-id)))
4236     ;; We have now found the real root of this thread.  It might have
4237     ;; been gathered into some loose thread, so we have to search
4238     ;; through the threads to find the thread we wanted.
4239     (let ((threads gnus-newsgroup-threads)
4240           sub)
4241       (while threads
4242         (setq sub (car threads))
4243         (if (stringp (car sub))
4244             ;; This is a gathered thread, so we look at the roots
4245             ;; below it to find whether this article is in this
4246             ;; gathered root.
4247             (progn
4248               (setq sub (cdr sub))
4249               (while sub
4250                 (when (member (caar sub) headers)
4251                   (setq thread (car threads)
4252                         threads nil
4253                         sub nil))
4254                 (setq sub (cdr sub))))
4255           ;; It's an ordinary thread, so we check it.
4256           (when (eq (car sub) (car headers))
4257             (setq thread sub
4258                   threads nil)))
4259         (setq threads (cdr threads)))
4260       ;; If this article is in no thread, then it's a root.
4261       (if thread
4262           (unless dont-remove
4263             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4264         (setq thread (gnus-id-to-thread last-id)))
4265       (when thread
4266         (prog1
4267             thread                      ; We return this thread.
4268           (unless dont-remove
4269             (if (stringp (car thread))
4270                 (progn
4271                   ;; If we use dummy roots, then we have to remove the
4272                   ;; dummy root as well.
4273                   (when (eq gnus-summary-make-false-root 'dummy)
4274                     ;; We go to the dummy root by going to
4275                     ;; the first sub-"thread", and then one line up.
4276                     (gnus-summary-goto-article
4277                      (mail-header-number (caadr thread)))
4278                     (forward-line -1)
4279                     (gnus-delete-line)
4280                     (gnus-data-compute-positions))
4281                   (setq thread (cdr thread))
4282                   (while thread
4283                     (gnus-remove-thread-1 (car thread))
4284                     (setq thread (cdr thread))))
4285               (gnus-remove-thread-1 thread))))))))
4286
4287 (defun gnus-remove-thread-1 (thread)
4288   "Remove the thread THREAD recursively."
4289   (let ((number (mail-header-number (pop thread)))
4290         d)
4291     (setq thread (reverse thread))
4292     (while thread
4293       (gnus-remove-thread-1 (pop thread)))
4294     (when (setq d (gnus-data-find number))
4295       (goto-char (gnus-data-pos d))
4296       (gnus-summary-show-thread)
4297       (gnus-data-remove
4298        number
4299        (- (gnus-point-at-bol)
4300           (prog1
4301               (1+ (gnus-point-at-eol))
4302             (gnus-delete-line)))))))
4303
4304 (defun gnus-sort-threads-1 (threads func)
4305   (sort (mapcar (lambda (thread)
4306                   (cons (car thread)
4307                         (and (cdr thread)
4308                              (gnus-sort-threads-1 (cdr thread) func))))
4309                 threads) func))
4310
4311 (defun gnus-sort-threads (threads)
4312   "Sort THREADS."
4313   (if (not gnus-thread-sort-functions)
4314       threads
4315     (gnus-message 8 "Sorting threads...")
4316     (let ((max-lisp-eval-depth 5000))
4317       (prog1 (gnus-sort-threads-1
4318          threads
4319          (gnus-make-sort-function gnus-thread-sort-functions))
4320         (gnus-message 8 "Sorting threads...done")))))
4321
4322 (defun gnus-sort-articles (articles)
4323   "Sort ARTICLES."
4324   (when gnus-article-sort-functions
4325     (gnus-message 7 "Sorting articles...")
4326     (prog1
4327         (setq gnus-newsgroup-headers
4328               (sort articles (gnus-make-sort-function
4329                               gnus-article-sort-functions)))
4330       (gnus-message 7 "Sorting articles...done"))))
4331
4332 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4333 (defmacro gnus-thread-header (thread)
4334   "Return header of first article in THREAD.
4335 Note that THREAD must never, ever be anything else than a variable -
4336 using some other form will lead to serious barfage."
4337   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4338   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4339   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4340         (vector thread) 2))
4341
4342 (defsubst gnus-article-sort-by-number (h1 h2)
4343   "Sort articles by article number."
4344   (< (mail-header-number h1)
4345      (mail-header-number h2)))
4346
4347 (defun gnus-thread-sort-by-number (h1 h2)
4348   "Sort threads by root article number."
4349   (gnus-article-sort-by-number
4350    (gnus-thread-header h1) (gnus-thread-header h2)))
4351
4352 (defsubst gnus-article-sort-by-random (h1 h2)
4353   "Sort articles by article number."
4354   (zerop (random 2)))
4355
4356 (defun gnus-thread-sort-by-random (h1 h2)
4357   "Sort threads by root article number."
4358   (gnus-article-sort-by-random
4359    (gnus-thread-header h1) (gnus-thread-header h2)))
4360
4361 (defsubst gnus-article-sort-by-lines (h1 h2)
4362   "Sort articles by article Lines header."
4363   (< (mail-header-lines h1)
4364      (mail-header-lines h2)))
4365
4366 (defun gnus-thread-sort-by-lines (h1 h2)
4367   "Sort threads by root article Lines header."
4368   (gnus-article-sort-by-lines
4369    (gnus-thread-header h1) (gnus-thread-header h2)))
4370
4371 (defsubst gnus-article-sort-by-chars (h1 h2)
4372   "Sort articles by octet length."
4373   (< (mail-header-chars h1)
4374      (mail-header-chars h2)))
4375
4376 (defun gnus-thread-sort-by-chars (h1 h2)
4377   "Sort threads by root article octet length."
4378   (gnus-article-sort-by-chars
4379    (gnus-thread-header h1) (gnus-thread-header h2)))
4380
4381 (defsubst gnus-article-sort-by-author (h1 h2)
4382   "Sort articles by root author."
4383   (string-lessp
4384    (let ((addr (car (mime-entity-read-field h1 'From))))
4385      (or (std11-full-name-string addr)
4386          (std11-address-string addr)
4387          ""))
4388    (let ((addr (car (mime-entity-read-field h2 'From))))
4389      (or (std11-full-name-string addr)
4390          (std11-address-string addr)
4391          ""))
4392    ))
4393
4394 (defun gnus-thread-sort-by-author (h1 h2)
4395   "Sort threads by root author."
4396   (gnus-article-sort-by-author
4397    (gnus-thread-header h1)  (gnus-thread-header h2)))
4398
4399 (defsubst gnus-article-sort-by-subject (h1 h2)
4400   "Sort articles by root subject."
4401   (string-lessp
4402    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4403    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4404
4405 (defun gnus-thread-sort-by-subject (h1 h2)
4406   "Sort threads by root subject."
4407   (gnus-article-sort-by-subject
4408    (gnus-thread-header h1) (gnus-thread-header h2)))
4409
4410 (defsubst gnus-article-sort-by-date (h1 h2)
4411   "Sort articles by root article date."
4412   (time-less-p
4413    (gnus-date-get-time (mail-header-date h1))
4414    (gnus-date-get-time (mail-header-date h2))))
4415
4416 (defun gnus-thread-sort-by-date (h1 h2)
4417   "Sort threads by root article date."
4418   (gnus-article-sort-by-date
4419    (gnus-thread-header h1) (gnus-thread-header h2)))
4420
4421 (defsubst gnus-article-sort-by-score (h1 h2)
4422   "Sort articles by root article score.
4423 Unscored articles will be counted as having a score of zero."
4424   (> (or (cdr (assq (mail-header-number h1)
4425                     gnus-newsgroup-scored))
4426          gnus-summary-default-score 0)
4427      (or (cdr (assq (mail-header-number h2)
4428                     gnus-newsgroup-scored))
4429          gnus-summary-default-score 0)))
4430
4431 (defun gnus-thread-sort-by-score (h1 h2)
4432   "Sort threads by root article score."
4433   (gnus-article-sort-by-score
4434    (gnus-thread-header h1) (gnus-thread-header h2)))
4435
4436 (defun gnus-thread-sort-by-total-score (h1 h2)
4437   "Sort threads by the sum of all scores in the thread.
4438 Unscored articles will be counted as having a score of zero."
4439   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4440
4441 (defun gnus-thread-total-score (thread)
4442   ;; This function find the total score of THREAD.
4443   (cond
4444    ((null thread)
4445     0)
4446    ((consp thread)
4447     (if (stringp (car thread))
4448         (apply gnus-thread-score-function 0
4449                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4450       (gnus-thread-total-score-1 thread)))
4451    (t
4452     (gnus-thread-total-score-1 (list thread)))))
4453
4454 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4455   "Sort threads such that the thread with the most recently arrived article comes first."
4456   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4457
4458 (defun gnus-thread-highest-number (thread)
4459   "Return the highest article number in THREAD."
4460   (apply 'max (mapcar (lambda (header)
4461                         (mail-header-number header))
4462                       (message-flatten-list thread))))
4463
4464 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4465   "Sort threads such that the thread with the most recently dated article comes first."
4466   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4467
4468 (defun gnus-thread-latest-date (thread)
4469   "Return the highest article date in THREAD."
4470   (let ((previous-time 0))
4471     (apply 'max
4472            (mapcar
4473             (lambda (header)
4474               (setq previous-time
4475                     (time-to-seconds
4476                      (condition-case ()
4477                          (mail-header-parse-date (mail-header-date header))
4478                        (error previous-time)))))
4479             (sort
4480              (message-flatten-list thread)
4481              (lambda (h1 h2)
4482                (< (mail-header-number h1)
4483                   (mail-header-number h2))))))))
4484
4485 (defun gnus-thread-total-score-1 (root)
4486   ;; This function find the total score of the thread below ROOT.
4487   (setq root (car root))
4488   (apply gnus-thread-score-function
4489          (or (append
4490               (mapcar 'gnus-thread-total-score
4491                       (cdr (gnus-id-to-thread (mail-header-id root))))
4492               (when (> (mail-header-number root) 0)
4493                 (list (or (cdr (assq (mail-header-number root)
4494                                      gnus-newsgroup-scored))
4495                           gnus-summary-default-score 0))))
4496              (list gnus-summary-default-score)
4497              '(0))))
4498
4499 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4500 (defvar gnus-tmp-prev-subject nil)
4501 (defvar gnus-tmp-false-parent nil)
4502 (defvar gnus-tmp-root-expunged nil)
4503 (defvar gnus-tmp-dummy-line nil)
4504
4505 (eval-when-compile (defvar gnus-tmp-header))
4506 (defun gnus-extra-header (type &optional header)
4507   "Return the extra header of TYPE."
4508   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4509       ""))
4510
4511 (defvar gnus-tmp-thread-tree-header-string "")
4512
4513 (defcustom gnus-sum-thread-tree-root "> "
4514   "With %B spec, used for the root of a thread.
4515 If nil, use subject instead."
4516   :type 'string
4517   :group 'gnus-thread)
4518 (defcustom gnus-sum-thread-tree-single-indent ""
4519   "With %B spec, used for a thread with just one message.
4520 If nil, use subject instead."
4521   :type 'string
4522   :group 'gnus-thread)
4523 (defcustom gnus-sum-thread-tree-vertical "| "
4524   "With %B spec, used for drawing a vertical line."
4525   :type 'string
4526   :group 'gnus-thread)
4527 (defcustom gnus-sum-thread-tree-indent "  "
4528   "With %B spec, used for indenting."
4529   :type 'string
4530   :group 'gnus-thread)
4531 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4532   "With %B spec, used for a leaf with brothers."
4533   :type 'string
4534   :group 'gnus-thread)
4535 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4536   "With %B spec, used for a leaf without brothers."
4537   :type 'string
4538   :group 'gnus-thread)
4539
4540 (defun gnus-summary-prepare-threads (threads)
4541   "Prepare summary buffer from THREADS and indentation LEVEL.
4542 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4543 or a straight list of headers."
4544   (gnus-message 7 "Generating summary...")
4545
4546   (setq gnus-newsgroup-threads threads)
4547   (beginning-of-line)
4548
4549   (let ((gnus-tmp-level 0)
4550         (default-score (or gnus-summary-default-score 0))
4551         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4552         (building-line-count gnus-summary-display-while-building)
4553         (building-count (integerp gnus-summary-display-while-building))
4554         thread number subject stack state gnus-tmp-gathered beg-match
4555         new-roots gnus-tmp-new-adopts thread-end simp-subject
4556         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4557         gnus-tmp-replied gnus-tmp-subject-or-nil
4558         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4559         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4560         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4561         tree-stack)
4562
4563     (setq gnus-tmp-prev-subject nil)
4564
4565     (if (vectorp (car threads))
4566         ;; If this is a straight (sic) list of headers, then a
4567         ;; threaded summary display isn't required, so we just create
4568         ;; an unthreaded one.
4569         (gnus-summary-prepare-unthreaded threads)
4570
4571       ;; Do the threaded display.
4572
4573       (if gnus-summary-display-while-building
4574           (switch-to-buffer (buffer-name)))
4575       (while (or threads stack gnus-tmp-new-adopts new-roots)
4576
4577         (if (and (= gnus-tmp-level 0)
4578                  (or (not stack)
4579                      (= (caar stack) 0))
4580                  (not gnus-tmp-false-parent)
4581                  (or gnus-tmp-new-adopts new-roots))
4582             (if gnus-tmp-new-adopts
4583                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4584                       thread (list (car gnus-tmp-new-adopts))
4585                       gnus-tmp-header (caar thread)
4586                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4587               (when new-roots
4588                 (setq thread (list (car new-roots))
4589                       gnus-tmp-header (caar thread)
4590                       new-roots (cdr new-roots))))
4591
4592           (if threads
4593               ;; If there are some threads, we do them before the
4594               ;; threads on the stack.
4595               (setq thread threads
4596                     gnus-tmp-header (caar thread))
4597             ;; There were no current threads, so we pop something off
4598             ;; the stack.
4599             (setq state (car stack)
4600                   gnus-tmp-level (car state)
4601                   tree-stack (cadr state)
4602                   thread (caddr state)
4603                   stack (cdr stack)
4604                   gnus-tmp-header (caar thread))))
4605
4606         (setq gnus-tmp-false-parent nil)
4607         (setq gnus-tmp-root-expunged nil)
4608         (setq thread-end nil)
4609
4610         (if (stringp gnus-tmp-header)
4611             ;; The header is a dummy root.
4612             (cond
4613              ((eq gnus-summary-make-false-root 'adopt)
4614               ;; We let the first article adopt the rest.
4615               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4616                                                (cddar thread)))
4617               (setq gnus-tmp-gathered
4618                     (nconc (mapcar
4619                             (lambda (h) (mail-header-number (car h)))
4620                             (cddar thread))
4621                            gnus-tmp-gathered))
4622               (setq thread (cons (list (caar thread)
4623                                        (cadar thread))
4624                                  (cdr thread)))
4625               (setq gnus-tmp-level -1
4626                     gnus-tmp-false-parent t))
4627              ((eq gnus-summary-make-false-root 'empty)
4628               ;; We print adopted articles with empty subject fields.
4629               (setq gnus-tmp-gathered
4630                     (nconc (mapcar
4631                             (lambda (h) (mail-header-number (car h)))
4632                             (cddar thread))
4633                            gnus-tmp-gathered))
4634               (setq gnus-tmp-level -1))
4635              ((eq gnus-summary-make-false-root 'dummy)
4636               ;; We remember that we probably want to output a dummy
4637               ;; root.
4638               (setq gnus-tmp-dummy-line gnus-tmp-header)
4639               (setq gnus-tmp-prev-subject gnus-tmp-header))
4640              (t
4641               ;; We do not make a root for the gathered
4642               ;; sub-threads at all.
4643               (setq gnus-tmp-level -1)))
4644
4645           (setq number (mail-header-number gnus-tmp-header)
4646                 subject (mail-header-subject gnus-tmp-header)
4647                 simp-subject (gnus-simplify-subject-fully subject))
4648
4649           (cond
4650            ;; If the thread has changed subject, we might want to make
4651            ;; this subthread into a root.
4652            ((and (null gnus-thread-ignore-subject)
4653                  (not (zerop gnus-tmp-level))
4654                  gnus-tmp-prev-subject
4655                  (not (string= gnus-tmp-prev-subject simp-subject)))
4656             (setq new-roots (nconc new-roots (list (car thread)))
4657                   thread-end t
4658                   gnus-tmp-header nil))
4659            ;; If the article lies outside the current limit,
4660            ;; then we do not display it.
4661            ((not (memq number gnus-newsgroup-limit))
4662             (setq gnus-tmp-gathered
4663                   (nconc (mapcar
4664                           (lambda (h) (mail-header-number (car h)))
4665                           (cdar thread))
4666                          gnus-tmp-gathered))
4667             (setq gnus-tmp-new-adopts (if (cdar thread)
4668                                           (append gnus-tmp-new-adopts
4669                                                   (cdar thread))
4670                                         gnus-tmp-new-adopts)
4671                   thread-end t
4672                   gnus-tmp-header nil)
4673             (when (zerop gnus-tmp-level)
4674               (setq gnus-tmp-root-expunged t)))
4675            ;; Perhaps this article is to be marked as read?
4676            ((and gnus-summary-mark-below
4677                  (< (or (cdr (assq number gnus-newsgroup-scored))
4678                         default-score)
4679                     gnus-summary-mark-below)
4680                  ;; Don't touch sparse articles.
4681                  (not (gnus-summary-article-sparse-p number))
4682                  (not (gnus-summary-article-ancient-p number)))
4683             (setq gnus-newsgroup-unreads
4684                   (delq number gnus-newsgroup-unreads))
4685             (if gnus-newsgroup-auto-expire
4686                 (setq gnus-newsgroup-expirable
4687                       (gnus-add-to-sorted-list
4688                        gnus-newsgroup-expirable number))
4689               (push (cons number gnus-low-score-mark)
4690                     gnus-newsgroup-reads))))
4691
4692           (when gnus-tmp-header
4693             ;; We may have an old dummy line to output before this
4694             ;; article.
4695             (when (and gnus-tmp-dummy-line
4696                        (gnus-subject-equal
4697                         gnus-tmp-dummy-line
4698                         (mail-header-subject gnus-tmp-header)))
4699               (gnus-summary-insert-dummy-line
4700                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4701               (setq gnus-tmp-dummy-line nil))
4702
4703             ;; Compute the mark.
4704             (setq gnus-tmp-unread (gnus-article-mark number))
4705
4706             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4707                                   gnus-tmp-header gnus-tmp-level)
4708                   gnus-newsgroup-data)
4709
4710             ;; Actually insert the line.
4711             (setq
4712              gnus-tmp-subject-or-nil
4713              (cond
4714               ((and gnus-thread-ignore-subject
4715                     gnus-tmp-prev-subject
4716                     (not (string= gnus-tmp-prev-subject simp-subject)))
4717                subject)
4718               ((zerop gnus-tmp-level)
4719                (if (and (eq gnus-summary-make-false-root 'empty)
4720                         (memq number gnus-tmp-gathered)
4721                         gnus-tmp-prev-subject
4722                         (string= gnus-tmp-prev-subject simp-subject))
4723                    gnus-summary-same-subject
4724                  subject))
4725               (t gnus-summary-same-subject)))
4726             (if (and (eq gnus-summary-make-false-root 'adopt)
4727                      (= gnus-tmp-level 1)
4728                      (memq number gnus-tmp-gathered))
4729                 (setq gnus-tmp-opening-bracket ?\<
4730                       gnus-tmp-closing-bracket ?\>)
4731               (setq gnus-tmp-opening-bracket ?\[
4732                     gnus-tmp-closing-bracket ?\]))
4733             (setq
4734              gnus-tmp-indentation
4735              (aref gnus-thread-indent-array gnus-tmp-level)
4736              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4737              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4738                                 gnus-summary-default-score 0)
4739              gnus-tmp-score-char
4740              (if (or (null gnus-summary-default-score)
4741                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4742                          gnus-summary-zcore-fuzz))
4743                  ?\ ;;;Whitespace
4744                (if (< gnus-tmp-score gnus-summary-default-score)
4745                    gnus-score-below-mark gnus-score-over-mark))
4746              gnus-tmp-replied
4747              (cond ((memq number gnus-newsgroup-processable)
4748                     gnus-process-mark)
4749                    ((memq number gnus-newsgroup-cached)
4750                     gnus-cached-mark)
4751                    ((memq number gnus-newsgroup-replied)
4752                     gnus-replied-mark)
4753                    ((memq number gnus-newsgroup-forwarded)
4754                     gnus-forwarded-mark)
4755                    ((memq number gnus-newsgroup-saved)
4756                     gnus-saved-mark)
4757                    ((memq number gnus-newsgroup-recent)
4758                     gnus-recent-mark)
4759                    ((memq number gnus-newsgroup-unseen)
4760                     gnus-unseen-mark)
4761                    (t gnus-no-mark))
4762              gnus-tmp-downloaded
4763              (cond ((memq number gnus-newsgroup-undownloaded) 
4764                     gnus-undownloaded-mark)
4765                    (gnus-newsgroup-agentized
4766                     gnus-downloaded-mark)
4767                    (t
4768                     gnus-no-mark))
4769              gnus-tmp-from (mail-header-from gnus-tmp-header)
4770              gnus-tmp-name
4771              (cond
4772               ((string-match "<[^>]+> *$" gnus-tmp-from)
4773                (setq beg-match (match-beginning 0))
4774                (or (and (string-match "^\".+\"" gnus-tmp-from)
4775                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4776                    (substring gnus-tmp-from 0 beg-match)))
4777               ((string-match "(.+)" gnus-tmp-from)
4778                (substring gnus-tmp-from
4779                           (1+ (match-beginning 0)) (1- (match-end 0))))
4780               (t gnus-tmp-from))
4781              gnus-tmp-thread-tree-header-string
4782              (cond
4783               ((not gnus-show-threads) "")
4784               ((zerop gnus-tmp-level)
4785                (if (cdar thread)
4786                    (or gnus-sum-thread-tree-root subject)
4787                  (or gnus-sum-thread-tree-single-indent subject)))
4788               (t
4789                (concat (apply 'concat
4790                               (mapcar (lambda (item)
4791                                         (if (= item 1)
4792                                             gnus-sum-thread-tree-vertical
4793                                           gnus-sum-thread-tree-indent))
4794                                       (cdr (reverse tree-stack))))
4795                        (if (nth 1 thread)
4796                            gnus-sum-thread-tree-leaf-with-other
4797                          gnus-sum-thread-tree-single-leaf)))))
4798             (when (string= gnus-tmp-name "")
4799               (setq gnus-tmp-name gnus-tmp-from))
4800             (unless (numberp gnus-tmp-lines)
4801               (setq gnus-tmp-lines -1))
4802             (if (= gnus-tmp-lines -1)
4803                 (setq gnus-tmp-lines "?")
4804               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4805             (gnus-put-text-property
4806              (point)
4807              (progn (eval gnus-summary-line-format-spec) (point))
4808              'gnus-number number)
4809             (when gnus-visual-p
4810               (forward-line -1)
4811               (gnus-run-hooks 'gnus-summary-update-hook)
4812               (forward-line 1))
4813
4814             (setq gnus-tmp-prev-subject simp-subject)))
4815
4816         (when (nth 1 thread)
4817           (push (list (max 0 gnus-tmp-level)
4818                       (copy-list tree-stack)
4819                       (nthcdr 1 thread))
4820                 stack))
4821         (push (if (nth 1 thread) 1 0) tree-stack)
4822         (incf gnus-tmp-level)
4823         (setq threads (if thread-end nil (cdar thread)))
4824         (if gnus-summary-display-while-building
4825             (if building-count
4826                 (progn
4827                   ;; use a set frequency
4828                   (setq building-line-count (1- building-line-count))
4829                   (when (= building-line-count 0)
4830                     (sit-for 0)
4831                     (setq building-line-count
4832                           gnus-summary-display-while-building)))
4833               ;; always
4834               (sit-for 0)))
4835         (unless threads
4836           (setq gnus-tmp-level 0)))))
4837   (gnus-message 7 "Generating summary...done"))
4838
4839 (defun gnus-summary-prepare-unthreaded (headers)
4840   "Generate an unthreaded summary buffer based on HEADERS."
4841   (let (header number mark)
4842
4843     (beginning-of-line)
4844
4845     (while headers
4846       ;; We may have to root out some bad articles...
4847       (when (memq (setq number (mail-header-number
4848                                 (setq header (pop headers))))
4849                   gnus-newsgroup-limit)
4850         ;; Mark article as read when it has a low score.
4851         (when (and gnus-summary-mark-below
4852                    (< (or (cdr (assq number gnus-newsgroup-scored))
4853                           gnus-summary-default-score 0)
4854                       gnus-summary-mark-below)
4855                    (not (gnus-summary-article-ancient-p number)))
4856           (setq gnus-newsgroup-unreads
4857                 (delq number gnus-newsgroup-unreads))
4858           (if gnus-newsgroup-auto-expire
4859               (push number gnus-newsgroup-expirable)
4860             (push (cons number gnus-low-score-mark)
4861                   gnus-newsgroup-reads)))
4862
4863         (setq mark (gnus-article-mark number))
4864         (push (gnus-data-make number mark (1+ (point)) header 0)
4865               gnus-newsgroup-data)
4866         (gnus-summary-insert-line
4867          header 0 number
4868          (memq number gnus-newsgroup-undownloaded)
4869          mark (memq number gnus-newsgroup-replied)
4870          (memq number gnus-newsgroup-expirable)
4871          (mail-header-subject header) nil
4872          (cdr (assq number gnus-newsgroup-scored))
4873          (memq number gnus-newsgroup-processable))))))
4874
4875 (defun gnus-summary-remove-list-identifiers ()
4876   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4877   (let ((regexp (if (consp gnus-list-identifiers)
4878                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4879                   gnus-list-identifiers))
4880         changed subject)
4881     (when regexp
4882       (dolist (header gnus-newsgroup-headers)
4883         (setq subject (mail-header-subject header)
4884               changed nil)
4885         (while (string-match
4886                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4887                 subject)
4888           (setq subject
4889                 (concat (substring subject 0 (match-beginning 2))
4890                         (substring subject (match-end 0)))
4891                 changed t))
4892         (when (and changed
4893                    (string-match
4894                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4895           (setq subject
4896                 (concat (substring subject 0 (match-beginning 1))
4897                         (substring subject (match-end 1)))))
4898         (when changed
4899           (mail-header-set-subject header subject))))))
4900
4901 (defun gnus-fetch-headers (articles)
4902   "Fetch headers of ARTICLES."
4903   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4904     (gnus-message 5 "Fetching headers for %s..." name)
4905     (prog1
4906         (if (eq 'nov
4907                 (setq gnus-headers-retrieved-by
4908                       (gnus-retrieve-headers
4909                        articles gnus-newsgroup-name
4910                        ;; We might want to fetch old headers, but
4911                        ;; not if there is only 1 article.
4912                        (and (or (and
4913                                  (not (eq gnus-fetch-old-headers 'some))
4914                                  (not (numberp gnus-fetch-old-headers)))
4915                                 (> (length articles) 1))
4916                             gnus-fetch-old-headers))))
4917             (gnus-get-newsgroup-headers-xover
4918              articles nil nil gnus-newsgroup-name t)
4919           (gnus-get-newsgroup-headers))
4920       (gnus-message 5 "Fetching headers for %s...done" name))))
4921
4922 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4923   "Select newsgroup GROUP.
4924 If READ-ALL is non-nil, all articles in the group are selected.
4925 If SELECT-ARTICLES, only select those articles from GROUP."
4926   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4927          ;;!!! Dirty hack; should be removed.
4928          (gnus-summary-ignore-duplicates
4929           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4930               t
4931             gnus-summary-ignore-duplicates))
4932          (info (nth 2 entry))
4933          articles fetched-articles cached)
4934
4935     (unless (gnus-check-server
4936              (set (make-local-variable 'gnus-current-select-method)
4937                   (gnus-find-method-for-group group)))
4938       (error "Couldn't open server"))
4939
4940     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4941         (gnus-activate-group group)     ; Or we can activate it...
4942         (progn                          ; Or we bug out.
4943           (when (equal major-mode 'gnus-summary-mode)
4944             (kill-buffer (current-buffer)))
4945           (error "Couldn't activate group %s: %s"
4946                  group (gnus-status-message group))))
4947
4948     (unless (gnus-request-group group t)
4949       (when (equal major-mode 'gnus-summary-mode)
4950         (kill-buffer (current-buffer)))
4951       (error "Couldn't request group %s: %s"
4952              group (gnus-status-message group)))
4953
4954     (setq gnus-newsgroup-name group
4955           gnus-newsgroup-unselected nil
4956           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4957
4958     (let ((display (gnus-group-find-parameter group 'display)))
4959       (setq gnus-newsgroup-display
4960             (cond
4961              ((not (zerop (or (car-safe read-all) 0)))
4962               ;; The user entered the group with C-u SPC/RET, let's show
4963               ;; all articles.
4964               'gnus-not-ignore)
4965              ((eq display 'all)
4966               'gnus-not-ignore)
4967              ((arrayp display)
4968               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4969              ((numberp display)
4970               ;; The following is probably the "correct" solution, but
4971               ;; it makes Gnus fetch all headers and then limit the
4972               ;; articles (which is slow), so instead we hack the
4973               ;; select-articles parameter instead. -- Simon Josefsson
4974               ;; <jas@kth.se>
4975               ;;
4976               ;; (gnus-byte-compile
4977               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4978               ;;                         display)))))
4979               (setq select-articles
4980                     (gnus-uncompress-range
4981                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4982                              (if (> tmp 0)
4983                                  tmp
4984                                1))
4985                            (cdr (gnus-active group)))))
4986               nil)
4987              (t
4988               nil))))
4989
4990     (gnus-summary-setup-default-charset)
4991
4992     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4993     (when (gnus-virtual-group-p group)
4994       (setq cached gnus-newsgroup-cached))
4995
4996     (setq gnus-newsgroup-unreads
4997           (gnus-sorted-ndifference
4998            (gnus-sorted-ndifference gnus-newsgroup-unreads
4999                                     gnus-newsgroup-marked)
5000            gnus-newsgroup-dormant))
5001
5002     (setq gnus-newsgroup-processable nil)
5003
5004     (gnus-update-read-articles group gnus-newsgroup-unreads)
5005
5006     ;; Adjust and set lists of article marks.
5007     (when info
5008       (gnus-adjust-marked-articles info))
5009     (if (setq articles select-articles)
5010         (setq gnus-newsgroup-unselected
5011               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5012       (setq articles (gnus-articles-to-read group read-all)))
5013
5014     (cond
5015      ((null articles)
5016       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5017       'quit)
5018      ((eq articles 0) nil)
5019      (t
5020       ;; Init the dependencies hash table.
5021       (setq gnus-newsgroup-dependencies
5022             (gnus-make-hashtable (length articles)))
5023       (gnus-set-global-variables)
5024       ;; Retrieve the headers and read them in.
5025
5026       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5027
5028       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5029       (when cached
5030         (setq gnus-newsgroup-cached cached))
5031
5032       ;; Suppress duplicates?
5033       (when gnus-suppress-duplicates
5034         (gnus-dup-suppress-articles))
5035
5036       ;; Set the initial limit.
5037       (setq gnus-newsgroup-limit (copy-sequence articles))
5038       ;; Remove canceled articles from the list of unread articles.
5039       (setq fetched-articles
5040             (mapcar (lambda (headers) (mail-header-number headers))
5041                     gnus-newsgroup-headers))
5042       (setq gnus-newsgroup-articles fetched-articles)
5043       (setq gnus-newsgroup-unreads
5044             (gnus-sorted-nintersection
5045              gnus-newsgroup-unreads fetched-articles))
5046       (gnus-compute-unseen-list)
5047
5048       ;; Removed marked articles that do not exist.
5049       (gnus-update-missing-marks
5050        (gnus-sorted-difference articles fetched-articles))
5051       ;; We might want to build some more threads first.
5052       (when (and gnus-fetch-old-headers
5053                  (eq gnus-headers-retrieved-by 'nov))
5054         (if (eq gnus-fetch-old-headers 'invisible)
5055             (gnus-build-all-threads)
5056           (gnus-build-old-threads)))
5057       ;; Let the Gnus agent mark articles as read.
5058       (when gnus-agent
5059         (gnus-agent-get-undownloaded-list))
5060       ;; Remove list identifiers from subject
5061       (when gnus-list-identifiers
5062         (gnus-summary-remove-list-identifiers))
5063       ;; Check whether auto-expire is to be done in this group.
5064       (setq gnus-newsgroup-auto-expire
5065             (gnus-group-auto-expirable-p group))
5066       ;; Set up the article buffer now, if necessary.
5067       (unless gnus-single-article-buffer
5068         (gnus-article-setup-buffer))
5069       ;; First and last article in this newsgroup.
5070       (when gnus-newsgroup-headers
5071         (setq gnus-newsgroup-begin
5072               (mail-header-number (car gnus-newsgroup-headers))
5073               gnus-newsgroup-end
5074               (mail-header-number
5075                (gnus-last-element gnus-newsgroup-headers))))
5076       ;; GROUP is successfully selected.
5077       (or gnus-newsgroup-headers t)))))
5078
5079 (defun gnus-compute-unseen-list ()
5080   ;; The `seen' marks are treated specially.
5081   (if (not gnus-newsgroup-seen)
5082       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5083     (setq gnus-newsgroup-unseen
5084           (gnus-inverse-list-range-intersection
5085            gnus-newsgroup-articles gnus-newsgroup-seen))))
5086
5087 (defun gnus-summary-display-make-predicate (display)
5088   (require 'gnus-agent)
5089   (when (= (length display) 1)
5090     (setq display (car display)))
5091   (unless gnus-summary-display-cache
5092     (dolist (elem (append '((unread . unread)
5093                             (read . read)
5094                             (unseen . unseen))
5095                           gnus-article-mark-lists))
5096       (push (cons (cdr elem)
5097                   (gnus-byte-compile
5098                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5099             gnus-summary-display-cache)))
5100   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5101         (gnus-category-predicate-cache gnus-summary-display-cache))
5102     (gnus-get-predicate display)))
5103
5104 ;; Uses the dynamically bound `number' variable.
5105 (eval-when-compile
5106   (defvar number))
5107 (defun gnus-article-marked-p (type &optional article)
5108   (let ((article (or article number)))
5109     (cond
5110      ((eq type 'tick)
5111       (memq article gnus-newsgroup-marked))
5112      ((eq type 'spam)
5113       (memq article gnus-newsgroup-spam-marked))
5114      ((eq type 'unsend)
5115       (memq article gnus-newsgroup-unsendable))
5116      ((eq type 'undownload)
5117       (memq article gnus-newsgroup-undownloaded))
5118      ((eq type 'download)
5119       (memq article gnus-newsgroup-downloadable))
5120      ((eq type 'unread)
5121       (memq article gnus-newsgroup-unreads))
5122      ((eq type 'read)
5123       (memq article gnus-newsgroup-reads))
5124      ((eq type 'dormant)
5125       (memq article gnus-newsgroup-dormant) )
5126      ((eq type 'expire)
5127       (memq article gnus-newsgroup-expirable))
5128      ((eq type 'reply)
5129       (memq article gnus-newsgroup-replied))
5130      ((eq type 'killed)
5131       (memq article gnus-newsgroup-killed))
5132      ((eq type 'bookmark)
5133       (assq article gnus-newsgroup-bookmarks))
5134      ((eq type 'score)
5135       (assq article gnus-newsgroup-scored))
5136      ((eq type 'save)
5137       (memq article gnus-newsgroup-saved))
5138      ((eq type 'cache)
5139       (memq article gnus-newsgroup-cached))
5140      ((eq type 'forward)
5141       (memq article gnus-newsgroup-forwarded))
5142      ((eq type 'seen)
5143       (not (memq article gnus-newsgroup-unseen)))
5144      ((eq type 'recent)
5145       (memq article gnus-newsgroup-recent))
5146      (t t))))
5147
5148 (defun gnus-articles-to-read (group &optional read-all)
5149   "Find out what articles the user wants to read."
5150   (let* ((display (gnus-group-find-parameter group 'display))
5151          (articles
5152           ;; Select all articles if `read-all' is non-nil, or if there
5153           ;; are no unread articles.
5154           (if (or read-all
5155                   (and (zerop (length gnus-newsgroup-marked))
5156                        (zerop (length gnus-newsgroup-unreads)))
5157                   ;; Fetch all if the predicate is non-nil.
5158                   gnus-newsgroup-display)
5159               ;; We want to select the headers for all the articles in
5160               ;; the group, so we select either all the active
5161               ;; articles in the group, or (if that's nil), the
5162               ;; articles in the cache.
5163               (or
5164                (gnus-uncompress-range (gnus-active group))
5165                (gnus-cache-articles-in-group group))
5166             ;; Select only the "normal" subset of articles.
5167             (gnus-sorted-nunion
5168              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5169              gnus-newsgroup-unreads)))
5170          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5171          (scored (length scored-list))
5172          (number (length articles))
5173          (marked (+ (length gnus-newsgroup-marked)
5174                     (length gnus-newsgroup-dormant)))
5175          (select
5176           (cond
5177            ((numberp read-all)
5178             read-all)
5179            ((numberp gnus-newsgroup-display)
5180             gnus-newsgroup-display)
5181            (t
5182             (condition-case ()
5183                 (cond
5184                  ((and (or (<= scored marked) (= scored number))
5185                        (numberp gnus-large-newsgroup)
5186                        (> number gnus-large-newsgroup))
5187                   (let* ((cursor-in-echo-area nil)
5188                          (initial (gnus-parameter-large-newsgroup-initial
5189                                    gnus-newsgroup-name))
5190                          (input
5191                           (read-string
5192                            (format
5193                             "How many articles from %s (%s %d): "
5194                             (gnus-limit-string
5195                              (gnus-group-decoded-name gnus-newsgroup-name)
5196                              35)
5197                             (if initial "max" "default")
5198                             number)
5199                            (if initial
5200                                (cons (number-to-string initial)
5201                                      0)))))
5202                     (if (string-match "^[ \t]*$" input) number input)))
5203                  ((and (> scored marked) (< scored number)
5204                        (> (- scored number) 20))
5205                   (let ((input
5206                          (read-string
5207                           (format "%s %s (%d scored, %d total): "
5208                                   "How many articles from"
5209                                   (gnus-group-decoded-name group)
5210                                   scored number))))
5211                     (if (string-match "^[ \t]*$" input)
5212                         number input)))
5213                  (t number))
5214               (quit
5215                (message "Quit getting the articles to read")
5216                nil))))))
5217     (setq select (if (stringp select) (string-to-number select) select))
5218     (if (or (null select) (zerop select))
5219         select
5220       (if (and (not (zerop scored)) (<= (abs select) scored))
5221           (progn
5222             (setq articles (sort scored-list '<))
5223             (setq number (length articles)))
5224         (setq articles (copy-sequence articles)))
5225
5226       (when (< (abs select) number)
5227         (if (< select 0)
5228             ;; Select the N oldest articles.
5229             (setcdr (nthcdr (1- (abs select)) articles) nil)
5230           ;; Select the N most recent articles.
5231           (setq articles (nthcdr (- number select) articles))))
5232       (setq gnus-newsgroup-unselected
5233             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5234       (when gnus-alter-articles-to-read-function
5235         (setq articles
5236               (sort
5237                (funcall gnus-alter-articles-to-read-function
5238                         gnus-newsgroup-name articles)
5239                '<)))
5240       articles)))
5241
5242 (defun gnus-killed-articles (killed articles)
5243   (let (out)
5244     (while articles
5245       (when (inline (gnus-member-of-range (car articles) killed))
5246         (push (car articles) out))
5247       (setq articles (cdr articles)))
5248     out))
5249
5250 (defun gnus-uncompress-marks (marks)
5251   "Uncompress the mark ranges in MARKS."
5252   (let ((uncompressed '(score bookmark))
5253         out)
5254     (while marks
5255       (if (memq (caar marks) uncompressed)
5256           (push (car marks) out)
5257         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5258       (setq marks (cdr marks)))
5259     out))
5260
5261 (defun gnus-article-mark-to-type (mark)
5262   "Return the type of MARK."
5263   (or (cadr (assq mark gnus-article-special-mark-lists))
5264       'list))
5265
5266 (defun gnus-article-unpropagatable-p (mark)
5267   "Return whether MARK should be propagated to backend."
5268   (memq mark gnus-article-unpropagated-mark-lists))
5269
5270 (defun gnus-adjust-marked-articles (info)
5271   "Set all article lists and remove all marks that are no longer valid."
5272   (let* ((marked-lists (gnus-info-marks info))
5273          (active (gnus-active (gnus-info-group info)))
5274          (min (car active))
5275          (max (cdr active))
5276          (types gnus-article-mark-lists)
5277          marks var articles article mark mark-type)
5278
5279     (dolist (marks marked-lists)
5280       (setq mark (car marks)
5281             mark-type (gnus-article-mark-to-type mark)
5282             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5283
5284       ;; We set the variable according to the type of the marks list,
5285       ;; and then adjust the marks to a subset of the active articles.
5286       (cond
5287        ;; Adjust "simple" lists.
5288        ((eq mark-type 'list)
5289         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5290         (when (memq mark '(tick dormant expire reply save))
5291           (while articles
5292             (when (or (< (setq article (pop articles)) min) (> article max))
5293               (set var (delq article (symbol-value var)))))))
5294        ;; Adjust assocs.
5295        ((eq mark-type 'tuple)
5296         (set var (setq articles (cdr marks)))
5297         (when (not (listp (cdr (symbol-value var))))
5298           (set var (list (symbol-value var))))
5299         (when (not (listp (cdr articles)))
5300           (setq articles (list articles)))
5301         (while articles
5302           (when (or (not (consp (setq article (pop articles))))
5303                     (< (car article) min)
5304                     (> (car article) max))
5305             (set var (delq article (symbol-value var))))))
5306        ;; Adjust ranges (sloppily).
5307        ((eq mark-type 'range)
5308         (cond
5309          ((eq mark 'seen)
5310           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5311           ;; It should be (seen (NUM1 . NUM2)).
5312           (when (numberp (cddr marks))
5313             (setcdr marks (list (cdr marks))))
5314           (setq articles (cdr marks))
5315           (while (and articles
5316                       (or (and (consp (car articles))
5317                                (> min (cdar articles)))
5318                           (and (numberp (car articles))
5319                                (> min (car articles)))))
5320             (pop articles))
5321           (set var articles))))))))
5322
5323 (defun gnus-update-missing-marks (missing)
5324   "Go through the list of MISSING articles and remove them from the mark lists."
5325   (when missing
5326     (let (var m)
5327       ;; Go through all types.
5328       (dolist (elem gnus-article-mark-lists)
5329         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5330           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5331           (when (symbol-value var)
5332             ;; This list has articles.  So we delete all missing
5333             ;; articles from it.
5334             (setq m missing)
5335             (while m
5336               (set var (delq (pop m) (symbol-value var))))))))))
5337
5338 (defun gnus-update-marks ()
5339   "Enter the various lists of marked articles into the newsgroup info list."
5340   (let ((types gnus-article-mark-lists)
5341         (info (gnus-get-info gnus-newsgroup-name))
5342         type list newmarked symbol delta-marks)
5343     (when info
5344       ;; Add all marks lists to the list of marks lists.
5345       (while (setq type (pop types))
5346         (setq list (symbol-value
5347                     (setq symbol
5348                           (intern (format "gnus-newsgroup-%s" (car type))))))
5349
5350         (when list
5351           ;; Get rid of the entries of the articles that have the
5352           ;; default score.
5353           (when (and (eq (cdr type) 'score)
5354                      gnus-save-score
5355                      list)
5356             (let* ((arts list)
5357                    (prev (cons nil list))
5358                    (all prev))
5359               (while arts
5360                 (if (or (not (consp (car arts)))
5361                         (= (cdar arts) gnus-summary-default-score))
5362                     (setcdr prev (cdr arts))
5363                   (setq prev arts))
5364                 (setq arts (cdr arts)))
5365               (setq list (cdr all)))))
5366
5367         (when (eq (cdr type) 'seen)
5368           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5369
5370         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5371           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5372
5373         (when (and (gnus-check-backend-function
5374                     'request-set-mark gnus-newsgroup-name)
5375                    (not (gnus-article-unpropagatable-p (cdr type))))
5376           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5377                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5378                  (add (gnus-remove-from-range
5379                        (gnus-copy-sequence list) old)))
5380             (when add
5381               (push (list add 'add (list (cdr type))) delta-marks))
5382             (when del
5383               (push (list del 'del (list (cdr type))) delta-marks))))
5384
5385         (when list
5386           (push (cons (cdr type) list) newmarked)))
5387
5388       (when delta-marks
5389         (unless (gnus-check-group gnus-newsgroup-name)
5390           (error "Can't open server for %s" gnus-newsgroup-name))
5391         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5392
5393       ;; Enter these new marks into the info of the group.
5394       (if (nthcdr 3 info)
5395           (setcar (nthcdr 3 info) newmarked)
5396         ;; Add the marks lists to the end of the info.
5397         (when newmarked
5398           (setcdr (nthcdr 2 info) (list newmarked))))
5399
5400       ;; Cut off the end of the info if there's nothing else there.
5401       (let ((i 5))
5402         (while (and (> i 2)
5403                     (not (nth i info)))
5404           (when (nthcdr (decf i) info)
5405             (setcdr (nthcdr i info) nil)))))))
5406
5407 (defun gnus-set-mode-line (where)
5408   "Set the mode line of the article or summary buffers.
5409 If WHERE is `summary', the summary mode line format will be used."
5410   ;; Is this mode line one we keep updated?
5411   (when (and (memq where gnus-updated-mode-lines)
5412              (symbol-value
5413               (intern (format "gnus-%s-mode-line-format-spec" where))))
5414     (let (mode-string)
5415       (save-excursion
5416         ;; We evaluate this in the summary buffer since these
5417         ;; variables are buffer-local to that buffer.
5418         (set-buffer gnus-summary-buffer)
5419         ;; We bind all these variables that are used in the `eval' form
5420         ;; below.
5421         (let* ((mformat (symbol-value
5422                          (intern
5423                           (format "gnus-%s-mode-line-format-spec" where))))
5424                (gnus-tmp-group-name (gnus-group-decoded-name
5425                                      gnus-newsgroup-name))
5426                (gnus-tmp-article-number (or gnus-current-article 0))
5427                (gnus-tmp-unread gnus-newsgroup-unreads)
5428                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5429                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5430                (gnus-tmp-unread-and-unselected
5431                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5432                             (zerop gnus-tmp-unselected))
5433                        "")
5434                       ((zerop gnus-tmp-unselected)
5435                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5436                       (t (format "{%d(+%d) more}"
5437                                  gnus-tmp-unread-and-unticked
5438                                  gnus-tmp-unselected))))
5439                (gnus-tmp-subject
5440                 (if (and gnus-current-headers
5441                          (vectorp gnus-current-headers))
5442                     (gnus-mode-string-quote
5443                      (mail-header-subject gnus-current-headers))
5444                   ""))
5445                bufname-length max-len
5446                gnus-tmp-header);; passed as argument to any user-format-funcs
5447           (setq mode-string (eval mformat))
5448           (setq bufname-length (if (string-match "%b" mode-string)
5449                                    (- (length
5450                                        (buffer-name
5451                                         (if (eq where 'summary)
5452                                             nil
5453                                           (get-buffer gnus-article-buffer))))
5454                                       2)
5455                                  0))
5456           (setq max-len (max 4 (if gnus-mode-non-string-length
5457                                    (- (window-width)
5458                                       gnus-mode-non-string-length
5459                                       bufname-length)
5460                                  (length mode-string))))
5461           ;; We might have to chop a bit of the string off...
5462           (when (> (length mode-string) max-len)
5463             (setq mode-string
5464                   (concat (gnus-truncate-string mode-string (- max-len 3))
5465                           "...")))
5466           ;; Pad the mode string a bit.
5467           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5468       ;; Update the mode line.
5469       (setq mode-line-buffer-identification
5470             (gnus-mode-line-buffer-identification (list mode-string)))
5471       (set-buffer-modified-p t))))
5472
5473 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5474   "Go through the HEADERS list and add all Xrefs to a hash table.
5475 The resulting hash table is returned, or nil if no Xrefs were found."
5476   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5477          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5478          (xref-hashtb (gnus-make-hashtable))
5479          start group entry number xrefs header)
5480     (while headers
5481       (setq header (pop headers))
5482       (when (and (setq xrefs (mail-header-xref header))
5483                  (not (memq (setq number (mail-header-number header))
5484                             unreads)))
5485         (setq start 0)
5486         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5487           (setq start (match-end 0))
5488           (setq group (if prefix
5489                           (concat prefix (substring xrefs (match-beginning 1)
5490                                                     (match-end 1)))
5491                         (substring xrefs (match-beginning 1) (match-end 1))))
5492           (setq number
5493                 (string-to-int (substring xrefs (match-beginning 2)
5494                                           (match-end 2))))
5495           (if (setq entry (gnus-gethash group xref-hashtb))
5496               (setcdr entry (cons number (cdr entry)))
5497             (gnus-sethash group (cons number nil) xref-hashtb)))))
5498     (and start xref-hashtb)))
5499
5500 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5501   "Look through all the headers and mark the Xrefs as read."
5502   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5503         name entry info xref-hashtb idlist method nth4)
5504     (save-excursion
5505       (set-buffer gnus-group-buffer)
5506       (when (setq xref-hashtb
5507                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5508         (mapatoms
5509          (lambda (group)
5510            (unless (string= from-newsgroup (setq name (symbol-name group)))
5511              (setq idlist (symbol-value group))
5512              ;; Dead groups are not updated.
5513              (and (prog1
5514                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5515                             info (nth 2 entry))
5516                     (when (stringp (setq nth4 (gnus-info-method info)))
5517                       (setq nth4 (gnus-server-to-method nth4))))
5518                   ;; Only do the xrefs if the group has the same
5519                   ;; select method as the group we have just read.
5520                   (or (gnus-methods-equal-p
5521                        nth4 (gnus-find-method-for-group from-newsgroup))
5522                       virtual
5523                       (equal nth4 (setq method (gnus-find-method-for-group
5524                                                 from-newsgroup)))
5525                       (and (equal (car nth4) (car method))
5526                            (equal (nth 1 nth4) (nth 1 method))))
5527                   gnus-use-cross-reference
5528                   (or (not (eq gnus-use-cross-reference t))
5529                       virtual
5530                       ;; Only do cross-references on subscribed
5531                       ;; groups, if that is what is wanted.
5532                       (<= (gnus-info-level info) gnus-level-subscribed))
5533                   (gnus-group-make-articles-read name idlist))))
5534          xref-hashtb)))))
5535
5536 (defun gnus-compute-read-articles (group articles)
5537   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5538          (info (nth 2 entry))
5539          (active (gnus-active group))
5540          ninfo)
5541     (when entry
5542       ;; First peel off all invalid article numbers.
5543       (when active
5544         (let ((ids articles)
5545               id first)
5546           (while (setq id (pop ids))
5547             (when (and first (> id (cdr active)))
5548               ;; We'll end up in this situation in one particular
5549               ;; obscure situation.  If you re-scan a group and get
5550               ;; a new article that is cross-posted to a different
5551               ;; group that has not been re-scanned, you might get
5552               ;; crossposted article that has a higher number than
5553               ;; Gnus believes possible.  So we re-activate this
5554               ;; group as well.  This might mean doing the
5555               ;; crossposting thingy will *increase* the number
5556               ;; of articles in some groups.  Tsk, tsk.
5557               (setq active (or (gnus-activate-group group) active)))
5558             (when (or (> id (cdr active))
5559                       (< id (car active)))
5560               (setq articles (delq id articles))))))
5561       ;; If the read list is nil, we init it.
5562       (if (and active
5563                (null (gnus-info-read info))
5564                (> (car active) 1))
5565           (setq ninfo (cons 1 (1- (car active))))
5566         (setq ninfo (gnus-info-read info)))
5567       ;; Then we add the read articles to the range.
5568       (gnus-add-to-range
5569        ninfo (setq articles (sort articles '<))))))
5570
5571 (defun gnus-group-make-articles-read (group articles)
5572   "Update the info of GROUP to say that ARTICLES are read."
5573   (let* ((num 0)
5574          (entry (gnus-gethash group gnus-newsrc-hashtb))
5575          (info (nth 2 entry))
5576          (active (gnus-active group))
5577          range)
5578     (when entry
5579       (setq range (gnus-compute-read-articles group articles))
5580       (save-excursion
5581         (set-buffer gnus-group-buffer)
5582         (gnus-undo-register
5583           `(progn
5584              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5585              (gnus-info-set-read ',info ',(gnus-info-read info))
5586              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5587              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5588              (gnus-group-update-group ,group t))))
5589       ;; Add the read articles to the range.
5590       (gnus-info-set-read info range)
5591       (gnus-request-set-mark group (list (list range 'add '(read))))
5592       ;; Then we have to re-compute how many unread
5593       ;; articles there are in this group.
5594       (when active
5595         (cond
5596          ((not range)
5597           (setq num (- (1+ (cdr active)) (car active))))
5598          ((not (listp (cdr range)))
5599           (setq num (- (cdr active) (- (1+ (cdr range))
5600                                        (car range)))))
5601          (t
5602           (while range
5603             (if (numberp (car range))
5604                 (setq num (1+ num))
5605               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5606             (setq range (cdr range)))
5607           (setq num (- (cdr active) num))))
5608         ;; Update the number of unread articles.
5609         (setcar entry num)
5610         ;; Update the group buffer.
5611         (unless (gnus-ephemeral-group-p group)
5612           (gnus-group-update-group group t))))))
5613
5614 (defvar gnus-newsgroup-none-id 0)
5615
5616 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5617   (let ((cur nntp-server-buffer)
5618         (dependencies
5619          (or dependencies
5620              (save-excursion (set-buffer gnus-summary-buffer)
5621                              gnus-newsgroup-dependencies)))
5622         headers id end ref
5623         (mail-parse-charset gnus-newsgroup-charset)
5624         (mail-parse-ignored-charsets
5625          (save-excursion (condition-case nil
5626                              (set-buffer gnus-summary-buffer)
5627                            (error))
5628                          gnus-newsgroup-ignored-charsets)))
5629     (save-excursion
5630       (set-buffer nntp-server-buffer)
5631       ;; Translate all TAB characters into SPACE characters.
5632       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5633       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5634       (gnus-run-hooks 'gnus-parse-headers-hook)
5635       (let ((case-fold-search t)
5636             in-reply-to header p lines chars ctype)
5637         (goto-char (point-min))
5638         ;; Search to the beginning of the next header.  Error messages
5639         ;; do not begin with 2 or 3.
5640         (while (re-search-forward "^[23][0-9]+ " nil t)
5641           (setq id nil
5642                 ref nil)
5643           ;; This implementation of this function, with nine
5644           ;; search-forwards instead of the one re-search-forward and
5645           ;; a case (which basically was the old function) is actually
5646           ;; about twice as fast, even though it looks messier.  You
5647           ;; can't have everything, I guess.  Speed and elegance
5648           ;; doesn't always go hand in hand.
5649           (setq
5650            header
5651            (make-full-mail-header
5652             ;; Number.
5653             (prog1
5654                 (read cur)
5655               (end-of-line)
5656               (setq p (point))
5657               (narrow-to-region (point)
5658                                 (or (and (search-forward "\n.\n" nil t)
5659                                          (- (point) 2))
5660                                     (point))))
5661             ;; Subject.
5662             (progn
5663               (goto-char p)
5664               (if (search-forward "\nsubject:" nil t)
5665                   (nnheader-header-value)
5666                 "(none)"))
5667             ;; From.
5668             (progn
5669               (goto-char p)
5670               (if (search-forward "\nfrom:" nil t)
5671                   (nnheader-header-value)
5672                 "(nobody)"))
5673             ;; Date.
5674             (progn
5675               (goto-char p)
5676               (if (search-forward "\ndate:" nil t)
5677                   (nnheader-header-value) ""))
5678             ;; Message-ID.
5679             (progn
5680               (goto-char p)
5681               (setq id (if (re-search-forward
5682                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5683                            ;; We do it this way to make sure the Message-ID
5684                            ;; is (somewhat) syntactically valid.
5685                            (buffer-substring (match-beginning 1)
5686                                              (match-end 1))
5687                          ;; If there was no message-id, we just fake one
5688                          ;; to make subsequent routines simpler.
5689                          (nnheader-generate-fake-message-id))))
5690             ;; References.
5691             (progn
5692               (goto-char p)
5693               (if (search-forward "\nreferences:" nil t)
5694                   (progn
5695                     (setq end (point))
5696                     (prog1
5697                         (nnheader-header-value)
5698                       (setq ref
5699                             (buffer-substring
5700                              (progn
5701                                ;; (end-of-line)
5702                                (search-backward ">" end t)
5703                                (1+ (point)))
5704                              (progn
5705                                (search-backward "<" end t)
5706                                (point))))))
5707                 ;; Get the references from the in-reply-to header if there
5708                 ;; were no references and the in-reply-to header looks
5709                 ;; promising.
5710                 (if (and (search-forward "\nin-reply-to:" nil t)
5711                          (setq in-reply-to (nnheader-header-value))
5712                          (string-match "<[^>]+>" in-reply-to))
5713                     (let (ref2)
5714                       (setq ref (substring in-reply-to (match-beginning 0)
5715                                            (match-end 0)))
5716                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5717                         (setq ref2 (substring in-reply-to (match-beginning 0)
5718                                               (match-end 0)))
5719                         (when (> (length ref2) (length ref))
5720                           (setq ref ref2)))
5721                       ref)
5722                   (setq ref nil))))
5723             ;; Chars.
5724             (progn
5725               (goto-char p)
5726               (if (search-forward "\nchars: " nil t)
5727                   (if (numberp (setq chars (ignore-errors (read cur))))
5728                       chars -1)
5729                 -1))
5730             ;; Lines.
5731             (progn
5732               (goto-char p)
5733               (if (search-forward "\nlines: " nil t)
5734                   (if (numberp (setq lines (ignore-errors (read cur))))
5735                       lines -1)
5736                 -1))
5737             ;; Xref.
5738             (progn
5739               (goto-char p)
5740               (and (search-forward "\nxref:" nil t)
5741                    (nnheader-header-value)))
5742             ;; Extra.
5743             (when gnus-extra-headers
5744               (let ((extra gnus-extra-headers)
5745                     out)
5746                 (while extra
5747                   (goto-char p)
5748                   (when (search-forward
5749                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5750                     (push (cons (car extra) (nnheader-header-value)) out))
5751                   (pop extra))
5752                 out))))
5753           (goto-char p)
5754           (if (and (search-forward "\ncontent-type: " nil t)
5755                    (setq ctype (nnheader-header-value)))
5756               (mime-entity-set-content-type-internal
5757                header (mime-parse-Content-Type ctype)))
5758           (when (equal id ref)
5759             (setq ref nil))
5760
5761           (when gnus-alter-header-function
5762             (funcall gnus-alter-header-function header)
5763             (setq id (mail-header-id header)
5764                   ref (gnus-parent-id (mail-header-references header))))
5765
5766           (when (setq header
5767                       (gnus-dependencies-add-header
5768                        header dependencies force-new))
5769             (push header headers))
5770           (goto-char (point-max))
5771           (widen))
5772         (nreverse headers)))))
5773
5774 ;; Goes through the xover lines and returns a list of vectors
5775 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5776                                                   force-new dependencies
5777                                                   group also-fetch-heads)
5778   "Parse the news overview data in the server buffer.
5779 Return a list of headers that match SEQUENCE (see
5780 `nntp-retrieve-headers')."
5781   ;; Get the Xref when the users reads the articles since most/some
5782   ;; NNTP servers do not include Xrefs when using XOVER.
5783   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5784   (let ((mail-parse-charset gnus-newsgroup-charset)
5785         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5786         (cur nntp-server-buffer)
5787         (dependencies (or dependencies gnus-newsgroup-dependencies))
5788         (allp (cond
5789                ((eq gnus-read-all-available-headers t)
5790                 t)
5791                ((stringp gnus-read-all-available-headers)
5792                 (string-match gnus-read-all-available-headers group))
5793                (t
5794                 nil)))
5795         number headers header)
5796     (save-excursion
5797       (set-buffer nntp-server-buffer)
5798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5799       ;; Allow the user to mangle the headers before parsing them.
5800       (gnus-run-hooks 'gnus-parse-headers-hook)
5801       (goto-char (point-min))
5802       (gnus-parse-without-error
5803         (while (and (or sequence allp)
5804                     (not (eobp)))
5805           (setq number (read cur))
5806           (when (not allp)
5807             (while (and sequence
5808                         (< (car sequence) number))
5809               (setq sequence (cdr sequence))))
5810           (when (and (or allp
5811                          (and sequence
5812                               (eq number (car sequence))))
5813                      (progn
5814                        (setq sequence (cdr sequence))
5815                        (setq header (inline
5816                                       (gnus-nov-parse-line
5817                                        number dependencies force-new)))))
5818             (push header headers))
5819           (forward-line 1)))
5820       ;; A common bug in inn is that if you have posted an article and
5821       ;; then retrieves the active file, it will answer correctly --
5822       ;; the new article is included.  However, a NOV entry for the
5823       ;; article may not have been generated yet, so this may fail.
5824       ;; We work around this problem by retrieving the last few
5825       ;; headers using HEAD.
5826       (if (or (not also-fetch-heads)
5827               (not sequence))
5828           ;; We (probably) got all the headers.
5829           (nreverse headers)
5830         (let ((gnus-nov-is-evil t))
5831           (nconc
5832            (nreverse headers)
5833            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5834              (gnus-get-newsgroup-headers))))))))
5835
5836 (defun gnus-article-get-xrefs ()
5837   "Fill in the Xref value in `gnus-current-headers', if necessary.
5838 This is meant to be called in `gnus-article-internal-prepare-hook'."
5839   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5840                                  gnus-current-headers)))
5841     (or (not gnus-use-cross-reference)
5842         (not headers)
5843         (and (mail-header-xref headers)
5844              (not (string= (mail-header-xref headers) "")))
5845         (let ((case-fold-search t)
5846               xref)
5847           (save-restriction
5848             (nnheader-narrow-to-headers)
5849             (goto-char (point-min))
5850             (when (or (and (not (eobp))
5851                            (eq (downcase (char-after)) ?x)
5852                            (looking-at "Xref:"))
5853                       (search-forward "\nXref:" nil t))
5854               (goto-char (1+ (match-end 0)))
5855               (setq xref (buffer-substring (point)
5856                                            (progn (end-of-line) (point))))
5857               (mail-header-set-xref headers xref)))))))
5858
5859 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5860   "Find article ID and insert the summary line for that article.
5861 OLD-HEADER can either be a header or a line number to insert
5862 the subject line on."
5863   (let* ((line (and (numberp old-header) old-header))
5864          (old-header (and (vectorp old-header) old-header))
5865          (header (cond ((and old-header use-old-header)
5866                         old-header)
5867                        ((and (numberp id)
5868                              (gnus-number-to-header id))
5869                         (gnus-number-to-header id))
5870                        (t
5871                         (gnus-read-header id))))
5872          (number (and (numberp id) id))
5873          d)
5874     (when header
5875       ;; Rebuild the thread that this article is part of and go to the
5876       ;; article we have fetched.
5877       (when (and (not gnus-show-threads)
5878                  old-header)
5879         (when (and number
5880                    (setq d (gnus-data-find (mail-header-number old-header))))
5881           (goto-char (gnus-data-pos d))
5882           (gnus-data-remove
5883            number
5884            (- (gnus-point-at-bol)
5885               (prog1
5886                   (1+ (gnus-point-at-eol))
5887                 (gnus-delete-line))))))
5888       (when old-header
5889         (mail-header-set-number header (mail-header-number old-header)))
5890       (setq gnus-newsgroup-sparse
5891             (delq (setq number (mail-header-number header))
5892                   gnus-newsgroup-sparse))
5893       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5894       (push number gnus-newsgroup-limit)
5895       (gnus-rebuild-thread (mail-header-id header) line)
5896       (gnus-summary-goto-subject number nil t))
5897     (when (and (numberp number)
5898                (> number 0))
5899       ;; We have to update the boundaries even if we can't fetch the
5900       ;; article if ID is a number -- so that the next `P' or `N'
5901       ;; command will fetch the previous (or next) article even
5902       ;; if the one we tried to fetch this time has been canceled.
5903       (when (> number gnus-newsgroup-end)
5904         (setq gnus-newsgroup-end number))
5905       (when (< number gnus-newsgroup-begin)
5906         (setq gnus-newsgroup-begin number))
5907       (setq gnus-newsgroup-unselected
5908             (delq number gnus-newsgroup-unselected)))
5909     ;; Report back a success?
5910     (and header (mail-header-number header))))
5911
5912 ;;; Process/prefix in the summary buffer
5913
5914 (defun gnus-summary-work-articles (n)
5915   "Return a list of articles to be worked upon.
5916 The prefix argument, the list of process marked articles, and the
5917 current article will be taken into consideration."
5918   (save-excursion
5919     (set-buffer gnus-summary-buffer)
5920     (cond
5921      (n
5922       ;; A numerical prefix has been given.
5923       (setq n (prefix-numeric-value n))
5924       (let ((backward (< n 0))
5925             (n (abs (prefix-numeric-value n)))
5926             articles article)
5927         (save-excursion
5928           (while
5929               (and (> n 0)
5930                    (push (setq article (gnus-summary-article-number))
5931                          articles)
5932                    (if backward
5933                        (gnus-summary-find-prev nil article)
5934                      (gnus-summary-find-next nil article)))
5935             (decf n)))
5936         (nreverse articles)))
5937      ((and (gnus-region-active-p) (mark))
5938       (message "region active")
5939       ;; Work on the region between point and mark.
5940       (let ((max (max (point) (mark)))
5941             articles article)
5942         (save-excursion
5943           (goto-char (min (point) (mark)))
5944           (while
5945               (and
5946                (push (setq article (gnus-summary-article-number)) articles)
5947                (gnus-summary-find-next nil article)
5948                (< (point) max)))
5949           (nreverse articles))))
5950      (gnus-newsgroup-processable
5951       ;; There are process-marked articles present.
5952       ;; Save current state.
5953       (gnus-summary-save-process-mark)
5954       ;; Return the list.
5955       (reverse gnus-newsgroup-processable))
5956      (t
5957       ;; Just return the current article.
5958       (list (gnus-summary-article-number))))))
5959
5960 (defmacro gnus-summary-iterate (arg &rest forms)
5961   "Iterate over the process/prefixed articles and do FORMS.
5962 ARG is the interactive prefix given to the command.  FORMS will be
5963 executed with point over the summary line of the articles."
5964   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5965     `(let ((,articles (gnus-summary-work-articles ,arg)))
5966        (while ,articles
5967          (gnus-summary-goto-subject (car ,articles))
5968          ,@forms
5969          (pop ,articles)))))
5970
5971 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5972 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5973
5974 (defun gnus-summary-save-process-mark ()
5975   "Push the current set of process marked articles on the stack."
5976   (interactive)
5977   (push (copy-sequence gnus-newsgroup-processable)
5978         gnus-newsgroup-process-stack))
5979
5980 (defun gnus-summary-kill-process-mark ()
5981   "Push the current set of process marked articles on the stack and unmark."
5982   (interactive)
5983   (gnus-summary-save-process-mark)
5984   (gnus-summary-unmark-all-processable))
5985
5986 (defun gnus-summary-yank-process-mark ()
5987   "Pop the last process mark state off the stack and restore it."
5988   (interactive)
5989   (unless gnus-newsgroup-process-stack
5990     (error "Empty mark stack"))
5991   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5992
5993 (defun gnus-summary-process-mark-set (set)
5994   "Make SET into the current process marked articles."
5995   (gnus-summary-unmark-all-processable)
5996   (while set
5997     (gnus-summary-set-process-mark (pop set))))
5998
5999 ;;; Searching and stuff
6000
6001 (defun gnus-summary-search-group (&optional backward use-level)
6002   "Search for next unread newsgroup.
6003 If optional argument BACKWARD is non-nil, search backward instead."
6004   (save-excursion
6005     (set-buffer gnus-group-buffer)
6006     (when (gnus-group-search-forward
6007            backward nil (if use-level (gnus-group-group-level) nil))
6008       (gnus-group-group-name))))
6009
6010 (defun gnus-summary-best-group (&optional exclude-group)
6011   "Find the name of the best unread group.
6012 If EXCLUDE-GROUP, do not go to this group."
6013   (save-excursion
6014     (set-buffer gnus-group-buffer)
6015     (save-excursion
6016       (gnus-group-best-unread-group exclude-group))))
6017
6018 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
6019   (if backward (gnus-summary-find-prev)
6020     (let* ((dummy (gnus-summary-article-intangible-p))
6021            (article (or article (gnus-summary-article-number)))
6022            (arts (gnus-data-find-list article))
6023            result)
6024       (when (and (not dummy)
6025                  (or (not gnus-summary-check-current)
6026                      (not unread)
6027                      (not (gnus-data-unread-p (car arts)))))
6028         (setq arts (cdr arts)))
6029       (when (setq result
6030                   (if unread
6031                       (progn
6032                         (while arts
6033                           (when (or (and undownloaded
6034                                          (memq (car arts) gnus-newsgroup-undownloaded))
6035                                     (gnus-data-unread-p (car arts)))
6036                             (setq result (car arts)
6037                                   arts nil))
6038                           (setq arts (cdr arts)))
6039                         result)
6040                     (car arts)))
6041         (goto-char (gnus-data-pos result))
6042         (gnus-data-number result)))))
6043
6044 (defun gnus-summary-find-prev (&optional unread article)
6045   (let* ((eobp (eobp))
6046          (article (or article (gnus-summary-article-number)))
6047          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6048          result)
6049     (when (and (not eobp)
6050                (or (not gnus-summary-check-current)
6051                    (not unread)
6052                    (not (gnus-data-unread-p (car arts)))))
6053       (setq arts (cdr arts)))
6054     (when (setq result
6055                 (if unread
6056                     (progn
6057                       (while arts
6058                         (when (gnus-data-unread-p (car arts))
6059                           (setq result (car arts)
6060                                 arts nil))
6061                         (setq arts (cdr arts)))
6062                       result)
6063                   (car arts)))
6064       (goto-char (gnus-data-pos result))
6065       (gnus-data-number result))))
6066
6067 (defun gnus-summary-find-subject (subject &optional unread backward article)
6068   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6069          (article (or article (gnus-summary-article-number)))
6070          (articles (gnus-data-list backward))
6071          (arts (gnus-data-find-list article articles))
6072          result)
6073     (when (or (not gnus-summary-check-current)
6074               (not unread)
6075               (not (gnus-data-unread-p (car arts))))
6076       (setq arts (cdr arts)))
6077     (while arts
6078       (and (or (not unread)
6079                (gnus-data-unread-p (car arts)))
6080            (vectorp (gnus-data-header (car arts)))
6081            (gnus-subject-equal
6082             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6083            (setq result (car arts)
6084                  arts nil))
6085       (setq arts (cdr arts)))
6086     (and result
6087          (goto-char (gnus-data-pos result))
6088          (gnus-data-number result))))
6089
6090 (defun gnus-summary-search-forward (&optional unread subject backward)
6091   "Search forward for an article.
6092 If UNREAD, look for unread articles.  If SUBJECT, look for
6093 articles with that subject.  If BACKWARD, search backward instead."
6094   (cond (subject (gnus-summary-find-subject subject unread backward))
6095         (backward (gnus-summary-find-prev unread))
6096         (t (gnus-summary-find-next unread))))
6097
6098 (defun gnus-recenter (&optional n)
6099   "Center point in window and redisplay frame.
6100 Also do horizontal recentering."
6101   (interactive "P")
6102   (when (and gnus-auto-center-summary
6103              (not (eq gnus-auto-center-summary 'vertical)))
6104     (gnus-horizontal-recenter))
6105   (recenter n))
6106
6107 (defun gnus-summary-recenter ()
6108   "Center point in the summary window.
6109 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6110 displayed, no centering will be performed."
6111   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6112   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6113   (interactive)
6114   (let* ((top (cond ((< (window-height) 4) 0)
6115                     ((< (window-height) 7) 1)
6116                     (t (if (numberp gnus-auto-center-summary)
6117                            gnus-auto-center-summary
6118                          2))))
6119          (height (1- (window-height)))
6120          (bottom (save-excursion (goto-char (point-max))
6121                                  (forward-line (- height))
6122                                  (point)))
6123          (window (get-buffer-window (current-buffer))))
6124     ;; The user has to want it.
6125     (when gnus-auto-center-summary
6126       (when (get-buffer-window gnus-article-buffer)
6127         ;; Only do recentering when the article buffer is displayed,
6128         ;; Set the window start to either `bottom', which is the biggest
6129         ;; possible valid number, or the second line from the top,
6130         ;; whichever is the least.
6131         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6132           (if (> bottom top-pos)
6133               ;; Keep the second line from the top visible
6134               (set-window-start window top-pos t)
6135             ;; Try to keep the bottom line visible; if it's partially
6136             ;; obscured, either scroll one more line to make it fully
6137             ;; visible, or revert to using TOP-POS.
6138             (save-excursion
6139               (goto-char (point-max))
6140               (forward-line -1)
6141               (let ((last-line-start (point)))
6142                 (goto-char bottom)
6143                 (set-window-start window (point) t)
6144                 (when (not (pos-visible-in-window-p last-line-start window))
6145                   (forward-line 1)
6146                   (set-window-start window (min (point) top-pos) t)))))))
6147       ;; Do horizontal recentering while we're at it.
6148       (when (and (get-buffer-window (current-buffer) t)
6149                  (not (eq gnus-auto-center-summary 'vertical)))
6150         (let ((selected (selected-window)))
6151           (select-window (get-buffer-window (current-buffer) t))
6152           (gnus-summary-position-point)
6153           (gnus-horizontal-recenter)
6154           (select-window selected))))))
6155
6156 (defun gnus-summary-jump-to-group (newsgroup)
6157   "Move point to NEWSGROUP in group mode buffer."
6158   ;; Keep update point of group mode buffer if visible.
6159   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6160       (save-window-excursion
6161         ;; Take care of tree window mode.
6162         (when (get-buffer-window gnus-group-buffer)
6163           (pop-to-buffer gnus-group-buffer))
6164         (gnus-group-jump-to-group newsgroup))
6165     (save-excursion
6166       ;; Take care of tree window mode.
6167       (if (get-buffer-window gnus-group-buffer)
6168           (pop-to-buffer gnus-group-buffer)
6169         (set-buffer gnus-group-buffer))
6170       (gnus-group-jump-to-group newsgroup))))
6171
6172 ;; This function returns a list of article numbers based on the
6173 ;; difference between the ranges of read articles in this group and
6174 ;; the range of active articles.
6175 (defun gnus-list-of-unread-articles (group)
6176   (let* ((read (gnus-info-read (gnus-get-info group)))
6177          (active (or (gnus-active group) (gnus-activate-group group)))
6178          (last (cdr active))
6179          first nlast unread)
6180     ;; If none are read, then all are unread.
6181     (if (not read)
6182         (setq first (car active))
6183       ;; If the range of read articles is a single range, then the
6184       ;; first unread article is the article after the last read
6185       ;; article.  Sounds logical, doesn't it?
6186       (if (and (not (listp (cdr read)))
6187                (or (< (car read) (car active))
6188                    (progn (setq read (list read))
6189                           nil)))
6190           (setq first (max (car active) (1+ (cdr read))))
6191         ;; `read' is a list of ranges.
6192         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6193                                   (caar read)))
6194                   1)
6195           (setq first (car active)))
6196         (while read
6197           (when first
6198             (while (< first nlast)
6199               (push first unread)
6200               (setq first (1+ first))))
6201           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6202           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6203           (setq read (cdr read)))))
6204     ;; And add the last unread articles.
6205     (while (<= first last)
6206       (push first unread)
6207       (setq first (1+ first)))
6208     ;; Return the list of unread articles.
6209     (delq 0 (nreverse unread))))
6210
6211 (defun gnus-list-of-read-articles (group)
6212   "Return a list of unread, unticked and non-dormant articles."
6213   (let* ((info (gnus-get-info group))
6214          (marked (gnus-info-marks info))
6215          (active (gnus-active group)))
6216     (and info active
6217          (gnus-list-range-difference
6218           (gnus-list-range-difference
6219            (gnus-sorted-complement
6220             (gnus-uncompress-range active)
6221             (gnus-list-of-unread-articles group))
6222            (cdr (assq 'dormant marked)))
6223           (cdr (assq 'tick marked))))))
6224
6225 ;; Various summary commands
6226
6227 (defun gnus-summary-select-article-buffer ()
6228   "Reconfigure windows to show article buffer."
6229   (interactive)
6230   (if (not (gnus-buffer-live-p gnus-article-buffer))
6231       (error "There is no article buffer for this summary buffer")
6232     (gnus-configure-windows 'article)
6233     (select-window (get-buffer-window gnus-article-buffer))))
6234
6235 (defun gnus-summary-universal-argument (arg)
6236   "Perform any operation on all articles that are process/prefixed."
6237   (interactive "P")
6238   (let ((articles (gnus-summary-work-articles arg))
6239         func article)
6240     (if (eq
6241          (setq
6242           func
6243           (key-binding
6244            (read-key-sequence
6245             (substitute-command-keys
6246              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6247          'undefined)
6248         (gnus-error 1 "Undefined key")
6249       (save-excursion
6250         (while articles
6251           (gnus-summary-goto-subject (setq article (pop articles)))
6252           (let (gnus-newsgroup-processable)
6253             (command-execute func))
6254           (gnus-summary-remove-process-mark article)))))
6255   (gnus-summary-position-point))
6256
6257 (defun gnus-summary-toggle-truncation (&optional arg)
6258   "Toggle truncation of summary lines.
6259 With arg, turn line truncation on if arg is positive."
6260   (interactive "P")
6261   (setq truncate-lines
6262         (if (null arg) (not truncate-lines)
6263           (> (prefix-numeric-value arg) 0)))
6264   (redraw-display))
6265
6266 (defun gnus-summary-find-uncancelled ()
6267   "Return the number of an uncancelled article.
6268 The current article is considered, then following articles, then previous
6269 articles.  If all articles are cancelled then return a dummy 0."
6270   (let (found)
6271     (dolist (rev '(nil t))
6272       (unless found      ; don't demand the reverse list if we don't need it
6273         (let ((data (gnus-data-find-list
6274                      (gnus-summary-article-number) (gnus-data-list rev))))
6275           (while (and data (not found))
6276             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6277                 (setq found (gnus-data-number (car data))))
6278             (setq data (cdr data))))))
6279     (or found 0)))
6280
6281 (defun gnus-summary-reselect-current-group (&optional all rescan)
6282   "Exit and then reselect the current newsgroup.
6283 The prefix argument ALL means to select all articles."
6284   (interactive "P")
6285   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6286     (error "Ephemeral groups can't be reselected"))
6287   (let ((current-subject (gnus-summary-find-uncancelled))
6288         (group gnus-newsgroup-name))
6289     (setq gnus-newsgroup-begin nil)
6290     (gnus-summary-exit)
6291     ;; We have to adjust the point of group mode buffer because
6292     ;; point was moved to the next unread newsgroup by exiting.
6293     (gnus-summary-jump-to-group group)
6294     (when rescan
6295       (save-excursion
6296         (save-window-excursion
6297           ;; Don't show group contents.
6298           (set-window-start (selected-window) (point-max))
6299           (gnus-group-get-new-news-this-group 1))))
6300     (gnus-group-read-group all t)
6301     (gnus-summary-goto-subject current-subject nil t)))
6302
6303 (defun gnus-summary-rescan-group (&optional all)
6304   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6305   (interactive "P")
6306   (gnus-summary-reselect-current-group all t))
6307
6308 (defun gnus-summary-update-info (&optional non-destructive)
6309   (save-excursion
6310     (let ((group gnus-newsgroup-name))
6311       (when group
6312         (when gnus-newsgroup-kill-headers
6313           (setq gnus-newsgroup-killed
6314                 (gnus-compress-sequence
6315                  (gnus-sorted-union
6316                   (gnus-list-range-intersection
6317                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6318                   gnus-newsgroup-unreads)
6319                  t)))
6320         (unless (listp (cdr gnus-newsgroup-killed))
6321           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6322         (let ((headers gnus-newsgroup-headers))
6323           ;; Set the new ranges of read articles.
6324           (save-excursion
6325             (set-buffer gnus-group-buffer)
6326             (gnus-undo-force-boundary))
6327           (gnus-update-read-articles
6328            group (gnus-sorted-union
6329                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6330           ;; Set the current article marks.
6331           (let ((gnus-newsgroup-scored
6332                  (if (and (not gnus-save-score)
6333                           (not non-destructive))
6334                      nil
6335                    gnus-newsgroup-scored)))
6336             (save-excursion
6337               (gnus-update-marks)))
6338           ;; Do the cross-ref thing.
6339           (when gnus-use-cross-reference
6340             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6341           ;; Do not switch windows but change the buffer to work.
6342           (set-buffer gnus-group-buffer)
6343           (unless (gnus-ephemeral-group-p group)
6344             (gnus-group-update-group group)))))))
6345
6346 (defun gnus-summary-save-newsrc (&optional force)
6347   "Save the current number of read/marked articles in the dribble buffer.
6348 The dribble buffer will then be saved.
6349 If FORCE (the prefix), also save the .newsrc file(s)."
6350   (interactive "P")
6351   (gnus-summary-update-info t)
6352   (if force
6353       (gnus-save-newsrc-file)
6354     (gnus-dribble-save)))
6355
6356 (defun gnus-summary-exit (&optional temporary)
6357   "Exit reading current newsgroup, and then return to group selection mode.
6358 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6359   (interactive)
6360   (gnus-set-global-variables)
6361   (gnus-kill-save-kill-buffer)
6362   (gnus-async-halt-prefetch)
6363   (let* ((group gnus-newsgroup-name)
6364          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6365          (gnus-group-is-exiting-p t)
6366          (mode major-mode)
6367          (group-point nil)
6368          (buf (current-buffer)))
6369     (unless quit-config
6370       ;; Do adaptive scoring, and possibly save score files.
6371       (when gnus-newsgroup-adaptive
6372         (gnus-score-adaptive))
6373       (when gnus-use-scoring
6374         (gnus-score-save)))
6375     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6376     ;; If we have several article buffers, we kill them at exit.
6377     (unless gnus-single-article-buffer
6378       (gnus-kill-buffer gnus-original-article-buffer)
6379       (setq gnus-article-current nil))
6380     (when gnus-use-cache
6381       (gnus-cache-possibly-remove-articles)
6382       (gnus-cache-save-buffers))
6383     (gnus-async-prefetch-remove-group group)
6384     (when gnus-suppress-duplicates
6385       (gnus-dup-enter-articles))
6386     (when gnus-use-trees
6387       (gnus-tree-close group))
6388     (when gnus-use-cache
6389       (gnus-cache-write-active))
6390     ;; Remove entries for this group.
6391     (nnmail-purge-split-history (gnus-group-real-name group))
6392     ;; Make all changes in this group permanent.
6393     (unless quit-config
6394       (gnus-run-hooks 'gnus-exit-group-hook)
6395       (gnus-summary-update-info))
6396     (gnus-close-group group)
6397     ;; Make sure where we were, and go to next newsgroup.
6398     (set-buffer gnus-group-buffer)
6399     (unless quit-config
6400       (gnus-group-jump-to-group group))
6401     (gnus-run-hooks 'gnus-summary-exit-hook)
6402     (unless (or quit-config
6403                 ;; If this group has disappeared from the summary
6404                 ;; buffer, don't skip forwards.
6405                 (not (string= group (gnus-group-group-name))))
6406       (gnus-group-next-unread-group 1))
6407     (setq group-point (point))
6408     (if temporary
6409         nil                             ;Nothing to do.
6410       ;; If we have several article buffers, we kill them at exit.
6411       (unless gnus-single-article-buffer
6412         (gnus-kill-buffer gnus-article-buffer)
6413         (gnus-kill-buffer gnus-original-article-buffer)
6414         (setq gnus-article-current nil))
6415       (set-buffer buf)
6416       (if (not gnus-kill-summary-on-exit)
6417           (progn
6418             (gnus-deaden-summary)
6419             (setq mode nil))
6420         ;; We set all buffer-local variables to nil.  It is unclear why
6421         ;; this is needed, but if we don't, buffer-local variables are
6422         ;; not garbage-collected, it seems.  This would the lead to en
6423         ;; ever-growing Emacs.
6424         (gnus-summary-clear-local-variables)
6425         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6426           (gnus-summary-clear-local-variables))
6427         (when (get-buffer gnus-article-buffer)
6428           (bury-buffer gnus-article-buffer))
6429         ;; We clear the global counterparts of the buffer-local
6430         ;; variables as well, just to be on the safe side.
6431         (set-buffer gnus-group-buffer)
6432         (gnus-summary-clear-local-variables)
6433         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6434           (gnus-summary-clear-local-variables))
6435         ;; Return to group mode buffer.
6436         (when (eq mode 'gnus-summary-mode)
6437           (gnus-kill-buffer buf)))
6438       (setq gnus-current-select-method gnus-select-method)
6439       (pop-to-buffer gnus-group-buffer)
6440       (if (not quit-config)
6441           (progn
6442             (goto-char group-point)
6443             (gnus-configure-windows 'group 'force)
6444             (unless (pos-visible-in-window-p)
6445               (forward-line (/ (static-if (featurep 'xemacs)
6446                                    (window-displayed-height)
6447                                  (1- (window-height)))
6448                                -2))
6449               (set-window-start (selected-window) (point))
6450               (goto-char group-point)))
6451         (gnus-handle-ephemeral-exit quit-config))
6452       ;; Clear the current group name.
6453       (unless quit-config
6454         (setq gnus-newsgroup-name nil)))))
6455
6456 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6457 (defun gnus-summary-exit-no-update (&optional no-questions)
6458   "Quit reading current newsgroup without updating read article info."
6459   (interactive)
6460   (let* ((group gnus-newsgroup-name)
6461          (gnus-group-is-exiting-p t)
6462          (quit-config (gnus-group-quit-config group)))
6463     (when (or no-questions
6464               gnus-expert-user
6465               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6466       (gnus-async-halt-prefetch)
6467       (mapcar 'funcall
6468               (delq 'gnus-summary-expire-articles
6469                     (copy-sequence gnus-summary-prepare-exit-hook)))
6470       ;; If we have several article buffers, we kill them at exit.
6471       (unless gnus-single-article-buffer
6472         (gnus-kill-buffer gnus-article-buffer)
6473         (gnus-kill-buffer gnus-original-article-buffer)
6474         (setq gnus-article-current nil))
6475       (if (not gnus-kill-summary-on-exit)
6476           (gnus-deaden-summary)
6477         (gnus-close-group group)
6478         (gnus-summary-clear-local-variables)
6479         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6480           (gnus-summary-clear-local-variables))
6481         (set-buffer gnus-group-buffer)
6482         (gnus-summary-clear-local-variables)
6483         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6484           (gnus-summary-clear-local-variables))
6485         (when (get-buffer gnus-summary-buffer)
6486           (kill-buffer gnus-summary-buffer)))
6487       (unless gnus-single-article-buffer
6488         (setq gnus-article-current nil))
6489       (when gnus-use-trees
6490         (gnus-tree-close group))
6491       (gnus-async-prefetch-remove-group group)
6492       (when (get-buffer gnus-article-buffer)
6493         (bury-buffer gnus-article-buffer))
6494       ;; Return to the group buffer.
6495       (gnus-configure-windows 'group 'force)
6496       ;; Clear the current group name.
6497       (setq gnus-newsgroup-name nil)
6498       (unless (gnus-ephemeral-group-p group)
6499         (gnus-group-update-group group))
6500       (when (equal (gnus-group-group-name) group)
6501         (gnus-group-next-unread-group 1))
6502       (when quit-config
6503         (gnus-handle-ephemeral-exit quit-config)))))
6504
6505 (defun gnus-handle-ephemeral-exit (quit-config)
6506   "Handle movement when leaving an ephemeral group.
6507 The state which existed when entering the ephemeral is reset."
6508   (if (not (buffer-name (car quit-config)))
6509       (gnus-configure-windows 'group 'force)
6510     (set-buffer (car quit-config))
6511     (cond ((eq major-mode 'gnus-summary-mode)
6512            (gnus-set-global-variables))
6513           ((eq major-mode 'gnus-article-mode)
6514            (save-excursion
6515              ;; The `gnus-summary-buffer' variable may point
6516              ;; to the old summary buffer when using a single
6517              ;; article buffer.
6518              (unless (gnus-buffer-live-p gnus-summary-buffer)
6519                (set-buffer gnus-group-buffer))
6520              (set-buffer gnus-summary-buffer)
6521              (gnus-set-global-variables))))
6522     (if (or (eq (cdr quit-config) 'article)
6523             (eq (cdr quit-config) 'pick))
6524         (progn
6525           ;; The current article may be from the ephemeral group
6526           ;; thus it is best that we reload this article
6527           (gnus-summary-show-article)
6528           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6529               (gnus-configure-windows 'pick 'force)
6530             (gnus-configure-windows (cdr quit-config) 'force)))
6531       (gnus-configure-windows (cdr quit-config) 'force))
6532     (when (eq major-mode 'gnus-summary-mode)
6533       (gnus-summary-next-subject 1 nil t)
6534       (gnus-summary-recenter)
6535       (gnus-summary-position-point))))
6536
6537 (defun gnus-summary-preview-mime-message ()
6538   "MIME decode and play this message."
6539   (interactive)
6540   (let ((gnus-break-pages nil)
6541         (gnus-show-mime t))
6542     (gnus-summary-select-article gnus-show-all-headers t))
6543   (let ((w (get-buffer-window gnus-article-buffer)))
6544     (when w
6545       (select-window (get-buffer-window gnus-article-buffer)))))
6546
6547 ;;; Dead summaries.
6548
6549 (defvar gnus-dead-summary-mode-map nil)
6550
6551 (unless gnus-dead-summary-mode-map
6552   (setq gnus-dead-summary-mode-map (make-keymap))
6553   (suppress-keymap gnus-dead-summary-mode-map)
6554   (substitute-key-definition
6555    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6556   (dolist (key '("\C-d" "\r" "\177" [delete]))
6557     (define-key gnus-dead-summary-mode-map
6558       key 'gnus-summary-wake-up-the-dead))
6559   (dolist (key '("q" "Q"))
6560     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6561
6562 (defvar gnus-dead-summary-mode nil
6563   "Minor mode for Gnus summary buffers.")
6564
6565 (defun gnus-dead-summary-mode (&optional arg)
6566   "Minor mode for Gnus summary buffers."
6567   (interactive "P")
6568   (when (eq major-mode 'gnus-summary-mode)
6569     (make-local-variable 'gnus-dead-summary-mode)
6570     (setq gnus-dead-summary-mode
6571           (if (null arg) (not gnus-dead-summary-mode)
6572             (> (prefix-numeric-value arg) 0)))
6573     (when gnus-dead-summary-mode
6574       (gnus-add-minor-mode
6575        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6576
6577 (defun gnus-deaden-summary ()
6578   "Make the current summary buffer into a dead summary buffer."
6579   ;; Kill any previous dead summary buffer.
6580   (when (and gnus-dead-summary
6581              (buffer-name gnus-dead-summary))
6582     (save-excursion
6583       (set-buffer gnus-dead-summary)
6584       (when gnus-dead-summary-mode
6585         (kill-buffer (current-buffer)))))
6586   ;; Make this the current dead summary.
6587   (setq gnus-dead-summary (current-buffer))
6588   (gnus-dead-summary-mode 1)
6589   (let ((name (buffer-name)))
6590     (when (string-match "Summary" name)
6591       (rename-buffer
6592        (concat (substring name 0 (match-beginning 0)) "Dead "
6593                (substring name (match-beginning 0)))
6594        t)
6595       (bury-buffer))))
6596
6597 (defun gnus-kill-or-deaden-summary (buffer)
6598   "Kill or deaden the summary BUFFER."
6599   (save-excursion
6600     (when (and (buffer-name buffer)
6601                (not gnus-single-article-buffer))
6602       (save-excursion
6603         (set-buffer buffer)
6604         (gnus-kill-buffer gnus-article-buffer)
6605         (gnus-kill-buffer gnus-original-article-buffer)))
6606     (cond
6607      ;; Kill the buffer.
6608      (gnus-kill-summary-on-exit
6609       (when (and gnus-use-trees
6610                  (gnus-buffer-exists-p buffer))
6611         (save-excursion
6612           (set-buffer buffer)
6613           (gnus-tree-close gnus-newsgroup-name)))
6614       (gnus-kill-buffer buffer))
6615      ;; Deaden the buffer.
6616      ((gnus-buffer-exists-p buffer)
6617       (save-excursion
6618         (set-buffer buffer)
6619         (gnus-deaden-summary))))))
6620
6621 (defun gnus-summary-wake-up-the-dead (&rest args)
6622   "Wake up the dead summary buffer."
6623   (interactive)
6624   (gnus-dead-summary-mode -1)
6625   (let ((name (buffer-name)))
6626     (when (string-match "Dead " name)
6627       (rename-buffer
6628        (concat (substring name 0 (match-beginning 0))
6629                (substring name (match-end 0)))
6630        t)))
6631   (gnus-message 3 "This dead summary is now alive again"))
6632
6633 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6634 (defun gnus-summary-fetch-faq (&optional faq-dir)
6635   "Fetch the FAQ for the current group.
6636 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6637 in."
6638   (interactive
6639    (list
6640     (when current-prefix-arg
6641       (completing-read
6642        "FAQ dir: " (and (listp gnus-group-faq-directory)
6643                         (mapcar (lambda (file) (list file))
6644                                 gnus-group-faq-directory))))))
6645   (let (gnus-faq-buffer)
6646     (when (setq gnus-faq-buffer
6647                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6648       (gnus-configure-windows 'summary-faq))))
6649
6650 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6651 (defun gnus-summary-describe-group (&optional force)
6652   "Describe the current newsgroup."
6653   (interactive "P")
6654   (gnus-group-describe-group force gnus-newsgroup-name))
6655
6656 (defun gnus-summary-describe-briefly ()
6657   "Describe summary mode commands briefly."
6658   (interactive)
6659   (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")))
6660
6661 ;; Walking around group mode buffer from summary mode.
6662
6663 (defun gnus-summary-next-group (&optional no-article target-group backward)
6664   "Exit current newsgroup and then select next unread newsgroup.
6665 If prefix argument NO-ARTICLE is non-nil, no article is selected
6666 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6667 previous group instead."
6668   (interactive "P")
6669   ;; Stop pre-fetching.
6670   (gnus-async-halt-prefetch)
6671   (let ((current-group gnus-newsgroup-name)
6672         (current-buffer (current-buffer))
6673         entered)
6674     (gnus-summary-exit t)
6675     (while (not entered)
6676       ;; Then we find what group we are supposed to enter.
6677       (set-buffer gnus-group-buffer)
6678       (gnus-group-jump-to-group current-group)
6679       (setq target-group
6680             (or target-group
6681                 (if (eq gnus-keep-same-level 'best)
6682                     (gnus-summary-best-group gnus-newsgroup-name)
6683                   (gnus-summary-search-group backward gnus-keep-same-level))))
6684       (if (not target-group)
6685           ;; There are no further groups, so we return to the group
6686           ;; buffer.
6687           (progn
6688             (gnus-message 5 "Returning to the group buffer")
6689             (setq entered t)
6690             (when (gnus-buffer-live-p current-buffer)
6691               (set-buffer current-buffer)
6692               (gnus-summary-exit))
6693             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6694         ;; We try to enter the target group.
6695         (gnus-group-jump-to-group target-group)
6696         (let ((unreads (gnus-group-group-unread)))
6697           (if (and (or (eq t unreads)
6698                        (and unreads (not (zerop unreads))))
6699                    (progn
6700                      ;; Now we semi-exit this group to update Xrefs
6701                      ;; and all variables.  We can't do a real exit,
6702                      ;; because the window conf must remain the same
6703                      ;; in case the user is prompted for info, and we
6704                      ;; don't want the window conf to change before
6705                      ;; that...
6706                      (when (gnus-buffer-live-p current-buffer)
6707                        (set-buffer current-buffer)
6708                        (gnus-summary-exit t))
6709                      (gnus-summary-read-group
6710                       target-group nil no-article
6711                       (and (buffer-name current-buffer) current-buffer)
6712                       nil backward)))
6713               (setq entered t)
6714             (setq current-group target-group
6715                   target-group nil)))))))
6716
6717 (defun gnus-summary-prev-group (&optional no-article)
6718   "Exit current newsgroup and then select previous unread newsgroup.
6719 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6720   (interactive "P")
6721   (gnus-summary-next-group no-article nil t))
6722
6723 ;; Walking around summary lines.
6724
6725 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6726   "Go to the first unread subject.
6727 If UNREAD is non-nil, go to the first unread article.
6728 Returns the article selected or nil if there are no unread articles."
6729   (interactive "P")
6730   (prog1
6731       (cond
6732        ;; Empty summary.
6733        ((null gnus-newsgroup-data)
6734         (gnus-message 3 "No articles in the group")
6735         nil)
6736        ;; Pick the first article.
6737        ((not unread)
6738         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6739         (gnus-data-number (car gnus-newsgroup-data)))
6740        ;; No unread articles.
6741        ((null gnus-newsgroup-unreads)
6742         (gnus-message 3 "No more unread articles")
6743         nil)
6744        ;; Find the first unread article.
6745        (t
6746         (let ((data gnus-newsgroup-data))
6747           (while (and data
6748                       (and (not (and undownloaded
6749                                      (memq (car data)
6750                                            gnus-newsgroup-undownloaded)))
6751                            (if unseen
6752                                (or (not (memq
6753                                          (gnus-data-number (car data))
6754                                          gnus-newsgroup-unseen))
6755                                    (not (gnus-data-unread-p (car data))))
6756                              (not (gnus-data-unread-p (car data))))))
6757             (setq data (cdr data)))
6758           (when data
6759             (goto-char (gnus-data-pos (car data)))
6760             (gnus-data-number (car data))))))
6761     (gnus-summary-position-point)))
6762
6763 (defun gnus-summary-next-subject (n &optional unread dont-display)
6764   "Go to next N'th summary line.
6765 If N is negative, go to the previous N'th subject line.
6766 If UNREAD is non-nil, only unread articles are selected.
6767 The difference between N and the actual number of steps taken is
6768 returned."
6769   (interactive "p")
6770   (let ((backward (< n 0))
6771         (n (abs n)))
6772     (while (and (> n 0)
6773                 (if backward
6774                     (gnus-summary-find-prev unread)
6775                   (gnus-summary-find-next unread)))
6776       (unless (zerop (setq n (1- n)))
6777         (gnus-summary-show-thread)))
6778     (when (/= 0 n)
6779       (gnus-message 7 "No more%s articles"
6780                     (if unread " unread" "")))
6781     (unless dont-display
6782       (gnus-summary-recenter)
6783       (gnus-summary-position-point))
6784     n))
6785
6786 (defun gnus-summary-next-unread-subject (n)
6787   "Go to next N'th unread summary line."
6788   (interactive "p")
6789   (gnus-summary-next-subject n t))
6790
6791 (defun gnus-summary-prev-subject (n &optional unread)
6792   "Go to previous N'th summary line.
6793 If optional argument UNREAD is non-nil, only unread article is selected."
6794   (interactive "p")
6795   (gnus-summary-next-subject (- n) unread))
6796
6797 (defun gnus-summary-prev-unread-subject (n)
6798   "Go to previous N'th unread summary line."
6799   (interactive "p")
6800   (gnus-summary-next-subject (- n) t))
6801
6802 (defun gnus-summary-goto-subjects (articles)
6803   "Insert the subject header for ARTICLES in the current buffer."
6804   (save-excursion
6805     (dolist (article articles)
6806       (gnus-summary-goto-subject article t)))
6807   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6808   (gnus-summary-position-point))
6809   
6810 (defun gnus-summary-goto-subject (article &optional force silent)
6811   "Go the subject line of ARTICLE.
6812 If FORCE, also allow jumping to articles not currently shown."
6813   (interactive "nArticle number: ")
6814   (unless (numberp article)
6815     (error "Article %s is not a number" article))
6816   (let ((b (point))
6817         (data (gnus-data-find article)))
6818     ;; We read in the article if we have to.
6819     (and (not data)
6820          force
6821          (gnus-summary-insert-subject
6822           article
6823           (if (or (numberp force) (vectorp force)) force)
6824           t)
6825          (setq data (gnus-data-find article)))
6826     (goto-char b)
6827     (if (not data)
6828         (progn
6829           (unless silent
6830             (gnus-message 3 "Can't find article %d" article))
6831           nil)
6832       (let ((pt (gnus-data-pos data)))
6833         (goto-char pt)
6834         (gnus-summary-set-article-display-arrow pt))
6835       (gnus-summary-position-point)
6836       article)))
6837
6838 ;; Walking around summary lines with displaying articles.
6839
6840 (defun gnus-summary-expand-window (&optional arg)
6841   "Make the summary buffer take up the entire Emacs frame.
6842 Given a prefix, will force an `article' buffer configuration."
6843   (interactive "P")
6844   (if arg
6845       (gnus-configure-windows 'article 'force)
6846     (gnus-configure-windows 'summary 'force)))
6847
6848 (defun gnus-summary-display-article (article &optional all-header)
6849   "Display ARTICLE in article buffer."
6850   (when (gnus-buffer-live-p gnus-article-buffer)
6851     (with-current-buffer gnus-article-buffer
6852       (set-buffer-multibyte t)))
6853   (gnus-set-global-variables)
6854   (when (gnus-buffer-live-p gnus-article-buffer)
6855     (with-current-buffer gnus-article-buffer
6856       (setq gnus-article-charset gnus-newsgroup-charset)
6857       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6858       (set-buffer-multibyte t)))
6859   (if (null article)
6860       nil
6861     (prog1
6862         (if gnus-summary-display-article-function
6863             (funcall gnus-summary-display-article-function article all-header)
6864           (gnus-article-prepare article all-header))
6865       (with-current-buffer gnus-article-buffer
6866         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6867              nil))
6868       (gnus-run-hooks 'gnus-select-article-hook)
6869       (when (and gnus-current-article
6870                  (not (zerop gnus-current-article)))
6871         (gnus-summary-goto-subject gnus-current-article))
6872       (gnus-summary-recenter)
6873       (when (and gnus-use-trees gnus-show-threads)
6874         (gnus-possibly-generate-tree article)
6875         (gnus-highlight-selected-tree article))
6876       ;; Successfully display article.
6877       (gnus-article-set-window-start
6878        (cdr (assq article gnus-newsgroup-bookmarks))))))
6879
6880 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6881   "Select the current article.
6882 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6883 non-nil, the article will be re-fetched even if it already present in
6884 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6885 be displayed."
6886   ;; Make sure we are in the summary buffer to work around bbdb bug.
6887   (unless (eq major-mode 'gnus-summary-mode)
6888     (set-buffer gnus-summary-buffer))
6889   (let ((article (or article (gnus-summary-article-number)))
6890         (all-headers (not (not all-headers))) ;Must be T or NIL.
6891         gnus-summary-display-article-function)
6892     (and (not pseudo)
6893          (gnus-summary-article-pseudo-p article)
6894          (error "This is a pseudo-article"))
6895     (save-excursion
6896       (set-buffer gnus-summary-buffer)
6897       (if (or (and gnus-single-article-buffer
6898                    (or (null gnus-current-article)
6899                        (null gnus-article-current)
6900                        (null (get-buffer gnus-article-buffer))
6901                        (not (eq article (cdr gnus-article-current)))
6902                        (not (equal (car gnus-article-current)
6903                                    gnus-newsgroup-name))))
6904               (and (not gnus-single-article-buffer)
6905                    (or (null gnus-current-article)
6906                        (not (eq gnus-current-article article))))
6907               force)
6908           ;; The requested article is different from the current article.
6909           (progn
6910             (gnus-summary-display-article article all-headers)
6911             (gnus-article-set-window-start
6912              (cdr (assq article gnus-newsgroup-bookmarks)))
6913             article)
6914         'old))))
6915
6916 (defun gnus-summary-force-verify-and-decrypt ()
6917   (interactive)
6918   (let ((mm-verify-option 'known)
6919         (mm-decrypt-option 'known)
6920         (gnus-buttonized-mime-types (append (list "multipart/signed"
6921                                                   "multipart/encrypted")
6922                                             gnus-buttonized-mime-types)))
6923     (gnus-summary-select-article nil 'force)))
6924
6925 (defun gnus-summary-set-current-mark (&optional current-mark)
6926   "Obsolete function."
6927   nil)
6928
6929 (defun gnus-summary-next-article (&optional unread subject backward push)
6930   "Select the next article.
6931 If UNREAD, only unread articles are selected.
6932 If SUBJECT, only articles with SUBJECT are selected.
6933 If BACKWARD, the previous article is selected instead of the next."
6934   (interactive "P")
6935   (cond
6936    ;; Is there such an article?
6937    ((and (gnus-summary-search-forward unread subject backward)
6938          (or (gnus-summary-display-article (gnus-summary-article-number))
6939              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6940     (gnus-summary-position-point))
6941    ;; If not, we try the first unread, if that is wanted.
6942    ((and subject
6943          gnus-auto-select-same
6944          (gnus-summary-first-unread-article))
6945     (gnus-summary-position-point)
6946     (gnus-message 6 "Wrapped"))
6947    ;; Try to get next/previous article not displayed in this group.
6948    ((and gnus-auto-extend-newsgroup
6949          (not unread) (not subject))
6950     (gnus-summary-goto-article
6951      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6952      nil (count-lines (point-min) (point))))
6953    ;; Go to next/previous group.
6954    (t
6955     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6956       (gnus-summary-jump-to-group gnus-newsgroup-name))
6957     (let ((cmd last-command-char)
6958           (point
6959            (save-excursion
6960              (set-buffer gnus-group-buffer)
6961              (point)))
6962           (group
6963            (if (eq gnus-keep-same-level 'best)
6964                (gnus-summary-best-group gnus-newsgroup-name)
6965              (gnus-summary-search-group backward gnus-keep-same-level))))
6966       ;; For some reason, the group window gets selected.  We change
6967       ;; it back.
6968       (select-window (get-buffer-window (current-buffer)))
6969       ;; Select next unread newsgroup automagically.
6970       (cond
6971        ((or (not gnus-auto-select-next)
6972             (not cmd))
6973         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6974        ((or (eq gnus-auto-select-next 'quietly)
6975             (and (eq gnus-auto-select-next 'slightly-quietly)
6976                  push)
6977             (and (eq gnus-auto-select-next 'almost-quietly)
6978                  (gnus-summary-last-article-p)))
6979         ;; Select quietly.
6980         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6981             (gnus-summary-exit)
6982           (gnus-message 7 "No more%s articles (%s)..."
6983                         (if unread " unread" "")
6984                         (if group (concat "selecting " group)
6985                           "exiting"))
6986           (gnus-summary-next-group nil group backward)))
6987        (t
6988         (when (gnus-key-press-event-p last-input-event)
6989           (gnus-summary-walk-group-buffer
6990            gnus-newsgroup-name cmd unread backward point))))))))
6991
6992 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6993   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6994                       (?\C-p (gnus-group-prev-unread-group 1))))
6995         (cursor-in-echo-area t)
6996         keve key group ended prompt)
6997     (save-excursion
6998       (set-buffer gnus-group-buffer)
6999       (goto-char start)
7000       (setq group
7001             (if (eq gnus-keep-same-level 'best)
7002                 (gnus-summary-best-group gnus-newsgroup-name)
7003               (gnus-summary-search-group backward gnus-keep-same-level))))
7004     (while (not ended)
7005       (setq prompt
7006             (format
7007              "No more%s articles%s " (if unread " unread" "")
7008              (if (and group
7009                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7010                  (format " (Type %s for %s [%s])"
7011                          (single-key-description cmd) group
7012                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7013                (format " (Type %s to exit %s)"
7014                        (single-key-description cmd)
7015                        gnus-newsgroup-name))))
7016       ;; Confirm auto selection.
7017       (setq key (car (setq keve (gnus-read-event-char prompt)))
7018             ended t)
7019       (cond
7020        ((assq key keystrokes)
7021         (let ((obuf (current-buffer)))
7022           (switch-to-buffer gnus-group-buffer)
7023           (when group
7024             (gnus-group-jump-to-group group))
7025           (eval (cadr (assq key keystrokes)))
7026           (setq group (gnus-group-group-name))
7027           (switch-to-buffer obuf))
7028         (setq ended nil))
7029        ((equal key cmd)
7030         (if (or (not group)
7031                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7032             (gnus-summary-exit)
7033           (gnus-summary-next-group nil group backward)))
7034        (t
7035         (push (cdr keve) unread-command-events))))))
7036
7037 (defun gnus-summary-next-unread-article ()
7038   "Select unread article after current one."
7039   (interactive)
7040   (gnus-summary-next-article
7041    (or (not (eq gnus-summary-goto-unread 'never))
7042        (gnus-summary-last-article-p (gnus-summary-article-number)))
7043    (and gnus-auto-select-same
7044         (gnus-summary-article-subject))))
7045
7046 (defun gnus-summary-prev-article (&optional unread subject)
7047   "Select the article after the current one.
7048 If UNREAD is non-nil, only unread articles are selected."
7049   (interactive "P")
7050   (gnus-summary-next-article unread subject t))
7051
7052 (defun gnus-summary-prev-unread-article ()
7053   "Select unread article before current one."
7054   (interactive)
7055   (gnus-summary-prev-article
7056    (or (not (eq gnus-summary-goto-unread 'never))
7057        (gnus-summary-first-article-p (gnus-summary-article-number)))
7058    (and gnus-auto-select-same
7059         (gnus-summary-article-subject))))
7060
7061 (defun gnus-summary-next-page (&optional lines circular stop)
7062   "Show next page of the selected article.
7063 If at the end of the current article, select the next article.
7064 LINES says how many lines should be scrolled up.
7065
7066 If CIRCULAR is non-nil, go to the start of the article instead of
7067 selecting the next article when reaching the end of the current
7068 article.
7069
7070 If STOP is non-nil, just stop when reaching the end of the message."
7071   (interactive "P")
7072   (setq gnus-summary-buffer (current-buffer))
7073   (gnus-set-global-variables)
7074   (let ((article (gnus-summary-article-number))
7075         (article-window (get-buffer-window gnus-article-buffer t))
7076         endp)
7077     ;; If the buffer is empty, we have no article.
7078     (unless article
7079       (error "No article to select"))
7080     (gnus-configure-windows 'article)
7081     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7082         (if (and (eq gnus-summary-goto-unread 'never)
7083                  (not (gnus-summary-last-article-p article)))
7084             (gnus-summary-next-article)
7085           (gnus-summary-next-unread-article))
7086       (if (or (null gnus-current-article)
7087               (null gnus-article-current)
7088               (/= article (cdr gnus-article-current))
7089               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7090           ;; Selected subject is different from current article's.
7091           (gnus-summary-display-article article)
7092         (when article-window
7093           (gnus-eval-in-buffer-window gnus-article-buffer
7094             (setq endp (gnus-article-next-page lines)))
7095           (when endp
7096             (cond (stop
7097                    (gnus-message 3 "End of message"))
7098                   (circular
7099                    (gnus-summary-beginning-of-article))
7100                   (lines
7101                    (gnus-message 3 "End of message"))
7102                   ((null lines)
7103                    (if (and (eq gnus-summary-goto-unread 'never)
7104                             (not (gnus-summary-last-article-p article)))
7105                        (gnus-summary-next-article)
7106                      (gnus-summary-next-unread-article))))))))
7107     (gnus-summary-recenter)
7108     (gnus-summary-position-point)))
7109
7110 (defun gnus-summary-prev-page (&optional lines move)
7111   "Show previous page of selected article.
7112 Argument LINES specifies lines to be scrolled down.
7113 If MOVE, move to the previous unread article if point is at
7114 the beginning of the buffer."
7115   (interactive "P")
7116   (let ((article (gnus-summary-article-number))
7117         (article-window (get-buffer-window gnus-article-buffer t))
7118         endp)
7119     (gnus-configure-windows 'article)
7120     (if (or (null gnus-current-article)
7121             (null gnus-article-current)
7122             (/= article (cdr gnus-article-current))
7123             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7124         ;; Selected subject is different from current article's.
7125         (gnus-summary-display-article article)
7126       (gnus-summary-recenter)
7127       (when article-window
7128         (gnus-eval-in-buffer-window gnus-article-buffer
7129           (setq endp (gnus-article-prev-page lines)))
7130         (when (and move endp)
7131           (cond (lines
7132                  (gnus-message 3 "Beginning of message"))
7133                 ((null lines)
7134                  (if (and (eq gnus-summary-goto-unread 'never)
7135                           (not (gnus-summary-first-article-p article)))
7136                      (gnus-summary-prev-article)
7137                    (gnus-summary-prev-unread-article))))))))
7138   (gnus-summary-position-point))
7139
7140 (defun gnus-summary-prev-page-or-article (&optional lines)
7141   "Show previous page of selected article.
7142 Argument LINES specifies lines to be scrolled down.
7143 If at the beginning of the article, go to the next article."
7144   (interactive "P")
7145   (gnus-summary-prev-page lines t))
7146
7147 (defun gnus-summary-scroll-up (lines)
7148   "Scroll up (or down) one line current article.
7149 Argument LINES specifies lines to be scrolled up (or down if negative)."
7150   (interactive "p")
7151   (gnus-configure-windows 'article)
7152   (gnus-summary-show-thread)
7153   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7154     (gnus-eval-in-buffer-window gnus-article-buffer
7155       (cond ((> lines 0)
7156              (when (gnus-article-next-page lines)
7157                (gnus-message 3 "End of message")))
7158             ((< lines 0)
7159              (gnus-article-prev-page (- lines))))))
7160   (gnus-summary-recenter)
7161   (gnus-summary-position-point))
7162
7163 (defun gnus-summary-scroll-down (lines)
7164   "Scroll down (or up) one line current article.
7165 Argument LINES specifies lines to be scrolled down (or up if negative)."
7166   (interactive "p")
7167   (gnus-summary-scroll-up (- lines)))
7168
7169 (defun gnus-summary-next-same-subject ()
7170   "Select next article which has the same subject as current one."
7171   (interactive)
7172   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7173
7174 (defun gnus-summary-prev-same-subject ()
7175   "Select previous article which has the same subject as current one."
7176   (interactive)
7177   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7178
7179 (defun gnus-summary-next-unread-same-subject ()
7180   "Select next unread article which has the same subject as current one."
7181   (interactive)
7182   (gnus-summary-next-article t (gnus-summary-article-subject)))
7183
7184 (defun gnus-summary-prev-unread-same-subject ()
7185   "Select previous unread article which has the same subject as current one."
7186   (interactive)
7187   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7188
7189 (defun gnus-summary-first-unread-article ()
7190   "Select the first unread article.
7191 Return nil if there are no unread articles."
7192   (interactive)
7193   (prog1
7194       (when (gnus-summary-first-subject t)
7195         (gnus-summary-show-thread)
7196         (gnus-summary-first-subject t)
7197         (gnus-summary-display-article (gnus-summary-article-number)))
7198     (gnus-summary-position-point)))
7199
7200 (defun gnus-summary-first-unread-subject ()
7201   "Place the point on the subject line of the first unread article.
7202 Return nil if there are no unread articles."
7203   (interactive)
7204   (prog1
7205       (when (gnus-summary-first-subject t)
7206         (gnus-summary-show-thread)
7207         (gnus-summary-first-subject t))
7208     (gnus-summary-position-point)))
7209
7210 (defun gnus-summary-first-unseen-subject ()
7211   "Place the point on the subject line of the first unseen article.
7212 Return nil if there are no unseen articles."
7213   (interactive)
7214   (prog1
7215       (when (gnus-summary-first-subject t t t)
7216         (gnus-summary-show-thread)
7217         (gnus-summary-first-subject t t t))
7218     (gnus-summary-position-point)))
7219
7220 (defun gnus-summary-first-unseen-or-unread-subject ()
7221   "Place the point on the subject line of the first unseen article.
7222 Return nil if there are no unseen articles."
7223   (interactive)
7224   (prog1
7225       (unless (when (gnus-summary-first-subject t t t)
7226                 (gnus-summary-show-thread)
7227                 (gnus-summary-first-subject t t t))
7228         (when (gnus-summary-first-subject t)
7229           (gnus-summary-show-thread)
7230           (gnus-summary-first-subject t)))
7231     (gnus-summary-position-point)))
7232
7233 (defun gnus-summary-first-article ()
7234   "Select the first article.
7235 Return nil if there are no articles."
7236   (interactive)
7237   (prog1
7238       (when (gnus-summary-first-subject)
7239         (gnus-summary-show-thread)
7240         (gnus-summary-first-subject)
7241         (gnus-summary-display-article (gnus-summary-article-number)))
7242     (gnus-summary-position-point)))
7243
7244 (defun gnus-summary-best-unread-article (&optional arg)
7245   "Select the unread article with the highest score.
7246 If given a prefix argument, select the next unread article that has a
7247 score higher than the default score."
7248   (interactive "P")
7249   (let ((article (if arg
7250                      (gnus-summary-better-unread-subject)
7251                    (gnus-summary-best-unread-subject))))
7252     (if article
7253         (gnus-summary-goto-article article)
7254       (error "No unread articles"))))
7255
7256 (defun gnus-summary-best-unread-subject ()
7257   "Select the unread subject with the highest score."
7258   (interactive)
7259   (let ((best -1000000)
7260         (data gnus-newsgroup-data)
7261         article score)
7262     (while data
7263       (and (gnus-data-unread-p (car data))
7264            (> (setq score
7265                     (gnus-summary-article-score (gnus-data-number (car data))))
7266               best)
7267            (setq best score
7268                  article (gnus-data-number (car data))))
7269       (setq data (cdr data)))
7270     (when article
7271       (gnus-summary-goto-subject article))
7272     (gnus-summary-position-point)
7273     article))
7274
7275 (defun gnus-summary-better-unread-subject ()
7276   "Select the first unread subject that has a score over the default score."
7277   (interactive)
7278   (let ((data gnus-newsgroup-data)
7279         article score)
7280     (while (and (setq article (gnus-data-number (car data)))
7281                 (or (gnus-data-read-p (car data))
7282                     (not (> (gnus-summary-article-score article)
7283                             gnus-summary-default-score))))
7284       (setq data (cdr data)))
7285     (when article
7286       (gnus-summary-goto-subject article))
7287     (gnus-summary-position-point)
7288     article))
7289
7290 (defun gnus-summary-last-subject ()
7291   "Go to the last displayed subject line in the group."
7292   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7293     (when article
7294       (gnus-summary-goto-subject article))))
7295
7296 (defun gnus-summary-goto-article (article &optional all-headers force)
7297   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7298 If ALL-HEADERS is non-nil, no header lines are hidden.
7299 If FORCE, go to the article even if it isn't displayed.  If FORCE
7300 is a number, it is the line the article is to be displayed on."
7301   (interactive
7302    (list
7303     (completing-read
7304      "Article number or Message-ID: "
7305      (mapcar (lambda (number) (list (int-to-string number)))
7306              gnus-newsgroup-limit))
7307     current-prefix-arg
7308     t))
7309   (prog1
7310       (if (and (stringp article)
7311                (string-match "@" article))
7312           (gnus-summary-refer-article article)
7313         (when (stringp article)
7314           (setq article (string-to-number article)))
7315         (if (gnus-summary-goto-subject article force)
7316             (gnus-summary-display-article article all-headers)
7317           (gnus-message 4 "Couldn't go to article %s" article) nil))
7318     (gnus-summary-position-point)))
7319
7320 (defun gnus-summary-goto-last-article ()
7321   "Go to the previously read article."
7322   (interactive)
7323   (prog1
7324       (when gnus-last-article
7325         (gnus-summary-goto-article gnus-last-article nil t))
7326     (gnus-summary-position-point)))
7327
7328 (defun gnus-summary-pop-article (number)
7329   "Pop one article off the history and go to the previous.
7330 NUMBER articles will be popped off."
7331   (interactive "p")
7332   (let (to)
7333     (setq gnus-newsgroup-history
7334           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7335     (if to
7336         (gnus-summary-goto-article (car to) nil t)
7337       (error "Article history empty")))
7338   (gnus-summary-position-point))
7339
7340 ;; Summary commands and functions for limiting the summary buffer.
7341
7342 (defun gnus-summary-limit-to-articles (n)
7343   "Limit the summary buffer to the next N articles.
7344 If not given a prefix, use the process marked articles instead."
7345   (interactive "P")
7346   (prog1
7347       (let ((articles (gnus-summary-work-articles n)))
7348         (setq gnus-newsgroup-processable nil)
7349         (gnus-summary-limit articles))
7350     (gnus-summary-position-point)))
7351
7352 (defun gnus-summary-pop-limit (&optional total)
7353   "Restore the previous limit.
7354 If given a prefix, remove all limits."
7355   (interactive "P")
7356   (when total
7357     (setq gnus-newsgroup-limits
7358           (list (mapcar (lambda (h) (mail-header-number h))
7359                         gnus-newsgroup-headers))))
7360   (unless gnus-newsgroup-limits
7361     (error "No limit to pop"))
7362   (prog1
7363       (gnus-summary-limit nil 'pop)
7364     (gnus-summary-position-point)))
7365
7366 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7367   "Limit the summary buffer to articles that have subjects that match a regexp.
7368 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7369   (interactive
7370    (list (read-string (if current-prefix-arg
7371                           "Exclude subject (regexp): "
7372                         "Limit to subject (regexp): "))
7373          nil current-prefix-arg))
7374   (unless header
7375     (setq header "subject"))
7376   (when (not (equal "" subject))
7377     (prog1
7378         (let ((articles (gnus-summary-find-matching
7379                          (or header "subject") subject 'all nil nil
7380                          not-matching)))
7381           (unless articles
7382             (error "Found no matches for \"%s\"" subject))
7383           (gnus-summary-limit articles))
7384       (gnus-summary-position-point))))
7385
7386 (defun gnus-summary-limit-to-author (from &optional not-matching)
7387   "Limit the summary buffer to articles that have authors that match a regexp.
7388 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7389   (interactive
7390    (list (read-string (if current-prefix-arg
7391                           "Exclude author (regexp): "
7392                         "Limit to author (regexp): "))
7393          current-prefix-arg))
7394   (gnus-summary-limit-to-subject from "from" not-matching))
7395
7396 (defun gnus-summary-limit-to-age (age &optional younger-p)
7397   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7398 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7399 articles that are younger than AGE days."
7400   (interactive
7401    (let ((younger current-prefix-arg)
7402          (days-got nil)
7403          days)
7404      (while (not days-got)
7405        (setq days (if younger
7406                       (read-string "Limit to articles younger than (in days, older when negative): ")
7407                     (read-string
7408                      "Limit to articles older than (in days, younger when negative): ")))
7409        (when (> (length days) 0)
7410          (setq days (read days)))
7411        (if (numberp days)
7412            (progn
7413              (setq days-got t)
7414              (if (< days 0)
7415                  (progn
7416                    (setq younger (not younger))
7417                    (setq days (* days -1)))))
7418          (message "Please enter a number.")
7419          (sleep-for 1)))
7420      (list days younger)))
7421   (prog1
7422       (let ((data gnus-newsgroup-data)
7423             (cutoff (days-to-time age))
7424             articles d date is-younger)
7425         (while (setq d (pop data))
7426           (when (and (vectorp (gnus-data-header d))
7427                      (setq date (mail-header-date (gnus-data-header d))))
7428             (setq is-younger (time-less-p
7429                               (time-since (condition-case ()
7430                                               (date-to-time date)
7431                                             (error '(0 0))))
7432                               cutoff))
7433             (when (if younger-p
7434                       is-younger
7435                     (not is-younger))
7436               (push (gnus-data-number d) articles))))
7437         (gnus-summary-limit (nreverse articles)))
7438     (gnus-summary-position-point)))
7439
7440 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7441   "Limit the summary buffer to articles that match an 'extra' header."
7442   (interactive
7443    (let ((header
7444           (intern
7445            (gnus-completing-read-with-default
7446             (symbol-name (car gnus-extra-headers))
7447             (if current-prefix-arg
7448                 "Exclude extra header:"
7449               "Limit extra header:")
7450             (mapcar (lambda (x)
7451                       (cons (symbol-name x) x))
7452                     gnus-extra-headers)
7453             nil
7454             t))))
7455      (list header
7456            (read-string (format "%s header %s (regexp): "
7457                                 (if current-prefix-arg "Exclude" "Limit to")
7458                                 header))
7459            current-prefix-arg)))
7460   (when (not (equal "" regexp))
7461     (prog1
7462         (let ((articles (gnus-summary-find-matching
7463                          (cons 'extra header) regexp 'all nil nil
7464                          not-matching)))
7465           (unless articles
7466             (error "Found no matches for \"%s\"" regexp))
7467           (gnus-summary-limit articles))
7468       (gnus-summary-position-point))))
7469
7470 (defun gnus-summary-limit-to-display-predicate ()
7471   "Limit the summary buffer to the predicated in the `display' group parameter."
7472   (interactive)
7473   (unless gnus-newsgroup-display
7474     (error "There is no `display' group parameter"))
7475   (let (articles)
7476     (dolist (number gnus-newsgroup-articles)
7477       (when (funcall gnus-newsgroup-display)
7478         (push number articles)))
7479     (gnus-summary-limit articles))
7480   (gnus-summary-position-point))
7481
7482 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7483 (make-obsolete
7484  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7485
7486 (defun gnus-summary-limit-to-unread (&optional all)
7487   "Limit the summary buffer to articles that are not marked as read.
7488 If ALL is non-nil, limit strictly to unread articles."
7489   (interactive "P")
7490   (if all
7491       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7492     (gnus-summary-limit-to-marks
7493      ;; Concat all the marks that say that an article is read and have
7494      ;; those removed.
7495      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7496            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7497            gnus-low-score-mark gnus-expirable-mark
7498            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7499            gnus-duplicate-mark gnus-souped-mark)
7500      'reverse)))
7501
7502 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7503 (make-obsolete 'gnus-summary-delete-marked-with
7504                'gnus-summary-limit-exclude-marks)
7505
7506 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7507   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7508 If REVERSE, limit the summary buffer to articles that are marked
7509 with MARKS.  MARKS can either be a string of marks or a list of marks.
7510 Returns how many articles were removed."
7511   (interactive "sMarks: ")
7512   (gnus-summary-limit-to-marks marks t))
7513
7514 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7515   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7516 If REVERSE (the prefix), limit the summary buffer to articles that are
7517 not marked with MARKS.  MARKS can either be a string of marks or a
7518 list of marks.
7519 Returns how many articles were removed."
7520   (interactive "sMarks: \nP")
7521   (prog1
7522       (let ((data gnus-newsgroup-data)
7523             (marks (if (listp marks) marks
7524                      (append marks nil))) ; Transform to list.
7525             articles)
7526         (while data
7527           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7528                   (memq (gnus-data-mark (car data)) marks))
7529             (push (gnus-data-number (car data)) articles))
7530           (setq data (cdr data)))
7531         (gnus-summary-limit articles))
7532     (gnus-summary-position-point)))
7533
7534 (defun gnus-summary-limit-to-score (score)
7535   "Limit to articles with score at or above SCORE."
7536   (interactive "NLimit to articles with score of at least: ")
7537   (let ((data gnus-newsgroup-data)
7538         articles)
7539     (while data
7540       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7541                 score)
7542         (push (gnus-data-number (car data)) articles))
7543       (setq data (cdr data)))
7544     (prog1
7545         (gnus-summary-limit articles)
7546       (gnus-summary-position-point))))
7547
7548 (defun gnus-summary-limit-to-unseen ()
7549   "Limit to unseen articles."
7550   (interactive)
7551   (prog1
7552       (gnus-summary-limit gnus-newsgroup-unseen)
7553     (gnus-summary-position-point)))
7554
7555 (defun gnus-summary-limit-include-thread (id)
7556   "Display all the hidden articles that is in the thread with ID in it.
7557 When called interactively, ID is the Message-ID of the current
7558 article."
7559   (interactive (list (mail-header-id (gnus-summary-article-header))))
7560   (let ((articles (gnus-articles-in-thread
7561                    (gnus-id-to-thread (gnus-root-id id)))))
7562     (prog1
7563         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7564       (gnus-summary-limit-include-matching-articles
7565        "subject"
7566        (regexp-quote (gnus-simplify-subject-re
7567                       (mail-header-subject (gnus-id-to-header id)))))
7568       (gnus-summary-position-point))))
7569
7570 (defun gnus-summary-limit-include-matching-articles (header regexp)
7571   "Display all the hidden articles that have HEADERs that match REGEXP."
7572   (interactive (list (read-string "Match on header: ")
7573                      (read-string "Regexp: ")))
7574   (let ((articles (gnus-find-matching-articles header regexp)))
7575     (prog1
7576         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7577       (gnus-summary-position-point))))
7578
7579 (defun gnus-summary-insert-dormant-articles ()
7580   "Insert all the dormat articles for this group into the current buffer."
7581   (interactive)
7582   (let ((gnus-verbose (max 6 gnus-verbose)))
7583     (if (not gnus-newsgroup-dormant)
7584         (gnus-message 3 "No cached articles for this group")
7585       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7586
7587 (defun gnus-summary-limit-include-dormant ()
7588   "Display all the hidden articles that are marked as dormant.
7589 Note that this command only works on a subset of the articles currently
7590 fetched for this group."
7591   (interactive)
7592   (unless gnus-newsgroup-dormant
7593     (error "There are no dormant articles in this group"))
7594   (prog1
7595       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7596     (gnus-summary-position-point)))
7597
7598 (defun gnus-summary-limit-exclude-dormant ()
7599   "Hide all dormant articles."
7600   (interactive)
7601   (prog1
7602       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7603     (gnus-summary-position-point)))
7604
7605 (defun gnus-summary-limit-exclude-childless-dormant ()
7606   "Hide all dormant articles that have no children."
7607   (interactive)
7608   (let ((data (gnus-data-list t))
7609         articles d children)
7610     ;; Find all articles that are either not dormant or have
7611     ;; children.
7612     (while (setq d (pop data))
7613       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7614                 (and (setq children
7615                            (gnus-article-children (gnus-data-number d)))
7616                      (let (found)
7617                        (while children
7618                          (when (memq (car children) articles)
7619                            (setq children nil
7620                                  found t))
7621                          (pop children))
7622                        found)))
7623         (push (gnus-data-number d) articles)))
7624     ;; Do the limiting.
7625     (prog1
7626         (gnus-summary-limit articles)
7627       (gnus-summary-position-point))))
7628
7629 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7630   "Mark all unread excluded articles as read.
7631 If ALL, mark even excluded ticked and dormants as read."
7632   (interactive "P")
7633   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7634   (let ((articles (gnus-sorted-ndifference
7635                    (sort
7636                     (mapcar (lambda (h) (mail-header-number h))
7637                             gnus-newsgroup-headers)
7638                     '<)
7639                    gnus-newsgroup-limit))
7640         article)
7641     (setq gnus-newsgroup-unreads
7642           (gnus-sorted-intersection gnus-newsgroup-unreads
7643                                     gnus-newsgroup-limit))
7644     (if all
7645         (setq gnus-newsgroup-dormant nil
7646               gnus-newsgroup-marked nil
7647               gnus-newsgroup-reads
7648               (nconc
7649                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7650                gnus-newsgroup-reads))
7651       (while (setq article (pop articles))
7652         (unless (or (memq article gnus-newsgroup-dormant)
7653                     (memq article gnus-newsgroup-marked))
7654           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7655
7656 (defun gnus-summary-limit (articles &optional pop)
7657   (if pop
7658       ;; We pop the previous limit off the stack and use that.
7659       (setq articles (car gnus-newsgroup-limits)
7660             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7661     ;; We use the new limit, so we push the old limit on the stack.
7662     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7663   ;; Set the limit.
7664   (setq gnus-newsgroup-limit articles)
7665   (let ((total (length gnus-newsgroup-data))
7666         (data (gnus-data-find-list (gnus-summary-article-number)))
7667         (gnus-summary-mark-below nil)   ; Inhibit this.
7668         found)
7669     ;; This will do all the work of generating the new summary buffer
7670     ;; according to the new limit.
7671     (gnus-summary-prepare)
7672     ;; Hide any threads, possibly.
7673     (gnus-summary-maybe-hide-threads)
7674     ;; Try to return to the article you were at, or one in the
7675     ;; neighborhood.
7676     (when data
7677       ;; We try to find some article after the current one.
7678       (while data
7679         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7680           (setq data nil
7681                 found t))
7682         (setq data (cdr data))))
7683     (unless found
7684       ;; If there is no data, that means that we were after the last
7685       ;; article.  The same goes when we can't find any articles
7686       ;; after the current one.
7687       (goto-char (point-max))
7688       (gnus-summary-find-prev))
7689     (gnus-set-mode-line 'summary)
7690     ;; We return how many articles were removed from the summary
7691     ;; buffer as a result of the new limit.
7692     (- total (length gnus-newsgroup-data))))
7693
7694 (defsubst gnus-invisible-cut-children (threads)
7695   (let ((num 0))
7696     (while threads
7697       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7698         (incf num))
7699       (pop threads))
7700     (< num 2)))
7701
7702 (defsubst gnus-cut-thread (thread)
7703   "Go forwards in the thread until we find an article that we want to display."
7704   (when (or (eq gnus-fetch-old-headers 'some)
7705             (eq gnus-fetch-old-headers 'invisible)
7706             (numberp gnus-fetch-old-headers)
7707             (eq gnus-build-sparse-threads 'some)
7708             (eq gnus-build-sparse-threads 'more))
7709     ;; Deal with old-fetched headers and sparse threads.
7710     (while (and
7711             thread
7712             (or
7713              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7714              (gnus-summary-article-ancient-p
7715               (mail-header-number (car thread))))
7716             (if (or (<= (length (cdr thread)) 1)
7717                     (eq gnus-fetch-old-headers 'invisible))
7718                 (setq gnus-newsgroup-limit
7719                       (delq (mail-header-number (car thread))
7720                             gnus-newsgroup-limit)
7721                       thread (cadr thread))
7722               (when (gnus-invisible-cut-children (cdr thread))
7723                 (let ((th (cdr thread)))
7724                   (while th
7725                     (if (memq (mail-header-number (caar th))
7726                               gnus-newsgroup-limit)
7727                         (setq thread (car th)
7728                               th nil)
7729                       (setq th (cdr th))))))))))
7730   thread)
7731
7732 (defun gnus-cut-threads (threads)
7733   "Cut off all uninteresting articles from the beginning of threads."
7734   (when (or (eq gnus-fetch-old-headers 'some)
7735             (eq gnus-fetch-old-headers 'invisible)
7736             (numberp gnus-fetch-old-headers)
7737             (eq gnus-build-sparse-threads 'some)
7738             (eq gnus-build-sparse-threads 'more))
7739     (let ((th threads))
7740       (while th
7741         (setcar th (gnus-cut-thread (car th)))
7742         (setq th (cdr th)))))
7743   ;; Remove nixed out threads.
7744   (delq nil threads))
7745
7746 (defun gnus-summary-initial-limit (&optional show-if-empty)
7747   "Figure out what the initial limit is supposed to be on group entry.
7748 This entails weeding out unwanted dormants, low-scored articles,
7749 fetch-old-headers verbiage, and so on."
7750   ;; Most groups have nothing to remove.
7751   (if (or gnus-inhibit-limiting
7752           (and (null gnus-newsgroup-dormant)
7753                (eq gnus-newsgroup-display 'gnus-not-ignore)
7754                (not (eq gnus-fetch-old-headers 'some))
7755                (not (numberp gnus-fetch-old-headers))
7756                (not (eq gnus-fetch-old-headers 'invisible))
7757                (null gnus-summary-expunge-below)
7758                (not (eq gnus-build-sparse-threads 'some))
7759                (not (eq gnus-build-sparse-threads 'more))
7760                (null gnus-thread-expunge-below)
7761                (not gnus-use-nocem)))
7762       ()                                ; Do nothing.
7763     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7764     (setq gnus-newsgroup-limit nil)
7765     (mapatoms
7766      (lambda (node)
7767        (unless (car (symbol-value node))
7768          ;; These threads have no parents -- they are roots.
7769          (let ((nodes (cdr (symbol-value node)))
7770                thread)
7771            (while nodes
7772              (if (and gnus-thread-expunge-below
7773                       (< (gnus-thread-total-score (car nodes))
7774                          gnus-thread-expunge-below))
7775                  (gnus-expunge-thread (pop nodes))
7776                (setq thread (pop nodes))
7777                (gnus-summary-limit-children thread))))))
7778      gnus-newsgroup-dependencies)
7779     ;; If this limitation resulted in an empty group, we might
7780     ;; pop the previous limit and use it instead.
7781     (when (and (not gnus-newsgroup-limit)
7782                show-if-empty)
7783       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7784     gnus-newsgroup-limit))
7785
7786 (defun gnus-summary-limit-children (thread)
7787   "Return 1 if this subthread is visible and 0 if it is not."
7788   ;; First we get the number of visible children to this thread.  This
7789   ;; is done by recursing down the thread using this function, so this
7790   ;; will really go down to a leaf article first, before slowly
7791   ;; working its way up towards the root.
7792   (when thread
7793     (let* ((max-lisp-eval-depth 5000)
7794            (children
7795            (if (cdr thread)
7796                (apply '+ (mapcar 'gnus-summary-limit-children
7797                                  (cdr thread)))
7798              0))
7799           (number (mail-header-number (car thread)))
7800           score)
7801       (if (and
7802            (not (memq number gnus-newsgroup-marked))
7803            (or
7804             ;; If this article is dormant and has absolutely no visible
7805             ;; children, then this article isn't visible.
7806             (and (memq number gnus-newsgroup-dormant)
7807                  (zerop children))
7808             ;; If this is "fetch-old-headered" and there is no
7809             ;; visible children, then we don't want this article.
7810             (and (or (eq gnus-fetch-old-headers 'some)
7811                      (numberp gnus-fetch-old-headers))
7812                  (gnus-summary-article-ancient-p number)
7813                  (zerop children))
7814             ;; If this is "fetch-old-headered" and `invisible', then
7815             ;; we don't want this article.
7816             (and (eq gnus-fetch-old-headers 'invisible)
7817                  (gnus-summary-article-ancient-p number))
7818             ;; If this is a sparsely inserted article with no children,
7819             ;; we don't want it.
7820             (and (eq gnus-build-sparse-threads 'some)
7821                  (gnus-summary-article-sparse-p number)
7822                  (zerop children))
7823             ;; If we use expunging, and this article is really
7824             ;; low-scored, then we don't want this article.
7825             (when (and gnus-summary-expunge-below
7826                        (< (setq score
7827                                 (or (cdr (assq number gnus-newsgroup-scored))
7828                                     gnus-summary-default-score))
7829                           gnus-summary-expunge-below))
7830               ;; We increase the expunge-tally here, but that has
7831               ;; nothing to do with the limits, really.
7832               (incf gnus-newsgroup-expunged-tally)
7833               ;; We also mark as read here, if that's wanted.
7834               (when (and gnus-summary-mark-below
7835                          (< score gnus-summary-mark-below))
7836                 (setq gnus-newsgroup-unreads
7837                       (delq number gnus-newsgroup-unreads))
7838                 (if gnus-newsgroup-auto-expire
7839                     (push number gnus-newsgroup-expirable)
7840                   (push (cons number gnus-low-score-mark)
7841                         gnus-newsgroup-reads)))
7842               t)
7843             ;; Do the `display' group parameter.
7844             (and gnus-newsgroup-display
7845                  (not (funcall gnus-newsgroup-display)))
7846             ;; Check NoCeM things.
7847             (if (and gnus-use-nocem
7848                      (gnus-nocem-unwanted-article-p
7849                       (mail-header-id (car thread))))
7850                 (progn
7851                   (setq gnus-newsgroup-unreads
7852                         (delq number gnus-newsgroup-unreads))
7853                   t))))
7854           ;; Nope, invisible article.
7855           0
7856         ;; Ok, this article is to be visible, so we add it to the limit
7857         ;; and return 1.
7858         (push number gnus-newsgroup-limit)
7859         1))))
7860
7861 (defun gnus-expunge-thread (thread)
7862   "Mark all articles in THREAD as read."
7863   (let* ((number (mail-header-number (car thread))))
7864     (incf gnus-newsgroup-expunged-tally)
7865     ;; We also mark as read here, if that's wanted.
7866     (setq gnus-newsgroup-unreads
7867           (delq number gnus-newsgroup-unreads))
7868     (if gnus-newsgroup-auto-expire
7869         (push number gnus-newsgroup-expirable)
7870       (push (cons number gnus-low-score-mark)
7871             gnus-newsgroup-reads)))
7872   ;; Go recursively through all subthreads.
7873   (mapcar 'gnus-expunge-thread (cdr thread)))
7874
7875 ;; Summary article oriented commands
7876
7877 (defun gnus-summary-refer-parent-article (n)
7878   "Refer parent article N times.
7879 If N is negative, go to ancestor -N instead.
7880 The difference between N and the number of articles fetched is returned."
7881   (interactive "p")
7882   (let ((skip 1)
7883         error header ref)
7884     (when (not (natnump n))
7885       (setq skip (abs n)
7886             n 1))
7887     (while (and (> n 0)
7888                 (not error))
7889       (setq header (gnus-summary-article-header))
7890       (if (and (eq (mail-header-number header)
7891                    (cdr gnus-article-current))
7892                (equal gnus-newsgroup-name
7893                       (car gnus-article-current)))
7894           ;; If we try to find the parent of the currently
7895           ;; displayed article, then we take a look at the actual
7896           ;; References header, since this is slightly more
7897           ;; reliable than the References field we got from the
7898           ;; server.
7899           (save-excursion
7900             (set-buffer gnus-original-article-buffer)
7901             (nnheader-narrow-to-headers)
7902             (unless (setq ref (message-fetch-field "references"))
7903               (setq ref (message-fetch-field "in-reply-to")))
7904             (widen))
7905         (setq ref
7906               ;; It's not the current article, so we take a bet on
7907               ;; the value we got from the server.
7908               (mail-header-references header)))
7909       (if (and ref
7910                (not (equal ref "")))
7911           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7912             (gnus-message 1 "Couldn't find parent"))
7913         (gnus-message 1 "No references in article %d"
7914                       (gnus-summary-article-number))
7915         (setq error t))
7916       (decf n))
7917     (gnus-summary-position-point)
7918     n))
7919
7920 (defun gnus-summary-refer-references ()
7921   "Fetch all articles mentioned in the References header.
7922 Return the number of articles fetched."
7923   (interactive)
7924   (let ((ref (mail-header-references (gnus-summary-article-header)))
7925         (current (gnus-summary-article-number))
7926         (n 0))
7927     (if (or (not ref)
7928             (equal ref ""))
7929         (error "No References in the current article")
7930       ;; For each Message-ID in the References header...
7931       (while (string-match "<[^>]*>" ref)
7932         (incf n)
7933         ;; ... fetch that article.
7934         (gnus-summary-refer-article
7935          (prog1 (match-string 0 ref)
7936            (setq ref (substring ref (match-end 0))))))
7937       (gnus-summary-goto-subject current)
7938       (gnus-summary-position-point)
7939       n)))
7940
7941 (defun gnus-summary-refer-thread (&optional limit)
7942   "Fetch all articles in the current thread.
7943 If LIMIT (the numerical prefix), fetch that many old headers instead
7944 of what's specified by the `gnus-refer-thread-limit' variable."
7945   (interactive "P")
7946   (let ((id (mail-header-id (gnus-summary-article-header)))
7947         (limit (if limit (prefix-numeric-value limit)
7948                  gnus-refer-thread-limit)))
7949     ;; We want to fetch LIMIT *old* headers, but we also have to
7950     ;; re-fetch all the headers in the current buffer, because many of
7951     ;; them may be undisplayed.  So we adjust LIMIT.
7952     (when (numberp limit)
7953       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7954     (unless (eq gnus-fetch-old-headers 'invisible)
7955       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7956       ;; Retrieve the headers and read them in.
7957       (if (eq (gnus-retrieve-headers
7958                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7959               'nov)
7960           (gnus-build-all-threads)
7961         (error "Can't fetch thread from backends that don't support NOV"))
7962       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7963     (gnus-summary-limit-include-thread id)))
7964
7965 (defun gnus-summary-refer-article (message-id)
7966   "Fetch an article specified by MESSAGE-ID."
7967   (interactive "sMessage-ID: ")
7968   (when (and (stringp message-id)
7969              (not (zerop (length message-id))))
7970     ;; Construct the correct Message-ID if necessary.
7971     ;; Suggested by tale@pawl.rpi.edu.
7972     (unless (string-match "^<" message-id)
7973       (setq message-id (concat "<" message-id)))
7974     (unless (string-match ">$" message-id)
7975       (setq message-id (concat message-id ">")))
7976     (let* ((header (gnus-id-to-header message-id))
7977            (sparse (and header
7978                         (gnus-summary-article-sparse-p
7979                          (mail-header-number header))
7980                         (memq (mail-header-number header)
7981                               gnus-newsgroup-limit)))
7982            number)
7983       (cond
7984        ;; If the article is present in the buffer we just go to it.
7985        ((and header
7986              (or (not (gnus-summary-article-sparse-p
7987                        (mail-header-number header)))
7988                  sparse))
7989         (prog1
7990             (gnus-summary-goto-article
7991              (mail-header-number header) nil t)
7992           (when sparse
7993             (gnus-summary-update-article (mail-header-number header)))))
7994        (t
7995         ;; We fetch the article.
7996         (catch 'found
7997           (dolist (gnus-override-method (gnus-refer-article-methods))
7998             (gnus-check-server gnus-override-method)
7999             ;; Fetch the header, and display the article.
8000             (when (setq number (gnus-summary-insert-subject message-id))
8001               (gnus-summary-select-article nil nil nil number)
8002               (throw 'found t)))
8003           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8004
8005 (defun gnus-refer-article-methods ()
8006   "Return a list of referable methods."
8007   (cond
8008    ;; No method, so we default to current and native.
8009    ((null gnus-refer-article-method)
8010     (list gnus-current-select-method gnus-select-method))
8011    ;; Current.
8012    ((eq 'current gnus-refer-article-method)
8013     (list gnus-current-select-method))
8014    ;; List of select methods.
8015    ((not (and (symbolp (car gnus-refer-article-method))
8016               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8017     (let (out)
8018       (dolist (method gnus-refer-article-method)
8019         (push (if (eq 'current method)
8020                   gnus-current-select-method
8021                 method)
8022               out))
8023       (nreverse out)))
8024    ;; One single select method.
8025    (t
8026     (list gnus-refer-article-method))))
8027
8028 (defun gnus-summary-edit-parameters ()
8029   "Edit the group parameters of the current group."
8030   (interactive)
8031   (gnus-group-edit-group gnus-newsgroup-name 'params))
8032
8033 (defun gnus-summary-customize-parameters ()
8034   "Customize the group parameters of the current group."
8035   (interactive)
8036   (gnus-group-customize gnus-newsgroup-name))
8037
8038 (defun gnus-summary-enter-digest-group (&optional force)
8039   "Enter an nndoc group based on the current article.
8040 If FORCE, force a digest interpretation.  If not, try
8041 to guess what the document format is."
8042   (interactive "P")
8043   (let ((conf gnus-current-window-configuration))
8044     (save-excursion
8045       (gnus-summary-select-article))
8046     (setq gnus-current-window-configuration conf)
8047     (let* ((name (format "%s-%d"
8048                          (gnus-group-prefixed-name
8049                           gnus-newsgroup-name (list 'nndoc ""))
8050                          (save-excursion
8051                            (set-buffer gnus-summary-buffer)
8052                            gnus-current-article)))
8053            (ogroup gnus-newsgroup-name)
8054            (params (append (gnus-info-params (gnus-get-info ogroup))
8055                            (list (cons 'to-group ogroup))
8056                            (list (cons 'save-article-group ogroup))))
8057            (case-fold-search t)
8058            (buf (current-buffer))
8059            dig to-address)
8060       (save-excursion
8061         (set-buffer gnus-original-article-buffer)
8062         ;; Have the digest group inherit the main mail address of
8063         ;; the parent article.
8064         (when (setq to-address (or (gnus-fetch-field "reply-to")
8065                                    (gnus-fetch-field "from")))
8066           (setq params (append
8067                         (list (cons 'to-address
8068                                     (funcall gnus-decode-encoded-word-function
8069                                              to-address))))))
8070         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8071         (insert-buffer-substring gnus-original-article-buffer)
8072         ;; Remove lines that may lead nndoc to misinterpret the
8073         ;; document type.
8074         (narrow-to-region
8075          (goto-char (point-min))
8076          (or (search-forward "\n\n" nil t) (point)))
8077         (goto-char (point-min))
8078         (delete-matching-lines "^Path:\\|^From ")
8079         (widen))
8080       (unwind-protect
8081           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8082                     (gnus-newsgroup-ephemeral-ignored-charsets
8083                      gnus-newsgroup-ignored-charsets))
8084                 (gnus-group-read-ephemeral-group
8085                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8086                               (nndoc-article-type
8087                                ,(if force 'mbox 'guess)))
8088                  t nil nil nil
8089                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8090                                                         "ADAPT")))))
8091               ;; Make all postings to this group go to the parent group.
8092               (nconc (gnus-info-params (gnus-get-info name))
8093                      params)
8094             ;; Couldn't select this doc group.
8095             (switch-to-buffer buf)
8096             (gnus-set-global-variables)
8097             (gnus-configure-windows 'summary)
8098             (gnus-message 3 "Article couldn't be entered?"))
8099         (kill-buffer dig)))))
8100
8101 (defun gnus-summary-read-document (n)
8102   "Open a new group based on the current article(s).
8103 This will allow you to read digests and other similar
8104 documents as newsgroups.
8105 Obeys the standard process/prefix convention."
8106   (interactive "P")
8107   (let* ((articles (gnus-summary-work-articles n))
8108          (ogroup gnus-newsgroup-name)
8109          (params (append (gnus-info-params (gnus-get-info ogroup))
8110                          (list (cons 'to-group ogroup))))
8111          article group egroup groups vgroup)
8112     (while (setq article (pop articles))
8113       (setq group (format "%s-%d" gnus-newsgroup-name article))
8114       (gnus-summary-remove-process-mark article)
8115       (when (gnus-summary-display-article article)
8116         (save-excursion
8117           (with-temp-buffer
8118             (insert-buffer-substring gnus-original-article-buffer)
8119             ;; Remove some headers that may lead nndoc to make
8120             ;; the wrong guess.
8121             (message-narrow-to-head)
8122             (goto-char (point-min))
8123             (delete-matching-lines "^\\(Path\\):\\|^From ")
8124             (widen)
8125             (if (setq egroup
8126                       (gnus-group-read-ephemeral-group
8127                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8128                                      (nndoc-article-type guess))
8129                        t nil t))
8130                 (progn
8131                   ;; Make all postings to this group go to the parent group.
8132                   (nconc (gnus-info-params (gnus-get-info egroup))
8133                          params)
8134                   (push egroup groups))
8135               ;; Couldn't select this doc group.
8136               (gnus-error 3 "Article couldn't be entered"))))))
8137     ;; Now we have selected all the documents.
8138     (cond
8139      ((not groups)
8140       (error "None of the articles could be interpreted as documents"))
8141      ((gnus-group-read-ephemeral-group
8142        (setq vgroup (format
8143                      "nnvirtual:%s-%s" gnus-newsgroup-name
8144                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8145        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8146        t
8147        (cons (current-buffer) 'summary)))
8148      (t
8149       (error "Couldn't select virtual nndoc group")))))
8150
8151 (defun gnus-summary-isearch-article (&optional regexp-p)
8152   "Do incremental search forward on the current article.
8153 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8154   (interactive "P")
8155   (let* ((gnus-inhibit-treatment t)
8156          (old (gnus-summary-select-article)))
8157     (gnus-configure-windows 'article)
8158     (gnus-eval-in-buffer-window gnus-article-buffer
8159       (save-restriction
8160         (widen)
8161         (when (eq 'old old)
8162           (gnus-article-show-all-headers))
8163         (goto-char (point-min))
8164         (isearch-forward regexp-p)))))
8165
8166 (defun gnus-summary-search-article-forward (regexp &optional backward)
8167   "Search for an article containing REGEXP forward.
8168 If BACKWARD, search backward instead."
8169   (interactive
8170    (list (read-string
8171           (format "Search article %s (regexp%s): "
8172                   (if current-prefix-arg "backward" "forward")
8173                   (if gnus-last-search-regexp
8174                       (concat ", default " gnus-last-search-regexp)
8175                     "")))
8176          current-prefix-arg))
8177   (if (string-equal regexp "")
8178       (setq regexp (or gnus-last-search-regexp ""))
8179     (setq gnus-last-search-regexp regexp)
8180     (setq gnus-article-before-search gnus-current-article))
8181   ;; Intentionally set gnus-last-article.
8182   (setq gnus-last-article gnus-article-before-search)
8183   (let ((gnus-last-article gnus-last-article))
8184     (if (gnus-summary-search-article regexp backward)
8185         (gnus-summary-show-thread)
8186       (error "Search failed: \"%s\"" regexp))))
8187
8188 (defun gnus-summary-search-article-backward (regexp)
8189   "Search for an article containing REGEXP backward."
8190   (interactive
8191    (list (read-string
8192           (format "Search article backward (regexp%s): "
8193                   (if gnus-last-search-regexp
8194                       (concat ", default " gnus-last-search-regexp)
8195                     "")))))
8196   (gnus-summary-search-article-forward regexp 'backward))
8197
8198 (eval-when-compile
8199   (defmacro gnus-summary-search-article-position-point (regexp backward)
8200     "Dehighlight the last matched text and goto the beginning position."
8201     (` (if (and gnus-summary-search-article-matched-data
8202                 (let ((text (caddr gnus-summary-search-article-matched-data))
8203                       (inhibit-read-only t)
8204                       buffer-read-only)
8205                   (delete-region
8206                    (goto-char (car gnus-summary-search-article-matched-data))
8207                    (cadr gnus-summary-search-article-matched-data))
8208                   (insert text)
8209                   (string-match (, regexp) text)))
8210            (if (, backward) (beginning-of-line) (end-of-line))
8211          (goto-char (if (, backward) (point-max) (point-min))))))
8212
8213   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8214     "Place point where X-Face image is displayed."
8215     (if (featurep 'xemacs)
8216         (` (let ((end (if (search-forward "\n\n" nil t)
8217                           (goto-char (1- (point)))
8218                         (point-min)))
8219                  extent)
8220              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8221              (unless (and (re-search-forward "^From:" end t)
8222                           (setq extent (extent-at (point)))
8223                           (extent-begin-glyph extent))
8224                (goto-char (, opoint)))))
8225       (` (let ((end (if (search-forward "\n\n" nil t)
8226                         (goto-char (1- (point)))
8227                       (point-min)))
8228                (start (or (search-backward "\n\n" nil t) (point-min))))
8229            (goto-char
8230             (or (text-property-any start end 'x-face-image t);; x-face-e21
8231                 (text-property-any start end 'x-face-mule-bitmap-image t)
8232                 (, opoint)))))))
8233
8234   (defmacro gnus-summary-search-article-highlight-matched-text
8235     (backward treated x-face)
8236     "Highlight matched text in the function `gnus-summary-search-article'."
8237     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8238              (end (set-marker (make-marker) (match-end 0)))
8239              (inhibit-read-only t)
8240              buffer-read-only)
8241          (unless treated
8242            (let ((,@
8243                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8244                     (mapcar
8245                      (lambda (item) (setq items (delq item items)))
8246                      '(gnus-treat-buttonize
8247                        gnus-treat-fill-article
8248                        gnus-treat-fill-long-lines
8249                        gnus-treat-emphasize
8250                        gnus-treat-highlight-headers
8251                        gnus-treat-highlight-citation
8252                        gnus-treat-highlight-signature
8253                        gnus-treat-overstrike
8254                        gnus-treat-display-xface
8255                        gnus-treat-buttonize-head
8256                        gnus-treat-decode-article-as-default-mime-charset))
8257                     (static-if (featurep 'xemacs)
8258                         items
8259                       (cons '(x-face-mule-delete-x-face-field
8260                               (quote never))
8261                             items))))
8262                  (gnus-treat-display-xface
8263                   (when (, x-face) gnus-treat-display-xface)))
8264              (gnus-article-prepare-mime-display)))
8265          (goto-char (if (, backward) start end))
8266          (when (, x-face)
8267            (gnus-summary-search-article-highlight-goto-x-face (point)))
8268          (setq gnus-summary-search-article-matched-data
8269                (list start end (buffer-substring start end)))
8270          (unless (eq start end);; matched text has been deleted. :-<
8271            (put-text-property start end 'face
8272                               (or (find-face 'isearch)
8273                                   'secondary-selection))))))
8274   )
8275
8276 (defun gnus-summary-search-article (regexp &optional backward)
8277   "Search for an article containing REGEXP.
8278 Optional argument BACKWARD means do search for backward.
8279 `gnus-select-article-hook' is not called during the search."
8280   ;; We have to require this here to make sure that the following
8281   ;; dynamic binding isn't shadowed by autoloading.
8282   (require 'gnus-async)
8283   (require 'gnus-art)
8284   (let ((gnus-select-article-hook nil)  ;Disable hook.
8285         (gnus-article-prepare-hook nil)
8286         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8287         (gnus-use-article-prefetch nil)
8288         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8289         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8290         (sum (current-buffer))
8291         (found nil)
8292         point treated)
8293     (gnus-save-hidden-threads
8294       (static-if (featurep 'xemacs)
8295           (let ((gnus-inhibit-treatment t))
8296             (setq treated (eq 'old (gnus-summary-select-article)))
8297             (when (and treated
8298                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8299                                  (window-live-p (get-buffer-window
8300                                                  gnus-article-buffer t)))))
8301               (gnus-summary-select-article nil t)
8302               (setq treated nil)))
8303         (let ((gnus-inhibit-treatment t)
8304               (x-face-mule-delete-x-face-field 'never))
8305           (setq treated (eq 'old (gnus-summary-select-article)))
8306           (when (and treated
8307                      (not
8308                       (and (gnus-buffer-live-p gnus-article-buffer)
8309                            (window-live-p (get-buffer-window
8310                                            gnus-article-buffer t))
8311                            (or (not (string-match "^\\^X-Face:" regexp))
8312                                (with-current-buffer gnus-article-buffer
8313                                  gnus-summary-search-article-matched-data)))))
8314             (gnus-summary-select-article nil t)
8315             (setq treated nil))))
8316       (set-buffer gnus-article-buffer)
8317       (widen)
8318       (if treated
8319           (progn
8320             (gnus-article-show-all-headers)
8321             (gnus-summary-search-article-position-point regexp backward))
8322         (goto-char (if backward (point-max) (point-min))))
8323       (while (not found)
8324         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8325         (if (if backward
8326                 (re-search-backward regexp nil t)
8327               (re-search-forward regexp nil t))
8328             ;; We found the regexp.
8329             (progn
8330               (gnus-summary-search-article-highlight-matched-text
8331                backward treated (string-match "^\\^X-Face:" regexp))
8332               (setq found 'found)
8333               (forward-line
8334                (/ (- 2 (window-height
8335                         (get-buffer-window gnus-article-buffer t)))
8336                   2))
8337               (set-window-start
8338                (get-buffer-window (current-buffer))
8339                (point))
8340               (set-buffer sum)
8341               (setq point (point)))
8342           ;; We didn't find it, so we go to the next article.
8343           (set-buffer sum)
8344           (setq found 'not)
8345           (while (eq found 'not)
8346             (if (not (if backward (gnus-summary-find-prev)
8347                        (gnus-summary-find-next)))
8348                 ;; No more articles.
8349                 (setq found t)
8350               ;; Select the next article and adjust point.
8351               (unless (gnus-summary-article-sparse-p
8352                        (gnus-summary-article-number))
8353                 (setq found nil)
8354                 (let ((gnus-inhibit-treatment t))
8355                   (gnus-summary-select-article))
8356                 (setq treated nil)
8357                 (set-buffer gnus-article-buffer)
8358                 (widen)
8359                 (goto-char (if backward (point-max) (point-min))))))))
8360       (gnus-message 7 ""))
8361     ;; Return whether we found the regexp.
8362     (when (eq found 'found)
8363       (goto-char point)
8364       (gnus-summary-show-thread)
8365       (gnus-summary-goto-subject gnus-current-article)
8366       (gnus-summary-position-point)
8367       t)))
8368
8369 (defun gnus-find-matching-articles (header regexp)
8370   "Return a list of all articles that match REGEXP on HEADER.
8371 This search includes all articles in the current group that Gnus has
8372 fetched headers for, whether they are displayed or not."
8373   (let ((articles nil)
8374         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8375         (case-fold-search t))
8376     (dolist (header gnus-newsgroup-headers)
8377       (when (string-match regexp (funcall func header))
8378         (push (mail-header-number header) articles)))
8379     (nreverse articles)))
8380
8381 (defun gnus-summary-find-matching (header regexp &optional backward unread
8382                                           not-case-fold not-matching)
8383   "Return a list of all articles that match REGEXP on HEADER.
8384 The search stars on the current article and goes forwards unless
8385 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8386 If UNREAD is non-nil, only unread articles will
8387 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8388 in the comparisons. If NOT-MATCHING, return a list of all articles that
8389 not match REGEXP on HEADER."
8390   (let ((case-fold-search (not not-case-fold))
8391         articles d func)
8392     (if (consp header)
8393         (if (eq (car header) 'extra)
8394             (setq func
8395                   `(lambda (h)
8396                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8397                          "")))
8398           (error "%s is an invalid header" header))
8399       (unless (fboundp (intern (concat "mail-header-" header)))
8400         (error "%s is not a valid header" header))
8401       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8402     (dolist (d (if (eq backward 'all)
8403                    gnus-newsgroup-data
8404                  (gnus-data-find-list
8405                   (gnus-summary-article-number)
8406                   (gnus-data-list backward))))
8407       (when (and (or (not unread)       ; We want all articles...
8408                      (gnus-data-unread-p d)) ; Or just unreads.
8409                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8410                  (if not-matching
8411                      (not (string-match
8412                            regexp
8413                            (funcall func (gnus-data-header d))))
8414                    (string-match regexp
8415                                  (funcall func (gnus-data-header d)))))
8416         (push (gnus-data-number d) articles))) ; Success!
8417     (nreverse articles)))
8418
8419 (defun gnus-summary-execute-command (header regexp command &optional backward)
8420   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8421 If HEADER is an empty string (or nil), the match is done on the entire
8422 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8423   (interactive
8424    (list (let ((completion-ignore-case t))
8425            (completing-read
8426             "Header name: "
8427             (mapcar (lambda (header) (list (format "%s" header)))
8428                     (append
8429                      '("Number" "Subject" "From" "Lines" "Date"
8430                        "Message-ID" "Xref" "References" "Body")
8431                      gnus-extra-headers))
8432             nil 'require-match))
8433          (read-string "Regexp: ")
8434          (read-key-sequence "Command: ")
8435          current-prefix-arg))
8436   (when (equal header "Body")
8437     (setq header ""))
8438   ;; Hidden thread subtrees must be searched as well.
8439   (gnus-summary-show-all-threads)
8440   ;; We don't want to change current point nor window configuration.
8441   (save-excursion
8442     (save-window-excursion
8443       (let (gnus-visual
8444             gnus-treat-strip-trailing-blank-lines
8445             gnus-treat-strip-leading-blank-lines
8446             gnus-treat-strip-multiple-blank-lines
8447             gnus-treat-hide-boring-headers
8448             gnus-treat-fold-newsgroups
8449             gnus-article-prepare-hook)
8450         (gnus-message 6 "Executing %s..." (key-description command))
8451         ;; We'd like to execute COMMAND interactively so as to give arguments.
8452         (gnus-execute header regexp
8453                       `(call-interactively ',(key-binding command))
8454                       backward)
8455         (gnus-message 6 "Executing %s...done" (key-description command))))))
8456
8457 (defun gnus-summary-beginning-of-article ()
8458   "Scroll the article back to the beginning."
8459   (interactive)
8460   (gnus-summary-select-article)
8461   (gnus-configure-windows 'article)
8462   (gnus-eval-in-buffer-window gnus-article-buffer
8463     (widen)
8464     (goto-char (point-min))
8465     (when gnus-page-broken
8466       (gnus-narrow-to-page))))
8467
8468 (defun gnus-summary-end-of-article ()
8469   "Scroll to the end of the article."
8470   (interactive)
8471   (gnus-summary-select-article)
8472   (gnus-configure-windows 'article)
8473   (gnus-eval-in-buffer-window gnus-article-buffer
8474     (widen)
8475     (goto-char (point-max))
8476     (recenter -3)
8477     (when gnus-page-broken
8478       (gnus-narrow-to-page))))
8479
8480 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8481   "Truncate to LEN and quote all \"(\"'s in STRING."
8482   (gnus-replace-in-string (if (and len (> (length string) len))
8483                               (substring string 0 len)
8484                             string)
8485                           "[()]" "\\\\\\&"))
8486
8487 (defun gnus-summary-print-article (&optional filename n)
8488   "Generate and print a PostScript image of the N next (mail) articles.
8489
8490 If N is negative, print the N previous articles.  If N is nil and articles
8491 have been marked with the process mark, print these instead.
8492
8493 If the optional first argument FILENAME is nil, send the image to the
8494 printer.  If FILENAME is a string, save the PostScript image in a file with
8495 that name.  If FILENAME is a number, prompt the user for the name of the file
8496 to save in."
8497   (interactive (list (ps-print-preprint current-prefix-arg)))
8498   (dolist (article (gnus-summary-work-articles n))
8499     (gnus-summary-select-article nil nil 'pseudo article)
8500     (gnus-eval-in-buffer-window gnus-article-buffer
8501       (gnus-print-buffer))
8502     (gnus-summary-remove-process-mark article))
8503   (ps-despool filename))
8504
8505 (defun gnus-print-buffer ()
8506   (let ((buffer (generate-new-buffer " *print*")))
8507     (unwind-protect
8508         (progn
8509           (copy-to-buffer buffer (point-min) (point-max))
8510           (set-buffer buffer)
8511           (gnus-article-delete-invisible-text)
8512           (gnus-remove-text-with-property 'gnus-decoration)
8513           (when (gnus-visual-p 'article-highlight 'highlight)
8514             ;; Copy-to-buffer doesn't copy overlay.  So redo
8515             ;; highlight.
8516             (let ((gnus-article-buffer buffer))
8517               (gnus-article-highlight-citation t)
8518               (gnus-article-highlight-signature)))
8519           (let ((ps-left-header
8520                  (list
8521                   (concat "("
8522                           (gnus-summary-print-truncate-and-quote
8523                            (mail-header-subject gnus-current-headers)
8524                            66) ")")
8525                   (concat "("
8526                           (gnus-summary-print-truncate-and-quote
8527                            (mail-header-from gnus-current-headers)
8528                            45) ")")))
8529                 (ps-right-header
8530                  (list
8531                   "/pagenumberstring load"
8532                   (concat "("
8533                           (mail-header-date gnus-current-headers) ")"))))
8534             (gnus-run-hooks 'gnus-ps-print-hook)
8535             (save-excursion
8536               (if window-system
8537                   (ps-spool-buffer-with-faces)
8538                 (ps-spool-buffer)))))
8539       (kill-buffer buffer))))
8540
8541 (defun gnus-summary-show-article (&optional arg)
8542   "Force redisplaying of the current article.
8543 If ARG (the prefix) is a number, show the article with the charset
8544 defined in `gnus-summary-show-article-charset-alist', or the charset
8545 input.
8546 If ARG (the prefix) is non-nil and not a number, show the raw article
8547 without any article massaging functions being run.  Normally, the key strokes
8548 are `C-u g'."
8549   (interactive "P")
8550   (cond
8551    ((numberp arg)
8552     (gnus-summary-show-article t)
8553     (let* ((gnus-newsgroup-charset
8554             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8555                 (mm-read-coding-system
8556                  "View as charset: " ;; actually it is coding system.
8557                  (save-excursion
8558                    (set-buffer gnus-article-buffer)
8559                    (mm-detect-coding-region (point) (point-max))))))
8560            (default-mime-charset gnus-newsgroup-charset)
8561            (gnus-newsgroup-ignored-charsets 'gnus-all))
8562       (gnus-summary-select-article nil 'force)
8563       (let ((deps gnus-newsgroup-dependencies)
8564             head header lines)
8565         (save-excursion
8566           (set-buffer gnus-original-article-buffer)
8567           (save-restriction
8568             (message-narrow-to-head)
8569             (setq head (buffer-string))
8570             (goto-char (point-min))
8571             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8572               (goto-char (point-max))
8573               (widen)
8574               (setq lines (1- (count-lines (point) (point-max))))))
8575           (with-temp-buffer
8576             (insert (format "211 %d Article retrieved.\n"
8577                             (cdr gnus-article-current)))
8578             (insert head)
8579             (if lines (insert (format "Lines: %d\n" lines)))
8580             (insert ".\n")
8581             (let ((nntp-server-buffer (current-buffer)))
8582               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8583         (gnus-data-set-header
8584          (gnus-data-find (cdr gnus-article-current))
8585          header)
8586         (gnus-summary-update-article-line
8587          (cdr gnus-article-current) header)
8588         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8589           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8590    ((not arg)
8591     ;; Select the article the normal way.
8592     (gnus-summary-select-article nil 'force))
8593    (t
8594     ;; We have to require this here to make sure that the following
8595     ;; dynamic binding isn't shadowed by autoloading.
8596     (require 'gnus-async)
8597     (require 'gnus-art)
8598     ;; Bind the article treatment functions to nil.
8599     (let ((gnus-have-all-headers t)
8600           gnus-article-prepare-hook
8601           gnus-article-decode-hook
8602           gnus-break-pages
8603           gnus-show-mime
8604           (gnus-inhibit-treatment t))
8605       (gnus-summary-select-article nil 'force))))
8606   (gnus-summary-goto-subject gnus-current-article)
8607   (gnus-summary-position-point))
8608
8609 (defun gnus-summary-show-raw-article ()
8610   "Show the raw article without any article massaging functions being run."
8611   (interactive)
8612   (gnus-summary-show-article t))
8613
8614 (defun gnus-summary-verbose-headers (&optional arg)
8615   "Toggle permanent full header display.
8616 If ARG is a positive number, turn header display on.
8617 If ARG is a negative number, turn header display off."
8618   (interactive "P")
8619   (setq gnus-show-all-headers
8620         (cond ((or (not (numberp arg))
8621                    (zerop arg))
8622                (not gnus-show-all-headers))
8623               ((natnump arg)
8624                t)))
8625   (gnus-summary-show-article))
8626
8627 (defun gnus-summary-toggle-header (&optional arg)
8628   "Show the headers if they are hidden, or hide them if they are shown.
8629 If ARG is a positive number, show the entire header.
8630 If ARG is a negative number, hide the unwanted header lines."
8631   (interactive "P")
8632   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8633                      (get-buffer-window gnus-article-buffer t))))
8634     (with-current-buffer gnus-article-buffer
8635       (widen)
8636       (article-narrow-to-head)
8637       (let* ((buffer-read-only nil)
8638              (inhibit-point-motion-hooks t)
8639              (hidden (if (numberp arg)
8640                          (>= arg 0)
8641                        (gnus-article-hidden-text-p 'headers)))
8642              s e)
8643         (delete-region (point-min) (point-max))
8644         (with-current-buffer gnus-original-article-buffer
8645           (goto-char (setq s (point-min)))
8646           (setq e (if (search-forward "\n\n" nil t)
8647                       (1- (point))
8648                     (point-max))))
8649         (insert-buffer-substring gnus-original-article-buffer s e)
8650         (article-decode-encoded-words)
8651         (if hidden
8652             (let ((gnus-treat-hide-headers nil)
8653                   (gnus-treat-hide-boring-headers nil))
8654               (gnus-delete-wash-type 'headers)
8655               (gnus-treat-article 'head))
8656           (gnus-treat-article 'head))
8657         (widen)
8658         (if window
8659             (set-window-start window (goto-char (point-min))))
8660         (setq gnus-page-broken
8661               (when gnus-break-pages
8662                 (gnus-narrow-to-page)
8663                 t))
8664         (gnus-set-mode-line 'article)))))
8665
8666 (defun gnus-summary-show-all-headers ()
8667   "Make all header lines visible."
8668   (interactive)
8669   (gnus-summary-toggle-header 1))
8670
8671 (defun gnus-summary-toggle-mime (&optional arg)
8672   "Toggle MIME processing.
8673 If ARG is a positive number, turn MIME processing on."
8674   (interactive "P")
8675   (setq gnus-show-mime
8676         (if (null arg)
8677             (not gnus-show-mime)
8678           (> (prefix-numeric-value arg) 0)))
8679   (gnus-summary-select-article t 'force))
8680
8681 (defun gnus-summary-caesar-message (&optional arg)
8682   "Caesar rotate the current article by 13.
8683 The numerical prefix specifies how many places to rotate each letter
8684 forward."
8685   (interactive "P")
8686   (gnus-summary-select-article)
8687   (let ((mail-header-separator ""))
8688     (gnus-eval-in-buffer-window gnus-article-buffer
8689       (save-restriction
8690         (widen)
8691         (let ((start (window-start))
8692               buffer-read-only)
8693           (message-caesar-buffer-body arg)
8694           (set-window-start (get-buffer-window (current-buffer)) start))))))
8695
8696 (autoload 'unmorse-region "morse"
8697   "Convert morse coded text in region to ordinary ASCII text."
8698   t)
8699
8700 (defun gnus-summary-morse-message (&optional arg)
8701   "Morse decode the current article."
8702   (interactive "P")
8703   (gnus-summary-select-article)
8704   (let ((mail-header-separator ""))
8705     (gnus-eval-in-buffer-window gnus-article-buffer
8706       (save-excursion
8707         (save-restriction
8708           (widen)
8709           (let ((pos (window-start))
8710                 buffer-read-only)
8711             (goto-char (point-min))
8712             (when (message-goto-body)
8713               (gnus-narrow-to-body))
8714             (goto-char (point-min))
8715             (while (re-search-forward "·" (point-max) t)
8716               (replace-match "."))
8717             (unmorse-region (point-min) (point-max))
8718             (widen)
8719             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8720
8721 (defun gnus-summary-stop-page-breaking ()
8722   "Stop page breaking in the current article."
8723   (interactive)
8724   (gnus-summary-select-article)
8725   (gnus-eval-in-buffer-window gnus-article-buffer
8726     (widen)
8727     (when (gnus-visual-p 'page-marker)
8728       (let ((buffer-read-only nil))
8729         (gnus-remove-text-with-property 'gnus-prev)
8730         (gnus-remove-text-with-property 'gnus-next))
8731       (setq gnus-page-broken nil))))
8732
8733 (defun gnus-summary-move-article (&optional n to-newsgroup
8734                                             select-method action)
8735   "Move the current article to a different newsgroup.
8736 If N is a positive number, move the N next articles.
8737 If N is a negative number, move the N previous articles.
8738 If N is nil and any articles have been marked with the process mark,
8739 move those articles instead.
8740 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8741 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8742 re-spool using this method.
8743
8744 When called interactively with TO-NEWSGROUP being nil, the value of
8745 the variable `gnus-move-split-methods' is used for finding a default
8746 for the target newsgroup.
8747
8748 For this function to work, both the current newsgroup and the
8749 newsgroup that you want to move to have to support the `request-move'
8750 and `request-accept' functions.
8751
8752 ACTION can be either `move' (the default), `crosspost' or `copy'."
8753   (interactive "P")
8754   (unless action
8755     (setq action 'move))
8756   ;; Check whether the source group supports the required functions.
8757   (cond ((and (eq action 'move)
8758               (not (gnus-check-backend-function
8759                     'request-move-article gnus-newsgroup-name)))
8760          (error "The current group does not support article moving"))
8761         ((and (eq action 'crosspost)
8762               (not (gnus-check-backend-function
8763                     'request-replace-article gnus-newsgroup-name)))
8764          (error "The current group does not support article editing")))
8765   (let ((articles (gnus-summary-work-articles n))
8766         (prefix (if (gnus-check-backend-function
8767                      'request-move-article gnus-newsgroup-name)
8768                     (gnus-group-real-prefix gnus-newsgroup-name)
8769                   ""))
8770         (names '((move "Move" "Moving")
8771                  (copy "Copy" "Copying")
8772                  (crosspost "Crosspost" "Crossposting")))
8773         (copy-buf (save-excursion
8774                     (nnheader-set-temp-buffer " *copy article*")))
8775         (default-marks gnus-article-mark-lists)
8776         (no-expire-marks (delete '(expirable . expire)
8777                                  (copy-sequence gnus-article-mark-lists)))
8778         art-group to-method new-xref article to-groups)
8779     (unless (assq action names)
8780       (error "Unknown action %s" action))
8781     ;; Read the newsgroup name.
8782     (when (and (not to-newsgroup)
8783                (not select-method))
8784       (if (and gnus-move-split-methods
8785                (not
8786                 (and (memq gnus-current-article articles)
8787                      (gnus-buffer-live-p gnus-original-article-buffer))))
8788           ;; When `gnus-move-split-methods' is non-nil, we have to
8789           ;; select an article to give `gnus-read-move-group-name' an
8790           ;; opportunity to suggest an appropriate default.  However,
8791           ;; we needn't render or mark the article.
8792           (let ((gnus-display-mime-function nil)
8793                 (gnus-article-prepare-hook nil)
8794                 (gnus-mark-article-hook nil))
8795             (gnus-summary-select-article nil nil nil (car articles))))
8796       (setq to-newsgroup
8797             (gnus-read-move-group-name
8798              (cadr (assq action names))
8799              (symbol-value (intern (format "gnus-current-%s-group" action)))
8800              articles prefix))
8801       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8802     (setq to-method (or select-method
8803                         (gnus-server-to-method
8804                          (gnus-group-method to-newsgroup))))
8805     ;; Check the method we are to move this article to...
8806     (unless (gnus-check-backend-function
8807              'request-accept-article (car to-method))
8808       (error "%s does not support article copying" (car to-method)))
8809     (unless (gnus-check-server to-method)
8810       (error "Can't open server %s" (car to-method)))
8811     (gnus-message 6 "%s to %s: %s..."
8812                   (caddr (assq action names))
8813                   (or (car select-method) to-newsgroup) articles)
8814     (while articles
8815       (setq article (pop articles))
8816       (setq
8817        art-group
8818        (cond
8819         ;; Move the article.
8820         ((eq action 'move)
8821          ;; Remove this article from future suppression.
8822          (gnus-dup-unsuppress-article article)
8823          (gnus-request-move-article
8824           article                       ; Article to move
8825           gnus-newsgroup-name           ; From newsgroup
8826           (nth 1 (gnus-find-method-for-group
8827                   gnus-newsgroup-name)) ; Server
8828           (list 'gnus-request-accept-article
8829                 to-newsgroup (list 'quote select-method)
8830                 (not articles) t)       ; Accept form
8831           (not articles)))              ; Only save nov last time
8832         ;; Copy the article.
8833         ((eq action 'copy)
8834          (save-excursion
8835            (set-buffer copy-buf)
8836            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8837              (gnus-request-accept-article
8838               to-newsgroup select-method (not articles) t))))
8839         ;; Crosspost the article.
8840         ((eq action 'crosspost)
8841          (let ((xref (message-tokenize-header
8842                       (mail-header-xref (gnus-summary-article-header article))
8843                       " ")))
8844            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8845                                   ":" (number-to-string article)))
8846            (unless xref
8847              (setq xref (list (system-name))))
8848            (setq new-xref
8849                  (concat
8850                   (mapconcat 'identity
8851                              (delete "Xref:" (delete new-xref xref))
8852                              " ")
8853                   " " new-xref))
8854            (save-excursion
8855              (set-buffer copy-buf)
8856              ;; First put the article in the destination group.
8857              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8858              (when (consp (setq art-group
8859                                 (gnus-request-accept-article
8860                                  to-newsgroup select-method (not articles))))
8861                (setq new-xref (concat new-xref " " (car art-group)
8862                                       ":"
8863                                       (number-to-string (cdr art-group))))
8864                ;; Now we have the new Xrefs header, so we insert
8865                ;; it and replace the new article.
8866                (nnheader-replace-header "Xref" new-xref)
8867                (gnus-request-replace-article
8868                 (cdr art-group) to-newsgroup (current-buffer))
8869                art-group))))))
8870       (cond
8871        ((not art-group)
8872         (gnus-message 1 "Couldn't %s article %s: %s"
8873                       (cadr (assq action names)) article
8874                       (nnheader-get-report (car to-method))))
8875        ((eq art-group 'junk)
8876         (when (eq action 'move)
8877           (gnus-summary-mark-article article gnus-canceled-mark)
8878           (gnus-message 4 "Deleted article %s" article)))
8879        (t
8880         (let* ((pto-group (gnus-group-prefixed-name
8881                            (car art-group) to-method))
8882                (entry
8883                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8884                (info (nth 2 entry))
8885                (to-group (gnus-info-group info))
8886                to-marks)
8887           ;; Update the group that has been moved to.
8888           (when (and info
8889                      (memq action '(move copy)))
8890             (unless (member to-group to-groups)
8891               (push to-group to-groups))
8892
8893             (unless (memq article gnus-newsgroup-unreads)
8894               (push 'read to-marks)
8895               (gnus-info-set-read
8896                info (gnus-add-to-range (gnus-info-read info)
8897                                        (list (cdr art-group)))))
8898
8899             ;; See whether the article is to be put in the cache.
8900             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8901                              default-marks
8902                            no-expire-marks))
8903                   (to-article (cdr art-group)))
8904
8905               ;; Enter the article into the cache in the new group,
8906               ;; if that is required.
8907               (when gnus-use-cache
8908                 (gnus-cache-possibly-enter-article
8909                  to-group to-article
8910                  (let ((header (copy-sequence
8911                                 (gnus-summary-article-header article))))
8912                    (mail-header-set-number header to-article)
8913                    header)
8914                  (memq article gnus-newsgroup-marked)
8915                  (memq article gnus-newsgroup-dormant)
8916                  (memq article gnus-newsgroup-unreads)))
8917
8918               (when gnus-preserve-marks
8919                 ;; Copy any marks over to the new group.
8920                 (when (and (equal to-group gnus-newsgroup-name)
8921                            (not (memq article gnus-newsgroup-unreads)))
8922                   ;; Mark this article as read in this group.
8923                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8924                   (setcdr (gnus-active to-group) to-article)
8925                   (setcdr gnus-newsgroup-active to-article))
8926
8927                 (while marks
8928                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8929                     (when (memq article (symbol-value
8930                                          (intern (format "gnus-newsgroup-%s"
8931                                                          (caar marks)))))
8932                       (push (cdar marks) to-marks)
8933                       ;; If the other group is the same as this group,
8934                       ;; then we have to add the mark to the list.
8935                       (when (equal to-group gnus-newsgroup-name)
8936                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8937                              (cons to-article
8938                                    (symbol-value
8939                                     (intern (format "gnus-newsgroup-%s"
8940                                                     (caar marks)))))))
8941                       ;; Copy the marks to other group.
8942                       (gnus-add-marked-articles
8943                        to-group (cdar marks) (list to-article) info)))
8944                   (setq marks (cdr marks)))
8945
8946                 (gnus-request-set-mark
8947                  to-group (list (list (list to-article) 'add to-marks))))
8948
8949               (gnus-dribble-enter
8950                (concat "(gnus-group-set-info '"
8951                        (gnus-prin1-to-string (gnus-get-info to-group))
8952                        ")"))))
8953
8954           ;; Update the Xref header in this article to point to
8955           ;; the new crossposted article we have just created.
8956           (when (eq action 'crosspost)
8957             (save-excursion
8958               (set-buffer copy-buf)
8959               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8960               (nnheader-replace-header "Xref" new-xref)
8961               (gnus-request-replace-article
8962                article gnus-newsgroup-name (current-buffer)))))
8963
8964         ;;;!!!Why is this necessary?
8965         (set-buffer gnus-summary-buffer)
8966
8967         (gnus-summary-goto-subject article)
8968         (when (eq action 'move)
8969           (gnus-summary-mark-article article gnus-canceled-mark))))
8970       (gnus-summary-remove-process-mark article))
8971     ;; Re-activate all groups that have been moved to.
8972     (save-excursion
8973       (set-buffer gnus-group-buffer)
8974       (let ((gnus-group-marked to-groups))
8975         (gnus-group-get-new-news-this-group nil t)))
8976
8977     (gnus-kill-buffer copy-buf)
8978     (gnus-summary-position-point)
8979     (gnus-set-mode-line 'summary)))
8980
8981 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8982   "Move the current article to a different newsgroup.
8983 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8984 When called interactively, if TO-NEWSGROUP is nil, use the value of
8985 the variable `gnus-move-split-methods' for finding a default target
8986 newsgroup.
8987 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8988 re-spool using this method."
8989   (interactive "P")
8990   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8991
8992 (defun gnus-summary-crosspost-article (&optional n)
8993   "Crosspost the current article to some other group."
8994   (interactive "P")
8995   (gnus-summary-move-article n nil nil 'crosspost))
8996
8997 (defcustom gnus-summary-respool-default-method nil
8998   "Default method type for respooling an article.
8999 If nil, use to the current newsgroup method."
9000   :type 'symbol
9001   :group 'gnus-summary-mail)
9002
9003 (defcustom gnus-summary-display-while-building nil
9004   "If not-nil, show and update the summary buffer as it's being built.
9005 If the value is t, update the buffer after every line is inserted.  If
9006 the value is an integer (N), update the display every N lines."
9007   :group 'gnus-thread
9008   :type '(choice (const :tag "off" nil)
9009                  number
9010                  (const :tag "frequently" t)))
9011
9012 (defun gnus-summary-respool-article (&optional n method)
9013   "Respool the current article.
9014 The article will be squeezed through the mail spooling process again,
9015 which means that it will be put in some mail newsgroup or other
9016 depending on `nnmail-split-methods'.
9017 If N is a positive number, respool the N next articles.
9018 If N is a negative number, respool the N previous articles.
9019 If N is nil and any articles have been marked with the process mark,
9020 respool those articles instead.
9021
9022 Respooling can be done both from mail groups and \"real\" newsgroups.
9023 In the former case, the articles in question will be moved from the
9024 current group into whatever groups they are destined to.  In the
9025 latter case, they will be copied into the relevant groups."
9026   (interactive
9027    (list current-prefix-arg
9028          (let* ((methods (gnus-methods-using 'respool))
9029                 (methname
9030                  (symbol-name (or gnus-summary-respool-default-method
9031                                   (car (gnus-find-method-for-group
9032                                         gnus-newsgroup-name)))))
9033                 (method
9034                  (gnus-completing-read-with-default
9035                   methname "What backend do you want to use when respooling?"
9036                   methods nil t nil 'gnus-mail-method-history))
9037                 ms)
9038            (cond
9039             ((zerop (length (setq ms (gnus-servers-using-backend
9040                                       (intern method)))))
9041              (list (intern method) ""))
9042             ((= 1 (length ms))
9043              (car ms))
9044             (t
9045              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9046                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9047                            ms-alist))))))))
9048   (unless method
9049     (error "No method given for respooling"))
9050   (if (assoc (symbol-name
9051               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9052              (gnus-methods-using 'respool))
9053       (gnus-summary-move-article n nil method)
9054     (gnus-summary-copy-article n nil method)))
9055
9056 (defun gnus-summary-import-article (file &optional edit)
9057   "Import an arbitrary file into a mail newsgroup."
9058   (interactive "fImport file: \nP")
9059   (let ((group gnus-newsgroup-name)
9060         (now (current-time))
9061         atts lines group-art)
9062     (unless (gnus-check-backend-function 'request-accept-article group)
9063       (error "%s does not support article importing" group))
9064     (or (file-readable-p file)
9065         (not (file-regular-p file))
9066         (error "Can't read %s" file))
9067     (save-excursion
9068       (set-buffer (gnus-get-buffer-create " *import file*"))
9069       (erase-buffer)
9070       (nnheader-insert-file-contents file)
9071       (goto-char (point-min))
9072       (if (nnheader-article-p)
9073           (save-restriction
9074             (goto-char (point-min))
9075             (search-forward "\n\n" nil t)
9076             (narrow-to-region (point-min) (1- (point)))
9077             (goto-char (point-min))
9078             (unless (re-search-forward "^date:" nil t)
9079               (goto-char (point-max))
9080               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9081         ;; This doesn't look like an article, so we fudge some headers.
9082         (setq atts (file-attributes file)
9083               lines (count-lines (point-min) (point-max)))
9084         (insert "From: " (read-string "From: ") "\n"
9085                 "Subject: " (read-string "Subject: ") "\n"
9086                 "Date: " (message-make-date (nth 5 atts)) "\n"
9087                 "Message-ID: " (message-make-message-id) "\n"
9088                 "Lines: " (int-to-string lines) "\n"
9089                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9090       (setq group-art (gnus-request-accept-article group nil t))
9091       (kill-buffer (current-buffer)))
9092     (setq gnus-newsgroup-active (gnus-activate-group group))
9093     (forward-line 1)
9094     (gnus-summary-goto-article (cdr group-art) nil t)
9095     (when edit
9096       (gnus-summary-edit-article))))
9097
9098 (defun gnus-summary-create-article ()
9099   "Create an article in a mail newsgroup."
9100   (interactive)
9101   (let ((group gnus-newsgroup-name)
9102         (now (current-time))
9103         group-art)
9104     (unless (gnus-check-backend-function 'request-accept-article group)
9105       (error "%s does not support article importing" group))
9106     (save-excursion
9107       (set-buffer (gnus-get-buffer-create " *import file*"))
9108       (erase-buffer)
9109       (goto-char (point-min))
9110       ;; This doesn't look like an article, so we fudge some headers.
9111       (insert "From: " (read-string "From: ") "\n"
9112               "Subject: " (read-string "Subject: ") "\n"
9113               "Date: " (message-make-date now) "\n"
9114               "Message-ID: " (message-make-message-id) "\n")
9115       (setq group-art (gnus-request-accept-article group nil t))
9116       (kill-buffer (current-buffer)))
9117     (setq gnus-newsgroup-active (gnus-activate-group group))
9118     (forward-line 1)
9119     (gnus-summary-goto-article (cdr group-art) nil t)
9120     (gnus-summary-edit-article)))
9121
9122 (defun gnus-summary-article-posted-p ()
9123   "Say whether the current (mail) article is available from news as well.
9124 This will be the case if the article has both been mailed and posted."
9125   (interactive)
9126   (let ((id (mail-header-references (gnus-summary-article-header)))
9127         (gnus-override-method (car (gnus-refer-article-methods))))
9128     (if (gnus-request-head id "")
9129         (gnus-message 2 "The current message was found on %s"
9130                       gnus-override-method)
9131       (gnus-message 2 "The current message couldn't be found on %s"
9132                     gnus-override-method)
9133       nil)))
9134
9135 (defun gnus-summary-expire-articles (&optional now)
9136   "Expire all articles that are marked as expirable in the current group."
9137   (interactive)
9138   (when (gnus-check-backend-function
9139          'request-expire-articles gnus-newsgroup-name)
9140     ;; This backend supports expiry.
9141     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9142            (expirable (if total
9143                           (progn
9144                             ;; We need to update the info for
9145                             ;; this group for `gnus-list-of-read-articles'
9146                             ;; to give us the right answer.
9147                             (gnus-run-hooks 'gnus-exit-group-hook)
9148                             (gnus-summary-update-info)
9149                             (gnus-list-of-read-articles gnus-newsgroup-name))
9150                         (setq gnus-newsgroup-expirable
9151                               (sort gnus-newsgroup-expirable '<))))
9152            (expiry-wait (if now 'immediate
9153                           (gnus-group-find-parameter
9154                            gnus-newsgroup-name 'expiry-wait)))
9155            (nnmail-expiry-target
9156             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9157                 nnmail-expiry-target))
9158            es)
9159       (when expirable
9160         ;; There are expirable articles in this group, so we run them
9161         ;; through the expiry process.
9162         (gnus-message 6 "Expiring articles...")
9163         (unless (gnus-check-group gnus-newsgroup-name)
9164           (error "Can't open server for %s" gnus-newsgroup-name))
9165         ;; The list of articles that weren't expired is returned.
9166         (save-excursion
9167           (if expiry-wait
9168               (let ((nnmail-expiry-wait-function nil)
9169                     (nnmail-expiry-wait expiry-wait))
9170                 (setq es (gnus-request-expire-articles
9171                           expirable gnus-newsgroup-name)))
9172             (setq es (gnus-request-expire-articles
9173                       expirable gnus-newsgroup-name)))
9174           (unless total
9175             (setq gnus-newsgroup-expirable es))
9176           ;; We go through the old list of expirable, and mark all
9177           ;; really expired articles as nonexistent.
9178           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9179             (let ((gnus-use-cache nil))
9180               (dolist (article expirable)
9181                 (when (and (not (memq article es))
9182                            (gnus-data-find article))
9183                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9184         (gnus-message 6 "Expiring articles...done")))))
9185
9186 (defun gnus-summary-expire-articles-now ()
9187   "Expunge all expirable articles in the current group.
9188 This means that *all* articles that are marked as expirable will be
9189 deleted forever, right now."
9190   (interactive)
9191   (or gnus-expert-user
9192       (gnus-yes-or-no-p
9193        "Are you really, really, really sure you want to delete all these messages? ")
9194       (error "Phew!"))
9195   (gnus-summary-expire-articles t))
9196
9197 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9198 (defun gnus-summary-delete-article (&optional n)
9199   "Delete the N next (mail) articles.
9200 This command actually deletes articles.  This is not a marking
9201 command.  The article will disappear forever from your life, never to
9202 return.
9203 If N is negative, delete backwards.
9204 If N is nil and articles have been marked with the process mark,
9205 delete these instead."
9206   (interactive "P")
9207   (unless (gnus-check-backend-function 'request-expire-articles
9208                                        gnus-newsgroup-name)
9209     (error "The current newsgroup does not support article deletion"))
9210   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9211     (error "Couldn't open server"))
9212   ;; Compute the list of articles to delete.
9213   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9214         (nnmail-expiry-target 'delete)
9215         not-deleted)
9216     (if (and gnus-novice-user
9217              (not (gnus-yes-or-no-p
9218                    (format "Do you really want to delete %s forever? "
9219                            (if (> (length articles) 1)
9220                                (format "these %s articles" (length articles))
9221                              "this article")))))
9222         ()
9223       ;; Delete the articles.
9224       (setq not-deleted (gnus-request-expire-articles
9225                          articles gnus-newsgroup-name 'force))
9226       (while articles
9227         (gnus-summary-remove-process-mark (car articles))
9228         ;; The backend might not have been able to delete the article
9229         ;; after all.
9230         (unless (memq (car articles) not-deleted)
9231           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9232         (setq articles (cdr articles)))
9233       (when not-deleted
9234         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9235     (gnus-summary-position-point)
9236     (gnus-set-mode-line 'summary)
9237     not-deleted))
9238
9239 (defun gnus-summary-edit-article (&optional force)
9240   "Edit the current article.
9241 This will have permanent effect only in mail groups.
9242 If FORCE is non-nil, allow editing of articles even in read-only
9243 groups."
9244   (interactive "P")
9245   (save-excursion
9246     (set-buffer gnus-summary-buffer)
9247     (let ((mail-parse-charset gnus-newsgroup-charset)
9248           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9249       (gnus-set-global-variables)
9250       (when (and (not force)
9251                  (gnus-group-read-only-p))
9252         (error "The current newsgroup does not support article editing"))
9253       (gnus-summary-show-article t)
9254       (gnus-article-edit-article
9255        'ignore
9256        `(lambda (no-highlight)
9257           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9258                 (message-options message-options)
9259                 (message-options-set-recipient)
9260                 (mail-parse-ignored-charsets
9261                  ',gnus-newsgroup-ignored-charsets))
9262             (gnus-summary-edit-article-done
9263              ,(or (mail-header-references gnus-current-headers) "")
9264              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9265
9266 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9267
9268 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9269                                                  no-highlight)
9270   "Make edits to the current article permanent."
9271   (interactive)
9272   (save-excursion
9273     ;; The buffer restriction contains the entire article if it exists.
9274     (when (article-goto-body)
9275       (let ((lines (count-lines (point) (point-max)))
9276             (length (- (point-max) (point)))
9277             (case-fold-search t)
9278             (body (copy-marker (point))))
9279         (goto-char (point-min))
9280         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9281           (delete-region (match-beginning 1) (match-end 1))
9282           (insert (number-to-string length)))
9283         (goto-char (point-min))
9284         (when (re-search-forward
9285                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9286           (delete-region (match-beginning 1) (match-end 1))
9287           (insert (number-to-string length)))
9288         (goto-char (point-min))
9289         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9290           (delete-region (match-beginning 1) (match-end 1))
9291           (insert (number-to-string lines))))))
9292   ;; Replace the article.
9293   (let ((buf (current-buffer)))
9294     (with-temp-buffer
9295       (insert-buffer-substring buf)
9296
9297       (if (and (not read-only)
9298                (not (gnus-request-replace-article
9299                      (cdr gnus-article-current) (car gnus-article-current)
9300                      (current-buffer) t)))
9301           (error "Couldn't replace article")
9302         ;; Update the summary buffer.
9303         (if (and references
9304                  (equal (message-tokenize-header references " ")
9305                         (message-tokenize-header
9306                          (or (message-fetch-field "references") "") " ")))
9307             ;; We only have to update this line.
9308             (save-excursion
9309               (save-restriction
9310                 (message-narrow-to-head)
9311                 (let ((head (buffer-string))
9312                       header)
9313                   (with-temp-buffer
9314                     (insert (format "211 %d Article retrieved.\n"
9315                                     (cdr gnus-article-current)))
9316                     (insert head)
9317                     (insert ".\n")
9318                     (let ((nntp-server-buffer (current-buffer)))
9319                       (setq header (car (gnus-get-newsgroup-headers
9320                                          nil t))))
9321                     (save-excursion
9322                       (set-buffer gnus-summary-buffer)
9323                       (gnus-data-set-header
9324                        (gnus-data-find (cdr gnus-article-current))
9325                        header)
9326                       (gnus-summary-update-article-line
9327                        (cdr gnus-article-current) header)
9328                       (if (gnus-summary-goto-subject
9329                            (cdr gnus-article-current) nil t)
9330                           (gnus-summary-update-secondary-mark
9331                            (cdr gnus-article-current))))))))
9332           ;; Update threads.
9333           (set-buffer (or buffer gnus-summary-buffer))
9334           (gnus-summary-update-article (cdr gnus-article-current))
9335           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9336               (gnus-summary-update-secondary-mark
9337                (cdr gnus-article-current))))
9338         ;; Prettify the article buffer again.
9339         (unless no-highlight
9340           (save-excursion
9341             (set-buffer gnus-article-buffer)
9342             ;;;!!! Fix this -- article should be rehighlighted.
9343             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9344             (set-buffer gnus-original-article-buffer)
9345             (gnus-request-article
9346              (cdr gnus-article-current)
9347              (car gnus-article-current) (current-buffer))))
9348         ;; Prettify the summary buffer line.
9349         (when (gnus-visual-p 'summary-highlight 'highlight)
9350           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9351
9352 (defun gnus-summary-edit-wash (key)
9353   "Perform editing command KEY in the article buffer."
9354   (interactive
9355    (list
9356     (progn
9357       (message "%s" (concat (this-command-keys) "- "))
9358       (read-char))))
9359   (message "")
9360   (gnus-summary-edit-article)
9361   (execute-kbd-macro (concat (this-command-keys) key))
9362   (gnus-article-edit-done))
9363
9364 ;;; Respooling
9365
9366 (defun gnus-summary-respool-query (&optional silent trace)
9367   "Query where the respool algorithm would put this article."
9368   (interactive)
9369   (let (gnus-mark-article-hook)
9370     (gnus-summary-select-article)
9371     (save-excursion
9372       (set-buffer gnus-original-article-buffer)
9373       (save-restriction
9374         (message-narrow-to-head)
9375         (let ((groups (nnmail-article-group 'identity trace)))
9376           (unless silent
9377             (if groups
9378                 (message "This message would go to %s"
9379                          (mapconcat 'car groups ", "))
9380               (message "This message would go to no groups"))
9381             groups))))))
9382
9383 (defun gnus-summary-respool-trace ()
9384   "Trace where the respool algorithm would put this article.
9385 Display a buffer showing all fancy splitting patterns which matched."
9386   (interactive)
9387   (gnus-summary-respool-query nil t))
9388
9389 ;; Summary marking commands.
9390
9391 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9392   "Mark articles which has the same subject as read, and then select the next.
9393 If UNMARK is positive, remove any kind of mark.
9394 If UNMARK is negative, tick articles."
9395   (interactive "P")
9396   (when unmark
9397     (setq unmark (prefix-numeric-value unmark)))
9398   (let ((count
9399          (gnus-summary-mark-same-subject
9400           (gnus-summary-article-subject) unmark)))
9401     ;; Select next unread article.  If auto-select-same mode, should
9402     ;; select the first unread article.
9403     (gnus-summary-next-article t (and gnus-auto-select-same
9404                                       (gnus-summary-article-subject)))
9405     (gnus-message 7 "%d article%s marked as %s"
9406                   count (if (= count 1) " is" "s are")
9407                   (if unmark "unread" "read"))))
9408
9409 (defun gnus-summary-kill-same-subject (&optional unmark)
9410   "Mark articles which has the same subject as read.
9411 If UNMARK is positive, remove any kind of mark.
9412 If UNMARK is negative, tick articles."
9413   (interactive "P")
9414   (when unmark
9415     (setq unmark (prefix-numeric-value unmark)))
9416   (let ((count
9417          (gnus-summary-mark-same-subject
9418           (gnus-summary-article-subject) unmark)))
9419     ;; If marked as read, go to next unread subject.
9420     (when (null unmark)
9421       ;; Go to next unread subject.
9422       (gnus-summary-next-subject 1 t))
9423     (gnus-message 7 "%d articles are marked as %s"
9424                   count (if unmark "unread" "read"))))
9425
9426 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9427   "Mark articles with same SUBJECT as read, and return marked number.
9428 If optional argument UNMARK is positive, remove any kinds of marks.
9429 If optional argument UNMARK is negative, mark articles as unread instead."
9430   (let ((count 1))
9431     (save-excursion
9432       (cond
9433        ((null unmark)                   ; Mark as read.
9434         (while (and
9435                 (progn
9436                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9437                   (gnus-summary-show-thread) t)
9438                 (gnus-summary-find-subject subject))
9439           (setq count (1+ count))))
9440        ((> unmark 0)                    ; Tick.
9441         (while (and
9442                 (progn
9443                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9444                   (gnus-summary-show-thread) t)
9445                 (gnus-summary-find-subject subject))
9446           (setq count (1+ count))))
9447        (t                               ; Mark as unread.
9448         (while (and
9449                 (progn
9450                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9451                   (gnus-summary-show-thread) t)
9452                 (gnus-summary-find-subject subject))
9453           (setq count (1+ count)))))
9454       (gnus-set-mode-line 'summary)
9455       ;; Return the number of marked articles.
9456       count)))
9457
9458 (defun gnus-summary-mark-as-processable (n &optional unmark)
9459   "Set the process mark on the next N articles.
9460 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9461 the process mark instead.  The difference between N and the actual
9462 number of articles marked is returned."
9463   (interactive "P")
9464   (if (and (null n) (gnus-region-active-p))
9465       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9466     (setq n (prefix-numeric-value n))
9467     (let ((backward (< n 0))
9468           (n (abs n)))
9469       (while (and
9470               (> n 0)
9471               (if unmark
9472                   (gnus-summary-remove-process-mark
9473                    (gnus-summary-article-number))
9474                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9475               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9476         (setq n (1- n)))
9477       (when (/= 0 n)
9478         (gnus-message 7 "No more articles"))
9479       (gnus-summary-recenter)
9480       (gnus-summary-position-point)
9481       n)))
9482
9483 (defun gnus-summary-unmark-as-processable (n)
9484   "Remove the process mark from the next N articles.
9485 If N is negative, unmark backward instead.  The difference between N and
9486 the actual number of articles unmarked is returned."
9487   (interactive "P")
9488   (gnus-summary-mark-as-processable n t))
9489
9490 (defun gnus-summary-unmark-all-processable ()
9491   "Remove the process mark from all articles."
9492   (interactive)
9493   (save-excursion
9494     (while gnus-newsgroup-processable
9495       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9496   (gnus-summary-position-point))
9497
9498 (defun gnus-summary-add-mark (article type)
9499   "Mark ARTICLE with a mark of TYPE."
9500   (let ((vtype (car (assq type gnus-article-mark-lists)))
9501         var)
9502     (if (not vtype)
9503         (error "No such mark type: %s" type)
9504       (setq var (intern (format "gnus-newsgroup-%s" type)))
9505       (set var (cons article (symbol-value var)))
9506       (if (memq type '(processable cached replied forwarded recent saved))
9507           (gnus-summary-update-secondary-mark article)
9508         ;;; !!! This is bogus.  We should find out what primary
9509         ;;; !!! mark we want to set.
9510         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9511
9512 (defun gnus-summary-mark-as-expirable (n)
9513   "Mark N articles forward as expirable.
9514 If N is negative, mark backward instead.  The difference between N and
9515 the actual number of articles marked is returned."
9516   (interactive "p")
9517   (gnus-summary-mark-forward n gnus-expirable-mark))
9518
9519 (defun gnus-summary-mark-as-spam (n)
9520   "Mark N articles forward as spam.
9521 If N is negative, mark backward instead.  The difference between N and
9522 the actual number of articles marked is returned."
9523   (interactive "p")
9524   (gnus-summary-mark-forward n gnus-spam-mark))
9525
9526 (defun gnus-summary-mark-article-as-replied (article)
9527   "Mark ARTICLE as replied to and update the summary line.
9528 ARTICLE can also be a list of articles."
9529   (interactive (list (gnus-summary-article-number)))
9530   (let ((articles (if (listp article) article (list article))))
9531     (dolist (article articles)
9532       (push article gnus-newsgroup-replied)
9533       (let ((buffer-read-only nil))
9534         (when (gnus-summary-goto-subject article nil t)
9535           (gnus-summary-update-secondary-mark article))))))
9536
9537 (defun gnus-summary-mark-article-as-forwarded (article)
9538   "Mark ARTICLE as forwarded and update the summary line.
9539 ARTICLE can also be a list of articles."
9540   (let ((articles (if (listp article) article (list article))))
9541     (dolist (article articles)
9542       (push article gnus-newsgroup-forwarded)
9543       (let ((buffer-read-only nil))
9544         (when (gnus-summary-goto-subject article nil t)
9545           (gnus-summary-update-secondary-mark article))))))
9546
9547 (defun gnus-summary-set-bookmark (article)
9548   "Set a bookmark in current article."
9549   (interactive (list (gnus-summary-article-number)))
9550   (when (or (not (get-buffer gnus-article-buffer))
9551             (not gnus-current-article)
9552             (not gnus-article-current)
9553             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9554     (error "No current article selected"))
9555   ;; Remove old bookmark, if one exists.
9556   (gnus-pull article gnus-newsgroup-bookmarks)
9557   ;; Set the new bookmark, which is on the form
9558   ;; (article-number . line-number-in-body).
9559   (push
9560    (cons article
9561          (save-excursion
9562            (set-buffer gnus-article-buffer)
9563            (count-lines
9564             (min (point)
9565                  (save-excursion
9566                    (article-goto-body)
9567                    (point)))
9568             (point))))
9569    gnus-newsgroup-bookmarks)
9570   (gnus-message 6 "A bookmark has been added to the current article."))
9571
9572 (defun gnus-summary-remove-bookmark (article)
9573   "Remove the bookmark from the current article."
9574   (interactive (list (gnus-summary-article-number)))
9575   ;; Remove old bookmark, if one exists.
9576   (if (not (assq article gnus-newsgroup-bookmarks))
9577       (gnus-message 6 "No bookmark in current article.")
9578     (gnus-pull article gnus-newsgroup-bookmarks)
9579     (gnus-message 6 "Removed bookmark.")))
9580
9581 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9582 (defun gnus-summary-mark-as-dormant (n)
9583   "Mark N articles forward as dormant.
9584 If N is negative, mark backward instead.  The difference between N and
9585 the actual number of articles marked is returned."
9586   (interactive "p")
9587   (gnus-summary-mark-forward n gnus-dormant-mark))
9588
9589 (defun gnus-summary-set-process-mark (article)
9590   "Set the process mark on ARTICLE and update the summary line."
9591   (setq gnus-newsgroup-processable
9592         (cons article
9593               (delq article gnus-newsgroup-processable)))
9594   (when (gnus-summary-goto-subject article)
9595     (gnus-summary-show-thread)
9596     (gnus-summary-goto-subject article)
9597     (gnus-summary-update-secondary-mark article)))
9598
9599 (defun gnus-summary-remove-process-mark (article)
9600   "Remove the process mark from ARTICLE and update the summary line."
9601   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9602   (when (gnus-summary-goto-subject article)
9603     (gnus-summary-show-thread)
9604     (gnus-summary-goto-subject article)
9605     (gnus-summary-update-secondary-mark article)))
9606
9607 (defun gnus-summary-set-saved-mark (article)
9608   "Set the process mark on ARTICLE and update the summary line."
9609   (push article gnus-newsgroup-saved)
9610   (when (gnus-summary-goto-subject article)
9611     (gnus-summary-update-secondary-mark article)))
9612
9613 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9614   "Mark N articles as read forwards.
9615 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9616 The difference between N and the actual number of articles marked is
9617 returned.
9618 If NO-EXPIRE, auto-expiry will be inhibited."
9619   (interactive "p")
9620   (gnus-summary-show-thread)
9621   (let ((backward (< n 0))
9622         (gnus-summary-goto-unread
9623          (and gnus-summary-goto-unread
9624               (not (eq gnus-summary-goto-unread 'never))
9625               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9626                                     gnus-ticked-mark gnus-dormant-mark)))))
9627         (n (abs n))
9628         (mark (or mark gnus-del-mark)))
9629     (while (and (> n 0)
9630                 (gnus-summary-mark-article nil mark no-expire)
9631                 (zerop (gnus-summary-next-subject
9632                         (if backward -1 1)
9633                         (and gnus-summary-goto-unread
9634                              (not (eq gnus-summary-goto-unread 'never)))
9635                         t)))
9636       (setq n (1- n)))
9637     (when (/= 0 n)
9638       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9639     (gnus-summary-recenter)
9640     (gnus-summary-position-point)
9641     (gnus-set-mode-line 'summary)
9642     n))
9643
9644 (defun gnus-summary-mark-article-as-read (mark)
9645   "Mark the current article quickly as read with MARK."
9646   (let ((article (gnus-summary-article-number)))
9647     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9648     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9649     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9650     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9651     (push (cons article mark) gnus-newsgroup-reads)
9652     ;; Possibly remove from cache, if that is used.
9653     (when gnus-use-cache
9654       (gnus-cache-enter-remove-article article))
9655     ;; Allow the backend to change the mark.
9656     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9657     ;; Check for auto-expiry.
9658     (when (and gnus-newsgroup-auto-expire
9659                (memq mark gnus-auto-expirable-marks))
9660       (setq mark gnus-expirable-mark)
9661       ;; Let the backend know about the mark change.
9662       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9663       (push article gnus-newsgroup-expirable))
9664     ;; Set the mark in the buffer.
9665     (gnus-summary-update-mark mark 'unread)
9666     t))
9667
9668 (defun gnus-summary-mark-article-as-unread (mark)
9669   "Mark the current article quickly as unread with MARK."
9670   (let* ((article (gnus-summary-article-number))
9671          (old-mark (gnus-summary-article-mark article)))
9672     ;; Allow the backend to change the mark.
9673     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9674     (if (eq mark old-mark)
9675         t
9676       (if (<= article 0)
9677           (progn
9678             (gnus-error 1 "Can't mark negative article numbers")
9679             nil)
9680         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9681         (setq gnus-newsgroup-spam-marked
9682               (delq article gnus-newsgroup-spam-marked))
9683         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9684         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9685         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9686         (cond ((= mark gnus-ticked-mark)
9687                (setq gnus-newsgroup-marked
9688                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9689                                               article)))
9690               ((= mark gnus-spam-mark)
9691                (setq gnus-newsgroup-spam-marked
9692                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9693                                               article)))
9694               ((= mark gnus-dormant-mark)
9695                (setq gnus-newsgroup-dormant
9696                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9697                                               article)))
9698               (t
9699                (setq gnus-newsgroup-unreads
9700                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9701                                               article))))
9702         (gnus-pull article gnus-newsgroup-reads)
9703
9704         ;; See whether the article is to be put in the cache.
9705         (and gnus-use-cache
9706              (vectorp (gnus-summary-article-header article))
9707              (save-excursion
9708                (gnus-cache-possibly-enter-article
9709                 gnus-newsgroup-name article
9710                 (gnus-summary-article-header article)
9711                 (= mark gnus-ticked-mark)
9712                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9713
9714         ;; Fix the mark.
9715         (gnus-summary-update-mark mark 'unread)
9716         t))))
9717
9718 (defun gnus-summary-mark-article (&optional article mark no-expire)
9719   "Mark ARTICLE with MARK.  MARK can be any character.
9720 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9721 `??' (dormant) and `?E' (expirable).
9722 If MARK is nil, then the default character `?r' is used.
9723 If ARTICLE is nil, then the article on the current line will be
9724 marked.
9725 If NO-EXPIRE, auto-expiry will be inhibited."
9726   ;; The mark might be a string.
9727   (when (stringp mark)
9728     (setq mark (aref mark 0)))
9729   ;; If no mark is given, then we check auto-expiring.
9730   (when (null mark)
9731     (setq mark gnus-del-mark))
9732   (when (and (not no-expire)
9733              gnus-newsgroup-auto-expire
9734              (memq mark gnus-auto-expirable-marks))
9735     (setq mark gnus-expirable-mark))
9736   (let ((article (or article (gnus-summary-article-number)))
9737         (old-mark (gnus-summary-article-mark article)))
9738     ;; Allow the backend to change the mark.
9739     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9740     (if (eq mark old-mark)
9741         t
9742       (unless article
9743         (error "No article on current line"))
9744       (if (not (if (or (= mark gnus-unread-mark)
9745                        (= mark gnus-ticked-mark)
9746                        (= mark gnus-spam-mark)
9747                        (= mark gnus-dormant-mark))
9748                    (gnus-mark-article-as-unread article mark)
9749                  (gnus-mark-article-as-read article mark)))
9750           t
9751         ;; See whether the article is to be put in the cache.
9752         (and gnus-use-cache
9753              (not (= mark gnus-canceled-mark))
9754              (vectorp (gnus-summary-article-header article))
9755              (save-excursion
9756                (gnus-cache-possibly-enter-article
9757                 gnus-newsgroup-name article
9758                 (gnus-summary-article-header article)
9759                 (= mark gnus-ticked-mark)
9760                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9761
9762         (when (gnus-summary-goto-subject article nil t)
9763           (let ((buffer-read-only nil))
9764             (gnus-summary-show-thread)
9765             ;; Fix the mark.
9766             (gnus-summary-update-mark mark 'unread)
9767             t))))))
9768
9769 (defun gnus-summary-update-secondary-mark (article)
9770   "Update the secondary (read, process, cache) mark."
9771   (gnus-summary-update-mark
9772    (cond ((memq article gnus-newsgroup-processable)
9773           gnus-process-mark)
9774          ((memq article gnus-newsgroup-cached)
9775           gnus-cached-mark)
9776          ((memq article gnus-newsgroup-replied)
9777           gnus-replied-mark)
9778          ((memq article gnus-newsgroup-forwarded)
9779           gnus-forwarded-mark)
9780          ((memq article gnus-newsgroup-saved)
9781           gnus-saved-mark)
9782          ((memq article gnus-newsgroup-recent)
9783           gnus-recent-mark)
9784          ((memq article gnus-newsgroup-unseen)
9785           gnus-unseen-mark)
9786          (t gnus-no-mark))
9787    'replied)
9788   (when (gnus-visual-p 'summary-highlight 'highlight)
9789     (gnus-run-hooks 'gnus-summary-update-hook))
9790   t)
9791
9792 (defun gnus-summary-update-download-mark (article)
9793   "Update the secondary (read, process, cache) mark."
9794   (gnus-summary-update-mark
9795    (cond ((memq article gnus-newsgroup-undownloaded) 
9796           gnus-undownloaded-mark)
9797          (gnus-newsgroup-agentized
9798           gnus-downloaded-mark)
9799          (t
9800           gnus-no-mark))
9801    'download)
9802   (gnus-summary-update-line t)
9803   t)
9804
9805 (defun gnus-summary-update-mark (mark type)
9806   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9807         (buffer-read-only nil))
9808     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9809     (when forward
9810       (when (looking-at "\r")
9811         (incf forward))
9812       (when (<= (+ forward (point)) (point-max))
9813         ;; Go to the right position on the line.
9814         (goto-char (+ forward (point)))
9815         ;; Replace the old mark with the new mark.
9816         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9817         ;; Optionally update the marks by some user rule.
9818         (when (eq type 'unread)
9819           (gnus-data-set-mark
9820            (gnus-data-find (gnus-summary-article-number)) mark)
9821           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9822
9823 (defun gnus-mark-article-as-read (article &optional mark)
9824   "Enter ARTICLE in the pertinent lists and remove it from others."
9825   ;; Make the article expirable.
9826   (let ((mark (or mark gnus-del-mark)))
9827     (setq gnus-newsgroup-expirable
9828           (if (= mark gnus-expirable-mark)
9829               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9830             (delq article gnus-newsgroup-expirable)))
9831     ;; Remove from unread and marked lists.
9832     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9833     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9834     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9835     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9836     (push (cons article mark) gnus-newsgroup-reads)
9837     ;; Possibly remove from cache, if that is used.
9838     (when gnus-use-cache
9839       (gnus-cache-enter-remove-article article))
9840     t))
9841
9842 (defun gnus-mark-article-as-unread (article &optional mark)
9843   "Enter ARTICLE in the pertinent lists and remove it from others."
9844   (let ((mark (or mark gnus-ticked-mark)))
9845     (if (<= article 0)
9846         (progn
9847           (gnus-error 1 "Can't mark negative article numbers")
9848           nil)
9849       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9850             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9851             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9852             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9853             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9854
9855       ;; Unsuppress duplicates?
9856       (when gnus-suppress-duplicates
9857         (gnus-dup-unsuppress-article article))
9858
9859       (cond ((= mark gnus-ticked-mark)
9860              (setq gnus-newsgroup-marked
9861                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9862             ((= mark gnus-spam-mark)
9863              (setq gnus-newsgroup-spam-marked
9864                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9865                                             article)))
9866             ((= mark gnus-dormant-mark)
9867              (setq gnus-newsgroup-dormant
9868                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9869             (t
9870              (setq gnus-newsgroup-unreads
9871                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9872       (gnus-pull article gnus-newsgroup-reads)
9873       t)))
9874
9875 (defalias 'gnus-summary-mark-as-unread-forward
9876   'gnus-summary-tick-article-forward)
9877 (make-obsolete 'gnus-summary-mark-as-unread-forward
9878                'gnus-summary-tick-article-forward)
9879 (defun gnus-summary-tick-article-forward (n)
9880   "Tick N articles forwards.
9881 If N is negative, tick backwards instead.
9882 The difference between N and the number of articles ticked is returned."
9883   (interactive "p")
9884   (gnus-summary-mark-forward n gnus-ticked-mark))
9885
9886 (defalias 'gnus-summary-mark-as-unread-backward
9887   'gnus-summary-tick-article-backward)
9888 (make-obsolete 'gnus-summary-mark-as-unread-backward
9889                'gnus-summary-tick-article-backward)
9890 (defun gnus-summary-tick-article-backward (n)
9891   "Tick N articles backwards.
9892 The difference between N and the number of articles ticked is returned."
9893   (interactive "p")
9894   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9895
9896 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9897 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9898 (defun gnus-summary-tick-article (&optional article clear-mark)
9899   "Mark current article as unread.
9900 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9901 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9902   (interactive)
9903   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9904                                        gnus-ticked-mark)))
9905
9906 (defun gnus-summary-mark-as-read-forward (n)
9907   "Mark N articles as read forwards.
9908 If N is negative, mark backwards instead.
9909 The difference between N and the actual number of articles marked is
9910 returned."
9911   (interactive "p")
9912   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9913
9914 (defun gnus-summary-mark-as-read-backward (n)
9915   "Mark the N articles as read backwards.
9916 The difference between N and the actual number of articles marked is
9917 returned."
9918   (interactive "p")
9919   (gnus-summary-mark-forward
9920    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9921
9922 (defun gnus-summary-mark-as-read (&optional article mark)
9923   "Mark current article as read.
9924 ARTICLE specifies the article to be marked as read.
9925 MARK specifies a string to be inserted at the beginning of the line."
9926   (gnus-summary-mark-article article mark))
9927
9928 (defun gnus-summary-clear-mark-forward (n)
9929   "Clear marks from N articles forward.
9930 If N is negative, clear backward instead.
9931 The difference between N and the number of marks cleared is returned."
9932   (interactive "p")
9933   (gnus-summary-mark-forward n gnus-unread-mark))
9934
9935 (defun gnus-summary-clear-mark-backward (n)
9936   "Clear marks from N articles backward.
9937 The difference between N and the number of marks cleared is returned."
9938   (interactive "p")
9939   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9940
9941 (defun gnus-summary-mark-unread-as-read ()
9942   "Intended to be used by `gnus-summary-mark-article-hook'."
9943   (when (memq gnus-current-article gnus-newsgroup-unreads)
9944     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9945
9946 (defun gnus-summary-mark-read-and-unread-as-read ()
9947   "Intended to be used by `gnus-summary-mark-article-hook'."
9948   (let ((mark (gnus-summary-article-mark)))
9949     (when (or (gnus-unread-mark-p mark)
9950               (gnus-read-mark-p mark))
9951       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9952
9953 (defun gnus-summary-mark-unread-as-ticked ()
9954   "Intended to be used by `gnus-summary-mark-article-hook'."
9955   (when (memq gnus-current-article gnus-newsgroup-unreads)
9956     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9957
9958 (defun gnus-summary-mark-region-as-read (point mark all)
9959   "Mark all unread articles between point and mark as read.
9960 If given a prefix, mark all articles between point and mark as read,
9961 even ticked and dormant ones."
9962   (interactive "r\nP")
9963   (save-excursion
9964     (let (article)
9965       (goto-char point)
9966       (beginning-of-line)
9967       (while (and
9968               (< (point) mark)
9969               (progn
9970                 (when (or all
9971                           (memq (setq article (gnus-summary-article-number))
9972                                 gnus-newsgroup-unreads))
9973                   (gnus-summary-mark-article article gnus-del-mark))
9974                 t)
9975               (gnus-summary-find-next))))))
9976
9977 (defun gnus-summary-mark-below (score mark)
9978   "Mark articles with score less than SCORE with MARK."
9979   (interactive "P\ncMark: ")
9980   (setq score (if score
9981                   (prefix-numeric-value score)
9982                 (or gnus-summary-default-score 0)))
9983   (save-excursion
9984     (set-buffer gnus-summary-buffer)
9985     (goto-char (point-min))
9986     (while
9987         (progn
9988           (and (< (gnus-summary-article-score) score)
9989                (gnus-summary-mark-article nil mark))
9990           (gnus-summary-find-next)))))
9991
9992 (defun gnus-summary-kill-below (&optional score)
9993   "Mark articles with score below SCORE as read."
9994   (interactive "P")
9995   (gnus-summary-mark-below score gnus-killed-mark))
9996
9997 (defun gnus-summary-clear-above (&optional score)
9998   "Clear all marks from articles with score above SCORE."
9999   (interactive "P")
10000   (gnus-summary-mark-above score gnus-unread-mark))
10001
10002 (defun gnus-summary-tick-above (&optional score)
10003   "Tick all articles with score above SCORE."
10004   (interactive "P")
10005   (gnus-summary-mark-above score gnus-ticked-mark))
10006
10007 (defun gnus-summary-mark-above (score mark)
10008   "Mark articles with score over SCORE with MARK."
10009   (interactive "P\ncMark: ")
10010   (setq score (if score
10011                   (prefix-numeric-value score)
10012                 (or gnus-summary-default-score 0)))
10013   (save-excursion
10014     (set-buffer gnus-summary-buffer)
10015     (goto-char (point-min))
10016     (while (and (progn
10017                   (when (> (gnus-summary-article-score) score)
10018                     (gnus-summary-mark-article nil mark))
10019                   t)
10020                 (gnus-summary-find-next)))))
10021
10022 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10023 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10024 (defun gnus-summary-limit-include-expunged (&optional no-error)
10025   "Display all the hidden articles that were expunged for low scores."
10026   (interactive)
10027   (let ((buffer-read-only nil))
10028     (let ((scored gnus-newsgroup-scored)
10029           headers h)
10030       (while scored
10031         (unless (gnus-summary-article-header (caar scored))
10032           (and (setq h (gnus-number-to-header (caar scored)))
10033                (< (cdar scored) gnus-summary-expunge-below)
10034                (push h headers)))
10035         (setq scored (cdr scored)))
10036       (if (not headers)
10037           (when (not no-error)
10038             (error "No expunged articles hidden"))
10039         (goto-char (point-min))
10040         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10041         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10042         (mapcar (lambda (x) (push (mail-header-number x)
10043                                   gnus-newsgroup-limit))
10044                 headers)
10045         (gnus-summary-prepare-unthreaded (nreverse headers))
10046         (goto-char (point-min))
10047         (gnus-summary-position-point)
10048         t))))
10049
10050 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10051   "Mark all unread articles in this newsgroup as read.
10052 If prefix argument ALL is non-nil, ticked and dormant articles will
10053 also be marked as read.
10054 If QUIETLY is non-nil, no questions will be asked.
10055 If TO-HERE is non-nil, it should be a point in the buffer.  All
10056 articles before (after, if REVERSE is set) this point will be marked as read.
10057 Note that this function will only catch up the unread article
10058 in the current summary buffer limitation.
10059 The number of articles marked as read is returned."
10060   (interactive "P")
10061   (prog1
10062       (save-excursion
10063         (when (or quietly
10064                   (not gnus-interactive-catchup) ;Without confirmation?
10065                   gnus-expert-user
10066                   (gnus-y-or-n-p
10067                    (if all
10068                        "Mark absolutely all articles as read? "
10069                      "Mark all unread articles as read? ")))
10070           (if (and not-mark
10071                    (not gnus-newsgroup-adaptive)
10072                    (not gnus-newsgroup-auto-expire)
10073                    (not gnus-suppress-duplicates)
10074                    (or (not gnus-use-cache)
10075                        (eq gnus-use-cache 'passive)))
10076               (progn
10077                 (when all
10078                   (setq gnus-newsgroup-marked nil
10079                         gnus-newsgroup-spam-marked nil
10080                         gnus-newsgroup-dormant nil))
10081                 (setq gnus-newsgroup-unreads
10082                       (gnus-intersection gnus-newsgroup-unreads
10083                                          gnus-newsgroup-downloadable)))
10084             ;; We actually mark all articles as canceled, which we
10085             ;; have to do when using auto-expiry or adaptive scoring.
10086             (gnus-summary-show-all-threads)
10087             (if (and to-here reverse)
10088                 (progn
10089                   (goto-char to-here)
10090                   (while (and
10091                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10092                           (gnus-summary-find-next (not all) nil nil t))))
10093               (when (gnus-summary-first-subject (not all) t)
10094                 (while (and
10095                         (if to-here (< (point) to-here) t)
10096                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10097                         (gnus-summary-find-next (not all) nil nil t)))))
10098             (gnus-set-mode-line 'summary))
10099           t))
10100     (gnus-summary-position-point)))
10101
10102 (defun gnus-summary-catchup-to-here (&optional all)
10103   "Mark all unticked articles before the current one as read.
10104 If ALL is non-nil, also mark ticked and dormant articles as read."
10105   (interactive "P")
10106   (save-excursion
10107     (gnus-save-hidden-threads
10108       (let ((beg (point)))
10109         ;; We check that there are unread articles.
10110         (when (or all (gnus-summary-find-prev))
10111           (gnus-summary-catchup all t beg)))))
10112   (gnus-summary-position-point))
10113
10114 (defun gnus-summary-catchup-from-here (&optional all)
10115   "Mark all unticked articles after the current one as read.
10116 If ALL is non-nil, also mark ticked and dormant articles as read."
10117   (interactive "P")
10118   (save-excursion
10119     (gnus-save-hidden-threads
10120       (let ((beg (point)))
10121         ;; We check that there are unread articles.
10122         (when (or all (gnus-summary-find-next))
10123           (gnus-summary-catchup all t beg nil t)))))
10124   (gnus-summary-position-point))
10125
10126 (defun gnus-summary-catchup-all (&optional quietly)
10127   "Mark all articles in this newsgroup as read.
10128 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10129 instead, which marks only unread articles as read."
10130   (interactive "P")
10131   (gnus-summary-catchup t quietly))
10132
10133 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10134   "Mark all unread articles in this group as read, then exit.
10135 If prefix argument ALL is non-nil, all articles are marked as read.
10136 If QUIETLY is non-nil, no questions will be asked."
10137   (interactive "P")
10138   (when (gnus-summary-catchup all quietly nil 'fast)
10139     ;; Select next newsgroup or exit.
10140     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10141              (eq gnus-auto-select-next 'quietly))
10142         (gnus-summary-next-group nil)
10143       (gnus-summary-exit))))
10144
10145 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10146   "Mark all articles in this newsgroup as read, and then exit.
10147 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10148 instead, which marks only unread articles as read."
10149   (interactive "P")
10150   (gnus-summary-catchup-and-exit t quietly))
10151
10152 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10153   "Mark all articles in this group as read and select the next group.
10154 If given a prefix, mark all articles, unread as well as ticked, as
10155 read."
10156   (interactive "P")
10157   (save-excursion
10158     (gnus-summary-catchup all))
10159   (gnus-summary-next-group))
10160
10161 ;;;
10162 ;;; with article
10163 ;;;
10164
10165 (defmacro gnus-with-article (article &rest forms)
10166   "Select ARTICLE and perform FORMS in the original article buffer.
10167 Then replace the article with the result."
10168   `(progn
10169      ;; We don't want the article to be marked as read.
10170      (let (gnus-mark-article-hook)
10171        (gnus-summary-select-article t t nil ,article))
10172      (set-buffer gnus-original-article-buffer)
10173      ,@forms
10174      (if (not (gnus-check-backend-function
10175                'request-replace-article (car gnus-article-current)))
10176          (gnus-message 5 "Read-only group; not replacing")
10177        (unless (gnus-request-replace-article
10178                 ,article (car gnus-article-current)
10179                 (current-buffer) t)
10180          (error "Couldn't replace article")))
10181      ;; The cache and backlog have to be flushed somewhat.
10182      (when gnus-keep-backlog
10183        (gnus-backlog-remove-article
10184         (car gnus-article-current) (cdr gnus-article-current)))
10185      (when gnus-use-cache
10186        (gnus-cache-update-article
10187         (car gnus-article-current) (cdr gnus-article-current)))))
10188
10189 (put 'gnus-with-article 'lisp-indent-function 1)
10190 (put 'gnus-with-article 'edebug-form-spec '(form body))
10191
10192 ;; Thread-based commands.
10193
10194 (defun gnus-summary-articles-in-thread (&optional article)
10195   "Return a list of all articles in the current thread.
10196 If ARTICLE is non-nil, return all articles in the thread that starts
10197 with that article."
10198   (let* ((article (or article (gnus-summary-article-number)))
10199          (data (gnus-data-find-list article))
10200          (top-level (gnus-data-level (car data)))
10201          (top-subject
10202           (cond ((null gnus-thread-operation-ignore-subject)
10203                  (gnus-simplify-subject-re
10204                   (mail-header-subject (gnus-data-header (car data)))))
10205                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10206                  (gnus-simplify-subject-fuzzy
10207                   (mail-header-subject (gnus-data-header (car data)))))
10208                 (t nil)))
10209          (end-point (save-excursion
10210                       (if (gnus-summary-go-to-next-thread)
10211                           (point) (point-max))))
10212          articles)
10213     (while (and data
10214                 (< (gnus-data-pos (car data)) end-point))
10215       (when (or (not top-subject)
10216                 (string= top-subject
10217                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10218                              (gnus-simplify-subject-fuzzy
10219                               (mail-header-subject
10220                                (gnus-data-header (car data))))
10221                            (gnus-simplify-subject-re
10222                             (mail-header-subject
10223                              (gnus-data-header (car data)))))))
10224         (push (gnus-data-number (car data)) articles))
10225       (unless (and (setq data (cdr data))
10226                    (> (gnus-data-level (car data)) top-level))
10227         (setq data nil)))
10228     ;; Return the list of articles.
10229     (nreverse articles)))
10230
10231 (defun gnus-summary-rethread-current ()
10232   "Rethread the thread the current article is part of."
10233   (interactive)
10234   (let* ((gnus-show-threads t)
10235          (article (gnus-summary-article-number))
10236          (id (mail-header-id (gnus-summary-article-header)))
10237          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10238     (unless id
10239       (error "No article on the current line"))
10240     (gnus-rebuild-thread id)
10241     (gnus-summary-goto-subject article)))
10242
10243 (defun gnus-summary-reparent-thread ()
10244   "Make the current article child of the marked (or previous) article.
10245
10246 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10247 is non-nil or the Subject: of both articles are the same."
10248   (interactive)
10249   (unless (not (gnus-group-read-only-p))
10250     (error "The current newsgroup does not support article editing"))
10251   (unless (<= (length gnus-newsgroup-processable) 1)
10252     (error "No more than one article may be marked"))
10253   (save-window-excursion
10254     (let ((gnus-article-buffer " *reparent*")
10255           (current-article (gnus-summary-article-number))
10256           ;; First grab the marked article, otherwise one line up.
10257           (parent-article (if (not (null gnus-newsgroup-processable))
10258                               (car gnus-newsgroup-processable)
10259                             (save-excursion
10260                               (if (eq (forward-line -1) 0)
10261                                   (gnus-summary-article-number)
10262                                 (error "Beginning of summary buffer"))))))
10263       (unless (not (eq current-article parent-article))
10264         (error "An article may not be self-referential"))
10265       (let ((message-id (mail-header-id
10266                          (gnus-summary-article-header parent-article))))
10267         (unless (and message-id (not (equal message-id "")))
10268           (error "No message-id in desired parent"))
10269         (gnus-with-article current-article
10270           (save-restriction
10271             (goto-char (point-min))
10272             (message-narrow-to-head)
10273             (if (re-search-forward "^References: " nil t)
10274                 (progn
10275                   (re-search-forward "^[^ \t]" nil t)
10276                   (forward-line -1)
10277                   (end-of-line)
10278                   (insert " " message-id))
10279               (insert "References: " message-id "\n"))))
10280         (set-buffer gnus-summary-buffer)
10281         (gnus-summary-unmark-all-processable)
10282         (gnus-summary-update-article current-article)
10283         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10284             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10285         (gnus-summary-rethread-current)
10286         (gnus-message 3 "Article %d is now the child of article %d"
10287                       current-article parent-article)))))
10288
10289 (defun gnus-summary-toggle-threads (&optional arg)
10290   "Toggle showing conversation threads.
10291 If ARG is positive number, turn showing conversation threads on."
10292   (interactive "P")
10293   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10294     (setq gnus-show-threads
10295           (if (null arg) (not gnus-show-threads)
10296             (> (prefix-numeric-value arg) 0)))
10297     (gnus-summary-prepare)
10298     (gnus-summary-goto-subject current)
10299     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10300     (gnus-summary-position-point)))
10301
10302 (defun gnus-summary-show-all-threads ()
10303   "Show all threads."
10304   (interactive)
10305   (save-excursion
10306     (let ((buffer-read-only nil))
10307       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10308   (gnus-summary-position-point))
10309
10310 (defun gnus-summary-show-thread ()
10311   "Show thread subtrees.
10312 Returns nil if no thread was there to be shown."
10313   (interactive)
10314   (let ((buffer-read-only nil)
10315         (orig (point))
10316         ;; first goto end then to beg, to have point at beg after let
10317         (end (progn (end-of-line) (point)))
10318         (beg (progn (beginning-of-line) (point))))
10319     (prog1
10320         ;; Any hidden lines here?
10321         (search-forward "\r" end t)
10322       (subst-char-in-region beg end ?\^M ?\n t)
10323       (goto-char orig)
10324       (gnus-summary-position-point))))
10325
10326 (defun gnus-summary-maybe-hide-threads ()
10327   "If requested, hide the threads that should be hidden."
10328   (when (and gnus-show-threads
10329              gnus-thread-hide-subtree)
10330     (gnus-summary-hide-all-threads
10331      (if (or (consp gnus-thread-hide-subtree)
10332              (gnus-functionp gnus-thread-hide-subtree))
10333          (gnus-make-predicate gnus-thread-hide-subtree)
10334        nil))))
10335
10336 ;;; Hiding predicates.
10337
10338 (defun gnus-article-unread-p (header)
10339   (memq (mail-header-number header) gnus-newsgroup-unreads))
10340
10341 (defun gnus-article-unseen-p (header)
10342   (memq (mail-header-number header) gnus-newsgroup-unseen))
10343
10344 (defun gnus-map-articles (predicate articles)
10345   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10346   (apply 'gnus-or (mapcar predicate
10347                           (mapcar 'gnus-summary-article-header articles))))
10348
10349 (defun gnus-summary-hide-all-threads (&optional predicate)
10350   "Hide all thread subtrees.
10351 If PREDICATE is supplied, threads that satisfy this predicate
10352 will not be hidden."
10353   (interactive)
10354   (save-excursion
10355     (goto-char (point-min))
10356     (let ((end nil))
10357       (while (not end)
10358         (when (or (not predicate)
10359                   (gnus-map-articles
10360                    predicate (gnus-summary-article-children)))
10361             (gnus-summary-hide-thread))
10362         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10363   (gnus-summary-position-point))
10364
10365 (defun gnus-summary-hide-thread ()
10366   "Hide thread subtrees.
10367 If PREDICATE is supplied, threads that satisfy this predicate
10368 will not be hidden.
10369 Returns nil if no threads were there to be hidden."
10370   (interactive)
10371   (let ((buffer-read-only nil)
10372         (start (point))
10373         (article (gnus-summary-article-number)))
10374     (goto-char start)
10375     ;; Go forward until either the buffer ends or the subthread
10376     ;; ends.
10377     (when (and (not (eobp))
10378                (or (zerop (gnus-summary-next-thread 1 t))
10379                    (goto-char (point-max))))
10380       (prog1
10381           (if (and (> (point) start)
10382                    (search-backward "\n" start t))
10383               (progn
10384                 (subst-char-in-region start (point) ?\n ?\^M)
10385                 (gnus-summary-goto-subject article))
10386             (goto-char start)
10387             nil)))))
10388
10389 (defun gnus-summary-go-to-next-thread (&optional previous)
10390   "Go to the same level (or less) next thread.
10391 If PREVIOUS is non-nil, go to previous thread instead.
10392 Return the article number moved to, or nil if moving was impossible."
10393   (let ((level (gnus-summary-thread-level))
10394         (way (if previous -1 1))
10395         (beg (point)))
10396     (forward-line way)
10397     (while (and (not (eobp))
10398                 (< level (gnus-summary-thread-level)))
10399       (forward-line way))
10400     (if (eobp)
10401         (progn
10402           (goto-char beg)
10403           nil)
10404       (setq beg (point))
10405       (prog1
10406           (gnus-summary-article-number)
10407         (goto-char beg)))))
10408
10409 (defun gnus-summary-next-thread (n &optional silent)
10410   "Go to the same level next N'th thread.
10411 If N is negative, search backward instead.
10412 Returns the difference between N and the number of skips actually
10413 done.
10414
10415 If SILENT, don't output messages."
10416   (interactive "p")
10417   (let ((backward (< n 0))
10418         (n (abs n)))
10419     (while (and (> n 0)
10420                 (gnus-summary-go-to-next-thread backward))
10421       (decf n))
10422     (unless silent
10423       (gnus-summary-position-point))
10424     (when (and (not silent) (/= 0 n))
10425       (gnus-message 7 "No more threads"))
10426     n))
10427
10428 (defun gnus-summary-prev-thread (n)
10429   "Go to the same level previous N'th thread.
10430 Returns the difference between N and the number of skips actually
10431 done."
10432   (interactive "p")
10433   (gnus-summary-next-thread (- n)))
10434
10435 (defun gnus-summary-go-down-thread ()
10436   "Go down one level in the current thread."
10437   (let ((children (gnus-summary-article-children)))
10438     (when children
10439       (gnus-summary-goto-subject (car children)))))
10440
10441 (defun gnus-summary-go-up-thread ()
10442   "Go up one level in the current thread."
10443   (let ((parent (gnus-summary-article-parent)))
10444     (when parent
10445       (gnus-summary-goto-subject parent))))
10446
10447 (defun gnus-summary-down-thread (n)
10448   "Go down thread N steps.
10449 If N is negative, go up instead.
10450 Returns the difference between N and how many steps down that were
10451 taken."
10452   (interactive "p")
10453   (let ((up (< n 0))
10454         (n (abs n)))
10455     (while (and (> n 0)
10456                 (if up (gnus-summary-go-up-thread)
10457                   (gnus-summary-go-down-thread)))
10458       (setq n (1- n)))
10459     (gnus-summary-position-point)
10460     (when (/= 0 n)
10461       (gnus-message 7 "Can't go further"))
10462     n))
10463
10464 (defun gnus-summary-up-thread (n)
10465   "Go up thread N steps.
10466 If N is negative, go down instead.
10467 Returns the difference between N and how many steps down that were
10468 taken."
10469   (interactive "p")
10470   (gnus-summary-down-thread (- n)))
10471
10472 (defun gnus-summary-top-thread ()
10473   "Go to the top of the thread."
10474   (interactive)
10475   (while (gnus-summary-go-up-thread))
10476   (gnus-summary-article-number))
10477
10478 (defun gnus-summary-kill-thread (&optional unmark)
10479   "Mark articles under current thread as read.
10480 If the prefix argument is positive, remove any kinds of marks.
10481 If the prefix argument is negative, tick articles instead."
10482   (interactive "P")
10483   (when unmark
10484     (setq unmark (prefix-numeric-value unmark)))
10485   (let ((articles (gnus-summary-articles-in-thread)))
10486     (save-excursion
10487       ;; Expand the thread.
10488       (gnus-summary-show-thread)
10489       ;; Mark all the articles.
10490       (while articles
10491         (gnus-summary-goto-subject (car articles))
10492         (cond ((null unmark)
10493                (gnus-summary-mark-article-as-read gnus-killed-mark))
10494               ((> unmark 0)
10495                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10496               (t
10497                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10498         (setq articles (cdr articles))))
10499     ;; Hide killed subtrees.
10500     (and (null unmark)
10501          gnus-thread-hide-killed
10502          (gnus-summary-hide-thread))
10503     ;; If marked as read, go to next unread subject.
10504     (when (null unmark)
10505       ;; Go to next unread subject.
10506       (gnus-summary-next-subject 1 t)))
10507   (gnus-set-mode-line 'summary))
10508
10509 ;; Summary sorting commands
10510
10511 (defun gnus-summary-sort-by-number (&optional reverse)
10512   "Sort the summary buffer by article number.
10513 Argument REVERSE means reverse order."
10514   (interactive "P")
10515   (gnus-summary-sort 'number reverse))
10516
10517 (defun gnus-summary-sort-by-random (&optional reverse)
10518   "Randomize the order in the summary buffer.
10519 Argument REVERSE means to randomize in reverse order."
10520   (interactive "P")
10521   (gnus-summary-sort 'random reverse))
10522
10523 (defun gnus-summary-sort-by-author (&optional reverse)
10524   "Sort the summary buffer by author name alphabetically.
10525 If `case-fold-search' is non-nil, case of letters is ignored.
10526 Argument REVERSE means reverse order."
10527   (interactive "P")
10528   (gnus-summary-sort 'author reverse))
10529
10530 (defun gnus-summary-sort-by-subject (&optional reverse)
10531   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10532 If `case-fold-search' is non-nil, case of letters is ignored.
10533 Argument REVERSE means reverse order."
10534   (interactive "P")
10535   (gnus-summary-sort 'subject reverse))
10536
10537 (defun gnus-summary-sort-by-date (&optional reverse)
10538   "Sort the summary buffer by date.
10539 Argument REVERSE means reverse order."
10540   (interactive "P")
10541   (gnus-summary-sort 'date reverse))
10542
10543 (defun gnus-summary-sort-by-score (&optional reverse)
10544   "Sort the summary buffer by score.
10545 Argument REVERSE means reverse order."
10546   (interactive "P")
10547   (gnus-summary-sort 'score reverse))
10548
10549 (defun gnus-summary-sort-by-lines (&optional reverse)
10550   "Sort the summary buffer by the number of lines.
10551 Argument REVERSE means reverse order."
10552   (interactive "P")
10553   (gnus-summary-sort 'lines reverse))
10554
10555 (defun gnus-summary-sort-by-chars (&optional reverse)
10556   "Sort the summary buffer by article length.
10557 Argument REVERSE means reverse order."
10558   (interactive "P")
10559   (gnus-summary-sort 'chars reverse))
10560
10561 (defun gnus-summary-sort-by-original (&optional reverse)
10562   "Sort the summary buffer using the default sorting method.
10563 Argument REVERSE means reverse order."
10564   (interactive "P")
10565   (let* ((buffer-read-only)
10566          (gnus-summary-prepare-hook nil))
10567     ;; We do the sorting by regenerating the threads.
10568     (gnus-summary-prepare)
10569     ;; Hide subthreads if needed.
10570     (gnus-summary-maybe-hide-threads)))
10571
10572 (defun gnus-summary-sort (predicate reverse)
10573   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10574   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10575          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10576          (gnus-thread-sort-functions
10577           (if (not reverse)
10578               thread
10579             `(lambda (t1 t2)
10580                (,thread t2 t1))))
10581          (gnus-sort-gathered-threads-function
10582           gnus-thread-sort-functions)
10583          (gnus-article-sort-functions
10584           (if (not reverse)
10585               article
10586             `(lambda (t1 t2)
10587                (,article t2 t1))))
10588          (buffer-read-only)
10589          (gnus-summary-prepare-hook nil))
10590     ;; We do the sorting by regenerating the threads.
10591     (gnus-summary-prepare)
10592     ;; Hide subthreads if needed.
10593     (gnus-summary-maybe-hide-threads)))
10594
10595 ;; Summary saving commands.
10596
10597 (defun gnus-summary-save-article (&optional n not-saved)
10598   "Save the current article using the default saver function.
10599 If N is a positive number, save the N next articles.
10600 If N is a negative number, save the N previous articles.
10601 If N is nil and any articles have been marked with the process mark,
10602 save those articles instead.
10603 The variable `gnus-default-article-saver' specifies the saver function."
10604   (interactive "P")
10605   (let* ((articles (gnus-summary-work-articles n))
10606          (save-buffer (save-excursion
10607                         (nnheader-set-temp-buffer " *Gnus Save*")))
10608          (num (length articles))
10609          header file)
10610     (dolist (article articles)
10611       (setq header (gnus-summary-article-header article))
10612       (if (not (vectorp header))
10613           ;; This is a pseudo-article.
10614           (if (assq 'name header)
10615               (gnus-copy-file (cdr (assq 'name header)))
10616             (gnus-message 1 "Article %d is unsaveable" article))
10617         ;; This is a real article.
10618         (save-window-excursion
10619           (let ((gnus-display-mime-function nil)
10620                 (gnus-article-prepare-hook nil))
10621             (gnus-summary-select-article t nil nil article)))
10622         (save-excursion
10623           (set-buffer save-buffer)
10624           (erase-buffer)
10625           (insert-buffer-substring gnus-original-article-buffer))
10626         (setq file (gnus-article-save save-buffer file num))
10627         (gnus-summary-remove-process-mark article)
10628         (unless not-saved
10629           (gnus-summary-set-saved-mark article))))
10630     (gnus-kill-buffer save-buffer)
10631     (gnus-summary-position-point)
10632     (gnus-set-mode-line 'summary)
10633     n))
10634
10635 (defun gnus-summary-pipe-output (&optional arg headers)
10636   "Pipe the current article to a subprocess.
10637 If N is a positive number, pipe the N next articles.
10638 If N is a negative number, pipe the N previous articles.
10639 If N is nil and any articles have been marked with the process mark,
10640 pipe those articles instead.
10641 If HEADERS (the symbolic prefix), include the headers, too."
10642   (interactive (gnus-interactive "P\ny"))
10643   (require 'gnus-art)
10644   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10645         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10646     (gnus-summary-save-article arg t))
10647   (let ((buffer (get-buffer "*Shell Command Output*")))
10648     (when (and buffer
10649                (not (zerop (buffer-size buffer))))
10650       (gnus-configure-windows 'pipe))))
10651
10652 (defun gnus-summary-save-article-mail (&optional arg)
10653   "Append the current article to an mail file.
10654 If N is a positive number, save the N next articles.
10655 If N is a negative number, save the N previous articles.
10656 If N is nil and any articles have been marked with the process mark,
10657 save those articles instead."
10658   (interactive "P")
10659   (require 'gnus-art)
10660   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10661     (gnus-summary-save-article arg)))
10662
10663 (defun gnus-summary-save-article-rmail (&optional arg)
10664   "Append the current article to an rmail file.
10665 If N is a positive number, save the N next articles.
10666 If N is a negative number, save the N previous articles.
10667 If N is nil and any articles have been marked with the process mark,
10668 save those articles instead."
10669   (interactive "P")
10670   (require 'gnus-art)
10671   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10672     (gnus-summary-save-article arg)))
10673
10674 (defun gnus-summary-save-article-file (&optional arg)
10675   "Append the current article to a file.
10676 If N is a positive number, save the N next articles.
10677 If N is a negative number, save the N previous articles.
10678 If N is nil and any articles have been marked with the process mark,
10679 save those articles instead."
10680   (interactive "P")
10681   (require 'gnus-art)
10682   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10683     (gnus-summary-save-article arg)))
10684
10685 (defun gnus-summary-write-article-file (&optional arg)
10686   "Write the current article to a file, deleting the previous file.
10687 If N is a positive number, save the N next articles.
10688 If N is a negative number, save the N previous articles.
10689 If N is nil and any articles have been marked with the process mark,
10690 save those articles instead."
10691   (interactive "P")
10692   (require 'gnus-art)
10693   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10694     (gnus-summary-save-article arg)))
10695
10696 (defun gnus-summary-save-article-body-file (&optional arg)
10697   "Append the current article body to a file.
10698 If N is a positive number, save the N next articles.
10699 If N is a negative number, save the N previous articles.
10700 If N is nil and any articles have been marked with the process mark,
10701 save those articles instead."
10702   (interactive "P")
10703   (require 'gnus-art)
10704   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10705     (gnus-summary-save-article arg)))
10706
10707 (defun gnus-summary-muttprint (&optional arg)
10708   "Print the current article using Muttprint.
10709 If N is a positive number, save the N next articles.
10710 If N is a negative number, save the N previous articles.
10711 If N is nil and any articles have been marked with the process mark,
10712 save those articles instead."
10713   (interactive "P")
10714   (require 'gnus-art)
10715   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10716     (gnus-summary-save-article arg t)))
10717
10718 (defun gnus-summary-pipe-message (program)
10719   "Pipe the current article through PROGRAM."
10720   (interactive "sProgram: ")
10721   (gnus-summary-select-article)
10722   (let ((mail-header-separator ""))
10723     (gnus-eval-in-buffer-window gnus-article-buffer
10724       (save-restriction
10725         (widen)
10726         (let ((start (window-start))
10727               buffer-read-only)
10728           (message-pipe-buffer-body program)
10729           (set-window-start (get-buffer-window (current-buffer)) start))))))
10730
10731 (defun gnus-get-split-value (methods)
10732   "Return a value based on the split METHODS."
10733   (let (split-name method result match)
10734     (when methods
10735       (save-excursion
10736         (set-buffer gnus-original-article-buffer)
10737         (save-restriction
10738           (nnheader-narrow-to-headers)
10739           (while (and methods (not split-name))
10740             (goto-char (point-min))
10741             (setq method (pop methods))
10742             (setq match (car method))
10743             (when (cond
10744                    ((stringp match)
10745                     ;; Regular expression.
10746                     (ignore-errors
10747                       (re-search-forward match nil t)))
10748                    ((gnus-functionp match)
10749                     ;; Function.
10750                     (save-restriction
10751                       (widen)
10752                       (setq result (funcall match gnus-newsgroup-name))))
10753                    ((consp match)
10754                     ;; Form.
10755                     (save-restriction
10756                       (widen)
10757                       (setq result (eval match)))))
10758               (setq split-name (cdr method))
10759               (cond ((stringp result)
10760                      (push (expand-file-name
10761                             result gnus-article-save-directory)
10762                            split-name))
10763                     ((consp result)
10764                      (setq split-name (append result split-name)))))))))
10765     (nreverse split-name)))
10766
10767 (defun gnus-valid-move-group-p (group)
10768   (and (boundp group)
10769        (symbol-name group)
10770        (symbol-value group)
10771        (gnus-get-function (gnus-find-method-for-group
10772                            (symbol-name group)) 'request-accept-article t)))
10773
10774 (defun gnus-read-move-group-name (prompt default articles prefix)
10775   "Read a group name."
10776   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10777          (minibuffer-confirm-incomplete nil) ; XEmacs
10778          (prom
10779           (format "%s %s to:"
10780                   prompt
10781                   (if (> (length articles) 1)
10782                       (format "these %d articles" (length articles))
10783                     "this article")))
10784          (to-newsgroup
10785           (cond
10786            ((null split-name)
10787             (gnus-completing-read-with-default
10788              default prom
10789              gnus-active-hashtb
10790              'gnus-valid-move-group-p
10791              nil prefix
10792              'gnus-group-history))
10793            ((= 1 (length split-name))
10794             (gnus-completing-read-with-default
10795              (car split-name) prom
10796              gnus-active-hashtb
10797              'gnus-valid-move-group-p
10798              nil nil
10799              'gnus-group-history))
10800            (t
10801             (gnus-completing-read-with-default
10802              nil prom
10803              (mapcar (lambda (el) (list el))
10804                      (nreverse split-name))
10805              nil nil nil
10806              'gnus-group-history))))
10807          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10808     (when to-newsgroup
10809       (if (or (string= to-newsgroup "")
10810               (string= to-newsgroup prefix))
10811           (setq to-newsgroup default))
10812       (unless to-newsgroup
10813         (error "No group name entered"))
10814       (or (gnus-active to-newsgroup)
10815           (gnus-activate-group to-newsgroup nil nil to-method)
10816           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10817                                      to-newsgroup))
10818               (or (and (gnus-request-create-group to-newsgroup to-method)
10819                        (gnus-activate-group
10820                         to-newsgroup nil nil to-method)
10821                        (gnus-subscribe-group to-newsgroup))
10822                   (error "Couldn't create group %s" to-newsgroup)))
10823           (error "No such group: %s" to-newsgroup)))
10824     to-newsgroup))
10825
10826 (defun gnus-summary-save-parts (type dir n &optional reverse)
10827   "Save parts matching TYPE to DIR.
10828 If REVERSE, save parts that do not match TYPE."
10829   (interactive
10830    (list (read-string "Save parts of type: "
10831                       (or (car gnus-summary-save-parts-type-history)
10832                           gnus-summary-save-parts-default-mime)
10833                       'gnus-summary-save-parts-type-history)
10834          (setq gnus-summary-save-parts-last-directory
10835                (read-file-name "Save to directory: "
10836                                gnus-summary-save-parts-last-directory
10837                                nil t))
10838          current-prefix-arg))
10839   (gnus-summary-iterate n
10840     (let ((gnus-display-mime-function nil)
10841           (gnus-inhibit-treatment t))
10842       (gnus-summary-select-article))
10843     (save-excursion
10844       (set-buffer gnus-article-buffer)
10845       (let ((handles (or gnus-article-mime-handles
10846                          (mm-dissect-buffer nil gnus-article-loose-mime)
10847                          (and gnus-article-emulate-mime
10848                               (mm-uu-dissect)))))
10849         (when handles
10850           (gnus-summary-save-parts-1 type dir handles reverse)
10851           (unless gnus-article-mime-handles ;; Don't destroy this case.
10852             (mm-destroy-parts handles)))))))
10853
10854 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10855   (if (stringp (car handle))
10856       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10857               (cdr handle))
10858     (when (if reverse
10859               (not (string-match type (mm-handle-media-type handle)))
10860             (string-match type (mm-handle-media-type handle)))
10861       (let ((file (expand-file-name
10862                    (file-name-nondirectory
10863                     (or
10864                      (mail-content-type-get
10865                       (mm-handle-disposition handle) 'filename)
10866                      (concat gnus-newsgroup-name
10867                              "." (number-to-string
10868                                   (cdr gnus-article-current)))))
10869                    dir)))
10870         (unless (file-exists-p file)
10871           (mm-save-part-to-file handle file))))))
10872
10873 ;; Summary extract commands
10874
10875 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10876   (let ((buffer-read-only nil)
10877         (article (gnus-summary-article-number))
10878         after-article b e)
10879     (unless (gnus-summary-goto-subject article)
10880       (error "No such article: %d" article))
10881     (gnus-summary-position-point)
10882     ;; If all commands are to be bunched up on one line, we collect
10883     ;; them here.
10884     (unless gnus-view-pseudos-separately
10885       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10886             files action)
10887         (while ps
10888           (setq action (cdr (assq 'action (car ps))))
10889           (setq files (list (cdr (assq 'name (car ps)))))
10890           (while (and ps (cdr ps)
10891                       (string= (or action "1")
10892                                (or (cdr (assq 'action (cadr ps))) "2")))
10893             (push (cdr (assq 'name (cadr ps))) files)
10894             (setcdr ps (cddr ps)))
10895           (when files
10896             (when (not (string-match "%s" action))
10897               (push " " files))
10898             (push " " files)
10899             (when (assq 'execute (car ps))
10900               (setcdr (assq 'execute (car ps))
10901                       (funcall (if (string-match "%s" action)
10902                                    'format 'concat)
10903                                action
10904                                (mapconcat
10905                                 (lambda (f)
10906                                   (if (equal f " ")
10907                                       f
10908                                     (gnus-quote-arg-for-sh-or-csh f)))
10909                                 files " ")))))
10910           (setq ps (cdr ps)))))
10911     (if (and gnus-view-pseudos (not not-view))
10912         (while pslist
10913           (when (assq 'execute (car pslist))
10914             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10915                                   (eq gnus-view-pseudos 'not-confirm)))
10916           (setq pslist (cdr pslist)))
10917       (save-excursion
10918         (while pslist
10919           (setq after-article (or (cdr (assq 'article (car pslist)))
10920                                   (gnus-summary-article-number)))
10921           (gnus-summary-goto-subject after-article)
10922           (forward-line 1)
10923           (setq b (point))
10924           (insert "    " (file-name-nondirectory
10925                           (cdr (assq 'name (car pslist))))
10926                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10927           (setq e (point))
10928           (forward-line -1)             ; back to `b'
10929           (gnus-add-text-properties
10930            b (1- e) (list 'gnus-number gnus-reffed-article-number
10931                           gnus-mouse-face-prop gnus-mouse-face))
10932           (gnus-data-enter
10933            after-article gnus-reffed-article-number
10934            gnus-unread-mark b (car pslist) 0 (- e b))
10935           (setq gnus-newsgroup-unreads
10936                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10937                                          gnus-reffed-article-number))
10938           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10939           (setq pslist (cdr pslist)))))))
10940
10941 (defun gnus-pseudos< (p1 p2)
10942   (let ((c1 (cdr (assq 'action p1)))
10943         (c2 (cdr (assq 'action p2))))
10944     (and c1 c2 (string< c1 c2))))
10945
10946 (defun gnus-request-pseudo-article (props)
10947   (cond ((assq 'execute props)
10948          (gnus-execute-command (cdr (assq 'execute props)))))
10949   (let ((gnus-current-article (gnus-summary-article-number)))
10950     (gnus-run-hooks 'gnus-mark-article-hook)))
10951
10952 (defun gnus-execute-command (command &optional automatic)
10953   (save-excursion
10954     (gnus-article-setup-buffer)
10955     (set-buffer gnus-article-buffer)
10956     (setq buffer-read-only nil)
10957     (let ((command (if automatic command
10958                      (read-string "Command: " (cons command 0)))))
10959       (erase-buffer)
10960       (insert "$ " command "\n\n")
10961       (if gnus-view-pseudo-asynchronously
10962           (start-process "gnus-execute" (current-buffer) shell-file-name
10963                          shell-command-switch command)
10964         (call-process shell-file-name nil t nil
10965                       shell-command-switch command)))))
10966
10967 ;; Summary kill commands.
10968
10969 (defun gnus-summary-edit-global-kill (article)
10970   "Edit the \"global\" kill file."
10971   (interactive (list (gnus-summary-article-number)))
10972   (gnus-group-edit-global-kill article))
10973
10974 (defun gnus-summary-edit-local-kill ()
10975   "Edit a local kill file applied to the current newsgroup."
10976   (interactive)
10977   (setq gnus-current-headers (gnus-summary-article-header))
10978   (gnus-group-edit-local-kill
10979    (gnus-summary-article-number) gnus-newsgroup-name))
10980
10981 ;;; Header reading.
10982
10983 (defun gnus-read-header (id &optional header)
10984   "Read the headers of article ID and enter them into the Gnus system."
10985   (let ((group gnus-newsgroup-name)
10986         (gnus-override-method
10987          (or
10988           gnus-override-method
10989           (and (gnus-news-group-p gnus-newsgroup-name)
10990                (car (gnus-refer-article-methods)))))
10991         where)
10992     ;; First we check to see whether the header in question is already
10993     ;; fetched.
10994     (if (stringp id)
10995         ;; This is a Message-ID.
10996         (setq header (or header (gnus-id-to-header id)))
10997       ;; This is an article number.
10998       (setq header (or header (gnus-summary-article-header id))))
10999     (if (and header
11000              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11001         ;; We have found the header.
11002         header
11003       ;; If this is a sparse article, we have to nix out its
11004       ;; previous entry in the thread hashtb.
11005       (when (and header
11006                  (gnus-summary-article-sparse-p (mail-header-number header)))
11007         (let* ((parent (gnus-parent-id (mail-header-references header)))
11008                (thread (and parent (gnus-id-to-thread parent))))
11009           (when thread
11010             (delq (assq header thread) thread))))
11011       ;; We have to really fetch the header to this article.
11012       (save-excursion
11013         (set-buffer nntp-server-buffer)
11014         (when (setq where (gnus-request-head id group))
11015           (nnheader-fold-continuation-lines)
11016           (goto-char (point-max))
11017           (insert ".\n")
11018           (goto-char (point-min))
11019           (insert "211 ")
11020           (princ (cond
11021                   ((numberp id) id)
11022                   ((cdr where) (cdr where))
11023                   (header (mail-header-number header))
11024                   (t gnus-reffed-article-number))
11025                  (current-buffer))
11026           (insert " Article retrieved.\n"))
11027         (if (or (not where)
11028                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11029             ()                          ; Malformed head.
11030           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11031             (when (and (stringp id)
11032                        (not (string= (gnus-group-real-name group)
11033                                      (car where))))
11034               ;; If we fetched by Message-ID and the article came
11035               ;; from a different group, we fudge some bogus article
11036               ;; numbers for this article.
11037               (mail-header-set-number header gnus-reffed-article-number))
11038             (save-excursion
11039               (set-buffer gnus-summary-buffer)
11040               (decf gnus-reffed-article-number)
11041               (gnus-remove-header (mail-header-number header))
11042               (push header gnus-newsgroup-headers)
11043               (setq gnus-current-headers header)
11044               (push (mail-header-number header) gnus-newsgroup-limit)))
11045           header)))))
11046
11047 (defun gnus-remove-header (number)
11048   "Remove header NUMBER from `gnus-newsgroup-headers'."
11049   (if (and gnus-newsgroup-headers
11050            (= number (mail-header-number (car gnus-newsgroup-headers))))
11051       (pop gnus-newsgroup-headers)
11052     (let ((headers gnus-newsgroup-headers))
11053       (while (and (cdr headers)
11054                   (not (= number (mail-header-number (cadr headers)))))
11055         (pop headers))
11056       (when (cdr headers)
11057         (setcdr headers (cddr headers))))))
11058
11059 ;;;
11060 ;;; summary highlights
11061 ;;;
11062
11063 (defun gnus-highlight-selected-summary ()
11064   "Highlight selected article in summary buffer."
11065   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11066   (when gnus-summary-selected-face
11067     (save-excursion
11068       (let* ((beg (progn (beginning-of-line) (point)))
11069              (end (progn (end-of-line) (point)))
11070              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11071              (from (if (get-text-property beg gnus-mouse-face-prop)
11072                        beg
11073                      (or (next-single-property-change
11074                           beg gnus-mouse-face-prop nil end)
11075                          beg)))
11076              (to
11077               (if (= from end)
11078                   (- from 2)
11079                 (or (next-single-property-change
11080                      from gnus-mouse-face-prop nil end)
11081                     end))))
11082         ;; If no mouse-face prop on line we will have to = from = end,
11083         ;; so we highlight the entire line instead.
11084         (when (= (+ to 2) from)
11085           (setq from beg)
11086           (setq to end))
11087         (if gnus-newsgroup-selected-overlay
11088             ;; Move old overlay.
11089             (gnus-move-overlay
11090              gnus-newsgroup-selected-overlay from to (current-buffer))
11091           ;; Create new overlay.
11092           (gnus-overlay-put
11093            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11094            'face gnus-summary-selected-face))))))
11095
11096 (defvar gnus-summary-highlight-line-cached nil)
11097 (defvar gnus-summary-highlight-line-trigger nil)
11098
11099 (defun gnus-summary-highlight-line-0 ()
11100   (if (and (eq gnus-summary-highlight-line-trigger 
11101                gnus-summary-highlight)
11102            gnus-summary-highlight-line-cached)
11103       gnus-summary-highlight-line-cached
11104     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11105           gnus-summary-highlight-line-cached
11106           (let* ((cond (list 'cond))
11107                  (c cond)
11108                  (list gnus-summary-highlight))
11109             (while list
11110               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11111               (setq c (cdr c)
11112                     list (cdr list)))
11113             (gnus-byte-compile (list 'lambda nil cond))))))
11114
11115 (defun gnus-summary-highlight-line ()
11116   "Highlight current line according to `gnus-summary-highlight'."
11117   (let* ((beg (gnus-point-at-bol))
11118          (article (or (gnus-summary-article-number) gnus-current-article))
11119          (score (or (cdr (assq article
11120                                gnus-newsgroup-scored))
11121                     gnus-summary-default-score 0))
11122          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11123          (inhibit-read-only t)
11124          (default gnus-summary-default-score)
11125          (default-high gnus-summary-default-high-score)
11126          (default-low gnus-summary-default-low-score)
11127          (uncached (memq article gnus-newsgroup-undownloaded))
11128          (downloaded (not uncached)))
11129     (let ((face (funcall (gnus-summary-highlight-line-0))))
11130       (unless (eq face (get-text-property beg 'face))
11131         (gnus-put-text-property-excluding-characters-with-faces
11132          beg (gnus-point-at-eol) 'face
11133          (setq face (if (boundp face) (symbol-value face) face)))
11134         (when gnus-summary-highlight-line-function
11135           (funcall gnus-summary-highlight-line-function article face))))))
11136
11137 (defun gnus-update-read-articles (group unread &optional compute)
11138   "Update the list of read articles in GROUP.
11139 UNREAD is a sorted list."
11140   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11141          (entry (gnus-gethash group gnus-newsrc-hashtb))
11142          (info (nth 2 entry))
11143          (prev 1)
11144          read)
11145     (if (or (not info) (not active))
11146         ;; There is no info on this group if it was, in fact,
11147         ;; killed.  Gnus stores no information on killed groups, so
11148         ;; there's nothing to be done.
11149         ;; One could store the information somewhere temporarily,
11150         ;; perhaps...  Hmmm...
11151         ()
11152       ;; Remove any negative articles numbers.
11153       (while (and unread (< (car unread) 0))
11154         (setq unread (cdr unread)))
11155       ;; Remove any expired article numbers
11156       (while (and unread (< (car unread) (car active)))
11157         (setq unread (cdr unread)))
11158       ;; Compute the ranges of read articles by looking at the list of
11159       ;; unread articles.
11160       (while unread
11161         (when (/= (car unread) prev)
11162           (push (if (= prev (1- (car unread))) prev
11163                   (cons prev (1- (car unread))))
11164                 read))
11165         (setq prev (1+ (car unread)))
11166         (setq unread (cdr unread)))
11167       (when (<= prev (cdr active))
11168         (push (cons prev (cdr active)) read))
11169       (setq read (if (> (length read) 1) (nreverse read) read))
11170       (if compute
11171           read
11172         (save-excursion
11173           (let (setmarkundo)
11174             ;; Propagate the read marks to the backend.
11175             (when (gnus-check-backend-function 'request-set-mark group)
11176               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11177                     (add (gnus-remove-from-range read (gnus-info-read info))))
11178                 (when (or add del)
11179                   (unless (gnus-check-group group)
11180                     (error "Can't open server for %s" group))
11181                   (gnus-request-set-mark
11182                    group (delq nil (list (if add (list add 'add '(read)))
11183                                          (if del (list del 'del '(read))))))
11184                   (setq setmarkundo
11185                         `(gnus-request-set-mark
11186                           ,group
11187                           ',(delq nil (list
11188                                        (if del (list del 'add '(read)))
11189                                        (if add (list add 'del '(read))))))))))
11190             (set-buffer gnus-group-buffer)
11191             (gnus-undo-register
11192               `(progn
11193                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11194                  (gnus-info-set-read ',info ',(gnus-info-read info))
11195                  (gnus-get-unread-articles-in-group ',info
11196                                                     (gnus-active ,group))
11197                  (gnus-group-update-group ,group t)
11198                  ,setmarkundo))))
11199         ;; Enter this list into the group info.
11200         (gnus-info-set-read info read)
11201         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11202         (gnus-get-unread-articles-in-group info (gnus-active group))
11203         t))))
11204
11205 (defun gnus-offer-save-summaries ()
11206   "Offer to save all active summary buffers."
11207   (let (buffers)
11208     ;; Go through all buffers and find all summaries.
11209     (dolist (buffer (buffer-list))
11210       (when (and (setq buffer (buffer-name buffer))
11211                  (string-match "Summary" buffer)
11212                  (save-excursion
11213                    (set-buffer buffer)
11214                    ;; We check that this is, indeed, a summary buffer.
11215                    (and (eq major-mode 'gnus-summary-mode)
11216                         ;; Also make sure this isn't bogus.
11217                         gnus-newsgroup-prepared
11218                         ;; Also make sure that this isn't a
11219                         ;; dead summary buffer.
11220                         (not gnus-dead-summary-mode))))
11221         (push buffer buffers)))
11222     ;; Go through all these summary buffers and offer to save them.
11223     (when buffers
11224       (save-excursion
11225         (map-y-or-n-p
11226          "Update summary buffer %s? "
11227          (lambda (buf)
11228            (switch-to-buffer buf)
11229            (gnus-summary-exit))
11230          buffers)))))
11231
11232
11233 ;;; @ for mime-partial
11234 ;;;
11235
11236 (defun gnus-request-partial-message ()
11237   (save-excursion
11238     (let ((number (gnus-summary-article-number))
11239           (group gnus-newsgroup-name)
11240           (mother gnus-article-buffer))
11241       (set-buffer (get-buffer-create " *Partial Article*"))
11242       (erase-buffer)
11243       (setq mime-preview-buffer mother)
11244       (gnus-request-article-this-buffer number group)
11245       (mime-parse-buffer)
11246       )))
11247
11248 (autoload 'mime-combine-message/partial-pieces-automatically
11249   "mime-partial"
11250   "Internal method to combine message/partial messages automatically.")
11251
11252 (mime-add-condition
11253  'action '((type . message)(subtype . partial)
11254            (major-mode . gnus-original-article-mode)
11255            (method . mime-combine-message/partial-pieces-automatically)
11256            (summary-buffer-exp . gnus-summary-buffer)
11257            (request-partial-message-method . gnus-request-partial-message)
11258            ))
11259
11260
11261 ;;; @ for message/rfc822
11262 ;;;
11263
11264 (defun gnus-mime-extract-message/rfc822 (entity situation)
11265   "Burst a forwarded article."
11266   (save-excursion
11267     (set-buffer gnus-summary-buffer)
11268     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11269                                    gnus-newsgroup-name 'gnus-group-history))
11270            (gnus-group-marked (list group))
11271            article info)
11272       (with-temp-buffer
11273         (mime-insert-entity-content entity)
11274         (setq article (gnus-request-accept-article group)))
11275       (when (and (consp article)
11276                  (numberp (setq article (cdr article))))
11277         (setq info (gnus-get-info group))
11278         (gnus-info-set-read info
11279                             (gnus-remove-from-range (gnus-info-read info)
11280                                                     (list article)))
11281         (when (string-equal group gnus-newsgroup-name)
11282           (forward-line 1)
11283           (let (gnus-show-threads)
11284             (gnus-summary-goto-subject article t))
11285           (gnus-summary-clear-mark-forward 1))
11286         (set-buffer gnus-group-buffer)
11287         (gnus-group-get-new-news-this-group nil t)))))
11288
11289 (mime-add-condition
11290  'action '((type . message)(subtype . rfc822)
11291            (major-mode . gnus-original-article-mode)
11292            (method . gnus-mime-extract-message/rfc822)
11293            (mode . "extract")
11294            ))
11295
11296 (mime-add-condition
11297  'action '((type . message)(subtype . news)
11298            (major-mode . gnus-original-article-mode)
11299            (method . gnus-mime-extract-message/rfc822)
11300            (mode . "extract")
11301            ))
11302
11303 (defun gnus-mime-extract-multipart (entity situation)
11304   (let ((children (mime-entity-children entity))
11305         mime-acting-situation-to-override
11306         f)
11307     (while children
11308       (mime-play-entity (car children)
11309                         (cons (assq 'mode situation)
11310                               mime-acting-situation-to-override))
11311       (setq children (cdr children)))
11312     (if (setq f (cdr (assq 'after-method
11313                            mime-acting-situation-to-override)))
11314         (eval f)
11315       )))
11316
11317 (mime-add-condition
11318  'action '((type . multipart)
11319            (method . gnus-mime-extract-multipart)
11320            (mode . "extract")
11321            )
11322  'with-default)
11323
11324
11325 ;;; @ end
11326 ;;;
11327
11328 (defun gnus-summary-inherit-default-charset ()
11329   "Import `default-mime-charset' from summary buffer.
11330 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11331 of FLIM is used."
11332   (if (buffer-live-p gnus-summary-buffer)
11333       (let (d-m-c d-m-c-u)
11334         (with-current-buffer gnus-summary-buffer
11335           (setq d-m-c (if (local-variable-p 'default-mime-charset
11336                                             gnus-summary-buffer)
11337                           default-mime-charset
11338                         t)
11339                 ;; LIMIT
11340                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11341                                               gnus-summary-buffer)
11342                             (symbol-value 'default-mime-charset-unlimited)
11343                           t)))
11344         (if (eq t d-m-c)
11345             (kill-local-variable 'default-mime-charset)
11346           (set (make-local-variable 'default-mime-charset) d-m-c))
11347         (if (eq t d-m-c-u)
11348             (kill-local-variable 'default-mime-charset-unlimited)
11349           (set (make-local-variable 'default-mime-charset-unlimited)
11350                d-m-c-u)))))
11351
11352 (defun gnus-summary-setup-default-charset ()
11353   "Setup newsgroup default charset."
11354   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11355       (progn
11356         (setq gnus-newsgroup-charset nil)
11357         (set (make-local-variable 'default-mime-charset) nil)
11358         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11359           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11360     (let ((ignored-charsets
11361            (or gnus-newsgroup-ephemeral-ignored-charsets
11362                (append
11363                 (and gnus-newsgroup-name
11364                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11365                 gnus-newsgroup-ignored-charsets)))
11366           charset)
11367       (setq gnus-newsgroup-charset
11368             (or gnus-newsgroup-ephemeral-charset
11369                 (when (and gnus-newsgroup-name
11370                            (setq charset (gnus-parameter-charset
11371                                           gnus-newsgroup-name)))
11372                   (make-local-variable 'default-mime-charset)
11373                   (setq default-mime-charset charset))
11374                 gnus-default-charset))
11375       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11376            ignored-charsets))))
11377
11378 ;;;
11379 ;;; Mime Commands
11380 ;;;
11381
11382 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11383   "Display the current article buffer fully MIME-buttonized.
11384 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11385 treated as multipart/mixed."
11386   (interactive "P")
11387   (require 'gnus-art)
11388   (let ((gnus-unbuttonized-mime-types nil)
11389         (gnus-mime-display-multipart-as-mixed show-all-parts))
11390     (gnus-summary-show-article)))
11391
11392 (defun gnus-summary-repair-multipart (article)
11393   "Add a Content-Type header to a multipart article without one."
11394   (interactive (list (gnus-summary-article-number)))
11395   (gnus-with-article article
11396     (message-narrow-to-head)
11397     (message-remove-header "Mime-Version")
11398     (goto-char (point-max))
11399     (insert "Mime-Version: 1.0\n")
11400     (widen)
11401     (when (search-forward "\n--" nil t)
11402       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11403         (message-narrow-to-head)
11404         (message-remove-header "Content-Type")
11405         (goto-char (point-max))
11406         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11407                         separator))
11408         (widen))))
11409   (let (gnus-mark-article-hook)
11410     (gnus-summary-select-article t t nil article)))
11411
11412 (defun gnus-summary-toggle-display-buttonized ()
11413   "Toggle the buttonizing of the article buffer."
11414   (interactive)
11415   (require 'gnus-art)
11416   (if (setq gnus-inhibit-mime-unbuttonizing
11417             (not gnus-inhibit-mime-unbuttonizing))
11418       (let ((gnus-unbuttonized-mime-types nil))
11419         (gnus-summary-show-article))
11420     (gnus-summary-show-article)))
11421
11422 ;;;
11423 ;;; Intelli-mouse commmands
11424 ;;;
11425
11426 (defun gnus-wheel-summary-scroll (event)
11427   (interactive "e")
11428   (let ((amount (if (memq 'shift (event-modifiers event))
11429                     (car gnus-wheel-scroll-amount)
11430                   (cdr gnus-wheel-scroll-amount)))
11431         (direction (- (* (static-if (featurep 'xemacs)
11432                              (event-button event)
11433                            (cond ((eq 'mouse-4 (event-basic-type event))
11434                                   4)
11435                                  ((eq 'mouse-5 (event-basic-type event))
11436                                   5)))
11437                          2) 9))
11438         edge)
11439     (gnus-summary-scroll-up (* amount direction))
11440     (when (gnus-eval-in-buffer-window gnus-article-buffer
11441             (save-restriction
11442               (widen)
11443               (and (if (< 0 direction)
11444                        (gnus-article-next-page 0)
11445                      (gnus-article-prev-page 0)
11446                      (bobp))
11447                    (if (setq edge (get-text-property
11448                                    (point-min) 'gnus-wheel-edge))
11449                        (setq edge (* edge direction))
11450                      (setq edge -1))
11451                    (or (plusp edge)
11452                        (let ((buffer-read-only nil)
11453                              (inhibit-read-only t))
11454                          (put-text-property (point-min) (point-max)
11455                                             'gnus-wheel-edge direction)
11456                          nil))
11457                    (or (> edge gnus-wheel-edge-resistance)
11458                        (let ((buffer-read-only nil)
11459                              (inhibit-read-only t))
11460                          (put-text-property (point-min) (point-max)
11461                                             'gnus-wheel-edge
11462                                             (* (1+ edge) direction))
11463                          nil))
11464                    (eq last-command 'gnus-wheel-summary-scroll))))
11465       (gnus-summary-next-article nil nil (minusp direction)))))
11466
11467 (defun gnus-wheel-install ()
11468   "Enable mouse wheel support on summary window."
11469   (when gnus-use-wheel
11470     (let ((keys
11471            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11472       (dolist (key keys)
11473         (define-key gnus-summary-mode-map key
11474           'gnus-wheel-summary-scroll)))))
11475
11476 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11477
11478 ;;;
11479 ;;; Traditional PGP commmands
11480 ;;;
11481
11482 (defun gnus-summary-decrypt-article (&optional force)
11483   "Decrypt the current article in traditional PGP way.
11484 This will have permanent effect only in mail groups.
11485 If FORCE is non-nil, allow editing of articles even in read-only
11486 groups."
11487   (interactive "P")
11488   (gnus-summary-select-article t)
11489   (gnus-eval-in-buffer-window gnus-article-buffer
11490     (save-excursion
11491       (save-restriction
11492         (widen)
11493         (goto-char (point-min))
11494         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11495           (error "Not a traditional PGP message!"))
11496         (let ((armor-start (match-beginning 0)))
11497           (if (and (pgg-decrypt-region armor-start (point-max))
11498                    (or force (not (gnus-group-read-only-p))))
11499               (let ((inhibit-read-only t)
11500                     buffer-read-only)
11501                 (delete-region armor-start
11502                                (progn
11503                                  (re-search-forward "^-+END PGP" nil t)
11504                                  (beginning-of-line 2)
11505                                  (point)))
11506                 (insert-buffer-substring pgg-output-buffer))))))))
11507
11508 (defun gnus-summary-verify-article ()
11509   "Verify the current article in traditional PGP way."
11510   (interactive)
11511   (save-excursion
11512     (set-buffer gnus-original-article-buffer)
11513     (goto-char (point-min))
11514     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11515       (error "Not a traditional PGP message!"))
11516     (re-search-forward "^-+END PGP" nil t)
11517     (beginning-of-line 2)
11518     (call-interactively (function pgg-verify-region))))
11519
11520 ;;;
11521 ;;; Generic summary marking commands
11522 ;;;
11523
11524 (defvar gnus-summary-marking-alist
11525   '((read gnus-del-mark "d")
11526     (unread gnus-unread-mark "u")
11527     (ticked gnus-ticked-mark "!")
11528     (dormant gnus-dormant-mark "?")
11529     (expirable gnus-expirable-mark "e"))
11530   "An alist of names/marks/keystrokes.")
11531
11532 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11533 (defvar gnus-summary-mark-map)
11534
11535 (defun gnus-summary-make-all-marking-commands ()
11536   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11537   (dolist (elem gnus-summary-marking-alist)
11538     (apply 'gnus-summary-make-marking-command elem)))
11539
11540 (defun gnus-summary-make-marking-command (name mark keystroke)
11541   (let ((map (make-sparse-keymap)))
11542     (define-key gnus-summary-generic-mark-map keystroke map)
11543     (dolist (lway `((next "next" next nil "n")
11544                     (next-unread "next unread" next t "N")
11545                     (prev "previous" prev nil "p")
11546                     (prev-unread "previous unread" prev t "P")
11547                     (nomove "" nil nil ,keystroke)))
11548       (let ((func (gnus-summary-make-marking-command-1
11549                    mark (car lway) lway name)))
11550         (setq func (eval func))
11551         (define-key map (nth 4 lway) func)))))
11552
11553 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11554   `(defun ,(intern
11555             (format "gnus-summary-put-mark-as-%s%s"
11556                     name (if (eq way 'nomove)
11557                              ""
11558                            (concat "-" (symbol-name way)))))
11559      (n)
11560      ,(format
11561        "Mark the current article as %s%s.
11562 If N, the prefix, then repeat N times.
11563 If N is negative, move in reverse order.
11564 The difference between N and the actual number of articles marked is
11565 returned."
11566        name (car (cdr lway)))
11567      (interactive "p")
11568      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11569
11570 (defun gnus-summary-generic-mark (n mark move unread)
11571   "Mark N articles with MARK."
11572   (unless (eq major-mode 'gnus-summary-mode)
11573     (error "This command can only be used in the summary buffer"))
11574   (gnus-summary-show-thread)
11575   (let ((nummove
11576          (cond
11577           ((eq move 'next) 1)
11578           ((eq move 'prev) -1)
11579           (t 0))))
11580     (if (zerop nummove)
11581         (setq n 1)
11582       (when (< n 0)
11583         (setq n (abs n)
11584               nummove (* -1 nummove))))
11585     (while (and (> n 0)
11586                 (gnus-summary-mark-article nil mark)
11587                 (zerop (gnus-summary-next-subject nummove unread t)))
11588       (setq n (1- n)))
11589     (when (/= 0 n)
11590       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11591     (gnus-summary-recenter)
11592     (gnus-summary-position-point)
11593     (gnus-set-mode-line 'summary)
11594     n))
11595
11596 (defun gnus-summary-insert-articles (articles)
11597   (when (setq articles
11598               (gnus-sorted-difference articles
11599                                       (mapcar (lambda (h)
11600                                                 (mail-header-number h))
11601                                               gnus-newsgroup-headers)))
11602     (setq gnus-newsgroup-headers
11603           (merge 'list
11604                  gnus-newsgroup-headers
11605                  (gnus-fetch-headers articles)
11606                  'gnus-article-sort-by-number))
11607     ;; Suppress duplicates?
11608     (when gnus-suppress-duplicates
11609       (gnus-dup-suppress-articles))
11610
11611     ;; We might want to build some more threads first.
11612     (when (and gnus-fetch-old-headers
11613                (eq gnus-headers-retrieved-by 'nov))
11614       (if (eq gnus-fetch-old-headers 'invisible)
11615           (gnus-build-all-threads)
11616         (gnus-build-old-threads)))
11617     ;; Let the Gnus agent mark articles as read.
11618     (when gnus-agent
11619       (gnus-agent-get-undownloaded-list))
11620     ;; Remove list identifiers from subject
11621     (when gnus-list-identifiers
11622       (gnus-summary-remove-list-identifiers))
11623     ;; First and last article in this newsgroup.
11624     (when gnus-newsgroup-headers
11625       (setq gnus-newsgroup-begin
11626             (mail-header-number (car gnus-newsgroup-headers))
11627             gnus-newsgroup-end
11628             (mail-header-number
11629              (gnus-last-element gnus-newsgroup-headers))))
11630     (when gnus-use-scoring
11631       (gnus-possibly-score-headers))))
11632
11633 (defun gnus-summary-insert-old-articles (&optional all)
11634   "Insert all old articles in this group.
11635 If ALL is non-nil, already read articles become readable.
11636 If ALL is a number, fetch this number of articles."
11637   (interactive "P")
11638   (prog1
11639       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11640             older len)
11641         (setq older
11642               ;; Some nntp servers lie about their active range.  When
11643               ;; this happens, the active range can be in the millions.
11644               ;; Use a compressed range to avoid creating a huge list.
11645               (gnus-range-difference (list gnus-newsgroup-active) old))
11646         (setq len (gnus-range-length older))
11647         (cond
11648          ((null older) nil)
11649          ((numberp all)
11650           (if (< all len)
11651               (let ((older-range (nreverse older)))
11652                 (setq older nil)
11653
11654                 (while (> all 0)
11655                   (let* ((r (pop older-range))
11656                          (min (if (numberp r) r (car r)))
11657                          (max (if (numberp r) r (cdr r))))
11658                     (while (and (<= min max)
11659                                 (> all 0))
11660                       (push max older)
11661                       (setq all (1- all)
11662                             max (1- max))))))
11663             (setq older (gnus-uncompress-range older))))
11664          (all
11665           (setq older (gnus-uncompress-range older)))
11666          (t
11667           (when (and (numberp gnus-large-newsgroup)
11668                    (> len gnus-large-newsgroup))
11669               (let* ((cursor-in-echo-area nil)
11670                      (initial (gnus-parameter-large-newsgroup-initial
11671                                gnus-newsgroup-name))
11672                      (input
11673                       (read-string
11674                        (format
11675                         "How many articles from %s (%s %d): "
11676                         (gnus-limit-string
11677                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11678                         (if initial "max" "default")
11679                         len)
11680                        (if initial
11681                            (cons (number-to-string initial)
11682                                  0)))))
11683                 (unless (string-match "^[ \t]*$" input)
11684                   (setq all (string-to-number input))
11685                   (if (< all len)
11686                       (let ((older-range (nreverse older)))
11687                         (setq older nil)
11688
11689                         (while (> all 0)
11690                           (let* ((r (pop older-range))
11691                                  (min (if (numberp r) r (car r)))
11692                                  (max (if (numberp r) r (cdr r))))
11693                             (while (and (<= min max)
11694                                         (> all 0))
11695                               (push max older)
11696                               (setq all (1- all)
11697                                     max (1- max))))))))))
11698           (setq older (gnus-uncompress-range older))))
11699         (if (not older)
11700             (message "No old news.")
11701           (gnus-summary-insert-articles older)
11702           (gnus-summary-limit (gnus-sorted-nunion old older))))
11703     (gnus-summary-position-point)))
11704
11705 (defun gnus-summary-insert-new-articles ()
11706   "Insert all new articles in this group."
11707   (interactive)
11708   (prog1
11709       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11710             (old-active gnus-newsgroup-active)
11711             (nnmail-fetched-sources (list t))
11712             i new)
11713         (setq gnus-newsgroup-active
11714               (gnus-activate-group gnus-newsgroup-name 'scan))
11715         (setq i (cdr gnus-newsgroup-active))
11716         (while (> i (cdr old-active))
11717           (push i new)
11718           (decf i))
11719         (if (not new)
11720             (message "No gnus is bad news.")
11721           (gnus-summary-insert-articles new)
11722           (setq gnus-newsgroup-unreads
11723                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11724           (gnus-summary-limit (gnus-sorted-nunion old new))))
11725     (gnus-summary-position-point)))
11726
11727 (gnus-summary-make-all-marking-commands)
11728
11729 (gnus-ems-redefine)
11730
11731 (provide 'gnus-sum)
11732
11733 (run-hooks 'gnus-sum-load-hook)
11734
11735 ;;; gnus-sum.el ends here