Synch with Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include: `gnus-simplify-subject-re',
145 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
146   :group 'gnus-thread
147   :type '(repeat function))
148
149 (defcustom gnus-simplify-ignored-prefixes nil
150   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  regexp))
154
155 (defcustom gnus-build-sparse-threads nil
156   "*If non-nil, fill in the gaps in threads.
157 If `some', only fill in the gaps that are needed to tie loose threads
158 together.  If `more', fill in all leaf nodes that Gnus can find.  If
159 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
160   :group 'gnus-thread
161   :type '(choice (const :tag "off" nil)
162                  (const some)
163                  (const more)
164                  (sexp :menu-tag "all" t)))
165
166 (defcustom gnus-summary-thread-gathering-function
167   'gnus-gather-threads-by-subject
168   "*Function used for gathering loose threads.
169 There are two pre-defined functions: `gnus-gather-threads-by-subject',
170 which only takes Subjects into consideration; and
171 `gnus-gather-threads-by-references', which compared the References
172 headers of the articles to find matches."
173   :group 'gnus-thread
174   :type '(radio (function-item gnus-gather-threads-by-subject)
175                 (function-item gnus-gather-threads-by-references)
176                 (function :tag "other")))
177
178 (defcustom gnus-summary-same-subject ""
179   "*String indicating that the current article has the same subject as the previous.
180 This variable will only be used if the value of
181 `gnus-summary-make-false-root' is `empty'."
182   :group 'gnus-summary-format
183   :type 'string)
184
185 (defcustom gnus-summary-goto-unread t
186   "*If t, many commands will go to the next unread article.
187 This applies to marking commands as well as other commands that
188 \"naturally\" select the next article, like, for instance, `SPC' at
189 the end of an article.
190
191 If nil, the marking commands do NOT go to the next unread article
192 (they go to the next article instead).  If `never', commands that
193 usually go to the next unread article, will go to the next article,
194 whether it is read or not."
195   :group 'gnus-summary-marks
196   :link '(custom-manual "(gnus)Setting Marks")
197   :type '(choice (const :tag "off" nil)
198                  (const never)
199                  (sexp :menu-tag "on" t)))
200
201 (defcustom gnus-summary-default-score 0
202   "*Default article score level.
203 All scores generated by the score files will be added to this score.
204 If this variable is nil, scoring will be disabled."
205   :group 'gnus-score-default
206   :type '(choice (const :tag "disable")
207                  integer))
208
209 (defcustom gnus-summary-zcore-fuzz 0
210   "*Fuzziness factor for the zcore in the summary buffer.
211 Articles with scores closer than this to `gnus-summary-default-score'
212 will not be marked."
213   :group 'gnus-summary-format
214   :type 'integer)
215
216 (defcustom gnus-simplify-subject-fuzzy-regexp nil
217   "*Strings to be removed when doing fuzzy matches.
218 This can either be a regular expression or list of regular expressions
219 that will be removed from subject strings if fuzzy subject
220 simplification is selected."
221   :group 'gnus-thread
222   :type '(repeat regexp))
223
224 (defcustom gnus-show-threads t
225   "*If non-nil, display threads in summary mode."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-hide-subtree nil
230   "*If non-nil, hide all threads initially.
231 If threads are hidden, you have to run the command
232 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
233 to expose hidden threads."
234   :group 'gnus-thread
235   :type 'boolean)
236
237 (defcustom gnus-thread-hide-killed t
238   "*If non-nil, hide killed threads automatically."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-ignore-subject t
243   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
244 If nil, articles that have different subjects from their parents will
245 start separate threads."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-operation-ignore-subject t
250   "*If non-nil, subjects will be ignored when doing thread commands.
251 This affects commands like `gnus-summary-kill-thread' and
252 `gnus-summary-lower-thread'.
253
254 If this variable is nil, articles in the same thread with different
255 subjects will not be included in the operation in question.  If this
256 variable is `fuzzy', only articles that have subjects that are fuzzily
257 equal will be included."
258   :group 'gnus-thread
259   :type '(choice (const :tag "off" nil)
260                  (const fuzzy)
261                  (sexp :tag "on" t)))
262
263 (defcustom gnus-thread-indent-level 4
264   "*Number that says how much each sub-thread should be indented."
265   :group 'gnus-thread
266   :type 'integer)
267
268 (defcustom gnus-auto-extend-newsgroup t
269   "*If non-nil, extend newsgroup forward and backward when requested."
270   :group 'gnus-summary-choose
271   :type 'boolean)
272
273 (defcustom gnus-auto-select-first t
274   "*If nil, don't select the first unread article when entering a group.
275 If this variable is `best', select the highest-scored unread article
276 in the group.  If t, select the first unread article.
277
278 This variable can also be a function to place point on a likely
279 subject line.  Useful values include `gnus-summary-first-unread-subject',
280 `gnus-summary-first-unread-article' and
281 `gnus-summary-best-unread-article'.
282
283 If you want to prevent automatic selection of the first unread article
284 in some newsgroups, set the variable to nil in
285 `gnus-select-group-hook'."
286   :group 'gnus-group-select
287   :type '(choice (const :tag "none" nil)
288                  (const best)
289                  (sexp :menu-tag "first" t)
290                  (function-item gnus-summary-first-unread-subject)
291                  (function-item gnus-summary-first-unread-article)
292                  (function-item gnus-summary-best-unread-article)))
293
294 (defcustom gnus-dont-select-after-jump-to-other-group nil
295   "If non-nil, don't select the first unread article after entering the
296 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
297 it is depend on the value of `gnus-auto-select-first' whether to select
298 or not."
299   :group 'gnus-group-select
300   :type 'boolean)
301
302 (defcustom gnus-auto-select-next t
303   "*If non-nil, offer to go to the next group from the end of the previous.
304 If the value is t and the next newsgroup is empty, Gnus will exit
305 summary mode and go back to group mode.  If the value is neither nil
306 nor t, Gnus will select the following unread newsgroup.  In
307 particular, if the value is the symbol `quietly', the next unread
308 newsgroup will be selected without any confirmation, and if it is
309 `almost-quietly', the next group will be selected without any
310 confirmation if you are located on the last article in the group.
311 Finally, if this variable is `slightly-quietly', the `Z n' command
312 will go to the next group without confirmation."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "off" nil)
315                  (const quietly)
316                  (const almost-quietly)
317                  (const slightly-quietly)
318                  (sexp :menu-tag "on" t)))
319
320 (defcustom gnus-auto-select-same nil
321   "*If non-nil, select the next article with the same subject.
322 If there are no more articles with the same subject, go to
323 the first unread article."
324   :group 'gnus-summary-maneuvering
325   :type 'boolean)
326
327 (defcustom gnus-summary-check-current nil
328   "*If non-nil, consider the current article when moving.
329 The \"unread\" movement commands will stay on the same line if the
330 current article is unread."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-auto-center-summary t
335   "*If non-nil, always center the current summary buffer.
336 In particular, if `vertical' do only vertical recentering.  If non-nil
337 and non-`vertical', do both horizontal and vertical recentering."
338   :group 'gnus-summary-maneuvering
339   :type '(choice (const :tag "none" nil)
340                  (const vertical)
341                  (integer :tag "height")
342                  (sexp :menu-tag "both" t)))
343
344 (defcustom gnus-show-all-headers nil
345   "*If non-nil, don't hide any headers."
346   :group 'gnus-article-hiding
347   :group 'gnus-article-headers
348   :type 'boolean)
349
350 (defcustom gnus-summary-ignore-duplicates nil
351   "*If non-nil, ignore articles with identical Message-ID headers."
352   :group 'gnus-summary
353   :type 'boolean)
354
355 (defcustom gnus-single-article-buffer t
356   "*If non-nil, display all articles in the same buffer.
357 If nil, each group will get its own article buffer."
358   :group 'gnus-article-various
359   :type 'boolean)
360
361 (defcustom gnus-break-pages t
362   "*If non-nil, do page breaking on articles.
363 The page delimiter is specified by the `gnus-page-delimiter'
364 variable."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-show-mime t
369   "*If non-nil, do mime processing of articles.
370 The articles will simply be fed to the function given by
371 `gnus-article-display-method-for-mime'."
372   :group 'gnus-article-mime
373   :type 'boolean)
374
375 (defcustom gnus-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable.
378 However, whereas `gnus-split-methods' specifies file names as targets,
379 this variable specifies group names."
380   :group 'gnus-summary-mail
381   :type '(repeat (choice (list :value (fun) function)
382                          (cons :value ("" "") regexp (repeat string))
383                          (sexp :value nil))))
384
385 (defcustom gnus-unread-mark ?  ;Whitespace
386   "*Mark used for unread articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-ticked-mark ?!
391   "*Mark used for ticked articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-dormant-mark ??
396   "*Mark used for dormant articles."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-del-mark ?r
401   "*Mark used for del'd articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-read-mark ?R
406   "*Mark used for read articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-expirable-mark ?E
411   "*Mark used for expirable articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-killed-mark ?K
416   "*Mark used for killed articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-souped-mark ?F
421   "*Mark used for killed articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-kill-file-mark ?X
426   "*Mark used for articles killed by kill files."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-low-score-mark ?Y
431   "*Mark used for articles with a low score."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-catchup-mark ?C
436   "*Mark used for articles that are caught up."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-replied-mark ?A
441   "*Mark used for articles that have been replied to."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-cached-mark ?*
446   "*Mark used for articles that are in the cache."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-saved-mark ?S
451   "*Mark used for articles that have been saved to."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-ancient-mark ?O
456   "*Mark used for ancient articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-sparse-mark ?Q
461   "*Mark used for sparsely reffed articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-canceled-mark ?G
466   "*Mark used for canceled articles."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-duplicate-mark ?M
471   "*Mark used for duplicate articles."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-undownloaded-mark ?@
476   "*Mark used for articles that weren't downloaded."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-downloadable-mark ?%
481   "*Mark used for articles that are to be downloaded."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unsendable-mark ?=
486   "*Mark used for articles that won't be sent."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-score-over-mark ?+
491   "*Score mark used for articles with high scores."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-score-below-mark ?-
496   "*Score mark used for articles with low scores."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-empty-thread-mark ?  ;Whitespace
501   "*There is no thread under the article."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-not-empty-thread-mark ?=
506   "*There is a thread under the article."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-view-pseudo-asynchronously nil
511   "*If non-nil, Gnus will view pseudo-articles asynchronously."
512   :group 'gnus-extract-view
513   :type 'boolean)
514
515 (defcustom gnus-auto-expirable-marks
516   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
517         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
518         gnus-souped-mark gnus-duplicate-mark)
519   "*The list of marks converted into expiration if a group is auto-expirable."
520   :group 'gnus-summary
521   :type '(repeat character))
522
523 (defcustom gnus-inhibit-user-auto-expire t
524   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
525   :group 'gnus-summary
526   :type 'boolean)
527
528 (defcustom gnus-view-pseudos nil
529   "*If `automatic', pseudo-articles will be viewed automatically.
530 If `not-confirm', pseudos will be viewed automatically, and the user
531 will not be asked to confirm the command."
532   :group 'gnus-extract-view
533   :type '(choice (const :tag "off" nil)
534                  (const automatic)
535                  (const not-confirm)))
536
537 (defcustom gnus-view-pseudos-separately t
538   "*If non-nil, one pseudo-article will be created for each file to be viewed.
539 If nil, all files that use the same viewing command will be given as a
540 list of parameters to that command."
541   :group 'gnus-extract-view
542   :type 'boolean)
543
544 (defcustom gnus-insert-pseudo-articles t
545   "*If non-nil, insert pseudo-articles when decoding articles."
546   :group 'gnus-extract-view
547   :type 'boolean)
548
549 (defcustom gnus-summary-dummy-line-format
550   "  %(:                          :%) %S\n"
551   "*The format specification for the dummy roots in the summary buffer.
552 It works along the same lines as a normal formatting string,
553 with some simple extensions.
554
555 %S  The subject"
556   :group 'gnus-threading
557   :type 'string)
558
559 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
560   "*The format specification for the summary mode line.
561 It works along the same lines as a normal formatting string,
562 with some simple extensions:
563
564 %G  Group name
565 %p  Unprefixed group name
566 %A  Current article number
567 %z  Current article score
568 %V  Gnus version
569 %U  Number of unread articles in the group
570 %e  Number of unselected articles in the group
571 %Z  A string with unread/unselected article counts
572 %g  Shortish group name
573 %S  Subject of the current article
574 %u  User-defined spec
575 %s  Current score file name
576 %d  Number of dormant articles
577 %r  Number of articles that have been marked as read in this session
578 %E  Number of articles expunged by the score files"
579   :group 'gnus-summary-format
580   :type 'string)
581
582 (defcustom gnus-list-identifiers nil
583   "Regexp that matches list identifiers to be removed from subject.
584 This can also be a list of regexps."
585   :group 'gnus-summary-format
586   :group 'gnus-article-hiding
587   :type '(choice (const :tag "none" nil)
588                  (regexp :value ".*")
589                  (repeat :value (".*") regexp)))
590
591 (defcustom gnus-summary-mark-below 0
592   "*Mark all articles with a score below this variable as read.
593 This variable is local to each summary buffer and usually set by the
594 score file."
595   :group 'gnus-score-default
596   :type 'integer)
597
598 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
599   "*List of functions used for sorting articles in the summary buffer.
600 This variable is only used when not using a threaded display."
601   :group 'gnus-summary-sort
602   :type '(repeat (choice (function-item gnus-article-sort-by-number)
603                          (function-item gnus-article-sort-by-author)
604                          (function-item gnus-article-sort-by-subject)
605                          (function-item gnus-article-sort-by-date)
606                          (function-item gnus-article-sort-by-score)
607                          (function :tag "other"))))
608
609 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
610   "*List of functions used for sorting threads in the summary buffer.
611 By default, threads are sorted by article number.
612
613 Each function takes two threads and return non-nil if the first thread
614 should be sorted before the other.  If you use more than one function,
615 the primary sort function should be the last.  You should probably
616 always include `gnus-thread-sort-by-number' in the list of sorting
617 functions -- preferably first.
618
619 Ready-made functions include `gnus-thread-sort-by-number',
620 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
621 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
622 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
623   :group 'gnus-summary-sort
624   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
625                          (function-item gnus-thread-sort-by-author)
626                          (function-item gnus-thread-sort-by-subject)
627                          (function-item gnus-thread-sort-by-date)
628                          (function-item gnus-thread-sort-by-score)
629                          (function-item gnus-thread-sort-by-total-score)
630                          (function :tag "other"))))
631
632 (defcustom gnus-thread-score-function '+
633   "*Function used for calculating the total score of a thread.
634
635 The function is called with the scores of the article and each
636 subthread and should then return the score of the thread.
637
638 Some functions you can use are `+', `max', or `min'."
639   :group 'gnus-summary-sort
640   :type 'function)
641
642 (defcustom gnus-summary-expunge-below nil
643   "All articles that have a score less than this variable will be expunged.
644 This variable is local to the summary buffers."
645   :group 'gnus-score-default
646   :type '(choice (const :tag "off" nil)
647                  integer))
648
649 (defcustom gnus-thread-expunge-below nil
650   "All threads that have a total score less than this variable will be expunged.
651 See `gnus-thread-score-function' for en explanation of what a
652 \"thread score\" is.
653
654 This variable is local to the summary buffers."
655   :group 'gnus-threading
656   :group 'gnus-score-default
657   :type '(choice (const :tag "off" nil)
658                  integer))
659
660 (defcustom gnus-summary-mode-hook nil
661   "*A hook for Gnus summary mode.
662 This hook is run before any variables are set in the summary buffer."
663   :group 'gnus-summary-various
664   :type 'hook)
665
666 (defcustom gnus-summary-menu-hook nil
667   "*Hook run after the creation of the summary mode menu."
668   :group 'gnus-summary-visual
669   :type 'hook)
670
671 (defcustom gnus-summary-exit-hook nil
672   "*A hook called on exit from the summary buffer.
673 It will be called with point in the group buffer."
674   :group 'gnus-summary-exit
675   :type 'hook)
676
677 (defcustom gnus-summary-prepare-hook nil
678   "*A hook called after the summary buffer has been generated.
679 If you want to modify the summary buffer, you can use this hook."
680   :group 'gnus-summary-various
681   :type 'hook)
682
683 (defcustom gnus-summary-prepared-hook nil
684   "*A hook called as the last thing after the summary buffer has been generated."
685   :group 'gnus-summary-various
686   :type 'hook)
687
688 (defcustom gnus-summary-generate-hook nil
689   "*A hook run just before generating the summary buffer.
690 This hook is commonly used to customize threading variables and the
691 like."
692   :group 'gnus-summary-various
693   :type 'hook)
694
695 (defcustom gnus-select-group-hook nil
696   "*A hook called when a newsgroup is selected.
697
698 If you'd like to simplify subjects like the
699 `gnus-summary-next-same-subject' command does, you can use the
700 following hook:
701
702  (setq gnus-select-group-hook
703       (list
704         (lambda ()
705           (mapcar (lambda (header)
706                      (mail-header-set-subject
707                       header
708                       (gnus-simplify-subject
709                        (mail-header-subject header) 're-only)))
710                   gnus-newsgroup-headers))))"
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-select-article-hook nil
715   "*A hook called when an article is selected."
716   :group 'gnus-summary-choose
717   :type 'hook)
718
719 (defcustom gnus-visual-mark-article-hook
720   (list 'gnus-highlight-selected-summary)
721   "*Hook run after selecting an article in the summary buffer.
722 It is meant to be used for highlighting the article in some way.  It
723 is not run if `gnus-visual' is nil."
724   :group 'gnus-summary-visual
725   :type 'hook)
726
727 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
728   "*A hook called before parsing the headers."
729   :group 'gnus-various
730   :type 'hook)
731
732 (defcustom gnus-exit-group-hook nil
733   "*A hook called when exiting summary mode.
734 This hook is not called from the non-updating exit commands like `Q'."
735   :group 'gnus-various
736   :type 'hook)
737
738 (defcustom gnus-summary-update-hook
739   (list 'gnus-summary-highlight-line)
740   "*A hook called when a summary line is changed.
741 The hook will not be called if `gnus-visual' is nil.
742
743 The default function `gnus-summary-highlight-line' will
744 highlight the line according to the `gnus-summary-highlight'
745 variable."
746   :group 'gnus-summary-visual
747   :type 'hook)
748
749 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
750   "*A hook called when an article is selected for the first time.
751 The hook is intended to mark an article as read (or unread)
752 automatically when it is selected."
753   :group 'gnus-summary-choose
754   :type 'hook)
755
756 (defcustom gnus-group-no-more-groups-hook nil
757   "*A hook run when returning to group mode having no more (unread) groups."
758   :group 'gnus-group-select
759   :type 'hook)
760
761 (defcustom gnus-ps-print-hook nil
762   "*A hook run before ps-printing something from Gnus."
763   :group 'gnus-summary
764   :type 'hook)
765
766 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
767   "Face used for highlighting the current article in the summary buffer."
768   :group 'gnus-summary-visual
769   :type 'face)
770
771 (defcustom gnus-summary-highlight
772   '(((= mark gnus-canceled-mark)
773      . gnus-summary-cancelled-face)
774     ((and (> score default)
775           (or (= mark gnus-dormant-mark)
776               (= mark gnus-ticked-mark)))
777      . gnus-summary-high-ticked-face)
778     ((and (< score default)
779           (or (= mark gnus-dormant-mark)
780               (= mark gnus-ticked-mark)))
781      . gnus-summary-low-ticked-face)
782     ((or (= mark gnus-dormant-mark)
783          (= mark gnus-ticked-mark))
784      . gnus-summary-normal-ticked-face)
785     ((and (> score default) (= mark gnus-ancient-mark))
786      . gnus-summary-high-ancient-face)
787     ((and (< score default) (= mark gnus-ancient-mark))
788      . gnus-summary-low-ancient-face)
789     ((= mark gnus-ancient-mark)
790      . gnus-summary-normal-ancient-face)
791     ((and (> score default) (= mark gnus-unread-mark))
792      . gnus-summary-high-unread-face)
793     ((and (< score default) (= mark gnus-unread-mark))
794      . gnus-summary-low-unread-face)
795     ((and (memq article gnus-newsgroup-incorporated)
796           (= mark gnus-unread-mark))
797      . gnus-summary-incorporated-face)
798     ((= mark gnus-unread-mark)
799      . gnus-summary-normal-unread-face)
800     ((and (> score default) (memq mark (list gnus-downloadable-mark
801                                              gnus-undownloaded-mark)))
802      . gnus-summary-high-unread-face)
803     ((and (< score default) (memq mark (list gnus-downloadable-mark
804                                              gnus-undownloaded-mark)))
805      . gnus-summary-low-unread-face)
806     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
807      . gnus-summary-normal-unread-face)
808     ((> score default)
809      . gnus-summary-high-read-face)
810     ((< score default)
811      . gnus-summary-low-read-face)
812     (t
813      . gnus-summary-normal-read-face))
814   "*Controls the highlighting of summary buffer lines.
815
816 A list of (FORM . FACE) pairs.  When deciding how a a particular
817 summary line should be displayed, each form is evaluated.  The content
818 of the face field after the first true form is used.  You can change
819 how those summary lines are displayed, by editing the face field.
820
821 You can use the following variables in the FORM field.
822
823 score:   The articles score
824 default: The default article score.
825 below:   The score below which articles are automatically marked as read.
826 mark:    The articles mark."
827   :group 'gnus-summary-visual
828   :type '(repeat (cons (sexp :tag "Form" nil)
829                        face)))
830
831 (defcustom gnus-alter-header-function nil
832   "Function called to allow alteration of article header structures.
833 The function is called with one parameter, the article header vector,
834 which it may alter in any way.")
835
836 (defvar gnus-decode-encoded-word-function
837   (mime-find-field-decoder 'From 'nov)
838   "Variable that says which function should be used to decode a string with encoded words.")
839
840 (defcustom gnus-extra-headers nil
841   "*Extra headers to parse."
842   :group 'gnus-summary
843   :type '(repeat symbol))
844
845 (defcustom gnus-ignored-from-addresses
846   (and user-mail-address (regexp-quote user-mail-address))
847   "*Regexp of From headers that may be suppressed in favor of To headers."
848   :group 'gnus-summary
849   :type 'regexp)
850
851 (defcustom gnus-group-charset-alist
852   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
853     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
854     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
855     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
856     ("^relcom\\>" koi8-r)
857     ("^fido7\\>" koi8-r)
858     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
859     ("^israel\\>" iso-8859-1)
860     ("^han\\>" euc-kr)
861     ("^alt.chinese.text.big5\\>" chinese-big5)
862     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
863     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
864     (".*" iso-8859-1))
865   "Alist of regexps (to match group names) and default charsets to be used when reading."
866   :type '(repeat (list (regexp :tag "Group")
867                        (symbol :tag "Charset")))
868   :group 'gnus-charset)
869
870 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
871   "List of charsets that should be ignored.
872 When these charsets are used in the \"charset\" parameter, the
873 default charset will be used instead."
874   :type '(repeat symbol)
875   :group 'gnus-charset)
876
877 (defcustom gnus-group-ignored-charsets-alist
878   '(("alt\\.chinese\\.text" iso-8859-1))
879   "Alist of regexps (to match group names) and charsets that should be ignored.
880 When these charsets are used in the \"charset\" parameter, the
881 default charset will be used instead."
882   :type '(repeat (cons (regexp :tag "Group")
883                        (repeat symbol)))
884   :group 'gnus-charset)
885
886 (defcustom gnus-group-highlight-words-alist nil
887   "Alist of group regexps and highlight regexps.
888 This variable uses the same syntax as `gnus-emphasis-alist'."
889   :type '(repeat (cons (regexp :tag "Group")
890                        (repeat (list (regexp :tag "Highlight regexp")
891                                      (number :tag "Group for entire word" 0)
892                                      (number :tag "Group for displayed part" 0)
893                                      (symbol :tag "Face"
894                                              gnus-emphasis-highlight-words)))))
895   :group 'gnus-summary-visual)
896
897 (defcustom gnus-use-wheel nil
898   "Use Intelli-mouse on summary movement"
899   :type 'boolean
900   :group 'gnus-summary-maneuvering)
901
902 (defcustom gnus-wheel-scroll-amount '(5 . 1)
903   "Amount to scroll messages by spinning the mouse wheel.
904 This is actually a cons cell, where the first item is the amount to scroll
905 on a normal wheel event, and the second is the amount to scroll when the
906 wheel is moved with the shift key depressed."
907   :type '(cons (integer :tag "Shift") integer)
908   :group 'gnus-summary-maneuvering)
909
910 (defcustom gnus-wheel-edge-resistance 2
911   "How hard it should be to change the current article
912 by moving the mouse over the edge of the article window."
913   :type 'integer
914   :group 'gnus-summary-maneuvering)
915
916 (defcustom gnus-summary-show-article-charset-alist
917   nil
918   "Alist of number and charset.
919 The article will be shown with the charset corresponding to the
920 numbered argument.
921 For example: ((1 . cn-gb-2312) (2 . big5))."
922   :type '(repeat (cons (number :tag "Argument" 1)
923                        (symbol :tag "Charset")))
924   :group 'gnus-charset)
925
926 (defcustom gnus-preserve-marks t
927   "Whether marks are preserved when moving, copying and respooling messages."
928   :type 'boolean
929   :group 'gnus-summary-marks)
930
931 (defcustom gnus-alter-articles-to-read-function nil
932   "Function to be called to alter the list of articles to be selected."
933   :type 'function
934   :group 'gnus-summary)
935
936 (defcustom gnus-orphan-score nil
937   "*All orphans get this score added.  Set in the score file."
938   :group 'gnus-score-default
939   :type '(choice (const nil)
940                  integer))
941
942 (defcustom gnus-summary-save-parts-default-mime "image/.*"
943   "*A regexp to match MIME parts when saving multiple parts of a message
944 with gnus-summary-save-parts (X m). This regexp will be used by default
945 when prompting the user for which type of files to save."
946   :group 'gnus-summary
947   :type 'regexp)
948
949
950 ;;; Internal variables
951
952 (defvar gnus-article-mime-handles nil)
953 (defvar gnus-article-decoded-p nil)
954 (defvar gnus-article-charset nil)
955 (defvar gnus-article-ignored-charsets nil)
956 (defvar gnus-scores-exclude-files nil)
957 (defvar gnus-page-broken nil)
958 (defvar gnus-inhibit-mime-unbuttonizing nil)
959
960 (defvar gnus-original-article nil)
961 (defvar gnus-article-internal-prepare-hook nil)
962 (defvar gnus-newsgroup-process-stack nil)
963
964 (defvar gnus-thread-indent-array nil)
965 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
966 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
967   "Function called to sort the articles within a thread after it has been gathered together.")
968
969 (defvar gnus-summary-save-parts-type-history nil)
970 (defvar gnus-summary-save-parts-last-directory nil)
971
972 ;; Avoid highlighting in kill files.
973 (defvar gnus-summary-inhibit-highlight nil)
974 (defvar gnus-newsgroup-selected-overlay nil)
975 (defvar gnus-inhibit-limiting nil)
976 (defvar gnus-newsgroup-adaptive-score-file nil)
977 (defvar gnus-current-score-file nil)
978 (defvar gnus-current-move-group nil)
979 (defvar gnus-current-copy-group nil)
980 (defvar gnus-current-crosspost-group nil)
981
982 (defvar gnus-newsgroup-dependencies nil)
983 (defvar gnus-newsgroup-adaptive nil)
984 (defvar gnus-summary-display-article-function nil)
985 (defvar gnus-summary-highlight-line-function nil
986   "Function called after highlighting a summary line.")
987
988 (defvar gnus-summary-line-format-alist
989   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
990     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
991     (?s gnus-tmp-subject-or-nil ?s)
992     (?n gnus-tmp-name ?s)
993     (?A (std11-address-string
994          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
995     (?a (or (std11-full-name-string
996              (car (mime-entity-read-field gnus-tmp-header 'From)))
997             gnus-tmp-from) ?s)
998     (?F gnus-tmp-from ?s)
999     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1000     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1001     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1002     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1003     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1004     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1005     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1006     (?L gnus-tmp-lines ?d)
1007     (?I gnus-tmp-indentation ?s)
1008     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1009     (?R gnus-tmp-replied ?c)
1010     (?\[ gnus-tmp-opening-bracket ?c)
1011     (?\] gnus-tmp-closing-bracket ?c)
1012     (?\> (make-string gnus-tmp-level ? ) ?s)
1013     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1014     (?i gnus-tmp-score ?d)
1015     (?z gnus-tmp-score-char ?c)
1016     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1017     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1018     (?U gnus-tmp-unread ?c)
1019     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1020     (?t (gnus-summary-number-of-articles-in-thread
1021          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1022         ?d)
1023     (?e (gnus-summary-number-of-articles-in-thread
1024          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1025         ?c)
1026     (?u gnus-tmp-user-defined ?s)
1027     (?P (gnus-pick-line-number) ?d))
1028   "An alist of format specifications that can appear in summary lines.
1029 These are paired with what variables they correspond with, along with
1030 the type of the variable (string, integer, character, etc).")
1031
1032 (defvar gnus-summary-dummy-line-format-alist
1033   `((?S gnus-tmp-subject ?s)
1034     (?N gnus-tmp-number ?d)
1035     (?u gnus-tmp-user-defined ?s)))
1036
1037 (defvar gnus-summary-mode-line-format-alist
1038   `((?G gnus-tmp-group-name ?s)
1039     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1040     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1041     (?A gnus-tmp-article-number ?d)
1042     (?Z gnus-tmp-unread-and-unselected ?s)
1043     (?V gnus-version ?s)
1044     (?U gnus-tmp-unread-and-unticked ?d)
1045     (?S gnus-tmp-subject ?s)
1046     (?e gnus-tmp-unselected ?d)
1047     (?u gnus-tmp-user-defined ?s)
1048     (?d (length gnus-newsgroup-dormant) ?d)
1049     (?t (length gnus-newsgroup-marked) ?d)
1050     (?r (length gnus-newsgroup-reads) ?d)
1051     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1052     (?E gnus-newsgroup-expunged-tally ?d)
1053     (?s (gnus-current-score-file-nondirectory) ?s)))
1054
1055 (defvar gnus-last-search-regexp nil
1056   "Default regexp for article search command.")
1057
1058 (defvar gnus-summary-search-article-matched-data nil
1059   "Last matched data of article search command.  It is the local variable
1060 in `gnus-article-buffer' which consists of the list of start position,
1061 end position and text.")
1062
1063 (defvar gnus-last-shell-command nil
1064   "Default shell command on article.")
1065
1066 (defvar gnus-newsgroup-begin nil)
1067 (defvar gnus-newsgroup-end nil)
1068 (defvar gnus-newsgroup-last-rmail nil)
1069 (defvar gnus-newsgroup-last-mail nil)
1070 (defvar gnus-newsgroup-last-folder nil)
1071 (defvar gnus-newsgroup-last-file nil)
1072 (defvar gnus-newsgroup-auto-expire nil)
1073 (defvar gnus-newsgroup-active nil)
1074
1075 (defvar gnus-newsgroup-data nil)
1076 (defvar gnus-newsgroup-data-reverse nil)
1077 (defvar gnus-newsgroup-limit nil)
1078 (defvar gnus-newsgroup-limits nil)
1079
1080 (defvar gnus-newsgroup-unreads nil
1081   "List of unread articles in the current newsgroup.")
1082
1083 (defvar gnus-newsgroup-unselected nil
1084   "List of unselected unread articles in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-reads nil
1087   "Alist of read articles and article marks in the current newsgroup.")
1088
1089 (defvar gnus-newsgroup-expunged-tally nil)
1090
1091 (defvar gnus-newsgroup-marked nil
1092   "List of ticked articles in the current newsgroup (a subset of unread art).")
1093
1094 (defvar gnus-newsgroup-killed nil
1095   "List of ranges of articles that have been through the scoring process.")
1096
1097 (defvar gnus-newsgroup-cached nil
1098   "List of articles that come from the article cache.")
1099
1100 (defvar gnus-newsgroup-saved nil
1101   "List of articles that have been saved.")
1102
1103 (defvar gnus-newsgroup-kill-headers nil)
1104
1105 (defvar gnus-newsgroup-replied nil
1106   "List of articles that have been replied to in the current newsgroup.")
1107
1108 (defvar gnus-newsgroup-expirable nil
1109   "List of articles in the current newsgroup that can be expired.")
1110
1111 (defvar gnus-newsgroup-processable nil
1112   "List of articles in the current newsgroup that can be processed.")
1113
1114 (defvar gnus-newsgroup-downloadable nil
1115   "List of articles in the current newsgroup that can be processed.")
1116
1117 (defvar gnus-newsgroup-undownloaded nil
1118   "List of articles in the current newsgroup that haven't been downloaded..")
1119
1120 (defvar gnus-newsgroup-unsendable nil
1121   "List of articles in the current newsgroup that won't be sent.")
1122
1123 (defvar gnus-newsgroup-bookmarks nil
1124   "List of articles in the current newsgroup that have bookmarks.")
1125
1126 (defvar gnus-newsgroup-dormant nil
1127   "List of dormant articles in the current newsgroup.")
1128
1129 (defvar gnus-newsgroup-scored nil
1130   "List of scored articles in the current newsgroup.")
1131
1132 (defvar gnus-newsgroup-incorporated nil
1133   "List of incorporated articles in the current newsgroup.")
1134
1135 (defvar gnus-newsgroup-headers nil
1136   "List of article headers in the current newsgroup.")
1137
1138 (defvar gnus-newsgroup-threads nil)
1139
1140 (defvar gnus-newsgroup-prepared nil
1141   "Whether the current group has been prepared properly.")
1142
1143 (defvar gnus-newsgroup-ancient nil
1144   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1145
1146 (defvar gnus-newsgroup-sparse nil)
1147
1148 (defvar gnus-current-article nil)
1149 (defvar gnus-article-current nil)
1150 (defvar gnus-current-headers nil)
1151 (defvar gnus-have-all-headers nil)
1152 (defvar gnus-last-article nil)
1153 (defvar gnus-newsgroup-history nil)
1154 (defvar gnus-newsgroup-charset nil)
1155 (defvar gnus-newsgroup-ephemeral-charset nil)
1156 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1157
1158 (defvar gnus-article-before-search nil)
1159
1160 (defconst gnus-summary-local-variables
1161   '(gnus-newsgroup-name
1162     gnus-newsgroup-begin gnus-newsgroup-end
1163     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1164     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1165     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1166     gnus-newsgroup-unselected gnus-newsgroup-marked
1167     gnus-newsgroup-reads gnus-newsgroup-saved
1168     gnus-newsgroup-replied gnus-newsgroup-expirable
1169     gnus-newsgroup-processable gnus-newsgroup-killed
1170     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1171     gnus-newsgroup-unsendable
1172     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1173     gnus-newsgroup-headers gnus-newsgroup-threads
1174     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1175     gnus-current-article gnus-current-headers gnus-have-all-headers
1176     gnus-last-article gnus-article-internal-prepare-hook
1177     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1178     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1179     gnus-thread-expunge-below
1180     gnus-score-alist gnus-current-score-file
1181     (gnus-summary-expunge-below . global)
1182     (gnus-summary-mark-below . global)
1183     (gnus-orphan-score . global)
1184     gnus-newsgroup-active gnus-scores-exclude-files
1185     gnus-newsgroup-history gnus-newsgroup-ancient
1186     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1187     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1188     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1189     (gnus-newsgroup-expunged-tally . 0)
1190     gnus-cache-removable-articles gnus-newsgroup-cached
1191     gnus-newsgroup-data gnus-newsgroup-data-reverse
1192     gnus-newsgroup-limit gnus-newsgroup-limits
1193     gnus-newsgroup-charset
1194     gnus-newsgroup-incorporated)
1195   "Variables that are buffer-local to the summary buffers.")
1196
1197 (defvar gnus-newsgroup-variables nil
1198   "Variables that have separate values in the newsgroups.")
1199
1200 ;; Byte-compiler warning.
1201 (defvar gnus-article-mode-map)
1202
1203 ;; Subject simplification.
1204
1205 (defun gnus-simplify-whitespace (str)
1206   "Remove excessive whitespace from STR."
1207   (let ((mystr str))
1208     ;; Multiple spaces.
1209     (while (string-match "[ \t][ \t]+" mystr)
1210       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1211                           " "
1212                           (substring mystr (match-end 0)))))
1213     ;; Leading spaces.
1214     (when (string-match "^[ \t]+" mystr)
1215       (setq mystr (substring mystr (match-end 0))))
1216     ;; Trailing spaces.
1217     (when (string-match "[ \t]+$" mystr)
1218       (setq mystr (substring mystr 0 (match-beginning 0))))
1219     mystr))
1220
1221 (defsubst gnus-simplify-subject-re (subject)
1222   "Remove \"Re:\" from subject lines."
1223   (if (string-match "^[Rr][Ee]: *" subject)
1224       (substring subject (match-end 0))
1225     subject))
1226
1227 (defun gnus-simplify-subject (subject &optional re-only)
1228   "Remove `Re:' and words in parentheses.
1229 If RE-ONLY is non-nil, strip leading `Re:'s only."
1230   (let ((case-fold-search t))           ;Ignore case.
1231     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1232     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1233       (setq subject (substring subject (match-end 0))))
1234     ;; Remove uninteresting prefixes.
1235     (when (and (not re-only)
1236                gnus-simplify-ignored-prefixes
1237                (string-match gnus-simplify-ignored-prefixes subject))
1238       (setq subject (substring subject (match-end 0))))
1239     ;; Remove words in parentheses from end.
1240     (unless re-only
1241       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1242         (setq subject (substring subject 0 (match-beginning 0)))))
1243     ;; Return subject string.
1244     subject))
1245
1246 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1247 ;; all whitespace.
1248 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1249   (goto-char (point-min))
1250   (while (re-search-forward regexp nil t)
1251     (replace-match (or newtext ""))))
1252
1253 (defun gnus-simplify-buffer-fuzzy ()
1254   "Simplify string in the buffer fuzzily.
1255 The string in the accessible portion of the current buffer is simplified.
1256 It is assumed to be a single-line subject.
1257 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1258 matter is removed.  Additional things can be deleted by setting
1259 `gnus-simplify-subject-fuzzy-regexp'."
1260   (let ((case-fold-search t)
1261         (modified-tick))
1262     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1263
1264     (while (not (eq modified-tick (buffer-modified-tick)))
1265       (setq modified-tick (buffer-modified-tick))
1266       (cond
1267        ((listp gnus-simplify-subject-fuzzy-regexp)
1268         (mapcar 'gnus-simplify-buffer-fuzzy-step
1269                 gnus-simplify-subject-fuzzy-regexp))
1270        (gnus-simplify-subject-fuzzy-regexp
1271         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1272       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1273       (gnus-simplify-buffer-fuzzy-step
1274        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1275       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1276
1277     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1278     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1279     (gnus-simplify-buffer-fuzzy-step " $")
1280     (gnus-simplify-buffer-fuzzy-step "^ +")))
1281
1282 (defun gnus-simplify-subject-fuzzy (subject)
1283   "Simplify a subject string fuzzily.
1284 See `gnus-simplify-buffer-fuzzy' for details."
1285   (save-excursion
1286     (gnus-set-work-buffer)
1287     (let ((case-fold-search t))
1288       ;; Remove uninteresting prefixes.
1289       (when (and gnus-simplify-ignored-prefixes
1290                  (string-match gnus-simplify-ignored-prefixes subject))
1291         (setq subject (substring subject (match-end 0))))
1292       (insert subject)
1293       (inline (gnus-simplify-buffer-fuzzy))
1294       (buffer-string))))
1295
1296 (defsubst gnus-simplify-subject-fully (subject)
1297   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1298   (cond
1299    (gnus-simplify-subject-functions
1300     (gnus-map-function gnus-simplify-subject-functions subject))
1301    ((null gnus-summary-gather-subject-limit)
1302     (gnus-simplify-subject-re subject))
1303    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1304     (gnus-simplify-subject-fuzzy subject))
1305    ((numberp gnus-summary-gather-subject-limit)
1306     (gnus-limit-string (gnus-simplify-subject-re subject)
1307                        gnus-summary-gather-subject-limit))
1308    (t
1309     subject)))
1310
1311 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1312   "Check whether two subjects are equal.
1313 If optional argument simple-first is t, first argument is already
1314 simplified."
1315   (cond
1316    ((null simple-first)
1317     (equal (gnus-simplify-subject-fully s1)
1318            (gnus-simplify-subject-fully s2)))
1319    (t
1320     (equal s1
1321            (gnus-simplify-subject-fully s2)))))
1322
1323 (defun gnus-summary-bubble-group ()
1324   "Increase the score of the current group.
1325 This is a handy function to add to `gnus-summary-exit-hook' to
1326 increase the score of each group you read."
1327   (gnus-group-add-score gnus-newsgroup-name))
1328
1329 \f
1330 ;;;
1331 ;;; Gnus summary mode
1332 ;;;
1333
1334 (put 'gnus-summary-mode 'mode-class 'special)
1335
1336 (when t
1337   ;; Non-orthogonal keys
1338
1339   (gnus-define-keys gnus-summary-mode-map
1340     " " gnus-summary-next-page
1341     "\177" gnus-summary-prev-page
1342     [delete] gnus-summary-prev-page
1343     [backspace] gnus-summary-prev-page
1344     "\r" gnus-summary-scroll-up
1345     "\M-\r" gnus-summary-scroll-down
1346     "n" gnus-summary-next-unread-article
1347     "p" gnus-summary-prev-unread-article
1348     "N" gnus-summary-next-article
1349     "P" gnus-summary-prev-article
1350     "\M-\C-n" gnus-summary-next-same-subject
1351     "\M-\C-p" gnus-summary-prev-same-subject
1352     "\M-n" gnus-summary-next-unread-subject
1353     "\M-p" gnus-summary-prev-unread-subject
1354     "." gnus-summary-first-unread-article
1355     "," gnus-summary-best-unread-article
1356     "\M-s" gnus-summary-search-article-forward
1357     "\M-r" gnus-summary-search-article-backward
1358     "<" gnus-summary-beginning-of-article
1359     ">" gnus-summary-end-of-article
1360     "j" gnus-summary-goto-article
1361     "^" gnus-summary-refer-parent-article
1362     "\M-^" gnus-summary-refer-article
1363     "u" gnus-summary-tick-article-forward
1364     "!" gnus-summary-tick-article-forward
1365     "U" gnus-summary-tick-article-backward
1366     "d" gnus-summary-mark-as-read-forward
1367     "D" gnus-summary-mark-as-read-backward
1368     "E" gnus-summary-mark-as-expirable
1369     "\M-u" gnus-summary-clear-mark-forward
1370     "\M-U" gnus-summary-clear-mark-backward
1371     "k" gnus-summary-kill-same-subject-and-select
1372     "\C-k" gnus-summary-kill-same-subject
1373     "\M-\C-k" gnus-summary-kill-thread
1374     "\M-\C-l" gnus-summary-lower-thread
1375     "e" gnus-summary-edit-article
1376     "#" gnus-summary-mark-as-processable
1377     "\M-#" gnus-summary-unmark-as-processable
1378     "\M-\C-t" gnus-summary-toggle-threads
1379     "\M-\C-s" gnus-summary-show-thread
1380     "\M-\C-h" gnus-summary-hide-thread
1381     "\M-\C-f" gnus-summary-next-thread
1382     "\M-\C-b" gnus-summary-prev-thread
1383     [(meta down)] gnus-summary-next-thread
1384     [(meta up)] gnus-summary-prev-thread
1385     "\M-\C-u" gnus-summary-up-thread
1386     "\M-\C-d" gnus-summary-down-thread
1387     "&" gnus-summary-execute-command
1388     "c" gnus-summary-catchup-and-exit
1389     "\C-w" gnus-summary-mark-region-as-read
1390     "\C-t" gnus-summary-toggle-truncation
1391     "?" gnus-summary-mark-as-dormant
1392     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1393     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1394     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1395     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1396     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1397     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1398     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1399     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1400     "=" gnus-summary-expand-window
1401     "\C-x\C-s" gnus-summary-reselect-current-group
1402     "\M-g" gnus-summary-rescan-group
1403     "w" gnus-summary-stop-page-breaking
1404     "\C-c\C-r" gnus-summary-caesar-message
1405     "\M-t" gnus-summary-toggle-mime
1406     "f" gnus-summary-followup
1407     "F" gnus-summary-followup-with-original
1408     "C" gnus-summary-cancel-article
1409     "r" gnus-summary-reply
1410     "R" gnus-summary-reply-with-original
1411     "\C-c\C-f" gnus-summary-mail-forward
1412     "o" gnus-summary-save-article
1413     "\C-o" gnus-summary-save-article-mail
1414     "|" gnus-summary-pipe-output
1415     "\M-k" gnus-summary-edit-local-kill
1416     "\M-K" gnus-summary-edit-global-kill
1417     ;; "V" gnus-version
1418     "\C-c\C-d" gnus-summary-describe-group
1419     "q" gnus-summary-exit
1420     "Q" gnus-summary-exit-no-update
1421     "\C-c\C-i" gnus-info-find-node
1422     gnus-mouse-2 gnus-mouse-pick-article
1423     "m" gnus-summary-mail-other-window
1424     "a" gnus-summary-post-news
1425     "x" gnus-summary-limit-to-unread
1426     "s" gnus-summary-isearch-article
1427     "t" gnus-article-toggle-headers
1428     "g" gnus-summary-show-article
1429     "l" gnus-summary-goto-last-article
1430     "v" gnus-summary-preview-mime-message
1431     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1432     "\C-d" gnus-summary-enter-digest-group
1433     "\M-\C-d" gnus-summary-read-document
1434     "\M-\C-e" gnus-summary-edit-parameters
1435     "\M-\C-a" gnus-summary-customize-parameters
1436     "\C-c\C-b" gnus-bug
1437     "*" gnus-cache-enter-article
1438     "\M-*" gnus-cache-remove-article
1439     "\M-&" gnus-summary-universal-argument
1440     "\C-l" gnus-recenter
1441     "I" gnus-summary-increase-score
1442     "L" gnus-summary-lower-score
1443     "\M-i" gnus-symbolic-argument
1444     "h" gnus-summary-select-article-buffer
1445
1446     "V" gnus-summary-score-map
1447     "X" gnus-uu-extract-map
1448     "S" gnus-summary-send-map)
1449
1450   ;; Sort of orthogonal keymap
1451   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1452     "t" gnus-summary-tick-article-forward
1453     "!" gnus-summary-tick-article-forward
1454     "d" gnus-summary-mark-as-read-forward
1455     "r" gnus-summary-mark-as-read-forward
1456     "c" gnus-summary-clear-mark-forward
1457     " " gnus-summary-clear-mark-forward
1458     "e" gnus-summary-mark-as-expirable
1459     "x" gnus-summary-mark-as-expirable
1460     "?" gnus-summary-mark-as-dormant
1461     "b" gnus-summary-set-bookmark
1462     "B" gnus-summary-remove-bookmark
1463     "#" gnus-summary-mark-as-processable
1464     "\M-#" gnus-summary-unmark-as-processable
1465     "S" gnus-summary-limit-include-expunged
1466     "C" gnus-summary-catchup
1467     "H" gnus-summary-catchup-to-here
1468     "\C-c" gnus-summary-catchup-all
1469     "k" gnus-summary-kill-same-subject-and-select
1470     "K" gnus-summary-kill-same-subject
1471     "P" gnus-uu-mark-map)
1472
1473   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1474     "c" gnus-summary-clear-above
1475     "u" gnus-summary-tick-above
1476     "m" gnus-summary-mark-above
1477     "k" gnus-summary-kill-below)
1478
1479   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1480     "/" gnus-summary-limit-to-subject
1481     "n" gnus-summary-limit-to-articles
1482     "w" gnus-summary-pop-limit
1483     "s" gnus-summary-limit-to-subject
1484     "a" gnus-summary-limit-to-author
1485     "u" gnus-summary-limit-to-unread
1486     "m" gnus-summary-limit-to-marks
1487     "M" gnus-summary-limit-exclude-marks
1488     "v" gnus-summary-limit-to-score
1489     "*" gnus-summary-limit-include-cached
1490     "D" gnus-summary-limit-include-dormant
1491     "T" gnus-summary-limit-include-thread
1492     "d" gnus-summary-limit-exclude-dormant
1493     "t" gnus-summary-limit-to-age
1494     "x" gnus-summary-limit-to-extra
1495     "E" gnus-summary-limit-include-expunged
1496     "c" gnus-summary-limit-exclude-childless-dormant
1497     "C" gnus-summary-limit-mark-excluded-as-read)
1498
1499   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1500     "n" gnus-summary-next-unread-article
1501     "p" gnus-summary-prev-unread-article
1502     "N" gnus-summary-next-article
1503     "P" gnus-summary-prev-article
1504     "\C-n" gnus-summary-next-same-subject
1505     "\C-p" gnus-summary-prev-same-subject
1506     "\M-n" gnus-summary-next-unread-subject
1507     "\M-p" gnus-summary-prev-unread-subject
1508     "f" gnus-summary-first-unread-article
1509     "b" gnus-summary-best-unread-article
1510     "j" gnus-summary-goto-article
1511     "g" gnus-summary-goto-subject
1512     "l" gnus-summary-goto-last-article
1513     "o" gnus-summary-pop-article)
1514
1515   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1516     "k" gnus-summary-kill-thread
1517     "l" gnus-summary-lower-thread
1518     "i" gnus-summary-raise-thread
1519     "T" gnus-summary-toggle-threads
1520     "t" gnus-summary-rethread-current
1521     "^" gnus-summary-reparent-thread
1522     "s" gnus-summary-show-thread
1523     "S" gnus-summary-show-all-threads
1524     "h" gnus-summary-hide-thread
1525     "H" gnus-summary-hide-all-threads
1526     "n" gnus-summary-next-thread
1527     "p" gnus-summary-prev-thread
1528     "u" gnus-summary-up-thread
1529     "o" gnus-summary-top-thread
1530     "d" gnus-summary-down-thread
1531     "#" gnus-uu-mark-thread
1532     "\M-#" gnus-uu-unmark-thread)
1533
1534   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1535     "g" gnus-summary-prepare
1536     "c" gnus-summary-insert-cached-articles)
1537
1538   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1539     "c" gnus-summary-catchup-and-exit
1540     "C" gnus-summary-catchup-all-and-exit
1541     "E" gnus-summary-exit-no-update
1542     "J" gnus-summary-jump-to-other-group
1543     "Q" gnus-summary-exit
1544     "Z" gnus-summary-exit
1545     "n" gnus-summary-catchup-and-goto-next-group
1546     "R" gnus-summary-reselect-current-group
1547     "G" gnus-summary-rescan-group
1548     "N" gnus-summary-next-group
1549     "s" gnus-summary-save-newsrc
1550     "P" gnus-summary-prev-group)
1551
1552   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1553     " " gnus-summary-next-page
1554     "n" gnus-summary-next-page
1555     "\177" gnus-summary-prev-page
1556     [delete] gnus-summary-prev-page
1557     "p" gnus-summary-prev-page
1558     "\r" gnus-summary-scroll-up
1559     "\M-\r" gnus-summary-scroll-down
1560     "<" gnus-summary-beginning-of-article
1561     ">" gnus-summary-end-of-article
1562     "b" gnus-summary-beginning-of-article
1563     "e" gnus-summary-end-of-article
1564     "^" gnus-summary-refer-parent-article
1565     "r" gnus-summary-refer-parent-article
1566     "D" gnus-summary-enter-digest-group
1567     "R" gnus-summary-refer-references
1568     "T" gnus-summary-refer-thread
1569     "g" gnus-summary-show-article
1570     "s" gnus-summary-isearch-article
1571     "P" gnus-summary-print-article
1572     "t" gnus-article-babel
1573     "d" gnus-summary-decrypt-article
1574     "v" gnus-summary-verify-article)
1575
1576   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1577     "b" gnus-article-add-buttons
1578     "B" gnus-article-add-buttons-to-head
1579     "o" gnus-article-treat-overstrike
1580     "e" gnus-article-emphasize
1581     "w" gnus-article-fill-cited-article
1582     "Q" gnus-article-fill-long-lines
1583     "C" gnus-article-capitalize-sentences
1584     "c" gnus-article-remove-cr
1585     "Z" gnus-article-decode-HZ
1586     "f" gnus-article-display-x-face
1587     "l" gnus-summary-stop-page-breaking
1588     "r" gnus-summary-caesar-message
1589     "t" gnus-article-toggle-headers
1590     "v" gnus-summary-verbose-headers
1591     "m" gnus-summary-toggle-mime
1592     "H" gnus-article-strip-headers-in-body
1593     "p" gnus-article-verify-x-pgp-sig
1594     "d" gnus-article-treat-dumbquotes
1595     "s" gnus-smiley-display)
1596
1597   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1598     "a" gnus-article-hide
1599     "h" gnus-article-toggle-headers
1600     "b" gnus-article-hide-boring-headers
1601     "s" gnus-article-hide-signature
1602     "c" gnus-article-hide-citation
1603     "C" gnus-article-hide-citation-in-followups
1604     "l" gnus-article-hide-list-identifiers
1605     "p" gnus-article-hide-pgp
1606     "B" gnus-article-strip-banner
1607     "P" gnus-article-hide-pem
1608     "\C-c" gnus-article-hide-citation-maybe)
1609
1610   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1611     "a" gnus-article-highlight
1612     "h" gnus-article-highlight-headers
1613     "c" gnus-article-highlight-citation
1614     "s" gnus-article-highlight-signature)
1615
1616   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1617     "z" gnus-article-date-ut
1618     "u" gnus-article-date-ut
1619     "l" gnus-article-date-local
1620     "e" gnus-article-date-lapsed
1621     "o" gnus-article-date-original
1622     "i" gnus-article-date-iso8601
1623     "s" gnus-article-date-user)
1624
1625   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1626     "t" gnus-article-remove-trailing-blank-lines
1627     "l" gnus-article-strip-leading-blank-lines
1628     "m" gnus-article-strip-multiple-blank-lines
1629     "a" gnus-article-strip-blank-lines
1630     "A" gnus-article-strip-all-blank-lines
1631     "s" gnus-article-strip-leading-space
1632     "e" gnus-article-strip-trailing-space)
1633
1634   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1635     "v" gnus-version
1636     "f" gnus-summary-fetch-faq
1637     "d" gnus-summary-describe-group
1638     "h" gnus-summary-describe-briefly
1639     "i" gnus-info-find-node)
1640
1641   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1642     "e" gnus-summary-expire-articles
1643     "\M-\C-e" gnus-summary-expire-articles-now
1644     "\177" gnus-summary-delete-article
1645     [delete] gnus-summary-delete-article
1646     [backspace] gnus-summary-delete-article
1647     "m" gnus-summary-move-article
1648     "r" gnus-summary-respool-article
1649     "w" gnus-summary-edit-article
1650     "c" gnus-summary-copy-article
1651     "B" gnus-summary-crosspost-article
1652     "q" gnus-summary-respool-query
1653     "t" gnus-summary-respool-trace
1654     "i" gnus-summary-import-article
1655     "p" gnus-summary-article-posted-p)
1656
1657   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1658     "o" gnus-summary-save-article
1659     "m" gnus-summary-save-article-mail
1660     "F" gnus-summary-write-article-file
1661     "r" gnus-summary-save-article-rmail
1662     "f" gnus-summary-save-article-file
1663     "b" gnus-summary-save-article-body-file
1664     "h" gnus-summary-save-article-folder
1665     "v" gnus-summary-save-article-vm
1666     "p" gnus-summary-pipe-output
1667     "s" gnus-soup-add-article)
1668
1669   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1670     "b" gnus-summary-display-buttonized
1671     "m" gnus-summary-repair-multipart
1672     "v" gnus-article-view-part
1673     "o" gnus-article-save-part
1674     "c" gnus-article-copy-part
1675     "C" gnus-article-view-part-as-charset
1676     "e" gnus-article-externalize-part
1677     "E" gnus-article-encrypt-body
1678     "i" gnus-article-inline-part
1679     "|" gnus-article-pipe-part))
1680
1681 (defun gnus-summary-make-menu-bar ()
1682   (gnus-turn-off-edit-menu 'summary)
1683
1684   (unless (boundp 'gnus-summary-misc-menu)
1685
1686     (easy-menu-define
1687      gnus-summary-kill-menu gnus-summary-mode-map ""
1688      (cons
1689       "Score"
1690       (nconc
1691        (list
1692         ["Enter score..." gnus-summary-score-entry t]
1693         ["Customize" gnus-score-customize t])
1694        (gnus-make-score-map 'increase)
1695        (gnus-make-score-map 'lower)
1696        '(("Mark"
1697           ["Kill below" gnus-summary-kill-below t]
1698           ["Mark above" gnus-summary-mark-above t]
1699           ["Tick above" gnus-summary-tick-above t]
1700           ["Clear above" gnus-summary-clear-above t])
1701          ["Current score" gnus-summary-current-score t]
1702          ["Set score" gnus-summary-set-score t]
1703          ["Switch current score file..." gnus-score-change-score-file t]
1704          ["Set mark below..." gnus-score-set-mark-below t]
1705          ["Set expunge below..." gnus-score-set-expunge-below t]
1706          ["Edit current score file" gnus-score-edit-current-scores t]
1707          ["Edit score file" gnus-score-edit-file t]
1708          ["Trace score" gnus-score-find-trace t]
1709          ["Find words" gnus-score-find-favourite-words t]
1710          ["Rescore buffer" gnus-summary-rescore t]
1711          ["Increase score..." gnus-summary-increase-score t]
1712          ["Lower score..." gnus-summary-lower-score t]))))
1713
1714     ;; Define both the Article menu in the summary buffer and the equivalent
1715     ;; Commands menu in the article buffer here for consistency.
1716     (let ((innards
1717            '(("Hide"
1718               ["All" gnus-article-hide t]
1719               ["Headers" gnus-article-toggle-headers t]
1720               ["Signature" gnus-article-hide-signature t]
1721               ["Citation" gnus-article-hide-citation t]
1722               ["List identifiers" gnus-article-hide-list-identifiers t]
1723               ["PGP" gnus-article-hide-pgp t]
1724               ["Banner" gnus-article-strip-banner t]
1725               ["Boring headers" gnus-article-hide-boring-headers t])
1726              ("Highlight"
1727               ["All" gnus-article-highlight t]
1728               ["Headers" gnus-article-highlight-headers t]
1729               ["Signature" gnus-article-highlight-signature t]
1730               ["Citation" gnus-article-highlight-citation t])
1731              ("Date"
1732               ["Local" gnus-article-date-local t]
1733               ["ISO8601" gnus-article-date-iso8601 t]
1734               ["UT" gnus-article-date-ut t]
1735               ["Original" gnus-article-date-original t]
1736               ["Lapsed" gnus-article-date-lapsed t]
1737               ["User-defined" gnus-article-date-user t])
1738              ("Washing"
1739               ("Remove Blanks"
1740                ["Leading" gnus-article-strip-leading-blank-lines t]
1741                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1742                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1743                ["All of the above" gnus-article-strip-blank-lines t]
1744                ["All" gnus-article-strip-all-blank-lines t]
1745                ["Leading space" gnus-article-strip-leading-space t]
1746                ["Trailing space" gnus-article-strip-trailing-space t])
1747               ["Overstrike" gnus-article-treat-overstrike t]
1748               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1749               ["Emphasis" gnus-article-emphasize t]
1750               ["Word wrap" gnus-article-fill-cited-article t]
1751               ["Fill long lines" gnus-article-fill-long-lines t]
1752               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1753               ["CR" gnus-article-remove-cr t]
1754               ["Show X-Face" gnus-article-display-x-face t]
1755               ["Rot 13" gnus-summary-caesar-message t]
1756               ["Unix pipe" gnus-summary-pipe-message t]
1757               ["Add buttons" gnus-article-add-buttons t]
1758               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1759               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1760               ["Toggle MIME" gnus-summary-toggle-mime t]
1761               ["Verbose header" gnus-summary-verbose-headers t]
1762               ["Toggle header" gnus-summary-toggle-header t]
1763               ["Toggle smileys" gnus-smiley-display t]
1764               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1765               ["HZ" gnus-article-decode-HZ t])
1766              ("Output"
1767               ["Save in default format" gnus-summary-save-article t]
1768               ["Save in file" gnus-summary-save-article-file t]
1769               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1770               ["Save in MH folder" gnus-summary-save-article-folder t]
1771               ["Save in VM folder" gnus-summary-save-article-vm t]
1772               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1773               ["Save body in file" gnus-summary-save-article-body-file t]
1774               ["Pipe through a filter" gnus-summary-pipe-output t]
1775               ["Add to SOUP packet" gnus-soup-add-article t]
1776               ["Print" gnus-summary-print-article t])
1777              ("Backend"
1778               ["Respool article..." gnus-summary-respool-article t]
1779               ["Move article..." gnus-summary-move-article
1780                (gnus-check-backend-function
1781                 'request-move-article gnus-newsgroup-name)]
1782               ["Copy article..." gnus-summary-copy-article t]
1783               ["Crosspost article..." gnus-summary-crosspost-article
1784                (gnus-check-backend-function
1785                 'request-replace-article gnus-newsgroup-name)]
1786               ["Import file..." gnus-summary-import-article t]
1787               ["Check if posted" gnus-summary-article-posted-p t]
1788               ["Edit article" gnus-summary-edit-article
1789                (not (gnus-group-read-only-p))]
1790               ["Delete article" gnus-summary-delete-article
1791                (gnus-check-backend-function
1792                 'request-expire-articles gnus-newsgroup-name)]
1793               ["Query respool" gnus-summary-respool-query t]
1794               ["Trace respool" gnus-summary-respool-trace t]
1795               ["Delete expirable articles" gnus-summary-expire-articles-now
1796                (gnus-check-backend-function
1797                 'request-expire-articles gnus-newsgroup-name)])
1798              ("Extract"
1799               ["Uudecode" gnus-uu-decode-uu t]
1800               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1801               ["Unshar" gnus-uu-decode-unshar t]
1802               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1803               ["Save" gnus-uu-decode-save t]
1804               ["Binhex" gnus-uu-decode-binhex t]
1805               ["Postscript" gnus-uu-decode-postscript t])
1806              ("Cache"
1807               ["Enter article" gnus-cache-enter-article t]
1808               ["Remove article" gnus-cache-remove-article t])
1809              ["Translate" gnus-article-babel t]
1810              ["Select article buffer" gnus-summary-select-article-buffer t]
1811              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1812              ["Isearch article..." gnus-summary-isearch-article t]
1813              ["Beginning of the article" gnus-summary-beginning-of-article t]
1814              ["End of the article" gnus-summary-end-of-article t]
1815              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1816              ["Fetch referenced articles" gnus-summary-refer-references t]
1817              ["Fetch current thread" gnus-summary-refer-thread t]
1818              ["Fetch article with id..." gnus-summary-refer-article t]
1819              ["Redisplay" gnus-summary-show-article t])))
1820       (easy-menu-define
1821        gnus-summary-article-menu gnus-summary-mode-map ""
1822        (cons "Article" innards))
1823
1824       (easy-menu-define
1825        gnus-article-commands-menu gnus-article-mode-map ""
1826        (cons "Commands" innards)))
1827
1828     (easy-menu-define
1829      gnus-summary-thread-menu gnus-summary-mode-map ""
1830      '("Threads"
1831        ["Toggle threading" gnus-summary-toggle-threads t]
1832        ["Hide threads" gnus-summary-hide-all-threads t]
1833        ["Show threads" gnus-summary-show-all-threads t]
1834        ["Hide thread" gnus-summary-hide-thread t]
1835        ["Show thread" gnus-summary-show-thread t]
1836        ["Go to next thread" gnus-summary-next-thread t]
1837        ["Go to previous thread" gnus-summary-prev-thread t]
1838        ["Go down thread" gnus-summary-down-thread t]
1839        ["Go up thread" gnus-summary-up-thread t]
1840        ["Top of thread" gnus-summary-top-thread t]
1841        ["Mark thread as read" gnus-summary-kill-thread t]
1842        ["Lower thread score" gnus-summary-lower-thread t]
1843        ["Raise thread score" gnus-summary-raise-thread t]
1844        ["Rethread current" gnus-summary-rethread-current t]))
1845
1846     (easy-menu-define
1847      gnus-summary-post-menu gnus-summary-mode-map ""
1848      '("Post"
1849        ["Post an article" gnus-summary-post-news t]
1850        ["Followup" gnus-summary-followup t]
1851        ["Followup and yank" gnus-summary-followup-with-original t]
1852        ["Supersede article" gnus-summary-supersede-article t]
1853        ["Cancel article" gnus-summary-cancel-article t]
1854        ["Reply" gnus-summary-reply t]
1855        ["Reply and yank" gnus-summary-reply-with-original t]
1856        ["Wide reply" gnus-summary-wide-reply t]
1857        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1858        ["Mail forward" gnus-summary-mail-forward t]
1859        ["Post forward" gnus-summary-post-forward t]
1860        ["Digest and mail" gnus-uu-digest-mail-forward t]
1861        ["Digest and post" gnus-uu-digest-post-forward t]
1862        ["Resend message" gnus-summary-resend-message t]
1863        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1864        ["Send a mail" gnus-summary-mail-other-window t]
1865        ["Uuencode and post" gnus-uu-post-news t]
1866        ["Followup via news" gnus-summary-followup-to-mail t]
1867        ["Followup via news and yank"
1868         gnus-summary-followup-to-mail-with-original t]
1869        ;;("Draft"
1870        ;;["Send" gnus-summary-send-draft t]
1871        ;;["Send bounced" gnus-resend-bounced-mail t])
1872        ))
1873
1874     (easy-menu-define
1875      gnus-summary-misc-menu gnus-summary-mode-map ""
1876      '("Misc"
1877        ("Mark Read"
1878         ["Mark as read" gnus-summary-mark-as-read-forward t]
1879         ["Mark same subject and select"
1880          gnus-summary-kill-same-subject-and-select t]
1881         ["Mark same subject" gnus-summary-kill-same-subject t]
1882         ["Catchup" gnus-summary-catchup t]
1883         ["Catchup all" gnus-summary-catchup-all t]
1884         ["Catchup to here" gnus-summary-catchup-to-here t]
1885         ["Catchup region" gnus-summary-mark-region-as-read t]
1886         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1887        ("Mark Various"
1888         ["Tick" gnus-summary-tick-article-forward t]
1889         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1890         ["Remove marks" gnus-summary-clear-mark-forward t]
1891         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1892         ["Set bookmark" gnus-summary-set-bookmark t]
1893         ["Remove bookmark" gnus-summary-remove-bookmark t])
1894        ("Mark Limit"
1895         ["Marks..." gnus-summary-limit-to-marks t]
1896         ["Subject..." gnus-summary-limit-to-subject t]
1897         ["Author..." gnus-summary-limit-to-author t]
1898         ["Age..." gnus-summary-limit-to-age t]
1899         ["Extra..." gnus-summary-limit-to-extra t]
1900         ["Score" gnus-summary-limit-to-score t]
1901         ["Unread" gnus-summary-limit-to-unread t]
1902         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1903         ["Articles" gnus-summary-limit-to-articles t]
1904         ["Pop limit" gnus-summary-pop-limit t]
1905         ["Show dormant" gnus-summary-limit-include-dormant t]
1906         ["Hide childless dormant"
1907          gnus-summary-limit-exclude-childless-dormant t]
1908         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1909         ["Hide marked" gnus-summary-limit-exclude-marks t]
1910         ["Show expunged" gnus-summary-show-all-expunged t])
1911        ("Process Mark"
1912         ["Set mark" gnus-summary-mark-as-processable t]
1913         ["Remove mark" gnus-summary-unmark-as-processable t]
1914         ["Remove all marks" gnus-summary-unmark-all-processable t]
1915         ["Mark above" gnus-uu-mark-over t]
1916         ["Mark series" gnus-uu-mark-series t]
1917         ["Mark region" gnus-uu-mark-region t]
1918         ["Unmark region" gnus-uu-unmark-region t]
1919         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1920         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1921         ["Mark all" gnus-uu-mark-all t]
1922         ["Mark buffer" gnus-uu-mark-buffer t]
1923         ["Mark sparse" gnus-uu-mark-sparse t]
1924         ["Mark thread" gnus-uu-mark-thread t]
1925         ["Unmark thread" gnus-uu-unmark-thread t]
1926         ("Process Mark Sets"
1927          ["Kill" gnus-summary-kill-process-mark t]
1928          ["Yank" gnus-summary-yank-process-mark
1929           gnus-newsgroup-process-stack]
1930          ["Save" gnus-summary-save-process-mark t]))
1931        ("Scroll article"
1932         ["Page forward" gnus-summary-next-page t]
1933         ["Page backward" gnus-summary-prev-page t]
1934         ["Line forward" gnus-summary-scroll-up t])
1935        ("Move"
1936         ["Next unread article" gnus-summary-next-unread-article t]
1937         ["Previous unread article" gnus-summary-prev-unread-article t]
1938         ["Next article" gnus-summary-next-article t]
1939         ["Previous article" gnus-summary-prev-article t]
1940         ["Next unread subject" gnus-summary-next-unread-subject t]
1941         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1942         ["Next article same subject" gnus-summary-next-same-subject t]
1943         ["Previous article same subject" gnus-summary-prev-same-subject t]
1944         ["First unread article" gnus-summary-first-unread-article t]
1945         ["Best unread article" gnus-summary-best-unread-article t]
1946         ["Go to subject number..." gnus-summary-goto-subject t]
1947         ["Go to article number..." gnus-summary-goto-article t]
1948         ["Go to the last article" gnus-summary-goto-last-article t]
1949         ["Pop article off history" gnus-summary-pop-article t])
1950        ("Sort"
1951         ["Sort by number" gnus-summary-sort-by-number t]
1952         ["Sort by author" gnus-summary-sort-by-author t]
1953         ["Sort by subject" gnus-summary-sort-by-subject t]
1954         ["Sort by date" gnus-summary-sort-by-date t]
1955         ["Sort by score" gnus-summary-sort-by-score t]
1956         ["Sort by lines" gnus-summary-sort-by-lines t]
1957         ["Sort by characters" gnus-summary-sort-by-chars t])
1958        ("Help"
1959         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1960         ["Describe group" gnus-summary-describe-group t]
1961         ["Read manual" gnus-info-find-node t])
1962        ("Modes"
1963         ["Pick and read" gnus-pick-mode t]
1964         ["Binary" gnus-binary-mode t])
1965        ("Regeneration"
1966         ["Regenerate" gnus-summary-prepare t]
1967         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1968         ["Toggle threading" gnus-summary-toggle-threads t])
1969        ["Filter articles..." gnus-summary-execute-command t]
1970        ["Run command on subjects..." gnus-summary-universal-argument t]
1971        ["Search articles forward..." gnus-summary-search-article-forward t]
1972        ["Search articles backward..." gnus-summary-search-article-backward t]
1973        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1974        ["Expand window" gnus-summary-expand-window t]
1975        ["Expire expirable articles" gnus-summary-expire-articles
1976         (gnus-check-backend-function
1977          'request-expire-articles gnus-newsgroup-name)]
1978        ["Edit local kill file" gnus-summary-edit-local-kill t]
1979        ["Edit main kill file" gnus-summary-edit-global-kill t]
1980        ["Edit group parameters" gnus-summary-edit-parameters t]
1981        ["Customize group parameters" gnus-summary-customize-parameters t]
1982        ["Send a bug report" gnus-bug t]
1983        ("Exit"
1984         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1985         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1986         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1987         ["Exit group" gnus-summary-exit t]
1988         ["Exit group without updating" gnus-summary-exit-no-update t]
1989         ["Exit and goto next group" gnus-summary-next-group t]
1990         ["Exit and goto prev group" gnus-summary-prev-group t]
1991         ["Reselect group" gnus-summary-reselect-current-group t]
1992         ["Rescan group" gnus-summary-rescan-group t]
1993         ["Update dribble" gnus-summary-save-newsrc t])))
1994
1995     (gnus-run-hooks 'gnus-summary-menu-hook)))
1996
1997 (defun gnus-score-set-default (var value)
1998   "A version of set that updates the GNU Emacs menu-bar."
1999   (set var value)
2000   ;; It is the message that forces the active status to be updated.
2001   (message ""))
2002
2003 (defun gnus-make-score-map (type)
2004   "Make a summary score map of type TYPE."
2005   (if t
2006       nil
2007     (let ((headers '(("author" "from" string)
2008                      ("subject" "subject" string)
2009                      ("article body" "body" string)
2010                      ("article head" "head" string)
2011                      ("xref" "xref" string)
2012                      ("extra header" "extra" string)
2013                      ("lines" "lines" number)
2014                      ("followups to author" "followup" string)))
2015           (types '((number ("less than" <)
2016                            ("greater than" >)
2017                            ("equal" =))
2018                    (string ("substring" s)
2019                            ("exact string" e)
2020                            ("fuzzy string" f)
2021                            ("regexp" r))))
2022           (perms '(("temporary" (current-time-string))
2023                    ("permanent" nil)
2024                    ("immediate" now)))
2025           header)
2026       (list
2027        (apply
2028         'nconc
2029         (list
2030          (if (eq type 'lower)
2031              "Lower score"
2032            "Increase score"))
2033         (let (outh)
2034           (while headers
2035             (setq header (car headers))
2036             (setq outh
2037                   (cons
2038                    (apply
2039                     'nconc
2040                     (list (car header))
2041                     (let ((ts (cdr (assoc (nth 2 header) types)))
2042                           outt)
2043                       (while ts
2044                         (setq outt
2045                               (cons
2046                                (apply
2047                                 'nconc
2048                                 (list (caar ts))
2049                                 (let ((ps perms)
2050                                       outp)
2051                                   (while ps
2052                                     (setq outp
2053                                           (cons
2054                                            (vector
2055                                             (caar ps)
2056                                             (list
2057                                              'gnus-summary-score-entry
2058                                              (nth 1 header)
2059                                              (if (or (string= (nth 1 header)
2060                                                               "head")
2061                                                      (string= (nth 1 header)
2062                                                               "body"))
2063                                                  ""
2064                                                (list 'gnus-summary-header
2065                                                      (nth 1 header)))
2066                                              (list 'quote (nth 1 (car ts)))
2067                                              (list 'gnus-score-delta-default
2068                                                    nil)
2069                                              (nth 1 (car ps))
2070                                              t)
2071                                             t)
2072                                            outp))
2073                                     (setq ps (cdr ps)))
2074                                   (list (nreverse outp))))
2075                                outt))
2076                         (setq ts (cdr ts)))
2077                       (list (nreverse outt))))
2078                    outh))
2079             (setq headers (cdr headers)))
2080           (list (nreverse outh))))))))
2081
2082 \f
2083
2084 (defun gnus-summary-mode (&optional group)
2085   "Major mode for reading articles.
2086
2087 All normal editing commands are switched off.
2088 \\<gnus-summary-mode-map>
2089 Each line in this buffer represents one article.  To read an
2090 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2091 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2092 respectively.
2093
2094 You can also post articles and send mail from this buffer.  To
2095 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2096 of an article, type `\\[gnus-summary-reply]'.
2097
2098 There are approx. one gazillion commands you can execute in this
2099 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2100
2101 The following commands are available:
2102
2103 \\{gnus-summary-mode-map}"
2104   (interactive)
2105   (when (gnus-visual-p 'summary-menu 'menu)
2106     (gnus-summary-make-menu-bar))
2107   (kill-all-local-variables)
2108   (gnus-summary-make-local-variables)
2109   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2110     (gnus-summary-make-local-variables))
2111   (gnus-make-thread-indent-array)
2112   (gnus-simplify-mode-line)
2113   (setq major-mode 'gnus-summary-mode)
2114   (setq mode-name "Summary")
2115   (make-local-variable 'minor-mode-alist)
2116   (use-local-map gnus-summary-mode-map)
2117   (buffer-disable-undo)
2118   (setq buffer-read-only t)             ;Disable modification
2119   (setq truncate-lines t)
2120   (setq selective-display t)
2121   (setq selective-display-ellipses t)   ;Display `...'
2122   (gnus-summary-set-display-table)
2123   (gnus-set-default-directory)
2124   (setq gnus-newsgroup-name group)
2125   (unless (gnus-news-group-p group)
2126     (setq gnus-newsgroup-incorporated
2127           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2128   (make-local-variable 'gnus-summary-line-format)
2129   (make-local-variable 'gnus-summary-line-format-spec)
2130   (make-local-variable 'gnus-summary-dummy-line-format)
2131   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2132   (make-local-variable 'gnus-summary-mark-positions)
2133   (make-local-hook 'pre-command-hook)
2134   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2135   (gnus-run-hooks 'gnus-summary-mode-hook)
2136   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2137   (gnus-update-summary-mark-positions))
2138
2139 (defun gnus-summary-make-local-variables ()
2140   "Make all the local summary buffer variables."
2141   (let (global)
2142     (dolist (local gnus-summary-local-variables)
2143       (if (consp local)
2144           (progn
2145             (if (eq (cdr local) 'global)
2146                 ;; Copy the global value of the variable.
2147                 (setq global (symbol-value (car local)))
2148               ;; Use the value from the list.
2149               (setq global (eval (cdr local))))
2150             (set (make-local-variable (car local)) global))
2151         ;; Simple nil-valued local variable.
2152         (set (make-local-variable local) nil)))))
2153
2154 (defun gnus-summary-clear-local-variables ()
2155   (let ((locals gnus-summary-local-variables))
2156     (while locals
2157       (if (consp (car locals))
2158           (and (vectorp (caar locals))
2159                (set (caar locals) nil))
2160         (and (vectorp (car locals))
2161              (set (car locals) nil)))
2162       (setq locals (cdr locals)))))
2163
2164 ;; Summary data functions.
2165
2166 (defmacro gnus-data-number (data)
2167   `(car ,data))
2168
2169 (defmacro gnus-data-set-number (data number)
2170   `(setcar ,data ,number))
2171
2172 (defmacro gnus-data-mark (data)
2173   `(nth 1 ,data))
2174
2175 (defmacro gnus-data-set-mark (data mark)
2176   `(setcar (nthcdr 1 ,data) ,mark))
2177
2178 (defmacro gnus-data-pos (data)
2179   `(nth 2 ,data))
2180
2181 (defmacro gnus-data-set-pos (data pos)
2182   `(setcar (nthcdr 2 ,data) ,pos))
2183
2184 (defmacro gnus-data-header (data)
2185   `(nth 3 ,data))
2186
2187 (defmacro gnus-data-set-header (data header)
2188   `(setcar (nthcdr 3 ,data) ,header))
2189
2190 (defmacro gnus-data-level (data)
2191   `(nth 4 ,data))
2192
2193 (defmacro gnus-data-unread-p (data)
2194   `(= (nth 1 ,data) gnus-unread-mark))
2195
2196 (defmacro gnus-data-read-p (data)
2197   `(/= (nth 1 ,data) gnus-unread-mark))
2198
2199 (defmacro gnus-data-pseudo-p (data)
2200   `(consp (nth 3 ,data)))
2201
2202 (defmacro gnus-data-find (number)
2203   `(assq ,number gnus-newsgroup-data))
2204
2205 (defmacro gnus-data-find-list (number &optional data)
2206   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2207      (memq (assq ,number bdata)
2208            bdata)))
2209
2210 (defmacro gnus-data-make (number mark pos header level)
2211   `(list ,number ,mark ,pos ,header ,level))
2212
2213 (defun gnus-data-enter (after-article number mark pos header level offset)
2214   (let ((data (gnus-data-find-list after-article)))
2215     (unless data
2216       (error "No such article: %d" after-article))
2217     (setcdr data (cons (gnus-data-make number mark pos header level)
2218                        (cdr data)))
2219     (setq gnus-newsgroup-data-reverse nil)
2220     (gnus-data-update-list (cddr data) offset)))
2221
2222 (defun gnus-data-enter-list (after-article list &optional offset)
2223   (when list
2224     (let ((data (and after-article (gnus-data-find-list after-article)))
2225           (ilist list))
2226       (if (not (or data
2227                    after-article))
2228           (let ((odata gnus-newsgroup-data))
2229             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2230             (when offset
2231               (gnus-data-update-list odata offset)))
2232         ;; Find the last element in the list to be spliced into the main
2233         ;; list.
2234         (while (cdr list)
2235           (setq list (cdr list)))
2236         (if (not data)
2237             (progn
2238               (setcdr list gnus-newsgroup-data)
2239               (setq gnus-newsgroup-data ilist)
2240               (when offset
2241                 (gnus-data-update-list (cdr list) offset)))
2242           (setcdr list (cdr data))
2243           (setcdr data ilist)
2244           (when offset
2245             (gnus-data-update-list (cdr list) offset))))
2246       (setq gnus-newsgroup-data-reverse nil))))
2247
2248 (defun gnus-data-remove (article &optional offset)
2249   (let ((data gnus-newsgroup-data))
2250     (if (= (gnus-data-number (car data)) article)
2251         (progn
2252           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2253                 gnus-newsgroup-data-reverse nil)
2254           (when offset
2255             (gnus-data-update-list gnus-newsgroup-data offset)))
2256       (while (cdr data)
2257         (when (= (gnus-data-number (cadr data)) article)
2258           (setcdr data (cddr data))
2259           (when offset
2260             (gnus-data-update-list (cdr data) offset))
2261           (setq data nil
2262                 gnus-newsgroup-data-reverse nil))
2263         (setq data (cdr data))))))
2264
2265 (defmacro gnus-data-list (backward)
2266   `(if ,backward
2267        (or gnus-newsgroup-data-reverse
2268            (setq gnus-newsgroup-data-reverse
2269                  (reverse gnus-newsgroup-data)))
2270      gnus-newsgroup-data))
2271
2272 (defun gnus-data-update-list (data offset)
2273   "Add OFFSET to the POS of all data entries in DATA."
2274   (setq gnus-newsgroup-data-reverse nil)
2275   (while data
2276     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2277     (setq data (cdr data))))
2278
2279 (defun gnus-summary-article-pseudo-p (article)
2280   "Say whether this article is a pseudo article or not."
2281   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2282
2283 (defmacro gnus-summary-article-sparse-p (article)
2284   "Say whether this article is a sparse article or not."
2285   `(memq ,article gnus-newsgroup-sparse))
2286
2287 (defmacro gnus-summary-article-ancient-p (article)
2288   "Say whether this article is a sparse article or not."
2289   `(memq ,article gnus-newsgroup-ancient))
2290
2291 (defun gnus-article-parent-p (number)
2292   "Say whether this article is a parent or not."
2293   (let ((data (gnus-data-find-list number)))
2294     (and (cdr data)                     ; There has to be an article after...
2295          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2296             (gnus-data-level (nth 1 data))))))
2297
2298 (defun gnus-article-children (number)
2299   "Return a list of all children to NUMBER."
2300   (let* ((data (gnus-data-find-list number))
2301          (level (gnus-data-level (car data)))
2302          children)
2303     (setq data (cdr data))
2304     (while (and data
2305                 (= (gnus-data-level (car data)) (1+ level)))
2306       (push (gnus-data-number (car data)) children)
2307       (setq data (cdr data)))
2308     children))
2309
2310 (defmacro gnus-summary-skip-intangible ()
2311   "If the current article is intangible, then jump to a different article."
2312   '(let ((to (get-text-property (point) 'gnus-intangible)))
2313      (and to (gnus-summary-goto-subject to))))
2314
2315 (defmacro gnus-summary-article-intangible-p ()
2316   "Say whether this article is intangible or not."
2317   '(get-text-property (point) 'gnus-intangible))
2318
2319 (defun gnus-article-read-p (article)
2320   "Say whether ARTICLE is read or not."
2321   (not (or (memq article gnus-newsgroup-marked)
2322            (memq article gnus-newsgroup-unreads)
2323            (memq article gnus-newsgroup-unselected)
2324            (memq article gnus-newsgroup-dormant))))
2325
2326 ;; Some summary mode macros.
2327
2328 (defmacro gnus-summary-article-number ()
2329   "The article number of the article on the current line.
2330 If there isn's an article number here, then we return the current
2331 article number."
2332   '(progn
2333      (gnus-summary-skip-intangible)
2334      (or (get-text-property (point) 'gnus-number)
2335          (gnus-summary-last-subject))))
2336
2337 (defmacro gnus-summary-article-header (&optional number)
2338   "Return the header of article NUMBER."
2339   `(gnus-data-header (gnus-data-find
2340                       ,(or number '(gnus-summary-article-number)))))
2341
2342 (defmacro gnus-summary-thread-level (&optional number)
2343   "Return the level of thread that starts with article NUMBER."
2344   `(if (and (eq gnus-summary-make-false-root 'dummy)
2345             (get-text-property (point) 'gnus-intangible))
2346        0
2347      (gnus-data-level (gnus-data-find
2348                        ,(or number '(gnus-summary-article-number))))))
2349
2350 (defmacro gnus-summary-article-mark (&optional number)
2351   "Return the mark of article NUMBER."
2352   `(gnus-data-mark (gnus-data-find
2353                     ,(or number '(gnus-summary-article-number)))))
2354
2355 (defmacro gnus-summary-article-pos (&optional number)
2356   "Return the position of the line of article NUMBER."
2357   `(gnus-data-pos (gnus-data-find
2358                    ,(or number '(gnus-summary-article-number)))))
2359
2360 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2361 (defmacro gnus-summary-article-subject (&optional number)
2362   "Return current subject string or nil if nothing."
2363   `(let ((headers
2364           ,(if number
2365                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2366              '(gnus-data-header (assq (gnus-summary-article-number)
2367                                       gnus-newsgroup-data)))))
2368      (and headers
2369           (vectorp headers)
2370           (mail-header-subject headers))))
2371
2372 (defmacro gnus-summary-article-score (&optional number)
2373   "Return current article score."
2374   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2375                   gnus-newsgroup-scored))
2376        gnus-summary-default-score 0))
2377
2378 (defun gnus-summary-article-children (&optional number)
2379   "Return a list of article numbers that are children of article NUMBER."
2380   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2381          (level (gnus-data-level (car data)))
2382          l children)
2383     (while (and (setq data (cdr data))
2384                 (> (setq l (gnus-data-level (car data))) level))
2385       (and (= (1+ level) l)
2386            (push (gnus-data-number (car data))
2387                  children)))
2388     (nreverse children)))
2389
2390 (defun gnus-summary-article-parent (&optional number)
2391   "Return the article number of the parent of article NUMBER."
2392   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2393                                     (gnus-data-list t)))
2394          (level (gnus-data-level (car data))))
2395     (if (zerop level)
2396         ()                              ; This is a root.
2397       ;; We search until we find an article with a level less than
2398       ;; this one.  That function has to be the parent.
2399       (while (and (setq data (cdr data))
2400                   (not (< (gnus-data-level (car data)) level))))
2401       (and data (gnus-data-number (car data))))))
2402
2403 (defun gnus-unread-mark-p (mark)
2404   "Say whether MARK is the unread mark."
2405   (= mark gnus-unread-mark))
2406
2407 (defun gnus-read-mark-p (mark)
2408   "Say whether MARK is one of the marks that mark as read.
2409 This is all marks except unread, ticked, dormant, and expirable."
2410   (not (or (= mark gnus-unread-mark)
2411            (= mark gnus-ticked-mark)
2412            (= mark gnus-dormant-mark)
2413            (= mark gnus-expirable-mark))))
2414
2415 (defmacro gnus-article-mark (number)
2416   "Return the MARK of article NUMBER.
2417 This macro should only be used when computing the mark the \"first\"
2418 time; i.e., when generating the summary lines.  After that,
2419 `gnus-summary-article-mark' should be used to examine the
2420 marks of articles."
2421   `(cond
2422     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2423     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2424     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2425     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2426     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2427     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2428     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2429     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2430            gnus-ancient-mark))))
2431
2432 ;; Saving hidden threads.
2433
2434 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2435 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2436
2437 (defmacro gnus-save-hidden-threads (&rest forms)
2438   "Save hidden threads, eval FORMS, and restore the hidden threads."
2439   (let ((config (make-symbol "config")))
2440     `(let ((,config (gnus-hidden-threads-configuration)))
2441        (unwind-protect
2442            (save-excursion
2443              ,@forms)
2444          (gnus-restore-hidden-threads-configuration ,config)))))
2445
2446 (defun gnus-data-compute-positions ()
2447   "Compute the positions of all articles."
2448   (setq gnus-newsgroup-data-reverse nil)
2449   (let ((data gnus-newsgroup-data))
2450     (save-excursion
2451       (gnus-save-hidden-threads
2452         (gnus-summary-show-all-threads)
2453         (goto-char (point-min))
2454         (while data
2455           (while (get-text-property (point) 'gnus-intangible)
2456             (forward-line 1))
2457           (gnus-data-set-pos (car data) (+ (point) 3))
2458           (setq data (cdr data))
2459           (forward-line 1))))))
2460
2461 (defun gnus-hidden-threads-configuration ()
2462   "Return the current hidden threads configuration."
2463   (save-excursion
2464     (let (config)
2465       (goto-char (point-min))
2466       (while (search-forward "\r" nil t)
2467         (push (1- (point)) config))
2468       config)))
2469
2470 (defun gnus-restore-hidden-threads-configuration (config)
2471   "Restore hidden threads configuration from CONFIG."
2472   (save-excursion
2473     (let (point buffer-read-only)
2474       (while (setq point (pop config))
2475         (when (and (< point (point-max))
2476                    (goto-char point)
2477                    (eq (char-after) ?\n))
2478           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2479
2480 ;; Various summary mode internalish functions.
2481
2482 (defun gnus-mouse-pick-article (e)
2483   (interactive "e")
2484   (mouse-set-point e)
2485   (gnus-summary-next-page nil t))
2486
2487 (defun gnus-summary-set-display-table ()
2488   "Change the display table.
2489 Odd characters have a tendency to mess
2490 up nicely formatted displays - we make all possible glyphs
2491 display only a single character."
2492
2493   ;; We start from the standard display table, if any.
2494   (let ((table (or (copy-sequence standard-display-table)
2495                    (make-display-table)))
2496         (i 32))
2497     ;; Nix out all the control chars...
2498     (while (>= (setq i (1- i)) 0)
2499       (aset table i [??]))
2500     ;; ... but not newline and cr, of course.  (cr is necessary for the
2501     ;; selective display).
2502     (aset table ?\n nil)
2503     (aset table ?\r nil)
2504     ;; We keep TAB as well.
2505     (aset table ?\t nil)
2506     ;; We nix out any glyphs over 126 that are not set already.
2507     (let ((i 256))
2508       (while (>= (setq i (1- i)) 127)
2509         ;; Only modify if the entry is nil.
2510         (unless (aref table i)
2511           (aset table i [??]))))
2512     (setq buffer-display-table table)))
2513
2514 (defun gnus-summary-setup-buffer (group)
2515   "Initialize summary buffer."
2516   (let ((buffer (concat "*Summary " group "*")))
2517     (if (get-buffer buffer)
2518         (progn
2519           (set-buffer buffer)
2520           (setq gnus-summary-buffer (current-buffer))
2521           (not gnus-newsgroup-prepared))
2522       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2523       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2524       (gnus-summary-mode group)
2525       (when gnus-carpal
2526         (gnus-carpal-setup-buffer 'summary))
2527       (unless gnus-single-article-buffer
2528         (make-local-variable 'gnus-article-buffer)
2529         (make-local-variable 'gnus-article-current)
2530         (make-local-variable 'gnus-original-article-buffer))
2531       (setq gnus-newsgroup-name group)
2532       t)))
2533
2534 (defun gnus-set-global-variables ()
2535   "Set the global equivalents of the buffer-local variables.
2536 They are set to the latest values they had.  These reflect the summary
2537 buffer that was in action when the last article was fetched."
2538   (when (eq major-mode 'gnus-summary-mode)
2539     (setq gnus-summary-buffer (current-buffer))
2540     (let ((name gnus-newsgroup-name)
2541           (marked gnus-newsgroup-marked)
2542           (unread gnus-newsgroup-unreads)
2543           (headers gnus-current-headers)
2544           (data gnus-newsgroup-data)
2545           (summary gnus-summary-buffer)
2546           (article-buffer gnus-article-buffer)
2547           (original gnus-original-article-buffer)
2548           (gac gnus-article-current)
2549           (reffed gnus-reffed-article-number)
2550           (score-file gnus-current-score-file)
2551           (default-charset gnus-newsgroup-charset)
2552           vlist)
2553       (let ((locals gnus-newsgroup-variables))
2554         (while locals
2555           (if (consp (car locals))
2556               (push (eval (caar locals)) vlist)
2557             (push (eval (car locals)) vlist))
2558           (setq locals (cdr locals)))
2559         (setq vlist (nreverse vlist)))
2560       (save-excursion
2561         (set-buffer gnus-group-buffer)
2562         (setq gnus-newsgroup-name name
2563               gnus-newsgroup-marked marked
2564               gnus-newsgroup-unreads unread
2565               gnus-current-headers headers
2566               gnus-newsgroup-data data
2567               gnus-article-current gac
2568               gnus-summary-buffer summary
2569               gnus-article-buffer article-buffer
2570               gnus-original-article-buffer original
2571               gnus-reffed-article-number reffed
2572               gnus-current-score-file score-file
2573               gnus-newsgroup-charset default-charset)
2574         (let ((locals gnus-newsgroup-variables))
2575           (while locals
2576             (if (consp (car locals))
2577                 (set (caar locals) (pop vlist))
2578               (set (car locals) (pop vlist)))
2579             (setq locals (cdr locals))))
2580         ;; The article buffer also has local variables.
2581         (when (gnus-buffer-live-p gnus-article-buffer)
2582           (set-buffer gnus-article-buffer)
2583           (setq gnus-summary-buffer summary))))))
2584
2585 (defun gnus-summary-article-unread-p (article)
2586   "Say whether ARTICLE is unread or not."
2587   (memq article gnus-newsgroup-unreads))
2588
2589 (defun gnus-summary-first-article-p (&optional article)
2590   "Return whether ARTICLE is the first article in the buffer."
2591   (if (not (setq article (or article (gnus-summary-article-number))))
2592       nil
2593     (eq article (caar gnus-newsgroup-data))))
2594
2595 (defun gnus-summary-last-article-p (&optional article)
2596   "Return whether ARTICLE is the last article in the buffer."
2597   (if (not (setq article (or article (gnus-summary-article-number))))
2598       ;; All non-existent numbers are the last article.  :-)
2599       t
2600     (not (cdr (gnus-data-find-list article)))))
2601
2602 (defun gnus-make-thread-indent-array ()
2603   (let ((n 200))
2604     (unless (and gnus-thread-indent-array
2605                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2606       (setq gnus-thread-indent-array (make-vector 201 "")
2607             gnus-thread-indent-array-level gnus-thread-indent-level)
2608       (while (>= n 0)
2609         (aset gnus-thread-indent-array n
2610               (make-string (* n gnus-thread-indent-level) ? ))
2611         (setq n (1- n))))))
2612
2613 (defun gnus-update-summary-mark-positions ()
2614   "Compute where the summary marks are to go."
2615   (save-excursion
2616     (when (gnus-buffer-exists-p gnus-summary-buffer)
2617       (set-buffer gnus-summary-buffer))
2618     (let ((gnus-replied-mark 129)
2619           (gnus-score-below-mark 130)
2620           (gnus-score-over-mark 130)
2621           (gnus-download-mark 131)
2622           (spec gnus-summary-line-format-spec)
2623           gnus-visual pos)
2624       (save-excursion
2625         (gnus-set-work-buffer)
2626         (let ((gnus-summary-line-format-spec spec)
2627               (gnus-newsgroup-downloadable '((0 . t))))
2628           (gnus-summary-insert-line
2629            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2630            0 nil 128 t nil "" nil 1)
2631           (goto-char (point-min))
2632           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2633                                              (- (point) 2)))))
2634           (goto-char (point-min))
2635           (push (cons 'replied (and (search-forward "\201" nil t)
2636                                     (- (point) 2)))
2637                 pos)
2638           (goto-char (point-min))
2639           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2640                 pos)
2641           (goto-char (point-min))
2642           (push (cons 'download
2643                       (and (search-forward "\203" nil t) (- (point) 2)))
2644                 pos)))
2645       (setq gnus-summary-mark-positions pos))))
2646
2647 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2648   "Insert a dummy root in the summary buffer."
2649   (beginning-of-line)
2650   (gnus-add-text-properties
2651    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2652    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2653
2654 (defun gnus-summary-from-or-to-or-newsgroups (header)
2655   (let ((to (cdr (assq 'To (mail-header-extra header))))
2656         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2657         (default-mime-charset (with-current-buffer gnus-summary-buffer
2658                                 default-mime-charset)))
2659     (cond
2660      ((and to
2661            gnus-ignored-from-addresses
2662            (string-match gnus-ignored-from-addresses
2663                          (mail-header-from header)))
2664       (concat "-> "
2665               (or (car (funcall gnus-extract-address-components
2666                                 (funcall
2667                                  gnus-decode-encoded-word-function to)))
2668                   (funcall gnus-decode-encoded-word-function to))))
2669      ((and newsgroups
2670            gnus-ignored-from-addresses
2671            (string-match gnus-ignored-from-addresses
2672                          (mail-header-from header)))
2673       (concat "=> " newsgroups))
2674      (t
2675       (or (car (funcall gnus-extract-address-components
2676                         (mail-header-from header)))
2677           (mail-header-from header))))))
2678
2679 (defun gnus-summary-insert-line (gnus-tmp-header
2680                                  gnus-tmp-level gnus-tmp-current
2681                                  gnus-tmp-unread gnus-tmp-replied
2682                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2683                                  &optional gnus-tmp-dummy gnus-tmp-score
2684                                  gnus-tmp-process)
2685   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2686          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2687          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2688          (gnus-tmp-score-char
2689           (if (or (null gnus-summary-default-score)
2690                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2691                       gnus-summary-zcore-fuzz))
2692               ?  ;Whitespace
2693             (if (< gnus-tmp-score gnus-summary-default-score)
2694                 gnus-score-below-mark gnus-score-over-mark)))
2695          (gnus-tmp-replied
2696           (cond (gnus-tmp-process gnus-process-mark)
2697                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2698                  gnus-cached-mark)
2699                 (gnus-tmp-replied gnus-replied-mark)
2700                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2701                  gnus-saved-mark)
2702                 (t gnus-unread-mark)))
2703          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2704          (gnus-tmp-name
2705           (cond
2706            ((string-match "<[^>]+> *$" gnus-tmp-from)
2707             (let ((beg (match-beginning 0)))
2708               (or (and (string-match "^\".+\"" gnus-tmp-from)
2709                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2710                   (substring gnus-tmp-from 0 beg))))
2711            ((string-match "(.+)" gnus-tmp-from)
2712             (substring gnus-tmp-from
2713                        (1+ (match-beginning 0)) (1- (match-end 0))))
2714            (t gnus-tmp-from)))
2715          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2716          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2717          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2718          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2719          (buffer-read-only nil))
2720     (when (string= gnus-tmp-name "")
2721       (setq gnus-tmp-name gnus-tmp-from))
2722     (unless (numberp gnus-tmp-lines)
2723       (setq gnus-tmp-lines 0))
2724     (gnus-put-text-property-excluding-characters-with-faces
2725      (point)
2726      (progn (eval gnus-summary-line-format-spec) (point))
2727      'gnus-number gnus-tmp-number)
2728     (when (gnus-visual-p 'summary-highlight 'highlight)
2729       (forward-line -1)
2730       (gnus-run-hooks 'gnus-summary-update-hook)
2731       (forward-line 1))))
2732
2733 (defun gnus-summary-update-line (&optional dont-update)
2734   "Update summary line after change."
2735   (when (and gnus-summary-default-score
2736              (not gnus-summary-inhibit-highlight))
2737     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2738            (article (gnus-summary-article-number))
2739            (score (gnus-summary-article-score article)))
2740       (unless dont-update
2741         (if (and gnus-summary-mark-below
2742                  (< (gnus-summary-article-score)
2743                     gnus-summary-mark-below))
2744             ;; This article has a low score, so we mark it as read.
2745             (when (memq article gnus-newsgroup-unreads)
2746               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2747           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2748             ;; This article was previously marked as read on account
2749             ;; of a low score, but now it has risen, so we mark it as
2750             ;; unread.
2751             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2752         (gnus-summary-update-mark
2753          (if (or (null gnus-summary-default-score)
2754                  (<= (abs (- score gnus-summary-default-score))
2755                      gnus-summary-zcore-fuzz))
2756              ?  ;Whitespace
2757            (if (< score gnus-summary-default-score)
2758                gnus-score-below-mark gnus-score-over-mark))
2759          'score))
2760       ;; Do visual highlighting.
2761       (when (gnus-visual-p 'summary-highlight 'highlight)
2762         (gnus-run-hooks 'gnus-summary-update-hook)))))
2763
2764 (defvar gnus-tmp-new-adopts nil)
2765
2766 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2767   "Return the number of articles in THREAD.
2768 This may be 0 in some cases -- if none of the articles in
2769 the thread are to be displayed."
2770   (let* ((number
2771           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2772           (cond
2773            ((not (listp thread))
2774             1)
2775            ((and (consp thread) (cdr thread))
2776             (apply
2777              '+ 1 (mapcar
2778                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2779            ((null thread)
2780             1)
2781            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2782             1)
2783            (t 0))))
2784     (when (and level (zerop level) gnus-tmp-new-adopts)
2785       (incf number
2786             (apply '+ (mapcar
2787                        'gnus-summary-number-of-articles-in-thread
2788                        gnus-tmp-new-adopts))))
2789     (if char
2790         (if (> number 1) gnus-not-empty-thread-mark
2791           gnus-empty-thread-mark)
2792       number)))
2793
2794 (defun gnus-summary-set-local-parameters (group)
2795   "Go through the local params of GROUP and set all variable specs in that list."
2796   (let ((params (gnus-group-find-parameter group))
2797         elem)
2798     (while params
2799       (setq elem (car params)
2800             params (cdr params))
2801       (and (consp elem)                 ; Has to be a cons.
2802            (consp (cdr elem))           ; The cdr has to be a list.
2803            (symbolp (car elem))         ; Has to be a symbol in there.
2804            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2805            (ignore-errors               ; So we set it.
2806              (make-local-variable (car elem))
2807              (set (car elem) (eval (nth 1 elem))))))))
2808
2809 (defun gnus-summary-read-group (group &optional show-all no-article
2810                                       kill-buffer no-display backward
2811                                       select-articles)
2812   "Start reading news in newsgroup GROUP.
2813 If SHOW-ALL is non-nil, already read articles are also listed.
2814 If NO-ARTICLE is non-nil, no article is selected initially.
2815 If NO-DISPLAY, don't generate a summary buffer."
2816   (let (result)
2817     (while (and group
2818                 (null (setq result
2819                             (let ((gnus-auto-select-next nil))
2820                               (or (gnus-summary-read-group-1
2821                                    group show-all no-article
2822                                    kill-buffer no-display
2823                                    select-articles)
2824                                   (setq show-all nil
2825                                         select-articles nil)))))
2826                 (eq gnus-auto-select-next 'quietly))
2827       (set-buffer gnus-group-buffer)
2828       ;; The entry function called above goes to the next
2829       ;; group automatically, so we go two groups back
2830       ;; if we are searching for the previous group.
2831       (when backward
2832         (gnus-group-prev-unread-group 2))
2833       (if (not (equal group (gnus-group-group-name)))
2834           (setq group (gnus-group-group-name))
2835         (setq group nil)))
2836     result))
2837
2838 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2839   "Directly jump to the other GROUP from summary buffer.
2840 If SHOW-ALL is non-nil, already read articles are also listed."
2841   (interactive
2842    (if (eq gnus-summary-buffer (current-buffer))
2843        (list (completing-read
2844               "Group: " gnus-active-hashtb nil t
2845               (when (and gnus-newsgroup-name
2846                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2847                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2848               'gnus-group-history)
2849              current-prefix-arg)
2850      (error "%s must be invoked from a gnus summary buffer." this-command)))
2851   (unless (or (zerop (length group))
2852               (and gnus-newsgroup-name
2853                    (string-equal gnus-newsgroup-name group)))
2854     (gnus-summary-exit)
2855     (gnus-summary-read-group group show-all
2856                              gnus-dont-select-after-jump-to-other-group)))
2857
2858 (defun gnus-summary-read-group-1 (group show-all no-article
2859                                         kill-buffer no-display
2860                                         &optional select-articles)
2861   ;; Killed foreign groups can't be entered.
2862   (when (and (not (gnus-group-native-p group))
2863              (not (gnus-gethash group gnus-newsrc-hashtb)))
2864     (error "Dead non-native groups can't be entered"))
2865   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2866   (let* ((new-group (gnus-summary-setup-buffer group))
2867          (quit-config (gnus-group-quit-config group))
2868          (did-select (and new-group (gnus-select-newsgroup
2869                                      group show-all select-articles))))
2870     (cond
2871      ;; This summary buffer exists already, so we just select it.
2872      ((not new-group)
2873       (gnus-set-global-variables)
2874       (when kill-buffer
2875         (gnus-kill-or-deaden-summary kill-buffer))
2876       (gnus-configure-windows 'summary 'force)
2877       (gnus-set-mode-line 'summary)
2878       (gnus-summary-position-point)
2879       (message "")
2880       t)
2881      ;; We couldn't select this group.
2882      ((null did-select)
2883       (when (and (eq major-mode 'gnus-summary-mode)
2884                  (not (equal (current-buffer) kill-buffer)))
2885         (kill-buffer (current-buffer))
2886         (if (not quit-config)
2887             (progn
2888               ;; Update the info -- marks might need to be removed,
2889               ;; for instance.
2890               (gnus-summary-update-info)
2891               (set-buffer gnus-group-buffer)
2892               (gnus-group-jump-to-group group)
2893               (gnus-group-next-unread-group 1))
2894           (gnus-handle-ephemeral-exit quit-config)))
2895       (gnus-message 3 "Can't select group")
2896       nil)
2897      ;; The user did a `C-g' while prompting for number of articles,
2898      ;; so we exit this group.
2899      ((eq did-select 'quit)
2900       (and (eq major-mode 'gnus-summary-mode)
2901            (not (equal (current-buffer) kill-buffer))
2902            (kill-buffer (current-buffer)))
2903       (when kill-buffer
2904         (gnus-kill-or-deaden-summary kill-buffer))
2905       (if (not quit-config)
2906           (progn
2907             (set-buffer gnus-group-buffer)
2908             (gnus-group-jump-to-group group)
2909             (gnus-group-next-unread-group 1)
2910             (gnus-configure-windows 'group 'force))
2911         (gnus-handle-ephemeral-exit quit-config))
2912       ;; Finally signal the quit.
2913       (signal 'quit nil))
2914      ;; The group was successfully selected.
2915      (t
2916       (gnus-set-global-variables)
2917       ;; Save the active value in effect when the group was entered.
2918       (setq gnus-newsgroup-active
2919             (gnus-copy-sequence
2920              (gnus-active gnus-newsgroup-name)))
2921       ;; You can change the summary buffer in some way with this hook.
2922       (gnus-run-hooks 'gnus-select-group-hook)
2923       ;; Set any local variables in the group parameters.
2924       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2925       (gnus-update-format-specifications
2926        nil 'summary 'summary-mode 'summary-dummy)
2927       (gnus-update-summary-mark-positions)
2928       ;; Do score processing.
2929       (when gnus-use-scoring
2930         (gnus-possibly-score-headers))
2931       ;; Check whether to fill in the gaps in the threads.
2932       (when gnus-build-sparse-threads
2933         (gnus-build-sparse-threads))
2934       ;; Find the initial limit.
2935       (if gnus-show-threads
2936           (if show-all
2937               (let ((gnus-newsgroup-dormant nil))
2938                 (gnus-summary-initial-limit show-all))
2939             (gnus-summary-initial-limit show-all))
2940         ;; When untreaded, all articles are always shown.
2941         (setq gnus-newsgroup-limit
2942               (mapcar
2943                (lambda (header) (mail-header-number header))
2944                gnus-newsgroup-headers)))
2945       ;; Generate the summary buffer.
2946       (unless no-display
2947         (gnus-summary-prepare))
2948       (when gnus-use-trees
2949         (gnus-tree-open group)
2950         (setq gnus-summary-highlight-line-function
2951               'gnus-tree-highlight-article))
2952       ;; If the summary buffer is empty, but there are some low-scored
2953       ;; articles or some excluded dormants, we include these in the
2954       ;; buffer.
2955       (when (and (zerop (buffer-size))
2956                  (not no-display))
2957         (cond (gnus-newsgroup-dormant
2958                (gnus-summary-limit-include-dormant))
2959               ((and gnus-newsgroup-scored show-all)
2960                (gnus-summary-limit-include-expunged t))))
2961       ;; Function `gnus-apply-kill-file' must be called in this hook.
2962       (gnus-run-hooks 'gnus-apply-kill-hook)
2963       (if (and (zerop (buffer-size))
2964                (not no-display))
2965           (progn
2966             ;; This newsgroup is empty.
2967             (gnus-summary-catchup-and-exit nil t)
2968             (gnus-message 6 "No unread news")
2969             (when kill-buffer
2970               (gnus-kill-or-deaden-summary kill-buffer))
2971             ;; Return nil from this function.
2972             nil)
2973         ;; Hide conversation thread subtrees.  We cannot do this in
2974         ;; gnus-summary-prepare-hook since kill processing may not
2975         ;; work with hidden articles.
2976         (and gnus-show-threads
2977              gnus-thread-hide-subtree
2978              (gnus-summary-hide-all-threads))
2979         (when kill-buffer
2980           (gnus-kill-or-deaden-summary kill-buffer))
2981         ;; Show first unread article if requested.
2982         (if (and (not no-article)
2983                  (not no-display)
2984                  gnus-newsgroup-unreads
2985                  gnus-auto-select-first)
2986             (progn
2987               (gnus-configure-windows 'summary)
2988               (cond
2989                ((eq gnus-auto-select-first 'best)
2990                 (gnus-summary-best-unread-article))
2991                ((eq gnus-auto-select-first t)
2992                 (gnus-summary-first-unread-article))
2993                ((gnus-functionp gnus-auto-select-first)
2994                 (funcall gnus-auto-select-first))))
2995           ;; Don't select any articles, just move point to the first
2996           ;; article in the group.
2997           (goto-char (point-min))
2998           (gnus-summary-position-point)
2999           (gnus-configure-windows 'summary 'force)
3000           (gnus-set-mode-line 'summary))
3001         (when (get-buffer-window gnus-group-buffer t)
3002           ;; Gotta use windows, because recenter does weird stuff if
3003           ;; the current buffer ain't the displayed window.
3004           (let ((owin (selected-window)))
3005             (select-window (get-buffer-window gnus-group-buffer t))
3006             (when (gnus-group-goto-group group)
3007               (recenter))
3008             (select-window owin)))
3009         ;; Mark this buffer as "prepared".
3010         (setq gnus-newsgroup-prepared t)
3011         (gnus-run-hooks 'gnus-summary-prepared-hook)
3012         t)))))
3013
3014 (defun gnus-summary-prepare ()
3015   "Generate the summary buffer."
3016   (interactive)
3017   (let ((buffer-read-only nil))
3018     (erase-buffer)
3019     (setq gnus-newsgroup-data nil
3020           gnus-newsgroup-data-reverse nil)
3021     (gnus-run-hooks 'gnus-summary-generate-hook)
3022     ;; Generate the buffer, either with threads or without.
3023     (when gnus-newsgroup-headers
3024       (gnus-summary-prepare-threads
3025        (if gnus-show-threads
3026            (gnus-sort-gathered-threads
3027             (funcall gnus-summary-thread-gathering-function
3028                      (gnus-sort-threads
3029                       (gnus-cut-threads (gnus-make-threads)))))
3030          ;; Unthreaded display.
3031          (gnus-sort-articles gnus-newsgroup-headers))))
3032     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3033     ;; Call hooks for modifying summary buffer.
3034     (goto-char (point-min))
3035     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3036
3037 (defsubst gnus-general-simplify-subject (subject)
3038   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3039   (setq subject
3040         (cond
3041          ;; Truncate the subject.
3042          (gnus-simplify-subject-functions
3043           (gnus-map-function gnus-simplify-subject-functions subject))
3044          ((numberp gnus-summary-gather-subject-limit)
3045           (setq subject (gnus-simplify-subject-re subject))
3046           (if (> (length subject) gnus-summary-gather-subject-limit)
3047               (substring subject 0 gnus-summary-gather-subject-limit)
3048             subject))
3049          ;; Fuzzily simplify it.
3050          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3051           (gnus-simplify-subject-fuzzy subject))
3052          ;; Just remove the leading "Re:".
3053          (t
3054           (gnus-simplify-subject-re subject))))
3055
3056   (if (and gnus-summary-gather-exclude-subject
3057            (string-match gnus-summary-gather-exclude-subject subject))
3058       nil                               ; This article shouldn't be gathered
3059     subject))
3060
3061 (defun gnus-summary-simplify-subject-query ()
3062   "Query where the respool algorithm would put this article."
3063   (interactive)
3064   (gnus-summary-select-article)
3065   (message "%s"
3066            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3067
3068 (defun gnus-gather-threads-by-subject (threads)
3069   "Gather threads by looking at Subject headers."
3070   (if (not gnus-summary-make-false-root)
3071       threads
3072     (let ((hashtb (gnus-make-hashtable 1024))
3073           (prev threads)
3074           (result threads)
3075           subject hthread whole-subject)
3076       (while threads
3077         (setq subject (gnus-general-simplify-subject
3078                        (setq whole-subject (mail-header-subject
3079                                             (caar threads)))))
3080         (when subject
3081           (if (setq hthread (gnus-gethash subject hashtb))
3082               (progn
3083                 ;; We enter a dummy root into the thread, if we
3084                 ;; haven't done that already.
3085                 (unless (stringp (caar hthread))
3086                   (setcar hthread (list whole-subject (car hthread))))
3087                 ;; We add this new gathered thread to this gathered
3088                 ;; thread.
3089                 (setcdr (car hthread)
3090                         (nconc (cdar hthread) (list (car threads))))
3091                 ;; Remove it from the list of threads.
3092                 (setcdr prev (cdr threads))
3093                 (setq threads prev))
3094             ;; Enter this thread into the hash table.
3095             (gnus-sethash subject threads hashtb)))
3096         (setq prev threads)
3097         (setq threads (cdr threads)))
3098       result)))
3099
3100 (defun gnus-gather-threads-by-references (threads)
3101   "Gather threads by looking at References headers."
3102   (let ((idhashtb (gnus-make-hashtable 1024))
3103         (thhashtb (gnus-make-hashtable 1024))
3104         (prev threads)
3105         (result threads)
3106         ids references id gthread gid entered ref)
3107     (while threads
3108       (when (setq references (mail-header-references (caar threads)))
3109         (setq id (mail-header-id (caar threads))
3110               ids (gnus-split-references references)
3111               entered nil)
3112         (while (setq ref (pop ids))
3113           (setq ids (delete ref ids))
3114           (if (not (setq gid (gnus-gethash ref idhashtb)))
3115               (progn
3116                 (gnus-sethash ref id idhashtb)
3117                 (gnus-sethash id threads thhashtb))
3118             (setq gthread (gnus-gethash gid thhashtb))
3119             (unless entered
3120               ;; We enter a dummy root into the thread, if we
3121               ;; haven't done that already.
3122               (unless (stringp (caar gthread))
3123                 (setcar gthread (list (mail-header-subject (caar gthread))
3124                                       (car gthread))))
3125               ;; We add this new gathered thread to this gathered
3126               ;; thread.
3127               (setcdr (car gthread)
3128                       (nconc (cdar gthread) (list (car threads)))))
3129             ;; Add it into the thread hash table.
3130             (gnus-sethash id gthread thhashtb)
3131             (setq entered t)
3132             ;; Remove it from the list of threads.
3133             (setcdr prev (cdr threads))
3134             (setq threads prev))))
3135       (setq prev threads)
3136       (setq threads (cdr threads)))
3137     result))
3138
3139 (defun gnus-sort-gathered-threads (threads)
3140   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3141   (let ((result threads))
3142     (while threads
3143       (when (stringp (caar threads))
3144         (setcdr (car threads)
3145                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3146       (setq threads (cdr threads)))
3147     result))
3148
3149 (defun gnus-thread-loop-p (root thread)
3150   "Say whether ROOT is in THREAD."
3151   (let ((stack (list thread))
3152         (infloop 0)
3153         th)
3154     (while (setq thread (pop stack))
3155       (setq th (cdr thread))
3156       (while (and th
3157                   (not (eq (caar th) root)))
3158         (pop th))
3159       (if th
3160           ;; We have found a loop.
3161           (let (ref-dep)
3162             (setcdr thread (delq (car th) (cdr thread)))
3163             (if (boundp (setq ref-dep (intern "none"
3164                                               gnus-newsgroup-dependencies)))
3165                 (setcdr (symbol-value ref-dep)
3166                         (nconc (cdr (symbol-value ref-dep))
3167                                (list (car th))))
3168               (set ref-dep (list nil (car th))))
3169             (setq infloop 1
3170                   stack nil))
3171         ;; Push all the subthreads onto the stack.
3172         (push (cdr thread) stack)))
3173     infloop))
3174
3175 (defun gnus-make-threads ()
3176   "Go through the dependency hashtb and find the roots.  Return all threads."
3177   (let (threads)
3178     (while (catch 'infloop
3179              (mapatoms
3180               (lambda (refs)
3181                 ;; Deal with self-referencing References loops.
3182                 (when (and (car (symbol-value refs))
3183                            (not (zerop
3184                                  (apply
3185                                   '+
3186                                   (mapcar
3187                                    (lambda (thread)
3188                                      (gnus-thread-loop-p
3189                                       (car (symbol-value refs)) thread))
3190                                    (cdr (symbol-value refs)))))))
3191                   (setq threads nil)
3192                   (throw 'infloop t))
3193                 (unless (car (symbol-value refs))
3194                   ;; These threads do not refer back to any other articles,
3195                   ;; so they're roots.
3196                   (setq threads (append (cdr (symbol-value refs)) threads))))
3197               gnus-newsgroup-dependencies)))
3198     threads))
3199
3200 ;; Build the thread tree.
3201 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3202   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3203
3204 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3205 if it was already present.
3206
3207 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3208 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3209 Message-IDs will be renamed be renamed to a unique Message-ID before
3210 being entered.
3211
3212 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3213   (let* ((id (mail-header-id header))
3214          (id-dep (and id (intern id dependencies)))
3215          ref ref-dep ref-header)
3216     ;; Enter this `header' in the `dependencies' table.
3217     (cond
3218      ((not id-dep)
3219       (setq header nil))
3220      ;; The first two cases do the normal part: enter a new `header'
3221      ;; in the `dependencies' table.
3222      ((not (boundp id-dep))
3223       (set id-dep (list header)))
3224      ((null (car (symbol-value id-dep)))
3225       (setcar (symbol-value id-dep) header))
3226
3227      ;; From here the `header' was already present in the
3228      ;; `dependencies' table.
3229      (force-new
3230       ;; Overrides an existing entry;
3231       ;; just set the header part of the entry.
3232       (setcar (symbol-value id-dep) header))
3233
3234      ;; Renames the existing `header' to a unique Message-ID.
3235      ((not gnus-summary-ignore-duplicates)
3236       ;; An article with this Message-ID has already been seen.
3237       ;; We rename the Message-ID.
3238       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3239            (list header))
3240       (mail-header-set-id header id))
3241
3242      ;; The last case ignores an existing entry, except it adds any
3243      ;; additional Xrefs (in case the two articles came from different
3244      ;; servers.
3245      ;; Also sets `header' to `nil' meaning that the `dependencies'
3246      ;; table was *not* modified.
3247      (t
3248       (mail-header-set-xref
3249        (car (symbol-value id-dep))
3250        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3251                    "")
3252                (or (mail-header-xref header) "")))
3253       (setq header nil)))
3254
3255     (when header
3256       ;; First check if that we are not creating a References loop.
3257       (setq ref (gnus-parent-id (mail-header-references header)))
3258       (while (and ref
3259                   (setq ref-dep (intern-soft ref dependencies))
3260                   (boundp ref-dep)
3261                   (setq ref-header (car (symbol-value ref-dep))))
3262         (if (string= id ref)
3263             ;; Yuk!  This is a reference loop.  Make the article be a
3264             ;; root article.
3265             (progn
3266               (mail-header-set-references (car (symbol-value id-dep)) "none")
3267               (setq ref nil))
3268           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3269       (setq ref (gnus-parent-id (mail-header-references header)))
3270       (setq ref-dep (intern (or ref "none") dependencies))
3271       (if (boundp ref-dep)
3272           (setcdr (symbol-value ref-dep)
3273                   (nconc (cdr (symbol-value ref-dep))
3274                          (list (symbol-value id-dep))))
3275         (set ref-dep (list nil (symbol-value id-dep)))))
3276     header))
3277
3278 (defun gnus-build-sparse-threads ()
3279   (let ((headers gnus-newsgroup-headers)
3280         (mail-parse-charset gnus-newsgroup-charset)
3281         (gnus-summary-ignore-duplicates t)
3282         header references generation relations
3283         subject child end new-child date)
3284     ;; First we create an alist of generations/relations, where
3285     ;; generations is how much we trust the relation, and the relation
3286     ;; is parent/child.
3287     (gnus-message 7 "Making sparse threads...")
3288     (save-excursion
3289       (nnheader-set-temp-buffer " *gnus sparse threads*")
3290       (while (setq header (pop headers))
3291         (when (and (setq references (mail-header-references header))
3292                    (not (string= references "")))
3293           (insert references)
3294           (setq child (mail-header-id header)
3295                 subject (mail-header-subject header)
3296                 date (mail-header-date header)
3297                 generation 0)
3298           (while (search-backward ">" nil t)
3299             (setq end (1+ (point)))
3300             (when (search-backward "<" nil t)
3301               (setq new-child (buffer-substring (point) end))
3302               (push (list (incf generation)
3303                           child (setq child new-child)
3304                           subject date)
3305                     relations)))
3306           (when child
3307             (push (list (1+ generation) child nil subject) relations))
3308           (erase-buffer)))
3309       (kill-buffer (current-buffer)))
3310     ;; Sort over trustworthiness.
3311     (mapcar
3312      (lambda (relation)
3313        (when (gnus-dependencies-add-header
3314               (make-full-mail-header-from-decoded-header
3315                gnus-reffed-article-number
3316                (nth 3 relation) "" (or (nth 4 relation) "")
3317                (nth 1 relation)
3318                (or (nth 2 relation) "") 0 0 "")
3319               gnus-newsgroup-dependencies nil)
3320          (push gnus-reffed-article-number gnus-newsgroup-limit)
3321          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3322          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3323                gnus-newsgroup-reads)
3324          (decf gnus-reffed-article-number)))
3325      (sort relations 'car-less-than-car))
3326     (gnus-message 7 "Making sparse threads...done")))
3327
3328 (defun gnus-build-old-threads ()
3329   ;; Look at all the articles that refer back to old articles, and
3330   ;; fetch the headers for the articles that aren't there.  This will
3331   ;; build complete threads - if the roots haven't been expired by the
3332   ;; server, that is.
3333   (let ((mail-parse-charset gnus-newsgroup-charset)
3334         id heads)
3335     (mapatoms
3336      (lambda (refs)
3337        (when (not (car (symbol-value refs)))
3338          (setq heads (cdr (symbol-value refs)))
3339          (while heads
3340            (if (memq (mail-header-number (caar heads))
3341                      gnus-newsgroup-dormant)
3342                (setq heads (cdr heads))
3343              (setq id (symbol-name refs))
3344              (while (and (setq id (gnus-build-get-header id))
3345                          (not (car (gnus-id-to-thread id)))))
3346              (setq heads nil)))))
3347      gnus-newsgroup-dependencies)))
3348
3349 ;; This function has to be called with point after the article number
3350 ;; on the beginning of the line.
3351 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3352   (let ((eol (gnus-point-at-eol))
3353         (buffer (current-buffer))
3354         header)
3355
3356     ;; overview: [num subject from date id refs chars lines misc]
3357     (unwind-protect
3358         (progn
3359           (narrow-to-region (point) eol)
3360           (unless (eobp)
3361             (forward-char))
3362
3363           (setq header
3364                 (make-full-mail-header
3365                  number                         ; number
3366                  (nnheader-nov-field)           ; subject
3367                  (nnheader-nov-field)           ; from
3368                  (nnheader-nov-field)           ; date
3369                  (nnheader-nov-read-message-id) ; id
3370                  (nnheader-nov-field)           ; refs
3371                  (nnheader-nov-read-integer)    ; chars
3372                  (nnheader-nov-read-integer)    ; lines
3373                  (unless (eobp)
3374                    (if (looking-at "Xref: ")
3375                        (goto-char (match-end 0)))
3376                    (nnheader-nov-field))        ; Xref
3377                  (nnheader-nov-parse-extra))))  ; extra
3378
3379       (widen))
3380
3381     (when gnus-alter-header-function
3382       (funcall gnus-alter-header-function header))
3383     (gnus-dependencies-add-header header dependencies force-new)))
3384
3385 (defun gnus-build-get-header (id)
3386   "Look through the buffer of NOV lines and find the header to ID.
3387 Enter this line into the dependencies hash table, and return
3388 the id of the parent article (if any)."
3389   (let ((deps gnus-newsgroup-dependencies)
3390         found header)
3391     (prog1
3392         (save-excursion
3393           (set-buffer nntp-server-buffer)
3394           (let ((case-fold-search nil))
3395             (goto-char (point-min))
3396             (while (and (not found)
3397                         (search-forward id nil t))
3398               (beginning-of-line)
3399               (setq found (looking-at
3400                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3401                                    (regexp-quote id))))
3402               (or found (beginning-of-line 2)))
3403             (when found
3404               (beginning-of-line)
3405               (and
3406                (setq header (gnus-nov-parse-line
3407                              (read (current-buffer)) deps))
3408                (gnus-parent-id (mail-header-references header))))))
3409       (when header
3410         (let ((number (mail-header-number header)))
3411           (push number gnus-newsgroup-limit)
3412           (push header gnus-newsgroup-headers)
3413           (if (memq number gnus-newsgroup-unselected)
3414               (progn
3415                 (push number gnus-newsgroup-unreads)
3416                 (setq gnus-newsgroup-unselected
3417                       (delq number gnus-newsgroup-unselected)))
3418             (push number gnus-newsgroup-ancient)))))))
3419
3420 (defun gnus-build-all-threads ()
3421   "Read all the headers."
3422   (let ((gnus-summary-ignore-duplicates t)
3423         (mail-parse-charset gnus-newsgroup-charset)
3424         (dependencies gnus-newsgroup-dependencies)
3425         header article)
3426     (save-excursion
3427       (set-buffer nntp-server-buffer)
3428       (let ((case-fold-search nil))
3429         (goto-char (point-min))
3430         (while (not (eobp))
3431           (ignore-errors
3432             (setq article (read (current-buffer))
3433                   header (gnus-nov-parse-line article dependencies)))
3434           (when header
3435             (save-excursion
3436               (set-buffer gnus-summary-buffer)
3437               (push header gnus-newsgroup-headers)
3438               (if (memq (setq article (mail-header-number header))
3439                         gnus-newsgroup-unselected)
3440                   (progn
3441                     (push article gnus-newsgroup-unreads)
3442                     (setq gnus-newsgroup-unselected
3443                           (delq article gnus-newsgroup-unselected)))
3444                 (push article gnus-newsgroup-ancient)))
3445             (forward-line 1)))))))
3446
3447 (defun gnus-summary-update-article-line (article header)
3448   "Update the line for ARTICLE using HEADERS."
3449   (let* ((id (mail-header-id header))
3450          (thread (gnus-id-to-thread id)))
3451     (unless thread
3452       (error "Article in no thread"))
3453     ;; Update the thread.
3454     (setcar thread header)
3455     (gnus-summary-goto-subject article)
3456     (let* ((datal (gnus-data-find-list article))
3457            (data (car datal))
3458            (length (when (cdr datal)
3459                      (- (gnus-data-pos data)
3460                         (gnus-data-pos (cadr datal)))))
3461            (buffer-read-only nil)
3462            (level (gnus-summary-thread-level)))
3463       (gnus-delete-line)
3464       (gnus-summary-insert-line
3465        header level nil (gnus-article-mark article)
3466        (memq article gnus-newsgroup-replied)
3467        (memq article gnus-newsgroup-expirable)
3468        ;; Only insert the Subject string when it's different
3469        ;; from the previous Subject string.
3470        (if (and
3471             gnus-show-threads
3472             (gnus-subject-equal
3473              (condition-case ()
3474                  (mail-header-subject
3475                   (gnus-data-header
3476                    (cadr
3477                     (gnus-data-find-list
3478                      article
3479                      (gnus-data-list t)))))
3480                ;; Error on the side of excessive subjects.
3481                (error ""))
3482              (mail-header-subject header)))
3483            ""
3484          (mail-header-subject header))
3485        nil (cdr (assq article gnus-newsgroup-scored))
3486        (memq article gnus-newsgroup-processable))
3487       (when length
3488         (gnus-data-update-list
3489          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3490
3491 (defun gnus-summary-update-article (article &optional iheader)
3492   "Update ARTICLE in the summary buffer."
3493   (set-buffer gnus-summary-buffer)
3494   (let* ((header (gnus-summary-article-header article))
3495          (id (mail-header-id header))
3496          (data (gnus-data-find article))
3497          (thread (gnus-id-to-thread id))
3498          (references (mail-header-references header))
3499          (parent
3500           (gnus-id-to-thread
3501            (or (gnus-parent-id
3502                 (when (and references
3503                            (not (equal "" references)))
3504                   references))
3505                "none")))
3506          (buffer-read-only nil)
3507          (old (car thread)))
3508     (when thread
3509       (unless iheader
3510         (setcar thread nil)
3511         (when parent
3512           (delq thread parent)))
3513       (if (gnus-summary-insert-subject id header)
3514           ;; Set the (possibly) new article number in the data structure.
3515           (gnus-data-set-number data (gnus-id-to-article id))
3516         (setcar thread old)
3517         nil))))
3518
3519 (defun gnus-rebuild-thread (id &optional line)
3520   "Rebuild the thread containing ID.
3521 If LINE, insert the rebuilt thread starting on line LINE."
3522   (let ((buffer-read-only nil)
3523         old-pos current thread data)
3524     (if (not gnus-show-threads)
3525         (setq thread (list (car (gnus-id-to-thread id))))
3526       ;; Get the thread this article is part of.
3527       (setq thread (gnus-remove-thread id)))
3528     (setq old-pos (gnus-point-at-bol))
3529     (setq current (save-excursion
3530                     (and (zerop (forward-line -1))
3531                          (gnus-summary-article-number))))
3532     ;; If this is a gathered thread, we have to go some re-gathering.
3533     (when (stringp (car thread))
3534       (let ((subject (car thread))
3535             roots thr)
3536         (setq thread (cdr thread))
3537         (while thread
3538           (unless (memq (setq thr (gnus-id-to-thread
3539                                    (gnus-root-id
3540                                     (mail-header-id (caar thread)))))
3541                         roots)
3542             (push thr roots))
3543           (setq thread (cdr thread)))
3544         ;; We now have all (unique) roots.
3545         (if (= (length roots) 1)
3546             ;; All the loose roots are now one solid root.
3547             (setq thread (car roots))
3548           (setq thread (cons subject (gnus-sort-threads roots))))))
3549     (let (threads)
3550       ;; We then insert this thread into the summary buffer.
3551       (when line
3552         (goto-char (point-min))
3553         (forward-line (1- line)))
3554       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3555         (if gnus-show-threads
3556             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3557           (gnus-summary-prepare-unthreaded thread))
3558         (setq data (nreverse gnus-newsgroup-data))
3559         (setq threads gnus-newsgroup-threads))
3560       ;; We splice the new data into the data structure.
3561       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3562       ;;!!! then we want to insert at the beginning of the buffer.
3563       ;;!!! That happens to be true with Gnus now, but that may
3564       ;;!!! change in the future.  Perhaps.
3565       (gnus-data-enter-list
3566        (if line nil current) data (- (point) old-pos))
3567       (setq gnus-newsgroup-threads
3568             (nconc threads gnus-newsgroup-threads))
3569       (gnus-data-compute-positions))))
3570
3571 (defun gnus-number-to-header (number)
3572   "Return the header for article NUMBER."
3573   (let ((headers gnus-newsgroup-headers))
3574     (while (and headers
3575                 (not (= number (mail-header-number (car headers)))))
3576       (pop headers))
3577     (when headers
3578       (car headers))))
3579
3580 (defun gnus-parent-headers (in-headers &optional generation)
3581   "Return the headers of the GENERATIONeth parent of HEADERS."
3582   (unless generation
3583     (setq generation 1))
3584   (let ((parent t)
3585         (headers in-headers)
3586         references)
3587     (while (and parent
3588                 (not (zerop generation))
3589                 (setq references (mail-header-references headers)))
3590       (setq headers (if (and references
3591                              (setq parent (gnus-parent-id references)))
3592                         (car (gnus-id-to-thread parent))
3593                       nil))
3594       (decf generation))
3595     (and (not (eq headers in-headers))
3596          headers)))
3597
3598 (defun gnus-id-to-thread (id)
3599   "Return the (sub-)thread where ID appears."
3600   (gnus-gethash id gnus-newsgroup-dependencies))
3601
3602 (defun gnus-id-to-article (id)
3603   "Return the article number of ID."
3604   (let ((thread (gnus-id-to-thread id)))
3605     (when (and thread
3606                (car thread))
3607       (mail-header-number (car thread)))))
3608
3609 (defun gnus-id-to-header (id)
3610   "Return the article headers of ID."
3611   (car (gnus-id-to-thread id)))
3612
3613 (defun gnus-article-displayed-root-p (article)
3614   "Say whether ARTICLE is a root(ish) article."
3615   (let ((level (gnus-summary-thread-level article))
3616         (refs (mail-header-references  (gnus-summary-article-header article)))
3617         particle)
3618     (cond
3619      ((null level) nil)
3620      ((zerop level) t)
3621      ((null refs) t)
3622      ((null (gnus-parent-id refs)) t)
3623      ((and (= 1 level)
3624            (null (setq particle (gnus-id-to-article
3625                                  (gnus-parent-id refs))))
3626            (null (gnus-summary-thread-level particle)))))))
3627
3628 (defun gnus-root-id (id)
3629   "Return the id of the root of the thread where ID appears."
3630   (let (last-id prev)
3631     (while (and id (setq prev (car (gnus-id-to-thread id))))
3632       (setq last-id id
3633             id (gnus-parent-id (mail-header-references prev))))
3634     last-id))
3635
3636 (defun gnus-articles-in-thread (thread)
3637   "Return the list of articles in THREAD."
3638   (cons (mail-header-number (car thread))
3639         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3640
3641 (defun gnus-remove-thread (id &optional dont-remove)
3642   "Remove the thread that has ID in it."
3643   (let (headers thread last-id)
3644     ;; First go up in this thread until we find the root.
3645     (setq last-id (gnus-root-id id)
3646           headers (message-flatten-list (gnus-id-to-thread last-id)))
3647     ;; We have now found the real root of this thread.  It might have
3648     ;; been gathered into some loose thread, so we have to search
3649     ;; through the threads to find the thread we wanted.
3650     (let ((threads gnus-newsgroup-threads)
3651           sub)
3652       (while threads
3653         (setq sub (car threads))
3654         (if (stringp (car sub))
3655             ;; This is a gathered thread, so we look at the roots
3656             ;; below it to find whether this article is in this
3657             ;; gathered root.
3658             (progn
3659               (setq sub (cdr sub))
3660               (while sub
3661                 (when (member (caar sub) headers)
3662                   (setq thread (car threads)
3663                         threads nil
3664                         sub nil))
3665                 (setq sub (cdr sub))))
3666           ;; It's an ordinary thread, so we check it.
3667           (when (eq (car sub) (car headers))
3668             (setq thread sub
3669                   threads nil)))
3670         (setq threads (cdr threads)))
3671       ;; If this article is in no thread, then it's a root.
3672       (if thread
3673           (unless dont-remove
3674             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3675         (setq thread (gnus-id-to-thread last-id)))
3676       (when thread
3677         (prog1
3678             thread                      ; We return this thread.
3679           (unless dont-remove
3680             (if (stringp (car thread))
3681                 (progn
3682                   ;; If we use dummy roots, then we have to remove the
3683                   ;; dummy root as well.
3684                   (when (eq gnus-summary-make-false-root 'dummy)
3685                     ;; We go to the dummy root by going to
3686                     ;; the first sub-"thread", and then one line up.
3687                     (gnus-summary-goto-article
3688                      (mail-header-number (caadr thread)))
3689                     (forward-line -1)
3690                     (gnus-delete-line)
3691                     (gnus-data-compute-positions))
3692                   (setq thread (cdr thread))
3693                   (while thread
3694                     (gnus-remove-thread-1 (car thread))
3695                     (setq thread (cdr thread))))
3696               (gnus-remove-thread-1 thread))))))))
3697
3698 (defun gnus-remove-thread-1 (thread)
3699   "Remove the thread THREAD recursively."
3700   (let ((number (mail-header-number (pop thread)))
3701         d)
3702     (setq thread (reverse thread))
3703     (while thread
3704       (gnus-remove-thread-1 (pop thread)))
3705     (when (setq d (gnus-data-find number))
3706       (goto-char (gnus-data-pos d))
3707       (gnus-summary-show-thread)
3708       (gnus-data-remove
3709        number
3710        (- (gnus-point-at-bol)
3711           (prog1
3712               (1+ (gnus-point-at-eol))
3713             (gnus-delete-line)))))))
3714
3715 (defun gnus-sort-threads-1 (threads func)
3716   (sort (mapcar (lambda (thread)
3717                   (cons (car thread)
3718                         (and (cdr thread)
3719                              (gnus-sort-threads-1 (cdr thread) func))))
3720                 threads) func))
3721
3722 (defun gnus-sort-threads (threads)
3723   "Sort THREADS."
3724   (if (not gnus-thread-sort-functions)
3725       threads
3726     (gnus-message 8 "Sorting threads...")
3727     (prog1
3728         (gnus-sort-threads-1 
3729          threads 
3730          (gnus-make-sort-function gnus-thread-sort-functions))
3731       (gnus-message 8 "Sorting threads...done"))))
3732
3733 (defun gnus-sort-articles (articles)
3734   "Sort ARTICLES."
3735   (when gnus-article-sort-functions
3736     (gnus-message 7 "Sorting articles...")
3737     (prog1
3738         (setq gnus-newsgroup-headers
3739               (sort articles (gnus-make-sort-function
3740                               gnus-article-sort-functions)))
3741       (gnus-message 7 "Sorting articles...done"))))
3742
3743 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3744 (defmacro gnus-thread-header (thread)
3745   "Return header of first article in THREAD.
3746 Note that THREAD must never, ever be anything else than a variable -
3747 using some other form will lead to serious barfage."
3748   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3749   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3750   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3751         (vector thread) 2))
3752
3753 (defsubst gnus-article-sort-by-number (h1 h2)
3754   "Sort articles by article number."
3755   (< (mail-header-number h1)
3756      (mail-header-number h2)))
3757
3758 (defun gnus-thread-sort-by-number (h1 h2)
3759   "Sort threads by root article number."
3760   (gnus-article-sort-by-number
3761    (gnus-thread-header h1) (gnus-thread-header h2)))
3762
3763 (defsubst gnus-article-sort-by-lines (h1 h2)
3764   "Sort articles by article Lines header."
3765   (< (mail-header-lines h1)
3766      (mail-header-lines h2)))
3767
3768 (defun gnus-thread-sort-by-lines (h1 h2)
3769   "Sort threads by root article Lines header."
3770   (gnus-article-sort-by-lines
3771    (gnus-thread-header h1) (gnus-thread-header h2)))
3772
3773 (defsubst gnus-article-sort-by-chars (h1 h2)
3774   "Sort articles by octet length."
3775   (< (mail-header-chars h1)
3776      (mail-header-chars h2)))
3777
3778 (defun gnus-thread-sort-by-chars (h1 h2)
3779   "Sort threads by root article octet length."
3780   (gnus-article-sort-by-chars
3781    (gnus-thread-header h1) (gnus-thread-header h2)))
3782
3783 (defsubst gnus-article-sort-by-author (h1 h2)
3784   "Sort articles by root author."
3785   (string-lessp
3786    (let ((addr (car (mime-entity-read-field h1 'From))))
3787      (or (std11-full-name-string addr)
3788          (std11-address-string addr)
3789          ""))
3790    (let ((addr (car (mime-entity-read-field h2 'From))))
3791      (or (std11-full-name-string addr)
3792          (std11-address-string addr)
3793          ""))
3794    ))
3795
3796 (defun gnus-thread-sort-by-author (h1 h2)
3797   "Sort threads by root author."
3798   (gnus-article-sort-by-author
3799    (gnus-thread-header h1)  (gnus-thread-header h2)))
3800
3801 (defsubst gnus-article-sort-by-subject (h1 h2)
3802   "Sort articles by root subject."
3803   (string-lessp
3804    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3805    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3806
3807 (defun gnus-thread-sort-by-subject (h1 h2)
3808   "Sort threads by root subject."
3809   (gnus-article-sort-by-subject
3810    (gnus-thread-header h1) (gnus-thread-header h2)))
3811
3812 (defsubst gnus-article-sort-by-date (h1 h2)
3813   "Sort articles by root article date."
3814   (time-less-p
3815    (gnus-date-get-time (mail-header-date h1))
3816    (gnus-date-get-time (mail-header-date h2))))
3817
3818 (defun gnus-thread-sort-by-date (h1 h2)
3819   "Sort threads by root article date."
3820   (gnus-article-sort-by-date
3821    (gnus-thread-header h1) (gnus-thread-header h2)))
3822
3823 (defsubst gnus-article-sort-by-score (h1 h2)
3824   "Sort articles by root article score.
3825 Unscored articles will be counted as having a score of zero."
3826   (> (or (cdr (assq (mail-header-number h1)
3827                     gnus-newsgroup-scored))
3828          gnus-summary-default-score 0)
3829      (or (cdr (assq (mail-header-number h2)
3830                     gnus-newsgroup-scored))
3831          gnus-summary-default-score 0)))
3832
3833 (defun gnus-thread-sort-by-score (h1 h2)
3834   "Sort threads by root article score."
3835   (gnus-article-sort-by-score
3836    (gnus-thread-header h1) (gnus-thread-header h2)))
3837
3838 (defun gnus-thread-sort-by-total-score (h1 h2)
3839   "Sort threads by the sum of all scores in the thread.
3840 Unscored articles will be counted as having a score of zero."
3841   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3842
3843 (defun gnus-thread-total-score (thread)
3844   ;; This function find the total score of THREAD.
3845   (cond ((null thread)
3846          0)
3847         ((consp thread)
3848          (if (stringp (car thread))
3849              (apply gnus-thread-score-function 0
3850                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3851            (gnus-thread-total-score-1 thread)))
3852         (t
3853          (gnus-thread-total-score-1 (list thread)))))
3854
3855 (defun gnus-thread-total-score-1 (root)
3856   ;; This function find the total score of the thread below ROOT.
3857   (setq root (car root))
3858   (apply gnus-thread-score-function
3859          (or (append
3860               (mapcar 'gnus-thread-total-score
3861                       (cdr (gnus-id-to-thread (mail-header-id root))))
3862               (when (> (mail-header-number root) 0)
3863                 (list (or (cdr (assq (mail-header-number root)
3864                                      gnus-newsgroup-scored))
3865                           gnus-summary-default-score 0))))
3866              (list gnus-summary-default-score)
3867              '(0))))
3868
3869 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3870 (defvar gnus-tmp-prev-subject nil)
3871 (defvar gnus-tmp-false-parent nil)
3872 (defvar gnus-tmp-root-expunged nil)
3873 (defvar gnus-tmp-dummy-line nil)
3874
3875 (defvar gnus-tmp-header)
3876 (defun gnus-extra-header (type &optional header)
3877   "Return the extra header of TYPE."
3878   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3879       ""))
3880
3881 (defun gnus-summary-prepare-threads (threads)
3882   "Prepare summary buffer from THREADS and indentation LEVEL.
3883 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3884 or a straight list of headers."
3885   (gnus-message 7 "Generating summary...")
3886
3887   (setq gnus-newsgroup-threads threads)
3888   (beginning-of-line)
3889
3890   (let ((gnus-tmp-level 0)
3891         (default-score (or gnus-summary-default-score 0))
3892         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3893         thread number subject stack state gnus-tmp-gathered beg-match
3894         new-roots gnus-tmp-new-adopts thread-end
3895         gnus-tmp-header gnus-tmp-unread
3896         gnus-tmp-replied gnus-tmp-subject-or-nil
3897         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3898         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3899         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3900
3901     (setq gnus-tmp-prev-subject nil)
3902
3903     (if (vectorp (car threads))
3904         ;; If this is a straight (sic) list of headers, then a
3905         ;; threaded summary display isn't required, so we just create
3906         ;; an unthreaded one.
3907         (gnus-summary-prepare-unthreaded threads)
3908
3909       ;; Do the threaded display.
3910
3911       (while (or threads stack gnus-tmp-new-adopts new-roots)
3912
3913         (if (and (= gnus-tmp-level 0)
3914                  (or (not stack)
3915                      (= (caar stack) 0))
3916                  (not gnus-tmp-false-parent)
3917                  (or gnus-tmp-new-adopts new-roots))
3918             (if gnus-tmp-new-adopts
3919                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3920                       thread (list (car gnus-tmp-new-adopts))
3921                       gnus-tmp-header (caar thread)
3922                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3923               (when new-roots
3924                 (setq thread (list (car new-roots))
3925                       gnus-tmp-header (caar thread)
3926                       new-roots (cdr new-roots))))
3927
3928           (if threads
3929               ;; If there are some threads, we do them before the
3930               ;; threads on the stack.
3931               (setq thread threads
3932                     gnus-tmp-header (caar thread))
3933             ;; There were no current threads, so we pop something off
3934             ;; the stack.
3935             (setq state (car stack)
3936                   gnus-tmp-level (car state)
3937                   thread (cdr state)
3938                   stack (cdr stack)
3939                   gnus-tmp-header (caar thread))))
3940
3941         (setq gnus-tmp-false-parent nil)
3942         (setq gnus-tmp-root-expunged nil)
3943         (setq thread-end nil)
3944
3945         (if (stringp gnus-tmp-header)
3946             ;; The header is a dummy root.
3947             (cond
3948              ((eq gnus-summary-make-false-root 'adopt)
3949               ;; We let the first article adopt the rest.
3950               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3951                                                (cddar thread)))
3952               (setq gnus-tmp-gathered
3953                     (nconc (mapcar
3954                             (lambda (h) (mail-header-number (car h)))
3955                             (cddar thread))
3956                            gnus-tmp-gathered))
3957               (setq thread (cons (list (caar thread)
3958                                        (cadar thread))
3959                                  (cdr thread)))
3960               (setq gnus-tmp-level -1
3961                     gnus-tmp-false-parent t))
3962              ((eq gnus-summary-make-false-root 'empty)
3963               ;; We print adopted articles with empty subject fields.
3964               (setq gnus-tmp-gathered
3965                     (nconc (mapcar
3966                             (lambda (h) (mail-header-number (car h)))
3967                             (cddar thread))
3968                            gnus-tmp-gathered))
3969               (setq gnus-tmp-level -1))
3970              ((eq gnus-summary-make-false-root 'dummy)
3971               ;; We remember that we probably want to output a dummy
3972               ;; root.
3973               (setq gnus-tmp-dummy-line gnus-tmp-header)
3974               (setq gnus-tmp-prev-subject gnus-tmp-header))
3975              (t
3976               ;; We do not make a root for the gathered
3977               ;; sub-threads at all.
3978               (setq gnus-tmp-level -1)))
3979
3980           (setq number (mail-header-number gnus-tmp-header)
3981                 subject (mail-header-subject gnus-tmp-header))
3982
3983           (cond
3984            ;; If the thread has changed subject, we might want to make
3985            ;; this subthread into a root.
3986            ((and (null gnus-thread-ignore-subject)
3987                  (not (zerop gnus-tmp-level))
3988                  gnus-tmp-prev-subject
3989                  (not (inline
3990                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3991             (setq new-roots (nconc new-roots (list (car thread)))
3992                   thread-end t
3993                   gnus-tmp-header nil))
3994            ;; If the article lies outside the current limit,
3995            ;; then we do not display it.
3996            ((not (memq number gnus-newsgroup-limit))
3997             (setq gnus-tmp-gathered
3998                   (nconc (mapcar
3999                           (lambda (h) (mail-header-number (car h)))
4000                           (cdar thread))
4001                          gnus-tmp-gathered))
4002             (setq gnus-tmp-new-adopts (if (cdar thread)
4003                                           (append gnus-tmp-new-adopts
4004                                                   (cdar thread))
4005                                         gnus-tmp-new-adopts)
4006                   thread-end t
4007                   gnus-tmp-header nil)
4008             (when (zerop gnus-tmp-level)
4009               (setq gnus-tmp-root-expunged t)))
4010            ;; Perhaps this article is to be marked as read?
4011            ((and gnus-summary-mark-below
4012                  (< (or (cdr (assq number gnus-newsgroup-scored))
4013                         default-score)
4014                     gnus-summary-mark-below)
4015                  ;; Don't touch sparse articles.
4016                  (not (gnus-summary-article-sparse-p number))
4017                  (not (gnus-summary-article-ancient-p number)))
4018             (setq gnus-newsgroup-unreads
4019                   (delq number gnus-newsgroup-unreads))
4020             (if gnus-newsgroup-auto-expire
4021                 (push number gnus-newsgroup-expirable)
4022               (push (cons number gnus-low-score-mark)
4023                     gnus-newsgroup-reads))))
4024
4025           (when gnus-tmp-header
4026             ;; We may have an old dummy line to output before this
4027             ;; article.
4028             (when (and gnus-tmp-dummy-line
4029                        (gnus-subject-equal
4030                         gnus-tmp-dummy-line
4031                         (mail-header-subject gnus-tmp-header)))
4032               (gnus-summary-insert-dummy-line
4033                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4034               (setq gnus-tmp-dummy-line nil))
4035
4036             ;; Compute the mark.
4037             (setq gnus-tmp-unread (gnus-article-mark number))
4038
4039             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4040                                   gnus-tmp-header gnus-tmp-level)
4041                   gnus-newsgroup-data)
4042
4043             ;; Actually insert the line.
4044             (setq
4045              gnus-tmp-subject-or-nil
4046              (cond
4047               ((and gnus-thread-ignore-subject
4048                     gnus-tmp-prev-subject
4049                     (not (inline (gnus-subject-equal
4050                                   gnus-tmp-prev-subject subject))))
4051                subject)
4052               ((zerop gnus-tmp-level)
4053                (if (and (eq gnus-summary-make-false-root 'empty)
4054                         (memq number gnus-tmp-gathered)
4055                         gnus-tmp-prev-subject
4056                         (inline (gnus-subject-equal
4057                                  gnus-tmp-prev-subject subject)))
4058                    gnus-summary-same-subject
4059                  subject))
4060               (t gnus-summary-same-subject)))
4061             (if (and (eq gnus-summary-make-false-root 'adopt)
4062                      (= gnus-tmp-level 1)
4063                      (memq number gnus-tmp-gathered))
4064                 (setq gnus-tmp-opening-bracket ?\<
4065                       gnus-tmp-closing-bracket ?\>)
4066               (setq gnus-tmp-opening-bracket ?\[
4067                     gnus-tmp-closing-bracket ?\]))
4068             (setq
4069              gnus-tmp-indentation
4070              (aref gnus-thread-indent-array gnus-tmp-level)
4071              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4072              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4073                                 gnus-summary-default-score 0)
4074              gnus-tmp-score-char
4075              (if (or (null gnus-summary-default-score)
4076                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4077                          gnus-summary-zcore-fuzz))
4078                  ?  ;Whitespace
4079                (if (< gnus-tmp-score gnus-summary-default-score)
4080                    gnus-score-below-mark gnus-score-over-mark))
4081              gnus-tmp-replied
4082              (cond ((memq number gnus-newsgroup-processable)
4083                     gnus-process-mark)
4084                    ((memq number gnus-newsgroup-cached)
4085                     gnus-cached-mark)
4086                    ((memq number gnus-newsgroup-replied)
4087                     gnus-replied-mark)
4088                    ((memq number gnus-newsgroup-saved)
4089                     gnus-saved-mark)
4090                    (t gnus-unread-mark))
4091              gnus-tmp-from (mail-header-from gnus-tmp-header)
4092              gnus-tmp-name
4093              (cond
4094               ((string-match "<[^>]+> *$" gnus-tmp-from)
4095                (setq beg-match (match-beginning 0))
4096                (or (and (string-match "^\".+\"" gnus-tmp-from)
4097                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4098                    (substring gnus-tmp-from 0 beg-match)))
4099               ((string-match "(.+)" gnus-tmp-from)
4100                (substring gnus-tmp-from
4101                           (1+ (match-beginning 0)) (1- (match-end 0))))
4102               (t gnus-tmp-from)))
4103             (when (string= gnus-tmp-name "")
4104               (setq gnus-tmp-name gnus-tmp-from))
4105             (unless (numberp gnus-tmp-lines)
4106               (setq gnus-tmp-lines 0))
4107             (gnus-put-text-property
4108              (point)
4109              (progn (eval gnus-summary-line-format-spec) (point))
4110              'gnus-number number)
4111             (when gnus-visual-p
4112               (forward-line -1)
4113               (gnus-run-hooks 'gnus-summary-update-hook)
4114               (forward-line 1))
4115
4116             (setq gnus-tmp-prev-subject subject)))
4117
4118         (when (nth 1 thread)
4119           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4120         (incf gnus-tmp-level)
4121         (setq threads (if thread-end nil (cdar thread)))
4122         (unless threads
4123           (setq gnus-tmp-level 0)))))
4124   (gnus-message 7 "Generating summary...done"))
4125
4126 (defun gnus-summary-prepare-unthreaded (headers)
4127   "Generate an unthreaded summary buffer based on HEADERS."
4128   (let (header number mark)
4129
4130     (beginning-of-line)
4131
4132     (while headers
4133       ;; We may have to root out some bad articles...
4134       (when (memq (setq number (mail-header-number
4135                                 (setq header (pop headers))))
4136                   gnus-newsgroup-limit)
4137         ;; Mark article as read when it has a low score.
4138         (when (and gnus-summary-mark-below
4139                    (< (or (cdr (assq number gnus-newsgroup-scored))
4140                           gnus-summary-default-score 0)
4141                       gnus-summary-mark-below)
4142                    (not (gnus-summary-article-ancient-p number)))
4143           (setq gnus-newsgroup-unreads
4144                 (delq number gnus-newsgroup-unreads))
4145           (if gnus-newsgroup-auto-expire
4146               (push number gnus-newsgroup-expirable)
4147             (push (cons number gnus-low-score-mark)
4148                   gnus-newsgroup-reads)))
4149
4150         (setq mark (gnus-article-mark number))
4151         (push (gnus-data-make number mark (1+ (point)) header 0)
4152               gnus-newsgroup-data)
4153         (gnus-summary-insert-line
4154          header 0 number
4155          mark (memq number gnus-newsgroup-replied)
4156          (memq number gnus-newsgroup-expirable)
4157          (mail-header-subject header) nil
4158          (cdr (assq number gnus-newsgroup-scored))
4159          (memq number gnus-newsgroup-processable))))))
4160
4161 (defun gnus-summary-remove-list-identifiers ()
4162   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4163   (let ((regexp (if (stringp gnus-list-identifiers)
4164                     gnus-list-identifiers
4165                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4166     (dolist (header gnus-newsgroup-headers)
4167       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4168                                   " *\\)\\)+\\(Re: +\\)?\\)")
4169                           (mail-header-subject header))
4170         (mail-header-set-subject
4171          header (concat (substring (mail-header-subject header)
4172                                    0 (match-beginning 1))
4173                         (or
4174                          (match-string 3 (mail-header-subject header))
4175                          (match-string 5 (mail-header-subject header)))
4176                         (substring (mail-header-subject header)
4177                                    (match-end 1))))))))
4178
4179 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4180   "Select newsgroup GROUP.
4181 If READ-ALL is non-nil, all articles in the group are selected.
4182 If SELECT-ARTICLES, only select those articles from GROUP."
4183   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4184          ;;!!! Dirty hack; should be removed.
4185          (gnus-summary-ignore-duplicates
4186           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4187               t
4188             gnus-summary-ignore-duplicates))
4189          (info (nth 2 entry))
4190          articles fetched-articles cached)
4191
4192     (unless (gnus-check-server
4193              (setq gnus-current-select-method
4194                    (gnus-find-method-for-group group)))
4195       (error "Couldn't open server"))
4196
4197     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4198         (gnus-activate-group group)     ; Or we can activate it...
4199         (progn                          ; Or we bug out.
4200           (when (equal major-mode 'gnus-summary-mode)
4201             (kill-buffer (current-buffer)))
4202           (error "Couldn't activate group %s: %s"
4203                  group (gnus-status-message group))))
4204
4205     (unless (gnus-request-group group t)
4206       (when (equal major-mode 'gnus-summary-mode)
4207         (kill-buffer (current-buffer)))
4208       (error "Couldn't request group %s: %s"
4209              group (gnus-status-message group)))
4210
4211     (setq gnus-newsgroup-name group)
4212     (setq gnus-newsgroup-unselected nil)
4213     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4214     (gnus-summary-setup-default-charset)
4215
4216     ;; Adjust and set lists of article marks.
4217     (when info
4218       (gnus-adjust-marked-articles info))
4219
4220     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4221     (setq cached
4222           (if (gnus-virtual-group-p group)
4223               gnus-newsgroup-cached
4224             (gnus-cache-articles-in-group group)))
4225
4226     (setq gnus-newsgroup-unreads
4227           (gnus-set-difference
4228            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4229            gnus-newsgroup-dormant))
4230
4231     (setq gnus-newsgroup-processable nil)
4232
4233     (gnus-update-read-articles group gnus-newsgroup-unreads)
4234
4235     (if (setq articles select-articles)
4236         (setq gnus-newsgroup-unselected
4237               (gnus-sorted-intersection
4238                gnus-newsgroup-unreads
4239                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4240       (setq articles (gnus-articles-to-read group read-all)))
4241
4242     (cond
4243      ((null articles)
4244       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4245       'quit)
4246      ((eq articles 0) nil)
4247      (t
4248       ;; Init the dependencies hash table.
4249       (setq gnus-newsgroup-dependencies
4250             (gnus-make-hashtable (length articles)))
4251       (gnus-set-global-variables)
4252       ;; Retrieve the headers and read them in.
4253       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4254       (setq gnus-newsgroup-headers
4255             (gnus-retrieve-parsed-headers
4256              articles gnus-newsgroup-name
4257              ;; We might want to fetch old headers, but
4258              ;; not if there is only 1 article.
4259              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4260                            (not (numberp gnus-fetch-old-headers)))
4261                       (> (length articles) 1))
4262                   gnus-fetch-old-headers)))
4263       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4264
4265       ;; Suppress duplicates?
4266       (when gnus-suppress-duplicates
4267         (gnus-dup-suppress-articles))
4268
4269       ;; Set the initial limit.
4270       (setq gnus-newsgroup-limit (copy-sequence articles))
4271       ;; Remove canceled articles from the list of unread articles.
4272       (setq gnus-newsgroup-unreads
4273             (gnus-set-sorted-intersection
4274              gnus-newsgroup-unreads
4275              (setq fetched-articles
4276                    (mapcar (lambda (headers) (mail-header-number headers))
4277                            gnus-newsgroup-headers))))
4278       ;; Removed marked articles that do not exist.
4279       (gnus-update-missing-marks
4280        (gnus-sorted-complement fetched-articles articles))
4281
4282       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4283       (when cached
4284         (setq gnus-newsgroup-cached cached))
4285
4286       ;; We might want to build some more threads first.
4287       (when (and gnus-fetch-old-headers
4288                  (eq gnus-headers-retrieved-by 'nov))
4289         (if (eq gnus-fetch-old-headers 'invisible)
4290             (gnus-build-all-threads)
4291           (gnus-build-old-threads)))
4292       ;; Let the Gnus agent mark articles as read.
4293       (when gnus-agent
4294         (gnus-agent-get-undownloaded-list))
4295       ;; Remove list identifiers from subject
4296       (when gnus-list-identifiers
4297         (gnus-summary-remove-list-identifiers))
4298       ;; Check whether auto-expire is to be done in this group.
4299       (setq gnus-newsgroup-auto-expire
4300             (gnus-group-auto-expirable-p group))
4301       ;; Set up the article buffer now, if necessary.
4302       (unless gnus-single-article-buffer
4303         (gnus-article-setup-buffer))
4304       ;; First and last article in this newsgroup.
4305       (when gnus-newsgroup-headers
4306         (setq gnus-newsgroup-begin
4307               (mail-header-number (car gnus-newsgroup-headers))
4308               gnus-newsgroup-end
4309               (mail-header-number
4310                (gnus-last-element gnus-newsgroup-headers))))
4311       ;; GROUP is successfully selected.
4312       (or gnus-newsgroup-headers t)))))
4313
4314 (defun gnus-articles-to-read (group &optional read-all)
4315   "Find out what articles the user wants to read."
4316   (let* ((articles
4317           ;; Select all articles if `read-all' is non-nil, or if there
4318           ;; are no unread articles.
4319           (if (or read-all
4320                   (and (zerop (length gnus-newsgroup-marked))
4321                        (zerop (length gnus-newsgroup-unreads)))
4322                   (eq (gnus-group-find-parameter group 'display)
4323                       'all))
4324               (or
4325                (gnus-uncompress-range (gnus-active group))
4326                (gnus-cache-articles-in-group group))
4327             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4328                           (copy-sequence gnus-newsgroup-unreads))
4329                   '<)))
4330          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4331          (scored (length scored-list))
4332          (number (length articles))
4333          (marked (+ (length gnus-newsgroup-marked)
4334                     (length gnus-newsgroup-dormant)))
4335          (select
4336           (cond
4337            ((numberp read-all)
4338             read-all)
4339            (t
4340             (condition-case ()
4341                 (cond
4342                  ((and (or (<= scored marked) (= scored number))
4343                        (natnump gnus-large-newsgroup)
4344                        (> number gnus-large-newsgroup))
4345                   (let* ((cursor-in-echo-area nil)
4346                          (input (read-from-minibuffer
4347                                  (format
4348                                   "How many articles from %s (max %d): "
4349                                   (gnus-limit-string gnus-newsgroup-name 35)
4350                                   number)
4351                                  (cons (number-to-string gnus-large-newsgroup)
4352                                        0))))
4353                     (if (string-match "^[ \t]*$" input)
4354                         number
4355                       input)))
4356                  ((and (> scored marked) (< scored number)
4357                        (> (- scored number) 20))
4358                   (let ((input
4359                          (read-string
4360                           (format "%s %s (%d scored, %d total): "
4361                                   "How many articles from"
4362                                   group scored number))))
4363                     (if (string-match "^[ \t]*$" input)
4364                         number input)))
4365                  (t number))
4366               (quit
4367                (message "Quit getting the articles to read")
4368                nil))))))
4369     (setq select (if (stringp select) (string-to-number select) select))
4370     (if (or (null select) (zerop select))
4371         select
4372       (if (and (not (zerop scored)) (<= (abs select) scored))
4373           (progn
4374             (setq articles (sort scored-list '<))
4375             (setq number (length articles)))
4376         (setq articles (copy-sequence articles)))
4377
4378       (when (< (abs select) number)
4379         (if (< select 0)
4380             ;; Select the N oldest articles.
4381             (setcdr (nthcdr (1- (abs select)) articles) nil)
4382           ;; Select the N most recent articles.
4383           (setq articles (nthcdr (- number select) articles))))
4384       (setq gnus-newsgroup-unselected
4385             (gnus-sorted-intersection
4386              gnus-newsgroup-unreads
4387              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4388       (when gnus-alter-articles-to-read-function
4389         (setq gnus-newsgroup-unreads
4390               (sort
4391                (funcall gnus-alter-articles-to-read-function
4392                         gnus-newsgroup-name gnus-newsgroup-unreads)
4393                '<)))
4394       articles)))
4395
4396 (defun gnus-killed-articles (killed articles)
4397   (let (out)
4398     (while articles
4399       (when (inline (gnus-member-of-range (car articles) killed))
4400         (push (car articles) out))
4401       (setq articles (cdr articles)))
4402     out))
4403
4404 (defun gnus-uncompress-marks (marks)
4405   "Uncompress the mark ranges in MARKS."
4406   (let ((uncompressed '(score bookmark))
4407         out)
4408     (while marks
4409       (if (memq (caar marks) uncompressed)
4410           (push (car marks) out)
4411         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4412       (setq marks (cdr marks)))
4413     out))
4414
4415 (defun gnus-adjust-marked-articles (info)
4416   "Set all article lists and remove all marks that are no longer valid."
4417   (let* ((marked-lists (gnus-info-marks info))
4418          (active (gnus-active (gnus-info-group info)))
4419          (min (car active))
4420          (max (cdr active))
4421          (types gnus-article-mark-lists)
4422          (uncompressed '(score bookmark killed))
4423          marks var articles article mark)
4424
4425     (while marked-lists
4426       (setq marks (pop marked-lists))
4427       (set (setq var (intern (format "gnus-newsgroup-%s"
4428                                      (car (rassq (setq mark (car marks))
4429                                                  types)))))
4430            (if (memq (car marks) uncompressed) (cdr marks)
4431              (gnus-uncompress-range (cdr marks))))
4432
4433       (setq articles (symbol-value var))
4434
4435       ;; All articles have to be subsets of the active articles.
4436       (cond
4437        ;; Adjust "simple" lists.
4438        ((memq mark '(tick dormant expire reply save))
4439         (while articles
4440           (when (or (< (setq article (pop articles)) min) (> article max))
4441             (set var (delq article (symbol-value var))))))
4442        ;; Adjust assocs.
4443        ((memq mark uncompressed)
4444         (when (not (listp (cdr (symbol-value var))))
4445           (set var (list (symbol-value var))))
4446         (when (not (listp (cdr articles)))
4447           (setq articles (list articles)))
4448         (while articles
4449           (when (or (not (consp (setq article (pop articles))))
4450                     (< (car article) min)
4451                     (> (car article) max))
4452             (set var (delq article (symbol-value var))))))))))
4453
4454 (defun gnus-update-missing-marks (missing)
4455   "Go through the list of MISSING articles and remove them from the mark lists."
4456   (when missing
4457     (let ((types gnus-article-mark-lists)
4458           var m)
4459       ;; Go through all types.
4460       (while types
4461         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4462         (when (symbol-value var)
4463           ;; This list has articles.  So we delete all missing articles
4464           ;; from it.
4465           (setq m missing)
4466           (while m
4467             (set var (delq (pop m) (symbol-value var)))))))))
4468
4469 (defun gnus-update-marks ()
4470   "Enter the various lists of marked articles into the newsgroup info list."
4471   (let ((types gnus-article-mark-lists)
4472         (info (gnus-get-info gnus-newsgroup-name))
4473         (uncompressed '(score bookmark killed))
4474         type list newmarked symbol delta-marks)
4475     (when info
4476       ;; Add all marks lists to the list of marks lists.
4477       (while (setq type (pop types))
4478         (setq list (symbol-value
4479                     (setq symbol
4480                           (intern (format "gnus-newsgroup-%s"
4481                                           (car type))))))
4482
4483         (when list
4484           ;; Get rid of the entries of the articles that have the
4485           ;; default score.
4486           (when (and (eq (cdr type) 'score)
4487                      gnus-save-score
4488                      list)
4489             (let* ((arts list)
4490                    (prev (cons nil list))
4491                    (all prev))
4492               (while arts
4493                 (if (or (not (consp (car arts)))
4494                         (= (cdar arts) gnus-summary-default-score))
4495                     (setcdr prev (cdr arts))
4496                   (setq prev arts))
4497                 (setq arts (cdr arts)))
4498               (setq list (cdr all)))))
4499
4500         (unless (memq (cdr type) uncompressed)
4501           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4502
4503         (when (gnus-check-backend-function
4504                'request-set-mark gnus-newsgroup-name)
4505           ;; propagate flags to server, with the following exceptions:
4506           ;; uncompressed:s are not proper flags (they are cons cells)
4507           ;; cache is a internal gnus flag
4508           ;; download are local to one gnus installation (well)
4509           ;; unsend are for nndraft groups only
4510           ;; xxx: generality of this?  this suits nnimap anyway
4511           (unless (memq (cdr type) (append '(cache download unsend)
4512                                            uncompressed))
4513             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4514                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4515                    (add (gnus-remove-from-range
4516                          (gnus-copy-sequence list) old)))
4517               (when add
4518                 (push (list add 'add (list (cdr type))) delta-marks))
4519               (when del
4520                 (push (list del 'del (list (cdr type))) delta-marks)))))
4521
4522         (when list
4523           (push (cons (cdr type) list) newmarked)))
4524
4525       (when delta-marks
4526         (unless (gnus-check-group gnus-newsgroup-name)
4527           (error "Can't open server for %s" gnus-newsgroup-name))
4528         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4529
4530       ;; Enter these new marks into the info of the group.
4531       (if (nthcdr 3 info)
4532           (setcar (nthcdr 3 info) newmarked)
4533         ;; Add the marks lists to the end of the info.
4534         (when newmarked
4535           (setcdr (nthcdr 2 info) (list newmarked))))
4536
4537       ;; Cut off the end of the info if there's nothing else there.
4538       (let ((i 5))
4539         (while (and (> i 2)
4540                     (not (nth i info)))
4541           (when (nthcdr (decf i) info)
4542             (setcdr (nthcdr i info) nil)))))))
4543
4544 (defun gnus-set-mode-line (where)
4545   "Set the mode line of the article or summary buffers.
4546 If WHERE is `summary', the summary mode line format will be used."
4547   ;; Is this mode line one we keep updated?
4548   (when (and (memq where gnus-updated-mode-lines)
4549              (symbol-value
4550               (intern (format "gnus-%s-mode-line-format-spec" where))))
4551     (let (mode-string)
4552       (save-excursion
4553         ;; We evaluate this in the summary buffer since these
4554         ;; variables are buffer-local to that buffer.
4555         (set-buffer gnus-summary-buffer)
4556         ;; We bind all these variables that are used in the `eval' form
4557         ;; below.
4558         (let* ((mformat (symbol-value
4559                          (intern
4560                           (format "gnus-%s-mode-line-format-spec" where))))
4561                (gnus-tmp-group-name (gnus-group-name-decode
4562                                      gnus-newsgroup-name
4563                                      (gnus-group-name-charset
4564                                       nil
4565                                       gnus-newsgroup-name)))
4566                (gnus-tmp-article-number (or gnus-current-article 0))
4567                (gnus-tmp-unread gnus-newsgroup-unreads)
4568                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4569                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4570                (gnus-tmp-unread-and-unselected
4571                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4572                             (zerop gnus-tmp-unselected))
4573                        "")
4574                       ((zerop gnus-tmp-unselected)
4575                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4576                       (t (format "{%d(+%d) more}"
4577                                  gnus-tmp-unread-and-unticked
4578                                  gnus-tmp-unselected))))
4579                (gnus-tmp-subject
4580                 (if (and gnus-current-headers
4581                          (vectorp gnus-current-headers))
4582                     (gnus-mode-string-quote
4583                      (mail-header-subject gnus-current-headers))
4584                   ""))
4585                bufname-length max-len
4586                gnus-tmp-header);; passed as argument to any user-format-funcs
4587           (setq mode-string (eval mformat))
4588           (setq bufname-length (if (string-match "%b" mode-string)
4589                                    (- (length
4590                                        (buffer-name
4591                                         (if (eq where 'summary)
4592                                             nil
4593                                           (get-buffer gnus-article-buffer))))
4594                                       2)
4595                                  0))
4596           (setq max-len (max 4 (if gnus-mode-non-string-length
4597                                    (- (window-width)
4598                                       gnus-mode-non-string-length
4599                                       bufname-length)
4600                                  (length mode-string))))
4601           ;; We might have to chop a bit of the string off...
4602           (when (> (length mode-string) max-len)
4603             (setq mode-string
4604                   (concat (gnus-truncate-string mode-string (- max-len 3))
4605                           "...")))
4606           ;; Pad the mode string a bit.
4607           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4608       ;; Update the mode line.
4609       (setq mode-line-buffer-identification
4610             (gnus-mode-line-buffer-identification (list mode-string)))
4611       (set-buffer-modified-p t))))
4612
4613 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4614   "Go through the HEADERS list and add all Xrefs to a hash table.
4615 The resulting hash table is returned, or nil if no Xrefs were found."
4616   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4617          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4618          (xref-hashtb (gnus-make-hashtable))
4619          start group entry number xrefs header)
4620     (while headers
4621       (setq header (pop headers))
4622       (when (and (setq xrefs (mail-header-xref header))
4623                  (not (memq (setq number (mail-header-number header))
4624                             unreads)))
4625         (setq start 0)
4626         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4627           (setq start (match-end 0))
4628           (setq group (if prefix
4629                           (concat prefix (substring xrefs (match-beginning 1)
4630                                                     (match-end 1)))
4631                         (substring xrefs (match-beginning 1) (match-end 1))))
4632           (setq number
4633                 (string-to-int (substring xrefs (match-beginning 2)
4634                                           (match-end 2))))
4635           (if (setq entry (gnus-gethash group xref-hashtb))
4636               (setcdr entry (cons number (cdr entry)))
4637             (gnus-sethash group (cons number nil) xref-hashtb)))))
4638     (and start xref-hashtb)))
4639
4640 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4641   "Look through all the headers and mark the Xrefs as read."
4642   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4643         name entry info xref-hashtb idlist method nth4)
4644     (save-excursion
4645       (set-buffer gnus-group-buffer)
4646       (when (setq xref-hashtb
4647                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4648         (mapatoms
4649          (lambda (group)
4650            (unless (string= from-newsgroup (setq name (symbol-name group)))
4651              (setq idlist (symbol-value group))
4652              ;; Dead groups are not updated.
4653              (and (prog1
4654                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4655                             info (nth 2 entry))
4656                     (when (stringp (setq nth4 (gnus-info-method info)))
4657                       (setq nth4 (gnus-server-to-method nth4))))
4658                   ;; Only do the xrefs if the group has the same
4659                   ;; select method as the group we have just read.
4660                   (or (gnus-methods-equal-p
4661                        nth4 (gnus-find-method-for-group from-newsgroup))
4662                       virtual
4663                       (equal nth4 (setq method (gnus-find-method-for-group
4664                                                 from-newsgroup)))
4665                       (and (equal (car nth4) (car method))
4666                            (equal (nth 1 nth4) (nth 1 method))))
4667                   gnus-use-cross-reference
4668                   (or (not (eq gnus-use-cross-reference t))
4669                       virtual
4670                       ;; Only do cross-references on subscribed
4671                       ;; groups, if that is what is wanted.
4672                       (<= (gnus-info-level info) gnus-level-subscribed))
4673                   (gnus-group-make-articles-read name idlist))))
4674          xref-hashtb)))))
4675
4676 (defun gnus-compute-read-articles (group articles)
4677   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4678          (info (nth 2 entry))
4679          (active (gnus-active group))
4680          ninfo)
4681     (when entry
4682       ;; First peel off all invalid article numbers.
4683       (when active
4684         (let ((ids articles)
4685               id first)
4686           (while (setq id (pop ids))
4687             (when (and first (> id (cdr active)))
4688               ;; We'll end up in this situation in one particular
4689               ;; obscure situation.  If you re-scan a group and get
4690               ;; a new article that is cross-posted to a different
4691               ;; group that has not been re-scanned, you might get
4692               ;; crossposted article that has a higher number than
4693               ;; Gnus believes possible.  So we re-activate this
4694               ;; group as well.  This might mean doing the
4695               ;; crossposting thingy will *increase* the number
4696               ;; of articles in some groups.  Tsk, tsk.
4697               (setq active (or (gnus-activate-group group) active)))
4698             (when (or (> id (cdr active))
4699                       (< id (car active)))
4700               (setq articles (delq id articles))))))
4701       ;; If the read list is nil, we init it.
4702       (if (and active
4703                (null (gnus-info-read info))
4704                (> (car active) 1))
4705           (setq ninfo (cons 1 (1- (car active))))
4706         (setq ninfo (gnus-info-read info)))
4707       ;; Then we add the read articles to the range.
4708       (gnus-add-to-range
4709        ninfo (setq articles (sort articles '<))))))
4710
4711 (defun gnus-group-make-articles-read (group articles)
4712   "Update the info of GROUP to say that ARTICLES are read."
4713   (let* ((num 0)
4714          (entry (gnus-gethash group gnus-newsrc-hashtb))
4715          (info (nth 2 entry))
4716          (active (gnus-active group))
4717          range)
4718     (when entry
4719       (setq range (gnus-compute-read-articles group articles))
4720       (save-excursion
4721         (set-buffer gnus-group-buffer)
4722         (gnus-undo-register
4723           `(progn
4724              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4725              (gnus-info-set-read ',info ',(gnus-info-read info))
4726              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4727              (gnus-group-update-group ,group t))))
4728       ;; Add the read articles to the range.
4729       (gnus-info-set-read info range)
4730       ;; Then we have to re-compute how many unread
4731       ;; articles there are in this group.
4732       (when active
4733         (cond
4734          ((not range)
4735           (setq num (- (1+ (cdr active)) (car active))))
4736          ((not (listp (cdr range)))
4737           (setq num (- (cdr active) (- (1+ (cdr range))
4738                                        (car range)))))
4739          (t
4740           (while range
4741             (if (numberp (car range))
4742                 (setq num (1+ num))
4743               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4744             (setq range (cdr range)))
4745           (setq num (- (cdr active) num))))
4746         ;; Update the number of unread articles.
4747         (setcar entry num)
4748         ;; Update the group buffer.
4749         (gnus-group-update-group group t)))))
4750
4751 (defvar gnus-newsgroup-none-id 0)
4752
4753 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4754   (let ((cur nntp-server-buffer)
4755         (dependencies
4756          (or dependencies
4757              (save-excursion (set-buffer gnus-summary-buffer)
4758                              gnus-newsgroup-dependencies)))
4759         headers id end ref
4760         (mail-parse-charset gnus-newsgroup-charset)
4761         (mail-parse-ignored-charsets
4762          (save-excursion (condition-case nil
4763                              (set-buffer gnus-summary-buffer)
4764                            (error))
4765                          gnus-newsgroup-ignored-charsets)))
4766     (save-excursion
4767       (set-buffer nntp-server-buffer)
4768       ;; Translate all TAB characters into SPACE characters.
4769       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4770       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4771       (gnus-run-hooks 'gnus-parse-headers-hook)
4772       (let ((case-fold-search t)
4773             in-reply-to header p lines chars ctype)
4774         (goto-char (point-min))
4775         ;; Search to the beginning of the next header.  Error messages
4776         ;; do not begin with 2 or 3.
4777         (while (re-search-forward "^[23][0-9]+ " nil t)
4778           (setq id nil
4779                 ref nil)
4780           ;; This implementation of this function, with nine
4781           ;; search-forwards instead of the one re-search-forward and
4782           ;; a case (which basically was the old function) is actually
4783           ;; about twice as fast, even though it looks messier.  You
4784           ;; can't have everything, I guess.  Speed and elegance
4785           ;; doesn't always go hand in hand.
4786           (setq
4787            header
4788            (make-full-mail-header
4789             ;; Number.
4790             (prog1
4791                 (read cur)
4792               (end-of-line)
4793               (setq p (point))
4794               (narrow-to-region (point)
4795                                 (or (and (search-forward "\n.\n" nil t)
4796                                          (- (point) 2))
4797                                     (point))))
4798             ;; Subject.
4799             (progn
4800               (goto-char p)
4801               (if (search-forward "\nsubject: " nil t)
4802                   (nnheader-header-value)
4803                 "(none)"))
4804             ;; From.
4805             (progn
4806               (goto-char p)
4807               (if (or (search-forward "\nfrom: " nil t)
4808                       (search-forward "\nfrom:" nil t))
4809                   (nnheader-header-value)
4810                 "(nobody)"))
4811             ;; Date.
4812             (progn
4813               (goto-char p)
4814               (if (search-forward "\ndate: " nil t)
4815                   (nnheader-header-value)
4816                 ""))
4817             ;; Message-ID.
4818             (progn
4819               (goto-char p)
4820               (setq id (if (re-search-forward
4821                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4822                            ;; We do it this way to make sure the Message-ID
4823                            ;; is (somewhat) syntactically valid.
4824                            (buffer-substring (match-beginning 1)
4825                                              (match-end 1))
4826                          ;; If there was no message-id, we just fake one
4827                          ;; to make subsequent routines simpler.
4828                          (nnheader-generate-fake-message-id))))
4829             ;; References.
4830             (progn
4831               (goto-char p)
4832               (if (search-forward "\nreferences: " nil t)
4833                   (progn
4834                     (setq end (point))
4835                     (prog1
4836                         (nnheader-header-value)
4837                       (setq ref
4838                             (buffer-substring
4839                              (progn
4840                                ;; (end-of-line)
4841                                (search-backward ">" end t)
4842                                (1+ (point)))
4843                              (progn
4844                                (search-backward "<" end t)
4845                                (point))))))
4846                 ;; Get the references from the in-reply-to header if there
4847                 ;; were no references and the in-reply-to header looks
4848                 ;; promising.
4849                 (if (and (search-forward "\nin-reply-to: " nil t)
4850                          (setq in-reply-to (nnheader-header-value))
4851                          (string-match "<[^>]+>" in-reply-to))
4852                     (let (ref2)
4853                       (setq ref (substring in-reply-to (match-beginning 0)
4854                                            (match-end 0)))
4855                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4856                         (setq ref2 (substring in-reply-to (match-beginning 0)
4857                                               (match-end 0)))
4858                         (when (> (length ref2) (length ref))
4859                           (setq ref ref2)))
4860                       ref)
4861                   (setq ref nil))))
4862             ;; Chars.
4863             (progn
4864               (goto-char p)
4865               (if (search-forward "\nchars: " nil t)
4866                   (if (numberp (setq chars (ignore-errors (read cur))))
4867                       chars 0)
4868                 0))
4869             ;; Lines.
4870             (progn
4871               (goto-char p)
4872               (if (search-forward "\nlines: " nil t)
4873                   (if (numberp (setq lines (ignore-errors (read cur))))
4874                       lines 0)
4875                 0))
4876             ;; Xref.
4877             (progn
4878               (goto-char p)
4879               (and (search-forward "\nxref: " nil t)
4880                    (nnheader-header-value)))
4881             ;; Extra.
4882             (when gnus-extra-headers
4883               (let ((extra gnus-extra-headers)
4884                     out)
4885                 (while extra
4886                   (goto-char p)
4887                   (when (search-forward
4888                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4889                     (push (cons (car extra) (nnheader-header-value)) out))
4890                   (pop extra))
4891                 out))))
4892           (goto-char p)
4893           (if (and (search-forward "\ncontent-type: " nil t)
4894                    (setq ctype (nnheader-header-value)))
4895               (mime-entity-set-content-type-internal
4896                header (mime-parse-Content-Type ctype)))
4897           (when (equal id ref)
4898             (setq ref nil))
4899
4900           (when gnus-alter-header-function
4901             (funcall gnus-alter-header-function header)
4902             (setq id (mail-header-id header)
4903                   ref (gnus-parent-id (mail-header-references header))))
4904
4905           (when (setq header
4906                       (gnus-dependencies-add-header
4907                        header dependencies force-new))
4908             (push header headers))
4909           (goto-char (point-max))
4910           (widen))
4911         (nreverse headers)))))
4912
4913 ;; Goes through the xover lines and returns a list of vectors
4914 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4915                                                   force-new dependencies
4916                                                   group also-fetch-heads)
4917   "Parse the news overview data in the server buffer.
4918 Return a list of headers that match SEQUENCE (see
4919 `nntp-retrieve-headers')."
4920   ;; Get the Xref when the users reads the articles since most/some
4921   ;; NNTP servers do not include Xrefs when using XOVER.
4922   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4923   (let ((mail-parse-charset gnus-newsgroup-charset)
4924         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4925         (cur nntp-server-buffer)
4926         (dependencies (or dependencies gnus-newsgroup-dependencies))
4927         number headers header)
4928     (save-excursion
4929       (set-buffer nntp-server-buffer)
4930       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4931       ;; Allow the user to mangle the headers before parsing them.
4932       (gnus-run-hooks 'gnus-parse-headers-hook)
4933       (goto-char (point-min))
4934       (while (not (eobp))
4935         (condition-case ()
4936             (while (and sequence (not (eobp)))
4937               (setq number (read cur))
4938               (while (and sequence
4939                           (< (car sequence) number))
4940                 (setq sequence (cdr sequence)))
4941               (and sequence
4942                    (eq number (car sequence))
4943                    (progn
4944                      (setq sequence (cdr sequence))
4945                      (setq header (inline
4946                                     (gnus-nov-parse-line
4947                                      number dependencies force-new))))
4948                    (push header headers))
4949               (forward-line 1))
4950           (error
4951            (gnus-error 4 "Strange nov line (%d)"
4952                        (count-lines (point-min) (point)))))
4953         (forward-line 1))
4954       ;; A common bug in inn is that if you have posted an article and
4955       ;; then retrieves the active file, it will answer correctly --
4956       ;; the new article is included.  However, a NOV entry for the
4957       ;; article may not have been generated yet, so this may fail.
4958       ;; We work around this problem by retrieving the last few
4959       ;; headers using HEAD.
4960       (if (or (not also-fetch-heads)
4961               (not sequence))
4962           ;; We (probably) got all the headers.
4963           (nreverse headers)
4964         (let ((gnus-nov-is-evil t))
4965           (nconc
4966            (nreverse headers)
4967            (gnus-retrieve-parsed-headers sequence group)
4968            ))))))
4969
4970 (defun gnus-article-get-xrefs ()
4971   "Fill in the Xref value in `gnus-current-headers', if necessary.
4972 This is meant to be called in `gnus-article-internal-prepare-hook'."
4973   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4974                                  gnus-current-headers)))
4975     (or (not gnus-use-cross-reference)
4976         (not headers)
4977         (and (mail-header-xref headers)
4978              (not (string= (mail-header-xref headers) "")))
4979         (let ((case-fold-search t)
4980               xref)
4981           (save-restriction
4982             (nnheader-narrow-to-headers)
4983             (goto-char (point-min))
4984             (when (or (and (not (eobp))
4985                            (eq (downcase (char-after)) ?x)
4986                            (looking-at "Xref:"))
4987                       (search-forward "\nXref:" nil t))
4988               (goto-char (1+ (match-end 0)))
4989               (setq xref (buffer-substring (point)
4990                                            (progn (end-of-line) (point))))
4991               (mail-header-set-xref headers xref)))))))
4992
4993 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4994   "Find article ID and insert the summary line for that article.
4995 OLD-HEADER can either be a header or a line number to insert
4996 the subject line on."
4997   (let* ((line (and (numberp old-header) old-header))
4998          (old-header (and (vectorp old-header) old-header))
4999          (header (cond ((and old-header use-old-header)
5000                         old-header)
5001                        ((and (numberp id)
5002                              (gnus-number-to-header id))
5003                         (gnus-number-to-header id))
5004                        (t
5005                         (gnus-read-header id))))
5006          (number (and (numberp id) id))
5007          d)
5008     (when header
5009       ;; Rebuild the thread that this article is part of and go to the
5010       ;; article we have fetched.
5011       (when (and (not gnus-show-threads)
5012                  old-header)
5013         (when (and number
5014                    (setq d (gnus-data-find (mail-header-number old-header))))
5015           (goto-char (gnus-data-pos d))
5016           (gnus-data-remove
5017            number
5018            (- (gnus-point-at-bol)
5019               (prog1
5020                   (1+ (gnus-point-at-eol))
5021                 (gnus-delete-line))))))
5022       (when old-header
5023         (mail-header-set-number header (mail-header-number old-header)))
5024       (setq gnus-newsgroup-sparse
5025             (delq (setq number (mail-header-number header))
5026                   gnus-newsgroup-sparse))
5027       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5028       (push number gnus-newsgroup-limit)
5029       (gnus-rebuild-thread (mail-header-id header) line)
5030       (gnus-summary-goto-subject number nil t))
5031     (when (and (numberp number)
5032                (> number 0))
5033       ;; We have to update the boundaries even if we can't fetch the
5034       ;; article if ID is a number -- so that the next `P' or `N'
5035       ;; command will fetch the previous (or next) article even
5036       ;; if the one we tried to fetch this time has been canceled.
5037       (when (> number gnus-newsgroup-end)
5038         (setq gnus-newsgroup-end number))
5039       (when (< number gnus-newsgroup-begin)
5040         (setq gnus-newsgroup-begin number))
5041       (setq gnus-newsgroup-unselected
5042             (delq number gnus-newsgroup-unselected)))
5043     ;; Report back a success?
5044     (and header (mail-header-number header))))
5045
5046 ;;; Process/prefix in the summary buffer
5047
5048 (defun gnus-summary-work-articles (n)
5049   "Return a list of articles to be worked upon.
5050 The prefix argument, the list of process marked articles, and the
5051 current article will be taken into consideration."
5052   (save-excursion
5053     (set-buffer gnus-summary-buffer)
5054     (cond
5055      (n
5056       ;; A numerical prefix has been given.
5057       (setq n (prefix-numeric-value n))
5058       (let ((backward (< n 0))
5059             (n (abs (prefix-numeric-value n)))
5060             articles article)
5061         (save-excursion
5062           (while
5063               (and (> n 0)
5064                    (push (setq article (gnus-summary-article-number))
5065                          articles)
5066                    (if backward
5067                        (gnus-summary-find-prev nil article)
5068                      (gnus-summary-find-next nil article)))
5069             (decf n)))
5070         (nreverse articles)))
5071      ((and (gnus-region-active-p) (mark))
5072       (message "region active")
5073       ;; Work on the region between point and mark.
5074       (let ((max (max (point) (mark)))
5075             articles article)
5076         (save-excursion
5077           (goto-char (min (point) (mark)))
5078           (while
5079               (and
5080                (push (setq article (gnus-summary-article-number)) articles)
5081                (gnus-summary-find-next nil article)
5082                (< (point) max)))
5083           (nreverse articles))))
5084      (gnus-newsgroup-processable
5085       ;; There are process-marked articles present.
5086       ;; Save current state.
5087       (gnus-summary-save-process-mark)
5088       ;; Return the list.
5089       (reverse gnus-newsgroup-processable))
5090      (t
5091       ;; Just return the current article.
5092       (list (gnus-summary-article-number))))))
5093
5094 (defmacro gnus-summary-iterate (arg &rest forms)
5095   "Iterate over the process/prefixed articles and do FORMS.
5096 ARG is the interactive prefix given to the command.  FORMS will be
5097 executed with point over the summary line of the articles."
5098   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5099     `(let ((,articles (gnus-summary-work-articles ,arg)))
5100        (while ,articles
5101          (gnus-summary-goto-subject (car ,articles))
5102          ,@forms
5103          (pop ,articles)))))
5104
5105 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5106 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5107
5108 (defun gnus-summary-save-process-mark ()
5109   "Push the current set of process marked articles on the stack."
5110   (interactive)
5111   (push (copy-sequence gnus-newsgroup-processable)
5112         gnus-newsgroup-process-stack))
5113
5114 (defun gnus-summary-kill-process-mark ()
5115   "Push the current set of process marked articles on the stack and unmark."
5116   (interactive)
5117   (gnus-summary-save-process-mark)
5118   (gnus-summary-unmark-all-processable))
5119
5120 (defun gnus-summary-yank-process-mark ()
5121   "Pop the last process mark state off the stack and restore it."
5122   (interactive)
5123   (unless gnus-newsgroup-process-stack
5124     (error "Empty mark stack"))
5125   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5126
5127 (defun gnus-summary-process-mark-set (set)
5128   "Make SET into the current process marked articles."
5129   (gnus-summary-unmark-all-processable)
5130   (while set
5131     (gnus-summary-set-process-mark (pop set))))
5132
5133 ;;; Searching and stuff
5134
5135 (defun gnus-summary-search-group (&optional backward use-level)
5136   "Search for next unread newsgroup.
5137 If optional argument BACKWARD is non-nil, search backward instead."
5138   (save-excursion
5139     (set-buffer gnus-group-buffer)
5140     (when (gnus-group-search-forward
5141            backward nil (if use-level (gnus-group-group-level) nil))
5142       (gnus-group-group-name))))
5143
5144 (defun gnus-summary-best-group (&optional exclude-group)
5145   "Find the name of the best unread group.
5146 If EXCLUDE-GROUP, do not go to this group."
5147   (save-excursion
5148     (set-buffer gnus-group-buffer)
5149     (save-excursion
5150       (gnus-group-best-unread-group exclude-group))))
5151
5152 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5153   (if backward (gnus-summary-find-prev)
5154     (let* ((dummy (gnus-summary-article-intangible-p))
5155            (article (or article (gnus-summary-article-number)))
5156            (arts (gnus-data-find-list article))
5157            result)
5158       (when (and (not dummy)
5159                  (or (not gnus-summary-check-current)
5160                      (not unread)
5161                      (not (gnus-data-unread-p (car arts)))))
5162         (setq arts (cdr arts)))
5163       (when (setq result
5164                   (if unread
5165                       (progn
5166                         (while arts
5167                           (when (or (and undownloaded
5168                                          (eq gnus-undownloaded-mark
5169                                              (gnus-data-mark (car arts))))
5170                                     (gnus-data-unread-p (car arts)))
5171                             (setq result (car arts)
5172                                   arts nil))
5173                           (setq arts (cdr arts)))
5174                         result)
5175                     (car arts)))
5176         (goto-char (gnus-data-pos result))
5177         (gnus-data-number result)))))
5178
5179 (defun gnus-summary-find-prev (&optional unread article)
5180   (let* ((eobp (eobp))
5181          (article (or article (gnus-summary-article-number)))
5182          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5183          result)
5184     (when (and (not eobp)
5185                (or (not gnus-summary-check-current)
5186                    (not unread)
5187                    (not (gnus-data-unread-p (car arts)))))
5188       (setq arts (cdr arts)))
5189     (when (setq result
5190                 (if unread
5191                     (progn
5192                       (while arts
5193                         (when (gnus-data-unread-p (car arts))
5194                           (setq result (car arts)
5195                                 arts nil))
5196                         (setq arts (cdr arts)))
5197                       result)
5198                   (car arts)))
5199       (goto-char (gnus-data-pos result))
5200       (gnus-data-number result))))
5201
5202 (defun gnus-summary-find-subject (subject &optional unread backward article)
5203   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5204          (article (or article (gnus-summary-article-number)))
5205          (articles (gnus-data-list backward))
5206          (arts (gnus-data-find-list article articles))
5207          result)
5208     (when (or (not gnus-summary-check-current)
5209               (not unread)
5210               (not (gnus-data-unread-p (car arts))))
5211       (setq arts (cdr arts)))
5212     (while arts
5213       (and (or (not unread)
5214                (gnus-data-unread-p (car arts)))
5215            (vectorp (gnus-data-header (car arts)))
5216            (gnus-subject-equal
5217             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5218            (setq result (car arts)
5219                  arts nil))
5220       (setq arts (cdr arts)))
5221     (and result
5222          (goto-char (gnus-data-pos result))
5223          (gnus-data-number result))))
5224
5225 (defun gnus-summary-search-forward (&optional unread subject backward)
5226   "Search forward for an article.
5227 If UNREAD, look for unread articles.  If SUBJECT, look for
5228 articles with that subject.  If BACKWARD, search backward instead."
5229   (cond (subject (gnus-summary-find-subject subject unread backward))
5230         (backward (gnus-summary-find-prev unread))
5231         (t (gnus-summary-find-next unread))))
5232
5233 (defun gnus-recenter (&optional n)
5234   "Center point in window and redisplay frame.
5235 Also do horizontal recentering."
5236   (interactive "P")
5237   (when (and gnus-auto-center-summary
5238              (not (eq gnus-auto-center-summary 'vertical)))
5239     (gnus-horizontal-recenter))
5240   (recenter n))
5241
5242 (defun gnus-summary-recenter ()
5243   "Center point in the summary window.
5244 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5245 displayed, no centering will be performed."
5246   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5247   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5248   (interactive)
5249   (let* ((top (cond ((< (window-height) 4) 0)
5250                     ((< (window-height) 7) 1)
5251                     (t (if (numberp gnus-auto-center-summary)
5252                            gnus-auto-center-summary
5253                          2))))
5254          (height (1- (window-height)))
5255          (bottom (save-excursion (goto-char (point-max))
5256                                  (forward-line (- height))
5257                                  (point)))
5258          (window (get-buffer-window (current-buffer))))
5259     ;; The user has to want it.
5260     (when gnus-auto-center-summary
5261       (when (get-buffer-window gnus-article-buffer)
5262         ;; Only do recentering when the article buffer is displayed,
5263         ;; Set the window start to either `bottom', which is the biggest
5264         ;; possible valid number, or the second line from the top,
5265         ;; whichever is the least.
5266         (set-window-start
5267          window (min bottom (save-excursion
5268                               (forward-line (- top)) (point)))
5269          t))
5270       ;; Do horizontal recentering while we're at it.
5271       (when (and (get-buffer-window (current-buffer) t)
5272                  (not (eq gnus-auto-center-summary 'vertical)))
5273         (let ((selected (selected-window)))
5274           (select-window (get-buffer-window (current-buffer) t))
5275           (gnus-summary-position-point)
5276           (gnus-horizontal-recenter)
5277           (select-window selected))))))
5278
5279 (defun gnus-summary-jump-to-group (newsgroup)
5280   "Move point to NEWSGROUP in group mode buffer."
5281   ;; Keep update point of group mode buffer if visible.
5282   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5283       (save-window-excursion
5284         ;; Take care of tree window mode.
5285         (when (get-buffer-window gnus-group-buffer)
5286           (pop-to-buffer gnus-group-buffer))
5287         (gnus-group-jump-to-group newsgroup))
5288     (save-excursion
5289       ;; Take care of tree window mode.
5290       (if (get-buffer-window gnus-group-buffer)
5291           (pop-to-buffer gnus-group-buffer)
5292         (set-buffer gnus-group-buffer))
5293       (gnus-group-jump-to-group newsgroup))))
5294
5295 ;; This function returns a list of article numbers based on the
5296 ;; difference between the ranges of read articles in this group and
5297 ;; the range of active articles.
5298 (defun gnus-list-of-unread-articles (group)
5299   (let* ((read (gnus-info-read (gnus-get-info group)))
5300          (active (or (gnus-active group) (gnus-activate-group group)))
5301          (last (cdr active))
5302          first nlast unread)
5303     ;; If none are read, then all are unread.
5304     (if (not read)
5305         (setq first (car active))
5306       ;; If the range of read articles is a single range, then the
5307       ;; first unread article is the article after the last read
5308       ;; article.  Sounds logical, doesn't it?
5309       (if (and (not (listp (cdr read)))
5310                (or (< (car read) (car active))
5311                    (progn (setq read (list read))
5312                           nil)))
5313           (setq first (max (car active) (1+ (cdr read))))
5314         ;; `read' is a list of ranges.
5315         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5316                                   (caar read)))
5317                   1)
5318           (setq first (car active)))
5319         (while read
5320           (when first
5321             (while (< first nlast)
5322               (push first unread)
5323               (setq first (1+ first))))
5324           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5325           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5326           (setq read (cdr read)))))
5327     ;; And add the last unread articles.
5328     (while (<= first last)
5329       (push first unread)
5330       (setq first (1+ first)))
5331     ;; Return the list of unread articles.
5332     (delq 0 (nreverse unread))))
5333
5334 (defun gnus-list-of-read-articles (group)
5335   "Return a list of unread, unticked and non-dormant articles."
5336   (let* ((info (gnus-get-info group))
5337          (marked (gnus-info-marks info))
5338          (active (gnus-active group)))
5339     (and info active
5340          (gnus-set-difference
5341           (gnus-sorted-complement
5342            (gnus-uncompress-range active)
5343            (gnus-list-of-unread-articles group))
5344           (append
5345            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5346            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5347
5348 ;; Various summary commands
5349
5350 (defun gnus-summary-select-article-buffer ()
5351   "Reconfigure windows to show article buffer."
5352   (interactive)
5353   (if (not (gnus-buffer-live-p gnus-article-buffer))
5354       (error "There is no article buffer for this summary buffer")
5355     (gnus-configure-windows 'article)
5356     (select-window (get-buffer-window gnus-article-buffer))))
5357
5358 (defun gnus-summary-universal-argument (arg)
5359   "Perform any operation on all articles that are process/prefixed."
5360   (interactive "P")
5361   (let ((articles (gnus-summary-work-articles arg))
5362         func article)
5363     (if (eq
5364          (setq
5365           func
5366           (key-binding
5367            (read-key-sequence
5368             (substitute-command-keys
5369              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5370          'undefined)
5371         (gnus-error 1 "Undefined key")
5372       (save-excursion
5373         (while articles
5374           (gnus-summary-goto-subject (setq article (pop articles)))
5375           (let (gnus-newsgroup-processable)
5376             (command-execute func))
5377           (gnus-summary-remove-process-mark article)))))
5378   (gnus-summary-position-point))
5379
5380 (defun gnus-summary-toggle-truncation (&optional arg)
5381   "Toggle truncation of summary lines.
5382 With arg, turn line truncation on iff arg is positive."
5383   (interactive "P")
5384   (setq truncate-lines
5385         (if (null arg) (not truncate-lines)
5386           (> (prefix-numeric-value arg) 0)))
5387   (redraw-display))
5388
5389 (defun gnus-summary-reselect-current-group (&optional all rescan)
5390   "Exit and then reselect the current newsgroup.
5391 The prefix argument ALL means to select all articles."
5392   (interactive "P")
5393   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5394     (error "Ephemeral groups can't be reselected"))
5395   (let ((current-subject (gnus-summary-article-number))
5396         (group gnus-newsgroup-name))
5397     (setq gnus-newsgroup-begin nil)
5398     (gnus-summary-exit)
5399     ;; We have to adjust the point of group mode buffer because
5400     ;; point was moved to the next unread newsgroup by exiting.
5401     (gnus-summary-jump-to-group group)
5402     (when rescan
5403       (save-excursion
5404         (save-window-excursion
5405           ;; Don't show group contents.
5406           (set-window-start (selected-window) (point-max))
5407           (gnus-group-get-new-news-this-group 1))))
5408     (gnus-group-read-group all t)
5409     (gnus-summary-goto-subject current-subject nil t)))
5410
5411 (defun gnus-summary-rescan-group (&optional all)
5412   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5413   (interactive "P")
5414   (gnus-summary-reselect-current-group all t))
5415
5416 (defun gnus-summary-update-info (&optional non-destructive)
5417   (save-excursion
5418     (let ((group gnus-newsgroup-name))
5419       (when group
5420         (when gnus-newsgroup-kill-headers
5421           (setq gnus-newsgroup-killed
5422                 (gnus-compress-sequence
5423                  (nconc
5424                   (gnus-set-sorted-intersection
5425                    (gnus-uncompress-range gnus-newsgroup-killed)
5426                    (setq gnus-newsgroup-unselected
5427                          (sort gnus-newsgroup-unselected '<)))
5428                   (setq gnus-newsgroup-unreads
5429                         (sort gnus-newsgroup-unreads '<)))
5430                  t)))
5431         (unless (listp (cdr gnus-newsgroup-killed))
5432           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5433         (let ((headers gnus-newsgroup-headers))
5434           ;; Set the new ranges of read articles.
5435           (save-excursion
5436             (set-buffer gnus-group-buffer)
5437             (gnus-undo-force-boundary))
5438           (gnus-update-read-articles
5439            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5440           ;; Set the current article marks.
5441           (let ((gnus-newsgroup-scored
5442                  (if (and (not gnus-save-score)
5443                           (not non-destructive))
5444                      nil
5445                    gnus-newsgroup-scored)))
5446             (save-excursion
5447               (gnus-update-marks)))
5448           ;; Do the cross-ref thing.
5449           (when gnus-use-cross-reference
5450             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5451           ;; Do not switch windows but change the buffer to work.
5452           (set-buffer gnus-group-buffer)
5453           (unless (gnus-ephemeral-group-p group)
5454             (gnus-group-update-group group)))))))
5455
5456 (defun gnus-summary-save-newsrc (&optional force)
5457   "Save the current number of read/marked articles in the dribble buffer.
5458 The dribble buffer will then be saved.
5459 If FORCE (the prefix), also save the .newsrc file(s)."
5460   (interactive "P")
5461   (gnus-summary-update-info t)
5462   (if force
5463       (gnus-save-newsrc-file)
5464     (gnus-dribble-save)))
5465
5466 (defun gnus-summary-exit (&optional temporary)
5467   "Exit reading current newsgroup, and then return to group selection mode.
5468 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5469   (interactive)
5470   (gnus-set-global-variables)
5471   (gnus-kill-save-kill-buffer)
5472   (gnus-async-halt-prefetch)
5473   (let* ((group gnus-newsgroup-name)
5474          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5475          (mode major-mode)
5476          (group-point nil)
5477          (buf (current-buffer)))
5478     (unless quit-config
5479       ;; Do adaptive scoring, and possibly save score files.
5480       (when gnus-newsgroup-adaptive
5481         (gnus-score-adaptive))
5482       (when gnus-use-scoring
5483         (gnus-score-save)))
5484     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5485     ;; If we have several article buffers, we kill them at exit.
5486     (unless gnus-single-article-buffer
5487       (gnus-kill-buffer gnus-original-article-buffer)
5488       (setq gnus-article-current nil))
5489     (when gnus-use-cache
5490       (gnus-cache-possibly-remove-articles)
5491       (gnus-cache-save-buffers))
5492     (gnus-async-prefetch-remove-group group)
5493     (when gnus-suppress-duplicates
5494       (gnus-dup-enter-articles))
5495     (when gnus-use-trees
5496       (gnus-tree-close group))
5497     (when gnus-use-cache
5498       (gnus-cache-write-active))
5499     ;; Remove entries for this group.
5500     (nnmail-purge-split-history (gnus-group-real-name group))
5501     ;; Make all changes in this group permanent.
5502     (unless quit-config
5503       (gnus-run-hooks 'gnus-exit-group-hook)
5504       (gnus-summary-update-info))
5505     (gnus-close-group group)
5506     ;; Make sure where we were, and go to next newsgroup.
5507     (set-buffer gnus-group-buffer)
5508     (unless quit-config
5509       (gnus-group-jump-to-group group))
5510     (gnus-run-hooks 'gnus-summary-exit-hook)
5511     (unless (or quit-config
5512                 ;; If this group has disappeared from the summary
5513                 ;; buffer, don't skip forwards.
5514                 (not (string= group (gnus-group-group-name))))
5515       (gnus-group-next-unread-group 1))
5516     (setq group-point (point))
5517     (if temporary
5518         nil                             ;Nothing to do.
5519       ;; If we have several article buffers, we kill them at exit.
5520       (unless gnus-single-article-buffer
5521         (gnus-kill-buffer gnus-article-buffer)
5522         (gnus-kill-buffer gnus-original-article-buffer)
5523         (setq gnus-article-current nil))
5524       (set-buffer buf)
5525       (if (not gnus-kill-summary-on-exit)
5526           (gnus-deaden-summary)
5527         ;; We set all buffer-local variables to nil.  It is unclear why
5528         ;; this is needed, but if we don't, buffer-local variables are
5529         ;; not garbage-collected, it seems.  This would the lead to en
5530         ;; ever-growing Emacs.
5531         (gnus-summary-clear-local-variables)
5532         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5533           (gnus-summary-clear-local-variables))
5534         (when (get-buffer gnus-article-buffer)
5535           (bury-buffer gnus-article-buffer))
5536         ;; We clear the global counterparts of the buffer-local
5537         ;; variables as well, just to be on the safe side.
5538         (set-buffer gnus-group-buffer)
5539         (gnus-summary-clear-local-variables)
5540         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5541           (gnus-summary-clear-local-variables))
5542         ;; Return to group mode buffer.
5543         (when (eq mode 'gnus-summary-mode)
5544           (gnus-kill-buffer buf)))
5545       (setq gnus-current-select-method gnus-select-method)
5546       (pop-to-buffer gnus-group-buffer)
5547       (if (not quit-config)
5548           (progn
5549             (goto-char group-point)
5550             (gnus-configure-windows 'group 'force)
5551             (unless (pos-visible-in-window-p)
5552               (forward-line (/ (static-if (featurep 'xemacs)
5553                                    (window-displayed-height)
5554                                  (1- (window-height)))
5555                                -2))
5556               (set-window-start (selected-window) (point))
5557               (goto-char group-point)))
5558         (gnus-handle-ephemeral-exit quit-config))
5559       ;; Clear the current group name.
5560       (unless quit-config
5561         (setq gnus-newsgroup-name nil)))))
5562
5563 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5564 (defun gnus-summary-exit-no-update (&optional no-questions)
5565   "Quit reading current newsgroup without updating read article info."
5566   (interactive)
5567   (let* ((group gnus-newsgroup-name)
5568          (quit-config (gnus-group-quit-config group)))
5569     (when (or no-questions
5570               gnus-expert-user
5571               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5572       (gnus-async-halt-prefetch)
5573       (mapcar 'funcall
5574               (delq 'gnus-summary-expire-articles
5575                     (copy-sequence gnus-summary-prepare-exit-hook)))
5576       ;; If we have several article buffers, we kill them at exit.
5577       (unless gnus-single-article-buffer
5578         (gnus-kill-buffer gnus-article-buffer)
5579         (gnus-kill-buffer gnus-original-article-buffer)
5580         (setq gnus-article-current nil))
5581       (if (not gnus-kill-summary-on-exit)
5582           (gnus-deaden-summary)
5583         (gnus-close-group group)
5584         (gnus-summary-clear-local-variables)
5585         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5586           (gnus-summary-clear-local-variables))
5587         (set-buffer gnus-group-buffer)
5588         (gnus-summary-clear-local-variables)
5589         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5590           (gnus-summary-clear-local-variables))
5591         (when (get-buffer gnus-summary-buffer)
5592           (kill-buffer gnus-summary-buffer)))
5593       (unless gnus-single-article-buffer
5594         (setq gnus-article-current nil))
5595       (when gnus-use-trees
5596         (gnus-tree-close group))
5597       (gnus-async-prefetch-remove-group group)
5598       (when (get-buffer gnus-article-buffer)
5599         (bury-buffer gnus-article-buffer))
5600       ;; Return to the group buffer.
5601       (gnus-configure-windows 'group 'force)
5602       ;; Clear the current group name.
5603       (setq gnus-newsgroup-name nil)
5604       (when (equal (gnus-group-group-name) group)
5605         (gnus-group-next-unread-group 1))
5606       (when quit-config
5607         (gnus-handle-ephemeral-exit quit-config)))))
5608
5609 (defun gnus-handle-ephemeral-exit (quit-config)
5610   "Handle movement when leaving an ephemeral group.
5611 The state which existed when entering the ephemeral is reset."
5612   (if (not (buffer-name (car quit-config)))
5613       (gnus-configure-windows 'group 'force)
5614     (set-buffer (car quit-config))
5615     (cond ((eq major-mode 'gnus-summary-mode)
5616            (gnus-set-global-variables))
5617           ((eq major-mode 'gnus-article-mode)
5618            (save-excursion
5619              ;; The `gnus-summary-buffer' variable may point
5620              ;; to the old summary buffer when using a single
5621              ;; article buffer.
5622              (unless (gnus-buffer-live-p gnus-summary-buffer)
5623                (set-buffer gnus-group-buffer))
5624              (set-buffer gnus-summary-buffer)
5625              (gnus-set-global-variables))))
5626     (if (or (eq (cdr quit-config) 'article)
5627             (eq (cdr quit-config) 'pick))
5628         (progn
5629           ;; The current article may be from the ephemeral group
5630           ;; thus it is best that we reload this article
5631           (gnus-summary-show-article)
5632           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5633               (gnus-configure-windows 'pick 'force)
5634             (gnus-configure-windows (cdr quit-config) 'force)))
5635       (gnus-configure-windows (cdr quit-config) 'force))
5636     (when (eq major-mode 'gnus-summary-mode)
5637       (gnus-summary-next-subject 1 nil t)
5638       (gnus-summary-recenter)
5639       (gnus-summary-position-point))))
5640
5641 (defun gnus-summary-preview-mime-message ()
5642   "MIME decode and play this message."
5643   (interactive)
5644   (let ((gnus-break-pages nil)
5645         (gnus-show-mime t))
5646     (gnus-summary-select-article gnus-show-all-headers t))
5647   (select-window (get-buffer-window gnus-article-buffer)))
5648
5649 ;;; Dead summaries.
5650
5651 (defvar gnus-dead-summary-mode-map nil)
5652
5653 (unless gnus-dead-summary-mode-map
5654   (setq gnus-dead-summary-mode-map (make-keymap))
5655   (suppress-keymap gnus-dead-summary-mode-map)
5656   (substitute-key-definition
5657    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5658   (let ((keys '("\C-d" "\r" "\177" [delete])))
5659     (while keys
5660       (define-key gnus-dead-summary-mode-map
5661         (pop keys) 'gnus-summary-wake-up-the-dead))))
5662
5663 (defvar gnus-dead-summary-mode nil
5664   "Minor mode for Gnus summary buffers.")
5665
5666 (defun gnus-dead-summary-mode (&optional arg)
5667   "Minor mode for Gnus summary buffers."
5668   (interactive "P")
5669   (when (eq major-mode 'gnus-summary-mode)
5670     (make-local-variable 'gnus-dead-summary-mode)
5671     (setq gnus-dead-summary-mode
5672           (if (null arg) (not gnus-dead-summary-mode)
5673             (> (prefix-numeric-value arg) 0)))
5674     (when gnus-dead-summary-mode
5675       (gnus-add-minor-mode
5676        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5677
5678 (defun gnus-deaden-summary ()
5679   "Make the current summary buffer into a dead summary buffer."
5680   ;; Kill any previous dead summary buffer.
5681   (when (and gnus-dead-summary
5682              (buffer-name gnus-dead-summary))
5683     (save-excursion
5684       (set-buffer gnus-dead-summary)
5685       (when gnus-dead-summary-mode
5686         (kill-buffer (current-buffer)))))
5687   ;; Make this the current dead summary.
5688   (setq gnus-dead-summary (current-buffer))
5689   (gnus-dead-summary-mode 1)
5690   (let ((name (buffer-name)))
5691     (when (string-match "Summary" name)
5692       (rename-buffer
5693        (concat (substring name 0 (match-beginning 0)) "Dead "
5694                (substring name (match-beginning 0)))
5695        t)
5696       (bury-buffer))))
5697
5698 (defun gnus-kill-or-deaden-summary (buffer)
5699   "Kill or deaden the summary BUFFER."
5700   (save-excursion
5701     (when (and (buffer-name buffer)
5702                (not gnus-single-article-buffer))
5703       (save-excursion
5704         (set-buffer buffer)
5705         (gnus-kill-buffer gnus-article-buffer)
5706         (gnus-kill-buffer gnus-original-article-buffer)))
5707     (cond (gnus-kill-summary-on-exit
5708            (when (and gnus-use-trees
5709                       (gnus-buffer-exists-p buffer))
5710              (save-excursion
5711                (set-buffer buffer)
5712                (gnus-tree-close gnus-newsgroup-name)))
5713            (gnus-kill-buffer buffer))
5714           ((gnus-buffer-exists-p buffer)
5715            (save-excursion
5716              (set-buffer buffer)
5717              (gnus-deaden-summary))))))
5718
5719 (defun gnus-summary-wake-up-the-dead (&rest args)
5720   "Wake up the dead summary buffer."
5721   (interactive)
5722   (gnus-dead-summary-mode -1)
5723   (let ((name (buffer-name)))
5724     (when (string-match "Dead " name)
5725       (rename-buffer
5726        (concat (substring name 0 (match-beginning 0))
5727                (substring name (match-end 0)))
5728        t)))
5729   (gnus-message 3 "This dead summary is now alive again"))
5730
5731 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5732 (defun gnus-summary-fetch-faq (&optional faq-dir)
5733   "Fetch the FAQ for the current group.
5734 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5735 in."
5736   (interactive
5737    (list
5738     (when current-prefix-arg
5739       (completing-read
5740        "Faq dir: " (and (listp gnus-group-faq-directory)
5741                         (mapcar (lambda (file) (list file))
5742                                 gnus-group-faq-directory))))))
5743   (let (gnus-faq-buffer)
5744     (when (setq gnus-faq-buffer
5745                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5746       (gnus-configure-windows 'summary-faq))))
5747
5748 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5749 (defun gnus-summary-describe-group (&optional force)
5750   "Describe the current newsgroup."
5751   (interactive "P")
5752   (gnus-group-describe-group force gnus-newsgroup-name))
5753
5754 (defun gnus-summary-describe-briefly ()
5755   "Describe summary mode commands briefly."
5756   (interactive)
5757   (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")))
5758
5759 ;; Walking around group mode buffer from summary mode.
5760
5761 (defun gnus-summary-next-group (&optional no-article target-group backward)
5762   "Exit current newsgroup and then select next unread newsgroup.
5763 If prefix argument NO-ARTICLE is non-nil, no article is selected
5764 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5765 previous group instead."
5766   (interactive "P")
5767   ;; Stop pre-fetching.
5768   (gnus-async-halt-prefetch)
5769   (let ((current-group gnus-newsgroup-name)
5770         (current-buffer (current-buffer))
5771         entered)
5772     ;; First we semi-exit this group to update Xrefs and all variables.
5773     ;; We can't do a real exit, because the window conf must remain
5774     ;; the same in case the user is prompted for info, and we don't
5775     ;; want the window conf to change before that...
5776     (gnus-summary-exit t)
5777     (while (not entered)
5778       ;; Then we find what group we are supposed to enter.
5779       (set-buffer gnus-group-buffer)
5780       (gnus-group-jump-to-group current-group)
5781       (setq target-group
5782             (or target-group
5783                 (if (eq gnus-keep-same-level 'best)
5784                     (gnus-summary-best-group gnus-newsgroup-name)
5785                   (gnus-summary-search-group backward gnus-keep-same-level))))
5786       (if (not target-group)
5787           ;; There are no further groups, so we return to the group
5788           ;; buffer.
5789           (progn
5790             (gnus-message 5 "Returning to the group buffer")
5791             (setq entered t)
5792             (when (gnus-buffer-live-p current-buffer)
5793               (set-buffer current-buffer)
5794               (gnus-summary-exit))
5795             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5796         ;; We try to enter the target group.
5797         (gnus-group-jump-to-group target-group)
5798         (let ((unreads (gnus-group-group-unread)))
5799           (if (and (or (eq t unreads)
5800                        (and unreads (not (zerop unreads))))
5801                    (gnus-summary-read-group
5802                     target-group nil no-article
5803                     (and (buffer-name current-buffer) current-buffer)
5804                     nil backward))
5805               (setq entered t)
5806             (setq current-group target-group
5807                   target-group nil)))))))
5808
5809 (defun gnus-summary-prev-group (&optional no-article)
5810   "Exit current newsgroup and then select previous unread newsgroup.
5811 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5812   (interactive "P")
5813   (gnus-summary-next-group no-article nil t))
5814
5815 ;; Walking around summary lines.
5816
5817 (defun gnus-summary-first-subject (&optional unread undownloaded)
5818   "Go to the first unread subject.
5819 If UNREAD is non-nil, go to the first unread article.
5820 Returns the article selected or nil if there are no unread articles."
5821   (interactive "P")
5822   (prog1
5823       (cond
5824        ;; Empty summary.
5825        ((null gnus-newsgroup-data)
5826         (gnus-message 3 "No articles in the group")
5827         nil)
5828        ;; Pick the first article.
5829        ((not unread)
5830         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5831         (gnus-data-number (car gnus-newsgroup-data)))
5832        ;; No unread articles.
5833        ((null gnus-newsgroup-unreads)
5834         (gnus-message 3 "No more unread articles")
5835         nil)
5836        ;; Find the first unread article.
5837        (t
5838         (let ((data gnus-newsgroup-data))
5839           (while (and data
5840                       (and (not (and undownloaded
5841                                      (eq gnus-undownloaded-mark
5842                                          (gnus-data-mark (car data)))))
5843                            (not (gnus-data-unread-p (car data)))))
5844             (setq data (cdr data)))
5845           (when data
5846             (goto-char (gnus-data-pos (car data)))
5847             (gnus-data-number (car data))))))
5848     (gnus-summary-position-point)))
5849
5850 (defun gnus-summary-next-subject (n &optional unread dont-display)
5851   "Go to next N'th summary line.
5852 If N is negative, go to the previous N'th subject line.
5853 If UNREAD is non-nil, only unread articles are selected.
5854 The difference between N and the actual number of steps taken is
5855 returned."
5856   (interactive "p")
5857   (let ((backward (< n 0))
5858         (n (abs n)))
5859     (while (and (> n 0)
5860                 (if backward
5861                     (gnus-summary-find-prev unread)
5862                   (gnus-summary-find-next unread)))
5863       (unless (zerop (setq n (1- n)))
5864         (gnus-summary-show-thread)))
5865     (when (/= 0 n)
5866       (gnus-message 7 "No more%s articles"
5867                     (if unread " unread" "")))
5868     (unless dont-display
5869       (gnus-summary-recenter)
5870       (gnus-summary-position-point))
5871     n))
5872
5873 (defun gnus-summary-next-unread-subject (n)
5874   "Go to next N'th unread summary line."
5875   (interactive "p")
5876   (gnus-summary-next-subject n t))
5877
5878 (defun gnus-summary-prev-subject (n &optional unread)
5879   "Go to previous N'th summary line.
5880 If optional argument UNREAD is non-nil, only unread article is selected."
5881   (interactive "p")
5882   (gnus-summary-next-subject (- n) unread))
5883
5884 (defun gnus-summary-prev-unread-subject (n)
5885   "Go to previous N'th unread summary line."
5886   (interactive "p")
5887   (gnus-summary-next-subject (- n) t))
5888
5889 (defun gnus-summary-goto-subject (article &optional force silent)
5890   "Go the subject line of ARTICLE.
5891 If FORCE, also allow jumping to articles not currently shown."
5892   (interactive "nArticle number: ")
5893   (let ((b (point))
5894         (data (gnus-data-find article)))
5895     ;; We read in the article if we have to.
5896     (and (not data)
5897          force
5898          (gnus-summary-insert-subject
5899           article
5900           (if (or (numberp force) (vectorp force)) force)
5901           t)
5902          (setq data (gnus-data-find article)))
5903     (goto-char b)
5904     (if (not data)
5905         (progn
5906           (unless silent
5907             (gnus-message 3 "Can't find article %d" article))
5908           nil)
5909       (goto-char (gnus-data-pos data))
5910       (gnus-summary-position-point)
5911       article)))
5912
5913 ;; Walking around summary lines with displaying articles.
5914
5915 (defun gnus-summary-expand-window (&optional arg)
5916   "Make the summary buffer take up the entire Emacs frame.
5917 Given a prefix, will force an `article' buffer configuration."
5918   (interactive "P")
5919   (if arg
5920       (gnus-configure-windows 'article 'force)
5921     (gnus-configure-windows 'summary 'force)))
5922
5923 (defun gnus-summary-display-article (article &optional all-header)
5924   "Display ARTICLE in article buffer."
5925   (gnus-set-global-variables)
5926   (when (gnus-buffer-live-p gnus-article-buffer)
5927     (with-current-buffer gnus-article-buffer
5928       (setq gnus-article-charset gnus-newsgroup-charset)
5929       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
5930   (if (null article)
5931       nil
5932     (prog1
5933         (if gnus-summary-display-article-function
5934             (funcall gnus-summary-display-article-function article all-header)
5935           (gnus-article-prepare article all-header))
5936       (with-current-buffer gnus-article-buffer
5937         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5938              nil))
5939       (gnus-run-hooks 'gnus-select-article-hook)
5940       (when (and gnus-current-article
5941                  (not (zerop gnus-current-article)))
5942         (gnus-summary-goto-subject gnus-current-article))
5943       (gnus-summary-recenter)
5944       (when (and gnus-use-trees gnus-show-threads)
5945         (gnus-possibly-generate-tree article)
5946         (gnus-highlight-selected-tree article))
5947       ;; Successfully display article.
5948       (gnus-article-set-window-start
5949        (cdr (assq article gnus-newsgroup-bookmarks))))))
5950
5951 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5952   "Select the current article.
5953 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5954 non-nil, the article will be re-fetched even if it already present in
5955 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5956 be displayed."
5957   ;; Make sure we are in the summary buffer to work around bbdb bug.
5958   (unless (eq major-mode 'gnus-summary-mode)
5959     (set-buffer gnus-summary-buffer))
5960   (let ((article (or article (gnus-summary-article-number)))
5961         (all-headers (not (not all-headers))) ;Must be T or NIL.
5962         gnus-summary-display-article-function)
5963     (and (not pseudo)
5964          (gnus-summary-article-pseudo-p article)
5965          (error "This is a pseudo-article"))
5966     (save-excursion
5967       (set-buffer gnus-summary-buffer)
5968       (if (or (and gnus-single-article-buffer
5969                    (or (null gnus-current-article)
5970                        (null gnus-article-current)
5971                        (null (get-buffer gnus-article-buffer))
5972                        (not (eq article (cdr gnus-article-current)))
5973                        (not (equal (car gnus-article-current)
5974                                    gnus-newsgroup-name))))
5975               (and (not gnus-single-article-buffer)
5976                    (or (null gnus-current-article)
5977                        (not (eq gnus-current-article article))))
5978               force)
5979           ;; The requested article is different from the current article.
5980           (progn
5981             (gnus-summary-display-article article all-headers)
5982             (when (or all-headers gnus-show-all-headers)
5983               (gnus-article-show-all-headers))
5984             (gnus-article-set-window-start
5985              (cdr (assq article gnus-newsgroup-bookmarks)))
5986             article)
5987         (when (or all-headers gnus-show-all-headers)
5988           (gnus-article-show-all-headers))
5989         'old))))
5990
5991 (defun gnus-summary-force-verify-and-decrypt ()
5992   (interactive)
5993   (let ((mm-verify-option 'known)
5994         (mm-decrypt-option 'known))
5995     (gnus-summary-select-article nil 'force)))
5996
5997 (defun gnus-summary-set-current-mark (&optional current-mark)
5998   "Obsolete function."
5999   nil)
6000
6001 (defun gnus-summary-next-article (&optional unread subject backward push)
6002   "Select the next article.
6003 If UNREAD, only unread articles are selected.
6004 If SUBJECT, only articles with SUBJECT are selected.
6005 If BACKWARD, the previous article is selected instead of the next."
6006   (interactive "P")
6007   (cond
6008    ;; Is there such an article?
6009    ((and (gnus-summary-search-forward unread subject backward)
6010          (or (gnus-summary-display-article (gnus-summary-article-number))
6011              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6012     (gnus-summary-position-point))
6013    ;; If not, we try the first unread, if that is wanted.
6014    ((and subject
6015          gnus-auto-select-same
6016          (gnus-summary-first-unread-article))
6017     (gnus-summary-position-point)
6018     (gnus-message 6 "Wrapped"))
6019    ;; Try to get next/previous article not displayed in this group.
6020    ((and gnus-auto-extend-newsgroup
6021          (not unread) (not subject))
6022     (gnus-summary-goto-article
6023      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6024      nil (count-lines (point-min) (point))))
6025    ;; Go to next/previous group.
6026    (t
6027     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6028       (gnus-summary-jump-to-group gnus-newsgroup-name))
6029     (let ((cmd last-command-char)
6030           (point
6031            (save-excursion
6032              (set-buffer gnus-group-buffer)
6033              (point)))
6034           (group
6035            (if (eq gnus-keep-same-level 'best)
6036                (gnus-summary-best-group gnus-newsgroup-name)
6037              (gnus-summary-search-group backward gnus-keep-same-level))))
6038       ;; For some reason, the group window gets selected.  We change
6039       ;; it back.
6040       (select-window (get-buffer-window (current-buffer)))
6041       ;; Select next unread newsgroup automagically.
6042       (cond
6043        ((or (not gnus-auto-select-next)
6044             (not cmd))
6045         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6046        ((or (eq gnus-auto-select-next 'quietly)
6047             (and (eq gnus-auto-select-next 'slightly-quietly)
6048                  push)
6049             (and (eq gnus-auto-select-next 'almost-quietly)
6050                  (gnus-summary-last-article-p)))
6051         ;; Select quietly.
6052         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6053             (gnus-summary-exit)
6054           (gnus-message 7 "No more%s articles (%s)..."
6055                         (if unread " unread" "")
6056                         (if group (concat "selecting " group)
6057                           "exiting"))
6058           (gnus-summary-next-group nil group backward)))
6059        (t
6060         (when (gnus-key-press-event-p last-input-event)
6061           (gnus-summary-walk-group-buffer
6062            gnus-newsgroup-name cmd unread backward point))))))))
6063
6064 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6065   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6066                       (?\C-p (gnus-group-prev-unread-group 1))))
6067         (cursor-in-echo-area t)
6068         keve key group ended)
6069     (save-excursion
6070       (set-buffer gnus-group-buffer)
6071       (goto-char start)
6072       (setq group
6073             (if (eq gnus-keep-same-level 'best)
6074                 (gnus-summary-best-group gnus-newsgroup-name)
6075               (gnus-summary-search-group backward gnus-keep-same-level))))
6076     (while (not ended)
6077       (gnus-message
6078        5 "No more%s articles%s" (if unread " unread" "")
6079        (if (and group
6080                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6081            (format " (Type %s for %s [%s])"
6082                    (single-key-description cmd) group
6083                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6084          (format " (Type %s to exit %s)"
6085                  (single-key-description cmd)
6086                  gnus-newsgroup-name)))
6087       ;; Confirm auto selection.
6088       (setq key (car (setq keve (gnus-read-event-char))))
6089       (setq ended t)
6090       (cond
6091        ((assq key keystrokes)
6092         (let ((obuf (current-buffer)))
6093           (switch-to-buffer gnus-group-buffer)
6094           (when group
6095             (gnus-group-jump-to-group group))
6096           (eval (cadr (assq key keystrokes)))
6097           (setq group (gnus-group-group-name))
6098           (switch-to-buffer obuf))
6099         (setq ended nil))
6100        ((equal key cmd)
6101         (if (or (not group)
6102                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6103             (gnus-summary-exit)
6104           (gnus-summary-next-group nil group backward)))
6105        (t
6106         (push (cdr keve) unread-command-events))))))
6107
6108 (defun gnus-summary-next-unread-article ()
6109   "Select unread article after current one."
6110   (interactive)
6111   (gnus-summary-next-article
6112    (or (not (eq gnus-summary-goto-unread 'never))
6113        (gnus-summary-last-article-p (gnus-summary-article-number)))
6114    (and gnus-auto-select-same
6115         (gnus-summary-article-subject))))
6116
6117 (defun gnus-summary-prev-article (&optional unread subject)
6118   "Select the article after the current one.
6119 If UNREAD is non-nil, only unread articles are selected."
6120   (interactive "P")
6121   (gnus-summary-next-article unread subject t))
6122
6123 (defun gnus-summary-prev-unread-article ()
6124   "Select unread article before current one."
6125   (interactive)
6126   (gnus-summary-prev-article
6127    (or (not (eq gnus-summary-goto-unread 'never))
6128        (gnus-summary-first-article-p (gnus-summary-article-number)))
6129    (and gnus-auto-select-same
6130         (gnus-summary-article-subject))))
6131
6132 (defun gnus-summary-next-page (&optional lines circular)
6133   "Show next page of the selected article.
6134 If at the end of the current article, select the next article.
6135 LINES says how many lines should be scrolled up.
6136
6137 If CIRCULAR is non-nil, go to the start of the article instead of
6138 selecting the next article when reaching the end of the current
6139 article."
6140   (interactive "P")
6141   (setq gnus-summary-buffer (current-buffer))
6142   (gnus-set-global-variables)
6143   (let ((article (gnus-summary-article-number))
6144         (article-window (get-buffer-window gnus-article-buffer t))
6145         endp)
6146     ;; If the buffer is empty, we have no article.
6147     (unless article
6148       (error "No article to select"))
6149     (gnus-configure-windows 'article)
6150     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6151         (if (and (eq gnus-summary-goto-unread 'never)
6152                  (not (gnus-summary-last-article-p article)))
6153             (gnus-summary-next-article)
6154           (gnus-summary-next-unread-article))
6155       (if (or (null gnus-current-article)
6156               (null gnus-article-current)
6157               (/= article (cdr gnus-article-current))
6158               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6159           ;; Selected subject is different from current article's.
6160           (gnus-summary-display-article article)
6161         (when article-window
6162           (gnus-eval-in-buffer-window gnus-article-buffer
6163             (setq endp (gnus-article-next-page lines)))
6164           (when endp
6165             (cond (circular
6166                    (gnus-summary-beginning-of-article))
6167                   (lines
6168                    (gnus-message 3 "End of message"))
6169                   ((null lines)
6170                    (if (and (eq gnus-summary-goto-unread 'never)
6171                             (not (gnus-summary-last-article-p article)))
6172                        (gnus-summary-next-article)
6173                      (gnus-summary-next-unread-article))))))))
6174     (gnus-summary-recenter)
6175     (gnus-summary-position-point)))
6176
6177 (defun gnus-summary-prev-page (&optional lines move)
6178   "Show previous page of selected article.
6179 Argument LINES specifies lines to be scrolled down.
6180 If MOVE, move to the previous unread article if point is at
6181 the beginning of the buffer."
6182   (interactive "P")
6183   (let ((article (gnus-summary-article-number))
6184         (article-window (get-buffer-window gnus-article-buffer t))
6185         endp)
6186     (gnus-configure-windows 'article)
6187     (if (or (null gnus-current-article)
6188             (null gnus-article-current)
6189             (/= article (cdr gnus-article-current))
6190             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6191         ;; Selected subject is different from current article's.
6192         (gnus-summary-display-article article)
6193       (gnus-summary-recenter)
6194       (when article-window
6195         (gnus-eval-in-buffer-window gnus-article-buffer
6196           (setq endp (gnus-article-prev-page lines)))
6197         (when (and move endp)
6198           (cond (lines
6199                  (gnus-message 3 "Beginning of message"))
6200                 ((null lines)
6201                  (if (and (eq gnus-summary-goto-unread 'never)
6202                           (not (gnus-summary-first-article-p article)))
6203                      (gnus-summary-prev-article)
6204                    (gnus-summary-prev-unread-article))))))))
6205   (gnus-summary-position-point))
6206
6207 (defun gnus-summary-prev-page-or-article (&optional lines)
6208   "Show previous page of selected article.
6209 Argument LINES specifies lines to be scrolled down.
6210 If at the beginning of the article, go to the next article."
6211   (interactive "P")
6212   (gnus-summary-prev-page lines t))
6213
6214 (defun gnus-summary-scroll-up (lines)
6215   "Scroll up (or down) one line current article.
6216 Argument LINES specifies lines to be scrolled up (or down if negative)."
6217   (interactive "p")
6218   (gnus-configure-windows 'article)
6219   (gnus-summary-show-thread)
6220   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6221     (gnus-eval-in-buffer-window gnus-article-buffer
6222       (cond ((> lines 0)
6223              (when (gnus-article-next-page lines)
6224                (gnus-message 3 "End of message")))
6225             ((< lines 0)
6226              (gnus-article-prev-page (- lines))))))
6227   (gnus-summary-recenter)
6228   (gnus-summary-position-point))
6229
6230 (defun gnus-summary-scroll-down (lines)
6231   "Scroll down (or up) one line current article.
6232 Argument LINES specifies lines to be scrolled down (or up if negative)."
6233   (interactive "p")
6234   (gnus-summary-scroll-up (- lines)))
6235
6236 (defun gnus-summary-next-same-subject ()
6237   "Select next article which has the same subject as current one."
6238   (interactive)
6239   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6240
6241 (defun gnus-summary-prev-same-subject ()
6242   "Select previous article which has the same subject as current one."
6243   (interactive)
6244   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6245
6246 (defun gnus-summary-next-unread-same-subject ()
6247   "Select next unread article which has the same subject as current one."
6248   (interactive)
6249   (gnus-summary-next-article t (gnus-summary-article-subject)))
6250
6251 (defun gnus-summary-prev-unread-same-subject ()
6252   "Select previous unread article which has the same subject as current one."
6253   (interactive)
6254   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6255
6256 (defun gnus-summary-first-unread-article ()
6257   "Select the first unread article.
6258 Return nil if there are no unread articles."
6259   (interactive)
6260   (prog1
6261       (when (gnus-summary-first-subject t)
6262         (gnus-summary-show-thread)
6263         (gnus-summary-first-subject t)
6264         (gnus-summary-display-article (gnus-summary-article-number)))
6265     (gnus-summary-position-point)))
6266
6267 (defun gnus-summary-first-unread-subject ()
6268   "Place the point on the subject line of the first unread article.
6269 Return nil if there are no unread articles."
6270   (interactive)
6271   (prog1
6272       (when (gnus-summary-first-subject t)
6273         (gnus-summary-show-thread)
6274         (gnus-summary-first-subject t))
6275     (gnus-summary-position-point)))
6276
6277 (defun gnus-summary-first-article ()
6278   "Select the first article.
6279 Return nil if there are no articles."
6280   (interactive)
6281   (prog1
6282       (when (gnus-summary-first-subject)
6283         (gnus-summary-show-thread)
6284         (gnus-summary-first-subject)
6285         (gnus-summary-display-article (gnus-summary-article-number)))
6286     (gnus-summary-position-point)))
6287
6288 (defun gnus-summary-best-unread-article ()
6289   "Select the unread article with the highest score."
6290   (interactive)
6291   (let ((best -1000000)
6292         (data gnus-newsgroup-data)
6293         article score)
6294     (while data
6295       (and (gnus-data-unread-p (car data))
6296            (> (setq score
6297                     (gnus-summary-article-score (gnus-data-number (car data))))
6298               best)
6299            (setq best score
6300                  article (gnus-data-number (car data))))
6301       (setq data (cdr data)))
6302     (prog1
6303         (if article
6304             (gnus-summary-goto-article article)
6305           (error "No unread articles"))
6306       (gnus-summary-position-point))))
6307
6308 (defun gnus-summary-last-subject ()
6309   "Go to the last displayed subject line in the group."
6310   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6311     (when article
6312       (gnus-summary-goto-subject article))))
6313
6314 (defun gnus-summary-goto-article (article &optional all-headers force)
6315   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6316 If ALL-HEADERS is non-nil, no header lines are hidden.
6317 If FORCE, go to the article even if it isn't displayed.  If FORCE
6318 is a number, it is the line the article is to be displayed on."
6319   (interactive
6320    (list
6321     (completing-read
6322      "Article number or Message-ID: "
6323      (mapcar (lambda (number) (list (int-to-string number)))
6324              gnus-newsgroup-limit))
6325     current-prefix-arg
6326     t))
6327   (prog1
6328       (if (and (stringp article)
6329                (string-match "@" article))
6330           (gnus-summary-refer-article article)
6331         (when (stringp article)
6332           (setq article (string-to-number article)))
6333         (if (gnus-summary-goto-subject article force)
6334             (gnus-summary-display-article article all-headers)
6335           (gnus-message 4 "Couldn't go to article %s" article) nil))
6336     (gnus-summary-position-point)))
6337
6338 (defun gnus-summary-goto-last-article ()
6339   "Go to the previously read article."
6340   (interactive)
6341   (prog1
6342       (when gnus-last-article
6343         (gnus-summary-goto-article gnus-last-article nil t))
6344     (gnus-summary-position-point)))
6345
6346 (defun gnus-summary-pop-article (number)
6347   "Pop one article off the history and go to the previous.
6348 NUMBER articles will be popped off."
6349   (interactive "p")
6350   (let (to)
6351     (setq gnus-newsgroup-history
6352           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6353     (if to
6354         (gnus-summary-goto-article (car to) nil t)
6355       (error "Article history empty")))
6356   (gnus-summary-position-point))
6357
6358 ;; Summary commands and functions for limiting the summary buffer.
6359
6360 (defun gnus-summary-limit-to-articles (n)
6361   "Limit the summary buffer to the next N articles.
6362 If not given a prefix, use the process marked articles instead."
6363   (interactive "P")
6364   (prog1
6365       (let ((articles (gnus-summary-work-articles n)))
6366         (setq gnus-newsgroup-processable nil)
6367         (gnus-summary-limit articles))
6368     (gnus-summary-position-point)))
6369
6370 (defun gnus-summary-pop-limit (&optional total)
6371   "Restore the previous limit.
6372 If given a prefix, remove all limits."
6373   (interactive "P")
6374   (when total
6375     (setq gnus-newsgroup-limits
6376           (list (mapcar (lambda (h) (mail-header-number h))
6377                         gnus-newsgroup-headers))))
6378   (unless gnus-newsgroup-limits
6379     (error "No limit to pop"))
6380   (prog1
6381       (gnus-summary-limit nil 'pop)
6382     (gnus-summary-position-point)))
6383
6384 (defun gnus-summary-limit-to-subject (subject &optional header)
6385   "Limit the summary buffer to articles that have subjects that match a regexp."
6386   (interactive "sLimit to subject (regexp): ")
6387   (unless header
6388     (setq header "subject"))
6389   (when (not (equal "" subject))
6390     (prog1
6391         (let ((articles (gnus-summary-find-matching
6392                          (or header "subject") subject 'all)))
6393           (unless articles
6394             (error "Found no matches for \"%s\"" subject))
6395           (gnus-summary-limit articles))
6396       (gnus-summary-position-point))))
6397
6398 (defun gnus-summary-limit-to-author (from)
6399   "Limit the summary buffer to articles that have authors that match a regexp."
6400   (interactive "sLimit to author (regexp): ")
6401   (gnus-summary-limit-to-subject from "from"))
6402
6403 (defun gnus-summary-limit-to-age (age &optional younger-p)
6404   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6405 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6406 articles that are younger than AGE days."
6407   (interactive
6408    (let ((younger current-prefix-arg)
6409          (days-got nil)
6410          days)
6411      (while (not days-got)
6412        (setq days (if younger
6413                       (read-string "Limit to articles within (in days): ")
6414                     (read-string "Limit to articles old than (in days): ")))
6415        (when (> (length days) 0)
6416          (setq days (read days)))
6417        (if (numberp days)
6418            (setq days-got t)
6419          (message "Please enter a number.")
6420          (sleep-for 1)))
6421      (list days younger)))
6422   (prog1
6423       (let ((data gnus-newsgroup-data)
6424             (cutoff (days-to-time age))
6425             articles d date is-younger)
6426         (while (setq d (pop data))
6427           (when (and (vectorp (gnus-data-header d))
6428                      (setq date (mail-header-date (gnus-data-header d))))
6429             (setq is-younger (time-less-p
6430                               (time-since (condition-case ()
6431                                               (date-to-time date)
6432                                             (error '(0 0))))
6433                               cutoff))
6434             (when (if younger-p
6435                       is-younger
6436                     (not is-younger))
6437               (push (gnus-data-number d) articles))))
6438         (gnus-summary-limit (nreverse articles)))
6439     (gnus-summary-position-point)))
6440
6441 (defun gnus-summary-limit-to-extra (header regexp)
6442   "Limit the summary buffer to articles that match an 'extra' header."
6443   (interactive
6444    (let ((header
6445           (intern
6446            (gnus-completing-read
6447             (symbol-name (car gnus-extra-headers))
6448             "Limit extra header:"
6449             (mapcar (lambda (x)
6450                       (cons (symbol-name x) x))
6451                     gnus-extra-headers)
6452             nil
6453             t))))
6454      (list header
6455            (read-string (format "Limit to header %s (regexp): " header)))))
6456   (when (not (equal "" regexp))
6457     (prog1
6458         (let ((articles (gnus-summary-find-matching
6459                          (cons 'extra header) regexp 'all)))
6460           (unless articles
6461             (error "Found no matches for \"%s\"" regexp))
6462           (gnus-summary-limit articles))
6463       (gnus-summary-position-point))))
6464
6465 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6466 (make-obsolete
6467  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6468
6469 (defun gnus-summary-limit-to-unread (&optional all)
6470   "Limit the summary buffer to articles that are not marked as read.
6471 If ALL is non-nil, limit strictly to unread articles."
6472   (interactive "P")
6473   (if all
6474       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6475     (gnus-summary-limit-to-marks
6476      ;; Concat all the marks that say that an article is read and have
6477      ;; those removed.
6478      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6479            gnus-killed-mark gnus-kill-file-mark
6480            gnus-low-score-mark gnus-expirable-mark
6481            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6482            gnus-duplicate-mark gnus-souped-mark)
6483      'reverse)))
6484
6485 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6486 (make-obsolete 'gnus-summary-delete-marked-with
6487                'gnus-summary-limit-exlude-marks)
6488
6489 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6490   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6491 If REVERSE, limit the summary buffer to articles that are marked
6492 with MARKS.  MARKS can either be a string of marks or a list of marks.
6493 Returns how many articles were removed."
6494   (interactive "sMarks: ")
6495   (gnus-summary-limit-to-marks marks t))
6496
6497 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6498   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6499 If REVERSE (the prefix), limit the summary buffer to articles that are
6500 not marked with MARKS.  MARKS can either be a string of marks or a
6501 list of marks.
6502 Returns how many articles were removed."
6503   (interactive "sMarks: \nP")
6504   (prog1
6505       (let ((data gnus-newsgroup-data)
6506             (marks (if (listp marks) marks
6507                      (append marks nil))) ; Transform to list.
6508             articles)
6509         (while data
6510           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6511                   (memq (gnus-data-mark (car data)) marks))
6512             (push (gnus-data-number (car data)) articles))
6513           (setq data (cdr data)))
6514         (gnus-summary-limit articles))
6515     (gnus-summary-position-point)))
6516
6517 (defun gnus-summary-limit-to-score (&optional score)
6518   "Limit to articles with score at or above SCORE."
6519   (interactive "P")
6520   (setq score (if score
6521                   (prefix-numeric-value score)
6522                 (or gnus-summary-default-score 0)))
6523   (let ((data gnus-newsgroup-data)
6524         articles)
6525     (while data
6526       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6527                 score)
6528         (push (gnus-data-number (car data)) articles))
6529       (setq data (cdr data)))
6530     (prog1
6531         (gnus-summary-limit articles)
6532       (gnus-summary-position-point))))
6533
6534 (defun gnus-summary-limit-include-thread (id)
6535   "Display all the hidden articles that in the current thread."
6536   (interactive (list (mail-header-id (gnus-summary-article-header))))
6537   (let ((articles (gnus-articles-in-thread
6538                    (gnus-id-to-thread (gnus-root-id id)))))
6539     (prog1
6540         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6541       (gnus-summary-position-point))))
6542
6543 (defun gnus-summary-limit-include-dormant ()
6544   "Display all the hidden articles that are marked as dormant.
6545 Note that this command only works on a subset of the articles currently
6546 fetched for this group."
6547   (interactive)
6548   (unless gnus-newsgroup-dormant
6549     (error "There are no dormant articles in this group"))
6550   (prog1
6551       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6552     (gnus-summary-position-point)))
6553
6554 (defun gnus-summary-limit-exclude-dormant ()
6555   "Hide all dormant articles."
6556   (interactive)
6557   (prog1
6558       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6559     (gnus-summary-position-point)))
6560
6561 (defun gnus-summary-limit-exclude-childless-dormant ()
6562   "Hide all dormant articles that have no children."
6563   (interactive)
6564   (let ((data (gnus-data-list t))
6565         articles d children)
6566     ;; Find all articles that are either not dormant or have
6567     ;; children.
6568     (while (setq d (pop data))
6569       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6570                 (and (setq children
6571                            (gnus-article-children (gnus-data-number d)))
6572                      (let (found)
6573                        (while children
6574                          (when (memq (car children) articles)
6575                            (setq children nil
6576                                  found t))
6577                          (pop children))
6578                        found)))
6579         (push (gnus-data-number d) articles)))
6580     ;; Do the limiting.
6581     (prog1
6582         (gnus-summary-limit articles)
6583       (gnus-summary-position-point))))
6584
6585 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6586   "Mark all unread excluded articles as read.
6587 If ALL, mark even excluded ticked and dormants as read."
6588   (interactive "P")
6589   (let ((articles (gnus-sorted-complement
6590                    (sort
6591                     (mapcar (lambda (h) (mail-header-number h))
6592                             gnus-newsgroup-headers)
6593                     '<)
6594                    (sort gnus-newsgroup-limit '<)))
6595         article)
6596     (setq gnus-newsgroup-unreads
6597           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6598     (if all
6599         (setq gnus-newsgroup-dormant nil
6600               gnus-newsgroup-marked nil
6601               gnus-newsgroup-reads
6602               (nconc
6603                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6604                gnus-newsgroup-reads))
6605       (while (setq article (pop articles))
6606         (unless (or (memq article gnus-newsgroup-dormant)
6607                     (memq article gnus-newsgroup-marked))
6608           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6609
6610 (defun gnus-summary-limit (articles &optional pop)
6611   (if pop
6612       ;; We pop the previous limit off the stack and use that.
6613       (setq articles (car gnus-newsgroup-limits)
6614             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6615     ;; We use the new limit, so we push the old limit on the stack.
6616     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6617   ;; Set the limit.
6618   (setq gnus-newsgroup-limit articles)
6619   (let ((total (length gnus-newsgroup-data))
6620         (data (gnus-data-find-list (gnus-summary-article-number)))
6621         (gnus-summary-mark-below nil)   ; Inhibit this.
6622         found)
6623     ;; This will do all the work of generating the new summary buffer
6624     ;; according to the new limit.
6625     (gnus-summary-prepare)
6626     ;; Hide any threads, possibly.
6627     (and gnus-show-threads
6628          gnus-thread-hide-subtree
6629          (gnus-summary-hide-all-threads))
6630     ;; Try to return to the article you were at, or one in the
6631     ;; neighborhood.
6632     (when data
6633       ;; We try to find some article after the current one.
6634       (while data
6635         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6636           (setq data nil
6637                 found t))
6638         (setq data (cdr data))))
6639     (unless found
6640       ;; If there is no data, that means that we were after the last
6641       ;; article.  The same goes when we can't find any articles
6642       ;; after the current one.
6643       (goto-char (point-max))
6644       (gnus-summary-find-prev))
6645     (gnus-set-mode-line 'summary)
6646     ;; We return how many articles were removed from the summary
6647     ;; buffer as a result of the new limit.
6648     (- total (length gnus-newsgroup-data))))
6649
6650 (defsubst gnus-invisible-cut-children (threads)
6651   (let ((num 0))
6652     (while threads
6653       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6654         (incf num))
6655       (pop threads))
6656     (< num 2)))
6657
6658 (defsubst gnus-cut-thread (thread)
6659   "Go forwards in the thread until we find an article that we want to display."
6660   (when (or (eq gnus-fetch-old-headers 'some)
6661             (eq gnus-fetch-old-headers 'invisible)
6662             (numberp gnus-fetch-old-headers)
6663             (eq gnus-build-sparse-threads 'some)
6664             (eq gnus-build-sparse-threads 'more))
6665     ;; Deal with old-fetched headers and sparse threads.
6666     (while (and
6667             thread
6668             (or
6669              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6670              (gnus-summary-article-ancient-p
6671               (mail-header-number (car thread))))
6672             (if (or (<= (length (cdr thread)) 1)
6673                     (eq gnus-fetch-old-headers 'invisible))
6674                 (setq gnus-newsgroup-limit
6675                       (delq (mail-header-number (car thread))
6676                             gnus-newsgroup-limit)
6677                       thread (cadr thread))
6678               (when (gnus-invisible-cut-children (cdr thread))
6679                 (let ((th (cdr thread)))
6680                   (while th
6681                     (if (memq (mail-header-number (caar th))
6682                               gnus-newsgroup-limit)
6683                         (setq thread (car th)
6684                               th nil)
6685                       (setq th (cdr th))))))))))
6686   thread)
6687
6688 (defun gnus-cut-threads (threads)
6689   "Cut off all uninteresting articles from the beginning of threads."
6690   (when (or (eq gnus-fetch-old-headers 'some)
6691             (eq gnus-fetch-old-headers 'invisible)
6692             (numberp gnus-fetch-old-headers)
6693             (eq gnus-build-sparse-threads 'some)
6694             (eq gnus-build-sparse-threads 'more))
6695     (let ((th threads))
6696       (while th
6697         (setcar th (gnus-cut-thread (car th)))
6698         (setq th (cdr th)))))
6699   ;; Remove nixed out threads.
6700   (delq nil threads))
6701
6702 (defun gnus-summary-initial-limit (&optional show-if-empty)
6703   "Figure out what the initial limit is supposed to be on group entry.
6704 This entails weeding out unwanted dormants, low-scored articles,
6705 fetch-old-headers verbiage, and so on."
6706   ;; Most groups have nothing to remove.
6707   (if (or gnus-inhibit-limiting
6708           (and (null gnus-newsgroup-dormant)
6709                (not (eq gnus-fetch-old-headers 'some))
6710                (not (numberp gnus-fetch-old-headers))
6711                (not (eq gnus-fetch-old-headers 'invisible))
6712                (null gnus-summary-expunge-below)
6713                (not (eq gnus-build-sparse-threads 'some))
6714                (not (eq gnus-build-sparse-threads 'more))
6715                (null gnus-thread-expunge-below)
6716                (not gnus-use-nocem)))
6717       ()                                ; Do nothing.
6718     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6719     (setq gnus-newsgroup-limit nil)
6720     (mapatoms
6721      (lambda (node)
6722        (unless (car (symbol-value node))
6723          ;; These threads have no parents -- they are roots.
6724          (let ((nodes (cdr (symbol-value node)))
6725                thread)
6726            (while nodes
6727              (if (and gnus-thread-expunge-below
6728                       (< (gnus-thread-total-score (car nodes))
6729                          gnus-thread-expunge-below))
6730                  (gnus-expunge-thread (pop nodes))
6731                (setq thread (pop nodes))
6732                (gnus-summary-limit-children thread))))))
6733      gnus-newsgroup-dependencies)
6734     ;; If this limitation resulted in an empty group, we might
6735     ;; pop the previous limit and use it instead.
6736     (when (and (not gnus-newsgroup-limit)
6737                show-if-empty)
6738       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6739     gnus-newsgroup-limit))
6740
6741 (defun gnus-summary-limit-children (thread)
6742   "Return 1 if this subthread is visible and 0 if it is not."
6743   ;; First we get the number of visible children to this thread.  This
6744   ;; is done by recursing down the thread using this function, so this
6745   ;; will really go down to a leaf article first, before slowly
6746   ;; working its way up towards the root.
6747   (when thread
6748     (let ((children
6749            (if (cdr thread)
6750                (apply '+ (mapcar 'gnus-summary-limit-children
6751                                  (cdr thread)))
6752              0))
6753           (number (mail-header-number (car thread)))
6754           score)
6755       (if (and
6756            (not (memq number gnus-newsgroup-marked))
6757            (or
6758             ;; If this article is dormant and has absolutely no visible
6759             ;; children, then this article isn't visible.
6760             (and (memq number gnus-newsgroup-dormant)
6761                  (zerop children))
6762             ;; If this is "fetch-old-headered" and there is no
6763             ;; visible children, then we don't want this article.
6764             (and (or (eq gnus-fetch-old-headers 'some)
6765                      (numberp gnus-fetch-old-headers))
6766                  (gnus-summary-article-ancient-p number)
6767                  (zerop children))
6768             ;; If this is "fetch-old-headered" and `invisible', then
6769             ;; we don't want this article.
6770             (and (eq gnus-fetch-old-headers 'invisible)
6771                  (gnus-summary-article-ancient-p number))
6772             ;; If this is a sparsely inserted article with no children,
6773             ;; we don't want it.
6774             (and (eq gnus-build-sparse-threads 'some)
6775                  (gnus-summary-article-sparse-p number)
6776                  (zerop children))
6777             ;; If we use expunging, and this article is really
6778             ;; low-scored, then we don't want this article.
6779             (when (and gnus-summary-expunge-below
6780                        (< (setq score
6781                                 (or (cdr (assq number gnus-newsgroup-scored))
6782                                     gnus-summary-default-score))
6783                           gnus-summary-expunge-below))
6784               ;; We increase the expunge-tally here, but that has
6785               ;; nothing to do with the limits, really.
6786               (incf gnus-newsgroup-expunged-tally)
6787               ;; We also mark as read here, if that's wanted.
6788               (when (and gnus-summary-mark-below
6789                          (< score gnus-summary-mark-below))
6790                 (setq gnus-newsgroup-unreads
6791                       (delq number gnus-newsgroup-unreads))
6792                 (if gnus-newsgroup-auto-expire
6793                     (push number gnus-newsgroup-expirable)
6794                   (push (cons number gnus-low-score-mark)
6795                         gnus-newsgroup-reads)))
6796               t)
6797             ;; Check NoCeM things.
6798             (if (and gnus-use-nocem
6799                      (gnus-nocem-unwanted-article-p
6800                       (mail-header-id (car thread))))
6801                 (progn
6802                   (setq gnus-newsgroup-unreads
6803                         (delq number gnus-newsgroup-unreads))
6804                   t))))
6805           ;; Nope, invisible article.
6806           0
6807         ;; Ok, this article is to be visible, so we add it to the limit
6808         ;; and return 1.
6809         (push number gnus-newsgroup-limit)
6810         1))))
6811
6812 (defun gnus-expunge-thread (thread)
6813   "Mark all articles in THREAD as read."
6814   (let* ((number (mail-header-number (car thread))))
6815     (incf gnus-newsgroup-expunged-tally)
6816     ;; We also mark as read here, if that's wanted.
6817     (setq gnus-newsgroup-unreads
6818           (delq number gnus-newsgroup-unreads))
6819     (if gnus-newsgroup-auto-expire
6820         (push number gnus-newsgroup-expirable)
6821       (push (cons number gnus-low-score-mark)
6822             gnus-newsgroup-reads)))
6823   ;; Go recursively through all subthreads.
6824   (mapcar 'gnus-expunge-thread (cdr thread)))
6825
6826 ;; Summary article oriented commands
6827
6828 (defun gnus-summary-refer-parent-article (n)
6829   "Refer parent article N times.
6830 If N is negative, go to ancestor -N instead.
6831 The difference between N and the number of articles fetched is returned."
6832   (interactive "p")
6833   (let ((skip 1)
6834         error header ref)
6835     (when (not (natnump n))
6836       (setq skip (abs n)
6837             n 1))
6838     (while (and (> n 0)
6839                 (not error))
6840       (setq header (gnus-summary-article-header))
6841       (if (and (eq (mail-header-number header)
6842                    (cdr gnus-article-current))
6843                (equal gnus-newsgroup-name
6844                       (car gnus-article-current)))
6845           ;; If we try to find the parent of the currently
6846           ;; displayed article, then we take a look at the actual
6847           ;; References header, since this is slightly more
6848           ;; reliable than the References field we got from the
6849           ;; server.
6850           (save-excursion
6851             (set-buffer gnus-original-article-buffer)
6852             (nnheader-narrow-to-headers)
6853             (unless (setq ref (message-fetch-field "references"))
6854               (setq ref (message-fetch-field "in-reply-to")))
6855             (widen))
6856         (setq ref
6857               ;; It's not the current article, so we take a bet on
6858               ;; the value we got from the server.
6859               (mail-header-references header)))
6860       (if (and ref
6861                (not (equal ref "")))
6862           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6863             (gnus-message 1 "Couldn't find parent"))
6864         (gnus-message 1 "No references in article %d"
6865                       (gnus-summary-article-number))
6866         (setq error t))
6867       (decf n))
6868     (gnus-summary-position-point)
6869     n))
6870
6871 (defun gnus-summary-refer-references ()
6872   "Fetch all articles mentioned in the References header.
6873 Return the number of articles fetched."
6874   (interactive)
6875   (let ((ref (mail-header-references (gnus-summary-article-header)))
6876         (current (gnus-summary-article-number))
6877         (n 0))
6878     (if (or (not ref)
6879             (equal ref ""))
6880         (error "No References in the current article")
6881       ;; For each Message-ID in the References header...
6882       (while (string-match "<[^>]*>" ref)
6883         (incf n)
6884         ;; ... fetch that article.
6885         (gnus-summary-refer-article
6886          (prog1 (match-string 0 ref)
6887            (setq ref (substring ref (match-end 0))))))
6888       (gnus-summary-goto-subject current)
6889       (gnus-summary-position-point)
6890       n)))
6891
6892 (defun gnus-summary-refer-thread (&optional limit)
6893   "Fetch all articles in the current thread.
6894 If LIMIT (the numerical prefix), fetch that many old headers instead
6895 of what's specified by the `gnus-refer-thread-limit' variable."
6896   (interactive "P")
6897   (let ((id (mail-header-id (gnus-summary-article-header)))
6898         (limit (if limit (prefix-numeric-value limit)
6899                  gnus-refer-thread-limit)))
6900     ;; We want to fetch LIMIT *old* headers, but we also have to
6901     ;; re-fetch all the headers in the current buffer, because many of
6902     ;; them may be undisplayed.  So we adjust LIMIT.
6903     (when (numberp limit)
6904       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6905     (unless (eq gnus-fetch-old-headers 'invisible)
6906       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6907       ;; Retrieve the headers and read them in.
6908       (if (eq (gnus-retrieve-headers
6909                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6910               'nov)
6911           (gnus-build-all-threads)
6912         (error "Can't fetch thread from backends that don't support NOV"))
6913       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6914     (gnus-summary-limit-include-thread id)))
6915
6916 (defun gnus-summary-refer-article (message-id)
6917   "Fetch an article specified by MESSAGE-ID."
6918   (interactive "sMessage-ID: ")
6919   (when (and (stringp message-id)
6920              (not (zerop (length message-id))))
6921     ;; Construct the correct Message-ID if necessary.
6922     ;; Suggested by tale@pawl.rpi.edu.
6923     (unless (string-match "^<" message-id)
6924       (setq message-id (concat "<" message-id)))
6925     (unless (string-match ">$" message-id)
6926       (setq message-id (concat message-id ">")))
6927     (let* ((header (gnus-id-to-header message-id))
6928            (sparse (and header
6929                         (gnus-summary-article-sparse-p
6930                          (mail-header-number header))
6931                         (memq (mail-header-number header)
6932                               gnus-newsgroup-limit)))
6933            number)
6934       (cond
6935        ;; If the article is present in the buffer we just go to it.
6936        ((and header
6937              (or (not (gnus-summary-article-sparse-p
6938                        (mail-header-number header)))
6939                  sparse))
6940         (prog1
6941             (gnus-summary-goto-article
6942              (mail-header-number header) nil t)
6943           (when sparse
6944             (gnus-summary-update-article (mail-header-number header)))))
6945        (t
6946         ;; We fetch the article.
6947         (catch 'found
6948           (dolist (gnus-override-method (gnus-refer-article-methods))
6949             (gnus-check-server gnus-override-method)
6950             ;; Fetch the header, and display the article.
6951             (when (setq number (gnus-summary-insert-subject message-id))
6952               (gnus-summary-select-article nil nil nil number)
6953               (throw 'found t)))
6954           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6955
6956 (defun gnus-refer-article-methods ()
6957   "Return a list of referrable methods."
6958   (cond
6959    ;; No method, so we default to current and native.
6960    ((null gnus-refer-article-method)
6961     (list gnus-current-select-method gnus-select-method))
6962    ;; Current.
6963    ((eq 'current gnus-refer-article-method)
6964     (list gnus-current-select-method))
6965    ;; List of select methods.
6966    ((not (and (symbolp (car gnus-refer-article-method))
6967               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6968     (let (out)
6969       (dolist (method gnus-refer-article-method)
6970         (push (if (eq 'current method)
6971                   gnus-current-select-method
6972                 method)
6973               out))
6974       (nreverse out)))
6975    ;; One single select method.
6976    (t
6977     (list gnus-refer-article-method))))
6978
6979 (defun gnus-summary-edit-parameters ()
6980   "Edit the group parameters of the current group."
6981   (interactive)
6982   (gnus-group-edit-group gnus-newsgroup-name 'params))
6983
6984 (defun gnus-summary-customize-parameters ()
6985   "Customize the group parameters of the current group."
6986   (interactive)
6987   (gnus-group-customize gnus-newsgroup-name))
6988
6989 (defun gnus-summary-enter-digest-group (&optional force)
6990   "Enter an nndoc group based on the current article.
6991 If FORCE, force a digest interpretation.  If not, try
6992 to guess what the document format is."
6993   (interactive "P")
6994   (let ((conf gnus-current-window-configuration))
6995     (save-excursion
6996       (gnus-summary-select-article))
6997     (setq gnus-current-window-configuration conf)
6998     (let* ((name (format "%s-%d"
6999                          (gnus-group-prefixed-name
7000                           gnus-newsgroup-name (list 'nndoc ""))
7001                          (save-excursion
7002                            (set-buffer gnus-summary-buffer)
7003                            gnus-current-article)))
7004            (ogroup gnus-newsgroup-name)
7005            (params (append (gnus-info-params (gnus-get-info ogroup))
7006                            (list (cons 'to-group ogroup))
7007                            (list (cons 'save-article-group ogroup))))
7008            (case-fold-search t)
7009            (buf (current-buffer))
7010            dig to-address)
7011       (save-excursion
7012         (set-buffer gnus-original-article-buffer)
7013         ;; Have the digest group inherit the main mail address of
7014         ;; the parent article.
7015         (when (setq to-address (or (message-fetch-field "reply-to")
7016                                    (message-fetch-field "from")))
7017           (setq params (append 
7018                         (list (cons 'to-address 
7019                                     (funcall gnus-decode-encoded-word-function
7020                                              to-address))))))
7021         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7022         (insert-buffer-substring gnus-original-article-buffer)
7023         ;; Remove lines that may lead nndoc to misinterpret the
7024         ;; document type.
7025         (narrow-to-region
7026          (goto-char (point-min))
7027          (or (search-forward "\n\n" nil t) (point)))
7028         (goto-char (point-min))
7029         (delete-matching-lines "^Path:\\|^From ")
7030         (widen))
7031       (unwind-protect
7032           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7033                     (gnus-newsgroup-ephemeral-ignored-charsets
7034                      gnus-newsgroup-ignored-charsets))
7035                 (gnus-group-read-ephemeral-group
7036                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7037                               (nndoc-article-type
7038                                ,(if force 'mbox 'guess))) t))
7039               ;; Make all postings to this group go to the parent group.
7040               (nconc (gnus-info-params (gnus-get-info name))
7041                      params)
7042             ;; Couldn't select this doc group.
7043             (switch-to-buffer buf)
7044             (gnus-set-global-variables)
7045             (gnus-configure-windows 'summary)
7046             (gnus-message 3 "Article couldn't be entered?"))
7047         (kill-buffer dig)))))
7048
7049 (defun gnus-summary-read-document (n)
7050   "Open a new group based on the current article(s).
7051 This will allow you to read digests and other similar
7052 documents as newsgroups.
7053 Obeys the standard process/prefix convention."
7054   (interactive "P")
7055   (let* ((articles (gnus-summary-work-articles n))
7056          (ogroup gnus-newsgroup-name)
7057          (params (append (gnus-info-params (gnus-get-info ogroup))
7058                          (list (cons 'to-group ogroup))))
7059          article group egroup groups vgroup)
7060     (while (setq article (pop articles))
7061       (setq group (format "%s-%d" gnus-newsgroup-name article))
7062       (gnus-summary-remove-process-mark article)
7063       (when (gnus-summary-display-article article)
7064         (save-excursion
7065           (with-temp-buffer
7066             (insert-buffer-substring gnus-original-article-buffer)
7067             ;; Remove some headers that may lead nndoc to make
7068             ;; the wrong guess.
7069             (message-narrow-to-head)
7070             (goto-char (point-min))
7071             (delete-matching-lines "^\\(Path\\):\\|^From ")
7072             (widen)
7073             (if (setq egroup
7074                       (gnus-group-read-ephemeral-group
7075                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7076                                      (nndoc-article-type guess))
7077                        t nil t))
7078                 (progn
7079                   ;; Make all postings to this group go to the parent group.
7080                   (nconc (gnus-info-params (gnus-get-info egroup))
7081                          params)
7082                   (push egroup groups))
7083               ;; Couldn't select this doc group.
7084               (gnus-error 3 "Article couldn't be entered"))))))
7085     ;; Now we have selected all the documents.
7086     (cond
7087      ((not groups)
7088       (error "None of the articles could be interpreted as documents"))
7089      ((gnus-group-read-ephemeral-group
7090        (setq vgroup (format
7091                      "nnvirtual:%s-%s" gnus-newsgroup-name
7092                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7093        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7094        t
7095        (cons (current-buffer) 'summary)))
7096      (t
7097       (error "Couldn't select virtual nndoc group")))))
7098
7099 (defun gnus-summary-isearch-article (&optional regexp-p)
7100   "Do incremental search forward on the current article.
7101 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7102   (interactive "P")
7103   (let* ((gnus-inhibit-treatment t)
7104          (old (gnus-summary-select-article)))
7105     (gnus-configure-windows 'article)
7106     (gnus-eval-in-buffer-window gnus-article-buffer
7107       (save-restriction
7108         (widen)
7109         (when (eq 'old old)
7110           (gnus-article-show-all-headers))
7111         (goto-char (point-min))
7112         (isearch-forward regexp-p)))))
7113
7114 (defun gnus-summary-search-article-forward (regexp &optional backward)
7115   "Search for an article containing REGEXP forward.
7116 If BACKWARD, search backward instead."
7117   (interactive
7118    (list (read-string
7119           (format "Search article %s (regexp%s): "
7120                   (if current-prefix-arg "backward" "forward")
7121                   (if gnus-last-search-regexp
7122                       (concat ", default " gnus-last-search-regexp)
7123                     "")))
7124          current-prefix-arg))
7125   (if (string-equal regexp "")
7126       (setq regexp (or gnus-last-search-regexp ""))
7127     (setq gnus-last-search-regexp regexp)
7128     (setq gnus-article-before-search gnus-current-article))
7129   ;; Intentionally set gnus-last-article.
7130   (setq gnus-last-article gnus-article-before-search)
7131   (let ((gnus-last-article gnus-last-article))
7132     (if (gnus-summary-search-article regexp backward)
7133         (gnus-summary-show-thread)
7134       (error "Search failed: \"%s\"" regexp))))
7135
7136 (defun gnus-summary-search-article-backward (regexp)
7137   "Search for an article containing REGEXP backward."
7138   (interactive
7139    (list (read-string
7140           (format "Search article backward (regexp%s): "
7141                   (if gnus-last-search-regexp
7142                       (concat ", default " gnus-last-search-regexp)
7143                     "")))))
7144   (gnus-summary-search-article-forward regexp 'backward))
7145
7146 (eval-when-compile
7147   (defmacro gnus-summary-search-article-position-point (regexp backward)
7148     "Dehighlight the last matched text and goto the beginning position."
7149     (` (if (and gnus-summary-search-article-matched-data
7150                 (let ((text (caddr gnus-summary-search-article-matched-data))
7151                       (inhibit-read-only t)
7152                       buffer-read-only)
7153                   (delete-region
7154                    (goto-char (car gnus-summary-search-article-matched-data))
7155                    (cadr gnus-summary-search-article-matched-data))
7156                   (insert text)
7157                   (string-match (, regexp) text)))
7158            (if (, backward) (beginning-of-line) (end-of-line))
7159          (goto-char (if (, backward) (point-max) (point-min))))))
7160
7161   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7162     "Place point where X-Face image is displayed."
7163     (if (featurep 'xemacs)
7164         (` (let ((end (if (search-forward "\n\n" nil t)
7165                           (goto-char (1- (point)))
7166                         (point-min)))
7167                  extent)
7168              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7169              (unless (and (re-search-forward "^From:" end t)
7170                           (setq extent (extent-at (point)))
7171                           (extent-begin-glyph extent))
7172                (goto-char (, opoint)))))
7173       (` (let ((end (if (search-forward "\n\n" nil t)
7174                         (goto-char (1- (point)))
7175                       (point-min))))
7176            (goto-char
7177             (or (text-property-any (or (search-backward "\n\n" nil t)
7178                                        (point-min))
7179                                    end 'x-face-mule-bitmap-image t)
7180                 (, opoint)))))))
7181
7182   (defmacro gnus-summary-search-article-highlight-matched-text
7183     (backward treated x-face)
7184     "Highlight matched text in the function `gnus-summary-search-article'."
7185     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7186              (end (set-marker (make-marker) (match-end 0)))
7187              (inhibit-read-only t)
7188              buffer-read-only)
7189          (unless treated
7190            (let ((,@
7191                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7192                     (mapcar
7193                      (lambda (item) (setq items (delq item items)))
7194                      '(gnus-treat-buttonize
7195                        gnus-treat-fill-article
7196                        gnus-treat-fill-long-lines
7197                        gnus-treat-emphasize
7198                        gnus-treat-highlight-headers
7199                        gnus-treat-highlight-citation
7200                        gnus-treat-highlight-signature
7201                        gnus-treat-overstrike
7202                        gnus-treat-display-xface
7203                        gnus-treat-buttonize-head
7204                        gnus-treat-decode-article-as-default-mime-charset))
7205                     (static-if (featurep 'xemacs)
7206                         items
7207                       (cons '(x-face-mule-delete-x-face-field
7208                               (quote never))
7209                             items))))
7210                  (gnus-treat-display-xface
7211                   (when (, x-face) gnus-treat-display-xface)))
7212              (gnus-article-prepare-mime-display)))
7213          (goto-char (if (, backward) start end))
7214          (when (, x-face)
7215            (gnus-summary-search-article-highlight-goto-x-face (point)))
7216          (setq gnus-summary-search-article-matched-data
7217                (list start end (buffer-substring start end)))
7218          (unless (eq start end);; matched text has been deleted. :-<
7219            (put-text-property start end 'face
7220                               (or (find-face 'isearch)
7221                                   'secondary-selection))))))
7222   )
7223
7224 (defun gnus-summary-search-article (regexp &optional backward)
7225   "Search for an article containing REGEXP.
7226 Optional argument BACKWARD means do search for backward.
7227 `gnus-select-article-hook' is not called during the search."
7228   ;; We have to require this here to make sure that the following
7229   ;; dynamic binding isn't shadowed by autoloading.
7230   (require 'gnus-async)
7231   (require 'gnus-art)
7232   (let ((gnus-select-article-hook nil)  ;Disable hook.
7233         (gnus-article-display-hook nil)
7234         (gnus-article-prepare-hook nil)
7235         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7236         (gnus-use-article-prefetch nil)
7237         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7238         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7239         (sum (current-buffer))
7240         (found nil)
7241         point treated)
7242     (gnus-save-hidden-threads
7243       (static-if (featurep 'xemacs)
7244           (let ((gnus-inhibit-treatment t))
7245             (setq treated (eq 'old (gnus-summary-select-article)))
7246             (when (and treated
7247                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7248                                  (window-live-p (get-buffer-window
7249                                                  gnus-article-buffer t)))))
7250               (gnus-summary-select-article nil t)
7251               (setq treated nil)))
7252         (let ((gnus-inhibit-treatment t)
7253               (x-face-mule-delete-x-face-field 'never))
7254           (setq treated (eq 'old (gnus-summary-select-article)))
7255           (when (and treated
7256                      (not
7257                       (and (gnus-buffer-live-p gnus-article-buffer)
7258                            (window-live-p (get-buffer-window
7259                                            gnus-article-buffer t))
7260                            (or (not (string-match "^\\^X-Face:" regexp))
7261                                (with-current-buffer gnus-article-buffer
7262                                  gnus-summary-search-article-matched-data)))))
7263             (gnus-summary-select-article nil t)
7264             (setq treated nil))))
7265       (set-buffer gnus-article-buffer)
7266       (widen)
7267       (if treated
7268           (progn
7269             (gnus-article-show-all-headers)
7270             (gnus-summary-search-article-position-point regexp backward))
7271         (goto-char (if backward (point-max) (point-min))))
7272       (while (not found)
7273         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7274         (if (if backward
7275                 (re-search-backward regexp nil t)
7276               (re-search-forward regexp nil t))
7277             ;; We found the regexp.
7278             (progn
7279               (gnus-summary-search-article-highlight-matched-text
7280                backward treated (string-match "^\\^X-Face:" regexp))
7281               (setq found 'found)
7282               (forward-line
7283                (/ (- 2 (window-height
7284                         (get-buffer-window gnus-article-buffer t)))
7285                   2))
7286               (set-window-start
7287                (get-buffer-window (current-buffer))
7288                (point))
7289               (set-buffer sum)
7290               (setq point (point)))
7291           ;; We didn't find it, so we go to the next article.
7292           (set-buffer sum)
7293           (setq found 'not)
7294           (while (eq found 'not)
7295             (if (not (if backward (gnus-summary-find-prev)
7296                        (gnus-summary-find-next)))
7297                 ;; No more articles.
7298                 (setq found t)
7299               ;; Select the next article and adjust point.
7300               (unless (gnus-summary-article-sparse-p
7301                        (gnus-summary-article-number))
7302                 (setq found nil)
7303                 (let ((gnus-inhibit-treatment t))
7304                   (gnus-summary-select-article))
7305                 (setq treated nil)
7306                 (set-buffer gnus-article-buffer)
7307                 (widen)
7308                 (goto-char (if backward (point-max) (point-min))))))))
7309       (gnus-message 7 ""))
7310     ;; Return whether we found the regexp.
7311     (when (eq found 'found)
7312       (goto-char point)
7313       (gnus-summary-show-thread)
7314       (gnus-summary-goto-subject gnus-current-article)
7315       (gnus-summary-position-point)
7316       t)))
7317
7318 (defun gnus-summary-find-matching (header regexp &optional backward unread
7319                                           not-case-fold)
7320   "Return a list of all articles that match REGEXP on HEADER.
7321 The search stars on the current article and goes forwards unless
7322 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7323 If UNREAD is non-nil, only unread articles will
7324 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7325 in the comparisons."
7326   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7327                 (gnus-data-find-list
7328                  (gnus-summary-article-number) (gnus-data-list backward))))
7329         (case-fold-search (not not-case-fold))
7330         articles d func)
7331     (if (consp header)
7332         (if (eq (car header) 'extra)
7333             (setq func
7334                   `(lambda (h)
7335                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7336                          "")))
7337           (error "%s is an invalid header" header))
7338       (unless (fboundp (intern (concat "mail-header-" header)))
7339         (error "%s is not a valid header" header))
7340       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7341     (while data
7342       (setq d (car data))
7343       (and (or (not unread)             ; We want all articles...
7344                (gnus-data-unread-p d))  ; Or just unreads.
7345            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7346            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7347            (push (gnus-data-number d) articles)) ; Success!
7348       (setq data (cdr data)))
7349     (nreverse articles)))
7350
7351 (defun gnus-summary-execute-command (header regexp command &optional backward)
7352   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7353 If HEADER is an empty string (or nil), the match is done on the entire
7354 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7355   (interactive
7356    (list (let ((completion-ignore-case t))
7357            (completing-read
7358             "Header name: "
7359             (mapcar (lambda (string) (list string))
7360                     '("Number" "Subject" "From" "Lines" "Date"
7361                       "Message-ID" "Xref" "References" "Body"))
7362             nil 'require-match))
7363          (read-string "Regexp: ")
7364          (read-key-sequence "Command: ")
7365          current-prefix-arg))
7366   (when (equal header "Body")
7367     (setq header ""))
7368   ;; Hidden thread subtrees must be searched as well.
7369   (gnus-summary-show-all-threads)
7370   ;; We don't want to change current point nor window configuration.
7371   (save-excursion
7372     (save-window-excursion
7373       (gnus-message 6 "Executing %s..." (key-description command))
7374       ;; We'd like to execute COMMAND interactively so as to give arguments.
7375       (gnus-execute header regexp
7376                     `(call-interactively ',(key-binding command))
7377                     backward)
7378       (gnus-message 6 "Executing %s...done" (key-description command)))))
7379
7380 (defun gnus-summary-beginning-of-article ()
7381   "Scroll the article back to the beginning."
7382   (interactive)
7383   (gnus-summary-select-article)
7384   (gnus-configure-windows 'article)
7385   (gnus-eval-in-buffer-window gnus-article-buffer
7386     (widen)
7387     (goto-char (point-min))
7388     (when gnus-page-broken
7389       (gnus-narrow-to-page))))
7390
7391 (defun gnus-summary-end-of-article ()
7392   "Scroll to the end of the article."
7393   (interactive)
7394   (gnus-summary-select-article)
7395   (gnus-configure-windows 'article)
7396   (gnus-eval-in-buffer-window gnus-article-buffer
7397     (widen)
7398     (goto-char (point-max))
7399     (recenter -3)
7400     (when gnus-page-broken
7401       (gnus-narrow-to-page))))
7402
7403 (defun gnus-summary-print-article (&optional filename n)
7404   "Generate and print a PostScript image of the N next (mail) articles.
7405
7406 If N is negative, print the N previous articles.  If N is nil and articles
7407 have been marked with the process mark, print these instead.
7408
7409 If the optional first argument FILENAME is nil, send the image to the
7410 printer.  If FILENAME is a string, save the PostScript image in a file with
7411 that name.  If FILENAME is a number, prompt the user for the name of the file
7412 to save in."
7413   (interactive (list (ps-print-preprint current-prefix-arg)
7414                      current-prefix-arg))
7415   (dolist (article (gnus-summary-work-articles n))
7416     (gnus-summary-select-article nil nil 'pseudo article)
7417     (gnus-eval-in-buffer-window gnus-article-buffer
7418       (let ((buffer (generate-new-buffer " *print*")))
7419         (unwind-protect
7420             (progn
7421               (copy-to-buffer buffer (point-min) (point-max))
7422               (set-buffer buffer)
7423               (gnus-article-delete-invisible-text)
7424               (let ((ps-left-header
7425                      (list
7426                       (concat "("
7427                               (mail-header-subject gnus-current-headers) ")")
7428                       (concat "("
7429                               (mail-header-from gnus-current-headers) ")")))
7430                     (ps-right-header
7431                      (list
7432                       "/pagenumberstring load"
7433                       (concat "("
7434                               (mail-header-date gnus-current-headers) ")"))))
7435                 (gnus-run-hooks 'gnus-ps-print-hook)
7436                 (save-excursion
7437                   (ps-print-buffer-with-faces filename))))
7438           (kill-buffer buffer))))))
7439
7440 (defun gnus-summary-show-article (&optional arg)
7441   "Force re-fetching of the current article.
7442 If ARG (the prefix) is a number, show the article with the charset
7443 defined in `gnus-summary-show-article-charset-alist', or the charset
7444 inputed.
7445 If ARG (the prefix) is non-nil and not a number, show the raw article
7446 without any article massaging functions being run."
7447   (interactive "P")
7448   (cond
7449    ((numberp arg)
7450     (let ((gnus-newsgroup-charset
7451            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7452                (read-coding-system "Charset: ")))
7453           (gnus-newsgroup-ignored-charsets 'gnus-all))
7454       (gnus-summary-select-article nil 'force)
7455       (let ((deps gnus-newsgroup-dependencies)
7456             head header)
7457         (save-excursion
7458           (set-buffer gnus-original-article-buffer)
7459           (save-restriction
7460             (message-narrow-to-head)
7461             (setq head (buffer-string)))
7462           (with-temp-buffer
7463             (insert (format "211 %d Article retrieved.\n"
7464                             (cdr gnus-article-current)))
7465             (insert head)
7466             (insert ".\n")
7467             (let ((nntp-server-buffer (current-buffer)))
7468               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7469         (gnus-data-set-header
7470          (gnus-data-find (cdr gnus-article-current))
7471          header)
7472         (gnus-summary-update-article-line
7473          (cdr gnus-article-current) header))))
7474    ((not arg)
7475     ;; Select the article the normal way.
7476     (gnus-summary-select-article nil 'force))
7477    (t
7478     ;; We have to require this here to make sure that the following
7479     ;; dynamic binding isn't shadowed by autoloading.
7480     (require 'gnus-async)
7481     (require 'gnus-art)
7482     ;; Bind the article treatment functions to nil.
7483     (let ((gnus-have-all-headers t)
7484           gnus-article-display-hook
7485           gnus-article-prepare-hook
7486           gnus-article-decode-hook
7487           gnus-break-pages
7488           gnus-show-mime)
7489       (gnus-summary-select-article nil 'force))))
7490   (gnus-summary-goto-subject gnus-current-article)
7491   (gnus-summary-position-point))
7492
7493 (defun gnus-summary-verbose-headers (&optional arg)
7494   "Toggle permanent full header display.
7495 If ARG is a positive number, turn header display on.
7496 If ARG is a negative number, turn header display off."
7497   (interactive "P")
7498   (setq gnus-show-all-headers
7499         (cond ((or (not (numberp arg))
7500                    (zerop arg))
7501                (not gnus-show-all-headers))
7502               ((natnump arg)
7503                t)))
7504   (gnus-summary-show-article))
7505
7506 (defun gnus-summary-toggle-header (&optional arg)
7507   "Show the headers if they are hidden, or hide them if they are shown.
7508 If ARG is a positive number, show the entire header.
7509 If ARG is a negative number, hide the unwanted header lines."
7510   (interactive "P")
7511   (save-excursion
7512     (set-buffer gnus-article-buffer)
7513     (save-restriction
7514       (let* ((buffer-read-only nil)
7515              (inhibit-point-motion-hooks t)
7516              hidden e)
7517         (setq hidden
7518               (if (numberp arg)
7519                   (>= arg 0)
7520                 (save-restriction
7521                   (article-narrow-to-head)
7522                   (gnus-article-hidden-text-p 'headers))))
7523         (goto-char (point-min))
7524         (when (search-forward "\n\n" nil t)
7525           (delete-region (point-min) (1- (point))))
7526         (goto-char (point-min))
7527         (save-excursion
7528           (set-buffer gnus-original-article-buffer)
7529           (goto-char (point-min))
7530           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7531         (insert-buffer-substring gnus-original-article-buffer 1 e)
7532         (save-restriction
7533           (narrow-to-region (point-min) (point))
7534           (article-decode-encoded-words)
7535           (if  hidden
7536               (let ((gnus-treat-hide-headers nil)
7537                     (gnus-treat-hide-boring-headers nil))
7538                 (setq gnus-article-wash-types
7539                       (delq 'headers gnus-article-wash-types))
7540                 (gnus-treat-article 'head))
7541             (gnus-treat-article 'head)))
7542         (gnus-set-mode-line 'article)))))
7543
7544 (defun gnus-summary-show-all-headers ()
7545   "Make all header lines visible."
7546   (interactive)
7547   (gnus-article-show-all-headers))
7548
7549 (defun gnus-summary-toggle-mime (&optional arg)
7550   "Toggle MIME processing.
7551 If ARG is a positive number, turn MIME processing on."
7552   (interactive "P")
7553   (setq gnus-show-mime
7554         (if (null arg)
7555             (not gnus-show-mime)
7556           (> (prefix-numeric-value arg) 0)))
7557   (gnus-summary-select-article t 'force))
7558
7559 (defun gnus-summary-caesar-message (&optional arg)
7560   "Caesar rotate the current article by 13.
7561 The numerical prefix specifies how many places to rotate each letter
7562 forward."
7563   (interactive "P")
7564   (gnus-summary-select-article)
7565   (let ((mail-header-separator ""))
7566     (gnus-eval-in-buffer-window gnus-article-buffer
7567       (save-restriction
7568         (widen)
7569         (let ((start (window-start))
7570               buffer-read-only)
7571           (message-caesar-buffer-body arg)
7572           (set-window-start (get-buffer-window (current-buffer)) start))))))
7573
7574 (defun gnus-summary-stop-page-breaking ()
7575   "Stop page breaking in the current article."
7576   (interactive)
7577   (gnus-summary-select-article)
7578   (gnus-eval-in-buffer-window gnus-article-buffer
7579     (widen)
7580     (when (gnus-visual-p 'page-marker)
7581       (let ((buffer-read-only nil))
7582         (gnus-remove-text-with-property 'gnus-prev)
7583         (gnus-remove-text-with-property 'gnus-next))
7584       (setq gnus-page-broken nil))))
7585
7586 (defun gnus-summary-move-article (&optional n to-newsgroup
7587                                             select-method action)
7588   "Move the current article to a different newsgroup.
7589 If N is a positive number, move the N next articles.
7590 If N is a negative number, move the N previous articles.
7591 If N is nil and any articles have been marked with the process mark,
7592 move those articles instead.
7593 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7594 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7595 re-spool using this method.
7596
7597 For this function to work, both the current newsgroup and the
7598 newsgroup that you want to move to have to support the `request-move'
7599 and `request-accept' functions.
7600
7601 ACTION can be either `move' (the default), `crosspost' or `copy'."
7602   (interactive "P")
7603   (unless action
7604     (setq action 'move))
7605   ;; Disable marking as read.
7606   (let (gnus-mark-article-hook)
7607     (save-window-excursion
7608       (gnus-summary-select-article)))
7609   ;; Check whether the source group supports the required functions.
7610   (cond ((and (eq action 'move)
7611               (not (gnus-check-backend-function
7612                     'request-move-article gnus-newsgroup-name)))
7613          (error "The current group does not support article moving"))
7614         ((and (eq action 'crosspost)
7615               (not (gnus-check-backend-function
7616                     'request-replace-article gnus-newsgroup-name)))
7617          (error "The current group does not support article editing")))
7618   (let ((articles (gnus-summary-work-articles n))
7619         (prefix (if (gnus-check-backend-function
7620                     'request-move-article gnus-newsgroup-name)
7621                     (gnus-group-real-prefix gnus-newsgroup-name)
7622                   ""))
7623         (names '((move "Move" "Moving")
7624                  (copy "Copy" "Copying")
7625                  (crosspost "Crosspost" "Crossposting")))
7626         (copy-buf (save-excursion
7627                     (nnheader-set-temp-buffer " *copy article*")))
7628         (default-marks gnus-article-mark-lists)
7629         (no-expire-marks (delete '(expirable . expire)
7630                                  (copy-sequence gnus-article-mark-lists)))
7631         art-group to-method new-xref article to-groups)
7632     (unless (assq action names)
7633       (error "Unknown action %s" action))
7634     ;; Read the newsgroup name.
7635     (when (and (not to-newsgroup)
7636                (not select-method))
7637       (setq to-newsgroup
7638             (gnus-read-move-group-name
7639              (cadr (assq action names))
7640              (symbol-value (intern (format "gnus-current-%s-group" action)))
7641              articles prefix))
7642       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7643     (setq to-method (or select-method
7644                         (gnus-server-to-method
7645                          (gnus-group-method to-newsgroup))))
7646     ;; Check the method we are to move this article to...
7647     (unless (gnus-check-backend-function
7648              'request-accept-article (car to-method))
7649       (error "%s does not support article copying" (car to-method)))
7650     (unless (gnus-check-server to-method)
7651       (error "Can't open server %s" (car to-method)))
7652     (gnus-message 6 "%s to %s: %s..."
7653                   (caddr (assq action names))
7654                   (or (car select-method) to-newsgroup) articles)
7655     (while articles
7656       (setq article (pop articles))
7657       (setq
7658        art-group
7659        (cond
7660         ;; Move the article.
7661         ((eq action 'move)
7662          ;; Remove this article from future suppression.
7663          (gnus-dup-unsuppress-article article)
7664          (gnus-request-move-article
7665           article                       ; Article to move
7666           gnus-newsgroup-name           ; From newsgroup
7667           (nth 1 (gnus-find-method-for-group
7668                   gnus-newsgroup-name)) ; Server
7669           (list 'gnus-request-accept-article
7670                 to-newsgroup (list 'quote select-method)
7671                 (not articles) t)       ; Accept form
7672           (not articles)))              ; Only save nov last time
7673         ;; Copy the article.
7674         ((eq action 'copy)
7675          (save-excursion
7676            (set-buffer copy-buf)
7677            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7678              (gnus-request-accept-article
7679               to-newsgroup select-method (not articles) t))))
7680         ;; Crosspost the article.
7681         ((eq action 'crosspost)
7682          (let ((xref (message-tokenize-header
7683                       (mail-header-xref (gnus-summary-article-header article))
7684                       " ")))
7685            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7686                                   ":" article))
7687            (unless xref
7688              (setq xref (list (system-name))))
7689            (setq new-xref
7690                  (concat
7691                   (mapconcat 'identity
7692                              (delete "Xref:" (delete new-xref xref))
7693                              " ")
7694                   " " new-xref))
7695            (save-excursion
7696              (set-buffer copy-buf)
7697              ;; First put the article in the destination group.
7698              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7699              (when (consp (setq art-group
7700                                 (gnus-request-accept-article
7701                                  to-newsgroup select-method (not articles))))
7702                (setq new-xref (concat new-xref " " (car art-group)
7703                                       ":" (cdr art-group)))
7704                ;; Now we have the new Xrefs header, so we insert
7705                ;; it and replace the new article.
7706                (nnheader-replace-header "Xref" new-xref)
7707                (gnus-request-replace-article
7708                 (cdr art-group) to-newsgroup (current-buffer))
7709                art-group))))))
7710       (cond
7711        ((not art-group)
7712         (gnus-message 1 "Couldn't %s article %s: %s"
7713                       (cadr (assq action names)) article
7714                       (nnheader-get-report (car to-method))))
7715        ((eq art-group 'junk)
7716         (when (eq action 'move)
7717           (gnus-summary-mark-article article gnus-canceled-mark)
7718           (gnus-message 4 "Deleted article %s" article)))
7719        (t
7720         (let* ((pto-group (gnus-group-prefixed-name
7721                            (car art-group) to-method))
7722                (entry
7723                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7724                (info (nth 2 entry))
7725                (to-group (gnus-info-group info))
7726                to-marks)
7727           ;; Update the group that has been moved to.
7728           (when (and info
7729                      (memq action '(move copy)))
7730             (unless (member to-group to-groups)
7731               (push to-group to-groups))
7732
7733             (unless (memq article gnus-newsgroup-unreads)
7734               (push 'read to-marks)
7735               (gnus-info-set-read
7736                info (gnus-add-to-range (gnus-info-read info)
7737                                        (list (cdr art-group)))))
7738
7739             ;; See whether the article is to be put in the cache.
7740             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7741                              default-marks
7742                            no-expire-marks))
7743                   (to-article (cdr art-group)))
7744
7745               ;; Enter the article into the cache in the new group,
7746               ;; if that is required.
7747               (when gnus-use-cache
7748                 (gnus-cache-possibly-enter-article
7749                  to-group to-article
7750                  (let ((header (copy-sequence
7751                                 (gnus-summary-article-header article))))
7752                    (mail-header-set-number header to-article)
7753                    header)
7754                  (memq article gnus-newsgroup-marked)
7755                  (memq article gnus-newsgroup-dormant)
7756                  (memq article gnus-newsgroup-unreads)))
7757
7758               (when gnus-preserve-marks
7759                 ;; Copy any marks over to the new group.
7760                 (when (and (equal to-group gnus-newsgroup-name)
7761                            (not (memq article gnus-newsgroup-unreads)))
7762                   ;; Mark this article as read in this group.
7763                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7764                   (setcdr (gnus-active to-group) to-article)
7765                   (setcdr gnus-newsgroup-active to-article))
7766
7767                 (while marks
7768                   (when (memq article (symbol-value
7769                                        (intern (format "gnus-newsgroup-%s"
7770                                                        (caar marks)))))
7771                     (push (cdar marks) to-marks)
7772                     ;; If the other group is the same as this group,
7773                     ;; then we have to add the mark to the list.
7774                     (when (equal to-group gnus-newsgroup-name)
7775                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7776                            (cons to-article
7777                                  (symbol-value
7778                                   (intern (format "gnus-newsgroup-%s"
7779                                                   (caar marks)))))))
7780                     ;; Copy the marks to other group.
7781                     (gnus-add-marked-articles
7782                      to-group (cdar marks) (list to-article) info))
7783                   (setq marks (cdr marks)))
7784
7785                 (gnus-request-set-mark to-group (list (list (list to-article)
7786                                                             'set
7787                                                             to-marks))))
7788
7789               (gnus-dribble-enter
7790                (concat "(gnus-group-set-info '"
7791                        (gnus-prin1-to-string (gnus-get-info to-group))
7792                        ")"))))
7793
7794           ;; Update the Xref header in this article to point to
7795           ;; the new crossposted article we have just created.
7796           (when (eq action 'crosspost)
7797             (save-excursion
7798               (set-buffer copy-buf)
7799               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7800               (nnheader-replace-header "Xref" new-xref)
7801               (gnus-request-replace-article
7802                article gnus-newsgroup-name (current-buffer)))))
7803
7804         ;;;!!!Why is this necessary?
7805         (set-buffer gnus-summary-buffer)
7806
7807         (gnus-summary-goto-subject article)
7808         (when (eq action 'move)
7809           (gnus-summary-mark-article article gnus-canceled-mark))))
7810       (gnus-summary-remove-process-mark article))
7811     ;; Re-activate all groups that have been moved to.
7812     (while to-groups
7813       (save-excursion
7814         (set-buffer gnus-group-buffer)
7815         (when (gnus-group-goto-group (car to-groups) t)
7816           (gnus-group-get-new-news-this-group 1 t))
7817         (pop to-groups)))
7818
7819     (gnus-kill-buffer copy-buf)
7820     (gnus-summary-position-point)
7821     (gnus-set-mode-line 'summary)))
7822
7823 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7824   "Move the current article to a different newsgroup.
7825 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7826 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7827 re-spool using this method."
7828   (interactive "P")
7829   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7830
7831 (defun gnus-summary-crosspost-article (&optional n)
7832   "Crosspost the current article to some other group."
7833   (interactive "P")
7834   (gnus-summary-move-article n nil nil 'crosspost))
7835
7836 (defcustom gnus-summary-respool-default-method nil
7837   "Default method for respooling an article.
7838 If nil, use to the current newsgroup method."
7839   :type '(choice (gnus-select-method :value (nnml ""))
7840                  (const nil))
7841   :group 'gnus-summary-mail)
7842
7843 (defun gnus-summary-respool-article (&optional n method)
7844   "Respool the current article.
7845 The article will be squeezed through the mail spooling process again,
7846 which means that it will be put in some mail newsgroup or other
7847 depending on `nnmail-split-methods'.
7848 If N is a positive number, respool the N next articles.
7849 If N is a negative number, respool the N previous articles.
7850 If N is nil and any articles have been marked with the process mark,
7851 respool those articles instead.
7852
7853 Respooling can be done both from mail groups and \"real\" newsgroups.
7854 In the former case, the articles in question will be moved from the
7855 current group into whatever groups they are destined to.  In the
7856 latter case, they will be copied into the relevant groups."
7857   (interactive
7858    (list current-prefix-arg
7859          (let* ((methods (gnus-methods-using 'respool))
7860                 (methname
7861                  (symbol-name (or gnus-summary-respool-default-method
7862                                   (car (gnus-find-method-for-group
7863                                         gnus-newsgroup-name)))))
7864                 (method
7865                  (gnus-completing-read
7866                   methname "What backend do you want to use when respooling?"
7867                   methods nil t nil 'gnus-mail-method-history))
7868                 ms)
7869            (cond
7870             ((zerop (length (setq ms (gnus-servers-using-backend
7871                                       (intern method)))))
7872              (list (intern method) ""))
7873             ((= 1 (length ms))
7874              (car ms))
7875             (t
7876              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7877                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7878                            ms-alist))))))))
7879   (unless method
7880     (error "No method given for respooling"))
7881   (if (assoc (symbol-name
7882               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7883              (gnus-methods-using 'respool))
7884       (gnus-summary-move-article n nil method)
7885     (gnus-summary-copy-article n nil method)))
7886
7887 (defun gnus-summary-import-article (file)
7888   "Import an arbitrary file into a mail newsgroup."
7889   (interactive "fImport file: ")
7890   (let ((group gnus-newsgroup-name)
7891         (now (current-time))
7892         atts lines)
7893     (unless (gnus-check-backend-function 'request-accept-article group)
7894       (error "%s does not support article importing" group))
7895     (or (file-readable-p file)
7896         (not (file-regular-p file))
7897         (error "Can't read %s" file))
7898     (save-excursion
7899       (set-buffer (gnus-get-buffer-create " *import file*"))
7900       (erase-buffer)
7901       (nnheader-insert-file-contents file)
7902       (goto-char (point-min))
7903       (unless (nnheader-article-p)
7904         ;; This doesn't look like an article, so we fudge some headers.
7905         (setq atts (file-attributes file)
7906               lines (count-lines (point-min) (point-max)))
7907         (insert "From: " (read-string "From: ") "\n"
7908                 "Subject: " (read-string "Subject: ") "\n"
7909                 "Date: " (message-make-date (nth 5 atts))
7910                 "\n"
7911                 "Message-ID: " (message-make-message-id) "\n"
7912                 "Lines: " (int-to-string lines) "\n"
7913                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7914       (gnus-request-accept-article group nil t)
7915       (kill-buffer (current-buffer)))))
7916
7917 (defun gnus-summary-article-posted-p ()
7918   "Say whether the current (mail) article is available from news as well.
7919 This will be the case if the article has both been mailed and posted."
7920   (interactive)
7921   (let ((id (mail-header-references (gnus-summary-article-header)))
7922         (gnus-override-method (car (gnus-refer-article-methods))))
7923     (if (gnus-request-head id "")
7924         (gnus-message 2 "The current message was found on %s"
7925                       gnus-override-method)
7926       (gnus-message 2 "The current message couldn't be found on %s"
7927                     gnus-override-method)
7928       nil)))
7929
7930 (defun gnus-summary-expire-articles (&optional now)
7931   "Expire all articles that are marked as expirable in the current group."
7932   (interactive)
7933   (when (gnus-check-backend-function
7934          'request-expire-articles gnus-newsgroup-name)
7935     ;; This backend supports expiry.
7936     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7937            (expirable (if total
7938                           (progn
7939                             ;; We need to update the info for
7940                             ;; this group for `gnus-list-of-read-articles'
7941                             ;; to give us the right answer.
7942                             (gnus-run-hooks 'gnus-exit-group-hook)
7943                             (gnus-summary-update-info)
7944                             (gnus-list-of-read-articles gnus-newsgroup-name))
7945                         (setq gnus-newsgroup-expirable
7946                               (sort gnus-newsgroup-expirable '<))))
7947            (expiry-wait (if now 'immediate
7948                           (gnus-group-find-parameter
7949                            gnus-newsgroup-name 'expiry-wait)))
7950            (nnmail-expiry-target
7951             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7952                 nnmail-expiry-target))
7953            es)
7954       (when expirable
7955         ;; There are expirable articles in this group, so we run them
7956         ;; through the expiry process.
7957         (gnus-message 6 "Expiring articles...")
7958         (unless (gnus-check-group gnus-newsgroup-name)
7959           (error "Can't open server for %s" gnus-newsgroup-name))
7960         ;; The list of articles that weren't expired is returned.
7961         (save-excursion
7962           (if expiry-wait
7963               (let ((nnmail-expiry-wait-function nil)
7964                     (nnmail-expiry-wait expiry-wait))
7965                 (setq es (gnus-request-expire-articles
7966                           expirable gnus-newsgroup-name)))
7967             (setq es (gnus-request-expire-articles
7968                       expirable gnus-newsgroup-name)))
7969           (unless total
7970             (setq gnus-newsgroup-expirable es))
7971           ;; We go through the old list of expirable, and mark all
7972           ;; really expired articles as nonexistent.
7973           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7974             (let ((gnus-use-cache nil))
7975               (while expirable
7976                 (unless (memq (car expirable) es)
7977                   (when (gnus-data-find (car expirable))
7978                     (gnus-summary-mark-article
7979                      (car expirable) gnus-canceled-mark)))
7980                 (setq expirable (cdr expirable))))))
7981         (gnus-message 6 "Expiring articles...done")))))
7982
7983 (defun gnus-summary-expire-articles-now ()
7984   "Expunge all expirable articles in the current group.
7985 This means that *all* articles that are marked as expirable will be
7986 deleted forever, right now."
7987   (interactive)
7988   (or gnus-expert-user
7989       (gnus-yes-or-no-p
7990        "Are you really, really, really sure you want to delete all these messages? ")
7991       (error "Phew!"))
7992   (gnus-summary-expire-articles t))
7993
7994 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7995 (defun gnus-summary-delete-article (&optional n)
7996   "Delete the N next (mail) articles.
7997 This command actually deletes articles.  This is not a marking
7998 command.  The article will disappear forever from your life, never to
7999 return.
8000 If N is negative, delete backwards.
8001 If N is nil and articles have been marked with the process mark,
8002 delete these instead."
8003   (interactive "P")
8004   (unless (gnus-check-backend-function 'request-expire-articles
8005                                        gnus-newsgroup-name)
8006     (error "The current newsgroup does not support article deletion"))
8007   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8008     (error "Couldn't open server"))
8009   ;; Compute the list of articles to delete.
8010   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8011         not-deleted)
8012     (if (and gnus-novice-user
8013              (not (gnus-yes-or-no-p
8014                    (format "Do you really want to delete %s forever? "
8015                            (if (> (length articles) 1)
8016                                (format "these %s articles" (length articles))
8017                              "this article")))))
8018         ()
8019       ;; Delete the articles.
8020       (setq not-deleted (gnus-request-expire-articles
8021                          articles gnus-newsgroup-name 'force))
8022       (while articles
8023         (gnus-summary-remove-process-mark (car articles))
8024         ;; The backend might not have been able to delete the article
8025         ;; after all.
8026         (unless (memq (car articles) not-deleted)
8027           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8028         (setq articles (cdr articles)))
8029       (when not-deleted
8030         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8031     (gnus-summary-position-point)
8032     (gnus-set-mode-line 'summary)
8033     not-deleted))
8034
8035 (defun gnus-summary-edit-article (&optional force)
8036   "Edit the current article.
8037 This will have permanent effect only in mail groups.
8038 If FORCE is non-nil, allow editing of articles even in read-only
8039 groups."
8040   (interactive "P")
8041   (save-excursion
8042     (set-buffer gnus-summary-buffer)
8043     (let ((mail-parse-charset gnus-newsgroup-charset)
8044           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8045       (gnus-set-global-variables)
8046       (when (and (not force)
8047                  (gnus-group-read-only-p))
8048         (error "The current newsgroup does not support article editing"))
8049       (gnus-summary-show-article t)
8050       (gnus-article-edit-article
8051        'ignore
8052        `(lambda (no-highlight)
8053           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8054                 (message-options message-options)
8055                 (message-options-set-recipient)
8056                 (mail-parse-ignored-charsets
8057                  ',gnus-newsgroup-ignored-charsets))
8058             (gnus-summary-edit-article-done
8059              ,(or (mail-header-references gnus-current-headers) "")
8060              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8061
8062 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8063
8064 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8065                                                  no-highlight)
8066   "Make edits to the current article permanent."
8067   (interactive)
8068   (save-excursion
8069     ;; The buffer restriction contains the entire article if it exists.
8070     (when (article-goto-body)
8071       (let ((lines (count-lines (point) (point-max)))
8072             (length (- (point-max) (point)))
8073             (case-fold-search t)
8074             (body (copy-marker (point))))
8075         (goto-char (point-min))
8076         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8077           (delete-region (match-beginning 1) (match-end 1))
8078           (insert (number-to-string length)))
8079         (goto-char (point-min))
8080         (when (re-search-forward
8081                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8082           (delete-region (match-beginning 1) (match-end 1))
8083           (insert (number-to-string length)))
8084         (goto-char (point-min))
8085         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8086           (delete-region (match-beginning 1) (match-end 1))
8087           (insert (number-to-string lines))))))
8088   ;; Replace the article.
8089   (let ((buf (current-buffer)))
8090     (with-temp-buffer
8091       (insert-buffer-substring buf)
8092       
8093       (if (and (not read-only)
8094                (not (gnus-request-replace-article
8095                      (cdr gnus-article-current) (car gnus-article-current)
8096                      (current-buffer) t)))
8097           (error "Couldn't replace article")
8098         ;; Update the summary buffer.
8099         (if (and references
8100                  (equal (message-tokenize-header references " ")
8101                         (message-tokenize-header
8102                          (or (message-fetch-field "references") "") " ")))
8103             ;; We only have to update this line.
8104             (save-excursion
8105               (save-restriction
8106                 (message-narrow-to-head)
8107                 (let ((head (buffer-string))
8108                       header)
8109                   (with-temp-buffer
8110                     (insert (format "211 %d Article retrieved.\n"
8111                                     (cdr gnus-article-current)))
8112                     (insert head)
8113                     (insert ".\n")
8114                     (let ((nntp-server-buffer (current-buffer)))
8115                       (setq header (car (gnus-get-newsgroup-headers
8116                                          (save-excursion
8117                                            (set-buffer gnus-summary-buffer)
8118                                            gnus-newsgroup-dependencies)
8119                                          t))))
8120                     (save-excursion
8121                       (set-buffer gnus-summary-buffer)
8122                       (gnus-data-set-header
8123                        (gnus-data-find (cdr gnus-article-current))
8124                        header)
8125                       (gnus-summary-update-article-line
8126                        (cdr gnus-article-current) header))))))
8127           ;; Update threads.
8128           (set-buffer (or buffer gnus-summary-buffer))
8129           (gnus-summary-update-article (cdr gnus-article-current)))
8130         ;; Prettify the article buffer again.
8131         (unless no-highlight
8132           (save-excursion
8133             (set-buffer gnus-article-buffer)
8134             ;;;!!! Fix this -- article should be rehighlighted.
8135             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8136             (set-buffer gnus-original-article-buffer)
8137             (gnus-request-article
8138              (cdr gnus-article-current)
8139              (car gnus-article-current) (current-buffer))))
8140         ;; Prettify the summary buffer line.
8141         (when (gnus-visual-p 'summary-highlight 'highlight)
8142           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8143
8144 (defun gnus-summary-edit-wash (key)
8145   "Perform editing command KEY in the article buffer."
8146   (interactive
8147    (list
8148     (progn
8149       (message "%s" (concat (this-command-keys) "- "))
8150       (read-char))))
8151   (message "")
8152   (gnus-summary-edit-article)
8153   (execute-kbd-macro (concat (this-command-keys) key))
8154   (gnus-article-edit-done))
8155
8156 ;;; Respooling
8157
8158 (defun gnus-summary-respool-query (&optional silent trace)
8159   "Query where the respool algorithm would put this article."
8160   (interactive)
8161   (let (gnus-mark-article-hook)
8162     (gnus-summary-select-article)
8163     (save-excursion
8164       (set-buffer gnus-original-article-buffer)
8165       (save-restriction
8166         (message-narrow-to-head)
8167         (let ((groups (nnmail-article-group 'identity trace)))
8168           (unless silent
8169             (if groups
8170                 (message "This message would go to %s"
8171                          (mapconcat 'car groups ", "))
8172               (message "This message would go to no groups"))
8173             groups))))))
8174
8175 (defun gnus-summary-respool-trace ()
8176   "Trace where the respool algorithm would put this article.
8177 Display a buffer showing all fancy splitting patterns which matched."
8178   (interactive)
8179   (gnus-summary-respool-query nil t))
8180
8181 ;; Summary marking commands.
8182
8183 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8184   "Mark articles which has the same subject as read, and then select the next.
8185 If UNMARK is positive, remove any kind of mark.
8186 If UNMARK is negative, tick articles."
8187   (interactive "P")
8188   (when unmark
8189     (setq unmark (prefix-numeric-value unmark)))
8190   (let ((count
8191          (gnus-summary-mark-same-subject
8192           (gnus-summary-article-subject) unmark)))
8193     ;; Select next unread article.  If auto-select-same mode, should
8194     ;; select the first unread article.
8195     (gnus-summary-next-article t (and gnus-auto-select-same
8196                                       (gnus-summary-article-subject)))
8197     (gnus-message 7 "%d article%s marked as %s"
8198                   count (if (= count 1) " is" "s are")
8199                   (if unmark "unread" "read"))))
8200
8201 (defun gnus-summary-kill-same-subject (&optional unmark)
8202   "Mark articles which has the same subject as read.
8203 If UNMARK is positive, remove any kind of mark.
8204 If UNMARK is negative, tick articles."
8205   (interactive "P")
8206   (when unmark
8207     (setq unmark (prefix-numeric-value unmark)))
8208   (let ((count
8209          (gnus-summary-mark-same-subject
8210           (gnus-summary-article-subject) unmark)))
8211     ;; If marked as read, go to next unread subject.
8212     (when (null unmark)
8213       ;; Go to next unread subject.
8214       (gnus-summary-next-subject 1 t))
8215     (gnus-message 7 "%d articles are marked as %s"
8216                   count (if unmark "unread" "read"))))
8217
8218 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8219   "Mark articles with same SUBJECT as read, and return marked number.
8220 If optional argument UNMARK is positive, remove any kinds of marks.
8221 If optional argument UNMARK is negative, mark articles as unread instead."
8222   (let ((count 1))
8223     (save-excursion
8224       (cond
8225        ((null unmark)                   ; Mark as read.
8226         (while (and
8227                 (progn
8228                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8229                   (gnus-summary-show-thread) t)
8230                 (gnus-summary-find-subject subject))
8231           (setq count (1+ count))))
8232        ((> unmark 0)                    ; Tick.
8233         (while (and
8234                 (progn
8235                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8236                   (gnus-summary-show-thread) t)
8237                 (gnus-summary-find-subject subject))
8238           (setq count (1+ count))))
8239        (t                               ; Mark as unread.
8240         (while (and
8241                 (progn
8242                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8243                   (gnus-summary-show-thread) t)
8244                 (gnus-summary-find-subject subject))
8245           (setq count (1+ count)))))
8246       (gnus-set-mode-line 'summary)
8247       ;; Return the number of marked articles.
8248       count)))
8249
8250 (defun gnus-summary-mark-as-processable (n &optional unmark)
8251   "Set the process mark on the next N articles.
8252 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8253 the process mark instead.  The difference between N and the actual
8254 number of articles marked is returned."
8255   (interactive "p")
8256   (let ((backward (< n 0))
8257         (n (abs n)))
8258     (while (and
8259             (> n 0)
8260             (if unmark
8261                 (gnus-summary-remove-process-mark
8262                  (gnus-summary-article-number))
8263               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8264             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8265       (setq n (1- n)))
8266     (when (/= 0 n)
8267       (gnus-message 7 "No more articles"))
8268     (gnus-summary-recenter)
8269     (gnus-summary-position-point)
8270     n))
8271
8272 (defun gnus-summary-unmark-as-processable (n)
8273   "Remove the process mark from the next N articles.
8274 If N is negative, unmark backward instead.  The difference between N and
8275 the actual number of articles unmarked is returned."
8276   (interactive "p")
8277   (gnus-summary-mark-as-processable n t))
8278
8279 (defun gnus-summary-unmark-all-processable ()
8280   "Remove the process mark from all articles."
8281   (interactive)
8282   (save-excursion
8283     (while gnus-newsgroup-processable
8284       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8285   (gnus-summary-position-point))
8286
8287 (defun gnus-summary-mark-as-expirable (n)
8288   "Mark N articles forward as expirable.
8289 If N is negative, mark backward instead.  The difference between N and
8290 the actual number of articles marked is returned."
8291   (interactive "p")
8292   (gnus-summary-mark-forward n gnus-expirable-mark))
8293
8294 (defun gnus-summary-mark-article-as-replied (article)
8295   "Mark ARTICLE replied and update the summary line."
8296   (push article gnus-newsgroup-replied)
8297   (let ((buffer-read-only nil))
8298     (when (gnus-summary-goto-subject article nil t)
8299       (gnus-summary-update-secondary-mark article))))
8300
8301 (defun gnus-summary-set-bookmark (article)
8302   "Set a bookmark in current article."
8303   (interactive (list (gnus-summary-article-number)))
8304   (when (or (not (get-buffer gnus-article-buffer))
8305             (not gnus-current-article)
8306             (not gnus-article-current)
8307             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8308     (error "No current article selected"))
8309   ;; Remove old bookmark, if one exists.
8310   (let ((old (assq article gnus-newsgroup-bookmarks)))
8311     (when old
8312       (setq gnus-newsgroup-bookmarks
8313             (delq old gnus-newsgroup-bookmarks))))
8314   ;; Set the new bookmark, which is on the form
8315   ;; (article-number . line-number-in-body).
8316   (push
8317    (cons article
8318          (save-excursion
8319            (set-buffer gnus-article-buffer)
8320            (count-lines
8321             (min (point)
8322                  (save-excursion
8323                    (goto-char (point-min))
8324                    (search-forward "\n\n" nil t)
8325                    (point)))
8326             (point))))
8327    gnus-newsgroup-bookmarks)
8328   (gnus-message 6 "A bookmark has been added to the current article."))
8329
8330 (defun gnus-summary-remove-bookmark (article)
8331   "Remove the bookmark from the current article."
8332   (interactive (list (gnus-summary-article-number)))
8333   ;; Remove old bookmark, if one exists.
8334   (let ((old (assq article gnus-newsgroup-bookmarks)))
8335     (if old
8336         (progn
8337           (setq gnus-newsgroup-bookmarks
8338                 (delq old gnus-newsgroup-bookmarks))
8339           (gnus-message 6 "Removed bookmark."))
8340       (gnus-message 6 "No bookmark in current article."))))
8341
8342 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8343 (defun gnus-summary-mark-as-dormant (n)
8344   "Mark N articles forward as dormant.
8345 If N is negative, mark backward instead.  The difference between N and
8346 the actual number of articles marked is returned."
8347   (interactive "p")
8348   (gnus-summary-mark-forward n gnus-dormant-mark))
8349
8350 (defun gnus-summary-set-process-mark (article)
8351   "Set the process mark on ARTICLE and update the summary line."
8352   (setq gnus-newsgroup-processable
8353         (cons article
8354               (delq article gnus-newsgroup-processable)))
8355   (when (gnus-summary-goto-subject article)
8356     (gnus-summary-show-thread)
8357     (gnus-summary-goto-subject article)
8358     (gnus-summary-update-secondary-mark article)))
8359
8360 (defun gnus-summary-remove-process-mark (article)
8361   "Remove the process mark from ARTICLE and update the summary line."
8362   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8363   (when (gnus-summary-goto-subject article)
8364     (gnus-summary-show-thread)
8365     (gnus-summary-goto-subject article)
8366     (gnus-summary-update-secondary-mark article)))
8367
8368 (defun gnus-summary-set-saved-mark (article)
8369   "Set the process mark on ARTICLE and update the summary line."
8370   (push article gnus-newsgroup-saved)
8371   (when (gnus-summary-goto-subject article)
8372     (gnus-summary-update-secondary-mark article)))
8373
8374 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8375   "Mark N articles as read forwards.
8376 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8377 The difference between N and the actual number of articles marked is
8378 returned.
8379 Iff NO-EXPIRE, auto-expiry will be inhibited."
8380   (interactive "p")
8381   (gnus-summary-show-thread)
8382   (let ((backward (< n 0))
8383         (gnus-summary-goto-unread
8384          (and gnus-summary-goto-unread
8385               (not (eq gnus-summary-goto-unread 'never))
8386               (not (memq mark (list gnus-unread-mark
8387                                     gnus-ticked-mark gnus-dormant-mark)))))
8388         (n (abs n))
8389         (mark (or mark gnus-del-mark)))
8390     (while (and (> n 0)
8391                 (gnus-summary-mark-article nil mark no-expire)
8392                 (zerop (gnus-summary-next-subject
8393                         (if backward -1 1)
8394                         (and gnus-summary-goto-unread
8395                              (not (eq gnus-summary-goto-unread 'never)))
8396                         t)))
8397       (setq n (1- n)))
8398     (when (/= 0 n)
8399       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8400     (gnus-summary-recenter)
8401     (gnus-summary-position-point)
8402     (gnus-set-mode-line 'summary)
8403     n))
8404
8405 (defun gnus-summary-mark-article-as-read (mark)
8406   "Mark the current article quickly as read with MARK."
8407   (let ((article (gnus-summary-article-number)))
8408     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8409     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8410     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8411     (push (cons article mark) gnus-newsgroup-reads)
8412     ;; Possibly remove from cache, if that is used.
8413     (when gnus-use-cache
8414       (gnus-cache-enter-remove-article article))
8415     ;; Allow the backend to change the mark.
8416     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8417     ;; Check for auto-expiry.
8418     (when (and gnus-newsgroup-auto-expire
8419                (memq mark gnus-auto-expirable-marks))
8420       (setq mark gnus-expirable-mark)
8421       ;; Let the backend know about the mark change.
8422       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8423       (push article gnus-newsgroup-expirable))
8424     ;; Set the mark in the buffer.
8425     (gnus-summary-update-mark mark 'unread)
8426     t))
8427
8428 (defun gnus-summary-mark-article-as-unread (mark)
8429   "Mark the current article quickly as unread with MARK."
8430   (let* ((article (gnus-summary-article-number))
8431          (old-mark (gnus-summary-article-mark article)))
8432     ;; Allow the backend to change the mark.
8433     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8434     (if (eq mark old-mark)
8435         t
8436       (if (<= article 0)
8437           (progn
8438             (gnus-error 1 "Can't mark negative article numbers")
8439             nil)
8440         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8441         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8442         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8443         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8444         (cond ((= mark gnus-ticked-mark)
8445                (push article gnus-newsgroup-marked))
8446               ((= mark gnus-dormant-mark)
8447                (push article gnus-newsgroup-dormant))
8448               (t
8449                (push article gnus-newsgroup-unreads)))
8450         (gnus-pull article gnus-newsgroup-reads)
8451
8452         ;; See whether the article is to be put in the cache.
8453         (and gnus-use-cache
8454              (vectorp (gnus-summary-article-header article))
8455              (save-excursion
8456                (gnus-cache-possibly-enter-article
8457                 gnus-newsgroup-name article
8458                 (gnus-summary-article-header article)
8459                 (= mark gnus-ticked-mark)
8460                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8461
8462         ;; Fix the mark.
8463         (gnus-summary-update-mark mark 'unread)
8464         t))))
8465
8466 (defun gnus-summary-mark-article (&optional article mark no-expire)
8467   "Mark ARTICLE with MARK.  MARK can be any character.
8468 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8469 `??' (dormant) and `?E' (expirable).
8470 If MARK is nil, then the default character `?r' is used.
8471 If ARTICLE is nil, then the article on the current line will be
8472 marked.
8473 Iff NO-EXPIRE, auto-expiry will be inhibited."
8474   ;; The mark might be a string.
8475   (when (stringp mark)
8476     (setq mark (aref mark 0)))
8477   ;; If no mark is given, then we check auto-expiring.
8478   (when (null mark)
8479     (setq mark gnus-del-mark))
8480   (when (and (not no-expire)
8481              gnus-newsgroup-auto-expire
8482              (memq mark gnus-auto-expirable-marks))
8483     (setq mark gnus-expirable-mark))
8484   (let ((article (or article (gnus-summary-article-number)))
8485         (old-mark (gnus-summary-article-mark article)))
8486     ;; Allow the backend to change the mark.
8487     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8488     (if (eq mark old-mark)
8489         t
8490       (unless article
8491         (error "No article on current line"))
8492       (if (not (if (or (= mark gnus-unread-mark)
8493                        (= mark gnus-ticked-mark)
8494                        (= mark gnus-dormant-mark))
8495                    (gnus-mark-article-as-unread article mark)
8496                  (gnus-mark-article-as-read article mark)))
8497           t
8498         ;; See whether the article is to be put in the cache.
8499         (and gnus-use-cache
8500              (not (= mark gnus-canceled-mark))
8501              (vectorp (gnus-summary-article-header article))
8502              (save-excursion
8503                (gnus-cache-possibly-enter-article
8504                 gnus-newsgroup-name article
8505                 (gnus-summary-article-header article)
8506                 (= mark gnus-ticked-mark)
8507                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8508
8509         (when (gnus-summary-goto-subject article nil t)
8510           (let ((buffer-read-only nil))
8511             (gnus-summary-show-thread)
8512             ;; Fix the mark.
8513             (gnus-summary-update-mark mark 'unread)
8514             t))))))
8515
8516 (defun gnus-summary-update-secondary-mark (article)
8517   "Update the secondary (read, process, cache) mark."
8518   (gnus-summary-update-mark
8519    (cond ((memq article gnus-newsgroup-processable)
8520           gnus-process-mark)
8521          ((memq article gnus-newsgroup-cached)
8522           gnus-cached-mark)
8523          ((memq article gnus-newsgroup-replied)
8524           gnus-replied-mark)
8525          ((memq article gnus-newsgroup-saved)
8526           gnus-saved-mark)
8527          (t gnus-unread-mark))
8528    'replied)
8529   (when (gnus-visual-p 'summary-highlight 'highlight)
8530     (gnus-run-hooks 'gnus-summary-update-hook))
8531   t)
8532
8533 (defun gnus-summary-update-mark (mark type)
8534   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8535         (buffer-read-only nil))
8536     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8537     (when forward
8538       (when (looking-at "\r")
8539         (incf forward))
8540       (when (<= (+ forward (point)) (point-max))
8541         ;; Go to the right position on the line.
8542         (goto-char (+ forward (point)))
8543         ;; Replace the old mark with the new mark.
8544         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8545         ;; Optionally update the marks by some user rule.
8546         (when (eq type 'unread)
8547           (gnus-data-set-mark
8548            (gnus-data-find (gnus-summary-article-number)) mark)
8549           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8550
8551 (defun gnus-mark-article-as-read (article &optional mark)
8552   "Enter ARTICLE in the pertinent lists and remove it from others."
8553   ;; Make the article expirable.
8554   (let ((mark (or mark gnus-del-mark)))
8555     (if (= mark gnus-expirable-mark)
8556         (push article gnus-newsgroup-expirable)
8557       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8558     ;; Remove from unread and marked lists.
8559     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8560     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8561     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8562     (push (cons article mark) gnus-newsgroup-reads)
8563     ;; Possibly remove from cache, if that is used.
8564     (when gnus-use-cache
8565       (gnus-cache-enter-remove-article article))
8566     t))
8567
8568 (defun gnus-mark-article-as-unread (article &optional mark)
8569   "Enter ARTICLE in the pertinent lists and remove it from others."
8570   (let ((mark (or mark gnus-ticked-mark)))
8571     (if (<= article 0)
8572         (progn
8573           (gnus-error 1 "Can't mark negative article numbers")
8574           nil)
8575       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8576             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8577             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8578             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8579
8580       ;; Unsuppress duplicates?
8581       (when gnus-suppress-duplicates
8582         (gnus-dup-unsuppress-article article))
8583
8584       (cond ((= mark gnus-ticked-mark)
8585              (push article gnus-newsgroup-marked))
8586             ((= mark gnus-dormant-mark)
8587              (push article gnus-newsgroup-dormant))
8588             (t
8589              (push article gnus-newsgroup-unreads)))
8590       (gnus-pull article gnus-newsgroup-reads)
8591       t)))
8592
8593 (defalias 'gnus-summary-mark-as-unread-forward
8594   'gnus-summary-tick-article-forward)
8595 (make-obsolete 'gnus-summary-mark-as-unread-forward
8596                'gnus-summary-tick-article-forward)
8597 (defun gnus-summary-tick-article-forward (n)
8598   "Tick N articles forwards.
8599 If N is negative, tick backwards instead.
8600 The difference between N and the number of articles ticked is returned."
8601   (interactive "p")
8602   (gnus-summary-mark-forward n gnus-ticked-mark))
8603
8604 (defalias 'gnus-summary-mark-as-unread-backward
8605   'gnus-summary-tick-article-backward)
8606 (make-obsolete 'gnus-summary-mark-as-unread-backward
8607                'gnus-summary-tick-article-backward)
8608 (defun gnus-summary-tick-article-backward (n)
8609   "Tick N articles backwards.
8610 The difference between N and the number of articles ticked is returned."
8611   (interactive "p")
8612   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8613
8614 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8615 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8616 (defun gnus-summary-tick-article (&optional article clear-mark)
8617   "Mark current article as unread.
8618 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8619 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8620   (interactive)
8621   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8622                                        gnus-ticked-mark)))
8623
8624 (defun gnus-summary-mark-as-read-forward (n)
8625   "Mark N articles as read forwards.
8626 If N is negative, mark backwards instead.
8627 The difference between N and the actual number of articles marked is
8628 returned."
8629   (interactive "p")
8630   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8631
8632 (defun gnus-summary-mark-as-read-backward (n)
8633   "Mark the N articles as read backwards.
8634 The difference between N and the actual number of articles marked is
8635 returned."
8636   (interactive "p")
8637   (gnus-summary-mark-forward
8638    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8639
8640 (defun gnus-summary-mark-as-read (&optional article mark)
8641   "Mark current article as read.
8642 ARTICLE specifies the article to be marked as read.
8643 MARK specifies a string to be inserted at the beginning of the line."
8644   (gnus-summary-mark-article article mark))
8645
8646 (defun gnus-summary-clear-mark-forward (n)
8647   "Clear marks from N articles forward.
8648 If N is negative, clear backward instead.
8649 The difference between N and the number of marks cleared is returned."
8650   (interactive "p")
8651   (gnus-summary-mark-forward n gnus-unread-mark))
8652
8653 (defun gnus-summary-clear-mark-backward (n)
8654   "Clear marks from N articles backward.
8655 The difference between N and the number of marks cleared is returned."
8656   (interactive "p")
8657   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8658
8659 (defun gnus-summary-mark-unread-as-read ()
8660   "Intended to be used by `gnus-summary-mark-article-hook'."
8661   (when (memq gnus-current-article gnus-newsgroup-unreads)
8662     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8663
8664 (defun gnus-summary-mark-read-and-unread-as-read ()
8665   "Intended to be used by `gnus-summary-mark-article-hook'."
8666   (let ((mark (gnus-summary-article-mark)))
8667     (when (or (gnus-unread-mark-p mark)
8668               (gnus-read-mark-p mark))
8669       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8670
8671 (defun gnus-summary-mark-region-as-read (point mark all)
8672   "Mark all unread articles between point and mark as read.
8673 If given a prefix, mark all articles between point and mark as read,
8674 even ticked and dormant ones."
8675   (interactive "r\nP")
8676   (save-excursion
8677     (let (article)
8678       (goto-char point)
8679       (beginning-of-line)
8680       (while (and
8681               (< (point) mark)
8682               (progn
8683                 (when (or all
8684                           (memq (setq article (gnus-summary-article-number))
8685                                 gnus-newsgroup-unreads))
8686                   (gnus-summary-mark-article article gnus-del-mark))
8687                 t)
8688               (gnus-summary-find-next))))))
8689
8690 (defun gnus-summary-mark-below (score mark)
8691   "Mark articles with score less than SCORE with MARK."
8692   (interactive "P\ncMark: ")
8693   (setq score (if score
8694                   (prefix-numeric-value score)
8695                 (or gnus-summary-default-score 0)))
8696   (save-excursion
8697     (set-buffer gnus-summary-buffer)
8698     (goto-char (point-min))
8699     (while
8700         (progn
8701           (and (< (gnus-summary-article-score) score)
8702                (gnus-summary-mark-article nil mark))
8703           (gnus-summary-find-next)))))
8704
8705 (defun gnus-summary-kill-below (&optional score)
8706   "Mark articles with score below SCORE as read."
8707   (interactive "P")
8708   (gnus-summary-mark-below score gnus-killed-mark))
8709
8710 (defun gnus-summary-clear-above (&optional score)
8711   "Clear all marks from articles with score above SCORE."
8712   (interactive "P")
8713   (gnus-summary-mark-above score gnus-unread-mark))
8714
8715 (defun gnus-summary-tick-above (&optional score)
8716   "Tick all articles with score above SCORE."
8717   (interactive "P")
8718   (gnus-summary-mark-above score gnus-ticked-mark))
8719
8720 (defun gnus-summary-mark-above (score mark)
8721   "Mark articles with score over SCORE with MARK."
8722   (interactive "P\ncMark: ")
8723   (setq score (if score
8724                   (prefix-numeric-value score)
8725                 (or gnus-summary-default-score 0)))
8726   (save-excursion
8727     (set-buffer gnus-summary-buffer)
8728     (goto-char (point-min))
8729     (while (and (progn
8730                   (when (> (gnus-summary-article-score) score)
8731                     (gnus-summary-mark-article nil mark))
8732                   t)
8733                 (gnus-summary-find-next)))))
8734
8735 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8736 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8737 (defun gnus-summary-limit-include-expunged (&optional no-error)
8738   "Display all the hidden articles that were expunged for low scores."
8739   (interactive)
8740   (let ((buffer-read-only nil))
8741     (let ((scored gnus-newsgroup-scored)
8742           headers h)
8743       (while scored
8744         (unless (gnus-summary-goto-subject (caar scored))
8745           (and (setq h (gnus-summary-article-header (caar scored)))
8746                (< (cdar scored) gnus-summary-expunge-below)
8747                (push h headers)))
8748         (setq scored (cdr scored)))
8749       (if (not headers)
8750           (when (not no-error)
8751             (error "No expunged articles hidden"))
8752         (goto-char (point-min))
8753         (gnus-summary-prepare-unthreaded (nreverse headers))
8754         (goto-char (point-min))
8755         (gnus-summary-position-point)
8756         t))))
8757
8758 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8759   "Mark all unread articles in this newsgroup as read.
8760 If prefix argument ALL is non-nil, ticked and dormant articles will
8761 also be marked as read.
8762 If QUIETLY is non-nil, no questions will be asked.
8763 If TO-HERE is non-nil, it should be a point in the buffer.  All
8764 articles before this point will be marked as read.
8765 Note that this function will only catch up the unread article
8766 in the current summary buffer limitation.
8767 The number of articles marked as read is returned."
8768   (interactive "P")
8769   (prog1
8770       (save-excursion
8771         (when (or quietly
8772                   (not gnus-interactive-catchup) ;Without confirmation?
8773                   gnus-expert-user
8774                   (gnus-y-or-n-p
8775                    (if all
8776                        "Mark absolutely all articles as read? "
8777                      "Mark all unread articles as read? ")))
8778           (if (and not-mark
8779                    (not gnus-newsgroup-adaptive)
8780                    (not gnus-newsgroup-auto-expire)
8781                    (not gnus-suppress-duplicates)
8782                    (or (not gnus-use-cache)
8783                        (eq gnus-use-cache 'passive)))
8784               (progn
8785                 (when all
8786                   (setq gnus-newsgroup-marked nil
8787                         gnus-newsgroup-dormant nil))
8788                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8789             ;; We actually mark all articles as canceled, which we
8790             ;; have to do when using auto-expiry or adaptive scoring.
8791             (gnus-summary-show-all-threads)
8792             (when (gnus-summary-first-subject (not all) t)
8793               (while (and
8794                       (if to-here (< (point) to-here) t)
8795                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8796                       (gnus-summary-find-next (not all) nil nil t))))
8797             (gnus-set-mode-line 'summary))
8798           t))
8799     (gnus-summary-position-point)))
8800
8801 (defun gnus-summary-catchup-to-here (&optional all)
8802   "Mark all unticked articles before the current one as read.
8803 If ALL is non-nil, also mark ticked and dormant articles as read."
8804   (interactive "P")
8805   (save-excursion
8806     (gnus-save-hidden-threads
8807       (let ((beg (point)))
8808         ;; We check that there are unread articles.
8809         (when (or all (gnus-summary-find-prev))
8810           (gnus-summary-catchup all t beg)))))
8811   (gnus-summary-position-point))
8812
8813 (defun gnus-summary-catchup-all (&optional quietly)
8814   "Mark all articles in this newsgroup as read."
8815   (interactive "P")
8816   (gnus-summary-catchup t quietly))
8817
8818 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8819   "Mark all unread articles in this group as read, then exit.
8820 If prefix argument ALL is non-nil, all articles are marked as read."
8821   (interactive "P")
8822   (when (gnus-summary-catchup all quietly nil 'fast)
8823     ;; Select next newsgroup or exit.
8824     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8825              (eq gnus-auto-select-next 'quietly))
8826         (gnus-summary-next-group nil)
8827       (gnus-summary-exit))))
8828
8829 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8830   "Mark all articles in this newsgroup as read, and then exit."
8831   (interactive "P")
8832   (gnus-summary-catchup-and-exit t quietly))
8833
8834 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8835   "Mark all articles in this group as read and select the next group.
8836 If given a prefix, mark all articles, unread as well as ticked, as
8837 read."
8838   (interactive "P")
8839   (save-excursion
8840     (gnus-summary-catchup all))
8841   (gnus-summary-next-group))
8842
8843 ;;;
8844 ;;; with article
8845 ;;;
8846
8847 (defmacro gnus-with-article (article &rest forms)
8848   "Select ARTICLE and perform FORMS in the original article buffer.
8849 Then replace the article with the result."
8850   `(progn
8851      ;; We don't want the article to be marked as read.
8852      (let (gnus-mark-article-hook)
8853        (gnus-summary-select-article t t nil ,article))
8854      (set-buffer gnus-original-article-buffer)
8855      ,@forms
8856      (if (not (gnus-check-backend-function
8857                'request-replace-article (car gnus-article-current)))
8858          (gnus-message 5 "Read-only group; not replacing")
8859        (unless (gnus-request-replace-article
8860                 ,article (car gnus-article-current)
8861                 (current-buffer) t)
8862          (error "Couldn't replace article")))
8863      ;; The cache and backlog have to be flushed somewhat.
8864      (when gnus-keep-backlog
8865        (gnus-backlog-remove-article
8866         (car gnus-article-current) (cdr gnus-article-current)))
8867      (when gnus-use-cache
8868        (gnus-cache-update-article
8869         (car gnus-article-current) (cdr gnus-article-current)))))
8870
8871 (put 'gnus-with-article 'lisp-indent-function 1)
8872 (put 'gnus-with-article 'edebug-form-spec '(form body))
8873
8874 ;; Thread-based commands.
8875
8876 (defun gnus-summary-articles-in-thread (&optional article)
8877   "Return a list of all articles in the current thread.
8878 If ARTICLE is non-nil, return all articles in the thread that starts
8879 with that article."
8880   (let* ((article (or article (gnus-summary-article-number)))
8881          (data (gnus-data-find-list article))
8882          (top-level (gnus-data-level (car data)))
8883          (top-subject
8884           (cond ((null gnus-thread-operation-ignore-subject)
8885                  (gnus-simplify-subject-re
8886                   (mail-header-subject (gnus-data-header (car data)))))
8887                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8888                  (gnus-simplify-subject-fuzzy
8889                   (mail-header-subject (gnus-data-header (car data)))))
8890                 (t nil)))
8891          (end-point (save-excursion
8892                       (if (gnus-summary-go-to-next-thread)
8893                           (point) (point-max))))
8894          articles)
8895     (while (and data
8896                 (< (gnus-data-pos (car data)) end-point))
8897       (when (or (not top-subject)
8898                 (string= top-subject
8899                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8900                              (gnus-simplify-subject-fuzzy
8901                               (mail-header-subject
8902                                (gnus-data-header (car data))))
8903                            (gnus-simplify-subject-re
8904                             (mail-header-subject
8905                              (gnus-data-header (car data)))))))
8906         (push (gnus-data-number (car data)) articles))
8907       (unless (and (setq data (cdr data))
8908                    (> (gnus-data-level (car data)) top-level))
8909         (setq data nil)))
8910     ;; Return the list of articles.
8911     (nreverse articles)))
8912
8913 (defun gnus-summary-rethread-current ()
8914   "Rethread the thread the current article is part of."
8915   (interactive)
8916   (let* ((gnus-show-threads t)
8917          (article (gnus-summary-article-number))
8918          (id (mail-header-id (gnus-summary-article-header)))
8919          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8920     (unless id
8921       (error "No article on the current line"))
8922     (gnus-rebuild-thread id)
8923     (gnus-summary-goto-subject article)))
8924
8925 (defun gnus-summary-reparent-thread ()
8926   "Make the current article child of the marked (or previous) article.
8927
8928 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8929 is non-nil or the Subject: of both articles are the same."
8930   (interactive)
8931   (unless (not (gnus-group-read-only-p))
8932     (error "The current newsgroup does not support article editing"))
8933   (unless (<= (length gnus-newsgroup-processable) 1)
8934     (error "No more than one article may be marked"))
8935   (save-window-excursion
8936     (let ((gnus-article-buffer " *reparent*")
8937           (current-article (gnus-summary-article-number))
8938           ;; First grab the marked article, otherwise one line up.
8939           (parent-article (if (not (null gnus-newsgroup-processable))
8940                               (car gnus-newsgroup-processable)
8941                             (save-excursion
8942                               (if (eq (forward-line -1) 0)
8943                                   (gnus-summary-article-number)
8944                                 (error "Beginning of summary buffer"))))))
8945       (unless (not (eq current-article parent-article))
8946         (error "An article may not be self-referential"))
8947       (let ((message-id (mail-header-id
8948                          (gnus-summary-article-header parent-article))))
8949         (unless (and message-id (not (equal message-id "")))
8950           (error "No message-id in desired parent"))
8951         (gnus-with-article current-article
8952           (save-restriction
8953             (goto-char (point-min))
8954             (message-narrow-to-head)
8955             (if (re-search-forward "^References: " nil t)
8956                 (progn
8957                   (re-search-forward "^[^ \t]" nil t)
8958                   (forward-line -1)
8959                   (end-of-line)
8960                   (insert " " message-id))
8961               (insert "References: " message-id "\n"))))
8962         (set-buffer gnus-summary-buffer)
8963         (gnus-summary-unmark-all-processable)
8964         (gnus-summary-update-article current-article)
8965         (gnus-summary-rethread-current)
8966         (gnus-message 3 "Article %d is now the child of article %d"
8967                       current-article parent-article)))))
8968
8969 (defun gnus-summary-toggle-threads (&optional arg)
8970   "Toggle showing conversation threads.
8971 If ARG is positive number, turn showing conversation threads on."
8972   (interactive "P")
8973   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8974     (setq gnus-show-threads
8975           (if (null arg) (not gnus-show-threads)
8976             (> (prefix-numeric-value arg) 0)))
8977     (gnus-summary-prepare)
8978     (gnus-summary-goto-subject current)
8979     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8980     (gnus-summary-position-point)))
8981
8982 (defun gnus-summary-show-all-threads ()
8983   "Show all threads."
8984   (interactive)
8985   (save-excursion
8986     (let ((buffer-read-only nil))
8987       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8988   (gnus-summary-position-point))
8989
8990 (defun gnus-summary-show-thread ()
8991   "Show thread subtrees.
8992 Returns nil if no thread was there to be shown."
8993   (interactive)
8994   (let ((buffer-read-only nil)
8995         (orig (point))
8996         ;; first goto end then to beg, to have point at beg after let
8997         (end (progn (end-of-line) (point)))
8998         (beg (progn (beginning-of-line) (point))))
8999     (prog1
9000         ;; Any hidden lines here?
9001         (search-forward "\r" end t)
9002       (subst-char-in-region beg end ?\^M ?\n t)
9003       (goto-char orig)
9004       (gnus-summary-position-point))))
9005
9006 (defun gnus-summary-hide-all-threads ()
9007   "Hide all thread subtrees."
9008   (interactive)
9009   (save-excursion
9010     (goto-char (point-min))
9011     (gnus-summary-hide-thread)
9012     (while (zerop (gnus-summary-next-thread 1 t))
9013       (gnus-summary-hide-thread)))
9014   (gnus-summary-position-point))
9015
9016 (defun gnus-summary-hide-thread ()
9017   "Hide thread subtrees.
9018 Returns nil if no threads were there to be hidden."
9019   (interactive)
9020   (let ((buffer-read-only nil)
9021         (start (point))
9022         (article (gnus-summary-article-number)))
9023     (goto-char start)
9024     ;; Go forward until either the buffer ends or the subthread
9025     ;; ends.
9026     (when (and (not (eobp))
9027                (or (zerop (gnus-summary-next-thread 1 t))
9028                    (goto-char (point-max))))
9029       (prog1
9030           (if (and (> (point) start)
9031                    (search-backward "\n" start t))
9032               (progn
9033                 (subst-char-in-region start (point) ?\n ?\^M)
9034                 (gnus-summary-goto-subject article))
9035             (goto-char start)
9036             nil)))))
9037
9038 (defun gnus-summary-go-to-next-thread (&optional previous)
9039   "Go to the same level (or less) next thread.
9040 If PREVIOUS is non-nil, go to previous thread instead.
9041 Return the article number moved to, or nil if moving was impossible."
9042   (let ((level (gnus-summary-thread-level))
9043         (way (if previous -1 1))
9044         (beg (point)))
9045     (forward-line way)
9046     (while (and (not (eobp))
9047                 (< level (gnus-summary-thread-level)))
9048       (forward-line way))
9049     (if (eobp)
9050         (progn
9051           (goto-char beg)
9052           nil)
9053       (setq beg (point))
9054       (prog1
9055           (gnus-summary-article-number)
9056         (goto-char beg)))))
9057
9058 (defun gnus-summary-next-thread (n &optional silent)
9059   "Go to the same level next N'th thread.
9060 If N is negative, search backward instead.
9061 Returns the difference between N and the number of skips actually
9062 done.
9063
9064 If SILENT, don't output messages."
9065   (interactive "p")
9066   (let ((backward (< n 0))
9067         (n (abs n)))
9068     (while (and (> n 0)
9069                 (gnus-summary-go-to-next-thread backward))
9070       (decf n))
9071     (unless silent
9072       (gnus-summary-position-point))
9073     (when (and (not silent) (/= 0 n))
9074       (gnus-message 7 "No more threads"))
9075     n))
9076
9077 (defun gnus-summary-prev-thread (n)
9078   "Go to the same level previous N'th thread.
9079 Returns the difference between N and the number of skips actually
9080 done."
9081   (interactive "p")
9082   (gnus-summary-next-thread (- n)))
9083
9084 (defun gnus-summary-go-down-thread ()
9085   "Go down one level in the current thread."
9086   (let ((children (gnus-summary-article-children)))
9087     (when children
9088       (gnus-summary-goto-subject (car children)))))
9089
9090 (defun gnus-summary-go-up-thread ()
9091   "Go up one level in the current thread."
9092   (let ((parent (gnus-summary-article-parent)))
9093     (when parent
9094       (gnus-summary-goto-subject parent))))
9095
9096 (defun gnus-summary-down-thread (n)
9097   "Go down thread N steps.
9098 If N is negative, go up instead.
9099 Returns the difference between N and how many steps down that were
9100 taken."
9101   (interactive "p")
9102   (let ((up (< n 0))
9103         (n (abs n)))
9104     (while (and (> n 0)
9105                 (if up (gnus-summary-go-up-thread)
9106                   (gnus-summary-go-down-thread)))
9107       (setq n (1- n)))
9108     (gnus-summary-position-point)
9109     (when (/= 0 n)
9110       (gnus-message 7 "Can't go further"))
9111     n))
9112
9113 (defun gnus-summary-up-thread (n)
9114   "Go up thread N steps.
9115 If N is negative, go up instead.
9116 Returns the difference between N and how many steps down that were
9117 taken."
9118   (interactive "p")
9119   (gnus-summary-down-thread (- n)))
9120
9121 (defun gnus-summary-top-thread ()
9122   "Go to the top of the thread."
9123   (interactive)
9124   (while (gnus-summary-go-up-thread))
9125   (gnus-summary-article-number))
9126
9127 (defun gnus-summary-kill-thread (&optional unmark)
9128   "Mark articles under current thread as read.
9129 If the prefix argument is positive, remove any kinds of marks.
9130 If the prefix argument is negative, tick articles instead."
9131   (interactive "P")
9132   (when unmark
9133     (setq unmark (prefix-numeric-value unmark)))
9134   (let ((articles (gnus-summary-articles-in-thread)))
9135     (save-excursion
9136       ;; Expand the thread.
9137       (gnus-summary-show-thread)
9138       ;; Mark all the articles.
9139       (while articles
9140         (gnus-summary-goto-subject (car articles))
9141         (cond ((null unmark)
9142                (gnus-summary-mark-article-as-read gnus-killed-mark))
9143               ((> unmark 0)
9144                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9145               (t
9146                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9147         (setq articles (cdr articles))))
9148     ;; Hide killed subtrees.
9149     (and (null unmark)
9150          gnus-thread-hide-killed
9151          (gnus-summary-hide-thread))
9152     ;; If marked as read, go to next unread subject.
9153     (when (null unmark)
9154       ;; Go to next unread subject.
9155       (gnus-summary-next-subject 1 t)))
9156   (gnus-set-mode-line 'summary))
9157
9158 ;; Summary sorting commands
9159
9160 (defun gnus-summary-sort-by-number (&optional reverse)
9161   "Sort the summary buffer by article number.
9162 Argument REVERSE means reverse order."
9163   (interactive "P")
9164   (gnus-summary-sort 'number reverse))
9165
9166 (defun gnus-summary-sort-by-author (&optional reverse)
9167   "Sort the summary buffer by author name alphabetically.
9168 If `case-fold-search' is non-nil, case of letters is ignored.
9169 Argument REVERSE means reverse order."
9170   (interactive "P")
9171   (gnus-summary-sort 'author reverse))
9172
9173 (defun gnus-summary-sort-by-subject (&optional reverse)
9174   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9175 If `case-fold-search' is non-nil, case of letters is ignored.
9176 Argument REVERSE means reverse order."
9177   (interactive "P")
9178   (gnus-summary-sort 'subject reverse))
9179
9180 (defun gnus-summary-sort-by-date (&optional reverse)
9181   "Sort the summary buffer by date.
9182 Argument REVERSE means reverse order."
9183   (interactive "P")
9184   (gnus-summary-sort 'date reverse))
9185
9186 (defun gnus-summary-sort-by-score (&optional reverse)
9187   "Sort the summary buffer by score.
9188 Argument REVERSE means reverse order."
9189   (interactive "P")
9190   (gnus-summary-sort 'score reverse))
9191
9192 (defun gnus-summary-sort-by-lines (&optional reverse)
9193   "Sort the summary buffer by the number of lines.
9194 Argument REVERSE means reverse order."
9195   (interactive "P")
9196   (gnus-summary-sort 'lines reverse))
9197
9198 (defun gnus-summary-sort-by-chars (&optional reverse)
9199   "Sort the summary buffer by article length.
9200 Argument REVERSE means reverse order."
9201   (interactive "P")
9202   (gnus-summary-sort 'chars reverse))
9203
9204 (defun gnus-summary-sort (predicate reverse)
9205   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9206   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9207          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9208          (gnus-thread-sort-functions
9209           (if (not reverse)
9210               thread
9211             `(lambda (t1 t2)
9212                (,thread t2 t1))))
9213          (gnus-sort-gathered-threads-function
9214           gnus-thread-sort-functions)
9215          (gnus-article-sort-functions
9216           (if (not reverse)
9217               article
9218             `(lambda (t1 t2)
9219                (,article t2 t1))))
9220          (buffer-read-only)
9221          (gnus-summary-prepare-hook nil))
9222     ;; We do the sorting by regenerating the threads.
9223     (gnus-summary-prepare)
9224     ;; Hide subthreads if needed.
9225     (when (and gnus-show-threads gnus-thread-hide-subtree)
9226       (gnus-summary-hide-all-threads))))
9227
9228 ;; Summary saving commands.
9229
9230 (defun gnus-summary-save-article (&optional n not-saved)
9231   "Save the current article using the default saver function.
9232 If N is a positive number, save the N next articles.
9233 If N is a negative number, save the N previous articles.
9234 If N is nil and any articles have been marked with the process mark,
9235 save those articles instead.
9236 The variable `gnus-default-article-saver' specifies the saver function."
9237   (interactive "P")
9238   (let* ((articles (gnus-summary-work-articles n))
9239          (save-buffer (save-excursion
9240                         (nnheader-set-temp-buffer " *Gnus Save*")))
9241          (num (length articles))
9242          header file)
9243     (dolist (article articles)
9244       (setq header (gnus-summary-article-header article))
9245       (if (not (vectorp header))
9246           ;; This is a pseudo-article.
9247           (if (assq 'name header)
9248               (gnus-copy-file (cdr (assq 'name header)))
9249             (gnus-message 1 "Article %d is unsaveable" article))
9250         ;; This is a real article.
9251         (save-window-excursion
9252           (gnus-summary-select-article t nil nil article))
9253         (save-excursion
9254           (set-buffer save-buffer)
9255           (erase-buffer)
9256           (insert-buffer-substring gnus-original-article-buffer))
9257         (setq file (gnus-article-save save-buffer file num))
9258         (gnus-summary-remove-process-mark article)
9259         (unless not-saved
9260           (gnus-summary-set-saved-mark article))))
9261     (gnus-kill-buffer save-buffer)
9262     (gnus-summary-position-point)
9263     (gnus-set-mode-line 'summary)
9264     n))
9265
9266 (defun gnus-summary-pipe-output (&optional arg)
9267   "Pipe the current article to a subprocess.
9268 If N is a positive number, pipe the N next articles.
9269 If N is a negative number, pipe the N previous articles.
9270 If N is nil and any articles have been marked with the process mark,
9271 pipe those articles instead."
9272   (interactive "P")
9273   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9274     (gnus-summary-save-article arg t))
9275   (gnus-configure-windows 'pipe))
9276
9277 (defun gnus-summary-save-article-mail (&optional arg)
9278   "Append the current article to an mail file.
9279 If N is a positive number, save the N next articles.
9280 If N is a negative number, save the N previous articles.
9281 If N is nil and any articles have been marked with the process mark,
9282 save those articles instead."
9283   (interactive "P")
9284   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9285     (gnus-summary-save-article arg)))
9286
9287 (defun gnus-summary-save-article-rmail (&optional arg)
9288   "Append the current article to an rmail file.
9289 If N is a positive number, save the N next articles.
9290 If N is a negative number, save the N previous articles.
9291 If N is nil and any articles have been marked with the process mark,
9292 save those articles instead."
9293   (interactive "P")
9294   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9295     (gnus-summary-save-article arg)))
9296
9297 (defun gnus-summary-save-article-file (&optional arg)
9298   "Append the current article to a file.
9299 If N is a positive number, save the N next articles.
9300 If N is a negative number, save the N previous articles.
9301 If N is nil and any articles have been marked with the process mark,
9302 save those articles instead."
9303   (interactive "P")
9304   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9305     (gnus-summary-save-article arg)))
9306
9307 (defun gnus-summary-write-article-file (&optional arg)
9308   "Write the current article to a file, deleting the previous file.
9309 If N is a positive number, save the N next articles.
9310 If N is a negative number, save the N previous articles.
9311 If N is nil and any articles have been marked with the process mark,
9312 save those articles instead."
9313   (interactive "P")
9314   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9315     (gnus-summary-save-article arg)))
9316
9317 (defun gnus-summary-save-article-body-file (&optional arg)
9318   "Append the current article body to a file.
9319 If N is a positive number, save the N next articles.
9320 If N is a negative number, save the N previous articles.
9321 If N is nil and any articles have been marked with the process mark,
9322 save those articles instead."
9323   (interactive "P")
9324   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9325     (gnus-summary-save-article arg)))
9326
9327 (defun gnus-summary-pipe-message (program)
9328   "Pipe the current article through PROGRAM."
9329   (interactive "sProgram: ")
9330   (gnus-summary-select-article)
9331   (let ((mail-header-separator ""))
9332     (gnus-eval-in-buffer-window gnus-article-buffer
9333       (save-restriction
9334         (widen)
9335         (let ((start (window-start))
9336               buffer-read-only)
9337           (message-pipe-buffer-body program)
9338           (set-window-start (get-buffer-window (current-buffer)) start))))))
9339
9340 (defun gnus-get-split-value (methods)
9341   "Return a value based on the split METHODS."
9342   (let (split-name method result match)
9343     (when methods
9344       (save-excursion
9345         (set-buffer gnus-original-article-buffer)
9346         (save-restriction
9347           (nnheader-narrow-to-headers)
9348           (while (and methods (not split-name))
9349             (goto-char (point-min))
9350             (setq method (pop methods))
9351             (setq match (car method))
9352             (when (cond
9353                    ((stringp match)
9354                     ;; Regular expression.
9355                     (ignore-errors
9356                       (re-search-forward match nil t)))
9357                    ((gnus-functionp match)
9358                     ;; Function.
9359                     (save-restriction
9360                       (widen)
9361                       (setq result (funcall match gnus-newsgroup-name))))
9362                    ((consp match)
9363                     ;; Form.
9364                     (save-restriction
9365                       (widen)
9366                       (setq result (eval match)))))
9367               (setq split-name (cdr method))
9368               (cond ((stringp result)
9369                      (push (expand-file-name
9370                             result gnus-article-save-directory)
9371                            split-name))
9372                     ((consp result)
9373                      (setq split-name (append result split-name)))))))))
9374     (nreverse split-name)))
9375
9376 (defun gnus-valid-move-group-p (group)
9377   (and (boundp group)
9378        (symbol-name group)
9379        (symbol-value group)
9380        (gnus-get-function (gnus-find-method-for-group
9381                            (symbol-name group)) 'request-accept-article t)))
9382
9383 (defun gnus-read-move-group-name (prompt default articles prefix)
9384   "Read a group name."
9385   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9386          (minibuffer-confirm-incomplete nil) ; XEmacs
9387          (prom
9388           (format "%s %s to:"
9389                   prompt
9390                   (if (> (length articles) 1)
9391                       (format "these %d articles" (length articles))
9392                     "this article")))
9393          (to-newsgroup
9394           (cond
9395            ((null split-name)
9396             (gnus-completing-read default prom
9397                                   gnus-active-hashtb
9398                                   'gnus-valid-move-group-p
9399                                   nil prefix
9400                                   'gnus-group-history))
9401            ((= 1 (length split-name))
9402             (gnus-completing-read (car split-name) prom
9403                                   gnus-active-hashtb
9404                                   'gnus-valid-move-group-p
9405                                   nil nil
9406                                   'gnus-group-history))
9407            (t
9408             (gnus-completing-read nil prom
9409                                   (mapcar (lambda (el) (list el))
9410                                           (nreverse split-name))
9411                                   nil nil nil
9412                                   'gnus-group-history))))
9413          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9414     (when to-newsgroup
9415       (if (or (string= to-newsgroup "")
9416               (string= to-newsgroup prefix))
9417           (setq to-newsgroup default))
9418       (unless to-newsgroup
9419         (error "No group name entered"))
9420       (or (gnus-active to-newsgroup)
9421           (gnus-activate-group to-newsgroup nil nil to-method)
9422           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9423                                      to-newsgroup))
9424               (or (and (gnus-request-create-group to-newsgroup to-method)
9425                        (gnus-activate-group
9426                         to-newsgroup nil nil to-method)
9427                        (gnus-subscribe-group to-newsgroup))
9428                   (error "Couldn't create group %s" to-newsgroup)))
9429           (error "No such group: %s" to-newsgroup)))
9430     to-newsgroup))
9431
9432 (defun gnus-summary-save-parts (type dir n &optional reverse)
9433   "Save parts matching TYPE to DIR.
9434 If REVERSE, save parts that do not match TYPE."
9435   (interactive
9436    (list (read-string "Save parts of type: " 
9437                       (or (car gnus-summary-save-parts-type-history)
9438                           gnus-summary-save-parts-default-mime)
9439                       'gnus-summary-save-parts-type-history)
9440          (setq gnus-summary-save-parts-last-directory
9441                (read-file-name "Save to directory: " 
9442                                gnus-summary-save-parts-last-directory
9443                                nil t))
9444          current-prefix-arg))
9445   (gnus-summary-iterate n
9446     (let ((gnus-display-mime-function nil)
9447           (gnus-inhibit-treatment t))
9448       (gnus-summary-select-article))
9449     (save-excursion
9450       (set-buffer gnus-article-buffer)
9451       (let ((handles (or gnus-article-mime-handles
9452                          (mm-dissect-buffer) (mm-uu-dissect))))
9453         (when handles
9454           (gnus-summary-save-parts-1 type dir handles reverse)
9455           (unless gnus-article-mime-handles ;; Don't destroy this case.
9456             (mm-destroy-parts handles)))))))
9457
9458 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9459   (if (stringp (car handle))
9460       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9461               (cdr handle))
9462     (when (if reverse
9463               (not (string-match type (mm-handle-media-type handle)))
9464             (string-match type (mm-handle-media-type handle)))
9465       (let ((file (expand-file-name
9466                    (file-name-nondirectory
9467                     (or
9468                      (mail-content-type-get
9469                       (mm-handle-disposition handle) 'filename)
9470                      (concat gnus-newsgroup-name
9471                              "." (number-to-string
9472                                   (cdr gnus-article-current)))))
9473                    dir)))
9474         (unless (file-exists-p file)
9475           (mm-save-part-to-file handle file))))))
9476
9477 ;; Summary extract commands
9478
9479 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9480   (let ((buffer-read-only nil)
9481         (article (gnus-summary-article-number))
9482         after-article b e)
9483     (unless (gnus-summary-goto-subject article)
9484       (error "No such article: %d" article))
9485     (gnus-summary-position-point)
9486     ;; If all commands are to be bunched up on one line, we collect
9487     ;; them here.
9488     (unless gnus-view-pseudos-separately
9489       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9490             files action)
9491         (while ps
9492           (setq action (cdr (assq 'action (car ps))))
9493           (setq files (list (cdr (assq 'name (car ps)))))
9494           (while (and ps (cdr ps)
9495                       (string= (or action "1")
9496                                (or (cdr (assq 'action (cadr ps))) "2")))
9497             (push (cdr (assq 'name (cadr ps))) files)
9498             (setcdr ps (cddr ps)))
9499           (when files
9500             (when (not (string-match "%s" action))
9501               (push " " files))
9502             (push " " files)
9503             (when (assq 'execute (car ps))
9504               (setcdr (assq 'execute (car ps))
9505                       (funcall (if (string-match "%s" action)
9506                                    'format 'concat)
9507                                action
9508                                (mapconcat
9509                                 (lambda (f)
9510                                   (if (equal f " ")
9511                                       f
9512                                     (gnus-quote-arg-for-sh-or-csh f)))
9513                                 files " ")))))
9514           (setq ps (cdr ps)))))
9515     (if (and gnus-view-pseudos (not not-view))
9516         (while pslist
9517           (when (assq 'execute (car pslist))
9518             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9519                                   (eq gnus-view-pseudos 'not-confirm)))
9520           (setq pslist (cdr pslist)))
9521       (save-excursion
9522         (while pslist
9523           (setq after-article (or (cdr (assq 'article (car pslist)))
9524                                   (gnus-summary-article-number)))
9525           (gnus-summary-goto-subject after-article)
9526           (forward-line 1)
9527           (setq b (point))
9528           (insert "    " (file-name-nondirectory
9529                           (cdr (assq 'name (car pslist))))
9530                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9531           (setq e (point))
9532           (forward-line -1)             ; back to `b'
9533           (gnus-add-text-properties
9534            b (1- e) (list 'gnus-number gnus-reffed-article-number
9535                           gnus-mouse-face-prop gnus-mouse-face))
9536           (gnus-data-enter
9537            after-article gnus-reffed-article-number
9538            gnus-unread-mark b (car pslist) 0 (- e b))
9539           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9540           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9541           (setq pslist (cdr pslist)))))))
9542
9543 (defun gnus-pseudos< (p1 p2)
9544   (let ((c1 (cdr (assq 'action p1)))
9545         (c2 (cdr (assq 'action p2))))
9546     (and c1 c2 (string< c1 c2))))
9547
9548 (defun gnus-request-pseudo-article (props)
9549   (cond ((assq 'execute props)
9550          (gnus-execute-command (cdr (assq 'execute props)))))
9551   (let ((gnus-current-article (gnus-summary-article-number)))
9552     (gnus-run-hooks 'gnus-mark-article-hook)))
9553
9554 (defun gnus-execute-command (command &optional automatic)
9555   (save-excursion
9556     (gnus-article-setup-buffer)
9557     (set-buffer gnus-article-buffer)
9558     (setq buffer-read-only nil)
9559     (let ((command (if automatic command
9560                      (read-string "Command: " (cons command 0)))))
9561       (erase-buffer)
9562       (insert "$ " command "\n\n")
9563       (if gnus-view-pseudo-asynchronously
9564           (start-process "gnus-execute" (current-buffer) shell-file-name
9565                          shell-command-switch command)
9566         (call-process shell-file-name nil t nil
9567                       shell-command-switch command)))))
9568
9569 ;; Summary kill commands.
9570
9571 (defun gnus-summary-edit-global-kill (article)
9572   "Edit the \"global\" kill file."
9573   (interactive (list (gnus-summary-article-number)))
9574   (gnus-group-edit-global-kill article))
9575
9576 (defun gnus-summary-edit-local-kill ()
9577   "Edit a local kill file applied to the current newsgroup."
9578   (interactive)
9579   (setq gnus-current-headers (gnus-summary-article-header))
9580   (gnus-group-edit-local-kill
9581    (gnus-summary-article-number) gnus-newsgroup-name))
9582
9583 ;;; Header reading.
9584
9585 (defun gnus-read-header (id &optional header)
9586   "Read the headers of article ID and enter them into the Gnus system."
9587   (let ((group gnus-newsgroup-name)
9588         (gnus-override-method
9589          (or
9590           gnus-override-method
9591           (and (gnus-news-group-p gnus-newsgroup-name)
9592                (car (gnus-refer-article-methods)))))
9593         where)
9594     ;; First we check to see whether the header in question is already
9595     ;; fetched.
9596     (if (stringp id)
9597         ;; This is a Message-ID.
9598         (setq header (or header (gnus-id-to-header id)))
9599       ;; This is an article number.
9600       (setq header (or header (gnus-summary-article-header id))))
9601     (if (and header
9602              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9603         ;; We have found the header.
9604         header
9605       ;; If this is a sparse article, we have to nix out its
9606       ;; previous entry in the thread hashtb.
9607       (when (and header
9608                  (gnus-summary-article-sparse-p (mail-header-number header)))
9609         (let* ((parent (gnus-parent-id (mail-header-references header)))
9610                (thread (and parent (gnus-id-to-thread parent))))
9611           (when thread
9612             (delq (assq header thread) thread))))
9613       ;; We have to really fetch the header to this article.
9614       (save-excursion
9615         (set-buffer nntp-server-buffer)
9616         (when (setq where (gnus-request-head id group))
9617           (nnheader-fold-continuation-lines)
9618           (goto-char (point-max))
9619           (insert ".\n")
9620           (goto-char (point-min))
9621           (insert "211 ")
9622           (princ (cond
9623                   ((numberp id) id)
9624                   ((cdr where) (cdr where))
9625                   (header (mail-header-number header))
9626                   (t gnus-reffed-article-number))
9627                  (current-buffer))
9628           (insert " Article retrieved.\n"))
9629         (if (or (not where)
9630                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9631             ()                          ; Malformed head.
9632           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9633             (when (and (stringp id)
9634                        (not (string= (gnus-group-real-name group)
9635                                      (car where))))
9636               ;; If we fetched by Message-ID and the article came
9637               ;; from a different group, we fudge some bogus article
9638               ;; numbers for this article.
9639               (mail-header-set-number header gnus-reffed-article-number))
9640             (save-excursion
9641               (set-buffer gnus-summary-buffer)
9642               (decf gnus-reffed-article-number)
9643               (gnus-remove-header (mail-header-number header))
9644               (push header gnus-newsgroup-headers)
9645               (setq gnus-current-headers header)
9646               (push (mail-header-number header) gnus-newsgroup-limit)))
9647           header)))))
9648
9649 (defun gnus-remove-header (number)
9650   "Remove header NUMBER from `gnus-newsgroup-headers'."
9651   (if (and gnus-newsgroup-headers
9652            (= number (mail-header-number (car gnus-newsgroup-headers))))
9653       (pop gnus-newsgroup-headers)
9654     (let ((headers gnus-newsgroup-headers))
9655       (while (and (cdr headers)
9656                   (not (= number (mail-header-number (cadr headers)))))
9657         (pop headers))
9658       (when (cdr headers)
9659         (setcdr headers (cddr headers))))))
9660
9661 ;;;
9662 ;;; summary highlights
9663 ;;;
9664
9665 (defun gnus-highlight-selected-summary ()
9666   "Highlight selected article in summary buffer."
9667   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9668   (when gnus-summary-selected-face
9669     (save-excursion
9670       (let* ((beg (progn (beginning-of-line) (point)))
9671              (end (progn (end-of-line) (point)))
9672              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9673              (from (if (get-text-property beg gnus-mouse-face-prop)
9674                        beg
9675                      (or (next-single-property-change
9676                           beg gnus-mouse-face-prop nil end)
9677                          beg)))
9678              (to
9679               (if (= from end)
9680                   (- from 2)
9681                 (or (next-single-property-change
9682                      from gnus-mouse-face-prop nil end)
9683                     end))))
9684         ;; If no mouse-face prop on line we will have to = from = end,
9685         ;; so we highlight the entire line instead.
9686         (when (= (+ to 2) from)
9687           (setq from beg)
9688           (setq to end))
9689         (if gnus-newsgroup-selected-overlay
9690             ;; Move old overlay.
9691             (gnus-move-overlay
9692              gnus-newsgroup-selected-overlay from to (current-buffer))
9693           ;; Create new overlay.
9694           (gnus-overlay-put
9695            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9696            'face gnus-summary-selected-face))))))
9697
9698 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9699 (defun gnus-summary-highlight-line ()
9700   "Highlight current line according to `gnus-summary-highlight'."
9701   (let* ((list gnus-summary-highlight)
9702          (p (point))
9703          (end (progn (end-of-line) (point)))
9704          ;; now find out where the line starts and leave point there.
9705          (beg (progn (beginning-of-line) (point)))
9706          (article (gnus-summary-article-number))
9707          (score (or (cdr (assq (or article gnus-current-article)
9708                                gnus-newsgroup-scored))
9709                     gnus-summary-default-score 0))
9710          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9711          (inhibit-read-only t))
9712     ;; Eval the cars of the lists until we find a match.
9713     (let ((default gnus-summary-default-score))
9714       (while (and list
9715                   (not (eval (caar list))))
9716         (setq list (cdr list))))
9717     (let ((face (cdar list)))
9718       (unless (eq face (get-text-property beg 'face))
9719         (gnus-put-text-property-excluding-characters-with-faces
9720          beg end 'face
9721          (setq face (if (boundp face) (symbol-value face) face)))
9722         (when gnus-summary-highlight-line-function
9723           (funcall gnus-summary-highlight-line-function article face))))
9724     (goto-char p)))
9725
9726 (defun gnus-update-read-articles (group unread &optional compute)
9727   "Update the list of read articles in GROUP."
9728   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9729          (entry (gnus-gethash group gnus-newsrc-hashtb))
9730          (info (nth 2 entry))
9731          (prev 1)
9732          (unread (sort (copy-sequence unread) '<))
9733          read)
9734     (if (or (not info) (not active))
9735         ;; There is no info on this group if it was, in fact,
9736         ;; killed.  Gnus stores no information on killed groups, so
9737         ;; there's nothing to be done.
9738         ;; One could store the information somewhere temporarily,
9739         ;; perhaps...  Hmmm...
9740         ()
9741       ;; Remove any negative articles numbers.
9742       (while (and unread (< (car unread) 0))
9743         (setq unread (cdr unread)))
9744       ;; Remove any expired article numbers
9745       (while (and unread (< (car unread) (car active)))
9746         (setq unread (cdr unread)))
9747       ;; Compute the ranges of read articles by looking at the list of
9748       ;; unread articles.
9749       (while unread
9750         (when (/= (car unread) prev)
9751           (push (if (= prev (1- (car unread))) prev
9752                   (cons prev (1- (car unread))))
9753                 read))
9754         (setq prev (1+ (car unread)))
9755         (setq unread (cdr unread)))
9756       (when (<= prev (cdr active))
9757         (push (cons prev (cdr active)) read))
9758       (setq read (if (> (length read) 1) (nreverse read) read))
9759       (if compute
9760           read
9761         (save-excursion
9762           (let (setmarkundo)
9763             ;; Propagate the read marks to the backend.
9764             (when (gnus-check-backend-function 'request-set-mark group)
9765               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9766                     (add (gnus-remove-from-range read (gnus-info-read info))))
9767                 (when (or add del)
9768                   (unless (gnus-check-group group)
9769                     (error "Can't open server for %s" group))
9770                   (gnus-request-set-mark
9771                    group (delq nil (list (if add (list add 'add '(read)))
9772                                          (if del (list del 'del '(read))))))
9773                   (setq setmarkundo
9774                         `(gnus-request-set-mark
9775                           ,group
9776                           ',(delq nil (list
9777                                        (if del (list del 'add '(read)))
9778                                        (if add (list add 'del '(read))))))))))
9779             (set-buffer gnus-group-buffer)
9780             (gnus-undo-register
9781               `(progn
9782                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9783                  (gnus-info-set-read ',info ',(gnus-info-read info))
9784                  (gnus-get-unread-articles-in-group ',info 
9785                                                     (gnus-active ,group))
9786                  (gnus-group-update-group ,group t)
9787                  ,setmarkundo))))
9788         ;; Enter this list into the group info.
9789         (gnus-info-set-read info read)
9790         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9791         (gnus-get-unread-articles-in-group info (gnus-active group))
9792         t))))
9793
9794 (defun gnus-offer-save-summaries ()
9795   "Offer to save all active summary buffers."
9796   (save-excursion
9797     (let ((buflist (buffer-list))
9798           buffers bufname)
9799       ;; Go through all buffers and find all summaries.
9800       (while buflist
9801         (and (setq bufname (buffer-name (car buflist)))
9802              (string-match "Summary" bufname)
9803              (save-excursion
9804                (set-buffer bufname)
9805                ;; We check that this is, indeed, a summary buffer.
9806                (and (eq major-mode 'gnus-summary-mode)
9807                     ;; Also make sure this isn't bogus.
9808                     gnus-newsgroup-prepared
9809                     ;; Also make sure that this isn't a dead summary buffer.
9810                     (not gnus-dead-summary-mode)))
9811              (push bufname buffers))
9812         (setq buflist (cdr buflist)))
9813       ;; Go through all these summary buffers and offer to save them.
9814       (when buffers
9815         (map-y-or-n-p
9816          "Update summary buffer %s? "
9817          (lambda (buf)
9818            (switch-to-buffer buf)
9819            (gnus-summary-exit))
9820          buffers)))))
9821
9822
9823 ;;; @ for mime-partial
9824 ;;;
9825
9826 (defun gnus-request-partial-message ()
9827   (save-excursion
9828     (let ((number (gnus-summary-article-number))
9829           (group gnus-newsgroup-name)
9830           (mother gnus-article-buffer))
9831       (set-buffer (get-buffer-create " *Partial Article*"))
9832       (erase-buffer)
9833       (setq mime-preview-buffer mother)
9834       (gnus-request-article-this-buffer number group)
9835       (mime-parse-buffer)
9836       )))
9837
9838 (autoload 'mime-combine-message/partial-pieces-automatically
9839   "mime-partial"
9840   "Internal method to combine message/partial messages automatically.")
9841
9842 (mime-add-condition
9843  'action '((type . message)(subtype . partial)
9844            (major-mode . gnus-original-article-mode)
9845            (method . mime-combine-message/partial-pieces-automatically)
9846            (summary-buffer-exp . gnus-summary-buffer)
9847            (request-partial-message-method . gnus-request-partial-message)
9848            ))
9849
9850
9851 ;;; @ for message/rfc822
9852 ;;;
9853
9854 (defun gnus-mime-extract-message/rfc822 (entity situation)
9855   (let (group article num cwin swin cur)
9856     (with-temp-buffer
9857       (mime-insert-entity-content entity)
9858       (setq group (or (cdr (assq 'group situation))
9859                       (completing-read "Group: "
9860                                        gnus-active-hashtb
9861                                        nil
9862                                        (gnus-read-active-file-p)
9863                                        gnus-newsgroup-name))
9864             article (gnus-request-accept-article group)))
9865     (when (and (consp article)
9866                (numberp (setq article (cdr article))))
9867       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9868             cwin (get-buffer-window (current-buffer) t))
9869       (save-window-excursion
9870         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9871             (select-window swin)
9872           (set-buffer gnus-summary-buffer))
9873         (setq cur gnus-current-article)
9874         (forward-line num)
9875         (let (gnus-show-threads)
9876           (gnus-summary-goto-subject article t))
9877         (gnus-summary-clear-mark-forward 1)
9878         (gnus-summary-goto-subject cur))
9879       (when (and cwin (window-frame cwin))
9880         (select-frame (window-frame cwin)))
9881       (when (boundp 'mime-acting-situation-to-override)
9882         (set-alist 'mime-acting-situation-to-override
9883                    'group
9884                    group)
9885         (set-alist 'mime-acting-situation-to-override
9886                    'after-method
9887                    `(progn
9888                       (save-current-buffer
9889                         (set-buffer gnus-group-buffer)
9890                         (gnus-activate-group ,group))
9891                       (gnus-summary-goto-article ,cur
9892                                                  gnus-show-all-headers)))
9893         (set-alist 'mime-acting-situation-to-override
9894                    'number num)))))
9895
9896 (mime-add-condition
9897  'action '((type . message)(subtype . rfc822)
9898            (major-mode . gnus-original-article-mode)
9899            (method . gnus-mime-extract-message/rfc822)
9900            (mode . "extract")
9901            ))
9902
9903 (mime-add-condition
9904  'action '((type . message)(subtype . news)
9905            (major-mode . gnus-original-article-mode)
9906            (method . gnus-mime-extract-message/rfc822)
9907            (mode . "extract")
9908            ))
9909
9910 (defun gnus-mime-extract-multipart (entity situation)
9911   (let ((children (mime-entity-children entity))
9912         mime-acting-situation-to-override
9913         f)
9914     (while children
9915       (mime-play-entity (car children)
9916                         (cons (assq 'mode situation)
9917                               mime-acting-situation-to-override))
9918       (setq children (cdr children)))
9919     (if (setq f (cdr (assq 'after-method
9920                            mime-acting-situation-to-override)))
9921         (eval f)
9922       )))
9923
9924 (mime-add-condition
9925  'action '((type . multipart)
9926            (method . gnus-mime-extract-multipart)
9927            (mode . "extract")
9928            )
9929  'with-default)
9930
9931
9932 ;;; @ end
9933 ;;;
9934
9935 (defun gnus-summary-setup-default-charset ()
9936   "Setup newsgroup default charset."
9937   (if (equal gnus-newsgroup-name "nndraft:drafts")
9938       (setq gnus-newsgroup-charset nil)
9939     (let* ((name (and gnus-newsgroup-name
9940                       (gnus-group-real-name gnus-newsgroup-name)))
9941            (ignored-charsets
9942             (or gnus-newsgroup-ephemeral-ignored-charsets
9943                 (append
9944                  (and gnus-newsgroup-name
9945                       (or (gnus-group-find-parameter gnus-newsgroup-name
9946                                                      'ignored-charsets t)
9947                           (let ((alist gnus-group-ignored-charsets-alist)
9948                                 elem (charsets nil))
9949                             (while (setq elem (pop alist))
9950                               (when (and name
9951                                          (string-match (car elem) name))
9952                                 (setq alist nil
9953                                       charsets (cdr elem))))
9954                             charsets)))
9955                  gnus-newsgroup-ignored-charsets))))
9956       (setq gnus-newsgroup-charset
9957             (or gnus-newsgroup-ephemeral-charset
9958                 (and gnus-newsgroup-name
9959                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9960                          (let ((alist gnus-group-charset-alist)
9961                                elem charset)
9962                            (while (setq elem (pop alist))
9963                              (when (and name
9964                                         (string-match (car elem) name))
9965                                (setq alist nil
9966                                      charset (cadr elem))))
9967                            charset)))
9968                 gnus-default-charset))
9969       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9970            ignored-charsets))))
9971
9972 ;;;
9973 ;;; Mime Commands
9974 ;;;
9975
9976 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9977   "Display the current article buffer fully MIME-buttonized.
9978 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9979 treated as multipart/mixed."
9980   (interactive "P")
9981   (require 'gnus-art)
9982   (let ((gnus-unbuttonized-mime-types nil)
9983         (gnus-mime-display-multipart-as-mixed show-all-parts))
9984     (gnus-summary-show-article)))
9985
9986 (defun gnus-summary-repair-multipart (article)
9987   "Add a Content-Type header to a multipart article without one."
9988   (interactive (list (gnus-summary-article-number)))
9989   (gnus-with-article article
9990     (message-narrow-to-head)
9991     (message-remove-header "Mime-Version")
9992     (goto-char (point-max))
9993     (insert "Mime-Version: 1.0\n")
9994     (widen)
9995     (when (search-forward "\n--" nil t)
9996       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9997         (message-narrow-to-head)
9998         (message-remove-header "Content-Type")
9999         (goto-char (point-max))
10000         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10001                         separator))
10002         (widen))))
10003   (let (gnus-mark-article-hook)
10004     (gnus-summary-select-article t t nil article)))
10005
10006 (defun gnus-summary-toggle-display-buttonized ()
10007   "Toggle the buttonizing of the article buffer."
10008   (interactive)
10009   (require 'gnus-art)
10010   (if (setq gnus-inhibit-mime-unbuttonizing
10011             (not gnus-inhibit-mime-unbuttonizing))
10012       (let ((gnus-unbuttonized-mime-types nil))
10013         (gnus-summary-show-article))
10014     (gnus-summary-show-article)))
10015
10016 ;;;
10017 ;;; Intelli-mouse commmands
10018 ;;;
10019
10020 (defun gnus-wheel-summary-scroll (event)
10021   (interactive "e")
10022   (let ((amount (if (memq 'shift (event-modifiers event))
10023                     (car gnus-wheel-scroll-amount)
10024                   (cdr gnus-wheel-scroll-amount)))
10025         (direction (- (* (static-if (featurep 'xemacs)
10026                              (event-button event)
10027                            (cond ((eq 'mouse-4 (event-basic-type event))
10028                                   4)
10029                                  ((eq 'mouse-5 (event-basic-type event))
10030                                   5)))
10031                          2) 9))
10032         edge)
10033     (gnus-summary-scroll-up (* amount direction))
10034     (when (gnus-eval-in-buffer-window gnus-article-buffer
10035             (save-restriction
10036               (widen)
10037               (and (if (< 0 direction)
10038                        (gnus-article-next-page 0)
10039                      (gnus-article-prev-page 0)
10040                      (bobp))
10041                    (if (setq edge (get-text-property
10042                                    (point-min) 'gnus-wheel-edge))
10043                        (setq edge (* edge direction))
10044                      (setq edge -1))
10045                    (or (plusp edge)
10046                        (let ((buffer-read-only nil)
10047                              (inhibit-read-only t))
10048                          (put-text-property (point-min) (point-max)
10049                                             'gnus-wheel-edge direction)
10050                          nil))
10051                    (or (> edge gnus-wheel-edge-resistance)
10052                        (let ((buffer-read-only nil)
10053                              (inhibit-read-only t))
10054                          (put-text-property (point-min) (point-max)
10055                                             'gnus-wheel-edge
10056                                             (* (1+ edge) direction))
10057                          nil))
10058                    (eq last-command 'gnus-wheel-summary-scroll))))
10059       (gnus-summary-next-article nil nil (minusp direction)))))
10060
10061 (defun gnus-wheel-install ()
10062   "Enable mouse wheel support on summary window."
10063   (when gnus-use-wheel
10064     (let ((keys
10065            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10066       (dolist (key keys)
10067         (define-key gnus-summary-mode-map key
10068           'gnus-wheel-summary-scroll)))))
10069
10070 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10071
10072 ;;;
10073 ;;; Traditional PGP commmands
10074 ;;;
10075
10076 (defun gnus-summary-decrypt-article (&optional force)
10077   "Decrypt the current article in traditional PGP way.
10078 This will have permanent effect only in mail groups.
10079 If FORCE is non-nil, allow editing of articles even in read-only
10080 groups."
10081   (interactive "P")
10082   (gnus-summary-select-article t)
10083   (gnus-eval-in-buffer-window gnus-article-buffer
10084     (save-excursion
10085       (save-restriction
10086         (widen)
10087         (goto-char (point-min))
10088         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10089           (error "Not a traditional PGP message!"))
10090         (let ((armor-start (match-beginning 0)))
10091           (if (and (pgg-decrypt-region armor-start (point-max))
10092                    (or force (not (gnus-group-read-only-p))))
10093               (let ((inhibit-read-only t)
10094                     buffer-read-only)
10095                 (delete-region armor-start
10096                                (progn
10097                                  (re-search-forward "^-+END PGP" nil t)
10098                                  (beginning-of-line 2)
10099                                  (point)))
10100                 (insert-buffer-substring pgg-output-buffer))))))))
10101
10102 (defun gnus-summary-verify-article ()
10103   "Verify the current article in traditional PGP way."
10104   (interactive)
10105   (save-excursion
10106     (set-buffer gnus-original-article-buffer)
10107     (goto-char (point-min))
10108     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10109       (error "Not a traditional PGP message!"))
10110     (re-search-forward "^-+END PGP" nil t)
10111     (beginning-of-line 2)
10112     (call-interactively (function pgg-verify-region))))
10113
10114 ;;;
10115 ;;; Generic summary marking commands
10116 ;;;
10117
10118 (defvar gnus-summary-marking-alist
10119   '((read gnus-del-mark "d")
10120     (unread gnus-unread-mark "u")
10121     (ticked gnus-ticked-mark "!")
10122     (dormant gnus-dormant-mark "?")
10123     (expirable gnus-expirable-mark "e"))
10124   "An alist of names/marks/keystrokes.")
10125
10126 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10127 (defvar gnus-summary-mark-map)
10128
10129 (defun gnus-summary-make-all-marking-commands ()
10130   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10131   (dolist (elem gnus-summary-marking-alist)
10132     (apply 'gnus-summary-make-marking-command elem)))
10133
10134 (defun gnus-summary-make-marking-command (name mark keystroke)
10135   (let ((map (make-sparse-keymap)))
10136     (define-key gnus-summary-generic-mark-map keystroke map)
10137     (dolist (lway `((next "next" next nil "n")
10138                     (next-unread "next unread" next t "N")
10139                     (prev "previous" prev nil "p")
10140                     (prev-unread "previous unread" prev t "P")
10141                     (nomove "" nil nil ,keystroke)))
10142       (let ((func (gnus-summary-make-marking-command-1
10143                    mark (car lway) lway name)))
10144         (setq func (eval func))
10145         (define-key map (nth 4 lway) func)))))
10146
10147 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10148   `(defun ,(intern
10149             (format "gnus-summary-put-mark-as-%s%s"
10150                     name (if (eq way 'nomove)
10151                              ""
10152                            (concat "-" (symbol-name way)))))
10153      (n)
10154      ,(format
10155        "Mark the current article as %s%s.
10156 If N, the prefix, then repeat N times.
10157 If N is negative, move in reverse order.
10158 The difference between N and the actual number of articles marked is
10159 returned."
10160        name (car (cdr lway)))
10161      (interactive "p")
10162      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10163
10164 (defun gnus-summary-generic-mark (n mark move unread)
10165   "Mark N articles with MARK."
10166   (unless (eq major-mode 'gnus-summary-mode)
10167     (error "This command can only be used in the summary buffer"))
10168   (gnus-summary-show-thread)
10169   (let ((nummove
10170          (cond
10171           ((eq move 'next) 1)
10172           ((eq move 'prev) -1)
10173           (t 0))))
10174     (if (zerop nummove)
10175         (setq n 1)
10176       (when (< n 0)
10177         (setq n (abs n)
10178               nummove (* -1 nummove))))
10179     (while (and (> n 0)
10180                 (gnus-summary-mark-article nil mark)
10181                 (zerop (gnus-summary-next-subject nummove unread t)))
10182       (setq n (1- n)))
10183     (when (/= 0 n)
10184       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10185     (gnus-summary-recenter)
10186     (gnus-summary-position-point)
10187     (gnus-set-mode-line 'summary)
10188     n))
10189
10190 (gnus-summary-make-all-marking-commands)
10191
10192 (gnus-ems-redefine)
10193
10194 (provide 'gnus-sum)
10195
10196 (run-hooks 'gnus-sum-load-hook)
10197
10198 ;;; gnus-sum.el ends here