* lisp/Makefile.in (install): Use the lisp function `dgnushack-exported-files'.
[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-article-charset nil)
953 (defvar gnus-article-ignored-charsets nil)
954 (defvar gnus-scores-exclude-files nil)
955 (defvar gnus-page-broken nil)
956 (defvar gnus-inhibit-mime-unbuttonizing nil)
957
958 (defvar gnus-original-article nil)
959 (defvar gnus-article-internal-prepare-hook nil)
960 (defvar gnus-newsgroup-process-stack nil)
961
962 (defvar gnus-thread-indent-array nil)
963 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
964 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
965   "Function called to sort the articles within a thread after it has been gathered together.")
966
967 (defvar gnus-summary-save-parts-type-history nil)
968 (defvar gnus-summary-save-parts-last-directory nil)
969
970 ;; Avoid highlighting in kill files.
971 (defvar gnus-summary-inhibit-highlight nil)
972 (defvar gnus-newsgroup-selected-overlay nil)
973 (defvar gnus-inhibit-limiting nil)
974 (defvar gnus-newsgroup-adaptive-score-file nil)
975 (defvar gnus-current-score-file nil)
976 (defvar gnus-current-move-group nil)
977 (defvar gnus-current-copy-group nil)
978 (defvar gnus-current-crosspost-group nil)
979
980 (defvar gnus-newsgroup-dependencies nil)
981 (defvar gnus-newsgroup-adaptive nil)
982 (defvar gnus-summary-display-article-function nil)
983 (defvar gnus-summary-highlight-line-function nil
984   "Function called after highlighting a summary line.")
985
986 (defvar gnus-summary-line-format-alist
987   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
988     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
989     (?s gnus-tmp-subject-or-nil ?s)
990     (?n gnus-tmp-name ?s)
991     (?A (std11-address-string
992          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
993     (?a (or (std11-full-name-string
994              (car (mime-entity-read-field gnus-tmp-header 'From)))
995             gnus-tmp-from) ?s)
996     (?F gnus-tmp-from ?s)
997     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
998     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
999     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1000     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1001     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1002     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1003     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1004     (?L gnus-tmp-lines ?d)
1005     (?I gnus-tmp-indentation ?s)
1006     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1007     (?R gnus-tmp-replied ?c)
1008     (?\[ gnus-tmp-opening-bracket ?c)
1009     (?\] gnus-tmp-closing-bracket ?c)
1010     (?\> (make-string gnus-tmp-level ? ) ?s)
1011     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1012     (?i gnus-tmp-score ?d)
1013     (?z gnus-tmp-score-char ?c)
1014     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1015     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1016     (?U gnus-tmp-unread ?c)
1017     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1018     (?t (gnus-summary-number-of-articles-in-thread
1019          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1020         ?d)
1021     (?e (gnus-summary-number-of-articles-in-thread
1022          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1023         ?c)
1024     (?u gnus-tmp-user-defined ?s)
1025     (?P (gnus-pick-line-number) ?d))
1026   "An alist of format specifications that can appear in summary lines.
1027 These are paired with what variables they correspond with, along with
1028 the type of the variable (string, integer, character, etc).")
1029
1030 (defvar gnus-summary-dummy-line-format-alist
1031   `((?S gnus-tmp-subject ?s)
1032     (?N gnus-tmp-number ?d)
1033     (?u gnus-tmp-user-defined ?s)))
1034
1035 (defvar gnus-summary-mode-line-format-alist
1036   `((?G gnus-tmp-group-name ?s)
1037     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1038     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1039     (?A gnus-tmp-article-number ?d)
1040     (?Z gnus-tmp-unread-and-unselected ?s)
1041     (?V gnus-version ?s)
1042     (?U gnus-tmp-unread-and-unticked ?d)
1043     (?S gnus-tmp-subject ?s)
1044     (?e gnus-tmp-unselected ?d)
1045     (?u gnus-tmp-user-defined ?s)
1046     (?d (length gnus-newsgroup-dormant) ?d)
1047     (?t (length gnus-newsgroup-marked) ?d)
1048     (?r (length gnus-newsgroup-reads) ?d)
1049     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1050     (?E gnus-newsgroup-expunged-tally ?d)
1051     (?s (gnus-current-score-file-nondirectory) ?s)))
1052
1053 (defvar gnus-last-search-regexp nil
1054   "Default regexp for article search command.")
1055
1056 (defvar gnus-summary-search-article-matched-data nil
1057   "Last matched data of article search command.  It is the local variable
1058 in `gnus-article-buffer' which consists of the list of start position,
1059 end position and text.")
1060
1061 (defvar gnus-last-shell-command nil
1062   "Default shell command on article.")
1063
1064 (defvar gnus-newsgroup-begin nil)
1065 (defvar gnus-newsgroup-end nil)
1066 (defvar gnus-newsgroup-last-rmail nil)
1067 (defvar gnus-newsgroup-last-mail nil)
1068 (defvar gnus-newsgroup-last-folder nil)
1069 (defvar gnus-newsgroup-last-file nil)
1070 (defvar gnus-newsgroup-auto-expire nil)
1071 (defvar gnus-newsgroup-active nil)
1072
1073 (defvar gnus-newsgroup-data nil)
1074 (defvar gnus-newsgroup-data-reverse nil)
1075 (defvar gnus-newsgroup-limit nil)
1076 (defvar gnus-newsgroup-limits nil)
1077
1078 (defvar gnus-newsgroup-unreads nil
1079   "List of unread articles in the current newsgroup.")
1080
1081 (defvar gnus-newsgroup-unselected nil
1082   "List of unselected unread articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-reads nil
1085   "Alist of read articles and article marks in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-expunged-tally nil)
1088
1089 (defvar gnus-newsgroup-marked nil
1090   "List of ticked articles in the current newsgroup (a subset of unread art).")
1091
1092 (defvar gnus-newsgroup-killed nil
1093   "List of ranges of articles that have been through the scoring process.")
1094
1095 (defvar gnus-newsgroup-cached nil
1096   "List of articles that come from the article cache.")
1097
1098 (defvar gnus-newsgroup-saved nil
1099   "List of articles that have been saved.")
1100
1101 (defvar gnus-newsgroup-kill-headers nil)
1102
1103 (defvar gnus-newsgroup-replied nil
1104   "List of articles that have been replied to in the current newsgroup.")
1105
1106 (defvar gnus-newsgroup-expirable nil
1107   "List of articles in the current newsgroup that can be expired.")
1108
1109 (defvar gnus-newsgroup-processable nil
1110   "List of articles in the current newsgroup that can be processed.")
1111
1112 (defvar gnus-newsgroup-downloadable nil
1113   "List of articles in the current newsgroup that can be processed.")
1114
1115 (defvar gnus-newsgroup-undownloaded nil
1116   "List of articles in the current newsgroup that haven't been downloaded..")
1117
1118 (defvar gnus-newsgroup-unsendable nil
1119   "List of articles in the current newsgroup that won't be sent.")
1120
1121 (defvar gnus-newsgroup-bookmarks nil
1122   "List of articles in the current newsgroup that have bookmarks.")
1123
1124 (defvar gnus-newsgroup-dormant nil
1125   "List of dormant articles in the current newsgroup.")
1126
1127 (defvar gnus-newsgroup-scored nil
1128   "List of scored articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-incorporated nil
1131   "List of incorporated articles in the current newsgroup.")
1132
1133 (defvar gnus-newsgroup-headers nil
1134   "List of article headers in the current newsgroup.")
1135
1136 (defvar gnus-newsgroup-threads nil)
1137
1138 (defvar gnus-newsgroup-prepared nil
1139   "Whether the current group has been prepared properly.")
1140
1141 (defvar gnus-newsgroup-ancient nil
1142   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1143
1144 (defvar gnus-newsgroup-sparse nil)
1145
1146 (defvar gnus-current-article nil)
1147 (defvar gnus-article-current nil)
1148 (defvar gnus-current-headers nil)
1149 (defvar gnus-have-all-headers nil)
1150 (defvar gnus-last-article nil)
1151 (defvar gnus-newsgroup-history nil)
1152 (defvar gnus-newsgroup-charset nil)
1153 (defvar gnus-newsgroup-ephemeral-charset nil)
1154 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1155
1156 (defvar gnus-article-before-search nil)
1157
1158 (defconst gnus-summary-local-variables
1159   '(gnus-newsgroup-name
1160     gnus-newsgroup-begin gnus-newsgroup-end
1161     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1162     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1163     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1164     gnus-newsgroup-unselected gnus-newsgroup-marked
1165     gnus-newsgroup-reads gnus-newsgroup-saved
1166     gnus-newsgroup-replied gnus-newsgroup-expirable
1167     gnus-newsgroup-processable gnus-newsgroup-killed
1168     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1169     gnus-newsgroup-unsendable
1170     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1171     gnus-newsgroup-headers gnus-newsgroup-threads
1172     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1173     gnus-current-article gnus-current-headers gnus-have-all-headers
1174     gnus-last-article gnus-article-internal-prepare-hook
1175     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1176     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1177     gnus-thread-expunge-below
1178     gnus-score-alist gnus-current-score-file
1179     (gnus-summary-expunge-below . global)
1180     (gnus-summary-mark-below . global)
1181     (gnus-orphan-score . global)
1182     gnus-newsgroup-active gnus-scores-exclude-files
1183     gnus-newsgroup-history gnus-newsgroup-ancient
1184     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1185     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1186     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1187     (gnus-newsgroup-expunged-tally . 0)
1188     gnus-cache-removable-articles gnus-newsgroup-cached
1189     gnus-newsgroup-data gnus-newsgroup-data-reverse
1190     gnus-newsgroup-limit gnus-newsgroup-limits
1191     gnus-newsgroup-charset
1192     gnus-newsgroup-incorporated)
1193   "Variables that are buffer-local to the summary buffers.")
1194
1195 (defvar gnus-newsgroup-variables nil
1196   "Variables that have separate values in the newsgroups.")
1197
1198 ;; Byte-compiler warning.
1199 (defvar gnus-article-mode-map)
1200
1201 ;; Subject simplification.
1202
1203 (defun gnus-simplify-whitespace (str)
1204   "Remove excessive whitespace from STR."
1205   (let ((mystr str))
1206     ;; Multiple spaces.
1207     (while (string-match "[ \t][ \t]+" mystr)
1208       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1209                           " "
1210                           (substring mystr (match-end 0)))))
1211     ;; Leading spaces.
1212     (when (string-match "^[ \t]+" mystr)
1213       (setq mystr (substring mystr (match-end 0))))
1214     ;; Trailing spaces.
1215     (when (string-match "[ \t]+$" mystr)
1216       (setq mystr (substring mystr 0 (match-beginning 0))))
1217     mystr))
1218
1219 (defsubst gnus-simplify-subject-re (subject)
1220   "Remove \"Re:\" from subject lines."
1221   (if (string-match "^[Rr][Ee]: *" subject)
1222       (substring subject (match-end 0))
1223     subject))
1224
1225 (defun gnus-simplify-subject (subject &optional re-only)
1226   "Remove `Re:' and words in parentheses.
1227 If RE-ONLY is non-nil, strip leading `Re:'s only."
1228   (let ((case-fold-search t))           ;Ignore case.
1229     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1230     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1231       (setq subject (substring subject (match-end 0))))
1232     ;; Remove uninteresting prefixes.
1233     (when (and (not re-only)
1234                gnus-simplify-ignored-prefixes
1235                (string-match gnus-simplify-ignored-prefixes subject))
1236       (setq subject (substring subject (match-end 0))))
1237     ;; Remove words in parentheses from end.
1238     (unless re-only
1239       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1240         (setq subject (substring subject 0 (match-beginning 0)))))
1241     ;; Return subject string.
1242     subject))
1243
1244 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1245 ;; all whitespace.
1246 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1247   (goto-char (point-min))
1248   (while (re-search-forward regexp nil t)
1249     (replace-match (or newtext ""))))
1250
1251 (defun gnus-simplify-buffer-fuzzy ()
1252   "Simplify string in the buffer fuzzily.
1253 The string in the accessible portion of the current buffer is simplified.
1254 It is assumed to be a single-line subject.
1255 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1256 matter is removed.  Additional things can be deleted by setting
1257 `gnus-simplify-subject-fuzzy-regexp'."
1258   (let ((case-fold-search t)
1259         (modified-tick))
1260     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1261
1262     (while (not (eq modified-tick (buffer-modified-tick)))
1263       (setq modified-tick (buffer-modified-tick))
1264       (cond
1265        ((listp gnus-simplify-subject-fuzzy-regexp)
1266         (mapcar 'gnus-simplify-buffer-fuzzy-step
1267                 gnus-simplify-subject-fuzzy-regexp))
1268        (gnus-simplify-subject-fuzzy-regexp
1269         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1270       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1271       (gnus-simplify-buffer-fuzzy-step
1272        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1273       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1274
1275     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1276     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1277     (gnus-simplify-buffer-fuzzy-step " $")
1278     (gnus-simplify-buffer-fuzzy-step "^ +")))
1279
1280 (defun gnus-simplify-subject-fuzzy (subject)
1281   "Simplify a subject string fuzzily.
1282 See `gnus-simplify-buffer-fuzzy' for details."
1283   (save-excursion
1284     (gnus-set-work-buffer)
1285     (let ((case-fold-search t))
1286       ;; Remove uninteresting prefixes.
1287       (when (and gnus-simplify-ignored-prefixes
1288                  (string-match gnus-simplify-ignored-prefixes subject))
1289         (setq subject (substring subject (match-end 0))))
1290       (insert subject)
1291       (inline (gnus-simplify-buffer-fuzzy))
1292       (buffer-string))))
1293
1294 (defsubst gnus-simplify-subject-fully (subject)
1295   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1296   (cond
1297    (gnus-simplify-subject-functions
1298     (gnus-map-function gnus-simplify-subject-functions subject))
1299    ((null gnus-summary-gather-subject-limit)
1300     (gnus-simplify-subject-re subject))
1301    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1302     (gnus-simplify-subject-fuzzy subject))
1303    ((numberp gnus-summary-gather-subject-limit)
1304     (gnus-limit-string (gnus-simplify-subject-re subject)
1305                        gnus-summary-gather-subject-limit))
1306    (t
1307     subject)))
1308
1309 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1310   "Check whether two subjects are equal.
1311 If optional argument simple-first is t, first argument is already
1312 simplified."
1313   (cond
1314    ((null simple-first)
1315     (equal (gnus-simplify-subject-fully s1)
1316            (gnus-simplify-subject-fully s2)))
1317    (t
1318     (equal s1
1319            (gnus-simplify-subject-fully s2)))))
1320
1321 (defun gnus-summary-bubble-group ()
1322   "Increase the score of the current group.
1323 This is a handy function to add to `gnus-summary-exit-hook' to
1324 increase the score of each group you read."
1325   (gnus-group-add-score gnus-newsgroup-name))
1326
1327 \f
1328 ;;;
1329 ;;; Gnus summary mode
1330 ;;;
1331
1332 (put 'gnus-summary-mode 'mode-class 'special)
1333
1334 (when t
1335   ;; Non-orthogonal keys
1336
1337   (gnus-define-keys gnus-summary-mode-map
1338     " " gnus-summary-next-page
1339     "\177" gnus-summary-prev-page
1340     [delete] gnus-summary-prev-page
1341     [backspace] gnus-summary-prev-page
1342     "\r" gnus-summary-scroll-up
1343     "\M-\r" gnus-summary-scroll-down
1344     "n" gnus-summary-next-unread-article
1345     "p" gnus-summary-prev-unread-article
1346     "N" gnus-summary-next-article
1347     "P" gnus-summary-prev-article
1348     "\M-\C-n" gnus-summary-next-same-subject
1349     "\M-\C-p" gnus-summary-prev-same-subject
1350     "\M-n" gnus-summary-next-unread-subject
1351     "\M-p" gnus-summary-prev-unread-subject
1352     "." gnus-summary-first-unread-article
1353     "," gnus-summary-best-unread-article
1354     "\M-s" gnus-summary-search-article-forward
1355     "\M-r" gnus-summary-search-article-backward
1356     "<" gnus-summary-beginning-of-article
1357     ">" gnus-summary-end-of-article
1358     "j" gnus-summary-goto-article
1359     "^" gnus-summary-refer-parent-article
1360     "\M-^" gnus-summary-refer-article
1361     "u" gnus-summary-tick-article-forward
1362     "!" gnus-summary-tick-article-forward
1363     "U" gnus-summary-tick-article-backward
1364     "d" gnus-summary-mark-as-read-forward
1365     "D" gnus-summary-mark-as-read-backward
1366     "E" gnus-summary-mark-as-expirable
1367     "\M-u" gnus-summary-clear-mark-forward
1368     "\M-U" gnus-summary-clear-mark-backward
1369     "k" gnus-summary-kill-same-subject-and-select
1370     "\C-k" gnus-summary-kill-same-subject
1371     "\M-\C-k" gnus-summary-kill-thread
1372     "\M-\C-l" gnus-summary-lower-thread
1373     "e" gnus-summary-edit-article
1374     "#" gnus-summary-mark-as-processable
1375     "\M-#" gnus-summary-unmark-as-processable
1376     "\M-\C-t" gnus-summary-toggle-threads
1377     "\M-\C-s" gnus-summary-show-thread
1378     "\M-\C-h" gnus-summary-hide-thread
1379     "\M-\C-f" gnus-summary-next-thread
1380     "\M-\C-b" gnus-summary-prev-thread
1381     [(meta down)] gnus-summary-next-thread
1382     [(meta up)] gnus-summary-prev-thread
1383     "\M-\C-u" gnus-summary-up-thread
1384     "\M-\C-d" gnus-summary-down-thread
1385     "&" gnus-summary-execute-command
1386     "c" gnus-summary-catchup-and-exit
1387     "\C-w" gnus-summary-mark-region-as-read
1388     "\C-t" gnus-summary-toggle-truncation
1389     "?" gnus-summary-mark-as-dormant
1390     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1391     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1392     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1393     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1394     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1395     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1396     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1397     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1398     "=" gnus-summary-expand-window
1399     "\C-x\C-s" gnus-summary-reselect-current-group
1400     "\M-g" gnus-summary-rescan-group
1401     "w" gnus-summary-stop-page-breaking
1402     "\C-c\C-r" gnus-summary-caesar-message
1403     "\M-t" gnus-summary-toggle-mime
1404     "f" gnus-summary-followup
1405     "F" gnus-summary-followup-with-original
1406     "C" gnus-summary-cancel-article
1407     "r" gnus-summary-reply
1408     "R" gnus-summary-reply-with-original
1409     "\C-c\C-f" gnus-summary-mail-forward
1410     "o" gnus-summary-save-article
1411     "\C-o" gnus-summary-save-article-mail
1412     "|" gnus-summary-pipe-output
1413     "\M-k" gnus-summary-edit-local-kill
1414     "\M-K" gnus-summary-edit-global-kill
1415     ;; "V" gnus-version
1416     "\C-c\C-d" gnus-summary-describe-group
1417     "q" gnus-summary-exit
1418     "Q" gnus-summary-exit-no-update
1419     "\C-c\C-i" gnus-info-find-node
1420     gnus-mouse-2 gnus-mouse-pick-article
1421     "m" gnus-summary-mail-other-window
1422     "a" gnus-summary-post-news
1423     "x" gnus-summary-limit-to-unread
1424     "s" gnus-summary-isearch-article
1425     "t" gnus-article-toggle-headers
1426     "g" gnus-summary-show-article
1427     "l" gnus-summary-goto-last-article
1428     "v" gnus-summary-preview-mime-message
1429     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1430     "\C-d" gnus-summary-enter-digest-group
1431     "\M-\C-d" gnus-summary-read-document
1432     "\M-\C-e" gnus-summary-edit-parameters
1433     "\M-\C-a" gnus-summary-customize-parameters
1434     "\C-c\C-b" gnus-bug
1435     "*" gnus-cache-enter-article
1436     "\M-*" gnus-cache-remove-article
1437     "\M-&" gnus-summary-universal-argument
1438     "\C-l" gnus-recenter
1439     "I" gnus-summary-increase-score
1440     "L" gnus-summary-lower-score
1441     "\M-i" gnus-symbolic-argument
1442     "h" gnus-summary-select-article-buffer
1443
1444     "V" gnus-summary-score-map
1445     "X" gnus-uu-extract-map
1446     "S" gnus-summary-send-map)
1447
1448   ;; Sort of orthogonal keymap
1449   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1450     "t" gnus-summary-tick-article-forward
1451     "!" gnus-summary-tick-article-forward
1452     "d" gnus-summary-mark-as-read-forward
1453     "r" gnus-summary-mark-as-read-forward
1454     "c" gnus-summary-clear-mark-forward
1455     " " gnus-summary-clear-mark-forward
1456     "e" gnus-summary-mark-as-expirable
1457     "x" gnus-summary-mark-as-expirable
1458     "?" gnus-summary-mark-as-dormant
1459     "b" gnus-summary-set-bookmark
1460     "B" gnus-summary-remove-bookmark
1461     "#" gnus-summary-mark-as-processable
1462     "\M-#" gnus-summary-unmark-as-processable
1463     "S" gnus-summary-limit-include-expunged
1464     "C" gnus-summary-catchup
1465     "H" gnus-summary-catchup-to-here
1466     "\C-c" gnus-summary-catchup-all
1467     "k" gnus-summary-kill-same-subject-and-select
1468     "K" gnus-summary-kill-same-subject
1469     "P" gnus-uu-mark-map)
1470
1471   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1472     "c" gnus-summary-clear-above
1473     "u" gnus-summary-tick-above
1474     "m" gnus-summary-mark-above
1475     "k" gnus-summary-kill-below)
1476
1477   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1478     "/" gnus-summary-limit-to-subject
1479     "n" gnus-summary-limit-to-articles
1480     "w" gnus-summary-pop-limit
1481     "s" gnus-summary-limit-to-subject
1482     "a" gnus-summary-limit-to-author
1483     "u" gnus-summary-limit-to-unread
1484     "m" gnus-summary-limit-to-marks
1485     "M" gnus-summary-limit-exclude-marks
1486     "v" gnus-summary-limit-to-score
1487     "*" gnus-summary-limit-include-cached
1488     "D" gnus-summary-limit-include-dormant
1489     "T" gnus-summary-limit-include-thread
1490     "d" gnus-summary-limit-exclude-dormant
1491     "t" gnus-summary-limit-to-age
1492     "x" gnus-summary-limit-to-extra
1493     "E" gnus-summary-limit-include-expunged
1494     "c" gnus-summary-limit-exclude-childless-dormant
1495     "C" gnus-summary-limit-mark-excluded-as-read)
1496
1497   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1498     "n" gnus-summary-next-unread-article
1499     "p" gnus-summary-prev-unread-article
1500     "N" gnus-summary-next-article
1501     "P" gnus-summary-prev-article
1502     "\C-n" gnus-summary-next-same-subject
1503     "\C-p" gnus-summary-prev-same-subject
1504     "\M-n" gnus-summary-next-unread-subject
1505     "\M-p" gnus-summary-prev-unread-subject
1506     "f" gnus-summary-first-unread-article
1507     "b" gnus-summary-best-unread-article
1508     "j" gnus-summary-goto-article
1509     "g" gnus-summary-goto-subject
1510     "l" gnus-summary-goto-last-article
1511     "o" gnus-summary-pop-article)
1512
1513   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1514     "k" gnus-summary-kill-thread
1515     "l" gnus-summary-lower-thread
1516     "i" gnus-summary-raise-thread
1517     "T" gnus-summary-toggle-threads
1518     "t" gnus-summary-rethread-current
1519     "^" gnus-summary-reparent-thread
1520     "s" gnus-summary-show-thread
1521     "S" gnus-summary-show-all-threads
1522     "h" gnus-summary-hide-thread
1523     "H" gnus-summary-hide-all-threads
1524     "n" gnus-summary-next-thread
1525     "p" gnus-summary-prev-thread
1526     "u" gnus-summary-up-thread
1527     "o" gnus-summary-top-thread
1528     "d" gnus-summary-down-thread
1529     "#" gnus-uu-mark-thread
1530     "\M-#" gnus-uu-unmark-thread)
1531
1532   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1533     "g" gnus-summary-prepare
1534     "c" gnus-summary-insert-cached-articles)
1535
1536   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1537     "c" gnus-summary-catchup-and-exit
1538     "C" gnus-summary-catchup-all-and-exit
1539     "E" gnus-summary-exit-no-update
1540     "J" gnus-summary-jump-to-other-group
1541     "Q" gnus-summary-exit
1542     "Z" gnus-summary-exit
1543     "n" gnus-summary-catchup-and-goto-next-group
1544     "R" gnus-summary-reselect-current-group
1545     "G" gnus-summary-rescan-group
1546     "N" gnus-summary-next-group
1547     "s" gnus-summary-save-newsrc
1548     "P" gnus-summary-prev-group)
1549
1550   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1551     " " gnus-summary-next-page
1552     "n" gnus-summary-next-page
1553     "\177" gnus-summary-prev-page
1554     [delete] gnus-summary-prev-page
1555     "p" gnus-summary-prev-page
1556     "\r" gnus-summary-scroll-up
1557     "\M-\r" gnus-summary-scroll-down
1558     "<" gnus-summary-beginning-of-article
1559     ">" gnus-summary-end-of-article
1560     "b" gnus-summary-beginning-of-article
1561     "e" gnus-summary-end-of-article
1562     "^" gnus-summary-refer-parent-article
1563     "r" gnus-summary-refer-parent-article
1564     "D" gnus-summary-enter-digest-group
1565     "R" gnus-summary-refer-references
1566     "T" gnus-summary-refer-thread
1567     "g" gnus-summary-show-article
1568     "s" gnus-summary-isearch-article
1569     "P" gnus-summary-print-article
1570     "t" gnus-article-babel
1571     "d" gnus-summary-decrypt-article
1572     "v" gnus-summary-verify-article)
1573
1574   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1575     "b" gnus-article-add-buttons
1576     "B" gnus-article-add-buttons-to-head
1577     "o" gnus-article-treat-overstrike
1578     "e" gnus-article-emphasize
1579     "w" gnus-article-fill-cited-article
1580     "Q" gnus-article-fill-long-lines
1581     "C" gnus-article-capitalize-sentences
1582     "c" gnus-article-remove-cr
1583     "Z" gnus-article-decode-HZ
1584     "f" gnus-article-display-x-face
1585     "l" gnus-summary-stop-page-breaking
1586     "r" gnus-summary-caesar-message
1587     "t" gnus-article-toggle-headers
1588     "v" gnus-summary-verbose-headers
1589     "m" gnus-summary-toggle-mime
1590     "H" gnus-article-strip-headers-in-body
1591     "d" gnus-article-treat-dumbquotes
1592     "s" gnus-smiley-display)
1593
1594   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1595     "a" gnus-article-hide
1596     "h" gnus-article-toggle-headers
1597     "b" gnus-article-hide-boring-headers
1598     "s" gnus-article-hide-signature
1599     "c" gnus-article-hide-citation
1600     "C" gnus-article-hide-citation-in-followups
1601     "l" gnus-article-hide-list-identifiers
1602     "p" gnus-article-hide-pgp
1603     "B" gnus-article-strip-banner
1604     "P" gnus-article-hide-pem
1605     "\C-c" gnus-article-hide-citation-maybe)
1606
1607   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1608     "a" gnus-article-highlight
1609     "h" gnus-article-highlight-headers
1610     "c" gnus-article-highlight-citation
1611     "s" gnus-article-highlight-signature)
1612
1613   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1614     "z" gnus-article-date-ut
1615     "u" gnus-article-date-ut
1616     "l" gnus-article-date-local
1617     "e" gnus-article-date-lapsed
1618     "o" gnus-article-date-original
1619     "i" gnus-article-date-iso8601
1620     "s" gnus-article-date-user)
1621
1622   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1623     "t" gnus-article-remove-trailing-blank-lines
1624     "l" gnus-article-strip-leading-blank-lines
1625     "m" gnus-article-strip-multiple-blank-lines
1626     "a" gnus-article-strip-blank-lines
1627     "A" gnus-article-strip-all-blank-lines
1628     "s" gnus-article-strip-leading-space
1629     "e" gnus-article-strip-trailing-space)
1630
1631   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1632     "v" gnus-version
1633     "f" gnus-summary-fetch-faq
1634     "d" gnus-summary-describe-group
1635     "h" gnus-summary-describe-briefly
1636     "i" gnus-info-find-node)
1637
1638   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1639     "e" gnus-summary-expire-articles
1640     "\M-\C-e" gnus-summary-expire-articles-now
1641     "\177" gnus-summary-delete-article
1642     [delete] gnus-summary-delete-article
1643     [backspace] gnus-summary-delete-article
1644     "m" gnus-summary-move-article
1645     "r" gnus-summary-respool-article
1646     "w" gnus-summary-edit-article
1647     "c" gnus-summary-copy-article
1648     "B" gnus-summary-crosspost-article
1649     "q" gnus-summary-respool-query
1650     "t" gnus-summary-respool-trace
1651     "i" gnus-summary-import-article
1652     "p" gnus-summary-article-posted-p)
1653
1654   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1655     "o" gnus-summary-save-article
1656     "m" gnus-summary-save-article-mail
1657     "F" gnus-summary-write-article-file
1658     "r" gnus-summary-save-article-rmail
1659     "f" gnus-summary-save-article-file
1660     "b" gnus-summary-save-article-body-file
1661     "h" gnus-summary-save-article-folder
1662     "v" gnus-summary-save-article-vm
1663     "p" gnus-summary-pipe-output
1664     "s" gnus-soup-add-article)
1665
1666   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1667     "b" gnus-summary-display-buttonized
1668     "m" gnus-summary-repair-multipart
1669     "v" gnus-article-view-part
1670     "o" gnus-article-save-part
1671     "c" gnus-article-copy-part
1672     "e" gnus-article-externalize-part
1673     "i" gnus-article-inline-part
1674     "|" gnus-article-pipe-part))
1675
1676 (defun gnus-summary-make-menu-bar ()
1677   (gnus-turn-off-edit-menu 'summary)
1678
1679   (unless (boundp 'gnus-summary-misc-menu)
1680
1681     (easy-menu-define
1682      gnus-summary-kill-menu gnus-summary-mode-map ""
1683      (cons
1684       "Score"
1685       (nconc
1686        (list
1687         ["Enter score..." gnus-summary-score-entry t]
1688         ["Customize" gnus-score-customize t])
1689        (gnus-make-score-map 'increase)
1690        (gnus-make-score-map 'lower)
1691        '(("Mark"
1692           ["Kill below" gnus-summary-kill-below t]
1693           ["Mark above" gnus-summary-mark-above t]
1694           ["Tick above" gnus-summary-tick-above t]
1695           ["Clear above" gnus-summary-clear-above t])
1696          ["Current score" gnus-summary-current-score t]
1697          ["Set score" gnus-summary-set-score t]
1698          ["Switch current score file..." gnus-score-change-score-file t]
1699          ["Set mark below..." gnus-score-set-mark-below t]
1700          ["Set expunge below..." gnus-score-set-expunge-below t]
1701          ["Edit current score file" gnus-score-edit-current-scores t]
1702          ["Edit score file" gnus-score-edit-file t]
1703          ["Trace score" gnus-score-find-trace t]
1704          ["Find words" gnus-score-find-favourite-words t]
1705          ["Rescore buffer" gnus-summary-rescore t]
1706          ["Increase score..." gnus-summary-increase-score t]
1707          ["Lower score..." gnus-summary-lower-score t]))))
1708
1709     ;; Define both the Article menu in the summary buffer and the equivalent
1710     ;; Commands menu in the article buffer here for consistency.
1711     (let ((innards
1712            '(("Hide"
1713               ["All" gnus-article-hide t]
1714               ["Headers" gnus-article-toggle-headers t]
1715               ["Signature" gnus-article-hide-signature t]
1716               ["Citation" gnus-article-hide-citation t]
1717               ["List identifiers" gnus-article-hide-list-identifiers t]
1718               ["PGP" gnus-article-hide-pgp t]
1719               ["Banner" gnus-article-strip-banner t]
1720               ["Boring headers" gnus-article-hide-boring-headers t])
1721              ("Highlight"
1722               ["All" gnus-article-highlight t]
1723               ["Headers" gnus-article-highlight-headers t]
1724               ["Signature" gnus-article-highlight-signature t]
1725               ["Citation" gnus-article-highlight-citation t])
1726              ("Date"
1727               ["Local" gnus-article-date-local t]
1728               ["ISO8601" gnus-article-date-iso8601 t]
1729               ["UT" gnus-article-date-ut t]
1730               ["Original" gnus-article-date-original t]
1731               ["Lapsed" gnus-article-date-lapsed t]
1732               ["User-defined" gnus-article-date-user t])
1733              ("Washing"
1734               ("Remove Blanks"
1735                ["Leading" gnus-article-strip-leading-blank-lines t]
1736                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1737                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1738                ["All of the above" gnus-article-strip-blank-lines t]
1739                ["All" gnus-article-strip-all-blank-lines t]
1740                ["Leading space" gnus-article-strip-leading-space t]
1741                ["Trailing space" gnus-article-strip-trailing-space t])
1742               ["Overstrike" gnus-article-treat-overstrike t]
1743               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1744               ["Emphasis" gnus-article-emphasize t]
1745               ["Word wrap" gnus-article-fill-cited-article t]
1746               ["Fill long lines" gnus-article-fill-long-lines t]
1747               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1748               ["CR" gnus-article-remove-cr t]
1749               ["Show X-Face" gnus-article-display-x-face t]
1750               ["Rot 13" gnus-summary-caesar-message t]
1751               ["Unix pipe" gnus-summary-pipe-message t]
1752               ["Add buttons" gnus-article-add-buttons t]
1753               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1754               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1755               ["Toggle MIME" gnus-summary-toggle-mime t]
1756               ["Verbose header" gnus-summary-verbose-headers t]
1757               ["Toggle header" gnus-summary-toggle-header t]
1758               ["Toggle smileys" gnus-smiley-display t]
1759               ["HZ" gnus-article-decode-HZ t])
1760              ("Output"
1761               ["Save in default format" gnus-summary-save-article t]
1762               ["Save in file" gnus-summary-save-article-file t]
1763               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1764               ["Save in MH folder" gnus-summary-save-article-folder t]
1765               ["Save in VM folder" gnus-summary-save-article-vm t]
1766               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1767               ["Save body in file" gnus-summary-save-article-body-file t]
1768               ["Pipe through a filter" gnus-summary-pipe-output t]
1769               ["Add to SOUP packet" gnus-soup-add-article t]
1770               ["Print" gnus-summary-print-article t])
1771              ("Backend"
1772               ["Respool article..." gnus-summary-respool-article t]
1773               ["Move article..." gnus-summary-move-article
1774                (gnus-check-backend-function
1775                 'request-move-article gnus-newsgroup-name)]
1776               ["Copy article..." gnus-summary-copy-article t]
1777               ["Crosspost article..." gnus-summary-crosspost-article
1778                (gnus-check-backend-function
1779                 'request-replace-article gnus-newsgroup-name)]
1780               ["Import file..." gnus-summary-import-article t]
1781               ["Check if posted" gnus-summary-article-posted-p t]
1782               ["Edit article" gnus-summary-edit-article
1783                (not (gnus-group-read-only-p))]
1784               ["Delete article" gnus-summary-delete-article
1785                (gnus-check-backend-function
1786                 'request-expire-articles gnus-newsgroup-name)]
1787               ["Query respool" gnus-summary-respool-query t]
1788               ["Trace respool" gnus-summary-respool-trace t]
1789               ["Delete expirable articles" gnus-summary-expire-articles-now
1790                (gnus-check-backend-function
1791                 'request-expire-articles gnus-newsgroup-name)])
1792              ("Extract"
1793               ["Uudecode" gnus-uu-decode-uu t]
1794               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1795               ["Unshar" gnus-uu-decode-unshar t]
1796               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1797               ["Save" gnus-uu-decode-save t]
1798               ["Binhex" gnus-uu-decode-binhex t]
1799               ["Postscript" gnus-uu-decode-postscript t])
1800              ("Cache"
1801               ["Enter article" gnus-cache-enter-article t]
1802               ["Remove article" gnus-cache-remove-article t])
1803              ["Translate" gnus-article-babel t]
1804              ["Select article buffer" gnus-summary-select-article-buffer t]
1805              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1806              ["Isearch article..." gnus-summary-isearch-article t]
1807              ["Beginning of the article" gnus-summary-beginning-of-article t]
1808              ["End of the article" gnus-summary-end-of-article t]
1809              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1810              ["Fetch referenced articles" gnus-summary-refer-references t]
1811              ["Fetch current thread" gnus-summary-refer-thread t]
1812              ["Fetch article with id..." gnus-summary-refer-article t]
1813              ["Redisplay" gnus-summary-show-article t])))
1814       (easy-menu-define
1815        gnus-summary-article-menu gnus-summary-mode-map ""
1816        (cons "Article" innards))
1817
1818       (easy-menu-define
1819        gnus-article-commands-menu gnus-article-mode-map ""
1820        (cons "Commands" innards)))
1821
1822     (easy-menu-define
1823      gnus-summary-thread-menu gnus-summary-mode-map ""
1824      '("Threads"
1825        ["Toggle threading" gnus-summary-toggle-threads t]
1826        ["Hide threads" gnus-summary-hide-all-threads t]
1827        ["Show threads" gnus-summary-show-all-threads t]
1828        ["Hide thread" gnus-summary-hide-thread t]
1829        ["Show thread" gnus-summary-show-thread t]
1830        ["Go to next thread" gnus-summary-next-thread t]
1831        ["Go to previous thread" gnus-summary-prev-thread t]
1832        ["Go down thread" gnus-summary-down-thread t]
1833        ["Go up thread" gnus-summary-up-thread t]
1834        ["Top of thread" gnus-summary-top-thread t]
1835        ["Mark thread as read" gnus-summary-kill-thread t]
1836        ["Lower thread score" gnus-summary-lower-thread t]
1837        ["Raise thread score" gnus-summary-raise-thread t]
1838        ["Rethread current" gnus-summary-rethread-current t]))
1839
1840     (easy-menu-define
1841      gnus-summary-post-menu gnus-summary-mode-map ""
1842      '("Post"
1843        ["Post an article" gnus-summary-post-news t]
1844        ["Followup" gnus-summary-followup t]
1845        ["Followup and yank" gnus-summary-followup-with-original t]
1846        ["Supersede article" gnus-summary-supersede-article t]
1847        ["Cancel article" gnus-summary-cancel-article t]
1848        ["Reply" gnus-summary-reply t]
1849        ["Reply and yank" gnus-summary-reply-with-original t]
1850        ["Wide reply" gnus-summary-wide-reply t]
1851        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1852        ["Mail forward" gnus-summary-mail-forward t]
1853        ["Post forward" gnus-summary-post-forward t]
1854        ["Digest and mail" gnus-uu-digest-mail-forward t]
1855        ["Digest and post" gnus-uu-digest-post-forward t]
1856        ["Resend message" gnus-summary-resend-message t]
1857        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1858        ["Send a mail" gnus-summary-mail-other-window t]
1859        ["Uuencode and post" gnus-uu-post-news t]
1860        ["Followup via news" gnus-summary-followup-to-mail t]
1861        ["Followup via news and yank"
1862         gnus-summary-followup-to-mail-with-original t]
1863        ;;("Draft"
1864        ;;["Send" gnus-summary-send-draft t]
1865        ;;["Send bounced" gnus-resend-bounced-mail t])
1866        ))
1867
1868     (easy-menu-define
1869      gnus-summary-misc-menu gnus-summary-mode-map ""
1870      '("Misc"
1871        ("Mark Read"
1872         ["Mark as read" gnus-summary-mark-as-read-forward t]
1873         ["Mark same subject and select"
1874          gnus-summary-kill-same-subject-and-select t]
1875         ["Mark same subject" gnus-summary-kill-same-subject t]
1876         ["Catchup" gnus-summary-catchup t]
1877         ["Catchup all" gnus-summary-catchup-all t]
1878         ["Catchup to here" gnus-summary-catchup-to-here t]
1879         ["Catchup region" gnus-summary-mark-region-as-read t]
1880         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1881        ("Mark Various"
1882         ["Tick" gnus-summary-tick-article-forward t]
1883         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1884         ["Remove marks" gnus-summary-clear-mark-forward t]
1885         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1886         ["Set bookmark" gnus-summary-set-bookmark t]
1887         ["Remove bookmark" gnus-summary-remove-bookmark t])
1888        ("Mark Limit"
1889         ["Marks..." gnus-summary-limit-to-marks t]
1890         ["Subject..." gnus-summary-limit-to-subject t]
1891         ["Author..." gnus-summary-limit-to-author t]
1892         ["Age..." gnus-summary-limit-to-age t]
1893         ["Extra..." gnus-summary-limit-to-extra t]
1894         ["Score" gnus-summary-limit-to-score t]
1895         ["Unread" gnus-summary-limit-to-unread t]
1896         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1897         ["Articles" gnus-summary-limit-to-articles t]
1898         ["Pop limit" gnus-summary-pop-limit t]
1899         ["Show dormant" gnus-summary-limit-include-dormant t]
1900         ["Hide childless dormant"
1901          gnus-summary-limit-exclude-childless-dormant t]
1902         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1903         ["Hide marked" gnus-summary-limit-exclude-marks t]
1904         ["Show expunged" gnus-summary-show-all-expunged t])
1905        ("Process Mark"
1906         ["Set mark" gnus-summary-mark-as-processable t]
1907         ["Remove mark" gnus-summary-unmark-as-processable t]
1908         ["Remove all marks" gnus-summary-unmark-all-processable t]
1909         ["Mark above" gnus-uu-mark-over t]
1910         ["Mark series" gnus-uu-mark-series t]
1911         ["Mark region" gnus-uu-mark-region t]
1912         ["Unmark region" gnus-uu-unmark-region t]
1913         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1914         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1915         ["Mark all" gnus-uu-mark-all t]
1916         ["Mark buffer" gnus-uu-mark-buffer t]
1917         ["Mark sparse" gnus-uu-mark-sparse t]
1918         ["Mark thread" gnus-uu-mark-thread t]
1919         ["Unmark thread" gnus-uu-unmark-thread t]
1920         ("Process Mark Sets"
1921          ["Kill" gnus-summary-kill-process-mark t]
1922          ["Yank" gnus-summary-yank-process-mark
1923           gnus-newsgroup-process-stack]
1924          ["Save" gnus-summary-save-process-mark t]))
1925        ("Scroll article"
1926         ["Page forward" gnus-summary-next-page t]
1927         ["Page backward" gnus-summary-prev-page t]
1928         ["Line forward" gnus-summary-scroll-up t])
1929        ("Move"
1930         ["Next unread article" gnus-summary-next-unread-article t]
1931         ["Previous unread article" gnus-summary-prev-unread-article t]
1932         ["Next article" gnus-summary-next-article t]
1933         ["Previous article" gnus-summary-prev-article t]
1934         ["Next unread subject" gnus-summary-next-unread-subject t]
1935         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1936         ["Next article same subject" gnus-summary-next-same-subject t]
1937         ["Previous article same subject" gnus-summary-prev-same-subject t]
1938         ["First unread article" gnus-summary-first-unread-article t]
1939         ["Best unread article" gnus-summary-best-unread-article t]
1940         ["Go to subject number..." gnus-summary-goto-subject t]
1941         ["Go to article number..." gnus-summary-goto-article t]
1942         ["Go to the last article" gnus-summary-goto-last-article t]
1943         ["Pop article off history" gnus-summary-pop-article t])
1944        ("Sort"
1945         ["Sort by number" gnus-summary-sort-by-number t]
1946         ["Sort by author" gnus-summary-sort-by-author t]
1947         ["Sort by subject" gnus-summary-sort-by-subject t]
1948         ["Sort by date" gnus-summary-sort-by-date t]
1949         ["Sort by score" gnus-summary-sort-by-score t]
1950         ["Sort by lines" gnus-summary-sort-by-lines t]
1951         ["Sort by characters" gnus-summary-sort-by-chars t])
1952        ("Help"
1953         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1954         ["Describe group" gnus-summary-describe-group t]
1955         ["Read manual" gnus-info-find-node t])
1956        ("Modes"
1957         ["Pick and read" gnus-pick-mode t]
1958         ["Binary" gnus-binary-mode t])
1959        ("Regeneration"
1960         ["Regenerate" gnus-summary-prepare t]
1961         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1962         ["Toggle threading" gnus-summary-toggle-threads t])
1963        ["Filter articles..." gnus-summary-execute-command t]
1964        ["Run command on subjects..." gnus-summary-universal-argument t]
1965        ["Search articles forward..." gnus-summary-search-article-forward t]
1966        ["Search articles backward..." gnus-summary-search-article-backward t]
1967        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1968        ["Expand window" gnus-summary-expand-window t]
1969        ["Expire expirable articles" gnus-summary-expire-articles
1970         (gnus-check-backend-function
1971          'request-expire-articles gnus-newsgroup-name)]
1972        ["Edit local kill file" gnus-summary-edit-local-kill t]
1973        ["Edit main kill file" gnus-summary-edit-global-kill t]
1974        ["Edit group parameters" gnus-summary-edit-parameters t]
1975        ["Customize group parameters" gnus-summary-customize-parameters t]
1976        ["Send a bug report" gnus-bug t]
1977        ("Exit"
1978         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1979         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1980         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1981         ["Exit group" gnus-summary-exit t]
1982         ["Exit group without updating" gnus-summary-exit-no-update t]
1983         ["Exit and goto next group" gnus-summary-next-group t]
1984         ["Exit and goto prev group" gnus-summary-prev-group t]
1985         ["Reselect group" gnus-summary-reselect-current-group t]
1986         ["Rescan group" gnus-summary-rescan-group t]
1987         ["Update dribble" gnus-summary-save-newsrc t])))
1988
1989     (gnus-run-hooks 'gnus-summary-menu-hook)))
1990
1991 (defun gnus-score-set-default (var value)
1992   "A version of set that updates the GNU Emacs menu-bar."
1993   (set var value)
1994   ;; It is the message that forces the active status to be updated.
1995   (message ""))
1996
1997 (defun gnus-make-score-map (type)
1998   "Make a summary score map of type TYPE."
1999   (if t
2000       nil
2001     (let ((headers '(("author" "from" string)
2002                      ("subject" "subject" string)
2003                      ("article body" "body" string)
2004                      ("article head" "head" string)
2005                      ("xref" "xref" string)
2006                      ("extra header" "extra" string)
2007                      ("lines" "lines" number)
2008                      ("followups to author" "followup" string)))
2009           (types '((number ("less than" <)
2010                            ("greater than" >)
2011                            ("equal" =))
2012                    (string ("substring" s)
2013                            ("exact string" e)
2014                            ("fuzzy string" f)
2015                            ("regexp" r))))
2016           (perms '(("temporary" (current-time-string))
2017                    ("permanent" nil)
2018                    ("immediate" now)))
2019           header)
2020       (list
2021        (apply
2022         'nconc
2023         (list
2024          (if (eq type 'lower)
2025              "Lower score"
2026            "Increase score"))
2027         (let (outh)
2028           (while headers
2029             (setq header (car headers))
2030             (setq outh
2031                   (cons
2032                    (apply
2033                     'nconc
2034                     (list (car header))
2035                     (let ((ts (cdr (assoc (nth 2 header) types)))
2036                           outt)
2037                       (while ts
2038                         (setq outt
2039                               (cons
2040                                (apply
2041                                 'nconc
2042                                 (list (caar ts))
2043                                 (let ((ps perms)
2044                                       outp)
2045                                   (while ps
2046                                     (setq outp
2047                                           (cons
2048                                            (vector
2049                                             (caar ps)
2050                                             (list
2051                                              'gnus-summary-score-entry
2052                                              (nth 1 header)
2053                                              (if (or (string= (nth 1 header)
2054                                                               "head")
2055                                                      (string= (nth 1 header)
2056                                                               "body"))
2057                                                  ""
2058                                                (list 'gnus-summary-header
2059                                                      (nth 1 header)))
2060                                              (list 'quote (nth 1 (car ts)))
2061                                              (list 'gnus-score-delta-default
2062                                                    nil)
2063                                              (nth 1 (car ps))
2064                                              t)
2065                                             t)
2066                                            outp))
2067                                     (setq ps (cdr ps)))
2068                                   (list (nreverse outp))))
2069                                outt))
2070                         (setq ts (cdr ts)))
2071                       (list (nreverse outt))))
2072                    outh))
2073             (setq headers (cdr headers)))
2074           (list (nreverse outh))))))))
2075
2076 \f
2077
2078 (defun gnus-summary-mode (&optional group)
2079   "Major mode for reading articles.
2080
2081 All normal editing commands are switched off.
2082 \\<gnus-summary-mode-map>
2083 Each line in this buffer represents one article.  To read an
2084 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2085 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2086 respectively.
2087
2088 You can also post articles and send mail from this buffer.  To
2089 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2090 of an article, type `\\[gnus-summary-reply]'.
2091
2092 There are approx. one gazillion commands you can execute in this
2093 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2094
2095 The following commands are available:
2096
2097 \\{gnus-summary-mode-map}"
2098   (interactive)
2099   (when (gnus-visual-p 'summary-menu 'menu)
2100     (gnus-summary-make-menu-bar))
2101   (kill-all-local-variables)
2102   (gnus-summary-make-local-variables)
2103   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2104     (gnus-summary-make-local-variables))
2105   (gnus-make-thread-indent-array)
2106   (gnus-simplify-mode-line)
2107   (setq major-mode 'gnus-summary-mode)
2108   (setq mode-name "Summary")
2109   (make-local-variable 'minor-mode-alist)
2110   (use-local-map gnus-summary-mode-map)
2111   (buffer-disable-undo)
2112   (setq buffer-read-only t)             ;Disable modification
2113   (setq truncate-lines t)
2114   (setq selective-display t)
2115   (setq selective-display-ellipses t)   ;Display `...'
2116   (gnus-summary-set-display-table)
2117   (gnus-set-default-directory)
2118   (setq gnus-newsgroup-name group)
2119   (unless (gnus-news-group-p group)
2120     (setq gnus-newsgroup-incorporated
2121           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2122   (make-local-variable 'gnus-summary-line-format)
2123   (make-local-variable 'gnus-summary-line-format-spec)
2124   (make-local-variable 'gnus-summary-dummy-line-format)
2125   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2126   (make-local-variable 'gnus-summary-mark-positions)
2127   (make-local-hook 'pre-command-hook)
2128   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2129   (gnus-run-hooks 'gnus-summary-mode-hook)
2130   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2131   (gnus-update-summary-mark-positions))
2132
2133 (defun gnus-summary-make-local-variables ()
2134   "Make all the local summary buffer variables."
2135   (let (global)
2136     (dolist (local gnus-summary-local-variables)
2137       (if (consp local)
2138           (progn
2139             (if (eq (cdr local) 'global)
2140                 ;; Copy the global value of the variable.
2141                 (setq global (symbol-value (car local)))
2142               ;; Use the value from the list.
2143               (setq global (eval (cdr local))))
2144             (set (make-local-variable (car local)) global))
2145         ;; Simple nil-valued local variable.
2146         (set (make-local-variable local) nil)))))
2147
2148 (defun gnus-summary-clear-local-variables ()
2149   (let ((locals gnus-summary-local-variables))
2150     (while locals
2151       (if (consp (car locals))
2152           (and (vectorp (caar locals))
2153                (set (caar locals) nil))
2154         (and (vectorp (car locals))
2155              (set (car locals) nil)))
2156       (setq locals (cdr locals)))))
2157
2158 ;; Summary data functions.
2159
2160 (defmacro gnus-data-number (data)
2161   `(car ,data))
2162
2163 (defmacro gnus-data-set-number (data number)
2164   `(setcar ,data ,number))
2165
2166 (defmacro gnus-data-mark (data)
2167   `(nth 1 ,data))
2168
2169 (defmacro gnus-data-set-mark (data mark)
2170   `(setcar (nthcdr 1 ,data) ,mark))
2171
2172 (defmacro gnus-data-pos (data)
2173   `(nth 2 ,data))
2174
2175 (defmacro gnus-data-set-pos (data pos)
2176   `(setcar (nthcdr 2 ,data) ,pos))
2177
2178 (defmacro gnus-data-header (data)
2179   `(nth 3 ,data))
2180
2181 (defmacro gnus-data-set-header (data header)
2182   `(setcar (nthcdr 3 ,data) ,header))
2183
2184 (defmacro gnus-data-level (data)
2185   `(nth 4 ,data))
2186
2187 (defmacro gnus-data-unread-p (data)
2188   `(= (nth 1 ,data) gnus-unread-mark))
2189
2190 (defmacro gnus-data-read-p (data)
2191   `(/= (nth 1 ,data) gnus-unread-mark))
2192
2193 (defmacro gnus-data-pseudo-p (data)
2194   `(consp (nth 3 ,data)))
2195
2196 (defmacro gnus-data-find (number)
2197   `(assq ,number gnus-newsgroup-data))
2198
2199 (defmacro gnus-data-find-list (number &optional data)
2200   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2201      (memq (assq ,number bdata)
2202            bdata)))
2203
2204 (defmacro gnus-data-make (number mark pos header level)
2205   `(list ,number ,mark ,pos ,header ,level))
2206
2207 (defun gnus-data-enter (after-article number mark pos header level offset)
2208   (let ((data (gnus-data-find-list after-article)))
2209     (unless data
2210       (error "No such article: %d" after-article))
2211     (setcdr data (cons (gnus-data-make number mark pos header level)
2212                        (cdr data)))
2213     (setq gnus-newsgroup-data-reverse nil)
2214     (gnus-data-update-list (cddr data) offset)))
2215
2216 (defun gnus-data-enter-list (after-article list &optional offset)
2217   (when list
2218     (let ((data (and after-article (gnus-data-find-list after-article)))
2219           (ilist list))
2220       (if (not (or data
2221                    after-article))
2222           (let ((odata gnus-newsgroup-data))
2223             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2224             (when offset
2225               (gnus-data-update-list odata offset)))
2226         ;; Find the last element in the list to be spliced into the main
2227         ;; list.
2228         (while (cdr list)
2229           (setq list (cdr list)))
2230         (if (not data)
2231             (progn
2232               (setcdr list gnus-newsgroup-data)
2233               (setq gnus-newsgroup-data ilist)
2234               (when offset
2235                 (gnus-data-update-list (cdr list) offset)))
2236           (setcdr list (cdr data))
2237           (setcdr data ilist)
2238           (when offset
2239             (gnus-data-update-list (cdr list) offset))))
2240       (setq gnus-newsgroup-data-reverse nil))))
2241
2242 (defun gnus-data-remove (article &optional offset)
2243   (let ((data gnus-newsgroup-data))
2244     (if (= (gnus-data-number (car data)) article)
2245         (progn
2246           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2247                 gnus-newsgroup-data-reverse nil)
2248           (when offset
2249             (gnus-data-update-list gnus-newsgroup-data offset)))
2250       (while (cdr data)
2251         (when (= (gnus-data-number (cadr data)) article)
2252           (setcdr data (cddr data))
2253           (when offset
2254             (gnus-data-update-list (cdr data) offset))
2255           (setq data nil
2256                 gnus-newsgroup-data-reverse nil))
2257         (setq data (cdr data))))))
2258
2259 (defmacro gnus-data-list (backward)
2260   `(if ,backward
2261        (or gnus-newsgroup-data-reverse
2262            (setq gnus-newsgroup-data-reverse
2263                  (reverse gnus-newsgroup-data)))
2264      gnus-newsgroup-data))
2265
2266 (defun gnus-data-update-list (data offset)
2267   "Add OFFSET to the POS of all data entries in DATA."
2268   (setq gnus-newsgroup-data-reverse nil)
2269   (while data
2270     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2271     (setq data (cdr data))))
2272
2273 (defun gnus-summary-article-pseudo-p (article)
2274   "Say whether this article is a pseudo article or not."
2275   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2276
2277 (defmacro gnus-summary-article-sparse-p (article)
2278   "Say whether this article is a sparse article or not."
2279   `(memq ,article gnus-newsgroup-sparse))
2280
2281 (defmacro gnus-summary-article-ancient-p (article)
2282   "Say whether this article is a sparse article or not."
2283   `(memq ,article gnus-newsgroup-ancient))
2284
2285 (defun gnus-article-parent-p (number)
2286   "Say whether this article is a parent or not."
2287   (let ((data (gnus-data-find-list number)))
2288     (and (cdr data)                     ; There has to be an article after...
2289          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2290             (gnus-data-level (nth 1 data))))))
2291
2292 (defun gnus-article-children (number)
2293   "Return a list of all children to NUMBER."
2294   (let* ((data (gnus-data-find-list number))
2295          (level (gnus-data-level (car data)))
2296          children)
2297     (setq data (cdr data))
2298     (while (and data
2299                 (= (gnus-data-level (car data)) (1+ level)))
2300       (push (gnus-data-number (car data)) children)
2301       (setq data (cdr data)))
2302     children))
2303
2304 (defmacro gnus-summary-skip-intangible ()
2305   "If the current article is intangible, then jump to a different article."
2306   '(let ((to (get-text-property (point) 'gnus-intangible)))
2307      (and to (gnus-summary-goto-subject to))))
2308
2309 (defmacro gnus-summary-article-intangible-p ()
2310   "Say whether this article is intangible or not."
2311   '(get-text-property (point) 'gnus-intangible))
2312
2313 (defun gnus-article-read-p (article)
2314   "Say whether ARTICLE is read or not."
2315   (not (or (memq article gnus-newsgroup-marked)
2316            (memq article gnus-newsgroup-unreads)
2317            (memq article gnus-newsgroup-unselected)
2318            (memq article gnus-newsgroup-dormant))))
2319
2320 ;; Some summary mode macros.
2321
2322 (defmacro gnus-summary-article-number ()
2323   "The article number of the article on the current line.
2324 If there isn's an article number here, then we return the current
2325 article number."
2326   '(progn
2327      (gnus-summary-skip-intangible)
2328      (or (get-text-property (point) 'gnus-number)
2329          (gnus-summary-last-subject))))
2330
2331 (defmacro gnus-summary-article-header (&optional number)
2332   "Return the header of article NUMBER."
2333   `(gnus-data-header (gnus-data-find
2334                       ,(or number '(gnus-summary-article-number)))))
2335
2336 (defmacro gnus-summary-thread-level (&optional number)
2337   "Return the level of thread that starts with article NUMBER."
2338   `(if (and (eq gnus-summary-make-false-root 'dummy)
2339             (get-text-property (point) 'gnus-intangible))
2340        0
2341      (gnus-data-level (gnus-data-find
2342                        ,(or number '(gnus-summary-article-number))))))
2343
2344 (defmacro gnus-summary-article-mark (&optional number)
2345   "Return the mark of article NUMBER."
2346   `(gnus-data-mark (gnus-data-find
2347                     ,(or number '(gnus-summary-article-number)))))
2348
2349 (defmacro gnus-summary-article-pos (&optional number)
2350   "Return the position of the line of article NUMBER."
2351   `(gnus-data-pos (gnus-data-find
2352                    ,(or number '(gnus-summary-article-number)))))
2353
2354 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2355 (defmacro gnus-summary-article-subject (&optional number)
2356   "Return current subject string or nil if nothing."
2357   `(let ((headers
2358           ,(if number
2359                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2360              '(gnus-data-header (assq (gnus-summary-article-number)
2361                                       gnus-newsgroup-data)))))
2362      (and headers
2363           (vectorp headers)
2364           (mail-header-subject headers))))
2365
2366 (defmacro gnus-summary-article-score (&optional number)
2367   "Return current article score."
2368   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2369                   gnus-newsgroup-scored))
2370        gnus-summary-default-score 0))
2371
2372 (defun gnus-summary-article-children (&optional number)
2373   "Return a list of article numbers that are children of article NUMBER."
2374   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2375          (level (gnus-data-level (car data)))
2376          l children)
2377     (while (and (setq data (cdr data))
2378                 (> (setq l (gnus-data-level (car data))) level))
2379       (and (= (1+ level) l)
2380            (push (gnus-data-number (car data))
2381                  children)))
2382     (nreverse children)))
2383
2384 (defun gnus-summary-article-parent (&optional number)
2385   "Return the article number of the parent of article NUMBER."
2386   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2387                                     (gnus-data-list t)))
2388          (level (gnus-data-level (car data))))
2389     (if (zerop level)
2390         ()                              ; This is a root.
2391       ;; We search until we find an article with a level less than
2392       ;; this one.  That function has to be the parent.
2393       (while (and (setq data (cdr data))
2394                   (not (< (gnus-data-level (car data)) level))))
2395       (and data (gnus-data-number (car data))))))
2396
2397 (defun gnus-unread-mark-p (mark)
2398   "Say whether MARK is the unread mark."
2399   (= mark gnus-unread-mark))
2400
2401 (defun gnus-read-mark-p (mark)
2402   "Say whether MARK is one of the marks that mark as read.
2403 This is all marks except unread, ticked, dormant, and expirable."
2404   (not (or (= mark gnus-unread-mark)
2405            (= mark gnus-ticked-mark)
2406            (= mark gnus-dormant-mark)
2407            (= mark gnus-expirable-mark))))
2408
2409 (defmacro gnus-article-mark (number)
2410   "Return the MARK of article NUMBER.
2411 This macro should only be used when computing the mark the \"first\"
2412 time; i.e., when generating the summary lines.  After that,
2413 `gnus-summary-article-mark' should be used to examine the
2414 marks of articles."
2415   `(cond
2416     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2417     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2418     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2419     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2420     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2421     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2422     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2423     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2424            gnus-ancient-mark))))
2425
2426 ;; Saving hidden threads.
2427
2428 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2429 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2430
2431 (defmacro gnus-save-hidden-threads (&rest forms)
2432   "Save hidden threads, eval FORMS, and restore the hidden threads."
2433   (let ((config (make-symbol "config")))
2434     `(let ((,config (gnus-hidden-threads-configuration)))
2435        (unwind-protect
2436            (save-excursion
2437              ,@forms)
2438          (gnus-restore-hidden-threads-configuration ,config)))))
2439
2440 (defun gnus-data-compute-positions ()
2441   "Compute the positions of all articles."
2442   (setq gnus-newsgroup-data-reverse nil)
2443   (let ((data gnus-newsgroup-data))
2444     (save-excursion
2445       (gnus-save-hidden-threads
2446         (gnus-summary-show-all-threads)
2447         (goto-char (point-min))
2448         (while data
2449           (while (get-text-property (point) 'gnus-intangible)
2450             (forward-line 1))
2451           (gnus-data-set-pos (car data) (+ (point) 3))
2452           (setq data (cdr data))
2453           (forward-line 1))))))
2454
2455 (defun gnus-hidden-threads-configuration ()
2456   "Return the current hidden threads configuration."
2457   (save-excursion
2458     (let (config)
2459       (goto-char (point-min))
2460       (while (search-forward "\r" nil t)
2461         (push (1- (point)) config))
2462       config)))
2463
2464 (defun gnus-restore-hidden-threads-configuration (config)
2465   "Restore hidden threads configuration from CONFIG."
2466   (save-excursion
2467     (let (point buffer-read-only)
2468       (while (setq point (pop config))
2469         (when (and (< point (point-max))
2470                    (goto-char point)
2471                    (eq (char-after) ?\n))
2472           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2473
2474 ;; Various summary mode internalish functions.
2475
2476 (defun gnus-mouse-pick-article (e)
2477   (interactive "e")
2478   (mouse-set-point e)
2479   (gnus-summary-next-page nil t))
2480
2481 (defun gnus-summary-set-display-table ()
2482   "Change the display table.
2483 Odd characters have a tendency to mess
2484 up nicely formatted displays - we make all possible glyphs
2485 display only a single character."
2486
2487   ;; We start from the standard display table, if any.
2488   (let ((table (or (copy-sequence standard-display-table)
2489                    (make-display-table)))
2490         (i 32))
2491     ;; Nix out all the control chars...
2492     (while (>= (setq i (1- i)) 0)
2493       (aset table i [??]))
2494     ;; ... but not newline and cr, of course.  (cr is necessary for the
2495     ;; selective display).
2496     (aset table ?\n nil)
2497     (aset table ?\r nil)
2498     ;; We keep TAB as well.
2499     (aset table ?\t nil)
2500     ;; We nix out any glyphs over 126 that are not set already.
2501     (let ((i 256))
2502       (while (>= (setq i (1- i)) 127)
2503         ;; Only modify if the entry is nil.
2504         (unless (aref table i)
2505           (aset table i [??]))))
2506     (setq buffer-display-table table)))
2507
2508 (defun gnus-summary-setup-buffer (group)
2509   "Initialize summary buffer."
2510   (let ((buffer (concat "*Summary " group "*")))
2511     (if (get-buffer buffer)
2512         (progn
2513           (set-buffer buffer)
2514           (setq gnus-summary-buffer (current-buffer))
2515           (not gnus-newsgroup-prepared))
2516       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2517       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2518       (gnus-summary-mode group)
2519       (when gnus-carpal
2520         (gnus-carpal-setup-buffer 'summary))
2521       (unless gnus-single-article-buffer
2522         (make-local-variable 'gnus-article-buffer)
2523         (make-local-variable 'gnus-article-current)
2524         (make-local-variable 'gnus-original-article-buffer))
2525       (setq gnus-newsgroup-name group)
2526       t)))
2527
2528 (defun gnus-set-global-variables ()
2529   "Set the global equivalents of the buffer-local variables.
2530 They are set to the latest values they had.  These reflect the summary
2531 buffer that was in action when the last article was fetched."
2532   (when (eq major-mode 'gnus-summary-mode)
2533     (setq gnus-summary-buffer (current-buffer))
2534     (let ((name gnus-newsgroup-name)
2535           (marked gnus-newsgroup-marked)
2536           (unread gnus-newsgroup-unreads)
2537           (headers gnus-current-headers)
2538           (data gnus-newsgroup-data)
2539           (summary gnus-summary-buffer)
2540           (article-buffer gnus-article-buffer)
2541           (original gnus-original-article-buffer)
2542           (gac gnus-article-current)
2543           (reffed gnus-reffed-article-number)
2544           (score-file gnus-current-score-file)
2545           (default-charset gnus-newsgroup-charset)
2546           vlist)
2547       (let ((locals gnus-newsgroup-variables))
2548         (while locals
2549           (if (consp (car locals))
2550               (push (eval (caar locals)) vlist)
2551             (push (eval (car locals)) vlist))
2552           (setq locals (cdr locals)))
2553         (setq vlist (nreverse vlist)))
2554       (save-excursion
2555         (set-buffer gnus-group-buffer)
2556         (setq gnus-newsgroup-name name
2557               gnus-newsgroup-marked marked
2558               gnus-newsgroup-unreads unread
2559               gnus-current-headers headers
2560               gnus-newsgroup-data data
2561               gnus-article-current gac
2562               gnus-summary-buffer summary
2563               gnus-article-buffer article-buffer
2564               gnus-original-article-buffer original
2565               gnus-reffed-article-number reffed
2566               gnus-current-score-file score-file
2567               gnus-newsgroup-charset default-charset)
2568         (let ((locals gnus-newsgroup-variables))
2569           (while locals
2570             (if (consp (car locals))
2571                 (set (caar locals) (pop vlist))
2572               (set (car locals) (pop vlist)))
2573             (setq locals (cdr locals))))
2574         ;; The article buffer also has local variables.
2575         (when (gnus-buffer-live-p gnus-article-buffer)
2576           (set-buffer gnus-article-buffer)
2577           (setq gnus-summary-buffer summary))))))
2578
2579 (defun gnus-summary-article-unread-p (article)
2580   "Say whether ARTICLE is unread or not."
2581   (memq article gnus-newsgroup-unreads))
2582
2583 (defun gnus-summary-first-article-p (&optional article)
2584   "Return whether ARTICLE is the first article in the buffer."
2585   (if (not (setq article (or article (gnus-summary-article-number))))
2586       nil
2587     (eq article (caar gnus-newsgroup-data))))
2588
2589 (defun gnus-summary-last-article-p (&optional article)
2590   "Return whether ARTICLE is the last article in the buffer."
2591   (if (not (setq article (or article (gnus-summary-article-number))))
2592       ;; All non-existent numbers are the last article.  :-)
2593       t
2594     (not (cdr (gnus-data-find-list article)))))
2595
2596 (defun gnus-make-thread-indent-array ()
2597   (let ((n 200))
2598     (unless (and gnus-thread-indent-array
2599                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2600       (setq gnus-thread-indent-array (make-vector 201 "")
2601             gnus-thread-indent-array-level gnus-thread-indent-level)
2602       (while (>= n 0)
2603         (aset gnus-thread-indent-array n
2604               (make-string (* n gnus-thread-indent-level) ? ))
2605         (setq n (1- n))))))
2606
2607 (defun gnus-update-summary-mark-positions ()
2608   "Compute where the summary marks are to go."
2609   (save-excursion
2610     (when (gnus-buffer-exists-p gnus-summary-buffer)
2611       (set-buffer gnus-summary-buffer))
2612     (let ((gnus-replied-mark 129)
2613           (gnus-score-below-mark 130)
2614           (gnus-score-over-mark 130)
2615           (gnus-download-mark 131)
2616           (spec gnus-summary-line-format-spec)
2617           gnus-visual pos)
2618       (save-excursion
2619         (gnus-set-work-buffer)
2620         (let ((gnus-summary-line-format-spec spec)
2621               (gnus-newsgroup-downloadable '((0 . t))))
2622           (gnus-summary-insert-line
2623            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2624            0 nil 128 t nil "" nil 1)
2625           (goto-char (point-min))
2626           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2627                                              (- (point) 2)))))
2628           (goto-char (point-min))
2629           (push (cons 'replied (and (search-forward "\201" nil t)
2630                                     (- (point) 2)))
2631                 pos)
2632           (goto-char (point-min))
2633           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2634                 pos)
2635           (goto-char (point-min))
2636           (push (cons 'download
2637                       (and (search-forward "\203" nil t) (- (point) 2)))
2638                 pos)))
2639       (setq gnus-summary-mark-positions pos))))
2640
2641 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2642   "Insert a dummy root in the summary buffer."
2643   (beginning-of-line)
2644   (gnus-add-text-properties
2645    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2646    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2647
2648 (defun gnus-summary-from-or-to-or-newsgroups (header)
2649   (let ((to (cdr (assq 'To (mail-header-extra header))))
2650         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2651         (default-mime-charset (with-current-buffer gnus-summary-buffer
2652                                 default-mime-charset)))
2653     (cond
2654      ((and to
2655            gnus-ignored-from-addresses
2656            (string-match gnus-ignored-from-addresses
2657                          (mail-header-from header)))
2658       (concat "-> "
2659               (or (car (funcall gnus-extract-address-components
2660                                 (funcall
2661                                  gnus-decode-encoded-word-function to)))
2662                   (funcall gnus-decode-encoded-word-function to))))
2663      ((and newsgroups
2664            gnus-ignored-from-addresses
2665            (string-match gnus-ignored-from-addresses
2666                          (mail-header-from header)))
2667       (concat "=> " newsgroups))
2668      (t
2669       (or (car (funcall gnus-extract-address-components
2670                         (mail-header-from header)))
2671           (mail-header-from header))))))
2672
2673 (defun gnus-summary-insert-line (gnus-tmp-header
2674                                  gnus-tmp-level gnus-tmp-current
2675                                  gnus-tmp-unread gnus-tmp-replied
2676                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2677                                  &optional gnus-tmp-dummy gnus-tmp-score
2678                                  gnus-tmp-process)
2679   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2680          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2681          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2682          (gnus-tmp-score-char
2683           (if (or (null gnus-summary-default-score)
2684                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2685                       gnus-summary-zcore-fuzz))
2686               ?  ;Whitespace
2687             (if (< gnus-tmp-score gnus-summary-default-score)
2688                 gnus-score-below-mark gnus-score-over-mark)))
2689          (gnus-tmp-replied
2690           (cond (gnus-tmp-process gnus-process-mark)
2691                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2692                  gnus-cached-mark)
2693                 (gnus-tmp-replied gnus-replied-mark)
2694                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2695                  gnus-saved-mark)
2696                 (t gnus-unread-mark)))
2697          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2698          (gnus-tmp-name
2699           (cond
2700            ((string-match "<[^>]+> *$" gnus-tmp-from)
2701             (let ((beg (match-beginning 0)))
2702               (or (and (string-match "^\".+\"" gnus-tmp-from)
2703                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2704                   (substring gnus-tmp-from 0 beg))))
2705            ((string-match "(.+)" gnus-tmp-from)
2706             (substring gnus-tmp-from
2707                        (1+ (match-beginning 0)) (1- (match-end 0))))
2708            (t gnus-tmp-from)))
2709          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2710          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2711          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2712          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2713          (buffer-read-only nil))
2714     (when (string= gnus-tmp-name "")
2715       (setq gnus-tmp-name gnus-tmp-from))
2716     (unless (numberp gnus-tmp-lines)
2717       (setq gnus-tmp-lines 0))
2718     (gnus-put-text-property-excluding-characters-with-faces
2719      (point)
2720      (progn (eval gnus-summary-line-format-spec) (point))
2721      'gnus-number gnus-tmp-number)
2722     (when (gnus-visual-p 'summary-highlight 'highlight)
2723       (forward-line -1)
2724       (gnus-run-hooks 'gnus-summary-update-hook)
2725       (forward-line 1))))
2726
2727 (defun gnus-summary-update-line (&optional dont-update)
2728   "Update summary line after change."
2729   (when (and gnus-summary-default-score
2730              (not gnus-summary-inhibit-highlight))
2731     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2732            (article (gnus-summary-article-number))
2733            (score (gnus-summary-article-score article)))
2734       (unless dont-update
2735         (if (and gnus-summary-mark-below
2736                  (< (gnus-summary-article-score)
2737                     gnus-summary-mark-below))
2738             ;; This article has a low score, so we mark it as read.
2739             (when (memq article gnus-newsgroup-unreads)
2740               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2741           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2742             ;; This article was previously marked as read on account
2743             ;; of a low score, but now it has risen, so we mark it as
2744             ;; unread.
2745             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2746         (gnus-summary-update-mark
2747          (if (or (null gnus-summary-default-score)
2748                  (<= (abs (- score gnus-summary-default-score))
2749                      gnus-summary-zcore-fuzz))
2750              ?  ;Whitespace
2751            (if (< score gnus-summary-default-score)
2752                gnus-score-below-mark gnus-score-over-mark))
2753          'score))
2754       ;; Do visual highlighting.
2755       (when (gnus-visual-p 'summary-highlight 'highlight)
2756         (gnus-run-hooks 'gnus-summary-update-hook)))))
2757
2758 (defvar gnus-tmp-new-adopts nil)
2759
2760 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2761   "Return the number of articles in THREAD.
2762 This may be 0 in some cases -- if none of the articles in
2763 the thread are to be displayed."
2764   (let* ((number
2765           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2766           (cond
2767            ((not (listp thread))
2768             1)
2769            ((and (consp thread) (cdr thread))
2770             (apply
2771              '+ 1 (mapcar
2772                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2773            ((null thread)
2774             1)
2775            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2776             1)
2777            (t 0))))
2778     (when (and level (zerop level) gnus-tmp-new-adopts)
2779       (incf number
2780             (apply '+ (mapcar
2781                        'gnus-summary-number-of-articles-in-thread
2782                        gnus-tmp-new-adopts))))
2783     (if char
2784         (if (> number 1) gnus-not-empty-thread-mark
2785           gnus-empty-thread-mark)
2786       number)))
2787
2788 (defun gnus-summary-set-local-parameters (group)
2789   "Go through the local params of GROUP and set all variable specs in that list."
2790   (let ((params (gnus-group-find-parameter group))
2791         elem)
2792     (while params
2793       (setq elem (car params)
2794             params (cdr params))
2795       (and (consp elem)                 ; Has to be a cons.
2796            (consp (cdr elem))           ; The cdr has to be a list.
2797            (symbolp (car elem))         ; Has to be a symbol in there.
2798            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2799            (ignore-errors               ; So we set it.
2800              (make-local-variable (car elem))
2801              (set (car elem) (eval (nth 1 elem))))))))
2802
2803 (defun gnus-summary-read-group (group &optional show-all no-article
2804                                       kill-buffer no-display backward
2805                                       select-articles)
2806   "Start reading news in newsgroup GROUP.
2807 If SHOW-ALL is non-nil, already read articles are also listed.
2808 If NO-ARTICLE is non-nil, no article is selected initially.
2809 If NO-DISPLAY, don't generate a summary buffer."
2810   (let (result)
2811     (while (and group
2812                 (null (setq result
2813                             (let ((gnus-auto-select-next nil))
2814                               (or (gnus-summary-read-group-1
2815                                    group show-all no-article
2816                                    kill-buffer no-display
2817                                    select-articles)
2818                                   (setq show-all nil
2819                                         select-articles nil)))))
2820                 (eq gnus-auto-select-next 'quietly))
2821       (set-buffer gnus-group-buffer)
2822       ;; The entry function called above goes to the next
2823       ;; group automatically, so we go two groups back
2824       ;; if we are searching for the previous group.
2825       (when backward
2826         (gnus-group-prev-unread-group 2))
2827       (if (not (equal group (gnus-group-group-name)))
2828           (setq group (gnus-group-group-name))
2829         (setq group nil)))
2830     result))
2831
2832 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2833   "Directly jump to the other GROUP from summary buffer.
2834 If SHOW-ALL is non-nil, already read articles are also listed."
2835   (interactive
2836    (if (eq gnus-summary-buffer (current-buffer))
2837        (list (completing-read
2838               "Group: " gnus-active-hashtb nil t
2839               (when (and gnus-newsgroup-name
2840                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2841                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2842               'gnus-group-history)
2843              current-prefix-arg)
2844      (error "%s must be invoked from a gnus summary buffer." this-command)))
2845   (unless (or (zerop (length group))
2846               (and gnus-newsgroup-name
2847                    (string-equal gnus-newsgroup-name group)))
2848     (gnus-summary-exit)
2849     (gnus-summary-read-group group show-all
2850                              gnus-dont-select-after-jump-to-other-group)))
2851
2852 (defun gnus-summary-read-group-1 (group show-all no-article
2853                                         kill-buffer no-display
2854                                         &optional select-articles)
2855   ;; Killed foreign groups can't be entered.
2856   (when (and (not (gnus-group-native-p group))
2857              (not (gnus-gethash group gnus-newsrc-hashtb)))
2858     (error "Dead non-native groups can't be entered"))
2859   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2860   (let* ((new-group (gnus-summary-setup-buffer group))
2861          (quit-config (gnus-group-quit-config group))
2862          (did-select (and new-group (gnus-select-newsgroup
2863                                      group show-all select-articles))))
2864     (cond
2865      ;; This summary buffer exists already, so we just select it.
2866      ((not new-group)
2867       (gnus-set-global-variables)
2868       (when kill-buffer
2869         (gnus-kill-or-deaden-summary kill-buffer))
2870       (gnus-configure-windows 'summary 'force)
2871       (gnus-set-mode-line 'summary)
2872       (gnus-summary-position-point)
2873       (message "")
2874       t)
2875      ;; We couldn't select this group.
2876      ((null did-select)
2877       (when (and (eq major-mode 'gnus-summary-mode)
2878                  (not (equal (current-buffer) kill-buffer)))
2879         (kill-buffer (current-buffer))
2880         (if (not quit-config)
2881             (progn
2882               ;; Update the info -- marks might need to be removed,
2883               ;; for instance.
2884               (gnus-summary-update-info)
2885               (set-buffer gnus-group-buffer)
2886               (gnus-group-jump-to-group group)
2887               (gnus-group-next-unread-group 1))
2888           (gnus-handle-ephemeral-exit quit-config)))
2889       (gnus-message 3 "Can't select group")
2890       nil)
2891      ;; The user did a `C-g' while prompting for number of articles,
2892      ;; so we exit this group.
2893      ((eq did-select 'quit)
2894       (and (eq major-mode 'gnus-summary-mode)
2895            (not (equal (current-buffer) kill-buffer))
2896            (kill-buffer (current-buffer)))
2897       (when kill-buffer
2898         (gnus-kill-or-deaden-summary kill-buffer))
2899       (if (not quit-config)
2900           (progn
2901             (set-buffer gnus-group-buffer)
2902             (gnus-group-jump-to-group group)
2903             (gnus-group-next-unread-group 1)
2904             (gnus-configure-windows 'group 'force))
2905         (gnus-handle-ephemeral-exit quit-config))
2906       ;; Finally signal the quit.
2907       (signal 'quit nil))
2908      ;; The group was successfully selected.
2909      (t
2910       (gnus-set-global-variables)
2911       ;; Save the active value in effect when the group was entered.
2912       (setq gnus-newsgroup-active
2913             (gnus-copy-sequence
2914              (gnus-active gnus-newsgroup-name)))
2915       ;; You can change the summary buffer in some way with this hook.
2916       (gnus-run-hooks 'gnus-select-group-hook)
2917       ;; Set any local variables in the group parameters.
2918       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2919       (gnus-update-format-specifications
2920        nil 'summary 'summary-mode 'summary-dummy)
2921       (gnus-update-summary-mark-positions)
2922       ;; Do score processing.
2923       (when gnus-use-scoring
2924         (gnus-possibly-score-headers))
2925       ;; Check whether to fill in the gaps in the threads.
2926       (when gnus-build-sparse-threads
2927         (gnus-build-sparse-threads))
2928       ;; Find the initial limit.
2929       (if gnus-show-threads
2930           (if show-all
2931               (let ((gnus-newsgroup-dormant nil))
2932                 (gnus-summary-initial-limit show-all))
2933             (gnus-summary-initial-limit show-all))
2934         ;; When untreaded, all articles are always shown.
2935         (setq gnus-newsgroup-limit
2936               (mapcar
2937                (lambda (header) (mail-header-number header))
2938                gnus-newsgroup-headers)))
2939       ;; Generate the summary buffer.
2940       (unless no-display
2941         (gnus-summary-prepare))
2942       (when gnus-use-trees
2943         (gnus-tree-open group)
2944         (setq gnus-summary-highlight-line-function
2945               'gnus-tree-highlight-article))
2946       ;; If the summary buffer is empty, but there are some low-scored
2947       ;; articles or some excluded dormants, we include these in the
2948       ;; buffer.
2949       (when (and (zerop (buffer-size))
2950                  (not no-display))
2951         (cond (gnus-newsgroup-dormant
2952                (gnus-summary-limit-include-dormant))
2953               ((and gnus-newsgroup-scored show-all)
2954                (gnus-summary-limit-include-expunged t))))
2955       ;; Function `gnus-apply-kill-file' must be called in this hook.
2956       (gnus-run-hooks 'gnus-apply-kill-hook)
2957       (if (and (zerop (buffer-size))
2958                (not no-display))
2959           (progn
2960             ;; This newsgroup is empty.
2961             (gnus-summary-catchup-and-exit nil t)
2962             (gnus-message 6 "No unread news")
2963             (when kill-buffer
2964               (gnus-kill-or-deaden-summary kill-buffer))
2965             ;; Return nil from this function.
2966             nil)
2967         ;; Hide conversation thread subtrees.  We cannot do this in
2968         ;; gnus-summary-prepare-hook since kill processing may not
2969         ;; work with hidden articles.
2970         (and gnus-show-threads
2971              gnus-thread-hide-subtree
2972              (gnus-summary-hide-all-threads))
2973         (when kill-buffer
2974           (gnus-kill-or-deaden-summary kill-buffer))
2975         ;; Show first unread article if requested.
2976         (if (and (not no-article)
2977                  (not no-display)
2978                  gnus-newsgroup-unreads
2979                  gnus-auto-select-first)
2980             (progn
2981               (gnus-configure-windows 'summary)
2982               (cond
2983                ((eq gnus-auto-select-first 'best)
2984                 (gnus-summary-best-unread-article))
2985                ((eq gnus-auto-select-first t)
2986                 (gnus-summary-first-unread-article))
2987                ((gnus-functionp gnus-auto-select-first)
2988                 (funcall gnus-auto-select-first))))
2989           ;; Don't select any articles, just move point to the first
2990           ;; article in the group.
2991           (goto-char (point-min))
2992           (gnus-summary-position-point)
2993           (gnus-configure-windows 'summary 'force)
2994           (gnus-set-mode-line 'summary))
2995         (when (get-buffer-window gnus-group-buffer t)
2996           ;; Gotta use windows, because recenter does weird stuff if
2997           ;; the current buffer ain't the displayed window.
2998           (let ((owin (selected-window)))
2999             (select-window (get-buffer-window gnus-group-buffer t))
3000             (when (gnus-group-goto-group group)
3001               (recenter))
3002             (select-window owin)))
3003         ;; Mark this buffer as "prepared".
3004         (setq gnus-newsgroup-prepared t)
3005         (gnus-run-hooks 'gnus-summary-prepared-hook)
3006         t)))))
3007
3008 (defun gnus-summary-prepare ()
3009   "Generate the summary buffer."
3010   (interactive)
3011   (let ((buffer-read-only nil))
3012     (erase-buffer)
3013     (setq gnus-newsgroup-data nil
3014           gnus-newsgroup-data-reverse nil)
3015     (gnus-run-hooks 'gnus-summary-generate-hook)
3016     ;; Generate the buffer, either with threads or without.
3017     (when gnus-newsgroup-headers
3018       (gnus-summary-prepare-threads
3019        (if gnus-show-threads
3020            (gnus-sort-gathered-threads
3021             (funcall gnus-summary-thread-gathering-function
3022                      (gnus-sort-threads
3023                       (gnus-cut-threads (gnus-make-threads)))))
3024          ;; Unthreaded display.
3025          (gnus-sort-articles gnus-newsgroup-headers))))
3026     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3027     ;; Call hooks for modifying summary buffer.
3028     (goto-char (point-min))
3029     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3030
3031 (defsubst gnus-general-simplify-subject (subject)
3032   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3033   (setq subject
3034         (cond
3035          ;; Truncate the subject.
3036          (gnus-simplify-subject-functions
3037           (gnus-map-function gnus-simplify-subject-functions subject))
3038          ((numberp gnus-summary-gather-subject-limit)
3039           (setq subject (gnus-simplify-subject-re subject))
3040           (if (> (length subject) gnus-summary-gather-subject-limit)
3041               (substring subject 0 gnus-summary-gather-subject-limit)
3042             subject))
3043          ;; Fuzzily simplify it.
3044          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3045           (gnus-simplify-subject-fuzzy subject))
3046          ;; Just remove the leading "Re:".
3047          (t
3048           (gnus-simplify-subject-re subject))))
3049
3050   (if (and gnus-summary-gather-exclude-subject
3051            (string-match gnus-summary-gather-exclude-subject subject))
3052       nil                               ; This article shouldn't be gathered
3053     subject))
3054
3055 (defun gnus-summary-simplify-subject-query ()
3056   "Query where the respool algorithm would put this article."
3057   (interactive)
3058   (gnus-summary-select-article)
3059   (message "%s"
3060            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3061
3062 (defun gnus-gather-threads-by-subject (threads)
3063   "Gather threads by looking at Subject headers."
3064   (if (not gnus-summary-make-false-root)
3065       threads
3066     (let ((hashtb (gnus-make-hashtable 1024))
3067           (prev threads)
3068           (result threads)
3069           subject hthread whole-subject)
3070       (while threads
3071         (setq subject (gnus-general-simplify-subject
3072                        (setq whole-subject (mail-header-subject
3073                                             (caar threads)))))
3074         (when subject
3075           (if (setq hthread (gnus-gethash subject hashtb))
3076               (progn
3077                 ;; We enter a dummy root into the thread, if we
3078                 ;; haven't done that already.
3079                 (unless (stringp (caar hthread))
3080                   (setcar hthread (list whole-subject (car hthread))))
3081                 ;; We add this new gathered thread to this gathered
3082                 ;; thread.
3083                 (setcdr (car hthread)
3084                         (nconc (cdar hthread) (list (car threads))))
3085                 ;; Remove it from the list of threads.
3086                 (setcdr prev (cdr threads))
3087                 (setq threads prev))
3088             ;; Enter this thread into the hash table.
3089             (gnus-sethash subject threads hashtb)))
3090         (setq prev threads)
3091         (setq threads (cdr threads)))
3092       result)))
3093
3094 (defun gnus-gather-threads-by-references (threads)
3095   "Gather threads by looking at References headers."
3096   (let ((idhashtb (gnus-make-hashtable 1024))
3097         (thhashtb (gnus-make-hashtable 1024))
3098         (prev threads)
3099         (result threads)
3100         ids references id gthread gid entered ref)
3101     (while threads
3102       (when (setq references (mail-header-references (caar threads)))
3103         (setq id (mail-header-id (caar threads))
3104               ids (gnus-split-references references)
3105               entered nil)
3106         (while (setq ref (pop ids))
3107           (setq ids (delete ref ids))
3108           (if (not (setq gid (gnus-gethash ref idhashtb)))
3109               (progn
3110                 (gnus-sethash ref id idhashtb)
3111                 (gnus-sethash id threads thhashtb))
3112             (setq gthread (gnus-gethash gid thhashtb))
3113             (unless entered
3114               ;; We enter a dummy root into the thread, if we
3115               ;; haven't done that already.
3116               (unless (stringp (caar gthread))
3117                 (setcar gthread (list (mail-header-subject (caar gthread))
3118                                       (car gthread))))
3119               ;; We add this new gathered thread to this gathered
3120               ;; thread.
3121               (setcdr (car gthread)
3122                       (nconc (cdar gthread) (list (car threads)))))
3123             ;; Add it into the thread hash table.
3124             (gnus-sethash id gthread thhashtb)
3125             (setq entered t)
3126             ;; Remove it from the list of threads.
3127             (setcdr prev (cdr threads))
3128             (setq threads prev))))
3129       (setq prev threads)
3130       (setq threads (cdr threads)))
3131     result))
3132
3133 (defun gnus-sort-gathered-threads (threads)
3134   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3135   (let ((result threads))
3136     (while threads
3137       (when (stringp (caar threads))
3138         (setcdr (car threads)
3139                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3140       (setq threads (cdr threads)))
3141     result))
3142
3143 (defun gnus-thread-loop-p (root thread)
3144   "Say whether ROOT is in THREAD."
3145   (let ((stack (list thread))
3146         (infloop 0)
3147         th)
3148     (while (setq thread (pop stack))
3149       (setq th (cdr thread))
3150       (while (and th
3151                   (not (eq (caar th) root)))
3152         (pop th))
3153       (if th
3154           ;; We have found a loop.
3155           (let (ref-dep)
3156             (setcdr thread (delq (car th) (cdr thread)))
3157             (if (boundp (setq ref-dep (intern "none"
3158                                               gnus-newsgroup-dependencies)))
3159                 (setcdr (symbol-value ref-dep)
3160                         (nconc (cdr (symbol-value ref-dep))
3161                                (list (car th))))
3162               (set ref-dep (list nil (car th))))
3163             (setq infloop 1
3164                   stack nil))
3165         ;; Push all the subthreads onto the stack.
3166         (push (cdr thread) stack)))
3167     infloop))
3168
3169 (defun gnus-make-threads ()
3170   "Go through the dependency hashtb and find the roots.  Return all threads."
3171   (let (threads)
3172     (while (catch 'infloop
3173              (mapatoms
3174               (lambda (refs)
3175                 ;; Deal with self-referencing References loops.
3176                 (when (and (car (symbol-value refs))
3177                            (not (zerop
3178                                  (apply
3179                                   '+
3180                                   (mapcar
3181                                    (lambda (thread)
3182                                      (gnus-thread-loop-p
3183                                       (car (symbol-value refs)) thread))
3184                                    (cdr (symbol-value refs)))))))
3185                   (setq threads nil)
3186                   (throw 'infloop t))
3187                 (unless (car (symbol-value refs))
3188                   ;; These threads do not refer back to any other articles,
3189                   ;; so they're roots.
3190                   (setq threads (append (cdr (symbol-value refs)) threads))))
3191               gnus-newsgroup-dependencies)))
3192     threads))
3193
3194 ;; Build the thread tree.
3195 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3196   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3197
3198 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3199 if it was already present.
3200
3201 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3202 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3203 Message-IDs will be renamed be renamed to a unique Message-ID before
3204 being entered.
3205
3206 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3207   (let* ((id (mail-header-id header))
3208          (id-dep (and id (intern id dependencies)))
3209          ref ref-dep ref-header)
3210     ;; Enter this `header' in the `dependencies' table.
3211     (cond
3212      ((not id-dep)
3213       (setq header nil))
3214      ;; The first two cases do the normal part: enter a new `header'
3215      ;; in the `dependencies' table.
3216      ((not (boundp id-dep))
3217       (set id-dep (list header)))
3218      ((null (car (symbol-value id-dep)))
3219       (setcar (symbol-value id-dep) header))
3220
3221      ;; From here the `header' was already present in the
3222      ;; `dependencies' table.
3223      (force-new
3224       ;; Overrides an existing entry;
3225       ;; just set the header part of the entry.
3226       (setcar (symbol-value id-dep) header))
3227
3228      ;; Renames the existing `header' to a unique Message-ID.
3229      ((not gnus-summary-ignore-duplicates)
3230       ;; An article with this Message-ID has already been seen.
3231       ;; We rename the Message-ID.
3232       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3233            (list header))
3234       (mail-header-set-id header id))
3235
3236      ;; The last case ignores an existing entry, except it adds any
3237      ;; additional Xrefs (in case the two articles came from different
3238      ;; servers.
3239      ;; Also sets `header' to `nil' meaning that the `dependencies'
3240      ;; table was *not* modified.
3241      (t
3242       (mail-header-set-xref
3243        (car (symbol-value id-dep))
3244        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3245                    "")
3246                (or (mail-header-xref header) "")))
3247       (setq header nil)))
3248
3249     (when header
3250       ;; First check if that we are not creating a References loop.
3251       (setq ref (gnus-parent-id (mail-header-references header)))
3252       (while (and ref
3253                   (setq ref-dep (intern-soft ref dependencies))
3254                   (boundp ref-dep)
3255                   (setq ref-header (car (symbol-value ref-dep))))
3256         (if (string= id ref)
3257             ;; Yuk!  This is a reference loop.  Make the article be a
3258             ;; root article.
3259             (progn
3260               (mail-header-set-references (car (symbol-value id-dep)) "none")
3261               (setq ref nil))
3262           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3263       (setq ref (gnus-parent-id (mail-header-references header)))
3264       (setq ref-dep (intern (or ref "none") dependencies))
3265       (if (boundp ref-dep)
3266           (setcdr (symbol-value ref-dep)
3267                   (nconc (cdr (symbol-value ref-dep))
3268                          (list (symbol-value id-dep))))
3269         (set ref-dep (list nil (symbol-value id-dep)))))
3270     header))
3271
3272 (defun gnus-build-sparse-threads ()
3273   (let ((headers gnus-newsgroup-headers)
3274         (mail-parse-charset gnus-newsgroup-charset)
3275         (gnus-summary-ignore-duplicates t)
3276         header references generation relations
3277         subject child end new-child date)
3278     ;; First we create an alist of generations/relations, where
3279     ;; generations is how much we trust the relation, and the relation
3280     ;; is parent/child.
3281     (gnus-message 7 "Making sparse threads...")
3282     (save-excursion
3283       (nnheader-set-temp-buffer " *gnus sparse threads*")
3284       (while (setq header (pop headers))
3285         (when (and (setq references (mail-header-references header))
3286                    (not (string= references "")))
3287           (insert references)
3288           (setq child (mail-header-id header)
3289                 subject (mail-header-subject header)
3290                 date (mail-header-date header)
3291                 generation 0)
3292           (while (search-backward ">" nil t)
3293             (setq end (1+ (point)))
3294             (when (search-backward "<" nil t)
3295               (setq new-child (buffer-substring (point) end))
3296               (push (list (incf generation)
3297                           child (setq child new-child)
3298                           subject date)
3299                     relations)))
3300           (when child
3301             (push (list (1+ generation) child nil subject) relations))
3302           (erase-buffer)))
3303       (kill-buffer (current-buffer)))
3304     ;; Sort over trustworthiness.
3305     (mapcar
3306      (lambda (relation)
3307        (when (gnus-dependencies-add-header
3308               (make-full-mail-header-from-decoded-header
3309                gnus-reffed-article-number
3310                (nth 3 relation) "" (or (nth 4 relation) "")
3311                (nth 1 relation)
3312                (or (nth 2 relation) "") 0 0 "")
3313               gnus-newsgroup-dependencies nil)
3314          (push gnus-reffed-article-number gnus-newsgroup-limit)
3315          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3316          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3317                gnus-newsgroup-reads)
3318          (decf gnus-reffed-article-number)))
3319      (sort relations 'car-less-than-car))
3320     (gnus-message 7 "Making sparse threads...done")))
3321
3322 (defun gnus-build-old-threads ()
3323   ;; Look at all the articles that refer back to old articles, and
3324   ;; fetch the headers for the articles that aren't there.  This will
3325   ;; build complete threads - if the roots haven't been expired by the
3326   ;; server, that is.
3327   (let ((mail-parse-charset gnus-newsgroup-charset)
3328         id heads)
3329     (mapatoms
3330      (lambda (refs)
3331        (when (not (car (symbol-value refs)))
3332          (setq heads (cdr (symbol-value refs)))
3333          (while heads
3334            (if (memq (mail-header-number (caar heads))
3335                      gnus-newsgroup-dormant)
3336                (setq heads (cdr heads))
3337              (setq id (symbol-name refs))
3338              (while (and (setq id (gnus-build-get-header id))
3339                          (not (car (gnus-id-to-thread id)))))
3340              (setq heads nil)))))
3341      gnus-newsgroup-dependencies)))
3342
3343 ;; This function has to be called with point after the article number
3344 ;; on the beginning of the line.
3345 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3346   (let ((eol (gnus-point-at-eol))
3347         (buffer (current-buffer))
3348         header)
3349
3350     ;; overview: [num subject from date id refs chars lines misc]
3351     (unwind-protect
3352         (progn
3353           (narrow-to-region (point) eol)
3354           (unless (eobp)
3355             (forward-char))
3356
3357           (setq header
3358                 (make-full-mail-header
3359                  number                         ; number
3360                  (nnheader-nov-field)           ; subject
3361                  (nnheader-nov-field)           ; from
3362                  (nnheader-nov-field)           ; date
3363                  (nnheader-nov-read-message-id) ; id
3364                  (nnheader-nov-field)           ; refs
3365                  (nnheader-nov-read-integer)    ; chars
3366                  (nnheader-nov-read-integer)    ; lines
3367                  (unless (eobp)
3368                    (if (looking-at "Xref: ")
3369                        (goto-char (match-end 0)))
3370                    (nnheader-nov-field))        ; Xref
3371                  (nnheader-nov-parse-extra))))  ; extra
3372
3373       (widen))
3374
3375     (when gnus-alter-header-function
3376       (funcall gnus-alter-header-function header))
3377     (gnus-dependencies-add-header header dependencies force-new)))
3378
3379 (defun gnus-build-get-header (id)
3380   "Look through the buffer of NOV lines and find the header to ID.
3381 Enter this line into the dependencies hash table, and return
3382 the id of the parent article (if any)."
3383   (let ((deps gnus-newsgroup-dependencies)
3384         found header)
3385     (prog1
3386         (save-excursion
3387           (set-buffer nntp-server-buffer)
3388           (let ((case-fold-search nil))
3389             (goto-char (point-min))
3390             (while (and (not found)
3391                         (search-forward id nil t))
3392               (beginning-of-line)
3393               (setq found (looking-at
3394                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3395                                    (regexp-quote id))))
3396               (or found (beginning-of-line 2)))
3397             (when found
3398               (beginning-of-line)
3399               (and
3400                (setq header (gnus-nov-parse-line
3401                              (read (current-buffer)) deps))
3402                (gnus-parent-id (mail-header-references header))))))
3403       (when header
3404         (let ((number (mail-header-number header)))
3405           (push number gnus-newsgroup-limit)
3406           (push header gnus-newsgroup-headers)
3407           (if (memq number gnus-newsgroup-unselected)
3408               (progn
3409                 (push number gnus-newsgroup-unreads)
3410                 (setq gnus-newsgroup-unselected
3411                       (delq number gnus-newsgroup-unselected)))
3412             (push number gnus-newsgroup-ancient)))))))
3413
3414 (defun gnus-build-all-threads ()
3415   "Read all the headers."
3416   (let ((gnus-summary-ignore-duplicates t)
3417         (mail-parse-charset gnus-newsgroup-charset)
3418         (dependencies gnus-newsgroup-dependencies)
3419         header article)
3420     (save-excursion
3421       (set-buffer nntp-server-buffer)
3422       (let ((case-fold-search nil))
3423         (goto-char (point-min))
3424         (while (not (eobp))
3425           (ignore-errors
3426             (setq article (read (current-buffer))
3427                   header (gnus-nov-parse-line article dependencies)))
3428           (when header
3429             (save-excursion
3430               (set-buffer gnus-summary-buffer)
3431               (push header gnus-newsgroup-headers)
3432               (if (memq (setq article (mail-header-number header))
3433                         gnus-newsgroup-unselected)
3434                   (progn
3435                     (push article gnus-newsgroup-unreads)
3436                     (setq gnus-newsgroup-unselected
3437                           (delq article gnus-newsgroup-unselected)))
3438                 (push article gnus-newsgroup-ancient)))
3439             (forward-line 1)))))))
3440
3441 (defun gnus-summary-update-article-line (article header)
3442   "Update the line for ARTICLE using HEADERS."
3443   (let* ((id (mail-header-id header))
3444          (thread (gnus-id-to-thread id)))
3445     (unless thread
3446       (error "Article in no thread"))
3447     ;; Update the thread.
3448     (setcar thread header)
3449     (gnus-summary-goto-subject article)
3450     (let* ((datal (gnus-data-find-list article))
3451            (data (car datal))
3452            (length (when (cdr datal)
3453                      (- (gnus-data-pos data)
3454                         (gnus-data-pos (cadr datal)))))
3455            (buffer-read-only nil)
3456            (level (gnus-summary-thread-level)))
3457       (gnus-delete-line)
3458       (gnus-summary-insert-line
3459        header level nil (gnus-article-mark article)
3460        (memq article gnus-newsgroup-replied)
3461        (memq article gnus-newsgroup-expirable)
3462        ;; Only insert the Subject string when it's different
3463        ;; from the previous Subject string.
3464        (if (and
3465             gnus-show-threads
3466             (gnus-subject-equal
3467              (condition-case ()
3468                  (mail-header-subject
3469                   (gnus-data-header
3470                    (cadr
3471                     (gnus-data-find-list
3472                      article
3473                      (gnus-data-list t)))))
3474                ;; Error on the side of excessive subjects.
3475                (error ""))
3476              (mail-header-subject header)))
3477            ""
3478          (mail-header-subject header))
3479        nil (cdr (assq article gnus-newsgroup-scored))
3480        (memq article gnus-newsgroup-processable))
3481       (when length
3482         (gnus-data-update-list
3483          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3484
3485 (defun gnus-summary-update-article (article &optional iheader)
3486   "Update ARTICLE in the summary buffer."
3487   (set-buffer gnus-summary-buffer)
3488   (let* ((header (gnus-summary-article-header article))
3489          (id (mail-header-id header))
3490          (data (gnus-data-find article))
3491          (thread (gnus-id-to-thread id))
3492          (references (mail-header-references header))
3493          (parent
3494           (gnus-id-to-thread
3495            (or (gnus-parent-id
3496                 (when (and references
3497                            (not (equal "" references)))
3498                   references))
3499                "none")))
3500          (buffer-read-only nil)
3501          (old (car thread)))
3502     (when thread
3503       (unless iheader
3504         (setcar thread nil)
3505         (when parent
3506           (delq thread parent)))
3507       (if (gnus-summary-insert-subject id header)
3508           ;; Set the (possibly) new article number in the data structure.
3509           (gnus-data-set-number data (gnus-id-to-article id))
3510         (setcar thread old)
3511         nil))))
3512
3513 (defun gnus-rebuild-thread (id &optional line)
3514   "Rebuild the thread containing ID.
3515 If LINE, insert the rebuilt thread starting on line LINE."
3516   (let ((buffer-read-only nil)
3517         old-pos current thread data)
3518     (if (not gnus-show-threads)
3519         (setq thread (list (car (gnus-id-to-thread id))))
3520       ;; Get the thread this article is part of.
3521       (setq thread (gnus-remove-thread id)))
3522     (setq old-pos (gnus-point-at-bol))
3523     (setq current (save-excursion
3524                     (and (zerop (forward-line -1))
3525                          (gnus-summary-article-number))))
3526     ;; If this is a gathered thread, we have to go some re-gathering.
3527     (when (stringp (car thread))
3528       (let ((subject (car thread))
3529             roots thr)
3530         (setq thread (cdr thread))
3531         (while thread
3532           (unless (memq (setq thr (gnus-id-to-thread
3533                                    (gnus-root-id
3534                                     (mail-header-id (caar thread)))))
3535                         roots)
3536             (push thr roots))
3537           (setq thread (cdr thread)))
3538         ;; We now have all (unique) roots.
3539         (if (= (length roots) 1)
3540             ;; All the loose roots are now one solid root.
3541             (setq thread (car roots))
3542           (setq thread (cons subject (gnus-sort-threads roots))))))
3543     (let (threads)
3544       ;; We then insert this thread into the summary buffer.
3545       (when line
3546         (goto-char (point-min))
3547         (forward-line (1- line)))
3548       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3549         (if gnus-show-threads
3550             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3551           (gnus-summary-prepare-unthreaded thread))
3552         (setq data (nreverse gnus-newsgroup-data))
3553         (setq threads gnus-newsgroup-threads))
3554       ;; We splice the new data into the data structure.
3555       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3556       ;;!!! then we want to insert at the beginning of the buffer.
3557       ;;!!! That happens to be true with Gnus now, but that may
3558       ;;!!! change in the future.  Perhaps.
3559       (gnus-data-enter-list
3560        (if line nil current) data (- (point) old-pos))
3561       (setq gnus-newsgroup-threads
3562             (nconc threads gnus-newsgroup-threads))
3563       (gnus-data-compute-positions))))
3564
3565 (defun gnus-number-to-header (number)
3566   "Return the header for article NUMBER."
3567   (let ((headers gnus-newsgroup-headers))
3568     (while (and headers
3569                 (not (= number (mail-header-number (car headers)))))
3570       (pop headers))
3571     (when headers
3572       (car headers))))
3573
3574 (defun gnus-parent-headers (in-headers &optional generation)
3575   "Return the headers of the GENERATIONeth parent of HEADERS."
3576   (unless generation
3577     (setq generation 1))
3578   (let ((parent t)
3579         (headers in-headers)
3580         references)
3581     (while (and parent
3582                 (not (zerop generation))
3583                 (setq references (mail-header-references headers)))
3584       (setq headers (if (and references
3585                              (setq parent (gnus-parent-id references)))
3586                         (car (gnus-id-to-thread parent))
3587                       nil))
3588       (decf generation))
3589     (and (not (eq headers in-headers))
3590          headers)))
3591
3592 (defun gnus-id-to-thread (id)
3593   "Return the (sub-)thread where ID appears."
3594   (gnus-gethash id gnus-newsgroup-dependencies))
3595
3596 (defun gnus-id-to-article (id)
3597   "Return the article number of ID."
3598   (let ((thread (gnus-id-to-thread id)))
3599     (when (and thread
3600                (car thread))
3601       (mail-header-number (car thread)))))
3602
3603 (defun gnus-id-to-header (id)
3604   "Return the article headers of ID."
3605   (car (gnus-id-to-thread id)))
3606
3607 (defun gnus-article-displayed-root-p (article)
3608   "Say whether ARTICLE is a root(ish) article."
3609   (let ((level (gnus-summary-thread-level article))
3610         (refs (mail-header-references  (gnus-summary-article-header article)))
3611         particle)
3612     (cond
3613      ((null level) nil)
3614      ((zerop level) t)
3615      ((null refs) t)
3616      ((null (gnus-parent-id refs)) t)
3617      ((and (= 1 level)
3618            (null (setq particle (gnus-id-to-article
3619                                  (gnus-parent-id refs))))
3620            (null (gnus-summary-thread-level particle)))))))
3621
3622 (defun gnus-root-id (id)
3623   "Return the id of the root of the thread where ID appears."
3624   (let (last-id prev)
3625     (while (and id (setq prev (car (gnus-id-to-thread id))))
3626       (setq last-id id
3627             id (gnus-parent-id (mail-header-references prev))))
3628     last-id))
3629
3630 (defun gnus-articles-in-thread (thread)
3631   "Return the list of articles in THREAD."
3632   (cons (mail-header-number (car thread))
3633         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3634
3635 (defun gnus-remove-thread (id &optional dont-remove)
3636   "Remove the thread that has ID in it."
3637   (let (headers thread last-id)
3638     ;; First go up in this thread until we find the root.
3639     (setq last-id (gnus-root-id id)
3640           headers (message-flatten-list (gnus-id-to-thread last-id)))
3641     ;; We have now found the real root of this thread.  It might have
3642     ;; been gathered into some loose thread, so we have to search
3643     ;; through the threads to find the thread we wanted.
3644     (let ((threads gnus-newsgroup-threads)
3645           sub)
3646       (while threads
3647         (setq sub (car threads))
3648         (if (stringp (car sub))
3649             ;; This is a gathered thread, so we look at the roots
3650             ;; below it to find whether this article is in this
3651             ;; gathered root.
3652             (progn
3653               (setq sub (cdr sub))
3654               (while sub
3655                 (when (member (caar sub) headers)
3656                   (setq thread (car threads)
3657                         threads nil
3658                         sub nil))
3659                 (setq sub (cdr sub))))
3660           ;; It's an ordinary thread, so we check it.
3661           (when (eq (car sub) (car headers))
3662             (setq thread sub
3663                   threads nil)))
3664         (setq threads (cdr threads)))
3665       ;; If this article is in no thread, then it's a root.
3666       (if thread
3667           (unless dont-remove
3668             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3669         (setq thread (gnus-id-to-thread last-id)))
3670       (when thread
3671         (prog1
3672             thread                      ; We return this thread.
3673           (unless dont-remove
3674             (if (stringp (car thread))
3675                 (progn
3676                   ;; If we use dummy roots, then we have to remove the
3677                   ;; dummy root as well.
3678                   (when (eq gnus-summary-make-false-root 'dummy)
3679                     ;; We go to the dummy root by going to
3680                     ;; the first sub-"thread", and then one line up.
3681                     (gnus-summary-goto-article
3682                      (mail-header-number (caadr thread)))
3683                     (forward-line -1)
3684                     (gnus-delete-line)
3685                     (gnus-data-compute-positions))
3686                   (setq thread (cdr thread))
3687                   (while thread
3688                     (gnus-remove-thread-1 (car thread))
3689                     (setq thread (cdr thread))))
3690               (gnus-remove-thread-1 thread))))))))
3691
3692 (defun gnus-remove-thread-1 (thread)
3693   "Remove the thread THREAD recursively."
3694   (let ((number (mail-header-number (pop thread)))
3695         d)
3696     (setq thread (reverse thread))
3697     (while thread
3698       (gnus-remove-thread-1 (pop thread)))
3699     (when (setq d (gnus-data-find number))
3700       (goto-char (gnus-data-pos d))
3701       (gnus-summary-show-thread)
3702       (gnus-data-remove
3703        number
3704        (- (gnus-point-at-bol)
3705           (prog1
3706               (1+ (gnus-point-at-eol))
3707             (gnus-delete-line)))))))
3708
3709 (defun gnus-sort-threads-1 (threads func)
3710   (sort (mapcar (lambda (thread)
3711                   (cons (car thread)
3712                         (and (cdr thread)
3713                              (gnus-sort-threads-1 (cdr thread) func))))
3714                 threads) func))
3715
3716 (defun gnus-sort-threads (threads)
3717   "Sort THREADS."
3718   (if (not gnus-thread-sort-functions)
3719       threads
3720     (gnus-message 8 "Sorting threads...")
3721     (prog1
3722         (gnus-sort-threads-1 
3723          threads 
3724          (gnus-make-sort-function gnus-thread-sort-functions))
3725       (gnus-message 8 "Sorting threads...done"))))
3726
3727 (defun gnus-sort-articles (articles)
3728   "Sort ARTICLES."
3729   (when gnus-article-sort-functions
3730     (gnus-message 7 "Sorting articles...")
3731     (prog1
3732         (setq gnus-newsgroup-headers
3733               (sort articles (gnus-make-sort-function
3734                               gnus-article-sort-functions)))
3735       (gnus-message 7 "Sorting articles...done"))))
3736
3737 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3738 (defmacro gnus-thread-header (thread)
3739   "Return header of first article in THREAD.
3740 Note that THREAD must never, ever be anything else than a variable -
3741 using some other form will lead to serious barfage."
3742   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3743   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3744   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3745         (vector thread) 2))
3746
3747 (defsubst gnus-article-sort-by-number (h1 h2)
3748   "Sort articles by article number."
3749   (< (mail-header-number h1)
3750      (mail-header-number h2)))
3751
3752 (defun gnus-thread-sort-by-number (h1 h2)
3753   "Sort threads by root article number."
3754   (gnus-article-sort-by-number
3755    (gnus-thread-header h1) (gnus-thread-header h2)))
3756
3757 (defsubst gnus-article-sort-by-lines (h1 h2)
3758   "Sort articles by article Lines header."
3759   (< (mail-header-lines h1)
3760      (mail-header-lines h2)))
3761
3762 (defun gnus-thread-sort-by-lines (h1 h2)
3763   "Sort threads by root article Lines header."
3764   (gnus-article-sort-by-lines
3765    (gnus-thread-header h1) (gnus-thread-header h2)))
3766
3767 (defsubst gnus-article-sort-by-chars (h1 h2)
3768   "Sort articles by octet length."
3769   (< (mail-header-chars h1)
3770      (mail-header-chars h2)))
3771
3772 (defun gnus-thread-sort-by-chars (h1 h2)
3773   "Sort threads by root article octet length."
3774   (gnus-article-sort-by-chars
3775    (gnus-thread-header h1) (gnus-thread-header h2)))
3776
3777 (defsubst gnus-article-sort-by-author (h1 h2)
3778   "Sort articles by root author."
3779   (string-lessp
3780    (let ((addr (car (mime-entity-read-field h1 'From))))
3781      (or (std11-full-name-string addr)
3782          (std11-address-string addr)
3783          ""))
3784    (let ((addr (car (mime-entity-read-field h2 'From))))
3785      (or (std11-full-name-string addr)
3786          (std11-address-string addr)
3787          ""))
3788    ))
3789
3790 (defun gnus-thread-sort-by-author (h1 h2)
3791   "Sort threads by root author."
3792   (gnus-article-sort-by-author
3793    (gnus-thread-header h1)  (gnus-thread-header h2)))
3794
3795 (defsubst gnus-article-sort-by-subject (h1 h2)
3796   "Sort articles by root subject."
3797   (string-lessp
3798    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3799    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3800
3801 (defun gnus-thread-sort-by-subject (h1 h2)
3802   "Sort threads by root subject."
3803   (gnus-article-sort-by-subject
3804    (gnus-thread-header h1) (gnus-thread-header h2)))
3805
3806 (defsubst gnus-article-sort-by-date (h1 h2)
3807   "Sort articles by root article date."
3808   (time-less-p
3809    (gnus-date-get-time (mail-header-date h1))
3810    (gnus-date-get-time (mail-header-date h2))))
3811
3812 (defun gnus-thread-sort-by-date (h1 h2)
3813   "Sort threads by root article date."
3814   (gnus-article-sort-by-date
3815    (gnus-thread-header h1) (gnus-thread-header h2)))
3816
3817 (defsubst gnus-article-sort-by-score (h1 h2)
3818   "Sort articles by root article score.
3819 Unscored articles will be counted as having a score of zero."
3820   (> (or (cdr (assq (mail-header-number h1)
3821                     gnus-newsgroup-scored))
3822          gnus-summary-default-score 0)
3823      (or (cdr (assq (mail-header-number h2)
3824                     gnus-newsgroup-scored))
3825          gnus-summary-default-score 0)))
3826
3827 (defun gnus-thread-sort-by-score (h1 h2)
3828   "Sort threads by root article score."
3829   (gnus-article-sort-by-score
3830    (gnus-thread-header h1) (gnus-thread-header h2)))
3831
3832 (defun gnus-thread-sort-by-total-score (h1 h2)
3833   "Sort threads by the sum of all scores in the thread.
3834 Unscored articles will be counted as having a score of zero."
3835   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3836
3837 (defun gnus-thread-total-score (thread)
3838   ;; This function find the total score of THREAD.
3839   (cond ((null thread)
3840          0)
3841         ((consp thread)
3842          (if (stringp (car thread))
3843              (apply gnus-thread-score-function 0
3844                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3845            (gnus-thread-total-score-1 thread)))
3846         (t
3847          (gnus-thread-total-score-1 (list thread)))))
3848
3849 (defun gnus-thread-total-score-1 (root)
3850   ;; This function find the total score of the thread below ROOT.
3851   (setq root (car root))
3852   (apply gnus-thread-score-function
3853          (or (append
3854               (mapcar 'gnus-thread-total-score
3855                       (cdr (gnus-id-to-thread (mail-header-id root))))
3856               (when (> (mail-header-number root) 0)
3857                 (list (or (cdr (assq (mail-header-number root)
3858                                      gnus-newsgroup-scored))
3859                           gnus-summary-default-score 0))))
3860              (list gnus-summary-default-score)
3861              '(0))))
3862
3863 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3864 (defvar gnus-tmp-prev-subject nil)
3865 (defvar gnus-tmp-false-parent nil)
3866 (defvar gnus-tmp-root-expunged nil)
3867 (defvar gnus-tmp-dummy-line nil)
3868
3869 (defvar gnus-tmp-header)
3870 (defun gnus-extra-header (type &optional header)
3871   "Return the extra header of TYPE."
3872   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3873       ""))
3874
3875 (defun gnus-summary-prepare-threads (threads)
3876   "Prepare summary buffer from THREADS and indentation LEVEL.
3877 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3878 or a straight list of headers."
3879   (gnus-message 7 "Generating summary...")
3880
3881   (setq gnus-newsgroup-threads threads)
3882   (beginning-of-line)
3883
3884   (let ((gnus-tmp-level 0)
3885         (default-score (or gnus-summary-default-score 0))
3886         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3887         thread number subject stack state gnus-tmp-gathered beg-match
3888         new-roots gnus-tmp-new-adopts thread-end
3889         gnus-tmp-header gnus-tmp-unread
3890         gnus-tmp-replied gnus-tmp-subject-or-nil
3891         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3892         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3893         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3894
3895     (setq gnus-tmp-prev-subject nil)
3896
3897     (if (vectorp (car threads))
3898         ;; If this is a straight (sic) list of headers, then a
3899         ;; threaded summary display isn't required, so we just create
3900         ;; an unthreaded one.
3901         (gnus-summary-prepare-unthreaded threads)
3902
3903       ;; Do the threaded display.
3904
3905       (while (or threads stack gnus-tmp-new-adopts new-roots)
3906
3907         (if (and (= gnus-tmp-level 0)
3908                  (or (not stack)
3909                      (= (caar stack) 0))
3910                  (not gnus-tmp-false-parent)
3911                  (or gnus-tmp-new-adopts new-roots))
3912             (if gnus-tmp-new-adopts
3913                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3914                       thread (list (car gnus-tmp-new-adopts))
3915                       gnus-tmp-header (caar thread)
3916                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3917               (when new-roots
3918                 (setq thread (list (car new-roots))
3919                       gnus-tmp-header (caar thread)
3920                       new-roots (cdr new-roots))))
3921
3922           (if threads
3923               ;; If there are some threads, we do them before the
3924               ;; threads on the stack.
3925               (setq thread threads
3926                     gnus-tmp-header (caar thread))
3927             ;; There were no current threads, so we pop something off
3928             ;; the stack.
3929             (setq state (car stack)
3930                   gnus-tmp-level (car state)
3931                   thread (cdr state)
3932                   stack (cdr stack)
3933                   gnus-tmp-header (caar thread))))
3934
3935         (setq gnus-tmp-false-parent nil)
3936         (setq gnus-tmp-root-expunged nil)
3937         (setq thread-end nil)
3938
3939         (if (stringp gnus-tmp-header)
3940             ;; The header is a dummy root.
3941             (cond
3942              ((eq gnus-summary-make-false-root 'adopt)
3943               ;; We let the first article adopt the rest.
3944               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3945                                                (cddar thread)))
3946               (setq gnus-tmp-gathered
3947                     (nconc (mapcar
3948                             (lambda (h) (mail-header-number (car h)))
3949                             (cddar thread))
3950                            gnus-tmp-gathered))
3951               (setq thread (cons (list (caar thread)
3952                                        (cadar thread))
3953                                  (cdr thread)))
3954               (setq gnus-tmp-level -1
3955                     gnus-tmp-false-parent t))
3956              ((eq gnus-summary-make-false-root 'empty)
3957               ;; We print adopted articles with empty subject fields.
3958               (setq gnus-tmp-gathered
3959                     (nconc (mapcar
3960                             (lambda (h) (mail-header-number (car h)))
3961                             (cddar thread))
3962                            gnus-tmp-gathered))
3963               (setq gnus-tmp-level -1))
3964              ((eq gnus-summary-make-false-root 'dummy)
3965               ;; We remember that we probably want to output a dummy
3966               ;; root.
3967               (setq gnus-tmp-dummy-line gnus-tmp-header)
3968               (setq gnus-tmp-prev-subject gnus-tmp-header))
3969              (t
3970               ;; We do not make a root for the gathered
3971               ;; sub-threads at all.
3972               (setq gnus-tmp-level -1)))
3973
3974           (setq number (mail-header-number gnus-tmp-header)
3975                 subject (mail-header-subject gnus-tmp-header))
3976
3977           (cond
3978            ;; If the thread has changed subject, we might want to make
3979            ;; this subthread into a root.
3980            ((and (null gnus-thread-ignore-subject)
3981                  (not (zerop gnus-tmp-level))
3982                  gnus-tmp-prev-subject
3983                  (not (inline
3984                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3985             (setq new-roots (nconc new-roots (list (car thread)))
3986                   thread-end t
3987                   gnus-tmp-header nil))
3988            ;; If the article lies outside the current limit,
3989            ;; then we do not display it.
3990            ((not (memq number gnus-newsgroup-limit))
3991             (setq gnus-tmp-gathered
3992                   (nconc (mapcar
3993                           (lambda (h) (mail-header-number (car h)))
3994                           (cdar thread))
3995                          gnus-tmp-gathered))
3996             (setq gnus-tmp-new-adopts (if (cdar thread)
3997                                           (append gnus-tmp-new-adopts
3998                                                   (cdar thread))
3999                                         gnus-tmp-new-adopts)
4000                   thread-end t
4001                   gnus-tmp-header nil)
4002             (when (zerop gnus-tmp-level)
4003               (setq gnus-tmp-root-expunged t)))
4004            ;; Perhaps this article is to be marked as read?
4005            ((and gnus-summary-mark-below
4006                  (< (or (cdr (assq number gnus-newsgroup-scored))
4007                         default-score)
4008                     gnus-summary-mark-below)
4009                  ;; Don't touch sparse articles.
4010                  (not (gnus-summary-article-sparse-p number))
4011                  (not (gnus-summary-article-ancient-p number)))
4012             (setq gnus-newsgroup-unreads
4013                   (delq number gnus-newsgroup-unreads))
4014             (if gnus-newsgroup-auto-expire
4015                 (push number gnus-newsgroup-expirable)
4016               (push (cons number gnus-low-score-mark)
4017                     gnus-newsgroup-reads))))
4018
4019           (when gnus-tmp-header
4020             ;; We may have an old dummy line to output before this
4021             ;; article.
4022             (when (and gnus-tmp-dummy-line
4023                        (gnus-subject-equal
4024                         gnus-tmp-dummy-line
4025                         (mail-header-subject gnus-tmp-header)))
4026               (gnus-summary-insert-dummy-line
4027                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4028               (setq gnus-tmp-dummy-line nil))
4029
4030             ;; Compute the mark.
4031             (setq gnus-tmp-unread (gnus-article-mark number))
4032
4033             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4034                                   gnus-tmp-header gnus-tmp-level)
4035                   gnus-newsgroup-data)
4036
4037             ;; Actually insert the line.
4038             (setq
4039              gnus-tmp-subject-or-nil
4040              (cond
4041               ((and gnus-thread-ignore-subject
4042                     gnus-tmp-prev-subject
4043                     (not (inline (gnus-subject-equal
4044                                   gnus-tmp-prev-subject subject))))
4045                subject)
4046               ((zerop gnus-tmp-level)
4047                (if (and (eq gnus-summary-make-false-root 'empty)
4048                         (memq number gnus-tmp-gathered)
4049                         gnus-tmp-prev-subject
4050                         (inline (gnus-subject-equal
4051                                  gnus-tmp-prev-subject subject)))
4052                    gnus-summary-same-subject
4053                  subject))
4054               (t gnus-summary-same-subject)))
4055             (if (and (eq gnus-summary-make-false-root 'adopt)
4056                      (= gnus-tmp-level 1)
4057                      (memq number gnus-tmp-gathered))
4058                 (setq gnus-tmp-opening-bracket ?\<
4059                       gnus-tmp-closing-bracket ?\>)
4060               (setq gnus-tmp-opening-bracket ?\[
4061                     gnus-tmp-closing-bracket ?\]))
4062             (setq
4063              gnus-tmp-indentation
4064              (aref gnus-thread-indent-array gnus-tmp-level)
4065              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4066              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4067                                 gnus-summary-default-score 0)
4068              gnus-tmp-score-char
4069              (if (or (null gnus-summary-default-score)
4070                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4071                          gnus-summary-zcore-fuzz))
4072                  ?  ;Whitespace
4073                (if (< gnus-tmp-score gnus-summary-default-score)
4074                    gnus-score-below-mark gnus-score-over-mark))
4075              gnus-tmp-replied
4076              (cond ((memq number gnus-newsgroup-processable)
4077                     gnus-process-mark)
4078                    ((memq number gnus-newsgroup-cached)
4079                     gnus-cached-mark)
4080                    ((memq number gnus-newsgroup-replied)
4081                     gnus-replied-mark)
4082                    ((memq number gnus-newsgroup-saved)
4083                     gnus-saved-mark)
4084                    (t gnus-unread-mark))
4085              gnus-tmp-from (mail-header-from gnus-tmp-header)
4086              gnus-tmp-name
4087              (cond
4088               ((string-match "<[^>]+> *$" gnus-tmp-from)
4089                (setq beg-match (match-beginning 0))
4090                (or (and (string-match "^\".+\"" gnus-tmp-from)
4091                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4092                    (substring gnus-tmp-from 0 beg-match)))
4093               ((string-match "(.+)" gnus-tmp-from)
4094                (substring gnus-tmp-from
4095                           (1+ (match-beginning 0)) (1- (match-end 0))))
4096               (t gnus-tmp-from)))
4097             (when (string= gnus-tmp-name "")
4098               (setq gnus-tmp-name gnus-tmp-from))
4099             (unless (numberp gnus-tmp-lines)
4100               (setq gnus-tmp-lines 0))
4101             (gnus-put-text-property
4102              (point)
4103              (progn (eval gnus-summary-line-format-spec) (point))
4104              'gnus-number number)
4105             (when gnus-visual-p
4106               (forward-line -1)
4107               (gnus-run-hooks 'gnus-summary-update-hook)
4108               (forward-line 1))
4109
4110             (setq gnus-tmp-prev-subject subject)))
4111
4112         (when (nth 1 thread)
4113           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4114         (incf gnus-tmp-level)
4115         (setq threads (if thread-end nil (cdar thread)))
4116         (unless threads
4117           (setq gnus-tmp-level 0)))))
4118   (gnus-message 7 "Generating summary...done"))
4119
4120 (defun gnus-summary-prepare-unthreaded (headers)
4121   "Generate an unthreaded summary buffer based on HEADERS."
4122   (let (header number mark)
4123
4124     (beginning-of-line)
4125
4126     (while headers
4127       ;; We may have to root out some bad articles...
4128       (when (memq (setq number (mail-header-number
4129                                 (setq header (pop headers))))
4130                   gnus-newsgroup-limit)
4131         ;; Mark article as read when it has a low score.
4132         (when (and gnus-summary-mark-below
4133                    (< (or (cdr (assq number gnus-newsgroup-scored))
4134                           gnus-summary-default-score 0)
4135                       gnus-summary-mark-below)
4136                    (not (gnus-summary-article-ancient-p number)))
4137           (setq gnus-newsgroup-unreads
4138                 (delq number gnus-newsgroup-unreads))
4139           (if gnus-newsgroup-auto-expire
4140               (push number gnus-newsgroup-expirable)
4141             (push (cons number gnus-low-score-mark)
4142                   gnus-newsgroup-reads)))
4143
4144         (setq mark (gnus-article-mark number))
4145         (push (gnus-data-make number mark (1+ (point)) header 0)
4146               gnus-newsgroup-data)
4147         (gnus-summary-insert-line
4148          header 0 number
4149          mark (memq number gnus-newsgroup-replied)
4150          (memq number gnus-newsgroup-expirable)
4151          (mail-header-subject header) nil
4152          (cdr (assq number gnus-newsgroup-scored))
4153          (memq number gnus-newsgroup-processable))))))
4154
4155 (defun gnus-summary-remove-list-identifiers ()
4156   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4157   (let ((regexp (if (stringp gnus-list-identifiers)
4158                     gnus-list-identifiers
4159                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4160     (dolist (header gnus-newsgroup-headers)
4161       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4162                                   " *\\)\\)+\\(Re: +\\)?\\)")
4163                           (mail-header-subject header))
4164         (mail-header-set-subject
4165          header (concat (substring (mail-header-subject header)
4166                                    0 (match-beginning 1))
4167                         (or
4168                          (match-string 3 (mail-header-subject header))
4169                          (match-string 5 (mail-header-subject header)))
4170                         (substring (mail-header-subject header)
4171                                    (match-end 1))))))))
4172
4173 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4174   "Select newsgroup GROUP.
4175 If READ-ALL is non-nil, all articles in the group are selected.
4176 If SELECT-ARTICLES, only select those articles from GROUP."
4177   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4178          ;;!!! Dirty hack; should be removed.
4179          (gnus-summary-ignore-duplicates
4180           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4181               t
4182             gnus-summary-ignore-duplicates))
4183          (info (nth 2 entry))
4184          articles fetched-articles cached)
4185
4186     (unless (gnus-check-server
4187              (setq gnus-current-select-method
4188                    (gnus-find-method-for-group group)))
4189       (error "Couldn't open server"))
4190
4191     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4192         (gnus-activate-group group)     ; Or we can activate it...
4193         (progn                          ; Or we bug out.
4194           (when (equal major-mode 'gnus-summary-mode)
4195             (kill-buffer (current-buffer)))
4196           (error "Couldn't request group %s: %s"
4197                  group (gnus-status-message group))))
4198
4199     (unless (gnus-request-group group t)
4200       (when (equal major-mode 'gnus-summary-mode)
4201         (kill-buffer (current-buffer)))
4202       (error "Couldn't request group %s: %s"
4203              group (gnus-status-message group)))
4204
4205     (setq gnus-newsgroup-name group)
4206     (setq gnus-newsgroup-unselected nil)
4207     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4208     (gnus-summary-setup-default-charset)
4209
4210     ;; Adjust and set lists of article marks.
4211     (when info
4212       (gnus-adjust-marked-articles info))
4213
4214     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4215     (setq cached
4216           (if (gnus-virtual-group-p group)
4217               gnus-newsgroup-cached
4218             (gnus-cache-articles-in-group group)))
4219
4220     (setq gnus-newsgroup-unreads
4221           (gnus-set-difference
4222            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4223            gnus-newsgroup-dormant))
4224
4225     (setq gnus-newsgroup-processable nil)
4226
4227     (gnus-update-read-articles group gnus-newsgroup-unreads)
4228
4229     (if (setq articles select-articles)
4230         (setq gnus-newsgroup-unselected
4231               (gnus-sorted-intersection
4232                gnus-newsgroup-unreads
4233                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4234       (setq articles (gnus-articles-to-read group read-all)))
4235
4236     (cond
4237      ((null articles)
4238       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4239       'quit)
4240      ((eq articles 0) nil)
4241      (t
4242       ;; Init the dependencies hash table.
4243       (setq gnus-newsgroup-dependencies
4244             (gnus-make-hashtable (length articles)))
4245       (gnus-set-global-variables)
4246       ;; Retrieve the headers and read them in.
4247       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4248       (setq gnus-newsgroup-headers
4249             (gnus-retrieve-parsed-headers
4250              articles gnus-newsgroup-name
4251              ;; We might want to fetch old headers, but
4252              ;; not if there is only 1 article.
4253              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4254                            (not (numberp gnus-fetch-old-headers)))
4255                       (> (length articles) 1))
4256                   gnus-fetch-old-headers)))
4257       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4258
4259       ;; Suppress duplicates?
4260       (when gnus-suppress-duplicates
4261         (gnus-dup-suppress-articles))
4262
4263       ;; Set the initial limit.
4264       (setq gnus-newsgroup-limit (copy-sequence articles))
4265       ;; Remove canceled articles from the list of unread articles.
4266       (setq gnus-newsgroup-unreads
4267             (gnus-set-sorted-intersection
4268              gnus-newsgroup-unreads
4269              (setq fetched-articles
4270                    (mapcar (lambda (headers) (mail-header-number headers))
4271                            gnus-newsgroup-headers))))
4272       ;; Removed marked articles that do not exist.
4273       (gnus-update-missing-marks
4274        (gnus-sorted-complement fetched-articles articles))
4275
4276       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4277       (when cached
4278         (setq gnus-newsgroup-cached cached))
4279
4280       ;; We might want to build some more threads first.
4281       (when (and gnus-fetch-old-headers
4282                  (eq gnus-headers-retrieved-by 'nov))
4283         (if (eq gnus-fetch-old-headers 'invisible)
4284             (gnus-build-all-threads)
4285           (gnus-build-old-threads)))
4286       ;; Let the Gnus agent mark articles as read.
4287       (when gnus-agent
4288         (gnus-agent-get-undownloaded-list))
4289       ;; Remove list identifiers from subject
4290       (when gnus-list-identifiers
4291         (gnus-summary-remove-list-identifiers))
4292       ;; Check whether auto-expire is to be done in this group.
4293       (setq gnus-newsgroup-auto-expire
4294             (gnus-group-auto-expirable-p group))
4295       ;; Set up the article buffer now, if necessary.
4296       (unless gnus-single-article-buffer
4297         (gnus-article-setup-buffer))
4298       ;; First and last article in this newsgroup.
4299       (when gnus-newsgroup-headers
4300         (setq gnus-newsgroup-begin
4301               (mail-header-number (car gnus-newsgroup-headers))
4302               gnus-newsgroup-end
4303               (mail-header-number
4304                (gnus-last-element gnus-newsgroup-headers))))
4305       ;; GROUP is successfully selected.
4306       (or gnus-newsgroup-headers t)))))
4307
4308 (defun gnus-articles-to-read (group &optional read-all)
4309   "Find out what articles the user wants to read."
4310   (let* ((articles
4311           ;; Select all articles if `read-all' is non-nil, or if there
4312           ;; are no unread articles.
4313           (if (or read-all
4314                   (and (zerop (length gnus-newsgroup-marked))
4315                        (zerop (length gnus-newsgroup-unreads)))
4316                   (eq (gnus-group-find-parameter group 'display)
4317                       'all))
4318               (or
4319                (gnus-uncompress-range (gnus-active group))
4320                (gnus-cache-articles-in-group group))
4321             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4322                           (copy-sequence gnus-newsgroup-unreads))
4323                   '<)))
4324          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4325          (scored (length scored-list))
4326          (number (length articles))
4327          (marked (+ (length gnus-newsgroup-marked)
4328                     (length gnus-newsgroup-dormant)))
4329          (select
4330           (cond
4331            ((numberp read-all)
4332             read-all)
4333            (t
4334             (condition-case ()
4335                 (cond
4336                  ((and (or (<= scored marked) (= scored number))
4337                        (natnump gnus-large-newsgroup)
4338                        (> number gnus-large-newsgroup))
4339                   (let* ((cursor-in-echo-area nil)
4340                          (input (read-from-minibuffer
4341                                  (format
4342                                   "How many articles from %s (max %d): "
4343                                   (gnus-limit-string gnus-newsgroup-name 35)
4344                                   number)
4345                                  (cons (number-to-string gnus-large-newsgroup)
4346                                        0))))
4347                     (if (string-match "^[ \t]*$" input)
4348                         number
4349                       input)))
4350                  ((and (> scored marked) (< scored number)
4351                        (> (- scored number) 20))
4352                   (let ((input
4353                          (read-string
4354                           (format "%s %s (%d scored, %d total): "
4355                                   "How many articles from"
4356                                   group scored number))))
4357                     (if (string-match "^[ \t]*$" input)
4358                         number input)))
4359                  (t number))
4360               (quit
4361                (message "Quit getting the articles to read")
4362                nil))))))
4363     (setq select (if (stringp select) (string-to-number select) select))
4364     (if (or (null select) (zerop select))
4365         select
4366       (if (and (not (zerop scored)) (<= (abs select) scored))
4367           (progn
4368             (setq articles (sort scored-list '<))
4369             (setq number (length articles)))
4370         (setq articles (copy-sequence articles)))
4371
4372       (when (< (abs select) number)
4373         (if (< select 0)
4374             ;; Select the N oldest articles.
4375             (setcdr (nthcdr (1- (abs select)) articles) nil)
4376           ;; Select the N most recent articles.
4377           (setq articles (nthcdr (- number select) articles))))
4378       (setq gnus-newsgroup-unselected
4379             (gnus-sorted-intersection
4380              gnus-newsgroup-unreads
4381              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4382       (when gnus-alter-articles-to-read-function
4383         (setq gnus-newsgroup-unreads
4384               (sort
4385                (funcall gnus-alter-articles-to-read-function
4386                         gnus-newsgroup-name gnus-newsgroup-unreads)
4387                '<)))
4388       articles)))
4389
4390 (defun gnus-killed-articles (killed articles)
4391   (let (out)
4392     (while articles
4393       (when (inline (gnus-member-of-range (car articles) killed))
4394         (push (car articles) out))
4395       (setq articles (cdr articles)))
4396     out))
4397
4398 (defun gnus-uncompress-marks (marks)
4399   "Uncompress the mark ranges in MARKS."
4400   (let ((uncompressed '(score bookmark))
4401         out)
4402     (while marks
4403       (if (memq (caar marks) uncompressed)
4404           (push (car marks) out)
4405         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4406       (setq marks (cdr marks)))
4407     out))
4408
4409 (defun gnus-adjust-marked-articles (info)
4410   "Set all article lists and remove all marks that are no longer valid."
4411   (let* ((marked-lists (gnus-info-marks info))
4412          (active (gnus-active (gnus-info-group info)))
4413          (min (car active))
4414          (max (cdr active))
4415          (types gnus-article-mark-lists)
4416          (uncompressed '(score bookmark killed))
4417          marks var articles article mark)
4418
4419     (while marked-lists
4420       (setq marks (pop marked-lists))
4421       (set (setq var (intern (format "gnus-newsgroup-%s"
4422                                      (car (rassq (setq mark (car marks))
4423                                                  types)))))
4424            (if (memq (car marks) uncompressed) (cdr marks)
4425              (gnus-uncompress-range (cdr marks))))
4426
4427       (setq articles (symbol-value var))
4428
4429       ;; All articles have to be subsets of the active articles.
4430       (cond
4431        ;; Adjust "simple" lists.
4432        ((memq mark '(tick dormant expire reply save))
4433         (while articles
4434           (when (or (< (setq article (pop articles)) min) (> article max))
4435             (set var (delq article (symbol-value var))))))
4436        ;; Adjust assocs.
4437        ((memq mark uncompressed)
4438         (when (not (listp (cdr (symbol-value var))))
4439           (set var (list (symbol-value var))))
4440         (when (not (listp (cdr articles)))
4441           (setq articles (list articles)))
4442         (while articles
4443           (when (or (not (consp (setq article (pop articles))))
4444                     (< (car article) min)
4445                     (> (car article) max))
4446             (set var (delq article (symbol-value var))))))))))
4447
4448 (defun gnus-update-missing-marks (missing)
4449   "Go through the list of MISSING articles and remove them from the mark lists."
4450   (when missing
4451     (let ((types gnus-article-mark-lists)
4452           var m)
4453       ;; Go through all types.
4454       (while types
4455         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4456         (when (symbol-value var)
4457           ;; This list has articles.  So we delete all missing articles
4458           ;; from it.
4459           (setq m missing)
4460           (while m
4461             (set var (delq (pop m) (symbol-value var)))))))))
4462
4463 (defun gnus-update-marks ()
4464   "Enter the various lists of marked articles into the newsgroup info list."
4465   (let ((types gnus-article-mark-lists)
4466         (info (gnus-get-info gnus-newsgroup-name))
4467         (uncompressed '(score bookmark killed))
4468         type list newmarked symbol delta-marks)
4469     (when info
4470       ;; Add all marks lists to the list of marks lists.
4471       (while (setq type (pop types))
4472         (setq list (symbol-value
4473                     (setq symbol
4474                           (intern (format "gnus-newsgroup-%s"
4475                                           (car type))))))
4476
4477         (when list
4478           ;; Get rid of the entries of the articles that have the
4479           ;; default score.
4480           (when (and (eq (cdr type) 'score)
4481                      gnus-save-score
4482                      list)
4483             (let* ((arts list)
4484                    (prev (cons nil list))
4485                    (all prev))
4486               (while arts
4487                 (if (or (not (consp (car arts)))
4488                         (= (cdar arts) gnus-summary-default-score))
4489                     (setcdr prev (cdr arts))
4490                   (setq prev arts))
4491                 (setq arts (cdr arts)))
4492               (setq list (cdr all)))))
4493
4494         (unless (memq (cdr type) uncompressed)
4495           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4496
4497         (when (gnus-check-backend-function
4498                'request-set-mark gnus-newsgroup-name)
4499           ;; propagate flags to server, with the following exceptions:
4500           ;; uncompressed:s are not proper flags (they are cons cells)
4501           ;; cache is a internal gnus flag
4502           ;; download are local to one gnus installation (well)
4503           ;; unsend are for nndraft groups only
4504           ;; xxx: generality of this?  this suits nnimap anyway
4505           (unless (memq (cdr type) (append '(cache download unsend)
4506                                            uncompressed))
4507             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4508                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4509                    (add (gnus-remove-from-range
4510                          (gnus-copy-sequence list) old)))
4511               (when add
4512                 (push (list add 'add (list (cdr type))) delta-marks))
4513               (when del
4514                 (push (list del 'del (list (cdr type))) delta-marks)))))
4515
4516         (when list
4517           (push (cons (cdr type) list) newmarked)))
4518
4519       (when delta-marks
4520         (unless (gnus-check-group gnus-newsgroup-name)
4521           (error "Can't open server for %s" gnus-newsgroup-name))
4522         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4523
4524       ;; Enter these new marks into the info of the group.
4525       (if (nthcdr 3 info)
4526           (setcar (nthcdr 3 info) newmarked)
4527         ;; Add the marks lists to the end of the info.
4528         (when newmarked
4529           (setcdr (nthcdr 2 info) (list newmarked))))
4530
4531       ;; Cut off the end of the info if there's nothing else there.
4532       (let ((i 5))
4533         (while (and (> i 2)
4534                     (not (nth i info)))
4535           (when (nthcdr (decf i) info)
4536             (setcdr (nthcdr i info) nil)))))))
4537
4538 (defun gnus-set-mode-line (where)
4539   "Set the mode line of the article or summary buffers.
4540 If WHERE is `summary', the summary mode line format will be used."
4541   ;; Is this mode line one we keep updated?
4542   (when (and (memq where gnus-updated-mode-lines)
4543              (symbol-value
4544               (intern (format "gnus-%s-mode-line-format-spec" where))))
4545     (let (mode-string)
4546       (save-excursion
4547         ;; We evaluate this in the summary buffer since these
4548         ;; variables are buffer-local to that buffer.
4549         (set-buffer gnus-summary-buffer)
4550         ;; We bind all these variables that are used in the `eval' form
4551         ;; below.
4552         (let* ((mformat (symbol-value
4553                          (intern
4554                           (format "gnus-%s-mode-line-format-spec" where))))
4555                (gnus-tmp-group-name (gnus-group-name-decode
4556                                      gnus-newsgroup-name
4557                                      (gnus-group-name-charset
4558                                       nil
4559                                       gnus-newsgroup-name)))
4560                (gnus-tmp-article-number (or gnus-current-article 0))
4561                (gnus-tmp-unread gnus-newsgroup-unreads)
4562                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4563                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4564                (gnus-tmp-unread-and-unselected
4565                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4566                             (zerop gnus-tmp-unselected))
4567                        "")
4568                       ((zerop gnus-tmp-unselected)
4569                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4570                       (t (format "{%d(+%d) more}"
4571                                  gnus-tmp-unread-and-unticked
4572                                  gnus-tmp-unselected))))
4573                (gnus-tmp-subject
4574                 (if (and gnus-current-headers
4575                          (vectorp gnus-current-headers))
4576                     (gnus-mode-string-quote
4577                      (mail-header-subject gnus-current-headers))
4578                   ""))
4579                bufname-length max-len
4580                gnus-tmp-header);; passed as argument to any user-format-funcs
4581           (setq mode-string (eval mformat))
4582           (setq bufname-length (if (string-match "%b" mode-string)
4583                                    (- (length
4584                                        (buffer-name
4585                                         (if (eq where 'summary)
4586                                             nil
4587                                           (get-buffer gnus-article-buffer))))
4588                                       2)
4589                                  0))
4590           (setq max-len (max 4 (if gnus-mode-non-string-length
4591                                    (- (window-width)
4592                                       gnus-mode-non-string-length
4593                                       bufname-length)
4594                                  (length mode-string))))
4595           ;; We might have to chop a bit of the string off...
4596           (when (> (length mode-string) max-len)
4597             (setq mode-string
4598                   (concat (gnus-truncate-string mode-string (- max-len 3))
4599                           "...")))
4600           ;; Pad the mode string a bit.
4601           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4602       ;; Update the mode line.
4603       (setq mode-line-buffer-identification
4604             (gnus-mode-line-buffer-identification (list mode-string)))
4605       (set-buffer-modified-p t))))
4606
4607 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4608   "Go through the HEADERS list and add all Xrefs to a hash table.
4609 The resulting hash table is returned, or nil if no Xrefs were found."
4610   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4611          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4612          (xref-hashtb (gnus-make-hashtable))
4613          start group entry number xrefs header)
4614     (while headers
4615       (setq header (pop headers))
4616       (when (and (setq xrefs (mail-header-xref header))
4617                  (not (memq (setq number (mail-header-number header))
4618                             unreads)))
4619         (setq start 0)
4620         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4621           (setq start (match-end 0))
4622           (setq group (if prefix
4623                           (concat prefix (substring xrefs (match-beginning 1)
4624                                                     (match-end 1)))
4625                         (substring xrefs (match-beginning 1) (match-end 1))))
4626           (setq number
4627                 (string-to-int (substring xrefs (match-beginning 2)
4628                                           (match-end 2))))
4629           (if (setq entry (gnus-gethash group xref-hashtb))
4630               (setcdr entry (cons number (cdr entry)))
4631             (gnus-sethash group (cons number nil) xref-hashtb)))))
4632     (and start xref-hashtb)))
4633
4634 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4635   "Look through all the headers and mark the Xrefs as read."
4636   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4637         name entry info xref-hashtb idlist method nth4)
4638     (save-excursion
4639       (set-buffer gnus-group-buffer)
4640       (when (setq xref-hashtb
4641                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4642         (mapatoms
4643          (lambda (group)
4644            (unless (string= from-newsgroup (setq name (symbol-name group)))
4645              (setq idlist (symbol-value group))
4646              ;; Dead groups are not updated.
4647              (and (prog1
4648                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4649                             info (nth 2 entry))
4650                     (when (stringp (setq nth4 (gnus-info-method info)))
4651                       (setq nth4 (gnus-server-to-method nth4))))
4652                   ;; Only do the xrefs if the group has the same
4653                   ;; select method as the group we have just read.
4654                   (or (gnus-methods-equal-p
4655                        nth4 (gnus-find-method-for-group from-newsgroup))
4656                       virtual
4657                       (equal nth4 (setq method (gnus-find-method-for-group
4658                                                 from-newsgroup)))
4659                       (and (equal (car nth4) (car method))
4660                            (equal (nth 1 nth4) (nth 1 method))))
4661                   gnus-use-cross-reference
4662                   (or (not (eq gnus-use-cross-reference t))
4663                       virtual
4664                       ;; Only do cross-references on subscribed
4665                       ;; groups, if that is what is wanted.
4666                       (<= (gnus-info-level info) gnus-level-subscribed))
4667                   (gnus-group-make-articles-read name idlist))))
4668          xref-hashtb)))))
4669
4670 (defun gnus-compute-read-articles (group articles)
4671   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4672          (info (nth 2 entry))
4673          (active (gnus-active group))
4674          ninfo)
4675     (when entry
4676       ;; First peel off all invalid article numbers.
4677       (when active
4678         (let ((ids articles)
4679               id first)
4680           (while (setq id (pop ids))
4681             (when (and first (> id (cdr active)))
4682               ;; We'll end up in this situation in one particular
4683               ;; obscure situation.  If you re-scan a group and get
4684               ;; a new article that is cross-posted to a different
4685               ;; group that has not been re-scanned, you might get
4686               ;; crossposted article that has a higher number than
4687               ;; Gnus believes possible.  So we re-activate this
4688               ;; group as well.  This might mean doing the
4689               ;; crossposting thingy will *increase* the number
4690               ;; of articles in some groups.  Tsk, tsk.
4691               (setq active (or (gnus-activate-group group) active)))
4692             (when (or (> id (cdr active))
4693                       (< id (car active)))
4694               (setq articles (delq id articles))))))
4695       ;; If the read list is nil, we init it.
4696       (if (and active
4697                (null (gnus-info-read info))
4698                (> (car active) 1))
4699           (setq ninfo (cons 1 (1- (car active))))
4700         (setq ninfo (gnus-info-read info)))
4701       ;; Then we add the read articles to the range.
4702       (gnus-add-to-range
4703        ninfo (setq articles (sort articles '<))))))
4704
4705 (defun gnus-group-make-articles-read (group articles)
4706   "Update the info of GROUP to say that ARTICLES are read."
4707   (let* ((num 0)
4708          (entry (gnus-gethash group gnus-newsrc-hashtb))
4709          (info (nth 2 entry))
4710          (active (gnus-active group))
4711          range)
4712     (when entry
4713       (setq range (gnus-compute-read-articles group articles))
4714       (save-excursion
4715         (set-buffer gnus-group-buffer)
4716         (gnus-undo-register
4717           `(progn
4718              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4719              (gnus-info-set-read ',info ',(gnus-info-read info))
4720              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4721              (gnus-group-update-group ,group t))))
4722       ;; Add the read articles to the range.
4723       (gnus-info-set-read info range)
4724       ;; Then we have to re-compute how many unread
4725       ;; articles there are in this group.
4726       (when active
4727         (cond
4728          ((not range)
4729           (setq num (- (1+ (cdr active)) (car active))))
4730          ((not (listp (cdr range)))
4731           (setq num (- (cdr active) (- (1+ (cdr range))
4732                                        (car range)))))
4733          (t
4734           (while range
4735             (if (numberp (car range))
4736                 (setq num (1+ num))
4737               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4738             (setq range (cdr range)))
4739           (setq num (- (cdr active) num))))
4740         ;; Update the number of unread articles.
4741         (setcar entry num)
4742         ;; Update the group buffer.
4743         (gnus-group-update-group group t)))))
4744
4745 (defvar gnus-newsgroup-none-id 0)
4746
4747 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4748   (let ((cur nntp-server-buffer)
4749         (dependencies
4750          (or dependencies
4751              (save-excursion (set-buffer gnus-summary-buffer)
4752                              gnus-newsgroup-dependencies)))
4753         headers id end ref
4754         (mail-parse-charset gnus-newsgroup-charset)
4755         (mail-parse-ignored-charsets
4756          (save-excursion (condition-case nil
4757                              (set-buffer gnus-summary-buffer)
4758                            (error))
4759                          gnus-newsgroup-ignored-charsets)))
4760     (save-excursion
4761       (set-buffer nntp-server-buffer)
4762       ;; Translate all TAB characters into SPACE characters.
4763       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4764       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4765       (gnus-run-hooks 'gnus-parse-headers-hook)
4766       (let ((case-fold-search t)
4767             in-reply-to header p lines chars ctype)
4768         (goto-char (point-min))
4769         ;; Search to the beginning of the next header.  Error messages
4770         ;; do not begin with 2 or 3.
4771         (while (re-search-forward "^[23][0-9]+ " nil t)
4772           (setq id nil
4773                 ref nil)
4774           ;; This implementation of this function, with nine
4775           ;; search-forwards instead of the one re-search-forward and
4776           ;; a case (which basically was the old function) is actually
4777           ;; about twice as fast, even though it looks messier.  You
4778           ;; can't have everything, I guess.  Speed and elegance
4779           ;; doesn't always go hand in hand.
4780           (setq
4781            header
4782            (make-full-mail-header
4783             ;; Number.
4784             (prog1
4785                 (read cur)
4786               (end-of-line)
4787               (setq p (point))
4788               (narrow-to-region (point)
4789                                 (or (and (search-forward "\n.\n" nil t)
4790                                          (- (point) 2))
4791                                     (point))))
4792             ;; Subject.
4793             (progn
4794               (goto-char p)
4795               (if (search-forward "\nsubject: " nil t)
4796                   (nnheader-header-value)
4797                 "(none)"))
4798             ;; From.
4799             (progn
4800               (goto-char p)
4801               (if (or (search-forward "\nfrom: " nil t)
4802                       (search-forward "\nfrom:" nil t))
4803                   (nnheader-header-value)
4804                 "(nobody)"))
4805             ;; Date.
4806             (progn
4807               (goto-char p)
4808               (if (search-forward "\ndate: " nil t)
4809                   (nnheader-header-value)
4810                 ""))
4811             ;; Message-ID.
4812             (progn
4813               (goto-char p)
4814               (setq id (if (re-search-forward
4815                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4816                            ;; We do it this way to make sure the Message-ID
4817                            ;; is (somewhat) syntactically valid.
4818                            (buffer-substring (match-beginning 1)
4819                                              (match-end 1))
4820                          ;; If there was no message-id, we just fake one
4821                          ;; to make subsequent routines simpler.
4822                          (nnheader-generate-fake-message-id))))
4823             ;; References.
4824             (progn
4825               (goto-char p)
4826               (if (search-forward "\nreferences: " nil t)
4827                   (progn
4828                     (setq end (point))
4829                     (prog1
4830                         (nnheader-header-value)
4831                       (setq ref
4832                             (buffer-substring
4833                              (progn
4834                                ;; (end-of-line)
4835                                (search-backward ">" end t)
4836                                (1+ (point)))
4837                              (progn
4838                                (search-backward "<" end t)
4839                                (point))))))
4840                 ;; Get the references from the in-reply-to header if there
4841                 ;; were no references and the in-reply-to header looks
4842                 ;; promising.
4843                 (if (and (search-forward "\nin-reply-to: " nil t)
4844                          (setq in-reply-to (nnheader-header-value))
4845                          (string-match "<[^>]+>" in-reply-to))
4846                     (let (ref2)
4847                       (setq ref (substring in-reply-to (match-beginning 0)
4848                                            (match-end 0)))
4849                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4850                         (setq ref2 (substring in-reply-to (match-beginning 0)
4851                                               (match-end 0)))
4852                         (when (> (length ref2) (length ref))
4853                           (setq ref ref2)))
4854                       ref)
4855                   (setq ref nil))))
4856             ;; Chars.
4857             (progn
4858               (goto-char p)
4859               (if (search-forward "\nchars: " nil t)
4860                   (if (numberp (setq chars (ignore-errors (read cur))))
4861                       chars 0)
4862                 0))
4863             ;; Lines.
4864             (progn
4865               (goto-char p)
4866               (if (search-forward "\nlines: " nil t)
4867                   (if (numberp (setq lines (ignore-errors (read cur))))
4868                       lines 0)
4869                 0))
4870             ;; Xref.
4871             (progn
4872               (goto-char p)
4873               (and (search-forward "\nxref: " nil t)
4874                    (nnheader-header-value)))
4875             ;; Extra.
4876             (when gnus-extra-headers
4877               (let ((extra gnus-extra-headers)
4878                     out)
4879                 (while extra
4880                   (goto-char p)
4881                   (when (search-forward
4882                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4883                     (push (cons (car extra) (nnheader-header-value)) out))
4884                   (pop extra))
4885                 out))))
4886           (goto-char p)
4887           (if (and (search-forward "\ncontent-type: " nil t)
4888                    (setq ctype (nnheader-header-value)))
4889               (mime-entity-set-content-type-internal
4890                header (mime-parse-Content-Type ctype)))
4891           (when (equal id ref)
4892             (setq ref nil))
4893
4894           (when gnus-alter-header-function
4895             (funcall gnus-alter-header-function header)
4896             (setq id (mail-header-id header)
4897                   ref (gnus-parent-id (mail-header-references header))))
4898
4899           (when (setq header
4900                       (gnus-dependencies-add-header
4901                        header dependencies force-new))
4902             (push header headers))
4903           (goto-char (point-max))
4904           (widen))
4905         (nreverse headers)))))
4906
4907 ;; Goes through the xover lines and returns a list of vectors
4908 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4909                                                   force-new dependencies
4910                                                   group also-fetch-heads)
4911   "Parse the news overview data in the server buffer.
4912 Return a list of headers that match SEQUENCE (see
4913 `nntp-retrieve-headers')."
4914   ;; Get the Xref when the users reads the articles since most/some
4915   ;; NNTP servers do not include Xrefs when using XOVER.
4916   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4917   (let ((mail-parse-charset gnus-newsgroup-charset)
4918         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4919         (cur nntp-server-buffer)
4920         (dependencies (or dependencies gnus-newsgroup-dependencies))
4921         number headers header)
4922     (save-excursion
4923       (set-buffer nntp-server-buffer)
4924       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4925       ;; Allow the user to mangle the headers before parsing them.
4926       (gnus-run-hooks 'gnus-parse-headers-hook)
4927       (goto-char (point-min))
4928       (while (not (eobp))
4929         (condition-case ()
4930             (while (and sequence (not (eobp)))
4931               (setq number (read cur))
4932               (while (and sequence
4933                           (< (car sequence) number))
4934                 (setq sequence (cdr sequence)))
4935               (and sequence
4936                    (eq number (car sequence))
4937                    (progn
4938                      (setq sequence (cdr sequence))
4939                      (setq header (inline
4940                                     (gnus-nov-parse-line
4941                                      number dependencies force-new))))
4942                    (push header headers))
4943               (forward-line 1))
4944           (error
4945            (gnus-error 4 "Strange nov line (%d)"
4946                        (count-lines (point-min) (point)))))
4947         (forward-line 1))
4948       ;; A common bug in inn is that if you have posted an article and
4949       ;; then retrieves the active file, it will answer correctly --
4950       ;; the new article is included.  However, a NOV entry for the
4951       ;; article may not have been generated yet, so this may fail.
4952       ;; We work around this problem by retrieving the last few
4953       ;; headers using HEAD.
4954       (if (or (not also-fetch-heads)
4955               (not sequence))
4956           ;; We (probably) got all the headers.
4957           (nreverse headers)
4958         (let ((gnus-nov-is-evil t))
4959           (nconc
4960            (nreverse headers)
4961            (gnus-retrieve-parsed-headers sequence group)
4962            ))))))
4963
4964 (defun gnus-article-get-xrefs ()
4965   "Fill in the Xref value in `gnus-current-headers', if necessary.
4966 This is meant to be called in `gnus-article-internal-prepare-hook'."
4967   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4968                                  gnus-current-headers)))
4969     (or (not gnus-use-cross-reference)
4970         (not headers)
4971         (and (mail-header-xref headers)
4972              (not (string= (mail-header-xref headers) "")))
4973         (let ((case-fold-search t)
4974               xref)
4975           (save-restriction
4976             (nnheader-narrow-to-headers)
4977             (goto-char (point-min))
4978             (when (or (and (not (eobp))
4979                            (eq (downcase (char-after)) ?x)
4980                            (looking-at "Xref:"))
4981                       (search-forward "\nXref:" nil t))
4982               (goto-char (1+ (match-end 0)))
4983               (setq xref (buffer-substring (point)
4984                                            (progn (end-of-line) (point))))
4985               (mail-header-set-xref headers xref)))))))
4986
4987 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4988   "Find article ID and insert the summary line for that article.
4989 OLD-HEADER can either be a header or a line number to insert
4990 the subject line on."
4991   (let* ((line (and (numberp old-header) old-header))
4992          (old-header (and (vectorp old-header) old-header))
4993          (header (cond ((and old-header use-old-header)
4994                         old-header)
4995                        ((and (numberp id)
4996                              (gnus-number-to-header id))
4997                         (gnus-number-to-header id))
4998                        (t
4999                         (gnus-read-header id))))
5000          (number (and (numberp id) id))
5001          d)
5002     (when header
5003       ;; Rebuild the thread that this article is part of and go to the
5004       ;; article we have fetched.
5005       (when (and (not gnus-show-threads)
5006                  old-header)
5007         (when (and number
5008                    (setq d (gnus-data-find (mail-header-number old-header))))
5009           (goto-char (gnus-data-pos d))
5010           (gnus-data-remove
5011            number
5012            (- (gnus-point-at-bol)
5013               (prog1
5014                   (1+ (gnus-point-at-eol))
5015                 (gnus-delete-line))))))
5016       (when old-header
5017         (mail-header-set-number header (mail-header-number old-header)))
5018       (setq gnus-newsgroup-sparse
5019             (delq (setq number (mail-header-number header))
5020                   gnus-newsgroup-sparse))
5021       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5022       (push number gnus-newsgroup-limit)
5023       (gnus-rebuild-thread (mail-header-id header) line)
5024       (gnus-summary-goto-subject number nil t))
5025     (when (and (numberp number)
5026                (> number 0))
5027       ;; We have to update the boundaries even if we can't fetch the
5028       ;; article if ID is a number -- so that the next `P' or `N'
5029       ;; command will fetch the previous (or next) article even
5030       ;; if the one we tried to fetch this time has been canceled.
5031       (when (> number gnus-newsgroup-end)
5032         (setq gnus-newsgroup-end number))
5033       (when (< number gnus-newsgroup-begin)
5034         (setq gnus-newsgroup-begin number))
5035       (setq gnus-newsgroup-unselected
5036             (delq number gnus-newsgroup-unselected)))
5037     ;; Report back a success?
5038     (and header (mail-header-number header))))
5039
5040 ;;; Process/prefix in the summary buffer
5041
5042 (defun gnus-summary-work-articles (n)
5043   "Return a list of articles to be worked upon.
5044 The prefix argument, the list of process marked articles, and the
5045 current article will be taken into consideration."
5046   (save-excursion
5047     (set-buffer gnus-summary-buffer)
5048     (cond
5049      (n
5050       ;; A numerical prefix has been given.
5051       (setq n (prefix-numeric-value n))
5052       (let ((backward (< n 0))
5053             (n (abs (prefix-numeric-value n)))
5054             articles article)
5055         (save-excursion
5056           (while
5057               (and (> n 0)
5058                    (push (setq article (gnus-summary-article-number))
5059                          articles)
5060                    (if backward
5061                        (gnus-summary-find-prev nil article)
5062                      (gnus-summary-find-next nil article)))
5063             (decf n)))
5064         (nreverse articles)))
5065      ((and (gnus-region-active-p) (mark))
5066       (message "region active")
5067       ;; Work on the region between point and mark.
5068       (let ((max (max (point) (mark)))
5069             articles article)
5070         (save-excursion
5071           (goto-char (min (point) (mark)))
5072           (while
5073               (and
5074                (push (setq article (gnus-summary-article-number)) articles)
5075                (gnus-summary-find-next nil article)
5076                (< (point) max)))
5077           (nreverse articles))))
5078      (gnus-newsgroup-processable
5079       ;; There are process-marked articles present.
5080       ;; Save current state.
5081       (gnus-summary-save-process-mark)
5082       ;; Return the list.
5083       (reverse gnus-newsgroup-processable))
5084      (t
5085       ;; Just return the current article.
5086       (list (gnus-summary-article-number))))))
5087
5088 (defmacro gnus-summary-iterate (arg &rest forms)
5089   "Iterate over the process/prefixed articles and do FORMS.
5090 ARG is the interactive prefix given to the command.  FORMS will be
5091 executed with point over the summary line of the articles."
5092   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5093     `(let ((,articles (gnus-summary-work-articles ,arg)))
5094        (while ,articles
5095          (gnus-summary-goto-subject (car ,articles))
5096          ,@forms
5097          (pop ,articles)))))
5098
5099 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5100 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5101
5102 (defun gnus-summary-save-process-mark ()
5103   "Push the current set of process marked articles on the stack."
5104   (interactive)
5105   (push (copy-sequence gnus-newsgroup-processable)
5106         gnus-newsgroup-process-stack))
5107
5108 (defun gnus-summary-kill-process-mark ()
5109   "Push the current set of process marked articles on the stack and unmark."
5110   (interactive)
5111   (gnus-summary-save-process-mark)
5112   (gnus-summary-unmark-all-processable))
5113
5114 (defun gnus-summary-yank-process-mark ()
5115   "Pop the last process mark state off the stack and restore it."
5116   (interactive)
5117   (unless gnus-newsgroup-process-stack
5118     (error "Empty mark stack"))
5119   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5120
5121 (defun gnus-summary-process-mark-set (set)
5122   "Make SET into the current process marked articles."
5123   (gnus-summary-unmark-all-processable)
5124   (while set
5125     (gnus-summary-set-process-mark (pop set))))
5126
5127 ;;; Searching and stuff
5128
5129 (defun gnus-summary-search-group (&optional backward use-level)
5130   "Search for next unread newsgroup.
5131 If optional argument BACKWARD is non-nil, search backward instead."
5132   (save-excursion
5133     (set-buffer gnus-group-buffer)
5134     (when (gnus-group-search-forward
5135            backward nil (if use-level (gnus-group-group-level) nil))
5136       (gnus-group-group-name))))
5137
5138 (defun gnus-summary-best-group (&optional exclude-group)
5139   "Find the name of the best unread group.
5140 If EXCLUDE-GROUP, do not go to this group."
5141   (save-excursion
5142     (set-buffer gnus-group-buffer)
5143     (save-excursion
5144       (gnus-group-best-unread-group exclude-group))))
5145
5146 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5147   (if backward (gnus-summary-find-prev)
5148     (let* ((dummy (gnus-summary-article-intangible-p))
5149            (article (or article (gnus-summary-article-number)))
5150            (arts (gnus-data-find-list article))
5151            result)
5152       (when (and (not dummy)
5153                  (or (not gnus-summary-check-current)
5154                      (not unread)
5155                      (not (gnus-data-unread-p (car arts)))))
5156         (setq arts (cdr arts)))
5157       (when (setq result
5158                   (if unread
5159                       (progn
5160                         (while arts
5161                           (when (or (and undownloaded
5162                                          (eq gnus-undownloaded-mark
5163                                              (gnus-data-mark (car arts))))
5164                                     (gnus-data-unread-p (car arts)))
5165                             (setq result (car arts)
5166                                   arts nil))
5167                           (setq arts (cdr arts)))
5168                         result)
5169                     (car arts)))
5170         (goto-char (gnus-data-pos result))
5171         (gnus-data-number result)))))
5172
5173 (defun gnus-summary-find-prev (&optional unread article)
5174   (let* ((eobp (eobp))
5175          (article (or article (gnus-summary-article-number)))
5176          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5177          result)
5178     (when (and (not eobp)
5179                (or (not gnus-summary-check-current)
5180                    (not unread)
5181                    (not (gnus-data-unread-p (car arts)))))
5182       (setq arts (cdr arts)))
5183     (when (setq result
5184                 (if unread
5185                     (progn
5186                       (while arts
5187                         (when (gnus-data-unread-p (car arts))
5188                           (setq result (car arts)
5189                                 arts nil))
5190                         (setq arts (cdr arts)))
5191                       result)
5192                   (car arts)))
5193       (goto-char (gnus-data-pos result))
5194       (gnus-data-number result))))
5195
5196 (defun gnus-summary-find-subject (subject &optional unread backward article)
5197   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5198          (article (or article (gnus-summary-article-number)))
5199          (articles (gnus-data-list backward))
5200          (arts (gnus-data-find-list article articles))
5201          result)
5202     (when (or (not gnus-summary-check-current)
5203               (not unread)
5204               (not (gnus-data-unread-p (car arts))))
5205       (setq arts (cdr arts)))
5206     (while arts
5207       (and (or (not unread)
5208                (gnus-data-unread-p (car arts)))
5209            (vectorp (gnus-data-header (car arts)))
5210            (gnus-subject-equal
5211             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5212            (setq result (car arts)
5213                  arts nil))
5214       (setq arts (cdr arts)))
5215     (and result
5216          (goto-char (gnus-data-pos result))
5217          (gnus-data-number result))))
5218
5219 (defun gnus-summary-search-forward (&optional unread subject backward)
5220   "Search forward for an article.
5221 If UNREAD, look for unread articles.  If SUBJECT, look for
5222 articles with that subject.  If BACKWARD, search backward instead."
5223   (cond (subject (gnus-summary-find-subject subject unread backward))
5224         (backward (gnus-summary-find-prev unread))
5225         (t (gnus-summary-find-next unread))))
5226
5227 (defun gnus-recenter (&optional n)
5228   "Center point in window and redisplay frame.
5229 Also do horizontal recentering."
5230   (interactive "P")
5231   (when (and gnus-auto-center-summary
5232              (not (eq gnus-auto-center-summary 'vertical)))
5233     (gnus-horizontal-recenter))
5234   (recenter n))
5235
5236 (defun gnus-summary-recenter ()
5237   "Center point in the summary window.
5238 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5239 displayed, no centering will be performed."
5240   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5241   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5242   (interactive)
5243   (let* ((top (cond ((< (window-height) 4) 0)
5244                     ((< (window-height) 7) 1)
5245                     (t (if (numberp gnus-auto-center-summary)
5246                            gnus-auto-center-summary
5247                          2))))
5248          (height (1- (window-height)))
5249          (bottom (save-excursion (goto-char (point-max))
5250                                  (forward-line (- height))
5251                                  (point)))
5252          (window (get-buffer-window (current-buffer))))
5253     ;; The user has to want it.
5254     (when gnus-auto-center-summary
5255       (when (get-buffer-window gnus-article-buffer)
5256         ;; Only do recentering when the article buffer is displayed,
5257         ;; Set the window start to either `bottom', which is the biggest
5258         ;; possible valid number, or the second line from the top,
5259         ;; whichever is the least.
5260         (set-window-start
5261          window (min bottom (save-excursion
5262                               (forward-line (- top)) (point)))
5263          t))
5264       ;; Do horizontal recentering while we're at it.
5265       (when (and (get-buffer-window (current-buffer) t)
5266                  (not (eq gnus-auto-center-summary 'vertical)))
5267         (let ((selected (selected-window)))
5268           (select-window (get-buffer-window (current-buffer) t))
5269           (gnus-summary-position-point)
5270           (gnus-horizontal-recenter)
5271           (select-window selected))))))
5272
5273 (defun gnus-summary-jump-to-group (newsgroup)
5274   "Move point to NEWSGROUP in group mode buffer."
5275   ;; Keep update point of group mode buffer if visible.
5276   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5277       (save-window-excursion
5278         ;; Take care of tree window mode.
5279         (when (get-buffer-window gnus-group-buffer)
5280           (pop-to-buffer gnus-group-buffer))
5281         (gnus-group-jump-to-group newsgroup))
5282     (save-excursion
5283       ;; Take care of tree window mode.
5284       (if (get-buffer-window gnus-group-buffer)
5285           (pop-to-buffer gnus-group-buffer)
5286         (set-buffer gnus-group-buffer))
5287       (gnus-group-jump-to-group newsgroup))))
5288
5289 ;; This function returns a list of article numbers based on the
5290 ;; difference between the ranges of read articles in this group and
5291 ;; the range of active articles.
5292 (defun gnus-list-of-unread-articles (group)
5293   (let* ((read (gnus-info-read (gnus-get-info group)))
5294          (active (or (gnus-active group) (gnus-activate-group group)))
5295          (last (cdr active))
5296          first nlast unread)
5297     ;; If none are read, then all are unread.
5298     (if (not read)
5299         (setq first (car active))
5300       ;; If the range of read articles is a single range, then the
5301       ;; first unread article is the article after the last read
5302       ;; article.  Sounds logical, doesn't it?
5303       (if (and (not (listp (cdr read)))
5304                (or (< (car read) (car active))
5305                    (progn (setq read (list read))
5306                           nil)))
5307           (setq first (max (car active) (1+ (cdr read))))
5308         ;; `read' is a list of ranges.
5309         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5310                                   (caar read)))
5311                   1)
5312           (setq first (car active)))
5313         (while read
5314           (when first
5315             (while (< first nlast)
5316               (push first unread)
5317               (setq first (1+ first))))
5318           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5319           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5320           (setq read (cdr read)))))
5321     ;; And add the last unread articles.
5322     (while (<= first last)
5323       (push first unread)
5324       (setq first (1+ first)))
5325     ;; Return the list of unread articles.
5326     (delq 0 (nreverse unread))))
5327
5328 (defun gnus-list-of-read-articles (group)
5329   "Return a list of unread, unticked and non-dormant articles."
5330   (let* ((info (gnus-get-info group))
5331          (marked (gnus-info-marks info))
5332          (active (gnus-active group)))
5333     (and info active
5334          (gnus-set-difference
5335           (gnus-sorted-complement
5336            (gnus-uncompress-range active)
5337            (gnus-list-of-unread-articles group))
5338           (append
5339            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5340            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5341
5342 ;; Various summary commands
5343
5344 (defun gnus-summary-select-article-buffer ()
5345   "Reconfigure windows to show article buffer."
5346   (interactive)
5347   (if (not (gnus-buffer-live-p gnus-article-buffer))
5348       (error "There is no article buffer for this summary buffer")
5349     (gnus-configure-windows 'article)
5350     (select-window (get-buffer-window gnus-article-buffer))))
5351
5352 (defun gnus-summary-universal-argument (arg)
5353   "Perform any operation on all articles that are process/prefixed."
5354   (interactive "P")
5355   (let ((articles (gnus-summary-work-articles arg))
5356         func article)
5357     (if (eq
5358          (setq
5359           func
5360           (key-binding
5361            (read-key-sequence
5362             (substitute-command-keys
5363              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5364          'undefined)
5365         (gnus-error 1 "Undefined key")
5366       (save-excursion
5367         (while articles
5368           (gnus-summary-goto-subject (setq article (pop articles)))
5369           (let (gnus-newsgroup-processable)
5370             (command-execute func))
5371           (gnus-summary-remove-process-mark article)))))
5372   (gnus-summary-position-point))
5373
5374 (defun gnus-summary-toggle-truncation (&optional arg)
5375   "Toggle truncation of summary lines.
5376 With arg, turn line truncation on iff arg is positive."
5377   (interactive "P")
5378   (setq truncate-lines
5379         (if (null arg) (not truncate-lines)
5380           (> (prefix-numeric-value arg) 0)))
5381   (redraw-display))
5382
5383 (defun gnus-summary-reselect-current-group (&optional all rescan)
5384   "Exit and then reselect the current newsgroup.
5385 The prefix argument ALL means to select all articles."
5386   (interactive "P")
5387   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5388     (error "Ephemeral groups can't be reselected"))
5389   (let ((current-subject (gnus-summary-article-number))
5390         (group gnus-newsgroup-name))
5391     (setq gnus-newsgroup-begin nil)
5392     (gnus-summary-exit)
5393     ;; We have to adjust the point of group mode buffer because
5394     ;; point was moved to the next unread newsgroup by exiting.
5395     (gnus-summary-jump-to-group group)
5396     (when rescan
5397       (save-excursion
5398         (save-window-excursion
5399           ;; Don't show group contents.
5400           (set-window-start (selected-window) (point-max))
5401           (gnus-group-get-new-news-this-group 1))))
5402     (gnus-group-read-group all t)
5403     (gnus-summary-goto-subject current-subject nil t)))
5404
5405 (defun gnus-summary-rescan-group (&optional all)
5406   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5407   (interactive "P")
5408   (gnus-summary-reselect-current-group all t))
5409
5410 (defun gnus-summary-update-info (&optional non-destructive)
5411   (save-excursion
5412     (let ((group gnus-newsgroup-name))
5413       (when group
5414         (when gnus-newsgroup-kill-headers
5415           (setq gnus-newsgroup-killed
5416                 (gnus-compress-sequence
5417                  (nconc
5418                   (gnus-set-sorted-intersection
5419                    (gnus-uncompress-range gnus-newsgroup-killed)
5420                    (setq gnus-newsgroup-unselected
5421                          (sort gnus-newsgroup-unselected '<)))
5422                   (setq gnus-newsgroup-unreads
5423                         (sort gnus-newsgroup-unreads '<)))
5424                  t)))
5425         (unless (listp (cdr gnus-newsgroup-killed))
5426           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5427         (let ((headers gnus-newsgroup-headers))
5428           ;; Set the new ranges of read articles.
5429           (save-excursion
5430             (set-buffer gnus-group-buffer)
5431             (gnus-undo-force-boundary))
5432           (gnus-update-read-articles
5433            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5434           ;; Set the current article marks.
5435           (let ((gnus-newsgroup-scored
5436                  (if (and (not gnus-save-score)
5437                           (not non-destructive))
5438                      nil
5439                    gnus-newsgroup-scored)))
5440             (save-excursion
5441               (gnus-update-marks)))
5442           ;; Do the cross-ref thing.
5443           (when gnus-use-cross-reference
5444             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5445           ;; Do not switch windows but change the buffer to work.
5446           (set-buffer gnus-group-buffer)
5447           (unless (gnus-ephemeral-group-p group)
5448             (gnus-group-update-group group)))))))
5449
5450 (defun gnus-summary-save-newsrc (&optional force)
5451   "Save the current number of read/marked articles in the dribble buffer.
5452 The dribble buffer will then be saved.
5453 If FORCE (the prefix), also save the .newsrc file(s)."
5454   (interactive "P")
5455   (gnus-summary-update-info t)
5456   (if force
5457       (gnus-save-newsrc-file)
5458     (gnus-dribble-save)))
5459
5460 (defun gnus-summary-exit (&optional temporary)
5461   "Exit reading current newsgroup, and then return to group selection mode.
5462 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5463   (interactive)
5464   (gnus-set-global-variables)
5465   (gnus-kill-save-kill-buffer)
5466   (gnus-async-halt-prefetch)
5467   (let* ((group gnus-newsgroup-name)
5468          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5469          (mode major-mode)
5470          (group-point nil)
5471          (buf (current-buffer)))
5472     (unless quit-config
5473       ;; Do adaptive scoring, and possibly save score files.
5474       (when gnus-newsgroup-adaptive
5475         (gnus-score-adaptive))
5476       (when gnus-use-scoring
5477         (gnus-score-save)))
5478     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5479     ;; If we have several article buffers, we kill them at exit.
5480     (unless gnus-single-article-buffer
5481       (gnus-kill-buffer gnus-original-article-buffer)
5482       (setq gnus-article-current nil))
5483     (when gnus-use-cache
5484       (gnus-cache-possibly-remove-articles)
5485       (gnus-cache-save-buffers))
5486     (gnus-async-prefetch-remove-group group)
5487     (when gnus-suppress-duplicates
5488       (gnus-dup-enter-articles))
5489     (when gnus-use-trees
5490       (gnus-tree-close group))
5491     (when gnus-use-cache
5492       (gnus-cache-write-active))
5493     ;; Remove entries for this group.
5494     (nnmail-purge-split-history (gnus-group-real-name group))
5495     ;; Make all changes in this group permanent.
5496     (unless quit-config
5497       (gnus-run-hooks 'gnus-exit-group-hook)
5498       (gnus-summary-update-info))
5499     (gnus-close-group group)
5500     ;; Make sure where we were, and go to next newsgroup.
5501     (set-buffer gnus-group-buffer)
5502     (unless quit-config
5503       (gnus-group-jump-to-group group))
5504     (gnus-run-hooks 'gnus-summary-exit-hook)
5505     (unless (or quit-config
5506                 ;; If this group has disappeared from the summary
5507                 ;; buffer, don't skip forwards.
5508                 (not (string= group (gnus-group-group-name))))
5509       (gnus-group-next-unread-group 1))
5510     (setq group-point (point))
5511     (if temporary
5512         nil                             ;Nothing to do.
5513       ;; If we have several article buffers, we kill them at exit.
5514       (unless gnus-single-article-buffer
5515         (gnus-kill-buffer gnus-article-buffer)
5516         (gnus-kill-buffer gnus-original-article-buffer)
5517         (setq gnus-article-current nil))
5518       (set-buffer buf)
5519       (if (not gnus-kill-summary-on-exit)
5520           (gnus-deaden-summary)
5521         ;; We set all buffer-local variables to nil.  It is unclear why
5522         ;; this is needed, but if we don't, buffer-local variables are
5523         ;; not garbage-collected, it seems.  This would the lead to en
5524         ;; ever-growing Emacs.
5525         (gnus-summary-clear-local-variables)
5526         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5527           (gnus-summary-clear-local-variables))
5528         (when (get-buffer gnus-article-buffer)
5529           (bury-buffer gnus-article-buffer))
5530         ;; We clear the global counterparts of the buffer-local
5531         ;; variables as well, just to be on the safe side.
5532         (set-buffer gnus-group-buffer)
5533         (gnus-summary-clear-local-variables)
5534         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5535           (gnus-summary-clear-local-variables))
5536         ;; Return to group mode buffer.
5537         (when (eq mode 'gnus-summary-mode)
5538           (gnus-kill-buffer buf)))
5539       (setq gnus-current-select-method gnus-select-method)
5540       (pop-to-buffer gnus-group-buffer)
5541       (if (not quit-config)
5542           (progn
5543             (goto-char group-point)
5544             (gnus-configure-windows 'group 'force)
5545             (unless (pos-visible-in-window-p)
5546               (forward-line (/ (static-if (featurep 'xemacs)
5547                                    (window-displayed-height)
5548                                  (1- (window-height)))
5549                                -2))
5550               (set-window-start (selected-window) (point))
5551               (goto-char group-point)))
5552         (gnus-handle-ephemeral-exit quit-config))
5553       ;; Clear the current group name.
5554       (unless quit-config
5555         (setq gnus-newsgroup-name nil)))))
5556
5557 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5558 (defun gnus-summary-exit-no-update (&optional no-questions)
5559   "Quit reading current newsgroup without updating read article info."
5560   (interactive)
5561   (let* ((group gnus-newsgroup-name)
5562          (quit-config (gnus-group-quit-config group)))
5563     (when (or no-questions
5564               gnus-expert-user
5565               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5566       (gnus-async-halt-prefetch)
5567       (mapcar 'funcall
5568               (delq 'gnus-summary-expire-articles
5569                     (copy-sequence gnus-summary-prepare-exit-hook)))
5570       ;; If we have several article buffers, we kill them at exit.
5571       (unless gnus-single-article-buffer
5572         (gnus-kill-buffer gnus-article-buffer)
5573         (gnus-kill-buffer gnus-original-article-buffer)
5574         (setq gnus-article-current nil))
5575       (if (not gnus-kill-summary-on-exit)
5576           (gnus-deaden-summary)
5577         (gnus-close-group group)
5578         (gnus-summary-clear-local-variables)
5579         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5580           (gnus-summary-clear-local-variables))
5581         (set-buffer gnus-group-buffer)
5582         (gnus-summary-clear-local-variables)
5583         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5584           (gnus-summary-clear-local-variables))
5585         (when (get-buffer gnus-summary-buffer)
5586           (kill-buffer gnus-summary-buffer)))
5587       (unless gnus-single-article-buffer
5588         (setq gnus-article-current nil))
5589       (when gnus-use-trees
5590         (gnus-tree-close group))
5591       (gnus-async-prefetch-remove-group group)
5592       (when (get-buffer gnus-article-buffer)
5593         (bury-buffer gnus-article-buffer))
5594       ;; Return to the group buffer.
5595       (gnus-configure-windows 'group 'force)
5596       ;; Clear the current group name.
5597       (setq gnus-newsgroup-name nil)
5598       (when (equal (gnus-group-group-name) group)
5599         (gnus-group-next-unread-group 1))
5600       (when quit-config
5601         (gnus-handle-ephemeral-exit quit-config)))))
5602
5603 (defun gnus-handle-ephemeral-exit (quit-config)
5604   "Handle movement when leaving an ephemeral group.
5605 The state which existed when entering the ephemeral is reset."
5606   (if (not (buffer-name (car quit-config)))
5607       (gnus-configure-windows 'group 'force)
5608     (set-buffer (car quit-config))
5609     (cond ((eq major-mode 'gnus-summary-mode)
5610            (gnus-set-global-variables))
5611           ((eq major-mode 'gnus-article-mode)
5612            (save-excursion
5613              ;; The `gnus-summary-buffer' variable may point
5614              ;; to the old summary buffer when using a single
5615              ;; article buffer.
5616              (unless (gnus-buffer-live-p gnus-summary-buffer)
5617                (set-buffer gnus-group-buffer))
5618              (set-buffer gnus-summary-buffer)
5619              (gnus-set-global-variables))))
5620     (if (or (eq (cdr quit-config) 'article)
5621             (eq (cdr quit-config) 'pick))
5622         (progn
5623           ;; The current article may be from the ephemeral group
5624           ;; thus it is best that we reload this article
5625           (gnus-summary-show-article)
5626           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5627               (gnus-configure-windows 'pick 'force)
5628             (gnus-configure-windows (cdr quit-config) 'force)))
5629       (gnus-configure-windows (cdr quit-config) 'force))
5630     (when (eq major-mode 'gnus-summary-mode)
5631       (gnus-summary-next-subject 1 nil t)
5632       (gnus-summary-recenter)
5633       (gnus-summary-position-point))))
5634
5635 (defun gnus-summary-preview-mime-message ()
5636   "MIME decode and play this message."
5637   (interactive)
5638   (let ((gnus-break-pages nil)
5639         (gnus-show-mime t))
5640     (gnus-summary-select-article gnus-show-all-headers t))
5641   (select-window (get-buffer-window gnus-article-buffer)))
5642
5643 ;;; Dead summaries.
5644
5645 (defvar gnus-dead-summary-mode-map nil)
5646
5647 (unless gnus-dead-summary-mode-map
5648   (setq gnus-dead-summary-mode-map (make-keymap))
5649   (suppress-keymap gnus-dead-summary-mode-map)
5650   (substitute-key-definition
5651    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5652   (let ((keys '("\C-d" "\r" "\177" [delete])))
5653     (while keys
5654       (define-key gnus-dead-summary-mode-map
5655         (pop keys) 'gnus-summary-wake-up-the-dead))))
5656
5657 (defvar gnus-dead-summary-mode nil
5658   "Minor mode for Gnus summary buffers.")
5659
5660 (defun gnus-dead-summary-mode (&optional arg)
5661   "Minor mode for Gnus summary buffers."
5662   (interactive "P")
5663   (when (eq major-mode 'gnus-summary-mode)
5664     (make-local-variable 'gnus-dead-summary-mode)
5665     (setq gnus-dead-summary-mode
5666           (if (null arg) (not gnus-dead-summary-mode)
5667             (> (prefix-numeric-value arg) 0)))
5668     (when gnus-dead-summary-mode
5669       (gnus-add-minor-mode
5670        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5671
5672 (defun gnus-deaden-summary ()
5673   "Make the current summary buffer into a dead summary buffer."
5674   ;; Kill any previous dead summary buffer.
5675   (when (and gnus-dead-summary
5676              (buffer-name gnus-dead-summary))
5677     (save-excursion
5678       (set-buffer gnus-dead-summary)
5679       (when gnus-dead-summary-mode
5680         (kill-buffer (current-buffer)))))
5681   ;; Make this the current dead summary.
5682   (setq gnus-dead-summary (current-buffer))
5683   (gnus-dead-summary-mode 1)
5684   (let ((name (buffer-name)))
5685     (when (string-match "Summary" name)
5686       (rename-buffer
5687        (concat (substring name 0 (match-beginning 0)) "Dead "
5688                (substring name (match-beginning 0)))
5689        t)
5690       (bury-buffer))))
5691
5692 (defun gnus-kill-or-deaden-summary (buffer)
5693   "Kill or deaden the summary BUFFER."
5694   (save-excursion
5695     (when (and (buffer-name buffer)
5696                (not gnus-single-article-buffer))
5697       (save-excursion
5698         (set-buffer buffer)
5699         (gnus-kill-buffer gnus-article-buffer)
5700         (gnus-kill-buffer gnus-original-article-buffer)))
5701     (cond (gnus-kill-summary-on-exit
5702            (when (and gnus-use-trees
5703                       (gnus-buffer-exists-p buffer))
5704              (save-excursion
5705                (set-buffer buffer)
5706                (gnus-tree-close gnus-newsgroup-name)))
5707            (gnus-kill-buffer buffer))
5708           ((gnus-buffer-exists-p buffer)
5709            (save-excursion
5710              (set-buffer buffer)
5711              (gnus-deaden-summary))))))
5712
5713 (defun gnus-summary-wake-up-the-dead (&rest args)
5714   "Wake up the dead summary buffer."
5715   (interactive)
5716   (gnus-dead-summary-mode -1)
5717   (let ((name (buffer-name)))
5718     (when (string-match "Dead " name)
5719       (rename-buffer
5720        (concat (substring name 0 (match-beginning 0))
5721                (substring name (match-end 0)))
5722        t)))
5723   (gnus-message 3 "This dead summary is now alive again"))
5724
5725 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5726 (defun gnus-summary-fetch-faq (&optional faq-dir)
5727   "Fetch the FAQ for the current group.
5728 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5729 in."
5730   (interactive
5731    (list
5732     (when current-prefix-arg
5733       (completing-read
5734        "Faq dir: " (and (listp gnus-group-faq-directory)
5735                         (mapcar (lambda (file) (list file))
5736                                 gnus-group-faq-directory))))))
5737   (let (gnus-faq-buffer)
5738     (when (setq gnus-faq-buffer
5739                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5740       (gnus-configure-windows 'summary-faq))))
5741
5742 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5743 (defun gnus-summary-describe-group (&optional force)
5744   "Describe the current newsgroup."
5745   (interactive "P")
5746   (gnus-group-describe-group force gnus-newsgroup-name))
5747
5748 (defun gnus-summary-describe-briefly ()
5749   "Describe summary mode commands briefly."
5750   (interactive)
5751   (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")))
5752
5753 ;; Walking around group mode buffer from summary mode.
5754
5755 (defun gnus-summary-next-group (&optional no-article target-group backward)
5756   "Exit current newsgroup and then select next unread newsgroup.
5757 If prefix argument NO-ARTICLE is non-nil, no article is selected
5758 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5759 previous group instead."
5760   (interactive "P")
5761   ;; Stop pre-fetching.
5762   (gnus-async-halt-prefetch)
5763   (let ((current-group gnus-newsgroup-name)
5764         (current-buffer (current-buffer))
5765         entered)
5766     ;; First we semi-exit this group to update Xrefs and all variables.
5767     ;; We can't do a real exit, because the window conf must remain
5768     ;; the same in case the user is prompted for info, and we don't
5769     ;; want the window conf to change before that...
5770     (gnus-summary-exit t)
5771     (while (not entered)
5772       ;; Then we find what group we are supposed to enter.
5773       (set-buffer gnus-group-buffer)
5774       (gnus-group-jump-to-group current-group)
5775       (setq target-group
5776             (or target-group
5777                 (if (eq gnus-keep-same-level 'best)
5778                     (gnus-summary-best-group gnus-newsgroup-name)
5779                   (gnus-summary-search-group backward gnus-keep-same-level))))
5780       (if (not target-group)
5781           ;; There are no further groups, so we return to the group
5782           ;; buffer.
5783           (progn
5784             (gnus-message 5 "Returning to the group buffer")
5785             (setq entered t)
5786             (when (gnus-buffer-live-p current-buffer)
5787               (set-buffer current-buffer)
5788               (gnus-summary-exit))
5789             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5790         ;; We try to enter the target group.
5791         (gnus-group-jump-to-group target-group)
5792         (let ((unreads (gnus-group-group-unread)))
5793           (if (and (or (eq t unreads)
5794                        (and unreads (not (zerop unreads))))
5795                    (gnus-summary-read-group
5796                     target-group nil no-article
5797                     (and (buffer-name current-buffer) current-buffer)
5798                     nil backward))
5799               (setq entered t)
5800             (setq current-group target-group
5801                   target-group nil)))))))
5802
5803 (defun gnus-summary-prev-group (&optional no-article)
5804   "Exit current newsgroup and then select previous unread newsgroup.
5805 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5806   (interactive "P")
5807   (gnus-summary-next-group no-article nil t))
5808
5809 ;; Walking around summary lines.
5810
5811 (defun gnus-summary-first-subject (&optional unread undownloaded)
5812   "Go to the first unread subject.
5813 If UNREAD is non-nil, go to the first unread article.
5814 Returns the article selected or nil if there are no unread articles."
5815   (interactive "P")
5816   (prog1
5817       (cond
5818        ;; Empty summary.
5819        ((null gnus-newsgroup-data)
5820         (gnus-message 3 "No articles in the group")
5821         nil)
5822        ;; Pick the first article.
5823        ((not unread)
5824         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5825         (gnus-data-number (car gnus-newsgroup-data)))
5826        ;; No unread articles.
5827        ((null gnus-newsgroup-unreads)
5828         (gnus-message 3 "No more unread articles")
5829         nil)
5830        ;; Find the first unread article.
5831        (t
5832         (let ((data gnus-newsgroup-data))
5833           (while (and data
5834                       (and (not (and undownloaded
5835                                      (eq gnus-undownloaded-mark
5836                                          (gnus-data-mark (car data)))))
5837                            (not (gnus-data-unread-p (car data)))))
5838             (setq data (cdr data)))
5839           (when data
5840             (goto-char (gnus-data-pos (car data)))
5841             (gnus-data-number (car data))))))
5842     (gnus-summary-position-point)))
5843
5844 (defun gnus-summary-next-subject (n &optional unread dont-display)
5845   "Go to next N'th summary line.
5846 If N is negative, go to the previous N'th subject line.
5847 If UNREAD is non-nil, only unread articles are selected.
5848 The difference between N and the actual number of steps taken is
5849 returned."
5850   (interactive "p")
5851   (let ((backward (< n 0))
5852         (n (abs n)))
5853     (while (and (> n 0)
5854                 (if backward
5855                     (gnus-summary-find-prev unread)
5856                   (gnus-summary-find-next unread)))
5857       (unless (zerop (setq n (1- n)))
5858         (gnus-summary-show-thread)))
5859     (when (/= 0 n)
5860       (gnus-message 7 "No more%s articles"
5861                     (if unread " unread" "")))
5862     (unless dont-display
5863       (gnus-summary-recenter)
5864       (gnus-summary-position-point))
5865     n))
5866
5867 (defun gnus-summary-next-unread-subject (n)
5868   "Go to next N'th unread summary line."
5869   (interactive "p")
5870   (gnus-summary-next-subject n t))
5871
5872 (defun gnus-summary-prev-subject (n &optional unread)
5873   "Go to previous N'th summary line.
5874 If optional argument UNREAD is non-nil, only unread article is selected."
5875   (interactive "p")
5876   (gnus-summary-next-subject (- n) unread))
5877
5878 (defun gnus-summary-prev-unread-subject (n)
5879   "Go to previous N'th unread summary line."
5880   (interactive "p")
5881   (gnus-summary-next-subject (- n) t))
5882
5883 (defun gnus-summary-goto-subject (article &optional force silent)
5884   "Go the subject line of ARTICLE.
5885 If FORCE, also allow jumping to articles not currently shown."
5886   (interactive "nArticle number: ")
5887   (let ((b (point))
5888         (data (gnus-data-find article)))
5889     ;; We read in the article if we have to.
5890     (and (not data)
5891          force
5892          (gnus-summary-insert-subject
5893           article
5894           (if (or (numberp force) (vectorp force)) force)
5895           t)
5896          (setq data (gnus-data-find article)))
5897     (goto-char b)
5898     (if (not data)
5899         (progn
5900           (unless silent
5901             (gnus-message 3 "Can't find article %d" article))
5902           nil)
5903       (goto-char (gnus-data-pos data))
5904       (gnus-summary-position-point)
5905       article)))
5906
5907 ;; Walking around summary lines with displaying articles.
5908
5909 (defun gnus-summary-expand-window (&optional arg)
5910   "Make the summary buffer take up the entire Emacs frame.
5911 Given a prefix, will force an `article' buffer configuration."
5912   (interactive "P")
5913   (if arg
5914       (gnus-configure-windows 'article 'force)
5915     (gnus-configure-windows 'summary 'force)))
5916
5917 (defun gnus-summary-display-article (article &optional all-header)
5918   "Display ARTICLE in article buffer."
5919   (gnus-set-global-variables)
5920   (when (gnus-buffer-live-p gnus-article-buffer)
5921     (with-current-buffer gnus-article-buffer
5922       (setq gnus-article-charset gnus-newsgroup-charset)
5923       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
5924   (if (null article)
5925       nil
5926     (prog1
5927         (if gnus-summary-display-article-function
5928             (funcall gnus-summary-display-article-function article all-header)
5929           (gnus-article-prepare article all-header))
5930       (with-current-buffer gnus-article-buffer
5931         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5932              nil))
5933       (gnus-run-hooks 'gnus-select-article-hook)
5934       (when (and gnus-current-article
5935                  (not (zerop gnus-current-article)))
5936         (gnus-summary-goto-subject gnus-current-article))
5937       (gnus-summary-recenter)
5938       (when (and gnus-use-trees gnus-show-threads)
5939         (gnus-possibly-generate-tree article)
5940         (gnus-highlight-selected-tree article))
5941       ;; Successfully display article.
5942       (gnus-article-set-window-start
5943        (cdr (assq article gnus-newsgroup-bookmarks))))))
5944
5945 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5946   "Select the current article.
5947 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5948 non-nil, the article will be re-fetched even if it already present in
5949 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5950 be displayed."
5951   ;; Make sure we are in the summary buffer to work around bbdb bug.
5952   (unless (eq major-mode 'gnus-summary-mode)
5953     (set-buffer gnus-summary-buffer))
5954   (let ((article (or article (gnus-summary-article-number)))
5955         (all-headers (not (not all-headers))) ;Must be T or NIL.
5956         gnus-summary-display-article-function)
5957     (and (not pseudo)
5958          (gnus-summary-article-pseudo-p article)
5959          (error "This is a pseudo-article"))
5960     (save-excursion
5961       (set-buffer gnus-summary-buffer)
5962       (if (or (and gnus-single-article-buffer
5963                    (or (null gnus-current-article)
5964                        (null gnus-article-current)
5965                        (null (get-buffer gnus-article-buffer))
5966                        (not (eq article (cdr gnus-article-current)))
5967                        (not (equal (car gnus-article-current)
5968                                    gnus-newsgroup-name))))
5969               (and (not gnus-single-article-buffer)
5970                    (or (null gnus-current-article)
5971                        (not (eq gnus-current-article article))))
5972               force)
5973           ;; The requested article is different from the current article.
5974           (progn
5975             (gnus-summary-display-article article all-headers)
5976             (when (or all-headers gnus-show-all-headers)
5977               (gnus-article-show-all-headers))
5978             (gnus-article-set-window-start
5979              (cdr (assq article gnus-newsgroup-bookmarks)))
5980             article)
5981         (when (or all-headers gnus-show-all-headers)
5982           (gnus-article-show-all-headers))
5983         'old))))
5984
5985 (defun gnus-summary-force-verify-and-decrypt ()
5986   (interactive)
5987   (let ((mm-verify-option 'known)
5988         (mm-decrypt-option 'known))
5989     (gnus-summary-select-article nil 'force)))
5990
5991 (defun gnus-summary-set-current-mark (&optional current-mark)
5992   "Obsolete function."
5993   nil)
5994
5995 (defun gnus-summary-next-article (&optional unread subject backward push)
5996   "Select the next article.
5997 If UNREAD, only unread articles are selected.
5998 If SUBJECT, only articles with SUBJECT are selected.
5999 If BACKWARD, the previous article is selected instead of the next."
6000   (interactive "P")
6001   (cond
6002    ;; Is there such an article?
6003    ((and (gnus-summary-search-forward unread subject backward)
6004          (or (gnus-summary-display-article (gnus-summary-article-number))
6005              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6006     (gnus-summary-position-point))
6007    ;; If not, we try the first unread, if that is wanted.
6008    ((and subject
6009          gnus-auto-select-same
6010          (gnus-summary-first-unread-article))
6011     (gnus-summary-position-point)
6012     (gnus-message 6 "Wrapped"))
6013    ;; Try to get next/previous article not displayed in this group.
6014    ((and gnus-auto-extend-newsgroup
6015          (not unread) (not subject))
6016     (gnus-summary-goto-article
6017      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6018      nil (count-lines (point-min) (point))))
6019    ;; Go to next/previous group.
6020    (t
6021     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6022       (gnus-summary-jump-to-group gnus-newsgroup-name))
6023     (let ((cmd last-command-char)
6024           (point
6025            (save-excursion
6026              (set-buffer gnus-group-buffer)
6027              (point)))
6028           (group
6029            (if (eq gnus-keep-same-level 'best)
6030                (gnus-summary-best-group gnus-newsgroup-name)
6031              (gnus-summary-search-group backward gnus-keep-same-level))))
6032       ;; For some reason, the group window gets selected.  We change
6033       ;; it back.
6034       (select-window (get-buffer-window (current-buffer)))
6035       ;; Select next unread newsgroup automagically.
6036       (cond
6037        ((or (not gnus-auto-select-next)
6038             (not cmd))
6039         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6040        ((or (eq gnus-auto-select-next 'quietly)
6041             (and (eq gnus-auto-select-next 'slightly-quietly)
6042                  push)
6043             (and (eq gnus-auto-select-next 'almost-quietly)
6044                  (gnus-summary-last-article-p)))
6045         ;; Select quietly.
6046         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6047             (gnus-summary-exit)
6048           (gnus-message 7 "No more%s articles (%s)..."
6049                         (if unread " unread" "")
6050                         (if group (concat "selecting " group)
6051                           "exiting"))
6052           (gnus-summary-next-group nil group backward)))
6053        (t
6054         (when (gnus-key-press-event-p last-input-event)
6055           (gnus-summary-walk-group-buffer
6056            gnus-newsgroup-name cmd unread backward point))))))))
6057
6058 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6059   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6060                       (?\C-p (gnus-group-prev-unread-group 1))))
6061         (cursor-in-echo-area t)
6062         keve key group ended)
6063     (save-excursion
6064       (set-buffer gnus-group-buffer)
6065       (goto-char start)
6066       (setq group
6067             (if (eq gnus-keep-same-level 'best)
6068                 (gnus-summary-best-group gnus-newsgroup-name)
6069               (gnus-summary-search-group backward gnus-keep-same-level))))
6070     (while (not ended)
6071       (gnus-message
6072        5 "No more%s articles%s" (if unread " unread" "")
6073        (if (and group
6074                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6075            (format " (Type %s for %s [%s])"
6076                    (single-key-description cmd) group
6077                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6078          (format " (Type %s to exit %s)"
6079                  (single-key-description cmd)
6080                  gnus-newsgroup-name)))
6081       ;; Confirm auto selection.
6082       (setq key (car (setq keve (gnus-read-event-char))))
6083       (setq ended t)
6084       (cond
6085        ((assq key keystrokes)
6086         (let ((obuf (current-buffer)))
6087           (switch-to-buffer gnus-group-buffer)
6088           (when group
6089             (gnus-group-jump-to-group group))
6090           (eval (cadr (assq key keystrokes)))
6091           (setq group (gnus-group-group-name))
6092           (switch-to-buffer obuf))
6093         (setq ended nil))
6094        ((equal key cmd)
6095         (if (or (not group)
6096                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6097             (gnus-summary-exit)
6098           (gnus-summary-next-group nil group backward)))
6099        (t
6100         (push (cdr keve) unread-command-events))))))
6101
6102 (defun gnus-summary-next-unread-article ()
6103   "Select unread article after current one."
6104   (interactive)
6105   (gnus-summary-next-article
6106    (or (not (eq gnus-summary-goto-unread 'never))
6107        (gnus-summary-last-article-p (gnus-summary-article-number)))
6108    (and gnus-auto-select-same
6109         (gnus-summary-article-subject))))
6110
6111 (defun gnus-summary-prev-article (&optional unread subject)
6112   "Select the article after the current one.
6113 If UNREAD is non-nil, only unread articles are selected."
6114   (interactive "P")
6115   (gnus-summary-next-article unread subject t))
6116
6117 (defun gnus-summary-prev-unread-article ()
6118   "Select unread article before current one."
6119   (interactive)
6120   (gnus-summary-prev-article
6121    (or (not (eq gnus-summary-goto-unread 'never))
6122        (gnus-summary-first-article-p (gnus-summary-article-number)))
6123    (and gnus-auto-select-same
6124         (gnus-summary-article-subject))))
6125
6126 (defun gnus-summary-next-page (&optional lines circular)
6127   "Show next page of the selected article.
6128 If at the end of the current article, select the next article.
6129 LINES says how many lines should be scrolled up.
6130
6131 If CIRCULAR is non-nil, go to the start of the article instead of
6132 selecting the next article when reaching the end of the current
6133 article."
6134   (interactive "P")
6135   (setq gnus-summary-buffer (current-buffer))
6136   (gnus-set-global-variables)
6137   (let ((article (gnus-summary-article-number))
6138         (article-window (get-buffer-window gnus-article-buffer t))
6139         endp)
6140     ;; If the buffer is empty, we have no article.
6141     (unless article
6142       (error "No article to select"))
6143     (gnus-configure-windows 'article)
6144     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6145         (if (and (eq gnus-summary-goto-unread 'never)
6146                  (not (gnus-summary-last-article-p article)))
6147             (gnus-summary-next-article)
6148           (gnus-summary-next-unread-article))
6149       (if (or (null gnus-current-article)
6150               (null gnus-article-current)
6151               (/= article (cdr gnus-article-current))
6152               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6153           ;; Selected subject is different from current article's.
6154           (gnus-summary-display-article article)
6155         (when article-window
6156           (gnus-eval-in-buffer-window gnus-article-buffer
6157             (setq endp (gnus-article-next-page lines)))
6158           (when endp
6159             (cond (circular
6160                    (gnus-summary-beginning-of-article))
6161                   (lines
6162                    (gnus-message 3 "End of message"))
6163                   ((null lines)
6164                    (if (and (eq gnus-summary-goto-unread 'never)
6165                             (not (gnus-summary-last-article-p article)))
6166                        (gnus-summary-next-article)
6167                      (gnus-summary-next-unread-article))))))))
6168     (gnus-summary-recenter)
6169     (gnus-summary-position-point)))
6170
6171 (defun gnus-summary-prev-page (&optional lines move)
6172   "Show previous page of selected article.
6173 Argument LINES specifies lines to be scrolled down.
6174 If MOVE, move to the previous unread article if point is at
6175 the beginning of the buffer."
6176   (interactive "P")
6177   (let ((article (gnus-summary-article-number))
6178         (article-window (get-buffer-window gnus-article-buffer t))
6179         endp)
6180     (gnus-configure-windows 'article)
6181     (if (or (null gnus-current-article)
6182             (null gnus-article-current)
6183             (/= article (cdr gnus-article-current))
6184             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6185         ;; Selected subject is different from current article's.
6186         (gnus-summary-display-article article)
6187       (gnus-summary-recenter)
6188       (when article-window
6189         (gnus-eval-in-buffer-window gnus-article-buffer
6190           (setq endp (gnus-article-prev-page lines)))
6191         (when (and move endp)
6192           (cond (lines
6193                  (gnus-message 3 "Beginning of message"))
6194                 ((null lines)
6195                  (if (and (eq gnus-summary-goto-unread 'never)
6196                           (not (gnus-summary-first-article-p article)))
6197                      (gnus-summary-prev-article)
6198                    (gnus-summary-prev-unread-article))))))))
6199   (gnus-summary-position-point))
6200
6201 (defun gnus-summary-prev-page-or-article (&optional lines)
6202   "Show previous page of selected article.
6203 Argument LINES specifies lines to be scrolled down.
6204 If at the beginning of the article, go to the next article."
6205   (interactive "P")
6206   (gnus-summary-prev-page lines t))
6207
6208 (defun gnus-summary-scroll-up (lines)
6209   "Scroll up (or down) one line current article.
6210 Argument LINES specifies lines to be scrolled up (or down if negative)."
6211   (interactive "p")
6212   (gnus-configure-windows 'article)
6213   (gnus-summary-show-thread)
6214   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6215     (gnus-eval-in-buffer-window gnus-article-buffer
6216       (cond ((> lines 0)
6217              (when (gnus-article-next-page lines)
6218                (gnus-message 3 "End of message")))
6219             ((< lines 0)
6220              (gnus-article-prev-page (- lines))))))
6221   (gnus-summary-recenter)
6222   (gnus-summary-position-point))
6223
6224 (defun gnus-summary-scroll-down (lines)
6225   "Scroll down (or up) one line current article.
6226 Argument LINES specifies lines to be scrolled down (or up if negative)."
6227   (interactive "p")
6228   (gnus-summary-scroll-up (- lines)))
6229
6230 (defun gnus-summary-next-same-subject ()
6231   "Select next article which has the same subject as current one."
6232   (interactive)
6233   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6234
6235 (defun gnus-summary-prev-same-subject ()
6236   "Select previous article which has the same subject as current one."
6237   (interactive)
6238   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6239
6240 (defun gnus-summary-next-unread-same-subject ()
6241   "Select next unread article which has the same subject as current one."
6242   (interactive)
6243   (gnus-summary-next-article t (gnus-summary-article-subject)))
6244
6245 (defun gnus-summary-prev-unread-same-subject ()
6246   "Select previous unread article which has the same subject as current one."
6247   (interactive)
6248   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6249
6250 (defun gnus-summary-first-unread-article ()
6251   "Select the first unread article.
6252 Return nil if there are no unread articles."
6253   (interactive)
6254   (prog1
6255       (when (gnus-summary-first-subject t)
6256         (gnus-summary-show-thread)
6257         (gnus-summary-first-subject t)
6258         (gnus-summary-display-article (gnus-summary-article-number)))
6259     (gnus-summary-position-point)))
6260
6261 (defun gnus-summary-first-unread-subject ()
6262   "Place the point on the subject line of the first unread article.
6263 Return nil if there are no unread articles."
6264   (interactive)
6265   (prog1
6266       (when (gnus-summary-first-subject t)
6267         (gnus-summary-show-thread)
6268         (gnus-summary-first-subject t))
6269     (gnus-summary-position-point)))
6270
6271 (defun gnus-summary-first-article ()
6272   "Select the first article.
6273 Return nil if there are no articles."
6274   (interactive)
6275   (prog1
6276       (when (gnus-summary-first-subject)
6277         (gnus-summary-show-thread)
6278         (gnus-summary-first-subject)
6279         (gnus-summary-display-article (gnus-summary-article-number)))
6280     (gnus-summary-position-point)))
6281
6282 (defun gnus-summary-best-unread-article ()
6283   "Select the unread article with the highest score."
6284   (interactive)
6285   (let ((best -1000000)
6286         (data gnus-newsgroup-data)
6287         article score)
6288     (while data
6289       (and (gnus-data-unread-p (car data))
6290            (> (setq score
6291                     (gnus-summary-article-score (gnus-data-number (car data))))
6292               best)
6293            (setq best score
6294                  article (gnus-data-number (car data))))
6295       (setq data (cdr data)))
6296     (prog1
6297         (if article
6298             (gnus-summary-goto-article article)
6299           (error "No unread articles"))
6300       (gnus-summary-position-point))))
6301
6302 (defun gnus-summary-last-subject ()
6303   "Go to the last displayed subject line in the group."
6304   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6305     (when article
6306       (gnus-summary-goto-subject article))))
6307
6308 (defun gnus-summary-goto-article (article &optional all-headers force)
6309   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6310 If ALL-HEADERS is non-nil, no header lines are hidden.
6311 If FORCE, go to the article even if it isn't displayed.  If FORCE
6312 is a number, it is the line the article is to be displayed on."
6313   (interactive
6314    (list
6315     (completing-read
6316      "Article number or Message-ID: "
6317      (mapcar (lambda (number) (list (int-to-string number)))
6318              gnus-newsgroup-limit))
6319     current-prefix-arg
6320     t))
6321   (prog1
6322       (if (and (stringp article)
6323                (string-match "@" article))
6324           (gnus-summary-refer-article article)
6325         (when (stringp article)
6326           (setq article (string-to-number article)))
6327         (if (gnus-summary-goto-subject article force)
6328             (gnus-summary-display-article article all-headers)
6329           (gnus-message 4 "Couldn't go to article %s" article) nil))
6330     (gnus-summary-position-point)))
6331
6332 (defun gnus-summary-goto-last-article ()
6333   "Go to the previously read article."
6334   (interactive)
6335   (prog1
6336       (when gnus-last-article
6337         (gnus-summary-goto-article gnus-last-article nil t))
6338     (gnus-summary-position-point)))
6339
6340 (defun gnus-summary-pop-article (number)
6341   "Pop one article off the history and go to the previous.
6342 NUMBER articles will be popped off."
6343   (interactive "p")
6344   (let (to)
6345     (setq gnus-newsgroup-history
6346           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6347     (if to
6348         (gnus-summary-goto-article (car to) nil t)
6349       (error "Article history empty")))
6350   (gnus-summary-position-point))
6351
6352 ;; Summary commands and functions for limiting the summary buffer.
6353
6354 (defun gnus-summary-limit-to-articles (n)
6355   "Limit the summary buffer to the next N articles.
6356 If not given a prefix, use the process marked articles instead."
6357   (interactive "P")
6358   (prog1
6359       (let ((articles (gnus-summary-work-articles n)))
6360         (setq gnus-newsgroup-processable nil)
6361         (gnus-summary-limit articles))
6362     (gnus-summary-position-point)))
6363
6364 (defun gnus-summary-pop-limit (&optional total)
6365   "Restore the previous limit.
6366 If given a prefix, remove all limits."
6367   (interactive "P")
6368   (when total
6369     (setq gnus-newsgroup-limits
6370           (list (mapcar (lambda (h) (mail-header-number h))
6371                         gnus-newsgroup-headers))))
6372   (unless gnus-newsgroup-limits
6373     (error "No limit to pop"))
6374   (prog1
6375       (gnus-summary-limit nil 'pop)
6376     (gnus-summary-position-point)))
6377
6378 (defun gnus-summary-limit-to-subject (subject &optional header)
6379   "Limit the summary buffer to articles that have subjects that match a regexp."
6380   (interactive "sLimit to subject (regexp): ")
6381   (unless header
6382     (setq header "subject"))
6383   (when (not (equal "" subject))
6384     (prog1
6385         (let ((articles (gnus-summary-find-matching
6386                          (or header "subject") subject 'all)))
6387           (unless articles
6388             (error "Found no matches for \"%s\"" subject))
6389           (gnus-summary-limit articles))
6390       (gnus-summary-position-point))))
6391
6392 (defun gnus-summary-limit-to-author (from)
6393   "Limit the summary buffer to articles that have authors that match a regexp."
6394   (interactive "sLimit to author (regexp): ")
6395   (gnus-summary-limit-to-subject from "from"))
6396
6397 (defun gnus-summary-limit-to-age (age &optional younger-p)
6398   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6399 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6400 articles that are younger than AGE days."
6401   (interactive
6402    (let ((younger current-prefix-arg)
6403          (days-got nil)
6404          days)
6405      (while (not days-got)
6406        (setq days (if younger
6407                       (read-string "Limit to articles within (in days): ")
6408                     (read-string "Limit to articles old than (in days): ")))
6409        (when (> (length days) 0)
6410          (setq days (read days)))
6411        (if (numberp days)
6412            (setq days-got t)
6413          (message "Please enter a number.")
6414          (sleep-for 1)))
6415      (list days younger)))
6416   (prog1
6417       (let ((data gnus-newsgroup-data)
6418             (cutoff (days-to-time age))
6419             articles d date is-younger)
6420         (while (setq d (pop data))
6421           (when (and (vectorp (gnus-data-header d))
6422                      (setq date (mail-header-date (gnus-data-header d))))
6423             (setq is-younger (time-less-p
6424                               (time-since (condition-case ()
6425                                               (date-to-time date)
6426                                             (error '(0 0))))
6427                               cutoff))
6428             (when (if younger-p
6429                       is-younger
6430                     (not is-younger))
6431               (push (gnus-data-number d) articles))))
6432         (gnus-summary-limit (nreverse articles)))
6433     (gnus-summary-position-point)))
6434
6435 (defun gnus-summary-limit-to-extra (header regexp)
6436   "Limit the summary buffer to articles that match an 'extra' header."
6437   (interactive
6438    (let ((header
6439           (intern
6440            (gnus-completing-read
6441             (symbol-name (car gnus-extra-headers))
6442             "Limit extra header:"
6443             (mapcar (lambda (x)
6444                       (cons (symbol-name x) x))
6445                     gnus-extra-headers)
6446             nil
6447             t))))
6448      (list header
6449            (read-string (format "Limit to header %s (regexp): " header)))))
6450   (when (not (equal "" regexp))
6451     (prog1
6452         (let ((articles (gnus-summary-find-matching
6453                          (cons 'extra header) regexp 'all)))
6454           (unless articles
6455             (error "Found no matches for \"%s\"" regexp))
6456           (gnus-summary-limit articles))
6457       (gnus-summary-position-point))))
6458
6459 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6460 (make-obsolete
6461  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6462
6463 (defun gnus-summary-limit-to-unread (&optional all)
6464   "Limit the summary buffer to articles that are not marked as read.
6465 If ALL is non-nil, limit strictly to unread articles."
6466   (interactive "P")
6467   (if all
6468       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6469     (gnus-summary-limit-to-marks
6470      ;; Concat all the marks that say that an article is read and have
6471      ;; those removed.
6472      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6473            gnus-killed-mark gnus-kill-file-mark
6474            gnus-low-score-mark gnus-expirable-mark
6475            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6476            gnus-duplicate-mark gnus-souped-mark)
6477      'reverse)))
6478
6479 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6480 (make-obsolete 'gnus-summary-delete-marked-with
6481                'gnus-summary-limit-exlude-marks)
6482
6483 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6484   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6485 If REVERSE, limit the summary buffer to articles that are marked
6486 with MARKS.  MARKS can either be a string of marks or a list of marks.
6487 Returns how many articles were removed."
6488   (interactive "sMarks: ")
6489   (gnus-summary-limit-to-marks marks t))
6490
6491 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6492   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6493 If REVERSE (the prefix), limit the summary buffer to articles that are
6494 not marked with MARKS.  MARKS can either be a string of marks or a
6495 list of marks.
6496 Returns how many articles were removed."
6497   (interactive "sMarks: \nP")
6498   (prog1
6499       (let ((data gnus-newsgroup-data)
6500             (marks (if (listp marks) marks
6501                      (append marks nil))) ; Transform to list.
6502             articles)
6503         (while data
6504           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6505                   (memq (gnus-data-mark (car data)) marks))
6506             (push (gnus-data-number (car data)) articles))
6507           (setq data (cdr data)))
6508         (gnus-summary-limit articles))
6509     (gnus-summary-position-point)))
6510
6511 (defun gnus-summary-limit-to-score (&optional score)
6512   "Limit to articles with score at or above SCORE."
6513   (interactive "P")
6514   (setq score (if score
6515                   (prefix-numeric-value score)
6516                 (or gnus-summary-default-score 0)))
6517   (let ((data gnus-newsgroup-data)
6518         articles)
6519     (while data
6520       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6521                 score)
6522         (push (gnus-data-number (car data)) articles))
6523       (setq data (cdr data)))
6524     (prog1
6525         (gnus-summary-limit articles)
6526       (gnus-summary-position-point))))
6527
6528 (defun gnus-summary-limit-include-thread (id)
6529   "Display all the hidden articles that in the current thread."
6530   (interactive (list (mail-header-id (gnus-summary-article-header))))
6531   (let ((articles (gnus-articles-in-thread
6532                    (gnus-id-to-thread (gnus-root-id id)))))
6533     (prog1
6534         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6535       (gnus-summary-position-point))))
6536
6537 (defun gnus-summary-limit-include-dormant ()
6538   "Display all the hidden articles that are marked as dormant.
6539 Note that this command only works on a subset of the articles currently
6540 fetched for this group."
6541   (interactive)
6542   (unless gnus-newsgroup-dormant
6543     (error "There are no dormant articles in this group"))
6544   (prog1
6545       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6546     (gnus-summary-position-point)))
6547
6548 (defun gnus-summary-limit-exclude-dormant ()
6549   "Hide all dormant articles."
6550   (interactive)
6551   (prog1
6552       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6553     (gnus-summary-position-point)))
6554
6555 (defun gnus-summary-limit-exclude-childless-dormant ()
6556   "Hide all dormant articles that have no children."
6557   (interactive)
6558   (let ((data (gnus-data-list t))
6559         articles d children)
6560     ;; Find all articles that are either not dormant or have
6561     ;; children.
6562     (while (setq d (pop data))
6563       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6564                 (and (setq children
6565                            (gnus-article-children (gnus-data-number d)))
6566                      (let (found)
6567                        (while children
6568                          (when (memq (car children) articles)
6569                            (setq children nil
6570                                  found t))
6571                          (pop children))
6572                        found)))
6573         (push (gnus-data-number d) articles)))
6574     ;; Do the limiting.
6575     (prog1
6576         (gnus-summary-limit articles)
6577       (gnus-summary-position-point))))
6578
6579 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6580   "Mark all unread excluded articles as read.
6581 If ALL, mark even excluded ticked and dormants as read."
6582   (interactive "P")
6583   (let ((articles (gnus-sorted-complement
6584                    (sort
6585                     (mapcar (lambda (h) (mail-header-number h))
6586                             gnus-newsgroup-headers)
6587                     '<)
6588                    (sort gnus-newsgroup-limit '<)))
6589         article)
6590     (setq gnus-newsgroup-unreads
6591           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6592     (if all
6593         (setq gnus-newsgroup-dormant nil
6594               gnus-newsgroup-marked nil
6595               gnus-newsgroup-reads
6596               (nconc
6597                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6598                gnus-newsgroup-reads))
6599       (while (setq article (pop articles))
6600         (unless (or (memq article gnus-newsgroup-dormant)
6601                     (memq article gnus-newsgroup-marked))
6602           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6603
6604 (defun gnus-summary-limit (articles &optional pop)
6605   (if pop
6606       ;; We pop the previous limit off the stack and use that.
6607       (setq articles (car gnus-newsgroup-limits)
6608             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6609     ;; We use the new limit, so we push the old limit on the stack.
6610     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6611   ;; Set the limit.
6612   (setq gnus-newsgroup-limit articles)
6613   (let ((total (length gnus-newsgroup-data))
6614         (data (gnus-data-find-list (gnus-summary-article-number)))
6615         (gnus-summary-mark-below nil)   ; Inhibit this.
6616         found)
6617     ;; This will do all the work of generating the new summary buffer
6618     ;; according to the new limit.
6619     (gnus-summary-prepare)
6620     ;; Hide any threads, possibly.
6621     (and gnus-show-threads
6622          gnus-thread-hide-subtree
6623          (gnus-summary-hide-all-threads))
6624     ;; Try to return to the article you were at, or one in the
6625     ;; neighborhood.
6626     (when data
6627       ;; We try to find some article after the current one.
6628       (while data
6629         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6630           (setq data nil
6631                 found t))
6632         (setq data (cdr data))))
6633     (unless found
6634       ;; If there is no data, that means that we were after the last
6635       ;; article.  The same goes when we can't find any articles
6636       ;; after the current one.
6637       (goto-char (point-max))
6638       (gnus-summary-find-prev))
6639     (gnus-set-mode-line 'summary)
6640     ;; We return how many articles were removed from the summary
6641     ;; buffer as a result of the new limit.
6642     (- total (length gnus-newsgroup-data))))
6643
6644 (defsubst gnus-invisible-cut-children (threads)
6645   (let ((num 0))
6646     (while threads
6647       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6648         (incf num))
6649       (pop threads))
6650     (< num 2)))
6651
6652 (defsubst gnus-cut-thread (thread)
6653   "Go forwards in the thread until we find an article that we want to display."
6654   (when (or (eq gnus-fetch-old-headers 'some)
6655             (eq gnus-fetch-old-headers 'invisible)
6656             (numberp gnus-fetch-old-headers)
6657             (eq gnus-build-sparse-threads 'some)
6658             (eq gnus-build-sparse-threads 'more))
6659     ;; Deal with old-fetched headers and sparse threads.
6660     (while (and
6661             thread
6662             (or
6663              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6664              (gnus-summary-article-ancient-p
6665               (mail-header-number (car thread))))
6666             (if (or (<= (length (cdr thread)) 1)
6667                     (eq gnus-fetch-old-headers 'invisible))
6668                 (setq gnus-newsgroup-limit
6669                       (delq (mail-header-number (car thread))
6670                             gnus-newsgroup-limit)
6671                       thread (cadr thread))
6672               (when (gnus-invisible-cut-children (cdr thread))
6673                 (let ((th (cdr thread)))
6674                   (while th
6675                     (if (memq (mail-header-number (caar th))
6676                               gnus-newsgroup-limit)
6677                         (setq thread (car th)
6678                               th nil)
6679                       (setq th (cdr th))))))))))
6680   thread)
6681
6682 (defun gnus-cut-threads (threads)
6683   "Cut off all uninteresting articles from the beginning of threads."
6684   (when (or (eq gnus-fetch-old-headers 'some)
6685             (eq gnus-fetch-old-headers 'invisible)
6686             (numberp gnus-fetch-old-headers)
6687             (eq gnus-build-sparse-threads 'some)
6688             (eq gnus-build-sparse-threads 'more))
6689     (let ((th threads))
6690       (while th
6691         (setcar th (gnus-cut-thread (car th)))
6692         (setq th (cdr th)))))
6693   ;; Remove nixed out threads.
6694   (delq nil threads))
6695
6696 (defun gnus-summary-initial-limit (&optional show-if-empty)
6697   "Figure out what the initial limit is supposed to be on group entry.
6698 This entails weeding out unwanted dormants, low-scored articles,
6699 fetch-old-headers verbiage, and so on."
6700   ;; Most groups have nothing to remove.
6701   (if (or gnus-inhibit-limiting
6702           (and (null gnus-newsgroup-dormant)
6703                (not (eq gnus-fetch-old-headers 'some))
6704                (not (numberp gnus-fetch-old-headers))
6705                (not (eq gnus-fetch-old-headers 'invisible))
6706                (null gnus-summary-expunge-below)
6707                (not (eq gnus-build-sparse-threads 'some))
6708                (not (eq gnus-build-sparse-threads 'more))
6709                (null gnus-thread-expunge-below)
6710                (not gnus-use-nocem)))
6711       ()                                ; Do nothing.
6712     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6713     (setq gnus-newsgroup-limit nil)
6714     (mapatoms
6715      (lambda (node)
6716        (unless (car (symbol-value node))
6717          ;; These threads have no parents -- they are roots.
6718          (let ((nodes (cdr (symbol-value node)))
6719                thread)
6720            (while nodes
6721              (if (and gnus-thread-expunge-below
6722                       (< (gnus-thread-total-score (car nodes))
6723                          gnus-thread-expunge-below))
6724                  (gnus-expunge-thread (pop nodes))
6725                (setq thread (pop nodes))
6726                (gnus-summary-limit-children thread))))))
6727      gnus-newsgroup-dependencies)
6728     ;; If this limitation resulted in an empty group, we might
6729     ;; pop the previous limit and use it instead.
6730     (when (and (not gnus-newsgroup-limit)
6731                show-if-empty)
6732       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6733     gnus-newsgroup-limit))
6734
6735 (defun gnus-summary-limit-children (thread)
6736   "Return 1 if this subthread is visible and 0 if it is not."
6737   ;; First we get the number of visible children to this thread.  This
6738   ;; is done by recursing down the thread using this function, so this
6739   ;; will really go down to a leaf article first, before slowly
6740   ;; working its way up towards the root.
6741   (when thread
6742     (let ((children
6743            (if (cdr thread)
6744                (apply '+ (mapcar 'gnus-summary-limit-children
6745                                  (cdr thread)))
6746              0))
6747           (number (mail-header-number (car thread)))
6748           score)
6749       (if (and
6750            (not (memq number gnus-newsgroup-marked))
6751            (or
6752             ;; If this article is dormant and has absolutely no visible
6753             ;; children, then this article isn't visible.
6754             (and (memq number gnus-newsgroup-dormant)
6755                  (zerop children))
6756             ;; If this is "fetch-old-headered" and there is no
6757             ;; visible children, then we don't want this article.
6758             (and (or (eq gnus-fetch-old-headers 'some)
6759                      (numberp gnus-fetch-old-headers))
6760                  (gnus-summary-article-ancient-p number)
6761                  (zerop children))
6762             ;; If this is "fetch-old-headered" and `invisible', then
6763             ;; we don't want this article.
6764             (and (eq gnus-fetch-old-headers 'invisible)
6765                  (gnus-summary-article-ancient-p number))
6766             ;; If this is a sparsely inserted article with no children,
6767             ;; we don't want it.
6768             (and (eq gnus-build-sparse-threads 'some)
6769                  (gnus-summary-article-sparse-p number)
6770                  (zerop children))
6771             ;; If we use expunging, and this article is really
6772             ;; low-scored, then we don't want this article.
6773             (when (and gnus-summary-expunge-below
6774                        (< (setq score
6775                                 (or (cdr (assq number gnus-newsgroup-scored))
6776                                     gnus-summary-default-score))
6777                           gnus-summary-expunge-below))
6778               ;; We increase the expunge-tally here, but that has
6779               ;; nothing to do with the limits, really.
6780               (incf gnus-newsgroup-expunged-tally)
6781               ;; We also mark as read here, if that's wanted.
6782               (when (and gnus-summary-mark-below
6783                          (< score gnus-summary-mark-below))
6784                 (setq gnus-newsgroup-unreads
6785                       (delq number gnus-newsgroup-unreads))
6786                 (if gnus-newsgroup-auto-expire
6787                     (push number gnus-newsgroup-expirable)
6788                   (push (cons number gnus-low-score-mark)
6789                         gnus-newsgroup-reads)))
6790               t)
6791             ;; Check NoCeM things.
6792             (if (and gnus-use-nocem
6793                      (gnus-nocem-unwanted-article-p
6794                       (mail-header-id (car thread))))
6795                 (progn
6796                   (setq gnus-newsgroup-unreads
6797                         (delq number gnus-newsgroup-unreads))
6798                   t))))
6799           ;; Nope, invisible article.
6800           0
6801         ;; Ok, this article is to be visible, so we add it to the limit
6802         ;; and return 1.
6803         (push number gnus-newsgroup-limit)
6804         1))))
6805
6806 (defun gnus-expunge-thread (thread)
6807   "Mark all articles in THREAD as read."
6808   (let* ((number (mail-header-number (car thread))))
6809     (incf gnus-newsgroup-expunged-tally)
6810     ;; We also mark as read here, if that's wanted.
6811     (setq gnus-newsgroup-unreads
6812           (delq number gnus-newsgroup-unreads))
6813     (if gnus-newsgroup-auto-expire
6814         (push number gnus-newsgroup-expirable)
6815       (push (cons number gnus-low-score-mark)
6816             gnus-newsgroup-reads)))
6817   ;; Go recursively through all subthreads.
6818   (mapcar 'gnus-expunge-thread (cdr thread)))
6819
6820 ;; Summary article oriented commands
6821
6822 (defun gnus-summary-refer-parent-article (n)
6823   "Refer parent article N times.
6824 If N is negative, go to ancestor -N instead.
6825 The difference between N and the number of articles fetched is returned."
6826   (interactive "p")
6827   (let ((skip 1)
6828         error header ref)
6829     (when (not (natnump n))
6830       (setq skip (abs n)
6831             n 1))
6832     (while (and (> n 0)
6833                 (not error))
6834       (setq header (gnus-summary-article-header))
6835       (if (and (eq (mail-header-number header)
6836                    (cdr gnus-article-current))
6837                (equal gnus-newsgroup-name
6838                       (car gnus-article-current)))
6839           ;; If we try to find the parent of the currently
6840           ;; displayed article, then we take a look at the actual
6841           ;; References header, since this is slightly more
6842           ;; reliable than the References field we got from the
6843           ;; server.
6844           (save-excursion
6845             (set-buffer gnus-original-article-buffer)
6846             (nnheader-narrow-to-headers)
6847             (unless (setq ref (message-fetch-field "references"))
6848               (setq ref (message-fetch-field "in-reply-to")))
6849             (widen))
6850         (setq ref
6851               ;; It's not the current article, so we take a bet on
6852               ;; the value we got from the server.
6853               (mail-header-references header)))
6854       (if (and ref
6855                (not (equal ref "")))
6856           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6857             (gnus-message 1 "Couldn't find parent"))
6858         (gnus-message 1 "No references in article %d"
6859                       (gnus-summary-article-number))
6860         (setq error t))
6861       (decf n))
6862     (gnus-summary-position-point)
6863     n))
6864
6865 (defun gnus-summary-refer-references ()
6866   "Fetch all articles mentioned in the References header.
6867 Return the number of articles fetched."
6868   (interactive)
6869   (let ((ref (mail-header-references (gnus-summary-article-header)))
6870         (current (gnus-summary-article-number))
6871         (n 0))
6872     (if (or (not ref)
6873             (equal ref ""))
6874         (error "No References in the current article")
6875       ;; For each Message-ID in the References header...
6876       (while (string-match "<[^>]*>" ref)
6877         (incf n)
6878         ;; ... fetch that article.
6879         (gnus-summary-refer-article
6880          (prog1 (match-string 0 ref)
6881            (setq ref (substring ref (match-end 0))))))
6882       (gnus-summary-goto-subject current)
6883       (gnus-summary-position-point)
6884       n)))
6885
6886 (defun gnus-summary-refer-thread (&optional limit)
6887   "Fetch all articles in the current thread.
6888 If LIMIT (the numerical prefix), fetch that many old headers instead
6889 of what's specified by the `gnus-refer-thread-limit' variable."
6890   (interactive "P")
6891   (let ((id (mail-header-id (gnus-summary-article-header)))
6892         (limit (if limit (prefix-numeric-value limit)
6893                  gnus-refer-thread-limit)))
6894     ;; We want to fetch LIMIT *old* headers, but we also have to
6895     ;; re-fetch all the headers in the current buffer, because many of
6896     ;; them may be undisplayed.  So we adjust LIMIT.
6897     (when (numberp limit)
6898       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6899     (unless (eq gnus-fetch-old-headers 'invisible)
6900       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6901       ;; Retrieve the headers and read them in.
6902       (if (eq (gnus-retrieve-headers
6903                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6904               'nov)
6905           (gnus-build-all-threads)
6906         (error "Can't fetch thread from backends that don't support NOV"))
6907       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6908     (gnus-summary-limit-include-thread id)))
6909
6910 (defun gnus-summary-refer-article (message-id)
6911   "Fetch an article specified by MESSAGE-ID."
6912   (interactive "sMessage-ID: ")
6913   (when (and (stringp message-id)
6914              (not (zerop (length message-id))))
6915     ;; Construct the correct Message-ID if necessary.
6916     ;; Suggested by tale@pawl.rpi.edu.
6917     (unless (string-match "^<" message-id)
6918       (setq message-id (concat "<" message-id)))
6919     (unless (string-match ">$" message-id)
6920       (setq message-id (concat message-id ">")))
6921     (let* ((header (gnus-id-to-header message-id))
6922            (sparse (and header
6923                         (gnus-summary-article-sparse-p
6924                          (mail-header-number header))
6925                         (memq (mail-header-number header)
6926                               gnus-newsgroup-limit)))
6927            number)
6928       (cond
6929        ;; If the article is present in the buffer we just go to it.
6930        ((and header
6931              (or (not (gnus-summary-article-sparse-p
6932                        (mail-header-number header)))
6933                  sparse))
6934         (prog1
6935             (gnus-summary-goto-article
6936              (mail-header-number header) nil t)
6937           (when sparse
6938             (gnus-summary-update-article (mail-header-number header)))))
6939        (t
6940         ;; We fetch the article.
6941         (catch 'found
6942           (dolist (gnus-override-method (gnus-refer-article-methods))
6943             (gnus-check-server gnus-override-method)
6944             ;; Fetch the header, and display the article.
6945             (when (setq number (gnus-summary-insert-subject message-id))
6946               (gnus-summary-select-article nil nil nil number)
6947               (throw 'found t)))
6948           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6949
6950 (defun gnus-refer-article-methods ()
6951   "Return a list of referrable methods."
6952   (cond
6953    ;; No method, so we default to current and native.
6954    ((null gnus-refer-article-method)
6955     (list gnus-current-select-method gnus-select-method))
6956    ;; Current.
6957    ((eq 'current gnus-refer-article-method)
6958     (list gnus-current-select-method))
6959    ;; List of select methods.
6960    ((not (and (symbolp (car gnus-refer-article-method))
6961               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6962     (let (out)
6963       (dolist (method gnus-refer-article-method)
6964         (push (if (eq 'current method)
6965                   gnus-current-select-method
6966                 method)
6967               out))
6968       (nreverse out)))
6969    ;; One single select method.
6970    (t
6971     (list gnus-refer-article-method))))
6972
6973 (defun gnus-summary-edit-parameters ()
6974   "Edit the group parameters of the current group."
6975   (interactive)
6976   (gnus-group-edit-group gnus-newsgroup-name 'params))
6977
6978 (defun gnus-summary-customize-parameters ()
6979   "Customize the group parameters of the current group."
6980   (interactive)
6981   (gnus-group-customize gnus-newsgroup-name))
6982
6983 (defun gnus-summary-enter-digest-group (&optional force)
6984   "Enter an nndoc group based on the current article.
6985 If FORCE, force a digest interpretation.  If not, try
6986 to guess what the document format is."
6987   (interactive "P")
6988   (let ((conf gnus-current-window-configuration))
6989     (save-excursion
6990       (gnus-summary-select-article))
6991     (setq gnus-current-window-configuration conf)
6992     (let* ((name (format "%s-%d"
6993                          (gnus-group-prefixed-name
6994                           gnus-newsgroup-name (list 'nndoc ""))
6995                          (save-excursion
6996                            (set-buffer gnus-summary-buffer)
6997                            gnus-current-article)))
6998            (ogroup gnus-newsgroup-name)
6999            (params (append (gnus-info-params (gnus-get-info ogroup))
7000                            (list (cons 'to-group ogroup))
7001                            (list (cons 'save-article-group ogroup))))
7002            (case-fold-search t)
7003            (buf (current-buffer))
7004            dig to-address)
7005       (save-excursion
7006         (set-buffer gnus-original-article-buffer)
7007         ;; Have the digest group inherit the main mail address of
7008         ;; the parent article.
7009         (when (setq to-address (or (message-fetch-field "reply-to")
7010                                    (message-fetch-field "from")))
7011           (setq params (append 
7012                         (list (cons 'to-address 
7013                                     (funcall gnus-decode-encoded-word-function
7014                                              to-address))))))
7015         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7016         (insert-buffer-substring gnus-original-article-buffer)
7017         ;; Remove lines that may lead nndoc to misinterpret the
7018         ;; document type.
7019         (narrow-to-region
7020          (goto-char (point-min))
7021          (or (search-forward "\n\n" nil t) (point)))
7022         (goto-char (point-min))
7023         (delete-matching-lines "^Path:\\|^From ")
7024         (widen))
7025       (unwind-protect
7026           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7027                     (gnus-newsgroup-ephemeral-ignored-charsets
7028                      gnus-newsgroup-ignored-charsets))
7029                 (gnus-group-read-ephemeral-group
7030                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7031                               (nndoc-article-type
7032                                ,(if force 'mbox 'guess))) t))
7033               ;; Make all postings to this group go to the parent group.
7034               (nconc (gnus-info-params (gnus-get-info name))
7035                      params)
7036             ;; Couldn't select this doc group.
7037             (switch-to-buffer buf)
7038             (gnus-set-global-variables)
7039             (gnus-configure-windows 'summary)
7040             (gnus-message 3 "Article couldn't be entered?"))
7041         (kill-buffer dig)))))
7042
7043 (defun gnus-summary-read-document (n)
7044   "Open a new group based on the current article(s).
7045 This will allow you to read digests and other similar
7046 documents as newsgroups.
7047 Obeys the standard process/prefix convention."
7048   (interactive "P")
7049   (let* ((articles (gnus-summary-work-articles n))
7050          (ogroup gnus-newsgroup-name)
7051          (params (append (gnus-info-params (gnus-get-info ogroup))
7052                          (list (cons 'to-group ogroup))))
7053          article group egroup groups vgroup)
7054     (while (setq article (pop articles))
7055       (setq group (format "%s-%d" gnus-newsgroup-name article))
7056       (gnus-summary-remove-process-mark article)
7057       (when (gnus-summary-display-article article)
7058         (save-excursion
7059           (with-temp-buffer
7060             (insert-buffer-substring gnus-original-article-buffer)
7061             ;; Remove some headers that may lead nndoc to make
7062             ;; the wrong guess.
7063             (message-narrow-to-head)
7064             (goto-char (point-min))
7065             (delete-matching-lines "^\\(Path\\):\\|^From ")
7066             (widen)
7067             (if (setq egroup
7068                       (gnus-group-read-ephemeral-group
7069                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7070                                      (nndoc-article-type guess))
7071                        t nil t))
7072                 (progn
7073                   ;; Make all postings to this group go to the parent group.
7074                   (nconc (gnus-info-params (gnus-get-info egroup))
7075                          params)
7076                   (push egroup groups))
7077               ;; Couldn't select this doc group.
7078               (gnus-error 3 "Article couldn't be entered"))))))
7079     ;; Now we have selected all the documents.
7080     (cond
7081      ((not groups)
7082       (error "None of the articles could be interpreted as documents"))
7083      ((gnus-group-read-ephemeral-group
7084        (setq vgroup (format
7085                      "nnvirtual:%s-%s" gnus-newsgroup-name
7086                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7087        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7088        t
7089        (cons (current-buffer) 'summary)))
7090      (t
7091       (error "Couldn't select virtual nndoc group")))))
7092
7093 (defun gnus-summary-isearch-article (&optional regexp-p)
7094   "Do incremental search forward on the current article.
7095 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7096   (interactive "P")
7097   (let* ((gnus-inhibit-treatment t)
7098          (old (gnus-summary-select-article)))
7099     (gnus-configure-windows 'article)
7100     (gnus-eval-in-buffer-window gnus-article-buffer
7101       (save-restriction
7102         (widen)
7103         (when (eq 'old old)
7104           (gnus-article-show-all-headers))
7105         (goto-char (point-min))
7106         (isearch-forward regexp-p)))))
7107
7108 (defun gnus-summary-search-article-forward (regexp &optional backward)
7109   "Search for an article containing REGEXP forward.
7110 If BACKWARD, search backward instead."
7111   (interactive
7112    (list (read-string
7113           (format "Search article %s (regexp%s): "
7114                   (if current-prefix-arg "backward" "forward")
7115                   (if gnus-last-search-regexp
7116                       (concat ", default " gnus-last-search-regexp)
7117                     "")))
7118          current-prefix-arg))
7119   (if (string-equal regexp "")
7120       (setq regexp (or gnus-last-search-regexp ""))
7121     (setq gnus-last-search-regexp regexp)
7122     (setq gnus-article-before-search gnus-current-article))
7123   ;; Intentionally set gnus-last-article.
7124   (setq gnus-last-article gnus-article-before-search)
7125   (let ((gnus-last-article gnus-last-article))
7126     (if (gnus-summary-search-article regexp backward)
7127         (gnus-summary-show-thread)
7128       (error "Search failed: \"%s\"" regexp))))
7129
7130 (defun gnus-summary-search-article-backward (regexp)
7131   "Search for an article containing REGEXP backward."
7132   (interactive
7133    (list (read-string
7134           (format "Search article backward (regexp%s): "
7135                   (if gnus-last-search-regexp
7136                       (concat ", default " gnus-last-search-regexp)
7137                     "")))))
7138   (gnus-summary-search-article-forward regexp 'backward))
7139
7140 (eval-when-compile
7141   (defmacro gnus-summary-search-article-position-point (regexp backward)
7142     "Dehighlight the last matched text and goto the beginning position."
7143     (` (if (and gnus-summary-search-article-matched-data
7144                 (let ((text (caddr gnus-summary-search-article-matched-data))
7145                       (inhibit-read-only t)
7146                       buffer-read-only)
7147                   (delete-region
7148                    (goto-char (car gnus-summary-search-article-matched-data))
7149                    (cadr gnus-summary-search-article-matched-data))
7150                   (insert text)
7151                   (string-match (, regexp) text)))
7152            (if (, backward) (beginning-of-line) (end-of-line))
7153          (goto-char (if (, backward) (point-max) (point-min))))))
7154
7155   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7156     "Place point where X-Face image is displayed."
7157     (if (featurep 'xemacs)
7158         (` (let ((end (if (search-forward "\n\n" nil t)
7159                           (goto-char (1- (point)))
7160                         (point-min)))
7161                  extent)
7162              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7163              (unless (and (re-search-forward "^From:" end t)
7164                           (setq extent (extent-at (point)))
7165                           (extent-begin-glyph extent))
7166                (goto-char (, opoint)))))
7167       (` (let ((end (if (search-forward "\n\n" nil t)
7168                         (goto-char (1- (point)))
7169                       (point-min))))
7170            (goto-char
7171             (or (text-property-any (or (search-backward "\n\n" nil t)
7172                                        (point-min))
7173                                    end 'x-face-mule-bitmap-image t)
7174                 (, opoint)))))))
7175
7176   (defmacro gnus-summary-search-article-highlight-matched-text
7177     (backward treated x-face)
7178     "Highlight matched text in the function `gnus-summary-search-article'."
7179     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7180              (end (set-marker (make-marker) (match-end 0)))
7181              (inhibit-read-only t)
7182              buffer-read-only)
7183          (unless treated
7184            (let ((,@
7185                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7186                     (mapcar
7187                      (lambda (item) (setq items (delq item items)))
7188                      '(gnus-treat-buttonize
7189                        gnus-treat-fill-article
7190                        gnus-treat-fill-long-lines
7191                        gnus-treat-emphasize
7192                        gnus-treat-highlight-headers
7193                        gnus-treat-highlight-citation
7194                        gnus-treat-highlight-signature
7195                        gnus-treat-overstrike
7196                        gnus-treat-display-xface
7197                        gnus-treat-buttonize-head
7198                        gnus-treat-decode-article-as-default-mime-charset))
7199                     (static-if (featurep 'xemacs)
7200                         items
7201                       (cons '(x-face-mule-delete-x-face-field
7202                               (quote never))
7203                             items))))
7204                  (gnus-treat-display-xface
7205                   (when (, x-face) gnus-treat-display-xface)))
7206              (gnus-article-prepare-mime-display)))
7207          (goto-char (if (, backward) start end))
7208          (when (, x-face)
7209            (gnus-summary-search-article-highlight-goto-x-face (point)))
7210          (setq gnus-summary-search-article-matched-data
7211                (list start end (buffer-substring start end)))
7212          (unless (eq start end);; matched text has been deleted. :-<
7213            (put-text-property start end 'face
7214                               (or (find-face 'isearch)
7215                                   'secondary-selection))))))
7216   )
7217
7218 (defun gnus-summary-search-article (regexp &optional backward)
7219   "Search for an article containing REGEXP.
7220 Optional argument BACKWARD means do search for backward.
7221 `gnus-select-article-hook' is not called during the search."
7222   ;; We have to require this here to make sure that the following
7223   ;; dynamic binding isn't shadowed by autoloading.
7224   (require 'gnus-async)
7225   (require 'gnus-art)
7226   (let ((gnus-select-article-hook nil)  ;Disable hook.
7227         (gnus-article-display-hook nil)
7228         (gnus-article-prepare-hook nil)
7229         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7230         (gnus-use-article-prefetch nil)
7231         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7232         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7233         (sum (current-buffer))
7234         (found nil)
7235         point treated)
7236     (gnus-save-hidden-threads
7237       (static-if (featurep 'xemacs)
7238           (let ((gnus-inhibit-treatment t))
7239             (setq treated (eq 'old (gnus-summary-select-article)))
7240             (when (and treated
7241                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7242                                  (window-live-p (get-buffer-window
7243                                                  gnus-article-buffer t)))))
7244               (gnus-summary-select-article nil t)
7245               (setq treated nil)))
7246         (let ((gnus-inhibit-treatment t)
7247               (x-face-mule-delete-x-face-field 'never))
7248           (setq treated (eq 'old (gnus-summary-select-article)))
7249           (when (and treated
7250                      (not
7251                       (and (gnus-buffer-live-p gnus-article-buffer)
7252                            (window-live-p (get-buffer-window
7253                                            gnus-article-buffer t))
7254                            (or (not (string-match "^\\^X-Face:" regexp))
7255                                (with-current-buffer gnus-article-buffer
7256                                  gnus-summary-search-article-matched-data)))))
7257             (gnus-summary-select-article nil t)
7258             (setq treated nil))))
7259       (set-buffer gnus-article-buffer)
7260       (widen)
7261       (if treated
7262           (progn
7263             (gnus-article-show-all-headers)
7264             (gnus-summary-search-article-position-point regexp backward))
7265         (goto-char (if backward (point-max) (point-min))))
7266       (while (not found)
7267         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7268         (if (if backward
7269                 (re-search-backward regexp nil t)
7270               (re-search-forward regexp nil t))
7271             ;; We found the regexp.
7272             (progn
7273               (gnus-summary-search-article-highlight-matched-text
7274                backward treated (string-match "^\\^X-Face:" regexp))
7275               (setq found 'found)
7276               (forward-line
7277                (/ (- 2 (window-height
7278                         (get-buffer-window gnus-article-buffer t)))
7279                   2))
7280               (set-window-start
7281                (get-buffer-window (current-buffer))
7282                (point))
7283               (set-buffer sum)
7284               (setq point (point)))
7285           ;; We didn't find it, so we go to the next article.
7286           (set-buffer sum)
7287           (setq found 'not)
7288           (while (eq found 'not)
7289             (if (not (if backward (gnus-summary-find-prev)
7290                        (gnus-summary-find-next)))
7291                 ;; No more articles.
7292                 (setq found t)
7293               ;; Select the next article and adjust point.
7294               (unless (gnus-summary-article-sparse-p
7295                        (gnus-summary-article-number))
7296                 (setq found nil)
7297                 (let ((gnus-inhibit-treatment t))
7298                   (gnus-summary-select-article))
7299                 (setq treated nil)
7300                 (set-buffer gnus-article-buffer)
7301                 (widen)
7302                 (goto-char (if backward (point-max) (point-min))))))))
7303       (gnus-message 7 ""))
7304     ;; Return whether we found the regexp.
7305     (when (eq found 'found)
7306       (goto-char point)
7307       (gnus-summary-show-thread)
7308       (gnus-summary-goto-subject gnus-current-article)
7309       (gnus-summary-position-point)
7310       t)))
7311
7312 (defun gnus-summary-find-matching (header regexp &optional backward unread
7313                                           not-case-fold)
7314   "Return a list of all articles that match REGEXP on HEADER.
7315 The search stars on the current article and goes forwards unless
7316 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7317 If UNREAD is non-nil, only unread articles will
7318 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7319 in the comparisons."
7320   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7321                 (gnus-data-find-list
7322                  (gnus-summary-article-number) (gnus-data-list backward))))
7323         (case-fold-search (not not-case-fold))
7324         articles d func)
7325     (if (consp header)
7326         (if (eq (car header) 'extra)
7327             (setq func
7328                   `(lambda (h)
7329                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7330                          "")))
7331           (error "%s is an invalid header" header))
7332       (unless (fboundp (intern (concat "mail-header-" header)))
7333         (error "%s is not a valid header" header))
7334       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7335     (while data
7336       (setq d (car data))
7337       (and (or (not unread)             ; We want all articles...
7338                (gnus-data-unread-p d))  ; Or just unreads.
7339            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7340            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7341            (push (gnus-data-number d) articles)) ; Success!
7342       (setq data (cdr data)))
7343     (nreverse articles)))
7344
7345 (defun gnus-summary-execute-command (header regexp command &optional backward)
7346   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7347 If HEADER is an empty string (or nil), the match is done on the entire
7348 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7349   (interactive
7350    (list (let ((completion-ignore-case t))
7351            (completing-read
7352             "Header name: "
7353             (mapcar (lambda (string) (list string))
7354                     '("Number" "Subject" "From" "Lines" "Date"
7355                       "Message-ID" "Xref" "References" "Body"))
7356             nil 'require-match))
7357          (read-string "Regexp: ")
7358          (read-key-sequence "Command: ")
7359          current-prefix-arg))
7360   (when (equal header "Body")
7361     (setq header ""))
7362   ;; Hidden thread subtrees must be searched as well.
7363   (gnus-summary-show-all-threads)
7364   ;; We don't want to change current point nor window configuration.
7365   (save-excursion
7366     (save-window-excursion
7367       (gnus-message 6 "Executing %s..." (key-description command))
7368       ;; We'd like to execute COMMAND interactively so as to give arguments.
7369       (gnus-execute header regexp
7370                     `(call-interactively ',(key-binding command))
7371                     backward)
7372       (gnus-message 6 "Executing %s...done" (key-description command)))))
7373
7374 (defun gnus-summary-beginning-of-article ()
7375   "Scroll the article back to the beginning."
7376   (interactive)
7377   (gnus-summary-select-article)
7378   (gnus-configure-windows 'article)
7379   (gnus-eval-in-buffer-window gnus-article-buffer
7380     (widen)
7381     (goto-char (point-min))
7382     (when gnus-page-broken
7383       (gnus-narrow-to-page))))
7384
7385 (defun gnus-summary-end-of-article ()
7386   "Scroll to the end of the article."
7387   (interactive)
7388   (gnus-summary-select-article)
7389   (gnus-configure-windows 'article)
7390   (gnus-eval-in-buffer-window gnus-article-buffer
7391     (widen)
7392     (goto-char (point-max))
7393     (recenter -3)
7394     (when gnus-page-broken
7395       (gnus-narrow-to-page))))
7396
7397 (defun gnus-summary-print-article (&optional filename n)
7398   "Generate and print a PostScript image of the N next (mail) articles.
7399
7400 If N is negative, print the N previous articles.  If N is nil and articles
7401 have been marked with the process mark, print these instead.
7402
7403 If the optional first argument FILENAME is nil, send the image to the
7404 printer.  If FILENAME is a string, save the PostScript image in a file with
7405 that name.  If FILENAME is a number, prompt the user for the name of the file
7406 to save in."
7407   (interactive (list (ps-print-preprint current-prefix-arg)
7408                      current-prefix-arg))
7409   (dolist (article (gnus-summary-work-articles n))
7410     (gnus-summary-select-article nil nil 'pseudo article)
7411     (gnus-eval-in-buffer-window gnus-article-buffer
7412       (let ((buffer (generate-new-buffer " *print*")))
7413         (unwind-protect
7414             (progn
7415               (copy-to-buffer buffer (point-min) (point-max))
7416               (set-buffer buffer)
7417               (gnus-article-delete-invisible-text)
7418               (let ((ps-left-header
7419                      (list
7420                       (concat "("
7421                               (mail-header-subject gnus-current-headers) ")")
7422                       (concat "("
7423                               (mail-header-from gnus-current-headers) ")")))
7424                     (ps-right-header
7425                      (list
7426                       "/pagenumberstring load"
7427                       (concat "("
7428                               (mail-header-date gnus-current-headers) ")"))))
7429                 (gnus-run-hooks 'gnus-ps-print-hook)
7430                 (save-excursion
7431                   (ps-print-buffer-with-faces filename))))
7432           (kill-buffer buffer))))))
7433
7434 (defun gnus-summary-show-article (&optional arg)
7435   "Force re-fetching of the current article.
7436 If ARG (the prefix) is a number, show the article with the charset
7437 defined in `gnus-summary-show-article-charset-alist', or the charset
7438 inputed.
7439 If ARG (the prefix) is non-nil and not a number, show the raw article
7440 without any article massaging functions being run."
7441   (interactive "P")
7442   (cond
7443    ((numberp arg)
7444     (let ((gnus-newsgroup-charset
7445            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7446                (read-coding-system "Charset: ")))
7447           (gnus-newsgroup-ignored-charsets 'gnus-all))
7448       (gnus-summary-select-article nil 'force)
7449       (let ((deps gnus-newsgroup-dependencies)
7450             head header)
7451         (save-excursion
7452           (set-buffer gnus-original-article-buffer)
7453           (save-restriction
7454             (message-narrow-to-head)
7455             (setq head (buffer-string)))
7456           (with-temp-buffer
7457             (insert (format "211 %d Article retrieved.\n"
7458                             (cdr gnus-article-current)))
7459             (insert head)
7460             (insert ".\n")
7461             (let ((nntp-server-buffer (current-buffer)))
7462               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7463         (gnus-data-set-header
7464          (gnus-data-find (cdr gnus-article-current))
7465          header)
7466         (gnus-summary-update-article-line
7467          (cdr gnus-article-current) header))))
7468    ((not arg)
7469     ;; Select the article the normal way.
7470     (gnus-summary-select-article nil 'force))
7471    (t
7472     ;; We have to require this here to make sure that the following
7473     ;; dynamic binding isn't shadowed by autoloading.
7474     (require 'gnus-async)
7475     (require 'gnus-art)
7476     ;; Bind the article treatment functions to nil.
7477     (let ((gnus-have-all-headers t)
7478           gnus-article-display-hook
7479           gnus-article-prepare-hook
7480           gnus-article-decode-hook
7481           gnus-break-pages
7482           gnus-show-mime)
7483       (gnus-summary-select-article nil 'force))))
7484   (gnus-summary-goto-subject gnus-current-article)
7485   (gnus-summary-position-point))
7486
7487 (defun gnus-summary-verbose-headers (&optional arg)
7488   "Toggle permanent full header display.
7489 If ARG is a positive number, turn header display on.
7490 If ARG is a negative number, turn header display off."
7491   (interactive "P")
7492   (setq gnus-show-all-headers
7493         (cond ((or (not (numberp arg))
7494                    (zerop arg))
7495                (not gnus-show-all-headers))
7496               ((natnump arg)
7497                t)))
7498   (gnus-summary-show-article))
7499
7500 (defun gnus-summary-toggle-header (&optional arg)
7501   "Show the headers if they are hidden, or hide them if they are shown.
7502 If ARG is a positive number, show the entire header.
7503 If ARG is a negative number, hide the unwanted header lines."
7504   (interactive "P")
7505   (save-excursion
7506     (set-buffer gnus-article-buffer)
7507     (save-restriction
7508       (let* ((buffer-read-only nil)
7509              (inhibit-point-motion-hooks t)
7510              hidden e)
7511         (setq hidden
7512               (if (numberp arg)
7513                   (>= arg 0)
7514                 (save-restriction
7515                   (article-narrow-to-head)
7516                   (gnus-article-hidden-text-p 'headers))))
7517         (goto-char (point-min))
7518         (when (search-forward "\n\n" nil t)
7519           (delete-region (point-min) (1- (point))))
7520         (goto-char (point-min))
7521         (save-excursion
7522           (set-buffer gnus-original-article-buffer)
7523           (goto-char (point-min))
7524           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7525         (insert-buffer-substring gnus-original-article-buffer 1 e)
7526         (save-restriction
7527           (narrow-to-region (point-min) (point))
7528           (article-decode-encoded-words)
7529           (if  hidden
7530               (let ((gnus-treat-hide-headers nil)
7531                     (gnus-treat-hide-boring-headers nil))
7532                 (setq gnus-article-wash-types
7533                       (delq 'headers gnus-article-wash-types))
7534                 (gnus-treat-article 'head))
7535             (gnus-treat-article 'head)))
7536         (gnus-set-mode-line 'article)))))
7537
7538 (defun gnus-summary-show-all-headers ()
7539   "Make all header lines visible."
7540   (interactive)
7541   (gnus-article-show-all-headers))
7542
7543 (defun gnus-summary-toggle-mime (&optional arg)
7544   "Toggle MIME processing.
7545 If ARG is a positive number, turn MIME processing on."
7546   (interactive "P")
7547   (setq gnus-show-mime
7548         (if (null arg)
7549             (not gnus-show-mime)
7550           (> (prefix-numeric-value arg) 0)))
7551   (gnus-summary-select-article t 'force))
7552
7553 (defun gnus-summary-caesar-message (&optional arg)
7554   "Caesar rotate the current article by 13.
7555 The numerical prefix specifies how many places to rotate each letter
7556 forward."
7557   (interactive "P")
7558   (gnus-summary-select-article)
7559   (let ((mail-header-separator ""))
7560     (gnus-eval-in-buffer-window gnus-article-buffer
7561       (save-restriction
7562         (widen)
7563         (let ((start (window-start))
7564               buffer-read-only)
7565           (message-caesar-buffer-body arg)
7566           (set-window-start (get-buffer-window (current-buffer)) start))))))
7567
7568 (defun gnus-summary-stop-page-breaking ()
7569   "Stop page breaking in the current article."
7570   (interactive)
7571   (gnus-summary-select-article)
7572   (gnus-eval-in-buffer-window gnus-article-buffer
7573     (widen)
7574     (when (gnus-visual-p 'page-marker)
7575       (let ((buffer-read-only nil))
7576         (gnus-remove-text-with-property 'gnus-prev)
7577         (gnus-remove-text-with-property 'gnus-next))
7578       (setq gnus-page-broken nil))))
7579
7580 (defun gnus-summary-move-article (&optional n to-newsgroup
7581                                             select-method action)
7582   "Move the current article to a different newsgroup.
7583 If N is a positive number, move the N next articles.
7584 If N is a negative number, move the N previous articles.
7585 If N is nil and any articles have been marked with the process mark,
7586 move those articles instead.
7587 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7588 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7589 re-spool using this method.
7590
7591 For this function to work, both the current newsgroup and the
7592 newsgroup that you want to move to have to support the `request-move'
7593 and `request-accept' functions.
7594
7595 ACTION can be either `move' (the default), `crosspost' or `copy'."
7596   (interactive "P")
7597   (unless action
7598     (setq action 'move))
7599   ;; Disable marking as read.
7600   (let (gnus-mark-article-hook)
7601     (save-window-excursion
7602       (gnus-summary-select-article)))
7603   ;; Check whether the source group supports the required functions.
7604   (cond ((and (eq action 'move)
7605               (not (gnus-check-backend-function
7606                     'request-move-article gnus-newsgroup-name)))
7607          (error "The current group does not support article moving"))
7608         ((and (eq action 'crosspost)
7609               (not (gnus-check-backend-function
7610                     'request-replace-article gnus-newsgroup-name)))
7611          (error "The current group does not support article editing")))
7612   (let ((articles (gnus-summary-work-articles n))
7613         (prefix (if (gnus-check-backend-function
7614                     'request-move-article gnus-newsgroup-name)
7615                     (gnus-group-real-prefix gnus-newsgroup-name)
7616                   ""))
7617         (names '((move "Move" "Moving")
7618                  (copy "Copy" "Copying")
7619                  (crosspost "Crosspost" "Crossposting")))
7620         (copy-buf (save-excursion
7621                     (nnheader-set-temp-buffer " *copy article*")))
7622         (default-marks gnus-article-mark-lists)
7623         (no-expire-marks (delete '(expirable . expire)
7624                                  (copy-sequence gnus-article-mark-lists)))
7625         art-group to-method new-xref article to-groups)
7626     (unless (assq action names)
7627       (error "Unknown action %s" action))
7628     ;; Read the newsgroup name.
7629     (when (and (not to-newsgroup)
7630                (not select-method))
7631       (setq to-newsgroup
7632             (gnus-read-move-group-name
7633              (cadr (assq action names))
7634              (symbol-value (intern (format "gnus-current-%s-group" action)))
7635              articles prefix))
7636       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7637     (setq to-method (or select-method
7638                         (gnus-server-to-method
7639                          (gnus-group-method to-newsgroup))))
7640     ;; Check the method we are to move this article to...
7641     (unless (gnus-check-backend-function
7642              'request-accept-article (car to-method))
7643       (error "%s does not support article copying" (car to-method)))
7644     (unless (gnus-check-server to-method)
7645       (error "Can't open server %s" (car to-method)))
7646     (gnus-message 6 "%s to %s: %s..."
7647                   (caddr (assq action names))
7648                   (or (car select-method) to-newsgroup) articles)
7649     (while articles
7650       (setq article (pop articles))
7651       (setq
7652        art-group
7653        (cond
7654         ;; Move the article.
7655         ((eq action 'move)
7656          ;; Remove this article from future suppression.
7657          (gnus-dup-unsuppress-article article)
7658          (gnus-request-move-article
7659           article                       ; Article to move
7660           gnus-newsgroup-name           ; From newsgroup
7661           (nth 1 (gnus-find-method-for-group
7662                   gnus-newsgroup-name)) ; Server
7663           (list 'gnus-request-accept-article
7664                 to-newsgroup (list 'quote select-method)
7665                 (not articles) t)       ; Accept form
7666           (not articles)))              ; Only save nov last time
7667         ;; Copy the article.
7668         ((eq action 'copy)
7669          (save-excursion
7670            (set-buffer copy-buf)
7671            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7672              (gnus-request-accept-article
7673               to-newsgroup select-method (not articles) t))))
7674         ;; Crosspost the article.
7675         ((eq action 'crosspost)
7676          (let ((xref (message-tokenize-header
7677                       (mail-header-xref (gnus-summary-article-header article))
7678                       " ")))
7679            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7680                                   ":" article))
7681            (unless xref
7682              (setq xref (list (system-name))))
7683            (setq new-xref
7684                  (concat
7685                   (mapconcat 'identity
7686                              (delete "Xref:" (delete new-xref xref))
7687                              " ")
7688                   " " new-xref))
7689            (save-excursion
7690              (set-buffer copy-buf)
7691              ;; First put the article in the destination group.
7692              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7693              (when (consp (setq art-group
7694                                 (gnus-request-accept-article
7695                                  to-newsgroup select-method (not articles))))
7696                (setq new-xref (concat new-xref " " (car art-group)
7697                                       ":" (cdr art-group)))
7698                ;; Now we have the new Xrefs header, so we insert
7699                ;; it and replace the new article.
7700                (nnheader-replace-header "Xref" new-xref)
7701                (gnus-request-replace-article
7702                 (cdr art-group) to-newsgroup (current-buffer))
7703                art-group))))))
7704       (cond
7705        ((not art-group)
7706         (gnus-message 1 "Couldn't %s article %s: %s"
7707                       (cadr (assq action names)) article
7708                       (nnheader-get-report (car to-method))))
7709        ((eq art-group 'junk)
7710         (when (eq action 'move)
7711           (gnus-summary-mark-article article gnus-canceled-mark)
7712           (gnus-message 4 "Deleted article %s" article)))
7713        (t
7714         (let* ((pto-group (gnus-group-prefixed-name
7715                            (car art-group) to-method))
7716                (entry
7717                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7718                (info (nth 2 entry))
7719                (to-group (gnus-info-group info))
7720                to-marks)
7721           ;; Update the group that has been moved to.
7722           (when (and info
7723                      (memq action '(move copy)))
7724             (unless (member to-group to-groups)
7725               (push to-group to-groups))
7726
7727             (unless (memq article gnus-newsgroup-unreads)
7728               (push 'read to-marks)
7729               (gnus-info-set-read
7730                info (gnus-add-to-range (gnus-info-read info)
7731                                        (list (cdr art-group)))))
7732
7733             ;; See whether the article is to be put in the cache.
7734             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7735                              default-marks
7736                            no-expire-marks))
7737                   (to-article (cdr art-group)))
7738
7739               ;; Enter the article into the cache in the new group,
7740               ;; if that is required.
7741               (when gnus-use-cache
7742                 (gnus-cache-possibly-enter-article
7743                  to-group to-article
7744                  (let ((header (copy-sequence
7745                                 (gnus-summary-article-header article))))
7746                    (mail-header-set-number header to-article)
7747                    header)
7748                  (memq article gnus-newsgroup-marked)
7749                  (memq article gnus-newsgroup-dormant)
7750                  (memq article gnus-newsgroup-unreads)))
7751
7752               (when gnus-preserve-marks
7753                 ;; Copy any marks over to the new group.
7754                 (when (and (equal to-group gnus-newsgroup-name)
7755                            (not (memq article gnus-newsgroup-unreads)))
7756                   ;; Mark this article as read in this group.
7757                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7758                   (setcdr (gnus-active to-group) to-article)
7759                   (setcdr gnus-newsgroup-active to-article))
7760
7761                 (while marks
7762                   (when (memq article (symbol-value
7763                                        (intern (format "gnus-newsgroup-%s"
7764                                                        (caar marks)))))
7765                     (push (cdar marks) to-marks)
7766                     ;; If the other group is the same as this group,
7767                     ;; then we have to add the mark to the list.
7768                     (when (equal to-group gnus-newsgroup-name)
7769                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7770                            (cons to-article
7771                                  (symbol-value
7772                                   (intern (format "gnus-newsgroup-%s"
7773                                                   (caar marks)))))))
7774                     ;; Copy the marks to other group.
7775                     (gnus-add-marked-articles
7776                      to-group (cdar marks) (list to-article) info))
7777                   (setq marks (cdr marks)))
7778
7779                 (gnus-request-set-mark to-group (list (list (list to-article)
7780                                                             'set
7781                                                             to-marks))))
7782
7783               (gnus-dribble-enter
7784                (concat "(gnus-group-set-info '"
7785                        (gnus-prin1-to-string (gnus-get-info to-group))
7786                        ")"))))
7787
7788           ;; Update the Xref header in this article to point to
7789           ;; the new crossposted article we have just created.
7790           (when (eq action 'crosspost)
7791             (save-excursion
7792               (set-buffer copy-buf)
7793               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7794               (nnheader-replace-header "Xref" new-xref)
7795               (gnus-request-replace-article
7796                article gnus-newsgroup-name (current-buffer)))))
7797
7798         ;;;!!!Why is this necessary?
7799         (set-buffer gnus-summary-buffer)
7800
7801         (gnus-summary-goto-subject article)
7802         (when (eq action 'move)
7803           (gnus-summary-mark-article article gnus-canceled-mark))))
7804       (gnus-summary-remove-process-mark article))
7805     ;; Re-activate all groups that have been moved to.
7806     (while to-groups
7807       (save-excursion
7808         (set-buffer gnus-group-buffer)
7809         (when (gnus-group-goto-group (car to-groups) t)
7810           (gnus-group-get-new-news-this-group 1 t))
7811         (pop to-groups)))
7812
7813     (gnus-kill-buffer copy-buf)
7814     (gnus-summary-position-point)
7815     (gnus-set-mode-line 'summary)))
7816
7817 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7818   "Move the current article to a different newsgroup.
7819 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7820 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7821 re-spool using this method."
7822   (interactive "P")
7823   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7824
7825 (defun gnus-summary-crosspost-article (&optional n)
7826   "Crosspost the current article to some other group."
7827   (interactive "P")
7828   (gnus-summary-move-article n nil nil 'crosspost))
7829
7830 (defcustom gnus-summary-respool-default-method nil
7831   "Default method for respooling an article.
7832 If nil, use to the current newsgroup method."
7833   :type '(choice (gnus-select-method :value (nnml ""))
7834                  (const nil))
7835   :group 'gnus-summary-mail)
7836
7837 (defun gnus-summary-respool-article (&optional n method)
7838   "Respool the current article.
7839 The article will be squeezed through the mail spooling process again,
7840 which means that it will be put in some mail newsgroup or other
7841 depending on `nnmail-split-methods'.
7842 If N is a positive number, respool the N next articles.
7843 If N is a negative number, respool the N previous articles.
7844 If N is nil and any articles have been marked with the process mark,
7845 respool those articles instead.
7846
7847 Respooling can be done both from mail groups and \"real\" newsgroups.
7848 In the former case, the articles in question will be moved from the
7849 current group into whatever groups they are destined to.  In the
7850 latter case, they will be copied into the relevant groups."
7851   (interactive
7852    (list current-prefix-arg
7853          (let* ((methods (gnus-methods-using 'respool))
7854                 (methname
7855                  (symbol-name (or gnus-summary-respool-default-method
7856                                   (car (gnus-find-method-for-group
7857                                         gnus-newsgroup-name)))))
7858                 (method
7859                  (gnus-completing-read
7860                   methname "What backend do you want to use when respooling?"
7861                   methods nil t nil 'gnus-mail-method-history))
7862                 ms)
7863            (cond
7864             ((zerop (length (setq ms (gnus-servers-using-backend
7865                                       (intern method)))))
7866              (list (intern method) ""))
7867             ((= 1 (length ms))
7868              (car ms))
7869             (t
7870              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7871                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7872                            ms-alist))))))))
7873   (unless method
7874     (error "No method given for respooling"))
7875   (if (assoc (symbol-name
7876               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7877              (gnus-methods-using 'respool))
7878       (gnus-summary-move-article n nil method)
7879     (gnus-summary-copy-article n nil method)))
7880
7881 (defun gnus-summary-import-article (file)
7882   "Import an arbitrary file into a mail newsgroup."
7883   (interactive "fImport file: ")
7884   (let ((group gnus-newsgroup-name)
7885         (now (current-time))
7886         atts lines)
7887     (unless (gnus-check-backend-function 'request-accept-article group)
7888       (error "%s does not support article importing" group))
7889     (or (file-readable-p file)
7890         (not (file-regular-p file))
7891         (error "Can't read %s" file))
7892     (save-excursion
7893       (set-buffer (gnus-get-buffer-create " *import file*"))
7894       (erase-buffer)
7895       (nnheader-insert-file-contents file)
7896       (goto-char (point-min))
7897       (unless (nnheader-article-p)
7898         ;; This doesn't look like an article, so we fudge some headers.
7899         (setq atts (file-attributes file)
7900               lines (count-lines (point-min) (point-max)))
7901         (insert "From: " (read-string "From: ") "\n"
7902                 "Subject: " (read-string "Subject: ") "\n"
7903                 "Date: " (message-make-date (nth 5 atts))
7904                 "\n"
7905                 "Message-ID: " (message-make-message-id) "\n"
7906                 "Lines: " (int-to-string lines) "\n"
7907                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7908       (gnus-request-accept-article group nil t)
7909       (kill-buffer (current-buffer)))))
7910
7911 (defun gnus-summary-article-posted-p ()
7912   "Say whether the current (mail) article is available from news as well.
7913 This will be the case if the article has both been mailed and posted."
7914   (interactive)
7915   (let ((id (mail-header-references (gnus-summary-article-header)))
7916         (gnus-override-method (car (gnus-refer-article-methods))))
7917     (if (gnus-request-head id "")
7918         (gnus-message 2 "The current message was found on %s"
7919                       gnus-override-method)
7920       (gnus-message 2 "The current message couldn't be found on %s"
7921                     gnus-override-method)
7922       nil)))
7923
7924 (defun gnus-summary-expire-articles (&optional now)
7925   "Expire all articles that are marked as expirable in the current group."
7926   (interactive)
7927   (when (gnus-check-backend-function
7928          'request-expire-articles gnus-newsgroup-name)
7929     ;; This backend supports expiry.
7930     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7931            (expirable (if total
7932                           (progn
7933                             ;; We need to update the info for
7934                             ;; this group for `gnus-list-of-read-articles'
7935                             ;; to give us the right answer.
7936                             (gnus-run-hooks 'gnus-exit-group-hook)
7937                             (gnus-summary-update-info)
7938                             (gnus-list-of-read-articles gnus-newsgroup-name))
7939                         (setq gnus-newsgroup-expirable
7940                               (sort gnus-newsgroup-expirable '<))))
7941            (expiry-wait (if now 'immediate
7942                           (gnus-group-find-parameter
7943                            gnus-newsgroup-name 'expiry-wait)))
7944            (nnmail-expiry-target
7945             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7946                 nnmail-expiry-target))
7947            es)
7948       (when expirable
7949         ;; There are expirable articles in this group, so we run them
7950         ;; through the expiry process.
7951         (gnus-message 6 "Expiring articles...")
7952         (unless (gnus-check-group gnus-newsgroup-name)
7953           (error "Can't open server for %s" gnus-newsgroup-name))
7954         ;; The list of articles that weren't expired is returned.
7955         (save-excursion
7956           (if expiry-wait
7957               (let ((nnmail-expiry-wait-function nil)
7958                     (nnmail-expiry-wait expiry-wait))
7959                 (setq es (gnus-request-expire-articles
7960                           expirable gnus-newsgroup-name)))
7961             (setq es (gnus-request-expire-articles
7962                       expirable gnus-newsgroup-name)))
7963           (unless total
7964             (setq gnus-newsgroup-expirable es))
7965           ;; We go through the old list of expirable, and mark all
7966           ;; really expired articles as nonexistent.
7967           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7968             (let ((gnus-use-cache nil))
7969               (while expirable
7970                 (unless (memq (car expirable) es)
7971                   (when (gnus-data-find (car expirable))
7972                     (gnus-summary-mark-article
7973                      (car expirable) gnus-canceled-mark)))
7974                 (setq expirable (cdr expirable))))))
7975         (gnus-message 6 "Expiring articles...done")))))
7976
7977 (defun gnus-summary-expire-articles-now ()
7978   "Expunge all expirable articles in the current group.
7979 This means that *all* articles that are marked as expirable will be
7980 deleted forever, right now."
7981   (interactive)
7982   (or gnus-expert-user
7983       (gnus-yes-or-no-p
7984        "Are you really, really, really sure you want to delete all these messages? ")
7985       (error "Phew!"))
7986   (gnus-summary-expire-articles t))
7987
7988 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7989 (defun gnus-summary-delete-article (&optional n)
7990   "Delete the N next (mail) articles.
7991 This command actually deletes articles.  This is not a marking
7992 command.  The article will disappear forever from your life, never to
7993 return.
7994 If N is negative, delete backwards.
7995 If N is nil and articles have been marked with the process mark,
7996 delete these instead."
7997   (interactive "P")
7998   (unless (gnus-check-backend-function 'request-expire-articles
7999                                        gnus-newsgroup-name)
8000     (error "The current newsgroup does not support article deletion"))
8001   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8002     (error "Couldn't open server"))
8003   ;; Compute the list of articles to delete.
8004   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8005         not-deleted)
8006     (if (and gnus-novice-user
8007              (not (gnus-yes-or-no-p
8008                    (format "Do you really want to delete %s forever? "
8009                            (if (> (length articles) 1)
8010                                (format "these %s articles" (length articles))
8011                              "this article")))))
8012         ()
8013       ;; Delete the articles.
8014       (setq not-deleted (gnus-request-expire-articles
8015                          articles gnus-newsgroup-name 'force))
8016       (while articles
8017         (gnus-summary-remove-process-mark (car articles))
8018         ;; The backend might not have been able to delete the article
8019         ;; after all.
8020         (unless (memq (car articles) not-deleted)
8021           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8022         (setq articles (cdr articles)))
8023       (when not-deleted
8024         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8025     (gnus-summary-position-point)
8026     (gnus-set-mode-line 'summary)
8027     not-deleted))
8028
8029 (defun gnus-summary-edit-article (&optional force)
8030   "Edit the current article.
8031 This will have permanent effect only in mail groups.
8032 If FORCE is non-nil, allow editing of articles even in read-only
8033 groups."
8034   (interactive "P")
8035   (save-excursion
8036     (set-buffer gnus-summary-buffer)
8037     (let ((mail-parse-charset gnus-newsgroup-charset)
8038           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8039       (gnus-set-global-variables)
8040       (when (and (not force)
8041                  (gnus-group-read-only-p))
8042         (error "The current newsgroup does not support article editing"))
8043       (gnus-summary-show-article t)
8044       (gnus-article-edit-article
8045        'ignore
8046        `(lambda (no-highlight)
8047           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8048                 (message-options message-options)
8049                 (message-options-set-recipient)
8050                 (mail-parse-ignored-charsets
8051                  ',gnus-newsgroup-ignored-charsets))
8052             (gnus-summary-edit-article-done
8053              ,(or (mail-header-references gnus-current-headers) "")
8054              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8055
8056 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8057
8058 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8059                                                  no-highlight)
8060   "Make edits to the current article permanent."
8061   (interactive)
8062   (save-excursion
8063     ;; The buffer restriction contains the entire article if it exists.
8064     (when (article-goto-body)
8065       (let ((lines (count-lines (point) (point-max)))
8066             (length (- (point-max) (point)))
8067             (case-fold-search t)
8068             (body (copy-marker (point))))
8069         (goto-char (point-min))
8070         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8071           (delete-region (match-beginning 1) (match-end 1))
8072           (insert (number-to-string length)))
8073         (goto-char (point-min))
8074         (when (re-search-forward
8075                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8076           (delete-region (match-beginning 1) (match-end 1))
8077           (insert (number-to-string length)))
8078         (goto-char (point-min))
8079         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8080           (delete-region (match-beginning 1) (match-end 1))
8081           (insert (number-to-string lines))))))
8082   ;; Replace the article.
8083   (let ((buf (current-buffer)))
8084     (with-temp-buffer
8085       (insert-buffer-substring buf)
8086       
8087       (if (and (not read-only)
8088                (not (gnus-request-replace-article
8089                      (cdr gnus-article-current) (car gnus-article-current)
8090                      (current-buffer) t)))
8091           (error "Couldn't replace article")
8092         ;; Update the summary buffer.
8093         (if (and references
8094                  (equal (message-tokenize-header references " ")
8095                         (message-tokenize-header
8096                          (or (message-fetch-field "references") "") " ")))
8097             ;; We only have to update this line.
8098             (save-excursion
8099               (save-restriction
8100                 (message-narrow-to-head)
8101                 (let ((head (buffer-string))
8102                       header)
8103                   (with-temp-buffer
8104                     (insert (format "211 %d Article retrieved.\n"
8105                                     (cdr gnus-article-current)))
8106                     (insert head)
8107                     (insert ".\n")
8108                     (let ((nntp-server-buffer (current-buffer)))
8109                       (setq header (car (gnus-get-newsgroup-headers
8110                                          (save-excursion
8111                                            (set-buffer gnus-summary-buffer)
8112                                            gnus-newsgroup-dependencies)
8113                                          t))))
8114                     (save-excursion
8115                       (set-buffer gnus-summary-buffer)
8116                       (gnus-data-set-header
8117                        (gnus-data-find (cdr gnus-article-current))
8118                        header)
8119                       (gnus-summary-update-article-line
8120                        (cdr gnus-article-current) header))))))
8121           ;; Update threads.
8122           (set-buffer (or buffer gnus-summary-buffer))
8123           (gnus-summary-update-article (cdr gnus-article-current)))
8124         ;; Prettify the article buffer again.
8125         (unless no-highlight
8126           (save-excursion
8127             (set-buffer gnus-article-buffer)
8128             ;;;!!! Fix this -- article should be rehighlighted.
8129             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8130             (set-buffer gnus-original-article-buffer)
8131             (gnus-request-article
8132              (cdr gnus-article-current)
8133              (car gnus-article-current) (current-buffer))))
8134         ;; Prettify the summary buffer line.
8135         (when (gnus-visual-p 'summary-highlight 'highlight)
8136           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8137
8138 (defun gnus-summary-edit-wash (key)
8139   "Perform editing command KEY in the article buffer."
8140   (interactive
8141    (list
8142     (progn
8143       (message "%s" (concat (this-command-keys) "- "))
8144       (read-char))))
8145   (message "")
8146   (gnus-summary-edit-article)
8147   (execute-kbd-macro (concat (this-command-keys) key))
8148   (gnus-article-edit-done))
8149
8150 ;;; Respooling
8151
8152 (defun gnus-summary-respool-query (&optional silent trace)
8153   "Query where the respool algorithm would put this article."
8154   (interactive)
8155   (let (gnus-mark-article-hook)
8156     (gnus-summary-select-article)
8157     (save-excursion
8158       (set-buffer gnus-original-article-buffer)
8159       (save-restriction
8160         (message-narrow-to-head)
8161         (let ((groups (nnmail-article-group 'identity trace)))
8162           (unless silent
8163             (if groups
8164                 (message "This message would go to %s"
8165                          (mapconcat 'car groups ", "))
8166               (message "This message would go to no groups"))
8167             groups))))))
8168
8169 (defun gnus-summary-respool-trace ()
8170   "Trace where the respool algorithm would put this article.
8171 Display a buffer showing all fancy splitting patterns which matched."
8172   (interactive)
8173   (gnus-summary-respool-query nil t))
8174
8175 ;; Summary marking commands.
8176
8177 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8178   "Mark articles which has the same subject as read, and then select the next.
8179 If UNMARK is positive, remove any kind of mark.
8180 If UNMARK is negative, tick articles."
8181   (interactive "P")
8182   (when unmark
8183     (setq unmark (prefix-numeric-value unmark)))
8184   (let ((count
8185          (gnus-summary-mark-same-subject
8186           (gnus-summary-article-subject) unmark)))
8187     ;; Select next unread article.  If auto-select-same mode, should
8188     ;; select the first unread article.
8189     (gnus-summary-next-article t (and gnus-auto-select-same
8190                                       (gnus-summary-article-subject)))
8191     (gnus-message 7 "%d article%s marked as %s"
8192                   count (if (= count 1) " is" "s are")
8193                   (if unmark "unread" "read"))))
8194
8195 (defun gnus-summary-kill-same-subject (&optional unmark)
8196   "Mark articles which has the same subject as read.
8197 If UNMARK is positive, remove any kind of mark.
8198 If UNMARK is negative, tick articles."
8199   (interactive "P")
8200   (when unmark
8201     (setq unmark (prefix-numeric-value unmark)))
8202   (let ((count
8203          (gnus-summary-mark-same-subject
8204           (gnus-summary-article-subject) unmark)))
8205     ;; If marked as read, go to next unread subject.
8206     (when (null unmark)
8207       ;; Go to next unread subject.
8208       (gnus-summary-next-subject 1 t))
8209     (gnus-message 7 "%d articles are marked as %s"
8210                   count (if unmark "unread" "read"))))
8211
8212 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8213   "Mark articles with same SUBJECT as read, and return marked number.
8214 If optional argument UNMARK is positive, remove any kinds of marks.
8215 If optional argument UNMARK is negative, mark articles as unread instead."
8216   (let ((count 1))
8217     (save-excursion
8218       (cond
8219        ((null unmark)                   ; Mark as read.
8220         (while (and
8221                 (progn
8222                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8223                   (gnus-summary-show-thread) t)
8224                 (gnus-summary-find-subject subject))
8225           (setq count (1+ count))))
8226        ((> unmark 0)                    ; Tick.
8227         (while (and
8228                 (progn
8229                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8230                   (gnus-summary-show-thread) t)
8231                 (gnus-summary-find-subject subject))
8232           (setq count (1+ count))))
8233        (t                               ; Mark as unread.
8234         (while (and
8235                 (progn
8236                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8237                   (gnus-summary-show-thread) t)
8238                 (gnus-summary-find-subject subject))
8239           (setq count (1+ count)))))
8240       (gnus-set-mode-line 'summary)
8241       ;; Return the number of marked articles.
8242       count)))
8243
8244 (defun gnus-summary-mark-as-processable (n &optional unmark)
8245   "Set the process mark on the next N articles.
8246 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8247 the process mark instead.  The difference between N and the actual
8248 number of articles marked is returned."
8249   (interactive "p")
8250   (let ((backward (< n 0))
8251         (n (abs n)))
8252     (while (and
8253             (> n 0)
8254             (if unmark
8255                 (gnus-summary-remove-process-mark
8256                  (gnus-summary-article-number))
8257               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8258             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8259       (setq n (1- n)))
8260     (when (/= 0 n)
8261       (gnus-message 7 "No more articles"))
8262     (gnus-summary-recenter)
8263     (gnus-summary-position-point)
8264     n))
8265
8266 (defun gnus-summary-unmark-as-processable (n)
8267   "Remove the process mark from the next N articles.
8268 If N is negative, unmark backward instead.  The difference between N and
8269 the actual number of articles unmarked is returned."
8270   (interactive "p")
8271   (gnus-summary-mark-as-processable n t))
8272
8273 (defun gnus-summary-unmark-all-processable ()
8274   "Remove the process mark from all articles."
8275   (interactive)
8276   (save-excursion
8277     (while gnus-newsgroup-processable
8278       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8279   (gnus-summary-position-point))
8280
8281 (defun gnus-summary-mark-as-expirable (n)
8282   "Mark N articles forward as expirable.
8283 If N is negative, mark backward instead.  The difference between N and
8284 the actual number of articles marked is returned."
8285   (interactive "p")
8286   (gnus-summary-mark-forward n gnus-expirable-mark))
8287
8288 (defun gnus-summary-mark-article-as-replied (article)
8289   "Mark ARTICLE replied and update the summary line."
8290   (push article gnus-newsgroup-replied)
8291   (let ((buffer-read-only nil))
8292     (when (gnus-summary-goto-subject article nil t)
8293       (gnus-summary-update-secondary-mark article))))
8294
8295 (defun gnus-summary-set-bookmark (article)
8296   "Set a bookmark in current article."
8297   (interactive (list (gnus-summary-article-number)))
8298   (when (or (not (get-buffer gnus-article-buffer))
8299             (not gnus-current-article)
8300             (not gnus-article-current)
8301             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8302     (error "No current article selected"))
8303   ;; Remove old bookmark, if one exists.
8304   (let ((old (assq article gnus-newsgroup-bookmarks)))
8305     (when old
8306       (setq gnus-newsgroup-bookmarks
8307             (delq old gnus-newsgroup-bookmarks))))
8308   ;; Set the new bookmark, which is on the form
8309   ;; (article-number . line-number-in-body).
8310   (push
8311    (cons article
8312          (save-excursion
8313            (set-buffer gnus-article-buffer)
8314            (count-lines
8315             (min (point)
8316                  (save-excursion
8317                    (goto-char (point-min))
8318                    (search-forward "\n\n" nil t)
8319                    (point)))
8320             (point))))
8321    gnus-newsgroup-bookmarks)
8322   (gnus-message 6 "A bookmark has been added to the current article."))
8323
8324 (defun gnus-summary-remove-bookmark (article)
8325   "Remove the bookmark from the current article."
8326   (interactive (list (gnus-summary-article-number)))
8327   ;; Remove old bookmark, if one exists.
8328   (let ((old (assq article gnus-newsgroup-bookmarks)))
8329     (if old
8330         (progn
8331           (setq gnus-newsgroup-bookmarks
8332                 (delq old gnus-newsgroup-bookmarks))
8333           (gnus-message 6 "Removed bookmark."))
8334       (gnus-message 6 "No bookmark in current article."))))
8335
8336 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8337 (defun gnus-summary-mark-as-dormant (n)
8338   "Mark N articles forward as dormant.
8339 If N is negative, mark backward instead.  The difference between N and
8340 the actual number of articles marked is returned."
8341   (interactive "p")
8342   (gnus-summary-mark-forward n gnus-dormant-mark))
8343
8344 (defun gnus-summary-set-process-mark (article)
8345   "Set the process mark on ARTICLE and update the summary line."
8346   (setq gnus-newsgroup-processable
8347         (cons article
8348               (delq article gnus-newsgroup-processable)))
8349   (when (gnus-summary-goto-subject article)
8350     (gnus-summary-show-thread)
8351     (gnus-summary-goto-subject article)
8352     (gnus-summary-update-secondary-mark article)))
8353
8354 (defun gnus-summary-remove-process-mark (article)
8355   "Remove the process mark from ARTICLE and update the summary line."
8356   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8357   (when (gnus-summary-goto-subject article)
8358     (gnus-summary-show-thread)
8359     (gnus-summary-goto-subject article)
8360     (gnus-summary-update-secondary-mark article)))
8361
8362 (defun gnus-summary-set-saved-mark (article)
8363   "Set the process mark on ARTICLE and update the summary line."
8364   (push article gnus-newsgroup-saved)
8365   (when (gnus-summary-goto-subject article)
8366     (gnus-summary-update-secondary-mark article)))
8367
8368 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8369   "Mark N articles as read forwards.
8370 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8371 The difference between N and the actual number of articles marked is
8372 returned.
8373 Iff NO-EXPIRE, auto-expiry will be inhibited."
8374   (interactive "p")
8375   (gnus-summary-show-thread)
8376   (let ((backward (< n 0))
8377         (gnus-summary-goto-unread
8378          (and gnus-summary-goto-unread
8379               (not (eq gnus-summary-goto-unread 'never))
8380               (not (memq mark (list gnus-unread-mark
8381                                     gnus-ticked-mark gnus-dormant-mark)))))
8382         (n (abs n))
8383         (mark (or mark gnus-del-mark)))
8384     (while (and (> n 0)
8385                 (gnus-summary-mark-article nil mark no-expire)
8386                 (zerop (gnus-summary-next-subject
8387                         (if backward -1 1)
8388                         (and gnus-summary-goto-unread
8389                              (not (eq gnus-summary-goto-unread 'never)))
8390                         t)))
8391       (setq n (1- n)))
8392     (when (/= 0 n)
8393       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8394     (gnus-summary-recenter)
8395     (gnus-summary-position-point)
8396     (gnus-set-mode-line 'summary)
8397     n))
8398
8399 (defun gnus-summary-mark-article-as-read (mark)
8400   "Mark the current article quickly as read with MARK."
8401   (let ((article (gnus-summary-article-number)))
8402     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8403     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8404     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8405     (push (cons article mark) gnus-newsgroup-reads)
8406     ;; Possibly remove from cache, if that is used.
8407     (when gnus-use-cache
8408       (gnus-cache-enter-remove-article article))
8409     ;; Allow the backend to change the mark.
8410     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8411     ;; Check for auto-expiry.
8412     (when (and gnus-newsgroup-auto-expire
8413                (memq mark gnus-auto-expirable-marks))
8414       (setq mark gnus-expirable-mark)
8415       ;; Let the backend know about the mark change.
8416       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8417       (push article gnus-newsgroup-expirable))
8418     ;; Set the mark in the buffer.
8419     (gnus-summary-update-mark mark 'unread)
8420     t))
8421
8422 (defun gnus-summary-mark-article-as-unread (mark)
8423   "Mark the current article quickly as unread with MARK."
8424   (let* ((article (gnus-summary-article-number))
8425          (old-mark (gnus-summary-article-mark article)))
8426     ;; Allow the backend to change the mark.
8427     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8428     (if (eq mark old-mark)
8429         t
8430       (if (<= article 0)
8431           (progn
8432             (gnus-error 1 "Can't mark negative article numbers")
8433             nil)
8434         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8435         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8436         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8437         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8438         (cond ((= mark gnus-ticked-mark)
8439                (push article gnus-newsgroup-marked))
8440               ((= mark gnus-dormant-mark)
8441                (push article gnus-newsgroup-dormant))
8442               (t
8443                (push article gnus-newsgroup-unreads)))
8444         (gnus-pull article gnus-newsgroup-reads)
8445
8446         ;; See whether the article is to be put in the cache.
8447         (and gnus-use-cache
8448              (vectorp (gnus-summary-article-header article))
8449              (save-excursion
8450                (gnus-cache-possibly-enter-article
8451                 gnus-newsgroup-name article
8452                 (gnus-summary-article-header article)
8453                 (= mark gnus-ticked-mark)
8454                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8455
8456         ;; Fix the mark.
8457         (gnus-summary-update-mark mark 'unread)
8458         t))))
8459
8460 (defun gnus-summary-mark-article (&optional article mark no-expire)
8461   "Mark ARTICLE with MARK.  MARK can be any character.
8462 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8463 `??' (dormant) and `?E' (expirable).
8464 If MARK is nil, then the default character `?r' is used.
8465 If ARTICLE is nil, then the article on the current line will be
8466 marked.
8467 Iff NO-EXPIRE, auto-expiry will be inhibited."
8468   ;; The mark might be a string.
8469   (when (stringp mark)
8470     (setq mark (aref mark 0)))
8471   ;; If no mark is given, then we check auto-expiring.
8472   (when (null mark)
8473     (setq mark gnus-del-mark))
8474   (when (and (not no-expire)
8475              gnus-newsgroup-auto-expire
8476              (memq mark gnus-auto-expirable-marks))
8477     (setq mark gnus-expirable-mark))
8478   (let ((article (or article (gnus-summary-article-number)))
8479         (old-mark (gnus-summary-article-mark article)))
8480     ;; Allow the backend to change the mark.
8481     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8482     (if (eq mark old-mark)
8483         t
8484       (unless article
8485         (error "No article on current line"))
8486       (if (not (if (or (= mark gnus-unread-mark)
8487                        (= mark gnus-ticked-mark)
8488                        (= mark gnus-dormant-mark))
8489                    (gnus-mark-article-as-unread article mark)
8490                  (gnus-mark-article-as-read article mark)))
8491           t
8492         ;; See whether the article is to be put in the cache.
8493         (and gnus-use-cache
8494              (not (= mark gnus-canceled-mark))
8495              (vectorp (gnus-summary-article-header article))
8496              (save-excursion
8497                (gnus-cache-possibly-enter-article
8498                 gnus-newsgroup-name article
8499                 (gnus-summary-article-header article)
8500                 (= mark gnus-ticked-mark)
8501                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8502
8503         (when (gnus-summary-goto-subject article nil t)
8504           (let ((buffer-read-only nil))
8505             (gnus-summary-show-thread)
8506             ;; Fix the mark.
8507             (gnus-summary-update-mark mark 'unread)
8508             t))))))
8509
8510 (defun gnus-summary-update-secondary-mark (article)
8511   "Update the secondary (read, process, cache) mark."
8512   (gnus-summary-update-mark
8513    (cond ((memq article gnus-newsgroup-processable)
8514           gnus-process-mark)
8515          ((memq article gnus-newsgroup-cached)
8516           gnus-cached-mark)
8517          ((memq article gnus-newsgroup-replied)
8518           gnus-replied-mark)
8519          ((memq article gnus-newsgroup-saved)
8520           gnus-saved-mark)
8521          (t gnus-unread-mark))
8522    'replied)
8523   (when (gnus-visual-p 'summary-highlight 'highlight)
8524     (gnus-run-hooks 'gnus-summary-update-hook))
8525   t)
8526
8527 (defun gnus-summary-update-mark (mark type)
8528   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8529         (buffer-read-only nil))
8530     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8531     (when forward
8532       (when (looking-at "\r")
8533         (incf forward))
8534       (when (<= (+ forward (point)) (point-max))
8535         ;; Go to the right position on the line.
8536         (goto-char (+ forward (point)))
8537         ;; Replace the old mark with the new mark.
8538         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8539         ;; Optionally update the marks by some user rule.
8540         (when (eq type 'unread)
8541           (gnus-data-set-mark
8542            (gnus-data-find (gnus-summary-article-number)) mark)
8543           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8544
8545 (defun gnus-mark-article-as-read (article &optional mark)
8546   "Enter ARTICLE in the pertinent lists and remove it from others."
8547   ;; Make the article expirable.
8548   (let ((mark (or mark gnus-del-mark)))
8549     (if (= mark gnus-expirable-mark)
8550         (push article gnus-newsgroup-expirable)
8551       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8552     ;; Remove from unread and marked lists.
8553     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8554     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8555     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8556     (push (cons article mark) gnus-newsgroup-reads)
8557     ;; Possibly remove from cache, if that is used.
8558     (when gnus-use-cache
8559       (gnus-cache-enter-remove-article article))
8560     t))
8561
8562 (defun gnus-mark-article-as-unread (article &optional mark)
8563   "Enter ARTICLE in the pertinent lists and remove it from others."
8564   (let ((mark (or mark gnus-ticked-mark)))
8565     (if (<= article 0)
8566         (progn
8567           (gnus-error 1 "Can't mark negative article numbers")
8568           nil)
8569       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8570             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8571             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8572             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8573
8574       ;; Unsuppress duplicates?
8575       (when gnus-suppress-duplicates
8576         (gnus-dup-unsuppress-article article))
8577
8578       (cond ((= mark gnus-ticked-mark)
8579              (push article gnus-newsgroup-marked))
8580             ((= mark gnus-dormant-mark)
8581              (push article gnus-newsgroup-dormant))
8582             (t
8583              (push article gnus-newsgroup-unreads)))
8584       (gnus-pull article gnus-newsgroup-reads)
8585       t)))
8586
8587 (defalias 'gnus-summary-mark-as-unread-forward
8588   'gnus-summary-tick-article-forward)
8589 (make-obsolete 'gnus-summary-mark-as-unread-forward
8590                'gnus-summary-tick-article-forward)
8591 (defun gnus-summary-tick-article-forward (n)
8592   "Tick N articles forwards.
8593 If N is negative, tick backwards instead.
8594 The difference between N and the number of articles ticked is returned."
8595   (interactive "p")
8596   (gnus-summary-mark-forward n gnus-ticked-mark))
8597
8598 (defalias 'gnus-summary-mark-as-unread-backward
8599   'gnus-summary-tick-article-backward)
8600 (make-obsolete 'gnus-summary-mark-as-unread-backward
8601                'gnus-summary-tick-article-backward)
8602 (defun gnus-summary-tick-article-backward (n)
8603   "Tick N articles backwards.
8604 The difference between N and the number of articles ticked is returned."
8605   (interactive "p")
8606   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8607
8608 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8609 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8610 (defun gnus-summary-tick-article (&optional article clear-mark)
8611   "Mark current article as unread.
8612 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8613 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8614   (interactive)
8615   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8616                                        gnus-ticked-mark)))
8617
8618 (defun gnus-summary-mark-as-read-forward (n)
8619   "Mark N articles as read forwards.
8620 If N is negative, mark backwards instead.
8621 The difference between N and the actual number of articles marked is
8622 returned."
8623   (interactive "p")
8624   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8625
8626 (defun gnus-summary-mark-as-read-backward (n)
8627   "Mark the N articles as read backwards.
8628 The difference between N and the actual number of articles marked is
8629 returned."
8630   (interactive "p")
8631   (gnus-summary-mark-forward
8632    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8633
8634 (defun gnus-summary-mark-as-read (&optional article mark)
8635   "Mark current article as read.
8636 ARTICLE specifies the article to be marked as read.
8637 MARK specifies a string to be inserted at the beginning of the line."
8638   (gnus-summary-mark-article article mark))
8639
8640 (defun gnus-summary-clear-mark-forward (n)
8641   "Clear marks from N articles forward.
8642 If N is negative, clear backward instead.
8643 The difference between N and the number of marks cleared is returned."
8644   (interactive "p")
8645   (gnus-summary-mark-forward n gnus-unread-mark))
8646
8647 (defun gnus-summary-clear-mark-backward (n)
8648   "Clear marks from N articles backward.
8649 The difference between N and the number of marks cleared is returned."
8650   (interactive "p")
8651   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8652
8653 (defun gnus-summary-mark-unread-as-read ()
8654   "Intended to be used by `gnus-summary-mark-article-hook'."
8655   (when (memq gnus-current-article gnus-newsgroup-unreads)
8656     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8657
8658 (defun gnus-summary-mark-read-and-unread-as-read ()
8659   "Intended to be used by `gnus-summary-mark-article-hook'."
8660   (let ((mark (gnus-summary-article-mark)))
8661     (when (or (gnus-unread-mark-p mark)
8662               (gnus-read-mark-p mark))
8663       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8664
8665 (defun gnus-summary-mark-region-as-read (point mark all)
8666   "Mark all unread articles between point and mark as read.
8667 If given a prefix, mark all articles between point and mark as read,
8668 even ticked and dormant ones."
8669   (interactive "r\nP")
8670   (save-excursion
8671     (let (article)
8672       (goto-char point)
8673       (beginning-of-line)
8674       (while (and
8675               (< (point) mark)
8676               (progn
8677                 (when (or all
8678                           (memq (setq article (gnus-summary-article-number))
8679                                 gnus-newsgroup-unreads))
8680                   (gnus-summary-mark-article article gnus-del-mark))
8681                 t)
8682               (gnus-summary-find-next))))))
8683
8684 (defun gnus-summary-mark-below (score mark)
8685   "Mark articles with score less than SCORE with MARK."
8686   (interactive "P\ncMark: ")
8687   (setq score (if score
8688                   (prefix-numeric-value score)
8689                 (or gnus-summary-default-score 0)))
8690   (save-excursion
8691     (set-buffer gnus-summary-buffer)
8692     (goto-char (point-min))
8693     (while
8694         (progn
8695           (and (< (gnus-summary-article-score) score)
8696                (gnus-summary-mark-article nil mark))
8697           (gnus-summary-find-next)))))
8698
8699 (defun gnus-summary-kill-below (&optional score)
8700   "Mark articles with score below SCORE as read."
8701   (interactive "P")
8702   (gnus-summary-mark-below score gnus-killed-mark))
8703
8704 (defun gnus-summary-clear-above (&optional score)
8705   "Clear all marks from articles with score above SCORE."
8706   (interactive "P")
8707   (gnus-summary-mark-above score gnus-unread-mark))
8708
8709 (defun gnus-summary-tick-above (&optional score)
8710   "Tick all articles with score above SCORE."
8711   (interactive "P")
8712   (gnus-summary-mark-above score gnus-ticked-mark))
8713
8714 (defun gnus-summary-mark-above (score mark)
8715   "Mark articles with score over SCORE with MARK."
8716   (interactive "P\ncMark: ")
8717   (setq score (if score
8718                   (prefix-numeric-value score)
8719                 (or gnus-summary-default-score 0)))
8720   (save-excursion
8721     (set-buffer gnus-summary-buffer)
8722     (goto-char (point-min))
8723     (while (and (progn
8724                   (when (> (gnus-summary-article-score) score)
8725                     (gnus-summary-mark-article nil mark))
8726                   t)
8727                 (gnus-summary-find-next)))))
8728
8729 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8730 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8731 (defun gnus-summary-limit-include-expunged (&optional no-error)
8732   "Display all the hidden articles that were expunged for low scores."
8733   (interactive)
8734   (let ((buffer-read-only nil))
8735     (let ((scored gnus-newsgroup-scored)
8736           headers h)
8737       (while scored
8738         (unless (gnus-summary-goto-subject (caar scored))
8739           (and (setq h (gnus-summary-article-header (caar scored)))
8740                (< (cdar scored) gnus-summary-expunge-below)
8741                (push h headers)))
8742         (setq scored (cdr scored)))
8743       (if (not headers)
8744           (when (not no-error)
8745             (error "No expunged articles hidden"))
8746         (goto-char (point-min))
8747         (gnus-summary-prepare-unthreaded (nreverse headers))
8748         (goto-char (point-min))
8749         (gnus-summary-position-point)
8750         t))))
8751
8752 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8753   "Mark all unread articles in this newsgroup as read.
8754 If prefix argument ALL is non-nil, ticked and dormant articles will
8755 also be marked as read.
8756 If QUIETLY is non-nil, no questions will be asked.
8757 If TO-HERE is non-nil, it should be a point in the buffer.  All
8758 articles before this point will be marked as read.
8759 Note that this function will only catch up the unread article
8760 in the current summary buffer limitation.
8761 The number of articles marked as read is returned."
8762   (interactive "P")
8763   (prog1
8764       (save-excursion
8765         (when (or quietly
8766                   (not gnus-interactive-catchup) ;Without confirmation?
8767                   gnus-expert-user
8768                   (gnus-y-or-n-p
8769                    (if all
8770                        "Mark absolutely all articles as read? "
8771                      "Mark all unread articles as read? ")))
8772           (if (and not-mark
8773                    (not gnus-newsgroup-adaptive)
8774                    (not gnus-newsgroup-auto-expire)
8775                    (not gnus-suppress-duplicates)
8776                    (or (not gnus-use-cache)
8777                        (eq gnus-use-cache 'passive)))
8778               (progn
8779                 (when all
8780                   (setq gnus-newsgroup-marked nil
8781                         gnus-newsgroup-dormant nil))
8782                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8783             ;; We actually mark all articles as canceled, which we
8784             ;; have to do when using auto-expiry or adaptive scoring.
8785             (gnus-summary-show-all-threads)
8786             (when (gnus-summary-first-subject (not all) t)
8787               (while (and
8788                       (if to-here (< (point) to-here) t)
8789                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8790                       (gnus-summary-find-next (not all) nil nil t))))
8791             (gnus-set-mode-line 'summary))
8792           t))
8793     (gnus-summary-position-point)))
8794
8795 (defun gnus-summary-catchup-to-here (&optional all)
8796   "Mark all unticked articles before the current one as read.
8797 If ALL is non-nil, also mark ticked and dormant articles as read."
8798   (interactive "P")
8799   (save-excursion
8800     (gnus-save-hidden-threads
8801       (let ((beg (point)))
8802         ;; We check that there are unread articles.
8803         (when (or all (gnus-summary-find-prev))
8804           (gnus-summary-catchup all t beg)))))
8805   (gnus-summary-position-point))
8806
8807 (defun gnus-summary-catchup-all (&optional quietly)
8808   "Mark all articles in this newsgroup as read."
8809   (interactive "P")
8810   (gnus-summary-catchup t quietly))
8811
8812 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8813   "Mark all unread articles in this group as read, then exit.
8814 If prefix argument ALL is non-nil, all articles are marked as read."
8815   (interactive "P")
8816   (when (gnus-summary-catchup all quietly nil 'fast)
8817     ;; Select next newsgroup or exit.
8818     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8819              (eq gnus-auto-select-next 'quietly))
8820         (gnus-summary-next-group nil)
8821       (gnus-summary-exit))))
8822
8823 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8824   "Mark all articles in this newsgroup as read, and then exit."
8825   (interactive "P")
8826   (gnus-summary-catchup-and-exit t quietly))
8827
8828 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8829   "Mark all articles in this group as read and select the next group.
8830 If given a prefix, mark all articles, unread as well as ticked, as
8831 read."
8832   (interactive "P")
8833   (save-excursion
8834     (gnus-summary-catchup all))
8835   (gnus-summary-next-group))
8836
8837 ;;;
8838 ;;; with article
8839 ;;;
8840
8841 (defmacro gnus-with-article (article &rest forms)
8842   "Select ARTICLE and perform FORMS in the original article buffer.
8843 Then replace the article with the result."
8844   `(progn
8845      ;; We don't want the article to be marked as read.
8846      (let (gnus-mark-article-hook)
8847        (gnus-summary-select-article t t nil ,article))
8848      (set-buffer gnus-original-article-buffer)
8849      ,@forms
8850      (if (not (gnus-check-backend-function
8851                'request-replace-article (car gnus-article-current)))
8852          (gnus-message 5 "Read-only group; not replacing")
8853        (unless (gnus-request-replace-article
8854                 ,article (car gnus-article-current)
8855                 (current-buffer) t)
8856          (error "Couldn't replace article")))
8857      ;; The cache and backlog have to be flushed somewhat.
8858      (when gnus-keep-backlog
8859        (gnus-backlog-remove-article
8860         (car gnus-article-current) (cdr gnus-article-current)))
8861      (when gnus-use-cache
8862        (gnus-cache-update-article
8863         (car gnus-article-current) (cdr gnus-article-current)))))
8864
8865 (put 'gnus-with-article 'lisp-indent-function 1)
8866 (put 'gnus-with-article 'edebug-form-spec '(form body))
8867
8868 ;; Thread-based commands.
8869
8870 (defun gnus-summary-articles-in-thread (&optional article)
8871   "Return a list of all articles in the current thread.
8872 If ARTICLE is non-nil, return all articles in the thread that starts
8873 with that article."
8874   (let* ((article (or article (gnus-summary-article-number)))
8875          (data (gnus-data-find-list article))
8876          (top-level (gnus-data-level (car data)))
8877          (top-subject
8878           (cond ((null gnus-thread-operation-ignore-subject)
8879                  (gnus-simplify-subject-re
8880                   (mail-header-subject (gnus-data-header (car data)))))
8881                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8882                  (gnus-simplify-subject-fuzzy
8883                   (mail-header-subject (gnus-data-header (car data)))))
8884                 (t nil)))
8885          (end-point (save-excursion
8886                       (if (gnus-summary-go-to-next-thread)
8887                           (point) (point-max))))
8888          articles)
8889     (while (and data
8890                 (< (gnus-data-pos (car data)) end-point))
8891       (when (or (not top-subject)
8892                 (string= top-subject
8893                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8894                              (gnus-simplify-subject-fuzzy
8895                               (mail-header-subject
8896                                (gnus-data-header (car data))))
8897                            (gnus-simplify-subject-re
8898                             (mail-header-subject
8899                              (gnus-data-header (car data)))))))
8900         (push (gnus-data-number (car data)) articles))
8901       (unless (and (setq data (cdr data))
8902                    (> (gnus-data-level (car data)) top-level))
8903         (setq data nil)))
8904     ;; Return the list of articles.
8905     (nreverse articles)))
8906
8907 (defun gnus-summary-rethread-current ()
8908   "Rethread the thread the current article is part of."
8909   (interactive)
8910   (let* ((gnus-show-threads t)
8911          (article (gnus-summary-article-number))
8912          (id (mail-header-id (gnus-summary-article-header)))
8913          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8914     (unless id
8915       (error "No article on the current line"))
8916     (gnus-rebuild-thread id)
8917     (gnus-summary-goto-subject article)))
8918
8919 (defun gnus-summary-reparent-thread ()
8920   "Make the current article child of the marked (or previous) article.
8921
8922 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8923 is non-nil or the Subject: of both articles are the same."
8924   (interactive)
8925   (unless (not (gnus-group-read-only-p))
8926     (error "The current newsgroup does not support article editing"))
8927   (unless (<= (length gnus-newsgroup-processable) 1)
8928     (error "No more than one article may be marked"))
8929   (save-window-excursion
8930     (let ((gnus-article-buffer " *reparent*")
8931           (current-article (gnus-summary-article-number))
8932           ;; First grab the marked article, otherwise one line up.
8933           (parent-article (if (not (null gnus-newsgroup-processable))
8934                               (car gnus-newsgroup-processable)
8935                             (save-excursion
8936                               (if (eq (forward-line -1) 0)
8937                                   (gnus-summary-article-number)
8938                                 (error "Beginning of summary buffer"))))))
8939       (unless (not (eq current-article parent-article))
8940         (error "An article may not be self-referential"))
8941       (let ((message-id (mail-header-id
8942                          (gnus-summary-article-header parent-article))))
8943         (unless (and message-id (not (equal message-id "")))
8944           (error "No message-id in desired parent"))
8945         (gnus-with-article current-article
8946           (save-restriction
8947             (goto-char (point-min))
8948             (message-narrow-to-head)
8949             (if (re-search-forward "^References: " nil t)
8950                 (progn
8951                   (re-search-forward "^[^ \t]" nil t)
8952                   (forward-line -1)
8953                   (end-of-line)
8954                   (insert " " message-id))
8955               (insert "References: " message-id "\n"))))
8956         (set-buffer gnus-summary-buffer)
8957         (gnus-summary-unmark-all-processable)
8958         (gnus-summary-update-article current-article)
8959         (gnus-summary-rethread-current)
8960         (gnus-message 3 "Article %d is now the child of article %d"
8961                       current-article parent-article)))))
8962
8963 (defun gnus-summary-toggle-threads (&optional arg)
8964   "Toggle showing conversation threads.
8965 If ARG is positive number, turn showing conversation threads on."
8966   (interactive "P")
8967   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8968     (setq gnus-show-threads
8969           (if (null arg) (not gnus-show-threads)
8970             (> (prefix-numeric-value arg) 0)))
8971     (gnus-summary-prepare)
8972     (gnus-summary-goto-subject current)
8973     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8974     (gnus-summary-position-point)))
8975
8976 (defun gnus-summary-show-all-threads ()
8977   "Show all threads."
8978   (interactive)
8979   (save-excursion
8980     (let ((buffer-read-only nil))
8981       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8982   (gnus-summary-position-point))
8983
8984 (defun gnus-summary-show-thread ()
8985   "Show thread subtrees.
8986 Returns nil if no thread was there to be shown."
8987   (interactive)
8988   (let ((buffer-read-only nil)
8989         (orig (point))
8990         ;; first goto end then to beg, to have point at beg after let
8991         (end (progn (end-of-line) (point)))
8992         (beg (progn (beginning-of-line) (point))))
8993     (prog1
8994         ;; Any hidden lines here?
8995         (search-forward "\r" end t)
8996       (subst-char-in-region beg end ?\^M ?\n t)
8997       (goto-char orig)
8998       (gnus-summary-position-point))))
8999
9000 (defun gnus-summary-hide-all-threads ()
9001   "Hide all thread subtrees."
9002   (interactive)
9003   (save-excursion
9004     (goto-char (point-min))
9005     (gnus-summary-hide-thread)
9006     (while (zerop (gnus-summary-next-thread 1 t))
9007       (gnus-summary-hide-thread)))
9008   (gnus-summary-position-point))
9009
9010 (defun gnus-summary-hide-thread ()
9011   "Hide thread subtrees.
9012 Returns nil if no threads were there to be hidden."
9013   (interactive)
9014   (let ((buffer-read-only nil)
9015         (start (point))
9016         (article (gnus-summary-article-number)))
9017     (goto-char start)
9018     ;; Go forward until either the buffer ends or the subthread
9019     ;; ends.
9020     (when (and (not (eobp))
9021                (or (zerop (gnus-summary-next-thread 1 t))
9022                    (goto-char (point-max))))
9023       (prog1
9024           (if (and (> (point) start)
9025                    (search-backward "\n" start t))
9026               (progn
9027                 (subst-char-in-region start (point) ?\n ?\^M)
9028                 (gnus-summary-goto-subject article))
9029             (goto-char start)
9030             nil)))))
9031
9032 (defun gnus-summary-go-to-next-thread (&optional previous)
9033   "Go to the same level (or less) next thread.
9034 If PREVIOUS is non-nil, go to previous thread instead.
9035 Return the article number moved to, or nil if moving was impossible."
9036   (let ((level (gnus-summary-thread-level))
9037         (way (if previous -1 1))
9038         (beg (point)))
9039     (forward-line way)
9040     (while (and (not (eobp))
9041                 (< level (gnus-summary-thread-level)))
9042       (forward-line way))
9043     (if (eobp)
9044         (progn
9045           (goto-char beg)
9046           nil)
9047       (setq beg (point))
9048       (prog1
9049           (gnus-summary-article-number)
9050         (goto-char beg)))))
9051
9052 (defun gnus-summary-next-thread (n &optional silent)
9053   "Go to the same level next N'th thread.
9054 If N is negative, search backward instead.
9055 Returns the difference between N and the number of skips actually
9056 done.
9057
9058 If SILENT, don't output messages."
9059   (interactive "p")
9060   (let ((backward (< n 0))
9061         (n (abs n)))
9062     (while (and (> n 0)
9063                 (gnus-summary-go-to-next-thread backward))
9064       (decf n))
9065     (unless silent
9066       (gnus-summary-position-point))
9067     (when (and (not silent) (/= 0 n))
9068       (gnus-message 7 "No more threads"))
9069     n))
9070
9071 (defun gnus-summary-prev-thread (n)
9072   "Go to the same level previous N'th thread.
9073 Returns the difference between N and the number of skips actually
9074 done."
9075   (interactive "p")
9076   (gnus-summary-next-thread (- n)))
9077
9078 (defun gnus-summary-go-down-thread ()
9079   "Go down one level in the current thread."
9080   (let ((children (gnus-summary-article-children)))
9081     (when children
9082       (gnus-summary-goto-subject (car children)))))
9083
9084 (defun gnus-summary-go-up-thread ()
9085   "Go up one level in the current thread."
9086   (let ((parent (gnus-summary-article-parent)))
9087     (when parent
9088       (gnus-summary-goto-subject parent))))
9089
9090 (defun gnus-summary-down-thread (n)
9091   "Go down thread N steps.
9092 If N is negative, go up instead.
9093 Returns the difference between N and how many steps down that were
9094 taken."
9095   (interactive "p")
9096   (let ((up (< n 0))
9097         (n (abs n)))
9098     (while (and (> n 0)
9099                 (if up (gnus-summary-go-up-thread)
9100                   (gnus-summary-go-down-thread)))
9101       (setq n (1- n)))
9102     (gnus-summary-position-point)
9103     (when (/= 0 n)
9104       (gnus-message 7 "Can't go further"))
9105     n))
9106
9107 (defun gnus-summary-up-thread (n)
9108   "Go up thread N steps.
9109 If N is negative, go up instead.
9110 Returns the difference between N and how many steps down that were
9111 taken."
9112   (interactive "p")
9113   (gnus-summary-down-thread (- n)))
9114
9115 (defun gnus-summary-top-thread ()
9116   "Go to the top of the thread."
9117   (interactive)
9118   (while (gnus-summary-go-up-thread))
9119   (gnus-summary-article-number))
9120
9121 (defun gnus-summary-kill-thread (&optional unmark)
9122   "Mark articles under current thread as read.
9123 If the prefix argument is positive, remove any kinds of marks.
9124 If the prefix argument is negative, tick articles instead."
9125   (interactive "P")
9126   (when unmark
9127     (setq unmark (prefix-numeric-value unmark)))
9128   (let ((articles (gnus-summary-articles-in-thread)))
9129     (save-excursion
9130       ;; Expand the thread.
9131       (gnus-summary-show-thread)
9132       ;; Mark all the articles.
9133       (while articles
9134         (gnus-summary-goto-subject (car articles))
9135         (cond ((null unmark)
9136                (gnus-summary-mark-article-as-read gnus-killed-mark))
9137               ((> unmark 0)
9138                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9139               (t
9140                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9141         (setq articles (cdr articles))))
9142     ;; Hide killed subtrees.
9143     (and (null unmark)
9144          gnus-thread-hide-killed
9145          (gnus-summary-hide-thread))
9146     ;; If marked as read, go to next unread subject.
9147     (when (null unmark)
9148       ;; Go to next unread subject.
9149       (gnus-summary-next-subject 1 t)))
9150   (gnus-set-mode-line 'summary))
9151
9152 ;; Summary sorting commands
9153
9154 (defun gnus-summary-sort-by-number (&optional reverse)
9155   "Sort the summary buffer by article number.
9156 Argument REVERSE means reverse order."
9157   (interactive "P")
9158   (gnus-summary-sort 'number reverse))
9159
9160 (defun gnus-summary-sort-by-author (&optional reverse)
9161   "Sort the summary buffer by author name alphabetically.
9162 If `case-fold-search' is non-nil, case of letters is ignored.
9163 Argument REVERSE means reverse order."
9164   (interactive "P")
9165   (gnus-summary-sort 'author reverse))
9166
9167 (defun gnus-summary-sort-by-subject (&optional reverse)
9168   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9169 If `case-fold-search' is non-nil, case of letters is ignored.
9170 Argument REVERSE means reverse order."
9171   (interactive "P")
9172   (gnus-summary-sort 'subject reverse))
9173
9174 (defun gnus-summary-sort-by-date (&optional reverse)
9175   "Sort the summary buffer by date.
9176 Argument REVERSE means reverse order."
9177   (interactive "P")
9178   (gnus-summary-sort 'date reverse))
9179
9180 (defun gnus-summary-sort-by-score (&optional reverse)
9181   "Sort the summary buffer by score.
9182 Argument REVERSE means reverse order."
9183   (interactive "P")
9184   (gnus-summary-sort 'score reverse))
9185
9186 (defun gnus-summary-sort-by-lines (&optional reverse)
9187   "Sort the summary buffer by the number of lines.
9188 Argument REVERSE means reverse order."
9189   (interactive "P")
9190   (gnus-summary-sort 'lines reverse))
9191
9192 (defun gnus-summary-sort-by-chars (&optional reverse)
9193   "Sort the summary buffer by article length.
9194 Argument REVERSE means reverse order."
9195   (interactive "P")
9196   (gnus-summary-sort 'chars reverse))
9197
9198 (defun gnus-summary-sort (predicate reverse)
9199   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9200   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9201          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9202          (gnus-thread-sort-functions
9203           (if (not reverse)
9204               thread
9205             `(lambda (t1 t2)
9206                (,thread t2 t1))))
9207          (gnus-sort-gathered-threads-function
9208           gnus-thread-sort-functions)
9209          (gnus-article-sort-functions
9210           (if (not reverse)
9211               article
9212             `(lambda (t1 t2)
9213                (,article t2 t1))))
9214          (buffer-read-only)
9215          (gnus-summary-prepare-hook nil))
9216     ;; We do the sorting by regenerating the threads.
9217     (gnus-summary-prepare)
9218     ;; Hide subthreads if needed.
9219     (when (and gnus-show-threads gnus-thread-hide-subtree)
9220       (gnus-summary-hide-all-threads))))
9221
9222 ;; Summary saving commands.
9223
9224 (defun gnus-summary-save-article (&optional n not-saved)
9225   "Save the current article using the default saver function.
9226 If N is a positive number, save the N next articles.
9227 If N is a negative number, save the N previous articles.
9228 If N is nil and any articles have been marked with the process mark,
9229 save those articles instead.
9230 The variable `gnus-default-article-saver' specifies the saver function."
9231   (interactive "P")
9232   (let* ((articles (gnus-summary-work-articles n))
9233          (save-buffer (save-excursion
9234                         (nnheader-set-temp-buffer " *Gnus Save*")))
9235          (num (length articles))
9236          header file)
9237     (dolist (article articles)
9238       (setq header (gnus-summary-article-header article))
9239       (if (not (vectorp header))
9240           ;; This is a pseudo-article.
9241           (if (assq 'name header)
9242               (gnus-copy-file (cdr (assq 'name header)))
9243             (gnus-message 1 "Article %d is unsaveable" article))
9244         ;; This is a real article.
9245         (save-window-excursion
9246           (gnus-summary-select-article t nil nil article))
9247         (save-excursion
9248           (set-buffer save-buffer)
9249           (erase-buffer)
9250           (insert-buffer-substring gnus-original-article-buffer))
9251         (setq file (gnus-article-save save-buffer file num))
9252         (gnus-summary-remove-process-mark article)
9253         (unless not-saved
9254           (gnus-summary-set-saved-mark article))))
9255     (gnus-kill-buffer save-buffer)
9256     (gnus-summary-position-point)
9257     (gnus-set-mode-line 'summary)
9258     n))
9259
9260 (defun gnus-summary-pipe-output (&optional arg)
9261   "Pipe the current article to a subprocess.
9262 If N is a positive number, pipe the N next articles.
9263 If N is a negative number, pipe the N previous articles.
9264 If N is nil and any articles have been marked with the process mark,
9265 pipe those articles instead."
9266   (interactive "P")
9267   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9268     (gnus-summary-save-article arg t))
9269   (gnus-configure-windows 'pipe))
9270
9271 (defun gnus-summary-save-article-mail (&optional arg)
9272   "Append the current article to an mail file.
9273 If N is a positive number, save the N next articles.
9274 If N is a negative number, save the N previous articles.
9275 If N is nil and any articles have been marked with the process mark,
9276 save those articles instead."
9277   (interactive "P")
9278   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9279     (gnus-summary-save-article arg)))
9280
9281 (defun gnus-summary-save-article-rmail (&optional arg)
9282   "Append the current article to an rmail file.
9283 If N is a positive number, save the N next articles.
9284 If N is a negative number, save the N previous articles.
9285 If N is nil and any articles have been marked with the process mark,
9286 save those articles instead."
9287   (interactive "P")
9288   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9289     (gnus-summary-save-article arg)))
9290
9291 (defun gnus-summary-save-article-file (&optional arg)
9292   "Append the current article to a file.
9293 If N is a positive number, save the N next articles.
9294 If N is a negative number, save the N previous articles.
9295 If N is nil and any articles have been marked with the process mark,
9296 save those articles instead."
9297   (interactive "P")
9298   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9299     (gnus-summary-save-article arg)))
9300
9301 (defun gnus-summary-write-article-file (&optional arg)
9302   "Write the current article to a file, deleting the previous file.
9303 If N is a positive number, save the N next articles.
9304 If N is a negative number, save the N previous articles.
9305 If N is nil and any articles have been marked with the process mark,
9306 save those articles instead."
9307   (interactive "P")
9308   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9309     (gnus-summary-save-article arg)))
9310
9311 (defun gnus-summary-save-article-body-file (&optional arg)
9312   "Append the current article body to a file.
9313 If N is a positive number, save the N next articles.
9314 If N is a negative number, save the N previous articles.
9315 If N is nil and any articles have been marked with the process mark,
9316 save those articles instead."
9317   (interactive "P")
9318   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9319     (gnus-summary-save-article arg)))
9320
9321 (defun gnus-summary-pipe-message (program)
9322   "Pipe the current article through PROGRAM."
9323   (interactive "sProgram: ")
9324   (gnus-summary-select-article)
9325   (let ((mail-header-separator ""))
9326     (gnus-eval-in-buffer-window gnus-article-buffer
9327       (save-restriction
9328         (widen)
9329         (let ((start (window-start))
9330               buffer-read-only)
9331           (message-pipe-buffer-body program)
9332           (set-window-start (get-buffer-window (current-buffer)) start))))))
9333
9334 (defun gnus-get-split-value (methods)
9335   "Return a value based on the split METHODS."
9336   (let (split-name method result match)
9337     (when methods
9338       (save-excursion
9339         (set-buffer gnus-original-article-buffer)
9340         (save-restriction
9341           (nnheader-narrow-to-headers)
9342           (while (and methods (not split-name))
9343             (goto-char (point-min))
9344             (setq method (pop methods))
9345             (setq match (car method))
9346             (when (cond
9347                    ((stringp match)
9348                     ;; Regular expression.
9349                     (ignore-errors
9350                       (re-search-forward match nil t)))
9351                    ((gnus-functionp match)
9352                     ;; Function.
9353                     (save-restriction
9354                       (widen)
9355                       (setq result (funcall match gnus-newsgroup-name))))
9356                    ((consp match)
9357                     ;; Form.
9358                     (save-restriction
9359                       (widen)
9360                       (setq result (eval match)))))
9361               (setq split-name (cdr method))
9362               (cond ((stringp result)
9363                      (push (expand-file-name
9364                             result gnus-article-save-directory)
9365                            split-name))
9366                     ((consp result)
9367                      (setq split-name (append result split-name)))))))))
9368     (nreverse split-name)))
9369
9370 (defun gnus-valid-move-group-p (group)
9371   (and (boundp group)
9372        (symbol-name group)
9373        (symbol-value group)
9374        (gnus-get-function (gnus-find-method-for-group
9375                            (symbol-name group)) 'request-accept-article t)))
9376
9377 (defun gnus-read-move-group-name (prompt default articles prefix)
9378   "Read a group name."
9379   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9380          (minibuffer-confirm-incomplete nil) ; XEmacs
9381          (prom
9382           (format "%s %s to:"
9383                   prompt
9384                   (if (> (length articles) 1)
9385                       (format "these %d articles" (length articles))
9386                     "this article")))
9387          (to-newsgroup
9388           (cond
9389            ((null split-name)
9390             (gnus-completing-read default prom
9391                                   gnus-active-hashtb
9392                                   'gnus-valid-move-group-p
9393                                   nil prefix
9394                                   'gnus-group-history))
9395            ((= 1 (length split-name))
9396             (gnus-completing-read (car split-name) prom
9397                                   gnus-active-hashtb
9398                                   'gnus-valid-move-group-p
9399                                   nil nil
9400                                   'gnus-group-history))
9401            (t
9402             (gnus-completing-read nil prom
9403                                   (mapcar (lambda (el) (list el))
9404                                           (nreverse split-name))
9405                                   nil nil nil
9406                                   'gnus-group-history))))
9407          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9408     (when to-newsgroup
9409       (if (or (string= to-newsgroup "")
9410               (string= to-newsgroup prefix))
9411           (setq to-newsgroup default))
9412       (unless to-newsgroup
9413         (error "No group name entered"))
9414       (or (gnus-active to-newsgroup)
9415           (gnus-activate-group to-newsgroup nil nil to-method)
9416           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9417                                      to-newsgroup))
9418               (or (and (gnus-request-create-group to-newsgroup to-method)
9419                        (gnus-activate-group
9420                         to-newsgroup nil nil to-method)
9421                        (gnus-subscribe-group to-newsgroup))
9422                   (error "Couldn't create group %s" to-newsgroup)))
9423           (error "No such group: %s" to-newsgroup)))
9424     to-newsgroup))
9425
9426 (defun gnus-summary-save-parts (type dir n &optional reverse)
9427   "Save parts matching TYPE to DIR.
9428 If REVERSE, save parts that do not match TYPE."
9429   (interactive
9430    (list (read-string "Save parts of type: " 
9431                       (or (car gnus-summary-save-parts-type-history)
9432                           gnus-summary-save-parts-default-mime)
9433                       'gnus-summary-save-parts-type-history)
9434          (setq gnus-summary-save-parts-last-directory
9435                (read-file-name "Save to directory: " 
9436                                gnus-summary-save-parts-last-directory
9437                                nil t))
9438          current-prefix-arg))
9439   (gnus-summary-iterate n
9440     (let ((gnus-display-mime-function nil)
9441           (gnus-inhibit-treatment t))
9442       (gnus-summary-select-article))
9443     (save-excursion
9444       (set-buffer gnus-article-buffer)
9445       (let ((handles (or gnus-article-mime-handles
9446                          (mm-dissect-buffer) (mm-uu-dissect))))
9447         (when handles
9448           (gnus-summary-save-parts-1 type dir handles reverse)
9449           (unless gnus-article-mime-handles ;; Don't destroy this case.
9450             (mm-destroy-parts handles)))))))
9451
9452 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9453   (if (stringp (car handle))
9454       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9455               (cdr handle))
9456     (when (if reverse
9457               (not (string-match type (mm-handle-media-type handle)))
9458             (string-match type (mm-handle-media-type handle)))
9459       (let ((file (expand-file-name
9460                    (file-name-nondirectory
9461                     (or
9462                      (mail-content-type-get
9463                       (mm-handle-disposition handle) 'filename)
9464                      (concat gnus-newsgroup-name
9465                              "." (number-to-string
9466                                   (cdr gnus-article-current)))))
9467                    dir)))
9468         (unless (file-exists-p file)
9469           (mm-save-part-to-file handle file))))))
9470
9471 ;; Summary extract commands
9472
9473 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9474   (let ((buffer-read-only nil)
9475         (article (gnus-summary-article-number))
9476         after-article b e)
9477     (unless (gnus-summary-goto-subject article)
9478       (error "No such article: %d" article))
9479     (gnus-summary-position-point)
9480     ;; If all commands are to be bunched up on one line, we collect
9481     ;; them here.
9482     (unless gnus-view-pseudos-separately
9483       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9484             files action)
9485         (while ps
9486           (setq action (cdr (assq 'action (car ps))))
9487           (setq files (list (cdr (assq 'name (car ps)))))
9488           (while (and ps (cdr ps)
9489                       (string= (or action "1")
9490                                (or (cdr (assq 'action (cadr ps))) "2")))
9491             (push (cdr (assq 'name (cadr ps))) files)
9492             (setcdr ps (cddr ps)))
9493           (when files
9494             (when (not (string-match "%s" action))
9495               (push " " files))
9496             (push " " files)
9497             (when (assq 'execute (car ps))
9498               (setcdr (assq 'execute (car ps))
9499                       (funcall (if (string-match "%s" action)
9500                                    'format 'concat)
9501                                action
9502                                (mapconcat
9503                                 (lambda (f)
9504                                   (if (equal f " ")
9505                                       f
9506                                     (gnus-quote-arg-for-sh-or-csh f)))
9507                                 files " ")))))
9508           (setq ps (cdr ps)))))
9509     (if (and gnus-view-pseudos (not not-view))
9510         (while pslist
9511           (when (assq 'execute (car pslist))
9512             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9513                                   (eq gnus-view-pseudos 'not-confirm)))
9514           (setq pslist (cdr pslist)))
9515       (save-excursion
9516         (while pslist
9517           (setq after-article (or (cdr (assq 'article (car pslist)))
9518                                   (gnus-summary-article-number)))
9519           (gnus-summary-goto-subject after-article)
9520           (forward-line 1)
9521           (setq b (point))
9522           (insert "    " (file-name-nondirectory
9523                           (cdr (assq 'name (car pslist))))
9524                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9525           (setq e (point))
9526           (forward-line -1)             ; back to `b'
9527           (gnus-add-text-properties
9528            b (1- e) (list 'gnus-number gnus-reffed-article-number
9529                           gnus-mouse-face-prop gnus-mouse-face))
9530           (gnus-data-enter
9531            after-article gnus-reffed-article-number
9532            gnus-unread-mark b (car pslist) 0 (- e b))
9533           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9534           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9535           (setq pslist (cdr pslist)))))))
9536
9537 (defun gnus-pseudos< (p1 p2)
9538   (let ((c1 (cdr (assq 'action p1)))
9539         (c2 (cdr (assq 'action p2))))
9540     (and c1 c2 (string< c1 c2))))
9541
9542 (defun gnus-request-pseudo-article (props)
9543   (cond ((assq 'execute props)
9544          (gnus-execute-command (cdr (assq 'execute props)))))
9545   (let ((gnus-current-article (gnus-summary-article-number)))
9546     (gnus-run-hooks 'gnus-mark-article-hook)))
9547
9548 (defun gnus-execute-command (command &optional automatic)
9549   (save-excursion
9550     (gnus-article-setup-buffer)
9551     (set-buffer gnus-article-buffer)
9552     (setq buffer-read-only nil)
9553     (let ((command (if automatic command
9554                      (read-string "Command: " (cons command 0)))))
9555       (erase-buffer)
9556       (insert "$ " command "\n\n")
9557       (if gnus-view-pseudo-asynchronously
9558           (start-process "gnus-execute" (current-buffer) shell-file-name
9559                          shell-command-switch command)
9560         (call-process shell-file-name nil t nil
9561                       shell-command-switch command)))))
9562
9563 ;; Summary kill commands.
9564
9565 (defun gnus-summary-edit-global-kill (article)
9566   "Edit the \"global\" kill file."
9567   (interactive (list (gnus-summary-article-number)))
9568   (gnus-group-edit-global-kill article))
9569
9570 (defun gnus-summary-edit-local-kill ()
9571   "Edit a local kill file applied to the current newsgroup."
9572   (interactive)
9573   (setq gnus-current-headers (gnus-summary-article-header))
9574   (gnus-group-edit-local-kill
9575    (gnus-summary-article-number) gnus-newsgroup-name))
9576
9577 ;;; Header reading.
9578
9579 (defun gnus-read-header (id &optional header)
9580   "Read the headers of article ID and enter them into the Gnus system."
9581   (let ((group gnus-newsgroup-name)
9582         (gnus-override-method
9583          (or
9584           gnus-override-method
9585           (and (gnus-news-group-p gnus-newsgroup-name)
9586                (car (gnus-refer-article-methods)))))
9587         where)
9588     ;; First we check to see whether the header in question is already
9589     ;; fetched.
9590     (if (stringp id)
9591         ;; This is a Message-ID.
9592         (setq header (or header (gnus-id-to-header id)))
9593       ;; This is an article number.
9594       (setq header (or header (gnus-summary-article-header id))))
9595     (if (and header
9596              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9597         ;; We have found the header.
9598         header
9599       ;; If this is a sparse article, we have to nix out its
9600       ;; previous entry in the thread hashtb.
9601       (when (and header
9602                  (gnus-summary-article-sparse-p (mail-header-number header)))
9603         (let* ((parent (gnus-parent-id (mail-header-references header)))
9604                (thread (and parent (gnus-id-to-thread parent))))
9605           (when thread
9606             (delq (assq header thread) thread))))
9607       ;; We have to really fetch the header to this article.
9608       (save-excursion
9609         (set-buffer nntp-server-buffer)
9610         (when (setq where (gnus-request-head id group))
9611           (nnheader-fold-continuation-lines)
9612           (goto-char (point-max))
9613           (insert ".\n")
9614           (goto-char (point-min))
9615           (insert "211 ")
9616           (princ (cond
9617                   ((numberp id) id)
9618                   ((cdr where) (cdr where))
9619                   (header (mail-header-number header))
9620                   (t gnus-reffed-article-number))
9621                  (current-buffer))
9622           (insert " Article retrieved.\n"))
9623         (if (or (not where)
9624                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9625             ()                          ; Malformed head.
9626           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9627             (when (and (stringp id)
9628                        (not (string= (gnus-group-real-name group)
9629                                      (car where))))
9630               ;; If we fetched by Message-ID and the article came
9631               ;; from a different group, we fudge some bogus article
9632               ;; numbers for this article.
9633               (mail-header-set-number header gnus-reffed-article-number))
9634             (save-excursion
9635               (set-buffer gnus-summary-buffer)
9636               (decf gnus-reffed-article-number)
9637               (gnus-remove-header (mail-header-number header))
9638               (push header gnus-newsgroup-headers)
9639               (setq gnus-current-headers header)
9640               (push (mail-header-number header) gnus-newsgroup-limit)))
9641           header)))))
9642
9643 (defun gnus-remove-header (number)
9644   "Remove header NUMBER from `gnus-newsgroup-headers'."
9645   (if (and gnus-newsgroup-headers
9646            (= number (mail-header-number (car gnus-newsgroup-headers))))
9647       (pop gnus-newsgroup-headers)
9648     (let ((headers gnus-newsgroup-headers))
9649       (while (and (cdr headers)
9650                   (not (= number (mail-header-number (cadr headers)))))
9651         (pop headers))
9652       (when (cdr headers)
9653         (setcdr headers (cddr headers))))))
9654
9655 ;;;
9656 ;;; summary highlights
9657 ;;;
9658
9659 (defun gnus-highlight-selected-summary ()
9660   "Highlight selected article in summary buffer."
9661   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9662   (when gnus-summary-selected-face
9663     (save-excursion
9664       (let* ((beg (progn (beginning-of-line) (point)))
9665              (end (progn (end-of-line) (point)))
9666              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9667              (from (if (get-text-property beg gnus-mouse-face-prop)
9668                        beg
9669                      (or (next-single-property-change
9670                           beg gnus-mouse-face-prop nil end)
9671                          beg)))
9672              (to
9673               (if (= from end)
9674                   (- from 2)
9675                 (or (next-single-property-change
9676                      from gnus-mouse-face-prop nil end)
9677                     end))))
9678         ;; If no mouse-face prop on line we will have to = from = end,
9679         ;; so we highlight the entire line instead.
9680         (when (= (+ to 2) from)
9681           (setq from beg)
9682           (setq to end))
9683         (if gnus-newsgroup-selected-overlay
9684             ;; Move old overlay.
9685             (gnus-move-overlay
9686              gnus-newsgroup-selected-overlay from to (current-buffer))
9687           ;; Create new overlay.
9688           (gnus-overlay-put
9689            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9690            'face gnus-summary-selected-face))))))
9691
9692 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9693 (defun gnus-summary-highlight-line ()
9694   "Highlight current line according to `gnus-summary-highlight'."
9695   (let* ((list gnus-summary-highlight)
9696          (p (point))
9697          (end (progn (end-of-line) (point)))
9698          ;; now find out where the line starts and leave point there.
9699          (beg (progn (beginning-of-line) (point)))
9700          (article (gnus-summary-article-number))
9701          (score (or (cdr (assq (or article gnus-current-article)
9702                                gnus-newsgroup-scored))
9703                     gnus-summary-default-score 0))
9704          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9705          (inhibit-read-only t))
9706     ;; Eval the cars of the lists until we find a match.
9707     (let ((default gnus-summary-default-score))
9708       (while (and list
9709                   (not (eval (caar list))))
9710         (setq list (cdr list))))
9711     (let ((face (cdar list)))
9712       (unless (eq face (get-text-property beg 'face))
9713         (gnus-put-text-property-excluding-characters-with-faces
9714          beg end 'face
9715          (setq face (if (boundp face) (symbol-value face) face)))
9716         (when gnus-summary-highlight-line-function
9717           (funcall gnus-summary-highlight-line-function article face))))
9718     (goto-char p)))
9719
9720 (defun gnus-update-read-articles (group unread &optional compute)
9721   "Update the list of read articles in GROUP."
9722   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9723          (entry (gnus-gethash group gnus-newsrc-hashtb))
9724          (info (nth 2 entry))
9725          (prev 1)
9726          (unread (sort (copy-sequence unread) '<))
9727          read)
9728     (if (or (not info) (not active))
9729         ;; There is no info on this group if it was, in fact,
9730         ;; killed.  Gnus stores no information on killed groups, so
9731         ;; there's nothing to be done.
9732         ;; One could store the information somewhere temporarily,
9733         ;; perhaps...  Hmmm...
9734         ()
9735       ;; Remove any negative articles numbers.
9736       (while (and unread (< (car unread) 0))
9737         (setq unread (cdr unread)))
9738       ;; Remove any expired article numbers
9739       (while (and unread (< (car unread) (car active)))
9740         (setq unread (cdr unread)))
9741       ;; Compute the ranges of read articles by looking at the list of
9742       ;; unread articles.
9743       (while unread
9744         (when (/= (car unread) prev)
9745           (push (if (= prev (1- (car unread))) prev
9746                   (cons prev (1- (car unread))))
9747                 read))
9748         (setq prev (1+ (car unread)))
9749         (setq unread (cdr unread)))
9750       (when (<= prev (cdr active))
9751         (push (cons prev (cdr active)) read))
9752       (setq read (if (> (length read) 1) (nreverse read) read))
9753       (if compute
9754           read
9755         (save-excursion
9756           (let (setmarkundo)
9757             ;; Propagate the read marks to the backend.
9758             (when (gnus-check-backend-function 'request-set-mark group)
9759               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9760                     (add (gnus-remove-from-range read (gnus-info-read info))))
9761                 (when (or add del)
9762                   (unless (gnus-check-group group)
9763                     (error "Can't open server for %s" group))
9764                   (gnus-request-set-mark
9765                    group (delq nil (list (if add (list add 'add '(read)))
9766                                          (if del (list del 'del '(read))))))
9767                   (setq setmarkundo
9768                         `(gnus-request-set-mark
9769                           ,group
9770                           ',(delq nil (list
9771                                        (if del (list del 'add '(read)))
9772                                        (if add (list add 'del '(read))))))))))
9773             (set-buffer gnus-group-buffer)
9774             (gnus-undo-register
9775               `(progn
9776                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9777                  (gnus-info-set-read ',info ',(gnus-info-read info))
9778                  (gnus-get-unread-articles-in-group ',info 
9779                                                     (gnus-active ,group))
9780                  (gnus-group-update-group ,group t)
9781                  ,setmarkundo))))
9782         ;; Enter this list into the group info.
9783         (gnus-info-set-read info read)
9784         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9785         (gnus-get-unread-articles-in-group info (gnus-active group))
9786         t))))
9787
9788 (defun gnus-offer-save-summaries ()
9789   "Offer to save all active summary buffers."
9790   (save-excursion
9791     (let ((buflist (buffer-list))
9792           buffers bufname)
9793       ;; Go through all buffers and find all summaries.
9794       (while buflist
9795         (and (setq bufname (buffer-name (car buflist)))
9796              (string-match "Summary" bufname)
9797              (save-excursion
9798                (set-buffer bufname)
9799                ;; We check that this is, indeed, a summary buffer.
9800                (and (eq major-mode 'gnus-summary-mode)
9801                     ;; Also make sure this isn't bogus.
9802                     gnus-newsgroup-prepared
9803                     ;; Also make sure that this isn't a dead summary buffer.
9804                     (not gnus-dead-summary-mode)))
9805              (push bufname buffers))
9806         (setq buflist (cdr buflist)))
9807       ;; Go through all these summary buffers and offer to save them.
9808       (when buffers
9809         (map-y-or-n-p
9810          "Update summary buffer %s? "
9811          (lambda (buf)
9812            (switch-to-buffer buf)
9813            (gnus-summary-exit))
9814          buffers)))))
9815
9816
9817 ;;; @ for mime-partial
9818 ;;;
9819
9820 (defun gnus-request-partial-message ()
9821   (save-excursion
9822     (let ((number (gnus-summary-article-number))
9823           (group gnus-newsgroup-name)
9824           (mother gnus-article-buffer))
9825       (set-buffer (get-buffer-create " *Partial Article*"))
9826       (erase-buffer)
9827       (setq mime-preview-buffer mother)
9828       (gnus-request-article-this-buffer number group)
9829       (mime-parse-buffer)
9830       )))
9831
9832 (autoload 'mime-combine-message/partial-pieces-automatically
9833   "mime-partial"
9834   "Internal method to combine message/partial messages automatically.")
9835
9836 (mime-add-condition
9837  'action '((type . message)(subtype . partial)
9838            (major-mode . gnus-original-article-mode)
9839            (method . mime-combine-message/partial-pieces-automatically)
9840            (summary-buffer-exp . gnus-summary-buffer)
9841            (request-partial-message-method . gnus-request-partial-message)
9842            ))
9843
9844
9845 ;;; @ for message/rfc822
9846 ;;;
9847
9848 (defun gnus-mime-extract-message/rfc822 (entity situation)
9849   (let (group article num cwin swin cur)
9850     (with-temp-buffer
9851       (mime-insert-entity-content entity)
9852       (setq group (or (cdr (assq 'group situation))
9853                       (completing-read "Group: "
9854                                        gnus-active-hashtb
9855                                        nil
9856                                        (gnus-read-active-file-p)
9857                                        gnus-newsgroup-name))
9858             article (gnus-request-accept-article group)))
9859     (when (and (consp article)
9860                (numberp (setq article (cdr article))))
9861       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9862             cwin (get-buffer-window (current-buffer) t))
9863       (save-window-excursion
9864         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9865             (select-window swin)
9866           (set-buffer gnus-summary-buffer))
9867         (setq cur gnus-current-article)
9868         (forward-line num)
9869         (let (gnus-show-threads)
9870           (gnus-summary-goto-subject article t))
9871         (gnus-summary-clear-mark-forward 1)
9872         (gnus-summary-goto-subject cur))
9873       (when (and cwin (window-frame cwin))
9874         (select-frame (window-frame cwin)))
9875       (when (boundp 'mime-acting-situation-to-override)
9876         (set-alist 'mime-acting-situation-to-override
9877                    'group
9878                    group)
9879         (set-alist 'mime-acting-situation-to-override
9880                    'after-method
9881                    `(progn
9882                       (save-current-buffer
9883                         (set-buffer gnus-group-buffer)
9884                         (gnus-activate-group ,group))
9885                       (gnus-summary-goto-article ,cur
9886                                                  gnus-show-all-headers)))
9887         (set-alist 'mime-acting-situation-to-override
9888                    'number num)))))
9889
9890 (mime-add-condition
9891  'action '((type . message)(subtype . rfc822)
9892            (major-mode . gnus-original-article-mode)
9893            (method . gnus-mime-extract-message/rfc822)
9894            (mode . "extract")
9895            ))
9896
9897 (mime-add-condition
9898  'action '((type . message)(subtype . news)
9899            (major-mode . gnus-original-article-mode)
9900            (method . gnus-mime-extract-message/rfc822)
9901            (mode . "extract")
9902            ))
9903
9904 (defun gnus-mime-extract-multipart (entity situation)
9905   (let ((children (mime-entity-children entity))
9906         mime-acting-situation-to-override
9907         f)
9908     (while children
9909       (mime-play-entity (car children)
9910                         (cons (assq 'mode situation)
9911                               mime-acting-situation-to-override))
9912       (setq children (cdr children)))
9913     (if (setq f (cdr (assq 'after-method
9914                            mime-acting-situation-to-override)))
9915         (eval f)
9916       )))
9917
9918 (mime-add-condition
9919  'action '((type . multipart)
9920            (method . gnus-mime-extract-multipart)
9921            (mode . "extract")
9922            )
9923  'with-default)
9924
9925
9926 ;;; @ end
9927 ;;;
9928
9929 (defun gnus-summary-setup-default-charset ()
9930   "Setup newsgroup default charset."
9931   (if (equal gnus-newsgroup-name "nndraft:drafts")
9932       (setq gnus-newsgroup-charset nil)
9933     (let* ((name (and gnus-newsgroup-name
9934                       (gnus-group-real-name gnus-newsgroup-name)))
9935            (ignored-charsets
9936             (or gnus-newsgroup-ephemeral-ignored-charsets
9937                 (append
9938                  (and gnus-newsgroup-name
9939                       (or (gnus-group-find-parameter gnus-newsgroup-name
9940                                                      'ignored-charsets t)
9941                           (let ((alist gnus-group-ignored-charsets-alist)
9942                                 elem (charsets nil))
9943                             (while (setq elem (pop alist))
9944                               (when (and name
9945                                          (string-match (car elem) name))
9946                                 (setq alist nil
9947                                       charsets (cdr elem))))
9948                             charsets)))
9949                  gnus-newsgroup-ignored-charsets))))
9950       (setq gnus-newsgroup-charset
9951             (or gnus-newsgroup-ephemeral-charset
9952                 (and gnus-newsgroup-name
9953                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9954                          (let ((alist gnus-group-charset-alist)
9955                                elem charset)
9956                            (while (setq elem (pop alist))
9957                              (when (and name
9958                                         (string-match (car elem) name))
9959                                (setq alist nil
9960                                      charset (cadr elem))))
9961                            charset)))
9962                 gnus-default-charset))
9963       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9964            ignored-charsets))))
9965
9966 ;;;
9967 ;;; Mime Commands
9968 ;;;
9969
9970 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9971   "Display the current article buffer fully MIME-buttonized.
9972 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9973 treated as multipart/mixed."
9974   (interactive "P")
9975   (require 'gnus-art)
9976   (let ((gnus-unbuttonized-mime-types nil)
9977         (gnus-mime-display-multipart-as-mixed show-all-parts))
9978     (gnus-summary-show-article)))
9979
9980 (defun gnus-summary-repair-multipart (article)
9981   "Add a Content-Type header to a multipart article without one."
9982   (interactive (list (gnus-summary-article-number)))
9983   (gnus-with-article article
9984     (message-narrow-to-head)
9985     (goto-char (point-max))
9986     (widen)
9987     (when (search-forward "\n--" nil t)
9988       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9989         (message-narrow-to-head)
9990         (message-remove-header "Mime-Version")
9991         (message-remove-header "Content-Type")
9992         (goto-char (point-max))
9993         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9994                         separator))
9995         (insert "Mime-Version: 1.0\n")
9996         (widen))))
9997   (let (gnus-mark-article-hook)
9998     (gnus-summary-select-article t t nil article)))
9999
10000 (defun gnus-summary-toggle-display-buttonized ()
10001   "Toggle the buttonizing of the article buffer."
10002   (interactive)
10003   (require 'gnus-art)
10004   (if (setq gnus-inhibit-mime-unbuttonizing
10005             (not gnus-inhibit-mime-unbuttonizing))
10006       (let ((gnus-unbuttonized-mime-types nil))
10007         (gnus-summary-show-article))
10008     (gnus-summary-show-article)))
10009
10010 ;;;
10011 ;;; Intelli-mouse commmands
10012 ;;;
10013
10014 (defun gnus-wheel-summary-scroll (event)
10015   (interactive "e")
10016   (let ((amount (if (memq 'shift (event-modifiers event))
10017                     (car gnus-wheel-scroll-amount)
10018                   (cdr gnus-wheel-scroll-amount)))
10019         (direction (- (* (static-if (featurep 'xemacs)
10020                              (event-button event)
10021                            (cond ((eq 'mouse-4 (event-basic-type event))
10022                                   4)
10023                                  ((eq 'mouse-5 (event-basic-type event))
10024                                   5)))
10025                          2) 9))
10026         edge)
10027     (gnus-summary-scroll-up (* amount direction))
10028     (when (gnus-eval-in-buffer-window gnus-article-buffer
10029             (save-restriction
10030               (widen)
10031               (and (if (< 0 direction)
10032                        (gnus-article-next-page 0)
10033                      (gnus-article-prev-page 0)
10034                      (bobp))
10035                    (if (setq edge (get-text-property
10036                                    (point-min) 'gnus-wheel-edge))
10037                        (setq edge (* edge direction))
10038                      (setq edge -1))
10039                    (or (plusp edge)
10040                        (let ((buffer-read-only nil)
10041                              (inhibit-read-only t))
10042                          (put-text-property (point-min) (point-max)
10043                                             'gnus-wheel-edge direction)
10044                          nil))
10045                    (or (> edge gnus-wheel-edge-resistance)
10046                        (let ((buffer-read-only nil)
10047                              (inhibit-read-only t))
10048                          (put-text-property (point-min) (point-max)
10049                                             'gnus-wheel-edge
10050                                             (* (1+ edge) direction))
10051                          nil))
10052                    (eq last-command 'gnus-wheel-summary-scroll))))
10053       (gnus-summary-next-article nil nil (minusp direction)))))
10054
10055 (defun gnus-wheel-install ()
10056   "Enable mouse wheel support on summary window."
10057   (when gnus-use-wheel
10058     (let ((keys
10059            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10060       (dolist (key keys)
10061         (define-key gnus-summary-mode-map key
10062           'gnus-wheel-summary-scroll)))))
10063
10064 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10065
10066 ;;;
10067 ;;; Traditional PGP commmands
10068 ;;;
10069
10070 (defun gnus-summary-decrypt-article (&optional force)
10071   "Decrypt the current article in traditional PGP way.
10072 This will have permanent effect only in mail groups.
10073 If FORCE is non-nil, allow editing of articles even in read-only
10074 groups."
10075   (interactive "P")
10076   (gnus-summary-select-article t)
10077   (gnus-eval-in-buffer-window gnus-article-buffer
10078     (save-excursion
10079       (save-restriction
10080         (widen)
10081         (goto-char (point-min))
10082         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10083           (error "Not a traditional PGP message!"))
10084         (let ((armor-start (match-beginning 0)))
10085           (if (and (pgg-decrypt-region armor-start (point-max))
10086                    (or force (not (gnus-group-read-only-p))))
10087               (let ((inhibit-read-only t)
10088                     buffer-read-only)
10089                 (delete-region armor-start
10090                                (progn
10091                                  (re-search-forward "^-+END PGP" nil t)
10092                                  (beginning-of-line 2)
10093                                  (point)))
10094                 (insert-buffer-substring pgg-output-buffer))))))))
10095
10096 (defun gnus-summary-verify-article ()
10097   "Verify the current article in traditional PGP way."
10098   (interactive)
10099   (save-excursion
10100     (set-buffer gnus-original-article-buffer)
10101     (goto-char (point-min))
10102     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10103       (error "Not a traditional PGP message!"))
10104     (re-search-forward "^-+END PGP" nil t)
10105     (beginning-of-line 2)
10106     (call-interactively (function pgg-verify-region))))
10107
10108 ;;;
10109 ;;; Generic summary marking commands
10110 ;;;
10111
10112 (defvar gnus-summary-marking-alist
10113   '((read gnus-del-mark "d")
10114     (unread gnus-unread-mark "u")
10115     (ticked gnus-ticked-mark "!")
10116     (dormant gnus-dormant-mark "?")
10117     (expirable gnus-expirable-mark "e"))
10118   "An alist of names/marks/keystrokes.")
10119
10120 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10121 (defvar gnus-summary-mark-map)
10122
10123 (defun gnus-summary-make-all-marking-commands ()
10124   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10125   (dolist (elem gnus-summary-marking-alist)
10126     (apply 'gnus-summary-make-marking-command elem)))
10127
10128 (defun gnus-summary-make-marking-command (name mark keystroke)
10129   (let ((map (make-sparse-keymap)))
10130     (define-key gnus-summary-generic-mark-map keystroke map)
10131     (dolist (lway `((next "next" next nil "n")
10132                     (next-unread "next unread" next t "N")
10133                     (prev "previous" prev nil "p")
10134                     (prev-unread "previous unread" prev t "P")
10135                     (nomove "" nil nil ,keystroke)))
10136       (let ((func (gnus-summary-make-marking-command-1
10137                    mark (car lway) lway name)))
10138         (setq func (eval func))
10139         (define-key map (nth 4 lway) func)))))
10140
10141 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10142   `(defun ,(intern
10143             (format "gnus-summary-put-mark-as-%s%s"
10144                     name (if (eq way 'nomove)
10145                              ""
10146                            (concat "-" (symbol-name way)))))
10147      (n)
10148      ,(format
10149        "Mark the current article as %s%s.
10150 If N, the prefix, then repeat N times.
10151 If N is negative, move in reverse order.
10152 The difference between N and the actual number of articles marked is
10153 returned."
10154        name (car (cdr lway)))
10155      (interactive "p")
10156      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10157
10158 (defun gnus-summary-generic-mark (n mark move unread)
10159   "Mark N articles with MARK."
10160   (unless (eq major-mode 'gnus-summary-mode)
10161     (error "This command can only be used in the summary buffer"))
10162   (gnus-summary-show-thread)
10163   (let ((nummove
10164          (cond
10165           ((eq move 'next) 1)
10166           ((eq move 'prev) -1)
10167           (t 0))))
10168     (if (zerop nummove)
10169         (setq n 1)
10170       (when (< n 0)
10171         (setq n (abs n)
10172               nummove (* -1 nummove))))
10173     (while (and (> n 0)
10174                 (gnus-summary-mark-article nil mark)
10175                 (zerop (gnus-summary-next-subject nummove unread t)))
10176       (setq n (1- n)))
10177     (when (/= 0 n)
10178       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10179     (gnus-summary-recenter)
10180     (gnus-summary-position-point)
10181     (gnus-set-mode-line 'summary)
10182     n))
10183
10184 (gnus-summary-make-all-marking-commands)
10185
10186 (gnus-ems-redefine)
10187
10188 (provide 'gnus-sum)
10189
10190 (run-hooks 'gnus-sum-load-hook)
10191
10192 ;;; gnus-sum.el ends here