Translated.
[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 ?$
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-article-move-hook nil
877   "*A hook called after an article is moved, copied, respooled, or crossposted."
878   :group 'gnus-summary
879   :type 'hook)
880
881 (defcustom gnus-summary-article-delete-hook nil
882   "*A hook called after an article is deleted."
883   :group 'gnus-summary
884   :type 'hook)
885
886 (defcustom gnus-summary-article-expire-hook nil
887   "*A hook called after an article is expired."
888   :group 'gnus-summary
889   :type 'hook)
890
891 (defcustom gnus-summary-display-arrow
892   (and (fboundp 'display-graphic-p)
893        (display-graphic-p))
894   "*If non-nil, display an arrow highlighting the current article."
895   :version "21.1"
896   :group 'gnus-summary
897   :type 'boolean)
898
899 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
900   "Face used for highlighting the current article in the summary buffer."
901   :group 'gnus-summary-visual
902   :type 'face)
903
904 (defvar gnus-tmp-downloaded nil)
905
906 (defcustom gnus-summary-highlight
907   '(((eq mark gnus-canceled-mark)
908      . gnus-summary-cancelled-face)
909     ((and uncached (> score default-high))
910      . gnus-summary-high-undownloaded-face)
911     ((and uncached (< score default-low))
912      . gnus-summary-low-undownloaded-face)
913     (uncached
914      . gnus-summary-normal-undownloaded-face)
915     ((and (> score default-high)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-high-ticked-face)
919     ((and (< score default-low)
920           (or (eq mark gnus-dormant-mark)
921               (eq mark gnus-ticked-mark)))
922      . gnus-summary-low-ticked-face)
923     ((or (eq mark gnus-dormant-mark)
924          (eq mark gnus-ticked-mark))
925      . gnus-summary-normal-ticked-face)
926     ((and (> score default-high) (eq mark gnus-ancient-mark))
927      . gnus-summary-high-ancient-face)
928     ((and (< score default-low) (eq mark gnus-ancient-mark))
929      . gnus-summary-low-ancient-face)
930     ((eq mark gnus-ancient-mark)
931      . gnus-summary-normal-ancient-face)
932     ((and (> score default-high) (eq mark gnus-unread-mark))
933      . gnus-summary-high-unread-face)
934     ((and (< score default-low) (eq mark gnus-unread-mark))
935      . gnus-summary-low-unread-face)
936     ((eq mark gnus-unread-mark)
937      . gnus-summary-normal-unread-face)
938     ((> score default-high)
939      . gnus-summary-high-read-face)
940     ((< score default-low)
941      . gnus-summary-low-read-face)
942     (t
943      . gnus-summary-normal-read-face))
944   "*Controls the highlighting of summary buffer lines.
945
946 A list of (FORM . FACE) pairs.  When deciding how a a particular
947 summary line should be displayed, each form is evaluated.  The content
948 of the face field after the first true form is used.  You can change
949 how those summary lines are displayed, by editing the face field.
950
951 You can use the following variables in the FORM field.
952
953 score:        The article's score
954 default:      The default article score.
955 default-high: The default score for high scored articles.
956 default-low:  The default score for low scored articles.
957 below:        The score below which articles are automatically marked as read.
958 mark:         The articles mark."
959   :group 'gnus-summary-visual
960   :type '(repeat (cons (sexp :tag "Form" nil)
961                        face)))
962
963 (defcustom gnus-alter-header-function nil
964   "Function called to allow alteration of article header structures.
965 The function is called with one parameter, the article header vector,
966 which it may alter in any way."
967   :type '(choice (const :tag "None" nil)
968                  function)
969   :group 'gnus-summary)
970
971 (defvar gnus-decode-encoded-word-function
972   (mime-find-field-decoder 'From 'nov)
973   "Variable that says which function should be used to decode a string with encoded words.")
974
975 (defcustom gnus-extra-headers '(To Newsgroups)
976   "*Extra headers to parse."
977   :version "21.1"
978   :group 'gnus-summary
979   :type '(repeat symbol))
980
981 (defcustom gnus-ignored-from-addresses
982   (and user-mail-address (regexp-quote user-mail-address))
983   "*Regexp of From headers that may be suppressed in favor of To headers."
984   :version "21.1"
985   :group 'gnus-summary
986   :type 'regexp)
987
988 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
989   "List of charsets that should be ignored.
990 When these charsets are used in the \"charset\" parameter, the
991 default charset will be used instead."
992   :version "21.1"
993   :type '(repeat symbol)
994   :group 'gnus-charset)
995
996 (gnus-define-group-parameter
997  ignored-charsets
998  :type list
999  :function-document
1000  "Return the ignored charsets of GROUP."
1001  :variable gnus-group-ignored-charsets-alist
1002  :variable-default
1003  '(("alt\\.chinese\\.text" iso-8859-1))
1004  :variable-document
1005  "Alist of regexps (to match group names) and charsets that should be ignored.
1006 When these charsets are used in the \"charset\" parameter, the
1007 default charset will be used instead."
1008  :variable-group gnus-charset
1009  :variable-type '(repeat (cons (regexp :tag "Group")
1010                                (repeat symbol)))
1011  :parameter-type '(choice :tag "Ignored charsets"
1012                           :value nil
1013                           (repeat (symbol)))
1014  :parameter-document       "\
1015 List of charsets that should be ignored.
1016
1017 When these charsets are used in the \"charset\" parameter, the
1018 default charset will be used instead.")
1019
1020 (defcustom gnus-group-highlight-words-alist nil
1021   "Alist of group regexps and highlight regexps.
1022 This variable uses the same syntax as `gnus-emphasis-alist'."
1023   :version "21.1"
1024   :type '(repeat (cons (regexp :tag "Group")
1025                        (repeat (list (regexp :tag "Highlight regexp")
1026                                      (number :tag "Group for entire word" 0)
1027                                      (number :tag "Group for displayed part" 0)
1028                                      (symbol :tag "Face"
1029                                              gnus-emphasis-highlight-words)))))
1030   :group 'gnus-summary-visual)
1031
1032 (defcustom gnus-use-wheel nil
1033   "Use Intelli-mouse on summary movement"
1034   :type 'boolean
1035   :group 'gnus-summary-maneuvering)
1036
1037 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1038   "Amount to scroll messages by spinning the mouse wheel.
1039 This is actually a cons cell, where the first item is the amount to scroll
1040 on a normal wheel event, and the second is the amount to scroll when the
1041 wheel is moved with the shift key depressed."
1042   :type '(cons (integer :tag "Shift") integer)
1043   :group 'gnus-summary-maneuvering)
1044
1045 (defcustom gnus-wheel-edge-resistance 2
1046   "How hard it should be to change the current article
1047 by moving the mouse over the edge of the article window."
1048   :type 'integer
1049   :group 'gnus-summary-maneuvering)
1050
1051 (defcustom gnus-summary-show-article-charset-alist
1052   nil
1053   "Alist of number and charset.
1054 The article will be shown with the charset corresponding to the
1055 numbered argument.
1056 For example: ((1 . cn-gb-2312) (2 . big5))."
1057   :version "21.1"
1058   :type '(repeat (cons (number :tag "Argument" 1)
1059                        (symbol :tag "Charset")))
1060   :group 'gnus-charset)
1061
1062 (defcustom gnus-preserve-marks t
1063   "Whether marks are preserved when moving, copying and respooling messages."
1064   :version "21.1"
1065   :type 'boolean
1066   :group 'gnus-summary-marks)
1067
1068 (defcustom gnus-alter-articles-to-read-function nil
1069   "Function to be called to alter the list of articles to be selected."
1070   :type '(choice (const nil) function)
1071   :group 'gnus-summary)
1072
1073 (defcustom gnus-orphan-score nil
1074   "*All orphans get this score added.  Set in the score file."
1075   :group 'gnus-score-default
1076   :type '(choice (const nil)
1077                  integer))
1078
1079 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1080   "*A regexp to match MIME parts when saving multiple parts of a message
1081 with gnus-summary-save-parts (X m). This regexp will be used by default
1082 when prompting the user for which type of files to save."
1083   :group 'gnus-summary
1084   :type 'regexp)
1085
1086 (defcustom gnus-read-all-available-headers nil
1087   "Whether Gnus should parse all headers made available to it.
1088 This is mostly relevant for slow backends where the user may
1089 wish to widen the summary buffer to include all headers
1090 that were fetched.  Say, for nnultimate groups."
1091   :group 'gnus-summary
1092   :type '(choice boolean regexp))
1093
1094 (defcustom gnus-summary-muttprint-program "muttprint"
1095   "Command (and optional arguments) used to run Muttprint."
1096   :version "21.3"
1097   :group 'gnus-summary
1098   :type 'string)
1099
1100 (defcustom gnus-article-loose-mime nil
1101   "If non-nil, don't require MIME-Version header.
1102 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1103 supply the MIME-Version header or deliberately strip it From the mail.
1104 Set it to non-nil, Gnus will treat some articles as MIME even if
1105 the MIME-Version header is missed."
1106   :version "21.3"
1107   :type 'boolean
1108   :group 'gnus-article)
1109
1110 (defcustom gnus-article-emulate-mime t
1111   "If non-nil, use MIME emulation for uuencode and the like.
1112 This means that Gnus will search message bodies for text that look
1113 like uuencoded bits, yEncoded bits, and so on, and present that using
1114 the normal Gnus MIME machinery."
1115   :type 'boolean
1116   :group 'gnus-article)
1117
1118 ;;; Internal variables
1119
1120 (defvar gnus-summary-display-cache nil)
1121 (defvar gnus-article-mime-handles nil)
1122 (defvar gnus-article-decoded-p nil)
1123 (defvar gnus-article-charset nil)
1124 (defvar gnus-article-ignored-charsets nil)
1125 (defvar gnus-scores-exclude-files nil)
1126 (defvar gnus-page-broken nil)
1127
1128 (defvar gnus-original-article nil)
1129 (defvar gnus-article-internal-prepare-hook nil)
1130 (defvar gnus-newsgroup-process-stack nil)
1131
1132 (defvar gnus-thread-indent-array nil)
1133 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1134 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1135   "Function called to sort the articles within a thread after it has been gathered together.")
1136
1137 (defvar gnus-summary-save-parts-type-history nil)
1138 (defvar gnus-summary-save-parts-last-directory nil)
1139
1140 ;; Avoid highlighting in kill files.
1141 (defvar gnus-summary-inhibit-highlight nil)
1142 (defvar gnus-newsgroup-selected-overlay nil)
1143 (defvar gnus-inhibit-limiting nil)
1144 (defvar gnus-newsgroup-adaptive-score-file nil)
1145 (defvar gnus-current-score-file nil)
1146 (defvar gnus-current-move-group nil)
1147 (defvar gnus-current-copy-group nil)
1148 (defvar gnus-current-crosspost-group nil)
1149 (defvar gnus-newsgroup-display nil)
1150
1151 (defvar gnus-newsgroup-dependencies nil)
1152 (defvar gnus-newsgroup-adaptive nil)
1153 (defvar gnus-summary-display-article-function nil)
1154 (defvar gnus-summary-highlight-line-function nil
1155   "Function called after highlighting a summary line.")
1156
1157 (defvar gnus-summary-line-format-alist
1158   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1159     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1160     (?s gnus-tmp-subject-or-nil ?s)
1161     (?n gnus-tmp-name ?s)
1162     (?A (std11-address-string
1163          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1164     (?a (or (std11-full-name-string
1165              (car (mime-entity-read-field gnus-tmp-header 'From)))
1166             gnus-tmp-from) ?s)
1167     (?F gnus-tmp-from ?s)
1168     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1169     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1170     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1171     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1172     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1173     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1174     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1175     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1176     (?L gnus-tmp-lines ?s)
1177     (?O gnus-tmp-downloaded ?c)
1178     (?I gnus-tmp-indentation ?s)
1179     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1180     (?R gnus-tmp-replied ?c)
1181     (?\[ gnus-tmp-opening-bracket ?c)
1182     (?\] gnus-tmp-closing-bracket ?c)
1183     (?\> (make-string gnus-tmp-level ? ) ?s)
1184     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1185     (?i gnus-tmp-score ?d)
1186     (?z gnus-tmp-score-char ?c)
1187     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1188     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1189     (?U gnus-tmp-unread ?c)
1190     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1191         ?s)
1192     (?t (gnus-summary-number-of-articles-in-thread
1193          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1194         ?d)
1195     (?e (gnus-summary-number-of-articles-in-thread
1196          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1197         ?c)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?P (gnus-pick-line-number) ?d)
1200     (?B gnus-tmp-thread-tree-header-string ?s)
1201     (user-date (gnus-user-date
1202                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1203   "An alist of format specifications that can appear in summary lines.
1204 These are paired with what variables they correspond with, along with
1205 the type of the variable (string, integer, character, etc).")
1206
1207 (defvar gnus-summary-dummy-line-format-alist
1208   `((?S gnus-tmp-subject ?s)
1209     (?N gnus-tmp-number ?d)
1210     (?u gnus-tmp-user-defined ?s)))
1211
1212 (defvar gnus-summary-mode-line-format-alist
1213   `((?G gnus-tmp-group-name ?s)
1214     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1215     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1216     (?A gnus-tmp-article-number ?d)
1217     (?Z gnus-tmp-unread-and-unselected ?s)
1218     (?V gnus-version ?s)
1219     (?U gnus-tmp-unread-and-unticked ?d)
1220     (?S gnus-tmp-subject ?s)
1221     (?e gnus-tmp-unselected ?d)
1222     (?u gnus-tmp-user-defined ?s)
1223     (?d (length gnus-newsgroup-dormant) ?d)
1224     (?t (length gnus-newsgroup-marked) ?d)
1225     (?h (length gnus-newsgroup-spam-marked) ?d)
1226     (?r (length gnus-newsgroup-reads) ?d)
1227     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1228     (?E gnus-newsgroup-expunged-tally ?d)
1229     (?s (gnus-current-score-file-nondirectory) ?s)))
1230
1231 (defvar gnus-last-search-regexp nil
1232   "Default regexp for article search command.")
1233
1234 (defvar gnus-summary-search-article-matched-data nil
1235   "Last matched data of article search command.  It is the local variable
1236 in `gnus-article-buffer' which consists of the list of start position,
1237 end position and text.")
1238
1239 (defvar gnus-last-shell-command nil
1240   "Default shell command on article.")
1241
1242 (defvar gnus-newsgroup-agentized nil
1243   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1244 (defvar gnus-newsgroup-begin nil)
1245 (defvar gnus-newsgroup-end nil)
1246 (defvar gnus-newsgroup-last-rmail nil)
1247 (defvar gnus-newsgroup-last-mail nil)
1248 (defvar gnus-newsgroup-last-folder nil)
1249 (defvar gnus-newsgroup-last-file nil)
1250 (defvar gnus-newsgroup-auto-expire nil)
1251 (defvar gnus-newsgroup-active nil)
1252
1253 (defvar gnus-newsgroup-data nil)
1254 (defvar gnus-newsgroup-data-reverse nil)
1255 (defvar gnus-newsgroup-limit nil)
1256 (defvar gnus-newsgroup-limits nil)
1257
1258 (defvar gnus-newsgroup-unreads nil
1259   "Sorted list of unread articles in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-unselected nil
1262   "Sorted list of unselected unread articles in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-reads nil
1265   "Alist of read articles and article marks in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-expunged-tally nil)
1268
1269 (defvar gnus-newsgroup-marked nil
1270   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1271
1272 (defvar gnus-newsgroup-spam-marked nil
1273   "List of ranges of articles that have been marked as spam.")
1274
1275 (defvar gnus-newsgroup-killed nil
1276   "List of ranges of articles that have been through the scoring process.")
1277
1278 (defvar gnus-newsgroup-cached nil
1279   "Sorted list of articles that come from the article cache.")
1280
1281 (defvar gnus-newsgroup-saved nil
1282   "List of articles that have been saved.")
1283
1284 (defvar gnus-newsgroup-kill-headers nil)
1285
1286 (defvar gnus-newsgroup-replied nil
1287   "List of articles that have been replied to in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-forwarded nil
1290   "List of articles that have been forwarded in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-recent nil
1293   "List of articles that have are recent in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-expirable nil
1296   "Sorted list of articles in the current newsgroup that can be expired.")
1297
1298 (defvar gnus-newsgroup-processable nil
1299   "List of articles in the current newsgroup that can be processed.")
1300
1301 (defvar gnus-newsgroup-downloadable nil
1302   "Sorted list of articles in the current newsgroup that can be processed.")
1303
1304 (defvar gnus-newsgroup-unfetched nil
1305   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1306
1307 (defvar gnus-newsgroup-undownloaded nil
1308   "List of articles in the current newsgroup that haven't been downloaded..")
1309
1310 (defvar gnus-newsgroup-unsendable nil
1311   "List of articles in the current newsgroup that won't be sent.")
1312
1313 (defvar gnus-newsgroup-bookmarks nil
1314   "List of articles in the current newsgroup that have bookmarks.")
1315
1316 (defvar gnus-newsgroup-dormant nil
1317   "Sorted list of dormant articles in the current newsgroup.")
1318
1319 (defvar gnus-newsgroup-unseen nil
1320   "List of unseen articles in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-seen nil
1323   "Range of seen articles in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-articles nil
1326   "List of articles in the current newsgroup.")
1327
1328 (defvar gnus-newsgroup-scored nil
1329   "List of scored articles in the current newsgroup.")
1330
1331 (defvar gnus-newsgroup-incorporated nil
1332   "List of incorporated articles in the current newsgroup.")
1333
1334 (defvar gnus-newsgroup-headers nil
1335   "List of article headers in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-threads nil)
1338
1339 (defvar gnus-newsgroup-prepared nil
1340   "Whether the current group has been prepared properly.")
1341
1342 (defvar gnus-newsgroup-ancient nil
1343   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-sparse nil)
1346
1347 (defvar gnus-current-article nil)
1348 (defvar gnus-article-current nil)
1349 (defvar gnus-current-headers nil)
1350 (defvar gnus-have-all-headers nil)
1351 (defvar gnus-last-article nil)
1352 (defvar gnus-newsgroup-history nil)
1353 (defvar gnus-newsgroup-charset nil)
1354 (defvar gnus-newsgroup-ephemeral-charset nil)
1355 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1356
1357 (defvar gnus-article-before-search nil)
1358
1359 (defconst gnus-summary-local-variables
1360   '(gnus-newsgroup-name
1361     gnus-newsgroup-begin gnus-newsgroup-end
1362     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1363     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1364     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1365     gnus-newsgroup-unselected gnus-newsgroup-marked
1366     gnus-newsgroup-spam-marked
1367     gnus-newsgroup-reads gnus-newsgroup-saved
1368     gnus-newsgroup-replied gnus-newsgroup-forwarded
1369     gnus-newsgroup-recent
1370     gnus-newsgroup-expirable
1371     gnus-newsgroup-processable gnus-newsgroup-killed
1372     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1373     gnus-newsgroup-unfetched
1374     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1375     gnus-newsgroup-seen gnus-newsgroup-articles
1376     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1377     gnus-newsgroup-headers gnus-newsgroup-threads
1378     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1379     gnus-current-article gnus-current-headers gnus-have-all-headers
1380     gnus-last-article gnus-article-internal-prepare-hook
1381     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1382     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1383     gnus-thread-expunge-below
1384     gnus-score-alist gnus-current-score-file
1385     (gnus-summary-expunge-below . global)
1386     (gnus-summary-mark-below . global)
1387     (gnus-orphan-score . global)
1388     gnus-newsgroup-active gnus-scores-exclude-files
1389     gnus-newsgroup-history gnus-newsgroup-ancient
1390     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1391     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1392     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1393     (gnus-newsgroup-expunged-tally . 0)
1394     gnus-cache-removable-articles gnus-newsgroup-cached
1395     gnus-newsgroup-data gnus-newsgroup-data-reverse
1396     gnus-newsgroup-limit gnus-newsgroup-limits
1397     gnus-newsgroup-charset gnus-newsgroup-display
1398     gnus-newsgroup-incorporated)
1399   "Variables that are buffer-local to the summary buffers.")
1400
1401 (defvar gnus-newsgroup-variables nil
1402   "A list of variables that have separate values in different newsgroups.
1403 A list of newsgroup (summary buffer) local variables, or cons of
1404 variables and their default values (when the default values are not
1405 nil), that should be made global while the summary buffer is active.
1406 These variables can be used to set variables in the group parameters
1407 while still allowing them to affect operations done in other
1408 buffers. For example:
1409
1410 \(setq gnus-newsgroup-variables
1411      '(message-use-followup-to
1412        (gnus-visible-headers .
1413          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1414 ")
1415
1416 ;; Byte-compiler warning.
1417 ;(eval-when-compile (defvar gnus-article-mode-map))
1418 (eval-when-compile
1419   (let ((features (cons 'gnus-sum features)))
1420     (require 'gnus)
1421     (require 'gnus-agent)
1422     (require 'gnus-art)))
1423
1424 ;; Subject simplification.
1425
1426 (defun gnus-simplify-whitespace (str)
1427   "Remove excessive whitespace from STR."
1428   ;; Multiple spaces.
1429   (while (string-match "[ \t][ \t]+" str)
1430     (setq str (concat (substring str 0 (match-beginning 0))
1431                         " "
1432                         (substring str (match-end 0)))))
1433   ;; Leading spaces.
1434   (when (string-match "^[ \t]+" str)
1435     (setq str (substring str (match-end 0))))
1436   ;; Trailing spaces.
1437   (when (string-match "[ \t]+$" str)
1438     (setq str (substring str 0 (match-beginning 0))))
1439   str)
1440
1441 (defun gnus-simplify-all-whitespace (str)
1442   "Remove all whitespace from STR."
1443   (while (string-match "[ \t\n]+" str)
1444     (setq str (replace-match "" nil nil str)))
1445   str)
1446
1447 (defsubst gnus-simplify-subject-re (subject)
1448   "Remove \"Re:\" from subject lines."
1449   (if (string-match message-subject-re-regexp subject)
1450       (substring subject (match-end 0))
1451     subject))
1452
1453 (defun gnus-simplify-subject (subject &optional re-only)
1454   "Remove `Re:' and words in parentheses.
1455 If RE-ONLY is non-nil, strip leading `Re:'s only."
1456   (let ((case-fold-search t))           ;Ignore case.
1457     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1458     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1459       (setq subject (substring subject (match-end 0))))
1460     ;; Remove uninteresting prefixes.
1461     (when (and (not re-only)
1462                gnus-simplify-ignored-prefixes
1463                (string-match gnus-simplify-ignored-prefixes subject))
1464       (setq subject (substring subject (match-end 0))))
1465     ;; Remove words in parentheses from end.
1466     (unless re-only
1467       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1468         (setq subject (substring subject 0 (match-beginning 0)))))
1469     ;; Return subject string.
1470     subject))
1471
1472 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1473 ;; all whitespace.
1474 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1475   (goto-char (point-min))
1476   (while (re-search-forward regexp nil t)
1477     (replace-match (or newtext ""))))
1478
1479 (defun gnus-simplify-buffer-fuzzy ()
1480   "Simplify string in the buffer fuzzily.
1481 The string in the accessible portion of the current buffer is simplified.
1482 It is assumed to be a single-line subject.
1483 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1484 matter is removed.  Additional things can be deleted by setting
1485 `gnus-simplify-subject-fuzzy-regexp'."
1486   (let ((case-fold-search t)
1487         (modified-tick))
1488     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1489
1490     (while (not (eq modified-tick (buffer-modified-tick)))
1491       (setq modified-tick (buffer-modified-tick))
1492       (cond
1493        ((listp gnus-simplify-subject-fuzzy-regexp)
1494         (mapcar 'gnus-simplify-buffer-fuzzy-step
1495                 gnus-simplify-subject-fuzzy-regexp))
1496        (gnus-simplify-subject-fuzzy-regexp
1497         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1498       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1499       (gnus-simplify-buffer-fuzzy-step
1500        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1501       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1502
1503     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1504     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1505     (gnus-simplify-buffer-fuzzy-step " $")
1506     (gnus-simplify-buffer-fuzzy-step "^ +")))
1507
1508 (defun gnus-simplify-subject-fuzzy (subject)
1509   "Simplify a subject string fuzzily.
1510 See `gnus-simplify-buffer-fuzzy' for details."
1511   (save-excursion
1512     (gnus-set-work-buffer)
1513     (let ((case-fold-search t))
1514       ;; Remove uninteresting prefixes.
1515       (when (and gnus-simplify-ignored-prefixes
1516                  (string-match gnus-simplify-ignored-prefixes subject))
1517         (setq subject (substring subject (match-end 0))))
1518       (insert subject)
1519       (inline (gnus-simplify-buffer-fuzzy))
1520       (buffer-string))))
1521
1522 (defsubst gnus-simplify-subject-fully (subject)
1523   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1524   (cond
1525    (gnus-simplify-subject-functions
1526     (gnus-map-function gnus-simplify-subject-functions subject))
1527    ((null gnus-summary-gather-subject-limit)
1528     (gnus-simplify-subject-re subject))
1529    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1530     (gnus-simplify-subject-fuzzy subject))
1531    ((numberp gnus-summary-gather-subject-limit)
1532     (gnus-limit-string (gnus-simplify-subject-re subject)
1533                        gnus-summary-gather-subject-limit))
1534    (t
1535     subject)))
1536
1537 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1538   "Check whether two subjects are equal.
1539 If optional argument simple-first is t, first argument is already
1540 simplified."
1541   (cond
1542    ((null simple-first)
1543     (equal (gnus-simplify-subject-fully s1)
1544            (gnus-simplify-subject-fully s2)))
1545    (t
1546     (equal s1
1547            (gnus-simplify-subject-fully s2)))))
1548
1549 (defun gnus-summary-bubble-group ()
1550   "Increase the score of the current group.
1551 This is a handy function to add to `gnus-summary-exit-hook' to
1552 increase the score of each group you read."
1553   (gnus-group-add-score gnus-newsgroup-name))
1554
1555 \f
1556 ;;;
1557 ;;; Gnus summary mode
1558 ;;;
1559
1560 (put 'gnus-summary-mode 'mode-class 'special)
1561
1562 (defvar gnus-article-commands-menu)
1563
1564 ;; Non-orthogonal keys
1565
1566 (gnus-define-keys gnus-summary-mode-map
1567   " " gnus-summary-next-page
1568   "\177" gnus-summary-prev-page
1569   [delete] gnus-summary-prev-page
1570   [backspace] gnus-summary-prev-page
1571   "\r" gnus-summary-scroll-up
1572   "\M-\r" gnus-summary-scroll-down
1573   "n" gnus-summary-next-unread-article
1574   "p" gnus-summary-prev-unread-article
1575   "N" gnus-summary-next-article
1576   "P" gnus-summary-prev-article
1577   "\M-\C-n" gnus-summary-next-same-subject
1578   "\M-\C-p" gnus-summary-prev-same-subject
1579   "\M-n" gnus-summary-next-unread-subject
1580   "\M-p" gnus-summary-prev-unread-subject
1581   "." gnus-summary-first-unread-article
1582   "," gnus-summary-best-unread-article
1583   "\M-s" gnus-summary-search-article-forward
1584   "\M-r" gnus-summary-search-article-backward
1585   "<" gnus-summary-beginning-of-article
1586   ">" gnus-summary-end-of-article
1587   "j" gnus-summary-goto-article
1588   "^" gnus-summary-refer-parent-article
1589   "\M-^" gnus-summary-refer-article
1590   "u" gnus-summary-tick-article-forward
1591   "!" gnus-summary-tick-article-forward
1592   "U" gnus-summary-tick-article-backward
1593   "d" gnus-summary-mark-as-read-forward
1594   "D" gnus-summary-mark-as-read-backward
1595   "E" gnus-summary-mark-as-expirable
1596   "\M-u" gnus-summary-clear-mark-forward
1597   "\M-U" gnus-summary-clear-mark-backward
1598   "k" gnus-summary-kill-same-subject-and-select
1599   "\C-k" gnus-summary-kill-same-subject
1600   "\M-\C-k" gnus-summary-kill-thread
1601   "\M-\C-l" gnus-summary-lower-thread
1602   "e" gnus-summary-edit-article
1603   "#" gnus-summary-mark-as-processable
1604   "\M-#" gnus-summary-unmark-as-processable
1605   "\M-\C-t" gnus-summary-toggle-threads
1606   "\M-\C-s" gnus-summary-show-thread
1607   "\M-\C-h" gnus-summary-hide-thread
1608   "\M-\C-f" gnus-summary-next-thread
1609   "\M-\C-b" gnus-summary-prev-thread
1610   [(meta down)] gnus-summary-next-thread
1611   [(meta up)] gnus-summary-prev-thread
1612   "\M-\C-u" gnus-summary-up-thread
1613   "\M-\C-d" gnus-summary-down-thread
1614   "&" gnus-summary-execute-command
1615   "c" gnus-summary-catchup-and-exit
1616   "\C-w" gnus-summary-mark-region-as-read
1617   "\C-t" gnus-summary-toggle-truncation
1618   "?" gnus-summary-mark-as-dormant
1619   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1620   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1621   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1622   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1623   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1624   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1625   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1626   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1627   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1628   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1629   "=" gnus-summary-expand-window
1630   "\C-x\C-s" gnus-summary-reselect-current-group
1631   "\M-g" gnus-summary-rescan-group
1632   "w" gnus-summary-stop-page-breaking
1633   "\C-c\C-r" gnus-summary-caesar-message
1634   "\M-t" gnus-summary-toggle-mime
1635   "f" gnus-summary-followup
1636   "F" gnus-summary-followup-with-original
1637   "C" gnus-summary-cancel-article
1638   "r" gnus-summary-reply
1639   "R" gnus-summary-reply-with-original
1640   "\C-c\C-f" gnus-summary-mail-forward
1641   "o" gnus-summary-save-article
1642   "\C-o" gnus-summary-save-article-mail
1643   "|" gnus-summary-pipe-output
1644   "\M-k" gnus-summary-edit-local-kill
1645   "\M-K" gnus-summary-edit-global-kill
1646   ;; "V" gnus-version
1647   "\C-c\C-d" gnus-summary-describe-group
1648   "q" gnus-summary-exit
1649   "Q" gnus-summary-exit-no-update
1650   "\C-c\C-i" gnus-info-find-node
1651   gnus-mouse-2 gnus-mouse-pick-article
1652   "m" gnus-summary-mail-other-window
1653   "a" gnus-summary-post-news
1654   "i" gnus-summary-news-other-window
1655   "x" gnus-summary-limit-to-unread
1656   "s" gnus-summary-isearch-article
1657   "t" gnus-summary-toggle-header
1658   "g" gnus-summary-show-article
1659   "l" gnus-summary-goto-last-article
1660   "v" gnus-summary-preview-mime-message
1661   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1662   "\C-d" gnus-summary-enter-digest-group
1663   "\M-\C-d" gnus-summary-read-document
1664   "\M-\C-e" gnus-summary-edit-parameters
1665   "\M-\C-a" gnus-summary-customize-parameters
1666   "\C-c\C-b" gnus-bug
1667   "\C-c\C-n" gnus-namazu-search
1668   "*" gnus-cache-enter-article
1669   "\M-*" gnus-cache-remove-article
1670   "\M-&" gnus-summary-universal-argument
1671   "\C-l" gnus-recenter
1672   "I" gnus-summary-increase-score
1673   "L" gnus-summary-lower-score
1674   "\M-i" gnus-symbolic-argument
1675   "h" gnus-summary-select-article-buffer
1676
1677   "V" gnus-summary-score-map
1678   "X" gnus-uu-extract-map
1679   "S" gnus-summary-send-map)
1680
1681   ;; Sort of orthogonal keymap
1682 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1683   "t" gnus-summary-tick-article-forward
1684   "!" gnus-summary-tick-article-forward
1685   "d" gnus-summary-mark-as-read-forward
1686   "r" gnus-summary-mark-as-read-forward
1687   "c" gnus-summary-clear-mark-forward
1688   " " gnus-summary-clear-mark-forward
1689   "e" gnus-summary-mark-as-expirable
1690   "x" gnus-summary-mark-as-expirable
1691   "?" gnus-summary-mark-as-dormant
1692   "b" gnus-summary-set-bookmark
1693   "B" gnus-summary-remove-bookmark
1694   "#" gnus-summary-mark-as-processable
1695   "\M-#" gnus-summary-unmark-as-processable
1696   "S" gnus-summary-limit-include-expunged
1697   "C" gnus-summary-catchup
1698   "H" gnus-summary-catchup-to-here
1699   "h" gnus-summary-catchup-from-here
1700   "\C-c" gnus-summary-catchup-all
1701   "k" gnus-summary-kill-same-subject-and-select
1702   "K" gnus-summary-kill-same-subject
1703   "P" gnus-uu-mark-map)
1704
1705 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1706   "c" gnus-summary-clear-above
1707   "u" gnus-summary-tick-above
1708   "m" gnus-summary-mark-above
1709   "k" gnus-summary-kill-below)
1710
1711 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1712   "/" gnus-summary-limit-to-subject
1713   "n" gnus-summary-limit-to-articles
1714   "w" gnus-summary-pop-limit
1715   "s" gnus-summary-limit-to-subject
1716   "a" gnus-summary-limit-to-author
1717   "u" gnus-summary-limit-to-unread
1718   "m" gnus-summary-limit-to-marks
1719   "M" gnus-summary-limit-exclude-marks
1720   "v" gnus-summary-limit-to-score
1721   "*" gnus-summary-limit-include-cached
1722   "D" gnus-summary-limit-include-dormant
1723   "T" gnus-summary-limit-include-thread
1724   "d" gnus-summary-limit-exclude-dormant
1725   "t" gnus-summary-limit-to-age
1726   "." gnus-summary-limit-to-unseen
1727   "x" gnus-summary-limit-to-extra
1728   "p" gnus-summary-limit-to-display-predicate
1729   "E" gnus-summary-limit-include-expunged
1730   "c" gnus-summary-limit-exclude-childless-dormant
1731   "C" gnus-summary-limit-mark-excluded-as-read
1732   "o" gnus-summary-insert-old-articles
1733   "N" gnus-summary-insert-new-articles)
1734
1735 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1736   "n" gnus-summary-next-unread-article
1737   "p" gnus-summary-prev-unread-article
1738   "N" gnus-summary-next-article
1739   "P" gnus-summary-prev-article
1740   "\C-n" gnus-summary-next-same-subject
1741   "\C-p" gnus-summary-prev-same-subject
1742   "\M-n" gnus-summary-next-unread-subject
1743   "\M-p" gnus-summary-prev-unread-subject
1744   "f" gnus-summary-first-unread-article
1745   "b" gnus-summary-best-unread-article
1746   "j" gnus-summary-goto-article
1747   "g" gnus-summary-goto-subject
1748   "l" gnus-summary-goto-last-article
1749   "o" gnus-summary-pop-article)
1750
1751 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1752   "k" gnus-summary-kill-thread
1753   "l" gnus-summary-lower-thread
1754   "i" gnus-summary-raise-thread
1755   "T" gnus-summary-toggle-threads
1756   "t" gnus-summary-rethread-current
1757   "^" gnus-summary-reparent-thread
1758   "s" gnus-summary-show-thread
1759   "S" gnus-summary-show-all-threads
1760   "h" gnus-summary-hide-thread
1761   "H" gnus-summary-hide-all-threads
1762   "n" gnus-summary-next-thread
1763   "p" gnus-summary-prev-thread
1764   "u" gnus-summary-up-thread
1765   "o" gnus-summary-top-thread
1766   "d" gnus-summary-down-thread
1767   "#" gnus-uu-mark-thread
1768   "\M-#" gnus-uu-unmark-thread)
1769
1770 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1771   "g" gnus-summary-prepare
1772   "c" gnus-summary-insert-cached-articles
1773   "d" gnus-summary-insert-dormant-articles)
1774
1775 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1776   "c" gnus-summary-catchup-and-exit
1777   "C" gnus-summary-catchup-all-and-exit
1778   "E" gnus-summary-exit-no-update
1779   "J" gnus-summary-jump-to-other-group
1780   "Q" gnus-summary-exit
1781   "Z" gnus-summary-exit
1782   "n" gnus-summary-catchup-and-goto-next-group
1783   "R" gnus-summary-reselect-current-group
1784   "G" gnus-summary-rescan-group
1785   "N" gnus-summary-next-group
1786   "s" gnus-summary-save-newsrc
1787   "P" gnus-summary-prev-group)
1788
1789 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1790   " " gnus-summary-next-page
1791   "n" gnus-summary-next-page
1792   "\177" gnus-summary-prev-page
1793   [delete] gnus-summary-prev-page
1794   "p" gnus-summary-prev-page
1795   "\r" gnus-summary-scroll-up
1796   "\M-\r" gnus-summary-scroll-down
1797   "<" gnus-summary-beginning-of-article
1798   ">" gnus-summary-end-of-article
1799   "b" gnus-summary-beginning-of-article
1800   "e" gnus-summary-end-of-article
1801   "^" gnus-summary-refer-parent-article
1802   "r" gnus-summary-refer-parent-article
1803   "D" gnus-summary-enter-digest-group
1804   "R" gnus-summary-refer-references
1805   "T" gnus-summary-refer-thread
1806   "g" gnus-summary-show-article
1807   "s" gnus-summary-isearch-article
1808   "P" gnus-summary-print-article
1809   "M" gnus-mailing-list-insinuate
1810   "t" gnus-article-babel)
1811
1812 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1813   "b" gnus-article-add-buttons
1814   "B" gnus-article-add-buttons-to-head
1815   "o" gnus-article-treat-overstrike
1816   "e" gnus-article-emphasize
1817   "w" gnus-article-fill-cited-article
1818   "Q" gnus-article-fill-long-lines
1819   "C" gnus-article-capitalize-sentences
1820   "c" gnus-article-remove-cr
1821   "Z" gnus-article-decode-HZ
1822   "h" gnus-article-wash-html
1823   "u" gnus-article-unsplit-urls
1824   "f" gnus-article-display-x-face
1825   "l" gnus-summary-stop-page-breaking
1826   "r" gnus-summary-caesar-message
1827   "m" gnus-summary-morse-message
1828   "t" gnus-summary-toggle-header
1829   "g" gnus-treat-smiley
1830   "v" gnus-summary-verbose-headers
1831   "m" gnus-summary-toggle-mime
1832   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1833   "p" gnus-article-verify-x-pgp-sig
1834   "d" gnus-article-treat-dumbquotes)
1835
1836 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1837   ;; mnemonic: deuglif*Y*
1838   "u" gnus-article-outlook-unwrap-lines
1839   "a" gnus-article-outlook-repair-attribution
1840   "c" gnus-article-outlook-rearrange-citation
1841   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1842
1843 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1844   "a" gnus-article-hide
1845   "h" gnus-article-hide-headers
1846   "b" gnus-article-hide-boring-headers
1847   "s" gnus-article-hide-signature
1848   "c" gnus-article-hide-citation
1849   "C" gnus-article-hide-citation-in-followups
1850   "l" gnus-article-hide-list-identifiers
1851   "B" gnus-article-strip-banner
1852   "P" gnus-article-hide-pem
1853   "\C-c" gnus-article-hide-citation-maybe)
1854
1855 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1856   "a" gnus-article-highlight
1857   "h" gnus-article-highlight-headers
1858   "c" gnus-article-highlight-citation
1859   "s" gnus-article-highlight-signature)
1860
1861 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1862   "f" gnus-article-treat-fold-headers
1863   "u" gnus-article-treat-unfold-headers
1864   "n" gnus-article-treat-fold-newsgroups)
1865
1866 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1867   "x" gnus-article-display-x-face
1868   "d" gnus-article-display-face
1869   "s" gnus-treat-smiley
1870   "D" gnus-article-remove-images
1871   "f" gnus-treat-from-picon
1872   "m" gnus-treat-mail-picon
1873   "n" gnus-treat-newsgroups-picon)
1874
1875 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1876   "z" gnus-article-date-ut
1877   "u" gnus-article-date-ut
1878   "l" gnus-article-date-local
1879   "p" gnus-article-date-english
1880   "e" gnus-article-date-lapsed
1881   "o" gnus-article-date-original
1882   "i" gnus-article-date-iso8601
1883   "s" gnus-article-date-user)
1884
1885 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1886   "t" gnus-article-remove-trailing-blank-lines
1887   "l" gnus-article-strip-leading-blank-lines
1888   "m" gnus-article-strip-multiple-blank-lines
1889   "a" gnus-article-strip-blank-lines
1890   "A" gnus-article-strip-all-blank-lines
1891   "s" gnus-article-strip-leading-space
1892   "e" gnus-article-strip-trailing-space
1893   "w" gnus-article-remove-leading-whitespace)
1894
1895 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1896   "v" gnus-version
1897   "f" gnus-summary-fetch-faq
1898   "d" gnus-summary-describe-group
1899   "h" gnus-summary-describe-briefly
1900   "i" gnus-info-find-node
1901   "c" gnus-group-fetch-charter
1902   "C" gnus-group-fetch-control)
1903
1904 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1905   "e" gnus-summary-expire-articles
1906   "\M-\C-e" gnus-summary-expire-articles-now
1907   "\177" gnus-summary-delete-article
1908   [delete] gnus-summary-delete-article
1909   [backspace] gnus-summary-delete-article
1910   "m" gnus-summary-move-article
1911   "r" gnus-summary-respool-article
1912   "w" gnus-summary-edit-article
1913   "c" gnus-summary-copy-article
1914   "B" gnus-summary-crosspost-article
1915   "q" gnus-summary-respool-query
1916   "t" gnus-summary-respool-trace
1917   "i" gnus-summary-import-article
1918   "I" gnus-summary-create-article
1919   "p" gnus-summary-article-posted-p)
1920
1921 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1922   "o" gnus-summary-save-article
1923   "m" gnus-summary-save-article-mail
1924   "F" gnus-summary-write-article-file
1925   "r" gnus-summary-save-article-rmail
1926   "f" gnus-summary-save-article-file
1927   "b" gnus-summary-save-article-body-file
1928   "h" gnus-summary-save-article-folder
1929   "v" gnus-summary-save-article-vm
1930   "p" gnus-summary-pipe-output
1931   "P" gnus-summary-muttprint
1932   "s" gnus-soup-add-article)
1933
1934 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1935   "b" gnus-summary-display-buttonized
1936   "m" gnus-summary-repair-multipart
1937   "v" gnus-article-view-part
1938   "o" gnus-article-save-part
1939   "c" gnus-article-copy-part
1940   "C" gnus-article-view-part-as-charset
1941   "e" gnus-article-view-part-externally
1942   "E" gnus-article-encrypt-body
1943   "i" gnus-article-inline-part
1944   "|" gnus-article-pipe-part)
1945
1946 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1947   "p" gnus-summary-mark-as-processable
1948   "u" gnus-summary-unmark-as-processable
1949   "U" gnus-summary-unmark-all-processable
1950   "v" gnus-uu-mark-over
1951   "s" gnus-uu-mark-series
1952   "r" gnus-uu-mark-region
1953   "g" gnus-uu-unmark-region
1954   "R" gnus-uu-mark-by-regexp
1955   "G" gnus-uu-unmark-by-regexp
1956   "t" gnus-uu-mark-thread
1957   "T" gnus-uu-unmark-thread
1958   "a" gnus-uu-mark-all
1959   "b" gnus-uu-mark-buffer
1960   "S" gnus-uu-mark-sparse
1961   "k" gnus-summary-kill-process-mark
1962   "y" gnus-summary-yank-process-mark
1963   "w" gnus-summary-save-process-mark
1964   "i" gnus-uu-invert-processable)
1965
1966 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1967   ;;"x" gnus-uu-extract-any
1968   "m" gnus-summary-save-parts
1969   "u" gnus-uu-decode-uu
1970   "U" gnus-uu-decode-uu-and-save
1971   "s" gnus-uu-decode-unshar
1972   "S" gnus-uu-decode-unshar-and-save
1973   "o" gnus-uu-decode-save
1974   "O" gnus-uu-decode-save
1975   "b" gnus-uu-decode-binhex
1976   "B" gnus-uu-decode-binhex
1977   "p" gnus-uu-decode-postscript
1978   "P" gnus-uu-decode-postscript-and-save)
1979
1980 (gnus-define-keys
1981     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1982   "u" gnus-uu-decode-uu-view
1983   "U" gnus-uu-decode-uu-and-save-view
1984   "s" gnus-uu-decode-unshar-view
1985   "S" gnus-uu-decode-unshar-and-save-view
1986   "o" gnus-uu-decode-save-view
1987   "O" gnus-uu-decode-save-view
1988   "b" gnus-uu-decode-binhex-view
1989   "B" gnus-uu-decode-binhex-view
1990   "p" gnus-uu-decode-postscript-view
1991   "P" gnus-uu-decode-postscript-and-save-view)
1992
1993 (defvar gnus-article-post-menu nil)
1994
1995 (defconst gnus-summary-menu-maxlen 20)
1996
1997 (defun gnus-summary-menu-split (menu)
1998   ;; If we have lots of elements, divide them into groups of 20
1999   ;; and make a pane (or submenu) for each one.
2000   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2001       (let ((menu menu) sublists next
2002             (i 1))
2003         (while menu
2004           ;; Pull off the next gnus-summary-menu-maxlen elements
2005           ;; and make them the next element of sublist.
2006           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2007           (if next
2008               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2009                       nil))
2010           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2011                                              (aref (car (last menu)) 0)) menu)
2012                                sublists))
2013           (setq i (1+ i))
2014           (setq menu next))
2015         (nreverse sublists))
2016     ;; Few elements--put them all in one pane.
2017     menu))
2018
2019 (defun gnus-summary-make-menu-bar ()
2020   (gnus-turn-off-edit-menu 'summary)
2021
2022   (unless (boundp 'gnus-summary-misc-menu)
2023
2024     (easy-menu-define
2025      gnus-summary-kill-menu gnus-summary-mode-map ""
2026      (cons
2027       "Score"
2028       (nconc
2029        (list
2030         ["Customize" gnus-score-customize t])
2031        (gnus-make-score-map 'increase)
2032        (gnus-make-score-map 'lower)
2033        '(("Mark"
2034           ["Kill below" gnus-summary-kill-below t]
2035           ["Mark above" gnus-summary-mark-above t]
2036           ["Tick above" gnus-summary-tick-above t]
2037           ["Clear above" gnus-summary-clear-above t])
2038          ["Current score" gnus-summary-current-score t]
2039          ["Set score" gnus-summary-set-score t]
2040          ["Switch current score file..." gnus-score-change-score-file t]
2041          ["Set mark below..." gnus-score-set-mark-below t]
2042          ["Set expunge below..." gnus-score-set-expunge-below t]
2043          ["Edit current score file" gnus-score-edit-current-scores t]
2044          ["Edit score file" gnus-score-edit-file t]
2045          ["Trace score" gnus-score-find-trace t]
2046          ["Find words" gnus-score-find-favourite-words t]
2047          ["Rescore buffer" gnus-summary-rescore t]
2048          ["Increase score..." gnus-summary-increase-score t]
2049          ["Lower score..." gnus-summary-lower-score t]))))
2050
2051     ;; Define both the Article menu in the summary buffer and the
2052     ;; equivalent Commands menu in the article buffer here for
2053     ;; consistency.
2054     (let ((innards
2055            `(("Hide"
2056               ["All" gnus-article-hide t]
2057               ["Headers" gnus-article-hide-headers t]
2058               ["Signature" gnus-article-hide-signature t]
2059               ["Citation" gnus-article-hide-citation t]
2060               ["List identifiers" gnus-article-hide-list-identifiers t]
2061               ["Banner" gnus-article-strip-banner t]
2062               ["Boring headers" gnus-article-hide-boring-headers t])
2063              ("Highlight"
2064               ["All" gnus-article-highlight t]
2065               ["Headers" gnus-article-highlight-headers t]
2066               ["Signature" gnus-article-highlight-signature t]
2067               ["Citation" gnus-article-highlight-citation t])
2068              ("Date"
2069               ["Local" gnus-article-date-local t]
2070               ["ISO8601" gnus-article-date-iso8601 t]
2071               ["UT" gnus-article-date-ut t]
2072               ["Original" gnus-article-date-original t]
2073               ["Lapsed" gnus-article-date-lapsed t]
2074               ["User-defined" gnus-article-date-user t])
2075              ("Display"
2076               ["Remove images" gnus-article-remove-images t]
2077               ["Toggle smiley" gnus-treat-smiley t]
2078               ["Show X-Face" gnus-article-display-x-face t]
2079               ["Show picons in From" gnus-treat-from-picon t]
2080               ["Show picons in mail headers" gnus-treat-mail-picon t]
2081               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2082               ("View as different encoding"
2083                ,@(gnus-summary-menu-split
2084                   (mapcar
2085                    (lambda (cs)
2086                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2087                      ;; forms for menu commands, we should provide intern'ed
2088                      ;; function symbols.
2089                      (let ((command (intern (format "\
2090 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2091                        (fset command
2092                              `(lambda ()
2093                                 (interactive)
2094                                 (let ((gnus-summary-show-article-charset-alist
2095                                        '((1 . ,cs))))
2096                                   (gnus-summary-show-article 1))))
2097                        `[,(symbol-name cs) ,command t]))
2098                    (sort (if (fboundp 'coding-system-list)
2099                              (coding-system-list)
2100                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2101                            )
2102                          'string<)))))
2103              ("Washing"
2104               ("Remove Blanks"
2105                ["Leading" gnus-article-strip-leading-blank-lines t]
2106                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2107                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2108                ["All of the above" gnus-article-strip-blank-lines t]
2109                ["All" gnus-article-strip-all-blank-lines t]
2110                ["Leading space" gnus-article-strip-leading-space t]
2111                ["Trailing space" gnus-article-strip-trailing-space t]
2112                ["Leading space in headers"
2113                 gnus-article-remove-leading-whitespace t])
2114               ["Overstrike" gnus-article-treat-overstrike t]
2115               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2116               ["Emphasis" gnus-article-emphasize t]
2117               ["Word wrap" gnus-article-fill-cited-article t]
2118               ["Fill long lines" gnus-article-fill-long-lines t]
2119               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2120               ["CR" gnus-article-remove-cr t]
2121               ["Rot 13" gnus-summary-caesar-message
2122                ,@(if (featurep 'xemacs) '(t)
2123                    '(:help "\"Caesar rotate\" article by 13"))]
2124               ["Morse decode" gnus-summary-morse-message t]
2125               ["Unix pipe..." gnus-summary-pipe-message t]
2126               ["Add buttons" gnus-article-add-buttons t]
2127               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2128               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2129               ["Toggle MIME" gnus-summary-toggle-mime t]
2130               ["Verbose header" gnus-summary-verbose-headers t]
2131               ["Toggle header" gnus-summary-toggle-header t]
2132               ["Unfold headers" gnus-article-treat-unfold-headers t]
2133               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2134               ["Html" gnus-article-wash-html t]
2135               ["URLs" gnus-article-unsplit-urls t]
2136               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2137               ["HZ" gnus-article-decode-HZ t]
2138               ("(Outlook) Deuglify"
2139                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2140                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2141                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2142                ["Full (Outlook) deuglify"
2143                 gnus-article-outlook-deuglify-article t])
2144               )
2145              ("Output"
2146               ["Save in default format" gnus-summary-save-article
2147                ,@(if (featurep 'xemacs) '(t)
2148                    '(:help "Save article using default method"))]
2149               ["Save in file" gnus-summary-save-article-file
2150                ,@(if (featurep 'xemacs) '(t)
2151                    '(:help "Save article in file"))]
2152               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2153               ["Save in MH folder" gnus-summary-save-article-folder t]
2154               ["Save in VM folder" gnus-summary-save-article-vm t]
2155               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2156               ["Save body in file" gnus-summary-save-article-body-file t]
2157               ["Pipe through a filter" gnus-summary-pipe-output t]
2158               ["Add to SOUP packet" gnus-soup-add-article t]
2159               ["Print with Muttprint" gnus-summary-muttprint t]
2160               ["Print" gnus-summary-print-article t])
2161              ("Backend"
2162               ["Respool article..." gnus-summary-respool-article t]
2163               ["Move article..." gnus-summary-move-article
2164                (gnus-check-backend-function
2165                 'request-move-article gnus-newsgroup-name)]
2166               ["Copy article..." gnus-summary-copy-article t]
2167               ["Crosspost article..." gnus-summary-crosspost-article
2168                (gnus-check-backend-function
2169                 'request-replace-article gnus-newsgroup-name)]
2170               ["Import file..." gnus-summary-import-article t]
2171               ["Create article..." gnus-summary-create-article t]
2172               ["Check if posted" gnus-summary-article-posted-p t]
2173               ["Edit article" gnus-summary-edit-article
2174                (not (gnus-group-read-only-p))]
2175               ["Delete article" gnus-summary-delete-article
2176                (gnus-check-backend-function
2177                 'request-expire-articles gnus-newsgroup-name)]
2178               ["Query respool" gnus-summary-respool-query t]
2179               ["Trace respool" gnus-summary-respool-trace t]
2180               ["Delete expirable articles" gnus-summary-expire-articles-now
2181                (gnus-check-backend-function
2182                 'request-expire-articles gnus-newsgroup-name)])
2183              ("Extract"
2184               ["Uudecode" gnus-uu-decode-uu
2185                ,@(if (featurep 'xemacs) '(t)
2186                    '(:help "Decode uuencoded article(s)"))]
2187               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2188               ["Unshar" gnus-uu-decode-unshar t]
2189               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2190               ["Save" gnus-uu-decode-save t]
2191               ["Binhex" gnus-uu-decode-binhex t]
2192               ["Postscript" gnus-uu-decode-postscript t])
2193              ("Cache"
2194               ["Enter article" gnus-cache-enter-article t]
2195               ["Remove article" gnus-cache-remove-article t])
2196              ["Translate" gnus-article-babel t]
2197              ["Select article buffer" gnus-summary-select-article-buffer t]
2198              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2199              ["Isearch article..." gnus-summary-isearch-article t]
2200              ["Beginning of the article" gnus-summary-beginning-of-article t]
2201              ["End of the article" gnus-summary-end-of-article t]
2202              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2203              ["Fetch referenced articles" gnus-summary-refer-references t]
2204              ["Fetch current thread" gnus-summary-refer-thread t]
2205              ["Fetch article with id..." gnus-summary-refer-article t]
2206              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2207              ["Redisplay" gnus-summary-show-article t]
2208              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2209       (easy-menu-define
2210        gnus-summary-article-menu gnus-summary-mode-map ""
2211        (cons "Article" innards))
2212
2213       (if (not (keymapp gnus-summary-article-menu))
2214           (easy-menu-define
2215            gnus-article-commands-menu gnus-article-mode-map ""
2216            (cons "Commands" innards))
2217         ;; in Emacs, don't share menu.
2218         (setq gnus-article-commands-menu
2219               (copy-keymap gnus-summary-article-menu))
2220         (define-key gnus-article-mode-map [menu-bar commands]
2221           (cons "Commands" gnus-article-commands-menu))))
2222
2223     (easy-menu-define
2224      gnus-summary-thread-menu gnus-summary-mode-map ""
2225      '("Threads"
2226        ["Find all messages in thread" gnus-summary-refer-thread t]
2227        ["Toggle threading" gnus-summary-toggle-threads t]
2228        ["Hide threads" gnus-summary-hide-all-threads t]
2229        ["Show threads" gnus-summary-show-all-threads t]
2230        ["Hide thread" gnus-summary-hide-thread t]
2231        ["Show thread" gnus-summary-show-thread t]
2232        ["Go to next thread" gnus-summary-next-thread t]
2233        ["Go to previous thread" gnus-summary-prev-thread t]
2234        ["Go down thread" gnus-summary-down-thread t]
2235        ["Go up thread" gnus-summary-up-thread t]
2236        ["Top of thread" gnus-summary-top-thread t]
2237        ["Mark thread as read" gnus-summary-kill-thread t]
2238        ["Lower thread score" gnus-summary-lower-thread t]
2239        ["Raise thread score" gnus-summary-raise-thread t]
2240        ["Rethread current" gnus-summary-rethread-current t]))
2241
2242     (easy-menu-define
2243      gnus-summary-post-menu gnus-summary-mode-map ""
2244      `("Post"
2245        ["Send a message (mail or news)" gnus-summary-post-news
2246         ,@(if (featurep 'xemacs) '(t)
2247             '(:help "Post an article"))]
2248        ["Followup" gnus-summary-followup
2249         ,@(if (featurep 'xemacs) '(t)
2250             '(:help "Post followup to this article"))]
2251        ["Followup and yank" gnus-summary-followup-with-original
2252         ,@(if (featurep 'xemacs) '(t)
2253             '(:help "Post followup to this article, quoting its contents"))]
2254        ["Supersede article" gnus-summary-supersede-article t]
2255        ["Cancel article" gnus-summary-cancel-article
2256         ,@(if (featurep 'xemacs) '(t)
2257             '(:help "Cancel an article you posted"))]
2258        ["Reply" gnus-summary-reply t]
2259        ["Reply and yank" gnus-summary-reply-with-original t]
2260        ["Wide reply" gnus-summary-wide-reply t]
2261        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2262         ,@(if (featurep 'xemacs) '(t)
2263             '(:help "Mail a reply, quoting this article"))]
2264        ["Very wide reply" gnus-summary-very-wide-reply t]
2265        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2266         ,@(if (featurep 'xemacs) '(t)
2267             '(:help "Mail a very wide reply, quoting this article"))]
2268        ["Mail forward" gnus-summary-mail-forward t]
2269        ["Post forward" gnus-summary-post-forward t]
2270        ["Digest and mail" gnus-summary-digest-mail-forward t]
2271        ["Digest and post" gnus-summary-digest-post-forward t]
2272        ["Resend message" gnus-summary-resend-message t]
2273        ["Resend message edit" gnus-summary-resend-message-edit t]
2274        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2275        ["Send a mail" gnus-summary-mail-other-window t]
2276        ["Create a local message" gnus-summary-news-other-window t]
2277        ["Uuencode and post" gnus-uu-post-news
2278         ,@(if (featurep 'xemacs) '(t)
2279             '(:help "Post a uuencoded article"))]
2280        ["Followup via news" gnus-summary-followup-to-mail t]
2281        ["Followup via news and yank"
2282         gnus-summary-followup-to-mail-with-original t]
2283        ;;("Draft"
2284        ;;["Send" gnus-summary-send-draft t]
2285        ;;["Send bounced" gnus-resend-bounced-mail t])
2286        ))
2287
2288     (cond
2289      ((not (keymapp gnus-summary-post-menu))
2290       (setq gnus-article-post-menu gnus-summary-post-menu))
2291      ((not gnus-article-post-menu)
2292       ;; Don't share post menu.
2293       (setq gnus-article-post-menu
2294             (copy-keymap gnus-summary-post-menu))))
2295     (define-key gnus-article-mode-map [menu-bar post]
2296       (cons "Post" gnus-article-post-menu))
2297
2298     (easy-menu-define
2299      gnus-summary-misc-menu gnus-summary-mode-map ""
2300      `("Gnus"
2301        ("Mark Read"
2302         ["Mark as read" gnus-summary-mark-as-read-forward t]
2303         ["Mark same subject and select"
2304          gnus-summary-kill-same-subject-and-select t]
2305         ["Mark same subject" gnus-summary-kill-same-subject t]
2306         ["Catchup" gnus-summary-catchup
2307          ,@(if (featurep 'xemacs) '(t)
2308              '(:help "Mark unread articles in this group as read"))]
2309         ["Catchup all" gnus-summary-catchup-all t]
2310         ["Catchup to here" gnus-summary-catchup-to-here t]
2311         ["Catchup from here" gnus-summary-catchup-from-here t]
2312         ["Catchup region" gnus-summary-mark-region-as-read
2313          (gnus-mark-active-p)]
2314         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2315        ("Mark Various"
2316         ["Tick" gnus-summary-tick-article-forward t]
2317         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2318         ["Remove marks" gnus-summary-clear-mark-forward t]
2319         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2320         ["Set bookmark" gnus-summary-set-bookmark t]
2321         ["Remove bookmark" gnus-summary-remove-bookmark t])
2322        ("Limit to"
2323         ["Marks..." gnus-summary-limit-to-marks t]
2324         ["Subject..." gnus-summary-limit-to-subject t]
2325         ["Author..." gnus-summary-limit-to-author t]
2326         ["Age..." gnus-summary-limit-to-age t]
2327         ["Extra..." gnus-summary-limit-to-extra t]
2328         ["Score..." gnus-summary-limit-to-score t]
2329         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2330         ["Unread" gnus-summary-limit-to-unread t]
2331         ["Unseen" gnus-summary-limit-to-unseen t]
2332         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2333         ["Next articles" gnus-summary-limit-to-articles t]
2334         ["Pop limit" gnus-summary-pop-limit t]
2335         ["Show dormant" gnus-summary-limit-include-dormant t]
2336         ["Hide childless dormant"
2337          gnus-summary-limit-exclude-childless-dormant t]
2338         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2339         ["Hide marked" gnus-summary-limit-exclude-marks t]
2340         ["Show expunged" gnus-summary-limit-include-expunged t])
2341        ("Process Mark"
2342         ["Set mark" gnus-summary-mark-as-processable t]
2343         ["Remove mark" gnus-summary-unmark-as-processable t]
2344         ["Remove all marks" gnus-summary-unmark-all-processable t]
2345         ["Mark above" gnus-uu-mark-over t]
2346         ["Mark series" gnus-uu-mark-series t]
2347         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2348         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2349         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2350         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2351         ["Mark all" gnus-uu-mark-all t]
2352         ["Mark buffer" gnus-uu-mark-buffer t]
2353         ["Mark sparse" gnus-uu-mark-sparse t]
2354         ["Mark thread" gnus-uu-mark-thread t]
2355         ["Unmark thread" gnus-uu-unmark-thread t]
2356         ("Process Mark Sets"
2357          ["Kill" gnus-summary-kill-process-mark t]
2358          ["Yank" gnus-summary-yank-process-mark
2359           gnus-newsgroup-process-stack]
2360          ["Save" gnus-summary-save-process-mark t]
2361          ["Run command on marked..." gnus-summary-universal-argument t]))
2362        ("Scroll article"
2363         ["Page forward" gnus-summary-next-page
2364          ,@(if (featurep 'xemacs) '(t)
2365              '(:help "Show next page of article"))]
2366         ["Page backward" gnus-summary-prev-page
2367          ,@(if (featurep 'xemacs) '(t)
2368              '(:help "Show previous page of article"))]
2369         ["Line forward" gnus-summary-scroll-up t])
2370        ("Move"
2371         ["Next unread article" gnus-summary-next-unread-article t]
2372         ["Previous unread article" gnus-summary-prev-unread-article t]
2373         ["Next article" gnus-summary-next-article t]
2374         ["Previous article" gnus-summary-prev-article t]
2375         ["Next unread subject" gnus-summary-next-unread-subject t]
2376         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2377         ["Next article same subject" gnus-summary-next-same-subject t]
2378         ["Previous article same subject" gnus-summary-prev-same-subject t]
2379         ["First unread article" gnus-summary-first-unread-article t]
2380         ["Best unread article" gnus-summary-best-unread-article t]
2381         ["Go to subject number..." gnus-summary-goto-subject t]
2382         ["Go to article number..." gnus-summary-goto-article t]
2383         ["Go to the last article" gnus-summary-goto-last-article t]
2384         ["Pop article off history" gnus-summary-pop-article t])
2385        ("Sort"
2386         ["Sort by number" gnus-summary-sort-by-number t]
2387         ["Sort by author" gnus-summary-sort-by-author t]
2388         ["Sort by subject" gnus-summary-sort-by-subject t]
2389         ["Sort by date" gnus-summary-sort-by-date t]
2390         ["Sort by score" gnus-summary-sort-by-score t]
2391         ["Sort by lines" gnus-summary-sort-by-lines t]
2392         ["Sort by characters" gnus-summary-sort-by-chars t]
2393         ["Randomize" gnus-summary-sort-by-random t]
2394         ["Original sort" gnus-summary-sort-by-original t])
2395        ("Help"
2396         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2397         ["Describe group" gnus-summary-describe-group t]
2398         ["Fetch charter" gnus-group-fetch-charter
2399          ,@(if (featurep 'xemacs) nil
2400              '(:help "Display the charter of the current group"))]
2401         ["Fetch control message" gnus-group-fetch-control
2402          ,@(if (featurep 'xemacs) nil
2403              '(:help "Display the archived control message for the current group"))]
2404         ["Read manual" gnus-info-find-node t])
2405        ("Modes"
2406         ["Pick and read" gnus-pick-mode t]
2407         ["Binary" gnus-binary-mode t])
2408        ("Regeneration"
2409         ["Regenerate" gnus-summary-prepare t]
2410         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2411         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2412         ["Toggle threading" gnus-summary-toggle-threads t])
2413        ["See old articles" gnus-summary-insert-old-articles t]
2414        ["See new articles" gnus-summary-insert-new-articles t]
2415        ["Filter articles..." gnus-summary-execute-command t]
2416        ["Run command on articles..." gnus-summary-universal-argument t]
2417        ["Search articles forward..." gnus-summary-search-article-forward t]
2418        ["Search articles backward..." gnus-summary-search-article-backward t]
2419        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2420        ["Expand window" gnus-summary-expand-window t]
2421        ["Expire expirable articles" gnus-summary-expire-articles
2422         (gnus-check-backend-function
2423          'request-expire-articles gnus-newsgroup-name)]
2424        ["Edit local kill file" gnus-summary-edit-local-kill t]
2425        ["Edit main kill file" gnus-summary-edit-global-kill t]
2426        ["Edit group parameters" gnus-summary-edit-parameters t]
2427        ["Customize group parameters" gnus-summary-customize-parameters t]
2428        ["Send a bug report" gnus-bug t]
2429        ("Exit"
2430         ["Catchup and exit" gnus-summary-catchup-and-exit
2431          ,@(if (featurep 'xemacs) '(t)
2432              '(:help "Mark unread articles in this group as read, then exit"))]
2433         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2434         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2435         ["Exit group" gnus-summary-exit
2436          ,@(if (featurep 'xemacs) '(t)
2437              '(:help "Exit current group, return to group selection mode"))]
2438         ["Exit group without updating" gnus-summary-exit-no-update t]
2439         ["Exit and goto next group" gnus-summary-next-group t]
2440         ["Exit and goto prev group" gnus-summary-prev-group t]
2441         ["Reselect group" gnus-summary-reselect-current-group t]
2442         ["Rescan group" gnus-summary-rescan-group t]
2443         ["Update dribble" gnus-summary-save-newsrc t])))
2444
2445     (gnus-run-hooks 'gnus-summary-menu-hook)))
2446
2447 (defvar gnus-summary-tool-bar-map nil)
2448
2449 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2450 (defun gnus-summary-make-tool-bar ()
2451   (if (and (fboundp 'tool-bar-add-item-from-menu)
2452            (default-value 'tool-bar-mode)
2453            (not gnus-summary-tool-bar-map))
2454       (setq gnus-summary-tool-bar-map
2455             (let ((tool-bar-map (make-sparse-keymap))
2456                   (load-path (mm-image-load-path)))
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-post-news "post" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-followup "followup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-reply "reply" gnus-summary-mode-map)
2471               (tool-bar-add-item-from-menu
2472                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2473               (tool-bar-add-item-from-menu
2474                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2475               (tool-bar-add-item-from-menu
2476                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2477               (tool-bar-add-item-from-menu
2478                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2479               (tool-bar-add-item-from-menu
2480                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2481               (tool-bar-add-item-from-menu
2482                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2483               (tool-bar-add-item-from-menu
2484                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2485               (tool-bar-add-item-from-menu
2486                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2487               tool-bar-map)))
2488   (if gnus-summary-tool-bar-map
2489       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2490
2491 (defun gnus-score-set-default (var value)
2492   "A version of set that updates the GNU Emacs menu-bar."
2493   (set var value)
2494   ;; It is the message that forces the active status to be updated.
2495   (message ""))
2496
2497 (defun gnus-make-score-map (type)
2498   "Make a summary score map of type TYPE."
2499   (if t
2500       nil
2501     (let ((headers '(("author" "from" string)
2502                      ("subject" "subject" string)
2503                      ("article body" "body" string)
2504                      ("article head" "head" string)
2505                      ("xref" "xref" string)
2506                      ("extra header" "extra" string)
2507                      ("lines" "lines" number)
2508                      ("followups to author" "followup" string)))
2509           (types '((number ("less than" <)
2510                            ("greater than" >)
2511                            ("equal" =))
2512                    (string ("substring" s)
2513                            ("exact string" e)
2514                            ("fuzzy string" f)
2515                            ("regexp" r))))
2516           (perms '(("temporary" (current-time-string))
2517                    ("permanent" nil)
2518                    ("immediate" now)))
2519           header)
2520       (list
2521        (apply
2522         'nconc
2523         (list
2524          (if (eq type 'lower)
2525              "Lower score"
2526            "Increase score"))
2527         (let (outh)
2528           (while headers
2529             (setq header (car headers))
2530             (setq outh
2531                   (cons
2532                    (apply
2533                     'nconc
2534                     (list (car header))
2535                     (let ((ts (cdr (assoc (nth 2 header) types)))
2536                           outt)
2537                       (while ts
2538                         (setq outt
2539                               (cons
2540                                (apply
2541                                 'nconc
2542                                 (list (caar ts))
2543                                 (let ((ps perms)
2544                                       outp)
2545                                   (while ps
2546                                     (setq outp
2547                                           (cons
2548                                            (vector
2549                                             (caar ps)
2550                                             (list
2551                                              'gnus-summary-score-entry
2552                                              (nth 1 header)
2553                                              (if (or (string= (nth 1 header)
2554                                                               "head")
2555                                                      (string= (nth 1 header)
2556                                                               "body"))
2557                                                  ""
2558                                                (list 'gnus-summary-header
2559                                                      (nth 1 header)))
2560                                              (list 'quote (nth 1 (car ts)))
2561                                              (list 'gnus-score-delta-default
2562                                                    nil)
2563                                              (nth 1 (car ps))
2564                                              t)
2565                                             t)
2566                                            outp))
2567                                     (setq ps (cdr ps)))
2568                                   (list (nreverse outp))))
2569                                outt))
2570                         (setq ts (cdr ts)))
2571                       (list (nreverse outt))))
2572                    outh))
2573             (setq headers (cdr headers)))
2574           (list (nreverse outh))))))))
2575
2576 \f
2577
2578 (defun gnus-summary-mode (&optional group)
2579   "Major mode for reading articles.
2580
2581 All normal editing commands are switched off.
2582 \\<gnus-summary-mode-map>
2583 Each line in this buffer represents one article.  To read an
2584 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2585 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2586 respectively.
2587
2588 You can also post articles and send mail from this buffer.  To
2589 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2590 of an article, type `\\[gnus-summary-reply]'.
2591
2592 There are approx. one gazillion commands you can execute in this
2593 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2594
2595 The following commands are available:
2596
2597 \\{gnus-summary-mode-map}"
2598   (interactive)
2599   (kill-all-local-variables)
2600   (when (gnus-visual-p 'summary-menu 'menu)
2601     (gnus-summary-make-menu-bar)
2602     (gnus-summary-make-tool-bar))
2603   (gnus-summary-make-local-variables)
2604   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2605     (gnus-summary-make-local-variables))
2606   (gnus-make-thread-indent-array)
2607   (gnus-simplify-mode-line)
2608   (setq major-mode 'gnus-summary-mode)
2609   (setq mode-name "Summary")
2610   (make-local-variable 'minor-mode-alist)
2611   (use-local-map gnus-summary-mode-map)
2612   (buffer-disable-undo)
2613   (setq buffer-read-only t)             ;Disable modification
2614   (setq truncate-lines t)
2615   (setq selective-display t)
2616   (setq selective-display-ellipses t)   ;Display `...'
2617   (gnus-summary-set-display-table)
2618   (gnus-set-default-directory)
2619   (setq gnus-newsgroup-name group)
2620   (unless (gnus-news-group-p group)
2621     (setq gnus-newsgroup-incorporated
2622           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2623   (make-local-variable 'gnus-summary-line-format)
2624   (make-local-variable 'gnus-summary-line-format-spec)
2625   (make-local-variable 'gnus-summary-dummy-line-format)
2626   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2627   (make-local-variable 'gnus-summary-mark-positions)
2628   (make-local-hook 'pre-command-hook)
2629   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2630   (gnus-run-hooks 'gnus-summary-mode-hook)
2631   (turn-on-gnus-mailing-list-mode)
2632   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2633   (gnus-update-summary-mark-positions))
2634
2635 (defun gnus-summary-make-local-variables ()
2636   "Make all the local summary buffer variables."
2637   (let (global)
2638     (dolist (local gnus-summary-local-variables)
2639       (if (consp local)
2640           (progn
2641             (if (eq (cdr local) 'global)
2642                 ;; Copy the global value of the variable.
2643                 (setq global (symbol-value (car local)))
2644               ;; Use the value from the list.
2645               (setq global (eval (cdr local))))
2646             (set (make-local-variable (car local)) global))
2647         ;; Simple nil-valued local variable.
2648         (set (make-local-variable local) nil)))))
2649
2650 (defun gnus-summary-clear-local-variables ()
2651   (let ((locals gnus-summary-local-variables))
2652     (while locals
2653       (if (consp (car locals))
2654           (and (vectorp (caar locals))
2655                (set (caar locals) nil))
2656         (and (vectorp (car locals))
2657              (set (car locals) nil)))
2658       (setq locals (cdr locals)))))
2659
2660 ;; Summary data functions.
2661
2662 (defmacro gnus-data-number (data)
2663   `(car ,data))
2664
2665 (defmacro gnus-data-set-number (data number)
2666   `(setcar ,data ,number))
2667
2668 (defmacro gnus-data-mark (data)
2669   `(nth 1 ,data))
2670
2671 (defmacro gnus-data-set-mark (data mark)
2672   `(setcar (nthcdr 1 ,data) ,mark))
2673
2674 (defmacro gnus-data-pos (data)
2675   `(nth 2 ,data))
2676
2677 (defmacro gnus-data-set-pos (data pos)
2678   `(setcar (nthcdr 2 ,data) ,pos))
2679
2680 (defmacro gnus-data-header (data)
2681   `(nth 3 ,data))
2682
2683 (defmacro gnus-data-set-header (data header)
2684   `(setcar (nthcdr 3 ,data) ,header))
2685
2686 (defmacro gnus-data-level (data)
2687   `(nth 4 ,data))
2688
2689 (defmacro gnus-data-unread-p (data)
2690   `(= (nth 1 ,data) gnus-unread-mark))
2691
2692 (defmacro gnus-data-read-p (data)
2693   `(/= (nth 1 ,data) gnus-unread-mark))
2694
2695 (defmacro gnus-data-pseudo-p (data)
2696   `(consp (nth 3 ,data)))
2697
2698 (defmacro gnus-data-find (number)
2699   `(assq ,number gnus-newsgroup-data))
2700
2701 (defmacro gnus-data-find-list (number &optional data)
2702   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2703      (memq (assq ,number bdata)
2704            bdata)))
2705
2706 (defmacro gnus-data-make (number mark pos header level)
2707   `(list ,number ,mark ,pos ,header ,level))
2708
2709 (defun gnus-data-enter (after-article number mark pos header level offset)
2710   (let ((data (gnus-data-find-list after-article)))
2711     (unless data
2712       (error "No such article: %d" after-article))
2713     (setcdr data (cons (gnus-data-make number mark pos header level)
2714                        (cdr data)))
2715     (setq gnus-newsgroup-data-reverse nil)
2716     (gnus-data-update-list (cddr data) offset)))
2717
2718 (defun gnus-data-enter-list (after-article list &optional offset)
2719   (when list
2720     (let ((data (and after-article (gnus-data-find-list after-article)))
2721           (ilist list))
2722       (if (not (or data
2723                    after-article))
2724           (let ((odata gnus-newsgroup-data))
2725             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2726             (when offset
2727               (gnus-data-update-list odata offset)))
2728         ;; Find the last element in the list to be spliced into the main
2729         ;; list.
2730         (while (cdr list)
2731           (setq list (cdr list)))
2732         (if (not data)
2733             (progn
2734               (setcdr list gnus-newsgroup-data)
2735               (setq gnus-newsgroup-data ilist)
2736               (when offset
2737                 (gnus-data-update-list (cdr list) offset)))
2738           (setcdr list (cdr data))
2739           (setcdr data ilist)
2740           (when offset
2741             (gnus-data-update-list (cdr list) offset))))
2742       (setq gnus-newsgroup-data-reverse nil))))
2743
2744 (defun gnus-data-remove (article &optional offset)
2745   (let ((data gnus-newsgroup-data))
2746     (if (= (gnus-data-number (car data)) article)
2747         (progn
2748           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2749                 gnus-newsgroup-data-reverse nil)
2750           (when offset
2751             (gnus-data-update-list gnus-newsgroup-data offset)))
2752       (while (cdr data)
2753         (when (= (gnus-data-number (cadr data)) article)
2754           (setcdr data (cddr data))
2755           (when offset
2756             (gnus-data-update-list (cdr data) offset))
2757           (setq data nil
2758                 gnus-newsgroup-data-reverse nil))
2759         (setq data (cdr data))))))
2760
2761 (defmacro gnus-data-list (backward)
2762   `(if ,backward
2763        (or gnus-newsgroup-data-reverse
2764            (setq gnus-newsgroup-data-reverse
2765                  (reverse gnus-newsgroup-data)))
2766      gnus-newsgroup-data))
2767
2768 (defun gnus-data-update-list (data offset)
2769   "Add OFFSET to the POS of all data entries in DATA."
2770   (setq gnus-newsgroup-data-reverse nil)
2771   (while data
2772     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2773     (setq data (cdr data))))
2774
2775 (defun gnus-summary-article-pseudo-p (article)
2776   "Say whether this article is a pseudo article or not."
2777   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2778
2779 (defmacro gnus-summary-article-sparse-p (article)
2780   "Say whether this article is a sparse article or not."
2781   `(memq ,article gnus-newsgroup-sparse))
2782
2783 (defmacro gnus-summary-article-ancient-p (article)
2784   "Say whether this article is a sparse article or not."
2785   `(memq ,article gnus-newsgroup-ancient))
2786
2787 (defun gnus-article-parent-p (number)
2788   "Say whether this article is a parent or not."
2789   (let ((data (gnus-data-find-list number)))
2790     (and (cdr data)                     ; There has to be an article after...
2791          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2792             (gnus-data-level (nth 1 data))))))
2793
2794 (defun gnus-article-children (number)
2795   "Return a list of all children to NUMBER."
2796   (let* ((data (gnus-data-find-list number))
2797          (level (gnus-data-level (car data)))
2798          children)
2799     (setq data (cdr data))
2800     (while (and data
2801                 (= (gnus-data-level (car data)) (1+ level)))
2802       (push (gnus-data-number (car data)) children)
2803       (setq data (cdr data)))
2804     children))
2805
2806 (defmacro gnus-summary-skip-intangible ()
2807   "If the current article is intangible, then jump to a different article."
2808   '(let ((to (get-text-property (point) 'gnus-intangible)))
2809      (and to (gnus-summary-goto-subject to))))
2810
2811 (defmacro gnus-summary-article-intangible-p ()
2812   "Say whether this article is intangible or not."
2813   '(get-text-property (point) 'gnus-intangible))
2814
2815 (defun gnus-article-read-p (article)
2816   "Say whether ARTICLE is read or not."
2817   (not (or (memq article gnus-newsgroup-marked)
2818            (memq article gnus-newsgroup-spam-marked)
2819            (memq article gnus-newsgroup-unreads)
2820            (memq article gnus-newsgroup-unselected)
2821            (memq article gnus-newsgroup-dormant))))
2822
2823 ;; Some summary mode macros.
2824
2825 (defmacro gnus-summary-article-number ()
2826   "The article number of the article on the current line.
2827 If there isn't an article number here, then we return the current
2828 article number."
2829   '(progn
2830      (gnus-summary-skip-intangible)
2831      (or (get-text-property (point) 'gnus-number)
2832          (gnus-summary-last-subject))))
2833
2834 (defmacro gnus-summary-article-header (&optional number)
2835   "Return the header of article NUMBER."
2836   `(gnus-data-header (gnus-data-find
2837                       ,(or number '(gnus-summary-article-number)))))
2838
2839 (defmacro gnus-summary-thread-level (&optional number)
2840   "Return the level of thread that starts with article NUMBER."
2841   `(if (and (eq gnus-summary-make-false-root 'dummy)
2842             (get-text-property (point) 'gnus-intangible))
2843        0
2844      (gnus-data-level (gnus-data-find
2845                        ,(or number '(gnus-summary-article-number))))))
2846
2847 (defmacro gnus-summary-article-mark (&optional number)
2848   "Return the mark of article NUMBER."
2849   `(gnus-data-mark (gnus-data-find
2850                     ,(or number '(gnus-summary-article-number)))))
2851
2852 (defmacro gnus-summary-article-pos (&optional number)
2853   "Return the position of the line of article NUMBER."
2854   `(gnus-data-pos (gnus-data-find
2855                    ,(or number '(gnus-summary-article-number)))))
2856
2857 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2858 (defmacro gnus-summary-article-subject (&optional number)
2859   "Return current subject string or nil if nothing."
2860   `(let ((headers
2861           ,(if number
2862                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2863              '(gnus-data-header (assq (gnus-summary-article-number)
2864                                       gnus-newsgroup-data)))))
2865      (and headers
2866           (vectorp headers)
2867           (mail-header-subject headers))))
2868
2869 (defmacro gnus-summary-article-score (&optional number)
2870   "Return current article score."
2871   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2872                   gnus-newsgroup-scored))
2873        gnus-summary-default-score 0))
2874
2875 (defun gnus-summary-article-children (&optional number)
2876   "Return a list of article numbers that are children of article NUMBER."
2877   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2878          (level (gnus-data-level (car data)))
2879          l children)
2880     (while (and (setq data (cdr data))
2881                 (> (setq l (gnus-data-level (car data))) level))
2882       (and (= (1+ level) l)
2883            (push (gnus-data-number (car data))
2884                  children)))
2885     (nreverse children)))
2886
2887 (defun gnus-summary-article-parent (&optional number)
2888   "Return the article number of the parent of article NUMBER."
2889   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2890                                     (gnus-data-list t)))
2891          (level (gnus-data-level (car data))))
2892     (if (zerop level)
2893         ()                              ; This is a root.
2894       ;; We search until we find an article with a level less than
2895       ;; this one.  That function has to be the parent.
2896       (while (and (setq data (cdr data))
2897                   (not (< (gnus-data-level (car data)) level))))
2898       (and data (gnus-data-number (car data))))))
2899
2900 (defun gnus-unread-mark-p (mark)
2901   "Say whether MARK is the unread mark."
2902   (= mark gnus-unread-mark))
2903
2904 (defun gnus-read-mark-p (mark)
2905   "Say whether MARK is one of the marks that mark as read.
2906 This is all marks except unread, ticked, dormant, and expirable."
2907   (not (or (= mark gnus-unread-mark)
2908            (= mark gnus-ticked-mark)
2909            (= mark gnus-spam-mark)
2910            (= mark gnus-dormant-mark)
2911            (= mark gnus-expirable-mark))))
2912
2913 (defmacro gnus-article-mark (number)
2914   "Return the MARK of article NUMBER.
2915 This macro should only be used when computing the mark the \"first\"
2916 time; i.e., when generating the summary lines.  After that,
2917 `gnus-summary-article-mark' should be used to examine the
2918 marks of articles."
2919   `(cond
2920     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2921     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2922     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2923     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2924     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2925     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2926     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2927     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2928            gnus-ancient-mark))))
2929
2930 ;; Saving hidden threads.
2931
2932 (defmacro gnus-save-hidden-threads (&rest forms)
2933   "Save hidden threads, eval FORMS, and restore the hidden threads."
2934   (let ((config (make-symbol "config")))
2935     `(let ((,config (gnus-hidden-threads-configuration)))
2936        (unwind-protect
2937            (save-excursion
2938              ,@forms)
2939          (gnus-restore-hidden-threads-configuration ,config)))))
2940 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2941 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2942
2943 (defun gnus-data-compute-positions ()
2944   "Compute the positions of all articles."
2945   (setq gnus-newsgroup-data-reverse nil)
2946   (let ((data gnus-newsgroup-data))
2947     (save-excursion
2948       (gnus-save-hidden-threads
2949         (gnus-summary-show-all-threads)
2950         (goto-char (point-min))
2951         (while data
2952           (while (get-text-property (point) 'gnus-intangible)
2953             (forward-line 1))
2954           (gnus-data-set-pos (car data) (+ (point) 3))
2955           (setq data (cdr data))
2956           (forward-line 1))))))
2957
2958 (defun gnus-hidden-threads-configuration ()
2959   "Return the current hidden threads configuration."
2960   (save-excursion
2961     (let (config)
2962       (goto-char (point-min))
2963       (while (search-forward "\r" nil t)
2964         (push (1- (point)) config))
2965       config)))
2966
2967 (defun gnus-restore-hidden-threads-configuration (config)
2968   "Restore hidden threads configuration from CONFIG."
2969   (save-excursion
2970     (let (point buffer-read-only)
2971       (while (setq point (pop config))
2972         (when (and (< point (point-max))
2973                    (goto-char point)
2974                    (eq (char-after) ?\n))
2975           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2976
2977 ;; Various summary mode internalish functions.
2978
2979 (defun gnus-mouse-pick-article (e)
2980   (interactive "e")
2981   (mouse-set-point e)
2982   (gnus-summary-next-page nil t))
2983
2984 (defun gnus-summary-set-display-table ()
2985   "Change the display table.
2986 Odd characters have a tendency to mess
2987 up nicely formatted displays - we make all possible glyphs
2988 display only a single character."
2989
2990   ;; We start from the standard display table, if any.
2991   (let ((table (or (copy-sequence standard-display-table)
2992                    (make-display-table)))
2993         (i 32))
2994     ;; Nix out all the control chars...
2995     (while (>= (setq i (1- i)) 0)
2996       (aset table i [??]))
2997     ;; ... but not newline and cr, of course.  (cr is necessary for the
2998     ;; selective display).
2999     (aset table ?\n nil)
3000     (aset table ?\r nil)
3001     ;; We keep TAB as well.
3002     (aset table ?\t nil)
3003     ;; We nix out any glyphs over 126 that are not set already.
3004     (let ((i 256))
3005       (while (>= (setq i (1- i)) 127)
3006         ;; Only modify if the entry is nil.
3007         (unless (aref table i)
3008           (aset table i [??]))))
3009     (setq buffer-display-table table)))
3010
3011 (defun gnus-summary-set-article-display-arrow (pos)
3012   "Update the overlay arrow to point to line at position POS."
3013   (when (and gnus-summary-display-arrow
3014              (boundp 'overlay-arrow-position)
3015              (boundp 'overlay-arrow-string))
3016     (save-excursion
3017       (goto-char pos)
3018       (beginning-of-line)
3019       (unless overlay-arrow-position
3020         (setq overlay-arrow-position (make-marker)))
3021       (setq overlay-arrow-string "=>"
3022             overlay-arrow-position (set-marker overlay-arrow-position
3023                                                (point)
3024                                                (current-buffer))))))
3025
3026 (defun gnus-summary-buffer-name (group)
3027   "Return the summary buffer name of GROUP."
3028   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3029
3030 (defun gnus-summary-setup-buffer (group)
3031   "Initialize summary buffer."
3032   (let ((buffer (gnus-summary-buffer-name group))
3033         (dead-name (concat "*Dead Summary "
3034                            (gnus-group-decoded-name group) "*")))
3035     ;; If a dead summary buffer exists, we kill it.
3036     (when (gnus-buffer-live-p dead-name)
3037       (gnus-kill-buffer dead-name))
3038     (if (get-buffer buffer)
3039         (progn
3040           (set-buffer buffer)
3041           (setq gnus-summary-buffer (current-buffer))
3042           (not gnus-newsgroup-prepared))
3043       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3044       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3045       (gnus-summary-mode group)
3046       (when gnus-carpal
3047         (gnus-carpal-setup-buffer 'summary))
3048       (unless gnus-single-article-buffer
3049         (make-local-variable 'gnus-article-buffer)
3050         (make-local-variable 'gnus-article-current)
3051         (make-local-variable 'gnus-original-article-buffer))
3052       (setq gnus-newsgroup-name group)
3053       ;; Set any local variables in the group parameters.
3054       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3055       t)))
3056
3057 (defun gnus-set-global-variables ()
3058   "Set the global equivalents of the buffer-local variables.
3059 They are set to the latest values they had.  These reflect the summary
3060 buffer that was in action when the last article was fetched."
3061   (when (eq major-mode 'gnus-summary-mode)
3062     (setq gnus-summary-buffer (current-buffer))
3063     (let ((name gnus-newsgroup-name)
3064           (marked gnus-newsgroup-marked)
3065           (spam gnus-newsgroup-spam-marked)
3066           (unread gnus-newsgroup-unreads)
3067           (headers gnus-current-headers)
3068           (data gnus-newsgroup-data)
3069           (summary gnus-summary-buffer)
3070           (article-buffer gnus-article-buffer)
3071           (original gnus-original-article-buffer)
3072           (gac gnus-article-current)
3073           (reffed gnus-reffed-article-number)
3074           (score-file gnus-current-score-file)
3075           (default-charset gnus-newsgroup-charset)
3076           vlist)
3077       (let ((locals gnus-newsgroup-variables))
3078         (while locals
3079           (if (consp (car locals))
3080               (push (eval (caar locals)) vlist)
3081             (push (eval (car locals)) vlist))
3082           (setq locals (cdr locals)))
3083         (setq vlist (nreverse vlist)))
3084       (save-excursion
3085         (set-buffer gnus-group-buffer)
3086         (setq gnus-newsgroup-name name
3087               gnus-newsgroup-marked marked
3088               gnus-newsgroup-spam-marked spam
3089               gnus-newsgroup-unreads unread
3090               gnus-current-headers headers
3091               gnus-newsgroup-data data
3092               gnus-article-current gac
3093               gnus-summary-buffer summary
3094               gnus-article-buffer article-buffer
3095               gnus-original-article-buffer original
3096               gnus-reffed-article-number reffed
3097               gnus-current-score-file score-file
3098               gnus-newsgroup-charset default-charset)
3099         (let ((locals gnus-newsgroup-variables))
3100           (while locals
3101             (if (consp (car locals))
3102                 (set (caar locals) (pop vlist))
3103               (set (car locals) (pop vlist)))
3104             (setq locals (cdr locals))))
3105         ;; The article buffer also has local variables.
3106         (when (gnus-buffer-live-p gnus-article-buffer)
3107           (set-buffer gnus-article-buffer)
3108           (setq gnus-summary-buffer summary))))))
3109
3110 (defun gnus-summary-article-unread-p (article)
3111   "Say whether ARTICLE is unread or not."
3112   (memq article gnus-newsgroup-unreads))
3113
3114 (defun gnus-summary-first-article-p (&optional article)
3115   "Return whether ARTICLE is the first article in the buffer."
3116   (if (not (setq article (or article (gnus-summary-article-number))))
3117       nil
3118     (eq article (caar gnus-newsgroup-data))))
3119
3120 (defun gnus-summary-last-article-p (&optional article)
3121   "Return whether ARTICLE is the last article in the buffer."
3122   (if (not (setq article (or article (gnus-summary-article-number))))
3123       ;; All non-existent numbers are the last article.  :-)
3124       t
3125     (not (cdr (gnus-data-find-list article)))))
3126
3127 (defun gnus-make-thread-indent-array ()
3128   (let ((n 200))
3129     (unless (and gnus-thread-indent-array
3130                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3131       (setq gnus-thread-indent-array (make-vector 201 "")
3132             gnus-thread-indent-array-level gnus-thread-indent-level)
3133       (while (>= n 0)
3134         (aset gnus-thread-indent-array n
3135               (make-string (* n gnus-thread-indent-level) ? ))
3136         (setq n (1- n))))))
3137
3138 (defun gnus-update-summary-mark-positions ()
3139   "Compute where the summary marks are to go."
3140   (save-excursion
3141     (when (gnus-buffer-exists-p gnus-summary-buffer)
3142       (set-buffer gnus-summary-buffer))
3143     (let ((gnus-replied-mark 129)
3144           (gnus-score-below-mark 130)
3145           (gnus-score-over-mark 130)
3146           (gnus-undownloaded-mark 131)
3147           (spec gnus-summary-line-format-spec)
3148           gnus-visual pos)
3149       (save-excursion
3150         (gnus-set-work-buffer)
3151         (let ((gnus-summary-line-format-spec spec)
3152               (gnus-newsgroup-downloadable '(0)))
3153           (gnus-summary-insert-line
3154            (make-full-mail-header 0 "" "nobody"
3155                                   "05 Apr 2001 23:33:09 +0400"
3156                                   "" "" 0 0 "" nil)
3157            0 nil t 128 t nil "" nil 1)
3158           (goto-char (point-min))
3159           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3160                                              (- (point) (point-min) 1)))))
3161           (goto-char (point-min))
3162           (push (cons 'replied (and (search-forward "\201" nil t)
3163                                     (- (point) (point-min) 1)))
3164                 pos)
3165           (goto-char (point-min))
3166           (push (cons 'score (and (search-forward "\202" nil t)
3167                                   (- (point) (point-min) 1)))
3168                 pos)
3169           (goto-char (point-min))
3170           (push (cons 'download
3171                       (and (search-forward "\203" nil t)
3172                            (- (point) (point-min) 1)))
3173                 pos)))
3174       (setq gnus-summary-mark-positions pos))))
3175
3176 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3177   "Insert a dummy root in the summary buffer."
3178   (beginning-of-line)
3179   (gnus-add-text-properties
3180    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3181    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3182
3183 (defun gnus-summary-extract-address-component (from)
3184   (or (car (funcall gnus-extract-address-components from))
3185       from))
3186
3187 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3188   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3189                                 default-mime-charset)))
3190     ;; Is it really necessary to do this next part for each summary line?
3191     ;; Luckily, doesn't seem to slow things down much.
3192     (or
3193      (and gnus-ignored-from-addresses
3194           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3195           (let ((extra-headers (mail-header-extra header))
3196                 to
3197                 newsgroups)
3198             (cond
3199              ((setq to (cdr (assq 'To extra-headers)))
3200               (concat "-> "
3201                       (inline
3202                         (gnus-summary-extract-address-component
3203                          (funcall gnus-decode-encoded-word-function to)))))
3204              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3205               (concat "=> " newsgroups)))))
3206      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3207
3208 (defun gnus-summary-insert-line (gnus-tmp-header
3209                                  gnus-tmp-level gnus-tmp-current
3210                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3211                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3212                                  &optional gnus-tmp-dummy gnus-tmp-score
3213                                  gnus-tmp-process)
3214   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3215          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3216          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3217          (gnus-tmp-score-char
3218           (if (or (null gnus-summary-default-score)
3219                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3220                       gnus-summary-zcore-fuzz))
3221               ?\ ;;;Whitespace
3222             (if (< gnus-tmp-score gnus-summary-default-score)
3223                 gnus-score-below-mark gnus-score-over-mark)))
3224          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3225          (gnus-tmp-replied
3226           (cond (gnus-tmp-process gnus-process-mark)
3227                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3228                  gnus-cached-mark)
3229                 (gnus-tmp-replied gnus-replied-mark)
3230                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3231                  gnus-forwarded-mark)
3232                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3233                  gnus-saved-mark)
3234                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3235                  gnus-recent-mark)
3236                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3237                  gnus-unseen-mark)
3238                 (t gnus-no-mark)))
3239          (gnus-tmp-downloaded
3240           (cond (undownloaded 
3241                  gnus-undownloaded-mark)
3242                 (gnus-newsgroup-agentized
3243                  gnus-downloaded-mark)
3244                 (t
3245                  gnus-no-mark)))
3246          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3247          (gnus-tmp-name
3248           (cond
3249            ((string-match "<[^>]+> *$" gnus-tmp-from)
3250             (let ((beg (match-beginning 0)))
3251               (or (and (string-match "^\".+\"" gnus-tmp-from)
3252                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3253                   (substring gnus-tmp-from 0 beg))))
3254            ((string-match "(.+)" gnus-tmp-from)
3255             (substring gnus-tmp-from
3256                        (1+ (match-beginning 0)) (1- (match-end 0))))
3257            (t gnus-tmp-from)))
3258          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3259          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3260          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3261          (buffer-read-only nil))
3262     (when (string= gnus-tmp-name "")
3263       (setq gnus-tmp-name gnus-tmp-from))
3264     (unless (numberp gnus-tmp-lines)
3265       (setq gnus-tmp-lines -1))
3266     (if (= gnus-tmp-lines -1)
3267         (setq gnus-tmp-lines "?")
3268       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3269     (gnus-put-text-property-excluding-characters-with-faces
3270      (point)
3271      (progn (eval gnus-summary-line-format-spec) (point))
3272      'gnus-number gnus-tmp-number)
3273     (when (gnus-visual-p 'summary-highlight 'highlight)
3274       (forward-line -1)
3275       (gnus-run-hooks 'gnus-summary-update-hook)
3276       (forward-line 1))))
3277
3278 (defun gnus-summary-update-line (&optional dont-update)
3279   "Update summary line after change."
3280   (when (and gnus-summary-default-score
3281              (not gnus-summary-inhibit-highlight))
3282     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3283            (article (gnus-summary-article-number))
3284            (score (gnus-summary-article-score article)))
3285       (unless dont-update
3286         (if (and gnus-summary-mark-below
3287                  (< (gnus-summary-article-score)
3288                     gnus-summary-mark-below))
3289             ;; This article has a low score, so we mark it as read.
3290             (when (memq article gnus-newsgroup-unreads)
3291               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3292           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3293             ;; This article was previously marked as read on account
3294             ;; of a low score, but now it has risen, so we mark it as
3295             ;; unread.
3296             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3297         (gnus-summary-update-mark
3298          (if (or (null gnus-summary-default-score)
3299                  (<= (abs (- score gnus-summary-default-score))
3300                      gnus-summary-zcore-fuzz))
3301              ?\ ;;;Whitespace
3302            (if (< score gnus-summary-default-score)
3303                gnus-score-below-mark gnus-score-over-mark))
3304          'score))
3305       ;; Do visual highlighting.
3306       (when (gnus-visual-p 'summary-highlight 'highlight)
3307         (gnus-run-hooks 'gnus-summary-update-hook)))))
3308
3309 (defvar gnus-tmp-new-adopts nil)
3310
3311 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3312   "Return the number of articles in THREAD.
3313 This may be 0 in some cases -- if none of the articles in
3314 the thread are to be displayed."
3315   (let* ((number
3316           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3317           (cond
3318            ((not (listp thread))
3319             1)
3320            ((and (consp thread) (cdr thread))
3321             (apply
3322              '+ 1 (mapcar
3323                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3324            ((null thread)
3325             1)
3326            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3327             1)
3328            (t 0))))
3329     (when (and level (zerop level) gnus-tmp-new-adopts)
3330       (incf number
3331             (apply '+ (mapcar
3332                        'gnus-summary-number-of-articles-in-thread
3333                        gnus-tmp-new-adopts))))
3334     (if char
3335         (if (> number 1) gnus-not-empty-thread-mark
3336           gnus-empty-thread-mark)
3337       number)))
3338
3339 (defsubst gnus-summary-line-message-size (head)
3340   "Return pretty-printed version of message size.
3341 This function is intended to be used in
3342 `gnus-summary-line-format-alist', which see."
3343   (let ((c (or (mail-header-chars head) -1)))
3344     (cond ((< c 0) "n/a")               ; chars not available
3345           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3346           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3347           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3348           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3349
3350
3351 (defun gnus-summary-set-local-parameters (group)
3352   "Go through the local params of GROUP and set all variable specs in that list."
3353   (let ((params (gnus-group-find-parameter group))
3354         (vars '(quit-config))           ; Ignore quit-config.
3355         elem)
3356     (while params
3357       (setq elem (car params)
3358             params (cdr params))
3359       (and (consp elem)                 ; Has to be a cons.
3360            (consp (cdr elem))           ; The cdr has to be a list.
3361            (symbolp (car elem))         ; Has to be a symbol in there.
3362            (not (memq (car elem) vars))
3363            (ignore-errors               ; So we set it.
3364              (push (car elem) vars)
3365              (make-local-variable (car elem))
3366              (set (car elem) (eval (nth 1 elem))))))))
3367
3368 (defun gnus-summary-read-group (group &optional show-all no-article
3369                                       kill-buffer no-display backward
3370                                       select-articles)
3371   "Start reading news in newsgroup GROUP.
3372 If SHOW-ALL is non-nil, already read articles are also listed.
3373 If NO-ARTICLE is non-nil, no article is selected initially.
3374 If NO-DISPLAY, don't generate a summary buffer."
3375   (let (result)
3376     (while (and group
3377                 (null (setq result
3378                             (let ((gnus-auto-select-next nil))
3379                               (or (gnus-summary-read-group-1
3380                                    group show-all no-article
3381                                    kill-buffer no-display
3382                                    select-articles)
3383                                   (setq show-all nil
3384                                         select-articles nil)))))
3385                 (eq gnus-auto-select-next 'quietly))
3386       (set-buffer gnus-group-buffer)
3387       ;; The entry function called above goes to the next
3388       ;; group automatically, so we go two groups back
3389       ;; if we are searching for the previous group.
3390       (when backward
3391         (gnus-group-prev-unread-group 2))
3392       (if (not (equal group (gnus-group-group-name)))
3393           (setq group (gnus-group-group-name))
3394         (setq group nil)))
3395     result))
3396
3397 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3398   "Directly jump to the other GROUP from summary buffer.
3399 If SHOW-ALL is non-nil, already read articles are also listed."
3400   (interactive
3401    (if (eq gnus-summary-buffer (current-buffer))
3402        (list (completing-read
3403               "Group: " gnus-active-hashtb nil t
3404               (when (and gnus-newsgroup-name
3405                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3406                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3407               'gnus-group-history)
3408              current-prefix-arg)
3409      (error "%s must be invoked from a gnus summary buffer." this-command)))
3410   (unless (or (zerop (length group))
3411               (and gnus-newsgroup-name
3412                    (string-equal gnus-newsgroup-name group)))
3413     (gnus-summary-exit)
3414     (gnus-summary-read-group group show-all
3415                              gnus-dont-select-after-jump-to-other-group)))
3416
3417 (defun gnus-summary-read-group-1 (group show-all no-article
3418                                         kill-buffer no-display
3419                                         &optional select-articles)
3420   ;; Killed foreign groups can't be entered.
3421   ;;  (when (and (not (gnus-group-native-p group))
3422   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3423   ;;    (error "Dead non-native groups can't be entered"))
3424   (gnus-message 5 "Retrieving newsgroup: %s..."
3425                 (gnus-group-decoded-name group))
3426   (let* ((new-group (gnus-summary-setup-buffer group))
3427          (quit-config (gnus-group-quit-config group))
3428          (did-select (and new-group (gnus-select-newsgroup
3429                                      group show-all select-articles))))
3430     (cond
3431      ;; This summary buffer exists already, so we just select it.
3432      ((not new-group)
3433       (gnus-set-global-variables)
3434       (when kill-buffer
3435         (gnus-kill-or-deaden-summary kill-buffer))
3436       (gnus-configure-windows 'summary 'force)
3437       (gnus-set-mode-line 'summary)
3438       (gnus-summary-position-point)
3439       (message "")
3440       t)
3441      ;; We couldn't select this group.
3442      ((null did-select)
3443       (when (and (eq major-mode 'gnus-summary-mode)
3444                  (not (equal (current-buffer) kill-buffer)))
3445         (kill-buffer (current-buffer))
3446         (if (not quit-config)
3447             (progn
3448               ;; Update the info -- marks might need to be removed,
3449               ;; for instance.
3450               (gnus-summary-update-info)
3451               (set-buffer gnus-group-buffer)
3452               (gnus-group-jump-to-group group)
3453               (gnus-group-next-unread-group 1))
3454           (gnus-handle-ephemeral-exit quit-config)))
3455       (let ((grpinfo (gnus-get-info group)))
3456         (if (null (gnus-info-read grpinfo))
3457             (gnus-message 3 "Group %s contains no messages"
3458                           (gnus-group-decoded-name group))
3459           (gnus-message 3 "Can't select group")))
3460       nil)
3461      ;; The user did a `C-g' while prompting for number of articles,
3462      ;; so we exit this group.
3463      ((eq did-select 'quit)
3464       (and (eq major-mode 'gnus-summary-mode)
3465            (not (equal (current-buffer) kill-buffer))
3466            (kill-buffer (current-buffer)))
3467       (when kill-buffer
3468         (gnus-kill-or-deaden-summary kill-buffer))
3469       (if (not quit-config)
3470           (progn
3471             (set-buffer gnus-group-buffer)
3472             (gnus-group-jump-to-group group)
3473             (gnus-group-next-unread-group 1)
3474             (gnus-configure-windows 'group 'force))
3475         (gnus-handle-ephemeral-exit quit-config))
3476       ;; Finally signal the quit.
3477       (signal 'quit nil))
3478      ;; The group was successfully selected.
3479      (t
3480       (gnus-set-global-variables)
3481       ;; Save the active value in effect when the group was entered.
3482       (setq gnus-newsgroup-active
3483             (gnus-copy-sequence
3484              (gnus-active gnus-newsgroup-name)))
3485       ;; You can change the summary buffer in some way with this hook.
3486       (gnus-run-hooks 'gnus-select-group-hook)
3487       (gnus-update-format-specifications
3488        nil 'summary 'summary-mode 'summary-dummy)
3489       (gnus-update-summary-mark-positions)
3490       ;; Do score processing.
3491       (when gnus-use-scoring
3492         (gnus-possibly-score-headers))
3493       ;; Check whether to fill in the gaps in the threads.
3494       (when gnus-build-sparse-threads
3495         (gnus-build-sparse-threads))
3496       ;; Find the initial limit.
3497       (if gnus-show-threads
3498           (if show-all
3499               (let ((gnus-newsgroup-dormant nil))
3500                 (gnus-summary-initial-limit show-all))
3501             (gnus-summary-initial-limit show-all))
3502         ;; When unthreaded, all articles are always shown.
3503         (setq gnus-newsgroup-limit
3504               (mapcar
3505                (lambda (header) (mail-header-number header))
3506                gnus-newsgroup-headers)))
3507       ;; Generate the summary buffer.
3508       (unless no-display
3509         (gnus-summary-prepare))
3510       (when gnus-use-trees
3511         (gnus-tree-open group)
3512         (setq gnus-summary-highlight-line-function
3513               'gnus-tree-highlight-article))
3514       ;; If the summary buffer is empty, but there are some low-scored
3515       ;; articles or some excluded dormants, we include these in the
3516       ;; buffer.
3517       (when (and (zerop (buffer-size))
3518                  (not no-display))
3519         (cond (gnus-newsgroup-dormant
3520                (gnus-summary-limit-include-dormant))
3521               ((and gnus-newsgroup-scored show-all)
3522                (gnus-summary-limit-include-expunged t))))
3523       ;; Function `gnus-apply-kill-file' must be called in this hook.
3524       (gnus-run-hooks 'gnus-apply-kill-hook)
3525       (if (and (zerop (buffer-size))
3526                (not no-display))
3527           (progn
3528             ;; This newsgroup is empty.
3529             (gnus-summary-catchup-and-exit nil t)
3530             (gnus-message 6 "No unread news")
3531             (when kill-buffer
3532               (gnus-kill-or-deaden-summary kill-buffer))
3533             ;; Return nil from this function.
3534             nil)
3535         ;; Hide conversation thread subtrees.  We cannot do this in
3536         ;; gnus-summary-prepare-hook since kill processing may not
3537         ;; work with hidden articles.
3538         (gnus-summary-maybe-hide-threads)
3539         (when kill-buffer
3540           (gnus-kill-or-deaden-summary kill-buffer))
3541         (gnus-summary-auto-select-subject)
3542         ;; Show first unread article if requested.
3543         (if (and (not no-article)
3544                  (not no-display)
3545                  gnus-newsgroup-unreads
3546                  gnus-auto-select-first)
3547             (progn
3548               (gnus-configure-windows 'summary)
3549               (let ((art (gnus-summary-article-number)))
3550                 (unless (and (not gnus-plugged)
3551                              (or (memq art gnus-newsgroup-undownloaded)
3552                                  (memq art gnus-newsgroup-downloadable)))
3553                   (gnus-summary-goto-article art))))
3554           ;; Don't select any articles.
3555           (gnus-summary-position-point)
3556           (gnus-configure-windows 'summary 'force)
3557           (gnus-set-mode-line 'summary))
3558         (when (get-buffer-window gnus-group-buffer t)
3559           ;; Gotta use windows, because recenter does weird stuff if
3560           ;; the current buffer ain't the displayed window.
3561           (let ((owin (selected-window)))
3562             (select-window (get-buffer-window gnus-group-buffer t))
3563             (when (gnus-group-goto-group group)
3564               (recenter))
3565             (select-window owin)))
3566         ;; Mark this buffer as "prepared".
3567         (setq gnus-newsgroup-prepared t)
3568         (gnus-run-hooks 'gnus-summary-prepared-hook)
3569         (unless (gnus-ephemeral-group-p group)
3570           (gnus-group-update-group group))
3571         t)))))
3572
3573 (defun gnus-summary-auto-select-subject ()
3574   "Select the subject line on initial group entry."
3575   (goto-char (point-min))
3576   (cond
3577    ((eq gnus-auto-select-subject 'best)
3578     (gnus-summary-best-unread-subject))
3579    ((eq gnus-auto-select-subject 'unread)
3580     (gnus-summary-first-unread-subject))
3581    ((eq gnus-auto-select-subject 'unseen)
3582     (gnus-summary-first-unseen-subject))
3583    ((eq gnus-auto-select-subject 'unseen-or-unread)
3584     (gnus-summary-first-unseen-or-unread-subject))
3585    ((eq gnus-auto-select-subject 'first)
3586     ;; Do nothing.
3587     )
3588    ((gnus-functionp gnus-auto-select-subject)
3589     (funcall gnus-auto-select-subject))))
3590
3591 (defun gnus-summary-prepare ()
3592   "Generate the summary buffer."
3593   (interactive)
3594   (let ((buffer-read-only nil))
3595     (erase-buffer)
3596     (setq gnus-newsgroup-data nil
3597           gnus-newsgroup-data-reverse nil)
3598     (gnus-run-hooks 'gnus-summary-generate-hook)
3599     ;; Generate the buffer, either with threads or without.
3600     (when gnus-newsgroup-headers
3601       (gnus-summary-prepare-threads
3602        (if gnus-show-threads
3603            (gnus-sort-gathered-threads
3604             (funcall gnus-summary-thread-gathering-function
3605                      (gnus-sort-threads
3606                       (gnus-cut-threads (gnus-make-threads)))))
3607          ;; Unthreaded display.
3608          (gnus-sort-articles gnus-newsgroup-headers))))
3609     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3610     ;; Call hooks for modifying summary buffer.
3611     (goto-char (point-min))
3612     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3613
3614 (defsubst gnus-general-simplify-subject (subject)
3615   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3616   (setq subject
3617         (cond
3618          ;; Truncate the subject.
3619          (gnus-simplify-subject-functions
3620           (gnus-map-function gnus-simplify-subject-functions subject))
3621          ((numberp gnus-summary-gather-subject-limit)
3622           (setq subject (gnus-simplify-subject-re subject))
3623           (if (> (length subject) gnus-summary-gather-subject-limit)
3624               (substring subject 0 gnus-summary-gather-subject-limit)
3625             subject))
3626          ;; Fuzzily simplify it.
3627          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3628           (gnus-simplify-subject-fuzzy subject))
3629          ;; Just remove the leading "Re:".
3630          (t
3631           (gnus-simplify-subject-re subject))))
3632
3633   (if (and gnus-summary-gather-exclude-subject
3634            (string-match gnus-summary-gather-exclude-subject subject))
3635       nil                               ; This article shouldn't be gathered
3636     subject))
3637
3638 (defun gnus-summary-simplify-subject-query ()
3639   "Query where the respool algorithm would put this article."
3640   (interactive)
3641   (gnus-summary-select-article)
3642   (message "%s"
3643            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3644
3645 (defun gnus-gather-threads-by-subject (threads)
3646   "Gather threads by looking at Subject headers."
3647   (if (not gnus-summary-make-false-root)
3648       threads
3649     (let ((hashtb (gnus-make-hashtable 1024))
3650           (prev threads)
3651           (result threads)
3652           subject hthread whole-subject)
3653       (while threads
3654         (setq subject (gnus-general-simplify-subject
3655                        (setq whole-subject (mail-header-subject
3656                                             (caar threads)))))
3657         (when subject
3658           (if (setq hthread (gnus-gethash subject hashtb))
3659               (progn
3660                 ;; We enter a dummy root into the thread, if we
3661                 ;; haven't done that already.
3662                 (unless (stringp (caar hthread))
3663                   (setcar hthread (list whole-subject (car hthread))))
3664                 ;; We add this new gathered thread to this gathered
3665                 ;; thread.
3666                 (setcdr (car hthread)
3667                         (nconc (cdar hthread) (list (car threads))))
3668                 ;; Remove it from the list of threads.
3669                 (setcdr prev (cdr threads))
3670                 (setq threads prev))
3671             ;; Enter this thread into the hash table.
3672             (gnus-sethash subject
3673                           (if gnus-summary-make-false-root-always
3674                               (progn
3675                                 ;; If you want a dummy root above all
3676                                 ;; threads...
3677                                 (setcar threads (list whole-subject
3678                                                       (car threads)))
3679                                 threads)
3680                             threads)
3681                           hashtb)))
3682         (setq prev threads)
3683         (setq threads (cdr threads)))
3684       result)))
3685
3686 (defun gnus-gather-threads-by-references (threads)
3687   "Gather threads by looking at References headers."
3688   (let ((idhashtb (gnus-make-hashtable 1024))
3689         (thhashtb (gnus-make-hashtable 1024))
3690         (prev threads)
3691         (result threads)
3692         ids references id gthread gid entered ref)
3693     (while threads
3694       (when (setq references (mail-header-references (caar threads)))
3695         (setq id (mail-header-id (caar threads))
3696               ids (inline (gnus-split-references references))
3697               entered nil)
3698         (while (setq ref (pop ids))
3699           (setq ids (delete ref ids))
3700           (if (not (setq gid (gnus-gethash ref idhashtb)))
3701               (progn
3702                 (gnus-sethash ref id idhashtb)
3703                 (gnus-sethash id threads thhashtb))
3704             (setq gthread (gnus-gethash gid thhashtb))
3705             (unless entered
3706               ;; We enter a dummy root into the thread, if we
3707               ;; haven't done that already.
3708               (unless (stringp (caar gthread))
3709                 (setcar gthread (list (mail-header-subject (caar gthread))
3710                                       (car gthread))))
3711               ;; We add this new gathered thread to this gathered
3712               ;; thread.
3713               (setcdr (car gthread)
3714                       (nconc (cdar gthread) (list (car threads)))))
3715             ;; Add it into the thread hash table.
3716             (gnus-sethash id gthread thhashtb)
3717             (setq entered t)
3718             ;; Remove it from the list of threads.
3719             (setcdr prev (cdr threads))
3720             (setq threads prev))))
3721       (setq prev threads)
3722       (setq threads (cdr threads)))
3723     result))
3724
3725 (defun gnus-sort-gathered-threads (threads)
3726   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3727   (let ((result threads))
3728     (while threads
3729       (when (stringp (caar threads))
3730         (setcdr (car threads)
3731                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3732       (setq threads (cdr threads)))
3733     result))
3734
3735 (defun gnus-thread-loop-p (root thread)
3736   "Say whether ROOT is in THREAD."
3737   (let ((stack (list thread))
3738         (infloop 0)
3739         th)
3740     (while (setq thread (pop stack))
3741       (setq th (cdr thread))
3742       (while (and th
3743                   (not (eq (caar th) root)))
3744         (pop th))
3745       (if th
3746           ;; We have found a loop.
3747           (let (ref-dep)
3748             (setcdr thread (delq (car th) (cdr thread)))
3749             (if (boundp (setq ref-dep (intern "none"
3750                                               gnus-newsgroup-dependencies)))
3751                 (setcdr (symbol-value ref-dep)
3752                         (nconc (cdr (symbol-value ref-dep))
3753                                (list (car th))))
3754               (set ref-dep (list nil (car th))))
3755             (setq infloop 1
3756                   stack nil))
3757         ;; Push all the subthreads onto the stack.
3758         (push (cdr thread) stack)))
3759     infloop))
3760
3761 (defun gnus-make-threads ()
3762   "Go through the dependency hashtb and find the roots.  Return all threads."
3763   (let (threads)
3764     (while (catch 'infloop
3765              (mapatoms
3766               (lambda (refs)
3767                 ;; Deal with self-referencing References loops.
3768                 (when (and (car (symbol-value refs))
3769                            (not (zerop
3770                                  (apply
3771                                   '+
3772                                   (mapcar
3773                                    (lambda (thread)
3774                                      (gnus-thread-loop-p
3775                                       (car (symbol-value refs)) thread))
3776                                    (cdr (symbol-value refs)))))))
3777                   (setq threads nil)
3778                   (throw 'infloop t))
3779                 (unless (car (symbol-value refs))
3780                   ;; These threads do not refer back to any other articles,
3781                   ;; so they're roots.
3782                   (setq threads (append (cdr (symbol-value refs)) threads))))
3783               gnus-newsgroup-dependencies)))
3784     threads))
3785
3786 ;; Build the thread tree.
3787 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3788   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3789
3790 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3791 if it was already present.
3792
3793 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3794 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3795 Message-IDs will be renamed to a unique Message-ID before being
3796 entered.
3797
3798 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3799   (let* ((id (mail-header-id header))
3800          (id-dep (and id (intern id dependencies)))
3801          parent-id ref ref-dep ref-header replaced)
3802     ;; Enter this `header' in the `dependencies' table.
3803     (cond
3804      ((not id-dep)
3805       (setq header nil))
3806      ;; The first two cases do the normal part: enter a new `header'
3807      ;; in the `dependencies' table.
3808      ((not (boundp id-dep))
3809       (set id-dep (list header)))
3810      ((null (car (symbol-value id-dep)))
3811       (setcar (symbol-value id-dep) header))
3812
3813      ;; From here the `header' was already present in the
3814      ;; `dependencies' table.
3815      (force-new
3816       ;; Overrides an existing entry;
3817       ;; just set the header part of the entry.
3818       (setcar (symbol-value id-dep) header)
3819       (setq replaced t))
3820
3821      ;; Renames the existing `header' to a unique Message-ID.
3822      ((not gnus-summary-ignore-duplicates)
3823       ;; An article with this Message-ID has already been seen.
3824       ;; We rename the Message-ID.
3825       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3826            (list header))
3827       (mail-header-set-id header id))
3828
3829      ;; The last case ignores an existing entry, except it adds any
3830      ;; additional Xrefs (in case the two articles came from different
3831      ;; servers.
3832      ;; Also sets `header' to `nil' meaning that the `dependencies'
3833      ;; table was *not* modified.
3834      (t
3835       (mail-header-set-xref
3836        (car (symbol-value id-dep))
3837        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3838                    "")
3839                (or (mail-header-xref header) "")))
3840       (setq header nil)))
3841
3842     (when (and header (not replaced))
3843       ;; First check that we are not creating a References loop.
3844       (setq parent-id (gnus-parent-id (mail-header-references header)))
3845       (setq ref parent-id)
3846       (while (and ref
3847                   (setq ref-dep (intern-soft ref dependencies))
3848                   (boundp ref-dep)
3849                   (setq ref-header (car (symbol-value ref-dep))))
3850         (if (string= id ref)
3851             ;; Yuk!  This is a reference loop.  Make the article be a
3852             ;; root article.
3853             (progn
3854               (mail-header-set-references (car (symbol-value id-dep)) "none")
3855               (setq ref nil)
3856               (setq parent-id nil))
3857           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3858       (setq ref-dep (intern (or parent-id "none") dependencies))
3859       (if (boundp ref-dep)
3860           (setcdr (symbol-value ref-dep)
3861                   (nconc (cdr (symbol-value ref-dep))
3862                          (list (symbol-value id-dep))))
3863         (set ref-dep (list nil (symbol-value id-dep)))))
3864     header))
3865
3866 (defun gnus-extract-message-id-from-in-reply-to (string)
3867   (if (string-match "<[^>]+>" string)
3868       (substring string (match-beginning 0) (match-end 0))
3869     nil))
3870
3871 (defun gnus-build-sparse-threads ()
3872   (let ((headers gnus-newsgroup-headers)
3873         (mail-parse-charset gnus-newsgroup-charset)
3874         (gnus-summary-ignore-duplicates t)
3875         header references generation relations
3876         subject child end new-child date)
3877     ;; First we create an alist of generations/relations, where
3878     ;; generations is how much we trust the relation, and the relation
3879     ;; is parent/child.
3880     (gnus-message 7 "Making sparse threads...")
3881     (save-excursion
3882       (nnheader-set-temp-buffer " *gnus sparse threads*")
3883       (while (setq header (pop headers))
3884         (when (and (setq references (mail-header-references header))
3885                    (not (string= references "")))
3886           (insert references)
3887           (setq child (mail-header-id header)
3888                 subject (mail-header-subject header)
3889                 date (mail-header-date header)
3890                 generation 0)
3891           (while (search-backward ">" nil t)
3892             (setq end (1+ (point)))
3893             (when (search-backward "<" nil t)
3894               (setq new-child (buffer-substring (point) end))
3895               (push (list (incf generation)
3896                           child (setq child new-child)
3897                           subject date)
3898                     relations)))
3899           (when child
3900             (push (list (1+ generation) child nil subject) relations))
3901           (erase-buffer)))
3902       (kill-buffer (current-buffer)))
3903     ;; Sort over trustworthiness.
3904     (mapcar
3905      (lambda (relation)
3906        (when (gnus-dependencies-add-header
3907               (make-full-mail-header-from-decoded-header
3908                gnus-reffed-article-number
3909                (nth 3 relation) "" (or (nth 4 relation) "")
3910                (nth 1 relation)
3911                (or (nth 2 relation) "") 0 0 "")
3912               gnus-newsgroup-dependencies nil)
3913          (push gnus-reffed-article-number gnus-newsgroup-limit)
3914          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3915          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3916                gnus-newsgroup-reads)
3917          (decf gnus-reffed-article-number)))
3918      (sort relations 'car-less-than-car))
3919     (gnus-message 7 "Making sparse threads...done")))
3920
3921 (defun gnus-build-old-threads ()
3922   ;; Look at all the articles that refer back to old articles, and
3923   ;; fetch the headers for the articles that aren't there.  This will
3924   ;; build complete threads - if the roots haven't been expired by the
3925   ;; server, that is.
3926   (let ((mail-parse-charset gnus-newsgroup-charset)
3927         id heads)
3928     (mapatoms
3929      (lambda (refs)
3930        (when (not (car (symbol-value refs)))
3931          (setq heads (cdr (symbol-value refs)))
3932          (while heads
3933            (if (memq (mail-header-number (caar heads))
3934                      gnus-newsgroup-dormant)
3935                (setq heads (cdr heads))
3936              (setq id (symbol-name refs))
3937              (while (and (setq id (gnus-build-get-header id))
3938                          (not (car (gnus-id-to-thread id)))))
3939              (setq heads nil)))))
3940      gnus-newsgroup-dependencies)))
3941
3942 ;; This function has to be called with point after the article number
3943 ;; on the beginning of the line.
3944 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3945   (let ((eol (gnus-point-at-eol))
3946         (buffer (current-buffer))
3947         header references in-reply-to)
3948
3949     ;; overview: [num subject from date id refs chars lines misc]
3950     (unwind-protect
3951         (progn
3952           (narrow-to-region (point) eol)
3953           (unless (eobp)
3954             (forward-char))
3955
3956           (setq header
3957                 (make-full-mail-header
3958                  number                         ; number
3959                  (nnheader-nov-field)           ; subject
3960                  (nnheader-nov-field)           ; from
3961                  (nnheader-nov-field)           ; date
3962                  (nnheader-nov-read-message-id) ; id
3963                  (nnheader-nov-field)           ; refs
3964                  (nnheader-nov-read-integer)    ; chars
3965                  (nnheader-nov-read-integer)    ; lines
3966                  (unless (eobp)
3967                    (if (looking-at "Xref: ")
3968                        (goto-char (match-end 0)))
3969                    (nnheader-nov-field))        ; Xref
3970                  (nnheader-nov-parse-extra))))  ; extra
3971
3972       (widen))
3973
3974     (when (and (string= references "")
3975                (setq in-reply-to (mail-header-extra header))
3976                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3977       (mail-header-set-references
3978        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3979
3980     (when gnus-alter-header-function
3981       (funcall gnus-alter-header-function header))
3982     (gnus-dependencies-add-header header dependencies force-new)))
3983
3984 (defun gnus-build-get-header (id)
3985   "Look through the buffer of NOV lines and find the header to ID.
3986 Enter this line into the dependencies hash table, and return
3987 the id of the parent article (if any)."
3988   (let ((deps gnus-newsgroup-dependencies)
3989         found header)
3990     (prog1
3991         (save-excursion
3992           (set-buffer nntp-server-buffer)
3993           (let ((case-fold-search nil))
3994             (goto-char (point-min))
3995             (while (and (not found)
3996                         (search-forward id nil t))
3997               (beginning-of-line)
3998               (setq found (looking-at
3999                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4000                                    (regexp-quote id))))
4001               (or found (beginning-of-line 2)))
4002             (when found
4003               (beginning-of-line)
4004               (and
4005                (setq header (gnus-nov-parse-line
4006                              (read (current-buffer)) deps))
4007                (gnus-parent-id (mail-header-references header))))))
4008       (when header
4009         (let ((number (mail-header-number header)))
4010           (push number gnus-newsgroup-limit)
4011           (push header gnus-newsgroup-headers)
4012           (if (memq number gnus-newsgroup-unselected)
4013               (progn
4014                 (setq gnus-newsgroup-unreads
4015                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4016                                                number))
4017                 (setq gnus-newsgroup-unselected
4018                       (delq number gnus-newsgroup-unselected)))
4019             (push number gnus-newsgroup-ancient)))))))
4020
4021 (defun gnus-build-all-threads ()
4022   "Read all the headers."
4023   (let ((gnus-summary-ignore-duplicates t)
4024         (mail-parse-charset gnus-newsgroup-charset)
4025         (dependencies gnus-newsgroup-dependencies)
4026         header article)
4027     (save-excursion
4028       (set-buffer nntp-server-buffer)
4029       (let ((case-fold-search nil))
4030         (goto-char (point-min))
4031         (while (not (eobp))
4032           (ignore-errors
4033             (setq article (read (current-buffer))
4034                   header (gnus-nov-parse-line article dependencies)))
4035           (when header
4036             (save-excursion
4037               (set-buffer gnus-summary-buffer)
4038               (push header gnus-newsgroup-headers)
4039               (if (memq (setq article (mail-header-number header))
4040                         gnus-newsgroup-unselected)
4041                   (progn
4042                     (setq gnus-newsgroup-unreads
4043                           (gnus-add-to-sorted-list
4044                            gnus-newsgroup-unreads article))
4045                     (setq gnus-newsgroup-unselected
4046                           (delq article gnus-newsgroup-unselected)))
4047                 (push article gnus-newsgroup-ancient)))
4048             (forward-line 1)))))))
4049
4050 (defun gnus-summary-update-article-line (article header)
4051   "Update the line for ARTICLE using HEADERS."
4052   (let* ((id (mail-header-id header))
4053          (thread (gnus-id-to-thread id)))
4054     (unless thread
4055       (error "Article in no thread"))
4056     ;; Update the thread.
4057     (setcar thread header)
4058     (gnus-summary-goto-subject article)
4059     (let* ((datal (gnus-data-find-list article))
4060            (data (car datal))
4061            (buffer-read-only nil)
4062            (level (gnus-summary-thread-level)))
4063       (gnus-delete-line)
4064       (let ((inserted (- (point)
4065                          (progn
4066                            (gnus-summary-insert-line
4067                             header level nil 
4068                             (memq article gnus-newsgroup-undownloaded)
4069                             (gnus-article-mark article)
4070                             (memq article gnus-newsgroup-replied)
4071                             (memq article gnus-newsgroup-expirable)
4072                             ;; Only insert the Subject string when it's different
4073                             ;; from the previous Subject string.
4074                             (if (and
4075                                  gnus-show-threads
4076                                  (gnus-subject-equal
4077                                   (condition-case ()
4078                                       (mail-header-subject
4079                                        (gnus-data-header
4080                                         (cadr
4081                                          (gnus-data-find-list
4082                                           article
4083                                           (gnus-data-list t)))))
4084                                     ;; Error on the side of excessive subjects.
4085                                     (error ""))
4086                                   (mail-header-subject header)))
4087                                 ""
4088                               (mail-header-subject header))
4089                             nil (cdr (assq article gnus-newsgroup-scored))
4090                             (memq article gnus-newsgroup-processable))
4091                            (point)))))
4092         (when (cdr datal)
4093           (gnus-data-update-list
4094            (cdr datal) 
4095            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4096
4097 (defun gnus-summary-update-article (article &optional iheader)
4098   "Update ARTICLE in the summary buffer."
4099   (set-buffer gnus-summary-buffer)
4100   (let* ((header (gnus-summary-article-header article))
4101          (id (mail-header-id header))
4102          (data (gnus-data-find article))
4103          (thread (gnus-id-to-thread id))
4104          (references (mail-header-references header))
4105          (parent
4106           (gnus-id-to-thread
4107            (or (gnus-parent-id
4108                 (when (and references
4109                            (not (equal "" references)))
4110                   references))
4111                "none")))
4112          (buffer-read-only nil)
4113          (old (car thread)))
4114     (when thread
4115       (unless iheader
4116         (setcar thread nil)
4117         (when parent
4118           (delq thread parent)))
4119       (if (gnus-summary-insert-subject id header)
4120           ;; Set the (possibly) new article number in the data structure.
4121           (gnus-data-set-number data (gnus-id-to-article id))
4122         (setcar thread old)
4123         nil))))
4124
4125 (defun gnus-rebuild-thread (id &optional line)
4126   "Rebuild the thread containing ID.
4127 If LINE, insert the rebuilt thread starting on line LINE."
4128   (let ((buffer-read-only nil)
4129         old-pos current thread data)
4130     (if (not gnus-show-threads)
4131         (setq thread (list (car (gnus-id-to-thread id))))
4132       ;; Get the thread this article is part of.
4133       (setq thread (gnus-remove-thread id)))
4134     (setq old-pos (gnus-point-at-bol))
4135     (setq current (save-excursion
4136                     (and (re-search-backward "[\r\n]" nil t)
4137                          (gnus-summary-article-number))))
4138     ;; If this is a gathered thread, we have to go some re-gathering.
4139     (when (stringp (car thread))
4140       (let ((subject (car thread))
4141             roots thr)
4142         (setq thread (cdr thread))
4143         (while thread
4144           (unless (memq (setq thr (gnus-id-to-thread
4145                                    (gnus-root-id
4146                                     (mail-header-id (caar thread)))))
4147                         roots)
4148             (push thr roots))
4149           (setq thread (cdr thread)))
4150         ;; We now have all (unique) roots.
4151         (if (= (length roots) 1)
4152             ;; All the loose roots are now one solid root.
4153             (setq thread (car roots))
4154           (setq thread (cons subject (gnus-sort-threads roots))))))
4155     (let (threads)
4156       ;; We then insert this thread into the summary buffer.
4157       (when line
4158         (goto-char (point-min))
4159         (forward-line (1- line)))
4160       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4161         (if gnus-show-threads
4162             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4163           (gnus-summary-prepare-unthreaded thread))
4164         (setq data (nreverse gnus-newsgroup-data))
4165         (setq threads gnus-newsgroup-threads))
4166       ;; We splice the new data into the data structure.
4167       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4168       ;;!!! then we want to insert at the beginning of the buffer.
4169       ;;!!! That happens to be true with Gnus now, but that may
4170       ;;!!! change in the future.  Perhaps.
4171       (gnus-data-enter-list
4172        (if line nil current) data (- (point) old-pos))
4173       (setq gnus-newsgroup-threads
4174             (nconc threads gnus-newsgroup-threads))
4175       (gnus-data-compute-positions))))
4176
4177 (defun gnus-number-to-header (number)
4178   "Return the header for article NUMBER."
4179   (let ((headers gnus-newsgroup-headers))
4180     (while (and headers
4181                 (not (= number (mail-header-number (car headers)))))
4182       (pop headers))
4183     (when headers
4184       (car headers))))
4185
4186 (defun gnus-parent-headers (in-headers &optional generation)
4187   "Return the headers of the GENERATIONeth parent of HEADERS."
4188   (unless generation
4189     (setq generation 1))
4190   (let ((parent t)
4191         (headers in-headers)
4192         references)
4193     (while (and parent
4194                 (not (zerop generation))
4195                 (setq references (mail-header-references headers)))
4196       (setq headers (if (and references
4197                              (setq parent (gnus-parent-id references)))
4198                         (car (gnus-id-to-thread parent))
4199                       nil))
4200       (decf generation))
4201     (and (not (eq headers in-headers))
4202          headers)))
4203
4204 (defun gnus-id-to-thread (id)
4205   "Return the (sub-)thread where ID appears."
4206   (gnus-gethash id gnus-newsgroup-dependencies))
4207
4208 (defun gnus-id-to-article (id)
4209   "Return the article number of ID."
4210   (let ((thread (gnus-id-to-thread id)))
4211     (when (and thread
4212                (car thread))
4213       (mail-header-number (car thread)))))
4214
4215 (defun gnus-id-to-header (id)
4216   "Return the article headers of ID."
4217   (car (gnus-id-to-thread id)))
4218
4219 (defun gnus-article-displayed-root-p (article)
4220   "Say whether ARTICLE is a root(ish) article."
4221   (let ((level (gnus-summary-thread-level article))
4222         (refs (mail-header-references  (gnus-summary-article-header article)))
4223         particle)
4224     (cond
4225      ((null level) nil)
4226      ((zerop level) t)
4227      ((null refs) t)
4228      ((null (gnus-parent-id refs)) t)
4229      ((and (= 1 level)
4230            (null (setq particle (gnus-id-to-article
4231                                  (gnus-parent-id refs))))
4232            (null (gnus-summary-thread-level particle)))))))
4233
4234 (defun gnus-root-id (id)
4235   "Return the id of the root of the thread where ID appears."
4236   (let (last-id prev)
4237     (while (and id (setq prev (car (gnus-id-to-thread id))))
4238       (setq last-id id
4239             id (gnus-parent-id (mail-header-references prev))))
4240     last-id))
4241
4242 (defun gnus-articles-in-thread (thread)
4243   "Return the list of articles in THREAD."
4244   (cons (mail-header-number (car thread))
4245         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4246
4247 (defun gnus-remove-thread (id &optional dont-remove)
4248   "Remove the thread that has ID in it."
4249   (let (headers thread last-id)
4250     ;; First go up in this thread until we find the root.
4251     (setq last-id (gnus-root-id id)
4252           headers (message-flatten-list (gnus-id-to-thread last-id)))
4253     ;; We have now found the real root of this thread.  It might have
4254     ;; been gathered into some loose thread, so we have to search
4255     ;; through the threads to find the thread we wanted.
4256     (let ((threads gnus-newsgroup-threads)
4257           sub)
4258       (while threads
4259         (setq sub (car threads))
4260         (if (stringp (car sub))
4261             ;; This is a gathered thread, so we look at the roots
4262             ;; below it to find whether this article is in this
4263             ;; gathered root.
4264             (progn
4265               (setq sub (cdr sub))
4266               (while sub
4267                 (when (member (caar sub) headers)
4268                   (setq thread (car threads)
4269                         threads nil
4270                         sub nil))
4271                 (setq sub (cdr sub))))
4272           ;; It's an ordinary thread, so we check it.
4273           (when (eq (car sub) (car headers))
4274             (setq thread sub
4275                   threads nil)))
4276         (setq threads (cdr threads)))
4277       ;; If this article is in no thread, then it's a root.
4278       (if thread
4279           (unless dont-remove
4280             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4281         (setq thread (gnus-id-to-thread last-id)))
4282       (when thread
4283         (prog1
4284             thread                      ; We return this thread.
4285           (unless dont-remove
4286             (if (stringp (car thread))
4287                 (progn
4288                   ;; If we use dummy roots, then we have to remove the
4289                   ;; dummy root as well.
4290                   (when (eq gnus-summary-make-false-root 'dummy)
4291                     ;; We go to the dummy root by going to
4292                     ;; the first sub-"thread", and then one line up.
4293                     (gnus-summary-goto-article
4294                      (mail-header-number (caadr thread)))
4295                     (forward-line -1)
4296                     (gnus-delete-line)
4297                     (gnus-data-compute-positions))
4298                   (setq thread (cdr thread))
4299                   (while thread
4300                     (gnus-remove-thread-1 (car thread))
4301                     (setq thread (cdr thread))))
4302               (gnus-remove-thread-1 thread))))))))
4303
4304 (defun gnus-remove-thread-1 (thread)
4305   "Remove the thread THREAD recursively."
4306   (let ((number (mail-header-number (pop thread)))
4307         d)
4308     (setq thread (reverse thread))
4309     (while thread
4310       (gnus-remove-thread-1 (pop thread)))
4311     (when (setq d (gnus-data-find number))
4312       (goto-char (gnus-data-pos d))
4313       (gnus-summary-show-thread)
4314       (gnus-data-remove
4315        number
4316        (- (gnus-point-at-bol)
4317           (prog1
4318               (1+ (gnus-point-at-eol))
4319             (gnus-delete-line)))))))
4320
4321 (defun gnus-sort-threads-1 (threads func)
4322   (sort (mapcar (lambda (thread)
4323                   (cons (car thread)
4324                         (and (cdr thread)
4325                              (gnus-sort-threads-1 (cdr thread) func))))
4326                 threads) func))
4327
4328 (defun gnus-sort-threads (threads)
4329   "Sort THREADS."
4330   (if (not gnus-thread-sort-functions)
4331       threads
4332     (gnus-message 8 "Sorting threads...")
4333     (let ((max-lisp-eval-depth 5000))
4334       (prog1 (gnus-sort-threads-1
4335          threads
4336          (gnus-make-sort-function gnus-thread-sort-functions))
4337         (gnus-message 8 "Sorting threads...done")))))
4338
4339 (defun gnus-sort-articles (articles)
4340   "Sort ARTICLES."
4341   (when gnus-article-sort-functions
4342     (gnus-message 7 "Sorting articles...")
4343     (prog1
4344         (setq gnus-newsgroup-headers
4345               (sort articles (gnus-make-sort-function
4346                               gnus-article-sort-functions)))
4347       (gnus-message 7 "Sorting articles...done"))))
4348
4349 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4350 (defmacro gnus-thread-header (thread)
4351   "Return header of first article in THREAD.
4352 Note that THREAD must never, ever be anything else than a variable -
4353 using some other form will lead to serious barfage."
4354   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4355   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4356   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4357         (vector thread) 2))
4358
4359 (defsubst gnus-article-sort-by-number (h1 h2)
4360   "Sort articles by article number."
4361   (< (mail-header-number h1)
4362      (mail-header-number h2)))
4363
4364 (defun gnus-thread-sort-by-number (h1 h2)
4365   "Sort threads by root article number."
4366   (gnus-article-sort-by-number
4367    (gnus-thread-header h1) (gnus-thread-header h2)))
4368
4369 (defsubst gnus-article-sort-by-random (h1 h2)
4370   "Sort articles by article number."
4371   (zerop (random 2)))
4372
4373 (defun gnus-thread-sort-by-random (h1 h2)
4374   "Sort threads by root article number."
4375   (gnus-article-sort-by-random
4376    (gnus-thread-header h1) (gnus-thread-header h2)))
4377
4378 (defsubst gnus-article-sort-by-lines (h1 h2)
4379   "Sort articles by article Lines header."
4380   (< (mail-header-lines h1)
4381      (mail-header-lines h2)))
4382
4383 (defun gnus-thread-sort-by-lines (h1 h2)
4384   "Sort threads by root article Lines header."
4385   (gnus-article-sort-by-lines
4386    (gnus-thread-header h1) (gnus-thread-header h2)))
4387
4388 (defsubst gnus-article-sort-by-chars (h1 h2)
4389   "Sort articles by octet length."
4390   (< (mail-header-chars h1)
4391      (mail-header-chars h2)))
4392
4393 (defun gnus-thread-sort-by-chars (h1 h2)
4394   "Sort threads by root article octet length."
4395   (gnus-article-sort-by-chars
4396    (gnus-thread-header h1) (gnus-thread-header h2)))
4397
4398 (defsubst gnus-article-sort-by-author (h1 h2)
4399   "Sort articles by root author."
4400   (string-lessp
4401    (let ((addr (car (mime-entity-read-field h1 'From))))
4402      (or (std11-full-name-string addr)
4403          (std11-address-string addr)
4404          ""))
4405    (let ((addr (car (mime-entity-read-field h2 'From))))
4406      (or (std11-full-name-string addr)
4407          (std11-address-string addr)
4408          ""))
4409    ))
4410
4411 (defun gnus-thread-sort-by-author (h1 h2)
4412   "Sort threads by root author."
4413   (gnus-article-sort-by-author
4414    (gnus-thread-header h1)  (gnus-thread-header h2)))
4415
4416 (defsubst gnus-article-sort-by-subject (h1 h2)
4417   "Sort articles by root subject."
4418   (string-lessp
4419    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4420    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4421
4422 (defun gnus-thread-sort-by-subject (h1 h2)
4423   "Sort threads by root subject."
4424   (gnus-article-sort-by-subject
4425    (gnus-thread-header h1) (gnus-thread-header h2)))
4426
4427 (defsubst gnus-article-sort-by-date (h1 h2)
4428   "Sort articles by root article date."
4429   (time-less-p
4430    (gnus-date-get-time (mail-header-date h1))
4431    (gnus-date-get-time (mail-header-date h2))))
4432
4433 (defun gnus-thread-sort-by-date (h1 h2)
4434   "Sort threads by root article date."
4435   (gnus-article-sort-by-date
4436    (gnus-thread-header h1) (gnus-thread-header h2)))
4437
4438 (defsubst gnus-article-sort-by-score (h1 h2)
4439   "Sort articles by root article score.
4440 Unscored articles will be counted as having a score of zero."
4441   (> (or (cdr (assq (mail-header-number h1)
4442                     gnus-newsgroup-scored))
4443          gnus-summary-default-score 0)
4444      (or (cdr (assq (mail-header-number h2)
4445                     gnus-newsgroup-scored))
4446          gnus-summary-default-score 0)))
4447
4448 (defun gnus-thread-sort-by-score (h1 h2)
4449   "Sort threads by root article score."
4450   (gnus-article-sort-by-score
4451    (gnus-thread-header h1) (gnus-thread-header h2)))
4452
4453 (defun gnus-thread-sort-by-total-score (h1 h2)
4454   "Sort threads by the sum of all scores in the thread.
4455 Unscored articles will be counted as having a score of zero."
4456   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4457
4458 (defun gnus-thread-total-score (thread)
4459   ;; This function find the total score of THREAD.
4460   (cond
4461    ((null thread)
4462     0)
4463    ((consp thread)
4464     (if (stringp (car thread))
4465         (apply gnus-thread-score-function 0
4466                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4467       (gnus-thread-total-score-1 thread)))
4468    (t
4469     (gnus-thread-total-score-1 (list thread)))))
4470
4471 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4472   "Sort threads such that the thread with the most recently arrived article comes first."
4473   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4474
4475 (defun gnus-thread-highest-number (thread)
4476   "Return the highest article number in THREAD."
4477   (apply 'max (mapcar (lambda (header)
4478                         (mail-header-number header))
4479                       (message-flatten-list thread))))
4480
4481 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4482   "Sort threads such that the thread with the most recently dated article comes first."
4483   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4484
4485 (defun gnus-thread-latest-date (thread)
4486   "Return the highest article date in THREAD."
4487   (let ((previous-time 0))
4488     (apply 'max
4489            (mapcar
4490             (lambda (header)
4491               (setq previous-time
4492                     (time-to-seconds
4493                      (condition-case ()
4494                          (mail-header-parse-date (mail-header-date header))
4495                        (error previous-time)))))
4496             (sort
4497              (message-flatten-list thread)
4498              (lambda (h1 h2)
4499                (< (mail-header-number h1)
4500                   (mail-header-number h2))))))))
4501
4502 (defun gnus-thread-total-score-1 (root)
4503   ;; This function find the total score of the thread below ROOT.
4504   (setq root (car root))
4505   (apply gnus-thread-score-function
4506          (or (append
4507               (mapcar 'gnus-thread-total-score
4508                       (cdr (gnus-id-to-thread (mail-header-id root))))
4509               (when (> (mail-header-number root) 0)
4510                 (list (or (cdr (assq (mail-header-number root)
4511                                      gnus-newsgroup-scored))
4512                           gnus-summary-default-score 0))))
4513              (list gnus-summary-default-score)
4514              '(0))))
4515
4516 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4517 (defvar gnus-tmp-prev-subject nil)
4518 (defvar gnus-tmp-false-parent nil)
4519 (defvar gnus-tmp-root-expunged nil)
4520 (defvar gnus-tmp-dummy-line nil)
4521
4522 (eval-when-compile (defvar gnus-tmp-header))
4523 (defun gnus-extra-header (type &optional header)
4524   "Return the extra header of TYPE."
4525   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4526       ""))
4527
4528 (defvar gnus-tmp-thread-tree-header-string "")
4529
4530 (defcustom gnus-sum-thread-tree-root "> "
4531   "With %B spec, used for the root of a thread.
4532 If nil, use subject instead."
4533   :type 'string
4534   :group 'gnus-thread)
4535 (defcustom gnus-sum-thread-tree-single-indent ""
4536   "With %B spec, used for a thread with just one message.
4537 If nil, use subject instead."
4538   :type 'string
4539   :group 'gnus-thread)
4540 (defcustom gnus-sum-thread-tree-vertical "| "
4541   "With %B spec, used for drawing a vertical line."
4542   :type 'string
4543   :group 'gnus-thread)
4544 (defcustom gnus-sum-thread-tree-indent "  "
4545   "With %B spec, used for indenting."
4546   :type 'string
4547   :group 'gnus-thread)
4548 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4549   "With %B spec, used for a leaf with brothers."
4550   :type 'string
4551   :group 'gnus-thread)
4552 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4553   "With %B spec, used for a leaf without brothers."
4554   :type 'string
4555   :group 'gnus-thread)
4556
4557 (defun gnus-summary-prepare-threads (threads)
4558   "Prepare summary buffer from THREADS and indentation LEVEL.
4559 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4560 or a straight list of headers."
4561   (gnus-message 7 "Generating summary...")
4562
4563   (setq gnus-newsgroup-threads threads)
4564   (beginning-of-line)
4565
4566   (let ((gnus-tmp-level 0)
4567         (default-score (or gnus-summary-default-score 0))
4568         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4569         (building-line-count gnus-summary-display-while-building)
4570         (building-count (integerp gnus-summary-display-while-building))
4571         thread number subject stack state gnus-tmp-gathered beg-match
4572         new-roots gnus-tmp-new-adopts thread-end simp-subject
4573         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4574         gnus-tmp-replied gnus-tmp-subject-or-nil
4575         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4576         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4577         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4578         tree-stack)
4579
4580     (setq gnus-tmp-prev-subject nil
4581           gnus-tmp-thread-tree-header-string "")
4582
4583     (if (vectorp (car threads))
4584         ;; If this is a straight (sic) list of headers, then a
4585         ;; threaded summary display isn't required, so we just create
4586         ;; an unthreaded one.
4587         (gnus-summary-prepare-unthreaded threads)
4588
4589       ;; Do the threaded display.
4590
4591       (if gnus-summary-display-while-building
4592           (switch-to-buffer (buffer-name)))
4593       (while (or threads stack gnus-tmp-new-adopts new-roots)
4594
4595         (if (and (= gnus-tmp-level 0)
4596                  (or (not stack)
4597                      (= (caar stack) 0))
4598                  (not gnus-tmp-false-parent)
4599                  (or gnus-tmp-new-adopts new-roots))
4600             (if gnus-tmp-new-adopts
4601                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4602                       thread (list (car gnus-tmp-new-adopts))
4603                       gnus-tmp-header (caar thread)
4604                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4605               (when new-roots
4606                 (setq thread (list (car new-roots))
4607                       gnus-tmp-header (caar thread)
4608                       new-roots (cdr new-roots))))
4609
4610           (if threads
4611               ;; If there are some threads, we do them before the
4612               ;; threads on the stack.
4613               (setq thread threads
4614                     gnus-tmp-header (caar thread))
4615             ;; There were no current threads, so we pop something off
4616             ;; the stack.
4617             (setq state (car stack)
4618                   gnus-tmp-level (car state)
4619                   tree-stack (cadr state)
4620                   thread (caddr state)
4621                   stack (cdr stack)
4622                   gnus-tmp-header (caar thread))))
4623
4624         (setq gnus-tmp-false-parent nil)
4625         (setq gnus-tmp-root-expunged nil)
4626         (setq thread-end nil)
4627
4628         (if (stringp gnus-tmp-header)
4629             ;; The header is a dummy root.
4630             (cond
4631              ((eq gnus-summary-make-false-root 'adopt)
4632               ;; We let the first article adopt the rest.
4633               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4634                                                (cddar thread)))
4635               (setq gnus-tmp-gathered
4636                     (nconc (mapcar
4637                             (lambda (h) (mail-header-number (car h)))
4638                             (cddar thread))
4639                            gnus-tmp-gathered))
4640               (setq thread (cons (list (caar thread)
4641                                        (cadar thread))
4642                                  (cdr thread)))
4643               (setq gnus-tmp-level -1
4644                     gnus-tmp-false-parent t))
4645              ((eq gnus-summary-make-false-root 'empty)
4646               ;; We print adopted articles with empty subject fields.
4647               (setq gnus-tmp-gathered
4648                     (nconc (mapcar
4649                             (lambda (h) (mail-header-number (car h)))
4650                             (cddar thread))
4651                            gnus-tmp-gathered))
4652               (setq gnus-tmp-level -1))
4653              ((eq gnus-summary-make-false-root 'dummy)
4654               ;; We remember that we probably want to output a dummy
4655               ;; root.
4656               (setq gnus-tmp-dummy-line gnus-tmp-header)
4657               (setq gnus-tmp-prev-subject gnus-tmp-header))
4658              (t
4659               ;; We do not make a root for the gathered
4660               ;; sub-threads at all.
4661               (setq gnus-tmp-level -1)))
4662
4663           (setq number (mail-header-number gnus-tmp-header)
4664                 subject (mail-header-subject gnus-tmp-header)
4665                 simp-subject (gnus-simplify-subject-fully subject))
4666
4667           (cond
4668            ;; If the thread has changed subject, we might want to make
4669            ;; this subthread into a root.
4670            ((and (null gnus-thread-ignore-subject)
4671                  (not (zerop gnus-tmp-level))
4672                  gnus-tmp-prev-subject
4673                  (not (string= gnus-tmp-prev-subject simp-subject)))
4674             (setq new-roots (nconc new-roots (list (car thread)))
4675                   thread-end t
4676                   gnus-tmp-header nil))
4677            ;; If the article lies outside the current limit,
4678            ;; then we do not display it.
4679            ((not (memq number gnus-newsgroup-limit))
4680             (setq gnus-tmp-gathered
4681                   (nconc (mapcar
4682                           (lambda (h) (mail-header-number (car h)))
4683                           (cdar thread))
4684                          gnus-tmp-gathered))
4685             (setq gnus-tmp-new-adopts (if (cdar thread)
4686                                           (append gnus-tmp-new-adopts
4687                                                   (cdar thread))
4688                                         gnus-tmp-new-adopts)
4689                   thread-end t
4690                   gnus-tmp-header nil)
4691             (when (zerop gnus-tmp-level)
4692               (setq gnus-tmp-root-expunged t)))
4693            ;; Perhaps this article is to be marked as read?
4694            ((and gnus-summary-mark-below
4695                  (< (or (cdr (assq number gnus-newsgroup-scored))
4696                         default-score)
4697                     gnus-summary-mark-below)
4698                  ;; Don't touch sparse articles.
4699                  (not (gnus-summary-article-sparse-p number))
4700                  (not (gnus-summary-article-ancient-p number)))
4701             (setq gnus-newsgroup-unreads
4702                   (delq number gnus-newsgroup-unreads))
4703             (if gnus-newsgroup-auto-expire
4704                 (setq gnus-newsgroup-expirable
4705                       (gnus-add-to-sorted-list
4706                        gnus-newsgroup-expirable number))
4707               (push (cons number gnus-low-score-mark)
4708                     gnus-newsgroup-reads))))
4709
4710           (when gnus-tmp-header
4711             ;; We may have an old dummy line to output before this
4712             ;; article.
4713             (when (and gnus-tmp-dummy-line
4714                        (gnus-subject-equal
4715                         gnus-tmp-dummy-line
4716                         (mail-header-subject gnus-tmp-header)))
4717               (gnus-summary-insert-dummy-line
4718                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4719               (setq gnus-tmp-dummy-line nil))
4720
4721             ;; Compute the mark.
4722             (setq gnus-tmp-unread (gnus-article-mark number))
4723
4724             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4725                                   gnus-tmp-header gnus-tmp-level)
4726                   gnus-newsgroup-data)
4727
4728             ;; Actually insert the line.
4729             (setq
4730              gnus-tmp-subject-or-nil
4731              (cond
4732               ((and gnus-thread-ignore-subject
4733                     gnus-tmp-prev-subject
4734                     (not (string= gnus-tmp-prev-subject simp-subject)))
4735                subject)
4736               ((zerop gnus-tmp-level)
4737                (if (and (eq gnus-summary-make-false-root 'empty)
4738                         (memq number gnus-tmp-gathered)
4739                         gnus-tmp-prev-subject
4740                         (string= gnus-tmp-prev-subject simp-subject))
4741                    gnus-summary-same-subject
4742                  subject))
4743               (t gnus-summary-same-subject)))
4744             (if (and (eq gnus-summary-make-false-root 'adopt)
4745                      (= gnus-tmp-level 1)
4746                      (memq number gnus-tmp-gathered))
4747                 (setq gnus-tmp-opening-bracket ?\<
4748                       gnus-tmp-closing-bracket ?\>)
4749               (setq gnus-tmp-opening-bracket ?\[
4750                     gnus-tmp-closing-bracket ?\]))
4751             (setq
4752              gnus-tmp-indentation
4753              (aref gnus-thread-indent-array gnus-tmp-level)
4754              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4755              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4756                                 gnus-summary-default-score 0)
4757              gnus-tmp-score-char
4758              (if (or (null gnus-summary-default-score)
4759                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4760                          gnus-summary-zcore-fuzz))
4761                  ?\ ;;;Whitespace
4762                (if (< gnus-tmp-score gnus-summary-default-score)
4763                    gnus-score-below-mark gnus-score-over-mark))
4764              gnus-tmp-replied
4765              (cond ((memq number gnus-newsgroup-processable)
4766                     gnus-process-mark)
4767                    ((memq number gnus-newsgroup-cached)
4768                     gnus-cached-mark)
4769                    ((memq number gnus-newsgroup-replied)
4770                     gnus-replied-mark)
4771                    ((memq number gnus-newsgroup-forwarded)
4772                     gnus-forwarded-mark)
4773                    ((memq number gnus-newsgroup-saved)
4774                     gnus-saved-mark)
4775                    ((memq number gnus-newsgroup-recent)
4776                     gnus-recent-mark)
4777                    ((memq number gnus-newsgroup-unseen)
4778                     gnus-unseen-mark)
4779                    (t gnus-no-mark))
4780              gnus-tmp-downloaded
4781              (cond ((memq number gnus-newsgroup-undownloaded) 
4782                     gnus-undownloaded-mark)
4783                    (gnus-newsgroup-agentized
4784                     gnus-downloaded-mark)
4785                    (t
4786                     gnus-no-mark))
4787              gnus-tmp-from (mail-header-from gnus-tmp-header)
4788              gnus-tmp-name
4789              (cond
4790               ((string-match "<[^>]+> *$" gnus-tmp-from)
4791                (setq beg-match (match-beginning 0))
4792                (or (and (string-match "^\".+\"" gnus-tmp-from)
4793                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4794                    (substring gnus-tmp-from 0 beg-match)))
4795               ((string-match "(.+)" gnus-tmp-from)
4796                (substring gnus-tmp-from
4797                           (1+ (match-beginning 0)) (1- (match-end 0))))
4798               (t gnus-tmp-from))
4799              gnus-tmp-thread-tree-header-string
4800              (cond
4801               ((not gnus-show-threads) "")
4802               ((zerop gnus-tmp-level)
4803                (if (cdar thread)
4804                    (or gnus-sum-thread-tree-root subject)
4805                  (or gnus-sum-thread-tree-single-indent subject)))
4806               (t
4807                (concat (apply 'concat
4808                               (mapcar (lambda (item)
4809                                         (if (= item 1)
4810                                             gnus-sum-thread-tree-vertical
4811                                           gnus-sum-thread-tree-indent))
4812                                       (cdr (reverse tree-stack))))
4813                        (if (nth 1 thread)
4814                            gnus-sum-thread-tree-leaf-with-other
4815                          gnus-sum-thread-tree-single-leaf)))))
4816             (when (string= gnus-tmp-name "")
4817               (setq gnus-tmp-name gnus-tmp-from))
4818             (unless (numberp gnus-tmp-lines)
4819               (setq gnus-tmp-lines -1))
4820             (if (= gnus-tmp-lines -1)
4821                 (setq gnus-tmp-lines "?")
4822               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4823               (gnus-put-text-property
4824              (point)
4825              (progn (eval gnus-summary-line-format-spec) (point))
4826                'gnus-number number)
4827             (when gnus-visual-p
4828               (forward-line -1)
4829               (gnus-run-hooks 'gnus-summary-update-hook)
4830               (forward-line 1))
4831
4832             (setq gnus-tmp-prev-subject simp-subject)))
4833
4834         (when (nth 1 thread)
4835           (push (list (max 0 gnus-tmp-level)
4836                       (copy-list tree-stack)
4837                       (nthcdr 1 thread))
4838                 stack))
4839         (push (if (nth 1 thread) 1 0) tree-stack)
4840         (incf gnus-tmp-level)
4841         (setq threads (if thread-end nil (cdar thread)))
4842         (if gnus-summary-display-while-building
4843             (if building-count
4844                 (progn
4845                   ;; use a set frequency
4846                   (setq building-line-count (1- building-line-count))
4847                   (when (= building-line-count 0)
4848                     (sit-for 0)
4849                     (setq building-line-count
4850                           gnus-summary-display-while-building)))
4851               ;; always
4852               (sit-for 0)))
4853         (unless threads
4854           (setq gnus-tmp-level 0)))))
4855   (gnus-message 7 "Generating summary...done"))
4856
4857 (defun gnus-summary-prepare-unthreaded (headers)
4858   "Generate an unthreaded summary buffer based on HEADERS."
4859   (let (header number mark)
4860
4861     (beginning-of-line)
4862
4863     (while headers
4864       ;; We may have to root out some bad articles...
4865       (when (memq (setq number (mail-header-number
4866                                 (setq header (pop headers))))
4867                   gnus-newsgroup-limit)
4868         ;; Mark article as read when it has a low score.
4869         (when (and gnus-summary-mark-below
4870                    (< (or (cdr (assq number gnus-newsgroup-scored))
4871                           gnus-summary-default-score 0)
4872                       gnus-summary-mark-below)
4873                    (not (gnus-summary-article-ancient-p number)))
4874           (setq gnus-newsgroup-unreads
4875                 (delq number gnus-newsgroup-unreads))
4876           (if gnus-newsgroup-auto-expire
4877               (push number gnus-newsgroup-expirable)
4878             (push (cons number gnus-low-score-mark)
4879                   gnus-newsgroup-reads)))
4880
4881         (setq mark (gnus-article-mark number))
4882         (push (gnus-data-make number mark (1+ (point)) header 0)
4883               gnus-newsgroup-data)
4884         (gnus-summary-insert-line
4885          header 0 number
4886          (memq number gnus-newsgroup-undownloaded)
4887          mark (memq number gnus-newsgroup-replied)
4888          (memq number gnus-newsgroup-expirable)
4889          (mail-header-subject header) nil
4890          (cdr (assq number gnus-newsgroup-scored))
4891          (memq number gnus-newsgroup-processable))))))
4892
4893 (defun gnus-summary-remove-list-identifiers ()
4894   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4895   (let ((regexp (if (consp gnus-list-identifiers)
4896                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4897                   gnus-list-identifiers))
4898         changed subject)
4899     (when regexp
4900       (dolist (header gnus-newsgroup-headers)
4901         (setq subject (mail-header-subject header)
4902               changed nil)
4903         (while (string-match
4904                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4905                 subject)
4906           (setq subject
4907                 (concat (substring subject 0 (match-beginning 2))
4908                         (substring subject (match-end 0)))
4909                 changed t))
4910         (when (and changed
4911                    (string-match
4912                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4913           (setq subject
4914                 (concat (substring subject 0 (match-beginning 1))
4915                         (substring subject (match-end 1)))))
4916         (when changed
4917           (mail-header-set-subject header subject))))))
4918
4919 (defun gnus-fetch-headers (articles)
4920   "Fetch headers of ARTICLES."
4921   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4922     (gnus-message 5 "Fetching headers for %s..." name)
4923     (prog1
4924         (if (eq 'nov
4925                 (setq gnus-headers-retrieved-by
4926                       (gnus-retrieve-headers
4927                        articles gnus-newsgroup-name
4928                        ;; We might want to fetch old headers, but
4929                        ;; not if there is only 1 article.
4930                        (and (or (and
4931                                  (not (eq gnus-fetch-old-headers 'some))
4932                                  (not (numberp gnus-fetch-old-headers)))
4933                                 (> (length articles) 1))
4934                             gnus-fetch-old-headers))))
4935             (gnus-get-newsgroup-headers-xover
4936              articles nil nil gnus-newsgroup-name t)
4937           (gnus-get-newsgroup-headers))
4938       (gnus-message 5 "Fetching headers for %s...done" name))))
4939
4940 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4941   "Select newsgroup GROUP.
4942 If READ-ALL is non-nil, all articles in the group are selected.
4943 If SELECT-ARTICLES, only select those articles from GROUP."
4944   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4945          ;;!!! Dirty hack; should be removed.
4946          (gnus-summary-ignore-duplicates
4947           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4948               t
4949             gnus-summary-ignore-duplicates))
4950          (info (nth 2 entry))
4951          articles fetched-articles cached)
4952
4953     (unless (gnus-check-server
4954              (set (make-local-variable 'gnus-current-select-method)
4955                   (gnus-find-method-for-group group)))
4956       (error "Couldn't open server"))
4957
4958     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4959         (gnus-activate-group group)     ; Or we can activate it...
4960         (progn                          ; Or we bug out.
4961           (when (equal major-mode 'gnus-summary-mode)
4962             (gnus-kill-buffer (current-buffer)))
4963           (error "Couldn't activate group %s: %s"
4964                  group (gnus-status-message group))))
4965
4966     (unless (gnus-request-group group t)
4967       (when (equal major-mode 'gnus-summary-mode)
4968         (gnus-kill-buffer (current-buffer)))
4969       (error "Couldn't request group %s: %s"
4970              group (gnus-status-message group)))
4971
4972     (setq gnus-newsgroup-name group
4973           gnus-newsgroup-unselected nil
4974           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4975
4976     (let ((display (gnus-group-find-parameter group 'display)))
4977       (setq gnus-newsgroup-display
4978             (cond
4979              ((not (zerop (or (car-safe read-all) 0)))
4980               ;; The user entered the group with C-u SPC/RET, let's show
4981               ;; all articles.
4982               'gnus-not-ignore)
4983              ((eq display 'all)
4984               'gnus-not-ignore)
4985              ((arrayp display)
4986               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4987              ((numberp display)
4988               ;; The following is probably the "correct" solution, but
4989               ;; it makes Gnus fetch all headers and then limit the
4990               ;; articles (which is slow), so instead we hack the
4991               ;; select-articles parameter instead. -- Simon Josefsson
4992               ;; <jas@kth.se>
4993               ;;
4994               ;; (gnus-byte-compile
4995               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4996               ;;                         display)))))
4997               (setq select-articles
4998                     (gnus-uncompress-range
4999                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5000                              (if (> tmp 0)
5001                                  tmp
5002                                1))
5003                            (cdr (gnus-active group)))))
5004               nil)
5005              (t
5006               nil))))
5007
5008     (gnus-summary-setup-default-charset)
5009
5010     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5011     (when (gnus-virtual-group-p group)
5012       (setq cached gnus-newsgroup-cached))
5013
5014     (setq gnus-newsgroup-unreads
5015           (gnus-sorted-ndifference
5016            (gnus-sorted-ndifference gnus-newsgroup-unreads
5017                                     gnus-newsgroup-marked)
5018            gnus-newsgroup-dormant))
5019
5020     (setq gnus-newsgroup-processable nil)
5021
5022     (gnus-update-read-articles group gnus-newsgroup-unreads)
5023
5024     ;; Adjust and set lists of article marks.
5025     (when info
5026       (gnus-adjust-marked-articles info))
5027     (if (setq articles select-articles)
5028         (setq gnus-newsgroup-unselected
5029               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5030       (setq articles (gnus-articles-to-read group read-all)))
5031
5032     (cond
5033      ((null articles)
5034       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5035       'quit)
5036      ((eq articles 0) nil)
5037      (t
5038       ;; Init the dependencies hash table.
5039       (setq gnus-newsgroup-dependencies
5040             (gnus-make-hashtable (length articles)))
5041       (gnus-set-global-variables)
5042       ;; Retrieve the headers and read them in.
5043
5044       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5045
5046       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5047       (when cached
5048         (setq gnus-newsgroup-cached cached))
5049
5050       ;; Suppress duplicates?
5051       (when gnus-suppress-duplicates
5052         (gnus-dup-suppress-articles))
5053
5054       ;; Set the initial limit.
5055       (setq gnus-newsgroup-limit (copy-sequence articles))
5056       ;; Remove canceled articles from the list of unread articles.
5057       (setq fetched-articles
5058             (mapcar (lambda (headers) (mail-header-number headers))
5059                     gnus-newsgroup-headers))
5060       (setq gnus-newsgroup-articles fetched-articles)
5061       (setq gnus-newsgroup-unreads
5062             (gnus-sorted-nintersection
5063              gnus-newsgroup-unreads fetched-articles))
5064       (gnus-compute-unseen-list)
5065
5066       ;; Removed marked articles that do not exist.
5067       (gnus-update-missing-marks
5068        (gnus-sorted-difference articles fetched-articles))
5069       ;; We might want to build some more threads first.
5070       (when (and gnus-fetch-old-headers
5071                  (eq gnus-headers-retrieved-by 'nov))
5072         (if (eq gnus-fetch-old-headers 'invisible)
5073             (gnus-build-all-threads)
5074           (gnus-build-old-threads)))
5075       ;; Let the Gnus agent mark articles as read.
5076       (when gnus-agent
5077         (gnus-agent-get-undownloaded-list))
5078       ;; Remove list identifiers from subject
5079       (when gnus-list-identifiers
5080         (gnus-summary-remove-list-identifiers))
5081       ;; Check whether auto-expire is to be done in this group.
5082       (setq gnus-newsgroup-auto-expire
5083             (gnus-group-auto-expirable-p group))
5084       ;; Set up the article buffer now, if necessary.
5085       (unless gnus-single-article-buffer
5086         (gnus-article-setup-buffer))
5087       ;; First and last article in this newsgroup.
5088       (when gnus-newsgroup-headers
5089         (setq gnus-newsgroup-begin
5090               (mail-header-number (car gnus-newsgroup-headers))
5091               gnus-newsgroup-end
5092               (mail-header-number
5093                (gnus-last-element gnus-newsgroup-headers))))
5094       ;; GROUP is successfully selected.
5095       (or gnus-newsgroup-headers t)))))
5096
5097 (defun gnus-compute-unseen-list ()
5098   ;; The `seen' marks are treated specially.
5099   (if (not gnus-newsgroup-seen)
5100       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5101     (setq gnus-newsgroup-unseen
5102           (gnus-inverse-list-range-intersection
5103            gnus-newsgroup-articles gnus-newsgroup-seen))))
5104
5105 (defun gnus-summary-display-make-predicate (display)
5106   (require 'gnus-agent)
5107   (when (= (length display) 1)
5108     (setq display (car display)))
5109   (unless gnus-summary-display-cache
5110     (dolist (elem (append '((unread . unread)
5111                             (read . read)
5112                             (unseen . unseen))
5113                           gnus-article-mark-lists))
5114       (push (cons (cdr elem)
5115                   (gnus-byte-compile
5116                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5117             gnus-summary-display-cache)))
5118   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5119         (gnus-category-predicate-cache gnus-summary-display-cache))
5120     (gnus-get-predicate display)))
5121
5122 ;; Uses the dynamically bound `number' variable.
5123 (eval-when-compile
5124   (defvar number))
5125 (defun gnus-article-marked-p (type &optional article)
5126   (let ((article (or article number)))
5127     (cond
5128      ((eq type 'tick)
5129       (memq article gnus-newsgroup-marked))
5130      ((eq type 'spam)
5131       (memq article gnus-newsgroup-spam-marked))
5132      ((eq type 'unsend)
5133       (memq article gnus-newsgroup-unsendable))
5134      ((eq type 'undownload)
5135       (memq article gnus-newsgroup-undownloaded))
5136      ((eq type 'download)
5137       (memq article gnus-newsgroup-downloadable))
5138      ((eq type 'unread)
5139       (memq article gnus-newsgroup-unreads))
5140      ((eq type 'read)
5141       (memq article gnus-newsgroup-reads))
5142      ((eq type 'dormant)
5143       (memq article gnus-newsgroup-dormant) )
5144      ((eq type 'expire)
5145       (memq article gnus-newsgroup-expirable))
5146      ((eq type 'reply)
5147       (memq article gnus-newsgroup-replied))
5148      ((eq type 'killed)
5149       (memq article gnus-newsgroup-killed))
5150      ((eq type 'bookmark)
5151       (assq article gnus-newsgroup-bookmarks))
5152      ((eq type 'score)
5153       (assq article gnus-newsgroup-scored))
5154      ((eq type 'save)
5155       (memq article gnus-newsgroup-saved))
5156      ((eq type 'cache)
5157       (memq article gnus-newsgroup-cached))
5158      ((eq type 'forward)
5159       (memq article gnus-newsgroup-forwarded))
5160      ((eq type 'seen)
5161       (not (memq article gnus-newsgroup-unseen)))
5162      ((eq type 'recent)
5163       (memq article gnus-newsgroup-recent))
5164      (t t))))
5165
5166 (defun gnus-articles-to-read (group &optional read-all)
5167   "Find out what articles the user wants to read."
5168   (let* ((display (gnus-group-find-parameter group 'display))
5169          (articles
5170           ;; Select all articles if `read-all' is non-nil, or if there
5171           ;; are no unread articles.
5172           (if (or read-all
5173                   (and (zerop (length gnus-newsgroup-marked))
5174                        (zerop (length gnus-newsgroup-unreads)))
5175                   ;; Fetch all if the predicate is non-nil.
5176                   gnus-newsgroup-display)
5177               ;; We want to select the headers for all the articles in
5178               ;; the group, so we select either all the active
5179               ;; articles in the group, or (if that's nil), the
5180               ;; articles in the cache.
5181               (or
5182                (gnus-uncompress-range (gnus-active group))
5183                (gnus-cache-articles-in-group group))
5184             ;; Select only the "normal" subset of articles.
5185             (gnus-sorted-nunion
5186              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5187              gnus-newsgroup-unreads)))
5188          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5189          (scored (length scored-list))
5190          (number (length articles))
5191          (marked (+ (length gnus-newsgroup-marked)
5192                     (length gnus-newsgroup-dormant)))
5193          (select
5194           (cond
5195            ((numberp read-all)
5196             read-all)
5197            ((numberp gnus-newsgroup-display)
5198             gnus-newsgroup-display)
5199            (t
5200             (condition-case ()
5201                 (cond
5202                  ((and (or (<= scored marked) (= scored number))
5203                        (numberp gnus-large-newsgroup)
5204                        (> number gnus-large-newsgroup))
5205                   (let* ((cursor-in-echo-area nil)
5206                          (initial (gnus-parameter-large-newsgroup-initial
5207                                    gnus-newsgroup-name))
5208                          (input
5209                           (read-string
5210                            (format
5211                             "How many articles from %s (%s %d): "
5212                             (gnus-limit-string
5213                              (gnus-group-decoded-name gnus-newsgroup-name)
5214                              35)
5215                             (if initial "max" "default")
5216                             number)
5217                            (if initial
5218                                (cons (number-to-string initial)
5219                                      0)))))
5220                     (if (string-match "^[ \t]*$" input) number input)))
5221                  ((and (> scored marked) (< scored number)
5222                        (> (- scored number) 20))
5223                   (let ((input
5224                          (read-string
5225                           (format "%s %s (%d scored, %d total): "
5226                                   "How many articles from"
5227                                   (gnus-group-decoded-name group)
5228                                   scored number))))
5229                     (if (string-match "^[ \t]*$" input)
5230                         number input)))
5231                  (t number))
5232               (quit
5233                (message "Quit getting the articles to read")
5234                nil))))))
5235     (setq select (if (stringp select) (string-to-number select) select))
5236     (if (or (null select) (zerop select))
5237         select
5238       (if (and (not (zerop scored)) (<= (abs select) scored))
5239           (progn
5240             (setq articles (sort scored-list '<))
5241             (setq number (length articles)))
5242         (setq articles (copy-sequence articles)))
5243
5244       (when (< (abs select) number)
5245         (if (< select 0)
5246             ;; Select the N oldest articles.
5247             (setcdr (nthcdr (1- (abs select)) articles) nil)
5248           ;; Select the N most recent articles.
5249           (setq articles (nthcdr (- number select) articles))))
5250       (setq gnus-newsgroup-unselected
5251             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5252       (when gnus-alter-articles-to-read-function
5253         (setq articles
5254               (sort
5255                (funcall gnus-alter-articles-to-read-function
5256                         gnus-newsgroup-name articles)
5257                '<)))
5258       articles)))
5259
5260 (defun gnus-killed-articles (killed articles)
5261   (let (out)
5262     (while articles
5263       (when (inline (gnus-member-of-range (car articles) killed))
5264         (push (car articles) out))
5265       (setq articles (cdr articles)))
5266     out))
5267
5268 (defun gnus-uncompress-marks (marks)
5269   "Uncompress the mark ranges in MARKS."
5270   (let ((uncompressed '(score bookmark))
5271         out)
5272     (while marks
5273       (if (memq (caar marks) uncompressed)
5274           (push (car marks) out)
5275         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5276       (setq marks (cdr marks)))
5277     out))
5278
5279 (defun gnus-article-mark-to-type (mark)
5280   "Return the type of MARK."
5281   (or (cadr (assq mark gnus-article-special-mark-lists))
5282       'list))
5283
5284 (defun gnus-article-unpropagatable-p (mark)
5285   "Return whether MARK should be propagated to backend."
5286   (memq mark gnus-article-unpropagated-mark-lists))
5287
5288 (defun gnus-adjust-marked-articles (info)
5289   "Set all article lists and remove all marks that are no longer valid."
5290   (let* ((marked-lists (gnus-info-marks info))
5291          (active (gnus-active (gnus-info-group info)))
5292          (min (car active))
5293          (max (cdr active))
5294          (types gnus-article-mark-lists)
5295          marks var articles article mark mark-type)
5296
5297     (dolist (marks marked-lists)
5298       (setq mark (car marks)
5299             mark-type (gnus-article-mark-to-type mark)
5300             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5301
5302       ;; We set the variable according to the type of the marks list,
5303       ;; and then adjust the marks to a subset of the active articles.
5304       (cond
5305        ;; Adjust "simple" lists.
5306        ((eq mark-type 'list)
5307         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5308         (when (memq mark '(tick dormant expire reply save))
5309           (while articles
5310             (when (or (< (setq article (pop articles)) min) (> article max))
5311               (set var (delq article (symbol-value var)))))))
5312        ;; Adjust assocs.
5313        ((eq mark-type 'tuple)
5314         (set var (setq articles (cdr marks)))
5315         (when (not (listp (cdr (symbol-value var))))
5316           (set var (list (symbol-value var))))
5317         (when (not (listp (cdr articles)))
5318           (setq articles (list articles)))
5319         (while articles
5320           (when (or (not (consp (setq article (pop articles))))
5321                     (< (car article) min)
5322                     (> (car article) max))
5323             (set var (delq article (symbol-value var))))))
5324        ;; Adjust ranges (sloppily).
5325        ((eq mark-type 'range)
5326         (cond
5327          ((eq mark 'seen)
5328           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5329           ;; It should be (seen (NUM1 . NUM2)).
5330           (when (numberp (cddr marks))
5331             (setcdr marks (list (cdr marks))))
5332           (setq articles (cdr marks))
5333           (while (and articles
5334                       (or (and (consp (car articles))
5335                                (> min (cdar articles)))
5336                           (and (numberp (car articles))
5337                                (> min (car articles)))))
5338             (pop articles))
5339           (set var articles))))))))
5340
5341 (defun gnus-update-missing-marks (missing)
5342   "Go through the list of MISSING articles and remove them from the mark lists."
5343   (when missing
5344     (let (var m)
5345       ;; Go through all types.
5346       (dolist (elem gnus-article-mark-lists)
5347         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5348           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5349           (when (symbol-value var)
5350             ;; This list has articles.  So we delete all missing
5351             ;; articles from it.
5352             (setq m missing)
5353             (while m
5354               (set var (delq (pop m) (symbol-value var))))))))))
5355
5356 (defun gnus-update-marks ()
5357   "Enter the various lists of marked articles into the newsgroup info list."
5358   (let ((types gnus-article-mark-lists)
5359         (info (gnus-get-info gnus-newsgroup-name))
5360         type list newmarked symbol delta-marks)
5361     (when info
5362       ;; Add all marks lists to the list of marks lists.
5363       (while (setq type (pop types))
5364         (setq list (symbol-value
5365                     (setq symbol
5366                           (intern (format "gnus-newsgroup-%s" (car type))))))
5367
5368         (when list
5369           ;; Get rid of the entries of the articles that have the
5370           ;; default score.
5371           (when (and (eq (cdr type) 'score)
5372                      gnus-save-score
5373                      list)
5374             (let* ((arts list)
5375                    (prev (cons nil list))
5376                    (all prev))
5377               (while arts
5378                 (if (or (not (consp (car arts)))
5379                         (= (cdar arts) gnus-summary-default-score))
5380                     (setcdr prev (cdr arts))
5381                   (setq prev arts))
5382                 (setq arts (cdr arts)))
5383               (setq list (cdr all)))))
5384
5385         (when (eq (cdr type) 'seen)
5386           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5387
5388         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5389           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5390
5391         (when (and (gnus-check-backend-function
5392                     'request-set-mark gnus-newsgroup-name)
5393                    (not (gnus-article-unpropagatable-p (cdr type))))
5394           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5395                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5396                  (add (gnus-remove-from-range
5397                        (gnus-copy-sequence list) old)))
5398             (when add
5399               (push (list add 'add (list (cdr type))) delta-marks))
5400             (when del
5401               (push (list del 'del (list (cdr type))) delta-marks))))
5402
5403         (when list
5404           (push (cons (cdr type) list) newmarked)))
5405
5406       (when delta-marks
5407         (unless (gnus-check-group gnus-newsgroup-name)
5408           (error "Can't open server for %s" gnus-newsgroup-name))
5409         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5410
5411       ;; Enter these new marks into the info of the group.
5412       (if (nthcdr 3 info)
5413           (setcar (nthcdr 3 info) newmarked)
5414         ;; Add the marks lists to the end of the info.
5415         (when newmarked
5416           (setcdr (nthcdr 2 info) (list newmarked))))
5417
5418       ;; Cut off the end of the info if there's nothing else there.
5419       (let ((i 5))
5420         (while (and (> i 2)
5421                     (not (nth i info)))
5422           (when (nthcdr (decf i) info)
5423             (setcdr (nthcdr i info) nil)))))))
5424
5425 (defun gnus-set-mode-line (where)
5426   "Set the mode line of the article or summary buffers.
5427 If WHERE is `summary', the summary mode line format will be used."
5428   ;; Is this mode line one we keep updated?
5429   (when (and (memq where gnus-updated-mode-lines)
5430              (symbol-value
5431               (intern (format "gnus-%s-mode-line-format-spec" where))))
5432     (let (mode-string)
5433       (save-excursion
5434         ;; We evaluate this in the summary buffer since these
5435         ;; variables are buffer-local to that buffer.
5436         (set-buffer gnus-summary-buffer)
5437         ;; We bind all these variables that are used in the `eval' form
5438         ;; below.
5439         (let* ((mformat (symbol-value
5440                          (intern
5441                           (format "gnus-%s-mode-line-format-spec" where))))
5442                (gnus-tmp-group-name (gnus-group-decoded-name
5443                                      gnus-newsgroup-name))
5444                (gnus-tmp-article-number (or gnus-current-article 0))
5445                (gnus-tmp-unread gnus-newsgroup-unreads)
5446                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5447                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5448                (gnus-tmp-unread-and-unselected
5449                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5450                             (zerop gnus-tmp-unselected))
5451                        "")
5452                       ((zerop gnus-tmp-unselected)
5453                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5454                       (t (format "{%d(+%d) more}"
5455                                  gnus-tmp-unread-and-unticked
5456                                  gnus-tmp-unselected))))
5457                (gnus-tmp-subject
5458                 (if (and gnus-current-headers
5459                          (vectorp gnus-current-headers))
5460                     (gnus-mode-string-quote
5461                      (mail-header-subject gnus-current-headers))
5462                   ""))
5463                bufname-length max-len
5464                gnus-tmp-header);; passed as argument to any user-format-funcs
5465           (setq mode-string (eval mformat))
5466           (setq bufname-length (if (string-match "%b" mode-string)
5467                                    (- (length
5468                                        (buffer-name
5469                                         (if (eq where 'summary)
5470                                             nil
5471                                           (get-buffer gnus-article-buffer))))
5472                                       2)
5473                                  0))
5474           (setq max-len (max 4 (if gnus-mode-non-string-length
5475                                    (- (window-width)
5476                                       gnus-mode-non-string-length
5477                                       bufname-length)
5478                                  (length mode-string))))
5479           ;; We might have to chop a bit of the string off...
5480           (when (> (length mode-string) max-len)
5481             (setq mode-string
5482                   (concat (gnus-truncate-string mode-string (- max-len 3))
5483                           "...")))
5484           ;; Pad the mode string a bit.
5485           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5486       ;; Update the mode line.
5487       (setq mode-line-buffer-identification
5488             (gnus-mode-line-buffer-identification (list mode-string)))
5489       (set-buffer-modified-p t))))
5490
5491 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5492   "Go through the HEADERS list and add all Xrefs to a hash table.
5493 The resulting hash table is returned, or nil if no Xrefs were found."
5494   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5495          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5496          (xref-hashtb (gnus-make-hashtable))
5497          start group entry number xrefs header)
5498     (while headers
5499       (setq header (pop headers))
5500       (when (and (setq xrefs (mail-header-xref header))
5501                  (not (memq (setq number (mail-header-number header))
5502                             unreads)))
5503         (setq start 0)
5504         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5505           (setq start (match-end 0))
5506           (setq group (if prefix
5507                           (concat prefix (substring xrefs (match-beginning 1)
5508                                                     (match-end 1)))
5509                         (substring xrefs (match-beginning 1) (match-end 1))))
5510           (setq number
5511                 (string-to-int (substring xrefs (match-beginning 2)
5512                                           (match-end 2))))
5513           (if (setq entry (gnus-gethash group xref-hashtb))
5514               (setcdr entry (cons number (cdr entry)))
5515             (gnus-sethash group (cons number nil) xref-hashtb)))))
5516     (and start xref-hashtb)))
5517
5518 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5519   "Look through all the headers and mark the Xrefs as read."
5520   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5521         name entry info xref-hashtb idlist method nth4)
5522     (save-excursion
5523       (set-buffer gnus-group-buffer)
5524       (when (setq xref-hashtb
5525                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5526         (mapatoms
5527          (lambda (group)
5528            (unless (string= from-newsgroup (setq name (symbol-name group)))
5529              (setq idlist (symbol-value group))
5530              ;; Dead groups are not updated.
5531              (and (prog1
5532                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5533                             info (nth 2 entry))
5534                     (when (stringp (setq nth4 (gnus-info-method info)))
5535                       (setq nth4 (gnus-server-to-method nth4))))
5536                   ;; Only do the xrefs if the group has the same
5537                   ;; select method as the group we have just read.
5538                   (or (gnus-methods-equal-p
5539                        nth4 (gnus-find-method-for-group from-newsgroup))
5540                       virtual
5541                       (equal nth4 (setq method (gnus-find-method-for-group
5542                                                 from-newsgroup)))
5543                       (and (equal (car nth4) (car method))
5544                            (equal (nth 1 nth4) (nth 1 method))))
5545                   gnus-use-cross-reference
5546                   (or (not (eq gnus-use-cross-reference t))
5547                       virtual
5548                       ;; Only do cross-references on subscribed
5549                       ;; groups, if that is what is wanted.
5550                       (<= (gnus-info-level info) gnus-level-subscribed))
5551                   (gnus-group-make-articles-read name idlist))))
5552          xref-hashtb)))))
5553
5554 (defun gnus-compute-read-articles (group articles)
5555   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5556          (info (nth 2 entry))
5557          (active (gnus-active group))
5558          ninfo)
5559     (when entry
5560       ;; First peel off all invalid article numbers.
5561       (when active
5562         (let ((ids articles)
5563               id first)
5564           (while (setq id (pop ids))
5565             (when (and first (> id (cdr active)))
5566               ;; We'll end up in this situation in one particular
5567               ;; obscure situation.  If you re-scan a group and get
5568               ;; a new article that is cross-posted to a different
5569               ;; group that has not been re-scanned, you might get
5570               ;; crossposted article that has a higher number than
5571               ;; Gnus believes possible.  So we re-activate this
5572               ;; group as well.  This might mean doing the
5573               ;; crossposting thingy will *increase* the number
5574               ;; of articles in some groups.  Tsk, tsk.
5575               (setq active (or (gnus-activate-group group) active)))
5576             (when (or (> id (cdr active))
5577                       (< id (car active)))
5578               (setq articles (delq id articles))))))
5579       ;; If the read list is nil, we init it.
5580       (if (and active
5581                (null (gnus-info-read info))
5582                (> (car active) 1))
5583           (setq ninfo (cons 1 (1- (car active))))
5584         (setq ninfo (gnus-info-read info)))
5585       ;; Then we add the read articles to the range.
5586       (gnus-add-to-range
5587        ninfo (setq articles (sort articles '<))))))
5588
5589 (defun gnus-group-make-articles-read (group articles)
5590   "Update the info of GROUP to say that ARTICLES are read."
5591   (let* ((num 0)
5592          (entry (gnus-gethash group gnus-newsrc-hashtb))
5593          (info (nth 2 entry))
5594          (active (gnus-active group))
5595          range)
5596     (when entry
5597       (setq range (gnus-compute-read-articles group articles))
5598       (save-excursion
5599         (set-buffer gnus-group-buffer)
5600         (gnus-undo-register
5601           `(progn
5602              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5603              (gnus-info-set-read ',info ',(gnus-info-read info))
5604              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5605              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5606              (gnus-group-update-group ,group t))))
5607       ;; Add the read articles to the range.
5608       (gnus-info-set-read info range)
5609       (gnus-request-set-mark group (list (list range 'add '(read))))
5610       ;; Then we have to re-compute how many unread
5611       ;; articles there are in this group.
5612       (when active
5613         (cond
5614          ((not range)
5615           (setq num (- (1+ (cdr active)) (car active))))
5616          ((not (listp (cdr range)))
5617           (setq num (- (cdr active) (- (1+ (cdr range))
5618                                        (car range)))))
5619          (t
5620           (while range
5621             (if (numberp (car range))
5622                 (setq num (1+ num))
5623               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5624             (setq range (cdr range)))
5625           (setq num (- (cdr active) num))))
5626         ;; Update the number of unread articles.
5627         (setcar entry num)
5628         ;; Update the group buffer.
5629         (unless (gnus-ephemeral-group-p group)
5630           (gnus-group-update-group group t))))))
5631
5632 (defvar gnus-newsgroup-none-id 0)
5633
5634 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5635   (let ((cur nntp-server-buffer)
5636         (dependencies
5637          (or dependencies
5638              (save-excursion (set-buffer gnus-summary-buffer)
5639                              gnus-newsgroup-dependencies)))
5640         headers id end ref
5641         (mail-parse-charset gnus-newsgroup-charset)
5642         (mail-parse-ignored-charsets
5643          (save-excursion (condition-case nil
5644                              (set-buffer gnus-summary-buffer)
5645                            (error))
5646                          gnus-newsgroup-ignored-charsets)))
5647     (save-excursion
5648       (set-buffer nntp-server-buffer)
5649       ;; Translate all TAB characters into SPACE characters.
5650       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5651       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5652       (gnus-run-hooks 'gnus-parse-headers-hook)
5653       (let ((case-fold-search t)
5654             in-reply-to header p lines chars ctype)
5655         (goto-char (point-min))
5656         ;; Search to the beginning of the next header.  Error messages
5657         ;; do not begin with 2 or 3.
5658         (while (re-search-forward "^[23][0-9]+ " nil t)
5659           (setq id nil
5660                 ref nil)
5661           ;; This implementation of this function, with nine
5662           ;; search-forwards instead of the one re-search-forward and
5663           ;; a case (which basically was the old function) is actually
5664           ;; about twice as fast, even though it looks messier.  You
5665           ;; can't have everything, I guess.  Speed and elegance
5666           ;; doesn't always go hand in hand.
5667           (setq
5668            header
5669            (make-full-mail-header
5670             ;; Number.
5671             (prog1
5672                 (read cur)
5673               (end-of-line)
5674               (setq p (point))
5675               (narrow-to-region (point)
5676                                 (or (and (search-forward "\n.\n" nil t)
5677                                          (- (point) 2))
5678                                     (point))))
5679             ;; Subject.
5680             (progn
5681               (goto-char p)
5682               (if (search-forward "\nsubject:" nil t)
5683                   (nnheader-header-value)
5684                 "(none)"))
5685             ;; From.
5686             (progn
5687               (goto-char p)
5688               (if (search-forward "\nfrom:" nil t)
5689                   (nnheader-header-value)
5690                 "(nobody)"))
5691             ;; Date.
5692             (progn
5693               (goto-char p)
5694               (if (search-forward "\ndate:" nil t)
5695                   (nnheader-header-value) ""))
5696             ;; Message-ID.
5697             (progn
5698               (goto-char p)
5699               (setq id (if (re-search-forward
5700                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5701                            ;; We do it this way to make sure the Message-ID
5702                            ;; is (somewhat) syntactically valid.
5703                            (buffer-substring (match-beginning 1)
5704                                              (match-end 1))
5705                          ;; If there was no message-id, we just fake one
5706                          ;; to make subsequent routines simpler.
5707                          (nnheader-generate-fake-message-id))))
5708             ;; References.
5709             (progn
5710               (goto-char p)
5711               (if (search-forward "\nreferences:" nil t)
5712                   (progn
5713                     (setq end (point))
5714                     (prog1
5715                         (nnheader-header-value)
5716                       (setq ref
5717                             (buffer-substring
5718                              (progn
5719                                ;; (end-of-line)
5720                                (search-backward ">" end t)
5721                                (1+ (point)))
5722                              (progn
5723                                (search-backward "<" end t)
5724                                (point))))))
5725                 ;; Get the references from the in-reply-to header if there
5726                 ;; were no references and the in-reply-to header looks
5727                 ;; promising.
5728                 (if (and (search-forward "\nin-reply-to:" nil t)
5729                          (setq in-reply-to (nnheader-header-value))
5730                          (string-match "<[^>]+>" in-reply-to))
5731                     (let (ref2)
5732                       (setq ref (substring in-reply-to (match-beginning 0)
5733                                            (match-end 0)))
5734                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5735                         (setq ref2 (substring in-reply-to (match-beginning 0)
5736                                               (match-end 0)))
5737                         (when (> (length ref2) (length ref))
5738                           (setq ref ref2)))
5739                       ref)
5740                   (setq ref nil))))
5741             ;; Chars.
5742             (progn
5743               (goto-char p)
5744               (if (search-forward "\nchars: " nil t)
5745                   (if (numberp (setq chars (ignore-errors (read cur))))
5746                       chars -1)
5747                 -1))
5748             ;; Lines.
5749             (progn
5750               (goto-char p)
5751               (if (search-forward "\nlines: " nil t)
5752                   (if (numberp (setq lines (ignore-errors (read cur))))
5753                       lines -1)
5754                 -1))
5755             ;; Xref.
5756             (progn
5757               (goto-char p)
5758               (and (search-forward "\nxref:" nil t)
5759                    (nnheader-header-value)))
5760             ;; Extra.
5761             (when gnus-extra-headers
5762               (let ((extra gnus-extra-headers)
5763                     out)
5764                 (while extra
5765                   (goto-char p)
5766                   (when (search-forward
5767                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5768                     (push (cons (car extra) (nnheader-header-value)) out))
5769                   (pop extra))
5770                 out))))
5771           (goto-char p)
5772           (if (and (search-forward "\ncontent-type: " nil t)
5773                    (setq ctype (nnheader-header-value)))
5774               (mime-entity-set-content-type-internal
5775                header (mime-parse-Content-Type ctype)))
5776           (when (equal id ref)
5777             (setq ref nil))
5778
5779           (when gnus-alter-header-function
5780             (funcall gnus-alter-header-function header)
5781             (setq id (mail-header-id header)
5782                   ref (gnus-parent-id (mail-header-references header))))
5783
5784           (when (setq header
5785                       (gnus-dependencies-add-header
5786                        header dependencies force-new))
5787             (push header headers))
5788           (goto-char (point-max))
5789           (widen))
5790         (nreverse headers)))))
5791
5792 ;; Goes through the xover lines and returns a list of vectors
5793 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5794                                                   force-new dependencies
5795                                                   group also-fetch-heads)
5796   "Parse the news overview data in the server buffer.
5797 Return a list of headers that match SEQUENCE (see
5798 `nntp-retrieve-headers')."
5799   ;; Get the Xref when the users reads the articles since most/some
5800   ;; NNTP servers do not include Xrefs when using XOVER.
5801   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5802   (let ((mail-parse-charset gnus-newsgroup-charset)
5803         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5804         (cur nntp-server-buffer)
5805         (dependencies (or dependencies gnus-newsgroup-dependencies))
5806         (allp (cond
5807                ((eq gnus-read-all-available-headers t)
5808                 t)
5809                ((stringp gnus-read-all-available-headers)
5810                 (string-match gnus-read-all-available-headers group))
5811                (t
5812                 nil)))
5813         number headers header)
5814     (save-excursion
5815       (set-buffer nntp-server-buffer)
5816       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5817       ;; Allow the user to mangle the headers before parsing them.
5818       (gnus-run-hooks 'gnus-parse-headers-hook)
5819       (goto-char (point-min))
5820       (gnus-parse-without-error
5821         (while (and (or sequence allp)
5822                     (not (eobp)))
5823           (setq number (read cur))
5824           (when (not allp)
5825             (while (and sequence
5826                         (< (car sequence) number))
5827               (setq sequence (cdr sequence))))
5828           (when (and (or allp
5829                          (and sequence
5830                               (eq number (car sequence))))
5831                      (progn
5832                        (setq sequence (cdr sequence))
5833                        (setq header (inline
5834                                       (gnus-nov-parse-line
5835                                        number dependencies force-new)))))
5836             (push header headers))
5837           (forward-line 1)))
5838       ;; A common bug in inn is that if you have posted an article and
5839       ;; then retrieves the active file, it will answer correctly --
5840       ;; the new article is included.  However, a NOV entry for the
5841       ;; article may not have been generated yet, so this may fail.
5842       ;; We work around this problem by retrieving the last few
5843       ;; headers using HEAD.
5844       (if (or (not also-fetch-heads)
5845               (not sequence))
5846           ;; We (probably) got all the headers.
5847           (nreverse headers)
5848         (let ((gnus-nov-is-evil t))
5849           (nconc
5850            (nreverse headers)
5851            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5852              (gnus-get-newsgroup-headers))))))))
5853
5854 (defun gnus-article-get-xrefs ()
5855   "Fill in the Xref value in `gnus-current-headers', if necessary.
5856 This is meant to be called in `gnus-article-internal-prepare-hook'."
5857   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5858                                  gnus-current-headers)))
5859     (or (not gnus-use-cross-reference)
5860         (not headers)
5861         (and (mail-header-xref headers)
5862              (not (string= (mail-header-xref headers) "")))
5863         (let ((case-fold-search t)
5864               xref)
5865           (save-restriction
5866             (nnheader-narrow-to-headers)
5867             (goto-char (point-min))
5868             (when (or (and (not (eobp))
5869                            (eq (downcase (char-after)) ?x)
5870                            (looking-at "Xref:"))
5871                       (search-forward "\nXref:" nil t))
5872               (goto-char (1+ (match-end 0)))
5873               (setq xref (buffer-substring (point)
5874                                            (progn (end-of-line) (point))))
5875               (mail-header-set-xref headers xref)))))))
5876
5877 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5878   "Find article ID and insert the summary line for that article.
5879 OLD-HEADER can either be a header or a line number to insert
5880 the subject line on."
5881   (let* ((line (and (numberp old-header) old-header))
5882          (old-header (and (vectorp old-header) old-header))
5883          (header (cond ((and old-header use-old-header)
5884                         old-header)
5885                        ((and (numberp id)
5886                              (gnus-number-to-header id))
5887                         (gnus-number-to-header id))
5888                        (t
5889                         (gnus-read-header id))))
5890          (number (and (numberp id) id))
5891          d)
5892     (when header
5893       ;; Rebuild the thread that this article is part of and go to the
5894       ;; article we have fetched.
5895       (when (and (not gnus-show-threads)
5896                  old-header)
5897         (when (and number
5898                    (setq d (gnus-data-find (mail-header-number old-header))))
5899           (goto-char (gnus-data-pos d))
5900           (gnus-data-remove
5901            number
5902            (- (gnus-point-at-bol)
5903               (prog1
5904                   (1+ (gnus-point-at-eol))
5905                 (gnus-delete-line))))))
5906       (when old-header
5907         (mail-header-set-number header (mail-header-number old-header)))
5908       (setq gnus-newsgroup-sparse
5909             (delq (setq number (mail-header-number header))
5910                   gnus-newsgroup-sparse))
5911       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5912       (push number gnus-newsgroup-limit)
5913       (gnus-rebuild-thread (mail-header-id header) line)
5914       (gnus-summary-goto-subject number nil t))
5915     (when (and (numberp number)
5916                (> number 0))
5917       ;; We have to update the boundaries even if we can't fetch the
5918       ;; article if ID is a number -- so that the next `P' or `N'
5919       ;; command will fetch the previous (or next) article even
5920       ;; if the one we tried to fetch this time has been canceled.
5921       (when (> number gnus-newsgroup-end)
5922         (setq gnus-newsgroup-end number))
5923       (when (< number gnus-newsgroup-begin)
5924         (setq gnus-newsgroup-begin number))
5925       (setq gnus-newsgroup-unselected
5926             (delq number gnus-newsgroup-unselected)))
5927     ;; Report back a success?
5928     (and header (mail-header-number header))))
5929
5930 ;;; Process/prefix in the summary buffer
5931
5932 (defun gnus-summary-work-articles (n)
5933   "Return a list of articles to be worked upon.
5934 The prefix argument, the list of process marked articles, and the
5935 current article will be taken into consideration."
5936   (save-excursion
5937     (set-buffer gnus-summary-buffer)
5938     (cond
5939      (n
5940       ;; A numerical prefix has been given.
5941       (setq n (prefix-numeric-value n))
5942       (let ((backward (< n 0))
5943             (n (abs (prefix-numeric-value n)))
5944             articles article)
5945         (save-excursion
5946           (while
5947               (and (> n 0)
5948                    (push (setq article (gnus-summary-article-number))
5949                          articles)
5950                    (if backward
5951                        (gnus-summary-find-prev nil article)
5952                      (gnus-summary-find-next nil article)))
5953             (decf n)))
5954         (nreverse articles)))
5955      ((and (gnus-region-active-p) (mark))
5956       (message "region active")
5957       ;; Work on the region between point and mark.
5958       (let ((max (max (point) (mark)))
5959             articles article)
5960         (save-excursion
5961           (goto-char (min (point) (mark)))
5962           (while
5963               (and
5964                (push (setq article (gnus-summary-article-number)) articles)
5965                (gnus-summary-find-next nil article)
5966                (< (point) max)))
5967           (nreverse articles))))
5968      (gnus-newsgroup-processable
5969       ;; There are process-marked articles present.
5970       ;; Save current state.
5971       (gnus-summary-save-process-mark)
5972       ;; Return the list.
5973       (reverse gnus-newsgroup-processable))
5974      (t
5975       ;; Just return the current article.
5976       (list (gnus-summary-article-number))))))
5977
5978 (defmacro gnus-summary-iterate (arg &rest forms)
5979   "Iterate over the process/prefixed articles and do FORMS.
5980 ARG is the interactive prefix given to the command.  FORMS will be
5981 executed with point over the summary line of the articles."
5982   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5983     `(let ((,articles (gnus-summary-work-articles ,arg)))
5984        (while ,articles
5985          (gnus-summary-goto-subject (car ,articles))
5986          ,@forms
5987          (pop ,articles)))))
5988
5989 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5990 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5991
5992 (defun gnus-summary-save-process-mark ()
5993   "Push the current set of process marked articles on the stack."
5994   (interactive)
5995   (push (copy-sequence gnus-newsgroup-processable)
5996         gnus-newsgroup-process-stack))
5997
5998 (defun gnus-summary-kill-process-mark ()
5999   "Push the current set of process marked articles on the stack and unmark."
6000   (interactive)
6001   (gnus-summary-save-process-mark)
6002   (gnus-summary-unmark-all-processable))
6003
6004 (defun gnus-summary-yank-process-mark ()
6005   "Pop the last process mark state off the stack and restore it."
6006   (interactive)
6007   (unless gnus-newsgroup-process-stack
6008     (error "Empty mark stack"))
6009   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6010
6011 (defun gnus-summary-process-mark-set (set)
6012   "Make SET into the current process marked articles."
6013   (gnus-summary-unmark-all-processable)
6014   (while set
6015     (gnus-summary-set-process-mark (pop set))))
6016
6017 ;;; Searching and stuff
6018
6019 (defun gnus-summary-search-group (&optional backward use-level)
6020   "Search for next unread newsgroup.
6021 If optional argument BACKWARD is non-nil, search backward instead."
6022   (save-excursion
6023     (set-buffer gnus-group-buffer)
6024     (when (gnus-group-search-forward
6025            backward nil (if use-level (gnus-group-group-level) nil))
6026       (gnus-group-group-name))))
6027
6028 (defun gnus-summary-best-group (&optional exclude-group)
6029   "Find the name of the best unread group.
6030 If EXCLUDE-GROUP, do not go to this group."
6031   (save-excursion
6032     (set-buffer gnus-group-buffer)
6033     (save-excursion
6034       (gnus-group-best-unread-group exclude-group))))
6035
6036 (defun gnus-summary-find-next (&optional unread article backward)
6037   (if backward (gnus-summary-find-prev)
6038     (let* ((dummy (gnus-summary-article-intangible-p))
6039            (article (or article (gnus-summary-article-number)))
6040            (data (gnus-data-find-list article))
6041            result)
6042       (when (and (not dummy)
6043                  (or (not gnus-summary-check-current)
6044                      (not unread)
6045                      (not (gnus-data-unread-p (car data)))))
6046         (setq data (cdr data)))
6047       (when (setq result
6048                   (if unread
6049                       (progn
6050                         (while data
6051                           (unless (memq (gnus-data-number (car data)) 
6052                                         gnus-newsgroup-unfetched)
6053                             (when (gnus-data-unread-p (car data))
6054                               (setq result (car data)
6055                                     data nil)))
6056                           (setq data (cdr data)))
6057                         result)
6058                     (car data)))
6059         (goto-char (gnus-data-pos result))
6060         (gnus-data-number result)))))
6061
6062 (defun gnus-summary-find-prev (&optional unread article)
6063   (let* ((eobp (eobp))
6064          (article (or article (gnus-summary-article-number)))
6065          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6066          result)
6067     (when (and (not eobp)
6068                (or (not gnus-summary-check-current)
6069                    (not unread)
6070                    (not (gnus-data-unread-p (car data)))))
6071       (setq data (cdr data)))
6072     (when (setq result
6073                 (if unread
6074                     (progn
6075                       (while data
6076                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6077                           (when (gnus-data-unread-p (car data))
6078                             (setq result (car data)
6079                                   data nil)))
6080                         (setq data (cdr data)))
6081                       result)
6082                   (car data)))
6083       (goto-char (gnus-data-pos result))
6084       (gnus-data-number result))))
6085
6086 (defun gnus-summary-find-subject (subject &optional unread backward article)
6087   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6088          (article (or article (gnus-summary-article-number)))
6089          (articles (gnus-data-list backward))
6090          (arts (gnus-data-find-list article articles))
6091          result)
6092     (when (or (not gnus-summary-check-current)
6093               (not unread)
6094               (not (gnus-data-unread-p (car arts))))
6095       (setq arts (cdr arts)))
6096     (while arts
6097       (and (or (not unread)
6098                (gnus-data-unread-p (car arts)))
6099            (vectorp (gnus-data-header (car arts)))
6100            (gnus-subject-equal
6101             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6102            (setq result (car arts)
6103                  arts nil))
6104       (setq arts (cdr arts)))
6105     (and result
6106          (goto-char (gnus-data-pos result))
6107          (gnus-data-number result))))
6108
6109 (defun gnus-summary-search-forward (&optional unread subject backward)
6110   "Search forward for an article.
6111 If UNREAD, look for unread articles.  If SUBJECT, look for
6112 articles with that subject.  If BACKWARD, search backward instead."
6113   (cond (subject (gnus-summary-find-subject subject unread backward))
6114         (backward (gnus-summary-find-prev unread))
6115         (t (gnus-summary-find-next unread))))
6116
6117 (defun gnus-recenter (&optional n)
6118   "Center point in window and redisplay frame.
6119 Also do horizontal recentering."
6120   (interactive "P")
6121   (when (and gnus-auto-center-summary
6122              (not (eq gnus-auto-center-summary 'vertical)))
6123     (gnus-horizontal-recenter))
6124   (recenter n))
6125
6126 (defun gnus-summary-recenter ()
6127   "Center point in the summary window.
6128 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6129 displayed, no centering will be performed."
6130   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6131   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6132   (interactive)
6133   ;; The user has to want it.
6134   (when gnus-auto-center-summary
6135     (let* ((top (cond ((< (window-height) 4) 0)
6136                       ((< (window-height) 7) 1)
6137                       (t (if (numberp gnus-auto-center-summary)
6138                              gnus-auto-center-summary
6139                            2))))
6140            (height (1- (window-height)))
6141            (bottom (save-excursion (goto-char (point-max))
6142                                    (forward-line (- height))
6143                                    (point)))
6144            (window (get-buffer-window (current-buffer))))
6145       (when (get-buffer-window gnus-article-buffer)
6146         ;; Only do recentering when the article buffer is displayed,
6147         ;; Set the window start to either `bottom', which is the biggest
6148         ;; possible valid number, or the second line from the top,
6149         ;; whichever is the least.
6150         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6151           (if (> bottom top-pos)
6152               ;; Keep the second line from the top visible
6153               (set-window-start window top-pos t)
6154             ;; Try to keep the bottom line visible; if it's partially
6155             ;; obscured, either scroll one more line to make it fully
6156             ;; visible, or revert to using TOP-POS.
6157             (save-excursion
6158               (goto-char (point-max))
6159               (forward-line -1)
6160               (let ((last-line-start (point)))
6161                 (goto-char bottom)
6162                 (set-window-start window (point) t)
6163                 (when (not (pos-visible-in-window-p last-line-start window))
6164                   (forward-line 1)
6165                   (set-window-start window (min (point) top-pos) t)))))))
6166       ;; Do horizontal recentering while we're at it.
6167       (when (and (get-buffer-window (current-buffer) t)
6168                  (not (eq gnus-auto-center-summary 'vertical)))
6169         (let ((selected (selected-window)))
6170           (select-window (get-buffer-window (current-buffer) t))
6171           (gnus-summary-position-point)
6172           (gnus-horizontal-recenter)
6173           (select-window selected))))))
6174
6175 (defun gnus-summary-jump-to-group (newsgroup)
6176   "Move point to NEWSGROUP in group mode buffer."
6177   ;; Keep update point of group mode buffer if visible.
6178   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6179       (save-window-excursion
6180         ;; Take care of tree window mode.
6181         (when (get-buffer-window gnus-group-buffer)
6182           (pop-to-buffer gnus-group-buffer))
6183         (gnus-group-jump-to-group newsgroup))
6184     (save-excursion
6185       ;; Take care of tree window mode.
6186       (if (get-buffer-window gnus-group-buffer)
6187           (pop-to-buffer gnus-group-buffer)
6188         (set-buffer gnus-group-buffer))
6189       (gnus-group-jump-to-group newsgroup))))
6190
6191 ;; This function returns a list of article numbers based on the
6192 ;; difference between the ranges of read articles in this group and
6193 ;; the range of active articles.
6194 (defun gnus-list-of-unread-articles (group)
6195   (let* ((read (gnus-info-read (gnus-get-info group)))
6196          (active (or (gnus-active group) (gnus-activate-group group)))
6197          (last (cdr active))
6198          first nlast unread)
6199     ;; If none are read, then all are unread.
6200     (if (not read)
6201         (setq first (car active))
6202       ;; If the range of read articles is a single range, then the
6203       ;; first unread article is the article after the last read
6204       ;; article.  Sounds logical, doesn't it?
6205       (if (and (not (listp (cdr read)))
6206                (or (< (car read) (car active))
6207                    (progn (setq read (list read))
6208                           nil)))
6209           (setq first (max (car active) (1+ (cdr read))))
6210         ;; `read' is a list of ranges.
6211         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6212                                   (caar read)))
6213                   1)
6214           (setq first (car active)))
6215         (while read
6216           (when first
6217             (while (< first nlast)
6218               (push first unread)
6219               (setq first (1+ first))))
6220           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6221           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6222           (setq read (cdr read)))))
6223     ;; And add the last unread articles.
6224     (while (<= first last)
6225       (push first unread)
6226       (setq first (1+ first)))
6227     ;; Return the list of unread articles.
6228     (delq 0 (nreverse unread))))
6229
6230 (defun gnus-list-of-read-articles (group)
6231   "Return a list of unread, unticked and non-dormant articles."
6232   (let* ((info (gnus-get-info group))
6233          (marked (gnus-info-marks info))
6234          (active (gnus-active group)))
6235     (and info active
6236          (gnus-list-range-difference
6237           (gnus-list-range-difference
6238            (gnus-sorted-complement
6239             (gnus-uncompress-range active)
6240             (gnus-list-of-unread-articles group))
6241            (cdr (assq 'dormant marked)))
6242           (cdr (assq 'tick marked))))))
6243
6244 ;; Various summary commands
6245
6246 (defun gnus-summary-select-article-buffer ()
6247   "Reconfigure windows to show article buffer."
6248   (interactive)
6249   (if (not (gnus-buffer-live-p gnus-article-buffer))
6250       (error "There is no article buffer for this summary buffer")
6251     (gnus-configure-windows 'article)
6252     (select-window (get-buffer-window gnus-article-buffer))))
6253
6254 (defun gnus-summary-universal-argument (arg)
6255   "Perform any operation on all articles that are process/prefixed."
6256   (interactive "P")
6257   (let ((articles (gnus-summary-work-articles arg))
6258         func article)
6259     (if (eq
6260          (setq
6261           func
6262           (key-binding
6263            (read-key-sequence
6264             (substitute-command-keys
6265              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6266          'undefined)
6267         (gnus-error 1 "Undefined key")
6268       (save-excursion
6269         (while articles
6270           (gnus-summary-goto-subject (setq article (pop articles)))
6271           (let (gnus-newsgroup-processable)
6272             (command-execute func))
6273           (gnus-summary-remove-process-mark article)))))
6274   (gnus-summary-position-point))
6275
6276 (defun gnus-summary-toggle-truncation (&optional arg)
6277   "Toggle truncation of summary lines.
6278 With arg, turn line truncation on if arg is positive."
6279   (interactive "P")
6280   (setq truncate-lines
6281         (if (null arg) (not truncate-lines)
6282           (> (prefix-numeric-value arg) 0)))
6283   (redraw-display))
6284
6285 (defun gnus-summary-find-uncancelled ()
6286   "Return the number of an uncancelled article.
6287 The current article is considered, then following articles, then previous
6288 articles.  If all articles are cancelled then return a dummy 0."
6289   (let (found)
6290     (dolist (rev '(nil t))
6291       (unless found      ; don't demand the reverse list if we don't need it
6292         (let ((data (gnus-data-find-list
6293                      (gnus-summary-article-number) (gnus-data-list rev))))
6294           (while (and data (not found))
6295             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6296                 (setq found (gnus-data-number (car data))))
6297             (setq data (cdr data))))))
6298     (or found 0)))
6299
6300 (defun gnus-summary-reselect-current-group (&optional all rescan)
6301   "Exit and then reselect the current newsgroup.
6302 The prefix argument ALL means to select all articles."
6303   (interactive "P")
6304   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6305     (error "Ephemeral groups can't be reselected"))
6306   (let ((current-subject (gnus-summary-find-uncancelled))
6307         (group gnus-newsgroup-name))
6308     (setq gnus-newsgroup-begin nil)
6309     (gnus-summary-exit)
6310     ;; We have to adjust the point of group mode buffer because
6311     ;; point was moved to the next unread newsgroup by exiting.
6312     (gnus-summary-jump-to-group group)
6313     (when rescan
6314       (save-excursion
6315         (save-window-excursion
6316           ;; Don't show group contents.
6317           (set-window-start (selected-window) (point-max))
6318           (gnus-group-get-new-news-this-group 1))))
6319     (gnus-group-read-group all t)
6320     (gnus-summary-goto-subject current-subject nil t)))
6321
6322 (defun gnus-summary-rescan-group (&optional all)
6323   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6324   (interactive "P")
6325   (gnus-summary-reselect-current-group all t))
6326
6327 (defun gnus-summary-update-info (&optional non-destructive)
6328   (save-excursion
6329     (let ((group gnus-newsgroup-name))
6330       (when group
6331         (when gnus-newsgroup-kill-headers
6332           (setq gnus-newsgroup-killed
6333                 (gnus-compress-sequence
6334                  (gnus-sorted-union
6335                   (gnus-list-range-intersection
6336                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6337                   gnus-newsgroup-unreads)
6338                  t)))
6339         (unless (listp (cdr gnus-newsgroup-killed))
6340           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6341         (let ((headers gnus-newsgroup-headers))
6342           ;; Set the new ranges of read articles.
6343           (save-excursion
6344             (set-buffer gnus-group-buffer)
6345             (gnus-undo-force-boundary))
6346           (gnus-update-read-articles
6347            group (gnus-sorted-union
6348                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6349           ;; Set the current article marks.
6350           (let ((gnus-newsgroup-scored
6351                  (if (and (not gnus-save-score)
6352                           (not non-destructive))
6353                      nil
6354                    gnus-newsgroup-scored)))
6355             (save-excursion
6356               (gnus-update-marks)))
6357           ;; Do the cross-ref thing.
6358           (when gnus-use-cross-reference
6359             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6360           ;; Do not switch windows but change the buffer to work.
6361           (set-buffer gnus-group-buffer)
6362           (unless (gnus-ephemeral-group-p group)
6363             (gnus-group-update-group group)))))))
6364
6365 (defun gnus-summary-save-newsrc (&optional force)
6366   "Save the current number of read/marked articles in the dribble buffer.
6367 The dribble buffer will then be saved.
6368 If FORCE (the prefix), also save the .newsrc file(s)."
6369   (interactive "P")
6370   (gnus-summary-update-info t)
6371   (if force
6372       (gnus-save-newsrc-file)
6373     (gnus-dribble-save)))
6374
6375 (defun gnus-summary-exit (&optional temporary)
6376   "Exit reading current newsgroup, and then return to group selection mode.
6377 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6378   (interactive)
6379   (gnus-set-global-variables)
6380   (gnus-kill-save-kill-buffer)
6381   (gnus-async-halt-prefetch)
6382   (let* ((group gnus-newsgroup-name)
6383          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6384          (gnus-group-is-exiting-p t)
6385          (mode major-mode)
6386          (group-point nil)
6387          (buf (current-buffer)))
6388     (unless quit-config
6389       ;; Do adaptive scoring, and possibly save score files.
6390       (when gnus-newsgroup-adaptive
6391         (gnus-score-adaptive))
6392       (when gnus-use-scoring
6393         (gnus-score-save)))
6394     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6395     ;; If we have several article buffers, we kill them at exit.
6396     (unless gnus-single-article-buffer
6397       (gnus-kill-buffer gnus-original-article-buffer)
6398       (setq gnus-article-current nil))
6399     (when gnus-use-cache
6400       (gnus-cache-possibly-remove-articles)
6401       (gnus-cache-save-buffers))
6402     (gnus-async-prefetch-remove-group group)
6403     (when gnus-suppress-duplicates
6404       (gnus-dup-enter-articles))
6405     (when gnus-use-trees
6406       (gnus-tree-close group))
6407     (when gnus-use-cache
6408       (gnus-cache-write-active))
6409     ;; Remove entries for this group.
6410     (nnmail-purge-split-history (gnus-group-real-name group))
6411     ;; Make all changes in this group permanent.
6412     (unless quit-config
6413       (gnus-run-hooks 'gnus-exit-group-hook)
6414       (gnus-summary-update-info))
6415     (gnus-close-group group)
6416     ;; Make sure where we were, and go to next newsgroup.
6417     (set-buffer gnus-group-buffer)
6418     (unless quit-config
6419       (gnus-group-jump-to-group group))
6420     (gnus-run-hooks 'gnus-summary-exit-hook)
6421     (unless (or quit-config
6422                 ;; If this group has disappeared from the summary
6423                 ;; buffer, don't skip forwards.
6424                 (not (string= group (gnus-group-group-name))))
6425       (gnus-group-next-unread-group 1))
6426     (setq group-point (point))
6427     (if temporary
6428         nil                             ;Nothing to do.
6429       ;; If we have several article buffers, we kill them at exit.
6430       (unless gnus-single-article-buffer
6431         (gnus-kill-buffer gnus-article-buffer)
6432         (gnus-kill-buffer gnus-original-article-buffer)
6433         (setq gnus-article-current nil))
6434       (set-buffer buf)
6435       (if (not gnus-kill-summary-on-exit)
6436           (progn
6437             (gnus-deaden-summary)
6438             (setq mode nil))
6439         ;; We set all buffer-local variables to nil.  It is unclear why
6440         ;; this is needed, but if we don't, buffer-local variables are
6441         ;; not garbage-collected, it seems.  This would the lead to en
6442         ;; ever-growing Emacs.
6443         (gnus-summary-clear-local-variables)
6444         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6445           (gnus-summary-clear-local-variables))
6446         (when (get-buffer gnus-article-buffer)
6447           (bury-buffer gnus-article-buffer))
6448         ;; We clear the global counterparts of the buffer-local
6449         ;; variables as well, just to be on the safe side.
6450         (set-buffer gnus-group-buffer)
6451         (gnus-summary-clear-local-variables)
6452         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6453           (gnus-summary-clear-local-variables))
6454         ;; Return to group mode buffer.
6455         (when (eq mode 'gnus-summary-mode)
6456           (gnus-kill-buffer buf)))
6457       (setq gnus-current-select-method gnus-select-method)
6458       (pop-to-buffer gnus-group-buffer)
6459       (if (not quit-config)
6460           (progn
6461             (goto-char group-point)
6462             (gnus-configure-windows 'group 'force)
6463             (unless (pos-visible-in-window-p)
6464               (forward-line (/ (static-if (featurep 'xemacs)
6465                                    (window-displayed-height)
6466                                  (1- (window-height)))
6467                                -2))
6468               (set-window-start (selected-window) (point))
6469               (goto-char group-point)))
6470         (gnus-handle-ephemeral-exit quit-config))
6471       ;; Clear the current group name.
6472       (unless quit-config
6473         (setq gnus-newsgroup-name nil)))))
6474
6475 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6476 (defun gnus-summary-exit-no-update (&optional no-questions)
6477   "Quit reading current newsgroup without updating read article info."
6478   (interactive)
6479   (let* ((group gnus-newsgroup-name)
6480          (gnus-group-is-exiting-p t)
6481          (quit-config (gnus-group-quit-config group)))
6482     (when (or no-questions
6483               gnus-expert-user
6484               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6485       (gnus-async-halt-prefetch)
6486       (mapcar 'funcall
6487               (delq 'gnus-summary-expire-articles
6488                     (copy-sequence gnus-summary-prepare-exit-hook)))
6489       ;; If we have several article buffers, we kill them at exit.
6490       (unless gnus-single-article-buffer
6491         (gnus-kill-buffer gnus-article-buffer)
6492         (gnus-kill-buffer gnus-original-article-buffer)
6493         (setq gnus-article-current nil))
6494       (if (not gnus-kill-summary-on-exit)
6495           (gnus-deaden-summary)
6496         (gnus-close-group group)
6497         (gnus-summary-clear-local-variables)
6498         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6499           (gnus-summary-clear-local-variables))
6500         (set-buffer gnus-group-buffer)
6501         (gnus-summary-clear-local-variables)
6502         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6503           (gnus-summary-clear-local-variables))
6504         (when (get-buffer gnus-summary-buffer)
6505           (kill-buffer gnus-summary-buffer)))
6506       (unless gnus-single-article-buffer
6507         (setq gnus-article-current nil))
6508       (when gnus-use-trees
6509         (gnus-tree-close group))
6510       (gnus-async-prefetch-remove-group group)
6511       (when (get-buffer gnus-article-buffer)
6512         (bury-buffer gnus-article-buffer))
6513       ;; Return to the group buffer.
6514       (gnus-configure-windows 'group 'force)
6515       ;; Clear the current group name.
6516       (setq gnus-newsgroup-name nil)
6517       (unless (gnus-ephemeral-group-p group)
6518         (gnus-group-update-group group))
6519       (when (equal (gnus-group-group-name) group)
6520         (gnus-group-next-unread-group 1))
6521       (when quit-config
6522         (gnus-handle-ephemeral-exit quit-config)))))
6523
6524 (defun gnus-handle-ephemeral-exit (quit-config)
6525   "Handle movement when leaving an ephemeral group.
6526 The state which existed when entering the ephemeral is reset."
6527   (if (not (buffer-name (car quit-config)))
6528       (gnus-configure-windows 'group 'force)
6529     (set-buffer (car quit-config))
6530     (cond ((eq major-mode 'gnus-summary-mode)
6531            (gnus-set-global-variables))
6532           ((eq major-mode 'gnus-article-mode)
6533            (save-excursion
6534              ;; The `gnus-summary-buffer' variable may point
6535              ;; to the old summary buffer when using a single
6536              ;; article buffer.
6537              (unless (gnus-buffer-live-p gnus-summary-buffer)
6538                (set-buffer gnus-group-buffer))
6539              (set-buffer gnus-summary-buffer)
6540              (gnus-set-global-variables))))
6541     (if (or (eq (cdr quit-config) 'article)
6542             (eq (cdr quit-config) 'pick))
6543         (progn
6544           ;; The current article may be from the ephemeral group
6545           ;; thus it is best that we reload this article
6546           (gnus-summary-show-article)
6547           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6548               (gnus-configure-windows 'pick 'force)
6549             (gnus-configure-windows (cdr quit-config) 'force)))
6550       (gnus-configure-windows (cdr quit-config) 'force))
6551     (when (eq major-mode 'gnus-summary-mode)
6552       (gnus-summary-next-subject 1 nil t)
6553       (gnus-summary-recenter)
6554       (gnus-summary-position-point))))
6555
6556 (defun gnus-summary-preview-mime-message ()
6557   "MIME decode and play this message."
6558   (interactive)
6559   (let ((gnus-break-pages nil)
6560         (gnus-show-mime t))
6561     (gnus-summary-select-article gnus-show-all-headers t))
6562   (let ((w (get-buffer-window gnus-article-buffer)))
6563     (when w
6564       (select-window (get-buffer-window gnus-article-buffer)))))
6565
6566 ;;; Dead summaries.
6567
6568 (defvar gnus-dead-summary-mode-map nil)
6569
6570 (unless gnus-dead-summary-mode-map
6571   (setq gnus-dead-summary-mode-map (make-keymap))
6572   (suppress-keymap gnus-dead-summary-mode-map)
6573   (substitute-key-definition
6574    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6575   (dolist (key '("\C-d" "\r" "\177" [delete]))
6576     (define-key gnus-dead-summary-mode-map
6577       key 'gnus-summary-wake-up-the-dead))
6578   (dolist (key '("q" "Q"))
6579     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6580
6581 (defvar gnus-dead-summary-mode nil
6582   "Minor mode for Gnus summary buffers.")
6583
6584 (defun gnus-dead-summary-mode (&optional arg)
6585   "Minor mode for Gnus summary buffers."
6586   (interactive "P")
6587   (when (eq major-mode 'gnus-summary-mode)
6588     (make-local-variable 'gnus-dead-summary-mode)
6589     (setq gnus-dead-summary-mode
6590           (if (null arg) (not gnus-dead-summary-mode)
6591             (> (prefix-numeric-value arg) 0)))
6592     (when gnus-dead-summary-mode
6593       (gnus-add-minor-mode
6594        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6595
6596 (defun gnus-deaden-summary ()
6597   "Make the current summary buffer into a dead summary buffer."
6598   ;; Kill any previous dead summary buffer.
6599   (when (and gnus-dead-summary
6600              (buffer-name gnus-dead-summary))
6601     (save-excursion
6602       (set-buffer gnus-dead-summary)
6603       (when gnus-dead-summary-mode
6604         (kill-buffer (current-buffer)))))
6605   ;; Make this the current dead summary.
6606   (setq gnus-dead-summary (current-buffer))
6607   (gnus-dead-summary-mode 1)
6608   (let ((name (buffer-name)))
6609     (when (string-match "Summary" name)
6610       (rename-buffer
6611        (concat (substring name 0 (match-beginning 0)) "Dead "
6612                (substring name (match-beginning 0)))
6613        t)
6614       (bury-buffer))))
6615
6616 (defun gnus-kill-or-deaden-summary (buffer)
6617   "Kill or deaden the summary BUFFER."
6618   (save-excursion
6619     (when (and (buffer-name buffer)
6620                (not gnus-single-article-buffer))
6621       (save-excursion
6622         (set-buffer buffer)
6623         (gnus-kill-buffer gnus-article-buffer)
6624         (gnus-kill-buffer gnus-original-article-buffer)))
6625     (cond
6626      ;; Kill the buffer.
6627      (gnus-kill-summary-on-exit
6628       (when (and gnus-use-trees
6629                  (gnus-buffer-exists-p buffer))
6630         (save-excursion
6631           (set-buffer buffer)
6632           (gnus-tree-close gnus-newsgroup-name)))
6633       (gnus-kill-buffer buffer))
6634      ;; Deaden the buffer.
6635      ((gnus-buffer-exists-p buffer)
6636       (save-excursion
6637         (set-buffer buffer)
6638         (gnus-deaden-summary))))))
6639
6640 (defun gnus-summary-wake-up-the-dead (&rest args)
6641   "Wake up the dead summary buffer."
6642   (interactive)
6643   (gnus-dead-summary-mode -1)
6644   (let ((name (buffer-name)))
6645     (when (string-match "Dead " name)
6646       (rename-buffer
6647        (concat (substring name 0 (match-beginning 0))
6648                (substring name (match-end 0)))
6649        t)))
6650   (gnus-message 3 "This dead summary is now alive again"))
6651
6652 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6653 (defun gnus-summary-fetch-faq (&optional faq-dir)
6654   "Fetch the FAQ for the current group.
6655 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6656 in."
6657   (interactive
6658    (list
6659     (when current-prefix-arg
6660       (completing-read
6661        "FAQ dir: " (and (listp gnus-group-faq-directory)
6662                         (mapcar (lambda (file) (list file))
6663                                 gnus-group-faq-directory))))))
6664   (let (gnus-faq-buffer)
6665     (when (setq gnus-faq-buffer
6666                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6667       (gnus-configure-windows 'summary-faq))))
6668
6669 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6670 (defun gnus-summary-describe-group (&optional force)
6671   "Describe the current newsgroup."
6672   (interactive "P")
6673   (gnus-group-describe-group force gnus-newsgroup-name))
6674
6675 (defun gnus-summary-describe-briefly ()
6676   "Describe summary mode commands briefly."
6677   (interactive)
6678   (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")))
6679
6680 ;; Walking around group mode buffer from summary mode.
6681
6682 (defun gnus-summary-next-group (&optional no-article target-group backward)
6683   "Exit current newsgroup and then select next unread newsgroup.
6684 If prefix argument NO-ARTICLE is non-nil, no article is selected
6685 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6686 previous group instead."
6687   (interactive "P")
6688   ;; Stop pre-fetching.
6689   (gnus-async-halt-prefetch)
6690   (let ((current-group gnus-newsgroup-name)
6691         (current-buffer (current-buffer))
6692         entered)
6693     (gnus-summary-exit t)
6694     (while (not entered)
6695       ;; Then we find what group we are supposed to enter.
6696       (set-buffer gnus-group-buffer)
6697       (gnus-group-jump-to-group current-group)
6698       (setq target-group
6699             (or target-group
6700                 (if (eq gnus-keep-same-level 'best)
6701                     (gnus-summary-best-group gnus-newsgroup-name)
6702                   (gnus-summary-search-group backward gnus-keep-same-level))))
6703       (if (not target-group)
6704           ;; There are no further groups, so we return to the group
6705           ;; buffer.
6706           (progn
6707             (gnus-message 5 "Returning to the group buffer")
6708             (setq entered t)
6709             (when (gnus-buffer-live-p current-buffer)
6710               (set-buffer current-buffer)
6711               (gnus-summary-exit))
6712             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6713         ;; We try to enter the target group.
6714         (gnus-group-jump-to-group target-group)
6715         (let ((unreads (gnus-group-group-unread)))
6716           (if (and (or (eq t unreads)
6717                        (and unreads (not (zerop unreads))))
6718                    (progn
6719                      ;; Now we semi-exit this group to update Xrefs
6720                      ;; and all variables.  We can't do a real exit,
6721                      ;; because the window conf must remain the same
6722                      ;; in case the user is prompted for info, and we
6723                      ;; don't want the window conf to change before
6724                      ;; that...
6725                      (when (gnus-buffer-live-p current-buffer)
6726                        (set-buffer current-buffer)
6727                        (gnus-summary-exit t))
6728                      (gnus-summary-read-group
6729                       target-group nil no-article
6730                       (and (buffer-name current-buffer) current-buffer)
6731                       nil backward)))
6732               (setq entered t)
6733             (setq current-group target-group
6734                   target-group nil)))))))
6735
6736 (defun gnus-summary-prev-group (&optional no-article)
6737   "Exit current newsgroup and then select previous unread newsgroup.
6738 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6739   (interactive "P")
6740   (gnus-summary-next-group no-article nil t))
6741
6742 ;; Walking around summary lines.
6743
6744 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6745   "Go to the first subject satisfying any non-nil constraint.
6746 If UNREAD is non-nil, the article should be unread.
6747 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6748 If UNSEED is non-nil, the article should be unseen.
6749 Returns the article selected or nil if there are no matching articles."
6750   (interactive "P")
6751   (cond
6752    ;; Empty summary.
6753    ((null gnus-newsgroup-data)
6754     (gnus-message 3 "No articles in the group")
6755     nil)
6756    ;; Pick the first article.
6757    ((not (or unread undownloaded unseen))
6758     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6759     (gnus-data-number (car gnus-newsgroup-data)))
6760    ;; Find the first unread article.
6761    (t
6762     (let ((data gnus-newsgroup-data))
6763       (while (and data
6764                   (let ((num (gnus-data-number (car data))))
6765                     (or (memq num gnus-newsgroup-unfetched)
6766                         (not (or (and unread
6767                                       (memq num gnus-newsgroup-unreads))
6768                                  (and undownloaded
6769                                       (memq num gnus-newsgroup-undownloaded))
6770                                  (and unseen
6771                                       (memq num gnus-newsgroup-unseen)))))))
6772         (setq data (cdr data)))
6773       (prog1 
6774           (if data
6775               (progn
6776                 (goto-char (gnus-data-pos (car data)))
6777                 (gnus-data-number (car data)))
6778             (gnus-message 3 "No more%s articles"
6779                           (let* ((r (when unread " unread"))
6780                                  (d (when undownloaded " undownloaded"))
6781                                  (s (when unseen " unseen"))
6782                                  (l (delq nil (list r d s))))
6783                             (cond ((= 3 (length l))
6784                                    (concat r "," d ", or" s))
6785                                   ((= 2 (length l))
6786                                    (concat (car l) ", or" (cadr l)))
6787                                   ((= 1 (length l))
6788                                    (car l))
6789                                   (t
6790                                    ""))))
6791             nil
6792             )
6793         (gnus-summary-position-point))))))
6794
6795 (defun gnus-summary-next-subject (n &optional unread dont-display)
6796   "Go to next N'th summary line.
6797 If N is negative, go to the previous N'th subject line.
6798 If UNREAD is non-nil, only unread articles are selected.
6799 The difference between N and the actual number of steps taken is
6800 returned."
6801   (interactive "p")
6802   (let ((backward (< n 0))
6803         (n (abs n)))
6804     (while (and (> n 0)
6805                 (if backward
6806                     (gnus-summary-find-prev unread)
6807                   (gnus-summary-find-next unread)))
6808       (unless (zerop (setq n (1- n)))
6809         (gnus-summary-show-thread)))
6810     (when (/= 0 n)
6811       (gnus-message 7 "No more%s articles"
6812                     (if unread " unread" "")))
6813     (unless dont-display
6814       (gnus-summary-recenter)
6815       (gnus-summary-position-point))
6816     n))
6817
6818 (defun gnus-summary-next-unread-subject (n)
6819   "Go to next N'th unread summary line."
6820   (interactive "p")
6821   (gnus-summary-next-subject n t))
6822
6823 (defun gnus-summary-prev-subject (n &optional unread)
6824   "Go to previous N'th summary line.
6825 If optional argument UNREAD is non-nil, only unread article is selected."
6826   (interactive "p")
6827   (gnus-summary-next-subject (- n) unread))
6828
6829 (defun gnus-summary-prev-unread-subject (n)
6830   "Go to previous N'th unread summary line."
6831   (interactive "p")
6832   (gnus-summary-next-subject (- n) t))
6833
6834 (defun gnus-summary-goto-subjects (articles)
6835   "Insert the subject header for ARTICLES in the current buffer."
6836   (save-excursion
6837     (dolist (article articles)
6838       (gnus-summary-goto-subject article t)))
6839   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6840   (gnus-summary-position-point))
6841  
6842 (defun gnus-summary-goto-subject (article &optional force silent)
6843   "Go the subject line of ARTICLE.
6844 If FORCE, also allow jumping to articles not currently shown."
6845   (interactive "nArticle number: ")
6846   (unless (numberp article)
6847     (error "Article %s is not a number" article))
6848   (let ((b (point))
6849         (data (gnus-data-find article)))
6850     ;; We read in the article if we have to.
6851     (and (not data)
6852          force
6853          (gnus-summary-insert-subject
6854           article
6855           (if (or (numberp force) (vectorp force)) force)
6856           t)
6857          (setq data (gnus-data-find article)))
6858     (goto-char b)
6859     (if (not data)
6860         (progn
6861           (unless silent
6862             (gnus-message 3 "Can't find article %d" article))
6863           nil)
6864       (let ((pt (gnus-data-pos data)))
6865         (goto-char pt)
6866         (gnus-summary-set-article-display-arrow pt))
6867       (gnus-summary-position-point)
6868       article)))
6869
6870 ;; Walking around summary lines with displaying articles.
6871
6872 (defun gnus-summary-expand-window (&optional arg)
6873   "Make the summary buffer take up the entire Emacs frame.
6874 Given a prefix, will force an `article' buffer configuration."
6875   (interactive "P")
6876   (if arg
6877       (gnus-configure-windows 'article 'force)
6878     (gnus-configure-windows 'summary 'force)))
6879
6880 (defun gnus-summary-display-article (article &optional all-header)
6881   "Display ARTICLE in article buffer."
6882   (when (gnus-buffer-live-p gnus-article-buffer)
6883     (with-current-buffer gnus-article-buffer
6884       (set-buffer-multibyte t)))
6885   (gnus-set-global-variables)
6886   (when (gnus-buffer-live-p gnus-article-buffer)
6887     (with-current-buffer gnus-article-buffer
6888       (setq gnus-article-charset gnus-newsgroup-charset)
6889       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6890       (set-buffer-multibyte t)))
6891   (if (null article)
6892       nil
6893     (prog1
6894         (if gnus-summary-display-article-function
6895             (funcall gnus-summary-display-article-function article all-header)
6896           (gnus-article-prepare article all-header))
6897       (with-current-buffer gnus-article-buffer
6898         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6899              nil))
6900       (gnus-run-hooks 'gnus-select-article-hook)
6901       (when (and gnus-current-article
6902                  (not (zerop gnus-current-article)))
6903         (gnus-summary-goto-subject gnus-current-article))
6904       (gnus-summary-recenter)
6905       (when (and gnus-use-trees gnus-show-threads)
6906         (gnus-possibly-generate-tree article)
6907         (gnus-highlight-selected-tree article))
6908       ;; Successfully display article.
6909       (gnus-article-set-window-start
6910        (cdr (assq article gnus-newsgroup-bookmarks))))))
6911
6912 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6913   "Select the current article.
6914 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6915 non-nil, the article will be re-fetched even if it already present in
6916 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6917 be displayed."
6918   ;; Make sure we are in the summary buffer to work around bbdb bug.
6919   (unless (eq major-mode 'gnus-summary-mode)
6920     (set-buffer gnus-summary-buffer))
6921   (let ((article (or article (gnus-summary-article-number)))
6922         (all-headers (not (not all-headers))) ;Must be T or NIL.
6923         ;; prevent redisplay to cause flashy displays
6924         (inhibit-redisplay t)
6925         gnus-summary-display-article-function)
6926     (and (not pseudo)
6927          (gnus-summary-article-pseudo-p article)
6928          (error "This is a pseudo-article"))
6929     (save-excursion
6930       (set-buffer gnus-summary-buffer)
6931       (if (or (and gnus-single-article-buffer
6932                    (or (null gnus-current-article)
6933                        (null gnus-article-current)
6934                        (null (get-buffer gnus-article-buffer))
6935                        (not (eq article (cdr gnus-article-current)))
6936                        (not (equal (car gnus-article-current)
6937                                    gnus-newsgroup-name))))
6938               (and (not gnus-single-article-buffer)
6939                    (or (null gnus-current-article)
6940                        (not (eq gnus-current-article article))))
6941               force)
6942           ;; The requested article is different from the current article.
6943           (progn
6944             (gnus-summary-display-article article all-headers)
6945             (gnus-article-set-window-start
6946              (cdr (assq article gnus-newsgroup-bookmarks)))
6947             article)
6948         'old))))
6949
6950 (defun gnus-summary-force-verify-and-decrypt ()
6951   "Display buttons for signed/encrypted parts and verify/decrypt them."
6952   (interactive)
6953   (let ((mm-verify-option 'known)
6954         (mm-decrypt-option 'known)
6955         (gnus-buttonized-mime-types (append (list "multipart/signed"
6956                                                   "multipart/encrypted")
6957                                             gnus-buttonized-mime-types)))
6958     (gnus-summary-select-article nil 'force)))
6959
6960 (defun gnus-summary-set-current-mark (&optional current-mark)
6961   "Obsolete function."
6962   nil)
6963
6964 (defun gnus-summary-next-article (&optional unread subject backward push)
6965   "Select the next article.
6966 If UNREAD, only unread articles are selected.
6967 If SUBJECT, only articles with SUBJECT are selected.
6968 If BACKWARD, the previous article is selected instead of the next."
6969   (interactive "P")
6970   (cond
6971    ;; Is there such an article?
6972    ((and (gnus-summary-search-forward unread subject backward)
6973          (or (gnus-summary-display-article (gnus-summary-article-number))
6974              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6975     (gnus-summary-position-point))
6976    ;; If not, we try the first unread, if that is wanted.
6977    ((and subject
6978          gnus-auto-select-same
6979          (gnus-summary-first-unread-article))
6980     (gnus-summary-position-point)
6981     (gnus-message 6 "Wrapped"))
6982    ;; Try to get next/previous article not displayed in this group.
6983    ((and gnus-auto-extend-newsgroup
6984          (not unread) (not subject))
6985     (gnus-summary-goto-article
6986      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6987      nil (count-lines (point-min) (point))))
6988    ;; Go to next/previous group.
6989    (t
6990     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6991       (gnus-summary-jump-to-group gnus-newsgroup-name))
6992     (let ((cmd last-command-char)
6993           (point
6994            (save-excursion
6995              (set-buffer gnus-group-buffer)
6996              (point)))
6997           (group
6998            (if (eq gnus-keep-same-level 'best)
6999                (gnus-summary-best-group gnus-newsgroup-name)
7000              (gnus-summary-search-group backward gnus-keep-same-level))))
7001       ;; For some reason, the group window gets selected.  We change
7002       ;; it back.
7003       (select-window (get-buffer-window (current-buffer)))
7004       ;; Select next unread newsgroup automagically.
7005       (cond
7006        ((or (not gnus-auto-select-next)
7007             (not cmd))
7008         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7009        ((or (eq gnus-auto-select-next 'quietly)
7010             (and (eq gnus-auto-select-next 'slightly-quietly)
7011                  push)
7012             (and (eq gnus-auto-select-next 'almost-quietly)
7013                  (gnus-summary-last-article-p)))
7014         ;; Select quietly.
7015         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7016             (gnus-summary-exit)
7017           (gnus-message 7 "No more%s articles (%s)..."
7018                         (if unread " unread" "")
7019                         (if group (concat "selecting " group)
7020                           "exiting"))
7021           (gnus-summary-next-group nil group backward)))
7022        (t
7023         (when (gnus-key-press-event-p last-input-event)
7024           (gnus-summary-walk-group-buffer
7025            gnus-newsgroup-name cmd unread backward point))))))))
7026
7027 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7028   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7029                       (?\C-p (gnus-group-prev-unread-group 1))))
7030         (cursor-in-echo-area t)
7031         keve key group ended prompt)
7032     (save-excursion
7033       (set-buffer gnus-group-buffer)
7034       (goto-char start)
7035       (setq group
7036             (if (eq gnus-keep-same-level 'best)
7037                 (gnus-summary-best-group gnus-newsgroup-name)
7038               (gnus-summary-search-group backward gnus-keep-same-level))))
7039     (while (not ended)
7040       (setq prompt
7041             (format
7042              "No more%s articles%s " (if unread " unread" "")
7043              (if (and group
7044                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7045                  (format " (Type %s for %s [%s])"
7046                          (single-key-description cmd) group
7047                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7048                (format " (Type %s to exit %s)"
7049                        (single-key-description cmd)
7050                        gnus-newsgroup-name))))
7051       ;; Confirm auto selection.
7052       (setq key (car (setq keve (gnus-read-event-char prompt)))
7053             ended t)
7054       (cond
7055        ((assq key keystrokes)
7056         (let ((obuf (current-buffer)))
7057           (switch-to-buffer gnus-group-buffer)
7058           (when group
7059             (gnus-group-jump-to-group group))
7060           (eval (cadr (assq key keystrokes)))
7061           (setq group (gnus-group-group-name))
7062           (switch-to-buffer obuf))
7063         (setq ended nil))
7064        ((equal key cmd)
7065         (if (or (not group)
7066                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7067             (gnus-summary-exit)
7068           (gnus-summary-next-group nil group backward)))
7069        (t
7070         (push (cdr keve) unread-command-events))))))
7071
7072 (defun gnus-summary-next-unread-article ()
7073   "Select unread article after current one."
7074   (interactive)
7075   (gnus-summary-next-article
7076    (or (not (eq gnus-summary-goto-unread 'never))
7077        (gnus-summary-last-article-p (gnus-summary-article-number)))
7078    (and gnus-auto-select-same
7079         (gnus-summary-article-subject))))
7080
7081 (defun gnus-summary-prev-article (&optional unread subject)
7082   "Select the article after the current one.
7083 If UNREAD is non-nil, only unread articles are selected."
7084   (interactive "P")
7085   (gnus-summary-next-article unread subject t))
7086
7087 (defun gnus-summary-prev-unread-article ()
7088   "Select unread article before current one."
7089   (interactive)
7090   (gnus-summary-prev-article
7091    (or (not (eq gnus-summary-goto-unread 'never))
7092        (gnus-summary-first-article-p (gnus-summary-article-number)))
7093    (and gnus-auto-select-same
7094         (gnus-summary-article-subject))))
7095
7096 (defun gnus-summary-next-page (&optional lines circular stop)
7097   "Show next page of the selected article.
7098 If at the end of the current article, select the next article.
7099 LINES says how many lines should be scrolled up.
7100
7101 If CIRCULAR is non-nil, go to the start of the article instead of
7102 selecting the next article when reaching the end of the current
7103 article.
7104
7105 If STOP is non-nil, just stop when reaching the end of the message."
7106   (interactive "P")
7107   (setq gnus-summary-buffer (current-buffer))
7108   (gnus-set-global-variables)
7109   (let ((article (gnus-summary-article-number))
7110         (article-window (get-buffer-window gnus-article-buffer t))
7111         endp)
7112     ;; If the buffer is empty, we have no article.
7113     (unless article
7114       (error "No article to select"))
7115     (gnus-configure-windows 'article)
7116     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7117         (if (and (eq gnus-summary-goto-unread 'never)
7118                  (not (gnus-summary-last-article-p article)))
7119             (gnus-summary-next-article)
7120           (gnus-summary-next-unread-article))
7121       (if (or (null gnus-current-article)
7122               (null gnus-article-current)
7123               (/= article (cdr gnus-article-current))
7124               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7125           ;; Selected subject is different from current article's.
7126           (gnus-summary-display-article article)
7127         (when article-window
7128           (gnus-eval-in-buffer-window gnus-article-buffer
7129             (setq endp (gnus-article-next-page lines)))
7130           (when endp
7131             (cond (stop
7132                    (gnus-message 3 "End of message"))
7133                   (circular
7134                    (gnus-summary-beginning-of-article))
7135                   (lines
7136                    (gnus-message 3 "End of message"))
7137                   ((null lines)
7138                    (if (and (eq gnus-summary-goto-unread 'never)
7139                             (not (gnus-summary-last-article-p article)))
7140                        (gnus-summary-next-article)
7141                      (gnus-summary-next-unread-article))))))))
7142     (gnus-summary-recenter)
7143     (gnus-summary-position-point)))
7144
7145 (defun gnus-summary-prev-page (&optional lines move)
7146   "Show previous page of selected article.
7147 Argument LINES specifies lines to be scrolled down.
7148 If MOVE, move to the previous unread article if point is at
7149 the beginning of the buffer."
7150   (interactive "P")
7151   (let ((article (gnus-summary-article-number))
7152         (article-window (get-buffer-window gnus-article-buffer t))
7153         endp)
7154     (gnus-configure-windows 'article)
7155     (if (or (null gnus-current-article)
7156             (null gnus-article-current)
7157             (/= article (cdr gnus-article-current))
7158             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7159         ;; Selected subject is different from current article's.
7160         (gnus-summary-display-article article)
7161       (gnus-summary-recenter)
7162       (when article-window
7163         (gnus-eval-in-buffer-window gnus-article-buffer
7164           (setq endp (gnus-article-prev-page lines)))
7165         (when (and move endp)
7166           (cond (lines
7167                  (gnus-message 3 "Beginning of message"))
7168                 ((null lines)
7169                  (if (and (eq gnus-summary-goto-unread 'never)
7170                           (not (gnus-summary-first-article-p article)))
7171                      (gnus-summary-prev-article)
7172                    (gnus-summary-prev-unread-article))))))))
7173   (gnus-summary-position-point))
7174
7175 (defun gnus-summary-prev-page-or-article (&optional lines)
7176   "Show previous page of selected article.
7177 Argument LINES specifies lines to be scrolled down.
7178 If at the beginning of the article, go to the next article."
7179   (interactive "P")
7180   (gnus-summary-prev-page lines t))
7181
7182 (defun gnus-summary-scroll-up (lines)
7183   "Scroll up (or down) one line current article.
7184 Argument LINES specifies lines to be scrolled up (or down if negative)."
7185   (interactive "p")
7186   (gnus-configure-windows 'article)
7187   (gnus-summary-show-thread)
7188   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7189     (gnus-eval-in-buffer-window gnus-article-buffer
7190       (cond ((> lines 0)
7191              (when (gnus-article-next-page lines)
7192                (gnus-message 3 "End of message")))
7193             ((< lines 0)
7194              (gnus-article-prev-page (- lines))))))
7195   (gnus-summary-recenter)
7196   (gnus-summary-position-point))
7197
7198 (defun gnus-summary-scroll-down (lines)
7199   "Scroll down (or up) one line current article.
7200 Argument LINES specifies lines to be scrolled down (or up if negative)."
7201   (interactive "p")
7202   (gnus-summary-scroll-up (- lines)))
7203
7204 (defun gnus-summary-next-same-subject ()
7205   "Select next article which has the same subject as current one."
7206   (interactive)
7207   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7208
7209 (defun gnus-summary-prev-same-subject ()
7210   "Select previous article which has the same subject as current one."
7211   (interactive)
7212   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7213
7214 (defun gnus-summary-next-unread-same-subject ()
7215   "Select next unread article which has the same subject as current one."
7216   (interactive)
7217   (gnus-summary-next-article t (gnus-summary-article-subject)))
7218
7219 (defun gnus-summary-prev-unread-same-subject ()
7220   "Select previous unread article which has the same subject as current one."
7221   (interactive)
7222   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7223
7224 (defun gnus-summary-first-unread-article ()
7225   "Select the first unread article.
7226 Return nil if there are no unread articles."
7227   (interactive)
7228   (prog1
7229       (when (gnus-summary-first-subject t)
7230         (gnus-summary-show-thread)
7231         (gnus-summary-first-subject t)
7232         (gnus-summary-display-article (gnus-summary-article-number)))
7233     (gnus-summary-position-point)))
7234
7235 (defun gnus-summary-first-unread-subject ()
7236   "Place the point on the subject line of the first unread article.
7237 Return nil if there are no unread articles."
7238   (interactive)
7239   (prog1
7240       (when (gnus-summary-first-subject t)
7241         (gnus-summary-show-thread)
7242         (gnus-summary-first-subject t))
7243     (gnus-summary-position-point)))
7244
7245 (defun gnus-summary-first-unseen-subject ()
7246   "Place the point on the subject line of the first unseen article.
7247 Return nil if there are no unseen articles."
7248   (interactive)
7249   (prog1
7250       (when (gnus-summary-first-subject nil nil t)
7251         (gnus-summary-show-thread)
7252         (gnus-summary-first-subject nil nil t))
7253     (gnus-summary-position-point)))
7254
7255 (defun gnus-summary-first-unseen-or-unread-subject ()
7256   "Place the point on the subject line of the first unseen article or,
7257 if all article have been seen, on the subject line of the first unread
7258 article."
7259   (interactive)
7260   (prog1
7261       (unless (when (gnus-summary-first-subject nil nil t)
7262                 (gnus-summary-show-thread)
7263                 (gnus-summary-first-subject nil nil t))
7264         (when (gnus-summary-first-subject t)
7265           (gnus-summary-show-thread)
7266           (gnus-summary-first-subject t)))
7267     (gnus-summary-position-point)))
7268
7269 (defun gnus-summary-first-article ()
7270   "Select the first article.
7271 Return nil if there are no articles."
7272   (interactive)
7273   (prog1
7274       (when (gnus-summary-first-subject)
7275         (gnus-summary-show-thread)
7276         (gnus-summary-first-subject)
7277         (gnus-summary-display-article (gnus-summary-article-number)))
7278     (gnus-summary-position-point)))
7279
7280 (defun gnus-summary-best-unread-article (&optional arg)
7281   "Select the unread article with the highest score.
7282 If given a prefix argument, select the next unread article that has a
7283 score higher than the default score."
7284   (interactive "P")
7285   (let ((article (if arg
7286                      (gnus-summary-better-unread-subject)
7287                    (gnus-summary-best-unread-subject))))
7288     (if article
7289         (gnus-summary-goto-article article)
7290       (error "No unread articles"))))
7291
7292 (defun gnus-summary-best-unread-subject ()
7293   "Select the unread subject with the highest score."
7294   (interactive)
7295   (let ((best -1000000)
7296         (data gnus-newsgroup-data)
7297         article score)
7298     (while data
7299       (and (gnus-data-unread-p (car data))
7300            (> (setq score
7301                     (gnus-summary-article-score (gnus-data-number (car data))))
7302               best)
7303            (setq best score
7304                  article (gnus-data-number (car data))))
7305       (setq data (cdr data)))
7306     (when article
7307       (gnus-summary-goto-subject article))
7308     (gnus-summary-position-point)
7309     article))
7310
7311 (defun gnus-summary-better-unread-subject ()
7312   "Select the first unread subject that has a score over the default score."
7313   (interactive)
7314   (let ((data gnus-newsgroup-data)
7315         article score)
7316     (while (and (setq article (gnus-data-number (car data)))
7317                 (or (gnus-data-read-p (car data))
7318                     (not (> (gnus-summary-article-score article)
7319                             gnus-summary-default-score))))
7320       (setq data (cdr data)))
7321     (when article
7322       (gnus-summary-goto-subject article))
7323     (gnus-summary-position-point)
7324     article))
7325
7326 (defun gnus-summary-last-subject ()
7327   "Go to the last displayed subject line in the group."
7328   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7329     (when article
7330       (gnus-summary-goto-subject article))))
7331
7332 (defun gnus-summary-goto-article (article &optional all-headers force)
7333   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7334 If ALL-HEADERS is non-nil, no header lines are hidden.
7335 If FORCE, go to the article even if it isn't displayed.  If FORCE
7336 is a number, it is the line the article is to be displayed on."
7337   (interactive
7338    (list
7339     (completing-read
7340      "Article number or Message-ID: "
7341      (mapcar (lambda (number) (list (int-to-string number)))
7342              gnus-newsgroup-limit))
7343     current-prefix-arg
7344     t))
7345   (prog1
7346       (if (and (stringp article)
7347                (string-match "@" article))
7348           (gnus-summary-refer-article article)
7349         (when (stringp article)
7350           (setq article (string-to-number article)))
7351         (if (gnus-summary-goto-subject article force)
7352             (gnus-summary-display-article article all-headers)
7353           (gnus-message 4 "Couldn't go to article %s" article) nil))
7354     (gnus-summary-position-point)))
7355
7356 (defun gnus-summary-goto-last-article ()
7357   "Go to the previously read article."
7358   (interactive)
7359   (prog1
7360       (when gnus-last-article
7361         (gnus-summary-goto-article gnus-last-article nil t))
7362     (gnus-summary-position-point)))
7363
7364 (defun gnus-summary-pop-article (number)
7365   "Pop one article off the history and go to the previous.
7366 NUMBER articles will be popped off."
7367   (interactive "p")
7368   (let (to)
7369     (setq gnus-newsgroup-history
7370           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7371     (if to
7372         (gnus-summary-goto-article (car to) nil t)
7373       (error "Article history empty")))
7374   (gnus-summary-position-point))
7375
7376 ;; Summary commands and functions for limiting the summary buffer.
7377
7378 (defun gnus-summary-limit-to-articles (n)
7379   "Limit the summary buffer to the next N articles.
7380 If not given a prefix, use the process marked articles instead."
7381   (interactive "P")
7382   (prog1
7383       (let ((articles (gnus-summary-work-articles n)))
7384         (setq gnus-newsgroup-processable nil)
7385         (gnus-summary-limit articles))
7386     (gnus-summary-position-point)))
7387
7388 (defun gnus-summary-pop-limit (&optional total)
7389   "Restore the previous limit.
7390 If given a prefix, remove all limits."
7391   (interactive "P")
7392   (when total
7393     (setq gnus-newsgroup-limits
7394           (list (mapcar (lambda (h) (mail-header-number h))
7395                         gnus-newsgroup-headers))))
7396   (unless gnus-newsgroup-limits
7397     (error "No limit to pop"))
7398   (prog1
7399       (gnus-summary-limit nil 'pop)
7400     (gnus-summary-position-point)))
7401
7402 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7403   "Limit the summary buffer to articles that have subjects that match a regexp.
7404 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7405   (interactive
7406    (list (read-string (if current-prefix-arg
7407                           "Exclude subject (regexp): "
7408                         "Limit to subject (regexp): "))
7409          nil current-prefix-arg))
7410   (unless header
7411     (setq header "subject"))
7412   (when (not (equal "" subject))
7413     (prog1
7414         (let ((articles (gnus-summary-find-matching
7415                          (or header "subject") subject 'all nil nil
7416                          not-matching)))
7417           (unless articles
7418             (error "Found no matches for \"%s\"" subject))
7419           (gnus-summary-limit articles))
7420       (gnus-summary-position-point))))
7421
7422 (defun gnus-summary-limit-to-author (from &optional not-matching)
7423   "Limit the summary buffer to articles that have authors that match a regexp.
7424 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7425   (interactive
7426    (list (read-string (if current-prefix-arg
7427                           "Exclude author (regexp): "
7428                         "Limit to author (regexp): "))
7429          current-prefix-arg))
7430   (gnus-summary-limit-to-subject from "from" not-matching))
7431
7432 (defun gnus-summary-limit-to-age (age &optional younger-p)
7433   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7434 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7435 articles that are younger than AGE days."
7436   (interactive
7437    (let ((younger current-prefix-arg)
7438          (days-got nil)
7439          days)
7440      (while (not days-got)
7441        (setq days (if younger
7442                       (read-string "Limit to articles younger than (in days, older when negative): ")
7443                     (read-string
7444                      "Limit to articles older than (in days, younger when negative): ")))
7445        (when (> (length days) 0)
7446          (setq days (read days)))
7447        (if (numberp days)
7448            (progn
7449              (setq days-got t)
7450              (if (< days 0)
7451                  (progn
7452                    (setq younger (not younger))
7453                    (setq days (* days -1)))))
7454          (message "Please enter a number.")
7455          (sleep-for 1)))
7456      (list days younger)))
7457   (prog1
7458       (let ((data gnus-newsgroup-data)
7459             (cutoff (days-to-time age))
7460             articles d date is-younger)
7461         (while (setq d (pop data))
7462           (when (and (vectorp (gnus-data-header d))
7463                      (setq date (mail-header-date (gnus-data-header d))))
7464             (setq is-younger (time-less-p
7465                               (time-since (condition-case ()
7466                                               (date-to-time date)
7467                                             (error '(0 0))))
7468                               cutoff))
7469             (when (if younger-p
7470                       is-younger
7471                     (not is-younger))
7472               (push (gnus-data-number d) articles))))
7473         (gnus-summary-limit (nreverse articles)))
7474     (gnus-summary-position-point)))
7475
7476 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7477   "Limit the summary buffer to articles that match an 'extra' header."
7478   (interactive
7479    (let ((header
7480           (intern
7481            (gnus-completing-read-with-default
7482             (symbol-name (car gnus-extra-headers))
7483             (if current-prefix-arg
7484                 "Exclude extra header:"
7485               "Limit extra header:")
7486             (mapcar (lambda (x)
7487                       (cons (symbol-name x) x))
7488                     gnus-extra-headers)
7489             nil
7490             t))))
7491      (list header
7492            (read-string (format "%s header %s (regexp): "
7493                                 (if current-prefix-arg "Exclude" "Limit to")
7494                                 header))
7495            current-prefix-arg)))
7496   (when (not (equal "" regexp))
7497     (prog1
7498         (let ((articles (gnus-summary-find-matching
7499                          (cons 'extra header) regexp 'all nil nil
7500                          not-matching)))
7501           (unless articles
7502             (error "Found no matches for \"%s\"" regexp))
7503           (gnus-summary-limit articles))
7504       (gnus-summary-position-point))))
7505
7506 (defun gnus-summary-limit-to-display-predicate ()
7507   "Limit the summary buffer to the predicated in the `display' group parameter."
7508   (interactive)
7509   (unless gnus-newsgroup-display
7510     (error "There is no `display' group parameter"))
7511   (let (articles)
7512     (dolist (number gnus-newsgroup-articles)
7513       (when (funcall gnus-newsgroup-display)
7514         (push number articles)))
7515     (gnus-summary-limit articles))
7516   (gnus-summary-position-point))
7517
7518 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7519 (make-obsolete
7520  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7521
7522 (defun gnus-summary-limit-to-unread (&optional all)
7523   "Limit the summary buffer to articles that are not marked as read.
7524 If ALL is non-nil, limit strictly to unread articles."
7525   (interactive "P")
7526   (if all
7527       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7528     (gnus-summary-limit-to-marks
7529      ;; Concat all the marks that say that an article is read and have
7530      ;; those removed.
7531      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7532            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7533            gnus-low-score-mark gnus-expirable-mark
7534            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7535            gnus-duplicate-mark gnus-souped-mark)
7536      'reverse)))
7537
7538 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7539 (make-obsolete 'gnus-summary-delete-marked-with
7540                'gnus-summary-limit-exclude-marks)
7541
7542 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7543   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7544 If REVERSE, limit the summary buffer to articles that are marked
7545 with MARKS.  MARKS can either be a string of marks or a list of marks.
7546 Returns how many articles were removed."
7547   (interactive "sMarks: ")
7548   (gnus-summary-limit-to-marks marks t))
7549
7550 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7551   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7552 If REVERSE (the prefix), limit the summary buffer to articles that are
7553 not marked with MARKS.  MARKS can either be a string of marks or a
7554 list of marks.
7555 Returns how many articles were removed."
7556   (interactive "sMarks: \nP")
7557   (prog1
7558       (let ((data gnus-newsgroup-data)
7559             (marks (if (listp marks) marks
7560                      (append marks nil))) ; Transform to list.
7561             articles)
7562         (while data
7563           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7564                   (memq (gnus-data-mark (car data)) marks))
7565             (push (gnus-data-number (car data)) articles))
7566           (setq data (cdr data)))
7567         (gnus-summary-limit articles))
7568     (gnus-summary-position-point)))
7569
7570 (defun gnus-summary-limit-to-score (score)
7571   "Limit to articles with score at or above SCORE."
7572   (interactive "NLimit to articles with score of at least: ")
7573   (let ((data gnus-newsgroup-data)
7574         articles)
7575     (while data
7576       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7577                 score)
7578         (push (gnus-data-number (car data)) articles))
7579       (setq data (cdr data)))
7580     (prog1
7581         (gnus-summary-limit articles)
7582       (gnus-summary-position-point))))
7583
7584 (defun gnus-summary-limit-to-unseen ()
7585   "Limit to unseen articles."
7586   (interactive)
7587   (prog1
7588       (gnus-summary-limit gnus-newsgroup-unseen)
7589     (gnus-summary-position-point)))
7590
7591 (defun gnus-summary-limit-include-thread (id)
7592   "Display all the hidden articles that is in the thread with ID in it.
7593 When called interactively, ID is the Message-ID of the current
7594 article."
7595   (interactive (list (mail-header-id (gnus-summary-article-header))))
7596   (let ((articles (gnus-articles-in-thread
7597                    (gnus-id-to-thread (gnus-root-id id)))))
7598     (prog1
7599         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7600       (gnus-summary-limit-include-matching-articles
7601        "subject"
7602        (regexp-quote (gnus-simplify-subject-re
7603                       (mail-header-subject (gnus-id-to-header id)))))
7604       (gnus-summary-position-point))))
7605
7606 (defun gnus-summary-limit-include-matching-articles (header regexp)
7607   "Display all the hidden articles that have HEADERs that match REGEXP."
7608   (interactive (list (read-string "Match on header: ")
7609                      (read-string "Regexp: ")))
7610   (let ((articles (gnus-find-matching-articles header regexp)))
7611     (prog1
7612         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7613       (gnus-summary-position-point))))
7614
7615 (defun gnus-summary-insert-dormant-articles ()
7616   "Insert all the dormat articles for this group into the current buffer."
7617   (interactive)
7618   (let ((gnus-verbose (max 6 gnus-verbose)))
7619     (if (not gnus-newsgroup-dormant)
7620         (gnus-message 3 "No cached articles for this group")
7621       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7622
7623 (defun gnus-summary-limit-include-dormant ()
7624   "Display all the hidden articles that are marked as dormant.
7625 Note that this command only works on a subset of the articles currently
7626 fetched for this group."
7627   (interactive)
7628   (unless gnus-newsgroup-dormant
7629     (error "There are no dormant articles in this group"))
7630   (prog1
7631       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7632     (gnus-summary-position-point)))
7633
7634 (defun gnus-summary-limit-exclude-dormant ()
7635   "Hide all dormant articles."
7636   (interactive)
7637   (prog1
7638       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7639     (gnus-summary-position-point)))
7640
7641 (defun gnus-summary-limit-exclude-childless-dormant ()
7642   "Hide all dormant articles that have no children."
7643   (interactive)
7644   (let ((data (gnus-data-list t))
7645         articles d children)
7646     ;; Find all articles that are either not dormant or have
7647     ;; children.
7648     (while (setq d (pop data))
7649       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7650                 (and (setq children
7651                            (gnus-article-children (gnus-data-number d)))
7652                      (let (found)
7653                        (while children
7654                          (when (memq (car children) articles)
7655                            (setq children nil
7656                                  found t))
7657                          (pop children))
7658                        found)))
7659         (push (gnus-data-number d) articles)))
7660     ;; Do the limiting.
7661     (prog1
7662         (gnus-summary-limit articles)
7663       (gnus-summary-position-point))))
7664
7665 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7666   "Mark all unread excluded articles as read.
7667 If ALL, mark even excluded ticked and dormants as read."
7668   (interactive "P")
7669   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7670   (let ((articles (gnus-sorted-ndifference
7671                    (sort
7672                     (mapcar (lambda (h) (mail-header-number h))
7673                             gnus-newsgroup-headers)
7674                     '<)
7675                    gnus-newsgroup-limit))
7676         article)
7677     (setq gnus-newsgroup-unreads
7678           (gnus-sorted-intersection gnus-newsgroup-unreads
7679                                     gnus-newsgroup-limit))
7680     (if all
7681         (setq gnus-newsgroup-dormant nil
7682               gnus-newsgroup-marked nil
7683               gnus-newsgroup-reads
7684               (nconc
7685                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7686                gnus-newsgroup-reads))
7687       (while (setq article (pop articles))
7688         (unless (or (memq article gnus-newsgroup-dormant)
7689                     (memq article gnus-newsgroup-marked))
7690           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7691
7692 (defun gnus-summary-limit (articles &optional pop)
7693   (if pop
7694       ;; We pop the previous limit off the stack and use that.
7695       (setq articles (car gnus-newsgroup-limits)
7696             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7697     ;; We use the new limit, so we push the old limit on the stack.
7698     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7699   ;; Set the limit.
7700   (setq gnus-newsgroup-limit articles)
7701   (let ((total (length gnus-newsgroup-data))
7702         (data (gnus-data-find-list (gnus-summary-article-number)))
7703         (gnus-summary-mark-below nil)   ; Inhibit this.
7704         found)
7705     ;; This will do all the work of generating the new summary buffer
7706     ;; according to the new limit.
7707     (gnus-summary-prepare)
7708     ;; Hide any threads, possibly.
7709     (gnus-summary-maybe-hide-threads)
7710     ;; Try to return to the article you were at, or one in the
7711     ;; neighborhood.
7712     (when data
7713       ;; We try to find some article after the current one.
7714       (while data
7715         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7716           (setq data nil
7717                 found t))
7718         (setq data (cdr data))))
7719     (unless found
7720       ;; If there is no data, that means that we were after the last
7721       ;; article.  The same goes when we can't find any articles
7722       ;; after the current one.
7723       (goto-char (point-max))
7724       (gnus-summary-find-prev))
7725     (gnus-set-mode-line 'summary)
7726     ;; We return how many articles were removed from the summary
7727     ;; buffer as a result of the new limit.
7728     (- total (length gnus-newsgroup-data))))
7729
7730 (defsubst gnus-invisible-cut-children (threads)
7731   (let ((num 0))
7732     (while threads
7733       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7734         (incf num))
7735       (pop threads))
7736     (< num 2)))
7737
7738 (defsubst gnus-cut-thread (thread)
7739   "Go forwards in the thread until we find an article that we want to display."
7740   (when (or (eq gnus-fetch-old-headers 'some)
7741             (eq gnus-fetch-old-headers 'invisible)
7742             (numberp gnus-fetch-old-headers)
7743             (eq gnus-build-sparse-threads 'some)
7744             (eq gnus-build-sparse-threads 'more))
7745     ;; Deal with old-fetched headers and sparse threads.
7746     (while (and
7747             thread
7748             (or
7749              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7750              (gnus-summary-article-ancient-p
7751               (mail-header-number (car thread))))
7752             (if (or (<= (length (cdr thread)) 1)
7753                     (eq gnus-fetch-old-headers 'invisible))
7754                 (setq gnus-newsgroup-limit
7755                       (delq (mail-header-number (car thread))
7756                             gnus-newsgroup-limit)
7757                       thread (cadr thread))
7758               (when (gnus-invisible-cut-children (cdr thread))
7759                 (let ((th (cdr thread)))
7760                   (while th
7761                     (if (memq (mail-header-number (caar th))
7762                               gnus-newsgroup-limit)
7763                         (setq thread (car th)
7764                               th nil)
7765                       (setq th (cdr th))))))))))
7766   thread)
7767
7768 (defun gnus-cut-threads (threads)
7769   "Cut off all uninteresting articles from the beginning of threads."
7770   (when (or (eq gnus-fetch-old-headers 'some)
7771             (eq gnus-fetch-old-headers 'invisible)
7772             (numberp gnus-fetch-old-headers)
7773             (eq gnus-build-sparse-threads 'some)
7774             (eq gnus-build-sparse-threads 'more))
7775     (let ((th threads))
7776       (while th
7777         (setcar th (gnus-cut-thread (car th)))
7778         (setq th (cdr th)))))
7779   ;; Remove nixed out threads.
7780   (delq nil threads))
7781
7782 (defun gnus-summary-initial-limit (&optional show-if-empty)
7783   "Figure out what the initial limit is supposed to be on group entry.
7784 This entails weeding out unwanted dormants, low-scored articles,
7785 fetch-old-headers verbiage, and so on."
7786   ;; Most groups have nothing to remove.
7787   (if (or gnus-inhibit-limiting
7788           (and (null gnus-newsgroup-dormant)
7789                (eq gnus-newsgroup-display 'gnus-not-ignore)
7790                (not (eq gnus-fetch-old-headers 'some))
7791                (not (numberp gnus-fetch-old-headers))
7792                (not (eq gnus-fetch-old-headers 'invisible))
7793                (null gnus-summary-expunge-below)
7794                (not (eq gnus-build-sparse-threads 'some))
7795                (not (eq gnus-build-sparse-threads 'more))
7796                (null gnus-thread-expunge-below)
7797                (not gnus-use-nocem)))
7798       ()                                ; Do nothing.
7799     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7800     (setq gnus-newsgroup-limit nil)
7801     (mapatoms
7802      (lambda (node)
7803        (unless (car (symbol-value node))
7804          ;; These threads have no parents -- they are roots.
7805          (let ((nodes (cdr (symbol-value node)))
7806                thread)
7807            (while nodes
7808              (if (and gnus-thread-expunge-below
7809                       (< (gnus-thread-total-score (car nodes))
7810                          gnus-thread-expunge-below))
7811                  (gnus-expunge-thread (pop nodes))
7812                (setq thread (pop nodes))
7813                (gnus-summary-limit-children thread))))))
7814      gnus-newsgroup-dependencies)
7815     ;; If this limitation resulted in an empty group, we might
7816     ;; pop the previous limit and use it instead.
7817     (when (and (not gnus-newsgroup-limit)
7818                show-if-empty)
7819       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7820     gnus-newsgroup-limit))
7821
7822 (defun gnus-summary-limit-children (thread)
7823   "Return 1 if this subthread is visible and 0 if it is not."
7824   ;; First we get the number of visible children to this thread.  This
7825   ;; is done by recursing down the thread using this function, so this
7826   ;; will really go down to a leaf article first, before slowly
7827   ;; working its way up towards the root.
7828   (when thread
7829     (let* ((max-lisp-eval-depth 5000)
7830            (children
7831            (if (cdr thread)
7832                (apply '+ (mapcar 'gnus-summary-limit-children
7833                                  (cdr thread)))
7834              0))
7835           (number (mail-header-number (car thread)))
7836           score)
7837       (if (and
7838            (not (memq number gnus-newsgroup-marked))
7839            (or
7840             ;; If this article is dormant and has absolutely no visible
7841             ;; children, then this article isn't visible.
7842             (and (memq number gnus-newsgroup-dormant)
7843                  (zerop children))
7844             ;; If this is "fetch-old-headered" and there is no
7845             ;; visible children, then we don't want this article.
7846             (and (or (eq gnus-fetch-old-headers 'some)
7847                      (numberp gnus-fetch-old-headers))
7848                  (gnus-summary-article-ancient-p number)
7849                  (zerop children))
7850             ;; If this is "fetch-old-headered" and `invisible', then
7851             ;; we don't want this article.
7852             (and (eq gnus-fetch-old-headers 'invisible)
7853                  (gnus-summary-article-ancient-p number))
7854             ;; If this is a sparsely inserted article with no children,
7855             ;; we don't want it.
7856             (and (eq gnus-build-sparse-threads 'some)
7857                  (gnus-summary-article-sparse-p number)
7858                  (zerop children))
7859             ;; If we use expunging, and this article is really
7860             ;; low-scored, then we don't want this article.
7861             (when (and gnus-summary-expunge-below
7862                        (< (setq score
7863                                 (or (cdr (assq number gnus-newsgroup-scored))
7864                                     gnus-summary-default-score))
7865                           gnus-summary-expunge-below))
7866               ;; We increase the expunge-tally here, but that has
7867               ;; nothing to do with the limits, really.
7868               (incf gnus-newsgroup-expunged-tally)
7869               ;; We also mark as read here, if that's wanted.
7870               (when (and gnus-summary-mark-below
7871                          (< score gnus-summary-mark-below))
7872                 (setq gnus-newsgroup-unreads
7873                       (delq number gnus-newsgroup-unreads))
7874                 (if gnus-newsgroup-auto-expire
7875                     (push number gnus-newsgroup-expirable)
7876                   (push (cons number gnus-low-score-mark)
7877                         gnus-newsgroup-reads)))
7878               t)
7879             ;; Do the `display' group parameter.
7880             (and gnus-newsgroup-display
7881                  (not (funcall gnus-newsgroup-display)))
7882             ;; Check NoCeM things.
7883             (if (and gnus-use-nocem
7884                      (gnus-nocem-unwanted-article-p
7885                       (mail-header-id (car thread))))
7886                 (progn
7887                   (setq gnus-newsgroup-unreads
7888                         (delq number gnus-newsgroup-unreads))
7889                   t))))
7890           ;; Nope, invisible article.
7891           0
7892         ;; Ok, this article is to be visible, so we add it to the limit
7893         ;; and return 1.
7894         (push number gnus-newsgroup-limit)
7895         1))))
7896
7897 (defun gnus-expunge-thread (thread)
7898   "Mark all articles in THREAD as read."
7899   (let* ((number (mail-header-number (car thread))))
7900     (incf gnus-newsgroup-expunged-tally)
7901     ;; We also mark as read here, if that's wanted.
7902     (setq gnus-newsgroup-unreads
7903           (delq number gnus-newsgroup-unreads))
7904     (if gnus-newsgroup-auto-expire
7905         (push number gnus-newsgroup-expirable)
7906       (push (cons number gnus-low-score-mark)
7907             gnus-newsgroup-reads)))
7908   ;; Go recursively through all subthreads.
7909   (mapcar 'gnus-expunge-thread (cdr thread)))
7910
7911 ;; Summary article oriented commands
7912
7913 (defun gnus-summary-refer-parent-article (n)
7914   "Refer parent article N times.
7915 If N is negative, go to ancestor -N instead.
7916 The difference between N and the number of articles fetched is returned."
7917   (interactive "p")
7918   (let ((skip 1)
7919         error header ref)
7920     (when (not (natnump n))
7921       (setq skip (abs n)
7922             n 1))
7923     (while (and (> n 0)
7924                 (not error))
7925       (setq header (gnus-summary-article-header))
7926       (if (and (eq (mail-header-number header)
7927                    (cdr gnus-article-current))
7928                (equal gnus-newsgroup-name
7929                       (car gnus-article-current)))
7930           ;; If we try to find the parent of the currently
7931           ;; displayed article, then we take a look at the actual
7932           ;; References header, since this is slightly more
7933           ;; reliable than the References field we got from the
7934           ;; server.
7935           (save-excursion
7936             (set-buffer gnus-original-article-buffer)
7937             (nnheader-narrow-to-headers)
7938             (unless (setq ref (message-fetch-field "references"))
7939               (setq ref (message-fetch-field "in-reply-to")))
7940             (widen))
7941         (setq ref
7942               ;; It's not the current article, so we take a bet on
7943               ;; the value we got from the server.
7944               (mail-header-references header)))
7945       (if (and ref
7946                (not (equal ref "")))
7947           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7948             (gnus-message 1 "Couldn't find parent"))
7949         (gnus-message 1 "No references in article %d"
7950                       (gnus-summary-article-number))
7951         (setq error t))
7952       (decf n))
7953     (gnus-summary-position-point)
7954     n))
7955
7956 (defun gnus-summary-refer-references ()
7957   "Fetch all articles mentioned in the References header.
7958 Return the number of articles fetched."
7959   (interactive)
7960   (let ((ref (mail-header-references (gnus-summary-article-header)))
7961         (current (gnus-summary-article-number))
7962         (n 0))
7963     (if (or (not ref)
7964             (equal ref ""))
7965         (error "No References in the current article")
7966       ;; For each Message-ID in the References header...
7967       (while (string-match "<[^>]*>" ref)
7968         (incf n)
7969         ;; ... fetch that article.
7970         (gnus-summary-refer-article
7971          (prog1 (match-string 0 ref)
7972            (setq ref (substring ref (match-end 0))))))
7973       (gnus-summary-goto-subject current)
7974       (gnus-summary-position-point)
7975       n)))
7976
7977 (defun gnus-summary-refer-thread (&optional limit)
7978   "Fetch all articles in the current thread.
7979 If LIMIT (the numerical prefix), fetch that many old headers instead
7980 of what's specified by the `gnus-refer-thread-limit' variable."
7981   (interactive "P")
7982   (let ((id (mail-header-id (gnus-summary-article-header)))
7983         (limit (if limit (prefix-numeric-value limit)
7984                  gnus-refer-thread-limit)))
7985     (unless (eq gnus-fetch-old-headers 'invisible)
7986       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7987       ;; Retrieve the headers and read them in.
7988       (if (eq (gnus-retrieve-headers
7989                (list (min
7990                       (+ (mail-header-number
7991                           (gnus-summary-article-header))
7992                          limit)
7993                       gnus-newsgroup-end))
7994                gnus-newsgroup-name (* limit 2))
7995               'nov)
7996           (gnus-build-all-threads)
7997         (error "Can't fetch thread from backends that don't support NOV"))
7998       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7999     (gnus-summary-limit-include-thread id)))
8000
8001 (defun gnus-summary-refer-article (message-id)
8002   "Fetch an article specified by MESSAGE-ID."
8003   (interactive "sMessage-ID: ")
8004   (when (and (stringp message-id)
8005              (not (zerop (length message-id))))
8006     ;; Construct the correct Message-ID if necessary.
8007     ;; Suggested by tale@pawl.rpi.edu.
8008     (unless (string-match "^<" message-id)
8009       (setq message-id (concat "<" message-id)))
8010     (unless (string-match ">$" message-id)
8011       (setq message-id (concat message-id ">")))
8012     (let* ((header (gnus-id-to-header message-id))
8013            (sparse (and header
8014                         (gnus-summary-article-sparse-p
8015                          (mail-header-number header))
8016                         (memq (mail-header-number header)
8017                               gnus-newsgroup-limit)))
8018            number)
8019       (cond
8020        ;; If the article is present in the buffer we just go to it.
8021        ((and header
8022              (or (not (gnus-summary-article-sparse-p
8023                        (mail-header-number header)))
8024                  sparse))
8025         (prog1
8026             (gnus-summary-goto-article
8027              (mail-header-number header) nil t)
8028           (when sparse
8029             (gnus-summary-update-article (mail-header-number header)))))
8030        (t
8031         ;; We fetch the article.
8032         (catch 'found
8033           (dolist (gnus-override-method (gnus-refer-article-methods))
8034             (gnus-check-server gnus-override-method)
8035             ;; Fetch the header, and display the article.
8036             (when (setq number (gnus-summary-insert-subject message-id))
8037               (gnus-summary-select-article nil nil nil number)
8038               (throw 'found t)))
8039           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8040
8041 (defun gnus-refer-article-methods ()
8042   "Return a list of referable methods."
8043   (cond
8044    ;; No method, so we default to current and native.
8045    ((null gnus-refer-article-method)
8046     (list gnus-current-select-method gnus-select-method))
8047    ;; Current.
8048    ((eq 'current gnus-refer-article-method)
8049     (list gnus-current-select-method))
8050    ;; List of select methods.
8051    ((not (and (symbolp (car gnus-refer-article-method))
8052               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8053     (let (out)
8054       (dolist (method gnus-refer-article-method)
8055         (push (if (eq 'current method)
8056                   gnus-current-select-method
8057                 method)
8058               out))
8059       (nreverse out)))
8060    ;; One single select method.
8061    (t
8062     (list gnus-refer-article-method))))
8063
8064 (defun gnus-summary-edit-parameters ()
8065   "Edit the group parameters of the current group."
8066   (interactive)
8067   (gnus-group-edit-group gnus-newsgroup-name 'params))
8068
8069 (defun gnus-summary-customize-parameters ()
8070   "Customize the group parameters of the current group."
8071   (interactive)
8072   (gnus-group-customize gnus-newsgroup-name))
8073
8074 (defun gnus-summary-enter-digest-group (&optional force)
8075   "Enter an nndoc group based on the current article.
8076 If FORCE, force a digest interpretation.  If not, try
8077 to guess what the document format is."
8078   (interactive "P")
8079   (let ((conf gnus-current-window-configuration))
8080     (save-excursion
8081       (gnus-summary-select-article))
8082     (setq gnus-current-window-configuration conf)
8083     (let* ((name (format "%s-%d"
8084                          (gnus-group-prefixed-name
8085                           gnus-newsgroup-name (list 'nndoc ""))
8086                          (save-excursion
8087                            (set-buffer gnus-summary-buffer)
8088                            gnus-current-article)))
8089            (ogroup gnus-newsgroup-name)
8090            (params (append (gnus-info-params (gnus-get-info ogroup))
8091                            (list (cons 'to-group ogroup))
8092                            (list (cons 'save-article-group ogroup))))
8093            (case-fold-search t)
8094            (buf (current-buffer))
8095            dig to-address)
8096       (save-excursion
8097         (set-buffer gnus-original-article-buffer)
8098         ;; Have the digest group inherit the main mail address of
8099         ;; the parent article.
8100         (when (setq to-address (or (gnus-fetch-field "reply-to")
8101                                    (gnus-fetch-field "from")))
8102           (setq params (append
8103                         (list (cons 'to-address
8104                                     (funcall gnus-decode-encoded-word-function
8105                                              to-address))))))
8106         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8107         (insert-buffer-substring gnus-original-article-buffer)
8108         ;; Remove lines that may lead nndoc to misinterpret the
8109         ;; document type.
8110         (narrow-to-region
8111          (goto-char (point-min))
8112          (or (search-forward "\n\n" nil t) (point)))
8113         (goto-char (point-min))
8114         (delete-matching-lines "^Path:\\|^From ")
8115         (widen))
8116       (unwind-protect
8117           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8118                     (gnus-newsgroup-ephemeral-ignored-charsets
8119                      gnus-newsgroup-ignored-charsets))
8120                 (gnus-group-read-ephemeral-group
8121                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8122                               (nndoc-article-type
8123                                ,(if force 'mbox 'guess)))
8124                  t nil nil nil
8125                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8126                                                         "ADAPT")))))
8127               ;; Make all postings to this group go to the parent group.
8128               (nconc (gnus-info-params (gnus-get-info name))
8129                      params)
8130             ;; Couldn't select this doc group.
8131             (switch-to-buffer buf)
8132             (gnus-set-global-variables)
8133             (gnus-configure-windows 'summary)
8134             (gnus-message 3 "Article couldn't be entered?"))
8135         (kill-buffer dig)))))
8136
8137 (defun gnus-summary-read-document (n)
8138   "Open a new group based on the current article(s).
8139 This will allow you to read digests and other similar
8140 documents as newsgroups.
8141 Obeys the standard process/prefix convention."
8142   (interactive "P")
8143   (let* ((articles (gnus-summary-work-articles n))
8144          (ogroup gnus-newsgroup-name)
8145          (params (append (gnus-info-params (gnus-get-info ogroup))
8146                          (list (cons 'to-group ogroup))))
8147          article group egroup groups vgroup)
8148     (while (setq article (pop articles))
8149       (setq group (format "%s-%d" gnus-newsgroup-name article))
8150       (gnus-summary-remove-process-mark article)
8151       (when (gnus-summary-display-article article)
8152         (save-excursion
8153           (with-temp-buffer
8154             (insert-buffer-substring gnus-original-article-buffer)
8155             ;; Remove some headers that may lead nndoc to make
8156             ;; the wrong guess.
8157             (message-narrow-to-head)
8158             (goto-char (point-min))
8159             (delete-matching-lines "^\\(Path\\):\\|^From ")
8160             (widen)
8161             (if (setq egroup
8162                       (gnus-group-read-ephemeral-group
8163                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8164                                      (nndoc-article-type guess))
8165                        t nil t))
8166                 (progn
8167                   ;; Make all postings to this group go to the parent group.
8168                   (nconc (gnus-info-params (gnus-get-info egroup))
8169                          params)
8170                   (push egroup groups))
8171               ;; Couldn't select this doc group.
8172               (gnus-error 3 "Article couldn't be entered"))))))
8173     ;; Now we have selected all the documents.
8174     (cond
8175      ((not groups)
8176       (error "None of the articles could be interpreted as documents"))
8177      ((gnus-group-read-ephemeral-group
8178        (setq vgroup (format
8179                      "nnvirtual:%s-%s" gnus-newsgroup-name
8180                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8181        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8182        t
8183        (cons (current-buffer) 'summary)))
8184      (t
8185       (error "Couldn't select virtual nndoc group")))))
8186
8187 (defun gnus-summary-isearch-article (&optional regexp-p)
8188   "Do incremental search forward on the current article.
8189 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8190   (interactive "P")
8191   (let* ((gnus-inhibit-treatment t)
8192          (old (gnus-summary-select-article)))
8193     (gnus-configure-windows 'article)
8194     (gnus-eval-in-buffer-window gnus-article-buffer
8195       (save-restriction
8196         (widen)
8197         (when (eq 'old old)
8198           (gnus-article-show-all-headers))
8199         (goto-char (point-min))
8200         (isearch-forward regexp-p)))))
8201
8202 (defun gnus-summary-search-article-forward (regexp &optional backward)
8203   "Search for an article containing REGEXP forward.
8204 If BACKWARD, search backward instead."
8205   (interactive
8206    (list (read-string
8207           (format "Search article %s (regexp%s): "
8208                   (if current-prefix-arg "backward" "forward")
8209                   (if gnus-last-search-regexp
8210                       (concat ", default " gnus-last-search-regexp)
8211                     "")))
8212          current-prefix-arg))
8213   (if (string-equal regexp "")
8214       (setq regexp (or gnus-last-search-regexp ""))
8215     (setq gnus-last-search-regexp regexp)
8216     (setq gnus-article-before-search gnus-current-article))
8217   ;; Intentionally set gnus-last-article.
8218   (setq gnus-last-article gnus-article-before-search)
8219   (let ((gnus-last-article gnus-last-article))
8220     (if (gnus-summary-search-article regexp backward)
8221         (gnus-summary-show-thread)
8222       (error "Search failed: \"%s\"" regexp))))
8223
8224 (defun gnus-summary-search-article-backward (regexp)
8225   "Search for an article containing REGEXP backward."
8226   (interactive
8227    (list (read-string
8228           (format "Search article backward (regexp%s): "
8229                   (if gnus-last-search-regexp
8230                       (concat ", default " gnus-last-search-regexp)
8231                     "")))))
8232   (gnus-summary-search-article-forward regexp 'backward))
8233
8234 (eval-when-compile
8235   (defmacro gnus-summary-search-article-position-point (regexp backward)
8236     "Dehighlight the last matched text and goto the beginning position."
8237     (` (if (and gnus-summary-search-article-matched-data
8238                 (let ((text (caddr gnus-summary-search-article-matched-data))
8239                       (inhibit-read-only t)
8240                       buffer-read-only)
8241                   (delete-region
8242                    (goto-char (car gnus-summary-search-article-matched-data))
8243                    (cadr gnus-summary-search-article-matched-data))
8244                   (insert text)
8245                   (string-match (, regexp) text)))
8246            (if (, backward) (beginning-of-line) (end-of-line))
8247          (goto-char (if (, backward) (point-max) (point-min))))))
8248
8249   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8250     "Place point where X-Face image is displayed."
8251     (if (featurep 'xemacs)
8252         (` (let ((end (if (search-forward "\n\n" nil t)
8253                           (goto-char (1- (point)))
8254                         (point-min)))
8255                  extent)
8256              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8257              (unless (and (re-search-forward "^From:" end t)
8258                           (setq extent (extent-at (point)))
8259                           (extent-begin-glyph extent))
8260                (goto-char (, opoint)))))
8261       (` (let ((end (if (search-forward "\n\n" nil t)
8262                         (goto-char (1- (point)))
8263                       (point-min)))
8264                (start (or (search-backward "\n\n" nil t) (point-min))))
8265            (goto-char
8266             (or (text-property-any start end 'x-face-image t);; x-face-e21
8267                 (text-property-any start end 'x-face-mule-bitmap-image t)
8268                 (, opoint)))))))
8269
8270   (defmacro gnus-summary-search-article-highlight-matched-text
8271     (backward treated x-face)
8272     "Highlight matched text in the function `gnus-summary-search-article'."
8273     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8274              (end (set-marker (make-marker) (match-end 0)))
8275              (inhibit-read-only t)
8276              buffer-read-only)
8277          (unless treated
8278            (let ((,@
8279                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8280                     (mapcar
8281                      (lambda (item) (setq items (delq item items)))
8282                      '(gnus-treat-buttonize
8283                        gnus-treat-fill-article
8284                        gnus-treat-fill-long-lines
8285                        gnus-treat-emphasize
8286                        gnus-treat-highlight-headers
8287                        gnus-treat-highlight-citation
8288                        gnus-treat-highlight-signature
8289                        gnus-treat-overstrike
8290                        gnus-treat-display-xface
8291                        gnus-treat-buttonize-head
8292                        gnus-treat-decode-article-as-default-mime-charset))
8293                     (static-if (featurep 'xemacs)
8294                         items
8295                       (cons '(x-face-mule-delete-x-face-field
8296                               (quote never))
8297                             items))))
8298                  (gnus-treat-display-xface
8299                   (when (, x-face) gnus-treat-display-xface)))
8300              (gnus-article-prepare-mime-display)))
8301          (goto-char (if (, backward) start end))
8302          (when (, x-face)
8303            (gnus-summary-search-article-highlight-goto-x-face (point)))
8304          (setq gnus-summary-search-article-matched-data
8305                (list start end (buffer-substring start end)))
8306          (unless (eq start end);; matched text has been deleted. :-<
8307            (put-text-property start end 'face
8308                               (or (find-face 'isearch)
8309                                   'secondary-selection))))))
8310   )
8311
8312 (defun gnus-summary-search-article (regexp &optional backward)
8313   "Search for an article containing REGEXP.
8314 Optional argument BACKWARD means do search for backward.
8315 `gnus-select-article-hook' is not called during the search."
8316   ;; We have to require this here to make sure that the following
8317   ;; dynamic binding isn't shadowed by autoloading.
8318   (require 'gnus-async)
8319   (require 'gnus-art)
8320   (let ((gnus-select-article-hook nil)  ;Disable hook.
8321         (gnus-article-prepare-hook nil)
8322         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8323         (gnus-use-article-prefetch nil)
8324         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8325         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8326         (gnus-visual nil)
8327         (gnus-keep-backlog nil)
8328         (gnus-break-pages nil)
8329         (gnus-summary-display-arrow nil)
8330         (gnus-updated-mode-lines nil)
8331         (gnus-auto-center-summary nil)
8332         (sum (current-buffer))
8333         (found nil)
8334         point treated)
8335     (gnus-save-hidden-threads
8336       (static-if (featurep 'xemacs)
8337           (let ((gnus-inhibit-treatment t))
8338             (setq treated (eq 'old (gnus-summary-select-article)))
8339             (when (and treated
8340                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8341                                  (window-live-p (get-buffer-window
8342                                                  gnus-article-buffer t)))))
8343               (gnus-summary-select-article nil t)
8344               (setq treated nil)))
8345         (let ((gnus-inhibit-treatment t)
8346               (x-face-mule-delete-x-face-field 'never))
8347           (setq treated (eq 'old (gnus-summary-select-article)))
8348           (when (and treated
8349                      (not
8350                       (and (gnus-buffer-live-p gnus-article-buffer)
8351                            (window-live-p (get-buffer-window
8352                                            gnus-article-buffer t))
8353                            (or (not (string-match "^\\^X-Face:" regexp))
8354                                (with-current-buffer gnus-article-buffer
8355                                  gnus-summary-search-article-matched-data)))))
8356             (gnus-summary-select-article nil t)
8357             (setq treated nil))))
8358       (set-buffer gnus-article-buffer)
8359       (widen)
8360       (if treated
8361           (progn
8362             (gnus-article-show-all-headers)
8363             (gnus-summary-search-article-position-point regexp backward))
8364         (goto-char (if backward (point-max) (point-min))))
8365       (while (not found)
8366         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8367         (if (if backward
8368                 (re-search-backward regexp nil t)
8369               (re-search-forward regexp nil t))
8370             ;; We found the regexp.
8371             (progn
8372               (gnus-summary-search-article-highlight-matched-text
8373                backward treated (string-match "^\\^X-Face:" regexp))
8374               (setq found 'found)
8375               (forward-line
8376                (/ (- 2 (window-height
8377                         (get-buffer-window gnus-article-buffer t)))
8378                   2))
8379               (set-window-start
8380                (get-buffer-window (current-buffer))
8381                (point))
8382               (set-buffer sum)
8383               (setq point (point)))
8384           ;; We didn't find it, so we go to the next article.
8385           (set-buffer sum)
8386           (setq found 'not)
8387           (while (eq found 'not)
8388             (if (not (if backward (gnus-summary-find-prev)
8389                        (gnus-summary-find-next)))
8390                 ;; No more articles.
8391                 (setq found t)
8392               ;; Select the next article and adjust point.
8393               (unless (gnus-summary-article-sparse-p
8394                        (gnus-summary-article-number))
8395                 (setq found nil)
8396                 (let ((gnus-inhibit-treatment t))
8397                   (gnus-summary-select-article))
8398                 (setq treated nil)
8399                 (set-buffer gnus-article-buffer)
8400                 (widen)
8401                 (goto-char (if backward (point-max) (point-min))))))))
8402       (gnus-message 7 ""))
8403     ;; Return whether we found the regexp.
8404     (when (eq found 'found)
8405       (goto-char point)
8406       (gnus-summary-show-thread)
8407       (gnus-summary-goto-subject gnus-current-article)
8408       (gnus-summary-position-point)
8409       t)))
8410
8411 (defun gnus-find-matching-articles (header regexp)
8412   "Return a list of all articles that match REGEXP on HEADER.
8413 This search includes all articles in the current group that Gnus has
8414 fetched headers for, whether they are displayed or not."
8415   (let ((articles nil)
8416         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8417         (case-fold-search t))
8418     (dolist (header gnus-newsgroup-headers)
8419       (when (string-match regexp (funcall func header))
8420         (push (mail-header-number header) articles)))
8421     (nreverse articles)))
8422
8423 (defun gnus-summary-find-matching (header regexp &optional backward unread
8424                                           not-case-fold not-matching)
8425   "Return a list of all articles that match REGEXP on HEADER.
8426 The search stars on the current article and goes forwards unless
8427 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8428 If UNREAD is non-nil, only unread articles will
8429 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8430 in the comparisons. If NOT-MATCHING, return a list of all articles that
8431 not match REGEXP on HEADER."
8432   (let ((case-fold-search (not not-case-fold))
8433         articles d func)
8434     (if (consp header)
8435         (if (eq (car header) 'extra)
8436             (setq func
8437                   `(lambda (h)
8438                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8439                          "")))
8440           (error "%s is an invalid header" header))
8441       (unless (fboundp (intern (concat "mail-header-" header)))
8442         (error "%s is not a valid header" header))
8443       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8444     (dolist (d (if (eq backward 'all)
8445                    gnus-newsgroup-data
8446                  (gnus-data-find-list
8447                   (gnus-summary-article-number)
8448                   (gnus-data-list backward))))
8449       (when (and (or (not unread)       ; We want all articles...
8450                      (gnus-data-unread-p d)) ; Or just unreads.
8451                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8452                  (if not-matching
8453                      (not (string-match
8454                            regexp
8455                            (funcall func (gnus-data-header d))))
8456                    (string-match regexp
8457                                  (funcall func (gnus-data-header d)))))
8458         (push (gnus-data-number d) articles))) ; Success!
8459     (nreverse articles)))
8460
8461 (defun gnus-summary-execute-command (header regexp command &optional backward)
8462   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8463 If HEADER is an empty string (or nil), the match is done on the entire
8464 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8465   (interactive
8466    (list (let ((completion-ignore-case t))
8467            (completing-read
8468             "Header name: "
8469             (mapcar (lambda (header) (list (format "%s" header)))
8470                     (append
8471                      '("Number" "Subject" "From" "Lines" "Date"
8472                        "Message-ID" "Xref" "References" "Body")
8473                      gnus-extra-headers))
8474             nil 'require-match))
8475          (read-string "Regexp: ")
8476          (read-key-sequence "Command: ")
8477          current-prefix-arg))
8478   (when (equal header "Body")
8479     (setq header ""))
8480   ;; Hidden thread subtrees must be searched as well.
8481   (gnus-summary-show-all-threads)
8482   ;; We don't want to change current point nor window configuration.
8483   (save-excursion
8484     (save-window-excursion
8485       (let (gnus-visual
8486             gnus-treat-strip-trailing-blank-lines
8487             gnus-treat-strip-leading-blank-lines
8488             gnus-treat-strip-multiple-blank-lines
8489             gnus-treat-hide-boring-headers
8490             gnus-treat-fold-newsgroups
8491             gnus-article-prepare-hook)
8492         (gnus-message 6 "Executing %s..." (key-description command))
8493         ;; We'd like to execute COMMAND interactively so as to give arguments.
8494         (gnus-execute header regexp
8495                       `(call-interactively ',(key-binding command))
8496                       backward)
8497         (gnus-message 6 "Executing %s...done" (key-description command))))))
8498
8499 (defun gnus-summary-beginning-of-article ()
8500   "Scroll the article back to the beginning."
8501   (interactive)
8502   (gnus-summary-select-article)
8503   (gnus-configure-windows 'article)
8504   (gnus-eval-in-buffer-window gnus-article-buffer
8505     (widen)
8506     (goto-char (point-min))
8507     (when gnus-page-broken
8508       (gnus-narrow-to-page))))
8509
8510 (defun gnus-summary-end-of-article ()
8511   "Scroll to the end of the article."
8512   (interactive)
8513   (gnus-summary-select-article)
8514   (gnus-configure-windows 'article)
8515   (gnus-eval-in-buffer-window gnus-article-buffer
8516     (widen)
8517     (goto-char (point-max))
8518     (recenter -3)
8519     (when gnus-page-broken
8520       (gnus-narrow-to-page))))
8521
8522 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8523   "Truncate to LEN and quote all \"(\"'s in STRING."
8524   (gnus-replace-in-string (if (and len (> (length string) len))
8525                               (substring string 0 len)
8526                             string)
8527                           "[()]" "\\\\\\&"))
8528
8529 (defun gnus-summary-print-article (&optional filename n)
8530   "Generate and print a PostScript image of the N next (mail) articles.
8531
8532 If N is negative, print the N previous articles.  If N is nil and articles
8533 have been marked with the process mark, print these instead.
8534
8535 If the optional first argument FILENAME is nil, send the image to the
8536 printer.  If FILENAME is a string, save the PostScript image in a file with
8537 that name.  If FILENAME is a number, prompt the user for the name of the file
8538 to save in."
8539   (interactive (list (ps-print-preprint current-prefix-arg)))
8540   (dolist (article (gnus-summary-work-articles n))
8541     (gnus-summary-select-article nil nil 'pseudo article)
8542     (gnus-eval-in-buffer-window gnus-article-buffer
8543       (gnus-print-buffer))
8544     (gnus-summary-remove-process-mark article))
8545   (ps-despool filename))
8546
8547 (defun gnus-print-buffer ()
8548   (let ((buffer (generate-new-buffer " *print*")))
8549     (unwind-protect
8550         (progn
8551           (copy-to-buffer buffer (point-min) (point-max))
8552           (set-buffer buffer)
8553           (gnus-article-delete-invisible-text)
8554           (gnus-remove-text-with-property 'gnus-decoration)
8555           (when (gnus-visual-p 'article-highlight 'highlight)
8556             ;; Copy-to-buffer doesn't copy overlay.  So redo
8557             ;; highlight.
8558             (let ((gnus-article-buffer buffer))
8559               (gnus-article-highlight-citation t)
8560               (gnus-article-highlight-signature)))
8561           (let ((ps-left-header
8562                  (list
8563                   (concat "("
8564                           (gnus-summary-print-truncate-and-quote
8565                            (mail-header-subject gnus-current-headers)
8566                            66) ")")
8567                   (concat "("
8568                           (gnus-summary-print-truncate-and-quote
8569                            (mail-header-from gnus-current-headers)
8570                            45) ")")))
8571                 (ps-right-header
8572                  (list
8573                   "/pagenumberstring load"
8574                   (concat "("
8575                           (mail-header-date gnus-current-headers) ")"))))
8576             (gnus-run-hooks 'gnus-ps-print-hook)
8577             (save-excursion
8578               (if window-system
8579                   (ps-spool-buffer-with-faces)
8580                 (ps-spool-buffer)))))
8581       (kill-buffer buffer))))
8582
8583 (defun gnus-summary-show-article (&optional arg)
8584   "Force redisplaying of the current article.
8585 If ARG (the prefix) is a number, show the article with the charset
8586 defined in `gnus-summary-show-article-charset-alist', or the charset
8587 input.
8588 If ARG (the prefix) is non-nil and not a number, show the raw article
8589 without any article massaging functions being run.  Normally, the key strokes
8590 are `C-u g'."
8591   (interactive "P")
8592   (cond
8593    ((numberp arg)
8594     (gnus-summary-show-article t)
8595     (let* ((gnus-newsgroup-charset
8596             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8597                 (mm-read-coding-system
8598                  "View as charset: " ;; actually it is coding system.
8599                  (save-excursion
8600                    (set-buffer gnus-article-buffer)
8601                    (mm-detect-coding-region (point) (point-max))))))
8602            (default-mime-charset gnus-newsgroup-charset)
8603            (gnus-newsgroup-ignored-charsets 'gnus-all))
8604       (gnus-summary-select-article nil 'force)
8605       (let ((deps gnus-newsgroup-dependencies)
8606             head header lines)
8607         (save-excursion
8608           (set-buffer gnus-original-article-buffer)
8609           (save-restriction
8610             (message-narrow-to-head)
8611             (setq head (buffer-string))
8612             (goto-char (point-min))
8613             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8614               (goto-char (point-max))
8615               (widen)
8616               (setq lines (1- (count-lines (point) (point-max))))))
8617           (with-temp-buffer
8618             (insert (format "211 %d Article retrieved.\n"
8619                             (cdr gnus-article-current)))
8620             (insert head)
8621             (if lines (insert (format "Lines: %d\n" lines)))
8622             (insert ".\n")
8623             (let ((nntp-server-buffer (current-buffer)))
8624               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8625         (gnus-data-set-header
8626          (gnus-data-find (cdr gnus-article-current))
8627          header)
8628         (gnus-summary-update-article-line
8629          (cdr gnus-article-current) header)
8630         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8631           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8632    ((not arg)
8633     ;; Select the article the normal way.
8634     (gnus-summary-select-article nil 'force))
8635    (t
8636     ;; We have to require this here to make sure that the following
8637     ;; dynamic binding isn't shadowed by autoloading.
8638     (require 'gnus-async)
8639     (require 'gnus-art)
8640     ;; Bind the article treatment functions to nil.
8641     (let ((gnus-have-all-headers t)
8642           gnus-article-prepare-hook
8643           gnus-article-decode-hook
8644           gnus-break-pages
8645           gnus-show-mime
8646           (gnus-inhibit-treatment t))
8647       (gnus-summary-select-article nil 'force))))
8648   (gnus-summary-goto-subject gnus-current-article)
8649   (gnus-summary-position-point))
8650
8651 (defun gnus-summary-show-raw-article ()
8652   "Show the raw article without any article massaging functions being run."
8653   (interactive)
8654   (gnus-summary-show-article t))
8655
8656 (defun gnus-summary-verbose-headers (&optional arg)
8657   "Toggle permanent full header display.
8658 If ARG is a positive number, turn header display on.
8659 If ARG is a negative number, turn header display off."
8660   (interactive "P")
8661   (setq gnus-show-all-headers
8662         (cond ((or (not (numberp arg))
8663                    (zerop arg))
8664                (not gnus-show-all-headers))
8665               ((natnump arg)
8666                t)))
8667   (gnus-summary-show-article))
8668
8669 (defun gnus-summary-toggle-header (&optional arg)
8670   "Show the headers if they are hidden, or hide them if they are shown.
8671 If ARG is a positive number, show the entire header.
8672 If ARG is a negative number, hide the unwanted header lines."
8673   (interactive "P")
8674   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8675                      (get-buffer-window gnus-article-buffer t))))
8676     (with-current-buffer gnus-article-buffer
8677       (widen)
8678       (article-narrow-to-head)
8679       (let* ((buffer-read-only nil)
8680              (inhibit-point-motion-hooks t)
8681              (hidden (if (numberp arg)
8682                          (>= arg 0)
8683                        (gnus-article-hidden-text-p 'headers)))
8684              s e)
8685         (delete-region (point-min) (point-max))
8686         (with-current-buffer gnus-original-article-buffer
8687           (goto-char (setq s (point-min)))
8688           (setq e (if (search-forward "\n\n" nil t)
8689                       (1- (point))
8690                     (point-max))))
8691         (insert-buffer-substring gnus-original-article-buffer s e)
8692         (article-decode-encoded-words)
8693         (if hidden
8694             (let ((gnus-treat-hide-headers nil)
8695                   (gnus-treat-hide-boring-headers nil))
8696               (gnus-delete-wash-type 'headers)
8697               (gnus-treat-article 'head))
8698           (gnus-treat-article 'head))
8699         (widen)
8700         (if window
8701             (set-window-start window (goto-char (point-min))))
8702         (setq gnus-page-broken
8703               (when gnus-break-pages
8704                 (gnus-narrow-to-page)
8705                 t))
8706         (gnus-set-mode-line 'article)))))
8707
8708 (defun gnus-summary-show-all-headers ()
8709   "Make all header lines visible."
8710   (interactive)
8711   (gnus-summary-toggle-header 1))
8712
8713 (defun gnus-summary-toggle-mime (&optional arg)
8714   "Toggle MIME processing.
8715 If ARG is a positive number, turn MIME processing on."
8716   (interactive "P")
8717   (setq gnus-show-mime
8718         (if (null arg)
8719             (not gnus-show-mime)
8720           (> (prefix-numeric-value arg) 0)))
8721   (gnus-summary-select-article t 'force))
8722
8723 (defun gnus-summary-caesar-message (&optional arg)
8724   "Caesar rotate the current article by 13.
8725 The numerical prefix specifies how many places to rotate each letter
8726 forward."
8727   (interactive "P")
8728   (gnus-summary-select-article)
8729   (let ((mail-header-separator ""))
8730     (gnus-eval-in-buffer-window gnus-article-buffer
8731       (save-restriction
8732         (widen)
8733         (let ((start (window-start))
8734               buffer-read-only)
8735           (message-caesar-buffer-body arg)
8736           (set-window-start (get-buffer-window (current-buffer)) start))))))
8737
8738 (autoload 'unmorse-region "morse"
8739   "Convert morse coded text in region to ordinary ASCII text."
8740   t)
8741
8742 (defun gnus-summary-morse-message (&optional arg)
8743   "Morse decode the current article."
8744   (interactive "P")
8745   (gnus-summary-select-article)
8746   (let ((mail-header-separator ""))
8747     (gnus-eval-in-buffer-window gnus-article-buffer
8748       (save-excursion
8749         (save-restriction
8750           (widen)
8751           (let ((pos (window-start))
8752                 buffer-read-only)
8753             (goto-char (point-min))
8754             (when (message-goto-body)
8755               (gnus-narrow-to-body))
8756             (goto-char (point-min))
8757             (while (re-search-forward "·" (point-max) t)
8758               (replace-match "."))
8759             (unmorse-region (point-min) (point-max))
8760             (widen)
8761             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8762
8763 (defun gnus-summary-stop-page-breaking ()
8764   "Stop page breaking in the current article."
8765   (interactive)
8766   (gnus-summary-select-article)
8767   (gnus-eval-in-buffer-window gnus-article-buffer
8768     (widen)
8769     (when (gnus-visual-p 'page-marker)
8770       (let ((buffer-read-only nil))
8771         (gnus-remove-text-with-property 'gnus-prev)
8772         (gnus-remove-text-with-property 'gnus-next))
8773       (setq gnus-page-broken nil))))
8774
8775 (defun gnus-summary-move-article (&optional n to-newsgroup
8776                                             select-method action)
8777   "Move the current article to a different newsgroup.
8778 If N is a positive number, move the N next articles.
8779 If N is a negative number, move the N previous articles.
8780 If N is nil and any articles have been marked with the process mark,
8781 move those articles instead.
8782 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8783 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8784 re-spool using this method.
8785
8786 When called interactively with TO-NEWSGROUP being nil, the value of
8787 the variable `gnus-move-split-methods' is used for finding a default
8788 for the target newsgroup.
8789
8790 For this function to work, both the current newsgroup and the
8791 newsgroup that you want to move to have to support the `request-move'
8792 and `request-accept' functions.
8793
8794 ACTION can be either `move' (the default), `crosspost' or `copy'."
8795   (interactive "P")
8796   (unless action
8797     (setq action 'move))
8798   ;; Check whether the source group supports the required functions.
8799   (cond ((and (eq action 'move)
8800               (not (gnus-check-backend-function
8801                     'request-move-article gnus-newsgroup-name)))
8802          (error "The current group does not support article moving"))
8803         ((and (eq action 'crosspost)
8804               (not (gnus-check-backend-function
8805                     'request-replace-article gnus-newsgroup-name)))
8806          (error "The current group does not support article editing")))
8807   (let ((articles (gnus-summary-work-articles n))
8808         (prefix (if (gnus-check-backend-function
8809                      'request-move-article gnus-newsgroup-name)
8810                     (gnus-group-real-prefix gnus-newsgroup-name)
8811                   ""))
8812         (names '((move "Move" "Moving")
8813                  (copy "Copy" "Copying")
8814                  (crosspost "Crosspost" "Crossposting")))
8815         (copy-buf (save-excursion
8816                     (nnheader-set-temp-buffer " *copy article*")))
8817         (default-marks gnus-article-mark-lists)
8818         (no-expire-marks (delete '(expirable . expire)
8819                                  (copy-sequence gnus-article-mark-lists)))
8820         art-group to-method new-xref article to-groups)
8821     (unless (assq action names)
8822       (error "Unknown action %s" action))
8823     ;; Read the newsgroup name.
8824     (when (and (not to-newsgroup)
8825                (not select-method))
8826       (if (and gnus-move-split-methods
8827                (not
8828                 (and (memq gnus-current-article articles)
8829                      (gnus-buffer-live-p gnus-original-article-buffer))))
8830           ;; When `gnus-move-split-methods' is non-nil, we have to
8831           ;; select an article to give `gnus-read-move-group-name' an
8832           ;; opportunity to suggest an appropriate default.  However,
8833           ;; we needn't render or mark the article.
8834           (let ((gnus-display-mime-function nil)
8835                 (gnus-article-prepare-hook nil)
8836                 (gnus-mark-article-hook nil))
8837             (gnus-summary-select-article nil nil nil (car articles))))
8838       (setq to-newsgroup
8839             (gnus-read-move-group-name
8840              (cadr (assq action names))
8841              (symbol-value (intern (format "gnus-current-%s-group" action)))
8842              articles prefix))
8843       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8844     (setq to-method (or select-method
8845                         (gnus-server-to-method
8846                          (gnus-group-method to-newsgroup))))
8847     ;; Check the method we are to move this article to...
8848     (unless (gnus-check-backend-function
8849              'request-accept-article (car to-method))
8850       (error "%s does not support article copying" (car to-method)))
8851     (unless (gnus-check-server to-method)
8852       (error "Can't open server %s" (car to-method)))
8853     (gnus-message 6 "%s to %s: %s..."
8854                   (caddr (assq action names))
8855                   (or (car select-method) to-newsgroup) articles)
8856     (while articles
8857       (setq article (pop articles))
8858       (setq
8859        art-group
8860        (cond
8861         ;; Move the article.
8862         ((eq action 'move)
8863          ;; Remove this article from future suppression.
8864          (gnus-dup-unsuppress-article article)
8865          (gnus-request-move-article
8866           article                       ; Article to move
8867           gnus-newsgroup-name           ; From newsgroup
8868           (nth 1 (gnus-find-method-for-group
8869                   gnus-newsgroup-name)) ; Server
8870           (list 'gnus-request-accept-article
8871                 to-newsgroup (list 'quote select-method)
8872                 (not articles) t)       ; Accept form
8873           (not articles)))              ; Only save nov last time
8874         ;; Copy the article.
8875         ((eq action 'copy)
8876          (save-excursion
8877            (set-buffer copy-buf)
8878            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8879              (gnus-request-accept-article
8880               to-newsgroup select-method (not articles) t))))
8881         ;; Crosspost the article.
8882         ((eq action 'crosspost)
8883          (let ((xref (message-tokenize-header
8884                       (mail-header-xref (gnus-summary-article-header article))
8885                       " ")))
8886            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8887                                   ":" (number-to-string article)))
8888            (unless xref
8889              (setq xref (list (system-name))))
8890            (setq new-xref
8891                  (concat
8892                   (mapconcat 'identity
8893                              (delete "Xref:" (delete new-xref xref))
8894                              " ")
8895                   " " new-xref))
8896            (save-excursion
8897              (set-buffer copy-buf)
8898              ;; First put the article in the destination group.
8899              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8900              (when (consp (setq art-group
8901                                 (gnus-request-accept-article
8902                                  to-newsgroup select-method (not articles))))
8903                (setq new-xref (concat new-xref " " (car art-group)
8904                                       ":"
8905                                       (number-to-string (cdr art-group))))
8906                ;; Now we have the new Xrefs header, so we insert
8907                ;; it and replace the new article.
8908                (nnheader-replace-header "Xref" new-xref)
8909                (gnus-request-replace-article
8910                 (cdr art-group) to-newsgroup (current-buffer))
8911                art-group))))))
8912       (cond
8913        ((not art-group)
8914         (gnus-message 1 "Couldn't %s article %s: %s"
8915                       (cadr (assq action names)) article
8916                       (nnheader-get-report (car to-method))))
8917        ((eq art-group 'junk)
8918         (when (eq action 'move)
8919           (let ((id (mail-header-id (gnus-data-header 
8920                                      (assoc article (gnus-data-list nil))))))
8921             (gnus-summary-mark-article article gnus-canceled-mark)
8922             (gnus-message 4 "Deleted article %s" article)
8923             ;; run the move/copy/crosspost/respool hook
8924             (run-hook-with-args 'gnus-summary-article-delete-hook 
8925                                 action id gnus-newsgroup-name nil
8926                                 select-method))))
8927        (t
8928         (let* ((pto-group (gnus-group-prefixed-name
8929                            (car art-group) to-method))
8930                (entry
8931                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8932                (info (nth 2 entry))
8933                (to-group (gnus-info-group info))
8934                to-marks)
8935           ;; Update the group that has been moved to.
8936           (when (and info
8937                      (memq action '(move copy)))
8938             (unless (member to-group to-groups)
8939               (push to-group to-groups))
8940
8941             (unless (memq article gnus-newsgroup-unreads)
8942               (push 'read to-marks)
8943               (gnus-info-set-read
8944                info (gnus-add-to-range (gnus-info-read info)
8945                                        (list (cdr art-group)))))
8946
8947             ;; See whether the article is to be put in the cache.
8948             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8949                              default-marks
8950                            no-expire-marks))
8951                   (to-article (cdr art-group)))
8952
8953               ;; Enter the article into the cache in the new group,
8954               ;; if that is required.
8955               (when gnus-use-cache
8956                 (gnus-cache-possibly-enter-article
8957                  to-group to-article
8958                  (let ((header (copy-sequence
8959                                 (gnus-summary-article-header article))))
8960                    (mail-header-set-number header to-article)
8961                    header)
8962                  (memq article gnus-newsgroup-marked)
8963                  (memq article gnus-newsgroup-dormant)
8964                  (memq article gnus-newsgroup-unreads)))
8965
8966               (when gnus-preserve-marks
8967                 ;; Copy any marks over to the new group.
8968                 (when (and (equal to-group gnus-newsgroup-name)
8969                            (not (memq article gnus-newsgroup-unreads)))
8970                   ;; Mark this article as read in this group.
8971                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8972                   (setcdr (gnus-active to-group) to-article)
8973                   (setcdr gnus-newsgroup-active to-article))
8974
8975                 (while marks
8976                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8977                     (when (memq article (symbol-value
8978                                          (intern (format "gnus-newsgroup-%s"
8979                                                          (caar marks)))))
8980                       (push (cdar marks) to-marks)
8981                       ;; If the other group is the same as this group,
8982                       ;; then we have to add the mark to the list.
8983                       (when (equal to-group gnus-newsgroup-name)
8984                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8985                              (cons to-article
8986                                    (symbol-value
8987                                     (intern (format "gnus-newsgroup-%s"
8988                                                     (caar marks)))))))
8989                       ;; Copy the marks to other group.
8990                       (gnus-add-marked-articles
8991                        to-group (cdar marks) (list to-article) info)))
8992                   (setq marks (cdr marks)))
8993
8994                 (gnus-request-set-mark
8995                  to-group (list (list (list to-article) 'add to-marks))))
8996
8997               (gnus-dribble-enter
8998                (concat "(gnus-group-set-info '"
8999                        (gnus-prin1-to-string (gnus-get-info to-group))
9000                        ")"))))
9001
9002           ;; Update the Xref header in this article to point to
9003           ;; the new crossposted article we have just created.
9004           (when (eq action 'crosspost)
9005             (save-excursion
9006               (set-buffer copy-buf)
9007               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9008               (nnheader-replace-header "Xref" new-xref)
9009               (gnus-request-replace-article
9010                article gnus-newsgroup-name (current-buffer))))
9011
9012           ;; run the move/copy/crosspost/respool hook
9013           (let ((id (mail-header-id (gnus-data-header 
9014                                    (assoc article (gnus-data-list nil))))))
9015           (run-hook-with-args 'gnus-summary-article-move-hook 
9016                               action id gnus-newsgroup-name to-newsgroup
9017                               select-method)))
9018
9019         ;;;!!!Why is this necessary?
9020         (set-buffer gnus-summary-buffer)
9021
9022         (gnus-summary-goto-subject article)
9023         (when (eq action 'move)
9024           (gnus-summary-mark-article article gnus-canceled-mark))))
9025       (gnus-summary-remove-process-mark article))
9026     ;; Re-activate all groups that have been moved to.
9027     (save-excursion
9028       (set-buffer gnus-group-buffer)
9029       (let ((gnus-group-marked to-groups))
9030         (gnus-group-get-new-news-this-group nil t)))
9031
9032     (gnus-kill-buffer copy-buf)
9033     (gnus-summary-position-point)
9034     (gnus-set-mode-line 'summary)))
9035
9036 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9037   "Move the current article to a different newsgroup.
9038 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9039 When called interactively, if TO-NEWSGROUP is nil, use the value of
9040 the variable `gnus-move-split-methods' for finding a default target
9041 newsgroup.
9042 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9043 re-spool using this method."
9044   (interactive "P")
9045   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9046
9047 (defun gnus-summary-crosspost-article (&optional n)
9048   "Crosspost the current article to some other group."
9049   (interactive "P")
9050   (gnus-summary-move-article n nil nil 'crosspost))
9051
9052 (defcustom gnus-summary-respool-default-method nil
9053   "Default method type for respooling an article.
9054 If nil, use to the current newsgroup method."
9055   :type 'symbol
9056   :group 'gnus-summary-mail)
9057
9058 (defcustom gnus-summary-display-while-building nil
9059   "If not-nil, show and update the summary buffer as it's being built.
9060 If the value is t, update the buffer after every line is inserted.  If
9061 the value is an integer (N), update the display every N lines."
9062   :group 'gnus-thread
9063   :type '(choice (const :tag "off" nil)
9064                  number
9065                  (const :tag "frequently" t)))
9066
9067 (defun gnus-summary-respool-article (&optional n method)
9068   "Respool the current article.
9069 The article will be squeezed through the mail spooling process again,
9070 which means that it will be put in some mail newsgroup or other
9071 depending on `nnmail-split-methods'.
9072 If N is a positive number, respool the N next articles.
9073 If N is a negative number, respool the N previous articles.
9074 If N is nil and any articles have been marked with the process mark,
9075 respool those articles instead.
9076
9077 Respooling can be done both from mail groups and \"real\" newsgroups.
9078 In the former case, the articles in question will be moved from the
9079 current group into whatever groups they are destined to.  In the
9080 latter case, they will be copied into the relevant groups."
9081   (interactive
9082    (list current-prefix-arg
9083          (let* ((methods (gnus-methods-using 'respool))
9084                 (methname
9085                  (symbol-name (or gnus-summary-respool-default-method
9086                                   (car (gnus-find-method-for-group
9087                                         gnus-newsgroup-name)))))
9088                 (method
9089                  (gnus-completing-read-with-default
9090                   methname "What backend do you want to use when respooling?"
9091                   methods nil t nil 'gnus-mail-method-history))
9092                 ms)
9093            (cond
9094             ((zerop (length (setq ms (gnus-servers-using-backend
9095                                       (intern method)))))
9096              (list (intern method) ""))
9097             ((= 1 (length ms))
9098              (car ms))
9099             (t
9100              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9101                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9102                            ms-alist))))))))
9103   (unless method
9104     (error "No method given for respooling"))
9105   (if (assoc (symbol-name
9106               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9107              (gnus-methods-using 'respool))
9108       (gnus-summary-move-article n nil method)
9109     (gnus-summary-copy-article n nil method)))
9110
9111 (defun gnus-summary-import-article (file &optional edit)
9112   "Import an arbitrary file into a mail newsgroup."
9113   (interactive "fImport file: \nP")
9114   (let ((group gnus-newsgroup-name)
9115         (now (current-time))
9116         atts lines group-art)
9117     (unless (gnus-check-backend-function 'request-accept-article group)
9118       (error "%s does not support article importing" group))
9119     (or (file-readable-p file)
9120         (not (file-regular-p file))
9121         (error "Can't read %s" file))
9122     (save-excursion
9123       (set-buffer (gnus-get-buffer-create " *import file*"))
9124       (erase-buffer)
9125       (nnheader-insert-file-contents file)
9126       (goto-char (point-min))
9127       (if (nnheader-article-p)
9128           (save-restriction
9129             (goto-char (point-min))
9130             (search-forward "\n\n" nil t)
9131             (narrow-to-region (point-min) (1- (point)))
9132             (goto-char (point-min))
9133             (unless (re-search-forward "^date:" nil t)
9134               (goto-char (point-max))
9135               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9136         ;; This doesn't look like an article, so we fudge some headers.
9137         (setq atts (file-attributes file)
9138               lines (count-lines (point-min) (point-max)))
9139         (insert "From: " (read-string "From: ") "\n"
9140                 "Subject: " (read-string "Subject: ") "\n"
9141                 "Date: " (message-make-date (nth 5 atts)) "\n"
9142                 "Message-ID: " (message-make-message-id) "\n"
9143                 "Lines: " (int-to-string lines) "\n"
9144                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9145       (setq group-art (gnus-request-accept-article group nil t))
9146       (kill-buffer (current-buffer)))
9147     (setq gnus-newsgroup-active (gnus-activate-group group))
9148     (forward-line 1)
9149     (gnus-summary-goto-article (cdr group-art) nil t)
9150     (when edit
9151       (gnus-summary-edit-article))))
9152
9153 (defun gnus-summary-create-article ()
9154   "Create an article in a mail newsgroup."
9155   (interactive)
9156   (let ((group gnus-newsgroup-name)
9157         (now (current-time))
9158         group-art)
9159     (unless (gnus-check-backend-function 'request-accept-article group)
9160       (error "%s does not support article importing" group))
9161     (save-excursion
9162       (set-buffer (gnus-get-buffer-create " *import file*"))
9163       (erase-buffer)
9164       (goto-char (point-min))
9165       ;; This doesn't look like an article, so we fudge some headers.
9166       (insert "From: " (read-string "From: ") "\n"
9167               "Subject: " (read-string "Subject: ") "\n"
9168               "Date: " (message-make-date now) "\n"
9169               "Message-ID: " (message-make-message-id) "\n")
9170       (setq group-art (gnus-request-accept-article group nil t))
9171       (kill-buffer (current-buffer)))
9172     (setq gnus-newsgroup-active (gnus-activate-group group))
9173     (forward-line 1)
9174     (gnus-summary-goto-article (cdr group-art) nil t)
9175     (gnus-summary-edit-article)))
9176
9177 (defun gnus-summary-article-posted-p ()
9178   "Say whether the current (mail) article is available from news as well.
9179 This will be the case if the article has both been mailed and posted."
9180   (interactive)
9181   (let ((id (mail-header-references (gnus-summary-article-header)))
9182         (gnus-override-method (car (gnus-refer-article-methods))))
9183     (if (gnus-request-head id "")
9184         (gnus-message 2 "The current message was found on %s"
9185                       gnus-override-method)
9186       (gnus-message 2 "The current message couldn't be found on %s"
9187                     gnus-override-method)
9188       nil)))
9189
9190 (defun gnus-summary-expire-articles (&optional now)
9191   "Expire all articles that are marked as expirable in the current group."
9192   (interactive)
9193   (when (gnus-check-backend-function
9194          'request-expire-articles gnus-newsgroup-name)
9195     ;; This backend supports expiry.
9196     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9197            (expirable (if total
9198                           (progn
9199                             ;; We need to update the info for
9200                             ;; this group for `gnus-list-of-read-articles'
9201                             ;; to give us the right answer.
9202                             (gnus-run-hooks 'gnus-exit-group-hook)
9203                             (gnus-summary-update-info)
9204                             (gnus-list-of-read-articles gnus-newsgroup-name))
9205                         (setq gnus-newsgroup-expirable
9206                               (sort gnus-newsgroup-expirable '<))))
9207            (expiry-wait (if now 'immediate
9208                           (gnus-group-find-parameter
9209                            gnus-newsgroup-name 'expiry-wait)))
9210            (nnmail-expiry-target
9211             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9212                 nnmail-expiry-target))
9213            es)
9214       (when expirable
9215         ;; There are expirable articles in this group, so we run them
9216         ;; through the expiry process.
9217         (gnus-message 6 "Expiring articles...")
9218         (unless (gnus-check-group gnus-newsgroup-name)
9219           (error "Can't open server for %s" gnus-newsgroup-name))
9220         ;; The list of articles that weren't expired is returned.
9221         (save-excursion
9222           (if expiry-wait
9223               (let ((nnmail-expiry-wait-function nil)
9224                     (nnmail-expiry-wait expiry-wait))
9225                 (setq es (gnus-request-expire-articles
9226                           expirable gnus-newsgroup-name)))
9227             (setq es (gnus-request-expire-articles
9228                       expirable gnus-newsgroup-name)))
9229           (unless total
9230             (setq gnus-newsgroup-expirable es))
9231           ;; We go through the old list of expirable, and mark all
9232           ;; really expired articles as nonexistent.
9233           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9234             (let ((gnus-use-cache nil))
9235               (dolist (article expirable)
9236                 (when (and (not (memq article es))
9237                            (gnus-data-find article))
9238                   (gnus-summary-mark-article article gnus-canceled-mark)
9239                   (let ((id (mail-header-id (gnus-data-header 
9240                                              (assoc article 
9241                                                     (gnus-data-list nil))))))
9242                     (run-hook-with-args 'gnus-summary-article-expire-hook
9243                                         'delete id gnus-newsgroup-name nil
9244                                         nil)))))))
9245         (gnus-message 6 "Expiring articles...done")))))
9246
9247 (defun gnus-summary-expire-articles-now ()
9248   "Expunge all expirable articles in the current group.
9249 This means that *all* articles that are marked as expirable will be
9250 deleted forever, right now."
9251   (interactive)
9252   (or gnus-expert-user
9253       (gnus-yes-or-no-p
9254        "Are you really, really, really sure you want to delete all these messages? ")
9255       (error "Phew!"))
9256   (gnus-summary-expire-articles t))
9257
9258 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9259 (defun gnus-summary-delete-article (&optional n)
9260   "Delete the N next (mail) articles.
9261 This command actually deletes articles.  This is not a marking
9262 command.  The article will disappear forever from your life, never to
9263 return.
9264 If N is negative, delete backwards.
9265 If N is nil and articles have been marked with the process mark,
9266 delete these instead."
9267   (interactive "P")
9268   (unless (gnus-check-backend-function 'request-expire-articles
9269                                        gnus-newsgroup-name)
9270     (error "The current newsgroup does not support article deletion"))
9271   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9272     (error "Couldn't open server"))
9273   ;; Compute the list of articles to delete.
9274   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9275         (nnmail-expiry-target 'delete)
9276         not-deleted)
9277     (if (and gnus-novice-user
9278              (not (gnus-yes-or-no-p
9279                    (format "Do you really want to delete %s forever? "
9280                            (if (> (length articles) 1)
9281                                (format "these %s articles" (length articles))
9282                              "this article")))))
9283         ()
9284       ;; Delete the articles.
9285       (setq not-deleted (gnus-request-expire-articles
9286                          articles gnus-newsgroup-name 'force))
9287       (while articles
9288         (gnus-summary-remove-process-mark (car articles))
9289         ;; The backend might not have been able to delete the article
9290         ;; after all.
9291         (unless (memq (car articles) not-deleted)
9292           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9293         (let* ((article (car articles))
9294                (id (mail-header-id (gnus-data-header 
9295                                     (assoc article (gnus-data-list nil))))))
9296           (run-hook-with-args 'gnus-summary-article-delete-hook
9297                               'delete id gnus-newsgroup-name nil
9298                               nil))
9299         (setq articles (cdr articles)))
9300       (when not-deleted
9301         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9302     (gnus-summary-position-point)
9303     (gnus-set-mode-line 'summary)
9304     not-deleted))
9305
9306 (defun gnus-summary-edit-article (&optional force)
9307   "Edit the current article.
9308 This will have permanent effect only in mail groups.
9309 If FORCE is non-nil, allow editing of articles even in read-only
9310 groups."
9311   (interactive "P")
9312   (save-excursion
9313     (set-buffer gnus-summary-buffer)
9314     (let ((mail-parse-charset gnus-newsgroup-charset)
9315           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9316       (gnus-set-global-variables)
9317       (when (and (not force)
9318                  (gnus-group-read-only-p))
9319         (error "The current newsgroup does not support article editing"))
9320       (gnus-summary-show-article t)
9321       (gnus-article-edit-article
9322        'ignore
9323        `(lambda (no-highlight)
9324           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9325                 (message-options message-options)
9326                 (message-options-set-recipient)
9327                 (mail-parse-ignored-charsets
9328                  ',gnus-newsgroup-ignored-charsets))
9329             (gnus-summary-edit-article-done
9330              ,(or (mail-header-references gnus-current-headers) "")
9331              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9332
9333 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9334
9335 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9336                                                  no-highlight)
9337   "Make edits to the current article permanent."
9338   (interactive)
9339   (save-excursion
9340     ;; The buffer restriction contains the entire article if it exists.
9341     (when (article-goto-body)
9342       (let ((lines (count-lines (point) (point-max)))
9343             (length (- (point-max) (point)))
9344             (case-fold-search t)
9345             (body (copy-marker (point))))
9346         (goto-char (point-min))
9347         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9348           (delete-region (match-beginning 1) (match-end 1))
9349           (insert (number-to-string length)))
9350         (goto-char (point-min))
9351         (when (re-search-forward
9352                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9353           (delete-region (match-beginning 1) (match-end 1))
9354           (insert (number-to-string length)))
9355         (goto-char (point-min))
9356         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9357           (delete-region (match-beginning 1) (match-end 1))
9358           (insert (number-to-string lines))))))
9359   ;; Replace the article.
9360   (let ((buf (current-buffer)))
9361     (with-temp-buffer
9362       (insert-buffer-substring buf)
9363
9364       (if (and (not read-only)
9365                (not (gnus-request-replace-article
9366                      (cdr gnus-article-current) (car gnus-article-current)
9367                      (current-buffer) t)))
9368           (error "Couldn't replace article")
9369         ;; Update the summary buffer.
9370         (if (and references
9371                  (equal (message-tokenize-header references " ")
9372                         (message-tokenize-header
9373                          (or (message-fetch-field "references") "") " ")))
9374             ;; We only have to update this line.
9375             (save-excursion
9376               (save-restriction
9377                 (message-narrow-to-head)
9378                 (let ((head (buffer-string))
9379                       header)
9380                   (with-temp-buffer
9381                     (insert (format "211 %d Article retrieved.\n"
9382                                     (cdr gnus-article-current)))
9383                     (insert head)
9384                     (insert ".\n")
9385                     (let ((nntp-server-buffer (current-buffer)))
9386                       (setq header (car (gnus-get-newsgroup-headers
9387                                          nil t))))
9388                     (save-excursion
9389                       (set-buffer gnus-summary-buffer)
9390                       (gnus-data-set-header
9391                        (gnus-data-find (cdr gnus-article-current))
9392                        header)
9393                       (gnus-summary-update-article-line
9394                        (cdr gnus-article-current) header)
9395                       (if (gnus-summary-goto-subject
9396                            (cdr gnus-article-current) nil t)
9397                           (gnus-summary-update-secondary-mark
9398                            (cdr gnus-article-current))))))))
9399           ;; Update threads.
9400           (set-buffer (or buffer gnus-summary-buffer))
9401           (gnus-summary-update-article (cdr gnus-article-current))
9402           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9403               (gnus-summary-update-secondary-mark
9404                (cdr gnus-article-current))))
9405         ;; Prettify the article buffer again.
9406         (unless no-highlight
9407           (save-excursion
9408             (set-buffer gnus-article-buffer)
9409             ;;;!!! Fix this -- article should be rehighlighted.
9410             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9411             (set-buffer gnus-original-article-buffer)
9412             (gnus-request-article
9413              (cdr gnus-article-current)
9414              (car gnus-article-current) (current-buffer))))
9415         ;; Prettify the summary buffer line.
9416         (when (gnus-visual-p 'summary-highlight 'highlight)
9417           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9418
9419 (defun gnus-summary-edit-wash (key)
9420   "Perform editing command KEY in the article buffer."
9421   (interactive
9422    (list
9423     (progn
9424       (message "%s" (concat (this-command-keys) "- "))
9425       (read-char))))
9426   (message "")
9427   (gnus-summary-edit-article)
9428   (execute-kbd-macro (concat (this-command-keys) key))
9429   (gnus-article-edit-done))
9430
9431 ;;; Respooling
9432
9433 (defun gnus-summary-respool-query (&optional silent trace)
9434   "Query where the respool algorithm would put this article."
9435   (interactive)
9436   (let (gnus-mark-article-hook)
9437     (gnus-summary-select-article)
9438     (save-excursion
9439       (set-buffer gnus-original-article-buffer)
9440       (save-restriction
9441         (message-narrow-to-head)
9442         (let ((groups (nnmail-article-group 'identity trace)))
9443           (unless silent
9444             (if groups
9445                 (message "This message would go to %s"
9446                          (mapconcat 'car groups ", "))
9447               (message "This message would go to no groups"))
9448             groups))))))
9449
9450 (defun gnus-summary-respool-trace ()
9451   "Trace where the respool algorithm would put this article.
9452 Display a buffer showing all fancy splitting patterns which matched."
9453   (interactive)
9454   (gnus-summary-respool-query nil t))
9455
9456 ;; Summary marking commands.
9457
9458 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9459   "Mark articles which has the same subject as read, and then select the next.
9460 If UNMARK is positive, remove any kind of mark.
9461 If UNMARK is negative, tick articles."
9462   (interactive "P")
9463   (when unmark
9464     (setq unmark (prefix-numeric-value unmark)))
9465   (let ((count
9466          (gnus-summary-mark-same-subject
9467           (gnus-summary-article-subject) unmark)))
9468     ;; Select next unread article.  If auto-select-same mode, should
9469     ;; select the first unread article.
9470     (gnus-summary-next-article t (and gnus-auto-select-same
9471                                       (gnus-summary-article-subject)))
9472     (gnus-message 7 "%d article%s marked as %s"
9473                   count (if (= count 1) " is" "s are")
9474                   (if unmark "unread" "read"))))
9475
9476 (defun gnus-summary-kill-same-subject (&optional unmark)
9477   "Mark articles which has the same subject as read.
9478 If UNMARK is positive, remove any kind of mark.
9479 If UNMARK is negative, tick articles."
9480   (interactive "P")
9481   (when unmark
9482     (setq unmark (prefix-numeric-value unmark)))
9483   (let ((count
9484          (gnus-summary-mark-same-subject
9485           (gnus-summary-article-subject) unmark)))
9486     ;; If marked as read, go to next unread subject.
9487     (when (null unmark)
9488       ;; Go to next unread subject.
9489       (gnus-summary-next-subject 1 t))
9490     (gnus-message 7 "%d articles are marked as %s"
9491                   count (if unmark "unread" "read"))))
9492
9493 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9494   "Mark articles with same SUBJECT as read, and return marked number.
9495 If optional argument UNMARK is positive, remove any kinds of marks.
9496 If optional argument UNMARK is negative, mark articles as unread instead."
9497   (let ((count 1))
9498     (save-excursion
9499       (cond
9500        ((null unmark)                   ; Mark as read.
9501         (while (and
9502                 (progn
9503                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9504                   (gnus-summary-show-thread) t)
9505                 (gnus-summary-find-subject subject))
9506           (setq count (1+ count))))
9507        ((> unmark 0)                    ; Tick.
9508         (while (and
9509                 (progn
9510                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9511                   (gnus-summary-show-thread) t)
9512                 (gnus-summary-find-subject subject))
9513           (setq count (1+ count))))
9514        (t                               ; Mark as unread.
9515         (while (and
9516                 (progn
9517                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9518                   (gnus-summary-show-thread) t)
9519                 (gnus-summary-find-subject subject))
9520           (setq count (1+ count)))))
9521       (gnus-set-mode-line 'summary)
9522       ;; Return the number of marked articles.
9523       count)))
9524
9525 (defun gnus-summary-mark-as-processable (n &optional unmark)
9526   "Set the process mark on the next N articles.
9527 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9528 the process mark instead.  The difference between N and the actual
9529 number of articles marked is returned."
9530   (interactive "P")
9531   (if (and (null n) (gnus-region-active-p))
9532       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9533     (setq n (prefix-numeric-value n))
9534     (let ((backward (< n 0))
9535           (n (abs n)))
9536       (while (and
9537               (> n 0)
9538               (if unmark
9539                   (gnus-summary-remove-process-mark
9540                    (gnus-summary-article-number))
9541                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9542               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9543         (setq n (1- n)))
9544       (when (/= 0 n)
9545         (gnus-message 7 "No more articles"))
9546       (gnus-summary-recenter)
9547       (gnus-summary-position-point)
9548       n)))
9549
9550 (defun gnus-summary-unmark-as-processable (n)
9551   "Remove the process mark from the next N articles.
9552 If N is negative, unmark backward instead.  The difference between N and
9553 the actual number of articles unmarked is returned."
9554   (interactive "P")
9555   (gnus-summary-mark-as-processable n t))
9556
9557 (defun gnus-summary-unmark-all-processable ()
9558   "Remove the process mark from all articles."
9559   (interactive)
9560   (save-excursion
9561     (while gnus-newsgroup-processable
9562       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9563   (gnus-summary-position-point))
9564
9565 (defun gnus-summary-add-mark (article type)
9566   "Mark ARTICLE with a mark of TYPE."
9567   (let ((vtype (car (assq type gnus-article-mark-lists)))
9568         var)
9569     (if (not vtype)
9570         (error "No such mark type: %s" type)
9571       (setq var (intern (format "gnus-newsgroup-%s" type)))
9572       (set var (cons article (symbol-value var)))
9573       (if (memq type '(processable cached replied forwarded recent saved))
9574           (gnus-summary-update-secondary-mark article)
9575         ;;; !!! This is bogus.  We should find out what primary
9576         ;;; !!! mark we want to set.
9577         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9578
9579 (defun gnus-summary-mark-as-expirable (n)
9580   "Mark N articles forward as expirable.
9581 If N is negative, mark backward instead.  The difference between N and
9582 the actual number of articles marked is returned."
9583   (interactive "p")
9584   (gnus-summary-mark-forward n gnus-expirable-mark))
9585
9586 (defun gnus-summary-mark-as-spam (n)
9587   "Mark N articles forward as spam.
9588 If N is negative, mark backward instead.  The difference between N and
9589 the actual number of articles marked is returned."
9590   (interactive "p")
9591   (gnus-summary-mark-forward n gnus-spam-mark))
9592
9593 (defun gnus-summary-mark-article-as-replied (article)
9594   "Mark ARTICLE as replied to and update the summary line.
9595 ARTICLE can also be a list of articles."
9596   (interactive (list (gnus-summary-article-number)))
9597   (let ((articles (if (listp article) article (list article))))
9598     (dolist (article articles)
9599       (unless (numberp article)
9600         (error "%s is not a number" article))
9601       (push article gnus-newsgroup-replied)
9602       (let ((buffer-read-only nil))
9603         (when (gnus-summary-goto-subject article nil t)
9604           (gnus-summary-update-secondary-mark article))))))
9605
9606 (defun gnus-summary-mark-article-as-forwarded (article)
9607   "Mark ARTICLE as forwarded and update the summary line.
9608 ARTICLE can also be a list of articles."
9609   (let ((articles (if (listp article) article (list article))))
9610     (dolist (article articles)
9611       (push article gnus-newsgroup-forwarded)
9612       (let ((buffer-read-only nil))
9613         (when (gnus-summary-goto-subject article nil t)
9614           (gnus-summary-update-secondary-mark article))))))
9615
9616 (defun gnus-summary-set-bookmark (article)
9617   "Set a bookmark in current article."
9618   (interactive (list (gnus-summary-article-number)))
9619   (when (or (not (get-buffer gnus-article-buffer))
9620             (not gnus-current-article)
9621             (not gnus-article-current)
9622             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9623     (error "No current article selected"))
9624   ;; Remove old bookmark, if one exists.
9625   (gnus-pull article gnus-newsgroup-bookmarks)
9626   ;; Set the new bookmark, which is on the form
9627   ;; (article-number . line-number-in-body).
9628   (push
9629    (cons article
9630          (save-excursion
9631            (set-buffer gnus-article-buffer)
9632            (count-lines
9633             (min (point)
9634                  (save-excursion
9635                    (article-goto-body)
9636                    (point)))
9637             (point))))
9638    gnus-newsgroup-bookmarks)
9639   (gnus-message 6 "A bookmark has been added to the current article."))
9640
9641 (defun gnus-summary-remove-bookmark (article)
9642   "Remove the bookmark from the current article."
9643   (interactive (list (gnus-summary-article-number)))
9644   ;; Remove old bookmark, if one exists.
9645   (if (not (assq article gnus-newsgroup-bookmarks))
9646       (gnus-message 6 "No bookmark in current article.")
9647     (gnus-pull article gnus-newsgroup-bookmarks)
9648     (gnus-message 6 "Removed bookmark.")))
9649
9650 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9651 (defun gnus-summary-mark-as-dormant (n)
9652   "Mark N articles forward as dormant.
9653 If N is negative, mark backward instead.  The difference between N and
9654 the actual number of articles marked is returned."
9655   (interactive "p")
9656   (gnus-summary-mark-forward n gnus-dormant-mark))
9657
9658 (defun gnus-summary-set-process-mark (article)
9659   "Set the process mark on ARTICLE and update the summary line."
9660   (setq gnus-newsgroup-processable
9661         (cons article
9662               (delq article gnus-newsgroup-processable)))
9663   (when (gnus-summary-goto-subject article)
9664     (gnus-summary-show-thread)
9665     (gnus-summary-goto-subject article)
9666     (gnus-summary-update-secondary-mark article)))
9667
9668 (defun gnus-summary-remove-process-mark (article)
9669   "Remove the process mark from ARTICLE and update the summary line."
9670   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9671   (when (gnus-summary-goto-subject article)
9672     (gnus-summary-show-thread)
9673     (gnus-summary-goto-subject article)
9674     (gnus-summary-update-secondary-mark article)))
9675
9676 (defun gnus-summary-set-saved-mark (article)
9677   "Set the process mark on ARTICLE and update the summary line."
9678   (push article gnus-newsgroup-saved)
9679   (when (gnus-summary-goto-subject article)
9680     (gnus-summary-update-secondary-mark article)))
9681
9682 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9683   "Mark N articles as read forwards.
9684 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9685 The difference between N and the actual number of articles marked is
9686 returned.
9687 If NO-EXPIRE, auto-expiry will be inhibited."
9688   (interactive "p")
9689   (gnus-summary-show-thread)
9690   (let ((backward (< n 0))
9691         (gnus-summary-goto-unread
9692          (and gnus-summary-goto-unread
9693               (not (eq gnus-summary-goto-unread 'never))
9694               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9695                                     gnus-ticked-mark gnus-dormant-mark)))))
9696         (n (abs n))
9697         (mark (or mark gnus-del-mark)))
9698     (while (and (> n 0)
9699                 (gnus-summary-mark-article nil mark no-expire)
9700                 (zerop (gnus-summary-next-subject
9701                         (if backward -1 1)
9702                         (and gnus-summary-goto-unread
9703                              (not (eq gnus-summary-goto-unread 'never)))
9704                         t)))
9705       (setq n (1- n)))
9706     (when (/= 0 n)
9707       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9708     (gnus-summary-recenter)
9709     (gnus-summary-position-point)
9710     (gnus-set-mode-line 'summary)
9711     n))
9712
9713 (defun gnus-summary-mark-article-as-read (mark)
9714   "Mark the current article quickly as read with MARK."
9715   (let ((article (gnus-summary-article-number)))
9716     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9717     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9718     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9719     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9720     (push (cons article mark) gnus-newsgroup-reads)
9721     ;; Possibly remove from cache, if that is used.
9722     (when gnus-use-cache
9723       (gnus-cache-enter-remove-article article))
9724     ;; Allow the backend to change the mark.
9725     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9726     ;; Check for auto-expiry.
9727     (when (and gnus-newsgroup-auto-expire
9728                (memq mark gnus-auto-expirable-marks))
9729       (setq mark gnus-expirable-mark)
9730       ;; Let the backend know about the mark change.
9731       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9732       (push article gnus-newsgroup-expirable))
9733     ;; Set the mark in the buffer.
9734     (gnus-summary-update-mark mark 'unread)
9735     t))
9736
9737 (defun gnus-summary-mark-article-as-unread (mark)
9738   "Mark the current article quickly as unread with MARK."
9739   (let* ((article (gnus-summary-article-number))
9740          (old-mark (gnus-summary-article-mark article)))
9741     ;; Allow the backend to change the mark.
9742     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9743     (if (eq mark old-mark)
9744         t
9745       (if (<= article 0)
9746           (progn
9747             (gnus-error 1 "Can't mark negative article numbers")
9748             nil)
9749         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9750         (setq gnus-newsgroup-spam-marked
9751               (delq article gnus-newsgroup-spam-marked))
9752         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9753         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9754         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9755         (cond ((= mark gnus-ticked-mark)
9756                (setq gnus-newsgroup-marked
9757                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9758                                               article)))
9759               ((= mark gnus-spam-mark)
9760                (setq gnus-newsgroup-spam-marked
9761                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9762                                               article)))
9763               ((= mark gnus-dormant-mark)
9764                (setq gnus-newsgroup-dormant
9765                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9766                                               article)))
9767               (t
9768                (setq gnus-newsgroup-unreads
9769                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9770                                               article))))
9771         (gnus-pull article gnus-newsgroup-reads)
9772
9773         ;; See whether the article is to be put in the cache.
9774         (and gnus-use-cache
9775              (vectorp (gnus-summary-article-header article))
9776              (save-excursion
9777                (gnus-cache-possibly-enter-article
9778                 gnus-newsgroup-name article
9779                 (gnus-summary-article-header article)
9780                 (= mark gnus-ticked-mark)
9781                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9782
9783         ;; Fix the mark.
9784         (gnus-summary-update-mark mark 'unread)
9785         t))))
9786
9787 (defun gnus-summary-mark-article (&optional article mark no-expire)
9788   "Mark ARTICLE with MARK.  MARK can be any character.
9789 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9790 `??' (dormant) and `?E' (expirable).
9791 If MARK is nil, then the default character `?r' is used.
9792 If ARTICLE is nil, then the article on the current line will be
9793 marked.
9794 If NO-EXPIRE, auto-expiry will be inhibited."
9795   ;; The mark might be a string.
9796   (when (stringp mark)
9797     (setq mark (aref mark 0)))
9798   ;; If no mark is given, then we check auto-expiring.
9799   (when (null mark)
9800     (setq mark gnus-del-mark))
9801   (when (and (not no-expire)
9802              gnus-newsgroup-auto-expire
9803              (memq mark gnus-auto-expirable-marks))
9804     (setq mark gnus-expirable-mark))
9805   (let ((article (or article (gnus-summary-article-number)))
9806         (old-mark (gnus-summary-article-mark article)))
9807     ;; Allow the backend to change the mark.
9808     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9809     (if (eq mark old-mark)
9810         t
9811       (unless article
9812         (error "No article on current line"))
9813       (if (not (if (or (= mark gnus-unread-mark)
9814                        (= mark gnus-ticked-mark)
9815                        (= mark gnus-spam-mark)
9816                        (= mark gnus-dormant-mark))
9817                    (gnus-mark-article-as-unread article mark)
9818                  (gnus-mark-article-as-read article mark)))
9819           t
9820         ;; See whether the article is to be put in the cache.
9821         (and gnus-use-cache
9822              (not (= mark gnus-canceled-mark))
9823              (vectorp (gnus-summary-article-header article))
9824              (save-excursion
9825                (gnus-cache-possibly-enter-article
9826                 gnus-newsgroup-name article
9827                 (gnus-summary-article-header article)
9828                 (= mark gnus-ticked-mark)
9829                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9830
9831         (when (gnus-summary-goto-subject article nil t)
9832           (let ((buffer-read-only nil))
9833             (gnus-summary-show-thread)
9834             ;; Fix the mark.
9835             (gnus-summary-update-mark mark 'unread)
9836             t))))))
9837
9838 (defun gnus-summary-update-secondary-mark (article)
9839   "Update the secondary (read, process, cache) mark."
9840   (gnus-summary-update-mark
9841    (cond ((memq article gnus-newsgroup-processable)
9842           gnus-process-mark)
9843          ((memq article gnus-newsgroup-cached)
9844           gnus-cached-mark)
9845          ((memq article gnus-newsgroup-replied)
9846           gnus-replied-mark)
9847          ((memq article gnus-newsgroup-forwarded)
9848           gnus-forwarded-mark)
9849          ((memq article gnus-newsgroup-saved)
9850           gnus-saved-mark)
9851          ((memq article gnus-newsgroup-recent)
9852           gnus-recent-mark)
9853          ((memq article gnus-newsgroup-unseen)
9854           gnus-unseen-mark)
9855          (t gnus-no-mark))
9856    'replied)
9857   (when (gnus-visual-p 'summary-highlight 'highlight)
9858     (gnus-run-hooks 'gnus-summary-update-hook))
9859   t)
9860
9861 (defun gnus-summary-update-download-mark (article)
9862   "Update the secondary (read, process, cache) mark."
9863   (gnus-summary-update-mark
9864    (cond ((memq article gnus-newsgroup-undownloaded) 
9865           gnus-undownloaded-mark)
9866          (gnus-newsgroup-agentized
9867           gnus-downloaded-mark)
9868          (t
9869           gnus-no-mark))
9870    'download)
9871   (gnus-summary-update-line t)
9872   t)
9873
9874 (defun gnus-summary-update-mark (mark type)
9875   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9876         (buffer-read-only nil))
9877     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9878     (when forward
9879       (when (looking-at "\r")
9880         (incf forward))
9881       (when (<= (+ forward (point)) (point-max))
9882         ;; Go to the right position on the line.
9883         (goto-char (+ forward (point)))
9884         ;; Replace the old mark with the new mark.
9885         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9886         ;; Optionally update the marks by some user rule.
9887         (when (eq type 'unread)
9888           (gnus-data-set-mark
9889            (gnus-data-find (gnus-summary-article-number)) mark)
9890           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9891
9892 (defun gnus-mark-article-as-read (article &optional mark)
9893   "Enter ARTICLE in the pertinent lists and remove it from others."
9894   ;; Make the article expirable.
9895   (let ((mark (or mark gnus-del-mark)))
9896     (setq gnus-newsgroup-expirable
9897           (if (= mark gnus-expirable-mark)
9898               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9899             (delq article gnus-newsgroup-expirable)))
9900     ;; Remove from unread and marked lists.
9901     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9902     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9903     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9904     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9905     (push (cons article mark) gnus-newsgroup-reads)
9906     ;; Possibly remove from cache, if that is used.
9907     (when gnus-use-cache
9908       (gnus-cache-enter-remove-article article))
9909     t))
9910
9911 (defun gnus-mark-article-as-unread (article &optional mark)
9912   "Enter ARTICLE in the pertinent lists and remove it from others."
9913   (let ((mark (or mark gnus-ticked-mark)))
9914     (if (<= article 0)
9915         (progn
9916           (gnus-error 1 "Can't mark negative article numbers")
9917           nil)
9918       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9919             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9920             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9921             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9922             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9923
9924       ;; Unsuppress duplicates?
9925       (when gnus-suppress-duplicates
9926         (gnus-dup-unsuppress-article article))
9927
9928       (cond ((= mark gnus-ticked-mark)
9929              (setq gnus-newsgroup-marked
9930                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9931             ((= mark gnus-spam-mark)
9932              (setq gnus-newsgroup-spam-marked
9933                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9934                                             article)))
9935             ((= mark gnus-dormant-mark)
9936              (setq gnus-newsgroup-dormant
9937                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9938             (t
9939              (setq gnus-newsgroup-unreads
9940                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9941       (gnus-pull article gnus-newsgroup-reads)
9942       t)))
9943
9944 (defalias 'gnus-summary-mark-as-unread-forward
9945   'gnus-summary-tick-article-forward)
9946 (make-obsolete 'gnus-summary-mark-as-unread-forward
9947                'gnus-summary-tick-article-forward)
9948 (defun gnus-summary-tick-article-forward (n)
9949   "Tick N articles forwards.
9950 If N is negative, tick backwards instead.
9951 The difference between N and the number of articles ticked is returned."
9952   (interactive "p")
9953   (gnus-summary-mark-forward n gnus-ticked-mark))
9954
9955 (defalias 'gnus-summary-mark-as-unread-backward
9956   'gnus-summary-tick-article-backward)
9957 (make-obsolete 'gnus-summary-mark-as-unread-backward
9958                'gnus-summary-tick-article-backward)
9959 (defun gnus-summary-tick-article-backward (n)
9960   "Tick N articles backwards.
9961 The difference between N and the number of articles ticked is returned."
9962   (interactive "p")
9963   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9964
9965 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9966 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9967 (defun gnus-summary-tick-article (&optional article clear-mark)
9968   "Mark current article as unread.
9969 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9970 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9971   (interactive)
9972   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9973                                        gnus-ticked-mark)))
9974
9975 (defun gnus-summary-mark-as-read-forward (n)
9976   "Mark N articles as read forwards.
9977 If N is negative, mark backwards instead.
9978 The difference between N and the actual number of articles marked is
9979 returned."
9980   (interactive "p")
9981   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9982
9983 (defun gnus-summary-mark-as-read-backward (n)
9984   "Mark the N articles as read backwards.
9985 The difference between N and the actual number of articles marked is
9986 returned."
9987   (interactive "p")
9988   (gnus-summary-mark-forward
9989    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9990
9991 (defun gnus-summary-mark-as-read (&optional article mark)
9992   "Mark current article as read.
9993 ARTICLE specifies the article to be marked as read.
9994 MARK specifies a string to be inserted at the beginning of the line."
9995   (gnus-summary-mark-article article mark))
9996
9997 (defun gnus-summary-clear-mark-forward (n)
9998   "Clear marks from N articles forward.
9999 If N is negative, clear backward instead.
10000 The difference between N and the number of marks cleared is returned."
10001   (interactive "p")
10002   (gnus-summary-mark-forward n gnus-unread-mark))
10003
10004 (defun gnus-summary-clear-mark-backward (n)
10005   "Clear marks from N articles backward.
10006 The difference between N and the number of marks cleared is returned."
10007   (interactive "p")
10008   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10009
10010 (defun gnus-summary-mark-unread-as-read ()
10011   "Intended to be used by `gnus-summary-mark-article-hook'."
10012   (when (memq gnus-current-article gnus-newsgroup-unreads)
10013     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10014
10015 (defun gnus-summary-mark-read-and-unread-as-read ()
10016   "Intended to be used by `gnus-summary-mark-article-hook'."
10017   (let ((mark (gnus-summary-article-mark)))
10018     (when (or (gnus-unread-mark-p mark)
10019               (gnus-read-mark-p mark))
10020       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
10021
10022 (defun gnus-summary-mark-unread-as-ticked ()
10023   "Intended to be used by `gnus-summary-mark-article-hook'."
10024   (when (memq gnus-current-article gnus-newsgroup-unreads)
10025     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10026
10027 (defun gnus-summary-mark-region-as-read (point mark all)
10028   "Mark all unread articles between point and mark as read.
10029 If given a prefix, mark all articles between point and mark as read,
10030 even ticked and dormant ones."
10031   (interactive "r\nP")
10032   (save-excursion
10033     (let (article)
10034       (goto-char point)
10035       (beginning-of-line)
10036       (while (and
10037               (< (point) mark)
10038               (progn
10039                 (when (or all
10040                           (memq (setq article (gnus-summary-article-number))
10041                                 gnus-newsgroup-unreads))
10042                   (gnus-summary-mark-article article gnus-del-mark))
10043                 t)
10044               (gnus-summary-find-next))))))
10045
10046 (defun gnus-summary-mark-below (score mark)
10047   "Mark articles with score less than SCORE with MARK."
10048   (interactive "P\ncMark: ")
10049   (setq score (if score
10050                   (prefix-numeric-value score)
10051                 (or gnus-summary-default-score 0)))
10052   (save-excursion
10053     (set-buffer gnus-summary-buffer)
10054     (goto-char (point-min))
10055     (while
10056         (progn
10057           (and (< (gnus-summary-article-score) score)
10058                (gnus-summary-mark-article nil mark))
10059           (gnus-summary-find-next)))))
10060
10061 (defun gnus-summary-kill-below (&optional score)
10062   "Mark articles with score below SCORE as read."
10063   (interactive "P")
10064   (gnus-summary-mark-below score gnus-killed-mark))
10065
10066 (defun gnus-summary-clear-above (&optional score)
10067   "Clear all marks from articles with score above SCORE."
10068   (interactive "P")
10069   (gnus-summary-mark-above score gnus-unread-mark))
10070
10071 (defun gnus-summary-tick-above (&optional score)
10072   "Tick all articles with score above SCORE."
10073   (interactive "P")
10074   (gnus-summary-mark-above score gnus-ticked-mark))
10075
10076 (defun gnus-summary-mark-above (score mark)
10077   "Mark articles with score over SCORE with MARK."
10078   (interactive "P\ncMark: ")
10079   (setq score (if score
10080                   (prefix-numeric-value score)
10081                 (or gnus-summary-default-score 0)))
10082   (save-excursion
10083     (set-buffer gnus-summary-buffer)
10084     (goto-char (point-min))
10085     (while (and (progn
10086                   (when (> (gnus-summary-article-score) score)
10087                     (gnus-summary-mark-article nil mark))
10088                   t)
10089                 (gnus-summary-find-next)))))
10090
10091 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10092 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10093 (defun gnus-summary-limit-include-expunged (&optional no-error)
10094   "Display all the hidden articles that were expunged for low scores."
10095   (interactive)
10096   (let ((buffer-read-only nil))
10097     (let ((scored gnus-newsgroup-scored)
10098           headers h)
10099       (while scored
10100         (unless (gnus-summary-article-header (caar scored))
10101           (and (setq h (gnus-number-to-header (caar scored)))
10102                (< (cdar scored) gnus-summary-expunge-below)
10103                (push h headers)))
10104         (setq scored (cdr scored)))
10105       (if (not headers)
10106           (when (not no-error)
10107             (error "No expunged articles hidden"))
10108         (goto-char (point-min))
10109         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10110         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10111         (mapcar (lambda (x) (push (mail-header-number x)
10112                                   gnus-newsgroup-limit))
10113                 headers)
10114         (gnus-summary-prepare-unthreaded (nreverse headers))
10115         (goto-char (point-min))
10116         (gnus-summary-position-point)
10117         t))))
10118
10119 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10120   "Mark all unread articles in this newsgroup as read.
10121 If prefix argument ALL is non-nil, ticked and dormant articles will
10122 also be marked as read.
10123 If QUIETLY is non-nil, no questions will be asked.
10124 If TO-HERE is non-nil, it should be a point in the buffer.  All
10125 articles before (after, if REVERSE is set) this point will be marked as read.
10126 Note that this function will only catch up the unread article
10127 in the current summary buffer limitation.
10128 The number of articles marked as read is returned."
10129   (interactive "P")
10130   (prog1
10131       (save-excursion
10132         (when (or quietly
10133                   (not gnus-interactive-catchup) ;Without confirmation?
10134                   gnus-expert-user
10135                   (gnus-y-or-n-p
10136                    (if all
10137                        "Mark absolutely all articles as read? "
10138                      "Mark all unread articles as read? ")))
10139           (if (and not-mark
10140                    (not gnus-newsgroup-adaptive)
10141                    (not gnus-newsgroup-auto-expire)
10142                    (not gnus-suppress-duplicates)
10143                    (or (not gnus-use-cache)
10144                        (eq gnus-use-cache 'passive)))
10145               (progn
10146                 (when all
10147                   (setq gnus-newsgroup-marked nil
10148                         gnus-newsgroup-spam-marked nil
10149                         gnus-newsgroup-dormant nil))
10150                 (setq gnus-newsgroup-unreads
10151                       (gnus-sorted-nunion
10152                        (gnus-intersection gnus-newsgroup-unreads
10153                                           gnus-newsgroup-downloadable)
10154                        gnus-newsgroup-unfetched)))
10155             ;; We actually mark all articles as canceled, which we
10156             ;; have to do when using auto-expiry or adaptive scoring.
10157             (gnus-summary-show-all-threads)
10158             (if (and to-here reverse)
10159                 (progn
10160                   (goto-char to-here)
10161                   (while (and
10162                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10163                           (gnus-summary-find-next (not all)))))
10164               (when (gnus-summary-first-subject (not all))
10165                 (while (and
10166                         (if to-here (< (point) to-here) t)
10167                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10168                         (gnus-summary-find-next (not all))))))
10169             (gnus-set-mode-line 'summary))
10170           t))
10171     (gnus-summary-position-point)))
10172
10173 (defun gnus-summary-catchup-to-here (&optional all)
10174   "Mark all unticked articles before the current one as read.
10175 If ALL is non-nil, also mark ticked and dormant articles as read."
10176   (interactive "P")
10177   (save-excursion
10178     (gnus-save-hidden-threads
10179       (let ((beg (point)))
10180         ;; We check that there are unread articles.
10181         (when (or all (gnus-summary-find-prev))
10182           (gnus-summary-catchup all t beg)))))
10183   (gnus-summary-position-point))
10184
10185 (defun gnus-summary-catchup-from-here (&optional all)
10186   "Mark all unticked articles after the current one as read.
10187 If ALL is non-nil, also mark ticked and dormant articles as read."
10188   (interactive "P")
10189   (save-excursion
10190     (gnus-save-hidden-threads
10191       (let ((beg (point)))
10192         ;; We check that there are unread articles.
10193         (when (or all (gnus-summary-find-next))
10194           (gnus-summary-catchup all t beg nil t)))))
10195   (gnus-summary-position-point))
10196
10197 (defun gnus-summary-catchup-all (&optional quietly)
10198   "Mark all articles in this newsgroup as read.
10199 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10200 instead, which marks only unread articles as read."
10201   (interactive "P")
10202   (gnus-summary-catchup t quietly))
10203
10204 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10205   "Mark all unread articles in this group as read, then exit.
10206 If prefix argument ALL is non-nil, all articles are marked as read.
10207 If QUIETLY is non-nil, no questions will be asked."
10208   (interactive "P")
10209   (when (gnus-summary-catchup all quietly nil 'fast)
10210     ;; Select next newsgroup or exit.
10211     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10212              (eq gnus-auto-select-next 'quietly))
10213         (gnus-summary-next-group nil)
10214       (gnus-summary-exit))))
10215
10216 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10217   "Mark all articles in this newsgroup as read, and then exit.
10218 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10219 instead, which marks only unread articles as read."
10220   (interactive "P")
10221   (gnus-summary-catchup-and-exit t quietly))
10222
10223 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10224   "Mark all articles in this group as read and select the next group.
10225 If given a prefix, mark all articles, unread as well as ticked, as
10226 read."
10227   (interactive "P")
10228   (save-excursion
10229     (gnus-summary-catchup all))
10230   (gnus-summary-next-group))
10231
10232 ;;;
10233 ;;; with article
10234 ;;;
10235
10236 (defmacro gnus-with-article (article &rest forms)
10237   "Select ARTICLE and perform FORMS in the original article buffer.
10238 Then replace the article with the result."
10239   `(progn
10240      ;; We don't want the article to be marked as read.
10241      (let (gnus-mark-article-hook)
10242        (gnus-summary-select-article t t nil ,article))
10243      (set-buffer gnus-original-article-buffer)
10244      ,@forms
10245      (if (not (gnus-check-backend-function
10246                'request-replace-article (car gnus-article-current)))
10247          (gnus-message 5 "Read-only group; not replacing")
10248        (unless (gnus-request-replace-article
10249                 ,article (car gnus-article-current)
10250                 (current-buffer) t)
10251          (error "Couldn't replace article")))
10252      ;; The cache and backlog have to be flushed somewhat.
10253      (when gnus-keep-backlog
10254        (gnus-backlog-remove-article
10255         (car gnus-article-current) (cdr gnus-article-current)))
10256      (when gnus-use-cache
10257        (gnus-cache-update-article
10258         (car gnus-article-current) (cdr gnus-article-current)))))
10259
10260 (put 'gnus-with-article 'lisp-indent-function 1)
10261 (put 'gnus-with-article 'edebug-form-spec '(form body))
10262
10263 ;; Thread-based commands.
10264
10265 (defun gnus-summary-articles-in-thread (&optional article)
10266   "Return a list of all articles in the current thread.
10267 If ARTICLE is non-nil, return all articles in the thread that starts
10268 with that article."
10269   (let* ((article (or article (gnus-summary-article-number)))
10270          (data (gnus-data-find-list article))
10271          (top-level (gnus-data-level (car data)))
10272          (top-subject
10273           (cond ((null gnus-thread-operation-ignore-subject)
10274                  (gnus-simplify-subject-re
10275                   (mail-header-subject (gnus-data-header (car data)))))
10276                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10277                  (gnus-simplify-subject-fuzzy
10278                   (mail-header-subject (gnus-data-header (car data)))))
10279                 (t nil)))
10280          (end-point (save-excursion
10281                       (if (gnus-summary-go-to-next-thread)
10282                           (point) (point-max))))
10283          articles)
10284     (while (and data
10285                 (< (gnus-data-pos (car data)) end-point))
10286       (when (or (not top-subject)
10287                 (string= top-subject
10288                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10289                              (gnus-simplify-subject-fuzzy
10290                               (mail-header-subject
10291                                (gnus-data-header (car data))))
10292                            (gnus-simplify-subject-re
10293                             (mail-header-subject
10294                              (gnus-data-header (car data)))))))
10295         (push (gnus-data-number (car data)) articles))
10296       (unless (and (setq data (cdr data))
10297                    (> (gnus-data-level (car data)) top-level))
10298         (setq data nil)))
10299     ;; Return the list of articles.
10300     (nreverse articles)))
10301
10302 (defun gnus-summary-rethread-current ()
10303   "Rethread the thread the current article is part of."
10304   (interactive)
10305   (let* ((gnus-show-threads t)
10306          (article (gnus-summary-article-number))
10307          (id (mail-header-id (gnus-summary-article-header)))
10308          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10309     (unless id
10310       (error "No article on the current line"))
10311     (gnus-rebuild-thread id)
10312     (gnus-summary-goto-subject article)))
10313
10314 (defun gnus-summary-reparent-thread ()
10315   "Make the current article child of the marked (or previous) article.
10316
10317 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10318 is non-nil or the Subject: of both articles are the same."
10319   (interactive)
10320   (unless (not (gnus-group-read-only-p))
10321     (error "The current newsgroup does not support article editing"))
10322   (unless (<= (length gnus-newsgroup-processable) 1)
10323     (error "No more than one article may be marked"))
10324   (save-window-excursion
10325     (let ((gnus-article-buffer " *reparent*")
10326           (current-article (gnus-summary-article-number))
10327           ;; First grab the marked article, otherwise one line up.
10328           (parent-article (if (not (null gnus-newsgroup-processable))
10329                               (car gnus-newsgroup-processable)
10330                             (save-excursion
10331                               (if (eq (forward-line -1) 0)
10332                                   (gnus-summary-article-number)
10333                                 (error "Beginning of summary buffer"))))))
10334       (unless (not (eq current-article parent-article))
10335         (error "An article may not be self-referential"))
10336       (let ((message-id (mail-header-id
10337                          (gnus-summary-article-header parent-article))))
10338         (unless (and message-id (not (equal message-id "")))
10339           (error "No message-id in desired parent"))
10340         (gnus-with-article current-article
10341           (save-restriction
10342             (goto-char (point-min))
10343             (message-narrow-to-head)
10344             (if (re-search-forward "^References: " nil t)
10345                 (progn
10346                   (re-search-forward "^[^ \t]" nil t)
10347                   (forward-line -1)
10348                   (end-of-line)
10349                   (insert " " message-id))
10350               (insert "References: " message-id "\n"))))
10351         (set-buffer gnus-summary-buffer)
10352         (gnus-summary-unmark-all-processable)
10353         (gnus-summary-update-article current-article)
10354         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10355             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10356         (gnus-summary-rethread-current)
10357         (gnus-message 3 "Article %d is now the child of article %d"
10358                       current-article parent-article)))))
10359
10360 (defun gnus-summary-toggle-threads (&optional arg)
10361   "Toggle showing conversation threads.
10362 If ARG is positive number, turn showing conversation threads on."
10363   (interactive "P")
10364   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10365     (setq gnus-show-threads
10366           (if (null arg) (not gnus-show-threads)
10367             (> (prefix-numeric-value arg) 0)))
10368     (gnus-summary-prepare)
10369     (gnus-summary-goto-subject current)
10370     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10371     (gnus-summary-position-point)))
10372
10373 (defun gnus-summary-show-all-threads ()
10374   "Show all threads."
10375   (interactive)
10376   (save-excursion
10377     (let ((buffer-read-only nil))
10378       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10379   (gnus-summary-position-point))
10380
10381 (defun gnus-summary-show-thread ()
10382   "Show thread subtrees.
10383 Returns nil if no thread was there to be shown."
10384   (interactive)
10385   (let ((buffer-read-only nil)
10386         (orig (point))
10387         ;; first goto end then to beg, to have point at beg after let
10388         (end (progn (end-of-line) (point)))
10389         (beg (progn (beginning-of-line) (point))))
10390     (prog1
10391         ;; Any hidden lines here?
10392         (search-forward "\r" end t)
10393       (subst-char-in-region beg end ?\^M ?\n t)
10394       (goto-char orig)
10395       (gnus-summary-position-point))))
10396
10397 (defun gnus-summary-maybe-hide-threads ()
10398   "If requested, hide the threads that should be hidden."
10399   (when (and gnus-show-threads
10400              gnus-thread-hide-subtree)
10401     (gnus-summary-hide-all-threads
10402      (if (or (consp gnus-thread-hide-subtree)
10403              (gnus-functionp gnus-thread-hide-subtree))
10404          (gnus-make-predicate gnus-thread-hide-subtree)
10405        nil))))
10406
10407 ;;; Hiding predicates.
10408
10409 (defun gnus-article-unread-p (header)
10410   (memq (mail-header-number header) gnus-newsgroup-unreads))
10411
10412 (defun gnus-article-unseen-p (header)
10413   (memq (mail-header-number header) gnus-newsgroup-unseen))
10414
10415 (defun gnus-map-articles (predicate articles)
10416   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10417   (apply 'gnus-or (mapcar predicate
10418                           (mapcar 'gnus-summary-article-header articles))))
10419
10420 (defun gnus-summary-hide-all-threads (&optional predicate)
10421   "Hide all thread subtrees.
10422 If PREDICATE is supplied, threads that satisfy this predicate
10423 will not be hidden."
10424   (interactive)
10425   (save-excursion
10426     (goto-char (point-min))
10427     (let ((end nil))
10428       (while (not end)
10429         (when (or (not predicate)
10430                   (gnus-map-articles
10431                    predicate (gnus-summary-article-children)))
10432             (gnus-summary-hide-thread))
10433         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10434   (gnus-summary-position-point))
10435
10436 (defun gnus-summary-hide-thread ()
10437   "Hide thread subtrees.
10438 If PREDICATE is supplied, threads that satisfy this predicate
10439 will not be hidden.
10440 Returns nil if no threads were there to be hidden."
10441   (interactive)
10442   (let ((buffer-read-only nil)
10443         (start (point))
10444         (article (gnus-summary-article-number)))
10445     (goto-char start)
10446     ;; Go forward until either the buffer ends or the subthread
10447     ;; ends.
10448     (when (and (not (eobp))
10449                (or (zerop (gnus-summary-next-thread 1 t))
10450                    (goto-char (point-max))))
10451       (prog1
10452           (if (and (> (point) start)
10453                    (search-backward "\n" start t))
10454               (progn
10455                 (subst-char-in-region start (point) ?\n ?\^M)
10456                 (gnus-summary-goto-subject article))
10457             (goto-char start)
10458             nil)))))
10459
10460 (defun gnus-summary-go-to-next-thread (&optional previous)
10461   "Go to the same level (or less) next thread.
10462 If PREVIOUS is non-nil, go to previous thread instead.
10463 Return the article number moved to, or nil if moving was impossible."
10464   (let ((level (gnus-summary-thread-level))
10465         (way (if previous -1 1))
10466         (beg (point)))
10467     (forward-line way)
10468     (while (and (not (eobp))
10469                 (< level (gnus-summary-thread-level)))
10470       (forward-line way))
10471     (if (eobp)
10472         (progn
10473           (goto-char beg)
10474           nil)
10475       (setq beg (point))
10476       (prog1
10477           (gnus-summary-article-number)
10478         (goto-char beg)))))
10479
10480 (defun gnus-summary-next-thread (n &optional silent)
10481   "Go to the same level next N'th thread.
10482 If N is negative, search backward instead.
10483 Returns the difference between N and the number of skips actually
10484 done.
10485
10486 If SILENT, don't output messages."
10487   (interactive "p")
10488   (let ((backward (< n 0))
10489         (n (abs n)))
10490     (while (and (> n 0)
10491                 (gnus-summary-go-to-next-thread backward))
10492       (decf n))
10493     (unless silent
10494       (gnus-summary-position-point))
10495     (when (and (not silent) (/= 0 n))
10496       (gnus-message 7 "No more threads"))
10497     n))
10498
10499 (defun gnus-summary-prev-thread (n)
10500   "Go to the same level previous N'th thread.
10501 Returns the difference between N and the number of skips actually
10502 done."
10503   (interactive "p")
10504   (gnus-summary-next-thread (- n)))
10505
10506 (defun gnus-summary-go-down-thread ()
10507   "Go down one level in the current thread."
10508   (let ((children (gnus-summary-article-children)))
10509     (when children
10510       (gnus-summary-goto-subject (car children)))))
10511
10512 (defun gnus-summary-go-up-thread ()
10513   "Go up one level in the current thread."
10514   (let ((parent (gnus-summary-article-parent)))
10515     (when parent
10516       (gnus-summary-goto-subject parent))))
10517
10518 (defun gnus-summary-down-thread (n)
10519   "Go down thread N steps.
10520 If N is negative, go up instead.
10521 Returns the difference between N and how many steps down that were
10522 taken."
10523   (interactive "p")
10524   (let ((up (< n 0))
10525         (n (abs n)))
10526     (while (and (> n 0)
10527                 (if up (gnus-summary-go-up-thread)
10528                   (gnus-summary-go-down-thread)))
10529       (setq n (1- n)))
10530     (gnus-summary-position-point)
10531     (when (/= 0 n)
10532       (gnus-message 7 "Can't go further"))
10533     n))
10534
10535 (defun gnus-summary-up-thread (n)
10536   "Go up thread N steps.
10537 If N is negative, go down instead.
10538 Returns the difference between N and how many steps down that were
10539 taken."
10540   (interactive "p")
10541   (gnus-summary-down-thread (- n)))
10542
10543 (defun gnus-summary-top-thread ()
10544   "Go to the top of the thread."
10545   (interactive)
10546   (while (gnus-summary-go-up-thread))
10547   (gnus-summary-article-number))
10548
10549 (defun gnus-summary-kill-thread (&optional unmark)
10550   "Mark articles under current thread as read.
10551 If the prefix argument is positive, remove any kinds of marks.
10552 If the prefix argument is negative, tick articles instead."
10553   (interactive "P")
10554   (when unmark
10555     (setq unmark (prefix-numeric-value unmark)))
10556   (let ((articles (gnus-summary-articles-in-thread)))
10557     (save-excursion
10558       ;; Expand the thread.
10559       (gnus-summary-show-thread)
10560       ;; Mark all the articles.
10561       (while articles
10562         (gnus-summary-goto-subject (car articles))
10563         (cond ((null unmark)
10564                (gnus-summary-mark-article-as-read gnus-killed-mark))
10565               ((> unmark 0)
10566                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10567               (t
10568                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10569         (setq articles (cdr articles))))
10570     ;; Hide killed subtrees.
10571     (and (null unmark)
10572          gnus-thread-hide-killed
10573          (gnus-summary-hide-thread))
10574     ;; If marked as read, go to next unread subject.
10575     (when (null unmark)
10576       ;; Go to next unread subject.
10577       (gnus-summary-next-subject 1 t)))
10578   (gnus-set-mode-line 'summary))
10579
10580 ;; Summary sorting commands
10581
10582 (defun gnus-summary-sort-by-number (&optional reverse)
10583   "Sort the summary buffer by article number.
10584 Argument REVERSE means reverse order."
10585   (interactive "P")
10586   (gnus-summary-sort 'number reverse))
10587
10588 (defun gnus-summary-sort-by-random (&optional reverse)
10589   "Randomize the order in the summary buffer.
10590 Argument REVERSE means to randomize in reverse order."
10591   (interactive "P")
10592   (gnus-summary-sort 'random reverse))
10593
10594 (defun gnus-summary-sort-by-author (&optional reverse)
10595   "Sort the summary buffer by author name alphabetically.
10596 If `case-fold-search' is non-nil, case of letters is ignored.
10597 Argument REVERSE means reverse order."
10598   (interactive "P")
10599   (gnus-summary-sort 'author reverse))
10600
10601 (defun gnus-summary-sort-by-subject (&optional reverse)
10602   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10603 If `case-fold-search' is non-nil, case of letters is ignored.
10604 Argument REVERSE means reverse order."
10605   (interactive "P")
10606   (gnus-summary-sort 'subject reverse))
10607
10608 (defun gnus-summary-sort-by-date (&optional reverse)
10609   "Sort the summary buffer by date.
10610 Argument REVERSE means reverse order."
10611   (interactive "P")
10612   (gnus-summary-sort 'date reverse))
10613
10614 (defun gnus-summary-sort-by-score (&optional reverse)
10615   "Sort the summary buffer by score.
10616 Argument REVERSE means reverse order."
10617   (interactive "P")
10618   (gnus-summary-sort 'score reverse))
10619
10620 (defun gnus-summary-sort-by-lines (&optional reverse)
10621   "Sort the summary buffer by the number of lines.
10622 Argument REVERSE means reverse order."
10623   (interactive "P")
10624   (gnus-summary-sort 'lines reverse))
10625
10626 (defun gnus-summary-sort-by-chars (&optional reverse)
10627   "Sort the summary buffer by article length.
10628 Argument REVERSE means reverse order."
10629   (interactive "P")
10630   (gnus-summary-sort 'chars reverse))
10631
10632 (defun gnus-summary-sort-by-original (&optional reverse)
10633   "Sort the summary buffer using the default sorting method.
10634 Argument REVERSE means reverse order."
10635   (interactive "P")
10636   (let* ((buffer-read-only)
10637          (gnus-summary-prepare-hook nil))
10638     ;; We do the sorting by regenerating the threads.
10639     (gnus-summary-prepare)
10640     ;; Hide subthreads if needed.
10641     (gnus-summary-maybe-hide-threads)))
10642
10643 (defun gnus-summary-sort (predicate reverse)
10644   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10645   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10646          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10647          (gnus-thread-sort-functions
10648           (if (not reverse)
10649               thread
10650             `(lambda (t1 t2)
10651                (,thread t2 t1))))
10652          (gnus-sort-gathered-threads-function
10653           gnus-thread-sort-functions)
10654          (gnus-article-sort-functions
10655           (if (not reverse)
10656               article
10657             `(lambda (t1 t2)
10658                (,article t2 t1))))
10659          (buffer-read-only)
10660          (gnus-summary-prepare-hook nil))
10661     ;; We do the sorting by regenerating the threads.
10662     (gnus-summary-prepare)
10663     ;; Hide subthreads if needed.
10664     (gnus-summary-maybe-hide-threads)))
10665
10666 ;; Summary saving commands.
10667
10668 (defun gnus-summary-save-article (&optional n not-saved)
10669   "Save the current article using the default saver function.
10670 If N is a positive number, save the N next articles.
10671 If N is a negative number, save the N previous articles.
10672 If N is nil and any articles have been marked with the process mark,
10673 save those articles instead.
10674 The variable `gnus-default-article-saver' specifies the saver function."
10675   (interactive "P")
10676   (let* ((articles (gnus-summary-work-articles n))
10677          (save-buffer (save-excursion
10678                         (nnheader-set-temp-buffer " *Gnus Save*")))
10679          (num (length articles))
10680          header file)
10681     (dolist (article articles)
10682       (setq header (gnus-summary-article-header article))
10683       (if (not (vectorp header))
10684           ;; This is a pseudo-article.
10685           (if (assq 'name header)
10686               (gnus-copy-file (cdr (assq 'name header)))
10687             (gnus-message 1 "Article %d is unsaveable" article))
10688         ;; This is a real article.
10689         (save-window-excursion
10690           (let ((gnus-display-mime-function nil)
10691                 (gnus-article-prepare-hook nil))
10692             (gnus-summary-select-article t nil nil article)))
10693         (save-excursion
10694           (set-buffer save-buffer)
10695           (erase-buffer)
10696           (insert-buffer-substring gnus-original-article-buffer))
10697         (setq file (gnus-article-save save-buffer file num))
10698         (gnus-summary-remove-process-mark article)
10699         (unless not-saved
10700           (gnus-summary-set-saved-mark article))))
10701     (gnus-kill-buffer save-buffer)
10702     (gnus-summary-position-point)
10703     (gnus-set-mode-line 'summary)
10704     n))
10705
10706 (defun gnus-summary-pipe-output (&optional arg headers)
10707   "Pipe the current article to a subprocess.
10708 If N is a positive number, pipe the N next articles.
10709 If N is a negative number, pipe the N previous articles.
10710 If N is nil and any articles have been marked with the process mark,
10711 pipe those articles instead.
10712 If HEADERS (the symbolic prefix), include the headers, too."
10713   (interactive (gnus-interactive "P\ny"))
10714   (require 'gnus-art)
10715   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10716         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10717     (gnus-summary-save-article arg t))
10718   (let ((buffer (get-buffer "*Shell Command Output*")))
10719     (when (and buffer
10720                (not (zerop (buffer-size buffer))))
10721       (gnus-configure-windows 'pipe))))
10722
10723 (defun gnus-summary-save-article-mail (&optional arg)
10724   "Append the current article to an mail file.
10725 If N is a positive number, save the N next articles.
10726 If N is a negative number, save the N previous articles.
10727 If N is nil and any articles have been marked with the process mark,
10728 save those articles instead."
10729   (interactive "P")
10730   (require 'gnus-art)
10731   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10732     (gnus-summary-save-article arg)))
10733
10734 (defun gnus-summary-save-article-rmail (&optional arg)
10735   "Append the current article to an rmail file.
10736 If N is a positive number, save the N next articles.
10737 If N is a negative number, save the N previous articles.
10738 If N is nil and any articles have been marked with the process mark,
10739 save those articles instead."
10740   (interactive "P")
10741   (require 'gnus-art)
10742   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10743     (gnus-summary-save-article arg)))
10744
10745 (defun gnus-summary-save-article-file (&optional arg)
10746   "Append the current article to a file.
10747 If N is a positive number, save the N next articles.
10748 If N is a negative number, save the N previous articles.
10749 If N is nil and any articles have been marked with the process mark,
10750 save those articles instead."
10751   (interactive "P")
10752   (require 'gnus-art)
10753   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10754     (gnus-summary-save-article arg)))
10755
10756 (defun gnus-summary-write-article-file (&optional arg)
10757   "Write the current article to a file, deleting the previous file.
10758 If N is a positive number, save the N next articles.
10759 If N is a negative number, save the N previous articles.
10760 If N is nil and any articles have been marked with the process mark,
10761 save those articles instead."
10762   (interactive "P")
10763   (require 'gnus-art)
10764   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10765     (gnus-summary-save-article arg)))
10766
10767 (defun gnus-summary-save-article-body-file (&optional arg)
10768   "Append the current article body to a file.
10769 If N is a positive number, save the N next articles.
10770 If N is a negative number, save the N previous articles.
10771 If N is nil and any articles have been marked with the process mark,
10772 save those articles instead."
10773   (interactive "P")
10774   (require 'gnus-art)
10775   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10776     (gnus-summary-save-article arg)))
10777
10778 (defun gnus-summary-muttprint (&optional arg)
10779   "Print the current article using Muttprint.
10780 If N is a positive number, save the N next articles.
10781 If N is a negative number, save the N previous articles.
10782 If N is nil and any articles have been marked with the process mark,
10783 save those articles instead."
10784   (interactive "P")
10785   (require 'gnus-art)
10786   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10787     (gnus-summary-save-article arg t)))
10788
10789 (defun gnus-summary-pipe-message (program)
10790   "Pipe the current article through PROGRAM."
10791   (interactive "sProgram: ")
10792   (gnus-summary-select-article)
10793   (let ((mail-header-separator ""))
10794     (gnus-eval-in-buffer-window gnus-article-buffer
10795       (save-restriction
10796         (widen)
10797         (let ((start (window-start))
10798               buffer-read-only)
10799           (message-pipe-buffer-body program)
10800           (set-window-start (get-buffer-window (current-buffer)) start))))))
10801
10802 (defun gnus-get-split-value (methods)
10803   "Return a value based on the split METHODS."
10804   (let (split-name method result match)
10805     (when methods
10806       (save-excursion
10807         (set-buffer gnus-original-article-buffer)
10808         (save-restriction
10809           (nnheader-narrow-to-headers)
10810           (while (and methods (not split-name))
10811             (goto-char (point-min))
10812             (setq method (pop methods))
10813             (setq match (car method))
10814             (when (cond
10815                    ((stringp match)
10816                     ;; Regular expression.
10817                     (ignore-errors
10818                       (re-search-forward match nil t)))
10819                    ((gnus-functionp match)
10820                     ;; Function.
10821                     (save-restriction
10822                       (widen)
10823                       (setq result (funcall match gnus-newsgroup-name))))
10824                    ((consp match)
10825                     ;; Form.
10826                     (save-restriction
10827                       (widen)
10828                       (setq result (eval match)))))
10829               (setq split-name (cdr method))
10830               (cond ((stringp result)
10831                      (push (expand-file-name
10832                             result gnus-article-save-directory)
10833                            split-name))
10834                     ((consp result)
10835                      (setq split-name (append result split-name)))))))))
10836     (nreverse split-name)))
10837
10838 (defun gnus-valid-move-group-p (group)
10839   (and (boundp group)
10840        (symbol-name group)
10841        (symbol-value group)
10842        (gnus-get-function (gnus-find-method-for-group
10843                            (symbol-name group)) 'request-accept-article t)))
10844
10845 (defun gnus-read-move-group-name (prompt default articles prefix)
10846   "Read a group name."
10847   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10848          (minibuffer-confirm-incomplete nil) ; XEmacs
10849          (prom
10850           (format "%s %s to:"
10851                   prompt
10852                   (if (> (length articles) 1)
10853                       (format "these %d articles" (length articles))
10854                     "this article")))
10855          (to-newsgroup
10856           (cond
10857            ((null split-name)
10858             (gnus-completing-read-with-default
10859              default prom
10860              gnus-active-hashtb
10861              'gnus-valid-move-group-p
10862              nil prefix
10863              'gnus-group-history))
10864            ((= 1 (length split-name))
10865             (gnus-completing-read-with-default
10866              (car split-name) prom
10867              gnus-active-hashtb
10868              'gnus-valid-move-group-p
10869              nil nil
10870              'gnus-group-history))
10871            (t
10872             (gnus-completing-read-with-default
10873              nil prom
10874              (mapcar (lambda (el) (list el))
10875                      (nreverse split-name))
10876              nil nil nil
10877              'gnus-group-history))))
10878          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10879     (when to-newsgroup
10880       (if (or (string= to-newsgroup "")
10881               (string= to-newsgroup prefix))
10882           (setq to-newsgroup default))
10883       (unless to-newsgroup
10884         (error "No group name entered"))
10885       (or (gnus-active to-newsgroup)
10886           (gnus-activate-group to-newsgroup nil nil to-method)
10887           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10888                                      to-newsgroup))
10889               (or (and (gnus-request-create-group to-newsgroup to-method)
10890                        (gnus-activate-group
10891                         to-newsgroup nil nil to-method)
10892                        (gnus-subscribe-group to-newsgroup))
10893                   (error "Couldn't create group %s" to-newsgroup)))
10894           (error "No such group: %s" to-newsgroup)))
10895     to-newsgroup))
10896
10897 (defun gnus-summary-save-parts (type dir n &optional reverse)
10898   "Save parts matching TYPE to DIR.
10899 If REVERSE, save parts that do not match TYPE."
10900   (interactive
10901    (list (read-string "Save parts of type: "
10902                       (or (car gnus-summary-save-parts-type-history)
10903                           gnus-summary-save-parts-default-mime)
10904                       'gnus-summary-save-parts-type-history)
10905          (setq gnus-summary-save-parts-last-directory
10906                (read-file-name "Save to directory: "
10907                                gnus-summary-save-parts-last-directory
10908                                nil t))
10909          current-prefix-arg))
10910   (gnus-summary-iterate n
10911     (let ((gnus-display-mime-function nil)
10912           (gnus-inhibit-treatment t))
10913       (gnus-summary-select-article))
10914     (save-excursion
10915       (set-buffer gnus-article-buffer)
10916       (let ((handles (or gnus-article-mime-handles
10917                          (mm-dissect-buffer nil gnus-article-loose-mime)
10918                          (and gnus-article-emulate-mime
10919                               (mm-uu-dissect)))))
10920         (when handles
10921           (gnus-summary-save-parts-1 type dir handles reverse)
10922           (unless gnus-article-mime-handles ;; Don't destroy this case.
10923             (mm-destroy-parts handles)))))))
10924
10925 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10926   (if (stringp (car handle))
10927       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10928               (cdr handle))
10929     (when (if reverse
10930               (not (string-match type (mm-handle-media-type handle)))
10931             (string-match type (mm-handle-media-type handle)))
10932       (let ((file (expand-file-name
10933                    (file-name-nondirectory
10934                     (or
10935                      (mail-content-type-get
10936                       (mm-handle-disposition handle) 'filename)
10937                      (concat gnus-newsgroup-name
10938                              "." (number-to-string
10939                                   (cdr gnus-article-current)))))
10940                    dir)))
10941         (unless (file-exists-p file)
10942           (mm-save-part-to-file handle file))))))
10943
10944 ;; Summary extract commands
10945
10946 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10947   (let ((buffer-read-only nil)
10948         (article (gnus-summary-article-number))
10949         after-article b e)
10950     (unless (gnus-summary-goto-subject article)
10951       (error "No such article: %d" article))
10952     (gnus-summary-position-point)
10953     ;; If all commands are to be bunched up on one line, we collect
10954     ;; them here.
10955     (unless gnus-view-pseudos-separately
10956       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10957             files action)
10958         (while ps
10959           (setq action (cdr (assq 'action (car ps))))
10960           (setq files (list (cdr (assq 'name (car ps)))))
10961           (while (and ps (cdr ps)
10962                       (string= (or action "1")
10963                                (or (cdr (assq 'action (cadr ps))) "2")))
10964             (push (cdr (assq 'name (cadr ps))) files)
10965             (setcdr ps (cddr ps)))
10966           (when files
10967             (when (not (string-match "%s" action))
10968               (push " " files))
10969             (push " " files)
10970             (when (assq 'execute (car ps))
10971               (setcdr (assq 'execute (car ps))
10972                       (funcall (if (string-match "%s" action)
10973                                    'format 'concat)
10974                                action
10975                                (mapconcat
10976                                 (lambda (f)
10977                                   (if (equal f " ")
10978                                       f
10979                                     (gnus-quote-arg-for-sh-or-csh f)))
10980                                 files " ")))))
10981           (setq ps (cdr ps)))))
10982     (if (and gnus-view-pseudos (not not-view))
10983         (while pslist
10984           (when (assq 'execute (car pslist))
10985             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10986                                   (eq gnus-view-pseudos 'not-confirm)))
10987           (setq pslist (cdr pslist)))
10988       (save-excursion
10989         (while pslist
10990           (setq after-article (or (cdr (assq 'article (car pslist)))
10991                                   (gnus-summary-article-number)))
10992           (gnus-summary-goto-subject after-article)
10993           (forward-line 1)
10994           (setq b (point))
10995           (insert "    " (file-name-nondirectory
10996                           (cdr (assq 'name (car pslist))))
10997                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10998           (setq e (point))
10999           (forward-line -1)             ; back to `b'
11000           (gnus-add-text-properties
11001            b (1- e) (list 'gnus-number gnus-reffed-article-number
11002                           gnus-mouse-face-prop gnus-mouse-face))
11003           (gnus-data-enter
11004            after-article gnus-reffed-article-number
11005            gnus-unread-mark b (car pslist) 0 (- e b))
11006           (setq gnus-newsgroup-unreads
11007                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11008                                          gnus-reffed-article-number))
11009           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11010           (setq pslist (cdr pslist)))))))
11011
11012 (defun gnus-pseudos< (p1 p2)
11013   (let ((c1 (cdr (assq 'action p1)))
11014         (c2 (cdr (assq 'action p2))))
11015     (and c1 c2 (string< c1 c2))))
11016
11017 (defun gnus-request-pseudo-article (props)
11018   (cond ((assq 'execute props)
11019          (gnus-execute-command (cdr (assq 'execute props)))))
11020   (let ((gnus-current-article (gnus-summary-article-number)))
11021     (gnus-run-hooks 'gnus-mark-article-hook)))
11022
11023 (defun gnus-execute-command (command &optional automatic)
11024   (save-excursion
11025     (gnus-article-setup-buffer)
11026     (set-buffer gnus-article-buffer)
11027     (setq buffer-read-only nil)
11028     (let ((command (if automatic command
11029                      (read-string "Command: " (cons command 0)))))
11030       (erase-buffer)
11031       (insert "$ " command "\n\n")
11032       (if gnus-view-pseudo-asynchronously
11033           (start-process "gnus-execute" (current-buffer) shell-file-name
11034                          shell-command-switch command)
11035         (call-process shell-file-name nil t nil
11036                       shell-command-switch command)))))
11037
11038 ;; Summary kill commands.
11039
11040 (defun gnus-summary-edit-global-kill (article)
11041   "Edit the \"global\" kill file."
11042   (interactive (list (gnus-summary-article-number)))
11043   (gnus-group-edit-global-kill article))
11044
11045 (defun gnus-summary-edit-local-kill ()
11046   "Edit a local kill file applied to the current newsgroup."
11047   (interactive)
11048   (setq gnus-current-headers (gnus-summary-article-header))
11049   (gnus-group-edit-local-kill
11050    (gnus-summary-article-number) gnus-newsgroup-name))
11051
11052 ;;; Header reading.
11053
11054 (defun gnus-read-header (id &optional header)
11055   "Read the headers of article ID and enter them into the Gnus system."
11056   (let ((group gnus-newsgroup-name)
11057         (gnus-override-method
11058          (or
11059           gnus-override-method
11060           (and (gnus-news-group-p gnus-newsgroup-name)
11061                (car (gnus-refer-article-methods)))))
11062         where)
11063     ;; First we check to see whether the header in question is already
11064     ;; fetched.
11065     (if (stringp id)
11066         ;; This is a Message-ID.
11067         (setq header (or header (gnus-id-to-header id)))
11068       ;; This is an article number.
11069       (setq header (or header (gnus-summary-article-header id))))
11070     (if (and header
11071              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11072         ;; We have found the header.
11073         header
11074       ;; If this is a sparse article, we have to nix out its
11075       ;; previous entry in the thread hashtb.
11076       (when (and header
11077                  (gnus-summary-article-sparse-p (mail-header-number header)))
11078         (let* ((parent (gnus-parent-id (mail-header-references header)))
11079                (thread (and parent (gnus-id-to-thread parent))))
11080           (when thread
11081             (delq (assq header thread) thread))))
11082       ;; We have to really fetch the header to this article.
11083       (save-excursion
11084         (set-buffer nntp-server-buffer)
11085         (when (setq where (gnus-request-head id group))
11086           (nnheader-fold-continuation-lines)
11087           (goto-char (point-max))
11088           (insert ".\n")
11089           (goto-char (point-min))
11090           (insert "211 ")
11091           (princ (cond
11092                   ((numberp id) id)
11093                   ((cdr where) (cdr where))
11094                   (header (mail-header-number header))
11095                   (t gnus-reffed-article-number))
11096                  (current-buffer))
11097           (insert " Article retrieved.\n"))
11098         (if (or (not where)
11099                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11100             ()                          ; Malformed head.
11101           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11102             (when (and (stringp id)
11103                        (not (string= (gnus-group-real-name group)
11104                                      (car where))))
11105               ;; If we fetched by Message-ID and the article came
11106               ;; from a different group, we fudge some bogus article
11107               ;; numbers for this article.
11108               (mail-header-set-number header gnus-reffed-article-number))
11109             (save-excursion
11110               (set-buffer gnus-summary-buffer)
11111               (decf gnus-reffed-article-number)
11112               (gnus-remove-header (mail-header-number header))
11113               (push header gnus-newsgroup-headers)
11114               (setq gnus-current-headers header)
11115               (push (mail-header-number header) gnus-newsgroup-limit)))
11116           header)))))
11117
11118 (defun gnus-remove-header (number)
11119   "Remove header NUMBER from `gnus-newsgroup-headers'."
11120   (if (and gnus-newsgroup-headers
11121            (= number (mail-header-number (car gnus-newsgroup-headers))))
11122       (pop gnus-newsgroup-headers)
11123     (let ((headers gnus-newsgroup-headers))
11124       (while (and (cdr headers)
11125                   (not (= number (mail-header-number (cadr headers)))))
11126         (pop headers))
11127       (when (cdr headers)
11128         (setcdr headers (cddr headers))))))
11129
11130 ;;;
11131 ;;; summary highlights
11132 ;;;
11133
11134 (defun gnus-highlight-selected-summary ()
11135   "Highlight selected article in summary buffer."
11136   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11137   (when gnus-summary-selected-face
11138     (save-excursion
11139       (let* ((beg (progn (beginning-of-line) (point)))
11140              (end (progn (end-of-line) (point)))
11141              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11142              (from (if (get-text-property beg gnus-mouse-face-prop)
11143                        beg
11144                      (or (next-single-property-change
11145                           beg gnus-mouse-face-prop nil end)
11146                          beg)))
11147              (to
11148               (if (= from end)
11149                   (- from 2)
11150                 (or (next-single-property-change
11151                      from gnus-mouse-face-prop nil end)
11152                     end))))
11153         ;; If no mouse-face prop on line we will have to = from = end,
11154         ;; so we highlight the entire line instead.
11155         (when (= (+ to 2) from)
11156           (setq from beg)
11157           (setq to end))
11158         (if gnus-newsgroup-selected-overlay
11159             ;; Move old overlay.
11160             (gnus-move-overlay
11161              gnus-newsgroup-selected-overlay from to (current-buffer))
11162           ;; Create new overlay.
11163           (gnus-overlay-put
11164            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11165            'face gnus-summary-selected-face))))))
11166
11167 (defvar gnus-summary-highlight-line-cached nil)
11168 (defvar gnus-summary-highlight-line-trigger nil)
11169
11170 (defun gnus-summary-highlight-line-0 ()
11171   (if (and (eq gnus-summary-highlight-line-trigger 
11172                gnus-summary-highlight)
11173            gnus-summary-highlight-line-cached)
11174       gnus-summary-highlight-line-cached
11175     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11176           gnus-summary-highlight-line-cached
11177           (let* ((cond (list 'cond))
11178                  (c cond)
11179                  (list gnus-summary-highlight))
11180             (while list
11181               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11182               (setq c (cdr c)
11183                     list (cdr list)))
11184             (gnus-byte-compile (list 'lambda nil cond))))))
11185
11186 (defun gnus-summary-highlight-line ()
11187   "Highlight current line according to `gnus-summary-highlight'."
11188   (let* ((beg (gnus-point-at-bol))
11189          (article (or (gnus-summary-article-number) gnus-current-article))
11190          (score (or (cdr (assq article
11191                                gnus-newsgroup-scored))
11192                     gnus-summary-default-score 0))
11193          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11194          (inhibit-read-only t)
11195          (default gnus-summary-default-score)
11196          (default-high gnus-summary-default-high-score)
11197          (default-low gnus-summary-default-low-score)
11198          (uncached (memq article gnus-newsgroup-undownloaded))
11199          (downloaded (not uncached)))
11200     (let ((face (funcall (gnus-summary-highlight-line-0))))
11201       (unless (eq face (get-text-property beg 'face))
11202         (gnus-put-text-property-excluding-characters-with-faces
11203          beg (gnus-point-at-eol) 'face
11204          (setq face (if (boundp face) (symbol-value face) face)))
11205         (when gnus-summary-highlight-line-function
11206           (funcall gnus-summary-highlight-line-function article face))))))
11207
11208 (defun gnus-update-read-articles (group unread &optional compute)
11209   "Update the list of read articles in GROUP.
11210 UNREAD is a sorted list."
11211   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11212          (entry (gnus-gethash group gnus-newsrc-hashtb))
11213          (info (nth 2 entry))
11214          (prev 1)
11215          read)
11216     (if (or (not info) (not active))
11217         ;; There is no info on this group if it was, in fact,
11218         ;; killed.  Gnus stores no information on killed groups, so
11219         ;; there's nothing to be done.
11220         ;; One could store the information somewhere temporarily,
11221         ;; perhaps...  Hmmm...
11222         ()
11223       ;; Remove any negative articles numbers.
11224       (while (and unread (< (car unread) 0))
11225         (setq unread (cdr unread)))
11226       ;; Remove any expired article numbers
11227       (while (and unread (< (car unread) (car active)))
11228         (setq unread (cdr unread)))
11229       ;; Compute the ranges of read articles by looking at the list of
11230       ;; unread articles.
11231       (while unread
11232         (when (/= (car unread) prev)
11233           (push (if (= prev (1- (car unread))) prev
11234                   (cons prev (1- (car unread))))
11235                 read))
11236         (setq prev (1+ (car unread)))
11237         (setq unread (cdr unread)))
11238       (when (<= prev (cdr active))
11239         (push (cons prev (cdr active)) read))
11240       (setq read (if (> (length read) 1) (nreverse read) read))
11241       (if compute
11242           read
11243         (save-excursion
11244           (let (setmarkundo)
11245             ;; Propagate the read marks to the backend.
11246             (when (gnus-check-backend-function 'request-set-mark group)
11247               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11248                     (add (gnus-remove-from-range read (gnus-info-read info))))
11249                 (when (or add del)
11250                   (unless (gnus-check-group group)
11251                     (error "Can't open server for %s" group))
11252                   (gnus-request-set-mark
11253                    group (delq nil (list (if add (list add 'add '(read)))
11254                                          (if del (list del 'del '(read))))))
11255                   (setq setmarkundo
11256                         `(gnus-request-set-mark
11257                           ,group
11258                           ',(delq nil (list
11259                                        (if del (list del 'add '(read)))
11260                                        (if add (list add 'del '(read))))))))))
11261             (set-buffer gnus-group-buffer)
11262             (gnus-undo-register
11263               `(progn
11264                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11265                  (gnus-info-set-read ',info ',(gnus-info-read info))
11266                  (gnus-get-unread-articles-in-group ',info
11267                                                     (gnus-active ,group))
11268                  (gnus-group-update-group ,group t)
11269                  ,setmarkundo))))
11270         ;; Enter this list into the group info.
11271         (gnus-info-set-read info read)
11272         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11273         (gnus-get-unread-articles-in-group info (gnus-active group))
11274         t))))
11275
11276 (defun gnus-offer-save-summaries ()
11277   "Offer to save all active summary buffers."
11278   (let (buffers)
11279     ;; Go through all buffers and find all summaries.
11280     (dolist (buffer (buffer-list))
11281       (when (and (setq buffer (buffer-name buffer))
11282                  (string-match "Summary" buffer)
11283                  (save-excursion
11284                    (set-buffer buffer)
11285                    ;; We check that this is, indeed, a summary buffer.
11286                    (and (eq major-mode 'gnus-summary-mode)
11287                         ;; Also make sure this isn't bogus.
11288                         gnus-newsgroup-prepared
11289                         ;; Also make sure that this isn't a
11290                         ;; dead summary buffer.
11291                         (not gnus-dead-summary-mode))))
11292         (push buffer buffers)))
11293     ;; Go through all these summary buffers and offer to save them.
11294     (when buffers
11295       (save-excursion
11296         (map-y-or-n-p
11297          "Update summary buffer %s? "
11298          (lambda (buf)
11299            (switch-to-buffer buf)
11300            (gnus-summary-exit))
11301          buffers)))))
11302
11303
11304 ;;; @ for mime-partial
11305 ;;;
11306
11307 (defun gnus-request-partial-message ()
11308   (save-excursion
11309     (let ((number (gnus-summary-article-number))
11310           (group gnus-newsgroup-name)
11311           (mother gnus-article-buffer))
11312       (set-buffer (get-buffer-create " *Partial Article*"))
11313       (erase-buffer)
11314       (setq mime-preview-buffer mother)
11315       (gnus-request-article-this-buffer number group)
11316       (mime-parse-buffer)
11317       )))
11318
11319 (autoload 'mime-combine-message/partial-pieces-automatically
11320   "mime-partial"
11321   "Internal method to combine message/partial messages automatically.")
11322
11323 (mime-add-condition
11324  'action '((type . message)(subtype . partial)
11325            (major-mode . gnus-original-article-mode)
11326            (method . mime-combine-message/partial-pieces-automatically)
11327            (summary-buffer-exp . gnus-summary-buffer)
11328            (request-partial-message-method . gnus-request-partial-message)
11329            ))
11330
11331
11332 ;;; @ for message/rfc822
11333 ;;;
11334
11335 (defun gnus-mime-extract-message/rfc822 (entity situation)
11336   "Burst a forwarded article."
11337   (save-excursion
11338     (set-buffer gnus-summary-buffer)
11339     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11340                                    gnus-newsgroup-name 'gnus-group-history))
11341            (gnus-group-marked (list group))
11342            article info)
11343       (with-temp-buffer
11344         (mime-insert-entity-content entity)
11345         (setq article (gnus-request-accept-article group)))
11346       (when (and (consp article)
11347                  (numberp (setq article (cdr article))))
11348         (setq info (gnus-get-info group))
11349         (gnus-info-set-read info
11350                             (gnus-remove-from-range (gnus-info-read info)
11351                                                     (list article)))
11352         (when (string-equal group gnus-newsgroup-name)
11353           (forward-line 1)
11354           (let (gnus-show-threads)
11355             (gnus-summary-goto-subject article t))
11356           (gnus-summary-clear-mark-forward 1))
11357         (set-buffer gnus-group-buffer)
11358         (gnus-group-get-new-news-this-group nil t)))))
11359
11360 (mime-add-condition
11361  'action '((type . message)(subtype . rfc822)
11362            (major-mode . gnus-original-article-mode)
11363            (method . gnus-mime-extract-message/rfc822)
11364            (mode . "extract")
11365            ))
11366
11367 (mime-add-condition
11368  'action '((type . message)(subtype . news)
11369            (major-mode . gnus-original-article-mode)
11370            (method . gnus-mime-extract-message/rfc822)
11371            (mode . "extract")
11372            ))
11373
11374 (defun gnus-mime-extract-multipart (entity situation)
11375   (let ((children (mime-entity-children entity))
11376         mime-acting-situation-to-override
11377         f)
11378     (while children
11379       (mime-play-entity (car children)
11380                         (cons (assq 'mode situation)
11381                               mime-acting-situation-to-override))
11382       (setq children (cdr children)))
11383     (if (setq f (cdr (assq 'after-method
11384                            mime-acting-situation-to-override)))
11385         (eval f)
11386       )))
11387
11388 (mime-add-condition
11389  'action '((type . multipart)
11390            (method . gnus-mime-extract-multipart)
11391            (mode . "extract")
11392            )
11393  'with-default)
11394
11395
11396 ;;; @ end
11397 ;;;
11398
11399 (defun gnus-summary-inherit-default-charset ()
11400   "Import `default-mime-charset' from summary buffer.
11401 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11402 of FLIM is used."
11403   (if (buffer-live-p gnus-summary-buffer)
11404       (let (d-m-c d-m-c-u)
11405         (with-current-buffer gnus-summary-buffer
11406           (setq d-m-c (if (local-variable-p 'default-mime-charset
11407                                             gnus-summary-buffer)
11408                           default-mime-charset
11409                         t)
11410                 ;; LIMIT
11411                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11412                                               gnus-summary-buffer)
11413                             (symbol-value 'default-mime-charset-unlimited)
11414                           t)))
11415         (if (eq t d-m-c)
11416             (kill-local-variable 'default-mime-charset)
11417           (set (make-local-variable 'default-mime-charset) d-m-c))
11418         (if (eq t d-m-c-u)
11419             (kill-local-variable 'default-mime-charset-unlimited)
11420           (set (make-local-variable 'default-mime-charset-unlimited)
11421                d-m-c-u)))))
11422
11423 (defun gnus-summary-setup-default-charset ()
11424   "Setup newsgroup default charset."
11425   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11426       (progn
11427         (setq gnus-newsgroup-charset nil)
11428         (set (make-local-variable 'default-mime-charset) nil)
11429         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11430           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11431     (let ((ignored-charsets
11432            (or gnus-newsgroup-ephemeral-ignored-charsets
11433                (append
11434                 (and gnus-newsgroup-name
11435                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11436                 gnus-newsgroup-ignored-charsets)))
11437           charset)
11438       (setq gnus-newsgroup-charset
11439             (or gnus-newsgroup-ephemeral-charset
11440                 (when (and gnus-newsgroup-name
11441                            (setq charset (gnus-parameter-charset
11442                                           gnus-newsgroup-name)))
11443                   (make-local-variable 'default-mime-charset)
11444                   (setq default-mime-charset charset))
11445                 gnus-default-charset))
11446       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11447            ignored-charsets))))
11448
11449 ;;;
11450 ;;; Mime Commands
11451 ;;;
11452
11453 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11454   "Display the current article buffer fully MIME-buttonized.
11455 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11456 treated as multipart/mixed."
11457   (interactive "P")
11458   (require 'gnus-art)
11459   (let ((gnus-unbuttonized-mime-types nil)
11460         (gnus-mime-display-multipart-as-mixed show-all-parts))
11461     (gnus-summary-show-article)))
11462
11463 (defun gnus-summary-repair-multipart (article)
11464   "Add a Content-Type header to a multipart article without one."
11465   (interactive (list (gnus-summary-article-number)))
11466   (gnus-with-article article
11467     (message-narrow-to-head)
11468     (message-remove-header "Mime-Version")
11469     (goto-char (point-max))
11470     (insert "Mime-Version: 1.0\n")
11471     (widen)
11472     (when (search-forward "\n--" nil t)
11473       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11474         (message-narrow-to-head)
11475         (message-remove-header "Content-Type")
11476         (goto-char (point-max))
11477         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11478                         separator))
11479         (widen))))
11480   (let (gnus-mark-article-hook)
11481     (gnus-summary-select-article t t nil article)))
11482
11483 (defun gnus-summary-toggle-display-buttonized ()
11484   "Toggle the buttonizing of the article buffer."
11485   (interactive)
11486   (require 'gnus-art)
11487   (if (setq gnus-inhibit-mime-unbuttonizing
11488             (not gnus-inhibit-mime-unbuttonizing))
11489       (let ((gnus-unbuttonized-mime-types nil))
11490         (gnus-summary-show-article))
11491     (gnus-summary-show-article)))
11492
11493 ;;;
11494 ;;; Intelli-mouse commmands
11495 ;;;
11496
11497 (defun gnus-wheel-summary-scroll (event)
11498   (interactive "e")
11499   (let ((amount (if (memq 'shift (event-modifiers event))
11500                     (car gnus-wheel-scroll-amount)
11501                   (cdr gnus-wheel-scroll-amount)))
11502         (direction (- (* (static-if (featurep 'xemacs)
11503                              (event-button event)
11504                            (cond ((eq 'mouse-4 (event-basic-type event))
11505                                   4)
11506                                  ((eq 'mouse-5 (event-basic-type event))
11507                                   5)))
11508                          2) 9))
11509         edge)
11510     (gnus-summary-scroll-up (* amount direction))
11511     (when (gnus-eval-in-buffer-window gnus-article-buffer
11512             (save-restriction
11513               (widen)
11514               (and (if (< 0 direction)
11515                        (gnus-article-next-page 0)
11516                      (gnus-article-prev-page 0)
11517                      (bobp))
11518                    (if (setq edge (get-text-property
11519                                    (point-min) 'gnus-wheel-edge))
11520                        (setq edge (* edge direction))
11521                      (setq edge -1))
11522                    (or (plusp edge)
11523                        (let ((buffer-read-only nil)
11524                              (inhibit-read-only t))
11525                          (put-text-property (point-min) (point-max)
11526                                             'gnus-wheel-edge direction)
11527                          nil))
11528                    (or (> edge gnus-wheel-edge-resistance)
11529                        (let ((buffer-read-only nil)
11530                              (inhibit-read-only t))
11531                          (put-text-property (point-min) (point-max)
11532                                             'gnus-wheel-edge
11533                                             (* (1+ edge) direction))
11534                          nil))
11535                    (eq last-command 'gnus-wheel-summary-scroll))))
11536       (gnus-summary-next-article nil nil (minusp direction)))))
11537
11538 (defun gnus-wheel-install ()
11539   "Enable mouse wheel support on summary window."
11540   (when gnus-use-wheel
11541     (let ((keys
11542            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11543       (dolist (key keys)
11544         (define-key gnus-summary-mode-map key
11545           'gnus-wheel-summary-scroll)))))
11546
11547 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11548
11549 ;;;
11550 ;;; Traditional PGP commmands
11551 ;;;
11552
11553 (defun gnus-summary-decrypt-article (&optional force)
11554   "Decrypt the current article in traditional PGP way.
11555 This will have permanent effect only in mail groups.
11556 If FORCE is non-nil, allow editing of articles even in read-only
11557 groups."
11558   (interactive "P")
11559   (gnus-summary-select-article t)
11560   (gnus-eval-in-buffer-window gnus-article-buffer
11561     (save-excursion
11562       (save-restriction
11563         (widen)
11564         (goto-char (point-min))
11565         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11566           (error "Not a traditional PGP message!"))
11567         (let ((armor-start (match-beginning 0)))
11568           (if (and (pgg-decrypt-region armor-start (point-max))
11569                    (or force (not (gnus-group-read-only-p))))
11570               (let ((inhibit-read-only t)
11571                     buffer-read-only)
11572                 (delete-region armor-start
11573                                (progn
11574                                  (re-search-forward "^-+END PGP" nil t)
11575                                  (beginning-of-line 2)
11576                                  (point)))
11577                 (insert-buffer-substring pgg-output-buffer))))))))
11578
11579 (defun gnus-summary-verify-article ()
11580   "Verify the current article in traditional PGP way."
11581   (interactive)
11582   (save-excursion
11583     (set-buffer gnus-original-article-buffer)
11584     (goto-char (point-min))
11585     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11586       (error "Not a traditional PGP message!"))
11587     (re-search-forward "^-+END PGP" nil t)
11588     (beginning-of-line 2)
11589     (call-interactively (function pgg-verify-region))))
11590
11591 ;;;
11592 ;;; Generic summary marking commands
11593 ;;;
11594
11595 (defvar gnus-summary-marking-alist
11596   '((read gnus-del-mark "d")
11597     (unread gnus-unread-mark "u")
11598     (ticked gnus-ticked-mark "!")
11599     (dormant gnus-dormant-mark "?")
11600     (expirable gnus-expirable-mark "e"))
11601   "An alist of names/marks/keystrokes.")
11602
11603 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11604 (defvar gnus-summary-mark-map)
11605
11606 (defun gnus-summary-make-all-marking-commands ()
11607   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11608   (dolist (elem gnus-summary-marking-alist)
11609     (apply 'gnus-summary-make-marking-command elem)))
11610
11611 (defun gnus-summary-make-marking-command (name mark keystroke)
11612   (let ((map (make-sparse-keymap)))
11613     (define-key gnus-summary-generic-mark-map keystroke map)
11614     (dolist (lway `((next "next" next nil "n")
11615                     (next-unread "next unread" next t "N")
11616                     (prev "previous" prev nil "p")
11617                     (prev-unread "previous unread" prev t "P")
11618                     (nomove "" nil nil ,keystroke)))
11619       (let ((func (gnus-summary-make-marking-command-1
11620                    mark (car lway) lway name)))
11621         (setq func (eval func))
11622         (define-key map (nth 4 lway) func)))))
11623
11624 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11625   `(defun ,(intern
11626             (format "gnus-summary-put-mark-as-%s%s"
11627                     name (if (eq way 'nomove)
11628                              ""
11629                            (concat "-" (symbol-name way)))))
11630      (n)
11631      ,(format
11632        "Mark the current article as %s%s.
11633 If N, the prefix, then repeat N times.
11634 If N is negative, move in reverse order.
11635 The difference between N and the actual number of articles marked is
11636 returned."
11637        name (car (cdr lway)))
11638      (interactive "p")
11639      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11640
11641 (defun gnus-summary-generic-mark (n mark move unread)
11642   "Mark N articles with MARK."
11643   (unless (eq major-mode 'gnus-summary-mode)
11644     (error "This command can only be used in the summary buffer"))
11645   (gnus-summary-show-thread)
11646   (let ((nummove
11647          (cond
11648           ((eq move 'next) 1)
11649           ((eq move 'prev) -1)
11650           (t 0))))
11651     (if (zerop nummove)
11652         (setq n 1)
11653       (when (< n 0)
11654         (setq n (abs n)
11655               nummove (* -1 nummove))))
11656     (while (and (> n 0)
11657                 (gnus-summary-mark-article nil mark)
11658                 (zerop (gnus-summary-next-subject nummove unread t)))
11659       (setq n (1- n)))
11660     (when (/= 0 n)
11661       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11662     (gnus-summary-recenter)
11663     (gnus-summary-position-point)
11664     (gnus-set-mode-line 'summary)
11665     n))
11666
11667 (defun gnus-summary-insert-articles (articles)
11668   (when (setq articles
11669               (gnus-sorted-difference articles
11670                                       (mapcar (lambda (h)
11671                                                 (mail-header-number h))
11672                                               gnus-newsgroup-headers)))
11673     (setq gnus-newsgroup-headers
11674           (merge 'list
11675                  gnus-newsgroup-headers
11676                  (gnus-fetch-headers articles)
11677                  'gnus-article-sort-by-number))
11678     ;; Suppress duplicates?
11679     (when gnus-suppress-duplicates
11680       (gnus-dup-suppress-articles))
11681
11682     ;; We might want to build some more threads first.
11683     (when (and gnus-fetch-old-headers
11684                (eq gnus-headers-retrieved-by 'nov))
11685       (if (eq gnus-fetch-old-headers 'invisible)
11686           (gnus-build-all-threads)
11687         (gnus-build-old-threads)))
11688     ;; Let the Gnus agent mark articles as read.
11689     (when gnus-agent
11690       (gnus-agent-get-undownloaded-list))
11691     ;; Remove list identifiers from subject
11692     (when gnus-list-identifiers
11693       (gnus-summary-remove-list-identifiers))
11694     ;; First and last article in this newsgroup.
11695     (when gnus-newsgroup-headers
11696       (setq gnus-newsgroup-begin
11697             (mail-header-number (car gnus-newsgroup-headers))
11698             gnus-newsgroup-end
11699             (mail-header-number
11700              (gnus-last-element gnus-newsgroup-headers))))
11701     (when gnus-use-scoring
11702       (gnus-possibly-score-headers))))
11703
11704 (defun gnus-summary-insert-old-articles (&optional all)
11705   "Insert all old articles in this group.
11706 If ALL is non-nil, already read articles become readable.
11707 If ALL is a number, fetch this number of articles."
11708   (interactive "P")
11709   (prog1
11710       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11711             older len)
11712         (setq older
11713               ;; Some nntp servers lie about their active range.  When
11714               ;; this happens, the active range can be in the millions.
11715               ;; Use a compressed range to avoid creating a huge list.
11716               (gnus-range-difference (list gnus-newsgroup-active) old))
11717         (setq len (gnus-range-length older))
11718         (cond
11719          ((null older) nil)
11720          ((numberp all)
11721           (if (< all len)
11722               (let ((older-range (nreverse older)))
11723                 (setq older nil)
11724
11725                 (while (> all 0)
11726                   (let* ((r (pop older-range))
11727                          (min (if (numberp r) r (car r)))
11728                          (max (if (numberp r) r (cdr r))))
11729                     (while (and (<= min max)
11730                                 (> all 0))
11731                       (push max older)
11732                       (setq all (1- all)
11733                             max (1- max))))))
11734             (setq older (gnus-uncompress-range older))))
11735          (all
11736           (setq older (gnus-uncompress-range older)))
11737          (t
11738           (when (and (numberp gnus-large-newsgroup)
11739                    (> len gnus-large-newsgroup))
11740               (let* ((cursor-in-echo-area nil)
11741                      (initial (gnus-parameter-large-newsgroup-initial
11742                                gnus-newsgroup-name))
11743                      (input
11744                       (read-string
11745                        (format
11746                         "How many articles from %s (%s %d): "
11747                         (gnus-limit-string
11748                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11749                         (if initial "max" "default")
11750                         len)
11751                        (if initial
11752                            (cons (number-to-string initial)
11753                                  0)))))
11754                 (unless (string-match "^[ \t]*$" input)
11755                   (setq all (string-to-number input))
11756                   (if (< all len)
11757                       (let ((older-range (nreverse older)))
11758                         (setq older nil)
11759
11760                         (while (> all 0)
11761                           (let* ((r (pop older-range))
11762                                  (min (if (numberp r) r (car r)))
11763                                  (max (if (numberp r) r (cdr r))))
11764                             (while (and (<= min max)
11765                                         (> all 0))
11766                               (push max older)
11767                               (setq all (1- all)
11768                                     max (1- max))))))))))
11769           (setq older (gnus-uncompress-range older))))
11770         (if (not older)
11771             (message "No old news.")
11772           (gnus-summary-insert-articles older)
11773           (gnus-summary-limit (gnus-sorted-nunion old older))))
11774     (gnus-summary-position-point)))
11775
11776 (defun gnus-summary-insert-new-articles ()
11777   "Insert all new articles in this group."
11778   (interactive)
11779   (prog1
11780       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11781             (old-active gnus-newsgroup-active)
11782             (nnmail-fetched-sources (list t))
11783             i new)
11784         (setq gnus-newsgroup-active
11785               (gnus-activate-group gnus-newsgroup-name 'scan))
11786         (setq i (cdr gnus-newsgroup-active))
11787         (while (> i (cdr old-active))
11788           (push i new)
11789           (decf i))
11790         (if (not new)
11791             (message "No gnus is bad news.")
11792           (gnus-summary-insert-articles new)
11793           (setq gnus-newsgroup-unreads
11794                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11795           (gnus-summary-limit (gnus-sorted-nunion old new))))
11796     (gnus-summary-position-point)))
11797
11798 (gnus-summary-make-all-marking-commands)
11799
11800 (gnus-ems-redefine)
11801
11802 (provide 'gnus-sum)
11803
11804 (run-hooks 'gnus-sum-load-hook)
11805
11806 ;;; gnus-sum.el ends here