525e41d7056340dfdab85d99ebe1c58bf5a2c6b5
[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 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
119   "*A regexp to match subjects to be excluded from loose thread gathering.
120 As loose thread gathering is done on subjects only, that means that
121 there can be many false gatherings performed.  By rooting out certain
122 common subjects, gathering might become saner."
123   :group 'gnus-thread
124   :type 'regexp)
125
126 (defcustom gnus-summary-gather-subject-limit nil
127   "*Maximum length of subject comparisons when gathering loose threads.
128 Use nil to compare full subjects.  Setting this variable to a low
129 number will help gather threads that have been corrupted by
130 newsreaders chopping off subject lines, but it might also mean that
131 unrelated articles that have subject that happen to begin with the
132 same few characters will be incorrectly gathered.
133
134 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
135 comparing subjects."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  (const fuzzy)
139                  (sexp :menu-tag "on" t)))
140
141 (defcustom gnus-simplify-subject-functions nil
142   "List of functions taking a string argument that simplify subjects.
143 The functions are applied recursively.
144
145 Useful functions to put in this list include: `gnus-simplify-subject-re',
146 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 (they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-zcore-fuzz 0
211   "*Fuzziness factor for the zcore in the summary buffer.
212 Articles with scores closer than this to `gnus-summary-default-score'
213 will not be marked."
214   :group 'gnus-summary-format
215   :type 'integer)
216
217 (defcustom gnus-simplify-subject-fuzzy-regexp nil
218   "*Strings to be removed when doing fuzzy matches.
219 This can either be a regular expression or list of regular expressions
220 that will be removed from subject strings if fuzzy subject
221 simplification is selected."
222   :group 'gnus-thread
223   :type '(repeat regexp))
224
225 (defcustom gnus-show-threads t
226   "*If non-nil, display threads in summary mode."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-hide-subtree nil
231   "*If non-nil, hide all threads initially.
232 If threads are hidden, you have to run the command
233 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
234 to expose hidden threads."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-hide-killed t
239   "*If non-nil, hide killed threads automatically."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-ignore-subject t
244   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
245 If nil, articles that have different subjects from their parents will
246 start separate threads."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-operation-ignore-subject t
251   "*If non-nil, subjects will be ignored when doing thread commands.
252 This affects commands like `gnus-summary-kill-thread' and
253 `gnus-summary-lower-thread'.
254
255 If this variable is nil, articles in the same thread with different
256 subjects will not be included in the operation in question.  If this
257 variable is `fuzzy', only articles that have subjects that are fuzzily
258 equal will be included."
259   :group 'gnus-thread
260   :type '(choice (const :tag "off" nil)
261                  (const fuzzy)
262                  (sexp :tag "on" t)))
263
264 (defcustom gnus-thread-indent-level 4
265   "*Number that says how much each sub-thread should be indented."
266   :group 'gnus-thread
267   :type 'integer)
268
269 (defcustom gnus-auto-extend-newsgroup t
270   "*If non-nil, extend newsgroup forward and backward when requested."
271   :group 'gnus-summary-choose
272   :type 'boolean)
273
274 (defcustom gnus-auto-select-first t
275   "*If nil, don't select the first unread article when entering a group.
276 If this variable is `best', select the highest-scored unread article
277 in the group.  If t, select the first unread article.
278
279 This variable can also be a function to place point on a likely
280 subject line.  Useful values include `gnus-summary-first-unread-subject',
281 `gnus-summary-first-unread-article' and
282 `gnus-summary-best-unread-article'.
283
284 If you want to prevent automatic selection of the first unread article
285 in some newsgroups, set the variable to nil in
286 `gnus-select-group-hook'."
287   :group 'gnus-group-select
288   :type '(choice (const :tag "none" nil)
289                  (const best)
290                  (sexp :menu-tag "first" t)
291                  (function-item gnus-summary-first-unread-subject)
292                  (function-item gnus-summary-first-unread-article)
293                  (function-item gnus-summary-best-unread-article)))
294
295 (defcustom gnus-dont-select-after-jump-to-other-group nil
296   "If non-nil, don't select the first unread article after entering the
297 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
298 it is depend on the value of `gnus-auto-select-first' whether to select
299 or not."
300   :group 'gnus-group-select
301   :type 'boolean)
302
303 (defcustom gnus-auto-select-next t
304   "*If non-nil, offer to go to the next group from the end of the previous.
305 If the value is t and the next newsgroup is empty, Gnus will exit
306 summary mode and go back to group mode.  If the value is neither nil
307 nor t, Gnus will select the following unread newsgroup.  In
308 particular, if the value is the symbol `quietly', the next unread
309 newsgroup will be selected without any confirmation, and if it is
310 `almost-quietly', the next group will be selected without any
311 confirmation if you are located on the last article in the group.
312 Finally, if this variable is `slightly-quietly', the `Z n' command
313 will go to the next group without confirmation."
314   :group 'gnus-summary-maneuvering
315   :type '(choice (const :tag "off" nil)
316                  (const quietly)
317                  (const almost-quietly)
318                  (const slightly-quietly)
319                  (sexp :menu-tag "on" t)))
320
321 (defcustom gnus-auto-select-same nil
322   "*If non-nil, select the next article with the same subject.
323 If there are no more articles with the same subject, go to
324 the first unread article."
325   :group 'gnus-summary-maneuvering
326   :type 'boolean)
327
328 (defcustom gnus-summary-check-current nil
329   "*If non-nil, consider the current article when moving.
330 The \"unread\" movement commands will stay on the same line if the
331 current article is unread."
332   :group 'gnus-summary-maneuvering
333   :type 'boolean)
334
335 (defcustom gnus-auto-center-summary t
336   "*If non-nil, always center the current summary buffer.
337 In particular, if `vertical' do only vertical recentering.  If non-nil
338 and non-`vertical', do both horizontal and vertical recentering."
339   :group 'gnus-summary-maneuvering
340   :type '(choice (const :tag "none" nil)
341                  (const vertical)
342                  (integer :tag "height")
343                  (sexp :menu-tag "both" t)))
344
345 (defcustom gnus-show-all-headers nil
346   "*If non-nil, don't hide any headers."
347   :group 'gnus-article-hiding
348   :group 'gnus-article-headers
349   :type 'boolean)
350
351 (defcustom gnus-summary-ignore-duplicates nil
352   "*If non-nil, ignore articles with identical Message-ID headers."
353   :group 'gnus-summary
354   :type 'boolean)
355
356 (defcustom gnus-single-article-buffer t
357   "*If non-nil, display all articles in the same buffer.
358 If nil, each group will get its own article buffer."
359   :group 'gnus-article-various
360   :type 'boolean)
361
362 (defcustom gnus-break-pages t
363   "*If non-nil, do page breaking on articles.
364 The page delimiter is specified by the `gnus-page-delimiter'
365 variable."
366   :group 'gnus-article-various
367   :type 'boolean)
368
369 (defcustom gnus-show-mime t
370   "*If non-nil, do mime processing of articles.
371 The articles will simply be fed to the function given by
372 `gnus-article-display-method-for-mime'."
373   :group 'gnus-article-mime
374   :type 'boolean)
375
376 (defcustom gnus-move-split-methods nil
377   "*Variable used to suggest where articles are to be moved to.
378 It uses the same syntax as the `gnus-split-methods' variable.
379 However, whereas `gnus-split-methods' specifies file names as targets,
380 this variable specifies group names."
381   :group 'gnus-summary-mail
382   :type '(repeat (choice (list :value (fun) function)
383                          (cons :value ("" "") regexp (repeat string))
384                          (sexp :value nil))))
385
386 (defcustom gnus-unread-mark ?  ;Whitespace
387   "*Mark used for unread articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-ticked-mark ?!
392   "*Mark used for ticked articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-dormant-mark ??
397   "*Mark used for dormant articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-del-mark ?r
402   "*Mark used for del'd articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-read-mark ?R
407   "*Mark used for read articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-expirable-mark ?E
412   "*Mark used for expirable articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-killed-mark ?K
417   "*Mark used for killed articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-souped-mark ?F
422   "*Mark used for killed articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-kill-file-mark ?X
427   "*Mark used for articles killed by kill files."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-low-score-mark ?Y
432   "*Mark used for articles with a low score."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-catchup-mark ?C
437   "*Mark used for articles that are caught up."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-replied-mark ?A
442   "*Mark used for articles that have been replied to."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-cached-mark ?*
447   "*Mark used for articles that are in the cache."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-saved-mark ?S
452   "*Mark used for articles that have been saved to."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-ancient-mark ?O
457   "*Mark used for ancient articles."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-sparse-mark ?Q
462   "*Mark used for sparsely reffed articles."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-canceled-mark ?G
467   "*Mark used for canceled articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-duplicate-mark ?M
472   "*Mark used for duplicate articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-undownloaded-mark ?@
477   "*Mark used for articles that weren't downloaded."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-downloadable-mark ?%
482   "*Mark used for articles that are to be downloaded."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-unsendable-mark ?=
487   "*Mark used for articles that won't be sent."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-score-over-mark ?+
492   "*Score mark used for articles with high scores."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-score-below-mark ?-
497   "*Score mark used for articles with low scores."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-empty-thread-mark ?  ;Whitespace
502   "*There is no thread under the article."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-not-empty-thread-mark ?=
507   "*There is a thread under the article."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-view-pseudo-asynchronously nil
512   "*If non-nil, Gnus will view pseudo-articles asynchronously."
513   :group 'gnus-extract-view
514   :type 'boolean)
515
516 (defcustom gnus-auto-expirable-marks
517   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
518         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
519         gnus-souped-mark gnus-duplicate-mark)
520   "*The list of marks converted into expiration if a group is auto-expirable."
521   :group 'gnus-summary
522   :type '(repeat character))
523
524 (defcustom gnus-inhibit-user-auto-expire t
525   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
526   :group 'gnus-summary
527   :type 'boolean)
528
529 (defcustom gnus-view-pseudos nil
530   "*If `automatic', pseudo-articles will be viewed automatically.
531 If `not-confirm', pseudos will be viewed automatically, and the user
532 will not be asked to confirm the command."
533   :group 'gnus-extract-view
534   :type '(choice (const :tag "off" nil)
535                  (const automatic)
536                  (const not-confirm)))
537
538 (defcustom gnus-view-pseudos-separately t
539   "*If non-nil, one pseudo-article will be created for each file to be viewed.
540 If nil, all files that use the same viewing command will be given as a
541 list of parameters to that command."
542   :group 'gnus-extract-view
543   :type 'boolean)
544
545 (defcustom gnus-insert-pseudo-articles t
546   "*If non-nil, insert pseudo-articles when decoding articles."
547   :group 'gnus-extract-view
548   :type 'boolean)
549
550 (defcustom gnus-summary-dummy-line-format
551   "  %(:                          :%) %S\n"
552   "*The format specification for the dummy roots in the summary buffer.
553 It works along the same lines as a normal formatting string,
554 with some simple extensions.
555
556 %S  The subject"
557   :group 'gnus-threading
558   :type 'string)
559
560 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
561   "*The format specification for the summary mode line.
562 It works along the same lines as a normal formatting string,
563 with some simple extensions:
564
565 %G  Group name
566 %p  Unprefixed group name
567 %A  Current article number
568 %z  Current article score
569 %V  Gnus version
570 %U  Number of unread articles in the group
571 %e  Number of unselected articles in the group
572 %Z  A string with unread/unselected article counts
573 %g  Shortish group name
574 %S  Subject of the current article
575 %u  User-defined spec
576 %s  Current score file name
577 %d  Number of dormant articles
578 %r  Number of articles that have been marked as read in this session
579 %E  Number of articles expunged by the score files"
580   :group 'gnus-summary-format
581   :type 'string)
582
583 (defcustom gnus-list-identifiers nil
584   "Regexp that matches list identifiers to be removed from subject.
585 This can also be a list of regexps."
586   :group 'gnus-summary-format
587   :group 'gnus-article-hiding
588   :type '(choice (const :tag "none" nil)
589                  (regexp :value ".*")
590                  (repeat :value (".*") regexp)))
591
592 (defcustom gnus-summary-mark-below 0
593   "*Mark all articles with a score below this variable as read.
594 This variable is local to each summary buffer and usually set by the
595 score file."
596   :group 'gnus-score-default
597   :type 'integer)
598
599 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
600   "*List of functions used for sorting articles in the summary buffer.
601 This variable is only used when not using a threaded display."
602   :group 'gnus-summary-sort
603   :type '(repeat (choice (function-item gnus-article-sort-by-number)
604                          (function-item gnus-article-sort-by-author)
605                          (function-item gnus-article-sort-by-subject)
606                          (function-item gnus-article-sort-by-date)
607                          (function-item gnus-article-sort-by-score)
608                          (function :tag "other"))))
609
610 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
611   "*List of functions used for sorting threads in the summary buffer.
612 By default, threads are sorted by article number.
613
614 Each function takes two threads and return non-nil if the first thread
615 should be sorted before the other.  If you use more than one function,
616 the primary sort function should be the last.  You should probably
617 always include `gnus-thread-sort-by-number' in the list of sorting
618 functions -- preferably first.
619
620 Ready-made functions include `gnus-thread-sort-by-number',
621 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
622 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
623 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
624   :group 'gnus-summary-sort
625   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
626                          (function-item gnus-thread-sort-by-author)
627                          (function-item gnus-thread-sort-by-subject)
628                          (function-item gnus-thread-sort-by-date)
629                          (function-item gnus-thread-sort-by-score)
630                          (function-item gnus-thread-sort-by-total-score)
631                          (function :tag "other"))))
632
633 (defcustom gnus-thread-score-function '+
634   "*Function used for calculating the total score of a thread.
635
636 The function is called with the scores of the article and each
637 subthread and should then return the score of the thread.
638
639 Some functions you can use are `+', `max', or `min'."
640   :group 'gnus-summary-sort
641   :type 'function)
642
643 (defcustom gnus-summary-expunge-below nil
644   "All articles that have a score less than this variable will be expunged.
645 This variable is local to the summary buffers."
646   :group 'gnus-score-default
647   :type '(choice (const :tag "off" nil)
648                  integer))
649
650 (defcustom gnus-thread-expunge-below nil
651   "All threads that have a total score less than this variable will be expunged.
652 See `gnus-thread-score-function' for en explanation of what a
653 \"thread score\" is.
654
655 This variable is local to the summary buffers."
656   :group 'gnus-threading
657   :group 'gnus-score-default
658   :type '(choice (const :tag "off" nil)
659                  integer))
660
661 (defcustom gnus-summary-mode-hook nil
662   "*A hook for Gnus summary mode.
663 This hook is run before any variables are set in the summary buffer."
664   :group 'gnus-summary-various
665   :type 'hook)
666
667 (defcustom gnus-summary-menu-hook nil
668   "*Hook run after the creation of the summary mode menu."
669   :group 'gnus-summary-visual
670   :type 'hook)
671
672 (defcustom gnus-summary-exit-hook nil
673   "*A hook called on exit from the summary buffer.
674 It will be called with point in the group buffer."
675   :group 'gnus-summary-exit
676   :type 'hook)
677
678 (defcustom gnus-summary-prepare-hook nil
679   "*A hook called after the summary buffer has been generated.
680 If you want to modify the summary buffer, you can use this hook."
681   :group 'gnus-summary-various
682   :type 'hook)
683
684 (defcustom gnus-summary-prepared-hook nil
685   "*A hook called as the last thing after the summary buffer has been generated."
686   :group 'gnus-summary-various
687   :type 'hook)
688
689 (defcustom gnus-summary-generate-hook nil
690   "*A hook run just before generating the summary buffer.
691 This hook is commonly used to customize threading variables and the
692 like."
693   :group 'gnus-summary-various
694   :type 'hook)
695
696 (defcustom gnus-select-group-hook nil
697   "*A hook called when a newsgroup is selected.
698
699 If you'd like to simplify subjects like the
700 `gnus-summary-next-same-subject' command does, you can use the
701 following hook:
702
703  (setq gnus-select-group-hook
704       (list
705         (lambda ()
706           (mapcar (lambda (header)
707                      (mail-header-set-subject
708                       header
709                       (gnus-simplify-subject
710                        (mail-header-subject header) 're-only)))
711                   gnus-newsgroup-headers))))"
712   :group 'gnus-group-select
713   :type 'hook)
714
715 (defcustom gnus-select-article-hook nil
716   "*A hook called when an article is selected."
717   :group 'gnus-summary-choose
718   :type 'hook)
719
720 (defcustom gnus-visual-mark-article-hook
721   (list 'gnus-highlight-selected-summary)
722   "*Hook run after selecting an article in the summary buffer.
723 It is meant to be used for highlighting the article in some way.  It
724 is not run if `gnus-visual' is nil."
725   :group 'gnus-summary-visual
726   :type 'hook)
727
728 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
729   "*A hook called before parsing the headers."
730   :group 'gnus-various
731   :type 'hook)
732
733 (defcustom gnus-exit-group-hook nil
734   "*A hook called when exiting summary mode.
735 This hook is not called from the non-updating exit commands like `Q'."
736   :group 'gnus-various
737   :type 'hook)
738
739 (defcustom gnus-summary-update-hook
740   (list 'gnus-summary-highlight-line)
741   "*A hook called when a summary line is changed.
742 The hook will not be called if `gnus-visual' is nil.
743
744 The default function `gnus-summary-highlight-line' will
745 highlight the line according to the `gnus-summary-highlight'
746 variable."
747   :group 'gnus-summary-visual
748   :type 'hook)
749
750 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
751   "*A hook called when an article is selected for the first time.
752 The hook is intended to mark an article as read (or unread)
753 automatically when it is selected."
754   :group 'gnus-summary-choose
755   :type 'hook)
756
757 (defcustom gnus-group-no-more-groups-hook nil
758   "*A hook run when returning to group mode having no more (unread) groups."
759   :group 'gnus-group-select
760   :type 'hook)
761
762 (defcustom gnus-ps-print-hook nil
763   "*A hook run before ps-printing something from Gnus."
764   :group 'gnus-summary
765   :type 'hook)
766
767 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
768   "Face used for highlighting the current article in the summary buffer."
769   :group 'gnus-summary-visual
770   :type 'face)
771
772 (defcustom gnus-summary-highlight
773   '(((= mark gnus-canceled-mark)
774      . gnus-summary-cancelled-face)
775     ((and (> score default)
776           (or (= mark gnus-dormant-mark)
777               (= mark gnus-ticked-mark)))
778      . gnus-summary-high-ticked-face)
779     ((and (< score default)
780           (or (= mark gnus-dormant-mark)
781               (= mark gnus-ticked-mark)))
782      . gnus-summary-low-ticked-face)
783     ((or (= mark gnus-dormant-mark)
784          (= mark gnus-ticked-mark))
785      . gnus-summary-normal-ticked-face)
786     ((and (> score default) (= mark gnus-ancient-mark))
787      . gnus-summary-high-ancient-face)
788     ((and (< score default) (= mark gnus-ancient-mark))
789      . gnus-summary-low-ancient-face)
790     ((= mark gnus-ancient-mark)
791      . gnus-summary-normal-ancient-face)
792     ((and (> score default) (= mark gnus-unread-mark))
793      . gnus-summary-high-unread-face)
794     ((and (< score default) (= mark gnus-unread-mark))
795      . gnus-summary-low-unread-face)
796     ((and (memq article gnus-newsgroup-incorporated)
797           (= mark gnus-unread-mark))
798      . gnus-summary-incorporated-face)
799     ((= mark gnus-unread-mark)
800      . gnus-summary-normal-unread-face)
801     ((and (> score default) (memq mark (list gnus-downloadable-mark
802                                              gnus-undownloaded-mark)))
803      . gnus-summary-high-unread-face)
804     ((and (< score default) (memq mark (list gnus-downloadable-mark
805                                              gnus-undownloaded-mark)))
806      . gnus-summary-low-unread-face)
807     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
808      . gnus-summary-normal-unread-face)
809     ((> score default)
810      . gnus-summary-high-read-face)
811     ((< score default)
812      . gnus-summary-low-read-face)
813     (t
814      . gnus-summary-normal-read-face))
815   "*Controls the highlighting of summary buffer lines.
816
817 A list of (FORM . FACE) pairs.  When deciding how a a particular
818 summary line should be displayed, each form is evaluated.  The content
819 of the face field after the first true form is used.  You can change
820 how those summary lines are displayed, by editing the face field.
821
822 You can use the following variables in the FORM field.
823
824 score:   The articles score
825 default: The default article score.
826 below:   The score below which articles are automatically marked as read.
827 mark:    The articles mark."
828   :group 'gnus-summary-visual
829   :type '(repeat (cons (sexp :tag "Form" nil)
830                        face)))
831
832 (defcustom gnus-alter-header-function nil
833   "Function called to allow alteration of article header structures.
834 The function is called with one parameter, the article header vector,
835 which it may alter in any way.")
836
837 (defvar gnus-decode-encoded-word-function
838   (mime-find-field-decoder 'From 'nov)
839   "Variable that says which function should be used to decode a string with encoded words.")
840
841 (defcustom gnus-extra-headers nil
842   "*Extra headers to parse."
843   :group 'gnus-summary
844   :type '(repeat symbol))
845
846 (defcustom gnus-ignored-from-addresses
847   (and user-mail-address (regexp-quote user-mail-address))
848   "*Regexp of From headers that may be suppressed in favor of To headers."
849   :group 'gnus-summary
850   :type 'regexp)
851
852 (defcustom gnus-group-charset-alist
853   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
854     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
855     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
856     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
857     ("^relcom\\>" koi8-r)
858     ("^fido7\\>" koi8-r)
859     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
860     ("^israel\\>" iso-8859-1)
861     ("^han\\>" euc-kr)
862     ("^alt.chinese.text.big5\\>" chinese-big5)
863     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
864     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
865     (".*" iso-8859-1))
866   "Alist of regexps (to match group names) and default charsets to be used when reading."
867   :type '(repeat (list (regexp :tag "Group")
868                        (symbol :tag "Charset")))
869   :group 'gnus-charset)
870
871 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
872   "List of charsets that should be ignored.
873 When these charsets are used in the \"charset\" parameter, the
874 default charset will be used instead."
875   :type '(repeat symbol)
876   :group 'gnus-charset)
877
878 (defcustom gnus-group-ignored-charsets-alist
879   '(("alt\\.chinese\\.text" iso-8859-1))
880   "Alist of regexps (to match group names) and charsets that should be ignored.
881 When these charsets are used in the \"charset\" parameter, the
882 default charset will be used instead."
883   :type '(repeat (cons (regexp :tag "Group")
884                        (repeat symbol)))
885   :group 'gnus-charset)
886
887 (defcustom gnus-group-highlight-words-alist nil
888   "Alist of group regexps and highlight regexps.
889 This variable uses the same syntax as `gnus-emphasis-alist'."
890   :type '(repeat (cons (regexp :tag "Group")
891                        (repeat (list (regexp :tag "Highlight regexp")
892                                      (number :tag "Group for entire word" 0)
893                                      (number :tag "Group for displayed part" 0)
894                                      (symbol :tag "Face"
895                                              gnus-emphasis-highlight-words)))))
896   :group 'gnus-summary-visual)
897
898 (defcustom gnus-use-wheel nil
899   "Use Intelli-mouse on summary movement"
900   :type 'boolean
901   :group 'gnus-summary-maneuvering)
902
903 (defcustom gnus-wheel-scroll-amount '(5 . 1)
904   "Amount to scroll messages by spinning the mouse wheel.
905 This is actually a cons cell, where the first item is the amount to scroll
906 on a normal wheel event, and the second is the amount to scroll when the
907 wheel is moved with the shift key depressed."
908   :type '(cons (integer :tag "Shift") integer)
909   :group 'gnus-summary-maneuvering)
910
911 (defcustom gnus-wheel-edge-resistance 2
912   "How hard it should be to change the current article
913 by moving the mouse over the edge of the article window."
914   :type 'integer
915   :group 'gnus-summary-maneuvering)
916
917 (defcustom gnus-summary-show-article-charset-alist
918   nil
919   "Alist of number and charset.
920 The article will be shown with the charset corresponding to the
921 numbered argument.
922 For example: ((1 . cn-gb-2312) (2 . big5))."
923   :type '(repeat (cons (number :tag "Argument" 1)
924                        (symbol :tag "Charset")))
925   :group 'gnus-charset)
926
927 (defcustom gnus-preserve-marks t
928   "Whether marks are preserved when moving, copying and respooling messages."
929   :type 'boolean
930   :group 'gnus-summary-marks)
931
932 (defcustom gnus-alter-articles-to-read-function nil
933   "Function to be called to alter the list of articles to be selected."
934   :type 'function
935   :group 'gnus-summary)
936
937 (defcustom gnus-orphan-score nil
938   "*All orphans get this score added.  Set in the score file."
939   :group 'gnus-score-default
940   :type '(choice (const nil)
941                  integer))
942
943 (defcustom gnus-summary-save-parts-default-mime "image/.*"
944   "*A regexp to match MIME parts when saving multiple parts of a message
945 with gnus-summary-save-parts (X m). This regexp will be used by default
946 when prompting the user for which type of files to save."
947   :group 'gnus-summary
948   :type 'regexp)
949
950
951 ;;; Internal variables
952
953 (defvar gnus-article-mime-handles nil)
954 (defvar gnus-article-decoded-p nil)
955 (defvar gnus-article-charset nil)
956 (defvar gnus-article-ignored-charsets nil)
957 (defvar gnus-scores-exclude-files nil)
958 (defvar gnus-page-broken nil)
959 (defvar gnus-inhibit-mime-unbuttonizing nil)
960
961 (defvar gnus-original-article nil)
962 (defvar gnus-article-internal-prepare-hook nil)
963 (defvar gnus-newsgroup-process-stack nil)
964
965 (defvar gnus-thread-indent-array nil)
966 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
967 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
968   "Function called to sort the articles within a thread after it has been gathered together.")
969
970 (defvar gnus-summary-save-parts-type-history nil)
971 (defvar gnus-summary-save-parts-last-directory nil)
972
973 ;; Avoid highlighting in kill files.
974 (defvar gnus-summary-inhibit-highlight nil)
975 (defvar gnus-newsgroup-selected-overlay nil)
976 (defvar gnus-inhibit-limiting nil)
977 (defvar gnus-newsgroup-adaptive-score-file nil)
978 (defvar gnus-current-score-file nil)
979 (defvar gnus-current-move-group nil)
980 (defvar gnus-current-copy-group nil)
981 (defvar gnus-current-crosspost-group nil)
982
983 (defvar gnus-newsgroup-dependencies nil)
984 (defvar gnus-newsgroup-adaptive nil)
985 (defvar gnus-summary-display-article-function nil)
986 (defvar gnus-summary-highlight-line-function nil
987   "Function called after highlighting a summary line.")
988
989 (defvar gnus-summary-line-format-alist
990   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
991     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
992     (?s gnus-tmp-subject-or-nil ?s)
993     (?n gnus-tmp-name ?s)
994     (?A (std11-address-string
995          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
996     (?a (or (std11-full-name-string
997              (car (mime-entity-read-field gnus-tmp-header 'From)))
998             gnus-tmp-from) ?s)
999     (?F gnus-tmp-from ?s)
1000     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1001     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1002     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1003     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1004     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1005     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1006     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1007     (?L gnus-tmp-lines ?d)
1008     (?I gnus-tmp-indentation ?s)
1009     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1010     (?R gnus-tmp-replied ?c)
1011     (?\[ gnus-tmp-opening-bracket ?c)
1012     (?\] gnus-tmp-closing-bracket ?c)
1013     (?\> (make-string gnus-tmp-level ? ) ?s)
1014     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1015     (?i gnus-tmp-score ?d)
1016     (?z gnus-tmp-score-char ?c)
1017     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1018     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1019     (?U gnus-tmp-unread ?c)
1020     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1021     (?t (gnus-summary-number-of-articles-in-thread
1022          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1023         ?d)
1024     (?e (gnus-summary-number-of-articles-in-thread
1025          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1026         ?c)
1027     (?u gnus-tmp-user-defined ?s)
1028     (?P (gnus-pick-line-number) ?d))
1029   "An alist of format specifications that can appear in summary lines.
1030 These are paired with what variables they correspond with, along with
1031 the type of the variable (string, integer, character, etc).")
1032
1033 (defvar gnus-summary-dummy-line-format-alist
1034   `((?S gnus-tmp-subject ?s)
1035     (?N gnus-tmp-number ?d)
1036     (?u gnus-tmp-user-defined ?s)))
1037
1038 (defvar gnus-summary-mode-line-format-alist
1039   `((?G gnus-tmp-group-name ?s)
1040     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1041     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1042     (?A gnus-tmp-article-number ?d)
1043     (?Z gnus-tmp-unread-and-unselected ?s)
1044     (?V gnus-version ?s)
1045     (?U gnus-tmp-unread-and-unticked ?d)
1046     (?S gnus-tmp-subject ?s)
1047     (?e gnus-tmp-unselected ?d)
1048     (?u gnus-tmp-user-defined ?s)
1049     (?d (length gnus-newsgroup-dormant) ?d)
1050     (?t (length gnus-newsgroup-marked) ?d)
1051     (?r (length gnus-newsgroup-reads) ?d)
1052     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1053     (?E gnus-newsgroup-expunged-tally ?d)
1054     (?s (gnus-current-score-file-nondirectory) ?s)))
1055
1056 (defvar gnus-last-search-regexp nil
1057   "Default regexp for article search command.")
1058
1059 (defvar gnus-summary-search-article-matched-data nil
1060   "Last matched data of article search command.  It is the local variable
1061 in `gnus-article-buffer' which consists of the list of start position,
1062 end position and text.")
1063
1064 (defvar gnus-last-shell-command nil
1065   "Default shell command on article.")
1066
1067 (defvar gnus-newsgroup-begin nil)
1068 (defvar gnus-newsgroup-end nil)
1069 (defvar gnus-newsgroup-last-rmail nil)
1070 (defvar gnus-newsgroup-last-mail nil)
1071 (defvar gnus-newsgroup-last-folder nil)
1072 (defvar gnus-newsgroup-last-file nil)
1073 (defvar gnus-newsgroup-auto-expire nil)
1074 (defvar gnus-newsgroup-active nil)
1075
1076 (defvar gnus-newsgroup-data nil)
1077 (defvar gnus-newsgroup-data-reverse nil)
1078 (defvar gnus-newsgroup-limit nil)
1079 (defvar gnus-newsgroup-limits nil)
1080
1081 (defvar gnus-newsgroup-unreads nil
1082   "List of unread articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-unselected nil
1085   "List of unselected unread articles in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-reads nil
1088   "Alist of read articles and article marks in the current newsgroup.")
1089
1090 (defvar gnus-newsgroup-expunged-tally nil)
1091
1092 (defvar gnus-newsgroup-marked nil
1093   "List of ticked articles in the current newsgroup (a subset of unread art).")
1094
1095 (defvar gnus-newsgroup-killed nil
1096   "List of ranges of articles that have been through the scoring process.")
1097
1098 (defvar gnus-newsgroup-cached nil
1099   "List of articles that come from the article cache.")
1100
1101 (defvar gnus-newsgroup-saved nil
1102   "List of articles that have been saved.")
1103
1104 (defvar gnus-newsgroup-kill-headers nil)
1105
1106 (defvar gnus-newsgroup-replied nil
1107   "List of articles that have been replied to in the current newsgroup.")
1108
1109 (defvar gnus-newsgroup-expirable nil
1110   "List of articles in the current newsgroup that can be expired.")
1111
1112 (defvar gnus-newsgroup-processable nil
1113   "List of articles in the current newsgroup that can be processed.")
1114
1115 (defvar gnus-newsgroup-downloadable nil
1116   "List of articles in the current newsgroup that can be processed.")
1117
1118 (defvar gnus-newsgroup-undownloaded nil
1119   "List of articles in the current newsgroup that haven't been downloaded..")
1120
1121 (defvar gnus-newsgroup-unsendable nil
1122   "List of articles in the current newsgroup that won't be sent.")
1123
1124 (defvar gnus-newsgroup-bookmarks nil
1125   "List of articles in the current newsgroup that have bookmarks.")
1126
1127 (defvar gnus-newsgroup-dormant nil
1128   "List of dormant articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-scored nil
1131   "List of scored articles in the current newsgroup.")
1132
1133 (defvar gnus-newsgroup-incorporated nil
1134   "List of incorporated articles in the current newsgroup.")
1135
1136 (defvar gnus-newsgroup-headers nil
1137   "List of article headers in the current newsgroup.")
1138
1139 (defvar gnus-newsgroup-threads nil)
1140
1141 (defvar gnus-newsgroup-prepared nil
1142   "Whether the current group has been prepared properly.")
1143
1144 (defvar gnus-newsgroup-ancient nil
1145   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1146
1147 (defvar gnus-newsgroup-sparse nil)
1148
1149 (defvar gnus-current-article nil)
1150 (defvar gnus-article-current nil)
1151 (defvar gnus-current-headers nil)
1152 (defvar gnus-have-all-headers nil)
1153 (defvar gnus-last-article nil)
1154 (defvar gnus-newsgroup-history nil)
1155 (defvar gnus-newsgroup-charset nil)
1156 (defvar gnus-newsgroup-ephemeral-charset nil)
1157 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1158
1159 (defvar gnus-article-before-search nil)
1160
1161 (defconst gnus-summary-local-variables
1162   '(gnus-newsgroup-name
1163     gnus-newsgroup-begin gnus-newsgroup-end
1164     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1165     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1166     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1167     gnus-newsgroup-unselected gnus-newsgroup-marked
1168     gnus-newsgroup-reads gnus-newsgroup-saved
1169     gnus-newsgroup-replied gnus-newsgroup-expirable
1170     gnus-newsgroup-processable gnus-newsgroup-killed
1171     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1172     gnus-newsgroup-unsendable
1173     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1174     gnus-newsgroup-headers gnus-newsgroup-threads
1175     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1176     gnus-current-article gnus-current-headers gnus-have-all-headers
1177     gnus-last-article gnus-article-internal-prepare-hook
1178     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1179     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1180     gnus-thread-expunge-below
1181     gnus-score-alist gnus-current-score-file
1182     (gnus-summary-expunge-below . global)
1183     (gnus-summary-mark-below . global)
1184     (gnus-orphan-score . global)
1185     gnus-newsgroup-active gnus-scores-exclude-files
1186     gnus-newsgroup-history gnus-newsgroup-ancient
1187     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1188     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1189     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1190     (gnus-newsgroup-expunged-tally . 0)
1191     gnus-cache-removable-articles gnus-newsgroup-cached
1192     gnus-newsgroup-data gnus-newsgroup-data-reverse
1193     gnus-newsgroup-limit gnus-newsgroup-limits
1194     gnus-newsgroup-charset
1195     gnus-newsgroup-incorporated)
1196   "Variables that are buffer-local to the summary buffers.")
1197
1198 (defvar gnus-newsgroup-variables nil
1199   "Variables that have separate values in the newsgroups.")
1200
1201 ;; Byte-compiler warning.
1202 (defvar gnus-article-mode-map)
1203
1204 ;; Subject simplification.
1205
1206 (defun gnus-simplify-whitespace (str)
1207   "Remove excessive whitespace from STR."
1208   (let ((mystr str))
1209     ;; Multiple spaces.
1210     (while (string-match "[ \t][ \t]+" mystr)
1211       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1212                           " "
1213                           (substring mystr (match-end 0)))))
1214     ;; Leading spaces.
1215     (when (string-match "^[ \t]+" mystr)
1216       (setq mystr (substring mystr (match-end 0))))
1217     ;; Trailing spaces.
1218     (when (string-match "[ \t]+$" mystr)
1219       (setq mystr (substring mystr 0 (match-beginning 0))))
1220     mystr))
1221
1222 (defsubst gnus-simplify-subject-re (subject)
1223   "Remove \"Re:\" from subject lines."
1224   (if (string-match "^[Rr][Ee]: *" subject)
1225       (substring subject (match-end 0))
1226     subject))
1227
1228 (defun gnus-simplify-subject (subject &optional re-only)
1229   "Remove `Re:' and words in parentheses.
1230 If RE-ONLY is non-nil, strip leading `Re:'s only."
1231   (let ((case-fold-search t))           ;Ignore case.
1232     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1233     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1234       (setq subject (substring subject (match-end 0))))
1235     ;; Remove uninteresting prefixes.
1236     (when (and (not re-only)
1237                gnus-simplify-ignored-prefixes
1238                (string-match gnus-simplify-ignored-prefixes subject))
1239       (setq subject (substring subject (match-end 0))))
1240     ;; Remove words in parentheses from end.
1241     (unless re-only
1242       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1243         (setq subject (substring subject 0 (match-beginning 0)))))
1244     ;; Return subject string.
1245     subject))
1246
1247 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1248 ;; all whitespace.
1249 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1250   (goto-char (point-min))
1251   (while (re-search-forward regexp nil t)
1252     (replace-match (or newtext ""))))
1253
1254 (defun gnus-simplify-buffer-fuzzy ()
1255   "Simplify string in the buffer fuzzily.
1256 The string in the accessible portion of the current buffer is simplified.
1257 It is assumed to be a single-line subject.
1258 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1259 matter is removed.  Additional things can be deleted by setting
1260 `gnus-simplify-subject-fuzzy-regexp'."
1261   (let ((case-fold-search t)
1262         (modified-tick))
1263     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1264
1265     (while (not (eq modified-tick (buffer-modified-tick)))
1266       (setq modified-tick (buffer-modified-tick))
1267       (cond
1268        ((listp gnus-simplify-subject-fuzzy-regexp)
1269         (mapcar 'gnus-simplify-buffer-fuzzy-step
1270                 gnus-simplify-subject-fuzzy-regexp))
1271        (gnus-simplify-subject-fuzzy-regexp
1272         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1273       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1274       (gnus-simplify-buffer-fuzzy-step
1275        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1276       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1277
1278     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1279     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1280     (gnus-simplify-buffer-fuzzy-step " $")
1281     (gnus-simplify-buffer-fuzzy-step "^ +")))
1282
1283 (defun gnus-simplify-subject-fuzzy (subject)
1284   "Simplify a subject string fuzzily.
1285 See `gnus-simplify-buffer-fuzzy' for details."
1286   (save-excursion
1287     (gnus-set-work-buffer)
1288     (let ((case-fold-search t))
1289       ;; Remove uninteresting prefixes.
1290       (when (and gnus-simplify-ignored-prefixes
1291                  (string-match gnus-simplify-ignored-prefixes subject))
1292         (setq subject (substring subject (match-end 0))))
1293       (insert subject)
1294       (inline (gnus-simplify-buffer-fuzzy))
1295       (buffer-string))))
1296
1297 (defsubst gnus-simplify-subject-fully (subject)
1298   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1299   (cond
1300    (gnus-simplify-subject-functions
1301     (gnus-map-function gnus-simplify-subject-functions subject))
1302    ((null gnus-summary-gather-subject-limit)
1303     (gnus-simplify-subject-re subject))
1304    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1305     (gnus-simplify-subject-fuzzy subject))
1306    ((numberp gnus-summary-gather-subject-limit)
1307     (gnus-limit-string (gnus-simplify-subject-re subject)
1308                        gnus-summary-gather-subject-limit))
1309    (t
1310     subject)))
1311
1312 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1313   "Check whether two subjects are equal.
1314 If optional argument simple-first is t, first argument is already
1315 simplified."
1316   (cond
1317    ((null simple-first)
1318     (equal (gnus-simplify-subject-fully s1)
1319            (gnus-simplify-subject-fully s2)))
1320    (t
1321     (equal s1
1322            (gnus-simplify-subject-fully s2)))))
1323
1324 (defun gnus-summary-bubble-group ()
1325   "Increase the score of the current group.
1326 This is a handy function to add to `gnus-summary-exit-hook' to
1327 increase the score of each group you read."
1328   (gnus-group-add-score gnus-newsgroup-name))
1329
1330 \f
1331 ;;;
1332 ;;; Gnus summary mode
1333 ;;;
1334
1335 (put 'gnus-summary-mode 'mode-class 'special)
1336
1337 (when t
1338   ;; Non-orthogonal keys
1339
1340   (gnus-define-keys gnus-summary-mode-map
1341     " " gnus-summary-next-page
1342     "\177" gnus-summary-prev-page
1343     [delete] gnus-summary-prev-page
1344     [backspace] gnus-summary-prev-page
1345     "\r" gnus-summary-scroll-up
1346     "\M-\r" gnus-summary-scroll-down
1347     "n" gnus-summary-next-unread-article
1348     "p" gnus-summary-prev-unread-article
1349     "N" gnus-summary-next-article
1350     "P" gnus-summary-prev-article
1351     "\M-\C-n" gnus-summary-next-same-subject
1352     "\M-\C-p" gnus-summary-prev-same-subject
1353     "\M-n" gnus-summary-next-unread-subject
1354     "\M-p" gnus-summary-prev-unread-subject
1355     "." gnus-summary-first-unread-article
1356     "," gnus-summary-best-unread-article
1357     "\M-s" gnus-summary-search-article-forward
1358     "\M-r" gnus-summary-search-article-backward
1359     "<" gnus-summary-beginning-of-article
1360     ">" gnus-summary-end-of-article
1361     "j" gnus-summary-goto-article
1362     "^" gnus-summary-refer-parent-article
1363     "\M-^" gnus-summary-refer-article
1364     "u" gnus-summary-tick-article-forward
1365     "!" gnus-summary-tick-article-forward
1366     "U" gnus-summary-tick-article-backward
1367     "d" gnus-summary-mark-as-read-forward
1368     "D" gnus-summary-mark-as-read-backward
1369     "E" gnus-summary-mark-as-expirable
1370     "\M-u" gnus-summary-clear-mark-forward
1371     "\M-U" gnus-summary-clear-mark-backward
1372     "k" gnus-summary-kill-same-subject-and-select
1373     "\C-k" gnus-summary-kill-same-subject
1374     "\M-\C-k" gnus-summary-kill-thread
1375     "\M-\C-l" gnus-summary-lower-thread
1376     "e" gnus-summary-edit-article
1377     "#" gnus-summary-mark-as-processable
1378     "\M-#" gnus-summary-unmark-as-processable
1379     "\M-\C-t" gnus-summary-toggle-threads
1380     "\M-\C-s" gnus-summary-show-thread
1381     "\M-\C-h" gnus-summary-hide-thread
1382     "\M-\C-f" gnus-summary-next-thread
1383     "\M-\C-b" gnus-summary-prev-thread
1384     [(meta down)] gnus-summary-next-thread
1385     [(meta up)] gnus-summary-prev-thread
1386     "\M-\C-u" gnus-summary-up-thread
1387     "\M-\C-d" gnus-summary-down-thread
1388     "&" gnus-summary-execute-command
1389     "c" gnus-summary-catchup-and-exit
1390     "\C-w" gnus-summary-mark-region-as-read
1391     "\C-t" gnus-summary-toggle-truncation
1392     "?" gnus-summary-mark-as-dormant
1393     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1394     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1395     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1396     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1397     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1398     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1399     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1400     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1401     "=" gnus-summary-expand-window
1402     "\C-x\C-s" gnus-summary-reselect-current-group
1403     "\M-g" gnus-summary-rescan-group
1404     "w" gnus-summary-stop-page-breaking
1405     "\C-c\C-r" gnus-summary-caesar-message
1406     "\M-t" gnus-summary-toggle-mime
1407     "f" gnus-summary-followup
1408     "F" gnus-summary-followup-with-original
1409     "C" gnus-summary-cancel-article
1410     "r" gnus-summary-reply
1411     "R" gnus-summary-reply-with-original
1412     "\C-c\C-f" gnus-summary-mail-forward
1413     "o" gnus-summary-save-article
1414     "\C-o" gnus-summary-save-article-mail
1415     "|" gnus-summary-pipe-output
1416     "\M-k" gnus-summary-edit-local-kill
1417     "\M-K" gnus-summary-edit-global-kill
1418     ;; "V" gnus-version
1419     "\C-c\C-d" gnus-summary-describe-group
1420     "q" gnus-summary-exit
1421     "Q" gnus-summary-exit-no-update
1422     "\C-c\C-i" gnus-info-find-node
1423     gnus-mouse-2 gnus-mouse-pick-article
1424     "m" gnus-summary-mail-other-window
1425     "a" gnus-summary-post-news
1426     "x" gnus-summary-limit-to-unread
1427     "s" gnus-summary-isearch-article
1428     "t" gnus-article-toggle-headers
1429     "g" gnus-summary-show-article
1430     "l" gnus-summary-goto-last-article
1431     "v" gnus-summary-preview-mime-message
1432     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1433     "\C-d" gnus-summary-enter-digest-group
1434     "\M-\C-d" gnus-summary-read-document
1435     "\M-\C-e" gnus-summary-edit-parameters
1436     "\M-\C-a" gnus-summary-customize-parameters
1437     "\C-c\C-b" gnus-bug
1438     "*" gnus-cache-enter-article
1439     "\M-*" gnus-cache-remove-article
1440     "\M-&" gnus-summary-universal-argument
1441     "\C-l" gnus-recenter
1442     "I" gnus-summary-increase-score
1443     "L" gnus-summary-lower-score
1444     "\M-i" gnus-symbolic-argument
1445     "h" gnus-summary-select-article-buffer
1446
1447     "V" gnus-summary-score-map
1448     "X" gnus-uu-extract-map
1449     "S" gnus-summary-send-map)
1450
1451   ;; Sort of orthogonal keymap
1452   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1453     "t" gnus-summary-tick-article-forward
1454     "!" gnus-summary-tick-article-forward
1455     "d" gnus-summary-mark-as-read-forward
1456     "r" gnus-summary-mark-as-read-forward
1457     "c" gnus-summary-clear-mark-forward
1458     " " gnus-summary-clear-mark-forward
1459     "e" gnus-summary-mark-as-expirable
1460     "x" gnus-summary-mark-as-expirable
1461     "?" gnus-summary-mark-as-dormant
1462     "b" gnus-summary-set-bookmark
1463     "B" gnus-summary-remove-bookmark
1464     "#" gnus-summary-mark-as-processable
1465     "\M-#" gnus-summary-unmark-as-processable
1466     "S" gnus-summary-limit-include-expunged
1467     "C" gnus-summary-catchup
1468     "H" gnus-summary-catchup-to-here
1469     "\C-c" gnus-summary-catchup-all
1470     "k" gnus-summary-kill-same-subject-and-select
1471     "K" gnus-summary-kill-same-subject
1472     "P" gnus-uu-mark-map)
1473
1474   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1475     "c" gnus-summary-clear-above
1476     "u" gnus-summary-tick-above
1477     "m" gnus-summary-mark-above
1478     "k" gnus-summary-kill-below)
1479
1480   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1481     "/" gnus-summary-limit-to-subject
1482     "n" gnus-summary-limit-to-articles
1483     "w" gnus-summary-pop-limit
1484     "s" gnus-summary-limit-to-subject
1485     "a" gnus-summary-limit-to-author
1486     "u" gnus-summary-limit-to-unread
1487     "m" gnus-summary-limit-to-marks
1488     "M" gnus-summary-limit-exclude-marks
1489     "v" gnus-summary-limit-to-score
1490     "*" gnus-summary-limit-include-cached
1491     "D" gnus-summary-limit-include-dormant
1492     "T" gnus-summary-limit-include-thread
1493     "d" gnus-summary-limit-exclude-dormant
1494     "t" gnus-summary-limit-to-age
1495     "x" gnus-summary-limit-to-extra
1496     "E" gnus-summary-limit-include-expunged
1497     "c" gnus-summary-limit-exclude-childless-dormant
1498     "C" gnus-summary-limit-mark-excluded-as-read)
1499
1500   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1501     "n" gnus-summary-next-unread-article
1502     "p" gnus-summary-prev-unread-article
1503     "N" gnus-summary-next-article
1504     "P" gnus-summary-prev-article
1505     "\C-n" gnus-summary-next-same-subject
1506     "\C-p" gnus-summary-prev-same-subject
1507     "\M-n" gnus-summary-next-unread-subject
1508     "\M-p" gnus-summary-prev-unread-subject
1509     "f" gnus-summary-first-unread-article
1510     "b" gnus-summary-best-unread-article
1511     "j" gnus-summary-goto-article
1512     "g" gnus-summary-goto-subject
1513     "l" gnus-summary-goto-last-article
1514     "o" gnus-summary-pop-article)
1515
1516   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1517     "k" gnus-summary-kill-thread
1518     "l" gnus-summary-lower-thread
1519     "i" gnus-summary-raise-thread
1520     "T" gnus-summary-toggle-threads
1521     "t" gnus-summary-rethread-current
1522     "^" gnus-summary-reparent-thread
1523     "s" gnus-summary-show-thread
1524     "S" gnus-summary-show-all-threads
1525     "h" gnus-summary-hide-thread
1526     "H" gnus-summary-hide-all-threads
1527     "n" gnus-summary-next-thread
1528     "p" gnus-summary-prev-thread
1529     "u" gnus-summary-up-thread
1530     "o" gnus-summary-top-thread
1531     "d" gnus-summary-down-thread
1532     "#" gnus-uu-mark-thread
1533     "\M-#" gnus-uu-unmark-thread)
1534
1535   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1536     "g" gnus-summary-prepare
1537     "c" gnus-summary-insert-cached-articles)
1538
1539   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1540     "c" gnus-summary-catchup-and-exit
1541     "C" gnus-summary-catchup-all-and-exit
1542     "E" gnus-summary-exit-no-update
1543     "J" gnus-summary-jump-to-other-group
1544     "Q" gnus-summary-exit
1545     "Z" gnus-summary-exit
1546     "n" gnus-summary-catchup-and-goto-next-group
1547     "R" gnus-summary-reselect-current-group
1548     "G" gnus-summary-rescan-group
1549     "N" gnus-summary-next-group
1550     "s" gnus-summary-save-newsrc
1551     "P" gnus-summary-prev-group)
1552
1553   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1554     " " gnus-summary-next-page
1555     "n" gnus-summary-next-page
1556     "\177" gnus-summary-prev-page
1557     [delete] gnus-summary-prev-page
1558     "p" gnus-summary-prev-page
1559     "\r" gnus-summary-scroll-up
1560     "\M-\r" gnus-summary-scroll-down
1561     "<" gnus-summary-beginning-of-article
1562     ">" gnus-summary-end-of-article
1563     "b" gnus-summary-beginning-of-article
1564     "e" gnus-summary-end-of-article
1565     "^" gnus-summary-refer-parent-article
1566     "r" gnus-summary-refer-parent-article
1567     "D" gnus-summary-enter-digest-group
1568     "R" gnus-summary-refer-references
1569     "T" gnus-summary-refer-thread
1570     "g" gnus-summary-show-article
1571     "s" gnus-summary-isearch-article
1572     "P" gnus-summary-print-article
1573     "M" gnus-mailing-list-insinuate
1574     "t" gnus-article-babel)
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              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1820              ["Redisplay" gnus-summary-show-article t])))
1821       (easy-menu-define
1822        gnus-summary-article-menu gnus-summary-mode-map ""
1823        (cons "Article" innards))
1824
1825       (easy-menu-define
1826        gnus-article-commands-menu gnus-article-mode-map ""
1827        (cons "Commands" innards)))
1828
1829     (easy-menu-define
1830      gnus-summary-thread-menu gnus-summary-mode-map ""
1831      '("Threads"
1832        ["Toggle threading" gnus-summary-toggle-threads t]
1833        ["Hide threads" gnus-summary-hide-all-threads t]
1834        ["Show threads" gnus-summary-show-all-threads t]
1835        ["Hide thread" gnus-summary-hide-thread t]
1836        ["Show thread" gnus-summary-show-thread t]
1837        ["Go to next thread" gnus-summary-next-thread t]
1838        ["Go to previous thread" gnus-summary-prev-thread t]
1839        ["Go down thread" gnus-summary-down-thread t]
1840        ["Go up thread" gnus-summary-up-thread t]
1841        ["Top of thread" gnus-summary-top-thread t]
1842        ["Mark thread as read" gnus-summary-kill-thread t]
1843        ["Lower thread score" gnus-summary-lower-thread t]
1844        ["Raise thread score" gnus-summary-raise-thread t]
1845        ["Rethread current" gnus-summary-rethread-current t]))
1846
1847     (easy-menu-define
1848      gnus-summary-post-menu gnus-summary-mode-map ""
1849      '("Post"
1850        ["Post an article" gnus-summary-post-news t]
1851        ["Followup" gnus-summary-followup t]
1852        ["Followup and yank" gnus-summary-followup-with-original t]
1853        ["Supersede article" gnus-summary-supersede-article t]
1854        ["Cancel article" gnus-summary-cancel-article t]
1855        ["Reply" gnus-summary-reply t]
1856        ["Reply and yank" gnus-summary-reply-with-original t]
1857        ["Wide reply" gnus-summary-wide-reply t]
1858        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1859        ["Mail forward" gnus-summary-mail-forward t]
1860        ["Post forward" gnus-summary-post-forward t]
1861        ["Digest and mail" gnus-uu-digest-mail-forward t]
1862        ["Digest and post" gnus-uu-digest-post-forward t]
1863        ["Resend message" gnus-summary-resend-message t]
1864        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1865        ["Send a mail" gnus-summary-mail-other-window t]
1866        ["Uuencode and post" gnus-uu-post-news t]
1867        ["Followup via news" gnus-summary-followup-to-mail t]
1868        ["Followup via news and yank"
1869         gnus-summary-followup-to-mail-with-original t]
1870        ;;("Draft"
1871        ;;["Send" gnus-summary-send-draft t]
1872        ;;["Send bounced" gnus-resend-bounced-mail t])
1873        ))
1874
1875     (easy-menu-define
1876      gnus-summary-misc-menu gnus-summary-mode-map ""
1877      '("Misc"
1878        ("Mark Read"
1879         ["Mark as read" gnus-summary-mark-as-read-forward t]
1880         ["Mark same subject and select"
1881          gnus-summary-kill-same-subject-and-select t]
1882         ["Mark same subject" gnus-summary-kill-same-subject t]
1883         ["Catchup" gnus-summary-catchup t]
1884         ["Catchup all" gnus-summary-catchup-all t]
1885         ["Catchup to here" gnus-summary-catchup-to-here t]
1886         ["Catchup region" gnus-summary-mark-region-as-read t]
1887         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1888        ("Mark Various"
1889         ["Tick" gnus-summary-tick-article-forward t]
1890         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1891         ["Remove marks" gnus-summary-clear-mark-forward t]
1892         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1893         ["Set bookmark" gnus-summary-set-bookmark t]
1894         ["Remove bookmark" gnus-summary-remove-bookmark t])
1895        ("Mark Limit"
1896         ["Marks..." gnus-summary-limit-to-marks t]
1897         ["Subject..." gnus-summary-limit-to-subject t]
1898         ["Author..." gnus-summary-limit-to-author t]
1899         ["Age..." gnus-summary-limit-to-age t]
1900         ["Extra..." gnus-summary-limit-to-extra t]
1901         ["Score" gnus-summary-limit-to-score t]
1902         ["Unread" gnus-summary-limit-to-unread t]
1903         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1904         ["Articles" gnus-summary-limit-to-articles t]
1905         ["Pop limit" gnus-summary-pop-limit t]
1906         ["Show dormant" gnus-summary-limit-include-dormant t]
1907         ["Hide childless dormant"
1908          gnus-summary-limit-exclude-childless-dormant t]
1909         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1910         ["Hide marked" gnus-summary-limit-exclude-marks t]
1911         ["Show expunged" gnus-summary-show-all-expunged t])
1912        ("Process Mark"
1913         ["Set mark" gnus-summary-mark-as-processable t]
1914         ["Remove mark" gnus-summary-unmark-as-processable t]
1915         ["Remove all marks" gnus-summary-unmark-all-processable t]
1916         ["Mark above" gnus-uu-mark-over t]
1917         ["Mark series" gnus-uu-mark-series t]
1918         ["Mark region" gnus-uu-mark-region t]
1919         ["Unmark region" gnus-uu-unmark-region t]
1920         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1921         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1922         ["Mark all" gnus-uu-mark-all t]
1923         ["Mark buffer" gnus-uu-mark-buffer t]
1924         ["Mark sparse" gnus-uu-mark-sparse t]
1925         ["Mark thread" gnus-uu-mark-thread t]
1926         ["Unmark thread" gnus-uu-unmark-thread t]
1927         ("Process Mark Sets"
1928          ["Kill" gnus-summary-kill-process-mark t]
1929          ["Yank" gnus-summary-yank-process-mark
1930           gnus-newsgroup-process-stack]
1931          ["Save" gnus-summary-save-process-mark t]))
1932        ("Scroll article"
1933         ["Page forward" gnus-summary-next-page t]
1934         ["Page backward" gnus-summary-prev-page t]
1935         ["Line forward" gnus-summary-scroll-up t])
1936        ("Move"
1937         ["Next unread article" gnus-summary-next-unread-article t]
1938         ["Previous unread article" gnus-summary-prev-unread-article t]
1939         ["Next article" gnus-summary-next-article t]
1940         ["Previous article" gnus-summary-prev-article t]
1941         ["Next unread subject" gnus-summary-next-unread-subject t]
1942         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1943         ["Next article same subject" gnus-summary-next-same-subject t]
1944         ["Previous article same subject" gnus-summary-prev-same-subject t]
1945         ["First unread article" gnus-summary-first-unread-article t]
1946         ["Best unread article" gnus-summary-best-unread-article t]
1947         ["Go to subject number..." gnus-summary-goto-subject t]
1948         ["Go to article number..." gnus-summary-goto-article t]
1949         ["Go to the last article" gnus-summary-goto-last-article t]
1950         ["Pop article off history" gnus-summary-pop-article t])
1951        ("Sort"
1952         ["Sort by number" gnus-summary-sort-by-number t]
1953         ["Sort by author" gnus-summary-sort-by-author t]
1954         ["Sort by subject" gnus-summary-sort-by-subject t]
1955         ["Sort by date" gnus-summary-sort-by-date t]
1956         ["Sort by score" gnus-summary-sort-by-score t]
1957         ["Sort by lines" gnus-summary-sort-by-lines t]
1958         ["Sort by characters" gnus-summary-sort-by-chars t])
1959        ("Help"
1960         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1961         ["Describe group" gnus-summary-describe-group t]
1962         ["Read manual" gnus-info-find-node t])
1963        ("Modes"
1964         ["Pick and read" gnus-pick-mode t]
1965         ["Binary" gnus-binary-mode t])
1966        ("Regeneration"
1967         ["Regenerate" gnus-summary-prepare t]
1968         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1969         ["Toggle threading" gnus-summary-toggle-threads t])
1970        ["Filter articles..." gnus-summary-execute-command t]
1971        ["Run command on subjects..." gnus-summary-universal-argument t]
1972        ["Search articles forward..." gnus-summary-search-article-forward t]
1973        ["Search articles backward..." gnus-summary-search-article-backward t]
1974        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1975        ["Expand window" gnus-summary-expand-window t]
1976        ["Expire expirable articles" gnus-summary-expire-articles
1977         (gnus-check-backend-function
1978          'request-expire-articles gnus-newsgroup-name)]
1979        ["Edit local kill file" gnus-summary-edit-local-kill t]
1980        ["Edit main kill file" gnus-summary-edit-global-kill t]
1981        ["Edit group parameters" gnus-summary-edit-parameters t]
1982        ["Customize group parameters" gnus-summary-customize-parameters t]
1983        ["Send a bug report" gnus-bug t]
1984        ("Exit"
1985         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1986         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1987         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1988         ["Exit group" gnus-summary-exit t]
1989         ["Exit group without updating" gnus-summary-exit-no-update t]
1990         ["Exit and goto next group" gnus-summary-next-group t]
1991         ["Exit and goto prev group" gnus-summary-prev-group t]
1992         ["Reselect group" gnus-summary-reselect-current-group t]
1993         ["Rescan group" gnus-summary-rescan-group t]
1994         ["Update dribble" gnus-summary-save-newsrc t])))
1995
1996     (gnus-run-hooks 'gnus-summary-menu-hook)))
1997
1998 (defun gnus-score-set-default (var value)
1999   "A version of set that updates the GNU Emacs menu-bar."
2000   (set var value)
2001   ;; It is the message that forces the active status to be updated.
2002   (message ""))
2003
2004 (defun gnus-make-score-map (type)
2005   "Make a summary score map of type TYPE."
2006   (if t
2007       nil
2008     (let ((headers '(("author" "from" string)
2009                      ("subject" "subject" string)
2010                      ("article body" "body" string)
2011                      ("article head" "head" string)
2012                      ("xref" "xref" string)
2013                      ("extra header" "extra" string)
2014                      ("lines" "lines" number)
2015                      ("followups to author" "followup" string)))
2016           (types '((number ("less than" <)
2017                            ("greater than" >)
2018                            ("equal" =))
2019                    (string ("substring" s)
2020                            ("exact string" e)
2021                            ("fuzzy string" f)
2022                            ("regexp" r))))
2023           (perms '(("temporary" (current-time-string))
2024                    ("permanent" nil)
2025                    ("immediate" now)))
2026           header)
2027       (list
2028        (apply
2029         'nconc
2030         (list
2031          (if (eq type 'lower)
2032              "Lower score"
2033            "Increase score"))
2034         (let (outh)
2035           (while headers
2036             (setq header (car headers))
2037             (setq outh
2038                   (cons
2039                    (apply
2040                     'nconc
2041                     (list (car header))
2042                     (let ((ts (cdr (assoc (nth 2 header) types)))
2043                           outt)
2044                       (while ts
2045                         (setq outt
2046                               (cons
2047                                (apply
2048                                 'nconc
2049                                 (list (caar ts))
2050                                 (let ((ps perms)
2051                                       outp)
2052                                   (while ps
2053                                     (setq outp
2054                                           (cons
2055                                            (vector
2056                                             (caar ps)
2057                                             (list
2058                                              'gnus-summary-score-entry
2059                                              (nth 1 header)
2060                                              (if (or (string= (nth 1 header)
2061                                                               "head")
2062                                                      (string= (nth 1 header)
2063                                                               "body"))
2064                                                  ""
2065                                                (list 'gnus-summary-header
2066                                                      (nth 1 header)))
2067                                              (list 'quote (nth 1 (car ts)))
2068                                              (list 'gnus-score-delta-default
2069                                                    nil)
2070                                              (nth 1 (car ps))
2071                                              t)
2072                                             t)
2073                                            outp))
2074                                     (setq ps (cdr ps)))
2075                                   (list (nreverse outp))))
2076                                outt))
2077                         (setq ts (cdr ts)))
2078                       (list (nreverse outt))))
2079                    outh))
2080             (setq headers (cdr headers)))
2081           (list (nreverse outh))))))))
2082
2083 \f
2084
2085 (defun gnus-summary-mode (&optional group)
2086   "Major mode for reading articles.
2087
2088 All normal editing commands are switched off.
2089 \\<gnus-summary-mode-map>
2090 Each line in this buffer represents one article.  To read an
2091 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2092 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2093 respectively.
2094
2095 You can also post articles and send mail from this buffer.  To
2096 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2097 of an article, type `\\[gnus-summary-reply]'.
2098
2099 There are approx. one gazillion commands you can execute in this
2100 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2101
2102 The following commands are available:
2103
2104 \\{gnus-summary-mode-map}"
2105   (interactive)
2106   (when (gnus-visual-p 'summary-menu 'menu)
2107     (gnus-summary-make-menu-bar))
2108   (kill-all-local-variables)
2109   (gnus-summary-make-local-variables)
2110   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2111     (gnus-summary-make-local-variables))
2112   (gnus-make-thread-indent-array)
2113   (gnus-simplify-mode-line)
2114   (setq major-mode 'gnus-summary-mode)
2115   (setq mode-name "Summary")
2116   (make-local-variable 'minor-mode-alist)
2117   (use-local-map gnus-summary-mode-map)
2118   (buffer-disable-undo)
2119   (setq buffer-read-only t)             ;Disable modification
2120   (setq truncate-lines t)
2121   (setq selective-display t)
2122   (setq selective-display-ellipses t)   ;Display `...'
2123   (gnus-summary-set-display-table)
2124   (gnus-set-default-directory)
2125   (setq gnus-newsgroup-name group)
2126   (unless (gnus-news-group-p group)
2127     (setq gnus-newsgroup-incorporated
2128           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2129   (make-local-variable 'gnus-summary-line-format)
2130   (make-local-variable 'gnus-summary-line-format-spec)
2131   (make-local-variable 'gnus-summary-dummy-line-format)
2132   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2133   (make-local-variable 'gnus-summary-mark-positions)
2134   (make-local-hook 'pre-command-hook)
2135   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2136   (gnus-run-hooks 'gnus-summary-mode-hook)
2137   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2138   (gnus-update-summary-mark-positions))
2139
2140 (defun gnus-summary-make-local-variables ()
2141   "Make all the local summary buffer variables."
2142   (let (global)
2143     (dolist (local gnus-summary-local-variables)
2144       (if (consp local)
2145           (progn
2146             (if (eq (cdr local) 'global)
2147                 ;; Copy the global value of the variable.
2148                 (setq global (symbol-value (car local)))
2149               ;; Use the value from the list.
2150               (setq global (eval (cdr local))))
2151             (set (make-local-variable (car local)) global))
2152         ;; Simple nil-valued local variable.
2153         (set (make-local-variable local) nil)))))
2154
2155 (defun gnus-summary-clear-local-variables ()
2156   (let ((locals gnus-summary-local-variables))
2157     (while locals
2158       (if (consp (car locals))
2159           (and (vectorp (caar locals))
2160                (set (caar locals) nil))
2161         (and (vectorp (car locals))
2162              (set (car locals) nil)))
2163       (setq locals (cdr locals)))))
2164
2165 ;; Summary data functions.
2166
2167 (defmacro gnus-data-number (data)
2168   `(car ,data))
2169
2170 (defmacro gnus-data-set-number (data number)
2171   `(setcar ,data ,number))
2172
2173 (defmacro gnus-data-mark (data)
2174   `(nth 1 ,data))
2175
2176 (defmacro gnus-data-set-mark (data mark)
2177   `(setcar (nthcdr 1 ,data) ,mark))
2178
2179 (defmacro gnus-data-pos (data)
2180   `(nth 2 ,data))
2181
2182 (defmacro gnus-data-set-pos (data pos)
2183   `(setcar (nthcdr 2 ,data) ,pos))
2184
2185 (defmacro gnus-data-header (data)
2186   `(nth 3 ,data))
2187
2188 (defmacro gnus-data-set-header (data header)
2189   `(setcar (nthcdr 3 ,data) ,header))
2190
2191 (defmacro gnus-data-level (data)
2192   `(nth 4 ,data))
2193
2194 (defmacro gnus-data-unread-p (data)
2195   `(= (nth 1 ,data) gnus-unread-mark))
2196
2197 (defmacro gnus-data-read-p (data)
2198   `(/= (nth 1 ,data) gnus-unread-mark))
2199
2200 (defmacro gnus-data-pseudo-p (data)
2201   `(consp (nth 3 ,data)))
2202
2203 (defmacro gnus-data-find (number)
2204   `(assq ,number gnus-newsgroup-data))
2205
2206 (defmacro gnus-data-find-list (number &optional data)
2207   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2208      (memq (assq ,number bdata)
2209            bdata)))
2210
2211 (defmacro gnus-data-make (number mark pos header level)
2212   `(list ,number ,mark ,pos ,header ,level))
2213
2214 (defun gnus-data-enter (after-article number mark pos header level offset)
2215   (let ((data (gnus-data-find-list after-article)))
2216     (unless data
2217       (error "No such article: %d" after-article))
2218     (setcdr data (cons (gnus-data-make number mark pos header level)
2219                        (cdr data)))
2220     (setq gnus-newsgroup-data-reverse nil)
2221     (gnus-data-update-list (cddr data) offset)))
2222
2223 (defun gnus-data-enter-list (after-article list &optional offset)
2224   (when list
2225     (let ((data (and after-article (gnus-data-find-list after-article)))
2226           (ilist list))
2227       (if (not (or data
2228                    after-article))
2229           (let ((odata gnus-newsgroup-data))
2230             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2231             (when offset
2232               (gnus-data-update-list odata offset)))
2233         ;; Find the last element in the list to be spliced into the main
2234         ;; list.
2235         (while (cdr list)
2236           (setq list (cdr list)))
2237         (if (not data)
2238             (progn
2239               (setcdr list gnus-newsgroup-data)
2240               (setq gnus-newsgroup-data ilist)
2241               (when offset
2242                 (gnus-data-update-list (cdr list) offset)))
2243           (setcdr list (cdr data))
2244           (setcdr data ilist)
2245           (when offset
2246             (gnus-data-update-list (cdr list) offset))))
2247       (setq gnus-newsgroup-data-reverse nil))))
2248
2249 (defun gnus-data-remove (article &optional offset)
2250   (let ((data gnus-newsgroup-data))
2251     (if (= (gnus-data-number (car data)) article)
2252         (progn
2253           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2254                 gnus-newsgroup-data-reverse nil)
2255           (when offset
2256             (gnus-data-update-list gnus-newsgroup-data offset)))
2257       (while (cdr data)
2258         (when (= (gnus-data-number (cadr data)) article)
2259           (setcdr data (cddr data))
2260           (when offset
2261             (gnus-data-update-list (cdr data) offset))
2262           (setq data nil
2263                 gnus-newsgroup-data-reverse nil))
2264         (setq data (cdr data))))))
2265
2266 (defmacro gnus-data-list (backward)
2267   `(if ,backward
2268        (or gnus-newsgroup-data-reverse
2269            (setq gnus-newsgroup-data-reverse
2270                  (reverse gnus-newsgroup-data)))
2271      gnus-newsgroup-data))
2272
2273 (defun gnus-data-update-list (data offset)
2274   "Add OFFSET to the POS of all data entries in DATA."
2275   (setq gnus-newsgroup-data-reverse nil)
2276   (while data
2277     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2278     (setq data (cdr data))))
2279
2280 (defun gnus-summary-article-pseudo-p (article)
2281   "Say whether this article is a pseudo article or not."
2282   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2283
2284 (defmacro gnus-summary-article-sparse-p (article)
2285   "Say whether this article is a sparse article or not."
2286   `(memq ,article gnus-newsgroup-sparse))
2287
2288 (defmacro gnus-summary-article-ancient-p (article)
2289   "Say whether this article is a sparse article or not."
2290   `(memq ,article gnus-newsgroup-ancient))
2291
2292 (defun gnus-article-parent-p (number)
2293   "Say whether this article is a parent or not."
2294   (let ((data (gnus-data-find-list number)))
2295     (and (cdr data)                     ; There has to be an article after...
2296          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2297             (gnus-data-level (nth 1 data))))))
2298
2299 (defun gnus-article-children (number)
2300   "Return a list of all children to NUMBER."
2301   (let* ((data (gnus-data-find-list number))
2302          (level (gnus-data-level (car data)))
2303          children)
2304     (setq data (cdr data))
2305     (while (and data
2306                 (= (gnus-data-level (car data)) (1+ level)))
2307       (push (gnus-data-number (car data)) children)
2308       (setq data (cdr data)))
2309     children))
2310
2311 (defmacro gnus-summary-skip-intangible ()
2312   "If the current article is intangible, then jump to a different article."
2313   '(let ((to (get-text-property (point) 'gnus-intangible)))
2314      (and to (gnus-summary-goto-subject to))))
2315
2316 (defmacro gnus-summary-article-intangible-p ()
2317   "Say whether this article is intangible or not."
2318   '(get-text-property (point) 'gnus-intangible))
2319
2320 (defun gnus-article-read-p (article)
2321   "Say whether ARTICLE is read or not."
2322   (not (or (memq article gnus-newsgroup-marked)
2323            (memq article gnus-newsgroup-unreads)
2324            (memq article gnus-newsgroup-unselected)
2325            (memq article gnus-newsgroup-dormant))))
2326
2327 ;; Some summary mode macros.
2328
2329 (defmacro gnus-summary-article-number ()
2330   "The article number of the article on the current line.
2331 If there isn's an article number here, then we return the current
2332 article number."
2333   '(progn
2334      (gnus-summary-skip-intangible)
2335      (or (get-text-property (point) 'gnus-number)
2336          (gnus-summary-last-subject))))
2337
2338 (defmacro gnus-summary-article-header (&optional number)
2339   "Return the header of article NUMBER."
2340   `(gnus-data-header (gnus-data-find
2341                       ,(or number '(gnus-summary-article-number)))))
2342
2343 (defmacro gnus-summary-thread-level (&optional number)
2344   "Return the level of thread that starts with article NUMBER."
2345   `(if (and (eq gnus-summary-make-false-root 'dummy)
2346             (get-text-property (point) 'gnus-intangible))
2347        0
2348      (gnus-data-level (gnus-data-find
2349                        ,(or number '(gnus-summary-article-number))))))
2350
2351 (defmacro gnus-summary-article-mark (&optional number)
2352   "Return the mark of article NUMBER."
2353   `(gnus-data-mark (gnus-data-find
2354                     ,(or number '(gnus-summary-article-number)))))
2355
2356 (defmacro gnus-summary-article-pos (&optional number)
2357   "Return the position of the line of article NUMBER."
2358   `(gnus-data-pos (gnus-data-find
2359                    ,(or number '(gnus-summary-article-number)))))
2360
2361 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2362 (defmacro gnus-summary-article-subject (&optional number)
2363   "Return current subject string or nil if nothing."
2364   `(let ((headers
2365           ,(if number
2366                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2367              '(gnus-data-header (assq (gnus-summary-article-number)
2368                                       gnus-newsgroup-data)))))
2369      (and headers
2370           (vectorp headers)
2371           (mail-header-subject headers))))
2372
2373 (defmacro gnus-summary-article-score (&optional number)
2374   "Return current article score."
2375   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2376                   gnus-newsgroup-scored))
2377        gnus-summary-default-score 0))
2378
2379 (defun gnus-summary-article-children (&optional number)
2380   "Return a list of article numbers that are children of article NUMBER."
2381   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2382          (level (gnus-data-level (car data)))
2383          l children)
2384     (while (and (setq data (cdr data))
2385                 (> (setq l (gnus-data-level (car data))) level))
2386       (and (= (1+ level) l)
2387            (push (gnus-data-number (car data))
2388                  children)))
2389     (nreverse children)))
2390
2391 (defun gnus-summary-article-parent (&optional number)
2392   "Return the article number of the parent of article NUMBER."
2393   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2394                                     (gnus-data-list t)))
2395          (level (gnus-data-level (car data))))
2396     (if (zerop level)
2397         ()                              ; This is a root.
2398       ;; We search until we find an article with a level less than
2399       ;; this one.  That function has to be the parent.
2400       (while (and (setq data (cdr data))
2401                   (not (< (gnus-data-level (car data)) level))))
2402       (and data (gnus-data-number (car data))))))
2403
2404 (defun gnus-unread-mark-p (mark)
2405   "Say whether MARK is the unread mark."
2406   (= mark gnus-unread-mark))
2407
2408 (defun gnus-read-mark-p (mark)
2409   "Say whether MARK is one of the marks that mark as read.
2410 This is all marks except unread, ticked, dormant, and expirable."
2411   (not (or (= mark gnus-unread-mark)
2412            (= mark gnus-ticked-mark)
2413            (= mark gnus-dormant-mark)
2414            (= mark gnus-expirable-mark))))
2415
2416 (defmacro gnus-article-mark (number)
2417   "Return the MARK of article NUMBER.
2418 This macro should only be used when computing the mark the \"first\"
2419 time; i.e., when generating the summary lines.  After that,
2420 `gnus-summary-article-mark' should be used to examine the
2421 marks of articles."
2422   `(cond
2423     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2424     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2425     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2426     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2427     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2428     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2429     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2430     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2431            gnus-ancient-mark))))
2432
2433 ;; Saving hidden threads.
2434
2435 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2436 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2437
2438 (defmacro gnus-save-hidden-threads (&rest forms)
2439   "Save hidden threads, eval FORMS, and restore the hidden threads."
2440   (let ((config (make-symbol "config")))
2441     `(let ((,config (gnus-hidden-threads-configuration)))
2442        (unwind-protect
2443            (save-excursion
2444              ,@forms)
2445          (gnus-restore-hidden-threads-configuration ,config)))))
2446
2447 (defun gnus-data-compute-positions ()
2448   "Compute the positions of all articles."
2449   (setq gnus-newsgroup-data-reverse nil)
2450   (let ((data gnus-newsgroup-data))
2451     (save-excursion
2452       (gnus-save-hidden-threads
2453         (gnus-summary-show-all-threads)
2454         (goto-char (point-min))
2455         (while data
2456           (while (get-text-property (point) 'gnus-intangible)
2457             (forward-line 1))
2458           (gnus-data-set-pos (car data) (+ (point) 3))
2459           (setq data (cdr data))
2460           (forward-line 1))))))
2461
2462 (defun gnus-hidden-threads-configuration ()
2463   "Return the current hidden threads configuration."
2464   (save-excursion
2465     (let (config)
2466       (goto-char (point-min))
2467       (while (search-forward "\r" nil t)
2468         (push (1- (point)) config))
2469       config)))
2470
2471 (defun gnus-restore-hidden-threads-configuration (config)
2472   "Restore hidden threads configuration from CONFIG."
2473   (save-excursion
2474     (let (point buffer-read-only)
2475       (while (setq point (pop config))
2476         (when (and (< point (point-max))
2477                    (goto-char point)
2478                    (eq (char-after) ?\n))
2479           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2480
2481 ;; Various summary mode internalish functions.
2482
2483 (defun gnus-mouse-pick-article (e)
2484   (interactive "e")
2485   (mouse-set-point e)
2486   (gnus-summary-next-page nil t))
2487
2488 (defun gnus-summary-set-display-table ()
2489   "Change the display table.
2490 Odd characters have a tendency to mess
2491 up nicely formatted displays - we make all possible glyphs
2492 display only a single character."
2493
2494   ;; We start from the standard display table, if any.
2495   (let ((table (or (copy-sequence standard-display-table)
2496                    (make-display-table)))
2497         (i 32))
2498     ;; Nix out all the control chars...
2499     (while (>= (setq i (1- i)) 0)
2500       (aset table i [??]))
2501     ;; ... but not newline and cr, of course.  (cr is necessary for the
2502     ;; selective display).
2503     (aset table ?\n nil)
2504     (aset table ?\r nil)
2505     ;; We keep TAB as well.
2506     (aset table ?\t nil)
2507     ;; We nix out any glyphs over 126 that are not set already.
2508     (let ((i 256))
2509       (while (>= (setq i (1- i)) 127)
2510         ;; Only modify if the entry is nil.
2511         (unless (aref table i)
2512           (aset table i [??]))))
2513     (setq buffer-display-table table)))
2514
2515 (defun gnus-summary-setup-buffer (group)
2516   "Initialize summary buffer."
2517   (let ((buffer (concat "*Summary " group "*")))
2518     (if (get-buffer buffer)
2519         (progn
2520           (set-buffer buffer)
2521           (setq gnus-summary-buffer (current-buffer))
2522           (not gnus-newsgroup-prepared))
2523       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2524       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2525       (gnus-summary-mode group)
2526       (when gnus-carpal
2527         (gnus-carpal-setup-buffer 'summary))
2528       (unless gnus-single-article-buffer
2529         (make-local-variable 'gnus-article-buffer)
2530         (make-local-variable 'gnus-article-current)
2531         (make-local-variable 'gnus-original-article-buffer))
2532       (setq gnus-newsgroup-name group)
2533       t)))
2534
2535 (defun gnus-set-global-variables ()
2536   "Set the global equivalents of the buffer-local variables.
2537 They are set to the latest values they had.  These reflect the summary
2538 buffer that was in action when the last article was fetched."
2539   (when (eq major-mode 'gnus-summary-mode)
2540     (setq gnus-summary-buffer (current-buffer))
2541     (let ((name gnus-newsgroup-name)
2542           (marked gnus-newsgroup-marked)
2543           (unread gnus-newsgroup-unreads)
2544           (headers gnus-current-headers)
2545           (data gnus-newsgroup-data)
2546           (summary gnus-summary-buffer)
2547           (article-buffer gnus-article-buffer)
2548           (original gnus-original-article-buffer)
2549           (gac gnus-article-current)
2550           (reffed gnus-reffed-article-number)
2551           (score-file gnus-current-score-file)
2552           (default-charset gnus-newsgroup-charset)
2553           vlist)
2554       (let ((locals gnus-newsgroup-variables))
2555         (while locals
2556           (if (consp (car locals))
2557               (push (eval (caar locals)) vlist)
2558             (push (eval (car locals)) vlist))
2559           (setq locals (cdr locals)))
2560         (setq vlist (nreverse vlist)))
2561       (save-excursion
2562         (set-buffer gnus-group-buffer)
2563         (setq gnus-newsgroup-name name
2564               gnus-newsgroup-marked marked
2565               gnus-newsgroup-unreads unread
2566               gnus-current-headers headers
2567               gnus-newsgroup-data data
2568               gnus-article-current gac
2569               gnus-summary-buffer summary
2570               gnus-article-buffer article-buffer
2571               gnus-original-article-buffer original
2572               gnus-reffed-article-number reffed
2573               gnus-current-score-file score-file
2574               gnus-newsgroup-charset default-charset)
2575         (let ((locals gnus-newsgroup-variables))
2576           (while locals
2577             (if (consp (car locals))
2578                 (set (caar locals) (pop vlist))
2579               (set (car locals) (pop vlist)))
2580             (setq locals (cdr locals))))
2581         ;; The article buffer also has local variables.
2582         (when (gnus-buffer-live-p gnus-article-buffer)
2583           (set-buffer gnus-article-buffer)
2584           (setq gnus-summary-buffer summary))))))
2585
2586 (defun gnus-summary-article-unread-p (article)
2587   "Say whether ARTICLE is unread or not."
2588   (memq article gnus-newsgroup-unreads))
2589
2590 (defun gnus-summary-first-article-p (&optional article)
2591   "Return whether ARTICLE is the first article in the buffer."
2592   (if (not (setq article (or article (gnus-summary-article-number))))
2593       nil
2594     (eq article (caar gnus-newsgroup-data))))
2595
2596 (defun gnus-summary-last-article-p (&optional article)
2597   "Return whether ARTICLE is the last article in the buffer."
2598   (if (not (setq article (or article (gnus-summary-article-number))))
2599       ;; All non-existent numbers are the last article.  :-)
2600       t
2601     (not (cdr (gnus-data-find-list article)))))
2602
2603 (defun gnus-make-thread-indent-array ()
2604   (let ((n 200))
2605     (unless (and gnus-thread-indent-array
2606                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2607       (setq gnus-thread-indent-array (make-vector 201 "")
2608             gnus-thread-indent-array-level gnus-thread-indent-level)
2609       (while (>= n 0)
2610         (aset gnus-thread-indent-array n
2611               (make-string (* n gnus-thread-indent-level) ? ))
2612         (setq n (1- n))))))
2613
2614 (defun gnus-update-summary-mark-positions ()
2615   "Compute where the summary marks are to go."
2616   (save-excursion
2617     (when (gnus-buffer-exists-p gnus-summary-buffer)
2618       (set-buffer gnus-summary-buffer))
2619     (let ((gnus-replied-mark 129)
2620           (gnus-score-below-mark 130)
2621           (gnus-score-over-mark 130)
2622           (gnus-download-mark 131)
2623           (spec gnus-summary-line-format-spec)
2624           gnus-visual pos)
2625       (save-excursion
2626         (gnus-set-work-buffer)
2627         (let ((gnus-summary-line-format-spec spec)
2628               (gnus-newsgroup-downloadable '((0 . t))))
2629           (gnus-summary-insert-line
2630            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2631            0 nil 128 t nil "" nil 1)
2632           (goto-char (point-min))
2633           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2634                                              (- (point) 2)))))
2635           (goto-char (point-min))
2636           (push (cons 'replied (and (search-forward "\201" nil t)
2637                                     (- (point) 2)))
2638                 pos)
2639           (goto-char (point-min))
2640           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2641                 pos)
2642           (goto-char (point-min))
2643           (push (cons 'download
2644                       (and (search-forward "\203" nil t) (- (point) 2)))
2645                 pos)))
2646       (setq gnus-summary-mark-positions pos))))
2647
2648 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2649   "Insert a dummy root in the summary buffer."
2650   (beginning-of-line)
2651   (gnus-add-text-properties
2652    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2653    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2654
2655 (defun gnus-summary-from-or-to-or-newsgroups (header)
2656   (let ((to (cdr (assq 'To (mail-header-extra header))))
2657         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2658         (default-mime-charset (with-current-buffer gnus-summary-buffer
2659                                 default-mime-charset)))
2660     (cond
2661      ((and to
2662            gnus-ignored-from-addresses
2663            (string-match gnus-ignored-from-addresses
2664                          (mail-header-from header)))
2665       (concat "-> "
2666               (or (car (funcall gnus-extract-address-components
2667                                 (funcall
2668                                  gnus-decode-encoded-word-function to)))
2669                   (funcall gnus-decode-encoded-word-function to))))
2670      ((and newsgroups
2671            gnus-ignored-from-addresses
2672            (string-match gnus-ignored-from-addresses
2673                          (mail-header-from header)))
2674       (concat "=> " newsgroups))
2675      (t
2676       (or (car (funcall gnus-extract-address-components
2677                         (mail-header-from header)))
2678           (mail-header-from header))))))
2679
2680 (defun gnus-summary-insert-line (gnus-tmp-header
2681                                  gnus-tmp-level gnus-tmp-current
2682                                  gnus-tmp-unread gnus-tmp-replied
2683                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2684                                  &optional gnus-tmp-dummy gnus-tmp-score
2685                                  gnus-tmp-process)
2686   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2687          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2688          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2689          (gnus-tmp-score-char
2690           (if (or (null gnus-summary-default-score)
2691                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2692                       gnus-summary-zcore-fuzz))
2693               ?  ;Whitespace
2694             (if (< gnus-tmp-score gnus-summary-default-score)
2695                 gnus-score-below-mark gnus-score-over-mark)))
2696          (gnus-tmp-replied
2697           (cond (gnus-tmp-process gnus-process-mark)
2698                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2699                  gnus-cached-mark)
2700                 (gnus-tmp-replied gnus-replied-mark)
2701                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2702                  gnus-saved-mark)
2703                 (t gnus-unread-mark)))
2704          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2705          (gnus-tmp-name
2706           (cond
2707            ((string-match "<[^>]+> *$" gnus-tmp-from)
2708             (let ((beg (match-beginning 0)))
2709               (or (and (string-match "^\".+\"" gnus-tmp-from)
2710                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2711                   (substring gnus-tmp-from 0 beg))))
2712            ((string-match "(.+)" gnus-tmp-from)
2713             (substring gnus-tmp-from
2714                        (1+ (match-beginning 0)) (1- (match-end 0))))
2715            (t gnus-tmp-from)))
2716          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2717          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2718          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2719          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2720          (buffer-read-only nil))
2721     (when (string= gnus-tmp-name "")
2722       (setq gnus-tmp-name gnus-tmp-from))
2723     (unless (numberp gnus-tmp-lines)
2724       (setq gnus-tmp-lines 0))
2725     (gnus-put-text-property-excluding-characters-with-faces
2726      (point)
2727      (progn (eval gnus-summary-line-format-spec) (point))
2728      'gnus-number gnus-tmp-number)
2729     (when (gnus-visual-p 'summary-highlight 'highlight)
2730       (forward-line -1)
2731       (gnus-run-hooks 'gnus-summary-update-hook)
2732       (forward-line 1))))
2733
2734 (defun gnus-summary-update-line (&optional dont-update)
2735   "Update summary line after change."
2736   (when (and gnus-summary-default-score
2737              (not gnus-summary-inhibit-highlight))
2738     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2739            (article (gnus-summary-article-number))
2740            (score (gnus-summary-article-score article)))
2741       (unless dont-update
2742         (if (and gnus-summary-mark-below
2743                  (< (gnus-summary-article-score)
2744                     gnus-summary-mark-below))
2745             ;; This article has a low score, so we mark it as read.
2746             (when (memq article gnus-newsgroup-unreads)
2747               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2748           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2749             ;; This article was previously marked as read on account
2750             ;; of a low score, but now it has risen, so we mark it as
2751             ;; unread.
2752             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2753         (gnus-summary-update-mark
2754          (if (or (null gnus-summary-default-score)
2755                  (<= (abs (- score gnus-summary-default-score))
2756                      gnus-summary-zcore-fuzz))
2757              ?  ;Whitespace
2758            (if (< score gnus-summary-default-score)
2759                gnus-score-below-mark gnus-score-over-mark))
2760          'score))
2761       ;; Do visual highlighting.
2762       (when (gnus-visual-p 'summary-highlight 'highlight)
2763         (gnus-run-hooks 'gnus-summary-update-hook)))))
2764
2765 (defvar gnus-tmp-new-adopts nil)
2766
2767 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2768   "Return the number of articles in THREAD.
2769 This may be 0 in some cases -- if none of the articles in
2770 the thread are to be displayed."
2771   (let* ((number
2772           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2773           (cond
2774            ((not (listp thread))
2775             1)
2776            ((and (consp thread) (cdr thread))
2777             (apply
2778              '+ 1 (mapcar
2779                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2780            ((null thread)
2781             1)
2782            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2783             1)
2784            (t 0))))
2785     (when (and level (zerop level) gnus-tmp-new-adopts)
2786       (incf number
2787             (apply '+ (mapcar
2788                        'gnus-summary-number-of-articles-in-thread
2789                        gnus-tmp-new-adopts))))
2790     (if char
2791         (if (> number 1) gnus-not-empty-thread-mark
2792           gnus-empty-thread-mark)
2793       number)))
2794
2795 (defun gnus-summary-set-local-parameters (group)
2796   "Go through the local params of GROUP and set all variable specs in that list."
2797   (let ((params (gnus-group-find-parameter group))
2798         elem)
2799     (while params
2800       (setq elem (car params)
2801             params (cdr params))
2802       (and (consp elem)                 ; Has to be a cons.
2803            (consp (cdr elem))           ; The cdr has to be a list.
2804            (symbolp (car elem))         ; Has to be a symbol in there.
2805            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2806            (ignore-errors               ; So we set it.
2807              (make-local-variable (car elem))
2808              (set (car elem) (eval (nth 1 elem))))))))
2809
2810 (defun gnus-summary-read-group (group &optional show-all no-article
2811                                       kill-buffer no-display backward
2812                                       select-articles)
2813   "Start reading news in newsgroup GROUP.
2814 If SHOW-ALL is non-nil, already read articles are also listed.
2815 If NO-ARTICLE is non-nil, no article is selected initially.
2816 If NO-DISPLAY, don't generate a summary buffer."
2817   (let (result)
2818     (while (and group
2819                 (null (setq result
2820                             (let ((gnus-auto-select-next nil))
2821                               (or (gnus-summary-read-group-1
2822                                    group show-all no-article
2823                                    kill-buffer no-display
2824                                    select-articles)
2825                                   (setq show-all nil
2826                                         select-articles nil)))))
2827                 (eq gnus-auto-select-next 'quietly))
2828       (set-buffer gnus-group-buffer)
2829       ;; The entry function called above goes to the next
2830       ;; group automatically, so we go two groups back
2831       ;; if we are searching for the previous group.
2832       (when backward
2833         (gnus-group-prev-unread-group 2))
2834       (if (not (equal group (gnus-group-group-name)))
2835           (setq group (gnus-group-group-name))
2836         (setq group nil)))
2837     result))
2838
2839 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2840   "Directly jump to the other GROUP from summary buffer.
2841 If SHOW-ALL is non-nil, already read articles are also listed."
2842   (interactive
2843    (if (eq gnus-summary-buffer (current-buffer))
2844        (list (completing-read
2845               "Group: " gnus-active-hashtb nil t
2846               (when (and gnus-newsgroup-name
2847                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2848                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2849               'gnus-group-history)
2850              current-prefix-arg)
2851      (error "%s must be invoked from a gnus summary buffer." this-command)))
2852   (unless (or (zerop (length group))
2853               (and gnus-newsgroup-name
2854                    (string-equal gnus-newsgroup-name group)))
2855     (gnus-summary-exit)
2856     (gnus-summary-read-group group show-all
2857                              gnus-dont-select-after-jump-to-other-group)))
2858
2859 (defun gnus-summary-read-group-1 (group show-all no-article
2860                                         kill-buffer no-display
2861                                         &optional select-articles)
2862   ;; Killed foreign groups can't be entered.
2863   (when (and (not (gnus-group-native-p group))
2864              (not (gnus-gethash group gnus-newsrc-hashtb)))
2865     (error "Dead non-native groups can't be entered"))
2866   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2867   (let* ((new-group (gnus-summary-setup-buffer group))
2868          (quit-config (gnus-group-quit-config group))
2869          (did-select (and new-group (gnus-select-newsgroup
2870                                      group show-all select-articles))))
2871     (cond
2872      ;; This summary buffer exists already, so we just select it.
2873      ((not new-group)
2874       (gnus-set-global-variables)
2875       (when kill-buffer
2876         (gnus-kill-or-deaden-summary kill-buffer))
2877       (gnus-configure-windows 'summary 'force)
2878       (gnus-set-mode-line 'summary)
2879       (gnus-summary-position-point)
2880       (message "")
2881       t)
2882      ;; We couldn't select this group.
2883      ((null did-select)
2884       (when (and (eq major-mode 'gnus-summary-mode)
2885                  (not (equal (current-buffer) kill-buffer)))
2886         (kill-buffer (current-buffer))
2887         (if (not quit-config)
2888             (progn
2889               ;; Update the info -- marks might need to be removed,
2890               ;; for instance.
2891               (gnus-summary-update-info)
2892               (set-buffer gnus-group-buffer)
2893               (gnus-group-jump-to-group group)
2894               (gnus-group-next-unread-group 1))
2895           (gnus-handle-ephemeral-exit quit-config)))
2896       (gnus-message 3 "Can't select group")
2897       nil)
2898      ;; The user did a `C-g' while prompting for number of articles,
2899      ;; so we exit this group.
2900      ((eq did-select 'quit)
2901       (and (eq major-mode 'gnus-summary-mode)
2902            (not (equal (current-buffer) kill-buffer))
2903            (kill-buffer (current-buffer)))
2904       (when kill-buffer
2905         (gnus-kill-or-deaden-summary kill-buffer))
2906       (if (not quit-config)
2907           (progn
2908             (set-buffer gnus-group-buffer)
2909             (gnus-group-jump-to-group group)
2910             (gnus-group-next-unread-group 1)
2911             (gnus-configure-windows 'group 'force))
2912         (gnus-handle-ephemeral-exit quit-config))
2913       ;; Finally signal the quit.
2914       (signal 'quit nil))
2915      ;; The group was successfully selected.
2916      (t
2917       (gnus-set-global-variables)
2918       ;; Save the active value in effect when the group was entered.
2919       (setq gnus-newsgroup-active
2920             (gnus-copy-sequence
2921              (gnus-active gnus-newsgroup-name)))
2922       ;; You can change the summary buffer in some way with this hook.
2923       (gnus-run-hooks 'gnus-select-group-hook)
2924       ;; Set any local variables in the group parameters.
2925       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2926       (gnus-update-format-specifications
2927        nil 'summary 'summary-mode 'summary-dummy)
2928       (gnus-update-summary-mark-positions)
2929       ;; Do score processing.
2930       (when gnus-use-scoring
2931         (gnus-possibly-score-headers))
2932       ;; Check whether to fill in the gaps in the threads.
2933       (when gnus-build-sparse-threads
2934         (gnus-build-sparse-threads))
2935       ;; Find the initial limit.
2936       (if gnus-show-threads
2937           (if show-all
2938               (let ((gnus-newsgroup-dormant nil))
2939                 (gnus-summary-initial-limit show-all))
2940             (gnus-summary-initial-limit show-all))
2941         ;; When untreaded, all articles are always shown.
2942         (setq gnus-newsgroup-limit
2943               (mapcar
2944                (lambda (header) (mail-header-number header))
2945                gnus-newsgroup-headers)))
2946       ;; Generate the summary buffer.
2947       (unless no-display
2948         (gnus-summary-prepare))
2949       (when gnus-use-trees
2950         (gnus-tree-open group)
2951         (setq gnus-summary-highlight-line-function
2952               'gnus-tree-highlight-article))
2953       ;; If the summary buffer is empty, but there are some low-scored
2954       ;; articles or some excluded dormants, we include these in the
2955       ;; buffer.
2956       (when (and (zerop (buffer-size))
2957                  (not no-display))
2958         (cond (gnus-newsgroup-dormant
2959                (gnus-summary-limit-include-dormant))
2960               ((and gnus-newsgroup-scored show-all)
2961                (gnus-summary-limit-include-expunged t))))
2962       ;; Function `gnus-apply-kill-file' must be called in this hook.
2963       (gnus-run-hooks 'gnus-apply-kill-hook)
2964       (if (and (zerop (buffer-size))
2965                (not no-display))
2966           (progn
2967             ;; This newsgroup is empty.
2968             (gnus-summary-catchup-and-exit nil t)
2969             (gnus-message 6 "No unread news")
2970             (when kill-buffer
2971               (gnus-kill-or-deaden-summary kill-buffer))
2972             ;; Return nil from this function.
2973             nil)
2974         ;; Hide conversation thread subtrees.  We cannot do this in
2975         ;; gnus-summary-prepare-hook since kill processing may not
2976         ;; work with hidden articles.
2977         (and gnus-show-threads
2978              gnus-thread-hide-subtree
2979              (gnus-summary-hide-all-threads))
2980         (when kill-buffer
2981           (gnus-kill-or-deaden-summary kill-buffer))
2982         ;; Show first unread article if requested.
2983         (if (and (not no-article)
2984                  (not no-display)
2985                  gnus-newsgroup-unreads
2986                  gnus-auto-select-first)
2987             (progn
2988               (gnus-configure-windows 'summary)
2989               (cond
2990                ((eq gnus-auto-select-first 'best)
2991                 (gnus-summary-best-unread-article))
2992                ((eq gnus-auto-select-first t)
2993                 (gnus-summary-first-unread-article))
2994                ((gnus-functionp gnus-auto-select-first)
2995                 (funcall gnus-auto-select-first))))
2996           ;; Don't select any articles, just move point to the first
2997           ;; article in the group.
2998           (goto-char (point-min))
2999           (gnus-summary-position-point)
3000           (gnus-configure-windows 'summary 'force)
3001           (gnus-set-mode-line 'summary))
3002         (when (get-buffer-window gnus-group-buffer t)
3003           ;; Gotta use windows, because recenter does weird stuff if
3004           ;; the current buffer ain't the displayed window.
3005           (let ((owin (selected-window)))
3006             (select-window (get-buffer-window gnus-group-buffer t))
3007             (when (gnus-group-goto-group group)
3008               (recenter))
3009             (select-window owin)))
3010         ;; Mark this buffer as "prepared".
3011         (setq gnus-newsgroup-prepared t)
3012         (gnus-run-hooks 'gnus-summary-prepared-hook)
3013         t)))))
3014
3015 (defun gnus-summary-prepare ()
3016   "Generate the summary buffer."
3017   (interactive)
3018   (let ((buffer-read-only nil))
3019     (erase-buffer)
3020     (setq gnus-newsgroup-data nil
3021           gnus-newsgroup-data-reverse nil)
3022     (gnus-run-hooks 'gnus-summary-generate-hook)
3023     ;; Generate the buffer, either with threads or without.
3024     (when gnus-newsgroup-headers
3025       (gnus-summary-prepare-threads
3026        (if gnus-show-threads
3027            (gnus-sort-gathered-threads
3028             (funcall gnus-summary-thread-gathering-function
3029                      (gnus-sort-threads
3030                       (gnus-cut-threads (gnus-make-threads)))))
3031          ;; Unthreaded display.
3032          (gnus-sort-articles gnus-newsgroup-headers))))
3033     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3034     ;; Call hooks for modifying summary buffer.
3035     (goto-char (point-min))
3036     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3037
3038 (defsubst gnus-general-simplify-subject (subject)
3039   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3040   (setq subject
3041         (cond
3042          ;; Truncate the subject.
3043          (gnus-simplify-subject-functions
3044           (gnus-map-function gnus-simplify-subject-functions subject))
3045          ((numberp gnus-summary-gather-subject-limit)
3046           (setq subject (gnus-simplify-subject-re subject))
3047           (if (> (length subject) gnus-summary-gather-subject-limit)
3048               (substring subject 0 gnus-summary-gather-subject-limit)
3049             subject))
3050          ;; Fuzzily simplify it.
3051          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3052           (gnus-simplify-subject-fuzzy subject))
3053          ;; Just remove the leading "Re:".
3054          (t
3055           (gnus-simplify-subject-re subject))))
3056
3057   (if (and gnus-summary-gather-exclude-subject
3058            (string-match gnus-summary-gather-exclude-subject subject))
3059       nil                               ; This article shouldn't be gathered
3060     subject))
3061
3062 (defun gnus-summary-simplify-subject-query ()
3063   "Query where the respool algorithm would put this article."
3064   (interactive)
3065   (gnus-summary-select-article)
3066   (message "%s"
3067            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3068
3069 (defun gnus-gather-threads-by-subject (threads)
3070   "Gather threads by looking at Subject headers."
3071   (if (not gnus-summary-make-false-root)
3072       threads
3073     (let ((hashtb (gnus-make-hashtable 1024))
3074           (prev threads)
3075           (result threads)
3076           subject hthread whole-subject)
3077       (while threads
3078         (setq subject (gnus-general-simplify-subject
3079                        (setq whole-subject (mail-header-subject
3080                                             (caar threads)))))
3081         (when subject
3082           (if (setq hthread (gnus-gethash subject hashtb))
3083               (progn
3084                 ;; We enter a dummy root into the thread, if we
3085                 ;; haven't done that already.
3086                 (unless (stringp (caar hthread))
3087                   (setcar hthread (list whole-subject (car hthread))))
3088                 ;; We add this new gathered thread to this gathered
3089                 ;; thread.
3090                 (setcdr (car hthread)
3091                         (nconc (cdar hthread) (list (car threads))))
3092                 ;; Remove it from the list of threads.
3093                 (setcdr prev (cdr threads))
3094                 (setq threads prev))
3095             ;; Enter this thread into the hash table.
3096             (gnus-sethash subject threads hashtb)))
3097         (setq prev threads)
3098         (setq threads (cdr threads)))
3099       result)))
3100
3101 (defun gnus-gather-threads-by-references (threads)
3102   "Gather threads by looking at References headers."
3103   (let ((idhashtb (gnus-make-hashtable 1024))
3104         (thhashtb (gnus-make-hashtable 1024))
3105         (prev threads)
3106         (result threads)
3107         ids references id gthread gid entered ref)
3108     (while threads
3109       (when (setq references (mail-header-references (caar threads)))
3110         (setq id (mail-header-id (caar threads))
3111               ids (gnus-split-references references)
3112               entered nil)
3113         (while (setq ref (pop ids))
3114           (setq ids (delete ref ids))
3115           (if (not (setq gid (gnus-gethash ref idhashtb)))
3116               (progn
3117                 (gnus-sethash ref id idhashtb)
3118                 (gnus-sethash id threads thhashtb))
3119             (setq gthread (gnus-gethash gid thhashtb))
3120             (unless entered
3121               ;; We enter a dummy root into the thread, if we
3122               ;; haven't done that already.
3123               (unless (stringp (caar gthread))
3124                 (setcar gthread (list (mail-header-subject (caar gthread))
3125                                       (car gthread))))
3126               ;; We add this new gathered thread to this gathered
3127               ;; thread.
3128               (setcdr (car gthread)
3129                       (nconc (cdar gthread) (list (car threads)))))
3130             ;; Add it into the thread hash table.
3131             (gnus-sethash id gthread thhashtb)
3132             (setq entered t)
3133             ;; Remove it from the list of threads.
3134             (setcdr prev (cdr threads))
3135             (setq threads prev))))
3136       (setq prev threads)
3137       (setq threads (cdr threads)))
3138     result))
3139
3140 (defun gnus-sort-gathered-threads (threads)
3141   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3142   (let ((result threads))
3143     (while threads
3144       (when (stringp (caar threads))
3145         (setcdr (car threads)
3146                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3147       (setq threads (cdr threads)))
3148     result))
3149
3150 (defun gnus-thread-loop-p (root thread)
3151   "Say whether ROOT is in THREAD."
3152   (let ((stack (list thread))
3153         (infloop 0)
3154         th)
3155     (while (setq thread (pop stack))
3156       (setq th (cdr thread))
3157       (while (and th
3158                   (not (eq (caar th) root)))
3159         (pop th))
3160       (if th
3161           ;; We have found a loop.
3162           (let (ref-dep)
3163             (setcdr thread (delq (car th) (cdr thread)))
3164             (if (boundp (setq ref-dep (intern "none"
3165                                               gnus-newsgroup-dependencies)))
3166                 (setcdr (symbol-value ref-dep)
3167                         (nconc (cdr (symbol-value ref-dep))
3168                                (list (car th))))
3169               (set ref-dep (list nil (car th))))
3170             (setq infloop 1
3171                   stack nil))
3172         ;; Push all the subthreads onto the stack.
3173         (push (cdr thread) stack)))
3174     infloop))
3175
3176 (defun gnus-make-threads ()
3177   "Go through the dependency hashtb and find the roots.  Return all threads."
3178   (let (threads)
3179     (while (catch 'infloop
3180              (mapatoms
3181               (lambda (refs)
3182                 ;; Deal with self-referencing References loops.
3183                 (when (and (car (symbol-value refs))
3184                            (not (zerop
3185                                  (apply
3186                                   '+
3187                                   (mapcar
3188                                    (lambda (thread)
3189                                      (gnus-thread-loop-p
3190                                       (car (symbol-value refs)) thread))
3191                                    (cdr (symbol-value refs)))))))
3192                   (setq threads nil)
3193                   (throw 'infloop t))
3194                 (unless (car (symbol-value refs))
3195                   ;; These threads do not refer back to any other articles,
3196                   ;; so they're roots.
3197                   (setq threads (append (cdr (symbol-value refs)) threads))))
3198               gnus-newsgroup-dependencies)))
3199     threads))
3200
3201 ;; Build the thread tree.
3202 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3203   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3204
3205 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3206 if it was already present.
3207
3208 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3209 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3210 Message-IDs will be renamed be renamed to a unique Message-ID before
3211 being entered.
3212
3213 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3214   (let* ((id (mail-header-id header))
3215          (id-dep (and id (intern id dependencies)))
3216          ref ref-dep ref-header)
3217     ;; Enter this `header' in the `dependencies' table.
3218     (cond
3219      ((not id-dep)
3220       (setq header nil))
3221      ;; The first two cases do the normal part: enter a new `header'
3222      ;; in the `dependencies' table.
3223      ((not (boundp id-dep))
3224       (set id-dep (list header)))
3225      ((null (car (symbol-value id-dep)))
3226       (setcar (symbol-value id-dep) header))
3227
3228      ;; From here the `header' was already present in the
3229      ;; `dependencies' table.
3230      (force-new
3231       ;; Overrides an existing entry;
3232       ;; just set the header part of the entry.
3233       (setcar (symbol-value id-dep) header))
3234
3235      ;; Renames the existing `header' to a unique Message-ID.
3236      ((not gnus-summary-ignore-duplicates)
3237       ;; An article with this Message-ID has already been seen.
3238       ;; We rename the Message-ID.
3239       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3240            (list header))
3241       (mail-header-set-id header id))
3242
3243      ;; The last case ignores an existing entry, except it adds any
3244      ;; additional Xrefs (in case the two articles came from different
3245      ;; servers.
3246      ;; Also sets `header' to `nil' meaning that the `dependencies'
3247      ;; table was *not* modified.
3248      (t
3249       (mail-header-set-xref
3250        (car (symbol-value id-dep))
3251        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3252                    "")
3253                (or (mail-header-xref header) "")))
3254       (setq header nil)))
3255
3256     (when header
3257       ;; First check if that we are not creating a References loop.
3258       (setq ref (gnus-parent-id (mail-header-references header)))
3259       (while (and ref
3260                   (setq ref-dep (intern-soft ref dependencies))
3261                   (boundp ref-dep)
3262                   (setq ref-header (car (symbol-value ref-dep))))
3263         (if (string= id ref)
3264             ;; Yuk!  This is a reference loop.  Make the article be a
3265             ;; root article.
3266             (progn
3267               (mail-header-set-references (car (symbol-value id-dep)) "none")
3268               (setq ref nil))
3269           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3270       (setq ref (gnus-parent-id (mail-header-references header)))
3271       (setq ref-dep (intern (or ref "none") dependencies))
3272       (if (boundp ref-dep)
3273           (setcdr (symbol-value ref-dep)
3274                   (nconc (cdr (symbol-value ref-dep))
3275                          (list (symbol-value id-dep))))
3276         (set ref-dep (list nil (symbol-value id-dep)))))
3277     header))
3278
3279 (defun gnus-build-sparse-threads ()
3280   (let ((headers gnus-newsgroup-headers)
3281         (mail-parse-charset gnus-newsgroup-charset)
3282         (gnus-summary-ignore-duplicates t)
3283         header references generation relations
3284         subject child end new-child date)
3285     ;; First we create an alist of generations/relations, where
3286     ;; generations is how much we trust the relation, and the relation
3287     ;; is parent/child.
3288     (gnus-message 7 "Making sparse threads...")
3289     (save-excursion
3290       (nnheader-set-temp-buffer " *gnus sparse threads*")
3291       (while (setq header (pop headers))
3292         (when (and (setq references (mail-header-references header))
3293                    (not (string= references "")))
3294           (insert references)
3295           (setq child (mail-header-id header)
3296                 subject (mail-header-subject header)
3297                 date (mail-header-date header)
3298                 generation 0)
3299           (while (search-backward ">" nil t)
3300             (setq end (1+ (point)))
3301             (when (search-backward "<" nil t)
3302               (setq new-child (buffer-substring (point) end))
3303               (push (list (incf generation)
3304                           child (setq child new-child)
3305                           subject date)
3306                     relations)))
3307           (when child
3308             (push (list (1+ generation) child nil subject) relations))
3309           (erase-buffer)))
3310       (kill-buffer (current-buffer)))
3311     ;; Sort over trustworthiness.
3312     (mapcar
3313      (lambda (relation)
3314        (when (gnus-dependencies-add-header
3315               (make-full-mail-header-from-decoded-header
3316                gnus-reffed-article-number
3317                (nth 3 relation) "" (or (nth 4 relation) "")
3318                (nth 1 relation)
3319                (or (nth 2 relation) "") 0 0 "")
3320               gnus-newsgroup-dependencies nil)
3321          (push gnus-reffed-article-number gnus-newsgroup-limit)
3322          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3323          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3324                gnus-newsgroup-reads)
3325          (decf gnus-reffed-article-number)))
3326      (sort relations 'car-less-than-car))
3327     (gnus-message 7 "Making sparse threads...done")))
3328
3329 (defun gnus-build-old-threads ()
3330   ;; Look at all the articles that refer back to old articles, and
3331   ;; fetch the headers for the articles that aren't there.  This will
3332   ;; build complete threads - if the roots haven't been expired by the
3333   ;; server, that is.
3334   (let ((mail-parse-charset gnus-newsgroup-charset)
3335         id heads)
3336     (mapatoms
3337      (lambda (refs)
3338        (when (not (car (symbol-value refs)))
3339          (setq heads (cdr (symbol-value refs)))
3340          (while heads
3341            (if (memq (mail-header-number (caar heads))
3342                      gnus-newsgroup-dormant)
3343                (setq heads (cdr heads))
3344              (setq id (symbol-name refs))
3345              (while (and (setq id (gnus-build-get-header id))
3346                          (not (car (gnus-id-to-thread id)))))
3347              (setq heads nil)))))
3348      gnus-newsgroup-dependencies)))
3349
3350 ;; This function has to be called with point after the article number
3351 ;; on the beginning of the line.
3352 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3353   (let ((eol (gnus-point-at-eol))
3354         (buffer (current-buffer))
3355         header)
3356
3357     ;; overview: [num subject from date id refs chars lines misc]
3358     (unwind-protect
3359         (progn
3360           (narrow-to-region (point) eol)
3361           (unless (eobp)
3362             (forward-char))
3363
3364           (setq header
3365                 (make-full-mail-header
3366                  number                         ; number
3367                  (nnheader-nov-field)           ; subject
3368                  (nnheader-nov-field)           ; from
3369                  (nnheader-nov-field)           ; date
3370                  (nnheader-nov-read-message-id) ; id
3371                  (nnheader-nov-field)           ; refs
3372                  (nnheader-nov-read-integer)    ; chars
3373                  (nnheader-nov-read-integer)    ; lines
3374                  (unless (eobp)
3375                    (if (looking-at "Xref: ")
3376                        (goto-char (match-end 0)))
3377                    (nnheader-nov-field))        ; Xref
3378                  (nnheader-nov-parse-extra))))  ; extra
3379
3380       (widen))
3381
3382     (when gnus-alter-header-function
3383       (funcall gnus-alter-header-function header))
3384     (gnus-dependencies-add-header header dependencies force-new)))
3385
3386 (defun gnus-build-get-header (id)
3387   "Look through the buffer of NOV lines and find the header to ID.
3388 Enter this line into the dependencies hash table, and return
3389 the id of the parent article (if any)."
3390   (let ((deps gnus-newsgroup-dependencies)
3391         found header)
3392     (prog1
3393         (save-excursion
3394           (set-buffer nntp-server-buffer)
3395           (let ((case-fold-search nil))
3396             (goto-char (point-min))
3397             (while (and (not found)
3398                         (search-forward id nil t))
3399               (beginning-of-line)
3400               (setq found (looking-at
3401                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3402                                    (regexp-quote id))))
3403               (or found (beginning-of-line 2)))
3404             (when found
3405               (beginning-of-line)
3406               (and
3407                (setq header (gnus-nov-parse-line
3408                              (read (current-buffer)) deps))
3409                (gnus-parent-id (mail-header-references header))))))
3410       (when header
3411         (let ((number (mail-header-number header)))
3412           (push number gnus-newsgroup-limit)
3413           (push header gnus-newsgroup-headers)
3414           (if (memq number gnus-newsgroup-unselected)
3415               (progn
3416                 (push number gnus-newsgroup-unreads)
3417                 (setq gnus-newsgroup-unselected
3418                       (delq number gnus-newsgroup-unselected)))
3419             (push number gnus-newsgroup-ancient)))))))
3420
3421 (defun gnus-build-all-threads ()
3422   "Read all the headers."
3423   (let ((gnus-summary-ignore-duplicates t)
3424         (mail-parse-charset gnus-newsgroup-charset)
3425         (dependencies gnus-newsgroup-dependencies)
3426         header article)
3427     (save-excursion
3428       (set-buffer nntp-server-buffer)
3429       (let ((case-fold-search nil))
3430         (goto-char (point-min))
3431         (while (not (eobp))
3432           (ignore-errors
3433             (setq article (read (current-buffer))
3434                   header (gnus-nov-parse-line article dependencies)))
3435           (when header
3436             (save-excursion
3437               (set-buffer gnus-summary-buffer)
3438               (push header gnus-newsgroup-headers)
3439               (if (memq (setq article (mail-header-number header))
3440                         gnus-newsgroup-unselected)
3441                   (progn
3442                     (push article gnus-newsgroup-unreads)
3443                     (setq gnus-newsgroup-unselected
3444                           (delq article gnus-newsgroup-unselected)))
3445                 (push article gnus-newsgroup-ancient)))
3446             (forward-line 1)))))))
3447
3448 (defun gnus-summary-update-article-line (article header)
3449   "Update the line for ARTICLE using HEADERS."
3450   (let* ((id (mail-header-id header))
3451          (thread (gnus-id-to-thread id)))
3452     (unless thread
3453       (error "Article in no thread"))
3454     ;; Update the thread.
3455     (setcar thread header)
3456     (gnus-summary-goto-subject article)
3457     (let* ((datal (gnus-data-find-list article))
3458            (data (car datal))
3459            (length (when (cdr datal)
3460                      (- (gnus-data-pos data)
3461                         (gnus-data-pos (cadr datal)))))
3462            (buffer-read-only nil)
3463            (level (gnus-summary-thread-level)))
3464       (gnus-delete-line)
3465       (gnus-summary-insert-line
3466        header level nil (gnus-article-mark article)
3467        (memq article gnus-newsgroup-replied)
3468        (memq article gnus-newsgroup-expirable)
3469        ;; Only insert the Subject string when it's different
3470        ;; from the previous Subject string.
3471        (if (and
3472             gnus-show-threads
3473             (gnus-subject-equal
3474              (condition-case ()
3475                  (mail-header-subject
3476                   (gnus-data-header
3477                    (cadr
3478                     (gnus-data-find-list
3479                      article
3480                      (gnus-data-list t)))))
3481                ;; Error on the side of excessive subjects.
3482                (error ""))
3483              (mail-header-subject header)))
3484            ""
3485          (mail-header-subject header))
3486        nil (cdr (assq article gnus-newsgroup-scored))
3487        (memq article gnus-newsgroup-processable))
3488       (when length
3489         (gnus-data-update-list
3490          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3491
3492 (defun gnus-summary-update-article (article &optional iheader)
3493   "Update ARTICLE in the summary buffer."
3494   (set-buffer gnus-summary-buffer)
3495   (let* ((header (gnus-summary-article-header article))
3496          (id (mail-header-id header))
3497          (data (gnus-data-find article))
3498          (thread (gnus-id-to-thread id))
3499          (references (mail-header-references header))
3500          (parent
3501           (gnus-id-to-thread
3502            (or (gnus-parent-id
3503                 (when (and references
3504                            (not (equal "" references)))
3505                   references))
3506                "none")))
3507          (buffer-read-only nil)
3508          (old (car thread)))
3509     (when thread
3510       (unless iheader
3511         (setcar thread nil)
3512         (when parent
3513           (delq thread parent)))
3514       (if (gnus-summary-insert-subject id header)
3515           ;; Set the (possibly) new article number in the data structure.
3516           (gnus-data-set-number data (gnus-id-to-article id))
3517         (setcar thread old)
3518         nil))))
3519
3520 (defun gnus-rebuild-thread (id &optional line)
3521   "Rebuild the thread containing ID.
3522 If LINE, insert the rebuilt thread starting on line LINE."
3523   (let ((buffer-read-only nil)
3524         old-pos current thread data)
3525     (if (not gnus-show-threads)
3526         (setq thread (list (car (gnus-id-to-thread id))))
3527       ;; Get the thread this article is part of.
3528       (setq thread (gnus-remove-thread id)))
3529     (setq old-pos (gnus-point-at-bol))
3530     (setq current (save-excursion
3531                     (and (zerop (forward-line -1))
3532                          (gnus-summary-article-number))))
3533     ;; If this is a gathered thread, we have to go some re-gathering.
3534     (when (stringp (car thread))
3535       (let ((subject (car thread))
3536             roots thr)
3537         (setq thread (cdr thread))
3538         (while thread
3539           (unless (memq (setq thr (gnus-id-to-thread
3540                                    (gnus-root-id
3541                                     (mail-header-id (caar thread)))))
3542                         roots)
3543             (push thr roots))
3544           (setq thread (cdr thread)))
3545         ;; We now have all (unique) roots.
3546         (if (= (length roots) 1)
3547             ;; All the loose roots are now one solid root.
3548             (setq thread (car roots))
3549           (setq thread (cons subject (gnus-sort-threads roots))))))
3550     (let (threads)
3551       ;; We then insert this thread into the summary buffer.
3552       (when line
3553         (goto-char (point-min))
3554         (forward-line (1- line)))
3555       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3556         (if gnus-show-threads
3557             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3558           (gnus-summary-prepare-unthreaded thread))
3559         (setq data (nreverse gnus-newsgroup-data))
3560         (setq threads gnus-newsgroup-threads))
3561       ;; We splice the new data into the data structure.
3562       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3563       ;;!!! then we want to insert at the beginning of the buffer.
3564       ;;!!! That happens to be true with Gnus now, but that may
3565       ;;!!! change in the future.  Perhaps.
3566       (gnus-data-enter-list
3567        (if line nil current) data (- (point) old-pos))
3568       (setq gnus-newsgroup-threads
3569             (nconc threads gnus-newsgroup-threads))
3570       (gnus-data-compute-positions))))
3571
3572 (defun gnus-number-to-header (number)
3573   "Return the header for article NUMBER."
3574   (let ((headers gnus-newsgroup-headers))
3575     (while (and headers
3576                 (not (= number (mail-header-number (car headers)))))
3577       (pop headers))
3578     (when headers
3579       (car headers))))
3580
3581 (defun gnus-parent-headers (in-headers &optional generation)
3582   "Return the headers of the GENERATIONeth parent of HEADERS."
3583   (unless generation
3584     (setq generation 1))
3585   (let ((parent t)
3586         (headers in-headers)
3587         references)
3588     (while (and parent
3589                 (not (zerop generation))
3590                 (setq references (mail-header-references headers)))
3591       (setq headers (if (and references
3592                              (setq parent (gnus-parent-id references)))
3593                         (car (gnus-id-to-thread parent))
3594                       nil))
3595       (decf generation))
3596     (and (not (eq headers in-headers))
3597          headers)))
3598
3599 (defun gnus-id-to-thread (id)
3600   "Return the (sub-)thread where ID appears."
3601   (gnus-gethash id gnus-newsgroup-dependencies))
3602
3603 (defun gnus-id-to-article (id)
3604   "Return the article number of ID."
3605   (let ((thread (gnus-id-to-thread id)))
3606     (when (and thread
3607                (car thread))
3608       (mail-header-number (car thread)))))
3609
3610 (defun gnus-id-to-header (id)
3611   "Return the article headers of ID."
3612   (car (gnus-id-to-thread id)))
3613
3614 (defun gnus-article-displayed-root-p (article)
3615   "Say whether ARTICLE is a root(ish) article."
3616   (let ((level (gnus-summary-thread-level article))
3617         (refs (mail-header-references  (gnus-summary-article-header article)))
3618         particle)
3619     (cond
3620      ((null level) nil)
3621      ((zerop level) t)
3622      ((null refs) t)
3623      ((null (gnus-parent-id refs)) t)
3624      ((and (= 1 level)
3625            (null (setq particle (gnus-id-to-article
3626                                  (gnus-parent-id refs))))
3627            (null (gnus-summary-thread-level particle)))))))
3628
3629 (defun gnus-root-id (id)
3630   "Return the id of the root of the thread where ID appears."
3631   (let (last-id prev)
3632     (while (and id (setq prev (car (gnus-id-to-thread id))))
3633       (setq last-id id
3634             id (gnus-parent-id (mail-header-references prev))))
3635     last-id))
3636
3637 (defun gnus-articles-in-thread (thread)
3638   "Return the list of articles in THREAD."
3639   (cons (mail-header-number (car thread))
3640         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3641
3642 (defun gnus-remove-thread (id &optional dont-remove)
3643   "Remove the thread that has ID in it."
3644   (let (headers thread last-id)
3645     ;; First go up in this thread until we find the root.
3646     (setq last-id (gnus-root-id id)
3647           headers (message-flatten-list (gnus-id-to-thread last-id)))
3648     ;; We have now found the real root of this thread.  It might have
3649     ;; been gathered into some loose thread, so we have to search
3650     ;; through the threads to find the thread we wanted.
3651     (let ((threads gnus-newsgroup-threads)
3652           sub)
3653       (while threads
3654         (setq sub (car threads))
3655         (if (stringp (car sub))
3656             ;; This is a gathered thread, so we look at the roots
3657             ;; below it to find whether this article is in this
3658             ;; gathered root.
3659             (progn
3660               (setq sub (cdr sub))
3661               (while sub
3662                 (when (member (caar sub) headers)
3663                   (setq thread (car threads)
3664                         threads nil
3665                         sub nil))
3666                 (setq sub (cdr sub))))
3667           ;; It's an ordinary thread, so we check it.
3668           (when (eq (car sub) (car headers))
3669             (setq thread sub
3670                   threads nil)))
3671         (setq threads (cdr threads)))
3672       ;; If this article is in no thread, then it's a root.
3673       (if thread
3674           (unless dont-remove
3675             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3676         (setq thread (gnus-id-to-thread last-id)))
3677       (when thread
3678         (prog1
3679             thread                      ; We return this thread.
3680           (unless dont-remove
3681             (if (stringp (car thread))
3682                 (progn
3683                   ;; If we use dummy roots, then we have to remove the
3684                   ;; dummy root as well.
3685                   (when (eq gnus-summary-make-false-root 'dummy)
3686                     ;; We go to the dummy root by going to
3687                     ;; the first sub-"thread", and then one line up.
3688                     (gnus-summary-goto-article
3689                      (mail-header-number (caadr thread)))
3690                     (forward-line -1)
3691                     (gnus-delete-line)
3692                     (gnus-data-compute-positions))
3693                   (setq thread (cdr thread))
3694                   (while thread
3695                     (gnus-remove-thread-1 (car thread))
3696                     (setq thread (cdr thread))))
3697               (gnus-remove-thread-1 thread))))))))
3698
3699 (defun gnus-remove-thread-1 (thread)
3700   "Remove the thread THREAD recursively."
3701   (let ((number (mail-header-number (pop thread)))
3702         d)
3703     (setq thread (reverse thread))
3704     (while thread
3705       (gnus-remove-thread-1 (pop thread)))
3706     (when (setq d (gnus-data-find number))
3707       (goto-char (gnus-data-pos d))
3708       (gnus-summary-show-thread)
3709       (gnus-data-remove
3710        number
3711        (- (gnus-point-at-bol)
3712           (prog1
3713               (1+ (gnus-point-at-eol))
3714             (gnus-delete-line)))))))
3715
3716 (defun gnus-sort-threads-1 (threads func)
3717   (sort (mapcar (lambda (thread)
3718                   (cons (car thread)
3719                         (and (cdr thread)
3720                              (gnus-sort-threads-1 (cdr thread) func))))
3721                 threads) func))
3722
3723 (defun gnus-sort-threads (threads)
3724   "Sort THREADS."
3725   (if (not gnus-thread-sort-functions)
3726       threads
3727     (gnus-message 8 "Sorting threads...")
3728     (prog1
3729         (gnus-sort-threads-1 
3730          threads 
3731          (gnus-make-sort-function gnus-thread-sort-functions))
3732       (gnus-message 8 "Sorting threads...done"))))
3733
3734 (defun gnus-sort-articles (articles)
3735   "Sort ARTICLES."
3736   (when gnus-article-sort-functions
3737     (gnus-message 7 "Sorting articles...")
3738     (prog1
3739         (setq gnus-newsgroup-headers
3740               (sort articles (gnus-make-sort-function
3741                               gnus-article-sort-functions)))
3742       (gnus-message 7 "Sorting articles...done"))))
3743
3744 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3745 (defmacro gnus-thread-header (thread)
3746   "Return header of first article in THREAD.
3747 Note that THREAD must never, ever be anything else than a variable -
3748 using some other form will lead to serious barfage."
3749   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3750   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3751   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3752         (vector thread) 2))
3753
3754 (defsubst gnus-article-sort-by-number (h1 h2)
3755   "Sort articles by article number."
3756   (< (mail-header-number h1)
3757      (mail-header-number h2)))
3758
3759 (defun gnus-thread-sort-by-number (h1 h2)
3760   "Sort threads by root article number."
3761   (gnus-article-sort-by-number
3762    (gnus-thread-header h1) (gnus-thread-header h2)))
3763
3764 (defsubst gnus-article-sort-by-lines (h1 h2)
3765   "Sort articles by article Lines header."
3766   (< (mail-header-lines h1)
3767      (mail-header-lines h2)))
3768
3769 (defun gnus-thread-sort-by-lines (h1 h2)
3770   "Sort threads by root article Lines header."
3771   (gnus-article-sort-by-lines
3772    (gnus-thread-header h1) (gnus-thread-header h2)))
3773
3774 (defsubst gnus-article-sort-by-chars (h1 h2)
3775   "Sort articles by octet length."
3776   (< (mail-header-chars h1)
3777      (mail-header-chars h2)))
3778
3779 (defun gnus-thread-sort-by-chars (h1 h2)
3780   "Sort threads by root article octet length."
3781   (gnus-article-sort-by-chars
3782    (gnus-thread-header h1) (gnus-thread-header h2)))
3783
3784 (defsubst gnus-article-sort-by-author (h1 h2)
3785   "Sort articles by root author."
3786   (string-lessp
3787    (let ((addr (car (mime-entity-read-field h1 'From))))
3788      (or (std11-full-name-string addr)
3789          (std11-address-string addr)
3790          ""))
3791    (let ((addr (car (mime-entity-read-field h2 'From))))
3792      (or (std11-full-name-string addr)
3793          (std11-address-string addr)
3794          ""))
3795    ))
3796
3797 (defun gnus-thread-sort-by-author (h1 h2)
3798   "Sort threads by root author."
3799   (gnus-article-sort-by-author
3800    (gnus-thread-header h1)  (gnus-thread-header h2)))
3801
3802 (defsubst gnus-article-sort-by-subject (h1 h2)
3803   "Sort articles by root subject."
3804   (string-lessp
3805    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3806    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3807
3808 (defun gnus-thread-sort-by-subject (h1 h2)
3809   "Sort threads by root subject."
3810   (gnus-article-sort-by-subject
3811    (gnus-thread-header h1) (gnus-thread-header h2)))
3812
3813 (defsubst gnus-article-sort-by-date (h1 h2)
3814   "Sort articles by root article date."
3815   (time-less-p
3816    (gnus-date-get-time (mail-header-date h1))
3817    (gnus-date-get-time (mail-header-date h2))))
3818
3819 (defun gnus-thread-sort-by-date (h1 h2)
3820   "Sort threads by root article date."
3821   (gnus-article-sort-by-date
3822    (gnus-thread-header h1) (gnus-thread-header h2)))
3823
3824 (defsubst gnus-article-sort-by-score (h1 h2)
3825   "Sort articles by root article score.
3826 Unscored articles will be counted as having a score of zero."
3827   (> (or (cdr (assq (mail-header-number h1)
3828                     gnus-newsgroup-scored))
3829          gnus-summary-default-score 0)
3830      (or (cdr (assq (mail-header-number h2)
3831                     gnus-newsgroup-scored))
3832          gnus-summary-default-score 0)))
3833
3834 (defun gnus-thread-sort-by-score (h1 h2)
3835   "Sort threads by root article score."
3836   (gnus-article-sort-by-score
3837    (gnus-thread-header h1) (gnus-thread-header h2)))
3838
3839 (defun gnus-thread-sort-by-total-score (h1 h2)
3840   "Sort threads by the sum of all scores in the thread.
3841 Unscored articles will be counted as having a score of zero."
3842   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3843
3844 (defun gnus-thread-total-score (thread)
3845   ;; This function find the total score of THREAD.
3846   (cond ((null thread)
3847          0)
3848         ((consp thread)
3849          (if (stringp (car thread))
3850              (apply gnus-thread-score-function 0
3851                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3852            (gnus-thread-total-score-1 thread)))
3853         (t
3854          (gnus-thread-total-score-1 (list thread)))))
3855
3856 (defun gnus-thread-total-score-1 (root)
3857   ;; This function find the total score of the thread below ROOT.
3858   (setq root (car root))
3859   (apply gnus-thread-score-function
3860          (or (append
3861               (mapcar 'gnus-thread-total-score
3862                       (cdr (gnus-id-to-thread (mail-header-id root))))
3863               (when (> (mail-header-number root) 0)
3864                 (list (or (cdr (assq (mail-header-number root)
3865                                      gnus-newsgroup-scored))
3866                           gnus-summary-default-score 0))))
3867              (list gnus-summary-default-score)
3868              '(0))))
3869
3870 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3871 (defvar gnus-tmp-prev-subject nil)
3872 (defvar gnus-tmp-false-parent nil)
3873 (defvar gnus-tmp-root-expunged nil)
3874 (defvar gnus-tmp-dummy-line nil)
3875
3876 (defvar gnus-tmp-header)
3877 (defun gnus-extra-header (type &optional header)
3878   "Return the extra header of TYPE."
3879   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3880       ""))
3881
3882 (defun gnus-summary-prepare-threads (threads)
3883   "Prepare summary buffer from THREADS and indentation LEVEL.
3884 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3885 or a straight list of headers."
3886   (gnus-message 7 "Generating summary...")
3887
3888   (setq gnus-newsgroup-threads threads)
3889   (beginning-of-line)
3890
3891   (let ((gnus-tmp-level 0)
3892         (default-score (or gnus-summary-default-score 0))
3893         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3894         thread number subject stack state gnus-tmp-gathered beg-match
3895         new-roots gnus-tmp-new-adopts thread-end
3896         gnus-tmp-header gnus-tmp-unread
3897         gnus-tmp-replied gnus-tmp-subject-or-nil
3898         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3899         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3900         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3901
3902     (setq gnus-tmp-prev-subject nil)
3903
3904     (if (vectorp (car threads))
3905         ;; If this is a straight (sic) list of headers, then a
3906         ;; threaded summary display isn't required, so we just create
3907         ;; an unthreaded one.
3908         (gnus-summary-prepare-unthreaded threads)
3909
3910       ;; Do the threaded display.
3911
3912       (while (or threads stack gnus-tmp-new-adopts new-roots)
3913
3914         (if (and (= gnus-tmp-level 0)
3915                  (or (not stack)
3916                      (= (caar stack) 0))
3917                  (not gnus-tmp-false-parent)
3918                  (or gnus-tmp-new-adopts new-roots))
3919             (if gnus-tmp-new-adopts
3920                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3921                       thread (list (car gnus-tmp-new-adopts))
3922                       gnus-tmp-header (caar thread)
3923                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3924               (when new-roots
3925                 (setq thread (list (car new-roots))
3926                       gnus-tmp-header (caar thread)
3927                       new-roots (cdr new-roots))))
3928
3929           (if threads
3930               ;; If there are some threads, we do them before the
3931               ;; threads on the stack.
3932               (setq thread threads
3933                     gnus-tmp-header (caar thread))
3934             ;; There were no current threads, so we pop something off
3935             ;; the stack.
3936             (setq state (car stack)
3937                   gnus-tmp-level (car state)
3938                   thread (cdr state)
3939                   stack (cdr stack)
3940                   gnus-tmp-header (caar thread))))
3941
3942         (setq gnus-tmp-false-parent nil)
3943         (setq gnus-tmp-root-expunged nil)
3944         (setq thread-end nil)
3945
3946         (if (stringp gnus-tmp-header)
3947             ;; The header is a dummy root.
3948             (cond
3949              ((eq gnus-summary-make-false-root 'adopt)
3950               ;; We let the first article adopt the rest.
3951               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3952                                                (cddar thread)))
3953               (setq gnus-tmp-gathered
3954                     (nconc (mapcar
3955                             (lambda (h) (mail-header-number (car h)))
3956                             (cddar thread))
3957                            gnus-tmp-gathered))
3958               (setq thread (cons (list (caar thread)
3959                                        (cadar thread))
3960                                  (cdr thread)))
3961               (setq gnus-tmp-level -1
3962                     gnus-tmp-false-parent t))
3963              ((eq gnus-summary-make-false-root 'empty)
3964               ;; We print adopted articles with empty subject fields.
3965               (setq gnus-tmp-gathered
3966                     (nconc (mapcar
3967                             (lambda (h) (mail-header-number (car h)))
3968                             (cddar thread))
3969                            gnus-tmp-gathered))
3970               (setq gnus-tmp-level -1))
3971              ((eq gnus-summary-make-false-root 'dummy)
3972               ;; We remember that we probably want to output a dummy
3973               ;; root.
3974               (setq gnus-tmp-dummy-line gnus-tmp-header)
3975               (setq gnus-tmp-prev-subject gnus-tmp-header))
3976              (t
3977               ;; We do not make a root for the gathered
3978               ;; sub-threads at all.
3979               (setq gnus-tmp-level -1)))
3980
3981           (setq number (mail-header-number gnus-tmp-header)
3982                 subject (mail-header-subject gnus-tmp-header))
3983
3984           (cond
3985            ;; If the thread has changed subject, we might want to make
3986            ;; this subthread into a root.
3987            ((and (null gnus-thread-ignore-subject)
3988                  (not (zerop gnus-tmp-level))
3989                  gnus-tmp-prev-subject
3990                  (not (inline
3991                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3992             (setq new-roots (nconc new-roots (list (car thread)))
3993                   thread-end t
3994                   gnus-tmp-header nil))
3995            ;; If the article lies outside the current limit,
3996            ;; then we do not display it.
3997            ((not (memq number gnus-newsgroup-limit))
3998             (setq gnus-tmp-gathered
3999                   (nconc (mapcar
4000                           (lambda (h) (mail-header-number (car h)))
4001                           (cdar thread))
4002                          gnus-tmp-gathered))
4003             (setq gnus-tmp-new-adopts (if (cdar thread)
4004                                           (append gnus-tmp-new-adopts
4005                                                   (cdar thread))
4006                                         gnus-tmp-new-adopts)
4007                   thread-end t
4008                   gnus-tmp-header nil)
4009             (when (zerop gnus-tmp-level)
4010               (setq gnus-tmp-root-expunged t)))
4011            ;; Perhaps this article is to be marked as read?
4012            ((and gnus-summary-mark-below
4013                  (< (or (cdr (assq number gnus-newsgroup-scored))
4014                         default-score)
4015                     gnus-summary-mark-below)
4016                  ;; Don't touch sparse articles.
4017                  (not (gnus-summary-article-sparse-p number))
4018                  (not (gnus-summary-article-ancient-p number)))
4019             (setq gnus-newsgroup-unreads
4020                   (delq number gnus-newsgroup-unreads))
4021             (if gnus-newsgroup-auto-expire
4022                 (push number gnus-newsgroup-expirable)
4023               (push (cons number gnus-low-score-mark)
4024                     gnus-newsgroup-reads))))
4025
4026           (when gnus-tmp-header
4027             ;; We may have an old dummy line to output before this
4028             ;; article.
4029             (when (and gnus-tmp-dummy-line
4030                        (gnus-subject-equal
4031                         gnus-tmp-dummy-line
4032                         (mail-header-subject gnus-tmp-header)))
4033               (gnus-summary-insert-dummy-line
4034                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4035               (setq gnus-tmp-dummy-line nil))
4036
4037             ;; Compute the mark.
4038             (setq gnus-tmp-unread (gnus-article-mark number))
4039
4040             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4041                                   gnus-tmp-header gnus-tmp-level)
4042                   gnus-newsgroup-data)
4043
4044             ;; Actually insert the line.
4045             (setq
4046              gnus-tmp-subject-or-nil
4047              (cond
4048               ((and gnus-thread-ignore-subject
4049                     gnus-tmp-prev-subject
4050                     (not (inline (gnus-subject-equal
4051                                   gnus-tmp-prev-subject subject))))
4052                subject)
4053               ((zerop gnus-tmp-level)
4054                (if (and (eq gnus-summary-make-false-root 'empty)
4055                         (memq number gnus-tmp-gathered)
4056                         gnus-tmp-prev-subject
4057                         (inline (gnus-subject-equal
4058                                  gnus-tmp-prev-subject subject)))
4059                    gnus-summary-same-subject
4060                  subject))
4061               (t gnus-summary-same-subject)))
4062             (if (and (eq gnus-summary-make-false-root 'adopt)
4063                      (= gnus-tmp-level 1)
4064                      (memq number gnus-tmp-gathered))
4065                 (setq gnus-tmp-opening-bracket ?\<
4066                       gnus-tmp-closing-bracket ?\>)
4067               (setq gnus-tmp-opening-bracket ?\[
4068                     gnus-tmp-closing-bracket ?\]))
4069             (setq
4070              gnus-tmp-indentation
4071              (aref gnus-thread-indent-array gnus-tmp-level)
4072              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4073              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4074                                 gnus-summary-default-score 0)
4075              gnus-tmp-score-char
4076              (if (or (null gnus-summary-default-score)
4077                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4078                          gnus-summary-zcore-fuzz))
4079                  ?  ;Whitespace
4080                (if (< gnus-tmp-score gnus-summary-default-score)
4081                    gnus-score-below-mark gnus-score-over-mark))
4082              gnus-tmp-replied
4083              (cond ((memq number gnus-newsgroup-processable)
4084                     gnus-process-mark)
4085                    ((memq number gnus-newsgroup-cached)
4086                     gnus-cached-mark)
4087                    ((memq number gnus-newsgroup-replied)
4088                     gnus-replied-mark)
4089                    ((memq number gnus-newsgroup-saved)
4090                     gnus-saved-mark)
4091                    (t gnus-unread-mark))
4092              gnus-tmp-from (mail-header-from gnus-tmp-header)
4093              gnus-tmp-name
4094              (cond
4095               ((string-match "<[^>]+> *$" gnus-tmp-from)
4096                (setq beg-match (match-beginning 0))
4097                (or (and (string-match "^\".+\"" gnus-tmp-from)
4098                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4099                    (substring gnus-tmp-from 0 beg-match)))
4100               ((string-match "(.+)" gnus-tmp-from)
4101                (substring gnus-tmp-from
4102                           (1+ (match-beginning 0)) (1- (match-end 0))))
4103               (t gnus-tmp-from)))
4104             (when (string= gnus-tmp-name "")
4105               (setq gnus-tmp-name gnus-tmp-from))
4106             (unless (numberp gnus-tmp-lines)
4107               (setq gnus-tmp-lines 0))
4108             (gnus-put-text-property
4109              (point)
4110              (progn (eval gnus-summary-line-format-spec) (point))
4111              'gnus-number number)
4112             (when gnus-visual-p
4113               (forward-line -1)
4114               (gnus-run-hooks 'gnus-summary-update-hook)
4115               (forward-line 1))
4116
4117             (setq gnus-tmp-prev-subject subject)))
4118
4119         (when (nth 1 thread)
4120           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4121         (incf gnus-tmp-level)
4122         (setq threads (if thread-end nil (cdar thread)))
4123         (unless threads
4124           (setq gnus-tmp-level 0)))))
4125   (gnus-message 7 "Generating summary...done"))
4126
4127 (defun gnus-summary-prepare-unthreaded (headers)
4128   "Generate an unthreaded summary buffer based on HEADERS."
4129   (let (header number mark)
4130
4131     (beginning-of-line)
4132
4133     (while headers
4134       ;; We may have to root out some bad articles...
4135       (when (memq (setq number (mail-header-number
4136                                 (setq header (pop headers))))
4137                   gnus-newsgroup-limit)
4138         ;; Mark article as read when it has a low score.
4139         (when (and gnus-summary-mark-below
4140                    (< (or (cdr (assq number gnus-newsgroup-scored))
4141                           gnus-summary-default-score 0)
4142                       gnus-summary-mark-below)
4143                    (not (gnus-summary-article-ancient-p number)))
4144           (setq gnus-newsgroup-unreads
4145                 (delq number gnus-newsgroup-unreads))
4146           (if gnus-newsgroup-auto-expire
4147               (push number gnus-newsgroup-expirable)
4148             (push (cons number gnus-low-score-mark)
4149                   gnus-newsgroup-reads)))
4150
4151         (setq mark (gnus-article-mark number))
4152         (push (gnus-data-make number mark (1+ (point)) header 0)
4153               gnus-newsgroup-data)
4154         (gnus-summary-insert-line
4155          header 0 number
4156          mark (memq number gnus-newsgroup-replied)
4157          (memq number gnus-newsgroup-expirable)
4158          (mail-header-subject header) nil
4159          (cdr (assq number gnus-newsgroup-scored))
4160          (memq number gnus-newsgroup-processable))))))
4161
4162 (defun gnus-summary-remove-list-identifiers ()
4163   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4164   (let ((regexp (if (stringp gnus-list-identifiers)
4165                     gnus-list-identifiers
4166                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4167     (dolist (header gnus-newsgroup-headers)
4168       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4169                                   " *\\)\\)+\\(Re: +\\)?\\)")
4170                           (mail-header-subject header))
4171         (mail-header-set-subject
4172          header (concat (substring (mail-header-subject header)
4173                                    0 (match-beginning 1))
4174                         (or
4175                          (match-string 3 (mail-header-subject header))
4176                          (match-string 5 (mail-header-subject header)))
4177                         (substring (mail-header-subject header)
4178                                    (match-end 1))))))))
4179
4180 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4181   "Select newsgroup GROUP.
4182 If READ-ALL is non-nil, all articles in the group are selected.
4183 If SELECT-ARTICLES, only select those articles from GROUP."
4184   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4185          ;;!!! Dirty hack; should be removed.
4186          (gnus-summary-ignore-duplicates
4187           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4188               t
4189             gnus-summary-ignore-duplicates))
4190          (info (nth 2 entry))
4191          articles fetched-articles cached)
4192
4193     (unless (gnus-check-server
4194              (setq gnus-current-select-method
4195                    (gnus-find-method-for-group group)))
4196       (error "Couldn't open server"))
4197
4198     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4199         (gnus-activate-group group)     ; Or we can activate it...
4200         (progn                          ; Or we bug out.
4201           (when (equal major-mode 'gnus-summary-mode)
4202             (kill-buffer (current-buffer)))
4203           (error "Couldn't activate group %s: %s"
4204                  group (gnus-status-message group))))
4205
4206     (unless (gnus-request-group group t)
4207       (when (equal major-mode 'gnus-summary-mode)
4208         (kill-buffer (current-buffer)))
4209       (error "Couldn't request group %s: %s"
4210              group (gnus-status-message group)))
4211
4212     (setq gnus-newsgroup-name group)
4213     (setq gnus-newsgroup-unselected nil)
4214     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4215     (gnus-summary-setup-default-charset)
4216
4217     ;; Adjust and set lists of article marks.
4218     (when info
4219       (gnus-adjust-marked-articles info))
4220
4221     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4222     (setq cached
4223           (if (gnus-virtual-group-p group)
4224               gnus-newsgroup-cached
4225             (gnus-cache-articles-in-group group)))
4226
4227     (setq gnus-newsgroup-unreads
4228           (gnus-set-difference
4229            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4230            gnus-newsgroup-dormant))
4231
4232     (setq gnus-newsgroup-processable nil)
4233
4234     (gnus-update-read-articles group gnus-newsgroup-unreads)
4235
4236     (if (setq articles select-articles)
4237         (setq gnus-newsgroup-unselected
4238               (gnus-sorted-intersection
4239                gnus-newsgroup-unreads
4240                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4241       (setq articles (gnus-articles-to-read group read-all)))
4242
4243     (cond
4244      ((null articles)
4245       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4246       'quit)
4247      ((eq articles 0) nil)
4248      (t
4249       ;; Init the dependencies hash table.
4250       (setq gnus-newsgroup-dependencies
4251             (gnus-make-hashtable (length articles)))
4252       (gnus-set-global-variables)
4253       ;; Retrieve the headers and read them in.
4254       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4255       (setq gnus-newsgroup-headers
4256             (gnus-retrieve-parsed-headers
4257              articles gnus-newsgroup-name
4258              ;; We might want to fetch old headers, but
4259              ;; not if there is only 1 article.
4260              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4261                            (not (numberp gnus-fetch-old-headers)))
4262                       (> (length articles) 1))
4263                   gnus-fetch-old-headers)))
4264       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4265
4266       ;; Suppress duplicates?
4267       (when gnus-suppress-duplicates
4268         (gnus-dup-suppress-articles))
4269
4270       ;; Set the initial limit.
4271       (setq gnus-newsgroup-limit (copy-sequence articles))
4272       ;; Remove canceled articles from the list of unread articles.
4273       (setq gnus-newsgroup-unreads
4274             (gnus-set-sorted-intersection
4275              gnus-newsgroup-unreads
4276              (setq fetched-articles
4277                    (mapcar (lambda (headers) (mail-header-number headers))
4278                            gnus-newsgroup-headers))))
4279       ;; Removed marked articles that do not exist.
4280       (gnus-update-missing-marks
4281        (gnus-sorted-complement fetched-articles articles))
4282
4283       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4284       (when cached
4285         (setq gnus-newsgroup-cached cached))
4286
4287       ;; We might want to build some more threads first.
4288       (when (and gnus-fetch-old-headers
4289                  (eq gnus-headers-retrieved-by 'nov))
4290         (if (eq gnus-fetch-old-headers 'invisible)
4291             (gnus-build-all-threads)
4292           (gnus-build-old-threads)))
4293       ;; Let the Gnus agent mark articles as read.
4294       (when gnus-agent
4295         (gnus-agent-get-undownloaded-list))
4296       ;; Remove list identifiers from subject
4297       (when gnus-list-identifiers
4298         (gnus-summary-remove-list-identifiers))
4299       ;; Check whether auto-expire is to be done in this group.
4300       (setq gnus-newsgroup-auto-expire
4301             (gnus-group-auto-expirable-p group))
4302       ;; Set up the article buffer now, if necessary.
4303       (unless gnus-single-article-buffer
4304         (gnus-article-setup-buffer))
4305       ;; First and last article in this newsgroup.
4306       (when gnus-newsgroup-headers
4307         (setq gnus-newsgroup-begin
4308               (mail-header-number (car gnus-newsgroup-headers))
4309               gnus-newsgroup-end
4310               (mail-header-number
4311                (gnus-last-element gnus-newsgroup-headers))))
4312       ;; GROUP is successfully selected.
4313       (or gnus-newsgroup-headers t)))))
4314
4315 (defun gnus-articles-to-read (group &optional read-all)
4316   "Find out what articles the user wants to read."
4317   (let* ((articles
4318           ;; Select all articles if `read-all' is non-nil, or if there
4319           ;; are no unread articles.
4320           (if (or read-all
4321                   (and (zerop (length gnus-newsgroup-marked))
4322                        (zerop (length gnus-newsgroup-unreads)))
4323                   (eq (gnus-group-find-parameter group 'display)
4324                       'all))
4325               (or
4326                (gnus-uncompress-range (gnus-active group))
4327                (gnus-cache-articles-in-group group))
4328             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4329                           (copy-sequence gnus-newsgroup-unreads))
4330                   '<)))
4331          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4332          (scored (length scored-list))
4333          (number (length articles))
4334          (marked (+ (length gnus-newsgroup-marked)
4335                     (length gnus-newsgroup-dormant)))
4336          (select
4337           (cond
4338            ((numberp read-all)
4339             read-all)
4340            (t
4341             (condition-case ()
4342                 (cond
4343                  ((and (or (<= scored marked) (= scored number))
4344                        (natnump gnus-large-newsgroup)
4345                        (> number gnus-large-newsgroup))
4346                   (let* ((cursor-in-echo-area nil)
4347                          (input (read-from-minibuffer
4348                                  (format
4349                                   "How many articles from %s (max %d): "
4350                                   (gnus-limit-string gnus-newsgroup-name 35)
4351                                   number)
4352                                  (cons (number-to-string gnus-large-newsgroup)
4353                                        0))))
4354                     (if (string-match "^[ \t]*$" input)
4355                         number
4356                       input)))
4357                  ((and (> scored marked) (< scored number)
4358                        (> (- scored number) 20))
4359                   (let ((input
4360                          (read-string
4361                           (format "%s %s (%d scored, %d total): "
4362                                   "How many articles from"
4363                                   group scored number))))
4364                     (if (string-match "^[ \t]*$" input)
4365                         number input)))
4366                  (t number))
4367               (quit
4368                (message "Quit getting the articles to read")
4369                nil))))))
4370     (setq select (if (stringp select) (string-to-number select) select))
4371     (if (or (null select) (zerop select))
4372         select
4373       (if (and (not (zerop scored)) (<= (abs select) scored))
4374           (progn
4375             (setq articles (sort scored-list '<))
4376             (setq number (length articles)))
4377         (setq articles (copy-sequence articles)))
4378
4379       (when (< (abs select) number)
4380         (if (< select 0)
4381             ;; Select the N oldest articles.
4382             (setcdr (nthcdr (1- (abs select)) articles) nil)
4383           ;; Select the N most recent articles.
4384           (setq articles (nthcdr (- number select) articles))))
4385       (setq gnus-newsgroup-unselected
4386             (gnus-sorted-intersection
4387              gnus-newsgroup-unreads
4388              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4389       (when gnus-alter-articles-to-read-function
4390         (setq gnus-newsgroup-unreads
4391               (sort
4392                (funcall gnus-alter-articles-to-read-function
4393                         gnus-newsgroup-name gnus-newsgroup-unreads)
4394                '<)))
4395       articles)))
4396
4397 (defun gnus-killed-articles (killed articles)
4398   (let (out)
4399     (while articles
4400       (when (inline (gnus-member-of-range (car articles) killed))
4401         (push (car articles) out))
4402       (setq articles (cdr articles)))
4403     out))
4404
4405 (defun gnus-uncompress-marks (marks)
4406   "Uncompress the mark ranges in MARKS."
4407   (let ((uncompressed '(score bookmark))
4408         out)
4409     (while marks
4410       (if (memq (caar marks) uncompressed)
4411           (push (car marks) out)
4412         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4413       (setq marks (cdr marks)))
4414     out))
4415
4416 (defun gnus-adjust-marked-articles (info)
4417   "Set all article lists and remove all marks that are no longer valid."
4418   (let* ((marked-lists (gnus-info-marks info))
4419          (active (gnus-active (gnus-info-group info)))
4420          (min (car active))
4421          (max (cdr active))
4422          (types gnus-article-mark-lists)
4423          (uncompressed '(score bookmark killed))
4424          marks var articles article mark)
4425
4426     (while marked-lists
4427       (setq marks (pop marked-lists))
4428       (set (setq var (intern (format "gnus-newsgroup-%s"
4429                                      (car (rassq (setq mark (car marks))
4430                                                  types)))))
4431            (if (memq (car marks) uncompressed) (cdr marks)
4432              (gnus-uncompress-range (cdr marks))))
4433
4434       (setq articles (symbol-value var))
4435
4436       ;; All articles have to be subsets of the active articles.
4437       (cond
4438        ;; Adjust "simple" lists.
4439        ((memq mark '(tick dormant expire reply save))
4440         (while articles
4441           (when (or (< (setq article (pop articles)) min) (> article max))
4442             (set var (delq article (symbol-value var))))))
4443        ;; Adjust assocs.
4444        ((memq mark uncompressed)
4445         (when (not (listp (cdr (symbol-value var))))
4446           (set var (list (symbol-value var))))
4447         (when (not (listp (cdr articles)))
4448           (setq articles (list articles)))
4449         (while articles
4450           (when (or (not (consp (setq article (pop articles))))
4451                     (< (car article) min)
4452                     (> (car article) max))
4453             (set var (delq article (symbol-value var))))))))))
4454
4455 (defun gnus-update-missing-marks (missing)
4456   "Go through the list of MISSING articles and remove them from the mark lists."
4457   (when missing
4458     (let ((types gnus-article-mark-lists)
4459           var m)
4460       ;; Go through all types.
4461       (while types
4462         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4463         (when (symbol-value var)
4464           ;; This list has articles.  So we delete all missing articles
4465           ;; from it.
4466           (setq m missing)
4467           (while m
4468             (set var (delq (pop m) (symbol-value var)))))))))
4469
4470 (defun gnus-update-marks ()
4471   "Enter the various lists of marked articles into the newsgroup info list."
4472   (let ((types gnus-article-mark-lists)
4473         (info (gnus-get-info gnus-newsgroup-name))
4474         (uncompressed '(score bookmark killed))
4475         type list newmarked symbol delta-marks)
4476     (when info
4477       ;; Add all marks lists to the list of marks lists.
4478       (while (setq type (pop types))
4479         (setq list (symbol-value
4480                     (setq symbol
4481                           (intern (format "gnus-newsgroup-%s"
4482                                           (car type))))))
4483
4484         (when list
4485           ;; Get rid of the entries of the articles that have the
4486           ;; default score.
4487           (when (and (eq (cdr type) 'score)
4488                      gnus-save-score
4489                      list)
4490             (let* ((arts list)
4491                    (prev (cons nil list))
4492                    (all prev))
4493               (while arts
4494                 (if (or (not (consp (car arts)))
4495                         (= (cdar arts) gnus-summary-default-score))
4496                     (setcdr prev (cdr arts))
4497                   (setq prev arts))
4498                 (setq arts (cdr arts)))
4499               (setq list (cdr all)))))
4500
4501         (unless (memq (cdr type) uncompressed)
4502           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4503
4504         (when (gnus-check-backend-function
4505                'request-set-mark gnus-newsgroup-name)
4506           ;; propagate flags to server, with the following exceptions:
4507           ;; uncompressed:s are not proper flags (they are cons cells)
4508           ;; cache is a internal gnus flag
4509           ;; download are local to one gnus installation (well)
4510           ;; unsend are for nndraft groups only
4511           ;; xxx: generality of this?  this suits nnimap anyway
4512           (unless (memq (cdr type) (append '(cache download unsend)
4513                                            uncompressed))
4514             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4515                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4516                    (add (gnus-remove-from-range
4517                          (gnus-copy-sequence list) old)))
4518               (when add
4519                 (push (list add 'add (list (cdr type))) delta-marks))
4520               (when del
4521                 (push (list del 'del (list (cdr type))) delta-marks)))))
4522
4523         (when list
4524           (push (cons (cdr type) list) newmarked)))
4525
4526       (when delta-marks
4527         (unless (gnus-check-group gnus-newsgroup-name)
4528           (error "Can't open server for %s" gnus-newsgroup-name))
4529         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4530
4531       ;; Enter these new marks into the info of the group.
4532       (if (nthcdr 3 info)
4533           (setcar (nthcdr 3 info) newmarked)
4534         ;; Add the marks lists to the end of the info.
4535         (when newmarked
4536           (setcdr (nthcdr 2 info) (list newmarked))))
4537
4538       ;; Cut off the end of the info if there's nothing else there.
4539       (let ((i 5))
4540         (while (and (> i 2)
4541                     (not (nth i info)))
4542           (when (nthcdr (decf i) info)
4543             (setcdr (nthcdr i info) nil)))))))
4544
4545 (defun gnus-set-mode-line (where)
4546   "Set the mode line of the article or summary buffers.
4547 If WHERE is `summary', the summary mode line format will be used."
4548   ;; Is this mode line one we keep updated?
4549   (when (and (memq where gnus-updated-mode-lines)
4550              (symbol-value
4551               (intern (format "gnus-%s-mode-line-format-spec" where))))
4552     (let (mode-string)
4553       (save-excursion
4554         ;; We evaluate this in the summary buffer since these
4555         ;; variables are buffer-local to that buffer.
4556         (set-buffer gnus-summary-buffer)
4557         ;; We bind all these variables that are used in the `eval' form
4558         ;; below.
4559         (let* ((mformat (symbol-value
4560                          (intern
4561                           (format "gnus-%s-mode-line-format-spec" where))))
4562                (gnus-tmp-group-name (gnus-group-name-decode
4563                                      gnus-newsgroup-name
4564                                      (gnus-group-name-charset
4565                                       nil
4566                                       gnus-newsgroup-name)))
4567                (gnus-tmp-article-number (or gnus-current-article 0))
4568                (gnus-tmp-unread gnus-newsgroup-unreads)
4569                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4570                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4571                (gnus-tmp-unread-and-unselected
4572                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4573                             (zerop gnus-tmp-unselected))
4574                        "")
4575                       ((zerop gnus-tmp-unselected)
4576                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4577                       (t (format "{%d(+%d) more}"
4578                                  gnus-tmp-unread-and-unticked
4579                                  gnus-tmp-unselected))))
4580                (gnus-tmp-subject
4581                 (if (and gnus-current-headers
4582                          (vectorp gnus-current-headers))
4583                     (gnus-mode-string-quote
4584                      (mail-header-subject gnus-current-headers))
4585                   ""))
4586                bufname-length max-len
4587                gnus-tmp-header);; passed as argument to any user-format-funcs
4588           (setq mode-string (eval mformat))
4589           (setq bufname-length (if (string-match "%b" mode-string)
4590                                    (- (length
4591                                        (buffer-name
4592                                         (if (eq where 'summary)
4593                                             nil
4594                                           (get-buffer gnus-article-buffer))))
4595                                       2)
4596                                  0))
4597           (setq max-len (max 4 (if gnus-mode-non-string-length
4598                                    (- (window-width)
4599                                       gnus-mode-non-string-length
4600                                       bufname-length)
4601                                  (length mode-string))))
4602           ;; We might have to chop a bit of the string off...
4603           (when (> (length mode-string) max-len)
4604             (setq mode-string
4605                   (concat (gnus-truncate-string mode-string (- max-len 3))
4606                           "...")))
4607           ;; Pad the mode string a bit.
4608           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4609       ;; Update the mode line.
4610       (setq mode-line-buffer-identification
4611             (gnus-mode-line-buffer-identification (list mode-string)))
4612       (set-buffer-modified-p t))))
4613
4614 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4615   "Go through the HEADERS list and add all Xrefs to a hash table.
4616 The resulting hash table is returned, or nil if no Xrefs were found."
4617   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4618          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4619          (xref-hashtb (gnus-make-hashtable))
4620          start group entry number xrefs header)
4621     (while headers
4622       (setq header (pop headers))
4623       (when (and (setq xrefs (mail-header-xref header))
4624                  (not (memq (setq number (mail-header-number header))
4625                             unreads)))
4626         (setq start 0)
4627         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4628           (setq start (match-end 0))
4629           (setq group (if prefix
4630                           (concat prefix (substring xrefs (match-beginning 1)
4631                                                     (match-end 1)))
4632                         (substring xrefs (match-beginning 1) (match-end 1))))
4633           (setq number
4634                 (string-to-int (substring xrefs (match-beginning 2)
4635                                           (match-end 2))))
4636           (if (setq entry (gnus-gethash group xref-hashtb))
4637               (setcdr entry (cons number (cdr entry)))
4638             (gnus-sethash group (cons number nil) xref-hashtb)))))
4639     (and start xref-hashtb)))
4640
4641 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4642   "Look through all the headers and mark the Xrefs as read."
4643   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4644         name entry info xref-hashtb idlist method nth4)
4645     (save-excursion
4646       (set-buffer gnus-group-buffer)
4647       (when (setq xref-hashtb
4648                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4649         (mapatoms
4650          (lambda (group)
4651            (unless (string= from-newsgroup (setq name (symbol-name group)))
4652              (setq idlist (symbol-value group))
4653              ;; Dead groups are not updated.
4654              (and (prog1
4655                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4656                             info (nth 2 entry))
4657                     (when (stringp (setq nth4 (gnus-info-method info)))
4658                       (setq nth4 (gnus-server-to-method nth4))))
4659                   ;; Only do the xrefs if the group has the same
4660                   ;; select method as the group we have just read.
4661                   (or (gnus-methods-equal-p
4662                        nth4 (gnus-find-method-for-group from-newsgroup))
4663                       virtual
4664                       (equal nth4 (setq method (gnus-find-method-for-group
4665                                                 from-newsgroup)))
4666                       (and (equal (car nth4) (car method))
4667                            (equal (nth 1 nth4) (nth 1 method))))
4668                   gnus-use-cross-reference
4669                   (or (not (eq gnus-use-cross-reference t))
4670                       virtual
4671                       ;; Only do cross-references on subscribed
4672                       ;; groups, if that is what is wanted.
4673                       (<= (gnus-info-level info) gnus-level-subscribed))
4674                   (gnus-group-make-articles-read name idlist))))
4675          xref-hashtb)))))
4676
4677 (defun gnus-compute-read-articles (group articles)
4678   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4679          (info (nth 2 entry))
4680          (active (gnus-active group))
4681          ninfo)
4682     (when entry
4683       ;; First peel off all invalid article numbers.
4684       (when active
4685         (let ((ids articles)
4686               id first)
4687           (while (setq id (pop ids))
4688             (when (and first (> id (cdr active)))
4689               ;; We'll end up in this situation in one particular
4690               ;; obscure situation.  If you re-scan a group and get
4691               ;; a new article that is cross-posted to a different
4692               ;; group that has not been re-scanned, you might get
4693               ;; crossposted article that has a higher number than
4694               ;; Gnus believes possible.  So we re-activate this
4695               ;; group as well.  This might mean doing the
4696               ;; crossposting thingy will *increase* the number
4697               ;; of articles in some groups.  Tsk, tsk.
4698               (setq active (or (gnus-activate-group group) active)))
4699             (when (or (> id (cdr active))
4700                       (< id (car active)))
4701               (setq articles (delq id articles))))))
4702       ;; If the read list is nil, we init it.
4703       (if (and active
4704                (null (gnus-info-read info))
4705                (> (car active) 1))
4706           (setq ninfo (cons 1 (1- (car active))))
4707         (setq ninfo (gnus-info-read info)))
4708       ;; Then we add the read articles to the range.
4709       (gnus-add-to-range
4710        ninfo (setq articles (sort articles '<))))))
4711
4712 (defun gnus-group-make-articles-read (group articles)
4713   "Update the info of GROUP to say that ARTICLES are read."
4714   (let* ((num 0)
4715          (entry (gnus-gethash group gnus-newsrc-hashtb))
4716          (info (nth 2 entry))
4717          (active (gnus-active group))
4718          range)
4719     (when entry
4720       (setq range (gnus-compute-read-articles group articles))
4721       (save-excursion
4722         (set-buffer gnus-group-buffer)
4723         (gnus-undo-register
4724           `(progn
4725              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4726              (gnus-info-set-read ',info ',(gnus-info-read info))
4727              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4728              (gnus-group-update-group ,group t))))
4729       ;; Add the read articles to the range.
4730       (gnus-info-set-read info range)
4731       ;; Then we have to re-compute how many unread
4732       ;; articles there are in this group.
4733       (when active
4734         (cond
4735          ((not range)
4736           (setq num (- (1+ (cdr active)) (car active))))
4737          ((not (listp (cdr range)))
4738           (setq num (- (cdr active) (- (1+ (cdr range))
4739                                        (car range)))))
4740          (t
4741           (while range
4742             (if (numberp (car range))
4743                 (setq num (1+ num))
4744               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4745             (setq range (cdr range)))
4746           (setq num (- (cdr active) num))))
4747         ;; Update the number of unread articles.
4748         (setcar entry num)
4749         ;; Update the group buffer.
4750         (gnus-group-update-group group t)))))
4751
4752 (defvar gnus-newsgroup-none-id 0)
4753
4754 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4755   (let ((cur nntp-server-buffer)
4756         (dependencies
4757          (or dependencies
4758              (save-excursion (set-buffer gnus-summary-buffer)
4759                              gnus-newsgroup-dependencies)))
4760         headers id end ref
4761         (mail-parse-charset gnus-newsgroup-charset)
4762         (mail-parse-ignored-charsets
4763          (save-excursion (condition-case nil
4764                              (set-buffer gnus-summary-buffer)
4765                            (error))
4766                          gnus-newsgroup-ignored-charsets)))
4767     (save-excursion
4768       (set-buffer nntp-server-buffer)
4769       ;; Translate all TAB characters into SPACE characters.
4770       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4771       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4772       (gnus-run-hooks 'gnus-parse-headers-hook)
4773       (let ((case-fold-search t)
4774             in-reply-to header p lines chars ctype)
4775         (goto-char (point-min))
4776         ;; Search to the beginning of the next header.  Error messages
4777         ;; do not begin with 2 or 3.
4778         (while (re-search-forward "^[23][0-9]+ " nil t)
4779           (setq id nil
4780                 ref nil)
4781           ;; This implementation of this function, with nine
4782           ;; search-forwards instead of the one re-search-forward and
4783           ;; a case (which basically was the old function) is actually
4784           ;; about twice as fast, even though it looks messier.  You
4785           ;; can't have everything, I guess.  Speed and elegance
4786           ;; doesn't always go hand in hand.
4787           (setq
4788            header
4789            (make-full-mail-header
4790             ;; Number.
4791             (prog1
4792                 (read cur)
4793               (end-of-line)
4794               (setq p (point))
4795               (narrow-to-region (point)
4796                                 (or (and (search-forward "\n.\n" nil t)
4797                                          (- (point) 2))
4798                                     (point))))
4799             ;; Subject.
4800             (progn
4801               (goto-char p)
4802               (if (search-forward "\nsubject: " nil t)
4803                   (nnheader-header-value)
4804                 "(none)"))
4805             ;; From.
4806             (progn
4807               (goto-char p)
4808               (if (or (search-forward "\nfrom: " nil t)
4809                       (search-forward "\nfrom:" nil t))
4810                   (nnheader-header-value)
4811                 "(nobody)"))
4812             ;; Date.
4813             (progn
4814               (goto-char p)
4815               (if (search-forward "\ndate: " nil t)
4816                   (nnheader-header-value)
4817                 ""))
4818             ;; Message-ID.
4819             (progn
4820               (goto-char p)
4821               (setq id (if (re-search-forward
4822                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4823                            ;; We do it this way to make sure the Message-ID
4824                            ;; is (somewhat) syntactically valid.
4825                            (buffer-substring (match-beginning 1)
4826                                              (match-end 1))
4827                          ;; If there was no message-id, we just fake one
4828                          ;; to make subsequent routines simpler.
4829                          (nnheader-generate-fake-message-id))))
4830             ;; References.
4831             (progn
4832               (goto-char p)
4833               (if (search-forward "\nreferences: " nil t)
4834                   (progn
4835                     (setq end (point))
4836                     (prog1
4837                         (nnheader-header-value)
4838                       (setq ref
4839                             (buffer-substring
4840                              (progn
4841                                ;; (end-of-line)
4842                                (search-backward ">" end t)
4843                                (1+ (point)))
4844                              (progn
4845                                (search-backward "<" end t)
4846                                (point))))))
4847                 ;; Get the references from the in-reply-to header if there
4848                 ;; were no references and the in-reply-to header looks
4849                 ;; promising.
4850                 (if (and (search-forward "\nin-reply-to: " nil t)
4851                          (setq in-reply-to (nnheader-header-value))
4852                          (string-match "<[^>]+>" in-reply-to))
4853                     (let (ref2)
4854                       (setq ref (substring in-reply-to (match-beginning 0)
4855                                            (match-end 0)))
4856                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4857                         (setq ref2 (substring in-reply-to (match-beginning 0)
4858                                               (match-end 0)))
4859                         (when (> (length ref2) (length ref))
4860                           (setq ref ref2)))
4861                       ref)
4862                   (setq ref nil))))
4863             ;; Chars.
4864             (progn
4865               (goto-char p)
4866               (if (search-forward "\nchars: " nil t)
4867                   (if (numberp (setq chars (ignore-errors (read cur))))
4868                       chars 0)
4869                 0))
4870             ;; Lines.
4871             (progn
4872               (goto-char p)
4873               (if (search-forward "\nlines: " nil t)
4874                   (if (numberp (setq lines (ignore-errors (read cur))))
4875                       lines 0)
4876                 0))
4877             ;; Xref.
4878             (progn
4879               (goto-char p)
4880               (and (search-forward "\nxref: " nil t)
4881                    (nnheader-header-value)))
4882             ;; Extra.
4883             (when gnus-extra-headers
4884               (let ((extra gnus-extra-headers)
4885                     out)
4886                 (while extra
4887                   (goto-char p)
4888                   (when (search-forward
4889                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4890                     (push (cons (car extra) (nnheader-header-value)) out))
4891                   (pop extra))
4892                 out))))
4893           (goto-char p)
4894           (if (and (search-forward "\ncontent-type: " nil t)
4895                    (setq ctype (nnheader-header-value)))
4896               (mime-entity-set-content-type-internal
4897                header (mime-parse-Content-Type ctype)))
4898           (when (equal id ref)
4899             (setq ref nil))
4900
4901           (when gnus-alter-header-function
4902             (funcall gnus-alter-header-function header)
4903             (setq id (mail-header-id header)
4904                   ref (gnus-parent-id (mail-header-references header))))
4905
4906           (when (setq header
4907                       (gnus-dependencies-add-header
4908                        header dependencies force-new))
4909             (push header headers))
4910           (goto-char (point-max))
4911           (widen))
4912         (nreverse headers)))))
4913
4914 ;; Goes through the xover lines and returns a list of vectors
4915 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4916                                                   force-new dependencies
4917                                                   group also-fetch-heads)
4918   "Parse the news overview data in the server buffer.
4919 Return a list of headers that match SEQUENCE (see
4920 `nntp-retrieve-headers')."
4921   ;; Get the Xref when the users reads the articles since most/some
4922   ;; NNTP servers do not include Xrefs when using XOVER.
4923   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4924   (let ((mail-parse-charset gnus-newsgroup-charset)
4925         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4926         (cur nntp-server-buffer)
4927         (dependencies (or dependencies gnus-newsgroup-dependencies))
4928         number headers header)
4929     (save-excursion
4930       (set-buffer nntp-server-buffer)
4931       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4932       ;; Allow the user to mangle the headers before parsing them.
4933       (gnus-run-hooks 'gnus-parse-headers-hook)
4934       (goto-char (point-min))
4935       (while (not (eobp))
4936         (condition-case ()
4937             (while (and sequence (not (eobp)))
4938               (setq number (read cur))
4939               (while (and sequence
4940                           (< (car sequence) number))
4941                 (setq sequence (cdr sequence)))
4942               (and sequence
4943                    (eq number (car sequence))
4944                    (progn
4945                      (setq sequence (cdr sequence))
4946                      (setq header (inline
4947                                     (gnus-nov-parse-line
4948                                      number dependencies force-new))))
4949                    (push header headers))
4950               (forward-line 1))
4951           (error
4952            (gnus-error 4 "Strange nov line (%d)"
4953                        (count-lines (point-min) (point)))))
4954         (forward-line 1))
4955       ;; A common bug in inn is that if you have posted an article and
4956       ;; then retrieves the active file, it will answer correctly --
4957       ;; the new article is included.  However, a NOV entry for the
4958       ;; article may not have been generated yet, so this may fail.
4959       ;; We work around this problem by retrieving the last few
4960       ;; headers using HEAD.
4961       (if (or (not also-fetch-heads)
4962               (not sequence))
4963           ;; We (probably) got all the headers.
4964           (nreverse headers)
4965         (let ((gnus-nov-is-evil t))
4966           (nconc
4967            (nreverse headers)
4968            (gnus-retrieve-parsed-headers sequence group)
4969            ))))))
4970
4971 (defun gnus-article-get-xrefs ()
4972   "Fill in the Xref value in `gnus-current-headers', if necessary.
4973 This is meant to be called in `gnus-article-internal-prepare-hook'."
4974   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4975                                  gnus-current-headers)))
4976     (or (not gnus-use-cross-reference)
4977         (not headers)
4978         (and (mail-header-xref headers)
4979              (not (string= (mail-header-xref headers) "")))
4980         (let ((case-fold-search t)
4981               xref)
4982           (save-restriction
4983             (nnheader-narrow-to-headers)
4984             (goto-char (point-min))
4985             (when (or (and (not (eobp))
4986                            (eq (downcase (char-after)) ?x)
4987                            (looking-at "Xref:"))
4988                       (search-forward "\nXref:" nil t))
4989               (goto-char (1+ (match-end 0)))
4990               (setq xref (buffer-substring (point)
4991                                            (progn (end-of-line) (point))))
4992               (mail-header-set-xref headers xref)))))))
4993
4994 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4995   "Find article ID and insert the summary line for that article.
4996 OLD-HEADER can either be a header or a line number to insert
4997 the subject line on."
4998   (let* ((line (and (numberp old-header) old-header))
4999          (old-header (and (vectorp old-header) old-header))
5000          (header (cond ((and old-header use-old-header)
5001                         old-header)
5002                        ((and (numberp id)
5003                              (gnus-number-to-header id))
5004                         (gnus-number-to-header id))
5005                        (t
5006                         (gnus-read-header id))))
5007          (number (and (numberp id) id))
5008          d)
5009     (when header
5010       ;; Rebuild the thread that this article is part of and go to the
5011       ;; article we have fetched.
5012       (when (and (not gnus-show-threads)
5013                  old-header)
5014         (when (and number
5015                    (setq d (gnus-data-find (mail-header-number old-header))))
5016           (goto-char (gnus-data-pos d))
5017           (gnus-data-remove
5018            number
5019            (- (gnus-point-at-bol)
5020               (prog1
5021                   (1+ (gnus-point-at-eol))
5022                 (gnus-delete-line))))))
5023       (when old-header
5024         (mail-header-set-number header (mail-header-number old-header)))
5025       (setq gnus-newsgroup-sparse
5026             (delq (setq number (mail-header-number header))
5027                   gnus-newsgroup-sparse))
5028       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5029       (push number gnus-newsgroup-limit)
5030       (gnus-rebuild-thread (mail-header-id header) line)
5031       (gnus-summary-goto-subject number nil t))
5032     (when (and (numberp number)
5033                (> number 0))
5034       ;; We have to update the boundaries even if we can't fetch the
5035       ;; article if ID is a number -- so that the next `P' or `N'
5036       ;; command will fetch the previous (or next) article even
5037       ;; if the one we tried to fetch this time has been canceled.
5038       (when (> number gnus-newsgroup-end)
5039         (setq gnus-newsgroup-end number))
5040       (when (< number gnus-newsgroup-begin)
5041         (setq gnus-newsgroup-begin number))
5042       (setq gnus-newsgroup-unselected
5043             (delq number gnus-newsgroup-unselected)))
5044     ;; Report back a success?
5045     (and header (mail-header-number header))))
5046
5047 ;;; Process/prefix in the summary buffer
5048
5049 (defun gnus-summary-work-articles (n)
5050   "Return a list of articles to be worked upon.
5051 The prefix argument, the list of process marked articles, and the
5052 current article will be taken into consideration."
5053   (save-excursion
5054     (set-buffer gnus-summary-buffer)
5055     (cond
5056      (n
5057       ;; A numerical prefix has been given.
5058       (setq n (prefix-numeric-value n))
5059       (let ((backward (< n 0))
5060             (n (abs (prefix-numeric-value n)))
5061             articles article)
5062         (save-excursion
5063           (while
5064               (and (> n 0)
5065                    (push (setq article (gnus-summary-article-number))
5066                          articles)
5067                    (if backward
5068                        (gnus-summary-find-prev nil article)
5069                      (gnus-summary-find-next nil article)))
5070             (decf n)))
5071         (nreverse articles)))
5072      ((and (gnus-region-active-p) (mark))
5073       (message "region active")
5074       ;; Work on the region between point and mark.
5075       (let ((max (max (point) (mark)))
5076             articles article)
5077         (save-excursion
5078           (goto-char (min (point) (mark)))
5079           (while
5080               (and
5081                (push (setq article (gnus-summary-article-number)) articles)
5082                (gnus-summary-find-next nil article)
5083                (< (point) max)))
5084           (nreverse articles))))
5085      (gnus-newsgroup-processable
5086       ;; There are process-marked articles present.
5087       ;; Save current state.
5088       (gnus-summary-save-process-mark)
5089       ;; Return the list.
5090       (reverse gnus-newsgroup-processable))
5091      (t
5092       ;; Just return the current article.
5093       (list (gnus-summary-article-number))))))
5094
5095 (defmacro gnus-summary-iterate (arg &rest forms)
5096   "Iterate over the process/prefixed articles and do FORMS.
5097 ARG is the interactive prefix given to the command.  FORMS will be
5098 executed with point over the summary line of the articles."
5099   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5100     `(let ((,articles (gnus-summary-work-articles ,arg)))
5101        (while ,articles
5102          (gnus-summary-goto-subject (car ,articles))
5103          ,@forms
5104          (pop ,articles)))))
5105
5106 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5107 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5108
5109 (defun gnus-summary-save-process-mark ()
5110   "Push the current set of process marked articles on the stack."
5111   (interactive)
5112   (push (copy-sequence gnus-newsgroup-processable)
5113         gnus-newsgroup-process-stack))
5114
5115 (defun gnus-summary-kill-process-mark ()
5116   "Push the current set of process marked articles on the stack and unmark."
5117   (interactive)
5118   (gnus-summary-save-process-mark)
5119   (gnus-summary-unmark-all-processable))
5120
5121 (defun gnus-summary-yank-process-mark ()
5122   "Pop the last process mark state off the stack and restore it."
5123   (interactive)
5124   (unless gnus-newsgroup-process-stack
5125     (error "Empty mark stack"))
5126   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5127
5128 (defun gnus-summary-process-mark-set (set)
5129   "Make SET into the current process marked articles."
5130   (gnus-summary-unmark-all-processable)
5131   (while set
5132     (gnus-summary-set-process-mark (pop set))))
5133
5134 ;;; Searching and stuff
5135
5136 (defun gnus-summary-search-group (&optional backward use-level)
5137   "Search for next unread newsgroup.
5138 If optional argument BACKWARD is non-nil, search backward instead."
5139   (save-excursion
5140     (set-buffer gnus-group-buffer)
5141     (when (gnus-group-search-forward
5142            backward nil (if use-level (gnus-group-group-level) nil))
5143       (gnus-group-group-name))))
5144
5145 (defun gnus-summary-best-group (&optional exclude-group)
5146   "Find the name of the best unread group.
5147 If EXCLUDE-GROUP, do not go to this group."
5148   (save-excursion
5149     (set-buffer gnus-group-buffer)
5150     (save-excursion
5151       (gnus-group-best-unread-group exclude-group))))
5152
5153 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5154   (if backward (gnus-summary-find-prev)
5155     (let* ((dummy (gnus-summary-article-intangible-p))
5156            (article (or article (gnus-summary-article-number)))
5157            (arts (gnus-data-find-list article))
5158            result)
5159       (when (and (not dummy)
5160                  (or (not gnus-summary-check-current)
5161                      (not unread)
5162                      (not (gnus-data-unread-p (car arts)))))
5163         (setq arts (cdr arts)))
5164       (when (setq result
5165                   (if unread
5166                       (progn
5167                         (while arts
5168                           (when (or (and undownloaded
5169                                          (eq gnus-undownloaded-mark
5170                                              (gnus-data-mark (car arts))))
5171                                     (gnus-data-unread-p (car arts)))
5172                             (setq result (car arts)
5173                                   arts nil))
5174                           (setq arts (cdr arts)))
5175                         result)
5176                     (car arts)))
5177         (goto-char (gnus-data-pos result))
5178         (gnus-data-number result)))))
5179
5180 (defun gnus-summary-find-prev (&optional unread article)
5181   (let* ((eobp (eobp))
5182          (article (or article (gnus-summary-article-number)))
5183          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5184          result)
5185     (when (and (not eobp)
5186                (or (not gnus-summary-check-current)
5187                    (not unread)
5188                    (not (gnus-data-unread-p (car arts)))))
5189       (setq arts (cdr arts)))
5190     (when (setq result
5191                 (if unread
5192                     (progn
5193                       (while arts
5194                         (when (gnus-data-unread-p (car arts))
5195                           (setq result (car arts)
5196                                 arts nil))
5197                         (setq arts (cdr arts)))
5198                       result)
5199                   (car arts)))
5200       (goto-char (gnus-data-pos result))
5201       (gnus-data-number result))))
5202
5203 (defun gnus-summary-find-subject (subject &optional unread backward article)
5204   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5205          (article (or article (gnus-summary-article-number)))
5206          (articles (gnus-data-list backward))
5207          (arts (gnus-data-find-list article articles))
5208          result)
5209     (when (or (not gnus-summary-check-current)
5210               (not unread)
5211               (not (gnus-data-unread-p (car arts))))
5212       (setq arts (cdr arts)))
5213     (while arts
5214       (and (or (not unread)
5215                (gnus-data-unread-p (car arts)))
5216            (vectorp (gnus-data-header (car arts)))
5217            (gnus-subject-equal
5218             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5219            (setq result (car arts)
5220                  arts nil))
5221       (setq arts (cdr arts)))
5222     (and result
5223          (goto-char (gnus-data-pos result))
5224          (gnus-data-number result))))
5225
5226 (defun gnus-summary-search-forward (&optional unread subject backward)
5227   "Search forward for an article.
5228 If UNREAD, look for unread articles.  If SUBJECT, look for
5229 articles with that subject.  If BACKWARD, search backward instead."
5230   (cond (subject (gnus-summary-find-subject subject unread backward))
5231         (backward (gnus-summary-find-prev unread))
5232         (t (gnus-summary-find-next unread))))
5233
5234 (defun gnus-recenter (&optional n)
5235   "Center point in window and redisplay frame.
5236 Also do horizontal recentering."
5237   (interactive "P")
5238   (when (and gnus-auto-center-summary
5239              (not (eq gnus-auto-center-summary 'vertical)))
5240     (gnus-horizontal-recenter))
5241   (recenter n))
5242
5243 (defun gnus-summary-recenter ()
5244   "Center point in the summary window.
5245 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5246 displayed, no centering will be performed."
5247   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5248   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5249   (interactive)
5250   (let* ((top (cond ((< (window-height) 4) 0)
5251                     ((< (window-height) 7) 1)
5252                     (t (if (numberp gnus-auto-center-summary)
5253                            gnus-auto-center-summary
5254                          2))))
5255          (height (1- (window-height)))
5256          (bottom (save-excursion (goto-char (point-max))
5257                                  (forward-line (- height))
5258                                  (point)))
5259          (window (get-buffer-window (current-buffer))))
5260     ;; The user has to want it.
5261     (when gnus-auto-center-summary
5262       (when (get-buffer-window gnus-article-buffer)
5263         ;; Only do recentering when the article buffer is displayed,
5264         ;; Set the window start to either `bottom', which is the biggest
5265         ;; possible valid number, or the second line from the top,
5266         ;; whichever is the least.
5267         (set-window-start
5268          window (min bottom (save-excursion
5269                               (forward-line (- top)) (point)))
5270          t))
5271       ;; Do horizontal recentering while we're at it.
5272       (when (and (get-buffer-window (current-buffer) t)
5273                  (not (eq gnus-auto-center-summary 'vertical)))
5274         (let ((selected (selected-window)))
5275           (select-window (get-buffer-window (current-buffer) t))
5276           (gnus-summary-position-point)
5277           (gnus-horizontal-recenter)
5278           (select-window selected))))))
5279
5280 (defun gnus-summary-jump-to-group (newsgroup)
5281   "Move point to NEWSGROUP in group mode buffer."
5282   ;; Keep update point of group mode buffer if visible.
5283   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5284       (save-window-excursion
5285         ;; Take care of tree window mode.
5286         (when (get-buffer-window gnus-group-buffer)
5287           (pop-to-buffer gnus-group-buffer))
5288         (gnus-group-jump-to-group newsgroup))
5289     (save-excursion
5290       ;; Take care of tree window mode.
5291       (if (get-buffer-window gnus-group-buffer)
5292           (pop-to-buffer gnus-group-buffer)
5293         (set-buffer gnus-group-buffer))
5294       (gnus-group-jump-to-group newsgroup))))
5295
5296 ;; This function returns a list of article numbers based on the
5297 ;; difference between the ranges of read articles in this group and
5298 ;; the range of active articles.
5299 (defun gnus-list-of-unread-articles (group)
5300   (let* ((read (gnus-info-read (gnus-get-info group)))
5301          (active (or (gnus-active group) (gnus-activate-group group)))
5302          (last (cdr active))
5303          first nlast unread)
5304     ;; If none are read, then all are unread.
5305     (if (not read)
5306         (setq first (car active))
5307       ;; If the range of read articles is a single range, then the
5308       ;; first unread article is the article after the last read
5309       ;; article.  Sounds logical, doesn't it?
5310       (if (and (not (listp (cdr read)))
5311                (or (< (car read) (car active))
5312                    (progn (setq read (list read))
5313                           nil)))
5314           (setq first (max (car active) (1+ (cdr read))))
5315         ;; `read' is a list of ranges.
5316         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5317                                   (caar read)))
5318                   1)
5319           (setq first (car active)))
5320         (while read
5321           (when first
5322             (while (< first nlast)
5323               (push first unread)
5324               (setq first (1+ first))))
5325           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5326           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5327           (setq read (cdr read)))))
5328     ;; And add the last unread articles.
5329     (while (<= first last)
5330       (push first unread)
5331       (setq first (1+ first)))
5332     ;; Return the list of unread articles.
5333     (delq 0 (nreverse unread))))
5334
5335 (defun gnus-list-of-read-articles (group)
5336   "Return a list of unread, unticked and non-dormant articles."
5337   (let* ((info (gnus-get-info group))
5338          (marked (gnus-info-marks info))
5339          (active (gnus-active group)))
5340     (and info active
5341          (gnus-set-difference
5342           (gnus-sorted-complement
5343            (gnus-uncompress-range active)
5344            (gnus-list-of-unread-articles group))
5345           (append
5346            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5347            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5348
5349 ;; Various summary commands
5350
5351 (defun gnus-summary-select-article-buffer ()
5352   "Reconfigure windows to show article buffer."
5353   (interactive)
5354   (if (not (gnus-buffer-live-p gnus-article-buffer))
5355       (error "There is no article buffer for this summary buffer")
5356     (gnus-configure-windows 'article)
5357     (select-window (get-buffer-window gnus-article-buffer))))
5358
5359 (defun gnus-summary-universal-argument (arg)
5360   "Perform any operation on all articles that are process/prefixed."
5361   (interactive "P")
5362   (let ((articles (gnus-summary-work-articles arg))
5363         func article)
5364     (if (eq
5365          (setq
5366           func
5367           (key-binding
5368            (read-key-sequence
5369             (substitute-command-keys
5370              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5371          'undefined)
5372         (gnus-error 1 "Undefined key")
5373       (save-excursion
5374         (while articles
5375           (gnus-summary-goto-subject (setq article (pop articles)))
5376           (let (gnus-newsgroup-processable)
5377             (command-execute func))
5378           (gnus-summary-remove-process-mark article)))))
5379   (gnus-summary-position-point))
5380
5381 (defun gnus-summary-toggle-truncation (&optional arg)
5382   "Toggle truncation of summary lines.
5383 With arg, turn line truncation on iff arg is positive."
5384   (interactive "P")
5385   (setq truncate-lines
5386         (if (null arg) (not truncate-lines)
5387           (> (prefix-numeric-value arg) 0)))
5388   (redraw-display))
5389
5390 (defun gnus-summary-reselect-current-group (&optional all rescan)
5391   "Exit and then reselect the current newsgroup.
5392 The prefix argument ALL means to select all articles."
5393   (interactive "P")
5394   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5395     (error "Ephemeral groups can't be reselected"))
5396   (let ((current-subject (gnus-summary-article-number))
5397         (group gnus-newsgroup-name))
5398     (setq gnus-newsgroup-begin nil)
5399     (gnus-summary-exit)
5400     ;; We have to adjust the point of group mode buffer because
5401     ;; point was moved to the next unread newsgroup by exiting.
5402     (gnus-summary-jump-to-group group)
5403     (when rescan
5404       (save-excursion
5405         (save-window-excursion
5406           ;; Don't show group contents.
5407           (set-window-start (selected-window) (point-max))
5408           (gnus-group-get-new-news-this-group 1))))
5409     (gnus-group-read-group all t)
5410     (gnus-summary-goto-subject current-subject nil t)))
5411
5412 (defun gnus-summary-rescan-group (&optional all)
5413   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5414   (interactive "P")
5415   (gnus-summary-reselect-current-group all t))
5416
5417 (defun gnus-summary-update-info (&optional non-destructive)
5418   (save-excursion
5419     (let ((group gnus-newsgroup-name))
5420       (when group
5421         (when gnus-newsgroup-kill-headers
5422           (setq gnus-newsgroup-killed
5423                 (gnus-compress-sequence
5424                  (nconc
5425                   (gnus-set-sorted-intersection
5426                    (gnus-uncompress-range gnus-newsgroup-killed)
5427                    (setq gnus-newsgroup-unselected
5428                          (sort gnus-newsgroup-unselected '<)))
5429                   (setq gnus-newsgroup-unreads
5430                         (sort gnus-newsgroup-unreads '<)))
5431                  t)))
5432         (unless (listp (cdr gnus-newsgroup-killed))
5433           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5434         (let ((headers gnus-newsgroup-headers))
5435           ;; Set the new ranges of read articles.
5436           (save-excursion
5437             (set-buffer gnus-group-buffer)
5438             (gnus-undo-force-boundary))
5439           (gnus-update-read-articles
5440            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5441           ;; Set the current article marks.
5442           (let ((gnus-newsgroup-scored
5443                  (if (and (not gnus-save-score)
5444                           (not non-destructive))
5445                      nil
5446                    gnus-newsgroup-scored)))
5447             (save-excursion
5448               (gnus-update-marks)))
5449           ;; Do the cross-ref thing.
5450           (when gnus-use-cross-reference
5451             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5452           ;; Do not switch windows but change the buffer to work.
5453           (set-buffer gnus-group-buffer)
5454           (unless (gnus-ephemeral-group-p group)
5455             (gnus-group-update-group group)))))))
5456
5457 (defun gnus-summary-save-newsrc (&optional force)
5458   "Save the current number of read/marked articles in the dribble buffer.
5459 The dribble buffer will then be saved.
5460 If FORCE (the prefix), also save the .newsrc file(s)."
5461   (interactive "P")
5462   (gnus-summary-update-info t)
5463   (if force
5464       (gnus-save-newsrc-file)
5465     (gnus-dribble-save)))
5466
5467 (defun gnus-summary-exit (&optional temporary)
5468   "Exit reading current newsgroup, and then return to group selection mode.
5469 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5470   (interactive)
5471   (gnus-set-global-variables)
5472   (gnus-kill-save-kill-buffer)
5473   (gnus-async-halt-prefetch)
5474   (let* ((group gnus-newsgroup-name)
5475          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5476          (mode major-mode)
5477          (group-point nil)
5478          (buf (current-buffer)))
5479     (unless quit-config
5480       ;; Do adaptive scoring, and possibly save score files.
5481       (when gnus-newsgroup-adaptive
5482         (gnus-score-adaptive))
5483       (when gnus-use-scoring
5484         (gnus-score-save)))
5485     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5486     ;; If we have several article buffers, we kill them at exit.
5487     (unless gnus-single-article-buffer
5488       (gnus-kill-buffer gnus-original-article-buffer)
5489       (setq gnus-article-current nil))
5490     (when gnus-use-cache
5491       (gnus-cache-possibly-remove-articles)
5492       (gnus-cache-save-buffers))
5493     (gnus-async-prefetch-remove-group group)
5494     (when gnus-suppress-duplicates
5495       (gnus-dup-enter-articles))
5496     (when gnus-use-trees
5497       (gnus-tree-close group))
5498     (when gnus-use-cache
5499       (gnus-cache-write-active))
5500     ;; Remove entries for this group.
5501     (nnmail-purge-split-history (gnus-group-real-name group))
5502     ;; Make all changes in this group permanent.
5503     (unless quit-config
5504       (gnus-run-hooks 'gnus-exit-group-hook)
5505       (gnus-summary-update-info))
5506     (gnus-close-group group)
5507     ;; Make sure where we were, and go to next newsgroup.
5508     (set-buffer gnus-group-buffer)
5509     (unless quit-config
5510       (gnus-group-jump-to-group group))
5511     (gnus-run-hooks 'gnus-summary-exit-hook)
5512     (unless (or quit-config
5513                 ;; If this group has disappeared from the summary
5514                 ;; buffer, don't skip forwards.
5515                 (not (string= group (gnus-group-group-name))))
5516       (gnus-group-next-unread-group 1))
5517     (setq group-point (point))
5518     (if temporary
5519         nil                             ;Nothing to do.
5520       ;; If we have several article buffers, we kill them at exit.
5521       (unless gnus-single-article-buffer
5522         (gnus-kill-buffer gnus-article-buffer)
5523         (gnus-kill-buffer gnus-original-article-buffer)
5524         (setq gnus-article-current nil))
5525       (set-buffer buf)
5526       (if (not gnus-kill-summary-on-exit)
5527           (gnus-deaden-summary)
5528         ;; We set all buffer-local variables to nil.  It is unclear why
5529         ;; this is needed, but if we don't, buffer-local variables are
5530         ;; not garbage-collected, it seems.  This would the lead to en
5531         ;; ever-growing Emacs.
5532         (gnus-summary-clear-local-variables)
5533         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5534           (gnus-summary-clear-local-variables))
5535         (when (get-buffer gnus-article-buffer)
5536           (bury-buffer gnus-article-buffer))
5537         ;; We clear the global counterparts of the buffer-local
5538         ;; variables as well, just to be on the safe side.
5539         (set-buffer gnus-group-buffer)
5540         (gnus-summary-clear-local-variables)
5541         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5542           (gnus-summary-clear-local-variables))
5543         ;; Return to group mode buffer.
5544         (when (eq mode 'gnus-summary-mode)
5545           (gnus-kill-buffer buf)))
5546       (setq gnus-current-select-method gnus-select-method)
5547       (pop-to-buffer gnus-group-buffer)
5548       (if (not quit-config)
5549           (progn
5550             (goto-char group-point)
5551             (gnus-configure-windows 'group 'force)
5552             (unless (pos-visible-in-window-p)
5553               (forward-line (/ (static-if (featurep 'xemacs)
5554                                    (window-displayed-height)
5555                                  (1- (window-height)))
5556                                -2))
5557               (set-window-start (selected-window) (point))
5558               (goto-char group-point)))
5559         (gnus-handle-ephemeral-exit quit-config))
5560       ;; Clear the current group name.
5561       (unless quit-config
5562         (setq gnus-newsgroup-name nil)))))
5563
5564 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5565 (defun gnus-summary-exit-no-update (&optional no-questions)
5566   "Quit reading current newsgroup without updating read article info."
5567   (interactive)
5568   (let* ((group gnus-newsgroup-name)
5569          (quit-config (gnus-group-quit-config group)))
5570     (when (or no-questions
5571               gnus-expert-user
5572               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5573       (gnus-async-halt-prefetch)
5574       (mapcar 'funcall
5575               (delq 'gnus-summary-expire-articles
5576                     (copy-sequence gnus-summary-prepare-exit-hook)))
5577       ;; If we have several article buffers, we kill them at exit.
5578       (unless gnus-single-article-buffer
5579         (gnus-kill-buffer gnus-article-buffer)
5580         (gnus-kill-buffer gnus-original-article-buffer)
5581         (setq gnus-article-current nil))
5582       (if (not gnus-kill-summary-on-exit)
5583           (gnus-deaden-summary)
5584         (gnus-close-group group)
5585         (gnus-summary-clear-local-variables)
5586         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5587           (gnus-summary-clear-local-variables))
5588         (set-buffer gnus-group-buffer)
5589         (gnus-summary-clear-local-variables)
5590         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5591           (gnus-summary-clear-local-variables))
5592         (when (get-buffer gnus-summary-buffer)
5593           (kill-buffer gnus-summary-buffer)))
5594       (unless gnus-single-article-buffer
5595         (setq gnus-article-current nil))
5596       (when gnus-use-trees
5597         (gnus-tree-close group))
5598       (gnus-async-prefetch-remove-group group)
5599       (when (get-buffer gnus-article-buffer)
5600         (bury-buffer gnus-article-buffer))
5601       ;; Return to the group buffer.
5602       (gnus-configure-windows 'group 'force)
5603       ;; Clear the current group name.
5604       (setq gnus-newsgroup-name nil)
5605       (when (equal (gnus-group-group-name) group)
5606         (gnus-group-next-unread-group 1))
5607       (when quit-config
5608         (gnus-handle-ephemeral-exit quit-config)))))
5609
5610 (defun gnus-handle-ephemeral-exit (quit-config)
5611   "Handle movement when leaving an ephemeral group.
5612 The state which existed when entering the ephemeral is reset."
5613   (if (not (buffer-name (car quit-config)))
5614       (gnus-configure-windows 'group 'force)
5615     (set-buffer (car quit-config))
5616     (cond ((eq major-mode 'gnus-summary-mode)
5617            (gnus-set-global-variables))
5618           ((eq major-mode 'gnus-article-mode)
5619            (save-excursion
5620              ;; The `gnus-summary-buffer' variable may point
5621              ;; to the old summary buffer when using a single
5622              ;; article buffer.
5623              (unless (gnus-buffer-live-p gnus-summary-buffer)
5624                (set-buffer gnus-group-buffer))
5625              (set-buffer gnus-summary-buffer)
5626              (gnus-set-global-variables))))
5627     (if (or (eq (cdr quit-config) 'article)
5628             (eq (cdr quit-config) 'pick))
5629         (progn
5630           ;; The current article may be from the ephemeral group
5631           ;; thus it is best that we reload this article
5632           (gnus-summary-show-article)
5633           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5634               (gnus-configure-windows 'pick 'force)
5635             (gnus-configure-windows (cdr quit-config) 'force)))
5636       (gnus-configure-windows (cdr quit-config) 'force))
5637     (when (eq major-mode 'gnus-summary-mode)
5638       (gnus-summary-next-subject 1 nil t)
5639       (gnus-summary-recenter)
5640       (gnus-summary-position-point))))
5641
5642 (defun gnus-summary-preview-mime-message ()
5643   "MIME decode and play this message."
5644   (interactive)
5645   (let ((gnus-break-pages nil)
5646         (gnus-show-mime t))
5647     (gnus-summary-select-article gnus-show-all-headers t))
5648   (select-window (get-buffer-window gnus-article-buffer)))
5649
5650 ;;; Dead summaries.
5651
5652 (defvar gnus-dead-summary-mode-map nil)
5653
5654 (unless gnus-dead-summary-mode-map
5655   (setq gnus-dead-summary-mode-map (make-keymap))
5656   (suppress-keymap gnus-dead-summary-mode-map)
5657   (substitute-key-definition
5658    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5659   (let ((keys '("\C-d" "\r" "\177" [delete])))
5660     (while keys
5661       (define-key gnus-dead-summary-mode-map
5662         (pop keys) 'gnus-summary-wake-up-the-dead))))
5663
5664 (defvar gnus-dead-summary-mode nil
5665   "Minor mode for Gnus summary buffers.")
5666
5667 (defun gnus-dead-summary-mode (&optional arg)
5668   "Minor mode for Gnus summary buffers."
5669   (interactive "P")
5670   (when (eq major-mode 'gnus-summary-mode)
5671     (make-local-variable 'gnus-dead-summary-mode)
5672     (setq gnus-dead-summary-mode
5673           (if (null arg) (not gnus-dead-summary-mode)
5674             (> (prefix-numeric-value arg) 0)))
5675     (when gnus-dead-summary-mode
5676       (gnus-add-minor-mode
5677        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5678
5679 (defun gnus-deaden-summary ()
5680   "Make the current summary buffer into a dead summary buffer."
5681   ;; Kill any previous dead summary buffer.
5682   (when (and gnus-dead-summary
5683              (buffer-name gnus-dead-summary))
5684     (save-excursion
5685       (set-buffer gnus-dead-summary)
5686       (when gnus-dead-summary-mode
5687         (kill-buffer (current-buffer)))))
5688   ;; Make this the current dead summary.
5689   (setq gnus-dead-summary (current-buffer))
5690   (gnus-dead-summary-mode 1)
5691   (let ((name (buffer-name)))
5692     (when (string-match "Summary" name)
5693       (rename-buffer
5694        (concat (substring name 0 (match-beginning 0)) "Dead "
5695                (substring name (match-beginning 0)))
5696        t)
5697       (bury-buffer))))
5698
5699 (defun gnus-kill-or-deaden-summary (buffer)
5700   "Kill or deaden the summary BUFFER."
5701   (save-excursion
5702     (when (and (buffer-name buffer)
5703                (not gnus-single-article-buffer))
5704       (save-excursion
5705         (set-buffer buffer)
5706         (gnus-kill-buffer gnus-article-buffer)
5707         (gnus-kill-buffer gnus-original-article-buffer)))
5708     (cond (gnus-kill-summary-on-exit
5709            (when (and gnus-use-trees
5710                       (gnus-buffer-exists-p buffer))
5711              (save-excursion
5712                (set-buffer buffer)
5713                (gnus-tree-close gnus-newsgroup-name)))
5714            (gnus-kill-buffer buffer))
5715           ((gnus-buffer-exists-p buffer)
5716            (save-excursion
5717              (set-buffer buffer)
5718              (gnus-deaden-summary))))))
5719
5720 (defun gnus-summary-wake-up-the-dead (&rest args)
5721   "Wake up the dead summary buffer."
5722   (interactive)
5723   (gnus-dead-summary-mode -1)
5724   (let ((name (buffer-name)))
5725     (when (string-match "Dead " name)
5726       (rename-buffer
5727        (concat (substring name 0 (match-beginning 0))
5728                (substring name (match-end 0)))
5729        t)))
5730   (gnus-message 3 "This dead summary is now alive again"))
5731
5732 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5733 (defun gnus-summary-fetch-faq (&optional faq-dir)
5734   "Fetch the FAQ for the current group.
5735 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5736 in."
5737   (interactive
5738    (list
5739     (when current-prefix-arg
5740       (completing-read
5741        "Faq dir: " (and (listp gnus-group-faq-directory)
5742                         (mapcar (lambda (file) (list file))
5743                                 gnus-group-faq-directory))))))
5744   (let (gnus-faq-buffer)
5745     (when (setq gnus-faq-buffer
5746                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5747       (gnus-configure-windows 'summary-faq))))
5748
5749 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5750 (defun gnus-summary-describe-group (&optional force)
5751   "Describe the current newsgroup."
5752   (interactive "P")
5753   (gnus-group-describe-group force gnus-newsgroup-name))
5754
5755 (defun gnus-summary-describe-briefly ()
5756   "Describe summary mode commands briefly."
5757   (interactive)
5758   (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")))
5759
5760 ;; Walking around group mode buffer from summary mode.
5761
5762 (defun gnus-summary-next-group (&optional no-article target-group backward)
5763   "Exit current newsgroup and then select next unread newsgroup.
5764 If prefix argument NO-ARTICLE is non-nil, no article is selected
5765 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5766 previous group instead."
5767   (interactive "P")
5768   ;; Stop pre-fetching.
5769   (gnus-async-halt-prefetch)
5770   (let ((current-group gnus-newsgroup-name)
5771         (current-buffer (current-buffer))
5772         entered)
5773     ;; First we semi-exit this group to update Xrefs and all variables.
5774     ;; We can't do a real exit, because the window conf must remain
5775     ;; the same in case the user is prompted for info, and we don't
5776     ;; want the window conf to change before that...
5777     (gnus-summary-exit t)
5778     (while (not entered)
5779       ;; Then we find what group we are supposed to enter.
5780       (set-buffer gnus-group-buffer)
5781       (gnus-group-jump-to-group current-group)
5782       (setq target-group
5783             (or target-group
5784                 (if (eq gnus-keep-same-level 'best)
5785                     (gnus-summary-best-group gnus-newsgroup-name)
5786                   (gnus-summary-search-group backward gnus-keep-same-level))))
5787       (if (not target-group)
5788           ;; There are no further groups, so we return to the group
5789           ;; buffer.
5790           (progn
5791             (gnus-message 5 "Returning to the group buffer")
5792             (setq entered t)
5793             (when (gnus-buffer-live-p current-buffer)
5794               (set-buffer current-buffer)
5795               (gnus-summary-exit))
5796             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5797         ;; We try to enter the target group.
5798         (gnus-group-jump-to-group target-group)
5799         (let ((unreads (gnus-group-group-unread)))
5800           (if (and (or (eq t unreads)
5801                        (and unreads (not (zerop unreads))))
5802                    (gnus-summary-read-group
5803                     target-group nil no-article
5804                     (and (buffer-name current-buffer) current-buffer)
5805                     nil backward))
5806               (setq entered t)
5807             (setq current-group target-group
5808                   target-group nil)))))))
5809
5810 (defun gnus-summary-prev-group (&optional no-article)
5811   "Exit current newsgroup and then select previous unread newsgroup.
5812 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5813   (interactive "P")
5814   (gnus-summary-next-group no-article nil t))
5815
5816 ;; Walking around summary lines.
5817
5818 (defun gnus-summary-first-subject (&optional unread undownloaded)
5819   "Go to the first unread subject.
5820 If UNREAD is non-nil, go to the first unread article.
5821 Returns the article selected or nil if there are no unread articles."
5822   (interactive "P")
5823   (prog1
5824       (cond
5825        ;; Empty summary.
5826        ((null gnus-newsgroup-data)
5827         (gnus-message 3 "No articles in the group")
5828         nil)
5829        ;; Pick the first article.
5830        ((not unread)
5831         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5832         (gnus-data-number (car gnus-newsgroup-data)))
5833        ;; No unread articles.
5834        ((null gnus-newsgroup-unreads)
5835         (gnus-message 3 "No more unread articles")
5836         nil)
5837        ;; Find the first unread article.
5838        (t
5839         (let ((data gnus-newsgroup-data))
5840           (while (and data
5841                       (and (not (and undownloaded
5842                                      (eq gnus-undownloaded-mark
5843                                          (gnus-data-mark (car data)))))
5844                            (not (gnus-data-unread-p (car data)))))
5845             (setq data (cdr data)))
5846           (when data
5847             (goto-char (gnus-data-pos (car data)))
5848             (gnus-data-number (car data))))))
5849     (gnus-summary-position-point)))
5850
5851 (defun gnus-summary-next-subject (n &optional unread dont-display)
5852   "Go to next N'th summary line.
5853 If N is negative, go to the previous N'th subject line.
5854 If UNREAD is non-nil, only unread articles are selected.
5855 The difference between N and the actual number of steps taken is
5856 returned."
5857   (interactive "p")
5858   (let ((backward (< n 0))
5859         (n (abs n)))
5860     (while (and (> n 0)
5861                 (if backward
5862                     (gnus-summary-find-prev unread)
5863                   (gnus-summary-find-next unread)))
5864       (unless (zerop (setq n (1- n)))
5865         (gnus-summary-show-thread)))
5866     (when (/= 0 n)
5867       (gnus-message 7 "No more%s articles"
5868                     (if unread " unread" "")))
5869     (unless dont-display
5870       (gnus-summary-recenter)
5871       (gnus-summary-position-point))
5872     n))
5873
5874 (defun gnus-summary-next-unread-subject (n)
5875   "Go to next N'th unread summary line."
5876   (interactive "p")
5877   (gnus-summary-next-subject n t))
5878
5879 (defun gnus-summary-prev-subject (n &optional unread)
5880   "Go to previous N'th summary line.
5881 If optional argument UNREAD is non-nil, only unread article is selected."
5882   (interactive "p")
5883   (gnus-summary-next-subject (- n) unread))
5884
5885 (defun gnus-summary-prev-unread-subject (n)
5886   "Go to previous N'th unread summary line."
5887   (interactive "p")
5888   (gnus-summary-next-subject (- n) t))
5889
5890 (defun gnus-summary-goto-subject (article &optional force silent)
5891   "Go the subject line of ARTICLE.
5892 If FORCE, also allow jumping to articles not currently shown."
5893   (interactive "nArticle number: ")
5894   (let ((b (point))
5895         (data (gnus-data-find article)))
5896     ;; We read in the article if we have to.
5897     (and (not data)
5898          force
5899          (gnus-summary-insert-subject
5900           article
5901           (if (or (numberp force) (vectorp force)) force)
5902           t)
5903          (setq data (gnus-data-find article)))
5904     (goto-char b)
5905     (if (not data)
5906         (progn
5907           (unless silent
5908             (gnus-message 3 "Can't find article %d" article))
5909           nil)
5910       (goto-char (gnus-data-pos data))
5911       (gnus-summary-position-point)
5912       article)))
5913
5914 ;; Walking around summary lines with displaying articles.
5915
5916 (defun gnus-summary-expand-window (&optional arg)
5917   "Make the summary buffer take up the entire Emacs frame.
5918 Given a prefix, will force an `article' buffer configuration."
5919   (interactive "P")
5920   (if arg
5921       (gnus-configure-windows 'article 'force)
5922     (gnus-configure-windows 'summary 'force)))
5923
5924 (defun gnus-summary-display-article (article &optional all-header)
5925   "Display ARTICLE in article buffer."
5926   (gnus-set-global-variables)
5927   (when (gnus-buffer-live-p gnus-article-buffer)
5928     (with-current-buffer gnus-article-buffer
5929       (setq gnus-article-charset gnus-newsgroup-charset)
5930       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
5931   (if (null article)
5932       nil
5933     (prog1
5934         (if gnus-summary-display-article-function
5935             (funcall gnus-summary-display-article-function article all-header)
5936           (gnus-article-prepare article all-header))
5937       (with-current-buffer gnus-article-buffer
5938         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5939              nil))
5940       (gnus-run-hooks 'gnus-select-article-hook)
5941       (when (and gnus-current-article
5942                  (not (zerop gnus-current-article)))
5943         (gnus-summary-goto-subject gnus-current-article))
5944       (gnus-summary-recenter)
5945       (when (and gnus-use-trees gnus-show-threads)
5946         (gnus-possibly-generate-tree article)
5947         (gnus-highlight-selected-tree article))
5948       ;; Successfully display article.
5949       (gnus-article-set-window-start
5950        (cdr (assq article gnus-newsgroup-bookmarks))))))
5951
5952 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5953   "Select the current article.
5954 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5955 non-nil, the article will be re-fetched even if it already present in
5956 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5957 be displayed."
5958   ;; Make sure we are in the summary buffer to work around bbdb bug.
5959   (unless (eq major-mode 'gnus-summary-mode)
5960     (set-buffer gnus-summary-buffer))
5961   (let ((article (or article (gnus-summary-article-number)))
5962         (all-headers (not (not all-headers))) ;Must be T or NIL.
5963         gnus-summary-display-article-function)
5964     (and (not pseudo)
5965          (gnus-summary-article-pseudo-p article)
5966          (error "This is a pseudo-article"))
5967     (save-excursion
5968       (set-buffer gnus-summary-buffer)
5969       (if (or (and gnus-single-article-buffer
5970                    (or (null gnus-current-article)
5971                        (null gnus-article-current)
5972                        (null (get-buffer gnus-article-buffer))
5973                        (not (eq article (cdr gnus-article-current)))
5974                        (not (equal (car gnus-article-current)
5975                                    gnus-newsgroup-name))))
5976               (and (not gnus-single-article-buffer)
5977                    (or (null gnus-current-article)
5978                        (not (eq gnus-current-article article))))
5979               force)
5980           ;; The requested article is different from the current article.
5981           (progn
5982             (gnus-summary-display-article article all-headers)
5983             (when (or all-headers gnus-show-all-headers)
5984               (gnus-article-show-all-headers))
5985             (gnus-article-set-window-start
5986              (cdr (assq article gnus-newsgroup-bookmarks)))
5987             article)
5988         (when (or all-headers gnus-show-all-headers)
5989           (gnus-article-show-all-headers))
5990         'old))))
5991
5992 (defun gnus-summary-force-verify-and-decrypt ()
5993   (interactive)
5994   (let ((mm-verify-option 'known)
5995         (mm-decrypt-option 'known))
5996     (gnus-summary-select-article nil 'force)))
5997
5998 (defun gnus-summary-set-current-mark (&optional current-mark)
5999   "Obsolete function."
6000   nil)
6001
6002 (defun gnus-summary-next-article (&optional unread subject backward push)
6003   "Select the next article.
6004 If UNREAD, only unread articles are selected.
6005 If SUBJECT, only articles with SUBJECT are selected.
6006 If BACKWARD, the previous article is selected instead of the next."
6007   (interactive "P")
6008   (cond
6009    ;; Is there such an article?
6010    ((and (gnus-summary-search-forward unread subject backward)
6011          (or (gnus-summary-display-article (gnus-summary-article-number))
6012              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6013     (gnus-summary-position-point))
6014    ;; If not, we try the first unread, if that is wanted.
6015    ((and subject
6016          gnus-auto-select-same
6017          (gnus-summary-first-unread-article))
6018     (gnus-summary-position-point)
6019     (gnus-message 6 "Wrapped"))
6020    ;; Try to get next/previous article not displayed in this group.
6021    ((and gnus-auto-extend-newsgroup
6022          (not unread) (not subject))
6023     (gnus-summary-goto-article
6024      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6025      nil (count-lines (point-min) (point))))
6026    ;; Go to next/previous group.
6027    (t
6028     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6029       (gnus-summary-jump-to-group gnus-newsgroup-name))
6030     (let ((cmd last-command-char)
6031           (point
6032            (save-excursion
6033              (set-buffer gnus-group-buffer)
6034              (point)))
6035           (group
6036            (if (eq gnus-keep-same-level 'best)
6037                (gnus-summary-best-group gnus-newsgroup-name)
6038              (gnus-summary-search-group backward gnus-keep-same-level))))
6039       ;; For some reason, the group window gets selected.  We change
6040       ;; it back.
6041       (select-window (get-buffer-window (current-buffer)))
6042       ;; Select next unread newsgroup automagically.
6043       (cond
6044        ((or (not gnus-auto-select-next)
6045             (not cmd))
6046         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6047        ((or (eq gnus-auto-select-next 'quietly)
6048             (and (eq gnus-auto-select-next 'slightly-quietly)
6049                  push)
6050             (and (eq gnus-auto-select-next 'almost-quietly)
6051                  (gnus-summary-last-article-p)))
6052         ;; Select quietly.
6053         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6054             (gnus-summary-exit)
6055           (gnus-message 7 "No more%s articles (%s)..."
6056                         (if unread " unread" "")
6057                         (if group (concat "selecting " group)
6058                           "exiting"))
6059           (gnus-summary-next-group nil group backward)))
6060        (t
6061         (when (gnus-key-press-event-p last-input-event)
6062           (gnus-summary-walk-group-buffer
6063            gnus-newsgroup-name cmd unread backward point))))))))
6064
6065 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6066   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6067                       (?\C-p (gnus-group-prev-unread-group 1))))
6068         (cursor-in-echo-area t)
6069         keve key group ended)
6070     (save-excursion
6071       (set-buffer gnus-group-buffer)
6072       (goto-char start)
6073       (setq group
6074             (if (eq gnus-keep-same-level 'best)
6075                 (gnus-summary-best-group gnus-newsgroup-name)
6076               (gnus-summary-search-group backward gnus-keep-same-level))))
6077     (while (not ended)
6078       (gnus-message
6079        5 "No more%s articles%s" (if unread " unread" "")
6080        (if (and group
6081                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6082            (format " (Type %s for %s [%s])"
6083                    (single-key-description cmd) group
6084                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6085          (format " (Type %s to exit %s)"
6086                  (single-key-description cmd)
6087                  gnus-newsgroup-name)))
6088       ;; Confirm auto selection.
6089       (setq key (car (setq keve (gnus-read-event-char))))
6090       (setq ended t)
6091       (cond
6092        ((assq key keystrokes)
6093         (let ((obuf (current-buffer)))
6094           (switch-to-buffer gnus-group-buffer)
6095           (when group
6096             (gnus-group-jump-to-group group))
6097           (eval (cadr (assq key keystrokes)))
6098           (setq group (gnus-group-group-name))
6099           (switch-to-buffer obuf))
6100         (setq ended nil))
6101        ((equal key cmd)
6102         (if (or (not group)
6103                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6104             (gnus-summary-exit)
6105           (gnus-summary-next-group nil group backward)))
6106        (t
6107         (push (cdr keve) unread-command-events))))))
6108
6109 (defun gnus-summary-next-unread-article ()
6110   "Select unread article after current one."
6111   (interactive)
6112   (gnus-summary-next-article
6113    (or (not (eq gnus-summary-goto-unread 'never))
6114        (gnus-summary-last-article-p (gnus-summary-article-number)))
6115    (and gnus-auto-select-same
6116         (gnus-summary-article-subject))))
6117
6118 (defun gnus-summary-prev-article (&optional unread subject)
6119   "Select the article after the current one.
6120 If UNREAD is non-nil, only unread articles are selected."
6121   (interactive "P")
6122   (gnus-summary-next-article unread subject t))
6123
6124 (defun gnus-summary-prev-unread-article ()
6125   "Select unread article before current one."
6126   (interactive)
6127   (gnus-summary-prev-article
6128    (or (not (eq gnus-summary-goto-unread 'never))
6129        (gnus-summary-first-article-p (gnus-summary-article-number)))
6130    (and gnus-auto-select-same
6131         (gnus-summary-article-subject))))
6132
6133 (defun gnus-summary-next-page (&optional lines circular)
6134   "Show next page of the selected article.
6135 If at the end of the current article, select the next article.
6136 LINES says how many lines should be scrolled up.
6137
6138 If CIRCULAR is non-nil, go to the start of the article instead of
6139 selecting the next article when reaching the end of the current
6140 article."
6141   (interactive "P")
6142   (setq gnus-summary-buffer (current-buffer))
6143   (gnus-set-global-variables)
6144   (let ((article (gnus-summary-article-number))
6145         (article-window (get-buffer-window gnus-article-buffer t))
6146         endp)
6147     ;; If the buffer is empty, we have no article.
6148     (unless article
6149       (error "No article to select"))
6150     (gnus-configure-windows 'article)
6151     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6152         (if (and (eq gnus-summary-goto-unread 'never)
6153                  (not (gnus-summary-last-article-p article)))
6154             (gnus-summary-next-article)
6155           (gnus-summary-next-unread-article))
6156       (if (or (null gnus-current-article)
6157               (null gnus-article-current)
6158               (/= article (cdr gnus-article-current))
6159               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6160           ;; Selected subject is different from current article's.
6161           (gnus-summary-display-article article)
6162         (when article-window
6163           (gnus-eval-in-buffer-window gnus-article-buffer
6164             (setq endp (gnus-article-next-page lines)))
6165           (when endp
6166             (cond (circular
6167                    (gnus-summary-beginning-of-article))
6168                   (lines
6169                    (gnus-message 3 "End of message"))
6170                   ((null lines)
6171                    (if (and (eq gnus-summary-goto-unread 'never)
6172                             (not (gnus-summary-last-article-p article)))
6173                        (gnus-summary-next-article)
6174                      (gnus-summary-next-unread-article))))))))
6175     (gnus-summary-recenter)
6176     (gnus-summary-position-point)))
6177
6178 (defun gnus-summary-prev-page (&optional lines move)
6179   "Show previous page of selected article.
6180 Argument LINES specifies lines to be scrolled down.
6181 If MOVE, move to the previous unread article if point is at
6182 the beginning of the buffer."
6183   (interactive "P")
6184   (let ((article (gnus-summary-article-number))
6185         (article-window (get-buffer-window gnus-article-buffer t))
6186         endp)
6187     (gnus-configure-windows 'article)
6188     (if (or (null gnus-current-article)
6189             (null gnus-article-current)
6190             (/= article (cdr gnus-article-current))
6191             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6192         ;; Selected subject is different from current article's.
6193         (gnus-summary-display-article article)
6194       (gnus-summary-recenter)
6195       (when article-window
6196         (gnus-eval-in-buffer-window gnus-article-buffer
6197           (setq endp (gnus-article-prev-page lines)))
6198         (when (and move endp)
6199           (cond (lines
6200                  (gnus-message 3 "Beginning of message"))
6201                 ((null lines)
6202                  (if (and (eq gnus-summary-goto-unread 'never)
6203                           (not (gnus-summary-first-article-p article)))
6204                      (gnus-summary-prev-article)
6205                    (gnus-summary-prev-unread-article))))))))
6206   (gnus-summary-position-point))
6207
6208 (defun gnus-summary-prev-page-or-article (&optional lines)
6209   "Show previous page of selected article.
6210 Argument LINES specifies lines to be scrolled down.
6211 If at the beginning of the article, go to the next article."
6212   (interactive "P")
6213   (gnus-summary-prev-page lines t))
6214
6215 (defun gnus-summary-scroll-up (lines)
6216   "Scroll up (or down) one line current article.
6217 Argument LINES specifies lines to be scrolled up (or down if negative)."
6218   (interactive "p")
6219   (gnus-configure-windows 'article)
6220   (gnus-summary-show-thread)
6221   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6222     (gnus-eval-in-buffer-window gnus-article-buffer
6223       (cond ((> lines 0)
6224              (when (gnus-article-next-page lines)
6225                (gnus-message 3 "End of message")))
6226             ((< lines 0)
6227              (gnus-article-prev-page (- lines))))))
6228   (gnus-summary-recenter)
6229   (gnus-summary-position-point))
6230
6231 (defun gnus-summary-scroll-down (lines)
6232   "Scroll down (or up) one line current article.
6233 Argument LINES specifies lines to be scrolled down (or up if negative)."
6234   (interactive "p")
6235   (gnus-summary-scroll-up (- lines)))
6236
6237 (defun gnus-summary-next-same-subject ()
6238   "Select next article which has the same subject as current one."
6239   (interactive)
6240   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6241
6242 (defun gnus-summary-prev-same-subject ()
6243   "Select previous article which has the same subject as current one."
6244   (interactive)
6245   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6246
6247 (defun gnus-summary-next-unread-same-subject ()
6248   "Select next unread article which has the same subject as current one."
6249   (interactive)
6250   (gnus-summary-next-article t (gnus-summary-article-subject)))
6251
6252 (defun gnus-summary-prev-unread-same-subject ()
6253   "Select previous unread article which has the same subject as current one."
6254   (interactive)
6255   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6256
6257 (defun gnus-summary-first-unread-article ()
6258   "Select the first unread article.
6259 Return nil if there are no unread articles."
6260   (interactive)
6261   (prog1
6262       (when (gnus-summary-first-subject t)
6263         (gnus-summary-show-thread)
6264         (gnus-summary-first-subject t)
6265         (gnus-summary-display-article (gnus-summary-article-number)))
6266     (gnus-summary-position-point)))
6267
6268 (defun gnus-summary-first-unread-subject ()
6269   "Place the point on the subject line of the first unread article.
6270 Return nil if there are no unread articles."
6271   (interactive)
6272   (prog1
6273       (when (gnus-summary-first-subject t)
6274         (gnus-summary-show-thread)
6275         (gnus-summary-first-subject t))
6276     (gnus-summary-position-point)))
6277
6278 (defun gnus-summary-first-article ()
6279   "Select the first article.
6280 Return nil if there are no articles."
6281   (interactive)
6282   (prog1
6283       (when (gnus-summary-first-subject)
6284         (gnus-summary-show-thread)
6285         (gnus-summary-first-subject)
6286         (gnus-summary-display-article (gnus-summary-article-number)))
6287     (gnus-summary-position-point)))
6288
6289 (defun gnus-summary-best-unread-article ()
6290   "Select the unread article with the highest score."
6291   (interactive)
6292   (let ((best -1000000)
6293         (data gnus-newsgroup-data)
6294         article score)
6295     (while data
6296       (and (gnus-data-unread-p (car data))
6297            (> (setq score
6298                     (gnus-summary-article-score (gnus-data-number (car data))))
6299               best)
6300            (setq best score
6301                  article (gnus-data-number (car data))))
6302       (setq data (cdr data)))
6303     (prog1
6304         (if article
6305             (gnus-summary-goto-article article)
6306           (error "No unread articles"))
6307       (gnus-summary-position-point))))
6308
6309 (defun gnus-summary-last-subject ()
6310   "Go to the last displayed subject line in the group."
6311   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6312     (when article
6313       (gnus-summary-goto-subject article))))
6314
6315 (defun gnus-summary-goto-article (article &optional all-headers force)
6316   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6317 If ALL-HEADERS is non-nil, no header lines are hidden.
6318 If FORCE, go to the article even if it isn't displayed.  If FORCE
6319 is a number, it is the line the article is to be displayed on."
6320   (interactive
6321    (list
6322     (completing-read
6323      "Article number or Message-ID: "
6324      (mapcar (lambda (number) (list (int-to-string number)))
6325              gnus-newsgroup-limit))
6326     current-prefix-arg
6327     t))
6328   (prog1
6329       (if (and (stringp article)
6330                (string-match "@" article))
6331           (gnus-summary-refer-article article)
6332         (when (stringp article)
6333           (setq article (string-to-number article)))
6334         (if (gnus-summary-goto-subject article force)
6335             (gnus-summary-display-article article all-headers)
6336           (gnus-message 4 "Couldn't go to article %s" article) nil))
6337     (gnus-summary-position-point)))
6338
6339 (defun gnus-summary-goto-last-article ()
6340   "Go to the previously read article."
6341   (interactive)
6342   (prog1
6343       (when gnus-last-article
6344         (gnus-summary-goto-article gnus-last-article nil t))
6345     (gnus-summary-position-point)))
6346
6347 (defun gnus-summary-pop-article (number)
6348   "Pop one article off the history and go to the previous.
6349 NUMBER articles will be popped off."
6350   (interactive "p")
6351   (let (to)
6352     (setq gnus-newsgroup-history
6353           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6354     (if to
6355         (gnus-summary-goto-article (car to) nil t)
6356       (error "Article history empty")))
6357   (gnus-summary-position-point))
6358
6359 ;; Summary commands and functions for limiting the summary buffer.
6360
6361 (defun gnus-summary-limit-to-articles (n)
6362   "Limit the summary buffer to the next N articles.
6363 If not given a prefix, use the process marked articles instead."
6364   (interactive "P")
6365   (prog1
6366       (let ((articles (gnus-summary-work-articles n)))
6367         (setq gnus-newsgroup-processable nil)
6368         (gnus-summary-limit articles))
6369     (gnus-summary-position-point)))
6370
6371 (defun gnus-summary-pop-limit (&optional total)
6372   "Restore the previous limit.
6373 If given a prefix, remove all limits."
6374   (interactive "P")
6375   (when total
6376     (setq gnus-newsgroup-limits
6377           (list (mapcar (lambda (h) (mail-header-number h))
6378                         gnus-newsgroup-headers))))
6379   (unless gnus-newsgroup-limits
6380     (error "No limit to pop"))
6381   (prog1
6382       (gnus-summary-limit nil 'pop)
6383     (gnus-summary-position-point)))
6384
6385 (defun gnus-summary-limit-to-subject (subject &optional header)
6386   "Limit the summary buffer to articles that have subjects that match a regexp."
6387   (interactive "sLimit to subject (regexp): ")
6388   (unless header
6389     (setq header "subject"))
6390   (when (not (equal "" subject))
6391     (prog1
6392         (let ((articles (gnus-summary-find-matching
6393                          (or header "subject") subject 'all)))
6394           (unless articles
6395             (error "Found no matches for \"%s\"" subject))
6396           (gnus-summary-limit articles))
6397       (gnus-summary-position-point))))
6398
6399 (defun gnus-summary-limit-to-author (from)
6400   "Limit the summary buffer to articles that have authors that match a regexp."
6401   (interactive "sLimit to author (regexp): ")
6402   (gnus-summary-limit-to-subject from "from"))
6403
6404 (defun gnus-summary-limit-to-age (age &optional younger-p)
6405   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6406 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6407 articles that are younger than AGE days."
6408   (interactive
6409    (let ((younger current-prefix-arg)
6410          (days-got nil)
6411          days)
6412      (while (not days-got)
6413        (setq days (if younger
6414                       (read-string "Limit to articles within (in days): ")
6415                     (read-string "Limit to articles old than (in days): ")))
6416        (when (> (length days) 0)
6417          (setq days (read days)))
6418        (if (numberp days)
6419            (setq days-got t)
6420          (message "Please enter a number.")
6421          (sleep-for 1)))
6422      (list days younger)))
6423   (prog1
6424       (let ((data gnus-newsgroup-data)
6425             (cutoff (days-to-time age))
6426             articles d date is-younger)
6427         (while (setq d (pop data))
6428           (when (and (vectorp (gnus-data-header d))
6429                      (setq date (mail-header-date (gnus-data-header d))))
6430             (setq is-younger (time-less-p
6431                               (time-since (condition-case ()
6432                                               (date-to-time date)
6433                                             (error '(0 0))))
6434                               cutoff))
6435             (when (if younger-p
6436                       is-younger
6437                     (not is-younger))
6438               (push (gnus-data-number d) articles))))
6439         (gnus-summary-limit (nreverse articles)))
6440     (gnus-summary-position-point)))
6441
6442 (defun gnus-summary-limit-to-extra (header regexp)
6443   "Limit the summary buffer to articles that match an 'extra' header."
6444   (interactive
6445    (let ((header
6446           (intern
6447            (gnus-completing-read
6448             (symbol-name (car gnus-extra-headers))
6449             "Limit extra header:"
6450             (mapcar (lambda (x)
6451                       (cons (symbol-name x) x))
6452                     gnus-extra-headers)
6453             nil
6454             t))))
6455      (list header
6456            (read-string (format "Limit to header %s (regexp): " header)))))
6457   (when (not (equal "" regexp))
6458     (prog1
6459         (let ((articles (gnus-summary-find-matching
6460                          (cons 'extra header) regexp 'all)))
6461           (unless articles
6462             (error "Found no matches for \"%s\"" regexp))
6463           (gnus-summary-limit articles))
6464       (gnus-summary-position-point))))
6465
6466 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6467 (make-obsolete
6468  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6469
6470 (defun gnus-summary-limit-to-unread (&optional all)
6471   "Limit the summary buffer to articles that are not marked as read.
6472 If ALL is non-nil, limit strictly to unread articles."
6473   (interactive "P")
6474   (if all
6475       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6476     (gnus-summary-limit-to-marks
6477      ;; Concat all the marks that say that an article is read and have
6478      ;; those removed.
6479      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6480            gnus-killed-mark gnus-kill-file-mark
6481            gnus-low-score-mark gnus-expirable-mark
6482            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6483            gnus-duplicate-mark gnus-souped-mark)
6484      'reverse)))
6485
6486 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6487 (make-obsolete 'gnus-summary-delete-marked-with
6488                'gnus-summary-limit-exlude-marks)
6489
6490 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6491   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6492 If REVERSE, limit the summary buffer to articles that are marked
6493 with MARKS.  MARKS can either be a string of marks or a list of marks.
6494 Returns how many articles were removed."
6495   (interactive "sMarks: ")
6496   (gnus-summary-limit-to-marks marks t))
6497
6498 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6499   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6500 If REVERSE (the prefix), limit the summary buffer to articles that are
6501 not marked with MARKS.  MARKS can either be a string of marks or a
6502 list of marks.
6503 Returns how many articles were removed."
6504   (interactive "sMarks: \nP")
6505   (prog1
6506       (let ((data gnus-newsgroup-data)
6507             (marks (if (listp marks) marks
6508                      (append marks nil))) ; Transform to list.
6509             articles)
6510         (while data
6511           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6512                   (memq (gnus-data-mark (car data)) marks))
6513             (push (gnus-data-number (car data)) articles))
6514           (setq data (cdr data)))
6515         (gnus-summary-limit articles))
6516     (gnus-summary-position-point)))
6517
6518 (defun gnus-summary-limit-to-score (&optional score)
6519   "Limit to articles with score at or above SCORE."
6520   (interactive "P")
6521   (setq score (if score
6522                   (prefix-numeric-value score)
6523                 (or gnus-summary-default-score 0)))
6524   (let ((data gnus-newsgroup-data)
6525         articles)
6526     (while data
6527       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6528                 score)
6529         (push (gnus-data-number (car data)) articles))
6530       (setq data (cdr data)))
6531     (prog1
6532         (gnus-summary-limit articles)
6533       (gnus-summary-position-point))))
6534
6535 (defun gnus-summary-limit-include-thread (id)
6536   "Display all the hidden articles that in the current thread."
6537   (interactive (list (mail-header-id (gnus-summary-article-header))))
6538   (let ((articles (gnus-articles-in-thread
6539                    (gnus-id-to-thread (gnus-root-id id)))))
6540     (prog1
6541         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6542       (gnus-summary-position-point))))
6543
6544 (defun gnus-summary-limit-include-dormant ()
6545   "Display all the hidden articles that are marked as dormant.
6546 Note that this command only works on a subset of the articles currently
6547 fetched for this group."
6548   (interactive)
6549   (unless gnus-newsgroup-dormant
6550     (error "There are no dormant articles in this group"))
6551   (prog1
6552       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6553     (gnus-summary-position-point)))
6554
6555 (defun gnus-summary-limit-exclude-dormant ()
6556   "Hide all dormant articles."
6557   (interactive)
6558   (prog1
6559       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6560     (gnus-summary-position-point)))
6561
6562 (defun gnus-summary-limit-exclude-childless-dormant ()
6563   "Hide all dormant articles that have no children."
6564   (interactive)
6565   (let ((data (gnus-data-list t))
6566         articles d children)
6567     ;; Find all articles that are either not dormant or have
6568     ;; children.
6569     (while (setq d (pop data))
6570       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6571                 (and (setq children
6572                            (gnus-article-children (gnus-data-number d)))
6573                      (let (found)
6574                        (while children
6575                          (when (memq (car children) articles)
6576                            (setq children nil
6577                                  found t))
6578                          (pop children))
6579                        found)))
6580         (push (gnus-data-number d) articles)))
6581     ;; Do the limiting.
6582     (prog1
6583         (gnus-summary-limit articles)
6584       (gnus-summary-position-point))))
6585
6586 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6587   "Mark all unread excluded articles as read.
6588 If ALL, mark even excluded ticked and dormants as read."
6589   (interactive "P")
6590   (let ((articles (gnus-sorted-complement
6591                    (sort
6592                     (mapcar (lambda (h) (mail-header-number h))
6593                             gnus-newsgroup-headers)
6594                     '<)
6595                    (sort gnus-newsgroup-limit '<)))
6596         article)
6597     (setq gnus-newsgroup-unreads
6598           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6599     (if all
6600         (setq gnus-newsgroup-dormant nil
6601               gnus-newsgroup-marked nil
6602               gnus-newsgroup-reads
6603               (nconc
6604                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6605                gnus-newsgroup-reads))
6606       (while (setq article (pop articles))
6607         (unless (or (memq article gnus-newsgroup-dormant)
6608                     (memq article gnus-newsgroup-marked))
6609           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6610
6611 (defun gnus-summary-limit (articles &optional pop)
6612   (if pop
6613       ;; We pop the previous limit off the stack and use that.
6614       (setq articles (car gnus-newsgroup-limits)
6615             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6616     ;; We use the new limit, so we push the old limit on the stack.
6617     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6618   ;; Set the limit.
6619   (setq gnus-newsgroup-limit articles)
6620   (let ((total (length gnus-newsgroup-data))
6621         (data (gnus-data-find-list (gnus-summary-article-number)))
6622         (gnus-summary-mark-below nil)   ; Inhibit this.
6623         found)
6624     ;; This will do all the work of generating the new summary buffer
6625     ;; according to the new limit.
6626     (gnus-summary-prepare)
6627     ;; Hide any threads, possibly.
6628     (and gnus-show-threads
6629          gnus-thread-hide-subtree
6630          (gnus-summary-hide-all-threads))
6631     ;; Try to return to the article you were at, or one in the
6632     ;; neighborhood.
6633     (when data
6634       ;; We try to find some article after the current one.
6635       (while data
6636         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6637           (setq data nil
6638                 found t))
6639         (setq data (cdr data))))
6640     (unless found
6641       ;; If there is no data, that means that we were after the last
6642       ;; article.  The same goes when we can't find any articles
6643       ;; after the current one.
6644       (goto-char (point-max))
6645       (gnus-summary-find-prev))
6646     (gnus-set-mode-line 'summary)
6647     ;; We return how many articles were removed from the summary
6648     ;; buffer as a result of the new limit.
6649     (- total (length gnus-newsgroup-data))))
6650
6651 (defsubst gnus-invisible-cut-children (threads)
6652   (let ((num 0))
6653     (while threads
6654       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6655         (incf num))
6656       (pop threads))
6657     (< num 2)))
6658
6659 (defsubst gnus-cut-thread (thread)
6660   "Go forwards in the thread until we find an article that we want to display."
6661   (when (or (eq gnus-fetch-old-headers 'some)
6662             (eq gnus-fetch-old-headers 'invisible)
6663             (numberp gnus-fetch-old-headers)
6664             (eq gnus-build-sparse-threads 'some)
6665             (eq gnus-build-sparse-threads 'more))
6666     ;; Deal with old-fetched headers and sparse threads.
6667     (while (and
6668             thread
6669             (or
6670              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6671              (gnus-summary-article-ancient-p
6672               (mail-header-number (car thread))))
6673             (if (or (<= (length (cdr thread)) 1)
6674                     (eq gnus-fetch-old-headers 'invisible))
6675                 (setq gnus-newsgroup-limit
6676                       (delq (mail-header-number (car thread))
6677                             gnus-newsgroup-limit)
6678                       thread (cadr thread))
6679               (when (gnus-invisible-cut-children (cdr thread))
6680                 (let ((th (cdr thread)))
6681                   (while th
6682                     (if (memq (mail-header-number (caar th))
6683                               gnus-newsgroup-limit)
6684                         (setq thread (car th)
6685                               th nil)
6686                       (setq th (cdr th))))))))))
6687   thread)
6688
6689 (defun gnus-cut-threads (threads)
6690   "Cut off all uninteresting articles from the beginning of threads."
6691   (when (or (eq gnus-fetch-old-headers 'some)
6692             (eq gnus-fetch-old-headers 'invisible)
6693             (numberp gnus-fetch-old-headers)
6694             (eq gnus-build-sparse-threads 'some)
6695             (eq gnus-build-sparse-threads 'more))
6696     (let ((th threads))
6697       (while th
6698         (setcar th (gnus-cut-thread (car th)))
6699         (setq th (cdr th)))))
6700   ;; Remove nixed out threads.
6701   (delq nil threads))
6702
6703 (defun gnus-summary-initial-limit (&optional show-if-empty)
6704   "Figure out what the initial limit is supposed to be on group entry.
6705 This entails weeding out unwanted dormants, low-scored articles,
6706 fetch-old-headers verbiage, and so on."
6707   ;; Most groups have nothing to remove.
6708   (if (or gnus-inhibit-limiting
6709           (and (null gnus-newsgroup-dormant)
6710                (not (eq gnus-fetch-old-headers 'some))
6711                (not (numberp gnus-fetch-old-headers))
6712                (not (eq gnus-fetch-old-headers 'invisible))
6713                (null gnus-summary-expunge-below)
6714                (not (eq gnus-build-sparse-threads 'some))
6715                (not (eq gnus-build-sparse-threads 'more))
6716                (null gnus-thread-expunge-below)
6717                (not gnus-use-nocem)))
6718       ()                                ; Do nothing.
6719     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6720     (setq gnus-newsgroup-limit nil)
6721     (mapatoms
6722      (lambda (node)
6723        (unless (car (symbol-value node))
6724          ;; These threads have no parents -- they are roots.
6725          (let ((nodes (cdr (symbol-value node)))
6726                thread)
6727            (while nodes
6728              (if (and gnus-thread-expunge-below
6729                       (< (gnus-thread-total-score (car nodes))
6730                          gnus-thread-expunge-below))
6731                  (gnus-expunge-thread (pop nodes))
6732                (setq thread (pop nodes))
6733                (gnus-summary-limit-children thread))))))
6734      gnus-newsgroup-dependencies)
6735     ;; If this limitation resulted in an empty group, we might
6736     ;; pop the previous limit and use it instead.
6737     (when (and (not gnus-newsgroup-limit)
6738                show-if-empty)
6739       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6740     gnus-newsgroup-limit))
6741
6742 (defun gnus-summary-limit-children (thread)
6743   "Return 1 if this subthread is visible and 0 if it is not."
6744   ;; First we get the number of visible children to this thread.  This
6745   ;; is done by recursing down the thread using this function, so this
6746   ;; will really go down to a leaf article first, before slowly
6747   ;; working its way up towards the root.
6748   (when thread
6749     (let ((children
6750            (if (cdr thread)
6751                (apply '+ (mapcar 'gnus-summary-limit-children
6752                                  (cdr thread)))
6753              0))
6754           (number (mail-header-number (car thread)))
6755           score)
6756       (if (and
6757            (not (memq number gnus-newsgroup-marked))
6758            (or
6759             ;; If this article is dormant and has absolutely no visible
6760             ;; children, then this article isn't visible.
6761             (and (memq number gnus-newsgroup-dormant)
6762                  (zerop children))
6763             ;; If this is "fetch-old-headered" and there is no
6764             ;; visible children, then we don't want this article.
6765             (and (or (eq gnus-fetch-old-headers 'some)
6766                      (numberp gnus-fetch-old-headers))
6767                  (gnus-summary-article-ancient-p number)
6768                  (zerop children))
6769             ;; If this is "fetch-old-headered" and `invisible', then
6770             ;; we don't want this article.
6771             (and (eq gnus-fetch-old-headers 'invisible)
6772                  (gnus-summary-article-ancient-p number))
6773             ;; If this is a sparsely inserted article with no children,
6774             ;; we don't want it.
6775             (and (eq gnus-build-sparse-threads 'some)
6776                  (gnus-summary-article-sparse-p number)
6777                  (zerop children))
6778             ;; If we use expunging, and this article is really
6779             ;; low-scored, then we don't want this article.
6780             (when (and gnus-summary-expunge-below
6781                        (< (setq score
6782                                 (or (cdr (assq number gnus-newsgroup-scored))
6783                                     gnus-summary-default-score))
6784                           gnus-summary-expunge-below))
6785               ;; We increase the expunge-tally here, but that has
6786               ;; nothing to do with the limits, really.
6787               (incf gnus-newsgroup-expunged-tally)
6788               ;; We also mark as read here, if that's wanted.
6789               (when (and gnus-summary-mark-below
6790                          (< score gnus-summary-mark-below))
6791                 (setq gnus-newsgroup-unreads
6792                       (delq number gnus-newsgroup-unreads))
6793                 (if gnus-newsgroup-auto-expire
6794                     (push number gnus-newsgroup-expirable)
6795                   (push (cons number gnus-low-score-mark)
6796                         gnus-newsgroup-reads)))
6797               t)
6798             ;; Check NoCeM things.
6799             (if (and gnus-use-nocem
6800                      (gnus-nocem-unwanted-article-p
6801                       (mail-header-id (car thread))))
6802                 (progn
6803                   (setq gnus-newsgroup-unreads
6804                         (delq number gnus-newsgroup-unreads))
6805                   t))))
6806           ;; Nope, invisible article.
6807           0
6808         ;; Ok, this article is to be visible, so we add it to the limit
6809         ;; and return 1.
6810         (push number gnus-newsgroup-limit)
6811         1))))
6812
6813 (defun gnus-expunge-thread (thread)
6814   "Mark all articles in THREAD as read."
6815   (let* ((number (mail-header-number (car thread))))
6816     (incf gnus-newsgroup-expunged-tally)
6817     ;; We also mark as read here, if that's wanted.
6818     (setq gnus-newsgroup-unreads
6819           (delq number gnus-newsgroup-unreads))
6820     (if gnus-newsgroup-auto-expire
6821         (push number gnus-newsgroup-expirable)
6822       (push (cons number gnus-low-score-mark)
6823             gnus-newsgroup-reads)))
6824   ;; Go recursively through all subthreads.
6825   (mapcar 'gnus-expunge-thread (cdr thread)))
6826
6827 ;; Summary article oriented commands
6828
6829 (defun gnus-summary-refer-parent-article (n)
6830   "Refer parent article N times.
6831 If N is negative, go to ancestor -N instead.
6832 The difference between N and the number of articles fetched is returned."
6833   (interactive "p")
6834   (let ((skip 1)
6835         error header ref)
6836     (when (not (natnump n))
6837       (setq skip (abs n)
6838             n 1))
6839     (while (and (> n 0)
6840                 (not error))
6841       (setq header (gnus-summary-article-header))
6842       (if (and (eq (mail-header-number header)
6843                    (cdr gnus-article-current))
6844                (equal gnus-newsgroup-name
6845                       (car gnus-article-current)))
6846           ;; If we try to find the parent of the currently
6847           ;; displayed article, then we take a look at the actual
6848           ;; References header, since this is slightly more
6849           ;; reliable than the References field we got from the
6850           ;; server.
6851           (save-excursion
6852             (set-buffer gnus-original-article-buffer)
6853             (nnheader-narrow-to-headers)
6854             (unless (setq ref (message-fetch-field "references"))
6855               (setq ref (message-fetch-field "in-reply-to")))
6856             (widen))
6857         (setq ref
6858               ;; It's not the current article, so we take a bet on
6859               ;; the value we got from the server.
6860               (mail-header-references header)))
6861       (if (and ref
6862                (not (equal ref "")))
6863           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6864             (gnus-message 1 "Couldn't find parent"))
6865         (gnus-message 1 "No references in article %d"
6866                       (gnus-summary-article-number))
6867         (setq error t))
6868       (decf n))
6869     (gnus-summary-position-point)
6870     n))
6871
6872 (defun gnus-summary-refer-references ()
6873   "Fetch all articles mentioned in the References header.
6874 Return the number of articles fetched."
6875   (interactive)
6876   (let ((ref (mail-header-references (gnus-summary-article-header)))
6877         (current (gnus-summary-article-number))
6878         (n 0))
6879     (if (or (not ref)
6880             (equal ref ""))
6881         (error "No References in the current article")
6882       ;; For each Message-ID in the References header...
6883       (while (string-match "<[^>]*>" ref)
6884         (incf n)
6885         ;; ... fetch that article.
6886         (gnus-summary-refer-article
6887          (prog1 (match-string 0 ref)
6888            (setq ref (substring ref (match-end 0))))))
6889       (gnus-summary-goto-subject current)
6890       (gnus-summary-position-point)
6891       n)))
6892
6893 (defun gnus-summary-refer-thread (&optional limit)
6894   "Fetch all articles in the current thread.
6895 If LIMIT (the numerical prefix), fetch that many old headers instead
6896 of what's specified by the `gnus-refer-thread-limit' variable."
6897   (interactive "P")
6898   (let ((id (mail-header-id (gnus-summary-article-header)))
6899         (limit (if limit (prefix-numeric-value limit)
6900                  gnus-refer-thread-limit)))
6901     ;; We want to fetch LIMIT *old* headers, but we also have to
6902     ;; re-fetch all the headers in the current buffer, because many of
6903     ;; them may be undisplayed.  So we adjust LIMIT.
6904     (when (numberp limit)
6905       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6906     (unless (eq gnus-fetch-old-headers 'invisible)
6907       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6908       ;; Retrieve the headers and read them in.
6909       (if (eq (gnus-retrieve-headers
6910                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6911               'nov)
6912           (gnus-build-all-threads)
6913         (error "Can't fetch thread from backends that don't support NOV"))
6914       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6915     (gnus-summary-limit-include-thread id)))
6916
6917 (defun gnus-summary-refer-article (message-id)
6918   "Fetch an article specified by MESSAGE-ID."
6919   (interactive "sMessage-ID: ")
6920   (when (and (stringp message-id)
6921              (not (zerop (length message-id))))
6922     ;; Construct the correct Message-ID if necessary.
6923     ;; Suggested by tale@pawl.rpi.edu.
6924     (unless (string-match "^<" message-id)
6925       (setq message-id (concat "<" message-id)))
6926     (unless (string-match ">$" message-id)
6927       (setq message-id (concat message-id ">")))
6928     (let* ((header (gnus-id-to-header message-id))
6929            (sparse (and header
6930                         (gnus-summary-article-sparse-p
6931                          (mail-header-number header))
6932                         (memq (mail-header-number header)
6933                               gnus-newsgroup-limit)))
6934            number)
6935       (cond
6936        ;; If the article is present in the buffer we just go to it.
6937        ((and header
6938              (or (not (gnus-summary-article-sparse-p
6939                        (mail-header-number header)))
6940                  sparse))
6941         (prog1
6942             (gnus-summary-goto-article
6943              (mail-header-number header) nil t)
6944           (when sparse
6945             (gnus-summary-update-article (mail-header-number header)))))
6946        (t
6947         ;; We fetch the article.
6948         (catch 'found
6949           (dolist (gnus-override-method (gnus-refer-article-methods))
6950             (gnus-check-server gnus-override-method)
6951             ;; Fetch the header, and display the article.
6952             (when (setq number (gnus-summary-insert-subject message-id))
6953               (gnus-summary-select-article nil nil nil number)
6954               (throw 'found t)))
6955           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6956
6957 (defun gnus-refer-article-methods ()
6958   "Return a list of referrable methods."
6959   (cond
6960    ;; No method, so we default to current and native.
6961    ((null gnus-refer-article-method)
6962     (list gnus-current-select-method gnus-select-method))
6963    ;; Current.
6964    ((eq 'current gnus-refer-article-method)
6965     (list gnus-current-select-method))
6966    ;; List of select methods.
6967    ((not (and (symbolp (car gnus-refer-article-method))
6968               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6969     (let (out)
6970       (dolist (method gnus-refer-article-method)
6971         (push (if (eq 'current method)
6972                   gnus-current-select-method
6973                 method)
6974               out))
6975       (nreverse out)))
6976    ;; One single select method.
6977    (t
6978     (list gnus-refer-article-method))))
6979
6980 (defun gnus-summary-edit-parameters ()
6981   "Edit the group parameters of the current group."
6982   (interactive)
6983   (gnus-group-edit-group gnus-newsgroup-name 'params))
6984
6985 (defun gnus-summary-customize-parameters ()
6986   "Customize the group parameters of the current group."
6987   (interactive)
6988   (gnus-group-customize gnus-newsgroup-name))
6989
6990 (defun gnus-summary-enter-digest-group (&optional force)
6991   "Enter an nndoc group based on the current article.
6992 If FORCE, force a digest interpretation.  If not, try
6993 to guess what the document format is."
6994   (interactive "P")
6995   (let ((conf gnus-current-window-configuration))
6996     (save-excursion
6997       (gnus-summary-select-article))
6998     (setq gnus-current-window-configuration conf)
6999     (let* ((name (format "%s-%d"
7000                          (gnus-group-prefixed-name
7001                           gnus-newsgroup-name (list 'nndoc ""))
7002                          (save-excursion
7003                            (set-buffer gnus-summary-buffer)
7004                            gnus-current-article)))
7005            (ogroup gnus-newsgroup-name)
7006            (params (append (gnus-info-params (gnus-get-info ogroup))
7007                            (list (cons 'to-group ogroup))
7008                            (list (cons 'save-article-group ogroup))))
7009            (case-fold-search t)
7010            (buf (current-buffer))
7011            dig to-address)
7012       (save-excursion
7013         (set-buffer gnus-original-article-buffer)
7014         ;; Have the digest group inherit the main mail address of
7015         ;; the parent article.
7016         (when (setq to-address (or (message-fetch-field "reply-to")
7017                                    (message-fetch-field "from")))
7018           (setq params (append 
7019                         (list (cons 'to-address 
7020                                     (funcall gnus-decode-encoded-word-function
7021                                              to-address))))))
7022         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7023         (insert-buffer-substring gnus-original-article-buffer)
7024         ;; Remove lines that may lead nndoc to misinterpret the
7025         ;; document type.
7026         (narrow-to-region
7027          (goto-char (point-min))
7028          (or (search-forward "\n\n" nil t) (point)))
7029         (goto-char (point-min))
7030         (delete-matching-lines "^Path:\\|^From ")
7031         (widen))
7032       (unwind-protect
7033           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7034                     (gnus-newsgroup-ephemeral-ignored-charsets
7035                      gnus-newsgroup-ignored-charsets))
7036                 (gnus-group-read-ephemeral-group
7037                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7038                               (nndoc-article-type
7039                                ,(if force 'mbox 'guess))) t))
7040               ;; Make all postings to this group go to the parent group.
7041               (nconc (gnus-info-params (gnus-get-info name))
7042                      params)
7043             ;; Couldn't select this doc group.
7044             (switch-to-buffer buf)
7045             (gnus-set-global-variables)
7046             (gnus-configure-windows 'summary)
7047             (gnus-message 3 "Article couldn't be entered?"))
7048         (kill-buffer dig)))))
7049
7050 (defun gnus-summary-read-document (n)
7051   "Open a new group based on the current article(s).
7052 This will allow you to read digests and other similar
7053 documents as newsgroups.
7054 Obeys the standard process/prefix convention."
7055   (interactive "P")
7056   (let* ((articles (gnus-summary-work-articles n))
7057          (ogroup gnus-newsgroup-name)
7058          (params (append (gnus-info-params (gnus-get-info ogroup))
7059                          (list (cons 'to-group ogroup))))
7060          article group egroup groups vgroup)
7061     (while (setq article (pop articles))
7062       (setq group (format "%s-%d" gnus-newsgroup-name article))
7063       (gnus-summary-remove-process-mark article)
7064       (when (gnus-summary-display-article article)
7065         (save-excursion
7066           (with-temp-buffer
7067             (insert-buffer-substring gnus-original-article-buffer)
7068             ;; Remove some headers that may lead nndoc to make
7069             ;; the wrong guess.
7070             (message-narrow-to-head)
7071             (goto-char (point-min))
7072             (delete-matching-lines "^\\(Path\\):\\|^From ")
7073             (widen)
7074             (if (setq egroup
7075                       (gnus-group-read-ephemeral-group
7076                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7077                                      (nndoc-article-type guess))
7078                        t nil t))
7079                 (progn
7080                   ;; Make all postings to this group go to the parent group.
7081                   (nconc (gnus-info-params (gnus-get-info egroup))
7082                          params)
7083                   (push egroup groups))
7084               ;; Couldn't select this doc group.
7085               (gnus-error 3 "Article couldn't be entered"))))))
7086     ;; Now we have selected all the documents.
7087     (cond
7088      ((not groups)
7089       (error "None of the articles could be interpreted as documents"))
7090      ((gnus-group-read-ephemeral-group
7091        (setq vgroup (format
7092                      "nnvirtual:%s-%s" gnus-newsgroup-name
7093                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7094        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7095        t
7096        (cons (current-buffer) 'summary)))
7097      (t
7098       (error "Couldn't select virtual nndoc group")))))
7099
7100 (defun gnus-summary-isearch-article (&optional regexp-p)
7101   "Do incremental search forward on the current article.
7102 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7103   (interactive "P")
7104   (let* ((gnus-inhibit-treatment t)
7105          (old (gnus-summary-select-article)))
7106     (gnus-configure-windows 'article)
7107     (gnus-eval-in-buffer-window gnus-article-buffer
7108       (save-restriction
7109         (widen)
7110         (when (eq 'old old)
7111           (gnus-article-show-all-headers))
7112         (goto-char (point-min))
7113         (isearch-forward regexp-p)))))
7114
7115 (defun gnus-summary-search-article-forward (regexp &optional backward)
7116   "Search for an article containing REGEXP forward.
7117 If BACKWARD, search backward instead."
7118   (interactive
7119    (list (read-string
7120           (format "Search article %s (regexp%s): "
7121                   (if current-prefix-arg "backward" "forward")
7122                   (if gnus-last-search-regexp
7123                       (concat ", default " gnus-last-search-regexp)
7124                     "")))
7125          current-prefix-arg))
7126   (if (string-equal regexp "")
7127       (setq regexp (or gnus-last-search-regexp ""))
7128     (setq gnus-last-search-regexp regexp)
7129     (setq gnus-article-before-search gnus-current-article))
7130   ;; Intentionally set gnus-last-article.
7131   (setq gnus-last-article gnus-article-before-search)
7132   (let ((gnus-last-article gnus-last-article))
7133     (if (gnus-summary-search-article regexp backward)
7134         (gnus-summary-show-thread)
7135       (error "Search failed: \"%s\"" regexp))))
7136
7137 (defun gnus-summary-search-article-backward (regexp)
7138   "Search for an article containing REGEXP backward."
7139   (interactive
7140    (list (read-string
7141           (format "Search article backward (regexp%s): "
7142                   (if gnus-last-search-regexp
7143                       (concat ", default " gnus-last-search-regexp)
7144                     "")))))
7145   (gnus-summary-search-article-forward regexp 'backward))
7146
7147 (eval-when-compile
7148   (defmacro gnus-summary-search-article-position-point (regexp backward)
7149     "Dehighlight the last matched text and goto the beginning position."
7150     (` (if (and gnus-summary-search-article-matched-data
7151                 (let ((text (caddr gnus-summary-search-article-matched-data))
7152                       (inhibit-read-only t)
7153                       buffer-read-only)
7154                   (delete-region
7155                    (goto-char (car gnus-summary-search-article-matched-data))
7156                    (cadr gnus-summary-search-article-matched-data))
7157                   (insert text)
7158                   (string-match (, regexp) text)))
7159            (if (, backward) (beginning-of-line) (end-of-line))
7160          (goto-char (if (, backward) (point-max) (point-min))))))
7161
7162   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7163     "Place point where X-Face image is displayed."
7164     (if (featurep 'xemacs)
7165         (` (let ((end (if (search-forward "\n\n" nil t)
7166                           (goto-char (1- (point)))
7167                         (point-min)))
7168                  extent)
7169              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7170              (unless (and (re-search-forward "^From:" end t)
7171                           (setq extent (extent-at (point)))
7172                           (extent-begin-glyph extent))
7173                (goto-char (, opoint)))))
7174       (` (let ((end (if (search-forward "\n\n" nil t)
7175                         (goto-char (1- (point)))
7176                       (point-min)))
7177                (start (or (search-backward "\n\n" nil t) (point-min))))
7178            (goto-char
7179             (or (text-property-any start end 'x-face-image t);; x-face-e21
7180                 (text-property-any start end 'x-face-mule-bitmap-image t)
7181                 (, opoint)))))))
7182
7183   (defmacro gnus-summary-search-article-highlight-matched-text
7184     (backward treated x-face)
7185     "Highlight matched text in the function `gnus-summary-search-article'."
7186     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7187              (end (set-marker (make-marker) (match-end 0)))
7188              (inhibit-read-only t)
7189              buffer-read-only)
7190          (unless treated
7191            (let ((,@
7192                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7193                     (mapcar
7194                      (lambda (item) (setq items (delq item items)))
7195                      '(gnus-treat-buttonize
7196                        gnus-treat-fill-article
7197                        gnus-treat-fill-long-lines
7198                        gnus-treat-emphasize
7199                        gnus-treat-highlight-headers
7200                        gnus-treat-highlight-citation
7201                        gnus-treat-highlight-signature
7202                        gnus-treat-overstrike
7203                        gnus-treat-display-xface
7204                        gnus-treat-buttonize-head
7205                        gnus-treat-decode-article-as-default-mime-charset))
7206                     (static-if (featurep 'xemacs)
7207                         items
7208                       (cons '(x-face-mule-delete-x-face-field
7209                               (quote never))
7210                             items))))
7211                  (gnus-treat-display-xface
7212                   (when (, x-face) gnus-treat-display-xface)))
7213              (gnus-article-prepare-mime-display)))
7214          (goto-char (if (, backward) start end))
7215          (when (, x-face)
7216            (gnus-summary-search-article-highlight-goto-x-face (point)))
7217          (setq gnus-summary-search-article-matched-data
7218                (list start end (buffer-substring start end)))
7219          (unless (eq start end);; matched text has been deleted. :-<
7220            (put-text-property start end 'face
7221                               (or (find-face 'isearch)
7222                                   'secondary-selection))))))
7223   )
7224
7225 (defun gnus-summary-search-article (regexp &optional backward)
7226   "Search for an article containing REGEXP.
7227 Optional argument BACKWARD means do search for backward.
7228 `gnus-select-article-hook' is not called during the search."
7229   ;; We have to require this here to make sure that the following
7230   ;; dynamic binding isn't shadowed by autoloading.
7231   (require 'gnus-async)
7232   (require 'gnus-art)
7233   (let ((gnus-select-article-hook nil)  ;Disable hook.
7234         (gnus-article-display-hook nil)
7235         (gnus-article-prepare-hook nil)
7236         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7237         (gnus-use-article-prefetch nil)
7238         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7239         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7240         (sum (current-buffer))
7241         (found nil)
7242         point treated)
7243     (gnus-save-hidden-threads
7244       (static-if (featurep 'xemacs)
7245           (let ((gnus-inhibit-treatment t))
7246             (setq treated (eq 'old (gnus-summary-select-article)))
7247             (when (and treated
7248                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7249                                  (window-live-p (get-buffer-window
7250                                                  gnus-article-buffer t)))))
7251               (gnus-summary-select-article nil t)
7252               (setq treated nil)))
7253         (let ((gnus-inhibit-treatment t)
7254               (x-face-mule-delete-x-face-field 'never))
7255           (setq treated (eq 'old (gnus-summary-select-article)))
7256           (when (and treated
7257                      (not
7258                       (and (gnus-buffer-live-p gnus-article-buffer)
7259                            (window-live-p (get-buffer-window
7260                                            gnus-article-buffer t))
7261                            (or (not (string-match "^\\^X-Face:" regexp))
7262                                (with-current-buffer gnus-article-buffer
7263                                  gnus-summary-search-article-matched-data)))))
7264             (gnus-summary-select-article nil t)
7265             (setq treated nil))))
7266       (set-buffer gnus-article-buffer)
7267       (widen)
7268       (if treated
7269           (progn
7270             (gnus-article-show-all-headers)
7271             (gnus-summary-search-article-position-point regexp backward))
7272         (goto-char (if backward (point-max) (point-min))))
7273       (while (not found)
7274         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7275         (if (if backward
7276                 (re-search-backward regexp nil t)
7277               (re-search-forward regexp nil t))
7278             ;; We found the regexp.
7279             (progn
7280               (gnus-summary-search-article-highlight-matched-text
7281                backward treated (string-match "^\\^X-Face:" regexp))
7282               (setq found 'found)
7283               (forward-line
7284                (/ (- 2 (window-height
7285                         (get-buffer-window gnus-article-buffer t)))
7286                   2))
7287               (set-window-start
7288                (get-buffer-window (current-buffer))
7289                (point))
7290               (set-buffer sum)
7291               (setq point (point)))
7292           ;; We didn't find it, so we go to the next article.
7293           (set-buffer sum)
7294           (setq found 'not)
7295           (while (eq found 'not)
7296             (if (not (if backward (gnus-summary-find-prev)
7297                        (gnus-summary-find-next)))
7298                 ;; No more articles.
7299                 (setq found t)
7300               ;; Select the next article and adjust point.
7301               (unless (gnus-summary-article-sparse-p
7302                        (gnus-summary-article-number))
7303                 (setq found nil)
7304                 (let ((gnus-inhibit-treatment t))
7305                   (gnus-summary-select-article))
7306                 (setq treated nil)
7307                 (set-buffer gnus-article-buffer)
7308                 (widen)
7309                 (goto-char (if backward (point-max) (point-min))))))))
7310       (gnus-message 7 ""))
7311     ;; Return whether we found the regexp.
7312     (when (eq found 'found)
7313       (goto-char point)
7314       (gnus-summary-show-thread)
7315       (gnus-summary-goto-subject gnus-current-article)
7316       (gnus-summary-position-point)
7317       t)))
7318
7319 (defun gnus-summary-find-matching (header regexp &optional backward unread
7320                                           not-case-fold)
7321   "Return a list of all articles that match REGEXP on HEADER.
7322 The search stars on the current article and goes forwards unless
7323 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7324 If UNREAD is non-nil, only unread articles will
7325 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7326 in the comparisons."
7327   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7328                 (gnus-data-find-list
7329                  (gnus-summary-article-number) (gnus-data-list backward))))
7330         (case-fold-search (not not-case-fold))
7331         articles d func)
7332     (if (consp header)
7333         (if (eq (car header) 'extra)
7334             (setq func
7335                   `(lambda (h)
7336                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7337                          "")))
7338           (error "%s is an invalid header" header))
7339       (unless (fboundp (intern (concat "mail-header-" header)))
7340         (error "%s is not a valid header" header))
7341       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7342     (while data
7343       (setq d (car data))
7344       (and (or (not unread)             ; We want all articles...
7345                (gnus-data-unread-p d))  ; Or just unreads.
7346            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7347            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7348            (push (gnus-data-number d) articles)) ; Success!
7349       (setq data (cdr data)))
7350     (nreverse articles)))
7351
7352 (defun gnus-summary-execute-command (header regexp command &optional backward)
7353   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7354 If HEADER is an empty string (or nil), the match is done on the entire
7355 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7356   (interactive
7357    (list (let ((completion-ignore-case t))
7358            (completing-read
7359             "Header name: "
7360             (mapcar (lambda (string) (list string))
7361                     '("Number" "Subject" "From" "Lines" "Date"
7362                       "Message-ID" "Xref" "References" "Body"))
7363             nil 'require-match))
7364          (read-string "Regexp: ")
7365          (read-key-sequence "Command: ")
7366          current-prefix-arg))
7367   (when (equal header "Body")
7368     (setq header ""))
7369   ;; Hidden thread subtrees must be searched as well.
7370   (gnus-summary-show-all-threads)
7371   ;; We don't want to change current point nor window configuration.
7372   (save-excursion
7373     (save-window-excursion
7374       (gnus-message 6 "Executing %s..." (key-description command))
7375       ;; We'd like to execute COMMAND interactively so as to give arguments.
7376       (gnus-execute header regexp
7377                     `(call-interactively ',(key-binding command))
7378                     backward)
7379       (gnus-message 6 "Executing %s...done" (key-description command)))))
7380
7381 (defun gnus-summary-beginning-of-article ()
7382   "Scroll the article back to the beginning."
7383   (interactive)
7384   (gnus-summary-select-article)
7385   (gnus-configure-windows 'article)
7386   (gnus-eval-in-buffer-window gnus-article-buffer
7387     (widen)
7388     (goto-char (point-min))
7389     (when gnus-page-broken
7390       (gnus-narrow-to-page))))
7391
7392 (defun gnus-summary-end-of-article ()
7393   "Scroll to the end of the article."
7394   (interactive)
7395   (gnus-summary-select-article)
7396   (gnus-configure-windows 'article)
7397   (gnus-eval-in-buffer-window gnus-article-buffer
7398     (widen)
7399     (goto-char (point-max))
7400     (recenter -3)
7401     (when gnus-page-broken
7402       (gnus-narrow-to-page))))
7403
7404 (defun gnus-summary-print-article (&optional filename n)
7405   "Generate and print a PostScript image of the N next (mail) articles.
7406
7407 If N is negative, print the N previous articles.  If N is nil and articles
7408 have been marked with the process mark, print these instead.
7409
7410 If the optional first argument FILENAME is nil, send the image to the
7411 printer.  If FILENAME is a string, save the PostScript image in a file with
7412 that name.  If FILENAME is a number, prompt the user for the name of the file
7413 to save in."
7414   (interactive (list (ps-print-preprint current-prefix-arg)
7415                      current-prefix-arg))
7416   (dolist (article (gnus-summary-work-articles n))
7417     (gnus-summary-select-article nil nil 'pseudo article)
7418     (gnus-eval-in-buffer-window gnus-article-buffer
7419       (let ((buffer (generate-new-buffer " *print*")))
7420         (unwind-protect
7421             (progn
7422               (copy-to-buffer buffer (point-min) (point-max))
7423               (set-buffer buffer)
7424               (gnus-article-delete-invisible-text)
7425               (let ((ps-left-header
7426                      (list
7427                       (concat "("
7428                               (mail-header-subject gnus-current-headers) ")")
7429                       (concat "("
7430                               (mail-header-from gnus-current-headers) ")")))
7431                     (ps-right-header
7432                      (list
7433                       "/pagenumberstring load"
7434                       (concat "("
7435                               (mail-header-date gnus-current-headers) ")"))))
7436                 (gnus-run-hooks 'gnus-ps-print-hook)
7437                 (save-excursion
7438                   (ps-print-buffer-with-faces filename))))
7439           (kill-buffer buffer))))))
7440
7441 (defun gnus-summary-show-article (&optional arg)
7442   "Force re-fetching of the current article.
7443 If ARG (the prefix) is a number, show the article with the charset
7444 defined in `gnus-summary-show-article-charset-alist', or the charset
7445 inputed.
7446 If ARG (the prefix) is non-nil and not a number, show the raw article
7447 without any article massaging functions being run."
7448   (interactive "P")
7449   (cond
7450    ((numberp arg)
7451     (let ((gnus-newsgroup-charset
7452            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7453                (read-coding-system "Charset: ")))
7454           (gnus-newsgroup-ignored-charsets 'gnus-all))
7455       (gnus-summary-select-article nil 'force)
7456       (let ((deps gnus-newsgroup-dependencies)
7457             head header)
7458         (save-excursion
7459           (set-buffer gnus-original-article-buffer)
7460           (save-restriction
7461             (message-narrow-to-head)
7462             (setq head (buffer-string)))
7463           (with-temp-buffer
7464             (insert (format "211 %d Article retrieved.\n"
7465                             (cdr gnus-article-current)))
7466             (insert head)
7467             (insert ".\n")
7468             (let ((nntp-server-buffer (current-buffer)))
7469               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7470         (gnus-data-set-header
7471          (gnus-data-find (cdr gnus-article-current))
7472          header)
7473         (gnus-summary-update-article-line
7474          (cdr gnus-article-current) header))))
7475    ((not arg)
7476     ;; Select the article the normal way.
7477     (gnus-summary-select-article nil 'force))
7478    (t
7479     ;; We have to require this here to make sure that the following
7480     ;; dynamic binding isn't shadowed by autoloading.
7481     (require 'gnus-async)
7482     (require 'gnus-art)
7483     ;; Bind the article treatment functions to nil.
7484     (let ((gnus-have-all-headers t)
7485           gnus-article-display-hook
7486           gnus-article-prepare-hook
7487           gnus-article-decode-hook
7488           gnus-break-pages
7489           gnus-show-mime)
7490       (gnus-summary-select-article nil 'force))))
7491   (gnus-summary-goto-subject gnus-current-article)
7492   (gnus-summary-position-point))
7493
7494 (defun gnus-summary-verbose-headers (&optional arg)
7495   "Toggle permanent full header display.
7496 If ARG is a positive number, turn header display on.
7497 If ARG is a negative number, turn header display off."
7498   (interactive "P")
7499   (setq gnus-show-all-headers
7500         (cond ((or (not (numberp arg))
7501                    (zerop arg))
7502                (not gnus-show-all-headers))
7503               ((natnump arg)
7504                t)))
7505   (gnus-summary-show-article))
7506
7507 (defun gnus-summary-toggle-header (&optional arg)
7508   "Show the headers if they are hidden, or hide them if they are shown.
7509 If ARG is a positive number, show the entire header.
7510 If ARG is a negative number, hide the unwanted header lines."
7511   (interactive "P")
7512   (save-excursion
7513     (set-buffer gnus-article-buffer)
7514     (save-restriction
7515       (let* ((buffer-read-only nil)
7516              (inhibit-point-motion-hooks t)
7517              hidden e)
7518         (setq hidden
7519               (if (numberp arg)
7520                   (>= arg 0)
7521                 (save-restriction
7522                   (article-narrow-to-head)
7523                   (gnus-article-hidden-text-p 'headers))))
7524         (goto-char (point-min))
7525         (when (search-forward "\n\n" nil t)
7526           (delete-region (point-min) (1- (point))))
7527         (goto-char (point-min))
7528         (save-excursion
7529           (set-buffer gnus-original-article-buffer)
7530           (goto-char (point-min))
7531           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7532         (insert-buffer-substring gnus-original-article-buffer 1 e)
7533         (save-restriction
7534           (narrow-to-region (point-min) (point))
7535           (article-decode-encoded-words)
7536           (if  hidden
7537               (let ((gnus-treat-hide-headers nil)
7538                     (gnus-treat-hide-boring-headers nil))
7539                 (setq gnus-article-wash-types
7540                       (delq 'headers gnus-article-wash-types))
7541                 (gnus-treat-article 'head))
7542             (gnus-treat-article 'head)))
7543         (gnus-set-mode-line 'article)))))
7544
7545 (defun gnus-summary-show-all-headers ()
7546   "Make all header lines visible."
7547   (interactive)
7548   (gnus-article-show-all-headers))
7549
7550 (defun gnus-summary-toggle-mime (&optional arg)
7551   "Toggle MIME processing.
7552 If ARG is a positive number, turn MIME processing on."
7553   (interactive "P")
7554   (setq gnus-show-mime
7555         (if (null arg)
7556             (not gnus-show-mime)
7557           (> (prefix-numeric-value arg) 0)))
7558   (gnus-summary-select-article t 'force))
7559
7560 (defun gnus-summary-caesar-message (&optional arg)
7561   "Caesar rotate the current article by 13.
7562 The numerical prefix specifies how many places to rotate each letter
7563 forward."
7564   (interactive "P")
7565   (gnus-summary-select-article)
7566   (let ((mail-header-separator ""))
7567     (gnus-eval-in-buffer-window gnus-article-buffer
7568       (save-restriction
7569         (widen)
7570         (let ((start (window-start))
7571               buffer-read-only)
7572           (message-caesar-buffer-body arg)
7573           (set-window-start (get-buffer-window (current-buffer)) start))))))
7574
7575 (defun gnus-summary-stop-page-breaking ()
7576   "Stop page breaking in the current article."
7577   (interactive)
7578   (gnus-summary-select-article)
7579   (gnus-eval-in-buffer-window gnus-article-buffer
7580     (widen)
7581     (when (gnus-visual-p 'page-marker)
7582       (let ((buffer-read-only nil))
7583         (gnus-remove-text-with-property 'gnus-prev)
7584         (gnus-remove-text-with-property 'gnus-next))
7585       (setq gnus-page-broken nil))))
7586
7587 (defun gnus-summary-move-article (&optional n to-newsgroup
7588                                             select-method action)
7589   "Move the current article to a different newsgroup.
7590 If N is a positive number, move the N next articles.
7591 If N is a negative number, move the N previous articles.
7592 If N is nil and any articles have been marked with the process mark,
7593 move those articles instead.
7594 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7595 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7596 re-spool using this method.
7597
7598 For this function to work, both the current newsgroup and the
7599 newsgroup that you want to move to have to support the `request-move'
7600 and `request-accept' functions.
7601
7602 ACTION can be either `move' (the default), `crosspost' or `copy'."
7603   (interactive "P")
7604   (unless action
7605     (setq action 'move))
7606   ;; Disable marking as read.
7607   (let (gnus-mark-article-hook)
7608     (save-window-excursion
7609       (gnus-summary-select-article)))
7610   ;; Check whether the source group supports the required functions.
7611   (cond ((and (eq action 'move)
7612               (not (gnus-check-backend-function
7613                     'request-move-article gnus-newsgroup-name)))
7614          (error "The current group does not support article moving"))
7615         ((and (eq action 'crosspost)
7616               (not (gnus-check-backend-function
7617                     'request-replace-article gnus-newsgroup-name)))
7618          (error "The current group does not support article editing")))
7619   (let ((articles (gnus-summary-work-articles n))
7620         (prefix (if (gnus-check-backend-function
7621                     'request-move-article gnus-newsgroup-name)
7622                     (gnus-group-real-prefix gnus-newsgroup-name)
7623                   ""))
7624         (names '((move "Move" "Moving")
7625                  (copy "Copy" "Copying")
7626                  (crosspost "Crosspost" "Crossposting")))
7627         (copy-buf (save-excursion
7628                     (nnheader-set-temp-buffer " *copy article*")))
7629         (default-marks gnus-article-mark-lists)
7630         (no-expire-marks (delete '(expirable . expire)
7631                                  (copy-sequence gnus-article-mark-lists)))
7632         art-group to-method new-xref article to-groups)
7633     (unless (assq action names)
7634       (error "Unknown action %s" action))
7635     ;; Read the newsgroup name.
7636     (when (and (not to-newsgroup)
7637                (not select-method))
7638       (setq to-newsgroup
7639             (gnus-read-move-group-name
7640              (cadr (assq action names))
7641              (symbol-value (intern (format "gnus-current-%s-group" action)))
7642              articles prefix))
7643       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7644     (setq to-method (or select-method
7645                         (gnus-server-to-method
7646                          (gnus-group-method to-newsgroup))))
7647     ;; Check the method we are to move this article to...
7648     (unless (gnus-check-backend-function
7649              'request-accept-article (car to-method))
7650       (error "%s does not support article copying" (car to-method)))
7651     (unless (gnus-check-server to-method)
7652       (error "Can't open server %s" (car to-method)))
7653     (gnus-message 6 "%s to %s: %s..."
7654                   (caddr (assq action names))
7655                   (or (car select-method) to-newsgroup) articles)
7656     (while articles
7657       (setq article (pop articles))
7658       (setq
7659        art-group
7660        (cond
7661         ;; Move the article.
7662         ((eq action 'move)
7663          ;; Remove this article from future suppression.
7664          (gnus-dup-unsuppress-article article)
7665          (gnus-request-move-article
7666           article                       ; Article to move
7667           gnus-newsgroup-name           ; From newsgroup
7668           (nth 1 (gnus-find-method-for-group
7669                   gnus-newsgroup-name)) ; Server
7670           (list 'gnus-request-accept-article
7671                 to-newsgroup (list 'quote select-method)
7672                 (not articles) t)       ; Accept form
7673           (not articles)))              ; Only save nov last time
7674         ;; Copy the article.
7675         ((eq action 'copy)
7676          (save-excursion
7677            (set-buffer copy-buf)
7678            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7679              (gnus-request-accept-article
7680               to-newsgroup select-method (not articles) t))))
7681         ;; Crosspost the article.
7682         ((eq action 'crosspost)
7683          (let ((xref (message-tokenize-header
7684                       (mail-header-xref (gnus-summary-article-header article))
7685                       " ")))
7686            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7687                                   ":" article))
7688            (unless xref
7689              (setq xref (list (system-name))))
7690            (setq new-xref
7691                  (concat
7692                   (mapconcat 'identity
7693                              (delete "Xref:" (delete new-xref xref))
7694                              " ")
7695                   " " new-xref))
7696            (save-excursion
7697              (set-buffer copy-buf)
7698              ;; First put the article in the destination group.
7699              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7700              (when (consp (setq art-group
7701                                 (gnus-request-accept-article
7702                                  to-newsgroup select-method (not articles))))
7703                (setq new-xref (concat new-xref " " (car art-group)
7704                                       ":" (cdr art-group)))
7705                ;; Now we have the new Xrefs header, so we insert
7706                ;; it and replace the new article.
7707                (nnheader-replace-header "Xref" new-xref)
7708                (gnus-request-replace-article
7709                 (cdr art-group) to-newsgroup (current-buffer))
7710                art-group))))))
7711       (cond
7712        ((not art-group)
7713         (gnus-message 1 "Couldn't %s article %s: %s"
7714                       (cadr (assq action names)) article
7715                       (nnheader-get-report (car to-method))))
7716        ((eq art-group 'junk)
7717         (when (eq action 'move)
7718           (gnus-summary-mark-article article gnus-canceled-mark)
7719           (gnus-message 4 "Deleted article %s" article)))
7720        (t
7721         (let* ((pto-group (gnus-group-prefixed-name
7722                            (car art-group) to-method))
7723                (entry
7724                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7725                (info (nth 2 entry))
7726                (to-group (gnus-info-group info))
7727                to-marks)
7728           ;; Update the group that has been moved to.
7729           (when (and info
7730                      (memq action '(move copy)))
7731             (unless (member to-group to-groups)
7732               (push to-group to-groups))
7733
7734             (unless (memq article gnus-newsgroup-unreads)
7735               (push 'read to-marks)
7736               (gnus-info-set-read
7737                info (gnus-add-to-range (gnus-info-read info)
7738                                        (list (cdr art-group)))))
7739
7740             ;; See whether the article is to be put in the cache.
7741             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7742                              default-marks
7743                            no-expire-marks))
7744                   (to-article (cdr art-group)))
7745
7746               ;; Enter the article into the cache in the new group,
7747               ;; if that is required.
7748               (when gnus-use-cache
7749                 (gnus-cache-possibly-enter-article
7750                  to-group to-article
7751                  (let ((header (copy-sequence
7752                                 (gnus-summary-article-header article))))
7753                    (mail-header-set-number header to-article)
7754                    header)
7755                  (memq article gnus-newsgroup-marked)
7756                  (memq article gnus-newsgroup-dormant)
7757                  (memq article gnus-newsgroup-unreads)))
7758
7759               (when gnus-preserve-marks
7760                 ;; Copy any marks over to the new group.
7761                 (when (and (equal to-group gnus-newsgroup-name)
7762                            (not (memq article gnus-newsgroup-unreads)))
7763                   ;; Mark this article as read in this group.
7764                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7765                   (setcdr (gnus-active to-group) to-article)
7766                   (setcdr gnus-newsgroup-active to-article))
7767
7768                 (while marks
7769                   (when (memq article (symbol-value
7770                                        (intern (format "gnus-newsgroup-%s"
7771                                                        (caar marks)))))
7772                     (push (cdar marks) to-marks)
7773                     ;; If the other group is the same as this group,
7774                     ;; then we have to add the mark to the list.
7775                     (when (equal to-group gnus-newsgroup-name)
7776                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7777                            (cons to-article
7778                                  (symbol-value
7779                                   (intern (format "gnus-newsgroup-%s"
7780                                                   (caar marks)))))))
7781                     ;; Copy the marks to other group.
7782                     (gnus-add-marked-articles
7783                      to-group (cdar marks) (list to-article) info))
7784                   (setq marks (cdr marks)))
7785
7786                 (gnus-request-set-mark to-group (list (list (list to-article)
7787                                                             'set
7788                                                             to-marks))))
7789
7790               (gnus-dribble-enter
7791                (concat "(gnus-group-set-info '"
7792                        (gnus-prin1-to-string (gnus-get-info to-group))
7793                        ")"))))
7794
7795           ;; Update the Xref header in this article to point to
7796           ;; the new crossposted article we have just created.
7797           (when (eq action 'crosspost)
7798             (save-excursion
7799               (set-buffer copy-buf)
7800               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7801               (nnheader-replace-header "Xref" new-xref)
7802               (gnus-request-replace-article
7803                article gnus-newsgroup-name (current-buffer)))))
7804
7805         ;;;!!!Why is this necessary?
7806         (set-buffer gnus-summary-buffer)
7807
7808         (gnus-summary-goto-subject article)
7809         (when (eq action 'move)
7810           (gnus-summary-mark-article article gnus-canceled-mark))))
7811       (gnus-summary-remove-process-mark article))
7812     ;; Re-activate all groups that have been moved to.
7813     (while to-groups
7814       (save-excursion
7815         (set-buffer gnus-group-buffer)
7816         (when (gnus-group-goto-group (car to-groups) t)
7817           (gnus-group-get-new-news-this-group 1 t))
7818         (pop to-groups)))
7819
7820     (gnus-kill-buffer copy-buf)
7821     (gnus-summary-position-point)
7822     (gnus-set-mode-line 'summary)))
7823
7824 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7825   "Move the current article to a different newsgroup.
7826 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7827 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7828 re-spool using this method."
7829   (interactive "P")
7830   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7831
7832 (defun gnus-summary-crosspost-article (&optional n)
7833   "Crosspost the current article to some other group."
7834   (interactive "P")
7835   (gnus-summary-move-article n nil nil 'crosspost))
7836
7837 (defcustom gnus-summary-respool-default-method nil
7838   "Default method for respooling an article.
7839 If nil, use to the current newsgroup method."
7840   :type '(choice (gnus-select-method :value (nnml ""))
7841                  (const nil))
7842   :group 'gnus-summary-mail)
7843
7844 (defun gnus-summary-respool-article (&optional n method)
7845   "Respool the current article.
7846 The article will be squeezed through the mail spooling process again,
7847 which means that it will be put in some mail newsgroup or other
7848 depending on `nnmail-split-methods'.
7849 If N is a positive number, respool the N next articles.
7850 If N is a negative number, respool the N previous articles.
7851 If N is nil and any articles have been marked with the process mark,
7852 respool those articles instead.
7853
7854 Respooling can be done both from mail groups and \"real\" newsgroups.
7855 In the former case, the articles in question will be moved from the
7856 current group into whatever groups they are destined to.  In the
7857 latter case, they will be copied into the relevant groups."
7858   (interactive
7859    (list current-prefix-arg
7860          (let* ((methods (gnus-methods-using 'respool))
7861                 (methname
7862                  (symbol-name (or gnus-summary-respool-default-method
7863                                   (car (gnus-find-method-for-group
7864                                         gnus-newsgroup-name)))))
7865                 (method
7866                  (gnus-completing-read
7867                   methname "What backend do you want to use when respooling?"
7868                   methods nil t nil 'gnus-mail-method-history))
7869                 ms)
7870            (cond
7871             ((zerop (length (setq ms (gnus-servers-using-backend
7872                                       (intern method)))))
7873              (list (intern method) ""))
7874             ((= 1 (length ms))
7875              (car ms))
7876             (t
7877              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7878                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7879                            ms-alist))))))))
7880   (unless method
7881     (error "No method given for respooling"))
7882   (if (assoc (symbol-name
7883               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7884              (gnus-methods-using 'respool))
7885       (gnus-summary-move-article n nil method)
7886     (gnus-summary-copy-article n nil method)))
7887
7888 (defun gnus-summary-import-article (file)
7889   "Import an arbitrary file into a mail newsgroup."
7890   (interactive "fImport file: ")
7891   (let ((group gnus-newsgroup-name)
7892         (now (current-time))
7893         atts lines)
7894     (unless (gnus-check-backend-function 'request-accept-article group)
7895       (error "%s does not support article importing" group))
7896     (or (file-readable-p file)
7897         (not (file-regular-p file))
7898         (error "Can't read %s" file))
7899     (save-excursion
7900       (set-buffer (gnus-get-buffer-create " *import file*"))
7901       (erase-buffer)
7902       (nnheader-insert-file-contents file)
7903       (goto-char (point-min))
7904       (unless (nnheader-article-p)
7905         ;; This doesn't look like an article, so we fudge some headers.
7906         (setq atts (file-attributes file)
7907               lines (count-lines (point-min) (point-max)))
7908         (insert "From: " (read-string "From: ") "\n"
7909                 "Subject: " (read-string "Subject: ") "\n"
7910                 "Date: " (message-make-date (nth 5 atts))
7911                 "\n"
7912                 "Message-ID: " (message-make-message-id) "\n"
7913                 "Lines: " (int-to-string lines) "\n"
7914                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7915       (gnus-request-accept-article group nil t)
7916       (kill-buffer (current-buffer)))))
7917
7918 (defun gnus-summary-article-posted-p ()
7919   "Say whether the current (mail) article is available from news as well.
7920 This will be the case if the article has both been mailed and posted."
7921   (interactive)
7922   (let ((id (mail-header-references (gnus-summary-article-header)))
7923         (gnus-override-method (car (gnus-refer-article-methods))))
7924     (if (gnus-request-head id "")
7925         (gnus-message 2 "The current message was found on %s"
7926                       gnus-override-method)
7927       (gnus-message 2 "The current message couldn't be found on %s"
7928                     gnus-override-method)
7929       nil)))
7930
7931 (defun gnus-summary-expire-articles (&optional now)
7932   "Expire all articles that are marked as expirable in the current group."
7933   (interactive)
7934   (when (gnus-check-backend-function
7935          'request-expire-articles gnus-newsgroup-name)
7936     ;; This backend supports expiry.
7937     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7938            (expirable (if total
7939                           (progn
7940                             ;; We need to update the info for
7941                             ;; this group for `gnus-list-of-read-articles'
7942                             ;; to give us the right answer.
7943                             (gnus-run-hooks 'gnus-exit-group-hook)
7944                             (gnus-summary-update-info)
7945                             (gnus-list-of-read-articles gnus-newsgroup-name))
7946                         (setq gnus-newsgroup-expirable
7947                               (sort gnus-newsgroup-expirable '<))))
7948            (expiry-wait (if now 'immediate
7949                           (gnus-group-find-parameter
7950                            gnus-newsgroup-name 'expiry-wait)))
7951            (nnmail-expiry-target
7952             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7953                 nnmail-expiry-target))
7954            es)
7955       (when expirable
7956         ;; There are expirable articles in this group, so we run them
7957         ;; through the expiry process.
7958         (gnus-message 6 "Expiring articles...")
7959         (unless (gnus-check-group gnus-newsgroup-name)
7960           (error "Can't open server for %s" gnus-newsgroup-name))
7961         ;; The list of articles that weren't expired is returned.
7962         (save-excursion
7963           (if expiry-wait
7964               (let ((nnmail-expiry-wait-function nil)
7965                     (nnmail-expiry-wait expiry-wait))
7966                 (setq es (gnus-request-expire-articles
7967                           expirable gnus-newsgroup-name)))
7968             (setq es (gnus-request-expire-articles
7969                       expirable gnus-newsgroup-name)))
7970           (unless total
7971             (setq gnus-newsgroup-expirable es))
7972           ;; We go through the old list of expirable, and mark all
7973           ;; really expired articles as nonexistent.
7974           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7975             (let ((gnus-use-cache nil))
7976               (while expirable
7977                 (unless (memq (car expirable) es)
7978                   (when (gnus-data-find (car expirable))
7979                     (gnus-summary-mark-article
7980                      (car expirable) gnus-canceled-mark)))
7981                 (setq expirable (cdr expirable))))))
7982         (gnus-message 6 "Expiring articles...done")))))
7983
7984 (defun gnus-summary-expire-articles-now ()
7985   "Expunge all expirable articles in the current group.
7986 This means that *all* articles that are marked as expirable will be
7987 deleted forever, right now."
7988   (interactive)
7989   (or gnus-expert-user
7990       (gnus-yes-or-no-p
7991        "Are you really, really, really sure you want to delete all these messages? ")
7992       (error "Phew!"))
7993   (gnus-summary-expire-articles t))
7994
7995 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7996 (defun gnus-summary-delete-article (&optional n)
7997   "Delete the N next (mail) articles.
7998 This command actually deletes articles.  This is not a marking
7999 command.  The article will disappear forever from your life, never to
8000 return.
8001 If N is negative, delete backwards.
8002 If N is nil and articles have been marked with the process mark,
8003 delete these instead."
8004   (interactive "P")
8005   (unless (gnus-check-backend-function 'request-expire-articles
8006                                        gnus-newsgroup-name)
8007     (error "The current newsgroup does not support article deletion"))
8008   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8009     (error "Couldn't open server"))
8010   ;; Compute the list of articles to delete.
8011   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8012         not-deleted)
8013     (if (and gnus-novice-user
8014              (not (gnus-yes-or-no-p
8015                    (format "Do you really want to delete %s forever? "
8016                            (if (> (length articles) 1)
8017                                (format "these %s articles" (length articles))
8018                              "this article")))))
8019         ()
8020       ;; Delete the articles.
8021       (setq not-deleted (gnus-request-expire-articles
8022                          articles gnus-newsgroup-name 'force))
8023       (while articles
8024         (gnus-summary-remove-process-mark (car articles))
8025         ;; The backend might not have been able to delete the article
8026         ;; after all.
8027         (unless (memq (car articles) not-deleted)
8028           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8029         (setq articles (cdr articles)))
8030       (when not-deleted
8031         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8032     (gnus-summary-position-point)
8033     (gnus-set-mode-line 'summary)
8034     not-deleted))
8035
8036 (defun gnus-summary-edit-article (&optional force)
8037   "Edit the current article.
8038 This will have permanent effect only in mail groups.
8039 If FORCE is non-nil, allow editing of articles even in read-only
8040 groups."
8041   (interactive "P")
8042   (save-excursion
8043     (set-buffer gnus-summary-buffer)
8044     (let ((mail-parse-charset gnus-newsgroup-charset)
8045           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8046       (gnus-set-global-variables)
8047       (when (and (not force)
8048                  (gnus-group-read-only-p))
8049         (error "The current newsgroup does not support article editing"))
8050       (gnus-summary-show-article t)
8051       (gnus-article-edit-article
8052        'ignore
8053        `(lambda (no-highlight)
8054           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8055                 (message-options message-options)
8056                 (message-options-set-recipient)
8057                 (mail-parse-ignored-charsets
8058                  ',gnus-newsgroup-ignored-charsets))
8059             (gnus-summary-edit-article-done
8060              ,(or (mail-header-references gnus-current-headers) "")
8061              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8062
8063 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8064
8065 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8066                                                  no-highlight)
8067   "Make edits to the current article permanent."
8068   (interactive)
8069   (save-excursion
8070     ;; The buffer restriction contains the entire article if it exists.
8071     (when (article-goto-body)
8072       (let ((lines (count-lines (point) (point-max)))
8073             (length (- (point-max) (point)))
8074             (case-fold-search t)
8075             (body (copy-marker (point))))
8076         (goto-char (point-min))
8077         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8078           (delete-region (match-beginning 1) (match-end 1))
8079           (insert (number-to-string length)))
8080         (goto-char (point-min))
8081         (when (re-search-forward
8082                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8083           (delete-region (match-beginning 1) (match-end 1))
8084           (insert (number-to-string length)))
8085         (goto-char (point-min))
8086         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8087           (delete-region (match-beginning 1) (match-end 1))
8088           (insert (number-to-string lines))))))
8089   ;; Replace the article.
8090   (let ((buf (current-buffer)))
8091     (with-temp-buffer
8092       (insert-buffer-substring buf)
8093       
8094       (if (and (not read-only)
8095                (not (gnus-request-replace-article
8096                      (cdr gnus-article-current) (car gnus-article-current)
8097                      (current-buffer) t)))
8098           (error "Couldn't replace article")
8099         ;; Update the summary buffer.
8100         (if (and references
8101                  (equal (message-tokenize-header references " ")
8102                         (message-tokenize-header
8103                          (or (message-fetch-field "references") "") " ")))
8104             ;; We only have to update this line.
8105             (save-excursion
8106               (save-restriction
8107                 (message-narrow-to-head)
8108                 (let ((head (buffer-string))
8109                       header)
8110                   (with-temp-buffer
8111                     (insert (format "211 %d Article retrieved.\n"
8112                                     (cdr gnus-article-current)))
8113                     (insert head)
8114                     (insert ".\n")
8115                     (let ((nntp-server-buffer (current-buffer)))
8116                       (setq header (car (gnus-get-newsgroup-headers
8117                                          (save-excursion
8118                                            (set-buffer gnus-summary-buffer)
8119                                            gnus-newsgroup-dependencies)
8120                                          t))))
8121                     (save-excursion
8122                       (set-buffer gnus-summary-buffer)
8123                       (gnus-data-set-header
8124                        (gnus-data-find (cdr gnus-article-current))
8125                        header)
8126                       (gnus-summary-update-article-line
8127                        (cdr gnus-article-current) header))))))
8128           ;; Update threads.
8129           (set-buffer (or buffer gnus-summary-buffer))
8130           (gnus-summary-update-article (cdr gnus-article-current)))
8131         ;; Prettify the article buffer again.
8132         (unless no-highlight
8133           (save-excursion
8134             (set-buffer gnus-article-buffer)
8135             ;;;!!! Fix this -- article should be rehighlighted.
8136             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8137             (set-buffer gnus-original-article-buffer)
8138             (gnus-request-article
8139              (cdr gnus-article-current)
8140              (car gnus-article-current) (current-buffer))))
8141         ;; Prettify the summary buffer line.
8142         (when (gnus-visual-p 'summary-highlight 'highlight)
8143           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8144
8145 (defun gnus-summary-edit-wash (key)
8146   "Perform editing command KEY in the article buffer."
8147   (interactive
8148    (list
8149     (progn
8150       (message "%s" (concat (this-command-keys) "- "))
8151       (read-char))))
8152   (message "")
8153   (gnus-summary-edit-article)
8154   (execute-kbd-macro (concat (this-command-keys) key))
8155   (gnus-article-edit-done))
8156
8157 ;;; Respooling
8158
8159 (defun gnus-summary-respool-query (&optional silent trace)
8160   "Query where the respool algorithm would put this article."
8161   (interactive)
8162   (let (gnus-mark-article-hook)
8163     (gnus-summary-select-article)
8164     (save-excursion
8165       (set-buffer gnus-original-article-buffer)
8166       (save-restriction
8167         (message-narrow-to-head)
8168         (let ((groups (nnmail-article-group 'identity trace)))
8169           (unless silent
8170             (if groups
8171                 (message "This message would go to %s"
8172                          (mapconcat 'car groups ", "))
8173               (message "This message would go to no groups"))
8174             groups))))))
8175
8176 (defun gnus-summary-respool-trace ()
8177   "Trace where the respool algorithm would put this article.
8178 Display a buffer showing all fancy splitting patterns which matched."
8179   (interactive)
8180   (gnus-summary-respool-query nil t))
8181
8182 ;; Summary marking commands.
8183
8184 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8185   "Mark articles which has the same subject as read, and then select the next.
8186 If UNMARK is positive, remove any kind of mark.
8187 If UNMARK is negative, tick articles."
8188   (interactive "P")
8189   (when unmark
8190     (setq unmark (prefix-numeric-value unmark)))
8191   (let ((count
8192          (gnus-summary-mark-same-subject
8193           (gnus-summary-article-subject) unmark)))
8194     ;; Select next unread article.  If auto-select-same mode, should
8195     ;; select the first unread article.
8196     (gnus-summary-next-article t (and gnus-auto-select-same
8197                                       (gnus-summary-article-subject)))
8198     (gnus-message 7 "%d article%s marked as %s"
8199                   count (if (= count 1) " is" "s are")
8200                   (if unmark "unread" "read"))))
8201
8202 (defun gnus-summary-kill-same-subject (&optional unmark)
8203   "Mark articles which has the same subject as read.
8204 If UNMARK is positive, remove any kind of mark.
8205 If UNMARK is negative, tick articles."
8206   (interactive "P")
8207   (when unmark
8208     (setq unmark (prefix-numeric-value unmark)))
8209   (let ((count
8210          (gnus-summary-mark-same-subject
8211           (gnus-summary-article-subject) unmark)))
8212     ;; If marked as read, go to next unread subject.
8213     (when (null unmark)
8214       ;; Go to next unread subject.
8215       (gnus-summary-next-subject 1 t))
8216     (gnus-message 7 "%d articles are marked as %s"
8217                   count (if unmark "unread" "read"))))
8218
8219 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8220   "Mark articles with same SUBJECT as read, and return marked number.
8221 If optional argument UNMARK is positive, remove any kinds of marks.
8222 If optional argument UNMARK is negative, mark articles as unread instead."
8223   (let ((count 1))
8224     (save-excursion
8225       (cond
8226        ((null unmark)                   ; Mark as read.
8227         (while (and
8228                 (progn
8229                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8230                   (gnus-summary-show-thread) t)
8231                 (gnus-summary-find-subject subject))
8232           (setq count (1+ count))))
8233        ((> unmark 0)                    ; Tick.
8234         (while (and
8235                 (progn
8236                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8237                   (gnus-summary-show-thread) t)
8238                 (gnus-summary-find-subject subject))
8239           (setq count (1+ count))))
8240        (t                               ; Mark as unread.
8241         (while (and
8242                 (progn
8243                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8244                   (gnus-summary-show-thread) t)
8245                 (gnus-summary-find-subject subject))
8246           (setq count (1+ count)))))
8247       (gnus-set-mode-line 'summary)
8248       ;; Return the number of marked articles.
8249       count)))
8250
8251 (defun gnus-summary-mark-as-processable (n &optional unmark)
8252   "Set the process mark on the next N articles.
8253 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8254 the process mark instead.  The difference between N and the actual
8255 number of articles marked is returned."
8256   (interactive "p")
8257   (let ((backward (< n 0))
8258         (n (abs n)))
8259     (while (and
8260             (> n 0)
8261             (if unmark
8262                 (gnus-summary-remove-process-mark
8263                  (gnus-summary-article-number))
8264               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8265             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8266       (setq n (1- n)))
8267     (when (/= 0 n)
8268       (gnus-message 7 "No more articles"))
8269     (gnus-summary-recenter)
8270     (gnus-summary-position-point)
8271     n))
8272
8273 (defun gnus-summary-unmark-as-processable (n)
8274   "Remove the process mark from the next N articles.
8275 If N is negative, unmark backward instead.  The difference between N and
8276 the actual number of articles unmarked is returned."
8277   (interactive "p")
8278   (gnus-summary-mark-as-processable n t))
8279
8280 (defun gnus-summary-unmark-all-processable ()
8281   "Remove the process mark from all articles."
8282   (interactive)
8283   (save-excursion
8284     (while gnus-newsgroup-processable
8285       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8286   (gnus-summary-position-point))
8287
8288 (defun gnus-summary-mark-as-expirable (n)
8289   "Mark N articles forward as expirable.
8290 If N is negative, mark backward instead.  The difference between N and
8291 the actual number of articles marked is returned."
8292   (interactive "p")
8293   (gnus-summary-mark-forward n gnus-expirable-mark))
8294
8295 (defun gnus-summary-mark-article-as-replied (article)
8296   "Mark ARTICLE replied and update the summary line."
8297   (push article gnus-newsgroup-replied)
8298   (let ((buffer-read-only nil))
8299     (when (gnus-summary-goto-subject article nil t)
8300       (gnus-summary-update-secondary-mark article))))
8301
8302 (defun gnus-summary-set-bookmark (article)
8303   "Set a bookmark in current article."
8304   (interactive (list (gnus-summary-article-number)))
8305   (when (or (not (get-buffer gnus-article-buffer))
8306             (not gnus-current-article)
8307             (not gnus-article-current)
8308             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8309     (error "No current article selected"))
8310   ;; Remove old bookmark, if one exists.
8311   (let ((old (assq article gnus-newsgroup-bookmarks)))
8312     (when old
8313       (setq gnus-newsgroup-bookmarks
8314             (delq old gnus-newsgroup-bookmarks))))
8315   ;; Set the new bookmark, which is on the form
8316   ;; (article-number . line-number-in-body).
8317   (push
8318    (cons article
8319          (save-excursion
8320            (set-buffer gnus-article-buffer)
8321            (count-lines
8322             (min (point)
8323                  (save-excursion
8324                    (goto-char (point-min))
8325                    (search-forward "\n\n" nil t)
8326                    (point)))
8327             (point))))
8328    gnus-newsgroup-bookmarks)
8329   (gnus-message 6 "A bookmark has been added to the current article."))
8330
8331 (defun gnus-summary-remove-bookmark (article)
8332   "Remove the bookmark from the current article."
8333   (interactive (list (gnus-summary-article-number)))
8334   ;; Remove old bookmark, if one exists.
8335   (let ((old (assq article gnus-newsgroup-bookmarks)))
8336     (if old
8337         (progn
8338           (setq gnus-newsgroup-bookmarks
8339                 (delq old gnus-newsgroup-bookmarks))
8340           (gnus-message 6 "Removed bookmark."))
8341       (gnus-message 6 "No bookmark in current article."))))
8342
8343 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8344 (defun gnus-summary-mark-as-dormant (n)
8345   "Mark N articles forward as dormant.
8346 If N is negative, mark backward instead.  The difference between N and
8347 the actual number of articles marked is returned."
8348   (interactive "p")
8349   (gnus-summary-mark-forward n gnus-dormant-mark))
8350
8351 (defun gnus-summary-set-process-mark (article)
8352   "Set the process mark on ARTICLE and update the summary line."
8353   (setq gnus-newsgroup-processable
8354         (cons article
8355               (delq article gnus-newsgroup-processable)))
8356   (when (gnus-summary-goto-subject article)
8357     (gnus-summary-show-thread)
8358     (gnus-summary-goto-subject article)
8359     (gnus-summary-update-secondary-mark article)))
8360
8361 (defun gnus-summary-remove-process-mark (article)
8362   "Remove the process mark from ARTICLE and update the summary line."
8363   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8364   (when (gnus-summary-goto-subject article)
8365     (gnus-summary-show-thread)
8366     (gnus-summary-goto-subject article)
8367     (gnus-summary-update-secondary-mark article)))
8368
8369 (defun gnus-summary-set-saved-mark (article)
8370   "Set the process mark on ARTICLE and update the summary line."
8371   (push article gnus-newsgroup-saved)
8372   (when (gnus-summary-goto-subject article)
8373     (gnus-summary-update-secondary-mark article)))
8374
8375 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8376   "Mark N articles as read forwards.
8377 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8378 The difference between N and the actual number of articles marked is
8379 returned.
8380 Iff NO-EXPIRE, auto-expiry will be inhibited."
8381   (interactive "p")
8382   (gnus-summary-show-thread)
8383   (let ((backward (< n 0))
8384         (gnus-summary-goto-unread
8385          (and gnus-summary-goto-unread
8386               (not (eq gnus-summary-goto-unread 'never))
8387               (not (memq mark (list gnus-unread-mark
8388                                     gnus-ticked-mark gnus-dormant-mark)))))
8389         (n (abs n))
8390         (mark (or mark gnus-del-mark)))
8391     (while (and (> n 0)
8392                 (gnus-summary-mark-article nil mark no-expire)
8393                 (zerop (gnus-summary-next-subject
8394                         (if backward -1 1)
8395                         (and gnus-summary-goto-unread
8396                              (not (eq gnus-summary-goto-unread 'never)))
8397                         t)))
8398       (setq n (1- n)))
8399     (when (/= 0 n)
8400       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8401     (gnus-summary-recenter)
8402     (gnus-summary-position-point)
8403     (gnus-set-mode-line 'summary)
8404     n))
8405
8406 (defun gnus-summary-mark-article-as-read (mark)
8407   "Mark the current article quickly as read with MARK."
8408   (let ((article (gnus-summary-article-number)))
8409     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8410     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8411     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8412     (push (cons article mark) gnus-newsgroup-reads)
8413     ;; Possibly remove from cache, if that is used.
8414     (when gnus-use-cache
8415       (gnus-cache-enter-remove-article article))
8416     ;; Allow the backend to change the mark.
8417     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8418     ;; Check for auto-expiry.
8419     (when (and gnus-newsgroup-auto-expire
8420                (memq mark gnus-auto-expirable-marks))
8421       (setq mark gnus-expirable-mark)
8422       ;; Let the backend know about the mark change.
8423       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8424       (push article gnus-newsgroup-expirable))
8425     ;; Set the mark in the buffer.
8426     (gnus-summary-update-mark mark 'unread)
8427     t))
8428
8429 (defun gnus-summary-mark-article-as-unread (mark)
8430   "Mark the current article quickly as unread with MARK."
8431   (let* ((article (gnus-summary-article-number))
8432          (old-mark (gnus-summary-article-mark article)))
8433     ;; Allow the backend to change the mark.
8434     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8435     (if (eq mark old-mark)
8436         t
8437       (if (<= article 0)
8438           (progn
8439             (gnus-error 1 "Can't mark negative article numbers")
8440             nil)
8441         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8442         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8443         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8444         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8445         (cond ((= mark gnus-ticked-mark)
8446                (push article gnus-newsgroup-marked))
8447               ((= mark gnus-dormant-mark)
8448                (push article gnus-newsgroup-dormant))
8449               (t
8450                (push article gnus-newsgroup-unreads)))
8451         (gnus-pull article gnus-newsgroup-reads)
8452
8453         ;; See whether the article is to be put in the cache.
8454         (and gnus-use-cache
8455              (vectorp (gnus-summary-article-header article))
8456              (save-excursion
8457                (gnus-cache-possibly-enter-article
8458                 gnus-newsgroup-name article
8459                 (gnus-summary-article-header article)
8460                 (= mark gnus-ticked-mark)
8461                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8462
8463         ;; Fix the mark.
8464         (gnus-summary-update-mark mark 'unread)
8465         t))))
8466
8467 (defun gnus-summary-mark-article (&optional article mark no-expire)
8468   "Mark ARTICLE with MARK.  MARK can be any character.
8469 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8470 `??' (dormant) and `?E' (expirable).
8471 If MARK is nil, then the default character `?r' is used.
8472 If ARTICLE is nil, then the article on the current line will be
8473 marked.
8474 Iff NO-EXPIRE, auto-expiry will be inhibited."
8475   ;; The mark might be a string.
8476   (when (stringp mark)
8477     (setq mark (aref mark 0)))
8478   ;; If no mark is given, then we check auto-expiring.
8479   (when (null mark)
8480     (setq mark gnus-del-mark))
8481   (when (and (not no-expire)
8482              gnus-newsgroup-auto-expire
8483              (memq mark gnus-auto-expirable-marks))
8484     (setq mark gnus-expirable-mark))
8485   (let ((article (or article (gnus-summary-article-number)))
8486         (old-mark (gnus-summary-article-mark article)))
8487     ;; Allow the backend to change the mark.
8488     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8489     (if (eq mark old-mark)
8490         t
8491       (unless article
8492         (error "No article on current line"))
8493       (if (not (if (or (= mark gnus-unread-mark)
8494                        (= mark gnus-ticked-mark)
8495                        (= mark gnus-dormant-mark))
8496                    (gnus-mark-article-as-unread article mark)
8497                  (gnus-mark-article-as-read article mark)))
8498           t
8499         ;; See whether the article is to be put in the cache.
8500         (and gnus-use-cache
8501              (not (= mark gnus-canceled-mark))
8502              (vectorp (gnus-summary-article-header article))
8503              (save-excursion
8504                (gnus-cache-possibly-enter-article
8505                 gnus-newsgroup-name article
8506                 (gnus-summary-article-header article)
8507                 (= mark gnus-ticked-mark)
8508                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8509
8510         (when (gnus-summary-goto-subject article nil t)
8511           (let ((buffer-read-only nil))
8512             (gnus-summary-show-thread)
8513             ;; Fix the mark.
8514             (gnus-summary-update-mark mark 'unread)
8515             t))))))
8516
8517 (defun gnus-summary-update-secondary-mark (article)
8518   "Update the secondary (read, process, cache) mark."
8519   (gnus-summary-update-mark
8520    (cond ((memq article gnus-newsgroup-processable)
8521           gnus-process-mark)
8522          ((memq article gnus-newsgroup-cached)
8523           gnus-cached-mark)
8524          ((memq article gnus-newsgroup-replied)
8525           gnus-replied-mark)
8526          ((memq article gnus-newsgroup-saved)
8527           gnus-saved-mark)
8528          (t gnus-unread-mark))
8529    'replied)
8530   (when (gnus-visual-p 'summary-highlight 'highlight)
8531     (gnus-run-hooks 'gnus-summary-update-hook))
8532   t)
8533
8534 (defun gnus-summary-update-mark (mark type)
8535   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8536         (buffer-read-only nil))
8537     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8538     (when forward
8539       (when (looking-at "\r")
8540         (incf forward))
8541       (when (<= (+ forward (point)) (point-max))
8542         ;; Go to the right position on the line.
8543         (goto-char (+ forward (point)))
8544         ;; Replace the old mark with the new mark.
8545         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8546         ;; Optionally update the marks by some user rule.
8547         (when (eq type 'unread)
8548           (gnus-data-set-mark
8549            (gnus-data-find (gnus-summary-article-number)) mark)
8550           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8551
8552 (defun gnus-mark-article-as-read (article &optional mark)
8553   "Enter ARTICLE in the pertinent lists and remove it from others."
8554   ;; Make the article expirable.
8555   (let ((mark (or mark gnus-del-mark)))
8556     (if (= mark gnus-expirable-mark)
8557         (push article gnus-newsgroup-expirable)
8558       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8559     ;; Remove from unread and marked lists.
8560     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8561     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8562     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8563     (push (cons article mark) gnus-newsgroup-reads)
8564     ;; Possibly remove from cache, if that is used.
8565     (when gnus-use-cache
8566       (gnus-cache-enter-remove-article article))
8567     t))
8568
8569 (defun gnus-mark-article-as-unread (article &optional mark)
8570   "Enter ARTICLE in the pertinent lists and remove it from others."
8571   (let ((mark (or mark gnus-ticked-mark)))
8572     (if (<= article 0)
8573         (progn
8574           (gnus-error 1 "Can't mark negative article numbers")
8575           nil)
8576       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8577             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8578             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8579             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8580
8581       ;; Unsuppress duplicates?
8582       (when gnus-suppress-duplicates
8583         (gnus-dup-unsuppress-article article))
8584
8585       (cond ((= mark gnus-ticked-mark)
8586              (push article gnus-newsgroup-marked))
8587             ((= mark gnus-dormant-mark)
8588              (push article gnus-newsgroup-dormant))
8589             (t
8590              (push article gnus-newsgroup-unreads)))
8591       (gnus-pull article gnus-newsgroup-reads)
8592       t)))
8593
8594 (defalias 'gnus-summary-mark-as-unread-forward
8595   'gnus-summary-tick-article-forward)
8596 (make-obsolete 'gnus-summary-mark-as-unread-forward
8597                'gnus-summary-tick-article-forward)
8598 (defun gnus-summary-tick-article-forward (n)
8599   "Tick N articles forwards.
8600 If N is negative, tick backwards instead.
8601 The difference between N and the number of articles ticked is returned."
8602   (interactive "p")
8603   (gnus-summary-mark-forward n gnus-ticked-mark))
8604
8605 (defalias 'gnus-summary-mark-as-unread-backward
8606   'gnus-summary-tick-article-backward)
8607 (make-obsolete 'gnus-summary-mark-as-unread-backward
8608                'gnus-summary-tick-article-backward)
8609 (defun gnus-summary-tick-article-backward (n)
8610   "Tick N articles backwards.
8611 The difference between N and the number of articles ticked is returned."
8612   (interactive "p")
8613   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8614
8615 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8616 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8617 (defun gnus-summary-tick-article (&optional article clear-mark)
8618   "Mark current article as unread.
8619 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8620 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8621   (interactive)
8622   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8623                                        gnus-ticked-mark)))
8624
8625 (defun gnus-summary-mark-as-read-forward (n)
8626   "Mark N articles as read forwards.
8627 If N is negative, mark backwards instead.
8628 The difference between N and the actual number of articles marked is
8629 returned."
8630   (interactive "p")
8631   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8632
8633 (defun gnus-summary-mark-as-read-backward (n)
8634   "Mark the N articles as read backwards.
8635 The difference between N and the actual number of articles marked is
8636 returned."
8637   (interactive "p")
8638   (gnus-summary-mark-forward
8639    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8640
8641 (defun gnus-summary-mark-as-read (&optional article mark)
8642   "Mark current article as read.
8643 ARTICLE specifies the article to be marked as read.
8644 MARK specifies a string to be inserted at the beginning of the line."
8645   (gnus-summary-mark-article article mark))
8646
8647 (defun gnus-summary-clear-mark-forward (n)
8648   "Clear marks from N articles forward.
8649 If N is negative, clear backward instead.
8650 The difference between N and the number of marks cleared is returned."
8651   (interactive "p")
8652   (gnus-summary-mark-forward n gnus-unread-mark))
8653
8654 (defun gnus-summary-clear-mark-backward (n)
8655   "Clear marks from N articles backward.
8656 The difference between N and the number of marks cleared is returned."
8657   (interactive "p")
8658   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8659
8660 (defun gnus-summary-mark-unread-as-read ()
8661   "Intended to be used by `gnus-summary-mark-article-hook'."
8662   (when (memq gnus-current-article gnus-newsgroup-unreads)
8663     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8664
8665 (defun gnus-summary-mark-read-and-unread-as-read ()
8666   "Intended to be used by `gnus-summary-mark-article-hook'."
8667   (let ((mark (gnus-summary-article-mark)))
8668     (when (or (gnus-unread-mark-p mark)
8669               (gnus-read-mark-p mark))
8670       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8671
8672 (defun gnus-summary-mark-region-as-read (point mark all)
8673   "Mark all unread articles between point and mark as read.
8674 If given a prefix, mark all articles between point and mark as read,
8675 even ticked and dormant ones."
8676   (interactive "r\nP")
8677   (save-excursion
8678     (let (article)
8679       (goto-char point)
8680       (beginning-of-line)
8681       (while (and
8682               (< (point) mark)
8683               (progn
8684                 (when (or all
8685                           (memq (setq article (gnus-summary-article-number))
8686                                 gnus-newsgroup-unreads))
8687                   (gnus-summary-mark-article article gnus-del-mark))
8688                 t)
8689               (gnus-summary-find-next))))))
8690
8691 (defun gnus-summary-mark-below (score mark)
8692   "Mark articles with score less than SCORE with MARK."
8693   (interactive "P\ncMark: ")
8694   (setq score (if score
8695                   (prefix-numeric-value score)
8696                 (or gnus-summary-default-score 0)))
8697   (save-excursion
8698     (set-buffer gnus-summary-buffer)
8699     (goto-char (point-min))
8700     (while
8701         (progn
8702           (and (< (gnus-summary-article-score) score)
8703                (gnus-summary-mark-article nil mark))
8704           (gnus-summary-find-next)))))
8705
8706 (defun gnus-summary-kill-below (&optional score)
8707   "Mark articles with score below SCORE as read."
8708   (interactive "P")
8709   (gnus-summary-mark-below score gnus-killed-mark))
8710
8711 (defun gnus-summary-clear-above (&optional score)
8712   "Clear all marks from articles with score above SCORE."
8713   (interactive "P")
8714   (gnus-summary-mark-above score gnus-unread-mark))
8715
8716 (defun gnus-summary-tick-above (&optional score)
8717   "Tick all articles with score above SCORE."
8718   (interactive "P")
8719   (gnus-summary-mark-above score gnus-ticked-mark))
8720
8721 (defun gnus-summary-mark-above (score mark)
8722   "Mark articles with score over SCORE with MARK."
8723   (interactive "P\ncMark: ")
8724   (setq score (if score
8725                   (prefix-numeric-value score)
8726                 (or gnus-summary-default-score 0)))
8727   (save-excursion
8728     (set-buffer gnus-summary-buffer)
8729     (goto-char (point-min))
8730     (while (and (progn
8731                   (when (> (gnus-summary-article-score) score)
8732                     (gnus-summary-mark-article nil mark))
8733                   t)
8734                 (gnus-summary-find-next)))))
8735
8736 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8737 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8738 (defun gnus-summary-limit-include-expunged (&optional no-error)
8739   "Display all the hidden articles that were expunged for low scores."
8740   (interactive)
8741   (let ((buffer-read-only nil))
8742     (let ((scored gnus-newsgroup-scored)
8743           headers h)
8744       (while scored
8745         (unless (gnus-summary-goto-subject (caar scored))
8746           (and (setq h (gnus-summary-article-header (caar scored)))
8747                (< (cdar scored) gnus-summary-expunge-below)
8748                (push h headers)))
8749         (setq scored (cdr scored)))
8750       (if (not headers)
8751           (when (not no-error)
8752             (error "No expunged articles hidden"))
8753         (goto-char (point-min))
8754         (gnus-summary-prepare-unthreaded (nreverse headers))
8755         (goto-char (point-min))
8756         (gnus-summary-position-point)
8757         t))))
8758
8759 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8760   "Mark all unread articles in this newsgroup as read.
8761 If prefix argument ALL is non-nil, ticked and dormant articles will
8762 also be marked as read.
8763 If QUIETLY is non-nil, no questions will be asked.
8764 If TO-HERE is non-nil, it should be a point in the buffer.  All
8765 articles before this point will be marked as read.
8766 Note that this function will only catch up the unread article
8767 in the current summary buffer limitation.
8768 The number of articles marked as read is returned."
8769   (interactive "P")
8770   (prog1
8771       (save-excursion
8772         (when (or quietly
8773                   (not gnus-interactive-catchup) ;Without confirmation?
8774                   gnus-expert-user
8775                   (gnus-y-or-n-p
8776                    (if all
8777                        "Mark absolutely all articles as read? "
8778                      "Mark all unread articles as read? ")))
8779           (if (and not-mark
8780                    (not gnus-newsgroup-adaptive)
8781                    (not gnus-newsgroup-auto-expire)
8782                    (not gnus-suppress-duplicates)
8783                    (or (not gnus-use-cache)
8784                        (eq gnus-use-cache 'passive)))
8785               (progn
8786                 (when all
8787                   (setq gnus-newsgroup-marked nil
8788                         gnus-newsgroup-dormant nil))
8789                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8790             ;; We actually mark all articles as canceled, which we
8791             ;; have to do when using auto-expiry or adaptive scoring.
8792             (gnus-summary-show-all-threads)
8793             (when (gnus-summary-first-subject (not all) t)
8794               (while (and
8795                       (if to-here (< (point) to-here) t)
8796                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8797                       (gnus-summary-find-next (not all) nil nil t))))
8798             (gnus-set-mode-line 'summary))
8799           t))
8800     (gnus-summary-position-point)))
8801
8802 (defun gnus-summary-catchup-to-here (&optional all)
8803   "Mark all unticked articles before the current one as read.
8804 If ALL is non-nil, also mark ticked and dormant articles as read."
8805   (interactive "P")
8806   (save-excursion
8807     (gnus-save-hidden-threads
8808       (let ((beg (point)))
8809         ;; We check that there are unread articles.
8810         (when (or all (gnus-summary-find-prev))
8811           (gnus-summary-catchup all t beg)))))
8812   (gnus-summary-position-point))
8813
8814 (defun gnus-summary-catchup-all (&optional quietly)
8815   "Mark all articles in this newsgroup as read."
8816   (interactive "P")
8817   (gnus-summary-catchup t quietly))
8818
8819 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8820   "Mark all unread articles in this group as read, then exit.
8821 If prefix argument ALL is non-nil, all articles are marked as read."
8822   (interactive "P")
8823   (when (gnus-summary-catchup all quietly nil 'fast)
8824     ;; Select next newsgroup or exit.
8825     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8826              (eq gnus-auto-select-next 'quietly))
8827         (gnus-summary-next-group nil)
8828       (gnus-summary-exit))))
8829
8830 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8831   "Mark all articles in this newsgroup as read, and then exit."
8832   (interactive "P")
8833   (gnus-summary-catchup-and-exit t quietly))
8834
8835 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8836   "Mark all articles in this group as read and select the next group.
8837 If given a prefix, mark all articles, unread as well as ticked, as
8838 read."
8839   (interactive "P")
8840   (save-excursion
8841     (gnus-summary-catchup all))
8842   (gnus-summary-next-group))
8843
8844 ;;;
8845 ;;; with article
8846 ;;;
8847
8848 (defmacro gnus-with-article (article &rest forms)
8849   "Select ARTICLE and perform FORMS in the original article buffer.
8850 Then replace the article with the result."
8851   `(progn
8852      ;; We don't want the article to be marked as read.
8853      (let (gnus-mark-article-hook)
8854        (gnus-summary-select-article t t nil ,article))
8855      (set-buffer gnus-original-article-buffer)
8856      ,@forms
8857      (if (not (gnus-check-backend-function
8858                'request-replace-article (car gnus-article-current)))
8859          (gnus-message 5 "Read-only group; not replacing")
8860        (unless (gnus-request-replace-article
8861                 ,article (car gnus-article-current)
8862                 (current-buffer) t)
8863          (error "Couldn't replace article")))
8864      ;; The cache and backlog have to be flushed somewhat.
8865      (when gnus-keep-backlog
8866        (gnus-backlog-remove-article
8867         (car gnus-article-current) (cdr gnus-article-current)))
8868      (when gnus-use-cache
8869        (gnus-cache-update-article
8870         (car gnus-article-current) (cdr gnus-article-current)))))
8871
8872 (put 'gnus-with-article 'lisp-indent-function 1)
8873 (put 'gnus-with-article 'edebug-form-spec '(form body))
8874
8875 ;; Thread-based commands.
8876
8877 (defun gnus-summary-articles-in-thread (&optional article)
8878   "Return a list of all articles in the current thread.
8879 If ARTICLE is non-nil, return all articles in the thread that starts
8880 with that article."
8881   (let* ((article (or article (gnus-summary-article-number)))
8882          (data (gnus-data-find-list article))
8883          (top-level (gnus-data-level (car data)))
8884          (top-subject
8885           (cond ((null gnus-thread-operation-ignore-subject)
8886                  (gnus-simplify-subject-re
8887                   (mail-header-subject (gnus-data-header (car data)))))
8888                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8889                  (gnus-simplify-subject-fuzzy
8890                   (mail-header-subject (gnus-data-header (car data)))))
8891                 (t nil)))
8892          (end-point (save-excursion
8893                       (if (gnus-summary-go-to-next-thread)
8894                           (point) (point-max))))
8895          articles)
8896     (while (and data
8897                 (< (gnus-data-pos (car data)) end-point))
8898       (when (or (not top-subject)
8899                 (string= top-subject
8900                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8901                              (gnus-simplify-subject-fuzzy
8902                               (mail-header-subject
8903                                (gnus-data-header (car data))))
8904                            (gnus-simplify-subject-re
8905                             (mail-header-subject
8906                              (gnus-data-header (car data)))))))
8907         (push (gnus-data-number (car data)) articles))
8908       (unless (and (setq data (cdr data))
8909                    (> (gnus-data-level (car data)) top-level))
8910         (setq data nil)))
8911     ;; Return the list of articles.
8912     (nreverse articles)))
8913
8914 (defun gnus-summary-rethread-current ()
8915   "Rethread the thread the current article is part of."
8916   (interactive)
8917   (let* ((gnus-show-threads t)
8918          (article (gnus-summary-article-number))
8919          (id (mail-header-id (gnus-summary-article-header)))
8920          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8921     (unless id
8922       (error "No article on the current line"))
8923     (gnus-rebuild-thread id)
8924     (gnus-summary-goto-subject article)))
8925
8926 (defun gnus-summary-reparent-thread ()
8927   "Make the current article child of the marked (or previous) article.
8928
8929 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8930 is non-nil or the Subject: of both articles are the same."
8931   (interactive)
8932   (unless (not (gnus-group-read-only-p))
8933     (error "The current newsgroup does not support article editing"))
8934   (unless (<= (length gnus-newsgroup-processable) 1)
8935     (error "No more than one article may be marked"))
8936   (save-window-excursion
8937     (let ((gnus-article-buffer " *reparent*")
8938           (current-article (gnus-summary-article-number))
8939           ;; First grab the marked article, otherwise one line up.
8940           (parent-article (if (not (null gnus-newsgroup-processable))
8941                               (car gnus-newsgroup-processable)
8942                             (save-excursion
8943                               (if (eq (forward-line -1) 0)
8944                                   (gnus-summary-article-number)
8945                                 (error "Beginning of summary buffer"))))))
8946       (unless (not (eq current-article parent-article))
8947         (error "An article may not be self-referential"))
8948       (let ((message-id (mail-header-id
8949                          (gnus-summary-article-header parent-article))))
8950         (unless (and message-id (not (equal message-id "")))
8951           (error "No message-id in desired parent"))
8952         (gnus-with-article current-article
8953           (save-restriction
8954             (goto-char (point-min))
8955             (message-narrow-to-head)
8956             (if (re-search-forward "^References: " nil t)
8957                 (progn
8958                   (re-search-forward "^[^ \t]" nil t)
8959                   (forward-line -1)
8960                   (end-of-line)
8961                   (insert " " message-id))
8962               (insert "References: " message-id "\n"))))
8963         (set-buffer gnus-summary-buffer)
8964         (gnus-summary-unmark-all-processable)
8965         (gnus-summary-update-article current-article)
8966         (gnus-summary-rethread-current)
8967         (gnus-message 3 "Article %d is now the child of article %d"
8968                       current-article parent-article)))))
8969
8970 (defun gnus-summary-toggle-threads (&optional arg)
8971   "Toggle showing conversation threads.
8972 If ARG is positive number, turn showing conversation threads on."
8973   (interactive "P")
8974   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8975     (setq gnus-show-threads
8976           (if (null arg) (not gnus-show-threads)
8977             (> (prefix-numeric-value arg) 0)))
8978     (gnus-summary-prepare)
8979     (gnus-summary-goto-subject current)
8980     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8981     (gnus-summary-position-point)))
8982
8983 (defun gnus-summary-show-all-threads ()
8984   "Show all threads."
8985   (interactive)
8986   (save-excursion
8987     (let ((buffer-read-only nil))
8988       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8989   (gnus-summary-position-point))
8990
8991 (defun gnus-summary-show-thread ()
8992   "Show thread subtrees.
8993 Returns nil if no thread was there to be shown."
8994   (interactive)
8995   (let ((buffer-read-only nil)
8996         (orig (point))
8997         ;; first goto end then to beg, to have point at beg after let
8998         (end (progn (end-of-line) (point)))
8999         (beg (progn (beginning-of-line) (point))))
9000     (prog1
9001         ;; Any hidden lines here?
9002         (search-forward "\r" end t)
9003       (subst-char-in-region beg end ?\^M ?\n t)
9004       (goto-char orig)
9005       (gnus-summary-position-point))))
9006
9007 (defun gnus-summary-hide-all-threads ()
9008   "Hide all thread subtrees."
9009   (interactive)
9010   (save-excursion
9011     (goto-char (point-min))
9012     (gnus-summary-hide-thread)
9013     (while (zerop (gnus-summary-next-thread 1 t))
9014       (gnus-summary-hide-thread)))
9015   (gnus-summary-position-point))
9016
9017 (defun gnus-summary-hide-thread ()
9018   "Hide thread subtrees.
9019 Returns nil if no threads were there to be hidden."
9020   (interactive)
9021   (let ((buffer-read-only nil)
9022         (start (point))
9023         (article (gnus-summary-article-number)))
9024     (goto-char start)
9025     ;; Go forward until either the buffer ends or the subthread
9026     ;; ends.
9027     (when (and (not (eobp))
9028                (or (zerop (gnus-summary-next-thread 1 t))
9029                    (goto-char (point-max))))
9030       (prog1
9031           (if (and (> (point) start)
9032                    (search-backward "\n" start t))
9033               (progn
9034                 (subst-char-in-region start (point) ?\n ?\^M)
9035                 (gnus-summary-goto-subject article))
9036             (goto-char start)
9037             nil)))))
9038
9039 (defun gnus-summary-go-to-next-thread (&optional previous)
9040   "Go to the same level (or less) next thread.
9041 If PREVIOUS is non-nil, go to previous thread instead.
9042 Return the article number moved to, or nil if moving was impossible."
9043   (let ((level (gnus-summary-thread-level))
9044         (way (if previous -1 1))
9045         (beg (point)))
9046     (forward-line way)
9047     (while (and (not (eobp))
9048                 (< level (gnus-summary-thread-level)))
9049       (forward-line way))
9050     (if (eobp)
9051         (progn
9052           (goto-char beg)
9053           nil)
9054       (setq beg (point))
9055       (prog1
9056           (gnus-summary-article-number)
9057         (goto-char beg)))))
9058
9059 (defun gnus-summary-next-thread (n &optional silent)
9060   "Go to the same level next N'th thread.
9061 If N is negative, search backward instead.
9062 Returns the difference between N and the number of skips actually
9063 done.
9064
9065 If SILENT, don't output messages."
9066   (interactive "p")
9067   (let ((backward (< n 0))
9068         (n (abs n)))
9069     (while (and (> n 0)
9070                 (gnus-summary-go-to-next-thread backward))
9071       (decf n))
9072     (unless silent
9073       (gnus-summary-position-point))
9074     (when (and (not silent) (/= 0 n))
9075       (gnus-message 7 "No more threads"))
9076     n))
9077
9078 (defun gnus-summary-prev-thread (n)
9079   "Go to the same level previous N'th thread.
9080 Returns the difference between N and the number of skips actually
9081 done."
9082   (interactive "p")
9083   (gnus-summary-next-thread (- n)))
9084
9085 (defun gnus-summary-go-down-thread ()
9086   "Go down one level in the current thread."
9087   (let ((children (gnus-summary-article-children)))
9088     (when children
9089       (gnus-summary-goto-subject (car children)))))
9090
9091 (defun gnus-summary-go-up-thread ()
9092   "Go up one level in the current thread."
9093   (let ((parent (gnus-summary-article-parent)))
9094     (when parent
9095       (gnus-summary-goto-subject parent))))
9096
9097 (defun gnus-summary-down-thread (n)
9098   "Go down thread N steps.
9099 If N is negative, go up instead.
9100 Returns the difference between N and how many steps down that were
9101 taken."
9102   (interactive "p")
9103   (let ((up (< n 0))
9104         (n (abs n)))
9105     (while (and (> n 0)
9106                 (if up (gnus-summary-go-up-thread)
9107                   (gnus-summary-go-down-thread)))
9108       (setq n (1- n)))
9109     (gnus-summary-position-point)
9110     (when (/= 0 n)
9111       (gnus-message 7 "Can't go further"))
9112     n))
9113
9114 (defun gnus-summary-up-thread (n)
9115   "Go up thread N steps.
9116 If N is negative, go up instead.
9117 Returns the difference between N and how many steps down that were
9118 taken."
9119   (interactive "p")
9120   (gnus-summary-down-thread (- n)))
9121
9122 (defun gnus-summary-top-thread ()
9123   "Go to the top of the thread."
9124   (interactive)
9125   (while (gnus-summary-go-up-thread))
9126   (gnus-summary-article-number))
9127
9128 (defun gnus-summary-kill-thread (&optional unmark)
9129   "Mark articles under current thread as read.
9130 If the prefix argument is positive, remove any kinds of marks.
9131 If the prefix argument is negative, tick articles instead."
9132   (interactive "P")
9133   (when unmark
9134     (setq unmark (prefix-numeric-value unmark)))
9135   (let ((articles (gnus-summary-articles-in-thread)))
9136     (save-excursion
9137       ;; Expand the thread.
9138       (gnus-summary-show-thread)
9139       ;; Mark all the articles.
9140       (while articles
9141         (gnus-summary-goto-subject (car articles))
9142         (cond ((null unmark)
9143                (gnus-summary-mark-article-as-read gnus-killed-mark))
9144               ((> unmark 0)
9145                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9146               (t
9147                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9148         (setq articles (cdr articles))))
9149     ;; Hide killed subtrees.
9150     (and (null unmark)
9151          gnus-thread-hide-killed
9152          (gnus-summary-hide-thread))
9153     ;; If marked as read, go to next unread subject.
9154     (when (null unmark)
9155       ;; Go to next unread subject.
9156       (gnus-summary-next-subject 1 t)))
9157   (gnus-set-mode-line 'summary))
9158
9159 ;; Summary sorting commands
9160
9161 (defun gnus-summary-sort-by-number (&optional reverse)
9162   "Sort the summary buffer by article number.
9163 Argument REVERSE means reverse order."
9164   (interactive "P")
9165   (gnus-summary-sort 'number reverse))
9166
9167 (defun gnus-summary-sort-by-author (&optional reverse)
9168   "Sort the summary buffer by author name alphabetically.
9169 If `case-fold-search' is non-nil, case of letters is ignored.
9170 Argument REVERSE means reverse order."
9171   (interactive "P")
9172   (gnus-summary-sort 'author reverse))
9173
9174 (defun gnus-summary-sort-by-subject (&optional reverse)
9175   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9176 If `case-fold-search' is non-nil, case of letters is ignored.
9177 Argument REVERSE means reverse order."
9178   (interactive "P")
9179   (gnus-summary-sort 'subject reverse))
9180
9181 (defun gnus-summary-sort-by-date (&optional reverse)
9182   "Sort the summary buffer by date.
9183 Argument REVERSE means reverse order."
9184   (interactive "P")
9185   (gnus-summary-sort 'date reverse))
9186
9187 (defun gnus-summary-sort-by-score (&optional reverse)
9188   "Sort the summary buffer by score.
9189 Argument REVERSE means reverse order."
9190   (interactive "P")
9191   (gnus-summary-sort 'score reverse))
9192
9193 (defun gnus-summary-sort-by-lines (&optional reverse)
9194   "Sort the summary buffer by the number of lines.
9195 Argument REVERSE means reverse order."
9196   (interactive "P")
9197   (gnus-summary-sort 'lines reverse))
9198
9199 (defun gnus-summary-sort-by-chars (&optional reverse)
9200   "Sort the summary buffer by article length.
9201 Argument REVERSE means reverse order."
9202   (interactive "P")
9203   (gnus-summary-sort 'chars reverse))
9204
9205 (defun gnus-summary-sort (predicate reverse)
9206   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9207   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9208          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9209          (gnus-thread-sort-functions
9210           (if (not reverse)
9211               thread
9212             `(lambda (t1 t2)
9213                (,thread t2 t1))))
9214          (gnus-sort-gathered-threads-function
9215           gnus-thread-sort-functions)
9216          (gnus-article-sort-functions
9217           (if (not reverse)
9218               article
9219             `(lambda (t1 t2)
9220                (,article t2 t1))))
9221          (buffer-read-only)
9222          (gnus-summary-prepare-hook nil))
9223     ;; We do the sorting by regenerating the threads.
9224     (gnus-summary-prepare)
9225     ;; Hide subthreads if needed.
9226     (when (and gnus-show-threads gnus-thread-hide-subtree)
9227       (gnus-summary-hide-all-threads))))
9228
9229 ;; Summary saving commands.
9230
9231 (defun gnus-summary-save-article (&optional n not-saved)
9232   "Save the current article using the default saver function.
9233 If N is a positive number, save the N next articles.
9234 If N is a negative number, save the N previous articles.
9235 If N is nil and any articles have been marked with the process mark,
9236 save those articles instead.
9237 The variable `gnus-default-article-saver' specifies the saver function."
9238   (interactive "P")
9239   (let* ((articles (gnus-summary-work-articles n))
9240          (save-buffer (save-excursion
9241                         (nnheader-set-temp-buffer " *Gnus Save*")))
9242          (num (length articles))
9243          header file)
9244     (dolist (article articles)
9245       (setq header (gnus-summary-article-header article))
9246       (if (not (vectorp header))
9247           ;; This is a pseudo-article.
9248           (if (assq 'name header)
9249               (gnus-copy-file (cdr (assq 'name header)))
9250             (gnus-message 1 "Article %d is unsaveable" article))
9251         ;; This is a real article.
9252         (save-window-excursion
9253           (gnus-summary-select-article t nil nil article))
9254         (save-excursion
9255           (set-buffer save-buffer)
9256           (erase-buffer)
9257           (insert-buffer-substring gnus-original-article-buffer))
9258         (setq file (gnus-article-save save-buffer file num))
9259         (gnus-summary-remove-process-mark article)
9260         (unless not-saved
9261           (gnus-summary-set-saved-mark article))))
9262     (gnus-kill-buffer save-buffer)
9263     (gnus-summary-position-point)
9264     (gnus-set-mode-line 'summary)
9265     n))
9266
9267 (defun gnus-summary-pipe-output (&optional arg)
9268   "Pipe the current article to a subprocess.
9269 If N is a positive number, pipe the N next articles.
9270 If N is a negative number, pipe the N previous articles.
9271 If N is nil and any articles have been marked with the process mark,
9272 pipe those articles instead."
9273   (interactive "P")
9274   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9275     (gnus-summary-save-article arg t))
9276   (gnus-configure-windows 'pipe))
9277
9278 (defun gnus-summary-save-article-mail (&optional arg)
9279   "Append the current article to an mail file.
9280 If N is a positive number, save the N next articles.
9281 If N is a negative number, save the N previous articles.
9282 If N is nil and any articles have been marked with the process mark,
9283 save those articles instead."
9284   (interactive "P")
9285   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9286     (gnus-summary-save-article arg)))
9287
9288 (defun gnus-summary-save-article-rmail (&optional arg)
9289   "Append the current article to an rmail file.
9290 If N is a positive number, save the N next articles.
9291 If N is a negative number, save the N previous articles.
9292 If N is nil and any articles have been marked with the process mark,
9293 save those articles instead."
9294   (interactive "P")
9295   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9296     (gnus-summary-save-article arg)))
9297
9298 (defun gnus-summary-save-article-file (&optional arg)
9299   "Append the current article to a file.
9300 If N is a positive number, save the N next articles.
9301 If N is a negative number, save the N previous articles.
9302 If N is nil and any articles have been marked with the process mark,
9303 save those articles instead."
9304   (interactive "P")
9305   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9306     (gnus-summary-save-article arg)))
9307
9308 (defun gnus-summary-write-article-file (&optional arg)
9309   "Write the current article to a file, deleting the previous file.
9310 If N is a positive number, save the N next articles.
9311 If N is a negative number, save the N previous articles.
9312 If N is nil and any articles have been marked with the process mark,
9313 save those articles instead."
9314   (interactive "P")
9315   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9316     (gnus-summary-save-article arg)))
9317
9318 (defun gnus-summary-save-article-body-file (&optional arg)
9319   "Append the current article body to a file.
9320 If N is a positive number, save the N next articles.
9321 If N is a negative number, save the N previous articles.
9322 If N is nil and any articles have been marked with the process mark,
9323 save those articles instead."
9324   (interactive "P")
9325   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9326     (gnus-summary-save-article arg)))
9327
9328 (defun gnus-summary-pipe-message (program)
9329   "Pipe the current article through PROGRAM."
9330   (interactive "sProgram: ")
9331   (gnus-summary-select-article)
9332   (let ((mail-header-separator ""))
9333     (gnus-eval-in-buffer-window gnus-article-buffer
9334       (save-restriction
9335         (widen)
9336         (let ((start (window-start))
9337               buffer-read-only)
9338           (message-pipe-buffer-body program)
9339           (set-window-start (get-buffer-window (current-buffer)) start))))))
9340
9341 (defun gnus-get-split-value (methods)
9342   "Return a value based on the split METHODS."
9343   (let (split-name method result match)
9344     (when methods
9345       (save-excursion
9346         (set-buffer gnus-original-article-buffer)
9347         (save-restriction
9348           (nnheader-narrow-to-headers)
9349           (while (and methods (not split-name))
9350             (goto-char (point-min))
9351             (setq method (pop methods))
9352             (setq match (car method))
9353             (when (cond
9354                    ((stringp match)
9355                     ;; Regular expression.
9356                     (ignore-errors
9357                       (re-search-forward match nil t)))
9358                    ((gnus-functionp match)
9359                     ;; Function.
9360                     (save-restriction
9361                       (widen)
9362                       (setq result (funcall match gnus-newsgroup-name))))
9363                    ((consp match)
9364                     ;; Form.
9365                     (save-restriction
9366                       (widen)
9367                       (setq result (eval match)))))
9368               (setq split-name (cdr method))
9369               (cond ((stringp result)
9370                      (push (expand-file-name
9371                             result gnus-article-save-directory)
9372                            split-name))
9373                     ((consp result)
9374                      (setq split-name (append result split-name)))))))))
9375     (nreverse split-name)))
9376
9377 (defun gnus-valid-move-group-p (group)
9378   (and (boundp group)
9379        (symbol-name group)
9380        (symbol-value group)
9381        (gnus-get-function (gnus-find-method-for-group
9382                            (symbol-name group)) 'request-accept-article t)))
9383
9384 (defun gnus-read-move-group-name (prompt default articles prefix)
9385   "Read a group name."
9386   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9387          (minibuffer-confirm-incomplete nil) ; XEmacs
9388          (prom
9389           (format "%s %s to:"
9390                   prompt
9391                   (if (> (length articles) 1)
9392                       (format "these %d articles" (length articles))
9393                     "this article")))
9394          (to-newsgroup
9395           (cond
9396            ((null split-name)
9397             (gnus-completing-read default prom
9398                                   gnus-active-hashtb
9399                                   'gnus-valid-move-group-p
9400                                   nil prefix
9401                                   'gnus-group-history))
9402            ((= 1 (length split-name))
9403             (gnus-completing-read (car split-name) prom
9404                                   gnus-active-hashtb
9405                                   'gnus-valid-move-group-p
9406                                   nil nil
9407                                   'gnus-group-history))
9408            (t
9409             (gnus-completing-read nil prom
9410                                   (mapcar (lambda (el) (list el))
9411                                           (nreverse split-name))
9412                                   nil nil nil
9413                                   'gnus-group-history))))
9414          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9415     (when to-newsgroup
9416       (if (or (string= to-newsgroup "")
9417               (string= to-newsgroup prefix))
9418           (setq to-newsgroup default))
9419       (unless to-newsgroup
9420         (error "No group name entered"))
9421       (or (gnus-active to-newsgroup)
9422           (gnus-activate-group to-newsgroup nil nil to-method)
9423           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9424                                      to-newsgroup))
9425               (or (and (gnus-request-create-group to-newsgroup to-method)
9426                        (gnus-activate-group
9427                         to-newsgroup nil nil to-method)
9428                        (gnus-subscribe-group to-newsgroup))
9429                   (error "Couldn't create group %s" to-newsgroup)))
9430           (error "No such group: %s" to-newsgroup)))
9431     to-newsgroup))
9432
9433 (defun gnus-summary-save-parts (type dir n &optional reverse)
9434   "Save parts matching TYPE to DIR.
9435 If REVERSE, save parts that do not match TYPE."
9436   (interactive
9437    (list (read-string "Save parts of type: " 
9438                       (or (car gnus-summary-save-parts-type-history)
9439                           gnus-summary-save-parts-default-mime)
9440                       'gnus-summary-save-parts-type-history)
9441          (setq gnus-summary-save-parts-last-directory
9442                (read-file-name "Save to directory: " 
9443                                gnus-summary-save-parts-last-directory
9444                                nil t))
9445          current-prefix-arg))
9446   (gnus-summary-iterate n
9447     (let ((gnus-display-mime-function nil)
9448           (gnus-inhibit-treatment t))
9449       (gnus-summary-select-article))
9450     (save-excursion
9451       (set-buffer gnus-article-buffer)
9452       (let ((handles (or gnus-article-mime-handles
9453                          (mm-dissect-buffer) (mm-uu-dissect))))
9454         (when handles
9455           (gnus-summary-save-parts-1 type dir handles reverse)
9456           (unless gnus-article-mime-handles ;; Don't destroy this case.
9457             (mm-destroy-parts handles)))))))
9458
9459 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9460   (if (stringp (car handle))
9461       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9462               (cdr handle))
9463     (when (if reverse
9464               (not (string-match type (mm-handle-media-type handle)))
9465             (string-match type (mm-handle-media-type handle)))
9466       (let ((file (expand-file-name
9467                    (file-name-nondirectory
9468                     (or
9469                      (mail-content-type-get
9470                       (mm-handle-disposition handle) 'filename)
9471                      (concat gnus-newsgroup-name
9472                              "." (number-to-string
9473                                   (cdr gnus-article-current)))))
9474                    dir)))
9475         (unless (file-exists-p file)
9476           (mm-save-part-to-file handle file))))))
9477
9478 ;; Summary extract commands
9479
9480 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9481   (let ((buffer-read-only nil)
9482         (article (gnus-summary-article-number))
9483         after-article b e)
9484     (unless (gnus-summary-goto-subject article)
9485       (error "No such article: %d" article))
9486     (gnus-summary-position-point)
9487     ;; If all commands are to be bunched up on one line, we collect
9488     ;; them here.
9489     (unless gnus-view-pseudos-separately
9490       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9491             files action)
9492         (while ps
9493           (setq action (cdr (assq 'action (car ps))))
9494           (setq files (list (cdr (assq 'name (car ps)))))
9495           (while (and ps (cdr ps)
9496                       (string= (or action "1")
9497                                (or (cdr (assq 'action (cadr ps))) "2")))
9498             (push (cdr (assq 'name (cadr ps))) files)
9499             (setcdr ps (cddr ps)))
9500           (when files
9501             (when (not (string-match "%s" action))
9502               (push " " files))
9503             (push " " files)
9504             (when (assq 'execute (car ps))
9505               (setcdr (assq 'execute (car ps))
9506                       (funcall (if (string-match "%s" action)
9507                                    'format 'concat)
9508                                action
9509                                (mapconcat
9510                                 (lambda (f)
9511                                   (if (equal f " ")
9512                                       f
9513                                     (gnus-quote-arg-for-sh-or-csh f)))
9514                                 files " ")))))
9515           (setq ps (cdr ps)))))
9516     (if (and gnus-view-pseudos (not not-view))
9517         (while pslist
9518           (when (assq 'execute (car pslist))
9519             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9520                                   (eq gnus-view-pseudos 'not-confirm)))
9521           (setq pslist (cdr pslist)))
9522       (save-excursion
9523         (while pslist
9524           (setq after-article (or (cdr (assq 'article (car pslist)))
9525                                   (gnus-summary-article-number)))
9526           (gnus-summary-goto-subject after-article)
9527           (forward-line 1)
9528           (setq b (point))
9529           (insert "    " (file-name-nondirectory
9530                           (cdr (assq 'name (car pslist))))
9531                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9532           (setq e (point))
9533           (forward-line -1)             ; back to `b'
9534           (gnus-add-text-properties
9535            b (1- e) (list 'gnus-number gnus-reffed-article-number
9536                           gnus-mouse-face-prop gnus-mouse-face))
9537           (gnus-data-enter
9538            after-article gnus-reffed-article-number
9539            gnus-unread-mark b (car pslist) 0 (- e b))
9540           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9541           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9542           (setq pslist (cdr pslist)))))))
9543
9544 (defun gnus-pseudos< (p1 p2)
9545   (let ((c1 (cdr (assq 'action p1)))
9546         (c2 (cdr (assq 'action p2))))
9547     (and c1 c2 (string< c1 c2))))
9548
9549 (defun gnus-request-pseudo-article (props)
9550   (cond ((assq 'execute props)
9551          (gnus-execute-command (cdr (assq 'execute props)))))
9552   (let ((gnus-current-article (gnus-summary-article-number)))
9553     (gnus-run-hooks 'gnus-mark-article-hook)))
9554
9555 (defun gnus-execute-command (command &optional automatic)
9556   (save-excursion
9557     (gnus-article-setup-buffer)
9558     (set-buffer gnus-article-buffer)
9559     (setq buffer-read-only nil)
9560     (let ((command (if automatic command
9561                      (read-string "Command: " (cons command 0)))))
9562       (erase-buffer)
9563       (insert "$ " command "\n\n")
9564       (if gnus-view-pseudo-asynchronously
9565           (start-process "gnus-execute" (current-buffer) shell-file-name
9566                          shell-command-switch command)
9567         (call-process shell-file-name nil t nil
9568                       shell-command-switch command)))))
9569
9570 ;; Summary kill commands.
9571
9572 (defun gnus-summary-edit-global-kill (article)
9573   "Edit the \"global\" kill file."
9574   (interactive (list (gnus-summary-article-number)))
9575   (gnus-group-edit-global-kill article))
9576
9577 (defun gnus-summary-edit-local-kill ()
9578   "Edit a local kill file applied to the current newsgroup."
9579   (interactive)
9580   (setq gnus-current-headers (gnus-summary-article-header))
9581   (gnus-group-edit-local-kill
9582    (gnus-summary-article-number) gnus-newsgroup-name))
9583
9584 ;;; Header reading.
9585
9586 (defun gnus-read-header (id &optional header)
9587   "Read the headers of article ID and enter them into the Gnus system."
9588   (let ((group gnus-newsgroup-name)
9589         (gnus-override-method
9590          (or
9591           gnus-override-method
9592           (and (gnus-news-group-p gnus-newsgroup-name)
9593                (car (gnus-refer-article-methods)))))
9594         where)
9595     ;; First we check to see whether the header in question is already
9596     ;; fetched.
9597     (if (stringp id)
9598         ;; This is a Message-ID.
9599         (setq header (or header (gnus-id-to-header id)))
9600       ;; This is an article number.
9601       (setq header (or header (gnus-summary-article-header id))))
9602     (if (and header
9603              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9604         ;; We have found the header.
9605         header
9606       ;; If this is a sparse article, we have to nix out its
9607       ;; previous entry in the thread hashtb.
9608       (when (and header
9609                  (gnus-summary-article-sparse-p (mail-header-number header)))
9610         (let* ((parent (gnus-parent-id (mail-header-references header)))
9611                (thread (and parent (gnus-id-to-thread parent))))
9612           (when thread
9613             (delq (assq header thread) thread))))
9614       ;; We have to really fetch the header to this article.
9615       (save-excursion
9616         (set-buffer nntp-server-buffer)
9617         (when (setq where (gnus-request-head id group))
9618           (nnheader-fold-continuation-lines)
9619           (goto-char (point-max))
9620           (insert ".\n")
9621           (goto-char (point-min))
9622           (insert "211 ")
9623           (princ (cond
9624                   ((numberp id) id)
9625                   ((cdr where) (cdr where))
9626                   (header (mail-header-number header))
9627                   (t gnus-reffed-article-number))
9628                  (current-buffer))
9629           (insert " Article retrieved.\n"))
9630         (if (or (not where)
9631                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9632             ()                          ; Malformed head.
9633           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9634             (when (and (stringp id)
9635                        (not (string= (gnus-group-real-name group)
9636                                      (car where))))
9637               ;; If we fetched by Message-ID and the article came
9638               ;; from a different group, we fudge some bogus article
9639               ;; numbers for this article.
9640               (mail-header-set-number header gnus-reffed-article-number))
9641             (save-excursion
9642               (set-buffer gnus-summary-buffer)
9643               (decf gnus-reffed-article-number)
9644               (gnus-remove-header (mail-header-number header))
9645               (push header gnus-newsgroup-headers)
9646               (setq gnus-current-headers header)
9647               (push (mail-header-number header) gnus-newsgroup-limit)))
9648           header)))))
9649
9650 (defun gnus-remove-header (number)
9651   "Remove header NUMBER from `gnus-newsgroup-headers'."
9652   (if (and gnus-newsgroup-headers
9653            (= number (mail-header-number (car gnus-newsgroup-headers))))
9654       (pop gnus-newsgroup-headers)
9655     (let ((headers gnus-newsgroup-headers))
9656       (while (and (cdr headers)
9657                   (not (= number (mail-header-number (cadr headers)))))
9658         (pop headers))
9659       (when (cdr headers)
9660         (setcdr headers (cddr headers))))))
9661
9662 ;;;
9663 ;;; summary highlights
9664 ;;;
9665
9666 (defun gnus-highlight-selected-summary ()
9667   "Highlight selected article in summary buffer."
9668   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9669   (when gnus-summary-selected-face
9670     (save-excursion
9671       (let* ((beg (progn (beginning-of-line) (point)))
9672              (end (progn (end-of-line) (point)))
9673              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9674              (from (if (get-text-property beg gnus-mouse-face-prop)
9675                        beg
9676                      (or (next-single-property-change
9677                           beg gnus-mouse-face-prop nil end)
9678                          beg)))
9679              (to
9680               (if (= from end)
9681                   (- from 2)
9682                 (or (next-single-property-change
9683                      from gnus-mouse-face-prop nil end)
9684                     end))))
9685         ;; If no mouse-face prop on line we will have to = from = end,
9686         ;; so we highlight the entire line instead.
9687         (when (= (+ to 2) from)
9688           (setq from beg)
9689           (setq to end))
9690         (if gnus-newsgroup-selected-overlay
9691             ;; Move old overlay.
9692             (gnus-move-overlay
9693              gnus-newsgroup-selected-overlay from to (current-buffer))
9694           ;; Create new overlay.
9695           (gnus-overlay-put
9696            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9697            'face gnus-summary-selected-face))))))
9698
9699 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9700 (defun gnus-summary-highlight-line ()
9701   "Highlight current line according to `gnus-summary-highlight'."
9702   (let* ((list gnus-summary-highlight)
9703          (p (point))
9704          (end (progn (end-of-line) (point)))
9705          ;; now find out where the line starts and leave point there.
9706          (beg (progn (beginning-of-line) (point)))
9707          (article (gnus-summary-article-number))
9708          (score (or (cdr (assq (or article gnus-current-article)
9709                                gnus-newsgroup-scored))
9710                     gnus-summary-default-score 0))
9711          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9712          (inhibit-read-only t))
9713     ;; Eval the cars of the lists until we find a match.
9714     (let ((default gnus-summary-default-score))
9715       (while (and list
9716                   (not (eval (caar list))))
9717         (setq list (cdr list))))
9718     (let ((face (cdar list)))
9719       (unless (eq face (get-text-property beg 'face))
9720         (gnus-put-text-property-excluding-characters-with-faces
9721          beg end 'face
9722          (setq face (if (boundp face) (symbol-value face) face)))
9723         (when gnus-summary-highlight-line-function
9724           (funcall gnus-summary-highlight-line-function article face))))
9725     (goto-char p)))
9726
9727 (defun gnus-update-read-articles (group unread &optional compute)
9728   "Update the list of read articles in GROUP."
9729   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9730          (entry (gnus-gethash group gnus-newsrc-hashtb))
9731          (info (nth 2 entry))
9732          (prev 1)
9733          (unread (sort (copy-sequence unread) '<))
9734          read)
9735     (if (or (not info) (not active))
9736         ;; There is no info on this group if it was, in fact,
9737         ;; killed.  Gnus stores no information on killed groups, so
9738         ;; there's nothing to be done.
9739         ;; One could store the information somewhere temporarily,
9740         ;; perhaps...  Hmmm...
9741         ()
9742       ;; Remove any negative articles numbers.
9743       (while (and unread (< (car unread) 0))
9744         (setq unread (cdr unread)))
9745       ;; Remove any expired article numbers
9746       (while (and unread (< (car unread) (car active)))
9747         (setq unread (cdr unread)))
9748       ;; Compute the ranges of read articles by looking at the list of
9749       ;; unread articles.
9750       (while unread
9751         (when (/= (car unread) prev)
9752           (push (if (= prev (1- (car unread))) prev
9753                   (cons prev (1- (car unread))))
9754                 read))
9755         (setq prev (1+ (car unread)))
9756         (setq unread (cdr unread)))
9757       (when (<= prev (cdr active))
9758         (push (cons prev (cdr active)) read))
9759       (setq read (if (> (length read) 1) (nreverse read) read))
9760       (if compute
9761           read
9762         (save-excursion
9763           (let (setmarkundo)
9764             ;; Propagate the read marks to the backend.
9765             (when (gnus-check-backend-function 'request-set-mark group)
9766               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9767                     (add (gnus-remove-from-range read (gnus-info-read info))))
9768                 (when (or add del)
9769                   (unless (gnus-check-group group)
9770                     (error "Can't open server for %s" group))
9771                   (gnus-request-set-mark
9772                    group (delq nil (list (if add (list add 'add '(read)))
9773                                          (if del (list del 'del '(read))))))
9774                   (setq setmarkundo
9775                         `(gnus-request-set-mark
9776                           ,group
9777                           ',(delq nil (list
9778                                        (if del (list del 'add '(read)))
9779                                        (if add (list add 'del '(read))))))))))
9780             (set-buffer gnus-group-buffer)
9781             (gnus-undo-register
9782               `(progn
9783                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9784                  (gnus-info-set-read ',info ',(gnus-info-read info))
9785                  (gnus-get-unread-articles-in-group ',info 
9786                                                     (gnus-active ,group))
9787                  (gnus-group-update-group ,group t)
9788                  ,setmarkundo))))
9789         ;; Enter this list into the group info.
9790         (gnus-info-set-read info read)
9791         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9792         (gnus-get-unread-articles-in-group info (gnus-active group))
9793         t))))
9794
9795 (defun gnus-offer-save-summaries ()
9796   "Offer to save all active summary buffers."
9797   (save-excursion
9798     (let ((buflist (buffer-list))
9799           buffers bufname)
9800       ;; Go through all buffers and find all summaries.
9801       (while buflist
9802         (and (setq bufname (buffer-name (car buflist)))
9803              (string-match "Summary" bufname)
9804              (save-excursion
9805                (set-buffer bufname)
9806                ;; We check that this is, indeed, a summary buffer.
9807                (and (eq major-mode 'gnus-summary-mode)
9808                     ;; Also make sure this isn't bogus.
9809                     gnus-newsgroup-prepared
9810                     ;; Also make sure that this isn't a dead summary buffer.
9811                     (not gnus-dead-summary-mode)))
9812              (push bufname buffers))
9813         (setq buflist (cdr buflist)))
9814       ;; Go through all these summary buffers and offer to save them.
9815       (when buffers
9816         (map-y-or-n-p
9817          "Update summary buffer %s? "
9818          (lambda (buf)
9819            (switch-to-buffer buf)
9820            (gnus-summary-exit))
9821          buffers)))))
9822
9823
9824 ;;; @ for mime-partial
9825 ;;;
9826
9827 (defun gnus-request-partial-message ()
9828   (save-excursion
9829     (let ((number (gnus-summary-article-number))
9830           (group gnus-newsgroup-name)
9831           (mother gnus-article-buffer))
9832       (set-buffer (get-buffer-create " *Partial Article*"))
9833       (erase-buffer)
9834       (setq mime-preview-buffer mother)
9835       (gnus-request-article-this-buffer number group)
9836       (mime-parse-buffer)
9837       )))
9838
9839 (autoload 'mime-combine-message/partial-pieces-automatically
9840   "mime-partial"
9841   "Internal method to combine message/partial messages automatically.")
9842
9843 (mime-add-condition
9844  'action '((type . message)(subtype . partial)
9845            (major-mode . gnus-original-article-mode)
9846            (method . mime-combine-message/partial-pieces-automatically)
9847            (summary-buffer-exp . gnus-summary-buffer)
9848            (request-partial-message-method . gnus-request-partial-message)
9849            ))
9850
9851
9852 ;;; @ for message/rfc822
9853 ;;;
9854
9855 (defun gnus-mime-extract-message/rfc822 (entity situation)
9856   (let (group article num cwin swin cur)
9857     (with-temp-buffer
9858       (mime-insert-entity-content entity)
9859       (setq group (or (cdr (assq 'group situation))
9860                       (completing-read "Group: "
9861                                        gnus-active-hashtb
9862                                        nil
9863                                        (gnus-read-active-file-p)
9864                                        gnus-newsgroup-name))
9865             article (gnus-request-accept-article group)))
9866     (when (and (consp article)
9867                (numberp (setq article (cdr article))))
9868       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9869             cwin (get-buffer-window (current-buffer) t))
9870       (save-window-excursion
9871         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9872             (select-window swin)
9873           (set-buffer gnus-summary-buffer))
9874         (setq cur gnus-current-article)
9875         (forward-line num)
9876         (let (gnus-show-threads)
9877           (gnus-summary-goto-subject article t))
9878         (gnus-summary-clear-mark-forward 1)
9879         (gnus-summary-goto-subject cur))
9880       (when (and cwin (window-frame cwin))
9881         (select-frame (window-frame cwin)))
9882       (when (boundp 'mime-acting-situation-to-override)
9883         (set-alist 'mime-acting-situation-to-override
9884                    'group
9885                    group)
9886         (set-alist 'mime-acting-situation-to-override
9887                    'after-method
9888                    `(progn
9889                       (save-current-buffer
9890                         (set-buffer gnus-group-buffer)
9891                         (gnus-activate-group ,group))
9892                       (gnus-summary-goto-article ,cur
9893                                                  gnus-show-all-headers)))
9894         (set-alist 'mime-acting-situation-to-override
9895                    'number num)))))
9896
9897 (mime-add-condition
9898  'action '((type . message)(subtype . rfc822)
9899            (major-mode . gnus-original-article-mode)
9900            (method . gnus-mime-extract-message/rfc822)
9901            (mode . "extract")
9902            ))
9903
9904 (mime-add-condition
9905  'action '((type . message)(subtype . news)
9906            (major-mode . gnus-original-article-mode)
9907            (method . gnus-mime-extract-message/rfc822)
9908            (mode . "extract")
9909            ))
9910
9911 (defun gnus-mime-extract-multipart (entity situation)
9912   (let ((children (mime-entity-children entity))
9913         mime-acting-situation-to-override
9914         f)
9915     (while children
9916       (mime-play-entity (car children)
9917                         (cons (assq 'mode situation)
9918                               mime-acting-situation-to-override))
9919       (setq children (cdr children)))
9920     (if (setq f (cdr (assq 'after-method
9921                            mime-acting-situation-to-override)))
9922         (eval f)
9923       )))
9924
9925 (mime-add-condition
9926  'action '((type . multipart)
9927            (method . gnus-mime-extract-multipart)
9928            (mode . "extract")
9929            )
9930  'with-default)
9931
9932
9933 ;;; @ end
9934 ;;;
9935
9936 (defun gnus-summary-setup-default-charset ()
9937   "Setup newsgroup default charset."
9938   (if (equal gnus-newsgroup-name "nndraft:drafts")
9939       (setq gnus-newsgroup-charset nil)
9940     (let* ((name (and gnus-newsgroup-name
9941                       (gnus-group-real-name gnus-newsgroup-name)))
9942            (ignored-charsets
9943             (or gnus-newsgroup-ephemeral-ignored-charsets
9944                 (append
9945                  (and gnus-newsgroup-name
9946                       (or (gnus-group-find-parameter gnus-newsgroup-name
9947                                                      'ignored-charsets t)
9948                           (let ((alist gnus-group-ignored-charsets-alist)
9949                                 elem (charsets nil))
9950                             (while (setq elem (pop alist))
9951                               (when (and name
9952                                          (string-match (car elem) name))
9953                                 (setq alist nil
9954                                       charsets (cdr elem))))
9955                             charsets)))
9956                  gnus-newsgroup-ignored-charsets))))
9957       (setq gnus-newsgroup-charset
9958             (or gnus-newsgroup-ephemeral-charset
9959                 (and gnus-newsgroup-name
9960                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9961                          (let ((alist gnus-group-charset-alist)
9962                                elem charset)
9963                            (while (setq elem (pop alist))
9964                              (when (and name
9965                                         (string-match (car elem) name))
9966                                (setq alist nil
9967                                      charset (cadr elem))))
9968                            charset)))
9969                 gnus-default-charset))
9970       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9971            ignored-charsets))))
9972
9973 ;;;
9974 ;;; Mime Commands
9975 ;;;
9976
9977 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9978   "Display the current article buffer fully MIME-buttonized.
9979 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9980 treated as multipart/mixed."
9981   (interactive "P")
9982   (require 'gnus-art)
9983   (let ((gnus-unbuttonized-mime-types nil)
9984         (gnus-mime-display-multipart-as-mixed show-all-parts))
9985     (gnus-summary-show-article)))
9986
9987 (defun gnus-summary-repair-multipart (article)
9988   "Add a Content-Type header to a multipart article without one."
9989   (interactive (list (gnus-summary-article-number)))
9990   (gnus-with-article article
9991     (message-narrow-to-head)
9992     (message-remove-header "Mime-Version")
9993     (goto-char (point-max))
9994     (insert "Mime-Version: 1.0\n")
9995     (widen)
9996     (when (search-forward "\n--" nil t)
9997       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9998         (message-narrow-to-head)
9999         (message-remove-header "Content-Type")
10000         (goto-char (point-max))
10001         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10002                         separator))
10003         (widen))))
10004   (let (gnus-mark-article-hook)
10005     (gnus-summary-select-article t t nil article)))
10006
10007 (defun gnus-summary-toggle-display-buttonized ()
10008   "Toggle the buttonizing of the article buffer."
10009   (interactive)
10010   (require 'gnus-art)
10011   (if (setq gnus-inhibit-mime-unbuttonizing
10012             (not gnus-inhibit-mime-unbuttonizing))
10013       (let ((gnus-unbuttonized-mime-types nil))
10014         (gnus-summary-show-article))
10015     (gnus-summary-show-article)))
10016
10017 ;;;
10018 ;;; Intelli-mouse commmands
10019 ;;;
10020
10021 (defun gnus-wheel-summary-scroll (event)
10022   (interactive "e")
10023   (let ((amount (if (memq 'shift (event-modifiers event))
10024                     (car gnus-wheel-scroll-amount)
10025                   (cdr gnus-wheel-scroll-amount)))
10026         (direction (- (* (static-if (featurep 'xemacs)
10027                              (event-button event)
10028                            (cond ((eq 'mouse-4 (event-basic-type event))
10029                                   4)
10030                                  ((eq 'mouse-5 (event-basic-type event))
10031                                   5)))
10032                          2) 9))
10033         edge)
10034     (gnus-summary-scroll-up (* amount direction))
10035     (when (gnus-eval-in-buffer-window gnus-article-buffer
10036             (save-restriction
10037               (widen)
10038               (and (if (< 0 direction)
10039                        (gnus-article-next-page 0)
10040                      (gnus-article-prev-page 0)
10041                      (bobp))
10042                    (if (setq edge (get-text-property
10043                                    (point-min) 'gnus-wheel-edge))
10044                        (setq edge (* edge direction))
10045                      (setq edge -1))
10046                    (or (plusp edge)
10047                        (let ((buffer-read-only nil)
10048                              (inhibit-read-only t))
10049                          (put-text-property (point-min) (point-max)
10050                                             'gnus-wheel-edge direction)
10051                          nil))
10052                    (or (> edge gnus-wheel-edge-resistance)
10053                        (let ((buffer-read-only nil)
10054                              (inhibit-read-only t))
10055                          (put-text-property (point-min) (point-max)
10056                                             'gnus-wheel-edge
10057                                             (* (1+ edge) direction))
10058                          nil))
10059                    (eq last-command 'gnus-wheel-summary-scroll))))
10060       (gnus-summary-next-article nil nil (minusp direction)))))
10061
10062 (defun gnus-wheel-install ()
10063   "Enable mouse wheel support on summary window."
10064   (when gnus-use-wheel
10065     (let ((keys
10066            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10067       (dolist (key keys)
10068         (define-key gnus-summary-mode-map key
10069           'gnus-wheel-summary-scroll)))))
10070
10071 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10072
10073 ;;;
10074 ;;; Traditional PGP commmands
10075 ;;;
10076
10077 (defun gnus-summary-decrypt-article (&optional force)
10078   "Decrypt the current article in traditional PGP way.
10079 This will have permanent effect only in mail groups.
10080 If FORCE is non-nil, allow editing of articles even in read-only
10081 groups."
10082   (interactive "P")
10083   (gnus-summary-select-article t)
10084   (gnus-eval-in-buffer-window gnus-article-buffer
10085     (save-excursion
10086       (save-restriction
10087         (widen)
10088         (goto-char (point-min))
10089         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10090           (error "Not a traditional PGP message!"))
10091         (let ((armor-start (match-beginning 0)))
10092           (if (and (pgg-decrypt-region armor-start (point-max))
10093                    (or force (not (gnus-group-read-only-p))))
10094               (let ((inhibit-read-only t)
10095                     buffer-read-only)
10096                 (delete-region armor-start
10097                                (progn
10098                                  (re-search-forward "^-+END PGP" nil t)
10099                                  (beginning-of-line 2)
10100                                  (point)))
10101                 (insert-buffer-substring pgg-output-buffer))))))))
10102
10103 (defun gnus-summary-verify-article ()
10104   "Verify the current article in traditional PGP way."
10105   (interactive)
10106   (save-excursion
10107     (set-buffer gnus-original-article-buffer)
10108     (goto-char (point-min))
10109     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10110       (error "Not a traditional PGP message!"))
10111     (re-search-forward "^-+END PGP" nil t)
10112     (beginning-of-line 2)
10113     (call-interactively (function pgg-verify-region))))
10114
10115 ;;;
10116 ;;; Generic summary marking commands
10117 ;;;
10118
10119 (defvar gnus-summary-marking-alist
10120   '((read gnus-del-mark "d")
10121     (unread gnus-unread-mark "u")
10122     (ticked gnus-ticked-mark "!")
10123     (dormant gnus-dormant-mark "?")
10124     (expirable gnus-expirable-mark "e"))
10125   "An alist of names/marks/keystrokes.")
10126
10127 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10128 (defvar gnus-summary-mark-map)
10129
10130 (defun gnus-summary-make-all-marking-commands ()
10131   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10132   (dolist (elem gnus-summary-marking-alist)
10133     (apply 'gnus-summary-make-marking-command elem)))
10134
10135 (defun gnus-summary-make-marking-command (name mark keystroke)
10136   (let ((map (make-sparse-keymap)))
10137     (define-key gnus-summary-generic-mark-map keystroke map)
10138     (dolist (lway `((next "next" next nil "n")
10139                     (next-unread "next unread" next t "N")
10140                     (prev "previous" prev nil "p")
10141                     (prev-unread "previous unread" prev t "P")
10142                     (nomove "" nil nil ,keystroke)))
10143       (let ((func (gnus-summary-make-marking-command-1
10144                    mark (car lway) lway name)))
10145         (setq func (eval func))
10146         (define-key map (nth 4 lway) func)))))
10147
10148 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10149   `(defun ,(intern
10150             (format "gnus-summary-put-mark-as-%s%s"
10151                     name (if (eq way 'nomove)
10152                              ""
10153                            (concat "-" (symbol-name way)))))
10154      (n)
10155      ,(format
10156        "Mark the current article as %s%s.
10157 If N, the prefix, then repeat N times.
10158 If N is negative, move in reverse order.
10159 The difference between N and the actual number of articles marked is
10160 returned."
10161        name (car (cdr lway)))
10162      (interactive "p")
10163      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10164
10165 (defun gnus-summary-generic-mark (n mark move unread)
10166   "Mark N articles with MARK."
10167   (unless (eq major-mode 'gnus-summary-mode)
10168     (error "This command can only be used in the summary buffer"))
10169   (gnus-summary-show-thread)
10170   (let ((nummove
10171          (cond
10172           ((eq move 'next) 1)
10173           ((eq move 'prev) -1)
10174           (t 0))))
10175     (if (zerop nummove)
10176         (setq n 1)
10177       (when (< n 0)
10178         (setq n (abs n)
10179               nummove (* -1 nummove))))
10180     (while (and (> n 0)
10181                 (gnus-summary-mark-article nil mark)
10182                 (zerop (gnus-summary-next-subject nummove unread t)))
10183       (setq n (1- n)))
10184     (when (/= 0 n)
10185       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10186     (gnus-summary-recenter)
10187     (gnus-summary-position-point)
10188     (gnus-set-mode-line 'summary)
10189     n))
10190
10191 (gnus-summary-make-all-marking-commands)
10192
10193 (gnus-ems-redefine)
10194
10195 (provide 'gnus-sum)
10196
10197 (run-hooks 'gnus-sum-load-hook)
10198
10199 ;;; gnus-sum.el ends here