Synch with Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
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
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include: `gnus-simplify-subject-re',
145 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
146   :group 'gnus-thread
147   :type '(repeat function))
148
149 (defcustom gnus-simplify-ignored-prefixes nil
150   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  regexp))
154
155 (defcustom gnus-build-sparse-threads nil
156   "*If non-nil, fill in the gaps in threads.
157 If `some', only fill in the gaps that are needed to tie loose threads
158 together.  If `more', fill in all leaf nodes that Gnus can find.  If
159 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
160   :group 'gnus-thread
161   :type '(choice (const :tag "off" nil)
162                  (const some)
163                  (const more)
164                  (sexp :menu-tag "all" t)))
165
166 (defcustom gnus-summary-thread-gathering-function
167   'gnus-gather-threads-by-subject
168   "*Function used for gathering loose threads.
169 There are two pre-defined functions: `gnus-gather-threads-by-subject',
170 which only takes Subjects into consideration; and
171 `gnus-gather-threads-by-references', which compared the References
172 headers of the articles to find matches."
173   :group 'gnus-thread
174   :type '(radio (function-item gnus-gather-threads-by-subject)
175                 (function-item gnus-gather-threads-by-references)
176                 (function :tag "other")))
177
178 (defcustom gnus-summary-same-subject ""
179   "*String indicating that the current article has the same subject as the previous.
180 This variable will only be used if the value of
181 `gnus-summary-make-false-root' is `empty'."
182   :group 'gnus-summary-format
183   :type 'string)
184
185 (defcustom gnus-summary-goto-unread t
186   "*If t, many commands will go to the next unread article.
187 This applies to marking commands as well as other commands that
188 \"naturally\" select the next article, like, for instance, `SPC' at
189 the end of an article.
190
191 If nil, the marking commands do NOT go to the next unread article
192 (they go to the next article instead).  If `never', commands that
193 usually go to the next unread article, will go to the next article,
194 whether it is read or not."
195   :group 'gnus-summary-marks
196   :link '(custom-manual "(gnus)Setting Marks")
197   :type '(choice (const :tag "off" nil)
198                  (const never)
199                  (sexp :menu-tag "on" t)))
200
201 (defcustom gnus-summary-default-score 0
202   "*Default article score level.
203 All scores generated by the score files will be added to this score.
204 If this variable is nil, scoring will be disabled."
205   :group 'gnus-score-default
206   :type '(choice (const :tag "disable")
207                  integer))
208
209 (defcustom gnus-summary-zcore-fuzz 0
210   "*Fuzziness factor for the zcore in the summary buffer.
211 Articles with scores closer than this to `gnus-summary-default-score'
212 will not be marked."
213   :group 'gnus-summary-format
214   :type 'integer)
215
216 (defcustom gnus-simplify-subject-fuzzy-regexp nil
217   "*Strings to be removed when doing fuzzy matches.
218 This can either be a regular expression or list of regular expressions
219 that will be removed from subject strings if fuzzy subject
220 simplification is selected."
221   :group 'gnus-thread
222   :type '(repeat regexp))
223
224 (defcustom gnus-show-threads t
225   "*If non-nil, display threads in summary mode."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-hide-subtree nil
230   "*If non-nil, hide all threads initially.
231 If threads are hidden, you have to run the command
232 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
233 to expose hidden threads."
234   :group 'gnus-thread
235   :type 'boolean)
236
237 (defcustom gnus-thread-hide-killed t
238   "*If non-nil, hide killed threads automatically."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-ignore-subject t
243   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
244 If nil, articles that have different subjects from their parents will
245 start separate threads."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-operation-ignore-subject t
250   "*If non-nil, subjects will be ignored when doing thread commands.
251 This affects commands like `gnus-summary-kill-thread' and
252 `gnus-summary-lower-thread'.
253
254 If this variable is nil, articles in the same thread with different
255 subjects will not be included in the operation in question.  If this
256 variable is `fuzzy', only articles that have subjects that are fuzzily
257 equal will be included."
258   :group 'gnus-thread
259   :type '(choice (const :tag "off" nil)
260                  (const fuzzy)
261                  (sexp :tag "on" t)))
262
263 (defcustom gnus-thread-indent-level 4
264   "*Number that says how much each sub-thread should be indented."
265   :group 'gnus-thread
266   :type 'integer)
267
268 (defcustom gnus-auto-extend-newsgroup t
269   "*If non-nil, extend newsgroup forward and backward when requested."
270   :group 'gnus-summary-choose
271   :type 'boolean)
272
273 (defcustom gnus-auto-select-first t
274   "*If nil, don't select the first unread article when entering a group.
275 If this variable is `best', select the highest-scored unread article
276 in the group.  If t, select the first unread article.
277
278 This variable can also be a function to place point on a likely
279 subject line.  Useful values include `gnus-summary-first-unread-subject',
280 `gnus-summary-first-unread-article' and
281 `gnus-summary-best-unread-article'.
282
283 If you want to prevent automatic selection of the first unread article
284 in some newsgroups, set the variable to nil in
285 `gnus-select-group-hook'."
286   :group 'gnus-group-select
287   :type '(choice (const :tag "none" nil)
288                  (const best)
289                  (sexp :menu-tag "first" t)
290                  (function-item gnus-summary-first-unread-subject)
291                  (function-item gnus-summary-first-unread-article)
292                  (function-item gnus-summary-best-unread-article)))
293
294 (defcustom gnus-dont-select-after-jump-to-other-group nil
295   "If non-nil, don't select the first unread article after entering the
296 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
297 it is depend on the value of `gnus-auto-select-first' whether to select
298 or not."
299   :group 'gnus-group-select
300   :type 'boolean)
301
302 (defcustom gnus-auto-select-next t
303   "*If non-nil, offer to go to the next group from the end of the previous.
304 If the value is t and the next newsgroup is empty, Gnus will exit
305 summary mode and go back to group mode.  If the value is neither nil
306 nor t, Gnus will select the following unread newsgroup.  In
307 particular, if the value is the symbol `quietly', the next unread
308 newsgroup will be selected without any confirmation, and if it is
309 `almost-quietly', the next group will be selected without any
310 confirmation if you are located on the last article in the group.
311 Finally, if this variable is `slightly-quietly', the `Z n' command
312 will go to the next group without confirmation."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "off" nil)
315                  (const quietly)
316                  (const almost-quietly)
317                  (const slightly-quietly)
318                  (sexp :menu-tag "on" t)))
319
320 (defcustom gnus-auto-select-same nil
321   "*If non-nil, select the next article with the same subject.
322 If there are no more articles with the same subject, go to
323 the first unread article."
324   :group 'gnus-summary-maneuvering
325   :type 'boolean)
326
327 (defcustom gnus-summary-check-current nil
328   "*If non-nil, consider the current article when moving.
329 The \"unread\" movement commands will stay on the same line if the
330 current article is unread."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-auto-center-summary t
335   "*If non-nil, always center the current summary buffer.
336 In particular, if `vertical' do only vertical recentering.  If non-nil
337 and non-`vertical', do both horizontal and vertical recentering."
338   :group 'gnus-summary-maneuvering
339   :type '(choice (const :tag "none" nil)
340                  (const vertical)
341                  (integer :tag "height")
342                  (sexp :menu-tag "both" t)))
343
344 (defcustom gnus-show-all-headers nil
345   "*If non-nil, don't hide any headers."
346   :group 'gnus-article-hiding
347   :group 'gnus-article-headers
348   :type 'boolean)
349
350 (defcustom gnus-summary-ignore-duplicates nil
351   "*If non-nil, ignore articles with identical Message-ID headers."
352   :group 'gnus-summary
353   :type 'boolean)
354
355 (defcustom gnus-single-article-buffer t
356   "*If non-nil, display all articles in the same buffer.
357 If nil, each group will get its own article buffer."
358   :group 'gnus-article-various
359   :type 'boolean)
360
361 (defcustom gnus-break-pages t
362   "*If non-nil, do page breaking on articles.
363 The page delimiter is specified by the `gnus-page-delimiter'
364 variable."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-show-mime t
369   "*If non-nil, do mime processing of articles.
370 The articles will simply be fed to the function given by
371 `gnus-article-display-method-for-mime'."
372   :group 'gnus-article-mime
373   :type 'boolean)
374
375 (defcustom gnus-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable."
378   :group 'gnus-summary-mail
379   :type '(repeat (choice (list :value (fun) function)
380                          (cons :value ("" "") regexp (repeat string))
381                          (sexp :value nil))))
382
383 (defcustom gnus-unread-mark ?  ;Whitespace
384   "*Mark used for unread articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-ticked-mark ?!
389   "*Mark used for ticked articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-dormant-mark ??
394   "*Mark used for dormant articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-del-mark ?r
399   "*Mark used for del'd articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-read-mark ?R
404   "*Mark used for read articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-expirable-mark ?E
409   "*Mark used for expirable articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-killed-mark ?K
414   "*Mark used for killed articles."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-souped-mark ?F
419   "*Mark used for killed articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-kill-file-mark ?X
424   "*Mark used for articles killed by kill files."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-low-score-mark ?Y
429   "*Mark used for articles with a low score."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-catchup-mark ?C
434   "*Mark used for articles that are caught up."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-replied-mark ?A
439   "*Mark used for articles that have been replied to."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-cached-mark ?*
444   "*Mark used for articles that are in the cache."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-saved-mark ?S
449   "*Mark used for articles that have been saved to."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-ancient-mark ?O
454   "*Mark used for ancient articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-sparse-mark ?Q
459   "*Mark used for sparsely reffed articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-canceled-mark ?G
464   "*Mark used for canceled articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-duplicate-mark ?M
469   "*Mark used for duplicate articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-undownloaded-mark ?@
474   "*Mark used for articles that weren't downloaded."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-downloadable-mark ?%
479   "*Mark used for articles that are to be downloaded."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-unsendable-mark ?=
484   "*Mark used for articles that won't be sent."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-score-over-mark ?+
489   "*Score mark used for articles with high scores."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-score-below-mark ?-
494   "*Score mark used for articles with low scores."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-empty-thread-mark ?  ;Whitespace
499   "*There is no thread under the article."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-not-empty-thread-mark ?=
504   "*There is a thread under the article."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-view-pseudo-asynchronously nil
509   "*If non-nil, Gnus will view pseudo-articles asynchronously."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-auto-expirable-marks
514   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
515         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
516         gnus-souped-mark gnus-duplicate-mark)
517   "*The list of marks converted into expiration if a group is auto-expirable."
518   :group 'gnus-summary
519   :type '(repeat character))
520
521 (defcustom gnus-inhibit-user-auto-expire t
522   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
523   :group 'gnus-summary
524   :type 'boolean)
525
526 (defcustom gnus-view-pseudos nil
527   "*If `automatic', pseudo-articles will be viewed automatically.
528 If `not-confirm', pseudos will be viewed automatically, and the user
529 will not be asked to confirm the command."
530   :group 'gnus-extract-view
531   :type '(choice (const :tag "off" nil)
532                  (const automatic)
533                  (const not-confirm)))
534
535 (defcustom gnus-view-pseudos-separately t
536   "*If non-nil, one pseudo-article will be created for each file to be viewed.
537 If nil, all files that use the same viewing command will be given as a
538 list of parameters to that command."
539   :group 'gnus-extract-view
540   :type 'boolean)
541
542 (defcustom gnus-insert-pseudo-articles t
543   "*If non-nil, insert pseudo-articles when decoding articles."
544   :group 'gnus-extract-view
545   :type 'boolean)
546
547 (defcustom gnus-summary-dummy-line-format
548   "  %(:                          :%) %S\n"
549   "*The format specification for the dummy roots in the summary buffer.
550 It works along the same lines as a normal formatting string,
551 with some simple extensions.
552
553 %S  The subject"
554   :group 'gnus-threading
555   :type 'string)
556
557 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
558   "*The format specification for the summary mode line.
559 It works along the same lines as a normal formatting string,
560 with some simple extensions:
561
562 %G  Group name
563 %p  Unprefixed group name
564 %A  Current article number
565 %z  Current article score
566 %V  Gnus version
567 %U  Number of unread articles in the group
568 %e  Number of unselected articles in the group
569 %Z  A string with unread/unselected article counts
570 %g  Shortish group name
571 %S  Subject of the current article
572 %u  User-defined spec
573 %s  Current score file name
574 %d  Number of dormant articles
575 %r  Number of articles that have been marked as read in this session
576 %E  Number of articles expunged by the score files"
577   :group 'gnus-summary-format
578   :type 'string)
579
580 (defcustom gnus-list-identifiers nil
581   "Regexp that matches list identifiers to be removed from subject.
582 This can also be a list of regexps."
583   :group 'gnus-summary-format
584   :group 'gnus-article-hiding
585   :type '(choice (const :tag "none" nil)
586                  (regexp :value ".*")
587                  (repeat :value (".*") regexp)))
588
589 (defcustom gnus-summary-mark-below 0
590   "*Mark all articles with a score below this variable as read.
591 This variable is local to each summary buffer and usually set by the
592 score file."
593   :group 'gnus-score-default
594   :type 'integer)
595
596 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
597   "*List of functions used for sorting articles in the summary buffer.
598 This variable is only used when not using a threaded display."
599   :group 'gnus-summary-sort
600   :type '(repeat (choice (function-item gnus-article-sort-by-number)
601                          (function-item gnus-article-sort-by-author)
602                          (function-item gnus-article-sort-by-subject)
603                          (function-item gnus-article-sort-by-date)
604                          (function-item gnus-article-sort-by-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
608   "*List of functions used for sorting threads in the summary buffer.
609 By default, threads are sorted by article number.
610
611 Each function takes two threads and return non-nil if the first thread
612 should be sorted before the other.  If you use more than one function,
613 the primary sort function should be the last.  You should probably
614 always include `gnus-thread-sort-by-number' in the list of sorting
615 functions -- preferably first.
616
617 Ready-made functions include `gnus-thread-sort-by-number',
618 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
619 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
620 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
621   :group 'gnus-summary-sort
622   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
623                          (function-item gnus-thread-sort-by-author)
624                          (function-item gnus-thread-sort-by-subject)
625                          (function-item gnus-thread-sort-by-date)
626                          (function-item gnus-thread-sort-by-score)
627                          (function-item gnus-thread-sort-by-total-score)
628                          (function :tag "other"))))
629
630 (defcustom gnus-thread-score-function '+
631   "*Function used for calculating the total score of a thread.
632
633 The function is called with the scores of the article and each
634 subthread and should then return the score of the thread.
635
636 Some functions you can use are `+', `max', or `min'."
637   :group 'gnus-summary-sort
638   :type 'function)
639
640 (defcustom gnus-summary-expunge-below nil
641   "All articles that have a score less than this variable will be expunged.
642 This variable is local to the summary buffers."
643   :group 'gnus-score-default
644   :type '(choice (const :tag "off" nil)
645                  integer))
646
647 (defcustom gnus-thread-expunge-below nil
648   "All threads that have a total score less than this variable will be expunged.
649 See `gnus-thread-score-function' for en explanation of what a
650 \"thread score\" is.
651
652 This variable is local to the summary buffers."
653   :group 'gnus-threading
654   :group 'gnus-score-default
655   :type '(choice (const :tag "off" nil)
656                  integer))
657
658 (defcustom gnus-summary-mode-hook nil
659   "*A hook for Gnus summary mode.
660 This hook is run before any variables are set in the summary buffer."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-summary-menu-hook nil
665   "*Hook run after the creation of the summary mode menu."
666   :group 'gnus-summary-visual
667   :type 'hook)
668
669 (defcustom gnus-summary-exit-hook nil
670   "*A hook called on exit from the summary buffer.
671 It will be called with point in the group buffer."
672   :group 'gnus-summary-exit
673   :type 'hook)
674
675 (defcustom gnus-summary-prepare-hook nil
676   "*A hook called after the summary buffer has been generated.
677 If you want to modify the summary buffer, you can use this hook."
678   :group 'gnus-summary-various
679   :type 'hook)
680
681 (defcustom gnus-summary-prepared-hook nil
682   "*A hook called as the last thing after the summary buffer has been generated."
683   :group 'gnus-summary-various
684   :type 'hook)
685
686 (defcustom gnus-summary-generate-hook nil
687   "*A hook run just before generating the summary buffer.
688 This hook is commonly used to customize threading variables and the
689 like."
690   :group 'gnus-summary-various
691   :type 'hook)
692
693 (defcustom gnus-select-group-hook nil
694   "*A hook called when a newsgroup is selected.
695
696 If you'd like to simplify subjects like the
697 `gnus-summary-next-same-subject' command does, you can use the
698 following hook:
699
700  (setq gnus-select-group-hook
701       (list
702         (lambda ()
703           (mapcar (lambda (header)
704                      (mail-header-set-subject
705                       header
706                       (gnus-simplify-subject
707                        (mail-header-subject header) 're-only)))
708                   gnus-newsgroup-headers))))"
709   :group 'gnus-group-select
710   :type 'hook)
711
712 (defcustom gnus-select-article-hook nil
713   "*A hook called when an article is selected."
714   :group 'gnus-summary-choose
715   :type 'hook)
716
717 (defcustom gnus-visual-mark-article-hook
718   (list 'gnus-highlight-selected-summary)
719   "*Hook run after selecting an article in the summary buffer.
720 It is meant to be used for highlighting the article in some way.  It
721 is not run if `gnus-visual' is nil."
722   :group 'gnus-summary-visual
723   :type 'hook)
724
725 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
726   "*A hook called before parsing the headers."
727   :group 'gnus-various
728   :type 'hook)
729
730 (defcustom gnus-exit-group-hook nil
731   "*A hook called when exiting summary mode.
732 This hook is not called from the non-updating exit commands like `Q'."
733   :group 'gnus-various
734   :type 'hook)
735
736 (defcustom gnus-summary-update-hook
737   (list 'gnus-summary-highlight-line)
738   "*A hook called when a summary line is changed.
739 The hook will not be called if `gnus-visual' is nil.
740
741 The default function `gnus-summary-highlight-line' will
742 highlight the line according to the `gnus-summary-highlight'
743 variable."
744   :group 'gnus-summary-visual
745   :type 'hook)
746
747 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
748   "*A hook called when an article is selected for the first time.
749 The hook is intended to mark an article as read (or unread)
750 automatically when it is selected."
751   :group 'gnus-summary-choose
752   :type 'hook)
753
754 (defcustom gnus-group-no-more-groups-hook nil
755   "*A hook run when returning to group mode having no more (unread) groups."
756   :group 'gnus-group-select
757   :type 'hook)
758
759 (defcustom gnus-ps-print-hook nil
760   "*A hook run before ps-printing something from Gnus."
761   :group 'gnus-summary
762   :type 'hook)
763
764 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
765   "Face used for highlighting the current article in the summary buffer."
766   :group 'gnus-summary-visual
767   :type 'face)
768
769 (defcustom gnus-summary-highlight
770   '(((= mark gnus-canceled-mark)
771      . gnus-summary-cancelled-face)
772     ((and (> score default)
773           (or (= mark gnus-dormant-mark)
774               (= mark gnus-ticked-mark)))
775      . gnus-summary-high-ticked-face)
776     ((and (< score default)
777           (or (= mark gnus-dormant-mark)
778               (= mark gnus-ticked-mark)))
779      . gnus-summary-low-ticked-face)
780     ((or (= mark gnus-dormant-mark)
781          (= mark gnus-ticked-mark))
782      . gnus-summary-normal-ticked-face)
783     ((and (> score default) (= mark gnus-ancient-mark))
784      . gnus-summary-high-ancient-face)
785     ((and (< score default) (= mark gnus-ancient-mark))
786      . gnus-summary-low-ancient-face)
787     ((= mark gnus-ancient-mark)
788      . gnus-summary-normal-ancient-face)
789     ((and (> score default) (= mark gnus-unread-mark))
790      . gnus-summary-high-unread-face)
791     ((and (< score default) (= mark gnus-unread-mark))
792      . gnus-summary-low-unread-face)
793     ((and (memq article gnus-newsgroup-incorporated)
794           (= mark gnus-unread-mark))
795      . gnus-summary-incorporated-face)
796     ((= mark gnus-unread-mark)
797      . gnus-summary-normal-unread-face)
798     ((and (> score default) (memq mark (list gnus-downloadable-mark
799                                              gnus-undownloaded-mark)))
800      . gnus-summary-high-unread-face)
801     ((and (< score default) (memq mark (list gnus-downloadable-mark
802                                              gnus-undownloaded-mark)))
803      . gnus-summary-low-unread-face)
804     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
805      . gnus-summary-normal-unread-face)
806     ((> score default)
807      . gnus-summary-high-read-face)
808     ((< score default)
809      . gnus-summary-low-read-face)
810     (t
811      . gnus-summary-normal-read-face))
812   "*Controls the highlighting of summary buffer lines.
813
814 A list of (FORM . FACE) pairs.  When deciding how a a particular
815 summary line should be displayed, each form is evaluated.  The content
816 of the face field after the first true form is used.  You can change
817 how those summary lines are displayed, by editing the face field.
818
819 You can use the following variables in the FORM field.
820
821 score:   The articles score
822 default: The default article score.
823 below:   The score below which articles are automatically marked as read.
824 mark:    The articles mark."
825   :group 'gnus-summary-visual
826   :type '(repeat (cons (sexp :tag "Form" nil)
827                        face)))
828
829 (defcustom gnus-alter-header-function nil
830   "Function called to allow alteration of article header structures.
831 The function is called with one parameter, the article header vector,
832 which it may alter in any way.")
833
834 (defvar gnus-decode-encoded-word-function
835   (mime-find-field-decoder 'From 'nov)
836   "Variable that says which function should be used to decode a string with encoded words.")
837
838 (defcustom gnus-extra-headers nil
839   "*Extra headers to parse."
840   :group 'gnus-summary
841   :type '(repeat symbol))
842
843 (defcustom gnus-ignored-from-addresses
844   (and user-mail-address (regexp-quote user-mail-address))
845   "*Regexp of From headers that may be suppressed in favor of To headers."
846   :group 'gnus-summary
847   :type 'regexp)
848
849 (defcustom gnus-group-charset-alist
850   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
851     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
852     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
853     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
854     ("^relcom\\>" koi8-r)
855     ("^fido7\\>" koi8-r)
856     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
857     ("^israel\\>" iso-8859-1)
858     ("^han\\>" euc-kr)
859     ("^alt.chinese.text.big5\\>" chinese-big5)
860     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
861     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
862     (".*" iso-8859-1))
863   "Alist of regexps (to match group names) and default charsets to be used when reading."
864   :type '(repeat (list (regexp :tag "Group")
865                        (symbol :tag "Charset")))
866   :group 'gnus-charset)
867
868 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
869   "List of charsets that should be ignored.
870 When these charsets are used in the \"charset\" parameter, the
871 default charset will be used instead."
872   :type '(repeat symbol)
873   :group 'gnus-charset)
874
875 (defcustom gnus-group-ignored-charsets-alist
876   '(("alt\\.chinese\\.text" iso-8859-1))
877   "Alist of regexps (to match group names) and charsets that should be ignored.
878 When these charsets are used in the \"charset\" parameter, the
879 default charset will be used instead."
880   :type '(repeat (cons (regexp :tag "Group")
881                        (repeat symbol)))
882   :group 'gnus-charset)
883
884 (defcustom gnus-group-highlight-words-alist nil
885   "Alist of group regexps and highlight regexps.
886 This variable uses the same syntax as `gnus-emphasis-alist'."
887   :type '(repeat (cons (regexp :tag "Group")
888                        (repeat (list (regexp :tag "Highlight regexp")
889                                      (number :tag "Group for entire word" 0)
890                                      (number :tag "Group for displayed part" 0)
891                                      (symbol :tag "Face"
892                                              gnus-emphasis-highlight-words)))))
893   :group 'gnus-summary-visual)
894
895 (defcustom gnus-use-wheel nil
896   "Use Intelli-mouse on summary movement"
897   :type 'boolean
898   :group 'gnus-summary-maneuvering)
899
900 (defcustom gnus-wheel-scroll-amount '(5 . 1)
901   "Amount to scroll messages by spinning the mouse wheel.
902 This is actually a cons cell, where the first item is the amount to scroll
903 on a normal wheel event, and the second is the amount to scroll when the
904 wheel is moved with the shift key depressed."
905   :type '(cons (integer :tag "Shift") integer)
906   :group 'gnus-summary-maneuvering)
907
908 (defcustom gnus-wheel-edge-resistance 2
909   "How hard it should be to change the current article
910 by moving the mouse over the edge of the article window."
911   :type 'integer
912   :group 'gnus-summary-maneuvering)
913
914 (defcustom gnus-summary-show-article-charset-alist
915   nil
916   "Alist of number and charset.
917 The article will be shown with the charset corresponding to the
918 numbered argument.
919 For example: ((1 . cn-gb-2312) (2 . big5))."
920   :type '(repeat (cons (number :tag "Argument" 1)
921                        (symbol :tag "Charset")))
922   :group 'gnus-charset)
923
924 (defcustom gnus-preserve-marks t
925   "Whether marks are preserved when moving, copying and respooling messages."
926   :type 'boolean
927   :group 'gnus-summary-marks)
928
929 (defcustom gnus-alter-articles-to-read-function nil
930   "Function to be called to alter the list of articles to be selected."
931   :type 'function
932   :group 'gnus-summary)
933
934 (defcustom gnus-orphan-score nil
935   "*All orphans get this score added.  Set in the score file."
936   :group 'gnus-score-default
937   :type '(choice (const nil)
938                  integer))
939
940 (defcustom gnus-summary-save-parts-default-mime "image/.*"
941   "*A regexp to match MIME parts when saving multiple parts of a message
942 with gnus-summary-save-parts (X m). This regexp will be used by default
943 when prompting the user for which type of files to save."
944   :group 'gnus-summary
945   :type 'regexp)
946
947
948 ;;; Internal variables
949
950 (defvar gnus-article-mime-handles nil)
951 (defvar gnus-article-decoded-p nil)
952 (defvar gnus-scores-exclude-files nil)
953 (defvar gnus-page-broken nil)
954 (defvar gnus-inhibit-mime-unbuttonizing nil)
955
956 (defvar gnus-original-article nil)
957 (defvar gnus-article-internal-prepare-hook nil)
958 (defvar gnus-newsgroup-process-stack nil)
959
960 (defvar gnus-thread-indent-array nil)
961 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
962 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
963   "Function called to sort the articles within a thread after it has been gathered together.")
964
965 (defvar gnus-summary-save-parts-type-history nil)
966 (defvar gnus-summary-save-parts-last-directory nil)
967
968 ;; Avoid highlighting in kill files.
969 (defvar gnus-summary-inhibit-highlight nil)
970 (defvar gnus-newsgroup-selected-overlay nil)
971 (defvar gnus-inhibit-limiting nil)
972 (defvar gnus-newsgroup-adaptive-score-file nil)
973 (defvar gnus-current-score-file nil)
974 (defvar gnus-current-move-group nil)
975 (defvar gnus-current-copy-group nil)
976 (defvar gnus-current-crosspost-group nil)
977
978 (defvar gnus-newsgroup-dependencies nil)
979 (defvar gnus-newsgroup-adaptive nil)
980 (defvar gnus-summary-display-article-function nil)
981 (defvar gnus-summary-highlight-line-function nil
982   "Function called after highlighting a summary line.")
983
984 (defvar gnus-summary-line-format-alist
985   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
986     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
987     (?s gnus-tmp-subject-or-nil ?s)
988     (?n gnus-tmp-name ?s)
989     (?A (std11-address-string
990          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
991     (?a (or (std11-full-name-string
992              (car (mime-entity-read-field gnus-tmp-header 'From)))
993             gnus-tmp-from) ?s)
994     (?F gnus-tmp-from ?s)
995     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
996     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
997     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
998     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
999     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1000     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1001     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1002     (?L gnus-tmp-lines ?d)
1003     (?I gnus-tmp-indentation ?s)
1004     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1005     (?R gnus-tmp-replied ?c)
1006     (?\[ gnus-tmp-opening-bracket ?c)
1007     (?\] gnus-tmp-closing-bracket ?c)
1008     (?\> (make-string gnus-tmp-level ? ) ?s)
1009     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1010     (?i gnus-tmp-score ?d)
1011     (?z gnus-tmp-score-char ?c)
1012     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1013     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1014     (?U gnus-tmp-unread ?c)
1015     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1016     (?t (gnus-summary-number-of-articles-in-thread
1017          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1018         ?d)
1019     (?e (gnus-summary-number-of-articles-in-thread
1020          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1021         ?c)
1022     (?u gnus-tmp-user-defined ?s)
1023     (?P (gnus-pick-line-number) ?d))
1024   "An alist of format specifications that can appear in summary lines.
1025 These are paired with what variables they correspond with, along with
1026 the type of the variable (string, integer, character, etc).")
1027
1028 (defvar gnus-summary-dummy-line-format-alist
1029   `((?S gnus-tmp-subject ?s)
1030     (?N gnus-tmp-number ?d)
1031     (?u gnus-tmp-user-defined ?s)))
1032
1033 (defvar gnus-summary-mode-line-format-alist
1034   `((?G gnus-tmp-group-name ?s)
1035     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1036     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1037     (?A gnus-tmp-article-number ?d)
1038     (?Z gnus-tmp-unread-and-unselected ?s)
1039     (?V gnus-version ?s)
1040     (?U gnus-tmp-unread-and-unticked ?d)
1041     (?S gnus-tmp-subject ?s)
1042     (?e gnus-tmp-unselected ?d)
1043     (?u gnus-tmp-user-defined ?s)
1044     (?d (length gnus-newsgroup-dormant) ?d)
1045     (?t (length gnus-newsgroup-marked) ?d)
1046     (?r (length gnus-newsgroup-reads) ?d)
1047     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1048     (?E gnus-newsgroup-expunged-tally ?d)
1049     (?s (gnus-current-score-file-nondirectory) ?s)))
1050
1051 (defvar gnus-last-search-regexp nil
1052   "Default regexp for article search command.")
1053
1054 (defvar gnus-summary-search-article-matched-data nil
1055   "Last matched data of article search command.  It is the local variable
1056 in `gnus-article-buffer' which consists of the list of start position,
1057 end position and text.")
1058
1059 (defvar gnus-last-shell-command nil
1060   "Default shell command on article.")
1061
1062 (defvar gnus-newsgroup-begin nil)
1063 (defvar gnus-newsgroup-end nil)
1064 (defvar gnus-newsgroup-last-rmail nil)
1065 (defvar gnus-newsgroup-last-mail nil)
1066 (defvar gnus-newsgroup-last-folder nil)
1067 (defvar gnus-newsgroup-last-file nil)
1068 (defvar gnus-newsgroup-auto-expire nil)
1069 (defvar gnus-newsgroup-active nil)
1070
1071 (defvar gnus-newsgroup-data nil)
1072 (defvar gnus-newsgroup-data-reverse nil)
1073 (defvar gnus-newsgroup-limit nil)
1074 (defvar gnus-newsgroup-limits nil)
1075
1076 (defvar gnus-newsgroup-unreads nil
1077   "List of unread articles in the current newsgroup.")
1078
1079 (defvar gnus-newsgroup-unselected nil
1080   "List of unselected unread articles in the current newsgroup.")
1081
1082 (defvar gnus-newsgroup-reads nil
1083   "Alist of read articles and article marks in the current newsgroup.")
1084
1085 (defvar gnus-newsgroup-expunged-tally nil)
1086
1087 (defvar gnus-newsgroup-marked nil
1088   "List of ticked articles in the current newsgroup (a subset of unread art).")
1089
1090 (defvar gnus-newsgroup-killed nil
1091   "List of ranges of articles that have been through the scoring process.")
1092
1093 (defvar gnus-newsgroup-cached nil
1094   "List of articles that come from the article cache.")
1095
1096 (defvar gnus-newsgroup-saved nil
1097   "List of articles that have been saved.")
1098
1099 (defvar gnus-newsgroup-kill-headers nil)
1100
1101 (defvar gnus-newsgroup-replied nil
1102   "List of articles that have been replied to in the current newsgroup.")
1103
1104 (defvar gnus-newsgroup-expirable nil
1105   "List of articles in the current newsgroup that can be expired.")
1106
1107 (defvar gnus-newsgroup-processable nil
1108   "List of articles in the current newsgroup that can be processed.")
1109
1110 (defvar gnus-newsgroup-downloadable nil
1111   "List of articles in the current newsgroup that can be processed.")
1112
1113 (defvar gnus-newsgroup-undownloaded nil
1114   "List of articles in the current newsgroup that haven't been downloaded..")
1115
1116 (defvar gnus-newsgroup-unsendable nil
1117   "List of articles in the current newsgroup that won't be sent.")
1118
1119 (defvar gnus-newsgroup-bookmarks nil
1120   "List of articles in the current newsgroup that have bookmarks.")
1121
1122 (defvar gnus-newsgroup-dormant nil
1123   "List of dormant articles in the current newsgroup.")
1124
1125 (defvar gnus-newsgroup-scored nil
1126   "List of scored articles in the current newsgroup.")
1127
1128 (defvar gnus-newsgroup-incorporated nil
1129   "List of incorporated articles in the current newsgroup.")
1130
1131 (defvar gnus-newsgroup-headers nil
1132   "List of article headers in the current newsgroup.")
1133
1134 (defvar gnus-newsgroup-threads nil)
1135
1136 (defvar gnus-newsgroup-prepared nil
1137   "Whether the current group has been prepared properly.")
1138
1139 (defvar gnus-newsgroup-ancient nil
1140   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1141
1142 (defvar gnus-newsgroup-sparse nil)
1143
1144 (defvar gnus-current-article nil)
1145 (defvar gnus-article-current nil)
1146 (defvar gnus-current-headers nil)
1147 (defvar gnus-have-all-headers nil)
1148 (defvar gnus-last-article nil)
1149 (defvar gnus-newsgroup-history nil)
1150 (defvar gnus-newsgroup-charset nil)
1151 (defvar gnus-newsgroup-ephemeral-charset nil)
1152 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1153
1154 (defconst gnus-summary-local-variables
1155   '(gnus-newsgroup-name
1156     gnus-newsgroup-begin gnus-newsgroup-end
1157     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1158     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1159     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1160     gnus-newsgroup-unselected gnus-newsgroup-marked
1161     gnus-newsgroup-reads gnus-newsgroup-saved
1162     gnus-newsgroup-replied gnus-newsgroup-expirable
1163     gnus-newsgroup-processable gnus-newsgroup-killed
1164     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1165     gnus-newsgroup-unsendable
1166     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1167     gnus-newsgroup-headers gnus-newsgroup-threads
1168     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1169     gnus-current-article gnus-current-headers gnus-have-all-headers
1170     gnus-last-article gnus-article-internal-prepare-hook
1171     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1172     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1173     gnus-thread-expunge-below
1174     gnus-score-alist gnus-current-score-file
1175     (gnus-summary-expunge-below . global)
1176     (gnus-summary-mark-below . global)
1177     (gnus-orphan-score . global)
1178     gnus-newsgroup-active gnus-scores-exclude-files
1179     gnus-newsgroup-history gnus-newsgroup-ancient
1180     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1181     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1182     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1183     (gnus-newsgroup-expunged-tally . 0)
1184     gnus-cache-removable-articles gnus-newsgroup-cached
1185     gnus-newsgroup-data gnus-newsgroup-data-reverse
1186     gnus-newsgroup-limit gnus-newsgroup-limits
1187     gnus-newsgroup-charset
1188     gnus-newsgroup-incorporated)
1189   "Variables that are buffer-local to the summary buffers.")
1190
1191 ;; Byte-compiler warning.
1192 (defvar gnus-article-mode-map)
1193
1194 ;; Subject simplification.
1195
1196 (defun gnus-simplify-whitespace (str)
1197   "Remove excessive whitespace from STR."
1198   (let ((mystr str))
1199     ;; Multiple spaces.
1200     (while (string-match "[ \t][ \t]+" mystr)
1201       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1202                           " "
1203                           (substring mystr (match-end 0)))))
1204     ;; Leading spaces.
1205     (when (string-match "^[ \t]+" mystr)
1206       (setq mystr (substring mystr (match-end 0))))
1207     ;; Trailing spaces.
1208     (when (string-match "[ \t]+$" mystr)
1209       (setq mystr (substring mystr 0 (match-beginning 0))))
1210     mystr))
1211
1212 (defsubst gnus-simplify-subject-re (subject)
1213   "Remove \"Re:\" from subject lines."
1214   (if (string-match "^[Rr][Ee]: *" subject)
1215       (substring subject (match-end 0))
1216     subject))
1217
1218 (defun gnus-simplify-subject (subject &optional re-only)
1219   "Remove `Re:' and words in parentheses.
1220 If RE-ONLY is non-nil, strip leading `Re:'s only."
1221   (let ((case-fold-search t))           ;Ignore case.
1222     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1223     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1224       (setq subject (substring subject (match-end 0))))
1225     ;; Remove uninteresting prefixes.
1226     (when (and (not re-only)
1227                gnus-simplify-ignored-prefixes
1228                (string-match gnus-simplify-ignored-prefixes subject))
1229       (setq subject (substring subject (match-end 0))))
1230     ;; Remove words in parentheses from end.
1231     (unless re-only
1232       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1233         (setq subject (substring subject 0 (match-beginning 0)))))
1234     ;; Return subject string.
1235     subject))
1236
1237 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1238 ;; all whitespace.
1239 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1240   (goto-char (point-min))
1241   (while (re-search-forward regexp nil t)
1242     (replace-match (or newtext ""))))
1243
1244 (defun gnus-simplify-buffer-fuzzy ()
1245   "Simplify string in the buffer fuzzily.
1246 The string in the accessible portion of the current buffer is simplified.
1247 It is assumed to be a single-line subject.
1248 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1249 matter is removed.  Additional things can be deleted by setting
1250 `gnus-simplify-subject-fuzzy-regexp'."
1251   (let ((case-fold-search t)
1252         (modified-tick))
1253     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1254
1255     (while (not (eq modified-tick (buffer-modified-tick)))
1256       (setq modified-tick (buffer-modified-tick))
1257       (cond
1258        ((listp gnus-simplify-subject-fuzzy-regexp)
1259         (mapcar 'gnus-simplify-buffer-fuzzy-step
1260                 gnus-simplify-subject-fuzzy-regexp))
1261        (gnus-simplify-subject-fuzzy-regexp
1262         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1263       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1264       (gnus-simplify-buffer-fuzzy-step
1265        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1266       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1267
1268     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1269     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1270     (gnus-simplify-buffer-fuzzy-step " $")
1271     (gnus-simplify-buffer-fuzzy-step "^ +")))
1272
1273 (defun gnus-simplify-subject-fuzzy (subject)
1274   "Simplify a subject string fuzzily.
1275 See `gnus-simplify-buffer-fuzzy' for details."
1276   (save-excursion
1277     (gnus-set-work-buffer)
1278     (let ((case-fold-search t))
1279       ;; Remove uninteresting prefixes.
1280       (when (and gnus-simplify-ignored-prefixes
1281                  (string-match gnus-simplify-ignored-prefixes subject))
1282         (setq subject (substring subject (match-end 0))))
1283       (insert subject)
1284       (inline (gnus-simplify-buffer-fuzzy))
1285       (buffer-string))))
1286
1287 (defsubst gnus-simplify-subject-fully (subject)
1288   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1289   (cond
1290    (gnus-simplify-subject-functions
1291     (gnus-map-function gnus-simplify-subject-functions subject))
1292    ((null gnus-summary-gather-subject-limit)
1293     (gnus-simplify-subject-re subject))
1294    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1295     (gnus-simplify-subject-fuzzy subject))
1296    ((numberp gnus-summary-gather-subject-limit)
1297     (gnus-limit-string (gnus-simplify-subject-re subject)
1298                        gnus-summary-gather-subject-limit))
1299    (t
1300     subject)))
1301
1302 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1303   "Check whether two subjects are equal.
1304 If optional argument simple-first is t, first argument is already
1305 simplified."
1306   (cond
1307    ((null simple-first)
1308     (equal (gnus-simplify-subject-fully s1)
1309            (gnus-simplify-subject-fully s2)))
1310    (t
1311     (equal s1
1312            (gnus-simplify-subject-fully s2)))))
1313
1314 (defun gnus-summary-bubble-group ()
1315   "Increase the score of the current group.
1316 This is a handy function to add to `gnus-summary-exit-hook' to
1317 increase the score of each group you read."
1318   (gnus-group-add-score gnus-newsgroup-name))
1319
1320 \f
1321 ;;;
1322 ;;; Gnus summary mode
1323 ;;;
1324
1325 (put 'gnus-summary-mode 'mode-class 'special)
1326
1327 (when t
1328   ;; Non-orthogonal keys
1329
1330   (gnus-define-keys gnus-summary-mode-map
1331     " " gnus-summary-next-page
1332     "\177" gnus-summary-prev-page
1333     [delete] gnus-summary-prev-page
1334     [backspace] gnus-summary-prev-page
1335     "\r" gnus-summary-scroll-up
1336     "\M-\r" gnus-summary-scroll-down
1337     "n" gnus-summary-next-unread-article
1338     "p" gnus-summary-prev-unread-article
1339     "N" gnus-summary-next-article
1340     "P" gnus-summary-prev-article
1341     "\M-\C-n" gnus-summary-next-same-subject
1342     "\M-\C-p" gnus-summary-prev-same-subject
1343     "\M-n" gnus-summary-next-unread-subject
1344     "\M-p" gnus-summary-prev-unread-subject
1345     "." gnus-summary-first-unread-article
1346     "," gnus-summary-best-unread-article
1347     "\M-s" gnus-summary-search-article-forward
1348     "\M-r" gnus-summary-search-article-backward
1349     "<" gnus-summary-beginning-of-article
1350     ">" gnus-summary-end-of-article
1351     "j" gnus-summary-goto-article
1352     "^" gnus-summary-refer-parent-article
1353     "\M-^" gnus-summary-refer-article
1354     "u" gnus-summary-tick-article-forward
1355     "!" gnus-summary-tick-article-forward
1356     "U" gnus-summary-tick-article-backward
1357     "d" gnus-summary-mark-as-read-forward
1358     "D" gnus-summary-mark-as-read-backward
1359     "E" gnus-summary-mark-as-expirable
1360     "\M-u" gnus-summary-clear-mark-forward
1361     "\M-U" gnus-summary-clear-mark-backward
1362     "k" gnus-summary-kill-same-subject-and-select
1363     "\C-k" gnus-summary-kill-same-subject
1364     "\M-\C-k" gnus-summary-kill-thread
1365     "\M-\C-l" gnus-summary-lower-thread
1366     "e" gnus-summary-edit-article
1367     "#" gnus-summary-mark-as-processable
1368     "\M-#" gnus-summary-unmark-as-processable
1369     "\M-\C-t" gnus-summary-toggle-threads
1370     "\M-\C-s" gnus-summary-show-thread
1371     "\M-\C-h" gnus-summary-hide-thread
1372     "\M-\C-f" gnus-summary-next-thread
1373     "\M-\C-b" gnus-summary-prev-thread
1374     [(meta down)] gnus-summary-next-thread
1375     [(meta up)] gnus-summary-prev-thread
1376     "\M-\C-u" gnus-summary-up-thread
1377     "\M-\C-d" gnus-summary-down-thread
1378     "&" gnus-summary-execute-command
1379     "c" gnus-summary-catchup-and-exit
1380     "\C-w" gnus-summary-mark-region-as-read
1381     "\C-t" gnus-summary-toggle-truncation
1382     "?" gnus-summary-mark-as-dormant
1383     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1384     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1385     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1386     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1387     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1388     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1389     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1390     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1391     "=" gnus-summary-expand-window
1392     "\C-x\C-s" gnus-summary-reselect-current-group
1393     "\M-g" gnus-summary-rescan-group
1394     "w" gnus-summary-stop-page-breaking
1395     "\C-c\C-r" gnus-summary-caesar-message
1396     "\M-t" gnus-summary-toggle-mime
1397     "f" gnus-summary-followup
1398     "F" gnus-summary-followup-with-original
1399     "C" gnus-summary-cancel-article
1400     "r" gnus-summary-reply
1401     "R" gnus-summary-reply-with-original
1402     "\C-c\C-f" gnus-summary-mail-forward
1403     "o" gnus-summary-save-article
1404     "\C-o" gnus-summary-save-article-mail
1405     "|" gnus-summary-pipe-output
1406     "\M-k" gnus-summary-edit-local-kill
1407     "\M-K" gnus-summary-edit-global-kill
1408     ;; "V" gnus-version
1409     "\C-c\C-d" gnus-summary-describe-group
1410     "q" gnus-summary-exit
1411     "Q" gnus-summary-exit-no-update
1412     "\C-c\C-i" gnus-info-find-node
1413     gnus-mouse-2 gnus-mouse-pick-article
1414     "m" gnus-summary-mail-other-window
1415     "a" gnus-summary-post-news
1416     "x" gnus-summary-limit-to-unread
1417     "s" gnus-summary-isearch-article
1418     "t" gnus-article-toggle-headers
1419     "g" gnus-summary-show-article
1420     "l" gnus-summary-goto-last-article
1421     "v" gnus-summary-preview-mime-message
1422     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1423     "\C-d" gnus-summary-enter-digest-group
1424     "\M-\C-d" gnus-summary-read-document
1425     "\M-\C-e" gnus-summary-edit-parameters
1426     "\M-\C-a" gnus-summary-customize-parameters
1427     "\C-c\C-b" gnus-bug
1428     "*" gnus-cache-enter-article
1429     "\M-*" gnus-cache-remove-article
1430     "\M-&" gnus-summary-universal-argument
1431     "\C-l" gnus-recenter
1432     "I" gnus-summary-increase-score
1433     "L" gnus-summary-lower-score
1434     "\M-i" gnus-symbolic-argument
1435     "h" gnus-summary-select-article-buffer
1436
1437     "V" gnus-summary-score-map
1438     "X" gnus-uu-extract-map
1439     "S" gnus-summary-send-map)
1440
1441   ;; Sort of orthogonal keymap
1442   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1443     "t" gnus-summary-tick-article-forward
1444     "!" gnus-summary-tick-article-forward
1445     "d" gnus-summary-mark-as-read-forward
1446     "r" gnus-summary-mark-as-read-forward
1447     "c" gnus-summary-clear-mark-forward
1448     " " gnus-summary-clear-mark-forward
1449     "e" gnus-summary-mark-as-expirable
1450     "x" gnus-summary-mark-as-expirable
1451     "?" gnus-summary-mark-as-dormant
1452     "b" gnus-summary-set-bookmark
1453     "B" gnus-summary-remove-bookmark
1454     "#" gnus-summary-mark-as-processable
1455     "\M-#" gnus-summary-unmark-as-processable
1456     "S" gnus-summary-limit-include-expunged
1457     "C" gnus-summary-catchup
1458     "H" gnus-summary-catchup-to-here
1459     "\C-c" gnus-summary-catchup-all
1460     "k" gnus-summary-kill-same-subject-and-select
1461     "K" gnus-summary-kill-same-subject
1462     "P" gnus-uu-mark-map)
1463
1464   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1465     "c" gnus-summary-clear-above
1466     "u" gnus-summary-tick-above
1467     "m" gnus-summary-mark-above
1468     "k" gnus-summary-kill-below)
1469
1470   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1471     "/" gnus-summary-limit-to-subject
1472     "n" gnus-summary-limit-to-articles
1473     "w" gnus-summary-pop-limit
1474     "s" gnus-summary-limit-to-subject
1475     "a" gnus-summary-limit-to-author
1476     "u" gnus-summary-limit-to-unread
1477     "m" gnus-summary-limit-to-marks
1478     "M" gnus-summary-limit-exclude-marks
1479     "v" gnus-summary-limit-to-score
1480     "*" gnus-summary-limit-include-cached
1481     "D" gnus-summary-limit-include-dormant
1482     "T" gnus-summary-limit-include-thread
1483     "d" gnus-summary-limit-exclude-dormant
1484     "t" gnus-summary-limit-to-age
1485     "x" gnus-summary-limit-to-extra
1486     "E" gnus-summary-limit-include-expunged
1487     "c" gnus-summary-limit-exclude-childless-dormant
1488     "C" gnus-summary-limit-mark-excluded-as-read)
1489
1490   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1491     "n" gnus-summary-next-unread-article
1492     "p" gnus-summary-prev-unread-article
1493     "N" gnus-summary-next-article
1494     "P" gnus-summary-prev-article
1495     "\C-n" gnus-summary-next-same-subject
1496     "\C-p" gnus-summary-prev-same-subject
1497     "\M-n" gnus-summary-next-unread-subject
1498     "\M-p" gnus-summary-prev-unread-subject
1499     "f" gnus-summary-first-unread-article
1500     "b" gnus-summary-best-unread-article
1501     "j" gnus-summary-goto-article
1502     "g" gnus-summary-goto-subject
1503     "l" gnus-summary-goto-last-article
1504     "o" gnus-summary-pop-article)
1505
1506   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1507     "k" gnus-summary-kill-thread
1508     "l" gnus-summary-lower-thread
1509     "i" gnus-summary-raise-thread
1510     "T" gnus-summary-toggle-threads
1511     "t" gnus-summary-rethread-current
1512     "^" gnus-summary-reparent-thread
1513     "s" gnus-summary-show-thread
1514     "S" gnus-summary-show-all-threads
1515     "h" gnus-summary-hide-thread
1516     "H" gnus-summary-hide-all-threads
1517     "n" gnus-summary-next-thread
1518     "p" gnus-summary-prev-thread
1519     "u" gnus-summary-up-thread
1520     "o" gnus-summary-top-thread
1521     "d" gnus-summary-down-thread
1522     "#" gnus-uu-mark-thread
1523     "\M-#" gnus-uu-unmark-thread)
1524
1525   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1526     "g" gnus-summary-prepare
1527     "c" gnus-summary-insert-cached-articles)
1528
1529   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1530     "c" gnus-summary-catchup-and-exit
1531     "C" gnus-summary-catchup-all-and-exit
1532     "E" gnus-summary-exit-no-update
1533     "J" gnus-summary-jump-to-other-group
1534     "Q" gnus-summary-exit
1535     "Z" gnus-summary-exit
1536     "n" gnus-summary-catchup-and-goto-next-group
1537     "R" gnus-summary-reselect-current-group
1538     "G" gnus-summary-rescan-group
1539     "N" gnus-summary-next-group
1540     "s" gnus-summary-save-newsrc
1541     "P" gnus-summary-prev-group)
1542
1543   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1544     " " gnus-summary-next-page
1545     "n" gnus-summary-next-page
1546     "\177" gnus-summary-prev-page
1547     [delete] gnus-summary-prev-page
1548     "p" gnus-summary-prev-page
1549     "\r" gnus-summary-scroll-up
1550     "\M-\r" gnus-summary-scroll-down
1551     "<" gnus-summary-beginning-of-article
1552     ">" gnus-summary-end-of-article
1553     "b" gnus-summary-beginning-of-article
1554     "e" gnus-summary-end-of-article
1555     "^" gnus-summary-refer-parent-article
1556     "r" gnus-summary-refer-parent-article
1557     "D" gnus-summary-enter-digest-group
1558     "R" gnus-summary-refer-references
1559     "T" gnus-summary-refer-thread
1560     "g" gnus-summary-show-article
1561     "s" gnus-summary-isearch-article
1562     "P" gnus-summary-print-article
1563     "t" gnus-article-babel
1564     "d" gnus-summary-decrypt-article
1565     "v" gnus-summary-verify-article)
1566
1567   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1568     "b" gnus-article-add-buttons
1569     "B" gnus-article-add-buttons-to-head
1570     "o" gnus-article-treat-overstrike
1571     "e" gnus-article-emphasize
1572     "w" gnus-article-fill-cited-article
1573     "Q" gnus-article-fill-long-lines
1574     "C" gnus-article-capitalize-sentences
1575     "c" gnus-article-remove-cr
1576     "Z" gnus-article-decode-HZ
1577     "f" gnus-article-display-x-face
1578     "l" gnus-summary-stop-page-breaking
1579     "r" gnus-summary-caesar-message
1580     "t" gnus-article-toggle-headers
1581     "v" gnus-summary-verbose-headers
1582     "m" gnus-summary-toggle-mime
1583     "H" gnus-article-strip-headers-in-body
1584     "d" gnus-article-treat-dumbquotes
1585     "s" gnus-smiley-display)
1586
1587   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1588     "a" gnus-article-hide
1589     "h" gnus-article-toggle-headers
1590     "b" gnus-article-hide-boring-headers
1591     "s" gnus-article-hide-signature
1592     "c" gnus-article-hide-citation
1593     "C" gnus-article-hide-citation-in-followups
1594     "l" gnus-article-hide-list-identifiers
1595     "p" gnus-article-hide-pgp
1596     "B" gnus-article-strip-banner
1597     "P" gnus-article-hide-pem
1598     "\C-c" gnus-article-hide-citation-maybe)
1599
1600   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1601     "a" gnus-article-highlight
1602     "h" gnus-article-highlight-headers
1603     "c" gnus-article-highlight-citation
1604     "s" gnus-article-highlight-signature)
1605
1606   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1607     "z" gnus-article-date-ut
1608     "u" gnus-article-date-ut
1609     "l" gnus-article-date-local
1610     "e" gnus-article-date-lapsed
1611     "o" gnus-article-date-original
1612     "i" gnus-article-date-iso8601
1613     "s" gnus-article-date-user)
1614
1615   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1616     "t" gnus-article-remove-trailing-blank-lines
1617     "l" gnus-article-strip-leading-blank-lines
1618     "m" gnus-article-strip-multiple-blank-lines
1619     "a" gnus-article-strip-blank-lines
1620     "A" gnus-article-strip-all-blank-lines
1621     "s" gnus-article-strip-leading-space
1622     "e" gnus-article-strip-trailing-space)
1623
1624   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1625     "v" gnus-version
1626     "f" gnus-summary-fetch-faq
1627     "d" gnus-summary-describe-group
1628     "h" gnus-summary-describe-briefly
1629     "i" gnus-info-find-node)
1630
1631   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1632     "e" gnus-summary-expire-articles
1633     "\M-\C-e" gnus-summary-expire-articles-now
1634     "\177" gnus-summary-delete-article
1635     [delete] gnus-summary-delete-article
1636     [backspace] gnus-summary-delete-article
1637     "m" gnus-summary-move-article
1638     "r" gnus-summary-respool-article
1639     "w" gnus-summary-edit-article
1640     "c" gnus-summary-copy-article
1641     "B" gnus-summary-crosspost-article
1642     "q" gnus-summary-respool-query
1643     "t" gnus-summary-respool-trace
1644     "i" gnus-summary-import-article
1645     "p" gnus-summary-article-posted-p)
1646
1647   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1648     "o" gnus-summary-save-article
1649     "m" gnus-summary-save-article-mail
1650     "F" gnus-summary-write-article-file
1651     "r" gnus-summary-save-article-rmail
1652     "f" gnus-summary-save-article-file
1653     "b" gnus-summary-save-article-body-file
1654     "h" gnus-summary-save-article-folder
1655     "v" gnus-summary-save-article-vm
1656     "p" gnus-summary-pipe-output
1657     "s" gnus-soup-add-article)
1658
1659   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1660     "b" gnus-summary-display-buttonized
1661     "m" gnus-summary-repair-multipart
1662     "v" gnus-article-view-part
1663     "o" gnus-article-save-part
1664     "c" gnus-article-copy-part
1665     "e" gnus-article-externalize-part
1666     "i" gnus-article-inline-part
1667     "|" gnus-article-pipe-part))
1668
1669 (defun gnus-summary-make-menu-bar ()
1670   (gnus-turn-off-edit-menu 'summary)
1671
1672   (unless (boundp 'gnus-summary-misc-menu)
1673
1674     (easy-menu-define
1675      gnus-summary-kill-menu gnus-summary-mode-map ""
1676      (cons
1677       "Score"
1678       (nconc
1679        (list
1680         ["Enter score..." gnus-summary-score-entry t]
1681         ["Customize" gnus-score-customize t])
1682        (gnus-make-score-map 'increase)
1683        (gnus-make-score-map 'lower)
1684        '(("Mark"
1685           ["Kill below" gnus-summary-kill-below t]
1686           ["Mark above" gnus-summary-mark-above t]
1687           ["Tick above" gnus-summary-tick-above t]
1688           ["Clear above" gnus-summary-clear-above t])
1689          ["Current score" gnus-summary-current-score t]
1690          ["Set score" gnus-summary-set-score t]
1691          ["Switch current score file..." gnus-score-change-score-file t]
1692          ["Set mark below..." gnus-score-set-mark-below t]
1693          ["Set expunge below..." gnus-score-set-expunge-below t]
1694          ["Edit current score file" gnus-score-edit-current-scores t]
1695          ["Edit score file" gnus-score-edit-file t]
1696          ["Trace score" gnus-score-find-trace t]
1697          ["Find words" gnus-score-find-favourite-words t]
1698          ["Rescore buffer" gnus-summary-rescore t]
1699          ["Increase score..." gnus-summary-increase-score t]
1700          ["Lower score..." gnus-summary-lower-score t]))))
1701
1702     ;; Define both the Article menu in the summary buffer and the equivalent
1703     ;; Commands menu in the article buffer here for consistency.
1704     (let ((innards
1705            '(("Hide"
1706               ["All" gnus-article-hide t]
1707               ["Headers" gnus-article-toggle-headers t]
1708               ["Signature" gnus-article-hide-signature t]
1709               ["Citation" gnus-article-hide-citation t]
1710               ["List identifiers" gnus-article-hide-list-identifiers t]
1711               ["PGP" gnus-article-hide-pgp t]
1712               ["Banner" gnus-article-strip-banner t]
1713               ["Boring headers" gnus-article-hide-boring-headers t])
1714              ("Highlight"
1715               ["All" gnus-article-highlight t]
1716               ["Headers" gnus-article-highlight-headers t]
1717               ["Signature" gnus-article-highlight-signature t]
1718               ["Citation" gnus-article-highlight-citation t])
1719              ("Date"
1720               ["Local" gnus-article-date-local t]
1721               ["ISO8601" gnus-article-date-iso8601 t]
1722               ["UT" gnus-article-date-ut t]
1723               ["Original" gnus-article-date-original t]
1724               ["Lapsed" gnus-article-date-lapsed t]
1725               ["User-defined" gnus-article-date-user t])
1726              ("Washing"
1727               ("Remove Blanks"
1728                ["Leading" gnus-article-strip-leading-blank-lines t]
1729                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1730                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1731                ["All of the above" gnus-article-strip-blank-lines t]
1732                ["All" gnus-article-strip-all-blank-lines t]
1733                ["Leading space" gnus-article-strip-leading-space t]
1734                ["Trailing space" gnus-article-strip-trailing-space t])
1735               ["Overstrike" gnus-article-treat-overstrike t]
1736               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1737               ["Emphasis" gnus-article-emphasize t]
1738               ["Word wrap" gnus-article-fill-cited-article t]
1739               ["Fill long lines" gnus-article-fill-long-lines t]
1740               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1741               ["CR" gnus-article-remove-cr t]
1742               ["Show X-Face" gnus-article-display-x-face t]
1743               ["Rot 13" gnus-summary-caesar-message t]
1744               ["Unix pipe" gnus-summary-pipe-message t]
1745               ["Add buttons" gnus-article-add-buttons t]
1746               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1747               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1748               ["Toggle MIME" gnus-summary-toggle-mime t]
1749               ["Verbose header" gnus-summary-verbose-headers t]
1750               ["Toggle header" gnus-summary-toggle-header t]
1751               ["Toggle smileys" gnus-smiley-display t]
1752               ["HZ" gnus-article-decode-HZ t])
1753              ("Output"
1754               ["Save in default format" gnus-summary-save-article t]
1755               ["Save in file" gnus-summary-save-article-file t]
1756               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1757               ["Save in MH folder" gnus-summary-save-article-folder t]
1758               ["Save in VM folder" gnus-summary-save-article-vm t]
1759               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1760               ["Save body in file" gnus-summary-save-article-body-file t]
1761               ["Pipe through a filter" gnus-summary-pipe-output t]
1762               ["Add to SOUP packet" gnus-soup-add-article t]
1763               ["Print" gnus-summary-print-article t])
1764              ("Backend"
1765               ["Respool article..." gnus-summary-respool-article t]
1766               ["Move article..." gnus-summary-move-article
1767                (gnus-check-backend-function
1768                 'request-move-article gnus-newsgroup-name)]
1769               ["Copy article..." gnus-summary-copy-article t]
1770               ["Crosspost article..." gnus-summary-crosspost-article
1771                (gnus-check-backend-function
1772                 'request-replace-article gnus-newsgroup-name)]
1773               ["Import file..." gnus-summary-import-article t]
1774               ["Check if posted" gnus-summary-article-posted-p t]
1775               ["Edit article" gnus-summary-edit-article
1776                (not (gnus-group-read-only-p))]
1777               ["Delete article" gnus-summary-delete-article
1778                (gnus-check-backend-function
1779                 'request-expire-articles gnus-newsgroup-name)]
1780               ["Query respool" gnus-summary-respool-query t]
1781               ["Trace respool" gnus-summary-respool-trace t]
1782               ["Delete expirable articles" gnus-summary-expire-articles-now
1783                (gnus-check-backend-function
1784                 'request-expire-articles gnus-newsgroup-name)])
1785              ("Extract"
1786               ["Uudecode" gnus-uu-decode-uu t]
1787               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1788               ["Unshar" gnus-uu-decode-unshar t]
1789               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1790               ["Save" gnus-uu-decode-save t]
1791               ["Binhex" gnus-uu-decode-binhex t]
1792               ["Postscript" gnus-uu-decode-postscript t])
1793              ("Cache"
1794               ["Enter article" gnus-cache-enter-article t]
1795               ["Remove article" gnus-cache-remove-article t])
1796              ["Translate" gnus-article-babel t]
1797              ["Select article buffer" gnus-summary-select-article-buffer t]
1798              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1799              ["Isearch article..." gnus-summary-isearch-article t]
1800              ["Beginning of the article" gnus-summary-beginning-of-article t]
1801              ["End of the article" gnus-summary-end-of-article t]
1802              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1803              ["Fetch referenced articles" gnus-summary-refer-references t]
1804              ["Fetch current thread" gnus-summary-refer-thread t]
1805              ["Fetch article with id..." gnus-summary-refer-article t]
1806              ["Redisplay" gnus-summary-show-article t])))
1807       (easy-menu-define
1808        gnus-summary-article-menu gnus-summary-mode-map ""
1809        (cons "Article" innards))
1810
1811       (easy-menu-define
1812        gnus-article-commands-menu gnus-article-mode-map ""
1813        (cons "Commands" innards)))
1814
1815     (easy-menu-define
1816      gnus-summary-thread-menu gnus-summary-mode-map ""
1817      '("Threads"
1818        ["Toggle threading" gnus-summary-toggle-threads t]
1819        ["Hide threads" gnus-summary-hide-all-threads t]
1820        ["Show threads" gnus-summary-show-all-threads t]
1821        ["Hide thread" gnus-summary-hide-thread t]
1822        ["Show thread" gnus-summary-show-thread t]
1823        ["Go to next thread" gnus-summary-next-thread t]
1824        ["Go to previous thread" gnus-summary-prev-thread t]
1825        ["Go down thread" gnus-summary-down-thread t]
1826        ["Go up thread" gnus-summary-up-thread t]
1827        ["Top of thread" gnus-summary-top-thread t]
1828        ["Mark thread as read" gnus-summary-kill-thread t]
1829        ["Lower thread score" gnus-summary-lower-thread t]
1830        ["Raise thread score" gnus-summary-raise-thread t]
1831        ["Rethread current" gnus-summary-rethread-current t]))
1832
1833     (easy-menu-define
1834      gnus-summary-post-menu gnus-summary-mode-map ""
1835      '("Post"
1836        ["Post an article" gnus-summary-post-news t]
1837        ["Followup" gnus-summary-followup t]
1838        ["Followup and yank" gnus-summary-followup-with-original t]
1839        ["Supersede article" gnus-summary-supersede-article t]
1840        ["Cancel article" gnus-summary-cancel-article t]
1841        ["Reply" gnus-summary-reply t]
1842        ["Reply and yank" gnus-summary-reply-with-original t]
1843        ["Wide reply" gnus-summary-wide-reply t]
1844        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1845        ["Mail forward" gnus-summary-mail-forward t]
1846        ["Post forward" gnus-summary-post-forward t]
1847        ["Digest and mail" gnus-uu-digest-mail-forward t]
1848        ["Digest and post" gnus-uu-digest-post-forward t]
1849        ["Resend message" gnus-summary-resend-message t]
1850        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1851        ["Send a mail" gnus-summary-mail-other-window t]
1852        ["Uuencode and post" gnus-uu-post-news t]
1853        ["Followup via news" gnus-summary-followup-to-mail t]
1854        ["Followup via news and yank"
1855         gnus-summary-followup-to-mail-with-original t]
1856        ;;("Draft"
1857        ;;["Send" gnus-summary-send-draft t]
1858        ;;["Send bounced" gnus-resend-bounced-mail t])
1859        ))
1860
1861     (easy-menu-define
1862      gnus-summary-misc-menu gnus-summary-mode-map ""
1863      '("Misc"
1864        ("Mark Read"
1865         ["Mark as read" gnus-summary-mark-as-read-forward t]
1866         ["Mark same subject and select"
1867          gnus-summary-kill-same-subject-and-select t]
1868         ["Mark same subject" gnus-summary-kill-same-subject t]
1869         ["Catchup" gnus-summary-catchup t]
1870         ["Catchup all" gnus-summary-catchup-all t]
1871         ["Catchup to here" gnus-summary-catchup-to-here t]
1872         ["Catchup region" gnus-summary-mark-region-as-read t]
1873         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1874        ("Mark Various"
1875         ["Tick" gnus-summary-tick-article-forward t]
1876         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1877         ["Remove marks" gnus-summary-clear-mark-forward t]
1878         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1879         ["Set bookmark" gnus-summary-set-bookmark t]
1880         ["Remove bookmark" gnus-summary-remove-bookmark t])
1881        ("Mark Limit"
1882         ["Marks..." gnus-summary-limit-to-marks t]
1883         ["Subject..." gnus-summary-limit-to-subject t]
1884         ["Author..." gnus-summary-limit-to-author t]
1885         ["Age..." gnus-summary-limit-to-age t]
1886         ["Extra..." gnus-summary-limit-to-extra t]
1887         ["Score" gnus-summary-limit-to-score t]
1888         ["Unread" gnus-summary-limit-to-unread t]
1889         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1890         ["Articles" gnus-summary-limit-to-articles t]
1891         ["Pop limit" gnus-summary-pop-limit t]
1892         ["Show dormant" gnus-summary-limit-include-dormant t]
1893         ["Hide childless dormant"
1894          gnus-summary-limit-exclude-childless-dormant t]
1895         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1896         ["Hide marked" gnus-summary-limit-exclude-marks t]
1897         ["Show expunged" gnus-summary-show-all-expunged t])
1898        ("Process Mark"
1899         ["Set mark" gnus-summary-mark-as-processable t]
1900         ["Remove mark" gnus-summary-unmark-as-processable t]
1901         ["Remove all marks" gnus-summary-unmark-all-processable t]
1902         ["Mark above" gnus-uu-mark-over t]
1903         ["Mark series" gnus-uu-mark-series t]
1904         ["Mark region" gnus-uu-mark-region t]
1905         ["Unmark region" gnus-uu-unmark-region t]
1906         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1907         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1908         ["Mark all" gnus-uu-mark-all t]
1909         ["Mark buffer" gnus-uu-mark-buffer t]
1910         ["Mark sparse" gnus-uu-mark-sparse t]
1911         ["Mark thread" gnus-uu-mark-thread t]
1912         ["Unmark thread" gnus-uu-unmark-thread t]
1913         ("Process Mark Sets"
1914          ["Kill" gnus-summary-kill-process-mark t]
1915          ["Yank" gnus-summary-yank-process-mark
1916           gnus-newsgroup-process-stack]
1917          ["Save" gnus-summary-save-process-mark t]))
1918        ("Scroll article"
1919         ["Page forward" gnus-summary-next-page t]
1920         ["Page backward" gnus-summary-prev-page t]
1921         ["Line forward" gnus-summary-scroll-up t])
1922        ("Move"
1923         ["Next unread article" gnus-summary-next-unread-article t]
1924         ["Previous unread article" gnus-summary-prev-unread-article t]
1925         ["Next article" gnus-summary-next-article t]
1926         ["Previous article" gnus-summary-prev-article t]
1927         ["Next unread subject" gnus-summary-next-unread-subject t]
1928         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1929         ["Next article same subject" gnus-summary-next-same-subject t]
1930         ["Previous article same subject" gnus-summary-prev-same-subject t]
1931         ["First unread article" gnus-summary-first-unread-article t]
1932         ["Best unread article" gnus-summary-best-unread-article t]
1933         ["Go to subject number..." gnus-summary-goto-subject t]
1934         ["Go to article number..." gnus-summary-goto-article t]
1935         ["Go to the last article" gnus-summary-goto-last-article t]
1936         ["Pop article off history" gnus-summary-pop-article t])
1937        ("Sort"
1938         ["Sort by number" gnus-summary-sort-by-number t]
1939         ["Sort by author" gnus-summary-sort-by-author t]
1940         ["Sort by subject" gnus-summary-sort-by-subject t]
1941         ["Sort by date" gnus-summary-sort-by-date t]
1942         ["Sort by score" gnus-summary-sort-by-score t]
1943         ["Sort by lines" gnus-summary-sort-by-lines t]
1944         ["Sort by characters" gnus-summary-sort-by-chars t])
1945        ("Help"
1946         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1947         ["Describe group" gnus-summary-describe-group t]
1948         ["Read manual" gnus-info-find-node t])
1949        ("Modes"
1950         ["Pick and read" gnus-pick-mode t]
1951         ["Binary" gnus-binary-mode t])
1952        ("Regeneration"
1953         ["Regenerate" gnus-summary-prepare t]
1954         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1955         ["Toggle threading" gnus-summary-toggle-threads t])
1956        ["Filter articles..." gnus-summary-execute-command t]
1957        ["Run command on subjects..." gnus-summary-universal-argument t]
1958        ["Search articles forward..." gnus-summary-search-article-forward t]
1959        ["Search articles backward..." gnus-summary-search-article-backward t]
1960        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1961        ["Expand window" gnus-summary-expand-window t]
1962        ["Expire expirable articles" gnus-summary-expire-articles
1963         (gnus-check-backend-function
1964          'request-expire-articles gnus-newsgroup-name)]
1965        ["Edit local kill file" gnus-summary-edit-local-kill t]
1966        ["Edit main kill file" gnus-summary-edit-global-kill t]
1967        ["Edit group parameters" gnus-summary-edit-parameters t]
1968        ["Customize group parameters" gnus-summary-customize-parameters t]
1969        ["Send a bug report" gnus-bug t]
1970        ("Exit"
1971         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1972         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1973         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1974         ["Exit group" gnus-summary-exit t]
1975         ["Exit group without updating" gnus-summary-exit-no-update t]
1976         ["Exit and goto next group" gnus-summary-next-group t]
1977         ["Exit and goto prev group" gnus-summary-prev-group t]
1978         ["Reselect group" gnus-summary-reselect-current-group t]
1979         ["Rescan group" gnus-summary-rescan-group t]
1980         ["Update dribble" gnus-summary-save-newsrc t])))
1981
1982     (gnus-run-hooks 'gnus-summary-menu-hook)))
1983
1984 (defun gnus-score-set-default (var value)
1985   "A version of set that updates the GNU Emacs menu-bar."
1986   (set var value)
1987   ;; It is the message that forces the active status to be updated.
1988   (message ""))
1989
1990 (defun gnus-make-score-map (type)
1991   "Make a summary score map of type TYPE."
1992   (if t
1993       nil
1994     (let ((headers '(("author" "from" string)
1995                      ("subject" "subject" string)
1996                      ("article body" "body" string)
1997                      ("article head" "head" string)
1998                      ("xref" "xref" string)
1999                      ("extra header" "extra" string)
2000                      ("lines" "lines" number)
2001                      ("followups to author" "followup" string)))
2002           (types '((number ("less than" <)
2003                            ("greater than" >)
2004                            ("equal" =))
2005                    (string ("substring" s)
2006                            ("exact string" e)
2007                            ("fuzzy string" f)
2008                            ("regexp" r))))
2009           (perms '(("temporary" (current-time-string))
2010                    ("permanent" nil)
2011                    ("immediate" now)))
2012           header)
2013       (list
2014        (apply
2015         'nconc
2016         (list
2017          (if (eq type 'lower)
2018              "Lower score"
2019            "Increase score"))
2020         (let (outh)
2021           (while headers
2022             (setq header (car headers))
2023             (setq outh
2024                   (cons
2025                    (apply
2026                     'nconc
2027                     (list (car header))
2028                     (let ((ts (cdr (assoc (nth 2 header) types)))
2029                           outt)
2030                       (while ts
2031                         (setq outt
2032                               (cons
2033                                (apply
2034                                 'nconc
2035                                 (list (caar ts))
2036                                 (let ((ps perms)
2037                                       outp)
2038                                   (while ps
2039                                     (setq outp
2040                                           (cons
2041                                            (vector
2042                                             (caar ps)
2043                                             (list
2044                                              'gnus-summary-score-entry
2045                                              (nth 1 header)
2046                                              (if (or (string= (nth 1 header)
2047                                                               "head")
2048                                                      (string= (nth 1 header)
2049                                                               "body"))
2050                                                  ""
2051                                                (list 'gnus-summary-header
2052                                                      (nth 1 header)))
2053                                              (list 'quote (nth 1 (car ts)))
2054                                              (list 'gnus-score-delta-default
2055                                                    nil)
2056                                              (nth 1 (car ps))
2057                                              t)
2058                                             t)
2059                                            outp))
2060                                     (setq ps (cdr ps)))
2061                                   (list (nreverse outp))))
2062                                outt))
2063                         (setq ts (cdr ts)))
2064                       (list (nreverse outt))))
2065                    outh))
2066             (setq headers (cdr headers)))
2067           (list (nreverse outh))))))))
2068
2069 \f
2070
2071 (defun gnus-summary-mode (&optional group)
2072   "Major mode for reading articles.
2073
2074 All normal editing commands are switched off.
2075 \\<gnus-summary-mode-map>
2076 Each line in this buffer represents one article.  To read an
2077 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2078 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2079 respectively.
2080
2081 You can also post articles and send mail from this buffer.  To
2082 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2083 of an article, type `\\[gnus-summary-reply]'.
2084
2085 There are approx. one gazillion commands you can execute in this
2086 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2087
2088 The following commands are available:
2089
2090 \\{gnus-summary-mode-map}"
2091   (interactive)
2092   (when (gnus-visual-p 'summary-menu 'menu)
2093     (gnus-summary-make-menu-bar))
2094   (kill-all-local-variables)
2095   (gnus-summary-make-local-variables)
2096   (gnus-make-thread-indent-array)
2097   (gnus-simplify-mode-line)
2098   (setq major-mode 'gnus-summary-mode)
2099   (setq mode-name "Summary")
2100   (make-local-variable 'minor-mode-alist)
2101   (use-local-map gnus-summary-mode-map)
2102   (buffer-disable-undo)
2103   (setq buffer-read-only t)             ;Disable modification
2104   (setq truncate-lines t)
2105   (setq selective-display t)
2106   (setq selective-display-ellipses t)   ;Display `...'
2107   (gnus-summary-set-display-table)
2108   (gnus-set-default-directory)
2109   (setq gnus-newsgroup-name group)
2110   (unless (gnus-news-group-p group)
2111     (setq gnus-newsgroup-incorporated
2112           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2113   (make-local-variable 'gnus-summary-line-format)
2114   (make-local-variable 'gnus-summary-line-format-spec)
2115   (make-local-variable 'gnus-summary-dummy-line-format)
2116   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2117   (make-local-variable 'gnus-summary-mark-positions)
2118   (make-local-hook 'pre-command-hook)
2119   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2120   (gnus-run-hooks 'gnus-summary-mode-hook)
2121   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2122   (gnus-update-summary-mark-positions))
2123
2124 (defun gnus-summary-make-local-variables ()
2125   "Make all the local summary buffer variables."
2126   (let (global)
2127     (dolist (local gnus-summary-local-variables)
2128       (if (consp local)
2129           (progn
2130             (if (eq (cdr local) 'global)
2131                 ;; Copy the global value of the variable.
2132                 (setq global (symbol-value (car local)))
2133               ;; Use the value from the list.
2134               (setq global (eval (cdr local))))
2135             (set (make-local-variable (car local)) global))
2136         ;; Simple nil-valued local variable.
2137         (set (make-local-variable local) nil)))))
2138
2139 (defun gnus-summary-clear-local-variables ()
2140   (let ((locals gnus-summary-local-variables))
2141     (while locals
2142       (if (consp (car locals))
2143           (and (vectorp (caar locals))
2144                (set (caar locals) nil))
2145         (and (vectorp (car locals))
2146              (set (car locals) nil)))
2147       (setq locals (cdr locals)))))
2148
2149 ;; Summary data functions.
2150
2151 (defmacro gnus-data-number (data)
2152   `(car ,data))
2153
2154 (defmacro gnus-data-set-number (data number)
2155   `(setcar ,data ,number))
2156
2157 (defmacro gnus-data-mark (data)
2158   `(nth 1 ,data))
2159
2160 (defmacro gnus-data-set-mark (data mark)
2161   `(setcar (nthcdr 1 ,data) ,mark))
2162
2163 (defmacro gnus-data-pos (data)
2164   `(nth 2 ,data))
2165
2166 (defmacro gnus-data-set-pos (data pos)
2167   `(setcar (nthcdr 2 ,data) ,pos))
2168
2169 (defmacro gnus-data-header (data)
2170   `(nth 3 ,data))
2171
2172 (defmacro gnus-data-set-header (data header)
2173   `(setcar (nthcdr 3 ,data) ,header))
2174
2175 (defmacro gnus-data-level (data)
2176   `(nth 4 ,data))
2177
2178 (defmacro gnus-data-unread-p (data)
2179   `(= (nth 1 ,data) gnus-unread-mark))
2180
2181 (defmacro gnus-data-read-p (data)
2182   `(/= (nth 1 ,data) gnus-unread-mark))
2183
2184 (defmacro gnus-data-pseudo-p (data)
2185   `(consp (nth 3 ,data)))
2186
2187 (defmacro gnus-data-find (number)
2188   `(assq ,number gnus-newsgroup-data))
2189
2190 (defmacro gnus-data-find-list (number &optional data)
2191   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2192      (memq (assq ,number bdata)
2193            bdata)))
2194
2195 (defmacro gnus-data-make (number mark pos header level)
2196   `(list ,number ,mark ,pos ,header ,level))
2197
2198 (defun gnus-data-enter (after-article number mark pos header level offset)
2199   (let ((data (gnus-data-find-list after-article)))
2200     (unless data
2201       (error "No such article: %d" after-article))
2202     (setcdr data (cons (gnus-data-make number mark pos header level)
2203                        (cdr data)))
2204     (setq gnus-newsgroup-data-reverse nil)
2205     (gnus-data-update-list (cddr data) offset)))
2206
2207 (defun gnus-data-enter-list (after-article list &optional offset)
2208   (when list
2209     (let ((data (and after-article (gnus-data-find-list after-article)))
2210           (ilist list))
2211       (if (not (or data
2212                    after-article))
2213           (let ((odata gnus-newsgroup-data))
2214             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2215             (when offset
2216               (gnus-data-update-list odata offset)))
2217         ;; Find the last element in the list to be spliced into the main
2218         ;; list.
2219         (while (cdr list)
2220           (setq list (cdr list)))
2221         (if (not data)
2222             (progn
2223               (setcdr list gnus-newsgroup-data)
2224               (setq gnus-newsgroup-data ilist)
2225               (when offset
2226                 (gnus-data-update-list (cdr list) offset)))
2227           (setcdr list (cdr data))
2228           (setcdr data ilist)
2229           (when offset
2230             (gnus-data-update-list (cdr list) offset))))
2231       (setq gnus-newsgroup-data-reverse nil))))
2232
2233 (defun gnus-data-remove (article &optional offset)
2234   (let ((data gnus-newsgroup-data))
2235     (if (= (gnus-data-number (car data)) article)
2236         (progn
2237           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2238                 gnus-newsgroup-data-reverse nil)
2239           (when offset
2240             (gnus-data-update-list gnus-newsgroup-data offset)))
2241       (while (cdr data)
2242         (when (= (gnus-data-number (cadr data)) article)
2243           (setcdr data (cddr data))
2244           (when offset
2245             (gnus-data-update-list (cdr data) offset))
2246           (setq data nil
2247                 gnus-newsgroup-data-reverse nil))
2248         (setq data (cdr data))))))
2249
2250 (defmacro gnus-data-list (backward)
2251   `(if ,backward
2252        (or gnus-newsgroup-data-reverse
2253            (setq gnus-newsgroup-data-reverse
2254                  (reverse gnus-newsgroup-data)))
2255      gnus-newsgroup-data))
2256
2257 (defun gnus-data-update-list (data offset)
2258   "Add OFFSET to the POS of all data entries in DATA."
2259   (setq gnus-newsgroup-data-reverse nil)
2260   (while data
2261     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2262     (setq data (cdr data))))
2263
2264 (defun gnus-summary-article-pseudo-p (article)
2265   "Say whether this article is a pseudo article or not."
2266   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2267
2268 (defmacro gnus-summary-article-sparse-p (article)
2269   "Say whether this article is a sparse article or not."
2270   `(memq ,article gnus-newsgroup-sparse))
2271
2272 (defmacro gnus-summary-article-ancient-p (article)
2273   "Say whether this article is a sparse article or not."
2274   `(memq ,article gnus-newsgroup-ancient))
2275
2276 (defun gnus-article-parent-p (number)
2277   "Say whether this article is a parent or not."
2278   (let ((data (gnus-data-find-list number)))
2279     (and (cdr data)                     ; There has to be an article after...
2280          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2281             (gnus-data-level (nth 1 data))))))
2282
2283 (defun gnus-article-children (number)
2284   "Return a list of all children to NUMBER."
2285   (let* ((data (gnus-data-find-list number))
2286          (level (gnus-data-level (car data)))
2287          children)
2288     (setq data (cdr data))
2289     (while (and data
2290                 (= (gnus-data-level (car data)) (1+ level)))
2291       (push (gnus-data-number (car data)) children)
2292       (setq data (cdr data)))
2293     children))
2294
2295 (defmacro gnus-summary-skip-intangible ()
2296   "If the current article is intangible, then jump to a different article."
2297   '(let ((to (get-text-property (point) 'gnus-intangible)))
2298      (and to (gnus-summary-goto-subject to))))
2299
2300 (defmacro gnus-summary-article-intangible-p ()
2301   "Say whether this article is intangible or not."
2302   '(get-text-property (point) 'gnus-intangible))
2303
2304 (defun gnus-article-read-p (article)
2305   "Say whether ARTICLE is read or not."
2306   (not (or (memq article gnus-newsgroup-marked)
2307            (memq article gnus-newsgroup-unreads)
2308            (memq article gnus-newsgroup-unselected)
2309            (memq article gnus-newsgroup-dormant))))
2310
2311 ;; Some summary mode macros.
2312
2313 (defmacro gnus-summary-article-number ()
2314   "The article number of the article on the current line.
2315 If there isn's an article number here, then we return the current
2316 article number."
2317   '(progn
2318      (gnus-summary-skip-intangible)
2319      (or (get-text-property (point) 'gnus-number)
2320          (gnus-summary-last-subject))))
2321
2322 (defmacro gnus-summary-article-header (&optional number)
2323   "Return the header of article NUMBER."
2324   `(gnus-data-header (gnus-data-find
2325                       ,(or number '(gnus-summary-article-number)))))
2326
2327 (defmacro gnus-summary-thread-level (&optional number)
2328   "Return the level of thread that starts with article NUMBER."
2329   `(if (and (eq gnus-summary-make-false-root 'dummy)
2330             (get-text-property (point) 'gnus-intangible))
2331        0
2332      (gnus-data-level (gnus-data-find
2333                        ,(or number '(gnus-summary-article-number))))))
2334
2335 (defmacro gnus-summary-article-mark (&optional number)
2336   "Return the mark of article NUMBER."
2337   `(gnus-data-mark (gnus-data-find
2338                     ,(or number '(gnus-summary-article-number)))))
2339
2340 (defmacro gnus-summary-article-pos (&optional number)
2341   "Return the position of the line of article NUMBER."
2342   `(gnus-data-pos (gnus-data-find
2343                    ,(or number '(gnus-summary-article-number)))))
2344
2345 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2346 (defmacro gnus-summary-article-subject (&optional number)
2347   "Return current subject string or nil if nothing."
2348   `(let ((headers
2349           ,(if number
2350                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2351              '(gnus-data-header (assq (gnus-summary-article-number)
2352                                       gnus-newsgroup-data)))))
2353      (and headers
2354           (vectorp headers)
2355           (mail-header-subject headers))))
2356
2357 (defmacro gnus-summary-article-score (&optional number)
2358   "Return current article score."
2359   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2360                   gnus-newsgroup-scored))
2361        gnus-summary-default-score 0))
2362
2363 (defun gnus-summary-article-children (&optional number)
2364   "Return a list of article numbers that are children of article NUMBER."
2365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2366          (level (gnus-data-level (car data)))
2367          l children)
2368     (while (and (setq data (cdr data))
2369                 (> (setq l (gnus-data-level (car data))) level))
2370       (and (= (1+ level) l)
2371            (push (gnus-data-number (car data))
2372                  children)))
2373     (nreverse children)))
2374
2375 (defun gnus-summary-article-parent (&optional number)
2376   "Return the article number of the parent of article NUMBER."
2377   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2378                                     (gnus-data-list t)))
2379          (level (gnus-data-level (car data))))
2380     (if (zerop level)
2381         ()                              ; This is a root.
2382       ;; We search until we find an article with a level less than
2383       ;; this one.  That function has to be the parent.
2384       (while (and (setq data (cdr data))
2385                   (not (< (gnus-data-level (car data)) level))))
2386       (and data (gnus-data-number (car data))))))
2387
2388 (defun gnus-unread-mark-p (mark)
2389   "Say whether MARK is the unread mark."
2390   (= mark gnus-unread-mark))
2391
2392 (defun gnus-read-mark-p (mark)
2393   "Say whether MARK is one of the marks that mark as read.
2394 This is all marks except unread, ticked, dormant, and expirable."
2395   (not (or (= mark gnus-unread-mark)
2396            (= mark gnus-ticked-mark)
2397            (= mark gnus-dormant-mark)
2398            (= mark gnus-expirable-mark))))
2399
2400 (defmacro gnus-article-mark (number)
2401   "Return the MARK of article NUMBER.
2402 This macro should only be used when computing the mark the \"first\"
2403 time; i.e., when generating the summary lines.  After that,
2404 `gnus-summary-article-mark' should be used to examine the
2405 marks of articles."
2406   `(cond
2407     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2408     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2409     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2410     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2411     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2412     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2413     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2414     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2415            gnus-ancient-mark))))
2416
2417 ;; Saving hidden threads.
2418
2419 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2420 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2421
2422 (defmacro gnus-save-hidden-threads (&rest forms)
2423   "Save hidden threads, eval FORMS, and restore the hidden threads."
2424   (let ((config (make-symbol "config")))
2425     `(let ((,config (gnus-hidden-threads-configuration)))
2426        (unwind-protect
2427            (save-excursion
2428              ,@forms)
2429          (gnus-restore-hidden-threads-configuration ,config)))))
2430
2431 (defun gnus-data-compute-positions ()
2432   "Compute the positions of all articles."
2433   (setq gnus-newsgroup-data-reverse nil)
2434   (let ((data gnus-newsgroup-data))
2435     (save-excursion
2436       (gnus-save-hidden-threads
2437         (gnus-summary-show-all-threads)
2438         (goto-char (point-min))
2439         (while data
2440           (while (get-text-property (point) 'gnus-intangible)
2441             (forward-line 1))
2442           (gnus-data-set-pos (car data) (+ (point) 3))
2443           (setq data (cdr data))
2444           (forward-line 1))))))
2445
2446 (defun gnus-hidden-threads-configuration ()
2447   "Return the current hidden threads configuration."
2448   (save-excursion
2449     (let (config)
2450       (goto-char (point-min))
2451       (while (search-forward "\r" nil t)
2452         (push (1- (point)) config))
2453       config)))
2454
2455 (defun gnus-restore-hidden-threads-configuration (config)
2456   "Restore hidden threads configuration from CONFIG."
2457   (save-excursion
2458     (let (point buffer-read-only)
2459       (while (setq point (pop config))
2460         (when (and (< point (point-max))
2461                    (goto-char point)
2462                    (eq (char-after) ?\n))
2463           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2464
2465 ;; Various summary mode internalish functions.
2466
2467 (defun gnus-mouse-pick-article (e)
2468   (interactive "e")
2469   (mouse-set-point e)
2470   (gnus-summary-next-page nil t))
2471
2472 (defun gnus-summary-set-display-table ()
2473   "Change the display table.
2474 Odd characters have a tendency to mess
2475 up nicely formatted displays - we make all possible glyphs
2476 display only a single character."
2477
2478   ;; We start from the standard display table, if any.
2479   (let ((table (or (copy-sequence standard-display-table)
2480                    (make-display-table)))
2481         (i 32))
2482     ;; Nix out all the control chars...
2483     (while (>= (setq i (1- i)) 0)
2484       (aset table i [??]))
2485     ;; ... but not newline and cr, of course.  (cr is necessary for the
2486     ;; selective display).
2487     (aset table ?\n nil)
2488     (aset table ?\r nil)
2489     ;; We keep TAB as well.
2490     (aset table ?\t nil)
2491     ;; We nix out any glyphs over 126 that are not set already.
2492     (let ((i 256))
2493       (while (>= (setq i (1- i)) 127)
2494         ;; Only modify if the entry is nil.
2495         (unless (aref table i)
2496           (aset table i [??]))))
2497     (setq buffer-display-table table)))
2498
2499 (defun gnus-summary-setup-buffer (group)
2500   "Initialize summary buffer."
2501   (let ((buffer (concat "*Summary " group "*")))
2502     (if (get-buffer buffer)
2503         (progn
2504           (set-buffer buffer)
2505           (setq gnus-summary-buffer (current-buffer))
2506           (not gnus-newsgroup-prepared))
2507       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2508       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2509       (gnus-summary-mode group)
2510       (when gnus-carpal
2511         (gnus-carpal-setup-buffer 'summary))
2512       (unless gnus-single-article-buffer
2513         (make-local-variable 'gnus-article-buffer)
2514         (make-local-variable 'gnus-article-current)
2515         (make-local-variable 'gnus-original-article-buffer))
2516       (setq gnus-newsgroup-name group)
2517       t)))
2518
2519 (defun gnus-set-global-variables ()
2520   "Set the global equivalents of the buffer-local variables.
2521 They are set to the latest values they had.  These reflect the summary
2522 buffer that was in action when the last article was fetched."
2523   (when (eq major-mode 'gnus-summary-mode)
2524     (setq gnus-summary-buffer (current-buffer))
2525     (let ((name gnus-newsgroup-name)
2526           (marked gnus-newsgroup-marked)
2527           (unread gnus-newsgroup-unreads)
2528           (headers gnus-current-headers)
2529           (data gnus-newsgroup-data)
2530           (summary gnus-summary-buffer)
2531           (article-buffer gnus-article-buffer)
2532           (original gnus-original-article-buffer)
2533           (gac gnus-article-current)
2534           (reffed gnus-reffed-article-number)
2535           (score-file gnus-current-score-file)
2536           (default-charset gnus-newsgroup-charset))
2537       (save-excursion
2538         (set-buffer gnus-group-buffer)
2539         (setq gnus-newsgroup-name name
2540               gnus-newsgroup-marked marked
2541               gnus-newsgroup-unreads unread
2542               gnus-current-headers headers
2543               gnus-newsgroup-data data
2544               gnus-article-current gac
2545               gnus-summary-buffer summary
2546               gnus-article-buffer article-buffer
2547               gnus-original-article-buffer original
2548               gnus-reffed-article-number reffed
2549               gnus-current-score-file score-file
2550               gnus-newsgroup-charset default-charset)
2551         ;; The article buffer also has local variables.
2552         (when (gnus-buffer-live-p gnus-article-buffer)
2553           (set-buffer gnus-article-buffer)
2554           (setq gnus-summary-buffer summary))))))
2555
2556 (defun gnus-summary-article-unread-p (article)
2557   "Say whether ARTICLE is unread or not."
2558   (memq article gnus-newsgroup-unreads))
2559
2560 (defun gnus-summary-first-article-p (&optional article)
2561   "Return whether ARTICLE is the first article in the buffer."
2562   (if (not (setq article (or article (gnus-summary-article-number))))
2563       nil
2564     (eq article (caar gnus-newsgroup-data))))
2565
2566 (defun gnus-summary-last-article-p (&optional article)
2567   "Return whether ARTICLE is the last article in the buffer."
2568   (if (not (setq article (or article (gnus-summary-article-number))))
2569       ;; All non-existent numbers are the last article.  :-)
2570       t
2571     (not (cdr (gnus-data-find-list article)))))
2572
2573 (defun gnus-make-thread-indent-array ()
2574   (let ((n 200))
2575     (unless (and gnus-thread-indent-array
2576                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2577       (setq gnus-thread-indent-array (make-vector 201 "")
2578             gnus-thread-indent-array-level gnus-thread-indent-level)
2579       (while (>= n 0)
2580         (aset gnus-thread-indent-array n
2581               (make-string (* n gnus-thread-indent-level) ? ))
2582         (setq n (1- n))))))
2583
2584 (defun gnus-update-summary-mark-positions ()
2585   "Compute where the summary marks are to go."
2586   (save-excursion
2587     (when (gnus-buffer-exists-p gnus-summary-buffer)
2588       (set-buffer gnus-summary-buffer))
2589     (let ((gnus-replied-mark 129)
2590           (gnus-score-below-mark 130)
2591           (gnus-score-over-mark 130)
2592           (gnus-download-mark 131)
2593           (spec gnus-summary-line-format-spec)
2594           gnus-visual pos)
2595       (save-excursion
2596         (gnus-set-work-buffer)
2597         (let ((gnus-summary-line-format-spec spec)
2598               (gnus-newsgroup-downloadable '((0 . t))))
2599           (gnus-summary-insert-line
2600            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2601            0 nil 128 t nil "" nil 1)
2602           (goto-char (point-min))
2603           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2604                                              (- (point) 2)))))
2605           (goto-char (point-min))
2606           (push (cons 'replied (and (search-forward "\201" nil t)
2607                                     (- (point) 2)))
2608                 pos)
2609           (goto-char (point-min))
2610           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2611                 pos)
2612           (goto-char (point-min))
2613           (push (cons 'download
2614                       (and (search-forward "\203" nil t) (- (point) 2)))
2615                 pos)))
2616       (setq gnus-summary-mark-positions pos))))
2617
2618 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2619   "Insert a dummy root in the summary buffer."
2620   (beginning-of-line)
2621   (gnus-add-text-properties
2622    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2623    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2624
2625 (defun gnus-summary-from-or-to-or-newsgroups (header)
2626   (let ((to (cdr (assq 'To (mail-header-extra header))))
2627         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2628         (default-mime-charset (with-current-buffer gnus-summary-buffer
2629                                 default-mime-charset)))
2630     (cond
2631      ((and to
2632            gnus-ignored-from-addresses
2633            (string-match gnus-ignored-from-addresses
2634                          (mail-header-from header)))
2635       (concat "-> "
2636               (or (car (funcall gnus-extract-address-components
2637                                 (funcall
2638                                  gnus-decode-encoded-word-function to)))
2639                   (funcall gnus-decode-encoded-word-function to))))
2640      ((and newsgroups
2641            gnus-ignored-from-addresses
2642            (string-match gnus-ignored-from-addresses
2643                          (mail-header-from header)))
2644       (concat "=> " newsgroups))
2645      (t
2646       (or (car (funcall gnus-extract-address-components
2647                         (mail-header-from header)))
2648           (mail-header-from header))))))
2649
2650 (defun gnus-summary-insert-line (gnus-tmp-header
2651                                  gnus-tmp-level gnus-tmp-current
2652                                  gnus-tmp-unread gnus-tmp-replied
2653                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2654                                  &optional gnus-tmp-dummy gnus-tmp-score
2655                                  gnus-tmp-process)
2656   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2657          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2658          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2659          (gnus-tmp-score-char
2660           (if (or (null gnus-summary-default-score)
2661                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2662                       gnus-summary-zcore-fuzz))
2663               ?  ;Whitespace
2664             (if (< gnus-tmp-score gnus-summary-default-score)
2665                 gnus-score-below-mark gnus-score-over-mark)))
2666          (gnus-tmp-replied
2667           (cond (gnus-tmp-process gnus-process-mark)
2668                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2669                  gnus-cached-mark)
2670                 (gnus-tmp-replied gnus-replied-mark)
2671                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2672                  gnus-saved-mark)
2673                 (t gnus-unread-mark)))
2674          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2675          (gnus-tmp-name
2676           (cond
2677            ((string-match "<[^>]+> *$" gnus-tmp-from)
2678             (let ((beg (match-beginning 0)))
2679               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2680                        (substring gnus-tmp-from (1+ (match-beginning 0))
2681                                   (1- (match-end 0))))
2682                   (substring gnus-tmp-from 0 beg))))
2683            ((string-match "(.+)" gnus-tmp-from)
2684             (substring gnus-tmp-from
2685                        (1+ (match-beginning 0)) (1- (match-end 0))))
2686            (t gnus-tmp-from)))
2687          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2688          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2689          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2690          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2691          (buffer-read-only nil))
2692     (when (string= gnus-tmp-name "")
2693       (setq gnus-tmp-name gnus-tmp-from))
2694     (unless (numberp gnus-tmp-lines)
2695       (setq gnus-tmp-lines 0))
2696     (gnus-put-text-property-excluding-characters-with-faces
2697      (point)
2698      (progn (eval gnus-summary-line-format-spec) (point))
2699      'gnus-number gnus-tmp-number)
2700     (when (gnus-visual-p 'summary-highlight 'highlight)
2701       (forward-line -1)
2702       (gnus-run-hooks 'gnus-summary-update-hook)
2703       (forward-line 1))))
2704
2705 (defun gnus-summary-update-line (&optional dont-update)
2706   "Update summary line after change."
2707   (when (and gnus-summary-default-score
2708              (not gnus-summary-inhibit-highlight))
2709     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2710            (article (gnus-summary-article-number))
2711            (score (gnus-summary-article-score article)))
2712       (unless dont-update
2713         (if (and gnus-summary-mark-below
2714                  (< (gnus-summary-article-score)
2715                     gnus-summary-mark-below))
2716             ;; This article has a low score, so we mark it as read.
2717             (when (memq article gnus-newsgroup-unreads)
2718               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2719           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2720             ;; This article was previously marked as read on account
2721             ;; of a low score, but now it has risen, so we mark it as
2722             ;; unread.
2723             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2724         (gnus-summary-update-mark
2725          (if (or (null gnus-summary-default-score)
2726                  (<= (abs (- score gnus-summary-default-score))
2727                      gnus-summary-zcore-fuzz))
2728              ?  ;Whitespace
2729            (if (< score gnus-summary-default-score)
2730                gnus-score-below-mark gnus-score-over-mark))
2731          'score))
2732       ;; Do visual highlighting.
2733       (when (gnus-visual-p 'summary-highlight 'highlight)
2734         (gnus-run-hooks 'gnus-summary-update-hook)))))
2735
2736 (defvar gnus-tmp-new-adopts nil)
2737
2738 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2739   "Return the number of articles in THREAD.
2740 This may be 0 in some cases -- if none of the articles in
2741 the thread are to be displayed."
2742   (let* ((number
2743           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2744           (cond
2745            ((not (listp thread))
2746             1)
2747            ((and (consp thread) (cdr thread))
2748             (apply
2749              '+ 1 (mapcar
2750                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2751            ((null thread)
2752             1)
2753            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2754             1)
2755            (t 0))))
2756     (when (and level (zerop level) gnus-tmp-new-adopts)
2757       (incf number
2758             (apply '+ (mapcar
2759                        'gnus-summary-number-of-articles-in-thread
2760                        gnus-tmp-new-adopts))))
2761     (if char
2762         (if (> number 1) gnus-not-empty-thread-mark
2763           gnus-empty-thread-mark)
2764       number)))
2765
2766 (defun gnus-summary-set-local-parameters (group)
2767   "Go through the local params of GROUP and set all variable specs in that list."
2768   (let ((params (gnus-group-find-parameter group))
2769         elem)
2770     (while params
2771       (setq elem (car params)
2772             params (cdr params))
2773       (and (consp elem)                 ; Has to be a cons.
2774            (consp (cdr elem))           ; The cdr has to be a list.
2775            (symbolp (car elem))         ; Has to be a symbol in there.
2776            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2777            (ignore-errors               ; So we set it.
2778              (make-local-variable (car elem))
2779              (set (car elem) (eval (nth 1 elem))))))))
2780
2781 (defun gnus-summary-read-group (group &optional show-all no-article
2782                                       kill-buffer no-display backward
2783                                       select-articles)
2784   "Start reading news in newsgroup GROUP.
2785 If SHOW-ALL is non-nil, already read articles are also listed.
2786 If NO-ARTICLE is non-nil, no article is selected initially.
2787 If NO-DISPLAY, don't generate a summary buffer."
2788   (let (result)
2789     (while (and group
2790                 (null (setq result
2791                             (let ((gnus-auto-select-next nil))
2792                               (or (gnus-summary-read-group-1
2793                                    group show-all no-article
2794                                    kill-buffer no-display
2795                                    select-articles)
2796                                   (setq show-all nil
2797                                         select-articles nil)))))
2798                 (eq gnus-auto-select-next 'quietly))
2799       (set-buffer gnus-group-buffer)
2800       ;; The entry function called above goes to the next
2801       ;; group automatically, so we go two groups back
2802       ;; if we are searching for the previous group.
2803       (when backward
2804         (gnus-group-prev-unread-group 2))
2805       (if (not (equal group (gnus-group-group-name)))
2806           (setq group (gnus-group-group-name))
2807         (setq group nil)))
2808     result))
2809
2810 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2811   "Directly jump to the other GROUP from summary buffer.
2812 If SHOW-ALL is non-nil, already read articles are also listed."
2813   (interactive
2814    (if (eq gnus-summary-buffer (current-buffer))
2815        (list (completing-read
2816               "Group: " gnus-active-hashtb nil t
2817               (when (and gnus-newsgroup-name
2818                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2819                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2820               'gnus-group-history)
2821              current-prefix-arg)
2822      (error "%s must be invoked from a gnus summary buffer." this-command)))
2823   (unless (or (zerop (length group))
2824               (and gnus-newsgroup-name
2825                    (string-equal gnus-newsgroup-name group)))
2826     (gnus-summary-exit)
2827     (gnus-summary-read-group group show-all
2828                              gnus-dont-select-after-jump-to-other-group)))
2829
2830 (defun gnus-summary-read-group-1 (group show-all no-article
2831                                         kill-buffer no-display
2832                                         &optional select-articles)
2833   ;; Killed foreign groups can't be entered.
2834   (when (and (not (gnus-group-native-p group))
2835              (not (gnus-gethash group gnus-newsrc-hashtb)))
2836     (error "Dead non-native groups can't be entered"))
2837   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2838   (let* ((new-group (gnus-summary-setup-buffer group))
2839          (quit-config (gnus-group-quit-config group))
2840          (did-select (and new-group (gnus-select-newsgroup
2841                                      group show-all select-articles))))
2842     (cond
2843      ;; This summary buffer exists already, so we just select it.
2844      ((not new-group)
2845       (gnus-set-global-variables)
2846       (when kill-buffer
2847         (gnus-kill-or-deaden-summary kill-buffer))
2848       (gnus-configure-windows 'summary 'force)
2849       (gnus-set-mode-line 'summary)
2850       (gnus-summary-position-point)
2851       (message "")
2852       t)
2853      ;; We couldn't select this group.
2854      ((null did-select)
2855       (when (and (eq major-mode 'gnus-summary-mode)
2856                  (not (equal (current-buffer) kill-buffer)))
2857         (kill-buffer (current-buffer))
2858         (if (not quit-config)
2859             (progn
2860               ;; Update the info -- marks might need to be removed,
2861               ;; for instance.
2862               (gnus-summary-update-info)
2863               (set-buffer gnus-group-buffer)
2864               (gnus-group-jump-to-group group)
2865               (gnus-group-next-unread-group 1))
2866           (gnus-handle-ephemeral-exit quit-config)))
2867       (gnus-message 3 "Can't select group")
2868       nil)
2869      ;; The user did a `C-g' while prompting for number of articles,
2870      ;; so we exit this group.
2871      ((eq did-select 'quit)
2872       (and (eq major-mode 'gnus-summary-mode)
2873            (not (equal (current-buffer) kill-buffer))
2874            (kill-buffer (current-buffer)))
2875       (when kill-buffer
2876         (gnus-kill-or-deaden-summary kill-buffer))
2877       (if (not quit-config)
2878           (progn
2879             (set-buffer gnus-group-buffer)
2880             (gnus-group-jump-to-group group)
2881             (gnus-group-next-unread-group 1)
2882             (gnus-configure-windows 'group 'force))
2883         (gnus-handle-ephemeral-exit quit-config))
2884       ;; Finally signal the quit.
2885       (signal 'quit nil))
2886      ;; The group was successfully selected.
2887      (t
2888       (gnus-set-global-variables)
2889       ;; Save the active value in effect when the group was entered.
2890       (setq gnus-newsgroup-active
2891             (gnus-copy-sequence
2892              (gnus-active gnus-newsgroup-name)))
2893       ;; You can change the summary buffer in some way with this hook.
2894       (gnus-run-hooks 'gnus-select-group-hook)
2895       ;; Set any local variables in the group parameters.
2896       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2897       (gnus-update-format-specifications
2898        nil 'summary 'summary-mode 'summary-dummy)
2899       (gnus-update-summary-mark-positions)
2900       ;; Do score processing.
2901       (when gnus-use-scoring
2902         (gnus-possibly-score-headers))
2903       ;; Check whether to fill in the gaps in the threads.
2904       (when gnus-build-sparse-threads
2905         (gnus-build-sparse-threads))
2906       ;; Find the initial limit.
2907       (if gnus-show-threads
2908           (if show-all
2909               (let ((gnus-newsgroup-dormant nil))
2910                 (gnus-summary-initial-limit show-all))
2911             (gnus-summary-initial-limit show-all))
2912         ;; When untreaded, all articles are always shown.
2913         (setq gnus-newsgroup-limit
2914               (mapcar
2915                (lambda (header) (mail-header-number header))
2916                gnus-newsgroup-headers)))
2917       ;; Generate the summary buffer.
2918       (unless no-display
2919         (gnus-summary-prepare))
2920       (when gnus-use-trees
2921         (gnus-tree-open group)
2922         (setq gnus-summary-highlight-line-function
2923               'gnus-tree-highlight-article))
2924       ;; If the summary buffer is empty, but there are some low-scored
2925       ;; articles or some excluded dormants, we include these in the
2926       ;; buffer.
2927       (when (and (zerop (buffer-size))
2928                  (not no-display))
2929         (cond (gnus-newsgroup-dormant
2930                (gnus-summary-limit-include-dormant))
2931               ((and gnus-newsgroup-scored show-all)
2932                (gnus-summary-limit-include-expunged t))))
2933       ;; Function `gnus-apply-kill-file' must be called in this hook.
2934       (gnus-run-hooks 'gnus-apply-kill-hook)
2935       (if (and (zerop (buffer-size))
2936                (not no-display))
2937           (progn
2938             ;; This newsgroup is empty.
2939             (gnus-summary-catchup-and-exit nil t)
2940             (gnus-message 6 "No unread news")
2941             (when kill-buffer
2942               (gnus-kill-or-deaden-summary kill-buffer))
2943             ;; Return nil from this function.
2944             nil)
2945         ;; Hide conversation thread subtrees.  We cannot do this in
2946         ;; gnus-summary-prepare-hook since kill processing may not
2947         ;; work with hidden articles.
2948         (and gnus-show-threads
2949              gnus-thread-hide-subtree
2950              (gnus-summary-hide-all-threads))
2951         (when kill-buffer
2952           (gnus-kill-or-deaden-summary kill-buffer))
2953         ;; Show first unread article if requested.
2954         (if (and (not no-article)
2955                  (not no-display)
2956                  gnus-newsgroup-unreads
2957                  gnus-auto-select-first)
2958             (progn
2959               (gnus-configure-windows 'summary)
2960               (cond
2961                ((eq gnus-auto-select-first 'best)
2962                 (gnus-summary-best-unread-article))
2963                ((eq gnus-auto-select-first t)
2964                 (gnus-summary-first-unread-article))
2965                ((gnus-functionp gnus-auto-select-first)
2966                 (funcall gnus-auto-select-first))))
2967           ;; Don't select any articles, just move point to the first
2968           ;; article in the group.
2969           (goto-char (point-min))
2970           (gnus-summary-position-point)
2971           (gnus-configure-windows 'summary 'force)
2972           (gnus-set-mode-line 'summary))
2973         (when (get-buffer-window gnus-group-buffer t)
2974           ;; Gotta use windows, because recenter does weird stuff if
2975           ;; the current buffer ain't the displayed window.
2976           (let ((owin (selected-window)))
2977             (select-window (get-buffer-window gnus-group-buffer t))
2978             (when (gnus-group-goto-group group)
2979               (recenter))
2980             (select-window owin)))
2981         ;; Mark this buffer as "prepared".
2982         (setq gnus-newsgroup-prepared t)
2983         (gnus-run-hooks 'gnus-summary-prepared-hook)
2984         t)))))
2985
2986 (defun gnus-summary-prepare ()
2987   "Generate the summary buffer."
2988   (interactive)
2989   (let ((buffer-read-only nil))
2990     (erase-buffer)
2991     (setq gnus-newsgroup-data nil
2992           gnus-newsgroup-data-reverse nil)
2993     (gnus-run-hooks 'gnus-summary-generate-hook)
2994     ;; Generate the buffer, either with threads or without.
2995     (when gnus-newsgroup-headers
2996       (gnus-summary-prepare-threads
2997        (if gnus-show-threads
2998            (gnus-sort-gathered-threads
2999             (funcall gnus-summary-thread-gathering-function
3000                      (gnus-sort-threads
3001                       (gnus-cut-threads (gnus-make-threads)))))
3002          ;; Unthreaded display.
3003          (gnus-sort-articles gnus-newsgroup-headers))))
3004     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3005     ;; Call hooks for modifying summary buffer.
3006     (goto-char (point-min))
3007     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3008
3009 (defsubst gnus-general-simplify-subject (subject)
3010   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3011   (setq subject
3012         (cond
3013          ;; Truncate the subject.
3014          (gnus-simplify-subject-functions
3015           (gnus-map-function gnus-simplify-subject-functions subject))
3016          ((numberp gnus-summary-gather-subject-limit)
3017           (setq subject (gnus-simplify-subject-re subject))
3018           (if (> (length subject) gnus-summary-gather-subject-limit)
3019               (substring subject 0 gnus-summary-gather-subject-limit)
3020             subject))
3021          ;; Fuzzily simplify it.
3022          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3023           (gnus-simplify-subject-fuzzy subject))
3024          ;; Just remove the leading "Re:".
3025          (t
3026           (gnus-simplify-subject-re subject))))
3027
3028   (if (and gnus-summary-gather-exclude-subject
3029            (string-match gnus-summary-gather-exclude-subject subject))
3030       nil                               ; This article shouldn't be gathered
3031     subject))
3032
3033 (defun gnus-summary-simplify-subject-query ()
3034   "Query where the respool algorithm would put this article."
3035   (interactive)
3036   (gnus-summary-select-article)
3037   (message "%s"
3038            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3039
3040 (defun gnus-gather-threads-by-subject (threads)
3041   "Gather threads by looking at Subject headers."
3042   (if (not gnus-summary-make-false-root)
3043       threads
3044     (let ((hashtb (gnus-make-hashtable 1024))
3045           (prev threads)
3046           (result threads)
3047           subject hthread whole-subject)
3048       (while threads
3049         (setq subject (gnus-general-simplify-subject
3050                        (setq whole-subject (mail-header-subject
3051                                             (caar threads)))))
3052         (when subject
3053           (if (setq hthread (gnus-gethash subject hashtb))
3054               (progn
3055                 ;; We enter a dummy root into the thread, if we
3056                 ;; haven't done that already.
3057                 (unless (stringp (caar hthread))
3058                   (setcar hthread (list whole-subject (car hthread))))
3059                 ;; We add this new gathered thread to this gathered
3060                 ;; thread.
3061                 (setcdr (car hthread)
3062                         (nconc (cdar hthread) (list (car threads))))
3063                 ;; Remove it from the list of threads.
3064                 (setcdr prev (cdr threads))
3065                 (setq threads prev))
3066             ;; Enter this thread into the hash table.
3067             (gnus-sethash subject threads hashtb)))
3068         (setq prev threads)
3069         (setq threads (cdr threads)))
3070       result)))
3071
3072 (defun gnus-gather-threads-by-references (threads)
3073   "Gather threads by looking at References headers."
3074   (let ((idhashtb (gnus-make-hashtable 1024))
3075         (thhashtb (gnus-make-hashtable 1024))
3076         (prev threads)
3077         (result threads)
3078         ids references id gthread gid entered ref)
3079     (while threads
3080       (when (setq references (mail-header-references (caar threads)))
3081         (setq id (mail-header-id (caar threads))
3082               ids (gnus-split-references references)
3083               entered nil)
3084         (while (setq ref (pop ids))
3085           (setq ids (delete ref ids))
3086           (if (not (setq gid (gnus-gethash ref idhashtb)))
3087               (progn
3088                 (gnus-sethash ref id idhashtb)
3089                 (gnus-sethash id threads thhashtb))
3090             (setq gthread (gnus-gethash gid thhashtb))
3091             (unless entered
3092               ;; We enter a dummy root into the thread, if we
3093               ;; haven't done that already.
3094               (unless (stringp (caar gthread))
3095                 (setcar gthread (list (mail-header-subject (caar gthread))
3096                                       (car gthread))))
3097               ;; We add this new gathered thread to this gathered
3098               ;; thread.
3099               (setcdr (car gthread)
3100                       (nconc (cdar gthread) (list (car threads)))))
3101             ;; Add it into the thread hash table.
3102             (gnus-sethash id gthread thhashtb)
3103             (setq entered t)
3104             ;; Remove it from the list of threads.
3105             (setcdr prev (cdr threads))
3106             (setq threads prev))))
3107       (setq prev threads)
3108       (setq threads (cdr threads)))
3109     result))
3110
3111 (defun gnus-sort-gathered-threads (threads)
3112   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3113   (let ((result threads))
3114     (while threads
3115       (when (stringp (caar threads))
3116         (setcdr (car threads)
3117                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3118       (setq threads (cdr threads)))
3119     result))
3120
3121 (defun gnus-thread-loop-p (root thread)
3122   "Say whether ROOT is in THREAD."
3123   (let ((stack (list thread))
3124         (infloop 0)
3125         th)
3126     (while (setq thread (pop stack))
3127       (setq th (cdr thread))
3128       (while (and th
3129                   (not (eq (caar th) root)))
3130         (pop th))
3131       (if th
3132           ;; We have found a loop.
3133           (let (ref-dep)
3134             (setcdr thread (delq (car th) (cdr thread)))
3135             (if (boundp (setq ref-dep (intern "none"
3136                                               gnus-newsgroup-dependencies)))
3137                 (setcdr (symbol-value ref-dep)
3138                         (nconc (cdr (symbol-value ref-dep))
3139                                (list (car th))))
3140               (set ref-dep (list nil (car th))))
3141             (setq infloop 1
3142                   stack nil))
3143         ;; Push all the subthreads onto the stack.
3144         (push (cdr thread) stack)))
3145     infloop))
3146
3147 (defun gnus-make-threads ()
3148   "Go through the dependency hashtb and find the roots.  Return all threads."
3149   (let (threads)
3150     (while (catch 'infloop
3151              (mapatoms
3152               (lambda (refs)
3153                 ;; Deal with self-referencing References loops.
3154                 (when (and (car (symbol-value refs))
3155                            (not (zerop
3156                                  (apply
3157                                   '+
3158                                   (mapcar
3159                                    (lambda (thread)
3160                                      (gnus-thread-loop-p
3161                                       (car (symbol-value refs)) thread))
3162                                    (cdr (symbol-value refs)))))))
3163                   (setq threads nil)
3164                   (throw 'infloop t))
3165                 (unless (car (symbol-value refs))
3166                   ;; These threads do not refer back to any other articles,
3167                   ;; so they're roots.
3168                   (setq threads (append (cdr (symbol-value refs)) threads))))
3169               gnus-newsgroup-dependencies)))
3170     threads))
3171
3172 ;; Build the thread tree.
3173 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3174   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3175
3176 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3177 if it was already present.
3178
3179 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3180 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3181 Message-IDs will be renamed be renamed to a unique Message-ID before
3182 being entered.
3183
3184 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3185   (let* ((id (mail-header-id header))
3186          (id-dep (and id (intern id dependencies)))
3187          ref ref-dep ref-header)
3188     ;; Enter this `header' in the `dependencies' table.
3189     (cond
3190      ((not id-dep)
3191       (setq header nil))
3192      ;; The first two cases do the normal part: enter a new `header'
3193      ;; in the `dependencies' table.
3194      ((not (boundp id-dep))
3195       (set id-dep (list header)))
3196      ((null (car (symbol-value id-dep)))
3197       (setcar (symbol-value id-dep) header))
3198
3199      ;; From here the `header' was already present in the
3200      ;; `dependencies' table.
3201      (force-new
3202       ;; Overrides an existing entry;
3203       ;; just set the header part of the entry.
3204       (setcar (symbol-value id-dep) header))
3205
3206      ;; Renames the existing `header' to a unique Message-ID.
3207      ((not gnus-summary-ignore-duplicates)
3208       ;; An article with this Message-ID has already been seen.
3209       ;; We rename the Message-ID.
3210       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3211            (list header))
3212       (mail-header-set-id header id))
3213
3214      ;; The last case ignores an existing entry, except it adds any
3215      ;; additional Xrefs (in case the two articles came from different
3216      ;; servers.
3217      ;; Also sets `header' to `nil' meaning that the `dependencies'
3218      ;; table was *not* modified.
3219      (t
3220       (mail-header-set-xref
3221        (car (symbol-value id-dep))
3222        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3223                    "")
3224                (or (mail-header-xref header) "")))
3225       (setq header nil)))
3226
3227     (when header
3228       ;; First check if that we are not creating a References loop.
3229       (setq ref (gnus-parent-id (mail-header-references header)))
3230       (while (and ref
3231                   (setq ref-dep (intern-soft ref dependencies))
3232                   (boundp ref-dep)
3233                   (setq ref-header (car (symbol-value ref-dep))))
3234         (if (string= id ref)
3235             ;; Yuk!  This is a reference loop.  Make the article be a
3236             ;; root article.
3237             (progn
3238               (mail-header-set-references (car (symbol-value id-dep)) "none")
3239               (setq ref nil))
3240           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3241       (setq ref (gnus-parent-id (mail-header-references header)))
3242       (setq ref-dep (intern (or ref "none") dependencies))
3243       (if (boundp ref-dep)
3244           (setcdr (symbol-value ref-dep)
3245                   (nconc (cdr (symbol-value ref-dep))
3246                          (list (symbol-value id-dep))))
3247         (set ref-dep (list nil (symbol-value id-dep)))))
3248     header))
3249
3250 (defun gnus-build-sparse-threads ()
3251   (let ((headers gnus-newsgroup-headers)
3252         (mail-parse-charset gnus-newsgroup-charset)
3253         (gnus-summary-ignore-duplicates t)
3254         header references generation relations
3255         subject child end new-child date)
3256     ;; First we create an alist of generations/relations, where
3257     ;; generations is how much we trust the relation, and the relation
3258     ;; is parent/child.
3259     (gnus-message 7 "Making sparse threads...")
3260     (save-excursion
3261       (nnheader-set-temp-buffer " *gnus sparse threads*")
3262       (while (setq header (pop headers))
3263         (when (and (setq references (mail-header-references header))
3264                    (not (string= references "")))
3265           (insert references)
3266           (setq child (mail-header-id header)
3267                 subject (mail-header-subject header)
3268                 date (mail-header-date header)
3269                 generation 0)
3270           (while (search-backward ">" nil t)
3271             (setq end (1+ (point)))
3272             (when (search-backward "<" nil t)
3273               (setq new-child (buffer-substring (point) end))
3274               (push (list (incf generation)
3275                           child (setq child new-child)
3276                           subject date)
3277                     relations)))
3278           (when child
3279             (push (list (1+ generation) child nil subject) relations))
3280           (erase-buffer)))
3281       (kill-buffer (current-buffer)))
3282     ;; Sort over trustworthiness.
3283     (mapcar
3284      (lambda (relation)
3285        (when (gnus-dependencies-add-header
3286               (make-full-mail-header-from-decoded-header
3287                gnus-reffed-article-number
3288                (nth 3 relation) "" (or (nth 4 relation) "")
3289                (nth 1 relation)
3290                (or (nth 2 relation) "") 0 0 "")
3291               gnus-newsgroup-dependencies nil)
3292          (push gnus-reffed-article-number gnus-newsgroup-limit)
3293          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3294          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3295                gnus-newsgroup-reads)
3296          (decf gnus-reffed-article-number)))
3297      (sort relations 'car-less-than-car))
3298     (gnus-message 7 "Making sparse threads...done")))
3299
3300 (defun gnus-build-old-threads ()
3301   ;; Look at all the articles that refer back to old articles, and
3302   ;; fetch the headers for the articles that aren't there.  This will
3303   ;; build complete threads - if the roots haven't been expired by the
3304   ;; server, that is.
3305   (let ((mail-parse-charset gnus-newsgroup-charset)
3306         id heads)
3307     (mapatoms
3308      (lambda (refs)
3309        (when (not (car (symbol-value refs)))
3310          (setq heads (cdr (symbol-value refs)))
3311          (while heads
3312            (if (memq (mail-header-number (caar heads))
3313                      gnus-newsgroup-dormant)
3314                (setq heads (cdr heads))
3315              (setq id (symbol-name refs))
3316              (while (and (setq id (gnus-build-get-header id))
3317                          (not (car (gnus-id-to-thread id)))))
3318              (setq heads nil)))))
3319      gnus-newsgroup-dependencies)))
3320
3321 ;; This function has to be called with point after the article number
3322 ;; on the beginning of the line.
3323 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3324   (let ((eol (gnus-point-at-eol))
3325         (buffer (current-buffer))
3326         header)
3327
3328     ;; overview: [num subject from date id refs chars lines misc]
3329     (unwind-protect
3330         (progn
3331           (narrow-to-region (point) eol)
3332           (unless (eobp)
3333             (forward-char))
3334
3335           (setq header
3336                 (make-full-mail-header
3337                  number                         ; number
3338                  (nnheader-nov-field)           ; subject
3339                  (nnheader-nov-field)           ; from
3340                  (nnheader-nov-field)           ; date
3341                  (nnheader-nov-read-message-id) ; id
3342                  (nnheader-nov-field)           ; refs
3343                  (nnheader-nov-read-integer)    ; chars
3344                  (nnheader-nov-read-integer)    ; lines
3345                  (unless (eobp)
3346                    (if (looking-at "Xref: ")
3347                        (goto-char (match-end 0)))
3348                    (nnheader-nov-field))        ; Xref
3349                  (nnheader-nov-parse-extra))))  ; extra
3350
3351       (widen))
3352
3353     (when gnus-alter-header-function
3354       (funcall gnus-alter-header-function header))
3355     (gnus-dependencies-add-header header dependencies force-new)))
3356
3357 (defun gnus-build-get-header (id)
3358   "Look through the buffer of NOV lines and find the header to ID.
3359 Enter this line into the dependencies hash table, and return
3360 the id of the parent article (if any)."
3361   (let ((deps gnus-newsgroup-dependencies)
3362         found header)
3363     (prog1
3364         (save-excursion
3365           (set-buffer nntp-server-buffer)
3366           (let ((case-fold-search nil))
3367             (goto-char (point-min))
3368             (while (and (not found)
3369                         (search-forward id nil t))
3370               (beginning-of-line)
3371               (setq found (looking-at
3372                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3373                                    (regexp-quote id))))
3374               (or found (beginning-of-line 2)))
3375             (when found
3376               (beginning-of-line)
3377               (and
3378                (setq header (gnus-nov-parse-line
3379                              (read (current-buffer)) deps))
3380                (gnus-parent-id (mail-header-references header))))))
3381       (when header
3382         (let ((number (mail-header-number header)))
3383           (push number gnus-newsgroup-limit)
3384           (push header gnus-newsgroup-headers)
3385           (if (memq number gnus-newsgroup-unselected)
3386               (progn
3387                 (push number gnus-newsgroup-unreads)
3388                 (setq gnus-newsgroup-unselected
3389                       (delq number gnus-newsgroup-unselected)))
3390             (push number gnus-newsgroup-ancient)))))))
3391
3392 (defun gnus-build-all-threads ()
3393   "Read all the headers."
3394   (let ((gnus-summary-ignore-duplicates t)
3395         (mail-parse-charset gnus-newsgroup-charset)
3396         (dependencies gnus-newsgroup-dependencies)
3397         header article)
3398     (save-excursion
3399       (set-buffer nntp-server-buffer)
3400       (let ((case-fold-search nil))
3401         (goto-char (point-min))
3402         (while (not (eobp))
3403           (ignore-errors
3404             (setq article (read (current-buffer))
3405                   header (gnus-nov-parse-line article dependencies)))
3406           (when header
3407             (save-excursion
3408               (set-buffer gnus-summary-buffer)
3409               (push header gnus-newsgroup-headers)
3410               (if (memq (setq article (mail-header-number header))
3411                         gnus-newsgroup-unselected)
3412                   (progn
3413                     (push article gnus-newsgroup-unreads)
3414                     (setq gnus-newsgroup-unselected
3415                           (delq article gnus-newsgroup-unselected)))
3416                 (push article gnus-newsgroup-ancient)))
3417             (forward-line 1)))))))
3418
3419 (defun gnus-summary-update-article-line (article header)
3420   "Update the line for ARTICLE using HEADERS."
3421   (let* ((id (mail-header-id header))
3422          (thread (gnus-id-to-thread id)))
3423     (unless thread
3424       (error "Article in no thread"))
3425     ;; Update the thread.
3426     (setcar thread header)
3427     (gnus-summary-goto-subject article)
3428     (let* ((datal (gnus-data-find-list article))
3429            (data (car datal))
3430            (length (when (cdr datal)
3431                      (- (gnus-data-pos data)
3432                         (gnus-data-pos (cadr datal)))))
3433            (buffer-read-only nil)
3434            (level (gnus-summary-thread-level)))
3435       (gnus-delete-line)
3436       (gnus-summary-insert-line
3437        header level nil (gnus-article-mark article)
3438        (memq article gnus-newsgroup-replied)
3439        (memq article gnus-newsgroup-expirable)
3440        ;; Only insert the Subject string when it's different
3441        ;; from the previous Subject string.
3442        (if (and
3443             gnus-show-threads
3444             (gnus-subject-equal
3445              (condition-case ()
3446                  (mail-header-subject
3447                   (gnus-data-header
3448                    (cadr
3449                     (gnus-data-find-list
3450                      article
3451                      (gnus-data-list t)))))
3452                ;; Error on the side of excessive subjects.
3453                (error ""))
3454              (mail-header-subject header)))
3455            ""
3456          (mail-header-subject header))
3457        nil (cdr (assq article gnus-newsgroup-scored))
3458        (memq article gnus-newsgroup-processable))
3459       (when length
3460         (gnus-data-update-list
3461          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3462
3463 (defun gnus-summary-update-article (article &optional iheader)
3464   "Update ARTICLE in the summary buffer."
3465   (set-buffer gnus-summary-buffer)
3466   (let* ((header (gnus-summary-article-header article))
3467          (id (mail-header-id header))
3468          (data (gnus-data-find article))
3469          (thread (gnus-id-to-thread id))
3470          (references (mail-header-references header))
3471          (parent
3472           (gnus-id-to-thread
3473            (or (gnus-parent-id
3474                 (when (and references
3475                            (not (equal "" references)))
3476                   references))
3477                "none")))
3478          (buffer-read-only nil)
3479          (old (car thread)))
3480     (when thread
3481       (unless iheader
3482         (setcar thread nil)
3483         (when parent
3484           (delq thread parent)))
3485       (if (gnus-summary-insert-subject id header)
3486           ;; Set the (possibly) new article number in the data structure.
3487           (gnus-data-set-number data (gnus-id-to-article id))
3488         (setcar thread old)
3489         nil))))
3490
3491 (defun gnus-rebuild-thread (id &optional line)
3492   "Rebuild the thread containing ID.
3493 If LINE, insert the rebuilt thread starting on line LINE."
3494   (let ((buffer-read-only nil)
3495         old-pos current thread data)
3496     (if (not gnus-show-threads)
3497         (setq thread (list (car (gnus-id-to-thread id))))
3498       ;; Get the thread this article is part of.
3499       (setq thread (gnus-remove-thread id)))
3500     (setq old-pos (gnus-point-at-bol))
3501     (setq current (save-excursion
3502                     (and (zerop (forward-line -1))
3503                          (gnus-summary-article-number))))
3504     ;; If this is a gathered thread, we have to go some re-gathering.
3505     (when (stringp (car thread))
3506       (let ((subject (car thread))
3507             roots thr)
3508         (setq thread (cdr thread))
3509         (while thread
3510           (unless (memq (setq thr (gnus-id-to-thread
3511                                    (gnus-root-id
3512                                     (mail-header-id (caar thread)))))
3513                         roots)
3514             (push thr roots))
3515           (setq thread (cdr thread)))
3516         ;; We now have all (unique) roots.
3517         (if (= (length roots) 1)
3518             ;; All the loose roots are now one solid root.
3519             (setq thread (car roots))
3520           (setq thread (cons subject (gnus-sort-threads roots))))))
3521     (let (threads)
3522       ;; We then insert this thread into the summary buffer.
3523       (when line
3524         (goto-char (point-min))
3525         (forward-line (1- line)))
3526       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3527         (if gnus-show-threads
3528             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3529           (gnus-summary-prepare-unthreaded thread))
3530         (setq data (nreverse gnus-newsgroup-data))
3531         (setq threads gnus-newsgroup-threads))
3532       ;; We splice the new data into the data structure.
3533       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3534       ;;!!! then we want to insert at the beginning of the buffer.
3535       ;;!!! That happens to be true with Gnus now, but that may
3536       ;;!!! change in the future.  Perhaps.
3537       (gnus-data-enter-list
3538        (if line nil current) data (- (point) old-pos))
3539       (setq gnus-newsgroup-threads
3540             (nconc threads gnus-newsgroup-threads))
3541       (gnus-data-compute-positions))))
3542
3543 (defun gnus-number-to-header (number)
3544   "Return the header for article NUMBER."
3545   (let ((headers gnus-newsgroup-headers))
3546     (while (and headers
3547                 (not (= number (mail-header-number (car headers)))))
3548       (pop headers))
3549     (when headers
3550       (car headers))))
3551
3552 (defun gnus-parent-headers (in-headers &optional generation)
3553   "Return the headers of the GENERATIONeth parent of HEADERS."
3554   (unless generation
3555     (setq generation 1))
3556   (let ((parent t)
3557         (headers in-headers)
3558         references)
3559     (while (and parent
3560                 (not (zerop generation))
3561                 (setq references (mail-header-references headers)))
3562       (setq headers (if (and references
3563                              (setq parent (gnus-parent-id references)))
3564                         (car (gnus-id-to-thread parent))
3565                       nil))
3566       (decf generation))
3567     (and (not (eq headers in-headers))
3568          headers)))
3569
3570 (defun gnus-id-to-thread (id)
3571   "Return the (sub-)thread where ID appears."
3572   (gnus-gethash id gnus-newsgroup-dependencies))
3573
3574 (defun gnus-id-to-article (id)
3575   "Return the article number of ID."
3576   (let ((thread (gnus-id-to-thread id)))
3577     (when (and thread
3578                (car thread))
3579       (mail-header-number (car thread)))))
3580
3581 (defun gnus-id-to-header (id)
3582   "Return the article headers of ID."
3583   (car (gnus-id-to-thread id)))
3584
3585 (defun gnus-article-displayed-root-p (article)
3586   "Say whether ARTICLE is a root(ish) article."
3587   (let ((level (gnus-summary-thread-level article))
3588         (refs (mail-header-references  (gnus-summary-article-header article)))
3589         particle)
3590     (cond
3591      ((null level) nil)
3592      ((zerop level) t)
3593      ((null refs) t)
3594      ((null (gnus-parent-id refs)) t)
3595      ((and (= 1 level)
3596            (null (setq particle (gnus-id-to-article
3597                                  (gnus-parent-id refs))))
3598            (null (gnus-summary-thread-level particle)))))))
3599
3600 (defun gnus-root-id (id)
3601   "Return the id of the root of the thread where ID appears."
3602   (let (last-id prev)
3603     (while (and id (setq prev (car (gnus-id-to-thread id))))
3604       (setq last-id id
3605             id (gnus-parent-id (mail-header-references prev))))
3606     last-id))
3607
3608 (defun gnus-articles-in-thread (thread)
3609   "Return the list of articles in THREAD."
3610   (cons (mail-header-number (car thread))
3611         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3612
3613 (defun gnus-remove-thread (id &optional dont-remove)
3614   "Remove the thread that has ID in it."
3615   (let (headers thread last-id)
3616     ;; First go up in this thread until we find the root.
3617     (setq last-id (gnus-root-id id)
3618           headers (message-flatten-list (gnus-id-to-thread last-id)))
3619     ;; We have now found the real root of this thread.  It might have
3620     ;; been gathered into some loose thread, so we have to search
3621     ;; through the threads to find the thread we wanted.
3622     (let ((threads gnus-newsgroup-threads)
3623           sub)
3624       (while threads
3625         (setq sub (car threads))
3626         (if (stringp (car sub))
3627             ;; This is a gathered thread, so we look at the roots
3628             ;; below it to find whether this article is in this
3629             ;; gathered root.
3630             (progn
3631               (setq sub (cdr sub))
3632               (while sub
3633                 (when (member (caar sub) headers)
3634                   (setq thread (car threads)
3635                         threads nil
3636                         sub nil))
3637                 (setq sub (cdr sub))))
3638           ;; It's an ordinary thread, so we check it.
3639           (when (eq (car sub) (car headers))
3640             (setq thread sub
3641                   threads nil)))
3642         (setq threads (cdr threads)))
3643       ;; If this article is in no thread, then it's a root.
3644       (if thread
3645           (unless dont-remove
3646             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3647         (setq thread (gnus-id-to-thread last-id)))
3648       (when thread
3649         (prog1
3650             thread                      ; We return this thread.
3651           (unless dont-remove
3652             (if (stringp (car thread))
3653                 (progn
3654                   ;; If we use dummy roots, then we have to remove the
3655                   ;; dummy root as well.
3656                   (when (eq gnus-summary-make-false-root 'dummy)
3657                     ;; We go to the dummy root by going to
3658                     ;; the first sub-"thread", and then one line up.
3659                     (gnus-summary-goto-article
3660                      (mail-header-number (caadr thread)))
3661                     (forward-line -1)
3662                     (gnus-delete-line)
3663                     (gnus-data-compute-positions))
3664                   (setq thread (cdr thread))
3665                   (while thread
3666                     (gnus-remove-thread-1 (car thread))
3667                     (setq thread (cdr thread))))
3668               (gnus-remove-thread-1 thread))))))))
3669
3670 (defun gnus-remove-thread-1 (thread)
3671   "Remove the thread THREAD recursively."
3672   (let ((number (mail-header-number (pop thread)))
3673         d)
3674     (setq thread (reverse thread))
3675     (while thread
3676       (gnus-remove-thread-1 (pop thread)))
3677     (when (setq d (gnus-data-find number))
3678       (goto-char (gnus-data-pos d))
3679       (gnus-summary-show-thread)
3680       (gnus-data-remove
3681        number
3682        (- (gnus-point-at-bol)
3683           (prog1
3684               (1+ (gnus-point-at-eol))
3685             (gnus-delete-line)))))))
3686
3687 (defun gnus-sort-threads-1 (threads func)
3688   (sort (mapcar (lambda (thread)
3689                   (cons (car thread)
3690                         (and (cdr thread)
3691                              (gnus-sort-threads-1 (cdr thread) func))))
3692                 threads) func))
3693
3694 (defun gnus-sort-threads (threads)
3695   "Sort THREADS."
3696   (if (not gnus-thread-sort-functions)
3697       threads
3698     (gnus-message 8 "Sorting threads...")
3699     (prog1
3700         (gnus-sort-threads-1 
3701          threads 
3702          (gnus-make-sort-function gnus-thread-sort-functions))
3703       (gnus-message 8 "Sorting threads...done"))))
3704
3705 (defun gnus-sort-articles (articles)
3706   "Sort ARTICLES."
3707   (when gnus-article-sort-functions
3708     (gnus-message 7 "Sorting articles...")
3709     (prog1
3710         (setq gnus-newsgroup-headers
3711               (sort articles (gnus-make-sort-function
3712                               gnus-article-sort-functions)))
3713       (gnus-message 7 "Sorting articles...done"))))
3714
3715 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3716 (defmacro gnus-thread-header (thread)
3717   "Return header of first article in THREAD.
3718 Note that THREAD must never, ever be anything else than a variable -
3719 using some other form will lead to serious barfage."
3720   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3721   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3722   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3723         (vector thread) 2))
3724
3725 (defsubst gnus-article-sort-by-number (h1 h2)
3726   "Sort articles by article number."
3727   (< (mail-header-number h1)
3728      (mail-header-number h2)))
3729
3730 (defun gnus-thread-sort-by-number (h1 h2)
3731   "Sort threads by root article number."
3732   (gnus-article-sort-by-number
3733    (gnus-thread-header h1) (gnus-thread-header h2)))
3734
3735 (defsubst gnus-article-sort-by-lines (h1 h2)
3736   "Sort articles by article Lines header."
3737   (< (mail-header-lines h1)
3738      (mail-header-lines h2)))
3739
3740 (defun gnus-thread-sort-by-lines (h1 h2)
3741   "Sort threads by root article Lines header."
3742   (gnus-article-sort-by-lines
3743    (gnus-thread-header h1) (gnus-thread-header h2)))
3744
3745 (defsubst gnus-article-sort-by-chars (h1 h2)
3746   "Sort articles by octet length."
3747   (< (mail-header-chars h1)
3748      (mail-header-chars h2)))
3749
3750 (defun gnus-thread-sort-by-chars (h1 h2)
3751   "Sort threads by root article octet length."
3752   (gnus-article-sort-by-chars
3753    (gnus-thread-header h1) (gnus-thread-header h2)))
3754
3755 (defsubst gnus-article-sort-by-author (h1 h2)
3756   "Sort articles by root author."
3757   (string-lessp
3758    (let ((addr (car (mime-entity-read-field h1 'From))))
3759      (or (std11-full-name-string addr)
3760          (std11-address-string addr)
3761          ""))
3762    (let ((addr (car (mime-entity-read-field h2 'From))))
3763      (or (std11-full-name-string addr)
3764          (std11-address-string addr)
3765          ""))
3766    ))
3767
3768 (defun gnus-thread-sort-by-author (h1 h2)
3769   "Sort threads by root author."
3770   (gnus-article-sort-by-author
3771    (gnus-thread-header h1)  (gnus-thread-header h2)))
3772
3773 (defsubst gnus-article-sort-by-subject (h1 h2)
3774   "Sort articles by root subject."
3775   (string-lessp
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3777    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3778
3779 (defun gnus-thread-sort-by-subject (h1 h2)
3780   "Sort threads by root subject."
3781   (gnus-article-sort-by-subject
3782    (gnus-thread-header h1) (gnus-thread-header h2)))
3783
3784 (defsubst gnus-article-sort-by-date (h1 h2)
3785   "Sort articles by root article date."
3786   (time-less-p
3787    (gnus-date-get-time (mail-header-date h1))
3788    (gnus-date-get-time (mail-header-date h2))))
3789
3790 (defun gnus-thread-sort-by-date (h1 h2)
3791   "Sort threads by root article date."
3792   (gnus-article-sort-by-date
3793    (gnus-thread-header h1) (gnus-thread-header h2)))
3794
3795 (defsubst gnus-article-sort-by-score (h1 h2)
3796   "Sort articles by root article score.
3797 Unscored articles will be counted as having a score of zero."
3798   (> (or (cdr (assq (mail-header-number h1)
3799                     gnus-newsgroup-scored))
3800          gnus-summary-default-score 0)
3801      (or (cdr (assq (mail-header-number h2)
3802                     gnus-newsgroup-scored))
3803          gnus-summary-default-score 0)))
3804
3805 (defun gnus-thread-sort-by-score (h1 h2)
3806   "Sort threads by root article score."
3807   (gnus-article-sort-by-score
3808    (gnus-thread-header h1) (gnus-thread-header h2)))
3809
3810 (defun gnus-thread-sort-by-total-score (h1 h2)
3811   "Sort threads by the sum of all scores in the thread.
3812 Unscored articles will be counted as having a score of zero."
3813   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3814
3815 (defun gnus-thread-total-score (thread)
3816   ;; This function find the total score of THREAD.
3817   (cond ((null thread)
3818          0)
3819         ((consp thread)
3820          (if (stringp (car thread))
3821              (apply gnus-thread-score-function 0
3822                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3823            (gnus-thread-total-score-1 thread)))
3824         (t
3825          (gnus-thread-total-score-1 (list thread)))))
3826
3827 (defun gnus-thread-total-score-1 (root)
3828   ;; This function find the total score of the thread below ROOT.
3829   (setq root (car root))
3830   (apply gnus-thread-score-function
3831          (or (append
3832               (mapcar 'gnus-thread-total-score
3833                       (cdr (gnus-id-to-thread (mail-header-id root))))
3834               (when (> (mail-header-number root) 0)
3835                 (list (or (cdr (assq (mail-header-number root)
3836                                      gnus-newsgroup-scored))
3837                           gnus-summary-default-score 0))))
3838              (list gnus-summary-default-score)
3839              '(0))))
3840
3841 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3842 (defvar gnus-tmp-prev-subject nil)
3843 (defvar gnus-tmp-false-parent nil)
3844 (defvar gnus-tmp-root-expunged nil)
3845 (defvar gnus-tmp-dummy-line nil)
3846
3847 (defvar gnus-tmp-header)
3848 (defun gnus-extra-header (type &optional header)
3849   "Return the extra header of TYPE."
3850   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3851       ""))
3852
3853 (defun gnus-summary-prepare-threads (threads)
3854   "Prepare summary buffer from THREADS and indentation LEVEL.
3855 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3856 or a straight list of headers."
3857   (gnus-message 7 "Generating summary...")
3858
3859   (setq gnus-newsgroup-threads threads)
3860   (beginning-of-line)
3861
3862   (let ((gnus-tmp-level 0)
3863         (default-score (or gnus-summary-default-score 0))
3864         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3865         thread number subject stack state gnus-tmp-gathered beg-match
3866         new-roots gnus-tmp-new-adopts thread-end
3867         gnus-tmp-header gnus-tmp-unread
3868         gnus-tmp-replied gnus-tmp-subject-or-nil
3869         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3870         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3871         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3872
3873     (setq gnus-tmp-prev-subject nil)
3874
3875     (if (vectorp (car threads))
3876         ;; If this is a straight (sic) list of headers, then a
3877         ;; threaded summary display isn't required, so we just create
3878         ;; an unthreaded one.
3879         (gnus-summary-prepare-unthreaded threads)
3880
3881       ;; Do the threaded display.
3882
3883       (while (or threads stack gnus-tmp-new-adopts new-roots)
3884
3885         (if (and (= gnus-tmp-level 0)
3886                  (or (not stack)
3887                      (= (caar stack) 0))
3888                  (not gnus-tmp-false-parent)
3889                  (or gnus-tmp-new-adopts new-roots))
3890             (if gnus-tmp-new-adopts
3891                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3892                       thread (list (car gnus-tmp-new-adopts))
3893                       gnus-tmp-header (caar thread)
3894                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3895               (when new-roots
3896                 (setq thread (list (car new-roots))
3897                       gnus-tmp-header (caar thread)
3898                       new-roots (cdr new-roots))))
3899
3900           (if threads
3901               ;; If there are some threads, we do them before the
3902               ;; threads on the stack.
3903               (setq thread threads
3904                     gnus-tmp-header (caar thread))
3905             ;; There were no current threads, so we pop something off
3906             ;; the stack.
3907             (setq state (car stack)
3908                   gnus-tmp-level (car state)
3909                   thread (cdr state)
3910                   stack (cdr stack)
3911                   gnus-tmp-header (caar thread))))
3912
3913         (setq gnus-tmp-false-parent nil)
3914         (setq gnus-tmp-root-expunged nil)
3915         (setq thread-end nil)
3916
3917         (if (stringp gnus-tmp-header)
3918             ;; The header is a dummy root.
3919             (cond
3920              ((eq gnus-summary-make-false-root 'adopt)
3921               ;; We let the first article adopt the rest.
3922               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3923                                                (cddar thread)))
3924               (setq gnus-tmp-gathered
3925                     (nconc (mapcar
3926                             (lambda (h) (mail-header-number (car h)))
3927                             (cddar thread))
3928                            gnus-tmp-gathered))
3929               (setq thread (cons (list (caar thread)
3930                                        (cadar thread))
3931                                  (cdr thread)))
3932               (setq gnus-tmp-level -1
3933                     gnus-tmp-false-parent t))
3934              ((eq gnus-summary-make-false-root 'empty)
3935               ;; We print adopted articles with empty subject fields.
3936               (setq gnus-tmp-gathered
3937                     (nconc (mapcar
3938                             (lambda (h) (mail-header-number (car h)))
3939                             (cddar thread))
3940                            gnus-tmp-gathered))
3941               (setq gnus-tmp-level -1))
3942              ((eq gnus-summary-make-false-root 'dummy)
3943               ;; We remember that we probably want to output a dummy
3944               ;; root.
3945               (setq gnus-tmp-dummy-line gnus-tmp-header)
3946               (setq gnus-tmp-prev-subject gnus-tmp-header))
3947              (t
3948               ;; We do not make a root for the gathered
3949               ;; sub-threads at all.
3950               (setq gnus-tmp-level -1)))
3951
3952           (setq number (mail-header-number gnus-tmp-header)
3953                 subject (mail-header-subject gnus-tmp-header))
3954
3955           (cond
3956            ;; If the thread has changed subject, we might want to make
3957            ;; this subthread into a root.
3958            ((and (null gnus-thread-ignore-subject)
3959                  (not (zerop gnus-tmp-level))
3960                  gnus-tmp-prev-subject
3961                  (not (inline
3962                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3963             (setq new-roots (nconc new-roots (list (car thread)))
3964                   thread-end t
3965                   gnus-tmp-header nil))
3966            ;; If the article lies outside the current limit,
3967            ;; then we do not display it.
3968            ((not (memq number gnus-newsgroup-limit))
3969             (setq gnus-tmp-gathered
3970                   (nconc (mapcar
3971                           (lambda (h) (mail-header-number (car h)))
3972                           (cdar thread))
3973                          gnus-tmp-gathered))
3974             (setq gnus-tmp-new-adopts (if (cdar thread)
3975                                           (append gnus-tmp-new-adopts
3976                                                   (cdar thread))
3977                                         gnus-tmp-new-adopts)
3978                   thread-end t
3979                   gnus-tmp-header nil)
3980             (when (zerop gnus-tmp-level)
3981               (setq gnus-tmp-root-expunged t)))
3982            ;; Perhaps this article is to be marked as read?
3983            ((and gnus-summary-mark-below
3984                  (< (or (cdr (assq number gnus-newsgroup-scored))
3985                         default-score)
3986                     gnus-summary-mark-below)
3987                  ;; Don't touch sparse articles.
3988                  (not (gnus-summary-article-sparse-p number))
3989                  (not (gnus-summary-article-ancient-p number)))
3990             (setq gnus-newsgroup-unreads
3991                   (delq number gnus-newsgroup-unreads))
3992             (if gnus-newsgroup-auto-expire
3993                 (push number gnus-newsgroup-expirable)
3994               (push (cons number gnus-low-score-mark)
3995                     gnus-newsgroup-reads))))
3996
3997           (when gnus-tmp-header
3998             ;; We may have an old dummy line to output before this
3999             ;; article.
4000             (when (and gnus-tmp-dummy-line
4001                        (gnus-subject-equal
4002                         gnus-tmp-dummy-line
4003                         (mail-header-subject gnus-tmp-header)))
4004               (gnus-summary-insert-dummy-line
4005                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4006               (setq gnus-tmp-dummy-line nil))
4007
4008             ;; Compute the mark.
4009             (setq gnus-tmp-unread (gnus-article-mark number))
4010
4011             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4012                                   gnus-tmp-header gnus-tmp-level)
4013                   gnus-newsgroup-data)
4014
4015             ;; Actually insert the line.
4016             (setq
4017              gnus-tmp-subject-or-nil
4018              (cond
4019               ((and gnus-thread-ignore-subject
4020                     gnus-tmp-prev-subject
4021                     (not (inline (gnus-subject-equal
4022                                   gnus-tmp-prev-subject subject))))
4023                subject)
4024               ((zerop gnus-tmp-level)
4025                (if (and (eq gnus-summary-make-false-root 'empty)
4026                         (memq number gnus-tmp-gathered)
4027                         gnus-tmp-prev-subject
4028                         (inline (gnus-subject-equal
4029                                  gnus-tmp-prev-subject subject)))
4030                    gnus-summary-same-subject
4031                  subject))
4032               (t gnus-summary-same-subject)))
4033             (if (and (eq gnus-summary-make-false-root 'adopt)
4034                      (= gnus-tmp-level 1)
4035                      (memq number gnus-tmp-gathered))
4036                 (setq gnus-tmp-opening-bracket ?\<
4037                       gnus-tmp-closing-bracket ?\>)
4038               (setq gnus-tmp-opening-bracket ?\[
4039                     gnus-tmp-closing-bracket ?\]))
4040             (setq
4041              gnus-tmp-indentation
4042              (aref gnus-thread-indent-array gnus-tmp-level)
4043              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4044              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4045                                 gnus-summary-default-score 0)
4046              gnus-tmp-score-char
4047              (if (or (null gnus-summary-default-score)
4048                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4049                          gnus-summary-zcore-fuzz))
4050                  ?  ;Whitespace
4051                (if (< gnus-tmp-score gnus-summary-default-score)
4052                    gnus-score-below-mark gnus-score-over-mark))
4053              gnus-tmp-replied
4054              (cond ((memq number gnus-newsgroup-processable)
4055                     gnus-process-mark)
4056                    ((memq number gnus-newsgroup-cached)
4057                     gnus-cached-mark)
4058                    ((memq number gnus-newsgroup-replied)
4059                     gnus-replied-mark)
4060                    ((memq number gnus-newsgroup-saved)
4061                     gnus-saved-mark)
4062                    (t gnus-unread-mark))
4063              gnus-tmp-from (mail-header-from gnus-tmp-header)
4064              gnus-tmp-name
4065              (cond
4066               ((string-match "<[^>]+> *$" gnus-tmp-from)
4067                (setq beg-match (match-beginning 0))
4068                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4069                         (substring gnus-tmp-from (1+ (match-beginning 0))
4070                                    (1- (match-end 0))))
4071                    (substring gnus-tmp-from 0 beg-match)))
4072               ((string-match "(.+)" gnus-tmp-from)
4073                (substring gnus-tmp-from
4074                           (1+ (match-beginning 0)) (1- (match-end 0))))
4075               (t gnus-tmp-from)))
4076             (when (string= gnus-tmp-name "")
4077               (setq gnus-tmp-name gnus-tmp-from))
4078             (unless (numberp gnus-tmp-lines)
4079               (setq gnus-tmp-lines 0))
4080             (gnus-put-text-property
4081              (point)
4082              (progn (eval gnus-summary-line-format-spec) (point))
4083              'gnus-number number)
4084             (when gnus-visual-p
4085               (forward-line -1)
4086               (gnus-run-hooks 'gnus-summary-update-hook)
4087               (forward-line 1))
4088
4089             (setq gnus-tmp-prev-subject subject)))
4090
4091         (when (nth 1 thread)
4092           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4093         (incf gnus-tmp-level)
4094         (setq threads (if thread-end nil (cdar thread)))
4095         (unless threads
4096           (setq gnus-tmp-level 0)))))
4097   (gnus-message 7 "Generating summary...done"))
4098
4099 (defun gnus-summary-prepare-unthreaded (headers)
4100   "Generate an unthreaded summary buffer based on HEADERS."
4101   (let (header number mark)
4102
4103     (beginning-of-line)
4104
4105     (while headers
4106       ;; We may have to root out some bad articles...
4107       (when (memq (setq number (mail-header-number
4108                                 (setq header (pop headers))))
4109                   gnus-newsgroup-limit)
4110         ;; Mark article as read when it has a low score.
4111         (when (and gnus-summary-mark-below
4112                    (< (or (cdr (assq number gnus-newsgroup-scored))
4113                           gnus-summary-default-score 0)
4114                       gnus-summary-mark-below)
4115                    (not (gnus-summary-article-ancient-p number)))
4116           (setq gnus-newsgroup-unreads
4117                 (delq number gnus-newsgroup-unreads))
4118           (if gnus-newsgroup-auto-expire
4119               (push number gnus-newsgroup-expirable)
4120             (push (cons number gnus-low-score-mark)
4121                   gnus-newsgroup-reads)))
4122
4123         (setq mark (gnus-article-mark number))
4124         (push (gnus-data-make number mark (1+ (point)) header 0)
4125               gnus-newsgroup-data)
4126         (gnus-summary-insert-line
4127          header 0 number
4128          mark (memq number gnus-newsgroup-replied)
4129          (memq number gnus-newsgroup-expirable)
4130          (mail-header-subject header) nil
4131          (cdr (assq number gnus-newsgroup-scored))
4132          (memq number gnus-newsgroup-processable))))))
4133
4134 (defun gnus-summary-remove-list-identifiers ()
4135   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4136   (let ((regexp (if (stringp gnus-list-identifiers)
4137                     gnus-list-identifiers
4138                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4139     (dolist (header gnus-newsgroup-headers)
4140       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4141                                   " *\\)\\)+\\(Re: +\\)?\\)")
4142                           (mail-header-subject header))
4143         (mail-header-set-subject
4144          header (concat (substring (mail-header-subject header)
4145                                    0 (match-beginning 1))
4146                         (or
4147                          (match-string 3 (mail-header-subject header))
4148                          (match-string 5 (mail-header-subject header)))
4149                         (substring (mail-header-subject header)
4150                                    (match-end 1))))))))
4151
4152 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4153   "Select newsgroup GROUP.
4154 If READ-ALL is non-nil, all articles in the group are selected.
4155 If SELECT-ARTICLES, only select those articles from GROUP."
4156   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4157          ;;!!! Dirty hack; should be removed.
4158          (gnus-summary-ignore-duplicates
4159           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4160               t
4161             gnus-summary-ignore-duplicates))
4162          (info (nth 2 entry))
4163          articles fetched-articles cached)
4164
4165     (unless (gnus-check-server
4166              (setq gnus-current-select-method
4167                    (gnus-find-method-for-group group)))
4168       (error "Couldn't open server"))
4169
4170     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4171         (gnus-activate-group group)     ; Or we can activate it...
4172         (progn                          ; Or we bug out.
4173           (when (equal major-mode 'gnus-summary-mode)
4174             (kill-buffer (current-buffer)))
4175           (error "Couldn't request group %s: %s"
4176                  group (gnus-status-message group))))
4177
4178     (unless (gnus-request-group group t)
4179       (when (equal major-mode 'gnus-summary-mode)
4180         (kill-buffer (current-buffer)))
4181       (error "Couldn't request group %s: %s"
4182              group (gnus-status-message group)))
4183
4184     (setq gnus-newsgroup-name group)
4185     (setq gnus-newsgroup-unselected nil)
4186     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4187     (gnus-summary-setup-default-charset)
4188
4189     ;; Adjust and set lists of article marks.
4190     (when info
4191       (gnus-adjust-marked-articles info))
4192
4193     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4194     (setq cached
4195           (if (gnus-virtual-group-p group)
4196               gnus-newsgroup-cached
4197             (gnus-cache-articles-in-group group)))
4198
4199     (setq gnus-newsgroup-unreads
4200           (gnus-set-difference
4201            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4202            gnus-newsgroup-dormant))
4203
4204     (setq gnus-newsgroup-processable nil)
4205
4206     (gnus-update-read-articles group gnus-newsgroup-unreads)
4207
4208     (if (setq articles select-articles)
4209         (setq gnus-newsgroup-unselected
4210               (gnus-sorted-intersection
4211                gnus-newsgroup-unreads
4212                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4213       (setq articles (gnus-articles-to-read group read-all)))
4214
4215     (cond
4216      ((null articles)
4217       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4218       'quit)
4219      ((eq articles 0) nil)
4220      (t
4221       ;; Init the dependencies hash table.
4222       (setq gnus-newsgroup-dependencies
4223             (gnus-make-hashtable (length articles)))
4224       (gnus-set-global-variables)
4225       ;; Retrieve the headers and read them in.
4226       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4227       (setq gnus-newsgroup-headers
4228             (gnus-retrieve-parsed-headers
4229              articles gnus-newsgroup-name
4230              ;; We might want to fetch old headers, but
4231              ;; not if there is only 1 article.
4232              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4233                            (not (numberp gnus-fetch-old-headers)))
4234                       (> (length articles) 1))
4235                   gnus-fetch-old-headers)))
4236       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4237
4238       ;; Suppress duplicates?
4239       (when gnus-suppress-duplicates
4240         (gnus-dup-suppress-articles))
4241
4242       ;; Set the initial limit.
4243       (setq gnus-newsgroup-limit (copy-sequence articles))
4244       ;; Remove canceled articles from the list of unread articles.
4245       (setq gnus-newsgroup-unreads
4246             (gnus-set-sorted-intersection
4247              gnus-newsgroup-unreads
4248              (setq fetched-articles
4249                    (mapcar (lambda (headers) (mail-header-number headers))
4250                            gnus-newsgroup-headers))))
4251       ;; Removed marked articles that do not exist.
4252       (gnus-update-missing-marks
4253        (gnus-sorted-complement fetched-articles articles))
4254
4255       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4256       (when cached
4257         (setq gnus-newsgroup-cached cached))
4258
4259       ;; We might want to build some more threads first.
4260       (when (and gnus-fetch-old-headers
4261                  (eq gnus-headers-retrieved-by 'nov))
4262         (if (eq gnus-fetch-old-headers 'invisible)
4263             (gnus-build-all-threads)
4264           (gnus-build-old-threads)))
4265       ;; Let the Gnus agent mark articles as read.
4266       (when gnus-agent
4267         (gnus-agent-get-undownloaded-list))
4268       ;; Remove list identifiers from subject
4269       (when gnus-list-identifiers
4270         (gnus-summary-remove-list-identifiers))
4271       ;; Check whether auto-expire is to be done in this group.
4272       (setq gnus-newsgroup-auto-expire
4273             (gnus-group-auto-expirable-p group))
4274       ;; Set up the article buffer now, if necessary.
4275       (unless gnus-single-article-buffer
4276         (gnus-article-setup-buffer))
4277       ;; First and last article in this newsgroup.
4278       (when gnus-newsgroup-headers
4279         (setq gnus-newsgroup-begin
4280               (mail-header-number (car gnus-newsgroup-headers))
4281               gnus-newsgroup-end
4282               (mail-header-number
4283                (gnus-last-element gnus-newsgroup-headers))))
4284       ;; GROUP is successfully selected.
4285       (or gnus-newsgroup-headers t)))))
4286
4287 (defun gnus-articles-to-read (group &optional read-all)
4288   "Find out what articles the user wants to read."
4289   (let* ((articles
4290           ;; Select all articles if `read-all' is non-nil, or if there
4291           ;; are no unread articles.
4292           (if (or read-all
4293                   (and (zerop (length gnus-newsgroup-marked))
4294                        (zerop (length gnus-newsgroup-unreads)))
4295                   (eq (gnus-group-find-parameter group 'display)
4296                       'all))
4297               (or
4298                (gnus-uncompress-range (gnus-active group))
4299                (gnus-cache-articles-in-group group))
4300             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4301                           (copy-sequence gnus-newsgroup-unreads))
4302                   '<)))
4303          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4304          (scored (length scored-list))
4305          (number (length articles))
4306          (marked (+ (length gnus-newsgroup-marked)
4307                     (length gnus-newsgroup-dormant)))
4308          (select
4309           (cond
4310            ((numberp read-all)
4311             read-all)
4312            (t
4313             (condition-case ()
4314                 (cond
4315                  ((and (or (<= scored marked) (= scored number))
4316                        (natnump gnus-large-newsgroup)
4317                        (> number gnus-large-newsgroup))
4318                   (let* ((cursor-in-echo-area nil)
4319                          (input (read-from-minibuffer
4320                                  (format
4321                                   "How many articles from %s (max %d): "
4322                                   (gnus-limit-string gnus-newsgroup-name 35)
4323                                   number)
4324                                  (cons (number-to-string gnus-large-newsgroup)
4325                                        0))))
4326                     (if (string-match "^[ \t]*$" input)
4327                         number
4328                       input)))
4329                  ((and (> scored marked) (< scored number)
4330                        (> (- scored number) 20))
4331                   (let ((input
4332                          (read-string
4333                           (format "%s %s (%d scored, %d total): "
4334                                   "How many articles from"
4335                                   group scored number))))
4336                     (if (string-match "^[ \t]*$" input)
4337                         number input)))
4338                  (t number))
4339               (quit
4340                (message "Quit getting the articles to read")
4341                nil))))))
4342     (setq select (if (stringp select) (string-to-number select) select))
4343     (if (or (null select) (zerop select))
4344         select
4345       (if (and (not (zerop scored)) (<= (abs select) scored))
4346           (progn
4347             (setq articles (sort scored-list '<))
4348             (setq number (length articles)))
4349         (setq articles (copy-sequence articles)))
4350
4351       (when (< (abs select) number)
4352         (if (< select 0)
4353             ;; Select the N oldest articles.
4354             (setcdr (nthcdr (1- (abs select)) articles) nil)
4355           ;; Select the N most recent articles.
4356           (setq articles (nthcdr (- number select) articles))))
4357       (setq gnus-newsgroup-unselected
4358             (gnus-sorted-intersection
4359              gnus-newsgroup-unreads
4360              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4361       (when gnus-alter-articles-to-read-function
4362         (setq gnus-newsgroup-unreads
4363               (sort
4364                (funcall gnus-alter-articles-to-read-function
4365                         gnus-newsgroup-name gnus-newsgroup-unreads)
4366                '<)))
4367       articles)))
4368
4369 (defun gnus-killed-articles (killed articles)
4370   (let (out)
4371     (while articles
4372       (when (inline (gnus-member-of-range (car articles) killed))
4373         (push (car articles) out))
4374       (setq articles (cdr articles)))
4375     out))
4376
4377 (defun gnus-uncompress-marks (marks)
4378   "Uncompress the mark ranges in MARKS."
4379   (let ((uncompressed '(score bookmark))
4380         out)
4381     (while marks
4382       (if (memq (caar marks) uncompressed)
4383           (push (car marks) out)
4384         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4385       (setq marks (cdr marks)))
4386     out))
4387
4388 (defun gnus-adjust-marked-articles (info)
4389   "Set all article lists and remove all marks that are no longer valid."
4390   (let* ((marked-lists (gnus-info-marks info))
4391          (active (gnus-active (gnus-info-group info)))
4392          (min (car active))
4393          (max (cdr active))
4394          (types gnus-article-mark-lists)
4395          (uncompressed '(score bookmark killed))
4396          marks var articles article mark)
4397
4398     (while marked-lists
4399       (setq marks (pop marked-lists))
4400       (set (setq var (intern (format "gnus-newsgroup-%s"
4401                                      (car (rassq (setq mark (car marks))
4402                                                  types)))))
4403            (if (memq (car marks) uncompressed) (cdr marks)
4404              (gnus-uncompress-range (cdr marks))))
4405
4406       (setq articles (symbol-value var))
4407
4408       ;; All articles have to be subsets of the active articles.
4409       (cond
4410        ;; Adjust "simple" lists.
4411        ((memq mark '(tick dormant expire reply save))
4412         (while articles
4413           (when (or (< (setq article (pop articles)) min) (> article max))
4414             (set var (delq article (symbol-value var))))))
4415        ;; Adjust assocs.
4416        ((memq mark uncompressed)
4417         (when (not (listp (cdr (symbol-value var))))
4418           (set var (list (symbol-value var))))
4419         (when (not (listp (cdr articles)))
4420           (setq articles (list articles)))
4421         (while articles
4422           (when (or (not (consp (setq article (pop articles))))
4423                     (< (car article) min)
4424                     (> (car article) max))
4425             (set var (delq article (symbol-value var))))))))))
4426
4427 (defun gnus-update-missing-marks (missing)
4428   "Go through the list of MISSING articles and remove them from the mark lists."
4429   (when missing
4430     (let ((types gnus-article-mark-lists)
4431           var m)
4432       ;; Go through all types.
4433       (while types
4434         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4435         (when (symbol-value var)
4436           ;; This list has articles.  So we delete all missing articles
4437           ;; from it.
4438           (setq m missing)
4439           (while m
4440             (set var (delq (pop m) (symbol-value var)))))))))
4441
4442 (defun gnus-update-marks ()
4443   "Enter the various lists of marked articles into the newsgroup info list."
4444   (let ((types gnus-article-mark-lists)
4445         (info (gnus-get-info gnus-newsgroup-name))
4446         (uncompressed '(score bookmark killed))
4447         type list newmarked symbol delta-marks)
4448     (when info
4449       ;; Add all marks lists to the list of marks lists.
4450       (while (setq type (pop types))
4451         (setq list (symbol-value
4452                     (setq symbol
4453                           (intern (format "gnus-newsgroup-%s"
4454                                           (car type))))))
4455
4456         (when list
4457           ;; Get rid of the entries of the articles that have the
4458           ;; default score.
4459           (when (and (eq (cdr type) 'score)
4460                      gnus-save-score
4461                      list)
4462             (let* ((arts list)
4463                    (prev (cons nil list))
4464                    (all prev))
4465               (while arts
4466                 (if (or (not (consp (car arts)))
4467                         (= (cdar arts) gnus-summary-default-score))
4468                     (setcdr prev (cdr arts))
4469                   (setq prev arts))
4470                 (setq arts (cdr arts)))
4471               (setq list (cdr all)))))
4472
4473         (unless (memq (cdr type) uncompressed)
4474           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4475
4476         (when (gnus-check-backend-function
4477                'request-set-mark gnus-newsgroup-name)
4478           ;; propagate flags to server, with the following exceptions:
4479           ;; uncompressed:s are not proper flags (they are cons cells)
4480           ;; cache is a internal gnus flag
4481           ;; download are local to one gnus installation (well)
4482           ;; unsend are for nndraft groups only
4483           ;; xxx: generality of this?  this suits nnimap anyway
4484           (unless (memq (cdr type) (append '(cache download unsend)
4485                                            uncompressed))
4486             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4487                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4488                    (add (gnus-remove-from-range
4489                          (gnus-copy-sequence list) old)))
4490               (when add
4491                 (push (list add 'add (list (cdr type))) delta-marks))
4492               (when del
4493                 (push (list del 'del (list (cdr type))) delta-marks)))))
4494
4495         (when list
4496           (push (cons (cdr type) list) newmarked)))
4497
4498       (when delta-marks
4499         (unless (gnus-check-group gnus-newsgroup-name)
4500           (error "Can't open server for %s" gnus-newsgroup-name))
4501         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4502
4503       ;; Enter these new marks into the info of the group.
4504       (if (nthcdr 3 info)
4505           (setcar (nthcdr 3 info) newmarked)
4506         ;; Add the marks lists to the end of the info.
4507         (when newmarked
4508           (setcdr (nthcdr 2 info) (list newmarked))))
4509
4510       ;; Cut off the end of the info if there's nothing else there.
4511       (let ((i 5))
4512         (while (and (> i 2)
4513                     (not (nth i info)))
4514           (when (nthcdr (decf i) info)
4515             (setcdr (nthcdr i info) nil)))))))
4516
4517 (defun gnus-set-mode-line (where)
4518   "Set the mode line of the article or summary buffers.
4519 If WHERE is `summary', the summary mode line format will be used."
4520   ;; Is this mode line one we keep updated?
4521   (when (and (memq where gnus-updated-mode-lines)
4522              (symbol-value
4523               (intern (format "gnus-%s-mode-line-format-spec" where))))
4524     (let (mode-string)
4525       (save-excursion
4526         ;; We evaluate this in the summary buffer since these
4527         ;; variables are buffer-local to that buffer.
4528         (set-buffer gnus-summary-buffer)
4529         ;; We bind all these variables that are used in the `eval' form
4530         ;; below.
4531         (let* ((mformat (symbol-value
4532                          (intern
4533                           (format "gnus-%s-mode-line-format-spec" where))))
4534                (gnus-tmp-group-name (gnus-group-name-decode
4535                                      gnus-newsgroup-name
4536                                      (gnus-group-name-charset
4537                                       nil
4538                                       gnus-newsgroup-name)))
4539                (gnus-tmp-article-number (or gnus-current-article 0))
4540                (gnus-tmp-unread gnus-newsgroup-unreads)
4541                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4542                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4543                (gnus-tmp-unread-and-unselected
4544                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4545                             (zerop gnus-tmp-unselected))
4546                        "")
4547                       ((zerop gnus-tmp-unselected)
4548                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4549                       (t (format "{%d(+%d) more}"
4550                                  gnus-tmp-unread-and-unticked
4551                                  gnus-tmp-unselected))))
4552                (gnus-tmp-subject
4553                 (if (and gnus-current-headers
4554                          (vectorp gnus-current-headers))
4555                     (gnus-mode-string-quote
4556                      (mail-header-subject gnus-current-headers))
4557                   ""))
4558                bufname-length max-len
4559                gnus-tmp-header);; passed as argument to any user-format-funcs
4560           (setq mode-string (eval mformat))
4561           (setq bufname-length (if (string-match "%b" mode-string)
4562                                    (- (length
4563                                        (buffer-name
4564                                         (if (eq where 'summary)
4565                                             nil
4566                                           (get-buffer gnus-article-buffer))))
4567                                       2)
4568                                  0))
4569           (setq max-len (max 4 (if gnus-mode-non-string-length
4570                                    (- (window-width)
4571                                       gnus-mode-non-string-length
4572                                       bufname-length)
4573                                  (length mode-string))))
4574           ;; We might have to chop a bit of the string off...
4575           (when (> (length mode-string) max-len)
4576             (setq mode-string
4577                   (concat (gnus-truncate-string mode-string (- max-len 3))
4578                           "...")))
4579           ;; Pad the mode string a bit.
4580           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4581       ;; Update the mode line.
4582       (setq mode-line-buffer-identification
4583             (gnus-mode-line-buffer-identification (list mode-string)))
4584       (set-buffer-modified-p t))))
4585
4586 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4587   "Go through the HEADERS list and add all Xrefs to a hash table.
4588 The resulting hash table is returned, or nil if no Xrefs were found."
4589   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4590          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4591          (xref-hashtb (gnus-make-hashtable))
4592          start group entry number xrefs header)
4593     (while headers
4594       (setq header (pop headers))
4595       (when (and (setq xrefs (mail-header-xref header))
4596                  (not (memq (setq number (mail-header-number header))
4597                             unreads)))
4598         (setq start 0)
4599         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4600           (setq start (match-end 0))
4601           (setq group (if prefix
4602                           (concat prefix (substring xrefs (match-beginning 1)
4603                                                     (match-end 1)))
4604                         (substring xrefs (match-beginning 1) (match-end 1))))
4605           (setq number
4606                 (string-to-int (substring xrefs (match-beginning 2)
4607                                           (match-end 2))))
4608           (if (setq entry (gnus-gethash group xref-hashtb))
4609               (setcdr entry (cons number (cdr entry)))
4610             (gnus-sethash group (cons number nil) xref-hashtb)))))
4611     (and start xref-hashtb)))
4612
4613 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4614   "Look through all the headers and mark the Xrefs as read."
4615   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4616         name entry info xref-hashtb idlist method nth4)
4617     (save-excursion
4618       (set-buffer gnus-group-buffer)
4619       (when (setq xref-hashtb
4620                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4621         (mapatoms
4622          (lambda (group)
4623            (unless (string= from-newsgroup (setq name (symbol-name group)))
4624              (setq idlist (symbol-value group))
4625              ;; Dead groups are not updated.
4626              (and (prog1
4627                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4628                             info (nth 2 entry))
4629                     (when (stringp (setq nth4 (gnus-info-method info)))
4630                       (setq nth4 (gnus-server-to-method nth4))))
4631                   ;; Only do the xrefs if the group has the same
4632                   ;; select method as the group we have just read.
4633                   (or (gnus-methods-equal-p
4634                        nth4 (gnus-find-method-for-group from-newsgroup))
4635                       virtual
4636                       (equal nth4 (setq method (gnus-find-method-for-group
4637                                                 from-newsgroup)))
4638                       (and (equal (car nth4) (car method))
4639                            (equal (nth 1 nth4) (nth 1 method))))
4640                   gnus-use-cross-reference
4641                   (or (not (eq gnus-use-cross-reference t))
4642                       virtual
4643                       ;; Only do cross-references on subscribed
4644                       ;; groups, if that is what is wanted.
4645                       (<= (gnus-info-level info) gnus-level-subscribed))
4646                   (gnus-group-make-articles-read name idlist))))
4647          xref-hashtb)))))
4648
4649 (defun gnus-compute-read-articles (group articles)
4650   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4651          (info (nth 2 entry))
4652          (active (gnus-active group))
4653          ninfo)
4654     (when entry
4655       ;; First peel off all invalid article numbers.
4656       (when active
4657         (let ((ids articles)
4658               id first)
4659           (while (setq id (pop ids))
4660             (when (and first (> id (cdr active)))
4661               ;; We'll end up in this situation in one particular
4662               ;; obscure situation.  If you re-scan a group and get
4663               ;; a new article that is cross-posted to a different
4664               ;; group that has not been re-scanned, you might get
4665               ;; crossposted article that has a higher number than
4666               ;; Gnus believes possible.  So we re-activate this
4667               ;; group as well.  This might mean doing the
4668               ;; crossposting thingy will *increase* the number
4669               ;; of articles in some groups.  Tsk, tsk.
4670               (setq active (or (gnus-activate-group group) active)))
4671             (when (or (> id (cdr active))
4672                       (< id (car active)))
4673               (setq articles (delq id articles))))))
4674       ;; If the read list is nil, we init it.
4675       (if (and active
4676                (null (gnus-info-read info))
4677                (> (car active) 1))
4678           (setq ninfo (cons 1 (1- (car active))))
4679         (setq ninfo (gnus-info-read info)))
4680       ;; Then we add the read articles to the range.
4681       (gnus-add-to-range
4682        ninfo (setq articles (sort articles '<))))))
4683
4684 (defun gnus-group-make-articles-read (group articles)
4685   "Update the info of GROUP to say that ARTICLES are read."
4686   (let* ((num 0)
4687          (entry (gnus-gethash group gnus-newsrc-hashtb))
4688          (info (nth 2 entry))
4689          (active (gnus-active group))
4690          range)
4691     (when entry
4692       (setq range (gnus-compute-read-articles group articles))
4693       (save-excursion
4694         (set-buffer gnus-group-buffer)
4695         (gnus-undo-register
4696           `(progn
4697              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4698              (gnus-info-set-read ',info ',(gnus-info-read info))
4699              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4700              (gnus-group-update-group ,group t))))
4701       ;; Add the read articles to the range.
4702       (gnus-info-set-read info range)
4703       ;; Then we have to re-compute how many unread
4704       ;; articles there are in this group.
4705       (when active
4706         (cond
4707          ((not range)
4708           (setq num (- (1+ (cdr active)) (car active))))
4709          ((not (listp (cdr range)))
4710           (setq num (- (cdr active) (- (1+ (cdr range))
4711                                        (car range)))))
4712          (t
4713           (while range
4714             (if (numberp (car range))
4715                 (setq num (1+ num))
4716               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4717             (setq range (cdr range)))
4718           (setq num (- (cdr active) num))))
4719         ;; Update the number of unread articles.
4720         (setcar entry num)
4721         ;; Update the group buffer.
4722         (gnus-group-update-group group t)))))
4723
4724 (defvar gnus-newsgroup-none-id 0)
4725
4726 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4727   (let ((cur nntp-server-buffer)
4728         (dependencies
4729          (or dependencies
4730              (save-excursion (set-buffer gnus-summary-buffer)
4731                              gnus-newsgroup-dependencies)))
4732         headers id end ref
4733         (mail-parse-charset gnus-newsgroup-charset)
4734         (mail-parse-ignored-charsets
4735          (save-excursion (condition-case nil
4736                              (set-buffer gnus-summary-buffer)
4737                            (error))
4738                          gnus-newsgroup-ignored-charsets)))
4739     (save-excursion
4740       (set-buffer nntp-server-buffer)
4741       ;; Translate all TAB characters into SPACE characters.
4742       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4743       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4744       (gnus-run-hooks 'gnus-parse-headers-hook)
4745       (let ((case-fold-search t)
4746             in-reply-to header p lines chars ctype)
4747         (goto-char (point-min))
4748         ;; Search to the beginning of the next header.  Error messages
4749         ;; do not begin with 2 or 3.
4750         (while (re-search-forward "^[23][0-9]+ " nil t)
4751           (setq id nil
4752                 ref nil)
4753           ;; This implementation of this function, with nine
4754           ;; search-forwards instead of the one re-search-forward and
4755           ;; a case (which basically was the old function) is actually
4756           ;; about twice as fast, even though it looks messier.  You
4757           ;; can't have everything, I guess.  Speed and elegance
4758           ;; doesn't always go hand in hand.
4759           (setq
4760            header
4761            (make-full-mail-header
4762             ;; Number.
4763             (prog1
4764                 (read cur)
4765               (end-of-line)
4766               (setq p (point))
4767               (narrow-to-region (point)
4768                                 (or (and (search-forward "\n.\n" nil t)
4769                                          (- (point) 2))
4770                                     (point))))
4771             ;; Subject.
4772             (progn
4773               (goto-char p)
4774               (if (search-forward "\nsubject: " nil t)
4775                   (buffer-substring (match-end 0) (std11-field-end))
4776                 "(none)"))
4777             ;; From.
4778             (progn
4779               (goto-char p)
4780               (if (search-forward "\nfrom: " nil t)
4781                   (buffer-substring (match-end 0) (std11-field-end))
4782                 "(nobody)"))
4783             ;; Date.
4784             (progn
4785               (goto-char p)
4786               (if (search-forward "\ndate: " nil t)
4787                   (buffer-substring (match-end 0) (std11-field-end))
4788                 ""))
4789             ;; Message-ID.
4790             (progn
4791               (goto-char p)
4792               (setq id (if (re-search-forward
4793                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4794                            ;; We do it this way to make sure the Message-ID
4795                            ;; is (somewhat) syntactically valid.
4796                            (buffer-substring (match-beginning 1)
4797                                              (match-end 1))
4798                          ;; If there was no message-id, we just fake one
4799                          ;; to make subsequent routines simpler.
4800                          (nnheader-generate-fake-message-id))))
4801             ;; References.
4802             (progn
4803               (goto-char p)
4804               (if (search-forward "\nreferences: " nil t)
4805                   (progn
4806                     (setq end (point))
4807                     (prog1
4808                         (buffer-substring (match-end 0) (std11-field-end))
4809                       (setq ref
4810                             (buffer-substring
4811                              (progn
4812                                ;; (end-of-line)
4813                                (search-backward ">" end t)
4814                                (1+ (point)))
4815                              (progn
4816                                (search-backward "<" end t)
4817                                (point))))))
4818                 ;; Get the references from the in-reply-to header if there
4819                 ;; were no references and the in-reply-to header looks
4820                 ;; promising.
4821                 (if (and (search-forward "\nin-reply-to: " nil t)
4822                          (setq in-reply-to
4823                                (buffer-substring (match-end 0)
4824                                                  (std11-field-end)))
4825                          (string-match "<[^>]+>" in-reply-to))
4826                     (let (ref2)
4827                       (setq ref (substring in-reply-to (match-beginning 0)
4828                                            (match-end 0)))
4829                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4830                         (setq ref2 (substring in-reply-to (match-beginning 0)
4831                                               (match-end 0)))
4832                         (when (> (length ref2) (length ref))
4833                           (setq ref ref2)))
4834                       ref)
4835                   (setq ref nil))))
4836             ;; Chars.
4837             (progn
4838               (goto-char p)
4839               (if (search-forward "\nchars: " nil t)
4840                   (if (numberp (setq chars (ignore-errors (read cur))))
4841                       chars 0)
4842                 0))
4843             ;; Lines.
4844             (progn
4845               (goto-char p)
4846               (if (search-forward "\nlines: " nil t)
4847                   (if (numberp (setq lines (ignore-errors (read cur))))
4848                       lines 0)
4849                 0))
4850             ;; Xref.
4851             (progn
4852               (goto-char p)
4853               (and (search-forward "\nxref: " nil t)
4854                    (buffer-substring (match-end 0) (std11-field-end))))
4855             ;; Extra.
4856             (when gnus-extra-headers
4857               (let ((extra gnus-extra-headers)
4858                     out)
4859                 (while extra
4860                   (goto-char p)
4861                   (when (search-forward
4862                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4863                     (push (cons (car extra)
4864                                 (buffer-substring (match-end 0)
4865                                                   (std11-field-end)))
4866                           out))
4867                   (pop extra))
4868                 out))))
4869           (goto-char p)
4870           (if (and (search-forward "\ncontent-type: " nil t)
4871                    (setq ctype
4872                          (buffer-substring (match-end 0) (std11-field-end))))
4873               (mime-entity-set-content-type-internal
4874                header (mime-parse-Content-Type ctype)))
4875           (when (equal id ref)
4876             (setq ref nil))
4877
4878           (when gnus-alter-header-function
4879             (funcall gnus-alter-header-function header)
4880             (setq id (mail-header-id header)
4881                   ref (gnus-parent-id (mail-header-references header))))
4882
4883           (when (setq header
4884                       (gnus-dependencies-add-header
4885                        header dependencies force-new))
4886             (push header headers))
4887           (goto-char (point-max))
4888           (widen))
4889         (nreverse headers)))))
4890
4891 ;; Goes through the xover lines and returns a list of vectors
4892 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4893                                                   force-new dependencies
4894                                                   group also-fetch-heads)
4895   "Parse the news overview data in the server buffer.
4896 Return a list of headers that match SEQUENCE (see
4897 `nntp-retrieve-headers')."
4898   ;; Get the Xref when the users reads the articles since most/some
4899   ;; NNTP servers do not include Xrefs when using XOVER.
4900   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4901   (let ((mail-parse-charset gnus-newsgroup-charset)
4902         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4903         (cur nntp-server-buffer)
4904         (dependencies (or dependencies gnus-newsgroup-dependencies))
4905         number headers header)
4906     (save-excursion
4907       (set-buffer nntp-server-buffer)
4908       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4909       ;; Allow the user to mangle the headers before parsing them.
4910       (gnus-run-hooks 'gnus-parse-headers-hook)
4911       (goto-char (point-min))
4912       (while (not (eobp))
4913         (condition-case ()
4914             (while (and sequence (not (eobp)))
4915               (setq number (read cur))
4916               (while (and sequence
4917                           (< (car sequence) number))
4918                 (setq sequence (cdr sequence)))
4919               (and sequence
4920                    (eq number (car sequence))
4921                    (progn
4922                      (setq sequence (cdr sequence))
4923                      (setq header (inline
4924                                     (gnus-nov-parse-line
4925                                      number dependencies force-new))))
4926                    (push header headers))
4927               (forward-line 1))
4928           (error
4929            (gnus-error 4 "Strange nov line (%d)"
4930                        (count-lines (point-min) (point)))))
4931         (forward-line 1))
4932       ;; A common bug in inn is that if you have posted an article and
4933       ;; then retrieves the active file, it will answer correctly --
4934       ;; the new article is included.  However, a NOV entry for the
4935       ;; article may not have been generated yet, so this may fail.
4936       ;; We work around this problem by retrieving the last few
4937       ;; headers using HEAD.
4938       (if (or (not also-fetch-heads)
4939               (not sequence))
4940           ;; We (probably) got all the headers.
4941           (nreverse headers)
4942         (let ((gnus-nov-is-evil t))
4943           (nconc
4944            (nreverse headers)
4945            (gnus-retrieve-parsed-headers sequence group)
4946            ))))))
4947
4948 (defun gnus-article-get-xrefs ()
4949   "Fill in the Xref value in `gnus-current-headers', if necessary.
4950 This is meant to be called in `gnus-article-internal-prepare-hook'."
4951   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4952                                  gnus-current-headers)))
4953     (or (not gnus-use-cross-reference)
4954         (not headers)
4955         (and (mail-header-xref headers)
4956              (not (string= (mail-header-xref headers) "")))
4957         (let ((case-fold-search t)
4958               xref)
4959           (save-restriction
4960             (nnheader-narrow-to-headers)
4961             (goto-char (point-min))
4962             (when (or (and (not (eobp))
4963                            (eq (downcase (char-after)) ?x)
4964                            (looking-at "Xref:"))
4965                       (search-forward "\nXref:" nil t))
4966               (goto-char (1+ (match-end 0)))
4967               (setq xref (buffer-substring (point)
4968                                            (progn (end-of-line) (point))))
4969               (mail-header-set-xref headers xref)))))))
4970
4971 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4972   "Find article ID and insert the summary line for that article.
4973 OLD-HEADER can either be a header or a line number to insert
4974 the subject line on."
4975   (let* ((line (and (numberp old-header) old-header))
4976          (old-header (and (vectorp old-header) old-header))
4977          (header (cond ((and old-header use-old-header)
4978                         old-header)
4979                        ((and (numberp id)
4980                              (gnus-number-to-header id))
4981                         (gnus-number-to-header id))
4982                        (t
4983                         (gnus-read-header id))))
4984          (number (and (numberp id) id))
4985          d)
4986     (when header
4987       ;; Rebuild the thread that this article is part of and go to the
4988       ;; article we have fetched.
4989       (when (and (not gnus-show-threads)
4990                  old-header)
4991         (when (and number
4992                    (setq d (gnus-data-find (mail-header-number old-header))))
4993           (goto-char (gnus-data-pos d))
4994           (gnus-data-remove
4995            number
4996            (- (gnus-point-at-bol)
4997               (prog1
4998                   (1+ (gnus-point-at-eol))
4999                 (gnus-delete-line))))))
5000       (when old-header
5001         (mail-header-set-number header (mail-header-number old-header)))
5002       (setq gnus-newsgroup-sparse
5003             (delq (setq number (mail-header-number header))
5004                   gnus-newsgroup-sparse))
5005       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5006       (push number gnus-newsgroup-limit)
5007       (gnus-rebuild-thread (mail-header-id header) line)
5008       (gnus-summary-goto-subject number nil t))
5009     (when (and (numberp number)
5010                (> number 0))
5011       ;; We have to update the boundaries even if we can't fetch the
5012       ;; article if ID is a number -- so that the next `P' or `N'
5013       ;; command will fetch the previous (or next) article even
5014       ;; if the one we tried to fetch this time has been canceled.
5015       (when (> number gnus-newsgroup-end)
5016         (setq gnus-newsgroup-end number))
5017       (when (< number gnus-newsgroup-begin)
5018         (setq gnus-newsgroup-begin number))
5019       (setq gnus-newsgroup-unselected
5020             (delq number gnus-newsgroup-unselected)))
5021     ;; Report back a success?
5022     (and header (mail-header-number header))))
5023
5024 ;;; Process/prefix in the summary buffer
5025
5026 (defun gnus-summary-work-articles (n)
5027   "Return a list of articles to be worked upon.
5028 The prefix argument, the list of process marked articles, and the
5029 current article will be taken into consideration."
5030   (save-excursion
5031     (set-buffer gnus-summary-buffer)
5032     (cond
5033      (n
5034       ;; A numerical prefix has been given.
5035       (setq n (prefix-numeric-value n))
5036       (let ((backward (< n 0))
5037             (n (abs (prefix-numeric-value n)))
5038             articles article)
5039         (save-excursion
5040           (while
5041               (and (> n 0)
5042                    (push (setq article (gnus-summary-article-number))
5043                          articles)
5044                    (if backward
5045                        (gnus-summary-find-prev nil article)
5046                      (gnus-summary-find-next nil article)))
5047             (decf n)))
5048         (nreverse articles)))
5049      ((and (gnus-region-active-p) (mark))
5050       (message "region active")
5051       ;; Work on the region between point and mark.
5052       (let ((max (max (point) (mark)))
5053             articles article)
5054         (save-excursion
5055           (goto-char (min (point) (mark)))
5056           (while
5057               (and
5058                (push (setq article (gnus-summary-article-number)) articles)
5059                (gnus-summary-find-next nil article)
5060                (< (point) max)))
5061           (nreverse articles))))
5062      (gnus-newsgroup-processable
5063       ;; There are process-marked articles present.
5064       ;; Save current state.
5065       (gnus-summary-save-process-mark)
5066       ;; Return the list.
5067       (reverse gnus-newsgroup-processable))
5068      (t
5069       ;; Just return the current article.
5070       (list (gnus-summary-article-number))))))
5071
5072 (defmacro gnus-summary-iterate (arg &rest forms)
5073   "Iterate over the process/prefixed articles and do FORMS.
5074 ARG is the interactive prefix given to the command.  FORMS will be
5075 executed with point over the summary line of the articles."
5076   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5077     `(let ((,articles (gnus-summary-work-articles ,arg)))
5078        (while ,articles
5079          (gnus-summary-goto-subject (car ,articles))
5080          ,@forms
5081          (pop ,articles)))))
5082
5083 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5084 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5085
5086 (defun gnus-summary-save-process-mark ()
5087   "Push the current set of process marked articles on the stack."
5088   (interactive)
5089   (push (copy-sequence gnus-newsgroup-processable)
5090         gnus-newsgroup-process-stack))
5091
5092 (defun gnus-summary-kill-process-mark ()
5093   "Push the current set of process marked articles on the stack and unmark."
5094   (interactive)
5095   (gnus-summary-save-process-mark)
5096   (gnus-summary-unmark-all-processable))
5097
5098 (defun gnus-summary-yank-process-mark ()
5099   "Pop the last process mark state off the stack and restore it."
5100   (interactive)
5101   (unless gnus-newsgroup-process-stack
5102     (error "Empty mark stack"))
5103   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5104
5105 (defun gnus-summary-process-mark-set (set)
5106   "Make SET into the current process marked articles."
5107   (gnus-summary-unmark-all-processable)
5108   (while set
5109     (gnus-summary-set-process-mark (pop set))))
5110
5111 ;;; Searching and stuff
5112
5113 (defun gnus-summary-search-group (&optional backward use-level)
5114   "Search for next unread newsgroup.
5115 If optional argument BACKWARD is non-nil, search backward instead."
5116   (save-excursion
5117     (set-buffer gnus-group-buffer)
5118     (when (gnus-group-search-forward
5119            backward nil (if use-level (gnus-group-group-level) nil))
5120       (gnus-group-group-name))))
5121
5122 (defun gnus-summary-best-group (&optional exclude-group)
5123   "Find the name of the best unread group.
5124 If EXCLUDE-GROUP, do not go to this group."
5125   (save-excursion
5126     (set-buffer gnus-group-buffer)
5127     (save-excursion
5128       (gnus-group-best-unread-group exclude-group))))
5129
5130 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5131   (if backward (gnus-summary-find-prev)
5132     (let* ((dummy (gnus-summary-article-intangible-p))
5133            (article (or article (gnus-summary-article-number)))
5134            (arts (gnus-data-find-list article))
5135            result)
5136       (when (and (not dummy)
5137                  (or (not gnus-summary-check-current)
5138                      (not unread)
5139                      (not (gnus-data-unread-p (car arts)))))
5140         (setq arts (cdr arts)))
5141       (when (setq result
5142                   (if unread
5143                       (progn
5144                         (while arts
5145                           (when (or (and undownloaded
5146                                          (eq gnus-undownloaded-mark
5147                                              (gnus-data-mark (car arts))))
5148                                     (gnus-data-unread-p (car arts)))
5149                             (setq result (car arts)
5150                                   arts nil))
5151                           (setq arts (cdr arts)))
5152                         result)
5153                     (car arts)))
5154         (goto-char (gnus-data-pos result))
5155         (gnus-data-number result)))))
5156
5157 (defun gnus-summary-find-prev (&optional unread article)
5158   (let* ((eobp (eobp))
5159          (article (or article (gnus-summary-article-number)))
5160          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5161          result)
5162     (when (and (not eobp)
5163                (or (not gnus-summary-check-current)
5164                    (not unread)
5165                    (not (gnus-data-unread-p (car arts)))))
5166       (setq arts (cdr arts)))
5167     (when (setq result
5168                 (if unread
5169                     (progn
5170                       (while arts
5171                         (when (gnus-data-unread-p (car arts))
5172                           (setq result (car arts)
5173                                 arts nil))
5174                         (setq arts (cdr arts)))
5175                       result)
5176                   (car arts)))
5177       (goto-char (gnus-data-pos result))
5178       (gnus-data-number result))))
5179
5180 (defun gnus-summary-find-subject (subject &optional unread backward article)
5181   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5182          (article (or article (gnus-summary-article-number)))
5183          (articles (gnus-data-list backward))
5184          (arts (gnus-data-find-list article articles))
5185          result)
5186     (when (or (not gnus-summary-check-current)
5187               (not unread)
5188               (not (gnus-data-unread-p (car arts))))
5189       (setq arts (cdr arts)))
5190     (while arts
5191       (and (or (not unread)
5192                (gnus-data-unread-p (car arts)))
5193            (vectorp (gnus-data-header (car arts)))
5194            (gnus-subject-equal
5195             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5196            (setq result (car arts)
5197                  arts nil))
5198       (setq arts (cdr arts)))
5199     (and result
5200          (goto-char (gnus-data-pos result))
5201          (gnus-data-number result))))
5202
5203 (defun gnus-summary-search-forward (&optional unread subject backward)
5204   "Search forward for an article.
5205 If UNREAD, look for unread articles.  If SUBJECT, look for
5206 articles with that subject.  If BACKWARD, search backward instead."
5207   (cond (subject (gnus-summary-find-subject subject unread backward))
5208         (backward (gnus-summary-find-prev unread))
5209         (t (gnus-summary-find-next unread))))
5210
5211 (defun gnus-recenter (&optional n)
5212   "Center point in window and redisplay frame.
5213 Also do horizontal recentering."
5214   (interactive "P")
5215   (when (and gnus-auto-center-summary
5216              (not (eq gnus-auto-center-summary 'vertical)))
5217     (gnus-horizontal-recenter))
5218   (recenter n))
5219
5220 (defun gnus-summary-recenter ()
5221   "Center point in the summary window.
5222 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5223 displayed, no centering will be performed."
5224   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5225   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5226   (interactive)
5227   (let* ((top (cond ((< (window-height) 4) 0)
5228                     ((< (window-height) 7) 1)
5229                     (t (if (numberp gnus-auto-center-summary)
5230                            gnus-auto-center-summary
5231                          2))))
5232          (height (1- (window-height)))
5233          (bottom (save-excursion (goto-char (point-max))
5234                                  (forward-line (- height))
5235                                  (point)))
5236          (window (get-buffer-window (current-buffer))))
5237     ;; The user has to want it.
5238     (when gnus-auto-center-summary
5239       (when (get-buffer-window gnus-article-buffer)
5240         ;; Only do recentering when the article buffer is displayed,
5241         ;; Set the window start to either `bottom', which is the biggest
5242         ;; possible valid number, or the second line from the top,
5243         ;; whichever is the least.
5244         (set-window-start
5245          window (min bottom (save-excursion
5246                               (forward-line (- top)) (point)))
5247          t))
5248       ;; Do horizontal recentering while we're at it.
5249       (when (and (get-buffer-window (current-buffer) t)
5250                  (not (eq gnus-auto-center-summary 'vertical)))
5251         (let ((selected (selected-window)))
5252           (select-window (get-buffer-window (current-buffer) t))
5253           (gnus-summary-position-point)
5254           (gnus-horizontal-recenter)
5255           (select-window selected))))))
5256
5257 (defun gnus-summary-jump-to-group (newsgroup)
5258   "Move point to NEWSGROUP in group mode buffer."
5259   ;; Keep update point of group mode buffer if visible.
5260   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5261       (save-window-excursion
5262         ;; Take care of tree window mode.
5263         (when (get-buffer-window gnus-group-buffer)
5264           (pop-to-buffer gnus-group-buffer))
5265         (gnus-group-jump-to-group newsgroup))
5266     (save-excursion
5267       ;; Take care of tree window mode.
5268       (if (get-buffer-window gnus-group-buffer)
5269           (pop-to-buffer gnus-group-buffer)
5270         (set-buffer gnus-group-buffer))
5271       (gnus-group-jump-to-group newsgroup))))
5272
5273 ;; This function returns a list of article numbers based on the
5274 ;; difference between the ranges of read articles in this group and
5275 ;; the range of active articles.
5276 (defun gnus-list-of-unread-articles (group)
5277   (let* ((read (gnus-info-read (gnus-get-info group)))
5278          (active (or (gnus-active group) (gnus-activate-group group)))
5279          (last (cdr active))
5280          first nlast unread)
5281     ;; If none are read, then all are unread.
5282     (if (not read)
5283         (setq first (car active))
5284       ;; If the range of read articles is a single range, then the
5285       ;; first unread article is the article after the last read
5286       ;; article.  Sounds logical, doesn't it?
5287       (if (and (not (listp (cdr read)))
5288                (or (< (car read) (car active))
5289                    (progn (setq read (list read))
5290                           nil)))
5291           (setq first (max (car active) (1+ (cdr read))))
5292         ;; `read' is a list of ranges.
5293         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5294                                   (caar read)))
5295                   1)
5296           (setq first (car active)))
5297         (while read
5298           (when first
5299             (while (< first nlast)
5300               (push first unread)
5301               (setq first (1+ first))))
5302           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5303           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5304           (setq read (cdr read)))))
5305     ;; And add the last unread articles.
5306     (while (<= first last)
5307       (push first unread)
5308       (setq first (1+ first)))
5309     ;; Return the list of unread articles.
5310     (delq 0 (nreverse unread))))
5311
5312 (defun gnus-list-of-read-articles (group)
5313   "Return a list of unread, unticked and non-dormant articles."
5314   (let* ((info (gnus-get-info group))
5315          (marked (gnus-info-marks info))
5316          (active (gnus-active group)))
5317     (and info active
5318          (gnus-set-difference
5319           (gnus-sorted-complement
5320            (gnus-uncompress-range active)
5321            (gnus-list-of-unread-articles group))
5322           (append
5323            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5324            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5325
5326 ;; Various summary commands
5327
5328 (defun gnus-summary-select-article-buffer ()
5329   "Reconfigure windows to show article buffer."
5330   (interactive)
5331   (if (not (gnus-buffer-live-p gnus-article-buffer))
5332       (error "There is no article buffer for this summary buffer")
5333     (gnus-configure-windows 'article)
5334     (select-window (get-buffer-window gnus-article-buffer))))
5335
5336 (defun gnus-summary-universal-argument (arg)
5337   "Perform any operation on all articles that are process/prefixed."
5338   (interactive "P")
5339   (let ((articles (gnus-summary-work-articles arg))
5340         func article)
5341     (if (eq
5342          (setq
5343           func
5344           (key-binding
5345            (read-key-sequence
5346             (substitute-command-keys
5347              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5348          'undefined)
5349         (gnus-error 1 "Undefined key")
5350       (save-excursion
5351         (while articles
5352           (gnus-summary-goto-subject (setq article (pop articles)))
5353           (let (gnus-newsgroup-processable)
5354             (command-execute func))
5355           (gnus-summary-remove-process-mark article)))))
5356   (gnus-summary-position-point))
5357
5358 (defun gnus-summary-toggle-truncation (&optional arg)
5359   "Toggle truncation of summary lines.
5360 With arg, turn line truncation on iff arg is positive."
5361   (interactive "P")
5362   (setq truncate-lines
5363         (if (null arg) (not truncate-lines)
5364           (> (prefix-numeric-value arg) 0)))
5365   (redraw-display))
5366
5367 (defun gnus-summary-reselect-current-group (&optional all rescan)
5368   "Exit and then reselect the current newsgroup.
5369 The prefix argument ALL means to select all articles."
5370   (interactive "P")
5371   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5372     (error "Ephemeral groups can't be reselected"))
5373   (let ((current-subject (gnus-summary-article-number))
5374         (group gnus-newsgroup-name))
5375     (setq gnus-newsgroup-begin nil)
5376     (gnus-summary-exit)
5377     ;; We have to adjust the point of group mode buffer because
5378     ;; point was moved to the next unread newsgroup by exiting.
5379     (gnus-summary-jump-to-group group)
5380     (when rescan
5381       (save-excursion
5382         (save-window-excursion
5383           ;; Don't show group contents.
5384           (set-window-start (selected-window) (point-max))
5385           (gnus-group-get-new-news-this-group 1))))
5386     (gnus-group-read-group all t)
5387     (gnus-summary-goto-subject current-subject nil t)))
5388
5389 (defun gnus-summary-rescan-group (&optional all)
5390   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5391   (interactive "P")
5392   (gnus-summary-reselect-current-group all t))
5393
5394 (defun gnus-summary-update-info (&optional non-destructive)
5395   (save-excursion
5396     (let ((group gnus-newsgroup-name))
5397       (when group
5398         (when gnus-newsgroup-kill-headers
5399           (setq gnus-newsgroup-killed
5400                 (gnus-compress-sequence
5401                  (nconc
5402                   (gnus-set-sorted-intersection
5403                    (gnus-uncompress-range gnus-newsgroup-killed)
5404                    (setq gnus-newsgroup-unselected
5405                          (sort gnus-newsgroup-unselected '<)))
5406                   (setq gnus-newsgroup-unreads
5407                         (sort gnus-newsgroup-unreads '<)))
5408                  t)))
5409         (unless (listp (cdr gnus-newsgroup-killed))
5410           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5411         (let ((headers gnus-newsgroup-headers))
5412           ;; Set the new ranges of read articles.
5413           (save-excursion
5414             (set-buffer gnus-group-buffer)
5415             (gnus-undo-force-boundary))
5416           (gnus-update-read-articles
5417            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5418           ;; Set the current article marks.
5419           (let ((gnus-newsgroup-scored
5420                  (if (and (not gnus-save-score)
5421                           (not non-destructive))
5422                      nil
5423                    gnus-newsgroup-scored)))
5424             (save-excursion
5425               (gnus-update-marks)))
5426           ;; Do the cross-ref thing.
5427           (when gnus-use-cross-reference
5428             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5429           ;; Do not switch windows but change the buffer to work.
5430           (set-buffer gnus-group-buffer)
5431           (unless (gnus-ephemeral-group-p group)
5432             (gnus-group-update-group group)))))))
5433
5434 (defun gnus-summary-save-newsrc (&optional force)
5435   "Save the current number of read/marked articles in the dribble buffer.
5436 The dribble buffer will then be saved.
5437 If FORCE (the prefix), also save the .newsrc file(s)."
5438   (interactive "P")
5439   (gnus-summary-update-info t)
5440   (if force
5441       (gnus-save-newsrc-file)
5442     (gnus-dribble-save)))
5443
5444 (defun gnus-summary-exit (&optional temporary)
5445   "Exit reading current newsgroup, and then return to group selection mode.
5446 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5447   (interactive)
5448   (gnus-set-global-variables)
5449   (gnus-kill-save-kill-buffer)
5450   (gnus-async-halt-prefetch)
5451   (let* ((group gnus-newsgroup-name)
5452          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5453          (mode major-mode)
5454          (group-point nil)
5455          (buf (current-buffer)))
5456     (unless quit-config
5457       ;; Do adaptive scoring, and possibly save score files.
5458       (when gnus-newsgroup-adaptive
5459         (gnus-score-adaptive))
5460       (when gnus-use-scoring
5461         (gnus-score-save)))
5462     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5463     ;; If we have several article buffers, we kill them at exit.
5464     (unless gnus-single-article-buffer
5465       (gnus-kill-buffer gnus-original-article-buffer)
5466       (setq gnus-article-current nil))
5467     (when gnus-use-cache
5468       (gnus-cache-possibly-remove-articles)
5469       (gnus-cache-save-buffers))
5470     (gnus-async-prefetch-remove-group group)
5471     (when gnus-suppress-duplicates
5472       (gnus-dup-enter-articles))
5473     (when gnus-use-trees
5474       (gnus-tree-close group))
5475     (when gnus-use-cache
5476       (gnus-cache-write-active))
5477     ;; Remove entries for this group.
5478     (nnmail-purge-split-history (gnus-group-real-name group))
5479     ;; Make all changes in this group permanent.
5480     (unless quit-config
5481       (gnus-run-hooks 'gnus-exit-group-hook)
5482       (gnus-summary-update-info))
5483     (gnus-close-group group)
5484     ;; Make sure where we were, and go to next newsgroup.
5485     (set-buffer gnus-group-buffer)
5486     (unless quit-config
5487       (gnus-group-jump-to-group group))
5488     (gnus-run-hooks 'gnus-summary-exit-hook)
5489     (unless (or quit-config
5490                 ;; If this group has disappeared from the summary
5491                 ;; buffer, don't skip forwards.
5492                 (not (string= group (gnus-group-group-name))))
5493       (gnus-group-next-unread-group 1))
5494     (setq group-point (point))
5495     (if temporary
5496         nil                             ;Nothing to do.
5497       ;; If we have several article buffers, we kill them at exit.
5498       (unless gnus-single-article-buffer
5499         (gnus-kill-buffer gnus-article-buffer)
5500         (gnus-kill-buffer gnus-original-article-buffer)
5501         (setq gnus-article-current nil))
5502       (set-buffer buf)
5503       (if (not gnus-kill-summary-on-exit)
5504           (gnus-deaden-summary)
5505         ;; We set all buffer-local variables to nil.  It is unclear why
5506         ;; this is needed, but if we don't, buffer-local variables are
5507         ;; not garbage-collected, it seems.  This would the lead to en
5508         ;; ever-growing Emacs.
5509         (gnus-summary-clear-local-variables)
5510         (when (get-buffer gnus-article-buffer)
5511           (bury-buffer gnus-article-buffer))
5512         ;; We clear the global counterparts of the buffer-local
5513         ;; variables as well, just to be on the safe side.
5514         (set-buffer gnus-group-buffer)
5515         (gnus-summary-clear-local-variables)
5516         ;; Return to group mode buffer.
5517         (when (eq mode 'gnus-summary-mode)
5518           (gnus-kill-buffer buf)))
5519       (setq gnus-current-select-method gnus-select-method)
5520       (pop-to-buffer gnus-group-buffer)
5521       (if (not quit-config)
5522           (progn
5523             (goto-char group-point)
5524             (gnus-configure-windows 'group 'force)
5525             (unless (pos-visible-in-window-p)
5526               (forward-line (/ (static-if (featurep 'xemacs)
5527                                    (window-displayed-height)
5528                                  (1- (window-height)))
5529                                -2))
5530               (set-window-start (selected-window) (point))
5531               (goto-char group-point)))
5532         (gnus-handle-ephemeral-exit quit-config))
5533       ;; Clear the current group name.
5534       (unless quit-config
5535         (setq gnus-newsgroup-name nil)))))
5536
5537 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5538 (defun gnus-summary-exit-no-update (&optional no-questions)
5539   "Quit reading current newsgroup without updating read article info."
5540   (interactive)
5541   (let* ((group gnus-newsgroup-name)
5542          (quit-config (gnus-group-quit-config group)))
5543     (when (or no-questions
5544               gnus-expert-user
5545               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5546       (gnus-async-halt-prefetch)
5547       (mapcar 'funcall
5548               (delq 'gnus-summary-expire-articles
5549                     (copy-sequence gnus-summary-prepare-exit-hook)))
5550       ;; If we have several article buffers, we kill them at exit.
5551       (unless gnus-single-article-buffer
5552         (gnus-kill-buffer gnus-article-buffer)
5553         (gnus-kill-buffer gnus-original-article-buffer)
5554         (setq gnus-article-current nil))
5555       (if (not gnus-kill-summary-on-exit)
5556           (gnus-deaden-summary)
5557         (gnus-close-group group)
5558         (gnus-summary-clear-local-variables)
5559         (set-buffer gnus-group-buffer)
5560         (gnus-summary-clear-local-variables)
5561         (when (get-buffer gnus-summary-buffer)
5562           (kill-buffer gnus-summary-buffer)))
5563       (unless gnus-single-article-buffer
5564         (setq gnus-article-current nil))
5565       (when gnus-use-trees
5566         (gnus-tree-close group))
5567       (gnus-async-prefetch-remove-group group)
5568       (when (get-buffer gnus-article-buffer)
5569         (bury-buffer gnus-article-buffer))
5570       ;; Return to the group buffer.
5571       (gnus-configure-windows 'group 'force)
5572       ;; Clear the current group name.
5573       (setq gnus-newsgroup-name nil)
5574       (when (equal (gnus-group-group-name) group)
5575         (gnus-group-next-unread-group 1))
5576       (when quit-config
5577         (gnus-handle-ephemeral-exit quit-config)))))
5578
5579 (defun gnus-handle-ephemeral-exit (quit-config)
5580   "Handle movement when leaving an ephemeral group.
5581 The state which existed when entering the ephemeral is reset."
5582   (if (not (buffer-name (car quit-config)))
5583       (gnus-configure-windows 'group 'force)
5584     (set-buffer (car quit-config))
5585     (cond ((eq major-mode 'gnus-summary-mode)
5586            (gnus-set-global-variables))
5587           ((eq major-mode 'gnus-article-mode)
5588            (save-excursion
5589              ;; The `gnus-summary-buffer' variable may point
5590              ;; to the old summary buffer when using a single
5591              ;; article buffer.
5592              (unless (gnus-buffer-live-p gnus-summary-buffer)
5593                (set-buffer gnus-group-buffer))
5594              (set-buffer gnus-summary-buffer)
5595              (gnus-set-global-variables))))
5596     (if (or (eq (cdr quit-config) 'article)
5597             (eq (cdr quit-config) 'pick))
5598         (progn
5599           ;; The current article may be from the ephemeral group
5600           ;; thus it is best that we reload this article
5601           (gnus-summary-show-article)
5602           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5603               (gnus-configure-windows 'pick 'force)
5604             (gnus-configure-windows (cdr quit-config) 'force)))
5605       (gnus-configure-windows (cdr quit-config) 'force))
5606     (when (eq major-mode 'gnus-summary-mode)
5607       (gnus-summary-next-subject 1 nil t)
5608       (gnus-summary-recenter)
5609       (gnus-summary-position-point))))
5610
5611 (defun gnus-summary-preview-mime-message ()
5612   "MIME decode and play this message."
5613   (interactive)
5614   (let ((gnus-break-pages nil)
5615         (gnus-show-mime t))
5616     (gnus-summary-select-article gnus-show-all-headers t))
5617   (select-window (get-buffer-window gnus-article-buffer)))
5618
5619 ;;; Dead summaries.
5620
5621 (defvar gnus-dead-summary-mode-map nil)
5622
5623 (unless gnus-dead-summary-mode-map
5624   (setq gnus-dead-summary-mode-map (make-keymap))
5625   (suppress-keymap gnus-dead-summary-mode-map)
5626   (substitute-key-definition
5627    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5628   (let ((keys '("\C-d" "\r" "\177" [delete])))
5629     (while keys
5630       (define-key gnus-dead-summary-mode-map
5631         (pop keys) 'gnus-summary-wake-up-the-dead))))
5632
5633 (defvar gnus-dead-summary-mode nil
5634   "Minor mode for Gnus summary buffers.")
5635
5636 (defun gnus-dead-summary-mode (&optional arg)
5637   "Minor mode for Gnus summary buffers."
5638   (interactive "P")
5639   (when (eq major-mode 'gnus-summary-mode)
5640     (make-local-variable 'gnus-dead-summary-mode)
5641     (setq gnus-dead-summary-mode
5642           (if (null arg) (not gnus-dead-summary-mode)
5643             (> (prefix-numeric-value arg) 0)))
5644     (when gnus-dead-summary-mode
5645       (gnus-add-minor-mode
5646        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5647
5648 (defun gnus-deaden-summary ()
5649   "Make the current summary buffer into a dead summary buffer."
5650   ;; Kill any previous dead summary buffer.
5651   (when (and gnus-dead-summary
5652              (buffer-name gnus-dead-summary))
5653     (save-excursion
5654       (set-buffer gnus-dead-summary)
5655       (when gnus-dead-summary-mode
5656         (kill-buffer (current-buffer)))))
5657   ;; Make this the current dead summary.
5658   (setq gnus-dead-summary (current-buffer))
5659   (gnus-dead-summary-mode 1)
5660   (let ((name (buffer-name)))
5661     (when (string-match "Summary" name)
5662       (rename-buffer
5663        (concat (substring name 0 (match-beginning 0)) "Dead "
5664                (substring name (match-beginning 0)))
5665        t)
5666       (bury-buffer))))
5667
5668 (defun gnus-kill-or-deaden-summary (buffer)
5669   "Kill or deaden the summary BUFFER."
5670   (save-excursion
5671     (when (and (buffer-name buffer)
5672                (not gnus-single-article-buffer))
5673       (save-excursion
5674         (set-buffer buffer)
5675         (gnus-kill-buffer gnus-article-buffer)
5676         (gnus-kill-buffer gnus-original-article-buffer)))
5677     (cond (gnus-kill-summary-on-exit
5678            (when (and gnus-use-trees
5679                       (gnus-buffer-exists-p buffer))
5680              (save-excursion
5681                (set-buffer buffer)
5682                (gnus-tree-close gnus-newsgroup-name)))
5683            (gnus-kill-buffer buffer))
5684           ((gnus-buffer-exists-p buffer)
5685            (save-excursion
5686              (set-buffer buffer)
5687              (gnus-deaden-summary))))))
5688
5689 (defun gnus-summary-wake-up-the-dead (&rest args)
5690   "Wake up the dead summary buffer."
5691   (interactive)
5692   (gnus-dead-summary-mode -1)
5693   (let ((name (buffer-name)))
5694     (when (string-match "Dead " name)
5695       (rename-buffer
5696        (concat (substring name 0 (match-beginning 0))
5697                (substring name (match-end 0)))
5698        t)))
5699   (gnus-message 3 "This dead summary is now alive again"))
5700
5701 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5702 (defun gnus-summary-fetch-faq (&optional faq-dir)
5703   "Fetch the FAQ for the current group.
5704 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5705 in."
5706   (interactive
5707    (list
5708     (when current-prefix-arg
5709       (completing-read
5710        "Faq dir: " (and (listp gnus-group-faq-directory)
5711                         (mapcar (lambda (file) (list file))
5712                                 gnus-group-faq-directory))))))
5713   (let (gnus-faq-buffer)
5714     (when (setq gnus-faq-buffer
5715                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5716       (gnus-configure-windows 'summary-faq))))
5717
5718 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5719 (defun gnus-summary-describe-group (&optional force)
5720   "Describe the current newsgroup."
5721   (interactive "P")
5722   (gnus-group-describe-group force gnus-newsgroup-name))
5723
5724 (defun gnus-summary-describe-briefly ()
5725   "Describe summary mode commands briefly."
5726   (interactive)
5727   (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")))
5728
5729 ;; Walking around group mode buffer from summary mode.
5730
5731 (defun gnus-summary-next-group (&optional no-article target-group backward)
5732   "Exit current newsgroup and then select next unread newsgroup.
5733 If prefix argument NO-ARTICLE is non-nil, no article is selected
5734 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5735 previous group instead."
5736   (interactive "P")
5737   ;; Stop pre-fetching.
5738   (gnus-async-halt-prefetch)
5739   (let ((current-group gnus-newsgroup-name)
5740         (current-buffer (current-buffer))
5741         entered)
5742     ;; First we semi-exit this group to update Xrefs and all variables.
5743     ;; We can't do a real exit, because the window conf must remain
5744     ;; the same in case the user is prompted for info, and we don't
5745     ;; want the window conf to change before that...
5746     (gnus-summary-exit t)
5747     (while (not entered)
5748       ;; Then we find what group we are supposed to enter.
5749       (set-buffer gnus-group-buffer)
5750       (gnus-group-jump-to-group current-group)
5751       (setq target-group
5752             (or target-group
5753                 (if (eq gnus-keep-same-level 'best)
5754                     (gnus-summary-best-group gnus-newsgroup-name)
5755                   (gnus-summary-search-group backward gnus-keep-same-level))))
5756       (if (not target-group)
5757           ;; There are no further groups, so we return to the group
5758           ;; buffer.
5759           (progn
5760             (gnus-message 5 "Returning to the group buffer")
5761             (setq entered t)
5762             (when (gnus-buffer-live-p current-buffer)
5763               (set-buffer current-buffer)
5764               (gnus-summary-exit))
5765             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5766         ;; We try to enter the target group.
5767         (gnus-group-jump-to-group target-group)
5768         (let ((unreads (gnus-group-group-unread)))
5769           (if (and (or (eq t unreads)
5770                        (and unreads (not (zerop unreads))))
5771                    (gnus-summary-read-group
5772                     target-group nil no-article
5773                     (and (buffer-name current-buffer) current-buffer)
5774                     nil backward))
5775               (setq entered t)
5776             (setq current-group target-group
5777                   target-group nil)))))))
5778
5779 (defun gnus-summary-prev-group (&optional no-article)
5780   "Exit current newsgroup and then select previous unread newsgroup.
5781 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5782   (interactive "P")
5783   (gnus-summary-next-group no-article nil t))
5784
5785 ;; Walking around summary lines.
5786
5787 (defun gnus-summary-first-subject (&optional unread undownloaded)
5788   "Go to the first unread subject.
5789 If UNREAD is non-nil, go to the first unread article.
5790 Returns the article selected or nil if there are no unread articles."
5791   (interactive "P")
5792   (prog1
5793       (cond
5794        ;; Empty summary.
5795        ((null gnus-newsgroup-data)
5796         (gnus-message 3 "No articles in the group")
5797         nil)
5798        ;; Pick the first article.
5799        ((not unread)
5800         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5801         (gnus-data-number (car gnus-newsgroup-data)))
5802        ;; No unread articles.
5803        ((null gnus-newsgroup-unreads)
5804         (gnus-message 3 "No more unread articles")
5805         nil)
5806        ;; Find the first unread article.
5807        (t
5808         (let ((data gnus-newsgroup-data))
5809           (while (and data
5810                       (and (not (and undownloaded
5811                                      (eq gnus-undownloaded-mark
5812                                          (gnus-data-mark (car data)))))
5813                            (not (gnus-data-unread-p (car data)))))
5814             (setq data (cdr data)))
5815           (when data
5816             (goto-char (gnus-data-pos (car data)))
5817             (gnus-data-number (car data))))))
5818     (gnus-summary-position-point)))
5819
5820 (defun gnus-summary-next-subject (n &optional unread dont-display)
5821   "Go to next N'th summary line.
5822 If N is negative, go to the previous N'th subject line.
5823 If UNREAD is non-nil, only unread articles are selected.
5824 The difference between N and the actual number of steps taken is
5825 returned."
5826   (interactive "p")
5827   (let ((backward (< n 0))
5828         (n (abs n)))
5829     (while (and (> n 0)
5830                 (if backward
5831                     (gnus-summary-find-prev unread)
5832                   (gnus-summary-find-next unread)))
5833       (unless (zerop (setq n (1- n)))
5834         (gnus-summary-show-thread)))
5835     (when (/= 0 n)
5836       (gnus-message 7 "No more%s articles"
5837                     (if unread " unread" "")))
5838     (unless dont-display
5839       (gnus-summary-recenter)
5840       (gnus-summary-position-point))
5841     n))
5842
5843 (defun gnus-summary-next-unread-subject (n)
5844   "Go to next N'th unread summary line."
5845   (interactive "p")
5846   (gnus-summary-next-subject n t))
5847
5848 (defun gnus-summary-prev-subject (n &optional unread)
5849   "Go to previous N'th summary line.
5850 If optional argument UNREAD is non-nil, only unread article is selected."
5851   (interactive "p")
5852   (gnus-summary-next-subject (- n) unread))
5853
5854 (defun gnus-summary-prev-unread-subject (n)
5855   "Go to previous N'th unread summary line."
5856   (interactive "p")
5857   (gnus-summary-next-subject (- n) t))
5858
5859 (defun gnus-summary-goto-subject (article &optional force silent)
5860   "Go the subject line of ARTICLE.
5861 If FORCE, also allow jumping to articles not currently shown."
5862   (interactive "nArticle number: ")
5863   (let ((b (point))
5864         (data (gnus-data-find article)))
5865     ;; We read in the article if we have to.
5866     (and (not data)
5867          force
5868          (gnus-summary-insert-subject
5869           article
5870           (if (or (numberp force) (vectorp force)) force)
5871           t)
5872          (setq data (gnus-data-find article)))
5873     (goto-char b)
5874     (if (not data)
5875         (progn
5876           (unless silent
5877             (gnus-message 3 "Can't find article %d" article))
5878           nil)
5879       (goto-char (gnus-data-pos data))
5880       (gnus-summary-position-point)
5881       article)))
5882
5883 ;; Walking around summary lines with displaying articles.
5884
5885 (defun gnus-summary-expand-window (&optional arg)
5886   "Make the summary buffer take up the entire Emacs frame.
5887 Given a prefix, will force an `article' buffer configuration."
5888   (interactive "P")
5889   (if arg
5890       (gnus-configure-windows 'article 'force)
5891     (gnus-configure-windows 'summary 'force)))
5892
5893 (defun gnus-summary-display-article (article &optional all-header)
5894   "Display ARTICLE in article buffer."
5895   (gnus-set-global-variables)
5896   (if (null article)
5897       nil
5898     (prog1
5899         (if gnus-summary-display-article-function
5900             (funcall gnus-summary-display-article-function article all-header)
5901           (gnus-article-prepare article all-header))
5902       (with-current-buffer gnus-article-buffer
5903         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5904              nil))
5905       (gnus-run-hooks 'gnus-select-article-hook)
5906       (when (and gnus-current-article
5907                  (not (zerop gnus-current-article)))
5908         (gnus-summary-goto-subject gnus-current-article))
5909       (gnus-summary-recenter)
5910       (when (and gnus-use-trees gnus-show-threads)
5911         (gnus-possibly-generate-tree article)
5912         (gnus-highlight-selected-tree article))
5913       ;; Successfully display article.
5914       (gnus-article-set-window-start
5915        (cdr (assq article gnus-newsgroup-bookmarks))))))
5916
5917 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5918   "Select the current article.
5919 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5920 non-nil, the article will be re-fetched even if it already present in
5921 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5922 be displayed."
5923   ;; Make sure we are in the summary buffer to work around bbdb bug.
5924   (unless (eq major-mode 'gnus-summary-mode)
5925     (set-buffer gnus-summary-buffer))
5926   (let ((article (or article (gnus-summary-article-number)))
5927         (all-headers (not (not all-headers))) ;Must be T or NIL.
5928         gnus-summary-display-article-function)
5929     (and (not pseudo)
5930          (gnus-summary-article-pseudo-p article)
5931          (error "This is a pseudo-article"))
5932     (save-excursion
5933       (set-buffer gnus-summary-buffer)
5934       (if (or (and gnus-single-article-buffer
5935                    (or (null gnus-current-article)
5936                        (null gnus-article-current)
5937                        (null (get-buffer gnus-article-buffer))
5938                        (not (eq article (cdr gnus-article-current)))
5939                        (not (equal (car gnus-article-current)
5940                                    gnus-newsgroup-name))))
5941               (and (not gnus-single-article-buffer)
5942                    (or (null gnus-current-article)
5943                        (not (eq gnus-current-article article))))
5944               force)
5945           ;; The requested article is different from the current article.
5946           (progn
5947             (gnus-summary-display-article article all-headers)
5948             (when (or all-headers gnus-show-all-headers)
5949               (gnus-article-show-all-headers))
5950             (gnus-article-set-window-start
5951              (cdr (assq article gnus-newsgroup-bookmarks)))
5952             article)
5953         (when (or all-headers gnus-show-all-headers)
5954           (gnus-article-show-all-headers))
5955         'old))))
5956
5957 (defun gnus-summary-set-current-mark (&optional current-mark)
5958   "Obsolete function."
5959   nil)
5960
5961 (defun gnus-summary-next-article (&optional unread subject backward push)
5962   "Select the next article.
5963 If UNREAD, only unread articles are selected.
5964 If SUBJECT, only articles with SUBJECT are selected.
5965 If BACKWARD, the previous article is selected instead of the next."
5966   (interactive "P")
5967   (cond
5968    ;; Is there such an article?
5969    ((and (gnus-summary-search-forward unread subject backward)
5970          (or (gnus-summary-display-article (gnus-summary-article-number))
5971              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5972     (gnus-summary-position-point))
5973    ;; If not, we try the first unread, if that is wanted.
5974    ((and subject
5975          gnus-auto-select-same
5976          (gnus-summary-first-unread-article))
5977     (gnus-summary-position-point)
5978     (gnus-message 6 "Wrapped"))
5979    ;; Try to get next/previous article not displayed in this group.
5980    ((and gnus-auto-extend-newsgroup
5981          (not unread) (not subject))
5982     (gnus-summary-goto-article
5983      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5984      nil (count-lines (point-min) (point))))
5985    ;; Go to next/previous group.
5986    (t
5987     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5988       (gnus-summary-jump-to-group gnus-newsgroup-name))
5989     (let ((cmd last-command-char)
5990           (point
5991            (save-excursion
5992              (set-buffer gnus-group-buffer)
5993              (point)))
5994           (group
5995            (if (eq gnus-keep-same-level 'best)
5996                (gnus-summary-best-group gnus-newsgroup-name)
5997              (gnus-summary-search-group backward gnus-keep-same-level))))
5998       ;; For some reason, the group window gets selected.  We change
5999       ;; it back.
6000       (select-window (get-buffer-window (current-buffer)))
6001       ;; Select next unread newsgroup automagically.
6002       (cond
6003        ((or (not gnus-auto-select-next)
6004             (not cmd))
6005         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6006        ((or (eq gnus-auto-select-next 'quietly)
6007             (and (eq gnus-auto-select-next 'slightly-quietly)
6008                  push)
6009             (and (eq gnus-auto-select-next 'almost-quietly)
6010                  (gnus-summary-last-article-p)))
6011         ;; Select quietly.
6012         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6013             (gnus-summary-exit)
6014           (gnus-message 7 "No more%s articles (%s)..."
6015                         (if unread " unread" "")
6016                         (if group (concat "selecting " group)
6017                           "exiting"))
6018           (gnus-summary-next-group nil group backward)))
6019        (t
6020         (when (gnus-key-press-event-p last-input-event)
6021           (gnus-summary-walk-group-buffer
6022            gnus-newsgroup-name cmd unread backward point))))))))
6023
6024 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6025   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6026                       (?\C-p (gnus-group-prev-unread-group 1))))
6027         (cursor-in-echo-area t)
6028         keve key group ended)
6029     (save-excursion
6030       (set-buffer gnus-group-buffer)
6031       (goto-char start)
6032       (setq group
6033             (if (eq gnus-keep-same-level 'best)
6034                 (gnus-summary-best-group gnus-newsgroup-name)
6035               (gnus-summary-search-group backward gnus-keep-same-level))))
6036     (while (not ended)
6037       (gnus-message
6038        5 "No more%s articles%s" (if unread " unread" "")
6039        (if (and group
6040                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6041            (format " (Type %s for %s [%s])"
6042                    (single-key-description cmd) group
6043                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6044          (format " (Type %s to exit %s)"
6045                  (single-key-description cmd)
6046                  gnus-newsgroup-name)))
6047       ;; Confirm auto selection.
6048       (setq key (car (setq keve (gnus-read-event-char))))
6049       (setq ended t)
6050       (cond
6051        ((assq key keystrokes)
6052         (let ((obuf (current-buffer)))
6053           (switch-to-buffer gnus-group-buffer)
6054           (when group
6055             (gnus-group-jump-to-group group))
6056           (eval (cadr (assq key keystrokes)))
6057           (setq group (gnus-group-group-name))
6058           (switch-to-buffer obuf))
6059         (setq ended nil))
6060        ((equal key cmd)
6061         (if (or (not group)
6062                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6063             (gnus-summary-exit)
6064           (gnus-summary-next-group nil group backward)))
6065        (t
6066         (push (cdr keve) unread-command-events))))))
6067
6068 (defun gnus-summary-next-unread-article ()
6069   "Select unread article after current one."
6070   (interactive)
6071   (gnus-summary-next-article
6072    (or (not (eq gnus-summary-goto-unread 'never))
6073        (gnus-summary-last-article-p (gnus-summary-article-number)))
6074    (and gnus-auto-select-same
6075         (gnus-summary-article-subject))))
6076
6077 (defun gnus-summary-prev-article (&optional unread subject)
6078   "Select the article after the current one.
6079 If UNREAD is non-nil, only unread articles are selected."
6080   (interactive "P")
6081   (gnus-summary-next-article unread subject t))
6082
6083 (defun gnus-summary-prev-unread-article ()
6084   "Select unread article before current one."
6085   (interactive)
6086   (gnus-summary-prev-article
6087    (or (not (eq gnus-summary-goto-unread 'never))
6088        (gnus-summary-first-article-p (gnus-summary-article-number)))
6089    (and gnus-auto-select-same
6090         (gnus-summary-article-subject))))
6091
6092 (defun gnus-summary-next-page (&optional lines circular)
6093   "Show next page of the selected article.
6094 If at the end of the current article, select the next article.
6095 LINES says how many lines should be scrolled up.
6096
6097 If CIRCULAR is non-nil, go to the start of the article instead of
6098 selecting the next article when reaching the end of the current
6099 article."
6100   (interactive "P")
6101   (setq gnus-summary-buffer (current-buffer))
6102   (gnus-set-global-variables)
6103   (let ((article (gnus-summary-article-number))
6104         (article-window (get-buffer-window gnus-article-buffer t))
6105         endp)
6106     ;; If the buffer is empty, we have no article.
6107     (unless article
6108       (error "No article to select"))
6109     (gnus-configure-windows 'article)
6110     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6111         (if (and (eq gnus-summary-goto-unread 'never)
6112                  (not (gnus-summary-last-article-p article)))
6113             (gnus-summary-next-article)
6114           (gnus-summary-next-unread-article))
6115       (if (or (null gnus-current-article)
6116               (null gnus-article-current)
6117               (/= article (cdr gnus-article-current))
6118               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6119           ;; Selected subject is different from current article's.
6120           (gnus-summary-display-article article)
6121         (when article-window
6122           (gnus-eval-in-buffer-window gnus-article-buffer
6123             (setq endp (gnus-article-next-page lines)))
6124           (when endp
6125             (cond (circular
6126                    (gnus-summary-beginning-of-article))
6127                   (lines
6128                    (gnus-message 3 "End of message"))
6129                   ((null lines)
6130                    (if (and (eq gnus-summary-goto-unread 'never)
6131                             (not (gnus-summary-last-article-p article)))
6132                        (gnus-summary-next-article)
6133                      (gnus-summary-next-unread-article))))))))
6134     (gnus-summary-recenter)
6135     (gnus-summary-position-point)))
6136
6137 (defun gnus-summary-prev-page (&optional lines move)
6138   "Show previous page of selected article.
6139 Argument LINES specifies lines to be scrolled down.
6140 If MOVE, move to the previous unread article if point is at
6141 the beginning of the buffer."
6142   (interactive "P")
6143   (let ((article (gnus-summary-article-number))
6144         (article-window (get-buffer-window gnus-article-buffer t))
6145         endp)
6146     (gnus-configure-windows 'article)
6147     (if (or (null gnus-current-article)
6148             (null gnus-article-current)
6149             (/= article (cdr gnus-article-current))
6150             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6151         ;; Selected subject is different from current article's.
6152         (gnus-summary-display-article article)
6153       (gnus-summary-recenter)
6154       (when article-window
6155         (gnus-eval-in-buffer-window gnus-article-buffer
6156           (setq endp (gnus-article-prev-page lines)))
6157         (when (and move endp)
6158           (cond (lines
6159                  (gnus-message 3 "Beginning of message"))
6160                 ((null lines)
6161                  (if (and (eq gnus-summary-goto-unread 'never)
6162                           (not (gnus-summary-first-article-p article)))
6163                      (gnus-summary-prev-article)
6164                    (gnus-summary-prev-unread-article))))))))
6165   (gnus-summary-position-point))
6166
6167 (defun gnus-summary-prev-page-or-article (&optional lines)
6168   "Show previous page of selected article.
6169 Argument LINES specifies lines to be scrolled down.
6170 If at the beginning of the article, go to the next article."
6171   (interactive "P")
6172   (gnus-summary-prev-page lines t))
6173
6174 (defun gnus-summary-scroll-up (lines)
6175   "Scroll up (or down) one line current article.
6176 Argument LINES specifies lines to be scrolled up (or down if negative)."
6177   (interactive "p")
6178   (gnus-configure-windows 'article)
6179   (gnus-summary-show-thread)
6180   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6181     (gnus-eval-in-buffer-window gnus-article-buffer
6182       (cond ((> lines 0)
6183              (when (gnus-article-next-page lines)
6184                (gnus-message 3 "End of message")))
6185             ((< lines 0)
6186              (gnus-article-prev-page (- lines))))))
6187   (gnus-summary-recenter)
6188   (gnus-summary-position-point))
6189
6190 (defun gnus-summary-scroll-down (lines)
6191   "Scroll down (or up) one line current article.
6192 Argument LINES specifies lines to be scrolled down (or up if negative)."
6193   (interactive "p")
6194   (gnus-summary-scroll-up (- lines)))
6195
6196 (defun gnus-summary-next-same-subject ()
6197   "Select next article which has the same subject as current one."
6198   (interactive)
6199   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6200
6201 (defun gnus-summary-prev-same-subject ()
6202   "Select previous article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-next-unread-same-subject ()
6207   "Select next unread article which has the same subject as current one."
6208   (interactive)
6209   (gnus-summary-next-article t (gnus-summary-article-subject)))
6210
6211 (defun gnus-summary-prev-unread-same-subject ()
6212   "Select previous unread article which has the same subject as current one."
6213   (interactive)
6214   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6215
6216 (defun gnus-summary-first-unread-article ()
6217   "Select the first unread article.
6218 Return nil if there are no unread articles."
6219   (interactive)
6220   (prog1
6221       (when (gnus-summary-first-subject t)
6222         (gnus-summary-show-thread)
6223         (gnus-summary-first-subject t)
6224         (gnus-summary-display-article (gnus-summary-article-number)))
6225     (gnus-summary-position-point)))
6226
6227 (defun gnus-summary-first-unread-subject ()
6228   "Place the point on the subject line of the first unread article.
6229 Return nil if there are no unread articles."
6230   (interactive)
6231   (prog1
6232       (when (gnus-summary-first-subject t)
6233         (gnus-summary-show-thread)
6234         (gnus-summary-first-subject t))
6235     (gnus-summary-position-point)))
6236
6237 (defun gnus-summary-first-article ()
6238   "Select the first article.
6239 Return nil if there are no articles."
6240   (interactive)
6241   (prog1
6242       (when (gnus-summary-first-subject)
6243         (gnus-summary-show-thread)
6244         (gnus-summary-first-subject)
6245         (gnus-summary-display-article (gnus-summary-article-number)))
6246     (gnus-summary-position-point)))
6247
6248 (defun gnus-summary-best-unread-article ()
6249   "Select the unread article with the highest score."
6250   (interactive)
6251   (let ((best -1000000)
6252         (data gnus-newsgroup-data)
6253         article score)
6254     (while data
6255       (and (gnus-data-unread-p (car data))
6256            (> (setq score
6257                     (gnus-summary-article-score (gnus-data-number (car data))))
6258               best)
6259            (setq best score
6260                  article (gnus-data-number (car data))))
6261       (setq data (cdr data)))
6262     (prog1
6263         (if article
6264             (gnus-summary-goto-article article)
6265           (error "No unread articles"))
6266       (gnus-summary-position-point))))
6267
6268 (defun gnus-summary-last-subject ()
6269   "Go to the last displayed subject line in the group."
6270   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6271     (when article
6272       (gnus-summary-goto-subject article))))
6273
6274 (defun gnus-summary-goto-article (article &optional all-headers force)
6275   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6276 If ALL-HEADERS is non-nil, no header lines are hidden.
6277 If FORCE, go to the article even if it isn't displayed.  If FORCE
6278 is a number, it is the line the article is to be displayed on."
6279   (interactive
6280    (list
6281     (completing-read
6282      "Article number or Message-ID: "
6283      (mapcar (lambda (number) (list (int-to-string number)))
6284              gnus-newsgroup-limit))
6285     current-prefix-arg
6286     t))
6287   (prog1
6288       (if (and (stringp article)
6289                (string-match "@" article))
6290           (gnus-summary-refer-article article)
6291         (when (stringp article)
6292           (setq article (string-to-number article)))
6293         (if (gnus-summary-goto-subject article force)
6294             (gnus-summary-display-article article all-headers)
6295           (gnus-message 4 "Couldn't go to article %s" article) nil))
6296     (gnus-summary-position-point)))
6297
6298 (defun gnus-summary-goto-last-article ()
6299   "Go to the previously read article."
6300   (interactive)
6301   (prog1
6302       (when gnus-last-article
6303         (gnus-summary-goto-article gnus-last-article nil t))
6304     (gnus-summary-position-point)))
6305
6306 (defun gnus-summary-pop-article (number)
6307   "Pop one article off the history and go to the previous.
6308 NUMBER articles will be popped off."
6309   (interactive "p")
6310   (let (to)
6311     (setq gnus-newsgroup-history
6312           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6313     (if to
6314         (gnus-summary-goto-article (car to) nil t)
6315       (error "Article history empty")))
6316   (gnus-summary-position-point))
6317
6318 ;; Summary commands and functions for limiting the summary buffer.
6319
6320 (defun gnus-summary-limit-to-articles (n)
6321   "Limit the summary buffer to the next N articles.
6322 If not given a prefix, use the process marked articles instead."
6323   (interactive "P")
6324   (prog1
6325       (let ((articles (gnus-summary-work-articles n)))
6326         (setq gnus-newsgroup-processable nil)
6327         (gnus-summary-limit articles))
6328     (gnus-summary-position-point)))
6329
6330 (defun gnus-summary-pop-limit (&optional total)
6331   "Restore the previous limit.
6332 If given a prefix, remove all limits."
6333   (interactive "P")
6334   (when total
6335     (setq gnus-newsgroup-limits
6336           (list (mapcar (lambda (h) (mail-header-number h))
6337                         gnus-newsgroup-headers))))
6338   (unless gnus-newsgroup-limits
6339     (error "No limit to pop"))
6340   (prog1
6341       (gnus-summary-limit nil 'pop)
6342     (gnus-summary-position-point)))
6343
6344 (defun gnus-summary-limit-to-subject (subject &optional header)
6345   "Limit the summary buffer to articles that have subjects that match a regexp."
6346   (interactive "sLimit to subject (regexp): ")
6347   (unless header
6348     (setq header "subject"))
6349   (when (not (equal "" subject))
6350     (prog1
6351         (let ((articles (gnus-summary-find-matching
6352                          (or header "subject") subject 'all)))
6353           (unless articles
6354             (error "Found no matches for \"%s\"" subject))
6355           (gnus-summary-limit articles))
6356       (gnus-summary-position-point))))
6357
6358 (defun gnus-summary-limit-to-author (from)
6359   "Limit the summary buffer to articles that have authors that match a regexp."
6360   (interactive "sLimit to author (regexp): ")
6361   (gnus-summary-limit-to-subject from "from"))
6362
6363 (defun gnus-summary-limit-to-age (age &optional younger-p)
6364   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6365 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6366 articles that are younger than AGE days."
6367   (interactive
6368    (let ((younger current-prefix-arg)
6369          (days-got nil)
6370          days)
6371      (while (not days-got)
6372        (setq days (if younger
6373                       (read-string "Limit to articles within (in days): ")
6374                     (read-string "Limit to articles old than (in days): ")))
6375        (when (> (length days) 0)
6376          (setq days (read days)))
6377        (if (numberp days)
6378            (setq days-got t)
6379          (message "Please enter a number.")
6380          (sleep-for 1)))
6381      (list days younger)))
6382   (prog1
6383       (let ((data gnus-newsgroup-data)
6384             (cutoff (days-to-time age))
6385             articles d date is-younger)
6386         (while (setq d (pop data))
6387           (when (and (vectorp (gnus-data-header d))
6388                      (setq date (mail-header-date (gnus-data-header d))))
6389             (setq is-younger (time-less-p
6390                               (time-since (condition-case ()
6391                                               (date-to-time date)
6392                                             (error '(0 0))))
6393                               cutoff))
6394             (when (if younger-p
6395                       is-younger
6396                     (not is-younger))
6397               (push (gnus-data-number d) articles))))
6398         (gnus-summary-limit (nreverse articles)))
6399     (gnus-summary-position-point)))
6400
6401 (defun gnus-summary-limit-to-extra (header regexp)
6402   "Limit the summary buffer to articles that match an 'extra' header."
6403   (interactive
6404    (let ((header
6405           (intern
6406            (gnus-completing-read
6407             (symbol-name (car gnus-extra-headers))
6408             "Limit extra header:"
6409             (mapcar (lambda (x)
6410                       (cons (symbol-name x) x))
6411                     gnus-extra-headers)
6412             nil
6413             t))))
6414      (list header
6415            (read-string (format "Limit to header %s (regexp): " header)))))
6416   (when (not (equal "" regexp))
6417     (prog1
6418         (let ((articles (gnus-summary-find-matching
6419                          (cons 'extra header) regexp 'all)))
6420           (unless articles
6421             (error "Found no matches for \"%s\"" regexp))
6422           (gnus-summary-limit articles))
6423       (gnus-summary-position-point))))
6424
6425 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6426 (make-obsolete
6427  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6428
6429 (defun gnus-summary-limit-to-unread (&optional all)
6430   "Limit the summary buffer to articles that are not marked as read.
6431 If ALL is non-nil, limit strictly to unread articles."
6432   (interactive "P")
6433   (if all
6434       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6435     (gnus-summary-limit-to-marks
6436      ;; Concat all the marks that say that an article is read and have
6437      ;; those removed.
6438      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6439            gnus-killed-mark gnus-kill-file-mark
6440            gnus-low-score-mark gnus-expirable-mark
6441            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6442            gnus-duplicate-mark gnus-souped-mark)
6443      'reverse)))
6444
6445 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6446 (make-obsolete 'gnus-summary-delete-marked-with
6447                'gnus-summary-limit-exlude-marks)
6448
6449 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6450   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6451 If REVERSE, limit the summary buffer to articles that are marked
6452 with MARKS.  MARKS can either be a string of marks or a list of marks.
6453 Returns how many articles were removed."
6454   (interactive "sMarks: ")
6455   (gnus-summary-limit-to-marks marks t))
6456
6457 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6458   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6459 If REVERSE (the prefix), limit the summary buffer to articles that are
6460 not marked with MARKS.  MARKS can either be a string of marks or a
6461 list of marks.
6462 Returns how many articles were removed."
6463   (interactive "sMarks: \nP")
6464   (prog1
6465       (let ((data gnus-newsgroup-data)
6466             (marks (if (listp marks) marks
6467                      (append marks nil))) ; Transform to list.
6468             articles)
6469         (while data
6470           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6471                   (memq (gnus-data-mark (car data)) marks))
6472             (push (gnus-data-number (car data)) articles))
6473           (setq data (cdr data)))
6474         (gnus-summary-limit articles))
6475     (gnus-summary-position-point)))
6476
6477 (defun gnus-summary-limit-to-score (&optional score)
6478   "Limit to articles with score at or above SCORE."
6479   (interactive "P")
6480   (setq score (if score
6481                   (prefix-numeric-value score)
6482                 (or gnus-summary-default-score 0)))
6483   (let ((data gnus-newsgroup-data)
6484         articles)
6485     (while data
6486       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6487                 score)
6488         (push (gnus-data-number (car data)) articles))
6489       (setq data (cdr data)))
6490     (prog1
6491         (gnus-summary-limit articles)
6492       (gnus-summary-position-point))))
6493
6494 (defun gnus-summary-limit-include-thread (id)
6495   "Display all the hidden articles that in the current thread."
6496   (interactive (list (mail-header-id (gnus-summary-article-header))))
6497   (let ((articles (gnus-articles-in-thread
6498                    (gnus-id-to-thread (gnus-root-id id)))))
6499     (prog1
6500         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6501       (gnus-summary-position-point))))
6502
6503 (defun gnus-summary-limit-include-dormant ()
6504   "Display all the hidden articles that are marked as dormant.
6505 Note that this command only works on a subset of the articles currently
6506 fetched for this group."
6507   (interactive)
6508   (unless gnus-newsgroup-dormant
6509     (error "There are no dormant articles in this group"))
6510   (prog1
6511       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6512     (gnus-summary-position-point)))
6513
6514 (defun gnus-summary-limit-exclude-dormant ()
6515   "Hide all dormant articles."
6516   (interactive)
6517   (prog1
6518       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6519     (gnus-summary-position-point)))
6520
6521 (defun gnus-summary-limit-exclude-childless-dormant ()
6522   "Hide all dormant articles that have no children."
6523   (interactive)
6524   (let ((data (gnus-data-list t))
6525         articles d children)
6526     ;; Find all articles that are either not dormant or have
6527     ;; children.
6528     (while (setq d (pop data))
6529       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6530                 (and (setq children
6531                            (gnus-article-children (gnus-data-number d)))
6532                      (let (found)
6533                        (while children
6534                          (when (memq (car children) articles)
6535                            (setq children nil
6536                                  found t))
6537                          (pop children))
6538                        found)))
6539         (push (gnus-data-number d) articles)))
6540     ;; Do the limiting.
6541     (prog1
6542         (gnus-summary-limit articles)
6543       (gnus-summary-position-point))))
6544
6545 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6546   "Mark all unread excluded articles as read.
6547 If ALL, mark even excluded ticked and dormants as read."
6548   (interactive "P")
6549   (let ((articles (gnus-sorted-complement
6550                    (sort
6551                     (mapcar (lambda (h) (mail-header-number h))
6552                             gnus-newsgroup-headers)
6553                     '<)
6554                    (sort gnus-newsgroup-limit '<)))
6555         article)
6556     (setq gnus-newsgroup-unreads
6557           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6558     (if all
6559         (setq gnus-newsgroup-dormant nil
6560               gnus-newsgroup-marked nil
6561               gnus-newsgroup-reads
6562               (nconc
6563                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6564                gnus-newsgroup-reads))
6565       (while (setq article (pop articles))
6566         (unless (or (memq article gnus-newsgroup-dormant)
6567                     (memq article gnus-newsgroup-marked))
6568           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6569
6570 (defun gnus-summary-limit (articles &optional pop)
6571   (if pop
6572       ;; We pop the previous limit off the stack and use that.
6573       (setq articles (car gnus-newsgroup-limits)
6574             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6575     ;; We use the new limit, so we push the old limit on the stack.
6576     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6577   ;; Set the limit.
6578   (setq gnus-newsgroup-limit articles)
6579   (let ((total (length gnus-newsgroup-data))
6580         (data (gnus-data-find-list (gnus-summary-article-number)))
6581         (gnus-summary-mark-below nil)   ; Inhibit this.
6582         found)
6583     ;; This will do all the work of generating the new summary buffer
6584     ;; according to the new limit.
6585     (gnus-summary-prepare)
6586     ;; Hide any threads, possibly.
6587     (and gnus-show-threads
6588          gnus-thread-hide-subtree
6589          (gnus-summary-hide-all-threads))
6590     ;; Try to return to the article you were at, or one in the
6591     ;; neighborhood.
6592     (when data
6593       ;; We try to find some article after the current one.
6594       (while data
6595         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6596           (setq data nil
6597                 found t))
6598         (setq data (cdr data))))
6599     (unless found
6600       ;; If there is no data, that means that we were after the last
6601       ;; article.  The same goes when we can't find any articles
6602       ;; after the current one.
6603       (goto-char (point-max))
6604       (gnus-summary-find-prev))
6605     (gnus-set-mode-line 'summary)
6606     ;; We return how many articles were removed from the summary
6607     ;; buffer as a result of the new limit.
6608     (- total (length gnus-newsgroup-data))))
6609
6610 (defsubst gnus-invisible-cut-children (threads)
6611   (let ((num 0))
6612     (while threads
6613       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6614         (incf num))
6615       (pop threads))
6616     (< num 2)))
6617
6618 (defsubst gnus-cut-thread (thread)
6619   "Go forwards in the thread until we find an article that we want to display."
6620   (when (or (eq gnus-fetch-old-headers 'some)
6621             (eq gnus-fetch-old-headers 'invisible)
6622             (numberp gnus-fetch-old-headers)
6623             (eq gnus-build-sparse-threads 'some)
6624             (eq gnus-build-sparse-threads 'more))
6625     ;; Deal with old-fetched headers and sparse threads.
6626     (while (and
6627             thread
6628             (or
6629              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6630              (gnus-summary-article-ancient-p
6631               (mail-header-number (car thread))))
6632             (if (or (<= (length (cdr thread)) 1)
6633                     (eq gnus-fetch-old-headers 'invisible))
6634                 (setq gnus-newsgroup-limit
6635                       (delq (mail-header-number (car thread))
6636                             gnus-newsgroup-limit)
6637                       thread (cadr thread))
6638               (when (gnus-invisible-cut-children (cdr thread))
6639                 (let ((th (cdr thread)))
6640                   (while th
6641                     (if (memq (mail-header-number (caar th))
6642                               gnus-newsgroup-limit)
6643                         (setq thread (car th)
6644                               th nil)
6645                       (setq th (cdr th))))))))))
6646   thread)
6647
6648 (defun gnus-cut-threads (threads)
6649   "Cut off all uninteresting articles from the beginning of threads."
6650   (when (or (eq gnus-fetch-old-headers 'some)
6651             (eq gnus-fetch-old-headers 'invisible)
6652             (numberp gnus-fetch-old-headers)
6653             (eq gnus-build-sparse-threads 'some)
6654             (eq gnus-build-sparse-threads 'more))
6655     (let ((th threads))
6656       (while th
6657         (setcar th (gnus-cut-thread (car th)))
6658         (setq th (cdr th)))))
6659   ;; Remove nixed out threads.
6660   (delq nil threads))
6661
6662 (defun gnus-summary-initial-limit (&optional show-if-empty)
6663   "Figure out what the initial limit is supposed to be on group entry.
6664 This entails weeding out unwanted dormants, low-scored articles,
6665 fetch-old-headers verbiage, and so on."
6666   ;; Most groups have nothing to remove.
6667   (if (or gnus-inhibit-limiting
6668           (and (null gnus-newsgroup-dormant)
6669                (not (eq gnus-fetch-old-headers 'some))
6670                (not (numberp gnus-fetch-old-headers))
6671                (not (eq gnus-fetch-old-headers 'invisible))
6672                (null gnus-summary-expunge-below)
6673                (not (eq gnus-build-sparse-threads 'some))
6674                (not (eq gnus-build-sparse-threads 'more))
6675                (null gnus-thread-expunge-below)
6676                (not gnus-use-nocem)))
6677       ()                                ; Do nothing.
6678     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6679     (setq gnus-newsgroup-limit nil)
6680     (mapatoms
6681      (lambda (node)
6682        (unless (car (symbol-value node))
6683          ;; These threads have no parents -- they are roots.
6684          (let ((nodes (cdr (symbol-value node)))
6685                thread)
6686            (while nodes
6687              (if (and gnus-thread-expunge-below
6688                       (< (gnus-thread-total-score (car nodes))
6689                          gnus-thread-expunge-below))
6690                  (gnus-expunge-thread (pop nodes))
6691                (setq thread (pop nodes))
6692                (gnus-summary-limit-children thread))))))
6693      gnus-newsgroup-dependencies)
6694     ;; If this limitation resulted in an empty group, we might
6695     ;; pop the previous limit and use it instead.
6696     (when (and (not gnus-newsgroup-limit)
6697                show-if-empty)
6698       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6699     gnus-newsgroup-limit))
6700
6701 (defun gnus-summary-limit-children (thread)
6702   "Return 1 if this subthread is visible and 0 if it is not."
6703   ;; First we get the number of visible children to this thread.  This
6704   ;; is done by recursing down the thread using this function, so this
6705   ;; will really go down to a leaf article first, before slowly
6706   ;; working its way up towards the root.
6707   (when thread
6708     (let ((children
6709            (if (cdr thread)
6710                (apply '+ (mapcar 'gnus-summary-limit-children
6711                                  (cdr thread)))
6712              0))
6713           (number (mail-header-number (car thread)))
6714           score)
6715       (if (and
6716            (not (memq number gnus-newsgroup-marked))
6717            (or
6718             ;; If this article is dormant and has absolutely no visible
6719             ;; children, then this article isn't visible.
6720             (and (memq number gnus-newsgroup-dormant)
6721                  (zerop children))
6722             ;; If this is "fetch-old-headered" and there is no
6723             ;; visible children, then we don't want this article.
6724             (and (or (eq gnus-fetch-old-headers 'some)
6725                      (numberp gnus-fetch-old-headers))
6726                  (gnus-summary-article-ancient-p number)
6727                  (zerop children))
6728             ;; If this is "fetch-old-headered" and `invisible', then
6729             ;; we don't want this article.
6730             (and (eq gnus-fetch-old-headers 'invisible)
6731                  (gnus-summary-article-ancient-p number))
6732             ;; If this is a sparsely inserted article with no children,
6733             ;; we don't want it.
6734             (and (eq gnus-build-sparse-threads 'some)
6735                  (gnus-summary-article-sparse-p number)
6736                  (zerop children))
6737             ;; If we use expunging, and this article is really
6738             ;; low-scored, then we don't want this article.
6739             (when (and gnus-summary-expunge-below
6740                        (< (setq score
6741                                 (or (cdr (assq number gnus-newsgroup-scored))
6742                                     gnus-summary-default-score))
6743                           gnus-summary-expunge-below))
6744               ;; We increase the expunge-tally here, but that has
6745               ;; nothing to do with the limits, really.
6746               (incf gnus-newsgroup-expunged-tally)
6747               ;; We also mark as read here, if that's wanted.
6748               (when (and gnus-summary-mark-below
6749                          (< score gnus-summary-mark-below))
6750                 (setq gnus-newsgroup-unreads
6751                       (delq number gnus-newsgroup-unreads))
6752                 (if gnus-newsgroup-auto-expire
6753                     (push number gnus-newsgroup-expirable)
6754                   (push (cons number gnus-low-score-mark)
6755                         gnus-newsgroup-reads)))
6756               t)
6757             ;; Check NoCeM things.
6758             (if (and gnus-use-nocem
6759                      (gnus-nocem-unwanted-article-p
6760                       (mail-header-id (car thread))))
6761                 (progn
6762                   (setq gnus-newsgroup-unreads
6763                         (delq number gnus-newsgroup-unreads))
6764                   t))))
6765           ;; Nope, invisible article.
6766           0
6767         ;; Ok, this article is to be visible, so we add it to the limit
6768         ;; and return 1.
6769         (push number gnus-newsgroup-limit)
6770         1))))
6771
6772 (defun gnus-expunge-thread (thread)
6773   "Mark all articles in THREAD as read."
6774   (let* ((number (mail-header-number (car thread))))
6775     (incf gnus-newsgroup-expunged-tally)
6776     ;; We also mark as read here, if that's wanted.
6777     (setq gnus-newsgroup-unreads
6778           (delq number gnus-newsgroup-unreads))
6779     (if gnus-newsgroup-auto-expire
6780         (push number gnus-newsgroup-expirable)
6781       (push (cons number gnus-low-score-mark)
6782             gnus-newsgroup-reads)))
6783   ;; Go recursively through all subthreads.
6784   (mapcar 'gnus-expunge-thread (cdr thread)))
6785
6786 ;; Summary article oriented commands
6787
6788 (defun gnus-summary-refer-parent-article (n)
6789   "Refer parent article N times.
6790 If N is negative, go to ancestor -N instead.
6791 The difference between N and the number of articles fetched is returned."
6792   (interactive "p")
6793   (let ((skip 1)
6794         error header ref)
6795     (when (not (natnump n))
6796       (setq skip (abs n)
6797             n 1))
6798     (while (and (> n 0)
6799                 (not error))
6800       (setq header (gnus-summary-article-header))
6801       (if (and (eq (mail-header-number header)
6802                    (cdr gnus-article-current))
6803                (equal gnus-newsgroup-name
6804                       (car gnus-article-current)))
6805           ;; If we try to find the parent of the currently
6806           ;; displayed article, then we take a look at the actual
6807           ;; References header, since this is slightly more
6808           ;; reliable than the References field we got from the
6809           ;; server.
6810           (save-excursion
6811             (set-buffer gnus-original-article-buffer)
6812             (nnheader-narrow-to-headers)
6813             (unless (setq ref (message-fetch-field "references"))
6814               (setq ref (message-fetch-field "in-reply-to")))
6815             (widen))
6816         (setq ref
6817               ;; It's not the current article, so we take a bet on
6818               ;; the value we got from the server.
6819               (mail-header-references header)))
6820       (if (and ref
6821                (not (equal ref "")))
6822           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6823             (gnus-message 1 "Couldn't find parent"))
6824         (gnus-message 1 "No references in article %d"
6825                       (gnus-summary-article-number))
6826         (setq error t))
6827       (decf n))
6828     (gnus-summary-position-point)
6829     n))
6830
6831 (defun gnus-summary-refer-references ()
6832   "Fetch all articles mentioned in the References header.
6833 Return the number of articles fetched."
6834   (interactive)
6835   (let ((ref (mail-header-references (gnus-summary-article-header)))
6836         (current (gnus-summary-article-number))
6837         (n 0))
6838     (if (or (not ref)
6839             (equal ref ""))
6840         (error "No References in the current article")
6841       ;; For each Message-ID in the References header...
6842       (while (string-match "<[^>]*>" ref)
6843         (incf n)
6844         ;; ... fetch that article.
6845         (gnus-summary-refer-article
6846          (prog1 (match-string 0 ref)
6847            (setq ref (substring ref (match-end 0))))))
6848       (gnus-summary-goto-subject current)
6849       (gnus-summary-position-point)
6850       n)))
6851
6852 (defun gnus-summary-refer-thread (&optional limit)
6853   "Fetch all articles in the current thread.
6854 If LIMIT (the numerical prefix), fetch that many old headers instead
6855 of what's specified by the `gnus-refer-thread-limit' variable."
6856   (interactive "P")
6857   (let ((id (mail-header-id (gnus-summary-article-header)))
6858         (limit (if limit (prefix-numeric-value limit)
6859                  gnus-refer-thread-limit)))
6860     ;; We want to fetch LIMIT *old* headers, but we also have to
6861     ;; re-fetch all the headers in the current buffer, because many of
6862     ;; them may be undisplayed.  So we adjust LIMIT.
6863     (when (numberp limit)
6864       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6865     (unless (eq gnus-fetch-old-headers 'invisible)
6866       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6867       ;; Retrieve the headers and read them in.
6868       (if (eq (gnus-retrieve-headers
6869                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6870               'nov)
6871           (gnus-build-all-threads)
6872         (error "Can't fetch thread from backends that don't support NOV"))
6873       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6874     (gnus-summary-limit-include-thread id)))
6875
6876 (defun gnus-summary-refer-article (message-id)
6877   "Fetch an article specified by MESSAGE-ID."
6878   (interactive "sMessage-ID: ")
6879   (when (and (stringp message-id)
6880              (not (zerop (length message-id))))
6881     ;; Construct the correct Message-ID if necessary.
6882     ;; Suggested by tale@pawl.rpi.edu.
6883     (unless (string-match "^<" message-id)
6884       (setq message-id (concat "<" message-id)))
6885     (unless (string-match ">$" message-id)
6886       (setq message-id (concat message-id ">")))
6887     (let* ((header (gnus-id-to-header message-id))
6888            (sparse (and header
6889                         (gnus-summary-article-sparse-p
6890                          (mail-header-number header))
6891                         (memq (mail-header-number header)
6892                               gnus-newsgroup-limit)))
6893            number)
6894       (cond
6895        ;; If the article is present in the buffer we just go to it.
6896        ((and header
6897              (or (not (gnus-summary-article-sparse-p
6898                        (mail-header-number header)))
6899                  sparse))
6900         (prog1
6901             (gnus-summary-goto-article
6902              (mail-header-number header) nil t)
6903           (when sparse
6904             (gnus-summary-update-article (mail-header-number header)))))
6905        (t
6906         ;; We fetch the article.
6907         (catch 'found
6908           (dolist (gnus-override-method (gnus-refer-article-methods))
6909             (gnus-check-server gnus-override-method)
6910             ;; Fetch the header, and display the article.
6911             (when (setq number (gnus-summary-insert-subject message-id))
6912               (gnus-summary-select-article nil nil nil number)
6913               (throw 'found t)))
6914           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6915
6916 (defun gnus-refer-article-methods ()
6917   "Return a list of referrable methods."
6918   (cond
6919    ;; No method, so we default to current and native.
6920    ((null gnus-refer-article-method)
6921     (list gnus-current-select-method gnus-select-method))
6922    ;; Current.
6923    ((eq 'current gnus-refer-article-method)
6924     (list gnus-current-select-method))
6925    ;; List of select methods.
6926    ((not (stringp (cadr gnus-refer-article-method)))
6927     (let (out)
6928       (dolist (method gnus-refer-article-method)
6929         (push (if (eq 'current method)
6930                   gnus-current-select-method
6931                 method)
6932               out))
6933       (nreverse out)))
6934    ;; One single select method.
6935    (t
6936     (list gnus-refer-article-method))))
6937
6938 (defun gnus-summary-edit-parameters ()
6939   "Edit the group parameters of the current group."
6940   (interactive)
6941   (gnus-group-edit-group gnus-newsgroup-name 'params))
6942
6943 (defun gnus-summary-customize-parameters ()
6944   "Customize the group parameters of the current group."
6945   (interactive)
6946   (gnus-group-customize gnus-newsgroup-name))
6947
6948 (defun gnus-summary-enter-digest-group (&optional force)
6949   "Enter an nndoc group based on the current article.
6950 If FORCE, force a digest interpretation.  If not, try
6951 to guess what the document format is."
6952   (interactive "P")
6953   (let ((conf gnus-current-window-configuration))
6954     (save-excursion
6955       (gnus-summary-select-article))
6956     (setq gnus-current-window-configuration conf)
6957     (let* ((name (format "%s-%d"
6958                          (gnus-group-prefixed-name
6959                           gnus-newsgroup-name (list 'nndoc ""))
6960                          (save-excursion
6961                            (set-buffer gnus-summary-buffer)
6962                            gnus-current-article)))
6963            (ogroup gnus-newsgroup-name)
6964            (params (append (gnus-info-params (gnus-get-info ogroup))
6965                            (list (cons 'to-group ogroup))
6966                            (list (cons 'save-article-group ogroup))))
6967            (case-fold-search t)
6968            (buf (current-buffer))
6969            dig to-address)
6970       (save-excursion
6971         (set-buffer gnus-original-article-buffer)
6972         ;; Have the digest group inherit the main mail address of
6973         ;; the parent article.
6974         (when (setq to-address (or (message-fetch-field "reply-to")
6975                                    (message-fetch-field "from")))
6976           (setq params (append 
6977                         (list (cons 'to-address 
6978                                     (funcall gnus-decode-encoded-word-function
6979                                              to-address))))))
6980         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6981         (insert-buffer-substring gnus-original-article-buffer)
6982         ;; Remove lines that may lead nndoc to misinterpret the
6983         ;; document type.
6984         (narrow-to-region
6985          (goto-char (point-min))
6986          (or (search-forward "\n\n" nil t) (point)))
6987         (goto-char (point-min))
6988         (delete-matching-lines "^Path:\\|^From ")
6989         (widen))
6990       (unwind-protect
6991           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6992                     (gnus-newsgroup-ephemeral-ignored-charsets
6993                      gnus-newsgroup-ignored-charsets))
6994                 (gnus-group-read-ephemeral-group
6995                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6996                               (nndoc-article-type
6997                                ,(if force 'mbox 'guess))) t))
6998               ;; Make all postings to this group go to the parent group.
6999               (nconc (gnus-info-params (gnus-get-info name))
7000                      params)
7001             ;; Couldn't select this doc group.
7002             (switch-to-buffer buf)
7003             (gnus-set-global-variables)
7004             (gnus-configure-windows 'summary)
7005             (gnus-message 3 "Article couldn't be entered?"))
7006         (kill-buffer dig)))))
7007
7008 (defun gnus-summary-read-document (n)
7009   "Open a new group based on the current article(s).
7010 This will allow you to read digests and other similar
7011 documents as newsgroups.
7012 Obeys the standard process/prefix convention."
7013   (interactive "P")
7014   (let* ((articles (gnus-summary-work-articles n))
7015          (ogroup gnus-newsgroup-name)
7016          (params (append (gnus-info-params (gnus-get-info ogroup))
7017                          (list (cons 'to-group ogroup))))
7018          article group egroup groups vgroup)
7019     (while (setq article (pop articles))
7020       (setq group (format "%s-%d" gnus-newsgroup-name article))
7021       (gnus-summary-remove-process-mark article)
7022       (when (gnus-summary-display-article article)
7023         (save-excursion
7024           (with-temp-buffer
7025             (insert-buffer-substring gnus-original-article-buffer)
7026             ;; Remove some headers that may lead nndoc to make
7027             ;; the wrong guess.
7028             (message-narrow-to-head)
7029             (goto-char (point-min))
7030             (delete-matching-lines "^\\(Path\\):\\|^From ")
7031             (widen)
7032             (if (setq egroup
7033                       (gnus-group-read-ephemeral-group
7034                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7035                                      (nndoc-article-type guess))
7036                        t nil t))
7037                 (progn
7038                   ;; Make all postings to this group go to the parent group.
7039                   (nconc (gnus-info-params (gnus-get-info egroup))
7040                          params)
7041                   (push egroup groups))
7042               ;; Couldn't select this doc group.
7043               (gnus-error 3 "Article couldn't be entered"))))))
7044     ;; Now we have selected all the documents.
7045     (cond
7046      ((not groups)
7047       (error "None of the articles could be interpreted as documents"))
7048      ((gnus-group-read-ephemeral-group
7049        (setq vgroup (format
7050                      "nnvirtual:%s-%s" gnus-newsgroup-name
7051                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7052        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7053        t
7054        (cons (current-buffer) 'summary)))
7055      (t
7056       (error "Couldn't select virtual nndoc group")))))
7057
7058 (defun gnus-summary-isearch-article (&optional regexp-p)
7059   "Do incremental search forward on the current article.
7060 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7061   (interactive "P")
7062   (let* ((gnus-inhibit-treatment t)
7063          (old (gnus-summary-select-article)))
7064     (gnus-configure-windows 'article)
7065     (gnus-eval-in-buffer-window gnus-article-buffer
7066       (save-restriction
7067         (widen)
7068         (when (eq 'old old)
7069           (gnus-article-show-all-headers))
7070         (goto-char (point-min))
7071         (isearch-forward regexp-p)))))
7072
7073 (defun gnus-summary-search-article-forward (regexp &optional backward)
7074   "Search for an article containing REGEXP forward.
7075 If BACKWARD, search backward instead."
7076   (interactive
7077    (list (read-string
7078           (format "Search article %s (regexp%s): "
7079                   (if current-prefix-arg "backward" "forward")
7080                   (if gnus-last-search-regexp
7081                       (concat ", default " gnus-last-search-regexp)
7082                     "")))
7083          current-prefix-arg))
7084   (if (string-equal regexp "")
7085       (setq regexp (or gnus-last-search-regexp ""))
7086     (setq gnus-last-search-regexp regexp))
7087   (if (gnus-summary-search-article regexp backward)
7088       (gnus-summary-show-thread)
7089     (error "Search failed: \"%s\"" regexp)))
7090
7091 (defun gnus-summary-search-article-backward (regexp)
7092   "Search for an article containing REGEXP backward."
7093   (interactive
7094    (list (read-string
7095           (format "Search article backward (regexp%s): "
7096                   (if gnus-last-search-regexp
7097                       (concat ", default " gnus-last-search-regexp)
7098                     "")))))
7099   (gnus-summary-search-article-forward regexp 'backward))
7100
7101 (eval-when-compile
7102   (defmacro gnus-summary-search-article-position-point (regexp backward)
7103     "Dehighlight the last matched text and goto the beginning position."
7104     (` (if (and gnus-summary-search-article-matched-data
7105                 (let ((text (caddr gnus-summary-search-article-matched-data))
7106                       (inhibit-read-only t)
7107                       buffer-read-only)
7108                   (delete-region
7109                    (goto-char (car gnus-summary-search-article-matched-data))
7110                    (cadr gnus-summary-search-article-matched-data))
7111                   (insert text)
7112                   (string-match (, regexp) text)))
7113            (if (, backward) (beginning-of-line) (end-of-line))
7114          (goto-char (if (, backward) (point-max) (point-min))))))
7115
7116   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7117     "Place point where X-Face image is displayed."
7118     (if (featurep 'xemacs)
7119         (` (let ((end (if (search-forward "\n\n" nil t)
7120                           (goto-char (1- (point)))
7121                         (point-min)))
7122                  extent)
7123              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7124              (unless (and (re-search-forward "^From:" end t)
7125                           (setq extent (extent-at (point)))
7126                           (extent-begin-glyph extent))
7127                (goto-char (, opoint)))))
7128       (` (let ((end (if (search-forward "\n\n" nil t)
7129                         (goto-char (1- (point)))
7130                       (point-min))))
7131            (goto-char
7132             (or (text-property-any (or (search-backward "\n\n" nil t)
7133                                        (point-min))
7134                                    end 'x-face-mule-bitmap-image t)
7135                 (, opoint)))))))
7136
7137   (defmacro gnus-summary-search-article-highlight-matched-text
7138     (backward treated x-face)
7139     "Highlight matched text in the function `gnus-summary-search-article'."
7140     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7141              (end (set-marker (make-marker) (match-end 0)))
7142              (inhibit-read-only t)
7143              buffer-read-only)
7144          (unless treated
7145            (let ((,@
7146                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7147                     (mapcar
7148                      (lambda (item) (setq items (delq item items)))
7149                      '(gnus-treat-buttonize
7150                        gnus-treat-fill-article
7151                        gnus-treat-fill-long-lines
7152                        gnus-treat-emphasize
7153                        gnus-treat-highlight-headers
7154                        gnus-treat-highlight-citation
7155                        gnus-treat-highlight-signature
7156                        gnus-treat-overstrike
7157                        gnus-treat-display-xface
7158                        gnus-treat-buttonize-head
7159                        gnus-treat-decode-article-as-default-mime-charset))
7160                     (static-if (featurep 'xemacs)
7161                         items
7162                       (cons '(x-face-mule-delete-x-face-field
7163                               (quote never))
7164                             items))))
7165                  (gnus-treat-display-xface
7166                   (when (, x-face) gnus-treat-display-xface)))
7167              (gnus-article-prepare-mime-display)))
7168          (goto-char (if (, backward) start end))
7169          (when (, x-face)
7170            (gnus-summary-search-article-highlight-goto-x-face (point)))
7171          (setq gnus-summary-search-article-matched-data
7172                (list start end (buffer-substring start end)))
7173          (unless (eq start end);; matched text has been deleted. :-<
7174            (put-text-property start end 'face
7175                               (or (find-face 'isearch)
7176                                   'secondary-selection))))))
7177   )
7178
7179 (defun gnus-summary-search-article (regexp &optional backward)
7180   "Search for an article containing REGEXP.
7181 Optional argument BACKWARD means do search for backward.
7182 `gnus-select-article-hook' is not called during the search."
7183   ;; We have to require this here to make sure that the following
7184   ;; dynamic binding isn't shadowed by autoloading.
7185   (require 'gnus-async)
7186   (require 'gnus-art)
7187   (let ((gnus-select-article-hook nil)  ;Disable hook.
7188         (gnus-article-display-hook nil)
7189         (gnus-article-prepare-hook nil)
7190         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7191         (gnus-use-article-prefetch nil)
7192         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7193         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7194         (sum (current-buffer))
7195         (found nil)
7196         point treated)
7197     (gnus-save-hidden-threads
7198       (static-if (featurep 'xemacs)
7199           (let ((gnus-inhibit-treatment t))
7200             (setq treated (eq 'old (gnus-summary-select-article)))
7201             (when (and treated
7202                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7203                                  (window-live-p (get-buffer-window
7204                                                  gnus-article-buffer t)))))
7205               (gnus-summary-select-article nil t)
7206               (setq treated nil)))
7207         (let ((gnus-inhibit-treatment t)
7208               (x-face-mule-delete-x-face-field 'never))
7209           (setq treated (eq 'old (gnus-summary-select-article)))
7210           (when (and treated
7211                      (not
7212                       (and (gnus-buffer-live-p gnus-article-buffer)
7213                            (window-live-p (get-buffer-window
7214                                            gnus-article-buffer t))
7215                            (or (not (string-match "^\\^X-Face:" regexp))
7216                                (with-current-buffer gnus-article-buffer
7217                                  gnus-summary-search-article-matched-data)))))
7218             (gnus-summary-select-article nil t)
7219             (setq treated nil))))
7220       (set-buffer gnus-article-buffer)
7221       (widen)
7222       (if treated
7223           (progn
7224             (gnus-article-show-all-headers)
7225             (gnus-summary-search-article-position-point regexp backward))
7226         (goto-char (if backward (point-max) (point-min))))
7227       (while (not found)
7228         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7229         (if (if backward
7230                 (re-search-backward regexp nil t)
7231               (re-search-forward regexp nil t))
7232             ;; We found the regexp.
7233             (progn
7234               (gnus-summary-search-article-highlight-matched-text
7235                backward treated (string-match "^\\^X-Face:" regexp))
7236               (setq found 'found)
7237               (forward-line
7238                (/ (- 2 (window-height
7239                         (get-buffer-window gnus-article-buffer t)))
7240                   2))
7241               (set-window-start
7242                (get-buffer-window (current-buffer))
7243                (point))
7244               (set-buffer sum)
7245               (setq point (point)))
7246           ;; We didn't find it, so we go to the next article.
7247           (set-buffer sum)
7248           (setq found 'not)
7249           (while (eq found 'not)
7250             (if (not (if backward (gnus-summary-find-prev)
7251                        (gnus-summary-find-next)))
7252                 ;; No more articles.
7253                 (setq found t)
7254               ;; Select the next article and adjust point.
7255               (unless (gnus-summary-article-sparse-p
7256                        (gnus-summary-article-number))
7257                 (setq found nil)
7258                 (let ((gnus-inhibit-treatment t))
7259                   (gnus-summary-select-article))
7260                 (setq treated nil)
7261                 (set-buffer gnus-article-buffer)
7262                 (widen)
7263                 (goto-char (if backward (point-max) (point-min))))))))
7264       (gnus-message 7 ""))
7265     ;; Return whether we found the regexp.
7266     (when (eq found 'found)
7267       (goto-char point)
7268       (gnus-summary-show-thread)
7269       (gnus-summary-goto-subject gnus-current-article)
7270       (gnus-summary-position-point)
7271       t)))
7272
7273 (defun gnus-summary-find-matching (header regexp &optional backward unread
7274                                           not-case-fold)
7275   "Return a list of all articles that match REGEXP on HEADER.
7276 The search stars on the current article and goes forwards unless
7277 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7278 If UNREAD is non-nil, only unread articles will
7279 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7280 in the comparisons."
7281   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7282                 (gnus-data-find-list
7283                  (gnus-summary-article-number) (gnus-data-list backward))))
7284         (case-fold-search (not not-case-fold))
7285         articles d func)
7286     (if (consp header)
7287         (if (eq (car header) 'extra)
7288             (setq func
7289                   `(lambda (h)
7290                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7291                          "")))
7292           (error "%s is an invalid header" header))
7293       (unless (fboundp (intern (concat "mail-header-" header)))
7294         (error "%s is not a valid header" header))
7295       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7296     (while data
7297       (setq d (car data))
7298       (and (or (not unread)             ; We want all articles...
7299                (gnus-data-unread-p d))  ; Or just unreads.
7300            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7301            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7302            (push (gnus-data-number d) articles)) ; Success!
7303       (setq data (cdr data)))
7304     (nreverse articles)))
7305
7306 (defun gnus-summary-execute-command (header regexp command &optional backward)
7307   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7308 If HEADER is an empty string (or nil), the match is done on the entire
7309 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7310   (interactive
7311    (list (let ((completion-ignore-case t))
7312            (completing-read
7313             "Header name: "
7314             (mapcar (lambda (string) (list string))
7315                     '("Number" "Subject" "From" "Lines" "Date"
7316                       "Message-ID" "Xref" "References" "Body"))
7317             nil 'require-match))
7318          (read-string "Regexp: ")
7319          (read-key-sequence "Command: ")
7320          current-prefix-arg))
7321   (when (equal header "Body")
7322     (setq header ""))
7323   ;; Hidden thread subtrees must be searched as well.
7324   (gnus-summary-show-all-threads)
7325   ;; We don't want to change current point nor window configuration.
7326   (save-excursion
7327     (save-window-excursion
7328       (gnus-message 6 "Executing %s..." (key-description command))
7329       ;; We'd like to execute COMMAND interactively so as to give arguments.
7330       (gnus-execute header regexp
7331                     `(call-interactively ',(key-binding command))
7332                     backward)
7333       (gnus-message 6 "Executing %s...done" (key-description command)))))
7334
7335 (defun gnus-summary-beginning-of-article ()
7336   "Scroll the article back to the beginning."
7337   (interactive)
7338   (gnus-summary-select-article)
7339   (gnus-configure-windows 'article)
7340   (gnus-eval-in-buffer-window gnus-article-buffer
7341     (widen)
7342     (goto-char (point-min))
7343     (when gnus-page-broken
7344       (gnus-narrow-to-page))))
7345
7346 (defun gnus-summary-end-of-article ()
7347   "Scroll to the end of the article."
7348   (interactive)
7349   (gnus-summary-select-article)
7350   (gnus-configure-windows 'article)
7351   (gnus-eval-in-buffer-window gnus-article-buffer
7352     (widen)
7353     (goto-char (point-max))
7354     (recenter -3)
7355     (when gnus-page-broken
7356       (gnus-narrow-to-page))))
7357
7358 (defun gnus-summary-print-article (&optional filename n)
7359   "Generate and print a PostScript image of the N next (mail) articles.
7360
7361 If N is negative, print the N previous articles.  If N is nil and articles
7362 have been marked with the process mark, print these instead.
7363
7364 If the optional first argument FILENAME is nil, send the image to the
7365 printer.  If FILENAME is a string, save the PostScript image in a file with
7366 that name.  If FILENAME is a number, prompt the user for the name of the file
7367 to save in."
7368   (interactive (list (ps-print-preprint current-prefix-arg)
7369                      current-prefix-arg))
7370   (dolist (article (gnus-summary-work-articles n))
7371     (gnus-summary-select-article nil nil 'pseudo article)
7372     (gnus-eval-in-buffer-window gnus-article-buffer
7373       (let ((buffer (generate-new-buffer " *print*")))
7374         (unwind-protect
7375             (progn
7376               (copy-to-buffer buffer (point-min) (point-max))
7377               (set-buffer buffer)
7378               (gnus-article-delete-invisible-text)
7379               (let ((ps-left-header
7380                      (list
7381                       (concat "("
7382                               (mail-header-subject gnus-current-headers) ")")
7383                       (concat "("
7384                               (mail-header-from gnus-current-headers) ")")))
7385                     (ps-right-header
7386                      (list
7387                       "/pagenumberstring load"
7388                       (concat "("
7389                               (mail-header-date gnus-current-headers) ")"))))
7390                 (gnus-run-hooks 'gnus-ps-print-hook)
7391                 (save-excursion
7392                   (ps-print-buffer-with-faces filename))))
7393           (kill-buffer buffer))))))
7394
7395 (defun gnus-summary-show-article (&optional arg)
7396   "Force re-fetching of the current article.
7397 If ARG (the prefix) is a number, show the article with the charset
7398 defined in `gnus-summary-show-article-charset-alist', or the charset
7399 inputed.
7400 If ARG (the prefix) is non-nil and not a number, show the raw article
7401 without any article massaging functions being run."
7402   (interactive "P")
7403   (cond
7404    ((numberp arg)
7405     (let ((gnus-newsgroup-charset
7406            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7407                (read-coding-system "Charset: ")))
7408           (gnus-newsgroup-ignored-charsets 'gnus-all))
7409       (gnus-summary-select-article nil 'force)))
7410    ((not arg)
7411     ;; Select the article the normal way.
7412     (gnus-summary-select-article nil 'force))
7413    (t
7414     ;; We have to require this here to make sure that the following
7415     ;; dynamic binding isn't shadowed by autoloading.
7416     (require 'gnus-async)
7417     (require 'gnus-art)
7418     ;; Bind the article treatment functions to nil.
7419     (let ((gnus-have-all-headers t)
7420           gnus-article-display-hook
7421           gnus-article-prepare-hook
7422           gnus-article-decode-hook
7423           gnus-break-pages
7424           gnus-show-mime)
7425       (gnus-summary-select-article nil 'force))))
7426   (gnus-summary-goto-subject gnus-current-article)
7427   (gnus-summary-position-point))
7428
7429 (defun gnus-summary-verbose-headers (&optional arg)
7430   "Toggle permanent full header display.
7431 If ARG is a positive number, turn header display on.
7432 If ARG is a negative number, turn header display off."
7433   (interactive "P")
7434   (setq gnus-show-all-headers
7435         (cond ((or (not (numberp arg))
7436                    (zerop arg))
7437                (not gnus-show-all-headers))
7438               ((natnump arg)
7439                t)))
7440   (gnus-summary-show-article))
7441
7442 (defun gnus-summary-toggle-header (&optional arg)
7443   "Show the headers if they are hidden, or hide them if they are shown.
7444 If ARG is a positive number, show the entire header.
7445 If ARG is a negative number, hide the unwanted header lines."
7446   (interactive "P")
7447   (save-excursion
7448     (set-buffer gnus-article-buffer)
7449     (save-restriction
7450       (let* ((buffer-read-only nil)
7451              (inhibit-point-motion-hooks t)
7452              hidden e)
7453         (setq hidden
7454               (if (numberp arg)
7455                   (>= arg 0)
7456                 (save-restriction
7457                   (article-narrow-to-head)
7458                   (gnus-article-hidden-text-p 'headers))))
7459         (goto-char (point-min))
7460         (when (search-forward "\n\n" nil t)
7461           (delete-region (point-min) (1- (point))))
7462         (goto-char (point-min))
7463         (save-excursion
7464           (set-buffer gnus-original-article-buffer)
7465           (goto-char (point-min))
7466           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7467         (insert-buffer-substring gnus-original-article-buffer 1 e)
7468         (save-restriction
7469           (narrow-to-region (point-min) (point))
7470           (article-decode-encoded-words)
7471           (if  hidden
7472               (let ((gnus-treat-hide-headers nil)
7473                     (gnus-treat-hide-boring-headers nil))
7474                 (setq gnus-article-wash-types
7475                       (delq 'headers gnus-article-wash-types))
7476                 (gnus-treat-article 'head))
7477             (gnus-treat-article 'head)))
7478         (gnus-set-mode-line 'article)))))
7479
7480 (defun gnus-summary-show-all-headers ()
7481   "Make all header lines visible."
7482   (interactive)
7483   (gnus-article-show-all-headers))
7484
7485 (defun gnus-summary-toggle-mime (&optional arg)
7486   "Toggle MIME processing.
7487 If ARG is a positive number, turn MIME processing on."
7488   (interactive "P")
7489   (setq gnus-show-mime
7490         (if (null arg)
7491             (not gnus-show-mime)
7492           (> (prefix-numeric-value arg) 0)))
7493   (gnus-summary-select-article t 'force))
7494
7495 (defun gnus-summary-caesar-message (&optional arg)
7496   "Caesar rotate the current article by 13.
7497 The numerical prefix specifies how many places to rotate each letter
7498 forward."
7499   (interactive "P")
7500   (gnus-summary-select-article)
7501   (let ((mail-header-separator ""))
7502     (gnus-eval-in-buffer-window gnus-article-buffer
7503       (save-restriction
7504         (widen)
7505         (let ((start (window-start))
7506               buffer-read-only)
7507           (message-caesar-buffer-body arg)
7508           (set-window-start (get-buffer-window (current-buffer)) start))))))
7509
7510 (defun gnus-summary-stop-page-breaking ()
7511   "Stop page breaking in the current article."
7512   (interactive)
7513   (gnus-summary-select-article)
7514   (gnus-eval-in-buffer-window gnus-article-buffer
7515     (widen)
7516     (when (gnus-visual-p 'page-marker)
7517       (let ((buffer-read-only nil))
7518         (gnus-remove-text-with-property 'gnus-prev)
7519         (gnus-remove-text-with-property 'gnus-next))
7520       (setq gnus-page-broken nil))))
7521
7522 (defun gnus-summary-move-article (&optional n to-newsgroup
7523                                             select-method action)
7524   "Move the current article to a different newsgroup.
7525 If N is a positive number, move the N next articles.
7526 If N is a negative number, move the N previous articles.
7527 If N is nil and any articles have been marked with the process mark,
7528 move those articles instead.
7529 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7530 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7531 re-spool using this method.
7532
7533 For this function to work, both the current newsgroup and the
7534 newsgroup that you want to move to have to support the `request-move'
7535 and `request-accept' functions.
7536
7537 ACTION can be either `move' (the default), `crosspost' or `copy'."
7538   (interactive "P")
7539   (unless action
7540     (setq action 'move))
7541   ;; Disable marking as read.
7542   (let (gnus-mark-article-hook)
7543     (save-window-excursion
7544       (gnus-summary-select-article)))
7545   ;; Check whether the source group supports the required functions.
7546   (cond ((and (eq action 'move)
7547               (not (gnus-check-backend-function
7548                     'request-move-article gnus-newsgroup-name)))
7549          (error "The current group does not support article moving"))
7550         ((and (eq action 'crosspost)
7551               (not (gnus-check-backend-function
7552                     'request-replace-article gnus-newsgroup-name)))
7553          (error "The current group does not support article editing")))
7554   (let ((articles (gnus-summary-work-articles n))
7555         (prefix (if (gnus-check-backend-function
7556                     'request-move-article gnus-newsgroup-name)
7557                     (gnus-group-real-prefix gnus-newsgroup-name)
7558                   ""))
7559         (names '((move "Move" "Moving")
7560                  (copy "Copy" "Copying")
7561                  (crosspost "Crosspost" "Crossposting")))
7562         (copy-buf (save-excursion
7563                     (nnheader-set-temp-buffer " *copy article*")))
7564         (default-marks gnus-article-mark-lists)
7565         (no-expire-marks (delete '(expirable . expire)
7566                                  (copy-sequence gnus-article-mark-lists)))
7567         art-group to-method new-xref article to-groups)
7568     (unless (assq action names)
7569       (error "Unknown action %s" action))
7570     ;; Read the newsgroup name.
7571     (when (and (not to-newsgroup)
7572                (not select-method))
7573       (setq to-newsgroup
7574             (gnus-read-move-group-name
7575              (cadr (assq action names))
7576              (symbol-value (intern (format "gnus-current-%s-group" action)))
7577              articles prefix))
7578       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7579     (setq to-method (or select-method
7580                         (gnus-server-to-method
7581                          (gnus-group-method to-newsgroup))))
7582     ;; Check the method we are to move this article to...
7583     (unless (gnus-check-backend-function
7584              'request-accept-article (car to-method))
7585       (error "%s does not support article copying" (car to-method)))
7586     (unless (gnus-check-server to-method)
7587       (error "Can't open server %s" (car to-method)))
7588     (gnus-message 6 "%s to %s: %s..."
7589                   (caddr (assq action names))
7590                   (or (car select-method) to-newsgroup) articles)
7591     (while articles
7592       (setq article (pop articles))
7593       (setq
7594        art-group
7595        (cond
7596         ;; Move the article.
7597         ((eq action 'move)
7598          ;; Remove this article from future suppression.
7599          (gnus-dup-unsuppress-article article)
7600          (gnus-request-move-article
7601           article                       ; Article to move
7602           gnus-newsgroup-name           ; From newsgroup
7603           (nth 1 (gnus-find-method-for-group
7604                   gnus-newsgroup-name)) ; Server
7605           (list 'gnus-request-accept-article
7606                 to-newsgroup (list 'quote select-method)
7607                 (not articles) t)       ; Accept form
7608           (not articles)))              ; Only save nov last time
7609         ;; Copy the article.
7610         ((eq action 'copy)
7611          (save-excursion
7612            (set-buffer copy-buf)
7613            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7614              (gnus-request-accept-article
7615               to-newsgroup select-method (not articles) t))))
7616         ;; Crosspost the article.
7617         ((eq action 'crosspost)
7618          (let ((xref (message-tokenize-header
7619                       (mail-header-xref (gnus-summary-article-header article))
7620                       " ")))
7621            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7622                                   ":" article))
7623            (unless xref
7624              (setq xref (list (system-name))))
7625            (setq new-xref
7626                  (concat
7627                   (mapconcat 'identity
7628                              (delete "Xref:" (delete new-xref xref))
7629                              " ")
7630                   " " new-xref))
7631            (save-excursion
7632              (set-buffer copy-buf)
7633              ;; First put the article in the destination group.
7634              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7635              (when (consp (setq art-group
7636                                 (gnus-request-accept-article
7637                                  to-newsgroup select-method (not articles))))
7638                (setq new-xref (concat new-xref " " (car art-group)
7639                                       ":" (cdr art-group)))
7640                ;; Now we have the new Xrefs header, so we insert
7641                ;; it and replace the new article.
7642                (nnheader-replace-header "Xref" new-xref)
7643                (gnus-request-replace-article
7644                 (cdr art-group) to-newsgroup (current-buffer))
7645                art-group))))))
7646       (cond
7647        ((not art-group)
7648         (gnus-message 1 "Couldn't %s article %s: %s"
7649                       (cadr (assq action names)) article
7650                       (nnheader-get-report (car to-method))))
7651        ((eq art-group 'junk)
7652         (when (eq action 'move)
7653           (gnus-summary-mark-article article gnus-canceled-mark)
7654           (gnus-message 4 "Deleted article %s" article)))
7655        (t
7656         (let* ((pto-group (gnus-group-prefixed-name
7657                            (car art-group) to-method))
7658                (entry
7659                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7660                (info (nth 2 entry))
7661                (to-group (gnus-info-group info))
7662                to-marks)
7663           ;; Update the group that has been moved to.
7664           (when (and info
7665                      (memq action '(move copy)))
7666             (unless (member to-group to-groups)
7667               (push to-group to-groups))
7668
7669             (unless (memq article gnus-newsgroup-unreads)
7670               (push 'read to-marks)
7671               (gnus-info-set-read
7672                info (gnus-add-to-range (gnus-info-read info)
7673                                        (list (cdr art-group)))))
7674
7675             ;; See whether the article is to be put in the cache.
7676             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7677                              default-marks
7678                            no-expire-marks))
7679                   (to-article (cdr art-group)))
7680
7681               ;; Enter the article into the cache in the new group,
7682               ;; if that is required.
7683               (when gnus-use-cache
7684                 (gnus-cache-possibly-enter-article
7685                  to-group to-article
7686                  (let ((header (copy-sequence
7687                                 (gnus-summary-article-header article))))
7688                    (mail-header-set-number header to-article)
7689                    header)
7690                  (memq article gnus-newsgroup-marked)
7691                  (memq article gnus-newsgroup-dormant)
7692                  (memq article gnus-newsgroup-unreads)))
7693
7694               (when gnus-preserve-marks
7695                 ;; Copy any marks over to the new group.
7696                 (when (and (equal to-group gnus-newsgroup-name)
7697                            (not (memq article gnus-newsgroup-unreads)))
7698                   ;; Mark this article as read in this group.
7699                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7700                   (setcdr (gnus-active to-group) to-article)
7701                   (setcdr gnus-newsgroup-active to-article))
7702
7703                 (while marks
7704                   (when (memq article (symbol-value
7705                                        (intern (format "gnus-newsgroup-%s"
7706                                                        (caar marks)))))
7707                     (push (cdar marks) to-marks)
7708                     ;; If the other group is the same as this group,
7709                     ;; then we have to add the mark to the list.
7710                     (when (equal to-group gnus-newsgroup-name)
7711                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7712                            (cons to-article
7713                                  (symbol-value
7714                                   (intern (format "gnus-newsgroup-%s"
7715                                                   (caar marks)))))))
7716                     ;; Copy the marks to other group.
7717                     (gnus-add-marked-articles
7718                      to-group (cdar marks) (list to-article) info))
7719                   (setq marks (cdr marks)))
7720
7721                 (gnus-request-set-mark to-group (list (list (list to-article)
7722                                                             'set
7723                                                             to-marks))))
7724
7725               (gnus-dribble-enter
7726                (concat "(gnus-group-set-info '"
7727                        (gnus-prin1-to-string (gnus-get-info to-group))
7728                        ")"))))
7729
7730           ;; Update the Xref header in this article to point to
7731           ;; the new crossposted article we have just created.
7732           (when (eq action 'crosspost)
7733             (save-excursion
7734               (set-buffer copy-buf)
7735               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7736               (nnheader-replace-header "Xref" new-xref)
7737               (gnus-request-replace-article
7738                article gnus-newsgroup-name (current-buffer)))))
7739
7740         ;;;!!!Why is this necessary?
7741         (set-buffer gnus-summary-buffer)
7742
7743         (gnus-summary-goto-subject article)
7744         (when (eq action 'move)
7745           (gnus-summary-mark-article article gnus-canceled-mark))))
7746       (gnus-summary-remove-process-mark article))
7747     ;; Re-activate all groups that have been moved to.
7748     (while to-groups
7749       (save-excursion
7750         (set-buffer gnus-group-buffer)
7751         (when (gnus-group-goto-group (car to-groups) t)
7752           (gnus-group-get-new-news-this-group 1 t))
7753         (pop to-groups)))
7754
7755     (gnus-kill-buffer copy-buf)
7756     (gnus-summary-position-point)
7757     (gnus-set-mode-line 'summary)))
7758
7759 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7760   "Move the current article to a different newsgroup.
7761 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7762 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7763 re-spool using this method."
7764   (interactive "P")
7765   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7766
7767 (defun gnus-summary-crosspost-article (&optional n)
7768   "Crosspost the current article to some other group."
7769   (interactive "P")
7770   (gnus-summary-move-article n nil nil 'crosspost))
7771
7772 (defcustom gnus-summary-respool-default-method nil
7773   "Default method for respooling an article.
7774 If nil, use to the current newsgroup method."
7775   :type '(choice (gnus-select-method :value (nnml ""))
7776                  (const nil))
7777   :group 'gnus-summary-mail)
7778
7779 (defun gnus-summary-respool-article (&optional n method)
7780   "Respool the current article.
7781 The article will be squeezed through the mail spooling process again,
7782 which means that it will be put in some mail newsgroup or other
7783 depending on `nnmail-split-methods'.
7784 If N is a positive number, respool the N next articles.
7785 If N is a negative number, respool the N previous articles.
7786 If N is nil and any articles have been marked with the process mark,
7787 respool those articles instead.
7788
7789 Respooling can be done both from mail groups and \"real\" newsgroups.
7790 In the former case, the articles in question will be moved from the
7791 current group into whatever groups they are destined to.  In the
7792 latter case, they will be copied into the relevant groups."
7793   (interactive
7794    (list current-prefix-arg
7795          (let* ((methods (gnus-methods-using 'respool))
7796                 (methname
7797                  (symbol-name (or gnus-summary-respool-default-method
7798                                   (car (gnus-find-method-for-group
7799                                         gnus-newsgroup-name)))))
7800                 (method
7801                  (gnus-completing-read
7802                   methname "What backend do you want to use when respooling?"
7803                   methods nil t nil 'gnus-mail-method-history))
7804                 ms)
7805            (cond
7806             ((zerop (length (setq ms (gnus-servers-using-backend
7807                                       (intern method)))))
7808              (list (intern method) ""))
7809             ((= 1 (length ms))
7810              (car ms))
7811             (t
7812              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7813                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7814                            ms-alist))))))))
7815   (unless method
7816     (error "No method given for respooling"))
7817   (if (assoc (symbol-name
7818               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7819              (gnus-methods-using 'respool))
7820       (gnus-summary-move-article n nil method)
7821     (gnus-summary-copy-article n nil method)))
7822
7823 (defun gnus-summary-import-article (file)
7824   "Import an arbitrary file into a mail newsgroup."
7825   (interactive "fImport file: ")
7826   (let ((group gnus-newsgroup-name)
7827         (now (current-time))
7828         atts lines)
7829     (unless (gnus-check-backend-function 'request-accept-article group)
7830       (error "%s does not support article importing" group))
7831     (or (file-readable-p file)
7832         (not (file-regular-p file))
7833         (error "Can't read %s" file))
7834     (save-excursion
7835       (set-buffer (gnus-get-buffer-create " *import file*"))
7836       (erase-buffer)
7837       (nnheader-insert-file-contents file)
7838       (goto-char (point-min))
7839       (unless (nnheader-article-p)
7840         ;; This doesn't look like an article, so we fudge some headers.
7841         (setq atts (file-attributes file)
7842               lines (count-lines (point-min) (point-max)))
7843         (insert "From: " (read-string "From: ") "\n"
7844                 "Subject: " (read-string "Subject: ") "\n"
7845                 "Date: " (message-make-date (nth 5 atts))
7846                 "\n"
7847                 "Message-ID: " (message-make-message-id) "\n"
7848                 "Lines: " (int-to-string lines) "\n"
7849                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7850       (gnus-request-accept-article group nil t)
7851       (kill-buffer (current-buffer)))))
7852
7853 (defun gnus-summary-article-posted-p ()
7854   "Say whether the current (mail) article is available from news as well.
7855 This will be the case if the article has both been mailed and posted."
7856   (interactive)
7857   (let ((id (mail-header-references (gnus-summary-article-header)))
7858         (gnus-override-method (car (gnus-refer-article-methods))))
7859     (if (gnus-request-head id "")
7860         (gnus-message 2 "The current message was found on %s"
7861                       gnus-override-method)
7862       (gnus-message 2 "The current message couldn't be found on %s"
7863                     gnus-override-method)
7864       nil)))
7865
7866 (defun gnus-summary-expire-articles (&optional now)
7867   "Expire all articles that are marked as expirable in the current group."
7868   (interactive)
7869   (when (gnus-check-backend-function
7870          'request-expire-articles gnus-newsgroup-name)
7871     ;; This backend supports expiry.
7872     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7873            (expirable (if total
7874                           (progn
7875                             ;; We need to update the info for
7876                             ;; this group for `gnus-list-of-read-articles'
7877                             ;; to give us the right answer.
7878                             (gnus-run-hooks 'gnus-exit-group-hook)
7879                             (gnus-summary-update-info)
7880                             (gnus-list-of-read-articles gnus-newsgroup-name))
7881                         (setq gnus-newsgroup-expirable
7882                               (sort gnus-newsgroup-expirable '<))))
7883            (expiry-wait (if now 'immediate
7884                           (gnus-group-find-parameter
7885                            gnus-newsgroup-name 'expiry-wait)))
7886            (nnmail-expiry-target
7887             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7888                 nnmail-expiry-target))
7889            es)
7890       (when expirable
7891         ;; There are expirable articles in this group, so we run them
7892         ;; through the expiry process.
7893         (gnus-message 6 "Expiring articles...")
7894         (unless (gnus-check-group gnus-newsgroup-name)
7895           (error "Can't open server for %s" gnus-newsgroup-name))
7896         ;; The list of articles that weren't expired is returned.
7897         (save-excursion
7898           (if expiry-wait
7899               (let ((nnmail-expiry-wait-function nil)
7900                     (nnmail-expiry-wait expiry-wait))
7901                 (setq es (gnus-request-expire-articles
7902                           expirable gnus-newsgroup-name)))
7903             (setq es (gnus-request-expire-articles
7904                       expirable gnus-newsgroup-name))))
7905         (unless total
7906           (setq gnus-newsgroup-expirable es))
7907         ;; We go through the old list of expirable, and mark all
7908         ;; really expired articles as nonexistent.
7909         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7910           (let ((gnus-use-cache nil))
7911             (while expirable
7912               (unless (memq (car expirable) es)
7913                 (when (gnus-data-find (car expirable))
7914                   (gnus-summary-mark-article
7915                    (car expirable) gnus-canceled-mark)))
7916               (setq expirable (cdr expirable)))))
7917         (gnus-message 6 "Expiring articles...done")))))
7918
7919 (defun gnus-summary-expire-articles-now ()
7920   "Expunge all expirable articles in the current group.
7921 This means that *all* articles that are marked as expirable will be
7922 deleted forever, right now."
7923   (interactive)
7924   (or gnus-expert-user
7925       (gnus-yes-or-no-p
7926        "Are you really, really, really sure you want to delete all these messages? ")
7927       (error "Phew!"))
7928   (gnus-summary-expire-articles t))
7929
7930 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7931 (defun gnus-summary-delete-article (&optional n)
7932   "Delete the N next (mail) articles.
7933 This command actually deletes articles.  This is not a marking
7934 command.  The article will disappear forever from your life, never to
7935 return.
7936 If N is negative, delete backwards.
7937 If N is nil and articles have been marked with the process mark,
7938 delete these instead."
7939   (interactive "P")
7940   (unless (gnus-check-backend-function 'request-expire-articles
7941                                        gnus-newsgroup-name)
7942     (error "The current newsgroup does not support article deletion"))
7943   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7944     (error "Couldn't open server"))
7945   ;; Compute the list of articles to delete.
7946   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7947         not-deleted)
7948     (if (and gnus-novice-user
7949              (not (gnus-yes-or-no-p
7950                    (format "Do you really want to delete %s forever? "
7951                            (if (> (length articles) 1)
7952                                (format "these %s articles" (length articles))
7953                              "this article")))))
7954         ()
7955       ;; Delete the articles.
7956       (setq not-deleted (gnus-request-expire-articles
7957                          articles gnus-newsgroup-name 'force))
7958       (while articles
7959         (gnus-summary-remove-process-mark (car articles))
7960         ;; The backend might not have been able to delete the article
7961         ;; after all.
7962         (unless (memq (car articles) not-deleted)
7963           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7964         (setq articles (cdr articles)))
7965       (when not-deleted
7966         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7967     (gnus-summary-position-point)
7968     (gnus-set-mode-line 'summary)
7969     not-deleted))
7970
7971 (defun gnus-summary-edit-article (&optional force)
7972   "Edit the current article.
7973 This will have permanent effect only in mail groups.
7974 If FORCE is non-nil, allow editing of articles even in read-only
7975 groups."
7976   (interactive "P")
7977   (save-excursion
7978     (set-buffer gnus-summary-buffer)
7979     (let ((mail-parse-charset gnus-newsgroup-charset)
7980           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7981       (gnus-set-global-variables)
7982       (when (and (not force)
7983                  (gnus-group-read-only-p))
7984         (error "The current newsgroup does not support article editing"))
7985       (gnus-summary-show-article t)
7986       (gnus-article-edit-article
7987        'ignore
7988        `(lambda (no-highlight)
7989           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7990                 (mail-parse-ignored-charsets
7991                  ',gnus-newsgroup-ignored-charsets))
7992             (gnus-summary-edit-article-done
7993              ,(or (mail-header-references gnus-current-headers) "")
7994              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7995
7996 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7997
7998 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7999                                                  no-highlight)
8000   "Make edits to the current article permanent."
8001   (interactive)
8002   ;; Replace the article.
8003   (let ((buf (current-buffer)))
8004     (with-temp-buffer
8005       (insert-buffer-substring buf)
8006       (if (and (not read-only)
8007                (not (gnus-request-replace-article
8008                      (cdr gnus-article-current) (car gnus-article-current)
8009                      (current-buffer) t)))
8010           (error "Couldn't replace article")
8011         ;; Update the summary buffer.
8012         (if (and references
8013                  (equal (message-tokenize-header references " ")
8014                         (message-tokenize-header
8015                          (or (message-fetch-field "references") "") " ")))
8016             ;; We only have to update this line.
8017             (save-excursion
8018               (save-restriction
8019                 (message-narrow-to-head)
8020                 (let ((head (buffer-string))
8021                       header)
8022                   (with-temp-buffer
8023                     (insert (format "211 %d Article retrieved.\n"
8024                                     (cdr gnus-article-current)))
8025                     (insert head)
8026                     (insert ".\n")
8027                     (let ((nntp-server-buffer (current-buffer)))
8028                       (setq header (car (gnus-get-newsgroup-headers
8029                                          (save-excursion
8030                                            (set-buffer gnus-summary-buffer)
8031                                            gnus-newsgroup-dependencies)
8032                                          t))))
8033                     (save-excursion
8034                       (set-buffer gnus-summary-buffer)
8035                       (gnus-data-set-header
8036                        (gnus-data-find (cdr gnus-article-current))
8037                        header)
8038                       (gnus-summary-update-article-line
8039                        (cdr gnus-article-current) header))))))
8040           ;; Update threads.
8041           (set-buffer (or buffer gnus-summary-buffer))
8042           (gnus-summary-update-article (cdr gnus-article-current)))
8043         ;; Prettify the article buffer again.
8044         (unless no-highlight
8045           (save-excursion
8046             (set-buffer gnus-article-buffer)
8047             ;;;!!! Fix this -- article should be rehighlighted.
8048             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8049             (set-buffer gnus-original-article-buffer)
8050             (gnus-request-article
8051              (cdr gnus-article-current)
8052              (car gnus-article-current) (current-buffer))))
8053         ;; Prettify the summary buffer line.
8054         (when (gnus-visual-p 'summary-highlight 'highlight)
8055           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8056
8057 (defun gnus-summary-edit-wash (key)
8058   "Perform editing command KEY in the article buffer."
8059   (interactive
8060    (list
8061     (progn
8062       (message "%s" (concat (this-command-keys) "- "))
8063       (read-char))))
8064   (message "")
8065   (gnus-summary-edit-article)
8066   (execute-kbd-macro (concat (this-command-keys) key))
8067   (gnus-article-edit-done))
8068
8069 ;;; Respooling
8070
8071 (defun gnus-summary-respool-query (&optional silent trace)
8072   "Query where the respool algorithm would put this article."
8073   (interactive)
8074   (let (gnus-mark-article-hook)
8075     (gnus-summary-select-article)
8076     (save-excursion
8077       (set-buffer gnus-original-article-buffer)
8078       (save-restriction
8079         (message-narrow-to-head)
8080         (let ((groups (nnmail-article-group 'identity trace)))
8081           (unless silent
8082             (if groups
8083                 (message "This message would go to %s"
8084                          (mapconcat 'car groups ", "))
8085               (message "This message would go to no groups"))
8086             groups))))))
8087
8088 (defun gnus-summary-respool-trace ()
8089   "Trace where the respool algorithm would put this article.
8090 Display a buffer showing all fancy splitting patterns which matched."
8091   (interactive)
8092   (gnus-summary-respool-query nil t))
8093
8094 ;; Summary marking commands.
8095
8096 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8097   "Mark articles which has the same subject as read, and then select the next.
8098 If UNMARK is positive, remove any kind of mark.
8099 If UNMARK is negative, tick articles."
8100   (interactive "P")
8101   (when unmark
8102     (setq unmark (prefix-numeric-value unmark)))
8103   (let ((count
8104          (gnus-summary-mark-same-subject
8105           (gnus-summary-article-subject) unmark)))
8106     ;; Select next unread article.  If auto-select-same mode, should
8107     ;; select the first unread article.
8108     (gnus-summary-next-article t (and gnus-auto-select-same
8109                                       (gnus-summary-article-subject)))
8110     (gnus-message 7 "%d article%s marked as %s"
8111                   count (if (= count 1) " is" "s are")
8112                   (if unmark "unread" "read"))))
8113
8114 (defun gnus-summary-kill-same-subject (&optional unmark)
8115   "Mark articles which has the same subject as read.
8116 If UNMARK is positive, remove any kind of mark.
8117 If UNMARK is negative, tick articles."
8118   (interactive "P")
8119   (when unmark
8120     (setq unmark (prefix-numeric-value unmark)))
8121   (let ((count
8122          (gnus-summary-mark-same-subject
8123           (gnus-summary-article-subject) unmark)))
8124     ;; If marked as read, go to next unread subject.
8125     (when (null unmark)
8126       ;; Go to next unread subject.
8127       (gnus-summary-next-subject 1 t))
8128     (gnus-message 7 "%d articles are marked as %s"
8129                   count (if unmark "unread" "read"))))
8130
8131 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8132   "Mark articles with same SUBJECT as read, and return marked number.
8133 If optional argument UNMARK is positive, remove any kinds of marks.
8134 If optional argument UNMARK is negative, mark articles as unread instead."
8135   (let ((count 1))
8136     (save-excursion
8137       (cond
8138        ((null unmark)                   ; Mark as read.
8139         (while (and
8140                 (progn
8141                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8142                   (gnus-summary-show-thread) t)
8143                 (gnus-summary-find-subject subject))
8144           (setq count (1+ count))))
8145        ((> unmark 0)                    ; Tick.
8146         (while (and
8147                 (progn
8148                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8149                   (gnus-summary-show-thread) t)
8150                 (gnus-summary-find-subject subject))
8151           (setq count (1+ count))))
8152        (t                               ; Mark as unread.
8153         (while (and
8154                 (progn
8155                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8156                   (gnus-summary-show-thread) t)
8157                 (gnus-summary-find-subject subject))
8158           (setq count (1+ count)))))
8159       (gnus-set-mode-line 'summary)
8160       ;; Return the number of marked articles.
8161       count)))
8162
8163 (defun gnus-summary-mark-as-processable (n &optional unmark)
8164   "Set the process mark on the next N articles.
8165 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8166 the process mark instead.  The difference between N and the actual
8167 number of articles marked is returned."
8168   (interactive "p")
8169   (let ((backward (< n 0))
8170         (n (abs n)))
8171     (while (and
8172             (> n 0)
8173             (if unmark
8174                 (gnus-summary-remove-process-mark
8175                  (gnus-summary-article-number))
8176               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8177             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8178       (setq n (1- n)))
8179     (when (/= 0 n)
8180       (gnus-message 7 "No more articles"))
8181     (gnus-summary-recenter)
8182     (gnus-summary-position-point)
8183     n))
8184
8185 (defun gnus-summary-unmark-as-processable (n)
8186   "Remove the process mark from the next N articles.
8187 If N is negative, unmark backward instead.  The difference between N and
8188 the actual number of articles unmarked is returned."
8189   (interactive "p")
8190   (gnus-summary-mark-as-processable n t))
8191
8192 (defun gnus-summary-unmark-all-processable ()
8193   "Remove the process mark from all articles."
8194   (interactive)
8195   (save-excursion
8196     (while gnus-newsgroup-processable
8197       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8198   (gnus-summary-position-point))
8199
8200 (defun gnus-summary-mark-as-expirable (n)
8201   "Mark N articles forward as expirable.
8202 If N is negative, mark backward instead.  The difference between N and
8203 the actual number of articles marked is returned."
8204   (interactive "p")
8205   (gnus-summary-mark-forward n gnus-expirable-mark))
8206
8207 (defun gnus-summary-mark-article-as-replied (article)
8208   "Mark ARTICLE replied and update the summary line."
8209   (push article gnus-newsgroup-replied)
8210   (let ((buffer-read-only nil))
8211     (when (gnus-summary-goto-subject article nil t)
8212       (gnus-summary-update-secondary-mark article))))
8213
8214 (defun gnus-summary-set-bookmark (article)
8215   "Set a bookmark in current article."
8216   (interactive (list (gnus-summary-article-number)))
8217   (when (or (not (get-buffer gnus-article-buffer))
8218             (not gnus-current-article)
8219             (not gnus-article-current)
8220             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8221     (error "No current article selected"))
8222   ;; Remove old bookmark, if one exists.
8223   (let ((old (assq article gnus-newsgroup-bookmarks)))
8224     (when old
8225       (setq gnus-newsgroup-bookmarks
8226             (delq old gnus-newsgroup-bookmarks))))
8227   ;; Set the new bookmark, which is on the form
8228   ;; (article-number . line-number-in-body).
8229   (push
8230    (cons article
8231          (save-excursion
8232            (set-buffer gnus-article-buffer)
8233            (count-lines
8234             (min (point)
8235                  (save-excursion
8236                    (goto-char (point-min))
8237                    (search-forward "\n\n" nil t)
8238                    (point)))
8239             (point))))
8240    gnus-newsgroup-bookmarks)
8241   (gnus-message 6 "A bookmark has been added to the current article."))
8242
8243 (defun gnus-summary-remove-bookmark (article)
8244   "Remove the bookmark from the current article."
8245   (interactive (list (gnus-summary-article-number)))
8246   ;; Remove old bookmark, if one exists.
8247   (let ((old (assq article gnus-newsgroup-bookmarks)))
8248     (if old
8249         (progn
8250           (setq gnus-newsgroup-bookmarks
8251                 (delq old gnus-newsgroup-bookmarks))
8252           (gnus-message 6 "Removed bookmark."))
8253       (gnus-message 6 "No bookmark in current article."))))
8254
8255 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8256 (defun gnus-summary-mark-as-dormant (n)
8257   "Mark N articles forward as dormant.
8258 If N is negative, mark backward instead.  The difference between N and
8259 the actual number of articles marked is returned."
8260   (interactive "p")
8261   (gnus-summary-mark-forward n gnus-dormant-mark))
8262
8263 (defun gnus-summary-set-process-mark (article)
8264   "Set the process mark on ARTICLE and update the summary line."
8265   (setq gnus-newsgroup-processable
8266         (cons article
8267               (delq article gnus-newsgroup-processable)))
8268   (when (gnus-summary-goto-subject article)
8269     (gnus-summary-show-thread)
8270     (gnus-summary-goto-subject article)
8271     (gnus-summary-update-secondary-mark article)))
8272
8273 (defun gnus-summary-remove-process-mark (article)
8274   "Remove the process mark from ARTICLE and update the summary line."
8275   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8276   (when (gnus-summary-goto-subject article)
8277     (gnus-summary-show-thread)
8278     (gnus-summary-goto-subject article)
8279     (gnus-summary-update-secondary-mark article)))
8280
8281 (defun gnus-summary-set-saved-mark (article)
8282   "Set the process mark on ARTICLE and update the summary line."
8283   (push article gnus-newsgroup-saved)
8284   (when (gnus-summary-goto-subject article)
8285     (gnus-summary-update-secondary-mark article)))
8286
8287 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8288   "Mark N articles as read forwards.
8289 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8290 The difference between N and the actual number of articles marked is
8291 returned.
8292 Iff NO-EXPIRE, auto-expiry will be inhibited."
8293   (interactive "p")
8294   (gnus-summary-show-thread)
8295   (let ((backward (< n 0))
8296         (gnus-summary-goto-unread
8297          (and gnus-summary-goto-unread
8298               (not (eq gnus-summary-goto-unread 'never))
8299               (not (memq mark (list gnus-unread-mark
8300                                     gnus-ticked-mark gnus-dormant-mark)))))
8301         (n (abs n))
8302         (mark (or mark gnus-del-mark)))
8303     (while (and (> n 0)
8304                 (gnus-summary-mark-article nil mark no-expire)
8305                 (zerop (gnus-summary-next-subject
8306                         (if backward -1 1)
8307                         (and gnus-summary-goto-unread
8308                              (not (eq gnus-summary-goto-unread 'never)))
8309                         t)))
8310       (setq n (1- n)))
8311     (when (/= 0 n)
8312       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8313     (gnus-summary-recenter)
8314     (gnus-summary-position-point)
8315     (gnus-set-mode-line 'summary)
8316     n))
8317
8318 (defun gnus-summary-mark-article-as-read (mark)
8319   "Mark the current article quickly as read with MARK."
8320   (let ((article (gnus-summary-article-number)))
8321     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8322     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8323     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8324     (push (cons article mark) gnus-newsgroup-reads)
8325     ;; Possibly remove from cache, if that is used.
8326     (when gnus-use-cache
8327       (gnus-cache-enter-remove-article article))
8328     ;; Allow the backend to change the mark.
8329     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8330     ;; Check for auto-expiry.
8331     (when (and gnus-newsgroup-auto-expire
8332                (memq mark gnus-auto-expirable-marks))
8333       (setq mark gnus-expirable-mark)
8334       ;; Let the backend know about the mark change.
8335       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8336       (push article gnus-newsgroup-expirable))
8337     ;; Set the mark in the buffer.
8338     (gnus-summary-update-mark mark 'unread)
8339     t))
8340
8341 (defun gnus-summary-mark-article-as-unread (mark)
8342   "Mark the current article quickly as unread with MARK."
8343   (let* ((article (gnus-summary-article-number))
8344          (old-mark (gnus-summary-article-mark article)))
8345     ;; Allow the backend to change the mark.
8346     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8347     (if (eq mark old-mark)
8348         t
8349       (if (<= article 0)
8350           (progn
8351             (gnus-error 1 "Can't mark negative article numbers")
8352             nil)
8353         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8354         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8355         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8356         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8357         (cond ((= mark gnus-ticked-mark)
8358                (push article gnus-newsgroup-marked))
8359               ((= mark gnus-dormant-mark)
8360                (push article gnus-newsgroup-dormant))
8361               (t
8362                (push article gnus-newsgroup-unreads)))
8363         (gnus-pull article gnus-newsgroup-reads)
8364
8365         ;; See whether the article is to be put in the cache.
8366         (and gnus-use-cache
8367              (vectorp (gnus-summary-article-header article))
8368              (save-excursion
8369                (gnus-cache-possibly-enter-article
8370                 gnus-newsgroup-name article
8371                 (gnus-summary-article-header article)
8372                 (= mark gnus-ticked-mark)
8373                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8374
8375         ;; Fix the mark.
8376         (gnus-summary-update-mark mark 'unread)
8377         t))))
8378
8379 (defun gnus-summary-mark-article (&optional article mark no-expire)
8380   "Mark ARTICLE with MARK.  MARK can be any character.
8381 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8382 `??' (dormant) and `?E' (expirable).
8383 If MARK is nil, then the default character `?r' is used.
8384 If ARTICLE is nil, then the article on the current line will be
8385 marked.
8386 Iff NO-EXPIRE, auto-expiry will be inhibited."
8387   ;; The mark might be a string.
8388   (when (stringp mark)
8389     (setq mark (aref mark 0)))
8390   ;; If no mark is given, then we check auto-expiring.
8391   (when (null mark)
8392     (setq mark gnus-del-mark))
8393   (when (and (not no-expire)
8394              gnus-newsgroup-auto-expire
8395              (memq mark gnus-auto-expirable-marks))
8396     (setq mark gnus-expirable-mark))
8397   (let ((article (or article (gnus-summary-article-number)))
8398         (old-mark (gnus-summary-article-mark article)))
8399     ;; Allow the backend to change the mark.
8400     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8401     (if (eq mark old-mark)
8402         t
8403       (unless article
8404         (error "No article on current line"))
8405       (if (not (if (or (= mark gnus-unread-mark)
8406                        (= mark gnus-ticked-mark)
8407                        (= mark gnus-dormant-mark))
8408                    (gnus-mark-article-as-unread article mark)
8409                  (gnus-mark-article-as-read article mark)))
8410           t
8411         ;; See whether the article is to be put in the cache.
8412         (and gnus-use-cache
8413              (not (= mark gnus-canceled-mark))
8414              (vectorp (gnus-summary-article-header article))
8415              (save-excursion
8416                (gnus-cache-possibly-enter-article
8417                 gnus-newsgroup-name article
8418                 (gnus-summary-article-header article)
8419                 (= mark gnus-ticked-mark)
8420                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8421
8422         (when (gnus-summary-goto-subject article nil t)
8423           (let ((buffer-read-only nil))
8424             (gnus-summary-show-thread)
8425             ;; Fix the mark.
8426             (gnus-summary-update-mark mark 'unread)
8427             t))))))
8428
8429 (defun gnus-summary-update-secondary-mark (article)
8430   "Update the secondary (read, process, cache) mark."
8431   (gnus-summary-update-mark
8432    (cond ((memq article gnus-newsgroup-processable)
8433           gnus-process-mark)
8434          ((memq article gnus-newsgroup-cached)
8435           gnus-cached-mark)
8436          ((memq article gnus-newsgroup-replied)
8437           gnus-replied-mark)
8438          ((memq article gnus-newsgroup-saved)
8439           gnus-saved-mark)
8440          (t gnus-unread-mark))
8441    'replied)
8442   (when (gnus-visual-p 'summary-highlight 'highlight)
8443     (gnus-run-hooks 'gnus-summary-update-hook))
8444   t)
8445
8446 (defun gnus-summary-update-mark (mark type)
8447   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8448         (buffer-read-only nil))
8449     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8450     (when forward
8451       (when (looking-at "\r")
8452         (incf forward))
8453       (when (<= (+ forward (point)) (point-max))
8454         ;; Go to the right position on the line.
8455         (goto-char (+ forward (point)))
8456         ;; Replace the old mark with the new mark.
8457         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8458         ;; Optionally update the marks by some user rule.
8459         (when (eq type 'unread)
8460           (gnus-data-set-mark
8461            (gnus-data-find (gnus-summary-article-number)) mark)
8462           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8463
8464 (defun gnus-mark-article-as-read (article &optional mark)
8465   "Enter ARTICLE in the pertinent lists and remove it from others."
8466   ;; Make the article expirable.
8467   (let ((mark (or mark gnus-del-mark)))
8468     (if (= mark gnus-expirable-mark)
8469         (push article gnus-newsgroup-expirable)
8470       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8471     ;; Remove from unread and marked lists.
8472     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8473     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8474     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8475     (push (cons article mark) gnus-newsgroup-reads)
8476     ;; Possibly remove from cache, if that is used.
8477     (when gnus-use-cache
8478       (gnus-cache-enter-remove-article article))
8479     t))
8480
8481 (defun gnus-mark-article-as-unread (article &optional mark)
8482   "Enter ARTICLE in the pertinent lists and remove it from others."
8483   (let ((mark (or mark gnus-ticked-mark)))
8484     (if (<= article 0)
8485         (progn
8486           (gnus-error 1 "Can't mark negative article numbers")
8487           nil)
8488       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8489             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8490             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8491             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8492
8493       ;; Unsuppress duplicates?
8494       (when gnus-suppress-duplicates
8495         (gnus-dup-unsuppress-article article))
8496
8497       (cond ((= mark gnus-ticked-mark)
8498              (push article gnus-newsgroup-marked))
8499             ((= mark gnus-dormant-mark)
8500              (push article gnus-newsgroup-dormant))
8501             (t
8502              (push article gnus-newsgroup-unreads)))
8503       (gnus-pull article gnus-newsgroup-reads)
8504       t)))
8505
8506 (defalias 'gnus-summary-mark-as-unread-forward
8507   'gnus-summary-tick-article-forward)
8508 (make-obsolete 'gnus-summary-mark-as-unread-forward
8509                'gnus-summary-tick-article-forward)
8510 (defun gnus-summary-tick-article-forward (n)
8511   "Tick N articles forwards.
8512 If N is negative, tick backwards instead.
8513 The difference between N and the number of articles ticked is returned."
8514   (interactive "p")
8515   (gnus-summary-mark-forward n gnus-ticked-mark))
8516
8517 (defalias 'gnus-summary-mark-as-unread-backward
8518   'gnus-summary-tick-article-backward)
8519 (make-obsolete 'gnus-summary-mark-as-unread-backward
8520                'gnus-summary-tick-article-backward)
8521 (defun gnus-summary-tick-article-backward (n)
8522   "Tick N articles backwards.
8523 The difference between N and the number of articles ticked is returned."
8524   (interactive "p")
8525   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8526
8527 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8528 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8529 (defun gnus-summary-tick-article (&optional article clear-mark)
8530   "Mark current article as unread.
8531 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8532 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8533   (interactive)
8534   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8535                                        gnus-ticked-mark)))
8536
8537 (defun gnus-summary-mark-as-read-forward (n)
8538   "Mark N articles as read forwards.
8539 If N is negative, mark backwards instead.
8540 The difference between N and the actual number of articles marked is
8541 returned."
8542   (interactive "p")
8543   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8544
8545 (defun gnus-summary-mark-as-read-backward (n)
8546   "Mark the N articles as read backwards.
8547 The difference between N and the actual number of articles marked is
8548 returned."
8549   (interactive "p")
8550   (gnus-summary-mark-forward
8551    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8552
8553 (defun gnus-summary-mark-as-read (&optional article mark)
8554   "Mark current article as read.
8555 ARTICLE specifies the article to be marked as read.
8556 MARK specifies a string to be inserted at the beginning of the line."
8557   (gnus-summary-mark-article article mark))
8558
8559 (defun gnus-summary-clear-mark-forward (n)
8560   "Clear marks from N articles forward.
8561 If N is negative, clear backward instead.
8562 The difference between N and the number of marks cleared is returned."
8563   (interactive "p")
8564   (gnus-summary-mark-forward n gnus-unread-mark))
8565
8566 (defun gnus-summary-clear-mark-backward (n)
8567   "Clear marks from N articles backward.
8568 The difference between N and the number of marks cleared is returned."
8569   (interactive "p")
8570   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8571
8572 (defun gnus-summary-mark-unread-as-read ()
8573   "Intended to be used by `gnus-summary-mark-article-hook'."
8574   (when (memq gnus-current-article gnus-newsgroup-unreads)
8575     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8576
8577 (defun gnus-summary-mark-read-and-unread-as-read ()
8578   "Intended to be used by `gnus-summary-mark-article-hook'."
8579   (let ((mark (gnus-summary-article-mark)))
8580     (when (or (gnus-unread-mark-p mark)
8581               (gnus-read-mark-p mark))
8582       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8583
8584 (defun gnus-summary-mark-region-as-read (point mark all)
8585   "Mark all unread articles between point and mark as read.
8586 If given a prefix, mark all articles between point and mark as read,
8587 even ticked and dormant ones."
8588   (interactive "r\nP")
8589   (save-excursion
8590     (let (article)
8591       (goto-char point)
8592       (beginning-of-line)
8593       (while (and
8594               (< (point) mark)
8595               (progn
8596                 (when (or all
8597                           (memq (setq article (gnus-summary-article-number))
8598                                 gnus-newsgroup-unreads))
8599                   (gnus-summary-mark-article article gnus-del-mark))
8600                 t)
8601               (gnus-summary-find-next))))))
8602
8603 (defun gnus-summary-mark-below (score mark)
8604   "Mark articles with score less than SCORE with MARK."
8605   (interactive "P\ncMark: ")
8606   (setq score (if score
8607                   (prefix-numeric-value score)
8608                 (or gnus-summary-default-score 0)))
8609   (save-excursion
8610     (set-buffer gnus-summary-buffer)
8611     (goto-char (point-min))
8612     (while
8613         (progn
8614           (and (< (gnus-summary-article-score) score)
8615                (gnus-summary-mark-article nil mark))
8616           (gnus-summary-find-next)))))
8617
8618 (defun gnus-summary-kill-below (&optional score)
8619   "Mark articles with score below SCORE as read."
8620   (interactive "P")
8621   (gnus-summary-mark-below score gnus-killed-mark))
8622
8623 (defun gnus-summary-clear-above (&optional score)
8624   "Clear all marks from articles with score above SCORE."
8625   (interactive "P")
8626   (gnus-summary-mark-above score gnus-unread-mark))
8627
8628 (defun gnus-summary-tick-above (&optional score)
8629   "Tick all articles with score above SCORE."
8630   (interactive "P")
8631   (gnus-summary-mark-above score gnus-ticked-mark))
8632
8633 (defun gnus-summary-mark-above (score mark)
8634   "Mark articles with score over SCORE with MARK."
8635   (interactive "P\ncMark: ")
8636   (setq score (if score
8637                   (prefix-numeric-value score)
8638                 (or gnus-summary-default-score 0)))
8639   (save-excursion
8640     (set-buffer gnus-summary-buffer)
8641     (goto-char (point-min))
8642     (while (and (progn
8643                   (when (> (gnus-summary-article-score) score)
8644                     (gnus-summary-mark-article nil mark))
8645                   t)
8646                 (gnus-summary-find-next)))))
8647
8648 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8649 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8650 (defun gnus-summary-limit-include-expunged (&optional no-error)
8651   "Display all the hidden articles that were expunged for low scores."
8652   (interactive)
8653   (let ((buffer-read-only nil))
8654     (let ((scored gnus-newsgroup-scored)
8655           headers h)
8656       (while scored
8657         (unless (gnus-summary-goto-subject (caar scored))
8658           (and (setq h (gnus-summary-article-header (caar scored)))
8659                (< (cdar scored) gnus-summary-expunge-below)
8660                (push h headers)))
8661         (setq scored (cdr scored)))
8662       (if (not headers)
8663           (when (not no-error)
8664             (error "No expunged articles hidden"))
8665         (goto-char (point-min))
8666         (gnus-summary-prepare-unthreaded (nreverse headers))
8667         (goto-char (point-min))
8668         (gnus-summary-position-point)
8669         t))))
8670
8671 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8672   "Mark all unread articles in this newsgroup as read.
8673 If prefix argument ALL is non-nil, ticked and dormant articles will
8674 also be marked as read.
8675 If QUIETLY is non-nil, no questions will be asked.
8676 If TO-HERE is non-nil, it should be a point in the buffer.  All
8677 articles before this point will be marked as read.
8678 Note that this function will only catch up the unread article
8679 in the current summary buffer limitation.
8680 The number of articles marked as read is returned."
8681   (interactive "P")
8682   (prog1
8683       (save-excursion
8684         (when (or quietly
8685                   (not gnus-interactive-catchup) ;Without confirmation?
8686                   gnus-expert-user
8687                   (gnus-y-or-n-p
8688                    (if all
8689                        "Mark absolutely all articles as read? "
8690                      "Mark all unread articles as read? ")))
8691           (if (and not-mark
8692                    (not gnus-newsgroup-adaptive)
8693                    (not gnus-newsgroup-auto-expire)
8694                    (not gnus-suppress-duplicates)
8695                    (or (not gnus-use-cache)
8696                        (eq gnus-use-cache 'passive)))
8697               (progn
8698                 (when all
8699                   (setq gnus-newsgroup-marked nil
8700                         gnus-newsgroup-dormant nil))
8701                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8702             ;; We actually mark all articles as canceled, which we
8703             ;; have to do when using auto-expiry or adaptive scoring.
8704             (gnus-summary-show-all-threads)
8705             (when (gnus-summary-first-subject (not all) t)
8706               (while (and
8707                       (if to-here (< (point) to-here) t)
8708                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8709                       (gnus-summary-find-next (not all) nil nil t))))
8710             (gnus-set-mode-line 'summary))
8711           t))
8712     (gnus-summary-position-point)))
8713
8714 (defun gnus-summary-catchup-to-here (&optional all)
8715   "Mark all unticked articles before the current one as read.
8716 If ALL is non-nil, also mark ticked and dormant articles as read."
8717   (interactive "P")
8718   (save-excursion
8719     (gnus-save-hidden-threads
8720       (let ((beg (point)))
8721         ;; We check that there are unread articles.
8722         (when (or all (gnus-summary-find-prev))
8723           (gnus-summary-catchup all t beg)))))
8724   (gnus-summary-position-point))
8725
8726 (defun gnus-summary-catchup-all (&optional quietly)
8727   "Mark all articles in this newsgroup as read."
8728   (interactive "P")
8729   (gnus-summary-catchup t quietly))
8730
8731 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8732   "Mark all unread articles in this group as read, then exit.
8733 If prefix argument ALL is non-nil, all articles are marked as read."
8734   (interactive "P")
8735   (when (gnus-summary-catchup all quietly nil 'fast)
8736     ;; Select next newsgroup or exit.
8737     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8738              (eq gnus-auto-select-next 'quietly))
8739         (gnus-summary-next-group nil)
8740       (gnus-summary-exit))))
8741
8742 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8743   "Mark all articles in this newsgroup as read, and then exit."
8744   (interactive "P")
8745   (gnus-summary-catchup-and-exit t quietly))
8746
8747 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8748   "Mark all articles in this group as read and select the next group.
8749 If given a prefix, mark all articles, unread as well as ticked, as
8750 read."
8751   (interactive "P")
8752   (save-excursion
8753     (gnus-summary-catchup all))
8754   (gnus-summary-next-group))
8755
8756 ;;;
8757 ;;; with article
8758 ;;;
8759
8760 (defmacro gnus-with-article (article &rest forms)
8761   "Select ARTICLE and perform FORMS in the original article buffer.
8762 Then replace the article with the result."
8763   `(progn
8764      ;; We don't want the article to be marked as read.
8765      (let (gnus-mark-article-hook)
8766        (gnus-summary-select-article t t nil ,article))
8767      (set-buffer gnus-original-article-buffer)
8768      ,@forms
8769      (if (not (gnus-check-backend-function
8770                'request-replace-article (car gnus-article-current)))
8771          (gnus-message 5 "Read-only group; not replacing")
8772        (unless (gnus-request-replace-article
8773                 ,article (car gnus-article-current)
8774                 (current-buffer) t)
8775          (error "Couldn't replace article")))
8776      ;; The cache and backlog have to be flushed somewhat.
8777      (when gnus-keep-backlog
8778        (gnus-backlog-remove-article
8779         (car gnus-article-current) (cdr gnus-article-current)))
8780      (when gnus-use-cache
8781        (gnus-cache-update-article
8782         (car gnus-article-current) (cdr gnus-article-current)))))
8783
8784 (put 'gnus-with-article 'lisp-indent-function 1)
8785 (put 'gnus-with-article 'edebug-form-spec '(form body))
8786
8787 ;; Thread-based commands.
8788
8789 (defun gnus-summary-articles-in-thread (&optional article)
8790   "Return a list of all articles in the current thread.
8791 If ARTICLE is non-nil, return all articles in the thread that starts
8792 with that article."
8793   (let* ((article (or article (gnus-summary-article-number)))
8794          (data (gnus-data-find-list article))
8795          (top-level (gnus-data-level (car data)))
8796          (top-subject
8797           (cond ((null gnus-thread-operation-ignore-subject)
8798                  (gnus-simplify-subject-re
8799                   (mail-header-subject (gnus-data-header (car data)))))
8800                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8801                  (gnus-simplify-subject-fuzzy
8802                   (mail-header-subject (gnus-data-header (car data)))))
8803                 (t nil)))
8804          (end-point (save-excursion
8805                       (if (gnus-summary-go-to-next-thread)
8806                           (point) (point-max))))
8807          articles)
8808     (while (and data
8809                 (< (gnus-data-pos (car data)) end-point))
8810       (when (or (not top-subject)
8811                 (string= top-subject
8812                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8813                              (gnus-simplify-subject-fuzzy
8814                               (mail-header-subject
8815                                (gnus-data-header (car data))))
8816                            (gnus-simplify-subject-re
8817                             (mail-header-subject
8818                              (gnus-data-header (car data)))))))
8819         (push (gnus-data-number (car data)) articles))
8820       (unless (and (setq data (cdr data))
8821                    (> (gnus-data-level (car data)) top-level))
8822         (setq data nil)))
8823     ;; Return the list of articles.
8824     (nreverse articles)))
8825
8826 (defun gnus-summary-rethread-current ()
8827   "Rethread the thread the current article is part of."
8828   (interactive)
8829   (let* ((gnus-show-threads t)
8830          (article (gnus-summary-article-number))
8831          (id (mail-header-id (gnus-summary-article-header)))
8832          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8833     (unless id
8834       (error "No article on the current line"))
8835     (gnus-rebuild-thread id)
8836     (gnus-summary-goto-subject article)))
8837
8838 (defun gnus-summary-reparent-thread ()
8839   "Make the current article child of the marked (or previous) article.
8840
8841 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8842 is non-nil or the Subject: of both articles are the same."
8843   (interactive)
8844   (unless (not (gnus-group-read-only-p))
8845     (error "The current newsgroup does not support article editing"))
8846   (unless (<= (length gnus-newsgroup-processable) 1)
8847     (error "No more than one article may be marked"))
8848   (save-window-excursion
8849     (let ((gnus-article-buffer " *reparent*")
8850           (current-article (gnus-summary-article-number))
8851           ;; First grab the marked article, otherwise one line up.
8852           (parent-article (if (not (null gnus-newsgroup-processable))
8853                               (car gnus-newsgroup-processable)
8854                             (save-excursion
8855                               (if (eq (forward-line -1) 0)
8856                                   (gnus-summary-article-number)
8857                                 (error "Beginning of summary buffer"))))))
8858       (unless (not (eq current-article parent-article))
8859         (error "An article may not be self-referential"))
8860       (let ((message-id (mail-header-id
8861                          (gnus-summary-article-header parent-article))))
8862         (unless (and message-id (not (equal message-id "")))
8863           (error "No message-id in desired parent"))
8864         (gnus-with-article current-article
8865           (save-restriction
8866             (goto-char (point-min))
8867             (message-narrow-to-head)
8868             (if (re-search-forward "^References: " nil t)
8869                 (progn
8870                   (re-search-forward "^[^ \t]" nil t)
8871                   (forward-line -1)
8872                   (end-of-line)
8873                   (insert " " message-id))
8874               (insert "References: " message-id "\n"))))
8875         (set-buffer gnus-summary-buffer)
8876         (gnus-summary-unmark-all-processable)
8877         (gnus-summary-update-article current-article)
8878         (gnus-summary-rethread-current)
8879         (gnus-message 3 "Article %d is now the child of article %d"
8880                       current-article parent-article)))))
8881
8882 (defun gnus-summary-toggle-threads (&optional arg)
8883   "Toggle showing conversation threads.
8884 If ARG is positive number, turn showing conversation threads on."
8885   (interactive "P")
8886   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8887     (setq gnus-show-threads
8888           (if (null arg) (not gnus-show-threads)
8889             (> (prefix-numeric-value arg) 0)))
8890     (gnus-summary-prepare)
8891     (gnus-summary-goto-subject current)
8892     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8893     (gnus-summary-position-point)))
8894
8895 (defun gnus-summary-show-all-threads ()
8896   "Show all threads."
8897   (interactive)
8898   (save-excursion
8899     (let ((buffer-read-only nil))
8900       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8901   (gnus-summary-position-point))
8902
8903 (defun gnus-summary-show-thread ()
8904   "Show thread subtrees.
8905 Returns nil if no thread was there to be shown."
8906   (interactive)
8907   (let ((buffer-read-only nil)
8908         (orig (point))
8909         ;; first goto end then to beg, to have point at beg after let
8910         (end (progn (end-of-line) (point)))
8911         (beg (progn (beginning-of-line) (point))))
8912     (prog1
8913         ;; Any hidden lines here?
8914         (search-forward "\r" end t)
8915       (subst-char-in-region beg end ?\^M ?\n t)
8916       (goto-char orig)
8917       (gnus-summary-position-point))))
8918
8919 (defun gnus-summary-hide-all-threads ()
8920   "Hide all thread subtrees."
8921   (interactive)
8922   (save-excursion
8923     (goto-char (point-min))
8924     (gnus-summary-hide-thread)
8925     (while (zerop (gnus-summary-next-thread 1 t))
8926       (gnus-summary-hide-thread)))
8927   (gnus-summary-position-point))
8928
8929 (defun gnus-summary-hide-thread ()
8930   "Hide thread subtrees.
8931 Returns nil if no threads were there to be hidden."
8932   (interactive)
8933   (let ((buffer-read-only nil)
8934         (start (point))
8935         (article (gnus-summary-article-number)))
8936     (goto-char start)
8937     ;; Go forward until either the buffer ends or the subthread
8938     ;; ends.
8939     (when (and (not (eobp))
8940                (or (zerop (gnus-summary-next-thread 1 t))
8941                    (goto-char (point-max))))
8942       (prog1
8943           (if (and (> (point) start)
8944                    (search-backward "\n" start t))
8945               (progn
8946                 (subst-char-in-region start (point) ?\n ?\^M)
8947                 (gnus-summary-goto-subject article))
8948             (goto-char start)
8949             nil)))))
8950
8951 (defun gnus-summary-go-to-next-thread (&optional previous)
8952   "Go to the same level (or less) next thread.
8953 If PREVIOUS is non-nil, go to previous thread instead.
8954 Return the article number moved to, or nil if moving was impossible."
8955   (let ((level (gnus-summary-thread-level))
8956         (way (if previous -1 1))
8957         (beg (point)))
8958     (forward-line way)
8959     (while (and (not (eobp))
8960                 (< level (gnus-summary-thread-level)))
8961       (forward-line way))
8962     (if (eobp)
8963         (progn
8964           (goto-char beg)
8965           nil)
8966       (setq beg (point))
8967       (prog1
8968           (gnus-summary-article-number)
8969         (goto-char beg)))))
8970
8971 (defun gnus-summary-next-thread (n &optional silent)
8972   "Go to the same level next N'th thread.
8973 If N is negative, search backward instead.
8974 Returns the difference between N and the number of skips actually
8975 done.
8976
8977 If SILENT, don't output messages."
8978   (interactive "p")
8979   (let ((backward (< n 0))
8980         (n (abs n)))
8981     (while (and (> n 0)
8982                 (gnus-summary-go-to-next-thread backward))
8983       (decf n))
8984     (unless silent
8985       (gnus-summary-position-point))
8986     (when (and (not silent) (/= 0 n))
8987       (gnus-message 7 "No more threads"))
8988     n))
8989
8990 (defun gnus-summary-prev-thread (n)
8991   "Go to the same level previous N'th thread.
8992 Returns the difference between N and the number of skips actually
8993 done."
8994   (interactive "p")
8995   (gnus-summary-next-thread (- n)))
8996
8997 (defun gnus-summary-go-down-thread ()
8998   "Go down one level in the current thread."
8999   (let ((children (gnus-summary-article-children)))
9000     (when children
9001       (gnus-summary-goto-subject (car children)))))
9002
9003 (defun gnus-summary-go-up-thread ()
9004   "Go up one level in the current thread."
9005   (let ((parent (gnus-summary-article-parent)))
9006     (when parent
9007       (gnus-summary-goto-subject parent))))
9008
9009 (defun gnus-summary-down-thread (n)
9010   "Go down thread N steps.
9011 If N is negative, go up instead.
9012 Returns the difference between N and how many steps down that were
9013 taken."
9014   (interactive "p")
9015   (let ((up (< n 0))
9016         (n (abs n)))
9017     (while (and (> n 0)
9018                 (if up (gnus-summary-go-up-thread)
9019                   (gnus-summary-go-down-thread)))
9020       (setq n (1- n)))
9021     (gnus-summary-position-point)
9022     (when (/= 0 n)
9023       (gnus-message 7 "Can't go further"))
9024     n))
9025
9026 (defun gnus-summary-up-thread (n)
9027   "Go up thread N steps.
9028 If N is negative, go up instead.
9029 Returns the difference between N and how many steps down that were
9030 taken."
9031   (interactive "p")
9032   (gnus-summary-down-thread (- n)))
9033
9034 (defun gnus-summary-top-thread ()
9035   "Go to the top of the thread."
9036   (interactive)
9037   (while (gnus-summary-go-up-thread))
9038   (gnus-summary-article-number))
9039
9040 (defun gnus-summary-kill-thread (&optional unmark)
9041   "Mark articles under current thread as read.
9042 If the prefix argument is positive, remove any kinds of marks.
9043 If the prefix argument is negative, tick articles instead."
9044   (interactive "P")
9045   (when unmark
9046     (setq unmark (prefix-numeric-value unmark)))
9047   (let ((articles (gnus-summary-articles-in-thread)))
9048     (save-excursion
9049       ;; Expand the thread.
9050       (gnus-summary-show-thread)
9051       ;; Mark all the articles.
9052       (while articles
9053         (gnus-summary-goto-subject (car articles))
9054         (cond ((null unmark)
9055                (gnus-summary-mark-article-as-read gnus-killed-mark))
9056               ((> unmark 0)
9057                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9058               (t
9059                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9060         (setq articles (cdr articles))))
9061     ;; Hide killed subtrees.
9062     (and (null unmark)
9063          gnus-thread-hide-killed
9064          (gnus-summary-hide-thread))
9065     ;; If marked as read, go to next unread subject.
9066     (when (null unmark)
9067       ;; Go to next unread subject.
9068       (gnus-summary-next-subject 1 t)))
9069   (gnus-set-mode-line 'summary))
9070
9071 ;; Summary sorting commands
9072
9073 (defun gnus-summary-sort-by-number (&optional reverse)
9074   "Sort the summary buffer by article number.
9075 Argument REVERSE means reverse order."
9076   (interactive "P")
9077   (gnus-summary-sort 'number reverse))
9078
9079 (defun gnus-summary-sort-by-author (&optional reverse)
9080   "Sort the summary buffer by author name alphabetically.
9081 If `case-fold-search' is non-nil, case of letters is ignored.
9082 Argument REVERSE means reverse order."
9083   (interactive "P")
9084   (gnus-summary-sort 'author reverse))
9085
9086 (defun gnus-summary-sort-by-subject (&optional reverse)
9087   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9088 If `case-fold-search' is non-nil, case of letters is ignored.
9089 Argument REVERSE means reverse order."
9090   (interactive "P")
9091   (gnus-summary-sort 'subject reverse))
9092
9093 (defun gnus-summary-sort-by-date (&optional reverse)
9094   "Sort the summary buffer by date.
9095 Argument REVERSE means reverse order."
9096   (interactive "P")
9097   (gnus-summary-sort 'date reverse))
9098
9099 (defun gnus-summary-sort-by-score (&optional reverse)
9100   "Sort the summary buffer by score.
9101 Argument REVERSE means reverse order."
9102   (interactive "P")
9103   (gnus-summary-sort 'score reverse))
9104
9105 (defun gnus-summary-sort-by-lines (&optional reverse)
9106   "Sort the summary buffer by the number of lines.
9107 Argument REVERSE means reverse order."
9108   (interactive "P")
9109   (gnus-summary-sort 'lines reverse))
9110
9111 (defun gnus-summary-sort-by-chars (&optional reverse)
9112   "Sort the summary buffer by article length.
9113 Argument REVERSE means reverse order."
9114   (interactive "P")
9115   (gnus-summary-sort 'chars reverse))
9116
9117 (defun gnus-summary-sort (predicate reverse)
9118   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9119   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9120          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9121          (gnus-thread-sort-functions
9122           (if (not reverse)
9123               thread
9124             `(lambda (t1 t2)
9125                (,thread t2 t1))))
9126          (gnus-sort-gathered-threads-function
9127           gnus-thread-sort-functions)
9128          (gnus-article-sort-functions
9129           (if (not reverse)
9130               article
9131             `(lambda (t1 t2)
9132                (,article t2 t1))))
9133          (buffer-read-only)
9134          (gnus-summary-prepare-hook nil))
9135     ;; We do the sorting by regenerating the threads.
9136     (gnus-summary-prepare)
9137     ;; Hide subthreads if needed.
9138     (when (and gnus-show-threads gnus-thread-hide-subtree)
9139       (gnus-summary-hide-all-threads))))
9140
9141 ;; Summary saving commands.
9142
9143 (defun gnus-summary-save-article (&optional n not-saved)
9144   "Save the current article using the default saver function.
9145 If N is a positive number, save the N next articles.
9146 If N is a negative number, save the N previous articles.
9147 If N is nil and any articles have been marked with the process mark,
9148 save those articles instead.
9149 The variable `gnus-default-article-saver' specifies the saver function."
9150   (interactive "P")
9151   (let* ((articles (gnus-summary-work-articles n))
9152          (save-buffer (save-excursion
9153                         (nnheader-set-temp-buffer " *Gnus Save*")))
9154          (num (length articles))
9155          header file)
9156     (dolist (article articles)
9157       (setq header (gnus-summary-article-header article))
9158       (if (not (vectorp header))
9159           ;; This is a pseudo-article.
9160           (if (assq 'name header)
9161               (gnus-copy-file (cdr (assq 'name header)))
9162             (gnus-message 1 "Article %d is unsaveable" article))
9163         ;; This is a real article.
9164         (save-window-excursion
9165           (gnus-summary-select-article t nil nil article))
9166         (save-excursion
9167           (set-buffer save-buffer)
9168           (erase-buffer)
9169           (insert-buffer-substring gnus-original-article-buffer))
9170         (setq file (gnus-article-save save-buffer file num))
9171         (gnus-summary-remove-process-mark article)
9172         (unless not-saved
9173           (gnus-summary-set-saved-mark article))))
9174     (gnus-kill-buffer save-buffer)
9175     (gnus-summary-position-point)
9176     (gnus-set-mode-line 'summary)
9177     n))
9178
9179 (defun gnus-summary-pipe-output (&optional arg)
9180   "Pipe the current article to a subprocess.
9181 If N is a positive number, pipe the N next articles.
9182 If N is a negative number, pipe the N previous articles.
9183 If N is nil and any articles have been marked with the process mark,
9184 pipe those articles instead."
9185   (interactive "P")
9186   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9187     (gnus-summary-save-article arg t))
9188   (gnus-configure-windows 'pipe))
9189
9190 (defun gnus-summary-save-article-mail (&optional arg)
9191   "Append the current article to an mail file.
9192 If N is a positive number, save the N next articles.
9193 If N is a negative number, save the N previous articles.
9194 If N is nil and any articles have been marked with the process mark,
9195 save those articles instead."
9196   (interactive "P")
9197   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9198     (gnus-summary-save-article arg)))
9199
9200 (defun gnus-summary-save-article-rmail (&optional arg)
9201   "Append the current article to an rmail file.
9202 If N is a positive number, save the N next articles.
9203 If N is a negative number, save the N previous articles.
9204 If N is nil and any articles have been marked with the process mark,
9205 save those articles instead."
9206   (interactive "P")
9207   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9208     (gnus-summary-save-article arg)))
9209
9210 (defun gnus-summary-save-article-file (&optional arg)
9211   "Append the current article to a file.
9212 If N is a positive number, save the N next articles.
9213 If N is a negative number, save the N previous articles.
9214 If N is nil and any articles have been marked with the process mark,
9215 save those articles instead."
9216   (interactive "P")
9217   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9218     (gnus-summary-save-article arg)))
9219
9220 (defun gnus-summary-write-article-file (&optional arg)
9221   "Write the current article to a file, deleting the previous file.
9222 If N is a positive number, save the N next articles.
9223 If N is a negative number, save the N previous articles.
9224 If N is nil and any articles have been marked with the process mark,
9225 save those articles instead."
9226   (interactive "P")
9227   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9228     (gnus-summary-save-article arg)))
9229
9230 (defun gnus-summary-save-article-body-file (&optional arg)
9231   "Append the current article body to a file.
9232 If N is a positive number, save the N next articles.
9233 If N is a negative number, save the N previous articles.
9234 If N is nil and any articles have been marked with the process mark,
9235 save those articles instead."
9236   (interactive "P")
9237   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9238     (gnus-summary-save-article arg)))
9239
9240 (defun gnus-summary-pipe-message (program)
9241   "Pipe the current article through PROGRAM."
9242   (interactive "sProgram: ")
9243   (gnus-summary-select-article)
9244   (let ((mail-header-separator ""))
9245     (gnus-eval-in-buffer-window gnus-article-buffer
9246       (save-restriction
9247         (widen)
9248         (let ((start (window-start))
9249               buffer-read-only)
9250           (message-pipe-buffer-body program)
9251           (set-window-start (get-buffer-window (current-buffer)) start))))))
9252
9253 (defun gnus-get-split-value (methods)
9254   "Return a value based on the split METHODS."
9255   (let (split-name method result match)
9256     (when methods
9257       (save-excursion
9258         (set-buffer gnus-original-article-buffer)
9259         (save-restriction
9260           (nnheader-narrow-to-headers)
9261           (while (and methods (not split-name))
9262             (goto-char (point-min))
9263             (setq method (pop methods))
9264             (setq match (car method))
9265             (when (cond
9266                    ((stringp match)
9267                     ;; Regular expression.
9268                     (ignore-errors
9269                       (re-search-forward match nil t)))
9270                    ((gnus-functionp match)
9271                     ;; Function.
9272                     (save-restriction
9273                       (widen)
9274                       (setq result (funcall match gnus-newsgroup-name))))
9275                    ((consp match)
9276                     ;; Form.
9277                     (save-restriction
9278                       (widen)
9279                       (setq result (eval match)))))
9280               (setq split-name (cdr method))
9281               (cond ((stringp result)
9282                      (push (expand-file-name
9283                             result gnus-article-save-directory)
9284                            split-name))
9285                     ((consp result)
9286                      (setq split-name (append result split-name)))))))))
9287     (nreverse split-name)))
9288
9289 (defun gnus-valid-move-group-p (group)
9290   (and (boundp group)
9291        (symbol-name group)
9292        (symbol-value group)
9293        (gnus-get-function (gnus-find-method-for-group
9294                            (symbol-name group)) 'request-accept-article t)))
9295
9296 (defun gnus-read-move-group-name (prompt default articles prefix)
9297   "Read a group name."
9298   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9299          (minibuffer-confirm-incomplete nil) ; XEmacs
9300          (prom
9301           (format "%s %s to:"
9302                   prompt
9303                   (if (> (length articles) 1)
9304                       (format "these %d articles" (length articles))
9305                     "this article")))
9306          (to-newsgroup
9307           (cond
9308            ((null split-name)
9309             (gnus-completing-read default prom
9310                                   gnus-active-hashtb
9311                                   'gnus-valid-move-group-p
9312                                   nil prefix
9313                                   'gnus-group-history))
9314            ((= 1 (length split-name))
9315             (gnus-completing-read (car split-name) prom
9316                                   gnus-active-hashtb
9317                                   'gnus-valid-move-group-p
9318                                   nil nil
9319                                   'gnus-group-history))
9320            (t
9321             (gnus-completing-read nil prom
9322                                   (mapcar (lambda (el) (list el))
9323                                           (nreverse split-name))
9324                                   nil nil nil
9325                                   'gnus-group-history))))
9326          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9327     (when to-newsgroup
9328       (if (or (string= to-newsgroup "")
9329               (string= to-newsgroup prefix))
9330           (setq to-newsgroup default))
9331       (unless to-newsgroup
9332         (error "No group name entered"))
9333       (or (gnus-active to-newsgroup)
9334           (gnus-activate-group to-newsgroup nil nil to-method)
9335           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9336                                      to-newsgroup))
9337               (or (and (gnus-request-create-group to-newsgroup to-method)
9338                        (gnus-activate-group
9339                         to-newsgroup nil nil to-method)
9340                        (gnus-subscribe-group to-newsgroup))
9341                   (error "Couldn't create group %s" to-newsgroup)))
9342           (error "No such group: %s" to-newsgroup)))
9343     to-newsgroup))
9344
9345 (defun gnus-summary-save-parts (type dir n &optional reverse)
9346   "Save parts matching TYPE to DIR.
9347 If REVERSE, save parts that do not match TYPE."
9348   (interactive
9349    (list (read-string "Save parts of type: " 
9350                       (or (car gnus-summary-save-parts-type-history)
9351                           gnus-summary-save-parts-default-mime)
9352                       'gnus-summary-save-parts-type-history)
9353          (setq gnus-summary-save-parts-last-directory
9354                (read-file-name "Save to directory: " 
9355                                gnus-summary-save-parts-last-directory
9356                                nil t))
9357          current-prefix-arg))
9358   (gnus-summary-iterate n
9359     (let ((gnus-display-mime-function nil)
9360           (gnus-inhibit-treatment t))
9361       (gnus-summary-select-article))
9362     (save-excursion
9363       (set-buffer gnus-article-buffer)
9364       (let ((handles (or gnus-article-mime-handles
9365                          (mm-dissect-buffer) (mm-uu-dissect))))
9366         (when handles
9367           (gnus-summary-save-parts-1 type dir handles reverse)
9368           (unless gnus-article-mime-handles ;; Don't destroy this case.
9369             (mm-destroy-parts handles)))))))
9370
9371 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9372   (if (stringp (car handle))
9373       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9374               (cdr handle))
9375     (when (if reverse
9376               (not (string-match type (mm-handle-media-type handle)))
9377             (string-match type (mm-handle-media-type handle)))
9378       (let ((file (expand-file-name
9379                    (file-name-nondirectory
9380                     (or
9381                      (mail-content-type-get
9382                       (mm-handle-disposition handle) 'filename)
9383                      (concat gnus-newsgroup-name
9384                              "." (number-to-string
9385                                   (cdr gnus-article-current)))))
9386                    dir)))
9387         (unless (file-exists-p file)
9388           (mm-save-part-to-file handle file))))))
9389
9390 ;; Summary extract commands
9391
9392 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9393   (let ((buffer-read-only nil)
9394         (article (gnus-summary-article-number))
9395         after-article b e)
9396     (unless (gnus-summary-goto-subject article)
9397       (error "No such article: %d" article))
9398     (gnus-summary-position-point)
9399     ;; If all commands are to be bunched up on one line, we collect
9400     ;; them here.
9401     (unless gnus-view-pseudos-separately
9402       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9403             files action)
9404         (while ps
9405           (setq action (cdr (assq 'action (car ps))))
9406           (setq files (list (cdr (assq 'name (car ps)))))
9407           (while (and ps (cdr ps)
9408                       (string= (or action "1")
9409                                (or (cdr (assq 'action (cadr ps))) "2")))
9410             (push (cdr (assq 'name (cadr ps))) files)
9411             (setcdr ps (cddr ps)))
9412           (when files
9413             (when (not (string-match "%s" action))
9414               (push " " files))
9415             (push " " files)
9416             (when (assq 'execute (car ps))
9417               (setcdr (assq 'execute (car ps))
9418                       (funcall (if (string-match "%s" action)
9419                                    'format 'concat)
9420                                action
9421                                (mapconcat
9422                                 (lambda (f)
9423                                   (if (equal f " ")
9424                                       f
9425                                     (gnus-quote-arg-for-sh-or-csh f)))
9426                                 files " ")))))
9427           (setq ps (cdr ps)))))
9428     (if (and gnus-view-pseudos (not not-view))
9429         (while pslist
9430           (when (assq 'execute (car pslist))
9431             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9432                                   (eq gnus-view-pseudos 'not-confirm)))
9433           (setq pslist (cdr pslist)))
9434       (save-excursion
9435         (while pslist
9436           (setq after-article (or (cdr (assq 'article (car pslist)))
9437                                   (gnus-summary-article-number)))
9438           (gnus-summary-goto-subject after-article)
9439           (forward-line 1)
9440           (setq b (point))
9441           (insert "    " (file-name-nondirectory
9442                           (cdr (assq 'name (car pslist))))
9443                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9444           (setq e (point))
9445           (forward-line -1)             ; back to `b'
9446           (gnus-add-text-properties
9447            b (1- e) (list 'gnus-number gnus-reffed-article-number
9448                           gnus-mouse-face-prop gnus-mouse-face))
9449           (gnus-data-enter
9450            after-article gnus-reffed-article-number
9451            gnus-unread-mark b (car pslist) 0 (- e b))
9452           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9453           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9454           (setq pslist (cdr pslist)))))))
9455
9456 (defun gnus-pseudos< (p1 p2)
9457   (let ((c1 (cdr (assq 'action p1)))
9458         (c2 (cdr (assq 'action p2))))
9459     (and c1 c2 (string< c1 c2))))
9460
9461 (defun gnus-request-pseudo-article (props)
9462   (cond ((assq 'execute props)
9463          (gnus-execute-command (cdr (assq 'execute props)))))
9464   (let ((gnus-current-article (gnus-summary-article-number)))
9465     (gnus-run-hooks 'gnus-mark-article-hook)))
9466
9467 (defun gnus-execute-command (command &optional automatic)
9468   (save-excursion
9469     (gnus-article-setup-buffer)
9470     (set-buffer gnus-article-buffer)
9471     (setq buffer-read-only nil)
9472     (let ((command (if automatic command
9473                      (read-string "Command: " (cons command 0)))))
9474       (erase-buffer)
9475       (insert "$ " command "\n\n")
9476       (if gnus-view-pseudo-asynchronously
9477           (start-process "gnus-execute" (current-buffer) shell-file-name
9478                          shell-command-switch command)
9479         (call-process shell-file-name nil t nil
9480                       shell-command-switch command)))))
9481
9482 ;; Summary kill commands.
9483
9484 (defun gnus-summary-edit-global-kill (article)
9485   "Edit the \"global\" kill file."
9486   (interactive (list (gnus-summary-article-number)))
9487   (gnus-group-edit-global-kill article))
9488
9489 (defun gnus-summary-edit-local-kill ()
9490   "Edit a local kill file applied to the current newsgroup."
9491   (interactive)
9492   (setq gnus-current-headers (gnus-summary-article-header))
9493   (gnus-group-edit-local-kill
9494    (gnus-summary-article-number) gnus-newsgroup-name))
9495
9496 ;;; Header reading.
9497
9498 (defun gnus-read-header (id &optional header)
9499   "Read the headers of article ID and enter them into the Gnus system."
9500   (let ((group gnus-newsgroup-name)
9501         (gnus-override-method
9502          (or
9503           gnus-override-method
9504           (and (gnus-news-group-p gnus-newsgroup-name)
9505                (car (gnus-refer-article-methods)))))
9506         where)
9507     ;; First we check to see whether the header in question is already
9508     ;; fetched.
9509     (if (stringp id)
9510         ;; This is a Message-ID.
9511         (setq header (or header (gnus-id-to-header id)))
9512       ;; This is an article number.
9513       (setq header (or header (gnus-summary-article-header id))))
9514     (if (and header
9515              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9516         ;; We have found the header.
9517         header
9518       ;; If this is a sparse article, we have to nix out its
9519       ;; previous entry in the thread hashtb.
9520       (when (and header
9521                  (gnus-summary-article-sparse-p (mail-header-number header)))
9522         (let* ((parent (gnus-parent-id (mail-header-references header)))
9523                (thread (and parent (gnus-id-to-thread parent))))
9524           (when thread
9525             (delq (assq header thread) thread))))
9526       ;; We have to really fetch the header to this article.
9527       (save-excursion
9528         (set-buffer nntp-server-buffer)
9529         (when (setq where (gnus-request-head id group))
9530           (nnheader-fold-continuation-lines)
9531           (goto-char (point-max))
9532           (insert ".\n")
9533           (goto-char (point-min))
9534           (insert "211 ")
9535           (princ (cond
9536                   ((numberp id) id)
9537                   ((cdr where) (cdr where))
9538                   (header (mail-header-number header))
9539                   (t gnus-reffed-article-number))
9540                  (current-buffer))
9541           (insert " Article retrieved.\n"))
9542         (if (or (not where)
9543                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9544             ()                          ; Malformed head.
9545           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9546             (when (and (stringp id)
9547                        (not (string= (gnus-group-real-name group)
9548                                      (car where))))
9549               ;; If we fetched by Message-ID and the article came
9550               ;; from a different group, we fudge some bogus article
9551               ;; numbers for this article.
9552               (mail-header-set-number header gnus-reffed-article-number))
9553             (save-excursion
9554               (set-buffer gnus-summary-buffer)
9555               (decf gnus-reffed-article-number)
9556               (gnus-remove-header (mail-header-number header))
9557               (push header gnus-newsgroup-headers)
9558               (setq gnus-current-headers header)
9559               (push (mail-header-number header) gnus-newsgroup-limit)))
9560           header)))))
9561
9562 (defun gnus-remove-header (number)
9563   "Remove header NUMBER from `gnus-newsgroup-headers'."
9564   (if (and gnus-newsgroup-headers
9565            (= number (mail-header-number (car gnus-newsgroup-headers))))
9566       (pop gnus-newsgroup-headers)
9567     (let ((headers gnus-newsgroup-headers))
9568       (while (and (cdr headers)
9569                   (not (= number (mail-header-number (cadr headers)))))
9570         (pop headers))
9571       (when (cdr headers)
9572         (setcdr headers (cddr headers))))))
9573
9574 ;;;
9575 ;;; summary highlights
9576 ;;;
9577
9578 (defun gnus-highlight-selected-summary ()
9579   "Highlight selected article in summary buffer."
9580   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9581   (when gnus-summary-selected-face
9582     (save-excursion
9583       (let* ((beg (progn (beginning-of-line) (point)))
9584              (end (progn (end-of-line) (point)))
9585              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9586              (from (if (get-text-property beg gnus-mouse-face-prop)
9587                        beg
9588                      (or (next-single-property-change
9589                           beg gnus-mouse-face-prop nil end)
9590                          beg)))
9591              (to
9592               (if (= from end)
9593                   (- from 2)
9594                 (or (next-single-property-change
9595                      from gnus-mouse-face-prop nil end)
9596                     end))))
9597         ;; If no mouse-face prop on line we will have to = from = end,
9598         ;; so we highlight the entire line instead.
9599         (when (= (+ to 2) from)
9600           (setq from beg)
9601           (setq to end))
9602         (if gnus-newsgroup-selected-overlay
9603             ;; Move old overlay.
9604             (gnus-move-overlay
9605              gnus-newsgroup-selected-overlay from to (current-buffer))
9606           ;; Create new overlay.
9607           (gnus-overlay-put
9608            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9609            'face gnus-summary-selected-face))))))
9610
9611 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9612 (defun gnus-summary-highlight-line ()
9613   "Highlight current line according to `gnus-summary-highlight'."
9614   (let* ((list gnus-summary-highlight)
9615          (p (point))
9616          (end (progn (end-of-line) (point)))
9617          ;; now find out where the line starts and leave point there.
9618          (beg (progn (beginning-of-line) (point)))
9619          (article (gnus-summary-article-number))
9620          (score (or (cdr (assq (or article gnus-current-article)
9621                                gnus-newsgroup-scored))
9622                     gnus-summary-default-score 0))
9623          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9624          (inhibit-read-only t))
9625     ;; Eval the cars of the lists until we find a match.
9626     (let ((default gnus-summary-default-score))
9627       (while (and list
9628                   (not (eval (caar list))))
9629         (setq list (cdr list))))
9630     (let ((face (cdar list)))
9631       (unless (eq face (get-text-property beg 'face))
9632         (gnus-put-text-property-excluding-characters-with-faces
9633          beg end 'face
9634          (setq face (if (boundp face) (symbol-value face) face)))
9635         (when gnus-summary-highlight-line-function
9636           (funcall gnus-summary-highlight-line-function article face))))
9637     (goto-char p)))
9638
9639 (defun gnus-update-read-articles (group unread &optional compute)
9640   "Update the list of read articles in GROUP."
9641   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9642          (entry (gnus-gethash group gnus-newsrc-hashtb))
9643          (info (nth 2 entry))
9644          (prev 1)
9645          (unread (sort (copy-sequence unread) '<))
9646          read)
9647     (if (or (not info) (not active))
9648         ;; There is no info on this group if it was, in fact,
9649         ;; killed.  Gnus stores no information on killed groups, so
9650         ;; there's nothing to be done.
9651         ;; One could store the information somewhere temporarily,
9652         ;; perhaps...  Hmmm...
9653         ()
9654       ;; Remove any negative articles numbers.
9655       (while (and unread (< (car unread) 0))
9656         (setq unread (cdr unread)))
9657       ;; Remove any expired article numbers
9658       (while (and unread (< (car unread) (car active)))
9659         (setq unread (cdr unread)))
9660       ;; Compute the ranges of read articles by looking at the list of
9661       ;; unread articles.
9662       (while unread
9663         (when (/= (car unread) prev)
9664           (push (if (= prev (1- (car unread))) prev
9665                   (cons prev (1- (car unread))))
9666                 read))
9667         (setq prev (1+ (car unread)))
9668         (setq unread (cdr unread)))
9669       (when (<= prev (cdr active))
9670         (push (cons prev (cdr active)) read))
9671       (setq read (if (> (length read) 1) (nreverse read) read))
9672       (if compute
9673           read
9674         (save-excursion
9675           (let (setmarkundo)
9676             ;; Propagate the read marks to the backend.
9677             (when (gnus-check-backend-function 'request-set-mark group)
9678               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9679                     (add (gnus-remove-from-range read (gnus-info-read info))))
9680                 (when (or add del)
9681                   (unless (gnus-check-group group)
9682                     (error "Can't open server for %s" group))
9683                   (gnus-request-set-mark
9684                    group (delq nil (list (if add (list add 'add '(read)))
9685                                          (if del (list del 'del '(read))))))
9686                   (setq setmarkundo
9687                         `(gnus-request-set-mark
9688                           ,group
9689                           ',(delq nil (list
9690                                        (if del (list del 'add '(read)))
9691                                        (if add (list add 'del '(read))))))))))
9692             (set-buffer gnus-group-buffer)
9693             (gnus-undo-register
9694               `(progn
9695                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9696                  (gnus-info-set-read ',info ',(gnus-info-read info))
9697                  (gnus-get-unread-articles-in-group ',info 
9698                                                     (gnus-active ,group))
9699                  (gnus-group-update-group ,group t)
9700                  ,setmarkundo))))
9701         ;; Enter this list into the group info.
9702         (gnus-info-set-read info read)
9703         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9704         (gnus-get-unread-articles-in-group info (gnus-active group))
9705         t))))
9706
9707 (defun gnus-offer-save-summaries ()
9708   "Offer to save all active summary buffers."
9709   (save-excursion
9710     (let ((buflist (buffer-list))
9711           buffers bufname)
9712       ;; Go through all buffers and find all summaries.
9713       (while buflist
9714         (and (setq bufname (buffer-name (car buflist)))
9715              (string-match "Summary" bufname)
9716              (save-excursion
9717                (set-buffer bufname)
9718                ;; We check that this is, indeed, a summary buffer.
9719                (and (eq major-mode 'gnus-summary-mode)
9720                     ;; Also make sure this isn't bogus.
9721                     gnus-newsgroup-prepared
9722                     ;; Also make sure that this isn't a dead summary buffer.
9723                     (not gnus-dead-summary-mode)))
9724              (push bufname buffers))
9725         (setq buflist (cdr buflist)))
9726       ;; Go through all these summary buffers and offer to save them.
9727       (when buffers
9728         (map-y-or-n-p
9729          "Update summary buffer %s? "
9730          (lambda (buf)
9731            (switch-to-buffer buf)
9732            (gnus-summary-exit))
9733          buffers)))))
9734
9735
9736 ;;; @ for mime-partial
9737 ;;;
9738
9739 (defun gnus-request-partial-message ()
9740   (save-excursion
9741     (let ((number (gnus-summary-article-number))
9742           (group gnus-newsgroup-name)
9743           (mother gnus-article-buffer))
9744       (set-buffer (get-buffer-create " *Partial Article*"))
9745       (erase-buffer)
9746       (setq mime-preview-buffer mother)
9747       (gnus-request-article-this-buffer number group)
9748       (mime-parse-buffer)
9749       )))
9750
9751 (autoload 'mime-combine-message/partial-pieces-automatically
9752   "mime-partial"
9753   "Internal method to combine message/partial messages automatically.")
9754
9755 (mime-add-condition
9756  'action '((type . message)(subtype . partial)
9757            (major-mode . gnus-original-article-mode)
9758            (method . mime-combine-message/partial-pieces-automatically)
9759            (summary-buffer-exp . gnus-summary-buffer)
9760            (request-partial-message-method . gnus-request-partial-message)
9761            ))
9762
9763
9764 ;;; @ for message/rfc822
9765 ;;;
9766
9767 (defun gnus-mime-extract-message/rfc822 (entity situation)
9768   (let (group article num cwin swin cur)
9769     (with-temp-buffer
9770       (mime-insert-entity-content entity)
9771       (setq group (or (cdr (assq 'group situation))
9772                       (completing-read "Group: "
9773                                        gnus-active-hashtb
9774                                        nil
9775                                        (gnus-read-active-file-p)
9776                                        gnus-newsgroup-name))
9777             article (gnus-request-accept-article group)))
9778     (when (and (consp article)
9779                (numberp (setq article (cdr article))))
9780       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9781             cwin (get-buffer-window (current-buffer) t))
9782       (save-window-excursion
9783         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9784             (select-window swin)
9785           (set-buffer gnus-summary-buffer))
9786         (setq cur gnus-current-article)
9787         (forward-line num)
9788         (let (gnus-show-threads)
9789           (gnus-summary-goto-subject article t))
9790         (gnus-summary-clear-mark-forward 1)
9791         (gnus-summary-goto-subject cur))
9792       (when (and cwin (window-frame cwin))
9793         (select-frame (window-frame cwin)))
9794       (when (boundp 'mime-acting-situation-to-override)
9795         (set-alist 'mime-acting-situation-to-override
9796                    'group
9797                    group)
9798         (set-alist 'mime-acting-situation-to-override
9799                    'after-method
9800                    `(progn
9801                       (save-current-buffer
9802                         (set-buffer gnus-group-buffer)
9803                         (gnus-activate-group ,group))
9804                       (gnus-summary-goto-article ,cur
9805                                                  gnus-show-all-headers)))
9806         (set-alist 'mime-acting-situation-to-override
9807                    'number num)))))
9808
9809 (mime-add-condition
9810  'action '((type . message)(subtype . rfc822)
9811            (major-mode . gnus-original-article-mode)
9812            (method . gnus-mime-extract-message/rfc822)
9813            (mode . "extract")
9814            ))
9815
9816 (mime-add-condition
9817  'action '((type . message)(subtype . news)
9818            (major-mode . gnus-original-article-mode)
9819            (method . gnus-mime-extract-message/rfc822)
9820            (mode . "extract")
9821            ))
9822
9823 (defun gnus-mime-extract-multipart (entity situation)
9824   (let ((children (mime-entity-children entity))
9825         mime-acting-situation-to-override
9826         f)
9827     (while children
9828       (mime-play-entity (car children)
9829                         (cons (assq 'mode situation)
9830                               mime-acting-situation-to-override))
9831       (setq children (cdr children)))
9832     (if (setq f (cdr (assq 'after-method
9833                            mime-acting-situation-to-override)))
9834         (eval f)
9835       )))
9836
9837 (mime-add-condition
9838  'action '((type . multipart)
9839            (method . gnus-mime-extract-multipart)
9840            (mode . "extract")
9841            )
9842  'with-default)
9843
9844
9845 ;;; @ end
9846 ;;;
9847
9848 (defun gnus-summary-setup-default-charset ()
9849   "Setup newsgroup default charset."
9850   (if (equal gnus-newsgroup-name "nndraft:drafts")
9851       (setq gnus-newsgroup-charset nil)
9852     (let* ((name (and gnus-newsgroup-name
9853                       (gnus-group-real-name gnus-newsgroup-name)))
9854            (ignored-charsets
9855             (or gnus-newsgroup-ephemeral-ignored-charsets
9856                 (append
9857                  (and gnus-newsgroup-name
9858                       (or (gnus-group-find-parameter gnus-newsgroup-name
9859                                                      'ignored-charsets t)
9860                           (let ((alist gnus-group-ignored-charsets-alist)
9861                                 elem (charsets nil))
9862                             (while (setq elem (pop alist))
9863                               (when (and name
9864                                          (string-match (car elem) name))
9865                                 (setq alist nil
9866                                       charsets (cdr elem))))
9867                             charsets)))
9868                  gnus-newsgroup-ignored-charsets))))
9869       (setq gnus-newsgroup-charset
9870             (or gnus-newsgroup-ephemeral-charset
9871                 (and gnus-newsgroup-name
9872                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9873                          (let ((alist gnus-group-charset-alist)
9874                                elem charset)
9875                            (while (setq elem (pop alist))
9876                              (when (and name
9877                                         (string-match (car elem) name))
9878                                (setq alist nil
9879                                      charset (cadr elem))))
9880                            charset)))
9881                 gnus-default-charset))
9882       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9883            ignored-charsets))))
9884
9885 ;;;
9886 ;;; Mime Commands
9887 ;;;
9888
9889 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9890   "Display the current article buffer fully MIME-buttonized.
9891 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9892 treated as multipart/mixed."
9893   (interactive "P")
9894   (require 'gnus-art)
9895   (let ((gnus-unbuttonized-mime-types nil)
9896         (gnus-mime-display-multipart-as-mixed show-all-parts))
9897     (gnus-summary-show-article)))
9898
9899 (defun gnus-summary-repair-multipart (article)
9900   "Add a Content-Type header to a multipart article without one."
9901   (interactive (list (gnus-summary-article-number)))
9902   (gnus-with-article article
9903     (message-narrow-to-head)
9904     (goto-char (point-max))
9905     (widen)
9906     (when (search-forward "\n--" nil t)
9907       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9908         (message-narrow-to-head)
9909         (message-remove-header "Mime-Version")
9910         (message-remove-header "Content-Type")
9911         (goto-char (point-max))
9912         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9913                         separator))
9914         (insert "Mime-Version: 1.0\n")
9915         (widen))))
9916   (let (gnus-mark-article-hook)
9917     (gnus-summary-select-article t t nil article)))
9918
9919 (defun gnus-summary-toggle-display-buttonized ()
9920   "Toggle the buttonizing of the article buffer."
9921   (interactive)
9922   (require 'gnus-art)
9923   (if (setq gnus-inhibit-mime-unbuttonizing
9924             (not gnus-inhibit-mime-unbuttonizing))
9925       (let ((gnus-unbuttonized-mime-types nil))
9926         (gnus-summary-show-article))
9927     (gnus-summary-show-article)))
9928
9929 ;;;
9930 ;;; Intelli-mouse commmands
9931 ;;;
9932
9933 (defun gnus-wheel-summary-scroll (event)
9934   (interactive "e")
9935   (let ((amount (if (memq 'shift (event-modifiers event))
9936                     (car gnus-wheel-scroll-amount)
9937                   (cdr gnus-wheel-scroll-amount)))
9938         (direction (- (* (static-if (featurep 'xemacs)
9939                              (event-button event)
9940                            (cond ((eq 'mouse-4 (event-basic-type event))
9941                                   4)
9942                                  ((eq 'mouse-5 (event-basic-type event))
9943                                   5)))
9944                          2) 9))
9945         edge)
9946     (gnus-summary-scroll-up (* amount direction))
9947     (when (gnus-eval-in-buffer-window gnus-article-buffer
9948             (save-restriction
9949               (widen)
9950               (and (if (< 0 direction)
9951                        (gnus-article-next-page 0)
9952                      (gnus-article-prev-page 0)
9953                      (bobp))
9954                    (if (setq edge (get-text-property
9955                                    (point-min) 'gnus-wheel-edge))
9956                        (setq edge (* edge direction))
9957                      (setq edge -1))
9958                    (or (plusp edge)
9959                        (let ((buffer-read-only nil)
9960                              (inhibit-read-only t))
9961                          (put-text-property (point-min) (point-max)
9962                                             'gnus-wheel-edge direction)
9963                          nil))
9964                    (or (> edge gnus-wheel-edge-resistance)
9965                        (let ((buffer-read-only nil)
9966                              (inhibit-read-only t))
9967                          (put-text-property (point-min) (point-max)
9968                                             'gnus-wheel-edge
9969                                             (* (1+ edge) direction))
9970                          nil))
9971                    (eq last-command 'gnus-wheel-summary-scroll))))
9972       (gnus-summary-next-article nil nil (minusp direction)))))
9973
9974 (defun gnus-wheel-install ()
9975   "Enable mouse wheel support on summary window."
9976   (when gnus-use-wheel
9977     (let ((keys
9978            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9979       (dolist (key keys)
9980         (define-key gnus-summary-mode-map key
9981           'gnus-wheel-summary-scroll)))))
9982
9983 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9984
9985 ;;;
9986 ;;; Traditional PGP commmands
9987 ;;;
9988
9989 (defun gnus-summary-decrypt-article (&optional force)
9990   "Decrypt the current article in traditional PGP way.
9991 This will have permanent effect only in mail groups.
9992 If FORCE is non-nil, allow editing of articles even in read-only
9993 groups."
9994   (interactive "P")
9995   (gnus-summary-select-article t)
9996   (gnus-eval-in-buffer-window gnus-article-buffer
9997     (save-excursion
9998       (save-restriction
9999         (widen)
10000         (goto-char (point-min))
10001         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10002           (error "Not a traditional PGP message!"))
10003         (let ((armor-start (match-beginning 0)))
10004           (if (and (pgg-decrypt-region armor-start (point-max))
10005                    (or force (not (gnus-group-read-only-p))))
10006               (let ((inhibit-read-only t)
10007                     buffer-read-only)
10008                 (delete-region armor-start
10009                                (progn
10010                                  (re-search-forward "^-+END PGP" nil t)
10011                                  (beginning-of-line 2)
10012                                  (point)))
10013                 (insert-buffer-substring pgg-output-buffer))))))))
10014
10015 (defun gnus-summary-verify-article ()
10016   "Verify the current article in traditional PGP way."
10017   (interactive)
10018   (save-excursion
10019     (set-buffer gnus-original-article-buffer)
10020     (goto-char (point-min))
10021     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10022       (error "Not a traditional PGP message!"))
10023     (re-search-forward "^-+END PGP" nil t)
10024     (beginning-of-line 2)
10025     (call-interactively (function pgg-verify-region))))
10026
10027 ;;;
10028 ;;; Generic summary marking commands
10029 ;;;
10030
10031 (defvar gnus-summary-marking-alist
10032   '((read gnus-del-mark "d")
10033     (unread gnus-unread-mark "u")
10034     (ticked gnus-ticked-mark "!")
10035     (dormant gnus-dormant-mark "?")
10036     (expirable gnus-expirable-mark "e"))
10037   "An alist of names/marks/keystrokes.")
10038
10039 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10040 (defvar gnus-summary-mark-map)
10041
10042 (defun gnus-summary-make-all-marking-commands ()
10043   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10044   (dolist (elem gnus-summary-marking-alist)
10045     (apply 'gnus-summary-make-marking-command elem)))
10046
10047 (defun gnus-summary-make-marking-command (name mark keystroke)
10048   (let ((map (make-sparse-keymap)))
10049     (define-key gnus-summary-generic-mark-map keystroke map)
10050     (dolist (lway `((next "next" next nil "n")
10051                     (next-unread "next unread" next t "N")
10052                     (prev "previous" prev nil "p")
10053                     (prev-unread "previous unread" prev t "P")
10054                     (nomove "" nil nil ,keystroke)))
10055       (let ((func (gnus-summary-make-marking-command-1
10056                    mark (car lway) lway name)))
10057         (setq func (eval func))
10058         (define-key map (nth 4 lway) func)))))
10059
10060 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10061   `(defun ,(intern
10062             (format "gnus-summary-put-mark-as-%s%s"
10063                     name (if (eq way 'nomove)
10064                              ""
10065                            (concat "-" (symbol-name way)))))
10066      (n)
10067      ,(format
10068        "Mark the current article as %s%s.
10069 If N, the prefix, then repeat N times.
10070 If N is negative, move in reverse order.
10071 The difference between N and the actual number of articles marked is
10072 returned."
10073        name (car (cdr lway)))
10074      (interactive "p")
10075      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10076
10077 (defun gnus-summary-generic-mark (n mark move unread)
10078   "Mark N articles with MARK."
10079   (unless (eq major-mode 'gnus-summary-mode)
10080     (error "This command can only be used in the summary buffer"))
10081   (gnus-summary-show-thread)
10082   (let ((nummove
10083          (cond
10084           ((eq move 'next) 1)
10085           ((eq move 'prev) -1)
10086           (t 0))))
10087     (if (zerop nummove)
10088         (setq n 1)
10089       (when (< n 0)
10090         (setq n (abs n)
10091               nummove (* -1 nummove))))
10092     (while (and (> n 0)
10093                 (gnus-summary-mark-article nil mark)
10094                 (zerop (gnus-summary-next-subject nummove unread t)))
10095       (setq n (1- n)))
10096     (when (/= 0 n)
10097       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10098     (gnus-summary-recenter)
10099     (gnus-summary-position-point)
10100     (gnus-set-mode-line 'summary)
10101     n))
10102
10103 (gnus-summary-make-all-marking-commands)
10104
10105 (gnus-ems-redefine)
10106
10107 (provide 'gnus-sum)
10108
10109 (run-hooks 'gnus-sum-load-hook)
10110
10111 ;;; gnus-sum.el ends here