Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If nil, don't select the first unread article when entering a group.
296 If this variable is `best', select the highest-scored unread article
297 in the group.  If t, select the first unread article.
298
299 This variable can also be a function to place point on a likely
300 subject line.  Useful values include `gnus-summary-first-unread-subject',
301 `gnus-summary-first-unread-article' and
302 `gnus-summary-best-unread-article'.
303
304 If you want to prevent automatic selection of the first unread article
305 in some newsgroups, set the variable to nil in
306 `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (const best)
310                  (sexp :menu-tag "first" t)
311                  (function-item gnus-summary-first-unread-subject)
312                  (function-item gnus-summary-first-unread-article)
313                  (function-item gnus-summary-best-unread-article)))
314
315 (defcustom gnus-dont-select-after-jump-to-other-group nil
316   "If non-nil, don't select the first unread article after entering the
317 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
318 it is depend on the value of `gnus-auto-select-first' whether to select
319 or not."
320   :group 'gnus-group-select
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `Z n' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-summary-check-current nil
349   "*If non-nil, consider the current article when moving.
350 The \"unread\" movement commands will stay on the same line if the
351 current article is unread."
352   :group 'gnus-summary-maneuvering
353   :type 'boolean)
354
355 (defcustom gnus-auto-center-summary t
356   "*If non-nil, always center the current summary buffer.
357 In particular, if `vertical' do only vertical recentering.  If non-nil
358 and non-`vertical', do both horizontal and vertical recentering."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "none" nil)
361                  (const vertical)
362                  (integer :tag "height")
363                  (sexp :menu-tag "both" t)))
364
365 (defcustom gnus-show-all-headers nil
366   "*If non-nil, don't hide any headers."
367   :group 'gnus-article-hiding
368   :group 'gnus-article-headers
369   :type 'boolean)
370
371 (defcustom gnus-summary-ignore-duplicates nil
372   "*If non-nil, ignore articles with identical Message-ID headers."
373   :group 'gnus-summary
374   :type 'boolean)
375
376 (defcustom gnus-single-article-buffer t
377   "*If non-nil, display all articles in the same buffer.
378 If nil, each group will get its own article buffer."
379   :group 'gnus-article-various
380   :type 'boolean)
381
382 (defcustom gnus-break-pages t
383   "*If non-nil, do page breaking on articles.
384 The page delimiter is specified by the `gnus-page-delimiter'
385 variable."
386   :group 'gnus-article-various
387   :type 'boolean)
388
389 (defcustom gnus-show-mime t
390   "*If non-nil, do mime processing of articles.
391 The articles will simply be fed to the function given by
392 `gnus-article-display-method-for-mime'."
393   :group 'gnus-article-mime
394   :type 'boolean)
395
396 (defcustom gnus-move-split-methods nil
397   "*Variable used to suggest where articles are to be moved to.
398 It uses the same syntax as the `gnus-split-methods' variable.
399 However, whereas `gnus-split-methods' specifies file names as targets,
400 this variable specifies group names."
401   :group 'gnus-summary-mail
402   :type '(repeat (choice (list :value (fun) function)
403                          (cons :value ("" "") regexp (repeat string))
404                          (sexp :value nil))))
405
406 (defcustom gnus-unread-mark ?  ;Whitespace
407   "*Mark used for unread articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-ticked-mark ?!
412   "*Mark used for ticked articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-dormant-mark ??
417   "*Mark used for dormant articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-del-mark ?r
422   "*Mark used for del'd articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-read-mark ?R
427   "*Mark used for read articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-expirable-mark ?E
432   "*Mark used for expirable articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-killed-mark ?K
437   "*Mark used for killed articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-souped-mark ?F
442   "*Mark used for souped articles."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-kill-file-mark ?X
447   "*Mark used for articles killed by kill files."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-low-score-mark ?Y
452   "*Mark used for articles with a low score."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-catchup-mark ?C
457   "*Mark used for articles that are caught up."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-replied-mark ?A
462   "*Mark used for articles that have been replied to."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-forwarded-mark ?O
467   "*Mark used for articles that have been forwarded."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-cached-mark ?*
472   "*Mark used for articles that are in the cache."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-saved-mark ?S
477   "*Mark used for articles that have been saved to."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-no-mark ?  ;Whitespace
482   "*Mark used for articles that have no other secondary mark."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-ancient-mark ?O
487   "*Mark used for ancient articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-sparse-mark ?Q
492   "*Mark used for sparsely reffed articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-canceled-mark ?G
497   "*Mark used for canceled articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-duplicate-mark ?M
502   "*Mark used for duplicate articles."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-undownloaded-mark ?@
507   "*Mark used for articles that weren't downloaded."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-downloadable-mark ?%
512   "*Mark used for articles that are to be downloaded."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-unsendable-mark ?=
517   "*Mark used for articles that won't be sent."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-score-over-mark ?+
522   "*Score mark used for articles with high scores."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-score-below-mark ?-
527   "*Score mark used for articles with low scores."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-empty-thread-mark ?  ;Whitespace
532   "*There is no thread under the article."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-not-empty-thread-mark ?=
537   "*There is a thread under the article."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-view-pseudo-asynchronously nil
542   "*If non-nil, Gnus will view pseudo-articles asynchronously."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-auto-expirable-marks
547   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
548         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
549         gnus-souped-mark gnus-duplicate-mark)
550   "*The list of marks converted into expiration if a group is auto-expirable."
551   :version "21.1"
552   :group 'gnus-summary
553   :type '(repeat character))
554
555 (defcustom gnus-inhibit-user-auto-expire t
556   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
557   :version "21.1"
558   :group 'gnus-summary
559   :type 'boolean)
560
561 (defcustom gnus-view-pseudos nil
562   "*If `automatic', pseudo-articles will be viewed automatically.
563 If `not-confirm', pseudos will be viewed automatically, and the user
564 will not be asked to confirm the command."
565   :group 'gnus-extract-view
566   :type '(choice (const :tag "off" nil)
567                  (const automatic)
568                  (const not-confirm)))
569
570 (defcustom gnus-view-pseudos-separately t
571   "*If non-nil, one pseudo-article will be created for each file to be viewed.
572 If nil, all files that use the same viewing command will be given as a
573 list of parameters to that command."
574   :group 'gnus-extract-view
575   :type 'boolean)
576
577 (defcustom gnus-insert-pseudo-articles t
578   "*If non-nil, insert pseudo-articles when decoding articles."
579   :group 'gnus-extract-view
580   :type 'boolean)
581
582 (defcustom gnus-summary-dummy-line-format
583   "  %(:                          :%) %S\n"
584   "*The format specification for the dummy roots in the summary buffer.
585 It works along the same lines as a normal formatting string,
586 with some simple extensions.
587
588 %S  The subject"
589   :group 'gnus-threading
590   :type 'string)
591
592 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
593   "*The format specification for the summary mode line.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions:
596
597 %G  Group name
598 %p  Unprefixed group name
599 %A  Current article number
600 %z  Current article score
601 %V  Gnus version
602 %U  Number of unread articles in the group
603 %e  Number of unselected articles in the group
604 %Z  A string with unread/unselected article counts
605 %g  Shortish group name
606 %S  Subject of the current article
607 %u  User-defined spec
608 %s  Current score file name
609 %d  Number of dormant articles
610 %r  Number of articles that have been marked as read in this session
611 %E  Number of articles expunged by the score files"
612   :group 'gnus-summary-format
613   :type 'string)
614
615 (defcustom gnus-list-identifiers nil
616   "Regexp that matches list identifiers to be removed from subject.
617 This can also be a list of regexps."
618   :version "21.1"
619   :group 'gnus-summary-format
620   :group 'gnus-article-hiding
621   :type '(choice (const :tag "none" nil)
622                  (regexp :value ".*")
623                  (repeat :value (".*") regexp)))
624
625 (defcustom gnus-summary-mark-below 0
626   "*Mark all articles with a score below this variable as read.
627 This variable is local to each summary buffer and usually set by the
628 score file."
629   :group 'gnus-score-default
630   :type 'integer)
631
632 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
633   "*List of functions used for sorting articles in the summary buffer.
634
635 Each function takes two articles and returns non-nil if the first
636 article should be sorted before the other.  If you use more than one
637 function, the primary sort function should be the last.  You should
638 probably always include `gnus-article-sort-by-number' in the list of
639 sorting functions -- preferably first.  Also note that sorting by date
640 is often much slower than sorting by number, and the sorting order is
641 very similar.  (Sorting by date means sorting by the time the message
642 was sent, sorting by number means sorting by arrival time.)
643
644 Ready-made functions include `gnus-article-sort-by-number',
645 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
646 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
647
648 When threading is turned on, the variable `gnus-thread-sort-functions'
649 controls how articles are sorted."
650   :group 'gnus-summary-sort
651   :type '(repeat (choice (function-item gnus-article-sort-by-number)
652                          (function-item gnus-article-sort-by-author)
653                          (function-item gnus-article-sort-by-subject)
654                          (function-item gnus-article-sort-by-date)
655                          (function-item gnus-article-sort-by-score)
656                          (function :tag "other"))))
657
658 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
659   "*List of functions used for sorting threads in the summary buffer.
660 By default, threads are sorted by article number.
661
662 Each function takes two threads and returns non-nil if the first
663 thread should be sorted before the other.  If you use more than one
664 function, the primary sort function should be the last.  You should
665 probably always include `gnus-thread-sort-by-number' in the list of
666 sorting functions -- preferably first.  Also note that sorting by date
667 is often much slower than sorting by number, and the sorting order is
668 very similar.  (Sorting by date means sorting by the time the message
669 was sent, sorting by number means sorting by arrival time.)
670
671 Ready-made functions include `gnus-thread-sort-by-number',
672 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
673 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
674 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
675
676 When threading is turned off, the variable
677 `gnus-article-sort-functions' controls how articles are sorted."
678   :group 'gnus-summary-sort
679   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
680                          (function-item gnus-thread-sort-by-author)
681                          (function-item gnus-thread-sort-by-subject)
682                          (function-item gnus-thread-sort-by-date)
683                          (function-item gnus-thread-sort-by-score)
684                          (function-item gnus-thread-sort-by-total-score)
685                          (function :tag "other"))))
686
687 (defcustom gnus-thread-score-function '+
688   "*Function used for calculating the total score of a thread.
689
690 The function is called with the scores of the article and each
691 subthread and should then return the score of the thread.
692
693 Some functions you can use are `+', `max', or `min'."
694   :group 'gnus-summary-sort
695   :type 'function)
696
697 (defcustom gnus-summary-expunge-below nil
698   "All articles that have a score less than this variable will be expunged.
699 This variable is local to the summary buffers."
700   :group 'gnus-score-default
701   :type '(choice (const :tag "off" nil)
702                  integer))
703
704 (defcustom gnus-thread-expunge-below nil
705   "All threads that have a total score less than this variable will be expunged.
706 See `gnus-thread-score-function' for en explanation of what a
707 \"thread score\" is.
708
709 This variable is local to the summary buffers."
710   :group 'gnus-threading
711   :group 'gnus-score-default
712   :type '(choice (const :tag "off" nil)
713                  integer))
714
715 (defcustom gnus-summary-mode-hook nil
716   "*A hook for Gnus summary mode.
717 This hook is run before any variables are set in the summary buffer."
718   :options '(turn-on-gnus-mailing-list-mode)
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
723 (when (featurep 'xemacs)
724   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
725   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
726   (add-hook 'gnus-summary-mode-hook
727             'gnus-xmas-switch-horizontal-scrollbar-off))
728
729 (defcustom gnus-summary-menu-hook nil
730   "*Hook run after the creation of the summary mode menu."
731   :group 'gnus-summary-visual
732   :type 'hook)
733
734 (defcustom gnus-summary-exit-hook nil
735   "*A hook called on exit from the summary buffer.
736 It will be called with point in the group buffer."
737   :group 'gnus-summary-exit
738   :type 'hook)
739
740 (defcustom gnus-summary-prepare-hook nil
741   "*A hook called after the summary buffer has been generated.
742 If you want to modify the summary buffer, you can use this hook."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-prepared-hook nil
747   "*A hook called as the last thing after the summary buffer has been generated."
748   :group 'gnus-summary-various
749   :type 'hook)
750
751 (defcustom gnus-summary-generate-hook nil
752   "*A hook run just before generating the summary buffer.
753 This hook is commonly used to customize threading variables and the
754 like."
755   :group 'gnus-summary-various
756   :type 'hook)
757
758 (defcustom gnus-select-group-hook nil
759   "*A hook called when a newsgroup is selected.
760
761 If you'd like to simplify subjects like the
762 `gnus-summary-next-same-subject' command does, you can use the
763 following hook:
764
765  (setq gnus-select-group-hook
766       (list
767         (lambda ()
768           (mapcar (lambda (header)
769                      (mail-header-set-subject
770                       header
771                       (gnus-simplify-subject
772                        (mail-header-subject header) 're-only)))
773                   gnus-newsgroup-headers))))"
774   :group 'gnus-group-select
775   :type 'hook)
776
777 (defcustom gnus-select-article-hook nil
778   "*A hook called when an article is selected."
779   :group 'gnus-summary-choose
780   :type 'hook)
781
782 (defcustom gnus-visual-mark-article-hook
783   (list 'gnus-highlight-selected-summary)
784   "*Hook run after selecting an article in the summary buffer.
785 It is meant to be used for highlighting the article in some way.  It
786 is not run if `gnus-visual' is nil."
787   :group 'gnus-summary-visual
788   :type 'hook)
789
790 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
791   "*A hook called before parsing the headers."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-exit-group-hook nil
796   "*A hook called when exiting summary mode.
797 This hook is not called from the non-updating exit commands like `Q'."
798   :group 'gnus-various
799   :type 'hook)
800
801 (defcustom gnus-summary-update-hook
802   (list 'gnus-summary-highlight-line)
803   "*A hook called when a summary line is changed.
804 The hook will not be called if `gnus-visual' is nil.
805
806 The default function `gnus-summary-highlight-line' will
807 highlight the line according to the `gnus-summary-highlight'
808 variable."
809   :group 'gnus-summary-visual
810   :type 'hook)
811
812 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
813   "*A hook called when an article is selected for the first time.
814 The hook is intended to mark an article as read (or unread)
815 automatically when it is selected."
816   :group 'gnus-summary-choose
817   :type 'hook)
818
819 (defcustom gnus-group-no-more-groups-hook nil
820   "*A hook run when returning to group mode having no more (unread) groups."
821   :group 'gnus-group-select
822   :type 'hook)
823
824 (defcustom gnus-ps-print-hook nil
825   "*A hook run before ps-printing something from Gnus."
826   :group 'gnus-summary
827   :type 'hook)
828
829 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
830   "Face used for highlighting the current article in the summary buffer."
831   :group 'gnus-summary-visual
832   :type 'face)
833
834 (defcustom gnus-summary-highlight
835   '(((= mark gnus-canceled-mark)
836      . gnus-summary-cancelled-face)
837     ((and (> score default-high)
838           (or (= mark gnus-dormant-mark)
839               (= mark gnus-ticked-mark)))
840      . gnus-summary-high-ticked-face)
841     ((and (< score default-low)
842           (or (= mark gnus-dormant-mark)
843               (= mark gnus-ticked-mark)))
844      . gnus-summary-low-ticked-face)
845     ((or (= mark gnus-dormant-mark)
846          (= mark gnus-ticked-mark))
847      . gnus-summary-normal-ticked-face)
848     ((and (> score default-high) (= mark gnus-ancient-mark))
849      . gnus-summary-high-ancient-face)
850     ((and (< score default-low) (= mark gnus-ancient-mark))
851      . gnus-summary-low-ancient-face)
852     ((= mark gnus-ancient-mark)
853      . gnus-summary-normal-ancient-face)
854     ((and (> score default-high) (= mark gnus-unread-mark))
855      . gnus-summary-high-unread-face)
856     ((and (< score default-low) (= mark gnus-unread-mark))
857      . gnus-summary-low-unread-face)
858     ((= mark gnus-unread-mark)
859      . gnus-summary-normal-unread-face)
860     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
861                                              gnus-undownloaded-mark)))
862      . gnus-summary-high-unread-face)
863     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
864                                              gnus-undownloaded-mark)))
865      . gnus-summary-low-unread-face)
866     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
867           (memq article gnus-newsgroup-unreads))
868      . gnus-summary-normal-unread-face)
869     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
870      . gnus-summary-normal-read-face)
871     ((> score default-high)
872      . gnus-summary-high-read-face)
873     ((< score default-low)
874      . gnus-summary-low-read-face)
875     (t
876      . gnus-summary-normal-read-face))
877   "*Controls the highlighting of summary buffer lines.
878
879 A list of (FORM . FACE) pairs.  When deciding how a a particular
880 summary line should be displayed, each form is evaluated.  The content
881 of the face field after the first true form is used.  You can change
882 how those summary lines are displayed, by editing the face field.
883
884 You can use the following variables in the FORM field.
885
886 score:        The article's score
887 default:      The default article score.
888 default-high: The default score for high scored articles.
889 default-low:  The default score for low scored articles.
890 below:        The score below which articles are automatically marked as read.
891 mark:         The articles mark."
892   :group 'gnus-summary-visual
893   :type '(repeat (cons (sexp :tag "Form" nil)
894                        face)))
895
896 (defcustom gnus-alter-header-function nil
897   "Function called to allow alteration of article header structures.
898 The function is called with one parameter, the article header vector,
899 which it may alter in any way.")
900
901 (defvar gnus-decode-encoded-word-function
902   (mime-find-field-decoder 'From 'nov)
903   "Variable that says which function should be used to decode a string with encoded words.")
904
905 (defcustom gnus-extra-headers nil
906   "*Extra headers to parse."
907   :version "21.1"
908   :group 'gnus-summary
909   :type '(repeat symbol))
910
911 (defcustom gnus-ignored-from-addresses
912   (and user-mail-address (regexp-quote user-mail-address))
913   "*Regexp of From headers that may be suppressed in favor of To headers."
914   :version "21.1"
915   :group 'gnus-summary
916   :type 'regexp)
917
918 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
919   "List of charsets that should be ignored.
920 When these charsets are used in the \"charset\" parameter, the
921 default charset will be used instead."
922   :version "21.1"
923   :type '(repeat symbol)
924   :group 'gnus-charset)
925
926 (gnus-define-group-parameter
927  ignored-charsets
928  :type list
929  :function-document
930  "Return the ignored charsets of GROUP."
931  :variable gnus-group-ignored-charsets-alist
932  :variable-default 
933  '(("alt\\.chinese\\.text" iso-8859-1))
934  :variable-document
935  "Alist of regexps (to match group names) and charsets that should be ignored.
936 When these charsets are used in the \"charset\" parameter, the
937 default charset will be used instead."
938  :variable-group gnus-charset
939  :variable-type '(repeat (cons (regexp :tag "Group")
940                                (repeat symbol)))
941  :parameter-type '(choice :tag "Ignored charsets" 
942                           :value nil
943                           (repeat (symbol)))
944  :parameter-document       "\
945 List of charsets that should be ignored.
946
947 When these charsets are used in the \"charset\" parameter, the
948 default charset will be used instead.")
949
950 (defcustom gnus-group-highlight-words-alist nil
951   "Alist of group regexps and highlight regexps.
952 This variable uses the same syntax as `gnus-emphasis-alist'."
953   :version "21.1"
954   :type '(repeat (cons (regexp :tag "Group")
955                        (repeat (list (regexp :tag "Highlight regexp")
956                                      (number :tag "Group for entire word" 0)
957                                      (number :tag "Group for displayed part" 0)
958                                      (symbol :tag "Face"
959                                              gnus-emphasis-highlight-words)))))
960   :group 'gnus-summary-visual)
961
962 (defcustom gnus-use-wheel nil
963   "Use Intelli-mouse on summary movement"
964   :type 'boolean
965   :group 'gnus-summary-maneuvering)
966
967 (defcustom gnus-wheel-scroll-amount '(5 . 1)
968   "Amount to scroll messages by spinning the mouse wheel.
969 This is actually a cons cell, where the first item is the amount to scroll
970 on a normal wheel event, and the second is the amount to scroll when the
971 wheel is moved with the shift key depressed."
972   :type '(cons (integer :tag "Shift") integer)
973   :group 'gnus-summary-maneuvering)
974
975 (defcustom gnus-wheel-edge-resistance 2
976   "How hard it should be to change the current article
977 by moving the mouse over the edge of the article window."
978   :type 'integer
979   :group 'gnus-summary-maneuvering)
980
981 (defcustom gnus-summary-show-article-charset-alist
982   nil
983   "Alist of number and charset.
984 The article will be shown with the charset corresponding to the
985 numbered argument.
986 For example: ((1 . cn-gb-2312) (2 . big5))."
987   :version "21.1"
988   :type '(repeat (cons (number :tag "Argument" 1)
989                        (symbol :tag "Charset")))
990   :group 'gnus-charset)
991
992 (defcustom gnus-preserve-marks t
993   "Whether marks are preserved when moving, copying and respooling messages."
994   :version "21.1"
995   :type 'boolean
996   :group 'gnus-summary-marks)
997
998 (defcustom gnus-alter-articles-to-read-function nil
999   "Function to be called to alter the list of articles to be selected."
1000   :type '(choice (const nil) function)
1001   :group 'gnus-summary)
1002
1003 (defcustom gnus-orphan-score nil
1004   "*All orphans get this score added.  Set in the score file."
1005   :group 'gnus-score-default
1006   :type '(choice (const nil)
1007                  integer))
1008
1009 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1010   "*A regexp to match MIME parts when saving multiple parts of a message
1011 with gnus-summary-save-parts (X m). This regexp will be used by default
1012 when prompting the user for which type of files to save."
1013   :group 'gnus-summary
1014   :type 'regexp)
1015
1016
1017 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1018   "*A regexp to match MIME parts when saving multiple parts of a message
1019 with gnus-summary-save-parts (X m). This regexp will be used by default
1020 when prompting the user for which type of files to save."
1021   :group 'gnus-summary
1022   :type 'regexp)
1023
1024 (defcustom gnus-read-all-available-headers nil
1025   "Whether Gnus should parse all headers made available to it.
1026 This is mostly relevant for slow backends where the user may
1027 wish to widen the summary buffer to include all headers
1028 that were fetched.  Say, for nnultimate groups."
1029   :group 'gnus-summary
1030   :type '(choice boolean regexp))
1031
1032 ;;; Internal variables
1033
1034 (defvar gnus-article-mime-handles nil)
1035 (defvar gnus-article-decoded-p nil)
1036 (defvar gnus-article-charset nil)
1037 (defvar gnus-article-ignored-charsets nil)
1038 (defvar gnus-scores-exclude-files nil)
1039 (defvar gnus-page-broken nil)
1040 (defvar gnus-inhibit-mime-unbuttonizing nil)
1041
1042 (defvar gnus-original-article nil)
1043 (defvar gnus-article-internal-prepare-hook nil)
1044 (defvar gnus-newsgroup-process-stack nil)
1045
1046 (defvar gnus-thread-indent-array nil)
1047 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1048 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1049   "Function called to sort the articles within a thread after it has been gathered together.")
1050
1051 (defvar gnus-summary-save-parts-type-history nil)
1052 (defvar gnus-summary-save-parts-last-directory nil)
1053
1054 (defvar gnus-summary-save-parts-type-history nil)
1055 (defvar gnus-summary-save-parts-last-directory nil)
1056
1057 ;; Avoid highlighting in kill files.
1058 (defvar gnus-summary-inhibit-highlight nil)
1059 (defvar gnus-newsgroup-selected-overlay nil)
1060 (defvar gnus-inhibit-limiting nil)
1061 (defvar gnus-newsgroup-adaptive-score-file nil)
1062 (defvar gnus-current-score-file nil)
1063 (defvar gnus-current-move-group nil)
1064 (defvar gnus-current-copy-group nil)
1065 (defvar gnus-current-crosspost-group nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (std11-address-string
1079          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1080     (?a (or (std11-full-name-string
1081              (car (mime-entity-read-field gnus-tmp-header 'From)))
1082             gnus-tmp-from) ?s)
1083     (?F gnus-tmp-from ?s)
1084     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1085     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1086     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1087     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1088     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1089     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1090     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1091     (?L gnus-tmp-lines ?s)
1092     (?I gnus-tmp-indentation ?s)
1093     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1094     (?R gnus-tmp-replied ?c)
1095     (?\[ gnus-tmp-opening-bracket ?c)
1096     (?\] gnus-tmp-closing-bracket ?c)
1097     (?\> (make-string gnus-tmp-level ? ) ?s)
1098     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1099     (?i gnus-tmp-score ?d)
1100     (?z gnus-tmp-score-char ?c)
1101     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1102     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1103     (?U gnus-tmp-unread ?c)
1104     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d))
1113   "An alist of format specifications that can appear in summary lines.
1114 These are paired with what variables they correspond with, along with
1115 the type of the variable (string, integer, character, etc).")
1116
1117 (defvar gnus-summary-dummy-line-format-alist
1118   `((?S gnus-tmp-subject ?s)
1119     (?N gnus-tmp-number ?d)
1120     (?u gnus-tmp-user-defined ?s)))
1121
1122 (defvar gnus-summary-mode-line-format-alist
1123   `((?G gnus-tmp-group-name ?s)
1124     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1125     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1126     (?A gnus-tmp-article-number ?d)
1127     (?Z gnus-tmp-unread-and-unselected ?s)
1128     (?V gnus-version ?s)
1129     (?U gnus-tmp-unread-and-unticked ?d)
1130     (?S gnus-tmp-subject ?s)
1131     (?e gnus-tmp-unselected ?d)
1132     (?u gnus-tmp-user-defined ?s)
1133     (?d (length gnus-newsgroup-dormant) ?d)
1134     (?t (length gnus-newsgroup-marked) ?d)
1135     (?r (length gnus-newsgroup-reads) ?d)
1136     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1137     (?E gnus-newsgroup-expunged-tally ?d)
1138     (?s (gnus-current-score-file-nondirectory) ?s)))
1139
1140 (defvar gnus-last-search-regexp nil
1141   "Default regexp for article search command.")
1142
1143 (defvar gnus-summary-search-article-matched-data nil
1144   "Last matched data of article search command.  It is the local variable
1145 in `gnus-article-buffer' which consists of the list of start position,
1146 end position and text.")
1147
1148 (defvar gnus-last-shell-command nil
1149   "Default shell command on article.")
1150
1151 (defvar gnus-newsgroup-begin nil)
1152 (defvar gnus-newsgroup-end nil)
1153 (defvar gnus-newsgroup-last-rmail nil)
1154 (defvar gnus-newsgroup-last-mail nil)
1155 (defvar gnus-newsgroup-last-folder nil)
1156 (defvar gnus-newsgroup-last-file nil)
1157 (defvar gnus-newsgroup-auto-expire nil)
1158 (defvar gnus-newsgroup-active nil)
1159
1160 (defvar gnus-newsgroup-data nil)
1161 (defvar gnus-newsgroup-data-reverse nil)
1162 (defvar gnus-newsgroup-limit nil)
1163 (defvar gnus-newsgroup-limits nil)
1164
1165 (defvar gnus-newsgroup-unreads nil
1166   "List of unread articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-unselected nil
1169   "List of unselected unread articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-reads nil
1172   "Alist of read articles and article marks in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-expunged-tally nil)
1175
1176 (defvar gnus-newsgroup-marked nil
1177   "List of ticked articles in the current newsgroup (a subset of unread art).")
1178
1179 (defvar gnus-newsgroup-killed nil
1180   "List of ranges of articles that have been through the scoring process.")
1181
1182 (defvar gnus-newsgroup-cached nil
1183   "List of articles that come from the article cache.")
1184
1185 (defvar gnus-newsgroup-saved nil
1186   "List of articles that have been saved.")
1187
1188 (defvar gnus-newsgroup-kill-headers nil)
1189
1190 (defvar gnus-newsgroup-replied nil
1191   "List of articles that have been replied to in the current newsgroup.")
1192
1193 (defvar gnus-newsgroup-forwarded nil
1194   "List of articles that have been forwarded in the current newsgroup.")
1195
1196 (defvar gnus-newsgroup-expirable nil
1197   "List of articles in the current newsgroup that can be expired.")
1198
1199 (defvar gnus-newsgroup-processable nil
1200   "List of articles in the current newsgroup that can be processed.")
1201
1202 (defvar gnus-newsgroup-downloadable nil
1203   "List of articles in the current newsgroup that can be processed.")
1204
1205 (defvar gnus-newsgroup-undownloaded nil
1206   "List of articles in the current newsgroup that haven't been downloaded..")
1207
1208 (defvar gnus-newsgroup-unsendable nil
1209   "List of articles in the current newsgroup that won't be sent.")
1210
1211 (defvar gnus-newsgroup-bookmarks nil
1212   "List of articles in the current newsgroup that have bookmarks.")
1213
1214 (defvar gnus-newsgroup-dormant nil
1215   "List of dormant articles in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-scored nil
1218   "List of scored articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-incorporated nil
1221   "List of incorporated articles in the current newsgroup.")
1222
1223 (defvar gnus-newsgroup-headers nil
1224   "List of article headers in the current newsgroup.")
1225
1226 (defvar gnus-newsgroup-threads nil)
1227
1228 (defvar gnus-newsgroup-prepared nil
1229   "Whether the current group has been prepared properly.")
1230
1231 (defvar gnus-newsgroup-ancient nil
1232   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-sparse nil)
1235
1236 (defvar gnus-current-article nil)
1237 (defvar gnus-article-current nil)
1238 (defvar gnus-current-headers nil)
1239 (defvar gnus-have-all-headers nil)
1240 (defvar gnus-last-article nil)
1241 (defvar gnus-newsgroup-history nil)
1242 (defvar gnus-newsgroup-charset nil)
1243 (defvar gnus-newsgroup-ephemeral-charset nil)
1244 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1245
1246 (defvar gnus-article-before-search nil)
1247
1248 (defconst gnus-summary-local-variables
1249   '(gnus-newsgroup-name
1250     gnus-newsgroup-begin gnus-newsgroup-end
1251     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1252     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1253     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1254     gnus-newsgroup-unselected gnus-newsgroup-marked
1255     gnus-newsgroup-reads gnus-newsgroup-saved
1256     gnus-newsgroup-replied gnus-newsgroup-forwarded
1257     gnus-newsgroup-expirable
1258     gnus-newsgroup-processable gnus-newsgroup-killed
1259     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1260     gnus-newsgroup-unsendable
1261     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1262     gnus-newsgroup-headers gnus-newsgroup-threads
1263     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1264     gnus-current-article gnus-current-headers gnus-have-all-headers
1265     gnus-last-article gnus-article-internal-prepare-hook
1266     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1267     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1268     gnus-thread-expunge-below
1269     gnus-score-alist gnus-current-score-file
1270     (gnus-summary-expunge-below . global)
1271     (gnus-summary-mark-below . global)
1272     (gnus-orphan-score . global)
1273     gnus-newsgroup-active gnus-scores-exclude-files
1274     gnus-newsgroup-history gnus-newsgroup-ancient
1275     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1276     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1277     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1278     (gnus-newsgroup-expunged-tally . 0)
1279     gnus-cache-removable-articles gnus-newsgroup-cached
1280     gnus-newsgroup-data gnus-newsgroup-data-reverse
1281     gnus-newsgroup-limit gnus-newsgroup-limits
1282     gnus-newsgroup-charset
1283     gnus-newsgroup-incorporated)
1284   "Variables that are buffer-local to the summary buffers.")
1285
1286 (defvar gnus-newsgroup-variables nil
1287   "Variables that have separate values in the newsgroups.")
1288
1289 ;; Byte-compiler warning.
1290 (eval-when-compile (defvar gnus-article-mode-map))
1291
1292 ;; Subject simplification.
1293
1294 (defun gnus-simplify-whitespace (str)
1295   "Remove excessive whitespace from STR."
1296   (let ((mystr str))
1297     ;; Multiple spaces.
1298     (while (string-match "[ \t][ \t]+" mystr)
1299       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1300                           " "
1301                           (substring mystr (match-end 0)))))
1302     ;; Leading spaces.
1303     (when (string-match "^[ \t]+" mystr)
1304       (setq mystr (substring mystr (match-end 0))))
1305     ;; Trailing spaces.
1306     (when (string-match "[ \t]+$" mystr)
1307       (setq mystr (substring mystr 0 (match-beginning 0))))
1308     mystr))
1309
1310 (defsubst gnus-simplify-subject-re (subject)
1311   "Remove \"Re:\" from subject lines."
1312   (if (string-match message-subject-re-regexp subject)
1313       (substring subject (match-end 0))
1314     subject))
1315
1316 (defun gnus-simplify-subject (subject &optional re-only)
1317   "Remove `Re:' and words in parentheses.
1318 If RE-ONLY is non-nil, strip leading `Re:'s only."
1319   (let ((case-fold-search t))           ;Ignore case.
1320     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1321     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1322       (setq subject (substring subject (match-end 0))))
1323     ;; Remove uninteresting prefixes.
1324     (when (and (not re-only)
1325                gnus-simplify-ignored-prefixes
1326                (string-match gnus-simplify-ignored-prefixes subject))
1327       (setq subject (substring subject (match-end 0))))
1328     ;; Remove words in parentheses from end.
1329     (unless re-only
1330       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1331         (setq subject (substring subject 0 (match-beginning 0)))))
1332     ;; Return subject string.
1333     subject))
1334
1335 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1336 ;; all whitespace.
1337 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1338   (goto-char (point-min))
1339   (while (re-search-forward regexp nil t)
1340     (replace-match (or newtext ""))))
1341
1342 (defun gnus-simplify-buffer-fuzzy ()
1343   "Simplify string in the buffer fuzzily.
1344 The string in the accessible portion of the current buffer is simplified.
1345 It is assumed to be a single-line subject.
1346 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1347 matter is removed.  Additional things can be deleted by setting
1348 `gnus-simplify-subject-fuzzy-regexp'."
1349   (let ((case-fold-search t)
1350         (modified-tick))
1351     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1352
1353     (while (not (eq modified-tick (buffer-modified-tick)))
1354       (setq modified-tick (buffer-modified-tick))
1355       (cond
1356        ((listp gnus-simplify-subject-fuzzy-regexp)
1357         (mapcar 'gnus-simplify-buffer-fuzzy-step
1358                 gnus-simplify-subject-fuzzy-regexp))
1359        (gnus-simplify-subject-fuzzy-regexp
1360         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1361       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1362       (gnus-simplify-buffer-fuzzy-step
1363        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1364       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1365
1366     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1367     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1368     (gnus-simplify-buffer-fuzzy-step " $")
1369     (gnus-simplify-buffer-fuzzy-step "^ +")))
1370
1371 (defun gnus-simplify-subject-fuzzy (subject)
1372   "Simplify a subject string fuzzily.
1373 See `gnus-simplify-buffer-fuzzy' for details."
1374   (save-excursion
1375     (gnus-set-work-buffer)
1376     (let ((case-fold-search t))
1377       ;; Remove uninteresting prefixes.
1378       (when (and gnus-simplify-ignored-prefixes
1379                  (string-match gnus-simplify-ignored-prefixes subject))
1380         (setq subject (substring subject (match-end 0))))
1381       (insert subject)
1382       (inline (gnus-simplify-buffer-fuzzy))
1383       (buffer-string))))
1384
1385 (defsubst gnus-simplify-subject-fully (subject)
1386   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1387   (cond
1388    (gnus-simplify-subject-functions
1389     (gnus-map-function gnus-simplify-subject-functions subject))
1390    ((null gnus-summary-gather-subject-limit)
1391     (gnus-simplify-subject-re subject))
1392    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1393     (gnus-simplify-subject-fuzzy subject))
1394    ((numberp gnus-summary-gather-subject-limit)
1395     (gnus-limit-string (gnus-simplify-subject-re subject)
1396                        gnus-summary-gather-subject-limit))
1397    (t
1398     subject)))
1399
1400 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1401   "Check whether two subjects are equal.
1402 If optional argument simple-first is t, first argument is already
1403 simplified."
1404   (cond
1405    ((null simple-first)
1406     (equal (gnus-simplify-subject-fully s1)
1407            (gnus-simplify-subject-fully s2)))
1408    (t
1409     (equal s1
1410            (gnus-simplify-subject-fully s2)))))
1411
1412 (defun gnus-summary-bubble-group ()
1413   "Increase the score of the current group.
1414 This is a handy function to add to `gnus-summary-exit-hook' to
1415 increase the score of each group you read."
1416   (gnus-group-add-score gnus-newsgroup-name))
1417
1418 \f
1419 ;;;
1420 ;;; Gnus summary mode
1421 ;;;
1422
1423 (put 'gnus-summary-mode 'mode-class 'special)
1424
1425 (defvar gnus-article-commands-menu)
1426
1427 (when t
1428   ;; Non-orthogonal keys
1429
1430   (gnus-define-keys gnus-summary-mode-map
1431     " " gnus-summary-next-page
1432     "\177" gnus-summary-prev-page
1433     [delete] gnus-summary-prev-page
1434     [backspace] gnus-summary-prev-page
1435     "\r" gnus-summary-scroll-up
1436     "\M-\r" gnus-summary-scroll-down
1437     "n" gnus-summary-next-unread-article
1438     "p" gnus-summary-prev-unread-article
1439     "N" gnus-summary-next-article
1440     "P" gnus-summary-prev-article
1441     "\M-\C-n" gnus-summary-next-same-subject
1442     "\M-\C-p" gnus-summary-prev-same-subject
1443     "\M-n" gnus-summary-next-unread-subject
1444     "\M-p" gnus-summary-prev-unread-subject
1445     "." gnus-summary-first-unread-article
1446     "," gnus-summary-best-unread-article
1447     "\M-s" gnus-summary-search-article-forward
1448     "\M-r" gnus-summary-search-article-backward
1449     "<" gnus-summary-beginning-of-article
1450     ">" gnus-summary-end-of-article
1451     "j" gnus-summary-goto-article
1452     "^" gnus-summary-refer-parent-article
1453     "\M-^" gnus-summary-refer-article
1454     "u" gnus-summary-tick-article-forward
1455     "!" gnus-summary-tick-article-forward
1456     "U" gnus-summary-tick-article-backward
1457     "d" gnus-summary-mark-as-read-forward
1458     "D" gnus-summary-mark-as-read-backward
1459     "E" gnus-summary-mark-as-expirable
1460     "\M-u" gnus-summary-clear-mark-forward
1461     "\M-U" gnus-summary-clear-mark-backward
1462     "k" gnus-summary-kill-same-subject-and-select
1463     "\C-k" gnus-summary-kill-same-subject
1464     "\M-\C-k" gnus-summary-kill-thread
1465     "\M-\C-l" gnus-summary-lower-thread
1466     "e" gnus-summary-edit-article
1467     "#" gnus-summary-mark-as-processable
1468     "\M-#" gnus-summary-unmark-as-processable
1469     "\M-\C-t" gnus-summary-toggle-threads
1470     "\M-\C-s" gnus-summary-show-thread
1471     "\M-\C-h" gnus-summary-hide-thread
1472     "\M-\C-f" gnus-summary-next-thread
1473     "\M-\C-b" gnus-summary-prev-thread
1474     [(meta down)] gnus-summary-next-thread
1475     [(meta up)] gnus-summary-prev-thread
1476     "\M-\C-u" gnus-summary-up-thread
1477     "\M-\C-d" gnus-summary-down-thread
1478     "&" gnus-summary-execute-command
1479     "c" gnus-summary-catchup-and-exit
1480     "\C-w" gnus-summary-mark-region-as-read
1481     "\C-t" gnus-summary-toggle-truncation
1482     "?" gnus-summary-mark-as-dormant
1483     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1484     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1485     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1486     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1487     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1488     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1489     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1490     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1491     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1492     "=" gnus-summary-expand-window
1493     "\C-x\C-s" gnus-summary-reselect-current-group
1494     "\M-g" gnus-summary-rescan-group
1495     "w" gnus-summary-stop-page-breaking
1496     "\C-c\C-r" gnus-summary-caesar-message
1497     "\M-t" gnus-summary-toggle-mime
1498     "f" gnus-summary-followup
1499     "F" gnus-summary-followup-with-original
1500     "C" gnus-summary-cancel-article
1501     "r" gnus-summary-reply
1502     "R" gnus-summary-reply-with-original
1503     "\C-c\C-f" gnus-summary-mail-forward
1504     "o" gnus-summary-save-article
1505     "\C-o" gnus-summary-save-article-mail
1506     "|" gnus-summary-pipe-output
1507     "\M-k" gnus-summary-edit-local-kill
1508     "\M-K" gnus-summary-edit-global-kill
1509     ;; "V" gnus-version
1510     "\C-c\C-d" gnus-summary-describe-group
1511     "q" gnus-summary-exit
1512     "Q" gnus-summary-exit-no-update
1513     "\C-c\C-i" gnus-info-find-node
1514     gnus-mouse-2 gnus-mouse-pick-article
1515     "m" gnus-summary-mail-other-window
1516     "a" gnus-summary-post-news
1517     "x" gnus-summary-limit-to-unread
1518     "s" gnus-summary-isearch-article
1519     "t" gnus-article-toggle-headers
1520     "g" gnus-summary-show-article
1521     "l" gnus-summary-goto-last-article
1522     "v" gnus-summary-preview-mime-message
1523     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1524     "\C-d" gnus-summary-enter-digest-group
1525     "\M-\C-d" gnus-summary-read-document
1526     "\M-\C-e" gnus-summary-edit-parameters
1527     "\M-\C-a" gnus-summary-customize-parameters
1528     "\C-c\C-b" gnus-bug
1529     "*" gnus-cache-enter-article
1530     "\M-*" gnus-cache-remove-article
1531     "\M-&" gnus-summary-universal-argument
1532     "\C-l" gnus-recenter
1533     "I" gnus-summary-increase-score
1534     "L" gnus-summary-lower-score
1535     "\M-i" gnus-symbolic-argument
1536     "h" gnus-summary-select-article-buffer
1537
1538     "V" gnus-summary-score-map
1539     "X" gnus-uu-extract-map
1540     "S" gnus-summary-send-map)
1541
1542   ;; Sort of orthogonal keymap
1543   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1544     "t" gnus-summary-tick-article-forward
1545     "!" gnus-summary-tick-article-forward
1546     "d" gnus-summary-mark-as-read-forward
1547     "r" gnus-summary-mark-as-read-forward
1548     "c" gnus-summary-clear-mark-forward
1549     " " gnus-summary-clear-mark-forward
1550     "e" gnus-summary-mark-as-expirable
1551     "x" gnus-summary-mark-as-expirable
1552     "?" gnus-summary-mark-as-dormant
1553     "b" gnus-summary-set-bookmark
1554     "B" gnus-summary-remove-bookmark
1555     "#" gnus-summary-mark-as-processable
1556     "\M-#" gnus-summary-unmark-as-processable
1557     "S" gnus-summary-limit-include-expunged
1558     "C" gnus-summary-catchup
1559     "H" gnus-summary-catchup-to-here
1560     "h" gnus-summary-catchup-from-here
1561     "\C-c" gnus-summary-catchup-all
1562     "k" gnus-summary-kill-same-subject-and-select
1563     "K" gnus-summary-kill-same-subject
1564     "P" gnus-uu-mark-map)
1565
1566   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1567     "c" gnus-summary-clear-above
1568     "u" gnus-summary-tick-above
1569     "m" gnus-summary-mark-above
1570     "k" gnus-summary-kill-below)
1571
1572   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1573     "/" gnus-summary-limit-to-subject
1574     "n" gnus-summary-limit-to-articles
1575     "w" gnus-summary-pop-limit
1576     "s" gnus-summary-limit-to-subject
1577     "a" gnus-summary-limit-to-author
1578     "u" gnus-summary-limit-to-unread
1579     "m" gnus-summary-limit-to-marks
1580     "M" gnus-summary-limit-exclude-marks
1581     "v" gnus-summary-limit-to-score
1582     "*" gnus-summary-limit-include-cached
1583     "D" gnus-summary-limit-include-dormant
1584     "T" gnus-summary-limit-include-thread
1585     "d" gnus-summary-limit-exclude-dormant
1586     "t" gnus-summary-limit-to-age
1587     "x" gnus-summary-limit-to-extra
1588     "E" gnus-summary-limit-include-expunged
1589     "c" gnus-summary-limit-exclude-childless-dormant
1590     "C" gnus-summary-limit-mark-excluded-as-read
1591     "o" gnus-summary-insert-old-articles
1592     "N" gnus-summary-insert-new-articles)
1593
1594   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1595     "n" gnus-summary-next-unread-article
1596     "p" gnus-summary-prev-unread-article
1597     "N" gnus-summary-next-article
1598     "P" gnus-summary-prev-article
1599     "\C-n" gnus-summary-next-same-subject
1600     "\C-p" gnus-summary-prev-same-subject
1601     "\M-n" gnus-summary-next-unread-subject
1602     "\M-p" gnus-summary-prev-unread-subject
1603     "f" gnus-summary-first-unread-article
1604     "b" gnus-summary-best-unread-article
1605     "j" gnus-summary-goto-article
1606     "g" gnus-summary-goto-subject
1607     "l" gnus-summary-goto-last-article
1608     "o" gnus-summary-pop-article)
1609
1610   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1611     "k" gnus-summary-kill-thread
1612     "l" gnus-summary-lower-thread
1613     "i" gnus-summary-raise-thread
1614     "T" gnus-summary-toggle-threads
1615     "t" gnus-summary-rethread-current
1616     "^" gnus-summary-reparent-thread
1617     "s" gnus-summary-show-thread
1618     "S" gnus-summary-show-all-threads
1619     "h" gnus-summary-hide-thread
1620     "H" gnus-summary-hide-all-threads
1621     "n" gnus-summary-next-thread
1622     "p" gnus-summary-prev-thread
1623     "u" gnus-summary-up-thread
1624     "o" gnus-summary-top-thread
1625     "d" gnus-summary-down-thread
1626     "#" gnus-uu-mark-thread
1627     "\M-#" gnus-uu-unmark-thread)
1628
1629   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1630     "g" gnus-summary-prepare
1631     "c" gnus-summary-insert-cached-articles)
1632
1633   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1634     "c" gnus-summary-catchup-and-exit
1635     "C" gnus-summary-catchup-all-and-exit
1636     "E" gnus-summary-exit-no-update
1637     "J" gnus-summary-jump-to-other-group
1638     "Q" gnus-summary-exit
1639     "Z" gnus-summary-exit
1640     "n" gnus-summary-catchup-and-goto-next-group
1641     "R" gnus-summary-reselect-current-group
1642     "G" gnus-summary-rescan-group
1643     "N" gnus-summary-next-group
1644     "s" gnus-summary-save-newsrc
1645     "P" gnus-summary-prev-group)
1646
1647   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1648     " " gnus-summary-next-page
1649     "n" gnus-summary-next-page
1650     "\177" gnus-summary-prev-page
1651     [delete] gnus-summary-prev-page
1652     "p" gnus-summary-prev-page
1653     "\r" gnus-summary-scroll-up
1654     "\M-\r" gnus-summary-scroll-down
1655     "<" gnus-summary-beginning-of-article
1656     ">" gnus-summary-end-of-article
1657     "b" gnus-summary-beginning-of-article
1658     "e" gnus-summary-end-of-article
1659     "^" gnus-summary-refer-parent-article
1660     "r" gnus-summary-refer-parent-article
1661     "D" gnus-summary-enter-digest-group
1662     "R" gnus-summary-refer-references
1663     "T" gnus-summary-refer-thread
1664     "g" gnus-summary-show-article
1665     "s" gnus-summary-isearch-article
1666     "P" gnus-summary-print-article
1667     "M" gnus-mailing-list-insinuate
1668     "t" gnus-article-babel)
1669
1670   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1671     "b" gnus-article-add-buttons
1672     "B" gnus-article-add-buttons-to-head
1673     "o" gnus-article-treat-overstrike
1674     "e" gnus-article-emphasize
1675     "w" gnus-article-fill-cited-article
1676     "Q" gnus-article-fill-long-lines
1677     "C" gnus-article-capitalize-sentences
1678     "c" gnus-article-remove-cr
1679     "Z" gnus-article-decode-HZ
1680     "f" gnus-article-display-x-face
1681     "l" gnus-summary-stop-page-breaking
1682     "r" gnus-summary-caesar-message
1683     "t" gnus-article-toggle-headers
1684     "v" gnus-summary-verbose-headers
1685     "m" gnus-summary-toggle-mime
1686     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1687     "p" gnus-article-verify-x-pgp-sig
1688     "d" gnus-article-treat-dumbquotes
1689     "s" gnus-smiley-display)
1690
1691   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1692     "a" gnus-article-hide
1693     "h" gnus-article-toggle-headers
1694     "b" gnus-article-hide-boring-headers
1695     "s" gnus-article-hide-signature
1696     "c" gnus-article-hide-citation
1697     "C" gnus-article-hide-citation-in-followups
1698     "l" gnus-article-hide-list-identifiers
1699     "p" gnus-article-hide-pgp
1700     "B" gnus-article-strip-banner
1701     "P" gnus-article-hide-pem
1702     "\C-c" gnus-article-hide-citation-maybe)
1703
1704   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1705     "a" gnus-article-highlight
1706     "h" gnus-article-highlight-headers
1707     "c" gnus-article-highlight-citation
1708     "s" gnus-article-highlight-signature)
1709
1710   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1711     "z" gnus-article-date-ut
1712     "u" gnus-article-date-ut
1713     "l" gnus-article-date-local
1714     "p" gnus-article-date-english
1715     "e" gnus-article-date-lapsed
1716     "o" gnus-article-date-original
1717     "i" gnus-article-date-iso8601
1718     "s" gnus-article-date-user)
1719
1720   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1721     "t" gnus-article-remove-trailing-blank-lines
1722     "l" gnus-article-strip-leading-blank-lines
1723     "m" gnus-article-strip-multiple-blank-lines
1724     "a" gnus-article-strip-blank-lines
1725     "A" gnus-article-strip-all-blank-lines
1726     "s" gnus-article-strip-leading-space
1727     "e" gnus-article-strip-trailing-space
1728     "w" gnus-article-remove-leading-whitespace)
1729
1730   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1731     "v" gnus-version
1732     "f" gnus-summary-fetch-faq
1733     "d" gnus-summary-describe-group
1734     "h" gnus-summary-describe-briefly
1735     "i" gnus-info-find-node)
1736
1737   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1738     "e" gnus-summary-expire-articles
1739     "\M-\C-e" gnus-summary-expire-articles-now
1740     "\177" gnus-summary-delete-article
1741     [delete] gnus-summary-delete-article
1742     [backspace] gnus-summary-delete-article
1743     "m" gnus-summary-move-article
1744     "r" gnus-summary-respool-article
1745     "w" gnus-summary-edit-article
1746     "c" gnus-summary-copy-article
1747     "B" gnus-summary-crosspost-article
1748     "q" gnus-summary-respool-query
1749     "t" gnus-summary-respool-trace
1750     "i" gnus-summary-import-article
1751     "I" gnus-summary-create-article
1752     "p" gnus-summary-article-posted-p)
1753
1754   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1755     "o" gnus-summary-save-article
1756     "m" gnus-summary-save-article-mail
1757     "F" gnus-summary-write-article-file
1758     "r" gnus-summary-save-article-rmail
1759     "f" gnus-summary-save-article-file
1760     "b" gnus-summary-save-article-body-file
1761     "h" gnus-summary-save-article-folder
1762     "v" gnus-summary-save-article-vm
1763     "p" gnus-summary-pipe-output
1764     "s" gnus-soup-add-article)
1765
1766   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1767     "b" gnus-summary-display-buttonized
1768     "m" gnus-summary-repair-multipart
1769     "v" gnus-article-view-part
1770     "o" gnus-article-save-part
1771     "c" gnus-article-copy-part
1772     "C" gnus-article-view-part-as-charset
1773     "e" gnus-article-externalize-part
1774     "E" gnus-article-encrypt-body
1775     "i" gnus-article-inline-part
1776     "|" gnus-article-pipe-part))
1777
1778 (defvar gnus-article-post-menu nil)
1779
1780 (defun gnus-summary-make-menu-bar ()
1781   (gnus-turn-off-edit-menu 'summary)
1782
1783   (unless (boundp 'gnus-summary-misc-menu)
1784
1785     (easy-menu-define
1786      gnus-summary-kill-menu gnus-summary-mode-map ""
1787      (cons
1788       "Score"
1789       (nconc
1790        (list
1791         ["Customize" gnus-score-customize t])
1792        (gnus-make-score-map 'increase)
1793        (gnus-make-score-map 'lower)
1794        '(("Mark"
1795           ["Kill below" gnus-summary-kill-below t]
1796           ["Mark above" gnus-summary-mark-above t]
1797           ["Tick above" gnus-summary-tick-above t]
1798           ["Clear above" gnus-summary-clear-above t])
1799          ["Current score" gnus-summary-current-score t]
1800          ["Set score" gnus-summary-set-score t]
1801          ["Switch current score file..." gnus-score-change-score-file t]
1802          ["Set mark below..." gnus-score-set-mark-below t]
1803          ["Set expunge below..." gnus-score-set-expunge-below t]
1804          ["Edit current score file" gnus-score-edit-current-scores t]
1805          ["Edit score file" gnus-score-edit-file t]
1806          ["Trace score" gnus-score-find-trace t]
1807          ["Find words" gnus-score-find-favourite-words t]
1808          ["Rescore buffer" gnus-summary-rescore t]
1809          ["Increase score..." gnus-summary-increase-score t]
1810          ["Lower score..." gnus-summary-lower-score t]))))
1811
1812     ;; Define both the Article menu in the summary buffer and the equivalent
1813     ;; Commands menu in the article buffer here for consistency.
1814     (let ((innards
1815            `(("Hide"
1816               ["All" gnus-article-hide t]
1817               ["Headers" gnus-article-toggle-headers t]
1818               ["Signature" gnus-article-hide-signature t]
1819               ["Citation" gnus-article-hide-citation t]
1820               ["List identifiers" gnus-article-hide-list-identifiers t]
1821               ["PGP" gnus-article-hide-pgp t]
1822               ["Banner" gnus-article-strip-banner t]
1823               ["Boring headers" gnus-article-hide-boring-headers t])
1824              ("Highlight"
1825               ["All" gnus-article-highlight t]
1826               ["Headers" gnus-article-highlight-headers t]
1827               ["Signature" gnus-article-highlight-signature t]
1828               ["Citation" gnus-article-highlight-citation t])
1829              ("Date"
1830               ["Local" gnus-article-date-local t]
1831               ["ISO8601" gnus-article-date-iso8601 t]
1832               ["UT" gnus-article-date-ut t]
1833               ["Original" gnus-article-date-original t]
1834               ["Lapsed" gnus-article-date-lapsed t]
1835               ["User-defined" gnus-article-date-user t])
1836              ("Washing"
1837               ("Remove Blanks"
1838                ["Leading" gnus-article-strip-leading-blank-lines t]
1839                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1840                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1841                ["All of the above" gnus-article-strip-blank-lines t]
1842                ["All" gnus-article-strip-all-blank-lines t]
1843                ["Leading space" gnus-article-strip-leading-space t]
1844                ["Trailing space" gnus-article-strip-trailing-space t]
1845                ["Leading space in headers" 
1846                 gnus-article-remove-leading-whitespace t])
1847               ["Overstrike" gnus-article-treat-overstrike t]
1848               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1849               ["Emphasis" gnus-article-emphasize t]
1850               ["Word wrap" gnus-article-fill-cited-article t]
1851               ["Fill long lines" gnus-article-fill-long-lines t]
1852               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1853               ["CR" gnus-article-remove-cr t]
1854               ["Show X-Face" gnus-article-display-x-face t]
1855               ["Rot 13" gnus-summary-caesar-message
1856                ,@(if (featurep 'xemacs) '(t)
1857                    '(:help "\"Caesar rotate\" article by 13"))]
1858               ["Unix pipe" gnus-summary-pipe-message t]
1859               ["Add buttons" gnus-article-add-buttons t]
1860               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1861               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1862               ["Toggle MIME" gnus-summary-toggle-mime t]
1863               ["Verbose header" gnus-summary-verbose-headers t]
1864               ["Toggle header" gnus-summary-toggle-header t]
1865               ["Toggle smileys" gnus-smiley-display t]
1866               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1867               ["HZ" gnus-article-decode-HZ t])
1868              ("Output"
1869               ["Save in default format" gnus-summary-save-article
1870                ,@(if (featurep 'xemacs) '(t)
1871                    '(:help "Save article using default method"))]
1872               ["Save in file" gnus-summary-save-article-file
1873                ,@(if (featurep 'xemacs) '(t)
1874                    '(:help "Save article in file"))]
1875               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1876               ["Save in MH folder" gnus-summary-save-article-folder t]
1877               ["Save in VM folder" gnus-summary-save-article-vm t]
1878               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1879               ["Save body in file" gnus-summary-save-article-body-file t]
1880               ["Pipe through a filter" gnus-summary-pipe-output t]
1881               ["Add to SOUP packet" gnus-soup-add-article t]
1882               ["Print" gnus-summary-print-article t])
1883              ("Backend"
1884               ["Respool article..." gnus-summary-respool-article t]
1885               ["Move article..." gnus-summary-move-article
1886                (gnus-check-backend-function
1887                 'request-move-article gnus-newsgroup-name)]
1888               ["Copy article..." gnus-summary-copy-article t]
1889               ["Crosspost article..." gnus-summary-crosspost-article
1890                (gnus-check-backend-function
1891                 'request-replace-article gnus-newsgroup-name)]
1892               ["Import file..." gnus-summary-import-article t]
1893               ["Create article..." gnus-summary-create-article t]
1894               ["Check if posted" gnus-summary-article-posted-p t]
1895               ["Edit article" gnus-summary-edit-article
1896                (not (gnus-group-read-only-p))]
1897               ["Delete article" gnus-summary-delete-article
1898                (gnus-check-backend-function
1899                 'request-expire-articles gnus-newsgroup-name)]
1900               ["Query respool" gnus-summary-respool-query t]
1901               ["Trace respool" gnus-summary-respool-trace t]
1902               ["Delete expirable articles" gnus-summary-expire-articles-now
1903                (gnus-check-backend-function
1904                 'request-expire-articles gnus-newsgroup-name)])
1905              ("Extract"
1906               ["Uudecode" gnus-uu-decode-uu
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Decode uuencoded article(s)"))]
1909               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1910               ["Unshar" gnus-uu-decode-unshar t]
1911               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1912               ["Save" gnus-uu-decode-save t]
1913               ["Binhex" gnus-uu-decode-binhex t]
1914               ["Postscript" gnus-uu-decode-postscript t])
1915              ("Cache"
1916               ["Enter article" gnus-cache-enter-article t]
1917               ["Remove article" gnus-cache-remove-article t])
1918              ["Translate" gnus-article-babel t]
1919              ["Select article buffer" gnus-summary-select-article-buffer t]
1920              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1921              ["Isearch article..." gnus-summary-isearch-article t]
1922              ["Beginning of the article" gnus-summary-beginning-of-article t]
1923              ["End of the article" gnus-summary-end-of-article t]
1924              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1925              ["Fetch referenced articles" gnus-summary-refer-references t]
1926              ["Fetch current thread" gnus-summary-refer-thread t]
1927              ["Fetch article with id..." gnus-summary-refer-article t]
1928              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1929              ["Redisplay" gnus-summary-show-article t]
1930              ["Raw article" gnus-summary-show-raw-article t])))
1931       (easy-menu-define
1932        gnus-summary-article-menu gnus-summary-mode-map ""
1933        (cons "Article" innards))
1934
1935       (if (not (keymapp gnus-summary-article-menu))
1936           (easy-menu-define
1937             gnus-article-commands-menu gnus-article-mode-map ""
1938             (cons "Commands" innards))
1939         ;; in Emacs, don't share menu.
1940         (setq gnus-article-commands-menu 
1941               (copy-keymap gnus-summary-article-menu))
1942         (define-key gnus-article-mode-map [menu-bar commands]
1943           (cons "Commands" gnus-article-commands-menu))))
1944
1945     (easy-menu-define
1946      gnus-summary-thread-menu gnus-summary-mode-map ""
1947      '("Threads"
1948        ["Toggle threading" gnus-summary-toggle-threads t]
1949        ["Hide threads" gnus-summary-hide-all-threads t]
1950        ["Show threads" gnus-summary-show-all-threads t]
1951        ["Hide thread" gnus-summary-hide-thread t]
1952        ["Show thread" gnus-summary-show-thread t]
1953        ["Go to next thread" gnus-summary-next-thread t]
1954        ["Go to previous thread" gnus-summary-prev-thread t]
1955        ["Go down thread" gnus-summary-down-thread t]
1956        ["Go up thread" gnus-summary-up-thread t]
1957        ["Top of thread" gnus-summary-top-thread t]
1958        ["Mark thread as read" gnus-summary-kill-thread t]
1959        ["Lower thread score" gnus-summary-lower-thread t]
1960        ["Raise thread score" gnus-summary-raise-thread t]
1961        ["Rethread current" gnus-summary-rethread-current t]))
1962
1963     (easy-menu-define
1964      gnus-summary-post-menu gnus-summary-mode-map ""
1965      `("Post"
1966        ["Post an article" gnus-summary-post-news
1967         ,@(if (featurep 'xemacs) '(t)
1968             '(:help "Post an article"))]
1969        ["Followup" gnus-summary-followup
1970         ,@(if (featurep 'xemacs) '(t)
1971             '(:help "Post followup to this article"))]
1972        ["Followup and yank" gnus-summary-followup-with-original
1973         ,@(if (featurep 'xemacs) '(t)
1974             '(:help "Post followup to this article, quoting its contents"))]
1975        ["Supersede article" gnus-summary-supersede-article t]
1976        ["Cancel article" gnus-summary-cancel-article
1977         ,@(if (featurep 'xemacs) '(t)
1978             '(:help "Cancel an article you posted"))]
1979        ["Reply" gnus-summary-reply t]
1980        ["Reply and yank" gnus-summary-reply-with-original t]
1981        ["Wide reply" gnus-summary-wide-reply t]
1982        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1983         ,@(if (featurep 'xemacs) '(t)
1984             '(:help "Mail a reply, quoting this article"))]
1985        ["Mail forward" gnus-summary-mail-forward t]
1986        ["Post forward" gnus-summary-post-forward t]
1987        ["Digest and mail" gnus-summary-digest-mail-forward t]
1988        ["Digest and post" gnus-summary-digest-post-forward t]
1989        ["Resend message" gnus-summary-resend-message t]
1990        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1991        ["Send a mail" gnus-summary-mail-other-window t]
1992        ["Uuencode and post" gnus-uu-post-news
1993         ,@(if (featurep 'xemacs) '(t)
1994             '(:help "Post a uuencoded article"))]
1995        ["Followup via news" gnus-summary-followup-to-mail t]
1996        ["Followup via news and yank"
1997         gnus-summary-followup-to-mail-with-original t]
1998        ;;("Draft"
1999        ;;["Send" gnus-summary-send-draft t]
2000        ;;["Send bounced" gnus-resend-bounced-mail t])
2001        ))
2002
2003     (cond 
2004      ((not (keymapp gnus-summary-post-menu))
2005       (setq gnus-article-post-menu gnus-summary-post-menu))
2006      ((not gnus-article-post-menu)
2007       ;; Don't share post menu.
2008       (setq gnus-article-post-menu
2009             (copy-keymap gnus-summary-post-menu))))
2010     (define-key gnus-article-mode-map [menu-bar post]
2011       (cons "Post" gnus-article-post-menu))
2012
2013     (easy-menu-define
2014      gnus-summary-misc-menu gnus-summary-mode-map ""
2015      `("Misc"
2016        ("Mark Read"
2017         ["Mark as read" gnus-summary-mark-as-read-forward t]
2018         ["Mark same subject and select"
2019          gnus-summary-kill-same-subject-and-select t]
2020         ["Mark same subject" gnus-summary-kill-same-subject t]
2021         ["Catchup" gnus-summary-catchup
2022          ,@(if (featurep 'xemacs) '(t)
2023              '(:help "Mark unread articles in this group as read"))]
2024         ["Catchup all" gnus-summary-catchup-all t]
2025         ["Catchup to here" gnus-summary-catchup-to-here t]
2026         ["Catchup from here" gnus-summary-catchup-from-here t]
2027         ["Catchup region" gnus-summary-mark-region-as-read t]
2028         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2029        ("Mark Various"
2030         ["Tick" gnus-summary-tick-article-forward t]
2031         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2032         ["Remove marks" gnus-summary-clear-mark-forward t]
2033         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2034         ["Set bookmark" gnus-summary-set-bookmark t]
2035         ["Remove bookmark" gnus-summary-remove-bookmark t])
2036        ("Mark Limit"
2037         ["Marks..." gnus-summary-limit-to-marks t]
2038         ["Subject..." gnus-summary-limit-to-subject t]
2039         ["Author..." gnus-summary-limit-to-author t]
2040         ["Age..." gnus-summary-limit-to-age t]
2041         ["Extra..." gnus-summary-limit-to-extra t]
2042         ["Score" gnus-summary-limit-to-score t]
2043         ["Unread" gnus-summary-limit-to-unread t]
2044         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2045         ["Articles" gnus-summary-limit-to-articles t]
2046         ["Pop limit" gnus-summary-pop-limit t]
2047         ["Show dormant" gnus-summary-limit-include-dormant t]
2048         ["Hide childless dormant"
2049          gnus-summary-limit-exclude-childless-dormant t]
2050         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2051         ["Hide marked" gnus-summary-limit-exclude-marks t]
2052         ["Show expunged" gnus-summary-limit-include-expunged t])
2053        ("Process Mark"
2054         ["Set mark" gnus-summary-mark-as-processable t]
2055         ["Remove mark" gnus-summary-unmark-as-processable t]
2056         ["Remove all marks" gnus-summary-unmark-all-processable t]
2057         ["Mark above" gnus-uu-mark-over t]
2058         ["Mark series" gnus-uu-mark-series t]
2059         ["Mark region" gnus-uu-mark-region t]
2060         ["Unmark region" gnus-uu-unmark-region t]
2061         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2062         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2063         ["Mark all" gnus-uu-mark-all t]
2064         ["Mark buffer" gnus-uu-mark-buffer t]
2065         ["Mark sparse" gnus-uu-mark-sparse t]
2066         ["Mark thread" gnus-uu-mark-thread t]
2067         ["Unmark thread" gnus-uu-unmark-thread t]
2068         ("Process Mark Sets"
2069          ["Kill" gnus-summary-kill-process-mark t]
2070          ["Yank" gnus-summary-yank-process-mark
2071           gnus-newsgroup-process-stack]
2072          ["Save" gnus-summary-save-process-mark t]))
2073        ("Scroll article"
2074         ["Page forward" gnus-summary-next-page
2075          ,@(if (featurep 'xemacs) '(t)
2076              '(:help "Show next page of article"))]
2077         ["Page backward" gnus-summary-prev-page
2078          ,@(if (featurep 'xemacs) '(t)
2079              '(:help "Show previous page of article"))]
2080         ["Line forward" gnus-summary-scroll-up t])
2081        ("Move"
2082         ["Next unread article" gnus-summary-next-unread-article t]
2083         ["Previous unread article" gnus-summary-prev-unread-article t]
2084         ["Next article" gnus-summary-next-article t]
2085         ["Previous article" gnus-summary-prev-article t]
2086         ["Next unread subject" gnus-summary-next-unread-subject t]
2087         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2088         ["Next article same subject" gnus-summary-next-same-subject t]
2089         ["Previous article same subject" gnus-summary-prev-same-subject t]
2090         ["First unread article" gnus-summary-first-unread-article t]
2091         ["Best unread article" gnus-summary-best-unread-article t]
2092         ["Go to subject number..." gnus-summary-goto-subject t]
2093         ["Go to article number..." gnus-summary-goto-article t]
2094         ["Go to the last article" gnus-summary-goto-last-article t]
2095         ["Pop article off history" gnus-summary-pop-article t])
2096        ("Sort"
2097         ["Sort by number" gnus-summary-sort-by-number t]
2098         ["Sort by author" gnus-summary-sort-by-author t]
2099         ["Sort by subject" gnus-summary-sort-by-subject t]
2100         ["Sort by date" gnus-summary-sort-by-date t]
2101         ["Sort by score" gnus-summary-sort-by-score t]
2102         ["Sort by lines" gnus-summary-sort-by-lines t]
2103         ["Sort by characters" gnus-summary-sort-by-chars t]
2104         ["Original sort" gnus-summary-sort-by-original t])
2105        ("Help"
2106         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2107         ["Describe group" gnus-summary-describe-group t]
2108         ["Read manual" gnus-info-find-node t])
2109        ("Modes"
2110         ["Pick and read" gnus-pick-mode t]
2111         ["Binary" gnus-binary-mode t])
2112        ("Regeneration"
2113         ["Regenerate" gnus-summary-prepare t]
2114         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2115         ["Toggle threading" gnus-summary-toggle-threads t])
2116        ["See old articles" gnus-summary-insert-old-articles t]
2117        ["See new articles" gnus-summary-insert-new-articles t]
2118        ["Filter articles..." gnus-summary-execute-command t]
2119        ["Run command on subjects..." gnus-summary-universal-argument t]
2120        ["Search articles forward..." gnus-summary-search-article-forward t]
2121        ["Search articles backward..." gnus-summary-search-article-backward t]
2122        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2123        ["Expand window" gnus-summary-expand-window t]
2124        ["Expire expirable articles" gnus-summary-expire-articles
2125         (gnus-check-backend-function
2126          'request-expire-articles gnus-newsgroup-name)]
2127        ["Edit local kill file" gnus-summary-edit-local-kill t]
2128        ["Edit main kill file" gnus-summary-edit-global-kill t]
2129        ["Edit group parameters" gnus-summary-edit-parameters t]
2130        ["Customize group parameters" gnus-summary-customize-parameters t]
2131        ["Send a bug report" gnus-bug t]
2132        ("Exit"
2133         ["Catchup and exit" gnus-summary-catchup-and-exit
2134          ,@(if (featurep 'xemacs) '(t)
2135              '(:help "Mark unread articles in this group as read, then exit"))]
2136         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2137         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2138         ["Exit group" gnus-summary-exit
2139          ,@(if (featurep 'xemacs) '(t)
2140              '(:help "Exit current group, return to group selection mode"))]
2141         ["Exit group without updating" gnus-summary-exit-no-update t]
2142         ["Exit and goto next group" gnus-summary-next-group t]
2143         ["Exit and goto prev group" gnus-summary-prev-group t]
2144         ["Reselect group" gnus-summary-reselect-current-group t]
2145         ["Rescan group" gnus-summary-rescan-group t]
2146         ["Update dribble" gnus-summary-save-newsrc t])))
2147
2148     (gnus-run-hooks 'gnus-summary-menu-hook)))
2149
2150 (defvar gnus-summary-tool-bar-map nil)
2151
2152 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2153 (defun gnus-summary-make-tool-bar ()
2154   (if (and (fboundp 'tool-bar-add-item-from-menu)
2155            (default-value 'tool-bar-mode)
2156            (not gnus-summary-tool-bar-map))
2157       (setq gnus-summary-tool-bar-map
2158             (let ((tool-bar-map (make-sparse-keymap))
2159                   (load-path (mm-image-load-path)))
2160               (tool-bar-add-item-from-menu
2161                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2164               (tool-bar-add-item-from-menu
2165                'gnus-summary-post-news "post" gnus-summary-mode-map)
2166               (tool-bar-add-item-from-menu
2167                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2168               (tool-bar-add-item-from-menu
2169                'gnus-summary-followup "followup" gnus-summary-mode-map)
2170               (tool-bar-add-item-from-menu
2171                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2172               (tool-bar-add-item-from-menu
2173                'gnus-summary-reply "reply" gnus-summary-mode-map)
2174               (tool-bar-add-item-from-menu
2175                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2176               (tool-bar-add-item-from-menu
2177                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2178               (tool-bar-add-item-from-menu
2179                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2180               (tool-bar-add-item-from-menu
2181                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2182               (tool-bar-add-item-from-menu
2183                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2184               (tool-bar-add-item-from-menu
2185                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2186               (tool-bar-add-item-from-menu
2187                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2188               (tool-bar-add-item-from-menu
2189                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2190               tool-bar-map)))
2191   (if gnus-summary-tool-bar-map
2192       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2193
2194 (defun gnus-score-set-default (var value)
2195   "A version of set that updates the GNU Emacs menu-bar."
2196   (set var value)
2197   ;; It is the message that forces the active status to be updated.
2198   (message ""))
2199
2200 (defun gnus-make-score-map (type)
2201   "Make a summary score map of type TYPE."
2202   (if t
2203       nil
2204     (let ((headers '(("author" "from" string)
2205                      ("subject" "subject" string)
2206                      ("article body" "body" string)
2207                      ("article head" "head" string)
2208                      ("xref" "xref" string)
2209                      ("extra header" "extra" string)
2210                      ("lines" "lines" number)
2211                      ("followups to author" "followup" string)))
2212           (types '((number ("less than" <)
2213                            ("greater than" >)
2214                            ("equal" =))
2215                    (string ("substring" s)
2216                            ("exact string" e)
2217                            ("fuzzy string" f)
2218                            ("regexp" r))))
2219           (perms '(("temporary" (current-time-string))
2220                    ("permanent" nil)
2221                    ("immediate" now)))
2222           header)
2223       (list
2224        (apply
2225         'nconc
2226         (list
2227          (if (eq type 'lower)
2228              "Lower score"
2229            "Increase score"))
2230         (let (outh)
2231           (while headers
2232             (setq header (car headers))
2233             (setq outh
2234                   (cons
2235                    (apply
2236                     'nconc
2237                     (list (car header))
2238                     (let ((ts (cdr (assoc (nth 2 header) types)))
2239                           outt)
2240                       (while ts
2241                         (setq outt
2242                               (cons
2243                                (apply
2244                                 'nconc
2245                                 (list (caar ts))
2246                                 (let ((ps perms)
2247                                       outp)
2248                                   (while ps
2249                                     (setq outp
2250                                           (cons
2251                                            (vector
2252                                             (caar ps)
2253                                             (list
2254                                              'gnus-summary-score-entry
2255                                              (nth 1 header)
2256                                              (if (or (string= (nth 1 header)
2257                                                               "head")
2258                                                      (string= (nth 1 header)
2259                                                               "body"))
2260                                                  ""
2261                                                (list 'gnus-summary-header
2262                                                      (nth 1 header)))
2263                                              (list 'quote (nth 1 (car ts)))
2264                                              (list 'gnus-score-delta-default
2265                                                    nil)
2266                                              (nth 1 (car ps))
2267                                              t)
2268                                             t)
2269                                            outp))
2270                                     (setq ps (cdr ps)))
2271                                   (list (nreverse outp))))
2272                                outt))
2273                         (setq ts (cdr ts)))
2274                       (list (nreverse outt))))
2275                    outh))
2276             (setq headers (cdr headers)))
2277           (list (nreverse outh))))))))
2278
2279 \f
2280
2281 (defun gnus-summary-mode (&optional group)
2282   "Major mode for reading articles.
2283
2284 All normal editing commands are switched off.
2285 \\<gnus-summary-mode-map>
2286 Each line in this buffer represents one article.  To read an
2287 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2288 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2289 respectively.
2290
2291 You can also post articles and send mail from this buffer.  To
2292 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2293 of an article, type `\\[gnus-summary-reply]'.
2294
2295 There are approx. one gazillion commands you can execute in this
2296 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2297
2298 The following commands are available:
2299
2300 \\{gnus-summary-mode-map}"
2301   (interactive)
2302   (kill-all-local-variables)
2303   (when (gnus-visual-p 'summary-menu 'menu)
2304     (gnus-summary-make-menu-bar)
2305     (gnus-summary-make-tool-bar))
2306   (gnus-summary-make-local-variables)
2307   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2308     (gnus-summary-make-local-variables))
2309   (gnus-make-thread-indent-array)
2310   (gnus-simplify-mode-line)
2311   (setq major-mode 'gnus-summary-mode)
2312   (setq mode-name "Summary")
2313   (make-local-variable 'minor-mode-alist)
2314   (use-local-map gnus-summary-mode-map)
2315   (buffer-disable-undo)
2316   (setq buffer-read-only t)             ;Disable modification
2317   (setq truncate-lines t)
2318   (setq selective-display t)
2319   (setq selective-display-ellipses t)   ;Display `...'
2320   (gnus-summary-set-display-table)
2321   (gnus-set-default-directory)
2322   (setq gnus-newsgroup-name group)
2323   (unless (gnus-news-group-p group)
2324     (setq gnus-newsgroup-incorporated
2325           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2326   (make-local-variable 'gnus-summary-line-format)
2327   (make-local-variable 'gnus-summary-line-format-spec)
2328   (make-local-variable 'gnus-summary-dummy-line-format)
2329   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2330   (make-local-variable 'gnus-summary-mark-positions)
2331   (make-local-hook 'pre-command-hook)
2332   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2333   (gnus-run-hooks 'gnus-summary-mode-hook)
2334   (turn-on-gnus-mailing-list-mode)
2335   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2336   (gnus-update-summary-mark-positions))
2337
2338 (defun gnus-summary-make-local-variables ()
2339   "Make all the local summary buffer variables."
2340   (let (global)
2341     (dolist (local gnus-summary-local-variables)
2342       (if (consp local)
2343           (progn
2344             (if (eq (cdr local) 'global)
2345                 ;; Copy the global value of the variable.
2346                 (setq global (symbol-value (car local)))
2347               ;; Use the value from the list.
2348               (setq global (eval (cdr local))))
2349             (set (make-local-variable (car local)) global))
2350         ;; Simple nil-valued local variable.
2351         (set (make-local-variable local) nil)))))
2352
2353 (defun gnus-summary-clear-local-variables ()
2354   (let ((locals gnus-summary-local-variables))
2355     (while locals
2356       (if (consp (car locals))
2357           (and (vectorp (caar locals))
2358                (set (caar locals) nil))
2359         (and (vectorp (car locals))
2360              (set (car locals) nil)))
2361       (setq locals (cdr locals)))))
2362
2363 ;; Summary data functions.
2364
2365 (defmacro gnus-data-number (data)
2366   `(car ,data))
2367
2368 (defmacro gnus-data-set-number (data number)
2369   `(setcar ,data ,number))
2370
2371 (defmacro gnus-data-mark (data)
2372   `(nth 1 ,data))
2373
2374 (defmacro gnus-data-set-mark (data mark)
2375   `(setcar (nthcdr 1 ,data) ,mark))
2376
2377 (defmacro gnus-data-pos (data)
2378   `(nth 2 ,data))
2379
2380 (defmacro gnus-data-set-pos (data pos)
2381   `(setcar (nthcdr 2 ,data) ,pos))
2382
2383 (defmacro gnus-data-header (data)
2384   `(nth 3 ,data))
2385
2386 (defmacro gnus-data-set-header (data header)
2387   `(setcar (nthcdr 3 ,data) ,header))
2388
2389 (defmacro gnus-data-level (data)
2390   `(nth 4 ,data))
2391
2392 (defmacro gnus-data-unread-p (data)
2393   `(= (nth 1 ,data) gnus-unread-mark))
2394
2395 (defmacro gnus-data-read-p (data)
2396   `(/= (nth 1 ,data) gnus-unread-mark))
2397
2398 (defmacro gnus-data-pseudo-p (data)
2399   `(consp (nth 3 ,data)))
2400
2401 (defmacro gnus-data-find (number)
2402   `(assq ,number gnus-newsgroup-data))
2403
2404 (defmacro gnus-data-find-list (number &optional data)
2405   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2406      (memq (assq ,number bdata)
2407            bdata)))
2408
2409 (defmacro gnus-data-make (number mark pos header level)
2410   `(list ,number ,mark ,pos ,header ,level))
2411
2412 (defun gnus-data-enter (after-article number mark pos header level offset)
2413   (let ((data (gnus-data-find-list after-article)))
2414     (unless data
2415       (error "No such article: %d" after-article))
2416     (setcdr data (cons (gnus-data-make number mark pos header level)
2417                        (cdr data)))
2418     (setq gnus-newsgroup-data-reverse nil)
2419     (gnus-data-update-list (cddr data) offset)))
2420
2421 (defun gnus-data-enter-list (after-article list &optional offset)
2422   (when list
2423     (let ((data (and after-article (gnus-data-find-list after-article)))
2424           (ilist list))
2425       (if (not (or data
2426                    after-article))
2427           (let ((odata gnus-newsgroup-data))
2428             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2429             (when offset
2430               (gnus-data-update-list odata offset)))
2431         ;; Find the last element in the list to be spliced into the main
2432         ;; list.
2433         (while (cdr list)
2434           (setq list (cdr list)))
2435         (if (not data)
2436             (progn
2437               (setcdr list gnus-newsgroup-data)
2438               (setq gnus-newsgroup-data ilist)
2439               (when offset
2440                 (gnus-data-update-list (cdr list) offset)))
2441           (setcdr list (cdr data))
2442           (setcdr data ilist)
2443           (when offset
2444             (gnus-data-update-list (cdr list) offset))))
2445       (setq gnus-newsgroup-data-reverse nil))))
2446
2447 (defun gnus-data-remove (article &optional offset)
2448   (let ((data gnus-newsgroup-data))
2449     (if (= (gnus-data-number (car data)) article)
2450         (progn
2451           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2452                 gnus-newsgroup-data-reverse nil)
2453           (when offset
2454             (gnus-data-update-list gnus-newsgroup-data offset)))
2455       (while (cdr data)
2456         (when (= (gnus-data-number (cadr data)) article)
2457           (setcdr data (cddr data))
2458           (when offset
2459             (gnus-data-update-list (cdr data) offset))
2460           (setq data nil
2461                 gnus-newsgroup-data-reverse nil))
2462         (setq data (cdr data))))))
2463
2464 (defmacro gnus-data-list (backward)
2465   `(if ,backward
2466        (or gnus-newsgroup-data-reverse
2467            (setq gnus-newsgroup-data-reverse
2468                  (reverse gnus-newsgroup-data)))
2469      gnus-newsgroup-data))
2470
2471 (defun gnus-data-update-list (data offset)
2472   "Add OFFSET to the POS of all data entries in DATA."
2473   (setq gnus-newsgroup-data-reverse nil)
2474   (while data
2475     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2476     (setq data (cdr data))))
2477
2478 (defun gnus-summary-article-pseudo-p (article)
2479   "Say whether this article is a pseudo article or not."
2480   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2481
2482 (defmacro gnus-summary-article-sparse-p (article)
2483   "Say whether this article is a sparse article or not."
2484   `(memq ,article gnus-newsgroup-sparse))
2485
2486 (defmacro gnus-summary-article-ancient-p (article)
2487   "Say whether this article is a sparse article or not."
2488   `(memq ,article gnus-newsgroup-ancient))
2489
2490 (defun gnus-article-parent-p (number)
2491   "Say whether this article is a parent or not."
2492   (let ((data (gnus-data-find-list number)))
2493     (and (cdr data)                     ; There has to be an article after...
2494          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2495             (gnus-data-level (nth 1 data))))))
2496
2497 (defun gnus-article-children (number)
2498   "Return a list of all children to NUMBER."
2499   (let* ((data (gnus-data-find-list number))
2500          (level (gnus-data-level (car data)))
2501          children)
2502     (setq data (cdr data))
2503     (while (and data
2504                 (= (gnus-data-level (car data)) (1+ level)))
2505       (push (gnus-data-number (car data)) children)
2506       (setq data (cdr data)))
2507     children))
2508
2509 (defmacro gnus-summary-skip-intangible ()
2510   "If the current article is intangible, then jump to a different article."
2511   '(let ((to (get-text-property (point) 'gnus-intangible)))
2512      (and to (gnus-summary-goto-subject to))))
2513
2514 (defmacro gnus-summary-article-intangible-p ()
2515   "Say whether this article is intangible or not."
2516   '(get-text-property (point) 'gnus-intangible))
2517
2518 (defun gnus-article-read-p (article)
2519   "Say whether ARTICLE is read or not."
2520   (not (or (memq article gnus-newsgroup-marked)
2521            (memq article gnus-newsgroup-unreads)
2522            (memq article gnus-newsgroup-unselected)
2523            (memq article gnus-newsgroup-dormant))))
2524
2525 ;; Some summary mode macros.
2526
2527 (defmacro gnus-summary-article-number ()
2528   "The article number of the article on the current line.
2529 If there isn's an article number here, then we return the current
2530 article number."
2531   '(progn
2532      (gnus-summary-skip-intangible)
2533      (or (get-text-property (point) 'gnus-number)
2534          (gnus-summary-last-subject))))
2535
2536 (defmacro gnus-summary-article-header (&optional number)
2537   "Return the header of article NUMBER."
2538   `(gnus-data-header (gnus-data-find
2539                       ,(or number '(gnus-summary-article-number)))))
2540
2541 (defmacro gnus-summary-thread-level (&optional number)
2542   "Return the level of thread that starts with article NUMBER."
2543   `(if (and (eq gnus-summary-make-false-root 'dummy)
2544             (get-text-property (point) 'gnus-intangible))
2545        0
2546      (gnus-data-level (gnus-data-find
2547                        ,(or number '(gnus-summary-article-number))))))
2548
2549 (defmacro gnus-summary-article-mark (&optional number)
2550   "Return the mark of article NUMBER."
2551   `(gnus-data-mark (gnus-data-find
2552                     ,(or number '(gnus-summary-article-number)))))
2553
2554 (defmacro gnus-summary-article-pos (&optional number)
2555   "Return the position of the line of article NUMBER."
2556   `(gnus-data-pos (gnus-data-find
2557                    ,(or number '(gnus-summary-article-number)))))
2558
2559 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2560 (defmacro gnus-summary-article-subject (&optional number)
2561   "Return current subject string or nil if nothing."
2562   `(let ((headers
2563           ,(if number
2564                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2565              '(gnus-data-header (assq (gnus-summary-article-number)
2566                                       gnus-newsgroup-data)))))
2567      (and headers
2568           (vectorp headers)
2569           (mail-header-subject headers))))
2570
2571 (defmacro gnus-summary-article-score (&optional number)
2572   "Return current article score."
2573   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2574                   gnus-newsgroup-scored))
2575        gnus-summary-default-score 0))
2576
2577 (defun gnus-summary-article-children (&optional number)
2578   "Return a list of article numbers that are children of article NUMBER."
2579   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2580          (level (gnus-data-level (car data)))
2581          l children)
2582     (while (and (setq data (cdr data))
2583                 (> (setq l (gnus-data-level (car data))) level))
2584       (and (= (1+ level) l)
2585            (push (gnus-data-number (car data))
2586                  children)))
2587     (nreverse children)))
2588
2589 (defun gnus-summary-article-parent (&optional number)
2590   "Return the article number of the parent of article NUMBER."
2591   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2592                                     (gnus-data-list t)))
2593          (level (gnus-data-level (car data))))
2594     (if (zerop level)
2595         ()                              ; This is a root.
2596       ;; We search until we find an article with a level less than
2597       ;; this one.  That function has to be the parent.
2598       (while (and (setq data (cdr data))
2599                   (not (< (gnus-data-level (car data)) level))))
2600       (and data (gnus-data-number (car data))))))
2601
2602 (defun gnus-unread-mark-p (mark)
2603   "Say whether MARK is the unread mark."
2604   (= mark gnus-unread-mark))
2605
2606 (defun gnus-read-mark-p (mark)
2607   "Say whether MARK is one of the marks that mark as read.
2608 This is all marks except unread, ticked, dormant, and expirable."
2609   (not (or (= mark gnus-unread-mark)
2610            (= mark gnus-ticked-mark)
2611            (= mark gnus-dormant-mark)
2612            (= mark gnus-expirable-mark))))
2613
2614 (defmacro gnus-article-mark (number)
2615   "Return the MARK of article NUMBER.
2616 This macro should only be used when computing the mark the \"first\"
2617 time; i.e., when generating the summary lines.  After that,
2618 `gnus-summary-article-mark' should be used to examine the
2619 marks of articles."
2620   `(cond
2621     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2622     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2623     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2624     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2625     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2626     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2627     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2628     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2629            gnus-ancient-mark))))
2630
2631 ;; Saving hidden threads.
2632
2633 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2634 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2635
2636 (defmacro gnus-save-hidden-threads (&rest forms)
2637   "Save hidden threads, eval FORMS, and restore the hidden threads."
2638   (let ((config (make-symbol "config")))
2639     `(let ((,config (gnus-hidden-threads-configuration)))
2640        (unwind-protect
2641            (save-excursion
2642              ,@forms)
2643          (gnus-restore-hidden-threads-configuration ,config)))))
2644
2645 (defun gnus-data-compute-positions ()
2646   "Compute the positions of all articles."
2647   (setq gnus-newsgroup-data-reverse nil)
2648   (let ((data gnus-newsgroup-data))
2649     (save-excursion
2650       (gnus-save-hidden-threads
2651         (gnus-summary-show-all-threads)
2652         (goto-char (point-min))
2653         (while data
2654           (while (get-text-property (point) 'gnus-intangible)
2655             (forward-line 1))
2656           (gnus-data-set-pos (car data) (+ (point) 3))
2657           (setq data (cdr data))
2658           (forward-line 1))))))
2659
2660 (defun gnus-hidden-threads-configuration ()
2661   "Return the current hidden threads configuration."
2662   (save-excursion
2663     (let (config)
2664       (goto-char (point-min))
2665       (while (search-forward "\r" nil t)
2666         (push (1- (point)) config))
2667       config)))
2668
2669 (defun gnus-restore-hidden-threads-configuration (config)
2670   "Restore hidden threads configuration from CONFIG."
2671   (save-excursion
2672     (let (point buffer-read-only)
2673       (while (setq point (pop config))
2674         (when (and (< point (point-max))
2675                    (goto-char point)
2676                    (eq (char-after) ?\n))
2677           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2678
2679 ;; Various summary mode internalish functions.
2680
2681 (defun gnus-mouse-pick-article (e)
2682   (interactive "e")
2683   (mouse-set-point e)
2684   (gnus-summary-next-page nil t))
2685
2686 (defun gnus-summary-set-display-table ()
2687   "Change the display table.
2688 Odd characters have a tendency to mess
2689 up nicely formatted displays - we make all possible glyphs
2690 display only a single character."
2691
2692   ;; We start from the standard display table, if any.
2693   (let ((table (or (copy-sequence standard-display-table)
2694                    (make-display-table)))
2695         (i 32))
2696     ;; Nix out all the control chars...
2697     (while (>= (setq i (1- i)) 0)
2698       (aset table i [??]))
2699     ;; ... but not newline and cr, of course.  (cr is necessary for the
2700     ;; selective display).
2701     (aset table ?\n nil)
2702     (aset table ?\r nil)
2703     ;; We keep TAB as well.
2704     (aset table ?\t nil)
2705     ;; We nix out any glyphs over 126 that are not set already.
2706     (let ((i 256))
2707       (while (>= (setq i (1- i)) 127)
2708         ;; Only modify if the entry is nil.
2709         (unless (aref table i)
2710           (aset table i [??]))))
2711     (setq buffer-display-table table)))
2712
2713 (defun gnus-summary-buffer-name (group)
2714   "Return the summary buffer name of GROUP."
2715   (concat "*Summary " group "*"))
2716
2717 (defun gnus-summary-setup-buffer (group)
2718   "Initialize summary buffer."
2719   (let ((buffer (gnus-summary-buffer-name group)))
2720     (if (get-buffer buffer)
2721         (progn
2722           (set-buffer buffer)
2723           (setq gnus-summary-buffer (current-buffer))
2724           (not gnus-newsgroup-prepared))
2725       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2726       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2727       (gnus-summary-mode group)
2728       (when gnus-carpal
2729         (gnus-carpal-setup-buffer 'summary))
2730       (unless gnus-single-article-buffer
2731         (make-local-variable 'gnus-article-buffer)
2732         (make-local-variable 'gnus-article-current)
2733         (make-local-variable 'gnus-original-article-buffer))
2734       (setq gnus-newsgroup-name group)
2735       ;; Set any local variables in the group parameters.
2736       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2737       t)))
2738
2739 (defun gnus-set-global-variables ()
2740   "Set the global equivalents of the buffer-local variables.
2741 They are set to the latest values they had.  These reflect the summary
2742 buffer that was in action when the last article was fetched."
2743   (when (eq major-mode 'gnus-summary-mode)
2744     (setq gnus-summary-buffer (current-buffer))
2745     (let ((name gnus-newsgroup-name)
2746           (marked gnus-newsgroup-marked)
2747           (unread gnus-newsgroup-unreads)
2748           (headers gnus-current-headers)
2749           (data gnus-newsgroup-data)
2750           (summary gnus-summary-buffer)
2751           (article-buffer gnus-article-buffer)
2752           (original gnus-original-article-buffer)
2753           (gac gnus-article-current)
2754           (reffed gnus-reffed-article-number)
2755           (score-file gnus-current-score-file)
2756           (default-charset gnus-newsgroup-charset)
2757           vlist)
2758       (let ((locals gnus-newsgroup-variables))
2759         (while locals
2760           (if (consp (car locals))
2761               (push (eval (caar locals)) vlist)
2762             (push (eval (car locals)) vlist))
2763           (setq locals (cdr locals)))
2764         (setq vlist (nreverse vlist)))
2765       (save-excursion
2766         (set-buffer gnus-group-buffer)
2767         (setq gnus-newsgroup-name name
2768               gnus-newsgroup-marked marked
2769               gnus-newsgroup-unreads unread
2770               gnus-current-headers headers
2771               gnus-newsgroup-data data
2772               gnus-article-current gac
2773               gnus-summary-buffer summary
2774               gnus-article-buffer article-buffer
2775               gnus-original-article-buffer original
2776               gnus-reffed-article-number reffed
2777               gnus-current-score-file score-file
2778               gnus-newsgroup-charset default-charset)
2779         (let ((locals gnus-newsgroup-variables))
2780           (while locals
2781             (if (consp (car locals))
2782                 (set (caar locals) (pop vlist))
2783               (set (car locals) (pop vlist)))
2784             (setq locals (cdr locals))))
2785         ;; The article buffer also has local variables.
2786         (when (gnus-buffer-live-p gnus-article-buffer)
2787           (set-buffer gnus-article-buffer)
2788           (setq gnus-summary-buffer summary))))))
2789
2790 (defun gnus-summary-article-unread-p (article)
2791   "Say whether ARTICLE is unread or not."
2792   (memq article gnus-newsgroup-unreads))
2793
2794 (defun gnus-summary-first-article-p (&optional article)
2795   "Return whether ARTICLE is the first article in the buffer."
2796   (if (not (setq article (or article (gnus-summary-article-number))))
2797       nil
2798     (eq article (caar gnus-newsgroup-data))))
2799
2800 (defun gnus-summary-last-article-p (&optional article)
2801   "Return whether ARTICLE is the last article in the buffer."
2802   (if (not (setq article (or article (gnus-summary-article-number))))
2803       ;; All non-existent numbers are the last article.  :-)
2804       t
2805     (not (cdr (gnus-data-find-list article)))))
2806
2807 (defun gnus-make-thread-indent-array ()
2808   (let ((n 200))
2809     (unless (and gnus-thread-indent-array
2810                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2811       (setq gnus-thread-indent-array (make-vector 201 "")
2812             gnus-thread-indent-array-level gnus-thread-indent-level)
2813       (while (>= n 0)
2814         (aset gnus-thread-indent-array n
2815               (make-string (* n gnus-thread-indent-level) ? ))
2816         (setq n (1- n))))))
2817
2818 (defun gnus-update-summary-mark-positions ()
2819   "Compute where the summary marks are to go."
2820   (save-excursion
2821     (when (gnus-buffer-exists-p gnus-summary-buffer)
2822       (set-buffer gnus-summary-buffer))
2823     (let ((gnus-replied-mark 129)
2824           (gnus-score-below-mark 130)
2825           (gnus-score-over-mark 130)
2826           (gnus-download-mark 131)
2827           (spec gnus-summary-line-format-spec)
2828           gnus-visual pos)
2829       (save-excursion
2830         (gnus-set-work-buffer)
2831         (let ((gnus-summary-line-format-spec spec)
2832               (gnus-newsgroup-downloadable '((0 . t))))
2833           (gnus-summary-insert-line
2834            (make-full-mail-header 0 "" "nobody"
2835                                   "05 Apr 2001 23:33:09 +0400"
2836                                   "" "" 0 0 "" nil)
2837            0 nil 128 t nil "" nil 1)
2838           (goto-char (point-min))
2839           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2840                                              (- (point) 2)))))
2841           (goto-char (point-min))
2842           (push (cons 'replied (and (search-forward "\201" nil t)
2843                                     (- (point) 2)))
2844                 pos)
2845           (goto-char (point-min))
2846           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2847                 pos)
2848           (goto-char (point-min))
2849           (push (cons 'download
2850                       (and (search-forward "\203" nil t) (- (point) 2)))
2851                 pos)))
2852       (setq gnus-summary-mark-positions pos))))
2853
2854 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2855   "Insert a dummy root in the summary buffer."
2856   (beginning-of-line)
2857   (gnus-add-text-properties
2858    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2859    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2860
2861 (defun gnus-summary-from-or-to-or-newsgroups (header)
2862   (let ((to (cdr (assq 'To (mail-header-extra header))))
2863         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2864         (default-mime-charset (with-current-buffer gnus-summary-buffer
2865                                 default-mime-charset)))
2866     (cond
2867      ((and to
2868            gnus-ignored-from-addresses
2869            (string-match gnus-ignored-from-addresses
2870                          (mail-header-from header)))
2871       (concat "-> "
2872               (or (car (funcall gnus-extract-address-components
2873                                 (funcall
2874                                  gnus-decode-encoded-word-function to)))
2875                   (funcall gnus-decode-encoded-word-function to))))
2876      ((and newsgroups
2877            gnus-ignored-from-addresses
2878            (string-match gnus-ignored-from-addresses
2879                          (mail-header-from header)))
2880       (concat "=> " newsgroups))
2881      (t
2882       (or (car (funcall gnus-extract-address-components
2883                         (mail-header-from header)))
2884           (mail-header-from header))))))
2885
2886 (defun gnus-summary-insert-line (gnus-tmp-header
2887                                  gnus-tmp-level gnus-tmp-current
2888                                  gnus-tmp-unread gnus-tmp-replied
2889                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2890                                  &optional gnus-tmp-dummy gnus-tmp-score
2891                                  gnus-tmp-process)
2892   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2893          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2894          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2895          (gnus-tmp-score-char
2896           (if (or (null gnus-summary-default-score)
2897                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2898                       gnus-summary-zcore-fuzz))
2899               ?  ;Whitespace
2900             (if (< gnus-tmp-score gnus-summary-default-score)
2901                 gnus-score-below-mark gnus-score-over-mark)))
2902          (gnus-tmp-replied
2903           (cond (gnus-tmp-process gnus-process-mark)
2904                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2905                  gnus-cached-mark)
2906                 (gnus-tmp-replied gnus-replied-mark)
2907                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2908                  gnus-saved-mark)
2909                 (t gnus-no-mark)))
2910          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2911          (gnus-tmp-name
2912           (cond
2913            ((string-match "<[^>]+> *$" gnus-tmp-from)
2914             (let ((beg (match-beginning 0)))
2915               (or (and (string-match "^\".+\"" gnus-tmp-from)
2916                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2917                   (substring gnus-tmp-from 0 beg))))
2918            ((string-match "(.+)" gnus-tmp-from)
2919             (substring gnus-tmp-from
2920                        (1+ (match-beginning 0)) (1- (match-end 0))))
2921            (t gnus-tmp-from)))
2922          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2923          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2924          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2925          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2926          (buffer-read-only nil))
2927     (when (string= gnus-tmp-name "")
2928       (setq gnus-tmp-name gnus-tmp-from))
2929     (unless (numberp gnus-tmp-lines)
2930       (setq gnus-tmp-lines -1))
2931     (when (= gnus-tmp-lines -1)
2932       (setq gnus-tmp-lines "?"))
2933     (gnus-put-text-property-excluding-characters-with-faces
2934      (point)
2935      (progn (eval gnus-summary-line-format-spec) (point))
2936      'gnus-number gnus-tmp-number)
2937     (when (gnus-visual-p 'summary-highlight 'highlight)
2938       (forward-line -1)
2939       (gnus-run-hooks 'gnus-summary-update-hook)
2940       (forward-line 1))))
2941
2942 (defun gnus-summary-update-line (&optional dont-update)
2943   "Update summary line after change."
2944   (when (and gnus-summary-default-score
2945              (not gnus-summary-inhibit-highlight))
2946     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2947            (article (gnus-summary-article-number))
2948            (score (gnus-summary-article-score article)))
2949       (unless dont-update
2950         (if (and gnus-summary-mark-below
2951                  (< (gnus-summary-article-score)
2952                     gnus-summary-mark-below))
2953             ;; This article has a low score, so we mark it as read.
2954             (when (memq article gnus-newsgroup-unreads)
2955               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2956           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2957             ;; This article was previously marked as read on account
2958             ;; of a low score, but now it has risen, so we mark it as
2959             ;; unread.
2960             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2961         (gnus-summary-update-mark
2962          (if (or (null gnus-summary-default-score)
2963                  (<= (abs (- score gnus-summary-default-score))
2964                      gnus-summary-zcore-fuzz))
2965              ?  ;Whitespace
2966            (if (< score gnus-summary-default-score)
2967                gnus-score-below-mark gnus-score-over-mark))
2968          'score))
2969       ;; Do visual highlighting.
2970       (when (gnus-visual-p 'summary-highlight 'highlight)
2971         (gnus-run-hooks 'gnus-summary-update-hook)))))
2972
2973 (defvar gnus-tmp-new-adopts nil)
2974
2975 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2976   "Return the number of articles in THREAD.
2977 This may be 0 in some cases -- if none of the articles in
2978 the thread are to be displayed."
2979   (let* ((number
2980           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2981           (cond
2982            ((not (listp thread))
2983             1)
2984            ((and (consp thread) (cdr thread))
2985             (apply
2986              '+ 1 (mapcar
2987                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2988            ((null thread)
2989             1)
2990            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2991             1)
2992            (t 0))))
2993     (when (and level (zerop level) gnus-tmp-new-adopts)
2994       (incf number
2995             (apply '+ (mapcar
2996                        'gnus-summary-number-of-articles-in-thread
2997                        gnus-tmp-new-adopts))))
2998     (if char
2999         (if (> number 1) gnus-not-empty-thread-mark
3000           gnus-empty-thread-mark)
3001       number)))
3002
3003 (defun gnus-summary-set-local-parameters (group)
3004   "Go through the local params of GROUP and set all variable specs in that list."
3005   (let ((params (gnus-group-find-parameter group))
3006         (vars '(quit-config)) ; Ignore quit-config.
3007         elem)
3008     (while params
3009       (setq elem (car params)
3010             params (cdr params))
3011       (and (consp elem)                 ; Has to be a cons.
3012            (consp (cdr elem))           ; The cdr has to be a list.
3013            (symbolp (car elem))         ; Has to be a symbol in there.
3014            (not (memq (car elem) vars))
3015            (ignore-errors               ; So we set it.
3016              (push (car elem) vars)
3017              (make-local-variable (car elem))
3018              (set (car elem) (eval (nth 1 elem))))))))
3019
3020 (defun gnus-summary-read-group (group &optional show-all no-article
3021                                       kill-buffer no-display backward
3022                                       select-articles)
3023   "Start reading news in newsgroup GROUP.
3024 If SHOW-ALL is non-nil, already read articles are also listed.
3025 If NO-ARTICLE is non-nil, no article is selected initially.
3026 If NO-DISPLAY, don't generate a summary buffer."
3027   (let (result)
3028     (while (and group
3029                 (null (setq result
3030                             (let ((gnus-auto-select-next nil))
3031                               (or (gnus-summary-read-group-1
3032                                    group show-all no-article
3033                                    kill-buffer no-display
3034                                    select-articles)
3035                                   (setq show-all nil
3036                                         select-articles nil)))))
3037                 (eq gnus-auto-select-next 'quietly))
3038       (set-buffer gnus-group-buffer)
3039       ;; The entry function called above goes to the next
3040       ;; group automatically, so we go two groups back
3041       ;; if we are searching for the previous group.
3042       (when backward
3043         (gnus-group-prev-unread-group 2))
3044       (if (not (equal group (gnus-group-group-name)))
3045           (setq group (gnus-group-group-name))
3046         (setq group nil)))
3047     result))
3048
3049 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3050   "Directly jump to the other GROUP from summary buffer.
3051 If SHOW-ALL is non-nil, already read articles are also listed."
3052   (interactive
3053    (if (eq gnus-summary-buffer (current-buffer))
3054        (list (completing-read
3055               "Group: " gnus-active-hashtb nil t
3056               (when (and gnus-newsgroup-name
3057                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3058                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3059               'gnus-group-history)
3060              current-prefix-arg)
3061      (error "%s must be invoked from a gnus summary buffer." this-command)))
3062   (unless (or (zerop (length group))
3063               (and gnus-newsgroup-name
3064                    (string-equal gnus-newsgroup-name group)))
3065     (gnus-summary-exit)
3066     (gnus-summary-read-group group show-all
3067                              gnus-dont-select-after-jump-to-other-group)))
3068
3069 (defun gnus-summary-read-group-1 (group show-all no-article
3070                                         kill-buffer no-display
3071                                         &optional select-articles)
3072   ;; Killed foreign groups can't be entered.
3073   ;;  (when (and (not (gnus-group-native-p group))
3074   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3075   ;;    (error "Dead non-native groups can't be entered"))
3076   (gnus-message 5 "Retrieving newsgroup: %s..."
3077                 (gnus-group-decoded-name group))
3078   (let* ((new-group (gnus-summary-setup-buffer group))
3079          (quit-config (gnus-group-quit-config group))
3080          (did-select (and new-group (gnus-select-newsgroup
3081                                      group show-all select-articles))))
3082     (cond
3083      ;; This summary buffer exists already, so we just select it.
3084      ((not new-group)
3085       (gnus-set-global-variables)
3086       (when kill-buffer
3087         (gnus-kill-or-deaden-summary kill-buffer))
3088       (gnus-configure-windows 'summary 'force)
3089       (gnus-set-mode-line 'summary)
3090       (gnus-summary-position-point)
3091       (message "")
3092       t)
3093      ;; We couldn't select this group.
3094      ((null did-select)
3095       (when (and (eq major-mode 'gnus-summary-mode)
3096                  (not (equal (current-buffer) kill-buffer)))
3097         (kill-buffer (current-buffer))
3098         (if (not quit-config)
3099             (progn
3100               ;; Update the info -- marks might need to be removed,
3101               ;; for instance.
3102               (gnus-summary-update-info)
3103               (set-buffer gnus-group-buffer)
3104               (gnus-group-jump-to-group group)
3105               (gnus-group-next-unread-group 1))
3106           (gnus-handle-ephemeral-exit quit-config)))
3107       (let ((grpinfo (gnus-get-info group)))
3108         (if (null (gnus-info-read grpinfo))
3109             (gnus-message 3 "Group %s contains no messages" 
3110                           (gnus-group-decoded-name group))
3111           (gnus-message 3 "Can't select group")))
3112       nil)
3113      ;; The user did a `C-g' while prompting for number of articles,
3114      ;; so we exit this group.
3115      ((eq did-select 'quit)
3116       (and (eq major-mode 'gnus-summary-mode)
3117            (not (equal (current-buffer) kill-buffer))
3118            (kill-buffer (current-buffer)))
3119       (when kill-buffer
3120         (gnus-kill-or-deaden-summary kill-buffer))
3121       (if (not quit-config)
3122           (progn
3123             (set-buffer gnus-group-buffer)
3124             (gnus-group-jump-to-group group)
3125             (gnus-group-next-unread-group 1)
3126             (gnus-configure-windows 'group 'force))
3127         (gnus-handle-ephemeral-exit quit-config))
3128       ;; Finally signal the quit.
3129       (signal 'quit nil))
3130      ;; The group was successfully selected.
3131      (t
3132       (gnus-set-global-variables)
3133       ;; Save the active value in effect when the group was entered.
3134       (setq gnus-newsgroup-active
3135             (gnus-copy-sequence
3136              (gnus-active gnus-newsgroup-name)))
3137       ;; You can change the summary buffer in some way with this hook.
3138       (gnus-run-hooks 'gnus-select-group-hook)
3139       (gnus-update-format-specifications
3140        nil 'summary 'summary-mode 'summary-dummy)
3141       (gnus-update-summary-mark-positions)
3142       ;; Do score processing.
3143       (when gnus-use-scoring
3144         (gnus-possibly-score-headers))
3145       ;; Check whether to fill in the gaps in the threads.
3146       (when gnus-build-sparse-threads
3147         (gnus-build-sparse-threads))
3148       ;; Find the initial limit.
3149       (if gnus-show-threads
3150           (if show-all
3151               (let ((gnus-newsgroup-dormant nil))
3152                 (gnus-summary-initial-limit show-all))
3153             (gnus-summary-initial-limit show-all))
3154         ;; When untreaded, all articles are always shown.
3155         (setq gnus-newsgroup-limit
3156               (mapcar
3157                (lambda (header) (mail-header-number header))
3158                gnus-newsgroup-headers)))
3159       ;; Generate the summary buffer.
3160       (unless no-display
3161         (gnus-summary-prepare))
3162       (when gnus-use-trees
3163         (gnus-tree-open group)
3164         (setq gnus-summary-highlight-line-function
3165               'gnus-tree-highlight-article))
3166       ;; If the summary buffer is empty, but there are some low-scored
3167       ;; articles or some excluded dormants, we include these in the
3168       ;; buffer.
3169       (when (and (zerop (buffer-size))
3170                  (not no-display))
3171         (cond (gnus-newsgroup-dormant
3172                (gnus-summary-limit-include-dormant))
3173               ((and gnus-newsgroup-scored show-all)
3174                (gnus-summary-limit-include-expunged t))))
3175       ;; Function `gnus-apply-kill-file' must be called in this hook.
3176       (gnus-run-hooks 'gnus-apply-kill-hook)
3177       (if (and (zerop (buffer-size))
3178                (not no-display))
3179           (progn
3180             ;; This newsgroup is empty.
3181             (gnus-summary-catchup-and-exit nil t)
3182             (gnus-message 6 "No unread news")
3183             (when kill-buffer
3184               (gnus-kill-or-deaden-summary kill-buffer))
3185             ;; Return nil from this function.
3186             nil)
3187         ;; Hide conversation thread subtrees.  We cannot do this in
3188         ;; gnus-summary-prepare-hook since kill processing may not
3189         ;; work with hidden articles.
3190         (and gnus-show-threads
3191              gnus-thread-hide-subtree
3192              (gnus-summary-hide-all-threads))
3193         (when kill-buffer
3194           (gnus-kill-or-deaden-summary kill-buffer))
3195         ;; Show first unread article if requested.
3196         (if (and (not no-article)
3197                  (not no-display)
3198                  gnus-newsgroup-unreads
3199                  gnus-auto-select-first)
3200             (progn
3201               (gnus-configure-windows 'summary)
3202               (cond
3203                ((eq gnus-auto-select-first 'best)
3204                 (gnus-summary-best-unread-article))
3205                ((eq gnus-auto-select-first t)
3206                 (gnus-summary-first-unread-article))
3207                ((gnus-functionp gnus-auto-select-first)
3208                 (funcall gnus-auto-select-first))))
3209           ;; Don't select any articles, just move point to the first
3210           ;; article in the group.
3211           (goto-char (point-min))
3212           (gnus-summary-position-point)
3213           (gnus-configure-windows 'summary 'force)
3214           (gnus-set-mode-line 'summary))
3215         (when (get-buffer-window gnus-group-buffer t)
3216           ;; Gotta use windows, because recenter does weird stuff if
3217           ;; the current buffer ain't the displayed window.
3218           (let ((owin (selected-window)))
3219             (select-window (get-buffer-window gnus-group-buffer t))
3220             (when (gnus-group-goto-group group)
3221               (recenter))
3222             (select-window owin)))
3223         ;; Mark this buffer as "prepared".
3224         (setq gnus-newsgroup-prepared t)
3225         (gnus-run-hooks 'gnus-summary-prepared-hook)
3226         t)))))
3227
3228 (defun gnus-summary-prepare ()
3229   "Generate the summary buffer."
3230   (interactive)
3231   (let ((buffer-read-only nil))
3232     (erase-buffer)
3233     (setq gnus-newsgroup-data nil
3234           gnus-newsgroup-data-reverse nil)
3235     (gnus-run-hooks 'gnus-summary-generate-hook)
3236     ;; Generate the buffer, either with threads or without.
3237     (when gnus-newsgroup-headers
3238       (gnus-summary-prepare-threads
3239        (if gnus-show-threads
3240            (gnus-sort-gathered-threads
3241             (funcall gnus-summary-thread-gathering-function
3242                      (gnus-sort-threads
3243                       (gnus-cut-threads (gnus-make-threads)))))
3244          ;; Unthreaded display.
3245          (gnus-sort-articles gnus-newsgroup-headers))))
3246     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3247     ;; Call hooks for modifying summary buffer.
3248     (goto-char (point-min))
3249     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3250
3251 (defsubst gnus-general-simplify-subject (subject)
3252   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3253   (setq subject
3254         (cond
3255          ;; Truncate the subject.
3256          (gnus-simplify-subject-functions
3257           (gnus-map-function gnus-simplify-subject-functions subject))
3258          ((numberp gnus-summary-gather-subject-limit)
3259           (setq subject (gnus-simplify-subject-re subject))
3260           (if (> (length subject) gnus-summary-gather-subject-limit)
3261               (substring subject 0 gnus-summary-gather-subject-limit)
3262             subject))
3263          ;; Fuzzily simplify it.
3264          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3265           (gnus-simplify-subject-fuzzy subject))
3266          ;; Just remove the leading "Re:".
3267          (t
3268           (gnus-simplify-subject-re subject))))
3269
3270   (if (and gnus-summary-gather-exclude-subject
3271            (string-match gnus-summary-gather-exclude-subject subject))
3272       nil                               ; This article shouldn't be gathered
3273     subject))
3274
3275 (defun gnus-summary-simplify-subject-query ()
3276   "Query where the respool algorithm would put this article."
3277   (interactive)
3278   (gnus-summary-select-article)
3279   (message "%s"
3280            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3281
3282 (defun gnus-gather-threads-by-subject (threads)
3283   "Gather threads by looking at Subject headers."
3284   (if (not gnus-summary-make-false-root)
3285       threads
3286     (let ((hashtb (gnus-make-hashtable 1024))
3287           (prev threads)
3288           (result threads)
3289           subject hthread whole-subject)
3290       (while threads
3291         (setq subject (gnus-general-simplify-subject
3292                        (setq whole-subject (mail-header-subject
3293                                             (caar threads)))))
3294         (when subject
3295           (if (setq hthread (gnus-gethash subject hashtb))
3296               (progn
3297                 ;; We enter a dummy root into the thread, if we
3298                 ;; haven't done that already.
3299                 (unless (stringp (caar hthread))
3300                   (setcar hthread (list whole-subject (car hthread))))
3301                 ;; We add this new gathered thread to this gathered
3302                 ;; thread.
3303                 (setcdr (car hthread)
3304                         (nconc (cdar hthread) (list (car threads))))
3305                 ;; Remove it from the list of threads.
3306                 (setcdr prev (cdr threads))
3307                 (setq threads prev))
3308             ;; Enter this thread into the hash table.
3309             (gnus-sethash subject threads hashtb)))
3310         (setq prev threads)
3311         (setq threads (cdr threads)))
3312       result)))
3313
3314 (defun gnus-gather-threads-by-references (threads)
3315   "Gather threads by looking at References headers."
3316   (let ((idhashtb (gnus-make-hashtable 1024))
3317         (thhashtb (gnus-make-hashtable 1024))
3318         (prev threads)
3319         (result threads)
3320         ids references id gthread gid entered ref)
3321     (while threads
3322       (when (setq references (mail-header-references (caar threads)))
3323         (setq id (mail-header-id (caar threads))
3324               ids (gnus-split-references references)
3325               entered nil)
3326         (while (setq ref (pop ids))
3327           (setq ids (delete ref ids))
3328           (if (not (setq gid (gnus-gethash ref idhashtb)))
3329               (progn
3330                 (gnus-sethash ref id idhashtb)
3331                 (gnus-sethash id threads thhashtb))
3332             (setq gthread (gnus-gethash gid thhashtb))
3333             (unless entered
3334               ;; We enter a dummy root into the thread, if we
3335               ;; haven't done that already.
3336               (unless (stringp (caar gthread))
3337                 (setcar gthread (list (mail-header-subject (caar gthread))
3338                                       (car gthread))))
3339               ;; We add this new gathered thread to this gathered
3340               ;; thread.
3341               (setcdr (car gthread)
3342                       (nconc (cdar gthread) (list (car threads)))))
3343             ;; Add it into the thread hash table.
3344             (gnus-sethash id gthread thhashtb)
3345             (setq entered t)
3346             ;; Remove it from the list of threads.
3347             (setcdr prev (cdr threads))
3348             (setq threads prev))))
3349       (setq prev threads)
3350       (setq threads (cdr threads)))
3351     result))
3352
3353 (defun gnus-sort-gathered-threads (threads)
3354   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3355   (let ((result threads))
3356     (while threads
3357       (when (stringp (caar threads))
3358         (setcdr (car threads)
3359                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3360       (setq threads (cdr threads)))
3361     result))
3362
3363 (defun gnus-thread-loop-p (root thread)
3364   "Say whether ROOT is in THREAD."
3365   (let ((stack (list thread))
3366         (infloop 0)
3367         th)
3368     (while (setq thread (pop stack))
3369       (setq th (cdr thread))
3370       (while (and th
3371                   (not (eq (caar th) root)))
3372         (pop th))
3373       (if th
3374           ;; We have found a loop.
3375           (let (ref-dep)
3376             (setcdr thread (delq (car th) (cdr thread)))
3377             (if (boundp (setq ref-dep (intern "none"
3378                                               gnus-newsgroup-dependencies)))
3379                 (setcdr (symbol-value ref-dep)
3380                         (nconc (cdr (symbol-value ref-dep))
3381                                (list (car th))))
3382               (set ref-dep (list nil (car th))))
3383             (setq infloop 1
3384                   stack nil))
3385         ;; Push all the subthreads onto the stack.
3386         (push (cdr thread) stack)))
3387     infloop))
3388
3389 (defun gnus-make-threads ()
3390   "Go through the dependency hashtb and find the roots.  Return all threads."
3391   (let (threads)
3392     (while (catch 'infloop
3393              (mapatoms
3394               (lambda (refs)
3395                 ;; Deal with self-referencing References loops.
3396                 (when (and (car (symbol-value refs))
3397                            (not (zerop
3398                                  (apply
3399                                   '+
3400                                   (mapcar
3401                                    (lambda (thread)
3402                                      (gnus-thread-loop-p
3403                                       (car (symbol-value refs)) thread))
3404                                    (cdr (symbol-value refs)))))))
3405                   (setq threads nil)
3406                   (throw 'infloop t))
3407                 (unless (car (symbol-value refs))
3408                   ;; These threads do not refer back to any other articles,
3409                   ;; so they're roots.
3410                   (setq threads (append (cdr (symbol-value refs)) threads))))
3411               gnus-newsgroup-dependencies)))
3412     threads))
3413
3414 ;; Build the thread tree.
3415 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3416   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3417
3418 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3419 if it was already present.
3420
3421 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3422 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3423 Message-IDs will be renamed be renamed to a unique Message-ID before
3424 being entered.
3425
3426 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3427   (let* ((id (mail-header-id header))
3428          (id-dep (and id (intern id dependencies)))
3429          ref ref-dep ref-header)
3430     ;; Enter this `header' in the `dependencies' table.
3431     (cond
3432      ((not id-dep)
3433       (setq header nil))
3434      ;; The first two cases do the normal part: enter a new `header'
3435      ;; in the `dependencies' table.
3436      ((not (boundp id-dep))
3437       (set id-dep (list header)))
3438      ((null (car (symbol-value id-dep)))
3439       (setcar (symbol-value id-dep) header))
3440
3441      ;; From here the `header' was already present in the
3442      ;; `dependencies' table.
3443      (force-new
3444       ;; Overrides an existing entry;
3445       ;; just set the header part of the entry.
3446       (setcar (symbol-value id-dep) header))
3447
3448      ;; Renames the existing `header' to a unique Message-ID.
3449      ((not gnus-summary-ignore-duplicates)
3450       ;; An article with this Message-ID has already been seen.
3451       ;; We rename the Message-ID.
3452       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3453            (list header))
3454       (mail-header-set-id header id))
3455
3456      ;; The last case ignores an existing entry, except it adds any
3457      ;; additional Xrefs (in case the two articles came from different
3458      ;; servers.
3459      ;; Also sets `header' to `nil' meaning that the `dependencies'
3460      ;; table was *not* modified.
3461      (t
3462       (mail-header-set-xref
3463        (car (symbol-value id-dep))
3464        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3465                    "")
3466                (or (mail-header-xref header) "")))
3467       (setq header nil)))
3468
3469     (when header
3470       ;; First check if that we are not creating a References loop.
3471       (setq ref (gnus-parent-id (mail-header-references header)))
3472       (while (and ref
3473                   (setq ref-dep (intern-soft ref dependencies))
3474                   (boundp ref-dep)
3475                   (setq ref-header (car (symbol-value ref-dep))))
3476         (if (string= id ref)
3477             ;; Yuk!  This is a reference loop.  Make the article be a
3478             ;; root article.
3479             (progn
3480               (mail-header-set-references (car (symbol-value id-dep)) "none")
3481               (setq ref nil))
3482           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3483       (setq ref (gnus-parent-id (mail-header-references header)))
3484       (setq ref-dep (intern (or ref "none") dependencies))
3485       (if (boundp ref-dep)
3486           (setcdr (symbol-value ref-dep)
3487                   (nconc (cdr (symbol-value ref-dep))
3488                          (list (symbol-value id-dep))))
3489         (set ref-dep (list nil (symbol-value id-dep)))))
3490     header))
3491
3492 (defun gnus-build-sparse-threads ()
3493   (let ((headers gnus-newsgroup-headers)
3494         (mail-parse-charset gnus-newsgroup-charset)
3495         (gnus-summary-ignore-duplicates t)
3496         header references generation relations
3497         subject child end new-child date)
3498     ;; First we create an alist of generations/relations, where
3499     ;; generations is how much we trust the relation, and the relation
3500     ;; is parent/child.
3501     (gnus-message 7 "Making sparse threads...")
3502     (save-excursion
3503       (nnheader-set-temp-buffer " *gnus sparse threads*")
3504       (while (setq header (pop headers))
3505         (when (and (setq references (mail-header-references header))
3506                    (not (string= references "")))
3507           (insert references)
3508           (setq child (mail-header-id header)
3509                 subject (mail-header-subject header)
3510                 date (mail-header-date header)
3511                 generation 0)
3512           (while (search-backward ">" nil t)
3513             (setq end (1+ (point)))
3514             (when (search-backward "<" nil t)
3515               (setq new-child (buffer-substring (point) end))
3516               (push (list (incf generation)
3517                           child (setq child new-child)
3518                           subject date)
3519                     relations)))
3520           (when child
3521             (push (list (1+ generation) child nil subject) relations))
3522           (erase-buffer)))
3523       (kill-buffer (current-buffer)))
3524     ;; Sort over trustworthiness.
3525     (mapcar
3526      (lambda (relation)
3527        (when (gnus-dependencies-add-header
3528               (make-full-mail-header-from-decoded-header
3529                gnus-reffed-article-number
3530                (nth 3 relation) "" (or (nth 4 relation) "")
3531                (nth 1 relation)
3532                (or (nth 2 relation) "") 0 0 "")
3533               gnus-newsgroup-dependencies nil)
3534          (push gnus-reffed-article-number gnus-newsgroup-limit)
3535          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3536          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3537                gnus-newsgroup-reads)
3538          (decf gnus-reffed-article-number)))
3539      (sort relations 'car-less-than-car))
3540     (gnus-message 7 "Making sparse threads...done")))
3541
3542 (defun gnus-build-old-threads ()
3543   ;; Look at all the articles that refer back to old articles, and
3544   ;; fetch the headers for the articles that aren't there.  This will
3545   ;; build complete threads - if the roots haven't been expired by the
3546   ;; server, that is.
3547   (let ((mail-parse-charset gnus-newsgroup-charset)
3548         id heads)
3549     (mapatoms
3550      (lambda (refs)
3551        (when (not (car (symbol-value refs)))
3552          (setq heads (cdr (symbol-value refs)))
3553          (while heads
3554            (if (memq (mail-header-number (caar heads))
3555                      gnus-newsgroup-dormant)
3556                (setq heads (cdr heads))
3557              (setq id (symbol-name refs))
3558              (while (and (setq id (gnus-build-get-header id))
3559                          (not (car (gnus-id-to-thread id)))))
3560              (setq heads nil)))))
3561      gnus-newsgroup-dependencies)))
3562
3563 ;; This function has to be called with point after the article number
3564 ;; on the beginning of the line.
3565 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3566   (let ((eol (gnus-point-at-eol))
3567         (buffer (current-buffer))
3568         header)
3569
3570     ;; overview: [num subject from date id refs chars lines misc]
3571     (unwind-protect
3572         (progn
3573           (narrow-to-region (point) eol)
3574           (unless (eobp)
3575             (forward-char))
3576
3577           (setq header
3578                 (make-full-mail-header
3579                  number                         ; number
3580                  (nnheader-nov-field)           ; subject
3581                  (nnheader-nov-field)           ; from
3582                  (nnheader-nov-field)           ; date
3583                  (nnheader-nov-read-message-id) ; id
3584                  (nnheader-nov-field)           ; refs
3585                  (nnheader-nov-read-integer)    ; chars
3586                  (nnheader-nov-read-integer)    ; lines
3587                  (unless (eobp)
3588                    (if (looking-at "Xref: ")
3589                        (goto-char (match-end 0)))
3590                    (nnheader-nov-field))        ; Xref
3591                  (nnheader-nov-parse-extra))))  ; extra
3592
3593       (widen))
3594
3595     (when gnus-alter-header-function
3596       (funcall gnus-alter-header-function header))
3597     (gnus-dependencies-add-header header dependencies force-new)))
3598
3599 (defun gnus-build-get-header (id)
3600   "Look through the buffer of NOV lines and find the header to ID.
3601 Enter this line into the dependencies hash table, and return
3602 the id of the parent article (if any)."
3603   (let ((deps gnus-newsgroup-dependencies)
3604         found header)
3605     (prog1
3606         (save-excursion
3607           (set-buffer nntp-server-buffer)
3608           (let ((case-fold-search nil))
3609             (goto-char (point-min))
3610             (while (and (not found)
3611                         (search-forward id nil t))
3612               (beginning-of-line)
3613               (setq found (looking-at
3614                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3615                                    (regexp-quote id))))
3616               (or found (beginning-of-line 2)))
3617             (when found
3618               (beginning-of-line)
3619               (and
3620                (setq header (gnus-nov-parse-line
3621                              (read (current-buffer)) deps))
3622                (gnus-parent-id (mail-header-references header))))))
3623       (when header
3624         (let ((number (mail-header-number header)))
3625           (push number gnus-newsgroup-limit)
3626           (push header gnus-newsgroup-headers)
3627           (if (memq number gnus-newsgroup-unselected)
3628               (progn
3629                 (push number gnus-newsgroup-unreads)
3630                 (setq gnus-newsgroup-unselected
3631                       (delq number gnus-newsgroup-unselected)))
3632             (push number gnus-newsgroup-ancient)))))))
3633
3634 (defun gnus-build-all-threads ()
3635   "Read all the headers."
3636   (let ((gnus-summary-ignore-duplicates t)
3637         (mail-parse-charset gnus-newsgroup-charset)
3638         (dependencies gnus-newsgroup-dependencies)
3639         header article)
3640     (save-excursion
3641       (set-buffer nntp-server-buffer)
3642       (let ((case-fold-search nil))
3643         (goto-char (point-min))
3644         (while (not (eobp))
3645           (ignore-errors
3646             (setq article (read (current-buffer))
3647                   header (gnus-nov-parse-line article dependencies)))
3648           (when header
3649             (save-excursion
3650               (set-buffer gnus-summary-buffer)
3651               (push header gnus-newsgroup-headers)
3652               (if (memq (setq article (mail-header-number header))
3653                         gnus-newsgroup-unselected)
3654                   (progn
3655                     (push article gnus-newsgroup-unreads)
3656                     (setq gnus-newsgroup-unselected
3657                           (delq article gnus-newsgroup-unselected)))
3658                 (push article gnus-newsgroup-ancient)))
3659             (forward-line 1)))))))
3660
3661 (defun gnus-summary-update-article-line (article header)
3662   "Update the line for ARTICLE using HEADERS."
3663   (let* ((id (mail-header-id header))
3664          (thread (gnus-id-to-thread id)))
3665     (unless thread
3666       (error "Article in no thread"))
3667     ;; Update the thread.
3668     (setcar thread header)
3669     (gnus-summary-goto-subject article)
3670     (let* ((datal (gnus-data-find-list article))
3671            (data (car datal))
3672            (length (when (cdr datal)
3673                      (- (gnus-data-pos data)
3674                         (gnus-data-pos (cadr datal)))))
3675            (buffer-read-only nil)
3676            (level (gnus-summary-thread-level)))
3677       (gnus-delete-line)
3678       (gnus-summary-insert-line
3679        header level nil (gnus-article-mark article)
3680        (memq article gnus-newsgroup-replied)
3681        (memq article gnus-newsgroup-expirable)
3682        ;; Only insert the Subject string when it's different
3683        ;; from the previous Subject string.
3684        (if (and
3685             gnus-show-threads
3686             (gnus-subject-equal
3687              (condition-case ()
3688                  (mail-header-subject
3689                   (gnus-data-header
3690                    (cadr
3691                     (gnus-data-find-list
3692                      article
3693                      (gnus-data-list t)))))
3694                ;; Error on the side of excessive subjects.
3695                (error ""))
3696              (mail-header-subject header)))
3697            ""
3698          (mail-header-subject header))
3699        nil (cdr (assq article gnus-newsgroup-scored))
3700        (memq article gnus-newsgroup-processable))
3701       (when length
3702         (gnus-data-update-list
3703          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3704
3705 (defun gnus-summary-update-article (article &optional iheader)
3706   "Update ARTICLE in the summary buffer."
3707   (set-buffer gnus-summary-buffer)
3708   (let* ((header (gnus-summary-article-header article))
3709          (id (mail-header-id header))
3710          (data (gnus-data-find article))
3711          (thread (gnus-id-to-thread id))
3712          (references (mail-header-references header))
3713          (parent
3714           (gnus-id-to-thread
3715            (or (gnus-parent-id
3716                 (when (and references
3717                            (not (equal "" references)))
3718                   references))
3719                "none")))
3720          (buffer-read-only nil)
3721          (old (car thread)))
3722     (when thread
3723       (unless iheader
3724         (setcar thread nil)
3725         (when parent
3726           (delq thread parent)))
3727       (if (gnus-summary-insert-subject id header)
3728           ;; Set the (possibly) new article number in the data structure.
3729           (gnus-data-set-number data (gnus-id-to-article id))
3730         (setcar thread old)
3731         nil))))
3732
3733 (defun gnus-rebuild-thread (id &optional line)
3734   "Rebuild the thread containing ID.
3735 If LINE, insert the rebuilt thread starting on line LINE."
3736   (let ((buffer-read-only nil)
3737         old-pos current thread data)
3738     (if (not gnus-show-threads)
3739         (setq thread (list (car (gnus-id-to-thread id))))
3740       ;; Get the thread this article is part of.
3741       (setq thread (gnus-remove-thread id)))
3742     (setq old-pos (gnus-point-at-bol))
3743     (setq current (save-excursion
3744                     (and (re-search-backward "[\r\n]" nil t)
3745                          (gnus-summary-article-number))))
3746     ;; If this is a gathered thread, we have to go some re-gathering.
3747     (when (stringp (car thread))
3748       (let ((subject (car thread))
3749             roots thr)
3750         (setq thread (cdr thread))
3751         (while thread
3752           (unless (memq (setq thr (gnus-id-to-thread
3753                                    (gnus-root-id
3754                                     (mail-header-id (caar thread)))))
3755                         roots)
3756             (push thr roots))
3757           (setq thread (cdr thread)))
3758         ;; We now have all (unique) roots.
3759         (if (= (length roots) 1)
3760             ;; All the loose roots are now one solid root.
3761             (setq thread (car roots))
3762           (setq thread (cons subject (gnus-sort-threads roots))))))
3763     (let (threads)
3764       ;; We then insert this thread into the summary buffer.
3765       (when line
3766         (goto-char (point-min))
3767         (forward-line (1- line)))
3768       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3769         (if gnus-show-threads
3770             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3771           (gnus-summary-prepare-unthreaded thread))
3772         (setq data (nreverse gnus-newsgroup-data))
3773         (setq threads gnus-newsgroup-threads))
3774       ;; We splice the new data into the data structure.
3775       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3776       ;;!!! then we want to insert at the beginning of the buffer.
3777       ;;!!! That happens to be true with Gnus now, but that may
3778       ;;!!! change in the future.  Perhaps.
3779       (gnus-data-enter-list
3780        (if line nil current) data (- (point) old-pos))
3781       (setq gnus-newsgroup-threads
3782             (nconc threads gnus-newsgroup-threads))
3783       (gnus-data-compute-positions))))
3784
3785 (defun gnus-number-to-header (number)
3786   "Return the header for article NUMBER."
3787   (let ((headers gnus-newsgroup-headers))
3788     (while (and headers
3789                 (not (= number (mail-header-number (car headers)))))
3790       (pop headers))
3791     (when headers
3792       (car headers))))
3793
3794 (defun gnus-parent-headers (in-headers &optional generation)
3795   "Return the headers of the GENERATIONeth parent of HEADERS."
3796   (unless generation
3797     (setq generation 1))
3798   (let ((parent t)
3799         (headers in-headers)
3800         references)
3801     (while (and parent
3802                 (not (zerop generation))
3803                 (setq references (mail-header-references headers)))
3804       (setq headers (if (and references
3805                              (setq parent (gnus-parent-id references)))
3806                         (car (gnus-id-to-thread parent))
3807                       nil))
3808       (decf generation))
3809     (and (not (eq headers in-headers))
3810          headers)))
3811
3812 (defun gnus-id-to-thread (id)
3813   "Return the (sub-)thread where ID appears."
3814   (gnus-gethash id gnus-newsgroup-dependencies))
3815
3816 (defun gnus-id-to-article (id)
3817   "Return the article number of ID."
3818   (let ((thread (gnus-id-to-thread id)))
3819     (when (and thread
3820                (car thread))
3821       (mail-header-number (car thread)))))
3822
3823 (defun gnus-id-to-header (id)
3824   "Return the article headers of ID."
3825   (car (gnus-id-to-thread id)))
3826
3827 (defun gnus-article-displayed-root-p (article)
3828   "Say whether ARTICLE is a root(ish) article."
3829   (let ((level (gnus-summary-thread-level article))
3830         (refs (mail-header-references  (gnus-summary-article-header article)))
3831         particle)
3832     (cond
3833      ((null level) nil)
3834      ((zerop level) t)
3835      ((null refs) t)
3836      ((null (gnus-parent-id refs)) t)
3837      ((and (= 1 level)
3838            (null (setq particle (gnus-id-to-article
3839                                  (gnus-parent-id refs))))
3840            (null (gnus-summary-thread-level particle)))))))
3841
3842 (defun gnus-root-id (id)
3843   "Return the id of the root of the thread where ID appears."
3844   (let (last-id prev)
3845     (while (and id (setq prev (car (gnus-id-to-thread id))))
3846       (setq last-id id
3847             id (gnus-parent-id (mail-header-references prev))))
3848     last-id))
3849
3850 (defun gnus-articles-in-thread (thread)
3851   "Return the list of articles in THREAD."
3852   (cons (mail-header-number (car thread))
3853         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3854
3855 (defun gnus-remove-thread (id &optional dont-remove)
3856   "Remove the thread that has ID in it."
3857   (let (headers thread last-id)
3858     ;; First go up in this thread until we find the root.
3859     (setq last-id (gnus-root-id id)
3860           headers (message-flatten-list (gnus-id-to-thread last-id)))
3861     ;; We have now found the real root of this thread.  It might have
3862     ;; been gathered into some loose thread, so we have to search
3863     ;; through the threads to find the thread we wanted.
3864     (let ((threads gnus-newsgroup-threads)
3865           sub)
3866       (while threads
3867         (setq sub (car threads))
3868         (if (stringp (car sub))
3869             ;; This is a gathered thread, so we look at the roots
3870             ;; below it to find whether this article is in this
3871             ;; gathered root.
3872             (progn
3873               (setq sub (cdr sub))
3874               (while sub
3875                 (when (member (caar sub) headers)
3876                   (setq thread (car threads)
3877                         threads nil
3878                         sub nil))
3879                 (setq sub (cdr sub))))
3880           ;; It's an ordinary thread, so we check it.
3881           (when (eq (car sub) (car headers))
3882             (setq thread sub
3883                   threads nil)))
3884         (setq threads (cdr threads)))
3885       ;; If this article is in no thread, then it's a root.
3886       (if thread
3887           (unless dont-remove
3888             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3889         (setq thread (gnus-id-to-thread last-id)))
3890       (when thread
3891         (prog1
3892             thread                      ; We return this thread.
3893           (unless dont-remove
3894             (if (stringp (car thread))
3895                 (progn
3896                   ;; If we use dummy roots, then we have to remove the
3897                   ;; dummy root as well.
3898                   (when (eq gnus-summary-make-false-root 'dummy)
3899                     ;; We go to the dummy root by going to
3900                     ;; the first sub-"thread", and then one line up.
3901                     (gnus-summary-goto-article
3902                      (mail-header-number (caadr thread)))
3903                     (forward-line -1)
3904                     (gnus-delete-line)
3905                     (gnus-data-compute-positions))
3906                   (setq thread (cdr thread))
3907                   (while thread
3908                     (gnus-remove-thread-1 (car thread))
3909                     (setq thread (cdr thread))))
3910               (gnus-remove-thread-1 thread))))))))
3911
3912 (defun gnus-remove-thread-1 (thread)
3913   "Remove the thread THREAD recursively."
3914   (let ((number (mail-header-number (pop thread)))
3915         d)
3916     (setq thread (reverse thread))
3917     (while thread
3918       (gnus-remove-thread-1 (pop thread)))
3919     (when (setq d (gnus-data-find number))
3920       (goto-char (gnus-data-pos d))
3921       (gnus-summary-show-thread)
3922       (gnus-data-remove
3923        number
3924        (- (gnus-point-at-bol)
3925           (prog1
3926               (1+ (gnus-point-at-eol))
3927             (gnus-delete-line)))))))
3928
3929 (defun gnus-sort-threads-1 (threads func)
3930   (sort (mapcar (lambda (thread)
3931                   (cons (car thread)
3932                         (and (cdr thread)
3933                              (gnus-sort-threads-1 (cdr thread) func))))
3934                 threads) func))
3935
3936 (defun gnus-sort-threads (threads)
3937   "Sort THREADS."
3938   (if (not gnus-thread-sort-functions)
3939       threads
3940     (gnus-message 8 "Sorting threads...")
3941     (prog1
3942         (gnus-sort-threads-1
3943          threads
3944          (gnus-make-sort-function gnus-thread-sort-functions))
3945       (gnus-message 8 "Sorting threads...done"))))
3946
3947 (defun gnus-sort-articles (articles)
3948   "Sort ARTICLES."
3949   (when gnus-article-sort-functions
3950     (gnus-message 7 "Sorting articles...")
3951     (prog1
3952         (setq gnus-newsgroup-headers
3953               (sort articles (gnus-make-sort-function
3954                               gnus-article-sort-functions)))
3955       (gnus-message 7 "Sorting articles...done"))))
3956
3957 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3958 (defmacro gnus-thread-header (thread)
3959   "Return header of first article in THREAD.
3960 Note that THREAD must never, ever be anything else than a variable -
3961 using some other form will lead to serious barfage."
3962   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3963   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3964   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3965         (vector thread) 2))
3966
3967 (defsubst gnus-article-sort-by-number (h1 h2)
3968   "Sort articles by article number."
3969   (< (mail-header-number h1)
3970      (mail-header-number h2)))
3971
3972 (defun gnus-thread-sort-by-number (h1 h2)
3973   "Sort threads by root article number."
3974   (gnus-article-sort-by-number
3975    (gnus-thread-header h1) (gnus-thread-header h2)))
3976
3977 (defsubst gnus-article-sort-by-lines (h1 h2)
3978   "Sort articles by article Lines header."
3979   (< (mail-header-lines h1)
3980      (mail-header-lines h2)))
3981
3982 (defun gnus-thread-sort-by-lines (h1 h2)
3983   "Sort threads by root article Lines header."
3984   (gnus-article-sort-by-lines
3985    (gnus-thread-header h1) (gnus-thread-header h2)))
3986
3987 (defsubst gnus-article-sort-by-chars (h1 h2)
3988   "Sort articles by octet length."
3989   (< (mail-header-chars h1)
3990      (mail-header-chars h2)))
3991
3992 (defun gnus-thread-sort-by-chars (h1 h2)
3993   "Sort threads by root article octet length."
3994   (gnus-article-sort-by-chars
3995    (gnus-thread-header h1) (gnus-thread-header h2)))
3996
3997 (defsubst gnus-article-sort-by-author (h1 h2)
3998   "Sort articles by root author."
3999   (string-lessp
4000    (let ((addr (car (mime-entity-read-field h1 'From))))
4001      (or (std11-full-name-string addr)
4002          (std11-address-string addr)
4003          ""))
4004    (let ((addr (car (mime-entity-read-field h2 'From))))
4005      (or (std11-full-name-string addr)
4006          (std11-address-string addr)
4007          ""))
4008    ))
4009
4010 (defun gnus-thread-sort-by-author (h1 h2)
4011   "Sort threads by root author."
4012   (gnus-article-sort-by-author
4013    (gnus-thread-header h1)  (gnus-thread-header h2)))
4014
4015 (defsubst gnus-article-sort-by-subject (h1 h2)
4016   "Sort articles by root subject."
4017   (string-lessp
4018    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4019    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4020
4021 (defun gnus-thread-sort-by-subject (h1 h2)
4022   "Sort threads by root subject."
4023   (gnus-article-sort-by-subject
4024    (gnus-thread-header h1) (gnus-thread-header h2)))
4025
4026 (defsubst gnus-article-sort-by-date (h1 h2)
4027   "Sort articles by root article date."
4028   (time-less-p
4029    (gnus-date-get-time (mail-header-date h1))
4030    (gnus-date-get-time (mail-header-date h2))))
4031
4032 (defun gnus-thread-sort-by-date (h1 h2)
4033   "Sort threads by root article date."
4034   (gnus-article-sort-by-date
4035    (gnus-thread-header h1) (gnus-thread-header h2)))
4036
4037 (defsubst gnus-article-sort-by-score (h1 h2)
4038   "Sort articles by root article score.
4039 Unscored articles will be counted as having a score of zero."
4040   (> (or (cdr (assq (mail-header-number h1)
4041                     gnus-newsgroup-scored))
4042          gnus-summary-default-score 0)
4043      (or (cdr (assq (mail-header-number h2)
4044                     gnus-newsgroup-scored))
4045          gnus-summary-default-score 0)))
4046
4047 (defun gnus-thread-sort-by-score (h1 h2)
4048   "Sort threads by root article score."
4049   (gnus-article-sort-by-score
4050    (gnus-thread-header h1) (gnus-thread-header h2)))
4051
4052 (defun gnus-thread-sort-by-total-score (h1 h2)
4053   "Sort threads by the sum of all scores in the thread.
4054 Unscored articles will be counted as having a score of zero."
4055   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4056
4057 (defun gnus-thread-total-score (thread)
4058   ;; This function find the total score of THREAD.
4059   (cond ((null thread)
4060          0)
4061         ((consp thread)
4062          (if (stringp (car thread))
4063              (apply gnus-thread-score-function 0
4064                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4065            (gnus-thread-total-score-1 thread)))
4066         (t
4067          (gnus-thread-total-score-1 (list thread)))))
4068
4069 (defun gnus-thread-total-score-1 (root)
4070   ;; This function find the total score of the thread below ROOT.
4071   (setq root (car root))
4072   (apply gnus-thread-score-function
4073          (or (append
4074               (mapcar 'gnus-thread-total-score
4075                       (cdr (gnus-id-to-thread (mail-header-id root))))
4076               (when (> (mail-header-number root) 0)
4077                 (list (or (cdr (assq (mail-header-number root)
4078                                      gnus-newsgroup-scored))
4079                           gnus-summary-default-score 0))))
4080              (list gnus-summary-default-score)
4081              '(0))))
4082
4083 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4084 (defvar gnus-tmp-prev-subject nil)
4085 (defvar gnus-tmp-false-parent nil)
4086 (defvar gnus-tmp-root-expunged nil)
4087 (defvar gnus-tmp-dummy-line nil)
4088
4089 (eval-when-compile (defvar gnus-tmp-header))
4090 (defun gnus-extra-header (type &optional header)
4091   "Return the extra header of TYPE."
4092   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4093       ""))
4094
4095 (defun gnus-summary-prepare-threads (threads)
4096   "Prepare summary buffer from THREADS and indentation LEVEL.
4097 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4098 or a straight list of headers."
4099   (gnus-message 7 "Generating summary...")
4100
4101   (setq gnus-newsgroup-threads threads)
4102   (beginning-of-line)
4103
4104   (let ((gnus-tmp-level 0)
4105         (default-score (or gnus-summary-default-score 0))
4106         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4107         thread number subject stack state gnus-tmp-gathered beg-match
4108         new-roots gnus-tmp-new-adopts thread-end
4109         gnus-tmp-header gnus-tmp-unread
4110         gnus-tmp-replied gnus-tmp-subject-or-nil
4111         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4112         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4113         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4114
4115     (setq gnus-tmp-prev-subject nil)
4116
4117     (if (vectorp (car threads))
4118         ;; If this is a straight (sic) list of headers, then a
4119         ;; threaded summary display isn't required, so we just create
4120         ;; an unthreaded one.
4121         (gnus-summary-prepare-unthreaded threads)
4122
4123       ;; Do the threaded display.
4124
4125       (while (or threads stack gnus-tmp-new-adopts new-roots)
4126
4127         (if (and (= gnus-tmp-level 0)
4128                  (or (not stack)
4129                      (= (caar stack) 0))
4130                  (not gnus-tmp-false-parent)
4131                  (or gnus-tmp-new-adopts new-roots))
4132             (if gnus-tmp-new-adopts
4133                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4134                       thread (list (car gnus-tmp-new-adopts))
4135                       gnus-tmp-header (caar thread)
4136                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4137               (when new-roots
4138                 (setq thread (list (car new-roots))
4139                       gnus-tmp-header (caar thread)
4140                       new-roots (cdr new-roots))))
4141
4142           (if threads
4143               ;; If there are some threads, we do them before the
4144               ;; threads on the stack.
4145               (setq thread threads
4146                     gnus-tmp-header (caar thread))
4147             ;; There were no current threads, so we pop something off
4148             ;; the stack.
4149             (setq state (car stack)
4150                   gnus-tmp-level (car state)
4151                   thread (cdr state)
4152                   stack (cdr stack)
4153                   gnus-tmp-header (caar thread))))
4154
4155         (setq gnus-tmp-false-parent nil)
4156         (setq gnus-tmp-root-expunged nil)
4157         (setq thread-end nil)
4158
4159         (if (stringp gnus-tmp-header)
4160             ;; The header is a dummy root.
4161             (cond
4162              ((eq gnus-summary-make-false-root 'adopt)
4163               ;; We let the first article adopt the rest.
4164               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4165                                                (cddar thread)))
4166               (setq gnus-tmp-gathered
4167                     (nconc (mapcar
4168                             (lambda (h) (mail-header-number (car h)))
4169                             (cddar thread))
4170                            gnus-tmp-gathered))
4171               (setq thread (cons (list (caar thread)
4172                                        (cadar thread))
4173                                  (cdr thread)))
4174               (setq gnus-tmp-level -1
4175                     gnus-tmp-false-parent t))
4176              ((eq gnus-summary-make-false-root 'empty)
4177               ;; We print adopted articles with empty subject fields.
4178               (setq gnus-tmp-gathered
4179                     (nconc (mapcar
4180                             (lambda (h) (mail-header-number (car h)))
4181                             (cddar thread))
4182                            gnus-tmp-gathered))
4183               (setq gnus-tmp-level -1))
4184              ((eq gnus-summary-make-false-root 'dummy)
4185               ;; We remember that we probably want to output a dummy
4186               ;; root.
4187               (setq gnus-tmp-dummy-line gnus-tmp-header)
4188               (setq gnus-tmp-prev-subject gnus-tmp-header))
4189              (t
4190               ;; We do not make a root for the gathered
4191               ;; sub-threads at all.
4192               (setq gnus-tmp-level -1)))
4193
4194           (setq number (mail-header-number gnus-tmp-header)
4195                 subject (mail-header-subject gnus-tmp-header))
4196
4197           (cond
4198            ;; If the thread has changed subject, we might want to make
4199            ;; this subthread into a root.
4200            ((and (null gnus-thread-ignore-subject)
4201                  (not (zerop gnus-tmp-level))
4202                  gnus-tmp-prev-subject
4203                  (not (inline
4204                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4205             (setq new-roots (nconc new-roots (list (car thread)))
4206                   thread-end t
4207                   gnus-tmp-header nil))
4208            ;; If the article lies outside the current limit,
4209            ;; then we do not display it.
4210            ((not (memq number gnus-newsgroup-limit))
4211             (setq gnus-tmp-gathered
4212                   (nconc (mapcar
4213                           (lambda (h) (mail-header-number (car h)))
4214                           (cdar thread))
4215                          gnus-tmp-gathered))
4216             (setq gnus-tmp-new-adopts (if (cdar thread)
4217                                           (append gnus-tmp-new-adopts
4218                                                   (cdar thread))
4219                                         gnus-tmp-new-adopts)
4220                   thread-end t
4221                   gnus-tmp-header nil)
4222             (when (zerop gnus-tmp-level)
4223               (setq gnus-tmp-root-expunged t)))
4224            ;; Perhaps this article is to be marked as read?
4225            ((and gnus-summary-mark-below
4226                  (< (or (cdr (assq number gnus-newsgroup-scored))
4227                         default-score)
4228                     gnus-summary-mark-below)
4229                  ;; Don't touch sparse articles.
4230                  (not (gnus-summary-article-sparse-p number))
4231                  (not (gnus-summary-article-ancient-p number)))
4232             (setq gnus-newsgroup-unreads
4233                   (delq number gnus-newsgroup-unreads))
4234             (if gnus-newsgroup-auto-expire
4235                 (push number gnus-newsgroup-expirable)
4236               (push (cons number gnus-low-score-mark)
4237                     gnus-newsgroup-reads))))
4238
4239           (when gnus-tmp-header
4240             ;; We may have an old dummy line to output before this
4241             ;; article.
4242             (when (and gnus-tmp-dummy-line
4243                        (gnus-subject-equal
4244                         gnus-tmp-dummy-line
4245                         (mail-header-subject gnus-tmp-header)))
4246               (gnus-summary-insert-dummy-line
4247                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4248               (setq gnus-tmp-dummy-line nil))
4249
4250             ;; Compute the mark.
4251             (setq gnus-tmp-unread (gnus-article-mark number))
4252
4253             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4254                                   gnus-tmp-header gnus-tmp-level)
4255                   gnus-newsgroup-data)
4256
4257             ;; Actually insert the line.
4258             (setq
4259              gnus-tmp-subject-or-nil
4260              (cond
4261               ((and gnus-thread-ignore-subject
4262                     gnus-tmp-prev-subject
4263                     (not (inline (gnus-subject-equal
4264                                   gnus-tmp-prev-subject subject))))
4265                subject)
4266               ((zerop gnus-tmp-level)
4267                (if (and (eq gnus-summary-make-false-root 'empty)
4268                         (memq number gnus-tmp-gathered)
4269                         gnus-tmp-prev-subject
4270                         (inline (gnus-subject-equal
4271                                  gnus-tmp-prev-subject subject)))
4272                    gnus-summary-same-subject
4273                  subject))
4274               (t gnus-summary-same-subject)))
4275             (if (and (eq gnus-summary-make-false-root 'adopt)
4276                      (= gnus-tmp-level 1)
4277                      (memq number gnus-tmp-gathered))
4278                 (setq gnus-tmp-opening-bracket ?\<
4279                       gnus-tmp-closing-bracket ?\>)
4280               (setq gnus-tmp-opening-bracket ?\[
4281                     gnus-tmp-closing-bracket ?\]))
4282             (setq
4283              gnus-tmp-indentation
4284              (aref gnus-thread-indent-array gnus-tmp-level)
4285              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4286              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4287                                 gnus-summary-default-score 0)
4288              gnus-tmp-score-char
4289              (if (or (null gnus-summary-default-score)
4290                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4291                          gnus-summary-zcore-fuzz))
4292                  ?  ;Whitespace
4293                (if (< gnus-tmp-score gnus-summary-default-score)
4294                    gnus-score-below-mark gnus-score-over-mark))
4295              gnus-tmp-replied
4296              (cond ((memq number gnus-newsgroup-processable)
4297                     gnus-process-mark)
4298                    ((memq number gnus-newsgroup-cached)
4299                     gnus-cached-mark)
4300                    ((memq number gnus-newsgroup-replied)
4301                     gnus-replied-mark)
4302                    ((memq number gnus-newsgroup-forwarded)
4303                     gnus-forwarded-mark)
4304                    ((memq number gnus-newsgroup-saved)
4305                     gnus-saved-mark)
4306                    (t gnus-no-mark))
4307              gnus-tmp-from (mail-header-from gnus-tmp-header)
4308              gnus-tmp-name
4309              (cond
4310               ((string-match "<[^>]+> *$" gnus-tmp-from)
4311                (setq beg-match (match-beginning 0))
4312                (or (and (string-match "^\".+\"" gnus-tmp-from)
4313                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4314                    (substring gnus-tmp-from 0 beg-match)))
4315               ((string-match "(.+)" gnus-tmp-from)
4316                (substring gnus-tmp-from
4317                           (1+ (match-beginning 0)) (1- (match-end 0))))
4318               (t gnus-tmp-from)))
4319             (when (string= gnus-tmp-name "")
4320               (setq gnus-tmp-name gnus-tmp-from))
4321             (unless (numberp gnus-tmp-lines)
4322               (setq gnus-tmp-lines -1))
4323             (when (= gnus-tmp-lines -1)
4324               (setq gnus-tmp-lines "?"))
4325             (gnus-put-text-property
4326              (point)
4327              (progn (eval gnus-summary-line-format-spec) (point))
4328              'gnus-number number)
4329             (when gnus-visual-p
4330               (forward-line -1)
4331               (gnus-run-hooks 'gnus-summary-update-hook)
4332               (forward-line 1))
4333
4334             (setq gnus-tmp-prev-subject subject)))
4335
4336         (when (nth 1 thread)
4337           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4338         (incf gnus-tmp-level)
4339         (setq threads (if thread-end nil (cdar thread)))
4340         (unless threads
4341           (setq gnus-tmp-level 0)))))
4342   (gnus-message 7 "Generating summary...done"))
4343
4344 (defun gnus-summary-prepare-unthreaded (headers)
4345   "Generate an unthreaded summary buffer based on HEADERS."
4346   (let (header number mark)
4347
4348     (beginning-of-line)
4349
4350     (while headers
4351       ;; We may have to root out some bad articles...
4352       (when (memq (setq number (mail-header-number
4353                                 (setq header (pop headers))))
4354                   gnus-newsgroup-limit)
4355         ;; Mark article as read when it has a low score.
4356         (when (and gnus-summary-mark-below
4357                    (< (or (cdr (assq number gnus-newsgroup-scored))
4358                           gnus-summary-default-score 0)
4359                       gnus-summary-mark-below)
4360                    (not (gnus-summary-article-ancient-p number)))
4361           (setq gnus-newsgroup-unreads
4362                 (delq number gnus-newsgroup-unreads))
4363           (if gnus-newsgroup-auto-expire
4364               (push number gnus-newsgroup-expirable)
4365             (push (cons number gnus-low-score-mark)
4366                   gnus-newsgroup-reads)))
4367
4368         (setq mark (gnus-article-mark number))
4369         (push (gnus-data-make number mark (1+ (point)) header 0)
4370               gnus-newsgroup-data)
4371         (gnus-summary-insert-line
4372          header 0 number
4373          mark (memq number gnus-newsgroup-replied)
4374          (memq number gnus-newsgroup-expirable)
4375          (mail-header-subject header) nil
4376          (cdr (assq number gnus-newsgroup-scored))
4377          (memq number gnus-newsgroup-processable))))))
4378
4379 (defun gnus-summary-remove-list-identifiers ()
4380   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4381   (let ((regexp (if (consp gnus-list-identifiers)
4382                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4383                   gnus-list-identifiers))
4384         changed subject)
4385     (when regexp
4386       (dolist (header gnus-newsgroup-headers)
4387         (setq subject (mail-header-subject header)
4388               changed nil)
4389         (while (string-match
4390                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4391                 subject)
4392           (setq subject
4393                 (concat (substring subject 0 (match-beginning 2))
4394                         (substring subject (match-end 0)))
4395                 changed t))
4396         (when (and changed
4397                    (string-match
4398                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4399           (setq subject
4400                 (concat (substring subject 0 (match-beginning 1))
4401                         (substring subject (match-end 1)))))
4402         (when changed
4403           (mail-header-set-subject header subject))))))
4404
4405 (defun gnus-fetch-headers (articles)
4406   "Fetch headers of ARTICLES."
4407   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4408     (gnus-message 5 "Fetching headers for %s..." name)
4409     (prog1
4410         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4411         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4412             (if (eq 'nov
4413                     (setq gnus-headers-retrieved-by
4414                           (gnus-retrieve-headers
4415                            articles gnus-newsgroup-name
4416                            ;; We might want to fetch old headers, but
4417                            ;; not if there is only 1 article.
4418                            (and (or (and
4419                                      (not (eq gnus-fetch-old-headers 'some))
4420                                      (not (numberp gnus-fetch-old-headers)))
4421                                     (> (length articles) 1))
4422                                 gnus-fetch-old-headers))))
4423                 (gnus-get-newsgroup-headers-xover
4424                  articles nil nil gnus-newsgroup-name t)
4425               (gnus-get-newsgroup-headers))
4426           (gnus-retrieve-parsed-headers
4427            articles gnus-newsgroup-name
4428            ;; We might want to fetch old headers, but
4429            ;; not if there is only 1 article.
4430            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4431                          (not (numberp gnus-fetch-old-headers)))
4432                     (> (length articles) 1))
4433                 gnus-fetch-old-headers)))
4434       (gnus-message 5 "Fetching headers for %s...done" name))))
4435
4436 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4437   "Select newsgroup GROUP.
4438 If READ-ALL is non-nil, all articles in the group are selected.
4439 If SELECT-ARTICLES, only select those articles from GROUP."
4440   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4441          ;;!!! Dirty hack; should be removed.
4442          (gnus-summary-ignore-duplicates
4443           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4444               t
4445             gnus-summary-ignore-duplicates))
4446          (info (nth 2 entry))
4447          articles fetched-articles cached)
4448
4449     (unless (gnus-check-server
4450              (setq gnus-current-select-method
4451                    (gnus-find-method-for-group group)))
4452       (error "Couldn't open server"))
4453
4454     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4455         (gnus-activate-group group)     ; Or we can activate it...
4456         (progn                          ; Or we bug out.
4457           (when (equal major-mode 'gnus-summary-mode)
4458             (kill-buffer (current-buffer)))
4459           (error "Couldn't activate group %s: %s"
4460                  group (gnus-status-message group))))
4461
4462     (unless (gnus-request-group group t)
4463       (when (equal major-mode 'gnus-summary-mode)
4464         (kill-buffer (current-buffer)))
4465       (error "Couldn't request group %s: %s"
4466              group (gnus-status-message group)))
4467
4468     (setq gnus-newsgroup-name group)
4469     (setq gnus-newsgroup-unselected nil)
4470     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4471     (gnus-summary-setup-default-charset)
4472
4473     ;; Adjust and set lists of article marks.
4474     (when info
4475       (gnus-adjust-marked-articles info))
4476
4477     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4478     (setq cached
4479           (if (gnus-virtual-group-p group)
4480               gnus-newsgroup-cached
4481             (gnus-cache-articles-in-group group)))
4482
4483     (setq gnus-newsgroup-unreads
4484           (gnus-set-difference
4485            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4486            gnus-newsgroup-dormant))
4487
4488     (setq gnus-newsgroup-processable nil)
4489
4490     (gnus-update-read-articles group gnus-newsgroup-unreads)
4491
4492     (if (setq articles select-articles)
4493         (setq gnus-newsgroup-unselected
4494               (gnus-sorted-intersection
4495                gnus-newsgroup-unreads
4496                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4497       (setq articles (gnus-articles-to-read group read-all)))
4498
4499     (cond
4500      ((null articles)
4501       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4502       'quit)
4503      ((eq articles 0) nil)
4504      (t
4505       ;; Init the dependencies hash table.
4506       (setq gnus-newsgroup-dependencies
4507             (gnus-make-hashtable (length articles)))
4508       (gnus-set-global-variables)
4509       ;; Retrieve the headers and read them in.
4510       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4511
4512       ;; Suppress duplicates?
4513       (when gnus-suppress-duplicates
4514         (gnus-dup-suppress-articles))
4515
4516       ;; Set the initial limit.
4517       (setq gnus-newsgroup-limit (copy-sequence articles))
4518       ;; Remove canceled articles from the list of unread articles.
4519       (setq gnus-newsgroup-unreads
4520             (gnus-set-sorted-intersection
4521              gnus-newsgroup-unreads
4522              (setq fetched-articles
4523                    (mapcar (lambda (headers) (mail-header-number headers))
4524                            gnus-newsgroup-headers))))
4525       ;; Removed marked articles that do not exist.
4526       (gnus-update-missing-marks
4527        (gnus-sorted-complement fetched-articles articles))
4528
4529       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4530       (when cached
4531         (setq gnus-newsgroup-cached cached))
4532
4533       ;; We might want to build some more threads first.
4534       (when (and gnus-fetch-old-headers
4535                  (eq gnus-headers-retrieved-by 'nov))
4536         (if (eq gnus-fetch-old-headers 'invisible)
4537             (gnus-build-all-threads)
4538           (gnus-build-old-threads)))
4539       ;; Let the Gnus agent mark articles as read.
4540       (when gnus-agent
4541         (gnus-agent-get-undownloaded-list))
4542       ;; Remove list identifiers from subject
4543       (when gnus-list-identifiers
4544         (gnus-summary-remove-list-identifiers))
4545       ;; Check whether auto-expire is to be done in this group.
4546       (setq gnus-newsgroup-auto-expire
4547             (gnus-group-auto-expirable-p group))
4548       ;; Set up the article buffer now, if necessary.
4549       (unless gnus-single-article-buffer
4550         (gnus-article-setup-buffer))
4551       ;; First and last article in this newsgroup.
4552       (when gnus-newsgroup-headers
4553         (setq gnus-newsgroup-begin
4554               (mail-header-number (car gnus-newsgroup-headers))
4555               gnus-newsgroup-end
4556               (mail-header-number
4557                (gnus-last-element gnus-newsgroup-headers))))
4558       ;; GROUP is successfully selected.
4559       (or gnus-newsgroup-headers t)))))
4560
4561 (defun gnus-articles-to-read (group &optional read-all)
4562   "Find out what articles the user wants to read."
4563   (let* ((articles
4564           ;; Select all articles if `read-all' is non-nil, or if there
4565           ;; are no unread articles.
4566           (if (or read-all
4567                   (and (zerop (length gnus-newsgroup-marked))
4568                        (zerop (length gnus-newsgroup-unreads)))
4569                   (eq (gnus-group-find-parameter group 'display)
4570                       'all))
4571               (or
4572                (gnus-uncompress-range (gnus-active group))
4573                (gnus-cache-articles-in-group group))
4574             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4575                           (copy-sequence gnus-newsgroup-unreads))
4576                   '<)))
4577          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4578          (scored (length scored-list))
4579          (number (length articles))
4580          (marked (+ (length gnus-newsgroup-marked)
4581                     (length gnus-newsgroup-dormant)))
4582          (select
4583           (cond
4584            ((numberp read-all)
4585             read-all)
4586            (t
4587             (condition-case ()
4588                 (cond
4589                  ((and (or (<= scored marked) (= scored number))
4590                        (natnump gnus-large-newsgroup)
4591                        (> number gnus-large-newsgroup))
4592                   (let* ((cursor-in-echo-area nil)
4593                          (input (read-from-minibuffer
4594                                  (format
4595                                   "How many articles from %s (max %d): "
4596                                   (gnus-limit-string gnus-newsgroup-name 35)
4597                                   number)
4598                                  (cons (number-to-string gnus-large-newsgroup)
4599                                        0))))
4600                     (if (string-match "^[ \t]*$" input)
4601                         number
4602                       input)))
4603                  ((and (> scored marked) (< scored number)
4604                        (> (- scored number) 20))
4605                   (let ((input
4606                          (read-string
4607                           (format "%s %s (%d scored, %d total): "
4608                                   "How many articles from"
4609                                   group scored number))))
4610                     (if (string-match "^[ \t]*$" input)
4611                         number input)))
4612                  (t number))
4613               (quit
4614                (message "Quit getting the articles to read")
4615                nil))))))
4616     (setq select (if (stringp select) (string-to-number select) select))
4617     (if (or (null select) (zerop select))
4618         select
4619       (if (and (not (zerop scored)) (<= (abs select) scored))
4620           (progn
4621             (setq articles (sort scored-list '<))
4622             (setq number (length articles)))
4623         (setq articles (copy-sequence articles)))
4624
4625       (when (< (abs select) number)
4626         (if (< select 0)
4627             ;; Select the N oldest articles.
4628             (setcdr (nthcdr (1- (abs select)) articles) nil)
4629           ;; Select the N most recent articles.
4630           (setq articles (nthcdr (- number select) articles))))
4631       (setq gnus-newsgroup-unselected
4632             (gnus-sorted-intersection
4633              gnus-newsgroup-unreads
4634              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4635       (when gnus-alter-articles-to-read-function
4636         (setq gnus-newsgroup-unreads
4637               (sort
4638                (funcall gnus-alter-articles-to-read-function
4639                         gnus-newsgroup-name gnus-newsgroup-unreads)
4640                '<)))
4641       articles)))
4642
4643 (defun gnus-killed-articles (killed articles)
4644   (let (out)
4645     (while articles
4646       (when (inline (gnus-member-of-range (car articles) killed))
4647         (push (car articles) out))
4648       (setq articles (cdr articles)))
4649     out))
4650
4651 (defun gnus-uncompress-marks (marks)
4652   "Uncompress the mark ranges in MARKS."
4653   (let ((uncompressed '(score bookmark))
4654         out)
4655     (while marks
4656       (if (memq (caar marks) uncompressed)
4657           (push (car marks) out)
4658         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4659       (setq marks (cdr marks)))
4660     out))
4661
4662 (defun gnus-adjust-marked-articles (info)
4663   "Set all article lists and remove all marks that are no longer valid."
4664   (let* ((marked-lists (gnus-info-marks info))
4665          (active (gnus-active (gnus-info-group info)))
4666          (min (car active))
4667          (max (cdr active))
4668          (types gnus-article-mark-lists)
4669          (uncompressed '(score bookmark killed))
4670          marks var articles article mark)
4671
4672     (while marked-lists
4673       (setq marks (pop marked-lists))
4674       (set (setq var (intern (format "gnus-newsgroup-%s"
4675                                      (car (rassq (setq mark (car marks))
4676                                                  types)))))
4677            (if (memq (car marks) uncompressed) (cdr marks)
4678              (gnus-uncompress-range (cdr marks))))
4679
4680       (setq articles (symbol-value var))
4681
4682       ;; All articles have to be subsets of the active articles.
4683       (cond
4684        ;; Adjust "simple" lists.
4685        ((memq mark '(tick dormant expire reply save))
4686         (while articles
4687           (when (or (< (setq article (pop articles)) min) (> article max))
4688             (set var (delq article (symbol-value var))))))
4689        ;; Adjust assocs.
4690        ((memq mark uncompressed)
4691         (when (not (listp (cdr (symbol-value var))))
4692           (set var (list (symbol-value var))))
4693         (when (not (listp (cdr articles)))
4694           (setq articles (list articles)))
4695         (while articles
4696           (when (or (not (consp (setq article (pop articles))))
4697                     (< (car article) min)
4698                     (> (car article) max))
4699             (set var (delq article (symbol-value var))))))))))
4700
4701 (defun gnus-update-missing-marks (missing)
4702   "Go through the list of MISSING articles and remove them from the mark lists."
4703   (when missing
4704     (let ((types gnus-article-mark-lists)
4705           var m)
4706       ;; Go through all types.
4707       (while types
4708         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4709         (when (symbol-value var)
4710           ;; This list has articles.  So we delete all missing articles
4711           ;; from it.
4712           (setq m missing)
4713           (while m
4714             (set var (delq (pop m) (symbol-value var)))))))))
4715
4716 (defun gnus-update-marks ()
4717   "Enter the various lists of marked articles into the newsgroup info list."
4718   (let ((types gnus-article-mark-lists)
4719         (info (gnus-get-info gnus-newsgroup-name))
4720         (uncompressed '(score bookmark killed))
4721         type list newmarked symbol delta-marks)
4722     (when info
4723       ;; Add all marks lists to the list of marks lists.
4724       (while (setq type (pop types))
4725         (setq list (symbol-value
4726                     (setq symbol
4727                           (intern (format "gnus-newsgroup-%s"
4728                                           (car type))))))
4729
4730         (when list
4731           ;; Get rid of the entries of the articles that have the
4732           ;; default score.
4733           (when (and (eq (cdr type) 'score)
4734                      gnus-save-score
4735                      list)
4736             (let* ((arts list)
4737                    (prev (cons nil list))
4738                    (all prev))
4739               (while arts
4740                 (if (or (not (consp (car arts)))
4741                         (= (cdar arts) gnus-summary-default-score))
4742                     (setcdr prev (cdr arts))
4743                   (setq prev arts))
4744                 (setq arts (cdr arts)))
4745               (setq list (cdr all)))))
4746
4747         (unless (memq (cdr type) uncompressed)
4748           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4749
4750         (when (gnus-check-backend-function
4751                'request-set-mark gnus-newsgroup-name)
4752           ;; propagate flags to server, with the following exceptions:
4753           ;; uncompressed:s are not proper flags (they are cons cells)
4754           ;; cache is a internal gnus flag
4755           ;; download are local to one gnus installation (well)
4756           ;; unsend are for nndraft groups only
4757           ;; xxx: generality of this?  this suits nnimap anyway
4758           (unless (memq (cdr type) (append '(cache download unsend)
4759                                            uncompressed))
4760             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4761                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4762                    (add (gnus-remove-from-range
4763                          (gnus-copy-sequence list) old)))
4764               (when add
4765                 (push (list add 'add (list (cdr type))) delta-marks))
4766               (when del
4767                 (push (list del 'del (list (cdr type))) delta-marks)))))
4768
4769         (when list
4770           (push (cons (cdr type) list) newmarked)))
4771
4772       (when delta-marks
4773         (unless (gnus-check-group gnus-newsgroup-name)
4774           (error "Can't open server for %s" gnus-newsgroup-name))
4775         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4776
4777       ;; Enter these new marks into the info of the group.
4778       (if (nthcdr 3 info)
4779           (setcar (nthcdr 3 info) newmarked)
4780         ;; Add the marks lists to the end of the info.
4781         (when newmarked
4782           (setcdr (nthcdr 2 info) (list newmarked))))
4783
4784       ;; Cut off the end of the info if there's nothing else there.
4785       (let ((i 5))
4786         (while (and (> i 2)
4787                     (not (nth i info)))
4788           (when (nthcdr (decf i) info)
4789             (setcdr (nthcdr i info) nil)))))))
4790
4791 (defun gnus-set-mode-line (where)
4792   "Set the mode line of the article or summary buffers.
4793 If WHERE is `summary', the summary mode line format will be used."
4794   ;; Is this mode line one we keep updated?
4795   (when (and (memq where gnus-updated-mode-lines)
4796              (symbol-value
4797               (intern (format "gnus-%s-mode-line-format-spec" where))))
4798     (let (mode-string)
4799       (save-excursion
4800         ;; We evaluate this in the summary buffer since these
4801         ;; variables are buffer-local to that buffer.
4802         (set-buffer gnus-summary-buffer)
4803         ;; We bind all these variables that are used in the `eval' form
4804         ;; below.
4805         (let* ((mformat (symbol-value
4806                          (intern
4807                           (format "gnus-%s-mode-line-format-spec" where))))
4808                (gnus-tmp-group-name (gnus-group-decoded-name 
4809                                      gnus-newsgroup-name))
4810                (gnus-tmp-article-number (or gnus-current-article 0))
4811                (gnus-tmp-unread gnus-newsgroup-unreads)
4812                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4813                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4814                (gnus-tmp-unread-and-unselected
4815                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4816                             (zerop gnus-tmp-unselected))
4817                        "")
4818                       ((zerop gnus-tmp-unselected)
4819                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4820                       (t (format "{%d(+%d) more}"
4821                                  gnus-tmp-unread-and-unticked
4822                                  gnus-tmp-unselected))))
4823                (gnus-tmp-subject
4824                 (if (and gnus-current-headers
4825                          (vectorp gnus-current-headers))
4826                     (gnus-mode-string-quote
4827                      (mail-header-subject gnus-current-headers))
4828                   ""))
4829                bufname-length max-len
4830                gnus-tmp-header);; passed as argument to any user-format-funcs
4831           (setq mode-string (eval mformat))
4832           (setq bufname-length (if (string-match "%b" mode-string)
4833                                    (- (length
4834                                        (buffer-name
4835                                         (if (eq where 'summary)
4836                                             nil
4837                                           (get-buffer gnus-article-buffer))))
4838                                       2)
4839                                  0))
4840           (setq max-len (max 4 (if gnus-mode-non-string-length
4841                                    (- (window-width)
4842                                       gnus-mode-non-string-length
4843                                       bufname-length)
4844                                  (length mode-string))))
4845           ;; We might have to chop a bit of the string off...
4846           (when (> (length mode-string) max-len)
4847             (setq mode-string
4848                   (concat (gnus-truncate-string mode-string (- max-len 3))
4849                           "...")))
4850           ;; Pad the mode string a bit.
4851           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4852       ;; Update the mode line.
4853       (setq mode-line-buffer-identification
4854             (gnus-mode-line-buffer-identification (list mode-string)))
4855       (set-buffer-modified-p t))))
4856
4857 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4858   "Go through the HEADERS list and add all Xrefs to a hash table.
4859 The resulting hash table is returned, or nil if no Xrefs were found."
4860   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4861          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4862          (xref-hashtb (gnus-make-hashtable))
4863          start group entry number xrefs header)
4864     (while headers
4865       (setq header (pop headers))
4866       (when (and (setq xrefs (mail-header-xref header))
4867                  (not (memq (setq number (mail-header-number header))
4868                             unreads)))
4869         (setq start 0)
4870         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4871           (setq start (match-end 0))
4872           (setq group (if prefix
4873                           (concat prefix (substring xrefs (match-beginning 1)
4874                                                     (match-end 1)))
4875                         (substring xrefs (match-beginning 1) (match-end 1))))
4876           (setq number
4877                 (string-to-int (substring xrefs (match-beginning 2)
4878                                           (match-end 2))))
4879           (if (setq entry (gnus-gethash group xref-hashtb))
4880               (setcdr entry (cons number (cdr entry)))
4881             (gnus-sethash group (cons number nil) xref-hashtb)))))
4882     (and start xref-hashtb)))
4883
4884 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4885   "Look through all the headers and mark the Xrefs as read."
4886   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4887         name entry info xref-hashtb idlist method nth4)
4888     (save-excursion
4889       (set-buffer gnus-group-buffer)
4890       (when (setq xref-hashtb
4891                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4892         (mapatoms
4893          (lambda (group)
4894            (unless (string= from-newsgroup (setq name (symbol-name group)))
4895              (setq idlist (symbol-value group))
4896              ;; Dead groups are not updated.
4897              (and (prog1
4898                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4899                             info (nth 2 entry))
4900                     (when (stringp (setq nth4 (gnus-info-method info)))
4901                       (setq nth4 (gnus-server-to-method nth4))))
4902                   ;; Only do the xrefs if the group has the same
4903                   ;; select method as the group we have just read.
4904                   (or (gnus-methods-equal-p
4905                        nth4 (gnus-find-method-for-group from-newsgroup))
4906                       virtual
4907                       (equal nth4 (setq method (gnus-find-method-for-group
4908                                                 from-newsgroup)))
4909                       (and (equal (car nth4) (car method))
4910                            (equal (nth 1 nth4) (nth 1 method))))
4911                   gnus-use-cross-reference
4912                   (or (not (eq gnus-use-cross-reference t))
4913                       virtual
4914                       ;; Only do cross-references on subscribed
4915                       ;; groups, if that is what is wanted.
4916                       (<= (gnus-info-level info) gnus-level-subscribed))
4917                   (gnus-group-make-articles-read name idlist))))
4918          xref-hashtb)))))
4919
4920 (defun gnus-compute-read-articles (group articles)
4921   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4922          (info (nth 2 entry))
4923          (active (gnus-active group))
4924          ninfo)
4925     (when entry
4926       ;; First peel off all invalid article numbers.
4927       (when active
4928         (let ((ids articles)
4929               id first)
4930           (while (setq id (pop ids))
4931             (when (and first (> id (cdr active)))
4932               ;; We'll end up in this situation in one particular
4933               ;; obscure situation.  If you re-scan a group and get
4934               ;; a new article that is cross-posted to a different
4935               ;; group that has not been re-scanned, you might get
4936               ;; crossposted article that has a higher number than
4937               ;; Gnus believes possible.  So we re-activate this
4938               ;; group as well.  This might mean doing the
4939               ;; crossposting thingy will *increase* the number
4940               ;; of articles in some groups.  Tsk, tsk.
4941               (setq active (or (gnus-activate-group group) active)))
4942             (when (or (> id (cdr active))
4943                       (< id (car active)))
4944               (setq articles (delq id articles))))))
4945       ;; If the read list is nil, we init it.
4946       (if (and active
4947                (null (gnus-info-read info))
4948                (> (car active) 1))
4949           (setq ninfo (cons 1 (1- (car active))))
4950         (setq ninfo (gnus-info-read info)))
4951       ;; Then we add the read articles to the range.
4952       (gnus-add-to-range
4953        ninfo (setq articles (sort articles '<))))))
4954
4955 (defun gnus-group-make-articles-read (group articles)
4956   "Update the info of GROUP to say that ARTICLES are read."
4957   (let* ((num 0)
4958          (entry (gnus-gethash group gnus-newsrc-hashtb))
4959          (info (nth 2 entry))
4960          (active (gnus-active group))
4961          range)
4962     (when entry
4963       (setq range (gnus-compute-read-articles group articles))
4964       (save-excursion
4965         (set-buffer gnus-group-buffer)
4966         (gnus-undo-register
4967           `(progn
4968              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4969              (gnus-info-set-read ',info ',(gnus-info-read info))
4970              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4971              (gnus-group-update-group ,group t))))
4972       ;; Add the read articles to the range.
4973       (gnus-info-set-read info range)
4974       ;; Then we have to re-compute how many unread
4975       ;; articles there are in this group.
4976       (when active
4977         (cond
4978          ((not range)
4979           (setq num (- (1+ (cdr active)) (car active))))
4980          ((not (listp (cdr range)))
4981           (setq num (- (cdr active) (- (1+ (cdr range))
4982                                        (car range)))))
4983          (t
4984           (while range
4985             (if (numberp (car range))
4986                 (setq num (1+ num))
4987               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4988             (setq range (cdr range)))
4989           (setq num (- (cdr active) num))))
4990         ;; Update the number of unread articles.
4991         (setcar entry num)
4992         ;; Update the group buffer.
4993         (gnus-group-update-group group t)))))
4994
4995 (defvar gnus-newsgroup-none-id 0)
4996
4997 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4998   (let ((cur nntp-server-buffer)
4999         (dependencies
5000          (or dependencies
5001              (save-excursion (set-buffer gnus-summary-buffer)
5002                              gnus-newsgroup-dependencies)))
5003         headers id end ref
5004         (mail-parse-charset gnus-newsgroup-charset)
5005         (mail-parse-ignored-charsets
5006          (save-excursion (condition-case nil
5007                              (set-buffer gnus-summary-buffer)
5008                            (error))
5009                          gnus-newsgroup-ignored-charsets)))
5010     (save-excursion
5011       (set-buffer nntp-server-buffer)
5012       ;; Translate all TAB characters into SPACE characters.
5013       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5014       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5015       (gnus-run-hooks 'gnus-parse-headers-hook)
5016       (let ((case-fold-search t)
5017             in-reply-to header p lines chars ctype)
5018         (goto-char (point-min))
5019         ;; Search to the beginning of the next header.  Error messages
5020         ;; do not begin with 2 or 3.
5021         (while (re-search-forward "^[23][0-9]+ " nil t)
5022           (setq id nil
5023                 ref nil)
5024           ;; This implementation of this function, with nine
5025           ;; search-forwards instead of the one re-search-forward and
5026           ;; a case (which basically was the old function) is actually
5027           ;; about twice as fast, even though it looks messier.  You
5028           ;; can't have everything, I guess.  Speed and elegance
5029           ;; doesn't always go hand in hand.
5030           (setq
5031            header
5032            (make-full-mail-header
5033             ;; Number.
5034             (prog1
5035                 (read cur)
5036               (end-of-line)
5037               (setq p (point))
5038               (narrow-to-region (point)
5039                                 (or (and (search-forward "\n.\n" nil t)
5040                                          (- (point) 2))
5041                                     (point))))
5042             ;; Subject.
5043             (progn
5044               (goto-char p)
5045               (if (search-forward "\nsubject:" nil t)
5046                   (nnheader-header-value)
5047                 "(none)"))
5048             ;; From.
5049             (progn
5050               (goto-char p)
5051               (if (search-forward "\nfrom:" nil t)
5052                   (nnheader-header-value)
5053                 "(nobody)"))
5054             ;; Date.
5055             (progn
5056               (goto-char p)
5057               (if (search-forward "\ndate:" nil t)
5058                   (nnheader-header-value) ""))
5059             ;; Message-ID.
5060             (progn
5061               (goto-char p)
5062               (setq id (if (re-search-forward
5063                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5064                            ;; We do it this way to make sure the Message-ID
5065                            ;; is (somewhat) syntactically valid.
5066                            (buffer-substring (match-beginning 1)
5067                                              (match-end 1))
5068                          ;; If there was no message-id, we just fake one
5069                          ;; to make subsequent routines simpler.
5070                          (nnheader-generate-fake-message-id))))
5071             ;; References.
5072             (progn
5073               (goto-char p)
5074               (if (search-forward "\nreferences:" nil t)
5075                   (progn
5076                     (setq end (point))
5077                     (prog1
5078                         (nnheader-header-value)
5079                       (setq ref
5080                             (buffer-substring
5081                              (progn
5082                                ;; (end-of-line)
5083                                (search-backward ">" end t)
5084                                (1+ (point)))
5085                              (progn
5086                                (search-backward "<" end t)
5087                                (point))))))
5088                 ;; Get the references from the in-reply-to header if there
5089                 ;; were no references and the in-reply-to header looks
5090                 ;; promising.
5091                 (if (and (search-forward "\nin-reply-to:" nil t)
5092                          (setq in-reply-to (nnheader-header-value))
5093                          (string-match "<[^>]+>" in-reply-to))
5094                     (let (ref2)
5095                       (setq ref (substring in-reply-to (match-beginning 0)
5096                                            (match-end 0)))
5097                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5098                         (setq ref2 (substring in-reply-to (match-beginning 0)
5099                                               (match-end 0)))
5100                         (when (> (length ref2) (length ref))
5101                           (setq ref ref2)))
5102                       ref)
5103                   (setq ref nil))))
5104             ;; Chars.
5105             (progn
5106               (goto-char p)
5107               (if (search-forward "\nchars: " nil t)
5108                   (if (numberp (setq chars (ignore-errors (read cur))))
5109                       chars -1)
5110                 -1))
5111             ;; Lines.
5112             (progn
5113               (goto-char p)
5114               (if (search-forward "\nlines: " nil t)
5115                   (if (numberp (setq lines (ignore-errors (read cur))))
5116                       lines -1)
5117                 -1))
5118             ;; Xref.
5119             (progn
5120               (goto-char p)
5121               (and (search-forward "\nxref:" nil t)
5122                    (nnheader-header-value)))
5123             ;; Extra.
5124             (when gnus-extra-headers
5125               (let ((extra gnus-extra-headers)
5126                     out)
5127                 (while extra
5128                   (goto-char p)
5129                   (when (search-forward
5130                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5131                     (push (cons (car extra) (nnheader-header-value)) out))
5132                   (pop extra))
5133                 out))))
5134           (goto-char p)
5135           (if (and (search-forward "\ncontent-type: " nil t)
5136                    (setq ctype (nnheader-header-value)))
5137               (mime-entity-set-content-type-internal
5138                header (mime-parse-Content-Type ctype)))
5139           (when (equal id ref)
5140             (setq ref nil))
5141
5142           (when gnus-alter-header-function
5143             (funcall gnus-alter-header-function header)
5144             (setq id (mail-header-id header)
5145                   ref (gnus-parent-id (mail-header-references header))))
5146
5147           (when (setq header
5148                       (gnus-dependencies-add-header
5149                        header dependencies force-new))
5150             (push header headers))
5151           (goto-char (point-max))
5152           (widen))
5153         (nreverse headers)))))
5154
5155 ;; Goes through the xover lines and returns a list of vectors
5156 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5157                                                   force-new dependencies
5158                                                   group also-fetch-heads)
5159   "Parse the news overview data in the server buffer.
5160 Return a list of headers that match SEQUENCE (see
5161 `nntp-retrieve-headers')."
5162   ;; Get the Xref when the users reads the articles since most/some
5163   ;; NNTP servers do not include Xrefs when using XOVER.
5164   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5165   (let ((mail-parse-charset gnus-newsgroup-charset)
5166         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5167         (cur nntp-server-buffer)
5168         (dependencies (or dependencies gnus-newsgroup-dependencies))
5169         (allp (cond
5170                ((eq gnus-read-all-available-headers t)
5171                 t)
5172                ((stringp gnus-read-all-available-headers)
5173                 (string-match gnus-read-all-available-headers group))
5174                (t
5175                 nil)))
5176         number headers header)
5177     (save-excursion
5178       (set-buffer nntp-server-buffer)
5179       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5180       ;; Allow the user to mangle the headers before parsing them.
5181       (gnus-run-hooks 'gnus-parse-headers-hook)
5182       (goto-char (point-min))
5183       (while (not (eobp))
5184         (condition-case ()
5185             (while (and (or sequence allp)
5186                         (not (eobp)))
5187               (setq number (read cur))
5188               (when (not allp)
5189                 (while (and sequence
5190                             (< (car sequence) number))
5191                   (setq sequence (cdr sequence))))
5192               (when (and (or allp
5193                              (and sequence
5194                                   (eq number (car sequence))))
5195                          (progn
5196                            (setq sequence (cdr sequence))
5197                            (setq header (inline
5198                                           (gnus-nov-parse-line
5199                                            number dependencies force-new)))))
5200                 (push header headers))
5201               (forward-line 1))
5202           (error
5203            (gnus-error 4 "Strange nov line (%d)"
5204                        (count-lines (point-min) (point)))))
5205         (forward-line 1))
5206       ;; A common bug in inn is that if you have posted an article and
5207       ;; then retrieves the active file, it will answer correctly --
5208       ;; the new article is included.  However, a NOV entry for the
5209       ;; article may not have been generated yet, so this may fail.
5210       ;; We work around this problem by retrieving the last few
5211       ;; headers using HEAD.
5212       (if (or (not also-fetch-heads)
5213               (not sequence))
5214           ;; We (probably) got all the headers.
5215           (nreverse headers)
5216         (let ((gnus-nov-is-evil t))
5217           (nconc
5218            (nreverse headers)
5219            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5220            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5221                (when (eq (gnus-retrieve-headers sequence group) 'headers)
5222                  (gnus-get-newsgroup-headers))
5223              (gnus-retrieve-parsed-headers sequence group))))))))
5224
5225 (defun gnus-article-get-xrefs ()
5226   "Fill in the Xref value in `gnus-current-headers', if necessary.
5227 This is meant to be called in `gnus-article-internal-prepare-hook'."
5228   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5229                                  gnus-current-headers)))
5230     (or (not gnus-use-cross-reference)
5231         (not headers)
5232         (and (mail-header-xref headers)
5233              (not (string= (mail-header-xref headers) "")))
5234         (let ((case-fold-search t)
5235               xref)
5236           (save-restriction
5237             (nnheader-narrow-to-headers)
5238             (goto-char (point-min))
5239             (when (or (and (not (eobp))
5240                            (eq (downcase (char-after)) ?x)
5241                            (looking-at "Xref:"))
5242                       (search-forward "\nXref:" nil t))
5243               (goto-char (1+ (match-end 0)))
5244               (setq xref (buffer-substring (point)
5245                                            (progn (end-of-line) (point))))
5246               (mail-header-set-xref headers xref)))))))
5247
5248 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5249   "Find article ID and insert the summary line for that article.
5250 OLD-HEADER can either be a header or a line number to insert
5251 the subject line on."
5252   (let* ((line (and (numberp old-header) old-header))
5253          (old-header (and (vectorp old-header) old-header))
5254          (header (cond ((and old-header use-old-header)
5255                         old-header)
5256                        ((and (numberp id)
5257                              (gnus-number-to-header id))
5258                         (gnus-number-to-header id))
5259                        (t
5260                         (gnus-read-header id))))
5261          (number (and (numberp id) id))
5262          d)
5263     (when header
5264       ;; Rebuild the thread that this article is part of and go to the
5265       ;; article we have fetched.
5266       (when (and (not gnus-show-threads)
5267                  old-header)
5268         (when (and number
5269                    (setq d (gnus-data-find (mail-header-number old-header))))
5270           (goto-char (gnus-data-pos d))
5271           (gnus-data-remove
5272            number
5273            (- (gnus-point-at-bol)
5274               (prog1
5275                   (1+ (gnus-point-at-eol))
5276                 (gnus-delete-line))))))
5277       (when old-header
5278         (mail-header-set-number header (mail-header-number old-header)))
5279       (setq gnus-newsgroup-sparse
5280             (delq (setq number (mail-header-number header))
5281                   gnus-newsgroup-sparse))
5282       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5283       (push number gnus-newsgroup-limit)
5284       (gnus-rebuild-thread (mail-header-id header) line)
5285       (gnus-summary-goto-subject number nil t))
5286     (when (and (numberp number)
5287                (> number 0))
5288       ;; We have to update the boundaries even if we can't fetch the
5289       ;; article if ID is a number -- so that the next `P' or `N'
5290       ;; command will fetch the previous (or next) article even
5291       ;; if the one we tried to fetch this time has been canceled.
5292       (when (> number gnus-newsgroup-end)
5293         (setq gnus-newsgroup-end number))
5294       (when (< number gnus-newsgroup-begin)
5295         (setq gnus-newsgroup-begin number))
5296       (setq gnus-newsgroup-unselected
5297             (delq number gnus-newsgroup-unselected)))
5298     ;; Report back a success?
5299     (and header (mail-header-number header))))
5300
5301 ;;; Process/prefix in the summary buffer
5302
5303 (defun gnus-summary-work-articles (n)
5304   "Return a list of articles to be worked upon.
5305 The prefix argument, the list of process marked articles, and the
5306 current article will be taken into consideration."
5307   (save-excursion
5308     (set-buffer gnus-summary-buffer)
5309     (cond
5310      (n
5311       ;; A numerical prefix has been given.
5312       (setq n (prefix-numeric-value n))
5313       (let ((backward (< n 0))
5314             (n (abs (prefix-numeric-value n)))
5315             articles article)
5316         (save-excursion
5317           (while
5318               (and (> n 0)
5319                    (push (setq article (gnus-summary-article-number))
5320                          articles)
5321                    (if backward
5322                        (gnus-summary-find-prev nil article)
5323                      (gnus-summary-find-next nil article)))
5324             (decf n)))
5325         (nreverse articles)))
5326      ((and (gnus-region-active-p) (mark))
5327       (message "region active")
5328       ;; Work on the region between point and mark.
5329       (let ((max (max (point) (mark)))
5330             articles article)
5331         (save-excursion
5332           (goto-char (min (point) (mark)))
5333           (while
5334               (and
5335                (push (setq article (gnus-summary-article-number)) articles)
5336                (gnus-summary-find-next nil article)
5337                (< (point) max)))
5338           (nreverse articles))))
5339      (gnus-newsgroup-processable
5340       ;; There are process-marked articles present.
5341       ;; Save current state.
5342       (gnus-summary-save-process-mark)
5343       ;; Return the list.
5344       (reverse gnus-newsgroup-processable))
5345      (t
5346       ;; Just return the current article.
5347       (list (gnus-summary-article-number))))))
5348
5349 (defmacro gnus-summary-iterate (arg &rest forms)
5350   "Iterate over the process/prefixed articles and do FORMS.
5351 ARG is the interactive prefix given to the command.  FORMS will be
5352 executed with point over the summary line of the articles."
5353   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5354     `(let ((,articles (gnus-summary-work-articles ,arg)))
5355        (while ,articles
5356          (gnus-summary-goto-subject (car ,articles))
5357          ,@forms
5358          (pop ,articles)))))
5359
5360 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5361 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5362
5363 (defun gnus-summary-save-process-mark ()
5364   "Push the current set of process marked articles on the stack."
5365   (interactive)
5366   (push (copy-sequence gnus-newsgroup-processable)
5367         gnus-newsgroup-process-stack))
5368
5369 (defun gnus-summary-kill-process-mark ()
5370   "Push the current set of process marked articles on the stack and unmark."
5371   (interactive)
5372   (gnus-summary-save-process-mark)
5373   (gnus-summary-unmark-all-processable))
5374
5375 (defun gnus-summary-yank-process-mark ()
5376   "Pop the last process mark state off the stack and restore it."
5377   (interactive)
5378   (unless gnus-newsgroup-process-stack
5379     (error "Empty mark stack"))
5380   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5381
5382 (defun gnus-summary-process-mark-set (set)
5383   "Make SET into the current process marked articles."
5384   (gnus-summary-unmark-all-processable)
5385   (while set
5386     (gnus-summary-set-process-mark (pop set))))
5387
5388 ;;; Searching and stuff
5389
5390 (defun gnus-summary-search-group (&optional backward use-level)
5391   "Search for next unread newsgroup.
5392 If optional argument BACKWARD is non-nil, search backward instead."
5393   (save-excursion
5394     (set-buffer gnus-group-buffer)
5395     (when (gnus-group-search-forward
5396            backward nil (if use-level (gnus-group-group-level) nil))
5397       (gnus-group-group-name))))
5398
5399 (defun gnus-summary-best-group (&optional exclude-group)
5400   "Find the name of the best unread group.
5401 If EXCLUDE-GROUP, do not go to this group."
5402   (save-excursion
5403     (set-buffer gnus-group-buffer)
5404     (save-excursion
5405       (gnus-group-best-unread-group exclude-group))))
5406
5407 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5408   (if backward (gnus-summary-find-prev)
5409     (let* ((dummy (gnus-summary-article-intangible-p))
5410            (article (or article (gnus-summary-article-number)))
5411            (arts (gnus-data-find-list article))
5412            result)
5413       (when (and (not dummy)
5414                  (or (not gnus-summary-check-current)
5415                      (not unread)
5416                      (not (gnus-data-unread-p (car arts)))))
5417         (setq arts (cdr arts)))
5418       (when (setq result
5419                   (if unread
5420                       (progn
5421                         (while arts
5422                           (when (or (and undownloaded
5423                                          (eq gnus-undownloaded-mark
5424                                              (gnus-data-mark (car arts))))
5425                                     (gnus-data-unread-p (car arts)))
5426                             (setq result (car arts)
5427                                   arts nil))
5428                           (setq arts (cdr arts)))
5429                         result)
5430                     (car arts)))
5431         (goto-char (gnus-data-pos result))
5432         (gnus-data-number result)))))
5433
5434 (defun gnus-summary-find-prev (&optional unread article)
5435   (let* ((eobp (eobp))
5436          (article (or article (gnus-summary-article-number)))
5437          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5438          result)
5439     (when (and (not eobp)
5440                (or (not gnus-summary-check-current)
5441                    (not unread)
5442                    (not (gnus-data-unread-p (car arts)))))
5443       (setq arts (cdr arts)))
5444     (when (setq result
5445                 (if unread
5446                     (progn
5447                       (while arts
5448                         (when (gnus-data-unread-p (car arts))
5449                           (setq result (car arts)
5450                                 arts nil))
5451                         (setq arts (cdr arts)))
5452                       result)
5453                   (car arts)))
5454       (goto-char (gnus-data-pos result))
5455       (gnus-data-number result))))
5456
5457 (defun gnus-summary-find-subject (subject &optional unread backward article)
5458   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5459          (article (or article (gnus-summary-article-number)))
5460          (articles (gnus-data-list backward))
5461          (arts (gnus-data-find-list article articles))
5462          result)
5463     (when (or (not gnus-summary-check-current)
5464               (not unread)
5465               (not (gnus-data-unread-p (car arts))))
5466       (setq arts (cdr arts)))
5467     (while arts
5468       (and (or (not unread)
5469                (gnus-data-unread-p (car arts)))
5470            (vectorp (gnus-data-header (car arts)))
5471            (gnus-subject-equal
5472             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5473            (setq result (car arts)
5474                  arts nil))
5475       (setq arts (cdr arts)))
5476     (and result
5477          (goto-char (gnus-data-pos result))
5478          (gnus-data-number result))))
5479
5480 (defun gnus-summary-search-forward (&optional unread subject backward)
5481   "Search forward for an article.
5482 If UNREAD, look for unread articles.  If SUBJECT, look for
5483 articles with that subject.  If BACKWARD, search backward instead."
5484   (cond (subject (gnus-summary-find-subject subject unread backward))
5485         (backward (gnus-summary-find-prev unread))
5486         (t (gnus-summary-find-next unread))))
5487
5488 (defun gnus-recenter (&optional n)
5489   "Center point in window and redisplay frame.
5490 Also do horizontal recentering."
5491   (interactive "P")
5492   (when (and gnus-auto-center-summary
5493              (not (eq gnus-auto-center-summary 'vertical)))
5494     (gnus-horizontal-recenter))
5495   (recenter n))
5496
5497 (defun gnus-summary-recenter ()
5498   "Center point in the summary window.
5499 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5500 displayed, no centering will be performed."
5501   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5502   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5503   (interactive)
5504   (let* ((top (cond ((< (window-height) 4) 0)
5505                     ((< (window-height) 7) 1)
5506                     (t (if (numberp gnus-auto-center-summary)
5507                            gnus-auto-center-summary
5508                          2))))
5509          (height (1- (window-height)))
5510          (bottom (save-excursion (goto-char (point-max))
5511                                  (forward-line (- height))
5512                                  (point)))
5513          (window (get-buffer-window (current-buffer))))
5514     ;; The user has to want it.
5515     (when gnus-auto-center-summary
5516       (when (get-buffer-window gnus-article-buffer)
5517         ;; Only do recentering when the article buffer is displayed,
5518         ;; Set the window start to either `bottom', which is the biggest
5519         ;; possible valid number, or the second line from the top,
5520         ;; whichever is the least.
5521         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5522           (if (> bottom top-pos)
5523               ;; Keep the second line from the top visible
5524               (set-window-start window top-pos t)
5525             ;; Try to keep the bottom line visible; if it's partially
5526             ;; obscured, either scroll one more line to make it fully
5527             ;; visible, or revert to using TOP-POS.
5528             (save-excursion
5529               (goto-char (point-max))
5530               (forward-line -1)
5531               (let ((last-line-start (point)))
5532                 (goto-char bottom)
5533                 (set-window-start window (point) t)
5534                 (when (not (pos-visible-in-window-p last-line-start window))
5535                   (forward-line 1)
5536                   (set-window-start window (min (point) top-pos) t)))))))
5537       ;; Do horizontal recentering while we're at it.
5538       (when (and (get-buffer-window (current-buffer) t)
5539                  (not (eq gnus-auto-center-summary 'vertical)))
5540         (let ((selected (selected-window)))
5541           (select-window (get-buffer-window (current-buffer) t))
5542           (gnus-summary-position-point)
5543           (gnus-horizontal-recenter)
5544           (select-window selected))))))
5545
5546 (defun gnus-summary-jump-to-group (newsgroup)
5547   "Move point to NEWSGROUP in group mode buffer."
5548   ;; Keep update point of group mode buffer if visible.
5549   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5550       (save-window-excursion
5551         ;; Take care of tree window mode.
5552         (when (get-buffer-window gnus-group-buffer)
5553           (pop-to-buffer gnus-group-buffer))
5554         (gnus-group-jump-to-group newsgroup))
5555     (save-excursion
5556       ;; Take care of tree window mode.
5557       (if (get-buffer-window gnus-group-buffer)
5558           (pop-to-buffer gnus-group-buffer)
5559         (set-buffer gnus-group-buffer))
5560       (gnus-group-jump-to-group newsgroup))))
5561
5562 ;; This function returns a list of article numbers based on the
5563 ;; difference between the ranges of read articles in this group and
5564 ;; the range of active articles.
5565 (defun gnus-list-of-unread-articles (group)
5566   (let* ((read (gnus-info-read (gnus-get-info group)))
5567          (active (or (gnus-active group) (gnus-activate-group group)))
5568          (last (cdr active))
5569          first nlast unread)
5570     ;; If none are read, then all are unread.
5571     (if (not read)
5572         (setq first (car active))
5573       ;; If the range of read articles is a single range, then the
5574       ;; first unread article is the article after the last read
5575       ;; article.  Sounds logical, doesn't it?
5576       (if (and (not (listp (cdr read)))
5577                (or (< (car read) (car active))
5578                    (progn (setq read (list read))
5579                           nil)))
5580           (setq first (max (car active) (1+ (cdr read))))
5581         ;; `read' is a list of ranges.
5582         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5583                                   (caar read)))
5584                   1)
5585           (setq first (car active)))
5586         (while read
5587           (when first
5588             (while (< first nlast)
5589               (push first unread)
5590               (setq first (1+ first))))
5591           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5592           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5593           (setq read (cdr read)))))
5594     ;; And add the last unread articles.
5595     (while (<= first last)
5596       (push first unread)
5597       (setq first (1+ first)))
5598     ;; Return the list of unread articles.
5599     (delq 0 (nreverse unread))))
5600
5601 (defun gnus-list-of-read-articles (group)
5602   "Return a list of unread, unticked and non-dormant articles."
5603   (let* ((info (gnus-get-info group))
5604          (marked (gnus-info-marks info))
5605          (active (gnus-active group)))
5606     (and info active
5607          (gnus-set-difference
5608           (gnus-sorted-complement
5609            (gnus-uncompress-range active)
5610            (gnus-list-of-unread-articles group))
5611           (append
5612            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5613            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5614
5615 ;; Various summary commands
5616
5617 (defun gnus-summary-select-article-buffer ()
5618   "Reconfigure windows to show article buffer."
5619   (interactive)
5620   (if (not (gnus-buffer-live-p gnus-article-buffer))
5621       (error "There is no article buffer for this summary buffer")
5622     (gnus-configure-windows 'article)
5623     (select-window (get-buffer-window gnus-article-buffer))))
5624
5625 (defun gnus-summary-universal-argument (arg)
5626   "Perform any operation on all articles that are process/prefixed."
5627   (interactive "P")
5628   (let ((articles (gnus-summary-work-articles arg))
5629         func article)
5630     (if (eq
5631          (setq
5632           func
5633           (key-binding
5634            (read-key-sequence
5635             (substitute-command-keys
5636              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5637          'undefined)
5638         (gnus-error 1 "Undefined key")
5639       (save-excursion
5640         (while articles
5641           (gnus-summary-goto-subject (setq article (pop articles)))
5642           (let (gnus-newsgroup-processable)
5643             (command-execute func))
5644           (gnus-summary-remove-process-mark article)))))
5645   (gnus-summary-position-point))
5646
5647 (defun gnus-summary-toggle-truncation (&optional arg)
5648   "Toggle truncation of summary lines.
5649 With arg, turn line truncation on iff arg is positive."
5650   (interactive "P")
5651   (setq truncate-lines
5652         (if (null arg) (not truncate-lines)
5653           (> (prefix-numeric-value arg) 0)))
5654   (redraw-display))
5655
5656 (defun gnus-summary-reselect-current-group (&optional all rescan)
5657   "Exit and then reselect the current newsgroup.
5658 The prefix argument ALL means to select all articles."
5659   (interactive "P")
5660   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5661     (error "Ephemeral groups can't be reselected"))
5662   (let ((current-subject (gnus-summary-article-number))
5663         (group gnus-newsgroup-name))
5664     (setq gnus-newsgroup-begin nil)
5665     (gnus-summary-exit)
5666     ;; We have to adjust the point of group mode buffer because
5667     ;; point was moved to the next unread newsgroup by exiting.
5668     (gnus-summary-jump-to-group group)
5669     (when rescan
5670       (save-excursion
5671         (save-window-excursion
5672           ;; Don't show group contents.
5673           (set-window-start (selected-window) (point-max))
5674           (gnus-group-get-new-news-this-group 1))))
5675     (gnus-group-read-group all t)
5676     (gnus-summary-goto-subject current-subject nil t)))
5677
5678 (defun gnus-summary-rescan-group (&optional all)
5679   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5680   (interactive "P")
5681   (gnus-summary-reselect-current-group all t))
5682
5683 (defun gnus-summary-update-info (&optional non-destructive)
5684   (save-excursion
5685     (let ((group gnus-newsgroup-name))
5686       (when group
5687         (when gnus-newsgroup-kill-headers
5688           (setq gnus-newsgroup-killed
5689                 (gnus-compress-sequence
5690                  (nconc
5691                   (gnus-set-sorted-intersection
5692                    (gnus-uncompress-range gnus-newsgroup-killed)
5693                    (setq gnus-newsgroup-unselected
5694                          (sort gnus-newsgroup-unselected '<)))
5695                   (setq gnus-newsgroup-unreads
5696                         (sort gnus-newsgroup-unreads '<)))
5697                  t)))
5698         (unless (listp (cdr gnus-newsgroup-killed))
5699           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5700         (let ((headers gnus-newsgroup-headers))
5701           ;; Set the new ranges of read articles.
5702           (save-excursion
5703             (set-buffer gnus-group-buffer)
5704             (gnus-undo-force-boundary))
5705           (gnus-update-read-articles
5706            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5707           ;; Set the current article marks.
5708           (let ((gnus-newsgroup-scored
5709                  (if (and (not gnus-save-score)
5710                           (not non-destructive))
5711                      nil
5712                    gnus-newsgroup-scored)))
5713             (save-excursion
5714               (gnus-update-marks)))
5715           ;; Do the cross-ref thing.
5716           (when gnus-use-cross-reference
5717             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5718           ;; Do not switch windows but change the buffer to work.
5719           (set-buffer gnus-group-buffer)
5720           (unless (gnus-ephemeral-group-p group)
5721             (gnus-group-update-group group)))))))
5722
5723 (defun gnus-summary-save-newsrc (&optional force)
5724   "Save the current number of read/marked articles in the dribble buffer.
5725 The dribble buffer will then be saved.
5726 If FORCE (the prefix), also save the .newsrc file(s)."
5727   (interactive "P")
5728   (gnus-summary-update-info t)
5729   (if force
5730       (gnus-save-newsrc-file)
5731     (gnus-dribble-save)))
5732
5733 (defun gnus-summary-exit (&optional temporary)
5734   "Exit reading current newsgroup, and then return to group selection mode.
5735 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5736   (interactive)
5737   (gnus-set-global-variables)
5738   (gnus-kill-save-kill-buffer)
5739   (gnus-async-halt-prefetch)
5740   (let* ((group gnus-newsgroup-name)
5741          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5742          (mode major-mode)
5743          (group-point nil)
5744          (buf (current-buffer)))
5745     (unless quit-config
5746       ;; Do adaptive scoring, and possibly save score files.
5747       (when gnus-newsgroup-adaptive
5748         (gnus-score-adaptive))
5749       (when gnus-use-scoring
5750         (gnus-score-save)))
5751     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5752     ;; If we have several article buffers, we kill them at exit.
5753     (unless gnus-single-article-buffer
5754       (gnus-kill-buffer gnus-original-article-buffer)
5755       (setq gnus-article-current nil))
5756     (when gnus-use-cache
5757       (gnus-cache-possibly-remove-articles)
5758       (gnus-cache-save-buffers))
5759     (gnus-async-prefetch-remove-group group)
5760     (when gnus-suppress-duplicates
5761       (gnus-dup-enter-articles))
5762     (when gnus-use-trees
5763       (gnus-tree-close group))
5764     (when gnus-use-cache
5765       (gnus-cache-write-active))
5766     ;; Remove entries for this group.
5767     (nnmail-purge-split-history (gnus-group-real-name group))
5768     ;; Make all changes in this group permanent.
5769     (unless quit-config
5770       (gnus-run-hooks 'gnus-exit-group-hook)
5771       (gnus-summary-update-info))
5772     (gnus-close-group group)
5773     ;; Make sure where we were, and go to next newsgroup.
5774     (set-buffer gnus-group-buffer)
5775     (unless quit-config
5776       (gnus-group-jump-to-group group))
5777     (gnus-run-hooks 'gnus-summary-exit-hook)
5778     (unless (or quit-config
5779                 ;; If this group has disappeared from the summary
5780                 ;; buffer, don't skip forwards.
5781                 (not (string= group (gnus-group-group-name))))
5782       (gnus-group-next-unread-group 1))
5783     (setq group-point (point))
5784     (if temporary
5785         nil                             ;Nothing to do.
5786       ;; If we have several article buffers, we kill them at exit.
5787       (unless gnus-single-article-buffer
5788         (gnus-kill-buffer gnus-article-buffer)
5789         (gnus-kill-buffer gnus-original-article-buffer)
5790         (setq gnus-article-current nil))
5791       (set-buffer buf)
5792       (if (not gnus-kill-summary-on-exit)
5793           (progn
5794             (gnus-deaden-summary)
5795             (setq mode nil))
5796         ;; We set all buffer-local variables to nil.  It is unclear why
5797         ;; this is needed, but if we don't, buffer-local variables are
5798         ;; not garbage-collected, it seems.  This would the lead to en
5799         ;; ever-growing Emacs.
5800         (gnus-summary-clear-local-variables)
5801         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5802           (gnus-summary-clear-local-variables))
5803         (when (get-buffer gnus-article-buffer)
5804           (bury-buffer gnus-article-buffer))
5805         ;; We clear the global counterparts of the buffer-local
5806         ;; variables as well, just to be on the safe side.
5807         (set-buffer gnus-group-buffer)
5808         (gnus-summary-clear-local-variables)
5809         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5810           (gnus-summary-clear-local-variables)))
5811       (setq gnus-current-select-method gnus-select-method)
5812       (pop-to-buffer gnus-group-buffer)
5813       (if (not quit-config)
5814           (progn
5815             (goto-char group-point)
5816             (gnus-configure-windows 'group 'force)
5817             (unless (pos-visible-in-window-p)
5818               (forward-line (/ (static-if (featurep 'xemacs)
5819                                    (window-displayed-height)
5820                                  (1- (window-height)))
5821                                -2))
5822               (set-window-start (selected-window) (point))
5823               (goto-char group-point)))
5824         (gnus-handle-ephemeral-exit quit-config))
5825       ;; Return to group mode buffer.
5826       (when (eq mode 'gnus-summary-mode)
5827         (gnus-kill-buffer buf))
5828       ;; Clear the current group name.
5829       (unless quit-config
5830         (setq gnus-newsgroup-name nil)))))
5831
5832 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5833 (defun gnus-summary-exit-no-update (&optional no-questions)
5834   "Quit reading current newsgroup without updating read article info."
5835   (interactive)
5836   (let* ((group gnus-newsgroup-name)
5837          (quit-config (gnus-group-quit-config group)))
5838     (when (or no-questions
5839               gnus-expert-user
5840               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5841       (gnus-async-halt-prefetch)
5842       (mapcar 'funcall
5843               (delq 'gnus-summary-expire-articles
5844                     (copy-sequence gnus-summary-prepare-exit-hook)))
5845       ;; If we have several article buffers, we kill them at exit.
5846       (unless gnus-single-article-buffer
5847         (gnus-kill-buffer gnus-article-buffer)
5848         (gnus-kill-buffer gnus-original-article-buffer)
5849         (setq gnus-article-current nil))
5850       (if (not gnus-kill-summary-on-exit)
5851           (gnus-deaden-summary)
5852         (gnus-close-group group)
5853         (gnus-summary-clear-local-variables)
5854         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5855           (gnus-summary-clear-local-variables))
5856         (set-buffer gnus-group-buffer)
5857         (gnus-summary-clear-local-variables)
5858         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5859           (gnus-summary-clear-local-variables))
5860         (when (get-buffer gnus-summary-buffer)
5861           (kill-buffer gnus-summary-buffer)))
5862       (unless gnus-single-article-buffer
5863         (setq gnus-article-current nil))
5864       (when gnus-use-trees
5865         (gnus-tree-close group))
5866       (gnus-async-prefetch-remove-group group)
5867       (when (get-buffer gnus-article-buffer)
5868         (bury-buffer gnus-article-buffer))
5869       ;; Return to the group buffer.
5870       (gnus-configure-windows 'group 'force)
5871       ;; Clear the current group name.
5872       (setq gnus-newsgroup-name nil)
5873       (when (equal (gnus-group-group-name) group)
5874         (gnus-group-next-unread-group 1))
5875       (when quit-config
5876         (gnus-handle-ephemeral-exit quit-config)))))
5877
5878 (defun gnus-handle-ephemeral-exit (quit-config)
5879   "Handle movement when leaving an ephemeral group.
5880 The state which existed when entering the ephemeral is reset."
5881   (if (not (buffer-name (car quit-config)))
5882       (gnus-configure-windows 'group 'force)
5883     (set-buffer (car quit-config))
5884     (cond ((eq major-mode 'gnus-summary-mode)
5885            (gnus-set-global-variables))
5886           ((eq major-mode 'gnus-article-mode)
5887            (save-excursion
5888              ;; The `gnus-summary-buffer' variable may point
5889              ;; to the old summary buffer when using a single
5890              ;; article buffer.
5891              (unless (gnus-buffer-live-p gnus-summary-buffer)
5892                (set-buffer gnus-group-buffer))
5893              (set-buffer gnus-summary-buffer)
5894              (gnus-set-global-variables))))
5895     (if (or (eq (cdr quit-config) 'article)
5896             (eq (cdr quit-config) 'pick))
5897         (progn
5898           ;; The current article may be from the ephemeral group
5899           ;; thus it is best that we reload this article
5900           (gnus-summary-show-article)
5901           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5902               (gnus-configure-windows 'pick 'force)
5903             (gnus-configure-windows (cdr quit-config) 'force)))
5904       (gnus-configure-windows (cdr quit-config) 'force))
5905     (when (eq major-mode 'gnus-summary-mode)
5906       (gnus-summary-next-subject 1 nil t)
5907       (gnus-summary-recenter)
5908       (gnus-summary-position-point))))
5909
5910 (defun gnus-summary-preview-mime-message ()
5911   "MIME decode and play this message."
5912   (interactive)
5913   (let ((gnus-break-pages nil)
5914         (gnus-show-mime t))
5915     (gnus-summary-select-article gnus-show-all-headers t))
5916   (select-window (get-buffer-window gnus-article-buffer)))
5917
5918 ;;; Dead summaries.
5919
5920 (defvar gnus-dead-summary-mode-map nil)
5921
5922 (unless gnus-dead-summary-mode-map
5923   (setq gnus-dead-summary-mode-map (make-keymap))
5924   (suppress-keymap gnus-dead-summary-mode-map)
5925   (substitute-key-definition
5926    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5927   (let ((keys '("\C-d" "\r" "\177" [delete])))
5928     (while keys
5929       (define-key gnus-dead-summary-mode-map
5930         (pop keys) 'gnus-summary-wake-up-the-dead))))
5931
5932 (defvar gnus-dead-summary-mode nil
5933   "Minor mode for Gnus summary buffers.")
5934
5935 (defun gnus-dead-summary-mode (&optional arg)
5936   "Minor mode for Gnus summary buffers."
5937   (interactive "P")
5938   (when (eq major-mode 'gnus-summary-mode)
5939     (make-local-variable 'gnus-dead-summary-mode)
5940     (setq gnus-dead-summary-mode
5941           (if (null arg) (not gnus-dead-summary-mode)
5942             (> (prefix-numeric-value arg) 0)))
5943     (when gnus-dead-summary-mode
5944       (gnus-add-minor-mode
5945        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5946
5947 (defun gnus-deaden-summary ()
5948   "Make the current summary buffer into a dead summary buffer."
5949   ;; Kill any previous dead summary buffer.
5950   (when (and gnus-dead-summary
5951              (buffer-name gnus-dead-summary))
5952     (save-excursion
5953       (set-buffer gnus-dead-summary)
5954       (when gnus-dead-summary-mode
5955         (kill-buffer (current-buffer)))))
5956   ;; Make this the current dead summary.
5957   (setq gnus-dead-summary (current-buffer))
5958   (gnus-dead-summary-mode 1)
5959   (let ((name (buffer-name)))
5960     (when (string-match "Summary" name)
5961       (rename-buffer
5962        (concat (substring name 0 (match-beginning 0)) "Dead "
5963                (substring name (match-beginning 0)))
5964        t)
5965       (bury-buffer))))
5966
5967 (defun gnus-kill-or-deaden-summary (buffer)
5968   "Kill or deaden the summary BUFFER."
5969   (save-excursion
5970     (when (and (buffer-name buffer)
5971                (not gnus-single-article-buffer))
5972       (save-excursion
5973         (set-buffer buffer)
5974         (gnus-kill-buffer gnus-article-buffer)
5975         (gnus-kill-buffer gnus-original-article-buffer)))
5976     (cond (gnus-kill-summary-on-exit
5977            (when (and gnus-use-trees
5978                       (gnus-buffer-exists-p buffer))
5979              (save-excursion
5980                (set-buffer buffer)
5981                (gnus-tree-close gnus-newsgroup-name)))
5982            (gnus-kill-buffer buffer))
5983           ((gnus-buffer-exists-p buffer)
5984            (save-excursion
5985              (set-buffer buffer)
5986              (gnus-deaden-summary))))))
5987
5988 (defun gnus-summary-wake-up-the-dead (&rest args)
5989   "Wake up the dead summary buffer."
5990   (interactive)
5991   (gnus-dead-summary-mode -1)
5992   (let ((name (buffer-name)))
5993     (when (string-match "Dead " name)
5994       (rename-buffer
5995        (concat (substring name 0 (match-beginning 0))
5996                (substring name (match-end 0)))
5997        t)))
5998   (gnus-message 3 "This dead summary is now alive again"))
5999
6000 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6001 (defun gnus-summary-fetch-faq (&optional faq-dir)
6002   "Fetch the FAQ for the current group.
6003 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6004 in."
6005   (interactive
6006    (list
6007     (when current-prefix-arg
6008       (completing-read
6009        "Faq dir: " (and (listp gnus-group-faq-directory)
6010                         (mapcar (lambda (file) (list file))
6011                                 gnus-group-faq-directory))))))
6012   (let (gnus-faq-buffer)
6013     (when (setq gnus-faq-buffer
6014                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6015       (gnus-configure-windows 'summary-faq))))
6016
6017 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6018 (defun gnus-summary-describe-group (&optional force)
6019   "Describe the current newsgroup."
6020   (interactive "P")
6021   (gnus-group-describe-group force gnus-newsgroup-name))
6022
6023 (defun gnus-summary-describe-briefly ()
6024   "Describe summary mode commands briefly."
6025   (interactive)
6026   (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")))
6027
6028 ;; Walking around group mode buffer from summary mode.
6029
6030 (defun gnus-summary-next-group (&optional no-article target-group backward)
6031   "Exit current newsgroup and then select next unread newsgroup.
6032 If prefix argument NO-ARTICLE is non-nil, no article is selected
6033 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6034 previous group instead."
6035   (interactive "P")
6036   ;; Stop pre-fetching.
6037   (gnus-async-halt-prefetch)
6038   (let ((current-group gnus-newsgroup-name)
6039         (current-buffer (current-buffer))
6040         entered)
6041     ;; First we semi-exit this group to update Xrefs and all variables.
6042     ;; We can't do a real exit, because the window conf must remain
6043     ;; the same in case the user is prompted for info, and we don't
6044     ;; want the window conf to change before that...
6045     (gnus-summary-exit t)
6046     (while (not entered)
6047       ;; Then we find what group we are supposed to enter.
6048       (set-buffer gnus-group-buffer)
6049       (gnus-group-jump-to-group current-group)
6050       (setq target-group
6051             (or target-group
6052                 (if (eq gnus-keep-same-level 'best)
6053                     (gnus-summary-best-group gnus-newsgroup-name)
6054                   (gnus-summary-search-group backward gnus-keep-same-level))))
6055       (if (not target-group)
6056           ;; There are no further groups, so we return to the group
6057           ;; buffer.
6058           (progn
6059             (gnus-message 5 "Returning to the group buffer")
6060             (setq entered t)
6061             (when (gnus-buffer-live-p current-buffer)
6062               (set-buffer current-buffer)
6063               (gnus-summary-exit))
6064             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6065         ;; We try to enter the target group.
6066         (gnus-group-jump-to-group target-group)
6067         (let ((unreads (gnus-group-group-unread)))
6068           (if (and (or (eq t unreads)
6069                        (and unreads (not (zerop unreads))))
6070                    (gnus-summary-read-group
6071                     target-group nil no-article
6072                     (and (buffer-name current-buffer) current-buffer)
6073                     nil backward))
6074               (setq entered t)
6075             (setq current-group target-group
6076                   target-group nil)))))))
6077
6078 (defun gnus-summary-prev-group (&optional no-article)
6079   "Exit current newsgroup and then select previous unread newsgroup.
6080 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6081   (interactive "P")
6082   (gnus-summary-next-group no-article nil t))
6083
6084 ;; Walking around summary lines.
6085
6086 (defun gnus-summary-first-subject (&optional unread undownloaded)
6087   "Go to the first unread subject.
6088 If UNREAD is non-nil, go to the first unread article.
6089 Returns the article selected or nil if there are no unread articles."
6090   (interactive "P")
6091   (prog1
6092       (cond
6093        ;; Empty summary.
6094        ((null gnus-newsgroup-data)
6095         (gnus-message 3 "No articles in the group")
6096         nil)
6097        ;; Pick the first article.
6098        ((not unread)
6099         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6100         (gnus-data-number (car gnus-newsgroup-data)))
6101        ;; No unread articles.
6102        ((null gnus-newsgroup-unreads)
6103         (gnus-message 3 "No more unread articles")
6104         nil)
6105        ;; Find the first unread article.
6106        (t
6107         (let ((data gnus-newsgroup-data))
6108           (while (and data
6109                       (and (not (and undownloaded
6110                                      (eq gnus-undownloaded-mark
6111                                          (gnus-data-mark (car data)))))
6112                            (not (gnus-data-unread-p (car data)))))
6113             (setq data (cdr data)))
6114           (when data
6115             (goto-char (gnus-data-pos (car data)))
6116             (gnus-data-number (car data))))))
6117     (gnus-summary-position-point)))
6118
6119 (defun gnus-summary-next-subject (n &optional unread dont-display)
6120   "Go to next N'th summary line.
6121 If N is negative, go to the previous N'th subject line.
6122 If UNREAD is non-nil, only unread articles are selected.
6123 The difference between N and the actual number of steps taken is
6124 returned."
6125   (interactive "p")
6126   (let ((backward (< n 0))
6127         (n (abs n)))
6128     (while (and (> n 0)
6129                 (if backward
6130                     (gnus-summary-find-prev unread)
6131                   (gnus-summary-find-next unread)))
6132       (unless (zerop (setq n (1- n)))
6133         (gnus-summary-show-thread)))
6134     (when (/= 0 n)
6135       (gnus-message 7 "No more%s articles"
6136                     (if unread " unread" "")))
6137     (unless dont-display
6138       (gnus-summary-recenter)
6139       (gnus-summary-position-point))
6140     n))
6141
6142 (defun gnus-summary-next-unread-subject (n)
6143   "Go to next N'th unread summary line."
6144   (interactive "p")
6145   (gnus-summary-next-subject n t))
6146
6147 (defun gnus-summary-prev-subject (n &optional unread)
6148   "Go to previous N'th summary line.
6149 If optional argument UNREAD is non-nil, only unread article is selected."
6150   (interactive "p")
6151   (gnus-summary-next-subject (- n) unread))
6152
6153 (defun gnus-summary-prev-unread-subject (n)
6154   "Go to previous N'th unread summary line."
6155   (interactive "p")
6156   (gnus-summary-next-subject (- n) t))
6157
6158 (defun gnus-summary-goto-subject (article &optional force silent)
6159   "Go the subject line of ARTICLE.
6160 If FORCE, also allow jumping to articles not currently shown."
6161   (interactive "nArticle number: ")
6162   (let ((b (point))
6163         (data (gnus-data-find article)))
6164     ;; We read in the article if we have to.
6165     (and (not data)
6166          force
6167          (gnus-summary-insert-subject
6168           article
6169           (if (or (numberp force) (vectorp force)) force)
6170           t)
6171          (setq data (gnus-data-find article)))
6172     (goto-char b)
6173     (if (not data)
6174         (progn
6175           (unless silent
6176             (gnus-message 3 "Can't find article %d" article))
6177           nil)
6178       (goto-char (gnus-data-pos data))
6179       (gnus-summary-position-point)
6180       article)))
6181
6182 ;; Walking around summary lines with displaying articles.
6183
6184 (defun gnus-summary-expand-window (&optional arg)
6185   "Make the summary buffer take up the entire Emacs frame.
6186 Given a prefix, will force an `article' buffer configuration."
6187   (interactive "P")
6188   (if arg
6189       (gnus-configure-windows 'article 'force)
6190     (gnus-configure-windows 'summary 'force)))
6191
6192 (defun gnus-summary-display-article (article &optional all-header)
6193   "Display ARTICLE in article buffer."
6194   (when (gnus-buffer-live-p gnus-article-buffer)
6195     (with-current-buffer gnus-article-buffer
6196       (set-buffer-multibyte t)))
6197   (gnus-set-global-variables)
6198   (when (gnus-buffer-live-p gnus-article-buffer)
6199     (with-current-buffer gnus-article-buffer
6200       (setq gnus-article-charset gnus-newsgroup-charset)
6201       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6202   (if (null article)
6203       nil
6204     (prog1
6205         (if gnus-summary-display-article-function
6206             (funcall gnus-summary-display-article-function article all-header)
6207           (gnus-article-prepare article all-header))
6208       (with-current-buffer gnus-article-buffer
6209         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6210              nil))
6211       (gnus-run-hooks 'gnus-select-article-hook)
6212       (when (and gnus-current-article
6213                  (not (zerop gnus-current-article)))
6214         (gnus-summary-goto-subject gnus-current-article))
6215       (gnus-summary-recenter)
6216       (when (and gnus-use-trees gnus-show-threads)
6217         (gnus-possibly-generate-tree article)
6218         (gnus-highlight-selected-tree article))
6219       ;; Successfully display article.
6220       (gnus-article-set-window-start
6221        (cdr (assq article gnus-newsgroup-bookmarks))))))
6222
6223 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6224   "Select the current article.
6225 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6226 non-nil, the article will be re-fetched even if it already present in
6227 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6228 be displayed."
6229   ;; Make sure we are in the summary buffer to work around bbdb bug.
6230   (unless (eq major-mode 'gnus-summary-mode)
6231     (set-buffer gnus-summary-buffer))
6232   (let ((article (or article (gnus-summary-article-number)))
6233         (all-headers (not (not all-headers))) ;Must be T or NIL.
6234         gnus-summary-display-article-function)
6235     (and (not pseudo)
6236          (gnus-summary-article-pseudo-p article)
6237          (error "This is a pseudo-article"))
6238     (save-excursion
6239       (set-buffer gnus-summary-buffer)
6240       (if (or (and gnus-single-article-buffer
6241                    (or (null gnus-current-article)
6242                        (null gnus-article-current)
6243                        (null (get-buffer gnus-article-buffer))
6244                        (not (eq article (cdr gnus-article-current)))
6245                        (not (equal (car gnus-article-current)
6246                                    gnus-newsgroup-name))))
6247               (and (not gnus-single-article-buffer)
6248                    (or (null gnus-current-article)
6249                        (not (eq gnus-current-article article))))
6250               force)
6251           ;; The requested article is different from the current article.
6252           (progn
6253             (gnus-summary-display-article article all-headers)
6254             (when (or all-headers gnus-show-all-headers)
6255               (gnus-article-show-all-headers))
6256             (gnus-article-set-window-start
6257              (cdr (assq article gnus-newsgroup-bookmarks)))
6258             article)
6259         (when (or all-headers gnus-show-all-headers)
6260           (gnus-article-show-all-headers))
6261         'old))))
6262
6263 (defun gnus-summary-force-verify-and-decrypt ()
6264   (interactive)
6265   (let ((mm-verify-option 'known)
6266         (mm-decrypt-option 'known))
6267     (gnus-summary-select-article nil 'force)))
6268
6269 (defun gnus-summary-set-current-mark (&optional current-mark)
6270   "Obsolete function."
6271   nil)
6272
6273 (defun gnus-summary-next-article (&optional unread subject backward push)
6274   "Select the next article.
6275 If UNREAD, only unread articles are selected.
6276 If SUBJECT, only articles with SUBJECT are selected.
6277 If BACKWARD, the previous article is selected instead of the next."
6278   (interactive "P")
6279   (cond
6280    ;; Is there such an article?
6281    ((and (gnus-summary-search-forward unread subject backward)
6282          (or (gnus-summary-display-article (gnus-summary-article-number))
6283              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6284     (gnus-summary-position-point))
6285    ;; If not, we try the first unread, if that is wanted.
6286    ((and subject
6287          gnus-auto-select-same
6288          (gnus-summary-first-unread-article))
6289     (gnus-summary-position-point)
6290     (gnus-message 6 "Wrapped"))
6291    ;; Try to get next/previous article not displayed in this group.
6292    ((and gnus-auto-extend-newsgroup
6293          (not unread) (not subject))
6294     (gnus-summary-goto-article
6295      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6296      nil (count-lines (point-min) (point))))
6297    ;; Go to next/previous group.
6298    (t
6299     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6300       (gnus-summary-jump-to-group gnus-newsgroup-name))
6301     (let ((cmd last-command-char)
6302           (point
6303            (save-excursion
6304              (set-buffer gnus-group-buffer)
6305              (point)))
6306           (group
6307            (if (eq gnus-keep-same-level 'best)
6308                (gnus-summary-best-group gnus-newsgroup-name)
6309              (gnus-summary-search-group backward gnus-keep-same-level))))
6310       ;; For some reason, the group window gets selected.  We change
6311       ;; it back.
6312       (select-window (get-buffer-window (current-buffer)))
6313       ;; Select next unread newsgroup automagically.
6314       (cond
6315        ((or (not gnus-auto-select-next)
6316             (not cmd))
6317         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6318        ((or (eq gnus-auto-select-next 'quietly)
6319             (and (eq gnus-auto-select-next 'slightly-quietly)
6320                  push)
6321             (and (eq gnus-auto-select-next 'almost-quietly)
6322                  (gnus-summary-last-article-p)))
6323         ;; Select quietly.
6324         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6325             (gnus-summary-exit)
6326           (gnus-message 7 "No more%s articles (%s)..."
6327                         (if unread " unread" "")
6328                         (if group (concat "selecting " group)
6329                           "exiting"))
6330           (gnus-summary-next-group nil group backward)))
6331        (t
6332         (when (gnus-key-press-event-p last-input-event)
6333           (gnus-summary-walk-group-buffer
6334            gnus-newsgroup-name cmd unread backward point))))))))
6335
6336 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6337   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6338                       (?\C-p (gnus-group-prev-unread-group 1))))
6339         (cursor-in-echo-area t)
6340         keve key group ended)
6341     (save-excursion
6342       (set-buffer gnus-group-buffer)
6343       (goto-char start)
6344       (setq group
6345             (if (eq gnus-keep-same-level 'best)
6346                 (gnus-summary-best-group gnus-newsgroup-name)
6347               (gnus-summary-search-group backward gnus-keep-same-level))))
6348     (while (not ended)
6349       (gnus-message
6350        5 "No more%s articles%s" (if unread " unread" "")
6351        (if (and group
6352                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6353            (format " (Type %s for %s [%s])"
6354                    (single-key-description cmd) group
6355                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6356          (format " (Type %s to exit %s)"
6357                  (single-key-description cmd)
6358                  gnus-newsgroup-name)))
6359       ;; Confirm auto selection.
6360       (setq key (car (setq keve (gnus-read-event-char))))
6361       (setq ended t)
6362       (cond
6363        ((assq key keystrokes)
6364         (let ((obuf (current-buffer)))
6365           (switch-to-buffer gnus-group-buffer)
6366           (when group
6367             (gnus-group-jump-to-group group))
6368           (eval (cadr (assq key keystrokes)))
6369           (setq group (gnus-group-group-name))
6370           (switch-to-buffer obuf))
6371         (setq ended nil))
6372        ((equal key cmd)
6373         (if (or (not group)
6374                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6375             (gnus-summary-exit)
6376           (gnus-summary-next-group nil group backward)))
6377        (t
6378         (push (cdr keve) unread-command-events))))))
6379
6380 (defun gnus-summary-next-unread-article ()
6381   "Select unread article after current one."
6382   (interactive)
6383   (gnus-summary-next-article
6384    (or (not (eq gnus-summary-goto-unread 'never))
6385        (gnus-summary-last-article-p (gnus-summary-article-number)))
6386    (and gnus-auto-select-same
6387         (gnus-summary-article-subject))))
6388
6389 (defun gnus-summary-prev-article (&optional unread subject)
6390   "Select the article after the current one.
6391 If UNREAD is non-nil, only unread articles are selected."
6392   (interactive "P")
6393   (gnus-summary-next-article unread subject t))
6394
6395 (defun gnus-summary-prev-unread-article ()
6396   "Select unread article before current one."
6397   (interactive)
6398   (gnus-summary-prev-article
6399    (or (not (eq gnus-summary-goto-unread 'never))
6400        (gnus-summary-first-article-p (gnus-summary-article-number)))
6401    (and gnus-auto-select-same
6402         (gnus-summary-article-subject))))
6403
6404 (defun gnus-summary-next-page (&optional lines circular)
6405   "Show next page of the selected article.
6406 If at the end of the current article, select the next article.
6407 LINES says how many lines should be scrolled up.
6408
6409 If CIRCULAR is non-nil, go to the start of the article instead of
6410 selecting the next article when reaching the end of the current
6411 article."
6412   (interactive "P")
6413   (setq gnus-summary-buffer (current-buffer))
6414   (gnus-set-global-variables)
6415   (let ((article (gnus-summary-article-number))
6416         (article-window (get-buffer-window gnus-article-buffer t))
6417         endp)
6418     ;; If the buffer is empty, we have no article.
6419     (unless article
6420       (error "No article to select"))
6421     (gnus-configure-windows 'article)
6422     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6423         (if (and (eq gnus-summary-goto-unread 'never)
6424                  (not (gnus-summary-last-article-p article)))
6425             (gnus-summary-next-article)
6426           (gnus-summary-next-unread-article))
6427       (if (or (null gnus-current-article)
6428               (null gnus-article-current)
6429               (/= article (cdr gnus-article-current))
6430               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6431           ;; Selected subject is different from current article's.
6432           (gnus-summary-display-article article)
6433         (when article-window
6434           (gnus-eval-in-buffer-window gnus-article-buffer
6435             (setq endp (gnus-article-next-page lines)))
6436           (when endp
6437             (cond (circular
6438                    (gnus-summary-beginning-of-article))
6439                   (lines
6440                    (gnus-message 3 "End of message"))
6441                   ((null lines)
6442                    (if (and (eq gnus-summary-goto-unread 'never)
6443                             (not (gnus-summary-last-article-p article)))
6444                        (gnus-summary-next-article)
6445                      (gnus-summary-next-unread-article))))))))
6446     (gnus-summary-recenter)
6447     (gnus-summary-position-point)))
6448
6449 (defun gnus-summary-prev-page (&optional lines move)
6450   "Show previous page of selected article.
6451 Argument LINES specifies lines to be scrolled down.
6452 If MOVE, move to the previous unread article if point is at
6453 the beginning of the buffer."
6454   (interactive "P")
6455   (let ((article (gnus-summary-article-number))
6456         (article-window (get-buffer-window gnus-article-buffer t))
6457         endp)
6458     (gnus-configure-windows 'article)
6459     (if (or (null gnus-current-article)
6460             (null gnus-article-current)
6461             (/= article (cdr gnus-article-current))
6462             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6463         ;; Selected subject is different from current article's.
6464         (gnus-summary-display-article article)
6465       (gnus-summary-recenter)
6466       (when article-window
6467         (gnus-eval-in-buffer-window gnus-article-buffer
6468           (setq endp (gnus-article-prev-page lines)))
6469         (when (and move endp)
6470           (cond (lines
6471                  (gnus-message 3 "Beginning of message"))
6472                 ((null lines)
6473                  (if (and (eq gnus-summary-goto-unread 'never)
6474                           (not (gnus-summary-first-article-p article)))
6475                      (gnus-summary-prev-article)
6476                    (gnus-summary-prev-unread-article))))))))
6477   (gnus-summary-position-point))
6478
6479 (defun gnus-summary-prev-page-or-article (&optional lines)
6480   "Show previous page of selected article.
6481 Argument LINES specifies lines to be scrolled down.
6482 If at the beginning of the article, go to the next article."
6483   (interactive "P")
6484   (gnus-summary-prev-page lines t))
6485
6486 (defun gnus-summary-scroll-up (lines)
6487   "Scroll up (or down) one line current article.
6488 Argument LINES specifies lines to be scrolled up (or down if negative)."
6489   (interactive "p")
6490   (gnus-configure-windows 'article)
6491   (gnus-summary-show-thread)
6492   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6493     (gnus-eval-in-buffer-window gnus-article-buffer
6494       (cond ((> lines 0)
6495              (when (gnus-article-next-page lines)
6496                (gnus-message 3 "End of message")))
6497             ((< lines 0)
6498              (gnus-article-prev-page (- lines))))))
6499   (gnus-summary-recenter)
6500   (gnus-summary-position-point))
6501
6502 (defun gnus-summary-scroll-down (lines)
6503   "Scroll down (or up) one line current article.
6504 Argument LINES specifies lines to be scrolled down (or up if negative)."
6505   (interactive "p")
6506   (gnus-summary-scroll-up (- lines)))
6507
6508 (defun gnus-summary-next-same-subject ()
6509   "Select next article which has the same subject as current one."
6510   (interactive)
6511   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6512
6513 (defun gnus-summary-prev-same-subject ()
6514   "Select previous article which has the same subject as current one."
6515   (interactive)
6516   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6517
6518 (defun gnus-summary-next-unread-same-subject ()
6519   "Select next unread article which has the same subject as current one."
6520   (interactive)
6521   (gnus-summary-next-article t (gnus-summary-article-subject)))
6522
6523 (defun gnus-summary-prev-unread-same-subject ()
6524   "Select previous unread article which has the same subject as current one."
6525   (interactive)
6526   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6527
6528 (defun gnus-summary-first-unread-article ()
6529   "Select the first unread article.
6530 Return nil if there are no unread articles."
6531   (interactive)
6532   (prog1
6533       (when (gnus-summary-first-subject t)
6534         (gnus-summary-show-thread)
6535         (gnus-summary-first-subject t)
6536         (gnus-summary-display-article (gnus-summary-article-number)))
6537     (gnus-summary-position-point)))
6538
6539 (defun gnus-summary-first-unread-subject ()
6540   "Place the point on the subject line of the first unread article.
6541 Return nil if there are no unread articles."
6542   (interactive)
6543   (prog1
6544       (when (gnus-summary-first-subject t)
6545         (gnus-summary-show-thread)
6546         (gnus-summary-first-subject t))
6547     (gnus-summary-position-point)))
6548
6549 (defun gnus-summary-first-article ()
6550   "Select the first article.
6551 Return nil if there are no articles."
6552   (interactive)
6553   (prog1
6554       (when (gnus-summary-first-subject)
6555         (gnus-summary-show-thread)
6556         (gnus-summary-first-subject)
6557         (gnus-summary-display-article (gnus-summary-article-number)))
6558     (gnus-summary-position-point)))
6559
6560 (defun gnus-summary-best-unread-article ()
6561   "Select the unread article with the highest score."
6562   (interactive)
6563   (let ((best -1000000)
6564         (data gnus-newsgroup-data)
6565         article score)
6566     (while data
6567       (and (gnus-data-unread-p (car data))
6568            (> (setq score
6569                     (gnus-summary-article-score (gnus-data-number (car data))))
6570               best)
6571            (setq best score
6572                  article (gnus-data-number (car data))))
6573       (setq data (cdr data)))
6574     (prog1
6575         (if article
6576             (gnus-summary-goto-article article)
6577           (error "No unread articles"))
6578       (gnus-summary-position-point))))
6579
6580 (defun gnus-summary-last-subject ()
6581   "Go to the last displayed subject line in the group."
6582   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6583     (when article
6584       (gnus-summary-goto-subject article))))
6585
6586 (defun gnus-summary-goto-article (article &optional all-headers force)
6587   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6588 If ALL-HEADERS is non-nil, no header lines are hidden.
6589 If FORCE, go to the article even if it isn't displayed.  If FORCE
6590 is a number, it is the line the article is to be displayed on."
6591   (interactive
6592    (list
6593     (completing-read
6594      "Article number or Message-ID: "
6595      (mapcar (lambda (number) (list (int-to-string number)))
6596              gnus-newsgroup-limit))
6597     current-prefix-arg
6598     t))
6599   (prog1
6600       (if (and (stringp article)
6601                (string-match "@" article))
6602           (gnus-summary-refer-article article)
6603         (when (stringp article)
6604           (setq article (string-to-number article)))
6605         (if (gnus-summary-goto-subject article force)
6606             (gnus-summary-display-article article all-headers)
6607           (gnus-message 4 "Couldn't go to article %s" article) nil))
6608     (gnus-summary-position-point)))
6609
6610 (defun gnus-summary-goto-last-article ()
6611   "Go to the previously read article."
6612   (interactive)
6613   (prog1
6614       (when gnus-last-article
6615         (gnus-summary-goto-article gnus-last-article nil t))
6616     (gnus-summary-position-point)))
6617
6618 (defun gnus-summary-pop-article (number)
6619   "Pop one article off the history and go to the previous.
6620 NUMBER articles will be popped off."
6621   (interactive "p")
6622   (let (to)
6623     (setq gnus-newsgroup-history
6624           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6625     (if to
6626         (gnus-summary-goto-article (car to) nil t)
6627       (error "Article history empty")))
6628   (gnus-summary-position-point))
6629
6630 ;; Summary commands and functions for limiting the summary buffer.
6631
6632 (defun gnus-summary-limit-to-articles (n)
6633   "Limit the summary buffer to the next N articles.
6634 If not given a prefix, use the process marked articles instead."
6635   (interactive "P")
6636   (prog1
6637       (let ((articles (gnus-summary-work-articles n)))
6638         (setq gnus-newsgroup-processable nil)
6639         (gnus-summary-limit articles))
6640     (gnus-summary-position-point)))
6641
6642 (defun gnus-summary-pop-limit (&optional total)
6643   "Restore the previous limit.
6644 If given a prefix, remove all limits."
6645   (interactive "P")
6646   (when total
6647     (setq gnus-newsgroup-limits
6648           (list (mapcar (lambda (h) (mail-header-number h))
6649                         gnus-newsgroup-headers))))
6650   (unless gnus-newsgroup-limits
6651     (error "No limit to pop"))
6652   (prog1
6653       (gnus-summary-limit nil 'pop)
6654     (gnus-summary-position-point)))
6655
6656 (defun gnus-summary-limit-to-subject (subject &optional header)
6657   "Limit the summary buffer to articles that have subjects that match a regexp."
6658   (interactive "sLimit to subject (regexp): ")
6659   (unless header
6660     (setq header "subject"))
6661   (when (not (equal "" subject))
6662     (prog1
6663         (let ((articles (gnus-summary-find-matching
6664                          (or header "subject") subject 'all)))
6665           (unless articles
6666             (error "Found no matches for \"%s\"" subject))
6667           (gnus-summary-limit articles))
6668       (gnus-summary-position-point))))
6669
6670 (defun gnus-summary-limit-to-author (from)
6671   "Limit the summary buffer to articles that have authors that match a regexp."
6672   (interactive "sLimit to author (regexp): ")
6673   (gnus-summary-limit-to-subject from "from"))
6674
6675 (defun gnus-summary-limit-to-age (age &optional younger-p)
6676   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6677 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6678 articles that are younger than AGE days."
6679   (interactive
6680    (let ((younger current-prefix-arg)
6681          (days-got nil)
6682          days)
6683      (while (not days-got)
6684        (setq days (if younger
6685                       (read-string "Limit to articles within (in days): ")
6686                     (read-string "Limit to articles older than (in days): ")))
6687        (when (> (length days) 0)
6688          (setq days (read days)))
6689        (if (numberp days)
6690            (setq days-got t)
6691          (message "Please enter a number.")
6692          (sleep-for 1)))
6693      (list days younger)))
6694   (prog1
6695       (let ((data gnus-newsgroup-data)
6696             (cutoff (days-to-time age))
6697             articles d date is-younger)
6698         (while (setq d (pop data))
6699           (when (and (vectorp (gnus-data-header d))
6700                      (setq date (mail-header-date (gnus-data-header d))))
6701             (setq is-younger (time-less-p
6702                               (time-since (condition-case ()
6703                                               (date-to-time date)
6704                                             (error '(0 0))))
6705                               cutoff))
6706             (when (if younger-p
6707                       is-younger
6708                     (not is-younger))
6709               (push (gnus-data-number d) articles))))
6710         (gnus-summary-limit (nreverse articles)))
6711     (gnus-summary-position-point)))
6712
6713 (defun gnus-summary-limit-to-extra (header regexp)
6714   "Limit the summary buffer to articles that match an 'extra' header."
6715   (interactive
6716    (let ((header
6717           (intern
6718            (gnus-completing-read
6719             (symbol-name (car gnus-extra-headers))
6720             "Limit extra header:"
6721             (mapcar (lambda (x)
6722                       (cons (symbol-name x) x))
6723                     gnus-extra-headers)
6724             nil
6725             t))))
6726      (list header
6727            (read-string (format "Limit to header %s (regexp): " header)))))
6728   (when (not (equal "" regexp))
6729     (prog1
6730         (let ((articles (gnus-summary-find-matching
6731                          (cons 'extra header) regexp 'all)))
6732           (unless articles
6733             (error "Found no matches for \"%s\"" regexp))
6734           (gnus-summary-limit articles))
6735       (gnus-summary-position-point))))
6736
6737 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6738 (make-obsolete
6739  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6740
6741 (defun gnus-summary-limit-to-unread (&optional all)
6742   "Limit the summary buffer to articles that are not marked as read.
6743 If ALL is non-nil, limit strictly to unread articles."
6744   (interactive "P")
6745   (if all
6746       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6747     (gnus-summary-limit-to-marks
6748      ;; Concat all the marks that say that an article is read and have
6749      ;; those removed.
6750      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6751            gnus-killed-mark gnus-kill-file-mark
6752            gnus-low-score-mark gnus-expirable-mark
6753            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6754            gnus-duplicate-mark gnus-souped-mark)
6755      'reverse)))
6756
6757 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6758 (make-obsolete 'gnus-summary-delete-marked-with
6759                'gnus-summary-limit-exlude-marks)
6760
6761 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6762   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6763 If REVERSE, limit the summary buffer to articles that are marked
6764 with MARKS.  MARKS can either be a string of marks or a list of marks.
6765 Returns how many articles were removed."
6766   (interactive "sMarks: ")
6767   (gnus-summary-limit-to-marks marks t))
6768
6769 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6770   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6771 If REVERSE (the prefix), limit the summary buffer to articles that are
6772 not marked with MARKS.  MARKS can either be a string of marks or a
6773 list of marks.
6774 Returns how many articles were removed."
6775   (interactive "sMarks: \nP")
6776   (prog1
6777       (let ((data gnus-newsgroup-data)
6778             (marks (if (listp marks) marks
6779                      (append marks nil))) ; Transform to list.
6780             articles)
6781         (while data
6782           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6783                   (memq (gnus-data-mark (car data)) marks))
6784             (push (gnus-data-number (car data)) articles))
6785           (setq data (cdr data)))
6786         (gnus-summary-limit articles))
6787     (gnus-summary-position-point)))
6788
6789 (defun gnus-summary-limit-to-score (&optional score)
6790   "Limit to articles with score at or above SCORE."
6791   (interactive "P")
6792   (setq score (if score
6793                   (prefix-numeric-value score)
6794                 (or gnus-summary-default-score 0)))
6795   (let ((data gnus-newsgroup-data)
6796         articles)
6797     (while data
6798       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6799                 score)
6800         (push (gnus-data-number (car data)) articles))
6801       (setq data (cdr data)))
6802     (prog1
6803         (gnus-summary-limit articles)
6804       (gnus-summary-position-point))))
6805
6806 (defun gnus-summary-limit-include-thread (id)
6807   "Display all the hidden articles that is in the thread with ID in it.
6808 When called interactively, ID is the Message-ID of the current
6809 article."
6810   (interactive (list (mail-header-id (gnus-summary-article-header))))
6811   (let ((articles (gnus-articles-in-thread
6812                    (gnus-id-to-thread (gnus-root-id id)))))
6813     (prog1
6814         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6815         (gnus-summary-limit-include-matching-articles
6816          "subject"
6817          (regexp-quote (gnus-simplify-subject-re
6818                         (mail-header-subject (gnus-id-to-header id)))))
6819       (gnus-summary-position-point))))
6820
6821 (defun gnus-summary-limit-include-matching-articles (header regexp)
6822   "Display all the hidden articles that have HEADERs that match REGEXP."
6823   (interactive (list (read-string "Match on header: ")
6824                      (read-string "Regexp: ")))
6825   (let ((articles (gnus-find-matching-articles header regexp)))
6826     (prog1
6827         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6828       (gnus-summary-position-point))))
6829
6830 (defun gnus-summary-limit-include-dormant ()
6831   "Display all the hidden articles that are marked as dormant.
6832 Note that this command only works on a subset of the articles currently
6833 fetched for this group."
6834   (interactive)
6835   (unless gnus-newsgroup-dormant
6836     (error "There are no dormant articles in this group"))
6837   (prog1
6838       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6839     (gnus-summary-position-point)))
6840
6841 (defun gnus-summary-limit-exclude-dormant ()
6842   "Hide all dormant articles."
6843   (interactive)
6844   (prog1
6845       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6846     (gnus-summary-position-point)))
6847
6848 (defun gnus-summary-limit-exclude-childless-dormant ()
6849   "Hide all dormant articles that have no children."
6850   (interactive)
6851   (let ((data (gnus-data-list t))
6852         articles d children)
6853     ;; Find all articles that are either not dormant or have
6854     ;; children.
6855     (while (setq d (pop data))
6856       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6857                 (and (setq children
6858                            (gnus-article-children (gnus-data-number d)))
6859                      (let (found)
6860                        (while children
6861                          (when (memq (car children) articles)
6862                            (setq children nil
6863                                  found t))
6864                          (pop children))
6865                        found)))
6866         (push (gnus-data-number d) articles)))
6867     ;; Do the limiting.
6868     (prog1
6869         (gnus-summary-limit articles)
6870       (gnus-summary-position-point))))
6871
6872 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6873   "Mark all unread excluded articles as read.
6874 If ALL, mark even excluded ticked and dormants as read."
6875   (interactive "P")
6876   (let ((articles (gnus-sorted-complement
6877                    (sort
6878                     (mapcar (lambda (h) (mail-header-number h))
6879                             gnus-newsgroup-headers)
6880                     '<)
6881                    (sort gnus-newsgroup-limit '<)))
6882         article)
6883     (setq gnus-newsgroup-unreads
6884           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6885     (if all
6886         (setq gnus-newsgroup-dormant nil
6887               gnus-newsgroup-marked nil
6888               gnus-newsgroup-reads
6889               (nconc
6890                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6891                gnus-newsgroup-reads))
6892       (while (setq article (pop articles))
6893         (unless (or (memq article gnus-newsgroup-dormant)
6894                     (memq article gnus-newsgroup-marked))
6895           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6896
6897 (defun gnus-summary-limit (articles &optional pop)
6898   (if pop
6899       ;; We pop the previous limit off the stack and use that.
6900       (setq articles (car gnus-newsgroup-limits)
6901             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6902     ;; We use the new limit, so we push the old limit on the stack.
6903     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6904   ;; Set the limit.
6905   (setq gnus-newsgroup-limit articles)
6906   (let ((total (length gnus-newsgroup-data))
6907         (data (gnus-data-find-list (gnus-summary-article-number)))
6908         (gnus-summary-mark-below nil)   ; Inhibit this.
6909         found)
6910     ;; This will do all the work of generating the new summary buffer
6911     ;; according to the new limit.
6912     (gnus-summary-prepare)
6913     ;; Hide any threads, possibly.
6914     (and gnus-show-threads
6915          gnus-thread-hide-subtree
6916          (gnus-summary-hide-all-threads))
6917     ;; Try to return to the article you were at, or one in the
6918     ;; neighborhood.
6919     (when data
6920       ;; We try to find some article after the current one.
6921       (while data
6922         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6923           (setq data nil
6924                 found t))
6925         (setq data (cdr data))))
6926     (unless found
6927       ;; If there is no data, that means that we were after the last
6928       ;; article.  The same goes when we can't find any articles
6929       ;; after the current one.
6930       (goto-char (point-max))
6931       (gnus-summary-find-prev))
6932     (gnus-set-mode-line 'summary)
6933     ;; We return how many articles were removed from the summary
6934     ;; buffer as a result of the new limit.
6935     (- total (length gnus-newsgroup-data))))
6936
6937 (defsubst gnus-invisible-cut-children (threads)
6938   (let ((num 0))
6939     (while threads
6940       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6941         (incf num))
6942       (pop threads))
6943     (< num 2)))
6944
6945 (defsubst gnus-cut-thread (thread)
6946   "Go forwards in the thread until we find an article that we want to display."
6947   (when (or (eq gnus-fetch-old-headers 'some)
6948             (eq gnus-fetch-old-headers 'invisible)
6949             (numberp gnus-fetch-old-headers)
6950             (eq gnus-build-sparse-threads 'some)
6951             (eq gnus-build-sparse-threads 'more))
6952     ;; Deal with old-fetched headers and sparse threads.
6953     (while (and
6954             thread
6955             (or
6956              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6957              (gnus-summary-article-ancient-p
6958               (mail-header-number (car thread))))
6959             (if (or (<= (length (cdr thread)) 1)
6960                     (eq gnus-fetch-old-headers 'invisible))
6961                 (setq gnus-newsgroup-limit
6962                       (delq (mail-header-number (car thread))
6963                             gnus-newsgroup-limit)
6964                       thread (cadr thread))
6965               (when (gnus-invisible-cut-children (cdr thread))
6966                 (let ((th (cdr thread)))
6967                   (while th
6968                     (if (memq (mail-header-number (caar th))
6969                               gnus-newsgroup-limit)
6970                         (setq thread (car th)
6971                               th nil)
6972                       (setq th (cdr th))))))))))
6973   thread)
6974
6975 (defun gnus-cut-threads (threads)
6976   "Cut off all uninteresting articles from the beginning of threads."
6977   (when (or (eq gnus-fetch-old-headers 'some)
6978             (eq gnus-fetch-old-headers 'invisible)
6979             (numberp gnus-fetch-old-headers)
6980             (eq gnus-build-sparse-threads 'some)
6981             (eq gnus-build-sparse-threads 'more))
6982     (let ((th threads))
6983       (while th
6984         (setcar th (gnus-cut-thread (car th)))
6985         (setq th (cdr th)))))
6986   ;; Remove nixed out threads.
6987   (delq nil threads))
6988
6989 (defun gnus-summary-initial-limit (&optional show-if-empty)
6990   "Figure out what the initial limit is supposed to be on group entry.
6991 This entails weeding out unwanted dormants, low-scored articles,
6992 fetch-old-headers verbiage, and so on."
6993   ;; Most groups have nothing to remove.
6994   (if (or gnus-inhibit-limiting
6995           (and (null gnus-newsgroup-dormant)
6996                (not (eq gnus-fetch-old-headers 'some))
6997                (not (numberp gnus-fetch-old-headers))
6998                (not (eq gnus-fetch-old-headers 'invisible))
6999                (null gnus-summary-expunge-below)
7000                (not (eq gnus-build-sparse-threads 'some))
7001                (not (eq gnus-build-sparse-threads 'more))
7002                (null gnus-thread-expunge-below)
7003                (not gnus-use-nocem)))
7004       ()                                ; Do nothing.
7005     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7006     (setq gnus-newsgroup-limit nil)
7007     (mapatoms
7008      (lambda (node)
7009        (unless (car (symbol-value node))
7010          ;; These threads have no parents -- they are roots.
7011          (let ((nodes (cdr (symbol-value node)))
7012                thread)
7013            (while nodes
7014              (if (and gnus-thread-expunge-below
7015                       (< (gnus-thread-total-score (car nodes))
7016                          gnus-thread-expunge-below))
7017                  (gnus-expunge-thread (pop nodes))
7018                (setq thread (pop nodes))
7019                (gnus-summary-limit-children thread))))))
7020      gnus-newsgroup-dependencies)
7021     ;; If this limitation resulted in an empty group, we might
7022     ;; pop the previous limit and use it instead.
7023     (when (and (not gnus-newsgroup-limit)
7024                show-if-empty)
7025       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7026     gnus-newsgroup-limit))
7027
7028 (defun gnus-summary-limit-children (thread)
7029   "Return 1 if this subthread is visible and 0 if it is not."
7030   ;; First we get the number of visible children to this thread.  This
7031   ;; is done by recursing down the thread using this function, so this
7032   ;; will really go down to a leaf article first, before slowly
7033   ;; working its way up towards the root.
7034   (when thread
7035     (let ((children
7036            (if (cdr thread)
7037                (apply '+ (mapcar 'gnus-summary-limit-children
7038                                  (cdr thread)))
7039              0))
7040           (number (mail-header-number (car thread)))
7041           score)
7042       (if (and
7043            (not (memq number gnus-newsgroup-marked))
7044            (or
7045             ;; If this article is dormant and has absolutely no visible
7046             ;; children, then this article isn't visible.
7047             (and (memq number gnus-newsgroup-dormant)
7048                  (zerop children))
7049             ;; If this is "fetch-old-headered" and there is no
7050             ;; visible children, then we don't want this article.
7051             (and (or (eq gnus-fetch-old-headers 'some)
7052                      (numberp gnus-fetch-old-headers))
7053                  (gnus-summary-article-ancient-p number)
7054                  (zerop children))
7055             ;; If this is "fetch-old-headered" and `invisible', then
7056             ;; we don't want this article.
7057             (and (eq gnus-fetch-old-headers 'invisible)
7058                  (gnus-summary-article-ancient-p number))
7059             ;; If this is a sparsely inserted article with no children,
7060             ;; we don't want it.
7061             (and (eq gnus-build-sparse-threads 'some)
7062                  (gnus-summary-article-sparse-p number)
7063                  (zerop children))
7064             ;; If we use expunging, and this article is really
7065             ;; low-scored, then we don't want this article.
7066             (when (and gnus-summary-expunge-below
7067                        (< (setq score
7068                                 (or (cdr (assq number gnus-newsgroup-scored))
7069                                     gnus-summary-default-score))
7070                           gnus-summary-expunge-below))
7071               ;; We increase the expunge-tally here, but that has
7072               ;; nothing to do with the limits, really.
7073               (incf gnus-newsgroup-expunged-tally)
7074               ;; We also mark as read here, if that's wanted.
7075               (when (and gnus-summary-mark-below
7076                          (< score gnus-summary-mark-below))
7077                 (setq gnus-newsgroup-unreads
7078                       (delq number gnus-newsgroup-unreads))
7079                 (if gnus-newsgroup-auto-expire
7080                     (push number gnus-newsgroup-expirable)
7081                   (push (cons number gnus-low-score-mark)
7082                         gnus-newsgroup-reads)))
7083               t)
7084             ;; Check NoCeM things.
7085             (if (and gnus-use-nocem
7086                      (gnus-nocem-unwanted-article-p
7087                       (mail-header-id (car thread))))
7088                 (progn
7089                   (setq gnus-newsgroup-unreads
7090                         (delq number gnus-newsgroup-unreads))
7091                   t))))
7092           ;; Nope, invisible article.
7093           0
7094         ;; Ok, this article is to be visible, so we add it to the limit
7095         ;; and return 1.
7096         (push number gnus-newsgroup-limit)
7097         1))))
7098
7099 (defun gnus-expunge-thread (thread)
7100   "Mark all articles in THREAD as read."
7101   (let* ((number (mail-header-number (car thread))))
7102     (incf gnus-newsgroup-expunged-tally)
7103     ;; We also mark as read here, if that's wanted.
7104     (setq gnus-newsgroup-unreads
7105           (delq number gnus-newsgroup-unreads))
7106     (if gnus-newsgroup-auto-expire
7107         (push number gnus-newsgroup-expirable)
7108       (push (cons number gnus-low-score-mark)
7109             gnus-newsgroup-reads)))
7110   ;; Go recursively through all subthreads.
7111   (mapcar 'gnus-expunge-thread (cdr thread)))
7112
7113 ;; Summary article oriented commands
7114
7115 (defun gnus-summary-refer-parent-article (n)
7116   "Refer parent article N times.
7117 If N is negative, go to ancestor -N instead.
7118 The difference between N and the number of articles fetched is returned."
7119   (interactive "p")
7120   (let ((skip 1)
7121         error header ref)
7122     (when (not (natnump n))
7123       (setq skip (abs n)
7124             n 1))
7125     (while (and (> n 0)
7126                 (not error))
7127       (setq header (gnus-summary-article-header))
7128       (if (and (eq (mail-header-number header)
7129                    (cdr gnus-article-current))
7130                (equal gnus-newsgroup-name
7131                       (car gnus-article-current)))
7132           ;; If we try to find the parent of the currently
7133           ;; displayed article, then we take a look at the actual
7134           ;; References header, since this is slightly more
7135           ;; reliable than the References field we got from the
7136           ;; server.
7137           (save-excursion
7138             (set-buffer gnus-original-article-buffer)
7139             (nnheader-narrow-to-headers)
7140             (unless (setq ref (message-fetch-field "references"))
7141               (setq ref (message-fetch-field "in-reply-to")))
7142             (widen))
7143         (setq ref
7144               ;; It's not the current article, so we take a bet on
7145               ;; the value we got from the server.
7146               (mail-header-references header)))
7147       (if (and ref
7148                (not (equal ref "")))
7149           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7150             (gnus-message 1 "Couldn't find parent"))
7151         (gnus-message 1 "No references in article %d"
7152                       (gnus-summary-article-number))
7153         (setq error t))
7154       (decf n))
7155     (gnus-summary-position-point)
7156     n))
7157
7158 (defun gnus-summary-refer-references ()
7159   "Fetch all articles mentioned in the References header.
7160 Return the number of articles fetched."
7161   (interactive)
7162   (let ((ref (mail-header-references (gnus-summary-article-header)))
7163         (current (gnus-summary-article-number))
7164         (n 0))
7165     (if (or (not ref)
7166             (equal ref ""))
7167         (error "No References in the current article")
7168       ;; For each Message-ID in the References header...
7169       (while (string-match "<[^>]*>" ref)
7170         (incf n)
7171         ;; ... fetch that article.
7172         (gnus-summary-refer-article
7173          (prog1 (match-string 0 ref)
7174            (setq ref (substring ref (match-end 0))))))
7175       (gnus-summary-goto-subject current)
7176       (gnus-summary-position-point)
7177       n)))
7178
7179 (defun gnus-summary-refer-thread (&optional limit)
7180   "Fetch all articles in the current thread.
7181 If LIMIT (the numerical prefix), fetch that many old headers instead
7182 of what's specified by the `gnus-refer-thread-limit' variable."
7183   (interactive "P")
7184   (let ((id (mail-header-id (gnus-summary-article-header)))
7185         (limit (if limit (prefix-numeric-value limit)
7186                  gnus-refer-thread-limit)))
7187     ;; We want to fetch LIMIT *old* headers, but we also have to
7188     ;; re-fetch all the headers in the current buffer, because many of
7189     ;; them may be undisplayed.  So we adjust LIMIT.
7190     (when (numberp limit)
7191       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7192     (unless (eq gnus-fetch-old-headers 'invisible)
7193       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7194       ;; Retrieve the headers and read them in.
7195       (if (eq (gnus-retrieve-headers
7196                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7197               'nov)
7198           (gnus-build-all-threads)
7199         (error "Can't fetch thread from backends that don't support NOV"))
7200       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7201     (gnus-summary-limit-include-thread id)))
7202
7203 (defun gnus-summary-refer-article (message-id)
7204   "Fetch an article specified by MESSAGE-ID."
7205   (interactive "sMessage-ID: ")
7206   (when (and (stringp message-id)
7207              (not (zerop (length message-id))))
7208     ;; Construct the correct Message-ID if necessary.
7209     ;; Suggested by tale@pawl.rpi.edu.
7210     (unless (string-match "^<" message-id)
7211       (setq message-id (concat "<" message-id)))
7212     (unless (string-match ">$" message-id)
7213       (setq message-id (concat message-id ">")))
7214     (let* ((header (gnus-id-to-header message-id))
7215            (sparse (and header
7216                         (gnus-summary-article-sparse-p
7217                          (mail-header-number header))
7218                         (memq (mail-header-number header)
7219                               gnus-newsgroup-limit)))
7220            number)
7221       (cond
7222        ;; If the article is present in the buffer we just go to it.
7223        ((and header
7224              (or (not (gnus-summary-article-sparse-p
7225                        (mail-header-number header)))
7226                  sparse))
7227         (prog1
7228             (gnus-summary-goto-article
7229              (mail-header-number header) nil t)
7230           (when sparse
7231             (gnus-summary-update-article (mail-header-number header)))))
7232        (t
7233         ;; We fetch the article.
7234         (catch 'found
7235           (dolist (gnus-override-method (gnus-refer-article-methods))
7236             (gnus-check-server gnus-override-method)
7237             ;; Fetch the header, and display the article.
7238             (when (setq number (gnus-summary-insert-subject message-id))
7239               (gnus-summary-select-article nil nil nil number)
7240               (throw 'found t)))
7241           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7242
7243 (defun gnus-refer-article-methods ()
7244   "Return a list of referrable methods."
7245   (cond
7246    ;; No method, so we default to current and native.
7247    ((null gnus-refer-article-method)
7248     (list gnus-current-select-method gnus-select-method))
7249    ;; Current.
7250    ((eq 'current gnus-refer-article-method)
7251     (list gnus-current-select-method))
7252    ;; List of select methods.
7253    ((not (and (symbolp (car gnus-refer-article-method))
7254               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7255     (let (out)
7256       (dolist (method gnus-refer-article-method)
7257         (push (if (eq 'current method)
7258                   gnus-current-select-method
7259                 method)
7260               out))
7261       (nreverse out)))
7262    ;; One single select method.
7263    (t
7264     (list gnus-refer-article-method))))
7265
7266 (defun gnus-summary-edit-parameters ()
7267   "Edit the group parameters of the current group."
7268   (interactive)
7269   (gnus-group-edit-group gnus-newsgroup-name 'params))
7270
7271 (defun gnus-summary-customize-parameters ()
7272   "Customize the group parameters of the current group."
7273   (interactive)
7274   (gnus-group-customize gnus-newsgroup-name))
7275
7276 (defun gnus-summary-enter-digest-group (&optional force)
7277   "Enter an nndoc group based on the current article.
7278 If FORCE, force a digest interpretation.  If not, try
7279 to guess what the document format is."
7280   (interactive "P")
7281   (let ((conf gnus-current-window-configuration))
7282     (save-excursion
7283       (gnus-summary-select-article))
7284     (setq gnus-current-window-configuration conf)
7285     (let* ((name (format "%s-%d"
7286                          (gnus-group-prefixed-name
7287                           gnus-newsgroup-name (list 'nndoc ""))
7288                          (save-excursion
7289                            (set-buffer gnus-summary-buffer)
7290                            gnus-current-article)))
7291            (ogroup gnus-newsgroup-name)
7292            (params (append (gnus-info-params (gnus-get-info ogroup))
7293                            (list (cons 'to-group ogroup))
7294                            (list (cons 'save-article-group ogroup))))
7295            (case-fold-search t)
7296            (buf (current-buffer))
7297            dig to-address)
7298       (save-excursion
7299         (set-buffer gnus-original-article-buffer)
7300         ;; Have the digest group inherit the main mail address of
7301         ;; the parent article.
7302         (when (setq to-address (or (message-fetch-field "reply-to")
7303                                    (message-fetch-field "from")))
7304           (setq params (append
7305                         (list (cons 'to-address
7306                                     (funcall gnus-decode-encoded-word-function
7307                                              to-address))))))
7308         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7309         (insert-buffer-substring gnus-original-article-buffer)
7310         ;; Remove lines that may lead nndoc to misinterpret the
7311         ;; document type.
7312         (narrow-to-region
7313          (goto-char (point-min))
7314          (or (search-forward "\n\n" nil t) (point)))
7315         (goto-char (point-min))
7316         (delete-matching-lines "^Path:\\|^From ")
7317         (widen))
7318       (unwind-protect
7319           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7320                     (gnus-newsgroup-ephemeral-ignored-charsets
7321                      gnus-newsgroup-ignored-charsets))
7322                 (gnus-group-read-ephemeral-group
7323                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7324                               (nndoc-article-type
7325                                ,(if force 'mbox 'guess))) t))
7326               ;; Make all postings to this group go to the parent group.
7327               (nconc (gnus-info-params (gnus-get-info name))
7328                      params)
7329             ;; Couldn't select this doc group.
7330             (switch-to-buffer buf)
7331             (gnus-set-global-variables)
7332             (gnus-configure-windows 'summary)
7333             (gnus-message 3 "Article couldn't be entered?"))
7334         (kill-buffer dig)))))
7335
7336 (defun gnus-summary-read-document (n)
7337   "Open a new group based on the current article(s).
7338 This will allow you to read digests and other similar
7339 documents as newsgroups.
7340 Obeys the standard process/prefix convention."
7341   (interactive "P")
7342   (let* ((articles (gnus-summary-work-articles n))
7343          (ogroup gnus-newsgroup-name)
7344          (params (append (gnus-info-params (gnus-get-info ogroup))
7345                          (list (cons 'to-group ogroup))))
7346          article group egroup groups vgroup)
7347     (while (setq article (pop articles))
7348       (setq group (format "%s-%d" gnus-newsgroup-name article))
7349       (gnus-summary-remove-process-mark article)
7350       (when (gnus-summary-display-article article)
7351         (save-excursion
7352           (with-temp-buffer
7353             (insert-buffer-substring gnus-original-article-buffer)
7354             ;; Remove some headers that may lead nndoc to make
7355             ;; the wrong guess.
7356             (message-narrow-to-head)
7357             (goto-char (point-min))
7358             (delete-matching-lines "^\\(Path\\):\\|^From ")
7359             (widen)
7360             (if (setq egroup
7361                       (gnus-group-read-ephemeral-group
7362                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7363                                      (nndoc-article-type guess))
7364                        t nil t))
7365                 (progn
7366                   ;; Make all postings to this group go to the parent group.
7367                   (nconc (gnus-info-params (gnus-get-info egroup))
7368                          params)
7369                   (push egroup groups))
7370               ;; Couldn't select this doc group.
7371               (gnus-error 3 "Article couldn't be entered"))))))
7372     ;; Now we have selected all the documents.
7373     (cond
7374      ((not groups)
7375       (error "None of the articles could be interpreted as documents"))
7376      ((gnus-group-read-ephemeral-group
7377        (setq vgroup (format
7378                      "nnvirtual:%s-%s" gnus-newsgroup-name
7379                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7380        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7381        t
7382        (cons (current-buffer) 'summary)))
7383      (t
7384       (error "Couldn't select virtual nndoc group")))))
7385
7386 (defun gnus-summary-isearch-article (&optional regexp-p)
7387   "Do incremental search forward on the current article.
7388 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7389   (interactive "P")
7390   (let* ((gnus-inhibit-treatment t)
7391          (old (gnus-summary-select-article)))
7392     (gnus-configure-windows 'article)
7393     (gnus-eval-in-buffer-window gnus-article-buffer
7394       (save-restriction
7395         (widen)
7396         (when (eq 'old old)
7397           (gnus-article-show-all-headers))
7398         (goto-char (point-min))
7399         (isearch-forward regexp-p)))))
7400
7401 (defun gnus-summary-search-article-forward (regexp &optional backward)
7402   "Search for an article containing REGEXP forward.
7403 If BACKWARD, search backward instead."
7404   (interactive
7405    (list (read-string
7406           (format "Search article %s (regexp%s): "
7407                   (if current-prefix-arg "backward" "forward")
7408                   (if gnus-last-search-regexp
7409                       (concat ", default " gnus-last-search-regexp)
7410                     "")))
7411          current-prefix-arg))
7412   (if (string-equal regexp "")
7413       (setq regexp (or gnus-last-search-regexp ""))
7414     (setq gnus-last-search-regexp regexp)
7415     (setq gnus-article-before-search gnus-current-article))
7416   ;; Intentionally set gnus-last-article.
7417   (setq gnus-last-article gnus-article-before-search)
7418   (let ((gnus-last-article gnus-last-article))
7419     (if (gnus-summary-search-article regexp backward)
7420         (gnus-summary-show-thread)
7421       (error "Search failed: \"%s\"" regexp))))
7422
7423 (defun gnus-summary-search-article-backward (regexp)
7424   "Search for an article containing REGEXP backward."
7425   (interactive
7426    (list (read-string
7427           (format "Search article backward (regexp%s): "
7428                   (if gnus-last-search-regexp
7429                       (concat ", default " gnus-last-search-regexp)
7430                     "")))))
7431   (gnus-summary-search-article-forward regexp 'backward))
7432
7433 (eval-when-compile
7434   (defmacro gnus-summary-search-article-position-point (regexp backward)
7435     "Dehighlight the last matched text and goto the beginning position."
7436     (` (if (and gnus-summary-search-article-matched-data
7437                 (let ((text (caddr gnus-summary-search-article-matched-data))
7438                       (inhibit-read-only t)
7439                       buffer-read-only)
7440                   (delete-region
7441                    (goto-char (car gnus-summary-search-article-matched-data))
7442                    (cadr gnus-summary-search-article-matched-data))
7443                   (insert text)
7444                   (string-match (, regexp) text)))
7445            (if (, backward) (beginning-of-line) (end-of-line))
7446          (goto-char (if (, backward) (point-max) (point-min))))))
7447
7448   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7449     "Place point where X-Face image is displayed."
7450     (if (featurep 'xemacs)
7451         (` (let ((end (if (search-forward "\n\n" nil t)
7452                           (goto-char (1- (point)))
7453                         (point-min)))
7454                  extent)
7455              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7456              (unless (and (re-search-forward "^From:" end t)
7457                           (setq extent (extent-at (point)))
7458                           (extent-begin-glyph extent))
7459                (goto-char (, opoint)))))
7460       (` (let ((end (if (search-forward "\n\n" nil t)
7461                         (goto-char (1- (point)))
7462                       (point-min)))
7463                (start (or (search-backward "\n\n" nil t) (point-min))))
7464            (goto-char
7465             (or (text-property-any start end 'x-face-image t);; x-face-e21
7466                 (text-property-any start end 'x-face-mule-bitmap-image t)
7467                 (, opoint)))))))
7468
7469   (defmacro gnus-summary-search-article-highlight-matched-text
7470     (backward treated x-face)
7471     "Highlight matched text in the function `gnus-summary-search-article'."
7472     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7473              (end (set-marker (make-marker) (match-end 0)))
7474              (inhibit-read-only t)
7475              buffer-read-only)
7476          (unless treated
7477            (let ((,@
7478                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7479                     (mapcar
7480                      (lambda (item) (setq items (delq item items)))
7481                      '(gnus-treat-buttonize
7482                        gnus-treat-fill-article
7483                        gnus-treat-fill-long-lines
7484                        gnus-treat-emphasize
7485                        gnus-treat-highlight-headers
7486                        gnus-treat-highlight-citation
7487                        gnus-treat-highlight-signature
7488                        gnus-treat-overstrike
7489                        gnus-treat-display-xface
7490                        gnus-treat-buttonize-head
7491                        gnus-treat-decode-article-as-default-mime-charset))
7492                     (static-if (featurep 'xemacs)
7493                         items
7494                       (cons '(x-face-mule-delete-x-face-field
7495                               (quote never))
7496                             items))))
7497                  (gnus-treat-display-xface
7498                   (when (, x-face) gnus-treat-display-xface)))
7499              (gnus-article-prepare-mime-display)))
7500          (goto-char (if (, backward) start end))
7501          (when (, x-face)
7502            (gnus-summary-search-article-highlight-goto-x-face (point)))
7503          (setq gnus-summary-search-article-matched-data
7504                (list start end (buffer-substring start end)))
7505          (unless (eq start end);; matched text has been deleted. :-<
7506            (put-text-property start end 'face
7507                               (or (find-face 'isearch)
7508                                   'secondary-selection))))))
7509   )
7510
7511 (defun gnus-summary-search-article (regexp &optional backward)
7512   "Search for an article containing REGEXP.
7513 Optional argument BACKWARD means do search for backward.
7514 `gnus-select-article-hook' is not called during the search."
7515   ;; We have to require this here to make sure that the following
7516   ;; dynamic binding isn't shadowed by autoloading.
7517   (require 'gnus-async)
7518   (require 'gnus-art)
7519   (let ((gnus-select-article-hook nil)  ;Disable hook.
7520         (gnus-article-prepare-hook nil)
7521         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7522         (gnus-use-article-prefetch nil)
7523         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7524         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7525         (sum (current-buffer))
7526         (found nil)
7527         point treated)
7528     (gnus-save-hidden-threads
7529       (static-if (featurep 'xemacs)
7530           (let ((gnus-inhibit-treatment t))
7531             (setq treated (eq 'old (gnus-summary-select-article)))
7532             (when (and treated
7533                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7534                                  (window-live-p (get-buffer-window
7535                                                  gnus-article-buffer t)))))
7536               (gnus-summary-select-article nil t)
7537               (setq treated nil)))
7538         (let ((gnus-inhibit-treatment t)
7539               (x-face-mule-delete-x-face-field 'never))
7540           (setq treated (eq 'old (gnus-summary-select-article)))
7541           (when (and treated
7542                      (not
7543                       (and (gnus-buffer-live-p gnus-article-buffer)
7544                            (window-live-p (get-buffer-window
7545                                            gnus-article-buffer t))
7546                            (or (not (string-match "^\\^X-Face:" regexp))
7547                                (with-current-buffer gnus-article-buffer
7548                                  gnus-summary-search-article-matched-data)))))
7549             (gnus-summary-select-article nil t)
7550             (setq treated nil))))
7551       (set-buffer gnus-article-buffer)
7552       (widen)
7553       (if treated
7554           (progn
7555             (gnus-article-show-all-headers)
7556             (gnus-summary-search-article-position-point regexp backward))
7557         (goto-char (if backward (point-max) (point-min))))
7558       (while (not found)
7559         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7560         (if (if backward
7561                 (re-search-backward regexp nil t)
7562               (re-search-forward regexp nil t))
7563             ;; We found the regexp.
7564             (progn
7565               (gnus-summary-search-article-highlight-matched-text
7566                backward treated (string-match "^\\^X-Face:" regexp))
7567               (setq found 'found)
7568               (forward-line
7569                (/ (- 2 (window-height
7570                         (get-buffer-window gnus-article-buffer t)))
7571                   2))
7572               (set-window-start
7573                (get-buffer-window (current-buffer))
7574                (point))
7575               (set-buffer sum)
7576               (setq point (point)))
7577           ;; We didn't find it, so we go to the next article.
7578           (set-buffer sum)
7579           (setq found 'not)
7580           (while (eq found 'not)
7581             (if (not (if backward (gnus-summary-find-prev)
7582                        (gnus-summary-find-next)))
7583                 ;; No more articles.
7584                 (setq found t)
7585               ;; Select the next article and adjust point.
7586               (unless (gnus-summary-article-sparse-p
7587                        (gnus-summary-article-number))
7588                 (setq found nil)
7589                 (let ((gnus-inhibit-treatment t))
7590                   (gnus-summary-select-article))
7591                 (setq treated nil)
7592                 (set-buffer gnus-article-buffer)
7593                 (widen)
7594                 (goto-char (if backward (point-max) (point-min))))))))
7595       (gnus-message 7 ""))
7596     ;; Return whether we found the regexp.
7597     (when (eq found 'found)
7598       (goto-char point)
7599       (gnus-summary-show-thread)
7600       (gnus-summary-goto-subject gnus-current-article)
7601       (gnus-summary-position-point)
7602       t)))
7603
7604 (defun gnus-find-matching-articles (header regexp)
7605   "Return a list of all articles that match REGEXP on HEADER.
7606 This search includes all articles in the current group that Gnus has
7607 fetched headers for, whether they are displayed or not."
7608   (let ((articles nil)
7609         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7610         (case-fold-search t))
7611     (dolist (header gnus-newsgroup-headers)
7612       (when (string-match regexp (funcall func header))
7613         (push (mail-header-number header) articles)))
7614     (nreverse articles)))
7615
7616 (defun gnus-summary-find-matching (header regexp &optional backward unread
7617                                           not-case-fold)
7618   "Return a list of all articles that match REGEXP on HEADER.
7619 The search stars on the current article and goes forwards unless
7620 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7621 If UNREAD is non-nil, only unread articles will
7622 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7623 in the comparisons."
7624   (let ((case-fold-search (not not-case-fold))
7625         articles d func)
7626     (if (consp header)
7627         (if (eq (car header) 'extra)
7628             (setq func
7629                   `(lambda (h)
7630                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7631                          "")))
7632           (error "%s is an invalid header" header))
7633       (unless (fboundp (intern (concat "mail-header-" header)))
7634         (error "%s is not a valid header" header))
7635       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7636     (dolist (d (if (eq backward 'all)
7637                    gnus-newsgroup-data
7638                  (gnus-data-find-list
7639                   (gnus-summary-article-number)
7640                   (gnus-data-list backward))))
7641       (when (and (or (not unread)       ; We want all articles...
7642                      (gnus-data-unread-p d)) ; Or just unreads.
7643                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7644                  (string-match regexp
7645                                (funcall func (gnus-data-header d)))) ; Match.
7646         (push (gnus-data-number d) articles))) ; Success!
7647     (nreverse articles)))
7648
7649 (defun gnus-summary-execute-command (header regexp command &optional backward)
7650   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7651 If HEADER is an empty string (or nil), the match is done on the entire
7652 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7653   (interactive
7654    (list (let ((completion-ignore-case t))
7655            (completing-read
7656             "Header name: "
7657             (mapcar (lambda (header) (list (format "%s" header)))
7658                     (append
7659                      '("Number" "Subject" "From" "Lines" "Date"
7660                        "Message-ID" "Xref" "References" "Body")
7661                      gnus-extra-headers))
7662             nil 'require-match))
7663          (read-string "Regexp: ")
7664          (read-key-sequence "Command: ")
7665          current-prefix-arg))
7666   (when (equal header "Body")
7667     (setq header ""))
7668   ;; Hidden thread subtrees must be searched as well.
7669   (gnus-summary-show-all-threads)
7670   ;; We don't want to change current point nor window configuration.
7671   (save-excursion
7672     (save-window-excursion
7673       (gnus-message 6 "Executing %s..." (key-description command))
7674       ;; We'd like to execute COMMAND interactively so as to give arguments.
7675       (gnus-execute header regexp
7676                     `(call-interactively ',(key-binding command))
7677                     backward)
7678       (gnus-message 6 "Executing %s...done" (key-description command)))))
7679
7680 (defun gnus-summary-beginning-of-article ()
7681   "Scroll the article back to the beginning."
7682   (interactive)
7683   (gnus-summary-select-article)
7684   (gnus-configure-windows 'article)
7685   (gnus-eval-in-buffer-window gnus-article-buffer
7686     (widen)
7687     (goto-char (point-min))
7688     (when gnus-page-broken
7689       (gnus-narrow-to-page))))
7690
7691 (defun gnus-summary-end-of-article ()
7692   "Scroll to the end of the article."
7693   (interactive)
7694   (gnus-summary-select-article)
7695   (gnus-configure-windows 'article)
7696   (gnus-eval-in-buffer-window gnus-article-buffer
7697     (widen)
7698     (goto-char (point-max))
7699     (recenter -3)
7700     (when gnus-page-broken
7701       (gnus-narrow-to-page))))
7702
7703 (defun gnus-summary-print-article (&optional filename n)
7704   "Generate and print a PostScript image of the N next (mail) articles.
7705
7706 If N is negative, print the N previous articles.  If N is nil and articles
7707 have been marked with the process mark, print these instead.
7708
7709 If the optional first argument FILENAME is nil, send the image to the
7710 printer.  If FILENAME is a string, save the PostScript image in a file with
7711 that name.  If FILENAME is a number, prompt the user for the name of the file
7712 to save in."
7713   (interactive (list (ps-print-preprint current-prefix-arg)))
7714   (dolist (article (gnus-summary-work-articles n))
7715     (gnus-summary-select-article nil nil 'pseudo article)
7716     (gnus-eval-in-buffer-window gnus-article-buffer
7717       (let ((buffer (generate-new-buffer " *print*")))
7718         (unwind-protect
7719             (progn
7720               (copy-to-buffer buffer (point-min) (point-max))
7721               (set-buffer buffer)
7722               (gnus-article-delete-invisible-text)
7723               (when (gnus-visual-p 'article-highlight 'highlight)
7724                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7725                 ;; highlight.
7726                 (let ((gnus-article-buffer buffer))
7727                   (gnus-article-highlight-citation t)
7728                   (gnus-article-highlight-signature)))
7729               (let ((ps-left-header
7730                      (list
7731                       (concat "("
7732                               (mail-header-subject gnus-current-headers) ")")
7733                       (concat "("
7734                               (mail-header-from gnus-current-headers) ")")))
7735                     (ps-right-header
7736                      (list
7737                       "/pagenumberstring load"
7738                       (concat "("
7739                               (mail-header-date gnus-current-headers) ")"))))
7740                 (gnus-run-hooks 'gnus-ps-print-hook)
7741                 (save-excursion
7742                   (if window-system
7743                       (ps-spool-buffer-with-faces)
7744                     (ps-spool-buffer)))))
7745           (kill-buffer buffer))))
7746     (gnus-summary-remove-process-mark article))
7747   (ps-despool filename))
7748
7749 (defun gnus-summary-show-article (&optional arg)
7750   "Force re-fetching of the current article.
7751 If ARG (the prefix) is a number, show the article with the charset
7752 defined in `gnus-summary-show-article-charset-alist', or the charset
7753 inputed.
7754 If ARG (the prefix) is non-nil and not a number, show the raw article
7755 without any article massaging functions being run."
7756   (interactive "P")
7757   (cond
7758    ((numberp arg)
7759     (let ((gnus-newsgroup-charset
7760            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7761                (read-coding-system "Charset: ")))
7762           (gnus-newsgroup-ignored-charsets 'gnus-all))
7763       (gnus-summary-select-article nil 'force)
7764       (let ((deps gnus-newsgroup-dependencies)
7765             head header)
7766         (save-excursion
7767           (set-buffer gnus-original-article-buffer)
7768           (save-restriction
7769             (message-narrow-to-head)
7770             (setq head (buffer-string)))
7771           (with-temp-buffer
7772             (insert (format "211 %d Article retrieved.\n"
7773                             (cdr gnus-article-current)))
7774             (insert head)
7775             (insert ".\n")
7776             (let ((nntp-server-buffer (current-buffer)))
7777               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7778         (gnus-data-set-header
7779          (gnus-data-find (cdr gnus-article-current))
7780          header)
7781         (gnus-summary-update-article-line
7782          (cdr gnus-article-current) header))))
7783    ((not arg)
7784     ;; Select the article the normal way.
7785     (gnus-summary-select-article nil 'force))
7786    (t
7787     ;; We have to require this here to make sure that the following
7788     ;; dynamic binding isn't shadowed by autoloading.
7789     (require 'gnus-async)
7790     (require 'gnus-art)
7791     ;; Bind the article treatment functions to nil.
7792     (let ((gnus-have-all-headers t)
7793           gnus-article-prepare-hook
7794           gnus-article-decode-hook
7795           gnus-break-pages
7796           gnus-show-mime
7797           (gnus-inhibit-treatment t))
7798       (gnus-summary-select-article nil 'force))))
7799   (gnus-summary-goto-subject gnus-current-article)
7800   (gnus-summary-position-point))
7801
7802 (defun gnus-summary-show-raw-article ()
7803   "Show the raw article without any article massaging functions being run."
7804   (interactive)
7805   (gnus-summary-show-article t))
7806
7807 (defun gnus-summary-verbose-headers (&optional arg)
7808   "Toggle permanent full header display.
7809 If ARG is a positive number, turn header display on.
7810 If ARG is a negative number, turn header display off."
7811   (interactive "P")
7812   (setq gnus-show-all-headers
7813         (cond ((or (not (numberp arg))
7814                    (zerop arg))
7815                (not gnus-show-all-headers))
7816               ((natnump arg)
7817                t)))
7818   (gnus-summary-show-article))
7819
7820 (defun gnus-summary-toggle-header (&optional arg)
7821   "Show the headers if they are hidden, or hide them if they are shown.
7822 If ARG is a positive number, show the entire header.
7823 If ARG is a negative number, hide the unwanted header lines."
7824   (interactive "P")
7825   (save-excursion
7826     (set-buffer gnus-article-buffer)
7827     (save-restriction
7828       (let* ((buffer-read-only nil)
7829              (inhibit-point-motion-hooks t)
7830              hidden e)
7831         (setq hidden
7832               (if (numberp arg)
7833                   (>= arg 0)
7834                 (save-restriction
7835                   (article-narrow-to-head)
7836                   (gnus-article-hidden-text-p 'headers))))
7837         (goto-char (point-min))
7838         (when (search-forward "\n\n" nil t)
7839           (delete-region (point-min) (1- (point))))
7840         (goto-char (point-min))
7841         (save-excursion
7842           (set-buffer gnus-original-article-buffer)
7843           (goto-char (point-min))
7844           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7845         (insert-buffer-substring gnus-original-article-buffer 1 e)
7846         (save-restriction
7847           (narrow-to-region (point-min) (point))
7848           (article-decode-encoded-words)
7849           (if  hidden
7850               (let ((gnus-treat-hide-headers nil)
7851                     (gnus-treat-hide-boring-headers nil))
7852                 (setq gnus-article-wash-types
7853                       (delq 'headers gnus-article-wash-types))
7854                 (gnus-treat-article 'head))
7855             (gnus-treat-article 'head)))
7856         (gnus-set-mode-line 'article)))))
7857
7858 (defun gnus-summary-show-all-headers ()
7859   "Make all header lines visible."
7860   (interactive)
7861   (gnus-summary-toggle-header 1))
7862
7863 (defun gnus-summary-toggle-mime (&optional arg)
7864   "Toggle MIME processing.
7865 If ARG is a positive number, turn MIME processing on."
7866   (interactive "P")
7867   (setq gnus-show-mime
7868         (if (null arg)
7869             (not gnus-show-mime)
7870           (> (prefix-numeric-value arg) 0)))
7871   (gnus-summary-select-article t 'force))
7872
7873 (defun gnus-summary-caesar-message (&optional arg)
7874   "Caesar rotate the current article by 13.
7875 The numerical prefix specifies how many places to rotate each letter
7876 forward."
7877   (interactive "P")
7878   (gnus-summary-select-article)
7879   (let ((mail-header-separator ""))
7880     (gnus-eval-in-buffer-window gnus-article-buffer
7881       (save-restriction
7882         (widen)
7883         (let ((start (window-start))
7884               buffer-read-only)
7885           (message-caesar-buffer-body arg)
7886           (set-window-start (get-buffer-window (current-buffer)) start))))))
7887
7888 (defun gnus-summary-stop-page-breaking ()
7889   "Stop page breaking in the current article."
7890   (interactive)
7891   (gnus-summary-select-article)
7892   (gnus-eval-in-buffer-window gnus-article-buffer
7893     (widen)
7894     (when (gnus-visual-p 'page-marker)
7895       (let ((buffer-read-only nil))
7896         (gnus-remove-text-with-property 'gnus-prev)
7897         (gnus-remove-text-with-property 'gnus-next))
7898       (setq gnus-page-broken nil))))
7899
7900 (defun gnus-summary-move-article (&optional n to-newsgroup
7901                                             select-method action)
7902   "Move the current article to a different newsgroup.
7903 If N is a positive number, move the N next articles.
7904 If N is a negative number, move the N previous articles.
7905 If N is nil and any articles have been marked with the process mark,
7906 move those articles instead.
7907 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7908 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7909 re-spool using this method.
7910
7911 For this function to work, both the current newsgroup and the
7912 newsgroup that you want to move to have to support the `request-move'
7913 and `request-accept' functions.
7914
7915 ACTION can be either `move' (the default), `crosspost' or `copy'."
7916   (interactive "P")
7917   (unless action
7918     (setq action 'move))
7919   ;; Disable marking as read.
7920   (let (gnus-mark-article-hook)
7921     (save-window-excursion
7922       (gnus-summary-select-article)))
7923   ;; Check whether the source group supports the required functions.
7924   (cond ((and (eq action 'move)
7925               (not (gnus-check-backend-function
7926                     'request-move-article gnus-newsgroup-name)))
7927          (error "The current group does not support article moving"))
7928         ((and (eq action 'crosspost)
7929               (not (gnus-check-backend-function
7930                     'request-replace-article gnus-newsgroup-name)))
7931          (error "The current group does not support article editing")))
7932   (let ((articles (gnus-summary-work-articles n))
7933         (prefix (if (gnus-check-backend-function
7934                     'request-move-article gnus-newsgroup-name)
7935                     (gnus-group-real-prefix gnus-newsgroup-name)
7936                   ""))
7937         (names '((move "Move" "Moving")
7938                  (copy "Copy" "Copying")
7939                  (crosspost "Crosspost" "Crossposting")))
7940         (copy-buf (save-excursion
7941                     (nnheader-set-temp-buffer " *copy article*")))
7942         (default-marks gnus-article-mark-lists)
7943         (no-expire-marks (delete '(expirable . expire)
7944                                  (copy-sequence gnus-article-mark-lists)))
7945         art-group to-method new-xref article to-groups)
7946     (unless (assq action names)
7947       (error "Unknown action %s" action))
7948     ;; Read the newsgroup name.
7949     (when (and (not to-newsgroup)
7950                (not select-method))
7951       (setq to-newsgroup
7952             (gnus-read-move-group-name
7953              (cadr (assq action names))
7954              (symbol-value (intern (format "gnus-current-%s-group" action)))
7955              articles prefix))
7956       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7957     (setq to-method (or select-method
7958                         (gnus-server-to-method
7959                          (gnus-group-method to-newsgroup))))
7960     ;; Check the method we are to move this article to...
7961     (unless (gnus-check-backend-function
7962              'request-accept-article (car to-method))
7963       (error "%s does not support article copying" (car to-method)))
7964     (unless (gnus-check-server to-method)
7965       (error "Can't open server %s" (car to-method)))
7966     (gnus-message 6 "%s to %s: %s..."
7967                   (caddr (assq action names))
7968                   (or (car select-method) to-newsgroup) articles)
7969     (while articles
7970       (setq article (pop articles))
7971       (setq
7972        art-group
7973        (cond
7974         ;; Move the article.
7975         ((eq action 'move)
7976          ;; Remove this article from future suppression.
7977          (gnus-dup-unsuppress-article article)
7978          (gnus-request-move-article
7979           article                       ; Article to move
7980           gnus-newsgroup-name           ; From newsgroup
7981           (nth 1 (gnus-find-method-for-group
7982                   gnus-newsgroup-name)) ; Server
7983           (list 'gnus-request-accept-article
7984                 to-newsgroup (list 'quote select-method)
7985                 (not articles) t)       ; Accept form
7986           (not articles)))              ; Only save nov last time
7987         ;; Copy the article.
7988         ((eq action 'copy)
7989          (save-excursion
7990            (set-buffer copy-buf)
7991            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7992              (gnus-request-accept-article
7993               to-newsgroup select-method (not articles) t))))
7994         ;; Crosspost the article.
7995         ((eq action 'crosspost)
7996          (let ((xref (message-tokenize-header
7997                       (mail-header-xref (gnus-summary-article-header article))
7998                       " ")))
7999            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8000                                   ":" article))
8001            (unless xref
8002              (setq xref (list (system-name))))
8003            (setq new-xref
8004                  (concat
8005                   (mapconcat 'identity
8006                              (delete "Xref:" (delete new-xref xref))
8007                              " ")
8008                   " " new-xref))
8009            (save-excursion
8010              (set-buffer copy-buf)
8011              ;; First put the article in the destination group.
8012              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8013              (when (consp (setq art-group
8014                                 (gnus-request-accept-article
8015                                  to-newsgroup select-method (not articles))))
8016                (setq new-xref (concat new-xref " " (car art-group)
8017                                       ":" (cdr art-group)))
8018                ;; Now we have the new Xrefs header, so we insert
8019                ;; it and replace the new article.
8020                (nnheader-replace-header "Xref" new-xref)
8021                (gnus-request-replace-article
8022                 (cdr art-group) to-newsgroup (current-buffer))
8023                art-group))))))
8024       (cond
8025        ((not art-group)
8026         (gnus-message 1 "Couldn't %s article %s: %s"
8027                       (cadr (assq action names)) article
8028                       (nnheader-get-report (car to-method))))
8029        ((eq art-group 'junk)
8030         (when (eq action 'move)
8031           (gnus-summary-mark-article article gnus-canceled-mark)
8032           (gnus-message 4 "Deleted article %s" article)))
8033        (t
8034         (let* ((pto-group (gnus-group-prefixed-name
8035                            (car art-group) to-method))
8036                (entry
8037                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8038                (info (nth 2 entry))
8039                (to-group (gnus-info-group info))
8040                to-marks)
8041           ;; Update the group that has been moved to.
8042           (when (and info
8043                      (memq action '(move copy)))
8044             (unless (member to-group to-groups)
8045               (push to-group to-groups))
8046
8047             (unless (memq article gnus-newsgroup-unreads)
8048               (push 'read to-marks)
8049               (gnus-info-set-read
8050                info (gnus-add-to-range (gnus-info-read info)
8051                                        (list (cdr art-group)))))
8052
8053             ;; See whether the article is to be put in the cache.
8054             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8055                              default-marks
8056                            no-expire-marks))
8057                   (to-article (cdr art-group)))
8058
8059               ;; Enter the article into the cache in the new group,
8060               ;; if that is required.
8061               (when gnus-use-cache
8062                 (gnus-cache-possibly-enter-article
8063                  to-group to-article
8064                  (let ((header (copy-sequence
8065                                 (gnus-summary-article-header article))))
8066                    (mail-header-set-number header to-article)
8067                    header)
8068                  (memq article gnus-newsgroup-marked)
8069                  (memq article gnus-newsgroup-dormant)
8070                  (memq article gnus-newsgroup-unreads)))
8071
8072               (when gnus-preserve-marks
8073                 ;; Copy any marks over to the new group.
8074                 (when (and (equal to-group gnus-newsgroup-name)
8075                            (not (memq article gnus-newsgroup-unreads)))
8076                   ;; Mark this article as read in this group.
8077                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8078                   (setcdr (gnus-active to-group) to-article)
8079                   (setcdr gnus-newsgroup-active to-article))
8080
8081                 (while marks
8082                   (when (memq article (symbol-value
8083                                        (intern (format "gnus-newsgroup-%s"
8084                                                        (caar marks)))))
8085                     (push (cdar marks) to-marks)
8086                     ;; If the other group is the same as this group,
8087                     ;; then we have to add the mark to the list.
8088                     (when (equal to-group gnus-newsgroup-name)
8089                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8090                            (cons to-article
8091                                  (symbol-value
8092                                   (intern (format "gnus-newsgroup-%s"
8093                                                   (caar marks)))))))
8094                     ;; Copy the marks to other group.
8095                     (gnus-add-marked-articles
8096                      to-group (cdar marks) (list to-article) info))
8097                   (setq marks (cdr marks)))
8098
8099                 (gnus-request-set-mark to-group (list (list (list to-article)
8100                                                             'set
8101                                                             to-marks))))
8102
8103               (gnus-dribble-enter
8104                (concat "(gnus-group-set-info '"
8105                        (gnus-prin1-to-string (gnus-get-info to-group))
8106                        ")"))))
8107
8108           ;; Update the Xref header in this article to point to
8109           ;; the new crossposted article we have just created.
8110           (when (eq action 'crosspost)
8111             (save-excursion
8112               (set-buffer copy-buf)
8113               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8114               (nnheader-replace-header "Xref" new-xref)
8115               (gnus-request-replace-article
8116                article gnus-newsgroup-name (current-buffer)))))
8117
8118         ;;;!!!Why is this necessary?
8119         (set-buffer gnus-summary-buffer)
8120
8121         (gnus-summary-goto-subject article)
8122         (when (eq action 'move)
8123           (gnus-summary-mark-article article gnus-canceled-mark))))
8124       (gnus-summary-remove-process-mark article))
8125     ;; Re-activate all groups that have been moved to.
8126     (while to-groups
8127       (save-excursion
8128         (set-buffer gnus-group-buffer)
8129         (when (gnus-group-goto-group (car to-groups) t)
8130           (gnus-group-get-new-news-this-group 1 t))
8131         (pop to-groups)))
8132
8133     (gnus-kill-buffer copy-buf)
8134     (gnus-summary-position-point)
8135     (gnus-set-mode-line 'summary)))
8136
8137 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8138   "Move the current article to a different newsgroup.
8139 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8140 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8141 re-spool using this method."
8142   (interactive "P")
8143   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8144
8145 (defun gnus-summary-crosspost-article (&optional n)
8146   "Crosspost the current article to some other group."
8147   (interactive "P")
8148   (gnus-summary-move-article n nil nil 'crosspost))
8149
8150 (defcustom gnus-summary-respool-default-method nil
8151   "Default method for respooling an article.
8152 If nil, use to the current newsgroup method."
8153   :type '(choice (gnus-select-method :value (nnml ""))
8154                  (const nil))
8155   :group 'gnus-summary-mail)
8156
8157 (defun gnus-summary-respool-article (&optional n method)
8158   "Respool the current article.
8159 The article will be squeezed through the mail spooling process again,
8160 which means that it will be put in some mail newsgroup or other
8161 depending on `nnmail-split-methods'.
8162 If N is a positive number, respool the N next articles.
8163 If N is a negative number, respool the N previous articles.
8164 If N is nil and any articles have been marked with the process mark,
8165 respool those articles instead.
8166
8167 Respooling can be done both from mail groups and \"real\" newsgroups.
8168 In the former case, the articles in question will be moved from the
8169 current group into whatever groups they are destined to.  In the
8170 latter case, they will be copied into the relevant groups."
8171   (interactive
8172    (list current-prefix-arg
8173          (let* ((methods (gnus-methods-using 'respool))
8174                 (methname
8175                  (symbol-name (or gnus-summary-respool-default-method
8176                                   (car (gnus-find-method-for-group
8177                                         gnus-newsgroup-name)))))
8178                 (method
8179                  (gnus-completing-read
8180                   methname "What backend do you want to use when respooling?"
8181                   methods nil t nil 'gnus-mail-method-history))
8182                 ms)
8183            (cond
8184             ((zerop (length (setq ms (gnus-servers-using-backend
8185                                       (intern method)))))
8186              (list (intern method) ""))
8187             ((= 1 (length ms))
8188              (car ms))
8189             (t
8190              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8191                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8192                            ms-alist))))))))
8193   (unless method
8194     (error "No method given for respooling"))
8195   (if (assoc (symbol-name
8196               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8197              (gnus-methods-using 'respool))
8198       (gnus-summary-move-article n nil method)
8199     (gnus-summary-copy-article n nil method)))
8200
8201 (defun gnus-summary-import-article (file &optional edit)
8202   "Import an arbitrary file into a mail newsgroup."
8203   (interactive "fImport file: \nP")
8204   (let ((group gnus-newsgroup-name)
8205         (now (current-time))
8206         atts lines group-art)
8207     (unless (gnus-check-backend-function 'request-accept-article group)
8208       (error "%s does not support article importing" group))
8209     (or (file-readable-p file)
8210         (not (file-regular-p file))
8211         (error "Can't read %s" file))
8212     (save-excursion
8213       (set-buffer (gnus-get-buffer-create " *import file*"))
8214       (erase-buffer)
8215       (nnheader-insert-file-contents file)
8216       (goto-char (point-min))
8217       (unless (nnheader-article-p)
8218         ;; This doesn't look like an article, so we fudge some headers.
8219         (setq atts (file-attributes file)
8220               lines (count-lines (point-min) (point-max)))
8221         (insert "From: " (read-string "From: ") "\n"
8222                 "Subject: " (read-string "Subject: ") "\n"
8223                 "Date: " (message-make-date (nth 5 atts)) "\n"
8224                 "Message-ID: " (message-make-message-id) "\n"
8225                 "Lines: " (int-to-string lines) "\n"
8226                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8227       (setq group-art (gnus-request-accept-article group nil t))
8228       (kill-buffer (current-buffer)))
8229     (setq gnus-newsgroup-active (gnus-activate-group group))
8230     (forward-line 1)
8231     (gnus-summary-goto-article (cdr group-art) nil t)
8232     (when edit
8233       (gnus-summary-edit-article))))
8234
8235 (defun gnus-summary-create-article ()
8236   "Create an article in a mail newsgroup."
8237   (interactive)
8238   (let ((group gnus-newsgroup-name)
8239         (now (current-time))
8240         group-art)
8241     (unless (gnus-check-backend-function 'request-accept-article group)
8242       (error "%s does not support article importing" group))
8243     (save-excursion
8244       (set-buffer (gnus-get-buffer-create " *import file*"))
8245       (erase-buffer)
8246       (goto-char (point-min))
8247       ;; This doesn't look like an article, so we fudge some headers.
8248       (insert "From: " (read-string "From: ") "\n"
8249               "Subject: " (read-string "Subject: ") "\n"
8250               "Date: " (message-make-date now) "\n"
8251               "Message-ID: " (message-make-message-id) "\n")
8252       (setq group-art (gnus-request-accept-article group nil t))
8253       (kill-buffer (current-buffer)))
8254     (setq gnus-newsgroup-active (gnus-activate-group group))
8255     (forward-line 1)
8256     (gnus-summary-goto-article (cdr group-art) nil t)
8257     (gnus-summary-edit-article)))
8258
8259 (defun gnus-summary-article-posted-p ()
8260   "Say whether the current (mail) article is available from news as well.
8261 This will be the case if the article has both been mailed and posted."
8262   (interactive)
8263   (let ((id (mail-header-references (gnus-summary-article-header)))
8264         (gnus-override-method (car (gnus-refer-article-methods))))
8265     (if (gnus-request-head id "")
8266         (gnus-message 2 "The current message was found on %s"
8267                       gnus-override-method)
8268       (gnus-message 2 "The current message couldn't be found on %s"
8269                     gnus-override-method)
8270       nil)))
8271
8272 (defun gnus-summary-expire-articles (&optional now)
8273   "Expire all articles that are marked as expirable in the current group."
8274   (interactive)
8275   (when (gnus-check-backend-function
8276          'request-expire-articles gnus-newsgroup-name)
8277     ;; This backend supports expiry.
8278     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8279            (expirable (if total
8280                           (progn
8281                             ;; We need to update the info for
8282                             ;; this group for `gnus-list-of-read-articles'
8283                             ;; to give us the right answer.
8284                             (gnus-run-hooks 'gnus-exit-group-hook)
8285                             (gnus-summary-update-info)
8286                             (gnus-list-of-read-articles gnus-newsgroup-name))
8287                         (setq gnus-newsgroup-expirable
8288                               (sort gnus-newsgroup-expirable '<))))
8289            (expiry-wait (if now 'immediate
8290                           (gnus-group-find-parameter
8291                            gnus-newsgroup-name 'expiry-wait)))
8292            (nnmail-expiry-target
8293             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8294                 nnmail-expiry-target))
8295            es)
8296       (when expirable
8297         ;; There are expirable articles in this group, so we run them
8298         ;; through the expiry process.
8299         (gnus-message 6 "Expiring articles...")
8300         (unless (gnus-check-group gnus-newsgroup-name)
8301           (error "Can't open server for %s" gnus-newsgroup-name))
8302         ;; The list of articles that weren't expired is returned.
8303         (save-excursion
8304           (if expiry-wait
8305               (let ((nnmail-expiry-wait-function nil)
8306                     (nnmail-expiry-wait expiry-wait))
8307                 (setq es (gnus-request-expire-articles
8308                           expirable gnus-newsgroup-name)))
8309             (setq es (gnus-request-expire-articles
8310                       expirable gnus-newsgroup-name)))
8311           (unless total
8312             (setq gnus-newsgroup-expirable es))
8313           ;; We go through the old list of expirable, and mark all
8314           ;; really expired articles as nonexistent.
8315           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8316             (let ((gnus-use-cache nil))
8317               (while expirable
8318                 (unless (memq (car expirable) es)
8319                   (when (gnus-data-find (car expirable))
8320                     (gnus-summary-mark-article
8321                      (car expirable) gnus-canceled-mark)))
8322                 (setq expirable (cdr expirable))))))
8323         (gnus-message 6 "Expiring articles...done")))))
8324
8325 (defun gnus-summary-expire-articles-now ()
8326   "Expunge all expirable articles in the current group.
8327 This means that *all* articles that are marked as expirable will be
8328 deleted forever, right now."
8329   (interactive)
8330   (or gnus-expert-user
8331       (gnus-yes-or-no-p
8332        "Are you really, really, really sure you want to delete all these messages? ")
8333       (error "Phew!"))
8334   (gnus-summary-expire-articles t))
8335
8336 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8337 (defun gnus-summary-delete-article (&optional n)
8338   "Delete the N next (mail) articles.
8339 This command actually deletes articles.  This is not a marking
8340 command.  The article will disappear forever from your life, never to
8341 return.
8342 If N is negative, delete backwards.
8343 If N is nil and articles have been marked with the process mark,
8344 delete these instead."
8345   (interactive "P")
8346   (unless (gnus-check-backend-function 'request-expire-articles
8347                                        gnus-newsgroup-name)
8348     (error "The current newsgroup does not support article deletion"))
8349   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8350     (error "Couldn't open server"))
8351   ;; Compute the list of articles to delete.
8352   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8353         not-deleted)
8354     (if (and gnus-novice-user
8355              (not (gnus-yes-or-no-p
8356                    (format "Do you really want to delete %s forever? "
8357                            (if (> (length articles) 1)
8358                                (format "these %s articles" (length articles))
8359                              "this article")))))
8360         ()
8361       ;; Delete the articles.
8362       (setq not-deleted (gnus-request-expire-articles
8363                          articles gnus-newsgroup-name 'force))
8364       (while articles
8365         (gnus-summary-remove-process-mark (car articles))
8366         ;; The backend might not have been able to delete the article
8367         ;; after all.
8368         (unless (memq (car articles) not-deleted)
8369           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8370         (setq articles (cdr articles)))
8371       (when not-deleted
8372         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8373     (gnus-summary-position-point)
8374     (gnus-set-mode-line 'summary)
8375     not-deleted))
8376
8377 (defun gnus-summary-edit-article (&optional force)
8378   "Edit the current article.
8379 This will have permanent effect only in mail groups.
8380 If FORCE is non-nil, allow editing of articles even in read-only
8381 groups."
8382   (interactive "P")
8383   (save-excursion
8384     (set-buffer gnus-summary-buffer)
8385     (let ((mail-parse-charset gnus-newsgroup-charset)
8386           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8387       (gnus-set-global-variables)
8388       (when (and (not force)
8389                  (gnus-group-read-only-p))
8390         (error "The current newsgroup does not support article editing"))
8391       (gnus-summary-show-article t)
8392       (gnus-article-edit-article
8393        'ignore
8394        `(lambda (no-highlight)
8395           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8396                 (message-options message-options)
8397                 (message-options-set-recipient)
8398                 (mail-parse-ignored-charsets
8399                  ',gnus-newsgroup-ignored-charsets))
8400             (gnus-summary-edit-article-done
8401              ,(or (mail-header-references gnus-current-headers) "")
8402              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8403
8404 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8405
8406 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8407                                                  no-highlight)
8408   "Make edits to the current article permanent."
8409   (interactive)
8410   (save-excursion
8411     ;; The buffer restriction contains the entire article if it exists.
8412     (when (article-goto-body)
8413       (let ((lines (count-lines (point) (point-max)))
8414             (length (- (point-max) (point)))
8415             (case-fold-search t)
8416             (body (copy-marker (point))))
8417         (goto-char (point-min))
8418         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8419           (delete-region (match-beginning 1) (match-end 1))
8420           (insert (number-to-string length)))
8421         (goto-char (point-min))
8422         (when (re-search-forward
8423                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8424           (delete-region (match-beginning 1) (match-end 1))
8425           (insert (number-to-string length)))
8426         (goto-char (point-min))
8427         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8428           (delete-region (match-beginning 1) (match-end 1))
8429           (insert (number-to-string lines))))))
8430   ;; Replace the article.
8431   (let ((buf (current-buffer)))
8432     (with-temp-buffer
8433       (insert-buffer-substring buf)
8434
8435       (if (and (not read-only)
8436                (not (gnus-request-replace-article
8437                      (cdr gnus-article-current) (car gnus-article-current)
8438                      (current-buffer) t)))
8439           (error "Couldn't replace article")
8440         ;; Update the summary buffer.
8441         (if (and references
8442                  (equal (message-tokenize-header references " ")
8443                         (message-tokenize-header
8444                          (or (message-fetch-field "references") "") " ")))
8445             ;; We only have to update this line.
8446             (save-excursion
8447               (save-restriction
8448                 (message-narrow-to-head)
8449                 (let ((head (buffer-string))
8450                       header)
8451                   (with-temp-buffer
8452                     (insert (format "211 %d Article retrieved.\n"
8453                                     (cdr gnus-article-current)))
8454                     (insert head)
8455                     (insert ".\n")
8456                     (let ((nntp-server-buffer (current-buffer)))
8457                       (setq header (car (gnus-get-newsgroup-headers
8458                                          (save-excursion
8459                                            (set-buffer gnus-summary-buffer)
8460                                            gnus-newsgroup-dependencies)
8461                                          t))))
8462                     (save-excursion
8463                       (set-buffer gnus-summary-buffer)
8464                       (gnus-data-set-header
8465                        (gnus-data-find (cdr gnus-article-current))
8466                        header)
8467                       (gnus-summary-update-article-line
8468                        (cdr gnus-article-current) header))))))
8469           ;; Update threads.
8470           (set-buffer (or buffer gnus-summary-buffer))
8471           (gnus-summary-update-article (cdr gnus-article-current)))
8472         ;; Prettify the article buffer again.
8473         (unless no-highlight
8474           (save-excursion
8475             (set-buffer gnus-article-buffer)
8476             ;;;!!! Fix this -- article should be rehighlighted.
8477             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8478             (set-buffer gnus-original-article-buffer)
8479             (gnus-request-article
8480              (cdr gnus-article-current)
8481              (car gnus-article-current) (current-buffer))))
8482         ;; Prettify the summary buffer line.
8483         (when (gnus-visual-p 'summary-highlight 'highlight)
8484           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8485
8486 (defun gnus-summary-edit-wash (key)
8487   "Perform editing command KEY in the article buffer."
8488   (interactive
8489    (list
8490     (progn
8491       (message "%s" (concat (this-command-keys) "- "))
8492       (read-char))))
8493   (message "")
8494   (gnus-summary-edit-article)
8495   (execute-kbd-macro (concat (this-command-keys) key))
8496   (gnus-article-edit-done))
8497
8498 ;;; Respooling
8499
8500 (defun gnus-summary-respool-query (&optional silent trace)
8501   "Query where the respool algorithm would put this article."
8502   (interactive)
8503   (let (gnus-mark-article-hook)
8504     (gnus-summary-select-article)
8505     (save-excursion
8506       (set-buffer gnus-original-article-buffer)
8507       (save-restriction
8508         (message-narrow-to-head)
8509         (let ((groups (nnmail-article-group 'identity trace)))
8510           (unless silent
8511             (if groups
8512                 (message "This message would go to %s"
8513                          (mapconcat 'car groups ", "))
8514               (message "This message would go to no groups"))
8515             groups))))))
8516
8517 (defun gnus-summary-respool-trace ()
8518   "Trace where the respool algorithm would put this article.
8519 Display a buffer showing all fancy splitting patterns which matched."
8520   (interactive)
8521   (gnus-summary-respool-query nil t))
8522
8523 ;; Summary marking commands.
8524
8525 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8526   "Mark articles which has the same subject as read, and then select the next.
8527 If UNMARK is positive, remove any kind of mark.
8528 If UNMARK is negative, tick articles."
8529   (interactive "P")
8530   (when unmark
8531     (setq unmark (prefix-numeric-value unmark)))
8532   (let ((count
8533          (gnus-summary-mark-same-subject
8534           (gnus-summary-article-subject) unmark)))
8535     ;; Select next unread article.  If auto-select-same mode, should
8536     ;; select the first unread article.
8537     (gnus-summary-next-article t (and gnus-auto-select-same
8538                                       (gnus-summary-article-subject)))
8539     (gnus-message 7 "%d article%s marked as %s"
8540                   count (if (= count 1) " is" "s are")
8541                   (if unmark "unread" "read"))))
8542
8543 (defun gnus-summary-kill-same-subject (&optional unmark)
8544   "Mark articles which has the same subject as read.
8545 If UNMARK is positive, remove any kind of mark.
8546 If UNMARK is negative, tick articles."
8547   (interactive "P")
8548   (when unmark
8549     (setq unmark (prefix-numeric-value unmark)))
8550   (let ((count
8551          (gnus-summary-mark-same-subject
8552           (gnus-summary-article-subject) unmark)))
8553     ;; If marked as read, go to next unread subject.
8554     (when (null unmark)
8555       ;; Go to next unread subject.
8556       (gnus-summary-next-subject 1 t))
8557     (gnus-message 7 "%d articles are marked as %s"
8558                   count (if unmark "unread" "read"))))
8559
8560 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8561   "Mark articles with same SUBJECT as read, and return marked number.
8562 If optional argument UNMARK is positive, remove any kinds of marks.
8563 If optional argument UNMARK is negative, mark articles as unread instead."
8564   (let ((count 1))
8565     (save-excursion
8566       (cond
8567        ((null unmark)                   ; Mark as read.
8568         (while (and
8569                 (progn
8570                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8571                   (gnus-summary-show-thread) t)
8572                 (gnus-summary-find-subject subject))
8573           (setq count (1+ count))))
8574        ((> unmark 0)                    ; Tick.
8575         (while (and
8576                 (progn
8577                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8578                   (gnus-summary-show-thread) t)
8579                 (gnus-summary-find-subject subject))
8580           (setq count (1+ count))))
8581        (t                               ; Mark as unread.
8582         (while (and
8583                 (progn
8584                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8585                   (gnus-summary-show-thread) t)
8586                 (gnus-summary-find-subject subject))
8587           (setq count (1+ count)))))
8588       (gnus-set-mode-line 'summary)
8589       ;; Return the number of marked articles.
8590       count)))
8591
8592 (defun gnus-summary-mark-as-processable (n &optional unmark)
8593   "Set the process mark on the next N articles.
8594 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8595 the process mark instead.  The difference between N and the actual
8596 number of articles marked is returned."
8597   (interactive "P")
8598   (if (and (null n) (gnus-region-active-p))
8599       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8600     (setq n (prefix-numeric-value n))
8601     (let ((backward (< n 0))
8602           (n (abs n)))
8603       (while (and
8604               (> n 0)
8605               (if unmark
8606                 (gnus-summary-remove-process-mark
8607                  (gnus-summary-article-number))
8608                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8609               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8610         (setq n (1- n)))
8611       (when (/= 0 n)
8612         (gnus-message 7 "No more articles"))
8613       (gnus-summary-recenter)
8614       (gnus-summary-position-point)
8615       n)))
8616
8617 (defun gnus-summary-unmark-as-processable (n)
8618   "Remove the process mark from the next N articles.
8619 If N is negative, unmark backward instead.  The difference between N and
8620 the actual number of articles unmarked is returned."
8621   (interactive "P")
8622   (gnus-summary-mark-as-processable n t))
8623
8624 (defun gnus-summary-unmark-all-processable ()
8625   "Remove the process mark from all articles."
8626   (interactive)
8627   (save-excursion
8628     (while gnus-newsgroup-processable
8629       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8630   (gnus-summary-position-point))
8631
8632 (defun gnus-summary-add-mark (article type)
8633   "Mark ARTICLE with a mark of TYPE."
8634   (let ((vtype (car (assq type gnus-article-mark-lists)))
8635         var)
8636     (if (not vtype)
8637         (error "No such mark type: %s" type)
8638       (setq var (intern (format "gnus-newsgroup-%s" type)))
8639       (set var (cons article (symbol-value var)))
8640       (if (memq type '(processable cached replied forwarded saved))
8641           (gnus-summary-update-secondary-mark article)
8642         ;;; !!! This is bobus.  We should find out what primary
8643         ;;; !!! mark we want to set.
8644         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8645
8646 (defun gnus-summary-mark-as-expirable (n)
8647   "Mark N articles forward as expirable.
8648 If N is negative, mark backward instead.  The difference between N and
8649 the actual number of articles marked is returned."
8650   (interactive "p")
8651   (gnus-summary-mark-forward n gnus-expirable-mark))
8652
8653 (defun gnus-summary-mark-article-as-replied (article)
8654   "Mark ARTICLE as replied to and update the summary line.
8655 ARTICLE can also be a list of articles."
8656   (interactive (list (gnus-summary-article-number)))
8657   (let ((articles (if (listp article) article (list article))))
8658     (dolist (article articles)
8659       (push article gnus-newsgroup-replied)
8660       (let ((buffer-read-only nil))
8661         (when (gnus-summary-goto-subject article nil t)
8662           (gnus-summary-update-secondary-mark article))))))
8663
8664 (defun gnus-summary-mark-article-as-forwarded (article)
8665   "Mark ARTICLE as forwarded and update the summary line.
8666 ARTICLE can also be a list of articles."
8667   (let ((articles (if (listp article) article (list article))))
8668     (dolist (article articles)
8669       (push article gnus-newsgroup-forwarded)
8670       (let ((buffer-read-only nil))
8671         (when (gnus-summary-goto-subject article nil t)
8672           (gnus-summary-update-secondary-mark article))))))
8673
8674 (defun gnus-summary-set-bookmark (article)
8675   "Set a bookmark in current article."
8676   (interactive (list (gnus-summary-article-number)))
8677   (when (or (not (get-buffer gnus-article-buffer))
8678             (not gnus-current-article)
8679             (not gnus-article-current)
8680             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8681     (error "No current article selected"))
8682   ;; Remove old bookmark, if one exists.
8683   (let ((old (assq article gnus-newsgroup-bookmarks)))
8684     (when old
8685       (setq gnus-newsgroup-bookmarks
8686             (delq old gnus-newsgroup-bookmarks))))
8687   ;; Set the new bookmark, which is on the form
8688   ;; (article-number . line-number-in-body).
8689   (push
8690    (cons article
8691          (save-excursion
8692            (set-buffer gnus-article-buffer)
8693            (count-lines
8694             (min (point)
8695                  (save-excursion
8696                    (goto-char (point-min))
8697                    (search-forward "\n\n" nil t)
8698                    (point)))
8699             (point))))
8700    gnus-newsgroup-bookmarks)
8701   (gnus-message 6 "A bookmark has been added to the current article."))
8702
8703 (defun gnus-summary-remove-bookmark (article)
8704   "Remove the bookmark from the current article."
8705   (interactive (list (gnus-summary-article-number)))
8706   ;; Remove old bookmark, if one exists.
8707   (let ((old (assq article gnus-newsgroup-bookmarks)))
8708     (if old
8709         (progn
8710           (setq gnus-newsgroup-bookmarks
8711                 (delq old gnus-newsgroup-bookmarks))
8712           (gnus-message 6 "Removed bookmark."))
8713       (gnus-message 6 "No bookmark in current article."))))
8714
8715 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8716 (defun gnus-summary-mark-as-dormant (n)
8717   "Mark N articles forward as dormant.
8718 If N is negative, mark backward instead.  The difference between N and
8719 the actual number of articles marked is returned."
8720   (interactive "p")
8721   (gnus-summary-mark-forward n gnus-dormant-mark))
8722
8723 (defun gnus-summary-set-process-mark (article)
8724   "Set the process mark on ARTICLE and update the summary line."
8725   (setq gnus-newsgroup-processable
8726         (cons article
8727               (delq article gnus-newsgroup-processable)))
8728   (when (gnus-summary-goto-subject article)
8729     (gnus-summary-show-thread)
8730     (gnus-summary-goto-subject article)
8731     (gnus-summary-update-secondary-mark article)))
8732
8733 (defun gnus-summary-remove-process-mark (article)
8734   "Remove the process mark from ARTICLE and update the summary line."
8735   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8736   (when (gnus-summary-goto-subject article)
8737     (gnus-summary-show-thread)
8738     (gnus-summary-goto-subject article)
8739     (gnus-summary-update-secondary-mark article)))
8740
8741 (defun gnus-summary-set-saved-mark (article)
8742   "Set the process mark on ARTICLE and update the summary line."
8743   (push article gnus-newsgroup-saved)
8744   (when (gnus-summary-goto-subject article)
8745     (gnus-summary-update-secondary-mark article)))
8746
8747 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8748   "Mark N articles as read forwards.
8749 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8750 The difference between N and the actual number of articles marked is
8751 returned.
8752 Iff NO-EXPIRE, auto-expiry will be inhibited."
8753   (interactive "p")
8754   (gnus-summary-show-thread)
8755   (let ((backward (< n 0))
8756         (gnus-summary-goto-unread
8757          (and gnus-summary-goto-unread
8758               (not (eq gnus-summary-goto-unread 'never))
8759               (not (memq mark (list gnus-unread-mark
8760                                     gnus-ticked-mark gnus-dormant-mark)))))
8761         (n (abs n))
8762         (mark (or mark gnus-del-mark)))
8763     (while (and (> n 0)
8764                 (gnus-summary-mark-article nil mark no-expire)
8765                 (zerop (gnus-summary-next-subject
8766                         (if backward -1 1)
8767                         (and gnus-summary-goto-unread
8768                              (not (eq gnus-summary-goto-unread 'never)))
8769                         t)))
8770       (setq n (1- n)))
8771     (when (/= 0 n)
8772       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8773     (gnus-summary-recenter)
8774     (gnus-summary-position-point)
8775     (gnus-set-mode-line 'summary)
8776     n))
8777
8778 (defun gnus-summary-mark-article-as-read (mark)
8779   "Mark the current article quickly as read with MARK."
8780   (let ((article (gnus-summary-article-number)))
8781     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8782     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8783     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8784     (push (cons article mark) gnus-newsgroup-reads)
8785     ;; Possibly remove from cache, if that is used.
8786     (when gnus-use-cache
8787       (gnus-cache-enter-remove-article article))
8788     ;; Allow the backend to change the mark.
8789     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8790     ;; Check for auto-expiry.
8791     (when (and gnus-newsgroup-auto-expire
8792                (memq mark gnus-auto-expirable-marks))
8793       (setq mark gnus-expirable-mark)
8794       ;; Let the backend know about the mark change.
8795       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8796       (push article gnus-newsgroup-expirable))
8797     ;; Set the mark in the buffer.
8798     (gnus-summary-update-mark mark 'unread)
8799     t))
8800
8801 (defun gnus-summary-mark-article-as-unread (mark)
8802   "Mark the current article quickly as unread with MARK."
8803   (let* ((article (gnus-summary-article-number))
8804          (old-mark (gnus-summary-article-mark article)))
8805     ;; Allow the backend to change the mark.
8806     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8807     (if (eq mark old-mark)
8808         t
8809       (if (<= article 0)
8810           (progn
8811             (gnus-error 1 "Can't mark negative article numbers")
8812             nil)
8813         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8814         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8815         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8816         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8817         (cond ((= mark gnus-ticked-mark)
8818                (push article gnus-newsgroup-marked))
8819               ((= mark gnus-dormant-mark)
8820                (push article gnus-newsgroup-dormant))
8821               (t
8822                (push article gnus-newsgroup-unreads)))
8823         (gnus-pull article gnus-newsgroup-reads)
8824
8825         ;; See whether the article is to be put in the cache.
8826         (and gnus-use-cache
8827              (vectorp (gnus-summary-article-header article))
8828              (save-excursion
8829                (gnus-cache-possibly-enter-article
8830                 gnus-newsgroup-name article
8831                 (gnus-summary-article-header article)
8832                 (= mark gnus-ticked-mark)
8833                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8834
8835         ;; Fix the mark.
8836         (gnus-summary-update-mark mark 'unread)
8837         t))))
8838
8839 (defun gnus-summary-mark-article (&optional article mark no-expire)
8840   "Mark ARTICLE with MARK.  MARK can be any character.
8841 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8842 `??' (dormant) and `?E' (expirable).
8843 If MARK is nil, then the default character `?r' is used.
8844 If ARTICLE is nil, then the article on the current line will be
8845 marked.
8846 Iff NO-EXPIRE, auto-expiry will be inhibited."
8847   ;; The mark might be a string.
8848   (when (stringp mark)
8849     (setq mark (aref mark 0)))
8850   ;; If no mark is given, then we check auto-expiring.
8851   (when (null mark)
8852     (setq mark gnus-del-mark))
8853   (when (and (not no-expire)
8854              gnus-newsgroup-auto-expire
8855              (memq mark gnus-auto-expirable-marks))
8856     (setq mark gnus-expirable-mark))
8857   (let ((article (or article (gnus-summary-article-number)))
8858         (old-mark (gnus-summary-article-mark article)))
8859     ;; Allow the backend to change the mark.
8860     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8861     (if (eq mark old-mark)
8862         t
8863       (unless article
8864         (error "No article on current line"))
8865       (if (not (if (or (= mark gnus-unread-mark)
8866                        (= mark gnus-ticked-mark)
8867                        (= mark gnus-dormant-mark))
8868                    (gnus-mark-article-as-unread article mark)
8869                  (gnus-mark-article-as-read article mark)))
8870           t
8871         ;; See whether the article is to be put in the cache.
8872         (and gnus-use-cache
8873              (not (= mark gnus-canceled-mark))
8874              (vectorp (gnus-summary-article-header article))
8875              (save-excursion
8876                (gnus-cache-possibly-enter-article
8877                 gnus-newsgroup-name article
8878                 (gnus-summary-article-header article)
8879                 (= mark gnus-ticked-mark)
8880                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8881
8882         (when (gnus-summary-goto-subject article nil t)
8883           (let ((buffer-read-only nil))
8884             (gnus-summary-show-thread)
8885             ;; Fix the mark.
8886             (gnus-summary-update-mark mark 'unread)
8887             t))))))
8888
8889 (defun gnus-summary-update-secondary-mark (article)
8890   "Update the secondary (read, process, cache) mark."
8891   (gnus-summary-update-mark
8892    (cond ((memq article gnus-newsgroup-processable)
8893           gnus-process-mark)
8894          ((memq article gnus-newsgroup-cached)
8895           gnus-cached-mark)
8896          ((memq article gnus-newsgroup-replied)
8897           gnus-replied-mark)
8898          ((memq article gnus-newsgroup-forwarded)
8899           gnus-forwarded-mark)
8900          ((memq article gnus-newsgroup-saved)
8901           gnus-saved-mark)
8902          (t gnus-no-mark))
8903    'replied)
8904   (when (gnus-visual-p 'summary-highlight 'highlight)
8905     (gnus-run-hooks 'gnus-summary-update-hook))
8906   t)
8907
8908 (defun gnus-summary-update-mark (mark type)
8909   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8910         (buffer-read-only nil))
8911     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8912     (when forward
8913       (when (looking-at "\r")
8914         (incf forward))
8915       (when (<= (+ forward (point)) (point-max))
8916         ;; Go to the right position on the line.
8917         (goto-char (+ forward (point)))
8918         ;; Replace the old mark with the new mark.
8919         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8920         ;; Optionally update the marks by some user rule.
8921         (when (eq type 'unread)
8922           (gnus-data-set-mark
8923            (gnus-data-find (gnus-summary-article-number)) mark)
8924           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8925
8926 (defun gnus-mark-article-as-read (article &optional mark)
8927   "Enter ARTICLE in the pertinent lists and remove it from others."
8928   ;; Make the article expirable.
8929   (let ((mark (or mark gnus-del-mark)))
8930     (if (= mark gnus-expirable-mark)
8931         (push article gnus-newsgroup-expirable)
8932       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8933     ;; Remove from unread and marked lists.
8934     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8935     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8936     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8937     (push (cons article mark) gnus-newsgroup-reads)
8938     ;; Possibly remove from cache, if that is used.
8939     (when gnus-use-cache
8940       (gnus-cache-enter-remove-article article))
8941     t))
8942
8943 (defun gnus-mark-article-as-unread (article &optional mark)
8944   "Enter ARTICLE in the pertinent lists and remove it from others."
8945   (let ((mark (or mark gnus-ticked-mark)))
8946     (if (<= article 0)
8947         (progn
8948           (gnus-error 1 "Can't mark negative article numbers")
8949           nil)
8950       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8951             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8952             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8953             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8954
8955       ;; Unsuppress duplicates?
8956       (when gnus-suppress-duplicates
8957         (gnus-dup-unsuppress-article article))
8958
8959       (cond ((= mark gnus-ticked-mark)
8960              (push article gnus-newsgroup-marked))
8961             ((= mark gnus-dormant-mark)
8962              (push article gnus-newsgroup-dormant))
8963             (t
8964              (push article gnus-newsgroup-unreads)))
8965       (gnus-pull article gnus-newsgroup-reads)
8966       t)))
8967
8968 (defalias 'gnus-summary-mark-as-unread-forward
8969   'gnus-summary-tick-article-forward)
8970 (make-obsolete 'gnus-summary-mark-as-unread-forward
8971                'gnus-summary-tick-article-forward)
8972 (defun gnus-summary-tick-article-forward (n)
8973   "Tick N articles forwards.
8974 If N is negative, tick backwards instead.
8975 The difference between N and the number of articles ticked is returned."
8976   (interactive "p")
8977   (gnus-summary-mark-forward n gnus-ticked-mark))
8978
8979 (defalias 'gnus-summary-mark-as-unread-backward
8980   'gnus-summary-tick-article-backward)
8981 (make-obsolete 'gnus-summary-mark-as-unread-backward
8982                'gnus-summary-tick-article-backward)
8983 (defun gnus-summary-tick-article-backward (n)
8984   "Tick N articles backwards.
8985 The difference between N and the number of articles ticked is returned."
8986   (interactive "p")
8987   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8988
8989 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8990 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8991 (defun gnus-summary-tick-article (&optional article clear-mark)
8992   "Mark current article as unread.
8993 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8994 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8995   (interactive)
8996   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8997                                        gnus-ticked-mark)))
8998
8999 (defun gnus-summary-mark-as-read-forward (n)
9000   "Mark N articles as read forwards.
9001 If N is negative, mark backwards instead.
9002 The difference between N and the actual number of articles marked is
9003 returned."
9004   (interactive "p")
9005   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9006
9007 (defun gnus-summary-mark-as-read-backward (n)
9008   "Mark the N articles as read backwards.
9009 The difference between N and the actual number of articles marked is
9010 returned."
9011   (interactive "p")
9012   (gnus-summary-mark-forward
9013    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9014
9015 (defun gnus-summary-mark-as-read (&optional article mark)
9016   "Mark current article as read.
9017 ARTICLE specifies the article to be marked as read.
9018 MARK specifies a string to be inserted at the beginning of the line."
9019   (gnus-summary-mark-article article mark))
9020
9021 (defun gnus-summary-clear-mark-forward (n)
9022   "Clear marks from N articles forward.
9023 If N is negative, clear backward instead.
9024 The difference between N and the number of marks cleared is returned."
9025   (interactive "p")
9026   (gnus-summary-mark-forward n gnus-unread-mark))
9027
9028 (defun gnus-summary-clear-mark-backward (n)
9029   "Clear marks from N articles backward.
9030 The difference between N and the number of marks cleared is returned."
9031   (interactive "p")
9032   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9033
9034 (defun gnus-summary-mark-unread-as-read ()
9035   "Intended to be used by `gnus-summary-mark-article-hook'."
9036   (when (memq gnus-current-article gnus-newsgroup-unreads)
9037     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9038
9039 (defun gnus-summary-mark-read-and-unread-as-read ()
9040   "Intended to be used by `gnus-summary-mark-article-hook'."
9041   (let ((mark (gnus-summary-article-mark)))
9042     (when (or (gnus-unread-mark-p mark)
9043               (gnus-read-mark-p mark))
9044       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9045
9046 (defun gnus-summary-mark-unread-as-ticked ()
9047    "Intended to be used by `gnus-summary-mark-article-hook'."
9048   (when (memq gnus-current-article gnus-newsgroup-unreads)
9049     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9050
9051 (defun gnus-summary-mark-region-as-read (point mark all)
9052   "Mark all unread articles between point and mark as read.
9053 If given a prefix, mark all articles between point and mark as read,
9054 even ticked and dormant ones."
9055   (interactive "r\nP")
9056   (save-excursion
9057     (let (article)
9058       (goto-char point)
9059       (beginning-of-line)
9060       (while (and
9061               (< (point) mark)
9062               (progn
9063                 (when (or all
9064                           (memq (setq article (gnus-summary-article-number))
9065                                 gnus-newsgroup-unreads))
9066                   (gnus-summary-mark-article article gnus-del-mark))
9067                 t)
9068               (gnus-summary-find-next))))))
9069
9070 (defun gnus-summary-mark-below (score mark)
9071   "Mark articles with score less than SCORE with MARK."
9072   (interactive "P\ncMark: ")
9073   (setq score (if score
9074                   (prefix-numeric-value score)
9075                 (or gnus-summary-default-score 0)))
9076   (save-excursion
9077     (set-buffer gnus-summary-buffer)
9078     (goto-char (point-min))
9079     (while
9080         (progn
9081           (and (< (gnus-summary-article-score) score)
9082                (gnus-summary-mark-article nil mark))
9083           (gnus-summary-find-next)))))
9084
9085 (defun gnus-summary-kill-below (&optional score)
9086   "Mark articles with score below SCORE as read."
9087   (interactive "P")
9088   (gnus-summary-mark-below score gnus-killed-mark))
9089
9090 (defun gnus-summary-clear-above (&optional score)
9091   "Clear all marks from articles with score above SCORE."
9092   (interactive "P")
9093   (gnus-summary-mark-above score gnus-unread-mark))
9094
9095 (defun gnus-summary-tick-above (&optional score)
9096   "Tick all articles with score above SCORE."
9097   (interactive "P")
9098   (gnus-summary-mark-above score gnus-ticked-mark))
9099
9100 (defun gnus-summary-mark-above (score mark)
9101   "Mark articles with score over SCORE with MARK."
9102   (interactive "P\ncMark: ")
9103   (setq score (if score
9104                   (prefix-numeric-value score)
9105                 (or gnus-summary-default-score 0)))
9106   (save-excursion
9107     (set-buffer gnus-summary-buffer)
9108     (goto-char (point-min))
9109     (while (and (progn
9110                   (when (> (gnus-summary-article-score) score)
9111                     (gnus-summary-mark-article nil mark))
9112                   t)
9113                 (gnus-summary-find-next)))))
9114
9115 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9116 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9117 (defun gnus-summary-limit-include-expunged (&optional no-error)
9118   "Display all the hidden articles that were expunged for low scores."
9119   (interactive)
9120   (let ((buffer-read-only nil))
9121     (let ((scored gnus-newsgroup-scored)
9122           headers h)
9123       (while scored
9124         (unless (gnus-summary-article-header (caar scored))
9125           (and (setq h (gnus-number-to-header (caar scored)))
9126                (< (cdar scored) gnus-summary-expunge-below)
9127                (push h headers)))
9128         (setq scored (cdr scored)))
9129       (if (not headers)
9130           (when (not no-error)
9131             (error "No expunged articles hidden"))
9132         (goto-char (point-min))
9133         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9134         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9135         (mapcar (lambda (x) (push (mail-header-number x) 
9136                                   gnus-newsgroup-limit))
9137                 headers)
9138         (gnus-summary-prepare-unthreaded (nreverse headers))
9139         (goto-char (point-min))
9140         (gnus-summary-position-point)
9141         t))))
9142
9143 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9144   "Mark all unread articles in this newsgroup as read.
9145 If prefix argument ALL is non-nil, ticked and dormant articles will
9146 also be marked as read.
9147 If QUIETLY is non-nil, no questions will be asked.
9148 If TO-HERE is non-nil, it should be a point in the buffer.  All
9149 articles before (after, if REVERSE is set) this point will be marked as read.
9150 Note that this function will only catch up the unread article
9151 in the current summary buffer limitation.
9152 The number of articles marked as read is returned."
9153   (interactive "P")
9154   (prog1
9155       (save-excursion
9156         (when (or quietly
9157                   (not gnus-interactive-catchup) ;Without confirmation?
9158                   gnus-expert-user
9159                   (gnus-y-or-n-p
9160                    (if all
9161                        "Mark absolutely all articles as read? "
9162                      "Mark all unread articles as read? ")))
9163           (if (and not-mark
9164                    (not gnus-newsgroup-adaptive)
9165                    (not gnus-newsgroup-auto-expire)
9166                    (not gnus-suppress-duplicates)
9167                    (or (not gnus-use-cache)
9168                        (eq gnus-use-cache 'passive)))
9169               (progn
9170                 (when all
9171                   (setq gnus-newsgroup-marked nil
9172                         gnus-newsgroup-dormant nil))
9173                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9174             ;; We actually mark all articles as canceled, which we
9175             ;; have to do when using auto-expiry or adaptive scoring.
9176             (gnus-summary-show-all-threads)
9177             (if (and to-here reverse)
9178                 (progn
9179                   (goto-char to-here)
9180                   (while (and
9181                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9182                           (gnus-summary-find-next (not all) nil nil t))))
9183               (when (gnus-summary-first-subject (not all) t)
9184                 (while (and
9185                         (if to-here (< (point) to-here) t)
9186                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9187                         (gnus-summary-find-next (not all) nil nil t)))))
9188             (gnus-set-mode-line 'summary))
9189           t))
9190     (gnus-summary-position-point)))
9191
9192 (defun gnus-summary-catchup-to-here (&optional all)
9193   "Mark all unticked articles before the current one as read.
9194 If ALL is non-nil, also mark ticked and dormant articles as read."
9195   (interactive "P")
9196   (save-excursion
9197     (gnus-save-hidden-threads
9198       (let ((beg (point)))
9199         ;; We check that there are unread articles.
9200         (when (or all (gnus-summary-find-prev))
9201           (gnus-summary-catchup all t beg)))))
9202   (gnus-summary-position-point))
9203
9204 (defun gnus-summary-catchup-from-here (&optional all)
9205   "Mark all unticked articles after the current one as read.
9206 If ALL is non-nil, also mark ticked and dormant articles as read."
9207   (interactive "P")
9208   (save-excursion
9209     (gnus-save-hidden-threads
9210       (let ((beg (point)))
9211         ;; We check that there are unread articles.
9212         (when (or all (gnus-summary-find-next))
9213           (gnus-summary-catchup all t beg nil t)))))
9214   (gnus-summary-position-point))
9215
9216 (defun gnus-summary-catchup-all (&optional quietly)
9217   "Mark all articles in this newsgroup as read."
9218   (interactive "P")
9219   (gnus-summary-catchup t quietly))
9220
9221 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9222   "Mark all unread articles in this group as read, then exit.
9223 If prefix argument ALL is non-nil, all articles are marked as read.
9224 If QUIETLY is non-nil, no questions will be asked."
9225   (interactive "P")
9226   (when (gnus-summary-catchup all quietly nil 'fast)
9227     ;; Select next newsgroup or exit.
9228     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9229              (eq gnus-auto-select-next 'quietly))
9230         (gnus-summary-next-group nil)
9231       (gnus-summary-exit))))
9232
9233 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9234   "Mark all articles in this newsgroup as read, and then exit."
9235   (interactive "P")
9236   (gnus-summary-catchup-and-exit t quietly))
9237
9238 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9239   "Mark all articles in this group as read and select the next group.
9240 If given a prefix, mark all articles, unread as well as ticked, as
9241 read."
9242   (interactive "P")
9243   (save-excursion
9244     (gnus-summary-catchup all))
9245   (gnus-summary-next-group))
9246
9247 ;;;
9248 ;;; with article
9249 ;;;
9250
9251 (defmacro gnus-with-article (article &rest forms)
9252   "Select ARTICLE and perform FORMS in the original article buffer.
9253 Then replace the article with the result."
9254   `(progn
9255      ;; We don't want the article to be marked as read.
9256      (let (gnus-mark-article-hook)
9257        (gnus-summary-select-article t t nil ,article))
9258      (set-buffer gnus-original-article-buffer)
9259      ,@forms
9260      (if (not (gnus-check-backend-function
9261                'request-replace-article (car gnus-article-current)))
9262          (gnus-message 5 "Read-only group; not replacing")
9263        (unless (gnus-request-replace-article
9264                 ,article (car gnus-article-current)
9265                 (current-buffer) t)
9266          (error "Couldn't replace article")))
9267      ;; The cache and backlog have to be flushed somewhat.
9268      (when gnus-keep-backlog
9269        (gnus-backlog-remove-article
9270         (car gnus-article-current) (cdr gnus-article-current)))
9271      (when gnus-use-cache
9272        (gnus-cache-update-article
9273         (car gnus-article-current) (cdr gnus-article-current)))))
9274
9275 (put 'gnus-with-article 'lisp-indent-function 1)
9276 (put 'gnus-with-article 'edebug-form-spec '(form body))
9277
9278 ;; Thread-based commands.
9279
9280 (defun gnus-summary-articles-in-thread (&optional article)
9281   "Return a list of all articles in the current thread.
9282 If ARTICLE is non-nil, return all articles in the thread that starts
9283 with that article."
9284   (let* ((article (or article (gnus-summary-article-number)))
9285          (data (gnus-data-find-list article))
9286          (top-level (gnus-data-level (car data)))
9287          (top-subject
9288           (cond ((null gnus-thread-operation-ignore-subject)
9289                  (gnus-simplify-subject-re
9290                   (mail-header-subject (gnus-data-header (car data)))))
9291                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9292                  (gnus-simplify-subject-fuzzy
9293                   (mail-header-subject (gnus-data-header (car data)))))
9294                 (t nil)))
9295          (end-point (save-excursion
9296                       (if (gnus-summary-go-to-next-thread)
9297                           (point) (point-max))))
9298          articles)
9299     (while (and data
9300                 (< (gnus-data-pos (car data)) end-point))
9301       (when (or (not top-subject)
9302                 (string= top-subject
9303                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9304                              (gnus-simplify-subject-fuzzy
9305                               (mail-header-subject
9306                                (gnus-data-header (car data))))
9307                            (gnus-simplify-subject-re
9308                             (mail-header-subject
9309                              (gnus-data-header (car data)))))))
9310         (push (gnus-data-number (car data)) articles))
9311       (unless (and (setq data (cdr data))
9312                    (> (gnus-data-level (car data)) top-level))
9313         (setq data nil)))
9314     ;; Return the list of articles.
9315     (nreverse articles)))
9316
9317 (defun gnus-summary-rethread-current ()
9318   "Rethread the thread the current article is part of."
9319   (interactive)
9320   (let* ((gnus-show-threads t)
9321          (article (gnus-summary-article-number))
9322          (id (mail-header-id (gnus-summary-article-header)))
9323          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9324     (unless id
9325       (error "No article on the current line"))
9326     (gnus-rebuild-thread id)
9327     (gnus-summary-goto-subject article)))
9328
9329 (defun gnus-summary-reparent-thread ()
9330   "Make the current article child of the marked (or previous) article.
9331
9332 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9333 is non-nil or the Subject: of both articles are the same."
9334   (interactive)
9335   (unless (not (gnus-group-read-only-p))
9336     (error "The current newsgroup does not support article editing"))
9337   (unless (<= (length gnus-newsgroup-processable) 1)
9338     (error "No more than one article may be marked"))
9339   (save-window-excursion
9340     (let ((gnus-article-buffer " *reparent*")
9341           (current-article (gnus-summary-article-number))
9342           ;; First grab the marked article, otherwise one line up.
9343           (parent-article (if (not (null gnus-newsgroup-processable))
9344                               (car gnus-newsgroup-processable)
9345                             (save-excursion
9346                               (if (eq (forward-line -1) 0)
9347                                   (gnus-summary-article-number)
9348                                 (error "Beginning of summary buffer"))))))
9349       (unless (not (eq current-article parent-article))
9350         (error "An article may not be self-referential"))
9351       (let ((message-id (mail-header-id
9352                          (gnus-summary-article-header parent-article))))
9353         (unless (and message-id (not (equal message-id "")))
9354           (error "No message-id in desired parent"))
9355         (gnus-with-article current-article
9356           (save-restriction
9357             (goto-char (point-min))
9358             (message-narrow-to-head)
9359             (if (re-search-forward "^References: " nil t)
9360                 (progn
9361                   (re-search-forward "^[^ \t]" nil t)
9362                   (forward-line -1)
9363                   (end-of-line)
9364                   (insert " " message-id))
9365               (insert "References: " message-id "\n"))))
9366         (set-buffer gnus-summary-buffer)
9367         (gnus-summary-unmark-all-processable)
9368         (gnus-summary-update-article current-article)
9369         (gnus-summary-rethread-current)
9370         (gnus-message 3 "Article %d is now the child of article %d"
9371                       current-article parent-article)))))
9372
9373 (defun gnus-summary-toggle-threads (&optional arg)
9374   "Toggle showing conversation threads.
9375 If ARG is positive number, turn showing conversation threads on."
9376   (interactive "P")
9377   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9378     (setq gnus-show-threads
9379           (if (null arg) (not gnus-show-threads)
9380             (> (prefix-numeric-value arg) 0)))
9381     (gnus-summary-prepare)
9382     (gnus-summary-goto-subject current)
9383     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9384     (gnus-summary-position-point)))
9385
9386 (defun gnus-summary-show-all-threads ()
9387   "Show all threads."
9388   (interactive)
9389   (save-excursion
9390     (let ((buffer-read-only nil))
9391       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9392   (gnus-summary-position-point))
9393
9394 (defun gnus-summary-show-thread ()
9395   "Show thread subtrees.
9396 Returns nil if no thread was there to be shown."
9397   (interactive)
9398   (let ((buffer-read-only nil)
9399         (orig (point))
9400         ;; first goto end then to beg, to have point at beg after let
9401         (end (progn (end-of-line) (point)))
9402         (beg (progn (beginning-of-line) (point))))
9403     (prog1
9404         ;; Any hidden lines here?
9405         (search-forward "\r" end t)
9406       (subst-char-in-region beg end ?\^M ?\n t)
9407       (goto-char orig)
9408       (gnus-summary-position-point))))
9409
9410 (defun gnus-summary-hide-all-threads ()
9411   "Hide all thread subtrees."
9412   (interactive)
9413   (save-excursion
9414     (goto-char (point-min))
9415     (gnus-summary-hide-thread)
9416     (while (zerop (gnus-summary-next-thread 1 t))
9417       (gnus-summary-hide-thread)))
9418   (gnus-summary-position-point))
9419
9420 (defun gnus-summary-hide-thread ()
9421   "Hide thread subtrees.
9422 Returns nil if no threads were there to be hidden."
9423   (interactive)
9424   (let ((buffer-read-only nil)
9425         (start (point))
9426         (article (gnus-summary-article-number)))
9427     (goto-char start)
9428     ;; Go forward until either the buffer ends or the subthread
9429     ;; ends.
9430     (when (and (not (eobp))
9431                (or (zerop (gnus-summary-next-thread 1 t))
9432                    (goto-char (point-max))))
9433       (prog1
9434           (if (and (> (point) start)
9435                    (search-backward "\n" start t))
9436               (progn
9437                 (subst-char-in-region start (point) ?\n ?\^M)
9438                 (gnus-summary-goto-subject article))
9439             (goto-char start)
9440             nil)))))
9441
9442 (defun gnus-summary-go-to-next-thread (&optional previous)
9443   "Go to the same level (or less) next thread.
9444 If PREVIOUS is non-nil, go to previous thread instead.
9445 Return the article number moved to, or nil if moving was impossible."
9446   (let ((level (gnus-summary-thread-level))
9447         (way (if previous -1 1))
9448         (beg (point)))
9449     (forward-line way)
9450     (while (and (not (eobp))
9451                 (< level (gnus-summary-thread-level)))
9452       (forward-line way))
9453     (if (eobp)
9454         (progn
9455           (goto-char beg)
9456           nil)
9457       (setq beg (point))
9458       (prog1
9459           (gnus-summary-article-number)
9460         (goto-char beg)))))
9461
9462 (defun gnus-summary-next-thread (n &optional silent)
9463   "Go to the same level next N'th thread.
9464 If N is negative, search backward instead.
9465 Returns the difference between N and the number of skips actually
9466 done.
9467
9468 If SILENT, don't output messages."
9469   (interactive "p")
9470   (let ((backward (< n 0))
9471         (n (abs n)))
9472     (while (and (> n 0)
9473                 (gnus-summary-go-to-next-thread backward))
9474       (decf n))
9475     (unless silent
9476       (gnus-summary-position-point))
9477     (when (and (not silent) (/= 0 n))
9478       (gnus-message 7 "No more threads"))
9479     n))
9480
9481 (defun gnus-summary-prev-thread (n)
9482   "Go to the same level previous N'th thread.
9483 Returns the difference between N and the number of skips actually
9484 done."
9485   (interactive "p")
9486   (gnus-summary-next-thread (- n)))
9487
9488 (defun gnus-summary-go-down-thread ()
9489   "Go down one level in the current thread."
9490   (let ((children (gnus-summary-article-children)))
9491     (when children
9492       (gnus-summary-goto-subject (car children)))))
9493
9494 (defun gnus-summary-go-up-thread ()
9495   "Go up one level in the current thread."
9496   (let ((parent (gnus-summary-article-parent)))
9497     (when parent
9498       (gnus-summary-goto-subject parent))))
9499
9500 (defun gnus-summary-down-thread (n)
9501   "Go down thread N steps.
9502 If N is negative, go up instead.
9503 Returns the difference between N and how many steps down that were
9504 taken."
9505   (interactive "p")
9506   (let ((up (< n 0))
9507         (n (abs n)))
9508     (while (and (> n 0)
9509                 (if up (gnus-summary-go-up-thread)
9510                   (gnus-summary-go-down-thread)))
9511       (setq n (1- n)))
9512     (gnus-summary-position-point)
9513     (when (/= 0 n)
9514       (gnus-message 7 "Can't go further"))
9515     n))
9516
9517 (defun gnus-summary-up-thread (n)
9518   "Go up thread N steps.
9519 If N is negative, go down instead.
9520 Returns the difference between N and how many steps down that were
9521 taken."
9522   (interactive "p")
9523   (gnus-summary-down-thread (- n)))
9524
9525 (defun gnus-summary-top-thread ()
9526   "Go to the top of the thread."
9527   (interactive)
9528   (while (gnus-summary-go-up-thread))
9529   (gnus-summary-article-number))
9530
9531 (defun gnus-summary-kill-thread (&optional unmark)
9532   "Mark articles under current thread as read.
9533 If the prefix argument is positive, remove any kinds of marks.
9534 If the prefix argument is negative, tick articles instead."
9535   (interactive "P")
9536   (when unmark
9537     (setq unmark (prefix-numeric-value unmark)))
9538   (let ((articles (gnus-summary-articles-in-thread)))
9539     (save-excursion
9540       ;; Expand the thread.
9541       (gnus-summary-show-thread)
9542       ;; Mark all the articles.
9543       (while articles
9544         (gnus-summary-goto-subject (car articles))
9545         (cond ((null unmark)
9546                (gnus-summary-mark-article-as-read gnus-killed-mark))
9547               ((> unmark 0)
9548                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9549               (t
9550                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9551         (setq articles (cdr articles))))
9552     ;; Hide killed subtrees.
9553     (and (null unmark)
9554          gnus-thread-hide-killed
9555          (gnus-summary-hide-thread))
9556     ;; If marked as read, go to next unread subject.
9557     (when (null unmark)
9558       ;; Go to next unread subject.
9559       (gnus-summary-next-subject 1 t)))
9560   (gnus-set-mode-line 'summary))
9561
9562 ;; Summary sorting commands
9563
9564 (defun gnus-summary-sort-by-number (&optional reverse)
9565   "Sort the summary buffer by article number.
9566 Argument REVERSE means reverse order."
9567   (interactive "P")
9568   (gnus-summary-sort 'number reverse))
9569
9570 (defun gnus-summary-sort-by-author (&optional reverse)
9571   "Sort the summary buffer by author name alphabetically.
9572 If `case-fold-search' is non-nil, case of letters is ignored.
9573 Argument REVERSE means reverse order."
9574   (interactive "P")
9575   (gnus-summary-sort 'author reverse))
9576
9577 (defun gnus-summary-sort-by-subject (&optional reverse)
9578   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9579 If `case-fold-search' is non-nil, case of letters is ignored.
9580 Argument REVERSE means reverse order."
9581   (interactive "P")
9582   (gnus-summary-sort 'subject reverse))
9583
9584 (defun gnus-summary-sort-by-date (&optional reverse)
9585   "Sort the summary buffer by date.
9586 Argument REVERSE means reverse order."
9587   (interactive "P")
9588   (gnus-summary-sort 'date reverse))
9589
9590 (defun gnus-summary-sort-by-score (&optional reverse)
9591   "Sort the summary buffer by score.
9592 Argument REVERSE means reverse order."
9593   (interactive "P")
9594   (gnus-summary-sort 'score reverse))
9595
9596 (defun gnus-summary-sort-by-lines (&optional reverse)
9597   "Sort the summary buffer by the number of lines.
9598 Argument REVERSE means reverse order."
9599   (interactive "P")
9600   (gnus-summary-sort 'lines reverse))
9601
9602 (defun gnus-summary-sort-by-chars (&optional reverse)
9603   "Sort the summary buffer by article length.
9604 Argument REVERSE means reverse order."
9605   (interactive "P")
9606   (gnus-summary-sort 'chars reverse))
9607
9608 (defun gnus-summary-sort-by-original (&optional reverse)
9609   "Sort the summary buffer using the default sorting method.
9610 Argument REVERSE means reverse order."
9611   (interactive "P")
9612   (let* ((buffer-read-only)
9613          (gnus-summary-prepare-hook nil))
9614     ;; We do the sorting by regenerating the threads.
9615     (gnus-summary-prepare)
9616     ;; Hide subthreads if needed.
9617     (when (and gnus-show-threads gnus-thread-hide-subtree)
9618       (gnus-summary-hide-all-threads))))
9619
9620 (defun gnus-summary-sort (predicate reverse)
9621   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9622   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9623          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9624          (gnus-thread-sort-functions
9625           (if (not reverse)
9626               thread
9627             `(lambda (t1 t2)
9628                (,thread t2 t1))))
9629          (gnus-sort-gathered-threads-function
9630           gnus-thread-sort-functions)
9631          (gnus-article-sort-functions
9632           (if (not reverse)
9633               article
9634             `(lambda (t1 t2)
9635                (,article t2 t1))))
9636          (buffer-read-only)
9637          (gnus-summary-prepare-hook nil))
9638     ;; We do the sorting by regenerating the threads.
9639     (gnus-summary-prepare)
9640     ;; Hide subthreads if needed.
9641     (when (and gnus-show-threads gnus-thread-hide-subtree)
9642       (gnus-summary-hide-all-threads))))
9643
9644 ;; Summary saving commands.
9645
9646 (defun gnus-summary-save-article (&optional n not-saved)
9647   "Save the current article using the default saver function.
9648 If N is a positive number, save the N next articles.
9649 If N is a negative number, save the N previous articles.
9650 If N is nil and any articles have been marked with the process mark,
9651 save those articles instead.
9652 The variable `gnus-default-article-saver' specifies the saver function."
9653   (interactive "P")
9654   (let* ((articles (gnus-summary-work-articles n))
9655          (save-buffer (save-excursion
9656                         (nnheader-set-temp-buffer " *Gnus Save*")))
9657          (num (length articles))
9658          header file)
9659     (dolist (article articles)
9660       (setq header (gnus-summary-article-header article))
9661       (if (not (vectorp header))
9662           ;; This is a pseudo-article.
9663           (if (assq 'name header)
9664               (gnus-copy-file (cdr (assq 'name header)))
9665             (gnus-message 1 "Article %d is unsaveable" article))
9666         ;; This is a real article.
9667         (save-window-excursion
9668           (gnus-summary-select-article t nil nil article))
9669         (save-excursion
9670           (set-buffer save-buffer)
9671           (erase-buffer)
9672           (insert-buffer-substring gnus-original-article-buffer))
9673         (setq file (gnus-article-save save-buffer file num))
9674         (gnus-summary-remove-process-mark article)
9675         (unless not-saved
9676           (gnus-summary-set-saved-mark article))))
9677     (gnus-kill-buffer save-buffer)
9678     (gnus-summary-position-point)
9679     (gnus-set-mode-line 'summary)
9680     n))
9681
9682 (defun gnus-summary-pipe-output (&optional arg)
9683   "Pipe the current article to a subprocess.
9684 If N is a positive number, pipe the N next articles.
9685 If N is a negative number, pipe the N previous articles.
9686 If N is nil and any articles have been marked with the process mark,
9687 pipe those articles instead."
9688   (interactive "P")
9689   (require 'gnus-art)
9690   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9691     (gnus-summary-save-article arg t))
9692   (let ((buffer (get-buffer "*Shell Command Output*")))
9693     (if (and buffer 
9694              (with-current-buffer buffer (> (point-max) (point-min))))
9695         (gnus-configure-windows 'pipe))))
9696
9697 (defun gnus-summary-save-article-mail (&optional arg)
9698   "Append the current article to an mail file.
9699 If N is a positive number, save the N next articles.
9700 If N is a negative number, save the N previous articles.
9701 If N is nil and any articles have been marked with the process mark,
9702 save those articles instead."
9703   (interactive "P")
9704   (require 'gnus-art)
9705   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9706     (gnus-summary-save-article arg)))
9707
9708 (defun gnus-summary-save-article-rmail (&optional arg)
9709   "Append the current article to an rmail file.
9710 If N is a positive number, save the N next articles.
9711 If N is a negative number, save the N previous articles.
9712 If N is nil and any articles have been marked with the process mark,
9713 save those articles instead."
9714   (interactive "P")
9715   (require 'gnus-art)
9716   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9717     (gnus-summary-save-article arg)))
9718
9719 (defun gnus-summary-save-article-file (&optional arg)
9720   "Append the current article to a file.
9721 If N is a positive number, save the N next articles.
9722 If N is a negative number, save the N previous articles.
9723 If N is nil and any articles have been marked with the process mark,
9724 save those articles instead."
9725   (interactive "P")
9726   (require 'gnus-art)
9727   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9728     (gnus-summary-save-article arg)))
9729
9730 (defun gnus-summary-write-article-file (&optional arg)
9731   "Write the current article to a file, deleting the previous file.
9732 If N is a positive number, save the N next articles.
9733 If N is a negative number, save the N previous articles.
9734 If N is nil and any articles have been marked with the process mark,
9735 save those articles instead."
9736   (interactive "P")
9737   (require 'gnus-art)
9738   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9739     (gnus-summary-save-article arg)))
9740
9741 (defun gnus-summary-save-article-body-file (&optional arg)
9742   "Append the current article body to a file.
9743 If N is a positive number, save the N next articles.
9744 If N is a negative number, save the N previous articles.
9745 If N is nil and any articles have been marked with the process mark,
9746 save those articles instead."
9747   (interactive "P")
9748   (require 'gnus-art)
9749   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9750     (gnus-summary-save-article arg)))
9751
9752 (defun gnus-summary-pipe-message (program)
9753   "Pipe the current article through PROGRAM."
9754   (interactive "sProgram: ")
9755   (gnus-summary-select-article)
9756   (let ((mail-header-separator ""))
9757     (gnus-eval-in-buffer-window gnus-article-buffer
9758       (save-restriction
9759         (widen)
9760         (let ((start (window-start))
9761               buffer-read-only)
9762           (message-pipe-buffer-body program)
9763           (set-window-start (get-buffer-window (current-buffer)) start))))))
9764
9765 (defun gnus-get-split-value (methods)
9766   "Return a value based on the split METHODS."
9767   (let (split-name method result match)
9768     (when methods
9769       (save-excursion
9770         (set-buffer gnus-original-article-buffer)
9771         (save-restriction
9772           (nnheader-narrow-to-headers)
9773           (while (and methods (not split-name))
9774             (goto-char (point-min))
9775             (setq method (pop methods))
9776             (setq match (car method))
9777             (when (cond
9778                    ((stringp match)
9779                     ;; Regular expression.
9780                     (ignore-errors
9781                       (re-search-forward match nil t)))
9782                    ((gnus-functionp match)
9783                     ;; Function.
9784                     (save-restriction
9785                       (widen)
9786                       (setq result (funcall match gnus-newsgroup-name))))
9787                    ((consp match)
9788                     ;; Form.
9789                     (save-restriction
9790                       (widen)
9791                       (setq result (eval match)))))
9792               (setq split-name (cdr method))
9793               (cond ((stringp result)
9794                      (push (expand-file-name
9795                             result gnus-article-save-directory)
9796                            split-name))
9797                     ((consp result)
9798                      (setq split-name (append result split-name)))))))))
9799     (nreverse split-name)))
9800
9801 (defun gnus-valid-move-group-p (group)
9802   (and (boundp group)
9803        (symbol-name group)
9804        (symbol-value group)
9805        (gnus-get-function (gnus-find-method-for-group
9806                            (symbol-name group)) 'request-accept-article t)))
9807
9808 (defun gnus-read-move-group-name (prompt default articles prefix)
9809   "Read a group name."
9810   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9811          (minibuffer-confirm-incomplete nil) ; XEmacs
9812          (prom
9813           (format "%s %s to:"
9814                   prompt
9815                   (if (> (length articles) 1)
9816                       (format "these %d articles" (length articles))
9817                     "this article")))
9818          (to-newsgroup
9819           (cond
9820            ((null split-name)
9821             (gnus-completing-read default prom
9822                                   gnus-active-hashtb
9823                                   'gnus-valid-move-group-p
9824                                   nil prefix
9825                                   'gnus-group-history))
9826            ((= 1 (length split-name))
9827             (gnus-completing-read (car split-name) prom
9828                                   gnus-active-hashtb
9829                                   'gnus-valid-move-group-p
9830                                   nil nil
9831                                   'gnus-group-history))
9832            (t
9833             (gnus-completing-read nil prom
9834                                   (mapcar (lambda (el) (list el))
9835                                           (nreverse split-name))
9836                                   nil nil nil
9837                                   'gnus-group-history))))
9838          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9839     (when to-newsgroup
9840       (if (or (string= to-newsgroup "")
9841               (string= to-newsgroup prefix))
9842           (setq to-newsgroup default))
9843       (unless to-newsgroup
9844         (error "No group name entered"))
9845       (or (gnus-active to-newsgroup)
9846           (gnus-activate-group to-newsgroup nil nil to-method)
9847           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9848                                      to-newsgroup))
9849               (or (and (gnus-request-create-group to-newsgroup to-method)
9850                        (gnus-activate-group
9851                         to-newsgroup nil nil to-method)
9852                        (gnus-subscribe-group to-newsgroup))
9853                   (error "Couldn't create group %s" to-newsgroup)))
9854           (error "No such group: %s" to-newsgroup)))
9855     to-newsgroup))
9856
9857 (defun gnus-summary-save-parts (type dir n &optional reverse)
9858   "Save parts matching TYPE to DIR.
9859 If REVERSE, save parts that do not match TYPE."
9860   (interactive
9861    (list (read-string "Save parts of type: "
9862                       (or (car gnus-summary-save-parts-type-history)
9863                           gnus-summary-save-parts-default-mime)
9864                       'gnus-summary-save-parts-type-history)
9865          (setq gnus-summary-save-parts-last-directory
9866                (read-file-name "Save to directory: "
9867                                gnus-summary-save-parts-last-directory
9868                                nil t))
9869          current-prefix-arg))
9870   (gnus-summary-iterate n
9871     (let ((gnus-display-mime-function nil)
9872           (gnus-inhibit-treatment t))
9873       (gnus-summary-select-article))
9874     (save-excursion
9875       (set-buffer gnus-article-buffer)
9876       (let ((handles (or gnus-article-mime-handles
9877                          (mm-dissect-buffer) (mm-uu-dissect))))
9878         (when handles
9879           (gnus-summary-save-parts-1 type dir handles reverse)
9880           (unless gnus-article-mime-handles ;; Don't destroy this case.
9881             (mm-destroy-parts handles)))))))
9882
9883 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9884   (if (stringp (car handle))
9885       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9886               (cdr handle))
9887     (when (if reverse
9888               (not (string-match type (mm-handle-media-type handle)))
9889             (string-match type (mm-handle-media-type handle)))
9890       (let ((file (expand-file-name
9891                    (file-name-nondirectory
9892                     (or
9893                      (mail-content-type-get
9894                       (mm-handle-disposition handle) 'filename)
9895                      (concat gnus-newsgroup-name
9896                              "." (number-to-string
9897                                   (cdr gnus-article-current)))))
9898                    dir)))
9899         (unless (file-exists-p file)
9900           (mm-save-part-to-file handle file))))))
9901
9902 ;; Summary extract commands
9903
9904 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9905   (let ((buffer-read-only nil)
9906         (article (gnus-summary-article-number))
9907         after-article b e)
9908     (unless (gnus-summary-goto-subject article)
9909       (error "No such article: %d" article))
9910     (gnus-summary-position-point)
9911     ;; If all commands are to be bunched up on one line, we collect
9912     ;; them here.
9913     (unless gnus-view-pseudos-separately
9914       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9915             files action)
9916         (while ps
9917           (setq action (cdr (assq 'action (car ps))))
9918           (setq files (list (cdr (assq 'name (car ps)))))
9919           (while (and ps (cdr ps)
9920                       (string= (or action "1")
9921                                (or (cdr (assq 'action (cadr ps))) "2")))
9922             (push (cdr (assq 'name (cadr ps))) files)
9923             (setcdr ps (cddr ps)))
9924           (when files
9925             (when (not (string-match "%s" action))
9926               (push " " files))
9927             (push " " files)
9928             (when (assq 'execute (car ps))
9929               (setcdr (assq 'execute (car ps))
9930                       (funcall (if (string-match "%s" action)
9931                                    'format 'concat)
9932                                action
9933                                (mapconcat
9934                                 (lambda (f)
9935                                   (if (equal f " ")
9936                                       f
9937                                     (gnus-quote-arg-for-sh-or-csh f)))
9938                                 files " ")))))
9939           (setq ps (cdr ps)))))
9940     (if (and gnus-view-pseudos (not not-view))
9941         (while pslist
9942           (when (assq 'execute (car pslist))
9943             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9944                                   (eq gnus-view-pseudos 'not-confirm)))
9945           (setq pslist (cdr pslist)))
9946       (save-excursion
9947         (while pslist
9948           (setq after-article (or (cdr (assq 'article (car pslist)))
9949                                   (gnus-summary-article-number)))
9950           (gnus-summary-goto-subject after-article)
9951           (forward-line 1)
9952           (setq b (point))
9953           (insert "    " (file-name-nondirectory
9954                           (cdr (assq 'name (car pslist))))
9955                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9956           (setq e (point))
9957           (forward-line -1)             ; back to `b'
9958           (gnus-add-text-properties
9959            b (1- e) (list 'gnus-number gnus-reffed-article-number
9960                           gnus-mouse-face-prop gnus-mouse-face))
9961           (gnus-data-enter
9962            after-article gnus-reffed-article-number
9963            gnus-unread-mark b (car pslist) 0 (- e b))
9964           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9965           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9966           (setq pslist (cdr pslist)))))))
9967
9968 (defun gnus-pseudos< (p1 p2)
9969   (let ((c1 (cdr (assq 'action p1)))
9970         (c2 (cdr (assq 'action p2))))
9971     (and c1 c2 (string< c1 c2))))
9972
9973 (defun gnus-request-pseudo-article (props)
9974   (cond ((assq 'execute props)
9975          (gnus-execute-command (cdr (assq 'execute props)))))
9976   (let ((gnus-current-article (gnus-summary-article-number)))
9977     (gnus-run-hooks 'gnus-mark-article-hook)))
9978
9979 (defun gnus-execute-command (command &optional automatic)
9980   (save-excursion
9981     (gnus-article-setup-buffer)
9982     (set-buffer gnus-article-buffer)
9983     (setq buffer-read-only nil)
9984     (let ((command (if automatic command
9985                      (read-string "Command: " (cons command 0)))))
9986       (erase-buffer)
9987       (insert "$ " command "\n\n")
9988       (if gnus-view-pseudo-asynchronously
9989           (start-process "gnus-execute" (current-buffer) shell-file-name
9990                          shell-command-switch command)
9991         (call-process shell-file-name nil t nil
9992                       shell-command-switch command)))))
9993
9994 ;; Summary kill commands.
9995
9996 (defun gnus-summary-edit-global-kill (article)
9997   "Edit the \"global\" kill file."
9998   (interactive (list (gnus-summary-article-number)))
9999   (gnus-group-edit-global-kill article))
10000
10001 (defun gnus-summary-edit-local-kill ()
10002   "Edit a local kill file applied to the current newsgroup."
10003   (interactive)
10004   (setq gnus-current-headers (gnus-summary-article-header))
10005   (gnus-group-edit-local-kill
10006    (gnus-summary-article-number) gnus-newsgroup-name))
10007
10008 ;;; Header reading.
10009
10010 (defun gnus-read-header (id &optional header)
10011   "Read the headers of article ID and enter them into the Gnus system."
10012   (let ((group gnus-newsgroup-name)
10013         (gnus-override-method
10014          (or
10015           gnus-override-method
10016           (and (gnus-news-group-p gnus-newsgroup-name)
10017                (car (gnus-refer-article-methods)))))
10018         where)
10019     ;; First we check to see whether the header in question is already
10020     ;; fetched.
10021     (if (stringp id)
10022         ;; This is a Message-ID.
10023         (setq header (or header (gnus-id-to-header id)))
10024       ;; This is an article number.
10025       (setq header (or header (gnus-summary-article-header id))))
10026     (if (and header
10027              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10028         ;; We have found the header.
10029         header
10030       ;; If this is a sparse article, we have to nix out its
10031       ;; previous entry in the thread hashtb.
10032       (when (and header
10033                  (gnus-summary-article-sparse-p (mail-header-number header)))
10034         (let* ((parent (gnus-parent-id (mail-header-references header)))
10035                (thread (and parent (gnus-id-to-thread parent))))
10036           (when thread
10037             (delq (assq header thread) thread))))
10038       ;; We have to really fetch the header to this article.
10039       (save-excursion
10040         (set-buffer nntp-server-buffer)
10041         (when (setq where (gnus-request-head id group))
10042           (nnheader-fold-continuation-lines)
10043           (goto-char (point-max))
10044           (insert ".\n")
10045           (goto-char (point-min))
10046           (insert "211 ")
10047           (princ (cond
10048                   ((numberp id) id)
10049                   ((cdr where) (cdr where))
10050                   (header (mail-header-number header))
10051                   (t gnus-reffed-article-number))
10052                  (current-buffer))
10053           (insert " Article retrieved.\n"))
10054         (if (or (not where)
10055                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10056             ()                          ; Malformed head.
10057           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10058             (when (and (stringp id)
10059                        (not (string= (gnus-group-real-name group)
10060                                      (car where))))
10061               ;; If we fetched by Message-ID and the article came
10062               ;; from a different group, we fudge some bogus article
10063               ;; numbers for this article.
10064               (mail-header-set-number header gnus-reffed-article-number))
10065             (save-excursion
10066               (set-buffer gnus-summary-buffer)
10067               (decf gnus-reffed-article-number)
10068               (gnus-remove-header (mail-header-number header))
10069               (push header gnus-newsgroup-headers)
10070               (setq gnus-current-headers header)
10071               (push (mail-header-number header) gnus-newsgroup-limit)))
10072           header)))))
10073
10074 (defun gnus-remove-header (number)
10075   "Remove header NUMBER from `gnus-newsgroup-headers'."
10076   (if (and gnus-newsgroup-headers
10077            (= number (mail-header-number (car gnus-newsgroup-headers))))
10078       (pop gnus-newsgroup-headers)
10079     (let ((headers gnus-newsgroup-headers))
10080       (while (and (cdr headers)
10081                   (not (= number (mail-header-number (cadr headers)))))
10082         (pop headers))
10083       (when (cdr headers)
10084         (setcdr headers (cddr headers))))))
10085
10086 ;;;
10087 ;;; summary highlights
10088 ;;;
10089
10090 (defun gnus-highlight-selected-summary ()
10091   "Highlight selected article in summary buffer."
10092   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10093   (when gnus-summary-selected-face
10094     (save-excursion
10095       (let* ((beg (progn (beginning-of-line) (point)))
10096              (end (progn (end-of-line) (point)))
10097              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10098              (from (if (get-text-property beg gnus-mouse-face-prop)
10099                        beg
10100                      (or (next-single-property-change
10101                           beg gnus-mouse-face-prop nil end)
10102                          beg)))
10103              (to
10104               (if (= from end)
10105                   (- from 2)
10106                 (or (next-single-property-change
10107                      from gnus-mouse-face-prop nil end)
10108                     end))))
10109         ;; If no mouse-face prop on line we will have to = from = end,
10110         ;; so we highlight the entire line instead.
10111         (when (= (+ to 2) from)
10112           (setq from beg)
10113           (setq to end))
10114         (if gnus-newsgroup-selected-overlay
10115             ;; Move old overlay.
10116             (gnus-move-overlay
10117              gnus-newsgroup-selected-overlay from to (current-buffer))
10118           ;; Create new overlay.
10119           (gnus-overlay-put
10120            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10121            'face gnus-summary-selected-face))))))
10122
10123 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10124 (defun gnus-summary-highlight-line ()
10125   "Highlight current line according to `gnus-summary-highlight'."
10126   (let* ((list gnus-summary-highlight)
10127          (p (point))
10128          (end (progn (end-of-line) (point)))
10129          ;; now find out where the line starts and leave point there.
10130          (beg (progn (beginning-of-line) (point)))
10131          (article (gnus-summary-article-number))
10132          (score (or (cdr (assq (or article gnus-current-article)
10133                                gnus-newsgroup-scored))
10134                     gnus-summary-default-score 0))
10135          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10136          (inhibit-read-only t))
10137     ;; Eval the cars of the lists until we find a match.
10138     (let ((default gnus-summary-default-score)
10139           (default-high gnus-summary-default-high-score)
10140           (default-low gnus-summary-default-low-score))
10141       (while (and list
10142                   (not (eval (caar list))))
10143         (setq list (cdr list))))
10144     (let ((face (cdar list)))
10145       (unless (eq face (get-text-property beg 'face))
10146         (gnus-put-text-property-excluding-characters-with-faces
10147          beg end 'face
10148          (setq face (if (boundp face) (symbol-value face) face)))
10149         (when gnus-summary-highlight-line-function
10150           (funcall gnus-summary-highlight-line-function article face))))
10151     (goto-char p)))
10152
10153 (defun gnus-update-read-articles (group unread &optional compute)
10154   "Update the list of read articles in GROUP."
10155   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10156          (entry (gnus-gethash group gnus-newsrc-hashtb))
10157          (info (nth 2 entry))
10158          (prev 1)
10159          (unread (sort (copy-sequence unread) '<))
10160          read)
10161     (if (or (not info) (not active))
10162         ;; There is no info on this group if it was, in fact,
10163         ;; killed.  Gnus stores no information on killed groups, so
10164         ;; there's nothing to be done.
10165         ;; One could store the information somewhere temporarily,
10166         ;; perhaps...  Hmmm...
10167         ()
10168       ;; Remove any negative articles numbers.
10169       (while (and unread (< (car unread) 0))
10170         (setq unread (cdr unread)))
10171       ;; Remove any expired article numbers
10172       (while (and unread (< (car unread) (car active)))
10173         (setq unread (cdr unread)))
10174       ;; Compute the ranges of read articles by looking at the list of
10175       ;; unread articles.
10176       (while unread
10177         (when (/= (car unread) prev)
10178           (push (if (= prev (1- (car unread))) prev
10179                   (cons prev (1- (car unread))))
10180                 read))
10181         (setq prev (1+ (car unread)))
10182         (setq unread (cdr unread)))
10183       (when (<= prev (cdr active))
10184         (push (cons prev (cdr active)) read))
10185       (setq read (if (> (length read) 1) (nreverse read) read))
10186       (if compute
10187           read
10188         (save-excursion
10189           (let (setmarkundo)
10190             ;; Propagate the read marks to the backend.
10191             (when (gnus-check-backend-function 'request-set-mark group)
10192               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10193                     (add (gnus-remove-from-range read (gnus-info-read info))))
10194                 (when (or add del)
10195                   (unless (gnus-check-group group)
10196                     (error "Can't open server for %s" group))
10197                   (gnus-request-set-mark
10198                    group (delq nil (list (if add (list add 'add '(read)))
10199                                          (if del (list del 'del '(read))))))
10200                   (setq setmarkundo
10201                         `(gnus-request-set-mark
10202                           ,group
10203                           ',(delq nil (list
10204                                        (if del (list del 'add '(read)))
10205                                        (if add (list add 'del '(read))))))))))
10206             (set-buffer gnus-group-buffer)
10207             (gnus-undo-register
10208               `(progn
10209                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10210                  (gnus-info-set-read ',info ',(gnus-info-read info))
10211                  (gnus-get-unread-articles-in-group ',info
10212                                                     (gnus-active ,group))
10213                  (gnus-group-update-group ,group t)
10214                  ,setmarkundo))))
10215         ;; Enter this list into the group info.
10216         (gnus-info-set-read info read)
10217         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10218         (gnus-get-unread-articles-in-group info (gnus-active group))
10219         t))))
10220
10221 (defun gnus-offer-save-summaries ()
10222   "Offer to save all active summary buffers."
10223   (let (buffers)
10224     ;; Go through all buffers and find all summaries.
10225     (dolist (buffer (buffer-list))
10226       (when (and (setq buffer (buffer-name buffer))
10227                  (string-match "Summary" buffer)
10228                  (save-excursion
10229                    (set-buffer buffer)
10230                    ;; We check that this is, indeed, a summary buffer.
10231                    (and (eq major-mode 'gnus-summary-mode)
10232                         ;; Also make sure this isn't bogus.
10233                         gnus-newsgroup-prepared
10234                         ;; Also make sure that this isn't a
10235                         ;; dead summary buffer.
10236                         (not gnus-dead-summary-mode))))
10237         (push buffer buffers)))
10238     ;; Go through all these summary buffers and offer to save them.
10239     (when buffers
10240       (save-excursion
10241         (map-y-or-n-p
10242          "Update summary buffer %s? "
10243          (lambda (buf)
10244            (switch-to-buffer buf)
10245            (gnus-summary-exit))
10246          buffers)))))
10247
10248
10249 ;;; @ for mime-partial
10250 ;;;
10251
10252 (defun gnus-request-partial-message ()
10253   (save-excursion
10254     (let ((number (gnus-summary-article-number))
10255           (group gnus-newsgroup-name)
10256           (mother gnus-article-buffer))
10257       (set-buffer (get-buffer-create " *Partial Article*"))
10258       (erase-buffer)
10259       (setq mime-preview-buffer mother)
10260       (gnus-request-article-this-buffer number group)
10261       (mime-parse-buffer)
10262       )))
10263
10264 (autoload 'mime-combine-message/partial-pieces-automatically
10265   "mime-partial"
10266   "Internal method to combine message/partial messages automatically.")
10267
10268 (mime-add-condition
10269  'action '((type . message)(subtype . partial)
10270            (major-mode . gnus-original-article-mode)
10271            (method . mime-combine-message/partial-pieces-automatically)
10272            (summary-buffer-exp . gnus-summary-buffer)
10273            (request-partial-message-method . gnus-request-partial-message)
10274            ))
10275
10276
10277 ;;; @ for message/rfc822
10278 ;;;
10279
10280 (defun gnus-mime-extract-message/rfc822 (entity situation)
10281   (let (group article num cwin swin cur)
10282     (with-temp-buffer
10283       (mime-insert-entity-content entity)
10284       (setq group (or (cdr (assq 'group situation))
10285                       (completing-read "Group: "
10286                                        gnus-active-hashtb
10287                                        nil
10288                                        (gnus-read-active-file-p)
10289                                        gnus-newsgroup-name))
10290             article (gnus-request-accept-article group)))
10291     (when (and (consp article)
10292                (numberp (setq article (cdr article))))
10293       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10294             cwin (get-buffer-window (current-buffer) t))
10295       (save-window-excursion
10296         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10297             (select-window swin)
10298           (set-buffer gnus-summary-buffer))
10299         (setq cur gnus-current-article)
10300         (forward-line num)
10301         (let (gnus-show-threads)
10302           (gnus-summary-goto-subject article t))
10303         (gnus-summary-clear-mark-forward 1)
10304         (gnus-summary-goto-subject cur))
10305       (when (and cwin (window-frame cwin))
10306         (select-frame (window-frame cwin)))
10307       (when (boundp 'mime-acting-situation-to-override)
10308         (set-alist 'mime-acting-situation-to-override
10309                    'group
10310                    group)
10311         (set-alist 'mime-acting-situation-to-override
10312                    'after-method
10313                    `(progn
10314                       (save-current-buffer
10315                         (set-buffer gnus-group-buffer)
10316                         (gnus-activate-group ,group))
10317                       (gnus-summary-goto-article ,cur
10318                                                  gnus-show-all-headers)))
10319         (set-alist 'mime-acting-situation-to-override
10320                    'number num)))))
10321
10322 (mime-add-condition
10323  'action '((type . message)(subtype . rfc822)
10324            (major-mode . gnus-original-article-mode)
10325            (method . gnus-mime-extract-message/rfc822)
10326            (mode . "extract")
10327            ))
10328
10329 (mime-add-condition
10330  'action '((type . message)(subtype . news)
10331            (major-mode . gnus-original-article-mode)
10332            (method . gnus-mime-extract-message/rfc822)
10333            (mode . "extract")
10334            ))
10335
10336 (defun gnus-mime-extract-multipart (entity situation)
10337   (let ((children (mime-entity-children entity))
10338         mime-acting-situation-to-override
10339         f)
10340     (while children
10341       (mime-play-entity (car children)
10342                         (cons (assq 'mode situation)
10343                               mime-acting-situation-to-override))
10344       (setq children (cdr children)))
10345     (if (setq f (cdr (assq 'after-method
10346                            mime-acting-situation-to-override)))
10347         (eval f)
10348       )))
10349
10350 (mime-add-condition
10351  'action '((type . multipart)
10352            (method . gnus-mime-extract-multipart)
10353            (mode . "extract")
10354            )
10355  'with-default)
10356
10357
10358 ;;; @ end
10359 ;;;
10360
10361 (defun gnus-summary-setup-default-charset ()
10362   "Setup newsgroup default charset."
10363   (if (equal gnus-newsgroup-name "nndraft:drafts")
10364       (setq gnus-newsgroup-charset nil)
10365     (let* ((ignored-charsets
10366             (or gnus-newsgroup-ephemeral-ignored-charsets
10367                 (append
10368                  (and gnus-newsgroup-name
10369                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10370                  gnus-newsgroup-ignored-charsets))))
10371       (setq gnus-newsgroup-charset
10372             (or gnus-newsgroup-ephemeral-charset
10373                 (and gnus-newsgroup-name
10374                      (gnus-parameter-charset gnus-newsgroup-name))
10375                 gnus-default-charset))
10376       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10377            ignored-charsets))))
10378
10379 ;;;
10380 ;;; Mime Commands
10381 ;;;
10382
10383 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10384   "Display the current article buffer fully MIME-buttonized.
10385 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10386 treated as multipart/mixed."
10387   (interactive "P")
10388   (require 'gnus-art)
10389   (let ((gnus-unbuttonized-mime-types nil)
10390         (gnus-mime-display-multipart-as-mixed show-all-parts))
10391     (gnus-summary-show-article)))
10392
10393 (defun gnus-summary-repair-multipart (article)
10394   "Add a Content-Type header to a multipart article without one."
10395   (interactive (list (gnus-summary-article-number)))
10396   (gnus-with-article article
10397     (message-narrow-to-head)
10398     (message-remove-header "Mime-Version")
10399     (goto-char (point-max))
10400     (insert "Mime-Version: 1.0\n")
10401     (widen)
10402     (when (search-forward "\n--" nil t)
10403       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10404         (message-narrow-to-head)
10405         (message-remove-header "Content-Type")
10406         (goto-char (point-max))
10407         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10408                         separator))
10409         (widen))))
10410   (let (gnus-mark-article-hook)
10411     (gnus-summary-select-article t t nil article)))
10412
10413 (defun gnus-summary-toggle-display-buttonized ()
10414   "Toggle the buttonizing of the article buffer."
10415   (interactive)
10416   (require 'gnus-art)
10417   (if (setq gnus-inhibit-mime-unbuttonizing
10418             (not gnus-inhibit-mime-unbuttonizing))
10419       (let ((gnus-unbuttonized-mime-types nil))
10420         (gnus-summary-show-article))
10421     (gnus-summary-show-article)))
10422
10423 ;;;
10424 ;;; Intelli-mouse commmands
10425 ;;;
10426
10427 (defun gnus-wheel-summary-scroll (event)
10428   (interactive "e")
10429   (let ((amount (if (memq 'shift (event-modifiers event))
10430                     (car gnus-wheel-scroll-amount)
10431                   (cdr gnus-wheel-scroll-amount)))
10432         (direction (- (* (static-if (featurep 'xemacs)
10433                              (event-button event)
10434                            (cond ((eq 'mouse-4 (event-basic-type event))
10435                                   4)
10436                                  ((eq 'mouse-5 (event-basic-type event))
10437                                   5)))
10438                          2) 9))
10439         edge)
10440     (gnus-summary-scroll-up (* amount direction))
10441     (when (gnus-eval-in-buffer-window gnus-article-buffer
10442             (save-restriction
10443               (widen)
10444               (and (if (< 0 direction)
10445                        (gnus-article-next-page 0)
10446                      (gnus-article-prev-page 0)
10447                      (bobp))
10448                    (if (setq edge (get-text-property
10449                                    (point-min) 'gnus-wheel-edge))
10450                        (setq edge (* edge direction))
10451                      (setq edge -1))
10452                    (or (plusp edge)
10453                        (let ((buffer-read-only nil)
10454                              (inhibit-read-only t))
10455                          (put-text-property (point-min) (point-max)
10456                                             'gnus-wheel-edge direction)
10457                          nil))
10458                    (or (> edge gnus-wheel-edge-resistance)
10459                        (let ((buffer-read-only nil)
10460                              (inhibit-read-only t))
10461                          (put-text-property (point-min) (point-max)
10462                                             'gnus-wheel-edge
10463                                             (* (1+ edge) direction))
10464                          nil))
10465                    (eq last-command 'gnus-wheel-summary-scroll))))
10466       (gnus-summary-next-article nil nil (minusp direction)))))
10467
10468 (defun gnus-wheel-install ()
10469   "Enable mouse wheel support on summary window."
10470   (when gnus-use-wheel
10471     (let ((keys
10472            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10473       (dolist (key keys)
10474         (define-key gnus-summary-mode-map key
10475           'gnus-wheel-summary-scroll)))))
10476
10477 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10478
10479 ;;;
10480 ;;; Traditional PGP commmands
10481 ;;;
10482
10483 (defun gnus-summary-decrypt-article (&optional force)
10484   "Decrypt the current article in traditional PGP way.
10485 This will have permanent effect only in mail groups.
10486 If FORCE is non-nil, allow editing of articles even in read-only
10487 groups."
10488   (interactive "P")
10489   (gnus-summary-select-article t)
10490   (gnus-eval-in-buffer-window gnus-article-buffer
10491     (save-excursion
10492       (save-restriction
10493         (widen)
10494         (goto-char (point-min))
10495         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10496           (error "Not a traditional PGP message!"))
10497         (let ((armor-start (match-beginning 0)))
10498           (if (and (pgg-decrypt-region armor-start (point-max))
10499                    (or force (not (gnus-group-read-only-p))))
10500               (let ((inhibit-read-only t)
10501                     buffer-read-only)
10502                 (delete-region armor-start
10503                                (progn
10504                                  (re-search-forward "^-+END PGP" nil t)
10505                                  (beginning-of-line 2)
10506                                  (point)))
10507                 (insert-buffer-substring pgg-output-buffer))))))))
10508
10509 (defun gnus-summary-verify-article ()
10510   "Verify the current article in traditional PGP way."
10511   (interactive)
10512   (save-excursion
10513     (set-buffer gnus-original-article-buffer)
10514     (goto-char (point-min))
10515     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10516       (error "Not a traditional PGP message!"))
10517     (re-search-forward "^-+END PGP" nil t)
10518     (beginning-of-line 2)
10519     (call-interactively (function pgg-verify-region))))
10520
10521 ;;;
10522 ;;; Generic summary marking commands
10523 ;;;
10524
10525 (defvar gnus-summary-marking-alist
10526   '((read gnus-del-mark "d")
10527     (unread gnus-unread-mark "u")
10528     (ticked gnus-ticked-mark "!")
10529     (dormant gnus-dormant-mark "?")
10530     (expirable gnus-expirable-mark "e"))
10531   "An alist of names/marks/keystrokes.")
10532
10533 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10534 (defvar gnus-summary-mark-map)
10535
10536 (defun gnus-summary-make-all-marking-commands ()
10537   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10538   (dolist (elem gnus-summary-marking-alist)
10539     (apply 'gnus-summary-make-marking-command elem)))
10540
10541 (defun gnus-summary-make-marking-command (name mark keystroke)
10542   (let ((map (make-sparse-keymap)))
10543     (define-key gnus-summary-generic-mark-map keystroke map)
10544     (dolist (lway `((next "next" next nil "n")
10545                     (next-unread "next unread" next t "N")
10546                     (prev "previous" prev nil "p")
10547                     (prev-unread "previous unread" prev t "P")
10548                     (nomove "" nil nil ,keystroke)))
10549       (let ((func (gnus-summary-make-marking-command-1
10550                    mark (car lway) lway name)))
10551         (setq func (eval func))
10552         (define-key map (nth 4 lway) func)))))
10553
10554 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10555   `(defun ,(intern
10556             (format "gnus-summary-put-mark-as-%s%s"
10557                     name (if (eq way 'nomove)
10558                              ""
10559                            (concat "-" (symbol-name way)))))
10560      (n)
10561      ,(format
10562        "Mark the current article as %s%s.
10563 If N, the prefix, then repeat N times.
10564 If N is negative, move in reverse order.
10565 The difference between N and the actual number of articles marked is
10566 returned."
10567        name (car (cdr lway)))
10568      (interactive "p")
10569      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10570
10571 (defun gnus-summary-generic-mark (n mark move unread)
10572   "Mark N articles with MARK."
10573   (unless (eq major-mode 'gnus-summary-mode)
10574     (error "This command can only be used in the summary buffer"))
10575   (gnus-summary-show-thread)
10576   (let ((nummove
10577          (cond
10578           ((eq move 'next) 1)
10579           ((eq move 'prev) -1)
10580           (t 0))))
10581     (if (zerop nummove)
10582         (setq n 1)
10583       (when (< n 0)
10584         (setq n (abs n)
10585               nummove (* -1 nummove))))
10586     (while (and (> n 0)
10587                 (gnus-summary-mark-article nil mark)
10588                 (zerop (gnus-summary-next-subject nummove unread t)))
10589       (setq n (1- n)))
10590     (when (/= 0 n)
10591       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10592     (gnus-summary-recenter)
10593     (gnus-summary-position-point)
10594     (gnus-set-mode-line 'summary)
10595     n))
10596
10597 (defun gnus-summary-insert-articles (articles)
10598   (when (setq articles
10599               (gnus-set-difference articles
10600                                    (mapcar (lambda (h) (mail-header-number h))
10601                                            gnus-newsgroup-headers)))
10602     (setq gnus-newsgroup-headers 
10603           (merge 'list
10604                  gnus-newsgroup-headers
10605                  (gnus-fetch-headers articles)
10606                  'gnus-article-sort-by-number))
10607     ;; Suppress duplicates?
10608     (when gnus-suppress-duplicates
10609       (gnus-dup-suppress-articles))
10610     
10611     ;; We might want to build some more threads first.
10612     (when (and gnus-fetch-old-headers
10613                (eq gnus-headers-retrieved-by 'nov))
10614       (if (eq gnus-fetch-old-headers 'invisible)
10615         (gnus-build-all-threads)
10616         (gnus-build-old-threads)))
10617     ;; Let the Gnus agent mark articles as read.
10618     (when gnus-agent
10619       (gnus-agent-get-undownloaded-list))
10620     ;; Remove list identifiers from subject
10621     (when gnus-list-identifiers
10622       (gnus-summary-remove-list-identifiers))
10623     ;; First and last article in this newsgroup.
10624     (when gnus-newsgroup-headers
10625       (setq gnus-newsgroup-begin
10626             (mail-header-number (car gnus-newsgroup-headers))
10627             gnus-newsgroup-end
10628             (mail-header-number
10629              (gnus-last-element gnus-newsgroup-headers))))
10630     (when gnus-use-scoring
10631       (gnus-possibly-score-headers))))
10632
10633 (defun gnus-summary-insert-old-articles (&optional all)
10634   "Insert all old articles in this group.
10635 If ALL is non-nil, already read articles become readable.
10636 If ALL is a number, fetch this number of articles."
10637   (interactive "P")
10638   (prog1
10639       (let ((old (mapcar 'car gnus-newsgroup-data))
10640             (i (car gnus-newsgroup-active))
10641             older len)
10642         (while (<= i (cdr gnus-newsgroup-active))
10643           (or (memq i old) (push i older))
10644           (incf i))
10645         (setq len (length older))
10646         (cond 
10647          ((null older) nil)
10648          ((numberp all) 
10649           (if (< all len)
10650               (setq older (subseq older 0 all))))
10651          (all nil)
10652          (t
10653           (if (and (numberp gnus-large-newsgroup)
10654                    (> len gnus-large-newsgroup))
10655               (let ((input
10656                      (read-string
10657                       (format
10658                        "How many articles from %s (default %d): "
10659                        (gnus-limit-string 
10660                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10661                        len))))
10662                 (unless (string-match "^[ \t]*$" input) 
10663                   (setq all (string-to-number input))
10664                   (if (< all len)
10665                       (setq older (subseq older 0 all))))))))
10666         (if (not older)
10667             (message "No old news.")
10668           (gnus-summary-insert-articles older)
10669           (gnus-summary-limit (gnus-union older old))))
10670     (gnus-summary-position-point)))
10671
10672 (defun gnus-summary-insert-new-articles ()
10673   "Insert all new articles in this group."
10674   (interactive)
10675   (prog1
10676       (let ((old (mapcar 'car gnus-newsgroup-data))
10677             (old-active gnus-newsgroup-active)
10678             (nnmail-fetched-sources (list t))
10679             i new)
10680         (setq gnus-newsgroup-active 
10681               (gnus-activate-group gnus-newsgroup-name 'scan))
10682         (setq i (1+ (cdr old-active)))
10683         (while (<= i (cdr gnus-newsgroup-active))
10684           (push i new)
10685           (incf i))
10686         (if (not new)
10687             (message "No gnus is bad news.")
10688           (setq new (nreverse new))
10689           (gnus-summary-insert-articles new)
10690           (setq gnus-newsgroup-unreads
10691                 (append gnus-newsgroup-unreads new))
10692           (gnus-summary-limit (gnus-union old new))))
10693     (gnus-summary-position-point)))
10694
10695 (gnus-summary-make-all-marking-commands)
10696
10697 (gnus-ems-redefine)
10698
10699 (provide 'gnus-sum)
10700
10701 (run-hooks 'gnus-sum-load-hook)
10702
10703 ;;; gnus-sum.el ends here