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 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 If threads are hidden, you have to run the command
252 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
253 to expose hidden threads."
254   :group 'gnus-thread
255   :type 'boolean)
256
257 (defcustom gnus-thread-hide-killed t
258   "*If non-nil, hide killed threads automatically."
259   :group 'gnus-thread
260   :type 'boolean)
261
262 (defcustom gnus-thread-ignore-subject t
263   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
264 If nil, articles that have different subjects from their parents will
265 start separate threads."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-operation-ignore-subject t
270   "*If non-nil, subjects will be ignored when doing thread commands.
271 This affects commands like `gnus-summary-kill-thread' and
272 `gnus-summary-lower-thread'.
273
274 If this variable is nil, articles in the same thread with different
275 subjects will not be included in the operation in question.  If this
276 variable is `fuzzy', only articles that have subjects that are fuzzily
277 equal will be included."
278   :group 'gnus-thread
279   :type '(choice (const :tag "off" nil)
280                  (const fuzzy)
281                  (sexp :tag "on" t)))
282
283 (defcustom gnus-thread-indent-level 4
284   "*Number that says how much each sub-thread should be indented."
285   :group 'gnus-thread
286   :type 'integer)
287
288 (defcustom gnus-auto-extend-newsgroup t
289   "*If non-nil, extend newsgroup forward and backward when requested."
290   :group 'gnus-summary-choose
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-first t
294   "*If nil, don't select the first unread article when entering a group.
295 If this variable is `best', select the highest-scored unread article
296 in the group.  If t, select the first unread article.
297
298 This variable can also be a function to place point on a likely
299 subject line.  Useful values include `gnus-summary-first-unread-subject',
300 `gnus-summary-first-unread-article' and
301 `gnus-summary-best-unread-article'.
302
303 If you want to prevent automatic selection of the first unread article
304 in some newsgroups, set the variable to nil in
305 `gnus-select-group-hook'."
306   :group 'gnus-group-select
307   :type '(choice (const :tag "none" nil)
308                  (const best)
309                  (sexp :menu-tag "first" t)
310                  (function-item gnus-summary-first-unread-subject)
311                  (function-item gnus-summary-first-unread-article)
312                  (function-item gnus-summary-best-unread-article)))
313
314 (defcustom gnus-dont-select-after-jump-to-other-group nil
315   "If non-nil, don't select the first unread article after entering the
316 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
317 it is depend on the value of `gnus-auto-select-first' whether to select
318 or not."
319   :group 'gnus-group-select
320   :type 'boolean)
321
322 (defcustom gnus-auto-select-next t
323   "*If non-nil, offer to go to the next group from the end of the previous.
324 If the value is t and the next newsgroup is empty, Gnus will exit
325 summary mode and go back to group mode.  If the value is neither nil
326 nor t, Gnus will select the following unread newsgroup.  In
327 particular, if the value is the symbol `quietly', the next unread
328 newsgroup will be selected without any confirmation, and if it is
329 `almost-quietly', the next group will be selected without any
330 confirmation if you are located on the last article in the group.
331 Finally, if this variable is `slightly-quietly', the `Z n' command
332 will go to the next group without confirmation."
333   :group 'gnus-summary-maneuvering
334   :type '(choice (const :tag "off" nil)
335                  (const quietly)
336                  (const almost-quietly)
337                  (const slightly-quietly)
338                  (sexp :menu-tag "on" t)))
339
340 (defcustom gnus-auto-select-same nil
341   "*If non-nil, select the next article with the same subject.
342 If there are no more articles with the same subject, go to
343 the first unread article."
344   :group 'gnus-summary-maneuvering
345   :type 'boolean)
346
347 (defcustom gnus-summary-check-current nil
348   "*If non-nil, consider the current article when moving.
349 The \"unread\" movement commands will stay on the same line if the
350 current article is unread."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-center-summary t
355   "*If non-nil, always center the current summary buffer.
356 In particular, if `vertical' do only vertical recentering.  If non-nil
357 and non-`vertical', do both horizontal and vertical recentering."
358   :group 'gnus-summary-maneuvering
359   :type '(choice (const :tag "none" nil)
360                  (const vertical)
361                  (integer :tag "height")
362                  (sexp :menu-tag "both" t)))
363
364 (defcustom gnus-show-all-headers nil
365   "*If non-nil, don't hide any headers."
366   :group 'gnus-article-hiding
367   :group 'gnus-article-headers
368   :type 'boolean)
369
370 (defcustom gnus-summary-ignore-duplicates nil
371   "*If non-nil, ignore articles with identical Message-ID headers."
372   :group 'gnus-summary
373   :type 'boolean)
374
375 (defcustom gnus-single-article-buffer t
376   "*If non-nil, display all articles in the same buffer.
377 If nil, each group will get its own article buffer."
378   :group 'gnus-article-various
379   :type 'boolean)
380
381 (defcustom gnus-break-pages t
382   "*If non-nil, do page breaking on articles.
383 The page delimiter is specified by the `gnus-page-delimiter'
384 variable."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-show-mime t
389   "*If non-nil, do mime processing of articles.
390 The articles will simply be fed to the function given by
391 `gnus-article-display-method-for-mime'."
392   :group 'gnus-article-mime
393   :type 'boolean)
394
395 (defcustom gnus-move-split-methods nil
396   "*Variable used to suggest where articles are to be moved to.
397 It uses the same syntax as the `gnus-split-methods' variable.
398 However, whereas `gnus-split-methods' specifies file names as targets,
399 this variable specifies group names."
400   :group 'gnus-summary-mail
401   :type '(repeat (choice (list :value (fun) function)
402                          (cons :value ("" "") regexp (repeat string))
403                          (sexp :value nil))))
404
405 (defcustom gnus-unread-mark ?\ ;;;Whitespace
406   "*Mark used for unread articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-ticked-mark ?!
411   "*Mark used for ticked articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-dormant-mark ??
416   "*Mark used for dormant articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-del-mark ?r
421   "*Mark used for del'd articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-read-mark ?R
426   "*Mark used for read articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-expirable-mark ?E
431   "*Mark used for expirable articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-killed-mark ?K
436   "*Mark used for killed articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?\ ;;;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?@
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloadable-mark ?%
521   "*Mark used for articles that are to be downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unsendable-mark ?=
526   "*Mark used for articles that won't be sent."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-score-over-mark ?+
531   "*Score mark used for articles with high scores."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-below-mark ?-
536   "*Score mark used for articles with low scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
541   "*There is no thread under the article."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-not-empty-thread-mark ?=
546   "*There is a thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-view-pseudo-asynchronously nil
551   "*If non-nil, Gnus will view pseudo-articles asynchronously."
552   :group 'gnus-extract-view
553   :type 'boolean)
554
555 (defcustom gnus-auto-expirable-marks
556   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
557         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
558         gnus-souped-mark gnus-duplicate-mark)
559   "*The list of marks converted into expiration if a group is auto-expirable."
560   :version "21.1"
561   :group 'gnus-summary
562   :type '(repeat character))
563
564 (defcustom gnus-inhibit-user-auto-expire t
565   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
566   :version "21.1"
567   :group 'gnus-summary
568   :type 'boolean)
569
570 (defcustom gnus-view-pseudos nil
571   "*If `automatic', pseudo-articles will be viewed automatically.
572 If `not-confirm', pseudos will be viewed automatically, and the user
573 will not be asked to confirm the command."
574   :group 'gnus-extract-view
575   :type '(choice (const :tag "off" nil)
576                  (const automatic)
577                  (const not-confirm)))
578
579 (defcustom gnus-view-pseudos-separately t
580   "*If non-nil, one pseudo-article will be created for each file to be viewed.
581 If nil, all files that use the same viewing command will be given as a
582 list of parameters to that command."
583   :group 'gnus-extract-view
584   :type 'boolean)
585
586 (defcustom gnus-insert-pseudo-articles t
587   "*If non-nil, insert pseudo-articles when decoding articles."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-summary-dummy-line-format
592   "  %(:                          :%) %S\n"
593   "*The format specification for the dummy roots in the summary buffer.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions.
596
597 %S  The subject"
598   :group 'gnus-threading
599   :type 'string)
600
601 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
602   "*The format specification for the summary mode line.
603 It works along the same lines as a normal formatting string,
604 with some simple extensions:
605
606 %G  Group name
607 %p  Unprefixed group name
608 %A  Current article number
609 %z  Current article score
610 %V  Gnus version
611 %U  Number of unread articles in the group
612 %e  Number of unselected articles in the group
613 %Z  A string with unread/unselected article counts
614 %g  Shortish group name
615 %S  Subject of the current article
616 %u  User-defined spec
617 %s  Current score file name
618 %d  Number of dormant articles
619 %r  Number of articles that have been marked as read in this session
620 %E  Number of articles expunged by the score files"
621   :group 'gnus-summary-format
622   :type 'string)
623
624 (defcustom gnus-list-identifiers nil
625   "Regexp that matches list identifiers to be removed from subject.
626 This can also be a list of regexps."
627   :version "21.1"
628   :group 'gnus-summary-format
629   :group 'gnus-article-hiding
630   :type '(choice (const :tag "none" nil)
631                  (regexp :value ".*")
632                  (repeat :value (".*") regexp)))
633
634 (defcustom gnus-summary-mark-below 0
635   "*Mark all articles with a score below this variable as read.
636 This variable is local to each summary buffer and usually set by the
637 score file."
638   :group 'gnus-score-default
639   :type 'integer)
640
641 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
642   "*List of functions used for sorting articles in the summary buffer.
643
644 Each function takes two articles and returns non-nil if the first
645 article should be sorted before the other.  If you use more than one
646 function, the primary sort function should be the last.  You should
647 probably always include `gnus-article-sort-by-number' in the list of
648 sorting functions -- preferably first.  Also note that sorting by date
649 is often much slower than sorting by number, and the sorting order is
650 very similar.  (Sorting by date means sorting by the time the message
651 was sent, sorting by number means sorting by arrival time.)
652
653 Ready-made functions include `gnus-article-sort-by-number',
654 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
655 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
656
657 When threading is turned on, the variable `gnus-thread-sort-functions'
658 controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-article-sort-by-number)
661                          (function-item gnus-article-sort-by-author)
662                          (function-item gnus-article-sort-by-subject)
663                          (function-item gnus-article-sort-by-date)
664                          (function-item gnus-article-sort-by-score)
665                          (function :tag "other"))))
666
667 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
668   "*List of functions used for sorting threads in the summary buffer.
669 By default, threads are sorted by article number.
670
671 Each function takes two threads and returns non-nil if the first
672 thread should be sorted before the other.  If you use more than one
673 function, the primary sort function should be the last.  You should
674 probably always include `gnus-thread-sort-by-number' in the list of
675 sorting functions -- preferably first.  Also note that sorting by date
676 is often much slower than sorting by number, and the sorting order is
677 very similar.  (Sorting by date means sorting by the time the message
678 was sent, sorting by number means sorting by arrival time.)
679
680 Ready-made functions include `gnus-thread-sort-by-number',
681 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
682 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
683 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
684
685 When threading is turned off, the variable
686 `gnus-article-sort-functions' controls how articles are sorted."
687   :group 'gnus-summary-sort
688   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
689                          (function-item gnus-thread-sort-by-author)
690                          (function-item gnus-thread-sort-by-subject)
691                          (function-item gnus-thread-sort-by-date)
692                          (function-item gnus-thread-sort-by-score)
693                          (function-item gnus-thread-sort-by-total-score)
694                          (function :tag "other"))))
695
696 (defcustom gnus-thread-score-function '+
697   "*Function used for calculating the total score of a thread.
698
699 The function is called with the scores of the article and each
700 subthread and should then return the score of the thread.
701
702 Some functions you can use are `+', `max', or `min'."
703   :group 'gnus-summary-sort
704   :type 'function)
705
706 (defcustom gnus-summary-expunge-below nil
707   "All articles that have a score less than this variable will be expunged.
708 This variable is local to the summary buffers."
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-thread-expunge-below nil
714   "All threads that have a total score less than this variable will be expunged.
715 See `gnus-thread-score-function' for en explanation of what a
716 \"thread score\" is.
717
718 This variable is local to the summary buffers."
719   :group 'gnus-threading
720   :group 'gnus-score-default
721   :type '(choice (const :tag "off" nil)
722                  integer))
723
724 (defcustom gnus-summary-mode-hook nil
725   "*A hook for Gnus summary mode.
726 This hook is run before any variables are set in the summary buffer."
727   :options '(turn-on-gnus-mailing-list-mode)
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
732 (when (featurep 'xemacs)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
734   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
735   (add-hook 'gnus-summary-mode-hook
736             'gnus-xmas-switch-horizontal-scrollbar-off))
737
738 (defcustom gnus-summary-menu-hook nil
739   "*Hook run after the creation of the summary mode menu."
740   :group 'gnus-summary-visual
741   :type 'hook)
742
743 (defcustom gnus-summary-exit-hook nil
744   "*A hook called on exit from the summary buffer.
745 It will be called with point in the group buffer."
746   :group 'gnus-summary-exit
747   :type 'hook)
748
749 (defcustom gnus-summary-prepare-hook nil
750   "*A hook called after the summary buffer has been generated.
751 If you want to modify the summary buffer, you can use this hook."
752   :group 'gnus-summary-various
753   :type 'hook)
754
755 (defcustom gnus-summary-prepared-hook nil
756   "*A hook called as the last thing after the summary buffer has been generated."
757   :group 'gnus-summary-various
758   :type 'hook)
759
760 (defcustom gnus-summary-generate-hook nil
761   "*A hook run just before generating the summary buffer.
762 This hook is commonly used to customize threading variables and the
763 like."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-select-group-hook nil
768   "*A hook called when a newsgroup is selected.
769
770 If you'd like to simplify subjects like the
771 `gnus-summary-next-same-subject' command does, you can use the
772 following hook:
773
774  (setq gnus-select-group-hook
775       (list
776         (lambda ()
777           (mapcar (lambda (header)
778                      (mail-header-set-subject
779                       header
780                       (gnus-simplify-subject
781                        (mail-header-subject header) 're-only)))
782                   gnus-newsgroup-headers))))"
783   :group 'gnus-group-select
784   :type 'hook)
785
786 (defcustom gnus-select-article-hook nil
787   "*A hook called when an article is selected."
788   :group 'gnus-summary-choose
789   :type 'hook)
790
791 (defcustom gnus-visual-mark-article-hook
792   (list 'gnus-highlight-selected-summary)
793   "*Hook run after selecting an article in the summary buffer.
794 It is meant to be used for highlighting the article in some way.  It
795 is not run if `gnus-visual' is nil."
796   :group 'gnus-summary-visual
797   :type 'hook)
798
799 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
800   "*A hook called before parsing the headers."
801   :group 'gnus-various
802   :type 'hook)
803
804 (defcustom gnus-exit-group-hook nil
805   "*A hook called when exiting summary mode.
806 This hook is not called from the non-updating exit commands like `Q'."
807   :group 'gnus-various
808   :type 'hook)
809
810 (defcustom gnus-summary-update-hook
811   (list 'gnus-summary-highlight-line)
812   "*A hook called when a summary line is changed.
813 The hook will not be called if `gnus-visual' is nil.
814
815 The default function `gnus-summary-highlight-line' will
816 highlight the line according to the `gnus-summary-highlight'
817 variable."
818   :group 'gnus-summary-visual
819   :type 'hook)
820
821 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
822   "*A hook called when an article is selected for the first time.
823 The hook is intended to mark an article as read (or unread)
824 automatically when it is selected."
825   :group 'gnus-summary-choose
826   :type 'hook)
827
828 (defcustom gnus-group-no-more-groups-hook nil
829   "*A hook run when returning to group mode having no more (unread) groups."
830   :group 'gnus-group-select
831   :type 'hook)
832
833 (defcustom gnus-ps-print-hook nil
834   "*A hook run before ps-printing something from Gnus."
835   :group 'gnus-summary
836   :type 'hook)
837
838 (defcustom gnus-summary-display-arrow
839   (and (fboundp 'display-graphic-p)
840        (display-graphic-p))
841   "*If non-nil, display an arrow highlighting the current article."
842   :version "21.1"
843   :group 'gnus-summary
844   :type 'boolean)
845
846 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
847   "Face used for highlighting the current article in the summary buffer."
848   :group 'gnus-summary-visual
849   :type 'face)
850
851 (defcustom gnus-summary-highlight
852   '(((= mark gnus-canceled-mark)
853      . gnus-summary-cancelled-face)
854     ((and (> score default-high)
855           (or (= mark gnus-dormant-mark)
856               (= mark gnus-ticked-mark)))
857      . gnus-summary-high-ticked-face)
858     ((and (< score default-low)
859           (or (= mark gnus-dormant-mark)
860               (= mark gnus-ticked-mark)))
861      . gnus-summary-low-ticked-face)
862     ((or (= mark gnus-dormant-mark)
863          (= mark gnus-ticked-mark))
864      . gnus-summary-normal-ticked-face)
865     ((and (> score default-high) (= mark gnus-ancient-mark))
866      . gnus-summary-high-ancient-face)
867     ((and (< score default-low) (= mark gnus-ancient-mark))
868      . gnus-summary-low-ancient-face)
869     ((= mark gnus-ancient-mark)
870      . gnus-summary-normal-ancient-face)
871     ((and (> score default-high) (= mark gnus-unread-mark))
872      . gnus-summary-high-unread-face)
873     ((and (< score default-low) (= mark gnus-unread-mark))
874      . gnus-summary-low-unread-face)
875     ((= mark gnus-unread-mark)
876      . gnus-summary-normal-unread-face)
877     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
878                                                   gnus-undownloaded-mark)))
879      . gnus-summary-high-unread-face)
880     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
881                                                  gnus-undownloaded-mark)))
882      . gnus-summary-low-unread-face)
883     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
884           (memq article gnus-newsgroup-unreads))
885      . gnus-summary-normal-unread-face)
886     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
887      . gnus-summary-normal-read-face)
888     ((> score default-high)
889      . gnus-summary-high-read-face)
890     ((< score default-low)
891      . gnus-summary-low-read-face)
892     (t
893      . gnus-summary-normal-read-face))
894   "*Controls the highlighting of summary buffer lines.
895
896 A list of (FORM . FACE) pairs.  When deciding how a a particular
897 summary line should be displayed, each form is evaluated.  The content
898 of the face field after the first true form is used.  You can change
899 how those summary lines are displayed, by editing the face field.
900
901 You can use the following variables in the FORM field.
902
903 score:        The article's score
904 default:      The default article score.
905 default-high: The default score for high scored articles.
906 default-low:  The default score for low scored articles.
907 below:        The score below which articles are automatically marked as read.
908 mark:         The articles mark."
909   :group 'gnus-summary-visual
910   :type '(repeat (cons (sexp :tag "Form" nil)
911                        face)))
912
913 (defcustom gnus-alter-header-function nil
914   "Function called to allow alteration of article header structures.
915 The function is called with one parameter, the article header vector,
916 which it may alter in any way.")
917
918 (defvar gnus-decode-encoded-word-function
919   (mime-find-field-decoder 'From 'nov)
920   "Variable that says which function should be used to decode a string with encoded words.")
921
922 (defcustom gnus-extra-headers nil
923   "*Extra headers to parse."
924   :version "21.1"
925   :group 'gnus-summary
926   :type '(repeat symbol))
927
928 (defcustom gnus-ignored-from-addresses
929   (and user-mail-address (regexp-quote user-mail-address))
930   "*Regexp of From headers that may be suppressed in favor of To headers."
931   :version "21.1"
932   :group 'gnus-summary
933   :type 'regexp)
934
935 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
936   "List of charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939   :version "21.1"
940   :type '(repeat symbol)
941   :group 'gnus-charset)
942
943 (gnus-define-group-parameter
944  ignored-charsets
945  :type list
946  :function-document
947  "Return the ignored charsets of GROUP."
948  :variable gnus-group-ignored-charsets-alist
949  :variable-default
950  '(("alt\\.chinese\\.text" iso-8859-1))
951  :variable-document
952  "Alist of regexps (to match group names) and charsets that should be ignored.
953 When these charsets are used in the \"charset\" parameter, the
954 default charset will be used instead."
955  :variable-group gnus-charset
956  :variable-type '(repeat (cons (regexp :tag "Group")
957                                (repeat symbol)))
958  :parameter-type '(choice :tag "Ignored charsets"
959                           :value nil
960                           (repeat (symbol)))
961  :parameter-document       "\
962 List of charsets that should be ignored.
963
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead.")
966
967 (defcustom gnus-group-highlight-words-alist nil
968   "Alist of group regexps and highlight regexps.
969 This variable uses the same syntax as `gnus-emphasis-alist'."
970   :version "21.1"
971   :type '(repeat (cons (regexp :tag "Group")
972                        (repeat (list (regexp :tag "Highlight regexp")
973                                      (number :tag "Group for entire word" 0)
974                                      (number :tag "Group for displayed part" 0)
975                                      (symbol :tag "Face"
976                                              gnus-emphasis-highlight-words)))))
977   :group 'gnus-summary-visual)
978
979 (defcustom gnus-use-wheel nil
980   "Use Intelli-mouse on summary movement"
981   :type 'boolean
982   :group 'gnus-summary-maneuvering)
983
984 (defcustom gnus-wheel-scroll-amount '(5 . 1)
985   "Amount to scroll messages by spinning the mouse wheel.
986 This is actually a cons cell, where the first item is the amount to scroll
987 on a normal wheel event, and the second is the amount to scroll when the
988 wheel is moved with the shift key depressed."
989   :type '(cons (integer :tag "Shift") integer)
990   :group 'gnus-summary-maneuvering)
991
992 (defcustom gnus-wheel-edge-resistance 2
993   "How hard it should be to change the current article
994 by moving the mouse over the edge of the article window."
995   :type 'integer
996   :group 'gnus-summary-maneuvering)
997
998 (defcustom gnus-summary-show-article-charset-alist
999   nil
1000   "Alist of number and charset.
1001 The article will be shown with the charset corresponding to the
1002 numbered argument.
1003 For example: ((1 . cn-gb-2312) (2 . big5))."
1004   :version "21.1"
1005   :type '(repeat (cons (number :tag "Argument" 1)
1006                        (symbol :tag "Charset")))
1007   :group 'gnus-charset)
1008
1009 (defcustom gnus-preserve-marks t
1010   "Whether marks are preserved when moving, copying and respooling messages."
1011   :version "21.1"
1012   :type 'boolean
1013   :group 'gnus-summary-marks)
1014
1015 (defcustom gnus-alter-articles-to-read-function nil
1016   "Function to be called to alter the list of articles to be selected."
1017   :type '(choice (const nil) function)
1018   :group 'gnus-summary)
1019
1020 (defcustom gnus-orphan-score nil
1021   "*All orphans get this score added.  Set in the score file."
1022   :group 'gnus-score-default
1023   :type '(choice (const nil)
1024                  integer))
1025
1026 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1027   "*A regexp to match MIME parts when saving multiple parts of a message
1028 with gnus-summary-save-parts (X m). This regexp will be used by default
1029 when prompting the user for which type of files to save."
1030   :group 'gnus-summary
1031   :type 'regexp)
1032
1033
1034 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1035   "*A regexp to match MIME parts when saving multiple parts of a message
1036 with gnus-summary-save-parts (X m). This regexp will be used by default
1037 when prompting the user for which type of files to save."
1038   :group 'gnus-summary
1039   :type 'regexp)
1040
1041 (defcustom gnus-read-all-available-headers nil
1042   "Whether Gnus should parse all headers made available to it.
1043 This is mostly relevant for slow backends where the user may
1044 wish to widen the summary buffer to include all headers
1045 that were fetched.  Say, for nnultimate groups."
1046   :group 'gnus-summary
1047   :type '(choice boolean regexp))
1048
1049 ;;; Internal variables
1050
1051 (defvar gnus-summary-display-cache nil)
1052 (defvar gnus-article-mime-handles nil)
1053 (defvar gnus-article-decoded-p nil)
1054 (defvar gnus-article-charset nil)
1055 (defvar gnus-article-ignored-charsets nil)
1056 (defvar gnus-scores-exclude-files nil)
1057 (defvar gnus-page-broken nil)
1058 (defvar gnus-inhibit-mime-unbuttonizing nil)
1059
1060 (defvar gnus-original-article nil)
1061 (defvar gnus-article-internal-prepare-hook nil)
1062 (defvar gnus-newsgroup-process-stack nil)
1063
1064 (defvar gnus-thread-indent-array nil)
1065 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1066 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1067   "Function called to sort the articles within a thread after it has been gathered together.")
1068
1069 (defvar gnus-summary-save-parts-type-history nil)
1070 (defvar gnus-summary-save-parts-last-directory nil)
1071
1072 (defvar gnus-summary-save-parts-type-history nil)
1073 (defvar gnus-summary-save-parts-last-directory nil)
1074
1075 ;; Avoid highlighting in kill files.
1076 (defvar gnus-summary-inhibit-highlight nil)
1077 (defvar gnus-newsgroup-selected-overlay nil)
1078 (defvar gnus-inhibit-limiting nil)
1079 (defvar gnus-newsgroup-adaptive-score-file nil)
1080 (defvar gnus-current-score-file nil)
1081 (defvar gnus-current-move-group nil)
1082 (defvar gnus-current-copy-group nil)
1083 (defvar gnus-current-crosspost-group nil)
1084 (defvar gnus-newsgroup-display nil)
1085
1086 (defvar gnus-newsgroup-dependencies nil)
1087 (defvar gnus-newsgroup-adaptive nil)
1088 (defvar gnus-summary-display-article-function nil)
1089 (defvar gnus-summary-highlight-line-function nil
1090   "Function called after highlighting a summary line.")
1091
1092 (defvar gnus-summary-line-format-alist
1093   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1094     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1095     (?s gnus-tmp-subject-or-nil ?s)
1096     (?n gnus-tmp-name ?s)
1097     (?A (std11-address-string
1098          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1099     (?a (or (std11-full-name-string
1100              (car (mime-entity-read-field gnus-tmp-header 'From)))
1101             gnus-tmp-from) ?s)
1102     (?F gnus-tmp-from ?s)
1103     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1104     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1105     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1106     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1107     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1108     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1109     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1110     (?L gnus-tmp-lines ?s)
1111     (?I gnus-tmp-indentation ?s)
1112     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1113     (?R gnus-tmp-replied ?c)
1114     (?\[ gnus-tmp-opening-bracket ?c)
1115     (?\] gnus-tmp-closing-bracket ?c)
1116     (?\> (make-string gnus-tmp-level ? ) ?s)
1117     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1118     (?i gnus-tmp-score ?d)
1119     (?z gnus-tmp-score-char ?c)
1120     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1121     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1122     (?U gnus-tmp-unread ?c)
1123     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1124     (?t (gnus-summary-number-of-articles-in-thread
1125          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1126         ?d)
1127     (?e (gnus-summary-number-of-articles-in-thread
1128          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1129         ?c)
1130     (?u gnus-tmp-user-defined ?s)
1131     (?P (gnus-pick-line-number) ?d)
1132     (?B gnus-tmp-thread-tree-header-string ?s))
1133   "An alist of format specifications that can appear in summary lines.
1134 These are paired with what variables they correspond with, along with
1135 the type of the variable (string, integer, character, etc).")
1136
1137 (defvar gnus-summary-dummy-line-format-alist
1138   `((?S gnus-tmp-subject ?s)
1139     (?N gnus-tmp-number ?d)
1140     (?u gnus-tmp-user-defined ?s)))
1141
1142 (defvar gnus-summary-mode-line-format-alist
1143   `((?G gnus-tmp-group-name ?s)
1144     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1145     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1146     (?A gnus-tmp-article-number ?d)
1147     (?Z gnus-tmp-unread-and-unselected ?s)
1148     (?V gnus-version ?s)
1149     (?U gnus-tmp-unread-and-unticked ?d)
1150     (?S gnus-tmp-subject ?s)
1151     (?e gnus-tmp-unselected ?d)
1152     (?u gnus-tmp-user-defined ?s)
1153     (?d (length gnus-newsgroup-dormant) ?d)
1154     (?t (length gnus-newsgroup-marked) ?d)
1155     (?r (length gnus-newsgroup-reads) ?d)
1156     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1157     (?E gnus-newsgroup-expunged-tally ?d)
1158     (?s (gnus-current-score-file-nondirectory) ?s)))
1159
1160 (defvar gnus-last-search-regexp nil
1161   "Default regexp for article search command.")
1162
1163 (defvar gnus-summary-search-article-matched-data nil
1164   "Last matched data of article search command.  It is the local variable
1165 in `gnus-article-buffer' which consists of the list of start position,
1166 end position and text.")
1167
1168 (defvar gnus-last-shell-command nil
1169   "Default shell command on article.")
1170
1171 (defvar gnus-newsgroup-begin nil)
1172 (defvar gnus-newsgroup-end nil)
1173 (defvar gnus-newsgroup-last-rmail nil)
1174 (defvar gnus-newsgroup-last-mail nil)
1175 (defvar gnus-newsgroup-last-folder nil)
1176 (defvar gnus-newsgroup-last-file nil)
1177 (defvar gnus-newsgroup-auto-expire nil)
1178 (defvar gnus-newsgroup-active nil)
1179
1180 (defvar gnus-newsgroup-data nil)
1181 (defvar gnus-newsgroup-data-reverse nil)
1182 (defvar gnus-newsgroup-limit nil)
1183 (defvar gnus-newsgroup-limits nil)
1184
1185 (defvar gnus-newsgroup-unreads nil
1186   "List of unread articles in the current newsgroup.")
1187
1188 (defvar gnus-newsgroup-unselected nil
1189   "List of unselected unread articles in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-reads nil
1192   "Alist of read articles and article marks in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-expunged-tally nil)
1195
1196 (defvar gnus-newsgroup-marked nil
1197   "List of ticked articles in the current newsgroup (a subset of unread art).")
1198
1199 (defvar gnus-newsgroup-killed nil
1200   "List of ranges of articles that have been through the scoring process.")
1201
1202 (defvar gnus-newsgroup-cached nil
1203   "List of articles that come from the article cache.")
1204
1205 (defvar gnus-newsgroup-saved nil
1206   "List of articles that have been saved.")
1207
1208 (defvar gnus-newsgroup-kill-headers nil)
1209
1210 (defvar gnus-newsgroup-replied nil
1211   "List of articles that have been replied to in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-forwarded nil
1214   "List of articles that have been forwarded in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-recent nil
1217   "List of articles that have are recent in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-expirable nil
1220   "List of articles in the current newsgroup that can be expired.")
1221
1222 (defvar gnus-newsgroup-processable nil
1223   "List of articles in the current newsgroup that can be processed.")
1224
1225 (defvar gnus-newsgroup-downloadable nil
1226   "List of articles in the current newsgroup that can be processed.")
1227
1228 (defvar gnus-newsgroup-undownloaded nil
1229   "List of articles in the current newsgroup that haven't been downloaded..")
1230
1231 (defvar gnus-newsgroup-unsendable nil
1232   "List of articles in the current newsgroup that won't be sent.")
1233
1234 (defvar gnus-newsgroup-bookmarks nil
1235   "List of articles in the current newsgroup that have bookmarks.")
1236
1237 (defvar gnus-newsgroup-dormant nil
1238   "List of dormant articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-unseen nil
1241   "List of unseen articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-seen nil
1244   "Range of seen articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-articles nil
1247   "List of articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-scored nil
1250   "List of scored articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-incorporated nil
1253   "List of incorporated articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-headers nil
1256   "List of article headers in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-threads nil)
1259
1260 (defvar gnus-newsgroup-prepared nil
1261   "Whether the current group has been prepared properly.")
1262
1263 (defvar gnus-newsgroup-ancient nil
1264   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-sparse nil)
1267
1268 (defvar gnus-current-article nil)
1269 (defvar gnus-article-current nil)
1270 (defvar gnus-current-headers nil)
1271 (defvar gnus-have-all-headers nil)
1272 (defvar gnus-last-article nil)
1273 (defvar gnus-newsgroup-history nil)
1274 (defvar gnus-newsgroup-charset nil)
1275 (defvar gnus-newsgroup-ephemeral-charset nil)
1276 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1277
1278 (defvar gnus-article-before-search nil)
1279
1280 (defconst gnus-summary-local-variables
1281   '(gnus-newsgroup-name
1282     gnus-newsgroup-begin gnus-newsgroup-end
1283     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1284     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1285     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1286     gnus-newsgroup-unselected gnus-newsgroup-marked
1287     gnus-newsgroup-reads gnus-newsgroup-saved
1288     gnus-newsgroup-replied gnus-newsgroup-forwarded
1289     gnus-newsgroup-recent
1290     gnus-newsgroup-expirable
1291     gnus-newsgroup-processable gnus-newsgroup-killed
1292     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1293     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1294     gnus-newsgroup-seen gnus-newsgroup-articles
1295     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1296     gnus-newsgroup-headers gnus-newsgroup-threads
1297     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1298     gnus-current-article gnus-current-headers gnus-have-all-headers
1299     gnus-last-article gnus-article-internal-prepare-hook
1300     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1301     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1302     gnus-thread-expunge-below
1303     gnus-score-alist gnus-current-score-file
1304     (gnus-summary-expunge-below . global)
1305     (gnus-summary-mark-below . global)
1306     (gnus-orphan-score . global)
1307     gnus-newsgroup-active gnus-scores-exclude-files
1308     gnus-newsgroup-history gnus-newsgroup-ancient
1309     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1310     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1311     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1312     (gnus-newsgroup-expunged-tally . 0)
1313     gnus-cache-removable-articles gnus-newsgroup-cached
1314     gnus-newsgroup-data gnus-newsgroup-data-reverse
1315     gnus-newsgroup-limit gnus-newsgroup-limits
1316     gnus-newsgroup-charset gnus-newsgroup-display
1317     gnus-newsgroup-incorporated)
1318   "Variables that are buffer-local to the summary buffers.")
1319
1320 (defvar gnus-newsgroup-variables nil
1321   "Variables that have separate values in the newsgroups.")
1322
1323 ;; Byte-compiler warning.
1324 (eval-when-compile (defvar gnus-article-mode-map))
1325
1326 ;; Subject simplification.
1327
1328 (defun gnus-simplify-whitespace (str)
1329   "Remove excessive whitespace from STR."
1330   (let ((mystr str))
1331     ;; Multiple spaces.
1332     (while (string-match "[ \t][ \t]+" mystr)
1333       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1334                           " "
1335                           (substring mystr (match-end 0)))))
1336     ;; Leading spaces.
1337     (when (string-match "^[ \t]+" mystr)
1338       (setq mystr (substring mystr (match-end 0))))
1339     ;; Trailing spaces.
1340     (when (string-match "[ \t]+$" mystr)
1341       (setq mystr (substring mystr 0 (match-beginning 0))))
1342     mystr))
1343
1344 (defsubst gnus-simplify-subject-re (subject)
1345   "Remove \"Re:\" from subject lines."
1346   (if (string-match message-subject-re-regexp subject)
1347       (substring subject (match-end 0))
1348     subject))
1349
1350 (defun gnus-simplify-subject (subject &optional re-only)
1351   "Remove `Re:' and words in parentheses.
1352 If RE-ONLY is non-nil, strip leading `Re:'s only."
1353   (let ((case-fold-search t))           ;Ignore case.
1354     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1355     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1356       (setq subject (substring subject (match-end 0))))
1357     ;; Remove uninteresting prefixes.
1358     (when (and (not re-only)
1359                gnus-simplify-ignored-prefixes
1360                (string-match gnus-simplify-ignored-prefixes subject))
1361       (setq subject (substring subject (match-end 0))))
1362     ;; Remove words in parentheses from end.
1363     (unless re-only
1364       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1365         (setq subject (substring subject 0 (match-beginning 0)))))
1366     ;; Return subject string.
1367     subject))
1368
1369 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1370 ;; all whitespace.
1371 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1372   (goto-char (point-min))
1373   (while (re-search-forward regexp nil t)
1374     (replace-match (or newtext ""))))
1375
1376 (defun gnus-simplify-buffer-fuzzy ()
1377   "Simplify string in the buffer fuzzily.
1378 The string in the accessible portion of the current buffer is simplified.
1379 It is assumed to be a single-line subject.
1380 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1381 matter is removed.  Additional things can be deleted by setting
1382 `gnus-simplify-subject-fuzzy-regexp'."
1383   (let ((case-fold-search t)
1384         (modified-tick))
1385     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1386
1387     (while (not (eq modified-tick (buffer-modified-tick)))
1388       (setq modified-tick (buffer-modified-tick))
1389       (cond
1390        ((listp gnus-simplify-subject-fuzzy-regexp)
1391         (mapcar 'gnus-simplify-buffer-fuzzy-step
1392                 gnus-simplify-subject-fuzzy-regexp))
1393        (gnus-simplify-subject-fuzzy-regexp
1394         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1395       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1396       (gnus-simplify-buffer-fuzzy-step
1397        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1398       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1399
1400     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1401     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1402     (gnus-simplify-buffer-fuzzy-step " $")
1403     (gnus-simplify-buffer-fuzzy-step "^ +")))
1404
1405 (defun gnus-simplify-subject-fuzzy (subject)
1406   "Simplify a subject string fuzzily.
1407 See `gnus-simplify-buffer-fuzzy' for details."
1408   (save-excursion
1409     (gnus-set-work-buffer)
1410     (let ((case-fold-search t))
1411       ;; Remove uninteresting prefixes.
1412       (when (and gnus-simplify-ignored-prefixes
1413                  (string-match gnus-simplify-ignored-prefixes subject))
1414         (setq subject (substring subject (match-end 0))))
1415       (insert subject)
1416       (inline (gnus-simplify-buffer-fuzzy))
1417       (buffer-string))))
1418
1419 (defsubst gnus-simplify-subject-fully (subject)
1420   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1421   (cond
1422    (gnus-simplify-subject-functions
1423     (gnus-map-function gnus-simplify-subject-functions subject))
1424    ((null gnus-summary-gather-subject-limit)
1425     (gnus-simplify-subject-re subject))
1426    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1427     (gnus-simplify-subject-fuzzy subject))
1428    ((numberp gnus-summary-gather-subject-limit)
1429     (gnus-limit-string (gnus-simplify-subject-re subject)
1430                        gnus-summary-gather-subject-limit))
1431    (t
1432     subject)))
1433
1434 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1435   "Check whether two subjects are equal.
1436 If optional argument simple-first is t, first argument is already
1437 simplified."
1438   (cond
1439    ((null simple-first)
1440     (equal (gnus-simplify-subject-fully s1)
1441            (gnus-simplify-subject-fully s2)))
1442    (t
1443     (equal s1
1444            (gnus-simplify-subject-fully s2)))))
1445
1446 (defun gnus-summary-bubble-group ()
1447   "Increase the score of the current group.
1448 This is a handy function to add to `gnus-summary-exit-hook' to
1449 increase the score of each group you read."
1450   (gnus-group-add-score gnus-newsgroup-name))
1451
1452 \f
1453 ;;;
1454 ;;; Gnus summary mode
1455 ;;;
1456
1457 (put 'gnus-summary-mode 'mode-class 'special)
1458
1459 (defvar gnus-article-commands-menu)
1460
1461 (when t
1462   ;; Non-orthogonal keys
1463
1464   (gnus-define-keys gnus-summary-mode-map
1465     " " gnus-summary-next-page
1466     "\177" gnus-summary-prev-page
1467     [delete] gnus-summary-prev-page
1468     [backspace] gnus-summary-prev-page
1469     "\r" gnus-summary-scroll-up
1470     "\M-\r" gnus-summary-scroll-down
1471     "n" gnus-summary-next-unread-article
1472     "p" gnus-summary-prev-unread-article
1473     "N" gnus-summary-next-article
1474     "P" gnus-summary-prev-article
1475     "\M-\C-n" gnus-summary-next-same-subject
1476     "\M-\C-p" gnus-summary-prev-same-subject
1477     "\M-n" gnus-summary-next-unread-subject
1478     "\M-p" gnus-summary-prev-unread-subject
1479     "." gnus-summary-first-unread-article
1480     "," gnus-summary-best-unread-article
1481     "\M-s" gnus-summary-search-article-forward
1482     "\M-r" gnus-summary-search-article-backward
1483     "<" gnus-summary-beginning-of-article
1484     ">" gnus-summary-end-of-article
1485     "j" gnus-summary-goto-article
1486     "^" gnus-summary-refer-parent-article
1487     "\M-^" gnus-summary-refer-article
1488     "u" gnus-summary-tick-article-forward
1489     "!" gnus-summary-tick-article-forward
1490     "U" gnus-summary-tick-article-backward
1491     "d" gnus-summary-mark-as-read-forward
1492     "D" gnus-summary-mark-as-read-backward
1493     "E" gnus-summary-mark-as-expirable
1494     "\M-u" gnus-summary-clear-mark-forward
1495     "\M-U" gnus-summary-clear-mark-backward
1496     "k" gnus-summary-kill-same-subject-and-select
1497     "\C-k" gnus-summary-kill-same-subject
1498     "\M-\C-k" gnus-summary-kill-thread
1499     "\M-\C-l" gnus-summary-lower-thread
1500     "e" gnus-summary-edit-article
1501     "#" gnus-summary-mark-as-processable
1502     "\M-#" gnus-summary-unmark-as-processable
1503     "\M-\C-t" gnus-summary-toggle-threads
1504     "\M-\C-s" gnus-summary-show-thread
1505     "\M-\C-h" gnus-summary-hide-thread
1506     "\M-\C-f" gnus-summary-next-thread
1507     "\M-\C-b" gnus-summary-prev-thread
1508     [(meta down)] gnus-summary-next-thread
1509     [(meta up)] gnus-summary-prev-thread
1510     "\M-\C-u" gnus-summary-up-thread
1511     "\M-\C-d" gnus-summary-down-thread
1512     "&" gnus-summary-execute-command
1513     "c" gnus-summary-catchup-and-exit
1514     "\C-w" gnus-summary-mark-region-as-read
1515     "\C-t" gnus-summary-toggle-truncation
1516     "?" gnus-summary-mark-as-dormant
1517     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1518     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1519     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1520     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1521     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1522     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1523     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1524     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1525     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1526     "=" gnus-summary-expand-window
1527     "\C-x\C-s" gnus-summary-reselect-current-group
1528     "\M-g" gnus-summary-rescan-group
1529     "w" gnus-summary-stop-page-breaking
1530     "\C-c\C-r" gnus-summary-caesar-message
1531     "\M-t" gnus-summary-toggle-mime
1532     "f" gnus-summary-followup
1533     "F" gnus-summary-followup-with-original
1534     "C" gnus-summary-cancel-article
1535     "r" gnus-summary-reply
1536     "R" gnus-summary-reply-with-original
1537     "\C-c\C-f" gnus-summary-mail-forward
1538     "o" gnus-summary-save-article
1539     "\C-o" gnus-summary-save-article-mail
1540     "|" gnus-summary-pipe-output
1541     "\M-k" gnus-summary-edit-local-kill
1542     "\M-K" gnus-summary-edit-global-kill
1543     ;; "V" gnus-version
1544     "\C-c\C-d" gnus-summary-describe-group
1545     "q" gnus-summary-exit
1546     "Q" gnus-summary-exit-no-update
1547     "\C-c\C-i" gnus-info-find-node
1548     gnus-mouse-2 gnus-mouse-pick-article
1549     "m" gnus-summary-mail-other-window
1550     "a" gnus-summary-post-news
1551     "x" gnus-summary-limit-to-unread
1552     "s" gnus-summary-isearch-article
1553     "t" gnus-article-toggle-headers
1554     "g" gnus-summary-show-article
1555     "l" gnus-summary-goto-last-article
1556     "v" gnus-summary-preview-mime-message
1557     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1558     "\C-d" gnus-summary-enter-digest-group
1559     "\M-\C-d" gnus-summary-read-document
1560     "\M-\C-e" gnus-summary-edit-parameters
1561     "\M-\C-a" gnus-summary-customize-parameters
1562     "\C-c\C-b" gnus-bug
1563     "*" gnus-cache-enter-article
1564     "\M-*" gnus-cache-remove-article
1565     "\M-&" gnus-summary-universal-argument
1566     "\C-l" gnus-recenter
1567     "I" gnus-summary-increase-score
1568     "L" gnus-summary-lower-score
1569     "\M-i" gnus-symbolic-argument
1570     "h" gnus-summary-select-article-buffer
1571
1572     "V" gnus-summary-score-map
1573     "X" gnus-uu-extract-map
1574     "S" gnus-summary-send-map)
1575
1576   ;; Sort of orthogonal keymap
1577   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1578     "t" gnus-summary-tick-article-forward
1579     "!" gnus-summary-tick-article-forward
1580     "d" gnus-summary-mark-as-read-forward
1581     "r" gnus-summary-mark-as-read-forward
1582     "c" gnus-summary-clear-mark-forward
1583     " " gnus-summary-clear-mark-forward
1584     "e" gnus-summary-mark-as-expirable
1585     "x" gnus-summary-mark-as-expirable
1586     "?" gnus-summary-mark-as-dormant
1587     "b" gnus-summary-set-bookmark
1588     "B" gnus-summary-remove-bookmark
1589     "#" gnus-summary-mark-as-processable
1590     "\M-#" gnus-summary-unmark-as-processable
1591     "S" gnus-summary-limit-include-expunged
1592     "C" gnus-summary-catchup
1593     "H" gnus-summary-catchup-to-here
1594     "h" gnus-summary-catchup-from-here
1595     "\C-c" gnus-summary-catchup-all
1596     "k" gnus-summary-kill-same-subject-and-select
1597     "K" gnus-summary-kill-same-subject
1598     "P" gnus-uu-mark-map)
1599
1600   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1601     "c" gnus-summary-clear-above
1602     "u" gnus-summary-tick-above
1603     "m" gnus-summary-mark-above
1604     "k" gnus-summary-kill-below)
1605
1606   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1607     "/" gnus-summary-limit-to-subject
1608     "n" gnus-summary-limit-to-articles
1609     "w" gnus-summary-pop-limit
1610     "s" gnus-summary-limit-to-subject
1611     "a" gnus-summary-limit-to-author
1612     "u" gnus-summary-limit-to-unread
1613     "m" gnus-summary-limit-to-marks
1614     "M" gnus-summary-limit-exclude-marks
1615     "v" gnus-summary-limit-to-score
1616     "*" gnus-summary-limit-include-cached
1617     "D" gnus-summary-limit-include-dormant
1618     "T" gnus-summary-limit-include-thread
1619     "d" gnus-summary-limit-exclude-dormant
1620     "t" gnus-summary-limit-to-age
1621     "x" gnus-summary-limit-to-extra
1622     "p" gnus-summary-limit-to-display-predicate
1623     "E" gnus-summary-limit-include-expunged
1624     "c" gnus-summary-limit-exclude-childless-dormant
1625     "C" gnus-summary-limit-mark-excluded-as-read
1626     "o" gnus-summary-insert-old-articles
1627     "N" gnus-summary-insert-new-articles)
1628
1629   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1630     "n" gnus-summary-next-unread-article
1631     "p" gnus-summary-prev-unread-article
1632     "N" gnus-summary-next-article
1633     "P" gnus-summary-prev-article
1634     "\C-n" gnus-summary-next-same-subject
1635     "\C-p" gnus-summary-prev-same-subject
1636     "\M-n" gnus-summary-next-unread-subject
1637     "\M-p" gnus-summary-prev-unread-subject
1638     "f" gnus-summary-first-unread-article
1639     "b" gnus-summary-best-unread-article
1640     "j" gnus-summary-goto-article
1641     "g" gnus-summary-goto-subject
1642     "l" gnus-summary-goto-last-article
1643     "o" gnus-summary-pop-article)
1644
1645   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1646     "k" gnus-summary-kill-thread
1647     "l" gnus-summary-lower-thread
1648     "i" gnus-summary-raise-thread
1649     "T" gnus-summary-toggle-threads
1650     "t" gnus-summary-rethread-current
1651     "^" gnus-summary-reparent-thread
1652     "s" gnus-summary-show-thread
1653     "S" gnus-summary-show-all-threads
1654     "h" gnus-summary-hide-thread
1655     "H" gnus-summary-hide-all-threads
1656     "n" gnus-summary-next-thread
1657     "p" gnus-summary-prev-thread
1658     "u" gnus-summary-up-thread
1659     "o" gnus-summary-top-thread
1660     "d" gnus-summary-down-thread
1661     "#" gnus-uu-mark-thread
1662     "\M-#" gnus-uu-unmark-thread)
1663
1664   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1665     "g" gnus-summary-prepare
1666     "c" gnus-summary-insert-cached-articles)
1667
1668   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1669     "c" gnus-summary-catchup-and-exit
1670     "C" gnus-summary-catchup-all-and-exit
1671     "E" gnus-summary-exit-no-update
1672     "J" gnus-summary-jump-to-other-group
1673     "Q" gnus-summary-exit
1674     "Z" gnus-summary-exit
1675     "n" gnus-summary-catchup-and-goto-next-group
1676     "R" gnus-summary-reselect-current-group
1677     "G" gnus-summary-rescan-group
1678     "N" gnus-summary-next-group
1679     "s" gnus-summary-save-newsrc
1680     "P" gnus-summary-prev-group)
1681
1682   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1683     " " gnus-summary-next-page
1684     "n" gnus-summary-next-page
1685     "\177" gnus-summary-prev-page
1686     [delete] gnus-summary-prev-page
1687     "p" gnus-summary-prev-page
1688     "\r" gnus-summary-scroll-up
1689     "\M-\r" gnus-summary-scroll-down
1690     "<" gnus-summary-beginning-of-article
1691     ">" gnus-summary-end-of-article
1692     "b" gnus-summary-beginning-of-article
1693     "e" gnus-summary-end-of-article
1694     "^" gnus-summary-refer-parent-article
1695     "r" gnus-summary-refer-parent-article
1696     "D" gnus-summary-enter-digest-group
1697     "R" gnus-summary-refer-references
1698     "T" gnus-summary-refer-thread
1699     "g" gnus-summary-show-article
1700     "s" gnus-summary-isearch-article
1701     "P" gnus-summary-print-article
1702     "M" gnus-mailing-list-insinuate
1703     "t" gnus-article-babel)
1704
1705   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1706     "b" gnus-article-add-buttons
1707     "B" gnus-article-add-buttons-to-head
1708     "o" gnus-article-treat-overstrike
1709     "e" gnus-article-emphasize
1710     "w" gnus-article-fill-cited-article
1711     "Q" gnus-article-fill-long-lines
1712     "C" gnus-article-capitalize-sentences
1713     "c" gnus-article-remove-cr
1714     "Z" gnus-article-decode-HZ
1715     "f" gnus-article-display-x-face
1716     "l" gnus-summary-stop-page-breaking
1717     "r" gnus-summary-caesar-message
1718     "t" gnus-article-toggle-headers
1719     "v" gnus-summary-verbose-headers
1720     "m" gnus-summary-toggle-mime
1721     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1722     "p" gnus-article-verify-x-pgp-sig
1723     "d" gnus-article-treat-dumbquotes
1724     "s" gnus-smiley-display)
1725
1726   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1727     "a" gnus-article-hide
1728     "h" gnus-article-toggle-headers
1729     "b" gnus-article-hide-boring-headers
1730     "s" gnus-article-hide-signature
1731     "c" gnus-article-hide-citation
1732     "C" gnus-article-hide-citation-in-followups
1733     "l" gnus-article-hide-list-identifiers
1734     "p" gnus-article-hide-pgp
1735     "B" gnus-article-strip-banner
1736     "P" gnus-article-hide-pem
1737     "\C-c" gnus-article-hide-citation-maybe)
1738
1739   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1740     "a" gnus-article-highlight
1741     "h" gnus-article-highlight-headers
1742     "c" gnus-article-highlight-citation
1743     "s" gnus-article-highlight-signature)
1744
1745   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1746     "z" gnus-article-date-ut
1747     "u" gnus-article-date-ut
1748     "l" gnus-article-date-local
1749     "p" gnus-article-date-english
1750     "e" gnus-article-date-lapsed
1751     "o" gnus-article-date-original
1752     "i" gnus-article-date-iso8601
1753     "s" gnus-article-date-user)
1754
1755   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1756     "t" gnus-article-remove-trailing-blank-lines
1757     "l" gnus-article-strip-leading-blank-lines
1758     "m" gnus-article-strip-multiple-blank-lines
1759     "a" gnus-article-strip-blank-lines
1760     "A" gnus-article-strip-all-blank-lines
1761     "s" gnus-article-strip-leading-space
1762     "e" gnus-article-strip-trailing-space
1763     "w" gnus-article-remove-leading-whitespace)
1764
1765   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1766     "v" gnus-version
1767     "f" gnus-summary-fetch-faq
1768     "d" gnus-summary-describe-group
1769     "h" gnus-summary-describe-briefly
1770     "i" gnus-info-find-node)
1771
1772   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1773     "e" gnus-summary-expire-articles
1774     "\M-\C-e" gnus-summary-expire-articles-now
1775     "\177" gnus-summary-delete-article
1776     [delete] gnus-summary-delete-article
1777     [backspace] gnus-summary-delete-article
1778     "m" gnus-summary-move-article
1779     "r" gnus-summary-respool-article
1780     "w" gnus-summary-edit-article
1781     "c" gnus-summary-copy-article
1782     "B" gnus-summary-crosspost-article
1783     "q" gnus-summary-respool-query
1784     "t" gnus-summary-respool-trace
1785     "i" gnus-summary-import-article
1786     "I" gnus-summary-create-article
1787     "p" gnus-summary-article-posted-p)
1788
1789   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1790     "o" gnus-summary-save-article
1791     "m" gnus-summary-save-article-mail
1792     "F" gnus-summary-write-article-file
1793     "r" gnus-summary-save-article-rmail
1794     "f" gnus-summary-save-article-file
1795     "b" gnus-summary-save-article-body-file
1796     "h" gnus-summary-save-article-folder
1797     "v" gnus-summary-save-article-vm
1798     "p" gnus-summary-pipe-output
1799     "s" gnus-soup-add-article)
1800
1801   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1802     "b" gnus-summary-display-buttonized
1803     "m" gnus-summary-repair-multipart
1804     "v" gnus-article-view-part
1805     "o" gnus-article-save-part
1806     "c" gnus-article-copy-part
1807     "C" gnus-article-view-part-as-charset
1808     "e" gnus-article-externalize-part
1809     "E" gnus-article-encrypt-body
1810     "i" gnus-article-inline-part
1811     "|" gnus-article-pipe-part))
1812
1813 (defvar gnus-article-post-menu nil)
1814
1815 (defun gnus-summary-make-menu-bar ()
1816   (gnus-turn-off-edit-menu 'summary)
1817
1818   (unless (boundp 'gnus-summary-misc-menu)
1819
1820     (easy-menu-define
1821      gnus-summary-kill-menu gnus-summary-mode-map ""
1822      (cons
1823       "Score"
1824       (nconc
1825        (list
1826         ["Customize" gnus-score-customize t])
1827        (gnus-make-score-map 'increase)
1828        (gnus-make-score-map 'lower)
1829        '(("Mark"
1830           ["Kill below" gnus-summary-kill-below t]
1831           ["Mark above" gnus-summary-mark-above t]
1832           ["Tick above" gnus-summary-tick-above t]
1833           ["Clear above" gnus-summary-clear-above t])
1834          ["Current score" gnus-summary-current-score t]
1835          ["Set score" gnus-summary-set-score t]
1836          ["Switch current score file..." gnus-score-change-score-file t]
1837          ["Set mark below..." gnus-score-set-mark-below t]
1838          ["Set expunge below..." gnus-score-set-expunge-below t]
1839          ["Edit current score file" gnus-score-edit-current-scores t]
1840          ["Edit score file" gnus-score-edit-file t]
1841          ["Trace score" gnus-score-find-trace t]
1842          ["Find words" gnus-score-find-favourite-words t]
1843          ["Rescore buffer" gnus-summary-rescore t]
1844          ["Increase score..." gnus-summary-increase-score t]
1845          ["Lower score..." gnus-summary-lower-score t]))))
1846
1847     ;; Define both the Article menu in the summary buffer and the equivalent
1848     ;; Commands menu in the article buffer here for consistency.
1849     (let ((innards
1850            `(("Hide"
1851               ["All" gnus-article-hide t]
1852               ["Headers" gnus-article-toggle-headers t]
1853               ["Signature" gnus-article-hide-signature t]
1854               ["Citation" gnus-article-hide-citation t]
1855               ["List identifiers" gnus-article-hide-list-identifiers t]
1856               ["PGP" gnus-article-hide-pgp t]
1857               ["Banner" gnus-article-strip-banner t]
1858               ["Boring headers" gnus-article-hide-boring-headers t])
1859              ("Highlight"
1860               ["All" gnus-article-highlight t]
1861               ["Headers" gnus-article-highlight-headers t]
1862               ["Signature" gnus-article-highlight-signature t]
1863               ["Citation" gnus-article-highlight-citation t])
1864              ("Date"
1865               ["Local" gnus-article-date-local t]
1866               ["ISO8601" gnus-article-date-iso8601 t]
1867               ["UT" gnus-article-date-ut t]
1868               ["Original" gnus-article-date-original t]
1869               ["Lapsed" gnus-article-date-lapsed t]
1870               ["User-defined" gnus-article-date-user t])
1871              ("Washing"
1872               ("Remove Blanks"
1873                ["Leading" gnus-article-strip-leading-blank-lines t]
1874                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1875                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1876                ["All of the above" gnus-article-strip-blank-lines t]
1877                ["All" gnus-article-strip-all-blank-lines t]
1878                ["Leading space" gnus-article-strip-leading-space t]
1879                ["Trailing space" gnus-article-strip-trailing-space t]
1880                ["Leading space in headers"
1881                 gnus-article-remove-leading-whitespace t])
1882               ["Overstrike" gnus-article-treat-overstrike t]
1883               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1884               ["Emphasis" gnus-article-emphasize t]
1885               ["Word wrap" gnus-article-fill-cited-article t]
1886               ["Fill long lines" gnus-article-fill-long-lines t]
1887               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1888               ["CR" gnus-article-remove-cr t]
1889               ["Show X-Face" gnus-article-display-x-face t]
1890               ["Rot 13" gnus-summary-caesar-message
1891                ,@(if (featurep 'xemacs) '(t)
1892                    '(:help "\"Caesar rotate\" article by 13"))]
1893               ["Unix pipe" gnus-summary-pipe-message t]
1894               ["Add buttons" gnus-article-add-buttons t]
1895               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1896               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1897               ["Toggle MIME" gnus-summary-toggle-mime t]
1898               ["Verbose header" gnus-summary-verbose-headers t]
1899               ["Toggle header" gnus-summary-toggle-header t]
1900               ["Toggle smileys" gnus-smiley-display t]
1901               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1902               ["HZ" gnus-article-decode-HZ t])
1903              ("Output"
1904               ["Save in default format" gnus-summary-save-article
1905                ,@(if (featurep 'xemacs) '(t)
1906                    '(:help "Save article using default method"))]
1907               ["Save in file" gnus-summary-save-article-file
1908                ,@(if (featurep 'xemacs) '(t)
1909                    '(:help "Save article in file"))]
1910               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1911               ["Save in MH folder" gnus-summary-save-article-folder t]
1912               ["Save in VM folder" gnus-summary-save-article-vm t]
1913               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1914               ["Save body in file" gnus-summary-save-article-body-file t]
1915               ["Pipe through a filter" gnus-summary-pipe-output t]
1916               ["Add to SOUP packet" gnus-soup-add-article t]
1917               ["Print" gnus-summary-print-article t])
1918              ("Backend"
1919               ["Respool article..." gnus-summary-respool-article t]
1920               ["Move article..." gnus-summary-move-article
1921                (gnus-check-backend-function
1922                 'request-move-article gnus-newsgroup-name)]
1923               ["Copy article..." gnus-summary-copy-article t]
1924               ["Crosspost article..." gnus-summary-crosspost-article
1925                (gnus-check-backend-function
1926                 'request-replace-article gnus-newsgroup-name)]
1927               ["Import file..." gnus-summary-import-article t]
1928               ["Create article..." gnus-summary-create-article t]
1929               ["Check if posted" gnus-summary-article-posted-p t]
1930               ["Edit article" gnus-summary-edit-article
1931                (not (gnus-group-read-only-p))]
1932               ["Delete article" gnus-summary-delete-article
1933                (gnus-check-backend-function
1934                 'request-expire-articles gnus-newsgroup-name)]
1935               ["Query respool" gnus-summary-respool-query t]
1936               ["Trace respool" gnus-summary-respool-trace t]
1937               ["Delete expirable articles" gnus-summary-expire-articles-now
1938                (gnus-check-backend-function
1939                 'request-expire-articles gnus-newsgroup-name)])
1940              ("Extract"
1941               ["Uudecode" gnus-uu-decode-uu
1942                ,@(if (featurep 'xemacs) '(t)
1943                    '(:help "Decode uuencoded article(s)"))]
1944               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1945               ["Unshar" gnus-uu-decode-unshar t]
1946               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1947               ["Save" gnus-uu-decode-save t]
1948               ["Binhex" gnus-uu-decode-binhex t]
1949               ["Postscript" gnus-uu-decode-postscript t])
1950              ("Cache"
1951               ["Enter article" gnus-cache-enter-article t]
1952               ["Remove article" gnus-cache-remove-article t])
1953              ["Translate" gnus-article-babel t]
1954              ["Select article buffer" gnus-summary-select-article-buffer t]
1955              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1956              ["Isearch article..." gnus-summary-isearch-article t]
1957              ["Beginning of the article" gnus-summary-beginning-of-article t]
1958              ["End of the article" gnus-summary-end-of-article t]
1959              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1960              ["Fetch referenced articles" gnus-summary-refer-references t]
1961              ["Fetch current thread" gnus-summary-refer-thread t]
1962              ["Fetch article with id..." gnus-summary-refer-article t]
1963              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1964              ["Redisplay" gnus-summary-show-article t]
1965              ["Raw article" gnus-summary-show-raw-article t])))
1966       (easy-menu-define
1967        gnus-summary-article-menu gnus-summary-mode-map ""
1968        (cons "Article" innards))
1969
1970       (if (not (keymapp gnus-summary-article-menu))
1971           (easy-menu-define
1972            gnus-article-commands-menu gnus-article-mode-map ""
1973            (cons "Commands" innards))
1974         ;; in Emacs, don't share menu.
1975         (setq gnus-article-commands-menu
1976               (copy-keymap gnus-summary-article-menu))
1977         (define-key gnus-article-mode-map [menu-bar commands]
1978           (cons "Commands" gnus-article-commands-menu))))
1979
1980     (easy-menu-define
1981      gnus-summary-thread-menu gnus-summary-mode-map ""
1982      '("Threads"
1983        ["Toggle threading" gnus-summary-toggle-threads t]
1984        ["Hide threads" gnus-summary-hide-all-threads t]
1985        ["Show threads" gnus-summary-show-all-threads t]
1986        ["Hide thread" gnus-summary-hide-thread t]
1987        ["Show thread" gnus-summary-show-thread t]
1988        ["Go to next thread" gnus-summary-next-thread t]
1989        ["Go to previous thread" gnus-summary-prev-thread t]
1990        ["Go down thread" gnus-summary-down-thread t]
1991        ["Go up thread" gnus-summary-up-thread t]
1992        ["Top of thread" gnus-summary-top-thread t]
1993        ["Mark thread as read" gnus-summary-kill-thread t]
1994        ["Lower thread score" gnus-summary-lower-thread t]
1995        ["Raise thread score" gnus-summary-raise-thread t]
1996        ["Rethread current" gnus-summary-rethread-current t]))
1997
1998     (easy-menu-define
1999      gnus-summary-post-menu gnus-summary-mode-map ""
2000      `("Post"
2001        ["Post an article" gnus-summary-post-news
2002         ,@(if (featurep 'xemacs) '(t)
2003             '(:help "Post an article"))]
2004        ["Followup" gnus-summary-followup
2005         ,@(if (featurep 'xemacs) '(t)
2006             '(:help "Post followup to this article"))]
2007        ["Followup and yank" gnus-summary-followup-with-original
2008         ,@(if (featurep 'xemacs) '(t)
2009             '(:help "Post followup to this article, quoting its contents"))]
2010        ["Supersede article" gnus-summary-supersede-article t]
2011        ["Cancel article" gnus-summary-cancel-article
2012         ,@(if (featurep 'xemacs) '(t)
2013             '(:help "Cancel an article you posted"))]
2014        ["Reply" gnus-summary-reply t]
2015        ["Reply and yank" gnus-summary-reply-with-original t]
2016        ["Wide reply" gnus-summary-wide-reply t]
2017        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2018         ,@(if (featurep 'xemacs) '(t)
2019             '(:help "Mail a reply, quoting this article"))]
2020        ["Mail forward" gnus-summary-mail-forward t]
2021        ["Post forward" gnus-summary-post-forward t]
2022        ["Digest and mail" gnus-summary-digest-mail-forward t]
2023        ["Digest and post" gnus-summary-digest-post-forward t]
2024        ["Resend message" gnus-summary-resend-message t]
2025        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2026        ["Send a mail" gnus-summary-mail-other-window t]
2027        ["Uuencode and post" gnus-uu-post-news
2028         ,@(if (featurep 'xemacs) '(t)
2029             '(:help "Post a uuencoded article"))]
2030        ["Followup via news" gnus-summary-followup-to-mail t]
2031        ["Followup via news and yank"
2032         gnus-summary-followup-to-mail-with-original t]
2033        ;;("Draft"
2034        ;;["Send" gnus-summary-send-draft t]
2035        ;;["Send bounced" gnus-resend-bounced-mail t])
2036        ))
2037
2038     (cond
2039      ((not (keymapp gnus-summary-post-menu))
2040       (setq gnus-article-post-menu gnus-summary-post-menu))
2041      ((not gnus-article-post-menu)
2042       ;; Don't share post menu.
2043       (setq gnus-article-post-menu
2044             (copy-keymap gnus-summary-post-menu))))
2045     (define-key gnus-article-mode-map [menu-bar post]
2046       (cons "Post" gnus-article-post-menu))
2047
2048     (easy-menu-define
2049      gnus-summary-misc-menu gnus-summary-mode-map ""
2050      `("Gnus"
2051        ("Mark Read"
2052         ["Mark as read" gnus-summary-mark-as-read-forward t]
2053         ["Mark same subject and select"
2054          gnus-summary-kill-same-subject-and-select t]
2055         ["Mark same subject" gnus-summary-kill-same-subject t]
2056         ["Catchup" gnus-summary-catchup
2057          ,@(if (featurep 'xemacs) '(t)
2058              '(:help "Mark unread articles in this group as read"))]
2059         ["Catchup all" gnus-summary-catchup-all t]
2060         ["Catchup to here" gnus-summary-catchup-to-here t]
2061         ["Catchup from here" gnus-summary-catchup-from-here t]
2062         ["Catchup region" gnus-summary-mark-region-as-read t]
2063         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2064        ("Mark Various"
2065         ["Tick" gnus-summary-tick-article-forward t]
2066         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2067         ["Remove marks" gnus-summary-clear-mark-forward t]
2068         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2069         ["Set bookmark" gnus-summary-set-bookmark t]
2070         ["Remove bookmark" gnus-summary-remove-bookmark t])
2071        ("Mark Limit"
2072         ["Marks..." gnus-summary-limit-to-marks t]
2073         ["Subject..." gnus-summary-limit-to-subject t]
2074         ["Author..." gnus-summary-limit-to-author t]
2075         ["Age..." gnus-summary-limit-to-age t]
2076         ["Extra..." gnus-summary-limit-to-extra t]
2077         ["Score" gnus-summary-limit-to-score t]
2078         ["Score" gnus-summary-limit-to-display-predicate t]
2079         ["Unread" gnus-summary-limit-to-unread t]
2080         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2081         ["Articles" gnus-summary-limit-to-articles t]
2082         ["Pop limit" gnus-summary-pop-limit t]
2083         ["Show dormant" gnus-summary-limit-include-dormant t]
2084         ["Hide childless dormant"
2085          gnus-summary-limit-exclude-childless-dormant t]
2086         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2087         ["Hide marked" gnus-summary-limit-exclude-marks t]
2088         ["Show expunged" gnus-summary-limit-include-expunged t])
2089        ("Process Mark"
2090         ["Set mark" gnus-summary-mark-as-processable t]
2091         ["Remove mark" gnus-summary-unmark-as-processable t]
2092         ["Remove all marks" gnus-summary-unmark-all-processable t]
2093         ["Mark above" gnus-uu-mark-over t]
2094         ["Mark series" gnus-uu-mark-series t]
2095         ["Mark region" gnus-uu-mark-region t]
2096         ["Unmark region" gnus-uu-unmark-region t]
2097         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2098         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2099         ["Mark all" gnus-uu-mark-all t]
2100         ["Mark buffer" gnus-uu-mark-buffer t]
2101         ["Mark sparse" gnus-uu-mark-sparse t]
2102         ["Mark thread" gnus-uu-mark-thread t]
2103         ["Unmark thread" gnus-uu-unmark-thread t]
2104         ("Process Mark Sets"
2105          ["Kill" gnus-summary-kill-process-mark t]
2106          ["Yank" gnus-summary-yank-process-mark
2107           gnus-newsgroup-process-stack]
2108          ["Save" gnus-summary-save-process-mark t]))
2109        ("Scroll article"
2110         ["Page forward" gnus-summary-next-page
2111          ,@(if (featurep 'xemacs) '(t)
2112              '(:help "Show next page of article"))]
2113         ["Page backward" gnus-summary-prev-page
2114          ,@(if (featurep 'xemacs) '(t)
2115              '(:help "Show previous page of article"))]
2116         ["Line forward" gnus-summary-scroll-up t])
2117        ("Move"
2118         ["Next unread article" gnus-summary-next-unread-article t]
2119         ["Previous unread article" gnus-summary-prev-unread-article t]
2120         ["Next article" gnus-summary-next-article t]
2121         ["Previous article" gnus-summary-prev-article t]
2122         ["Next unread subject" gnus-summary-next-unread-subject t]
2123         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2124         ["Next article same subject" gnus-summary-next-same-subject t]
2125         ["Previous article same subject" gnus-summary-prev-same-subject t]
2126         ["First unread article" gnus-summary-first-unread-article t]
2127         ["Best unread article" gnus-summary-best-unread-article t]
2128         ["Go to subject number..." gnus-summary-goto-subject t]
2129         ["Go to article number..." gnus-summary-goto-article t]
2130         ["Go to the last article" gnus-summary-goto-last-article t]
2131         ["Pop article off history" gnus-summary-pop-article t])
2132        ("Sort"
2133         ["Sort by number" gnus-summary-sort-by-number t]
2134         ["Sort by author" gnus-summary-sort-by-author t]
2135         ["Sort by subject" gnus-summary-sort-by-subject t]
2136         ["Sort by date" gnus-summary-sort-by-date t]
2137         ["Sort by score" gnus-summary-sort-by-score t]
2138         ["Sort by lines" gnus-summary-sort-by-lines t]
2139         ["Sort by characters" gnus-summary-sort-by-chars t]
2140         ["Original sort" gnus-summary-sort-by-original t])
2141        ("Help"
2142         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2143         ["Describe group" gnus-summary-describe-group t]
2144         ["Read manual" gnus-info-find-node t])
2145        ("Modes"
2146         ["Pick and read" gnus-pick-mode t]
2147         ["Binary" gnus-binary-mode t])
2148        ("Regeneration"
2149         ["Regenerate" gnus-summary-prepare t]
2150         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2151         ["Toggle threading" gnus-summary-toggle-threads t])
2152        ["See old articles" gnus-summary-insert-old-articles t]
2153        ["See new articles" gnus-summary-insert-new-articles t]
2154        ["Filter articles..." gnus-summary-execute-command t]
2155        ["Run command on subjects..." gnus-summary-universal-argument t]
2156        ["Search articles forward..." gnus-summary-search-article-forward t]
2157        ["Search articles backward..." gnus-summary-search-article-backward t]
2158        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2159        ["Expand window" gnus-summary-expand-window t]
2160        ["Expire expirable articles" gnus-summary-expire-articles
2161         (gnus-check-backend-function
2162          'request-expire-articles gnus-newsgroup-name)]
2163        ["Edit local kill file" gnus-summary-edit-local-kill t]
2164        ["Edit main kill file" gnus-summary-edit-global-kill t]
2165        ["Edit group parameters" gnus-summary-edit-parameters t]
2166        ["Customize group parameters" gnus-summary-customize-parameters t]
2167        ["Send a bug report" gnus-bug t]
2168        ("Exit"
2169         ["Catchup and exit" gnus-summary-catchup-and-exit
2170          ,@(if (featurep 'xemacs) '(t)
2171              '(:help "Mark unread articles in this group as read, then exit"))]
2172         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2173         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2174         ["Exit group" gnus-summary-exit
2175          ,@(if (featurep 'xemacs) '(t)
2176              '(:help "Exit current group, return to group selection mode"))]
2177         ["Exit group without updating" gnus-summary-exit-no-update t]
2178         ["Exit and goto next group" gnus-summary-next-group t]
2179         ["Exit and goto prev group" gnus-summary-prev-group t]
2180         ["Reselect group" gnus-summary-reselect-current-group t]
2181         ["Rescan group" gnus-summary-rescan-group t]
2182         ["Update dribble" gnus-summary-save-newsrc t])))
2183
2184     (gnus-run-hooks 'gnus-summary-menu-hook)))
2185
2186 (defvar gnus-summary-tool-bar-map nil)
2187
2188 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2189 (defun gnus-summary-make-tool-bar ()
2190   (if (and (fboundp 'tool-bar-add-item-from-menu)
2191            (default-value 'tool-bar-mode)
2192            (not gnus-summary-tool-bar-map))
2193       (setq gnus-summary-tool-bar-map
2194             (let ((tool-bar-map (make-sparse-keymap))
2195                   (load-path (mm-image-load-path)))
2196               (tool-bar-add-item-from-menu
2197                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-post-news "post" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-followup "followup" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-reply "reply" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2226               tool-bar-map)))
2227   (if gnus-summary-tool-bar-map
2228       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2229
2230 (defun gnus-score-set-default (var value)
2231   "A version of set that updates the GNU Emacs menu-bar."
2232   (set var value)
2233   ;; It is the message that forces the active status to be updated.
2234   (message ""))
2235
2236 (defun gnus-make-score-map (type)
2237   "Make a summary score map of type TYPE."
2238   (if t
2239       nil
2240     (let ((headers '(("author" "from" string)
2241                      ("subject" "subject" string)
2242                      ("article body" "body" string)
2243                      ("article head" "head" string)
2244                      ("xref" "xref" string)
2245                      ("extra header" "extra" string)
2246                      ("lines" "lines" number)
2247                      ("followups to author" "followup" string)))
2248           (types '((number ("less than" <)
2249                            ("greater than" >)
2250                            ("equal" =))
2251                    (string ("substring" s)
2252                            ("exact string" e)
2253                            ("fuzzy string" f)
2254                            ("regexp" r))))
2255           (perms '(("temporary" (current-time-string))
2256                    ("permanent" nil)
2257                    ("immediate" now)))
2258           header)
2259       (list
2260        (apply
2261         'nconc
2262         (list
2263          (if (eq type 'lower)
2264              "Lower score"
2265            "Increase score"))
2266         (let (outh)
2267           (while headers
2268             (setq header (car headers))
2269             (setq outh
2270                   (cons
2271                    (apply
2272                     'nconc
2273                     (list (car header))
2274                     (let ((ts (cdr (assoc (nth 2 header) types)))
2275                           outt)
2276                       (while ts
2277                         (setq outt
2278                               (cons
2279                                (apply
2280                                 'nconc
2281                                 (list (caar ts))
2282                                 (let ((ps perms)
2283                                       outp)
2284                                   (while ps
2285                                     (setq outp
2286                                           (cons
2287                                            (vector
2288                                             (caar ps)
2289                                             (list
2290                                              'gnus-summary-score-entry
2291                                              (nth 1 header)
2292                                              (if (or (string= (nth 1 header)
2293                                                               "head")
2294                                                      (string= (nth 1 header)
2295                                                               "body"))
2296                                                  ""
2297                                                (list 'gnus-summary-header
2298                                                      (nth 1 header)))
2299                                              (list 'quote (nth 1 (car ts)))
2300                                              (list 'gnus-score-delta-default
2301                                                    nil)
2302                                              (nth 1 (car ps))
2303                                              t)
2304                                             t)
2305                                            outp))
2306                                     (setq ps (cdr ps)))
2307                                   (list (nreverse outp))))
2308                                outt))
2309                         (setq ts (cdr ts)))
2310                       (list (nreverse outt))))
2311                    outh))
2312             (setq headers (cdr headers)))
2313           (list (nreverse outh))))))))
2314
2315 \f
2316
2317 (defun gnus-summary-mode (&optional group)
2318   "Major mode for reading articles.
2319
2320 All normal editing commands are switched off.
2321 \\<gnus-summary-mode-map>
2322 Each line in this buffer represents one article.  To read an
2323 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2324 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2325 respectively.
2326
2327 You can also post articles and send mail from this buffer.  To
2328 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2329 of an article, type `\\[gnus-summary-reply]'.
2330
2331 There are approx. one gazillion commands you can execute in this
2332 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2333
2334 The following commands are available:
2335
2336 \\{gnus-summary-mode-map}"
2337   (interactive)
2338   (kill-all-local-variables)
2339   (when (gnus-visual-p 'summary-menu 'menu)
2340     (gnus-summary-make-menu-bar)
2341     (gnus-summary-make-tool-bar))
2342   (gnus-summary-make-local-variables)
2343   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2344     (gnus-summary-make-local-variables))
2345   (gnus-make-thread-indent-array)
2346   (gnus-simplify-mode-line)
2347   (setq major-mode 'gnus-summary-mode)
2348   (setq mode-name "Summary")
2349   (make-local-variable 'minor-mode-alist)
2350   (use-local-map gnus-summary-mode-map)
2351   (buffer-disable-undo)
2352   (setq buffer-read-only t)             ;Disable modification
2353   (setq truncate-lines t)
2354   (setq selective-display t)
2355   (setq selective-display-ellipses t)   ;Display `...'
2356   (gnus-summary-set-display-table)
2357   (gnus-set-default-directory)
2358   (setq gnus-newsgroup-name group)
2359   (unless (gnus-news-group-p group)
2360     (setq gnus-newsgroup-incorporated
2361           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2362   (make-local-variable 'gnus-summary-line-format)
2363   (make-local-variable 'gnus-summary-line-format-spec)
2364   (make-local-variable 'gnus-summary-dummy-line-format)
2365   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2366   (make-local-variable 'gnus-summary-mark-positions)
2367   (make-local-hook 'pre-command-hook)
2368   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2369   (gnus-run-hooks 'gnus-summary-mode-hook)
2370   (turn-on-gnus-mailing-list-mode)
2371   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2372   (gnus-update-summary-mark-positions))
2373
2374 (defun gnus-summary-make-local-variables ()
2375   "Make all the local summary buffer variables."
2376   (let (global)
2377     (dolist (local gnus-summary-local-variables)
2378       (if (consp local)
2379           (progn
2380             (if (eq (cdr local) 'global)
2381                 ;; Copy the global value of the variable.
2382                 (setq global (symbol-value (car local)))
2383               ;; Use the value from the list.
2384               (setq global (eval (cdr local))))
2385             (set (make-local-variable (car local)) global))
2386         ;; Simple nil-valued local variable.
2387         (set (make-local-variable local) nil)))))
2388
2389 (defun gnus-summary-clear-local-variables ()
2390   (let ((locals gnus-summary-local-variables))
2391     (while locals
2392       (if (consp (car locals))
2393           (and (vectorp (caar locals))
2394                (set (caar locals) nil))
2395         (and (vectorp (car locals))
2396              (set (car locals) nil)))
2397       (setq locals (cdr locals)))))
2398
2399 ;; Summary data functions.
2400
2401 (defmacro gnus-data-number (data)
2402   `(car ,data))
2403
2404 (defmacro gnus-data-set-number (data number)
2405   `(setcar ,data ,number))
2406
2407 (defmacro gnus-data-mark (data)
2408   `(nth 1 ,data))
2409
2410 (defmacro gnus-data-set-mark (data mark)
2411   `(setcar (nthcdr 1 ,data) ,mark))
2412
2413 (defmacro gnus-data-pos (data)
2414   `(nth 2 ,data))
2415
2416 (defmacro gnus-data-set-pos (data pos)
2417   `(setcar (nthcdr 2 ,data) ,pos))
2418
2419 (defmacro gnus-data-header (data)
2420   `(nth 3 ,data))
2421
2422 (defmacro gnus-data-set-header (data header)
2423   `(setcar (nthcdr 3 ,data) ,header))
2424
2425 (defmacro gnus-data-level (data)
2426   `(nth 4 ,data))
2427
2428 (defmacro gnus-data-unread-p (data)
2429   `(= (nth 1 ,data) gnus-unread-mark))
2430
2431 (defmacro gnus-data-read-p (data)
2432   `(/= (nth 1 ,data) gnus-unread-mark))
2433
2434 (defmacro gnus-data-pseudo-p (data)
2435   `(consp (nth 3 ,data)))
2436
2437 (defmacro gnus-data-find (number)
2438   `(assq ,number gnus-newsgroup-data))
2439
2440 (defmacro gnus-data-find-list (number &optional data)
2441   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2442      (memq (assq ,number bdata)
2443            bdata)))
2444
2445 (defmacro gnus-data-make (number mark pos header level)
2446   `(list ,number ,mark ,pos ,header ,level))
2447
2448 (defun gnus-data-enter (after-article number mark pos header level offset)
2449   (let ((data (gnus-data-find-list after-article)))
2450     (unless data
2451       (error "No such article: %d" after-article))
2452     (setcdr data (cons (gnus-data-make number mark pos header level)
2453                        (cdr data)))
2454     (setq gnus-newsgroup-data-reverse nil)
2455     (gnus-data-update-list (cddr data) offset)))
2456
2457 (defun gnus-data-enter-list (after-article list &optional offset)
2458   (when list
2459     (let ((data (and after-article (gnus-data-find-list after-article)))
2460           (ilist list))
2461       (if (not (or data
2462                    after-article))
2463           (let ((odata gnus-newsgroup-data))
2464             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2465             (when offset
2466               (gnus-data-update-list odata offset)))
2467         ;; Find the last element in the list to be spliced into the main
2468         ;; list.
2469         (while (cdr list)
2470           (setq list (cdr list)))
2471         (if (not data)
2472             (progn
2473               (setcdr list gnus-newsgroup-data)
2474               (setq gnus-newsgroup-data ilist)
2475               (when offset
2476                 (gnus-data-update-list (cdr list) offset)))
2477           (setcdr list (cdr data))
2478           (setcdr data ilist)
2479           (when offset
2480             (gnus-data-update-list (cdr list) offset))))
2481       (setq gnus-newsgroup-data-reverse nil))))
2482
2483 (defun gnus-data-remove (article &optional offset)
2484   (let ((data gnus-newsgroup-data))
2485     (if (= (gnus-data-number (car data)) article)
2486         (progn
2487           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2488                 gnus-newsgroup-data-reverse nil)
2489           (when offset
2490             (gnus-data-update-list gnus-newsgroup-data offset)))
2491       (while (cdr data)
2492         (when (= (gnus-data-number (cadr data)) article)
2493           (setcdr data (cddr data))
2494           (when offset
2495             (gnus-data-update-list (cdr data) offset))
2496           (setq data nil
2497                 gnus-newsgroup-data-reverse nil))
2498         (setq data (cdr data))))))
2499
2500 (defmacro gnus-data-list (backward)
2501   `(if ,backward
2502        (or gnus-newsgroup-data-reverse
2503            (setq gnus-newsgroup-data-reverse
2504                  (reverse gnus-newsgroup-data)))
2505      gnus-newsgroup-data))
2506
2507 (defun gnus-data-update-list (data offset)
2508   "Add OFFSET to the POS of all data entries in DATA."
2509   (setq gnus-newsgroup-data-reverse nil)
2510   (while data
2511     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2512     (setq data (cdr data))))
2513
2514 (defun gnus-summary-article-pseudo-p (article)
2515   "Say whether this article is a pseudo article or not."
2516   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2517
2518 (defmacro gnus-summary-article-sparse-p (article)
2519   "Say whether this article is a sparse article or not."
2520   `(memq ,article gnus-newsgroup-sparse))
2521
2522 (defmacro gnus-summary-article-ancient-p (article)
2523   "Say whether this article is a sparse article or not."
2524   `(memq ,article gnus-newsgroup-ancient))
2525
2526 (defun gnus-article-parent-p (number)
2527   "Say whether this article is a parent or not."
2528   (let ((data (gnus-data-find-list number)))
2529     (and (cdr data)                     ; There has to be an article after...
2530          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2531             (gnus-data-level (nth 1 data))))))
2532
2533 (defun gnus-article-children (number)
2534   "Return a list of all children to NUMBER."
2535   (let* ((data (gnus-data-find-list number))
2536          (level (gnus-data-level (car data)))
2537          children)
2538     (setq data (cdr data))
2539     (while (and data
2540                 (= (gnus-data-level (car data)) (1+ level)))
2541       (push (gnus-data-number (car data)) children)
2542       (setq data (cdr data)))
2543     children))
2544
2545 (defmacro gnus-summary-skip-intangible ()
2546   "If the current article is intangible, then jump to a different article."
2547   '(let ((to (get-text-property (point) 'gnus-intangible)))
2548      (and to (gnus-summary-goto-subject to))))
2549
2550 (defmacro gnus-summary-article-intangible-p ()
2551   "Say whether this article is intangible or not."
2552   '(get-text-property (point) 'gnus-intangible))
2553
2554 (defun gnus-article-read-p (article)
2555   "Say whether ARTICLE is read or not."
2556   (not (or (memq article gnus-newsgroup-marked)
2557            (memq article gnus-newsgroup-unreads)
2558            (memq article gnus-newsgroup-unselected)
2559            (memq article gnus-newsgroup-dormant))))
2560
2561 ;; Some summary mode macros.
2562
2563 (defmacro gnus-summary-article-number ()
2564   "The article number of the article on the current line.
2565 If there isn's an article number here, then we return the current
2566 article number."
2567   '(progn
2568      (gnus-summary-skip-intangible)
2569      (or (get-text-property (point) 'gnus-number)
2570          (gnus-summary-last-subject))))
2571
2572 (defmacro gnus-summary-article-header (&optional number)
2573   "Return the header of article NUMBER."
2574   `(gnus-data-header (gnus-data-find
2575                       ,(or number '(gnus-summary-article-number)))))
2576
2577 (defmacro gnus-summary-thread-level (&optional number)
2578   "Return the level of thread that starts with article NUMBER."
2579   `(if (and (eq gnus-summary-make-false-root 'dummy)
2580             (get-text-property (point) 'gnus-intangible))
2581        0
2582      (gnus-data-level (gnus-data-find
2583                        ,(or number '(gnus-summary-article-number))))))
2584
2585 (defmacro gnus-summary-article-mark (&optional number)
2586   "Return the mark of article NUMBER."
2587   `(gnus-data-mark (gnus-data-find
2588                     ,(or number '(gnus-summary-article-number)))))
2589
2590 (defmacro gnus-summary-article-pos (&optional number)
2591   "Return the position of the line of article NUMBER."
2592   `(gnus-data-pos (gnus-data-find
2593                    ,(or number '(gnus-summary-article-number)))))
2594
2595 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2596 (defmacro gnus-summary-article-subject (&optional number)
2597   "Return current subject string or nil if nothing."
2598   `(let ((headers
2599           ,(if number
2600                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2601              '(gnus-data-header (assq (gnus-summary-article-number)
2602                                       gnus-newsgroup-data)))))
2603      (and headers
2604           (vectorp headers)
2605           (mail-header-subject headers))))
2606
2607 (defmacro gnus-summary-article-score (&optional number)
2608   "Return current article score."
2609   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2610                   gnus-newsgroup-scored))
2611        gnus-summary-default-score 0))
2612
2613 (defun gnus-summary-article-children (&optional number)
2614   "Return a list of article numbers that are children of article NUMBER."
2615   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2616          (level (gnus-data-level (car data)))
2617          l children)
2618     (while (and (setq data (cdr data))
2619                 (> (setq l (gnus-data-level (car data))) level))
2620       (and (= (1+ level) l)
2621            (push (gnus-data-number (car data))
2622                  children)))
2623     (nreverse children)))
2624
2625 (defun gnus-summary-article-parent (&optional number)
2626   "Return the article number of the parent of article NUMBER."
2627   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2628                                     (gnus-data-list t)))
2629          (level (gnus-data-level (car data))))
2630     (if (zerop level)
2631         ()                              ; This is a root.
2632       ;; We search until we find an article with a level less than
2633       ;; this one.  That function has to be the parent.
2634       (while (and (setq data (cdr data))
2635                   (not (< (gnus-data-level (car data)) level))))
2636       (and data (gnus-data-number (car data))))))
2637
2638 (defun gnus-unread-mark-p (mark)
2639   "Say whether MARK is the unread mark."
2640   (= mark gnus-unread-mark))
2641
2642 (defun gnus-read-mark-p (mark)
2643   "Say whether MARK is one of the marks that mark as read.
2644 This is all marks except unread, ticked, dormant, and expirable."
2645   (not (or (= mark gnus-unread-mark)
2646            (= mark gnus-ticked-mark)
2647            (= mark gnus-dormant-mark)
2648            (= mark gnus-expirable-mark))))
2649
2650 (defmacro gnus-article-mark (number)
2651   "Return the MARK of article NUMBER.
2652 This macro should only be used when computing the mark the \"first\"
2653 time; i.e., when generating the summary lines.  After that,
2654 `gnus-summary-article-mark' should be used to examine the
2655 marks of articles."
2656   `(cond
2657     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2658     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2659     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2660     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2661     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2662     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2663     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2664     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2665            gnus-ancient-mark))))
2666
2667 ;; Saving hidden threads.
2668
2669 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2670 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2671
2672 (defmacro gnus-save-hidden-threads (&rest forms)
2673   "Save hidden threads, eval FORMS, and restore the hidden threads."
2674   (let ((config (make-symbol "config")))
2675     `(let ((,config (gnus-hidden-threads-configuration)))
2676        (unwind-protect
2677            (save-excursion
2678              ,@forms)
2679          (gnus-restore-hidden-threads-configuration ,config)))))
2680
2681 (defun gnus-data-compute-positions ()
2682   "Compute the positions of all articles."
2683   (setq gnus-newsgroup-data-reverse nil)
2684   (let ((data gnus-newsgroup-data))
2685     (save-excursion
2686       (gnus-save-hidden-threads
2687         (gnus-summary-show-all-threads)
2688         (goto-char (point-min))
2689         (while data
2690           (while (get-text-property (point) 'gnus-intangible)
2691             (forward-line 1))
2692           (gnus-data-set-pos (car data) (+ (point) 3))
2693           (setq data (cdr data))
2694           (forward-line 1))))))
2695
2696 (defun gnus-hidden-threads-configuration ()
2697   "Return the current hidden threads configuration."
2698   (save-excursion
2699     (let (config)
2700       (goto-char (point-min))
2701       (while (search-forward "\r" nil t)
2702         (push (1- (point)) config))
2703       config)))
2704
2705 (defun gnus-restore-hidden-threads-configuration (config)
2706   "Restore hidden threads configuration from CONFIG."
2707   (save-excursion
2708     (let (point buffer-read-only)
2709       (while (setq point (pop config))
2710         (when (and (< point (point-max))
2711                    (goto-char point)
2712                    (eq (char-after) ?\n))
2713           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2714
2715 ;; Various summary mode internalish functions.
2716
2717 (defun gnus-mouse-pick-article (e)
2718   (interactive "e")
2719   (mouse-set-point e)
2720   (gnus-summary-next-page nil t))
2721
2722 (defun gnus-summary-set-display-table ()
2723   "Change the display table.
2724 Odd characters have a tendency to mess
2725 up nicely formatted displays - we make all possible glyphs
2726 display only a single character."
2727
2728   ;; We start from the standard display table, if any.
2729   (let ((table (or (copy-sequence standard-display-table)
2730                    (make-display-table)))
2731         (i 32))
2732     ;; Nix out all the control chars...
2733     (while (>= (setq i (1- i)) 0)
2734       (aset table i [??]))
2735     ;; ... but not newline and cr, of course.  (cr is necessary for the
2736     ;; selective display).
2737     (aset table ?\n nil)
2738     (aset table ?\r nil)
2739     ;; We keep TAB as well.
2740     (aset table ?\t nil)
2741     ;; We nix out any glyphs over 126 that are not set already.
2742     (let ((i 256))
2743       (while (>= (setq i (1- i)) 127)
2744         ;; Only modify if the entry is nil.
2745         (unless (aref table i)
2746           (aset table i [??]))))
2747     (setq buffer-display-table table)))
2748
2749 (defun gnus-summary-set-article-display-arrow (pos)
2750   "Update the overlay arrow to point to line at position POS."
2751   (when (and gnus-summary-display-arrow
2752              (boundp 'overlay-arrow-position)
2753              (boundp 'overlay-arrow-string))
2754     (save-excursion
2755       (goto-char pos)
2756       (beginning-of-line)
2757       (unless overlay-arrow-position
2758         (setq overlay-arrow-position (make-marker)))
2759       (setq overlay-arrow-string "=>"
2760             overlay-arrow-position (set-marker overlay-arrow-position
2761                                                (point)
2762                                                (current-buffer))))))
2763
2764 (defun gnus-summary-buffer-name (group)
2765   "Return the summary buffer name of GROUP."
2766   (concat "*Summary " group "*"))
2767
2768 (defun gnus-summary-setup-buffer (group)
2769   "Initialize summary buffer."
2770   (let ((buffer (gnus-summary-buffer-name group)))
2771     (if (get-buffer buffer)
2772         (progn
2773           (set-buffer buffer)
2774           (setq gnus-summary-buffer (current-buffer))
2775           (not gnus-newsgroup-prepared))
2776       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2777       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2778       (gnus-summary-mode group)
2779       (when gnus-carpal
2780         (gnus-carpal-setup-buffer 'summary))
2781       (unless gnus-single-article-buffer
2782         (make-local-variable 'gnus-article-buffer)
2783         (make-local-variable 'gnus-article-current)
2784         (make-local-variable 'gnus-original-article-buffer))
2785       (setq gnus-newsgroup-name group)
2786       ;; Set any local variables in the group parameters.
2787       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2788       t)))
2789
2790 (defun gnus-set-global-variables ()
2791   "Set the global equivalents of the buffer-local variables.
2792 They are set to the latest values they had.  These reflect the summary
2793 buffer that was in action when the last article was fetched."
2794   (when (eq major-mode 'gnus-summary-mode)
2795     (setq gnus-summary-buffer (current-buffer))
2796     (let ((name gnus-newsgroup-name)
2797           (marked gnus-newsgroup-marked)
2798           (unread gnus-newsgroup-unreads)
2799           (headers gnus-current-headers)
2800           (data gnus-newsgroup-data)
2801           (summary gnus-summary-buffer)
2802           (article-buffer gnus-article-buffer)
2803           (original gnus-original-article-buffer)
2804           (gac gnus-article-current)
2805           (reffed gnus-reffed-article-number)
2806           (score-file gnus-current-score-file)
2807           (default-charset gnus-newsgroup-charset)
2808           vlist)
2809       (let ((locals gnus-newsgroup-variables))
2810         (while locals
2811           (if (consp (car locals))
2812               (push (eval (caar locals)) vlist)
2813             (push (eval (car locals)) vlist))
2814           (setq locals (cdr locals)))
2815         (setq vlist (nreverse vlist)))
2816       (save-excursion
2817         (set-buffer gnus-group-buffer)
2818         (setq gnus-newsgroup-name name
2819               gnus-newsgroup-marked marked
2820               gnus-newsgroup-unreads unread
2821               gnus-current-headers headers
2822               gnus-newsgroup-data data
2823               gnus-article-current gac
2824               gnus-summary-buffer summary
2825               gnus-article-buffer article-buffer
2826               gnus-original-article-buffer original
2827               gnus-reffed-article-number reffed
2828               gnus-current-score-file score-file
2829               gnus-newsgroup-charset default-charset)
2830         (let ((locals gnus-newsgroup-variables))
2831           (while locals
2832             (if (consp (car locals))
2833                 (set (caar locals) (pop vlist))
2834               (set (car locals) (pop vlist)))
2835             (setq locals (cdr locals))))
2836         ;; The article buffer also has local variables.
2837         (when (gnus-buffer-live-p gnus-article-buffer)
2838           (set-buffer gnus-article-buffer)
2839           (setq gnus-summary-buffer summary))))))
2840
2841 (defun gnus-summary-article-unread-p (article)
2842   "Say whether ARTICLE is unread or not."
2843   (memq article gnus-newsgroup-unreads))
2844
2845 (defun gnus-summary-first-article-p (&optional article)
2846   "Return whether ARTICLE is the first article in the buffer."
2847   (if (not (setq article (or article (gnus-summary-article-number))))
2848       nil
2849     (eq article (caar gnus-newsgroup-data))))
2850
2851 (defun gnus-summary-last-article-p (&optional article)
2852   "Return whether ARTICLE is the last article in the buffer."
2853   (if (not (setq article (or article (gnus-summary-article-number))))
2854       ;; All non-existent numbers are the last article.  :-)
2855       t
2856     (not (cdr (gnus-data-find-list article)))))
2857
2858 (defun gnus-make-thread-indent-array ()
2859   (let ((n 200))
2860     (unless (and gnus-thread-indent-array
2861                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2862       (setq gnus-thread-indent-array (make-vector 201 "")
2863             gnus-thread-indent-array-level gnus-thread-indent-level)
2864       (while (>= n 0)
2865         (aset gnus-thread-indent-array n
2866               (make-string (* n gnus-thread-indent-level) ? ))
2867         (setq n (1- n))))))
2868
2869 (defun gnus-update-summary-mark-positions ()
2870   "Compute where the summary marks are to go."
2871   (save-excursion
2872     (when (gnus-buffer-exists-p gnus-summary-buffer)
2873       (set-buffer gnus-summary-buffer))
2874     (let ((gnus-replied-mark 129)
2875           (gnus-score-below-mark 130)
2876           (gnus-score-over-mark 130)
2877           (gnus-download-mark 131)
2878           (spec gnus-summary-line-format-spec)
2879           gnus-visual pos)
2880       (save-excursion
2881         (gnus-set-work-buffer)
2882         (let ((gnus-summary-line-format-spec spec)
2883               (gnus-newsgroup-downloadable '((0 . t))))
2884           (gnus-summary-insert-line
2885            (make-full-mail-header 0 "" "nobody"
2886                                   "05 Apr 2001 23:33:09 +0400"
2887                                   "" "" 0 0 "" nil)
2888            0 nil 128 t nil "" nil 1)
2889           (goto-char (point-min))
2890           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2891                                              (- (point) 2)))))
2892           (goto-char (point-min))
2893           (push (cons 'replied (and (search-forward "\201" nil t)
2894                                     (- (point) 2)))
2895                 pos)
2896           (goto-char (point-min))
2897           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2898                 pos)
2899           (goto-char (point-min))
2900           (push (cons 'download
2901                       (and (search-forward "\203" nil t) (- (point) 2)))
2902                 pos)))
2903       (setq gnus-summary-mark-positions pos))))
2904
2905 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2906   "Insert a dummy root in the summary buffer."
2907   (beginning-of-line)
2908   (gnus-add-text-properties
2909    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2910    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2911
2912 (defun gnus-summary-from-or-to-or-newsgroups (header)
2913   (let ((to (cdr (assq 'To (mail-header-extra header))))
2914         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2915         (default-mime-charset (with-current-buffer gnus-summary-buffer
2916                                 default-mime-charset)))
2917     (cond
2918      ((and to
2919            gnus-ignored-from-addresses
2920            (string-match gnus-ignored-from-addresses
2921                          (mail-header-from header)))
2922       (concat "-> "
2923               (or (car (funcall gnus-extract-address-components
2924                                 (funcall
2925                                  gnus-decode-encoded-word-function to)))
2926                   (funcall gnus-decode-encoded-word-function to))))
2927      ((and newsgroups
2928            gnus-ignored-from-addresses
2929            (string-match gnus-ignored-from-addresses
2930                          (mail-header-from header)))
2931       (concat "=> " newsgroups))
2932      (t
2933       (or (car (funcall gnus-extract-address-components
2934                         (mail-header-from header)))
2935           (mail-header-from header))))))
2936
2937 (defun gnus-summary-insert-line (gnus-tmp-header
2938                                  gnus-tmp-level gnus-tmp-current
2939                                  gnus-tmp-unread gnus-tmp-replied
2940                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2941                                  &optional gnus-tmp-dummy gnus-tmp-score
2942                                  gnus-tmp-process)
2943   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2944          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2945          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2946          (gnus-tmp-score-char
2947           (if (or (null gnus-summary-default-score)
2948                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2949                       gnus-summary-zcore-fuzz))
2950               ?\ ;;;Whitespace
2951             (if (< gnus-tmp-score gnus-summary-default-score)
2952                 gnus-score-below-mark gnus-score-over-mark)))
2953          (gnus-tmp-replied
2954           (cond (gnus-tmp-process gnus-process-mark)
2955                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2956                  gnus-cached-mark)
2957                 (gnus-tmp-replied gnus-replied-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2959                  gnus-saved-mark)
2960                 (t gnus-no-mark)))
2961          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2962          (gnus-tmp-name
2963           (cond
2964            ((string-match "<[^>]+> *$" gnus-tmp-from)
2965             (let ((beg (match-beginning 0)))
2966               (or (and (string-match "^\".+\"" gnus-tmp-from)
2967                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2968                   (substring gnus-tmp-from 0 beg))))
2969            ((string-match "(.+)" gnus-tmp-from)
2970             (substring gnus-tmp-from
2971                        (1+ (match-beginning 0)) (1- (match-end 0))))
2972            (t gnus-tmp-from)))
2973          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2974          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2975          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2976          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2977          (buffer-read-only nil))
2978     (when (string= gnus-tmp-name "")
2979       (setq gnus-tmp-name gnus-tmp-from))
2980     (unless (numberp gnus-tmp-lines)
2981       (setq gnus-tmp-lines -1))
2982     (when (= gnus-tmp-lines -1)
2983       (setq gnus-tmp-lines "?"))
2984     (gnus-put-text-property-excluding-characters-with-faces
2985      (point)
2986      (progn (eval gnus-summary-line-format-spec) (point))
2987      'gnus-number gnus-tmp-number)
2988     (when (gnus-visual-p 'summary-highlight 'highlight)
2989       (forward-line -1)
2990       (gnus-run-hooks 'gnus-summary-update-hook)
2991       (forward-line 1))))
2992
2993 (defun gnus-summary-update-line (&optional dont-update)
2994   "Update summary line after change."
2995   (when (and gnus-summary-default-score
2996              (not gnus-summary-inhibit-highlight))
2997     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2998            (article (gnus-summary-article-number))
2999            (score (gnus-summary-article-score article)))
3000       (unless dont-update
3001         (if (and gnus-summary-mark-below
3002                  (< (gnus-summary-article-score)
3003                     gnus-summary-mark-below))
3004             ;; This article has a low score, so we mark it as read.
3005             (when (memq article gnus-newsgroup-unreads)
3006               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3007           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3008             ;; This article was previously marked as read on account
3009             ;; of a low score, but now it has risen, so we mark it as
3010             ;; unread.
3011             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3012         (gnus-summary-update-mark
3013          (if (or (null gnus-summary-default-score)
3014                  (<= (abs (- score gnus-summary-default-score))
3015                      gnus-summary-zcore-fuzz))
3016              ?\ ;;;Whitespace
3017            (if (< score gnus-summary-default-score)
3018                gnus-score-below-mark gnus-score-over-mark))
3019          'score))
3020       ;; Do visual highlighting.
3021       (when (gnus-visual-p 'summary-highlight 'highlight)
3022         (gnus-run-hooks 'gnus-summary-update-hook)))))
3023
3024 (defvar gnus-tmp-new-adopts nil)
3025
3026 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3027   "Return the number of articles in THREAD.
3028 This may be 0 in some cases -- if none of the articles in
3029 the thread are to be displayed."
3030   (let* ((number
3031           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3032           (cond
3033            ((not (listp thread))
3034             1)
3035            ((and (consp thread) (cdr thread))
3036             (apply
3037              '+ 1 (mapcar
3038                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3039            ((null thread)
3040             1)
3041            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3042             1)
3043            (t 0))))
3044     (when (and level (zerop level) gnus-tmp-new-adopts)
3045       (incf number
3046             (apply '+ (mapcar
3047                        'gnus-summary-number-of-articles-in-thread
3048                        gnus-tmp-new-adopts))))
3049     (if char
3050         (if (> number 1) gnus-not-empty-thread-mark
3051           gnus-empty-thread-mark)
3052       number)))
3053
3054 (defun gnus-summary-set-local-parameters (group)
3055   "Go through the local params of GROUP and set all variable specs in that list."
3056   (let ((params (gnus-group-find-parameter group))
3057         (vars '(quit-config))           ; Ignore quit-config.
3058         elem)
3059     (while params
3060       (setq elem (car params)
3061             params (cdr params))
3062       (and (consp elem)                 ; Has to be a cons.
3063            (consp (cdr elem))           ; The cdr has to be a list.
3064            (symbolp (car elem))         ; Has to be a symbol in there.
3065            (not (memq (car elem) vars))
3066            (ignore-errors               ; So we set it.
3067              (push (car elem) vars)
3068              (make-local-variable (car elem))
3069              (set (car elem) (eval (nth 1 elem))))))))
3070
3071 (defun gnus-summary-read-group (group &optional show-all no-article
3072                                       kill-buffer no-display backward
3073                                       select-articles)
3074   "Start reading news in newsgroup GROUP.
3075 If SHOW-ALL is non-nil, already read articles are also listed.
3076 If NO-ARTICLE is non-nil, no article is selected initially.
3077 If NO-DISPLAY, don't generate a summary buffer."
3078   (let (result)
3079     (while (and group
3080                 (null (setq result
3081                             (let ((gnus-auto-select-next nil))
3082                               (or (gnus-summary-read-group-1
3083                                    group show-all no-article
3084                                    kill-buffer no-display
3085                                    select-articles)
3086                                   (setq show-all nil
3087                                         select-articles nil)))))
3088                 (eq gnus-auto-select-next 'quietly))
3089       (set-buffer gnus-group-buffer)
3090       ;; The entry function called above goes to the next
3091       ;; group automatically, so we go two groups back
3092       ;; if we are searching for the previous group.
3093       (when backward
3094         (gnus-group-prev-unread-group 2))
3095       (if (not (equal group (gnus-group-group-name)))
3096           (setq group (gnus-group-group-name))
3097         (setq group nil)))
3098     result))
3099
3100 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3101   "Directly jump to the other GROUP from summary buffer.
3102 If SHOW-ALL is non-nil, already read articles are also listed."
3103   (interactive
3104    (if (eq gnus-summary-buffer (current-buffer))
3105        (list (completing-read
3106               "Group: " gnus-active-hashtb nil t
3107               (when (and gnus-newsgroup-name
3108                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3109                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3110               'gnus-group-history)
3111              current-prefix-arg)
3112      (error "%s must be invoked from a gnus summary buffer." this-command)))
3113   (unless (or (zerop (length group))
3114               (and gnus-newsgroup-name
3115                    (string-equal gnus-newsgroup-name group)))
3116     (gnus-summary-exit)
3117     (gnus-summary-read-group group show-all
3118                              gnus-dont-select-after-jump-to-other-group)))
3119
3120 (defun gnus-summary-read-group-1 (group show-all no-article
3121                                         kill-buffer no-display
3122                                         &optional select-articles)
3123   ;; Killed foreign groups can't be entered.
3124   ;;  (when (and (not (gnus-group-native-p group))
3125   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3126   ;;    (error "Dead non-native groups can't be entered"))
3127   (gnus-message 5 "Retrieving newsgroup: %s..."
3128                 (gnus-group-decoded-name group))
3129   (let* ((new-group (gnus-summary-setup-buffer group))
3130          (quit-config (gnus-group-quit-config group))
3131          (did-select (and new-group (gnus-select-newsgroup
3132                                      group show-all select-articles))))
3133     (cond
3134      ;; This summary buffer exists already, so we just select it.
3135      ((not new-group)
3136       (gnus-set-global-variables)
3137       (when kill-buffer
3138         (gnus-kill-or-deaden-summary kill-buffer))
3139       (gnus-configure-windows 'summary 'force)
3140       (gnus-set-mode-line 'summary)
3141       (gnus-summary-position-point)
3142       (message "")
3143       t)
3144      ;; We couldn't select this group.
3145      ((null did-select)
3146       (when (and (eq major-mode 'gnus-summary-mode)
3147                  (not (equal (current-buffer) kill-buffer)))
3148         (kill-buffer (current-buffer))
3149         (if (not quit-config)
3150             (progn
3151               ;; Update the info -- marks might need to be removed,
3152               ;; for instance.
3153               (gnus-summary-update-info)
3154               (set-buffer gnus-group-buffer)
3155               (gnus-group-jump-to-group group)
3156               (gnus-group-next-unread-group 1))
3157           (gnus-handle-ephemeral-exit quit-config)))
3158       (let ((grpinfo (gnus-get-info group)))
3159         (if (null (gnus-info-read grpinfo))
3160             (gnus-message 3 "Group %s contains no messages"
3161                           (gnus-group-decoded-name group))
3162           (gnus-message 3 "Can't select group")))
3163       nil)
3164      ;; The user did a `C-g' while prompting for number of articles,
3165      ;; so we exit this group.
3166      ((eq did-select 'quit)
3167       (and (eq major-mode 'gnus-summary-mode)
3168            (not (equal (current-buffer) kill-buffer))
3169            (kill-buffer (current-buffer)))
3170       (when kill-buffer
3171         (gnus-kill-or-deaden-summary kill-buffer))
3172       (if (not quit-config)
3173           (progn
3174             (set-buffer gnus-group-buffer)
3175             (gnus-group-jump-to-group group)
3176             (gnus-group-next-unread-group 1)
3177             (gnus-configure-windows 'group 'force))
3178         (gnus-handle-ephemeral-exit quit-config))
3179       ;; Finally signal the quit.
3180       (signal 'quit nil))
3181      ;; The group was successfully selected.
3182      (t
3183       (gnus-set-global-variables)
3184       ;; Save the active value in effect when the group was entered.
3185       (setq gnus-newsgroup-active
3186             (gnus-copy-sequence
3187              (gnus-active gnus-newsgroup-name)))
3188       ;; You can change the summary buffer in some way with this hook.
3189       (gnus-run-hooks 'gnus-select-group-hook)
3190       (gnus-update-format-specifications
3191        nil 'summary 'summary-mode 'summary-dummy)
3192       (gnus-update-summary-mark-positions)
3193       ;; Do score processing.
3194       (when gnus-use-scoring
3195         (gnus-possibly-score-headers))
3196       ;; Check whether to fill in the gaps in the threads.
3197       (when gnus-build-sparse-threads
3198         (gnus-build-sparse-threads))
3199       ;; Find the initial limit.
3200       (if gnus-show-threads
3201           (if show-all
3202               (let ((gnus-newsgroup-dormant nil))
3203                 (gnus-summary-initial-limit show-all))
3204             (gnus-summary-initial-limit show-all))
3205         ;; When untreaded, all articles are always shown.
3206         (setq gnus-newsgroup-limit
3207               (mapcar
3208                (lambda (header) (mail-header-number header))
3209                gnus-newsgroup-headers)))
3210       ;; Generate the summary buffer.
3211       (unless no-display
3212         (gnus-summary-prepare))
3213       (when gnus-use-trees
3214         (gnus-tree-open group)
3215         (setq gnus-summary-highlight-line-function
3216               'gnus-tree-highlight-article))
3217       ;; If the summary buffer is empty, but there are some low-scored
3218       ;; articles or some excluded dormants, we include these in the
3219       ;; buffer.
3220       (when (and (zerop (buffer-size))
3221                  (not no-display))
3222         (cond (gnus-newsgroup-dormant
3223                (gnus-summary-limit-include-dormant))
3224               ((and gnus-newsgroup-scored show-all)
3225                (gnus-summary-limit-include-expunged t))))
3226       ;; Function `gnus-apply-kill-file' must be called in this hook.
3227       (gnus-run-hooks 'gnus-apply-kill-hook)
3228       (if (and (zerop (buffer-size))
3229                (not no-display))
3230           (progn
3231             ;; This newsgroup is empty.
3232             (gnus-summary-catchup-and-exit nil t)
3233             (gnus-message 6 "No unread news")
3234             (when kill-buffer
3235               (gnus-kill-or-deaden-summary kill-buffer))
3236             ;; Return nil from this function.
3237             nil)
3238         ;; Hide conversation thread subtrees.  We cannot do this in
3239         ;; gnus-summary-prepare-hook since kill processing may not
3240         ;; work with hidden articles.
3241         (and gnus-show-threads
3242              gnus-thread-hide-subtree
3243              (gnus-summary-hide-all-threads))
3244         (when kill-buffer
3245           (gnus-kill-or-deaden-summary kill-buffer))
3246         ;; Show first unread article if requested.
3247         (if (and (not no-article)
3248                  (not no-display)
3249                  gnus-newsgroup-unreads
3250                  gnus-auto-select-first)
3251             (progn
3252               (gnus-configure-windows 'summary)
3253               (cond
3254                ((eq gnus-auto-select-first 'best)
3255                 (gnus-summary-best-unread-article))
3256                ((eq gnus-auto-select-first t)
3257                 (gnus-summary-first-unread-article))
3258                ((gnus-functionp gnus-auto-select-first)
3259                 (funcall gnus-auto-select-first))))
3260           ;; Don't select any articles, just move point to the first
3261           ;; article in the group.
3262           (goto-char (point-min))
3263           (gnus-summary-position-point)
3264           (gnus-configure-windows 'summary 'force)
3265           (gnus-set-mode-line 'summary))
3266         (when (get-buffer-window gnus-group-buffer t)
3267           ;; Gotta use windows, because recenter does weird stuff if
3268           ;; the current buffer ain't the displayed window.
3269           (let ((owin (selected-window)))
3270             (select-window (get-buffer-window gnus-group-buffer t))
3271             (when (gnus-group-goto-group group)
3272               (recenter))
3273             (select-window owin)))
3274         ;; Mark this buffer as "prepared".
3275         (setq gnus-newsgroup-prepared t)
3276         (gnus-run-hooks 'gnus-summary-prepared-hook)
3277         t)))))
3278
3279 (defun gnus-summary-prepare ()
3280   "Generate the summary buffer."
3281   (interactive)
3282   (let ((buffer-read-only nil))
3283     (erase-buffer)
3284     (setq gnus-newsgroup-data nil
3285           gnus-newsgroup-data-reverse nil)
3286     (gnus-run-hooks 'gnus-summary-generate-hook)
3287     ;; Generate the buffer, either with threads or without.
3288     (when gnus-newsgroup-headers
3289       (gnus-summary-prepare-threads
3290        (if gnus-show-threads
3291            (gnus-sort-gathered-threads
3292             (funcall gnus-summary-thread-gathering-function
3293                      (gnus-sort-threads
3294                       (gnus-cut-threads (gnus-make-threads)))))
3295          ;; Unthreaded display.
3296          (gnus-sort-articles gnus-newsgroup-headers))))
3297     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3298     ;; Call hooks for modifying summary buffer.
3299     (goto-char (point-min))
3300     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3301
3302 (defsubst gnus-general-simplify-subject (subject)
3303   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3304   (setq subject
3305         (cond
3306          ;; Truncate the subject.
3307          (gnus-simplify-subject-functions
3308           (gnus-map-function gnus-simplify-subject-functions subject))
3309          ((numberp gnus-summary-gather-subject-limit)
3310           (setq subject (gnus-simplify-subject-re subject))
3311           (if (> (length subject) gnus-summary-gather-subject-limit)
3312               (substring subject 0 gnus-summary-gather-subject-limit)
3313             subject))
3314          ;; Fuzzily simplify it.
3315          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3316           (gnus-simplify-subject-fuzzy subject))
3317          ;; Just remove the leading "Re:".
3318          (t
3319           (gnus-simplify-subject-re subject))))
3320
3321   (if (and gnus-summary-gather-exclude-subject
3322            (string-match gnus-summary-gather-exclude-subject subject))
3323       nil                               ; This article shouldn't be gathered
3324     subject))
3325
3326 (defun gnus-summary-simplify-subject-query ()
3327   "Query where the respool algorithm would put this article."
3328   (interactive)
3329   (gnus-summary-select-article)
3330   (message "%s"
3331            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3332
3333 (defun gnus-gather-threads-by-subject (threads)
3334   "Gather threads by looking at Subject headers."
3335   (if (not gnus-summary-make-false-root)
3336       threads
3337     (let ((hashtb (gnus-make-hashtable 1024))
3338           (prev threads)
3339           (result threads)
3340           subject hthread whole-subject)
3341       (while threads
3342         (setq subject (gnus-general-simplify-subject
3343                        (setq whole-subject (mail-header-subject
3344                                             (caar threads)))))
3345         (when subject
3346           (if (setq hthread (gnus-gethash subject hashtb))
3347               (progn
3348                 ;; We enter a dummy root into the thread, if we
3349                 ;; haven't done that already.
3350                 (unless (stringp (caar hthread))
3351                   (setcar hthread (list whole-subject (car hthread))))
3352                 ;; We add this new gathered thread to this gathered
3353                 ;; thread.
3354                 (setcdr (car hthread)
3355                         (nconc (cdar hthread) (list (car threads))))
3356                 ;; Remove it from the list of threads.
3357                 (setcdr prev (cdr threads))
3358                 (setq threads prev))
3359             ;; Enter this thread into the hash table.
3360             (gnus-sethash subject threads hashtb)))
3361         (setq prev threads)
3362         (setq threads (cdr threads)))
3363       result)))
3364
3365 (defun gnus-gather-threads-by-references (threads)
3366   "Gather threads by looking at References headers."
3367   (let ((idhashtb (gnus-make-hashtable 1024))
3368         (thhashtb (gnus-make-hashtable 1024))
3369         (prev threads)
3370         (result threads)
3371         ids references id gthread gid entered ref)
3372     (while threads
3373       (when (setq references (mail-header-references (caar threads)))
3374         (setq id (mail-header-id (caar threads))
3375               ids (gnus-split-references references)
3376               entered nil)
3377         (while (setq ref (pop ids))
3378           (setq ids (delete ref ids))
3379           (if (not (setq gid (gnus-gethash ref idhashtb)))
3380               (progn
3381                 (gnus-sethash ref id idhashtb)
3382                 (gnus-sethash id threads thhashtb))
3383             (setq gthread (gnus-gethash gid thhashtb))
3384             (unless entered
3385               ;; We enter a dummy root into the thread, if we
3386               ;; haven't done that already.
3387               (unless (stringp (caar gthread))
3388                 (setcar gthread (list (mail-header-subject (caar gthread))
3389                                       (car gthread))))
3390               ;; We add this new gathered thread to this gathered
3391               ;; thread.
3392               (setcdr (car gthread)
3393                       (nconc (cdar gthread) (list (car threads)))))
3394             ;; Add it into the thread hash table.
3395             (gnus-sethash id gthread thhashtb)
3396             (setq entered t)
3397             ;; Remove it from the list of threads.
3398             (setcdr prev (cdr threads))
3399             (setq threads prev))))
3400       (setq prev threads)
3401       (setq threads (cdr threads)))
3402     result))
3403
3404 (defun gnus-sort-gathered-threads (threads)
3405   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3406   (let ((result threads))
3407     (while threads
3408       (when (stringp (caar threads))
3409         (setcdr (car threads)
3410                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3411       (setq threads (cdr threads)))
3412     result))
3413
3414 (defun gnus-thread-loop-p (root thread)
3415   "Say whether ROOT is in THREAD."
3416   (let ((stack (list thread))
3417         (infloop 0)
3418         th)
3419     (while (setq thread (pop stack))
3420       (setq th (cdr thread))
3421       (while (and th
3422                   (not (eq (caar th) root)))
3423         (pop th))
3424       (if th
3425           ;; We have found a loop.
3426           (let (ref-dep)
3427             (setcdr thread (delq (car th) (cdr thread)))
3428             (if (boundp (setq ref-dep (intern "none"
3429                                               gnus-newsgroup-dependencies)))
3430                 (setcdr (symbol-value ref-dep)
3431                         (nconc (cdr (symbol-value ref-dep))
3432                                (list (car th))))
3433               (set ref-dep (list nil (car th))))
3434             (setq infloop 1
3435                   stack nil))
3436         ;; Push all the subthreads onto the stack.
3437         (push (cdr thread) stack)))
3438     infloop))
3439
3440 (defun gnus-make-threads ()
3441   "Go through the dependency hashtb and find the roots.  Return all threads."
3442   (let (threads)
3443     (while (catch 'infloop
3444              (mapatoms
3445               (lambda (refs)
3446                 ;; Deal with self-referencing References loops.
3447                 (when (and (car (symbol-value refs))
3448                            (not (zerop
3449                                  (apply
3450                                   '+
3451                                   (mapcar
3452                                    (lambda (thread)
3453                                      (gnus-thread-loop-p
3454                                       (car (symbol-value refs)) thread))
3455                                    (cdr (symbol-value refs)))))))
3456                   (setq threads nil)
3457                   (throw 'infloop t))
3458                 (unless (car (symbol-value refs))
3459                   ;; These threads do not refer back to any other articles,
3460                   ;; so they're roots.
3461                   (setq threads (append (cdr (symbol-value refs)) threads))))
3462               gnus-newsgroup-dependencies)))
3463     threads))
3464
3465 ;; Build the thread tree.
3466 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3467   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3468
3469 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3470 if it was already present.
3471
3472 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3473 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3474 Message-IDs will be renamed be renamed to a unique Message-ID before
3475 being entered.
3476
3477 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3478   (let* ((id (mail-header-id header))
3479          (id-dep (and id (intern id dependencies)))
3480          ref ref-dep ref-header)
3481     ;; Enter this `header' in the `dependencies' table.
3482     (cond
3483      ((not id-dep)
3484       (setq header nil))
3485      ;; The first two cases do the normal part: enter a new `header'
3486      ;; in the `dependencies' table.
3487      ((not (boundp id-dep))
3488       (set id-dep (list header)))
3489      ((null (car (symbol-value id-dep)))
3490       (setcar (symbol-value id-dep) header))
3491
3492      ;; From here the `header' was already present in the
3493      ;; `dependencies' table.
3494      (force-new
3495       ;; Overrides an existing entry;
3496       ;; just set the header part of the entry.
3497       (setcar (symbol-value id-dep) header))
3498
3499      ;; Renames the existing `header' to a unique Message-ID.
3500      ((not gnus-summary-ignore-duplicates)
3501       ;; An article with this Message-ID has already been seen.
3502       ;; We rename the Message-ID.
3503       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3504            (list header))
3505       (mail-header-set-id header id))
3506
3507      ;; The last case ignores an existing entry, except it adds any
3508      ;; additional Xrefs (in case the two articles came from different
3509      ;; servers.
3510      ;; Also sets `header' to `nil' meaning that the `dependencies'
3511      ;; table was *not* modified.
3512      (t
3513       (mail-header-set-xref
3514        (car (symbol-value id-dep))
3515        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3516                    "")
3517                (or (mail-header-xref header) "")))
3518       (setq header nil)))
3519
3520     (when header
3521       ;; First check if that we are not creating a References loop.
3522       (setq ref (gnus-parent-id (mail-header-references header)))
3523       (while (and ref
3524                   (setq ref-dep (intern-soft ref dependencies))
3525                   (boundp ref-dep)
3526                   (setq ref-header (car (symbol-value ref-dep))))
3527         (if (string= id ref)
3528             ;; Yuk!  This is a reference loop.  Make the article be a
3529             ;; root article.
3530             (progn
3531               (mail-header-set-references (car (symbol-value id-dep)) "none")
3532               (setq ref nil))
3533           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3534       (setq ref (gnus-parent-id (mail-header-references header)))
3535       (setq ref-dep (intern (or ref "none") dependencies))
3536       (if (boundp ref-dep)
3537           (setcdr (symbol-value ref-dep)
3538                   (nconc (cdr (symbol-value ref-dep))
3539                          (list (symbol-value id-dep))))
3540         (set ref-dep (list nil (symbol-value id-dep)))))
3541     header))
3542
3543 (defun gnus-build-sparse-threads ()
3544   (let ((headers gnus-newsgroup-headers)
3545         (mail-parse-charset gnus-newsgroup-charset)
3546         (gnus-summary-ignore-duplicates t)
3547         header references generation relations
3548         subject child end new-child date)
3549     ;; First we create an alist of generations/relations, where
3550     ;; generations is how much we trust the relation, and the relation
3551     ;; is parent/child.
3552     (gnus-message 7 "Making sparse threads...")
3553     (save-excursion
3554       (nnheader-set-temp-buffer " *gnus sparse threads*")
3555       (while (setq header (pop headers))
3556         (when (and (setq references (mail-header-references header))
3557                    (not (string= references "")))
3558           (insert references)
3559           (setq child (mail-header-id header)
3560                 subject (mail-header-subject header)
3561                 date (mail-header-date header)
3562                 generation 0)
3563           (while (search-backward ">" nil t)
3564             (setq end (1+ (point)))
3565             (when (search-backward "<" nil t)
3566               (setq new-child (buffer-substring (point) end))
3567               (push (list (incf generation)
3568                           child (setq child new-child)
3569                           subject date)
3570                     relations)))
3571           (when child
3572             (push (list (1+ generation) child nil subject) relations))
3573           (erase-buffer)))
3574       (kill-buffer (current-buffer)))
3575     ;; Sort over trustworthiness.
3576     (mapcar
3577      (lambda (relation)
3578        (when (gnus-dependencies-add-header
3579               (make-full-mail-header-from-decoded-header
3580                gnus-reffed-article-number
3581                (nth 3 relation) "" (or (nth 4 relation) "")
3582                (nth 1 relation)
3583                (or (nth 2 relation) "") 0 0 "")
3584               gnus-newsgroup-dependencies nil)
3585          (push gnus-reffed-article-number gnus-newsgroup-limit)
3586          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3587          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3588                gnus-newsgroup-reads)
3589          (decf gnus-reffed-article-number)))
3590      (sort relations 'car-less-than-car))
3591     (gnus-message 7 "Making sparse threads...done")))
3592
3593 (defun gnus-build-old-threads ()
3594   ;; Look at all the articles that refer back to old articles, and
3595   ;; fetch the headers for the articles that aren't there.  This will
3596   ;; build complete threads - if the roots haven't been expired by the
3597   ;; server, that is.
3598   (let ((mail-parse-charset gnus-newsgroup-charset)
3599         id heads)
3600     (mapatoms
3601      (lambda (refs)
3602        (when (not (car (symbol-value refs)))
3603          (setq heads (cdr (symbol-value refs)))
3604          (while heads
3605            (if (memq (mail-header-number (caar heads))
3606                      gnus-newsgroup-dormant)
3607                (setq heads (cdr heads))
3608              (setq id (symbol-name refs))
3609              (while (and (setq id (gnus-build-get-header id))
3610                          (not (car (gnus-id-to-thread id)))))
3611              (setq heads nil)))))
3612      gnus-newsgroup-dependencies)))
3613
3614 ;; This function has to be called with point after the article number
3615 ;; on the beginning of the line.
3616 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3617   (let ((eol (gnus-point-at-eol))
3618         (buffer (current-buffer))
3619         header)
3620
3621     ;; overview: [num subject from date id refs chars lines misc]
3622     (unwind-protect
3623         (progn
3624           (narrow-to-region (point) eol)
3625           (unless (eobp)
3626             (forward-char))
3627
3628           (setq header
3629                 (make-full-mail-header
3630                  number                         ; number
3631                  (nnheader-nov-field)           ; subject
3632                  (nnheader-nov-field)           ; from
3633                  (nnheader-nov-field)           ; date
3634                  (nnheader-nov-read-message-id) ; id
3635                  (nnheader-nov-field)           ; refs
3636                  (nnheader-nov-read-integer)    ; chars
3637                  (nnheader-nov-read-integer)    ; lines
3638                  (unless (eobp)
3639                    (if (looking-at "Xref: ")
3640                        (goto-char (match-end 0)))
3641                    (nnheader-nov-field))        ; Xref
3642                  (nnheader-nov-parse-extra))))  ; extra
3643
3644       (widen))
3645
3646     (when gnus-alter-header-function
3647       (funcall gnus-alter-header-function header))
3648     (gnus-dependencies-add-header header dependencies force-new)))
3649
3650 (defun gnus-build-get-header (id)
3651   "Look through the buffer of NOV lines and find the header to ID.
3652 Enter this line into the dependencies hash table, and return
3653 the id of the parent article (if any)."
3654   (let ((deps gnus-newsgroup-dependencies)
3655         found header)
3656     (prog1
3657         (save-excursion
3658           (set-buffer nntp-server-buffer)
3659           (let ((case-fold-search nil))
3660             (goto-char (point-min))
3661             (while (and (not found)
3662                         (search-forward id nil t))
3663               (beginning-of-line)
3664               (setq found (looking-at
3665                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3666                                    (regexp-quote id))))
3667               (or found (beginning-of-line 2)))
3668             (when found
3669               (beginning-of-line)
3670               (and
3671                (setq header (gnus-nov-parse-line
3672                              (read (current-buffer)) deps))
3673                (gnus-parent-id (mail-header-references header))))))
3674       (when header
3675         (let ((number (mail-header-number header)))
3676           (push number gnus-newsgroup-limit)
3677           (push header gnus-newsgroup-headers)
3678           (if (memq number gnus-newsgroup-unselected)
3679               (progn
3680                 (push number gnus-newsgroup-unreads)
3681                 (setq gnus-newsgroup-unselected
3682                       (delq number gnus-newsgroup-unselected)))
3683             (push number gnus-newsgroup-ancient)))))))
3684
3685 (defun gnus-build-all-threads ()
3686   "Read all the headers."
3687   (let ((gnus-summary-ignore-duplicates t)
3688         (mail-parse-charset gnus-newsgroup-charset)
3689         (dependencies gnus-newsgroup-dependencies)
3690         header article)
3691     (save-excursion
3692       (set-buffer nntp-server-buffer)
3693       (let ((case-fold-search nil))
3694         (goto-char (point-min))
3695         (while (not (eobp))
3696           (ignore-errors
3697             (setq article (read (current-buffer))
3698                   header (gnus-nov-parse-line article dependencies)))
3699           (when header
3700             (save-excursion
3701               (set-buffer gnus-summary-buffer)
3702               (push header gnus-newsgroup-headers)
3703               (if (memq (setq article (mail-header-number header))
3704                         gnus-newsgroup-unselected)
3705                   (progn
3706                     (push article gnus-newsgroup-unreads)
3707                     (setq gnus-newsgroup-unselected
3708                           (delq article gnus-newsgroup-unselected)))
3709                 (push article gnus-newsgroup-ancient)))
3710             (forward-line 1)))))))
3711
3712 (defun gnus-summary-update-article-line (article header)
3713   "Update the line for ARTICLE using HEADERS."
3714   (let* ((id (mail-header-id header))
3715          (thread (gnus-id-to-thread id)))
3716     (unless thread
3717       (error "Article in no thread"))
3718     ;; Update the thread.
3719     (setcar thread header)
3720     (gnus-summary-goto-subject article)
3721     (let* ((datal (gnus-data-find-list article))
3722            (data (car datal))
3723            (length (when (cdr datal)
3724                      (- (gnus-data-pos data)
3725                         (gnus-data-pos (cadr datal)))))
3726            (buffer-read-only nil)
3727            (level (gnus-summary-thread-level)))
3728       (gnus-delete-line)
3729       (gnus-summary-insert-line
3730        header level nil (gnus-article-mark article)
3731        (memq article gnus-newsgroup-replied)
3732        (memq article gnus-newsgroup-expirable)
3733        ;; Only insert the Subject string when it's different
3734        ;; from the previous Subject string.
3735        (if (and
3736             gnus-show-threads
3737             (gnus-subject-equal
3738              (condition-case ()
3739                  (mail-header-subject
3740                   (gnus-data-header
3741                    (cadr
3742                     (gnus-data-find-list
3743                      article
3744                      (gnus-data-list t)))))
3745                ;; Error on the side of excessive subjects.
3746                (error ""))
3747              (mail-header-subject header)))
3748            ""
3749          (mail-header-subject header))
3750        nil (cdr (assq article gnus-newsgroup-scored))
3751        (memq article gnus-newsgroup-processable))
3752       (when length
3753         (gnus-data-update-list
3754          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3755
3756 (defun gnus-summary-update-article (article &optional iheader)
3757   "Update ARTICLE in the summary buffer."
3758   (set-buffer gnus-summary-buffer)
3759   (let* ((header (gnus-summary-article-header article))
3760          (id (mail-header-id header))
3761          (data (gnus-data-find article))
3762          (thread (gnus-id-to-thread id))
3763          (references (mail-header-references header))
3764          (parent
3765           (gnus-id-to-thread
3766            (or (gnus-parent-id
3767                 (when (and references
3768                            (not (equal "" references)))
3769                   references))
3770                "none")))
3771          (buffer-read-only nil)
3772          (old (car thread)))
3773     (when thread
3774       (unless iheader
3775         (setcar thread nil)
3776         (when parent
3777           (delq thread parent)))
3778       (if (gnus-summary-insert-subject id header)
3779           ;; Set the (possibly) new article number in the data structure.
3780           (gnus-data-set-number data (gnus-id-to-article id))
3781         (setcar thread old)
3782         nil))))
3783
3784 (defun gnus-rebuild-thread (id &optional line)
3785   "Rebuild the thread containing ID.
3786 If LINE, insert the rebuilt thread starting on line LINE."
3787   (let ((buffer-read-only nil)
3788         old-pos current thread data)
3789     (if (not gnus-show-threads)
3790         (setq thread (list (car (gnus-id-to-thread id))))
3791       ;; Get the thread this article is part of.
3792       (setq thread (gnus-remove-thread id)))
3793     (setq old-pos (gnus-point-at-bol))
3794     (setq current (save-excursion
3795                     (and (re-search-backward "[\r\n]" nil t)
3796                          (gnus-summary-article-number))))
3797     ;; If this is a gathered thread, we have to go some re-gathering.
3798     (when (stringp (car thread))
3799       (let ((subject (car thread))
3800             roots thr)
3801         (setq thread (cdr thread))
3802         (while thread
3803           (unless (memq (setq thr (gnus-id-to-thread
3804                                    (gnus-root-id
3805                                     (mail-header-id (caar thread)))))
3806                         roots)
3807             (push thr roots))
3808           (setq thread (cdr thread)))
3809         ;; We now have all (unique) roots.
3810         (if (= (length roots) 1)
3811             ;; All the loose roots are now one solid root.
3812             (setq thread (car roots))
3813           (setq thread (cons subject (gnus-sort-threads roots))))))
3814     (let (threads)
3815       ;; We then insert this thread into the summary buffer.
3816       (when line
3817         (goto-char (point-min))
3818         (forward-line (1- line)))
3819       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3820         (if gnus-show-threads
3821             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3822           (gnus-summary-prepare-unthreaded thread))
3823         (setq data (nreverse gnus-newsgroup-data))
3824         (setq threads gnus-newsgroup-threads))
3825       ;; We splice the new data into the data structure.
3826       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3827       ;;!!! then we want to insert at the beginning of the buffer.
3828       ;;!!! That happens to be true with Gnus now, but that may
3829       ;;!!! change in the future.  Perhaps.
3830       (gnus-data-enter-list
3831        (if line nil current) data (- (point) old-pos))
3832       (setq gnus-newsgroup-threads
3833             (nconc threads gnus-newsgroup-threads))
3834       (gnus-data-compute-positions))))
3835
3836 (defun gnus-number-to-header (number)
3837   "Return the header for article NUMBER."
3838   (let ((headers gnus-newsgroup-headers))
3839     (while (and headers
3840                 (not (= number (mail-header-number (car headers)))))
3841       (pop headers))
3842     (when headers
3843       (car headers))))
3844
3845 (defun gnus-parent-headers (in-headers &optional generation)
3846   "Return the headers of the GENERATIONeth parent of HEADERS."
3847   (unless generation
3848     (setq generation 1))
3849   (let ((parent t)
3850         (headers in-headers)
3851         references)
3852     (while (and parent
3853                 (not (zerop generation))
3854                 (setq references (mail-header-references headers)))
3855       (setq headers (if (and references
3856                              (setq parent (gnus-parent-id references)))
3857                         (car (gnus-id-to-thread parent))
3858                       nil))
3859       (decf generation))
3860     (and (not (eq headers in-headers))
3861          headers)))
3862
3863 (defun gnus-id-to-thread (id)
3864   "Return the (sub-)thread where ID appears."
3865   (gnus-gethash id gnus-newsgroup-dependencies))
3866
3867 (defun gnus-id-to-article (id)
3868   "Return the article number of ID."
3869   (let ((thread (gnus-id-to-thread id)))
3870     (when (and thread
3871                (car thread))
3872       (mail-header-number (car thread)))))
3873
3874 (defun gnus-id-to-header (id)
3875   "Return the article headers of ID."
3876   (car (gnus-id-to-thread id)))
3877
3878 (defun gnus-article-displayed-root-p (article)
3879   "Say whether ARTICLE is a root(ish) article."
3880   (let ((level (gnus-summary-thread-level article))
3881         (refs (mail-header-references  (gnus-summary-article-header article)))
3882         particle)
3883     (cond
3884      ((null level) nil)
3885      ((zerop level) t)
3886      ((null refs) t)
3887      ((null (gnus-parent-id refs)) t)
3888      ((and (= 1 level)
3889            (null (setq particle (gnus-id-to-article
3890                                  (gnus-parent-id refs))))
3891            (null (gnus-summary-thread-level particle)))))))
3892
3893 (defun gnus-root-id (id)
3894   "Return the id of the root of the thread where ID appears."
3895   (let (last-id prev)
3896     (while (and id (setq prev (car (gnus-id-to-thread id))))
3897       (setq last-id id
3898             id (gnus-parent-id (mail-header-references prev))))
3899     last-id))
3900
3901 (defun gnus-articles-in-thread (thread)
3902   "Return the list of articles in THREAD."
3903   (cons (mail-header-number (car thread))
3904         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3905
3906 (defun gnus-remove-thread (id &optional dont-remove)
3907   "Remove the thread that has ID in it."
3908   (let (headers thread last-id)
3909     ;; First go up in this thread until we find the root.
3910     (setq last-id (gnus-root-id id)
3911           headers (message-flatten-list (gnus-id-to-thread last-id)))
3912     ;; We have now found the real root of this thread.  It might have
3913     ;; been gathered into some loose thread, so we have to search
3914     ;; through the threads to find the thread we wanted.
3915     (let ((threads gnus-newsgroup-threads)
3916           sub)
3917       (while threads
3918         (setq sub (car threads))
3919         (if (stringp (car sub))
3920             ;; This is a gathered thread, so we look at the roots
3921             ;; below it to find whether this article is in this
3922             ;; gathered root.
3923             (progn
3924               (setq sub (cdr sub))
3925               (while sub
3926                 (when (member (caar sub) headers)
3927                   (setq thread (car threads)
3928                         threads nil
3929                         sub nil))
3930                 (setq sub (cdr sub))))
3931           ;; It's an ordinary thread, so we check it.
3932           (when (eq (car sub) (car headers))
3933             (setq thread sub
3934                   threads nil)))
3935         (setq threads (cdr threads)))
3936       ;; If this article is in no thread, then it's a root.
3937       (if thread
3938           (unless dont-remove
3939             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3940         (setq thread (gnus-id-to-thread last-id)))
3941       (when thread
3942         (prog1
3943             thread                      ; We return this thread.
3944           (unless dont-remove
3945             (if (stringp (car thread))
3946                 (progn
3947                   ;; If we use dummy roots, then we have to remove the
3948                   ;; dummy root as well.
3949                   (when (eq gnus-summary-make-false-root 'dummy)
3950                     ;; We go to the dummy root by going to
3951                     ;; the first sub-"thread", and then one line up.
3952                     (gnus-summary-goto-article
3953                      (mail-header-number (caadr thread)))
3954                     (forward-line -1)
3955                     (gnus-delete-line)
3956                     (gnus-data-compute-positions))
3957                   (setq thread (cdr thread))
3958                   (while thread
3959                     (gnus-remove-thread-1 (car thread))
3960                     (setq thread (cdr thread))))
3961               (gnus-remove-thread-1 thread))))))))
3962
3963 (defun gnus-remove-thread-1 (thread)
3964   "Remove the thread THREAD recursively."
3965   (let ((number (mail-header-number (pop thread)))
3966         d)
3967     (setq thread (reverse thread))
3968     (while thread
3969       (gnus-remove-thread-1 (pop thread)))
3970     (when (setq d (gnus-data-find number))
3971       (goto-char (gnus-data-pos d))
3972       (gnus-summary-show-thread)
3973       (gnus-data-remove
3974        number
3975        (- (gnus-point-at-bol)
3976           (prog1
3977               (1+ (gnus-point-at-eol))
3978             (gnus-delete-line)))))))
3979
3980 (defun gnus-sort-threads-1 (threads func)
3981   (sort (mapcar (lambda (thread)
3982                   (cons (car thread)
3983                         (and (cdr thread)
3984                              (gnus-sort-threads-1 (cdr thread) func))))
3985                 threads) func))
3986
3987 (defun gnus-sort-threads (threads)
3988   "Sort THREADS."
3989   (if (not gnus-thread-sort-functions)
3990       threads
3991     (gnus-message 8 "Sorting threads...")
3992     (prog1
3993         (gnus-sort-threads-1
3994          threads
3995          (gnus-make-sort-function gnus-thread-sort-functions))
3996       (gnus-message 8 "Sorting threads...done"))))
3997
3998 (defun gnus-sort-articles (articles)
3999   "Sort ARTICLES."
4000   (when gnus-article-sort-functions
4001     (gnus-message 7 "Sorting articles...")
4002     (prog1
4003         (setq gnus-newsgroup-headers
4004               (sort articles (gnus-make-sort-function
4005                               gnus-article-sort-functions)))
4006       (gnus-message 7 "Sorting articles...done"))))
4007
4008 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4009 (defmacro gnus-thread-header (thread)
4010   "Return header of first article in THREAD.
4011 Note that THREAD must never, ever be anything else than a variable -
4012 using some other form will lead to serious barfage."
4013   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4014   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4015   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4016         (vector thread) 2))
4017
4018 (defsubst gnus-article-sort-by-number (h1 h2)
4019   "Sort articles by article number."
4020   (< (mail-header-number h1)
4021      (mail-header-number h2)))
4022
4023 (defun gnus-thread-sort-by-number (h1 h2)
4024   "Sort threads by root article number."
4025   (gnus-article-sort-by-number
4026    (gnus-thread-header h1) (gnus-thread-header h2)))
4027
4028 (defsubst gnus-article-sort-by-lines (h1 h2)
4029   "Sort articles by article Lines header."
4030   (< (mail-header-lines h1)
4031      (mail-header-lines h2)))
4032
4033 (defun gnus-thread-sort-by-lines (h1 h2)
4034   "Sort threads by root article Lines header."
4035   (gnus-article-sort-by-lines
4036    (gnus-thread-header h1) (gnus-thread-header h2)))
4037
4038 (defsubst gnus-article-sort-by-chars (h1 h2)
4039   "Sort articles by octet length."
4040   (< (mail-header-chars h1)
4041      (mail-header-chars h2)))
4042
4043 (defun gnus-thread-sort-by-chars (h1 h2)
4044   "Sort threads by root article octet length."
4045   (gnus-article-sort-by-chars
4046    (gnus-thread-header h1) (gnus-thread-header h2)))
4047
4048 (defsubst gnus-article-sort-by-author (h1 h2)
4049   "Sort articles by root author."
4050   (string-lessp
4051    (let ((addr (car (mime-entity-read-field h1 'From))))
4052      (or (std11-full-name-string addr)
4053          (std11-address-string addr)
4054          ""))
4055    (let ((addr (car (mime-entity-read-field h2 'From))))
4056      (or (std11-full-name-string addr)
4057          (std11-address-string addr)
4058          ""))
4059    ))
4060
4061 (defun gnus-thread-sort-by-author (h1 h2)
4062   "Sort threads by root author."
4063   (gnus-article-sort-by-author
4064    (gnus-thread-header h1)  (gnus-thread-header h2)))
4065
4066 (defsubst gnus-article-sort-by-subject (h1 h2)
4067   "Sort articles by root subject."
4068   (string-lessp
4069    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4070    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4071
4072 (defun gnus-thread-sort-by-subject (h1 h2)
4073   "Sort threads by root subject."
4074   (gnus-article-sort-by-subject
4075    (gnus-thread-header h1) (gnus-thread-header h2)))
4076
4077 (defsubst gnus-article-sort-by-date (h1 h2)
4078   "Sort articles by root article date."
4079   (time-less-p
4080    (gnus-date-get-time (mail-header-date h1))
4081    (gnus-date-get-time (mail-header-date h2))))
4082
4083 (defun gnus-thread-sort-by-date (h1 h2)
4084   "Sort threads by root article date."
4085   (gnus-article-sort-by-date
4086    (gnus-thread-header h1) (gnus-thread-header h2)))
4087
4088 (defsubst gnus-article-sort-by-score (h1 h2)
4089   "Sort articles by root article score.
4090 Unscored articles will be counted as having a score of zero."
4091   (> (or (cdr (assq (mail-header-number h1)
4092                     gnus-newsgroup-scored))
4093          gnus-summary-default-score 0)
4094      (or (cdr (assq (mail-header-number h2)
4095                     gnus-newsgroup-scored))
4096          gnus-summary-default-score 0)))
4097
4098 (defun gnus-thread-sort-by-score (h1 h2)
4099   "Sort threads by root article score."
4100   (gnus-article-sort-by-score
4101    (gnus-thread-header h1) (gnus-thread-header h2)))
4102
4103 (defun gnus-thread-sort-by-total-score (h1 h2)
4104   "Sort threads by the sum of all scores in the thread.
4105 Unscored articles will be counted as having a score of zero."
4106   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4107
4108 (defun gnus-thread-total-score (thread)
4109   ;; This function find the total score of THREAD.
4110   (cond ((null thread)
4111          0)
4112         ((consp thread)
4113          (if (stringp (car thread))
4114              (apply gnus-thread-score-function 0
4115                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4116            (gnus-thread-total-score-1 thread)))
4117         (t
4118          (gnus-thread-total-score-1 (list thread)))))
4119
4120 (defun gnus-thread-total-score-1 (root)
4121   ;; This function find the total score of the thread below ROOT.
4122   (setq root (car root))
4123   (apply gnus-thread-score-function
4124          (or (append
4125               (mapcar 'gnus-thread-total-score
4126                       (cdr (gnus-id-to-thread (mail-header-id root))))
4127               (when (> (mail-header-number root) 0)
4128                 (list (or (cdr (assq (mail-header-number root)
4129                                      gnus-newsgroup-scored))
4130                           gnus-summary-default-score 0))))
4131              (list gnus-summary-default-score)
4132              '(0))))
4133
4134 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4135 (defvar gnus-tmp-prev-subject nil)
4136 (defvar gnus-tmp-false-parent nil)
4137 (defvar gnus-tmp-root-expunged nil)
4138 (defvar gnus-tmp-dummy-line nil)
4139
4140 (eval-when-compile (defvar gnus-tmp-header))
4141 (defun gnus-extra-header (type &optional header)
4142   "Return the extra header of TYPE."
4143   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4144       ""))
4145
4146 (defvar gnus-tmp-thread-tree-header-string "")
4147
4148 (defvar gnus-sum-thread-tree-root "> "
4149   "With %B spec, used for the root of a thread.
4150 If nil, use subject instead.")
4151 (defvar gnus-sum-thread-tree-single-indent ""
4152   "With %B spec, used for a thread with just one message.
4153 If nil, use subject instead.")
4154 (defvar gnus-sum-thread-tree-vertical "| "
4155   "With %B spec, used for drawing a vertical line.")
4156 (defvar gnus-sum-thread-tree-indent "  "
4157   "With %B spec, used for indenting.")
4158 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4159   "With %B spec, used for a leaf with brothers.")
4160 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4161   "With %B spec, used for a leaf without brothers.")
4162
4163 (defun gnus-summary-prepare-threads (threads)
4164   "Prepare summary buffer from THREADS and indentation LEVEL.
4165 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4166 or a straight list of headers."
4167   (gnus-message 7 "Generating summary...")
4168
4169   (setq gnus-newsgroup-threads threads)
4170   (beginning-of-line)
4171
4172   (let ((gnus-tmp-level 0)
4173         (default-score (or gnus-summary-default-score 0))
4174         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4175         thread number subject stack state gnus-tmp-gathered beg-match
4176         new-roots gnus-tmp-new-adopts thread-end
4177         gnus-tmp-header gnus-tmp-unread
4178         gnus-tmp-replied gnus-tmp-subject-or-nil
4179         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4180         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4181         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4182         tree-stack)
4183
4184     (setq gnus-tmp-prev-subject nil)
4185
4186     (if (vectorp (car threads))
4187         ;; If this is a straight (sic) list of headers, then a
4188         ;; threaded summary display isn't required, so we just create
4189         ;; an unthreaded one.
4190         (gnus-summary-prepare-unthreaded threads)
4191
4192       ;; Do the threaded display.
4193
4194       (while (or threads stack gnus-tmp-new-adopts new-roots)
4195
4196         (if (and (= gnus-tmp-level 0)
4197                  (or (not stack)
4198                      (= (caar stack) 0))
4199                  (not gnus-tmp-false-parent)
4200                  (or gnus-tmp-new-adopts new-roots))
4201             (if gnus-tmp-new-adopts
4202                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4203                       thread (list (car gnus-tmp-new-adopts))
4204                       gnus-tmp-header (caar thread)
4205                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4206               (when new-roots
4207                 (setq thread (list (car new-roots))
4208                       gnus-tmp-header (caar thread)
4209                       new-roots (cdr new-roots))))
4210
4211           (if threads
4212               ;; If there are some threads, we do them before the
4213               ;; threads on the stack.
4214               (setq thread threads
4215                     gnus-tmp-header (caar thread))
4216             ;; There were no current threads, so we pop something off
4217             ;; the stack.
4218             (setq state (car stack)
4219                   gnus-tmp-level (car state)
4220                   tree-stack (cadr state)
4221                   thread (caddr state)
4222                   stack (cdr stack)
4223                   gnus-tmp-header (caar thread))))
4224
4225         (setq gnus-tmp-false-parent nil)
4226         (setq gnus-tmp-root-expunged nil)
4227         (setq thread-end nil)
4228
4229         (if (stringp gnus-tmp-header)
4230             ;; The header is a dummy root.
4231             (cond
4232              ((eq gnus-summary-make-false-root 'adopt)
4233               ;; We let the first article adopt the rest.
4234               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4235                                                (cddar thread)))
4236               (setq gnus-tmp-gathered
4237                     (nconc (mapcar
4238                             (lambda (h) (mail-header-number (car h)))
4239                             (cddar thread))
4240                            gnus-tmp-gathered))
4241               (setq thread (cons (list (caar thread)
4242                                        (cadar thread))
4243                                  (cdr thread)))
4244               (setq gnus-tmp-level -1
4245                     gnus-tmp-false-parent t))
4246              ((eq gnus-summary-make-false-root 'empty)
4247               ;; We print adopted articles with empty subject fields.
4248               (setq gnus-tmp-gathered
4249                     (nconc (mapcar
4250                             (lambda (h) (mail-header-number (car h)))
4251                             (cddar thread))
4252                            gnus-tmp-gathered))
4253               (setq gnus-tmp-level -1))
4254              ((eq gnus-summary-make-false-root 'dummy)
4255               ;; We remember that we probably want to output a dummy
4256               ;; root.
4257               (setq gnus-tmp-dummy-line gnus-tmp-header)
4258               (setq gnus-tmp-prev-subject gnus-tmp-header))
4259              (t
4260               ;; We do not make a root for the gathered
4261               ;; sub-threads at all.
4262               (setq gnus-tmp-level -1)))
4263
4264           (setq number (mail-header-number gnus-tmp-header)
4265                 subject (mail-header-subject gnus-tmp-header))
4266
4267           (cond
4268            ;; If the thread has changed subject, we might want to make
4269            ;; this subthread into a root.
4270            ((and (null gnus-thread-ignore-subject)
4271                  (not (zerop gnus-tmp-level))
4272                  gnus-tmp-prev-subject
4273                  (not (inline
4274                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4275             (setq new-roots (nconc new-roots (list (car thread)))
4276                   thread-end t
4277                   gnus-tmp-header nil))
4278            ;; If the article lies outside the current limit,
4279            ;; then we do not display it.
4280            ((not (memq number gnus-newsgroup-limit))
4281             (setq gnus-tmp-gathered
4282                   (nconc (mapcar
4283                           (lambda (h) (mail-header-number (car h)))
4284                           (cdar thread))
4285                          gnus-tmp-gathered))
4286             (setq gnus-tmp-new-adopts (if (cdar thread)
4287                                           (append gnus-tmp-new-adopts
4288                                                   (cdar thread))
4289                                         gnus-tmp-new-adopts)
4290                   thread-end t
4291                   gnus-tmp-header nil)
4292             (when (zerop gnus-tmp-level)
4293               (setq gnus-tmp-root-expunged t)))
4294            ;; Perhaps this article is to be marked as read?
4295            ((and gnus-summary-mark-below
4296                  (< (or (cdr (assq number gnus-newsgroup-scored))
4297                         default-score)
4298                     gnus-summary-mark-below)
4299                  ;; Don't touch sparse articles.
4300                  (not (gnus-summary-article-sparse-p number))
4301                  (not (gnus-summary-article-ancient-p number)))
4302             (setq gnus-newsgroup-unreads
4303                   (delq number gnus-newsgroup-unreads))
4304             (if gnus-newsgroup-auto-expire
4305                 (push number gnus-newsgroup-expirable)
4306               (push (cons number gnus-low-score-mark)
4307                     gnus-newsgroup-reads))))
4308
4309           (when gnus-tmp-header
4310             ;; We may have an old dummy line to output before this
4311             ;; article.
4312             (when (and gnus-tmp-dummy-line
4313                        (gnus-subject-equal
4314                         gnus-tmp-dummy-line
4315                         (mail-header-subject gnus-tmp-header)))
4316               (gnus-summary-insert-dummy-line
4317                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4318               (setq gnus-tmp-dummy-line nil))
4319
4320             ;; Compute the mark.
4321             (setq gnus-tmp-unread (gnus-article-mark number))
4322
4323             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4324                                   gnus-tmp-header gnus-tmp-level)
4325                   gnus-newsgroup-data)
4326
4327             ;; Actually insert the line.
4328             (setq
4329              gnus-tmp-subject-or-nil
4330              (cond
4331               ((and gnus-thread-ignore-subject
4332                     gnus-tmp-prev-subject
4333                     (not (inline (gnus-subject-equal
4334                                   gnus-tmp-prev-subject subject))))
4335                subject)
4336               ((zerop gnus-tmp-level)
4337                (if (and (eq gnus-summary-make-false-root 'empty)
4338                         (memq number gnus-tmp-gathered)
4339                         gnus-tmp-prev-subject
4340                         (inline (gnus-subject-equal
4341                                  gnus-tmp-prev-subject subject)))
4342                    gnus-summary-same-subject
4343                  subject))
4344               (t gnus-summary-same-subject)))
4345             (if (and (eq gnus-summary-make-false-root 'adopt)
4346                      (= gnus-tmp-level 1)
4347                      (memq number gnus-tmp-gathered))
4348                 (setq gnus-tmp-opening-bracket ?\<
4349                       gnus-tmp-closing-bracket ?\>)
4350               (setq gnus-tmp-opening-bracket ?\[
4351                     gnus-tmp-closing-bracket ?\]))
4352             (setq
4353              gnus-tmp-indentation
4354              (aref gnus-thread-indent-array gnus-tmp-level)
4355              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4356              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4357                                 gnus-summary-default-score 0)
4358              gnus-tmp-score-char
4359              (if (or (null gnus-summary-default-score)
4360                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4361                          gnus-summary-zcore-fuzz))
4362                  ?\ ;;;Whitespace
4363                (if (< gnus-tmp-score gnus-summary-default-score)
4364                    gnus-score-below-mark gnus-score-over-mark))
4365              gnus-tmp-replied
4366              (cond ((memq number gnus-newsgroup-processable)
4367                     gnus-process-mark)
4368                    ((memq number gnus-newsgroup-cached)
4369                     gnus-cached-mark)
4370                    ((memq number gnus-newsgroup-replied)
4371                     gnus-replied-mark)
4372                    ((memq number gnus-newsgroup-forwarded)
4373                     gnus-forwarded-mark)
4374                    ((memq number gnus-newsgroup-saved)
4375                     gnus-saved-mark)
4376                    ((memq number gnus-newsgroup-recent)
4377                     gnus-recent-mark)
4378                    ((memq number gnus-newsgroup-unseen)
4379                     gnus-unseen-mark)
4380                    (t gnus-no-mark))
4381              gnus-tmp-from (mail-header-from gnus-tmp-header)
4382              gnus-tmp-name
4383              (cond
4384               ((string-match "<[^>]+> *$" gnus-tmp-from)
4385                (setq beg-match (match-beginning 0))
4386                (or (and (string-match "^\".+\"" gnus-tmp-from)
4387                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4388                    (substring gnus-tmp-from 0 beg-match)))
4389               ((string-match "(.+)" gnus-tmp-from)
4390                (substring gnus-tmp-from
4391                           (1+ (match-beginning 0)) (1- (match-end 0))))
4392               (t gnus-tmp-from))
4393              gnus-tmp-thread-tree-header-string
4394              (cond
4395               ((not gnus-show-threads) "")
4396               ((zerop gnus-tmp-level)
4397                (if (cdar thread)
4398                    (or gnus-sum-thread-tree-root subject)
4399                  (or gnus-sum-thread-tree-single-indent subject)))
4400               (t
4401                (concat (apply 'concat
4402                               (mapcar (lambda (item)
4403                                         (if (= item 1)
4404                                             gnus-sum-thread-tree-vertical
4405                                           gnus-sum-thread-tree-indent))
4406                                       (cdr (reverse tree-stack))))
4407                        (if (nth 1 thread)
4408                            gnus-sum-thread-tree-leaf-with-other
4409                          gnus-sum-thread-tree-single-leaf)))))
4410             (when (string= gnus-tmp-name "")
4411               (setq gnus-tmp-name gnus-tmp-from))
4412             (unless (numberp gnus-tmp-lines)
4413               (setq gnus-tmp-lines -1))
4414             (when (= gnus-tmp-lines -1)
4415               (setq gnus-tmp-lines "?"))
4416             (gnus-put-text-property
4417              (point)
4418              (progn (eval gnus-summary-line-format-spec) (point))
4419              'gnus-number number)
4420             (when gnus-visual-p
4421               (forward-line -1)
4422               (gnus-run-hooks 'gnus-summary-update-hook)
4423               (forward-line 1))
4424
4425             (setq gnus-tmp-prev-subject subject)))
4426
4427         (when (nth 1 thread)
4428           (push (list (max 0 gnus-tmp-level)
4429                       (copy-list tree-stack)
4430                       (nthcdr 1 thread))
4431                 stack))
4432         (push (if (nth 1 thread) 1 0) tree-stack)
4433         (incf gnus-tmp-level)
4434         (setq threads (if thread-end nil (cdar thread)))
4435         (unless threads
4436           (setq gnus-tmp-level 0)))))
4437   (gnus-message 7 "Generating summary...done"))
4438
4439 (defun gnus-summary-prepare-unthreaded (headers)
4440   "Generate an unthreaded summary buffer based on HEADERS."
4441   (let (header number mark)
4442
4443     (beginning-of-line)
4444
4445     (while headers
4446       ;; We may have to root out some bad articles...
4447       (when (memq (setq number (mail-header-number
4448                                 (setq header (pop headers))))
4449                   gnus-newsgroup-limit)
4450         ;; Mark article as read when it has a low score.
4451         (when (and gnus-summary-mark-below
4452                    (< (or (cdr (assq number gnus-newsgroup-scored))
4453                           gnus-summary-default-score 0)
4454                       gnus-summary-mark-below)
4455                    (not (gnus-summary-article-ancient-p number)))
4456           (setq gnus-newsgroup-unreads
4457                 (delq number gnus-newsgroup-unreads))
4458           (if gnus-newsgroup-auto-expire
4459               (push number gnus-newsgroup-expirable)
4460             (push (cons number gnus-low-score-mark)
4461                   gnus-newsgroup-reads)))
4462
4463         (setq mark (gnus-article-mark number))
4464         (push (gnus-data-make number mark (1+ (point)) header 0)
4465               gnus-newsgroup-data)
4466         (gnus-summary-insert-line
4467          header 0 number
4468          mark (memq number gnus-newsgroup-replied)
4469          (memq number gnus-newsgroup-expirable)
4470          (mail-header-subject header) nil
4471          (cdr (assq number gnus-newsgroup-scored))
4472          (memq number gnus-newsgroup-processable))))))
4473
4474 (defun gnus-summary-remove-list-identifiers ()
4475   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4476   (let ((regexp (if (consp gnus-list-identifiers)
4477                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4478                   gnus-list-identifiers))
4479         changed subject)
4480     (when regexp
4481       (dolist (header gnus-newsgroup-headers)
4482         (setq subject (mail-header-subject header)
4483               changed nil)
4484         (while (string-match
4485                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4486                 subject)
4487           (setq subject
4488                 (concat (substring subject 0 (match-beginning 2))
4489                         (substring subject (match-end 0)))
4490                 changed t))
4491         (when (and changed
4492                    (string-match
4493                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4494           (setq subject
4495                 (concat (substring subject 0 (match-beginning 1))
4496                         (substring subject (match-end 1)))))
4497         (when changed
4498           (mail-header-set-subject header subject))))))
4499
4500 (defun gnus-fetch-headers (articles)
4501   "Fetch headers of ARTICLES."
4502   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4503     (gnus-message 5 "Fetching headers for %s..." name)
4504     (prog1
4505         (if (eq 'nov
4506                 (setq gnus-headers-retrieved-by
4507                       (gnus-retrieve-headers
4508                        articles gnus-newsgroup-name
4509                        ;; We might want to fetch old headers, but
4510                        ;; not if there is only 1 article.
4511                        (and (or (and
4512                                  (not (eq gnus-fetch-old-headers 'some))
4513                                  (not (numberp gnus-fetch-old-headers)))
4514                                 (> (length articles) 1))
4515                             gnus-fetch-old-headers))))
4516             (gnus-get-newsgroup-headers-xover
4517              articles nil nil gnus-newsgroup-name t)
4518           (gnus-get-newsgroup-headers))
4519       (gnus-message 5 "Fetching headers for %s...done" name))))
4520
4521 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4522   "Select newsgroup GROUP.
4523 If READ-ALL is non-nil, all articles in the group are selected.
4524 If SELECT-ARTICLES, only select those articles from GROUP."
4525   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4526          ;;!!! Dirty hack; should be removed.
4527          (gnus-summary-ignore-duplicates
4528           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4529               t
4530             gnus-summary-ignore-duplicates))
4531          (info (nth 2 entry))
4532          articles fetched-articles cached)
4533
4534     (unless (gnus-check-server
4535              (setq gnus-current-select-method
4536                    (gnus-find-method-for-group group)))
4537       (error "Couldn't open server"))
4538
4539     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4540         (gnus-activate-group group)     ; Or we can activate it...
4541         (progn                          ; Or we bug out.
4542           (when (equal major-mode 'gnus-summary-mode)
4543             (kill-buffer (current-buffer)))
4544           (error "Couldn't activate group %s: %s"
4545                  group (gnus-status-message group))))
4546
4547     (unless (gnus-request-group group t)
4548       (when (equal major-mode 'gnus-summary-mode)
4549         (kill-buffer (current-buffer)))
4550       (error "Couldn't request group %s: %s"
4551              group (gnus-status-message group)))
4552
4553     (setq gnus-newsgroup-name group
4554           gnus-newsgroup-unselected nil
4555           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4556
4557     (let ((display (gnus-group-find-parameter group 'display)))
4558       (setq gnus-newsgroup-display
4559             (cond
4560              ((eq display 'all)
4561               'gnus-not-ignore)
4562              ((arrayp display)
4563               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4564              (t
4565               nil))))
4566
4567     (gnus-summary-setup-default-charset)
4568
4569     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4570     (when (gnus-virtual-group-p group)
4571       (setq cached gnus-newsgroup-cached))
4572
4573     (setq gnus-newsgroup-unreads
4574           (gnus-set-difference
4575            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4576            gnus-newsgroup-dormant))
4577
4578     (setq gnus-newsgroup-processable nil)
4579
4580     (gnus-update-read-articles group gnus-newsgroup-unreads)
4581
4582     ;; Adjust and set lists of article marks.
4583     (when info
4584       (gnus-adjust-marked-articles info))
4585
4586     (if (setq articles select-articles)
4587         (setq gnus-newsgroup-unselected
4588               (gnus-sorted-intersection
4589                gnus-newsgroup-unreads
4590                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4591       (setq articles (gnus-articles-to-read group read-all)))
4592
4593     (cond
4594      ((null articles)
4595       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4596       'quit)
4597      ((eq articles 0) nil)
4598      (t
4599       ;; Init the dependencies hash table.
4600       (setq gnus-newsgroup-dependencies
4601             (gnus-make-hashtable (length articles)))
4602       (gnus-set-global-variables)
4603       ;; Retrieve the headers and read them in.
4604       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4605
4606       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4607       (when cached
4608         (setq gnus-newsgroup-cached cached))
4609
4610       ;; Suppress duplicates?
4611       (when gnus-suppress-duplicates
4612         (gnus-dup-suppress-articles))
4613
4614       ;; Set the initial limit.
4615       (setq gnus-newsgroup-limit (copy-sequence articles))
4616       ;; Remove canceled articles from the list of unread articles.
4617       (setq fetched-articles
4618             (mapcar (lambda (headers) (mail-header-number headers))
4619                     gnus-newsgroup-headers))
4620       (setq gnus-newsgroup-articles fetched-articles)
4621       (setq gnus-newsgroup-unreads
4622             (gnus-set-sorted-intersection
4623              gnus-newsgroup-unreads fetched-articles))
4624
4625       (let ((marks (assq 'seen (gnus-info-marks info))))
4626         ;; The `seen' marks are treated specially.
4627         (when (setq gnus-newsgroup-seen (cdr marks))
4628           (dolist (article gnus-newsgroup-articles)
4629             (unless (gnus-member-of-range
4630                      article gnus-newsgroup-seen)
4631               (push article gnus-newsgroup-unseen)))))
4632
4633       ;; Removed marked articles that do not exist.
4634       (gnus-update-missing-marks
4635        (gnus-sorted-complement fetched-articles articles))
4636       ;; We might want to build some more threads first.
4637       (when (and gnus-fetch-old-headers
4638                  (eq gnus-headers-retrieved-by 'nov))
4639         (if (eq gnus-fetch-old-headers 'invisible)
4640             (gnus-build-all-threads)
4641           (gnus-build-old-threads)))
4642       ;; Let the Gnus agent mark articles as read.
4643       (when gnus-agent
4644         (gnus-agent-get-undownloaded-list))
4645       ;; Remove list identifiers from subject
4646       (when gnus-list-identifiers
4647         (gnus-summary-remove-list-identifiers))
4648       ;; Check whether auto-expire is to be done in this group.
4649       (setq gnus-newsgroup-auto-expire
4650             (gnus-group-auto-expirable-p group))
4651       ;; Set up the article buffer now, if necessary.
4652       (unless gnus-single-article-buffer
4653         (gnus-article-setup-buffer))
4654       ;; First and last article in this newsgroup.
4655       (when gnus-newsgroup-headers
4656         (setq gnus-newsgroup-begin
4657               (mail-header-number (car gnus-newsgroup-headers))
4658               gnus-newsgroup-end
4659               (mail-header-number
4660                (gnus-last-element gnus-newsgroup-headers))))
4661       ;; GROUP is successfully selected.
4662       (or gnus-newsgroup-headers t)))))
4663
4664 (defun gnus-summary-display-make-predicate (display)
4665   (require 'gnus-agent)
4666   (when (= (length display) 1)
4667     (setq display (car display)))
4668   (unless gnus-summary-display-cache
4669     (dolist (elem (append (list (cons 'read 'read)
4670                                 (cons 'unseen 'unseen))
4671                           gnus-article-mark-lists))
4672       (push (cons (cdr elem)
4673                   (gnus-byte-compile
4674                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4675             gnus-summary-display-cache)))
4676   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4677     (gnus-get-predicate display)))
4678
4679 ;; Uses the dynamically bound `number' variable.
4680 (defvar number)
4681 (defun gnus-article-marked-p (type &optional article)
4682   (let ((article (or article number)))
4683     (cond
4684      ((eq type 'tick)
4685       (memq article gnus-newsgroup-marked))
4686      ((eq type 'unsend)
4687       (memq article gnus-newsgroup-unsendable))
4688      ((eq type 'undownload)
4689       (memq article gnus-newsgroup-undownloaded))
4690      ((eq type 'download)
4691       (memq article gnus-newsgroup-downloadable))
4692      ((eq type 'unread)
4693       (memq article gnus-newsgroup-unreads))
4694      ((eq type 'read)
4695       (memq article gnus-newsgroup-reads))
4696      ((eq type 'dormant)
4697       (memq article gnus-newsgroup-dormant) )
4698      ((eq type 'expire)
4699       (memq article gnus-newsgroup-expirable))
4700      ((eq type 'reply)
4701       (memq article gnus-newsgroup-replied))
4702      ((eq type 'killed)
4703       (memq article gnus-newsgroup-killed))
4704      ((eq type 'bookmark)
4705       (assq article gnus-newsgroup-bookmarks))
4706      ((eq type 'score)
4707       (assq article gnus-newsgroup-scored))
4708      ((eq type 'save)
4709       (memq article gnus-newsgroup-saved))
4710      ((eq type 'cache)
4711       (memq article gnus-newsgroup-cached))
4712      ((eq type 'forward)
4713       (memq article gnus-newsgroup-forwarded))
4714      ((eq type 'seen)
4715       (not (memq article gnus-newsgroup-unseen)))
4716      ((eq type 'recent)
4717       (memq article gnus-newsgroup-recent))
4718      (t t))))
4719
4720 (defun gnus-articles-to-read (group &optional read-all)
4721   "Find out what articles the user wants to read."
4722   (let* ((articles
4723           ;; Select all articles if `read-all' is non-nil, or if there
4724           ;; are no unread articles.
4725           (if (or read-all
4726                   (and (zerop (length gnus-newsgroup-marked))
4727                        (zerop (length gnus-newsgroup-unreads)))
4728                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4729               ;; We want to select the headers for all the articles in
4730               ;; the group, so we select either all the active
4731               ;; articles in the group, or (if that's nil), the
4732               ;; articles in the cache.
4733               (or
4734                (gnus-uncompress-range (gnus-active group))
4735                (gnus-cache-articles-in-group group))
4736             ;; Select only the "normal" subset of articles.
4737             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4738                           (copy-sequence gnus-newsgroup-unreads))
4739                   '<)))
4740          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4741          (scored (length scored-list))
4742          (number (length articles))
4743          (marked (+ (length gnus-newsgroup-marked)
4744                     (length gnus-newsgroup-dormant)))
4745          (select
4746           (cond
4747            ((numberp read-all)
4748             read-all)
4749            (t
4750             (condition-case ()
4751                 (cond
4752                  ((and (or (<= scored marked) (= scored number))
4753                        (natnump gnus-large-newsgroup)
4754                        (> number gnus-large-newsgroup))
4755                   (let* ((cursor-in-echo-area nil)
4756                          (input
4757                           (read-from-minibuffer
4758                            (format
4759                             "How many articles from %s (max %d): "
4760                             (gnus-limit-string
4761                              (gnus-group-decoded-name gnus-newsgroup-name)
4762                              35)
4763                             number)
4764                            (cons (number-to-string gnus-large-newsgroup)
4765                                  0))))
4766                     (if (string-match "^[ \t]*$" input)
4767                         number
4768                       input)))
4769                  ((and (> scored marked) (< scored number)
4770                        (> (- scored number) 20))
4771                   (let ((input
4772                          (read-string
4773                           (format "%s %s (%d scored, %d total): "
4774                                   "How many articles from"
4775                                   (gnus-group-decoded-name group)
4776                                   scored number))))
4777                     (if (string-match "^[ \t]*$" input)
4778                         number input)))
4779                  (t number))
4780               (quit
4781                (message "Quit getting the articles to read")
4782                nil))))))
4783     (setq select (if (stringp select) (string-to-number select) select))
4784     (if (or (null select) (zerop select))
4785         select
4786       (if (and (not (zerop scored)) (<= (abs select) scored))
4787           (progn
4788             (setq articles (sort scored-list '<))
4789             (setq number (length articles)))
4790         (setq articles (copy-sequence articles)))
4791
4792       (when (< (abs select) number)
4793         (if (< select 0)
4794             ;; Select the N oldest articles.
4795             (setcdr (nthcdr (1- (abs select)) articles) nil)
4796           ;; Select the N most recent articles.
4797           (setq articles (nthcdr (- number select) articles))))
4798       (setq gnus-newsgroup-unselected
4799             (gnus-sorted-intersection
4800              gnus-newsgroup-unreads
4801              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4802       (when gnus-alter-articles-to-read-function
4803         (setq gnus-newsgroup-unreads
4804               (sort
4805                (funcall gnus-alter-articles-to-read-function
4806                         gnus-newsgroup-name gnus-newsgroup-unreads)
4807                '<)))
4808       articles)))
4809
4810 (defun gnus-killed-articles (killed articles)
4811   (let (out)
4812     (while articles
4813       (when (inline (gnus-member-of-range (car articles) killed))
4814         (push (car articles) out))
4815       (setq articles (cdr articles)))
4816     out))
4817
4818 (defun gnus-uncompress-marks (marks)
4819   "Uncompress the mark ranges in MARKS."
4820   (let ((uncompressed '(score bookmark))
4821         out)
4822     (while marks
4823       (if (memq (caar marks) uncompressed)
4824           (push (car marks) out)
4825         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4826       (setq marks (cdr marks)))
4827     out))
4828
4829 (defun gnus-article-mark-to-type (mark)
4830   "Return the type of MARK."
4831   (or (cadr (assq mark gnus-article-special-mark-lists))
4832       'list))
4833
4834 (defun gnus-adjust-marked-articles (info)
4835   "Set all article lists and remove all marks that are no longer valid."
4836   (let* ((marked-lists (gnus-info-marks info))
4837          (active (gnus-active (gnus-info-group info)))
4838          (min (car active))
4839          (max (cdr active))
4840          (types gnus-article-mark-lists)
4841          marks var articles article mark mark-type)
4842
4843     (dolist (marks marked-lists)
4844       (setq mark (car marks)
4845             mark-type (gnus-article-mark-to-type mark)
4846             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4847
4848       ;; We set the variable according to the type of the marks list,
4849       ;; and then adjust the marks to a subset of the active articles.
4850       (cond
4851        ;; Adjust "simple" lists.
4852        ((eq mark-type 'list)
4853         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4854         (when (memq mark '(tick dormant expire reply save))
4855           (while articles
4856             (when (or (< (setq article (pop articles)) min) (> article max))
4857               (set var (delq article (symbol-value var)))))))
4858        ;; Adjust assocs.
4859        ((eq mark-type 'tuple)
4860         (set var (setq articles (cdr marks)))
4861         (when (not (listp (cdr (symbol-value var))))
4862           (set var (list (symbol-value var))))
4863         (when (not (listp (cdr articles)))
4864           (setq articles (list articles)))
4865         (while articles
4866           (when (or (not (consp (setq article (pop articles))))
4867                     (< (car article) min)
4868                     (> (car article) max))
4869             (set var (delq article (symbol-value var))))))
4870        ((eq mark-type 'range)
4871         (cond
4872          ((eq mark 'seen))))))))
4873
4874 (defun gnus-update-missing-marks (missing)
4875   "Go through the list of MISSING articles and remove them from the mark lists."
4876   (when missing
4877     (let (var m)
4878       ;; Go through all types.
4879       (dolist (elem gnus-article-mark-lists)
4880         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4881           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4882           (when (symbol-value var)
4883             ;; This list has articles.  So we delete all missing
4884             ;; articles from it.
4885             (setq m missing)
4886             (while m
4887               (set var (delq (pop m) (symbol-value var))))))))))
4888
4889 (defun gnus-update-marks ()
4890   "Enter the various lists of marked articles into the newsgroup info list."
4891   (let ((types gnus-article-mark-lists)
4892         (info (gnus-get-info gnus-newsgroup-name))
4893         (uncompressed '(score bookmark killed seen))
4894         type list newmarked symbol delta-marks)
4895     (when info
4896       ;; Add all marks lists to the list of marks lists.
4897       (while (setq type (pop types))
4898         (setq list (symbol-value
4899                     (setq symbol
4900                           (intern (format "gnus-newsgroup-%s" (car type))))))
4901
4902         (when list
4903           ;; Get rid of the entries of the articles that have the
4904           ;; default score.
4905           (when (and (eq (cdr type) 'score)
4906                      gnus-save-score
4907                      list)
4908             (let* ((arts list)
4909                    (prev (cons nil list))
4910                    (all prev))
4911               (while arts
4912                 (if (or (not (consp (car arts)))
4913                         (= (cdar arts) gnus-summary-default-score))
4914                     (setcdr prev (cdr arts))
4915                   (setq prev arts))
4916                 (setq arts (cdr arts)))
4917               (setq list (cdr all)))))
4918
4919         (when (eq (cdr type) 'seen)
4920           (setq list
4921                 (if list
4922                     (gnus-add-to-range list gnus-newsgroup-unseen)
4923                   (gnus-compress-sequence gnus-newsgroup-articles))))
4924
4925         (unless (memq (cdr type) uncompressed)
4926           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4927
4928         (when (gnus-check-backend-function
4929                'request-set-mark gnus-newsgroup-name)
4930           ;; propagate flags to server, with the following exceptions:
4931           ;; uncompressed:s are not proper flags (they are cons cells)
4932           ;; cache is a internal gnus flag
4933           ;; download are local to one gnus installation (well)
4934           ;; unsend are for nndraft groups only
4935           ;; xxx: generality of this?  this suits nnimap anyway
4936           (unless (memq (cdr type) (append '(cache download unsend)
4937                                            uncompressed))
4938             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4939                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4940                    (add (gnus-remove-from-range
4941                          (gnus-copy-sequence list) old)))
4942               (when add
4943                 (push (list add 'add (list (cdr type))) delta-marks))
4944               (when del
4945                 (push (list del 'del (list (cdr type))) delta-marks)))))
4946
4947         (when list
4948           (push (cons (cdr type) list) newmarked)))
4949
4950       (when delta-marks
4951         (unless (gnus-check-group gnus-newsgroup-name)
4952           (error "Can't open server for %s" gnus-newsgroup-name))
4953         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4954
4955       ;; Enter these new marks into the info of the group.
4956       (if (nthcdr 3 info)
4957           (setcar (nthcdr 3 info) newmarked)
4958         ;; Add the marks lists to the end of the info.
4959         (when newmarked
4960           (setcdr (nthcdr 2 info) (list newmarked))))
4961
4962       ;; Cut off the end of the info if there's nothing else there.
4963       (let ((i 5))
4964         (while (and (> i 2)
4965                     (not (nth i info)))
4966           (when (nthcdr (decf i) info)
4967             (setcdr (nthcdr i info) nil)))))))
4968
4969 (defun gnus-set-mode-line (where)
4970   "Set the mode line of the article or summary buffers.
4971 If WHERE is `summary', the summary mode line format will be used."
4972   ;; Is this mode line one we keep updated?
4973   (when (and (memq where gnus-updated-mode-lines)
4974              (symbol-value
4975               (intern (format "gnus-%s-mode-line-format-spec" where))))
4976     (let (mode-string)
4977       (save-excursion
4978         ;; We evaluate this in the summary buffer since these
4979         ;; variables are buffer-local to that buffer.
4980         (set-buffer gnus-summary-buffer)
4981         ;; We bind all these variables that are used in the `eval' form
4982         ;; below.
4983         (let* ((mformat (symbol-value
4984                          (intern
4985                           (format "gnus-%s-mode-line-format-spec" where))))
4986                (gnus-tmp-group-name (gnus-group-decoded-name
4987                                      gnus-newsgroup-name))
4988                (gnus-tmp-article-number (or gnus-current-article 0))
4989                (gnus-tmp-unread gnus-newsgroup-unreads)
4990                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4991                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4992                (gnus-tmp-unread-and-unselected
4993                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4994                             (zerop gnus-tmp-unselected))
4995                        "")
4996                       ((zerop gnus-tmp-unselected)
4997                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4998                       (t (format "{%d(+%d) more}"
4999                                  gnus-tmp-unread-and-unticked
5000                                  gnus-tmp-unselected))))
5001                (gnus-tmp-subject
5002                 (if (and gnus-current-headers
5003                          (vectorp gnus-current-headers))
5004                     (gnus-mode-string-quote
5005                      (mail-header-subject gnus-current-headers))
5006                   ""))
5007                bufname-length max-len
5008                gnus-tmp-header);; passed as argument to any user-format-funcs
5009           (setq mode-string (eval mformat))
5010           (setq bufname-length (if (string-match "%b" mode-string)
5011                                    (- (length
5012                                        (buffer-name
5013                                         (if (eq where 'summary)
5014                                             nil
5015                                           (get-buffer gnus-article-buffer))))
5016                                       2)
5017                                  0))
5018           (setq max-len (max 4 (if gnus-mode-non-string-length
5019                                    (- (window-width)
5020                                       gnus-mode-non-string-length
5021                                       bufname-length)
5022                                  (length mode-string))))
5023           ;; We might have to chop a bit of the string off...
5024           (when (> (length mode-string) max-len)
5025             (setq mode-string
5026                   (concat (gnus-truncate-string mode-string (- max-len 3))
5027                           "...")))
5028           ;; Pad the mode string a bit.
5029           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5030       ;; Update the mode line.
5031       (setq mode-line-buffer-identification
5032             (gnus-mode-line-buffer-identification (list mode-string)))
5033       (set-buffer-modified-p t))))
5034
5035 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5036   "Go through the HEADERS list and add all Xrefs to a hash table.
5037 The resulting hash table is returned, or nil if no Xrefs were found."
5038   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5039          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5040          (xref-hashtb (gnus-make-hashtable))
5041          start group entry number xrefs header)
5042     (while headers
5043       (setq header (pop headers))
5044       (when (and (setq xrefs (mail-header-xref header))
5045                  (not (memq (setq number (mail-header-number header))
5046                             unreads)))
5047         (setq start 0)
5048         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5049           (setq start (match-end 0))
5050           (setq group (if prefix
5051                           (concat prefix (substring xrefs (match-beginning 1)
5052                                                     (match-end 1)))
5053                         (substring xrefs (match-beginning 1) (match-end 1))))
5054           (setq number
5055                 (string-to-int (substring xrefs (match-beginning 2)
5056                                           (match-end 2))))
5057           (if (setq entry (gnus-gethash group xref-hashtb))
5058               (setcdr entry (cons number (cdr entry)))
5059             (gnus-sethash group (cons number nil) xref-hashtb)))))
5060     (and start xref-hashtb)))
5061
5062 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5063   "Look through all the headers and mark the Xrefs as read."
5064   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5065         name entry info xref-hashtb idlist method nth4)
5066     (save-excursion
5067       (set-buffer gnus-group-buffer)
5068       (when (setq xref-hashtb
5069                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5070         (mapatoms
5071          (lambda (group)
5072            (unless (string= from-newsgroup (setq name (symbol-name group)))
5073              (setq idlist (symbol-value group))
5074              ;; Dead groups are not updated.
5075              (and (prog1
5076                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5077                             info (nth 2 entry))
5078                     (when (stringp (setq nth4 (gnus-info-method info)))
5079                       (setq nth4 (gnus-server-to-method nth4))))
5080                   ;; Only do the xrefs if the group has the same
5081                   ;; select method as the group we have just read.
5082                   (or (gnus-methods-equal-p
5083                        nth4 (gnus-find-method-for-group from-newsgroup))
5084                       virtual
5085                       (equal nth4 (setq method (gnus-find-method-for-group
5086                                                 from-newsgroup)))
5087                       (and (equal (car nth4) (car method))
5088                            (equal (nth 1 nth4) (nth 1 method))))
5089                   gnus-use-cross-reference
5090                   (or (not (eq gnus-use-cross-reference t))
5091                       virtual
5092                       ;; Only do cross-references on subscribed
5093                       ;; groups, if that is what is wanted.
5094                       (<= (gnus-info-level info) gnus-level-subscribed))
5095                   (gnus-group-make-articles-read name idlist))))
5096          xref-hashtb)))))
5097
5098 (defun gnus-compute-read-articles (group articles)
5099   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5100          (info (nth 2 entry))
5101          (active (gnus-active group))
5102          ninfo)
5103     (when entry
5104       ;; First peel off all invalid article numbers.
5105       (when active
5106         (let ((ids articles)
5107               id first)
5108           (while (setq id (pop ids))
5109             (when (and first (> id (cdr active)))
5110               ;; We'll end up in this situation in one particular
5111               ;; obscure situation.  If you re-scan a group and get
5112               ;; a new article that is cross-posted to a different
5113               ;; group that has not been re-scanned, you might get
5114               ;; crossposted article that has a higher number than
5115               ;; Gnus believes possible.  So we re-activate this
5116               ;; group as well.  This might mean doing the
5117               ;; crossposting thingy will *increase* the number
5118               ;; of articles in some groups.  Tsk, tsk.
5119               (setq active (or (gnus-activate-group group) active)))
5120             (when (or (> id (cdr active))
5121                       (< id (car active)))
5122               (setq articles (delq id articles))))))
5123       ;; If the read list is nil, we init it.
5124       (if (and active
5125                (null (gnus-info-read info))
5126                (> (car active) 1))
5127           (setq ninfo (cons 1 (1- (car active))))
5128         (setq ninfo (gnus-info-read info)))
5129       ;; Then we add the read articles to the range.
5130       (gnus-add-to-range
5131        ninfo (setq articles (sort articles '<))))))
5132
5133 (defun gnus-group-make-articles-read (group articles)
5134   "Update the info of GROUP to say that ARTICLES are read."
5135   (let* ((num 0)
5136          (entry (gnus-gethash group gnus-newsrc-hashtb))
5137          (info (nth 2 entry))
5138          (active (gnus-active group))
5139          range)
5140     (when entry
5141       (setq range (gnus-compute-read-articles group articles))
5142       (save-excursion
5143         (set-buffer gnus-group-buffer)
5144         (gnus-undo-register
5145           `(progn
5146              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5147              (gnus-info-set-read ',info ',(gnus-info-read info))
5148              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5149              (gnus-group-update-group ,group t))))
5150       ;; Add the read articles to the range.
5151       (gnus-info-set-read info range)
5152       (gnus-request-set-mark group (list (list range 'add '(read))))
5153       ;; Then we have to re-compute how many unread
5154       ;; articles there are in this group.
5155       (when active
5156         (cond
5157          ((not range)
5158           (setq num (- (1+ (cdr active)) (car active))))
5159          ((not (listp (cdr range)))
5160           (setq num (- (cdr active) (- (1+ (cdr range))
5161                                        (car range)))))
5162          (t
5163           (while range
5164             (if (numberp (car range))
5165                 (setq num (1+ num))
5166               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5167             (setq range (cdr range)))
5168           (setq num (- (cdr active) num))))
5169         ;; Update the number of unread articles.
5170         (setcar entry num)
5171         ;; Update the group buffer.
5172         (gnus-group-update-group group t)))))
5173
5174 (defvar gnus-newsgroup-none-id 0)
5175
5176 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5177   (let ((cur nntp-server-buffer)
5178         (dependencies
5179          (or dependencies
5180              (save-excursion (set-buffer gnus-summary-buffer)
5181                              gnus-newsgroup-dependencies)))
5182         headers id end ref
5183         (mail-parse-charset gnus-newsgroup-charset)
5184         (mail-parse-ignored-charsets
5185          (save-excursion (condition-case nil
5186                              (set-buffer gnus-summary-buffer)
5187                            (error))
5188                          gnus-newsgroup-ignored-charsets)))
5189     (save-excursion
5190       (set-buffer nntp-server-buffer)
5191       ;; Translate all TAB characters into SPACE characters.
5192       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5193       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5194       (gnus-run-hooks 'gnus-parse-headers-hook)
5195       (let ((case-fold-search t)
5196             in-reply-to header p lines chars ctype)
5197         (goto-char (point-min))
5198         ;; Search to the beginning of the next header.  Error messages
5199         ;; do not begin with 2 or 3.
5200         (while (re-search-forward "^[23][0-9]+ " nil t)
5201           (setq id nil
5202                 ref nil)
5203           ;; This implementation of this function, with nine
5204           ;; search-forwards instead of the one re-search-forward and
5205           ;; a case (which basically was the old function) is actually
5206           ;; about twice as fast, even though it looks messier.  You
5207           ;; can't have everything, I guess.  Speed and elegance
5208           ;; doesn't always go hand in hand.
5209           (setq
5210            header
5211            (make-full-mail-header
5212             ;; Number.
5213             (prog1
5214                 (read cur)
5215               (end-of-line)
5216               (setq p (point))
5217               (narrow-to-region (point)
5218                                 (or (and (search-forward "\n.\n" nil t)
5219                                          (- (point) 2))
5220                                     (point))))
5221             ;; Subject.
5222             (progn
5223               (goto-char p)
5224               (if (search-forward "\nsubject:" nil t)
5225                   (nnheader-header-value)
5226                 "(none)"))
5227             ;; From.
5228             (progn
5229               (goto-char p)
5230               (if (search-forward "\nfrom:" nil t)
5231                   (nnheader-header-value)
5232                 "(nobody)"))
5233             ;; Date.
5234             (progn
5235               (goto-char p)
5236               (if (search-forward "\ndate:" nil t)
5237                   (nnheader-header-value) ""))
5238             ;; Message-ID.
5239             (progn
5240               (goto-char p)
5241               (setq id (if (re-search-forward
5242                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5243                            ;; We do it this way to make sure the Message-ID
5244                            ;; is (somewhat) syntactically valid.
5245                            (buffer-substring (match-beginning 1)
5246                                              (match-end 1))
5247                          ;; If there was no message-id, we just fake one
5248                          ;; to make subsequent routines simpler.
5249                          (nnheader-generate-fake-message-id))))
5250             ;; References.
5251             (progn
5252               (goto-char p)
5253               (if (search-forward "\nreferences:" nil t)
5254                   (progn
5255                     (setq end (point))
5256                     (prog1
5257                         (nnheader-header-value)
5258                       (setq ref
5259                             (buffer-substring
5260                              (progn
5261                                ;; (end-of-line)
5262                                (search-backward ">" end t)
5263                                (1+ (point)))
5264                              (progn
5265                                (search-backward "<" end t)
5266                                (point))))))
5267                 ;; Get the references from the in-reply-to header if there
5268                 ;; were no references and the in-reply-to header looks
5269                 ;; promising.
5270                 (if (and (search-forward "\nin-reply-to:" nil t)
5271                          (setq in-reply-to (nnheader-header-value))
5272                          (string-match "<[^>]+>" in-reply-to))
5273                     (let (ref2)
5274                       (setq ref (substring in-reply-to (match-beginning 0)
5275                                            (match-end 0)))
5276                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5277                         (setq ref2 (substring in-reply-to (match-beginning 0)
5278                                               (match-end 0)))
5279                         (when (> (length ref2) (length ref))
5280                           (setq ref ref2)))
5281                       ref)
5282                   (setq ref nil))))
5283             ;; Chars.
5284             (progn
5285               (goto-char p)
5286               (if (search-forward "\nchars: " nil t)
5287                   (if (numberp (setq chars (ignore-errors (read cur))))
5288                       chars -1)
5289                 -1))
5290             ;; Lines.
5291             (progn
5292               (goto-char p)
5293               (if (search-forward "\nlines: " nil t)
5294                   (if (numberp (setq lines (ignore-errors (read cur))))
5295                       lines -1)
5296                 -1))
5297             ;; Xref.
5298             (progn
5299               (goto-char p)
5300               (and (search-forward "\nxref:" nil t)
5301                    (nnheader-header-value)))
5302             ;; Extra.
5303             (when gnus-extra-headers
5304               (let ((extra gnus-extra-headers)
5305                     out)
5306                 (while extra
5307                   (goto-char p)
5308                   (when (search-forward
5309                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5310                     (push (cons (car extra) (nnheader-header-value)) out))
5311                   (pop extra))
5312                 out))))
5313           (goto-char p)
5314           (if (and (search-forward "\ncontent-type: " nil t)
5315                    (setq ctype (nnheader-header-value)))
5316               (mime-entity-set-content-type-internal
5317                header (mime-parse-Content-Type ctype)))
5318           (when (equal id ref)
5319             (setq ref nil))
5320
5321           (when gnus-alter-header-function
5322             (funcall gnus-alter-header-function header)
5323             (setq id (mail-header-id header)
5324                   ref (gnus-parent-id (mail-header-references header))))
5325
5326           (when (setq header
5327                       (gnus-dependencies-add-header
5328                        header dependencies force-new))
5329             (push header headers))
5330           (goto-char (point-max))
5331           (widen))
5332         (nreverse headers)))))
5333
5334 ;; Goes through the xover lines and returns a list of vectors
5335 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5336                                                   force-new dependencies
5337                                                   group also-fetch-heads)
5338   "Parse the news overview data in the server buffer.
5339 Return a list of headers that match SEQUENCE (see
5340 `nntp-retrieve-headers')."
5341   ;; Get the Xref when the users reads the articles since most/some
5342   ;; NNTP servers do not include Xrefs when using XOVER.
5343   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5344   (let ((mail-parse-charset gnus-newsgroup-charset)
5345         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5346         (cur nntp-server-buffer)
5347         (dependencies (or dependencies gnus-newsgroup-dependencies))
5348         (allp (cond
5349                ((eq gnus-read-all-available-headers t)
5350                 t)
5351                ((stringp gnus-read-all-available-headers)
5352                 (string-match gnus-read-all-available-headers group))
5353                (t
5354                 nil)))
5355         number headers header)
5356     (save-excursion
5357       (set-buffer nntp-server-buffer)
5358       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5359       ;; Allow the user to mangle the headers before parsing them.
5360       (gnus-run-hooks 'gnus-parse-headers-hook)
5361       (goto-char (point-min))
5362       (while (not (eobp))
5363         (condition-case ()
5364             (while (and (or sequence allp)
5365                         (not (eobp)))
5366               (setq number (read cur))
5367               (when (not allp)
5368                 (while (and sequence
5369                             (< (car sequence) number))
5370                   (setq sequence (cdr sequence))))
5371               (when (and (or allp
5372                              (and sequence
5373                                   (eq number (car sequence))))
5374                          (progn
5375                            (setq sequence (cdr sequence))
5376                            (setq header (inline
5377                                           (gnus-nov-parse-line
5378                                            number dependencies force-new)))))
5379                 (push header headers))
5380               (forward-line 1))
5381           (error
5382            (gnus-error 4 "Strange nov line (%d)"
5383                        (count-lines (point-min) (point)))))
5384         (forward-line 1))
5385       ;; A common bug in inn is that if you have posted an article and
5386       ;; then retrieves the active file, it will answer correctly --
5387       ;; the new article is included.  However, a NOV entry for the
5388       ;; article may not have been generated yet, so this may fail.
5389       ;; We work around this problem by retrieving the last few
5390       ;; headers using HEAD.
5391       (if (or (not also-fetch-heads)
5392               (not sequence))
5393           ;; We (probably) got all the headers.
5394           (nreverse headers)
5395         (let ((gnus-nov-is-evil t))
5396           (nconc
5397            (nreverse headers)
5398            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5399              (gnus-get-newsgroup-headers))))))))
5400
5401 (defun gnus-article-get-xrefs ()
5402   "Fill in the Xref value in `gnus-current-headers', if necessary.
5403 This is meant to be called in `gnus-article-internal-prepare-hook'."
5404   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5405                                  gnus-current-headers)))
5406     (or (not gnus-use-cross-reference)
5407         (not headers)
5408         (and (mail-header-xref headers)
5409              (not (string= (mail-header-xref headers) "")))
5410         (let ((case-fold-search t)
5411               xref)
5412           (save-restriction
5413             (nnheader-narrow-to-headers)
5414             (goto-char (point-min))
5415             (when (or (and (not (eobp))
5416                            (eq (downcase (char-after)) ?x)
5417                            (looking-at "Xref:"))
5418                       (search-forward "\nXref:" nil t))
5419               (goto-char (1+ (match-end 0)))
5420               (setq xref (buffer-substring (point)
5421                                            (progn (end-of-line) (point))))
5422               (mail-header-set-xref headers xref)))))))
5423
5424 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5425   "Find article ID and insert the summary line for that article.
5426 OLD-HEADER can either be a header or a line number to insert
5427 the subject line on."
5428   (let* ((line (and (numberp old-header) old-header))
5429          (old-header (and (vectorp old-header) old-header))
5430          (header (cond ((and old-header use-old-header)
5431                         old-header)
5432                        ((and (numberp id)
5433                              (gnus-number-to-header id))
5434                         (gnus-number-to-header id))
5435                        (t
5436                         (gnus-read-header id))))
5437          (number (and (numberp id) id))
5438          d)
5439     (when header
5440       ;; Rebuild the thread that this article is part of and go to the
5441       ;; article we have fetched.
5442       (when (and (not gnus-show-threads)
5443                  old-header)
5444         (when (and number
5445                    (setq d (gnus-data-find (mail-header-number old-header))))
5446           (goto-char (gnus-data-pos d))
5447           (gnus-data-remove
5448            number
5449            (- (gnus-point-at-bol)
5450               (prog1
5451                   (1+ (gnus-point-at-eol))
5452                 (gnus-delete-line))))))
5453       (when old-header
5454         (mail-header-set-number header (mail-header-number old-header)))
5455       (setq gnus-newsgroup-sparse
5456             (delq (setq number (mail-header-number header))
5457                   gnus-newsgroup-sparse))
5458       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5459       (push number gnus-newsgroup-limit)
5460       (gnus-rebuild-thread (mail-header-id header) line)
5461       (gnus-summary-goto-subject number nil t))
5462     (when (and (numberp number)
5463                (> number 0))
5464       ;; We have to update the boundaries even if we can't fetch the
5465       ;; article if ID is a number -- so that the next `P' or `N'
5466       ;; command will fetch the previous (or next) article even
5467       ;; if the one we tried to fetch this time has been canceled.
5468       (when (> number gnus-newsgroup-end)
5469         (setq gnus-newsgroup-end number))
5470       (when (< number gnus-newsgroup-begin)
5471         (setq gnus-newsgroup-begin number))
5472       (setq gnus-newsgroup-unselected
5473             (delq number gnus-newsgroup-unselected)))
5474     ;; Report back a success?
5475     (and header (mail-header-number header))))
5476
5477 ;;; Process/prefix in the summary buffer
5478
5479 (defun gnus-summary-work-articles (n)
5480   "Return a list of articles to be worked upon.
5481 The prefix argument, the list of process marked articles, and the
5482 current article will be taken into consideration."
5483   (save-excursion
5484     (set-buffer gnus-summary-buffer)
5485     (cond
5486      (n
5487       ;; A numerical prefix has been given.
5488       (setq n (prefix-numeric-value n))
5489       (let ((backward (< n 0))
5490             (n (abs (prefix-numeric-value n)))
5491             articles article)
5492         (save-excursion
5493           (while
5494               (and (> n 0)
5495                    (push (setq article (gnus-summary-article-number))
5496                          articles)
5497                    (if backward
5498                        (gnus-summary-find-prev nil article)
5499                      (gnus-summary-find-next nil article)))
5500             (decf n)))
5501         (nreverse articles)))
5502      ((and (gnus-region-active-p) (mark))
5503       (message "region active")
5504       ;; Work on the region between point and mark.
5505       (let ((max (max (point) (mark)))
5506             articles article)
5507         (save-excursion
5508           (goto-char (min (point) (mark)))
5509           (while
5510               (and
5511                (push (setq article (gnus-summary-article-number)) articles)
5512                (gnus-summary-find-next nil article)
5513                (< (point) max)))
5514           (nreverse articles))))
5515      (gnus-newsgroup-processable
5516       ;; There are process-marked articles present.
5517       ;; Save current state.
5518       (gnus-summary-save-process-mark)
5519       ;; Return the list.
5520       (reverse gnus-newsgroup-processable))
5521      (t
5522       ;; Just return the current article.
5523       (list (gnus-summary-article-number))))))
5524
5525 (defmacro gnus-summary-iterate (arg &rest forms)
5526   "Iterate over the process/prefixed articles and do FORMS.
5527 ARG is the interactive prefix given to the command.  FORMS will be
5528 executed with point over the summary line of the articles."
5529   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5530     `(let ((,articles (gnus-summary-work-articles ,arg)))
5531        (while ,articles
5532          (gnus-summary-goto-subject (car ,articles))
5533          ,@forms
5534          (pop ,articles)))))
5535
5536 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5537 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5538
5539 (defun gnus-summary-save-process-mark ()
5540   "Push the current set of process marked articles on the stack."
5541   (interactive)
5542   (push (copy-sequence gnus-newsgroup-processable)
5543         gnus-newsgroup-process-stack))
5544
5545 (defun gnus-summary-kill-process-mark ()
5546   "Push the current set of process marked articles on the stack and unmark."
5547   (interactive)
5548   (gnus-summary-save-process-mark)
5549   (gnus-summary-unmark-all-processable))
5550
5551 (defun gnus-summary-yank-process-mark ()
5552   "Pop the last process mark state off the stack and restore it."
5553   (interactive)
5554   (unless gnus-newsgroup-process-stack
5555     (error "Empty mark stack"))
5556   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5557
5558 (defun gnus-summary-process-mark-set (set)
5559   "Make SET into the current process marked articles."
5560   (gnus-summary-unmark-all-processable)
5561   (while set
5562     (gnus-summary-set-process-mark (pop set))))
5563
5564 ;;; Searching and stuff
5565
5566 (defun gnus-summary-search-group (&optional backward use-level)
5567   "Search for next unread newsgroup.
5568 If optional argument BACKWARD is non-nil, search backward instead."
5569   (save-excursion
5570     (set-buffer gnus-group-buffer)
5571     (when (gnus-group-search-forward
5572            backward nil (if use-level (gnus-group-group-level) nil))
5573       (gnus-group-group-name))))
5574
5575 (defun gnus-summary-best-group (&optional exclude-group)
5576   "Find the name of the best unread group.
5577 If EXCLUDE-GROUP, do not go to this group."
5578   (save-excursion
5579     (set-buffer gnus-group-buffer)
5580     (save-excursion
5581       (gnus-group-best-unread-group exclude-group))))
5582
5583 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5584   (if backward (gnus-summary-find-prev)
5585     (let* ((dummy (gnus-summary-article-intangible-p))
5586            (article (or article (gnus-summary-article-number)))
5587            (arts (gnus-data-find-list article))
5588            result)
5589       (when (and (not dummy)
5590                  (or (not gnus-summary-check-current)
5591                      (not unread)
5592                      (not (gnus-data-unread-p (car arts)))))
5593         (setq arts (cdr arts)))
5594       (when (setq result
5595                   (if unread
5596                       (progn
5597                         (while arts
5598                           (when (or (and undownloaded
5599                                          (eq gnus-undownloaded-mark
5600                                              (gnus-data-mark (car arts))))
5601                                     (gnus-data-unread-p (car arts)))
5602                             (setq result (car arts)
5603                                   arts nil))
5604                           (setq arts (cdr arts)))
5605                         result)
5606                     (car arts)))
5607         (goto-char (gnus-data-pos result))
5608         (gnus-data-number result)))))
5609
5610 (defun gnus-summary-find-prev (&optional unread article)
5611   (let* ((eobp (eobp))
5612          (article (or article (gnus-summary-article-number)))
5613          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5614          result)
5615     (when (and (not eobp)
5616                (or (not gnus-summary-check-current)
5617                    (not unread)
5618                    (not (gnus-data-unread-p (car arts)))))
5619       (setq arts (cdr arts)))
5620     (when (setq result
5621                 (if unread
5622                     (progn
5623                       (while arts
5624                         (when (gnus-data-unread-p (car arts))
5625                           (setq result (car arts)
5626                                 arts nil))
5627                         (setq arts (cdr arts)))
5628                       result)
5629                   (car arts)))
5630       (goto-char (gnus-data-pos result))
5631       (gnus-data-number result))))
5632
5633 (defun gnus-summary-find-subject (subject &optional unread backward article)
5634   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5635          (article (or article (gnus-summary-article-number)))
5636          (articles (gnus-data-list backward))
5637          (arts (gnus-data-find-list article articles))
5638          result)
5639     (when (or (not gnus-summary-check-current)
5640               (not unread)
5641               (not (gnus-data-unread-p (car arts))))
5642       (setq arts (cdr arts)))
5643     (while arts
5644       (and (or (not unread)
5645                (gnus-data-unread-p (car arts)))
5646            (vectorp (gnus-data-header (car arts)))
5647            (gnus-subject-equal
5648             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5649            (setq result (car arts)
5650                  arts nil))
5651       (setq arts (cdr arts)))
5652     (and result
5653          (goto-char (gnus-data-pos result))
5654          (gnus-data-number result))))
5655
5656 (defun gnus-summary-search-forward (&optional unread subject backward)
5657   "Search forward for an article.
5658 If UNREAD, look for unread articles.  If SUBJECT, look for
5659 articles with that subject.  If BACKWARD, search backward instead."
5660   (cond (subject (gnus-summary-find-subject subject unread backward))
5661         (backward (gnus-summary-find-prev unread))
5662         (t (gnus-summary-find-next unread))))
5663
5664 (defun gnus-recenter (&optional n)
5665   "Center point in window and redisplay frame.
5666 Also do horizontal recentering."
5667   (interactive "P")
5668   (when (and gnus-auto-center-summary
5669              (not (eq gnus-auto-center-summary 'vertical)))
5670     (gnus-horizontal-recenter))
5671   (recenter n))
5672
5673 (defun gnus-summary-recenter ()
5674   "Center point in the summary window.
5675 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5676 displayed, no centering will be performed."
5677   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5678   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5679   (interactive)
5680   (let* ((top (cond ((< (window-height) 4) 0)
5681                     ((< (window-height) 7) 1)
5682                     (t (if (numberp gnus-auto-center-summary)
5683                            gnus-auto-center-summary
5684                          2))))
5685          (height (1- (window-height)))
5686          (bottom (save-excursion (goto-char (point-max))
5687                                  (forward-line (- height))
5688                                  (point)))
5689          (window (get-buffer-window (current-buffer))))
5690     ;; The user has to want it.
5691     (when gnus-auto-center-summary
5692       (when (get-buffer-window gnus-article-buffer)
5693         ;; Only do recentering when the article buffer is displayed,
5694         ;; Set the window start to either `bottom', which is the biggest
5695         ;; possible valid number, or the second line from the top,
5696         ;; whichever is the least.
5697         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5698           (if (> bottom top-pos)
5699               ;; Keep the second line from the top visible
5700               (set-window-start window top-pos t)
5701             ;; Try to keep the bottom line visible; if it's partially
5702             ;; obscured, either scroll one more line to make it fully
5703             ;; visible, or revert to using TOP-POS.
5704             (save-excursion
5705               (goto-char (point-max))
5706               (forward-line -1)
5707               (let ((last-line-start (point)))
5708                 (goto-char bottom)
5709                 (set-window-start window (point) t)
5710                 (when (not (pos-visible-in-window-p last-line-start window))
5711                   (forward-line 1)
5712                   (set-window-start window (min (point) top-pos) t)))))))
5713       ;; Do horizontal recentering while we're at it.
5714       (when (and (get-buffer-window (current-buffer) t)
5715                  (not (eq gnus-auto-center-summary 'vertical)))
5716         (let ((selected (selected-window)))
5717           (select-window (get-buffer-window (current-buffer) t))
5718           (gnus-summary-position-point)
5719           (gnus-horizontal-recenter)
5720           (select-window selected))))))
5721
5722 (defun gnus-summary-jump-to-group (newsgroup)
5723   "Move point to NEWSGROUP in group mode buffer."
5724   ;; Keep update point of group mode buffer if visible.
5725   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5726       (save-window-excursion
5727         ;; Take care of tree window mode.
5728         (when (get-buffer-window gnus-group-buffer)
5729           (pop-to-buffer gnus-group-buffer))
5730         (gnus-group-jump-to-group newsgroup))
5731     (save-excursion
5732       ;; Take care of tree window mode.
5733       (if (get-buffer-window gnus-group-buffer)
5734           (pop-to-buffer gnus-group-buffer)
5735         (set-buffer gnus-group-buffer))
5736       (gnus-group-jump-to-group newsgroup))))
5737
5738 ;; This function returns a list of article numbers based on the
5739 ;; difference between the ranges of read articles in this group and
5740 ;; the range of active articles.
5741 (defun gnus-list-of-unread-articles (group)
5742   (let* ((read (gnus-info-read (gnus-get-info group)))
5743          (active (or (gnus-active group) (gnus-activate-group group)))
5744          (last (cdr active))
5745          first nlast unread)
5746     ;; If none are read, then all are unread.
5747     (if (not read)
5748         (setq first (car active))
5749       ;; If the range of read articles is a single range, then the
5750       ;; first unread article is the article after the last read
5751       ;; article.  Sounds logical, doesn't it?
5752       (if (and (not (listp (cdr read)))
5753                (or (< (car read) (car active))
5754                    (progn (setq read (list read))
5755                           nil)))
5756           (setq first (max (car active) (1+ (cdr read))))
5757         ;; `read' is a list of ranges.
5758         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5759                                   (caar read)))
5760                   1)
5761           (setq first (car active)))
5762         (while read
5763           (when first
5764             (while (< first nlast)
5765               (push first unread)
5766               (setq first (1+ first))))
5767           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5768           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5769           (setq read (cdr read)))))
5770     ;; And add the last unread articles.
5771     (while (<= first last)
5772       (push first unread)
5773       (setq first (1+ first)))
5774     ;; Return the list of unread articles.
5775     (delq 0 (nreverse unread))))
5776
5777 (defun gnus-list-of-read-articles (group)
5778   "Return a list of unread, unticked and non-dormant articles."
5779   (let* ((info (gnus-get-info group))
5780          (marked (gnus-info-marks info))
5781          (active (gnus-active group)))
5782     (and info active
5783          (gnus-set-difference
5784           (gnus-sorted-complement
5785            (gnus-uncompress-range active)
5786            (gnus-list-of-unread-articles group))
5787           (append
5788            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5789            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5790
5791 ;; Various summary commands
5792
5793 (defun gnus-summary-select-article-buffer ()
5794   "Reconfigure windows to show article buffer."
5795   (interactive)
5796   (if (not (gnus-buffer-live-p gnus-article-buffer))
5797       (error "There is no article buffer for this summary buffer")
5798     (gnus-configure-windows 'article)
5799     (select-window (get-buffer-window gnus-article-buffer))))
5800
5801 (defun gnus-summary-universal-argument (arg)
5802   "Perform any operation on all articles that are process/prefixed."
5803   (interactive "P")
5804   (let ((articles (gnus-summary-work-articles arg))
5805         func article)
5806     (if (eq
5807          (setq
5808           func
5809           (key-binding
5810            (read-key-sequence
5811             (substitute-command-keys
5812              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5813          'undefined)
5814         (gnus-error 1 "Undefined key")
5815       (save-excursion
5816         (while articles
5817           (gnus-summary-goto-subject (setq article (pop articles)))
5818           (let (gnus-newsgroup-processable)
5819             (command-execute func))
5820           (gnus-summary-remove-process-mark article)))))
5821   (gnus-summary-position-point))
5822
5823 (defun gnus-summary-toggle-truncation (&optional arg)
5824   "Toggle truncation of summary lines.
5825 With arg, turn line truncation on iff arg is positive."
5826   (interactive "P")
5827   (setq truncate-lines
5828         (if (null arg) (not truncate-lines)
5829           (> (prefix-numeric-value arg) 0)))
5830   (redraw-display))
5831
5832 (defun gnus-summary-reselect-current-group (&optional all rescan)
5833   "Exit and then reselect the current newsgroup.
5834 The prefix argument ALL means to select all articles."
5835   (interactive "P")
5836   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5837     (error "Ephemeral groups can't be reselected"))
5838   (let ((current-subject (gnus-summary-article-number))
5839         (group gnus-newsgroup-name))
5840     (setq gnus-newsgroup-begin nil)
5841     (gnus-summary-exit)
5842     ;; We have to adjust the point of group mode buffer because
5843     ;; point was moved to the next unread newsgroup by exiting.
5844     (gnus-summary-jump-to-group group)
5845     (when rescan
5846       (save-excursion
5847         (save-window-excursion
5848           ;; Don't show group contents.
5849           (set-window-start (selected-window) (point-max))
5850           (gnus-group-get-new-news-this-group 1))))
5851     (gnus-group-read-group all t)
5852     (gnus-summary-goto-subject current-subject nil t)))
5853
5854 (defun gnus-summary-rescan-group (&optional all)
5855   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5856   (interactive "P")
5857   (gnus-summary-reselect-current-group all t))
5858
5859 (defun gnus-summary-update-info (&optional non-destructive)
5860   (save-excursion
5861     (let ((group gnus-newsgroup-name))
5862       (when group
5863         (when gnus-newsgroup-kill-headers
5864           (setq gnus-newsgroup-killed
5865                 (gnus-compress-sequence
5866                  (nconc
5867                   (gnus-set-sorted-intersection
5868                    (gnus-uncompress-range gnus-newsgroup-killed)
5869                    (setq gnus-newsgroup-unselected
5870                          (sort gnus-newsgroup-unselected '<)))
5871                   (setq gnus-newsgroup-unreads
5872                         (sort gnus-newsgroup-unreads '<)))
5873                  t)))
5874         (unless (listp (cdr gnus-newsgroup-killed))
5875           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5876         (let ((headers gnus-newsgroup-headers))
5877           ;; Set the new ranges of read articles.
5878           (save-excursion
5879             (set-buffer gnus-group-buffer)
5880             (gnus-undo-force-boundary))
5881           (gnus-update-read-articles
5882            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5883           ;; Set the current article marks.
5884           (let ((gnus-newsgroup-scored
5885                  (if (and (not gnus-save-score)
5886                           (not non-destructive))
5887                      nil
5888                    gnus-newsgroup-scored)))
5889             (save-excursion
5890               (gnus-update-marks)))
5891           ;; Do the cross-ref thing.
5892           (when gnus-use-cross-reference
5893             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5894           ;; Do not switch windows but change the buffer to work.
5895           (set-buffer gnus-group-buffer)
5896           (unless (gnus-ephemeral-group-p group)
5897             (gnus-group-update-group group)))))))
5898
5899 (defun gnus-summary-save-newsrc (&optional force)
5900   "Save the current number of read/marked articles in the dribble buffer.
5901 The dribble buffer will then be saved.
5902 If FORCE (the prefix), also save the .newsrc file(s)."
5903   (interactive "P")
5904   (gnus-summary-update-info t)
5905   (if force
5906       (gnus-save-newsrc-file)
5907     (gnus-dribble-save)))
5908
5909 (defun gnus-summary-exit (&optional temporary)
5910   "Exit reading current newsgroup, and then return to group selection mode.
5911 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5912   (interactive)
5913   (gnus-set-global-variables)
5914   (gnus-kill-save-kill-buffer)
5915   (gnus-async-halt-prefetch)
5916   (let* ((group gnus-newsgroup-name)
5917          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5918          (mode major-mode)
5919          (group-point nil)
5920          (buf (current-buffer)))
5921     (unless quit-config
5922       ;; Do adaptive scoring, and possibly save score files.
5923       (when gnus-newsgroup-adaptive
5924         (gnus-score-adaptive))
5925       (when gnus-use-scoring
5926         (gnus-score-save)))
5927     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5928     ;; If we have several article buffers, we kill them at exit.
5929     (unless gnus-single-article-buffer
5930       (gnus-kill-buffer gnus-original-article-buffer)
5931       (setq gnus-article-current nil))
5932     (when gnus-use-cache
5933       (gnus-cache-possibly-remove-articles)
5934       (gnus-cache-save-buffers))
5935     (gnus-async-prefetch-remove-group group)
5936     (when gnus-suppress-duplicates
5937       (gnus-dup-enter-articles))
5938     (when gnus-use-trees
5939       (gnus-tree-close group))
5940     (when gnus-use-cache
5941       (gnus-cache-write-active))
5942     ;; Remove entries for this group.
5943     (nnmail-purge-split-history (gnus-group-real-name group))
5944     ;; Make all changes in this group permanent.
5945     (unless quit-config
5946       (gnus-run-hooks 'gnus-exit-group-hook)
5947       (gnus-summary-update-info))
5948     (gnus-close-group group)
5949     ;; Make sure where we were, and go to next newsgroup.
5950     (set-buffer gnus-group-buffer)
5951     (unless quit-config
5952       (gnus-group-jump-to-group group))
5953     (gnus-run-hooks 'gnus-summary-exit-hook)
5954     (unless (or quit-config
5955                 ;; If this group has disappeared from the summary
5956                 ;; buffer, don't skip forwards.
5957                 (not (string= group (gnus-group-group-name))))
5958       (gnus-group-next-unread-group 1))
5959     (setq group-point (point))
5960     (if temporary
5961         nil                             ;Nothing to do.
5962       ;; If we have several article buffers, we kill them at exit.
5963       (unless gnus-single-article-buffer
5964         (gnus-kill-buffer gnus-article-buffer)
5965         (gnus-kill-buffer gnus-original-article-buffer)
5966         (setq gnus-article-current nil))
5967       (set-buffer buf)
5968       (if (not gnus-kill-summary-on-exit)
5969           (progn
5970             (gnus-deaden-summary)
5971             (setq mode nil))
5972         ;; We set all buffer-local variables to nil.  It is unclear why
5973         ;; this is needed, but if we don't, buffer-local variables are
5974         ;; not garbage-collected, it seems.  This would the lead to en
5975         ;; ever-growing Emacs.
5976         (gnus-summary-clear-local-variables)
5977         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5978           (gnus-summary-clear-local-variables))
5979         (when (get-buffer gnus-article-buffer)
5980           (bury-buffer gnus-article-buffer))
5981         ;; We clear the global counterparts of the buffer-local
5982         ;; variables as well, just to be on the safe side.
5983         (set-buffer gnus-group-buffer)
5984         (gnus-summary-clear-local-variables)
5985         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5986           (gnus-summary-clear-local-variables)))
5987       (setq gnus-current-select-method gnus-select-method)
5988       (pop-to-buffer gnus-group-buffer)
5989       (if (not quit-config)
5990           (progn
5991             (goto-char group-point)
5992             (gnus-configure-windows 'group 'force)
5993             (unless (pos-visible-in-window-p)
5994               (forward-line (/ (static-if (featurep 'xemacs)
5995                                    (window-displayed-height)
5996                                  (1- (window-height)))
5997                                -2))
5998               (set-window-start (selected-window) (point))
5999               (goto-char group-point)))
6000         (gnus-handle-ephemeral-exit quit-config))
6001       ;; Return to group mode buffer.
6002       (when (eq mode 'gnus-summary-mode)
6003         (gnus-kill-buffer buf))
6004       ;; Clear the current group name.
6005       (unless quit-config
6006         (setq gnus-newsgroup-name nil)))))
6007
6008 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6009 (defun gnus-summary-exit-no-update (&optional no-questions)
6010   "Quit reading current newsgroup without updating read article info."
6011   (interactive)
6012   (let* ((group gnus-newsgroup-name)
6013          (quit-config (gnus-group-quit-config group)))
6014     (when (or no-questions
6015               gnus-expert-user
6016               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6017       (gnus-async-halt-prefetch)
6018       (mapcar 'funcall
6019               (delq 'gnus-summary-expire-articles
6020                     (copy-sequence gnus-summary-prepare-exit-hook)))
6021       ;; If we have several article buffers, we kill them at exit.
6022       (unless gnus-single-article-buffer
6023         (gnus-kill-buffer gnus-article-buffer)
6024         (gnus-kill-buffer gnus-original-article-buffer)
6025         (setq gnus-article-current nil))
6026       (if (not gnus-kill-summary-on-exit)
6027           (gnus-deaden-summary)
6028         (gnus-close-group group)
6029         (gnus-summary-clear-local-variables)
6030         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6031           (gnus-summary-clear-local-variables))
6032         (set-buffer gnus-group-buffer)
6033         (gnus-summary-clear-local-variables)
6034         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6035           (gnus-summary-clear-local-variables))
6036         (when (get-buffer gnus-summary-buffer)
6037           (kill-buffer gnus-summary-buffer)))
6038       (unless gnus-single-article-buffer
6039         (setq gnus-article-current nil))
6040       (when gnus-use-trees
6041         (gnus-tree-close group))
6042       (gnus-async-prefetch-remove-group group)
6043       (when (get-buffer gnus-article-buffer)
6044         (bury-buffer gnus-article-buffer))
6045       ;; Return to the group buffer.
6046       (gnus-configure-windows 'group 'force)
6047       ;; Clear the current group name.
6048       (setq gnus-newsgroup-name nil)
6049       (when (equal (gnus-group-group-name) group)
6050         (gnus-group-next-unread-group 1))
6051       (when quit-config
6052         (gnus-handle-ephemeral-exit quit-config)))))
6053
6054 (defun gnus-handle-ephemeral-exit (quit-config)
6055   "Handle movement when leaving an ephemeral group.
6056 The state which existed when entering the ephemeral is reset."
6057   (if (not (buffer-name (car quit-config)))
6058       (gnus-configure-windows 'group 'force)
6059     (set-buffer (car quit-config))
6060     (cond ((eq major-mode 'gnus-summary-mode)
6061            (gnus-set-global-variables))
6062           ((eq major-mode 'gnus-article-mode)
6063            (save-excursion
6064              ;; The `gnus-summary-buffer' variable may point
6065              ;; to the old summary buffer when using a single
6066              ;; article buffer.
6067              (unless (gnus-buffer-live-p gnus-summary-buffer)
6068                (set-buffer gnus-group-buffer))
6069              (set-buffer gnus-summary-buffer)
6070              (gnus-set-global-variables))))
6071     (if (or (eq (cdr quit-config) 'article)
6072             (eq (cdr quit-config) 'pick))
6073         (progn
6074           ;; The current article may be from the ephemeral group
6075           ;; thus it is best that we reload this article
6076           (gnus-summary-show-article)
6077           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6078               (gnus-configure-windows 'pick 'force)
6079             (gnus-configure-windows (cdr quit-config) 'force)))
6080       (gnus-configure-windows (cdr quit-config) 'force))
6081     (when (eq major-mode 'gnus-summary-mode)
6082       (gnus-summary-next-subject 1 nil t)
6083       (gnus-summary-recenter)
6084       (gnus-summary-position-point))))
6085
6086 (defun gnus-summary-preview-mime-message ()
6087   "MIME decode and play this message."
6088   (interactive)
6089   (let ((gnus-break-pages nil)
6090         (gnus-show-mime t))
6091     (gnus-summary-select-article gnus-show-all-headers t))
6092   (select-window (get-buffer-window gnus-article-buffer)))
6093
6094 ;;; Dead summaries.
6095
6096 (defvar gnus-dead-summary-mode-map nil)
6097
6098 (unless gnus-dead-summary-mode-map
6099   (setq gnus-dead-summary-mode-map (make-keymap))
6100   (suppress-keymap gnus-dead-summary-mode-map)
6101   (substitute-key-definition
6102    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6103   (let ((keys '("\C-d" "\r" "\177" [delete])))
6104     (while keys
6105       (define-key gnus-dead-summary-mode-map
6106         (pop keys) 'gnus-summary-wake-up-the-dead))))
6107
6108 (defvar gnus-dead-summary-mode nil
6109   "Minor mode for Gnus summary buffers.")
6110
6111 (defun gnus-dead-summary-mode (&optional arg)
6112   "Minor mode for Gnus summary buffers."
6113   (interactive "P")
6114   (when (eq major-mode 'gnus-summary-mode)
6115     (make-local-variable 'gnus-dead-summary-mode)
6116     (setq gnus-dead-summary-mode
6117           (if (null arg) (not gnus-dead-summary-mode)
6118             (> (prefix-numeric-value arg) 0)))
6119     (when gnus-dead-summary-mode
6120       (gnus-add-minor-mode
6121        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6122
6123 (defun gnus-deaden-summary ()
6124   "Make the current summary buffer into a dead summary buffer."
6125   ;; Kill any previous dead summary buffer.
6126   (when (and gnus-dead-summary
6127              (buffer-name gnus-dead-summary))
6128     (save-excursion
6129       (set-buffer gnus-dead-summary)
6130       (when gnus-dead-summary-mode
6131         (kill-buffer (current-buffer)))))
6132   ;; Make this the current dead summary.
6133   (setq gnus-dead-summary (current-buffer))
6134   (gnus-dead-summary-mode 1)
6135   (let ((name (buffer-name)))
6136     (when (string-match "Summary" name)
6137       (rename-buffer
6138        (concat (substring name 0 (match-beginning 0)) "Dead "
6139                (substring name (match-beginning 0)))
6140        t)
6141       (bury-buffer))))
6142
6143 (defun gnus-kill-or-deaden-summary (buffer)
6144   "Kill or deaden the summary BUFFER."
6145   (save-excursion
6146     (when (and (buffer-name buffer)
6147                (not gnus-single-article-buffer))
6148       (save-excursion
6149         (set-buffer buffer)
6150         (gnus-kill-buffer gnus-article-buffer)
6151         (gnus-kill-buffer gnus-original-article-buffer)))
6152     (cond (gnus-kill-summary-on-exit
6153            (when (and gnus-use-trees
6154                       (gnus-buffer-exists-p buffer))
6155              (save-excursion
6156                (set-buffer buffer)
6157                (gnus-tree-close gnus-newsgroup-name)))
6158            (gnus-kill-buffer buffer))
6159           ((gnus-buffer-exists-p buffer)
6160            (save-excursion
6161              (set-buffer buffer)
6162              (gnus-deaden-summary))))))
6163
6164 (defun gnus-summary-wake-up-the-dead (&rest args)
6165   "Wake up the dead summary buffer."
6166   (interactive)
6167   (gnus-dead-summary-mode -1)
6168   (let ((name (buffer-name)))
6169     (when (string-match "Dead " name)
6170       (rename-buffer
6171        (concat (substring name 0 (match-beginning 0))
6172                (substring name (match-end 0)))
6173        t)))
6174   (gnus-message 3 "This dead summary is now alive again"))
6175
6176 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6177 (defun gnus-summary-fetch-faq (&optional faq-dir)
6178   "Fetch the FAQ for the current group.
6179 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6180 in."
6181   (interactive
6182    (list
6183     (when current-prefix-arg
6184       (completing-read
6185        "Faq dir: " (and (listp gnus-group-faq-directory)
6186                         (mapcar (lambda (file) (list file))
6187                                 gnus-group-faq-directory))))))
6188   (let (gnus-faq-buffer)
6189     (when (setq gnus-faq-buffer
6190                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6191       (gnus-configure-windows 'summary-faq))))
6192
6193 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6194 (defun gnus-summary-describe-group (&optional force)
6195   "Describe the current newsgroup."
6196   (interactive "P")
6197   (gnus-group-describe-group force gnus-newsgroup-name))
6198
6199 (defun gnus-summary-describe-briefly ()
6200   "Describe summary mode commands briefly."
6201   (interactive)
6202   (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")))
6203
6204 ;; Walking around group mode buffer from summary mode.
6205
6206 (defun gnus-summary-next-group (&optional no-article target-group backward)
6207   "Exit current newsgroup and then select next unread newsgroup.
6208 If prefix argument NO-ARTICLE is non-nil, no article is selected
6209 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6210 previous group instead."
6211   (interactive "P")
6212   ;; Stop pre-fetching.
6213   (gnus-async-halt-prefetch)
6214   (let ((current-group gnus-newsgroup-name)
6215         (current-buffer (current-buffer))
6216         entered)
6217     ;; First we semi-exit this group to update Xrefs and all variables.
6218     ;; We can't do a real exit, because the window conf must remain
6219     ;; the same in case the user is prompted for info, and we don't
6220     ;; want the window conf to change before that...
6221     (gnus-summary-exit t)
6222     (while (not entered)
6223       ;; Then we find what group we are supposed to enter.
6224       (set-buffer gnus-group-buffer)
6225       (gnus-group-jump-to-group current-group)
6226       (setq target-group
6227             (or target-group
6228                 (if (eq gnus-keep-same-level 'best)
6229                     (gnus-summary-best-group gnus-newsgroup-name)
6230                   (gnus-summary-search-group backward gnus-keep-same-level))))
6231       (if (not target-group)
6232           ;; There are no further groups, so we return to the group
6233           ;; buffer.
6234           (progn
6235             (gnus-message 5 "Returning to the group buffer")
6236             (setq entered t)
6237             (when (gnus-buffer-live-p current-buffer)
6238               (set-buffer current-buffer)
6239               (gnus-summary-exit))
6240             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6241         ;; We try to enter the target group.
6242         (gnus-group-jump-to-group target-group)
6243         (let ((unreads (gnus-group-group-unread)))
6244           (if (and (or (eq t unreads)
6245                        (and unreads (not (zerop unreads))))
6246                    (gnus-summary-read-group
6247                     target-group nil no-article
6248                     (and (buffer-name current-buffer) current-buffer)
6249                     nil backward))
6250               (setq entered t)
6251             (setq current-group target-group
6252                   target-group nil)))))))
6253
6254 (defun gnus-summary-prev-group (&optional no-article)
6255   "Exit current newsgroup and then select previous unread newsgroup.
6256 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6257   (interactive "P")
6258   (gnus-summary-next-group no-article nil t))
6259
6260 ;; Walking around summary lines.
6261
6262 (defun gnus-summary-first-subject (&optional unread undownloaded)
6263   "Go to the first unread subject.
6264 If UNREAD is non-nil, go to the first unread article.
6265 Returns the article selected or nil if there are no unread articles."
6266   (interactive "P")
6267   (prog1
6268       (cond
6269        ;; Empty summary.
6270        ((null gnus-newsgroup-data)
6271         (gnus-message 3 "No articles in the group")
6272         nil)
6273        ;; Pick the first article.
6274        ((not unread)
6275         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6276         (gnus-data-number (car gnus-newsgroup-data)))
6277        ;; No unread articles.
6278        ((null gnus-newsgroup-unreads)
6279         (gnus-message 3 "No more unread articles")
6280         nil)
6281        ;; Find the first unread article.
6282        (t
6283         (let ((data gnus-newsgroup-data))
6284           (while (and data
6285                       (and (not (and undownloaded
6286                                      (eq gnus-undownloaded-mark
6287                                          (gnus-data-mark (car data)))))
6288                            (not (gnus-data-unread-p (car data)))))
6289             (setq data (cdr data)))
6290           (when data
6291             (goto-char (gnus-data-pos (car data)))
6292             (gnus-data-number (car data))))))
6293     (gnus-summary-position-point)))
6294
6295 (defun gnus-summary-next-subject (n &optional unread dont-display)
6296   "Go to next N'th summary line.
6297 If N is negative, go to the previous N'th subject line.
6298 If UNREAD is non-nil, only unread articles are selected.
6299 The difference between N and the actual number of steps taken is
6300 returned."
6301   (interactive "p")
6302   (let ((backward (< n 0))
6303         (n (abs n)))
6304     (while (and (> n 0)
6305                 (if backward
6306                     (gnus-summary-find-prev unread)
6307                   (gnus-summary-find-next unread)))
6308       (unless (zerop (setq n (1- n)))
6309         (gnus-summary-show-thread)))
6310     (when (/= 0 n)
6311       (gnus-message 7 "No more%s articles"
6312                     (if unread " unread" "")))
6313     (unless dont-display
6314       (gnus-summary-recenter)
6315       (gnus-summary-position-point))
6316     n))
6317
6318 (defun gnus-summary-next-unread-subject (n)
6319   "Go to next N'th unread summary line."
6320   (interactive "p")
6321   (gnus-summary-next-subject n t))
6322
6323 (defun gnus-summary-prev-subject (n &optional unread)
6324   "Go to previous N'th summary line.
6325 If optional argument UNREAD is non-nil, only unread article is selected."
6326   (interactive "p")
6327   (gnus-summary-next-subject (- n) unread))
6328
6329 (defun gnus-summary-prev-unread-subject (n)
6330   "Go to previous N'th unread summary line."
6331   (interactive "p")
6332   (gnus-summary-next-subject (- n) t))
6333
6334 (defun gnus-summary-goto-subject (article &optional force silent)
6335   "Go the subject line of ARTICLE.
6336 If FORCE, also allow jumping to articles not currently shown."
6337   (interactive "nArticle number: ")
6338   (let ((b (point))
6339         (data (gnus-data-find article)))
6340     ;; We read in the article if we have to.
6341     (and (not data)
6342          force
6343          (gnus-summary-insert-subject
6344           article
6345           (if (or (numberp force) (vectorp force)) force)
6346           t)
6347          (setq data (gnus-data-find article)))
6348     (goto-char b)
6349     (if (not data)
6350         (progn
6351           (unless silent
6352             (gnus-message 3 "Can't find article %d" article))
6353           nil)
6354       (let ((pt (gnus-data-pos data)))
6355         (goto-char pt)
6356         (gnus-summary-set-article-display-arrow pt))
6357       (gnus-summary-position-point)
6358       article)))
6359
6360 ;; Walking around summary lines with displaying articles.
6361
6362 (defun gnus-summary-expand-window (&optional arg)
6363   "Make the summary buffer take up the entire Emacs frame.
6364 Given a prefix, will force an `article' buffer configuration."
6365   (interactive "P")
6366   (if arg
6367       (gnus-configure-windows 'article 'force)
6368     (gnus-configure-windows 'summary 'force)))
6369
6370 (defun gnus-summary-display-article (article &optional all-header)
6371   "Display ARTICLE in article buffer."
6372   (when (gnus-buffer-live-p gnus-article-buffer)
6373     (with-current-buffer gnus-article-buffer
6374       (set-buffer-multibyte t)))
6375   (gnus-set-global-variables)
6376   (when (gnus-buffer-live-p gnus-article-buffer)
6377     (with-current-buffer gnus-article-buffer
6378       (setq gnus-article-charset gnus-newsgroup-charset)
6379       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6380   (if (null article)
6381       nil
6382     (prog1
6383         (if gnus-summary-display-article-function
6384             (funcall gnus-summary-display-article-function article all-header)
6385           (gnus-article-prepare article all-header))
6386       (with-current-buffer gnus-article-buffer
6387         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6388              nil))
6389       (gnus-run-hooks 'gnus-select-article-hook)
6390       (when (and gnus-current-article
6391                  (not (zerop gnus-current-article)))
6392         (gnus-summary-goto-subject gnus-current-article))
6393       (gnus-summary-recenter)
6394       (when (and gnus-use-trees gnus-show-threads)
6395         (gnus-possibly-generate-tree article)
6396         (gnus-highlight-selected-tree article))
6397       ;; Successfully display article.
6398       (gnus-article-set-window-start
6399        (cdr (assq article gnus-newsgroup-bookmarks))))))
6400
6401 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6402   "Select the current article.
6403 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6404 non-nil, the article will be re-fetched even if it already present in
6405 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6406 be displayed."
6407   ;; Make sure we are in the summary buffer to work around bbdb bug.
6408   (unless (eq major-mode 'gnus-summary-mode)
6409     (set-buffer gnus-summary-buffer))
6410   (let ((article (or article (gnus-summary-article-number)))
6411         (all-headers (not (not all-headers))) ;Must be T or NIL.
6412         gnus-summary-display-article-function)
6413     (and (not pseudo)
6414          (gnus-summary-article-pseudo-p article)
6415          (error "This is a pseudo-article"))
6416     (save-excursion
6417       (set-buffer gnus-summary-buffer)
6418       (if (or (and gnus-single-article-buffer
6419                    (or (null gnus-current-article)
6420                        (null gnus-article-current)
6421                        (null (get-buffer gnus-article-buffer))
6422                        (not (eq article (cdr gnus-article-current)))
6423                        (not (equal (car gnus-article-current)
6424                                    gnus-newsgroup-name))))
6425               (and (not gnus-single-article-buffer)
6426                    (or (null gnus-current-article)
6427                        (not (eq gnus-current-article article))))
6428               force)
6429           ;; The requested article is different from the current article.
6430           (progn
6431             (gnus-summary-display-article article all-headers)
6432             (when (or all-headers gnus-show-all-headers)
6433               (gnus-article-show-all-headers))
6434             (gnus-article-set-window-start
6435              (cdr (assq article gnus-newsgroup-bookmarks)))
6436             article)
6437         (when (or all-headers gnus-show-all-headers)
6438           (gnus-article-show-all-headers))
6439         'old))))
6440
6441 (defun gnus-summary-force-verify-and-decrypt ()
6442   (interactive)
6443   (let ((mm-verify-option 'known)
6444         (mm-decrypt-option 'known))
6445     (gnus-summary-select-article nil 'force)))
6446
6447 (defun gnus-summary-set-current-mark (&optional current-mark)
6448   "Obsolete function."
6449   nil)
6450
6451 (defun gnus-summary-next-article (&optional unread subject backward push)
6452   "Select the next article.
6453 If UNREAD, only unread articles are selected.
6454 If SUBJECT, only articles with SUBJECT are selected.
6455 If BACKWARD, the previous article is selected instead of the next."
6456   (interactive "P")
6457   (cond
6458    ;; Is there such an article?
6459    ((and (gnus-summary-search-forward unread subject backward)
6460          (or (gnus-summary-display-article (gnus-summary-article-number))
6461              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6462     (gnus-summary-position-point))
6463    ;; If not, we try the first unread, if that is wanted.
6464    ((and subject
6465          gnus-auto-select-same
6466          (gnus-summary-first-unread-article))
6467     (gnus-summary-position-point)
6468     (gnus-message 6 "Wrapped"))
6469    ;; Try to get next/previous article not displayed in this group.
6470    ((and gnus-auto-extend-newsgroup
6471          (not unread) (not subject))
6472     (gnus-summary-goto-article
6473      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6474      nil (count-lines (point-min) (point))))
6475    ;; Go to next/previous group.
6476    (t
6477     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6478       (gnus-summary-jump-to-group gnus-newsgroup-name))
6479     (let ((cmd last-command-char)
6480           (point
6481            (save-excursion
6482              (set-buffer gnus-group-buffer)
6483              (point)))
6484           (group
6485            (if (eq gnus-keep-same-level 'best)
6486                (gnus-summary-best-group gnus-newsgroup-name)
6487              (gnus-summary-search-group backward gnus-keep-same-level))))
6488       ;; For some reason, the group window gets selected.  We change
6489       ;; it back.
6490       (select-window (get-buffer-window (current-buffer)))
6491       ;; Select next unread newsgroup automagically.
6492       (cond
6493        ((or (not gnus-auto-select-next)
6494             (not cmd))
6495         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6496        ((or (eq gnus-auto-select-next 'quietly)
6497             (and (eq gnus-auto-select-next 'slightly-quietly)
6498                  push)
6499             (and (eq gnus-auto-select-next 'almost-quietly)
6500                  (gnus-summary-last-article-p)))
6501         ;; Select quietly.
6502         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6503             (gnus-summary-exit)
6504           (gnus-message 7 "No more%s articles (%s)..."
6505                         (if unread " unread" "")
6506                         (if group (concat "selecting " group)
6507                           "exiting"))
6508           (gnus-summary-next-group nil group backward)))
6509        (t
6510         (when (gnus-key-press-event-p last-input-event)
6511           (gnus-summary-walk-group-buffer
6512            gnus-newsgroup-name cmd unread backward point))))))))
6513
6514 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6515   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6516                       (?\C-p (gnus-group-prev-unread-group 1))))
6517         (cursor-in-echo-area t)
6518         keve key group ended)
6519     (save-excursion
6520       (set-buffer gnus-group-buffer)
6521       (goto-char start)
6522       (setq group
6523             (if (eq gnus-keep-same-level 'best)
6524                 (gnus-summary-best-group gnus-newsgroup-name)
6525               (gnus-summary-search-group backward gnus-keep-same-level))))
6526     (while (not ended)
6527       (gnus-message
6528        5 "No more%s articles%s" (if unread " unread" "")
6529        (if (and group
6530                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6531            (format " (Type %s for %s [%s])"
6532                    (single-key-description cmd) group
6533                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6534          (format " (Type %s to exit %s)"
6535                  (single-key-description cmd)
6536                  gnus-newsgroup-name)))
6537       ;; Confirm auto selection.
6538       (setq key (car (setq keve (gnus-read-event-char))))
6539       (setq ended t)
6540       (cond
6541        ((assq key keystrokes)
6542         (let ((obuf (current-buffer)))
6543           (switch-to-buffer gnus-group-buffer)
6544           (when group
6545             (gnus-group-jump-to-group group))
6546           (eval (cadr (assq key keystrokes)))
6547           (setq group (gnus-group-group-name))
6548           (switch-to-buffer obuf))
6549         (setq ended nil))
6550        ((equal key cmd)
6551         (if (or (not group)
6552                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6553             (gnus-summary-exit)
6554           (gnus-summary-next-group nil group backward)))
6555        (t
6556         (push (cdr keve) unread-command-events))))))
6557
6558 (defun gnus-summary-next-unread-article ()
6559   "Select unread article after current one."
6560   (interactive)
6561   (gnus-summary-next-article
6562    (or (not (eq gnus-summary-goto-unread 'never))
6563        (gnus-summary-last-article-p (gnus-summary-article-number)))
6564    (and gnus-auto-select-same
6565         (gnus-summary-article-subject))))
6566
6567 (defun gnus-summary-prev-article (&optional unread subject)
6568   "Select the article after the current one.
6569 If UNREAD is non-nil, only unread articles are selected."
6570   (interactive "P")
6571   (gnus-summary-next-article unread subject t))
6572
6573 (defun gnus-summary-prev-unread-article ()
6574   "Select unread article before current one."
6575   (interactive)
6576   (gnus-summary-prev-article
6577    (or (not (eq gnus-summary-goto-unread 'never))
6578        (gnus-summary-first-article-p (gnus-summary-article-number)))
6579    (and gnus-auto-select-same
6580         (gnus-summary-article-subject))))
6581
6582 (defun gnus-summary-next-page (&optional lines circular)
6583   "Show next page of the selected article.
6584 If at the end of the current article, select the next article.
6585 LINES says how many lines should be scrolled up.
6586
6587 If CIRCULAR is non-nil, go to the start of the article instead of
6588 selecting the next article when reaching the end of the current
6589 article."
6590   (interactive "P")
6591   (setq gnus-summary-buffer (current-buffer))
6592   (gnus-set-global-variables)
6593   (let ((article (gnus-summary-article-number))
6594         (article-window (get-buffer-window gnus-article-buffer t))
6595         endp)
6596     ;; If the buffer is empty, we have no article.
6597     (unless article
6598       (error "No article to select"))
6599     (gnus-configure-windows 'article)
6600     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6601         (if (and (eq gnus-summary-goto-unread 'never)
6602                  (not (gnus-summary-last-article-p article)))
6603             (gnus-summary-next-article)
6604           (gnus-summary-next-unread-article))
6605       (if (or (null gnus-current-article)
6606               (null gnus-article-current)
6607               (/= article (cdr gnus-article-current))
6608               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6609           ;; Selected subject is different from current article's.
6610           (gnus-summary-display-article article)
6611         (when article-window
6612           (gnus-eval-in-buffer-window gnus-article-buffer
6613             (setq endp (gnus-article-next-page lines)))
6614           (when endp
6615             (cond (circular
6616                    (gnus-summary-beginning-of-article))
6617                   (lines
6618                    (gnus-message 3 "End of message"))
6619                   ((null lines)
6620                    (if (and (eq gnus-summary-goto-unread 'never)
6621                             (not (gnus-summary-last-article-p article)))
6622                        (gnus-summary-next-article)
6623                      (gnus-summary-next-unread-article))))))))
6624     (gnus-summary-recenter)
6625     (gnus-summary-position-point)))
6626
6627 (defun gnus-summary-prev-page (&optional lines move)
6628   "Show previous page of selected article.
6629 Argument LINES specifies lines to be scrolled down.
6630 If MOVE, move to the previous unread article if point is at
6631 the beginning of the buffer."
6632   (interactive "P")
6633   (let ((article (gnus-summary-article-number))
6634         (article-window (get-buffer-window gnus-article-buffer t))
6635         endp)
6636     (gnus-configure-windows 'article)
6637     (if (or (null gnus-current-article)
6638             (null gnus-article-current)
6639             (/= article (cdr gnus-article-current))
6640             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6641         ;; Selected subject is different from current article's.
6642         (gnus-summary-display-article article)
6643       (gnus-summary-recenter)
6644       (when article-window
6645         (gnus-eval-in-buffer-window gnus-article-buffer
6646           (setq endp (gnus-article-prev-page lines)))
6647         (when (and move endp)
6648           (cond (lines
6649                  (gnus-message 3 "Beginning of message"))
6650                 ((null lines)
6651                  (if (and (eq gnus-summary-goto-unread 'never)
6652                           (not (gnus-summary-first-article-p article)))
6653                      (gnus-summary-prev-article)
6654                    (gnus-summary-prev-unread-article))))))))
6655   (gnus-summary-position-point))
6656
6657 (defun gnus-summary-prev-page-or-article (&optional lines)
6658   "Show previous page of selected article.
6659 Argument LINES specifies lines to be scrolled down.
6660 If at the beginning of the article, go to the next article."
6661   (interactive "P")
6662   (gnus-summary-prev-page lines t))
6663
6664 (defun gnus-summary-scroll-up (lines)
6665   "Scroll up (or down) one line current article.
6666 Argument LINES specifies lines to be scrolled up (or down if negative)."
6667   (interactive "p")
6668   (gnus-configure-windows 'article)
6669   (gnus-summary-show-thread)
6670   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6671     (gnus-eval-in-buffer-window gnus-article-buffer
6672       (cond ((> lines 0)
6673              (when (gnus-article-next-page lines)
6674                (gnus-message 3 "End of message")))
6675             ((< lines 0)
6676              (gnus-article-prev-page (- lines))))))
6677   (gnus-summary-recenter)
6678   (gnus-summary-position-point))
6679
6680 (defun gnus-summary-scroll-down (lines)
6681   "Scroll down (or up) one line current article.
6682 Argument LINES specifies lines to be scrolled down (or up if negative)."
6683   (interactive "p")
6684   (gnus-summary-scroll-up (- lines)))
6685
6686 (defun gnus-summary-next-same-subject ()
6687   "Select next article which has the same subject as current one."
6688   (interactive)
6689   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6690
6691 (defun gnus-summary-prev-same-subject ()
6692   "Select previous article which has the same subject as current one."
6693   (interactive)
6694   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6695
6696 (defun gnus-summary-next-unread-same-subject ()
6697   "Select next unread article which has the same subject as current one."
6698   (interactive)
6699   (gnus-summary-next-article t (gnus-summary-article-subject)))
6700
6701 (defun gnus-summary-prev-unread-same-subject ()
6702   "Select previous unread article which has the same subject as current one."
6703   (interactive)
6704   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6705
6706 (defun gnus-summary-first-unread-article ()
6707   "Select the first unread article.
6708 Return nil if there are no unread articles."
6709   (interactive)
6710   (prog1
6711       (when (gnus-summary-first-subject t)
6712         (gnus-summary-show-thread)
6713         (gnus-summary-first-subject t)
6714         (gnus-summary-display-article (gnus-summary-article-number)))
6715     (gnus-summary-position-point)))
6716
6717 (defun gnus-summary-first-unread-subject ()
6718   "Place the point on the subject line of the first unread article.
6719 Return nil if there are no unread articles."
6720   (interactive)
6721   (prog1
6722       (when (gnus-summary-first-subject t)
6723         (gnus-summary-show-thread)
6724         (gnus-summary-first-subject t))
6725     (gnus-summary-position-point)))
6726
6727 (defun gnus-summary-first-article ()
6728   "Select the first article.
6729 Return nil if there are no articles."
6730   (interactive)
6731   (prog1
6732       (when (gnus-summary-first-subject)
6733         (gnus-summary-show-thread)
6734         (gnus-summary-first-subject)
6735         (gnus-summary-display-article (gnus-summary-article-number)))
6736     (gnus-summary-position-point)))
6737
6738 (defun gnus-summary-best-unread-article ()
6739   "Select the unread article with the highest score."
6740   (interactive)
6741   (let ((best -1000000)
6742         (data gnus-newsgroup-data)
6743         article score)
6744     (while data
6745       (and (gnus-data-unread-p (car data))
6746            (> (setq score
6747                     (gnus-summary-article-score (gnus-data-number (car data))))
6748               best)
6749            (setq best score
6750                  article (gnus-data-number (car data))))
6751       (setq data (cdr data)))
6752     (prog1
6753         (if article
6754             (gnus-summary-goto-article article)
6755           (error "No unread articles"))
6756       (gnus-summary-position-point))))
6757
6758 (defun gnus-summary-last-subject ()
6759   "Go to the last displayed subject line in the group."
6760   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6761     (when article
6762       (gnus-summary-goto-subject article))))
6763
6764 (defun gnus-summary-goto-article (article &optional all-headers force)
6765   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6766 If ALL-HEADERS is non-nil, no header lines are hidden.
6767 If FORCE, go to the article even if it isn't displayed.  If FORCE
6768 is a number, it is the line the article is to be displayed on."
6769   (interactive
6770    (list
6771     (completing-read
6772      "Article number or Message-ID: "
6773      (mapcar (lambda (number) (list (int-to-string number)))
6774              gnus-newsgroup-limit))
6775     current-prefix-arg
6776     t))
6777   (prog1
6778       (if (and (stringp article)
6779                (string-match "@" article))
6780           (gnus-summary-refer-article article)
6781         (when (stringp article)
6782           (setq article (string-to-number article)))
6783         (if (gnus-summary-goto-subject article force)
6784             (gnus-summary-display-article article all-headers)
6785           (gnus-message 4 "Couldn't go to article %s" article) nil))
6786     (gnus-summary-position-point)))
6787
6788 (defun gnus-summary-goto-last-article ()
6789   "Go to the previously read article."
6790   (interactive)
6791   (prog1
6792       (when gnus-last-article
6793         (gnus-summary-goto-article gnus-last-article nil t))
6794     (gnus-summary-position-point)))
6795
6796 (defun gnus-summary-pop-article (number)
6797   "Pop one article off the history and go to the previous.
6798 NUMBER articles will be popped off."
6799   (interactive "p")
6800   (let (to)
6801     (setq gnus-newsgroup-history
6802           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6803     (if to
6804         (gnus-summary-goto-article (car to) nil t)
6805       (error "Article history empty")))
6806   (gnus-summary-position-point))
6807
6808 ;; Summary commands and functions for limiting the summary buffer.
6809
6810 (defun gnus-summary-limit-to-articles (n)
6811   "Limit the summary buffer to the next N articles.
6812 If not given a prefix, use the process marked articles instead."
6813   (interactive "P")
6814   (prog1
6815       (let ((articles (gnus-summary-work-articles n)))
6816         (setq gnus-newsgroup-processable nil)
6817         (gnus-summary-limit articles))
6818     (gnus-summary-position-point)))
6819
6820 (defun gnus-summary-pop-limit (&optional total)
6821   "Restore the previous limit.
6822 If given a prefix, remove all limits."
6823   (interactive "P")
6824   (when total
6825     (setq gnus-newsgroup-limits
6826           (list (mapcar (lambda (h) (mail-header-number h))
6827                         gnus-newsgroup-headers))))
6828   (unless gnus-newsgroup-limits
6829     (error "No limit to pop"))
6830   (prog1
6831       (gnus-summary-limit nil 'pop)
6832     (gnus-summary-position-point)))
6833
6834 (defun gnus-summary-limit-to-subject (subject &optional header)
6835   "Limit the summary buffer to articles that have subjects that match a regexp."
6836   (interactive "sLimit to subject (regexp): ")
6837   (unless header
6838     (setq header "subject"))
6839   (when (not (equal "" subject))
6840     (prog1
6841         (let ((articles (gnus-summary-find-matching
6842                          (or header "subject") subject 'all)))
6843           (unless articles
6844             (error "Found no matches for \"%s\"" subject))
6845           (gnus-summary-limit articles))
6846       (gnus-summary-position-point))))
6847
6848 (defun gnus-summary-limit-to-author (from)
6849   "Limit the summary buffer to articles that have authors that match a regexp."
6850   (interactive "sLimit to author (regexp): ")
6851   (gnus-summary-limit-to-subject from "from"))
6852
6853 (defun gnus-summary-limit-to-age (age &optional younger-p)
6854   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6855 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6856 articles that are younger than AGE days."
6857   (interactive
6858    (let ((younger current-prefix-arg)
6859          (days-got nil)
6860          days)
6861      (while (not days-got)
6862        (setq days (if younger
6863                       (read-string "Limit to articles within (in days): ")
6864                     (read-string "Limit to articles older than (in days): ")))
6865        (when (> (length days) 0)
6866          (setq days (read days)))
6867        (if (numberp days)
6868            (setq days-got t)
6869          (message "Please enter a number.")
6870          (sleep-for 1)))
6871      (list days younger)))
6872   (prog1
6873       (let ((data gnus-newsgroup-data)
6874             (cutoff (days-to-time age))
6875             articles d date is-younger)
6876         (while (setq d (pop data))
6877           (when (and (vectorp (gnus-data-header d))
6878                      (setq date (mail-header-date (gnus-data-header d))))
6879             (setq is-younger (time-less-p
6880                               (time-since (condition-case ()
6881                                               (date-to-time date)
6882                                             (error '(0 0))))
6883                               cutoff))
6884             (when (if younger-p
6885                       is-younger
6886                     (not is-younger))
6887               (push (gnus-data-number d) articles))))
6888         (gnus-summary-limit (nreverse articles)))
6889     (gnus-summary-position-point)))
6890
6891 (defun gnus-summary-limit-to-extra (header regexp)
6892   "Limit the summary buffer to articles that match an 'extra' header."
6893   (interactive
6894    (let ((header
6895           (intern
6896            (gnus-completing-read
6897             (symbol-name (car gnus-extra-headers))
6898             "Limit extra header:"
6899             (mapcar (lambda (x)
6900                       (cons (symbol-name x) x))
6901                     gnus-extra-headers)
6902             nil
6903             t))))
6904      (list header
6905            (read-string (format "Limit to header %s (regexp): " header)))))
6906   (when (not (equal "" regexp))
6907     (prog1
6908         (let ((articles (gnus-summary-find-matching
6909                          (cons 'extra header) regexp 'all)))
6910           (unless articles
6911             (error "Found no matches for \"%s\"" regexp))
6912           (gnus-summary-limit articles))
6913       (gnus-summary-position-point))))
6914
6915 (defun gnus-summary-limit-to-display-predicate ()
6916   "Limit the summary buffer to the predicated in the `display' group parameter."
6917   (interactive)
6918   (unless gnus-newsgroup-display
6919     (error "There is no `diplay' group parameter"))
6920   (let (articles)
6921     (dolist (number gnus-newsgroup-articles)
6922       (when (funcall gnus-newsgroup-display)
6923         (push number articles)))
6924     (gnus-summary-limit articles))
6925   (gnus-summary-position-point))
6926
6927 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6928 (make-obsolete
6929  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6930
6931 (defun gnus-summary-limit-to-unread (&optional all)
6932   "Limit the summary buffer to articles that are not marked as read.
6933 If ALL is non-nil, limit strictly to unread articles."
6934   (interactive "P")
6935   (if all
6936       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6937     (gnus-summary-limit-to-marks
6938      ;; Concat all the marks that say that an article is read and have
6939      ;; those removed.
6940      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6941            gnus-killed-mark gnus-kill-file-mark
6942            gnus-low-score-mark gnus-expirable-mark
6943            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6944            gnus-duplicate-mark gnus-souped-mark)
6945      'reverse)))
6946
6947 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6948 (make-obsolete 'gnus-summary-delete-marked-with
6949                'gnus-summary-limit-exlude-marks)
6950
6951 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6952   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6953 If REVERSE, limit the summary buffer to articles that are marked
6954 with MARKS.  MARKS can either be a string of marks or a list of marks.
6955 Returns how many articles were removed."
6956   (interactive "sMarks: ")
6957   (gnus-summary-limit-to-marks marks t))
6958
6959 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6960   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6961 If REVERSE (the prefix), limit the summary buffer to articles that are
6962 not marked with MARKS.  MARKS can either be a string of marks or a
6963 list of marks.
6964 Returns how many articles were removed."
6965   (interactive "sMarks: \nP")
6966   (prog1
6967       (let ((data gnus-newsgroup-data)
6968             (marks (if (listp marks) marks
6969                      (append marks nil))) ; Transform to list.
6970             articles)
6971         (while data
6972           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6973                   (memq (gnus-data-mark (car data)) marks))
6974             (push (gnus-data-number (car data)) articles))
6975           (setq data (cdr data)))
6976         (gnus-summary-limit articles))
6977     (gnus-summary-position-point)))
6978
6979 (defun gnus-summary-limit-to-score (&optional score)
6980   "Limit to articles with score at or above SCORE."
6981   (interactive "P")
6982   (setq score (if score
6983                   (prefix-numeric-value score)
6984                 (or gnus-summary-default-score 0)))
6985   (let ((data gnus-newsgroup-data)
6986         articles)
6987     (while data
6988       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6989                 score)
6990         (push (gnus-data-number (car data)) articles))
6991       (setq data (cdr data)))
6992     (prog1
6993         (gnus-summary-limit articles)
6994       (gnus-summary-position-point))))
6995
6996 (defun gnus-summary-limit-include-thread (id)
6997   "Display all the hidden articles that is in the thread with ID in it.
6998 When called interactively, ID is the Message-ID of the current
6999 article."
7000   (interactive (list (mail-header-id (gnus-summary-article-header))))
7001   (let ((articles (gnus-articles-in-thread
7002                    (gnus-id-to-thread (gnus-root-id id)))))
7003     (prog1
7004         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7005       (gnus-summary-limit-include-matching-articles
7006        "subject"
7007        (regexp-quote (gnus-simplify-subject-re
7008                       (mail-header-subject (gnus-id-to-header id)))))
7009       (gnus-summary-position-point))))
7010
7011 (defun gnus-summary-limit-include-matching-articles (header regexp)
7012   "Display all the hidden articles that have HEADERs that match REGEXP."
7013   (interactive (list (read-string "Match on header: ")
7014                      (read-string "Regexp: ")))
7015   (let ((articles (gnus-find-matching-articles header regexp)))
7016     (prog1
7017         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7018       (gnus-summary-position-point))))
7019
7020 (defun gnus-summary-limit-include-dormant ()
7021   "Display all the hidden articles that are marked as dormant.
7022 Note that this command only works on a subset of the articles currently
7023 fetched for this group."
7024   (interactive)
7025   (unless gnus-newsgroup-dormant
7026     (error "There are no dormant articles in this group"))
7027   (prog1
7028       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7029     (gnus-summary-position-point)))
7030
7031 (defun gnus-summary-limit-exclude-dormant ()
7032   "Hide all dormant articles."
7033   (interactive)
7034   (prog1
7035       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7036     (gnus-summary-position-point)))
7037
7038 (defun gnus-summary-limit-exclude-childless-dormant ()
7039   "Hide all dormant articles that have no children."
7040   (interactive)
7041   (let ((data (gnus-data-list t))
7042         articles d children)
7043     ;; Find all articles that are either not dormant or have
7044     ;; children.
7045     (while (setq d (pop data))
7046       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7047                 (and (setq children
7048                            (gnus-article-children (gnus-data-number d)))
7049                      (let (found)
7050                        (while children
7051                          (when (memq (car children) articles)
7052                            (setq children nil
7053                                  found t))
7054                          (pop children))
7055                        found)))
7056         (push (gnus-data-number d) articles)))
7057     ;; Do the limiting.
7058     (prog1
7059         (gnus-summary-limit articles)
7060       (gnus-summary-position-point))))
7061
7062 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7063   "Mark all unread excluded articles as read.
7064 If ALL, mark even excluded ticked and dormants as read."
7065   (interactive "P")
7066   (let ((articles (gnus-sorted-complement
7067                    (sort
7068                     (mapcar (lambda (h) (mail-header-number h))
7069                             gnus-newsgroup-headers)
7070                     '<)
7071                    (sort gnus-newsgroup-limit '<)))
7072         article)
7073     (setq gnus-newsgroup-unreads
7074           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7075     (if all
7076         (setq gnus-newsgroup-dormant nil
7077               gnus-newsgroup-marked nil
7078               gnus-newsgroup-reads
7079               (nconc
7080                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7081                gnus-newsgroup-reads))
7082       (while (setq article (pop articles))
7083         (unless (or (memq article gnus-newsgroup-dormant)
7084                     (memq article gnus-newsgroup-marked))
7085           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7086
7087 (defun gnus-summary-limit (articles &optional pop)
7088   (if pop
7089       ;; We pop the previous limit off the stack and use that.
7090       (setq articles (car gnus-newsgroup-limits)
7091             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7092     ;; We use the new limit, so we push the old limit on the stack.
7093     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7094   ;; Set the limit.
7095   (setq gnus-newsgroup-limit articles)
7096   (let ((total (length gnus-newsgroup-data))
7097         (data (gnus-data-find-list (gnus-summary-article-number)))
7098         (gnus-summary-mark-below nil)   ; Inhibit this.
7099         found)
7100     ;; This will do all the work of generating the new summary buffer
7101     ;; according to the new limit.
7102     (gnus-summary-prepare)
7103     ;; Hide any threads, possibly.
7104     (and gnus-show-threads
7105          gnus-thread-hide-subtree
7106          (gnus-summary-hide-all-threads))
7107     ;; Try to return to the article you were at, or one in the
7108     ;; neighborhood.
7109     (when data
7110       ;; We try to find some article after the current one.
7111       (while data
7112         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7113           (setq data nil
7114                 found t))
7115         (setq data (cdr data))))
7116     (unless found
7117       ;; If there is no data, that means that we were after the last
7118       ;; article.  The same goes when we can't find any articles
7119       ;; after the current one.
7120       (goto-char (point-max))
7121       (gnus-summary-find-prev))
7122     (gnus-set-mode-line 'summary)
7123     ;; We return how many articles were removed from the summary
7124     ;; buffer as a result of the new limit.
7125     (- total (length gnus-newsgroup-data))))
7126
7127 (defsubst gnus-invisible-cut-children (threads)
7128   (let ((num 0))
7129     (while threads
7130       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7131         (incf num))
7132       (pop threads))
7133     (< num 2)))
7134
7135 (defsubst gnus-cut-thread (thread)
7136   "Go forwards in the thread until we find an article that we want to display."
7137   (when (or (eq gnus-fetch-old-headers 'some)
7138             (eq gnus-fetch-old-headers 'invisible)
7139             (numberp gnus-fetch-old-headers)
7140             (eq gnus-build-sparse-threads 'some)
7141             (eq gnus-build-sparse-threads 'more))
7142     ;; Deal with old-fetched headers and sparse threads.
7143     (while (and
7144             thread
7145             (or
7146              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7147              (gnus-summary-article-ancient-p
7148               (mail-header-number (car thread))))
7149             (if (or (<= (length (cdr thread)) 1)
7150                     (eq gnus-fetch-old-headers 'invisible))
7151                 (setq gnus-newsgroup-limit
7152                       (delq (mail-header-number (car thread))
7153                             gnus-newsgroup-limit)
7154                       thread (cadr thread))
7155               (when (gnus-invisible-cut-children (cdr thread))
7156                 (let ((th (cdr thread)))
7157                   (while th
7158                     (if (memq (mail-header-number (caar th))
7159                               gnus-newsgroup-limit)
7160                         (setq thread (car th)
7161                               th nil)
7162                       (setq th (cdr th))))))))))
7163   thread)
7164
7165 (defun gnus-cut-threads (threads)
7166   "Cut off all uninteresting articles from the beginning of threads."
7167   (when (or (eq gnus-fetch-old-headers 'some)
7168             (eq gnus-fetch-old-headers 'invisible)
7169             (numberp gnus-fetch-old-headers)
7170             (eq gnus-build-sparse-threads 'some)
7171             (eq gnus-build-sparse-threads 'more))
7172     (let ((th threads))
7173       (while th
7174         (setcar th (gnus-cut-thread (car th)))
7175         (setq th (cdr th)))))
7176   ;; Remove nixed out threads.
7177   (delq nil threads))
7178
7179 (defun gnus-summary-initial-limit (&optional show-if-empty)
7180   "Figure out what the initial limit is supposed to be on group entry.
7181 This entails weeding out unwanted dormants, low-scored articles,
7182 fetch-old-headers verbiage, and so on."
7183   ;; Most groups have nothing to remove.
7184   (if (or gnus-inhibit-limiting
7185           (and (null gnus-newsgroup-dormant)
7186                (eq gnus-newsgroup-display 'gnus-not-ignore)
7187                (not (eq gnus-fetch-old-headers 'some))
7188                (not (numberp gnus-fetch-old-headers))
7189                (not (eq gnus-fetch-old-headers 'invisible))
7190                (null gnus-summary-expunge-below)
7191                (not (eq gnus-build-sparse-threads 'some))
7192                (not (eq gnus-build-sparse-threads 'more))
7193                (null gnus-thread-expunge-below)
7194                (not gnus-use-nocem)))
7195       ()                                ; Do nothing.
7196     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7197     (setq gnus-newsgroup-limit nil)
7198     (mapatoms
7199      (lambda (node)
7200        (unless (car (symbol-value node))
7201          ;; These threads have no parents -- they are roots.
7202          (let ((nodes (cdr (symbol-value node)))
7203                thread)
7204            (while nodes
7205              (if (and gnus-thread-expunge-below
7206                       (< (gnus-thread-total-score (car nodes))
7207                          gnus-thread-expunge-below))
7208                  (gnus-expunge-thread (pop nodes))
7209                (setq thread (pop nodes))
7210                (gnus-summary-limit-children thread))))))
7211      gnus-newsgroup-dependencies)
7212     ;; If this limitation resulted in an empty group, we might
7213     ;; pop the previous limit and use it instead.
7214     (when (and (not gnus-newsgroup-limit)
7215                show-if-empty)
7216       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7217     gnus-newsgroup-limit))
7218
7219 (defun gnus-summary-limit-children (thread)
7220   "Return 1 if this subthread is visible and 0 if it is not."
7221   ;; First we get the number of visible children to this thread.  This
7222   ;; is done by recursing down the thread using this function, so this
7223   ;; will really go down to a leaf article first, before slowly
7224   ;; working its way up towards the root.
7225   (when thread
7226     (let ((children
7227            (if (cdr thread)
7228                (apply '+ (mapcar 'gnus-summary-limit-children
7229                                  (cdr thread)))
7230              0))
7231           (number (mail-header-number (car thread)))
7232           score)
7233       (if (and
7234            (not (memq number gnus-newsgroup-marked))
7235            (or
7236             ;; If this article is dormant and has absolutely no visible
7237             ;; children, then this article isn't visible.
7238             (and (memq number gnus-newsgroup-dormant)
7239                  (zerop children))
7240             ;; If this is "fetch-old-headered" and there is no
7241             ;; visible children, then we don't want this article.
7242             (and (or (eq gnus-fetch-old-headers 'some)
7243                      (numberp gnus-fetch-old-headers))
7244                  (gnus-summary-article-ancient-p number)
7245                  (zerop children))
7246             ;; If this is "fetch-old-headered" and `invisible', then
7247             ;; we don't want this article.
7248             (and (eq gnus-fetch-old-headers 'invisible)
7249                  (gnus-summary-article-ancient-p number))
7250             ;; If this is a sparsely inserted article with no children,
7251             ;; we don't want it.
7252             (and (eq gnus-build-sparse-threads 'some)
7253                  (gnus-summary-article-sparse-p number)
7254                  (zerop children))
7255             ;; If we use expunging, and this article is really
7256             ;; low-scored, then we don't want this article.
7257             (when (and gnus-summary-expunge-below
7258                        (< (setq score
7259                                 (or (cdr (assq number gnus-newsgroup-scored))
7260                                     gnus-summary-default-score))
7261                           gnus-summary-expunge-below))
7262               ;; We increase the expunge-tally here, but that has
7263               ;; nothing to do with the limits, really.
7264               (incf gnus-newsgroup-expunged-tally)
7265               ;; We also mark as read here, if that's wanted.
7266               (when (and gnus-summary-mark-below
7267                          (< score gnus-summary-mark-below))
7268                 (setq gnus-newsgroup-unreads
7269                       (delq number gnus-newsgroup-unreads))
7270                 (if gnus-newsgroup-auto-expire
7271                     (push number gnus-newsgroup-expirable)
7272                   (push (cons number gnus-low-score-mark)
7273                         gnus-newsgroup-reads)))
7274               t)
7275             ;; Do the `display' group parameter.
7276             (and gnus-newsgroup-display
7277                  (not (funcall gnus-newsgroup-display)))
7278             ;; Check NoCeM things.
7279             (if (and gnus-use-nocem
7280                      (gnus-nocem-unwanted-article-p
7281                       (mail-header-id (car thread))))
7282                 (progn
7283                   (setq gnus-newsgroup-unreads
7284                         (delq number gnus-newsgroup-unreads))
7285                   t))))
7286           ;; Nope, invisible article.
7287           0
7288         ;; Ok, this article is to be visible, so we add it to the limit
7289         ;; and return 1.
7290         (push number gnus-newsgroup-limit)
7291         1))))
7292
7293 (defun gnus-expunge-thread (thread)
7294   "Mark all articles in THREAD as read."
7295   (let* ((number (mail-header-number (car thread))))
7296     (incf gnus-newsgroup-expunged-tally)
7297     ;; We also mark as read here, if that's wanted.
7298     (setq gnus-newsgroup-unreads
7299           (delq number gnus-newsgroup-unreads))
7300     (if gnus-newsgroup-auto-expire
7301         (push number gnus-newsgroup-expirable)
7302       (push (cons number gnus-low-score-mark)
7303             gnus-newsgroup-reads)))
7304   ;; Go recursively through all subthreads.
7305   (mapcar 'gnus-expunge-thread (cdr thread)))
7306
7307 ;; Summary article oriented commands
7308
7309 (defun gnus-summary-refer-parent-article (n)
7310   "Refer parent article N times.
7311 If N is negative, go to ancestor -N instead.
7312 The difference between N and the number of articles fetched is returned."
7313   (interactive "p")
7314   (let ((skip 1)
7315         error header ref)
7316     (when (not (natnump n))
7317       (setq skip (abs n)
7318             n 1))
7319     (while (and (> n 0)
7320                 (not error))
7321       (setq header (gnus-summary-article-header))
7322       (if (and (eq (mail-header-number header)
7323                    (cdr gnus-article-current))
7324                (equal gnus-newsgroup-name
7325                       (car gnus-article-current)))
7326           ;; If we try to find the parent of the currently
7327           ;; displayed article, then we take a look at the actual
7328           ;; References header, since this is slightly more
7329           ;; reliable than the References field we got from the
7330           ;; server.
7331           (save-excursion
7332             (set-buffer gnus-original-article-buffer)
7333             (nnheader-narrow-to-headers)
7334             (unless (setq ref (message-fetch-field "references"))
7335               (setq ref (message-fetch-field "in-reply-to")))
7336             (widen))
7337         (setq ref
7338               ;; It's not the current article, so we take a bet on
7339               ;; the value we got from the server.
7340               (mail-header-references header)))
7341       (if (and ref
7342                (not (equal ref "")))
7343           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7344             (gnus-message 1 "Couldn't find parent"))
7345         (gnus-message 1 "No references in article %d"
7346                       (gnus-summary-article-number))
7347         (setq error t))
7348       (decf n))
7349     (gnus-summary-position-point)
7350     n))
7351
7352 (defun gnus-summary-refer-references ()
7353   "Fetch all articles mentioned in the References header.
7354 Return the number of articles fetched."
7355   (interactive)
7356   (let ((ref (mail-header-references (gnus-summary-article-header)))
7357         (current (gnus-summary-article-number))
7358         (n 0))
7359     (if (or (not ref)
7360             (equal ref ""))
7361         (error "No References in the current article")
7362       ;; For each Message-ID in the References header...
7363       (while (string-match "<[^>]*>" ref)
7364         (incf n)
7365         ;; ... fetch that article.
7366         (gnus-summary-refer-article
7367          (prog1 (match-string 0 ref)
7368            (setq ref (substring ref (match-end 0))))))
7369       (gnus-summary-goto-subject current)
7370       (gnus-summary-position-point)
7371       n)))
7372
7373 (defun gnus-summary-refer-thread (&optional limit)
7374   "Fetch all articles in the current thread.
7375 If LIMIT (the numerical prefix), fetch that many old headers instead
7376 of what's specified by the `gnus-refer-thread-limit' variable."
7377   (interactive "P")
7378   (let ((id (mail-header-id (gnus-summary-article-header)))
7379         (limit (if limit (prefix-numeric-value limit)
7380                  gnus-refer-thread-limit)))
7381     ;; We want to fetch LIMIT *old* headers, but we also have to
7382     ;; re-fetch all the headers in the current buffer, because many of
7383     ;; them may be undisplayed.  So we adjust LIMIT.
7384     (when (numberp limit)
7385       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7386     (unless (eq gnus-fetch-old-headers 'invisible)
7387       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7388       ;; Retrieve the headers and read them in.
7389       (if (eq (gnus-retrieve-headers
7390                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7391               'nov)
7392           (gnus-build-all-threads)
7393         (error "Can't fetch thread from backends that don't support NOV"))
7394       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7395     (gnus-summary-limit-include-thread id)))
7396
7397 (defun gnus-summary-refer-article (message-id)
7398   "Fetch an article specified by MESSAGE-ID."
7399   (interactive "sMessage-ID: ")
7400   (when (and (stringp message-id)
7401              (not (zerop (length message-id))))
7402     ;; Construct the correct Message-ID if necessary.
7403     ;; Suggested by tale@pawl.rpi.edu.
7404     (unless (string-match "^<" message-id)
7405       (setq message-id (concat "<" message-id)))
7406     (unless (string-match ">$" message-id)
7407       (setq message-id (concat message-id ">")))
7408     (let* ((header (gnus-id-to-header message-id))
7409            (sparse (and header
7410                         (gnus-summary-article-sparse-p
7411                          (mail-header-number header))
7412                         (memq (mail-header-number header)
7413                               gnus-newsgroup-limit)))
7414            number)
7415       (cond
7416        ;; If the article is present in the buffer we just go to it.
7417        ((and header
7418              (or (not (gnus-summary-article-sparse-p
7419                        (mail-header-number header)))
7420                  sparse))
7421         (prog1
7422             (gnus-summary-goto-article
7423              (mail-header-number header) nil t)
7424           (when sparse
7425             (gnus-summary-update-article (mail-header-number header)))))
7426        (t
7427         ;; We fetch the article.
7428         (catch 'found
7429           (dolist (gnus-override-method (gnus-refer-article-methods))
7430             (gnus-check-server gnus-override-method)
7431             ;; Fetch the header, and display the article.
7432             (when (setq number (gnus-summary-insert-subject message-id))
7433               (gnus-summary-select-article nil nil nil number)
7434               (throw 'found t)))
7435           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7436
7437 (defun gnus-refer-article-methods ()
7438   "Return a list of referrable methods."
7439   (cond
7440    ;; No method, so we default to current and native.
7441    ((null gnus-refer-article-method)
7442     (list gnus-current-select-method gnus-select-method))
7443    ;; Current.
7444    ((eq 'current gnus-refer-article-method)
7445     (list gnus-current-select-method))
7446    ;; List of select methods.
7447    ((not (and (symbolp (car gnus-refer-article-method))
7448               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7449     (let (out)
7450       (dolist (method gnus-refer-article-method)
7451         (push (if (eq 'current method)
7452                   gnus-current-select-method
7453                 method)
7454               out))
7455       (nreverse out)))
7456    ;; One single select method.
7457    (t
7458     (list gnus-refer-article-method))))
7459
7460 (defun gnus-summary-edit-parameters ()
7461   "Edit the group parameters of the current group."
7462   (interactive)
7463   (gnus-group-edit-group gnus-newsgroup-name 'params))
7464
7465 (defun gnus-summary-customize-parameters ()
7466   "Customize the group parameters of the current group."
7467   (interactive)
7468   (gnus-group-customize gnus-newsgroup-name))
7469
7470 (defun gnus-summary-enter-digest-group (&optional force)
7471   "Enter an nndoc group based on the current article.
7472 If FORCE, force a digest interpretation.  If not, try
7473 to guess what the document format is."
7474   (interactive "P")
7475   (let ((conf gnus-current-window-configuration))
7476     (save-excursion
7477       (gnus-summary-select-article))
7478     (setq gnus-current-window-configuration conf)
7479     (let* ((name (format "%s-%d"
7480                          (gnus-group-prefixed-name
7481                           gnus-newsgroup-name (list 'nndoc ""))
7482                          (save-excursion
7483                            (set-buffer gnus-summary-buffer)
7484                            gnus-current-article)))
7485            (ogroup gnus-newsgroup-name)
7486            (params (append (gnus-info-params (gnus-get-info ogroup))
7487                            (list (cons 'to-group ogroup))
7488                            (list (cons 'save-article-group ogroup))))
7489            (case-fold-search t)
7490            (buf (current-buffer))
7491            dig to-address)
7492       (save-excursion
7493         (set-buffer gnus-original-article-buffer)
7494         ;; Have the digest group inherit the main mail address of
7495         ;; the parent article.
7496         (when (setq to-address (or (message-fetch-field "reply-to")
7497                                    (message-fetch-field "from")))
7498           (setq params (append
7499                         (list (cons 'to-address
7500                                     (funcall gnus-decode-encoded-word-function
7501                                              to-address))))))
7502         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7503         (insert-buffer-substring gnus-original-article-buffer)
7504         ;; Remove lines that may lead nndoc to misinterpret the
7505         ;; document type.
7506         (narrow-to-region
7507          (goto-char (point-min))
7508          (or (search-forward "\n\n" nil t) (point)))
7509         (goto-char (point-min))
7510         (delete-matching-lines "^Path:\\|^From ")
7511         (widen))
7512       (unwind-protect
7513           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7514                     (gnus-newsgroup-ephemeral-ignored-charsets
7515                      gnus-newsgroup-ignored-charsets))
7516                 (gnus-group-read-ephemeral-group
7517                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7518                               (nndoc-article-type
7519                                ,(if force 'mbox 'guess))) t))
7520               ;; Make all postings to this group go to the parent group.
7521               (nconc (gnus-info-params (gnus-get-info name))
7522                      params)
7523             ;; Couldn't select this doc group.
7524             (switch-to-buffer buf)
7525             (gnus-set-global-variables)
7526             (gnus-configure-windows 'summary)
7527             (gnus-message 3 "Article couldn't be entered?"))
7528         (kill-buffer dig)))))
7529
7530 (defun gnus-summary-read-document (n)
7531   "Open a new group based on the current article(s).
7532 This will allow you to read digests and other similar
7533 documents as newsgroups.
7534 Obeys the standard process/prefix convention."
7535   (interactive "P")
7536   (let* ((articles (gnus-summary-work-articles n))
7537          (ogroup gnus-newsgroup-name)
7538          (params (append (gnus-info-params (gnus-get-info ogroup))
7539                          (list (cons 'to-group ogroup))))
7540          article group egroup groups vgroup)
7541     (while (setq article (pop articles))
7542       (setq group (format "%s-%d" gnus-newsgroup-name article))
7543       (gnus-summary-remove-process-mark article)
7544       (when (gnus-summary-display-article article)
7545         (save-excursion
7546           (with-temp-buffer
7547             (insert-buffer-substring gnus-original-article-buffer)
7548             ;; Remove some headers that may lead nndoc to make
7549             ;; the wrong guess.
7550             (message-narrow-to-head)
7551             (goto-char (point-min))
7552             (delete-matching-lines "^\\(Path\\):\\|^From ")
7553             (widen)
7554             (if (setq egroup
7555                       (gnus-group-read-ephemeral-group
7556                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7557                                      (nndoc-article-type guess))
7558                        t nil t))
7559                 (progn
7560                   ;; Make all postings to this group go to the parent group.
7561                   (nconc (gnus-info-params (gnus-get-info egroup))
7562                          params)
7563                   (push egroup groups))
7564               ;; Couldn't select this doc group.
7565               (gnus-error 3 "Article couldn't be entered"))))))
7566     ;; Now we have selected all the documents.
7567     (cond
7568      ((not groups)
7569       (error "None of the articles could be interpreted as documents"))
7570      ((gnus-group-read-ephemeral-group
7571        (setq vgroup (format
7572                      "nnvirtual:%s-%s" gnus-newsgroup-name
7573                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7574        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7575        t
7576        (cons (current-buffer) 'summary)))
7577      (t
7578       (error "Couldn't select virtual nndoc group")))))
7579
7580 (defun gnus-summary-isearch-article (&optional regexp-p)
7581   "Do incremental search forward on the current article.
7582 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7583   (interactive "P")
7584   (let* ((gnus-inhibit-treatment t)
7585          (old (gnus-summary-select-article)))
7586     (gnus-configure-windows 'article)
7587     (gnus-eval-in-buffer-window gnus-article-buffer
7588       (save-restriction
7589         (widen)
7590         (when (eq 'old old)
7591           (gnus-article-show-all-headers))
7592         (goto-char (point-min))
7593         (isearch-forward regexp-p)))))
7594
7595 (defun gnus-summary-search-article-forward (regexp &optional backward)
7596   "Search for an article containing REGEXP forward.
7597 If BACKWARD, search backward instead."
7598   (interactive
7599    (list (read-string
7600           (format "Search article %s (regexp%s): "
7601                   (if current-prefix-arg "backward" "forward")
7602                   (if gnus-last-search-regexp
7603                       (concat ", default " gnus-last-search-regexp)
7604                     "")))
7605          current-prefix-arg))
7606   (if (string-equal regexp "")
7607       (setq regexp (or gnus-last-search-regexp ""))
7608     (setq gnus-last-search-regexp regexp)
7609     (setq gnus-article-before-search gnus-current-article))
7610   ;; Intentionally set gnus-last-article.
7611   (setq gnus-last-article gnus-article-before-search)
7612   (let ((gnus-last-article gnus-last-article))
7613     (if (gnus-summary-search-article regexp backward)
7614         (gnus-summary-show-thread)
7615       (error "Search failed: \"%s\"" regexp))))
7616
7617 (defun gnus-summary-search-article-backward (regexp)
7618   "Search for an article containing REGEXP backward."
7619   (interactive
7620    (list (read-string
7621           (format "Search article backward (regexp%s): "
7622                   (if gnus-last-search-regexp
7623                       (concat ", default " gnus-last-search-regexp)
7624                     "")))))
7625   (gnus-summary-search-article-forward regexp 'backward))
7626
7627 (eval-when-compile
7628   (defmacro gnus-summary-search-article-position-point (regexp backward)
7629     "Dehighlight the last matched text and goto the beginning position."
7630     (` (if (and gnus-summary-search-article-matched-data
7631                 (let ((text (caddr gnus-summary-search-article-matched-data))
7632                       (inhibit-read-only t)
7633                       buffer-read-only)
7634                   (delete-region
7635                    (goto-char (car gnus-summary-search-article-matched-data))
7636                    (cadr gnus-summary-search-article-matched-data))
7637                   (insert text)
7638                   (string-match (, regexp) text)))
7639            (if (, backward) (beginning-of-line) (end-of-line))
7640          (goto-char (if (, backward) (point-max) (point-min))))))
7641
7642   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7643     "Place point where X-Face image is displayed."
7644     (if (featurep 'xemacs)
7645         (` (let ((end (if (search-forward "\n\n" nil t)
7646                           (goto-char (1- (point)))
7647                         (point-min)))
7648                  extent)
7649              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7650              (unless (and (re-search-forward "^From:" end t)
7651                           (setq extent (extent-at (point)))
7652                           (extent-begin-glyph extent))
7653                (goto-char (, opoint)))))
7654       (` (let ((end (if (search-forward "\n\n" nil t)
7655                         (goto-char (1- (point)))
7656                       (point-min)))
7657                (start (or (search-backward "\n\n" nil t) (point-min))))
7658            (goto-char
7659             (or (text-property-any start end 'x-face-image t);; x-face-e21
7660                 (text-property-any start end 'x-face-mule-bitmap-image t)
7661                 (, opoint)))))))
7662
7663   (defmacro gnus-summary-search-article-highlight-matched-text
7664     (backward treated x-face)
7665     "Highlight matched text in the function `gnus-summary-search-article'."
7666     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7667              (end (set-marker (make-marker) (match-end 0)))
7668              (inhibit-read-only t)
7669              buffer-read-only)
7670          (unless treated
7671            (let ((,@
7672                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7673                     (mapcar
7674                      (lambda (item) (setq items (delq item items)))
7675                      '(gnus-treat-buttonize
7676                        gnus-treat-fill-article
7677                        gnus-treat-fill-long-lines
7678                        gnus-treat-emphasize
7679                        gnus-treat-highlight-headers
7680                        gnus-treat-highlight-citation
7681                        gnus-treat-highlight-signature
7682                        gnus-treat-overstrike
7683                        gnus-treat-display-xface
7684                        gnus-treat-buttonize-head
7685                        gnus-treat-decode-article-as-default-mime-charset))
7686                     (static-if (featurep 'xemacs)
7687                         items
7688                       (cons '(x-face-mule-delete-x-face-field
7689                               (quote never))
7690                             items))))
7691                  (gnus-treat-display-xface
7692                   (when (, x-face) gnus-treat-display-xface)))
7693              (gnus-article-prepare-mime-display)))
7694          (goto-char (if (, backward) start end))
7695          (when (, x-face)
7696            (gnus-summary-search-article-highlight-goto-x-face (point)))
7697          (setq gnus-summary-search-article-matched-data
7698                (list start end (buffer-substring start end)))
7699          (unless (eq start end);; matched text has been deleted. :-<
7700            (put-text-property start end 'face
7701                               (or (find-face 'isearch)
7702                                   'secondary-selection))))))
7703   )
7704
7705 (defun gnus-summary-search-article (regexp &optional backward)
7706   "Search for an article containing REGEXP.
7707 Optional argument BACKWARD means do search for backward.
7708 `gnus-select-article-hook' is not called during the search."
7709   ;; We have to require this here to make sure that the following
7710   ;; dynamic binding isn't shadowed by autoloading.
7711   (require 'gnus-async)
7712   (require 'gnus-art)
7713   (let ((gnus-select-article-hook nil)  ;Disable hook.
7714         (gnus-article-prepare-hook nil)
7715         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7716         (gnus-use-article-prefetch nil)
7717         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7718         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7719         (sum (current-buffer))
7720         (found nil)
7721         point treated)
7722     (gnus-save-hidden-threads
7723       (static-if (featurep 'xemacs)
7724           (let ((gnus-inhibit-treatment t))
7725             (setq treated (eq 'old (gnus-summary-select-article)))
7726             (when (and treated
7727                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7728                                  (window-live-p (get-buffer-window
7729                                                  gnus-article-buffer t)))))
7730               (gnus-summary-select-article nil t)
7731               (setq treated nil)))
7732         (let ((gnus-inhibit-treatment t)
7733               (x-face-mule-delete-x-face-field 'never))
7734           (setq treated (eq 'old (gnus-summary-select-article)))
7735           (when (and treated
7736                      (not
7737                       (and (gnus-buffer-live-p gnus-article-buffer)
7738                            (window-live-p (get-buffer-window
7739                                            gnus-article-buffer t))
7740                            (or (not (string-match "^\\^X-Face:" regexp))
7741                                (with-current-buffer gnus-article-buffer
7742                                  gnus-summary-search-article-matched-data)))))
7743             (gnus-summary-select-article nil t)
7744             (setq treated nil))))
7745       (set-buffer gnus-article-buffer)
7746       (widen)
7747       (if treated
7748           (progn
7749             (gnus-article-show-all-headers)
7750             (gnus-summary-search-article-position-point regexp backward))
7751         (goto-char (if backward (point-max) (point-min))))
7752       (while (not found)
7753         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7754         (if (if backward
7755                 (re-search-backward regexp nil t)
7756               (re-search-forward regexp nil t))
7757             ;; We found the regexp.
7758             (progn
7759               (gnus-summary-search-article-highlight-matched-text
7760                backward treated (string-match "^\\^X-Face:" regexp))
7761               (setq found 'found)
7762               (forward-line
7763                (/ (- 2 (window-height
7764                         (get-buffer-window gnus-article-buffer t)))
7765                   2))
7766               (set-window-start
7767                (get-buffer-window (current-buffer))
7768                (point))
7769               (set-buffer sum)
7770               (setq point (point)))
7771           ;; We didn't find it, so we go to the next article.
7772           (set-buffer sum)
7773           (setq found 'not)
7774           (while (eq found 'not)
7775             (if (not (if backward (gnus-summary-find-prev)
7776                        (gnus-summary-find-next)))
7777                 ;; No more articles.
7778                 (setq found t)
7779               ;; Select the next article and adjust point.
7780               (unless (gnus-summary-article-sparse-p
7781                        (gnus-summary-article-number))
7782                 (setq found nil)
7783                 (let ((gnus-inhibit-treatment t))
7784                   (gnus-summary-select-article))
7785                 (setq treated nil)
7786                 (set-buffer gnus-article-buffer)
7787                 (widen)
7788                 (goto-char (if backward (point-max) (point-min))))))))
7789       (gnus-message 7 ""))
7790     ;; Return whether we found the regexp.
7791     (when (eq found 'found)
7792       (goto-char point)
7793       (gnus-summary-show-thread)
7794       (gnus-summary-goto-subject gnus-current-article)
7795       (gnus-summary-position-point)
7796       t)))
7797
7798 (defun gnus-find-matching-articles (header regexp)
7799   "Return a list of all articles that match REGEXP on HEADER.
7800 This search includes all articles in the current group that Gnus has
7801 fetched headers for, whether they are displayed or not."
7802   (let ((articles nil)
7803         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7804         (case-fold-search t))
7805     (dolist (header gnus-newsgroup-headers)
7806       (when (string-match regexp (funcall func header))
7807         (push (mail-header-number header) articles)))
7808     (nreverse articles)))
7809
7810 (defun gnus-summary-find-matching (header regexp &optional backward unread
7811                                           not-case-fold)
7812   "Return a list of all articles that match REGEXP on HEADER.
7813 The search stars on the current article and goes forwards unless
7814 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7815 If UNREAD is non-nil, only unread articles will
7816 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7817 in the comparisons."
7818   (let ((case-fold-search (not not-case-fold))
7819         articles d func)
7820     (if (consp header)
7821         (if (eq (car header) 'extra)
7822             (setq func
7823                   `(lambda (h)
7824                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7825                          "")))
7826           (error "%s is an invalid header" header))
7827       (unless (fboundp (intern (concat "mail-header-" header)))
7828         (error "%s is not a valid header" header))
7829       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7830     (dolist (d (if (eq backward 'all)
7831                    gnus-newsgroup-data
7832                  (gnus-data-find-list
7833                   (gnus-summary-article-number)
7834                   (gnus-data-list backward))))
7835       (when (and (or (not unread)       ; We want all articles...
7836                      (gnus-data-unread-p d)) ; Or just unreads.
7837                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7838                  (string-match regexp
7839                                (funcall func (gnus-data-header d)))) ; Match.
7840         (push (gnus-data-number d) articles))) ; Success!
7841     (nreverse articles)))
7842
7843 (defun gnus-summary-execute-command (header regexp command &optional backward)
7844   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7845 If HEADER is an empty string (or nil), the match is done on the entire
7846 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7847   (interactive
7848    (list (let ((completion-ignore-case t))
7849            (completing-read
7850             "Header name: "
7851             (mapcar (lambda (header) (list (format "%s" header)))
7852                     (append
7853                      '("Number" "Subject" "From" "Lines" "Date"
7854                        "Message-ID" "Xref" "References" "Body")
7855                      gnus-extra-headers))
7856             nil 'require-match))
7857          (read-string "Regexp: ")
7858          (read-key-sequence "Command: ")
7859          current-prefix-arg))
7860   (when (equal header "Body")
7861     (setq header ""))
7862   ;; Hidden thread subtrees must be searched as well.
7863   (gnus-summary-show-all-threads)
7864   ;; We don't want to change current point nor window configuration.
7865   (save-excursion
7866     (save-window-excursion
7867       (gnus-message 6 "Executing %s..." (key-description command))
7868       ;; We'd like to execute COMMAND interactively so as to give arguments.
7869       (gnus-execute header regexp
7870                     `(call-interactively ',(key-binding command))
7871                     backward)
7872       (gnus-message 6 "Executing %s...done" (key-description command)))))
7873
7874 (defun gnus-summary-beginning-of-article ()
7875   "Scroll the article back to the beginning."
7876   (interactive)
7877   (gnus-summary-select-article)
7878   (gnus-configure-windows 'article)
7879   (gnus-eval-in-buffer-window gnus-article-buffer
7880     (widen)
7881     (goto-char (point-min))
7882     (when gnus-page-broken
7883       (gnus-narrow-to-page))))
7884
7885 (defun gnus-summary-end-of-article ()
7886   "Scroll to the end of the article."
7887   (interactive)
7888   (gnus-summary-select-article)
7889   (gnus-configure-windows 'article)
7890   (gnus-eval-in-buffer-window gnus-article-buffer
7891     (widen)
7892     (goto-char (point-max))
7893     (recenter -3)
7894     (when gnus-page-broken
7895       (gnus-narrow-to-page))))
7896
7897 (defun gnus-summary-print-article (&optional filename n)
7898   "Generate and print a PostScript image of the N next (mail) articles.
7899
7900 If N is negative, print the N previous articles.  If N is nil and articles
7901 have been marked with the process mark, print these instead.
7902
7903 If the optional first argument FILENAME is nil, send the image to the
7904 printer.  If FILENAME is a string, save the PostScript image in a file with
7905 that name.  If FILENAME is a number, prompt the user for the name of the file
7906 to save in."
7907   (interactive (list (ps-print-preprint current-prefix-arg)))
7908   (dolist (article (gnus-summary-work-articles n))
7909     (gnus-summary-select-article nil nil 'pseudo article)
7910     (gnus-eval-in-buffer-window gnus-article-buffer
7911       (let ((buffer (generate-new-buffer " *print*")))
7912         (unwind-protect
7913             (progn
7914               (copy-to-buffer buffer (point-min) (point-max))
7915               (set-buffer buffer)
7916               (gnus-article-delete-invisible-text)
7917               (when (gnus-visual-p 'article-highlight 'highlight)
7918                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7919                 ;; highlight.
7920                 (let ((gnus-article-buffer buffer))
7921                   (gnus-article-highlight-citation t)
7922                   (gnus-article-highlight-signature)))
7923               (let ((ps-left-header
7924                      (list
7925                       (concat "("
7926                               (mail-header-subject gnus-current-headers) ")")
7927                       (concat "("
7928                               (mail-header-from gnus-current-headers) ")")))
7929                     (ps-right-header
7930                      (list
7931                       "/pagenumberstring load"
7932                       (concat "("
7933                               (mail-header-date gnus-current-headers) ")"))))
7934                 (gnus-run-hooks 'gnus-ps-print-hook)
7935                 (save-excursion
7936                   (if window-system
7937                       (ps-spool-buffer-with-faces)
7938                     (ps-spool-buffer)))))
7939           (kill-buffer buffer))))
7940     (gnus-summary-remove-process-mark article))
7941   (ps-despool filename))
7942
7943 (defun gnus-summary-show-article (&optional arg)
7944   "Force re-fetching of the current article.
7945 If ARG (the prefix) is a number, show the article with the charset
7946 defined in `gnus-summary-show-article-charset-alist', or the charset
7947 input.
7948 If ARG (the prefix) is non-nil and not a number, show the raw article
7949 without any article massaging functions being run."
7950   (interactive "P")
7951   (cond
7952    ((numberp arg)
7953     (gnus-summary-show-article t)
7954     (let ((gnus-newsgroup-charset
7955            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7956                (mm-read-coding-system
7957                 "View as charset: "
7958                 (save-excursion
7959                   (set-buffer gnus-article-buffer)
7960                   (detect-coding-region (point) (point-max) t)))))
7961           (gnus-newsgroup-ignored-charsets 'gnus-all))
7962       (gnus-summary-select-article nil 'force)
7963       (let ((deps gnus-newsgroup-dependencies)
7964             head header)
7965         (save-excursion
7966           (set-buffer gnus-original-article-buffer)
7967           (save-restriction
7968             (message-narrow-to-head)
7969             (setq head (buffer-string)))
7970           (with-temp-buffer
7971             (insert (format "211 %d Article retrieved.\n"
7972                             (cdr gnus-article-current)))
7973             (insert head)
7974             (insert ".\n")
7975             (let ((nntp-server-buffer (current-buffer)))
7976               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7977         (gnus-data-set-header
7978          (gnus-data-find (cdr gnus-article-current))
7979          header)
7980         (gnus-summary-update-article-line
7981          (cdr gnus-article-current) header)
7982         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7983           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7984    ((not arg)
7985     ;; Select the article the normal way.
7986     (gnus-summary-select-article nil 'force))
7987    (t
7988     ;; We have to require this here to make sure that the following
7989     ;; dynamic binding isn't shadowed by autoloading.
7990     (require 'gnus-async)
7991     (require 'gnus-art)
7992     ;; Bind the article treatment functions to nil.
7993     (let ((gnus-have-all-headers t)
7994           gnus-article-prepare-hook
7995           gnus-article-decode-hook
7996           gnus-break-pages
7997           gnus-show-mime
7998           (gnus-inhibit-treatment t))
7999       (gnus-summary-select-article nil 'force))))
8000   (gnus-summary-goto-subject gnus-current-article)
8001   (gnus-summary-position-point))
8002
8003 (defun gnus-summary-show-raw-article ()
8004   "Show the raw article without any article massaging functions being run."
8005   (interactive)
8006   (gnus-summary-show-article t))
8007
8008 (defun gnus-summary-verbose-headers (&optional arg)
8009   "Toggle permanent full header display.
8010 If ARG is a positive number, turn header display on.
8011 If ARG is a negative number, turn header display off."
8012   (interactive "P")
8013   (setq gnus-show-all-headers
8014         (cond ((or (not (numberp arg))
8015                    (zerop arg))
8016                (not gnus-show-all-headers))
8017               ((natnump arg)
8018                t)))
8019   (gnus-summary-show-article))
8020
8021 (defun gnus-summary-toggle-header (&optional arg)
8022   "Show the headers if they are hidden, or hide them if they are shown.
8023 If ARG is a positive number, show the entire header.
8024 If ARG is a negative number, hide the unwanted header lines."
8025   (interactive "P")
8026   (save-excursion
8027     (set-buffer gnus-article-buffer)
8028     (save-restriction
8029       (let* ((buffer-read-only nil)
8030              (inhibit-point-motion-hooks t)
8031              hidden e)
8032         (setq hidden
8033               (if (numberp arg)
8034                   (>= arg 0)
8035                 (save-restriction
8036                   (article-narrow-to-head)
8037                   (gnus-article-hidden-text-p 'headers))))
8038         (goto-char (point-min))
8039         (when (search-forward "\n\n" nil t)
8040           (delete-region (point-min) (1- (point))))
8041         (goto-char (point-min))
8042         (save-excursion
8043           (set-buffer gnus-original-article-buffer)
8044           (goto-char (point-min))
8045           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8046         (insert-buffer-substring gnus-original-article-buffer 1 e)
8047         (save-restriction
8048           (narrow-to-region (point-min) (point))
8049           (article-decode-encoded-words)
8050           (if  hidden
8051               (let ((gnus-treat-hide-headers nil)
8052                     (gnus-treat-hide-boring-headers nil))
8053                 (setq gnus-article-wash-types
8054                       (delq 'headers gnus-article-wash-types))
8055                 (gnus-treat-article 'head))
8056             (gnus-treat-article 'head)))
8057         (gnus-set-mode-line 'article)))))
8058
8059 (defun gnus-summary-show-all-headers ()
8060   "Make all header lines visible."
8061   (interactive)
8062   (gnus-summary-toggle-header 1))
8063
8064 (defun gnus-summary-toggle-mime (&optional arg)
8065   "Toggle MIME processing.
8066 If ARG is a positive number, turn MIME processing on."
8067   (interactive "P")
8068   (setq gnus-show-mime
8069         (if (null arg)
8070             (not gnus-show-mime)
8071           (> (prefix-numeric-value arg) 0)))
8072   (gnus-summary-select-article t 'force))
8073
8074 (defun gnus-summary-caesar-message (&optional arg)
8075   "Caesar rotate the current article by 13.
8076 The numerical prefix specifies how many places to rotate each letter
8077 forward."
8078   (interactive "P")
8079   (gnus-summary-select-article)
8080   (let ((mail-header-separator ""))
8081     (gnus-eval-in-buffer-window gnus-article-buffer
8082       (save-restriction
8083         (widen)
8084         (let ((start (window-start))
8085               buffer-read-only)
8086           (message-caesar-buffer-body arg)
8087           (set-window-start (get-buffer-window (current-buffer)) start))))))
8088
8089 (defun gnus-summary-stop-page-breaking ()
8090   "Stop page breaking in the current article."
8091   (interactive)
8092   (gnus-summary-select-article)
8093   (gnus-eval-in-buffer-window gnus-article-buffer
8094     (widen)
8095     (when (gnus-visual-p 'page-marker)
8096       (let ((buffer-read-only nil))
8097         (gnus-remove-text-with-property 'gnus-prev)
8098         (gnus-remove-text-with-property 'gnus-next))
8099       (setq gnus-page-broken nil))))
8100
8101 (defun gnus-summary-move-article (&optional n to-newsgroup
8102                                             select-method action)
8103   "Move the current article to a different newsgroup.
8104 If N is a positive number, move the N next articles.
8105 If N is a negative number, move the N previous articles.
8106 If N is nil and any articles have been marked with the process mark,
8107 move those articles instead.
8108 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8109 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8110 re-spool using this method.
8111
8112 For this function to work, both the current newsgroup and the
8113 newsgroup that you want to move to have to support the `request-move'
8114 and `request-accept' functions.
8115
8116 ACTION can be either `move' (the default), `crosspost' or `copy'."
8117   (interactive "P")
8118   (unless action
8119     (setq action 'move))
8120   ;; Check whether the source group supports the required functions.
8121   (cond ((and (eq action 'move)
8122               (not (gnus-check-backend-function
8123                     'request-move-article gnus-newsgroup-name)))
8124          (error "The current group does not support article moving"))
8125         ((and (eq action 'crosspost)
8126               (not (gnus-check-backend-function
8127                     'request-replace-article gnus-newsgroup-name)))
8128          (error "The current group does not support article editing")))
8129   (let ((articles (gnus-summary-work-articles n))
8130         (prefix (if (gnus-check-backend-function
8131                      'request-move-article gnus-newsgroup-name)
8132                     (gnus-group-real-prefix gnus-newsgroup-name)
8133                   ""))
8134         (names '((move "Move" "Moving")
8135                  (copy "Copy" "Copying")
8136                  (crosspost "Crosspost" "Crossposting")))
8137         (copy-buf (save-excursion
8138                     (nnheader-set-temp-buffer " *copy article*")))
8139         (default-marks gnus-article-mark-lists)
8140         (no-expire-marks (delete '(expirable . expire)
8141                                  (copy-sequence gnus-article-mark-lists)))
8142         art-group to-method new-xref article to-groups)
8143     (unless (assq action names)
8144       (error "Unknown action %s" action))
8145     ;; Read the newsgroup name.
8146     (when (and (not to-newsgroup)
8147                (not select-method))
8148       (setq to-newsgroup
8149             (gnus-read-move-group-name
8150              (cadr (assq action names))
8151              (symbol-value (intern (format "gnus-current-%s-group" action)))
8152              articles prefix))
8153       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8154     (setq to-method (or select-method
8155                         (gnus-server-to-method
8156                          (gnus-group-method to-newsgroup))))
8157     ;; Check the method we are to move this article to...
8158     (unless (gnus-check-backend-function
8159              'request-accept-article (car to-method))
8160       (error "%s does not support article copying" (car to-method)))
8161     (unless (gnus-check-server to-method)
8162       (error "Can't open server %s" (car to-method)))
8163     (gnus-message 6 "%s to %s: %s..."
8164                   (caddr (assq action names))
8165                   (or (car select-method) to-newsgroup) articles)
8166     (while articles
8167       (setq article (pop articles))
8168       (setq
8169        art-group
8170        (cond
8171         ;; Move the article.
8172         ((eq action 'move)
8173          ;; Remove this article from future suppression.
8174          (gnus-dup-unsuppress-article article)
8175          (gnus-request-move-article
8176           article                       ; Article to move
8177           gnus-newsgroup-name           ; From newsgroup
8178           (nth 1 (gnus-find-method-for-group
8179                   gnus-newsgroup-name)) ; Server
8180           (list 'gnus-request-accept-article
8181                 to-newsgroup (list 'quote select-method)
8182                 (not articles) t)       ; Accept form
8183           (not articles)))              ; Only save nov last time
8184         ;; Copy the article.
8185         ((eq action 'copy)
8186          (save-excursion
8187            (set-buffer copy-buf)
8188            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8189              (gnus-request-accept-article
8190               to-newsgroup select-method (not articles) t))))
8191         ;; Crosspost the article.
8192         ((eq action 'crosspost)
8193          (let ((xref (message-tokenize-header
8194                       (mail-header-xref (gnus-summary-article-header article))
8195                       " ")))
8196            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8197                                   ":" article))
8198            (unless xref
8199              (setq xref (list (system-name))))
8200            (setq new-xref
8201                  (concat
8202                   (mapconcat 'identity
8203                              (delete "Xref:" (delete new-xref xref))
8204                              " ")
8205                   " " new-xref))
8206            (save-excursion
8207              (set-buffer copy-buf)
8208              ;; First put the article in the destination group.
8209              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8210              (when (consp (setq art-group
8211                                 (gnus-request-accept-article
8212                                  to-newsgroup select-method (not articles))))
8213                (setq new-xref (concat new-xref " " (car art-group)
8214                                       ":" (cdr art-group)))
8215                ;; Now we have the new Xrefs header, so we insert
8216                ;; it and replace the new article.
8217                (nnheader-replace-header "Xref" new-xref)
8218                (gnus-request-replace-article
8219                 (cdr art-group) to-newsgroup (current-buffer))
8220                art-group))))))
8221       (cond
8222        ((not art-group)
8223         (gnus-message 1 "Couldn't %s article %s: %s"
8224                       (cadr (assq action names)) article
8225                       (nnheader-get-report (car to-method))))
8226        ((eq art-group 'junk)
8227         (when (eq action 'move)
8228           (gnus-summary-mark-article article gnus-canceled-mark)
8229           (gnus-message 4 "Deleted article %s" article)))
8230        (t
8231         (let* ((pto-group (gnus-group-prefixed-name
8232                            (car art-group) to-method))
8233                (entry
8234                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8235                (info (nth 2 entry))
8236                (to-group (gnus-info-group info))
8237                to-marks)
8238           ;; Update the group that has been moved to.
8239           (when (and info
8240                      (memq action '(move copy)))
8241             (unless (member to-group to-groups)
8242               (push to-group to-groups))
8243
8244             (unless (memq article gnus-newsgroup-unreads)
8245               (push 'read to-marks)
8246               (gnus-info-set-read
8247                info (gnus-add-to-range (gnus-info-read info)
8248                                        (list (cdr art-group)))))
8249
8250             ;; See whether the article is to be put in the cache.
8251             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8252                              default-marks
8253                            no-expire-marks))
8254                   (to-article (cdr art-group)))
8255
8256               ;; Enter the article into the cache in the new group,
8257               ;; if that is required.
8258               (when gnus-use-cache
8259                 (gnus-cache-possibly-enter-article
8260                  to-group to-article
8261                  (let ((header (copy-sequence
8262                                 (gnus-summary-article-header article))))
8263                    (mail-header-set-number header to-article)
8264                    header)
8265                  (memq article gnus-newsgroup-marked)
8266                  (memq article gnus-newsgroup-dormant)
8267                  (memq article gnus-newsgroup-unreads)))
8268
8269               (when gnus-preserve-marks
8270                 ;; Copy any marks over to the new group.
8271                 (when (and (equal to-group gnus-newsgroup-name)
8272                            (not (memq article gnus-newsgroup-unreads)))
8273                   ;; Mark this article as read in this group.
8274                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8275                   (setcdr (gnus-active to-group) to-article)
8276                   (setcdr gnus-newsgroup-active to-article))
8277
8278                 (while marks
8279                   (when (memq article (symbol-value
8280                                        (intern (format "gnus-newsgroup-%s"
8281                                                        (caar marks)))))
8282                     (push (cdar marks) to-marks)
8283                     ;; If the other group is the same as this group,
8284                     ;; then we have to add the mark to the list.
8285                     (when (equal to-group gnus-newsgroup-name)
8286                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8287                            (cons to-article
8288                                  (symbol-value
8289                                   (intern (format "gnus-newsgroup-%s"
8290                                                   (caar marks)))))))
8291                     ;; Copy the marks to other group.
8292                     (gnus-add-marked-articles
8293                      to-group (cdar marks) (list to-article) info))
8294                   (setq marks (cdr marks)))
8295
8296                 (gnus-request-set-mark to-group (list (list (list to-article)
8297                                                             'add
8298                                                             to-marks))))
8299
8300               (gnus-dribble-enter
8301                (concat "(gnus-group-set-info '"
8302                        (gnus-prin1-to-string (gnus-get-info to-group))
8303                        ")"))))
8304
8305           ;; Update the Xref header in this article to point to
8306           ;; the new crossposted article we have just created.
8307           (when (eq action 'crosspost)
8308             (save-excursion
8309               (set-buffer copy-buf)
8310               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8311               (nnheader-replace-header "Xref" new-xref)
8312               (gnus-request-replace-article
8313                article gnus-newsgroup-name (current-buffer)))))
8314
8315         ;;;!!!Why is this necessary?
8316         (set-buffer gnus-summary-buffer)
8317
8318         (gnus-summary-goto-subject article)
8319         (when (eq action 'move)
8320           (gnus-summary-mark-article article gnus-canceled-mark))))
8321       (gnus-summary-remove-process-mark article))
8322     ;; Re-activate all groups that have been moved to.
8323     (while to-groups
8324       (save-excursion
8325         (set-buffer gnus-group-buffer)
8326         (when (gnus-group-goto-group (car to-groups) t)
8327           (gnus-group-get-new-news-this-group 1 t))
8328         (pop to-groups)))
8329
8330     (gnus-kill-buffer copy-buf)
8331     (gnus-summary-position-point)
8332     (gnus-set-mode-line 'summary)))
8333
8334 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8335   "Move the current article to a different newsgroup.
8336 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8337 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8338 re-spool using this method."
8339   (interactive "P")
8340   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8341
8342 (defun gnus-summary-crosspost-article (&optional n)
8343   "Crosspost the current article to some other group."
8344   (interactive "P")
8345   (gnus-summary-move-article n nil nil 'crosspost))
8346
8347 (defcustom gnus-summary-respool-default-method nil
8348   "Default method for respooling an article.
8349 If nil, use to the current newsgroup method."
8350   :type '(choice (gnus-select-method :value (nnml ""))
8351                  (const nil))
8352   :group 'gnus-summary-mail)
8353
8354 (defun gnus-summary-respool-article (&optional n method)
8355   "Respool the current article.
8356 The article will be squeezed through the mail spooling process again,
8357 which means that it will be put in some mail newsgroup or other
8358 depending on `nnmail-split-methods'.
8359 If N is a positive number, respool the N next articles.
8360 If N is a negative number, respool the N previous articles.
8361 If N is nil and any articles have been marked with the process mark,
8362 respool those articles instead.
8363
8364 Respooling can be done both from mail groups and \"real\" newsgroups.
8365 In the former case, the articles in question will be moved from the
8366 current group into whatever groups they are destined to.  In the
8367 latter case, they will be copied into the relevant groups."
8368   (interactive
8369    (list current-prefix-arg
8370          (let* ((methods (gnus-methods-using 'respool))
8371                 (methname
8372                  (symbol-name (or gnus-summary-respool-default-method
8373                                   (car (gnus-find-method-for-group
8374                                         gnus-newsgroup-name)))))
8375                 (method
8376                  (gnus-completing-read
8377                   methname "What backend do you want to use when respooling?"
8378                   methods nil t nil 'gnus-mail-method-history))
8379                 ms)
8380            (cond
8381             ((zerop (length (setq ms (gnus-servers-using-backend
8382                                       (intern method)))))
8383              (list (intern method) ""))
8384             ((= 1 (length ms))
8385              (car ms))
8386             (t
8387              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8388                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8389                            ms-alist))))))))
8390   (unless method
8391     (error "No method given for respooling"))
8392   (if (assoc (symbol-name
8393               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8394              (gnus-methods-using 'respool))
8395       (gnus-summary-move-article n nil method)
8396     (gnus-summary-copy-article n nil method)))
8397
8398 (defun gnus-summary-import-article (file &optional edit)
8399   "Import an arbitrary file into a mail newsgroup."
8400   (interactive "fImport file: \nP")
8401   (let ((group gnus-newsgroup-name)
8402         (now (current-time))
8403         atts lines group-art)
8404     (unless (gnus-check-backend-function 'request-accept-article group)
8405       (error "%s does not support article importing" group))
8406     (or (file-readable-p file)
8407         (not (file-regular-p file))
8408         (error "Can't read %s" file))
8409     (save-excursion
8410       (set-buffer (gnus-get-buffer-create " *import file*"))
8411       (erase-buffer)
8412       (nnheader-insert-file-contents file)
8413       (goto-char (point-min))
8414       (if (nnheader-article-p)
8415           (save-restriction
8416             (goto-char (point-min))
8417             (search-forward "\n\n" nil t)
8418             (narrow-to-region (point-min) (1- (point)))
8419             (goto-char (point-min))
8420             (unless (re-search-forward "^date:" nil t)
8421               (goto-char (point-max))
8422               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8423         ;; This doesn't look like an article, so we fudge some headers.
8424         (setq atts (file-attributes file)
8425               lines (count-lines (point-min) (point-max)))
8426         (insert "From: " (read-string "From: ") "\n"
8427                 "Subject: " (read-string "Subject: ") "\n"
8428                 "Date: " (message-make-date (nth 5 atts)) "\n"
8429                 "Message-ID: " (message-make-message-id) "\n"
8430                 "Lines: " (int-to-string lines) "\n"
8431                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8432       (setq group-art (gnus-request-accept-article group nil t))
8433       (kill-buffer (current-buffer)))
8434     (setq gnus-newsgroup-active (gnus-activate-group group))
8435     (forward-line 1)
8436     (gnus-summary-goto-article (cdr group-art) nil t)
8437     (when edit
8438       (gnus-summary-edit-article))))
8439
8440 (defun gnus-summary-create-article ()
8441   "Create an article in a mail newsgroup."
8442   (interactive)
8443   (let ((group gnus-newsgroup-name)
8444         (now (current-time))
8445         group-art)
8446     (unless (gnus-check-backend-function 'request-accept-article group)
8447       (error "%s does not support article importing" group))
8448     (save-excursion
8449       (set-buffer (gnus-get-buffer-create " *import file*"))
8450       (erase-buffer)
8451       (goto-char (point-min))
8452       ;; This doesn't look like an article, so we fudge some headers.
8453       (insert "From: " (read-string "From: ") "\n"
8454               "Subject: " (read-string "Subject: ") "\n"
8455               "Date: " (message-make-date now) "\n"
8456               "Message-ID: " (message-make-message-id) "\n")
8457       (setq group-art (gnus-request-accept-article group nil t))
8458       (kill-buffer (current-buffer)))
8459     (setq gnus-newsgroup-active (gnus-activate-group group))
8460     (forward-line 1)
8461     (gnus-summary-goto-article (cdr group-art) nil t)
8462     (gnus-summary-edit-article)))
8463
8464 (defun gnus-summary-article-posted-p ()
8465   "Say whether the current (mail) article is available from news as well.
8466 This will be the case if the article has both been mailed and posted."
8467   (interactive)
8468   (let ((id (mail-header-references (gnus-summary-article-header)))
8469         (gnus-override-method (car (gnus-refer-article-methods))))
8470     (if (gnus-request-head id "")
8471         (gnus-message 2 "The current message was found on %s"
8472                       gnus-override-method)
8473       (gnus-message 2 "The current message couldn't be found on %s"
8474                     gnus-override-method)
8475       nil)))
8476
8477 (defun gnus-summary-expire-articles (&optional now)
8478   "Expire all articles that are marked as expirable in the current group."
8479   (interactive)
8480   (when (gnus-check-backend-function
8481          'request-expire-articles gnus-newsgroup-name)
8482     ;; This backend supports expiry.
8483     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8484            (expirable (if total
8485                           (progn
8486                             ;; We need to update the info for
8487                             ;; this group for `gnus-list-of-read-articles'
8488                             ;; to give us the right answer.
8489                             (gnus-run-hooks 'gnus-exit-group-hook)
8490                             (gnus-summary-update-info)
8491                             (gnus-list-of-read-articles gnus-newsgroup-name))
8492                         (setq gnus-newsgroup-expirable
8493                               (sort gnus-newsgroup-expirable '<))))
8494            (expiry-wait (if now 'immediate
8495                           (gnus-group-find-parameter
8496                            gnus-newsgroup-name 'expiry-wait)))
8497            (nnmail-expiry-target
8498             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8499                 nnmail-expiry-target))
8500            es)
8501       (when expirable
8502         ;; There are expirable articles in this group, so we run them
8503         ;; through the expiry process.
8504         (gnus-message 6 "Expiring articles...")
8505         (unless (gnus-check-group gnus-newsgroup-name)
8506           (error "Can't open server for %s" gnus-newsgroup-name))
8507         ;; The list of articles that weren't expired is returned.
8508         (save-excursion
8509           (if expiry-wait
8510               (let ((nnmail-expiry-wait-function nil)
8511                     (nnmail-expiry-wait expiry-wait))
8512                 (setq es (gnus-request-expire-articles
8513                           expirable gnus-newsgroup-name)))
8514             (setq es (gnus-request-expire-articles
8515                       expirable gnus-newsgroup-name)))
8516           (unless total
8517             (setq gnus-newsgroup-expirable es))
8518           ;; We go through the old list of expirable, and mark all
8519           ;; really expired articles as nonexistent.
8520           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8521             (let ((gnus-use-cache nil))
8522               (while expirable
8523                 (unless (memq (car expirable) es)
8524                   (when (gnus-data-find (car expirable))
8525                     (gnus-summary-mark-article
8526                      (car expirable) gnus-canceled-mark)))
8527                 (setq expirable (cdr expirable))))))
8528         (gnus-message 6 "Expiring articles...done")))))
8529
8530 (defun gnus-summary-expire-articles-now ()
8531   "Expunge all expirable articles in the current group.
8532 This means that *all* articles that are marked as expirable will be
8533 deleted forever, right now."
8534   (interactive)
8535   (or gnus-expert-user
8536       (gnus-yes-or-no-p
8537        "Are you really, really, really sure you want to delete all these messages? ")
8538       (error "Phew!"))
8539   (gnus-summary-expire-articles t))
8540
8541 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8542 (defun gnus-summary-delete-article (&optional n)
8543   "Delete the N next (mail) articles.
8544 This command actually deletes articles.  This is not a marking
8545 command.  The article will disappear forever from your life, never to
8546 return.
8547 If N is negative, delete backwards.
8548 If N is nil and articles have been marked with the process mark,
8549 delete these instead."
8550   (interactive "P")
8551   (unless (gnus-check-backend-function 'request-expire-articles
8552                                        gnus-newsgroup-name)
8553     (error "The current newsgroup does not support article deletion"))
8554   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8555     (error "Couldn't open server"))
8556   ;; Compute the list of articles to delete.
8557   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8558         not-deleted)
8559     (if (and gnus-novice-user
8560              (not (gnus-yes-or-no-p
8561                    (format "Do you really want to delete %s forever? "
8562                            (if (> (length articles) 1)
8563                                (format "these %s articles" (length articles))
8564                              "this article")))))
8565         ()
8566       ;; Delete the articles.
8567       (setq not-deleted (gnus-request-expire-articles
8568                          articles gnus-newsgroup-name 'force))
8569       (while articles
8570         (gnus-summary-remove-process-mark (car articles))
8571         ;; The backend might not have been able to delete the article
8572         ;; after all.
8573         (unless (memq (car articles) not-deleted)
8574           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8575         (setq articles (cdr articles)))
8576       (when not-deleted
8577         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8578     (gnus-summary-position-point)
8579     (gnus-set-mode-line 'summary)
8580     not-deleted))
8581
8582 (defun gnus-summary-edit-article (&optional force)
8583   "Edit the current article.
8584 This will have permanent effect only in mail groups.
8585 If FORCE is non-nil, allow editing of articles even in read-only
8586 groups."
8587   (interactive "P")
8588   (save-excursion
8589     (set-buffer gnus-summary-buffer)
8590     (let ((mail-parse-charset gnus-newsgroup-charset)
8591           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8592       (gnus-set-global-variables)
8593       (when (and (not force)
8594                  (gnus-group-read-only-p))
8595         (error "The current newsgroup does not support article editing"))
8596       (gnus-summary-show-article t)
8597       (gnus-article-edit-article
8598        'ignore
8599        `(lambda (no-highlight)
8600           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8601                 (message-options message-options)
8602                 (message-options-set-recipient)
8603                 (mail-parse-ignored-charsets
8604                  ',gnus-newsgroup-ignored-charsets))
8605             (gnus-summary-edit-article-done
8606              ,(or (mail-header-references gnus-current-headers) "")
8607              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8608
8609 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8610
8611 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8612                                                  no-highlight)
8613   "Make edits to the current article permanent."
8614   (interactive)
8615   (save-excursion
8616     ;; The buffer restriction contains the entire article if it exists.
8617     (when (article-goto-body)
8618       (let ((lines (count-lines (point) (point-max)))
8619             (length (- (point-max) (point)))
8620             (case-fold-search t)
8621             (body (copy-marker (point))))
8622         (goto-char (point-min))
8623         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8624           (delete-region (match-beginning 1) (match-end 1))
8625           (insert (number-to-string length)))
8626         (goto-char (point-min))
8627         (when (re-search-forward
8628                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8629           (delete-region (match-beginning 1) (match-end 1))
8630           (insert (number-to-string length)))
8631         (goto-char (point-min))
8632         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8633           (delete-region (match-beginning 1) (match-end 1))
8634           (insert (number-to-string lines))))))
8635   ;; Replace the article.
8636   (let ((buf (current-buffer)))
8637     (with-temp-buffer
8638       (insert-buffer-substring buf)
8639
8640       (if (and (not read-only)
8641                (not (gnus-request-replace-article
8642                      (cdr gnus-article-current) (car gnus-article-current)
8643                      (current-buffer) t)))
8644           (error "Couldn't replace article")
8645         ;; Update the summary buffer.
8646         (if (and references
8647                  (equal (message-tokenize-header references " ")
8648                         (message-tokenize-header
8649                          (or (message-fetch-field "references") "") " ")))
8650             ;; We only have to update this line.
8651             (save-excursion
8652               (save-restriction
8653                 (message-narrow-to-head)
8654                 (let ((head (buffer-string))
8655                       header)
8656                   (with-temp-buffer
8657                     (insert (format "211 %d Article retrieved.\n"
8658                                     (cdr gnus-article-current)))
8659                     (insert head)
8660                     (insert ".\n")
8661                     (let ((nntp-server-buffer (current-buffer)))
8662                       (setq header (car (gnus-get-newsgroup-headers
8663                                          (save-excursion
8664                                            (set-buffer gnus-summary-buffer)
8665                                            gnus-newsgroup-dependencies)
8666                                          t))))
8667                     (save-excursion
8668                       (set-buffer gnus-summary-buffer)
8669                       (gnus-data-set-header
8670                        (gnus-data-find (cdr gnus-article-current))
8671                        header)
8672                       (gnus-summary-update-article-line
8673                        (cdr gnus-article-current) header)
8674                       (if (gnus-summary-goto-subject
8675                            (cdr gnus-article-current) nil t)
8676                           (gnus-summary-update-secondary-mark
8677                            (cdr gnus-article-current))))))))
8678           ;; Update threads.
8679           (set-buffer (or buffer gnus-summary-buffer))
8680           (gnus-summary-update-article (cdr gnus-article-current))
8681           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8682               (gnus-summary-update-secondary-mark
8683                (cdr gnus-article-current))))
8684         ;; Prettify the article buffer again.
8685         (unless no-highlight
8686           (save-excursion
8687             (set-buffer gnus-article-buffer)
8688             ;;;!!! Fix this -- article should be rehighlighted.
8689             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8690             (set-buffer gnus-original-article-buffer)
8691             (gnus-request-article
8692              (cdr gnus-article-current)
8693              (car gnus-article-current) (current-buffer))))
8694         ;; Prettify the summary buffer line.
8695         (when (gnus-visual-p 'summary-highlight 'highlight)
8696           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8697
8698 (defun gnus-summary-edit-wash (key)
8699   "Perform editing command KEY in the article buffer."
8700   (interactive
8701    (list
8702     (progn
8703       (message "%s" (concat (this-command-keys) "- "))
8704       (read-char))))
8705   (message "")
8706   (gnus-summary-edit-article)
8707   (execute-kbd-macro (concat (this-command-keys) key))
8708   (gnus-article-edit-done))
8709
8710 ;;; Respooling
8711
8712 (defun gnus-summary-respool-query (&optional silent trace)
8713   "Query where the respool algorithm would put this article."
8714   (interactive)
8715   (let (gnus-mark-article-hook)
8716     (gnus-summary-select-article)
8717     (save-excursion
8718       (set-buffer gnus-original-article-buffer)
8719       (save-restriction
8720         (message-narrow-to-head)
8721         (let ((groups (nnmail-article-group 'identity trace)))
8722           (unless silent
8723             (if groups
8724                 (message "This message would go to %s"
8725                          (mapconcat 'car groups ", "))
8726               (message "This message would go to no groups"))
8727             groups))))))
8728
8729 (defun gnus-summary-respool-trace ()
8730   "Trace where the respool algorithm would put this article.
8731 Display a buffer showing all fancy splitting patterns which matched."
8732   (interactive)
8733   (gnus-summary-respool-query nil t))
8734
8735 ;; Summary marking commands.
8736
8737 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8738   "Mark articles which has the same subject as read, and then select the next.
8739 If UNMARK is positive, remove any kind of mark.
8740 If UNMARK is negative, tick articles."
8741   (interactive "P")
8742   (when unmark
8743     (setq unmark (prefix-numeric-value unmark)))
8744   (let ((count
8745          (gnus-summary-mark-same-subject
8746           (gnus-summary-article-subject) unmark)))
8747     ;; Select next unread article.  If auto-select-same mode, should
8748     ;; select the first unread article.
8749     (gnus-summary-next-article t (and gnus-auto-select-same
8750                                       (gnus-summary-article-subject)))
8751     (gnus-message 7 "%d article%s marked as %s"
8752                   count (if (= count 1) " is" "s are")
8753                   (if unmark "unread" "read"))))
8754
8755 (defun gnus-summary-kill-same-subject (&optional unmark)
8756   "Mark articles which has the same subject as read.
8757 If UNMARK is positive, remove any kind of mark.
8758 If UNMARK is negative, tick articles."
8759   (interactive "P")
8760   (when unmark
8761     (setq unmark (prefix-numeric-value unmark)))
8762   (let ((count
8763          (gnus-summary-mark-same-subject
8764           (gnus-summary-article-subject) unmark)))
8765     ;; If marked as read, go to next unread subject.
8766     (when (null unmark)
8767       ;; Go to next unread subject.
8768       (gnus-summary-next-subject 1 t))
8769     (gnus-message 7 "%d articles are marked as %s"
8770                   count (if unmark "unread" "read"))))
8771
8772 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8773   "Mark articles with same SUBJECT as read, and return marked number.
8774 If optional argument UNMARK is positive, remove any kinds of marks.
8775 If optional argument UNMARK is negative, mark articles as unread instead."
8776   (let ((count 1))
8777     (save-excursion
8778       (cond
8779        ((null unmark)                   ; Mark as read.
8780         (while (and
8781                 (progn
8782                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8783                   (gnus-summary-show-thread) t)
8784                 (gnus-summary-find-subject subject))
8785           (setq count (1+ count))))
8786        ((> unmark 0)                    ; Tick.
8787         (while (and
8788                 (progn
8789                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8790                   (gnus-summary-show-thread) t)
8791                 (gnus-summary-find-subject subject))
8792           (setq count (1+ count))))
8793        (t                               ; Mark as unread.
8794         (while (and
8795                 (progn
8796                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8797                   (gnus-summary-show-thread) t)
8798                 (gnus-summary-find-subject subject))
8799           (setq count (1+ count)))))
8800       (gnus-set-mode-line 'summary)
8801       ;; Return the number of marked articles.
8802       count)))
8803
8804 (defun gnus-summary-mark-as-processable (n &optional unmark)
8805   "Set the process mark on the next N articles.
8806 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8807 the process mark instead.  The difference between N and the actual
8808 number of articles marked is returned."
8809   (interactive "P")
8810   (if (and (null n) (gnus-region-active-p))
8811       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8812     (setq n (prefix-numeric-value n))
8813     (let ((backward (< n 0))
8814           (n (abs n)))
8815       (while (and
8816               (> n 0)
8817               (if unmark
8818                   (gnus-summary-remove-process-mark
8819                    (gnus-summary-article-number))
8820                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8821               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8822         (setq n (1- n)))
8823       (when (/= 0 n)
8824         (gnus-message 7 "No more articles"))
8825       (gnus-summary-recenter)
8826       (gnus-summary-position-point)
8827       n)))
8828
8829 (defun gnus-summary-unmark-as-processable (n)
8830   "Remove the process mark from the next N articles.
8831 If N is negative, unmark backward instead.  The difference between N and
8832 the actual number of articles unmarked is returned."
8833   (interactive "P")
8834   (gnus-summary-mark-as-processable n t))
8835
8836 (defun gnus-summary-unmark-all-processable ()
8837   "Remove the process mark from all articles."
8838   (interactive)
8839   (save-excursion
8840     (while gnus-newsgroup-processable
8841       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8842   (gnus-summary-position-point))
8843
8844 (defun gnus-summary-add-mark (article type)
8845   "Mark ARTICLE with a mark of TYPE."
8846   (let ((vtype (car (assq type gnus-article-mark-lists)))
8847         var)
8848     (if (not vtype)
8849         (error "No such mark type: %s" type)
8850       (setq var (intern (format "gnus-newsgroup-%s" type)))
8851       (set var (cons article (symbol-value var)))
8852       (if (memq type '(processable cached replied forwarded recent saved))
8853           (gnus-summary-update-secondary-mark article)
8854         ;;; !!! This is bogus.  We should find out what primary
8855         ;;; !!! mark we want to set.
8856         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8857
8858 (defun gnus-summary-mark-as-expirable (n)
8859   "Mark N articles forward as expirable.
8860 If N is negative, mark backward instead.  The difference between N and
8861 the actual number of articles marked is returned."
8862   (interactive "p")
8863   (gnus-summary-mark-forward n gnus-expirable-mark))
8864
8865 (defun gnus-summary-mark-article-as-replied (article)
8866   "Mark ARTICLE as replied to and update the summary line.
8867 ARTICLE can also be a list of articles."
8868   (interactive (list (gnus-summary-article-number)))
8869   (let ((articles (if (listp article) article (list article))))
8870     (dolist (article articles)
8871       (push article gnus-newsgroup-replied)
8872       (let ((buffer-read-only nil))
8873         (when (gnus-summary-goto-subject article nil t)
8874           (gnus-summary-update-secondary-mark article))))))
8875
8876 (defun gnus-summary-mark-article-as-forwarded (article)
8877   "Mark ARTICLE as forwarded and update the summary line.
8878 ARTICLE can also be a list of articles."
8879   (let ((articles (if (listp article) article (list article))))
8880     (dolist (article articles)
8881       (push article gnus-newsgroup-forwarded)
8882       (let ((buffer-read-only nil))
8883         (when (gnus-summary-goto-subject article nil t)
8884           (gnus-summary-update-secondary-mark article))))))
8885
8886 (defun gnus-summary-set-bookmark (article)
8887   "Set a bookmark in current article."
8888   (interactive (list (gnus-summary-article-number)))
8889   (when (or (not (get-buffer gnus-article-buffer))
8890             (not gnus-current-article)
8891             (not gnus-article-current)
8892             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8893     (error "No current article selected"))
8894   ;; Remove old bookmark, if one exists.
8895   (let ((old (assq article gnus-newsgroup-bookmarks)))
8896     (when old
8897       (setq gnus-newsgroup-bookmarks
8898             (delq old gnus-newsgroup-bookmarks))))
8899   ;; Set the new bookmark, which is on the form
8900   ;; (article-number . line-number-in-body).
8901   (push
8902    (cons article
8903          (save-excursion
8904            (set-buffer gnus-article-buffer)
8905            (count-lines
8906             (min (point)
8907                  (save-excursion
8908                    (goto-char (point-min))
8909                    (search-forward "\n\n" nil t)
8910                    (point)))
8911             (point))))
8912    gnus-newsgroup-bookmarks)
8913   (gnus-message 6 "A bookmark has been added to the current article."))
8914
8915 (defun gnus-summary-remove-bookmark (article)
8916   "Remove the bookmark from the current article."
8917   (interactive (list (gnus-summary-article-number)))
8918   ;; Remove old bookmark, if one exists.
8919   (let ((old (assq article gnus-newsgroup-bookmarks)))
8920     (if old
8921         (progn
8922           (setq gnus-newsgroup-bookmarks
8923                 (delq old gnus-newsgroup-bookmarks))
8924           (gnus-message 6 "Removed bookmark."))
8925       (gnus-message 6 "No bookmark in current article."))))
8926
8927 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8928 (defun gnus-summary-mark-as-dormant (n)
8929   "Mark N articles forward as dormant.
8930 If N is negative, mark backward instead.  The difference between N and
8931 the actual number of articles marked is returned."
8932   (interactive "p")
8933   (gnus-summary-mark-forward n gnus-dormant-mark))
8934
8935 (defun gnus-summary-set-process-mark (article)
8936   "Set the process mark on ARTICLE and update the summary line."
8937   (setq gnus-newsgroup-processable
8938         (cons article
8939               (delq article gnus-newsgroup-processable)))
8940   (when (gnus-summary-goto-subject article)
8941     (gnus-summary-show-thread)
8942     (gnus-summary-goto-subject article)
8943     (gnus-summary-update-secondary-mark article)))
8944
8945 (defun gnus-summary-remove-process-mark (article)
8946   "Remove the process mark from ARTICLE and update the summary line."
8947   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8948   (when (gnus-summary-goto-subject article)
8949     (gnus-summary-show-thread)
8950     (gnus-summary-goto-subject article)
8951     (gnus-summary-update-secondary-mark article)))
8952
8953 (defun gnus-summary-set-saved-mark (article)
8954   "Set the process mark on ARTICLE and update the summary line."
8955   (push article gnus-newsgroup-saved)
8956   (when (gnus-summary-goto-subject article)
8957     (gnus-summary-update-secondary-mark article)))
8958
8959 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8960   "Mark N articles as read forwards.
8961 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8962 The difference between N and the actual number of articles marked is
8963 returned.
8964 Iff NO-EXPIRE, auto-expiry will be inhibited."
8965   (interactive "p")
8966   (gnus-summary-show-thread)
8967   (let ((backward (< n 0))
8968         (gnus-summary-goto-unread
8969          (and gnus-summary-goto-unread
8970               (not (eq gnus-summary-goto-unread 'never))
8971               (not (memq mark (list gnus-unread-mark
8972                                     gnus-ticked-mark gnus-dormant-mark)))))
8973         (n (abs n))
8974         (mark (or mark gnus-del-mark)))
8975     (while (and (> n 0)
8976                 (gnus-summary-mark-article nil mark no-expire)
8977                 (zerop (gnus-summary-next-subject
8978                         (if backward -1 1)
8979                         (and gnus-summary-goto-unread
8980                              (not (eq gnus-summary-goto-unread 'never)))
8981                         t)))
8982       (setq n (1- n)))
8983     (when (/= 0 n)
8984       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8985     (gnus-summary-recenter)
8986     (gnus-summary-position-point)
8987     (gnus-set-mode-line 'summary)
8988     n))
8989
8990 (defun gnus-summary-mark-article-as-read (mark)
8991   "Mark the current article quickly as read with MARK."
8992   (let ((article (gnus-summary-article-number)))
8993     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8994     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8995     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8996     (push (cons article mark) gnus-newsgroup-reads)
8997     ;; Possibly remove from cache, if that is used.
8998     (when gnus-use-cache
8999       (gnus-cache-enter-remove-article article))
9000     ;; Allow the backend to change the mark.
9001     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9002     ;; Check for auto-expiry.
9003     (when (and gnus-newsgroup-auto-expire
9004                (memq mark gnus-auto-expirable-marks))
9005       (setq mark gnus-expirable-mark)
9006       ;; Let the backend know about the mark change.
9007       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9008       (push article gnus-newsgroup-expirable))
9009     ;; Set the mark in the buffer.
9010     (gnus-summary-update-mark mark 'unread)
9011     t))
9012
9013 (defun gnus-summary-mark-article-as-unread (mark)
9014   "Mark the current article quickly as unread with MARK."
9015   (let* ((article (gnus-summary-article-number))
9016          (old-mark (gnus-summary-article-mark article)))
9017     ;; Allow the backend to change the mark.
9018     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9019     (if (eq mark old-mark)
9020         t
9021       (if (<= article 0)
9022           (progn
9023             (gnus-error 1 "Can't mark negative article numbers")
9024             nil)
9025         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9026         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9027         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9028         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9029         (cond ((= mark gnus-ticked-mark)
9030                (push article gnus-newsgroup-marked))
9031               ((= mark gnus-dormant-mark)
9032                (push article gnus-newsgroup-dormant))
9033               (t
9034                (push article gnus-newsgroup-unreads)))
9035         (gnus-pull article gnus-newsgroup-reads)
9036
9037         ;; See whether the article is to be put in the cache.
9038         (and gnus-use-cache
9039              (vectorp (gnus-summary-article-header article))
9040              (save-excursion
9041                (gnus-cache-possibly-enter-article
9042                 gnus-newsgroup-name article
9043                 (gnus-summary-article-header article)
9044                 (= mark gnus-ticked-mark)
9045                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9046
9047         ;; Fix the mark.
9048         (gnus-summary-update-mark mark 'unread)
9049         t))))
9050
9051 (defun gnus-summary-mark-article (&optional article mark no-expire)
9052   "Mark ARTICLE with MARK.  MARK can be any character.
9053 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9054 `??' (dormant) and `?E' (expirable).
9055 If MARK is nil, then the default character `?r' is used.
9056 If ARTICLE is nil, then the article on the current line will be
9057 marked.
9058 Iff NO-EXPIRE, auto-expiry will be inhibited."
9059   ;; The mark might be a string.
9060   (when (stringp mark)
9061     (setq mark (aref mark 0)))
9062   ;; If no mark is given, then we check auto-expiring.
9063   (when (null mark)
9064     (setq mark gnus-del-mark))
9065   (when (and (not no-expire)
9066              gnus-newsgroup-auto-expire
9067              (memq mark gnus-auto-expirable-marks))
9068     (setq mark gnus-expirable-mark))
9069   (let ((article (or article (gnus-summary-article-number)))
9070         (old-mark (gnus-summary-article-mark article)))
9071     ;; Allow the backend to change the mark.
9072     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9073     (if (eq mark old-mark)
9074         t
9075       (unless article
9076         (error "No article on current line"))
9077       (if (not (if (or (= mark gnus-unread-mark)
9078                        (= mark gnus-ticked-mark)
9079                        (= mark gnus-dormant-mark))
9080                    (gnus-mark-article-as-unread article mark)
9081                  (gnus-mark-article-as-read article mark)))
9082           t
9083         ;; See whether the article is to be put in the cache.
9084         (and gnus-use-cache
9085              (not (= mark gnus-canceled-mark))
9086              (vectorp (gnus-summary-article-header article))
9087              (save-excursion
9088                (gnus-cache-possibly-enter-article
9089                 gnus-newsgroup-name article
9090                 (gnus-summary-article-header article)
9091                 (= mark gnus-ticked-mark)
9092                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9093
9094         (when (gnus-summary-goto-subject article nil t)
9095           (let ((buffer-read-only nil))
9096             (gnus-summary-show-thread)
9097             ;; Fix the mark.
9098             (gnus-summary-update-mark mark 'unread)
9099             t))))))
9100
9101 (defun gnus-summary-update-secondary-mark (article)
9102   "Update the secondary (read, process, cache) mark."
9103   (gnus-summary-update-mark
9104    (cond ((memq article gnus-newsgroup-processable)
9105           gnus-process-mark)
9106          ((memq article gnus-newsgroup-cached)
9107           gnus-cached-mark)
9108          ((memq article gnus-newsgroup-replied)
9109           gnus-replied-mark)
9110          ((memq article gnus-newsgroup-forwarded)
9111           gnus-forwarded-mark)
9112          ((memq article gnus-newsgroup-saved)
9113           gnus-saved-mark)
9114          ((memq article gnus-newsgroup-recent)
9115           gnus-recent-mark)
9116          ((memq article gnus-newsgroup-unseen)
9117           gnus-unseen-mark)
9118          (t gnus-no-mark))
9119    'replied)
9120   (when (gnus-visual-p 'summary-highlight 'highlight)
9121     (gnus-run-hooks 'gnus-summary-update-hook))
9122   t)
9123
9124 (defun gnus-summary-update-mark (mark type)
9125   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9126         (buffer-read-only nil))
9127     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9128     (when forward
9129       (when (looking-at "\r")
9130         (incf forward))
9131       (when (<= (+ forward (point)) (point-max))
9132         ;; Go to the right position on the line.
9133         (goto-char (+ forward (point)))
9134         ;; Replace the old mark with the new mark.
9135         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9136         ;; Optionally update the marks by some user rule.
9137         (when (eq type 'unread)
9138           (gnus-data-set-mark
9139            (gnus-data-find (gnus-summary-article-number)) mark)
9140           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9141
9142 (defun gnus-mark-article-as-read (article &optional mark)
9143   "Enter ARTICLE in the pertinent lists and remove it from others."
9144   ;; Make the article expirable.
9145   (let ((mark (or mark gnus-del-mark)))
9146     (if (= mark gnus-expirable-mark)
9147         (push article gnus-newsgroup-expirable)
9148       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9149     ;; Remove from unread and marked lists.
9150     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9151     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9152     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9153     (push (cons article mark) gnus-newsgroup-reads)
9154     ;; Possibly remove from cache, if that is used.
9155     (when gnus-use-cache
9156       (gnus-cache-enter-remove-article article))
9157     t))
9158
9159 (defun gnus-mark-article-as-unread (article &optional mark)
9160   "Enter ARTICLE in the pertinent lists and remove it from others."
9161   (let ((mark (or mark gnus-ticked-mark)))
9162     (if (<= article 0)
9163         (progn
9164           (gnus-error 1 "Can't mark negative article numbers")
9165           nil)
9166       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9167             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9168             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9169             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9170
9171       ;; Unsuppress duplicates?
9172       (when gnus-suppress-duplicates
9173         (gnus-dup-unsuppress-article article))
9174
9175       (cond ((= mark gnus-ticked-mark)
9176              (push article gnus-newsgroup-marked))
9177             ((= mark gnus-dormant-mark)
9178              (push article gnus-newsgroup-dormant))
9179             (t
9180              (push article gnus-newsgroup-unreads)))
9181       (gnus-pull article gnus-newsgroup-reads)
9182       t)))
9183
9184 (defalias 'gnus-summary-mark-as-unread-forward
9185   'gnus-summary-tick-article-forward)
9186 (make-obsolete 'gnus-summary-mark-as-unread-forward
9187                'gnus-summary-tick-article-forward)
9188 (defun gnus-summary-tick-article-forward (n)
9189   "Tick N articles forwards.
9190 If N is negative, tick backwards instead.
9191 The difference between N and the number of articles ticked is returned."
9192   (interactive "p")
9193   (gnus-summary-mark-forward n gnus-ticked-mark))
9194
9195 (defalias 'gnus-summary-mark-as-unread-backward
9196   'gnus-summary-tick-article-backward)
9197 (make-obsolete 'gnus-summary-mark-as-unread-backward
9198                'gnus-summary-tick-article-backward)
9199 (defun gnus-summary-tick-article-backward (n)
9200   "Tick N articles backwards.
9201 The difference between N and the number of articles ticked is returned."
9202   (interactive "p")
9203   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9204
9205 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9206 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9207 (defun gnus-summary-tick-article (&optional article clear-mark)
9208   "Mark current article as unread.
9209 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9210 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9211   (interactive)
9212   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9213                                        gnus-ticked-mark)))
9214
9215 (defun gnus-summary-mark-as-read-forward (n)
9216   "Mark N articles as read forwards.
9217 If N is negative, mark backwards instead.
9218 The difference between N and the actual number of articles marked is
9219 returned."
9220   (interactive "p")
9221   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9222
9223 (defun gnus-summary-mark-as-read-backward (n)
9224   "Mark the N articles as read backwards.
9225 The difference between N and the actual number of articles marked is
9226 returned."
9227   (interactive "p")
9228   (gnus-summary-mark-forward
9229    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9230
9231 (defun gnus-summary-mark-as-read (&optional article mark)
9232   "Mark current article as read.
9233 ARTICLE specifies the article to be marked as read.
9234 MARK specifies a string to be inserted at the beginning of the line."
9235   (gnus-summary-mark-article article mark))
9236
9237 (defun gnus-summary-clear-mark-forward (n)
9238   "Clear marks from N articles forward.
9239 If N is negative, clear backward instead.
9240 The difference between N and the number of marks cleared is returned."
9241   (interactive "p")
9242   (gnus-summary-mark-forward n gnus-unread-mark))
9243
9244 (defun gnus-summary-clear-mark-backward (n)
9245   "Clear marks from N articles backward.
9246 The difference between N and the number of marks cleared is returned."
9247   (interactive "p")
9248   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9249
9250 (defun gnus-summary-mark-unread-as-read ()
9251   "Intended to be used by `gnus-summary-mark-article-hook'."
9252   (when (memq gnus-current-article gnus-newsgroup-unreads)
9253     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9254
9255 (defun gnus-summary-mark-read-and-unread-as-read ()
9256   "Intended to be used by `gnus-summary-mark-article-hook'."
9257   (let ((mark (gnus-summary-article-mark)))
9258     (when (or (gnus-unread-mark-p mark)
9259               (gnus-read-mark-p mark))
9260       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9261
9262 (defun gnus-summary-mark-unread-as-ticked ()
9263   "Intended to be used by `gnus-summary-mark-article-hook'."
9264   (when (memq gnus-current-article gnus-newsgroup-unreads)
9265     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9266
9267 (defun gnus-summary-mark-region-as-read (point mark all)
9268   "Mark all unread articles between point and mark as read.
9269 If given a prefix, mark all articles between point and mark as read,
9270 even ticked and dormant ones."
9271   (interactive "r\nP")
9272   (save-excursion
9273     (let (article)
9274       (goto-char point)
9275       (beginning-of-line)
9276       (while (and
9277               (< (point) mark)
9278               (progn
9279                 (when (or all
9280                           (memq (setq article (gnus-summary-article-number))
9281                                 gnus-newsgroup-unreads))
9282                   (gnus-summary-mark-article article gnus-del-mark))
9283                 t)
9284               (gnus-summary-find-next))))))
9285
9286 (defun gnus-summary-mark-below (score mark)
9287   "Mark articles with score less than SCORE with MARK."
9288   (interactive "P\ncMark: ")
9289   (setq score (if score
9290                   (prefix-numeric-value score)
9291                 (or gnus-summary-default-score 0)))
9292   (save-excursion
9293     (set-buffer gnus-summary-buffer)
9294     (goto-char (point-min))
9295     (while
9296         (progn
9297           (and (< (gnus-summary-article-score) score)
9298                (gnus-summary-mark-article nil mark))
9299           (gnus-summary-find-next)))))
9300
9301 (defun gnus-summary-kill-below (&optional score)
9302   "Mark articles with score below SCORE as read."
9303   (interactive "P")
9304   (gnus-summary-mark-below score gnus-killed-mark))
9305
9306 (defun gnus-summary-clear-above (&optional score)
9307   "Clear all marks from articles with score above SCORE."
9308   (interactive "P")
9309   (gnus-summary-mark-above score gnus-unread-mark))
9310
9311 (defun gnus-summary-tick-above (&optional score)
9312   "Tick all articles with score above SCORE."
9313   (interactive "P")
9314   (gnus-summary-mark-above score gnus-ticked-mark))
9315
9316 (defun gnus-summary-mark-above (score mark)
9317   "Mark articles with score over SCORE with MARK."
9318   (interactive "P\ncMark: ")
9319   (setq score (if score
9320                   (prefix-numeric-value score)
9321                 (or gnus-summary-default-score 0)))
9322   (save-excursion
9323     (set-buffer gnus-summary-buffer)
9324     (goto-char (point-min))
9325     (while (and (progn
9326                   (when (> (gnus-summary-article-score) score)
9327                     (gnus-summary-mark-article nil mark))
9328                   t)
9329                 (gnus-summary-find-next)))))
9330
9331 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9332 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9333 (defun gnus-summary-limit-include-expunged (&optional no-error)
9334   "Display all the hidden articles that were expunged for low scores."
9335   (interactive)
9336   (let ((buffer-read-only nil))
9337     (let ((scored gnus-newsgroup-scored)
9338           headers h)
9339       (while scored
9340         (unless (gnus-summary-article-header (caar scored))
9341           (and (setq h (gnus-number-to-header (caar scored)))
9342                (< (cdar scored) gnus-summary-expunge-below)
9343                (push h headers)))
9344         (setq scored (cdr scored)))
9345       (if (not headers)
9346           (when (not no-error)
9347             (error "No expunged articles hidden"))
9348         (goto-char (point-min))
9349         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9350         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9351         (mapcar (lambda (x) (push (mail-header-number x)
9352                                   gnus-newsgroup-limit))
9353                 headers)
9354         (gnus-summary-prepare-unthreaded (nreverse headers))
9355         (goto-char (point-min))
9356         (gnus-summary-position-point)
9357         t))))
9358
9359 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9360   "Mark all unread articles in this newsgroup as read.
9361 If prefix argument ALL is non-nil, ticked and dormant articles will
9362 also be marked as read.
9363 If QUIETLY is non-nil, no questions will be asked.
9364 If TO-HERE is non-nil, it should be a point in the buffer.  All
9365 articles before (after, if REVERSE is set) this point will be marked as read.
9366 Note that this function will only catch up the unread article
9367 in the current summary buffer limitation.
9368 The number of articles marked as read is returned."
9369   (interactive "P")
9370   (prog1
9371       (save-excursion
9372         (when (or quietly
9373                   (not gnus-interactive-catchup) ;Without confirmation?
9374                   gnus-expert-user
9375                   (gnus-y-or-n-p
9376                    (if all
9377                        "Mark absolutely all articles as read? "
9378                      "Mark all unread articles as read? ")))
9379           (if (and not-mark
9380                    (not gnus-newsgroup-adaptive)
9381                    (not gnus-newsgroup-auto-expire)
9382                    (not gnus-suppress-duplicates)
9383                    (or (not gnus-use-cache)
9384                        (eq gnus-use-cache 'passive)))
9385               (progn
9386                 (when all
9387                   (setq gnus-newsgroup-marked nil
9388                         gnus-newsgroup-dormant nil))
9389                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9390             ;; We actually mark all articles as canceled, which we
9391             ;; have to do when using auto-expiry or adaptive scoring.
9392             (gnus-summary-show-all-threads)
9393             (if (and to-here reverse)
9394                 (progn
9395                   (goto-char to-here)
9396                   (while (and
9397                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9398                           (gnus-summary-find-next (not all) nil nil t))))
9399               (when (gnus-summary-first-subject (not all) t)
9400                 (while (and
9401                         (if to-here (< (point) to-here) t)
9402                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9403                         (gnus-summary-find-next (not all) nil nil t)))))
9404             (gnus-set-mode-line 'summary))
9405           t))
9406     (gnus-summary-position-point)))
9407
9408 (defun gnus-summary-catchup-to-here (&optional all)
9409   "Mark all unticked articles before the current one as read.
9410 If ALL is non-nil, also mark ticked and dormant articles as read."
9411   (interactive "P")
9412   (save-excursion
9413     (gnus-save-hidden-threads
9414       (let ((beg (point)))
9415         ;; We check that there are unread articles.
9416         (when (or all (gnus-summary-find-prev))
9417           (gnus-summary-catchup all t beg)))))
9418   (gnus-summary-position-point))
9419
9420 (defun gnus-summary-catchup-from-here (&optional all)
9421   "Mark all unticked articles after the current one as read.
9422 If ALL is non-nil, also mark ticked and dormant articles as read."
9423   (interactive "P")
9424   (save-excursion
9425     (gnus-save-hidden-threads
9426       (let ((beg (point)))
9427         ;; We check that there are unread articles.
9428         (when (or all (gnus-summary-find-next))
9429           (gnus-summary-catchup all t beg nil t)))))
9430   (gnus-summary-position-point))
9431
9432 (defun gnus-summary-catchup-all (&optional quietly)
9433   "Mark all articles in this newsgroup as read."
9434   (interactive "P")
9435   (gnus-summary-catchup t quietly))
9436
9437 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9438   "Mark all unread articles in this group as read, then exit.
9439 If prefix argument ALL is non-nil, all articles are marked as read.
9440 If QUIETLY is non-nil, no questions will be asked."
9441   (interactive "P")
9442   (when (gnus-summary-catchup all quietly nil 'fast)
9443     ;; Select next newsgroup or exit.
9444     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9445              (eq gnus-auto-select-next 'quietly))
9446         (gnus-summary-next-group nil)
9447       (gnus-summary-exit))))
9448
9449 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9450   "Mark all articles in this newsgroup as read, and then exit."
9451   (interactive "P")
9452   (gnus-summary-catchup-and-exit t quietly))
9453
9454 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9455   "Mark all articles in this group as read and select the next group.
9456 If given a prefix, mark all articles, unread as well as ticked, as
9457 read."
9458   (interactive "P")
9459   (save-excursion
9460     (gnus-summary-catchup all))
9461   (gnus-summary-next-group))
9462
9463 ;;;
9464 ;;; with article
9465 ;;;
9466
9467 (defmacro gnus-with-article (article &rest forms)
9468   "Select ARTICLE and perform FORMS in the original article buffer.
9469 Then replace the article with the result."
9470   `(progn
9471      ;; We don't want the article to be marked as read.
9472      (let (gnus-mark-article-hook)
9473        (gnus-summary-select-article t t nil ,article))
9474      (set-buffer gnus-original-article-buffer)
9475      ,@forms
9476      (if (not (gnus-check-backend-function
9477                'request-replace-article (car gnus-article-current)))
9478          (gnus-message 5 "Read-only group; not replacing")
9479        (unless (gnus-request-replace-article
9480                 ,article (car gnus-article-current)
9481                 (current-buffer) t)
9482          (error "Couldn't replace article")))
9483      ;; The cache and backlog have to be flushed somewhat.
9484      (when gnus-keep-backlog
9485        (gnus-backlog-remove-article
9486         (car gnus-article-current) (cdr gnus-article-current)))
9487      (when gnus-use-cache
9488        (gnus-cache-update-article
9489         (car gnus-article-current) (cdr gnus-article-current)))))
9490
9491 (put 'gnus-with-article 'lisp-indent-function 1)
9492 (put 'gnus-with-article 'edebug-form-spec '(form body))
9493
9494 ;; Thread-based commands.
9495
9496 (defun gnus-summary-articles-in-thread (&optional article)
9497   "Return a list of all articles in the current thread.
9498 If ARTICLE is non-nil, return all articles in the thread that starts
9499 with that article."
9500   (let* ((article (or article (gnus-summary-article-number)))
9501          (data (gnus-data-find-list article))
9502          (top-level (gnus-data-level (car data)))
9503          (top-subject
9504           (cond ((null gnus-thread-operation-ignore-subject)
9505                  (gnus-simplify-subject-re
9506                   (mail-header-subject (gnus-data-header (car data)))))
9507                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9508                  (gnus-simplify-subject-fuzzy
9509                   (mail-header-subject (gnus-data-header (car data)))))
9510                 (t nil)))
9511          (end-point (save-excursion
9512                       (if (gnus-summary-go-to-next-thread)
9513                           (point) (point-max))))
9514          articles)
9515     (while (and data
9516                 (< (gnus-data-pos (car data)) end-point))
9517       (when (or (not top-subject)
9518                 (string= top-subject
9519                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9520                              (gnus-simplify-subject-fuzzy
9521                               (mail-header-subject
9522                                (gnus-data-header (car data))))
9523                            (gnus-simplify-subject-re
9524                             (mail-header-subject
9525                              (gnus-data-header (car data)))))))
9526         (push (gnus-data-number (car data)) articles))
9527       (unless (and (setq data (cdr data))
9528                    (> (gnus-data-level (car data)) top-level))
9529         (setq data nil)))
9530     ;; Return the list of articles.
9531     (nreverse articles)))
9532
9533 (defun gnus-summary-rethread-current ()
9534   "Rethread the thread the current article is part of."
9535   (interactive)
9536   (let* ((gnus-show-threads t)
9537          (article (gnus-summary-article-number))
9538          (id (mail-header-id (gnus-summary-article-header)))
9539          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9540     (unless id
9541       (error "No article on the current line"))
9542     (gnus-rebuild-thread id)
9543     (gnus-summary-goto-subject article)))
9544
9545 (defun gnus-summary-reparent-thread ()
9546   "Make the current article child of the marked (or previous) article.
9547
9548 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9549 is non-nil or the Subject: of both articles are the same."
9550   (interactive)
9551   (unless (not (gnus-group-read-only-p))
9552     (error "The current newsgroup does not support article editing"))
9553   (unless (<= (length gnus-newsgroup-processable) 1)
9554     (error "No more than one article may be marked"))
9555   (save-window-excursion
9556     (let ((gnus-article-buffer " *reparent*")
9557           (current-article (gnus-summary-article-number))
9558           ;; First grab the marked article, otherwise one line up.
9559           (parent-article (if (not (null gnus-newsgroup-processable))
9560                               (car gnus-newsgroup-processable)
9561                             (save-excursion
9562                               (if (eq (forward-line -1) 0)
9563                                   (gnus-summary-article-number)
9564                                 (error "Beginning of summary buffer"))))))
9565       (unless (not (eq current-article parent-article))
9566         (error "An article may not be self-referential"))
9567       (let ((message-id (mail-header-id
9568                          (gnus-summary-article-header parent-article))))
9569         (unless (and message-id (not (equal message-id "")))
9570           (error "No message-id in desired parent"))
9571         (gnus-with-article current-article
9572           (save-restriction
9573             (goto-char (point-min))
9574             (message-narrow-to-head)
9575             (if (re-search-forward "^References: " nil t)
9576                 (progn
9577                   (re-search-forward "^[^ \t]" nil t)
9578                   (forward-line -1)
9579                   (end-of-line)
9580                   (insert " " message-id))
9581               (insert "References: " message-id "\n"))))
9582         (set-buffer gnus-summary-buffer)
9583         (gnus-summary-unmark-all-processable)
9584         (gnus-summary-update-article current-article)
9585         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9586             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9587         (gnus-summary-rethread-current)
9588         (gnus-message 3 "Article %d is now the child of article %d"
9589                       current-article parent-article)))))
9590
9591 (defun gnus-summary-toggle-threads (&optional arg)
9592   "Toggle showing conversation threads.
9593 If ARG is positive number, turn showing conversation threads on."
9594   (interactive "P")
9595   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9596     (setq gnus-show-threads
9597           (if (null arg) (not gnus-show-threads)
9598             (> (prefix-numeric-value arg) 0)))
9599     (gnus-summary-prepare)
9600     (gnus-summary-goto-subject current)
9601     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9602     (gnus-summary-position-point)))
9603
9604 (defun gnus-summary-show-all-threads ()
9605   "Show all threads."
9606   (interactive)
9607   (save-excursion
9608     (let ((buffer-read-only nil))
9609       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9610   (gnus-summary-position-point))
9611
9612 (defun gnus-summary-show-thread ()
9613   "Show thread subtrees.
9614 Returns nil if no thread was there to be shown."
9615   (interactive)
9616   (let ((buffer-read-only nil)
9617         (orig (point))
9618         ;; first goto end then to beg, to have point at beg after let
9619         (end (progn (end-of-line) (point)))
9620         (beg (progn (beginning-of-line) (point))))
9621     (prog1
9622         ;; Any hidden lines here?
9623         (search-forward "\r" end t)
9624       (subst-char-in-region beg end ?\^M ?\n t)
9625       (goto-char orig)
9626       (gnus-summary-position-point))))
9627
9628 (defun gnus-summary-hide-all-threads ()
9629   "Hide all thread subtrees."
9630   (interactive)
9631   (save-excursion
9632     (goto-char (point-min))
9633     (gnus-summary-hide-thread)
9634     (while (zerop (gnus-summary-next-thread 1 t))
9635       (gnus-summary-hide-thread)))
9636   (gnus-summary-position-point))
9637
9638 (defun gnus-summary-hide-thread ()
9639   "Hide thread subtrees.
9640 Returns nil if no threads were there to be hidden."
9641   (interactive)
9642   (let ((buffer-read-only nil)
9643         (start (point))
9644         (article (gnus-summary-article-number)))
9645     (goto-char start)
9646     ;; Go forward until either the buffer ends or the subthread
9647     ;; ends.
9648     (when (and (not (eobp))
9649                (or (zerop (gnus-summary-next-thread 1 t))
9650                    (goto-char (point-max))))
9651       (prog1
9652           (if (and (> (point) start)
9653                    (search-backward "\n" start t))
9654               (progn
9655                 (subst-char-in-region start (point) ?\n ?\^M)
9656                 (gnus-summary-goto-subject article))
9657             (goto-char start)
9658             nil)))))
9659
9660 (defun gnus-summary-go-to-next-thread (&optional previous)
9661   "Go to the same level (or less) next thread.
9662 If PREVIOUS is non-nil, go to previous thread instead.
9663 Return the article number moved to, or nil if moving was impossible."
9664   (let ((level (gnus-summary-thread-level))
9665         (way (if previous -1 1))
9666         (beg (point)))
9667     (forward-line way)
9668     (while (and (not (eobp))
9669                 (< level (gnus-summary-thread-level)))
9670       (forward-line way))
9671     (if (eobp)
9672         (progn
9673           (goto-char beg)
9674           nil)
9675       (setq beg (point))
9676       (prog1
9677           (gnus-summary-article-number)
9678         (goto-char beg)))))
9679
9680 (defun gnus-summary-next-thread (n &optional silent)
9681   "Go to the same level next N'th thread.
9682 If N is negative, search backward instead.
9683 Returns the difference between N and the number of skips actually
9684 done.
9685
9686 If SILENT, don't output messages."
9687   (interactive "p")
9688   (let ((backward (< n 0))
9689         (n (abs n)))
9690     (while (and (> n 0)
9691                 (gnus-summary-go-to-next-thread backward))
9692       (decf n))
9693     (unless silent
9694       (gnus-summary-position-point))
9695     (when (and (not silent) (/= 0 n))
9696       (gnus-message 7 "No more threads"))
9697     n))
9698
9699 (defun gnus-summary-prev-thread (n)
9700   "Go to the same level previous N'th thread.
9701 Returns the difference between N and the number of skips actually
9702 done."
9703   (interactive "p")
9704   (gnus-summary-next-thread (- n)))
9705
9706 (defun gnus-summary-go-down-thread ()
9707   "Go down one level in the current thread."
9708   (let ((children (gnus-summary-article-children)))
9709     (when children
9710       (gnus-summary-goto-subject (car children)))))
9711
9712 (defun gnus-summary-go-up-thread ()
9713   "Go up one level in the current thread."
9714   (let ((parent (gnus-summary-article-parent)))
9715     (when parent
9716       (gnus-summary-goto-subject parent))))
9717
9718 (defun gnus-summary-down-thread (n)
9719   "Go down thread N steps.
9720 If N is negative, go up instead.
9721 Returns the difference between N and how many steps down that were
9722 taken."
9723   (interactive "p")
9724   (let ((up (< n 0))
9725         (n (abs n)))
9726     (while (and (> n 0)
9727                 (if up (gnus-summary-go-up-thread)
9728                   (gnus-summary-go-down-thread)))
9729       (setq n (1- n)))
9730     (gnus-summary-position-point)
9731     (when (/= 0 n)
9732       (gnus-message 7 "Can't go further"))
9733     n))
9734
9735 (defun gnus-summary-up-thread (n)
9736   "Go up thread N steps.
9737 If N is negative, go down instead.
9738 Returns the difference between N and how many steps down that were
9739 taken."
9740   (interactive "p")
9741   (gnus-summary-down-thread (- n)))
9742
9743 (defun gnus-summary-top-thread ()
9744   "Go to the top of the thread."
9745   (interactive)
9746   (while (gnus-summary-go-up-thread))
9747   (gnus-summary-article-number))
9748
9749 (defun gnus-summary-kill-thread (&optional unmark)
9750   "Mark articles under current thread as read.
9751 If the prefix argument is positive, remove any kinds of marks.
9752 If the prefix argument is negative, tick articles instead."
9753   (interactive "P")
9754   (when unmark
9755     (setq unmark (prefix-numeric-value unmark)))
9756   (let ((articles (gnus-summary-articles-in-thread)))
9757     (save-excursion
9758       ;; Expand the thread.
9759       (gnus-summary-show-thread)
9760       ;; Mark all the articles.
9761       (while articles
9762         (gnus-summary-goto-subject (car articles))
9763         (cond ((null unmark)
9764                (gnus-summary-mark-article-as-read gnus-killed-mark))
9765               ((> unmark 0)
9766                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9767               (t
9768                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9769         (setq articles (cdr articles))))
9770     ;; Hide killed subtrees.
9771     (and (null unmark)
9772          gnus-thread-hide-killed
9773          (gnus-summary-hide-thread))
9774     ;; If marked as read, go to next unread subject.
9775     (when (null unmark)
9776       ;; Go to next unread subject.
9777       (gnus-summary-next-subject 1 t)))
9778   (gnus-set-mode-line 'summary))
9779
9780 ;; Summary sorting commands
9781
9782 (defun gnus-summary-sort-by-number (&optional reverse)
9783   "Sort the summary buffer by article number.
9784 Argument REVERSE means reverse order."
9785   (interactive "P")
9786   (gnus-summary-sort 'number reverse))
9787
9788 (defun gnus-summary-sort-by-author (&optional reverse)
9789   "Sort the summary buffer by author name alphabetically.
9790 If `case-fold-search' is non-nil, case of letters is ignored.
9791 Argument REVERSE means reverse order."
9792   (interactive "P")
9793   (gnus-summary-sort 'author reverse))
9794
9795 (defun gnus-summary-sort-by-subject (&optional reverse)
9796   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9797 If `case-fold-search' is non-nil, case of letters is ignored.
9798 Argument REVERSE means reverse order."
9799   (interactive "P")
9800   (gnus-summary-sort 'subject reverse))
9801
9802 (defun gnus-summary-sort-by-date (&optional reverse)
9803   "Sort the summary buffer by date.
9804 Argument REVERSE means reverse order."
9805   (interactive "P")
9806   (gnus-summary-sort 'date reverse))
9807
9808 (defun gnus-summary-sort-by-score (&optional reverse)
9809   "Sort the summary buffer by score.
9810 Argument REVERSE means reverse order."
9811   (interactive "P")
9812   (gnus-summary-sort 'score reverse))
9813
9814 (defun gnus-summary-sort-by-lines (&optional reverse)
9815   "Sort the summary buffer by the number of lines.
9816 Argument REVERSE means reverse order."
9817   (interactive "P")
9818   (gnus-summary-sort 'lines reverse))
9819
9820 (defun gnus-summary-sort-by-chars (&optional reverse)
9821   "Sort the summary buffer by article length.
9822 Argument REVERSE means reverse order."
9823   (interactive "P")
9824   (gnus-summary-sort 'chars reverse))
9825
9826 (defun gnus-summary-sort-by-original (&optional reverse)
9827   "Sort the summary buffer using the default sorting method.
9828 Argument REVERSE means reverse order."
9829   (interactive "P")
9830   (let* ((buffer-read-only)
9831          (gnus-summary-prepare-hook nil))
9832     ;; We do the sorting by regenerating the threads.
9833     (gnus-summary-prepare)
9834     ;; Hide subthreads if needed.
9835     (when (and gnus-show-threads gnus-thread-hide-subtree)
9836       (gnus-summary-hide-all-threads))))
9837
9838 (defun gnus-summary-sort (predicate reverse)
9839   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9840   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9841          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9842          (gnus-thread-sort-functions
9843           (if (not reverse)
9844               thread
9845             `(lambda (t1 t2)
9846                (,thread t2 t1))))
9847          (gnus-sort-gathered-threads-function
9848           gnus-thread-sort-functions)
9849          (gnus-article-sort-functions
9850           (if (not reverse)
9851               article
9852             `(lambda (t1 t2)
9853                (,article t2 t1))))
9854          (buffer-read-only)
9855          (gnus-summary-prepare-hook nil))
9856     ;; We do the sorting by regenerating the threads.
9857     (gnus-summary-prepare)
9858     ;; Hide subthreads if needed.
9859     (when (and gnus-show-threads gnus-thread-hide-subtree)
9860       (gnus-summary-hide-all-threads))))
9861
9862 ;; Summary saving commands.
9863
9864 (defun gnus-summary-save-article (&optional n not-saved)
9865   "Save the current article using the default saver function.
9866 If N is a positive number, save the N next articles.
9867 If N is a negative number, save the N previous articles.
9868 If N is nil and any articles have been marked with the process mark,
9869 save those articles instead.
9870 The variable `gnus-default-article-saver' specifies the saver function."
9871   (interactive "P")
9872   (let* ((articles (gnus-summary-work-articles n))
9873          (save-buffer (save-excursion
9874                         (nnheader-set-temp-buffer " *Gnus Save*")))
9875          (num (length articles))
9876          header file)
9877     (dolist (article articles)
9878       (setq header (gnus-summary-article-header article))
9879       (if (not (vectorp header))
9880           ;; This is a pseudo-article.
9881           (if (assq 'name header)
9882               (gnus-copy-file (cdr (assq 'name header)))
9883             (gnus-message 1 "Article %d is unsaveable" article))
9884         ;; This is a real article.
9885         (save-window-excursion
9886           (gnus-summary-select-article t nil nil article))
9887         (save-excursion
9888           (set-buffer save-buffer)
9889           (erase-buffer)
9890           (insert-buffer-substring gnus-original-article-buffer))
9891         (setq file (gnus-article-save save-buffer file num))
9892         (gnus-summary-remove-process-mark article)
9893         (unless not-saved
9894           (gnus-summary-set-saved-mark article))))
9895     (gnus-kill-buffer save-buffer)
9896     (gnus-summary-position-point)
9897     (gnus-set-mode-line 'summary)
9898     n))
9899
9900 (defun gnus-summary-pipe-output (&optional arg)
9901   "Pipe the current article to a subprocess.
9902 If N is a positive number, pipe the N next articles.
9903 If N is a negative number, pipe the N previous articles.
9904 If N is nil and any articles have been marked with the process mark,
9905 pipe those articles instead."
9906   (interactive "P")
9907   (require 'gnus-art)
9908   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9909     (gnus-summary-save-article arg t))
9910   (let ((buffer (get-buffer "*Shell Command Output*")))
9911     (if (and buffer
9912              (with-current-buffer buffer (> (point-max) (point-min))))
9913         (gnus-configure-windows 'pipe))))
9914
9915 (defun gnus-summary-save-article-mail (&optional arg)
9916   "Append the current article to an mail file.
9917 If N is a positive number, save the N next articles.
9918 If N is a negative number, save the N previous articles.
9919 If N is nil and any articles have been marked with the process mark,
9920 save those articles instead."
9921   (interactive "P")
9922   (require 'gnus-art)
9923   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9924     (gnus-summary-save-article arg)))
9925
9926 (defun gnus-summary-save-article-rmail (&optional arg)
9927   "Append the current article to an rmail file.
9928 If N is a positive number, save the N next articles.
9929 If N is a negative number, save the N previous articles.
9930 If N is nil and any articles have been marked with the process mark,
9931 save those articles instead."
9932   (interactive "P")
9933   (require 'gnus-art)
9934   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9935     (gnus-summary-save-article arg)))
9936
9937 (defun gnus-summary-save-article-file (&optional arg)
9938   "Append the current article to a file.
9939 If N is a positive number, save the N next articles.
9940 If N is a negative number, save the N previous articles.
9941 If N is nil and any articles have been marked with the process mark,
9942 save those articles instead."
9943   (interactive "P")
9944   (require 'gnus-art)
9945   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9946     (gnus-summary-save-article arg)))
9947
9948 (defun gnus-summary-write-article-file (&optional arg)
9949   "Write the current article to a file, deleting the previous file.
9950 If N is a positive number, save the N next articles.
9951 If N is a negative number, save the N previous articles.
9952 If N is nil and any articles have been marked with the process mark,
9953 save those articles instead."
9954   (interactive "P")
9955   (require 'gnus-art)
9956   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9957     (gnus-summary-save-article arg)))
9958
9959 (defun gnus-summary-save-article-body-file (&optional arg)
9960   "Append the current article body to a file.
9961 If N is a positive number, save the N next articles.
9962 If N is a negative number, save the N previous articles.
9963 If N is nil and any articles have been marked with the process mark,
9964 save those articles instead."
9965   (interactive "P")
9966   (require 'gnus-art)
9967   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9968     (gnus-summary-save-article arg)))
9969
9970 (defun gnus-summary-pipe-message (program)
9971   "Pipe the current article through PROGRAM."
9972   (interactive "sProgram: ")
9973   (gnus-summary-select-article)
9974   (let ((mail-header-separator ""))
9975     (gnus-eval-in-buffer-window gnus-article-buffer
9976       (save-restriction
9977         (widen)
9978         (let ((start (window-start))
9979               buffer-read-only)
9980           (message-pipe-buffer-body program)
9981           (set-window-start (get-buffer-window (current-buffer)) start))))))
9982
9983 (defun gnus-get-split-value (methods)
9984   "Return a value based on the split METHODS."
9985   (let (split-name method result match)
9986     (when methods
9987       (save-excursion
9988         (set-buffer gnus-original-article-buffer)
9989         (save-restriction
9990           (nnheader-narrow-to-headers)
9991           (while (and methods (not split-name))
9992             (goto-char (point-min))
9993             (setq method (pop methods))
9994             (setq match (car method))
9995             (when (cond
9996                    ((stringp match)
9997                     ;; Regular expression.
9998                     (ignore-errors
9999                       (re-search-forward match nil t)))
10000                    ((gnus-functionp match)
10001                     ;; Function.
10002                     (save-restriction
10003                       (widen)
10004                       (setq result (funcall match gnus-newsgroup-name))))
10005                    ((consp match)
10006                     ;; Form.
10007                     (save-restriction
10008                       (widen)
10009                       (setq result (eval match)))))
10010               (setq split-name (cdr method))
10011               (cond ((stringp result)
10012                      (push (expand-file-name
10013                             result gnus-article-save-directory)
10014                            split-name))
10015                     ((consp result)
10016                      (setq split-name (append result split-name)))))))))
10017     (nreverse split-name)))
10018
10019 (defun gnus-valid-move-group-p (group)
10020   (and (boundp group)
10021        (symbol-name group)
10022        (symbol-value group)
10023        (gnus-get-function (gnus-find-method-for-group
10024                            (symbol-name group)) 'request-accept-article t)))
10025
10026 (defun gnus-read-move-group-name (prompt default articles prefix)
10027   "Read a group name."
10028   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10029          (minibuffer-confirm-incomplete nil) ; XEmacs
10030          (prom
10031           (format "%s %s to:"
10032                   prompt
10033                   (if (> (length articles) 1)
10034                       (format "these %d articles" (length articles))
10035                     "this article")))
10036          (to-newsgroup
10037           (cond
10038            ((null split-name)
10039             (gnus-completing-read default prom
10040                                   gnus-active-hashtb
10041                                   'gnus-valid-move-group-p
10042                                   nil prefix
10043                                   'gnus-group-history))
10044            ((= 1 (length split-name))
10045             (gnus-completing-read (car split-name) prom
10046                                   gnus-active-hashtb
10047                                   'gnus-valid-move-group-p
10048                                   nil nil
10049                                   'gnus-group-history))
10050            (t
10051             (gnus-completing-read nil prom
10052                                   (mapcar (lambda (el) (list el))
10053                                           (nreverse split-name))
10054                                   nil nil nil
10055                                   'gnus-group-history))))
10056          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10057     (when to-newsgroup
10058       (if (or (string= to-newsgroup "")
10059               (string= to-newsgroup prefix))
10060           (setq to-newsgroup default))
10061       (unless to-newsgroup
10062         (error "No group name entered"))
10063       (or (gnus-active to-newsgroup)
10064           (gnus-activate-group to-newsgroup nil nil to-method)
10065           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10066                                      to-newsgroup))
10067               (or (and (gnus-request-create-group to-newsgroup to-method)
10068                        (gnus-activate-group
10069                         to-newsgroup nil nil to-method)
10070                        (gnus-subscribe-group to-newsgroup))
10071                   (error "Couldn't create group %s" to-newsgroup)))
10072           (error "No such group: %s" to-newsgroup)))
10073     to-newsgroup))
10074
10075 (defun gnus-summary-save-parts (type dir n &optional reverse)
10076   "Save parts matching TYPE to DIR.
10077 If REVERSE, save parts that do not match TYPE."
10078   (interactive
10079    (list (read-string "Save parts of type: "
10080                       (or (car gnus-summary-save-parts-type-history)
10081                           gnus-summary-save-parts-default-mime)
10082                       'gnus-summary-save-parts-type-history)
10083          (setq gnus-summary-save-parts-last-directory
10084                (read-file-name "Save to directory: "
10085                                gnus-summary-save-parts-last-directory
10086                                nil t))
10087          current-prefix-arg))
10088   (gnus-summary-iterate n
10089     (let ((gnus-display-mime-function nil)
10090           (gnus-inhibit-treatment t))
10091       (gnus-summary-select-article))
10092     (save-excursion
10093       (set-buffer gnus-article-buffer)
10094       (let ((handles (or gnus-article-mime-handles
10095                          (mm-dissect-buffer) (mm-uu-dissect))))
10096         (when handles
10097           (gnus-summary-save-parts-1 type dir handles reverse)
10098           (unless gnus-article-mime-handles ;; Don't destroy this case.
10099             (mm-destroy-parts handles)))))))
10100
10101 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10102   (if (stringp (car handle))
10103       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10104               (cdr handle))
10105     (when (if reverse
10106               (not (string-match type (mm-handle-media-type handle)))
10107             (string-match type (mm-handle-media-type handle)))
10108       (let ((file (expand-file-name
10109                    (file-name-nondirectory
10110                     (or
10111                      (mail-content-type-get
10112                       (mm-handle-disposition handle) 'filename)
10113                      (concat gnus-newsgroup-name
10114                              "." (number-to-string
10115                                   (cdr gnus-article-current)))))
10116                    dir)))
10117         (unless (file-exists-p file)
10118           (mm-save-part-to-file handle file))))))
10119
10120 ;; Summary extract commands
10121
10122 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10123   (let ((buffer-read-only nil)
10124         (article (gnus-summary-article-number))
10125         after-article b e)
10126     (unless (gnus-summary-goto-subject article)
10127       (error "No such article: %d" article))
10128     (gnus-summary-position-point)
10129     ;; If all commands are to be bunched up on one line, we collect
10130     ;; them here.
10131     (unless gnus-view-pseudos-separately
10132       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10133             files action)
10134         (while ps
10135           (setq action (cdr (assq 'action (car ps))))
10136           (setq files (list (cdr (assq 'name (car ps)))))
10137           (while (and ps (cdr ps)
10138                       (string= (or action "1")
10139                                (or (cdr (assq 'action (cadr ps))) "2")))
10140             (push (cdr (assq 'name (cadr ps))) files)
10141             (setcdr ps (cddr ps)))
10142           (when files
10143             (when (not (string-match "%s" action))
10144               (push " " files))
10145             (push " " files)
10146             (when (assq 'execute (car ps))
10147               (setcdr (assq 'execute (car ps))
10148                       (funcall (if (string-match "%s" action)
10149                                    'format 'concat)
10150                                action
10151                                (mapconcat
10152                                 (lambda (f)
10153                                   (if (equal f " ")
10154                                       f
10155                                     (gnus-quote-arg-for-sh-or-csh f)))
10156                                 files " ")))))
10157           (setq ps (cdr ps)))))
10158     (if (and gnus-view-pseudos (not not-view))
10159         (while pslist
10160           (when (assq 'execute (car pslist))
10161             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10162                                   (eq gnus-view-pseudos 'not-confirm)))
10163           (setq pslist (cdr pslist)))
10164       (save-excursion
10165         (while pslist
10166           (setq after-article (or (cdr (assq 'article (car pslist)))
10167                                   (gnus-summary-article-number)))
10168           (gnus-summary-goto-subject after-article)
10169           (forward-line 1)
10170           (setq b (point))
10171           (insert "    " (file-name-nondirectory
10172                           (cdr (assq 'name (car pslist))))
10173                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10174           (setq e (point))
10175           (forward-line -1)             ; back to `b'
10176           (gnus-add-text-properties
10177            b (1- e) (list 'gnus-number gnus-reffed-article-number
10178                           gnus-mouse-face-prop gnus-mouse-face))
10179           (gnus-data-enter
10180            after-article gnus-reffed-article-number
10181            gnus-unread-mark b (car pslist) 0 (- e b))
10182           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10183           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10184           (setq pslist (cdr pslist)))))))
10185
10186 (defun gnus-pseudos< (p1 p2)
10187   (let ((c1 (cdr (assq 'action p1)))
10188         (c2 (cdr (assq 'action p2))))
10189     (and c1 c2 (string< c1 c2))))
10190
10191 (defun gnus-request-pseudo-article (props)
10192   (cond ((assq 'execute props)
10193          (gnus-execute-command (cdr (assq 'execute props)))))
10194   (let ((gnus-current-article (gnus-summary-article-number)))
10195     (gnus-run-hooks 'gnus-mark-article-hook)))
10196
10197 (defun gnus-execute-command (command &optional automatic)
10198   (save-excursion
10199     (gnus-article-setup-buffer)
10200     (set-buffer gnus-article-buffer)
10201     (setq buffer-read-only nil)
10202     (let ((command (if automatic command
10203                      (read-string "Command: " (cons command 0)))))
10204       (erase-buffer)
10205       (insert "$ " command "\n\n")
10206       (if gnus-view-pseudo-asynchronously
10207           (start-process "gnus-execute" (current-buffer) shell-file-name
10208                          shell-command-switch command)
10209         (call-process shell-file-name nil t nil
10210                       shell-command-switch command)))))
10211
10212 ;; Summary kill commands.
10213
10214 (defun gnus-summary-edit-global-kill (article)
10215   "Edit the \"global\" kill file."
10216   (interactive (list (gnus-summary-article-number)))
10217   (gnus-group-edit-global-kill article))
10218
10219 (defun gnus-summary-edit-local-kill ()
10220   "Edit a local kill file applied to the current newsgroup."
10221   (interactive)
10222   (setq gnus-current-headers (gnus-summary-article-header))
10223   (gnus-group-edit-local-kill
10224    (gnus-summary-article-number) gnus-newsgroup-name))
10225
10226 ;;; Header reading.
10227
10228 (defun gnus-read-header (id &optional header)
10229   "Read the headers of article ID and enter them into the Gnus system."
10230   (let ((group gnus-newsgroup-name)
10231         (gnus-override-method
10232          (or
10233           gnus-override-method
10234           (and (gnus-news-group-p gnus-newsgroup-name)
10235                (car (gnus-refer-article-methods)))))
10236         where)
10237     ;; First we check to see whether the header in question is already
10238     ;; fetched.
10239     (if (stringp id)
10240         ;; This is a Message-ID.
10241         (setq header (or header (gnus-id-to-header id)))
10242       ;; This is an article number.
10243       (setq header (or header (gnus-summary-article-header id))))
10244     (if (and header
10245              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10246         ;; We have found the header.
10247         header
10248       ;; If this is a sparse article, we have to nix out its
10249       ;; previous entry in the thread hashtb.
10250       (when (and header
10251                  (gnus-summary-article-sparse-p (mail-header-number header)))
10252         (let* ((parent (gnus-parent-id (mail-header-references header)))
10253                (thread (and parent (gnus-id-to-thread parent))))
10254           (when thread
10255             (delq (assq header thread) thread))))
10256       ;; We have to really fetch the header to this article.
10257       (save-excursion
10258         (set-buffer nntp-server-buffer)
10259         (when (setq where (gnus-request-head id group))
10260           (nnheader-fold-continuation-lines)
10261           (goto-char (point-max))
10262           (insert ".\n")
10263           (goto-char (point-min))
10264           (insert "211 ")
10265           (princ (cond
10266                   ((numberp id) id)
10267                   ((cdr where) (cdr where))
10268                   (header (mail-header-number header))
10269                   (t gnus-reffed-article-number))
10270                  (current-buffer))
10271           (insert " Article retrieved.\n"))
10272         (if (or (not where)
10273                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10274             ()                          ; Malformed head.
10275           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10276             (when (and (stringp id)
10277                        (not (string= (gnus-group-real-name group)
10278                                      (car where))))
10279               ;; If we fetched by Message-ID and the article came
10280               ;; from a different group, we fudge some bogus article
10281               ;; numbers for this article.
10282               (mail-header-set-number header gnus-reffed-article-number))
10283             (save-excursion
10284               (set-buffer gnus-summary-buffer)
10285               (decf gnus-reffed-article-number)
10286               (gnus-remove-header (mail-header-number header))
10287               (push header gnus-newsgroup-headers)
10288               (setq gnus-current-headers header)
10289               (push (mail-header-number header) gnus-newsgroup-limit)))
10290           header)))))
10291
10292 (defun gnus-remove-header (number)
10293   "Remove header NUMBER from `gnus-newsgroup-headers'."
10294   (if (and gnus-newsgroup-headers
10295            (= number (mail-header-number (car gnus-newsgroup-headers))))
10296       (pop gnus-newsgroup-headers)
10297     (let ((headers gnus-newsgroup-headers))
10298       (while (and (cdr headers)
10299                   (not (= number (mail-header-number (cadr headers)))))
10300         (pop headers))
10301       (when (cdr headers)
10302         (setcdr headers (cddr headers))))))
10303
10304 ;;;
10305 ;;; summary highlights
10306 ;;;
10307
10308 (defun gnus-highlight-selected-summary ()
10309   "Highlight selected article in summary buffer."
10310   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10311   (when gnus-summary-selected-face
10312     (save-excursion
10313       (let* ((beg (progn (beginning-of-line) (point)))
10314              (end (progn (end-of-line) (point)))
10315              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10316              (from (if (get-text-property beg gnus-mouse-face-prop)
10317                        beg
10318                      (or (next-single-property-change
10319                           beg gnus-mouse-face-prop nil end)
10320                          beg)))
10321              (to
10322               (if (= from end)
10323                   (- from 2)
10324                 (or (next-single-property-change
10325                      from gnus-mouse-face-prop nil end)
10326                     end))))
10327         ;; If no mouse-face prop on line we will have to = from = end,
10328         ;; so we highlight the entire line instead.
10329         (when (= (+ to 2) from)
10330           (setq from beg)
10331           (setq to end))
10332         (if gnus-newsgroup-selected-overlay
10333             ;; Move old overlay.
10334             (gnus-move-overlay
10335              gnus-newsgroup-selected-overlay from to (current-buffer))
10336           ;; Create new overlay.
10337           (gnus-overlay-put
10338            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10339            'face gnus-summary-selected-face))))))
10340
10341 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10342 (defun gnus-summary-highlight-line ()
10343   "Highlight current line according to `gnus-summary-highlight'."
10344   (let* ((list gnus-summary-highlight)
10345          (p (point))
10346          (end (progn (end-of-line) (point)))
10347          ;; now find out where the line starts and leave point there.
10348          (beg (progn (beginning-of-line) (point)))
10349          (article (gnus-summary-article-number))
10350          (score (or (cdr (assq (or article gnus-current-article)
10351                                gnus-newsgroup-scored))
10352                     gnus-summary-default-score 0))
10353          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10354          (inhibit-read-only t))
10355     ;; Eval the cars of the lists until we find a match.
10356     (let ((default gnus-summary-default-score)
10357           (default-high gnus-summary-default-high-score)
10358           (default-low gnus-summary-default-low-score))
10359       (while (and list
10360                   (not (eval (caar list))))
10361         (setq list (cdr list))))
10362     (let ((face (cdar list)))
10363       (unless (eq face (get-text-property beg 'face))
10364         (gnus-put-text-property-excluding-characters-with-faces
10365          beg end 'face
10366          (setq face (if (boundp face) (symbol-value face) face)))
10367         (when gnus-summary-highlight-line-function
10368           (funcall gnus-summary-highlight-line-function article face))))
10369     (goto-char p)))
10370
10371 (defun gnus-update-read-articles (group unread &optional compute)
10372   "Update the list of read articles in GROUP."
10373   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10374          (entry (gnus-gethash group gnus-newsrc-hashtb))
10375          (info (nth 2 entry))
10376          (prev 1)
10377          (unread (sort (copy-sequence unread) '<))
10378          read)
10379     (if (or (not info) (not active))
10380         ;; There is no info on this group if it was, in fact,
10381         ;; killed.  Gnus stores no information on killed groups, so
10382         ;; there's nothing to be done.
10383         ;; One could store the information somewhere temporarily,
10384         ;; perhaps...  Hmmm...
10385         ()
10386       ;; Remove any negative articles numbers.
10387       (while (and unread (< (car unread) 0))
10388         (setq unread (cdr unread)))
10389       ;; Remove any expired article numbers
10390       (while (and unread (< (car unread) (car active)))
10391         (setq unread (cdr unread)))
10392       ;; Compute the ranges of read articles by looking at the list of
10393       ;; unread articles.
10394       (while unread
10395         (when (/= (car unread) prev)
10396           (push (if (= prev (1- (car unread))) prev
10397                   (cons prev (1- (car unread))))
10398                 read))
10399         (setq prev (1+ (car unread)))
10400         (setq unread (cdr unread)))
10401       (when (<= prev (cdr active))
10402         (push (cons prev (cdr active)) read))
10403       (setq read (if (> (length read) 1) (nreverse read) read))
10404       (if compute
10405           read
10406         (save-excursion
10407           (let (setmarkundo)
10408             ;; Propagate the read marks to the backend.
10409             (when (gnus-check-backend-function 'request-set-mark group)
10410               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10411                     (add (gnus-remove-from-range read (gnus-info-read info))))
10412                 (when (or add del)
10413                   (unless (gnus-check-group group)
10414                     (error "Can't open server for %s" group))
10415                   (gnus-request-set-mark
10416                    group (delq nil (list (if add (list add 'add '(read)))
10417                                          (if del (list del 'del '(read))))))
10418                   (setq setmarkundo
10419                         `(gnus-request-set-mark
10420                           ,group
10421                           ',(delq nil (list
10422                                        (if del (list del 'add '(read)))
10423                                        (if add (list add 'del '(read))))))))))
10424             (set-buffer gnus-group-buffer)
10425             (gnus-undo-register
10426               `(progn
10427                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10428                  (gnus-info-set-read ',info ',(gnus-info-read info))
10429                  (gnus-get-unread-articles-in-group ',info
10430                                                     (gnus-active ,group))
10431                  (gnus-group-update-group ,group t)
10432                  ,setmarkundo))))
10433         ;; Enter this list into the group info.
10434         (gnus-info-set-read info read)
10435         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10436         (gnus-get-unread-articles-in-group info (gnus-active group))
10437         t))))
10438
10439 (defun gnus-offer-save-summaries ()
10440   "Offer to save all active summary buffers."
10441   (let (buffers)
10442     ;; Go through all buffers and find all summaries.
10443     (dolist (buffer (buffer-list))
10444       (when (and (setq buffer (buffer-name buffer))
10445                  (string-match "Summary" buffer)
10446                  (save-excursion
10447                    (set-buffer buffer)
10448                    ;; We check that this is, indeed, a summary buffer.
10449                    (and (eq major-mode 'gnus-summary-mode)
10450                         ;; Also make sure this isn't bogus.
10451                         gnus-newsgroup-prepared
10452                         ;; Also make sure that this isn't a
10453                         ;; dead summary buffer.
10454                         (not gnus-dead-summary-mode))))
10455         (push buffer buffers)))
10456     ;; Go through all these summary buffers and offer to save them.
10457     (when buffers
10458       (save-excursion
10459         (map-y-or-n-p
10460          "Update summary buffer %s? "
10461          (lambda (buf)
10462            (switch-to-buffer buf)
10463            (gnus-summary-exit))
10464          buffers)))))
10465
10466
10467 ;;; @ for mime-partial
10468 ;;;
10469
10470 (defun gnus-request-partial-message ()
10471   (save-excursion
10472     (let ((number (gnus-summary-article-number))
10473           (group gnus-newsgroup-name)
10474           (mother gnus-article-buffer))
10475       (set-buffer (get-buffer-create " *Partial Article*"))
10476       (erase-buffer)
10477       (setq mime-preview-buffer mother)
10478       (gnus-request-article-this-buffer number group)
10479       (mime-parse-buffer)
10480       )))
10481
10482 (autoload 'mime-combine-message/partial-pieces-automatically
10483   "mime-partial"
10484   "Internal method to combine message/partial messages automatically.")
10485
10486 (mime-add-condition
10487  'action '((type . message)(subtype . partial)
10488            (major-mode . gnus-original-article-mode)
10489            (method . mime-combine-message/partial-pieces-automatically)
10490            (summary-buffer-exp . gnus-summary-buffer)
10491            (request-partial-message-method . gnus-request-partial-message)
10492            ))
10493
10494
10495 ;;; @ for message/rfc822
10496 ;;;
10497
10498 (defun gnus-mime-extract-message/rfc822 (entity situation)
10499   (let (group article num cwin swin cur)
10500     (with-temp-buffer
10501       (mime-insert-entity-content entity)
10502       (setq group (or (cdr (assq 'group situation))
10503                       (completing-read "Group: "
10504                                        gnus-active-hashtb
10505                                        nil
10506                                        (gnus-read-active-file-p)
10507                                        gnus-newsgroup-name))
10508             article (gnus-request-accept-article group)))
10509     (when (and (consp article)
10510                (numberp (setq article (cdr article))))
10511       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10512             cwin (get-buffer-window (current-buffer) t))
10513       (save-window-excursion
10514         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10515             (select-window swin)
10516           (set-buffer gnus-summary-buffer))
10517         (setq cur gnus-current-article)
10518         (forward-line num)
10519         (let (gnus-show-threads)
10520           (gnus-summary-goto-subject article t))
10521         (gnus-summary-clear-mark-forward 1)
10522         (gnus-summary-goto-subject cur))
10523       (when (and cwin (window-frame cwin))
10524         (select-frame (window-frame cwin)))
10525       (when (boundp 'mime-acting-situation-to-override)
10526         (set-alist 'mime-acting-situation-to-override
10527                    'group
10528                    group)
10529         (set-alist 'mime-acting-situation-to-override
10530                    'after-method
10531                    `(progn
10532                       (save-current-buffer
10533                         (set-buffer gnus-group-buffer)
10534                         (gnus-activate-group ,group))
10535                       (gnus-summary-goto-article ,cur
10536                                                  gnus-show-all-headers)))
10537         (set-alist 'mime-acting-situation-to-override
10538                    'number num)))))
10539
10540 (mime-add-condition
10541  'action '((type . message)(subtype . rfc822)
10542            (major-mode . gnus-original-article-mode)
10543            (method . gnus-mime-extract-message/rfc822)
10544            (mode . "extract")
10545            ))
10546
10547 (mime-add-condition
10548  'action '((type . message)(subtype . news)
10549            (major-mode . gnus-original-article-mode)
10550            (method . gnus-mime-extract-message/rfc822)
10551            (mode . "extract")
10552            ))
10553
10554 (defun gnus-mime-extract-multipart (entity situation)
10555   (let ((children (mime-entity-children entity))
10556         mime-acting-situation-to-override
10557         f)
10558     (while children
10559       (mime-play-entity (car children)
10560                         (cons (assq 'mode situation)
10561                               mime-acting-situation-to-override))
10562       (setq children (cdr children)))
10563     (if (setq f (cdr (assq 'after-method
10564                            mime-acting-situation-to-override)))
10565         (eval f)
10566       )))
10567
10568 (mime-add-condition
10569  'action '((type . multipart)
10570            (method . gnus-mime-extract-multipart)
10571            (mode . "extract")
10572            )
10573  'with-default)
10574
10575
10576 ;;; @ end
10577 ;;;
10578
10579 (defun gnus-summary-setup-default-charset ()
10580   "Setup newsgroup default charset."
10581   (if (equal gnus-newsgroup-name "nndraft:drafts")
10582       (setq gnus-newsgroup-charset nil)
10583     (let* ((ignored-charsets
10584             (or gnus-newsgroup-ephemeral-ignored-charsets
10585                 (append
10586                  (and gnus-newsgroup-name
10587                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10588                  gnus-newsgroup-ignored-charsets))))
10589       (setq gnus-newsgroup-charset
10590             (or gnus-newsgroup-ephemeral-charset
10591                 (and gnus-newsgroup-name
10592                      (gnus-parameter-charset gnus-newsgroup-name))
10593                 gnus-default-charset))
10594       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10595            ignored-charsets))))
10596
10597 ;;;
10598 ;;; Mime Commands
10599 ;;;
10600
10601 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10602   "Display the current article buffer fully MIME-buttonized.
10603 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10604 treated as multipart/mixed."
10605   (interactive "P")
10606   (require 'gnus-art)
10607   (let ((gnus-unbuttonized-mime-types nil)
10608         (gnus-mime-display-multipart-as-mixed show-all-parts))
10609     (gnus-summary-show-article)))
10610
10611 (defun gnus-summary-repair-multipart (article)
10612   "Add a Content-Type header to a multipart article without one."
10613   (interactive (list (gnus-summary-article-number)))
10614   (gnus-with-article article
10615     (message-narrow-to-head)
10616     (message-remove-header "Mime-Version")
10617     (goto-char (point-max))
10618     (insert "Mime-Version: 1.0\n")
10619     (widen)
10620     (when (search-forward "\n--" nil t)
10621       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10622         (message-narrow-to-head)
10623         (message-remove-header "Content-Type")
10624         (goto-char (point-max))
10625         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10626                         separator))
10627         (widen))))
10628   (let (gnus-mark-article-hook)
10629     (gnus-summary-select-article t t nil article)))
10630
10631 (defun gnus-summary-toggle-display-buttonized ()
10632   "Toggle the buttonizing of the article buffer."
10633   (interactive)
10634   (require 'gnus-art)
10635   (if (setq gnus-inhibit-mime-unbuttonizing
10636             (not gnus-inhibit-mime-unbuttonizing))
10637       (let ((gnus-unbuttonized-mime-types nil))
10638         (gnus-summary-show-article))
10639     (gnus-summary-show-article)))
10640
10641 ;;;
10642 ;;; Intelli-mouse commmands
10643 ;;;
10644
10645 (defun gnus-wheel-summary-scroll (event)
10646   (interactive "e")
10647   (let ((amount (if (memq 'shift (event-modifiers event))
10648                     (car gnus-wheel-scroll-amount)
10649                   (cdr gnus-wheel-scroll-amount)))
10650         (direction (- (* (static-if (featurep 'xemacs)
10651                              (event-button event)
10652                            (cond ((eq 'mouse-4 (event-basic-type event))
10653                                   4)
10654                                  ((eq 'mouse-5 (event-basic-type event))
10655                                   5)))
10656                          2) 9))
10657         edge)
10658     (gnus-summary-scroll-up (* amount direction))
10659     (when (gnus-eval-in-buffer-window gnus-article-buffer
10660             (save-restriction
10661               (widen)
10662               (and (if (< 0 direction)
10663                        (gnus-article-next-page 0)
10664                      (gnus-article-prev-page 0)
10665                      (bobp))
10666                    (if (setq edge (get-text-property
10667                                    (point-min) 'gnus-wheel-edge))
10668                        (setq edge (* edge direction))
10669                      (setq edge -1))
10670                    (or (plusp edge)
10671                        (let ((buffer-read-only nil)
10672                              (inhibit-read-only t))
10673                          (put-text-property (point-min) (point-max)
10674                                             'gnus-wheel-edge direction)
10675                          nil))
10676                    (or (> edge gnus-wheel-edge-resistance)
10677                        (let ((buffer-read-only nil)
10678                              (inhibit-read-only t))
10679                          (put-text-property (point-min) (point-max)
10680                                             'gnus-wheel-edge
10681                                             (* (1+ edge) direction))
10682                          nil))
10683                    (eq last-command 'gnus-wheel-summary-scroll))))
10684       (gnus-summary-next-article nil nil (minusp direction)))))
10685
10686 (defun gnus-wheel-install ()
10687   "Enable mouse wheel support on summary window."
10688   (when gnus-use-wheel
10689     (let ((keys
10690            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10691       (dolist (key keys)
10692         (define-key gnus-summary-mode-map key
10693           'gnus-wheel-summary-scroll)))))
10694
10695 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10696
10697 ;;;
10698 ;;; Traditional PGP commmands
10699 ;;;
10700
10701 (defun gnus-summary-decrypt-article (&optional force)
10702   "Decrypt the current article in traditional PGP way.
10703 This will have permanent effect only in mail groups.
10704 If FORCE is non-nil, allow editing of articles even in read-only
10705 groups."
10706   (interactive "P")
10707   (gnus-summary-select-article t)
10708   (gnus-eval-in-buffer-window gnus-article-buffer
10709     (save-excursion
10710       (save-restriction
10711         (widen)
10712         (goto-char (point-min))
10713         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10714           (error "Not a traditional PGP message!"))
10715         (let ((armor-start (match-beginning 0)))
10716           (if (and (pgg-decrypt-region armor-start (point-max))
10717                    (or force (not (gnus-group-read-only-p))))
10718               (let ((inhibit-read-only t)
10719                     buffer-read-only)
10720                 (delete-region armor-start
10721                                (progn
10722                                  (re-search-forward "^-+END PGP" nil t)
10723                                  (beginning-of-line 2)
10724                                  (point)))
10725                 (insert-buffer-substring pgg-output-buffer))))))))
10726
10727 (defun gnus-summary-verify-article ()
10728   "Verify the current article in traditional PGP way."
10729   (interactive)
10730   (save-excursion
10731     (set-buffer gnus-original-article-buffer)
10732     (goto-char (point-min))
10733     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10734       (error "Not a traditional PGP message!"))
10735     (re-search-forward "^-+END PGP" nil t)
10736     (beginning-of-line 2)
10737     (call-interactively (function pgg-verify-region))))
10738
10739 ;;;
10740 ;;; Generic summary marking commands
10741 ;;;
10742
10743 (defvar gnus-summary-marking-alist
10744   '((read gnus-del-mark "d")
10745     (unread gnus-unread-mark "u")
10746     (ticked gnus-ticked-mark "!")
10747     (dormant gnus-dormant-mark "?")
10748     (expirable gnus-expirable-mark "e"))
10749   "An alist of names/marks/keystrokes.")
10750
10751 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10752 (defvar gnus-summary-mark-map)
10753
10754 (defun gnus-summary-make-all-marking-commands ()
10755   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10756   (dolist (elem gnus-summary-marking-alist)
10757     (apply 'gnus-summary-make-marking-command elem)))
10758
10759 (defun gnus-summary-make-marking-command (name mark keystroke)
10760   (let ((map (make-sparse-keymap)))
10761     (define-key gnus-summary-generic-mark-map keystroke map)
10762     (dolist (lway `((next "next" next nil "n")
10763                     (next-unread "next unread" next t "N")
10764                     (prev "previous" prev nil "p")
10765                     (prev-unread "previous unread" prev t "P")
10766                     (nomove "" nil nil ,keystroke)))
10767       (let ((func (gnus-summary-make-marking-command-1
10768                    mark (car lway) lway name)))
10769         (setq func (eval func))
10770         (define-key map (nth 4 lway) func)))))
10771
10772 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10773   `(defun ,(intern
10774             (format "gnus-summary-put-mark-as-%s%s"
10775                     name (if (eq way 'nomove)
10776                              ""
10777                            (concat "-" (symbol-name way)))))
10778      (n)
10779      ,(format
10780        "Mark the current article as %s%s.
10781 If N, the prefix, then repeat N times.
10782 If N is negative, move in reverse order.
10783 The difference between N and the actual number of articles marked is
10784 returned."
10785        name (car (cdr lway)))
10786      (interactive "p")
10787      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10788
10789 (defun gnus-summary-generic-mark (n mark move unread)
10790   "Mark N articles with MARK."
10791   (unless (eq major-mode 'gnus-summary-mode)
10792     (error "This command can only be used in the summary buffer"))
10793   (gnus-summary-show-thread)
10794   (let ((nummove
10795          (cond
10796           ((eq move 'next) 1)
10797           ((eq move 'prev) -1)
10798           (t 0))))
10799     (if (zerop nummove)
10800         (setq n 1)
10801       (when (< n 0)
10802         (setq n (abs n)
10803               nummove (* -1 nummove))))
10804     (while (and (> n 0)
10805                 (gnus-summary-mark-article nil mark)
10806                 (zerop (gnus-summary-next-subject nummove unread t)))
10807       (setq n (1- n)))
10808     (when (/= 0 n)
10809       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10810     (gnus-summary-recenter)
10811     (gnus-summary-position-point)
10812     (gnus-set-mode-line 'summary)
10813     n))
10814
10815 (defun gnus-summary-insert-articles (articles)
10816   (when (setq articles
10817               (gnus-set-difference articles
10818                                    (mapcar (lambda (h) (mail-header-number h))
10819                                            gnus-newsgroup-headers)))
10820     (setq gnus-newsgroup-headers
10821           (merge 'list
10822                  gnus-newsgroup-headers
10823                  (gnus-fetch-headers articles)
10824                  'gnus-article-sort-by-number))
10825     ;; Suppress duplicates?
10826     (when gnus-suppress-duplicates
10827       (gnus-dup-suppress-articles))
10828
10829     ;; We might want to build some more threads first.
10830     (when (and gnus-fetch-old-headers
10831                (eq gnus-headers-retrieved-by 'nov))
10832       (if (eq gnus-fetch-old-headers 'invisible)
10833           (gnus-build-all-threads)
10834         (gnus-build-old-threads)))
10835     ;; Let the Gnus agent mark articles as read.
10836     (when gnus-agent
10837       (gnus-agent-get-undownloaded-list))
10838     ;; Remove list identifiers from subject
10839     (when gnus-list-identifiers
10840       (gnus-summary-remove-list-identifiers))
10841     ;; First and last article in this newsgroup.
10842     (when gnus-newsgroup-headers
10843       (setq gnus-newsgroup-begin
10844             (mail-header-number (car gnus-newsgroup-headers))
10845             gnus-newsgroup-end
10846             (mail-header-number
10847              (gnus-last-element gnus-newsgroup-headers))))
10848     (when gnus-use-scoring
10849       (gnus-possibly-score-headers))))
10850
10851 (defun gnus-summary-insert-old-articles (&optional all)
10852   "Insert all old articles in this group.
10853 If ALL is non-nil, already read articles become readable.
10854 If ALL is a number, fetch this number of articles."
10855   (interactive "P")
10856   (prog1
10857       (let ((old (mapcar 'car gnus-newsgroup-data))
10858             (i (car gnus-newsgroup-active))
10859             older len)
10860         (while (<= i (cdr gnus-newsgroup-active))
10861           (or (memq i old) (push i older))
10862           (incf i))
10863         (setq len (length older))
10864         (cond
10865          ((null older) nil)
10866          ((numberp all)
10867           (if (< all len)
10868               (setq older (subseq older 0 all))))
10869          (all nil)
10870          (t
10871           (if (and (numberp gnus-large-newsgroup)
10872                    (> len gnus-large-newsgroup))
10873               (let ((input
10874                      (read-string
10875                       (format
10876                        "How many articles from %s (default %d): "
10877                        (gnus-limit-string
10878                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10879                        len))))
10880                 (unless (string-match "^[ \t]*$" input)
10881                   (setq all (string-to-number input))
10882                   (if (< all len)
10883                       (setq older (subseq older 0 all))))))))
10884         (if (not older)
10885             (message "No old news.")
10886           (gnus-summary-insert-articles older)
10887           (gnus-summary-limit (gnus-union older old))))
10888     (gnus-summary-position-point)))
10889
10890 (defun gnus-summary-insert-new-articles ()
10891   "Insert all new articles in this group."
10892   (interactive)
10893   (prog1
10894       (let ((old (mapcar 'car gnus-newsgroup-data))
10895             (old-active gnus-newsgroup-active)
10896             (nnmail-fetched-sources (list t))
10897             i new)
10898         (setq gnus-newsgroup-active
10899               (gnus-activate-group gnus-newsgroup-name 'scan))
10900         (setq i (1+ (cdr old-active)))
10901         (while (<= i (cdr gnus-newsgroup-active))
10902           (push i new)
10903           (incf i))
10904         (if (not new)
10905             (message "No gnus is bad news.")
10906           (setq new (nreverse new))
10907           (gnus-summary-insert-articles new)
10908           (setq gnus-newsgroup-unreads
10909                 (append gnus-newsgroup-unreads new))
10910           (gnus-summary-limit (gnus-union old new))))
10911     (gnus-summary-position-point)))
10912
10913 (gnus-summary-make-all-marking-commands)
10914
10915 (gnus-ems-redefine)
10916
10917 (provide 'gnus-sum)
10918
10919 (run-hooks 'gnus-sum-load-hook)
10920
10921 ;;; gnus-sum.el ends here