* Makefile.in (install-package-ja): Compile and install lisp files first.
[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 (defconst gnus-summary-local-variables
1157   '(gnus-newsgroup-name
1158     gnus-newsgroup-begin gnus-newsgroup-end
1159     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1160     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1161     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1162     gnus-newsgroup-unselected gnus-newsgroup-marked
1163     gnus-newsgroup-reads gnus-newsgroup-saved
1164     gnus-newsgroup-replied gnus-newsgroup-expirable
1165     gnus-newsgroup-processable gnus-newsgroup-killed
1166     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1167     gnus-newsgroup-unsendable
1168     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1169     gnus-newsgroup-headers gnus-newsgroup-threads
1170     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1171     gnus-current-article gnus-current-headers gnus-have-all-headers
1172     gnus-last-article gnus-article-internal-prepare-hook
1173     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1174     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1175     gnus-thread-expunge-below
1176     gnus-score-alist gnus-current-score-file
1177     (gnus-summary-expunge-below . global)
1178     (gnus-summary-mark-below . global)
1179     (gnus-orphan-score . global)
1180     gnus-newsgroup-active gnus-scores-exclude-files
1181     gnus-newsgroup-history gnus-newsgroup-ancient
1182     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1183     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1184     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1185     (gnus-newsgroup-expunged-tally . 0)
1186     gnus-cache-removable-articles gnus-newsgroup-cached
1187     gnus-newsgroup-data gnus-newsgroup-data-reverse
1188     gnus-newsgroup-limit gnus-newsgroup-limits
1189     gnus-newsgroup-charset
1190     gnus-newsgroup-incorporated)
1191   "Variables that are buffer-local to the summary buffers.")
1192
1193 ;; Byte-compiler warning.
1194 (defvar gnus-article-mode-map)
1195
1196 ;; Subject simplification.
1197
1198 (defun gnus-simplify-whitespace (str)
1199   "Remove excessive whitespace from STR."
1200   (let ((mystr str))
1201     ;; Multiple spaces.
1202     (while (string-match "[ \t][ \t]+" mystr)
1203       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1204                           " "
1205                           (substring mystr (match-end 0)))))
1206     ;; Leading spaces.
1207     (when (string-match "^[ \t]+" mystr)
1208       (setq mystr (substring mystr (match-end 0))))
1209     ;; Trailing spaces.
1210     (when (string-match "[ \t]+$" mystr)
1211       (setq mystr (substring mystr 0 (match-beginning 0))))
1212     mystr))
1213
1214 (defsubst gnus-simplify-subject-re (subject)
1215   "Remove \"Re:\" from subject lines."
1216   (if (string-match "^[Rr][Ee]: *" subject)
1217       (substring subject (match-end 0))
1218     subject))
1219
1220 (defun gnus-simplify-subject (subject &optional re-only)
1221   "Remove `Re:' and words in parentheses.
1222 If RE-ONLY is non-nil, strip leading `Re:'s only."
1223   (let ((case-fold-search t))           ;Ignore case.
1224     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1225     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1226       (setq subject (substring subject (match-end 0))))
1227     ;; Remove uninteresting prefixes.
1228     (when (and (not re-only)
1229                gnus-simplify-ignored-prefixes
1230                (string-match gnus-simplify-ignored-prefixes subject))
1231       (setq subject (substring subject (match-end 0))))
1232     ;; Remove words in parentheses from end.
1233     (unless re-only
1234       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1235         (setq subject (substring subject 0 (match-beginning 0)))))
1236     ;; Return subject string.
1237     subject))
1238
1239 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1240 ;; all whitespace.
1241 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1242   (goto-char (point-min))
1243   (while (re-search-forward regexp nil t)
1244     (replace-match (or newtext ""))))
1245
1246 (defun gnus-simplify-buffer-fuzzy ()
1247   "Simplify string in the buffer fuzzily.
1248 The string in the accessible portion of the current buffer is simplified.
1249 It is assumed to be a single-line subject.
1250 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1251 matter is removed.  Additional things can be deleted by setting
1252 `gnus-simplify-subject-fuzzy-regexp'."
1253   (let ((case-fold-search t)
1254         (modified-tick))
1255     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1256
1257     (while (not (eq modified-tick (buffer-modified-tick)))
1258       (setq modified-tick (buffer-modified-tick))
1259       (cond
1260        ((listp gnus-simplify-subject-fuzzy-regexp)
1261         (mapcar 'gnus-simplify-buffer-fuzzy-step
1262                 gnus-simplify-subject-fuzzy-regexp))
1263        (gnus-simplify-subject-fuzzy-regexp
1264         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1265       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1266       (gnus-simplify-buffer-fuzzy-step
1267        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1268       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1269
1270     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1271     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1272     (gnus-simplify-buffer-fuzzy-step " $")
1273     (gnus-simplify-buffer-fuzzy-step "^ +")))
1274
1275 (defun gnus-simplify-subject-fuzzy (subject)
1276   "Simplify a subject string fuzzily.
1277 See `gnus-simplify-buffer-fuzzy' for details."
1278   (save-excursion
1279     (gnus-set-work-buffer)
1280     (let ((case-fold-search t))
1281       ;; Remove uninteresting prefixes.
1282       (when (and gnus-simplify-ignored-prefixes
1283                  (string-match gnus-simplify-ignored-prefixes subject))
1284         (setq subject (substring subject (match-end 0))))
1285       (insert subject)
1286       (inline (gnus-simplify-buffer-fuzzy))
1287       (buffer-string))))
1288
1289 (defsubst gnus-simplify-subject-fully (subject)
1290   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1291   (cond
1292    (gnus-simplify-subject-functions
1293     (gnus-map-function gnus-simplify-subject-functions subject))
1294    ((null gnus-summary-gather-subject-limit)
1295     (gnus-simplify-subject-re subject))
1296    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1297     (gnus-simplify-subject-fuzzy subject))
1298    ((numberp gnus-summary-gather-subject-limit)
1299     (gnus-limit-string (gnus-simplify-subject-re subject)
1300                        gnus-summary-gather-subject-limit))
1301    (t
1302     subject)))
1303
1304 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1305   "Check whether two subjects are equal.
1306 If optional argument simple-first is t, first argument is already
1307 simplified."
1308   (cond
1309    ((null simple-first)
1310     (equal (gnus-simplify-subject-fully s1)
1311            (gnus-simplify-subject-fully s2)))
1312    (t
1313     (equal s1
1314            (gnus-simplify-subject-fully s2)))))
1315
1316 (defun gnus-summary-bubble-group ()
1317   "Increase the score of the current group.
1318 This is a handy function to add to `gnus-summary-exit-hook' to
1319 increase the score of each group you read."
1320   (gnus-group-add-score gnus-newsgroup-name))
1321
1322 \f
1323 ;;;
1324 ;;; Gnus summary mode
1325 ;;;
1326
1327 (put 'gnus-summary-mode 'mode-class 'special)
1328
1329 (when t
1330   ;; Non-orthogonal keys
1331
1332   (gnus-define-keys gnus-summary-mode-map
1333     " " gnus-summary-next-page
1334     "\177" gnus-summary-prev-page
1335     [delete] gnus-summary-prev-page
1336     [backspace] gnus-summary-prev-page
1337     "\r" gnus-summary-scroll-up
1338     "\M-\r" gnus-summary-scroll-down
1339     "n" gnus-summary-next-unread-article
1340     "p" gnus-summary-prev-unread-article
1341     "N" gnus-summary-next-article
1342     "P" gnus-summary-prev-article
1343     "\M-\C-n" gnus-summary-next-same-subject
1344     "\M-\C-p" gnus-summary-prev-same-subject
1345     "\M-n" gnus-summary-next-unread-subject
1346     "\M-p" gnus-summary-prev-unread-subject
1347     "." gnus-summary-first-unread-article
1348     "," gnus-summary-best-unread-article
1349     "\M-s" gnus-summary-search-article-forward
1350     "\M-r" gnus-summary-search-article-backward
1351     "<" gnus-summary-beginning-of-article
1352     ">" gnus-summary-end-of-article
1353     "j" gnus-summary-goto-article
1354     "^" gnus-summary-refer-parent-article
1355     "\M-^" gnus-summary-refer-article
1356     "u" gnus-summary-tick-article-forward
1357     "!" gnus-summary-tick-article-forward
1358     "U" gnus-summary-tick-article-backward
1359     "d" gnus-summary-mark-as-read-forward
1360     "D" gnus-summary-mark-as-read-backward
1361     "E" gnus-summary-mark-as-expirable
1362     "\M-u" gnus-summary-clear-mark-forward
1363     "\M-U" gnus-summary-clear-mark-backward
1364     "k" gnus-summary-kill-same-subject-and-select
1365     "\C-k" gnus-summary-kill-same-subject
1366     "\M-\C-k" gnus-summary-kill-thread
1367     "\M-\C-l" gnus-summary-lower-thread
1368     "e" gnus-summary-edit-article
1369     "#" gnus-summary-mark-as-processable
1370     "\M-#" gnus-summary-unmark-as-processable
1371     "\M-\C-t" gnus-summary-toggle-threads
1372     "\M-\C-s" gnus-summary-show-thread
1373     "\M-\C-h" gnus-summary-hide-thread
1374     "\M-\C-f" gnus-summary-next-thread
1375     "\M-\C-b" gnus-summary-prev-thread
1376     [(meta down)] gnus-summary-next-thread
1377     [(meta up)] gnus-summary-prev-thread
1378     "\M-\C-u" gnus-summary-up-thread
1379     "\M-\C-d" gnus-summary-down-thread
1380     "&" gnus-summary-execute-command
1381     "c" gnus-summary-catchup-and-exit
1382     "\C-w" gnus-summary-mark-region-as-read
1383     "\C-t" gnus-summary-toggle-truncation
1384     "?" gnus-summary-mark-as-dormant
1385     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1386     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1387     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1388     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1389     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1390     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1391     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1392     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1393     "=" gnus-summary-expand-window
1394     "\C-x\C-s" gnus-summary-reselect-current-group
1395     "\M-g" gnus-summary-rescan-group
1396     "w" gnus-summary-stop-page-breaking
1397     "\C-c\C-r" gnus-summary-caesar-message
1398     "\M-t" gnus-summary-toggle-mime
1399     "f" gnus-summary-followup
1400     "F" gnus-summary-followup-with-original
1401     "C" gnus-summary-cancel-article
1402     "r" gnus-summary-reply
1403     "R" gnus-summary-reply-with-original
1404     "\C-c\C-f" gnus-summary-mail-forward
1405     "o" gnus-summary-save-article
1406     "\C-o" gnus-summary-save-article-mail
1407     "|" gnus-summary-pipe-output
1408     "\M-k" gnus-summary-edit-local-kill
1409     "\M-K" gnus-summary-edit-global-kill
1410     ;; "V" gnus-version
1411     "\C-c\C-d" gnus-summary-describe-group
1412     "q" gnus-summary-exit
1413     "Q" gnus-summary-exit-no-update
1414     "\C-c\C-i" gnus-info-find-node
1415     gnus-mouse-2 gnus-mouse-pick-article
1416     "m" gnus-summary-mail-other-window
1417     "a" gnus-summary-post-news
1418     "x" gnus-summary-limit-to-unread
1419     "s" gnus-summary-isearch-article
1420     "t" gnus-article-toggle-headers
1421     "g" gnus-summary-show-article
1422     "l" gnus-summary-goto-last-article
1423     "v" gnus-summary-preview-mime-message
1424     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1425     "\C-d" gnus-summary-enter-digest-group
1426     "\M-\C-d" gnus-summary-read-document
1427     "\M-\C-e" gnus-summary-edit-parameters
1428     "\M-\C-a" gnus-summary-customize-parameters
1429     "\C-c\C-b" gnus-bug
1430     "*" gnus-cache-enter-article
1431     "\M-*" gnus-cache-remove-article
1432     "\M-&" gnus-summary-universal-argument
1433     "\C-l" gnus-recenter
1434     "I" gnus-summary-increase-score
1435     "L" gnus-summary-lower-score
1436     "\M-i" gnus-symbolic-argument
1437     "h" gnus-summary-select-article-buffer
1438
1439     "V" gnus-summary-score-map
1440     "X" gnus-uu-extract-map
1441     "S" gnus-summary-send-map)
1442
1443   ;; Sort of orthogonal keymap
1444   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1445     "t" gnus-summary-tick-article-forward
1446     "!" gnus-summary-tick-article-forward
1447     "d" gnus-summary-mark-as-read-forward
1448     "r" gnus-summary-mark-as-read-forward
1449     "c" gnus-summary-clear-mark-forward
1450     " " gnus-summary-clear-mark-forward
1451     "e" gnus-summary-mark-as-expirable
1452     "x" gnus-summary-mark-as-expirable
1453     "?" gnus-summary-mark-as-dormant
1454     "b" gnus-summary-set-bookmark
1455     "B" gnus-summary-remove-bookmark
1456     "#" gnus-summary-mark-as-processable
1457     "\M-#" gnus-summary-unmark-as-processable
1458     "S" gnus-summary-limit-include-expunged
1459     "C" gnus-summary-catchup
1460     "H" gnus-summary-catchup-to-here
1461     "\C-c" gnus-summary-catchup-all
1462     "k" gnus-summary-kill-same-subject-and-select
1463     "K" gnus-summary-kill-same-subject
1464     "P" gnus-uu-mark-map)
1465
1466   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1467     "c" gnus-summary-clear-above
1468     "u" gnus-summary-tick-above
1469     "m" gnus-summary-mark-above
1470     "k" gnus-summary-kill-below)
1471
1472   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1473     "/" gnus-summary-limit-to-subject
1474     "n" gnus-summary-limit-to-articles
1475     "w" gnus-summary-pop-limit
1476     "s" gnus-summary-limit-to-subject
1477     "a" gnus-summary-limit-to-author
1478     "u" gnus-summary-limit-to-unread
1479     "m" gnus-summary-limit-to-marks
1480     "M" gnus-summary-limit-exclude-marks
1481     "v" gnus-summary-limit-to-score
1482     "*" gnus-summary-limit-include-cached
1483     "D" gnus-summary-limit-include-dormant
1484     "T" gnus-summary-limit-include-thread
1485     "d" gnus-summary-limit-exclude-dormant
1486     "t" gnus-summary-limit-to-age
1487     "x" gnus-summary-limit-to-extra
1488     "E" gnus-summary-limit-include-expunged
1489     "c" gnus-summary-limit-exclude-childless-dormant
1490     "C" gnus-summary-limit-mark-excluded-as-read)
1491
1492   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1493     "n" gnus-summary-next-unread-article
1494     "p" gnus-summary-prev-unread-article
1495     "N" gnus-summary-next-article
1496     "P" gnus-summary-prev-article
1497     "\C-n" gnus-summary-next-same-subject
1498     "\C-p" gnus-summary-prev-same-subject
1499     "\M-n" gnus-summary-next-unread-subject
1500     "\M-p" gnus-summary-prev-unread-subject
1501     "f" gnus-summary-first-unread-article
1502     "b" gnus-summary-best-unread-article
1503     "j" gnus-summary-goto-article
1504     "g" gnus-summary-goto-subject
1505     "l" gnus-summary-goto-last-article
1506     "o" gnus-summary-pop-article)
1507
1508   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1509     "k" gnus-summary-kill-thread
1510     "l" gnus-summary-lower-thread
1511     "i" gnus-summary-raise-thread
1512     "T" gnus-summary-toggle-threads
1513     "t" gnus-summary-rethread-current
1514     "^" gnus-summary-reparent-thread
1515     "s" gnus-summary-show-thread
1516     "S" gnus-summary-show-all-threads
1517     "h" gnus-summary-hide-thread
1518     "H" gnus-summary-hide-all-threads
1519     "n" gnus-summary-next-thread
1520     "p" gnus-summary-prev-thread
1521     "u" gnus-summary-up-thread
1522     "o" gnus-summary-top-thread
1523     "d" gnus-summary-down-thread
1524     "#" gnus-uu-mark-thread
1525     "\M-#" gnus-uu-unmark-thread)
1526
1527   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1528     "g" gnus-summary-prepare
1529     "c" gnus-summary-insert-cached-articles)
1530
1531   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1532     "c" gnus-summary-catchup-and-exit
1533     "C" gnus-summary-catchup-all-and-exit
1534     "E" gnus-summary-exit-no-update
1535     "J" gnus-summary-jump-to-other-group
1536     "Q" gnus-summary-exit
1537     "Z" gnus-summary-exit
1538     "n" gnus-summary-catchup-and-goto-next-group
1539     "R" gnus-summary-reselect-current-group
1540     "G" gnus-summary-rescan-group
1541     "N" gnus-summary-next-group
1542     "s" gnus-summary-save-newsrc
1543     "P" gnus-summary-prev-group)
1544
1545   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1546     " " gnus-summary-next-page
1547     "n" gnus-summary-next-page
1548     "\177" gnus-summary-prev-page
1549     [delete] gnus-summary-prev-page
1550     "p" gnus-summary-prev-page
1551     "\r" gnus-summary-scroll-up
1552     "\M-\r" gnus-summary-scroll-down
1553     "<" gnus-summary-beginning-of-article
1554     ">" gnus-summary-end-of-article
1555     "b" gnus-summary-beginning-of-article
1556     "e" gnus-summary-end-of-article
1557     "^" gnus-summary-refer-parent-article
1558     "r" gnus-summary-refer-parent-article
1559     "D" gnus-summary-enter-digest-group
1560     "R" gnus-summary-refer-references
1561     "T" gnus-summary-refer-thread
1562     "g" gnus-summary-show-article
1563     "s" gnus-summary-isearch-article
1564     "P" gnus-summary-print-article
1565     "t" gnus-article-babel
1566     "d" gnus-summary-decrypt-article
1567     "v" gnus-summary-verify-article)
1568
1569   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1570     "b" gnus-article-add-buttons
1571     "B" gnus-article-add-buttons-to-head
1572     "o" gnus-article-treat-overstrike
1573     "e" gnus-article-emphasize
1574     "w" gnus-article-fill-cited-article
1575     "Q" gnus-article-fill-long-lines
1576     "C" gnus-article-capitalize-sentences
1577     "c" gnus-article-remove-cr
1578     "Z" gnus-article-decode-HZ
1579     "f" gnus-article-display-x-face
1580     "l" gnus-summary-stop-page-breaking
1581     "r" gnus-summary-caesar-message
1582     "t" gnus-article-toggle-headers
1583     "v" gnus-summary-verbose-headers
1584     "m" gnus-summary-toggle-mime
1585     "H" gnus-article-strip-headers-in-body
1586     "d" gnus-article-treat-dumbquotes
1587     "s" gnus-smiley-display)
1588
1589   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1590     "a" gnus-article-hide
1591     "h" gnus-article-toggle-headers
1592     "b" gnus-article-hide-boring-headers
1593     "s" gnus-article-hide-signature
1594     "c" gnus-article-hide-citation
1595     "C" gnus-article-hide-citation-in-followups
1596     "l" gnus-article-hide-list-identifiers
1597     "p" gnus-article-hide-pgp
1598     "B" gnus-article-strip-banner
1599     "P" gnus-article-hide-pem
1600     "\C-c" gnus-article-hide-citation-maybe)
1601
1602   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1603     "a" gnus-article-highlight
1604     "h" gnus-article-highlight-headers
1605     "c" gnus-article-highlight-citation
1606     "s" gnus-article-highlight-signature)
1607
1608   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1609     "z" gnus-article-date-ut
1610     "u" gnus-article-date-ut
1611     "l" gnus-article-date-local
1612     "e" gnus-article-date-lapsed
1613     "o" gnus-article-date-original
1614     "i" gnus-article-date-iso8601
1615     "s" gnus-article-date-user)
1616
1617   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1618     "t" gnus-article-remove-trailing-blank-lines
1619     "l" gnus-article-strip-leading-blank-lines
1620     "m" gnus-article-strip-multiple-blank-lines
1621     "a" gnus-article-strip-blank-lines
1622     "A" gnus-article-strip-all-blank-lines
1623     "s" gnus-article-strip-leading-space
1624     "e" gnus-article-strip-trailing-space)
1625
1626   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1627     "v" gnus-version
1628     "f" gnus-summary-fetch-faq
1629     "d" gnus-summary-describe-group
1630     "h" gnus-summary-describe-briefly
1631     "i" gnus-info-find-node)
1632
1633   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1634     "e" gnus-summary-expire-articles
1635     "\M-\C-e" gnus-summary-expire-articles-now
1636     "\177" gnus-summary-delete-article
1637     [delete] gnus-summary-delete-article
1638     [backspace] gnus-summary-delete-article
1639     "m" gnus-summary-move-article
1640     "r" gnus-summary-respool-article
1641     "w" gnus-summary-edit-article
1642     "c" gnus-summary-copy-article
1643     "B" gnus-summary-crosspost-article
1644     "q" gnus-summary-respool-query
1645     "t" gnus-summary-respool-trace
1646     "i" gnus-summary-import-article
1647     "p" gnus-summary-article-posted-p)
1648
1649   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1650     "o" gnus-summary-save-article
1651     "m" gnus-summary-save-article-mail
1652     "F" gnus-summary-write-article-file
1653     "r" gnus-summary-save-article-rmail
1654     "f" gnus-summary-save-article-file
1655     "b" gnus-summary-save-article-body-file
1656     "h" gnus-summary-save-article-folder
1657     "v" gnus-summary-save-article-vm
1658     "p" gnus-summary-pipe-output
1659     "s" gnus-soup-add-article)
1660
1661   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1662     "b" gnus-summary-display-buttonized
1663     "m" gnus-summary-repair-multipart
1664     "v" gnus-article-view-part
1665     "o" gnus-article-save-part
1666     "c" gnus-article-copy-part
1667     "e" gnus-article-externalize-part
1668     "i" gnus-article-inline-part
1669     "|" gnus-article-pipe-part))
1670
1671 (defun gnus-summary-make-menu-bar ()
1672   (gnus-turn-off-edit-menu 'summary)
1673
1674   (unless (boundp 'gnus-summary-misc-menu)
1675
1676     (easy-menu-define
1677      gnus-summary-kill-menu gnus-summary-mode-map ""
1678      (cons
1679       "Score"
1680       (nconc
1681        (list
1682         ["Enter score..." gnus-summary-score-entry t]
1683         ["Customize" gnus-score-customize t])
1684        (gnus-make-score-map 'increase)
1685        (gnus-make-score-map 'lower)
1686        '(("Mark"
1687           ["Kill below" gnus-summary-kill-below t]
1688           ["Mark above" gnus-summary-mark-above t]
1689           ["Tick above" gnus-summary-tick-above t]
1690           ["Clear above" gnus-summary-clear-above t])
1691          ["Current score" gnus-summary-current-score t]
1692          ["Set score" gnus-summary-set-score t]
1693          ["Switch current score file..." gnus-score-change-score-file t]
1694          ["Set mark below..." gnus-score-set-mark-below t]
1695          ["Set expunge below..." gnus-score-set-expunge-below t]
1696          ["Edit current score file" gnus-score-edit-current-scores t]
1697          ["Edit score file" gnus-score-edit-file t]
1698          ["Trace score" gnus-score-find-trace t]
1699          ["Find words" gnus-score-find-favourite-words t]
1700          ["Rescore buffer" gnus-summary-rescore t]
1701          ["Increase score..." gnus-summary-increase-score t]
1702          ["Lower score..." gnus-summary-lower-score t]))))
1703
1704     ;; Define both the Article menu in the summary buffer and the equivalent
1705     ;; Commands menu in the article buffer here for consistency.
1706     (let ((innards
1707            '(("Hide"
1708               ["All" gnus-article-hide t]
1709               ["Headers" gnus-article-toggle-headers t]
1710               ["Signature" gnus-article-hide-signature t]
1711               ["Citation" gnus-article-hide-citation t]
1712               ["List identifiers" gnus-article-hide-list-identifiers t]
1713               ["PGP" gnus-article-hide-pgp t]
1714               ["Banner" gnus-article-strip-banner t]
1715               ["Boring headers" gnus-article-hide-boring-headers t])
1716              ("Highlight"
1717               ["All" gnus-article-highlight t]
1718               ["Headers" gnus-article-highlight-headers t]
1719               ["Signature" gnus-article-highlight-signature t]
1720               ["Citation" gnus-article-highlight-citation t])
1721              ("Date"
1722               ["Local" gnus-article-date-local t]
1723               ["ISO8601" gnus-article-date-iso8601 t]
1724               ["UT" gnus-article-date-ut t]
1725               ["Original" gnus-article-date-original t]
1726               ["Lapsed" gnus-article-date-lapsed t]
1727               ["User-defined" gnus-article-date-user t])
1728              ("Washing"
1729               ("Remove Blanks"
1730                ["Leading" gnus-article-strip-leading-blank-lines t]
1731                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1732                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1733                ["All of the above" gnus-article-strip-blank-lines t]
1734                ["All" gnus-article-strip-all-blank-lines t]
1735                ["Leading space" gnus-article-strip-leading-space t]
1736                ["Trailing space" gnus-article-strip-trailing-space t])
1737               ["Overstrike" gnus-article-treat-overstrike t]
1738               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1739               ["Emphasis" gnus-article-emphasize t]
1740               ["Word wrap" gnus-article-fill-cited-article t]
1741               ["Fill long lines" gnus-article-fill-long-lines t]
1742               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1743               ["CR" gnus-article-remove-cr t]
1744               ["Show X-Face" gnus-article-display-x-face t]
1745               ["Rot 13" gnus-summary-caesar-message t]
1746               ["Unix pipe" gnus-summary-pipe-message t]
1747               ["Add buttons" gnus-article-add-buttons t]
1748               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1749               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1750               ["Toggle MIME" gnus-summary-toggle-mime t]
1751               ["Verbose header" gnus-summary-verbose-headers t]
1752               ["Toggle header" gnus-summary-toggle-header t]
1753               ["Toggle smileys" gnus-smiley-display t]
1754               ["HZ" gnus-article-decode-HZ t])
1755              ("Output"
1756               ["Save in default format" gnus-summary-save-article t]
1757               ["Save in file" gnus-summary-save-article-file t]
1758               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1759               ["Save in MH folder" gnus-summary-save-article-folder t]
1760               ["Save in VM folder" gnus-summary-save-article-vm t]
1761               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1762               ["Save body in file" gnus-summary-save-article-body-file t]
1763               ["Pipe through a filter" gnus-summary-pipe-output t]
1764               ["Add to SOUP packet" gnus-soup-add-article t]
1765               ["Print" gnus-summary-print-article t])
1766              ("Backend"
1767               ["Respool article..." gnus-summary-respool-article t]
1768               ["Move article..." gnus-summary-move-article
1769                (gnus-check-backend-function
1770                 'request-move-article gnus-newsgroup-name)]
1771               ["Copy article..." gnus-summary-copy-article t]
1772               ["Crosspost article..." gnus-summary-crosspost-article
1773                (gnus-check-backend-function
1774                 'request-replace-article gnus-newsgroup-name)]
1775               ["Import file..." gnus-summary-import-article t]
1776               ["Check if posted" gnus-summary-article-posted-p t]
1777               ["Edit article" gnus-summary-edit-article
1778                (not (gnus-group-read-only-p))]
1779               ["Delete article" gnus-summary-delete-article
1780                (gnus-check-backend-function
1781                 'request-expire-articles gnus-newsgroup-name)]
1782               ["Query respool" gnus-summary-respool-query t]
1783               ["Trace respool" gnus-summary-respool-trace t]
1784               ["Delete expirable articles" gnus-summary-expire-articles-now
1785                (gnus-check-backend-function
1786                 'request-expire-articles gnus-newsgroup-name)])
1787              ("Extract"
1788               ["Uudecode" gnus-uu-decode-uu t]
1789               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1790               ["Unshar" gnus-uu-decode-unshar t]
1791               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1792               ["Save" gnus-uu-decode-save t]
1793               ["Binhex" gnus-uu-decode-binhex t]
1794               ["Postscript" gnus-uu-decode-postscript t])
1795              ("Cache"
1796               ["Enter article" gnus-cache-enter-article t]
1797               ["Remove article" gnus-cache-remove-article t])
1798              ["Translate" gnus-article-babel t]
1799              ["Select article buffer" gnus-summary-select-article-buffer t]
1800              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1801              ["Isearch article..." gnus-summary-isearch-article t]
1802              ["Beginning of the article" gnus-summary-beginning-of-article t]
1803              ["End of the article" gnus-summary-end-of-article t]
1804              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1805              ["Fetch referenced articles" gnus-summary-refer-references t]
1806              ["Fetch current thread" gnus-summary-refer-thread t]
1807              ["Fetch article with id..." gnus-summary-refer-article t]
1808              ["Redisplay" gnus-summary-show-article t])))
1809       (easy-menu-define
1810        gnus-summary-article-menu gnus-summary-mode-map ""
1811        (cons "Article" innards))
1812
1813       (easy-menu-define
1814        gnus-article-commands-menu gnus-article-mode-map ""
1815        (cons "Commands" innards)))
1816
1817     (easy-menu-define
1818      gnus-summary-thread-menu gnus-summary-mode-map ""
1819      '("Threads"
1820        ["Toggle threading" gnus-summary-toggle-threads t]
1821        ["Hide threads" gnus-summary-hide-all-threads t]
1822        ["Show threads" gnus-summary-show-all-threads t]
1823        ["Hide thread" gnus-summary-hide-thread t]
1824        ["Show thread" gnus-summary-show-thread t]
1825        ["Go to next thread" gnus-summary-next-thread t]
1826        ["Go to previous thread" gnus-summary-prev-thread t]
1827        ["Go down thread" gnus-summary-down-thread t]
1828        ["Go up thread" gnus-summary-up-thread t]
1829        ["Top of thread" gnus-summary-top-thread t]
1830        ["Mark thread as read" gnus-summary-kill-thread t]
1831        ["Lower thread score" gnus-summary-lower-thread t]
1832        ["Raise thread score" gnus-summary-raise-thread t]
1833        ["Rethread current" gnus-summary-rethread-current t]))
1834
1835     (easy-menu-define
1836      gnus-summary-post-menu gnus-summary-mode-map ""
1837      '("Post"
1838        ["Post an article" gnus-summary-post-news t]
1839        ["Followup" gnus-summary-followup t]
1840        ["Followup and yank" gnus-summary-followup-with-original t]
1841        ["Supersede article" gnus-summary-supersede-article t]
1842        ["Cancel article" gnus-summary-cancel-article t]
1843        ["Reply" gnus-summary-reply t]
1844        ["Reply and yank" gnus-summary-reply-with-original t]
1845        ["Wide reply" gnus-summary-wide-reply t]
1846        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1847        ["Mail forward" gnus-summary-mail-forward t]
1848        ["Post forward" gnus-summary-post-forward t]
1849        ["Digest and mail" gnus-uu-digest-mail-forward t]
1850        ["Digest and post" gnus-uu-digest-post-forward t]
1851        ["Resend message" gnus-summary-resend-message t]
1852        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1853        ["Send a mail" gnus-summary-mail-other-window t]
1854        ["Uuencode and post" gnus-uu-post-news t]
1855        ["Followup via news" gnus-summary-followup-to-mail t]
1856        ["Followup via news and yank"
1857         gnus-summary-followup-to-mail-with-original t]
1858        ;;("Draft"
1859        ;;["Send" gnus-summary-send-draft t]
1860        ;;["Send bounced" gnus-resend-bounced-mail t])
1861        ))
1862
1863     (easy-menu-define
1864      gnus-summary-misc-menu gnus-summary-mode-map ""
1865      '("Misc"
1866        ("Mark Read"
1867         ["Mark as read" gnus-summary-mark-as-read-forward t]
1868         ["Mark same subject and select"
1869          gnus-summary-kill-same-subject-and-select t]
1870         ["Mark same subject" gnus-summary-kill-same-subject t]
1871         ["Catchup" gnus-summary-catchup t]
1872         ["Catchup all" gnus-summary-catchup-all t]
1873         ["Catchup to here" gnus-summary-catchup-to-here t]
1874         ["Catchup region" gnus-summary-mark-region-as-read t]
1875         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1876        ("Mark Various"
1877         ["Tick" gnus-summary-tick-article-forward t]
1878         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1879         ["Remove marks" gnus-summary-clear-mark-forward t]
1880         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1881         ["Set bookmark" gnus-summary-set-bookmark t]
1882         ["Remove bookmark" gnus-summary-remove-bookmark t])
1883        ("Mark Limit"
1884         ["Marks..." gnus-summary-limit-to-marks t]
1885         ["Subject..." gnus-summary-limit-to-subject t]
1886         ["Author..." gnus-summary-limit-to-author t]
1887         ["Age..." gnus-summary-limit-to-age t]
1888         ["Extra..." gnus-summary-limit-to-extra t]
1889         ["Score" gnus-summary-limit-to-score t]
1890         ["Unread" gnus-summary-limit-to-unread t]
1891         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1892         ["Articles" gnus-summary-limit-to-articles t]
1893         ["Pop limit" gnus-summary-pop-limit t]
1894         ["Show dormant" gnus-summary-limit-include-dormant t]
1895         ["Hide childless dormant"
1896          gnus-summary-limit-exclude-childless-dormant t]
1897         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1898         ["Hide marked" gnus-summary-limit-exclude-marks t]
1899         ["Show expunged" gnus-summary-show-all-expunged t])
1900        ("Process Mark"
1901         ["Set mark" gnus-summary-mark-as-processable t]
1902         ["Remove mark" gnus-summary-unmark-as-processable t]
1903         ["Remove all marks" gnus-summary-unmark-all-processable t]
1904         ["Mark above" gnus-uu-mark-over t]
1905         ["Mark series" gnus-uu-mark-series t]
1906         ["Mark region" gnus-uu-mark-region t]
1907         ["Unmark region" gnus-uu-unmark-region t]
1908         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1909         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1910         ["Mark all" gnus-uu-mark-all t]
1911         ["Mark buffer" gnus-uu-mark-buffer t]
1912         ["Mark sparse" gnus-uu-mark-sparse t]
1913         ["Mark thread" gnus-uu-mark-thread t]
1914         ["Unmark thread" gnus-uu-unmark-thread t]
1915         ("Process Mark Sets"
1916          ["Kill" gnus-summary-kill-process-mark t]
1917          ["Yank" gnus-summary-yank-process-mark
1918           gnus-newsgroup-process-stack]
1919          ["Save" gnus-summary-save-process-mark t]))
1920        ("Scroll article"
1921         ["Page forward" gnus-summary-next-page t]
1922         ["Page backward" gnus-summary-prev-page t]
1923         ["Line forward" gnus-summary-scroll-up t])
1924        ("Move"
1925         ["Next unread article" gnus-summary-next-unread-article t]
1926         ["Previous unread article" gnus-summary-prev-unread-article t]
1927         ["Next article" gnus-summary-next-article t]
1928         ["Previous article" gnus-summary-prev-article t]
1929         ["Next unread subject" gnus-summary-next-unread-subject t]
1930         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1931         ["Next article same subject" gnus-summary-next-same-subject t]
1932         ["Previous article same subject" gnus-summary-prev-same-subject t]
1933         ["First unread article" gnus-summary-first-unread-article t]
1934         ["Best unread article" gnus-summary-best-unread-article t]
1935         ["Go to subject number..." gnus-summary-goto-subject t]
1936         ["Go to article number..." gnus-summary-goto-article t]
1937         ["Go to the last article" gnus-summary-goto-last-article t]
1938         ["Pop article off history" gnus-summary-pop-article t])
1939        ("Sort"
1940         ["Sort by number" gnus-summary-sort-by-number t]
1941         ["Sort by author" gnus-summary-sort-by-author t]
1942         ["Sort by subject" gnus-summary-sort-by-subject t]
1943         ["Sort by date" gnus-summary-sort-by-date t]
1944         ["Sort by score" gnus-summary-sort-by-score t]
1945         ["Sort by lines" gnus-summary-sort-by-lines t]
1946         ["Sort by characters" gnus-summary-sort-by-chars t])
1947        ("Help"
1948         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1949         ["Describe group" gnus-summary-describe-group t]
1950         ["Read manual" gnus-info-find-node t])
1951        ("Modes"
1952         ["Pick and read" gnus-pick-mode t]
1953         ["Binary" gnus-binary-mode t])
1954        ("Regeneration"
1955         ["Regenerate" gnus-summary-prepare t]
1956         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1957         ["Toggle threading" gnus-summary-toggle-threads t])
1958        ["Filter articles..." gnus-summary-execute-command t]
1959        ["Run command on subjects..." gnus-summary-universal-argument t]
1960        ["Search articles forward..." gnus-summary-search-article-forward t]
1961        ["Search articles backward..." gnus-summary-search-article-backward t]
1962        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1963        ["Expand window" gnus-summary-expand-window t]
1964        ["Expire expirable articles" gnus-summary-expire-articles
1965         (gnus-check-backend-function
1966          'request-expire-articles gnus-newsgroup-name)]
1967        ["Edit local kill file" gnus-summary-edit-local-kill t]
1968        ["Edit main kill file" gnus-summary-edit-global-kill t]
1969        ["Edit group parameters" gnus-summary-edit-parameters t]
1970        ["Customize group parameters" gnus-summary-customize-parameters t]
1971        ["Send a bug report" gnus-bug t]
1972        ("Exit"
1973         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1974         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1975         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1976         ["Exit group" gnus-summary-exit t]
1977         ["Exit group without updating" gnus-summary-exit-no-update t]
1978         ["Exit and goto next group" gnus-summary-next-group t]
1979         ["Exit and goto prev group" gnus-summary-prev-group t]
1980         ["Reselect group" gnus-summary-reselect-current-group t]
1981         ["Rescan group" gnus-summary-rescan-group t]
1982         ["Update dribble" gnus-summary-save-newsrc t])))
1983
1984     (gnus-run-hooks 'gnus-summary-menu-hook)))
1985
1986 (defun gnus-score-set-default (var value)
1987   "A version of set that updates the GNU Emacs menu-bar."
1988   (set var value)
1989   ;; It is the message that forces the active status to be updated.
1990   (message ""))
1991
1992 (defun gnus-make-score-map (type)
1993   "Make a summary score map of type TYPE."
1994   (if t
1995       nil
1996     (let ((headers '(("author" "from" string)
1997                      ("subject" "subject" string)
1998                      ("article body" "body" string)
1999                      ("article head" "head" string)
2000                      ("xref" "xref" string)
2001                      ("extra header" "extra" string)
2002                      ("lines" "lines" number)
2003                      ("followups to author" "followup" string)))
2004           (types '((number ("less than" <)
2005                            ("greater than" >)
2006                            ("equal" =))
2007                    (string ("substring" s)
2008                            ("exact string" e)
2009                            ("fuzzy string" f)
2010                            ("regexp" r))))
2011           (perms '(("temporary" (current-time-string))
2012                    ("permanent" nil)
2013                    ("immediate" now)))
2014           header)
2015       (list
2016        (apply
2017         'nconc
2018         (list
2019          (if (eq type 'lower)
2020              "Lower score"
2021            "Increase score"))
2022         (let (outh)
2023           (while headers
2024             (setq header (car headers))
2025             (setq outh
2026                   (cons
2027                    (apply
2028                     'nconc
2029                     (list (car header))
2030                     (let ((ts (cdr (assoc (nth 2 header) types)))
2031                           outt)
2032                       (while ts
2033                         (setq outt
2034                               (cons
2035                                (apply
2036                                 'nconc
2037                                 (list (caar ts))
2038                                 (let ((ps perms)
2039                                       outp)
2040                                   (while ps
2041                                     (setq outp
2042                                           (cons
2043                                            (vector
2044                                             (caar ps)
2045                                             (list
2046                                              'gnus-summary-score-entry
2047                                              (nth 1 header)
2048                                              (if (or (string= (nth 1 header)
2049                                                               "head")
2050                                                      (string= (nth 1 header)
2051                                                               "body"))
2052                                                  ""
2053                                                (list 'gnus-summary-header
2054                                                      (nth 1 header)))
2055                                              (list 'quote (nth 1 (car ts)))
2056                                              (list 'gnus-score-delta-default
2057                                                    nil)
2058                                              (nth 1 (car ps))
2059                                              t)
2060                                             t)
2061                                            outp))
2062                                     (setq ps (cdr ps)))
2063                                   (list (nreverse outp))))
2064                                outt))
2065                         (setq ts (cdr ts)))
2066                       (list (nreverse outt))))
2067                    outh))
2068             (setq headers (cdr headers)))
2069           (list (nreverse outh))))))))
2070
2071 \f
2072
2073 (defun gnus-summary-mode (&optional group)
2074   "Major mode for reading articles.
2075
2076 All normal editing commands are switched off.
2077 \\<gnus-summary-mode-map>
2078 Each line in this buffer represents one article.  To read an
2079 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2080 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2081 respectively.
2082
2083 You can also post articles and send mail from this buffer.  To
2084 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2085 of an article, type `\\[gnus-summary-reply]'.
2086
2087 There are approx. one gazillion commands you can execute in this
2088 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2089
2090 The following commands are available:
2091
2092 \\{gnus-summary-mode-map}"
2093   (interactive)
2094   (when (gnus-visual-p 'summary-menu 'menu)
2095     (gnus-summary-make-menu-bar))
2096   (kill-all-local-variables)
2097   (gnus-summary-make-local-variables)
2098   (gnus-make-thread-indent-array)
2099   (gnus-simplify-mode-line)
2100   (setq major-mode 'gnus-summary-mode)
2101   (setq mode-name "Summary")
2102   (make-local-variable 'minor-mode-alist)
2103   (use-local-map gnus-summary-mode-map)
2104   (buffer-disable-undo)
2105   (setq buffer-read-only t)             ;Disable modification
2106   (setq truncate-lines t)
2107   (setq selective-display t)
2108   (setq selective-display-ellipses t)   ;Display `...'
2109   (gnus-summary-set-display-table)
2110   (gnus-set-default-directory)
2111   (setq gnus-newsgroup-name group)
2112   (unless (gnus-news-group-p group)
2113     (setq gnus-newsgroup-incorporated
2114           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2115   (make-local-variable 'gnus-summary-line-format)
2116   (make-local-variable 'gnus-summary-line-format-spec)
2117   (make-local-variable 'gnus-summary-dummy-line-format)
2118   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2119   (make-local-variable 'gnus-summary-mark-positions)
2120   (make-local-hook 'pre-command-hook)
2121   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2122   (gnus-run-hooks 'gnus-summary-mode-hook)
2123   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2124   (gnus-update-summary-mark-positions))
2125
2126 (defun gnus-summary-make-local-variables ()
2127   "Make all the local summary buffer variables."
2128   (let (global)
2129     (dolist (local gnus-summary-local-variables)
2130       (if (consp local)
2131           (progn
2132             (if (eq (cdr local) 'global)
2133                 ;; Copy the global value of the variable.
2134                 (setq global (symbol-value (car local)))
2135               ;; Use the value from the list.
2136               (setq global (eval (cdr local))))
2137             (set (make-local-variable (car local)) global))
2138         ;; Simple nil-valued local variable.
2139         (set (make-local-variable local) nil)))))
2140
2141 (defun gnus-summary-clear-local-variables ()
2142   (let ((locals gnus-summary-local-variables))
2143     (while locals
2144       (if (consp (car locals))
2145           (and (vectorp (caar locals))
2146                (set (caar locals) nil))
2147         (and (vectorp (car locals))
2148              (set (car locals) nil)))
2149       (setq locals (cdr locals)))))
2150
2151 ;; Summary data functions.
2152
2153 (defmacro gnus-data-number (data)
2154   `(car ,data))
2155
2156 (defmacro gnus-data-set-number (data number)
2157   `(setcar ,data ,number))
2158
2159 (defmacro gnus-data-mark (data)
2160   `(nth 1 ,data))
2161
2162 (defmacro gnus-data-set-mark (data mark)
2163   `(setcar (nthcdr 1 ,data) ,mark))
2164
2165 (defmacro gnus-data-pos (data)
2166   `(nth 2 ,data))
2167
2168 (defmacro gnus-data-set-pos (data pos)
2169   `(setcar (nthcdr 2 ,data) ,pos))
2170
2171 (defmacro gnus-data-header (data)
2172   `(nth 3 ,data))
2173
2174 (defmacro gnus-data-set-header (data header)
2175   `(setcar (nthcdr 3 ,data) ,header))
2176
2177 (defmacro gnus-data-level (data)
2178   `(nth 4 ,data))
2179
2180 (defmacro gnus-data-unread-p (data)
2181   `(= (nth 1 ,data) gnus-unread-mark))
2182
2183 (defmacro gnus-data-read-p (data)
2184   `(/= (nth 1 ,data) gnus-unread-mark))
2185
2186 (defmacro gnus-data-pseudo-p (data)
2187   `(consp (nth 3 ,data)))
2188
2189 (defmacro gnus-data-find (number)
2190   `(assq ,number gnus-newsgroup-data))
2191
2192 (defmacro gnus-data-find-list (number &optional data)
2193   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2194      (memq (assq ,number bdata)
2195            bdata)))
2196
2197 (defmacro gnus-data-make (number mark pos header level)
2198   `(list ,number ,mark ,pos ,header ,level))
2199
2200 (defun gnus-data-enter (after-article number mark pos header level offset)
2201   (let ((data (gnus-data-find-list after-article)))
2202     (unless data
2203       (error "No such article: %d" after-article))
2204     (setcdr data (cons (gnus-data-make number mark pos header level)
2205                        (cdr data)))
2206     (setq gnus-newsgroup-data-reverse nil)
2207     (gnus-data-update-list (cddr data) offset)))
2208
2209 (defun gnus-data-enter-list (after-article list &optional offset)
2210   (when list
2211     (let ((data (and after-article (gnus-data-find-list after-article)))
2212           (ilist list))
2213       (if (not (or data
2214                    after-article))
2215           (let ((odata gnus-newsgroup-data))
2216             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2217             (when offset
2218               (gnus-data-update-list odata offset)))
2219         ;; Find the last element in the list to be spliced into the main
2220         ;; list.
2221         (while (cdr list)
2222           (setq list (cdr list)))
2223         (if (not data)
2224             (progn
2225               (setcdr list gnus-newsgroup-data)
2226               (setq gnus-newsgroup-data ilist)
2227               (when offset
2228                 (gnus-data-update-list (cdr list) offset)))
2229           (setcdr list (cdr data))
2230           (setcdr data ilist)
2231           (when offset
2232             (gnus-data-update-list (cdr list) offset))))
2233       (setq gnus-newsgroup-data-reverse nil))))
2234
2235 (defun gnus-data-remove (article &optional offset)
2236   (let ((data gnus-newsgroup-data))
2237     (if (= (gnus-data-number (car data)) article)
2238         (progn
2239           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2240                 gnus-newsgroup-data-reverse nil)
2241           (when offset
2242             (gnus-data-update-list gnus-newsgroup-data offset)))
2243       (while (cdr data)
2244         (when (= (gnus-data-number (cadr data)) article)
2245           (setcdr data (cddr data))
2246           (when offset
2247             (gnus-data-update-list (cdr data) offset))
2248           (setq data nil
2249                 gnus-newsgroup-data-reverse nil))
2250         (setq data (cdr data))))))
2251
2252 (defmacro gnus-data-list (backward)
2253   `(if ,backward
2254        (or gnus-newsgroup-data-reverse
2255            (setq gnus-newsgroup-data-reverse
2256                  (reverse gnus-newsgroup-data)))
2257      gnus-newsgroup-data))
2258
2259 (defun gnus-data-update-list (data offset)
2260   "Add OFFSET to the POS of all data entries in DATA."
2261   (setq gnus-newsgroup-data-reverse nil)
2262   (while data
2263     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2264     (setq data (cdr data))))
2265
2266 (defun gnus-summary-article-pseudo-p (article)
2267   "Say whether this article is a pseudo article or not."
2268   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2269
2270 (defmacro gnus-summary-article-sparse-p (article)
2271   "Say whether this article is a sparse article or not."
2272   `(memq ,article gnus-newsgroup-sparse))
2273
2274 (defmacro gnus-summary-article-ancient-p (article)
2275   "Say whether this article is a sparse article or not."
2276   `(memq ,article gnus-newsgroup-ancient))
2277
2278 (defun gnus-article-parent-p (number)
2279   "Say whether this article is a parent or not."
2280   (let ((data (gnus-data-find-list number)))
2281     (and (cdr data)                     ; There has to be an article after...
2282          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2283             (gnus-data-level (nth 1 data))))))
2284
2285 (defun gnus-article-children (number)
2286   "Return a list of all children to NUMBER."
2287   (let* ((data (gnus-data-find-list number))
2288          (level (gnus-data-level (car data)))
2289          children)
2290     (setq data (cdr data))
2291     (while (and data
2292                 (= (gnus-data-level (car data)) (1+ level)))
2293       (push (gnus-data-number (car data)) children)
2294       (setq data (cdr data)))
2295     children))
2296
2297 (defmacro gnus-summary-skip-intangible ()
2298   "If the current article is intangible, then jump to a different article."
2299   '(let ((to (get-text-property (point) 'gnus-intangible)))
2300      (and to (gnus-summary-goto-subject to))))
2301
2302 (defmacro gnus-summary-article-intangible-p ()
2303   "Say whether this article is intangible or not."
2304   '(get-text-property (point) 'gnus-intangible))
2305
2306 (defun gnus-article-read-p (article)
2307   "Say whether ARTICLE is read or not."
2308   (not (or (memq article gnus-newsgroup-marked)
2309            (memq article gnus-newsgroup-unreads)
2310            (memq article gnus-newsgroup-unselected)
2311            (memq article gnus-newsgroup-dormant))))
2312
2313 ;; Some summary mode macros.
2314
2315 (defmacro gnus-summary-article-number ()
2316   "The article number of the article on the current line.
2317 If there isn's an article number here, then we return the current
2318 article number."
2319   '(progn
2320      (gnus-summary-skip-intangible)
2321      (or (get-text-property (point) 'gnus-number)
2322          (gnus-summary-last-subject))))
2323
2324 (defmacro gnus-summary-article-header (&optional number)
2325   "Return the header of article NUMBER."
2326   `(gnus-data-header (gnus-data-find
2327                       ,(or number '(gnus-summary-article-number)))))
2328
2329 (defmacro gnus-summary-thread-level (&optional number)
2330   "Return the level of thread that starts with article NUMBER."
2331   `(if (and (eq gnus-summary-make-false-root 'dummy)
2332             (get-text-property (point) 'gnus-intangible))
2333        0
2334      (gnus-data-level (gnus-data-find
2335                        ,(or number '(gnus-summary-article-number))))))
2336
2337 (defmacro gnus-summary-article-mark (&optional number)
2338   "Return the mark of article NUMBER."
2339   `(gnus-data-mark (gnus-data-find
2340                     ,(or number '(gnus-summary-article-number)))))
2341
2342 (defmacro gnus-summary-article-pos (&optional number)
2343   "Return the position of the line of article NUMBER."
2344   `(gnus-data-pos (gnus-data-find
2345                    ,(or number '(gnus-summary-article-number)))))
2346
2347 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2348 (defmacro gnus-summary-article-subject (&optional number)
2349   "Return current subject string or nil if nothing."
2350   `(let ((headers
2351           ,(if number
2352                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2353              '(gnus-data-header (assq (gnus-summary-article-number)
2354                                       gnus-newsgroup-data)))))
2355      (and headers
2356           (vectorp headers)
2357           (mail-header-subject headers))))
2358
2359 (defmacro gnus-summary-article-score (&optional number)
2360   "Return current article score."
2361   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2362                   gnus-newsgroup-scored))
2363        gnus-summary-default-score 0))
2364
2365 (defun gnus-summary-article-children (&optional number)
2366   "Return a list of article numbers that are children of article NUMBER."
2367   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2368          (level (gnus-data-level (car data)))
2369          l children)
2370     (while (and (setq data (cdr data))
2371                 (> (setq l (gnus-data-level (car data))) level))
2372       (and (= (1+ level) l)
2373            (push (gnus-data-number (car data))
2374                  children)))
2375     (nreverse children)))
2376
2377 (defun gnus-summary-article-parent (&optional number)
2378   "Return the article number of the parent of article NUMBER."
2379   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2380                                     (gnus-data-list t)))
2381          (level (gnus-data-level (car data))))
2382     (if (zerop level)
2383         ()                              ; This is a root.
2384       ;; We search until we find an article with a level less than
2385       ;; this one.  That function has to be the parent.
2386       (while (and (setq data (cdr data))
2387                   (not (< (gnus-data-level (car data)) level))))
2388       (and data (gnus-data-number (car data))))))
2389
2390 (defun gnus-unread-mark-p (mark)
2391   "Say whether MARK is the unread mark."
2392   (= mark gnus-unread-mark))
2393
2394 (defun gnus-read-mark-p (mark)
2395   "Say whether MARK is one of the marks that mark as read.
2396 This is all marks except unread, ticked, dormant, and expirable."
2397   (not (or (= mark gnus-unread-mark)
2398            (= mark gnus-ticked-mark)
2399            (= mark gnus-dormant-mark)
2400            (= mark gnus-expirable-mark))))
2401
2402 (defmacro gnus-article-mark (number)
2403   "Return the MARK of article NUMBER.
2404 This macro should only be used when computing the mark the \"first\"
2405 time; i.e., when generating the summary lines.  After that,
2406 `gnus-summary-article-mark' should be used to examine the
2407 marks of articles."
2408   `(cond
2409     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2410     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2411     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2412     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2413     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2414     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2415     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2416     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2417            gnus-ancient-mark))))
2418
2419 ;; Saving hidden threads.
2420
2421 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2422 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2423
2424 (defmacro gnus-save-hidden-threads (&rest forms)
2425   "Save hidden threads, eval FORMS, and restore the hidden threads."
2426   (let ((config (make-symbol "config")))
2427     `(let ((,config (gnus-hidden-threads-configuration)))
2428        (unwind-protect
2429            (save-excursion
2430              ,@forms)
2431          (gnus-restore-hidden-threads-configuration ,config)))))
2432
2433 (defun gnus-data-compute-positions ()
2434   "Compute the positions of all articles."
2435   (setq gnus-newsgroup-data-reverse nil)
2436   (let ((data gnus-newsgroup-data))
2437     (save-excursion
2438       (gnus-save-hidden-threads
2439         (gnus-summary-show-all-threads)
2440         (goto-char (point-min))
2441         (while data
2442           (while (get-text-property (point) 'gnus-intangible)
2443             (forward-line 1))
2444           (gnus-data-set-pos (car data) (+ (point) 3))
2445           (setq data (cdr data))
2446           (forward-line 1))))))
2447
2448 (defun gnus-hidden-threads-configuration ()
2449   "Return the current hidden threads configuration."
2450   (save-excursion
2451     (let (config)
2452       (goto-char (point-min))
2453       (while (search-forward "\r" nil t)
2454         (push (1- (point)) config))
2455       config)))
2456
2457 (defun gnus-restore-hidden-threads-configuration (config)
2458   "Restore hidden threads configuration from CONFIG."
2459   (save-excursion
2460     (let (point buffer-read-only)
2461       (while (setq point (pop config))
2462         (when (and (< point (point-max))
2463                    (goto-char point)
2464                    (eq (char-after) ?\n))
2465           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2466
2467 ;; Various summary mode internalish functions.
2468
2469 (defun gnus-mouse-pick-article (e)
2470   (interactive "e")
2471   (mouse-set-point e)
2472   (gnus-summary-next-page nil t))
2473
2474 (defun gnus-summary-set-display-table ()
2475   "Change the display table.
2476 Odd characters have a tendency to mess
2477 up nicely formatted displays - we make all possible glyphs
2478 display only a single character."
2479
2480   ;; We start from the standard display table, if any.
2481   (let ((table (or (copy-sequence standard-display-table)
2482                    (make-display-table)))
2483         (i 32))
2484     ;; Nix out all the control chars...
2485     (while (>= (setq i (1- i)) 0)
2486       (aset table i [??]))
2487     ;; ... but not newline and cr, of course.  (cr is necessary for the
2488     ;; selective display).
2489     (aset table ?\n nil)
2490     (aset table ?\r nil)
2491     ;; We keep TAB as well.
2492     (aset table ?\t nil)
2493     ;; We nix out any glyphs over 126 that are not set already.
2494     (let ((i 256))
2495       (while (>= (setq i (1- i)) 127)
2496         ;; Only modify if the entry is nil.
2497         (unless (aref table i)
2498           (aset table i [??]))))
2499     (setq buffer-display-table table)))
2500
2501 (defun gnus-summary-setup-buffer (group)
2502   "Initialize summary buffer."
2503   (let ((buffer (concat "*Summary " group "*")))
2504     (if (get-buffer buffer)
2505         (progn
2506           (set-buffer buffer)
2507           (setq gnus-summary-buffer (current-buffer))
2508           (not gnus-newsgroup-prepared))
2509       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2510       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2511       (gnus-summary-mode group)
2512       (when gnus-carpal
2513         (gnus-carpal-setup-buffer 'summary))
2514       (unless gnus-single-article-buffer
2515         (make-local-variable 'gnus-article-buffer)
2516         (make-local-variable 'gnus-article-current)
2517         (make-local-variable 'gnus-original-article-buffer))
2518       (setq gnus-newsgroup-name group)
2519       t)))
2520
2521 (defun gnus-set-global-variables ()
2522   "Set the global equivalents of the buffer-local variables.
2523 They are set to the latest values they had.  These reflect the summary
2524 buffer that was in action when the last article was fetched."
2525   (when (eq major-mode 'gnus-summary-mode)
2526     (setq gnus-summary-buffer (current-buffer))
2527     (let ((name gnus-newsgroup-name)
2528           (marked gnus-newsgroup-marked)
2529           (unread gnus-newsgroup-unreads)
2530           (headers gnus-current-headers)
2531           (data gnus-newsgroup-data)
2532           (summary gnus-summary-buffer)
2533           (article-buffer gnus-article-buffer)
2534           (original gnus-original-article-buffer)
2535           (gac gnus-article-current)
2536           (reffed gnus-reffed-article-number)
2537           (score-file gnus-current-score-file)
2538           (default-charset gnus-newsgroup-charset))
2539       (save-excursion
2540         (set-buffer gnus-group-buffer)
2541         (setq gnus-newsgroup-name name
2542               gnus-newsgroup-marked marked
2543               gnus-newsgroup-unreads unread
2544               gnus-current-headers headers
2545               gnus-newsgroup-data data
2546               gnus-article-current gac
2547               gnus-summary-buffer summary
2548               gnus-article-buffer article-buffer
2549               gnus-original-article-buffer original
2550               gnus-reffed-article-number reffed
2551               gnus-current-score-file score-file
2552               gnus-newsgroup-charset default-charset)
2553         ;; The article buffer also has local variables.
2554         (when (gnus-buffer-live-p gnus-article-buffer)
2555           (set-buffer gnus-article-buffer)
2556           (setq gnus-summary-buffer summary))))))
2557
2558 (defun gnus-summary-article-unread-p (article)
2559   "Say whether ARTICLE is unread or not."
2560   (memq article gnus-newsgroup-unreads))
2561
2562 (defun gnus-summary-first-article-p (&optional article)
2563   "Return whether ARTICLE is the first article in the buffer."
2564   (if (not (setq article (or article (gnus-summary-article-number))))
2565       nil
2566     (eq article (caar gnus-newsgroup-data))))
2567
2568 (defun gnus-summary-last-article-p (&optional article)
2569   "Return whether ARTICLE is the last article in the buffer."
2570   (if (not (setq article (or article (gnus-summary-article-number))))
2571       ;; All non-existent numbers are the last article.  :-)
2572       t
2573     (not (cdr (gnus-data-find-list article)))))
2574
2575 (defun gnus-make-thread-indent-array ()
2576   (let ((n 200))
2577     (unless (and gnus-thread-indent-array
2578                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2579       (setq gnus-thread-indent-array (make-vector 201 "")
2580             gnus-thread-indent-array-level gnus-thread-indent-level)
2581       (while (>= n 0)
2582         (aset gnus-thread-indent-array n
2583               (make-string (* n gnus-thread-indent-level) ? ))
2584         (setq n (1- n))))))
2585
2586 (defun gnus-update-summary-mark-positions ()
2587   "Compute where the summary marks are to go."
2588   (save-excursion
2589     (when (gnus-buffer-exists-p gnus-summary-buffer)
2590       (set-buffer gnus-summary-buffer))
2591     (let ((gnus-replied-mark 129)
2592           (gnus-score-below-mark 130)
2593           (gnus-score-over-mark 130)
2594           (gnus-download-mark 131)
2595           (spec gnus-summary-line-format-spec)
2596           gnus-visual pos)
2597       (save-excursion
2598         (gnus-set-work-buffer)
2599         (let ((gnus-summary-line-format-spec spec)
2600               (gnus-newsgroup-downloadable '((0 . t))))
2601           (gnus-summary-insert-line
2602            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2603            0 nil 128 t nil "" nil 1)
2604           (goto-char (point-min))
2605           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2606                                              (- (point) 2)))))
2607           (goto-char (point-min))
2608           (push (cons 'replied (and (search-forward "\201" nil t)
2609                                     (- (point) 2)))
2610                 pos)
2611           (goto-char (point-min))
2612           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2613                 pos)
2614           (goto-char (point-min))
2615           (push (cons 'download
2616                       (and (search-forward "\203" nil t) (- (point) 2)))
2617                 pos)))
2618       (setq gnus-summary-mark-positions pos))))
2619
2620 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2621   "Insert a dummy root in the summary buffer."
2622   (beginning-of-line)
2623   (gnus-add-text-properties
2624    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2625    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2626
2627 (defun gnus-summary-from-or-to-or-newsgroups (header)
2628   (let ((to (cdr (assq 'To (mail-header-extra header))))
2629         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2630         (default-mime-charset (with-current-buffer gnus-summary-buffer
2631                                 default-mime-charset)))
2632     (cond
2633      ((and to
2634            gnus-ignored-from-addresses
2635            (string-match gnus-ignored-from-addresses
2636                          (mail-header-from header)))
2637       (concat "-> "
2638               (or (car (funcall gnus-extract-address-components
2639                                 (funcall
2640                                  gnus-decode-encoded-word-function to)))
2641                   (funcall gnus-decode-encoded-word-function to))))
2642      ((and newsgroups
2643            gnus-ignored-from-addresses
2644            (string-match gnus-ignored-from-addresses
2645                          (mail-header-from header)))
2646       (concat "=> " newsgroups))
2647      (t
2648       (or (car (funcall gnus-extract-address-components
2649                         (mail-header-from header)))
2650           (mail-header-from header))))))
2651
2652 (defun gnus-summary-insert-line (gnus-tmp-header
2653                                  gnus-tmp-level gnus-tmp-current
2654                                  gnus-tmp-unread gnus-tmp-replied
2655                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2656                                  &optional gnus-tmp-dummy gnus-tmp-score
2657                                  gnus-tmp-process)
2658   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2659          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2660          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2661          (gnus-tmp-score-char
2662           (if (or (null gnus-summary-default-score)
2663                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2664                       gnus-summary-zcore-fuzz))
2665               ?  ;Whitespace
2666             (if (< gnus-tmp-score gnus-summary-default-score)
2667                 gnus-score-below-mark gnus-score-over-mark)))
2668          (gnus-tmp-replied
2669           (cond (gnus-tmp-process gnus-process-mark)
2670                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2671                  gnus-cached-mark)
2672                 (gnus-tmp-replied gnus-replied-mark)
2673                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2674                  gnus-saved-mark)
2675                 (t gnus-unread-mark)))
2676          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2677          (gnus-tmp-name
2678           (cond
2679            ((string-match "<[^>]+> *$" gnus-tmp-from)
2680             (let ((beg (match-beginning 0)))
2681               (or (and (string-match "^\".+\"" gnus-tmp-from)
2682                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2683                   (substring gnus-tmp-from 0 beg))))
2684            ((string-match "(.+)" gnus-tmp-from)
2685             (substring gnus-tmp-from
2686                        (1+ (match-beginning 0)) (1- (match-end 0))))
2687            (t gnus-tmp-from)))
2688          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2689          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2690          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2691          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2692          (buffer-read-only nil))
2693     (when (string= gnus-tmp-name "")
2694       (setq gnus-tmp-name gnus-tmp-from))
2695     (unless (numberp gnus-tmp-lines)
2696       (setq gnus-tmp-lines 0))
2697     (gnus-put-text-property-excluding-characters-with-faces
2698      (point)
2699      (progn (eval gnus-summary-line-format-spec) (point))
2700      'gnus-number gnus-tmp-number)
2701     (when (gnus-visual-p 'summary-highlight 'highlight)
2702       (forward-line -1)
2703       (gnus-run-hooks 'gnus-summary-update-hook)
2704       (forward-line 1))))
2705
2706 (defun gnus-summary-update-line (&optional dont-update)
2707   "Update summary line after change."
2708   (when (and gnus-summary-default-score
2709              (not gnus-summary-inhibit-highlight))
2710     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2711            (article (gnus-summary-article-number))
2712            (score (gnus-summary-article-score article)))
2713       (unless dont-update
2714         (if (and gnus-summary-mark-below
2715                  (< (gnus-summary-article-score)
2716                     gnus-summary-mark-below))
2717             ;; This article has a low score, so we mark it as read.
2718             (when (memq article gnus-newsgroup-unreads)
2719               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2720           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2721             ;; This article was previously marked as read on account
2722             ;; of a low score, but now it has risen, so we mark it as
2723             ;; unread.
2724             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2725         (gnus-summary-update-mark
2726          (if (or (null gnus-summary-default-score)
2727                  (<= (abs (- score gnus-summary-default-score))
2728                      gnus-summary-zcore-fuzz))
2729              ?  ;Whitespace
2730            (if (< score gnus-summary-default-score)
2731                gnus-score-below-mark gnus-score-over-mark))
2732          'score))
2733       ;; Do visual highlighting.
2734       (when (gnus-visual-p 'summary-highlight 'highlight)
2735         (gnus-run-hooks 'gnus-summary-update-hook)))))
2736
2737 (defvar gnus-tmp-new-adopts nil)
2738
2739 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2740   "Return the number of articles in THREAD.
2741 This may be 0 in some cases -- if none of the articles in
2742 the thread are to be displayed."
2743   (let* ((number
2744           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2745           (cond
2746            ((not (listp thread))
2747             1)
2748            ((and (consp thread) (cdr thread))
2749             (apply
2750              '+ 1 (mapcar
2751                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2752            ((null thread)
2753             1)
2754            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2755             1)
2756            (t 0))))
2757     (when (and level (zerop level) gnus-tmp-new-adopts)
2758       (incf number
2759             (apply '+ (mapcar
2760                        'gnus-summary-number-of-articles-in-thread
2761                        gnus-tmp-new-adopts))))
2762     (if char
2763         (if (> number 1) gnus-not-empty-thread-mark
2764           gnus-empty-thread-mark)
2765       number)))
2766
2767 (defun gnus-summary-set-local-parameters (group)
2768   "Go through the local params of GROUP and set all variable specs in that list."
2769   (let ((params (gnus-group-find-parameter group))
2770         elem)
2771     (while params
2772       (setq elem (car params)
2773             params (cdr params))
2774       (and (consp elem)                 ; Has to be a cons.
2775            (consp (cdr elem))           ; The cdr has to be a list.
2776            (symbolp (car elem))         ; Has to be a symbol in there.
2777            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2778            (ignore-errors               ; So we set it.
2779              (make-local-variable (car elem))
2780              (set (car elem) (eval (nth 1 elem))))))))
2781
2782 (defun gnus-summary-read-group (group &optional show-all no-article
2783                                       kill-buffer no-display backward
2784                                       select-articles)
2785   "Start reading news in newsgroup GROUP.
2786 If SHOW-ALL is non-nil, already read articles are also listed.
2787 If NO-ARTICLE is non-nil, no article is selected initially.
2788 If NO-DISPLAY, don't generate a summary buffer."
2789   (let (result)
2790     (while (and group
2791                 (null (setq result
2792                             (let ((gnus-auto-select-next nil))
2793                               (or (gnus-summary-read-group-1
2794                                    group show-all no-article
2795                                    kill-buffer no-display
2796                                    select-articles)
2797                                   (setq show-all nil
2798                                         select-articles nil)))))
2799                 (eq gnus-auto-select-next 'quietly))
2800       (set-buffer gnus-group-buffer)
2801       ;; The entry function called above goes to the next
2802       ;; group automatically, so we go two groups back
2803       ;; if we are searching for the previous group.
2804       (when backward
2805         (gnus-group-prev-unread-group 2))
2806       (if (not (equal group (gnus-group-group-name)))
2807           (setq group (gnus-group-group-name))
2808         (setq group nil)))
2809     result))
2810
2811 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2812   "Directly jump to the other GROUP from summary buffer.
2813 If SHOW-ALL is non-nil, already read articles are also listed."
2814   (interactive
2815    (if (eq gnus-summary-buffer (current-buffer))
2816        (list (completing-read
2817               "Group: " gnus-active-hashtb nil t
2818               (when (and gnus-newsgroup-name
2819                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2820                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2821               'gnus-group-history)
2822              current-prefix-arg)
2823      (error "%s must be invoked from a gnus summary buffer." this-command)))
2824   (unless (or (zerop (length group))
2825               (and gnus-newsgroup-name
2826                    (string-equal gnus-newsgroup-name group)))
2827     (gnus-summary-exit)
2828     (gnus-summary-read-group group show-all
2829                              gnus-dont-select-after-jump-to-other-group)))
2830
2831 (defun gnus-summary-read-group-1 (group show-all no-article
2832                                         kill-buffer no-display
2833                                         &optional select-articles)
2834   ;; Killed foreign groups can't be entered.
2835   (when (and (not (gnus-group-native-p group))
2836              (not (gnus-gethash group gnus-newsrc-hashtb)))
2837     (error "Dead non-native groups can't be entered"))
2838   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2839   (let* ((new-group (gnus-summary-setup-buffer group))
2840          (quit-config (gnus-group-quit-config group))
2841          (did-select (and new-group (gnus-select-newsgroup
2842                                      group show-all select-articles))))
2843     (cond
2844      ;; This summary buffer exists already, so we just select it.
2845      ((not new-group)
2846       (gnus-set-global-variables)
2847       (when kill-buffer
2848         (gnus-kill-or-deaden-summary kill-buffer))
2849       (gnus-configure-windows 'summary 'force)
2850       (gnus-set-mode-line 'summary)
2851       (gnus-summary-position-point)
2852       (message "")
2853       t)
2854      ;; We couldn't select this group.
2855      ((null did-select)
2856       (when (and (eq major-mode 'gnus-summary-mode)
2857                  (not (equal (current-buffer) kill-buffer)))
2858         (kill-buffer (current-buffer))
2859         (if (not quit-config)
2860             (progn
2861               ;; Update the info -- marks might need to be removed,
2862               ;; for instance.
2863               (gnus-summary-update-info)
2864               (set-buffer gnus-group-buffer)
2865               (gnus-group-jump-to-group group)
2866               (gnus-group-next-unread-group 1))
2867           (gnus-handle-ephemeral-exit quit-config)))
2868       (gnus-message 3 "Can't select group")
2869       nil)
2870      ;; The user did a `C-g' while prompting for number of articles,
2871      ;; so we exit this group.
2872      ((eq did-select 'quit)
2873       (and (eq major-mode 'gnus-summary-mode)
2874            (not (equal (current-buffer) kill-buffer))
2875            (kill-buffer (current-buffer)))
2876       (when kill-buffer
2877         (gnus-kill-or-deaden-summary kill-buffer))
2878       (if (not quit-config)
2879           (progn
2880             (set-buffer gnus-group-buffer)
2881             (gnus-group-jump-to-group group)
2882             (gnus-group-next-unread-group 1)
2883             (gnus-configure-windows 'group 'force))
2884         (gnus-handle-ephemeral-exit quit-config))
2885       ;; Finally signal the quit.
2886       (signal 'quit nil))
2887      ;; The group was successfully selected.
2888      (t
2889       (gnus-set-global-variables)
2890       ;; Save the active value in effect when the group was entered.
2891       (setq gnus-newsgroup-active
2892             (gnus-copy-sequence
2893              (gnus-active gnus-newsgroup-name)))
2894       ;; You can change the summary buffer in some way with this hook.
2895       (gnus-run-hooks 'gnus-select-group-hook)
2896       ;; Set any local variables in the group parameters.
2897       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2898       (gnus-update-format-specifications
2899        nil 'summary 'summary-mode 'summary-dummy)
2900       (gnus-update-summary-mark-positions)
2901       ;; Do score processing.
2902       (when gnus-use-scoring
2903         (gnus-possibly-score-headers))
2904       ;; Check whether to fill in the gaps in the threads.
2905       (when gnus-build-sparse-threads
2906         (gnus-build-sparse-threads))
2907       ;; Find the initial limit.
2908       (if gnus-show-threads
2909           (if show-all
2910               (let ((gnus-newsgroup-dormant nil))
2911                 (gnus-summary-initial-limit show-all))
2912             (gnus-summary-initial-limit show-all))
2913         ;; When untreaded, all articles are always shown.
2914         (setq gnus-newsgroup-limit
2915               (mapcar
2916                (lambda (header) (mail-header-number header))
2917                gnus-newsgroup-headers)))
2918       ;; Generate the summary buffer.
2919       (unless no-display
2920         (gnus-summary-prepare))
2921       (when gnus-use-trees
2922         (gnus-tree-open group)
2923         (setq gnus-summary-highlight-line-function
2924               'gnus-tree-highlight-article))
2925       ;; If the summary buffer is empty, but there are some low-scored
2926       ;; articles or some excluded dormants, we include these in the
2927       ;; buffer.
2928       (when (and (zerop (buffer-size))
2929                  (not no-display))
2930         (cond (gnus-newsgroup-dormant
2931                (gnus-summary-limit-include-dormant))
2932               ((and gnus-newsgroup-scored show-all)
2933                (gnus-summary-limit-include-expunged t))))
2934       ;; Function `gnus-apply-kill-file' must be called in this hook.
2935       (gnus-run-hooks 'gnus-apply-kill-hook)
2936       (if (and (zerop (buffer-size))
2937                (not no-display))
2938           (progn
2939             ;; This newsgroup is empty.
2940             (gnus-summary-catchup-and-exit nil t)
2941             (gnus-message 6 "No unread news")
2942             (when kill-buffer
2943               (gnus-kill-or-deaden-summary kill-buffer))
2944             ;; Return nil from this function.
2945             nil)
2946         ;; Hide conversation thread subtrees.  We cannot do this in
2947         ;; gnus-summary-prepare-hook since kill processing may not
2948         ;; work with hidden articles.
2949         (and gnus-show-threads
2950              gnus-thread-hide-subtree
2951              (gnus-summary-hide-all-threads))
2952         (when kill-buffer
2953           (gnus-kill-or-deaden-summary kill-buffer))
2954         ;; Show first unread article if requested.
2955         (if (and (not no-article)
2956                  (not no-display)
2957                  gnus-newsgroup-unreads
2958                  gnus-auto-select-first)
2959             (progn
2960               (gnus-configure-windows 'summary)
2961               (cond
2962                ((eq gnus-auto-select-first 'best)
2963                 (gnus-summary-best-unread-article))
2964                ((eq gnus-auto-select-first t)
2965                 (gnus-summary-first-unread-article))
2966                ((gnus-functionp gnus-auto-select-first)
2967                 (funcall gnus-auto-select-first))))
2968           ;; Don't select any articles, just move point to the first
2969           ;; article in the group.
2970           (goto-char (point-min))
2971           (gnus-summary-position-point)
2972           (gnus-configure-windows 'summary 'force)
2973           (gnus-set-mode-line 'summary))
2974         (when (get-buffer-window gnus-group-buffer t)
2975           ;; Gotta use windows, because recenter does weird stuff if
2976           ;; the current buffer ain't the displayed window.
2977           (let ((owin (selected-window)))
2978             (select-window (get-buffer-window gnus-group-buffer t))
2979             (when (gnus-group-goto-group group)
2980               (recenter))
2981             (select-window owin)))
2982         ;; Mark this buffer as "prepared".
2983         (setq gnus-newsgroup-prepared t)
2984         (gnus-run-hooks 'gnus-summary-prepared-hook)
2985         t)))))
2986
2987 (defun gnus-summary-prepare ()
2988   "Generate the summary buffer."
2989   (interactive)
2990   (let ((buffer-read-only nil))
2991     (erase-buffer)
2992     (setq gnus-newsgroup-data nil
2993           gnus-newsgroup-data-reverse nil)
2994     (gnus-run-hooks 'gnus-summary-generate-hook)
2995     ;; Generate the buffer, either with threads or without.
2996     (when gnus-newsgroup-headers
2997       (gnus-summary-prepare-threads
2998        (if gnus-show-threads
2999            (gnus-sort-gathered-threads
3000             (funcall gnus-summary-thread-gathering-function
3001                      (gnus-sort-threads
3002                       (gnus-cut-threads (gnus-make-threads)))))
3003          ;; Unthreaded display.
3004          (gnus-sort-articles gnus-newsgroup-headers))))
3005     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3006     ;; Call hooks for modifying summary buffer.
3007     (goto-char (point-min))
3008     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3009
3010 (defsubst gnus-general-simplify-subject (subject)
3011   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3012   (setq subject
3013         (cond
3014          ;; Truncate the subject.
3015          (gnus-simplify-subject-functions
3016           (gnus-map-function gnus-simplify-subject-functions subject))
3017          ((numberp gnus-summary-gather-subject-limit)
3018           (setq subject (gnus-simplify-subject-re subject))
3019           (if (> (length subject) gnus-summary-gather-subject-limit)
3020               (substring subject 0 gnus-summary-gather-subject-limit)
3021             subject))
3022          ;; Fuzzily simplify it.
3023          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3024           (gnus-simplify-subject-fuzzy subject))
3025          ;; Just remove the leading "Re:".
3026          (t
3027           (gnus-simplify-subject-re subject))))
3028
3029   (if (and gnus-summary-gather-exclude-subject
3030            (string-match gnus-summary-gather-exclude-subject subject))
3031       nil                               ; This article shouldn't be gathered
3032     subject))
3033
3034 (defun gnus-summary-simplify-subject-query ()
3035   "Query where the respool algorithm would put this article."
3036   (interactive)
3037   (gnus-summary-select-article)
3038   (message "%s"
3039            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3040
3041 (defun gnus-gather-threads-by-subject (threads)
3042   "Gather threads by looking at Subject headers."
3043   (if (not gnus-summary-make-false-root)
3044       threads
3045     (let ((hashtb (gnus-make-hashtable 1024))
3046           (prev threads)
3047           (result threads)
3048           subject hthread whole-subject)
3049       (while threads
3050         (setq subject (gnus-general-simplify-subject
3051                        (setq whole-subject (mail-header-subject
3052                                             (caar threads)))))
3053         (when subject
3054           (if (setq hthread (gnus-gethash subject hashtb))
3055               (progn
3056                 ;; We enter a dummy root into the thread, if we
3057                 ;; haven't done that already.
3058                 (unless (stringp (caar hthread))
3059                   (setcar hthread (list whole-subject (car hthread))))
3060                 ;; We add this new gathered thread to this gathered
3061                 ;; thread.
3062                 (setcdr (car hthread)
3063                         (nconc (cdar hthread) (list (car threads))))
3064                 ;; Remove it from the list of threads.
3065                 (setcdr prev (cdr threads))
3066                 (setq threads prev))
3067             ;; Enter this thread into the hash table.
3068             (gnus-sethash subject threads hashtb)))
3069         (setq prev threads)
3070         (setq threads (cdr threads)))
3071       result)))
3072
3073 (defun gnus-gather-threads-by-references (threads)
3074   "Gather threads by looking at References headers."
3075   (let ((idhashtb (gnus-make-hashtable 1024))
3076         (thhashtb (gnus-make-hashtable 1024))
3077         (prev threads)
3078         (result threads)
3079         ids references id gthread gid entered ref)
3080     (while threads
3081       (when (setq references (mail-header-references (caar threads)))
3082         (setq id (mail-header-id (caar threads))
3083               ids (gnus-split-references references)
3084               entered nil)
3085         (while (setq ref (pop ids))
3086           (setq ids (delete ref ids))
3087           (if (not (setq gid (gnus-gethash ref idhashtb)))
3088               (progn
3089                 (gnus-sethash ref id idhashtb)
3090                 (gnus-sethash id threads thhashtb))
3091             (setq gthread (gnus-gethash gid thhashtb))
3092             (unless entered
3093               ;; We enter a dummy root into the thread, if we
3094               ;; haven't done that already.
3095               (unless (stringp (caar gthread))
3096                 (setcar gthread (list (mail-header-subject (caar gthread))
3097                                       (car gthread))))
3098               ;; We add this new gathered thread to this gathered
3099               ;; thread.
3100               (setcdr (car gthread)
3101                       (nconc (cdar gthread) (list (car threads)))))
3102             ;; Add it into the thread hash table.
3103             (gnus-sethash id gthread thhashtb)
3104             (setq entered t)
3105             ;; Remove it from the list of threads.
3106             (setcdr prev (cdr threads))
3107             (setq threads prev))))
3108       (setq prev threads)
3109       (setq threads (cdr threads)))
3110     result))
3111
3112 (defun gnus-sort-gathered-threads (threads)
3113   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3114   (let ((result threads))
3115     (while threads
3116       (when (stringp (caar threads))
3117         (setcdr (car threads)
3118                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3119       (setq threads (cdr threads)))
3120     result))
3121
3122 (defun gnus-thread-loop-p (root thread)
3123   "Say whether ROOT is in THREAD."
3124   (let ((stack (list thread))
3125         (infloop 0)
3126         th)
3127     (while (setq thread (pop stack))
3128       (setq th (cdr thread))
3129       (while (and th
3130                   (not (eq (caar th) root)))
3131         (pop th))
3132       (if th
3133           ;; We have found a loop.
3134           (let (ref-dep)
3135             (setcdr thread (delq (car th) (cdr thread)))
3136             (if (boundp (setq ref-dep (intern "none"
3137                                               gnus-newsgroup-dependencies)))
3138                 (setcdr (symbol-value ref-dep)
3139                         (nconc (cdr (symbol-value ref-dep))
3140                                (list (car th))))
3141               (set ref-dep (list nil (car th))))
3142             (setq infloop 1
3143                   stack nil))
3144         ;; Push all the subthreads onto the stack.
3145         (push (cdr thread) stack)))
3146     infloop))
3147
3148 (defun gnus-make-threads ()
3149   "Go through the dependency hashtb and find the roots.  Return all threads."
3150   (let (threads)
3151     (while (catch 'infloop
3152              (mapatoms
3153               (lambda (refs)
3154                 ;; Deal with self-referencing References loops.
3155                 (when (and (car (symbol-value refs))
3156                            (not (zerop
3157                                  (apply
3158                                   '+
3159                                   (mapcar
3160                                    (lambda (thread)
3161                                      (gnus-thread-loop-p
3162                                       (car (symbol-value refs)) thread))
3163                                    (cdr (symbol-value refs)))))))
3164                   (setq threads nil)
3165                   (throw 'infloop t))
3166                 (unless (car (symbol-value refs))
3167                   ;; These threads do not refer back to any other articles,
3168                   ;; so they're roots.
3169                   (setq threads (append (cdr (symbol-value refs)) threads))))
3170               gnus-newsgroup-dependencies)))
3171     threads))
3172
3173 ;; Build the thread tree.
3174 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3175   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3176
3177 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3178 if it was already present.
3179
3180 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3181 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3182 Message-IDs will be renamed be renamed to a unique Message-ID before
3183 being entered.
3184
3185 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3186   (let* ((id (mail-header-id header))
3187          (id-dep (and id (intern id dependencies)))
3188          ref ref-dep ref-header)
3189     ;; Enter this `header' in the `dependencies' table.
3190     (cond
3191      ((not id-dep)
3192       (setq header nil))
3193      ;; The first two cases do the normal part: enter a new `header'
3194      ;; in the `dependencies' table.
3195      ((not (boundp id-dep))
3196       (set id-dep (list header)))
3197      ((null (car (symbol-value id-dep)))
3198       (setcar (symbol-value id-dep) header))
3199
3200      ;; From here the `header' was already present in the
3201      ;; `dependencies' table.
3202      (force-new
3203       ;; Overrides an existing entry;
3204       ;; just set the header part of the entry.
3205       (setcar (symbol-value id-dep) header))
3206
3207      ;; Renames the existing `header' to a unique Message-ID.
3208      ((not gnus-summary-ignore-duplicates)
3209       ;; An article with this Message-ID has already been seen.
3210       ;; We rename the Message-ID.
3211       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3212            (list header))
3213       (mail-header-set-id header id))
3214
3215      ;; The last case ignores an existing entry, except it adds any
3216      ;; additional Xrefs (in case the two articles came from different
3217      ;; servers.
3218      ;; Also sets `header' to `nil' meaning that the `dependencies'
3219      ;; table was *not* modified.
3220      (t
3221       (mail-header-set-xref
3222        (car (symbol-value id-dep))
3223        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3224                    "")
3225                (or (mail-header-xref header) "")))
3226       (setq header nil)))
3227
3228     (when header
3229       ;; First check if that we are not creating a References loop.
3230       (setq ref (gnus-parent-id (mail-header-references header)))
3231       (while (and ref
3232                   (setq ref-dep (intern-soft ref dependencies))
3233                   (boundp ref-dep)
3234                   (setq ref-header (car (symbol-value ref-dep))))
3235         (if (string= id ref)
3236             ;; Yuk!  This is a reference loop.  Make the article be a
3237             ;; root article.
3238             (progn
3239               (mail-header-set-references (car (symbol-value id-dep)) "none")
3240               (setq ref nil))
3241           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3242       (setq ref (gnus-parent-id (mail-header-references header)))
3243       (setq ref-dep (intern (or ref "none") dependencies))
3244       (if (boundp ref-dep)
3245           (setcdr (symbol-value ref-dep)
3246                   (nconc (cdr (symbol-value ref-dep))
3247                          (list (symbol-value id-dep))))
3248         (set ref-dep (list nil (symbol-value id-dep)))))
3249     header))
3250
3251 (defun gnus-build-sparse-threads ()
3252   (let ((headers gnus-newsgroup-headers)
3253         (mail-parse-charset gnus-newsgroup-charset)
3254         (gnus-summary-ignore-duplicates t)
3255         header references generation relations
3256         subject child end new-child date)
3257     ;; First we create an alist of generations/relations, where
3258     ;; generations is how much we trust the relation, and the relation
3259     ;; is parent/child.
3260     (gnus-message 7 "Making sparse threads...")
3261     (save-excursion
3262       (nnheader-set-temp-buffer " *gnus sparse threads*")
3263       (while (setq header (pop headers))
3264         (when (and (setq references (mail-header-references header))
3265                    (not (string= references "")))
3266           (insert references)
3267           (setq child (mail-header-id header)
3268                 subject (mail-header-subject header)
3269                 date (mail-header-date header)
3270                 generation 0)
3271           (while (search-backward ">" nil t)
3272             (setq end (1+ (point)))
3273             (when (search-backward "<" nil t)
3274               (setq new-child (buffer-substring (point) end))
3275               (push (list (incf generation)
3276                           child (setq child new-child)
3277                           subject date)
3278                     relations)))
3279           (when child
3280             (push (list (1+ generation) child nil subject) relations))
3281           (erase-buffer)))
3282       (kill-buffer (current-buffer)))
3283     ;; Sort over trustworthiness.
3284     (mapcar
3285      (lambda (relation)
3286        (when (gnus-dependencies-add-header
3287               (make-full-mail-header-from-decoded-header
3288                gnus-reffed-article-number
3289                (nth 3 relation) "" (or (nth 4 relation) "")
3290                (nth 1 relation)
3291                (or (nth 2 relation) "") 0 0 "")
3292               gnus-newsgroup-dependencies nil)
3293          (push gnus-reffed-article-number gnus-newsgroup-limit)
3294          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3295          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3296                gnus-newsgroup-reads)
3297          (decf gnus-reffed-article-number)))
3298      (sort relations 'car-less-than-car))
3299     (gnus-message 7 "Making sparse threads...done")))
3300
3301 (defun gnus-build-old-threads ()
3302   ;; Look at all the articles that refer back to old articles, and
3303   ;; fetch the headers for the articles that aren't there.  This will
3304   ;; build complete threads - if the roots haven't been expired by the
3305   ;; server, that is.
3306   (let ((mail-parse-charset gnus-newsgroup-charset)
3307         id heads)
3308     (mapatoms
3309      (lambda (refs)
3310        (when (not (car (symbol-value refs)))
3311          (setq heads (cdr (symbol-value refs)))
3312          (while heads
3313            (if (memq (mail-header-number (caar heads))
3314                      gnus-newsgroup-dormant)
3315                (setq heads (cdr heads))
3316              (setq id (symbol-name refs))
3317              (while (and (setq id (gnus-build-get-header id))
3318                          (not (car (gnus-id-to-thread id)))))
3319              (setq heads nil)))))
3320      gnus-newsgroup-dependencies)))
3321
3322 ;; This function has to be called with point after the article number
3323 ;; on the beginning of the line.
3324 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3325   (let ((eol (gnus-point-at-eol))
3326         (buffer (current-buffer))
3327         header)
3328
3329     ;; overview: [num subject from date id refs chars lines misc]
3330     (unwind-protect
3331         (progn
3332           (narrow-to-region (point) eol)
3333           (unless (eobp)
3334             (forward-char))
3335
3336           (setq header
3337                 (make-full-mail-header
3338                  number                         ; number
3339                  (nnheader-nov-field)           ; subject
3340                  (nnheader-nov-field)           ; from
3341                  (nnheader-nov-field)           ; date
3342                  (nnheader-nov-read-message-id) ; id
3343                  (nnheader-nov-field)           ; refs
3344                  (nnheader-nov-read-integer)    ; chars
3345                  (nnheader-nov-read-integer)    ; lines
3346                  (unless (eobp)
3347                    (if (looking-at "Xref: ")
3348                        (goto-char (match-end 0)))
3349                    (nnheader-nov-field))        ; Xref
3350                  (nnheader-nov-parse-extra))))  ; extra
3351
3352       (widen))
3353
3354     (when gnus-alter-header-function
3355       (funcall gnus-alter-header-function header))
3356     (gnus-dependencies-add-header header dependencies force-new)))
3357
3358 (defun gnus-build-get-header (id)
3359   "Look through the buffer of NOV lines and find the header to ID.
3360 Enter this line into the dependencies hash table, and return
3361 the id of the parent article (if any)."
3362   (let ((deps gnus-newsgroup-dependencies)
3363         found header)
3364     (prog1
3365         (save-excursion
3366           (set-buffer nntp-server-buffer)
3367           (let ((case-fold-search nil))
3368             (goto-char (point-min))
3369             (while (and (not found)
3370                         (search-forward id nil t))
3371               (beginning-of-line)
3372               (setq found (looking-at
3373                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3374                                    (regexp-quote id))))
3375               (or found (beginning-of-line 2)))
3376             (when found
3377               (beginning-of-line)
3378               (and
3379                (setq header (gnus-nov-parse-line
3380                              (read (current-buffer)) deps))
3381                (gnus-parent-id (mail-header-references header))))))
3382       (when header
3383         (let ((number (mail-header-number header)))
3384           (push number gnus-newsgroup-limit)
3385           (push header gnus-newsgroup-headers)
3386           (if (memq number gnus-newsgroup-unselected)
3387               (progn
3388                 (push number gnus-newsgroup-unreads)
3389                 (setq gnus-newsgroup-unselected
3390                       (delq number gnus-newsgroup-unselected)))
3391             (push number gnus-newsgroup-ancient)))))))
3392
3393 (defun gnus-build-all-threads ()
3394   "Read all the headers."
3395   (let ((gnus-summary-ignore-duplicates t)
3396         (mail-parse-charset gnus-newsgroup-charset)
3397         (dependencies gnus-newsgroup-dependencies)
3398         header article)
3399     (save-excursion
3400       (set-buffer nntp-server-buffer)
3401       (let ((case-fold-search nil))
3402         (goto-char (point-min))
3403         (while (not (eobp))
3404           (ignore-errors
3405             (setq article (read (current-buffer))
3406                   header (gnus-nov-parse-line article dependencies)))
3407           (when header
3408             (save-excursion
3409               (set-buffer gnus-summary-buffer)
3410               (push header gnus-newsgroup-headers)
3411               (if (memq (setq article (mail-header-number header))
3412                         gnus-newsgroup-unselected)
3413                   (progn
3414                     (push article gnus-newsgroup-unreads)
3415                     (setq gnus-newsgroup-unselected
3416                           (delq article gnus-newsgroup-unselected)))
3417                 (push article gnus-newsgroup-ancient)))
3418             (forward-line 1)))))))
3419
3420 (defun gnus-summary-update-article-line (article header)
3421   "Update the line for ARTICLE using HEADERS."
3422   (let* ((id (mail-header-id header))
3423          (thread (gnus-id-to-thread id)))
3424     (unless thread
3425       (error "Article in no thread"))
3426     ;; Update the thread.
3427     (setcar thread header)
3428     (gnus-summary-goto-subject article)
3429     (let* ((datal (gnus-data-find-list article))
3430            (data (car datal))
3431            (length (when (cdr datal)
3432                      (- (gnus-data-pos data)
3433                         (gnus-data-pos (cadr datal)))))
3434            (buffer-read-only nil)
3435            (level (gnus-summary-thread-level)))
3436       (gnus-delete-line)
3437       (gnus-summary-insert-line
3438        header level nil (gnus-article-mark article)
3439        (memq article gnus-newsgroup-replied)
3440        (memq article gnus-newsgroup-expirable)
3441        ;; Only insert the Subject string when it's different
3442        ;; from the previous Subject string.
3443        (if (and
3444             gnus-show-threads
3445             (gnus-subject-equal
3446              (condition-case ()
3447                  (mail-header-subject
3448                   (gnus-data-header
3449                    (cadr
3450                     (gnus-data-find-list
3451                      article
3452                      (gnus-data-list t)))))
3453                ;; Error on the side of excessive subjects.
3454                (error ""))
3455              (mail-header-subject header)))
3456            ""
3457          (mail-header-subject header))
3458        nil (cdr (assq article gnus-newsgroup-scored))
3459        (memq article gnus-newsgroup-processable))
3460       (when length
3461         (gnus-data-update-list
3462          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3463
3464 (defun gnus-summary-update-article (article &optional iheader)
3465   "Update ARTICLE in the summary buffer."
3466   (set-buffer gnus-summary-buffer)
3467   (let* ((header (gnus-summary-article-header article))
3468          (id (mail-header-id header))
3469          (data (gnus-data-find article))
3470          (thread (gnus-id-to-thread id))
3471          (references (mail-header-references header))
3472          (parent
3473           (gnus-id-to-thread
3474            (or (gnus-parent-id
3475                 (when (and references
3476                            (not (equal "" references)))
3477                   references))
3478                "none")))
3479          (buffer-read-only nil)
3480          (old (car thread)))
3481     (when thread
3482       (unless iheader
3483         (setcar thread nil)
3484         (when parent
3485           (delq thread parent)))
3486       (if (gnus-summary-insert-subject id header)
3487           ;; Set the (possibly) new article number in the data structure.
3488           (gnus-data-set-number data (gnus-id-to-article id))
3489         (setcar thread old)
3490         nil))))
3491
3492 (defun gnus-rebuild-thread (id &optional line)
3493   "Rebuild the thread containing ID.
3494 If LINE, insert the rebuilt thread starting on line LINE."
3495   (let ((buffer-read-only nil)
3496         old-pos current thread data)
3497     (if (not gnus-show-threads)
3498         (setq thread (list (car (gnus-id-to-thread id))))
3499       ;; Get the thread this article is part of.
3500       (setq thread (gnus-remove-thread id)))
3501     (setq old-pos (gnus-point-at-bol))
3502     (setq current (save-excursion
3503                     (and (zerop (forward-line -1))
3504                          (gnus-summary-article-number))))
3505     ;; If this is a gathered thread, we have to go some re-gathering.
3506     (when (stringp (car thread))
3507       (let ((subject (car thread))
3508             roots thr)
3509         (setq thread (cdr thread))
3510         (while thread
3511           (unless (memq (setq thr (gnus-id-to-thread
3512                                    (gnus-root-id
3513                                     (mail-header-id (caar thread)))))
3514                         roots)
3515             (push thr roots))
3516           (setq thread (cdr thread)))
3517         ;; We now have all (unique) roots.
3518         (if (= (length roots) 1)
3519             ;; All the loose roots are now one solid root.
3520             (setq thread (car roots))
3521           (setq thread (cons subject (gnus-sort-threads roots))))))
3522     (let (threads)
3523       ;; We then insert this thread into the summary buffer.
3524       (when line
3525         (goto-char (point-min))
3526         (forward-line (1- line)))
3527       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3528         (if gnus-show-threads
3529             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3530           (gnus-summary-prepare-unthreaded thread))
3531         (setq data (nreverse gnus-newsgroup-data))
3532         (setq threads gnus-newsgroup-threads))
3533       ;; We splice the new data into the data structure.
3534       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3535       ;;!!! then we want to insert at the beginning of the buffer.
3536       ;;!!! That happens to be true with Gnus now, but that may
3537       ;;!!! change in the future.  Perhaps.
3538       (gnus-data-enter-list
3539        (if line nil current) data (- (point) old-pos))
3540       (setq gnus-newsgroup-threads
3541             (nconc threads gnus-newsgroup-threads))
3542       (gnus-data-compute-positions))))
3543
3544 (defun gnus-number-to-header (number)
3545   "Return the header for article NUMBER."
3546   (let ((headers gnus-newsgroup-headers))
3547     (while (and headers
3548                 (not (= number (mail-header-number (car headers)))))
3549       (pop headers))
3550     (when headers
3551       (car headers))))
3552
3553 (defun gnus-parent-headers (in-headers &optional generation)
3554   "Return the headers of the GENERATIONeth parent of HEADERS."
3555   (unless generation
3556     (setq generation 1))
3557   (let ((parent t)
3558         (headers in-headers)
3559         references)
3560     (while (and parent
3561                 (not (zerop generation))
3562                 (setq references (mail-header-references headers)))
3563       (setq headers (if (and references
3564                              (setq parent (gnus-parent-id references)))
3565                         (car (gnus-id-to-thread parent))
3566                       nil))
3567       (decf generation))
3568     (and (not (eq headers in-headers))
3569          headers)))
3570
3571 (defun gnus-id-to-thread (id)
3572   "Return the (sub-)thread where ID appears."
3573   (gnus-gethash id gnus-newsgroup-dependencies))
3574
3575 (defun gnus-id-to-article (id)
3576   "Return the article number of ID."
3577   (let ((thread (gnus-id-to-thread id)))
3578     (when (and thread
3579                (car thread))
3580       (mail-header-number (car thread)))))
3581
3582 (defun gnus-id-to-header (id)
3583   "Return the article headers of ID."
3584   (car (gnus-id-to-thread id)))
3585
3586 (defun gnus-article-displayed-root-p (article)
3587   "Say whether ARTICLE is a root(ish) article."
3588   (let ((level (gnus-summary-thread-level article))
3589         (refs (mail-header-references  (gnus-summary-article-header article)))
3590         particle)
3591     (cond
3592      ((null level) nil)
3593      ((zerop level) t)
3594      ((null refs) t)
3595      ((null (gnus-parent-id refs)) t)
3596      ((and (= 1 level)
3597            (null (setq particle (gnus-id-to-article
3598                                  (gnus-parent-id refs))))
3599            (null (gnus-summary-thread-level particle)))))))
3600
3601 (defun gnus-root-id (id)
3602   "Return the id of the root of the thread where ID appears."
3603   (let (last-id prev)
3604     (while (and id (setq prev (car (gnus-id-to-thread id))))
3605       (setq last-id id
3606             id (gnus-parent-id (mail-header-references prev))))
3607     last-id))
3608
3609 (defun gnus-articles-in-thread (thread)
3610   "Return the list of articles in THREAD."
3611   (cons (mail-header-number (car thread))
3612         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3613
3614 (defun gnus-remove-thread (id &optional dont-remove)
3615   "Remove the thread that has ID in it."
3616   (let (headers thread last-id)
3617     ;; First go up in this thread until we find the root.
3618     (setq last-id (gnus-root-id id)
3619           headers (message-flatten-list (gnus-id-to-thread last-id)))
3620     ;; We have now found the real root of this thread.  It might have
3621     ;; been gathered into some loose thread, so we have to search
3622     ;; through the threads to find the thread we wanted.
3623     (let ((threads gnus-newsgroup-threads)
3624           sub)
3625       (while threads
3626         (setq sub (car threads))
3627         (if (stringp (car sub))
3628             ;; This is a gathered thread, so we look at the roots
3629             ;; below it to find whether this article is in this
3630             ;; gathered root.
3631             (progn
3632               (setq sub (cdr sub))
3633               (while sub
3634                 (when (member (caar sub) headers)
3635                   (setq thread (car threads)
3636                         threads nil
3637                         sub nil))
3638                 (setq sub (cdr sub))))
3639           ;; It's an ordinary thread, so we check it.
3640           (when (eq (car sub) (car headers))
3641             (setq thread sub
3642                   threads nil)))
3643         (setq threads (cdr threads)))
3644       ;; If this article is in no thread, then it's a root.
3645       (if thread
3646           (unless dont-remove
3647             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3648         (setq thread (gnus-id-to-thread last-id)))
3649       (when thread
3650         (prog1
3651             thread                      ; We return this thread.
3652           (unless dont-remove
3653             (if (stringp (car thread))
3654                 (progn
3655                   ;; If we use dummy roots, then we have to remove the
3656                   ;; dummy root as well.
3657                   (when (eq gnus-summary-make-false-root 'dummy)
3658                     ;; We go to the dummy root by going to
3659                     ;; the first sub-"thread", and then one line up.
3660                     (gnus-summary-goto-article
3661                      (mail-header-number (caadr thread)))
3662                     (forward-line -1)
3663                     (gnus-delete-line)
3664                     (gnus-data-compute-positions))
3665                   (setq thread (cdr thread))
3666                   (while thread
3667                     (gnus-remove-thread-1 (car thread))
3668                     (setq thread (cdr thread))))
3669               (gnus-remove-thread-1 thread))))))))
3670
3671 (defun gnus-remove-thread-1 (thread)
3672   "Remove the thread THREAD recursively."
3673   (let ((number (mail-header-number (pop thread)))
3674         d)
3675     (setq thread (reverse thread))
3676     (while thread
3677       (gnus-remove-thread-1 (pop thread)))
3678     (when (setq d (gnus-data-find number))
3679       (goto-char (gnus-data-pos d))
3680       (gnus-summary-show-thread)
3681       (gnus-data-remove
3682        number
3683        (- (gnus-point-at-bol)
3684           (prog1
3685               (1+ (gnus-point-at-eol))
3686             (gnus-delete-line)))))))
3687
3688 (defun gnus-sort-threads-1 (threads func)
3689   (sort (mapcar (lambda (thread)
3690                   (cons (car thread)
3691                         (and (cdr thread)
3692                              (gnus-sort-threads-1 (cdr thread) func))))
3693                 threads) func))
3694
3695 (defun gnus-sort-threads (threads)
3696   "Sort THREADS."
3697   (if (not gnus-thread-sort-functions)
3698       threads
3699     (gnus-message 8 "Sorting threads...")
3700     (prog1
3701         (gnus-sort-threads-1 
3702          threads 
3703          (gnus-make-sort-function gnus-thread-sort-functions))
3704       (gnus-message 8 "Sorting threads...done"))))
3705
3706 (defun gnus-sort-articles (articles)
3707   "Sort ARTICLES."
3708   (when gnus-article-sort-functions
3709     (gnus-message 7 "Sorting articles...")
3710     (prog1
3711         (setq gnus-newsgroup-headers
3712               (sort articles (gnus-make-sort-function
3713                               gnus-article-sort-functions)))
3714       (gnus-message 7 "Sorting articles...done"))))
3715
3716 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3717 (defmacro gnus-thread-header (thread)
3718   "Return header of first article in THREAD.
3719 Note that THREAD must never, ever be anything else than a variable -
3720 using some other form will lead to serious barfage."
3721   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3722   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3723   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3724         (vector thread) 2))
3725
3726 (defsubst gnus-article-sort-by-number (h1 h2)
3727   "Sort articles by article number."
3728   (< (mail-header-number h1)
3729      (mail-header-number h2)))
3730
3731 (defun gnus-thread-sort-by-number (h1 h2)
3732   "Sort threads by root article number."
3733   (gnus-article-sort-by-number
3734    (gnus-thread-header h1) (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-lines (h1 h2)
3737   "Sort articles by article Lines header."
3738   (< (mail-header-lines h1)
3739      (mail-header-lines h2)))
3740
3741 (defun gnus-thread-sort-by-lines (h1 h2)
3742   "Sort threads by root article Lines header."
3743   (gnus-article-sort-by-lines
3744    (gnus-thread-header h1) (gnus-thread-header h2)))
3745
3746 (defsubst gnus-article-sort-by-chars (h1 h2)
3747   "Sort articles by octet length."
3748   (< (mail-header-chars h1)
3749      (mail-header-chars h2)))
3750
3751 (defun gnus-thread-sort-by-chars (h1 h2)
3752   "Sort threads by root article octet length."
3753   (gnus-article-sort-by-chars
3754    (gnus-thread-header h1) (gnus-thread-header h2)))
3755
3756 (defsubst gnus-article-sort-by-author (h1 h2)
3757   "Sort articles by root author."
3758   (string-lessp
3759    (let ((addr (car (mime-entity-read-field h1 'From))))
3760      (or (std11-full-name-string addr)
3761          (std11-address-string addr)
3762          ""))
3763    (let ((addr (car (mime-entity-read-field h2 'From))))
3764      (or (std11-full-name-string addr)
3765          (std11-address-string addr)
3766          ""))
3767    ))
3768
3769 (defun gnus-thread-sort-by-author (h1 h2)
3770   "Sort threads by root author."
3771   (gnus-article-sort-by-author
3772    (gnus-thread-header h1)  (gnus-thread-header h2)))
3773
3774 (defsubst gnus-article-sort-by-subject (h1 h2)
3775   "Sort articles by root subject."
3776   (string-lessp
3777    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3778    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3779
3780 (defun gnus-thread-sort-by-subject (h1 h2)
3781   "Sort threads by root subject."
3782   (gnus-article-sort-by-subject
3783    (gnus-thread-header h1) (gnus-thread-header h2)))
3784
3785 (defsubst gnus-article-sort-by-date (h1 h2)
3786   "Sort articles by root article date."
3787   (time-less-p
3788    (gnus-date-get-time (mail-header-date h1))
3789    (gnus-date-get-time (mail-header-date h2))))
3790
3791 (defun gnus-thread-sort-by-date (h1 h2)
3792   "Sort threads by root article date."
3793   (gnus-article-sort-by-date
3794    (gnus-thread-header h1) (gnus-thread-header h2)))
3795
3796 (defsubst gnus-article-sort-by-score (h1 h2)
3797   "Sort articles by root article score.
3798 Unscored articles will be counted as having a score of zero."
3799   (> (or (cdr (assq (mail-header-number h1)
3800                     gnus-newsgroup-scored))
3801          gnus-summary-default-score 0)
3802      (or (cdr (assq (mail-header-number h2)
3803                     gnus-newsgroup-scored))
3804          gnus-summary-default-score 0)))
3805
3806 (defun gnus-thread-sort-by-score (h1 h2)
3807   "Sort threads by root article score."
3808   (gnus-article-sort-by-score
3809    (gnus-thread-header h1) (gnus-thread-header h2)))
3810
3811 (defun gnus-thread-sort-by-total-score (h1 h2)
3812   "Sort threads by the sum of all scores in the thread.
3813 Unscored articles will be counted as having a score of zero."
3814   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3815
3816 (defun gnus-thread-total-score (thread)
3817   ;; This function find the total score of THREAD.
3818   (cond ((null thread)
3819          0)
3820         ((consp thread)
3821          (if (stringp (car thread))
3822              (apply gnus-thread-score-function 0
3823                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3824            (gnus-thread-total-score-1 thread)))
3825         (t
3826          (gnus-thread-total-score-1 (list thread)))))
3827
3828 (defun gnus-thread-total-score-1 (root)
3829   ;; This function find the total score of the thread below ROOT.
3830   (setq root (car root))
3831   (apply gnus-thread-score-function
3832          (or (append
3833               (mapcar 'gnus-thread-total-score
3834                       (cdr (gnus-id-to-thread (mail-header-id root))))
3835               (when (> (mail-header-number root) 0)
3836                 (list (or (cdr (assq (mail-header-number root)
3837                                      gnus-newsgroup-scored))
3838                           gnus-summary-default-score 0))))
3839              (list gnus-summary-default-score)
3840              '(0))))
3841
3842 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3843 (defvar gnus-tmp-prev-subject nil)
3844 (defvar gnus-tmp-false-parent nil)
3845 (defvar gnus-tmp-root-expunged nil)
3846 (defvar gnus-tmp-dummy-line nil)
3847
3848 (defvar gnus-tmp-header)
3849 (defun gnus-extra-header (type &optional header)
3850   "Return the extra header of TYPE."
3851   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3852       ""))
3853
3854 (defun gnus-summary-prepare-threads (threads)
3855   "Prepare summary buffer from THREADS and indentation LEVEL.
3856 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3857 or a straight list of headers."
3858   (gnus-message 7 "Generating summary...")
3859
3860   (setq gnus-newsgroup-threads threads)
3861   (beginning-of-line)
3862
3863   (let ((gnus-tmp-level 0)
3864         (default-score (or gnus-summary-default-score 0))
3865         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3866         thread number subject stack state gnus-tmp-gathered beg-match
3867         new-roots gnus-tmp-new-adopts thread-end
3868         gnus-tmp-header gnus-tmp-unread
3869         gnus-tmp-replied gnus-tmp-subject-or-nil
3870         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3871         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3872         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3873
3874     (setq gnus-tmp-prev-subject nil)
3875
3876     (if (vectorp (car threads))
3877         ;; If this is a straight (sic) list of headers, then a
3878         ;; threaded summary display isn't required, so we just create
3879         ;; an unthreaded one.
3880         (gnus-summary-prepare-unthreaded threads)
3881
3882       ;; Do the threaded display.
3883
3884       (while (or threads stack gnus-tmp-new-adopts new-roots)
3885
3886         (if (and (= gnus-tmp-level 0)
3887                  (or (not stack)
3888                      (= (caar stack) 0))
3889                  (not gnus-tmp-false-parent)
3890                  (or gnus-tmp-new-adopts new-roots))
3891             (if gnus-tmp-new-adopts
3892                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3893                       thread (list (car gnus-tmp-new-adopts))
3894                       gnus-tmp-header (caar thread)
3895                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3896               (when new-roots
3897                 (setq thread (list (car new-roots))
3898                       gnus-tmp-header (caar thread)
3899                       new-roots (cdr new-roots))))
3900
3901           (if threads
3902               ;; If there are some threads, we do them before the
3903               ;; threads on the stack.
3904               (setq thread threads
3905                     gnus-tmp-header (caar thread))
3906             ;; There were no current threads, so we pop something off
3907             ;; the stack.
3908             (setq state (car stack)
3909                   gnus-tmp-level (car state)
3910                   thread (cdr state)
3911                   stack (cdr stack)
3912                   gnus-tmp-header (caar thread))))
3913
3914         (setq gnus-tmp-false-parent nil)
3915         (setq gnus-tmp-root-expunged nil)
3916         (setq thread-end nil)
3917
3918         (if (stringp gnus-tmp-header)
3919             ;; The header is a dummy root.
3920             (cond
3921              ((eq gnus-summary-make-false-root 'adopt)
3922               ;; We let the first article adopt the rest.
3923               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3924                                                (cddar thread)))
3925               (setq gnus-tmp-gathered
3926                     (nconc (mapcar
3927                             (lambda (h) (mail-header-number (car h)))
3928                             (cddar thread))
3929                            gnus-tmp-gathered))
3930               (setq thread (cons (list (caar thread)
3931                                        (cadar thread))
3932                                  (cdr thread)))
3933               (setq gnus-tmp-level -1
3934                     gnus-tmp-false-parent t))
3935              ((eq gnus-summary-make-false-root 'empty)
3936               ;; We print adopted articles with empty subject fields.
3937               (setq gnus-tmp-gathered
3938                     (nconc (mapcar
3939                             (lambda (h) (mail-header-number (car h)))
3940                             (cddar thread))
3941                            gnus-tmp-gathered))
3942               (setq gnus-tmp-level -1))
3943              ((eq gnus-summary-make-false-root 'dummy)
3944               ;; We remember that we probably want to output a dummy
3945               ;; root.
3946               (setq gnus-tmp-dummy-line gnus-tmp-header)
3947               (setq gnus-tmp-prev-subject gnus-tmp-header))
3948              (t
3949               ;; We do not make a root for the gathered
3950               ;; sub-threads at all.
3951               (setq gnus-tmp-level -1)))
3952
3953           (setq number (mail-header-number gnus-tmp-header)
3954                 subject (mail-header-subject gnus-tmp-header))
3955
3956           (cond
3957            ;; If the thread has changed subject, we might want to make
3958            ;; this subthread into a root.
3959            ((and (null gnus-thread-ignore-subject)
3960                  (not (zerop gnus-tmp-level))
3961                  gnus-tmp-prev-subject
3962                  (not (inline
3963                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3964             (setq new-roots (nconc new-roots (list (car thread)))
3965                   thread-end t
3966                   gnus-tmp-header nil))
3967            ;; If the article lies outside the current limit,
3968            ;; then we do not display it.
3969            ((not (memq number gnus-newsgroup-limit))
3970             (setq gnus-tmp-gathered
3971                   (nconc (mapcar
3972                           (lambda (h) (mail-header-number (car h)))
3973                           (cdar thread))
3974                          gnus-tmp-gathered))
3975             (setq gnus-tmp-new-adopts (if (cdar thread)
3976                                           (append gnus-tmp-new-adopts
3977                                                   (cdar thread))
3978                                         gnus-tmp-new-adopts)
3979                   thread-end t
3980                   gnus-tmp-header nil)
3981             (when (zerop gnus-tmp-level)
3982               (setq gnus-tmp-root-expunged t)))
3983            ;; Perhaps this article is to be marked as read?
3984            ((and gnus-summary-mark-below
3985                  (< (or (cdr (assq number gnus-newsgroup-scored))
3986                         default-score)
3987                     gnus-summary-mark-below)
3988                  ;; Don't touch sparse articles.
3989                  (not (gnus-summary-article-sparse-p number))
3990                  (not (gnus-summary-article-ancient-p number)))
3991             (setq gnus-newsgroup-unreads
3992                   (delq number gnus-newsgroup-unreads))
3993             (if gnus-newsgroup-auto-expire
3994                 (push number gnus-newsgroup-expirable)
3995               (push (cons number gnus-low-score-mark)
3996                     gnus-newsgroup-reads))))
3997
3998           (when gnus-tmp-header
3999             ;; We may have an old dummy line to output before this
4000             ;; article.
4001             (when (and gnus-tmp-dummy-line
4002                        (gnus-subject-equal
4003                         gnus-tmp-dummy-line
4004                         (mail-header-subject gnus-tmp-header)))
4005               (gnus-summary-insert-dummy-line
4006                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4007               (setq gnus-tmp-dummy-line nil))
4008
4009             ;; Compute the mark.
4010             (setq gnus-tmp-unread (gnus-article-mark number))
4011
4012             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4013                                   gnus-tmp-header gnus-tmp-level)
4014                   gnus-newsgroup-data)
4015
4016             ;; Actually insert the line.
4017             (setq
4018              gnus-tmp-subject-or-nil
4019              (cond
4020               ((and gnus-thread-ignore-subject
4021                     gnus-tmp-prev-subject
4022                     (not (inline (gnus-subject-equal
4023                                   gnus-tmp-prev-subject subject))))
4024                subject)
4025               ((zerop gnus-tmp-level)
4026                (if (and (eq gnus-summary-make-false-root 'empty)
4027                         (memq number gnus-tmp-gathered)
4028                         gnus-tmp-prev-subject
4029                         (inline (gnus-subject-equal
4030                                  gnus-tmp-prev-subject subject)))
4031                    gnus-summary-same-subject
4032                  subject))
4033               (t gnus-summary-same-subject)))
4034             (if (and (eq gnus-summary-make-false-root 'adopt)
4035                      (= gnus-tmp-level 1)
4036                      (memq number gnus-tmp-gathered))
4037                 (setq gnus-tmp-opening-bracket ?\<
4038                       gnus-tmp-closing-bracket ?\>)
4039               (setq gnus-tmp-opening-bracket ?\[
4040                     gnus-tmp-closing-bracket ?\]))
4041             (setq
4042              gnus-tmp-indentation
4043              (aref gnus-thread-indent-array gnus-tmp-level)
4044              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4045              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4046                                 gnus-summary-default-score 0)
4047              gnus-tmp-score-char
4048              (if (or (null gnus-summary-default-score)
4049                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4050                          gnus-summary-zcore-fuzz))
4051                  ?  ;Whitespace
4052                (if (< gnus-tmp-score gnus-summary-default-score)
4053                    gnus-score-below-mark gnus-score-over-mark))
4054              gnus-tmp-replied
4055              (cond ((memq number gnus-newsgroup-processable)
4056                     gnus-process-mark)
4057                    ((memq number gnus-newsgroup-cached)
4058                     gnus-cached-mark)
4059                    ((memq number gnus-newsgroup-replied)
4060                     gnus-replied-mark)
4061                    ((memq number gnus-newsgroup-saved)
4062                     gnus-saved-mark)
4063                    (t gnus-unread-mark))
4064              gnus-tmp-from (mail-header-from gnus-tmp-header)
4065              gnus-tmp-name
4066              (cond
4067               ((string-match "<[^>]+> *$" gnus-tmp-from)
4068                (setq beg-match (match-beginning 0))
4069                (or (and (string-match "^\".+\"" gnus-tmp-from)
4070                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4071                    (substring gnus-tmp-from 0 beg-match)))
4072               ((string-match "(.+)" gnus-tmp-from)
4073                (substring gnus-tmp-from
4074                           (1+ (match-beginning 0)) (1- (match-end 0))))
4075               (t gnus-tmp-from)))
4076             (when (string= gnus-tmp-name "")
4077               (setq gnus-tmp-name gnus-tmp-from))
4078             (unless (numberp gnus-tmp-lines)
4079               (setq gnus-tmp-lines 0))
4080             (gnus-put-text-property
4081              (point)
4082              (progn (eval gnus-summary-line-format-spec) (point))
4083              'gnus-number number)
4084             (when gnus-visual-p
4085               (forward-line -1)
4086               (gnus-run-hooks 'gnus-summary-update-hook)
4087               (forward-line 1))
4088
4089             (setq gnus-tmp-prev-subject subject)))
4090
4091         (when (nth 1 thread)
4092           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4093         (incf gnus-tmp-level)
4094         (setq threads (if thread-end nil (cdar thread)))
4095         (unless threads
4096           (setq gnus-tmp-level 0)))))
4097   (gnus-message 7 "Generating summary...done"))
4098
4099 (defun gnus-summary-prepare-unthreaded (headers)
4100   "Generate an unthreaded summary buffer based on HEADERS."
4101   (let (header number mark)
4102
4103     (beginning-of-line)
4104
4105     (while headers
4106       ;; We may have to root out some bad articles...
4107       (when (memq (setq number (mail-header-number
4108                                 (setq header (pop headers))))
4109                   gnus-newsgroup-limit)
4110         ;; Mark article as read when it has a low score.
4111         (when (and gnus-summary-mark-below
4112                    (< (or (cdr (assq number gnus-newsgroup-scored))
4113                           gnus-summary-default-score 0)
4114                       gnus-summary-mark-below)
4115                    (not (gnus-summary-article-ancient-p number)))
4116           (setq gnus-newsgroup-unreads
4117                 (delq number gnus-newsgroup-unreads))
4118           (if gnus-newsgroup-auto-expire
4119               (push number gnus-newsgroup-expirable)
4120             (push (cons number gnus-low-score-mark)
4121                   gnus-newsgroup-reads)))
4122
4123         (setq mark (gnus-article-mark number))
4124         (push (gnus-data-make number mark (1+ (point)) header 0)
4125               gnus-newsgroup-data)
4126         (gnus-summary-insert-line
4127          header 0 number
4128          mark (memq number gnus-newsgroup-replied)
4129          (memq number gnus-newsgroup-expirable)
4130          (mail-header-subject header) nil
4131          (cdr (assq number gnus-newsgroup-scored))
4132          (memq number gnus-newsgroup-processable))))))
4133
4134 (defun gnus-summary-remove-list-identifiers ()
4135   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4136   (let ((regexp (if (stringp gnus-list-identifiers)
4137                     gnus-list-identifiers
4138                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4139     (dolist (header gnus-newsgroup-headers)
4140       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4141                                   " *\\)\\)+\\(Re: +\\)?\\)")
4142                           (mail-header-subject header))
4143         (mail-header-set-subject
4144          header (concat (substring (mail-header-subject header)
4145                                    0 (match-beginning 1))
4146                         (or
4147                          (match-string 3 (mail-header-subject header))
4148                          (match-string 5 (mail-header-subject header)))
4149                         (substring (mail-header-subject header)
4150                                    (match-end 1))))))))
4151
4152 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4153   "Select newsgroup GROUP.
4154 If READ-ALL is non-nil, all articles in the group are selected.
4155 If SELECT-ARTICLES, only select those articles from GROUP."
4156   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4157          ;;!!! Dirty hack; should be removed.
4158          (gnus-summary-ignore-duplicates
4159           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4160               t
4161             gnus-summary-ignore-duplicates))
4162          (info (nth 2 entry))
4163          articles fetched-articles cached)
4164
4165     (unless (gnus-check-server
4166              (setq gnus-current-select-method
4167                    (gnus-find-method-for-group group)))
4168       (error "Couldn't open server"))
4169
4170     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4171         (gnus-activate-group group)     ; Or we can activate it...
4172         (progn                          ; Or we bug out.
4173           (when (equal major-mode 'gnus-summary-mode)
4174             (kill-buffer (current-buffer)))
4175           (error "Couldn't request group %s: %s"
4176                  group (gnus-status-message group))))
4177
4178     (unless (gnus-request-group group t)
4179       (when (equal major-mode 'gnus-summary-mode)
4180         (kill-buffer (current-buffer)))
4181       (error "Couldn't request group %s: %s"
4182              group (gnus-status-message group)))
4183
4184     (setq gnus-newsgroup-name group)
4185     (setq gnus-newsgroup-unselected nil)
4186     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4187     (gnus-summary-setup-default-charset)
4188
4189     ;; Adjust and set lists of article marks.
4190     (when info
4191       (gnus-adjust-marked-articles info))
4192
4193     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4194     (setq cached
4195           (if (gnus-virtual-group-p group)
4196               gnus-newsgroup-cached
4197             (gnus-cache-articles-in-group group)))
4198
4199     (setq gnus-newsgroup-unreads
4200           (gnus-set-difference
4201            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4202            gnus-newsgroup-dormant))
4203
4204     (setq gnus-newsgroup-processable nil)
4205
4206     (gnus-update-read-articles group gnus-newsgroup-unreads)
4207
4208     (if (setq articles select-articles)
4209         (setq gnus-newsgroup-unselected
4210               (gnus-sorted-intersection
4211                gnus-newsgroup-unreads
4212                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4213       (setq articles (gnus-articles-to-read group read-all)))
4214
4215     (cond
4216      ((null articles)
4217       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4218       'quit)
4219      ((eq articles 0) nil)
4220      (t
4221       ;; Init the dependencies hash table.
4222       (setq gnus-newsgroup-dependencies
4223             (gnus-make-hashtable (length articles)))
4224       (gnus-set-global-variables)
4225       ;; Retrieve the headers and read them in.
4226       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4227       (setq gnus-newsgroup-headers
4228             (gnus-retrieve-parsed-headers
4229              articles gnus-newsgroup-name
4230              ;; We might want to fetch old headers, but
4231              ;; not if there is only 1 article.
4232              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4233                            (not (numberp gnus-fetch-old-headers)))
4234                       (> (length articles) 1))
4235                   gnus-fetch-old-headers)))
4236       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4237
4238       ;; Suppress duplicates?
4239       (when gnus-suppress-duplicates
4240         (gnus-dup-suppress-articles))
4241
4242       ;; Set the initial limit.
4243       (setq gnus-newsgroup-limit (copy-sequence articles))
4244       ;; Remove canceled articles from the list of unread articles.
4245       (setq gnus-newsgroup-unreads
4246             (gnus-set-sorted-intersection
4247              gnus-newsgroup-unreads
4248              (setq fetched-articles
4249                    (mapcar (lambda (headers) (mail-header-number headers))
4250                            gnus-newsgroup-headers))))
4251       ;; Removed marked articles that do not exist.
4252       (gnus-update-missing-marks
4253        (gnus-sorted-complement fetched-articles articles))
4254
4255       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4256       (when cached
4257         (setq gnus-newsgroup-cached cached))
4258
4259       ;; We might want to build some more threads first.
4260       (when (and gnus-fetch-old-headers
4261                  (eq gnus-headers-retrieved-by 'nov))
4262         (if (eq gnus-fetch-old-headers 'invisible)
4263             (gnus-build-all-threads)
4264           (gnus-build-old-threads)))
4265       ;; Let the Gnus agent mark articles as read.
4266       (when gnus-agent
4267         (gnus-agent-get-undownloaded-list))
4268       ;; Remove list identifiers from subject
4269       (when gnus-list-identifiers
4270         (gnus-summary-remove-list-identifiers))
4271       ;; Check whether auto-expire is to be done in this group.
4272       (setq gnus-newsgroup-auto-expire
4273             (gnus-group-auto-expirable-p group))
4274       ;; Set up the article buffer now, if necessary.
4275       (unless gnus-single-article-buffer
4276         (gnus-article-setup-buffer))
4277       ;; First and last article in this newsgroup.
4278       (when gnus-newsgroup-headers
4279         (setq gnus-newsgroup-begin
4280               (mail-header-number (car gnus-newsgroup-headers))
4281               gnus-newsgroup-end
4282               (mail-header-number
4283                (gnus-last-element gnus-newsgroup-headers))))
4284       ;; GROUP is successfully selected.
4285       (or gnus-newsgroup-headers t)))))
4286
4287 (defun gnus-articles-to-read (group &optional read-all)
4288   "Find out what articles the user wants to read."
4289   (let* ((articles
4290           ;; Select all articles if `read-all' is non-nil, or if there
4291           ;; are no unread articles.
4292           (if (or read-all
4293                   (and (zerop (length gnus-newsgroup-marked))
4294                        (zerop (length gnus-newsgroup-unreads)))
4295                   (eq (gnus-group-find-parameter group 'display)
4296                       'all))
4297               (or
4298                (gnus-uncompress-range (gnus-active group))
4299                (gnus-cache-articles-in-group group))
4300             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4301                           (copy-sequence gnus-newsgroup-unreads))
4302                   '<)))
4303          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4304          (scored (length scored-list))
4305          (number (length articles))
4306          (marked (+ (length gnus-newsgroup-marked)
4307                     (length gnus-newsgroup-dormant)))
4308          (select
4309           (cond
4310            ((numberp read-all)
4311             read-all)
4312            (t
4313             (condition-case ()
4314                 (cond
4315                  ((and (or (<= scored marked) (= scored number))
4316                        (natnump gnus-large-newsgroup)
4317                        (> number gnus-large-newsgroup))
4318                   (let* ((cursor-in-echo-area nil)
4319                          (input (read-from-minibuffer
4320                                  (format
4321                                   "How many articles from %s (max %d): "
4322                                   (gnus-limit-string gnus-newsgroup-name 35)
4323                                   number)
4324                                  (cons (number-to-string gnus-large-newsgroup)
4325                                        0))))
4326                     (if (string-match "^[ \t]*$" input)
4327                         number
4328                       input)))
4329                  ((and (> scored marked) (< scored number)
4330                        (> (- scored number) 20))
4331                   (let ((input
4332                          (read-string
4333                           (format "%s %s (%d scored, %d total): "
4334                                   "How many articles from"
4335                                   group scored number))))
4336                     (if (string-match "^[ \t]*$" input)
4337                         number input)))
4338                  (t number))
4339               (quit
4340                (message "Quit getting the articles to read")
4341                nil))))))
4342     (setq select (if (stringp select) (string-to-number select) select))
4343     (if (or (null select) (zerop select))
4344         select
4345       (if (and (not (zerop scored)) (<= (abs select) scored))
4346           (progn
4347             (setq articles (sort scored-list '<))
4348             (setq number (length articles)))
4349         (setq articles (copy-sequence articles)))
4350
4351       (when (< (abs select) number)
4352         (if (< select 0)
4353             ;; Select the N oldest articles.
4354             (setcdr (nthcdr (1- (abs select)) articles) nil)
4355           ;; Select the N most recent articles.
4356           (setq articles (nthcdr (- number select) articles))))
4357       (setq gnus-newsgroup-unselected
4358             (gnus-sorted-intersection
4359              gnus-newsgroup-unreads
4360              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4361       (when gnus-alter-articles-to-read-function
4362         (setq gnus-newsgroup-unreads
4363               (sort
4364                (funcall gnus-alter-articles-to-read-function
4365                         gnus-newsgroup-name gnus-newsgroup-unreads)
4366                '<)))
4367       articles)))
4368
4369 (defun gnus-killed-articles (killed articles)
4370   (let (out)
4371     (while articles
4372       (when (inline (gnus-member-of-range (car articles) killed))
4373         (push (car articles) out))
4374       (setq articles (cdr articles)))
4375     out))
4376
4377 (defun gnus-uncompress-marks (marks)
4378   "Uncompress the mark ranges in MARKS."
4379   (let ((uncompressed '(score bookmark))
4380         out)
4381     (while marks
4382       (if (memq (caar marks) uncompressed)
4383           (push (car marks) out)
4384         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4385       (setq marks (cdr marks)))
4386     out))
4387
4388 (defun gnus-adjust-marked-articles (info)
4389   "Set all article lists and remove all marks that are no longer valid."
4390   (let* ((marked-lists (gnus-info-marks info))
4391          (active (gnus-active (gnus-info-group info)))
4392          (min (car active))
4393          (max (cdr active))
4394          (types gnus-article-mark-lists)
4395          (uncompressed '(score bookmark killed))
4396          marks var articles article mark)
4397
4398     (while marked-lists
4399       (setq marks (pop marked-lists))
4400       (set (setq var (intern (format "gnus-newsgroup-%s"
4401                                      (car (rassq (setq mark (car marks))
4402                                                  types)))))
4403            (if (memq (car marks) uncompressed) (cdr marks)
4404              (gnus-uncompress-range (cdr marks))))
4405
4406       (setq articles (symbol-value var))
4407
4408       ;; All articles have to be subsets of the active articles.
4409       (cond
4410        ;; Adjust "simple" lists.
4411        ((memq mark '(tick dormant expire reply save))
4412         (while articles
4413           (when (or (< (setq article (pop articles)) min) (> article max))
4414             (set var (delq article (symbol-value var))))))
4415        ;; Adjust assocs.
4416        ((memq mark uncompressed)
4417         (when (not (listp (cdr (symbol-value var))))
4418           (set var (list (symbol-value var))))
4419         (when (not (listp (cdr articles)))
4420           (setq articles (list articles)))
4421         (while articles
4422           (when (or (not (consp (setq article (pop articles))))
4423                     (< (car article) min)
4424                     (> (car article) max))
4425             (set var (delq article (symbol-value var))))))))))
4426
4427 (defun gnus-update-missing-marks (missing)
4428   "Go through the list of MISSING articles and remove them from the mark lists."
4429   (when missing
4430     (let ((types gnus-article-mark-lists)
4431           var m)
4432       ;; Go through all types.
4433       (while types
4434         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4435         (when (symbol-value var)
4436           ;; This list has articles.  So we delete all missing articles
4437           ;; from it.
4438           (setq m missing)
4439           (while m
4440             (set var (delq (pop m) (symbol-value var)))))))))
4441
4442 (defun gnus-update-marks ()
4443   "Enter the various lists of marked articles into the newsgroup info list."
4444   (let ((types gnus-article-mark-lists)
4445         (info (gnus-get-info gnus-newsgroup-name))
4446         (uncompressed '(score bookmark killed))
4447         type list newmarked symbol delta-marks)
4448     (when info
4449       ;; Add all marks lists to the list of marks lists.
4450       (while (setq type (pop types))
4451         (setq list (symbol-value
4452                     (setq symbol
4453                           (intern (format "gnus-newsgroup-%s"
4454                                           (car type))))))
4455
4456         (when list
4457           ;; Get rid of the entries of the articles that have the
4458           ;; default score.
4459           (when (and (eq (cdr type) 'score)
4460                      gnus-save-score
4461                      list)
4462             (let* ((arts list)
4463                    (prev (cons nil list))
4464                    (all prev))
4465               (while arts
4466                 (if (or (not (consp (car arts)))
4467                         (= (cdar arts) gnus-summary-default-score))
4468                     (setcdr prev (cdr arts))
4469                   (setq prev arts))
4470                 (setq arts (cdr arts)))
4471               (setq list (cdr all)))))
4472
4473         (unless (memq (cdr type) uncompressed)
4474           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4475
4476         (when (gnus-check-backend-function
4477                'request-set-mark gnus-newsgroup-name)
4478           ;; propagate flags to server, with the following exceptions:
4479           ;; uncompressed:s are not proper flags (they are cons cells)
4480           ;; cache is a internal gnus flag
4481           ;; download are local to one gnus installation (well)
4482           ;; unsend are for nndraft groups only
4483           ;; xxx: generality of this?  this suits nnimap anyway
4484           (unless (memq (cdr type) (append '(cache download unsend)
4485                                            uncompressed))
4486             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4487                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4488                    (add (gnus-remove-from-range
4489                          (gnus-copy-sequence list) old)))
4490               (when add
4491                 (push (list add 'add (list (cdr type))) delta-marks))
4492               (when del
4493                 (push (list del 'del (list (cdr type))) delta-marks)))))
4494
4495         (when list
4496           (push (cons (cdr type) list) newmarked)))
4497
4498       (when delta-marks
4499         (unless (gnus-check-group gnus-newsgroup-name)
4500           (error "Can't open server for %s" gnus-newsgroup-name))
4501         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4502
4503       ;; Enter these new marks into the info of the group.
4504       (if (nthcdr 3 info)
4505           (setcar (nthcdr 3 info) newmarked)
4506         ;; Add the marks lists to the end of the info.
4507         (when newmarked
4508           (setcdr (nthcdr 2 info) (list newmarked))))
4509
4510       ;; Cut off the end of the info if there's nothing else there.
4511       (let ((i 5))
4512         (while (and (> i 2)
4513                     (not (nth i info)))
4514           (when (nthcdr (decf i) info)
4515             (setcdr (nthcdr i info) nil)))))))
4516
4517 (defun gnus-set-mode-line (where)
4518   "Set the mode line of the article or summary buffers.
4519 If WHERE is `summary', the summary mode line format will be used."
4520   ;; Is this mode line one we keep updated?
4521   (when (and (memq where gnus-updated-mode-lines)
4522              (symbol-value
4523               (intern (format "gnus-%s-mode-line-format-spec" where))))
4524     (let (mode-string)
4525       (save-excursion
4526         ;; We evaluate this in the summary buffer since these
4527         ;; variables are buffer-local to that buffer.
4528         (set-buffer gnus-summary-buffer)
4529         ;; We bind all these variables that are used in the `eval' form
4530         ;; below.
4531         (let* ((mformat (symbol-value
4532                          (intern
4533                           (format "gnus-%s-mode-line-format-spec" where))))
4534                (gnus-tmp-group-name (gnus-group-name-decode
4535                                      gnus-newsgroup-name
4536                                      (gnus-group-name-charset
4537                                       nil
4538                                       gnus-newsgroup-name)))
4539                (gnus-tmp-article-number (or gnus-current-article 0))
4540                (gnus-tmp-unread gnus-newsgroup-unreads)
4541                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4542                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4543                (gnus-tmp-unread-and-unselected
4544                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4545                             (zerop gnus-tmp-unselected))
4546                        "")
4547                       ((zerop gnus-tmp-unselected)
4548                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4549                       (t (format "{%d(+%d) more}"
4550                                  gnus-tmp-unread-and-unticked
4551                                  gnus-tmp-unselected))))
4552                (gnus-tmp-subject
4553                 (if (and gnus-current-headers
4554                          (vectorp gnus-current-headers))
4555                     (gnus-mode-string-quote
4556                      (mail-header-subject gnus-current-headers))
4557                   ""))
4558                bufname-length max-len
4559                gnus-tmp-header);; passed as argument to any user-format-funcs
4560           (setq mode-string (eval mformat))
4561           (setq bufname-length (if (string-match "%b" mode-string)
4562                                    (- (length
4563                                        (buffer-name
4564                                         (if (eq where 'summary)
4565                                             nil
4566                                           (get-buffer gnus-article-buffer))))
4567                                       2)
4568                                  0))
4569           (setq max-len (max 4 (if gnus-mode-non-string-length
4570                                    (- (window-width)
4571                                       gnus-mode-non-string-length
4572                                       bufname-length)
4573                                  (length mode-string))))
4574           ;; We might have to chop a bit of the string off...
4575           (when (> (length mode-string) max-len)
4576             (setq mode-string
4577                   (concat (gnus-truncate-string mode-string (- max-len 3))
4578                           "...")))
4579           ;; Pad the mode string a bit.
4580           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4581       ;; Update the mode line.
4582       (setq mode-line-buffer-identification
4583             (gnus-mode-line-buffer-identification (list mode-string)))
4584       (set-buffer-modified-p t))))
4585
4586 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4587   "Go through the HEADERS list and add all Xrefs to a hash table.
4588 The resulting hash table is returned, or nil if no Xrefs were found."
4589   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4590          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4591          (xref-hashtb (gnus-make-hashtable))
4592          start group entry number xrefs header)
4593     (while headers
4594       (setq header (pop headers))
4595       (when (and (setq xrefs (mail-header-xref header))
4596                  (not (memq (setq number (mail-header-number header))
4597                             unreads)))
4598         (setq start 0)
4599         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4600           (setq start (match-end 0))
4601           (setq group (if prefix
4602                           (concat prefix (substring xrefs (match-beginning 1)
4603                                                     (match-end 1)))
4604                         (substring xrefs (match-beginning 1) (match-end 1))))
4605           (setq number
4606                 (string-to-int (substring xrefs (match-beginning 2)
4607                                           (match-end 2))))
4608           (if (setq entry (gnus-gethash group xref-hashtb))
4609               (setcdr entry (cons number (cdr entry)))
4610             (gnus-sethash group (cons number nil) xref-hashtb)))))
4611     (and start xref-hashtb)))
4612
4613 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4614   "Look through all the headers and mark the Xrefs as read."
4615   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4616         name entry info xref-hashtb idlist method nth4)
4617     (save-excursion
4618       (set-buffer gnus-group-buffer)
4619       (when (setq xref-hashtb
4620                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4621         (mapatoms
4622          (lambda (group)
4623            (unless (string= from-newsgroup (setq name (symbol-name group)))
4624              (setq idlist (symbol-value group))
4625              ;; Dead groups are not updated.
4626              (and (prog1
4627                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4628                             info (nth 2 entry))
4629                     (when (stringp (setq nth4 (gnus-info-method info)))
4630                       (setq nth4 (gnus-server-to-method nth4))))
4631                   ;; Only do the xrefs if the group has the same
4632                   ;; select method as the group we have just read.
4633                   (or (gnus-methods-equal-p
4634                        nth4 (gnus-find-method-for-group from-newsgroup))
4635                       virtual
4636                       (equal nth4 (setq method (gnus-find-method-for-group
4637                                                 from-newsgroup)))
4638                       (and (equal (car nth4) (car method))
4639                            (equal (nth 1 nth4) (nth 1 method))))
4640                   gnus-use-cross-reference
4641                   (or (not (eq gnus-use-cross-reference t))
4642                       virtual
4643                       ;; Only do cross-references on subscribed
4644                       ;; groups, if that is what is wanted.
4645                       (<= (gnus-info-level info) gnus-level-subscribed))
4646                   (gnus-group-make-articles-read name idlist))))
4647          xref-hashtb)))))
4648
4649 (defun gnus-compute-read-articles (group articles)
4650   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4651          (info (nth 2 entry))
4652          (active (gnus-active group))
4653          ninfo)
4654     (when entry
4655       ;; First peel off all invalid article numbers.
4656       (when active
4657         (let ((ids articles)
4658               id first)
4659           (while (setq id (pop ids))
4660             (when (and first (> id (cdr active)))
4661               ;; We'll end up in this situation in one particular
4662               ;; obscure situation.  If you re-scan a group and get
4663               ;; a new article that is cross-posted to a different
4664               ;; group that has not been re-scanned, you might get
4665               ;; crossposted article that has a higher number than
4666               ;; Gnus believes possible.  So we re-activate this
4667               ;; group as well.  This might mean doing the
4668               ;; crossposting thingy will *increase* the number
4669               ;; of articles in some groups.  Tsk, tsk.
4670               (setq active (or (gnus-activate-group group) active)))
4671             (when (or (> id (cdr active))
4672                       (< id (car active)))
4673               (setq articles (delq id articles))))))
4674       ;; If the read list is nil, we init it.
4675       (if (and active
4676                (null (gnus-info-read info))
4677                (> (car active) 1))
4678           (setq ninfo (cons 1 (1- (car active))))
4679         (setq ninfo (gnus-info-read info)))
4680       ;; Then we add the read articles to the range.
4681       (gnus-add-to-range
4682        ninfo (setq articles (sort articles '<))))))
4683
4684 (defun gnus-group-make-articles-read (group articles)
4685   "Update the info of GROUP to say that ARTICLES are read."
4686   (let* ((num 0)
4687          (entry (gnus-gethash group gnus-newsrc-hashtb))
4688          (info (nth 2 entry))
4689          (active (gnus-active group))
4690          range)
4691     (when entry
4692       (setq range (gnus-compute-read-articles group articles))
4693       (save-excursion
4694         (set-buffer gnus-group-buffer)
4695         (gnus-undo-register
4696           `(progn
4697              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4698              (gnus-info-set-read ',info ',(gnus-info-read info))
4699              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4700              (gnus-group-update-group ,group t))))
4701       ;; Add the read articles to the range.
4702       (gnus-info-set-read info range)
4703       ;; Then we have to re-compute how many unread
4704       ;; articles there are in this group.
4705       (when active
4706         (cond
4707          ((not range)
4708           (setq num (- (1+ (cdr active)) (car active))))
4709          ((not (listp (cdr range)))
4710           (setq num (- (cdr active) (- (1+ (cdr range))
4711                                        (car range)))))
4712          (t
4713           (while range
4714             (if (numberp (car range))
4715                 (setq num (1+ num))
4716               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4717             (setq range (cdr range)))
4718           (setq num (- (cdr active) num))))
4719         ;; Update the number of unread articles.
4720         (setcar entry num)
4721         ;; Update the group buffer.
4722         (gnus-group-update-group group t)))))
4723
4724 (defvar gnus-newsgroup-none-id 0)
4725
4726 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4727   (let ((cur nntp-server-buffer)
4728         (dependencies
4729          (or dependencies
4730              (save-excursion (set-buffer gnus-summary-buffer)
4731                              gnus-newsgroup-dependencies)))
4732         headers id end ref
4733         (mail-parse-charset gnus-newsgroup-charset)
4734         (mail-parse-ignored-charsets
4735          (save-excursion (condition-case nil
4736                              (set-buffer gnus-summary-buffer)
4737                            (error))
4738                          gnus-newsgroup-ignored-charsets)))
4739     (save-excursion
4740       (set-buffer nntp-server-buffer)
4741       ;; Translate all TAB characters into SPACE characters.
4742       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4743       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4744       (gnus-run-hooks 'gnus-parse-headers-hook)
4745       (let ((case-fold-search t)
4746             in-reply-to header p lines chars ctype)
4747         (goto-char (point-min))
4748         ;; Search to the beginning of the next header.  Error messages
4749         ;; do not begin with 2 or 3.
4750         (while (re-search-forward "^[23][0-9]+ " nil t)
4751           (setq id nil
4752                 ref nil)
4753           ;; This implementation of this function, with nine
4754           ;; search-forwards instead of the one re-search-forward and
4755           ;; a case (which basically was the old function) is actually
4756           ;; about twice as fast, even though it looks messier.  You
4757           ;; can't have everything, I guess.  Speed and elegance
4758           ;; doesn't always go hand in hand.
4759           (setq
4760            header
4761            (make-full-mail-header
4762             ;; Number.
4763             (prog1
4764                 (read cur)
4765               (end-of-line)
4766               (setq p (point))
4767               (narrow-to-region (point)
4768                                 (or (and (search-forward "\n.\n" nil t)
4769                                          (- (point) 2))
4770                                     (point))))
4771             ;; Subject.
4772             (progn
4773               (goto-char p)
4774               (if (search-forward "\nsubject: " nil t)
4775                   (nnheader-header-value)
4776                 "(none)"))
4777             ;; From.
4778             (progn
4779               (goto-char p)
4780               (if (or (search-forward "\nfrom: " nil t)
4781                       (search-forward "\nfrom:" nil t))
4782                   (nnheader-header-value)
4783                 "(nobody)"))
4784             ;; Date.
4785             (progn
4786               (goto-char p)
4787               (if (search-forward "\ndate: " nil t)
4788                   (nnheader-header-value)
4789                 ""))
4790             ;; Message-ID.
4791             (progn
4792               (goto-char p)
4793               (setq id (if (re-search-forward
4794                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4795                            ;; We do it this way to make sure the Message-ID
4796                            ;; is (somewhat) syntactically valid.
4797                            (buffer-substring (match-beginning 1)
4798                                              (match-end 1))
4799                          ;; If there was no message-id, we just fake one
4800                          ;; to make subsequent routines simpler.
4801                          (nnheader-generate-fake-message-id))))
4802             ;; References.
4803             (progn
4804               (goto-char p)
4805               (if (search-forward "\nreferences: " nil t)
4806                   (progn
4807                     (setq end (point))
4808                     (prog1
4809                         (nnheader-header-value)
4810                       (setq ref
4811                             (buffer-substring
4812                              (progn
4813                                ;; (end-of-line)
4814                                (search-backward ">" end t)
4815                                (1+ (point)))
4816                              (progn
4817                                (search-backward "<" end t)
4818                                (point))))))
4819                 ;; Get the references from the in-reply-to header if there
4820                 ;; were no references and the in-reply-to header looks
4821                 ;; promising.
4822                 (if (and (search-forward "\nin-reply-to: " nil t)
4823                          (setq in-reply-to (nnheader-header-value))
4824                          (string-match "<[^>]+>" in-reply-to))
4825                     (let (ref2)
4826                       (setq ref (substring in-reply-to (match-beginning 0)
4827                                            (match-end 0)))
4828                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4829                         (setq ref2 (substring in-reply-to (match-beginning 0)
4830                                               (match-end 0)))
4831                         (when (> (length ref2) (length ref))
4832                           (setq ref ref2)))
4833                       ref)
4834                   (setq ref nil))))
4835             ;; Chars.
4836             (progn
4837               (goto-char p)
4838               (if (search-forward "\nchars: " nil t)
4839                   (if (numberp (setq chars (ignore-errors (read cur))))
4840                       chars 0)
4841                 0))
4842             ;; Lines.
4843             (progn
4844               (goto-char p)
4845               (if (search-forward "\nlines: " nil t)
4846                   (if (numberp (setq lines (ignore-errors (read cur))))
4847                       lines 0)
4848                 0))
4849             ;; Xref.
4850             (progn
4851               (goto-char p)
4852               (and (search-forward "\nxref: " nil t)
4853                    (nnheader-header-value)))
4854             ;; Extra.
4855             (when gnus-extra-headers
4856               (let ((extra gnus-extra-headers)
4857                     out)
4858                 (while extra
4859                   (goto-char p)
4860                   (when (search-forward
4861                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4862                     (push (cons (car extra) (nnheader-header-value)) out))
4863                   (pop extra))
4864                 out))))
4865           (goto-char p)
4866           (if (and (search-forward "\ncontent-type: " nil t)
4867                    (setq ctype (nnheader-header-value)))
4868               (mime-entity-set-content-type-internal
4869                header (mime-parse-Content-Type ctype)))
4870           (when (equal id ref)
4871             (setq ref nil))
4872
4873           (when gnus-alter-header-function
4874             (funcall gnus-alter-header-function header)
4875             (setq id (mail-header-id header)
4876                   ref (gnus-parent-id (mail-header-references header))))
4877
4878           (when (setq header
4879                       (gnus-dependencies-add-header
4880                        header dependencies force-new))
4881             (push header headers))
4882           (goto-char (point-max))
4883           (widen))
4884         (nreverse headers)))))
4885
4886 ;; Goes through the xover lines and returns a list of vectors
4887 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4888                                                   force-new dependencies
4889                                                   group also-fetch-heads)
4890   "Parse the news overview data in the server buffer.
4891 Return a list of headers that match SEQUENCE (see
4892 `nntp-retrieve-headers')."
4893   ;; Get the Xref when the users reads the articles since most/some
4894   ;; NNTP servers do not include Xrefs when using XOVER.
4895   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4896   (let ((mail-parse-charset gnus-newsgroup-charset)
4897         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4898         (cur nntp-server-buffer)
4899         (dependencies (or dependencies gnus-newsgroup-dependencies))
4900         number headers header)
4901     (save-excursion
4902       (set-buffer nntp-server-buffer)
4903       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4904       ;; Allow the user to mangle the headers before parsing them.
4905       (gnus-run-hooks 'gnus-parse-headers-hook)
4906       (goto-char (point-min))
4907       (while (not (eobp))
4908         (condition-case ()
4909             (while (and sequence (not (eobp)))
4910               (setq number (read cur))
4911               (while (and sequence
4912                           (< (car sequence) number))
4913                 (setq sequence (cdr sequence)))
4914               (and sequence
4915                    (eq number (car sequence))
4916                    (progn
4917                      (setq sequence (cdr sequence))
4918                      (setq header (inline
4919                                     (gnus-nov-parse-line
4920                                      number dependencies force-new))))
4921                    (push header headers))
4922               (forward-line 1))
4923           (error
4924            (gnus-error 4 "Strange nov line (%d)"
4925                        (count-lines (point-min) (point)))))
4926         (forward-line 1))
4927       ;; A common bug in inn is that if you have posted an article and
4928       ;; then retrieves the active file, it will answer correctly --
4929       ;; the new article is included.  However, a NOV entry for the
4930       ;; article may not have been generated yet, so this may fail.
4931       ;; We work around this problem by retrieving the last few
4932       ;; headers using HEAD.
4933       (if (or (not also-fetch-heads)
4934               (not sequence))
4935           ;; We (probably) got all the headers.
4936           (nreverse headers)
4937         (let ((gnus-nov-is-evil t))
4938           (nconc
4939            (nreverse headers)
4940            (gnus-retrieve-parsed-headers sequence group)
4941            ))))))
4942
4943 (defun gnus-article-get-xrefs ()
4944   "Fill in the Xref value in `gnus-current-headers', if necessary.
4945 This is meant to be called in `gnus-article-internal-prepare-hook'."
4946   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4947                                  gnus-current-headers)))
4948     (or (not gnus-use-cross-reference)
4949         (not headers)
4950         (and (mail-header-xref headers)
4951              (not (string= (mail-header-xref headers) "")))
4952         (let ((case-fold-search t)
4953               xref)
4954           (save-restriction
4955             (nnheader-narrow-to-headers)
4956             (goto-char (point-min))
4957             (when (or (and (not (eobp))
4958                            (eq (downcase (char-after)) ?x)
4959                            (looking-at "Xref:"))
4960                       (search-forward "\nXref:" nil t))
4961               (goto-char (1+ (match-end 0)))
4962               (setq xref (buffer-substring (point)
4963                                            (progn (end-of-line) (point))))
4964               (mail-header-set-xref headers xref)))))))
4965
4966 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4967   "Find article ID and insert the summary line for that article.
4968 OLD-HEADER can either be a header or a line number to insert
4969 the subject line on."
4970   (let* ((line (and (numberp old-header) old-header))
4971          (old-header (and (vectorp old-header) old-header))
4972          (header (cond ((and old-header use-old-header)
4973                         old-header)
4974                        ((and (numberp id)
4975                              (gnus-number-to-header id))
4976                         (gnus-number-to-header id))
4977                        (t
4978                         (gnus-read-header id))))
4979          (number (and (numberp id) id))
4980          d)
4981     (when header
4982       ;; Rebuild the thread that this article is part of and go to the
4983       ;; article we have fetched.
4984       (when (and (not gnus-show-threads)
4985                  old-header)
4986         (when (and number
4987                    (setq d (gnus-data-find (mail-header-number old-header))))
4988           (goto-char (gnus-data-pos d))
4989           (gnus-data-remove
4990            number
4991            (- (gnus-point-at-bol)
4992               (prog1
4993                   (1+ (gnus-point-at-eol))
4994                 (gnus-delete-line))))))
4995       (when old-header
4996         (mail-header-set-number header (mail-header-number old-header)))
4997       (setq gnus-newsgroup-sparse
4998             (delq (setq number (mail-header-number header))
4999                   gnus-newsgroup-sparse))
5000       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5001       (push number gnus-newsgroup-limit)
5002       (gnus-rebuild-thread (mail-header-id header) line)
5003       (gnus-summary-goto-subject number nil t))
5004     (when (and (numberp number)
5005                (> number 0))
5006       ;; We have to update the boundaries even if we can't fetch the
5007       ;; article if ID is a number -- so that the next `P' or `N'
5008       ;; command will fetch the previous (or next) article even
5009       ;; if the one we tried to fetch this time has been canceled.
5010       (when (> number gnus-newsgroup-end)
5011         (setq gnus-newsgroup-end number))
5012       (when (< number gnus-newsgroup-begin)
5013         (setq gnus-newsgroup-begin number))
5014       (setq gnus-newsgroup-unselected
5015             (delq number gnus-newsgroup-unselected)))
5016     ;; Report back a success?
5017     (and header (mail-header-number header))))
5018
5019 ;;; Process/prefix in the summary buffer
5020
5021 (defun gnus-summary-work-articles (n)
5022   "Return a list of articles to be worked upon.
5023 The prefix argument, the list of process marked articles, and the
5024 current article will be taken into consideration."
5025   (save-excursion
5026     (set-buffer gnus-summary-buffer)
5027     (cond
5028      (n
5029       ;; A numerical prefix has been given.
5030       (setq n (prefix-numeric-value n))
5031       (let ((backward (< n 0))
5032             (n (abs (prefix-numeric-value n)))
5033             articles article)
5034         (save-excursion
5035           (while
5036               (and (> n 0)
5037                    (push (setq article (gnus-summary-article-number))
5038                          articles)
5039                    (if backward
5040                        (gnus-summary-find-prev nil article)
5041                      (gnus-summary-find-next nil article)))
5042             (decf n)))
5043         (nreverse articles)))
5044      ((and (gnus-region-active-p) (mark))
5045       (message "region active")
5046       ;; Work on the region between point and mark.
5047       (let ((max (max (point) (mark)))
5048             articles article)
5049         (save-excursion
5050           (goto-char (min (point) (mark)))
5051           (while
5052               (and
5053                (push (setq article (gnus-summary-article-number)) articles)
5054                (gnus-summary-find-next nil article)
5055                (< (point) max)))
5056           (nreverse articles))))
5057      (gnus-newsgroup-processable
5058       ;; There are process-marked articles present.
5059       ;; Save current state.
5060       (gnus-summary-save-process-mark)
5061       ;; Return the list.
5062       (reverse gnus-newsgroup-processable))
5063      (t
5064       ;; Just return the current article.
5065       (list (gnus-summary-article-number))))))
5066
5067 (defmacro gnus-summary-iterate (arg &rest forms)
5068   "Iterate over the process/prefixed articles and do FORMS.
5069 ARG is the interactive prefix given to the command.  FORMS will be
5070 executed with point over the summary line of the articles."
5071   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5072     `(let ((,articles (gnus-summary-work-articles ,arg)))
5073        (while ,articles
5074          (gnus-summary-goto-subject (car ,articles))
5075          ,@forms
5076          (pop ,articles)))))
5077
5078 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5079 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5080
5081 (defun gnus-summary-save-process-mark ()
5082   "Push the current set of process marked articles on the stack."
5083   (interactive)
5084   (push (copy-sequence gnus-newsgroup-processable)
5085         gnus-newsgroup-process-stack))
5086
5087 (defun gnus-summary-kill-process-mark ()
5088   "Push the current set of process marked articles on the stack and unmark."
5089   (interactive)
5090   (gnus-summary-save-process-mark)
5091   (gnus-summary-unmark-all-processable))
5092
5093 (defun gnus-summary-yank-process-mark ()
5094   "Pop the last process mark state off the stack and restore it."
5095   (interactive)
5096   (unless gnus-newsgroup-process-stack
5097     (error "Empty mark stack"))
5098   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5099
5100 (defun gnus-summary-process-mark-set (set)
5101   "Make SET into the current process marked articles."
5102   (gnus-summary-unmark-all-processable)
5103   (while set
5104     (gnus-summary-set-process-mark (pop set))))
5105
5106 ;;; Searching and stuff
5107
5108 (defun gnus-summary-search-group (&optional backward use-level)
5109   "Search for next unread newsgroup.
5110 If optional argument BACKWARD is non-nil, search backward instead."
5111   (save-excursion
5112     (set-buffer gnus-group-buffer)
5113     (when (gnus-group-search-forward
5114            backward nil (if use-level (gnus-group-group-level) nil))
5115       (gnus-group-group-name))))
5116
5117 (defun gnus-summary-best-group (&optional exclude-group)
5118   "Find the name of the best unread group.
5119 If EXCLUDE-GROUP, do not go to this group."
5120   (save-excursion
5121     (set-buffer gnus-group-buffer)
5122     (save-excursion
5123       (gnus-group-best-unread-group exclude-group))))
5124
5125 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5126   (if backward (gnus-summary-find-prev)
5127     (let* ((dummy (gnus-summary-article-intangible-p))
5128            (article (or article (gnus-summary-article-number)))
5129            (arts (gnus-data-find-list article))
5130            result)
5131       (when (and (not dummy)
5132                  (or (not gnus-summary-check-current)
5133                      (not unread)
5134                      (not (gnus-data-unread-p (car arts)))))
5135         (setq arts (cdr arts)))
5136       (when (setq result
5137                   (if unread
5138                       (progn
5139                         (while arts
5140                           (when (or (and undownloaded
5141                                          (eq gnus-undownloaded-mark
5142                                              (gnus-data-mark (car arts))))
5143                                     (gnus-data-unread-p (car arts)))
5144                             (setq result (car arts)
5145                                   arts nil))
5146                           (setq arts (cdr arts)))
5147                         result)
5148                     (car arts)))
5149         (goto-char (gnus-data-pos result))
5150         (gnus-data-number result)))))
5151
5152 (defun gnus-summary-find-prev (&optional unread article)
5153   (let* ((eobp (eobp))
5154          (article (or article (gnus-summary-article-number)))
5155          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5156          result)
5157     (when (and (not eobp)
5158                (or (not gnus-summary-check-current)
5159                    (not unread)
5160                    (not (gnus-data-unread-p (car arts)))))
5161       (setq arts (cdr arts)))
5162     (when (setq result
5163                 (if unread
5164                     (progn
5165                       (while arts
5166                         (when (gnus-data-unread-p (car arts))
5167                           (setq result (car arts)
5168                                 arts nil))
5169                         (setq arts (cdr arts)))
5170                       result)
5171                   (car arts)))
5172       (goto-char (gnus-data-pos result))
5173       (gnus-data-number result))))
5174
5175 (defun gnus-summary-find-subject (subject &optional unread backward article)
5176   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5177          (article (or article (gnus-summary-article-number)))
5178          (articles (gnus-data-list backward))
5179          (arts (gnus-data-find-list article articles))
5180          result)
5181     (when (or (not gnus-summary-check-current)
5182               (not unread)
5183               (not (gnus-data-unread-p (car arts))))
5184       (setq arts (cdr arts)))
5185     (while arts
5186       (and (or (not unread)
5187                (gnus-data-unread-p (car arts)))
5188            (vectorp (gnus-data-header (car arts)))
5189            (gnus-subject-equal
5190             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5191            (setq result (car arts)
5192                  arts nil))
5193       (setq arts (cdr arts)))
5194     (and result
5195          (goto-char (gnus-data-pos result))
5196          (gnus-data-number result))))
5197
5198 (defun gnus-summary-search-forward (&optional unread subject backward)
5199   "Search forward for an article.
5200 If UNREAD, look for unread articles.  If SUBJECT, look for
5201 articles with that subject.  If BACKWARD, search backward instead."
5202   (cond (subject (gnus-summary-find-subject subject unread backward))
5203         (backward (gnus-summary-find-prev unread))
5204         (t (gnus-summary-find-next unread))))
5205
5206 (defun gnus-recenter (&optional n)
5207   "Center point in window and redisplay frame.
5208 Also do horizontal recentering."
5209   (interactive "P")
5210   (when (and gnus-auto-center-summary
5211              (not (eq gnus-auto-center-summary 'vertical)))
5212     (gnus-horizontal-recenter))
5213   (recenter n))
5214
5215 (defun gnus-summary-recenter ()
5216   "Center point in the summary window.
5217 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5218 displayed, no centering will be performed."
5219   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5220   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5221   (interactive)
5222   (let* ((top (cond ((< (window-height) 4) 0)
5223                     ((< (window-height) 7) 1)
5224                     (t (if (numberp gnus-auto-center-summary)
5225                            gnus-auto-center-summary
5226                          2))))
5227          (height (1- (window-height)))
5228          (bottom (save-excursion (goto-char (point-max))
5229                                  (forward-line (- height))
5230                                  (point)))
5231          (window (get-buffer-window (current-buffer))))
5232     ;; The user has to want it.
5233     (when gnus-auto-center-summary
5234       (when (get-buffer-window gnus-article-buffer)
5235         ;; Only do recentering when the article buffer is displayed,
5236         ;; Set the window start to either `bottom', which is the biggest
5237         ;; possible valid number, or the second line from the top,
5238         ;; whichever is the least.
5239         (set-window-start
5240          window (min bottom (save-excursion
5241                               (forward-line (- top)) (point)))
5242          t))
5243       ;; Do horizontal recentering while we're at it.
5244       (when (and (get-buffer-window (current-buffer) t)
5245                  (not (eq gnus-auto-center-summary 'vertical)))
5246         (let ((selected (selected-window)))
5247           (select-window (get-buffer-window (current-buffer) t))
5248           (gnus-summary-position-point)
5249           (gnus-horizontal-recenter)
5250           (select-window selected))))))
5251
5252 (defun gnus-summary-jump-to-group (newsgroup)
5253   "Move point to NEWSGROUP in group mode buffer."
5254   ;; Keep update point of group mode buffer if visible.
5255   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5256       (save-window-excursion
5257         ;; Take care of tree window mode.
5258         (when (get-buffer-window gnus-group-buffer)
5259           (pop-to-buffer gnus-group-buffer))
5260         (gnus-group-jump-to-group newsgroup))
5261     (save-excursion
5262       ;; Take care of tree window mode.
5263       (if (get-buffer-window gnus-group-buffer)
5264           (pop-to-buffer gnus-group-buffer)
5265         (set-buffer gnus-group-buffer))
5266       (gnus-group-jump-to-group newsgroup))))
5267
5268 ;; This function returns a list of article numbers based on the
5269 ;; difference between the ranges of read articles in this group and
5270 ;; the range of active articles.
5271 (defun gnus-list-of-unread-articles (group)
5272   (let* ((read (gnus-info-read (gnus-get-info group)))
5273          (active (or (gnus-active group) (gnus-activate-group group)))
5274          (last (cdr active))
5275          first nlast unread)
5276     ;; If none are read, then all are unread.
5277     (if (not read)
5278         (setq first (car active))
5279       ;; If the range of read articles is a single range, then the
5280       ;; first unread article is the article after the last read
5281       ;; article.  Sounds logical, doesn't it?
5282       (if (and (not (listp (cdr read)))
5283                (or (< (car read) (car active))
5284                    (progn (setq read (list read))
5285                           nil)))
5286           (setq first (max (car active) (1+ (cdr read))))
5287         ;; `read' is a list of ranges.
5288         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5289                                   (caar read)))
5290                   1)
5291           (setq first (car active)))
5292         (while read
5293           (when first
5294             (while (< first nlast)
5295               (push first unread)
5296               (setq first (1+ first))))
5297           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5298           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5299           (setq read (cdr read)))))
5300     ;; And add the last unread articles.
5301     (while (<= first last)
5302       (push first unread)
5303       (setq first (1+ first)))
5304     ;; Return the list of unread articles.
5305     (delq 0 (nreverse unread))))
5306
5307 (defun gnus-list-of-read-articles (group)
5308   "Return a list of unread, unticked and non-dormant articles."
5309   (let* ((info (gnus-get-info group))
5310          (marked (gnus-info-marks info))
5311          (active (gnus-active group)))
5312     (and info active
5313          (gnus-set-difference
5314           (gnus-sorted-complement
5315            (gnus-uncompress-range active)
5316            (gnus-list-of-unread-articles group))
5317           (append
5318            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5319            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5320
5321 ;; Various summary commands
5322
5323 (defun gnus-summary-select-article-buffer ()
5324   "Reconfigure windows to show article buffer."
5325   (interactive)
5326   (if (not (gnus-buffer-live-p gnus-article-buffer))
5327       (error "There is no article buffer for this summary buffer")
5328     (gnus-configure-windows 'article)
5329     (select-window (get-buffer-window gnus-article-buffer))))
5330
5331 (defun gnus-summary-universal-argument (arg)
5332   "Perform any operation on all articles that are process/prefixed."
5333   (interactive "P")
5334   (let ((articles (gnus-summary-work-articles arg))
5335         func article)
5336     (if (eq
5337          (setq
5338           func
5339           (key-binding
5340            (read-key-sequence
5341             (substitute-command-keys
5342              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5343          'undefined)
5344         (gnus-error 1 "Undefined key")
5345       (save-excursion
5346         (while articles
5347           (gnus-summary-goto-subject (setq article (pop articles)))
5348           (let (gnus-newsgroup-processable)
5349             (command-execute func))
5350           (gnus-summary-remove-process-mark article)))))
5351   (gnus-summary-position-point))
5352
5353 (defun gnus-summary-toggle-truncation (&optional arg)
5354   "Toggle truncation of summary lines.
5355 With arg, turn line truncation on iff arg is positive."
5356   (interactive "P")
5357   (setq truncate-lines
5358         (if (null arg) (not truncate-lines)
5359           (> (prefix-numeric-value arg) 0)))
5360   (redraw-display))
5361
5362 (defun gnus-summary-reselect-current-group (&optional all rescan)
5363   "Exit and then reselect the current newsgroup.
5364 The prefix argument ALL means to select all articles."
5365   (interactive "P")
5366   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5367     (error "Ephemeral groups can't be reselected"))
5368   (let ((current-subject (gnus-summary-article-number))
5369         (group gnus-newsgroup-name))
5370     (setq gnus-newsgroup-begin nil)
5371     (gnus-summary-exit)
5372     ;; We have to adjust the point of group mode buffer because
5373     ;; point was moved to the next unread newsgroup by exiting.
5374     (gnus-summary-jump-to-group group)
5375     (when rescan
5376       (save-excursion
5377         (save-window-excursion
5378           ;; Don't show group contents.
5379           (set-window-start (selected-window) (point-max))
5380           (gnus-group-get-new-news-this-group 1))))
5381     (gnus-group-read-group all t)
5382     (gnus-summary-goto-subject current-subject nil t)))
5383
5384 (defun gnus-summary-rescan-group (&optional all)
5385   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5386   (interactive "P")
5387   (gnus-summary-reselect-current-group all t))
5388
5389 (defun gnus-summary-update-info (&optional non-destructive)
5390   (save-excursion
5391     (let ((group gnus-newsgroup-name))
5392       (when group
5393         (when gnus-newsgroup-kill-headers
5394           (setq gnus-newsgroup-killed
5395                 (gnus-compress-sequence
5396                  (nconc
5397                   (gnus-set-sorted-intersection
5398                    (gnus-uncompress-range gnus-newsgroup-killed)
5399                    (setq gnus-newsgroup-unselected
5400                          (sort gnus-newsgroup-unselected '<)))
5401                   (setq gnus-newsgroup-unreads
5402                         (sort gnus-newsgroup-unreads '<)))
5403                  t)))
5404         (unless (listp (cdr gnus-newsgroup-killed))
5405           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5406         (let ((headers gnus-newsgroup-headers))
5407           ;; Set the new ranges of read articles.
5408           (save-excursion
5409             (set-buffer gnus-group-buffer)
5410             (gnus-undo-force-boundary))
5411           (gnus-update-read-articles
5412            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5413           ;; Set the current article marks.
5414           (let ((gnus-newsgroup-scored
5415                  (if (and (not gnus-save-score)
5416                           (not non-destructive))
5417                      nil
5418                    gnus-newsgroup-scored)))
5419             (save-excursion
5420               (gnus-update-marks)))
5421           ;; Do the cross-ref thing.
5422           (when gnus-use-cross-reference
5423             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5424           ;; Do not switch windows but change the buffer to work.
5425           (set-buffer gnus-group-buffer)
5426           (unless (gnus-ephemeral-group-p group)
5427             (gnus-group-update-group group)))))))
5428
5429 (defun gnus-summary-save-newsrc (&optional force)
5430   "Save the current number of read/marked articles in the dribble buffer.
5431 The dribble buffer will then be saved.
5432 If FORCE (the prefix), also save the .newsrc file(s)."
5433   (interactive "P")
5434   (gnus-summary-update-info t)
5435   (if force
5436       (gnus-save-newsrc-file)
5437     (gnus-dribble-save)))
5438
5439 (defun gnus-summary-exit (&optional temporary)
5440   "Exit reading current newsgroup, and then return to group selection mode.
5441 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5442   (interactive)
5443   (gnus-set-global-variables)
5444   (gnus-kill-save-kill-buffer)
5445   (gnus-async-halt-prefetch)
5446   (let* ((group gnus-newsgroup-name)
5447          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5448          (mode major-mode)
5449          (group-point nil)
5450          (buf (current-buffer)))
5451     (unless quit-config
5452       ;; Do adaptive scoring, and possibly save score files.
5453       (when gnus-newsgroup-adaptive
5454         (gnus-score-adaptive))
5455       (when gnus-use-scoring
5456         (gnus-score-save)))
5457     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5458     ;; If we have several article buffers, we kill them at exit.
5459     (unless gnus-single-article-buffer
5460       (gnus-kill-buffer gnus-original-article-buffer)
5461       (setq gnus-article-current nil))
5462     (when gnus-use-cache
5463       (gnus-cache-possibly-remove-articles)
5464       (gnus-cache-save-buffers))
5465     (gnus-async-prefetch-remove-group group)
5466     (when gnus-suppress-duplicates
5467       (gnus-dup-enter-articles))
5468     (when gnus-use-trees
5469       (gnus-tree-close group))
5470     (when gnus-use-cache
5471       (gnus-cache-write-active))
5472     ;; Remove entries for this group.
5473     (nnmail-purge-split-history (gnus-group-real-name group))
5474     ;; Make all changes in this group permanent.
5475     (unless quit-config
5476       (gnus-run-hooks 'gnus-exit-group-hook)
5477       (gnus-summary-update-info))
5478     (gnus-close-group group)
5479     ;; Make sure where we were, and go to next newsgroup.
5480     (set-buffer gnus-group-buffer)
5481     (unless quit-config
5482       (gnus-group-jump-to-group group))
5483     (gnus-run-hooks 'gnus-summary-exit-hook)
5484     (unless (or quit-config
5485                 ;; If this group has disappeared from the summary
5486                 ;; buffer, don't skip forwards.
5487                 (not (string= group (gnus-group-group-name))))
5488       (gnus-group-next-unread-group 1))
5489     (setq group-point (point))
5490     (if temporary
5491         nil                             ;Nothing to do.
5492       ;; If we have several article buffers, we kill them at exit.
5493       (unless gnus-single-article-buffer
5494         (gnus-kill-buffer gnus-article-buffer)
5495         (gnus-kill-buffer gnus-original-article-buffer)
5496         (setq gnus-article-current nil))
5497       (set-buffer buf)
5498       (if (not gnus-kill-summary-on-exit)
5499           (gnus-deaden-summary)
5500         ;; We set all buffer-local variables to nil.  It is unclear why
5501         ;; this is needed, but if we don't, buffer-local variables are
5502         ;; not garbage-collected, it seems.  This would the lead to en
5503         ;; ever-growing Emacs.
5504         (gnus-summary-clear-local-variables)
5505         (when (get-buffer gnus-article-buffer)
5506           (bury-buffer gnus-article-buffer))
5507         ;; We clear the global counterparts of the buffer-local
5508         ;; variables as well, just to be on the safe side.
5509         (set-buffer gnus-group-buffer)
5510         (gnus-summary-clear-local-variables)
5511         ;; Return to group mode buffer.
5512         (when (eq mode 'gnus-summary-mode)
5513           (gnus-kill-buffer buf)))
5514       (setq gnus-current-select-method gnus-select-method)
5515       (pop-to-buffer gnus-group-buffer)
5516       (if (not quit-config)
5517           (progn
5518             (goto-char group-point)
5519             (gnus-configure-windows 'group 'force)
5520             (unless (pos-visible-in-window-p)
5521               (forward-line (/ (static-if (featurep 'xemacs)
5522                                    (window-displayed-height)
5523                                  (1- (window-height)))
5524                                -2))
5525               (set-window-start (selected-window) (point))
5526               (goto-char group-point)))
5527         (gnus-handle-ephemeral-exit quit-config))
5528       ;; Clear the current group name.
5529       (unless quit-config
5530         (setq gnus-newsgroup-name nil)))))
5531
5532 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5533 (defun gnus-summary-exit-no-update (&optional no-questions)
5534   "Quit reading current newsgroup without updating read article info."
5535   (interactive)
5536   (let* ((group gnus-newsgroup-name)
5537          (quit-config (gnus-group-quit-config group)))
5538     (when (or no-questions
5539               gnus-expert-user
5540               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5541       (gnus-async-halt-prefetch)
5542       (mapcar 'funcall
5543               (delq 'gnus-summary-expire-articles
5544                     (copy-sequence gnus-summary-prepare-exit-hook)))
5545       ;; If we have several article buffers, we kill them at exit.
5546       (unless gnus-single-article-buffer
5547         (gnus-kill-buffer gnus-article-buffer)
5548         (gnus-kill-buffer gnus-original-article-buffer)
5549         (setq gnus-article-current nil))
5550       (if (not gnus-kill-summary-on-exit)
5551           (gnus-deaden-summary)
5552         (gnus-close-group group)
5553         (gnus-summary-clear-local-variables)
5554         (set-buffer gnus-group-buffer)
5555         (gnus-summary-clear-local-variables)
5556         (when (get-buffer gnus-summary-buffer)
5557           (kill-buffer gnus-summary-buffer)))
5558       (unless gnus-single-article-buffer
5559         (setq gnus-article-current nil))
5560       (when gnus-use-trees
5561         (gnus-tree-close group))
5562       (gnus-async-prefetch-remove-group group)
5563       (when (get-buffer gnus-article-buffer)
5564         (bury-buffer gnus-article-buffer))
5565       ;; Return to the group buffer.
5566       (gnus-configure-windows 'group 'force)
5567       ;; Clear the current group name.
5568       (setq gnus-newsgroup-name nil)
5569       (when (equal (gnus-group-group-name) group)
5570         (gnus-group-next-unread-group 1))
5571       (when quit-config
5572         (gnus-handle-ephemeral-exit quit-config)))))
5573
5574 (defun gnus-handle-ephemeral-exit (quit-config)
5575   "Handle movement when leaving an ephemeral group.
5576 The state which existed when entering the ephemeral is reset."
5577   (if (not (buffer-name (car quit-config)))
5578       (gnus-configure-windows 'group 'force)
5579     (set-buffer (car quit-config))
5580     (cond ((eq major-mode 'gnus-summary-mode)
5581            (gnus-set-global-variables))
5582           ((eq major-mode 'gnus-article-mode)
5583            (save-excursion
5584              ;; The `gnus-summary-buffer' variable may point
5585              ;; to the old summary buffer when using a single
5586              ;; article buffer.
5587              (unless (gnus-buffer-live-p gnus-summary-buffer)
5588                (set-buffer gnus-group-buffer))
5589              (set-buffer gnus-summary-buffer)
5590              (gnus-set-global-variables))))
5591     (if (or (eq (cdr quit-config) 'article)
5592             (eq (cdr quit-config) 'pick))
5593         (progn
5594           ;; The current article may be from the ephemeral group
5595           ;; thus it is best that we reload this article
5596           (gnus-summary-show-article)
5597           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5598               (gnus-configure-windows 'pick 'force)
5599             (gnus-configure-windows (cdr quit-config) 'force)))
5600       (gnus-configure-windows (cdr quit-config) 'force))
5601     (when (eq major-mode 'gnus-summary-mode)
5602       (gnus-summary-next-subject 1 nil t)
5603       (gnus-summary-recenter)
5604       (gnus-summary-position-point))))
5605
5606 (defun gnus-summary-preview-mime-message ()
5607   "MIME decode and play this message."
5608   (interactive)
5609   (let ((gnus-break-pages nil)
5610         (gnus-show-mime t))
5611     (gnus-summary-select-article gnus-show-all-headers t))
5612   (select-window (get-buffer-window gnus-article-buffer)))
5613
5614 ;;; Dead summaries.
5615
5616 (defvar gnus-dead-summary-mode-map nil)
5617
5618 (unless gnus-dead-summary-mode-map
5619   (setq gnus-dead-summary-mode-map (make-keymap))
5620   (suppress-keymap gnus-dead-summary-mode-map)
5621   (substitute-key-definition
5622    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5623   (let ((keys '("\C-d" "\r" "\177" [delete])))
5624     (while keys
5625       (define-key gnus-dead-summary-mode-map
5626         (pop keys) 'gnus-summary-wake-up-the-dead))))
5627
5628 (defvar gnus-dead-summary-mode nil
5629   "Minor mode for Gnus summary buffers.")
5630
5631 (defun gnus-dead-summary-mode (&optional arg)
5632   "Minor mode for Gnus summary buffers."
5633   (interactive "P")
5634   (when (eq major-mode 'gnus-summary-mode)
5635     (make-local-variable 'gnus-dead-summary-mode)
5636     (setq gnus-dead-summary-mode
5637           (if (null arg) (not gnus-dead-summary-mode)
5638             (> (prefix-numeric-value arg) 0)))
5639     (when gnus-dead-summary-mode
5640       (gnus-add-minor-mode
5641        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5642
5643 (defun gnus-deaden-summary ()
5644   "Make the current summary buffer into a dead summary buffer."
5645   ;; Kill any previous dead summary buffer.
5646   (when (and gnus-dead-summary
5647              (buffer-name gnus-dead-summary))
5648     (save-excursion
5649       (set-buffer gnus-dead-summary)
5650       (when gnus-dead-summary-mode
5651         (kill-buffer (current-buffer)))))
5652   ;; Make this the current dead summary.
5653   (setq gnus-dead-summary (current-buffer))
5654   (gnus-dead-summary-mode 1)
5655   (let ((name (buffer-name)))
5656     (when (string-match "Summary" name)
5657       (rename-buffer
5658        (concat (substring name 0 (match-beginning 0)) "Dead "
5659                (substring name (match-beginning 0)))
5660        t)
5661       (bury-buffer))))
5662
5663 (defun gnus-kill-or-deaden-summary (buffer)
5664   "Kill or deaden the summary BUFFER."
5665   (save-excursion
5666     (when (and (buffer-name buffer)
5667                (not gnus-single-article-buffer))
5668       (save-excursion
5669         (set-buffer buffer)
5670         (gnus-kill-buffer gnus-article-buffer)
5671         (gnus-kill-buffer gnus-original-article-buffer)))
5672     (cond (gnus-kill-summary-on-exit
5673            (when (and gnus-use-trees
5674                       (gnus-buffer-exists-p buffer))
5675              (save-excursion
5676                (set-buffer buffer)
5677                (gnus-tree-close gnus-newsgroup-name)))
5678            (gnus-kill-buffer buffer))
5679           ((gnus-buffer-exists-p buffer)
5680            (save-excursion
5681              (set-buffer buffer)
5682              (gnus-deaden-summary))))))
5683
5684 (defun gnus-summary-wake-up-the-dead (&rest args)
5685   "Wake up the dead summary buffer."
5686   (interactive)
5687   (gnus-dead-summary-mode -1)
5688   (let ((name (buffer-name)))
5689     (when (string-match "Dead " name)
5690       (rename-buffer
5691        (concat (substring name 0 (match-beginning 0))
5692                (substring name (match-end 0)))
5693        t)))
5694   (gnus-message 3 "This dead summary is now alive again"))
5695
5696 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5697 (defun gnus-summary-fetch-faq (&optional faq-dir)
5698   "Fetch the FAQ for the current group.
5699 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5700 in."
5701   (interactive
5702    (list
5703     (when current-prefix-arg
5704       (completing-read
5705        "Faq dir: " (and (listp gnus-group-faq-directory)
5706                         (mapcar (lambda (file) (list file))
5707                                 gnus-group-faq-directory))))))
5708   (let (gnus-faq-buffer)
5709     (when (setq gnus-faq-buffer
5710                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5711       (gnus-configure-windows 'summary-faq))))
5712
5713 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5714 (defun gnus-summary-describe-group (&optional force)
5715   "Describe the current newsgroup."
5716   (interactive "P")
5717   (gnus-group-describe-group force gnus-newsgroup-name))
5718
5719 (defun gnus-summary-describe-briefly ()
5720   "Describe summary mode commands briefly."
5721   (interactive)
5722   (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")))
5723
5724 ;; Walking around group mode buffer from summary mode.
5725
5726 (defun gnus-summary-next-group (&optional no-article target-group backward)
5727   "Exit current newsgroup and then select next unread newsgroup.
5728 If prefix argument NO-ARTICLE is non-nil, no article is selected
5729 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5730 previous group instead."
5731   (interactive "P")
5732   ;; Stop pre-fetching.
5733   (gnus-async-halt-prefetch)
5734   (let ((current-group gnus-newsgroup-name)
5735         (current-buffer (current-buffer))
5736         entered)
5737     ;; First we semi-exit this group to update Xrefs and all variables.
5738     ;; We can't do a real exit, because the window conf must remain
5739     ;; the same in case the user is prompted for info, and we don't
5740     ;; want the window conf to change before that...
5741     (gnus-summary-exit t)
5742     (while (not entered)
5743       ;; Then we find what group we are supposed to enter.
5744       (set-buffer gnus-group-buffer)
5745       (gnus-group-jump-to-group current-group)
5746       (setq target-group
5747             (or target-group
5748                 (if (eq gnus-keep-same-level 'best)
5749                     (gnus-summary-best-group gnus-newsgroup-name)
5750                   (gnus-summary-search-group backward gnus-keep-same-level))))
5751       (if (not target-group)
5752           ;; There are no further groups, so we return to the group
5753           ;; buffer.
5754           (progn
5755             (gnus-message 5 "Returning to the group buffer")
5756             (setq entered t)
5757             (when (gnus-buffer-live-p current-buffer)
5758               (set-buffer current-buffer)
5759               (gnus-summary-exit))
5760             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5761         ;; We try to enter the target group.
5762         (gnus-group-jump-to-group target-group)
5763         (let ((unreads (gnus-group-group-unread)))
5764           (if (and (or (eq t unreads)
5765                        (and unreads (not (zerop unreads))))
5766                    (gnus-summary-read-group
5767                     target-group nil no-article
5768                     (and (buffer-name current-buffer) current-buffer)
5769                     nil backward))
5770               (setq entered t)
5771             (setq current-group target-group
5772                   target-group nil)))))))
5773
5774 (defun gnus-summary-prev-group (&optional no-article)
5775   "Exit current newsgroup and then select previous unread newsgroup.
5776 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5777   (interactive "P")
5778   (gnus-summary-next-group no-article nil t))
5779
5780 ;; Walking around summary lines.
5781
5782 (defun gnus-summary-first-subject (&optional unread undownloaded)
5783   "Go to the first unread subject.
5784 If UNREAD is non-nil, go to the first unread article.
5785 Returns the article selected or nil if there are no unread articles."
5786   (interactive "P")
5787   (prog1
5788       (cond
5789        ;; Empty summary.
5790        ((null gnus-newsgroup-data)
5791         (gnus-message 3 "No articles in the group")
5792         nil)
5793        ;; Pick the first article.
5794        ((not unread)
5795         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5796         (gnus-data-number (car gnus-newsgroup-data)))
5797        ;; No unread articles.
5798        ((null gnus-newsgroup-unreads)
5799         (gnus-message 3 "No more unread articles")
5800         nil)
5801        ;; Find the first unread article.
5802        (t
5803         (let ((data gnus-newsgroup-data))
5804           (while (and data
5805                       (and (not (and undownloaded
5806                                      (eq gnus-undownloaded-mark
5807                                          (gnus-data-mark (car data)))))
5808                            (not (gnus-data-unread-p (car data)))))
5809             (setq data (cdr data)))
5810           (when data
5811             (goto-char (gnus-data-pos (car data)))
5812             (gnus-data-number (car data))))))
5813     (gnus-summary-position-point)))
5814
5815 (defun gnus-summary-next-subject (n &optional unread dont-display)
5816   "Go to next N'th summary line.
5817 If N is negative, go to the previous N'th subject line.
5818 If UNREAD is non-nil, only unread articles are selected.
5819 The difference between N and the actual number of steps taken is
5820 returned."
5821   (interactive "p")
5822   (let ((backward (< n 0))
5823         (n (abs n)))
5824     (while (and (> n 0)
5825                 (if backward
5826                     (gnus-summary-find-prev unread)
5827                   (gnus-summary-find-next unread)))
5828       (unless (zerop (setq n (1- n)))
5829         (gnus-summary-show-thread)))
5830     (when (/= 0 n)
5831       (gnus-message 7 "No more%s articles"
5832                     (if unread " unread" "")))
5833     (unless dont-display
5834       (gnus-summary-recenter)
5835       (gnus-summary-position-point))
5836     n))
5837
5838 (defun gnus-summary-next-unread-subject (n)
5839   "Go to next N'th unread summary line."
5840   (interactive "p")
5841   (gnus-summary-next-subject n t))
5842
5843 (defun gnus-summary-prev-subject (n &optional unread)
5844   "Go to previous N'th summary line.
5845 If optional argument UNREAD is non-nil, only unread article is selected."
5846   (interactive "p")
5847   (gnus-summary-next-subject (- n) unread))
5848
5849 (defun gnus-summary-prev-unread-subject (n)
5850   "Go to previous N'th unread summary line."
5851   (interactive "p")
5852   (gnus-summary-next-subject (- n) t))
5853
5854 (defun gnus-summary-goto-subject (article &optional force silent)
5855   "Go the subject line of ARTICLE.
5856 If FORCE, also allow jumping to articles not currently shown."
5857   (interactive "nArticle number: ")
5858   (let ((b (point))
5859         (data (gnus-data-find article)))
5860     ;; We read in the article if we have to.
5861     (and (not data)
5862          force
5863          (gnus-summary-insert-subject
5864           article
5865           (if (or (numberp force) (vectorp force)) force)
5866           t)
5867          (setq data (gnus-data-find article)))
5868     (goto-char b)
5869     (if (not data)
5870         (progn
5871           (unless silent
5872             (gnus-message 3 "Can't find article %d" article))
5873           nil)
5874       (goto-char (gnus-data-pos data))
5875       (gnus-summary-position-point)
5876       article)))
5877
5878 ;; Walking around summary lines with displaying articles.
5879
5880 (defun gnus-summary-expand-window (&optional arg)
5881   "Make the summary buffer take up the entire Emacs frame.
5882 Given a prefix, will force an `article' buffer configuration."
5883   (interactive "P")
5884   (if arg
5885       (gnus-configure-windows 'article 'force)
5886     (gnus-configure-windows 'summary 'force)))
5887
5888 (defun gnus-summary-display-article (article &optional all-header)
5889   "Display ARTICLE in article buffer."
5890   (gnus-set-global-variables)
5891   (when (gnus-buffer-live-p gnus-article-buffer)
5892     (with-current-buffer gnus-article-buffer
5893       (setq gnus-article-charset gnus-newsgroup-charset)
5894       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
5895   (if (null article)
5896       nil
5897     (prog1
5898         (if gnus-summary-display-article-function
5899             (funcall gnus-summary-display-article-function article all-header)
5900           (gnus-article-prepare article all-header))
5901       (with-current-buffer gnus-article-buffer
5902         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5903              nil))
5904       (gnus-run-hooks 'gnus-select-article-hook)
5905       (when (and gnus-current-article
5906                  (not (zerop gnus-current-article)))
5907         (gnus-summary-goto-subject gnus-current-article))
5908       (gnus-summary-recenter)
5909       (when (and gnus-use-trees gnus-show-threads)
5910         (gnus-possibly-generate-tree article)
5911         (gnus-highlight-selected-tree article))
5912       ;; Successfully display article.
5913       (gnus-article-set-window-start
5914        (cdr (assq article gnus-newsgroup-bookmarks))))))
5915
5916 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5917   "Select the current article.
5918 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5919 non-nil, the article will be re-fetched even if it already present in
5920 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5921 be displayed."
5922   ;; Make sure we are in the summary buffer to work around bbdb bug.
5923   (unless (eq major-mode 'gnus-summary-mode)
5924     (set-buffer gnus-summary-buffer))
5925   (let ((article (or article (gnus-summary-article-number)))
5926         (all-headers (not (not all-headers))) ;Must be T or NIL.
5927         gnus-summary-display-article-function)
5928     (and (not pseudo)
5929          (gnus-summary-article-pseudo-p article)
5930          (error "This is a pseudo-article"))
5931     (save-excursion
5932       (set-buffer gnus-summary-buffer)
5933       (if (or (and gnus-single-article-buffer
5934                    (or (null gnus-current-article)
5935                        (null gnus-article-current)
5936                        (null (get-buffer gnus-article-buffer))
5937                        (not (eq article (cdr gnus-article-current)))
5938                        (not (equal (car gnus-article-current)
5939                                    gnus-newsgroup-name))))
5940               (and (not gnus-single-article-buffer)
5941                    (or (null gnus-current-article)
5942                        (not (eq gnus-current-article article))))
5943               force)
5944           ;; The requested article is different from the current article.
5945           (progn
5946             (gnus-summary-display-article article all-headers)
5947             (when (or all-headers gnus-show-all-headers)
5948               (gnus-article-show-all-headers))
5949             (gnus-article-set-window-start
5950              (cdr (assq article gnus-newsgroup-bookmarks)))
5951             article)
5952         (when (or all-headers gnus-show-all-headers)
5953           (gnus-article-show-all-headers))
5954         'old))))
5955
5956 (defun gnus-summary-force-verify-and-decrypt ()
5957   (interactive)
5958   (let ((mm-verify-option 'known)
5959         (mm-decrypt-option 'known))
5960     (gnus-summary-select-article nil 'force)))
5961
5962 (defun gnus-summary-set-current-mark (&optional current-mark)
5963   "Obsolete function."
5964   nil)
5965
5966 (defun gnus-summary-next-article (&optional unread subject backward push)
5967   "Select the next article.
5968 If UNREAD, only unread articles are selected.
5969 If SUBJECT, only articles with SUBJECT are selected.
5970 If BACKWARD, the previous article is selected instead of the next."
5971   (interactive "P")
5972   (cond
5973    ;; Is there such an article?
5974    ((and (gnus-summary-search-forward unread subject backward)
5975          (or (gnus-summary-display-article (gnus-summary-article-number))
5976              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5977     (gnus-summary-position-point))
5978    ;; If not, we try the first unread, if that is wanted.
5979    ((and subject
5980          gnus-auto-select-same
5981          (gnus-summary-first-unread-article))
5982     (gnus-summary-position-point)
5983     (gnus-message 6 "Wrapped"))
5984    ;; Try to get next/previous article not displayed in this group.
5985    ((and gnus-auto-extend-newsgroup
5986          (not unread) (not subject))
5987     (gnus-summary-goto-article
5988      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5989      nil (count-lines (point-min) (point))))
5990    ;; Go to next/previous group.
5991    (t
5992     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5993       (gnus-summary-jump-to-group gnus-newsgroup-name))
5994     (let ((cmd last-command-char)
5995           (point
5996            (save-excursion
5997              (set-buffer gnus-group-buffer)
5998              (point)))
5999           (group
6000            (if (eq gnus-keep-same-level 'best)
6001                (gnus-summary-best-group gnus-newsgroup-name)
6002              (gnus-summary-search-group backward gnus-keep-same-level))))
6003       ;; For some reason, the group window gets selected.  We change
6004       ;; it back.
6005       (select-window (get-buffer-window (current-buffer)))
6006       ;; Select next unread newsgroup automagically.
6007       (cond
6008        ((or (not gnus-auto-select-next)
6009             (not cmd))
6010         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6011        ((or (eq gnus-auto-select-next 'quietly)
6012             (and (eq gnus-auto-select-next 'slightly-quietly)
6013                  push)
6014             (and (eq gnus-auto-select-next 'almost-quietly)
6015                  (gnus-summary-last-article-p)))
6016         ;; Select quietly.
6017         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6018             (gnus-summary-exit)
6019           (gnus-message 7 "No more%s articles (%s)..."
6020                         (if unread " unread" "")
6021                         (if group (concat "selecting " group)
6022                           "exiting"))
6023           (gnus-summary-next-group nil group backward)))
6024        (t
6025         (when (gnus-key-press-event-p last-input-event)
6026           (gnus-summary-walk-group-buffer
6027            gnus-newsgroup-name cmd unread backward point))))))))
6028
6029 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6030   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6031                       (?\C-p (gnus-group-prev-unread-group 1))))
6032         (cursor-in-echo-area t)
6033         keve key group ended)
6034     (save-excursion
6035       (set-buffer gnus-group-buffer)
6036       (goto-char start)
6037       (setq group
6038             (if (eq gnus-keep-same-level 'best)
6039                 (gnus-summary-best-group gnus-newsgroup-name)
6040               (gnus-summary-search-group backward gnus-keep-same-level))))
6041     (while (not ended)
6042       (gnus-message
6043        5 "No more%s articles%s" (if unread " unread" "")
6044        (if (and group
6045                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6046            (format " (Type %s for %s [%s])"
6047                    (single-key-description cmd) group
6048                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6049          (format " (Type %s to exit %s)"
6050                  (single-key-description cmd)
6051                  gnus-newsgroup-name)))
6052       ;; Confirm auto selection.
6053       (setq key (car (setq keve (gnus-read-event-char))))
6054       (setq ended t)
6055       (cond
6056        ((assq key keystrokes)
6057         (let ((obuf (current-buffer)))
6058           (switch-to-buffer gnus-group-buffer)
6059           (when group
6060             (gnus-group-jump-to-group group))
6061           (eval (cadr (assq key keystrokes)))
6062           (setq group (gnus-group-group-name))
6063           (switch-to-buffer obuf))
6064         (setq ended nil))
6065        ((equal key cmd)
6066         (if (or (not group)
6067                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6068             (gnus-summary-exit)
6069           (gnus-summary-next-group nil group backward)))
6070        (t
6071         (push (cdr keve) unread-command-events))))))
6072
6073 (defun gnus-summary-next-unread-article ()
6074   "Select unread article after current one."
6075   (interactive)
6076   (gnus-summary-next-article
6077    (or (not (eq gnus-summary-goto-unread 'never))
6078        (gnus-summary-last-article-p (gnus-summary-article-number)))
6079    (and gnus-auto-select-same
6080         (gnus-summary-article-subject))))
6081
6082 (defun gnus-summary-prev-article (&optional unread subject)
6083   "Select the article after the current one.
6084 If UNREAD is non-nil, only unread articles are selected."
6085   (interactive "P")
6086   (gnus-summary-next-article unread subject t))
6087
6088 (defun gnus-summary-prev-unread-article ()
6089   "Select unread article before current one."
6090   (interactive)
6091   (gnus-summary-prev-article
6092    (or (not (eq gnus-summary-goto-unread 'never))
6093        (gnus-summary-first-article-p (gnus-summary-article-number)))
6094    (and gnus-auto-select-same
6095         (gnus-summary-article-subject))))
6096
6097 (defun gnus-summary-next-page (&optional lines circular)
6098   "Show next page of the selected article.
6099 If at the end of the current article, select the next article.
6100 LINES says how many lines should be scrolled up.
6101
6102 If CIRCULAR is non-nil, go to the start of the article instead of
6103 selecting the next article when reaching the end of the current
6104 article."
6105   (interactive "P")
6106   (setq gnus-summary-buffer (current-buffer))
6107   (gnus-set-global-variables)
6108   (let ((article (gnus-summary-article-number))
6109         (article-window (get-buffer-window gnus-article-buffer t))
6110         endp)
6111     ;; If the buffer is empty, we have no article.
6112     (unless article
6113       (error "No article to select"))
6114     (gnus-configure-windows 'article)
6115     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6116         (if (and (eq gnus-summary-goto-unread 'never)
6117                  (not (gnus-summary-last-article-p article)))
6118             (gnus-summary-next-article)
6119           (gnus-summary-next-unread-article))
6120       (if (or (null gnus-current-article)
6121               (null gnus-article-current)
6122               (/= article (cdr gnus-article-current))
6123               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6124           ;; Selected subject is different from current article's.
6125           (gnus-summary-display-article article)
6126         (when article-window
6127           (gnus-eval-in-buffer-window gnus-article-buffer
6128             (setq endp (gnus-article-next-page lines)))
6129           (when endp
6130             (cond (circular
6131                    (gnus-summary-beginning-of-article))
6132                   (lines
6133                    (gnus-message 3 "End of message"))
6134                   ((null lines)
6135                    (if (and (eq gnus-summary-goto-unread 'never)
6136                             (not (gnus-summary-last-article-p article)))
6137                        (gnus-summary-next-article)
6138                      (gnus-summary-next-unread-article))))))))
6139     (gnus-summary-recenter)
6140     (gnus-summary-position-point)))
6141
6142 (defun gnus-summary-prev-page (&optional lines move)
6143   "Show previous page of selected article.
6144 Argument LINES specifies lines to be scrolled down.
6145 If MOVE, move to the previous unread article if point is at
6146 the beginning of the buffer."
6147   (interactive "P")
6148   (let ((article (gnus-summary-article-number))
6149         (article-window (get-buffer-window gnus-article-buffer t))
6150         endp)
6151     (gnus-configure-windows 'article)
6152     (if (or (null gnus-current-article)
6153             (null gnus-article-current)
6154             (/= article (cdr gnus-article-current))
6155             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6156         ;; Selected subject is different from current article's.
6157         (gnus-summary-display-article article)
6158       (gnus-summary-recenter)
6159       (when article-window
6160         (gnus-eval-in-buffer-window gnus-article-buffer
6161           (setq endp (gnus-article-prev-page lines)))
6162         (when (and move endp)
6163           (cond (lines
6164                  (gnus-message 3 "Beginning of message"))
6165                 ((null lines)
6166                  (if (and (eq gnus-summary-goto-unread 'never)
6167                           (not (gnus-summary-first-article-p article)))
6168                      (gnus-summary-prev-article)
6169                    (gnus-summary-prev-unread-article))))))))
6170   (gnus-summary-position-point))
6171
6172 (defun gnus-summary-prev-page-or-article (&optional lines)
6173   "Show previous page of selected article.
6174 Argument LINES specifies lines to be scrolled down.
6175 If at the beginning of the article, go to the next article."
6176   (interactive "P")
6177   (gnus-summary-prev-page lines t))
6178
6179 (defun gnus-summary-scroll-up (lines)
6180   "Scroll up (or down) one line current article.
6181 Argument LINES specifies lines to be scrolled up (or down if negative)."
6182   (interactive "p")
6183   (gnus-configure-windows 'article)
6184   (gnus-summary-show-thread)
6185   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6186     (gnus-eval-in-buffer-window gnus-article-buffer
6187       (cond ((> lines 0)
6188              (when (gnus-article-next-page lines)
6189                (gnus-message 3 "End of message")))
6190             ((< lines 0)
6191              (gnus-article-prev-page (- lines))))))
6192   (gnus-summary-recenter)
6193   (gnus-summary-position-point))
6194
6195 (defun gnus-summary-scroll-down (lines)
6196   "Scroll down (or up) one line current article.
6197 Argument LINES specifies lines to be scrolled down (or up if negative)."
6198   (interactive "p")
6199   (gnus-summary-scroll-up (- lines)))
6200
6201 (defun gnus-summary-next-same-subject ()
6202   "Select next article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-prev-same-subject ()
6207   "Select previous article which has the same subject as current one."
6208   (interactive)
6209   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6210
6211 (defun gnus-summary-next-unread-same-subject ()
6212   "Select next unread article which has the same subject as current one."
6213   (interactive)
6214   (gnus-summary-next-article t (gnus-summary-article-subject)))
6215
6216 (defun gnus-summary-prev-unread-same-subject ()
6217   "Select previous unread article which has the same subject as current one."
6218   (interactive)
6219   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6220
6221 (defun gnus-summary-first-unread-article ()
6222   "Select the first unread article.
6223 Return nil if there are no unread articles."
6224   (interactive)
6225   (prog1
6226       (when (gnus-summary-first-subject t)
6227         (gnus-summary-show-thread)
6228         (gnus-summary-first-subject t)
6229         (gnus-summary-display-article (gnus-summary-article-number)))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-first-unread-subject ()
6233   "Place the point on the subject line of the first unread article.
6234 Return nil if there are no unread articles."
6235   (interactive)
6236   (prog1
6237       (when (gnus-summary-first-subject t)
6238         (gnus-summary-show-thread)
6239         (gnus-summary-first-subject t))
6240     (gnus-summary-position-point)))
6241
6242 (defun gnus-summary-first-article ()
6243   "Select the first article.
6244 Return nil if there are no articles."
6245   (interactive)
6246   (prog1
6247       (when (gnus-summary-first-subject)
6248         (gnus-summary-show-thread)
6249         (gnus-summary-first-subject)
6250         (gnus-summary-display-article (gnus-summary-article-number)))
6251     (gnus-summary-position-point)))
6252
6253 (defun gnus-summary-best-unread-article ()
6254   "Select the unread article with the highest score."
6255   (interactive)
6256   (let ((best -1000000)
6257         (data gnus-newsgroup-data)
6258         article score)
6259     (while data
6260       (and (gnus-data-unread-p (car data))
6261            (> (setq score
6262                     (gnus-summary-article-score (gnus-data-number (car data))))
6263               best)
6264            (setq best score
6265                  article (gnus-data-number (car data))))
6266       (setq data (cdr data)))
6267     (prog1
6268         (if article
6269             (gnus-summary-goto-article article)
6270           (error "No unread articles"))
6271       (gnus-summary-position-point))))
6272
6273 (defun gnus-summary-last-subject ()
6274   "Go to the last displayed subject line in the group."
6275   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6276     (when article
6277       (gnus-summary-goto-subject article))))
6278
6279 (defun gnus-summary-goto-article (article &optional all-headers force)
6280   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6281 If ALL-HEADERS is non-nil, no header lines are hidden.
6282 If FORCE, go to the article even if it isn't displayed.  If FORCE
6283 is a number, it is the line the article is to be displayed on."
6284   (interactive
6285    (list
6286     (completing-read
6287      "Article number or Message-ID: "
6288      (mapcar (lambda (number) (list (int-to-string number)))
6289              gnus-newsgroup-limit))
6290     current-prefix-arg
6291     t))
6292   (prog1
6293       (if (and (stringp article)
6294                (string-match "@" article))
6295           (gnus-summary-refer-article article)
6296         (when (stringp article)
6297           (setq article (string-to-number article)))
6298         (if (gnus-summary-goto-subject article force)
6299             (gnus-summary-display-article article all-headers)
6300           (gnus-message 4 "Couldn't go to article %s" article) nil))
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-goto-last-article ()
6304   "Go to the previously read article."
6305   (interactive)
6306   (prog1
6307       (when gnus-last-article
6308         (gnus-summary-goto-article gnus-last-article nil t))
6309     (gnus-summary-position-point)))
6310
6311 (defun gnus-summary-pop-article (number)
6312   "Pop one article off the history and go to the previous.
6313 NUMBER articles will be popped off."
6314   (interactive "p")
6315   (let (to)
6316     (setq gnus-newsgroup-history
6317           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6318     (if to
6319         (gnus-summary-goto-article (car to) nil t)
6320       (error "Article history empty")))
6321   (gnus-summary-position-point))
6322
6323 ;; Summary commands and functions for limiting the summary buffer.
6324
6325 (defun gnus-summary-limit-to-articles (n)
6326   "Limit the summary buffer to the next N articles.
6327 If not given a prefix, use the process marked articles instead."
6328   (interactive "P")
6329   (prog1
6330       (let ((articles (gnus-summary-work-articles n)))
6331         (setq gnus-newsgroup-processable nil)
6332         (gnus-summary-limit articles))
6333     (gnus-summary-position-point)))
6334
6335 (defun gnus-summary-pop-limit (&optional total)
6336   "Restore the previous limit.
6337 If given a prefix, remove all limits."
6338   (interactive "P")
6339   (when total
6340     (setq gnus-newsgroup-limits
6341           (list (mapcar (lambda (h) (mail-header-number h))
6342                         gnus-newsgroup-headers))))
6343   (unless gnus-newsgroup-limits
6344     (error "No limit to pop"))
6345   (prog1
6346       (gnus-summary-limit nil 'pop)
6347     (gnus-summary-position-point)))
6348
6349 (defun gnus-summary-limit-to-subject (subject &optional header)
6350   "Limit the summary buffer to articles that have subjects that match a regexp."
6351   (interactive "sLimit to subject (regexp): ")
6352   (unless header
6353     (setq header "subject"))
6354   (when (not (equal "" subject))
6355     (prog1
6356         (let ((articles (gnus-summary-find-matching
6357                          (or header "subject") subject 'all)))
6358           (unless articles
6359             (error "Found no matches for \"%s\"" subject))
6360           (gnus-summary-limit articles))
6361       (gnus-summary-position-point))))
6362
6363 (defun gnus-summary-limit-to-author (from)
6364   "Limit the summary buffer to articles that have authors that match a regexp."
6365   (interactive "sLimit to author (regexp): ")
6366   (gnus-summary-limit-to-subject from "from"))
6367
6368 (defun gnus-summary-limit-to-age (age &optional younger-p)
6369   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6370 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6371 articles that are younger than AGE days."
6372   (interactive
6373    (let ((younger current-prefix-arg)
6374          (days-got nil)
6375          days)
6376      (while (not days-got)
6377        (setq days (if younger
6378                       (read-string "Limit to articles within (in days): ")
6379                     (read-string "Limit to articles old than (in days): ")))
6380        (when (> (length days) 0)
6381          (setq days (read days)))
6382        (if (numberp days)
6383            (setq days-got t)
6384          (message "Please enter a number.")
6385          (sleep-for 1)))
6386      (list days younger)))
6387   (prog1
6388       (let ((data gnus-newsgroup-data)
6389             (cutoff (days-to-time age))
6390             articles d date is-younger)
6391         (while (setq d (pop data))
6392           (when (and (vectorp (gnus-data-header d))
6393                      (setq date (mail-header-date (gnus-data-header d))))
6394             (setq is-younger (time-less-p
6395                               (time-since (condition-case ()
6396                                               (date-to-time date)
6397                                             (error '(0 0))))
6398                               cutoff))
6399             (when (if younger-p
6400                       is-younger
6401                     (not is-younger))
6402               (push (gnus-data-number d) articles))))
6403         (gnus-summary-limit (nreverse articles)))
6404     (gnus-summary-position-point)))
6405
6406 (defun gnus-summary-limit-to-extra (header regexp)
6407   "Limit the summary buffer to articles that match an 'extra' header."
6408   (interactive
6409    (let ((header
6410           (intern
6411            (gnus-completing-read
6412             (symbol-name (car gnus-extra-headers))
6413             "Limit extra header:"
6414             (mapcar (lambda (x)
6415                       (cons (symbol-name x) x))
6416                     gnus-extra-headers)
6417             nil
6418             t))))
6419      (list header
6420            (read-string (format "Limit to header %s (regexp): " header)))))
6421   (when (not (equal "" regexp))
6422     (prog1
6423         (let ((articles (gnus-summary-find-matching
6424                          (cons 'extra header) regexp 'all)))
6425           (unless articles
6426             (error "Found no matches for \"%s\"" regexp))
6427           (gnus-summary-limit articles))
6428       (gnus-summary-position-point))))
6429
6430 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6431 (make-obsolete
6432  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6433
6434 (defun gnus-summary-limit-to-unread (&optional all)
6435   "Limit the summary buffer to articles that are not marked as read.
6436 If ALL is non-nil, limit strictly to unread articles."
6437   (interactive "P")
6438   (if all
6439       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6440     (gnus-summary-limit-to-marks
6441      ;; Concat all the marks that say that an article is read and have
6442      ;; those removed.
6443      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6444            gnus-killed-mark gnus-kill-file-mark
6445            gnus-low-score-mark gnus-expirable-mark
6446            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6447            gnus-duplicate-mark gnus-souped-mark)
6448      'reverse)))
6449
6450 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6451 (make-obsolete 'gnus-summary-delete-marked-with
6452                'gnus-summary-limit-exlude-marks)
6453
6454 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6455   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6456 If REVERSE, limit the summary buffer to articles that are marked
6457 with MARKS.  MARKS can either be a string of marks or a list of marks.
6458 Returns how many articles were removed."
6459   (interactive "sMarks: ")
6460   (gnus-summary-limit-to-marks marks t))
6461
6462 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6463   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6464 If REVERSE (the prefix), limit the summary buffer to articles that are
6465 not marked with MARKS.  MARKS can either be a string of marks or a
6466 list of marks.
6467 Returns how many articles were removed."
6468   (interactive "sMarks: \nP")
6469   (prog1
6470       (let ((data gnus-newsgroup-data)
6471             (marks (if (listp marks) marks
6472                      (append marks nil))) ; Transform to list.
6473             articles)
6474         (while data
6475           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6476                   (memq (gnus-data-mark (car data)) marks))
6477             (push (gnus-data-number (car data)) articles))
6478           (setq data (cdr data)))
6479         (gnus-summary-limit articles))
6480     (gnus-summary-position-point)))
6481
6482 (defun gnus-summary-limit-to-score (&optional score)
6483   "Limit to articles with score at or above SCORE."
6484   (interactive "P")
6485   (setq score (if score
6486                   (prefix-numeric-value score)
6487                 (or gnus-summary-default-score 0)))
6488   (let ((data gnus-newsgroup-data)
6489         articles)
6490     (while data
6491       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6492                 score)
6493         (push (gnus-data-number (car data)) articles))
6494       (setq data (cdr data)))
6495     (prog1
6496         (gnus-summary-limit articles)
6497       (gnus-summary-position-point))))
6498
6499 (defun gnus-summary-limit-include-thread (id)
6500   "Display all the hidden articles that in the current thread."
6501   (interactive (list (mail-header-id (gnus-summary-article-header))))
6502   (let ((articles (gnus-articles-in-thread
6503                    (gnus-id-to-thread (gnus-root-id id)))))
6504     (prog1
6505         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6506       (gnus-summary-position-point))))
6507
6508 (defun gnus-summary-limit-include-dormant ()
6509   "Display all the hidden articles that are marked as dormant.
6510 Note that this command only works on a subset of the articles currently
6511 fetched for this group."
6512   (interactive)
6513   (unless gnus-newsgroup-dormant
6514     (error "There are no dormant articles in this group"))
6515   (prog1
6516       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6517     (gnus-summary-position-point)))
6518
6519 (defun gnus-summary-limit-exclude-dormant ()
6520   "Hide all dormant articles."
6521   (interactive)
6522   (prog1
6523       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6524     (gnus-summary-position-point)))
6525
6526 (defun gnus-summary-limit-exclude-childless-dormant ()
6527   "Hide all dormant articles that have no children."
6528   (interactive)
6529   (let ((data (gnus-data-list t))
6530         articles d children)
6531     ;; Find all articles that are either not dormant or have
6532     ;; children.
6533     (while (setq d (pop data))
6534       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6535                 (and (setq children
6536                            (gnus-article-children (gnus-data-number d)))
6537                      (let (found)
6538                        (while children
6539                          (when (memq (car children) articles)
6540                            (setq children nil
6541                                  found t))
6542                          (pop children))
6543                        found)))
6544         (push (gnus-data-number d) articles)))
6545     ;; Do the limiting.
6546     (prog1
6547         (gnus-summary-limit articles)
6548       (gnus-summary-position-point))))
6549
6550 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6551   "Mark all unread excluded articles as read.
6552 If ALL, mark even excluded ticked and dormants as read."
6553   (interactive "P")
6554   (let ((articles (gnus-sorted-complement
6555                    (sort
6556                     (mapcar (lambda (h) (mail-header-number h))
6557                             gnus-newsgroup-headers)
6558                     '<)
6559                    (sort gnus-newsgroup-limit '<)))
6560         article)
6561     (setq gnus-newsgroup-unreads
6562           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6563     (if all
6564         (setq gnus-newsgroup-dormant nil
6565               gnus-newsgroup-marked nil
6566               gnus-newsgroup-reads
6567               (nconc
6568                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6569                gnus-newsgroup-reads))
6570       (while (setq article (pop articles))
6571         (unless (or (memq article gnus-newsgroup-dormant)
6572                     (memq article gnus-newsgroup-marked))
6573           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6574
6575 (defun gnus-summary-limit (articles &optional pop)
6576   (if pop
6577       ;; We pop the previous limit off the stack and use that.
6578       (setq articles (car gnus-newsgroup-limits)
6579             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6580     ;; We use the new limit, so we push the old limit on the stack.
6581     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6582   ;; Set the limit.
6583   (setq gnus-newsgroup-limit articles)
6584   (let ((total (length gnus-newsgroup-data))
6585         (data (gnus-data-find-list (gnus-summary-article-number)))
6586         (gnus-summary-mark-below nil)   ; Inhibit this.
6587         found)
6588     ;; This will do all the work of generating the new summary buffer
6589     ;; according to the new limit.
6590     (gnus-summary-prepare)
6591     ;; Hide any threads, possibly.
6592     (and gnus-show-threads
6593          gnus-thread-hide-subtree
6594          (gnus-summary-hide-all-threads))
6595     ;; Try to return to the article you were at, or one in the
6596     ;; neighborhood.
6597     (when data
6598       ;; We try to find some article after the current one.
6599       (while data
6600         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6601           (setq data nil
6602                 found t))
6603         (setq data (cdr data))))
6604     (unless found
6605       ;; If there is no data, that means that we were after the last
6606       ;; article.  The same goes when we can't find any articles
6607       ;; after the current one.
6608       (goto-char (point-max))
6609       (gnus-summary-find-prev))
6610     (gnus-set-mode-line 'summary)
6611     ;; We return how many articles were removed from the summary
6612     ;; buffer as a result of the new limit.
6613     (- total (length gnus-newsgroup-data))))
6614
6615 (defsubst gnus-invisible-cut-children (threads)
6616   (let ((num 0))
6617     (while threads
6618       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6619         (incf num))
6620       (pop threads))
6621     (< num 2)))
6622
6623 (defsubst gnus-cut-thread (thread)
6624   "Go forwards in the thread until we find an article that we want to display."
6625   (when (or (eq gnus-fetch-old-headers 'some)
6626             (eq gnus-fetch-old-headers 'invisible)
6627             (numberp gnus-fetch-old-headers)
6628             (eq gnus-build-sparse-threads 'some)
6629             (eq gnus-build-sparse-threads 'more))
6630     ;; Deal with old-fetched headers and sparse threads.
6631     (while (and
6632             thread
6633             (or
6634              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6635              (gnus-summary-article-ancient-p
6636               (mail-header-number (car thread))))
6637             (if (or (<= (length (cdr thread)) 1)
6638                     (eq gnus-fetch-old-headers 'invisible))
6639                 (setq gnus-newsgroup-limit
6640                       (delq (mail-header-number (car thread))
6641                             gnus-newsgroup-limit)
6642                       thread (cadr thread))
6643               (when (gnus-invisible-cut-children (cdr thread))
6644                 (let ((th (cdr thread)))
6645                   (while th
6646                     (if (memq (mail-header-number (caar th))
6647                               gnus-newsgroup-limit)
6648                         (setq thread (car th)
6649                               th nil)
6650                       (setq th (cdr th))))))))))
6651   thread)
6652
6653 (defun gnus-cut-threads (threads)
6654   "Cut off all uninteresting articles from the beginning of threads."
6655   (when (or (eq gnus-fetch-old-headers 'some)
6656             (eq gnus-fetch-old-headers 'invisible)
6657             (numberp gnus-fetch-old-headers)
6658             (eq gnus-build-sparse-threads 'some)
6659             (eq gnus-build-sparse-threads 'more))
6660     (let ((th threads))
6661       (while th
6662         (setcar th (gnus-cut-thread (car th)))
6663         (setq th (cdr th)))))
6664   ;; Remove nixed out threads.
6665   (delq nil threads))
6666
6667 (defun gnus-summary-initial-limit (&optional show-if-empty)
6668   "Figure out what the initial limit is supposed to be on group entry.
6669 This entails weeding out unwanted dormants, low-scored articles,
6670 fetch-old-headers verbiage, and so on."
6671   ;; Most groups have nothing to remove.
6672   (if (or gnus-inhibit-limiting
6673           (and (null gnus-newsgroup-dormant)
6674                (not (eq gnus-fetch-old-headers 'some))
6675                (not (numberp gnus-fetch-old-headers))
6676                (not (eq gnus-fetch-old-headers 'invisible))
6677                (null gnus-summary-expunge-below)
6678                (not (eq gnus-build-sparse-threads 'some))
6679                (not (eq gnus-build-sparse-threads 'more))
6680                (null gnus-thread-expunge-below)
6681                (not gnus-use-nocem)))
6682       ()                                ; Do nothing.
6683     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6684     (setq gnus-newsgroup-limit nil)
6685     (mapatoms
6686      (lambda (node)
6687        (unless (car (symbol-value node))
6688          ;; These threads have no parents -- they are roots.
6689          (let ((nodes (cdr (symbol-value node)))
6690                thread)
6691            (while nodes
6692              (if (and gnus-thread-expunge-below
6693                       (< (gnus-thread-total-score (car nodes))
6694                          gnus-thread-expunge-below))
6695                  (gnus-expunge-thread (pop nodes))
6696                (setq thread (pop nodes))
6697                (gnus-summary-limit-children thread))))))
6698      gnus-newsgroup-dependencies)
6699     ;; If this limitation resulted in an empty group, we might
6700     ;; pop the previous limit and use it instead.
6701     (when (and (not gnus-newsgroup-limit)
6702                show-if-empty)
6703       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6704     gnus-newsgroup-limit))
6705
6706 (defun gnus-summary-limit-children (thread)
6707   "Return 1 if this subthread is visible and 0 if it is not."
6708   ;; First we get the number of visible children to this thread.  This
6709   ;; is done by recursing down the thread using this function, so this
6710   ;; will really go down to a leaf article first, before slowly
6711   ;; working its way up towards the root.
6712   (when thread
6713     (let ((children
6714            (if (cdr thread)
6715                (apply '+ (mapcar 'gnus-summary-limit-children
6716                                  (cdr thread)))
6717              0))
6718           (number (mail-header-number (car thread)))
6719           score)
6720       (if (and
6721            (not (memq number gnus-newsgroup-marked))
6722            (or
6723             ;; If this article is dormant and has absolutely no visible
6724             ;; children, then this article isn't visible.
6725             (and (memq number gnus-newsgroup-dormant)
6726                  (zerop children))
6727             ;; If this is "fetch-old-headered" and there is no
6728             ;; visible children, then we don't want this article.
6729             (and (or (eq gnus-fetch-old-headers 'some)
6730                      (numberp gnus-fetch-old-headers))
6731                  (gnus-summary-article-ancient-p number)
6732                  (zerop children))
6733             ;; If this is "fetch-old-headered" and `invisible', then
6734             ;; we don't want this article.
6735             (and (eq gnus-fetch-old-headers 'invisible)
6736                  (gnus-summary-article-ancient-p number))
6737             ;; If this is a sparsely inserted article with no children,
6738             ;; we don't want it.
6739             (and (eq gnus-build-sparse-threads 'some)
6740                  (gnus-summary-article-sparse-p number)
6741                  (zerop children))
6742             ;; If we use expunging, and this article is really
6743             ;; low-scored, then we don't want this article.
6744             (when (and gnus-summary-expunge-below
6745                        (< (setq score
6746                                 (or (cdr (assq number gnus-newsgroup-scored))
6747                                     gnus-summary-default-score))
6748                           gnus-summary-expunge-below))
6749               ;; We increase the expunge-tally here, but that has
6750               ;; nothing to do with the limits, really.
6751               (incf gnus-newsgroup-expunged-tally)
6752               ;; We also mark as read here, if that's wanted.
6753               (when (and gnus-summary-mark-below
6754                          (< score gnus-summary-mark-below))
6755                 (setq gnus-newsgroup-unreads
6756                       (delq number gnus-newsgroup-unreads))
6757                 (if gnus-newsgroup-auto-expire
6758                     (push number gnus-newsgroup-expirable)
6759                   (push (cons number gnus-low-score-mark)
6760                         gnus-newsgroup-reads)))
6761               t)
6762             ;; Check NoCeM things.
6763             (if (and gnus-use-nocem
6764                      (gnus-nocem-unwanted-article-p
6765                       (mail-header-id (car thread))))
6766                 (progn
6767                   (setq gnus-newsgroup-unreads
6768                         (delq number gnus-newsgroup-unreads))
6769                   t))))
6770           ;; Nope, invisible article.
6771           0
6772         ;; Ok, this article is to be visible, so we add it to the limit
6773         ;; and return 1.
6774         (push number gnus-newsgroup-limit)
6775         1))))
6776
6777 (defun gnus-expunge-thread (thread)
6778   "Mark all articles in THREAD as read."
6779   (let* ((number (mail-header-number (car thread))))
6780     (incf gnus-newsgroup-expunged-tally)
6781     ;; We also mark as read here, if that's wanted.
6782     (setq gnus-newsgroup-unreads
6783           (delq number gnus-newsgroup-unreads))
6784     (if gnus-newsgroup-auto-expire
6785         (push number gnus-newsgroup-expirable)
6786       (push (cons number gnus-low-score-mark)
6787             gnus-newsgroup-reads)))
6788   ;; Go recursively through all subthreads.
6789   (mapcar 'gnus-expunge-thread (cdr thread)))
6790
6791 ;; Summary article oriented commands
6792
6793 (defun gnus-summary-refer-parent-article (n)
6794   "Refer parent article N times.
6795 If N is negative, go to ancestor -N instead.
6796 The difference between N and the number of articles fetched is returned."
6797   (interactive "p")
6798   (let ((skip 1)
6799         error header ref)
6800     (when (not (natnump n))
6801       (setq skip (abs n)
6802             n 1))
6803     (while (and (> n 0)
6804                 (not error))
6805       (setq header (gnus-summary-article-header))
6806       (if (and (eq (mail-header-number header)
6807                    (cdr gnus-article-current))
6808                (equal gnus-newsgroup-name
6809                       (car gnus-article-current)))
6810           ;; If we try to find the parent of the currently
6811           ;; displayed article, then we take a look at the actual
6812           ;; References header, since this is slightly more
6813           ;; reliable than the References field we got from the
6814           ;; server.
6815           (save-excursion
6816             (set-buffer gnus-original-article-buffer)
6817             (nnheader-narrow-to-headers)
6818             (unless (setq ref (message-fetch-field "references"))
6819               (setq ref (message-fetch-field "in-reply-to")))
6820             (widen))
6821         (setq ref
6822               ;; It's not the current article, so we take a bet on
6823               ;; the value we got from the server.
6824               (mail-header-references header)))
6825       (if (and ref
6826                (not (equal ref "")))
6827           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6828             (gnus-message 1 "Couldn't find parent"))
6829         (gnus-message 1 "No references in article %d"
6830                       (gnus-summary-article-number))
6831         (setq error t))
6832       (decf n))
6833     (gnus-summary-position-point)
6834     n))
6835
6836 (defun gnus-summary-refer-references ()
6837   "Fetch all articles mentioned in the References header.
6838 Return the number of articles fetched."
6839   (interactive)
6840   (let ((ref (mail-header-references (gnus-summary-article-header)))
6841         (current (gnus-summary-article-number))
6842         (n 0))
6843     (if (or (not ref)
6844             (equal ref ""))
6845         (error "No References in the current article")
6846       ;; For each Message-ID in the References header...
6847       (while (string-match "<[^>]*>" ref)
6848         (incf n)
6849         ;; ... fetch that article.
6850         (gnus-summary-refer-article
6851          (prog1 (match-string 0 ref)
6852            (setq ref (substring ref (match-end 0))))))
6853       (gnus-summary-goto-subject current)
6854       (gnus-summary-position-point)
6855       n)))
6856
6857 (defun gnus-summary-refer-thread (&optional limit)
6858   "Fetch all articles in the current thread.
6859 If LIMIT (the numerical prefix), fetch that many old headers instead
6860 of what's specified by the `gnus-refer-thread-limit' variable."
6861   (interactive "P")
6862   (let ((id (mail-header-id (gnus-summary-article-header)))
6863         (limit (if limit (prefix-numeric-value limit)
6864                  gnus-refer-thread-limit)))
6865     ;; We want to fetch LIMIT *old* headers, but we also have to
6866     ;; re-fetch all the headers in the current buffer, because many of
6867     ;; them may be undisplayed.  So we adjust LIMIT.
6868     (when (numberp limit)
6869       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6870     (unless (eq gnus-fetch-old-headers 'invisible)
6871       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6872       ;; Retrieve the headers and read them in.
6873       (if (eq (gnus-retrieve-headers
6874                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6875               'nov)
6876           (gnus-build-all-threads)
6877         (error "Can't fetch thread from backends that don't support NOV"))
6878       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6879     (gnus-summary-limit-include-thread id)))
6880
6881 (defun gnus-summary-refer-article (message-id)
6882   "Fetch an article specified by MESSAGE-ID."
6883   (interactive "sMessage-ID: ")
6884   (when (and (stringp message-id)
6885              (not (zerop (length message-id))))
6886     ;; Construct the correct Message-ID if necessary.
6887     ;; Suggested by tale@pawl.rpi.edu.
6888     (unless (string-match "^<" message-id)
6889       (setq message-id (concat "<" message-id)))
6890     (unless (string-match ">$" message-id)
6891       (setq message-id (concat message-id ">")))
6892     (let* ((header (gnus-id-to-header message-id))
6893            (sparse (and header
6894                         (gnus-summary-article-sparse-p
6895                          (mail-header-number header))
6896                         (memq (mail-header-number header)
6897                               gnus-newsgroup-limit)))
6898            number)
6899       (cond
6900        ;; If the article is present in the buffer we just go to it.
6901        ((and header
6902              (or (not (gnus-summary-article-sparse-p
6903                        (mail-header-number header)))
6904                  sparse))
6905         (prog1
6906             (gnus-summary-goto-article
6907              (mail-header-number header) nil t)
6908           (when sparse
6909             (gnus-summary-update-article (mail-header-number header)))))
6910        (t
6911         ;; We fetch the article.
6912         (catch 'found
6913           (dolist (gnus-override-method (gnus-refer-article-methods))
6914             (gnus-check-server gnus-override-method)
6915             ;; Fetch the header, and display the article.
6916             (when (setq number (gnus-summary-insert-subject message-id))
6917               (gnus-summary-select-article nil nil nil number)
6918               (throw 'found t)))
6919           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6920
6921 (defun gnus-refer-article-methods ()
6922   "Return a list of referrable methods."
6923   (cond
6924    ;; No method, so we default to current and native.
6925    ((null gnus-refer-article-method)
6926     (list gnus-current-select-method gnus-select-method))
6927    ;; Current.
6928    ((eq 'current gnus-refer-article-method)
6929     (list gnus-current-select-method))
6930    ;; List of select methods.
6931    ((not (and (symbolp (car gnus-refer-article-method))
6932               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6933     (let (out)
6934       (dolist (method gnus-refer-article-method)
6935         (push (if (eq 'current method)
6936                   gnus-current-select-method
6937                 method)
6938               out))
6939       (nreverse out)))
6940    ;; One single select method.
6941    (t
6942     (list gnus-refer-article-method))))
6943
6944 (defun gnus-summary-edit-parameters ()
6945   "Edit the group parameters of the current group."
6946   (interactive)
6947   (gnus-group-edit-group gnus-newsgroup-name 'params))
6948
6949 (defun gnus-summary-customize-parameters ()
6950   "Customize the group parameters of the current group."
6951   (interactive)
6952   (gnus-group-customize gnus-newsgroup-name))
6953
6954 (defun gnus-summary-enter-digest-group (&optional force)
6955   "Enter an nndoc group based on the current article.
6956 If FORCE, force a digest interpretation.  If not, try
6957 to guess what the document format is."
6958   (interactive "P")
6959   (let ((conf gnus-current-window-configuration))
6960     (save-excursion
6961       (gnus-summary-select-article))
6962     (setq gnus-current-window-configuration conf)
6963     (let* ((name (format "%s-%d"
6964                          (gnus-group-prefixed-name
6965                           gnus-newsgroup-name (list 'nndoc ""))
6966                          (save-excursion
6967                            (set-buffer gnus-summary-buffer)
6968                            gnus-current-article)))
6969            (ogroup gnus-newsgroup-name)
6970            (params (append (gnus-info-params (gnus-get-info ogroup))
6971                            (list (cons 'to-group ogroup))
6972                            (list (cons 'save-article-group ogroup))))
6973            (case-fold-search t)
6974            (buf (current-buffer))
6975            dig to-address)
6976       (save-excursion
6977         (set-buffer gnus-original-article-buffer)
6978         ;; Have the digest group inherit the main mail address of
6979         ;; the parent article.
6980         (when (setq to-address (or (message-fetch-field "reply-to")
6981                                    (message-fetch-field "from")))
6982           (setq params (append 
6983                         (list (cons 'to-address 
6984                                     (funcall gnus-decode-encoded-word-function
6985                                              to-address))))))
6986         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6987         (insert-buffer-substring gnus-original-article-buffer)
6988         ;; Remove lines that may lead nndoc to misinterpret the
6989         ;; document type.
6990         (narrow-to-region
6991          (goto-char (point-min))
6992          (or (search-forward "\n\n" nil t) (point)))
6993         (goto-char (point-min))
6994         (delete-matching-lines "^Path:\\|^From ")
6995         (widen))
6996       (unwind-protect
6997           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6998                     (gnus-newsgroup-ephemeral-ignored-charsets
6999                      gnus-newsgroup-ignored-charsets))
7000                 (gnus-group-read-ephemeral-group
7001                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7002                               (nndoc-article-type
7003                                ,(if force 'mbox 'guess))) t))
7004               ;; Make all postings to this group go to the parent group.
7005               (nconc (gnus-info-params (gnus-get-info name))
7006                      params)
7007             ;; Couldn't select this doc group.
7008             (switch-to-buffer buf)
7009             (gnus-set-global-variables)
7010             (gnus-configure-windows 'summary)
7011             (gnus-message 3 "Article couldn't be entered?"))
7012         (kill-buffer dig)))))
7013
7014 (defun gnus-summary-read-document (n)
7015   "Open a new group based on the current article(s).
7016 This will allow you to read digests and other similar
7017 documents as newsgroups.
7018 Obeys the standard process/prefix convention."
7019   (interactive "P")
7020   (let* ((articles (gnus-summary-work-articles n))
7021          (ogroup gnus-newsgroup-name)
7022          (params (append (gnus-info-params (gnus-get-info ogroup))
7023                          (list (cons 'to-group ogroup))))
7024          article group egroup groups vgroup)
7025     (while (setq article (pop articles))
7026       (setq group (format "%s-%d" gnus-newsgroup-name article))
7027       (gnus-summary-remove-process-mark article)
7028       (when (gnus-summary-display-article article)
7029         (save-excursion
7030           (with-temp-buffer
7031             (insert-buffer-substring gnus-original-article-buffer)
7032             ;; Remove some headers that may lead nndoc to make
7033             ;; the wrong guess.
7034             (message-narrow-to-head)
7035             (goto-char (point-min))
7036             (delete-matching-lines "^\\(Path\\):\\|^From ")
7037             (widen)
7038             (if (setq egroup
7039                       (gnus-group-read-ephemeral-group
7040                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7041                                      (nndoc-article-type guess))
7042                        t nil t))
7043                 (progn
7044                   ;; Make all postings to this group go to the parent group.
7045                   (nconc (gnus-info-params (gnus-get-info egroup))
7046                          params)
7047                   (push egroup groups))
7048               ;; Couldn't select this doc group.
7049               (gnus-error 3 "Article couldn't be entered"))))))
7050     ;; Now we have selected all the documents.
7051     (cond
7052      ((not groups)
7053       (error "None of the articles could be interpreted as documents"))
7054      ((gnus-group-read-ephemeral-group
7055        (setq vgroup (format
7056                      "nnvirtual:%s-%s" gnus-newsgroup-name
7057                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7058        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7059        t
7060        (cons (current-buffer) 'summary)))
7061      (t
7062       (error "Couldn't select virtual nndoc group")))))
7063
7064 (defun gnus-summary-isearch-article (&optional regexp-p)
7065   "Do incremental search forward on the current article.
7066 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7067   (interactive "P")
7068   (let* ((gnus-inhibit-treatment t)
7069          (old (gnus-summary-select-article)))
7070     (gnus-configure-windows 'article)
7071     (gnus-eval-in-buffer-window gnus-article-buffer
7072       (save-restriction
7073         (widen)
7074         (when (eq 'old old)
7075           (gnus-article-show-all-headers))
7076         (goto-char (point-min))
7077         (isearch-forward regexp-p)))))
7078
7079 (defun gnus-summary-search-article-forward (regexp &optional backward)
7080   "Search for an article containing REGEXP forward.
7081 If BACKWARD, search backward instead."
7082   (interactive
7083    (list (read-string
7084           (format "Search article %s (regexp%s): "
7085                   (if current-prefix-arg "backward" "forward")
7086                   (if gnus-last-search-regexp
7087                       (concat ", default " gnus-last-search-regexp)
7088                     "")))
7089          current-prefix-arg))
7090   (if (string-equal regexp "")
7091       (setq regexp (or gnus-last-search-regexp ""))
7092     (setq gnus-last-search-regexp regexp))
7093   (if (gnus-summary-search-article regexp backward)
7094       (gnus-summary-show-thread)
7095     (error "Search failed: \"%s\"" regexp)))
7096
7097 (defun gnus-summary-search-article-backward (regexp)
7098   "Search for an article containing REGEXP backward."
7099   (interactive
7100    (list (read-string
7101           (format "Search article backward (regexp%s): "
7102                   (if gnus-last-search-regexp
7103                       (concat ", default " gnus-last-search-regexp)
7104                     "")))))
7105   (gnus-summary-search-article-forward regexp 'backward))
7106
7107 (eval-when-compile
7108   (defmacro gnus-summary-search-article-position-point (regexp backward)
7109     "Dehighlight the last matched text and goto the beginning position."
7110     (` (if (and gnus-summary-search-article-matched-data
7111                 (let ((text (caddr gnus-summary-search-article-matched-data))
7112                       (inhibit-read-only t)
7113                       buffer-read-only)
7114                   (delete-region
7115                    (goto-char (car gnus-summary-search-article-matched-data))
7116                    (cadr gnus-summary-search-article-matched-data))
7117                   (insert text)
7118                   (string-match (, regexp) text)))
7119            (if (, backward) (beginning-of-line) (end-of-line))
7120          (goto-char (if (, backward) (point-max) (point-min))))))
7121
7122   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7123     "Place point where X-Face image is displayed."
7124     (if (featurep 'xemacs)
7125         (` (let ((end (if (search-forward "\n\n" nil t)
7126                           (goto-char (1- (point)))
7127                         (point-min)))
7128                  extent)
7129              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7130              (unless (and (re-search-forward "^From:" end t)
7131                           (setq extent (extent-at (point)))
7132                           (extent-begin-glyph extent))
7133                (goto-char (, opoint)))))
7134       (` (let ((end (if (search-forward "\n\n" nil t)
7135                         (goto-char (1- (point)))
7136                       (point-min))))
7137            (goto-char
7138             (or (text-property-any (or (search-backward "\n\n" nil t)
7139                                        (point-min))
7140                                    end 'x-face-mule-bitmap-image t)
7141                 (, opoint)))))))
7142
7143   (defmacro gnus-summary-search-article-highlight-matched-text
7144     (backward treated x-face)
7145     "Highlight matched text in the function `gnus-summary-search-article'."
7146     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7147              (end (set-marker (make-marker) (match-end 0)))
7148              (inhibit-read-only t)
7149              buffer-read-only)
7150          (unless treated
7151            (let ((,@
7152                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7153                     (mapcar
7154                      (lambda (item) (setq items (delq item items)))
7155                      '(gnus-treat-buttonize
7156                        gnus-treat-fill-article
7157                        gnus-treat-fill-long-lines
7158                        gnus-treat-emphasize
7159                        gnus-treat-highlight-headers
7160                        gnus-treat-highlight-citation
7161                        gnus-treat-highlight-signature
7162                        gnus-treat-overstrike
7163                        gnus-treat-display-xface
7164                        gnus-treat-buttonize-head
7165                        gnus-treat-decode-article-as-default-mime-charset))
7166                     (static-if (featurep 'xemacs)
7167                         items
7168                       (cons '(x-face-mule-delete-x-face-field
7169                               (quote never))
7170                             items))))
7171                  (gnus-treat-display-xface
7172                   (when (, x-face) gnus-treat-display-xface)))
7173              (gnus-article-prepare-mime-display)))
7174          (goto-char (if (, backward) start end))
7175          (when (, x-face)
7176            (gnus-summary-search-article-highlight-goto-x-face (point)))
7177          (setq gnus-summary-search-article-matched-data
7178                (list start end (buffer-substring start end)))
7179          (unless (eq start end);; matched text has been deleted. :-<
7180            (put-text-property start end 'face
7181                               (or (find-face 'isearch)
7182                                   'secondary-selection))))))
7183   )
7184
7185 (defun gnus-summary-search-article (regexp &optional backward)
7186   "Search for an article containing REGEXP.
7187 Optional argument BACKWARD means do search for backward.
7188 `gnus-select-article-hook' is not called during the search."
7189   ;; We have to require this here to make sure that the following
7190   ;; dynamic binding isn't shadowed by autoloading.
7191   (require 'gnus-async)
7192   (require 'gnus-art)
7193   (let ((gnus-select-article-hook nil)  ;Disable hook.
7194         (gnus-article-display-hook nil)
7195         (gnus-article-prepare-hook nil)
7196         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7197         (gnus-use-article-prefetch nil)
7198         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7199         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7200         (sum (current-buffer))
7201         (found nil)
7202         point treated)
7203     (gnus-save-hidden-threads
7204       (static-if (featurep 'xemacs)
7205           (let ((gnus-inhibit-treatment t))
7206             (setq treated (eq 'old (gnus-summary-select-article)))
7207             (when (and treated
7208                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7209                                  (window-live-p (get-buffer-window
7210                                                  gnus-article-buffer t)))))
7211               (gnus-summary-select-article nil t)
7212               (setq treated nil)))
7213         (let ((gnus-inhibit-treatment t)
7214               (x-face-mule-delete-x-face-field 'never))
7215           (setq treated (eq 'old (gnus-summary-select-article)))
7216           (when (and treated
7217                      (not
7218                       (and (gnus-buffer-live-p gnus-article-buffer)
7219                            (window-live-p (get-buffer-window
7220                                            gnus-article-buffer t))
7221                            (or (not (string-match "^\\^X-Face:" regexp))
7222                                (with-current-buffer gnus-article-buffer
7223                                  gnus-summary-search-article-matched-data)))))
7224             (gnus-summary-select-article nil t)
7225             (setq treated nil))))
7226       (set-buffer gnus-article-buffer)
7227       (widen)
7228       (if treated
7229           (progn
7230             (gnus-article-show-all-headers)
7231             (gnus-summary-search-article-position-point regexp backward))
7232         (goto-char (if backward (point-max) (point-min))))
7233       (while (not found)
7234         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7235         (if (if backward
7236                 (re-search-backward regexp nil t)
7237               (re-search-forward regexp nil t))
7238             ;; We found the regexp.
7239             (progn
7240               (gnus-summary-search-article-highlight-matched-text
7241                backward treated (string-match "^\\^X-Face:" regexp))
7242               (setq found 'found)
7243               (forward-line
7244                (/ (- 2 (window-height
7245                         (get-buffer-window gnus-article-buffer t)))
7246                   2))
7247               (set-window-start
7248                (get-buffer-window (current-buffer))
7249                (point))
7250               (set-buffer sum)
7251               (setq point (point)))
7252           ;; We didn't find it, so we go to the next article.
7253           (set-buffer sum)
7254           (setq found 'not)
7255           (while (eq found 'not)
7256             (if (not (if backward (gnus-summary-find-prev)
7257                        (gnus-summary-find-next)))
7258                 ;; No more articles.
7259                 (setq found t)
7260               ;; Select the next article and adjust point.
7261               (unless (gnus-summary-article-sparse-p
7262                        (gnus-summary-article-number))
7263                 (setq found nil)
7264                 (let ((gnus-inhibit-treatment t))
7265                   (gnus-summary-select-article))
7266                 (setq treated nil)
7267                 (set-buffer gnus-article-buffer)
7268                 (widen)
7269                 (goto-char (if backward (point-max) (point-min))))))))
7270       (gnus-message 7 ""))
7271     ;; Return whether we found the regexp.
7272     (when (eq found 'found)
7273       (goto-char point)
7274       (gnus-summary-show-thread)
7275       (gnus-summary-goto-subject gnus-current-article)
7276       (gnus-summary-position-point)
7277       t)))
7278
7279 (defun gnus-summary-find-matching (header regexp &optional backward unread
7280                                           not-case-fold)
7281   "Return a list of all articles that match REGEXP on HEADER.
7282 The search stars on the current article and goes forwards unless
7283 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7284 If UNREAD is non-nil, only unread articles will
7285 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7286 in the comparisons."
7287   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7288                 (gnus-data-find-list
7289                  (gnus-summary-article-number) (gnus-data-list backward))))
7290         (case-fold-search (not not-case-fold))
7291         articles d func)
7292     (if (consp header)
7293         (if (eq (car header) 'extra)
7294             (setq func
7295                   `(lambda (h)
7296                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7297                          "")))
7298           (error "%s is an invalid header" header))
7299       (unless (fboundp (intern (concat "mail-header-" header)))
7300         (error "%s is not a valid header" header))
7301       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7302     (while data
7303       (setq d (car data))
7304       (and (or (not unread)             ; We want all articles...
7305                (gnus-data-unread-p d))  ; Or just unreads.
7306            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7307            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7308            (push (gnus-data-number d) articles)) ; Success!
7309       (setq data (cdr data)))
7310     (nreverse articles)))
7311
7312 (defun gnus-summary-execute-command (header regexp command &optional backward)
7313   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7314 If HEADER is an empty string (or nil), the match is done on the entire
7315 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7316   (interactive
7317    (list (let ((completion-ignore-case t))
7318            (completing-read
7319             "Header name: "
7320             (mapcar (lambda (string) (list string))
7321                     '("Number" "Subject" "From" "Lines" "Date"
7322                       "Message-ID" "Xref" "References" "Body"))
7323             nil 'require-match))
7324          (read-string "Regexp: ")
7325          (read-key-sequence "Command: ")
7326          current-prefix-arg))
7327   (when (equal header "Body")
7328     (setq header ""))
7329   ;; Hidden thread subtrees must be searched as well.
7330   (gnus-summary-show-all-threads)
7331   ;; We don't want to change current point nor window configuration.
7332   (save-excursion
7333     (save-window-excursion
7334       (gnus-message 6 "Executing %s..." (key-description command))
7335       ;; We'd like to execute COMMAND interactively so as to give arguments.
7336       (gnus-execute header regexp
7337                     `(call-interactively ',(key-binding command))
7338                     backward)
7339       (gnus-message 6 "Executing %s...done" (key-description command)))))
7340
7341 (defun gnus-summary-beginning-of-article ()
7342   "Scroll the article back to the beginning."
7343   (interactive)
7344   (gnus-summary-select-article)
7345   (gnus-configure-windows 'article)
7346   (gnus-eval-in-buffer-window gnus-article-buffer
7347     (widen)
7348     (goto-char (point-min))
7349     (when gnus-page-broken
7350       (gnus-narrow-to-page))))
7351
7352 (defun gnus-summary-end-of-article ()
7353   "Scroll to the end of the article."
7354   (interactive)
7355   (gnus-summary-select-article)
7356   (gnus-configure-windows 'article)
7357   (gnus-eval-in-buffer-window gnus-article-buffer
7358     (widen)
7359     (goto-char (point-max))
7360     (recenter -3)
7361     (when gnus-page-broken
7362       (gnus-narrow-to-page))))
7363
7364 (defun gnus-summary-print-article (&optional filename n)
7365   "Generate and print a PostScript image of the N next (mail) articles.
7366
7367 If N is negative, print the N previous articles.  If N is nil and articles
7368 have been marked with the process mark, print these instead.
7369
7370 If the optional first argument FILENAME is nil, send the image to the
7371 printer.  If FILENAME is a string, save the PostScript image in a file with
7372 that name.  If FILENAME is a number, prompt the user for the name of the file
7373 to save in."
7374   (interactive (list (ps-print-preprint current-prefix-arg)
7375                      current-prefix-arg))
7376   (dolist (article (gnus-summary-work-articles n))
7377     (gnus-summary-select-article nil nil 'pseudo article)
7378     (gnus-eval-in-buffer-window gnus-article-buffer
7379       (let ((buffer (generate-new-buffer " *print*")))
7380         (unwind-protect
7381             (progn
7382               (copy-to-buffer buffer (point-min) (point-max))
7383               (set-buffer buffer)
7384               (gnus-article-delete-invisible-text)
7385               (let ((ps-left-header
7386                      (list
7387                       (concat "("
7388                               (mail-header-subject gnus-current-headers) ")")
7389                       (concat "("
7390                               (mail-header-from gnus-current-headers) ")")))
7391                     (ps-right-header
7392                      (list
7393                       "/pagenumberstring load"
7394                       (concat "("
7395                               (mail-header-date gnus-current-headers) ")"))))
7396                 (gnus-run-hooks 'gnus-ps-print-hook)
7397                 (save-excursion
7398                   (ps-print-buffer-with-faces filename))))
7399           (kill-buffer buffer))))))
7400
7401 (defun gnus-summary-show-article (&optional arg)
7402   "Force re-fetching of the current article.
7403 If ARG (the prefix) is a number, show the article with the charset
7404 defined in `gnus-summary-show-article-charset-alist', or the charset
7405 inputed.
7406 If ARG (the prefix) is non-nil and not a number, show the raw article
7407 without any article massaging functions being run."
7408   (interactive "P")
7409   (cond
7410    ((numberp arg)
7411     (let ((gnus-newsgroup-charset
7412            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7413                (read-coding-system "Charset: ")))
7414           (gnus-newsgroup-ignored-charsets 'gnus-all))
7415       (gnus-summary-select-article nil 'force)
7416       (let ((deps gnus-newsgroup-dependencies)
7417             head header)
7418         (save-excursion
7419           (set-buffer gnus-original-article-buffer)
7420           (save-restriction
7421             (message-narrow-to-head)
7422             (setq head (buffer-string)))
7423           (with-temp-buffer
7424             (insert (format "211 %d Article retrieved.\n"
7425                             (cdr gnus-article-current)))
7426             (insert head)
7427             (insert ".\n")
7428             (let ((nntp-server-buffer (current-buffer)))
7429               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7430         (gnus-data-set-header
7431          (gnus-data-find (cdr gnus-article-current))
7432          header)
7433         (gnus-summary-update-article-line
7434          (cdr gnus-article-current) header))))
7435    ((not arg)
7436     ;; Select the article the normal way.
7437     (gnus-summary-select-article nil 'force))
7438    (t
7439     ;; We have to require this here to make sure that the following
7440     ;; dynamic binding isn't shadowed by autoloading.
7441     (require 'gnus-async)
7442     (require 'gnus-art)
7443     ;; Bind the article treatment functions to nil.
7444     (let ((gnus-have-all-headers t)
7445           gnus-article-display-hook
7446           gnus-article-prepare-hook
7447           gnus-article-decode-hook
7448           gnus-break-pages
7449           gnus-show-mime)
7450       (gnus-summary-select-article nil 'force))))
7451   (gnus-summary-goto-subject gnus-current-article)
7452   (gnus-summary-position-point))
7453
7454 (defun gnus-summary-verbose-headers (&optional arg)
7455   "Toggle permanent full header display.
7456 If ARG is a positive number, turn header display on.
7457 If ARG is a negative number, turn header display off."
7458   (interactive "P")
7459   (setq gnus-show-all-headers
7460         (cond ((or (not (numberp arg))
7461                    (zerop arg))
7462                (not gnus-show-all-headers))
7463               ((natnump arg)
7464                t)))
7465   (gnus-summary-show-article))
7466
7467 (defun gnus-summary-toggle-header (&optional arg)
7468   "Show the headers if they are hidden, or hide them if they are shown.
7469 If ARG is a positive number, show the entire header.
7470 If ARG is a negative number, hide the unwanted header lines."
7471   (interactive "P")
7472   (save-excursion
7473     (set-buffer gnus-article-buffer)
7474     (save-restriction
7475       (let* ((buffer-read-only nil)
7476              (inhibit-point-motion-hooks t)
7477              hidden e)
7478         (setq hidden
7479               (if (numberp arg)
7480                   (>= arg 0)
7481                 (save-restriction
7482                   (article-narrow-to-head)
7483                   (gnus-article-hidden-text-p 'headers))))
7484         (goto-char (point-min))
7485         (when (search-forward "\n\n" nil t)
7486           (delete-region (point-min) (1- (point))))
7487         (goto-char (point-min))
7488         (save-excursion
7489           (set-buffer gnus-original-article-buffer)
7490           (goto-char (point-min))
7491           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7492         (insert-buffer-substring gnus-original-article-buffer 1 e)
7493         (save-restriction
7494           (narrow-to-region (point-min) (point))
7495           (article-decode-encoded-words)
7496           (if  hidden
7497               (let ((gnus-treat-hide-headers nil)
7498                     (gnus-treat-hide-boring-headers nil))
7499                 (setq gnus-article-wash-types
7500                       (delq 'headers gnus-article-wash-types))
7501                 (gnus-treat-article 'head))
7502             (gnus-treat-article 'head)))
7503         (gnus-set-mode-line 'article)))))
7504
7505 (defun gnus-summary-show-all-headers ()
7506   "Make all header lines visible."
7507   (interactive)
7508   (gnus-article-show-all-headers))
7509
7510 (defun gnus-summary-toggle-mime (&optional arg)
7511   "Toggle MIME processing.
7512 If ARG is a positive number, turn MIME processing on."
7513   (interactive "P")
7514   (setq gnus-show-mime
7515         (if (null arg)
7516             (not gnus-show-mime)
7517           (> (prefix-numeric-value arg) 0)))
7518   (gnus-summary-select-article t 'force))
7519
7520 (defun gnus-summary-caesar-message (&optional arg)
7521   "Caesar rotate the current article by 13.
7522 The numerical prefix specifies how many places to rotate each letter
7523 forward."
7524   (interactive "P")
7525   (gnus-summary-select-article)
7526   (let ((mail-header-separator ""))
7527     (gnus-eval-in-buffer-window gnus-article-buffer
7528       (save-restriction
7529         (widen)
7530         (let ((start (window-start))
7531               buffer-read-only)
7532           (message-caesar-buffer-body arg)
7533           (set-window-start (get-buffer-window (current-buffer)) start))))))
7534
7535 (defun gnus-summary-stop-page-breaking ()
7536   "Stop page breaking in the current article."
7537   (interactive)
7538   (gnus-summary-select-article)
7539   (gnus-eval-in-buffer-window gnus-article-buffer
7540     (widen)
7541     (when (gnus-visual-p 'page-marker)
7542       (let ((buffer-read-only nil))
7543         (gnus-remove-text-with-property 'gnus-prev)
7544         (gnus-remove-text-with-property 'gnus-next))
7545       (setq gnus-page-broken nil))))
7546
7547 (defun gnus-summary-move-article (&optional n to-newsgroup
7548                                             select-method action)
7549   "Move the current article to a different newsgroup.
7550 If N is a positive number, move the N next articles.
7551 If N is a negative number, move the N previous articles.
7552 If N is nil and any articles have been marked with the process mark,
7553 move those articles instead.
7554 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7555 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7556 re-spool using this method.
7557
7558 For this function to work, both the current newsgroup and the
7559 newsgroup that you want to move to have to support the `request-move'
7560 and `request-accept' functions.
7561
7562 ACTION can be either `move' (the default), `crosspost' or `copy'."
7563   (interactive "P")
7564   (unless action
7565     (setq action 'move))
7566   ;; Disable marking as read.
7567   (let (gnus-mark-article-hook)
7568     (save-window-excursion
7569       (gnus-summary-select-article)))
7570   ;; Check whether the source group supports the required functions.
7571   (cond ((and (eq action 'move)
7572               (not (gnus-check-backend-function
7573                     'request-move-article gnus-newsgroup-name)))
7574          (error "The current group does not support article moving"))
7575         ((and (eq action 'crosspost)
7576               (not (gnus-check-backend-function
7577                     'request-replace-article gnus-newsgroup-name)))
7578          (error "The current group does not support article editing")))
7579   (let ((articles (gnus-summary-work-articles n))
7580         (prefix (if (gnus-check-backend-function
7581                     'request-move-article gnus-newsgroup-name)
7582                     (gnus-group-real-prefix gnus-newsgroup-name)
7583                   ""))
7584         (names '((move "Move" "Moving")
7585                  (copy "Copy" "Copying")
7586                  (crosspost "Crosspost" "Crossposting")))
7587         (copy-buf (save-excursion
7588                     (nnheader-set-temp-buffer " *copy article*")))
7589         (default-marks gnus-article-mark-lists)
7590         (no-expire-marks (delete '(expirable . expire)
7591                                  (copy-sequence gnus-article-mark-lists)))
7592         art-group to-method new-xref article to-groups)
7593     (unless (assq action names)
7594       (error "Unknown action %s" action))
7595     ;; Read the newsgroup name.
7596     (when (and (not to-newsgroup)
7597                (not select-method))
7598       (setq to-newsgroup
7599             (gnus-read-move-group-name
7600              (cadr (assq action names))
7601              (symbol-value (intern (format "gnus-current-%s-group" action)))
7602              articles prefix))
7603       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7604     (setq to-method (or select-method
7605                         (gnus-server-to-method
7606                          (gnus-group-method to-newsgroup))))
7607     ;; Check the method we are to move this article to...
7608     (unless (gnus-check-backend-function
7609              'request-accept-article (car to-method))
7610       (error "%s does not support article copying" (car to-method)))
7611     (unless (gnus-check-server to-method)
7612       (error "Can't open server %s" (car to-method)))
7613     (gnus-message 6 "%s to %s: %s..."
7614                   (caddr (assq action names))
7615                   (or (car select-method) to-newsgroup) articles)
7616     (while articles
7617       (setq article (pop articles))
7618       (setq
7619        art-group
7620        (cond
7621         ;; Move the article.
7622         ((eq action 'move)
7623          ;; Remove this article from future suppression.
7624          (gnus-dup-unsuppress-article article)
7625          (gnus-request-move-article
7626           article                       ; Article to move
7627           gnus-newsgroup-name           ; From newsgroup
7628           (nth 1 (gnus-find-method-for-group
7629                   gnus-newsgroup-name)) ; Server
7630           (list 'gnus-request-accept-article
7631                 to-newsgroup (list 'quote select-method)
7632                 (not articles) t)       ; Accept form
7633           (not articles)))              ; Only save nov last time
7634         ;; Copy the article.
7635         ((eq action 'copy)
7636          (save-excursion
7637            (set-buffer copy-buf)
7638            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7639              (gnus-request-accept-article
7640               to-newsgroup select-method (not articles) t))))
7641         ;; Crosspost the article.
7642         ((eq action 'crosspost)
7643          (let ((xref (message-tokenize-header
7644                       (mail-header-xref (gnus-summary-article-header article))
7645                       " ")))
7646            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7647                                   ":" article))
7648            (unless xref
7649              (setq xref (list (system-name))))
7650            (setq new-xref
7651                  (concat
7652                   (mapconcat 'identity
7653                              (delete "Xref:" (delete new-xref xref))
7654                              " ")
7655                   " " new-xref))
7656            (save-excursion
7657              (set-buffer copy-buf)
7658              ;; First put the article in the destination group.
7659              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7660              (when (consp (setq art-group
7661                                 (gnus-request-accept-article
7662                                  to-newsgroup select-method (not articles))))
7663                (setq new-xref (concat new-xref " " (car art-group)
7664                                       ":" (cdr art-group)))
7665                ;; Now we have the new Xrefs header, so we insert
7666                ;; it and replace the new article.
7667                (nnheader-replace-header "Xref" new-xref)
7668                (gnus-request-replace-article
7669                 (cdr art-group) to-newsgroup (current-buffer))
7670                art-group))))))
7671       (cond
7672        ((not art-group)
7673         (gnus-message 1 "Couldn't %s article %s: %s"
7674                       (cadr (assq action names)) article
7675                       (nnheader-get-report (car to-method))))
7676        ((eq art-group 'junk)
7677         (when (eq action 'move)
7678           (gnus-summary-mark-article article gnus-canceled-mark)
7679           (gnus-message 4 "Deleted article %s" article)))
7680        (t
7681         (let* ((pto-group (gnus-group-prefixed-name
7682                            (car art-group) to-method))
7683                (entry
7684                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7685                (info (nth 2 entry))
7686                (to-group (gnus-info-group info))
7687                to-marks)
7688           ;; Update the group that has been moved to.
7689           (when (and info
7690                      (memq action '(move copy)))
7691             (unless (member to-group to-groups)
7692               (push to-group to-groups))
7693
7694             (unless (memq article gnus-newsgroup-unreads)
7695               (push 'read to-marks)
7696               (gnus-info-set-read
7697                info (gnus-add-to-range (gnus-info-read info)
7698                                        (list (cdr art-group)))))
7699
7700             ;; See whether the article is to be put in the cache.
7701             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7702                              default-marks
7703                            no-expire-marks))
7704                   (to-article (cdr art-group)))
7705
7706               ;; Enter the article into the cache in the new group,
7707               ;; if that is required.
7708               (when gnus-use-cache
7709                 (gnus-cache-possibly-enter-article
7710                  to-group to-article
7711                  (let ((header (copy-sequence
7712                                 (gnus-summary-article-header article))))
7713                    (mail-header-set-number header to-article)
7714                    header)
7715                  (memq article gnus-newsgroup-marked)
7716                  (memq article gnus-newsgroup-dormant)
7717                  (memq article gnus-newsgroup-unreads)))
7718
7719               (when gnus-preserve-marks
7720                 ;; Copy any marks over to the new group.
7721                 (when (and (equal to-group gnus-newsgroup-name)
7722                            (not (memq article gnus-newsgroup-unreads)))
7723                   ;; Mark this article as read in this group.
7724                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7725                   (setcdr (gnus-active to-group) to-article)
7726                   (setcdr gnus-newsgroup-active to-article))
7727
7728                 (while marks
7729                   (when (memq article (symbol-value
7730                                        (intern (format "gnus-newsgroup-%s"
7731                                                        (caar marks)))))
7732                     (push (cdar marks) to-marks)
7733                     ;; If the other group is the same as this group,
7734                     ;; then we have to add the mark to the list.
7735                     (when (equal to-group gnus-newsgroup-name)
7736                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7737                            (cons to-article
7738                                  (symbol-value
7739                                   (intern (format "gnus-newsgroup-%s"
7740                                                   (caar marks)))))))
7741                     ;; Copy the marks to other group.
7742                     (gnus-add-marked-articles
7743                      to-group (cdar marks) (list to-article) info))
7744                   (setq marks (cdr marks)))
7745
7746                 (gnus-request-set-mark to-group (list (list (list to-article)
7747                                                             'set
7748                                                             to-marks))))
7749
7750               (gnus-dribble-enter
7751                (concat "(gnus-group-set-info '"
7752                        (gnus-prin1-to-string (gnus-get-info to-group))
7753                        ")"))))
7754
7755           ;; Update the Xref header in this article to point to
7756           ;; the new crossposted article we have just created.
7757           (when (eq action 'crosspost)
7758             (save-excursion
7759               (set-buffer copy-buf)
7760               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7761               (nnheader-replace-header "Xref" new-xref)
7762               (gnus-request-replace-article
7763                article gnus-newsgroup-name (current-buffer)))))
7764
7765         ;;;!!!Why is this necessary?
7766         (set-buffer gnus-summary-buffer)
7767
7768         (gnus-summary-goto-subject article)
7769         (when (eq action 'move)
7770           (gnus-summary-mark-article article gnus-canceled-mark))))
7771       (gnus-summary-remove-process-mark article))
7772     ;; Re-activate all groups that have been moved to.
7773     (while to-groups
7774       (save-excursion
7775         (set-buffer gnus-group-buffer)
7776         (when (gnus-group-goto-group (car to-groups) t)
7777           (gnus-group-get-new-news-this-group 1 t))
7778         (pop to-groups)))
7779
7780     (gnus-kill-buffer copy-buf)
7781     (gnus-summary-position-point)
7782     (gnus-set-mode-line 'summary)))
7783
7784 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7785   "Move the current article to a different newsgroup.
7786 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7787 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7788 re-spool using this method."
7789   (interactive "P")
7790   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7791
7792 (defun gnus-summary-crosspost-article (&optional n)
7793   "Crosspost the current article to some other group."
7794   (interactive "P")
7795   (gnus-summary-move-article n nil nil 'crosspost))
7796
7797 (defcustom gnus-summary-respool-default-method nil
7798   "Default method for respooling an article.
7799 If nil, use to the current newsgroup method."
7800   :type '(choice (gnus-select-method :value (nnml ""))
7801                  (const nil))
7802   :group 'gnus-summary-mail)
7803
7804 (defun gnus-summary-respool-article (&optional n method)
7805   "Respool the current article.
7806 The article will be squeezed through the mail spooling process again,
7807 which means that it will be put in some mail newsgroup or other
7808 depending on `nnmail-split-methods'.
7809 If N is a positive number, respool the N next articles.
7810 If N is a negative number, respool the N previous articles.
7811 If N is nil and any articles have been marked with the process mark,
7812 respool those articles instead.
7813
7814 Respooling can be done both from mail groups and \"real\" newsgroups.
7815 In the former case, the articles in question will be moved from the
7816 current group into whatever groups they are destined to.  In the
7817 latter case, they will be copied into the relevant groups."
7818   (interactive
7819    (list current-prefix-arg
7820          (let* ((methods (gnus-methods-using 'respool))
7821                 (methname
7822                  (symbol-name (or gnus-summary-respool-default-method
7823                                   (car (gnus-find-method-for-group
7824                                         gnus-newsgroup-name)))))
7825                 (method
7826                  (gnus-completing-read
7827                   methname "What backend do you want to use when respooling?"
7828                   methods nil t nil 'gnus-mail-method-history))
7829                 ms)
7830            (cond
7831             ((zerop (length (setq ms (gnus-servers-using-backend
7832                                       (intern method)))))
7833              (list (intern method) ""))
7834             ((= 1 (length ms))
7835              (car ms))
7836             (t
7837              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7838                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7839                            ms-alist))))))))
7840   (unless method
7841     (error "No method given for respooling"))
7842   (if (assoc (symbol-name
7843               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7844              (gnus-methods-using 'respool))
7845       (gnus-summary-move-article n nil method)
7846     (gnus-summary-copy-article n nil method)))
7847
7848 (defun gnus-summary-import-article (file)
7849   "Import an arbitrary file into a mail newsgroup."
7850   (interactive "fImport file: ")
7851   (let ((group gnus-newsgroup-name)
7852         (now (current-time))
7853         atts lines)
7854     (unless (gnus-check-backend-function 'request-accept-article group)
7855       (error "%s does not support article importing" group))
7856     (or (file-readable-p file)
7857         (not (file-regular-p file))
7858         (error "Can't read %s" file))
7859     (save-excursion
7860       (set-buffer (gnus-get-buffer-create " *import file*"))
7861       (erase-buffer)
7862       (nnheader-insert-file-contents file)
7863       (goto-char (point-min))
7864       (unless (nnheader-article-p)
7865         ;; This doesn't look like an article, so we fudge some headers.
7866         (setq atts (file-attributes file)
7867               lines (count-lines (point-min) (point-max)))
7868         (insert "From: " (read-string "From: ") "\n"
7869                 "Subject: " (read-string "Subject: ") "\n"
7870                 "Date: " (message-make-date (nth 5 atts))
7871                 "\n"
7872                 "Message-ID: " (message-make-message-id) "\n"
7873                 "Lines: " (int-to-string lines) "\n"
7874                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7875       (gnus-request-accept-article group nil t)
7876       (kill-buffer (current-buffer)))))
7877
7878 (defun gnus-summary-article-posted-p ()
7879   "Say whether the current (mail) article is available from news as well.
7880 This will be the case if the article has both been mailed and posted."
7881   (interactive)
7882   (let ((id (mail-header-references (gnus-summary-article-header)))
7883         (gnus-override-method (car (gnus-refer-article-methods))))
7884     (if (gnus-request-head id "")
7885         (gnus-message 2 "The current message was found on %s"
7886                       gnus-override-method)
7887       (gnus-message 2 "The current message couldn't be found on %s"
7888                     gnus-override-method)
7889       nil)))
7890
7891 (defun gnus-summary-expire-articles (&optional now)
7892   "Expire all articles that are marked as expirable in the current group."
7893   (interactive)
7894   (when (gnus-check-backend-function
7895          'request-expire-articles gnus-newsgroup-name)
7896     ;; This backend supports expiry.
7897     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7898            (expirable (if total
7899                           (progn
7900                             ;; We need to update the info for
7901                             ;; this group for `gnus-list-of-read-articles'
7902                             ;; to give us the right answer.
7903                             (gnus-run-hooks 'gnus-exit-group-hook)
7904                             (gnus-summary-update-info)
7905                             (gnus-list-of-read-articles gnus-newsgroup-name))
7906                         (setq gnus-newsgroup-expirable
7907                               (sort gnus-newsgroup-expirable '<))))
7908            (expiry-wait (if now 'immediate
7909                           (gnus-group-find-parameter
7910                            gnus-newsgroup-name 'expiry-wait)))
7911            (nnmail-expiry-target
7912             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7913                 nnmail-expiry-target))
7914            es)
7915       (when expirable
7916         ;; There are expirable articles in this group, so we run them
7917         ;; through the expiry process.
7918         (gnus-message 6 "Expiring articles...")
7919         (unless (gnus-check-group gnus-newsgroup-name)
7920           (error "Can't open server for %s" gnus-newsgroup-name))
7921         ;; The list of articles that weren't expired is returned.
7922         (save-excursion
7923           (if expiry-wait
7924               (let ((nnmail-expiry-wait-function nil)
7925                     (nnmail-expiry-wait expiry-wait))
7926                 (setq es (gnus-request-expire-articles
7927                           expirable gnus-newsgroup-name)))
7928             (setq es (gnus-request-expire-articles
7929                       expirable gnus-newsgroup-name)))
7930           (unless total
7931             (setq gnus-newsgroup-expirable es))
7932           ;; We go through the old list of expirable, and mark all
7933           ;; really expired articles as nonexistent.
7934           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7935             (let ((gnus-use-cache nil))
7936               (while expirable
7937                 (unless (memq (car expirable) es)
7938                   (when (gnus-data-find (car expirable))
7939                     (gnus-summary-mark-article
7940                      (car expirable) gnus-canceled-mark)))
7941                 (setq expirable (cdr expirable))))))
7942         (gnus-message 6 "Expiring articles...done")))))
7943
7944 (defun gnus-summary-expire-articles-now ()
7945   "Expunge all expirable articles in the current group.
7946 This means that *all* articles that are marked as expirable will be
7947 deleted forever, right now."
7948   (interactive)
7949   (or gnus-expert-user
7950       (gnus-yes-or-no-p
7951        "Are you really, really, really sure you want to delete all these messages? ")
7952       (error "Phew!"))
7953   (gnus-summary-expire-articles t))
7954
7955 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7956 (defun gnus-summary-delete-article (&optional n)
7957   "Delete the N next (mail) articles.
7958 This command actually deletes articles.  This is not a marking
7959 command.  The article will disappear forever from your life, never to
7960 return.
7961 If N is negative, delete backwards.
7962 If N is nil and articles have been marked with the process mark,
7963 delete these instead."
7964   (interactive "P")
7965   (unless (gnus-check-backend-function 'request-expire-articles
7966                                        gnus-newsgroup-name)
7967     (error "The current newsgroup does not support article deletion"))
7968   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7969     (error "Couldn't open server"))
7970   ;; Compute the list of articles to delete.
7971   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7972         not-deleted)
7973     (if (and gnus-novice-user
7974              (not (gnus-yes-or-no-p
7975                    (format "Do you really want to delete %s forever? "
7976                            (if (> (length articles) 1)
7977                                (format "these %s articles" (length articles))
7978                              "this article")))))
7979         ()
7980       ;; Delete the articles.
7981       (setq not-deleted (gnus-request-expire-articles
7982                          articles gnus-newsgroup-name 'force))
7983       (while articles
7984         (gnus-summary-remove-process-mark (car articles))
7985         ;; The backend might not have been able to delete the article
7986         ;; after all.
7987         (unless (memq (car articles) not-deleted)
7988           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7989         (setq articles (cdr articles)))
7990       (when not-deleted
7991         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7992     (gnus-summary-position-point)
7993     (gnus-set-mode-line 'summary)
7994     not-deleted))
7995
7996 (defun gnus-summary-edit-article (&optional force)
7997   "Edit the current article.
7998 This will have permanent effect only in mail groups.
7999 If FORCE is non-nil, allow editing of articles even in read-only
8000 groups."
8001   (interactive "P")
8002   (save-excursion
8003     (set-buffer gnus-summary-buffer)
8004     (let ((mail-parse-charset gnus-newsgroup-charset)
8005           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8006       (gnus-set-global-variables)
8007       (when (and (not force)
8008                  (gnus-group-read-only-p))
8009         (error "The current newsgroup does not support article editing"))
8010       (gnus-summary-show-article t)
8011       (gnus-article-edit-article
8012        'ignore
8013        `(lambda (no-highlight)
8014           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8015                 (message-options message-options)
8016                 (message-options-set-recipient)
8017                 (mail-parse-ignored-charsets
8018                  ',gnus-newsgroup-ignored-charsets))
8019             (gnus-summary-edit-article-done
8020              ,(or (mail-header-references gnus-current-headers) "")
8021              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8022
8023 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8024
8025 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8026                                                  no-highlight)
8027   "Make edits to the current article permanent."
8028   (interactive)
8029   ;; Replace the article.
8030   (let ((buf (current-buffer)))
8031     (with-temp-buffer
8032       (insert-buffer-substring buf)
8033       (if (and (not read-only)
8034                (not (gnus-request-replace-article
8035                      (cdr gnus-article-current) (car gnus-article-current)
8036                      (current-buffer) t)))
8037           (error "Couldn't replace article")
8038         ;; Update the summary buffer.
8039         (if (and references
8040                  (equal (message-tokenize-header references " ")
8041                         (message-tokenize-header
8042                          (or (message-fetch-field "references") "") " ")))
8043             ;; We only have to update this line.
8044             (save-excursion
8045               (save-restriction
8046                 (message-narrow-to-head)
8047                 (let ((head (buffer-string))
8048                       header)
8049                   (with-temp-buffer
8050                     (insert (format "211 %d Article retrieved.\n"
8051                                     (cdr gnus-article-current)))
8052                     (insert head)
8053                     (insert ".\n")
8054                     (let ((nntp-server-buffer (current-buffer)))
8055                       (setq header (car (gnus-get-newsgroup-headers
8056                                          (save-excursion
8057                                            (set-buffer gnus-summary-buffer)
8058                                            gnus-newsgroup-dependencies)
8059                                          t))))
8060                     (save-excursion
8061                       (set-buffer gnus-summary-buffer)
8062                       (gnus-data-set-header
8063                        (gnus-data-find (cdr gnus-article-current))
8064                        header)
8065                       (gnus-summary-update-article-line
8066                        (cdr gnus-article-current) header))))))
8067           ;; Update threads.
8068           (set-buffer (or buffer gnus-summary-buffer))
8069           (gnus-summary-update-article (cdr gnus-article-current)))
8070         ;; Prettify the article buffer again.
8071         (unless no-highlight
8072           (save-excursion
8073             (set-buffer gnus-article-buffer)
8074             ;;;!!! Fix this -- article should be rehighlighted.
8075             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8076             (set-buffer gnus-original-article-buffer)
8077             (gnus-request-article
8078              (cdr gnus-article-current)
8079              (car gnus-article-current) (current-buffer))))
8080         ;; Prettify the summary buffer line.
8081         (when (gnus-visual-p 'summary-highlight 'highlight)
8082           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8083
8084 (defun gnus-summary-edit-wash (key)
8085   "Perform editing command KEY in the article buffer."
8086   (interactive
8087    (list
8088     (progn
8089       (message "%s" (concat (this-command-keys) "- "))
8090       (read-char))))
8091   (message "")
8092   (gnus-summary-edit-article)
8093   (execute-kbd-macro (concat (this-command-keys) key))
8094   (gnus-article-edit-done))
8095
8096 ;;; Respooling
8097
8098 (defun gnus-summary-respool-query (&optional silent trace)
8099   "Query where the respool algorithm would put this article."
8100   (interactive)
8101   (let (gnus-mark-article-hook)
8102     (gnus-summary-select-article)
8103     (save-excursion
8104       (set-buffer gnus-original-article-buffer)
8105       (save-restriction
8106         (message-narrow-to-head)
8107         (let ((groups (nnmail-article-group 'identity trace)))
8108           (unless silent
8109             (if groups
8110                 (message "This message would go to %s"
8111                          (mapconcat 'car groups ", "))
8112               (message "This message would go to no groups"))
8113             groups))))))
8114
8115 (defun gnus-summary-respool-trace ()
8116   "Trace where the respool algorithm would put this article.
8117 Display a buffer showing all fancy splitting patterns which matched."
8118   (interactive)
8119   (gnus-summary-respool-query nil t))
8120
8121 ;; Summary marking commands.
8122
8123 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8124   "Mark articles which has the same subject as read, and then select the next.
8125 If UNMARK is positive, remove any kind of mark.
8126 If UNMARK is negative, tick articles."
8127   (interactive "P")
8128   (when unmark
8129     (setq unmark (prefix-numeric-value unmark)))
8130   (let ((count
8131          (gnus-summary-mark-same-subject
8132           (gnus-summary-article-subject) unmark)))
8133     ;; Select next unread article.  If auto-select-same mode, should
8134     ;; select the first unread article.
8135     (gnus-summary-next-article t (and gnus-auto-select-same
8136                                       (gnus-summary-article-subject)))
8137     (gnus-message 7 "%d article%s marked as %s"
8138                   count (if (= count 1) " is" "s are")
8139                   (if unmark "unread" "read"))))
8140
8141 (defun gnus-summary-kill-same-subject (&optional unmark)
8142   "Mark articles which has the same subject as read.
8143 If UNMARK is positive, remove any kind of mark.
8144 If UNMARK is negative, tick articles."
8145   (interactive "P")
8146   (when unmark
8147     (setq unmark (prefix-numeric-value unmark)))
8148   (let ((count
8149          (gnus-summary-mark-same-subject
8150           (gnus-summary-article-subject) unmark)))
8151     ;; If marked as read, go to next unread subject.
8152     (when (null unmark)
8153       ;; Go to next unread subject.
8154       (gnus-summary-next-subject 1 t))
8155     (gnus-message 7 "%d articles are marked as %s"
8156                   count (if unmark "unread" "read"))))
8157
8158 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8159   "Mark articles with same SUBJECT as read, and return marked number.
8160 If optional argument UNMARK is positive, remove any kinds of marks.
8161 If optional argument UNMARK is negative, mark articles as unread instead."
8162   (let ((count 1))
8163     (save-excursion
8164       (cond
8165        ((null unmark)                   ; Mark as read.
8166         (while (and
8167                 (progn
8168                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8169                   (gnus-summary-show-thread) t)
8170                 (gnus-summary-find-subject subject))
8171           (setq count (1+ count))))
8172        ((> unmark 0)                    ; Tick.
8173         (while (and
8174                 (progn
8175                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8176                   (gnus-summary-show-thread) t)
8177                 (gnus-summary-find-subject subject))
8178           (setq count (1+ count))))
8179        (t                               ; Mark as unread.
8180         (while (and
8181                 (progn
8182                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8183                   (gnus-summary-show-thread) t)
8184                 (gnus-summary-find-subject subject))
8185           (setq count (1+ count)))))
8186       (gnus-set-mode-line 'summary)
8187       ;; Return the number of marked articles.
8188       count)))
8189
8190 (defun gnus-summary-mark-as-processable (n &optional unmark)
8191   "Set the process mark on the next N articles.
8192 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8193 the process mark instead.  The difference between N and the actual
8194 number of articles marked is returned."
8195   (interactive "p")
8196   (let ((backward (< n 0))
8197         (n (abs n)))
8198     (while (and
8199             (> n 0)
8200             (if unmark
8201                 (gnus-summary-remove-process-mark
8202                  (gnus-summary-article-number))
8203               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8204             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8205       (setq n (1- n)))
8206     (when (/= 0 n)
8207       (gnus-message 7 "No more articles"))
8208     (gnus-summary-recenter)
8209     (gnus-summary-position-point)
8210     n))
8211
8212 (defun gnus-summary-unmark-as-processable (n)
8213   "Remove the process mark from the next N articles.
8214 If N is negative, unmark backward instead.  The difference between N and
8215 the actual number of articles unmarked is returned."
8216   (interactive "p")
8217   (gnus-summary-mark-as-processable n t))
8218
8219 (defun gnus-summary-unmark-all-processable ()
8220   "Remove the process mark from all articles."
8221   (interactive)
8222   (save-excursion
8223     (while gnus-newsgroup-processable
8224       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8225   (gnus-summary-position-point))
8226
8227 (defun gnus-summary-mark-as-expirable (n)
8228   "Mark N articles forward as expirable.
8229 If N is negative, mark backward instead.  The difference between N and
8230 the actual number of articles marked is returned."
8231   (interactive "p")
8232   (gnus-summary-mark-forward n gnus-expirable-mark))
8233
8234 (defun gnus-summary-mark-article-as-replied (article)
8235   "Mark ARTICLE replied and update the summary line."
8236   (push article gnus-newsgroup-replied)
8237   (let ((buffer-read-only nil))
8238     (when (gnus-summary-goto-subject article nil t)
8239       (gnus-summary-update-secondary-mark article))))
8240
8241 (defun gnus-summary-set-bookmark (article)
8242   "Set a bookmark in current article."
8243   (interactive (list (gnus-summary-article-number)))
8244   (when (or (not (get-buffer gnus-article-buffer))
8245             (not gnus-current-article)
8246             (not gnus-article-current)
8247             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8248     (error "No current article selected"))
8249   ;; Remove old bookmark, if one exists.
8250   (let ((old (assq article gnus-newsgroup-bookmarks)))
8251     (when old
8252       (setq gnus-newsgroup-bookmarks
8253             (delq old gnus-newsgroup-bookmarks))))
8254   ;; Set the new bookmark, which is on the form
8255   ;; (article-number . line-number-in-body).
8256   (push
8257    (cons article
8258          (save-excursion
8259            (set-buffer gnus-article-buffer)
8260            (count-lines
8261             (min (point)
8262                  (save-excursion
8263                    (goto-char (point-min))
8264                    (search-forward "\n\n" nil t)
8265                    (point)))
8266             (point))))
8267    gnus-newsgroup-bookmarks)
8268   (gnus-message 6 "A bookmark has been added to the current article."))
8269
8270 (defun gnus-summary-remove-bookmark (article)
8271   "Remove the bookmark from the current article."
8272   (interactive (list (gnus-summary-article-number)))
8273   ;; Remove old bookmark, if one exists.
8274   (let ((old (assq article gnus-newsgroup-bookmarks)))
8275     (if old
8276         (progn
8277           (setq gnus-newsgroup-bookmarks
8278                 (delq old gnus-newsgroup-bookmarks))
8279           (gnus-message 6 "Removed bookmark."))
8280       (gnus-message 6 "No bookmark in current article."))))
8281
8282 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8283 (defun gnus-summary-mark-as-dormant (n)
8284   "Mark N articles forward as dormant.
8285 If N is negative, mark backward instead.  The difference between N and
8286 the actual number of articles marked is returned."
8287   (interactive "p")
8288   (gnus-summary-mark-forward n gnus-dormant-mark))
8289
8290 (defun gnus-summary-set-process-mark (article)
8291   "Set the process mark on ARTICLE and update the summary line."
8292   (setq gnus-newsgroup-processable
8293         (cons article
8294               (delq article gnus-newsgroup-processable)))
8295   (when (gnus-summary-goto-subject article)
8296     (gnus-summary-show-thread)
8297     (gnus-summary-goto-subject article)
8298     (gnus-summary-update-secondary-mark article)))
8299
8300 (defun gnus-summary-remove-process-mark (article)
8301   "Remove the process mark from ARTICLE and update the summary line."
8302   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8303   (when (gnus-summary-goto-subject article)
8304     (gnus-summary-show-thread)
8305     (gnus-summary-goto-subject article)
8306     (gnus-summary-update-secondary-mark article)))
8307
8308 (defun gnus-summary-set-saved-mark (article)
8309   "Set the process mark on ARTICLE and update the summary line."
8310   (push article gnus-newsgroup-saved)
8311   (when (gnus-summary-goto-subject article)
8312     (gnus-summary-update-secondary-mark article)))
8313
8314 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8315   "Mark N articles as read forwards.
8316 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8317 The difference between N and the actual number of articles marked is
8318 returned.
8319 Iff NO-EXPIRE, auto-expiry will be inhibited."
8320   (interactive "p")
8321   (gnus-summary-show-thread)
8322   (let ((backward (< n 0))
8323         (gnus-summary-goto-unread
8324          (and gnus-summary-goto-unread
8325               (not (eq gnus-summary-goto-unread 'never))
8326               (not (memq mark (list gnus-unread-mark
8327                                     gnus-ticked-mark gnus-dormant-mark)))))
8328         (n (abs n))
8329         (mark (or mark gnus-del-mark)))
8330     (while (and (> n 0)
8331                 (gnus-summary-mark-article nil mark no-expire)
8332                 (zerop (gnus-summary-next-subject
8333                         (if backward -1 1)
8334                         (and gnus-summary-goto-unread
8335                              (not (eq gnus-summary-goto-unread 'never)))
8336                         t)))
8337       (setq n (1- n)))
8338     (when (/= 0 n)
8339       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8340     (gnus-summary-recenter)
8341     (gnus-summary-position-point)
8342     (gnus-set-mode-line 'summary)
8343     n))
8344
8345 (defun gnus-summary-mark-article-as-read (mark)
8346   "Mark the current article quickly as read with MARK."
8347   (let ((article (gnus-summary-article-number)))
8348     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8349     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8350     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8351     (push (cons article mark) gnus-newsgroup-reads)
8352     ;; Possibly remove from cache, if that is used.
8353     (when gnus-use-cache
8354       (gnus-cache-enter-remove-article article))
8355     ;; Allow the backend to change the mark.
8356     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8357     ;; Check for auto-expiry.
8358     (when (and gnus-newsgroup-auto-expire
8359                (memq mark gnus-auto-expirable-marks))
8360       (setq mark gnus-expirable-mark)
8361       ;; Let the backend know about the mark change.
8362       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8363       (push article gnus-newsgroup-expirable))
8364     ;; Set the mark in the buffer.
8365     (gnus-summary-update-mark mark 'unread)
8366     t))
8367
8368 (defun gnus-summary-mark-article-as-unread (mark)
8369   "Mark the current article quickly as unread with MARK."
8370   (let* ((article (gnus-summary-article-number))
8371          (old-mark (gnus-summary-article-mark article)))
8372     ;; Allow the backend to change the mark.
8373     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8374     (if (eq mark old-mark)
8375         t
8376       (if (<= article 0)
8377           (progn
8378             (gnus-error 1 "Can't mark negative article numbers")
8379             nil)
8380         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8381         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8382         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8383         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8384         (cond ((= mark gnus-ticked-mark)
8385                (push article gnus-newsgroup-marked))
8386               ((= mark gnus-dormant-mark)
8387                (push article gnus-newsgroup-dormant))
8388               (t
8389                (push article gnus-newsgroup-unreads)))
8390         (gnus-pull article gnus-newsgroup-reads)
8391
8392         ;; See whether the article is to be put in the cache.
8393         (and gnus-use-cache
8394              (vectorp (gnus-summary-article-header article))
8395              (save-excursion
8396                (gnus-cache-possibly-enter-article
8397                 gnus-newsgroup-name article
8398                 (gnus-summary-article-header article)
8399                 (= mark gnus-ticked-mark)
8400                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8401
8402         ;; Fix the mark.
8403         (gnus-summary-update-mark mark 'unread)
8404         t))))
8405
8406 (defun gnus-summary-mark-article (&optional article mark no-expire)
8407   "Mark ARTICLE with MARK.  MARK can be any character.
8408 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8409 `??' (dormant) and `?E' (expirable).
8410 If MARK is nil, then the default character `?r' is used.
8411 If ARTICLE is nil, then the article on the current line will be
8412 marked.
8413 Iff NO-EXPIRE, auto-expiry will be inhibited."
8414   ;; The mark might be a string.
8415   (when (stringp mark)
8416     (setq mark (aref mark 0)))
8417   ;; If no mark is given, then we check auto-expiring.
8418   (when (null mark)
8419     (setq mark gnus-del-mark))
8420   (when (and (not no-expire)
8421              gnus-newsgroup-auto-expire
8422              (memq mark gnus-auto-expirable-marks))
8423     (setq mark gnus-expirable-mark))
8424   (let ((article (or 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       (unless article
8431         (error "No article on current line"))
8432       (if (not (if (or (= mark gnus-unread-mark)
8433                        (= mark gnus-ticked-mark)
8434                        (= mark gnus-dormant-mark))
8435                    (gnus-mark-article-as-unread article mark)
8436                  (gnus-mark-article-as-read article mark)))
8437           t
8438         ;; See whether the article is to be put in the cache.
8439         (and gnus-use-cache
8440              (not (= mark gnus-canceled-mark))
8441              (vectorp (gnus-summary-article-header article))
8442              (save-excursion
8443                (gnus-cache-possibly-enter-article
8444                 gnus-newsgroup-name article
8445                 (gnus-summary-article-header article)
8446                 (= mark gnus-ticked-mark)
8447                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8448
8449         (when (gnus-summary-goto-subject article nil t)
8450           (let ((buffer-read-only nil))
8451             (gnus-summary-show-thread)
8452             ;; Fix the mark.
8453             (gnus-summary-update-mark mark 'unread)
8454             t))))))
8455
8456 (defun gnus-summary-update-secondary-mark (article)
8457   "Update the secondary (read, process, cache) mark."
8458   (gnus-summary-update-mark
8459    (cond ((memq article gnus-newsgroup-processable)
8460           gnus-process-mark)
8461          ((memq article gnus-newsgroup-cached)
8462           gnus-cached-mark)
8463          ((memq article gnus-newsgroup-replied)
8464           gnus-replied-mark)
8465          ((memq article gnus-newsgroup-saved)
8466           gnus-saved-mark)
8467          (t gnus-unread-mark))
8468    'replied)
8469   (when (gnus-visual-p 'summary-highlight 'highlight)
8470     (gnus-run-hooks 'gnus-summary-update-hook))
8471   t)
8472
8473 (defun gnus-summary-update-mark (mark type)
8474   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8475         (buffer-read-only nil))
8476     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8477     (when forward
8478       (when (looking-at "\r")
8479         (incf forward))
8480       (when (<= (+ forward (point)) (point-max))
8481         ;; Go to the right position on the line.
8482         (goto-char (+ forward (point)))
8483         ;; Replace the old mark with the new mark.
8484         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8485         ;; Optionally update the marks by some user rule.
8486         (when (eq type 'unread)
8487           (gnus-data-set-mark
8488            (gnus-data-find (gnus-summary-article-number)) mark)
8489           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8490
8491 (defun gnus-mark-article-as-read (article &optional mark)
8492   "Enter ARTICLE in the pertinent lists and remove it from others."
8493   ;; Make the article expirable.
8494   (let ((mark (or mark gnus-del-mark)))
8495     (if (= mark gnus-expirable-mark)
8496         (push article gnus-newsgroup-expirable)
8497       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8498     ;; Remove from unread and marked lists.
8499     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8500     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8501     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8502     (push (cons article mark) gnus-newsgroup-reads)
8503     ;; Possibly remove from cache, if that is used.
8504     (when gnus-use-cache
8505       (gnus-cache-enter-remove-article article))
8506     t))
8507
8508 (defun gnus-mark-article-as-unread (article &optional mark)
8509   "Enter ARTICLE in the pertinent lists and remove it from others."
8510   (let ((mark (or mark gnus-ticked-mark)))
8511     (if (<= article 0)
8512         (progn
8513           (gnus-error 1 "Can't mark negative article numbers")
8514           nil)
8515       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8516             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8517             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8518             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8519
8520       ;; Unsuppress duplicates?
8521       (when gnus-suppress-duplicates
8522         (gnus-dup-unsuppress-article article))
8523
8524       (cond ((= mark gnus-ticked-mark)
8525              (push article gnus-newsgroup-marked))
8526             ((= mark gnus-dormant-mark)
8527              (push article gnus-newsgroup-dormant))
8528             (t
8529              (push article gnus-newsgroup-unreads)))
8530       (gnus-pull article gnus-newsgroup-reads)
8531       t)))
8532
8533 (defalias 'gnus-summary-mark-as-unread-forward
8534   'gnus-summary-tick-article-forward)
8535 (make-obsolete 'gnus-summary-mark-as-unread-forward
8536                'gnus-summary-tick-article-forward)
8537 (defun gnus-summary-tick-article-forward (n)
8538   "Tick N articles forwards.
8539 If N is negative, tick backwards instead.
8540 The difference between N and the number of articles ticked is returned."
8541   (interactive "p")
8542   (gnus-summary-mark-forward n gnus-ticked-mark))
8543
8544 (defalias 'gnus-summary-mark-as-unread-backward
8545   'gnus-summary-tick-article-backward)
8546 (make-obsolete 'gnus-summary-mark-as-unread-backward
8547                'gnus-summary-tick-article-backward)
8548 (defun gnus-summary-tick-article-backward (n)
8549   "Tick N articles backwards.
8550 The difference between N and the number of articles ticked is returned."
8551   (interactive "p")
8552   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8553
8554 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8555 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8556 (defun gnus-summary-tick-article (&optional article clear-mark)
8557   "Mark current article as unread.
8558 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8559 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8560   (interactive)
8561   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8562                                        gnus-ticked-mark)))
8563
8564 (defun gnus-summary-mark-as-read-forward (n)
8565   "Mark N articles as read forwards.
8566 If N is negative, mark backwards instead.
8567 The difference between N and the actual number of articles marked is
8568 returned."
8569   (interactive "p")
8570   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8571
8572 (defun gnus-summary-mark-as-read-backward (n)
8573   "Mark the N articles as read backwards.
8574 The difference between N and the actual number of articles marked is
8575 returned."
8576   (interactive "p")
8577   (gnus-summary-mark-forward
8578    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8579
8580 (defun gnus-summary-mark-as-read (&optional article mark)
8581   "Mark current article as read.
8582 ARTICLE specifies the article to be marked as read.
8583 MARK specifies a string to be inserted at the beginning of the line."
8584   (gnus-summary-mark-article article mark))
8585
8586 (defun gnus-summary-clear-mark-forward (n)
8587   "Clear marks from N articles forward.
8588 If N is negative, clear backward instead.
8589 The difference between N and the number of marks cleared is returned."
8590   (interactive "p")
8591   (gnus-summary-mark-forward n gnus-unread-mark))
8592
8593 (defun gnus-summary-clear-mark-backward (n)
8594   "Clear marks from N articles backward.
8595 The difference between N and the number of marks cleared is returned."
8596   (interactive "p")
8597   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8598
8599 (defun gnus-summary-mark-unread-as-read ()
8600   "Intended to be used by `gnus-summary-mark-article-hook'."
8601   (when (memq gnus-current-article gnus-newsgroup-unreads)
8602     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8603
8604 (defun gnus-summary-mark-read-and-unread-as-read ()
8605   "Intended to be used by `gnus-summary-mark-article-hook'."
8606   (let ((mark (gnus-summary-article-mark)))
8607     (when (or (gnus-unread-mark-p mark)
8608               (gnus-read-mark-p mark))
8609       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8610
8611 (defun gnus-summary-mark-region-as-read (point mark all)
8612   "Mark all unread articles between point and mark as read.
8613 If given a prefix, mark all articles between point and mark as read,
8614 even ticked and dormant ones."
8615   (interactive "r\nP")
8616   (save-excursion
8617     (let (article)
8618       (goto-char point)
8619       (beginning-of-line)
8620       (while (and
8621               (< (point) mark)
8622               (progn
8623                 (when (or all
8624                           (memq (setq article (gnus-summary-article-number))
8625                                 gnus-newsgroup-unreads))
8626                   (gnus-summary-mark-article article gnus-del-mark))
8627                 t)
8628               (gnus-summary-find-next))))))
8629
8630 (defun gnus-summary-mark-below (score mark)
8631   "Mark articles with score less than SCORE with MARK."
8632   (interactive "P\ncMark: ")
8633   (setq score (if score
8634                   (prefix-numeric-value score)
8635                 (or gnus-summary-default-score 0)))
8636   (save-excursion
8637     (set-buffer gnus-summary-buffer)
8638     (goto-char (point-min))
8639     (while
8640         (progn
8641           (and (< (gnus-summary-article-score) score)
8642                (gnus-summary-mark-article nil mark))
8643           (gnus-summary-find-next)))))
8644
8645 (defun gnus-summary-kill-below (&optional score)
8646   "Mark articles with score below SCORE as read."
8647   (interactive "P")
8648   (gnus-summary-mark-below score gnus-killed-mark))
8649
8650 (defun gnus-summary-clear-above (&optional score)
8651   "Clear all marks from articles with score above SCORE."
8652   (interactive "P")
8653   (gnus-summary-mark-above score gnus-unread-mark))
8654
8655 (defun gnus-summary-tick-above (&optional score)
8656   "Tick all articles with score above SCORE."
8657   (interactive "P")
8658   (gnus-summary-mark-above score gnus-ticked-mark))
8659
8660 (defun gnus-summary-mark-above (score mark)
8661   "Mark articles with score over SCORE with MARK."
8662   (interactive "P\ncMark: ")
8663   (setq score (if score
8664                   (prefix-numeric-value score)
8665                 (or gnus-summary-default-score 0)))
8666   (save-excursion
8667     (set-buffer gnus-summary-buffer)
8668     (goto-char (point-min))
8669     (while (and (progn
8670                   (when (> (gnus-summary-article-score) score)
8671                     (gnus-summary-mark-article nil mark))
8672                   t)
8673                 (gnus-summary-find-next)))))
8674
8675 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8676 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8677 (defun gnus-summary-limit-include-expunged (&optional no-error)
8678   "Display all the hidden articles that were expunged for low scores."
8679   (interactive)
8680   (let ((buffer-read-only nil))
8681     (let ((scored gnus-newsgroup-scored)
8682           headers h)
8683       (while scored
8684         (unless (gnus-summary-goto-subject (caar scored))
8685           (and (setq h (gnus-summary-article-header (caar scored)))
8686                (< (cdar scored) gnus-summary-expunge-below)
8687                (push h headers)))
8688         (setq scored (cdr scored)))
8689       (if (not headers)
8690           (when (not no-error)
8691             (error "No expunged articles hidden"))
8692         (goto-char (point-min))
8693         (gnus-summary-prepare-unthreaded (nreverse headers))
8694         (goto-char (point-min))
8695         (gnus-summary-position-point)
8696         t))))
8697
8698 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8699   "Mark all unread articles in this newsgroup as read.
8700 If prefix argument ALL is non-nil, ticked and dormant articles will
8701 also be marked as read.
8702 If QUIETLY is non-nil, no questions will be asked.
8703 If TO-HERE is non-nil, it should be a point in the buffer.  All
8704 articles before this point will be marked as read.
8705 Note that this function will only catch up the unread article
8706 in the current summary buffer limitation.
8707 The number of articles marked as read is returned."
8708   (interactive "P")
8709   (prog1
8710       (save-excursion
8711         (when (or quietly
8712                   (not gnus-interactive-catchup) ;Without confirmation?
8713                   gnus-expert-user
8714                   (gnus-y-or-n-p
8715                    (if all
8716                        "Mark absolutely all articles as read? "
8717                      "Mark all unread articles as read? ")))
8718           (if (and not-mark
8719                    (not gnus-newsgroup-adaptive)
8720                    (not gnus-newsgroup-auto-expire)
8721                    (not gnus-suppress-duplicates)
8722                    (or (not gnus-use-cache)
8723                        (eq gnus-use-cache 'passive)))
8724               (progn
8725                 (when all
8726                   (setq gnus-newsgroup-marked nil
8727                         gnus-newsgroup-dormant nil))
8728                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8729             ;; We actually mark all articles as canceled, which we
8730             ;; have to do when using auto-expiry or adaptive scoring.
8731             (gnus-summary-show-all-threads)
8732             (when (gnus-summary-first-subject (not all) t)
8733               (while (and
8734                       (if to-here (< (point) to-here) t)
8735                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8736                       (gnus-summary-find-next (not all) nil nil t))))
8737             (gnus-set-mode-line 'summary))
8738           t))
8739     (gnus-summary-position-point)))
8740
8741 (defun gnus-summary-catchup-to-here (&optional all)
8742   "Mark all unticked articles before the current one as read.
8743 If ALL is non-nil, also mark ticked and dormant articles as read."
8744   (interactive "P")
8745   (save-excursion
8746     (gnus-save-hidden-threads
8747       (let ((beg (point)))
8748         ;; We check that there are unread articles.
8749         (when (or all (gnus-summary-find-prev))
8750           (gnus-summary-catchup all t beg)))))
8751   (gnus-summary-position-point))
8752
8753 (defun gnus-summary-catchup-all (&optional quietly)
8754   "Mark all articles in this newsgroup as read."
8755   (interactive "P")
8756   (gnus-summary-catchup t quietly))
8757
8758 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8759   "Mark all unread articles in this group as read, then exit.
8760 If prefix argument ALL is non-nil, all articles are marked as read."
8761   (interactive "P")
8762   (when (gnus-summary-catchup all quietly nil 'fast)
8763     ;; Select next newsgroup or exit.
8764     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8765              (eq gnus-auto-select-next 'quietly))
8766         (gnus-summary-next-group nil)
8767       (gnus-summary-exit))))
8768
8769 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8770   "Mark all articles in this newsgroup as read, and then exit."
8771   (interactive "P")
8772   (gnus-summary-catchup-and-exit t quietly))
8773
8774 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8775   "Mark all articles in this group as read and select the next group.
8776 If given a prefix, mark all articles, unread as well as ticked, as
8777 read."
8778   (interactive "P")
8779   (save-excursion
8780     (gnus-summary-catchup all))
8781   (gnus-summary-next-group))
8782
8783 ;;;
8784 ;;; with article
8785 ;;;
8786
8787 (defmacro gnus-with-article (article &rest forms)
8788   "Select ARTICLE and perform FORMS in the original article buffer.
8789 Then replace the article with the result."
8790   `(progn
8791      ;; We don't want the article to be marked as read.
8792      (let (gnus-mark-article-hook)
8793        (gnus-summary-select-article t t nil ,article))
8794      (set-buffer gnus-original-article-buffer)
8795      ,@forms
8796      (if (not (gnus-check-backend-function
8797                'request-replace-article (car gnus-article-current)))
8798          (gnus-message 5 "Read-only group; not replacing")
8799        (unless (gnus-request-replace-article
8800                 ,article (car gnus-article-current)
8801                 (current-buffer) t)
8802          (error "Couldn't replace article")))
8803      ;; The cache and backlog have to be flushed somewhat.
8804      (when gnus-keep-backlog
8805        (gnus-backlog-remove-article
8806         (car gnus-article-current) (cdr gnus-article-current)))
8807      (when gnus-use-cache
8808        (gnus-cache-update-article
8809         (car gnus-article-current) (cdr gnus-article-current)))))
8810
8811 (put 'gnus-with-article 'lisp-indent-function 1)
8812 (put 'gnus-with-article 'edebug-form-spec '(form body))
8813
8814 ;; Thread-based commands.
8815
8816 (defun gnus-summary-articles-in-thread (&optional article)
8817   "Return a list of all articles in the current thread.
8818 If ARTICLE is non-nil, return all articles in the thread that starts
8819 with that article."
8820   (let* ((article (or article (gnus-summary-article-number)))
8821          (data (gnus-data-find-list article))
8822          (top-level (gnus-data-level (car data)))
8823          (top-subject
8824           (cond ((null gnus-thread-operation-ignore-subject)
8825                  (gnus-simplify-subject-re
8826                   (mail-header-subject (gnus-data-header (car data)))))
8827                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8828                  (gnus-simplify-subject-fuzzy
8829                   (mail-header-subject (gnus-data-header (car data)))))
8830                 (t nil)))
8831          (end-point (save-excursion
8832                       (if (gnus-summary-go-to-next-thread)
8833                           (point) (point-max))))
8834          articles)
8835     (while (and data
8836                 (< (gnus-data-pos (car data)) end-point))
8837       (when (or (not top-subject)
8838                 (string= top-subject
8839                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8840                              (gnus-simplify-subject-fuzzy
8841                               (mail-header-subject
8842                                (gnus-data-header (car data))))
8843                            (gnus-simplify-subject-re
8844                             (mail-header-subject
8845                              (gnus-data-header (car data)))))))
8846         (push (gnus-data-number (car data)) articles))
8847       (unless (and (setq data (cdr data))
8848                    (> (gnus-data-level (car data)) top-level))
8849         (setq data nil)))
8850     ;; Return the list of articles.
8851     (nreverse articles)))
8852
8853 (defun gnus-summary-rethread-current ()
8854   "Rethread the thread the current article is part of."
8855   (interactive)
8856   (let* ((gnus-show-threads t)
8857          (article (gnus-summary-article-number))
8858          (id (mail-header-id (gnus-summary-article-header)))
8859          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8860     (unless id
8861       (error "No article on the current line"))
8862     (gnus-rebuild-thread id)
8863     (gnus-summary-goto-subject article)))
8864
8865 (defun gnus-summary-reparent-thread ()
8866   "Make the current article child of the marked (or previous) article.
8867
8868 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8869 is non-nil or the Subject: of both articles are the same."
8870   (interactive)
8871   (unless (not (gnus-group-read-only-p))
8872     (error "The current newsgroup does not support article editing"))
8873   (unless (<= (length gnus-newsgroup-processable) 1)
8874     (error "No more than one article may be marked"))
8875   (save-window-excursion
8876     (let ((gnus-article-buffer " *reparent*")
8877           (current-article (gnus-summary-article-number))
8878           ;; First grab the marked article, otherwise one line up.
8879           (parent-article (if (not (null gnus-newsgroup-processable))
8880                               (car gnus-newsgroup-processable)
8881                             (save-excursion
8882                               (if (eq (forward-line -1) 0)
8883                                   (gnus-summary-article-number)
8884                                 (error "Beginning of summary buffer"))))))
8885       (unless (not (eq current-article parent-article))
8886         (error "An article may not be self-referential"))
8887       (let ((message-id (mail-header-id
8888                          (gnus-summary-article-header parent-article))))
8889         (unless (and message-id (not (equal message-id "")))
8890           (error "No message-id in desired parent"))
8891         (gnus-with-article current-article
8892           (save-restriction
8893             (goto-char (point-min))
8894             (message-narrow-to-head)
8895             (if (re-search-forward "^References: " nil t)
8896                 (progn
8897                   (re-search-forward "^[^ \t]" nil t)
8898                   (forward-line -1)
8899                   (end-of-line)
8900                   (insert " " message-id))
8901               (insert "References: " message-id "\n"))))
8902         (set-buffer gnus-summary-buffer)
8903         (gnus-summary-unmark-all-processable)
8904         (gnus-summary-update-article current-article)
8905         (gnus-summary-rethread-current)
8906         (gnus-message 3 "Article %d is now the child of article %d"
8907                       current-article parent-article)))))
8908
8909 (defun gnus-summary-toggle-threads (&optional arg)
8910   "Toggle showing conversation threads.
8911 If ARG is positive number, turn showing conversation threads on."
8912   (interactive "P")
8913   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8914     (setq gnus-show-threads
8915           (if (null arg) (not gnus-show-threads)
8916             (> (prefix-numeric-value arg) 0)))
8917     (gnus-summary-prepare)
8918     (gnus-summary-goto-subject current)
8919     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8920     (gnus-summary-position-point)))
8921
8922 (defun gnus-summary-show-all-threads ()
8923   "Show all threads."
8924   (interactive)
8925   (save-excursion
8926     (let ((buffer-read-only nil))
8927       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8928   (gnus-summary-position-point))
8929
8930 (defun gnus-summary-show-thread ()
8931   "Show thread subtrees.
8932 Returns nil if no thread was there to be shown."
8933   (interactive)
8934   (let ((buffer-read-only nil)
8935         (orig (point))
8936         ;; first goto end then to beg, to have point at beg after let
8937         (end (progn (end-of-line) (point)))
8938         (beg (progn (beginning-of-line) (point))))
8939     (prog1
8940         ;; Any hidden lines here?
8941         (search-forward "\r" end t)
8942       (subst-char-in-region beg end ?\^M ?\n t)
8943       (goto-char orig)
8944       (gnus-summary-position-point))))
8945
8946 (defun gnus-summary-hide-all-threads ()
8947   "Hide all thread subtrees."
8948   (interactive)
8949   (save-excursion
8950     (goto-char (point-min))
8951     (gnus-summary-hide-thread)
8952     (while (zerop (gnus-summary-next-thread 1 t))
8953       (gnus-summary-hide-thread)))
8954   (gnus-summary-position-point))
8955
8956 (defun gnus-summary-hide-thread ()
8957   "Hide thread subtrees.
8958 Returns nil if no threads were there to be hidden."
8959   (interactive)
8960   (let ((buffer-read-only nil)
8961         (start (point))
8962         (article (gnus-summary-article-number)))
8963     (goto-char start)
8964     ;; Go forward until either the buffer ends or the subthread
8965     ;; ends.
8966     (when (and (not (eobp))
8967                (or (zerop (gnus-summary-next-thread 1 t))
8968                    (goto-char (point-max))))
8969       (prog1
8970           (if (and (> (point) start)
8971                    (search-backward "\n" start t))
8972               (progn
8973                 (subst-char-in-region start (point) ?\n ?\^M)
8974                 (gnus-summary-goto-subject article))
8975             (goto-char start)
8976             nil)))))
8977
8978 (defun gnus-summary-go-to-next-thread (&optional previous)
8979   "Go to the same level (or less) next thread.
8980 If PREVIOUS is non-nil, go to previous thread instead.
8981 Return the article number moved to, or nil if moving was impossible."
8982   (let ((level (gnus-summary-thread-level))
8983         (way (if previous -1 1))
8984         (beg (point)))
8985     (forward-line way)
8986     (while (and (not (eobp))
8987                 (< level (gnus-summary-thread-level)))
8988       (forward-line way))
8989     (if (eobp)
8990         (progn
8991           (goto-char beg)
8992           nil)
8993       (setq beg (point))
8994       (prog1
8995           (gnus-summary-article-number)
8996         (goto-char beg)))))
8997
8998 (defun gnus-summary-next-thread (n &optional silent)
8999   "Go to the same level next N'th thread.
9000 If N is negative, search backward instead.
9001 Returns the difference between N and the number of skips actually
9002 done.
9003
9004 If SILENT, don't output messages."
9005   (interactive "p")
9006   (let ((backward (< n 0))
9007         (n (abs n)))
9008     (while (and (> n 0)
9009                 (gnus-summary-go-to-next-thread backward))
9010       (decf n))
9011     (unless silent
9012       (gnus-summary-position-point))
9013     (when (and (not silent) (/= 0 n))
9014       (gnus-message 7 "No more threads"))
9015     n))
9016
9017 (defun gnus-summary-prev-thread (n)
9018   "Go to the same level previous N'th thread.
9019 Returns the difference between N and the number of skips actually
9020 done."
9021   (interactive "p")
9022   (gnus-summary-next-thread (- n)))
9023
9024 (defun gnus-summary-go-down-thread ()
9025   "Go down one level in the current thread."
9026   (let ((children (gnus-summary-article-children)))
9027     (when children
9028       (gnus-summary-goto-subject (car children)))))
9029
9030 (defun gnus-summary-go-up-thread ()
9031   "Go up one level in the current thread."
9032   (let ((parent (gnus-summary-article-parent)))
9033     (when parent
9034       (gnus-summary-goto-subject parent))))
9035
9036 (defun gnus-summary-down-thread (n)
9037   "Go down thread N steps.
9038 If N is negative, go up instead.
9039 Returns the difference between N and how many steps down that were
9040 taken."
9041   (interactive "p")
9042   (let ((up (< n 0))
9043         (n (abs n)))
9044     (while (and (> n 0)
9045                 (if up (gnus-summary-go-up-thread)
9046                   (gnus-summary-go-down-thread)))
9047       (setq n (1- n)))
9048     (gnus-summary-position-point)
9049     (when (/= 0 n)
9050       (gnus-message 7 "Can't go further"))
9051     n))
9052
9053 (defun gnus-summary-up-thread (n)
9054   "Go up thread N steps.
9055 If N is negative, go up instead.
9056 Returns the difference between N and how many steps down that were
9057 taken."
9058   (interactive "p")
9059   (gnus-summary-down-thread (- n)))
9060
9061 (defun gnus-summary-top-thread ()
9062   "Go to the top of the thread."
9063   (interactive)
9064   (while (gnus-summary-go-up-thread))
9065   (gnus-summary-article-number))
9066
9067 (defun gnus-summary-kill-thread (&optional unmark)
9068   "Mark articles under current thread as read.
9069 If the prefix argument is positive, remove any kinds of marks.
9070 If the prefix argument is negative, tick articles instead."
9071   (interactive "P")
9072   (when unmark
9073     (setq unmark (prefix-numeric-value unmark)))
9074   (let ((articles (gnus-summary-articles-in-thread)))
9075     (save-excursion
9076       ;; Expand the thread.
9077       (gnus-summary-show-thread)
9078       ;; Mark all the articles.
9079       (while articles
9080         (gnus-summary-goto-subject (car articles))
9081         (cond ((null unmark)
9082                (gnus-summary-mark-article-as-read gnus-killed-mark))
9083               ((> unmark 0)
9084                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9085               (t
9086                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9087         (setq articles (cdr articles))))
9088     ;; Hide killed subtrees.
9089     (and (null unmark)
9090          gnus-thread-hide-killed
9091          (gnus-summary-hide-thread))
9092     ;; If marked as read, go to next unread subject.
9093     (when (null unmark)
9094       ;; Go to next unread subject.
9095       (gnus-summary-next-subject 1 t)))
9096   (gnus-set-mode-line 'summary))
9097
9098 ;; Summary sorting commands
9099
9100 (defun gnus-summary-sort-by-number (&optional reverse)
9101   "Sort the summary buffer by article number.
9102 Argument REVERSE means reverse order."
9103   (interactive "P")
9104   (gnus-summary-sort 'number reverse))
9105
9106 (defun gnus-summary-sort-by-author (&optional reverse)
9107   "Sort the summary buffer by author name alphabetically.
9108 If `case-fold-search' is non-nil, case of letters is ignored.
9109 Argument REVERSE means reverse order."
9110   (interactive "P")
9111   (gnus-summary-sort 'author reverse))
9112
9113 (defun gnus-summary-sort-by-subject (&optional reverse)
9114   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9115 If `case-fold-search' is non-nil, case of letters is ignored.
9116 Argument REVERSE means reverse order."
9117   (interactive "P")
9118   (gnus-summary-sort 'subject reverse))
9119
9120 (defun gnus-summary-sort-by-date (&optional reverse)
9121   "Sort the summary buffer by date.
9122 Argument REVERSE means reverse order."
9123   (interactive "P")
9124   (gnus-summary-sort 'date reverse))
9125
9126 (defun gnus-summary-sort-by-score (&optional reverse)
9127   "Sort the summary buffer by score.
9128 Argument REVERSE means reverse order."
9129   (interactive "P")
9130   (gnus-summary-sort 'score reverse))
9131
9132 (defun gnus-summary-sort-by-lines (&optional reverse)
9133   "Sort the summary buffer by the number of lines.
9134 Argument REVERSE means reverse order."
9135   (interactive "P")
9136   (gnus-summary-sort 'lines reverse))
9137
9138 (defun gnus-summary-sort-by-chars (&optional reverse)
9139   "Sort the summary buffer by article length.
9140 Argument REVERSE means reverse order."
9141   (interactive "P")
9142   (gnus-summary-sort 'chars reverse))
9143
9144 (defun gnus-summary-sort (predicate reverse)
9145   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9146   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9147          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9148          (gnus-thread-sort-functions
9149           (if (not reverse)
9150               thread
9151             `(lambda (t1 t2)
9152                (,thread t2 t1))))
9153          (gnus-sort-gathered-threads-function
9154           gnus-thread-sort-functions)
9155          (gnus-article-sort-functions
9156           (if (not reverse)
9157               article
9158             `(lambda (t1 t2)
9159                (,article t2 t1))))
9160          (buffer-read-only)
9161          (gnus-summary-prepare-hook nil))
9162     ;; We do the sorting by regenerating the threads.
9163     (gnus-summary-prepare)
9164     ;; Hide subthreads if needed.
9165     (when (and gnus-show-threads gnus-thread-hide-subtree)
9166       (gnus-summary-hide-all-threads))))
9167
9168 ;; Summary saving commands.
9169
9170 (defun gnus-summary-save-article (&optional n not-saved)
9171   "Save the current article using the default saver function.
9172 If N is a positive number, save the N next articles.
9173 If N is a negative number, save the N previous articles.
9174 If N is nil and any articles have been marked with the process mark,
9175 save those articles instead.
9176 The variable `gnus-default-article-saver' specifies the saver function."
9177   (interactive "P")
9178   (let* ((articles (gnus-summary-work-articles n))
9179          (save-buffer (save-excursion
9180                         (nnheader-set-temp-buffer " *Gnus Save*")))
9181          (num (length articles))
9182          header file)
9183     (dolist (article articles)
9184       (setq header (gnus-summary-article-header article))
9185       (if (not (vectorp header))
9186           ;; This is a pseudo-article.
9187           (if (assq 'name header)
9188               (gnus-copy-file (cdr (assq 'name header)))
9189             (gnus-message 1 "Article %d is unsaveable" article))
9190         ;; This is a real article.
9191         (save-window-excursion
9192           (gnus-summary-select-article t nil nil article))
9193         (save-excursion
9194           (set-buffer save-buffer)
9195           (erase-buffer)
9196           (insert-buffer-substring gnus-original-article-buffer))
9197         (setq file (gnus-article-save save-buffer file num))
9198         (gnus-summary-remove-process-mark article)
9199         (unless not-saved
9200           (gnus-summary-set-saved-mark article))))
9201     (gnus-kill-buffer save-buffer)
9202     (gnus-summary-position-point)
9203     (gnus-set-mode-line 'summary)
9204     n))
9205
9206 (defun gnus-summary-pipe-output (&optional arg)
9207   "Pipe the current article to a subprocess.
9208 If N is a positive number, pipe the N next articles.
9209 If N is a negative number, pipe the N previous articles.
9210 If N is nil and any articles have been marked with the process mark,
9211 pipe those articles instead."
9212   (interactive "P")
9213   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9214     (gnus-summary-save-article arg t))
9215   (gnus-configure-windows 'pipe))
9216
9217 (defun gnus-summary-save-article-mail (&optional arg)
9218   "Append the current article to an mail file.
9219 If N is a positive number, save the N next articles.
9220 If N is a negative number, save the N previous articles.
9221 If N is nil and any articles have been marked with the process mark,
9222 save those articles instead."
9223   (interactive "P")
9224   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9225     (gnus-summary-save-article arg)))
9226
9227 (defun gnus-summary-save-article-rmail (&optional arg)
9228   "Append the current article to an rmail file.
9229 If N is a positive number, save the N next articles.
9230 If N is a negative number, save the N previous articles.
9231 If N is nil and any articles have been marked with the process mark,
9232 save those articles instead."
9233   (interactive "P")
9234   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9235     (gnus-summary-save-article arg)))
9236
9237 (defun gnus-summary-save-article-file (&optional arg)
9238   "Append the current article to a file.
9239 If N is a positive number, save the N next articles.
9240 If N is a negative number, save the N previous articles.
9241 If N is nil and any articles have been marked with the process mark,
9242 save those articles instead."
9243   (interactive "P")
9244   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9245     (gnus-summary-save-article arg)))
9246
9247 (defun gnus-summary-write-article-file (&optional arg)
9248   "Write the current article to a file, deleting the previous file.
9249 If N is a positive number, save the N next articles.
9250 If N is a negative number, save the N previous articles.
9251 If N is nil and any articles have been marked with the process mark,
9252 save those articles instead."
9253   (interactive "P")
9254   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9255     (gnus-summary-save-article arg)))
9256
9257 (defun gnus-summary-save-article-body-file (&optional arg)
9258   "Append the current article body to a file.
9259 If N is a positive number, save the N next articles.
9260 If N is a negative number, save the N previous articles.
9261 If N is nil and any articles have been marked with the process mark,
9262 save those articles instead."
9263   (interactive "P")
9264   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9265     (gnus-summary-save-article arg)))
9266
9267 (defun gnus-summary-pipe-message (program)
9268   "Pipe the current article through PROGRAM."
9269   (interactive "sProgram: ")
9270   (gnus-summary-select-article)
9271   (let ((mail-header-separator ""))
9272     (gnus-eval-in-buffer-window gnus-article-buffer
9273       (save-restriction
9274         (widen)
9275         (let ((start (window-start))
9276               buffer-read-only)
9277           (message-pipe-buffer-body program)
9278           (set-window-start (get-buffer-window (current-buffer)) start))))))
9279
9280 (defun gnus-get-split-value (methods)
9281   "Return a value based on the split METHODS."
9282   (let (split-name method result match)
9283     (when methods
9284       (save-excursion
9285         (set-buffer gnus-original-article-buffer)
9286         (save-restriction
9287           (nnheader-narrow-to-headers)
9288           (while (and methods (not split-name))
9289             (goto-char (point-min))
9290             (setq method (pop methods))
9291             (setq match (car method))
9292             (when (cond
9293                    ((stringp match)
9294                     ;; Regular expression.
9295                     (ignore-errors
9296                       (re-search-forward match nil t)))
9297                    ((gnus-functionp match)
9298                     ;; Function.
9299                     (save-restriction
9300                       (widen)
9301                       (setq result (funcall match gnus-newsgroup-name))))
9302                    ((consp match)
9303                     ;; Form.
9304                     (save-restriction
9305                       (widen)
9306                       (setq result (eval match)))))
9307               (setq split-name (cdr method))
9308               (cond ((stringp result)
9309                      (push (expand-file-name
9310                             result gnus-article-save-directory)
9311                            split-name))
9312                     ((consp result)
9313                      (setq split-name (append result split-name)))))))))
9314     (nreverse split-name)))
9315
9316 (defun gnus-valid-move-group-p (group)
9317   (and (boundp group)
9318        (symbol-name group)
9319        (symbol-value group)
9320        (gnus-get-function (gnus-find-method-for-group
9321                            (symbol-name group)) 'request-accept-article t)))
9322
9323 (defun gnus-read-move-group-name (prompt default articles prefix)
9324   "Read a group name."
9325   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9326          (minibuffer-confirm-incomplete nil) ; XEmacs
9327          (prom
9328           (format "%s %s to:"
9329                   prompt
9330                   (if (> (length articles) 1)
9331                       (format "these %d articles" (length articles))
9332                     "this article")))
9333          (to-newsgroup
9334           (cond
9335            ((null split-name)
9336             (gnus-completing-read default prom
9337                                   gnus-active-hashtb
9338                                   'gnus-valid-move-group-p
9339                                   nil prefix
9340                                   'gnus-group-history))
9341            ((= 1 (length split-name))
9342             (gnus-completing-read (car split-name) prom
9343                                   gnus-active-hashtb
9344                                   'gnus-valid-move-group-p
9345                                   nil nil
9346                                   'gnus-group-history))
9347            (t
9348             (gnus-completing-read nil prom
9349                                   (mapcar (lambda (el) (list el))
9350                                           (nreverse split-name))
9351                                   nil nil nil
9352                                   'gnus-group-history))))
9353          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9354     (when to-newsgroup
9355       (if (or (string= to-newsgroup "")
9356               (string= to-newsgroup prefix))
9357           (setq to-newsgroup default))
9358       (unless to-newsgroup
9359         (error "No group name entered"))
9360       (or (gnus-active to-newsgroup)
9361           (gnus-activate-group to-newsgroup nil nil to-method)
9362           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9363                                      to-newsgroup))
9364               (or (and (gnus-request-create-group to-newsgroup to-method)
9365                        (gnus-activate-group
9366                         to-newsgroup nil nil to-method)
9367                        (gnus-subscribe-group to-newsgroup))
9368                   (error "Couldn't create group %s" to-newsgroup)))
9369           (error "No such group: %s" to-newsgroup)))
9370     to-newsgroup))
9371
9372 (defun gnus-summary-save-parts (type dir n &optional reverse)
9373   "Save parts matching TYPE to DIR.
9374 If REVERSE, save parts that do not match TYPE."
9375   (interactive
9376    (list (read-string "Save parts of type: " 
9377                       (or (car gnus-summary-save-parts-type-history)
9378                           gnus-summary-save-parts-default-mime)
9379                       'gnus-summary-save-parts-type-history)
9380          (setq gnus-summary-save-parts-last-directory
9381                (read-file-name "Save to directory: " 
9382                                gnus-summary-save-parts-last-directory
9383                                nil t))
9384          current-prefix-arg))
9385   (gnus-summary-iterate n
9386     (let ((gnus-display-mime-function nil)
9387           (gnus-inhibit-treatment t))
9388       (gnus-summary-select-article))
9389     (save-excursion
9390       (set-buffer gnus-article-buffer)
9391       (let ((handles (or gnus-article-mime-handles
9392                          (mm-dissect-buffer) (mm-uu-dissect))))
9393         (when handles
9394           (gnus-summary-save-parts-1 type dir handles reverse)
9395           (unless gnus-article-mime-handles ;; Don't destroy this case.
9396             (mm-destroy-parts handles)))))))
9397
9398 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9399   (if (stringp (car handle))
9400       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9401               (cdr handle))
9402     (when (if reverse
9403               (not (string-match type (mm-handle-media-type handle)))
9404             (string-match type (mm-handle-media-type handle)))
9405       (let ((file (expand-file-name
9406                    (file-name-nondirectory
9407                     (or
9408                      (mail-content-type-get
9409                       (mm-handle-disposition handle) 'filename)
9410                      (concat gnus-newsgroup-name
9411                              "." (number-to-string
9412                                   (cdr gnus-article-current)))))
9413                    dir)))
9414         (unless (file-exists-p file)
9415           (mm-save-part-to-file handle file))))))
9416
9417 ;; Summary extract commands
9418
9419 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9420   (let ((buffer-read-only nil)
9421         (article (gnus-summary-article-number))
9422         after-article b e)
9423     (unless (gnus-summary-goto-subject article)
9424       (error "No such article: %d" article))
9425     (gnus-summary-position-point)
9426     ;; If all commands are to be bunched up on one line, we collect
9427     ;; them here.
9428     (unless gnus-view-pseudos-separately
9429       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9430             files action)
9431         (while ps
9432           (setq action (cdr (assq 'action (car ps))))
9433           (setq files (list (cdr (assq 'name (car ps)))))
9434           (while (and ps (cdr ps)
9435                       (string= (or action "1")
9436                                (or (cdr (assq 'action (cadr ps))) "2")))
9437             (push (cdr (assq 'name (cadr ps))) files)
9438             (setcdr ps (cddr ps)))
9439           (when files
9440             (when (not (string-match "%s" action))
9441               (push " " files))
9442             (push " " files)
9443             (when (assq 'execute (car ps))
9444               (setcdr (assq 'execute (car ps))
9445                       (funcall (if (string-match "%s" action)
9446                                    'format 'concat)
9447                                action
9448                                (mapconcat
9449                                 (lambda (f)
9450                                   (if (equal f " ")
9451                                       f
9452                                     (gnus-quote-arg-for-sh-or-csh f)))
9453                                 files " ")))))
9454           (setq ps (cdr ps)))))
9455     (if (and gnus-view-pseudos (not not-view))
9456         (while pslist
9457           (when (assq 'execute (car pslist))
9458             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9459                                   (eq gnus-view-pseudos 'not-confirm)))
9460           (setq pslist (cdr pslist)))
9461       (save-excursion
9462         (while pslist
9463           (setq after-article (or (cdr (assq 'article (car pslist)))
9464                                   (gnus-summary-article-number)))
9465           (gnus-summary-goto-subject after-article)
9466           (forward-line 1)
9467           (setq b (point))
9468           (insert "    " (file-name-nondirectory
9469                           (cdr (assq 'name (car pslist))))
9470                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9471           (setq e (point))
9472           (forward-line -1)             ; back to `b'
9473           (gnus-add-text-properties
9474            b (1- e) (list 'gnus-number gnus-reffed-article-number
9475                           gnus-mouse-face-prop gnus-mouse-face))
9476           (gnus-data-enter
9477            after-article gnus-reffed-article-number
9478            gnus-unread-mark b (car pslist) 0 (- e b))
9479           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9480           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9481           (setq pslist (cdr pslist)))))))
9482
9483 (defun gnus-pseudos< (p1 p2)
9484   (let ((c1 (cdr (assq 'action p1)))
9485         (c2 (cdr (assq 'action p2))))
9486     (and c1 c2 (string< c1 c2))))
9487
9488 (defun gnus-request-pseudo-article (props)
9489   (cond ((assq 'execute props)
9490          (gnus-execute-command (cdr (assq 'execute props)))))
9491   (let ((gnus-current-article (gnus-summary-article-number)))
9492     (gnus-run-hooks 'gnus-mark-article-hook)))
9493
9494 (defun gnus-execute-command (command &optional automatic)
9495   (save-excursion
9496     (gnus-article-setup-buffer)
9497     (set-buffer gnus-article-buffer)
9498     (setq buffer-read-only nil)
9499     (let ((command (if automatic command
9500                      (read-string "Command: " (cons command 0)))))
9501       (erase-buffer)
9502       (insert "$ " command "\n\n")
9503       (if gnus-view-pseudo-asynchronously
9504           (start-process "gnus-execute" (current-buffer) shell-file-name
9505                          shell-command-switch command)
9506         (call-process shell-file-name nil t nil
9507                       shell-command-switch command)))))
9508
9509 ;; Summary kill commands.
9510
9511 (defun gnus-summary-edit-global-kill (article)
9512   "Edit the \"global\" kill file."
9513   (interactive (list (gnus-summary-article-number)))
9514   (gnus-group-edit-global-kill article))
9515
9516 (defun gnus-summary-edit-local-kill ()
9517   "Edit a local kill file applied to the current newsgroup."
9518   (interactive)
9519   (setq gnus-current-headers (gnus-summary-article-header))
9520   (gnus-group-edit-local-kill
9521    (gnus-summary-article-number) gnus-newsgroup-name))
9522
9523 ;;; Header reading.
9524
9525 (defun gnus-read-header (id &optional header)
9526   "Read the headers of article ID and enter them into the Gnus system."
9527   (let ((group gnus-newsgroup-name)
9528         (gnus-override-method
9529          (or
9530           gnus-override-method
9531           (and (gnus-news-group-p gnus-newsgroup-name)
9532                (car (gnus-refer-article-methods)))))
9533         where)
9534     ;; First we check to see whether the header in question is already
9535     ;; fetched.
9536     (if (stringp id)
9537         ;; This is a Message-ID.
9538         (setq header (or header (gnus-id-to-header id)))
9539       ;; This is an article number.
9540       (setq header (or header (gnus-summary-article-header id))))
9541     (if (and header
9542              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9543         ;; We have found the header.
9544         header
9545       ;; If this is a sparse article, we have to nix out its
9546       ;; previous entry in the thread hashtb.
9547       (when (and header
9548                  (gnus-summary-article-sparse-p (mail-header-number header)))
9549         (let* ((parent (gnus-parent-id (mail-header-references header)))
9550                (thread (and parent (gnus-id-to-thread parent))))
9551           (when thread
9552             (delq (assq header thread) thread))))
9553       ;; We have to really fetch the header to this article.
9554       (save-excursion
9555         (set-buffer nntp-server-buffer)
9556         (when (setq where (gnus-request-head id group))
9557           (nnheader-fold-continuation-lines)
9558           (goto-char (point-max))
9559           (insert ".\n")
9560           (goto-char (point-min))
9561           (insert "211 ")
9562           (princ (cond
9563                   ((numberp id) id)
9564                   ((cdr where) (cdr where))
9565                   (header (mail-header-number header))
9566                   (t gnus-reffed-article-number))
9567                  (current-buffer))
9568           (insert " Article retrieved.\n"))
9569         (if (or (not where)
9570                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9571             ()                          ; Malformed head.
9572           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9573             (when (and (stringp id)
9574                        (not (string= (gnus-group-real-name group)
9575                                      (car where))))
9576               ;; If we fetched by Message-ID and the article came
9577               ;; from a different group, we fudge some bogus article
9578               ;; numbers for this article.
9579               (mail-header-set-number header gnus-reffed-article-number))
9580             (save-excursion
9581               (set-buffer gnus-summary-buffer)
9582               (decf gnus-reffed-article-number)
9583               (gnus-remove-header (mail-header-number header))
9584               (push header gnus-newsgroup-headers)
9585               (setq gnus-current-headers header)
9586               (push (mail-header-number header) gnus-newsgroup-limit)))
9587           header)))))
9588
9589 (defun gnus-remove-header (number)
9590   "Remove header NUMBER from `gnus-newsgroup-headers'."
9591   (if (and gnus-newsgroup-headers
9592            (= number (mail-header-number (car gnus-newsgroup-headers))))
9593       (pop gnus-newsgroup-headers)
9594     (let ((headers gnus-newsgroup-headers))
9595       (while (and (cdr headers)
9596                   (not (= number (mail-header-number (cadr headers)))))
9597         (pop headers))
9598       (when (cdr headers)
9599         (setcdr headers (cddr headers))))))
9600
9601 ;;;
9602 ;;; summary highlights
9603 ;;;
9604
9605 (defun gnus-highlight-selected-summary ()
9606   "Highlight selected article in summary buffer."
9607   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9608   (when gnus-summary-selected-face
9609     (save-excursion
9610       (let* ((beg (progn (beginning-of-line) (point)))
9611              (end (progn (end-of-line) (point)))
9612              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9613              (from (if (get-text-property beg gnus-mouse-face-prop)
9614                        beg
9615                      (or (next-single-property-change
9616                           beg gnus-mouse-face-prop nil end)
9617                          beg)))
9618              (to
9619               (if (= from end)
9620                   (- from 2)
9621                 (or (next-single-property-change
9622                      from gnus-mouse-face-prop nil end)
9623                     end))))
9624         ;; If no mouse-face prop on line we will have to = from = end,
9625         ;; so we highlight the entire line instead.
9626         (when (= (+ to 2) from)
9627           (setq from beg)
9628           (setq to end))
9629         (if gnus-newsgroup-selected-overlay
9630             ;; Move old overlay.
9631             (gnus-move-overlay
9632              gnus-newsgroup-selected-overlay from to (current-buffer))
9633           ;; Create new overlay.
9634           (gnus-overlay-put
9635            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9636            'face gnus-summary-selected-face))))))
9637
9638 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9639 (defun gnus-summary-highlight-line ()
9640   "Highlight current line according to `gnus-summary-highlight'."
9641   (let* ((list gnus-summary-highlight)
9642          (p (point))
9643          (end (progn (end-of-line) (point)))
9644          ;; now find out where the line starts and leave point there.
9645          (beg (progn (beginning-of-line) (point)))
9646          (article (gnus-summary-article-number))
9647          (score (or (cdr (assq (or article gnus-current-article)
9648                                gnus-newsgroup-scored))
9649                     gnus-summary-default-score 0))
9650          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9651          (inhibit-read-only t))
9652     ;; Eval the cars of the lists until we find a match.
9653     (let ((default gnus-summary-default-score))
9654       (while (and list
9655                   (not (eval (caar list))))
9656         (setq list (cdr list))))
9657     (let ((face (cdar list)))
9658       (unless (eq face (get-text-property beg 'face))
9659         (gnus-put-text-property-excluding-characters-with-faces
9660          beg end 'face
9661          (setq face (if (boundp face) (symbol-value face) face)))
9662         (when gnus-summary-highlight-line-function
9663           (funcall gnus-summary-highlight-line-function article face))))
9664     (goto-char p)))
9665
9666 (defun gnus-update-read-articles (group unread &optional compute)
9667   "Update the list of read articles in GROUP."
9668   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9669          (entry (gnus-gethash group gnus-newsrc-hashtb))
9670          (info (nth 2 entry))
9671          (prev 1)
9672          (unread (sort (copy-sequence unread) '<))
9673          read)
9674     (if (or (not info) (not active))
9675         ;; There is no info on this group if it was, in fact,
9676         ;; killed.  Gnus stores no information on killed groups, so
9677         ;; there's nothing to be done.
9678         ;; One could store the information somewhere temporarily,
9679         ;; perhaps...  Hmmm...
9680         ()
9681       ;; Remove any negative articles numbers.
9682       (while (and unread (< (car unread) 0))
9683         (setq unread (cdr unread)))
9684       ;; Remove any expired article numbers
9685       (while (and unread (< (car unread) (car active)))
9686         (setq unread (cdr unread)))
9687       ;; Compute the ranges of read articles by looking at the list of
9688       ;; unread articles.
9689       (while unread
9690         (when (/= (car unread) prev)
9691           (push (if (= prev (1- (car unread))) prev
9692                   (cons prev (1- (car unread))))
9693                 read))
9694         (setq prev (1+ (car unread)))
9695         (setq unread (cdr unread)))
9696       (when (<= prev (cdr active))
9697         (push (cons prev (cdr active)) read))
9698       (setq read (if (> (length read) 1) (nreverse read) read))
9699       (if compute
9700           read
9701         (save-excursion
9702           (let (setmarkundo)
9703             ;; Propagate the read marks to the backend.
9704             (when (gnus-check-backend-function 'request-set-mark group)
9705               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9706                     (add (gnus-remove-from-range read (gnus-info-read info))))
9707                 (when (or add del)
9708                   (unless (gnus-check-group group)
9709                     (error "Can't open server for %s" group))
9710                   (gnus-request-set-mark
9711                    group (delq nil (list (if add (list add 'add '(read)))
9712                                          (if del (list del 'del '(read))))))
9713                   (setq setmarkundo
9714                         `(gnus-request-set-mark
9715                           ,group
9716                           ',(delq nil (list
9717                                        (if del (list del 'add '(read)))
9718                                        (if add (list add 'del '(read))))))))))
9719             (set-buffer gnus-group-buffer)
9720             (gnus-undo-register
9721               `(progn
9722                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9723                  (gnus-info-set-read ',info ',(gnus-info-read info))
9724                  (gnus-get-unread-articles-in-group ',info 
9725                                                     (gnus-active ,group))
9726                  (gnus-group-update-group ,group t)
9727                  ,setmarkundo))))
9728         ;; Enter this list into the group info.
9729         (gnus-info-set-read info read)
9730         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9731         (gnus-get-unread-articles-in-group info (gnus-active group))
9732         t))))
9733
9734 (defun gnus-offer-save-summaries ()
9735   "Offer to save all active summary buffers."
9736   (save-excursion
9737     (let ((buflist (buffer-list))
9738           buffers bufname)
9739       ;; Go through all buffers and find all summaries.
9740       (while buflist
9741         (and (setq bufname (buffer-name (car buflist)))
9742              (string-match "Summary" bufname)
9743              (save-excursion
9744                (set-buffer bufname)
9745                ;; We check that this is, indeed, a summary buffer.
9746                (and (eq major-mode 'gnus-summary-mode)
9747                     ;; Also make sure this isn't bogus.
9748                     gnus-newsgroup-prepared
9749                     ;; Also make sure that this isn't a dead summary buffer.
9750                     (not gnus-dead-summary-mode)))
9751              (push bufname buffers))
9752         (setq buflist (cdr buflist)))
9753       ;; Go through all these summary buffers and offer to save them.
9754       (when buffers
9755         (map-y-or-n-p
9756          "Update summary buffer %s? "
9757          (lambda (buf)
9758            (switch-to-buffer buf)
9759            (gnus-summary-exit))
9760          buffers)))))
9761
9762
9763 ;;; @ for mime-partial
9764 ;;;
9765
9766 (defun gnus-request-partial-message ()
9767   (save-excursion
9768     (let ((number (gnus-summary-article-number))
9769           (group gnus-newsgroup-name)
9770           (mother gnus-article-buffer))
9771       (set-buffer (get-buffer-create " *Partial Article*"))
9772       (erase-buffer)
9773       (setq mime-preview-buffer mother)
9774       (gnus-request-article-this-buffer number group)
9775       (mime-parse-buffer)
9776       )))
9777
9778 (autoload 'mime-combine-message/partial-pieces-automatically
9779   "mime-partial"
9780   "Internal method to combine message/partial messages automatically.")
9781
9782 (mime-add-condition
9783  'action '((type . message)(subtype . partial)
9784            (major-mode . gnus-original-article-mode)
9785            (method . mime-combine-message/partial-pieces-automatically)
9786            (summary-buffer-exp . gnus-summary-buffer)
9787            (request-partial-message-method . gnus-request-partial-message)
9788            ))
9789
9790
9791 ;;; @ for message/rfc822
9792 ;;;
9793
9794 (defun gnus-mime-extract-message/rfc822 (entity situation)
9795   (let (group article num cwin swin cur)
9796     (with-temp-buffer
9797       (mime-insert-entity-content entity)
9798       (setq group (or (cdr (assq 'group situation))
9799                       (completing-read "Group: "
9800                                        gnus-active-hashtb
9801                                        nil
9802                                        (gnus-read-active-file-p)
9803                                        gnus-newsgroup-name))
9804             article (gnus-request-accept-article group)))
9805     (when (and (consp article)
9806                (numberp (setq article (cdr article))))
9807       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9808             cwin (get-buffer-window (current-buffer) t))
9809       (save-window-excursion
9810         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9811             (select-window swin)
9812           (set-buffer gnus-summary-buffer))
9813         (setq cur gnus-current-article)
9814         (forward-line num)
9815         (let (gnus-show-threads)
9816           (gnus-summary-goto-subject article t))
9817         (gnus-summary-clear-mark-forward 1)
9818         (gnus-summary-goto-subject cur))
9819       (when (and cwin (window-frame cwin))
9820         (select-frame (window-frame cwin)))
9821       (when (boundp 'mime-acting-situation-to-override)
9822         (set-alist 'mime-acting-situation-to-override
9823                    'group
9824                    group)
9825         (set-alist 'mime-acting-situation-to-override
9826                    'after-method
9827                    `(progn
9828                       (save-current-buffer
9829                         (set-buffer gnus-group-buffer)
9830                         (gnus-activate-group ,group))
9831                       (gnus-summary-goto-article ,cur
9832                                                  gnus-show-all-headers)))
9833         (set-alist 'mime-acting-situation-to-override
9834                    'number num)))))
9835
9836 (mime-add-condition
9837  'action '((type . message)(subtype . rfc822)
9838            (major-mode . gnus-original-article-mode)
9839            (method . gnus-mime-extract-message/rfc822)
9840            (mode . "extract")
9841            ))
9842
9843 (mime-add-condition
9844  'action '((type . message)(subtype . news)
9845            (major-mode . gnus-original-article-mode)
9846            (method . gnus-mime-extract-message/rfc822)
9847            (mode . "extract")
9848            ))
9849
9850 (defun gnus-mime-extract-multipart (entity situation)
9851   (let ((children (mime-entity-children entity))
9852         mime-acting-situation-to-override
9853         f)
9854     (while children
9855       (mime-play-entity (car children)
9856                         (cons (assq 'mode situation)
9857                               mime-acting-situation-to-override))
9858       (setq children (cdr children)))
9859     (if (setq f (cdr (assq 'after-method
9860                            mime-acting-situation-to-override)))
9861         (eval f)
9862       )))
9863
9864 (mime-add-condition
9865  'action '((type . multipart)
9866            (method . gnus-mime-extract-multipart)
9867            (mode . "extract")
9868            )
9869  'with-default)
9870
9871
9872 ;;; @ end
9873 ;;;
9874
9875 (defun gnus-summary-setup-default-charset ()
9876   "Setup newsgroup default charset."
9877   (if (equal gnus-newsgroup-name "nndraft:drafts")
9878       (setq gnus-newsgroup-charset nil)
9879     (let* ((name (and gnus-newsgroup-name
9880                       (gnus-group-real-name gnus-newsgroup-name)))
9881            (ignored-charsets
9882             (or gnus-newsgroup-ephemeral-ignored-charsets
9883                 (append
9884                  (and gnus-newsgroup-name
9885                       (or (gnus-group-find-parameter gnus-newsgroup-name
9886                                                      'ignored-charsets t)
9887                           (let ((alist gnus-group-ignored-charsets-alist)
9888                                 elem (charsets nil))
9889                             (while (setq elem (pop alist))
9890                               (when (and name
9891                                          (string-match (car elem) name))
9892                                 (setq alist nil
9893                                       charsets (cdr elem))))
9894                             charsets)))
9895                  gnus-newsgroup-ignored-charsets))))
9896       (setq gnus-newsgroup-charset
9897             (or gnus-newsgroup-ephemeral-charset
9898                 (and gnus-newsgroup-name
9899                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9900                          (let ((alist gnus-group-charset-alist)
9901                                elem charset)
9902                            (while (setq elem (pop alist))
9903                              (when (and name
9904                                         (string-match (car elem) name))
9905                                (setq alist nil
9906                                      charset (cadr elem))))
9907                            charset)))
9908                 gnus-default-charset))
9909       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9910            ignored-charsets))))
9911
9912 ;;;
9913 ;;; Mime Commands
9914 ;;;
9915
9916 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9917   "Display the current article buffer fully MIME-buttonized.
9918 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9919 treated as multipart/mixed."
9920   (interactive "P")
9921   (require 'gnus-art)
9922   (let ((gnus-unbuttonized-mime-types nil)
9923         (gnus-mime-display-multipart-as-mixed show-all-parts))
9924     (gnus-summary-show-article)))
9925
9926 (defun gnus-summary-repair-multipart (article)
9927   "Add a Content-Type header to a multipart article without one."
9928   (interactive (list (gnus-summary-article-number)))
9929   (gnus-with-article article
9930     (message-narrow-to-head)
9931     (goto-char (point-max))
9932     (widen)
9933     (when (search-forward "\n--" nil t)
9934       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9935         (message-narrow-to-head)
9936         (message-remove-header "Mime-Version")
9937         (message-remove-header "Content-Type")
9938         (goto-char (point-max))
9939         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9940                         separator))
9941         (insert "Mime-Version: 1.0\n")
9942         (widen))))
9943   (let (gnus-mark-article-hook)
9944     (gnus-summary-select-article t t nil article)))
9945
9946 (defun gnus-summary-toggle-display-buttonized ()
9947   "Toggle the buttonizing of the article buffer."
9948   (interactive)
9949   (require 'gnus-art)
9950   (if (setq gnus-inhibit-mime-unbuttonizing
9951             (not gnus-inhibit-mime-unbuttonizing))
9952       (let ((gnus-unbuttonized-mime-types nil))
9953         (gnus-summary-show-article))
9954     (gnus-summary-show-article)))
9955
9956 ;;;
9957 ;;; Intelli-mouse commmands
9958 ;;;
9959
9960 (defun gnus-wheel-summary-scroll (event)
9961   (interactive "e")
9962   (let ((amount (if (memq 'shift (event-modifiers event))
9963                     (car gnus-wheel-scroll-amount)
9964                   (cdr gnus-wheel-scroll-amount)))
9965         (direction (- (* (static-if (featurep 'xemacs)
9966                              (event-button event)
9967                            (cond ((eq 'mouse-4 (event-basic-type event))
9968                                   4)
9969                                  ((eq 'mouse-5 (event-basic-type event))
9970                                   5)))
9971                          2) 9))
9972         edge)
9973     (gnus-summary-scroll-up (* amount direction))
9974     (when (gnus-eval-in-buffer-window gnus-article-buffer
9975             (save-restriction
9976               (widen)
9977               (and (if (< 0 direction)
9978                        (gnus-article-next-page 0)
9979                      (gnus-article-prev-page 0)
9980                      (bobp))
9981                    (if (setq edge (get-text-property
9982                                    (point-min) 'gnus-wheel-edge))
9983                        (setq edge (* edge direction))
9984                      (setq edge -1))
9985                    (or (plusp edge)
9986                        (let ((buffer-read-only nil)
9987                              (inhibit-read-only t))
9988                          (put-text-property (point-min) (point-max)
9989                                             'gnus-wheel-edge direction)
9990                          nil))
9991                    (or (> edge gnus-wheel-edge-resistance)
9992                        (let ((buffer-read-only nil)
9993                              (inhibit-read-only t))
9994                          (put-text-property (point-min) (point-max)
9995                                             'gnus-wheel-edge
9996                                             (* (1+ edge) direction))
9997                          nil))
9998                    (eq last-command 'gnus-wheel-summary-scroll))))
9999       (gnus-summary-next-article nil nil (minusp direction)))))
10000
10001 (defun gnus-wheel-install ()
10002   "Enable mouse wheel support on summary window."
10003   (when gnus-use-wheel
10004     (let ((keys
10005            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10006       (dolist (key keys)
10007         (define-key gnus-summary-mode-map key
10008           'gnus-wheel-summary-scroll)))))
10009
10010 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10011
10012 ;;;
10013 ;;; Traditional PGP commmands
10014 ;;;
10015
10016 (defun gnus-summary-decrypt-article (&optional force)
10017   "Decrypt the current article in traditional PGP way.
10018 This will have permanent effect only in mail groups.
10019 If FORCE is non-nil, allow editing of articles even in read-only
10020 groups."
10021   (interactive "P")
10022   (gnus-summary-select-article t)
10023   (gnus-eval-in-buffer-window gnus-article-buffer
10024     (save-excursion
10025       (save-restriction
10026         (widen)
10027         (goto-char (point-min))
10028         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10029           (error "Not a traditional PGP message!"))
10030         (let ((armor-start (match-beginning 0)))
10031           (if (and (pgg-decrypt-region armor-start (point-max))
10032                    (or force (not (gnus-group-read-only-p))))
10033               (let ((inhibit-read-only t)
10034                     buffer-read-only)
10035                 (delete-region armor-start
10036                                (progn
10037                                  (re-search-forward "^-+END PGP" nil t)
10038                                  (beginning-of-line 2)
10039                                  (point)))
10040                 (insert-buffer-substring pgg-output-buffer))))))))
10041
10042 (defun gnus-summary-verify-article ()
10043   "Verify the current article in traditional PGP way."
10044   (interactive)
10045   (save-excursion
10046     (set-buffer gnus-original-article-buffer)
10047     (goto-char (point-min))
10048     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10049       (error "Not a traditional PGP message!"))
10050     (re-search-forward "^-+END PGP" nil t)
10051     (beginning-of-line 2)
10052     (call-interactively (function pgg-verify-region))))
10053
10054 ;;;
10055 ;;; Generic summary marking commands
10056 ;;;
10057
10058 (defvar gnus-summary-marking-alist
10059   '((read gnus-del-mark "d")
10060     (unread gnus-unread-mark "u")
10061     (ticked gnus-ticked-mark "!")
10062     (dormant gnus-dormant-mark "?")
10063     (expirable gnus-expirable-mark "e"))
10064   "An alist of names/marks/keystrokes.")
10065
10066 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10067 (defvar gnus-summary-mark-map)
10068
10069 (defun gnus-summary-make-all-marking-commands ()
10070   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10071   (dolist (elem gnus-summary-marking-alist)
10072     (apply 'gnus-summary-make-marking-command elem)))
10073
10074 (defun gnus-summary-make-marking-command (name mark keystroke)
10075   (let ((map (make-sparse-keymap)))
10076     (define-key gnus-summary-generic-mark-map keystroke map)
10077     (dolist (lway `((next "next" next nil "n")
10078                     (next-unread "next unread" next t "N")
10079                     (prev "previous" prev nil "p")
10080                     (prev-unread "previous unread" prev t "P")
10081                     (nomove "" nil nil ,keystroke)))
10082       (let ((func (gnus-summary-make-marking-command-1
10083                    mark (car lway) lway name)))
10084         (setq func (eval func))
10085         (define-key map (nth 4 lway) func)))))
10086
10087 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10088   `(defun ,(intern
10089             (format "gnus-summary-put-mark-as-%s%s"
10090                     name (if (eq way 'nomove)
10091                              ""
10092                            (concat "-" (symbol-name way)))))
10093      (n)
10094      ,(format
10095        "Mark the current article as %s%s.
10096 If N, the prefix, then repeat N times.
10097 If N is negative, move in reverse order.
10098 The difference between N and the actual number of articles marked is
10099 returned."
10100        name (car (cdr lway)))
10101      (interactive "p")
10102      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10103
10104 (defun gnus-summary-generic-mark (n mark move unread)
10105   "Mark N articles with MARK."
10106   (unless (eq major-mode 'gnus-summary-mode)
10107     (error "This command can only be used in the summary buffer"))
10108   (gnus-summary-show-thread)
10109   (let ((nummove
10110          (cond
10111           ((eq move 'next) 1)
10112           ((eq move 'prev) -1)
10113           (t 0))))
10114     (if (zerop nummove)
10115         (setq n 1)
10116       (when (< n 0)
10117         (setq n (abs n)
10118               nummove (* -1 nummove))))
10119     (while (and (> n 0)
10120                 (gnus-summary-mark-article nil mark)
10121                 (zerop (gnus-summary-next-subject nummove unread t)))
10122       (setq n (1- n)))
10123     (when (/= 0 n)
10124       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10125     (gnus-summary-recenter)
10126     (gnus-summary-position-point)
10127     (gnus-set-mode-line 'summary)
10128     n))
10129
10130 (gnus-summary-make-all-marking-commands)
10131
10132 (gnus-ems-redefine)
10133
10134 (provide 'gnus-sum)
10135
10136 (run-hooks 'gnus-sum-load-hook)
10137
10138 ;;; gnus-sum.el ends here