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     "E" gnus-summary-limit-include-expunged
1623     "c" gnus-summary-limit-exclude-childless-dormant
1624     "C" gnus-summary-limit-mark-excluded-as-read
1625     "o" gnus-summary-insert-old-articles
1626     "N" gnus-summary-insert-new-articles)
1627
1628   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1629     "n" gnus-summary-next-unread-article
1630     "p" gnus-summary-prev-unread-article
1631     "N" gnus-summary-next-article
1632     "P" gnus-summary-prev-article
1633     "\C-n" gnus-summary-next-same-subject
1634     "\C-p" gnus-summary-prev-same-subject
1635     "\M-n" gnus-summary-next-unread-subject
1636     "\M-p" gnus-summary-prev-unread-subject
1637     "f" gnus-summary-first-unread-article
1638     "b" gnus-summary-best-unread-article
1639     "j" gnus-summary-goto-article
1640     "g" gnus-summary-goto-subject
1641     "l" gnus-summary-goto-last-article
1642     "o" gnus-summary-pop-article)
1643
1644   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1645     "k" gnus-summary-kill-thread
1646     "l" gnus-summary-lower-thread
1647     "i" gnus-summary-raise-thread
1648     "T" gnus-summary-toggle-threads
1649     "t" gnus-summary-rethread-current
1650     "^" gnus-summary-reparent-thread
1651     "s" gnus-summary-show-thread
1652     "S" gnus-summary-show-all-threads
1653     "h" gnus-summary-hide-thread
1654     "H" gnus-summary-hide-all-threads
1655     "n" gnus-summary-next-thread
1656     "p" gnus-summary-prev-thread
1657     "u" gnus-summary-up-thread
1658     "o" gnus-summary-top-thread
1659     "d" gnus-summary-down-thread
1660     "#" gnus-uu-mark-thread
1661     "\M-#" gnus-uu-unmark-thread)
1662
1663   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1664     "g" gnus-summary-prepare
1665     "c" gnus-summary-insert-cached-articles)
1666
1667   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1668     "c" gnus-summary-catchup-and-exit
1669     "C" gnus-summary-catchup-all-and-exit
1670     "E" gnus-summary-exit-no-update
1671     "J" gnus-summary-jump-to-other-group
1672     "Q" gnus-summary-exit
1673     "Z" gnus-summary-exit
1674     "n" gnus-summary-catchup-and-goto-next-group
1675     "R" gnus-summary-reselect-current-group
1676     "G" gnus-summary-rescan-group
1677     "N" gnus-summary-next-group
1678     "s" gnus-summary-save-newsrc
1679     "P" gnus-summary-prev-group)
1680
1681   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1682     " " gnus-summary-next-page
1683     "n" gnus-summary-next-page
1684     "\177" gnus-summary-prev-page
1685     [delete] gnus-summary-prev-page
1686     "p" gnus-summary-prev-page
1687     "\r" gnus-summary-scroll-up
1688     "\M-\r" gnus-summary-scroll-down
1689     "<" gnus-summary-beginning-of-article
1690     ">" gnus-summary-end-of-article
1691     "b" gnus-summary-beginning-of-article
1692     "e" gnus-summary-end-of-article
1693     "^" gnus-summary-refer-parent-article
1694     "r" gnus-summary-refer-parent-article
1695     "D" gnus-summary-enter-digest-group
1696     "R" gnus-summary-refer-references
1697     "T" gnus-summary-refer-thread
1698     "g" gnus-summary-show-article
1699     "s" gnus-summary-isearch-article
1700     "P" gnus-summary-print-article
1701     "M" gnus-mailing-list-insinuate
1702     "t" gnus-article-babel)
1703
1704   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1705     "b" gnus-article-add-buttons
1706     "B" gnus-article-add-buttons-to-head
1707     "o" gnus-article-treat-overstrike
1708     "e" gnus-article-emphasize
1709     "w" gnus-article-fill-cited-article
1710     "Q" gnus-article-fill-long-lines
1711     "C" gnus-article-capitalize-sentences
1712     "c" gnus-article-remove-cr
1713     "Z" gnus-article-decode-HZ
1714     "f" gnus-article-display-x-face
1715     "l" gnus-summary-stop-page-breaking
1716     "r" gnus-summary-caesar-message
1717     "t" gnus-article-toggle-headers
1718     "v" gnus-summary-verbose-headers
1719     "m" gnus-summary-toggle-mime
1720     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1721     "p" gnus-article-verify-x-pgp-sig
1722     "d" gnus-article-treat-dumbquotes
1723     "s" gnus-smiley-display)
1724
1725   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1726     "a" gnus-article-hide
1727     "h" gnus-article-toggle-headers
1728     "b" gnus-article-hide-boring-headers
1729     "s" gnus-article-hide-signature
1730     "c" gnus-article-hide-citation
1731     "C" gnus-article-hide-citation-in-followups
1732     "l" gnus-article-hide-list-identifiers
1733     "p" gnus-article-hide-pgp
1734     "B" gnus-article-strip-banner
1735     "P" gnus-article-hide-pem
1736     "\C-c" gnus-article-hide-citation-maybe)
1737
1738   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1739     "a" gnus-article-highlight
1740     "h" gnus-article-highlight-headers
1741     "c" gnus-article-highlight-citation
1742     "s" gnus-article-highlight-signature)
1743
1744   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1745     "z" gnus-article-date-ut
1746     "u" gnus-article-date-ut
1747     "l" gnus-article-date-local
1748     "p" gnus-article-date-english
1749     "e" gnus-article-date-lapsed
1750     "o" gnus-article-date-original
1751     "i" gnus-article-date-iso8601
1752     "s" gnus-article-date-user)
1753
1754   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1755     "t" gnus-article-remove-trailing-blank-lines
1756     "l" gnus-article-strip-leading-blank-lines
1757     "m" gnus-article-strip-multiple-blank-lines
1758     "a" gnus-article-strip-blank-lines
1759     "A" gnus-article-strip-all-blank-lines
1760     "s" gnus-article-strip-leading-space
1761     "e" gnus-article-strip-trailing-space
1762     "w" gnus-article-remove-leading-whitespace)
1763
1764   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1765     "v" gnus-version
1766     "f" gnus-summary-fetch-faq
1767     "d" gnus-summary-describe-group
1768     "h" gnus-summary-describe-briefly
1769     "i" gnus-info-find-node)
1770
1771   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1772     "e" gnus-summary-expire-articles
1773     "\M-\C-e" gnus-summary-expire-articles-now
1774     "\177" gnus-summary-delete-article
1775     [delete] gnus-summary-delete-article
1776     [backspace] gnus-summary-delete-article
1777     "m" gnus-summary-move-article
1778     "r" gnus-summary-respool-article
1779     "w" gnus-summary-edit-article
1780     "c" gnus-summary-copy-article
1781     "B" gnus-summary-crosspost-article
1782     "q" gnus-summary-respool-query
1783     "t" gnus-summary-respool-trace
1784     "i" gnus-summary-import-article
1785     "I" gnus-summary-create-article
1786     "p" gnus-summary-article-posted-p)
1787
1788   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1789     "o" gnus-summary-save-article
1790     "m" gnus-summary-save-article-mail
1791     "F" gnus-summary-write-article-file
1792     "r" gnus-summary-save-article-rmail
1793     "f" gnus-summary-save-article-file
1794     "b" gnus-summary-save-article-body-file
1795     "h" gnus-summary-save-article-folder
1796     "v" gnus-summary-save-article-vm
1797     "p" gnus-summary-pipe-output
1798     "s" gnus-soup-add-article)
1799
1800   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1801     "b" gnus-summary-display-buttonized
1802     "m" gnus-summary-repair-multipart
1803     "v" gnus-article-view-part
1804     "o" gnus-article-save-part
1805     "c" gnus-article-copy-part
1806     "C" gnus-article-view-part-as-charset
1807     "e" gnus-article-externalize-part
1808     "E" gnus-article-encrypt-body
1809     "i" gnus-article-inline-part
1810     "|" gnus-article-pipe-part))
1811
1812 (defvar gnus-article-post-menu nil)
1813
1814 (defun gnus-summary-make-menu-bar ()
1815   (gnus-turn-off-edit-menu 'summary)
1816
1817   (unless (boundp 'gnus-summary-misc-menu)
1818
1819     (easy-menu-define
1820      gnus-summary-kill-menu gnus-summary-mode-map ""
1821      (cons
1822       "Score"
1823       (nconc
1824        (list
1825         ["Customize" gnus-score-customize t])
1826        (gnus-make-score-map 'increase)
1827        (gnus-make-score-map 'lower)
1828        '(("Mark"
1829           ["Kill below" gnus-summary-kill-below t]
1830           ["Mark above" gnus-summary-mark-above t]
1831           ["Tick above" gnus-summary-tick-above t]
1832           ["Clear above" gnus-summary-clear-above t])
1833          ["Current score" gnus-summary-current-score t]
1834          ["Set score" gnus-summary-set-score t]
1835          ["Switch current score file..." gnus-score-change-score-file t]
1836          ["Set mark below..." gnus-score-set-mark-below t]
1837          ["Set expunge below..." gnus-score-set-expunge-below t]
1838          ["Edit current score file" gnus-score-edit-current-scores t]
1839          ["Edit score file" gnus-score-edit-file t]
1840          ["Trace score" gnus-score-find-trace t]
1841          ["Find words" gnus-score-find-favourite-words t]
1842          ["Rescore buffer" gnus-summary-rescore t]
1843          ["Increase score..." gnus-summary-increase-score t]
1844          ["Lower score..." gnus-summary-lower-score t]))))
1845
1846     ;; Define both the Article menu in the summary buffer and the equivalent
1847     ;; Commands menu in the article buffer here for consistency.
1848     (let ((innards
1849            `(("Hide"
1850               ["All" gnus-article-hide t]
1851               ["Headers" gnus-article-toggle-headers t]
1852               ["Signature" gnus-article-hide-signature t]
1853               ["Citation" gnus-article-hide-citation t]
1854               ["List identifiers" gnus-article-hide-list-identifiers t]
1855               ["PGP" gnus-article-hide-pgp t]
1856               ["Banner" gnus-article-strip-banner t]
1857               ["Boring headers" gnus-article-hide-boring-headers t])
1858              ("Highlight"
1859               ["All" gnus-article-highlight t]
1860               ["Headers" gnus-article-highlight-headers t]
1861               ["Signature" gnus-article-highlight-signature t]
1862               ["Citation" gnus-article-highlight-citation t])
1863              ("Date"
1864               ["Local" gnus-article-date-local t]
1865               ["ISO8601" gnus-article-date-iso8601 t]
1866               ["UT" gnus-article-date-ut t]
1867               ["Original" gnus-article-date-original t]
1868               ["Lapsed" gnus-article-date-lapsed t]
1869               ["User-defined" gnus-article-date-user t])
1870              ("Washing"
1871               ("Remove Blanks"
1872                ["Leading" gnus-article-strip-leading-blank-lines t]
1873                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1874                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1875                ["All of the above" gnus-article-strip-blank-lines t]
1876                ["All" gnus-article-strip-all-blank-lines t]
1877                ["Leading space" gnus-article-strip-leading-space t]
1878                ["Trailing space" gnus-article-strip-trailing-space t]
1879                ["Leading space in headers"
1880                 gnus-article-remove-leading-whitespace t])
1881               ["Overstrike" gnus-article-treat-overstrike t]
1882               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1883               ["Emphasis" gnus-article-emphasize t]
1884               ["Word wrap" gnus-article-fill-cited-article t]
1885               ["Fill long lines" gnus-article-fill-long-lines t]
1886               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1887               ["CR" gnus-article-remove-cr t]
1888               ["Show X-Face" gnus-article-display-x-face t]
1889               ["Rot 13" gnus-summary-caesar-message
1890                ,@(if (featurep 'xemacs) '(t)
1891                    '(:help "\"Caesar rotate\" article by 13"))]
1892               ["Unix pipe" gnus-summary-pipe-message t]
1893               ["Add buttons" gnus-article-add-buttons t]
1894               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1895               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1896               ["Toggle MIME" gnus-summary-toggle-mime t]
1897               ["Verbose header" gnus-summary-verbose-headers t]
1898               ["Toggle header" gnus-summary-toggle-header t]
1899               ["Toggle smileys" gnus-smiley-display t]
1900               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1901               ["HZ" gnus-article-decode-HZ t])
1902              ("Output"
1903               ["Save in default format" gnus-summary-save-article
1904                ,@(if (featurep 'xemacs) '(t)
1905                    '(:help "Save article using default method"))]
1906               ["Save in file" gnus-summary-save-article-file
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Save article in file"))]
1909               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1910               ["Save in MH folder" gnus-summary-save-article-folder t]
1911               ["Save in VM folder" gnus-summary-save-article-vm t]
1912               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1913               ["Save body in file" gnus-summary-save-article-body-file t]
1914               ["Pipe through a filter" gnus-summary-pipe-output t]
1915               ["Add to SOUP packet" gnus-soup-add-article t]
1916               ["Print" gnus-summary-print-article t])
1917              ("Backend"
1918               ["Respool article..." gnus-summary-respool-article t]
1919               ["Move article..." gnus-summary-move-article
1920                (gnus-check-backend-function
1921                 'request-move-article gnus-newsgroup-name)]
1922               ["Copy article..." gnus-summary-copy-article t]
1923               ["Crosspost article..." gnus-summary-crosspost-article
1924                (gnus-check-backend-function
1925                 'request-replace-article gnus-newsgroup-name)]
1926               ["Import file..." gnus-summary-import-article t]
1927               ["Create article..." gnus-summary-create-article t]
1928               ["Check if posted" gnus-summary-article-posted-p t]
1929               ["Edit article" gnus-summary-edit-article
1930                (not (gnus-group-read-only-p))]
1931               ["Delete article" gnus-summary-delete-article
1932                (gnus-check-backend-function
1933                 'request-expire-articles gnus-newsgroup-name)]
1934               ["Query respool" gnus-summary-respool-query t]
1935               ["Trace respool" gnus-summary-respool-trace t]
1936               ["Delete expirable articles" gnus-summary-expire-articles-now
1937                (gnus-check-backend-function
1938                 'request-expire-articles gnus-newsgroup-name)])
1939              ("Extract"
1940               ["Uudecode" gnus-uu-decode-uu
1941                ,@(if (featurep 'xemacs) '(t)
1942                    '(:help "Decode uuencoded article(s)"))]
1943               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1944               ["Unshar" gnus-uu-decode-unshar t]
1945               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1946               ["Save" gnus-uu-decode-save t]
1947               ["Binhex" gnus-uu-decode-binhex t]
1948               ["Postscript" gnus-uu-decode-postscript t])
1949              ("Cache"
1950               ["Enter article" gnus-cache-enter-article t]
1951               ["Remove article" gnus-cache-remove-article t])
1952              ["Translate" gnus-article-babel t]
1953              ["Select article buffer" gnus-summary-select-article-buffer t]
1954              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1955              ["Isearch article..." gnus-summary-isearch-article t]
1956              ["Beginning of the article" gnus-summary-beginning-of-article t]
1957              ["End of the article" gnus-summary-end-of-article t]
1958              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1959              ["Fetch referenced articles" gnus-summary-refer-references t]
1960              ["Fetch current thread" gnus-summary-refer-thread t]
1961              ["Fetch article with id..." gnus-summary-refer-article t]
1962              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1963              ["Redisplay" gnus-summary-show-article t]
1964              ["Raw article" gnus-summary-show-raw-article t])))
1965       (easy-menu-define
1966        gnus-summary-article-menu gnus-summary-mode-map ""
1967        (cons "Article" innards))
1968
1969       (if (not (keymapp gnus-summary-article-menu))
1970           (easy-menu-define
1971            gnus-article-commands-menu gnus-article-mode-map ""
1972            (cons "Commands" innards))
1973         ;; in Emacs, don't share menu.
1974         (setq gnus-article-commands-menu
1975               (copy-keymap gnus-summary-article-menu))
1976         (define-key gnus-article-mode-map [menu-bar commands]
1977           (cons "Commands" gnus-article-commands-menu))))
1978
1979     (easy-menu-define
1980      gnus-summary-thread-menu gnus-summary-mode-map ""
1981      '("Threads"
1982        ["Toggle threading" gnus-summary-toggle-threads t]
1983        ["Hide threads" gnus-summary-hide-all-threads t]
1984        ["Show threads" gnus-summary-show-all-threads t]
1985        ["Hide thread" gnus-summary-hide-thread t]
1986        ["Show thread" gnus-summary-show-thread t]
1987        ["Go to next thread" gnus-summary-next-thread t]
1988        ["Go to previous thread" gnus-summary-prev-thread t]
1989        ["Go down thread" gnus-summary-down-thread t]
1990        ["Go up thread" gnus-summary-up-thread t]
1991        ["Top of thread" gnus-summary-top-thread t]
1992        ["Mark thread as read" gnus-summary-kill-thread t]
1993        ["Lower thread score" gnus-summary-lower-thread t]
1994        ["Raise thread score" gnus-summary-raise-thread t]
1995        ["Rethread current" gnus-summary-rethread-current t]))
1996
1997     (easy-menu-define
1998      gnus-summary-post-menu gnus-summary-mode-map ""
1999      `("Post"
2000        ["Post an article" gnus-summary-post-news
2001         ,@(if (featurep 'xemacs) '(t)
2002             '(:help "Post an article"))]
2003        ["Followup" gnus-summary-followup
2004         ,@(if (featurep 'xemacs) '(t)
2005             '(:help "Post followup to this article"))]
2006        ["Followup and yank" gnus-summary-followup-with-original
2007         ,@(if (featurep 'xemacs) '(t)
2008             '(:help "Post followup to this article, quoting its contents"))]
2009        ["Supersede article" gnus-summary-supersede-article t]
2010        ["Cancel article" gnus-summary-cancel-article
2011         ,@(if (featurep 'xemacs) '(t)
2012             '(:help "Cancel an article you posted"))]
2013        ["Reply" gnus-summary-reply t]
2014        ["Reply and yank" gnus-summary-reply-with-original t]
2015        ["Wide reply" gnus-summary-wide-reply t]
2016        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2017         ,@(if (featurep 'xemacs) '(t)
2018             '(:help "Mail a reply, quoting this article"))]
2019        ["Mail forward" gnus-summary-mail-forward t]
2020        ["Post forward" gnus-summary-post-forward t]
2021        ["Digest and mail" gnus-summary-digest-mail-forward t]
2022        ["Digest and post" gnus-summary-digest-post-forward t]
2023        ["Resend message" gnus-summary-resend-message t]
2024        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2025        ["Send a mail" gnus-summary-mail-other-window t]
2026        ["Uuencode and post" gnus-uu-post-news
2027         ,@(if (featurep 'xemacs) '(t)
2028             '(:help "Post a uuencoded article"))]
2029        ["Followup via news" gnus-summary-followup-to-mail t]
2030        ["Followup via news and yank"
2031         gnus-summary-followup-to-mail-with-original t]
2032        ;;("Draft"
2033        ;;["Send" gnus-summary-send-draft t]
2034        ;;["Send bounced" gnus-resend-bounced-mail t])
2035        ))
2036
2037     (cond
2038      ((not (keymapp gnus-summary-post-menu))
2039       (setq gnus-article-post-menu gnus-summary-post-menu))
2040      ((not gnus-article-post-menu)
2041       ;; Don't share post menu.
2042       (setq gnus-article-post-menu
2043             (copy-keymap gnus-summary-post-menu))))
2044     (define-key gnus-article-mode-map [menu-bar post]
2045       (cons "Post" gnus-article-post-menu))
2046
2047     (easy-menu-define
2048      gnus-summary-misc-menu gnus-summary-mode-map ""
2049      `("Gnus"
2050        ("Mark Read"
2051         ["Mark as read" gnus-summary-mark-as-read-forward t]
2052         ["Mark same subject and select"
2053          gnus-summary-kill-same-subject-and-select t]
2054         ["Mark same subject" gnus-summary-kill-same-subject t]
2055         ["Catchup" gnus-summary-catchup
2056          ,@(if (featurep 'xemacs) '(t)
2057              '(:help "Mark unread articles in this group as read"))]
2058         ["Catchup all" gnus-summary-catchup-all t]
2059         ["Catchup to here" gnus-summary-catchup-to-here t]
2060         ["Catchup from here" gnus-summary-catchup-from-here t]
2061         ["Catchup region" gnus-summary-mark-region-as-read t]
2062         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2063        ("Mark Various"
2064         ["Tick" gnus-summary-tick-article-forward t]
2065         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2066         ["Remove marks" gnus-summary-clear-mark-forward t]
2067         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2068         ["Set bookmark" gnus-summary-set-bookmark t]
2069         ["Remove bookmark" gnus-summary-remove-bookmark t])
2070        ("Mark Limit"
2071         ["Marks..." gnus-summary-limit-to-marks t]
2072         ["Subject..." gnus-summary-limit-to-subject t]
2073         ["Author..." gnus-summary-limit-to-author t]
2074         ["Age..." gnus-summary-limit-to-age t]
2075         ["Extra..." gnus-summary-limit-to-extra t]
2076         ["Score" gnus-summary-limit-to-score t]
2077         ["Unread" gnus-summary-limit-to-unread t]
2078         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2079         ["Articles" gnus-summary-limit-to-articles t]
2080         ["Pop limit" gnus-summary-pop-limit t]
2081         ["Show dormant" gnus-summary-limit-include-dormant t]
2082         ["Hide childless dormant"
2083          gnus-summary-limit-exclude-childless-dormant t]
2084         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2085         ["Hide marked" gnus-summary-limit-exclude-marks t]
2086         ["Show expunged" gnus-summary-limit-include-expunged t])
2087        ("Process Mark"
2088         ["Set mark" gnus-summary-mark-as-processable t]
2089         ["Remove mark" gnus-summary-unmark-as-processable t]
2090         ["Remove all marks" gnus-summary-unmark-all-processable t]
2091         ["Mark above" gnus-uu-mark-over t]
2092         ["Mark series" gnus-uu-mark-series t]
2093         ["Mark region" gnus-uu-mark-region t]
2094         ["Unmark region" gnus-uu-unmark-region t]
2095         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2096         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2097         ["Mark all" gnus-uu-mark-all t]
2098         ["Mark buffer" gnus-uu-mark-buffer t]
2099         ["Mark sparse" gnus-uu-mark-sparse t]
2100         ["Mark thread" gnus-uu-mark-thread t]
2101         ["Unmark thread" gnus-uu-unmark-thread t]
2102         ("Process Mark Sets"
2103          ["Kill" gnus-summary-kill-process-mark t]
2104          ["Yank" gnus-summary-yank-process-mark
2105           gnus-newsgroup-process-stack]
2106          ["Save" gnus-summary-save-process-mark t]))
2107        ("Scroll article"
2108         ["Page forward" gnus-summary-next-page
2109          ,@(if (featurep 'xemacs) '(t)
2110              '(:help "Show next page of article"))]
2111         ["Page backward" gnus-summary-prev-page
2112          ,@(if (featurep 'xemacs) '(t)
2113              '(:help "Show previous page of article"))]
2114         ["Line forward" gnus-summary-scroll-up t])
2115        ("Move"
2116         ["Next unread article" gnus-summary-next-unread-article t]
2117         ["Previous unread article" gnus-summary-prev-unread-article t]
2118         ["Next article" gnus-summary-next-article t]
2119         ["Previous article" gnus-summary-prev-article t]
2120         ["Next unread subject" gnus-summary-next-unread-subject t]
2121         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2122         ["Next article same subject" gnus-summary-next-same-subject t]
2123         ["Previous article same subject" gnus-summary-prev-same-subject t]
2124         ["First unread article" gnus-summary-first-unread-article t]
2125         ["Best unread article" gnus-summary-best-unread-article t]
2126         ["Go to subject number..." gnus-summary-goto-subject t]
2127         ["Go to article number..." gnus-summary-goto-article t]
2128         ["Go to the last article" gnus-summary-goto-last-article t]
2129         ["Pop article off history" gnus-summary-pop-article t])
2130        ("Sort"
2131         ["Sort by number" gnus-summary-sort-by-number t]
2132         ["Sort by author" gnus-summary-sort-by-author t]
2133         ["Sort by subject" gnus-summary-sort-by-subject t]
2134         ["Sort by date" gnus-summary-sort-by-date t]
2135         ["Sort by score" gnus-summary-sort-by-score t]
2136         ["Sort by lines" gnus-summary-sort-by-lines t]
2137         ["Sort by characters" gnus-summary-sort-by-chars t]
2138         ["Original sort" gnus-summary-sort-by-original t])
2139        ("Help"
2140         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2141         ["Describe group" gnus-summary-describe-group t]
2142         ["Read manual" gnus-info-find-node t])
2143        ("Modes"
2144         ["Pick and read" gnus-pick-mode t]
2145         ["Binary" gnus-binary-mode t])
2146        ("Regeneration"
2147         ["Regenerate" gnus-summary-prepare t]
2148         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2149         ["Toggle threading" gnus-summary-toggle-threads t])
2150        ["See old articles" gnus-summary-insert-old-articles t]
2151        ["See new articles" gnus-summary-insert-new-articles t]
2152        ["Filter articles..." gnus-summary-execute-command t]
2153        ["Run command on subjects..." gnus-summary-universal-argument t]
2154        ["Search articles forward..." gnus-summary-search-article-forward t]
2155        ["Search articles backward..." gnus-summary-search-article-backward t]
2156        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2157        ["Expand window" gnus-summary-expand-window t]
2158        ["Expire expirable articles" gnus-summary-expire-articles
2159         (gnus-check-backend-function
2160          'request-expire-articles gnus-newsgroup-name)]
2161        ["Edit local kill file" gnus-summary-edit-local-kill t]
2162        ["Edit main kill file" gnus-summary-edit-global-kill t]
2163        ["Edit group parameters" gnus-summary-edit-parameters t]
2164        ["Customize group parameters" gnus-summary-customize-parameters t]
2165        ["Send a bug report" gnus-bug t]
2166        ("Exit"
2167         ["Catchup and exit" gnus-summary-catchup-and-exit
2168          ,@(if (featurep 'xemacs) '(t)
2169              '(:help "Mark unread articles in this group as read, then exit"))]
2170         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2171         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2172         ["Exit group" gnus-summary-exit
2173          ,@(if (featurep 'xemacs) '(t)
2174              '(:help "Exit current group, return to group selection mode"))]
2175         ["Exit group without updating" gnus-summary-exit-no-update t]
2176         ["Exit and goto next group" gnus-summary-next-group t]
2177         ["Exit and goto prev group" gnus-summary-prev-group t]
2178         ["Reselect group" gnus-summary-reselect-current-group t]
2179         ["Rescan group" gnus-summary-rescan-group t]
2180         ["Update dribble" gnus-summary-save-newsrc t])))
2181
2182     (gnus-run-hooks 'gnus-summary-menu-hook)))
2183
2184 (defvar gnus-summary-tool-bar-map nil)
2185
2186 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2187 (defun gnus-summary-make-tool-bar ()
2188   (if (and (fboundp 'tool-bar-add-item-from-menu)
2189            (default-value 'tool-bar-mode)
2190            (not gnus-summary-tool-bar-map))
2191       (setq gnus-summary-tool-bar-map
2192             (let ((tool-bar-map (make-sparse-keymap))
2193                   (load-path (mm-image-load-path)))
2194               (tool-bar-add-item-from-menu
2195                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2196               (tool-bar-add-item-from-menu
2197                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-post-news "post" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-followup "followup" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-reply "reply" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2224               tool-bar-map)))
2225   (if gnus-summary-tool-bar-map
2226       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2227
2228 (defun gnus-score-set-default (var value)
2229   "A version of set that updates the GNU Emacs menu-bar."
2230   (set var value)
2231   ;; It is the message that forces the active status to be updated.
2232   (message ""))
2233
2234 (defun gnus-make-score-map (type)
2235   "Make a summary score map of type TYPE."
2236   (if t
2237       nil
2238     (let ((headers '(("author" "from" string)
2239                      ("subject" "subject" string)
2240                      ("article body" "body" string)
2241                      ("article head" "head" string)
2242                      ("xref" "xref" string)
2243                      ("extra header" "extra" string)
2244                      ("lines" "lines" number)
2245                      ("followups to author" "followup" string)))
2246           (types '((number ("less than" <)
2247                            ("greater than" >)
2248                            ("equal" =))
2249                    (string ("substring" s)
2250                            ("exact string" e)
2251                            ("fuzzy string" f)
2252                            ("regexp" r))))
2253           (perms '(("temporary" (current-time-string))
2254                    ("permanent" nil)
2255                    ("immediate" now)))
2256           header)
2257       (list
2258        (apply
2259         'nconc
2260         (list
2261          (if (eq type 'lower)
2262              "Lower score"
2263            "Increase score"))
2264         (let (outh)
2265           (while headers
2266             (setq header (car headers))
2267             (setq outh
2268                   (cons
2269                    (apply
2270                     'nconc
2271                     (list (car header))
2272                     (let ((ts (cdr (assoc (nth 2 header) types)))
2273                           outt)
2274                       (while ts
2275                         (setq outt
2276                               (cons
2277                                (apply
2278                                 'nconc
2279                                 (list (caar ts))
2280                                 (let ((ps perms)
2281                                       outp)
2282                                   (while ps
2283                                     (setq outp
2284                                           (cons
2285                                            (vector
2286                                             (caar ps)
2287                                             (list
2288                                              'gnus-summary-score-entry
2289                                              (nth 1 header)
2290                                              (if (or (string= (nth 1 header)
2291                                                               "head")
2292                                                      (string= (nth 1 header)
2293                                                               "body"))
2294                                                  ""
2295                                                (list 'gnus-summary-header
2296                                                      (nth 1 header)))
2297                                              (list 'quote (nth 1 (car ts)))
2298                                              (list 'gnus-score-delta-default
2299                                                    nil)
2300                                              (nth 1 (car ps))
2301                                              t)
2302                                             t)
2303                                            outp))
2304                                     (setq ps (cdr ps)))
2305                                   (list (nreverse outp))))
2306                                outt))
2307                         (setq ts (cdr ts)))
2308                       (list (nreverse outt))))
2309                    outh))
2310             (setq headers (cdr headers)))
2311           (list (nreverse outh))))))))
2312
2313 \f
2314
2315 (defun gnus-summary-mode (&optional group)
2316   "Major mode for reading articles.
2317
2318 All normal editing commands are switched off.
2319 \\<gnus-summary-mode-map>
2320 Each line in this buffer represents one article.  To read an
2321 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2322 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2323 respectively.
2324
2325 You can also post articles and send mail from this buffer.  To
2326 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2327 of an article, type `\\[gnus-summary-reply]'.
2328
2329 There are approx. one gazillion commands you can execute in this
2330 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2331
2332 The following commands are available:
2333
2334 \\{gnus-summary-mode-map}"
2335   (interactive)
2336   (kill-all-local-variables)
2337   (when (gnus-visual-p 'summary-menu 'menu)
2338     (gnus-summary-make-menu-bar)
2339     (gnus-summary-make-tool-bar))
2340   (gnus-summary-make-local-variables)
2341   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2342     (gnus-summary-make-local-variables))
2343   (gnus-make-thread-indent-array)
2344   (gnus-simplify-mode-line)
2345   (setq major-mode 'gnus-summary-mode)
2346   (setq mode-name "Summary")
2347   (make-local-variable 'minor-mode-alist)
2348   (use-local-map gnus-summary-mode-map)
2349   (buffer-disable-undo)
2350   (setq buffer-read-only t)             ;Disable modification
2351   (setq truncate-lines t)
2352   (setq selective-display t)
2353   (setq selective-display-ellipses t)   ;Display `...'
2354   (gnus-summary-set-display-table)
2355   (gnus-set-default-directory)
2356   (setq gnus-newsgroup-name group)
2357   (unless (gnus-news-group-p group)
2358     (setq gnus-newsgroup-incorporated
2359           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2360   (make-local-variable 'gnus-summary-line-format)
2361   (make-local-variable 'gnus-summary-line-format-spec)
2362   (make-local-variable 'gnus-summary-dummy-line-format)
2363   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2364   (make-local-variable 'gnus-summary-mark-positions)
2365   (make-local-hook 'pre-command-hook)
2366   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2367   (gnus-run-hooks 'gnus-summary-mode-hook)
2368   (turn-on-gnus-mailing-list-mode)
2369   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2370   (gnus-update-summary-mark-positions))
2371
2372 (defun gnus-summary-make-local-variables ()
2373   "Make all the local summary buffer variables."
2374   (let (global)
2375     (dolist (local gnus-summary-local-variables)
2376       (if (consp local)
2377           (progn
2378             (if (eq (cdr local) 'global)
2379                 ;; Copy the global value of the variable.
2380                 (setq global (symbol-value (car local)))
2381               ;; Use the value from the list.
2382               (setq global (eval (cdr local))))
2383             (set (make-local-variable (car local)) global))
2384         ;; Simple nil-valued local variable.
2385         (set (make-local-variable local) nil)))))
2386
2387 (defun gnus-summary-clear-local-variables ()
2388   (let ((locals gnus-summary-local-variables))
2389     (while locals
2390       (if (consp (car locals))
2391           (and (vectorp (caar locals))
2392                (set (caar locals) nil))
2393         (and (vectorp (car locals))
2394              (set (car locals) nil)))
2395       (setq locals (cdr locals)))))
2396
2397 ;; Summary data functions.
2398
2399 (defmacro gnus-data-number (data)
2400   `(car ,data))
2401
2402 (defmacro gnus-data-set-number (data number)
2403   `(setcar ,data ,number))
2404
2405 (defmacro gnus-data-mark (data)
2406   `(nth 1 ,data))
2407
2408 (defmacro gnus-data-set-mark (data mark)
2409   `(setcar (nthcdr 1 ,data) ,mark))
2410
2411 (defmacro gnus-data-pos (data)
2412   `(nth 2 ,data))
2413
2414 (defmacro gnus-data-set-pos (data pos)
2415   `(setcar (nthcdr 2 ,data) ,pos))
2416
2417 (defmacro gnus-data-header (data)
2418   `(nth 3 ,data))
2419
2420 (defmacro gnus-data-set-header (data header)
2421   `(setcar (nthcdr 3 ,data) ,header))
2422
2423 (defmacro gnus-data-level (data)
2424   `(nth 4 ,data))
2425
2426 (defmacro gnus-data-unread-p (data)
2427   `(= (nth 1 ,data) gnus-unread-mark))
2428
2429 (defmacro gnus-data-read-p (data)
2430   `(/= (nth 1 ,data) gnus-unread-mark))
2431
2432 (defmacro gnus-data-pseudo-p (data)
2433   `(consp (nth 3 ,data)))
2434
2435 (defmacro gnus-data-find (number)
2436   `(assq ,number gnus-newsgroup-data))
2437
2438 (defmacro gnus-data-find-list (number &optional data)
2439   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2440      (memq (assq ,number bdata)
2441            bdata)))
2442
2443 (defmacro gnus-data-make (number mark pos header level)
2444   `(list ,number ,mark ,pos ,header ,level))
2445
2446 (defun gnus-data-enter (after-article number mark pos header level offset)
2447   (let ((data (gnus-data-find-list after-article)))
2448     (unless data
2449       (error "No such article: %d" after-article))
2450     (setcdr data (cons (gnus-data-make number mark pos header level)
2451                        (cdr data)))
2452     (setq gnus-newsgroup-data-reverse nil)
2453     (gnus-data-update-list (cddr data) offset)))
2454
2455 (defun gnus-data-enter-list (after-article list &optional offset)
2456   (when list
2457     (let ((data (and after-article (gnus-data-find-list after-article)))
2458           (ilist list))
2459       (if (not (or data
2460                    after-article))
2461           (let ((odata gnus-newsgroup-data))
2462             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2463             (when offset
2464               (gnus-data-update-list odata offset)))
2465         ;; Find the last element in the list to be spliced into the main
2466         ;; list.
2467         (while (cdr list)
2468           (setq list (cdr list)))
2469         (if (not data)
2470             (progn
2471               (setcdr list gnus-newsgroup-data)
2472               (setq gnus-newsgroup-data ilist)
2473               (when offset
2474                 (gnus-data-update-list (cdr list) offset)))
2475           (setcdr list (cdr data))
2476           (setcdr data ilist)
2477           (when offset
2478             (gnus-data-update-list (cdr list) offset))))
2479       (setq gnus-newsgroup-data-reverse nil))))
2480
2481 (defun gnus-data-remove (article &optional offset)
2482   (let ((data gnus-newsgroup-data))
2483     (if (= (gnus-data-number (car data)) article)
2484         (progn
2485           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2486                 gnus-newsgroup-data-reverse nil)
2487           (when offset
2488             (gnus-data-update-list gnus-newsgroup-data offset)))
2489       (while (cdr data)
2490         (when (= (gnus-data-number (cadr data)) article)
2491           (setcdr data (cddr data))
2492           (when offset
2493             (gnus-data-update-list (cdr data) offset))
2494           (setq data nil
2495                 gnus-newsgroup-data-reverse nil))
2496         (setq data (cdr data))))))
2497
2498 (defmacro gnus-data-list (backward)
2499   `(if ,backward
2500        (or gnus-newsgroup-data-reverse
2501            (setq gnus-newsgroup-data-reverse
2502                  (reverse gnus-newsgroup-data)))
2503      gnus-newsgroup-data))
2504
2505 (defun gnus-data-update-list (data offset)
2506   "Add OFFSET to the POS of all data entries in DATA."
2507   (setq gnus-newsgroup-data-reverse nil)
2508   (while data
2509     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2510     (setq data (cdr data))))
2511
2512 (defun gnus-summary-article-pseudo-p (article)
2513   "Say whether this article is a pseudo article or not."
2514   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2515
2516 (defmacro gnus-summary-article-sparse-p (article)
2517   "Say whether this article is a sparse article or not."
2518   `(memq ,article gnus-newsgroup-sparse))
2519
2520 (defmacro gnus-summary-article-ancient-p (article)
2521   "Say whether this article is a sparse article or not."
2522   `(memq ,article gnus-newsgroup-ancient))
2523
2524 (defun gnus-article-parent-p (number)
2525   "Say whether this article is a parent or not."
2526   (let ((data (gnus-data-find-list number)))
2527     (and (cdr data)                     ; There has to be an article after...
2528          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2529             (gnus-data-level (nth 1 data))))))
2530
2531 (defun gnus-article-children (number)
2532   "Return a list of all children to NUMBER."
2533   (let* ((data (gnus-data-find-list number))
2534          (level (gnus-data-level (car data)))
2535          children)
2536     (setq data (cdr data))
2537     (while (and data
2538                 (= (gnus-data-level (car data)) (1+ level)))
2539       (push (gnus-data-number (car data)) children)
2540       (setq data (cdr data)))
2541     children))
2542
2543 (defmacro gnus-summary-skip-intangible ()
2544   "If the current article is intangible, then jump to a different article."
2545   '(let ((to (get-text-property (point) 'gnus-intangible)))
2546      (and to (gnus-summary-goto-subject to))))
2547
2548 (defmacro gnus-summary-article-intangible-p ()
2549   "Say whether this article is intangible or not."
2550   '(get-text-property (point) 'gnus-intangible))
2551
2552 (defun gnus-article-read-p (article)
2553   "Say whether ARTICLE is read or not."
2554   (not (or (memq article gnus-newsgroup-marked)
2555            (memq article gnus-newsgroup-unreads)
2556            (memq article gnus-newsgroup-unselected)
2557            (memq article gnus-newsgroup-dormant))))
2558
2559 ;; Some summary mode macros.
2560
2561 (defmacro gnus-summary-article-number ()
2562   "The article number of the article on the current line.
2563 If there isn's an article number here, then we return the current
2564 article number."
2565   '(progn
2566      (gnus-summary-skip-intangible)
2567      (or (get-text-property (point) 'gnus-number)
2568          (gnus-summary-last-subject))))
2569
2570 (defmacro gnus-summary-article-header (&optional number)
2571   "Return the header of article NUMBER."
2572   `(gnus-data-header (gnus-data-find
2573                       ,(or number '(gnus-summary-article-number)))))
2574
2575 (defmacro gnus-summary-thread-level (&optional number)
2576   "Return the level of thread that starts with article NUMBER."
2577   `(if (and (eq gnus-summary-make-false-root 'dummy)
2578             (get-text-property (point) 'gnus-intangible))
2579        0
2580      (gnus-data-level (gnus-data-find
2581                        ,(or number '(gnus-summary-article-number))))))
2582
2583 (defmacro gnus-summary-article-mark (&optional number)
2584   "Return the mark of article NUMBER."
2585   `(gnus-data-mark (gnus-data-find
2586                     ,(or number '(gnus-summary-article-number)))))
2587
2588 (defmacro gnus-summary-article-pos (&optional number)
2589   "Return the position of the line of article NUMBER."
2590   `(gnus-data-pos (gnus-data-find
2591                    ,(or number '(gnus-summary-article-number)))))
2592
2593 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2594 (defmacro gnus-summary-article-subject (&optional number)
2595   "Return current subject string or nil if nothing."
2596   `(let ((headers
2597           ,(if number
2598                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2599              '(gnus-data-header (assq (gnus-summary-article-number)
2600                                       gnus-newsgroup-data)))))
2601      (and headers
2602           (vectorp headers)
2603           (mail-header-subject headers))))
2604
2605 (defmacro gnus-summary-article-score (&optional number)
2606   "Return current article score."
2607   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2608                   gnus-newsgroup-scored))
2609        gnus-summary-default-score 0))
2610
2611 (defun gnus-summary-article-children (&optional number)
2612   "Return a list of article numbers that are children of article NUMBER."
2613   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2614          (level (gnus-data-level (car data)))
2615          l children)
2616     (while (and (setq data (cdr data))
2617                 (> (setq l (gnus-data-level (car data))) level))
2618       (and (= (1+ level) l)
2619            (push (gnus-data-number (car data))
2620                  children)))
2621     (nreverse children)))
2622
2623 (defun gnus-summary-article-parent (&optional number)
2624   "Return the article number of the parent of article NUMBER."
2625   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2626                                     (gnus-data-list t)))
2627          (level (gnus-data-level (car data))))
2628     (if (zerop level)
2629         ()                              ; This is a root.
2630       ;; We search until we find an article with a level less than
2631       ;; this one.  That function has to be the parent.
2632       (while (and (setq data (cdr data))
2633                   (not (< (gnus-data-level (car data)) level))))
2634       (and data (gnus-data-number (car data))))))
2635
2636 (defun gnus-unread-mark-p (mark)
2637   "Say whether MARK is the unread mark."
2638   (= mark gnus-unread-mark))
2639
2640 (defun gnus-read-mark-p (mark)
2641   "Say whether MARK is one of the marks that mark as read.
2642 This is all marks except unread, ticked, dormant, and expirable."
2643   (not (or (= mark gnus-unread-mark)
2644            (= mark gnus-ticked-mark)
2645            (= mark gnus-dormant-mark)
2646            (= mark gnus-expirable-mark))))
2647
2648 (defmacro gnus-article-mark (number)
2649   "Return the MARK of article NUMBER.
2650 This macro should only be used when computing the mark the \"first\"
2651 time; i.e., when generating the summary lines.  After that,
2652 `gnus-summary-article-mark' should be used to examine the
2653 marks of articles."
2654   `(cond
2655     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2656     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2657     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2658     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2659     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2660     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2661     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2662     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2663            gnus-ancient-mark))))
2664
2665 ;; Saving hidden threads.
2666
2667 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2668 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2669
2670 (defmacro gnus-save-hidden-threads (&rest forms)
2671   "Save hidden threads, eval FORMS, and restore the hidden threads."
2672   (let ((config (make-symbol "config")))
2673     `(let ((,config (gnus-hidden-threads-configuration)))
2674        (unwind-protect
2675            (save-excursion
2676              ,@forms)
2677          (gnus-restore-hidden-threads-configuration ,config)))))
2678
2679 (defun gnus-data-compute-positions ()
2680   "Compute the positions of all articles."
2681   (setq gnus-newsgroup-data-reverse nil)
2682   (let ((data gnus-newsgroup-data))
2683     (save-excursion
2684       (gnus-save-hidden-threads
2685         (gnus-summary-show-all-threads)
2686         (goto-char (point-min))
2687         (while data
2688           (while (get-text-property (point) 'gnus-intangible)
2689             (forward-line 1))
2690           (gnus-data-set-pos (car data) (+ (point) 3))
2691           (setq data (cdr data))
2692           (forward-line 1))))))
2693
2694 (defun gnus-hidden-threads-configuration ()
2695   "Return the current hidden threads configuration."
2696   (save-excursion
2697     (let (config)
2698       (goto-char (point-min))
2699       (while (search-forward "\r" nil t)
2700         (push (1- (point)) config))
2701       config)))
2702
2703 (defun gnus-restore-hidden-threads-configuration (config)
2704   "Restore hidden threads configuration from CONFIG."
2705   (save-excursion
2706     (let (point buffer-read-only)
2707       (while (setq point (pop config))
2708         (when (and (< point (point-max))
2709                    (goto-char point)
2710                    (eq (char-after) ?\n))
2711           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2712
2713 ;; Various summary mode internalish functions.
2714
2715 (defun gnus-mouse-pick-article (e)
2716   (interactive "e")
2717   (mouse-set-point e)
2718   (gnus-summary-next-page nil t))
2719
2720 (defun gnus-summary-set-display-table ()
2721   "Change the display table.
2722 Odd characters have a tendency to mess
2723 up nicely formatted displays - we make all possible glyphs
2724 display only a single character."
2725
2726   ;; We start from the standard display table, if any.
2727   (let ((table (or (copy-sequence standard-display-table)
2728                    (make-display-table)))
2729         (i 32))
2730     ;; Nix out all the control chars...
2731     (while (>= (setq i (1- i)) 0)
2732       (aset table i [??]))
2733     ;; ... but not newline and cr, of course.  (cr is necessary for the
2734     ;; selective display).
2735     (aset table ?\n nil)
2736     (aset table ?\r nil)
2737     ;; We keep TAB as well.
2738     (aset table ?\t nil)
2739     ;; We nix out any glyphs over 126 that are not set already.
2740     (let ((i 256))
2741       (while (>= (setq i (1- i)) 127)
2742         ;; Only modify if the entry is nil.
2743         (unless (aref table i)
2744           (aset table i [??]))))
2745     (setq buffer-display-table table)))
2746
2747 (defun gnus-summary-set-article-display-arrow (pos)
2748   "Update the overlay arrow to point to line at position POS."
2749   (when (and gnus-summary-display-arrow
2750              (boundp 'overlay-arrow-position)
2751              (boundp 'overlay-arrow-string))
2752     (save-excursion
2753       (goto-char pos)
2754       (beginning-of-line)
2755       (unless overlay-arrow-position
2756         (setq overlay-arrow-position (make-marker)))
2757       (setq overlay-arrow-string "=>"
2758             overlay-arrow-position (set-marker overlay-arrow-position
2759                                                (point)
2760                                                (current-buffer))))))
2761
2762 (defun gnus-summary-buffer-name (group)
2763   "Return the summary buffer name of GROUP."
2764   (concat "*Summary " group "*"))
2765
2766 (defun gnus-summary-setup-buffer (group)
2767   "Initialize summary buffer."
2768   (let ((buffer (gnus-summary-buffer-name group)))
2769     (if (get-buffer buffer)
2770         (progn
2771           (set-buffer buffer)
2772           (setq gnus-summary-buffer (current-buffer))
2773           (not gnus-newsgroup-prepared))
2774       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2775       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2776       (gnus-summary-mode group)
2777       (when gnus-carpal
2778         (gnus-carpal-setup-buffer 'summary))
2779       (unless gnus-single-article-buffer
2780         (make-local-variable 'gnus-article-buffer)
2781         (make-local-variable 'gnus-article-current)
2782         (make-local-variable 'gnus-original-article-buffer))
2783       (setq gnus-newsgroup-name group)
2784       ;; Set any local variables in the group parameters.
2785       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2786       t)))
2787
2788 (defun gnus-set-global-variables ()
2789   "Set the global equivalents of the buffer-local variables.
2790 They are set to the latest values they had.  These reflect the summary
2791 buffer that was in action when the last article was fetched."
2792   (when (eq major-mode 'gnus-summary-mode)
2793     (setq gnus-summary-buffer (current-buffer))
2794     (let ((name gnus-newsgroup-name)
2795           (marked gnus-newsgroup-marked)
2796           (unread gnus-newsgroup-unreads)
2797           (headers gnus-current-headers)
2798           (data gnus-newsgroup-data)
2799           (summary gnus-summary-buffer)
2800           (article-buffer gnus-article-buffer)
2801           (original gnus-original-article-buffer)
2802           (gac gnus-article-current)
2803           (reffed gnus-reffed-article-number)
2804           (score-file gnus-current-score-file)
2805           (default-charset gnus-newsgroup-charset)
2806           vlist)
2807       (let ((locals gnus-newsgroup-variables))
2808         (while locals
2809           (if (consp (car locals))
2810               (push (eval (caar locals)) vlist)
2811             (push (eval (car locals)) vlist))
2812           (setq locals (cdr locals)))
2813         (setq vlist (nreverse vlist)))
2814       (save-excursion
2815         (set-buffer gnus-group-buffer)
2816         (setq gnus-newsgroup-name name
2817               gnus-newsgroup-marked marked
2818               gnus-newsgroup-unreads unread
2819               gnus-current-headers headers
2820               gnus-newsgroup-data data
2821               gnus-article-current gac
2822               gnus-summary-buffer summary
2823               gnus-article-buffer article-buffer
2824               gnus-original-article-buffer original
2825               gnus-reffed-article-number reffed
2826               gnus-current-score-file score-file
2827               gnus-newsgroup-charset default-charset)
2828         (let ((locals gnus-newsgroup-variables))
2829           (while locals
2830             (if (consp (car locals))
2831                 (set (caar locals) (pop vlist))
2832               (set (car locals) (pop vlist)))
2833             (setq locals (cdr locals))))
2834         ;; The article buffer also has local variables.
2835         (when (gnus-buffer-live-p gnus-article-buffer)
2836           (set-buffer gnus-article-buffer)
2837           (setq gnus-summary-buffer summary))))))
2838
2839 (defun gnus-summary-article-unread-p (article)
2840   "Say whether ARTICLE is unread or not."
2841   (memq article gnus-newsgroup-unreads))
2842
2843 (defun gnus-summary-first-article-p (&optional article)
2844   "Return whether ARTICLE is the first article in the buffer."
2845   (if (not (setq article (or article (gnus-summary-article-number))))
2846       nil
2847     (eq article (caar gnus-newsgroup-data))))
2848
2849 (defun gnus-summary-last-article-p (&optional article)
2850   "Return whether ARTICLE is the last article in the buffer."
2851   (if (not (setq article (or article (gnus-summary-article-number))))
2852       ;; All non-existent numbers are the last article.  :-)
2853       t
2854     (not (cdr (gnus-data-find-list article)))))
2855
2856 (defun gnus-make-thread-indent-array ()
2857   (let ((n 200))
2858     (unless (and gnus-thread-indent-array
2859                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2860       (setq gnus-thread-indent-array (make-vector 201 "")
2861             gnus-thread-indent-array-level gnus-thread-indent-level)
2862       (while (>= n 0)
2863         (aset gnus-thread-indent-array n
2864               (make-string (* n gnus-thread-indent-level) ? ))
2865         (setq n (1- n))))))
2866
2867 (defun gnus-update-summary-mark-positions ()
2868   "Compute where the summary marks are to go."
2869   (save-excursion
2870     (when (gnus-buffer-exists-p gnus-summary-buffer)
2871       (set-buffer gnus-summary-buffer))
2872     (let ((gnus-replied-mark 129)
2873           (gnus-score-below-mark 130)
2874           (gnus-score-over-mark 130)
2875           (gnus-download-mark 131)
2876           (spec gnus-summary-line-format-spec)
2877           gnus-visual pos)
2878       (save-excursion
2879         (gnus-set-work-buffer)
2880         (let ((gnus-summary-line-format-spec spec)
2881               (gnus-newsgroup-downloadable '((0 . t))))
2882           (gnus-summary-insert-line
2883            (make-full-mail-header 0 "" "nobody"
2884                                   "05 Apr 2001 23:33:09 +0400"
2885                                   "" "" 0 0 "" nil)
2886            0 nil 128 t nil "" nil 1)
2887           (goto-char (point-min))
2888           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2889                                              (- (point) 2)))))
2890           (goto-char (point-min))
2891           (push (cons 'replied (and (search-forward "\201" nil t)
2892                                     (- (point) 2)))
2893                 pos)
2894           (goto-char (point-min))
2895           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2896                 pos)
2897           (goto-char (point-min))
2898           (push (cons 'download
2899                       (and (search-forward "\203" nil t) (- (point) 2)))
2900                 pos)))
2901       (setq gnus-summary-mark-positions pos))))
2902
2903 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2904   "Insert a dummy root in the summary buffer."
2905   (beginning-of-line)
2906   (gnus-add-text-properties
2907    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2908    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2909
2910 (defun gnus-summary-from-or-to-or-newsgroups (header)
2911   (let ((to (cdr (assq 'To (mail-header-extra header))))
2912         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2913         (default-mime-charset (with-current-buffer gnus-summary-buffer
2914                                 default-mime-charset)))
2915     (cond
2916      ((and to
2917            gnus-ignored-from-addresses
2918            (string-match gnus-ignored-from-addresses
2919                          (mail-header-from header)))
2920       (concat "-> "
2921               (or (car (funcall gnus-extract-address-components
2922                                 (funcall
2923                                  gnus-decode-encoded-word-function to)))
2924                   (funcall gnus-decode-encoded-word-function to))))
2925      ((and newsgroups
2926            gnus-ignored-from-addresses
2927            (string-match gnus-ignored-from-addresses
2928                          (mail-header-from header)))
2929       (concat "=> " newsgroups))
2930      (t
2931       (or (car (funcall gnus-extract-address-components
2932                         (mail-header-from header)))
2933           (mail-header-from header))))))
2934
2935 (defun gnus-summary-insert-line (gnus-tmp-header
2936                                  gnus-tmp-level gnus-tmp-current
2937                                  gnus-tmp-unread gnus-tmp-replied
2938                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2939                                  &optional gnus-tmp-dummy gnus-tmp-score
2940                                  gnus-tmp-process)
2941   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2942          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2943          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2944          (gnus-tmp-score-char
2945           (if (or (null gnus-summary-default-score)
2946                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2947                       gnus-summary-zcore-fuzz))
2948               ?\ ;;;Whitespace
2949             (if (< gnus-tmp-score gnus-summary-default-score)
2950                 gnus-score-below-mark gnus-score-over-mark)))
2951          (gnus-tmp-replied
2952           (cond (gnus-tmp-process gnus-process-mark)
2953                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2954                  gnus-cached-mark)
2955                 (gnus-tmp-replied gnus-replied-mark)
2956                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2957                  gnus-saved-mark)
2958                 (t gnus-no-mark)))
2959          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2960          (gnus-tmp-name
2961           (cond
2962            ((string-match "<[^>]+> *$" gnus-tmp-from)
2963             (let ((beg (match-beginning 0)))
2964               (or (and (string-match "^\".+\"" gnus-tmp-from)
2965                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2966                   (substring gnus-tmp-from 0 beg))))
2967            ((string-match "(.+)" gnus-tmp-from)
2968             (substring gnus-tmp-from
2969                        (1+ (match-beginning 0)) (1- (match-end 0))))
2970            (t gnus-tmp-from)))
2971          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2972          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2973          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2974          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2975          (buffer-read-only nil))
2976     (when (string= gnus-tmp-name "")
2977       (setq gnus-tmp-name gnus-tmp-from))
2978     (unless (numberp gnus-tmp-lines)
2979       (setq gnus-tmp-lines -1))
2980     (when (= gnus-tmp-lines -1)
2981       (setq gnus-tmp-lines "?"))
2982     (gnus-put-text-property-excluding-characters-with-faces
2983      (point)
2984      (progn (eval gnus-summary-line-format-spec) (point))
2985      'gnus-number gnus-tmp-number)
2986     (when (gnus-visual-p 'summary-highlight 'highlight)
2987       (forward-line -1)
2988       (gnus-run-hooks 'gnus-summary-update-hook)
2989       (forward-line 1))))
2990
2991 (defun gnus-summary-update-line (&optional dont-update)
2992   "Update summary line after change."
2993   (when (and gnus-summary-default-score
2994              (not gnus-summary-inhibit-highlight))
2995     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2996            (article (gnus-summary-article-number))
2997            (score (gnus-summary-article-score article)))
2998       (unless dont-update
2999         (if (and gnus-summary-mark-below
3000                  (< (gnus-summary-article-score)
3001                     gnus-summary-mark-below))
3002             ;; This article has a low score, so we mark it as read.
3003             (when (memq article gnus-newsgroup-unreads)
3004               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3005           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3006             ;; This article was previously marked as read on account
3007             ;; of a low score, but now it has risen, so we mark it as
3008             ;; unread.
3009             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3010         (gnus-summary-update-mark
3011          (if (or (null gnus-summary-default-score)
3012                  (<= (abs (- score gnus-summary-default-score))
3013                      gnus-summary-zcore-fuzz))
3014              ?\ ;;;Whitespace
3015            (if (< score gnus-summary-default-score)
3016                gnus-score-below-mark gnus-score-over-mark))
3017          'score))
3018       ;; Do visual highlighting.
3019       (when (gnus-visual-p 'summary-highlight 'highlight)
3020         (gnus-run-hooks 'gnus-summary-update-hook)))))
3021
3022 (defvar gnus-tmp-new-adopts nil)
3023
3024 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3025   "Return the number of articles in THREAD.
3026 This may be 0 in some cases -- if none of the articles in
3027 the thread are to be displayed."
3028   (let* ((number
3029           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3030           (cond
3031            ((not (listp thread))
3032             1)
3033            ((and (consp thread) (cdr thread))
3034             (apply
3035              '+ 1 (mapcar
3036                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3037            ((null thread)
3038             1)
3039            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3040             1)
3041            (t 0))))
3042     (when (and level (zerop level) gnus-tmp-new-adopts)
3043       (incf number
3044             (apply '+ (mapcar
3045                        'gnus-summary-number-of-articles-in-thread
3046                        gnus-tmp-new-adopts))))
3047     (if char
3048         (if (> number 1) gnus-not-empty-thread-mark
3049           gnus-empty-thread-mark)
3050       number)))
3051
3052 (defun gnus-summary-set-local-parameters (group)
3053   "Go through the local params of GROUP and set all variable specs in that list."
3054   (let ((params (gnus-group-find-parameter group))
3055         (vars '(quit-config))           ; Ignore quit-config.
3056         elem)
3057     (while params
3058       (setq elem (car params)
3059             params (cdr params))
3060       (and (consp elem)                 ; Has to be a cons.
3061            (consp (cdr elem))           ; The cdr has to be a list.
3062            (symbolp (car elem))         ; Has to be a symbol in there.
3063            (not (memq (car elem) vars))
3064            (ignore-errors               ; So we set it.
3065              (push (car elem) vars)
3066              (make-local-variable (car elem))
3067              (set (car elem) (eval (nth 1 elem))))))))
3068
3069 (defun gnus-summary-read-group (group &optional show-all no-article
3070                                       kill-buffer no-display backward
3071                                       select-articles)
3072   "Start reading news in newsgroup GROUP.
3073 If SHOW-ALL is non-nil, already read articles are also listed.
3074 If NO-ARTICLE is non-nil, no article is selected initially.
3075 If NO-DISPLAY, don't generate a summary buffer."
3076   (let (result)
3077     (while (and group
3078                 (null (setq result
3079                             (let ((gnus-auto-select-next nil))
3080                               (or (gnus-summary-read-group-1
3081                                    group show-all no-article
3082                                    kill-buffer no-display
3083                                    select-articles)
3084                                   (setq show-all nil
3085                                         select-articles nil)))))
3086                 (eq gnus-auto-select-next 'quietly))
3087       (set-buffer gnus-group-buffer)
3088       ;; The entry function called above goes to the next
3089       ;; group automatically, so we go two groups back
3090       ;; if we are searching for the previous group.
3091       (when backward
3092         (gnus-group-prev-unread-group 2))
3093       (if (not (equal group (gnus-group-group-name)))
3094           (setq group (gnus-group-group-name))
3095         (setq group nil)))
3096     result))
3097
3098 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3099   "Directly jump to the other GROUP from summary buffer.
3100 If SHOW-ALL is non-nil, already read articles are also listed."
3101   (interactive
3102    (if (eq gnus-summary-buffer (current-buffer))
3103        (list (completing-read
3104               "Group: " gnus-active-hashtb nil t
3105               (when (and gnus-newsgroup-name
3106                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3107                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3108               'gnus-group-history)
3109              current-prefix-arg)
3110      (error "%s must be invoked from a gnus summary buffer." this-command)))
3111   (unless (or (zerop (length group))
3112               (and gnus-newsgroup-name
3113                    (string-equal gnus-newsgroup-name group)))
3114     (gnus-summary-exit)
3115     (gnus-summary-read-group group show-all
3116                              gnus-dont-select-after-jump-to-other-group)))
3117
3118 (defun gnus-summary-read-group-1 (group show-all no-article
3119                                         kill-buffer no-display
3120                                         &optional select-articles)
3121   ;; Killed foreign groups can't be entered.
3122   ;;  (when (and (not (gnus-group-native-p group))
3123   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3124   ;;    (error "Dead non-native groups can't be entered"))
3125   (gnus-message 5 "Retrieving newsgroup: %s..."
3126                 (gnus-group-decoded-name group))
3127   (let* ((new-group (gnus-summary-setup-buffer group))
3128          (quit-config (gnus-group-quit-config group))
3129          (did-select (and new-group (gnus-select-newsgroup
3130                                      group show-all select-articles))))
3131     (cond
3132      ;; This summary buffer exists already, so we just select it.
3133      ((not new-group)
3134       (gnus-set-global-variables)
3135       (when kill-buffer
3136         (gnus-kill-or-deaden-summary kill-buffer))
3137       (gnus-configure-windows 'summary 'force)
3138       (gnus-set-mode-line 'summary)
3139       (gnus-summary-position-point)
3140       (message "")
3141       t)
3142      ;; We couldn't select this group.
3143      ((null did-select)
3144       (when (and (eq major-mode 'gnus-summary-mode)
3145                  (not (equal (current-buffer) kill-buffer)))
3146         (kill-buffer (current-buffer))
3147         (if (not quit-config)
3148             (progn
3149               ;; Update the info -- marks might need to be removed,
3150               ;; for instance.
3151               (gnus-summary-update-info)
3152               (set-buffer gnus-group-buffer)
3153               (gnus-group-jump-to-group group)
3154               (gnus-group-next-unread-group 1))
3155           (gnus-handle-ephemeral-exit quit-config)))
3156       (let ((grpinfo (gnus-get-info group)))
3157         (if (null (gnus-info-read grpinfo))
3158             (gnus-message 3 "Group %s contains no messages"
3159                           (gnus-group-decoded-name group))
3160           (gnus-message 3 "Can't select group")))
3161       nil)
3162      ;; The user did a `C-g' while prompting for number of articles,
3163      ;; so we exit this group.
3164      ((eq did-select 'quit)
3165       (and (eq major-mode 'gnus-summary-mode)
3166            (not (equal (current-buffer) kill-buffer))
3167            (kill-buffer (current-buffer)))
3168       (when kill-buffer
3169         (gnus-kill-or-deaden-summary kill-buffer))
3170       (if (not quit-config)
3171           (progn
3172             (set-buffer gnus-group-buffer)
3173             (gnus-group-jump-to-group group)
3174             (gnus-group-next-unread-group 1)
3175             (gnus-configure-windows 'group 'force))
3176         (gnus-handle-ephemeral-exit quit-config))
3177       ;; Finally signal the quit.
3178       (signal 'quit nil))
3179      ;; The group was successfully selected.
3180      (t
3181       (gnus-set-global-variables)
3182       ;; Save the active value in effect when the group was entered.
3183       (setq gnus-newsgroup-active
3184             (gnus-copy-sequence
3185              (gnus-active gnus-newsgroup-name)))
3186       ;; You can change the summary buffer in some way with this hook.
3187       (gnus-run-hooks 'gnus-select-group-hook)
3188       (gnus-update-format-specifications
3189        nil 'summary 'summary-mode 'summary-dummy)
3190       (gnus-update-summary-mark-positions)
3191       ;; Do score processing.
3192       (when gnus-use-scoring
3193         (gnus-possibly-score-headers))
3194       ;; Check whether to fill in the gaps in the threads.
3195       (when gnus-build-sparse-threads
3196         (gnus-build-sparse-threads))
3197       ;; Find the initial limit.
3198       (if gnus-show-threads
3199           (if show-all
3200               (let ((gnus-newsgroup-dormant nil))
3201                 (gnus-summary-initial-limit show-all))
3202             (gnus-summary-initial-limit show-all))
3203         ;; When untreaded, all articles are always shown.
3204         (setq gnus-newsgroup-limit
3205               (mapcar
3206                (lambda (header) (mail-header-number header))
3207                gnus-newsgroup-headers)))
3208       ;; Generate the summary buffer.
3209       (unless no-display
3210         (gnus-summary-prepare))
3211       (when gnus-use-trees
3212         (gnus-tree-open group)
3213         (setq gnus-summary-highlight-line-function
3214               'gnus-tree-highlight-article))
3215       ;; If the summary buffer is empty, but there are some low-scored
3216       ;; articles or some excluded dormants, we include these in the
3217       ;; buffer.
3218       (when (and (zerop (buffer-size))
3219                  (not no-display))
3220         (cond (gnus-newsgroup-dormant
3221                (gnus-summary-limit-include-dormant))
3222               ((and gnus-newsgroup-scored show-all)
3223                (gnus-summary-limit-include-expunged t))))
3224       ;; Function `gnus-apply-kill-file' must be called in this hook.
3225       (gnus-run-hooks 'gnus-apply-kill-hook)
3226       (if (and (zerop (buffer-size))
3227                (not no-display))
3228           (progn
3229             ;; This newsgroup is empty.
3230             (gnus-summary-catchup-and-exit nil t)
3231             (gnus-message 6 "No unread news")
3232             (when kill-buffer
3233               (gnus-kill-or-deaden-summary kill-buffer))
3234             ;; Return nil from this function.
3235             nil)
3236         ;; Hide conversation thread subtrees.  We cannot do this in
3237         ;; gnus-summary-prepare-hook since kill processing may not
3238         ;; work with hidden articles.
3239         (and gnus-show-threads
3240              gnus-thread-hide-subtree
3241              (gnus-summary-hide-all-threads))
3242         (when kill-buffer
3243           (gnus-kill-or-deaden-summary kill-buffer))
3244         ;; Show first unread article if requested.
3245         (if (and (not no-article)
3246                  (not no-display)
3247                  gnus-newsgroup-unreads
3248                  gnus-auto-select-first)
3249             (progn
3250               (gnus-configure-windows 'summary)
3251               (cond
3252                ((eq gnus-auto-select-first 'best)
3253                 (gnus-summary-best-unread-article))
3254                ((eq gnus-auto-select-first t)
3255                 (gnus-summary-first-unread-article))
3256                ((gnus-functionp gnus-auto-select-first)
3257                 (funcall gnus-auto-select-first))))
3258           ;; Don't select any articles, just move point to the first
3259           ;; article in the group.
3260           (goto-char (point-min))
3261           (gnus-summary-position-point)
3262           (gnus-configure-windows 'summary 'force)
3263           (gnus-set-mode-line 'summary))
3264         (when (get-buffer-window gnus-group-buffer t)
3265           ;; Gotta use windows, because recenter does weird stuff if
3266           ;; the current buffer ain't the displayed window.
3267           (let ((owin (selected-window)))
3268             (select-window (get-buffer-window gnus-group-buffer t))
3269             (when (gnus-group-goto-group group)
3270               (recenter))
3271             (select-window owin)))
3272         ;; Mark this buffer as "prepared".
3273         (setq gnus-newsgroup-prepared t)
3274         (gnus-run-hooks 'gnus-summary-prepared-hook)
3275         t)))))
3276
3277 (defun gnus-summary-prepare ()
3278   "Generate the summary buffer."
3279   (interactive)
3280   (let ((buffer-read-only nil))
3281     (erase-buffer)
3282     (setq gnus-newsgroup-data nil
3283           gnus-newsgroup-data-reverse nil)
3284     (gnus-run-hooks 'gnus-summary-generate-hook)
3285     ;; Generate the buffer, either with threads or without.
3286     (when gnus-newsgroup-headers
3287       (gnus-summary-prepare-threads
3288        (if gnus-show-threads
3289            (gnus-sort-gathered-threads
3290             (funcall gnus-summary-thread-gathering-function
3291                      (gnus-sort-threads
3292                       (gnus-cut-threads (gnus-make-threads)))))
3293          ;; Unthreaded display.
3294          (gnus-sort-articles gnus-newsgroup-headers))))
3295     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3296     ;; Call hooks for modifying summary buffer.
3297     (goto-char (point-min))
3298     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3299
3300 (defsubst gnus-general-simplify-subject (subject)
3301   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3302   (setq subject
3303         (cond
3304          ;; Truncate the subject.
3305          (gnus-simplify-subject-functions
3306           (gnus-map-function gnus-simplify-subject-functions subject))
3307          ((numberp gnus-summary-gather-subject-limit)
3308           (setq subject (gnus-simplify-subject-re subject))
3309           (if (> (length subject) gnus-summary-gather-subject-limit)
3310               (substring subject 0 gnus-summary-gather-subject-limit)
3311             subject))
3312          ;; Fuzzily simplify it.
3313          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3314           (gnus-simplify-subject-fuzzy subject))
3315          ;; Just remove the leading "Re:".
3316          (t
3317           (gnus-simplify-subject-re subject))))
3318
3319   (if (and gnus-summary-gather-exclude-subject
3320            (string-match gnus-summary-gather-exclude-subject subject))
3321       nil                               ; This article shouldn't be gathered
3322     subject))
3323
3324 (defun gnus-summary-simplify-subject-query ()
3325   "Query where the respool algorithm would put this article."
3326   (interactive)
3327   (gnus-summary-select-article)
3328   (message "%s"
3329            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3330
3331 (defun gnus-gather-threads-by-subject (threads)
3332   "Gather threads by looking at Subject headers."
3333   (if (not gnus-summary-make-false-root)
3334       threads
3335     (let ((hashtb (gnus-make-hashtable 1024))
3336           (prev threads)
3337           (result threads)
3338           subject hthread whole-subject)
3339       (while threads
3340         (setq subject (gnus-general-simplify-subject
3341                        (setq whole-subject (mail-header-subject
3342                                             (caar threads)))))
3343         (when subject
3344           (if (setq hthread (gnus-gethash subject hashtb))
3345               (progn
3346                 ;; We enter a dummy root into the thread, if we
3347                 ;; haven't done that already.
3348                 (unless (stringp (caar hthread))
3349                   (setcar hthread (list whole-subject (car hthread))))
3350                 ;; We add this new gathered thread to this gathered
3351                 ;; thread.
3352                 (setcdr (car hthread)
3353                         (nconc (cdar hthread) (list (car threads))))
3354                 ;; Remove it from the list of threads.
3355                 (setcdr prev (cdr threads))
3356                 (setq threads prev))
3357             ;; Enter this thread into the hash table.
3358             (gnus-sethash subject threads hashtb)))
3359         (setq prev threads)
3360         (setq threads (cdr threads)))
3361       result)))
3362
3363 (defun gnus-gather-threads-by-references (threads)
3364   "Gather threads by looking at References headers."
3365   (let ((idhashtb (gnus-make-hashtable 1024))
3366         (thhashtb (gnus-make-hashtable 1024))
3367         (prev threads)
3368         (result threads)
3369         ids references id gthread gid entered ref)
3370     (while threads
3371       (when (setq references (mail-header-references (caar threads)))
3372         (setq id (mail-header-id (caar threads))
3373               ids (gnus-split-references references)
3374               entered nil)
3375         (while (setq ref (pop ids))
3376           (setq ids (delete ref ids))
3377           (if (not (setq gid (gnus-gethash ref idhashtb)))
3378               (progn
3379                 (gnus-sethash ref id idhashtb)
3380                 (gnus-sethash id threads thhashtb))
3381             (setq gthread (gnus-gethash gid thhashtb))
3382             (unless entered
3383               ;; We enter a dummy root into the thread, if we
3384               ;; haven't done that already.
3385               (unless (stringp (caar gthread))
3386                 (setcar gthread (list (mail-header-subject (caar gthread))
3387                                       (car gthread))))
3388               ;; We add this new gathered thread to this gathered
3389               ;; thread.
3390               (setcdr (car gthread)
3391                       (nconc (cdar gthread) (list (car threads)))))
3392             ;; Add it into the thread hash table.
3393             (gnus-sethash id gthread thhashtb)
3394             (setq entered t)
3395             ;; Remove it from the list of threads.
3396             (setcdr prev (cdr threads))
3397             (setq threads prev))))
3398       (setq prev threads)
3399       (setq threads (cdr threads)))
3400     result))
3401
3402 (defun gnus-sort-gathered-threads (threads)
3403   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3404   (let ((result threads))
3405     (while threads
3406       (when (stringp (caar threads))
3407         (setcdr (car threads)
3408                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3409       (setq threads (cdr threads)))
3410     result))
3411
3412 (defun gnus-thread-loop-p (root thread)
3413   "Say whether ROOT is in THREAD."
3414   (let ((stack (list thread))
3415         (infloop 0)
3416         th)
3417     (while (setq thread (pop stack))
3418       (setq th (cdr thread))
3419       (while (and th
3420                   (not (eq (caar th) root)))
3421         (pop th))
3422       (if th
3423           ;; We have found a loop.
3424           (let (ref-dep)
3425             (setcdr thread (delq (car th) (cdr thread)))
3426             (if (boundp (setq ref-dep (intern "none"
3427                                               gnus-newsgroup-dependencies)))
3428                 (setcdr (symbol-value ref-dep)
3429                         (nconc (cdr (symbol-value ref-dep))
3430                                (list (car th))))
3431               (set ref-dep (list nil (car th))))
3432             (setq infloop 1
3433                   stack nil))
3434         ;; Push all the subthreads onto the stack.
3435         (push (cdr thread) stack)))
3436     infloop))
3437
3438 (defun gnus-make-threads ()
3439   "Go through the dependency hashtb and find the roots.  Return all threads."
3440   (let (threads)
3441     (while (catch 'infloop
3442              (mapatoms
3443               (lambda (refs)
3444                 ;; Deal with self-referencing References loops.
3445                 (when (and (car (symbol-value refs))
3446                            (not (zerop
3447                                  (apply
3448                                   '+
3449                                   (mapcar
3450                                    (lambda (thread)
3451                                      (gnus-thread-loop-p
3452                                       (car (symbol-value refs)) thread))
3453                                    (cdr (symbol-value refs)))))))
3454                   (setq threads nil)
3455                   (throw 'infloop t))
3456                 (unless (car (symbol-value refs))
3457                   ;; These threads do not refer back to any other articles,
3458                   ;; so they're roots.
3459                   (setq threads (append (cdr (symbol-value refs)) threads))))
3460               gnus-newsgroup-dependencies)))
3461     threads))
3462
3463 ;; Build the thread tree.
3464 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3465   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3466
3467 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3468 if it was already present.
3469
3470 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3471 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3472 Message-IDs will be renamed be renamed to a unique Message-ID before
3473 being entered.
3474
3475 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3476   (let* ((id (mail-header-id header))
3477          (id-dep (and id (intern id dependencies)))
3478          ref ref-dep ref-header)
3479     ;; Enter this `header' in the `dependencies' table.
3480     (cond
3481      ((not id-dep)
3482       (setq header nil))
3483      ;; The first two cases do the normal part: enter a new `header'
3484      ;; in the `dependencies' table.
3485      ((not (boundp id-dep))
3486       (set id-dep (list header)))
3487      ((null (car (symbol-value id-dep)))
3488       (setcar (symbol-value id-dep) header))
3489
3490      ;; From here the `header' was already present in the
3491      ;; `dependencies' table.
3492      (force-new
3493       ;; Overrides an existing entry;
3494       ;; just set the header part of the entry.
3495       (setcar (symbol-value id-dep) header))
3496
3497      ;; Renames the existing `header' to a unique Message-ID.
3498      ((not gnus-summary-ignore-duplicates)
3499       ;; An article with this Message-ID has already been seen.
3500       ;; We rename the Message-ID.
3501       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3502            (list header))
3503       (mail-header-set-id header id))
3504
3505      ;; The last case ignores an existing entry, except it adds any
3506      ;; additional Xrefs (in case the two articles came from different
3507      ;; servers.
3508      ;; Also sets `header' to `nil' meaning that the `dependencies'
3509      ;; table was *not* modified.
3510      (t
3511       (mail-header-set-xref
3512        (car (symbol-value id-dep))
3513        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3514                    "")
3515                (or (mail-header-xref header) "")))
3516       (setq header nil)))
3517
3518     (when header
3519       ;; First check if that we are not creating a References loop.
3520       (setq ref (gnus-parent-id (mail-header-references header)))
3521       (while (and ref
3522                   (setq ref-dep (intern-soft ref dependencies))
3523                   (boundp ref-dep)
3524                   (setq ref-header (car (symbol-value ref-dep))))
3525         (if (string= id ref)
3526             ;; Yuk!  This is a reference loop.  Make the article be a
3527             ;; root article.
3528             (progn
3529               (mail-header-set-references (car (symbol-value id-dep)) "none")
3530               (setq ref nil))
3531           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3532       (setq ref (gnus-parent-id (mail-header-references header)))
3533       (setq ref-dep (intern (or ref "none") dependencies))
3534       (if (boundp ref-dep)
3535           (setcdr (symbol-value ref-dep)
3536                   (nconc (cdr (symbol-value ref-dep))
3537                          (list (symbol-value id-dep))))
3538         (set ref-dep (list nil (symbol-value id-dep)))))
3539     header))
3540
3541 (defun gnus-build-sparse-threads ()
3542   (let ((headers gnus-newsgroup-headers)
3543         (mail-parse-charset gnus-newsgroup-charset)
3544         (gnus-summary-ignore-duplicates t)
3545         header references generation relations
3546         subject child end new-child date)
3547     ;; First we create an alist of generations/relations, where
3548     ;; generations is how much we trust the relation, and the relation
3549     ;; is parent/child.
3550     (gnus-message 7 "Making sparse threads...")
3551     (save-excursion
3552       (nnheader-set-temp-buffer " *gnus sparse threads*")
3553       (while (setq header (pop headers))
3554         (when (and (setq references (mail-header-references header))
3555                    (not (string= references "")))
3556           (insert references)
3557           (setq child (mail-header-id header)
3558                 subject (mail-header-subject header)
3559                 date (mail-header-date header)
3560                 generation 0)
3561           (while (search-backward ">" nil t)
3562             (setq end (1+ (point)))
3563             (when (search-backward "<" nil t)
3564               (setq new-child (buffer-substring (point) end))
3565               (push (list (incf generation)
3566                           child (setq child new-child)
3567                           subject date)
3568                     relations)))
3569           (when child
3570             (push (list (1+ generation) child nil subject) relations))
3571           (erase-buffer)))
3572       (kill-buffer (current-buffer)))
3573     ;; Sort over trustworthiness.
3574     (mapcar
3575      (lambda (relation)
3576        (when (gnus-dependencies-add-header
3577               (make-full-mail-header-from-decoded-header
3578                gnus-reffed-article-number
3579                (nth 3 relation) "" (or (nth 4 relation) "")
3580                (nth 1 relation)
3581                (or (nth 2 relation) "") 0 0 "")
3582               gnus-newsgroup-dependencies nil)
3583          (push gnus-reffed-article-number gnus-newsgroup-limit)
3584          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3585          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3586                gnus-newsgroup-reads)
3587          (decf gnus-reffed-article-number)))
3588      (sort relations 'car-less-than-car))
3589     (gnus-message 7 "Making sparse threads...done")))
3590
3591 (defun gnus-build-old-threads ()
3592   ;; Look at all the articles that refer back to old articles, and
3593   ;; fetch the headers for the articles that aren't there.  This will
3594   ;; build complete threads - if the roots haven't been expired by the
3595   ;; server, that is.
3596   (let ((mail-parse-charset gnus-newsgroup-charset)
3597         id heads)
3598     (mapatoms
3599      (lambda (refs)
3600        (when (not (car (symbol-value refs)))
3601          (setq heads (cdr (symbol-value refs)))
3602          (while heads
3603            (if (memq (mail-header-number (caar heads))
3604                      gnus-newsgroup-dormant)
3605                (setq heads (cdr heads))
3606              (setq id (symbol-name refs))
3607              (while (and (setq id (gnus-build-get-header id))
3608                          (not (car (gnus-id-to-thread id)))))
3609              (setq heads nil)))))
3610      gnus-newsgroup-dependencies)))
3611
3612 ;; This function has to be called with point after the article number
3613 ;; on the beginning of the line.
3614 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3615   (let ((eol (gnus-point-at-eol))
3616         (buffer (current-buffer))
3617         header)
3618
3619     ;; overview: [num subject from date id refs chars lines misc]
3620     (unwind-protect
3621         (progn
3622           (narrow-to-region (point) eol)
3623           (unless (eobp)
3624             (forward-char))
3625
3626           (setq header
3627                 (make-full-mail-header
3628                  number                         ; number
3629                  (nnheader-nov-field)           ; subject
3630                  (nnheader-nov-field)           ; from
3631                  (nnheader-nov-field)           ; date
3632                  (nnheader-nov-read-message-id) ; id
3633                  (nnheader-nov-field)           ; refs
3634                  (nnheader-nov-read-integer)    ; chars
3635                  (nnheader-nov-read-integer)    ; lines
3636                  (unless (eobp)
3637                    (if (looking-at "Xref: ")
3638                        (goto-char (match-end 0)))
3639                    (nnheader-nov-field))        ; Xref
3640                  (nnheader-nov-parse-extra))))  ; extra
3641
3642       (widen))
3643
3644     (when gnus-alter-header-function
3645       (funcall gnus-alter-header-function header))
3646     (gnus-dependencies-add-header header dependencies force-new)))
3647
3648 (defun gnus-build-get-header (id)
3649   "Look through the buffer of NOV lines and find the header to ID.
3650 Enter this line into the dependencies hash table, and return
3651 the id of the parent article (if any)."
3652   (let ((deps gnus-newsgroup-dependencies)
3653         found header)
3654     (prog1
3655         (save-excursion
3656           (set-buffer nntp-server-buffer)
3657           (let ((case-fold-search nil))
3658             (goto-char (point-min))
3659             (while (and (not found)
3660                         (search-forward id nil t))
3661               (beginning-of-line)
3662               (setq found (looking-at
3663                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3664                                    (regexp-quote id))))
3665               (or found (beginning-of-line 2)))
3666             (when found
3667               (beginning-of-line)
3668               (and
3669                (setq header (gnus-nov-parse-line
3670                              (read (current-buffer)) deps))
3671                (gnus-parent-id (mail-header-references header))))))
3672       (when header
3673         (let ((number (mail-header-number header)))
3674           (push number gnus-newsgroup-limit)
3675           (push header gnus-newsgroup-headers)
3676           (if (memq number gnus-newsgroup-unselected)
3677               (progn
3678                 (push number gnus-newsgroup-unreads)
3679                 (setq gnus-newsgroup-unselected
3680                       (delq number gnus-newsgroup-unselected)))
3681             (push number gnus-newsgroup-ancient)))))))
3682
3683 (defun gnus-build-all-threads ()
3684   "Read all the headers."
3685   (let ((gnus-summary-ignore-duplicates t)
3686         (mail-parse-charset gnus-newsgroup-charset)
3687         (dependencies gnus-newsgroup-dependencies)
3688         header article)
3689     (save-excursion
3690       (set-buffer nntp-server-buffer)
3691       (let ((case-fold-search nil))
3692         (goto-char (point-min))
3693         (while (not (eobp))
3694           (ignore-errors
3695             (setq article (read (current-buffer))
3696                   header (gnus-nov-parse-line article dependencies)))
3697           (when header
3698             (save-excursion
3699               (set-buffer gnus-summary-buffer)
3700               (push header gnus-newsgroup-headers)
3701               (if (memq (setq article (mail-header-number header))
3702                         gnus-newsgroup-unselected)
3703                   (progn
3704                     (push article gnus-newsgroup-unreads)
3705                     (setq gnus-newsgroup-unselected
3706                           (delq article gnus-newsgroup-unselected)))
3707                 (push article gnus-newsgroup-ancient)))
3708             (forward-line 1)))))))
3709
3710 (defun gnus-summary-update-article-line (article header)
3711   "Update the line for ARTICLE using HEADERS."
3712   (let* ((id (mail-header-id header))
3713          (thread (gnus-id-to-thread id)))
3714     (unless thread
3715       (error "Article in no thread"))
3716     ;; Update the thread.
3717     (setcar thread header)
3718     (gnus-summary-goto-subject article)
3719     (let* ((datal (gnus-data-find-list article))
3720            (data (car datal))
3721            (length (when (cdr datal)
3722                      (- (gnus-data-pos data)
3723                         (gnus-data-pos (cadr datal)))))
3724            (buffer-read-only nil)
3725            (level (gnus-summary-thread-level)))
3726       (gnus-delete-line)
3727       (gnus-summary-insert-line
3728        header level nil (gnus-article-mark article)
3729        (memq article gnus-newsgroup-replied)
3730        (memq article gnus-newsgroup-expirable)
3731        ;; Only insert the Subject string when it's different
3732        ;; from the previous Subject string.
3733        (if (and
3734             gnus-show-threads
3735             (gnus-subject-equal
3736              (condition-case ()
3737                  (mail-header-subject
3738                   (gnus-data-header
3739                    (cadr
3740                     (gnus-data-find-list
3741                      article
3742                      (gnus-data-list t)))))
3743                ;; Error on the side of excessive subjects.
3744                (error ""))
3745              (mail-header-subject header)))
3746            ""
3747          (mail-header-subject header))
3748        nil (cdr (assq article gnus-newsgroup-scored))
3749        (memq article gnus-newsgroup-processable))
3750       (when length
3751         (gnus-data-update-list
3752          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3753
3754 (defun gnus-summary-update-article (article &optional iheader)
3755   "Update ARTICLE in the summary buffer."
3756   (set-buffer gnus-summary-buffer)
3757   (let* ((header (gnus-summary-article-header article))
3758          (id (mail-header-id header))
3759          (data (gnus-data-find article))
3760          (thread (gnus-id-to-thread id))
3761          (references (mail-header-references header))
3762          (parent
3763           (gnus-id-to-thread
3764            (or (gnus-parent-id
3765                 (when (and references
3766                            (not (equal "" references)))
3767                   references))
3768                "none")))
3769          (buffer-read-only nil)
3770          (old (car thread)))
3771     (when thread
3772       (unless iheader
3773         (setcar thread nil)
3774         (when parent
3775           (delq thread parent)))
3776       (if (gnus-summary-insert-subject id header)
3777           ;; Set the (possibly) new article number in the data structure.
3778           (gnus-data-set-number data (gnus-id-to-article id))
3779         (setcar thread old)
3780         nil))))
3781
3782 (defun gnus-rebuild-thread (id &optional line)
3783   "Rebuild the thread containing ID.
3784 If LINE, insert the rebuilt thread starting on line LINE."
3785   (let ((buffer-read-only nil)
3786         old-pos current thread data)
3787     (if (not gnus-show-threads)
3788         (setq thread (list (car (gnus-id-to-thread id))))
3789       ;; Get the thread this article is part of.
3790       (setq thread (gnus-remove-thread id)))
3791     (setq old-pos (gnus-point-at-bol))
3792     (setq current (save-excursion
3793                     (and (re-search-backward "[\r\n]" nil t)
3794                          (gnus-summary-article-number))))
3795     ;; If this is a gathered thread, we have to go some re-gathering.
3796     (when (stringp (car thread))
3797       (let ((subject (car thread))
3798             roots thr)
3799         (setq thread (cdr thread))
3800         (while thread
3801           (unless (memq (setq thr (gnus-id-to-thread
3802                                    (gnus-root-id
3803                                     (mail-header-id (caar thread)))))
3804                         roots)
3805             (push thr roots))
3806           (setq thread (cdr thread)))
3807         ;; We now have all (unique) roots.
3808         (if (= (length roots) 1)
3809             ;; All the loose roots are now one solid root.
3810             (setq thread (car roots))
3811           (setq thread (cons subject (gnus-sort-threads roots))))))
3812     (let (threads)
3813       ;; We then insert this thread into the summary buffer.
3814       (when line
3815         (goto-char (point-min))
3816         (forward-line (1- line)))
3817       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3818         (if gnus-show-threads
3819             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3820           (gnus-summary-prepare-unthreaded thread))
3821         (setq data (nreverse gnus-newsgroup-data))
3822         (setq threads gnus-newsgroup-threads))
3823       ;; We splice the new data into the data structure.
3824       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3825       ;;!!! then we want to insert at the beginning of the buffer.
3826       ;;!!! That happens to be true with Gnus now, but that may
3827       ;;!!! change in the future.  Perhaps.
3828       (gnus-data-enter-list
3829        (if line nil current) data (- (point) old-pos))
3830       (setq gnus-newsgroup-threads
3831             (nconc threads gnus-newsgroup-threads))
3832       (gnus-data-compute-positions))))
3833
3834 (defun gnus-number-to-header (number)
3835   "Return the header for article NUMBER."
3836   (let ((headers gnus-newsgroup-headers))
3837     (while (and headers
3838                 (not (= number (mail-header-number (car headers)))))
3839       (pop headers))
3840     (when headers
3841       (car headers))))
3842
3843 (defun gnus-parent-headers (in-headers &optional generation)
3844   "Return the headers of the GENERATIONeth parent of HEADERS."
3845   (unless generation
3846     (setq generation 1))
3847   (let ((parent t)
3848         (headers in-headers)
3849         references)
3850     (while (and parent
3851                 (not (zerop generation))
3852                 (setq references (mail-header-references headers)))
3853       (setq headers (if (and references
3854                              (setq parent (gnus-parent-id references)))
3855                         (car (gnus-id-to-thread parent))
3856                       nil))
3857       (decf generation))
3858     (and (not (eq headers in-headers))
3859          headers)))
3860
3861 (defun gnus-id-to-thread (id)
3862   "Return the (sub-)thread where ID appears."
3863   (gnus-gethash id gnus-newsgroup-dependencies))
3864
3865 (defun gnus-id-to-article (id)
3866   "Return the article number of ID."
3867   (let ((thread (gnus-id-to-thread id)))
3868     (when (and thread
3869                (car thread))
3870       (mail-header-number (car thread)))))
3871
3872 (defun gnus-id-to-header (id)
3873   "Return the article headers of ID."
3874   (car (gnus-id-to-thread id)))
3875
3876 (defun gnus-article-displayed-root-p (article)
3877   "Say whether ARTICLE is a root(ish) article."
3878   (let ((level (gnus-summary-thread-level article))
3879         (refs (mail-header-references  (gnus-summary-article-header article)))
3880         particle)
3881     (cond
3882      ((null level) nil)
3883      ((zerop level) t)
3884      ((null refs) t)
3885      ((null (gnus-parent-id refs)) t)
3886      ((and (= 1 level)
3887            (null (setq particle (gnus-id-to-article
3888                                  (gnus-parent-id refs))))
3889            (null (gnus-summary-thread-level particle)))))))
3890
3891 (defun gnus-root-id (id)
3892   "Return the id of the root of the thread where ID appears."
3893   (let (last-id prev)
3894     (while (and id (setq prev (car (gnus-id-to-thread id))))
3895       (setq last-id id
3896             id (gnus-parent-id (mail-header-references prev))))
3897     last-id))
3898
3899 (defun gnus-articles-in-thread (thread)
3900   "Return the list of articles in THREAD."
3901   (cons (mail-header-number (car thread))
3902         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3903
3904 (defun gnus-remove-thread (id &optional dont-remove)
3905   "Remove the thread that has ID in it."
3906   (let (headers thread last-id)
3907     ;; First go up in this thread until we find the root.
3908     (setq last-id (gnus-root-id id)
3909           headers (message-flatten-list (gnus-id-to-thread last-id)))
3910     ;; We have now found the real root of this thread.  It might have
3911     ;; been gathered into some loose thread, so we have to search
3912     ;; through the threads to find the thread we wanted.
3913     (let ((threads gnus-newsgroup-threads)
3914           sub)
3915       (while threads
3916         (setq sub (car threads))
3917         (if (stringp (car sub))
3918             ;; This is a gathered thread, so we look at the roots
3919             ;; below it to find whether this article is in this
3920             ;; gathered root.
3921             (progn
3922               (setq sub (cdr sub))
3923               (while sub
3924                 (when (member (caar sub) headers)
3925                   (setq thread (car threads)
3926                         threads nil
3927                         sub nil))
3928                 (setq sub (cdr sub))))
3929           ;; It's an ordinary thread, so we check it.
3930           (when (eq (car sub) (car headers))
3931             (setq thread sub
3932                   threads nil)))
3933         (setq threads (cdr threads)))
3934       ;; If this article is in no thread, then it's a root.
3935       (if thread
3936           (unless dont-remove
3937             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3938         (setq thread (gnus-id-to-thread last-id)))
3939       (when thread
3940         (prog1
3941             thread                      ; We return this thread.
3942           (unless dont-remove
3943             (if (stringp (car thread))
3944                 (progn
3945                   ;; If we use dummy roots, then we have to remove the
3946                   ;; dummy root as well.
3947                   (when (eq gnus-summary-make-false-root 'dummy)
3948                     ;; We go to the dummy root by going to
3949                     ;; the first sub-"thread", and then one line up.
3950                     (gnus-summary-goto-article
3951                      (mail-header-number (caadr thread)))
3952                     (forward-line -1)
3953                     (gnus-delete-line)
3954                     (gnus-data-compute-positions))
3955                   (setq thread (cdr thread))
3956                   (while thread
3957                     (gnus-remove-thread-1 (car thread))
3958                     (setq thread (cdr thread))))
3959               (gnus-remove-thread-1 thread))))))))
3960
3961 (defun gnus-remove-thread-1 (thread)
3962   "Remove the thread THREAD recursively."
3963   (let ((number (mail-header-number (pop thread)))
3964         d)
3965     (setq thread (reverse thread))
3966     (while thread
3967       (gnus-remove-thread-1 (pop thread)))
3968     (when (setq d (gnus-data-find number))
3969       (goto-char (gnus-data-pos d))
3970       (gnus-summary-show-thread)
3971       (gnus-data-remove
3972        number
3973        (- (gnus-point-at-bol)
3974           (prog1
3975               (1+ (gnus-point-at-eol))
3976             (gnus-delete-line)))))))
3977
3978 (defun gnus-sort-threads-1 (threads func)
3979   (sort (mapcar (lambda (thread)
3980                   (cons (car thread)
3981                         (and (cdr thread)
3982                              (gnus-sort-threads-1 (cdr thread) func))))
3983                 threads) func))
3984
3985 (defun gnus-sort-threads (threads)
3986   "Sort THREADS."
3987   (if (not gnus-thread-sort-functions)
3988       threads
3989     (gnus-message 8 "Sorting threads...")
3990     (prog1
3991         (gnus-sort-threads-1
3992          threads
3993          (gnus-make-sort-function gnus-thread-sort-functions))
3994       (gnus-message 8 "Sorting threads...done"))))
3995
3996 (defun gnus-sort-articles (articles)
3997   "Sort ARTICLES."
3998   (when gnus-article-sort-functions
3999     (gnus-message 7 "Sorting articles...")
4000     (prog1
4001         (setq gnus-newsgroup-headers
4002               (sort articles (gnus-make-sort-function
4003                               gnus-article-sort-functions)))
4004       (gnus-message 7 "Sorting articles...done"))))
4005
4006 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4007 (defmacro gnus-thread-header (thread)
4008   "Return header of first article in THREAD.
4009 Note that THREAD must never, ever be anything else than a variable -
4010 using some other form will lead to serious barfage."
4011   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4012   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4013   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4014         (vector thread) 2))
4015
4016 (defsubst gnus-article-sort-by-number (h1 h2)
4017   "Sort articles by article number."
4018   (< (mail-header-number h1)
4019      (mail-header-number h2)))
4020
4021 (defun gnus-thread-sort-by-number (h1 h2)
4022   "Sort threads by root article number."
4023   (gnus-article-sort-by-number
4024    (gnus-thread-header h1) (gnus-thread-header h2)))
4025
4026 (defsubst gnus-article-sort-by-lines (h1 h2)
4027   "Sort articles by article Lines header."
4028   (< (mail-header-lines h1)
4029      (mail-header-lines h2)))
4030
4031 (defun gnus-thread-sort-by-lines (h1 h2)
4032   "Sort threads by root article Lines header."
4033   (gnus-article-sort-by-lines
4034    (gnus-thread-header h1) (gnus-thread-header h2)))
4035
4036 (defsubst gnus-article-sort-by-chars (h1 h2)
4037   "Sort articles by octet length."
4038   (< (mail-header-chars h1)
4039      (mail-header-chars h2)))
4040
4041 (defun gnus-thread-sort-by-chars (h1 h2)
4042   "Sort threads by root article octet length."
4043   (gnus-article-sort-by-chars
4044    (gnus-thread-header h1) (gnus-thread-header h2)))
4045
4046 (defsubst gnus-article-sort-by-author (h1 h2)
4047   "Sort articles by root author."
4048   (string-lessp
4049    (let ((addr (car (mime-entity-read-field h1 'From))))
4050      (or (std11-full-name-string addr)
4051          (std11-address-string addr)
4052          ""))
4053    (let ((addr (car (mime-entity-read-field h2 'From))))
4054      (or (std11-full-name-string addr)
4055          (std11-address-string addr)
4056          ""))
4057    ))
4058
4059 (defun gnus-thread-sort-by-author (h1 h2)
4060   "Sort threads by root author."
4061   (gnus-article-sort-by-author
4062    (gnus-thread-header h1)  (gnus-thread-header h2)))
4063
4064 (defsubst gnus-article-sort-by-subject (h1 h2)
4065   "Sort articles by root subject."
4066   (string-lessp
4067    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4068    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4069
4070 (defun gnus-thread-sort-by-subject (h1 h2)
4071   "Sort threads by root subject."
4072   (gnus-article-sort-by-subject
4073    (gnus-thread-header h1) (gnus-thread-header h2)))
4074
4075 (defsubst gnus-article-sort-by-date (h1 h2)
4076   "Sort articles by root article date."
4077   (time-less-p
4078    (gnus-date-get-time (mail-header-date h1))
4079    (gnus-date-get-time (mail-header-date h2))))
4080
4081 (defun gnus-thread-sort-by-date (h1 h2)
4082   "Sort threads by root article date."
4083   (gnus-article-sort-by-date
4084    (gnus-thread-header h1) (gnus-thread-header h2)))
4085
4086 (defsubst gnus-article-sort-by-score (h1 h2)
4087   "Sort articles by root article score.
4088 Unscored articles will be counted as having a score of zero."
4089   (> (or (cdr (assq (mail-header-number h1)
4090                     gnus-newsgroup-scored))
4091          gnus-summary-default-score 0)
4092      (or (cdr (assq (mail-header-number h2)
4093                     gnus-newsgroup-scored))
4094          gnus-summary-default-score 0)))
4095
4096 (defun gnus-thread-sort-by-score (h1 h2)
4097   "Sort threads by root article score."
4098   (gnus-article-sort-by-score
4099    (gnus-thread-header h1) (gnus-thread-header h2)))
4100
4101 (defun gnus-thread-sort-by-total-score (h1 h2)
4102   "Sort threads by the sum of all scores in the thread.
4103 Unscored articles will be counted as having a score of zero."
4104   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4105
4106 (defun gnus-thread-total-score (thread)
4107   ;; This function find the total score of THREAD.
4108   (cond ((null thread)
4109          0)
4110         ((consp thread)
4111          (if (stringp (car thread))
4112              (apply gnus-thread-score-function 0
4113                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4114            (gnus-thread-total-score-1 thread)))
4115         (t
4116          (gnus-thread-total-score-1 (list thread)))))
4117
4118 (defun gnus-thread-total-score-1 (root)
4119   ;; This function find the total score of the thread below ROOT.
4120   (setq root (car root))
4121   (apply gnus-thread-score-function
4122          (or (append
4123               (mapcar 'gnus-thread-total-score
4124                       (cdr (gnus-id-to-thread (mail-header-id root))))
4125               (when (> (mail-header-number root) 0)
4126                 (list (or (cdr (assq (mail-header-number root)
4127                                      gnus-newsgroup-scored))
4128                           gnus-summary-default-score 0))))
4129              (list gnus-summary-default-score)
4130              '(0))))
4131
4132 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4133 (defvar gnus-tmp-prev-subject nil)
4134 (defvar gnus-tmp-false-parent nil)
4135 (defvar gnus-tmp-root-expunged nil)
4136 (defvar gnus-tmp-dummy-line nil)
4137
4138 (eval-when-compile (defvar gnus-tmp-header))
4139 (defun gnus-extra-header (type &optional header)
4140   "Return the extra header of TYPE."
4141   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4142       ""))
4143
4144 (defvar gnus-tmp-thread-tree-header-string "")
4145
4146 (defvar gnus-sum-thread-tree-root "> "
4147   "With %B spec, used for the root of a thread.
4148 If nil, use subject instead.")
4149 (defvar gnus-sum-thread-tree-single-indent ""
4150   "With %B spec, used for a thread with just one message.
4151 If nil, use subject instead.")
4152 (defvar gnus-sum-thread-tree-vertical "| "
4153   "With %B spec, used for drawing a vertical line.")
4154 (defvar gnus-sum-thread-tree-indent "  "
4155   "With %B spec, used for indenting.")
4156 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4157   "With %B spec, used for a leaf with brothers.")
4158 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4159   "With %B spec, used for a leaf without brothers.")
4160
4161 (defun gnus-summary-prepare-threads (threads)
4162   "Prepare summary buffer from THREADS and indentation LEVEL.
4163 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4164 or a straight list of headers."
4165   (gnus-message 7 "Generating summary...")
4166
4167   (setq gnus-newsgroup-threads threads)
4168   (beginning-of-line)
4169
4170   (let ((gnus-tmp-level 0)
4171         (default-score (or gnus-summary-default-score 0))
4172         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4173         thread number subject stack state gnus-tmp-gathered beg-match
4174         new-roots gnus-tmp-new-adopts thread-end
4175         gnus-tmp-header gnus-tmp-unread
4176         gnus-tmp-replied gnus-tmp-subject-or-nil
4177         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4178         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4179         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4180         tree-stack)
4181
4182     (setq gnus-tmp-prev-subject nil)
4183
4184     (if (vectorp (car threads))
4185         ;; If this is a straight (sic) list of headers, then a
4186         ;; threaded summary display isn't required, so we just create
4187         ;; an unthreaded one.
4188         (gnus-summary-prepare-unthreaded threads)
4189
4190       ;; Do the threaded display.
4191
4192       (while (or threads stack gnus-tmp-new-adopts new-roots)
4193
4194         (if (and (= gnus-tmp-level 0)
4195                  (or (not stack)
4196                      (= (caar stack) 0))
4197                  (not gnus-tmp-false-parent)
4198                  (or gnus-tmp-new-adopts new-roots))
4199             (if gnus-tmp-new-adopts
4200                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4201                       thread (list (car gnus-tmp-new-adopts))
4202                       gnus-tmp-header (caar thread)
4203                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4204               (when new-roots
4205                 (setq thread (list (car new-roots))
4206                       gnus-tmp-header (caar thread)
4207                       new-roots (cdr new-roots))))
4208
4209           (if threads
4210               ;; If there are some threads, we do them before the
4211               ;; threads on the stack.
4212               (setq thread threads
4213                     gnus-tmp-header (caar thread))
4214             ;; There were no current threads, so we pop something off
4215             ;; the stack.
4216             (setq state (car stack)
4217                   gnus-tmp-level (car state)
4218                   tree-stack (cadr state)
4219                   thread (caddr state)
4220                   stack (cdr stack)
4221                   gnus-tmp-header (caar thread))))
4222
4223         (setq gnus-tmp-false-parent nil)
4224         (setq gnus-tmp-root-expunged nil)
4225         (setq thread-end nil)
4226
4227         (if (stringp gnus-tmp-header)
4228             ;; The header is a dummy root.
4229             (cond
4230              ((eq gnus-summary-make-false-root 'adopt)
4231               ;; We let the first article adopt the rest.
4232               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4233                                                (cddar thread)))
4234               (setq gnus-tmp-gathered
4235                     (nconc (mapcar
4236                             (lambda (h) (mail-header-number (car h)))
4237                             (cddar thread))
4238                            gnus-tmp-gathered))
4239               (setq thread (cons (list (caar thread)
4240                                        (cadar thread))
4241                                  (cdr thread)))
4242               (setq gnus-tmp-level -1
4243                     gnus-tmp-false-parent t))
4244              ((eq gnus-summary-make-false-root 'empty)
4245               ;; We print adopted articles with empty subject fields.
4246               (setq gnus-tmp-gathered
4247                     (nconc (mapcar
4248                             (lambda (h) (mail-header-number (car h)))
4249                             (cddar thread))
4250                            gnus-tmp-gathered))
4251               (setq gnus-tmp-level -1))
4252              ((eq gnus-summary-make-false-root 'dummy)
4253               ;; We remember that we probably want to output a dummy
4254               ;; root.
4255               (setq gnus-tmp-dummy-line gnus-tmp-header)
4256               (setq gnus-tmp-prev-subject gnus-tmp-header))
4257              (t
4258               ;; We do not make a root for the gathered
4259               ;; sub-threads at all.
4260               (setq gnus-tmp-level -1)))
4261
4262           (setq number (mail-header-number gnus-tmp-header)
4263                 subject (mail-header-subject gnus-tmp-header))
4264
4265           (cond
4266            ;; If the thread has changed subject, we might want to make
4267            ;; this subthread into a root.
4268            ((and (null gnus-thread-ignore-subject)
4269                  (not (zerop gnus-tmp-level))
4270                  gnus-tmp-prev-subject
4271                  (not (inline
4272                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4273             (setq new-roots (nconc new-roots (list (car thread)))
4274                   thread-end t
4275                   gnus-tmp-header nil))
4276            ;; If the article lies outside the current limit,
4277            ;; then we do not display it.
4278            ((not (memq number gnus-newsgroup-limit))
4279             (setq gnus-tmp-gathered
4280                   (nconc (mapcar
4281                           (lambda (h) (mail-header-number (car h)))
4282                           (cdar thread))
4283                          gnus-tmp-gathered))
4284             (setq gnus-tmp-new-adopts (if (cdar thread)
4285                                           (append gnus-tmp-new-adopts
4286                                                   (cdar thread))
4287                                         gnus-tmp-new-adopts)
4288                   thread-end t
4289                   gnus-tmp-header nil)
4290             (when (zerop gnus-tmp-level)
4291               (setq gnus-tmp-root-expunged t)))
4292            ;; Perhaps this article is to be marked as read?
4293            ((and gnus-summary-mark-below
4294                  (< (or (cdr (assq number gnus-newsgroup-scored))
4295                         default-score)
4296                     gnus-summary-mark-below)
4297                  ;; Don't touch sparse articles.
4298                  (not (gnus-summary-article-sparse-p number))
4299                  (not (gnus-summary-article-ancient-p number)))
4300             (setq gnus-newsgroup-unreads
4301                   (delq number gnus-newsgroup-unreads))
4302             (if gnus-newsgroup-auto-expire
4303                 (push number gnus-newsgroup-expirable)
4304               (push (cons number gnus-low-score-mark)
4305                     gnus-newsgroup-reads))))
4306
4307           (when gnus-tmp-header
4308             ;; We may have an old dummy line to output before this
4309             ;; article.
4310             (when (and gnus-tmp-dummy-line
4311                        (gnus-subject-equal
4312                         gnus-tmp-dummy-line
4313                         (mail-header-subject gnus-tmp-header)))
4314               (gnus-summary-insert-dummy-line
4315                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4316               (setq gnus-tmp-dummy-line nil))
4317
4318             ;; Compute the mark.
4319             (setq gnus-tmp-unread (gnus-article-mark number))
4320
4321             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4322                                   gnus-tmp-header gnus-tmp-level)
4323                   gnus-newsgroup-data)
4324
4325             ;; Actually insert the line.
4326             (setq
4327              gnus-tmp-subject-or-nil
4328              (cond
4329               ((and gnus-thread-ignore-subject
4330                     gnus-tmp-prev-subject
4331                     (not (inline (gnus-subject-equal
4332                                   gnus-tmp-prev-subject subject))))
4333                subject)
4334               ((zerop gnus-tmp-level)
4335                (if (and (eq gnus-summary-make-false-root 'empty)
4336                         (memq number gnus-tmp-gathered)
4337                         gnus-tmp-prev-subject
4338                         (inline (gnus-subject-equal
4339                                  gnus-tmp-prev-subject subject)))
4340                    gnus-summary-same-subject
4341                  subject))
4342               (t gnus-summary-same-subject)))
4343             (if (and (eq gnus-summary-make-false-root 'adopt)
4344                      (= gnus-tmp-level 1)
4345                      (memq number gnus-tmp-gathered))
4346                 (setq gnus-tmp-opening-bracket ?\<
4347                       gnus-tmp-closing-bracket ?\>)
4348               (setq gnus-tmp-opening-bracket ?\[
4349                     gnus-tmp-closing-bracket ?\]))
4350             (setq
4351              gnus-tmp-indentation
4352              (aref gnus-thread-indent-array gnus-tmp-level)
4353              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4354              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4355                                 gnus-summary-default-score 0)
4356              gnus-tmp-score-char
4357              (if (or (null gnus-summary-default-score)
4358                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4359                          gnus-summary-zcore-fuzz))
4360                  ?\ ;;;Whitespace
4361                (if (< gnus-tmp-score gnus-summary-default-score)
4362                    gnus-score-below-mark gnus-score-over-mark))
4363              gnus-tmp-replied
4364              (cond ((memq number gnus-newsgroup-processable)
4365                     gnus-process-mark)
4366                    ((memq number gnus-newsgroup-cached)
4367                     gnus-cached-mark)
4368                    ((memq number gnus-newsgroup-replied)
4369                     gnus-replied-mark)
4370                    ((memq number gnus-newsgroup-forwarded)
4371                     gnus-forwarded-mark)
4372                    ((memq number gnus-newsgroup-saved)
4373                     gnus-saved-mark)
4374                    ((memq number gnus-newsgroup-recent)
4375                     gnus-recent-mark)
4376                    ((memq number gnus-newsgroup-unseen)
4377                     gnus-unseen-mark)
4378                    (t gnus-no-mark))
4379              gnus-tmp-from (mail-header-from gnus-tmp-header)
4380              gnus-tmp-name
4381              (cond
4382               ((string-match "<[^>]+> *$" gnus-tmp-from)
4383                (setq beg-match (match-beginning 0))
4384                (or (and (string-match "^\".+\"" gnus-tmp-from)
4385                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4386                    (substring gnus-tmp-from 0 beg-match)))
4387               ((string-match "(.+)" gnus-tmp-from)
4388                (substring gnus-tmp-from
4389                           (1+ (match-beginning 0)) (1- (match-end 0))))
4390               (t gnus-tmp-from))
4391              gnus-tmp-thread-tree-header-string
4392              (cond
4393               ((not gnus-show-threads) "")
4394               ((zerop gnus-tmp-level)
4395                (if (cdar thread)
4396                    (or gnus-sum-thread-tree-root subject)
4397                  (or gnus-sum-thread-tree-single-indent subject)))
4398               (t
4399                (concat (apply 'concat
4400                               (mapcar (lambda (item)
4401                                         (if (= item 1)
4402                                             gnus-sum-thread-tree-vertical
4403                                           gnus-sum-thread-tree-indent))
4404                                       (cdr (reverse tree-stack))))
4405                        (if (nth 1 thread)
4406                            gnus-sum-thread-tree-leaf-with-other
4407                          gnus-sum-thread-tree-single-leaf)))))
4408             (when (string= gnus-tmp-name "")
4409               (setq gnus-tmp-name gnus-tmp-from))
4410             (unless (numberp gnus-tmp-lines)
4411               (setq gnus-tmp-lines -1))
4412             (when (= gnus-tmp-lines -1)
4413               (setq gnus-tmp-lines "?"))
4414             (gnus-put-text-property
4415              (point)
4416              (progn (eval gnus-summary-line-format-spec) (point))
4417              'gnus-number number)
4418             (when gnus-visual-p
4419               (forward-line -1)
4420               (gnus-run-hooks 'gnus-summary-update-hook)
4421               (forward-line 1))
4422
4423             (setq gnus-tmp-prev-subject subject)))
4424
4425         (when (nth 1 thread)
4426           (push (list (max 0 gnus-tmp-level)
4427                       (copy-list tree-stack)
4428                       (nthcdr 1 thread))
4429                 stack))
4430         (push (if (nth 1 thread) 1 0) tree-stack)
4431         (incf gnus-tmp-level)
4432         (setq threads (if thread-end nil (cdar thread)))
4433         (unless threads
4434           (setq gnus-tmp-level 0)))))
4435   (gnus-message 7 "Generating summary...done"))
4436
4437 (defun gnus-summary-prepare-unthreaded (headers)
4438   "Generate an unthreaded summary buffer based on HEADERS."
4439   (let (header number mark)
4440
4441     (beginning-of-line)
4442
4443     (while headers
4444       ;; We may have to root out some bad articles...
4445       (when (memq (setq number (mail-header-number
4446                                 (setq header (pop headers))))
4447                   gnus-newsgroup-limit)
4448         ;; Mark article as read when it has a low score.
4449         (when (and gnus-summary-mark-below
4450                    (< (or (cdr (assq number gnus-newsgroup-scored))
4451                           gnus-summary-default-score 0)
4452                       gnus-summary-mark-below)
4453                    (not (gnus-summary-article-ancient-p number)))
4454           (setq gnus-newsgroup-unreads
4455                 (delq number gnus-newsgroup-unreads))
4456           (if gnus-newsgroup-auto-expire
4457               (push number gnus-newsgroup-expirable)
4458             (push (cons number gnus-low-score-mark)
4459                   gnus-newsgroup-reads)))
4460
4461         (setq mark (gnus-article-mark number))
4462         (push (gnus-data-make number mark (1+ (point)) header 0)
4463               gnus-newsgroup-data)
4464         (gnus-summary-insert-line
4465          header 0 number
4466          mark (memq number gnus-newsgroup-replied)
4467          (memq number gnus-newsgroup-expirable)
4468          (mail-header-subject header) nil
4469          (cdr (assq number gnus-newsgroup-scored))
4470          (memq number gnus-newsgroup-processable))))))
4471
4472 (defun gnus-summary-remove-list-identifiers ()
4473   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4474   (let ((regexp (if (consp gnus-list-identifiers)
4475                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4476                   gnus-list-identifiers))
4477         changed subject)
4478     (when regexp
4479       (dolist (header gnus-newsgroup-headers)
4480         (setq subject (mail-header-subject header)
4481               changed nil)
4482         (while (string-match
4483                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4484                 subject)
4485           (setq subject
4486                 (concat (substring subject 0 (match-beginning 2))
4487                         (substring subject (match-end 0)))
4488                 changed t))
4489         (when (and changed
4490                    (string-match
4491                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4492           (setq subject
4493                 (concat (substring subject 0 (match-beginning 1))
4494                         (substring subject (match-end 1)))))
4495         (when changed
4496           (mail-header-set-subject header subject))))))
4497
4498 (defun gnus-fetch-headers (articles)
4499   "Fetch headers of ARTICLES."
4500   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4501     (gnus-message 5 "Fetching headers for %s..." name)
4502     (prog1
4503         (if (eq 'nov
4504                 (setq gnus-headers-retrieved-by
4505                       (gnus-retrieve-headers
4506                        articles gnus-newsgroup-name
4507                        ;; We might want to fetch old headers, but
4508                        ;; not if there is only 1 article.
4509                        (and (or (and
4510                                  (not (eq gnus-fetch-old-headers 'some))
4511                                  (not (numberp gnus-fetch-old-headers)))
4512                                 (> (length articles) 1))
4513                             gnus-fetch-old-headers))))
4514             (gnus-get-newsgroup-headers-xover
4515              articles nil nil gnus-newsgroup-name t)
4516           (gnus-get-newsgroup-headers))
4517       (gnus-message 5 "Fetching headers for %s...done" name))))
4518
4519 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4520   "Select newsgroup GROUP.
4521 If READ-ALL is non-nil, all articles in the group are selected.
4522 If SELECT-ARTICLES, only select those articles from GROUP."
4523   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4524          ;;!!! Dirty hack; should be removed.
4525          (gnus-summary-ignore-duplicates
4526           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4527               t
4528             gnus-summary-ignore-duplicates))
4529          (info (nth 2 entry))
4530          articles fetched-articles cached)
4531
4532     (unless (gnus-check-server
4533              (setq gnus-current-select-method
4534                    (gnus-find-method-for-group group)))
4535       (error "Couldn't open server"))
4536
4537     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4538         (gnus-activate-group group)     ; Or we can activate it...
4539         (progn                          ; Or we bug out.
4540           (when (equal major-mode 'gnus-summary-mode)
4541             (kill-buffer (current-buffer)))
4542           (error "Couldn't activate group %s: %s"
4543                  group (gnus-status-message group))))
4544
4545     (unless (gnus-request-group group t)
4546       (when (equal major-mode 'gnus-summary-mode)
4547         (kill-buffer (current-buffer)))
4548       (error "Couldn't request group %s: %s"
4549              group (gnus-status-message group)))
4550
4551     (setq gnus-newsgroup-name group
4552           gnus-newsgroup-unselected nil
4553           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4554
4555     (setq gnus-newsgroup-display (gnus-group-find-parameter group 'display))
4556     (setq gnus-newsgroup-display
4557           (cond
4558            ((eq gnus-newsgroup-display 'all)
4559             'all)
4560            ((arrayp gnus-newsgroup-display)
4561             (gnus-summary-display-make-predicate
4562              (mapcar 'identity gnus-newsgroup-display)))
4563            (t
4564             nil)))
4565
4566     (gnus-summary-setup-default-charset)
4567
4568     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4569     (when (gnus-virtual-group-p group)
4570       (setq cached gnus-newsgroup-cached))
4571
4572     (setq gnus-newsgroup-unreads
4573           (gnus-set-difference
4574            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4575            gnus-newsgroup-dormant))
4576
4577     (setq gnus-newsgroup-processable nil)
4578
4579     (gnus-update-read-articles group gnus-newsgroup-unreads)
4580
4581     ;; Adjust and set lists of article marks.
4582     (when info
4583       (gnus-adjust-marked-articles info))
4584
4585     (if (setq articles select-articles)
4586         (setq gnus-newsgroup-unselected
4587               (gnus-sorted-intersection
4588                gnus-newsgroup-unreads
4589                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4590       (setq articles (gnus-articles-to-read group read-all)))
4591
4592     (cond
4593      ((null articles)
4594       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4595       'quit)
4596      ((eq articles 0) nil)
4597      (t
4598       ;; Init the dependencies hash table.
4599       (setq gnus-newsgroup-dependencies
4600             (gnus-make-hashtable (length articles)))
4601       (gnus-set-global-variables)
4602       ;; Retrieve the headers and read them in.
4603       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4604
4605       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4606       (when cached
4607         (setq gnus-newsgroup-cached cached))
4608
4609       ;; Suppress duplicates?
4610       (when gnus-suppress-duplicates
4611         (gnus-dup-suppress-articles))
4612
4613       ;; Set the initial limit.
4614       (setq gnus-newsgroup-limit (copy-sequence articles))
4615       ;; Remove canceled articles from the list of unread articles.
4616       (setq fetched-articles
4617             (mapcar (lambda (headers) (mail-header-number headers))
4618                     gnus-newsgroup-headers))
4619       (setq gnus-newsgroup-articles fetched-articles)
4620       (setq gnus-newsgroup-unreads
4621             (gnus-set-sorted-intersection
4622              gnus-newsgroup-unreads fetched-articles))
4623
4624       (let ((marks (assq 'seen (gnus-info-marks info))))
4625         ;; The `seen' marks are treated specially.
4626         (when (setq gnus-newsgroup-seen (cdr marks))
4627           (dolist (article gnus-newsgroup-articles)
4628             (unless (gnus-member-of-range
4629                      article gnus-newsgroup-seen)
4630               (push article gnus-newsgroup-unseen)))))
4631
4632       ;; Removed marked articles that do not exist.
4633       (gnus-update-missing-marks
4634        (gnus-sorted-complement fetched-articles articles))
4635       ;; We might want to build some more threads first.
4636       (when (and gnus-fetch-old-headers
4637                  (eq gnus-headers-retrieved-by 'nov))
4638         (if (eq gnus-fetch-old-headers 'invisible)
4639             (gnus-build-all-threads)
4640           (gnus-build-old-threads)))
4641       ;; Let the Gnus agent mark articles as read.
4642       (when gnus-agent
4643         (gnus-agent-get-undownloaded-list))
4644       ;; Remove list identifiers from subject
4645       (when gnus-list-identifiers
4646         (gnus-summary-remove-list-identifiers))
4647       ;; Check whether auto-expire is to be done in this group.
4648       (setq gnus-newsgroup-auto-expire
4649             (gnus-group-auto-expirable-p group))
4650       ;; Set up the article buffer now, if necessary.
4651       (unless gnus-single-article-buffer
4652         (gnus-article-setup-buffer))
4653       ;; First and last article in this newsgroup.
4654       (when gnus-newsgroup-headers
4655         (setq gnus-newsgroup-begin
4656               (mail-header-number (car gnus-newsgroup-headers))
4657               gnus-newsgroup-end
4658               (mail-header-number
4659                (gnus-last-element gnus-newsgroup-headers))))
4660       ;; GROUP is successfully selected.
4661       (or gnus-newsgroup-headers t)))))
4662
4663 (defun gnus-summary-display-make-predicate (display)
4664   (require 'gnus-agent)
4665   (when (= (length display) 1)
4666     (setq display (car display)))
4667   (unless gnus-summary-display-cache
4668     (dolist (elem gnus-article-mark-lists)
4669         (push (cons (cdr elem)
4670                     (gnus-byte-compile
4671                      `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4672               gnus-summary-display-cache)))
4673   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4674     (gnus-get-predicate display)))
4675
4676 ;; Uses the dynamically bound `number' variable.
4677 (defvar number)
4678 (defun gnus-article-marked-p (type &optional article)
4679   (let ((article (or article number)))
4680     (cond
4681      ((eq type 'tick)
4682       (memq article gnus-newsgroup-marked))
4683      ((eq type 'unsend)
4684       (memq article gnus-newsgroup-unsendable))
4685      ((eq type 'undownload)
4686       (memq article gnus-newsgroup-undownloaded))
4687      ((eq type 'download)
4688       (memq article gnus-newsgroup-downloadable))
4689      ((eq type 'unread)
4690       (memq article gnus-newsgroup-unreads))
4691      ((eq type 'read)
4692       (memq article gnus-newsgroup-reads))
4693      ((eq type 'dormant)
4694       (memq article gnus-newsgroup-dormant) )
4695      ((eq type 'expire)
4696       (memq article gnus-newsgroup-expirable))
4697      ((eq type 'reply)
4698       (memq article gnus-newsgroup-replied))
4699      ((eq type 'killed)
4700       (memq article gnus-newsgroup-killed))
4701      ((eq type 'bookmark)
4702       (assq article gnus-newsgroup-bookmarks))
4703      ((eq type 'score)
4704       (assq article gnus-newsgroup-scored))
4705      ((eq type 'save)
4706       (memq article gnus-newsgroup-saved))
4707      ((eq type 'cache)
4708       (memq article gnus-newsgroup-cached))
4709      ((eq type 'forward)
4710       (memq article gnus-newsgroup-forwarded))
4711      ((eq type 'recent)
4712       (memq article gnus-newsgroup-recent))
4713      (t t))))
4714
4715 (defun gnus-articles-to-read (group &optional read-all)
4716   "Find out what articles the user wants to read."
4717   (let* ((articles
4718           ;; Select all articles if `read-all' is non-nil, or if there
4719           ;; are no unread articles.
4720           (if (or read-all
4721                   (and (zerop (length gnus-newsgroup-marked))
4722                        (zerop (length gnus-newsgroup-unreads)))
4723                   gnus-newsgroup-display)
4724               ;; We want to select the headers for all the articles in
4725               ;; the group, so we select either all the active
4726               ;; articles in the group, or (if that's nil), the
4727               ;; articles in the cache.
4728               (or
4729                (gnus-uncompress-range (gnus-active group))
4730                (gnus-cache-articles-in-group group))
4731             ;; Select only the "normal" subset of articles.
4732             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4733                           (copy-sequence gnus-newsgroup-unreads))
4734                   '<)))
4735          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4736          (scored (length scored-list))
4737          (number (length articles))
4738          (marked (+ (length gnus-newsgroup-marked)
4739                     (length gnus-newsgroup-dormant)))
4740          (select
4741           (cond
4742            ((numberp read-all)
4743             read-all)
4744            (t
4745             (condition-case ()
4746                 (cond
4747                  ((and (or (<= scored marked) (= scored number))
4748                        (natnump gnus-large-newsgroup)
4749                        (> number gnus-large-newsgroup))
4750                   (let* ((cursor-in-echo-area nil)
4751                          (input
4752                           (read-from-minibuffer
4753                            (format
4754                             "How many articles from %s (max %d): "
4755                             (gnus-limit-string
4756                              (gnus-group-decoded-name gnus-newsgroup-name)
4757                              35)
4758                             number)
4759                            (cons (number-to-string gnus-large-newsgroup)
4760                                  0))))
4761                     (if (string-match "^[ \t]*$" input)
4762                         number
4763                       input)))
4764                  ((and (> scored marked) (< scored number)
4765                        (> (- scored number) 20))
4766                   (let ((input
4767                          (read-string
4768                           (format "%s %s (%d scored, %d total): "
4769                                   "How many articles from"
4770                                   (gnus-group-decoded-name group)
4771                                   scored number))))
4772                     (if (string-match "^[ \t]*$" input)
4773                         number input)))
4774                  (t number))
4775               (quit
4776                (message "Quit getting the articles to read")
4777                nil))))))
4778     (setq select (if (stringp select) (string-to-number select) select))
4779     (if (or (null select) (zerop select))
4780         select
4781       (if (and (not (zerop scored)) (<= (abs select) scored))
4782           (progn
4783             (setq articles (sort scored-list '<))
4784             (setq number (length articles)))
4785         (setq articles (copy-sequence articles)))
4786
4787       (when (< (abs select) number)
4788         (if (< select 0)
4789             ;; Select the N oldest articles.
4790             (setcdr (nthcdr (1- (abs select)) articles) nil)
4791           ;; Select the N most recent articles.
4792           (setq articles (nthcdr (- number select) articles))))
4793       (setq gnus-newsgroup-unselected
4794             (gnus-sorted-intersection
4795              gnus-newsgroup-unreads
4796              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4797       (when gnus-alter-articles-to-read-function
4798         (setq gnus-newsgroup-unreads
4799               (sort
4800                (funcall gnus-alter-articles-to-read-function
4801                         gnus-newsgroup-name gnus-newsgroup-unreads)
4802                '<)))
4803       articles)))
4804
4805 (defun gnus-killed-articles (killed articles)
4806   (let (out)
4807     (while articles
4808       (when (inline (gnus-member-of-range (car articles) killed))
4809         (push (car articles) out))
4810       (setq articles (cdr articles)))
4811     out))
4812
4813 (defun gnus-uncompress-marks (marks)
4814   "Uncompress the mark ranges in MARKS."
4815   (let ((uncompressed '(score bookmark))
4816         out)
4817     (while marks
4818       (if (memq (caar marks) uncompressed)
4819           (push (car marks) out)
4820         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4821       (setq marks (cdr marks)))
4822     out))
4823
4824 (defun gnus-article-mark-to-type (mark)
4825   "Return the type of MARK."
4826   (or (cadr (assq mark gnus-article-special-mark-lists))
4827       'list))
4828
4829 (defun gnus-adjust-marked-articles (info)
4830   "Set all article lists and remove all marks that are no longer valid."
4831   (let* ((marked-lists (gnus-info-marks info))
4832          (active (gnus-active (gnus-info-group info)))
4833          (min (car active))
4834          (max (cdr active))
4835          (types gnus-article-mark-lists)
4836          marks var articles article mark mark-type)
4837
4838     (dolist (marks marked-lists)
4839       (setq mark (car marks)
4840             mark-type (gnus-article-mark-to-type mark)
4841             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4842
4843       ;; We set the variable according to the type of the marks list,
4844       ;; and then adjust the marks to a subset of the active articles.
4845       (cond
4846        ;; Adjust "simple" lists.
4847        ((eq mark-type 'list)
4848         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4849         (when (memq mark '(tick dormant expire reply save))
4850           (while articles
4851             (when (or (< (setq article (pop articles)) min) (> article max))
4852               (set var (delq article (symbol-value var)))))))
4853        ;; Adjust assocs.
4854        ((eq mark-type 'tuple)
4855         (set var (setq articles (cdr marks)))
4856         (when (not (listp (cdr (symbol-value var))))
4857           (set var (list (symbol-value var))))
4858         (when (not (listp (cdr articles)))
4859           (setq articles (list articles)))
4860         (while articles
4861           (when (or (not (consp (setq article (pop articles))))
4862                     (< (car article) min)
4863                     (> (car article) max))
4864             (set var (delq article (symbol-value var))))))
4865        ((eq mark-type 'range)
4866         (cond
4867          ((eq mark 'seen))))))))
4868
4869 (defun gnus-update-missing-marks (missing)
4870   "Go through the list of MISSING articles and remove them from the mark lists."
4871   (when missing
4872     (let (var m)
4873       ;; Go through all types.
4874       (dolist (elem gnus-article-mark-lists)
4875         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4876           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4877           (when (symbol-value var)
4878             ;; This list has articles.  So we delete all missing
4879             ;; articles from it.
4880             (setq m missing)
4881             (while m
4882               (set var (delq (pop m) (symbol-value var))))))))))
4883
4884 (defun gnus-update-marks ()
4885   "Enter the various lists of marked articles into the newsgroup info list."
4886   (let ((types gnus-article-mark-lists)
4887         (info (gnus-get-info gnus-newsgroup-name))
4888         (uncompressed '(score bookmark killed seen))
4889         type list newmarked symbol delta-marks)
4890     (when info
4891       ;; Add all marks lists to the list of marks lists.
4892       (while (setq type (pop types))
4893         (setq list (symbol-value
4894                     (setq symbol
4895                           (intern (format "gnus-newsgroup-%s" (car type))))))
4896
4897         (when list
4898           ;; Get rid of the entries of the articles that have the
4899           ;; default score.
4900           (when (and (eq (cdr type) 'score)
4901                      gnus-save-score
4902                      list)
4903             (let* ((arts list)
4904                    (prev (cons nil list))
4905                    (all prev))
4906               (while arts
4907                 (if (or (not (consp (car arts)))
4908                         (= (cdar arts) gnus-summary-default-score))
4909                     (setcdr prev (cdr arts))
4910                   (setq prev arts))
4911                 (setq arts (cdr arts)))
4912               (setq list (cdr all)))))
4913
4914         (when (eq (cdr type) 'seen)
4915           (setq list
4916                 (if list
4917                     (gnus-add-to-range list gnus-newsgroup-unseen)
4918                   (gnus-compress-sequence gnus-newsgroup-articles))))
4919
4920         (unless (memq (cdr type) uncompressed)
4921           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4922
4923         (when (gnus-check-backend-function
4924                'request-set-mark gnus-newsgroup-name)
4925           ;; propagate flags to server, with the following exceptions:
4926           ;; uncompressed:s are not proper flags (they are cons cells)
4927           ;; cache is a internal gnus flag
4928           ;; download are local to one gnus installation (well)
4929           ;; unsend are for nndraft groups only
4930           ;; xxx: generality of this?  this suits nnimap anyway
4931           (unless (memq (cdr type) (append '(cache download unsend)
4932                                            uncompressed))
4933             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4934                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4935                    (add (gnus-remove-from-range
4936                          (gnus-copy-sequence list) old)))
4937               (when add
4938                 (push (list add 'add (list (cdr type))) delta-marks))
4939               (when del
4940                 (push (list del 'del (list (cdr type))) delta-marks)))))
4941
4942         (when list
4943           (push (cons (cdr type) list) newmarked)))
4944
4945       (when delta-marks
4946         (unless (gnus-check-group gnus-newsgroup-name)
4947           (error "Can't open server for %s" gnus-newsgroup-name))
4948         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4949
4950       ;; Enter these new marks into the info of the group.
4951       (if (nthcdr 3 info)
4952           (setcar (nthcdr 3 info) newmarked)
4953         ;; Add the marks lists to the end of the info.
4954         (when newmarked
4955           (setcdr (nthcdr 2 info) (list newmarked))))
4956
4957       ;; Cut off the end of the info if there's nothing else there.
4958       (let ((i 5))
4959         (while (and (> i 2)
4960                     (not (nth i info)))
4961           (when (nthcdr (decf i) info)
4962             (setcdr (nthcdr i info) nil)))))))
4963
4964 (defun gnus-set-mode-line (where)
4965   "Set the mode line of the article or summary buffers.
4966 If WHERE is `summary', the summary mode line format will be used."
4967   ;; Is this mode line one we keep updated?
4968   (when (and (memq where gnus-updated-mode-lines)
4969              (symbol-value
4970               (intern (format "gnus-%s-mode-line-format-spec" where))))
4971     (let (mode-string)
4972       (save-excursion
4973         ;; We evaluate this in the summary buffer since these
4974         ;; variables are buffer-local to that buffer.
4975         (set-buffer gnus-summary-buffer)
4976         ;; We bind all these variables that are used in the `eval' form
4977         ;; below.
4978         (let* ((mformat (symbol-value
4979                          (intern
4980                           (format "gnus-%s-mode-line-format-spec" where))))
4981                (gnus-tmp-group-name (gnus-group-decoded-name
4982                                      gnus-newsgroup-name))
4983                (gnus-tmp-article-number (or gnus-current-article 0))
4984                (gnus-tmp-unread gnus-newsgroup-unreads)
4985                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4986                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4987                (gnus-tmp-unread-and-unselected
4988                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4989                             (zerop gnus-tmp-unselected))
4990                        "")
4991                       ((zerop gnus-tmp-unselected)
4992                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4993                       (t (format "{%d(+%d) more}"
4994                                  gnus-tmp-unread-and-unticked
4995                                  gnus-tmp-unselected))))
4996                (gnus-tmp-subject
4997                 (if (and gnus-current-headers
4998                          (vectorp gnus-current-headers))
4999                     (gnus-mode-string-quote
5000                      (mail-header-subject gnus-current-headers))
5001                   ""))
5002                bufname-length max-len
5003                gnus-tmp-header);; passed as argument to any user-format-funcs
5004           (setq mode-string (eval mformat))
5005           (setq bufname-length (if (string-match "%b" mode-string)
5006                                    (- (length
5007                                        (buffer-name
5008                                         (if (eq where 'summary)
5009                                             nil
5010                                           (get-buffer gnus-article-buffer))))
5011                                       2)
5012                                  0))
5013           (setq max-len (max 4 (if gnus-mode-non-string-length
5014                                    (- (window-width)
5015                                       gnus-mode-non-string-length
5016                                       bufname-length)
5017                                  (length mode-string))))
5018           ;; We might have to chop a bit of the string off...
5019           (when (> (length mode-string) max-len)
5020             (setq mode-string
5021                   (concat (gnus-truncate-string mode-string (- max-len 3))
5022                           "...")))
5023           ;; Pad the mode string a bit.
5024           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5025       ;; Update the mode line.
5026       (setq mode-line-buffer-identification
5027             (gnus-mode-line-buffer-identification (list mode-string)))
5028       (set-buffer-modified-p t))))
5029
5030 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5031   "Go through the HEADERS list and add all Xrefs to a hash table.
5032 The resulting hash table is returned, or nil if no Xrefs were found."
5033   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5034          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5035          (xref-hashtb (gnus-make-hashtable))
5036          start group entry number xrefs header)
5037     (while headers
5038       (setq header (pop headers))
5039       (when (and (setq xrefs (mail-header-xref header))
5040                  (not (memq (setq number (mail-header-number header))
5041                             unreads)))
5042         (setq start 0)
5043         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5044           (setq start (match-end 0))
5045           (setq group (if prefix
5046                           (concat prefix (substring xrefs (match-beginning 1)
5047                                                     (match-end 1)))
5048                         (substring xrefs (match-beginning 1) (match-end 1))))
5049           (setq number
5050                 (string-to-int (substring xrefs (match-beginning 2)
5051                                           (match-end 2))))
5052           (if (setq entry (gnus-gethash group xref-hashtb))
5053               (setcdr entry (cons number (cdr entry)))
5054             (gnus-sethash group (cons number nil) xref-hashtb)))))
5055     (and start xref-hashtb)))
5056
5057 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5058   "Look through all the headers and mark the Xrefs as read."
5059   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5060         name entry info xref-hashtb idlist method nth4)
5061     (save-excursion
5062       (set-buffer gnus-group-buffer)
5063       (when (setq xref-hashtb
5064                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5065         (mapatoms
5066          (lambda (group)
5067            (unless (string= from-newsgroup (setq name (symbol-name group)))
5068              (setq idlist (symbol-value group))
5069              ;; Dead groups are not updated.
5070              (and (prog1
5071                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5072                             info (nth 2 entry))
5073                     (when (stringp (setq nth4 (gnus-info-method info)))
5074                       (setq nth4 (gnus-server-to-method nth4))))
5075                   ;; Only do the xrefs if the group has the same
5076                   ;; select method as the group we have just read.
5077                   (or (gnus-methods-equal-p
5078                        nth4 (gnus-find-method-for-group from-newsgroup))
5079                       virtual
5080                       (equal nth4 (setq method (gnus-find-method-for-group
5081                                                 from-newsgroup)))
5082                       (and (equal (car nth4) (car method))
5083                            (equal (nth 1 nth4) (nth 1 method))))
5084                   gnus-use-cross-reference
5085                   (or (not (eq gnus-use-cross-reference t))
5086                       virtual
5087                       ;; Only do cross-references on subscribed
5088                       ;; groups, if that is what is wanted.
5089                       (<= (gnus-info-level info) gnus-level-subscribed))
5090                   (gnus-group-make-articles-read name idlist))))
5091          xref-hashtb)))))
5092
5093 (defun gnus-compute-read-articles (group articles)
5094   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5095          (info (nth 2 entry))
5096          (active (gnus-active group))
5097          ninfo)
5098     (when entry
5099       ;; First peel off all invalid article numbers.
5100       (when active
5101         (let ((ids articles)
5102               id first)
5103           (while (setq id (pop ids))
5104             (when (and first (> id (cdr active)))
5105               ;; We'll end up in this situation in one particular
5106               ;; obscure situation.  If you re-scan a group and get
5107               ;; a new article that is cross-posted to a different
5108               ;; group that has not been re-scanned, you might get
5109               ;; crossposted article that has a higher number than
5110               ;; Gnus believes possible.  So we re-activate this
5111               ;; group as well.  This might mean doing the
5112               ;; crossposting thingy will *increase* the number
5113               ;; of articles in some groups.  Tsk, tsk.
5114               (setq active (or (gnus-activate-group group) active)))
5115             (when (or (> id (cdr active))
5116                       (< id (car active)))
5117               (setq articles (delq id articles))))))
5118       ;; If the read list is nil, we init it.
5119       (if (and active
5120                (null (gnus-info-read info))
5121                (> (car active) 1))
5122           (setq ninfo (cons 1 (1- (car active))))
5123         (setq ninfo (gnus-info-read info)))
5124       ;; Then we add the read articles to the range.
5125       (gnus-add-to-range
5126        ninfo (setq articles (sort articles '<))))))
5127
5128 (defun gnus-group-make-articles-read (group articles)
5129   "Update the info of GROUP to say that ARTICLES are read."
5130   (let* ((num 0)
5131          (entry (gnus-gethash group gnus-newsrc-hashtb))
5132          (info (nth 2 entry))
5133          (active (gnus-active group))
5134          range)
5135     (when entry
5136       (setq range (gnus-compute-read-articles group articles))
5137       (save-excursion
5138         (set-buffer gnus-group-buffer)
5139         (gnus-undo-register
5140           `(progn
5141              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5142              (gnus-info-set-read ',info ',(gnus-info-read info))
5143              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5144              (gnus-group-update-group ,group t))))
5145       ;; Add the read articles to the range.
5146       (gnus-info-set-read info range)
5147       (gnus-request-set-mark group (list (list range 'add '(read))))
5148       ;; Then we have to re-compute how many unread
5149       ;; articles there are in this group.
5150       (when active
5151         (cond
5152          ((not range)
5153           (setq num (- (1+ (cdr active)) (car active))))
5154          ((not (listp (cdr range)))
5155           (setq num (- (cdr active) (- (1+ (cdr range))
5156                                        (car range)))))
5157          (t
5158           (while range
5159             (if (numberp (car range))
5160                 (setq num (1+ num))
5161               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5162             (setq range (cdr range)))
5163           (setq num (- (cdr active) num))))
5164         ;; Update the number of unread articles.
5165         (setcar entry num)
5166         ;; Update the group buffer.
5167         (gnus-group-update-group group t)))))
5168
5169 (defvar gnus-newsgroup-none-id 0)
5170
5171 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5172   (let ((cur nntp-server-buffer)
5173         (dependencies
5174          (or dependencies
5175              (save-excursion (set-buffer gnus-summary-buffer)
5176                              gnus-newsgroup-dependencies)))
5177         headers id end ref
5178         (mail-parse-charset gnus-newsgroup-charset)
5179         (mail-parse-ignored-charsets
5180          (save-excursion (condition-case nil
5181                              (set-buffer gnus-summary-buffer)
5182                            (error))
5183                          gnus-newsgroup-ignored-charsets)))
5184     (save-excursion
5185       (set-buffer nntp-server-buffer)
5186       ;; Translate all TAB characters into SPACE characters.
5187       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5188       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5189       (gnus-run-hooks 'gnus-parse-headers-hook)
5190       (let ((case-fold-search t)
5191             in-reply-to header p lines chars ctype)
5192         (goto-char (point-min))
5193         ;; Search to the beginning of the next header.  Error messages
5194         ;; do not begin with 2 or 3.
5195         (while (re-search-forward "^[23][0-9]+ " nil t)
5196           (setq id nil
5197                 ref nil)
5198           ;; This implementation of this function, with nine
5199           ;; search-forwards instead of the one re-search-forward and
5200           ;; a case (which basically was the old function) is actually
5201           ;; about twice as fast, even though it looks messier.  You
5202           ;; can't have everything, I guess.  Speed and elegance
5203           ;; doesn't always go hand in hand.
5204           (setq
5205            header
5206            (make-full-mail-header
5207             ;; Number.
5208             (prog1
5209                 (read cur)
5210               (end-of-line)
5211               (setq p (point))
5212               (narrow-to-region (point)
5213                                 (or (and (search-forward "\n.\n" nil t)
5214                                          (- (point) 2))
5215                                     (point))))
5216             ;; Subject.
5217             (progn
5218               (goto-char p)
5219               (if (search-forward "\nsubject:" nil t)
5220                   (nnheader-header-value)
5221                 "(none)"))
5222             ;; From.
5223             (progn
5224               (goto-char p)
5225               (if (search-forward "\nfrom:" nil t)
5226                   (nnheader-header-value)
5227                 "(nobody)"))
5228             ;; Date.
5229             (progn
5230               (goto-char p)
5231               (if (search-forward "\ndate:" nil t)
5232                   (nnheader-header-value) ""))
5233             ;; Message-ID.
5234             (progn
5235               (goto-char p)
5236               (setq id (if (re-search-forward
5237                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5238                            ;; We do it this way to make sure the Message-ID
5239                            ;; is (somewhat) syntactically valid.
5240                            (buffer-substring (match-beginning 1)
5241                                              (match-end 1))
5242                          ;; If there was no message-id, we just fake one
5243                          ;; to make subsequent routines simpler.
5244                          (nnheader-generate-fake-message-id))))
5245             ;; References.
5246             (progn
5247               (goto-char p)
5248               (if (search-forward "\nreferences:" nil t)
5249                   (progn
5250                     (setq end (point))
5251                     (prog1
5252                         (nnheader-header-value)
5253                       (setq ref
5254                             (buffer-substring
5255                              (progn
5256                                ;; (end-of-line)
5257                                (search-backward ">" end t)
5258                                (1+ (point)))
5259                              (progn
5260                                (search-backward "<" end t)
5261                                (point))))))
5262                 ;; Get the references from the in-reply-to header if there
5263                 ;; were no references and the in-reply-to header looks
5264                 ;; promising.
5265                 (if (and (search-forward "\nin-reply-to:" nil t)
5266                          (setq in-reply-to (nnheader-header-value))
5267                          (string-match "<[^>]+>" in-reply-to))
5268                     (let (ref2)
5269                       (setq ref (substring in-reply-to (match-beginning 0)
5270                                            (match-end 0)))
5271                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5272                         (setq ref2 (substring in-reply-to (match-beginning 0)
5273                                               (match-end 0)))
5274                         (when (> (length ref2) (length ref))
5275                           (setq ref ref2)))
5276                       ref)
5277                   (setq ref nil))))
5278             ;; Chars.
5279             (progn
5280               (goto-char p)
5281               (if (search-forward "\nchars: " nil t)
5282                   (if (numberp (setq chars (ignore-errors (read cur))))
5283                       chars -1)
5284                 -1))
5285             ;; Lines.
5286             (progn
5287               (goto-char p)
5288               (if (search-forward "\nlines: " nil t)
5289                   (if (numberp (setq lines (ignore-errors (read cur))))
5290                       lines -1)
5291                 -1))
5292             ;; Xref.
5293             (progn
5294               (goto-char p)
5295               (and (search-forward "\nxref:" nil t)
5296                    (nnheader-header-value)))
5297             ;; Extra.
5298             (when gnus-extra-headers
5299               (let ((extra gnus-extra-headers)
5300                     out)
5301                 (while extra
5302                   (goto-char p)
5303                   (when (search-forward
5304                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5305                     (push (cons (car extra) (nnheader-header-value)) out))
5306                   (pop extra))
5307                 out))))
5308           (goto-char p)
5309           (if (and (search-forward "\ncontent-type: " nil t)
5310                    (setq ctype (nnheader-header-value)))
5311               (mime-entity-set-content-type-internal
5312                header (mime-parse-Content-Type ctype)))
5313           (when (equal id ref)
5314             (setq ref nil))
5315
5316           (when gnus-alter-header-function
5317             (funcall gnus-alter-header-function header)
5318             (setq id (mail-header-id header)
5319                   ref (gnus-parent-id (mail-header-references header))))
5320
5321           (when (setq header
5322                       (gnus-dependencies-add-header
5323                        header dependencies force-new))
5324             (push header headers))
5325           (goto-char (point-max))
5326           (widen))
5327         (nreverse headers)))))
5328
5329 ;; Goes through the xover lines and returns a list of vectors
5330 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5331                                                   force-new dependencies
5332                                                   group also-fetch-heads)
5333   "Parse the news overview data in the server buffer.
5334 Return a list of headers that match SEQUENCE (see
5335 `nntp-retrieve-headers')."
5336   ;; Get the Xref when the users reads the articles since most/some
5337   ;; NNTP servers do not include Xrefs when using XOVER.
5338   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5339   (let ((mail-parse-charset gnus-newsgroup-charset)
5340         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5341         (cur nntp-server-buffer)
5342         (dependencies (or dependencies gnus-newsgroup-dependencies))
5343         (allp (cond
5344                ((eq gnus-read-all-available-headers t)
5345                 t)
5346                ((stringp gnus-read-all-available-headers)
5347                 (string-match gnus-read-all-available-headers group))
5348                (t
5349                 nil)))
5350         number headers header)
5351     (save-excursion
5352       (set-buffer nntp-server-buffer)
5353       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5354       ;; Allow the user to mangle the headers before parsing them.
5355       (gnus-run-hooks 'gnus-parse-headers-hook)
5356       (goto-char (point-min))
5357       (while (not (eobp))
5358         (condition-case ()
5359             (while (and (or sequence allp)
5360                         (not (eobp)))
5361               (setq number (read cur))
5362               (when (not allp)
5363                 (while (and sequence
5364                             (< (car sequence) number))
5365                   (setq sequence (cdr sequence))))
5366               (when (and (or allp
5367                              (and sequence
5368                                   (eq number (car sequence))))
5369                          (progn
5370                            (setq sequence (cdr sequence))
5371                            (setq header (inline
5372                                           (gnus-nov-parse-line
5373                                            number dependencies force-new)))))
5374                 (push header headers))
5375               (forward-line 1))
5376           (error
5377            (gnus-error 4 "Strange nov line (%d)"
5378                        (count-lines (point-min) (point)))))
5379         (forward-line 1))
5380       ;; A common bug in inn is that if you have posted an article and
5381       ;; then retrieves the active file, it will answer correctly --
5382       ;; the new article is included.  However, a NOV entry for the
5383       ;; article may not have been generated yet, so this may fail.
5384       ;; We work around this problem by retrieving the last few
5385       ;; headers using HEAD.
5386       (if (or (not also-fetch-heads)
5387               (not sequence))
5388           ;; We (probably) got all the headers.
5389           (nreverse headers)
5390         (let ((gnus-nov-is-evil t))
5391           (nconc
5392            (nreverse headers)
5393            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5394              (gnus-get-newsgroup-headers))))))))
5395
5396 (defun gnus-article-get-xrefs ()
5397   "Fill in the Xref value in `gnus-current-headers', if necessary.
5398 This is meant to be called in `gnus-article-internal-prepare-hook'."
5399   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5400                                  gnus-current-headers)))
5401     (or (not gnus-use-cross-reference)
5402         (not headers)
5403         (and (mail-header-xref headers)
5404              (not (string= (mail-header-xref headers) "")))
5405         (let ((case-fold-search t)
5406               xref)
5407           (save-restriction
5408             (nnheader-narrow-to-headers)
5409             (goto-char (point-min))
5410             (when (or (and (not (eobp))
5411                            (eq (downcase (char-after)) ?x)
5412                            (looking-at "Xref:"))
5413                       (search-forward "\nXref:" nil t))
5414               (goto-char (1+ (match-end 0)))
5415               (setq xref (buffer-substring (point)
5416                                            (progn (end-of-line) (point))))
5417               (mail-header-set-xref headers xref)))))))
5418
5419 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5420   "Find article ID and insert the summary line for that article.
5421 OLD-HEADER can either be a header or a line number to insert
5422 the subject line on."
5423   (let* ((line (and (numberp old-header) old-header))
5424          (old-header (and (vectorp old-header) old-header))
5425          (header (cond ((and old-header use-old-header)
5426                         old-header)
5427                        ((and (numberp id)
5428                              (gnus-number-to-header id))
5429                         (gnus-number-to-header id))
5430                        (t
5431                         (gnus-read-header id))))
5432          (number (and (numberp id) id))
5433          d)
5434     (when header
5435       ;; Rebuild the thread that this article is part of and go to the
5436       ;; article we have fetched.
5437       (when (and (not gnus-show-threads)
5438                  old-header)
5439         (when (and number
5440                    (setq d (gnus-data-find (mail-header-number old-header))))
5441           (goto-char (gnus-data-pos d))
5442           (gnus-data-remove
5443            number
5444            (- (gnus-point-at-bol)
5445               (prog1
5446                   (1+ (gnus-point-at-eol))
5447                 (gnus-delete-line))))))
5448       (when old-header
5449         (mail-header-set-number header (mail-header-number old-header)))
5450       (setq gnus-newsgroup-sparse
5451             (delq (setq number (mail-header-number header))
5452                   gnus-newsgroup-sparse))
5453       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5454       (push number gnus-newsgroup-limit)
5455       (gnus-rebuild-thread (mail-header-id header) line)
5456       (gnus-summary-goto-subject number nil t))
5457     (when (and (numberp number)
5458                (> number 0))
5459       ;; We have to update the boundaries even if we can't fetch the
5460       ;; article if ID is a number -- so that the next `P' or `N'
5461       ;; command will fetch the previous (or next) article even
5462       ;; if the one we tried to fetch this time has been canceled.
5463       (when (> number gnus-newsgroup-end)
5464         (setq gnus-newsgroup-end number))
5465       (when (< number gnus-newsgroup-begin)
5466         (setq gnus-newsgroup-begin number))
5467       (setq gnus-newsgroup-unselected
5468             (delq number gnus-newsgroup-unselected)))
5469     ;; Report back a success?
5470     (and header (mail-header-number header))))
5471
5472 ;;; Process/prefix in the summary buffer
5473
5474 (defun gnus-summary-work-articles (n)
5475   "Return a list of articles to be worked upon.
5476 The prefix argument, the list of process marked articles, and the
5477 current article will be taken into consideration."
5478   (save-excursion
5479     (set-buffer gnus-summary-buffer)
5480     (cond
5481      (n
5482       ;; A numerical prefix has been given.
5483       (setq n (prefix-numeric-value n))
5484       (let ((backward (< n 0))
5485             (n (abs (prefix-numeric-value n)))
5486             articles article)
5487         (save-excursion
5488           (while
5489               (and (> n 0)
5490                    (push (setq article (gnus-summary-article-number))
5491                          articles)
5492                    (if backward
5493                        (gnus-summary-find-prev nil article)
5494                      (gnus-summary-find-next nil article)))
5495             (decf n)))
5496         (nreverse articles)))
5497      ((and (gnus-region-active-p) (mark))
5498       (message "region active")
5499       ;; Work on the region between point and mark.
5500       (let ((max (max (point) (mark)))
5501             articles article)
5502         (save-excursion
5503           (goto-char (min (point) (mark)))
5504           (while
5505               (and
5506                (push (setq article (gnus-summary-article-number)) articles)
5507                (gnus-summary-find-next nil article)
5508                (< (point) max)))
5509           (nreverse articles))))
5510      (gnus-newsgroup-processable
5511       ;; There are process-marked articles present.
5512       ;; Save current state.
5513       (gnus-summary-save-process-mark)
5514       ;; Return the list.
5515       (reverse gnus-newsgroup-processable))
5516      (t
5517       ;; Just return the current article.
5518       (list (gnus-summary-article-number))))))
5519
5520 (defmacro gnus-summary-iterate (arg &rest forms)
5521   "Iterate over the process/prefixed articles and do FORMS.
5522 ARG is the interactive prefix given to the command.  FORMS will be
5523 executed with point over the summary line of the articles."
5524   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5525     `(let ((,articles (gnus-summary-work-articles ,arg)))
5526        (while ,articles
5527          (gnus-summary-goto-subject (car ,articles))
5528          ,@forms
5529          (pop ,articles)))))
5530
5531 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5532 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5533
5534 (defun gnus-summary-save-process-mark ()
5535   "Push the current set of process marked articles on the stack."
5536   (interactive)
5537   (push (copy-sequence gnus-newsgroup-processable)
5538         gnus-newsgroup-process-stack))
5539
5540 (defun gnus-summary-kill-process-mark ()
5541   "Push the current set of process marked articles on the stack and unmark."
5542   (interactive)
5543   (gnus-summary-save-process-mark)
5544   (gnus-summary-unmark-all-processable))
5545
5546 (defun gnus-summary-yank-process-mark ()
5547   "Pop the last process mark state off the stack and restore it."
5548   (interactive)
5549   (unless gnus-newsgroup-process-stack
5550     (error "Empty mark stack"))
5551   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5552
5553 (defun gnus-summary-process-mark-set (set)
5554   "Make SET into the current process marked articles."
5555   (gnus-summary-unmark-all-processable)
5556   (while set
5557     (gnus-summary-set-process-mark (pop set))))
5558
5559 ;;; Searching and stuff
5560
5561 (defun gnus-summary-search-group (&optional backward use-level)
5562   "Search for next unread newsgroup.
5563 If optional argument BACKWARD is non-nil, search backward instead."
5564   (save-excursion
5565     (set-buffer gnus-group-buffer)
5566     (when (gnus-group-search-forward
5567            backward nil (if use-level (gnus-group-group-level) nil))
5568       (gnus-group-group-name))))
5569
5570 (defun gnus-summary-best-group (&optional exclude-group)
5571   "Find the name of the best unread group.
5572 If EXCLUDE-GROUP, do not go to this group."
5573   (save-excursion
5574     (set-buffer gnus-group-buffer)
5575     (save-excursion
5576       (gnus-group-best-unread-group exclude-group))))
5577
5578 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5579   (if backward (gnus-summary-find-prev)
5580     (let* ((dummy (gnus-summary-article-intangible-p))
5581            (article (or article (gnus-summary-article-number)))
5582            (arts (gnus-data-find-list article))
5583            result)
5584       (when (and (not dummy)
5585                  (or (not gnus-summary-check-current)
5586                      (not unread)
5587                      (not (gnus-data-unread-p (car arts)))))
5588         (setq arts (cdr arts)))
5589       (when (setq result
5590                   (if unread
5591                       (progn
5592                         (while arts
5593                           (when (or (and undownloaded
5594                                          (eq gnus-undownloaded-mark
5595                                              (gnus-data-mark (car arts))))
5596                                     (gnus-data-unread-p (car arts)))
5597                             (setq result (car arts)
5598                                   arts nil))
5599                           (setq arts (cdr arts)))
5600                         result)
5601                     (car arts)))
5602         (goto-char (gnus-data-pos result))
5603         (gnus-data-number result)))))
5604
5605 (defun gnus-summary-find-prev (&optional unread article)
5606   (let* ((eobp (eobp))
5607          (article (or article (gnus-summary-article-number)))
5608          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5609          result)
5610     (when (and (not eobp)
5611                (or (not gnus-summary-check-current)
5612                    (not unread)
5613                    (not (gnus-data-unread-p (car arts)))))
5614       (setq arts (cdr arts)))
5615     (when (setq result
5616                 (if unread
5617                     (progn
5618                       (while arts
5619                         (when (gnus-data-unread-p (car arts))
5620                           (setq result (car arts)
5621                                 arts nil))
5622                         (setq arts (cdr arts)))
5623                       result)
5624                   (car arts)))
5625       (goto-char (gnus-data-pos result))
5626       (gnus-data-number result))))
5627
5628 (defun gnus-summary-find-subject (subject &optional unread backward article)
5629   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5630          (article (or article (gnus-summary-article-number)))
5631          (articles (gnus-data-list backward))
5632          (arts (gnus-data-find-list article articles))
5633          result)
5634     (when (or (not gnus-summary-check-current)
5635               (not unread)
5636               (not (gnus-data-unread-p (car arts))))
5637       (setq arts (cdr arts)))
5638     (while arts
5639       (and (or (not unread)
5640                (gnus-data-unread-p (car arts)))
5641            (vectorp (gnus-data-header (car arts)))
5642            (gnus-subject-equal
5643             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5644            (setq result (car arts)
5645                  arts nil))
5646       (setq arts (cdr arts)))
5647     (and result
5648          (goto-char (gnus-data-pos result))
5649          (gnus-data-number result))))
5650
5651 (defun gnus-summary-search-forward (&optional unread subject backward)
5652   "Search forward for an article.
5653 If UNREAD, look for unread articles.  If SUBJECT, look for
5654 articles with that subject.  If BACKWARD, search backward instead."
5655   (cond (subject (gnus-summary-find-subject subject unread backward))
5656         (backward (gnus-summary-find-prev unread))
5657         (t (gnus-summary-find-next unread))))
5658
5659 (defun gnus-recenter (&optional n)
5660   "Center point in window and redisplay frame.
5661 Also do horizontal recentering."
5662   (interactive "P")
5663   (when (and gnus-auto-center-summary
5664              (not (eq gnus-auto-center-summary 'vertical)))
5665     (gnus-horizontal-recenter))
5666   (recenter n))
5667
5668 (defun gnus-summary-recenter ()
5669   "Center point in the summary window.
5670 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5671 displayed, no centering will be performed."
5672   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5673   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5674   (interactive)
5675   (let* ((top (cond ((< (window-height) 4) 0)
5676                     ((< (window-height) 7) 1)
5677                     (t (if (numberp gnus-auto-center-summary)
5678                            gnus-auto-center-summary
5679                          2))))
5680          (height (1- (window-height)))
5681          (bottom (save-excursion (goto-char (point-max))
5682                                  (forward-line (- height))
5683                                  (point)))
5684          (window (get-buffer-window (current-buffer))))
5685     ;; The user has to want it.
5686     (when gnus-auto-center-summary
5687       (when (get-buffer-window gnus-article-buffer)
5688         ;; Only do recentering when the article buffer is displayed,
5689         ;; Set the window start to either `bottom', which is the biggest
5690         ;; possible valid number, or the second line from the top,
5691         ;; whichever is the least.
5692         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5693           (if (> bottom top-pos)
5694               ;; Keep the second line from the top visible
5695               (set-window-start window top-pos t)
5696             ;; Try to keep the bottom line visible; if it's partially
5697             ;; obscured, either scroll one more line to make it fully
5698             ;; visible, or revert to using TOP-POS.
5699             (save-excursion
5700               (goto-char (point-max))
5701               (forward-line -1)
5702               (let ((last-line-start (point)))
5703                 (goto-char bottom)
5704                 (set-window-start window (point) t)
5705                 (when (not (pos-visible-in-window-p last-line-start window))
5706                   (forward-line 1)
5707                   (set-window-start window (min (point) top-pos) t)))))))
5708       ;; Do horizontal recentering while we're at it.
5709       (when (and (get-buffer-window (current-buffer) t)
5710                  (not (eq gnus-auto-center-summary 'vertical)))
5711         (let ((selected (selected-window)))
5712           (select-window (get-buffer-window (current-buffer) t))
5713           (gnus-summary-position-point)
5714           (gnus-horizontal-recenter)
5715           (select-window selected))))))
5716
5717 (defun gnus-summary-jump-to-group (newsgroup)
5718   "Move point to NEWSGROUP in group mode buffer."
5719   ;; Keep update point of group mode buffer if visible.
5720   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5721       (save-window-excursion
5722         ;; Take care of tree window mode.
5723         (when (get-buffer-window gnus-group-buffer)
5724           (pop-to-buffer gnus-group-buffer))
5725         (gnus-group-jump-to-group newsgroup))
5726     (save-excursion
5727       ;; Take care of tree window mode.
5728       (if (get-buffer-window gnus-group-buffer)
5729           (pop-to-buffer gnus-group-buffer)
5730         (set-buffer gnus-group-buffer))
5731       (gnus-group-jump-to-group newsgroup))))
5732
5733 ;; This function returns a list of article numbers based on the
5734 ;; difference between the ranges of read articles in this group and
5735 ;; the range of active articles.
5736 (defun gnus-list-of-unread-articles (group)
5737   (let* ((read (gnus-info-read (gnus-get-info group)))
5738          (active (or (gnus-active group) (gnus-activate-group group)))
5739          (last (cdr active))
5740          first nlast unread)
5741     ;; If none are read, then all are unread.
5742     (if (not read)
5743         (setq first (car active))
5744       ;; If the range of read articles is a single range, then the
5745       ;; first unread article is the article after the last read
5746       ;; article.  Sounds logical, doesn't it?
5747       (if (and (not (listp (cdr read)))
5748                (or (< (car read) (car active))
5749                    (progn (setq read (list read))
5750                           nil)))
5751           (setq first (max (car active) (1+ (cdr read))))
5752         ;; `read' is a list of ranges.
5753         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5754                                   (caar read)))
5755                   1)
5756           (setq first (car active)))
5757         (while read
5758           (when first
5759             (while (< first nlast)
5760               (push first unread)
5761               (setq first (1+ first))))
5762           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5763           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5764           (setq read (cdr read)))))
5765     ;; And add the last unread articles.
5766     (while (<= first last)
5767       (push first unread)
5768       (setq first (1+ first)))
5769     ;; Return the list of unread articles.
5770     (delq 0 (nreverse unread))))
5771
5772 (defun gnus-list-of-read-articles (group)
5773   "Return a list of unread, unticked and non-dormant articles."
5774   (let* ((info (gnus-get-info group))
5775          (marked (gnus-info-marks info))
5776          (active (gnus-active group)))
5777     (and info active
5778          (gnus-set-difference
5779           (gnus-sorted-complement
5780            (gnus-uncompress-range active)
5781            (gnus-list-of-unread-articles group))
5782           (append
5783            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5784            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5785
5786 ;; Various summary commands
5787
5788 (defun gnus-summary-select-article-buffer ()
5789   "Reconfigure windows to show article buffer."
5790   (interactive)
5791   (if (not (gnus-buffer-live-p gnus-article-buffer))
5792       (error "There is no article buffer for this summary buffer")
5793     (gnus-configure-windows 'article)
5794     (select-window (get-buffer-window gnus-article-buffer))))
5795
5796 (defun gnus-summary-universal-argument (arg)
5797   "Perform any operation on all articles that are process/prefixed."
5798   (interactive "P")
5799   (let ((articles (gnus-summary-work-articles arg))
5800         func article)
5801     (if (eq
5802          (setq
5803           func
5804           (key-binding
5805            (read-key-sequence
5806             (substitute-command-keys
5807              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5808          'undefined)
5809         (gnus-error 1 "Undefined key")
5810       (save-excursion
5811         (while articles
5812           (gnus-summary-goto-subject (setq article (pop articles)))
5813           (let (gnus-newsgroup-processable)
5814             (command-execute func))
5815           (gnus-summary-remove-process-mark article)))))
5816   (gnus-summary-position-point))
5817
5818 (defun gnus-summary-toggle-truncation (&optional arg)
5819   "Toggle truncation of summary lines.
5820 With arg, turn line truncation on iff arg is positive."
5821   (interactive "P")
5822   (setq truncate-lines
5823         (if (null arg) (not truncate-lines)
5824           (> (prefix-numeric-value arg) 0)))
5825   (redraw-display))
5826
5827 (defun gnus-summary-reselect-current-group (&optional all rescan)
5828   "Exit and then reselect the current newsgroup.
5829 The prefix argument ALL means to select all articles."
5830   (interactive "P")
5831   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5832     (error "Ephemeral groups can't be reselected"))
5833   (let ((current-subject (gnus-summary-article-number))
5834         (group gnus-newsgroup-name))
5835     (setq gnus-newsgroup-begin nil)
5836     (gnus-summary-exit)
5837     ;; We have to adjust the point of group mode buffer because
5838     ;; point was moved to the next unread newsgroup by exiting.
5839     (gnus-summary-jump-to-group group)
5840     (when rescan
5841       (save-excursion
5842         (save-window-excursion
5843           ;; Don't show group contents.
5844           (set-window-start (selected-window) (point-max))
5845           (gnus-group-get-new-news-this-group 1))))
5846     (gnus-group-read-group all t)
5847     (gnus-summary-goto-subject current-subject nil t)))
5848
5849 (defun gnus-summary-rescan-group (&optional all)
5850   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5851   (interactive "P")
5852   (gnus-summary-reselect-current-group all t))
5853
5854 (defun gnus-summary-update-info (&optional non-destructive)
5855   (save-excursion
5856     (let ((group gnus-newsgroup-name))
5857       (when group
5858         (when gnus-newsgroup-kill-headers
5859           (setq gnus-newsgroup-killed
5860                 (gnus-compress-sequence
5861                  (nconc
5862                   (gnus-set-sorted-intersection
5863                    (gnus-uncompress-range gnus-newsgroup-killed)
5864                    (setq gnus-newsgroup-unselected
5865                          (sort gnus-newsgroup-unselected '<)))
5866                   (setq gnus-newsgroup-unreads
5867                         (sort gnus-newsgroup-unreads '<)))
5868                  t)))
5869         (unless (listp (cdr gnus-newsgroup-killed))
5870           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5871         (let ((headers gnus-newsgroup-headers))
5872           ;; Set the new ranges of read articles.
5873           (save-excursion
5874             (set-buffer gnus-group-buffer)
5875             (gnus-undo-force-boundary))
5876           (gnus-update-read-articles
5877            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5878           ;; Set the current article marks.
5879           (let ((gnus-newsgroup-scored
5880                  (if (and (not gnus-save-score)
5881                           (not non-destructive))
5882                      nil
5883                    gnus-newsgroup-scored)))
5884             (save-excursion
5885               (gnus-update-marks)))
5886           ;; Do the cross-ref thing.
5887           (when gnus-use-cross-reference
5888             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5889           ;; Do not switch windows but change the buffer to work.
5890           (set-buffer gnus-group-buffer)
5891           (unless (gnus-ephemeral-group-p group)
5892             (gnus-group-update-group group)))))))
5893
5894 (defun gnus-summary-save-newsrc (&optional force)
5895   "Save the current number of read/marked articles in the dribble buffer.
5896 The dribble buffer will then be saved.
5897 If FORCE (the prefix), also save the .newsrc file(s)."
5898   (interactive "P")
5899   (gnus-summary-update-info t)
5900   (if force
5901       (gnus-save-newsrc-file)
5902     (gnus-dribble-save)))
5903
5904 (defun gnus-summary-exit (&optional temporary)
5905   "Exit reading current newsgroup, and then return to group selection mode.
5906 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5907   (interactive)
5908   (gnus-set-global-variables)
5909   (gnus-kill-save-kill-buffer)
5910   (gnus-async-halt-prefetch)
5911   (let* ((group gnus-newsgroup-name)
5912          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5913          (mode major-mode)
5914          (group-point nil)
5915          (buf (current-buffer)))
5916     (unless quit-config
5917       ;; Do adaptive scoring, and possibly save score files.
5918       (when gnus-newsgroup-adaptive
5919         (gnus-score-adaptive))
5920       (when gnus-use-scoring
5921         (gnus-score-save)))
5922     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5923     ;; If we have several article buffers, we kill them at exit.
5924     (unless gnus-single-article-buffer
5925       (gnus-kill-buffer gnus-original-article-buffer)
5926       (setq gnus-article-current nil))
5927     (when gnus-use-cache
5928       (gnus-cache-possibly-remove-articles)
5929       (gnus-cache-save-buffers))
5930     (gnus-async-prefetch-remove-group group)
5931     (when gnus-suppress-duplicates
5932       (gnus-dup-enter-articles))
5933     (when gnus-use-trees
5934       (gnus-tree-close group))
5935     (when gnus-use-cache
5936       (gnus-cache-write-active))
5937     ;; Remove entries for this group.
5938     (nnmail-purge-split-history (gnus-group-real-name group))
5939     ;; Make all changes in this group permanent.
5940     (unless quit-config
5941       (gnus-run-hooks 'gnus-exit-group-hook)
5942       (gnus-summary-update-info))
5943     (gnus-close-group group)
5944     ;; Make sure where we were, and go to next newsgroup.
5945     (set-buffer gnus-group-buffer)
5946     (unless quit-config
5947       (gnus-group-jump-to-group group))
5948     (gnus-run-hooks 'gnus-summary-exit-hook)
5949     (unless (or quit-config
5950                 ;; If this group has disappeared from the summary
5951                 ;; buffer, don't skip forwards.
5952                 (not (string= group (gnus-group-group-name))))
5953       (gnus-group-next-unread-group 1))
5954     (setq group-point (point))
5955     (if temporary
5956         nil                             ;Nothing to do.
5957       ;; If we have several article buffers, we kill them at exit.
5958       (unless gnus-single-article-buffer
5959         (gnus-kill-buffer gnus-article-buffer)
5960         (gnus-kill-buffer gnus-original-article-buffer)
5961         (setq gnus-article-current nil))
5962       (set-buffer buf)
5963       (if (not gnus-kill-summary-on-exit)
5964           (progn
5965             (gnus-deaden-summary)
5966             (setq mode nil))
5967         ;; We set all buffer-local variables to nil.  It is unclear why
5968         ;; this is needed, but if we don't, buffer-local variables are
5969         ;; not garbage-collected, it seems.  This would the lead to en
5970         ;; ever-growing Emacs.
5971         (gnus-summary-clear-local-variables)
5972         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5973           (gnus-summary-clear-local-variables))
5974         (when (get-buffer gnus-article-buffer)
5975           (bury-buffer gnus-article-buffer))
5976         ;; We clear the global counterparts of the buffer-local
5977         ;; variables as well, just to be on the safe side.
5978         (set-buffer gnus-group-buffer)
5979         (gnus-summary-clear-local-variables)
5980         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5981           (gnus-summary-clear-local-variables)))
5982       (setq gnus-current-select-method gnus-select-method)
5983       (pop-to-buffer gnus-group-buffer)
5984       (if (not quit-config)
5985           (progn
5986             (goto-char group-point)
5987             (gnus-configure-windows 'group 'force)
5988             (unless (pos-visible-in-window-p)
5989               (forward-line (/ (static-if (featurep 'xemacs)
5990                                    (window-displayed-height)
5991                                  (1- (window-height)))
5992                                -2))
5993               (set-window-start (selected-window) (point))
5994               (goto-char group-point)))
5995         (gnus-handle-ephemeral-exit quit-config))
5996       ;; Return to group mode buffer.
5997       (when (eq mode 'gnus-summary-mode)
5998         (gnus-kill-buffer buf))
5999       ;; Clear the current group name.
6000       (unless quit-config
6001         (setq gnus-newsgroup-name nil)))))
6002
6003 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6004 (defun gnus-summary-exit-no-update (&optional no-questions)
6005   "Quit reading current newsgroup without updating read article info."
6006   (interactive)
6007   (let* ((group gnus-newsgroup-name)
6008          (quit-config (gnus-group-quit-config group)))
6009     (when (or no-questions
6010               gnus-expert-user
6011               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6012       (gnus-async-halt-prefetch)
6013       (mapcar 'funcall
6014               (delq 'gnus-summary-expire-articles
6015                     (copy-sequence gnus-summary-prepare-exit-hook)))
6016       ;; If we have several article buffers, we kill them at exit.
6017       (unless gnus-single-article-buffer
6018         (gnus-kill-buffer gnus-article-buffer)
6019         (gnus-kill-buffer gnus-original-article-buffer)
6020         (setq gnus-article-current nil))
6021       (if (not gnus-kill-summary-on-exit)
6022           (gnus-deaden-summary)
6023         (gnus-close-group group)
6024         (gnus-summary-clear-local-variables)
6025         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6026           (gnus-summary-clear-local-variables))
6027         (set-buffer gnus-group-buffer)
6028         (gnus-summary-clear-local-variables)
6029         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6030           (gnus-summary-clear-local-variables))
6031         (when (get-buffer gnus-summary-buffer)
6032           (kill-buffer gnus-summary-buffer)))
6033       (unless gnus-single-article-buffer
6034         (setq gnus-article-current nil))
6035       (when gnus-use-trees
6036         (gnus-tree-close group))
6037       (gnus-async-prefetch-remove-group group)
6038       (when (get-buffer gnus-article-buffer)
6039         (bury-buffer gnus-article-buffer))
6040       ;; Return to the group buffer.
6041       (gnus-configure-windows 'group 'force)
6042       ;; Clear the current group name.
6043       (setq gnus-newsgroup-name nil)
6044       (when (equal (gnus-group-group-name) group)
6045         (gnus-group-next-unread-group 1))
6046       (when quit-config
6047         (gnus-handle-ephemeral-exit quit-config)))))
6048
6049 (defun gnus-handle-ephemeral-exit (quit-config)
6050   "Handle movement when leaving an ephemeral group.
6051 The state which existed when entering the ephemeral is reset."
6052   (if (not (buffer-name (car quit-config)))
6053       (gnus-configure-windows 'group 'force)
6054     (set-buffer (car quit-config))
6055     (cond ((eq major-mode 'gnus-summary-mode)
6056            (gnus-set-global-variables))
6057           ((eq major-mode 'gnus-article-mode)
6058            (save-excursion
6059              ;; The `gnus-summary-buffer' variable may point
6060              ;; to the old summary buffer when using a single
6061              ;; article buffer.
6062              (unless (gnus-buffer-live-p gnus-summary-buffer)
6063                (set-buffer gnus-group-buffer))
6064              (set-buffer gnus-summary-buffer)
6065              (gnus-set-global-variables))))
6066     (if (or (eq (cdr quit-config) 'article)
6067             (eq (cdr quit-config) 'pick))
6068         (progn
6069           ;; The current article may be from the ephemeral group
6070           ;; thus it is best that we reload this article
6071           (gnus-summary-show-article)
6072           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6073               (gnus-configure-windows 'pick 'force)
6074             (gnus-configure-windows (cdr quit-config) 'force)))
6075       (gnus-configure-windows (cdr quit-config) 'force))
6076     (when (eq major-mode 'gnus-summary-mode)
6077       (gnus-summary-next-subject 1 nil t)
6078       (gnus-summary-recenter)
6079       (gnus-summary-position-point))))
6080
6081 (defun gnus-summary-preview-mime-message ()
6082   "MIME decode and play this message."
6083   (interactive)
6084   (let ((gnus-break-pages nil)
6085         (gnus-show-mime t))
6086     (gnus-summary-select-article gnus-show-all-headers t))
6087   (select-window (get-buffer-window gnus-article-buffer)))
6088
6089 ;;; Dead summaries.
6090
6091 (defvar gnus-dead-summary-mode-map nil)
6092
6093 (unless gnus-dead-summary-mode-map
6094   (setq gnus-dead-summary-mode-map (make-keymap))
6095   (suppress-keymap gnus-dead-summary-mode-map)
6096   (substitute-key-definition
6097    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6098   (let ((keys '("\C-d" "\r" "\177" [delete])))
6099     (while keys
6100       (define-key gnus-dead-summary-mode-map
6101         (pop keys) 'gnus-summary-wake-up-the-dead))))
6102
6103 (defvar gnus-dead-summary-mode nil
6104   "Minor mode for Gnus summary buffers.")
6105
6106 (defun gnus-dead-summary-mode (&optional arg)
6107   "Minor mode for Gnus summary buffers."
6108   (interactive "P")
6109   (when (eq major-mode 'gnus-summary-mode)
6110     (make-local-variable 'gnus-dead-summary-mode)
6111     (setq gnus-dead-summary-mode
6112           (if (null arg) (not gnus-dead-summary-mode)
6113             (> (prefix-numeric-value arg) 0)))
6114     (when gnus-dead-summary-mode
6115       (gnus-add-minor-mode
6116        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6117
6118 (defun gnus-deaden-summary ()
6119   "Make the current summary buffer into a dead summary buffer."
6120   ;; Kill any previous dead summary buffer.
6121   (when (and gnus-dead-summary
6122              (buffer-name gnus-dead-summary))
6123     (save-excursion
6124       (set-buffer gnus-dead-summary)
6125       (when gnus-dead-summary-mode
6126         (kill-buffer (current-buffer)))))
6127   ;; Make this the current dead summary.
6128   (setq gnus-dead-summary (current-buffer))
6129   (gnus-dead-summary-mode 1)
6130   (let ((name (buffer-name)))
6131     (when (string-match "Summary" name)
6132       (rename-buffer
6133        (concat (substring name 0 (match-beginning 0)) "Dead "
6134                (substring name (match-beginning 0)))
6135        t)
6136       (bury-buffer))))
6137
6138 (defun gnus-kill-or-deaden-summary (buffer)
6139   "Kill or deaden the summary BUFFER."
6140   (save-excursion
6141     (when (and (buffer-name buffer)
6142                (not gnus-single-article-buffer))
6143       (save-excursion
6144         (set-buffer buffer)
6145         (gnus-kill-buffer gnus-article-buffer)
6146         (gnus-kill-buffer gnus-original-article-buffer)))
6147     (cond (gnus-kill-summary-on-exit
6148            (when (and gnus-use-trees
6149                       (gnus-buffer-exists-p buffer))
6150              (save-excursion
6151                (set-buffer buffer)
6152                (gnus-tree-close gnus-newsgroup-name)))
6153            (gnus-kill-buffer buffer))
6154           ((gnus-buffer-exists-p buffer)
6155            (save-excursion
6156              (set-buffer buffer)
6157              (gnus-deaden-summary))))))
6158
6159 (defun gnus-summary-wake-up-the-dead (&rest args)
6160   "Wake up the dead summary buffer."
6161   (interactive)
6162   (gnus-dead-summary-mode -1)
6163   (let ((name (buffer-name)))
6164     (when (string-match "Dead " name)
6165       (rename-buffer
6166        (concat (substring name 0 (match-beginning 0))
6167                (substring name (match-end 0)))
6168        t)))
6169   (gnus-message 3 "This dead summary is now alive again"))
6170
6171 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6172 (defun gnus-summary-fetch-faq (&optional faq-dir)
6173   "Fetch the FAQ for the current group.
6174 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6175 in."
6176   (interactive
6177    (list
6178     (when current-prefix-arg
6179       (completing-read
6180        "Faq dir: " (and (listp gnus-group-faq-directory)
6181                         (mapcar (lambda (file) (list file))
6182                                 gnus-group-faq-directory))))))
6183   (let (gnus-faq-buffer)
6184     (when (setq gnus-faq-buffer
6185                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6186       (gnus-configure-windows 'summary-faq))))
6187
6188 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6189 (defun gnus-summary-describe-group (&optional force)
6190   "Describe the current newsgroup."
6191   (interactive "P")
6192   (gnus-group-describe-group force gnus-newsgroup-name))
6193
6194 (defun gnus-summary-describe-briefly ()
6195   "Describe summary mode commands briefly."
6196   (interactive)
6197   (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")))
6198
6199 ;; Walking around group mode buffer from summary mode.
6200
6201 (defun gnus-summary-next-group (&optional no-article target-group backward)
6202   "Exit current newsgroup and then select next unread newsgroup.
6203 If prefix argument NO-ARTICLE is non-nil, no article is selected
6204 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6205 previous group instead."
6206   (interactive "P")
6207   ;; Stop pre-fetching.
6208   (gnus-async-halt-prefetch)
6209   (let ((current-group gnus-newsgroup-name)
6210         (current-buffer (current-buffer))
6211         entered)
6212     ;; First we semi-exit this group to update Xrefs and all variables.
6213     ;; We can't do a real exit, because the window conf must remain
6214     ;; the same in case the user is prompted for info, and we don't
6215     ;; want the window conf to change before that...
6216     (gnus-summary-exit t)
6217     (while (not entered)
6218       ;; Then we find what group we are supposed to enter.
6219       (set-buffer gnus-group-buffer)
6220       (gnus-group-jump-to-group current-group)
6221       (setq target-group
6222             (or target-group
6223                 (if (eq gnus-keep-same-level 'best)
6224                     (gnus-summary-best-group gnus-newsgroup-name)
6225                   (gnus-summary-search-group backward gnus-keep-same-level))))
6226       (if (not target-group)
6227           ;; There are no further groups, so we return to the group
6228           ;; buffer.
6229           (progn
6230             (gnus-message 5 "Returning to the group buffer")
6231             (setq entered t)
6232             (when (gnus-buffer-live-p current-buffer)
6233               (set-buffer current-buffer)
6234               (gnus-summary-exit))
6235             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6236         ;; We try to enter the target group.
6237         (gnus-group-jump-to-group target-group)
6238         (let ((unreads (gnus-group-group-unread)))
6239           (if (and (or (eq t unreads)
6240                        (and unreads (not (zerop unreads))))
6241                    (gnus-summary-read-group
6242                     target-group nil no-article
6243                     (and (buffer-name current-buffer) current-buffer)
6244                     nil backward))
6245               (setq entered t)
6246             (setq current-group target-group
6247                   target-group nil)))))))
6248
6249 (defun gnus-summary-prev-group (&optional no-article)
6250   "Exit current newsgroup and then select previous unread newsgroup.
6251 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6252   (interactive "P")
6253   (gnus-summary-next-group no-article nil t))
6254
6255 ;; Walking around summary lines.
6256
6257 (defun gnus-summary-first-subject (&optional unread undownloaded)
6258   "Go to the first unread subject.
6259 If UNREAD is non-nil, go to the first unread article.
6260 Returns the article selected or nil if there are no unread articles."
6261   (interactive "P")
6262   (prog1
6263       (cond
6264        ;; Empty summary.
6265        ((null gnus-newsgroup-data)
6266         (gnus-message 3 "No articles in the group")
6267         nil)
6268        ;; Pick the first article.
6269        ((not unread)
6270         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6271         (gnus-data-number (car gnus-newsgroup-data)))
6272        ;; No unread articles.
6273        ((null gnus-newsgroup-unreads)
6274         (gnus-message 3 "No more unread articles")
6275         nil)
6276        ;; Find the first unread article.
6277        (t
6278         (let ((data gnus-newsgroup-data))
6279           (while (and data
6280                       (and (not (and undownloaded
6281                                      (eq gnus-undownloaded-mark
6282                                          (gnus-data-mark (car data)))))
6283                            (not (gnus-data-unread-p (car data)))))
6284             (setq data (cdr data)))
6285           (when data
6286             (goto-char (gnus-data-pos (car data)))
6287             (gnus-data-number (car data))))))
6288     (gnus-summary-position-point)))
6289
6290 (defun gnus-summary-next-subject (n &optional unread dont-display)
6291   "Go to next N'th summary line.
6292 If N is negative, go to the previous N'th subject line.
6293 If UNREAD is non-nil, only unread articles are selected.
6294 The difference between N and the actual number of steps taken is
6295 returned."
6296   (interactive "p")
6297   (let ((backward (< n 0))
6298         (n (abs n)))
6299     (while (and (> n 0)
6300                 (if backward
6301                     (gnus-summary-find-prev unread)
6302                   (gnus-summary-find-next unread)))
6303       (unless (zerop (setq n (1- n)))
6304         (gnus-summary-show-thread)))
6305     (when (/= 0 n)
6306       (gnus-message 7 "No more%s articles"
6307                     (if unread " unread" "")))
6308     (unless dont-display
6309       (gnus-summary-recenter)
6310       (gnus-summary-position-point))
6311     n))
6312
6313 (defun gnus-summary-next-unread-subject (n)
6314   "Go to next N'th unread summary line."
6315   (interactive "p")
6316   (gnus-summary-next-subject n t))
6317
6318 (defun gnus-summary-prev-subject (n &optional unread)
6319   "Go to previous N'th summary line.
6320 If optional argument UNREAD is non-nil, only unread article is selected."
6321   (interactive "p")
6322   (gnus-summary-next-subject (- n) unread))
6323
6324 (defun gnus-summary-prev-unread-subject (n)
6325   "Go to previous N'th unread summary line."
6326   (interactive "p")
6327   (gnus-summary-next-subject (- n) t))
6328
6329 (defun gnus-summary-goto-subject (article &optional force silent)
6330   "Go the subject line of ARTICLE.
6331 If FORCE, also allow jumping to articles not currently shown."
6332   (interactive "nArticle number: ")
6333   (let ((b (point))
6334         (data (gnus-data-find article)))
6335     ;; We read in the article if we have to.
6336     (and (not data)
6337          force
6338          (gnus-summary-insert-subject
6339           article
6340           (if (or (numberp force) (vectorp force)) force)
6341           t)
6342          (setq data (gnus-data-find article)))
6343     (goto-char b)
6344     (if (not data)
6345         (progn
6346           (unless silent
6347             (gnus-message 3 "Can't find article %d" article))
6348           nil)
6349       (let ((pt (gnus-data-pos data)))
6350         (goto-char pt)
6351         (gnus-summary-set-article-display-arrow pt))
6352       (gnus-summary-position-point)
6353       article)))
6354
6355 ;; Walking around summary lines with displaying articles.
6356
6357 (defun gnus-summary-expand-window (&optional arg)
6358   "Make the summary buffer take up the entire Emacs frame.
6359 Given a prefix, will force an `article' buffer configuration."
6360   (interactive "P")
6361   (if arg
6362       (gnus-configure-windows 'article 'force)
6363     (gnus-configure-windows 'summary 'force)))
6364
6365 (defun gnus-summary-display-article (article &optional all-header)
6366   "Display ARTICLE in article buffer."
6367   (when (gnus-buffer-live-p gnus-article-buffer)
6368     (with-current-buffer gnus-article-buffer
6369       (set-buffer-multibyte t)))
6370   (gnus-set-global-variables)
6371   (when (gnus-buffer-live-p gnus-article-buffer)
6372     (with-current-buffer gnus-article-buffer
6373       (setq gnus-article-charset gnus-newsgroup-charset)
6374       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6375   (if (null article)
6376       nil
6377     (prog1
6378         (if gnus-summary-display-article-function
6379             (funcall gnus-summary-display-article-function article all-header)
6380           (gnus-article-prepare article all-header))
6381       (with-current-buffer gnus-article-buffer
6382         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6383              nil))
6384       (gnus-run-hooks 'gnus-select-article-hook)
6385       (when (and gnus-current-article
6386                  (not (zerop gnus-current-article)))
6387         (gnus-summary-goto-subject gnus-current-article))
6388       (gnus-summary-recenter)
6389       (when (and gnus-use-trees gnus-show-threads)
6390         (gnus-possibly-generate-tree article)
6391         (gnus-highlight-selected-tree article))
6392       ;; Successfully display article.
6393       (gnus-article-set-window-start
6394        (cdr (assq article gnus-newsgroup-bookmarks))))))
6395
6396 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6397   "Select the current article.
6398 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6399 non-nil, the article will be re-fetched even if it already present in
6400 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6401 be displayed."
6402   ;; Make sure we are in the summary buffer to work around bbdb bug.
6403   (unless (eq major-mode 'gnus-summary-mode)
6404     (set-buffer gnus-summary-buffer))
6405   (let ((article (or article (gnus-summary-article-number)))
6406         (all-headers (not (not all-headers))) ;Must be T or NIL.
6407         gnus-summary-display-article-function)
6408     (and (not pseudo)
6409          (gnus-summary-article-pseudo-p article)
6410          (error "This is a pseudo-article"))
6411     (save-excursion
6412       (set-buffer gnus-summary-buffer)
6413       (if (or (and gnus-single-article-buffer
6414                    (or (null gnus-current-article)
6415                        (null gnus-article-current)
6416                        (null (get-buffer gnus-article-buffer))
6417                        (not (eq article (cdr gnus-article-current)))
6418                        (not (equal (car gnus-article-current)
6419                                    gnus-newsgroup-name))))
6420               (and (not gnus-single-article-buffer)
6421                    (or (null gnus-current-article)
6422                        (not (eq gnus-current-article article))))
6423               force)
6424           ;; The requested article is different from the current article.
6425           (progn
6426             (gnus-summary-display-article article all-headers)
6427             (when (or all-headers gnus-show-all-headers)
6428               (gnus-article-show-all-headers))
6429             (gnus-article-set-window-start
6430              (cdr (assq article gnus-newsgroup-bookmarks)))
6431             article)
6432         (when (or all-headers gnus-show-all-headers)
6433           (gnus-article-show-all-headers))
6434         'old))))
6435
6436 (defun gnus-summary-force-verify-and-decrypt ()
6437   (interactive)
6438   (let ((mm-verify-option 'known)
6439         (mm-decrypt-option 'known))
6440     (gnus-summary-select-article nil 'force)))
6441
6442 (defun gnus-summary-set-current-mark (&optional current-mark)
6443   "Obsolete function."
6444   nil)
6445
6446 (defun gnus-summary-next-article (&optional unread subject backward push)
6447   "Select the next article.
6448 If UNREAD, only unread articles are selected.
6449 If SUBJECT, only articles with SUBJECT are selected.
6450 If BACKWARD, the previous article is selected instead of the next."
6451   (interactive "P")
6452   (cond
6453    ;; Is there such an article?
6454    ((and (gnus-summary-search-forward unread subject backward)
6455          (or (gnus-summary-display-article (gnus-summary-article-number))
6456              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6457     (gnus-summary-position-point))
6458    ;; If not, we try the first unread, if that is wanted.
6459    ((and subject
6460          gnus-auto-select-same
6461          (gnus-summary-first-unread-article))
6462     (gnus-summary-position-point)
6463     (gnus-message 6 "Wrapped"))
6464    ;; Try to get next/previous article not displayed in this group.
6465    ((and gnus-auto-extend-newsgroup
6466          (not unread) (not subject))
6467     (gnus-summary-goto-article
6468      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6469      nil (count-lines (point-min) (point))))
6470    ;; Go to next/previous group.
6471    (t
6472     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6473       (gnus-summary-jump-to-group gnus-newsgroup-name))
6474     (let ((cmd last-command-char)
6475           (point
6476            (save-excursion
6477              (set-buffer gnus-group-buffer)
6478              (point)))
6479           (group
6480            (if (eq gnus-keep-same-level 'best)
6481                (gnus-summary-best-group gnus-newsgroup-name)
6482              (gnus-summary-search-group backward gnus-keep-same-level))))
6483       ;; For some reason, the group window gets selected.  We change
6484       ;; it back.
6485       (select-window (get-buffer-window (current-buffer)))
6486       ;; Select next unread newsgroup automagically.
6487       (cond
6488        ((or (not gnus-auto-select-next)
6489             (not cmd))
6490         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6491        ((or (eq gnus-auto-select-next 'quietly)
6492             (and (eq gnus-auto-select-next 'slightly-quietly)
6493                  push)
6494             (and (eq gnus-auto-select-next 'almost-quietly)
6495                  (gnus-summary-last-article-p)))
6496         ;; Select quietly.
6497         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6498             (gnus-summary-exit)
6499           (gnus-message 7 "No more%s articles (%s)..."
6500                         (if unread " unread" "")
6501                         (if group (concat "selecting " group)
6502                           "exiting"))
6503           (gnus-summary-next-group nil group backward)))
6504        (t
6505         (when (gnus-key-press-event-p last-input-event)
6506           (gnus-summary-walk-group-buffer
6507            gnus-newsgroup-name cmd unread backward point))))))))
6508
6509 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6510   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6511                       (?\C-p (gnus-group-prev-unread-group 1))))
6512         (cursor-in-echo-area t)
6513         keve key group ended)
6514     (save-excursion
6515       (set-buffer gnus-group-buffer)
6516       (goto-char start)
6517       (setq group
6518             (if (eq gnus-keep-same-level 'best)
6519                 (gnus-summary-best-group gnus-newsgroup-name)
6520               (gnus-summary-search-group backward gnus-keep-same-level))))
6521     (while (not ended)
6522       (gnus-message
6523        5 "No more%s articles%s" (if unread " unread" "")
6524        (if (and group
6525                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6526            (format " (Type %s for %s [%s])"
6527                    (single-key-description cmd) group
6528                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6529          (format " (Type %s to exit %s)"
6530                  (single-key-description cmd)
6531                  gnus-newsgroup-name)))
6532       ;; Confirm auto selection.
6533       (setq key (car (setq keve (gnus-read-event-char))))
6534       (setq ended t)
6535       (cond
6536        ((assq key keystrokes)
6537         (let ((obuf (current-buffer)))
6538           (switch-to-buffer gnus-group-buffer)
6539           (when group
6540             (gnus-group-jump-to-group group))
6541           (eval (cadr (assq key keystrokes)))
6542           (setq group (gnus-group-group-name))
6543           (switch-to-buffer obuf))
6544         (setq ended nil))
6545        ((equal key cmd)
6546         (if (or (not group)
6547                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6548             (gnus-summary-exit)
6549           (gnus-summary-next-group nil group backward)))
6550        (t
6551         (push (cdr keve) unread-command-events))))))
6552
6553 (defun gnus-summary-next-unread-article ()
6554   "Select unread article after current one."
6555   (interactive)
6556   (gnus-summary-next-article
6557    (or (not (eq gnus-summary-goto-unread 'never))
6558        (gnus-summary-last-article-p (gnus-summary-article-number)))
6559    (and gnus-auto-select-same
6560         (gnus-summary-article-subject))))
6561
6562 (defun gnus-summary-prev-article (&optional unread subject)
6563   "Select the article after the current one.
6564 If UNREAD is non-nil, only unread articles are selected."
6565   (interactive "P")
6566   (gnus-summary-next-article unread subject t))
6567
6568 (defun gnus-summary-prev-unread-article ()
6569   "Select unread article before current one."
6570   (interactive)
6571   (gnus-summary-prev-article
6572    (or (not (eq gnus-summary-goto-unread 'never))
6573        (gnus-summary-first-article-p (gnus-summary-article-number)))
6574    (and gnus-auto-select-same
6575         (gnus-summary-article-subject))))
6576
6577 (defun gnus-summary-next-page (&optional lines circular)
6578   "Show next page of the selected article.
6579 If at the end of the current article, select the next article.
6580 LINES says how many lines should be scrolled up.
6581
6582 If CIRCULAR is non-nil, go to the start of the article instead of
6583 selecting the next article when reaching the end of the current
6584 article."
6585   (interactive "P")
6586   (setq gnus-summary-buffer (current-buffer))
6587   (gnus-set-global-variables)
6588   (let ((article (gnus-summary-article-number))
6589         (article-window (get-buffer-window gnus-article-buffer t))
6590         endp)
6591     ;; If the buffer is empty, we have no article.
6592     (unless article
6593       (error "No article to select"))
6594     (gnus-configure-windows 'article)
6595     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6596         (if (and (eq gnus-summary-goto-unread 'never)
6597                  (not (gnus-summary-last-article-p article)))
6598             (gnus-summary-next-article)
6599           (gnus-summary-next-unread-article))
6600       (if (or (null gnus-current-article)
6601               (null gnus-article-current)
6602               (/= article (cdr gnus-article-current))
6603               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6604           ;; Selected subject is different from current article's.
6605           (gnus-summary-display-article article)
6606         (when article-window
6607           (gnus-eval-in-buffer-window gnus-article-buffer
6608             (setq endp (gnus-article-next-page lines)))
6609           (when endp
6610             (cond (circular
6611                    (gnus-summary-beginning-of-article))
6612                   (lines
6613                    (gnus-message 3 "End of message"))
6614                   ((null lines)
6615                    (if (and (eq gnus-summary-goto-unread 'never)
6616                             (not (gnus-summary-last-article-p article)))
6617                        (gnus-summary-next-article)
6618                      (gnus-summary-next-unread-article))))))))
6619     (gnus-summary-recenter)
6620     (gnus-summary-position-point)))
6621
6622 (defun gnus-summary-prev-page (&optional lines move)
6623   "Show previous page of selected article.
6624 Argument LINES specifies lines to be scrolled down.
6625 If MOVE, move to the previous unread article if point is at
6626 the beginning of the buffer."
6627   (interactive "P")
6628   (let ((article (gnus-summary-article-number))
6629         (article-window (get-buffer-window gnus-article-buffer t))
6630         endp)
6631     (gnus-configure-windows 'article)
6632     (if (or (null gnus-current-article)
6633             (null gnus-article-current)
6634             (/= article (cdr gnus-article-current))
6635             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6636         ;; Selected subject is different from current article's.
6637         (gnus-summary-display-article article)
6638       (gnus-summary-recenter)
6639       (when article-window
6640         (gnus-eval-in-buffer-window gnus-article-buffer
6641           (setq endp (gnus-article-prev-page lines)))
6642         (when (and move endp)
6643           (cond (lines
6644                  (gnus-message 3 "Beginning of message"))
6645                 ((null lines)
6646                  (if (and (eq gnus-summary-goto-unread 'never)
6647                           (not (gnus-summary-first-article-p article)))
6648                      (gnus-summary-prev-article)
6649                    (gnus-summary-prev-unread-article))))))))
6650   (gnus-summary-position-point))
6651
6652 (defun gnus-summary-prev-page-or-article (&optional lines)
6653   "Show previous page of selected article.
6654 Argument LINES specifies lines to be scrolled down.
6655 If at the beginning of the article, go to the next article."
6656   (interactive "P")
6657   (gnus-summary-prev-page lines t))
6658
6659 (defun gnus-summary-scroll-up (lines)
6660   "Scroll up (or down) one line current article.
6661 Argument LINES specifies lines to be scrolled up (or down if negative)."
6662   (interactive "p")
6663   (gnus-configure-windows 'article)
6664   (gnus-summary-show-thread)
6665   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6666     (gnus-eval-in-buffer-window gnus-article-buffer
6667       (cond ((> lines 0)
6668              (when (gnus-article-next-page lines)
6669                (gnus-message 3 "End of message")))
6670             ((< lines 0)
6671              (gnus-article-prev-page (- lines))))))
6672   (gnus-summary-recenter)
6673   (gnus-summary-position-point))
6674
6675 (defun gnus-summary-scroll-down (lines)
6676   "Scroll down (or up) one line current article.
6677 Argument LINES specifies lines to be scrolled down (or up if negative)."
6678   (interactive "p")
6679   (gnus-summary-scroll-up (- lines)))
6680
6681 (defun gnus-summary-next-same-subject ()
6682   "Select next article which has the same subject as current one."
6683   (interactive)
6684   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6685
6686 (defun gnus-summary-prev-same-subject ()
6687   "Select previous article which has the same subject as current one."
6688   (interactive)
6689   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6690
6691 (defun gnus-summary-next-unread-same-subject ()
6692   "Select next unread article which has the same subject as current one."
6693   (interactive)
6694   (gnus-summary-next-article t (gnus-summary-article-subject)))
6695
6696 (defun gnus-summary-prev-unread-same-subject ()
6697   "Select previous unread article which has the same subject as current one."
6698   (interactive)
6699   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6700
6701 (defun gnus-summary-first-unread-article ()
6702   "Select the first unread article.
6703 Return nil if there are no unread articles."
6704   (interactive)
6705   (prog1
6706       (when (gnus-summary-first-subject t)
6707         (gnus-summary-show-thread)
6708         (gnus-summary-first-subject t)
6709         (gnus-summary-display-article (gnus-summary-article-number)))
6710     (gnus-summary-position-point)))
6711
6712 (defun gnus-summary-first-unread-subject ()
6713   "Place the point on the subject line of the first unread article.
6714 Return nil if there are no unread articles."
6715   (interactive)
6716   (prog1
6717       (when (gnus-summary-first-subject t)
6718         (gnus-summary-show-thread)
6719         (gnus-summary-first-subject t))
6720     (gnus-summary-position-point)))
6721
6722 (defun gnus-summary-first-article ()
6723   "Select the first article.
6724 Return nil if there are no articles."
6725   (interactive)
6726   (prog1
6727       (when (gnus-summary-first-subject)
6728         (gnus-summary-show-thread)
6729         (gnus-summary-first-subject)
6730         (gnus-summary-display-article (gnus-summary-article-number)))
6731     (gnus-summary-position-point)))
6732
6733 (defun gnus-summary-best-unread-article ()
6734   "Select the unread article with the highest score."
6735   (interactive)
6736   (let ((best -1000000)
6737         (data gnus-newsgroup-data)
6738         article score)
6739     (while data
6740       (and (gnus-data-unread-p (car data))
6741            (> (setq score
6742                     (gnus-summary-article-score (gnus-data-number (car data))))
6743               best)
6744            (setq best score
6745                  article (gnus-data-number (car data))))
6746       (setq data (cdr data)))
6747     (prog1
6748         (if article
6749             (gnus-summary-goto-article article)
6750           (error "No unread articles"))
6751       (gnus-summary-position-point))))
6752
6753 (defun gnus-summary-last-subject ()
6754   "Go to the last displayed subject line in the group."
6755   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6756     (when article
6757       (gnus-summary-goto-subject article))))
6758
6759 (defun gnus-summary-goto-article (article &optional all-headers force)
6760   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6761 If ALL-HEADERS is non-nil, no header lines are hidden.
6762 If FORCE, go to the article even if it isn't displayed.  If FORCE
6763 is a number, it is the line the article is to be displayed on."
6764   (interactive
6765    (list
6766     (completing-read
6767      "Article number or Message-ID: "
6768      (mapcar (lambda (number) (list (int-to-string number)))
6769              gnus-newsgroup-limit))
6770     current-prefix-arg
6771     t))
6772   (prog1
6773       (if (and (stringp article)
6774                (string-match "@" article))
6775           (gnus-summary-refer-article article)
6776         (when (stringp article)
6777           (setq article (string-to-number article)))
6778         (if (gnus-summary-goto-subject article force)
6779             (gnus-summary-display-article article all-headers)
6780           (gnus-message 4 "Couldn't go to article %s" article) nil))
6781     (gnus-summary-position-point)))
6782
6783 (defun gnus-summary-goto-last-article ()
6784   "Go to the previously read article."
6785   (interactive)
6786   (prog1
6787       (when gnus-last-article
6788         (gnus-summary-goto-article gnus-last-article nil t))
6789     (gnus-summary-position-point)))
6790
6791 (defun gnus-summary-pop-article (number)
6792   "Pop one article off the history and go to the previous.
6793 NUMBER articles will be popped off."
6794   (interactive "p")
6795   (let (to)
6796     (setq gnus-newsgroup-history
6797           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6798     (if to
6799         (gnus-summary-goto-article (car to) nil t)
6800       (error "Article history empty")))
6801   (gnus-summary-position-point))
6802
6803 ;; Summary commands and functions for limiting the summary buffer.
6804
6805 (defun gnus-summary-limit-to-articles (n)
6806   "Limit the summary buffer to the next N articles.
6807 If not given a prefix, use the process marked articles instead."
6808   (interactive "P")
6809   (prog1
6810       (let ((articles (gnus-summary-work-articles n)))
6811         (setq gnus-newsgroup-processable nil)
6812         (gnus-summary-limit articles))
6813     (gnus-summary-position-point)))
6814
6815 (defun gnus-summary-pop-limit (&optional total)
6816   "Restore the previous limit.
6817 If given a prefix, remove all limits."
6818   (interactive "P")
6819   (when total
6820     (setq gnus-newsgroup-limits
6821           (list (mapcar (lambda (h) (mail-header-number h))
6822                         gnus-newsgroup-headers))))
6823   (unless gnus-newsgroup-limits
6824     (error "No limit to pop"))
6825   (prog1
6826       (gnus-summary-limit nil 'pop)
6827     (gnus-summary-position-point)))
6828
6829 (defun gnus-summary-limit-to-subject (subject &optional header)
6830   "Limit the summary buffer to articles that have subjects that match a regexp."
6831   (interactive "sLimit to subject (regexp): ")
6832   (unless header
6833     (setq header "subject"))
6834   (when (not (equal "" subject))
6835     (prog1
6836         (let ((articles (gnus-summary-find-matching
6837                          (or header "subject") subject 'all)))
6838           (unless articles
6839             (error "Found no matches for \"%s\"" subject))
6840           (gnus-summary-limit articles))
6841       (gnus-summary-position-point))))
6842
6843 (defun gnus-summary-limit-to-author (from)
6844   "Limit the summary buffer to articles that have authors that match a regexp."
6845   (interactive "sLimit to author (regexp): ")
6846   (gnus-summary-limit-to-subject from "from"))
6847
6848 (defun gnus-summary-limit-to-age (age &optional younger-p)
6849   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6850 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6851 articles that are younger than AGE days."
6852   (interactive
6853    (let ((younger current-prefix-arg)
6854          (days-got nil)
6855          days)
6856      (while (not days-got)
6857        (setq days (if younger
6858                       (read-string "Limit to articles within (in days): ")
6859                     (read-string "Limit to articles older than (in days): ")))
6860        (when (> (length days) 0)
6861          (setq days (read days)))
6862        (if (numberp days)
6863            (setq days-got t)
6864          (message "Please enter a number.")
6865          (sleep-for 1)))
6866      (list days younger)))
6867   (prog1
6868       (let ((data gnus-newsgroup-data)
6869             (cutoff (days-to-time age))
6870             articles d date is-younger)
6871         (while (setq d (pop data))
6872           (when (and (vectorp (gnus-data-header d))
6873                      (setq date (mail-header-date (gnus-data-header d))))
6874             (setq is-younger (time-less-p
6875                               (time-since (condition-case ()
6876                                               (date-to-time date)
6877                                             (error '(0 0))))
6878                               cutoff))
6879             (when (if younger-p
6880                       is-younger
6881                     (not is-younger))
6882               (push (gnus-data-number d) articles))))
6883         (gnus-summary-limit (nreverse articles)))
6884     (gnus-summary-position-point)))
6885
6886 (defun gnus-summary-limit-to-extra (header regexp)
6887   "Limit the summary buffer to articles that match an 'extra' header."
6888   (interactive
6889    (let ((header
6890           (intern
6891            (gnus-completing-read
6892             (symbol-name (car gnus-extra-headers))
6893             "Limit extra header:"
6894             (mapcar (lambda (x)
6895                       (cons (symbol-name x) x))
6896                     gnus-extra-headers)
6897             nil
6898             t))))
6899      (list header
6900            (read-string (format "Limit to header %s (regexp): " header)))))
6901   (when (not (equal "" regexp))
6902     (prog1
6903         (let ((articles (gnus-summary-find-matching
6904                          (cons 'extra header) regexp 'all)))
6905           (unless articles
6906             (error "Found no matches for \"%s\"" regexp))
6907           (gnus-summary-limit articles))
6908       (gnus-summary-position-point))))
6909
6910 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6911 (make-obsolete
6912  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6913
6914 (defun gnus-summary-limit-to-unread (&optional all)
6915   "Limit the summary buffer to articles that are not marked as read.
6916 If ALL is non-nil, limit strictly to unread articles."
6917   (interactive "P")
6918   (if all
6919       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6920     (gnus-summary-limit-to-marks
6921      ;; Concat all the marks that say that an article is read and have
6922      ;; those removed.
6923      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6924            gnus-killed-mark gnus-kill-file-mark
6925            gnus-low-score-mark gnus-expirable-mark
6926            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6927            gnus-duplicate-mark gnus-souped-mark)
6928      'reverse)))
6929
6930 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6931 (make-obsolete 'gnus-summary-delete-marked-with
6932                'gnus-summary-limit-exlude-marks)
6933
6934 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6935   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6936 If REVERSE, limit the summary buffer to articles that are marked
6937 with MARKS.  MARKS can either be a string of marks or a list of marks.
6938 Returns how many articles were removed."
6939   (interactive "sMarks: ")
6940   (gnus-summary-limit-to-marks marks t))
6941
6942 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6943   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6944 If REVERSE (the prefix), limit the summary buffer to articles that are
6945 not marked with MARKS.  MARKS can either be a string of marks or a
6946 list of marks.
6947 Returns how many articles were removed."
6948   (interactive "sMarks: \nP")
6949   (prog1
6950       (let ((data gnus-newsgroup-data)
6951             (marks (if (listp marks) marks
6952                      (append marks nil))) ; Transform to list.
6953             articles)
6954         (while data
6955           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6956                   (memq (gnus-data-mark (car data)) marks))
6957             (push (gnus-data-number (car data)) articles))
6958           (setq data (cdr data)))
6959         (gnus-summary-limit articles))
6960     (gnus-summary-position-point)))
6961
6962 (defun gnus-summary-limit-to-score (&optional score)
6963   "Limit to articles with score at or above SCORE."
6964   (interactive "P")
6965   (setq score (if score
6966                   (prefix-numeric-value score)
6967                 (or gnus-summary-default-score 0)))
6968   (let ((data gnus-newsgroup-data)
6969         articles)
6970     (while data
6971       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6972                 score)
6973         (push (gnus-data-number (car data)) articles))
6974       (setq data (cdr data)))
6975     (prog1
6976         (gnus-summary-limit articles)
6977       (gnus-summary-position-point))))
6978
6979 (defun gnus-summary-limit-include-thread (id)
6980   "Display all the hidden articles that is in the thread with ID in it.
6981 When called interactively, ID is the Message-ID of the current
6982 article."
6983   (interactive (list (mail-header-id (gnus-summary-article-header))))
6984   (let ((articles (gnus-articles-in-thread
6985                    (gnus-id-to-thread (gnus-root-id id)))))
6986     (prog1
6987         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6988       (gnus-summary-limit-include-matching-articles
6989        "subject"
6990        (regexp-quote (gnus-simplify-subject-re
6991                       (mail-header-subject (gnus-id-to-header id)))))
6992       (gnus-summary-position-point))))
6993
6994 (defun gnus-summary-limit-include-matching-articles (header regexp)
6995   "Display all the hidden articles that have HEADERs that match REGEXP."
6996   (interactive (list (read-string "Match on header: ")
6997                      (read-string "Regexp: ")))
6998   (let ((articles (gnus-find-matching-articles header regexp)))
6999     (prog1
7000         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7001       (gnus-summary-position-point))))
7002
7003 (defun gnus-summary-limit-include-dormant ()
7004   "Display all the hidden articles that are marked as dormant.
7005 Note that this command only works on a subset of the articles currently
7006 fetched for this group."
7007   (interactive)
7008   (unless gnus-newsgroup-dormant
7009     (error "There are no dormant articles in this group"))
7010   (prog1
7011       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7012     (gnus-summary-position-point)))
7013
7014 (defun gnus-summary-limit-exclude-dormant ()
7015   "Hide all dormant articles."
7016   (interactive)
7017   (prog1
7018       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7019     (gnus-summary-position-point)))
7020
7021 (defun gnus-summary-limit-exclude-childless-dormant ()
7022   "Hide all dormant articles that have no children."
7023   (interactive)
7024   (let ((data (gnus-data-list t))
7025         articles d children)
7026     ;; Find all articles that are either not dormant or have
7027     ;; children.
7028     (while (setq d (pop data))
7029       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7030                 (and (setq children
7031                            (gnus-article-children (gnus-data-number d)))
7032                      (let (found)
7033                        (while children
7034                          (when (memq (car children) articles)
7035                            (setq children nil
7036                                  found t))
7037                          (pop children))
7038                        found)))
7039         (push (gnus-data-number d) articles)))
7040     ;; Do the limiting.
7041     (prog1
7042         (gnus-summary-limit articles)
7043       (gnus-summary-position-point))))
7044
7045 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7046   "Mark all unread excluded articles as read.
7047 If ALL, mark even excluded ticked and dormants as read."
7048   (interactive "P")
7049   (let ((articles (gnus-sorted-complement
7050                    (sort
7051                     (mapcar (lambda (h) (mail-header-number h))
7052                             gnus-newsgroup-headers)
7053                     '<)
7054                    (sort gnus-newsgroup-limit '<)))
7055         article)
7056     (setq gnus-newsgroup-unreads
7057           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7058     (if all
7059         (setq gnus-newsgroup-dormant nil
7060               gnus-newsgroup-marked nil
7061               gnus-newsgroup-reads
7062               (nconc
7063                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7064                gnus-newsgroup-reads))
7065       (while (setq article (pop articles))
7066         (unless (or (memq article gnus-newsgroup-dormant)
7067                     (memq article gnus-newsgroup-marked))
7068           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7069
7070 (defun gnus-summary-limit (articles &optional pop)
7071   (if pop
7072       ;; We pop the previous limit off the stack and use that.
7073       (setq articles (car gnus-newsgroup-limits)
7074             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7075     ;; We use the new limit, so we push the old limit on the stack.
7076     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7077   ;; Set the limit.
7078   (setq gnus-newsgroup-limit articles)
7079   (let ((total (length gnus-newsgroup-data))
7080         (data (gnus-data-find-list (gnus-summary-article-number)))
7081         (gnus-summary-mark-below nil)   ; Inhibit this.
7082         found)
7083     ;; This will do all the work of generating the new summary buffer
7084     ;; according to the new limit.
7085     (gnus-summary-prepare)
7086     ;; Hide any threads, possibly.
7087     (and gnus-show-threads
7088          gnus-thread-hide-subtree
7089          (gnus-summary-hide-all-threads))
7090     ;; Try to return to the article you were at, or one in the
7091     ;; neighborhood.
7092     (when data
7093       ;; We try to find some article after the current one.
7094       (while data
7095         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7096           (setq data nil
7097                 found t))
7098         (setq data (cdr data))))
7099     (unless found
7100       ;; If there is no data, that means that we were after the last
7101       ;; article.  The same goes when we can't find any articles
7102       ;; after the current one.
7103       (goto-char (point-max))
7104       (gnus-summary-find-prev))
7105     (gnus-set-mode-line 'summary)
7106     ;; We return how many articles were removed from the summary
7107     ;; buffer as a result of the new limit.
7108     (- total (length gnus-newsgroup-data))))
7109
7110 (defsubst gnus-invisible-cut-children (threads)
7111   (let ((num 0))
7112     (while threads
7113       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7114         (incf num))
7115       (pop threads))
7116     (< num 2)))
7117
7118 (defsubst gnus-cut-thread (thread)
7119   "Go forwards in the thread until we find an article that we want to display."
7120   (when (or (eq gnus-fetch-old-headers 'some)
7121             (eq gnus-fetch-old-headers 'invisible)
7122             (numberp gnus-fetch-old-headers)
7123             (eq gnus-build-sparse-threads 'some)
7124             (eq gnus-build-sparse-threads 'more))
7125     ;; Deal with old-fetched headers and sparse threads.
7126     (while (and
7127             thread
7128             (or
7129              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7130              (gnus-summary-article-ancient-p
7131               (mail-header-number (car thread))))
7132             (if (or (<= (length (cdr thread)) 1)
7133                     (eq gnus-fetch-old-headers 'invisible))
7134                 (setq gnus-newsgroup-limit
7135                       (delq (mail-header-number (car thread))
7136                             gnus-newsgroup-limit)
7137                       thread (cadr thread))
7138               (when (gnus-invisible-cut-children (cdr thread))
7139                 (let ((th (cdr thread)))
7140                   (while th
7141                     (if (memq (mail-header-number (caar th))
7142                               gnus-newsgroup-limit)
7143                         (setq thread (car th)
7144                               th nil)
7145                       (setq th (cdr th))))))))))
7146   thread)
7147
7148 (defun gnus-cut-threads (threads)
7149   "Cut off all uninteresting articles from the beginning of threads."
7150   (when (or (eq gnus-fetch-old-headers 'some)
7151             (eq gnus-fetch-old-headers 'invisible)
7152             (numberp gnus-fetch-old-headers)
7153             (eq gnus-build-sparse-threads 'some)
7154             (eq gnus-build-sparse-threads 'more))
7155     (let ((th threads))
7156       (while th
7157         (setcar th (gnus-cut-thread (car th)))
7158         (setq th (cdr th)))))
7159   ;; Remove nixed out threads.
7160   (delq nil threads))
7161
7162 (defun gnus-summary-initial-limit (&optional show-if-empty)
7163   "Figure out what the initial limit is supposed to be on group entry.
7164 This entails weeding out unwanted dormants, low-scored articles,
7165 fetch-old-headers verbiage, and so on."
7166   ;; Most groups have nothing to remove.
7167   (if (or gnus-inhibit-limiting
7168           (and (null gnus-newsgroup-dormant)
7169                (eq gnus-newsgroup-display 'all)
7170                (not (eq gnus-fetch-old-headers 'some))
7171                (not (numberp gnus-fetch-old-headers))
7172                (not (eq gnus-fetch-old-headers 'invisible))
7173                (null gnus-summary-expunge-below)
7174                (not (eq gnus-build-sparse-threads 'some))
7175                (not (eq gnus-build-sparse-threads 'more))
7176                (null gnus-thread-expunge-below)
7177                (not gnus-use-nocem)))
7178       ()                                ; Do nothing.
7179     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7180     (setq gnus-newsgroup-limit nil)
7181     (mapatoms
7182      (lambda (node)
7183        (unless (car (symbol-value node))
7184          ;; These threads have no parents -- they are roots.
7185          (let ((nodes (cdr (symbol-value node)))
7186                thread)
7187            (while nodes
7188              (if (and gnus-thread-expunge-below
7189                       (< (gnus-thread-total-score (car nodes))
7190                          gnus-thread-expunge-below))
7191                  (gnus-expunge-thread (pop nodes))
7192                (setq thread (pop nodes))
7193                (gnus-summary-limit-children thread))))))
7194      gnus-newsgroup-dependencies)
7195     ;; If this limitation resulted in an empty group, we might
7196     ;; pop the previous limit and use it instead.
7197     (when (and (not gnus-newsgroup-limit)
7198                show-if-empty)
7199       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7200     gnus-newsgroup-limit))
7201
7202 (defun gnus-summary-limit-children (thread)
7203   "Return 1 if this subthread is visible and 0 if it is not."
7204   ;; First we get the number of visible children to this thread.  This
7205   ;; is done by recursing down the thread using this function, so this
7206   ;; will really go down to a leaf article first, before slowly
7207   ;; working its way up towards the root.
7208   (when thread
7209     (let ((children
7210            (if (cdr thread)
7211                (apply '+ (mapcar 'gnus-summary-limit-children
7212                                  (cdr thread)))
7213              0))
7214           (number (mail-header-number (car thread)))
7215           score)
7216       (if (and
7217            (not (memq number gnus-newsgroup-marked))
7218            (or
7219             ;; If this article is dormant and has absolutely no visible
7220             ;; children, then this article isn't visible.
7221             (and (memq number gnus-newsgroup-dormant)
7222                  (zerop children))
7223             ;; If this is "fetch-old-headered" and there is no
7224             ;; visible children, then we don't want this article.
7225             (and (or (eq gnus-fetch-old-headers 'some)
7226                      (numberp gnus-fetch-old-headers))
7227                  (gnus-summary-article-ancient-p number)
7228                  (zerop children))
7229             ;; If this is "fetch-old-headered" and `invisible', then
7230             ;; we don't want this article.
7231             (and (eq gnus-fetch-old-headers 'invisible)
7232                  (gnus-summary-article-ancient-p number))
7233             ;; If this is a sparsely inserted article with no children,
7234             ;; we don't want it.
7235             (and (eq gnus-build-sparse-threads 'some)
7236                  (gnus-summary-article-sparse-p number)
7237                  (zerop children))
7238             ;; If we use expunging, and this article is really
7239             ;; low-scored, then we don't want this article.
7240             (when (and gnus-summary-expunge-below
7241                        (< (setq score
7242                                 (or (cdr (assq number gnus-newsgroup-scored))
7243                                     gnus-summary-default-score))
7244                           gnus-summary-expunge-below))
7245               ;; We increase the expunge-tally here, but that has
7246               ;; nothing to do with the limits, really.
7247               (incf gnus-newsgroup-expunged-tally)
7248               ;; We also mark as read here, if that's wanted.
7249               (when (and gnus-summary-mark-below
7250                          (< score gnus-summary-mark-below))
7251                 (setq gnus-newsgroup-unreads
7252                       (delq number gnus-newsgroup-unreads))
7253                 (if gnus-newsgroup-auto-expire
7254                     (push number gnus-newsgroup-expirable)
7255                   (push (cons number gnus-low-score-mark)
7256                         gnus-newsgroup-reads)))
7257               t)
7258             ;; Do the `display' group parameter.
7259             (and gnus-newsgroup-display
7260                  (not (eq gnus-newsgroup-display 'all))
7261                  (not (funcall gnus-newsgroup-display)))
7262             ;; Check NoCeM things.
7263             (if (and gnus-use-nocem
7264                      (gnus-nocem-unwanted-article-p
7265                       (mail-header-id (car thread))))
7266                 (progn
7267                   (setq gnus-newsgroup-unreads
7268                         (delq number gnus-newsgroup-unreads))
7269                   t))))
7270           ;; Nope, invisible article.
7271           0
7272         ;; Ok, this article is to be visible, so we add it to the limit
7273         ;; and return 1.
7274         (push number gnus-newsgroup-limit)
7275         1))))
7276
7277 (defun gnus-expunge-thread (thread)
7278   "Mark all articles in THREAD as read."
7279   (let* ((number (mail-header-number (car thread))))
7280     (incf gnus-newsgroup-expunged-tally)
7281     ;; We also mark as read here, if that's wanted.
7282     (setq gnus-newsgroup-unreads
7283           (delq number gnus-newsgroup-unreads))
7284     (if gnus-newsgroup-auto-expire
7285         (push number gnus-newsgroup-expirable)
7286       (push (cons number gnus-low-score-mark)
7287             gnus-newsgroup-reads)))
7288   ;; Go recursively through all subthreads.
7289   (mapcar 'gnus-expunge-thread (cdr thread)))
7290
7291 ;; Summary article oriented commands
7292
7293 (defun gnus-summary-refer-parent-article (n)
7294   "Refer parent article N times.
7295 If N is negative, go to ancestor -N instead.
7296 The difference between N and the number of articles fetched is returned."
7297   (interactive "p")
7298   (let ((skip 1)
7299         error header ref)
7300     (when (not (natnump n))
7301       (setq skip (abs n)
7302             n 1))
7303     (while (and (> n 0)
7304                 (not error))
7305       (setq header (gnus-summary-article-header))
7306       (if (and (eq (mail-header-number header)
7307                    (cdr gnus-article-current))
7308                (equal gnus-newsgroup-name
7309                       (car gnus-article-current)))
7310           ;; If we try to find the parent of the currently
7311           ;; displayed article, then we take a look at the actual
7312           ;; References header, since this is slightly more
7313           ;; reliable than the References field we got from the
7314           ;; server.
7315           (save-excursion
7316             (set-buffer gnus-original-article-buffer)
7317             (nnheader-narrow-to-headers)
7318             (unless (setq ref (message-fetch-field "references"))
7319               (setq ref (message-fetch-field "in-reply-to")))
7320             (widen))
7321         (setq ref
7322               ;; It's not the current article, so we take a bet on
7323               ;; the value we got from the server.
7324               (mail-header-references header)))
7325       (if (and ref
7326                (not (equal ref "")))
7327           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7328             (gnus-message 1 "Couldn't find parent"))
7329         (gnus-message 1 "No references in article %d"
7330                       (gnus-summary-article-number))
7331         (setq error t))
7332       (decf n))
7333     (gnus-summary-position-point)
7334     n))
7335
7336 (defun gnus-summary-refer-references ()
7337   "Fetch all articles mentioned in the References header.
7338 Return the number of articles fetched."
7339   (interactive)
7340   (let ((ref (mail-header-references (gnus-summary-article-header)))
7341         (current (gnus-summary-article-number))
7342         (n 0))
7343     (if (or (not ref)
7344             (equal ref ""))
7345         (error "No References in the current article")
7346       ;; For each Message-ID in the References header...
7347       (while (string-match "<[^>]*>" ref)
7348         (incf n)
7349         ;; ... fetch that article.
7350         (gnus-summary-refer-article
7351          (prog1 (match-string 0 ref)
7352            (setq ref (substring ref (match-end 0))))))
7353       (gnus-summary-goto-subject current)
7354       (gnus-summary-position-point)
7355       n)))
7356
7357 (defun gnus-summary-refer-thread (&optional limit)
7358   "Fetch all articles in the current thread.
7359 If LIMIT (the numerical prefix), fetch that many old headers instead
7360 of what's specified by the `gnus-refer-thread-limit' variable."
7361   (interactive "P")
7362   (let ((id (mail-header-id (gnus-summary-article-header)))
7363         (limit (if limit (prefix-numeric-value limit)
7364                  gnus-refer-thread-limit)))
7365     ;; We want to fetch LIMIT *old* headers, but we also have to
7366     ;; re-fetch all the headers in the current buffer, because many of
7367     ;; them may be undisplayed.  So we adjust LIMIT.
7368     (when (numberp limit)
7369       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7370     (unless (eq gnus-fetch-old-headers 'invisible)
7371       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7372       ;; Retrieve the headers and read them in.
7373       (if (eq (gnus-retrieve-headers
7374                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7375               'nov)
7376           (gnus-build-all-threads)
7377         (error "Can't fetch thread from backends that don't support NOV"))
7378       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7379     (gnus-summary-limit-include-thread id)))
7380
7381 (defun gnus-summary-refer-article (message-id)
7382   "Fetch an article specified by MESSAGE-ID."
7383   (interactive "sMessage-ID: ")
7384   (when (and (stringp message-id)
7385              (not (zerop (length message-id))))
7386     ;; Construct the correct Message-ID if necessary.
7387     ;; Suggested by tale@pawl.rpi.edu.
7388     (unless (string-match "^<" message-id)
7389       (setq message-id (concat "<" message-id)))
7390     (unless (string-match ">$" message-id)
7391       (setq message-id (concat message-id ">")))
7392     (let* ((header (gnus-id-to-header message-id))
7393            (sparse (and header
7394                         (gnus-summary-article-sparse-p
7395                          (mail-header-number header))
7396                         (memq (mail-header-number header)
7397                               gnus-newsgroup-limit)))
7398            number)
7399       (cond
7400        ;; If the article is present in the buffer we just go to it.
7401        ((and header
7402              (or (not (gnus-summary-article-sparse-p
7403                        (mail-header-number header)))
7404                  sparse))
7405         (prog1
7406             (gnus-summary-goto-article
7407              (mail-header-number header) nil t)
7408           (when sparse
7409             (gnus-summary-update-article (mail-header-number header)))))
7410        (t
7411         ;; We fetch the article.
7412         (catch 'found
7413           (dolist (gnus-override-method (gnus-refer-article-methods))
7414             (gnus-check-server gnus-override-method)
7415             ;; Fetch the header, and display the article.
7416             (when (setq number (gnus-summary-insert-subject message-id))
7417               (gnus-summary-select-article nil nil nil number)
7418               (throw 'found t)))
7419           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7420
7421 (defun gnus-refer-article-methods ()
7422   "Return a list of referrable methods."
7423   (cond
7424    ;; No method, so we default to current and native.
7425    ((null gnus-refer-article-method)
7426     (list gnus-current-select-method gnus-select-method))
7427    ;; Current.
7428    ((eq 'current gnus-refer-article-method)
7429     (list gnus-current-select-method))
7430    ;; List of select methods.
7431    ((not (and (symbolp (car gnus-refer-article-method))
7432               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7433     (let (out)
7434       (dolist (method gnus-refer-article-method)
7435         (push (if (eq 'current method)
7436                   gnus-current-select-method
7437                 method)
7438               out))
7439       (nreverse out)))
7440    ;; One single select method.
7441    (t
7442     (list gnus-refer-article-method))))
7443
7444 (defun gnus-summary-edit-parameters ()
7445   "Edit the group parameters of the current group."
7446   (interactive)
7447   (gnus-group-edit-group gnus-newsgroup-name 'params))
7448
7449 (defun gnus-summary-customize-parameters ()
7450   "Customize the group parameters of the current group."
7451   (interactive)
7452   (gnus-group-customize gnus-newsgroup-name))
7453
7454 (defun gnus-summary-enter-digest-group (&optional force)
7455   "Enter an nndoc group based on the current article.
7456 If FORCE, force a digest interpretation.  If not, try
7457 to guess what the document format is."
7458   (interactive "P")
7459   (let ((conf gnus-current-window-configuration))
7460     (save-excursion
7461       (gnus-summary-select-article))
7462     (setq gnus-current-window-configuration conf)
7463     (let* ((name (format "%s-%d"
7464                          (gnus-group-prefixed-name
7465                           gnus-newsgroup-name (list 'nndoc ""))
7466                          (save-excursion
7467                            (set-buffer gnus-summary-buffer)
7468                            gnus-current-article)))
7469            (ogroup gnus-newsgroup-name)
7470            (params (append (gnus-info-params (gnus-get-info ogroup))
7471                            (list (cons 'to-group ogroup))
7472                            (list (cons 'save-article-group ogroup))))
7473            (case-fold-search t)
7474            (buf (current-buffer))
7475            dig to-address)
7476       (save-excursion
7477         (set-buffer gnus-original-article-buffer)
7478         ;; Have the digest group inherit the main mail address of
7479         ;; the parent article.
7480         (when (setq to-address (or (message-fetch-field "reply-to")
7481                                    (message-fetch-field "from")))
7482           (setq params (append
7483                         (list (cons 'to-address
7484                                     (funcall gnus-decode-encoded-word-function
7485                                              to-address))))))
7486         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7487         (insert-buffer-substring gnus-original-article-buffer)
7488         ;; Remove lines that may lead nndoc to misinterpret the
7489         ;; document type.
7490         (narrow-to-region
7491          (goto-char (point-min))
7492          (or (search-forward "\n\n" nil t) (point)))
7493         (goto-char (point-min))
7494         (delete-matching-lines "^Path:\\|^From ")
7495         (widen))
7496       (unwind-protect
7497           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7498                     (gnus-newsgroup-ephemeral-ignored-charsets
7499                      gnus-newsgroup-ignored-charsets))
7500                 (gnus-group-read-ephemeral-group
7501                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7502                               (nndoc-article-type
7503                                ,(if force 'mbox 'guess))) t))
7504               ;; Make all postings to this group go to the parent group.
7505               (nconc (gnus-info-params (gnus-get-info name))
7506                      params)
7507             ;; Couldn't select this doc group.
7508             (switch-to-buffer buf)
7509             (gnus-set-global-variables)
7510             (gnus-configure-windows 'summary)
7511             (gnus-message 3 "Article couldn't be entered?"))
7512         (kill-buffer dig)))))
7513
7514 (defun gnus-summary-read-document (n)
7515   "Open a new group based on the current article(s).
7516 This will allow you to read digests and other similar
7517 documents as newsgroups.
7518 Obeys the standard process/prefix convention."
7519   (interactive "P")
7520   (let* ((articles (gnus-summary-work-articles n))
7521          (ogroup gnus-newsgroup-name)
7522          (params (append (gnus-info-params (gnus-get-info ogroup))
7523                          (list (cons 'to-group ogroup))))
7524          article group egroup groups vgroup)
7525     (while (setq article (pop articles))
7526       (setq group (format "%s-%d" gnus-newsgroup-name article))
7527       (gnus-summary-remove-process-mark article)
7528       (when (gnus-summary-display-article article)
7529         (save-excursion
7530           (with-temp-buffer
7531             (insert-buffer-substring gnus-original-article-buffer)
7532             ;; Remove some headers that may lead nndoc to make
7533             ;; the wrong guess.
7534             (message-narrow-to-head)
7535             (goto-char (point-min))
7536             (delete-matching-lines "^\\(Path\\):\\|^From ")
7537             (widen)
7538             (if (setq egroup
7539                       (gnus-group-read-ephemeral-group
7540                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7541                                      (nndoc-article-type guess))
7542                        t nil t))
7543                 (progn
7544                   ;; Make all postings to this group go to the parent group.
7545                   (nconc (gnus-info-params (gnus-get-info egroup))
7546                          params)
7547                   (push egroup groups))
7548               ;; Couldn't select this doc group.
7549               (gnus-error 3 "Article couldn't be entered"))))))
7550     ;; Now we have selected all the documents.
7551     (cond
7552      ((not groups)
7553       (error "None of the articles could be interpreted as documents"))
7554      ((gnus-group-read-ephemeral-group
7555        (setq vgroup (format
7556                      "nnvirtual:%s-%s" gnus-newsgroup-name
7557                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7558        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7559        t
7560        (cons (current-buffer) 'summary)))
7561      (t
7562       (error "Couldn't select virtual nndoc group")))))
7563
7564 (defun gnus-summary-isearch-article (&optional regexp-p)
7565   "Do incremental search forward on the current article.
7566 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7567   (interactive "P")
7568   (let* ((gnus-inhibit-treatment t)
7569          (old (gnus-summary-select-article)))
7570     (gnus-configure-windows 'article)
7571     (gnus-eval-in-buffer-window gnus-article-buffer
7572       (save-restriction
7573         (widen)
7574         (when (eq 'old old)
7575           (gnus-article-show-all-headers))
7576         (goto-char (point-min))
7577         (isearch-forward regexp-p)))))
7578
7579 (defun gnus-summary-search-article-forward (regexp &optional backward)
7580   "Search for an article containing REGEXP forward.
7581 If BACKWARD, search backward instead."
7582   (interactive
7583    (list (read-string
7584           (format "Search article %s (regexp%s): "
7585                   (if current-prefix-arg "backward" "forward")
7586                   (if gnus-last-search-regexp
7587                       (concat ", default " gnus-last-search-regexp)
7588                     "")))
7589          current-prefix-arg))
7590   (if (string-equal regexp "")
7591       (setq regexp (or gnus-last-search-regexp ""))
7592     (setq gnus-last-search-regexp regexp)
7593     (setq gnus-article-before-search gnus-current-article))
7594   ;; Intentionally set gnus-last-article.
7595   (setq gnus-last-article gnus-article-before-search)
7596   (let ((gnus-last-article gnus-last-article))
7597     (if (gnus-summary-search-article regexp backward)
7598         (gnus-summary-show-thread)
7599       (error "Search failed: \"%s\"" regexp))))
7600
7601 (defun gnus-summary-search-article-backward (regexp)
7602   "Search for an article containing REGEXP backward."
7603   (interactive
7604    (list (read-string
7605           (format "Search article backward (regexp%s): "
7606                   (if gnus-last-search-regexp
7607                       (concat ", default " gnus-last-search-regexp)
7608                     "")))))
7609   (gnus-summary-search-article-forward regexp 'backward))
7610
7611 (eval-when-compile
7612   (defmacro gnus-summary-search-article-position-point (regexp backward)
7613     "Dehighlight the last matched text and goto the beginning position."
7614     (` (if (and gnus-summary-search-article-matched-data
7615                 (let ((text (caddr gnus-summary-search-article-matched-data))
7616                       (inhibit-read-only t)
7617                       buffer-read-only)
7618                   (delete-region
7619                    (goto-char (car gnus-summary-search-article-matched-data))
7620                    (cadr gnus-summary-search-article-matched-data))
7621                   (insert text)
7622                   (string-match (, regexp) text)))
7623            (if (, backward) (beginning-of-line) (end-of-line))
7624          (goto-char (if (, backward) (point-max) (point-min))))))
7625
7626   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7627     "Place point where X-Face image is displayed."
7628     (if (featurep 'xemacs)
7629         (` (let ((end (if (search-forward "\n\n" nil t)
7630                           (goto-char (1- (point)))
7631                         (point-min)))
7632                  extent)
7633              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7634              (unless (and (re-search-forward "^From:" end t)
7635                           (setq extent (extent-at (point)))
7636                           (extent-begin-glyph extent))
7637                (goto-char (, opoint)))))
7638       (` (let ((end (if (search-forward "\n\n" nil t)
7639                         (goto-char (1- (point)))
7640                       (point-min)))
7641                (start (or (search-backward "\n\n" nil t) (point-min))))
7642            (goto-char
7643             (or (text-property-any start end 'x-face-image t);; x-face-e21
7644                 (text-property-any start end 'x-face-mule-bitmap-image t)
7645                 (, opoint)))))))
7646
7647   (defmacro gnus-summary-search-article-highlight-matched-text
7648     (backward treated x-face)
7649     "Highlight matched text in the function `gnus-summary-search-article'."
7650     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7651              (end (set-marker (make-marker) (match-end 0)))
7652              (inhibit-read-only t)
7653              buffer-read-only)
7654          (unless treated
7655            (let ((,@
7656                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7657                     (mapcar
7658                      (lambda (item) (setq items (delq item items)))
7659                      '(gnus-treat-buttonize
7660                        gnus-treat-fill-article
7661                        gnus-treat-fill-long-lines
7662                        gnus-treat-emphasize
7663                        gnus-treat-highlight-headers
7664                        gnus-treat-highlight-citation
7665                        gnus-treat-highlight-signature
7666                        gnus-treat-overstrike
7667                        gnus-treat-display-xface
7668                        gnus-treat-buttonize-head
7669                        gnus-treat-decode-article-as-default-mime-charset))
7670                     (static-if (featurep 'xemacs)
7671                         items
7672                       (cons '(x-face-mule-delete-x-face-field
7673                               (quote never))
7674                             items))))
7675                  (gnus-treat-display-xface
7676                   (when (, x-face) gnus-treat-display-xface)))
7677              (gnus-article-prepare-mime-display)))
7678          (goto-char (if (, backward) start end))
7679          (when (, x-face)
7680            (gnus-summary-search-article-highlight-goto-x-face (point)))
7681          (setq gnus-summary-search-article-matched-data
7682                (list start end (buffer-substring start end)))
7683          (unless (eq start end);; matched text has been deleted. :-<
7684            (put-text-property start end 'face
7685                               (or (find-face 'isearch)
7686                                   'secondary-selection))))))
7687   )
7688
7689 (defun gnus-summary-search-article (regexp &optional backward)
7690   "Search for an article containing REGEXP.
7691 Optional argument BACKWARD means do search for backward.
7692 `gnus-select-article-hook' is not called during the search."
7693   ;; We have to require this here to make sure that the following
7694   ;; dynamic binding isn't shadowed by autoloading.
7695   (require 'gnus-async)
7696   (require 'gnus-art)
7697   (let ((gnus-select-article-hook nil)  ;Disable hook.
7698         (gnus-article-prepare-hook nil)
7699         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7700         (gnus-use-article-prefetch nil)
7701         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7702         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7703         (sum (current-buffer))
7704         (found nil)
7705         point treated)
7706     (gnus-save-hidden-threads
7707       (static-if (featurep 'xemacs)
7708           (let ((gnus-inhibit-treatment t))
7709             (setq treated (eq 'old (gnus-summary-select-article)))
7710             (when (and treated
7711                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7712                                  (window-live-p (get-buffer-window
7713                                                  gnus-article-buffer t)))))
7714               (gnus-summary-select-article nil t)
7715               (setq treated nil)))
7716         (let ((gnus-inhibit-treatment t)
7717               (x-face-mule-delete-x-face-field 'never))
7718           (setq treated (eq 'old (gnus-summary-select-article)))
7719           (when (and treated
7720                      (not
7721                       (and (gnus-buffer-live-p gnus-article-buffer)
7722                            (window-live-p (get-buffer-window
7723                                            gnus-article-buffer t))
7724                            (or (not (string-match "^\\^X-Face:" regexp))
7725                                (with-current-buffer gnus-article-buffer
7726                                  gnus-summary-search-article-matched-data)))))
7727             (gnus-summary-select-article nil t)
7728             (setq treated nil))))
7729       (set-buffer gnus-article-buffer)
7730       (widen)
7731       (if treated
7732           (progn
7733             (gnus-article-show-all-headers)
7734             (gnus-summary-search-article-position-point regexp backward))
7735         (goto-char (if backward (point-max) (point-min))))
7736       (while (not found)
7737         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7738         (if (if backward
7739                 (re-search-backward regexp nil t)
7740               (re-search-forward regexp nil t))
7741             ;; We found the regexp.
7742             (progn
7743               (gnus-summary-search-article-highlight-matched-text
7744                backward treated (string-match "^\\^X-Face:" regexp))
7745               (setq found 'found)
7746               (forward-line
7747                (/ (- 2 (window-height
7748                         (get-buffer-window gnus-article-buffer t)))
7749                   2))
7750               (set-window-start
7751                (get-buffer-window (current-buffer))
7752                (point))
7753               (set-buffer sum)
7754               (setq point (point)))
7755           ;; We didn't find it, so we go to the next article.
7756           (set-buffer sum)
7757           (setq found 'not)
7758           (while (eq found 'not)
7759             (if (not (if backward (gnus-summary-find-prev)
7760                        (gnus-summary-find-next)))
7761                 ;; No more articles.
7762                 (setq found t)
7763               ;; Select the next article and adjust point.
7764               (unless (gnus-summary-article-sparse-p
7765                        (gnus-summary-article-number))
7766                 (setq found nil)
7767                 (let ((gnus-inhibit-treatment t))
7768                   (gnus-summary-select-article))
7769                 (setq treated nil)
7770                 (set-buffer gnus-article-buffer)
7771                 (widen)
7772                 (goto-char (if backward (point-max) (point-min))))))))
7773       (gnus-message 7 ""))
7774     ;; Return whether we found the regexp.
7775     (when (eq found 'found)
7776       (goto-char point)
7777       (gnus-summary-show-thread)
7778       (gnus-summary-goto-subject gnus-current-article)
7779       (gnus-summary-position-point)
7780       t)))
7781
7782 (defun gnus-find-matching-articles (header regexp)
7783   "Return a list of all articles that match REGEXP on HEADER.
7784 This search includes all articles in the current group that Gnus has
7785 fetched headers for, whether they are displayed or not."
7786   (let ((articles nil)
7787         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7788         (case-fold-search t))
7789     (dolist (header gnus-newsgroup-headers)
7790       (when (string-match regexp (funcall func header))
7791         (push (mail-header-number header) articles)))
7792     (nreverse articles)))
7793
7794 (defun gnus-summary-find-matching (header regexp &optional backward unread
7795                                           not-case-fold)
7796   "Return a list of all articles that match REGEXP on HEADER.
7797 The search stars on the current article and goes forwards unless
7798 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7799 If UNREAD is non-nil, only unread articles will
7800 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7801 in the comparisons."
7802   (let ((case-fold-search (not not-case-fold))
7803         articles d func)
7804     (if (consp header)
7805         (if (eq (car header) 'extra)
7806             (setq func
7807                   `(lambda (h)
7808                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7809                          "")))
7810           (error "%s is an invalid header" header))
7811       (unless (fboundp (intern (concat "mail-header-" header)))
7812         (error "%s is not a valid header" header))
7813       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7814     (dolist (d (if (eq backward 'all)
7815                    gnus-newsgroup-data
7816                  (gnus-data-find-list
7817                   (gnus-summary-article-number)
7818                   (gnus-data-list backward))))
7819       (when (and (or (not unread)       ; We want all articles...
7820                      (gnus-data-unread-p d)) ; Or just unreads.
7821                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7822                  (string-match regexp
7823                                (funcall func (gnus-data-header d)))) ; Match.
7824         (push (gnus-data-number d) articles))) ; Success!
7825     (nreverse articles)))
7826
7827 (defun gnus-summary-execute-command (header regexp command &optional backward)
7828   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7829 If HEADER is an empty string (or nil), the match is done on the entire
7830 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7831   (interactive
7832    (list (let ((completion-ignore-case t))
7833            (completing-read
7834             "Header name: "
7835             (mapcar (lambda (header) (list (format "%s" header)))
7836                     (append
7837                      '("Number" "Subject" "From" "Lines" "Date"
7838                        "Message-ID" "Xref" "References" "Body")
7839                      gnus-extra-headers))
7840             nil 'require-match))
7841          (read-string "Regexp: ")
7842          (read-key-sequence "Command: ")
7843          current-prefix-arg))
7844   (when (equal header "Body")
7845     (setq header ""))
7846   ;; Hidden thread subtrees must be searched as well.
7847   (gnus-summary-show-all-threads)
7848   ;; We don't want to change current point nor window configuration.
7849   (save-excursion
7850     (save-window-excursion
7851       (gnus-message 6 "Executing %s..." (key-description command))
7852       ;; We'd like to execute COMMAND interactively so as to give arguments.
7853       (gnus-execute header regexp
7854                     `(call-interactively ',(key-binding command))
7855                     backward)
7856       (gnus-message 6 "Executing %s...done" (key-description command)))))
7857
7858 (defun gnus-summary-beginning-of-article ()
7859   "Scroll the article back to the beginning."
7860   (interactive)
7861   (gnus-summary-select-article)
7862   (gnus-configure-windows 'article)
7863   (gnus-eval-in-buffer-window gnus-article-buffer
7864     (widen)
7865     (goto-char (point-min))
7866     (when gnus-page-broken
7867       (gnus-narrow-to-page))))
7868
7869 (defun gnus-summary-end-of-article ()
7870   "Scroll to the end of the article."
7871   (interactive)
7872   (gnus-summary-select-article)
7873   (gnus-configure-windows 'article)
7874   (gnus-eval-in-buffer-window gnus-article-buffer
7875     (widen)
7876     (goto-char (point-max))
7877     (recenter -3)
7878     (when gnus-page-broken
7879       (gnus-narrow-to-page))))
7880
7881 (defun gnus-summary-print-article (&optional filename n)
7882   "Generate and print a PostScript image of the N next (mail) articles.
7883
7884 If N is negative, print the N previous articles.  If N is nil and articles
7885 have been marked with the process mark, print these instead.
7886
7887 If the optional first argument FILENAME is nil, send the image to the
7888 printer.  If FILENAME is a string, save the PostScript image in a file with
7889 that name.  If FILENAME is a number, prompt the user for the name of the file
7890 to save in."
7891   (interactive (list (ps-print-preprint current-prefix-arg)))
7892   (dolist (article (gnus-summary-work-articles n))
7893     (gnus-summary-select-article nil nil 'pseudo article)
7894     (gnus-eval-in-buffer-window gnus-article-buffer
7895       (let ((buffer (generate-new-buffer " *print*")))
7896         (unwind-protect
7897             (progn
7898               (copy-to-buffer buffer (point-min) (point-max))
7899               (set-buffer buffer)
7900               (gnus-article-delete-invisible-text)
7901               (when (gnus-visual-p 'article-highlight 'highlight)
7902                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7903                 ;; highlight.
7904                 (let ((gnus-article-buffer buffer))
7905                   (gnus-article-highlight-citation t)
7906                   (gnus-article-highlight-signature)))
7907               (let ((ps-left-header
7908                      (list
7909                       (concat "("
7910                               (mail-header-subject gnus-current-headers) ")")
7911                       (concat "("
7912                               (mail-header-from gnus-current-headers) ")")))
7913                     (ps-right-header
7914                      (list
7915                       "/pagenumberstring load"
7916                       (concat "("
7917                               (mail-header-date gnus-current-headers) ")"))))
7918                 (gnus-run-hooks 'gnus-ps-print-hook)
7919                 (save-excursion
7920                   (if window-system
7921                       (ps-spool-buffer-with-faces)
7922                     (ps-spool-buffer)))))
7923           (kill-buffer buffer))))
7924     (gnus-summary-remove-process-mark article))
7925   (ps-despool filename))
7926
7927 (defun gnus-summary-show-article (&optional arg)
7928   "Force re-fetching of the current article.
7929 If ARG (the prefix) is a number, show the article with the charset
7930 defined in `gnus-summary-show-article-charset-alist', or the charset
7931 input.
7932 If ARG (the prefix) is non-nil and not a number, show the raw article
7933 without any article massaging functions being run."
7934   (interactive "P")
7935   (cond
7936    ((numberp arg)
7937     (gnus-summary-show-article t)
7938     (let ((gnus-newsgroup-charset
7939            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7940                (mm-read-coding-system
7941                 "View as charset: "
7942                 (save-excursion
7943                   (set-buffer gnus-article-buffer)
7944                   (detect-coding-region (point) (point-max) t)))))
7945           (gnus-newsgroup-ignored-charsets 'gnus-all))
7946       (gnus-summary-select-article nil 'force)
7947       (let ((deps gnus-newsgroup-dependencies)
7948             head header)
7949         (save-excursion
7950           (set-buffer gnus-original-article-buffer)
7951           (save-restriction
7952             (message-narrow-to-head)
7953             (setq head (buffer-string)))
7954           (with-temp-buffer
7955             (insert (format "211 %d Article retrieved.\n"
7956                             (cdr gnus-article-current)))
7957             (insert head)
7958             (insert ".\n")
7959             (let ((nntp-server-buffer (current-buffer)))
7960               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7961         (gnus-data-set-header
7962          (gnus-data-find (cdr gnus-article-current))
7963          header)
7964         (gnus-summary-update-article-line
7965          (cdr gnus-article-current) header)
7966         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7967           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7968    ((not arg)
7969     ;; Select the article the normal way.
7970     (gnus-summary-select-article nil 'force))
7971    (t
7972     ;; We have to require this here to make sure that the following
7973     ;; dynamic binding isn't shadowed by autoloading.
7974     (require 'gnus-async)
7975     (require 'gnus-art)
7976     ;; Bind the article treatment functions to nil.
7977     (let ((gnus-have-all-headers t)
7978           gnus-article-prepare-hook
7979           gnus-article-decode-hook
7980           gnus-break-pages
7981           gnus-show-mime
7982           (gnus-inhibit-treatment t))
7983       (gnus-summary-select-article nil 'force))))
7984   (gnus-summary-goto-subject gnus-current-article)
7985   (gnus-summary-position-point))
7986
7987 (defun gnus-summary-show-raw-article ()
7988   "Show the raw article without any article massaging functions being run."
7989   (interactive)
7990   (gnus-summary-show-article t))
7991
7992 (defun gnus-summary-verbose-headers (&optional arg)
7993   "Toggle permanent full header display.
7994 If ARG is a positive number, turn header display on.
7995 If ARG is a negative number, turn header display off."
7996   (interactive "P")
7997   (setq gnus-show-all-headers
7998         (cond ((or (not (numberp arg))
7999                    (zerop arg))
8000                (not gnus-show-all-headers))
8001               ((natnump arg)
8002                t)))
8003   (gnus-summary-show-article))
8004
8005 (defun gnus-summary-toggle-header (&optional arg)
8006   "Show the headers if they are hidden, or hide them if they are shown.
8007 If ARG is a positive number, show the entire header.
8008 If ARG is a negative number, hide the unwanted header lines."
8009   (interactive "P")
8010   (save-excursion
8011     (set-buffer gnus-article-buffer)
8012     (save-restriction
8013       (let* ((buffer-read-only nil)
8014              (inhibit-point-motion-hooks t)
8015              hidden e)
8016         (setq hidden
8017               (if (numberp arg)
8018                   (>= arg 0)
8019                 (save-restriction
8020                   (article-narrow-to-head)
8021                   (gnus-article-hidden-text-p 'headers))))
8022         (goto-char (point-min))
8023         (when (search-forward "\n\n" nil t)
8024           (delete-region (point-min) (1- (point))))
8025         (goto-char (point-min))
8026         (save-excursion
8027           (set-buffer gnus-original-article-buffer)
8028           (goto-char (point-min))
8029           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8030         (insert-buffer-substring gnus-original-article-buffer 1 e)
8031         (save-restriction
8032           (narrow-to-region (point-min) (point))
8033           (article-decode-encoded-words)
8034           (if  hidden
8035               (let ((gnus-treat-hide-headers nil)
8036                     (gnus-treat-hide-boring-headers nil))
8037                 (setq gnus-article-wash-types
8038                       (delq 'headers gnus-article-wash-types))
8039                 (gnus-treat-article 'head))
8040             (gnus-treat-article 'head)))
8041         (gnus-set-mode-line 'article)))))
8042
8043 (defun gnus-summary-show-all-headers ()
8044   "Make all header lines visible."
8045   (interactive)
8046   (gnus-summary-toggle-header 1))
8047
8048 (defun gnus-summary-toggle-mime (&optional arg)
8049   "Toggle MIME processing.
8050 If ARG is a positive number, turn MIME processing on."
8051   (interactive "P")
8052   (setq gnus-show-mime
8053         (if (null arg)
8054             (not gnus-show-mime)
8055           (> (prefix-numeric-value arg) 0)))
8056   (gnus-summary-select-article t 'force))
8057
8058 (defun gnus-summary-caesar-message (&optional arg)
8059   "Caesar rotate the current article by 13.
8060 The numerical prefix specifies how many places to rotate each letter
8061 forward."
8062   (interactive "P")
8063   (gnus-summary-select-article)
8064   (let ((mail-header-separator ""))
8065     (gnus-eval-in-buffer-window gnus-article-buffer
8066       (save-restriction
8067         (widen)
8068         (let ((start (window-start))
8069               buffer-read-only)
8070           (message-caesar-buffer-body arg)
8071           (set-window-start (get-buffer-window (current-buffer)) start))))))
8072
8073 (defun gnus-summary-stop-page-breaking ()
8074   "Stop page breaking in the current article."
8075   (interactive)
8076   (gnus-summary-select-article)
8077   (gnus-eval-in-buffer-window gnus-article-buffer
8078     (widen)
8079     (when (gnus-visual-p 'page-marker)
8080       (let ((buffer-read-only nil))
8081         (gnus-remove-text-with-property 'gnus-prev)
8082         (gnus-remove-text-with-property 'gnus-next))
8083       (setq gnus-page-broken nil))))
8084
8085 (defun gnus-summary-move-article (&optional n to-newsgroup
8086                                             select-method action)
8087   "Move the current article to a different newsgroup.
8088 If N is a positive number, move the N next articles.
8089 If N is a negative number, move the N previous articles.
8090 If N is nil and any articles have been marked with the process mark,
8091 move those articles instead.
8092 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8093 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8094 re-spool using this method.
8095
8096 For this function to work, both the current newsgroup and the
8097 newsgroup that you want to move to have to support the `request-move'
8098 and `request-accept' functions.
8099
8100 ACTION can be either `move' (the default), `crosspost' or `copy'."
8101   (interactive "P")
8102   (unless action
8103     (setq action 'move))
8104   ;; Check whether the source group supports the required functions.
8105   (cond ((and (eq action 'move)
8106               (not (gnus-check-backend-function
8107                     'request-move-article gnus-newsgroup-name)))
8108          (error "The current group does not support article moving"))
8109         ((and (eq action 'crosspost)
8110               (not (gnus-check-backend-function
8111                     'request-replace-article gnus-newsgroup-name)))
8112          (error "The current group does not support article editing")))
8113   (let ((articles (gnus-summary-work-articles n))
8114         (prefix (if (gnus-check-backend-function
8115                      'request-move-article gnus-newsgroup-name)
8116                     (gnus-group-real-prefix gnus-newsgroup-name)
8117                   ""))
8118         (names '((move "Move" "Moving")
8119                  (copy "Copy" "Copying")
8120                  (crosspost "Crosspost" "Crossposting")))
8121         (copy-buf (save-excursion
8122                     (nnheader-set-temp-buffer " *copy article*")))
8123         (default-marks gnus-article-mark-lists)
8124         (no-expire-marks (delete '(expirable . expire)
8125                                  (copy-sequence gnus-article-mark-lists)))
8126         art-group to-method new-xref article to-groups)
8127     (unless (assq action names)
8128       (error "Unknown action %s" action))
8129     ;; Read the newsgroup name.
8130     (when (and (not to-newsgroup)
8131                (not select-method))
8132       (setq to-newsgroup
8133             (gnus-read-move-group-name
8134              (cadr (assq action names))
8135              (symbol-value (intern (format "gnus-current-%s-group" action)))
8136              articles prefix))
8137       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8138     (setq to-method (or select-method
8139                         (gnus-server-to-method
8140                          (gnus-group-method to-newsgroup))))
8141     ;; Check the method we are to move this article to...
8142     (unless (gnus-check-backend-function
8143              'request-accept-article (car to-method))
8144       (error "%s does not support article copying" (car to-method)))
8145     (unless (gnus-check-server to-method)
8146       (error "Can't open server %s" (car to-method)))
8147     (gnus-message 6 "%s to %s: %s..."
8148                   (caddr (assq action names))
8149                   (or (car select-method) to-newsgroup) articles)
8150     (while articles
8151       (setq article (pop articles))
8152       (setq
8153        art-group
8154        (cond
8155         ;; Move the article.
8156         ((eq action 'move)
8157          ;; Remove this article from future suppression.
8158          (gnus-dup-unsuppress-article article)
8159          (gnus-request-move-article
8160           article                       ; Article to move
8161           gnus-newsgroup-name           ; From newsgroup
8162           (nth 1 (gnus-find-method-for-group
8163                   gnus-newsgroup-name)) ; Server
8164           (list 'gnus-request-accept-article
8165                 to-newsgroup (list 'quote select-method)
8166                 (not articles) t)       ; Accept form
8167           (not articles)))              ; Only save nov last time
8168         ;; Copy the article.
8169         ((eq action 'copy)
8170          (save-excursion
8171            (set-buffer copy-buf)
8172            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8173              (gnus-request-accept-article
8174               to-newsgroup select-method (not articles) t))))
8175         ;; Crosspost the article.
8176         ((eq action 'crosspost)
8177          (let ((xref (message-tokenize-header
8178                       (mail-header-xref (gnus-summary-article-header article))
8179                       " ")))
8180            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8181                                   ":" article))
8182            (unless xref
8183              (setq xref (list (system-name))))
8184            (setq new-xref
8185                  (concat
8186                   (mapconcat 'identity
8187                              (delete "Xref:" (delete new-xref xref))
8188                              " ")
8189                   " " new-xref))
8190            (save-excursion
8191              (set-buffer copy-buf)
8192              ;; First put the article in the destination group.
8193              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8194              (when (consp (setq art-group
8195                                 (gnus-request-accept-article
8196                                  to-newsgroup select-method (not articles))))
8197                (setq new-xref (concat new-xref " " (car art-group)
8198                                       ":" (cdr art-group)))
8199                ;; Now we have the new Xrefs header, so we insert
8200                ;; it and replace the new article.
8201                (nnheader-replace-header "Xref" new-xref)
8202                (gnus-request-replace-article
8203                 (cdr art-group) to-newsgroup (current-buffer))
8204                art-group))))))
8205       (cond
8206        ((not art-group)
8207         (gnus-message 1 "Couldn't %s article %s: %s"
8208                       (cadr (assq action names)) article
8209                       (nnheader-get-report (car to-method))))
8210        ((eq art-group 'junk)
8211         (when (eq action 'move)
8212           (gnus-summary-mark-article article gnus-canceled-mark)
8213           (gnus-message 4 "Deleted article %s" article)))
8214        (t
8215         (let* ((pto-group (gnus-group-prefixed-name
8216                            (car art-group) to-method))
8217                (entry
8218                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8219                (info (nth 2 entry))
8220                (to-group (gnus-info-group info))
8221                to-marks)
8222           ;; Update the group that has been moved to.
8223           (when (and info
8224                      (memq action '(move copy)))
8225             (unless (member to-group to-groups)
8226               (push to-group to-groups))
8227
8228             (unless (memq article gnus-newsgroup-unreads)
8229               (push 'read to-marks)
8230               (gnus-info-set-read
8231                info (gnus-add-to-range (gnus-info-read info)
8232                                        (list (cdr art-group)))))
8233
8234             ;; See whether the article is to be put in the cache.
8235             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8236                              default-marks
8237                            no-expire-marks))
8238                   (to-article (cdr art-group)))
8239
8240               ;; Enter the article into the cache in the new group,
8241               ;; if that is required.
8242               (when gnus-use-cache
8243                 (gnus-cache-possibly-enter-article
8244                  to-group to-article
8245                  (let ((header (copy-sequence
8246                                 (gnus-summary-article-header article))))
8247                    (mail-header-set-number header to-article)
8248                    header)
8249                  (memq article gnus-newsgroup-marked)
8250                  (memq article gnus-newsgroup-dormant)
8251                  (memq article gnus-newsgroup-unreads)))
8252
8253               (when gnus-preserve-marks
8254                 ;; Copy any marks over to the new group.
8255                 (when (and (equal to-group gnus-newsgroup-name)
8256                            (not (memq article gnus-newsgroup-unreads)))
8257                   ;; Mark this article as read in this group.
8258                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8259                   (setcdr (gnus-active to-group) to-article)
8260                   (setcdr gnus-newsgroup-active to-article))
8261
8262                 (while marks
8263                   (when (memq article (symbol-value
8264                                        (intern (format "gnus-newsgroup-%s"
8265                                                        (caar marks)))))
8266                     (push (cdar marks) to-marks)
8267                     ;; If the other group is the same as this group,
8268                     ;; then we have to add the mark to the list.
8269                     (when (equal to-group gnus-newsgroup-name)
8270                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8271                            (cons to-article
8272                                  (symbol-value
8273                                   (intern (format "gnus-newsgroup-%s"
8274                                                   (caar marks)))))))
8275                     ;; Copy the marks to other group.
8276                     (gnus-add-marked-articles
8277                      to-group (cdar marks) (list to-article) info))
8278                   (setq marks (cdr marks)))
8279
8280                 (gnus-request-set-mark to-group (list (list (list to-article)
8281                                                             'add
8282                                                             to-marks))))
8283
8284               (gnus-dribble-enter
8285                (concat "(gnus-group-set-info '"
8286                        (gnus-prin1-to-string (gnus-get-info to-group))
8287                        ")"))))
8288
8289           ;; Update the Xref header in this article to point to
8290           ;; the new crossposted article we have just created.
8291           (when (eq action 'crosspost)
8292             (save-excursion
8293               (set-buffer copy-buf)
8294               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8295               (nnheader-replace-header "Xref" new-xref)
8296               (gnus-request-replace-article
8297                article gnus-newsgroup-name (current-buffer)))))
8298
8299         ;;;!!!Why is this necessary?
8300         (set-buffer gnus-summary-buffer)
8301
8302         (gnus-summary-goto-subject article)
8303         (when (eq action 'move)
8304           (gnus-summary-mark-article article gnus-canceled-mark))))
8305       (gnus-summary-remove-process-mark article))
8306     ;; Re-activate all groups that have been moved to.
8307     (while to-groups
8308       (save-excursion
8309         (set-buffer gnus-group-buffer)
8310         (when (gnus-group-goto-group (car to-groups) t)
8311           (gnus-group-get-new-news-this-group 1 t))
8312         (pop to-groups)))
8313
8314     (gnus-kill-buffer copy-buf)
8315     (gnus-summary-position-point)
8316     (gnus-set-mode-line 'summary)))
8317
8318 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8319   "Move the current article to a different newsgroup.
8320 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8321 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8322 re-spool using this method."
8323   (interactive "P")
8324   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8325
8326 (defun gnus-summary-crosspost-article (&optional n)
8327   "Crosspost the current article to some other group."
8328   (interactive "P")
8329   (gnus-summary-move-article n nil nil 'crosspost))
8330
8331 (defcustom gnus-summary-respool-default-method nil
8332   "Default method for respooling an article.
8333 If nil, use to the current newsgroup method."
8334   :type '(choice (gnus-select-method :value (nnml ""))
8335                  (const nil))
8336   :group 'gnus-summary-mail)
8337
8338 (defun gnus-summary-respool-article (&optional n method)
8339   "Respool the current article.
8340 The article will be squeezed through the mail spooling process again,
8341 which means that it will be put in some mail newsgroup or other
8342 depending on `nnmail-split-methods'.
8343 If N is a positive number, respool the N next articles.
8344 If N is a negative number, respool the N previous articles.
8345 If N is nil and any articles have been marked with the process mark,
8346 respool those articles instead.
8347
8348 Respooling can be done both from mail groups and \"real\" newsgroups.
8349 In the former case, the articles in question will be moved from the
8350 current group into whatever groups they are destined to.  In the
8351 latter case, they will be copied into the relevant groups."
8352   (interactive
8353    (list current-prefix-arg
8354          (let* ((methods (gnus-methods-using 'respool))
8355                 (methname
8356                  (symbol-name (or gnus-summary-respool-default-method
8357                                   (car (gnus-find-method-for-group
8358                                         gnus-newsgroup-name)))))
8359                 (method
8360                  (gnus-completing-read
8361                   methname "What backend do you want to use when respooling?"
8362                   methods nil t nil 'gnus-mail-method-history))
8363                 ms)
8364            (cond
8365             ((zerop (length (setq ms (gnus-servers-using-backend
8366                                       (intern method)))))
8367              (list (intern method) ""))
8368             ((= 1 (length ms))
8369              (car ms))
8370             (t
8371              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8372                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8373                            ms-alist))))))))
8374   (unless method
8375     (error "No method given for respooling"))
8376   (if (assoc (symbol-name
8377               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8378              (gnus-methods-using 'respool))
8379       (gnus-summary-move-article n nil method)
8380     (gnus-summary-copy-article n nil method)))
8381
8382 (defun gnus-summary-import-article (file &optional edit)
8383   "Import an arbitrary file into a mail newsgroup."
8384   (interactive "fImport file: \nP")
8385   (let ((group gnus-newsgroup-name)
8386         (now (current-time))
8387         atts lines group-art)
8388     (unless (gnus-check-backend-function 'request-accept-article group)
8389       (error "%s does not support article importing" group))
8390     (or (file-readable-p file)
8391         (not (file-regular-p file))
8392         (error "Can't read %s" file))
8393     (save-excursion
8394       (set-buffer (gnus-get-buffer-create " *import file*"))
8395       (erase-buffer)
8396       (nnheader-insert-file-contents file)
8397       (goto-char (point-min))
8398       (if (nnheader-article-p)
8399           (save-restriction
8400             (goto-char (point-min))
8401             (search-forward "\n\n" nil t)
8402             (narrow-to-region (point-min) (1- (point)))
8403             (goto-char (point-min))
8404             (unless (re-search-forward "^date:" nil t)
8405               (goto-char (point-max))
8406               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8407         ;; This doesn't look like an article, so we fudge some headers.
8408         (setq atts (file-attributes file)
8409               lines (count-lines (point-min) (point-max)))
8410         (insert "From: " (read-string "From: ") "\n"
8411                 "Subject: " (read-string "Subject: ") "\n"
8412                 "Date: " (message-make-date (nth 5 atts)) "\n"
8413                 "Message-ID: " (message-make-message-id) "\n"
8414                 "Lines: " (int-to-string lines) "\n"
8415                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8416       (setq group-art (gnus-request-accept-article group nil t))
8417       (kill-buffer (current-buffer)))
8418     (setq gnus-newsgroup-active (gnus-activate-group group))
8419     (forward-line 1)
8420     (gnus-summary-goto-article (cdr group-art) nil t)
8421     (when edit
8422       (gnus-summary-edit-article))))
8423
8424 (defun gnus-summary-create-article ()
8425   "Create an article in a mail newsgroup."
8426   (interactive)
8427   (let ((group gnus-newsgroup-name)
8428         (now (current-time))
8429         group-art)
8430     (unless (gnus-check-backend-function 'request-accept-article group)
8431       (error "%s does not support article importing" group))
8432     (save-excursion
8433       (set-buffer (gnus-get-buffer-create " *import file*"))
8434       (erase-buffer)
8435       (goto-char (point-min))
8436       ;; This doesn't look like an article, so we fudge some headers.
8437       (insert "From: " (read-string "From: ") "\n"
8438               "Subject: " (read-string "Subject: ") "\n"
8439               "Date: " (message-make-date now) "\n"
8440               "Message-ID: " (message-make-message-id) "\n")
8441       (setq group-art (gnus-request-accept-article group nil t))
8442       (kill-buffer (current-buffer)))
8443     (setq gnus-newsgroup-active (gnus-activate-group group))
8444     (forward-line 1)
8445     (gnus-summary-goto-article (cdr group-art) nil t)
8446     (gnus-summary-edit-article)))
8447
8448 (defun gnus-summary-article-posted-p ()
8449   "Say whether the current (mail) article is available from news as well.
8450 This will be the case if the article has both been mailed and posted."
8451   (interactive)
8452   (let ((id (mail-header-references (gnus-summary-article-header)))
8453         (gnus-override-method (car (gnus-refer-article-methods))))
8454     (if (gnus-request-head id "")
8455         (gnus-message 2 "The current message was found on %s"
8456                       gnus-override-method)
8457       (gnus-message 2 "The current message couldn't be found on %s"
8458                     gnus-override-method)
8459       nil)))
8460
8461 (defun gnus-summary-expire-articles (&optional now)
8462   "Expire all articles that are marked as expirable in the current group."
8463   (interactive)
8464   (when (gnus-check-backend-function
8465          'request-expire-articles gnus-newsgroup-name)
8466     ;; This backend supports expiry.
8467     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8468            (expirable (if total
8469                           (progn
8470                             ;; We need to update the info for
8471                             ;; this group for `gnus-list-of-read-articles'
8472                             ;; to give us the right answer.
8473                             (gnus-run-hooks 'gnus-exit-group-hook)
8474                             (gnus-summary-update-info)
8475                             (gnus-list-of-read-articles gnus-newsgroup-name))
8476                         (setq gnus-newsgroup-expirable
8477                               (sort gnus-newsgroup-expirable '<))))
8478            (expiry-wait (if now 'immediate
8479                           (gnus-group-find-parameter
8480                            gnus-newsgroup-name 'expiry-wait)))
8481            (nnmail-expiry-target
8482             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8483                 nnmail-expiry-target))
8484            es)
8485       (when expirable
8486         ;; There are expirable articles in this group, so we run them
8487         ;; through the expiry process.
8488         (gnus-message 6 "Expiring articles...")
8489         (unless (gnus-check-group gnus-newsgroup-name)
8490           (error "Can't open server for %s" gnus-newsgroup-name))
8491         ;; The list of articles that weren't expired is returned.
8492         (save-excursion
8493           (if expiry-wait
8494               (let ((nnmail-expiry-wait-function nil)
8495                     (nnmail-expiry-wait expiry-wait))
8496                 (setq es (gnus-request-expire-articles
8497                           expirable gnus-newsgroup-name)))
8498             (setq es (gnus-request-expire-articles
8499                       expirable gnus-newsgroup-name)))
8500           (unless total
8501             (setq gnus-newsgroup-expirable es))
8502           ;; We go through the old list of expirable, and mark all
8503           ;; really expired articles as nonexistent.
8504           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8505             (let ((gnus-use-cache nil))
8506               (while expirable
8507                 (unless (memq (car expirable) es)
8508                   (when (gnus-data-find (car expirable))
8509                     (gnus-summary-mark-article
8510                      (car expirable) gnus-canceled-mark)))
8511                 (setq expirable (cdr expirable))))))
8512         (gnus-message 6 "Expiring articles...done")))))
8513
8514 (defun gnus-summary-expire-articles-now ()
8515   "Expunge all expirable articles in the current group.
8516 This means that *all* articles that are marked as expirable will be
8517 deleted forever, right now."
8518   (interactive)
8519   (or gnus-expert-user
8520       (gnus-yes-or-no-p
8521        "Are you really, really, really sure you want to delete all these messages? ")
8522       (error "Phew!"))
8523   (gnus-summary-expire-articles t))
8524
8525 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8526 (defun gnus-summary-delete-article (&optional n)
8527   "Delete the N next (mail) articles.
8528 This command actually deletes articles.  This is not a marking
8529 command.  The article will disappear forever from your life, never to
8530 return.
8531 If N is negative, delete backwards.
8532 If N is nil and articles have been marked with the process mark,
8533 delete these instead."
8534   (interactive "P")
8535   (unless (gnus-check-backend-function 'request-expire-articles
8536                                        gnus-newsgroup-name)
8537     (error "The current newsgroup does not support article deletion"))
8538   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8539     (error "Couldn't open server"))
8540   ;; Compute the list of articles to delete.
8541   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8542         not-deleted)
8543     (if (and gnus-novice-user
8544              (not (gnus-yes-or-no-p
8545                    (format "Do you really want to delete %s forever? "
8546                            (if (> (length articles) 1)
8547                                (format "these %s articles" (length articles))
8548                              "this article")))))
8549         ()
8550       ;; Delete the articles.
8551       (setq not-deleted (gnus-request-expire-articles
8552                          articles gnus-newsgroup-name 'force))
8553       (while articles
8554         (gnus-summary-remove-process-mark (car articles))
8555         ;; The backend might not have been able to delete the article
8556         ;; after all.
8557         (unless (memq (car articles) not-deleted)
8558           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8559         (setq articles (cdr articles)))
8560       (when not-deleted
8561         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8562     (gnus-summary-position-point)
8563     (gnus-set-mode-line 'summary)
8564     not-deleted))
8565
8566 (defun gnus-summary-edit-article (&optional force)
8567   "Edit the current article.
8568 This will have permanent effect only in mail groups.
8569 If FORCE is non-nil, allow editing of articles even in read-only
8570 groups."
8571   (interactive "P")
8572   (save-excursion
8573     (set-buffer gnus-summary-buffer)
8574     (let ((mail-parse-charset gnus-newsgroup-charset)
8575           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8576       (gnus-set-global-variables)
8577       (when (and (not force)
8578                  (gnus-group-read-only-p))
8579         (error "The current newsgroup does not support article editing"))
8580       (gnus-summary-show-article t)
8581       (gnus-article-edit-article
8582        'ignore
8583        `(lambda (no-highlight)
8584           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8585                 (message-options message-options)
8586                 (message-options-set-recipient)
8587                 (mail-parse-ignored-charsets
8588                  ',gnus-newsgroup-ignored-charsets))
8589             (gnus-summary-edit-article-done
8590              ,(or (mail-header-references gnus-current-headers) "")
8591              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8592
8593 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8594
8595 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8596                                                  no-highlight)
8597   "Make edits to the current article permanent."
8598   (interactive)
8599   (save-excursion
8600     ;; The buffer restriction contains the entire article if it exists.
8601     (when (article-goto-body)
8602       (let ((lines (count-lines (point) (point-max)))
8603             (length (- (point-max) (point)))
8604             (case-fold-search t)
8605             (body (copy-marker (point))))
8606         (goto-char (point-min))
8607         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8608           (delete-region (match-beginning 1) (match-end 1))
8609           (insert (number-to-string length)))
8610         (goto-char (point-min))
8611         (when (re-search-forward
8612                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8613           (delete-region (match-beginning 1) (match-end 1))
8614           (insert (number-to-string length)))
8615         (goto-char (point-min))
8616         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8617           (delete-region (match-beginning 1) (match-end 1))
8618           (insert (number-to-string lines))))))
8619   ;; Replace the article.
8620   (let ((buf (current-buffer)))
8621     (with-temp-buffer
8622       (insert-buffer-substring buf)
8623
8624       (if (and (not read-only)
8625                (not (gnus-request-replace-article
8626                      (cdr gnus-article-current) (car gnus-article-current)
8627                      (current-buffer) t)))
8628           (error "Couldn't replace article")
8629         ;; Update the summary buffer.
8630         (if (and references
8631                  (equal (message-tokenize-header references " ")
8632                         (message-tokenize-header
8633                          (or (message-fetch-field "references") "") " ")))
8634             ;; We only have to update this line.
8635             (save-excursion
8636               (save-restriction
8637                 (message-narrow-to-head)
8638                 (let ((head (buffer-string))
8639                       header)
8640                   (with-temp-buffer
8641                     (insert (format "211 %d Article retrieved.\n"
8642                                     (cdr gnus-article-current)))
8643                     (insert head)
8644                     (insert ".\n")
8645                     (let ((nntp-server-buffer (current-buffer)))
8646                       (setq header (car (gnus-get-newsgroup-headers
8647                                          (save-excursion
8648                                            (set-buffer gnus-summary-buffer)
8649                                            gnus-newsgroup-dependencies)
8650                                          t))))
8651                     (save-excursion
8652                       (set-buffer gnus-summary-buffer)
8653                       (gnus-data-set-header
8654                        (gnus-data-find (cdr gnus-article-current))
8655                        header)
8656                       (gnus-summary-update-article-line
8657                        (cdr gnus-article-current) header)
8658                       (if (gnus-summary-goto-subject
8659                            (cdr gnus-article-current) nil t)
8660                           (gnus-summary-update-secondary-mark
8661                            (cdr gnus-article-current))))))))
8662           ;; Update threads.
8663           (set-buffer (or buffer gnus-summary-buffer))
8664           (gnus-summary-update-article (cdr gnus-article-current))
8665           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8666               (gnus-summary-update-secondary-mark
8667                (cdr gnus-article-current))))
8668         ;; Prettify the article buffer again.
8669         (unless no-highlight
8670           (save-excursion
8671             (set-buffer gnus-article-buffer)
8672             ;;;!!! Fix this -- article should be rehighlighted.
8673             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8674             (set-buffer gnus-original-article-buffer)
8675             (gnus-request-article
8676              (cdr gnus-article-current)
8677              (car gnus-article-current) (current-buffer))))
8678         ;; Prettify the summary buffer line.
8679         (when (gnus-visual-p 'summary-highlight 'highlight)
8680           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8681
8682 (defun gnus-summary-edit-wash (key)
8683   "Perform editing command KEY in the article buffer."
8684   (interactive
8685    (list
8686     (progn
8687       (message "%s" (concat (this-command-keys) "- "))
8688       (read-char))))
8689   (message "")
8690   (gnus-summary-edit-article)
8691   (execute-kbd-macro (concat (this-command-keys) key))
8692   (gnus-article-edit-done))
8693
8694 ;;; Respooling
8695
8696 (defun gnus-summary-respool-query (&optional silent trace)
8697   "Query where the respool algorithm would put this article."
8698   (interactive)
8699   (let (gnus-mark-article-hook)
8700     (gnus-summary-select-article)
8701     (save-excursion
8702       (set-buffer gnus-original-article-buffer)
8703       (save-restriction
8704         (message-narrow-to-head)
8705         (let ((groups (nnmail-article-group 'identity trace)))
8706           (unless silent
8707             (if groups
8708                 (message "This message would go to %s"
8709                          (mapconcat 'car groups ", "))
8710               (message "This message would go to no groups"))
8711             groups))))))
8712
8713 (defun gnus-summary-respool-trace ()
8714   "Trace where the respool algorithm would put this article.
8715 Display a buffer showing all fancy splitting patterns which matched."
8716   (interactive)
8717   (gnus-summary-respool-query nil t))
8718
8719 ;; Summary marking commands.
8720
8721 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8722   "Mark articles which has the same subject as read, and then select the next.
8723 If UNMARK is positive, remove any kind of mark.
8724 If UNMARK is negative, tick articles."
8725   (interactive "P")
8726   (when unmark
8727     (setq unmark (prefix-numeric-value unmark)))
8728   (let ((count
8729          (gnus-summary-mark-same-subject
8730           (gnus-summary-article-subject) unmark)))
8731     ;; Select next unread article.  If auto-select-same mode, should
8732     ;; select the first unread article.
8733     (gnus-summary-next-article t (and gnus-auto-select-same
8734                                       (gnus-summary-article-subject)))
8735     (gnus-message 7 "%d article%s marked as %s"
8736                   count (if (= count 1) " is" "s are")
8737                   (if unmark "unread" "read"))))
8738
8739 (defun gnus-summary-kill-same-subject (&optional unmark)
8740   "Mark articles which has the same subject as read.
8741 If UNMARK is positive, remove any kind of mark.
8742 If UNMARK is negative, tick articles."
8743   (interactive "P")
8744   (when unmark
8745     (setq unmark (prefix-numeric-value unmark)))
8746   (let ((count
8747          (gnus-summary-mark-same-subject
8748           (gnus-summary-article-subject) unmark)))
8749     ;; If marked as read, go to next unread subject.
8750     (when (null unmark)
8751       ;; Go to next unread subject.
8752       (gnus-summary-next-subject 1 t))
8753     (gnus-message 7 "%d articles are marked as %s"
8754                   count (if unmark "unread" "read"))))
8755
8756 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8757   "Mark articles with same SUBJECT as read, and return marked number.
8758 If optional argument UNMARK is positive, remove any kinds of marks.
8759 If optional argument UNMARK is negative, mark articles as unread instead."
8760   (let ((count 1))
8761     (save-excursion
8762       (cond
8763        ((null unmark)                   ; Mark as read.
8764         (while (and
8765                 (progn
8766                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8767                   (gnus-summary-show-thread) t)
8768                 (gnus-summary-find-subject subject))
8769           (setq count (1+ count))))
8770        ((> unmark 0)                    ; Tick.
8771         (while (and
8772                 (progn
8773                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8774                   (gnus-summary-show-thread) t)
8775                 (gnus-summary-find-subject subject))
8776           (setq count (1+ count))))
8777        (t                               ; Mark as unread.
8778         (while (and
8779                 (progn
8780                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8781                   (gnus-summary-show-thread) t)
8782                 (gnus-summary-find-subject subject))
8783           (setq count (1+ count)))))
8784       (gnus-set-mode-line 'summary)
8785       ;; Return the number of marked articles.
8786       count)))
8787
8788 (defun gnus-summary-mark-as-processable (n &optional unmark)
8789   "Set the process mark on the next N articles.
8790 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8791 the process mark instead.  The difference between N and the actual
8792 number of articles marked is returned."
8793   (interactive "P")
8794   (if (and (null n) (gnus-region-active-p))
8795       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8796     (setq n (prefix-numeric-value n))
8797     (let ((backward (< n 0))
8798           (n (abs n)))
8799       (while (and
8800               (> n 0)
8801               (if unmark
8802                   (gnus-summary-remove-process-mark
8803                    (gnus-summary-article-number))
8804                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8805               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8806         (setq n (1- n)))
8807       (when (/= 0 n)
8808         (gnus-message 7 "No more articles"))
8809       (gnus-summary-recenter)
8810       (gnus-summary-position-point)
8811       n)))
8812
8813 (defun gnus-summary-unmark-as-processable (n)
8814   "Remove the process mark from the next N articles.
8815 If N is negative, unmark backward instead.  The difference between N and
8816 the actual number of articles unmarked is returned."
8817   (interactive "P")
8818   (gnus-summary-mark-as-processable n t))
8819
8820 (defun gnus-summary-unmark-all-processable ()
8821   "Remove the process mark from all articles."
8822   (interactive)
8823   (save-excursion
8824     (while gnus-newsgroup-processable
8825       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8826   (gnus-summary-position-point))
8827
8828 (defun gnus-summary-add-mark (article type)
8829   "Mark ARTICLE with a mark of TYPE."
8830   (let ((vtype (car (assq type gnus-article-mark-lists)))
8831         var)
8832     (if (not vtype)
8833         (error "No such mark type: %s" type)
8834       (setq var (intern (format "gnus-newsgroup-%s" type)))
8835       (set var (cons article (symbol-value var)))
8836       (if (memq type '(processable cached replied forwarded recent saved))
8837           (gnus-summary-update-secondary-mark article)
8838         ;;; !!! This is bogus.  We should find out what primary
8839         ;;; !!! mark we want to set.
8840         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8841
8842 (defun gnus-summary-mark-as-expirable (n)
8843   "Mark N articles forward as expirable.
8844 If N is negative, mark backward instead.  The difference between N and
8845 the actual number of articles marked is returned."
8846   (interactive "p")
8847   (gnus-summary-mark-forward n gnus-expirable-mark))
8848
8849 (defun gnus-summary-mark-article-as-replied (article)
8850   "Mark ARTICLE as replied to and update the summary line.
8851 ARTICLE can also be a list of articles."
8852   (interactive (list (gnus-summary-article-number)))
8853   (let ((articles (if (listp article) article (list article))))
8854     (dolist (article articles)
8855       (push article gnus-newsgroup-replied)
8856       (let ((buffer-read-only nil))
8857         (when (gnus-summary-goto-subject article nil t)
8858           (gnus-summary-update-secondary-mark article))))))
8859
8860 (defun gnus-summary-mark-article-as-forwarded (article)
8861   "Mark ARTICLE as forwarded and update the summary line.
8862 ARTICLE can also be a list of articles."
8863   (let ((articles (if (listp article) article (list article))))
8864     (dolist (article articles)
8865       (push article gnus-newsgroup-forwarded)
8866       (let ((buffer-read-only nil))
8867         (when (gnus-summary-goto-subject article nil t)
8868           (gnus-summary-update-secondary-mark article))))))
8869
8870 (defun gnus-summary-set-bookmark (article)
8871   "Set a bookmark in current article."
8872   (interactive (list (gnus-summary-article-number)))
8873   (when (or (not (get-buffer gnus-article-buffer))
8874             (not gnus-current-article)
8875             (not gnus-article-current)
8876             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8877     (error "No current article selected"))
8878   ;; Remove old bookmark, if one exists.
8879   (let ((old (assq article gnus-newsgroup-bookmarks)))
8880     (when old
8881       (setq gnus-newsgroup-bookmarks
8882             (delq old gnus-newsgroup-bookmarks))))
8883   ;; Set the new bookmark, which is on the form
8884   ;; (article-number . line-number-in-body).
8885   (push
8886    (cons article
8887          (save-excursion
8888            (set-buffer gnus-article-buffer)
8889            (count-lines
8890             (min (point)
8891                  (save-excursion
8892                    (goto-char (point-min))
8893                    (search-forward "\n\n" nil t)
8894                    (point)))
8895             (point))))
8896    gnus-newsgroup-bookmarks)
8897   (gnus-message 6 "A bookmark has been added to the current article."))
8898
8899 (defun gnus-summary-remove-bookmark (article)
8900   "Remove the bookmark from the current article."
8901   (interactive (list (gnus-summary-article-number)))
8902   ;; Remove old bookmark, if one exists.
8903   (let ((old (assq article gnus-newsgroup-bookmarks)))
8904     (if old
8905         (progn
8906           (setq gnus-newsgroup-bookmarks
8907                 (delq old gnus-newsgroup-bookmarks))
8908           (gnus-message 6 "Removed bookmark."))
8909       (gnus-message 6 "No bookmark in current article."))))
8910
8911 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8912 (defun gnus-summary-mark-as-dormant (n)
8913   "Mark N articles forward as dormant.
8914 If N is negative, mark backward instead.  The difference between N and
8915 the actual number of articles marked is returned."
8916   (interactive "p")
8917   (gnus-summary-mark-forward n gnus-dormant-mark))
8918
8919 (defun gnus-summary-set-process-mark (article)
8920   "Set the process mark on ARTICLE and update the summary line."
8921   (setq gnus-newsgroup-processable
8922         (cons article
8923               (delq article gnus-newsgroup-processable)))
8924   (when (gnus-summary-goto-subject article)
8925     (gnus-summary-show-thread)
8926     (gnus-summary-goto-subject article)
8927     (gnus-summary-update-secondary-mark article)))
8928
8929 (defun gnus-summary-remove-process-mark (article)
8930   "Remove the process mark from ARTICLE and update the summary line."
8931   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8932   (when (gnus-summary-goto-subject article)
8933     (gnus-summary-show-thread)
8934     (gnus-summary-goto-subject article)
8935     (gnus-summary-update-secondary-mark article)))
8936
8937 (defun gnus-summary-set-saved-mark (article)
8938   "Set the process mark on ARTICLE and update the summary line."
8939   (push article gnus-newsgroup-saved)
8940   (when (gnus-summary-goto-subject article)
8941     (gnus-summary-update-secondary-mark article)))
8942
8943 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8944   "Mark N articles as read forwards.
8945 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8946 The difference between N and the actual number of articles marked is
8947 returned.
8948 Iff NO-EXPIRE, auto-expiry will be inhibited."
8949   (interactive "p")
8950   (gnus-summary-show-thread)
8951   (let ((backward (< n 0))
8952         (gnus-summary-goto-unread
8953          (and gnus-summary-goto-unread
8954               (not (eq gnus-summary-goto-unread 'never))
8955               (not (memq mark (list gnus-unread-mark
8956                                     gnus-ticked-mark gnus-dormant-mark)))))
8957         (n (abs n))
8958         (mark (or mark gnus-del-mark)))
8959     (while (and (> n 0)
8960                 (gnus-summary-mark-article nil mark no-expire)
8961                 (zerop (gnus-summary-next-subject
8962                         (if backward -1 1)
8963                         (and gnus-summary-goto-unread
8964                              (not (eq gnus-summary-goto-unread 'never)))
8965                         t)))
8966       (setq n (1- n)))
8967     (when (/= 0 n)
8968       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8969     (gnus-summary-recenter)
8970     (gnus-summary-position-point)
8971     (gnus-set-mode-line 'summary)
8972     n))
8973
8974 (defun gnus-summary-mark-article-as-read (mark)
8975   "Mark the current article quickly as read with MARK."
8976   (let ((article (gnus-summary-article-number)))
8977     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8978     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8979     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8980     (push (cons article mark) gnus-newsgroup-reads)
8981     ;; Possibly remove from cache, if that is used.
8982     (when gnus-use-cache
8983       (gnus-cache-enter-remove-article article))
8984     ;; Allow the backend to change the mark.
8985     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8986     ;; Check for auto-expiry.
8987     (when (and gnus-newsgroup-auto-expire
8988                (memq mark gnus-auto-expirable-marks))
8989       (setq mark gnus-expirable-mark)
8990       ;; Let the backend know about the mark change.
8991       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8992       (push article gnus-newsgroup-expirable))
8993     ;; Set the mark in the buffer.
8994     (gnus-summary-update-mark mark 'unread)
8995     t))
8996
8997 (defun gnus-summary-mark-article-as-unread (mark)
8998   "Mark the current article quickly as unread with MARK."
8999   (let* ((article (gnus-summary-article-number))
9000          (old-mark (gnus-summary-article-mark article)))
9001     ;; Allow the backend to change the mark.
9002     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9003     (if (eq mark old-mark)
9004         t
9005       (if (<= article 0)
9006           (progn
9007             (gnus-error 1 "Can't mark negative article numbers")
9008             nil)
9009         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9010         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9011         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9012         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9013         (cond ((= mark gnus-ticked-mark)
9014                (push article gnus-newsgroup-marked))
9015               ((= mark gnus-dormant-mark)
9016                (push article gnus-newsgroup-dormant))
9017               (t
9018                (push article gnus-newsgroup-unreads)))
9019         (gnus-pull article gnus-newsgroup-reads)
9020
9021         ;; See whether the article is to be put in the cache.
9022         (and gnus-use-cache
9023              (vectorp (gnus-summary-article-header article))
9024              (save-excursion
9025                (gnus-cache-possibly-enter-article
9026                 gnus-newsgroup-name article
9027                 (gnus-summary-article-header article)
9028                 (= mark gnus-ticked-mark)
9029                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9030
9031         ;; Fix the mark.
9032         (gnus-summary-update-mark mark 'unread)
9033         t))))
9034
9035 (defun gnus-summary-mark-article (&optional article mark no-expire)
9036   "Mark ARTICLE with MARK.  MARK can be any character.
9037 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9038 `??' (dormant) and `?E' (expirable).
9039 If MARK is nil, then the default character `?r' is used.
9040 If ARTICLE is nil, then the article on the current line will be
9041 marked.
9042 Iff NO-EXPIRE, auto-expiry will be inhibited."
9043   ;; The mark might be a string.
9044   (when (stringp mark)
9045     (setq mark (aref mark 0)))
9046   ;; If no mark is given, then we check auto-expiring.
9047   (when (null mark)
9048     (setq mark gnus-del-mark))
9049   (when (and (not no-expire)
9050              gnus-newsgroup-auto-expire
9051              (memq mark gnus-auto-expirable-marks))
9052     (setq mark gnus-expirable-mark))
9053   (let ((article (or article (gnus-summary-article-number)))
9054         (old-mark (gnus-summary-article-mark article)))
9055     ;; Allow the backend to change the mark.
9056     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9057     (if (eq mark old-mark)
9058         t
9059       (unless article
9060         (error "No article on current line"))
9061       (if (not (if (or (= mark gnus-unread-mark)
9062                        (= mark gnus-ticked-mark)
9063                        (= mark gnus-dormant-mark))
9064                    (gnus-mark-article-as-unread article mark)
9065                  (gnus-mark-article-as-read article mark)))
9066           t
9067         ;; See whether the article is to be put in the cache.
9068         (and gnus-use-cache
9069              (not (= mark gnus-canceled-mark))
9070              (vectorp (gnus-summary-article-header article))
9071              (save-excursion
9072                (gnus-cache-possibly-enter-article
9073                 gnus-newsgroup-name article
9074                 (gnus-summary-article-header article)
9075                 (= mark gnus-ticked-mark)
9076                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9077
9078         (when (gnus-summary-goto-subject article nil t)
9079           (let ((buffer-read-only nil))
9080             (gnus-summary-show-thread)
9081             ;; Fix the mark.
9082             (gnus-summary-update-mark mark 'unread)
9083             t))))))
9084
9085 (defun gnus-summary-update-secondary-mark (article)
9086   "Update the secondary (read, process, cache) mark."
9087   (gnus-summary-update-mark
9088    (cond ((memq article gnus-newsgroup-processable)
9089           gnus-process-mark)
9090          ((memq article gnus-newsgroup-cached)
9091           gnus-cached-mark)
9092          ((memq article gnus-newsgroup-replied)
9093           gnus-replied-mark)
9094          ((memq article gnus-newsgroup-forwarded)
9095           gnus-forwarded-mark)
9096          ((memq article gnus-newsgroup-saved)
9097           gnus-saved-mark)
9098          ((memq article gnus-newsgroup-recent)
9099           gnus-recent-mark)
9100          ((memq article gnus-newsgroup-unseen)
9101           gnus-unseen-mark)
9102          (t gnus-no-mark))
9103    'replied)
9104   (when (gnus-visual-p 'summary-highlight 'highlight)
9105     (gnus-run-hooks 'gnus-summary-update-hook))
9106   t)
9107
9108 (defun gnus-summary-update-mark (mark type)
9109   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9110         (buffer-read-only nil))
9111     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9112     (when forward
9113       (when (looking-at "\r")
9114         (incf forward))
9115       (when (<= (+ forward (point)) (point-max))
9116         ;; Go to the right position on the line.
9117         (goto-char (+ forward (point)))
9118         ;; Replace the old mark with the new mark.
9119         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9120         ;; Optionally update the marks by some user rule.
9121         (when (eq type 'unread)
9122           (gnus-data-set-mark
9123            (gnus-data-find (gnus-summary-article-number)) mark)
9124           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9125
9126 (defun gnus-mark-article-as-read (article &optional mark)
9127   "Enter ARTICLE in the pertinent lists and remove it from others."
9128   ;; Make the article expirable.
9129   (let ((mark (or mark gnus-del-mark)))
9130     (if (= mark gnus-expirable-mark)
9131         (push article gnus-newsgroup-expirable)
9132       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9133     ;; Remove from unread and marked lists.
9134     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9135     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9136     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9137     (push (cons article mark) gnus-newsgroup-reads)
9138     ;; Possibly remove from cache, if that is used.
9139     (when gnus-use-cache
9140       (gnus-cache-enter-remove-article article))
9141     t))
9142
9143 (defun gnus-mark-article-as-unread (article &optional mark)
9144   "Enter ARTICLE in the pertinent lists and remove it from others."
9145   (let ((mark (or mark gnus-ticked-mark)))
9146     (if (<= article 0)
9147         (progn
9148           (gnus-error 1 "Can't mark negative article numbers")
9149           nil)
9150       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9151             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9152             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9153             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9154
9155       ;; Unsuppress duplicates?
9156       (when gnus-suppress-duplicates
9157         (gnus-dup-unsuppress-article article))
9158
9159       (cond ((= mark gnus-ticked-mark)
9160              (push article gnus-newsgroup-marked))
9161             ((= mark gnus-dormant-mark)
9162              (push article gnus-newsgroup-dormant))
9163             (t
9164              (push article gnus-newsgroup-unreads)))
9165       (gnus-pull article gnus-newsgroup-reads)
9166       t)))
9167
9168 (defalias 'gnus-summary-mark-as-unread-forward
9169   'gnus-summary-tick-article-forward)
9170 (make-obsolete 'gnus-summary-mark-as-unread-forward
9171                'gnus-summary-tick-article-forward)
9172 (defun gnus-summary-tick-article-forward (n)
9173   "Tick N articles forwards.
9174 If N is negative, tick backwards instead.
9175 The difference between N and the number of articles ticked is returned."
9176   (interactive "p")
9177   (gnus-summary-mark-forward n gnus-ticked-mark))
9178
9179 (defalias 'gnus-summary-mark-as-unread-backward
9180   'gnus-summary-tick-article-backward)
9181 (make-obsolete 'gnus-summary-mark-as-unread-backward
9182                'gnus-summary-tick-article-backward)
9183 (defun gnus-summary-tick-article-backward (n)
9184   "Tick N articles backwards.
9185 The difference between N and the number of articles ticked is returned."
9186   (interactive "p")
9187   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9188
9189 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9190 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9191 (defun gnus-summary-tick-article (&optional article clear-mark)
9192   "Mark current article as unread.
9193 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9194 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9195   (interactive)
9196   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9197                                        gnus-ticked-mark)))
9198
9199 (defun gnus-summary-mark-as-read-forward (n)
9200   "Mark N articles as read forwards.
9201 If N is negative, mark backwards instead.
9202 The difference between N and the actual number of articles marked is
9203 returned."
9204   (interactive "p")
9205   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9206
9207 (defun gnus-summary-mark-as-read-backward (n)
9208   "Mark the N articles as read backwards.
9209 The difference between N and the actual number of articles marked is
9210 returned."
9211   (interactive "p")
9212   (gnus-summary-mark-forward
9213    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9214
9215 (defun gnus-summary-mark-as-read (&optional article mark)
9216   "Mark current article as read.
9217 ARTICLE specifies the article to be marked as read.
9218 MARK specifies a string to be inserted at the beginning of the line."
9219   (gnus-summary-mark-article article mark))
9220
9221 (defun gnus-summary-clear-mark-forward (n)
9222   "Clear marks from N articles forward.
9223 If N is negative, clear backward instead.
9224 The difference between N and the number of marks cleared is returned."
9225   (interactive "p")
9226   (gnus-summary-mark-forward n gnus-unread-mark))
9227
9228 (defun gnus-summary-clear-mark-backward (n)
9229   "Clear marks from N articles backward.
9230 The difference between N and the number of marks cleared is returned."
9231   (interactive "p")
9232   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9233
9234 (defun gnus-summary-mark-unread-as-read ()
9235   "Intended to be used by `gnus-summary-mark-article-hook'."
9236   (when (memq gnus-current-article gnus-newsgroup-unreads)
9237     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9238
9239 (defun gnus-summary-mark-read-and-unread-as-read ()
9240   "Intended to be used by `gnus-summary-mark-article-hook'."
9241   (let ((mark (gnus-summary-article-mark)))
9242     (when (or (gnus-unread-mark-p mark)
9243               (gnus-read-mark-p mark))
9244       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9245
9246 (defun gnus-summary-mark-unread-as-ticked ()
9247   "Intended to be used by `gnus-summary-mark-article-hook'."
9248   (when (memq gnus-current-article gnus-newsgroup-unreads)
9249     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9250
9251 (defun gnus-summary-mark-region-as-read (point mark all)
9252   "Mark all unread articles between point and mark as read.
9253 If given a prefix, mark all articles between point and mark as read,
9254 even ticked and dormant ones."
9255   (interactive "r\nP")
9256   (save-excursion
9257     (let (article)
9258       (goto-char point)
9259       (beginning-of-line)
9260       (while (and
9261               (< (point) mark)
9262               (progn
9263                 (when (or all
9264                           (memq (setq article (gnus-summary-article-number))
9265                                 gnus-newsgroup-unreads))
9266                   (gnus-summary-mark-article article gnus-del-mark))
9267                 t)
9268               (gnus-summary-find-next))))))
9269
9270 (defun gnus-summary-mark-below (score mark)
9271   "Mark articles with score less than SCORE with MARK."
9272   (interactive "P\ncMark: ")
9273   (setq score (if score
9274                   (prefix-numeric-value score)
9275                 (or gnus-summary-default-score 0)))
9276   (save-excursion
9277     (set-buffer gnus-summary-buffer)
9278     (goto-char (point-min))
9279     (while
9280         (progn
9281           (and (< (gnus-summary-article-score) score)
9282                (gnus-summary-mark-article nil mark))
9283           (gnus-summary-find-next)))))
9284
9285 (defun gnus-summary-kill-below (&optional score)
9286   "Mark articles with score below SCORE as read."
9287   (interactive "P")
9288   (gnus-summary-mark-below score gnus-killed-mark))
9289
9290 (defun gnus-summary-clear-above (&optional score)
9291   "Clear all marks from articles with score above SCORE."
9292   (interactive "P")
9293   (gnus-summary-mark-above score gnus-unread-mark))
9294
9295 (defun gnus-summary-tick-above (&optional score)
9296   "Tick all articles with score above SCORE."
9297   (interactive "P")
9298   (gnus-summary-mark-above score gnus-ticked-mark))
9299
9300 (defun gnus-summary-mark-above (score mark)
9301   "Mark articles with score over SCORE with MARK."
9302   (interactive "P\ncMark: ")
9303   (setq score (if score
9304                   (prefix-numeric-value score)
9305                 (or gnus-summary-default-score 0)))
9306   (save-excursion
9307     (set-buffer gnus-summary-buffer)
9308     (goto-char (point-min))
9309     (while (and (progn
9310                   (when (> (gnus-summary-article-score) score)
9311                     (gnus-summary-mark-article nil mark))
9312                   t)
9313                 (gnus-summary-find-next)))))
9314
9315 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9316 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9317 (defun gnus-summary-limit-include-expunged (&optional no-error)
9318   "Display all the hidden articles that were expunged for low scores."
9319   (interactive)
9320   (let ((buffer-read-only nil))
9321     (let ((scored gnus-newsgroup-scored)
9322           headers h)
9323       (while scored
9324         (unless (gnus-summary-article-header (caar scored))
9325           (and (setq h (gnus-number-to-header (caar scored)))
9326                (< (cdar scored) gnus-summary-expunge-below)
9327                (push h headers)))
9328         (setq scored (cdr scored)))
9329       (if (not headers)
9330           (when (not no-error)
9331             (error "No expunged articles hidden"))
9332         (goto-char (point-min))
9333         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9334         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9335         (mapcar (lambda (x) (push (mail-header-number x)
9336                                   gnus-newsgroup-limit))
9337                 headers)
9338         (gnus-summary-prepare-unthreaded (nreverse headers))
9339         (goto-char (point-min))
9340         (gnus-summary-position-point)
9341         t))))
9342
9343 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9344   "Mark all unread articles in this newsgroup as read.
9345 If prefix argument ALL is non-nil, ticked and dormant articles will
9346 also be marked as read.
9347 If QUIETLY is non-nil, no questions will be asked.
9348 If TO-HERE is non-nil, it should be a point in the buffer.  All
9349 articles before (after, if REVERSE is set) this point will be marked as read.
9350 Note that this function will only catch up the unread article
9351 in the current summary buffer limitation.
9352 The number of articles marked as read is returned."
9353   (interactive "P")
9354   (prog1
9355       (save-excursion
9356         (when (or quietly
9357                   (not gnus-interactive-catchup) ;Without confirmation?
9358                   gnus-expert-user
9359                   (gnus-y-or-n-p
9360                    (if all
9361                        "Mark absolutely all articles as read? "
9362                      "Mark all unread articles as read? ")))
9363           (if (and not-mark
9364                    (not gnus-newsgroup-adaptive)
9365                    (not gnus-newsgroup-auto-expire)
9366                    (not gnus-suppress-duplicates)
9367                    (or (not gnus-use-cache)
9368                        (eq gnus-use-cache 'passive)))
9369               (progn
9370                 (when all
9371                   (setq gnus-newsgroup-marked nil
9372                         gnus-newsgroup-dormant nil))
9373                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9374             ;; We actually mark all articles as canceled, which we
9375             ;; have to do when using auto-expiry or adaptive scoring.
9376             (gnus-summary-show-all-threads)
9377             (if (and to-here reverse)
9378                 (progn
9379                   (goto-char to-here)
9380                   (while (and
9381                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9382                           (gnus-summary-find-next (not all) nil nil t))))
9383               (when (gnus-summary-first-subject (not all) t)
9384                 (while (and
9385                         (if to-here (< (point) to-here) t)
9386                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9387                         (gnus-summary-find-next (not all) nil nil t)))))
9388             (gnus-set-mode-line 'summary))
9389           t))
9390     (gnus-summary-position-point)))
9391
9392 (defun gnus-summary-catchup-to-here (&optional all)
9393   "Mark all unticked articles before the current one as read.
9394 If ALL is non-nil, also mark ticked and dormant articles as read."
9395   (interactive "P")
9396   (save-excursion
9397     (gnus-save-hidden-threads
9398       (let ((beg (point)))
9399         ;; We check that there are unread articles.
9400         (when (or all (gnus-summary-find-prev))
9401           (gnus-summary-catchup all t beg)))))
9402   (gnus-summary-position-point))
9403
9404 (defun gnus-summary-catchup-from-here (&optional all)
9405   "Mark all unticked articles after the current one as read.
9406 If ALL is non-nil, also mark ticked and dormant articles as read."
9407   (interactive "P")
9408   (save-excursion
9409     (gnus-save-hidden-threads
9410       (let ((beg (point)))
9411         ;; We check that there are unread articles.
9412         (when (or all (gnus-summary-find-next))
9413           (gnus-summary-catchup all t beg nil t)))))
9414   (gnus-summary-position-point))
9415
9416 (defun gnus-summary-catchup-all (&optional quietly)
9417   "Mark all articles in this newsgroup as read."
9418   (interactive "P")
9419   (gnus-summary-catchup t quietly))
9420
9421 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9422   "Mark all unread articles in this group as read, then exit.
9423 If prefix argument ALL is non-nil, all articles are marked as read.
9424 If QUIETLY is non-nil, no questions will be asked."
9425   (interactive "P")
9426   (when (gnus-summary-catchup all quietly nil 'fast)
9427     ;; Select next newsgroup or exit.
9428     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9429              (eq gnus-auto-select-next 'quietly))
9430         (gnus-summary-next-group nil)
9431       (gnus-summary-exit))))
9432
9433 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9434   "Mark all articles in this newsgroup as read, and then exit."
9435   (interactive "P")
9436   (gnus-summary-catchup-and-exit t quietly))
9437
9438 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9439   "Mark all articles in this group as read and select the next group.
9440 If given a prefix, mark all articles, unread as well as ticked, as
9441 read."
9442   (interactive "P")
9443   (save-excursion
9444     (gnus-summary-catchup all))
9445   (gnus-summary-next-group))
9446
9447 ;;;
9448 ;;; with article
9449 ;;;
9450
9451 (defmacro gnus-with-article (article &rest forms)
9452   "Select ARTICLE and perform FORMS in the original article buffer.
9453 Then replace the article with the result."
9454   `(progn
9455      ;; We don't want the article to be marked as read.
9456      (let (gnus-mark-article-hook)
9457        (gnus-summary-select-article t t nil ,article))
9458      (set-buffer gnus-original-article-buffer)
9459      ,@forms
9460      (if (not (gnus-check-backend-function
9461                'request-replace-article (car gnus-article-current)))
9462          (gnus-message 5 "Read-only group; not replacing")
9463        (unless (gnus-request-replace-article
9464                 ,article (car gnus-article-current)
9465                 (current-buffer) t)
9466          (error "Couldn't replace article")))
9467      ;; The cache and backlog have to be flushed somewhat.
9468      (when gnus-keep-backlog
9469        (gnus-backlog-remove-article
9470         (car gnus-article-current) (cdr gnus-article-current)))
9471      (when gnus-use-cache
9472        (gnus-cache-update-article
9473         (car gnus-article-current) (cdr gnus-article-current)))))
9474
9475 (put 'gnus-with-article 'lisp-indent-function 1)
9476 (put 'gnus-with-article 'edebug-form-spec '(form body))
9477
9478 ;; Thread-based commands.
9479
9480 (defun gnus-summary-articles-in-thread (&optional article)
9481   "Return a list of all articles in the current thread.
9482 If ARTICLE is non-nil, return all articles in the thread that starts
9483 with that article."
9484   (let* ((article (or article (gnus-summary-article-number)))
9485          (data (gnus-data-find-list article))
9486          (top-level (gnus-data-level (car data)))
9487          (top-subject
9488           (cond ((null gnus-thread-operation-ignore-subject)
9489                  (gnus-simplify-subject-re
9490                   (mail-header-subject (gnus-data-header (car data)))))
9491                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9492                  (gnus-simplify-subject-fuzzy
9493                   (mail-header-subject (gnus-data-header (car data)))))
9494                 (t nil)))
9495          (end-point (save-excursion
9496                       (if (gnus-summary-go-to-next-thread)
9497                           (point) (point-max))))
9498          articles)
9499     (while (and data
9500                 (< (gnus-data-pos (car data)) end-point))
9501       (when (or (not top-subject)
9502                 (string= top-subject
9503                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9504                              (gnus-simplify-subject-fuzzy
9505                               (mail-header-subject
9506                                (gnus-data-header (car data))))
9507                            (gnus-simplify-subject-re
9508                             (mail-header-subject
9509                              (gnus-data-header (car data)))))))
9510         (push (gnus-data-number (car data)) articles))
9511       (unless (and (setq data (cdr data))
9512                    (> (gnus-data-level (car data)) top-level))
9513         (setq data nil)))
9514     ;; Return the list of articles.
9515     (nreverse articles)))
9516
9517 (defun gnus-summary-rethread-current ()
9518   "Rethread the thread the current article is part of."
9519   (interactive)
9520   (let* ((gnus-show-threads t)
9521          (article (gnus-summary-article-number))
9522          (id (mail-header-id (gnus-summary-article-header)))
9523          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9524     (unless id
9525       (error "No article on the current line"))
9526     (gnus-rebuild-thread id)
9527     (gnus-summary-goto-subject article)))
9528
9529 (defun gnus-summary-reparent-thread ()
9530   "Make the current article child of the marked (or previous) article.
9531
9532 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9533 is non-nil or the Subject: of both articles are the same."
9534   (interactive)
9535   (unless (not (gnus-group-read-only-p))
9536     (error "The current newsgroup does not support article editing"))
9537   (unless (<= (length gnus-newsgroup-processable) 1)
9538     (error "No more than one article may be marked"))
9539   (save-window-excursion
9540     (let ((gnus-article-buffer " *reparent*")
9541           (current-article (gnus-summary-article-number))
9542           ;; First grab the marked article, otherwise one line up.
9543           (parent-article (if (not (null gnus-newsgroup-processable))
9544                               (car gnus-newsgroup-processable)
9545                             (save-excursion
9546                               (if (eq (forward-line -1) 0)
9547                                   (gnus-summary-article-number)
9548                                 (error "Beginning of summary buffer"))))))
9549       (unless (not (eq current-article parent-article))
9550         (error "An article may not be self-referential"))
9551       (let ((message-id (mail-header-id
9552                          (gnus-summary-article-header parent-article))))
9553         (unless (and message-id (not (equal message-id "")))
9554           (error "No message-id in desired parent"))
9555         (gnus-with-article current-article
9556           (save-restriction
9557             (goto-char (point-min))
9558             (message-narrow-to-head)
9559             (if (re-search-forward "^References: " nil t)
9560                 (progn
9561                   (re-search-forward "^[^ \t]" nil t)
9562                   (forward-line -1)
9563                   (end-of-line)
9564                   (insert " " message-id))
9565               (insert "References: " message-id "\n"))))
9566         (set-buffer gnus-summary-buffer)
9567         (gnus-summary-unmark-all-processable)
9568         (gnus-summary-update-article current-article)
9569         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9570             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9571         (gnus-summary-rethread-current)
9572         (gnus-message 3 "Article %d is now the child of article %d"
9573                       current-article parent-article)))))
9574
9575 (defun gnus-summary-toggle-threads (&optional arg)
9576   "Toggle showing conversation threads.
9577 If ARG is positive number, turn showing conversation threads on."
9578   (interactive "P")
9579   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9580     (setq gnus-show-threads
9581           (if (null arg) (not gnus-show-threads)
9582             (> (prefix-numeric-value arg) 0)))
9583     (gnus-summary-prepare)
9584     (gnus-summary-goto-subject current)
9585     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9586     (gnus-summary-position-point)))
9587
9588 (defun gnus-summary-show-all-threads ()
9589   "Show all threads."
9590   (interactive)
9591   (save-excursion
9592     (let ((buffer-read-only nil))
9593       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9594   (gnus-summary-position-point))
9595
9596 (defun gnus-summary-show-thread ()
9597   "Show thread subtrees.
9598 Returns nil if no thread was there to be shown."
9599   (interactive)
9600   (let ((buffer-read-only nil)
9601         (orig (point))
9602         ;; first goto end then to beg, to have point at beg after let
9603         (end (progn (end-of-line) (point)))
9604         (beg (progn (beginning-of-line) (point))))
9605     (prog1
9606         ;; Any hidden lines here?
9607         (search-forward "\r" end t)
9608       (subst-char-in-region beg end ?\^M ?\n t)
9609       (goto-char orig)
9610       (gnus-summary-position-point))))
9611
9612 (defun gnus-summary-hide-all-threads ()
9613   "Hide all thread subtrees."
9614   (interactive)
9615   (save-excursion
9616     (goto-char (point-min))
9617     (gnus-summary-hide-thread)
9618     (while (zerop (gnus-summary-next-thread 1 t))
9619       (gnus-summary-hide-thread)))
9620   (gnus-summary-position-point))
9621
9622 (defun gnus-summary-hide-thread ()
9623   "Hide thread subtrees.
9624 Returns nil if no threads were there to be hidden."
9625   (interactive)
9626   (let ((buffer-read-only nil)
9627         (start (point))
9628         (article (gnus-summary-article-number)))
9629     (goto-char start)
9630     ;; Go forward until either the buffer ends or the subthread
9631     ;; ends.
9632     (when (and (not (eobp))
9633                (or (zerop (gnus-summary-next-thread 1 t))
9634                    (goto-char (point-max))))
9635       (prog1
9636           (if (and (> (point) start)
9637                    (search-backward "\n" start t))
9638               (progn
9639                 (subst-char-in-region start (point) ?\n ?\^M)
9640                 (gnus-summary-goto-subject article))
9641             (goto-char start)
9642             nil)))))
9643
9644 (defun gnus-summary-go-to-next-thread (&optional previous)
9645   "Go to the same level (or less) next thread.
9646 If PREVIOUS is non-nil, go to previous thread instead.
9647 Return the article number moved to, or nil if moving was impossible."
9648   (let ((level (gnus-summary-thread-level))
9649         (way (if previous -1 1))
9650         (beg (point)))
9651     (forward-line way)
9652     (while (and (not (eobp))
9653                 (< level (gnus-summary-thread-level)))
9654       (forward-line way))
9655     (if (eobp)
9656         (progn
9657           (goto-char beg)
9658           nil)
9659       (setq beg (point))
9660       (prog1
9661           (gnus-summary-article-number)
9662         (goto-char beg)))))
9663
9664 (defun gnus-summary-next-thread (n &optional silent)
9665   "Go to the same level next N'th thread.
9666 If N is negative, search backward instead.
9667 Returns the difference between N and the number of skips actually
9668 done.
9669
9670 If SILENT, don't output messages."
9671   (interactive "p")
9672   (let ((backward (< n 0))
9673         (n (abs n)))
9674     (while (and (> n 0)
9675                 (gnus-summary-go-to-next-thread backward))
9676       (decf n))
9677     (unless silent
9678       (gnus-summary-position-point))
9679     (when (and (not silent) (/= 0 n))
9680       (gnus-message 7 "No more threads"))
9681     n))
9682
9683 (defun gnus-summary-prev-thread (n)
9684   "Go to the same level previous N'th thread.
9685 Returns the difference between N and the number of skips actually
9686 done."
9687   (interactive "p")
9688   (gnus-summary-next-thread (- n)))
9689
9690 (defun gnus-summary-go-down-thread ()
9691   "Go down one level in the current thread."
9692   (let ((children (gnus-summary-article-children)))
9693     (when children
9694       (gnus-summary-goto-subject (car children)))))
9695
9696 (defun gnus-summary-go-up-thread ()
9697   "Go up one level in the current thread."
9698   (let ((parent (gnus-summary-article-parent)))
9699     (when parent
9700       (gnus-summary-goto-subject parent))))
9701
9702 (defun gnus-summary-down-thread (n)
9703   "Go down thread N steps.
9704 If N is negative, go up instead.
9705 Returns the difference between N and how many steps down that were
9706 taken."
9707   (interactive "p")
9708   (let ((up (< n 0))
9709         (n (abs n)))
9710     (while (and (> n 0)
9711                 (if up (gnus-summary-go-up-thread)
9712                   (gnus-summary-go-down-thread)))
9713       (setq n (1- n)))
9714     (gnus-summary-position-point)
9715     (when (/= 0 n)
9716       (gnus-message 7 "Can't go further"))
9717     n))
9718
9719 (defun gnus-summary-up-thread (n)
9720   "Go up thread N steps.
9721 If N is negative, go down instead.
9722 Returns the difference between N and how many steps down that were
9723 taken."
9724   (interactive "p")
9725   (gnus-summary-down-thread (- n)))
9726
9727 (defun gnus-summary-top-thread ()
9728   "Go to the top of the thread."
9729   (interactive)
9730   (while (gnus-summary-go-up-thread))
9731   (gnus-summary-article-number))
9732
9733 (defun gnus-summary-kill-thread (&optional unmark)
9734   "Mark articles under current thread as read.
9735 If the prefix argument is positive, remove any kinds of marks.
9736 If the prefix argument is negative, tick articles instead."
9737   (interactive "P")
9738   (when unmark
9739     (setq unmark (prefix-numeric-value unmark)))
9740   (let ((articles (gnus-summary-articles-in-thread)))
9741     (save-excursion
9742       ;; Expand the thread.
9743       (gnus-summary-show-thread)
9744       ;; Mark all the articles.
9745       (while articles
9746         (gnus-summary-goto-subject (car articles))
9747         (cond ((null unmark)
9748                (gnus-summary-mark-article-as-read gnus-killed-mark))
9749               ((> unmark 0)
9750                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9751               (t
9752                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9753         (setq articles (cdr articles))))
9754     ;; Hide killed subtrees.
9755     (and (null unmark)
9756          gnus-thread-hide-killed
9757          (gnus-summary-hide-thread))
9758     ;; If marked as read, go to next unread subject.
9759     (when (null unmark)
9760       ;; Go to next unread subject.
9761       (gnus-summary-next-subject 1 t)))
9762   (gnus-set-mode-line 'summary))
9763
9764 ;; Summary sorting commands
9765
9766 (defun gnus-summary-sort-by-number (&optional reverse)
9767   "Sort the summary buffer by article number.
9768 Argument REVERSE means reverse order."
9769   (interactive "P")
9770   (gnus-summary-sort 'number reverse))
9771
9772 (defun gnus-summary-sort-by-author (&optional reverse)
9773   "Sort the summary buffer by author name alphabetically.
9774 If `case-fold-search' is non-nil, case of letters is ignored.
9775 Argument REVERSE means reverse order."
9776   (interactive "P")
9777   (gnus-summary-sort 'author reverse))
9778
9779 (defun gnus-summary-sort-by-subject (&optional reverse)
9780   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9781 If `case-fold-search' is non-nil, case of letters is ignored.
9782 Argument REVERSE means reverse order."
9783   (interactive "P")
9784   (gnus-summary-sort 'subject reverse))
9785
9786 (defun gnus-summary-sort-by-date (&optional reverse)
9787   "Sort the summary buffer by date.
9788 Argument REVERSE means reverse order."
9789   (interactive "P")
9790   (gnus-summary-sort 'date reverse))
9791
9792 (defun gnus-summary-sort-by-score (&optional reverse)
9793   "Sort the summary buffer by score.
9794 Argument REVERSE means reverse order."
9795   (interactive "P")
9796   (gnus-summary-sort 'score reverse))
9797
9798 (defun gnus-summary-sort-by-lines (&optional reverse)
9799   "Sort the summary buffer by the number of lines.
9800 Argument REVERSE means reverse order."
9801   (interactive "P")
9802   (gnus-summary-sort 'lines reverse))
9803
9804 (defun gnus-summary-sort-by-chars (&optional reverse)
9805   "Sort the summary buffer by article length.
9806 Argument REVERSE means reverse order."
9807   (interactive "P")
9808   (gnus-summary-sort 'chars reverse))
9809
9810 (defun gnus-summary-sort-by-original (&optional reverse)
9811   "Sort the summary buffer using the default sorting method.
9812 Argument REVERSE means reverse order."
9813   (interactive "P")
9814   (let* ((buffer-read-only)
9815          (gnus-summary-prepare-hook nil))
9816     ;; We do the sorting by regenerating the threads.
9817     (gnus-summary-prepare)
9818     ;; Hide subthreads if needed.
9819     (when (and gnus-show-threads gnus-thread-hide-subtree)
9820       (gnus-summary-hide-all-threads))))
9821
9822 (defun gnus-summary-sort (predicate reverse)
9823   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9824   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9825          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9826          (gnus-thread-sort-functions
9827           (if (not reverse)
9828               thread
9829             `(lambda (t1 t2)
9830                (,thread t2 t1))))
9831          (gnus-sort-gathered-threads-function
9832           gnus-thread-sort-functions)
9833          (gnus-article-sort-functions
9834           (if (not reverse)
9835               article
9836             `(lambda (t1 t2)
9837                (,article t2 t1))))
9838          (buffer-read-only)
9839          (gnus-summary-prepare-hook nil))
9840     ;; We do the sorting by regenerating the threads.
9841     (gnus-summary-prepare)
9842     ;; Hide subthreads if needed.
9843     (when (and gnus-show-threads gnus-thread-hide-subtree)
9844       (gnus-summary-hide-all-threads))))
9845
9846 ;; Summary saving commands.
9847
9848 (defun gnus-summary-save-article (&optional n not-saved)
9849   "Save the current article using the default saver function.
9850 If N is a positive number, save the N next articles.
9851 If N is a negative number, save the N previous articles.
9852 If N is nil and any articles have been marked with the process mark,
9853 save those articles instead.
9854 The variable `gnus-default-article-saver' specifies the saver function."
9855   (interactive "P")
9856   (let* ((articles (gnus-summary-work-articles n))
9857          (save-buffer (save-excursion
9858                         (nnheader-set-temp-buffer " *Gnus Save*")))
9859          (num (length articles))
9860          header file)
9861     (dolist (article articles)
9862       (setq header (gnus-summary-article-header article))
9863       (if (not (vectorp header))
9864           ;; This is a pseudo-article.
9865           (if (assq 'name header)
9866               (gnus-copy-file (cdr (assq 'name header)))
9867             (gnus-message 1 "Article %d is unsaveable" article))
9868         ;; This is a real article.
9869         (save-window-excursion
9870           (gnus-summary-select-article t nil nil article))
9871         (save-excursion
9872           (set-buffer save-buffer)
9873           (erase-buffer)
9874           (insert-buffer-substring gnus-original-article-buffer))
9875         (setq file (gnus-article-save save-buffer file num))
9876         (gnus-summary-remove-process-mark article)
9877         (unless not-saved
9878           (gnus-summary-set-saved-mark article))))
9879     (gnus-kill-buffer save-buffer)
9880     (gnus-summary-position-point)
9881     (gnus-set-mode-line 'summary)
9882     n))
9883
9884 (defun gnus-summary-pipe-output (&optional arg)
9885   "Pipe the current article to a subprocess.
9886 If N is a positive number, pipe the N next articles.
9887 If N is a negative number, pipe the N previous articles.
9888 If N is nil and any articles have been marked with the process mark,
9889 pipe those articles instead."
9890   (interactive "P")
9891   (require 'gnus-art)
9892   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9893     (gnus-summary-save-article arg t))
9894   (let ((buffer (get-buffer "*Shell Command Output*")))
9895     (if (and buffer
9896              (with-current-buffer buffer (> (point-max) (point-min))))
9897         (gnus-configure-windows 'pipe))))
9898
9899 (defun gnus-summary-save-article-mail (&optional arg)
9900   "Append the current article to an mail file.
9901 If N is a positive number, save the N next articles.
9902 If N is a negative number, save the N previous articles.
9903 If N is nil and any articles have been marked with the process mark,
9904 save those articles instead."
9905   (interactive "P")
9906   (require 'gnus-art)
9907   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9908     (gnus-summary-save-article arg)))
9909
9910 (defun gnus-summary-save-article-rmail (&optional arg)
9911   "Append the current article to an rmail file.
9912 If N is a positive number, save the N next articles.
9913 If N is a negative number, save the N previous articles.
9914 If N is nil and any articles have been marked with the process mark,
9915 save those articles instead."
9916   (interactive "P")
9917   (require 'gnus-art)
9918   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9919     (gnus-summary-save-article arg)))
9920
9921 (defun gnus-summary-save-article-file (&optional arg)
9922   "Append the current article to a file.
9923 If N is a positive number, save the N next articles.
9924 If N is a negative number, save the N previous articles.
9925 If N is nil and any articles have been marked with the process mark,
9926 save those articles instead."
9927   (interactive "P")
9928   (require 'gnus-art)
9929   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9930     (gnus-summary-save-article arg)))
9931
9932 (defun gnus-summary-write-article-file (&optional arg)
9933   "Write the current article to a file, deleting the previous file.
9934 If N is a positive number, save the N next articles.
9935 If N is a negative number, save the N previous articles.
9936 If N is nil and any articles have been marked with the process mark,
9937 save those articles instead."
9938   (interactive "P")
9939   (require 'gnus-art)
9940   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9941     (gnus-summary-save-article arg)))
9942
9943 (defun gnus-summary-save-article-body-file (&optional arg)
9944   "Append the current article body to a file.
9945 If N is a positive number, save the N next articles.
9946 If N is a negative number, save the N previous articles.
9947 If N is nil and any articles have been marked with the process mark,
9948 save those articles instead."
9949   (interactive "P")
9950   (require 'gnus-art)
9951   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9952     (gnus-summary-save-article arg)))
9953
9954 (defun gnus-summary-pipe-message (program)
9955   "Pipe the current article through PROGRAM."
9956   (interactive "sProgram: ")
9957   (gnus-summary-select-article)
9958   (let ((mail-header-separator ""))
9959     (gnus-eval-in-buffer-window gnus-article-buffer
9960       (save-restriction
9961         (widen)
9962         (let ((start (window-start))
9963               buffer-read-only)
9964           (message-pipe-buffer-body program)
9965           (set-window-start (get-buffer-window (current-buffer)) start))))))
9966
9967 (defun gnus-get-split-value (methods)
9968   "Return a value based on the split METHODS."
9969   (let (split-name method result match)
9970     (when methods
9971       (save-excursion
9972         (set-buffer gnus-original-article-buffer)
9973         (save-restriction
9974           (nnheader-narrow-to-headers)
9975           (while (and methods (not split-name))
9976             (goto-char (point-min))
9977             (setq method (pop methods))
9978             (setq match (car method))
9979             (when (cond
9980                    ((stringp match)
9981                     ;; Regular expression.
9982                     (ignore-errors
9983                       (re-search-forward match nil t)))
9984                    ((gnus-functionp match)
9985                     ;; Function.
9986                     (save-restriction
9987                       (widen)
9988                       (setq result (funcall match gnus-newsgroup-name))))
9989                    ((consp match)
9990                     ;; Form.
9991                     (save-restriction
9992                       (widen)
9993                       (setq result (eval match)))))
9994               (setq split-name (cdr method))
9995               (cond ((stringp result)
9996                      (push (expand-file-name
9997                             result gnus-article-save-directory)
9998                            split-name))
9999                     ((consp result)
10000                      (setq split-name (append result split-name)))))))))
10001     (nreverse split-name)))
10002
10003 (defun gnus-valid-move-group-p (group)
10004   (and (boundp group)
10005        (symbol-name group)
10006        (symbol-value group)
10007        (gnus-get-function (gnus-find-method-for-group
10008                            (symbol-name group)) 'request-accept-article t)))
10009
10010 (defun gnus-read-move-group-name (prompt default articles prefix)
10011   "Read a group name."
10012   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10013          (minibuffer-confirm-incomplete nil) ; XEmacs
10014          (prom
10015           (format "%s %s to:"
10016                   prompt
10017                   (if (> (length articles) 1)
10018                       (format "these %d articles" (length articles))
10019                     "this article")))
10020          (to-newsgroup
10021           (cond
10022            ((null split-name)
10023             (gnus-completing-read default prom
10024                                   gnus-active-hashtb
10025                                   'gnus-valid-move-group-p
10026                                   nil prefix
10027                                   'gnus-group-history))
10028            ((= 1 (length split-name))
10029             (gnus-completing-read (car split-name) prom
10030                                   gnus-active-hashtb
10031                                   'gnus-valid-move-group-p
10032                                   nil nil
10033                                   'gnus-group-history))
10034            (t
10035             (gnus-completing-read nil prom
10036                                   (mapcar (lambda (el) (list el))
10037                                           (nreverse split-name))
10038                                   nil nil nil
10039                                   'gnus-group-history))))
10040          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10041     (when to-newsgroup
10042       (if (or (string= to-newsgroup "")
10043               (string= to-newsgroup prefix))
10044           (setq to-newsgroup default))
10045       (unless to-newsgroup
10046         (error "No group name entered"))
10047       (or (gnus-active to-newsgroup)
10048           (gnus-activate-group to-newsgroup nil nil to-method)
10049           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10050                                      to-newsgroup))
10051               (or (and (gnus-request-create-group to-newsgroup to-method)
10052                        (gnus-activate-group
10053                         to-newsgroup nil nil to-method)
10054                        (gnus-subscribe-group to-newsgroup))
10055                   (error "Couldn't create group %s" to-newsgroup)))
10056           (error "No such group: %s" to-newsgroup)))
10057     to-newsgroup))
10058
10059 (defun gnus-summary-save-parts (type dir n &optional reverse)
10060   "Save parts matching TYPE to DIR.
10061 If REVERSE, save parts that do not match TYPE."
10062   (interactive
10063    (list (read-string "Save parts of type: "
10064                       (or (car gnus-summary-save-parts-type-history)
10065                           gnus-summary-save-parts-default-mime)
10066                       'gnus-summary-save-parts-type-history)
10067          (setq gnus-summary-save-parts-last-directory
10068                (read-file-name "Save to directory: "
10069                                gnus-summary-save-parts-last-directory
10070                                nil t))
10071          current-prefix-arg))
10072   (gnus-summary-iterate n
10073     (let ((gnus-display-mime-function nil)
10074           (gnus-inhibit-treatment t))
10075       (gnus-summary-select-article))
10076     (save-excursion
10077       (set-buffer gnus-article-buffer)
10078       (let ((handles (or gnus-article-mime-handles
10079                          (mm-dissect-buffer) (mm-uu-dissect))))
10080         (when handles
10081           (gnus-summary-save-parts-1 type dir handles reverse)
10082           (unless gnus-article-mime-handles ;; Don't destroy this case.
10083             (mm-destroy-parts handles)))))))
10084
10085 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10086   (if (stringp (car handle))
10087       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10088               (cdr handle))
10089     (when (if reverse
10090               (not (string-match type (mm-handle-media-type handle)))
10091             (string-match type (mm-handle-media-type handle)))
10092       (let ((file (expand-file-name
10093                    (file-name-nondirectory
10094                     (or
10095                      (mail-content-type-get
10096                       (mm-handle-disposition handle) 'filename)
10097                      (concat gnus-newsgroup-name
10098                              "." (number-to-string
10099                                   (cdr gnus-article-current)))))
10100                    dir)))
10101         (unless (file-exists-p file)
10102           (mm-save-part-to-file handle file))))))
10103
10104 ;; Summary extract commands
10105
10106 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10107   (let ((buffer-read-only nil)
10108         (article (gnus-summary-article-number))
10109         after-article b e)
10110     (unless (gnus-summary-goto-subject article)
10111       (error "No such article: %d" article))
10112     (gnus-summary-position-point)
10113     ;; If all commands are to be bunched up on one line, we collect
10114     ;; them here.
10115     (unless gnus-view-pseudos-separately
10116       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10117             files action)
10118         (while ps
10119           (setq action (cdr (assq 'action (car ps))))
10120           (setq files (list (cdr (assq 'name (car ps)))))
10121           (while (and ps (cdr ps)
10122                       (string= (or action "1")
10123                                (or (cdr (assq 'action (cadr ps))) "2")))
10124             (push (cdr (assq 'name (cadr ps))) files)
10125             (setcdr ps (cddr ps)))
10126           (when files
10127             (when (not (string-match "%s" action))
10128               (push " " files))
10129             (push " " files)
10130             (when (assq 'execute (car ps))
10131               (setcdr (assq 'execute (car ps))
10132                       (funcall (if (string-match "%s" action)
10133                                    'format 'concat)
10134                                action
10135                                (mapconcat
10136                                 (lambda (f)
10137                                   (if (equal f " ")
10138                                       f
10139                                     (gnus-quote-arg-for-sh-or-csh f)))
10140                                 files " ")))))
10141           (setq ps (cdr ps)))))
10142     (if (and gnus-view-pseudos (not not-view))
10143         (while pslist
10144           (when (assq 'execute (car pslist))
10145             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10146                                   (eq gnus-view-pseudos 'not-confirm)))
10147           (setq pslist (cdr pslist)))
10148       (save-excursion
10149         (while pslist
10150           (setq after-article (or (cdr (assq 'article (car pslist)))
10151                                   (gnus-summary-article-number)))
10152           (gnus-summary-goto-subject after-article)
10153           (forward-line 1)
10154           (setq b (point))
10155           (insert "    " (file-name-nondirectory
10156                           (cdr (assq 'name (car pslist))))
10157                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10158           (setq e (point))
10159           (forward-line -1)             ; back to `b'
10160           (gnus-add-text-properties
10161            b (1- e) (list 'gnus-number gnus-reffed-article-number
10162                           gnus-mouse-face-prop gnus-mouse-face))
10163           (gnus-data-enter
10164            after-article gnus-reffed-article-number
10165            gnus-unread-mark b (car pslist) 0 (- e b))
10166           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10167           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10168           (setq pslist (cdr pslist)))))))
10169
10170 (defun gnus-pseudos< (p1 p2)
10171   (let ((c1 (cdr (assq 'action p1)))
10172         (c2 (cdr (assq 'action p2))))
10173     (and c1 c2 (string< c1 c2))))
10174
10175 (defun gnus-request-pseudo-article (props)
10176   (cond ((assq 'execute props)
10177          (gnus-execute-command (cdr (assq 'execute props)))))
10178   (let ((gnus-current-article (gnus-summary-article-number)))
10179     (gnus-run-hooks 'gnus-mark-article-hook)))
10180
10181 (defun gnus-execute-command (command &optional automatic)
10182   (save-excursion
10183     (gnus-article-setup-buffer)
10184     (set-buffer gnus-article-buffer)
10185     (setq buffer-read-only nil)
10186     (let ((command (if automatic command
10187                      (read-string "Command: " (cons command 0)))))
10188       (erase-buffer)
10189       (insert "$ " command "\n\n")
10190       (if gnus-view-pseudo-asynchronously
10191           (start-process "gnus-execute" (current-buffer) shell-file-name
10192                          shell-command-switch command)
10193         (call-process shell-file-name nil t nil
10194                       shell-command-switch command)))))
10195
10196 ;; Summary kill commands.
10197
10198 (defun gnus-summary-edit-global-kill (article)
10199   "Edit the \"global\" kill file."
10200   (interactive (list (gnus-summary-article-number)))
10201   (gnus-group-edit-global-kill article))
10202
10203 (defun gnus-summary-edit-local-kill ()
10204   "Edit a local kill file applied to the current newsgroup."
10205   (interactive)
10206   (setq gnus-current-headers (gnus-summary-article-header))
10207   (gnus-group-edit-local-kill
10208    (gnus-summary-article-number) gnus-newsgroup-name))
10209
10210 ;;; Header reading.
10211
10212 (defun gnus-read-header (id &optional header)
10213   "Read the headers of article ID and enter them into the Gnus system."
10214   (let ((group gnus-newsgroup-name)
10215         (gnus-override-method
10216          (or
10217           gnus-override-method
10218           (and (gnus-news-group-p gnus-newsgroup-name)
10219                (car (gnus-refer-article-methods)))))
10220         where)
10221     ;; First we check to see whether the header in question is already
10222     ;; fetched.
10223     (if (stringp id)
10224         ;; This is a Message-ID.
10225         (setq header (or header (gnus-id-to-header id)))
10226       ;; This is an article number.
10227       (setq header (or header (gnus-summary-article-header id))))
10228     (if (and header
10229              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10230         ;; We have found the header.
10231         header
10232       ;; If this is a sparse article, we have to nix out its
10233       ;; previous entry in the thread hashtb.
10234       (when (and header
10235                  (gnus-summary-article-sparse-p (mail-header-number header)))
10236         (let* ((parent (gnus-parent-id (mail-header-references header)))
10237                (thread (and parent (gnus-id-to-thread parent))))
10238           (when thread
10239             (delq (assq header thread) thread))))
10240       ;; We have to really fetch the header to this article.
10241       (save-excursion
10242         (set-buffer nntp-server-buffer)
10243         (when (setq where (gnus-request-head id group))
10244           (nnheader-fold-continuation-lines)
10245           (goto-char (point-max))
10246           (insert ".\n")
10247           (goto-char (point-min))
10248           (insert "211 ")
10249           (princ (cond
10250                   ((numberp id) id)
10251                   ((cdr where) (cdr where))
10252                   (header (mail-header-number header))
10253                   (t gnus-reffed-article-number))
10254                  (current-buffer))
10255           (insert " Article retrieved.\n"))
10256         (if (or (not where)
10257                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10258             ()                          ; Malformed head.
10259           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10260             (when (and (stringp id)
10261                        (not (string= (gnus-group-real-name group)
10262                                      (car where))))
10263               ;; If we fetched by Message-ID and the article came
10264               ;; from a different group, we fudge some bogus article
10265               ;; numbers for this article.
10266               (mail-header-set-number header gnus-reffed-article-number))
10267             (save-excursion
10268               (set-buffer gnus-summary-buffer)
10269               (decf gnus-reffed-article-number)
10270               (gnus-remove-header (mail-header-number header))
10271               (push header gnus-newsgroup-headers)
10272               (setq gnus-current-headers header)
10273               (push (mail-header-number header) gnus-newsgroup-limit)))
10274           header)))))
10275
10276 (defun gnus-remove-header (number)
10277   "Remove header NUMBER from `gnus-newsgroup-headers'."
10278   (if (and gnus-newsgroup-headers
10279            (= number (mail-header-number (car gnus-newsgroup-headers))))
10280       (pop gnus-newsgroup-headers)
10281     (let ((headers gnus-newsgroup-headers))
10282       (while (and (cdr headers)
10283                   (not (= number (mail-header-number (cadr headers)))))
10284         (pop headers))
10285       (when (cdr headers)
10286         (setcdr headers (cddr headers))))))
10287
10288 ;;;
10289 ;;; summary highlights
10290 ;;;
10291
10292 (defun gnus-highlight-selected-summary ()
10293   "Highlight selected article in summary buffer."
10294   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10295   (when gnus-summary-selected-face
10296     (save-excursion
10297       (let* ((beg (progn (beginning-of-line) (point)))
10298              (end (progn (end-of-line) (point)))
10299              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10300              (from (if (get-text-property beg gnus-mouse-face-prop)
10301                        beg
10302                      (or (next-single-property-change
10303                           beg gnus-mouse-face-prop nil end)
10304                          beg)))
10305              (to
10306               (if (= from end)
10307                   (- from 2)
10308                 (or (next-single-property-change
10309                      from gnus-mouse-face-prop nil end)
10310                     end))))
10311         ;; If no mouse-face prop on line we will have to = from = end,
10312         ;; so we highlight the entire line instead.
10313         (when (= (+ to 2) from)
10314           (setq from beg)
10315           (setq to end))
10316         (if gnus-newsgroup-selected-overlay
10317             ;; Move old overlay.
10318             (gnus-move-overlay
10319              gnus-newsgroup-selected-overlay from to (current-buffer))
10320           ;; Create new overlay.
10321           (gnus-overlay-put
10322            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10323            'face gnus-summary-selected-face))))))
10324
10325 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10326 (defun gnus-summary-highlight-line ()
10327   "Highlight current line according to `gnus-summary-highlight'."
10328   (let* ((list gnus-summary-highlight)
10329          (p (point))
10330          (end (progn (end-of-line) (point)))
10331          ;; now find out where the line starts and leave point there.
10332          (beg (progn (beginning-of-line) (point)))
10333          (article (gnus-summary-article-number))
10334          (score (or (cdr (assq (or article gnus-current-article)
10335                                gnus-newsgroup-scored))
10336                     gnus-summary-default-score 0))
10337          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10338          (inhibit-read-only t))
10339     ;; Eval the cars of the lists until we find a match.
10340     (let ((default gnus-summary-default-score)
10341           (default-high gnus-summary-default-high-score)
10342           (default-low gnus-summary-default-low-score))
10343       (while (and list
10344                   (not (eval (caar list))))
10345         (setq list (cdr list))))
10346     (let ((face (cdar list)))
10347       (unless (eq face (get-text-property beg 'face))
10348         (gnus-put-text-property-excluding-characters-with-faces
10349          beg end 'face
10350          (setq face (if (boundp face) (symbol-value face) face)))
10351         (when gnus-summary-highlight-line-function
10352           (funcall gnus-summary-highlight-line-function article face))))
10353     (goto-char p)))
10354
10355 (defun gnus-update-read-articles (group unread &optional compute)
10356   "Update the list of read articles in GROUP."
10357   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10358          (entry (gnus-gethash group gnus-newsrc-hashtb))
10359          (info (nth 2 entry))
10360          (prev 1)
10361          (unread (sort (copy-sequence unread) '<))
10362          read)
10363     (if (or (not info) (not active))
10364         ;; There is no info on this group if it was, in fact,
10365         ;; killed.  Gnus stores no information on killed groups, so
10366         ;; there's nothing to be done.
10367         ;; One could store the information somewhere temporarily,
10368         ;; perhaps...  Hmmm...
10369         ()
10370       ;; Remove any negative articles numbers.
10371       (while (and unread (< (car unread) 0))
10372         (setq unread (cdr unread)))
10373       ;; Remove any expired article numbers
10374       (while (and unread (< (car unread) (car active)))
10375         (setq unread (cdr unread)))
10376       ;; Compute the ranges of read articles by looking at the list of
10377       ;; unread articles.
10378       (while unread
10379         (when (/= (car unread) prev)
10380           (push (if (= prev (1- (car unread))) prev
10381                   (cons prev (1- (car unread))))
10382                 read))
10383         (setq prev (1+ (car unread)))
10384         (setq unread (cdr unread)))
10385       (when (<= prev (cdr active))
10386         (push (cons prev (cdr active)) read))
10387       (setq read (if (> (length read) 1) (nreverse read) read))
10388       (if compute
10389           read
10390         (save-excursion
10391           (let (setmarkundo)
10392             ;; Propagate the read marks to the backend.
10393             (when (gnus-check-backend-function 'request-set-mark group)
10394               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10395                     (add (gnus-remove-from-range read (gnus-info-read info))))
10396                 (when (or add del)
10397                   (unless (gnus-check-group group)
10398                     (error "Can't open server for %s" group))
10399                   (gnus-request-set-mark
10400                    group (delq nil (list (if add (list add 'add '(read)))
10401                                          (if del (list del 'del '(read))))))
10402                   (setq setmarkundo
10403                         `(gnus-request-set-mark
10404                           ,group
10405                           ',(delq nil (list
10406                                        (if del (list del 'add '(read)))
10407                                        (if add (list add 'del '(read))))))))))
10408             (set-buffer gnus-group-buffer)
10409             (gnus-undo-register
10410               `(progn
10411                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10412                  (gnus-info-set-read ',info ',(gnus-info-read info))
10413                  (gnus-get-unread-articles-in-group ',info
10414                                                     (gnus-active ,group))
10415                  (gnus-group-update-group ,group t)
10416                  ,setmarkundo))))
10417         ;; Enter this list into the group info.
10418         (gnus-info-set-read info read)
10419         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10420         (gnus-get-unread-articles-in-group info (gnus-active group))
10421         t))))
10422
10423 (defun gnus-offer-save-summaries ()
10424   "Offer to save all active summary buffers."
10425   (let (buffers)
10426     ;; Go through all buffers and find all summaries.
10427     (dolist (buffer (buffer-list))
10428       (when (and (setq buffer (buffer-name buffer))
10429                  (string-match "Summary" buffer)
10430                  (save-excursion
10431                    (set-buffer buffer)
10432                    ;; We check that this is, indeed, a summary buffer.
10433                    (and (eq major-mode 'gnus-summary-mode)
10434                         ;; Also make sure this isn't bogus.
10435                         gnus-newsgroup-prepared
10436                         ;; Also make sure that this isn't a
10437                         ;; dead summary buffer.
10438                         (not gnus-dead-summary-mode))))
10439         (push buffer buffers)))
10440     ;; Go through all these summary buffers and offer to save them.
10441     (when buffers
10442       (save-excursion
10443         (map-y-or-n-p
10444          "Update summary buffer %s? "
10445          (lambda (buf)
10446            (switch-to-buffer buf)
10447            (gnus-summary-exit))
10448          buffers)))))
10449
10450
10451 ;;; @ for mime-partial
10452 ;;;
10453
10454 (defun gnus-request-partial-message ()
10455   (save-excursion
10456     (let ((number (gnus-summary-article-number))
10457           (group gnus-newsgroup-name)
10458           (mother gnus-article-buffer))
10459       (set-buffer (get-buffer-create " *Partial Article*"))
10460       (erase-buffer)
10461       (setq mime-preview-buffer mother)
10462       (gnus-request-article-this-buffer number group)
10463       (mime-parse-buffer)
10464       )))
10465
10466 (autoload 'mime-combine-message/partial-pieces-automatically
10467   "mime-partial"
10468   "Internal method to combine message/partial messages automatically.")
10469
10470 (mime-add-condition
10471  'action '((type . message)(subtype . partial)
10472            (major-mode . gnus-original-article-mode)
10473            (method . mime-combine-message/partial-pieces-automatically)
10474            (summary-buffer-exp . gnus-summary-buffer)
10475            (request-partial-message-method . gnus-request-partial-message)
10476            ))
10477
10478
10479 ;;; @ for message/rfc822
10480 ;;;
10481
10482 (defun gnus-mime-extract-message/rfc822 (entity situation)
10483   (let (group article num cwin swin cur)
10484     (with-temp-buffer
10485       (mime-insert-entity-content entity)
10486       (setq group (or (cdr (assq 'group situation))
10487                       (completing-read "Group: "
10488                                        gnus-active-hashtb
10489                                        nil
10490                                        (gnus-read-active-file-p)
10491                                        gnus-newsgroup-name))
10492             article (gnus-request-accept-article group)))
10493     (when (and (consp article)
10494                (numberp (setq article (cdr article))))
10495       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10496             cwin (get-buffer-window (current-buffer) t))
10497       (save-window-excursion
10498         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10499             (select-window swin)
10500           (set-buffer gnus-summary-buffer))
10501         (setq cur gnus-current-article)
10502         (forward-line num)
10503         (let (gnus-show-threads)
10504           (gnus-summary-goto-subject article t))
10505         (gnus-summary-clear-mark-forward 1)
10506         (gnus-summary-goto-subject cur))
10507       (when (and cwin (window-frame cwin))
10508         (select-frame (window-frame cwin)))
10509       (when (boundp 'mime-acting-situation-to-override)
10510         (set-alist 'mime-acting-situation-to-override
10511                    'group
10512                    group)
10513         (set-alist 'mime-acting-situation-to-override
10514                    'after-method
10515                    `(progn
10516                       (save-current-buffer
10517                         (set-buffer gnus-group-buffer)
10518                         (gnus-activate-group ,group))
10519                       (gnus-summary-goto-article ,cur
10520                                                  gnus-show-all-headers)))
10521         (set-alist 'mime-acting-situation-to-override
10522                    'number num)))))
10523
10524 (mime-add-condition
10525  'action '((type . message)(subtype . rfc822)
10526            (major-mode . gnus-original-article-mode)
10527            (method . gnus-mime-extract-message/rfc822)
10528            (mode . "extract")
10529            ))
10530
10531 (mime-add-condition
10532  'action '((type . message)(subtype . news)
10533            (major-mode . gnus-original-article-mode)
10534            (method . gnus-mime-extract-message/rfc822)
10535            (mode . "extract")
10536            ))
10537
10538 (defun gnus-mime-extract-multipart (entity situation)
10539   (let ((children (mime-entity-children entity))
10540         mime-acting-situation-to-override
10541         f)
10542     (while children
10543       (mime-play-entity (car children)
10544                         (cons (assq 'mode situation)
10545                               mime-acting-situation-to-override))
10546       (setq children (cdr children)))
10547     (if (setq f (cdr (assq 'after-method
10548                            mime-acting-situation-to-override)))
10549         (eval f)
10550       )))
10551
10552 (mime-add-condition
10553  'action '((type . multipart)
10554            (method . gnus-mime-extract-multipart)
10555            (mode . "extract")
10556            )
10557  'with-default)
10558
10559
10560 ;;; @ end
10561 ;;;
10562
10563 (defun gnus-summary-setup-default-charset ()
10564   "Setup newsgroup default charset."
10565   (if (equal gnus-newsgroup-name "nndraft:drafts")
10566       (setq gnus-newsgroup-charset nil)
10567     (let* ((ignored-charsets
10568             (or gnus-newsgroup-ephemeral-ignored-charsets
10569                 (append
10570                  (and gnus-newsgroup-name
10571                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10572                  gnus-newsgroup-ignored-charsets))))
10573       (setq gnus-newsgroup-charset
10574             (or gnus-newsgroup-ephemeral-charset
10575                 (and gnus-newsgroup-name
10576                      (gnus-parameter-charset gnus-newsgroup-name))
10577                 gnus-default-charset))
10578       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10579            ignored-charsets))))
10580
10581 ;;;
10582 ;;; Mime Commands
10583 ;;;
10584
10585 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10586   "Display the current article buffer fully MIME-buttonized.
10587 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10588 treated as multipart/mixed."
10589   (interactive "P")
10590   (require 'gnus-art)
10591   (let ((gnus-unbuttonized-mime-types nil)
10592         (gnus-mime-display-multipart-as-mixed show-all-parts))
10593     (gnus-summary-show-article)))
10594
10595 (defun gnus-summary-repair-multipart (article)
10596   "Add a Content-Type header to a multipart article without one."
10597   (interactive (list (gnus-summary-article-number)))
10598   (gnus-with-article article
10599     (message-narrow-to-head)
10600     (message-remove-header "Mime-Version")
10601     (goto-char (point-max))
10602     (insert "Mime-Version: 1.0\n")
10603     (widen)
10604     (when (search-forward "\n--" nil t)
10605       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10606         (message-narrow-to-head)
10607         (message-remove-header "Content-Type")
10608         (goto-char (point-max))
10609         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10610                         separator))
10611         (widen))))
10612   (let (gnus-mark-article-hook)
10613     (gnus-summary-select-article t t nil article)))
10614
10615 (defun gnus-summary-toggle-display-buttonized ()
10616   "Toggle the buttonizing of the article buffer."
10617   (interactive)
10618   (require 'gnus-art)
10619   (if (setq gnus-inhibit-mime-unbuttonizing
10620             (not gnus-inhibit-mime-unbuttonizing))
10621       (let ((gnus-unbuttonized-mime-types nil))
10622         (gnus-summary-show-article))
10623     (gnus-summary-show-article)))
10624
10625 ;;;
10626 ;;; Intelli-mouse commmands
10627 ;;;
10628
10629 (defun gnus-wheel-summary-scroll (event)
10630   (interactive "e")
10631   (let ((amount (if (memq 'shift (event-modifiers event))
10632                     (car gnus-wheel-scroll-amount)
10633                   (cdr gnus-wheel-scroll-amount)))
10634         (direction (- (* (static-if (featurep 'xemacs)
10635                              (event-button event)
10636                            (cond ((eq 'mouse-4 (event-basic-type event))
10637                                   4)
10638                                  ((eq 'mouse-5 (event-basic-type event))
10639                                   5)))
10640                          2) 9))
10641         edge)
10642     (gnus-summary-scroll-up (* amount direction))
10643     (when (gnus-eval-in-buffer-window gnus-article-buffer
10644             (save-restriction
10645               (widen)
10646               (and (if (< 0 direction)
10647                        (gnus-article-next-page 0)
10648                      (gnus-article-prev-page 0)
10649                      (bobp))
10650                    (if (setq edge (get-text-property
10651                                    (point-min) 'gnus-wheel-edge))
10652                        (setq edge (* edge direction))
10653                      (setq edge -1))
10654                    (or (plusp edge)
10655                        (let ((buffer-read-only nil)
10656                              (inhibit-read-only t))
10657                          (put-text-property (point-min) (point-max)
10658                                             'gnus-wheel-edge direction)
10659                          nil))
10660                    (or (> edge gnus-wheel-edge-resistance)
10661                        (let ((buffer-read-only nil)
10662                              (inhibit-read-only t))
10663                          (put-text-property (point-min) (point-max)
10664                                             'gnus-wheel-edge
10665                                             (* (1+ edge) direction))
10666                          nil))
10667                    (eq last-command 'gnus-wheel-summary-scroll))))
10668       (gnus-summary-next-article nil nil (minusp direction)))))
10669
10670 (defun gnus-wheel-install ()
10671   "Enable mouse wheel support on summary window."
10672   (when gnus-use-wheel
10673     (let ((keys
10674            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10675       (dolist (key keys)
10676         (define-key gnus-summary-mode-map key
10677           'gnus-wheel-summary-scroll)))))
10678
10679 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10680
10681 ;;;
10682 ;;; Traditional PGP commmands
10683 ;;;
10684
10685 (defun gnus-summary-decrypt-article (&optional force)
10686   "Decrypt the current article in traditional PGP way.
10687 This will have permanent effect only in mail groups.
10688 If FORCE is non-nil, allow editing of articles even in read-only
10689 groups."
10690   (interactive "P")
10691   (gnus-summary-select-article t)
10692   (gnus-eval-in-buffer-window gnus-article-buffer
10693     (save-excursion
10694       (save-restriction
10695         (widen)
10696         (goto-char (point-min))
10697         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10698           (error "Not a traditional PGP message!"))
10699         (let ((armor-start (match-beginning 0)))
10700           (if (and (pgg-decrypt-region armor-start (point-max))
10701                    (or force (not (gnus-group-read-only-p))))
10702               (let ((inhibit-read-only t)
10703                     buffer-read-only)
10704                 (delete-region armor-start
10705                                (progn
10706                                  (re-search-forward "^-+END PGP" nil t)
10707                                  (beginning-of-line 2)
10708                                  (point)))
10709                 (insert-buffer-substring pgg-output-buffer))))))))
10710
10711 (defun gnus-summary-verify-article ()
10712   "Verify the current article in traditional PGP way."
10713   (interactive)
10714   (save-excursion
10715     (set-buffer gnus-original-article-buffer)
10716     (goto-char (point-min))
10717     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10718       (error "Not a traditional PGP message!"))
10719     (re-search-forward "^-+END PGP" nil t)
10720     (beginning-of-line 2)
10721     (call-interactively (function pgg-verify-region))))
10722
10723 ;;;
10724 ;;; Generic summary marking commands
10725 ;;;
10726
10727 (defvar gnus-summary-marking-alist
10728   '((read gnus-del-mark "d")
10729     (unread gnus-unread-mark "u")
10730     (ticked gnus-ticked-mark "!")
10731     (dormant gnus-dormant-mark "?")
10732     (expirable gnus-expirable-mark "e"))
10733   "An alist of names/marks/keystrokes.")
10734
10735 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10736 (defvar gnus-summary-mark-map)
10737
10738 (defun gnus-summary-make-all-marking-commands ()
10739   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10740   (dolist (elem gnus-summary-marking-alist)
10741     (apply 'gnus-summary-make-marking-command elem)))
10742
10743 (defun gnus-summary-make-marking-command (name mark keystroke)
10744   (let ((map (make-sparse-keymap)))
10745     (define-key gnus-summary-generic-mark-map keystroke map)
10746     (dolist (lway `((next "next" next nil "n")
10747                     (next-unread "next unread" next t "N")
10748                     (prev "previous" prev nil "p")
10749                     (prev-unread "previous unread" prev t "P")
10750                     (nomove "" nil nil ,keystroke)))
10751       (let ((func (gnus-summary-make-marking-command-1
10752                    mark (car lway) lway name)))
10753         (setq func (eval func))
10754         (define-key map (nth 4 lway) func)))))
10755
10756 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10757   `(defun ,(intern
10758             (format "gnus-summary-put-mark-as-%s%s"
10759                     name (if (eq way 'nomove)
10760                              ""
10761                            (concat "-" (symbol-name way)))))
10762      (n)
10763      ,(format
10764        "Mark the current article as %s%s.
10765 If N, the prefix, then repeat N times.
10766 If N is negative, move in reverse order.
10767 The difference between N and the actual number of articles marked is
10768 returned."
10769        name (car (cdr lway)))
10770      (interactive "p")
10771      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10772
10773 (defun gnus-summary-generic-mark (n mark move unread)
10774   "Mark N articles with MARK."
10775   (unless (eq major-mode 'gnus-summary-mode)
10776     (error "This command can only be used in the summary buffer"))
10777   (gnus-summary-show-thread)
10778   (let ((nummove
10779          (cond
10780           ((eq move 'next) 1)
10781           ((eq move 'prev) -1)
10782           (t 0))))
10783     (if (zerop nummove)
10784         (setq n 1)
10785       (when (< n 0)
10786         (setq n (abs n)
10787               nummove (* -1 nummove))))
10788     (while (and (> n 0)
10789                 (gnus-summary-mark-article nil mark)
10790                 (zerop (gnus-summary-next-subject nummove unread t)))
10791       (setq n (1- n)))
10792     (when (/= 0 n)
10793       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10794     (gnus-summary-recenter)
10795     (gnus-summary-position-point)
10796     (gnus-set-mode-line 'summary)
10797     n))
10798
10799 (defun gnus-summary-insert-articles (articles)
10800   (when (setq articles
10801               (gnus-set-difference articles
10802                                    (mapcar (lambda (h) (mail-header-number h))
10803                                            gnus-newsgroup-headers)))
10804     (setq gnus-newsgroup-headers
10805           (merge 'list
10806                  gnus-newsgroup-headers
10807                  (gnus-fetch-headers articles)
10808                  'gnus-article-sort-by-number))
10809     ;; Suppress duplicates?
10810     (when gnus-suppress-duplicates
10811       (gnus-dup-suppress-articles))
10812
10813     ;; We might want to build some more threads first.
10814     (when (and gnus-fetch-old-headers
10815                (eq gnus-headers-retrieved-by 'nov))
10816       (if (eq gnus-fetch-old-headers 'invisible)
10817           (gnus-build-all-threads)
10818         (gnus-build-old-threads)))
10819     ;; Let the Gnus agent mark articles as read.
10820     (when gnus-agent
10821       (gnus-agent-get-undownloaded-list))
10822     ;; Remove list identifiers from subject
10823     (when gnus-list-identifiers
10824       (gnus-summary-remove-list-identifiers))
10825     ;; First and last article in this newsgroup.
10826     (when gnus-newsgroup-headers
10827       (setq gnus-newsgroup-begin
10828             (mail-header-number (car gnus-newsgroup-headers))
10829             gnus-newsgroup-end
10830             (mail-header-number
10831              (gnus-last-element gnus-newsgroup-headers))))
10832     (when gnus-use-scoring
10833       (gnus-possibly-score-headers))))
10834
10835 (defun gnus-summary-insert-old-articles (&optional all)
10836   "Insert all old articles in this group.
10837 If ALL is non-nil, already read articles become readable.
10838 If ALL is a number, fetch this number of articles."
10839   (interactive "P")
10840   (prog1
10841       (let ((old (mapcar 'car gnus-newsgroup-data))
10842             (i (car gnus-newsgroup-active))
10843             older len)
10844         (while (<= i (cdr gnus-newsgroup-active))
10845           (or (memq i old) (push i older))
10846           (incf i))
10847         (setq len (length older))
10848         (cond
10849          ((null older) nil)
10850          ((numberp all)
10851           (if (< all len)
10852               (setq older (subseq older 0 all))))
10853          (all nil)
10854          (t
10855           (if (and (numberp gnus-large-newsgroup)
10856                    (> len gnus-large-newsgroup))
10857               (let ((input
10858                      (read-string
10859                       (format
10860                        "How many articles from %s (default %d): "
10861                        (gnus-limit-string
10862                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10863                        len))))
10864                 (unless (string-match "^[ \t]*$" input)
10865                   (setq all (string-to-number input))
10866                   (if (< all len)
10867                       (setq older (subseq older 0 all))))))))
10868         (if (not older)
10869             (message "No old news.")
10870           (gnus-summary-insert-articles older)
10871           (gnus-summary-limit (gnus-union older old))))
10872     (gnus-summary-position-point)))
10873
10874 (defun gnus-summary-insert-new-articles ()
10875   "Insert all new articles in this group."
10876   (interactive)
10877   (prog1
10878       (let ((old (mapcar 'car gnus-newsgroup-data))
10879             (old-active gnus-newsgroup-active)
10880             (nnmail-fetched-sources (list t))
10881             i new)
10882         (setq gnus-newsgroup-active
10883               (gnus-activate-group gnus-newsgroup-name 'scan))
10884         (setq i (1+ (cdr old-active)))
10885         (while (<= i (cdr gnus-newsgroup-active))
10886           (push i new)
10887           (incf i))
10888         (if (not new)
10889             (message "No gnus is bad news.")
10890           (setq new (nreverse new))
10891           (gnus-summary-insert-articles new)
10892           (setq gnus-newsgroup-unreads
10893                 (append gnus-newsgroup-unreads new))
10894           (gnus-summary-limit (gnus-union old new))))
10895     (gnus-summary-position-point)))
10896
10897 (gnus-summary-make-all-marking-commands)
10898
10899 (gnus-ems-redefine)
10900
10901 (provide 'gnus-sum)
10902
10903 (run-hooks 'gnus-sum-load-hook)
10904
10905 ;;; gnus-sum.el ends here