d5311100b7916f487788c5cd765cf41c24eb8757
[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             (setq gnus-newsgroup-display 'identity))
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-adjust-marked-articles (info)
4825   "Set all article lists and remove all marks that are no longer valid."
4826   (let* ((marked-lists (gnus-info-marks info))
4827          (active (gnus-active (gnus-info-group info)))
4828          (min (car active))
4829          (max (cdr active))
4830          (types gnus-article-mark-lists)
4831          (uncompressed '(score bookmark killed))
4832          marks var articles article mark)
4833
4834     (dolist (marks marked-lists)
4835       (setq mark (car marks))
4836       (unless (eq mark 'seen)
4837         ;; Do the rest of the marks.
4838         (set (setq var (intern (format "gnus-newsgroup-%s"
4839                                        (car (rassq mark types)))))
4840              (cond
4841               ((memq mark uncompressed)
4842                (cdr marks))
4843               (t
4844                (gnus-uncompress-range (cdr marks)))))
4845
4846         (setq articles (symbol-value var))
4847
4848         ;; All articles have to be subsets of the active articles.
4849         (cond
4850          ;; Adjust "simple" lists.
4851          ((memq mark '(tick dormant expire reply save))
4852           (while articles
4853             (when (or (< (setq article (pop articles)) min) (> article max))
4854               (set var (delq article (symbol-value var))))))
4855          ;; Adjust assocs.
4856          ((memq mark uncompressed)
4857           (when (not (listp (cdr (symbol-value var))))
4858             (set var (list (symbol-value var))))
4859           (when (not (listp (cdr articles)))
4860             (setq articles (list articles)))
4861           (while articles
4862             (when (or (not (consp (setq article (pop articles))))
4863                       (< (car article) min)
4864                       (> (car article) max))
4865               (set var (delq article (symbol-value var)))))))))))
4866
4867 (defun gnus-update-missing-marks (missing)
4868   "Go through the list of MISSING articles and remove them from the mark lists."
4869   (when missing
4870     (let ((types gnus-article-mark-lists)
4871           var m)
4872       ;; Go through all types.
4873       (while types
4874         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4875         (when (symbol-value var)
4876           ;; This list has articles.  So we delete all missing articles
4877           ;; from it.
4878           (setq m missing)
4879           (while m
4880             (set var (delq (pop m) (symbol-value var)))))))))
4881
4882 (defun gnus-update-marks ()
4883   "Enter the various lists of marked articles into the newsgroup info list."
4884   (let ((types gnus-article-mark-lists)
4885         (info (gnus-get-info gnus-newsgroup-name))
4886         (uncompressed '(score bookmark killed seen))
4887         type list newmarked symbol delta-marks)
4888     (when info
4889       ;; Add all marks lists to the list of marks lists.
4890       (while (setq type (pop types))
4891         (setq list (symbol-value
4892                     (setq symbol
4893                           (intern (format "gnus-newsgroup-%s" (car type))))))
4894
4895         (when list
4896           ;; Get rid of the entries of the articles that have the
4897           ;; default score.
4898           (when (and (eq (cdr type) 'score)
4899                      gnus-save-score
4900                      list)
4901             (let* ((arts list)
4902                    (prev (cons nil list))
4903                    (all prev))
4904               (while arts
4905                 (if (or (not (consp (car arts)))
4906                         (= (cdar arts) gnus-summary-default-score))
4907                     (setcdr prev (cdr arts))
4908                   (setq prev arts))
4909                 (setq arts (cdr arts)))
4910               (setq list (cdr all)))))
4911
4912         (when (eq (cdr type) 'seen)
4913           (setq list 
4914                 (if list
4915                     (gnus-add-to-range list gnus-newsgroup-unseen)
4916                   (gnus-compress-sequence gnus-newsgroup-articles))))
4917
4918         (unless (memq (cdr type) uncompressed)
4919           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4920
4921         (when (gnus-check-backend-function
4922                'request-set-mark gnus-newsgroup-name)
4923           ;; propagate flags to server, with the following exceptions:
4924           ;; uncompressed:s are not proper flags (they are cons cells)
4925           ;; cache is a internal gnus flag
4926           ;; download are local to one gnus installation (well)
4927           ;; unsend are for nndraft groups only
4928           ;; xxx: generality of this?  this suits nnimap anyway
4929           (unless (memq (cdr type) (append '(cache download unsend)
4930                                            uncompressed))
4931             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4932                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4933                    (add (gnus-remove-from-range
4934                          (gnus-copy-sequence list) old)))
4935               (when add
4936                 (push (list add 'add (list (cdr type))) delta-marks))
4937               (when del
4938                 (push (list del 'del (list (cdr type))) delta-marks)))))
4939
4940         (when list
4941           (push (cons (cdr type) list) newmarked)))
4942
4943       (when delta-marks
4944         (unless (gnus-check-group gnus-newsgroup-name)
4945           (error "Can't open server for %s" gnus-newsgroup-name))
4946         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4947
4948       ;; Enter these new marks into the info of the group.
4949       (if (nthcdr 3 info)
4950           (setcar (nthcdr 3 info) newmarked)
4951         ;; Add the marks lists to the end of the info.
4952         (when newmarked
4953           (setcdr (nthcdr 2 info) (list newmarked))))
4954
4955       ;; Cut off the end of the info if there's nothing else there.
4956       (let ((i 5))
4957         (while (and (> i 2)
4958                     (not (nth i info)))
4959           (when (nthcdr (decf i) info)
4960             (setcdr (nthcdr i info) nil)))))))
4961
4962 (defun gnus-set-mode-line (where)
4963   "Set the mode line of the article or summary buffers.
4964 If WHERE is `summary', the summary mode line format will be used."
4965   ;; Is this mode line one we keep updated?
4966   (when (and (memq where gnus-updated-mode-lines)
4967              (symbol-value
4968               (intern (format "gnus-%s-mode-line-format-spec" where))))
4969     (let (mode-string)
4970       (save-excursion
4971         ;; We evaluate this in the summary buffer since these
4972         ;; variables are buffer-local to that buffer.
4973         (set-buffer gnus-summary-buffer)
4974         ;; We bind all these variables that are used in the `eval' form
4975         ;; below.
4976         (let* ((mformat (symbol-value
4977                          (intern
4978                           (format "gnus-%s-mode-line-format-spec" where))))
4979                (gnus-tmp-group-name (gnus-group-decoded-name
4980                                      gnus-newsgroup-name))
4981                (gnus-tmp-article-number (or gnus-current-article 0))
4982                (gnus-tmp-unread gnus-newsgroup-unreads)
4983                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4984                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4985                (gnus-tmp-unread-and-unselected
4986                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4987                             (zerop gnus-tmp-unselected))
4988                        "")
4989                       ((zerop gnus-tmp-unselected)
4990                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4991                       (t (format "{%d(+%d) more}"
4992                                  gnus-tmp-unread-and-unticked
4993                                  gnus-tmp-unselected))))
4994                (gnus-tmp-subject
4995                 (if (and gnus-current-headers
4996                          (vectorp gnus-current-headers))
4997                     (gnus-mode-string-quote
4998                      (mail-header-subject gnus-current-headers))
4999                   ""))
5000                bufname-length max-len
5001                gnus-tmp-header);; passed as argument to any user-format-funcs
5002           (setq mode-string (eval mformat))
5003           (setq bufname-length (if (string-match "%b" mode-string)
5004                                    (- (length
5005                                        (buffer-name
5006                                         (if (eq where 'summary)
5007                                             nil
5008                                           (get-buffer gnus-article-buffer))))
5009                                       2)
5010                                  0))
5011           (setq max-len (max 4 (if gnus-mode-non-string-length
5012                                    (- (window-width)
5013                                       gnus-mode-non-string-length
5014                                       bufname-length)
5015                                  (length mode-string))))
5016           ;; We might have to chop a bit of the string off...
5017           (when (> (length mode-string) max-len)
5018             (setq mode-string
5019                   (concat (gnus-truncate-string mode-string (- max-len 3))
5020                           "...")))
5021           ;; Pad the mode string a bit.
5022           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5023       ;; Update the mode line.
5024       (setq mode-line-buffer-identification
5025             (gnus-mode-line-buffer-identification (list mode-string)))
5026       (set-buffer-modified-p t))))
5027
5028 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5029   "Go through the HEADERS list and add all Xrefs to a hash table.
5030 The resulting hash table is returned, or nil if no Xrefs were found."
5031   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5032          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5033          (xref-hashtb (gnus-make-hashtable))
5034          start group entry number xrefs header)
5035     (while headers
5036       (setq header (pop headers))
5037       (when (and (setq xrefs (mail-header-xref header))
5038                  (not (memq (setq number (mail-header-number header))
5039                             unreads)))
5040         (setq start 0)
5041         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5042           (setq start (match-end 0))
5043           (setq group (if prefix
5044                           (concat prefix (substring xrefs (match-beginning 1)
5045                                                     (match-end 1)))
5046                         (substring xrefs (match-beginning 1) (match-end 1))))
5047           (setq number
5048                 (string-to-int (substring xrefs (match-beginning 2)
5049                                           (match-end 2))))
5050           (if (setq entry (gnus-gethash group xref-hashtb))
5051               (setcdr entry (cons number (cdr entry)))
5052             (gnus-sethash group (cons number nil) xref-hashtb)))))
5053     (and start xref-hashtb)))
5054
5055 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5056   "Look through all the headers and mark the Xrefs as read."
5057   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5058         name entry info xref-hashtb idlist method nth4)
5059     (save-excursion
5060       (set-buffer gnus-group-buffer)
5061       (when (setq xref-hashtb
5062                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5063         (mapatoms
5064          (lambda (group)
5065            (unless (string= from-newsgroup (setq name (symbol-name group)))
5066              (setq idlist (symbol-value group))
5067              ;; Dead groups are not updated.
5068              (and (prog1
5069                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5070                             info (nth 2 entry))
5071                     (when (stringp (setq nth4 (gnus-info-method info)))
5072                       (setq nth4 (gnus-server-to-method nth4))))
5073                   ;; Only do the xrefs if the group has the same
5074                   ;; select method as the group we have just read.
5075                   (or (gnus-methods-equal-p
5076                        nth4 (gnus-find-method-for-group from-newsgroup))
5077                       virtual
5078                       (equal nth4 (setq method (gnus-find-method-for-group
5079                                                 from-newsgroup)))
5080                       (and (equal (car nth4) (car method))
5081                            (equal (nth 1 nth4) (nth 1 method))))
5082                   gnus-use-cross-reference
5083                   (or (not (eq gnus-use-cross-reference t))
5084                       virtual
5085                       ;; Only do cross-references on subscribed
5086                       ;; groups, if that is what is wanted.
5087                       (<= (gnus-info-level info) gnus-level-subscribed))
5088                   (gnus-group-make-articles-read name idlist))))
5089          xref-hashtb)))))
5090
5091 (defun gnus-compute-read-articles (group articles)
5092   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5093          (info (nth 2 entry))
5094          (active (gnus-active group))
5095          ninfo)
5096     (when entry
5097       ;; First peel off all invalid article numbers.
5098       (when active
5099         (let ((ids articles)
5100               id first)
5101           (while (setq id (pop ids))
5102             (when (and first (> id (cdr active)))
5103               ;; We'll end up in this situation in one particular
5104               ;; obscure situation.  If you re-scan a group and get
5105               ;; a new article that is cross-posted to a different
5106               ;; group that has not been re-scanned, you might get
5107               ;; crossposted article that has a higher number than
5108               ;; Gnus believes possible.  So we re-activate this
5109               ;; group as well.  This might mean doing the
5110               ;; crossposting thingy will *increase* the number
5111               ;; of articles in some groups.  Tsk, tsk.
5112               (setq active (or (gnus-activate-group group) active)))
5113             (when (or (> id (cdr active))
5114                       (< id (car active)))
5115               (setq articles (delq id articles))))))
5116       ;; If the read list is nil, we init it.
5117       (if (and active
5118                (null (gnus-info-read info))
5119                (> (car active) 1))
5120           (setq ninfo (cons 1 (1- (car active))))
5121         (setq ninfo (gnus-info-read info)))
5122       ;; Then we add the read articles to the range.
5123       (gnus-add-to-range
5124        ninfo (setq articles (sort articles '<))))))
5125
5126 (defun gnus-group-make-articles-read (group articles)
5127   "Update the info of GROUP to say that ARTICLES are read."
5128   (let* ((num 0)
5129          (entry (gnus-gethash group gnus-newsrc-hashtb))
5130          (info (nth 2 entry))
5131          (active (gnus-active group))
5132          range)
5133     (when entry
5134       (setq range (gnus-compute-read-articles group articles))
5135       (save-excursion
5136         (set-buffer gnus-group-buffer)
5137         (gnus-undo-register
5138           `(progn
5139              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5140              (gnus-info-set-read ',info ',(gnus-info-read info))
5141              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5142              (gnus-group-update-group ,group t))))
5143       ;; Add the read articles to the range.
5144       (gnus-info-set-read info range)
5145       (gnus-request-set-mark group (list (list range 'add '(read))))
5146       ;; Then we have to re-compute how many unread
5147       ;; articles there are in this group.
5148       (when active
5149         (cond
5150          ((not range)
5151           (setq num (- (1+ (cdr active)) (car active))))
5152          ((not (listp (cdr range)))
5153           (setq num (- (cdr active) (- (1+ (cdr range))
5154                                        (car range)))))
5155          (t
5156           (while range
5157             (if (numberp (car range))
5158                 (setq num (1+ num))
5159               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5160             (setq range (cdr range)))
5161           (setq num (- (cdr active) num))))
5162         ;; Update the number of unread articles.
5163         (setcar entry num)
5164         ;; Update the group buffer.
5165         (gnus-group-update-group group t)))))
5166
5167 (defvar gnus-newsgroup-none-id 0)
5168
5169 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5170   (let ((cur nntp-server-buffer)
5171         (dependencies
5172          (or dependencies
5173              (save-excursion (set-buffer gnus-summary-buffer)
5174                              gnus-newsgroup-dependencies)))
5175         headers id end ref
5176         (mail-parse-charset gnus-newsgroup-charset)
5177         (mail-parse-ignored-charsets
5178          (save-excursion (condition-case nil
5179                              (set-buffer gnus-summary-buffer)
5180                            (error))
5181                          gnus-newsgroup-ignored-charsets)))
5182     (save-excursion
5183       (set-buffer nntp-server-buffer)
5184       ;; Translate all TAB characters into SPACE characters.
5185       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5186       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5187       (gnus-run-hooks 'gnus-parse-headers-hook)
5188       (let ((case-fold-search t)
5189             in-reply-to header p lines chars ctype)
5190         (goto-char (point-min))
5191         ;; Search to the beginning of the next header.  Error messages
5192         ;; do not begin with 2 or 3.
5193         (while (re-search-forward "^[23][0-9]+ " nil t)
5194           (setq id nil
5195                 ref nil)
5196           ;; This implementation of this function, with nine
5197           ;; search-forwards instead of the one re-search-forward and
5198           ;; a case (which basically was the old function) is actually
5199           ;; about twice as fast, even though it looks messier.  You
5200           ;; can't have everything, I guess.  Speed and elegance
5201           ;; doesn't always go hand in hand.
5202           (setq
5203            header
5204            (make-full-mail-header
5205             ;; Number.
5206             (prog1
5207                 (read cur)
5208               (end-of-line)
5209               (setq p (point))
5210               (narrow-to-region (point)
5211                                 (or (and (search-forward "\n.\n" nil t)
5212                                          (- (point) 2))
5213                                     (point))))
5214             ;; Subject.
5215             (progn
5216               (goto-char p)
5217               (if (search-forward "\nsubject:" nil t)
5218                   (nnheader-header-value)
5219                 "(none)"))
5220             ;; From.
5221             (progn
5222               (goto-char p)
5223               (if (search-forward "\nfrom:" nil t)
5224                   (nnheader-header-value)
5225                 "(nobody)"))
5226             ;; Date.
5227             (progn
5228               (goto-char p)
5229               (if (search-forward "\ndate:" nil t)
5230                   (nnheader-header-value) ""))
5231             ;; Message-ID.
5232             (progn
5233               (goto-char p)
5234               (setq id (if (re-search-forward
5235                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5236                            ;; We do it this way to make sure the Message-ID
5237                            ;; is (somewhat) syntactically valid.
5238                            (buffer-substring (match-beginning 1)
5239                                              (match-end 1))
5240                          ;; If there was no message-id, we just fake one
5241                          ;; to make subsequent routines simpler.
5242                          (nnheader-generate-fake-message-id))))
5243             ;; References.
5244             (progn
5245               (goto-char p)
5246               (if (search-forward "\nreferences:" nil t)
5247                   (progn
5248                     (setq end (point))
5249                     (prog1
5250                         (nnheader-header-value)
5251                       (setq ref
5252                             (buffer-substring
5253                              (progn
5254                                ;; (end-of-line)
5255                                (search-backward ">" end t)
5256                                (1+ (point)))
5257                              (progn
5258                                (search-backward "<" end t)
5259                                (point))))))
5260                 ;; Get the references from the in-reply-to header if there
5261                 ;; were no references and the in-reply-to header looks
5262                 ;; promising.
5263                 (if (and (search-forward "\nin-reply-to:" nil t)
5264                          (setq in-reply-to (nnheader-header-value))
5265                          (string-match "<[^>]+>" in-reply-to))
5266                     (let (ref2)
5267                       (setq ref (substring in-reply-to (match-beginning 0)
5268                                            (match-end 0)))
5269                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5270                         (setq ref2 (substring in-reply-to (match-beginning 0)
5271                                               (match-end 0)))
5272                         (when (> (length ref2) (length ref))
5273                           (setq ref ref2)))
5274                       ref)
5275                   (setq ref nil))))
5276             ;; Chars.
5277             (progn
5278               (goto-char p)
5279               (if (search-forward "\nchars: " nil t)
5280                   (if (numberp (setq chars (ignore-errors (read cur))))
5281                       chars -1)
5282                 -1))
5283             ;; Lines.
5284             (progn
5285               (goto-char p)
5286               (if (search-forward "\nlines: " nil t)
5287                   (if (numberp (setq lines (ignore-errors (read cur))))
5288                       lines -1)
5289                 -1))
5290             ;; Xref.
5291             (progn
5292               (goto-char p)
5293               (and (search-forward "\nxref:" nil t)
5294                    (nnheader-header-value)))
5295             ;; Extra.
5296             (when gnus-extra-headers
5297               (let ((extra gnus-extra-headers)
5298                     out)
5299                 (while extra
5300                   (goto-char p)
5301                   (when (search-forward
5302                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5303                     (push (cons (car extra) (nnheader-header-value)) out))
5304                   (pop extra))
5305                 out))))
5306           (goto-char p)
5307           (if (and (search-forward "\ncontent-type: " nil t)
5308                    (setq ctype (nnheader-header-value)))
5309               (mime-entity-set-content-type-internal
5310                header (mime-parse-Content-Type ctype)))
5311           (when (equal id ref)
5312             (setq ref nil))
5313
5314           (when gnus-alter-header-function
5315             (funcall gnus-alter-header-function header)
5316             (setq id (mail-header-id header)
5317                   ref (gnus-parent-id (mail-header-references header))))
5318
5319           (when (setq header
5320                       (gnus-dependencies-add-header
5321                        header dependencies force-new))
5322             (push header headers))
5323           (goto-char (point-max))
5324           (widen))
5325         (nreverse headers)))))
5326
5327 ;; Goes through the xover lines and returns a list of vectors
5328 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5329                                                   force-new dependencies
5330                                                   group also-fetch-heads)
5331   "Parse the news overview data in the server buffer.
5332 Return a list of headers that match SEQUENCE (see
5333 `nntp-retrieve-headers')."
5334   ;; Get the Xref when the users reads the articles since most/some
5335   ;; NNTP servers do not include Xrefs when using XOVER.
5336   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5337   (let ((mail-parse-charset gnus-newsgroup-charset)
5338         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5339         (cur nntp-server-buffer)
5340         (dependencies (or dependencies gnus-newsgroup-dependencies))
5341         (allp (cond
5342                ((eq gnus-read-all-available-headers t)
5343                 t)
5344                ((stringp gnus-read-all-available-headers)
5345                 (string-match gnus-read-all-available-headers group))
5346                (t
5347                 nil)))
5348         number headers header)
5349     (save-excursion
5350       (set-buffer nntp-server-buffer)
5351       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5352       ;; Allow the user to mangle the headers before parsing them.
5353       (gnus-run-hooks 'gnus-parse-headers-hook)
5354       (goto-char (point-min))
5355       (while (not (eobp))
5356         (condition-case ()
5357             (while (and (or sequence allp)
5358                         (not (eobp)))
5359               (setq number (read cur))
5360               (when (not allp)
5361                 (while (and sequence
5362                             (< (car sequence) number))
5363                   (setq sequence (cdr sequence))))
5364               (when (and (or allp
5365                              (and sequence
5366                                   (eq number (car sequence))))
5367                          (progn
5368                            (setq sequence (cdr sequence))
5369                            (setq header (inline
5370                                           (gnus-nov-parse-line
5371                                            number dependencies force-new)))))
5372                 (push header headers))
5373               (forward-line 1))
5374           (error
5375            (gnus-error 4 "Strange nov line (%d)"
5376                        (count-lines (point-min) (point)))))
5377         (forward-line 1))
5378       ;; A common bug in inn is that if you have posted an article and
5379       ;; then retrieves the active file, it will answer correctly --
5380       ;; the new article is included.  However, a NOV entry for the
5381       ;; article may not have been generated yet, so this may fail.
5382       ;; We work around this problem by retrieving the last few
5383       ;; headers using HEAD.
5384       (if (or (not also-fetch-heads)
5385               (not sequence))
5386           ;; We (probably) got all the headers.
5387           (nreverse headers)
5388         (let ((gnus-nov-is-evil t))
5389           (nconc
5390            (nreverse headers)
5391            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5392              (gnus-get-newsgroup-headers))))))))
5393
5394 (defun gnus-article-get-xrefs ()
5395   "Fill in the Xref value in `gnus-current-headers', if necessary.
5396 This is meant to be called in `gnus-article-internal-prepare-hook'."
5397   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5398                                  gnus-current-headers)))
5399     (or (not gnus-use-cross-reference)
5400         (not headers)
5401         (and (mail-header-xref headers)
5402              (not (string= (mail-header-xref headers) "")))
5403         (let ((case-fold-search t)
5404               xref)
5405           (save-restriction
5406             (nnheader-narrow-to-headers)
5407             (goto-char (point-min))
5408             (when (or (and (not (eobp))
5409                            (eq (downcase (char-after)) ?x)
5410                            (looking-at "Xref:"))
5411                       (search-forward "\nXref:" nil t))
5412               (goto-char (1+ (match-end 0)))
5413               (setq xref (buffer-substring (point)
5414                                            (progn (end-of-line) (point))))
5415               (mail-header-set-xref headers xref)))))))
5416
5417 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5418   "Find article ID and insert the summary line for that article.
5419 OLD-HEADER can either be a header or a line number to insert
5420 the subject line on."
5421   (let* ((line (and (numberp old-header) old-header))
5422          (old-header (and (vectorp old-header) old-header))
5423          (header (cond ((and old-header use-old-header)
5424                         old-header)
5425                        ((and (numberp id)
5426                              (gnus-number-to-header id))
5427                         (gnus-number-to-header id))
5428                        (t
5429                         (gnus-read-header id))))
5430          (number (and (numberp id) id))
5431          d)
5432     (when header
5433       ;; Rebuild the thread that this article is part of and go to the
5434       ;; article we have fetched.
5435       (when (and (not gnus-show-threads)
5436                  old-header)
5437         (when (and number
5438                    (setq d (gnus-data-find (mail-header-number old-header))))
5439           (goto-char (gnus-data-pos d))
5440           (gnus-data-remove
5441            number
5442            (- (gnus-point-at-bol)
5443               (prog1
5444                   (1+ (gnus-point-at-eol))
5445                 (gnus-delete-line))))))
5446       (when old-header
5447         (mail-header-set-number header (mail-header-number old-header)))
5448       (setq gnus-newsgroup-sparse
5449             (delq (setq number (mail-header-number header))
5450                   gnus-newsgroup-sparse))
5451       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5452       (push number gnus-newsgroup-limit)
5453       (gnus-rebuild-thread (mail-header-id header) line)
5454       (gnus-summary-goto-subject number nil t))
5455     (when (and (numberp number)
5456                (> number 0))
5457       ;; We have to update the boundaries even if we can't fetch the
5458       ;; article if ID is a number -- so that the next `P' or `N'
5459       ;; command will fetch the previous (or next) article even
5460       ;; if the one we tried to fetch this time has been canceled.
5461       (when (> number gnus-newsgroup-end)
5462         (setq gnus-newsgroup-end number))
5463       (when (< number gnus-newsgroup-begin)
5464         (setq gnus-newsgroup-begin number))
5465       (setq gnus-newsgroup-unselected
5466             (delq number gnus-newsgroup-unselected)))
5467     ;; Report back a success?
5468     (and header (mail-header-number header))))
5469
5470 ;;; Process/prefix in the summary buffer
5471
5472 (defun gnus-summary-work-articles (n)
5473   "Return a list of articles to be worked upon.
5474 The prefix argument, the list of process marked articles, and the
5475 current article will be taken into consideration."
5476   (save-excursion
5477     (set-buffer gnus-summary-buffer)
5478     (cond
5479      (n
5480       ;; A numerical prefix has been given.
5481       (setq n (prefix-numeric-value n))
5482       (let ((backward (< n 0))
5483             (n (abs (prefix-numeric-value n)))
5484             articles article)
5485         (save-excursion
5486           (while
5487               (and (> n 0)
5488                    (push (setq article (gnus-summary-article-number))
5489                          articles)
5490                    (if backward
5491                        (gnus-summary-find-prev nil article)
5492                      (gnus-summary-find-next nil article)))
5493             (decf n)))
5494         (nreverse articles)))
5495      ((and (gnus-region-active-p) (mark))
5496       (message "region active")
5497       ;; Work on the region between point and mark.
5498       (let ((max (max (point) (mark)))
5499             articles article)
5500         (save-excursion
5501           (goto-char (min (point) (mark)))
5502           (while
5503               (and
5504                (push (setq article (gnus-summary-article-number)) articles)
5505                (gnus-summary-find-next nil article)
5506                (< (point) max)))
5507           (nreverse articles))))
5508      (gnus-newsgroup-processable
5509       ;; There are process-marked articles present.
5510       ;; Save current state.
5511       (gnus-summary-save-process-mark)
5512       ;; Return the list.
5513       (reverse gnus-newsgroup-processable))
5514      (t
5515       ;; Just return the current article.
5516       (list (gnus-summary-article-number))))))
5517
5518 (defmacro gnus-summary-iterate (arg &rest forms)
5519   "Iterate over the process/prefixed articles and do FORMS.
5520 ARG is the interactive prefix given to the command.  FORMS will be
5521 executed with point over the summary line of the articles."
5522   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5523     `(let ((,articles (gnus-summary-work-articles ,arg)))
5524        (while ,articles
5525          (gnus-summary-goto-subject (car ,articles))
5526          ,@forms
5527          (pop ,articles)))))
5528
5529 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5530 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5531
5532 (defun gnus-summary-save-process-mark ()
5533   "Push the current set of process marked articles on the stack."
5534   (interactive)
5535   (push (copy-sequence gnus-newsgroup-processable)
5536         gnus-newsgroup-process-stack))
5537
5538 (defun gnus-summary-kill-process-mark ()
5539   "Push the current set of process marked articles on the stack and unmark."
5540   (interactive)
5541   (gnus-summary-save-process-mark)
5542   (gnus-summary-unmark-all-processable))
5543
5544 (defun gnus-summary-yank-process-mark ()
5545   "Pop the last process mark state off the stack and restore it."
5546   (interactive)
5547   (unless gnus-newsgroup-process-stack
5548     (error "Empty mark stack"))
5549   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5550
5551 (defun gnus-summary-process-mark-set (set)
5552   "Make SET into the current process marked articles."
5553   (gnus-summary-unmark-all-processable)
5554   (while set
5555     (gnus-summary-set-process-mark (pop set))))
5556
5557 ;;; Searching and stuff
5558
5559 (defun gnus-summary-search-group (&optional backward use-level)
5560   "Search for next unread newsgroup.
5561 If optional argument BACKWARD is non-nil, search backward instead."
5562   (save-excursion
5563     (set-buffer gnus-group-buffer)
5564     (when (gnus-group-search-forward
5565            backward nil (if use-level (gnus-group-group-level) nil))
5566       (gnus-group-group-name))))
5567
5568 (defun gnus-summary-best-group (&optional exclude-group)
5569   "Find the name of the best unread group.
5570 If EXCLUDE-GROUP, do not go to this group."
5571   (save-excursion
5572     (set-buffer gnus-group-buffer)
5573     (save-excursion
5574       (gnus-group-best-unread-group exclude-group))))
5575
5576 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5577   (if backward (gnus-summary-find-prev)
5578     (let* ((dummy (gnus-summary-article-intangible-p))
5579            (article (or article (gnus-summary-article-number)))
5580            (arts (gnus-data-find-list article))
5581            result)
5582       (when (and (not dummy)
5583                  (or (not gnus-summary-check-current)
5584                      (not unread)
5585                      (not (gnus-data-unread-p (car arts)))))
5586         (setq arts (cdr arts)))
5587       (when (setq result
5588                   (if unread
5589                       (progn
5590                         (while arts
5591                           (when (or (and undownloaded
5592                                          (eq gnus-undownloaded-mark
5593                                              (gnus-data-mark (car arts))))
5594                                     (gnus-data-unread-p (car arts)))
5595                             (setq result (car arts)
5596                                   arts nil))
5597                           (setq arts (cdr arts)))
5598                         result)
5599                     (car arts)))
5600         (goto-char (gnus-data-pos result))
5601         (gnus-data-number result)))))
5602
5603 (defun gnus-summary-find-prev (&optional unread article)
5604   (let* ((eobp (eobp))
5605          (article (or article (gnus-summary-article-number)))
5606          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5607          result)
5608     (when (and (not eobp)
5609                (or (not gnus-summary-check-current)
5610                    (not unread)
5611                    (not (gnus-data-unread-p (car arts)))))
5612       (setq arts (cdr arts)))
5613     (when (setq result
5614                 (if unread
5615                     (progn
5616                       (while arts
5617                         (when (gnus-data-unread-p (car arts))
5618                           (setq result (car arts)
5619                                 arts nil))
5620                         (setq arts (cdr arts)))
5621                       result)
5622                   (car arts)))
5623       (goto-char (gnus-data-pos result))
5624       (gnus-data-number result))))
5625
5626 (defun gnus-summary-find-subject (subject &optional unread backward article)
5627   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5628          (article (or article (gnus-summary-article-number)))
5629          (articles (gnus-data-list backward))
5630          (arts (gnus-data-find-list article articles))
5631          result)
5632     (when (or (not gnus-summary-check-current)
5633               (not unread)
5634               (not (gnus-data-unread-p (car arts))))
5635       (setq arts (cdr arts)))
5636     (while arts
5637       (and (or (not unread)
5638                (gnus-data-unread-p (car arts)))
5639            (vectorp (gnus-data-header (car arts)))
5640            (gnus-subject-equal
5641             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5642            (setq result (car arts)
5643                  arts nil))
5644       (setq arts (cdr arts)))
5645     (and result
5646          (goto-char (gnus-data-pos result))
5647          (gnus-data-number result))))
5648
5649 (defun gnus-summary-search-forward (&optional unread subject backward)
5650   "Search forward for an article.
5651 If UNREAD, look for unread articles.  If SUBJECT, look for
5652 articles with that subject.  If BACKWARD, search backward instead."
5653   (cond (subject (gnus-summary-find-subject subject unread backward))
5654         (backward (gnus-summary-find-prev unread))
5655         (t (gnus-summary-find-next unread))))
5656
5657 (defun gnus-recenter (&optional n)
5658   "Center point in window and redisplay frame.
5659 Also do horizontal recentering."
5660   (interactive "P")
5661   (when (and gnus-auto-center-summary
5662              (not (eq gnus-auto-center-summary 'vertical)))
5663     (gnus-horizontal-recenter))
5664   (recenter n))
5665
5666 (defun gnus-summary-recenter ()
5667   "Center point in the summary window.
5668 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5669 displayed, no centering will be performed."
5670   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5671   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5672   (interactive)
5673   (let* ((top (cond ((< (window-height) 4) 0)
5674                     ((< (window-height) 7) 1)
5675                     (t (if (numberp gnus-auto-center-summary)
5676                            gnus-auto-center-summary
5677                          2))))
5678          (height (1- (window-height)))
5679          (bottom (save-excursion (goto-char (point-max))
5680                                  (forward-line (- height))
5681                                  (point)))
5682          (window (get-buffer-window (current-buffer))))
5683     ;; The user has to want it.
5684     (when gnus-auto-center-summary
5685       (when (get-buffer-window gnus-article-buffer)
5686         ;; Only do recentering when the article buffer is displayed,
5687         ;; Set the window start to either `bottom', which is the biggest
5688         ;; possible valid number, or the second line from the top,
5689         ;; whichever is the least.
5690         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5691           (if (> bottom top-pos)
5692               ;; Keep the second line from the top visible
5693               (set-window-start window top-pos t)
5694             ;; Try to keep the bottom line visible; if it's partially
5695             ;; obscured, either scroll one more line to make it fully
5696             ;; visible, or revert to using TOP-POS.
5697             (save-excursion
5698               (goto-char (point-max))
5699               (forward-line -1)
5700               (let ((last-line-start (point)))
5701                 (goto-char bottom)
5702                 (set-window-start window (point) t)
5703                 (when (not (pos-visible-in-window-p last-line-start window))
5704                   (forward-line 1)
5705                   (set-window-start window (min (point) top-pos) t)))))))
5706       ;; Do horizontal recentering while we're at it.
5707       (when (and (get-buffer-window (current-buffer) t)
5708                  (not (eq gnus-auto-center-summary 'vertical)))
5709         (let ((selected (selected-window)))
5710           (select-window (get-buffer-window (current-buffer) t))
5711           (gnus-summary-position-point)
5712           (gnus-horizontal-recenter)
5713           (select-window selected))))))
5714
5715 (defun gnus-summary-jump-to-group (newsgroup)
5716   "Move point to NEWSGROUP in group mode buffer."
5717   ;; Keep update point of group mode buffer if visible.
5718   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5719       (save-window-excursion
5720         ;; Take care of tree window mode.
5721         (when (get-buffer-window gnus-group-buffer)
5722           (pop-to-buffer gnus-group-buffer))
5723         (gnus-group-jump-to-group newsgroup))
5724     (save-excursion
5725       ;; Take care of tree window mode.
5726       (if (get-buffer-window gnus-group-buffer)
5727           (pop-to-buffer gnus-group-buffer)
5728         (set-buffer gnus-group-buffer))
5729       (gnus-group-jump-to-group newsgroup))))
5730
5731 ;; This function returns a list of article numbers based on the
5732 ;; difference between the ranges of read articles in this group and
5733 ;; the range of active articles.
5734 (defun gnus-list-of-unread-articles (group)
5735   (let* ((read (gnus-info-read (gnus-get-info group)))
5736          (active (or (gnus-active group) (gnus-activate-group group)))
5737          (last (cdr active))
5738          first nlast unread)
5739     ;; If none are read, then all are unread.
5740     (if (not read)
5741         (setq first (car active))
5742       ;; If the range of read articles is a single range, then the
5743       ;; first unread article is the article after the last read
5744       ;; article.  Sounds logical, doesn't it?
5745       (if (and (not (listp (cdr read)))
5746                (or (< (car read) (car active))
5747                    (progn (setq read (list read))
5748                           nil)))
5749           (setq first (max (car active) (1+ (cdr read))))
5750         ;; `read' is a list of ranges.
5751         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5752                                   (caar read)))
5753                   1)
5754           (setq first (car active)))
5755         (while read
5756           (when first
5757             (while (< first nlast)
5758               (push first unread)
5759               (setq first (1+ first))))
5760           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5761           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5762           (setq read (cdr read)))))
5763     ;; And add the last unread articles.
5764     (while (<= first last)
5765       (push first unread)
5766       (setq first (1+ first)))
5767     ;; Return the list of unread articles.
5768     (delq 0 (nreverse unread))))
5769
5770 (defun gnus-list-of-read-articles (group)
5771   "Return a list of unread, unticked and non-dormant articles."
5772   (let* ((info (gnus-get-info group))
5773          (marked (gnus-info-marks info))
5774          (active (gnus-active group)))
5775     (and info active
5776          (gnus-set-difference
5777           (gnus-sorted-complement
5778            (gnus-uncompress-range active)
5779            (gnus-list-of-unread-articles group))
5780           (append
5781            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5782            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5783
5784 ;; Various summary commands
5785
5786 (defun gnus-summary-select-article-buffer ()
5787   "Reconfigure windows to show article buffer."
5788   (interactive)
5789   (if (not (gnus-buffer-live-p gnus-article-buffer))
5790       (error "There is no article buffer for this summary buffer")
5791     (gnus-configure-windows 'article)
5792     (select-window (get-buffer-window gnus-article-buffer))))
5793
5794 (defun gnus-summary-universal-argument (arg)
5795   "Perform any operation on all articles that are process/prefixed."
5796   (interactive "P")
5797   (let ((articles (gnus-summary-work-articles arg))
5798         func article)
5799     (if (eq
5800          (setq
5801           func
5802           (key-binding
5803            (read-key-sequence
5804             (substitute-command-keys
5805              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5806          'undefined)
5807         (gnus-error 1 "Undefined key")
5808       (save-excursion
5809         (while articles
5810           (gnus-summary-goto-subject (setq article (pop articles)))
5811           (let (gnus-newsgroup-processable)
5812             (command-execute func))
5813           (gnus-summary-remove-process-mark article)))))
5814   (gnus-summary-position-point))
5815
5816 (defun gnus-summary-toggle-truncation (&optional arg)
5817   "Toggle truncation of summary lines.
5818 With arg, turn line truncation on iff arg is positive."
5819   (interactive "P")
5820   (setq truncate-lines
5821         (if (null arg) (not truncate-lines)
5822           (> (prefix-numeric-value arg) 0)))
5823   (redraw-display))
5824
5825 (defun gnus-summary-reselect-current-group (&optional all rescan)
5826   "Exit and then reselect the current newsgroup.
5827 The prefix argument ALL means to select all articles."
5828   (interactive "P")
5829   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5830     (error "Ephemeral groups can't be reselected"))
5831   (let ((current-subject (gnus-summary-article-number))
5832         (group gnus-newsgroup-name))
5833     (setq gnus-newsgroup-begin nil)
5834     (gnus-summary-exit)
5835     ;; We have to adjust the point of group mode buffer because
5836     ;; point was moved to the next unread newsgroup by exiting.
5837     (gnus-summary-jump-to-group group)
5838     (when rescan
5839       (save-excursion
5840         (save-window-excursion
5841           ;; Don't show group contents.
5842           (set-window-start (selected-window) (point-max))
5843           (gnus-group-get-new-news-this-group 1))))
5844     (gnus-group-read-group all t)
5845     (gnus-summary-goto-subject current-subject nil t)))
5846
5847 (defun gnus-summary-rescan-group (&optional all)
5848   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5849   (interactive "P")
5850   (gnus-summary-reselect-current-group all t))
5851
5852 (defun gnus-summary-update-info (&optional non-destructive)
5853   (save-excursion
5854     (let ((group gnus-newsgroup-name))
5855       (when group
5856         (when gnus-newsgroup-kill-headers
5857           (setq gnus-newsgroup-killed
5858                 (gnus-compress-sequence
5859                  (nconc
5860                   (gnus-set-sorted-intersection
5861                    (gnus-uncompress-range gnus-newsgroup-killed)
5862                    (setq gnus-newsgroup-unselected
5863                          (sort gnus-newsgroup-unselected '<)))
5864                   (setq gnus-newsgroup-unreads
5865                         (sort gnus-newsgroup-unreads '<)))
5866                  t)))
5867         (unless (listp (cdr gnus-newsgroup-killed))
5868           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5869         (let ((headers gnus-newsgroup-headers))
5870           ;; Set the new ranges of read articles.
5871           (save-excursion
5872             (set-buffer gnus-group-buffer)
5873             (gnus-undo-force-boundary))
5874           (gnus-update-read-articles
5875            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5876           ;; Set the current article marks.
5877           (let ((gnus-newsgroup-scored
5878                  (if (and (not gnus-save-score)
5879                           (not non-destructive))
5880                      nil
5881                    gnus-newsgroup-scored)))
5882             (save-excursion
5883               (gnus-update-marks)))
5884           ;; Do the cross-ref thing.
5885           (when gnus-use-cross-reference
5886             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5887           ;; Do not switch windows but change the buffer to work.
5888           (set-buffer gnus-group-buffer)
5889           (unless (gnus-ephemeral-group-p group)
5890             (gnus-group-update-group group)))))))
5891
5892 (defun gnus-summary-save-newsrc (&optional force)
5893   "Save the current number of read/marked articles in the dribble buffer.
5894 The dribble buffer will then be saved.
5895 If FORCE (the prefix), also save the .newsrc file(s)."
5896   (interactive "P")
5897   (gnus-summary-update-info t)
5898   (if force
5899       (gnus-save-newsrc-file)
5900     (gnus-dribble-save)))
5901
5902 (defun gnus-summary-exit (&optional temporary)
5903   "Exit reading current newsgroup, and then return to group selection mode.
5904 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5905   (interactive)
5906   (gnus-set-global-variables)
5907   (gnus-kill-save-kill-buffer)
5908   (gnus-async-halt-prefetch)
5909   (let* ((group gnus-newsgroup-name)
5910          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5911          (mode major-mode)
5912          (group-point nil)
5913          (buf (current-buffer)))
5914     (unless quit-config
5915       ;; Do adaptive scoring, and possibly save score files.
5916       (when gnus-newsgroup-adaptive
5917         (gnus-score-adaptive))
5918       (when gnus-use-scoring
5919         (gnus-score-save)))
5920     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5921     ;; If we have several article buffers, we kill them at exit.
5922     (unless gnus-single-article-buffer
5923       (gnus-kill-buffer gnus-original-article-buffer)
5924       (setq gnus-article-current nil))
5925     (when gnus-use-cache
5926       (gnus-cache-possibly-remove-articles)
5927       (gnus-cache-save-buffers))
5928     (gnus-async-prefetch-remove-group group)
5929     (when gnus-suppress-duplicates
5930       (gnus-dup-enter-articles))
5931     (when gnus-use-trees
5932       (gnus-tree-close group))
5933     (when gnus-use-cache
5934       (gnus-cache-write-active))
5935     ;; Remove entries for this group.
5936     (nnmail-purge-split-history (gnus-group-real-name group))
5937     ;; Make all changes in this group permanent.
5938     (unless quit-config
5939       (gnus-run-hooks 'gnus-exit-group-hook)
5940       (gnus-summary-update-info))
5941     (gnus-close-group group)
5942     ;; Make sure where we were, and go to next newsgroup.
5943     (set-buffer gnus-group-buffer)
5944     (unless quit-config
5945       (gnus-group-jump-to-group group))
5946     (gnus-run-hooks 'gnus-summary-exit-hook)
5947     (unless (or quit-config
5948                 ;; If this group has disappeared from the summary
5949                 ;; buffer, don't skip forwards.
5950                 (not (string= group (gnus-group-group-name))))
5951       (gnus-group-next-unread-group 1))
5952     (setq group-point (point))
5953     (if temporary
5954         nil                             ;Nothing to do.
5955       ;; If we have several article buffers, we kill them at exit.
5956       (unless gnus-single-article-buffer
5957         (gnus-kill-buffer gnus-article-buffer)
5958         (gnus-kill-buffer gnus-original-article-buffer)
5959         (setq gnus-article-current nil))
5960       (set-buffer buf)
5961       (if (not gnus-kill-summary-on-exit)
5962           (progn
5963             (gnus-deaden-summary)
5964             (setq mode nil))
5965         ;; We set all buffer-local variables to nil.  It is unclear why
5966         ;; this is needed, but if we don't, buffer-local variables are
5967         ;; not garbage-collected, it seems.  This would the lead to en
5968         ;; ever-growing Emacs.
5969         (gnus-summary-clear-local-variables)
5970         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5971           (gnus-summary-clear-local-variables))
5972         (when (get-buffer gnus-article-buffer)
5973           (bury-buffer gnus-article-buffer))
5974         ;; We clear the global counterparts of the buffer-local
5975         ;; variables as well, just to be on the safe side.
5976         (set-buffer gnus-group-buffer)
5977         (gnus-summary-clear-local-variables)
5978         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5979           (gnus-summary-clear-local-variables)))
5980       (setq gnus-current-select-method gnus-select-method)
5981       (pop-to-buffer gnus-group-buffer)
5982       (if (not quit-config)
5983           (progn
5984             (goto-char group-point)
5985             (gnus-configure-windows 'group 'force)
5986             (unless (pos-visible-in-window-p)
5987               (forward-line (/ (static-if (featurep 'xemacs)
5988                                    (window-displayed-height)
5989                                  (1- (window-height)))
5990                                -2))
5991               (set-window-start (selected-window) (point))
5992               (goto-char group-point)))
5993         (gnus-handle-ephemeral-exit quit-config))
5994       ;; Return to group mode buffer.
5995       (when (eq mode 'gnus-summary-mode)
5996         (gnus-kill-buffer buf))
5997       ;; Clear the current group name.
5998       (unless quit-config
5999         (setq gnus-newsgroup-name nil)))))
6000
6001 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6002 (defun gnus-summary-exit-no-update (&optional no-questions)
6003   "Quit reading current newsgroup without updating read article info."
6004   (interactive)
6005   (let* ((group gnus-newsgroup-name)
6006          (quit-config (gnus-group-quit-config group)))
6007     (when (or no-questions
6008               gnus-expert-user
6009               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6010       (gnus-async-halt-prefetch)
6011       (mapcar 'funcall
6012               (delq 'gnus-summary-expire-articles
6013                     (copy-sequence gnus-summary-prepare-exit-hook)))
6014       ;; If we have several article buffers, we kill them at exit.
6015       (unless gnus-single-article-buffer
6016         (gnus-kill-buffer gnus-article-buffer)
6017         (gnus-kill-buffer gnus-original-article-buffer)
6018         (setq gnus-article-current nil))
6019       (if (not gnus-kill-summary-on-exit)
6020           (gnus-deaden-summary)
6021         (gnus-close-group group)
6022         (gnus-summary-clear-local-variables)
6023         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6024           (gnus-summary-clear-local-variables))
6025         (set-buffer gnus-group-buffer)
6026         (gnus-summary-clear-local-variables)
6027         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6028           (gnus-summary-clear-local-variables))
6029         (when (get-buffer gnus-summary-buffer)
6030           (kill-buffer gnus-summary-buffer)))
6031       (unless gnus-single-article-buffer
6032         (setq gnus-article-current nil))
6033       (when gnus-use-trees
6034         (gnus-tree-close group))
6035       (gnus-async-prefetch-remove-group group)
6036       (when (get-buffer gnus-article-buffer)
6037         (bury-buffer gnus-article-buffer))
6038       ;; Return to the group buffer.
6039       (gnus-configure-windows 'group 'force)
6040       ;; Clear the current group name.
6041       (setq gnus-newsgroup-name nil)
6042       (when (equal (gnus-group-group-name) group)
6043         (gnus-group-next-unread-group 1))
6044       (when quit-config
6045         (gnus-handle-ephemeral-exit quit-config)))))
6046
6047 (defun gnus-handle-ephemeral-exit (quit-config)
6048   "Handle movement when leaving an ephemeral group.
6049 The state which existed when entering the ephemeral is reset."
6050   (if (not (buffer-name (car quit-config)))
6051       (gnus-configure-windows 'group 'force)
6052     (set-buffer (car quit-config))
6053     (cond ((eq major-mode 'gnus-summary-mode)
6054            (gnus-set-global-variables))
6055           ((eq major-mode 'gnus-article-mode)
6056            (save-excursion
6057              ;; The `gnus-summary-buffer' variable may point
6058              ;; to the old summary buffer when using a single
6059              ;; article buffer.
6060              (unless (gnus-buffer-live-p gnus-summary-buffer)
6061                (set-buffer gnus-group-buffer))
6062              (set-buffer gnus-summary-buffer)
6063              (gnus-set-global-variables))))
6064     (if (or (eq (cdr quit-config) 'article)
6065             (eq (cdr quit-config) 'pick))
6066         (progn
6067           ;; The current article may be from the ephemeral group
6068           ;; thus it is best that we reload this article
6069           (gnus-summary-show-article)
6070           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6071               (gnus-configure-windows 'pick 'force)
6072             (gnus-configure-windows (cdr quit-config) 'force)))
6073       (gnus-configure-windows (cdr quit-config) 'force))
6074     (when (eq major-mode 'gnus-summary-mode)
6075       (gnus-summary-next-subject 1 nil t)
6076       (gnus-summary-recenter)
6077       (gnus-summary-position-point))))
6078
6079 (defun gnus-summary-preview-mime-message ()
6080   "MIME decode and play this message."
6081   (interactive)
6082   (let ((gnus-break-pages nil)
6083         (gnus-show-mime t))
6084     (gnus-summary-select-article gnus-show-all-headers t))
6085   (select-window (get-buffer-window gnus-article-buffer)))
6086
6087 ;;; Dead summaries.
6088
6089 (defvar gnus-dead-summary-mode-map nil)
6090
6091 (unless gnus-dead-summary-mode-map
6092   (setq gnus-dead-summary-mode-map (make-keymap))
6093   (suppress-keymap gnus-dead-summary-mode-map)
6094   (substitute-key-definition
6095    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6096   (let ((keys '("\C-d" "\r" "\177" [delete])))
6097     (while keys
6098       (define-key gnus-dead-summary-mode-map
6099         (pop keys) 'gnus-summary-wake-up-the-dead))))
6100
6101 (defvar gnus-dead-summary-mode nil
6102   "Minor mode for Gnus summary buffers.")
6103
6104 (defun gnus-dead-summary-mode (&optional arg)
6105   "Minor mode for Gnus summary buffers."
6106   (interactive "P")
6107   (when (eq major-mode 'gnus-summary-mode)
6108     (make-local-variable 'gnus-dead-summary-mode)
6109     (setq gnus-dead-summary-mode
6110           (if (null arg) (not gnus-dead-summary-mode)
6111             (> (prefix-numeric-value arg) 0)))
6112     (when gnus-dead-summary-mode
6113       (gnus-add-minor-mode
6114        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6115
6116 (defun gnus-deaden-summary ()
6117   "Make the current summary buffer into a dead summary buffer."
6118   ;; Kill any previous dead summary buffer.
6119   (when (and gnus-dead-summary
6120              (buffer-name gnus-dead-summary))
6121     (save-excursion
6122       (set-buffer gnus-dead-summary)
6123       (when gnus-dead-summary-mode
6124         (kill-buffer (current-buffer)))))
6125   ;; Make this the current dead summary.
6126   (setq gnus-dead-summary (current-buffer))
6127   (gnus-dead-summary-mode 1)
6128   (let ((name (buffer-name)))
6129     (when (string-match "Summary" name)
6130       (rename-buffer
6131        (concat (substring name 0 (match-beginning 0)) "Dead "
6132                (substring name (match-beginning 0)))
6133        t)
6134       (bury-buffer))))
6135
6136 (defun gnus-kill-or-deaden-summary (buffer)
6137   "Kill or deaden the summary BUFFER."
6138   (save-excursion
6139     (when (and (buffer-name buffer)
6140                (not gnus-single-article-buffer))
6141       (save-excursion
6142         (set-buffer buffer)
6143         (gnus-kill-buffer gnus-article-buffer)
6144         (gnus-kill-buffer gnus-original-article-buffer)))
6145     (cond (gnus-kill-summary-on-exit
6146            (when (and gnus-use-trees
6147                       (gnus-buffer-exists-p buffer))
6148              (save-excursion
6149                (set-buffer buffer)
6150                (gnus-tree-close gnus-newsgroup-name)))
6151            (gnus-kill-buffer buffer))
6152           ((gnus-buffer-exists-p buffer)
6153            (save-excursion
6154              (set-buffer buffer)
6155              (gnus-deaden-summary))))))
6156
6157 (defun gnus-summary-wake-up-the-dead (&rest args)
6158   "Wake up the dead summary buffer."
6159   (interactive)
6160   (gnus-dead-summary-mode -1)
6161   (let ((name (buffer-name)))
6162     (when (string-match "Dead " name)
6163       (rename-buffer
6164        (concat (substring name 0 (match-beginning 0))
6165                (substring name (match-end 0)))
6166        t)))
6167   (gnus-message 3 "This dead summary is now alive again"))
6168
6169 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6170 (defun gnus-summary-fetch-faq (&optional faq-dir)
6171   "Fetch the FAQ for the current group.
6172 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6173 in."
6174   (interactive
6175    (list
6176     (when current-prefix-arg
6177       (completing-read
6178        "Faq dir: " (and (listp gnus-group-faq-directory)
6179                         (mapcar (lambda (file) (list file))
6180                                 gnus-group-faq-directory))))))
6181   (let (gnus-faq-buffer)
6182     (when (setq gnus-faq-buffer
6183                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6184       (gnus-configure-windows 'summary-faq))))
6185
6186 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6187 (defun gnus-summary-describe-group (&optional force)
6188   "Describe the current newsgroup."
6189   (interactive "P")
6190   (gnus-group-describe-group force gnus-newsgroup-name))
6191
6192 (defun gnus-summary-describe-briefly ()
6193   "Describe summary mode commands briefly."
6194   (interactive)
6195   (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")))
6196
6197 ;; Walking around group mode buffer from summary mode.
6198
6199 (defun gnus-summary-next-group (&optional no-article target-group backward)
6200   "Exit current newsgroup and then select next unread newsgroup.
6201 If prefix argument NO-ARTICLE is non-nil, no article is selected
6202 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6203 previous group instead."
6204   (interactive "P")
6205   ;; Stop pre-fetching.
6206   (gnus-async-halt-prefetch)
6207   (let ((current-group gnus-newsgroup-name)
6208         (current-buffer (current-buffer))
6209         entered)
6210     ;; First we semi-exit this group to update Xrefs and all variables.
6211     ;; We can't do a real exit, because the window conf must remain
6212     ;; the same in case the user is prompted for info, and we don't
6213     ;; want the window conf to change before that...
6214     (gnus-summary-exit t)
6215     (while (not entered)
6216       ;; Then we find what group we are supposed to enter.
6217       (set-buffer gnus-group-buffer)
6218       (gnus-group-jump-to-group current-group)
6219       (setq target-group
6220             (or target-group
6221                 (if (eq gnus-keep-same-level 'best)
6222                     (gnus-summary-best-group gnus-newsgroup-name)
6223                   (gnus-summary-search-group backward gnus-keep-same-level))))
6224       (if (not target-group)
6225           ;; There are no further groups, so we return to the group
6226           ;; buffer.
6227           (progn
6228             (gnus-message 5 "Returning to the group buffer")
6229             (setq entered t)
6230             (when (gnus-buffer-live-p current-buffer)
6231               (set-buffer current-buffer)
6232               (gnus-summary-exit))
6233             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6234         ;; We try to enter the target group.
6235         (gnus-group-jump-to-group target-group)
6236         (let ((unreads (gnus-group-group-unread)))
6237           (if (and (or (eq t unreads)
6238                        (and unreads (not (zerop unreads))))
6239                    (gnus-summary-read-group
6240                     target-group nil no-article
6241                     (and (buffer-name current-buffer) current-buffer)
6242                     nil backward))
6243               (setq entered t)
6244             (setq current-group target-group
6245                   target-group nil)))))))
6246
6247 (defun gnus-summary-prev-group (&optional no-article)
6248   "Exit current newsgroup and then select previous unread newsgroup.
6249 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6250   (interactive "P")
6251   (gnus-summary-next-group no-article nil t))
6252
6253 ;; Walking around summary lines.
6254
6255 (defun gnus-summary-first-subject (&optional unread undownloaded)
6256   "Go to the first unread subject.
6257 If UNREAD is non-nil, go to the first unread article.
6258 Returns the article selected or nil if there are no unread articles."
6259   (interactive "P")
6260   (prog1
6261       (cond
6262        ;; Empty summary.
6263        ((null gnus-newsgroup-data)
6264         (gnus-message 3 "No articles in the group")
6265         nil)
6266        ;; Pick the first article.
6267        ((not unread)
6268         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6269         (gnus-data-number (car gnus-newsgroup-data)))
6270        ;; No unread articles.
6271        ((null gnus-newsgroup-unreads)
6272         (gnus-message 3 "No more unread articles")
6273         nil)
6274        ;; Find the first unread article.
6275        (t
6276         (let ((data gnus-newsgroup-data))
6277           (while (and data
6278                       (and (not (and undownloaded
6279                                      (eq gnus-undownloaded-mark
6280                                          (gnus-data-mark (car data)))))
6281                            (not (gnus-data-unread-p (car data)))))
6282             (setq data (cdr data)))
6283           (when data
6284             (goto-char (gnus-data-pos (car data)))
6285             (gnus-data-number (car data))))))
6286     (gnus-summary-position-point)))
6287
6288 (defun gnus-summary-next-subject (n &optional unread dont-display)
6289   "Go to next N'th summary line.
6290 If N is negative, go to the previous N'th subject line.
6291 If UNREAD is non-nil, only unread articles are selected.
6292 The difference between N and the actual number of steps taken is
6293 returned."
6294   (interactive "p")
6295   (let ((backward (< n 0))
6296         (n (abs n)))
6297     (while (and (> n 0)
6298                 (if backward
6299                     (gnus-summary-find-prev unread)
6300                   (gnus-summary-find-next unread)))
6301       (unless (zerop (setq n (1- n)))
6302         (gnus-summary-show-thread)))
6303     (when (/= 0 n)
6304       (gnus-message 7 "No more%s articles"
6305                     (if unread " unread" "")))
6306     (unless dont-display
6307       (gnus-summary-recenter)
6308       (gnus-summary-position-point))
6309     n))
6310
6311 (defun gnus-summary-next-unread-subject (n)
6312   "Go to next N'th unread summary line."
6313   (interactive "p")
6314   (gnus-summary-next-subject n t))
6315
6316 (defun gnus-summary-prev-subject (n &optional unread)
6317   "Go to previous N'th summary line.
6318 If optional argument UNREAD is non-nil, only unread article is selected."
6319   (interactive "p")
6320   (gnus-summary-next-subject (- n) unread))
6321
6322 (defun gnus-summary-prev-unread-subject (n)
6323   "Go to previous N'th unread summary line."
6324   (interactive "p")
6325   (gnus-summary-next-subject (- n) t))
6326
6327 (defun gnus-summary-goto-subject (article &optional force silent)
6328   "Go the subject line of ARTICLE.
6329 If FORCE, also allow jumping to articles not currently shown."
6330   (interactive "nArticle number: ")
6331   (let ((b (point))
6332         (data (gnus-data-find article)))
6333     ;; We read in the article if we have to.
6334     (and (not data)
6335          force
6336          (gnus-summary-insert-subject
6337           article
6338           (if (or (numberp force) (vectorp force)) force)
6339           t)
6340          (setq data (gnus-data-find article)))
6341     (goto-char b)
6342     (if (not data)
6343         (progn
6344           (unless silent
6345             (gnus-message 3 "Can't find article %d" article))
6346           nil)
6347       (let ((pt (gnus-data-pos data)))
6348         (goto-char pt)
6349         (gnus-summary-set-article-display-arrow pt))
6350       (gnus-summary-position-point)
6351       article)))
6352
6353 ;; Walking around summary lines with displaying articles.
6354
6355 (defun gnus-summary-expand-window (&optional arg)
6356   "Make the summary buffer take up the entire Emacs frame.
6357 Given a prefix, will force an `article' buffer configuration."
6358   (interactive "P")
6359   (if arg
6360       (gnus-configure-windows 'article 'force)
6361     (gnus-configure-windows 'summary 'force)))
6362
6363 (defun gnus-summary-display-article (article &optional all-header)
6364   "Display ARTICLE in article buffer."
6365   (when (gnus-buffer-live-p gnus-article-buffer)
6366     (with-current-buffer gnus-article-buffer
6367       (set-buffer-multibyte t)))
6368   (gnus-set-global-variables)
6369   (when (gnus-buffer-live-p gnus-article-buffer)
6370     (with-current-buffer gnus-article-buffer
6371       (setq gnus-article-charset gnus-newsgroup-charset)
6372       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6373   (if (null article)
6374       nil
6375     (prog1
6376         (if gnus-summary-display-article-function
6377             (funcall gnus-summary-display-article-function article all-header)
6378           (gnus-article-prepare article all-header))
6379       (with-current-buffer gnus-article-buffer
6380         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6381              nil))
6382       (gnus-run-hooks 'gnus-select-article-hook)
6383       (when (and gnus-current-article
6384                  (not (zerop gnus-current-article)))
6385         (gnus-summary-goto-subject gnus-current-article))
6386       (gnus-summary-recenter)
6387       (when (and gnus-use-trees gnus-show-threads)
6388         (gnus-possibly-generate-tree article)
6389         (gnus-highlight-selected-tree article))
6390       ;; Successfully display article.
6391       (gnus-article-set-window-start
6392        (cdr (assq article gnus-newsgroup-bookmarks))))))
6393
6394 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6395   "Select the current article.
6396 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6397 non-nil, the article will be re-fetched even if it already present in
6398 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6399 be displayed."
6400   ;; Make sure we are in the summary buffer to work around bbdb bug.
6401   (unless (eq major-mode 'gnus-summary-mode)
6402     (set-buffer gnus-summary-buffer))
6403   (let ((article (or article (gnus-summary-article-number)))
6404         (all-headers (not (not all-headers))) ;Must be T or NIL.
6405         gnus-summary-display-article-function)
6406     (and (not pseudo)
6407          (gnus-summary-article-pseudo-p article)
6408          (error "This is a pseudo-article"))
6409     (save-excursion
6410       (set-buffer gnus-summary-buffer)
6411       (if (or (and gnus-single-article-buffer
6412                    (or (null gnus-current-article)
6413                        (null gnus-article-current)
6414                        (null (get-buffer gnus-article-buffer))
6415                        (not (eq article (cdr gnus-article-current)))
6416                        (not (equal (car gnus-article-current)
6417                                    gnus-newsgroup-name))))
6418               (and (not gnus-single-article-buffer)
6419                    (or (null gnus-current-article)
6420                        (not (eq gnus-current-article article))))
6421               force)
6422           ;; The requested article is different from the current article.
6423           (progn
6424             (gnus-summary-display-article article all-headers)
6425             (when (or all-headers gnus-show-all-headers)
6426               (gnus-article-show-all-headers))
6427             (gnus-article-set-window-start
6428              (cdr (assq article gnus-newsgroup-bookmarks)))
6429             article)
6430         (when (or all-headers gnus-show-all-headers)
6431           (gnus-article-show-all-headers))
6432         'old))))
6433
6434 (defun gnus-summary-force-verify-and-decrypt ()
6435   (interactive)
6436   (let ((mm-verify-option 'known)
6437         (mm-decrypt-option 'known))
6438     (gnus-summary-select-article nil 'force)))
6439
6440 (defun gnus-summary-set-current-mark (&optional current-mark)
6441   "Obsolete function."
6442   nil)
6443
6444 (defun gnus-summary-next-article (&optional unread subject backward push)
6445   "Select the next article.
6446 If UNREAD, only unread articles are selected.
6447 If SUBJECT, only articles with SUBJECT are selected.
6448 If BACKWARD, the previous article is selected instead of the next."
6449   (interactive "P")
6450   (cond
6451    ;; Is there such an article?
6452    ((and (gnus-summary-search-forward unread subject backward)
6453          (or (gnus-summary-display-article (gnus-summary-article-number))
6454              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6455     (gnus-summary-position-point))
6456    ;; If not, we try the first unread, if that is wanted.
6457    ((and subject
6458          gnus-auto-select-same
6459          (gnus-summary-first-unread-article))
6460     (gnus-summary-position-point)
6461     (gnus-message 6 "Wrapped"))
6462    ;; Try to get next/previous article not displayed in this group.
6463    ((and gnus-auto-extend-newsgroup
6464          (not unread) (not subject))
6465     (gnus-summary-goto-article
6466      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6467      nil (count-lines (point-min) (point))))
6468    ;; Go to next/previous group.
6469    (t
6470     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6471       (gnus-summary-jump-to-group gnus-newsgroup-name))
6472     (let ((cmd last-command-char)
6473           (point
6474            (save-excursion
6475              (set-buffer gnus-group-buffer)
6476              (point)))
6477           (group
6478            (if (eq gnus-keep-same-level 'best)
6479                (gnus-summary-best-group gnus-newsgroup-name)
6480              (gnus-summary-search-group backward gnus-keep-same-level))))
6481       ;; For some reason, the group window gets selected.  We change
6482       ;; it back.
6483       (select-window (get-buffer-window (current-buffer)))
6484       ;; Select next unread newsgroup automagically.
6485       (cond
6486        ((or (not gnus-auto-select-next)
6487             (not cmd))
6488         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6489        ((or (eq gnus-auto-select-next 'quietly)
6490             (and (eq gnus-auto-select-next 'slightly-quietly)
6491                  push)
6492             (and (eq gnus-auto-select-next 'almost-quietly)
6493                  (gnus-summary-last-article-p)))
6494         ;; Select quietly.
6495         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6496             (gnus-summary-exit)
6497           (gnus-message 7 "No more%s articles (%s)..."
6498                         (if unread " unread" "")
6499                         (if group (concat "selecting " group)
6500                           "exiting"))
6501           (gnus-summary-next-group nil group backward)))
6502        (t
6503         (when (gnus-key-press-event-p last-input-event)
6504           (gnus-summary-walk-group-buffer
6505            gnus-newsgroup-name cmd unread backward point))))))))
6506
6507 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6508   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6509                       (?\C-p (gnus-group-prev-unread-group 1))))
6510         (cursor-in-echo-area t)
6511         keve key group ended)
6512     (save-excursion
6513       (set-buffer gnus-group-buffer)
6514       (goto-char start)
6515       (setq group
6516             (if (eq gnus-keep-same-level 'best)
6517                 (gnus-summary-best-group gnus-newsgroup-name)
6518               (gnus-summary-search-group backward gnus-keep-same-level))))
6519     (while (not ended)
6520       (gnus-message
6521        5 "No more%s articles%s" (if unread " unread" "")
6522        (if (and group
6523                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6524            (format " (Type %s for %s [%s])"
6525                    (single-key-description cmd) group
6526                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6527          (format " (Type %s to exit %s)"
6528                  (single-key-description cmd)
6529                  gnus-newsgroup-name)))
6530       ;; Confirm auto selection.
6531       (setq key (car (setq keve (gnus-read-event-char))))
6532       (setq ended t)
6533       (cond
6534        ((assq key keystrokes)
6535         (let ((obuf (current-buffer)))
6536           (switch-to-buffer gnus-group-buffer)
6537           (when group
6538             (gnus-group-jump-to-group group))
6539           (eval (cadr (assq key keystrokes)))
6540           (setq group (gnus-group-group-name))
6541           (switch-to-buffer obuf))
6542         (setq ended nil))
6543        ((equal key cmd)
6544         (if (or (not group)
6545                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6546             (gnus-summary-exit)
6547           (gnus-summary-next-group nil group backward)))
6548        (t
6549         (push (cdr keve) unread-command-events))))))
6550
6551 (defun gnus-summary-next-unread-article ()
6552   "Select unread article after current one."
6553   (interactive)
6554   (gnus-summary-next-article
6555    (or (not (eq gnus-summary-goto-unread 'never))
6556        (gnus-summary-last-article-p (gnus-summary-article-number)))
6557    (and gnus-auto-select-same
6558         (gnus-summary-article-subject))))
6559
6560 (defun gnus-summary-prev-article (&optional unread subject)
6561   "Select the article after the current one.
6562 If UNREAD is non-nil, only unread articles are selected."
6563   (interactive "P")
6564   (gnus-summary-next-article unread subject t))
6565
6566 (defun gnus-summary-prev-unread-article ()
6567   "Select unread article before current one."
6568   (interactive)
6569   (gnus-summary-prev-article
6570    (or (not (eq gnus-summary-goto-unread 'never))
6571        (gnus-summary-first-article-p (gnus-summary-article-number)))
6572    (and gnus-auto-select-same
6573         (gnus-summary-article-subject))))
6574
6575 (defun gnus-summary-next-page (&optional lines circular)
6576   "Show next page of the selected article.
6577 If at the end of the current article, select the next article.
6578 LINES says how many lines should be scrolled up.
6579
6580 If CIRCULAR is non-nil, go to the start of the article instead of
6581 selecting the next article when reaching the end of the current
6582 article."
6583   (interactive "P")
6584   (setq gnus-summary-buffer (current-buffer))
6585   (gnus-set-global-variables)
6586   (let ((article (gnus-summary-article-number))
6587         (article-window (get-buffer-window gnus-article-buffer t))
6588         endp)
6589     ;; If the buffer is empty, we have no article.
6590     (unless article
6591       (error "No article to select"))
6592     (gnus-configure-windows 'article)
6593     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6594         (if (and (eq gnus-summary-goto-unread 'never)
6595                  (not (gnus-summary-last-article-p article)))
6596             (gnus-summary-next-article)
6597           (gnus-summary-next-unread-article))
6598       (if (or (null gnus-current-article)
6599               (null gnus-article-current)
6600               (/= article (cdr gnus-article-current))
6601               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6602           ;; Selected subject is different from current article's.
6603           (gnus-summary-display-article article)
6604         (when article-window
6605           (gnus-eval-in-buffer-window gnus-article-buffer
6606             (setq endp (gnus-article-next-page lines)))
6607           (when endp
6608             (cond (circular
6609                    (gnus-summary-beginning-of-article))
6610                   (lines
6611                    (gnus-message 3 "End of message"))
6612                   ((null lines)
6613                    (if (and (eq gnus-summary-goto-unread 'never)
6614                             (not (gnus-summary-last-article-p article)))
6615                        (gnus-summary-next-article)
6616                      (gnus-summary-next-unread-article))))))))
6617     (gnus-summary-recenter)
6618     (gnus-summary-position-point)))
6619
6620 (defun gnus-summary-prev-page (&optional lines move)
6621   "Show previous page of selected article.
6622 Argument LINES specifies lines to be scrolled down.
6623 If MOVE, move to the previous unread article if point is at
6624 the beginning of the buffer."
6625   (interactive "P")
6626   (let ((article (gnus-summary-article-number))
6627         (article-window (get-buffer-window gnus-article-buffer t))
6628         endp)
6629     (gnus-configure-windows 'article)
6630     (if (or (null gnus-current-article)
6631             (null gnus-article-current)
6632             (/= article (cdr gnus-article-current))
6633             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6634         ;; Selected subject is different from current article's.
6635         (gnus-summary-display-article article)
6636       (gnus-summary-recenter)
6637       (when article-window
6638         (gnus-eval-in-buffer-window gnus-article-buffer
6639           (setq endp (gnus-article-prev-page lines)))
6640         (when (and move endp)
6641           (cond (lines
6642                  (gnus-message 3 "Beginning of message"))
6643                 ((null lines)
6644                  (if (and (eq gnus-summary-goto-unread 'never)
6645                           (not (gnus-summary-first-article-p article)))
6646                      (gnus-summary-prev-article)
6647                    (gnus-summary-prev-unread-article))))))))
6648   (gnus-summary-position-point))
6649
6650 (defun gnus-summary-prev-page-or-article (&optional lines)
6651   "Show previous page of selected article.
6652 Argument LINES specifies lines to be scrolled down.
6653 If at the beginning of the article, go to the next article."
6654   (interactive "P")
6655   (gnus-summary-prev-page lines t))
6656
6657 (defun gnus-summary-scroll-up (lines)
6658   "Scroll up (or down) one line current article.
6659 Argument LINES specifies lines to be scrolled up (or down if negative)."
6660   (interactive "p")
6661   (gnus-configure-windows 'article)
6662   (gnus-summary-show-thread)
6663   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6664     (gnus-eval-in-buffer-window gnus-article-buffer
6665       (cond ((> lines 0)
6666              (when (gnus-article-next-page lines)
6667                (gnus-message 3 "End of message")))
6668             ((< lines 0)
6669              (gnus-article-prev-page (- lines))))))
6670   (gnus-summary-recenter)
6671   (gnus-summary-position-point))
6672
6673 (defun gnus-summary-scroll-down (lines)
6674   "Scroll down (or up) one line current article.
6675 Argument LINES specifies lines to be scrolled down (or up if negative)."
6676   (interactive "p")
6677   (gnus-summary-scroll-up (- lines)))
6678
6679 (defun gnus-summary-next-same-subject ()
6680   "Select next article which has the same subject as current one."
6681   (interactive)
6682   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6683
6684 (defun gnus-summary-prev-same-subject ()
6685   "Select previous article which has the same subject as current one."
6686   (interactive)
6687   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6688
6689 (defun gnus-summary-next-unread-same-subject ()
6690   "Select next unread article which has the same subject as current one."
6691   (interactive)
6692   (gnus-summary-next-article t (gnus-summary-article-subject)))
6693
6694 (defun gnus-summary-prev-unread-same-subject ()
6695   "Select previous unread article which has the same subject as current one."
6696   (interactive)
6697   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6698
6699 (defun gnus-summary-first-unread-article ()
6700   "Select the first unread article.
6701 Return nil if there are no unread articles."
6702   (interactive)
6703   (prog1
6704       (when (gnus-summary-first-subject t)
6705         (gnus-summary-show-thread)
6706         (gnus-summary-first-subject t)
6707         (gnus-summary-display-article (gnus-summary-article-number)))
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-summary-first-unread-subject ()
6711   "Place the point on the subject line of the first unread article.
6712 Return nil if there are no unread articles."
6713   (interactive)
6714   (prog1
6715       (when (gnus-summary-first-subject t)
6716         (gnus-summary-show-thread)
6717         (gnus-summary-first-subject t))
6718     (gnus-summary-position-point)))
6719
6720 (defun gnus-summary-first-article ()
6721   "Select the first article.
6722 Return nil if there are no articles."
6723   (interactive)
6724   (prog1
6725       (when (gnus-summary-first-subject)
6726         (gnus-summary-show-thread)
6727         (gnus-summary-first-subject)
6728         (gnus-summary-display-article (gnus-summary-article-number)))
6729     (gnus-summary-position-point)))
6730
6731 (defun gnus-summary-best-unread-article ()
6732   "Select the unread article with the highest score."
6733   (interactive)
6734   (let ((best -1000000)
6735         (data gnus-newsgroup-data)
6736         article score)
6737     (while data
6738       (and (gnus-data-unread-p (car data))
6739            (> (setq score
6740                     (gnus-summary-article-score (gnus-data-number (car data))))
6741               best)
6742            (setq best score
6743                  article (gnus-data-number (car data))))
6744       (setq data (cdr data)))
6745     (prog1
6746         (if article
6747             (gnus-summary-goto-article article)
6748           (error "No unread articles"))
6749       (gnus-summary-position-point))))
6750
6751 (defun gnus-summary-last-subject ()
6752   "Go to the last displayed subject line in the group."
6753   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6754     (when article
6755       (gnus-summary-goto-subject article))))
6756
6757 (defun gnus-summary-goto-article (article &optional all-headers force)
6758   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6759 If ALL-HEADERS is non-nil, no header lines are hidden.
6760 If FORCE, go to the article even if it isn't displayed.  If FORCE
6761 is a number, it is the line the article is to be displayed on."
6762   (interactive
6763    (list
6764     (completing-read
6765      "Article number or Message-ID: "
6766      (mapcar (lambda (number) (list (int-to-string number)))
6767              gnus-newsgroup-limit))
6768     current-prefix-arg
6769     t))
6770   (prog1
6771       (if (and (stringp article)
6772                (string-match "@" article))
6773           (gnus-summary-refer-article article)
6774         (when (stringp article)
6775           (setq article (string-to-number article)))
6776         (if (gnus-summary-goto-subject article force)
6777             (gnus-summary-display-article article all-headers)
6778           (gnus-message 4 "Couldn't go to article %s" article) nil))
6779     (gnus-summary-position-point)))
6780
6781 (defun gnus-summary-goto-last-article ()
6782   "Go to the previously read article."
6783   (interactive)
6784   (prog1
6785       (when gnus-last-article
6786         (gnus-summary-goto-article gnus-last-article nil t))
6787     (gnus-summary-position-point)))
6788
6789 (defun gnus-summary-pop-article (number)
6790   "Pop one article off the history and go to the previous.
6791 NUMBER articles will be popped off."
6792   (interactive "p")
6793   (let (to)
6794     (setq gnus-newsgroup-history
6795           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6796     (if to
6797         (gnus-summary-goto-article (car to) nil t)
6798       (error "Article history empty")))
6799   (gnus-summary-position-point))
6800
6801 ;; Summary commands and functions for limiting the summary buffer.
6802
6803 (defun gnus-summary-limit-to-articles (n)
6804   "Limit the summary buffer to the next N articles.
6805 If not given a prefix, use the process marked articles instead."
6806   (interactive "P")
6807   (prog1
6808       (let ((articles (gnus-summary-work-articles n)))
6809         (setq gnus-newsgroup-processable nil)
6810         (gnus-summary-limit articles))
6811     (gnus-summary-position-point)))
6812
6813 (defun gnus-summary-pop-limit (&optional total)
6814   "Restore the previous limit.
6815 If given a prefix, remove all limits."
6816   (interactive "P")
6817   (when total
6818     (setq gnus-newsgroup-limits
6819           (list (mapcar (lambda (h) (mail-header-number h))
6820                         gnus-newsgroup-headers))))
6821   (unless gnus-newsgroup-limits
6822     (error "No limit to pop"))
6823   (prog1
6824       (gnus-summary-limit nil 'pop)
6825     (gnus-summary-position-point)))
6826
6827 (defun gnus-summary-limit-to-subject (subject &optional header)
6828   "Limit the summary buffer to articles that have subjects that match a regexp."
6829   (interactive "sLimit to subject (regexp): ")
6830   (unless header
6831     (setq header "subject"))
6832   (when (not (equal "" subject))
6833     (prog1
6834         (let ((articles (gnus-summary-find-matching
6835                          (or header "subject") subject 'all)))
6836           (unless articles
6837             (error "Found no matches for \"%s\"" subject))
6838           (gnus-summary-limit articles))
6839       (gnus-summary-position-point))))
6840
6841 (defun gnus-summary-limit-to-author (from)
6842   "Limit the summary buffer to articles that have authors that match a regexp."
6843   (interactive "sLimit to author (regexp): ")
6844   (gnus-summary-limit-to-subject from "from"))
6845
6846 (defun gnus-summary-limit-to-age (age &optional younger-p)
6847   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6848 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6849 articles that are younger than AGE days."
6850   (interactive
6851    (let ((younger current-prefix-arg)
6852          (days-got nil)
6853          days)
6854      (while (not days-got)
6855        (setq days (if younger
6856                       (read-string "Limit to articles within (in days): ")
6857                     (read-string "Limit to articles older than (in days): ")))
6858        (when (> (length days) 0)
6859          (setq days (read days)))
6860        (if (numberp days)
6861            (setq days-got t)
6862          (message "Please enter a number.")
6863          (sleep-for 1)))
6864      (list days younger)))
6865   (prog1
6866       (let ((data gnus-newsgroup-data)
6867             (cutoff (days-to-time age))
6868             articles d date is-younger)
6869         (while (setq d (pop data))
6870           (when (and (vectorp (gnus-data-header d))
6871                      (setq date (mail-header-date (gnus-data-header d))))
6872             (setq is-younger (time-less-p
6873                               (time-since (condition-case ()
6874                                               (date-to-time date)
6875                                             (error '(0 0))))
6876                               cutoff))
6877             (when (if younger-p
6878                       is-younger
6879                     (not is-younger))
6880               (push (gnus-data-number d) articles))))
6881         (gnus-summary-limit (nreverse articles)))
6882     (gnus-summary-position-point)))
6883
6884 (defun gnus-summary-limit-to-extra (header regexp)
6885   "Limit the summary buffer to articles that match an 'extra' header."
6886   (interactive
6887    (let ((header
6888           (intern
6889            (gnus-completing-read
6890             (symbol-name (car gnus-extra-headers))
6891             "Limit extra header:"
6892             (mapcar (lambda (x)
6893                       (cons (symbol-name x) x))
6894                     gnus-extra-headers)
6895             nil
6896             t))))
6897      (list header
6898            (read-string (format "Limit to header %s (regexp): " header)))))
6899   (when (not (equal "" regexp))
6900     (prog1
6901         (let ((articles (gnus-summary-find-matching
6902                          (cons 'extra header) regexp 'all)))
6903           (unless articles
6904             (error "Found no matches for \"%s\"" regexp))
6905           (gnus-summary-limit articles))
6906       (gnus-summary-position-point))))
6907
6908 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6909 (make-obsolete
6910  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6911
6912 (defun gnus-summary-limit-to-unread (&optional all)
6913   "Limit the summary buffer to articles that are not marked as read.
6914 If ALL is non-nil, limit strictly to unread articles."
6915   (interactive "P")
6916   (if all
6917       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6918     (gnus-summary-limit-to-marks
6919      ;; Concat all the marks that say that an article is read and have
6920      ;; those removed.
6921      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6922            gnus-killed-mark gnus-kill-file-mark
6923            gnus-low-score-mark gnus-expirable-mark
6924            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6925            gnus-duplicate-mark gnus-souped-mark)
6926      'reverse)))
6927
6928 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6929 (make-obsolete 'gnus-summary-delete-marked-with
6930                'gnus-summary-limit-exlude-marks)
6931
6932 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6933   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6934 If REVERSE, limit the summary buffer to articles that are marked
6935 with MARKS.  MARKS can either be a string of marks or a list of marks.
6936 Returns how many articles were removed."
6937   (interactive "sMarks: ")
6938   (gnus-summary-limit-to-marks marks t))
6939
6940 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6941   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6942 If REVERSE (the prefix), limit the summary buffer to articles that are
6943 not marked with MARKS.  MARKS can either be a string of marks or a
6944 list of marks.
6945 Returns how many articles were removed."
6946   (interactive "sMarks: \nP")
6947   (prog1
6948       (let ((data gnus-newsgroup-data)
6949             (marks (if (listp marks) marks
6950                      (append marks nil))) ; Transform to list.
6951             articles)
6952         (while data
6953           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6954                   (memq (gnus-data-mark (car data)) marks))
6955             (push (gnus-data-number (car data)) articles))
6956           (setq data (cdr data)))
6957         (gnus-summary-limit articles))
6958     (gnus-summary-position-point)))
6959
6960 (defun gnus-summary-limit-to-score (&optional score)
6961   "Limit to articles with score at or above SCORE."
6962   (interactive "P")
6963   (setq score (if score
6964                   (prefix-numeric-value score)
6965                 (or gnus-summary-default-score 0)))
6966   (let ((data gnus-newsgroup-data)
6967         articles)
6968     (while data
6969       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6970                 score)
6971         (push (gnus-data-number (car data)) articles))
6972       (setq data (cdr data)))
6973     (prog1
6974         (gnus-summary-limit articles)
6975       (gnus-summary-position-point))))
6976
6977 (defun gnus-summary-limit-include-thread (id)
6978   "Display all the hidden articles that is in the thread with ID in it.
6979 When called interactively, ID is the Message-ID of the current
6980 article."
6981   (interactive (list (mail-header-id (gnus-summary-article-header))))
6982   (let ((articles (gnus-articles-in-thread
6983                    (gnus-id-to-thread (gnus-root-id id)))))
6984     (prog1
6985         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6986       (gnus-summary-limit-include-matching-articles
6987        "subject"
6988        (regexp-quote (gnus-simplify-subject-re
6989                       (mail-header-subject (gnus-id-to-header id)))))
6990       (gnus-summary-position-point))))
6991
6992 (defun gnus-summary-limit-include-matching-articles (header regexp)
6993   "Display all the hidden articles that have HEADERs that match REGEXP."
6994   (interactive (list (read-string "Match on header: ")
6995                      (read-string "Regexp: ")))
6996   (let ((articles (gnus-find-matching-articles header regexp)))
6997     (prog1
6998         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6999       (gnus-summary-position-point))))
7000
7001 (defun gnus-summary-limit-include-dormant ()
7002   "Display all the hidden articles that are marked as dormant.
7003 Note that this command only works on a subset of the articles currently
7004 fetched for this group."
7005   (interactive)
7006   (unless gnus-newsgroup-dormant
7007     (error "There are no dormant articles in this group"))
7008   (prog1
7009       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7010     (gnus-summary-position-point)))
7011
7012 (defun gnus-summary-limit-exclude-dormant ()
7013   "Hide all dormant articles."
7014   (interactive)
7015   (prog1
7016       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7017     (gnus-summary-position-point)))
7018
7019 (defun gnus-summary-limit-exclude-childless-dormant ()
7020   "Hide all dormant articles that have no children."
7021   (interactive)
7022   (let ((data (gnus-data-list t))
7023         articles d children)
7024     ;; Find all articles that are either not dormant or have
7025     ;; children.
7026     (while (setq d (pop data))
7027       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7028                 (and (setq children
7029                            (gnus-article-children (gnus-data-number d)))
7030                      (let (found)
7031                        (while children
7032                          (when (memq (car children) articles)
7033                            (setq children nil
7034                                  found t))
7035                          (pop children))
7036                        found)))
7037         (push (gnus-data-number d) articles)))
7038     ;; Do the limiting.
7039     (prog1
7040         (gnus-summary-limit articles)
7041       (gnus-summary-position-point))))
7042
7043 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7044   "Mark all unread excluded articles as read.
7045 If ALL, mark even excluded ticked and dormants as read."
7046   (interactive "P")
7047   (let ((articles (gnus-sorted-complement
7048                    (sort
7049                     (mapcar (lambda (h) (mail-header-number h))
7050                             gnus-newsgroup-headers)
7051                     '<)
7052                    (sort gnus-newsgroup-limit '<)))
7053         article)
7054     (setq gnus-newsgroup-unreads
7055           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7056     (if all
7057         (setq gnus-newsgroup-dormant nil
7058               gnus-newsgroup-marked nil
7059               gnus-newsgroup-reads
7060               (nconc
7061                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7062                gnus-newsgroup-reads))
7063       (while (setq article (pop articles))
7064         (unless (or (memq article gnus-newsgroup-dormant)
7065                     (memq article gnus-newsgroup-marked))
7066           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7067
7068 (defun gnus-summary-limit (articles &optional pop)
7069   (if pop
7070       ;; We pop the previous limit off the stack and use that.
7071       (setq articles (car gnus-newsgroup-limits)
7072             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7073     ;; We use the new limit, so we push the old limit on the stack.
7074     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7075   ;; Set the limit.
7076   (setq gnus-newsgroup-limit articles)
7077   (let ((total (length gnus-newsgroup-data))
7078         (data (gnus-data-find-list (gnus-summary-article-number)))
7079         (gnus-summary-mark-below nil)   ; Inhibit this.
7080         found)
7081     ;; This will do all the work of generating the new summary buffer
7082     ;; according to the new limit.
7083     (gnus-summary-prepare)
7084     ;; Hide any threads, possibly.
7085     (and gnus-show-threads
7086          gnus-thread-hide-subtree
7087          (gnus-summary-hide-all-threads))
7088     ;; Try to return to the article you were at, or one in the
7089     ;; neighborhood.
7090     (when data
7091       ;; We try to find some article after the current one.
7092       (while data
7093         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7094           (setq data nil
7095                 found t))
7096         (setq data (cdr data))))
7097     (unless found
7098       ;; If there is no data, that means that we were after the last
7099       ;; article.  The same goes when we can't find any articles
7100       ;; after the current one.
7101       (goto-char (point-max))
7102       (gnus-summary-find-prev))
7103     (gnus-set-mode-line 'summary)
7104     ;; We return how many articles were removed from the summary
7105     ;; buffer as a result of the new limit.
7106     (- total (length gnus-newsgroup-data))))
7107
7108 (defsubst gnus-invisible-cut-children (threads)
7109   (let ((num 0))
7110     (while threads
7111       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7112         (incf num))
7113       (pop threads))
7114     (< num 2)))
7115
7116 (defsubst gnus-cut-thread (thread)
7117   "Go forwards in the thread until we find an article that we want to display."
7118   (when (or (eq gnus-fetch-old-headers 'some)
7119             (eq gnus-fetch-old-headers 'invisible)
7120             (numberp gnus-fetch-old-headers)
7121             (eq gnus-build-sparse-threads 'some)
7122             (eq gnus-build-sparse-threads 'more))
7123     ;; Deal with old-fetched headers and sparse threads.
7124     (while (and
7125             thread
7126             (or
7127              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7128              (gnus-summary-article-ancient-p
7129               (mail-header-number (car thread))))
7130             (if (or (<= (length (cdr thread)) 1)
7131                     (eq gnus-fetch-old-headers 'invisible))
7132                 (setq gnus-newsgroup-limit
7133                       (delq (mail-header-number (car thread))
7134                             gnus-newsgroup-limit)
7135                       thread (cadr thread))
7136               (when (gnus-invisible-cut-children (cdr thread))
7137                 (let ((th (cdr thread)))
7138                   (while th
7139                     (if (memq (mail-header-number (caar th))
7140                               gnus-newsgroup-limit)
7141                         (setq thread (car th)
7142                               th nil)
7143                       (setq th (cdr th))))))))))
7144   thread)
7145
7146 (defun gnus-cut-threads (threads)
7147   "Cut off all uninteresting articles from the beginning of threads."
7148   (when (or (eq gnus-fetch-old-headers 'some)
7149             (eq gnus-fetch-old-headers 'invisible)
7150             (numberp gnus-fetch-old-headers)
7151             (eq gnus-build-sparse-threads 'some)
7152             (eq gnus-build-sparse-threads 'more))
7153     (let ((th threads))
7154       (while th
7155         (setcar th (gnus-cut-thread (car th)))
7156         (setq th (cdr th)))))
7157   ;; Remove nixed out threads.
7158   (delq nil threads))
7159
7160 (defun gnus-summary-initial-limit (&optional show-if-empty)
7161   "Figure out what the initial limit is supposed to be on group entry.
7162 This entails weeding out unwanted dormants, low-scored articles,
7163 fetch-old-headers verbiage, and so on."
7164   ;; Most groups have nothing to remove.
7165   (if (or gnus-inhibit-limiting
7166           (and (null gnus-newsgroup-dormant)
7167                (eq gnus-newsgroup-display 'identity)
7168                (not (eq gnus-fetch-old-headers 'some))
7169                (not (numberp gnus-fetch-old-headers))
7170                (not (eq gnus-fetch-old-headers 'invisible))
7171                (null gnus-summary-expunge-below)
7172                (not (eq gnus-build-sparse-threads 'some))
7173                (not (eq gnus-build-sparse-threads 'more))
7174                (null gnus-thread-expunge-below)
7175                (not gnus-use-nocem)))
7176       ()                                ; Do nothing.
7177     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7178     (setq gnus-newsgroup-limit nil)
7179     (mapatoms
7180      (lambda (node)
7181        (unless (car (symbol-value node))
7182          ;; These threads have no parents -- they are roots.
7183          (let ((nodes (cdr (symbol-value node)))
7184                thread)
7185            (while nodes
7186              (if (and gnus-thread-expunge-below
7187                       (< (gnus-thread-total-score (car nodes))
7188                          gnus-thread-expunge-below))
7189                  (gnus-expunge-thread (pop nodes))
7190                (setq thread (pop nodes))
7191                (gnus-summary-limit-children thread))))))
7192      gnus-newsgroup-dependencies)
7193     ;; If this limitation resulted in an empty group, we might
7194     ;; pop the previous limit and use it instead.
7195     (when (and (not gnus-newsgroup-limit)
7196                show-if-empty)
7197       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7198     gnus-newsgroup-limit))
7199
7200 (defun gnus-summary-limit-children (thread)
7201   "Return 1 if this subthread is visible and 0 if it is not."
7202   ;; First we get the number of visible children to this thread.  This
7203   ;; is done by recursing down the thread using this function, so this
7204   ;; will really go down to a leaf article first, before slowly
7205   ;; working its way up towards the root.
7206   (when thread
7207     (let ((children
7208            (if (cdr thread)
7209                (apply '+ (mapcar 'gnus-summary-limit-children
7210                                  (cdr thread)))
7211              0))
7212           (number (mail-header-number (car thread)))
7213           score)
7214       (if (and
7215            (not (memq number gnus-newsgroup-marked))
7216            (or
7217             ;; If this article is dormant and has absolutely no visible
7218             ;; children, then this article isn't visible.
7219             (and (memq number gnus-newsgroup-dormant)
7220                  (zerop children))
7221             ;; If this is "fetch-old-headered" and there is no
7222             ;; visible children, then we don't want this article.
7223             (and (or (eq gnus-fetch-old-headers 'some)
7224                      (numberp gnus-fetch-old-headers))
7225                  (gnus-summary-article-ancient-p number)
7226                  (zerop children))
7227             ;; If this is "fetch-old-headered" and `invisible', then
7228             ;; we don't want this article.
7229             (and (eq gnus-fetch-old-headers 'invisible)
7230                  (gnus-summary-article-ancient-p number))
7231             ;; If this is a sparsely inserted article with no children,
7232             ;; we don't want it.
7233             (and (eq gnus-build-sparse-threads 'some)
7234                  (gnus-summary-article-sparse-p number)
7235                  (zerop children))
7236             ;; If we use expunging, and this article is really
7237             ;; low-scored, then we don't want this article.
7238             (when (and gnus-summary-expunge-below
7239                        (< (setq score
7240                                 (or (cdr (assq number gnus-newsgroup-scored))
7241                                     gnus-summary-default-score))
7242                           gnus-summary-expunge-below))
7243               ;; We increase the expunge-tally here, but that has
7244               ;; nothing to do with the limits, really.
7245               (incf gnus-newsgroup-expunged-tally)
7246               ;; We also mark as read here, if that's wanted.
7247               (when (and gnus-summary-mark-below
7248                          (< score gnus-summary-mark-below))
7249                 (setq gnus-newsgroup-unreads
7250                       (delq number gnus-newsgroup-unreads))
7251                 (if gnus-newsgroup-auto-expire
7252                     (push number gnus-newsgroup-expirable)
7253                   (push (cons number gnus-low-score-mark)
7254                         gnus-newsgroup-reads)))
7255               t)
7256             ;; Do the `display' group parameter.
7257             (and gnus-newsgroup-display
7258                  (not (funcall gnus-newsgroup-display)))
7259             ;; Check NoCeM things.
7260             (if (and gnus-use-nocem
7261                      (gnus-nocem-unwanted-article-p
7262                       (mail-header-id (car thread))))
7263                 (progn
7264                   (setq gnus-newsgroup-unreads
7265                         (delq number gnus-newsgroup-unreads))
7266                   t))))
7267           ;; Nope, invisible article.
7268           0
7269         ;; Ok, this article is to be visible, so we add it to the limit
7270         ;; and return 1.
7271         (push number gnus-newsgroup-limit)
7272         1))))
7273
7274 (defun gnus-expunge-thread (thread)
7275   "Mark all articles in THREAD as read."
7276   (let* ((number (mail-header-number (car thread))))
7277     (incf gnus-newsgroup-expunged-tally)
7278     ;; We also mark as read here, if that's wanted.
7279     (setq gnus-newsgroup-unreads
7280           (delq number gnus-newsgroup-unreads))
7281     (if gnus-newsgroup-auto-expire
7282         (push number gnus-newsgroup-expirable)
7283       (push (cons number gnus-low-score-mark)
7284             gnus-newsgroup-reads)))
7285   ;; Go recursively through all subthreads.
7286   (mapcar 'gnus-expunge-thread (cdr thread)))
7287
7288 ;; Summary article oriented commands
7289
7290 (defun gnus-summary-refer-parent-article (n)
7291   "Refer parent article N times.
7292 If N is negative, go to ancestor -N instead.
7293 The difference between N and the number of articles fetched is returned."
7294   (interactive "p")
7295   (let ((skip 1)
7296         error header ref)
7297     (when (not (natnump n))
7298       (setq skip (abs n)
7299             n 1))
7300     (while (and (> n 0)
7301                 (not error))
7302       (setq header (gnus-summary-article-header))
7303       (if (and (eq (mail-header-number header)
7304                    (cdr gnus-article-current))
7305                (equal gnus-newsgroup-name
7306                       (car gnus-article-current)))
7307           ;; If we try to find the parent of the currently
7308           ;; displayed article, then we take a look at the actual
7309           ;; References header, since this is slightly more
7310           ;; reliable than the References field we got from the
7311           ;; server.
7312           (save-excursion
7313             (set-buffer gnus-original-article-buffer)
7314             (nnheader-narrow-to-headers)
7315             (unless (setq ref (message-fetch-field "references"))
7316               (setq ref (message-fetch-field "in-reply-to")))
7317             (widen))
7318         (setq ref
7319               ;; It's not the current article, so we take a bet on
7320               ;; the value we got from the server.
7321               (mail-header-references header)))
7322       (if (and ref
7323                (not (equal ref "")))
7324           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7325             (gnus-message 1 "Couldn't find parent"))
7326         (gnus-message 1 "No references in article %d"
7327                       (gnus-summary-article-number))
7328         (setq error t))
7329       (decf n))
7330     (gnus-summary-position-point)
7331     n))
7332
7333 (defun gnus-summary-refer-references ()
7334   "Fetch all articles mentioned in the References header.
7335 Return the number of articles fetched."
7336   (interactive)
7337   (let ((ref (mail-header-references (gnus-summary-article-header)))
7338         (current (gnus-summary-article-number))
7339         (n 0))
7340     (if (or (not ref)
7341             (equal ref ""))
7342         (error "No References in the current article")
7343       ;; For each Message-ID in the References header...
7344       (while (string-match "<[^>]*>" ref)
7345         (incf n)
7346         ;; ... fetch that article.
7347         (gnus-summary-refer-article
7348          (prog1 (match-string 0 ref)
7349            (setq ref (substring ref (match-end 0))))))
7350       (gnus-summary-goto-subject current)
7351       (gnus-summary-position-point)
7352       n)))
7353
7354 (defun gnus-summary-refer-thread (&optional limit)
7355   "Fetch all articles in the current thread.
7356 If LIMIT (the numerical prefix), fetch that many old headers instead
7357 of what's specified by the `gnus-refer-thread-limit' variable."
7358   (interactive "P")
7359   (let ((id (mail-header-id (gnus-summary-article-header)))
7360         (limit (if limit (prefix-numeric-value limit)
7361                  gnus-refer-thread-limit)))
7362     ;; We want to fetch LIMIT *old* headers, but we also have to
7363     ;; re-fetch all the headers in the current buffer, because many of
7364     ;; them may be undisplayed.  So we adjust LIMIT.
7365     (when (numberp limit)
7366       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7367     (unless (eq gnus-fetch-old-headers 'invisible)
7368       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7369       ;; Retrieve the headers and read them in.
7370       (if (eq (gnus-retrieve-headers
7371                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7372               'nov)
7373           (gnus-build-all-threads)
7374         (error "Can't fetch thread from backends that don't support NOV"))
7375       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7376     (gnus-summary-limit-include-thread id)))
7377
7378 (defun gnus-summary-refer-article (message-id)
7379   "Fetch an article specified by MESSAGE-ID."
7380   (interactive "sMessage-ID: ")
7381   (when (and (stringp message-id)
7382              (not (zerop (length message-id))))
7383     ;; Construct the correct Message-ID if necessary.
7384     ;; Suggested by tale@pawl.rpi.edu.
7385     (unless (string-match "^<" message-id)
7386       (setq message-id (concat "<" message-id)))
7387     (unless (string-match ">$" message-id)
7388       (setq message-id (concat message-id ">")))
7389     (let* ((header (gnus-id-to-header message-id))
7390            (sparse (and header
7391                         (gnus-summary-article-sparse-p
7392                          (mail-header-number header))
7393                         (memq (mail-header-number header)
7394                               gnus-newsgroup-limit)))
7395            number)
7396       (cond
7397        ;; If the article is present in the buffer we just go to it.
7398        ((and header
7399              (or (not (gnus-summary-article-sparse-p
7400                        (mail-header-number header)))
7401                  sparse))
7402         (prog1
7403             (gnus-summary-goto-article
7404              (mail-header-number header) nil t)
7405           (when sparse
7406             (gnus-summary-update-article (mail-header-number header)))))
7407        (t
7408         ;; We fetch the article.
7409         (catch 'found
7410           (dolist (gnus-override-method (gnus-refer-article-methods))
7411             (gnus-check-server gnus-override-method)
7412             ;; Fetch the header, and display the article.
7413             (when (setq number (gnus-summary-insert-subject message-id))
7414               (gnus-summary-select-article nil nil nil number)
7415               (throw 'found t)))
7416           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7417
7418 (defun gnus-refer-article-methods ()
7419   "Return a list of referrable methods."
7420   (cond
7421    ;; No method, so we default to current and native.
7422    ((null gnus-refer-article-method)
7423     (list gnus-current-select-method gnus-select-method))
7424    ;; Current.
7425    ((eq 'current gnus-refer-article-method)
7426     (list gnus-current-select-method))
7427    ;; List of select methods.
7428    ((not (and (symbolp (car gnus-refer-article-method))
7429               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7430     (let (out)
7431       (dolist (method gnus-refer-article-method)
7432         (push (if (eq 'current method)
7433                   gnus-current-select-method
7434                 method)
7435               out))
7436       (nreverse out)))
7437    ;; One single select method.
7438    (t
7439     (list gnus-refer-article-method))))
7440
7441 (defun gnus-summary-edit-parameters ()
7442   "Edit the group parameters of the current group."
7443   (interactive)
7444   (gnus-group-edit-group gnus-newsgroup-name 'params))
7445
7446 (defun gnus-summary-customize-parameters ()
7447   "Customize the group parameters of the current group."
7448   (interactive)
7449   (gnus-group-customize gnus-newsgroup-name))
7450
7451 (defun gnus-summary-enter-digest-group (&optional force)
7452   "Enter an nndoc group based on the current article.
7453 If FORCE, force a digest interpretation.  If not, try
7454 to guess what the document format is."
7455   (interactive "P")
7456   (let ((conf gnus-current-window-configuration))
7457     (save-excursion
7458       (gnus-summary-select-article))
7459     (setq gnus-current-window-configuration conf)
7460     (let* ((name (format "%s-%d"
7461                          (gnus-group-prefixed-name
7462                           gnus-newsgroup-name (list 'nndoc ""))
7463                          (save-excursion
7464                            (set-buffer gnus-summary-buffer)
7465                            gnus-current-article)))
7466            (ogroup gnus-newsgroup-name)
7467            (params (append (gnus-info-params (gnus-get-info ogroup))
7468                            (list (cons 'to-group ogroup))
7469                            (list (cons 'save-article-group ogroup))))
7470            (case-fold-search t)
7471            (buf (current-buffer))
7472            dig to-address)
7473       (save-excursion
7474         (set-buffer gnus-original-article-buffer)
7475         ;; Have the digest group inherit the main mail address of
7476         ;; the parent article.
7477         (when (setq to-address (or (message-fetch-field "reply-to")
7478                                    (message-fetch-field "from")))
7479           (setq params (append
7480                         (list (cons 'to-address
7481                                     (funcall gnus-decode-encoded-word-function
7482                                              to-address))))))
7483         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7484         (insert-buffer-substring gnus-original-article-buffer)
7485         ;; Remove lines that may lead nndoc to misinterpret the
7486         ;; document type.
7487         (narrow-to-region
7488          (goto-char (point-min))
7489          (or (search-forward "\n\n" nil t) (point)))
7490         (goto-char (point-min))
7491         (delete-matching-lines "^Path:\\|^From ")
7492         (widen))
7493       (unwind-protect
7494           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7495                     (gnus-newsgroup-ephemeral-ignored-charsets
7496                      gnus-newsgroup-ignored-charsets))
7497                 (gnus-group-read-ephemeral-group
7498                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7499                               (nndoc-article-type
7500                                ,(if force 'mbox 'guess))) t))
7501               ;; Make all postings to this group go to the parent group.
7502               (nconc (gnus-info-params (gnus-get-info name))
7503                      params)
7504             ;; Couldn't select this doc group.
7505             (switch-to-buffer buf)
7506             (gnus-set-global-variables)
7507             (gnus-configure-windows 'summary)
7508             (gnus-message 3 "Article couldn't be entered?"))
7509         (kill-buffer dig)))))
7510
7511 (defun gnus-summary-read-document (n)
7512   "Open a new group based on the current article(s).
7513 This will allow you to read digests and other similar
7514 documents as newsgroups.
7515 Obeys the standard process/prefix convention."
7516   (interactive "P")
7517   (let* ((articles (gnus-summary-work-articles n))
7518          (ogroup gnus-newsgroup-name)
7519          (params (append (gnus-info-params (gnus-get-info ogroup))
7520                          (list (cons 'to-group ogroup))))
7521          article group egroup groups vgroup)
7522     (while (setq article (pop articles))
7523       (setq group (format "%s-%d" gnus-newsgroup-name article))
7524       (gnus-summary-remove-process-mark article)
7525       (when (gnus-summary-display-article article)
7526         (save-excursion
7527           (with-temp-buffer
7528             (insert-buffer-substring gnus-original-article-buffer)
7529             ;; Remove some headers that may lead nndoc to make
7530             ;; the wrong guess.
7531             (message-narrow-to-head)
7532             (goto-char (point-min))
7533             (delete-matching-lines "^\\(Path\\):\\|^From ")
7534             (widen)
7535             (if (setq egroup
7536                       (gnus-group-read-ephemeral-group
7537                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7538                                      (nndoc-article-type guess))
7539                        t nil t))
7540                 (progn
7541                   ;; Make all postings to this group go to the parent group.
7542                   (nconc (gnus-info-params (gnus-get-info egroup))
7543                          params)
7544                   (push egroup groups))
7545               ;; Couldn't select this doc group.
7546               (gnus-error 3 "Article couldn't be entered"))))))
7547     ;; Now we have selected all the documents.
7548     (cond
7549      ((not groups)
7550       (error "None of the articles could be interpreted as documents"))
7551      ((gnus-group-read-ephemeral-group
7552        (setq vgroup (format
7553                      "nnvirtual:%s-%s" gnus-newsgroup-name
7554                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7555        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7556        t
7557        (cons (current-buffer) 'summary)))
7558      (t
7559       (error "Couldn't select virtual nndoc group")))))
7560
7561 (defun gnus-summary-isearch-article (&optional regexp-p)
7562   "Do incremental search forward on the current article.
7563 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7564   (interactive "P")
7565   (let* ((gnus-inhibit-treatment t)
7566          (old (gnus-summary-select-article)))
7567     (gnus-configure-windows 'article)
7568     (gnus-eval-in-buffer-window gnus-article-buffer
7569       (save-restriction
7570         (widen)
7571         (when (eq 'old old)
7572           (gnus-article-show-all-headers))
7573         (goto-char (point-min))
7574         (isearch-forward regexp-p)))))
7575
7576 (defun gnus-summary-search-article-forward (regexp &optional backward)
7577   "Search for an article containing REGEXP forward.
7578 If BACKWARD, search backward instead."
7579   (interactive
7580    (list (read-string
7581           (format "Search article %s (regexp%s): "
7582                   (if current-prefix-arg "backward" "forward")
7583                   (if gnus-last-search-regexp
7584                       (concat ", default " gnus-last-search-regexp)
7585                     "")))
7586          current-prefix-arg))
7587   (if (string-equal regexp "")
7588       (setq regexp (or gnus-last-search-regexp ""))
7589     (setq gnus-last-search-regexp regexp)
7590     (setq gnus-article-before-search gnus-current-article))
7591   ;; Intentionally set gnus-last-article.
7592   (setq gnus-last-article gnus-article-before-search)
7593   (let ((gnus-last-article gnus-last-article))
7594     (if (gnus-summary-search-article regexp backward)
7595         (gnus-summary-show-thread)
7596       (error "Search failed: \"%s\"" regexp))))
7597
7598 (defun gnus-summary-search-article-backward (regexp)
7599   "Search for an article containing REGEXP backward."
7600   (interactive
7601    (list (read-string
7602           (format "Search article backward (regexp%s): "
7603                   (if gnus-last-search-regexp
7604                       (concat ", default " gnus-last-search-regexp)
7605                     "")))))
7606   (gnus-summary-search-article-forward regexp 'backward))
7607
7608 (eval-when-compile
7609   (defmacro gnus-summary-search-article-position-point (regexp backward)
7610     "Dehighlight the last matched text and goto the beginning position."
7611     (` (if (and gnus-summary-search-article-matched-data
7612                 (let ((text (caddr gnus-summary-search-article-matched-data))
7613                       (inhibit-read-only t)
7614                       buffer-read-only)
7615                   (delete-region
7616                    (goto-char (car gnus-summary-search-article-matched-data))
7617                    (cadr gnus-summary-search-article-matched-data))
7618                   (insert text)
7619                   (string-match (, regexp) text)))
7620            (if (, backward) (beginning-of-line) (end-of-line))
7621          (goto-char (if (, backward) (point-max) (point-min))))))
7622
7623   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7624     "Place point where X-Face image is displayed."
7625     (if (featurep 'xemacs)
7626         (` (let ((end (if (search-forward "\n\n" nil t)
7627                           (goto-char (1- (point)))
7628                         (point-min)))
7629                  extent)
7630              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7631              (unless (and (re-search-forward "^From:" end t)
7632                           (setq extent (extent-at (point)))
7633                           (extent-begin-glyph extent))
7634                (goto-char (, opoint)))))
7635       (` (let ((end (if (search-forward "\n\n" nil t)
7636                         (goto-char (1- (point)))
7637                       (point-min)))
7638                (start (or (search-backward "\n\n" nil t) (point-min))))
7639            (goto-char
7640             (or (text-property-any start end 'x-face-image t);; x-face-e21
7641                 (text-property-any start end 'x-face-mule-bitmap-image t)
7642                 (, opoint)))))))
7643
7644   (defmacro gnus-summary-search-article-highlight-matched-text
7645     (backward treated x-face)
7646     "Highlight matched text in the function `gnus-summary-search-article'."
7647     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7648              (end (set-marker (make-marker) (match-end 0)))
7649              (inhibit-read-only t)
7650              buffer-read-only)
7651          (unless treated
7652            (let ((,@
7653                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7654                     (mapcar
7655                      (lambda (item) (setq items (delq item items)))
7656                      '(gnus-treat-buttonize
7657                        gnus-treat-fill-article
7658                        gnus-treat-fill-long-lines
7659                        gnus-treat-emphasize
7660                        gnus-treat-highlight-headers
7661                        gnus-treat-highlight-citation
7662                        gnus-treat-highlight-signature
7663                        gnus-treat-overstrike
7664                        gnus-treat-display-xface
7665                        gnus-treat-buttonize-head
7666                        gnus-treat-decode-article-as-default-mime-charset))
7667                     (static-if (featurep 'xemacs)
7668                         items
7669                       (cons '(x-face-mule-delete-x-face-field
7670                               (quote never))
7671                             items))))
7672                  (gnus-treat-display-xface
7673                   (when (, x-face) gnus-treat-display-xface)))
7674              (gnus-article-prepare-mime-display)))
7675          (goto-char (if (, backward) start end))
7676          (when (, x-face)
7677            (gnus-summary-search-article-highlight-goto-x-face (point)))
7678          (setq gnus-summary-search-article-matched-data
7679                (list start end (buffer-substring start end)))
7680          (unless (eq start end);; matched text has been deleted. :-<
7681            (put-text-property start end 'face
7682                               (or (find-face 'isearch)
7683                                   'secondary-selection))))))
7684   )
7685
7686 (defun gnus-summary-search-article (regexp &optional backward)
7687   "Search for an article containing REGEXP.
7688 Optional argument BACKWARD means do search for backward.
7689 `gnus-select-article-hook' is not called during the search."
7690   ;; We have to require this here to make sure that the following
7691   ;; dynamic binding isn't shadowed by autoloading.
7692   (require 'gnus-async)
7693   (require 'gnus-art)
7694   (let ((gnus-select-article-hook nil)  ;Disable hook.
7695         (gnus-article-prepare-hook nil)
7696         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7697         (gnus-use-article-prefetch nil)
7698         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7699         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7700         (sum (current-buffer))
7701         (found nil)
7702         point treated)
7703     (gnus-save-hidden-threads
7704       (static-if (featurep 'xemacs)
7705           (let ((gnus-inhibit-treatment t))
7706             (setq treated (eq 'old (gnus-summary-select-article)))
7707             (when (and treated
7708                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7709                                  (window-live-p (get-buffer-window
7710                                                  gnus-article-buffer t)))))
7711               (gnus-summary-select-article nil t)
7712               (setq treated nil)))
7713         (let ((gnus-inhibit-treatment t)
7714               (x-face-mule-delete-x-face-field 'never))
7715           (setq treated (eq 'old (gnus-summary-select-article)))
7716           (when (and treated
7717                      (not
7718                       (and (gnus-buffer-live-p gnus-article-buffer)
7719                            (window-live-p (get-buffer-window
7720                                            gnus-article-buffer t))
7721                            (or (not (string-match "^\\^X-Face:" regexp))
7722                                (with-current-buffer gnus-article-buffer
7723                                  gnus-summary-search-article-matched-data)))))
7724             (gnus-summary-select-article nil t)
7725             (setq treated nil))))
7726       (set-buffer gnus-article-buffer)
7727       (widen)
7728       (if treated
7729           (progn
7730             (gnus-article-show-all-headers)
7731             (gnus-summary-search-article-position-point regexp backward))
7732         (goto-char (if backward (point-max) (point-min))))
7733       (while (not found)
7734         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7735         (if (if backward
7736                 (re-search-backward regexp nil t)
7737               (re-search-forward regexp nil t))
7738             ;; We found the regexp.
7739             (progn
7740               (gnus-summary-search-article-highlight-matched-text
7741                backward treated (string-match "^\\^X-Face:" regexp))
7742               (setq found 'found)
7743               (forward-line
7744                (/ (- 2 (window-height
7745                         (get-buffer-window gnus-article-buffer t)))
7746                   2))
7747               (set-window-start
7748                (get-buffer-window (current-buffer))
7749                (point))
7750               (set-buffer sum)
7751               (setq point (point)))
7752           ;; We didn't find it, so we go to the next article.
7753           (set-buffer sum)
7754           (setq found 'not)
7755           (while (eq found 'not)
7756             (if (not (if backward (gnus-summary-find-prev)
7757                        (gnus-summary-find-next)))
7758                 ;; No more articles.
7759                 (setq found t)
7760               ;; Select the next article and adjust point.
7761               (unless (gnus-summary-article-sparse-p
7762                        (gnus-summary-article-number))
7763                 (setq found nil)
7764                 (let ((gnus-inhibit-treatment t))
7765                   (gnus-summary-select-article))
7766                 (setq treated nil)
7767                 (set-buffer gnus-article-buffer)
7768                 (widen)
7769                 (goto-char (if backward (point-max) (point-min))))))))
7770       (gnus-message 7 ""))
7771     ;; Return whether we found the regexp.
7772     (when (eq found 'found)
7773       (goto-char point)
7774       (gnus-summary-show-thread)
7775       (gnus-summary-goto-subject gnus-current-article)
7776       (gnus-summary-position-point)
7777       t)))
7778
7779 (defun gnus-find-matching-articles (header regexp)
7780   "Return a list of all articles that match REGEXP on HEADER.
7781 This search includes all articles in the current group that Gnus has
7782 fetched headers for, whether they are displayed or not."
7783   (let ((articles nil)
7784         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7785         (case-fold-search t))
7786     (dolist (header gnus-newsgroup-headers)
7787       (when (string-match regexp (funcall func header))
7788         (push (mail-header-number header) articles)))
7789     (nreverse articles)))
7790
7791 (defun gnus-summary-find-matching (header regexp &optional backward unread
7792                                           not-case-fold)
7793   "Return a list of all articles that match REGEXP on HEADER.
7794 The search stars on the current article and goes forwards unless
7795 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7796 If UNREAD is non-nil, only unread articles will
7797 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7798 in the comparisons."
7799   (let ((case-fold-search (not not-case-fold))
7800         articles d func)
7801     (if (consp header)
7802         (if (eq (car header) 'extra)
7803             (setq func
7804                   `(lambda (h)
7805                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7806                          "")))
7807           (error "%s is an invalid header" header))
7808       (unless (fboundp (intern (concat "mail-header-" header)))
7809         (error "%s is not a valid header" header))
7810       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7811     (dolist (d (if (eq backward 'all)
7812                    gnus-newsgroup-data
7813                  (gnus-data-find-list
7814                   (gnus-summary-article-number)
7815                   (gnus-data-list backward))))
7816       (when (and (or (not unread)       ; We want all articles...
7817                      (gnus-data-unread-p d)) ; Or just unreads.
7818                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7819                  (string-match regexp
7820                                (funcall func (gnus-data-header d)))) ; Match.
7821         (push (gnus-data-number d) articles))) ; Success!
7822     (nreverse articles)))
7823
7824 (defun gnus-summary-execute-command (header regexp command &optional backward)
7825   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7826 If HEADER is an empty string (or nil), the match is done on the entire
7827 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7828   (interactive
7829    (list (let ((completion-ignore-case t))
7830            (completing-read
7831             "Header name: "
7832             (mapcar (lambda (header) (list (format "%s" header)))
7833                     (append
7834                      '("Number" "Subject" "From" "Lines" "Date"
7835                        "Message-ID" "Xref" "References" "Body")
7836                      gnus-extra-headers))
7837             nil 'require-match))
7838          (read-string "Regexp: ")
7839          (read-key-sequence "Command: ")
7840          current-prefix-arg))
7841   (when (equal header "Body")
7842     (setq header ""))
7843   ;; Hidden thread subtrees must be searched as well.
7844   (gnus-summary-show-all-threads)
7845   ;; We don't want to change current point nor window configuration.
7846   (save-excursion
7847     (save-window-excursion
7848       (gnus-message 6 "Executing %s..." (key-description command))
7849       ;; We'd like to execute COMMAND interactively so as to give arguments.
7850       (gnus-execute header regexp
7851                     `(call-interactively ',(key-binding command))
7852                     backward)
7853       (gnus-message 6 "Executing %s...done" (key-description command)))))
7854
7855 (defun gnus-summary-beginning-of-article ()
7856   "Scroll the article back to the beginning."
7857   (interactive)
7858   (gnus-summary-select-article)
7859   (gnus-configure-windows 'article)
7860   (gnus-eval-in-buffer-window gnus-article-buffer
7861     (widen)
7862     (goto-char (point-min))
7863     (when gnus-page-broken
7864       (gnus-narrow-to-page))))
7865
7866 (defun gnus-summary-end-of-article ()
7867   "Scroll to the end of the article."
7868   (interactive)
7869   (gnus-summary-select-article)
7870   (gnus-configure-windows 'article)
7871   (gnus-eval-in-buffer-window gnus-article-buffer
7872     (widen)
7873     (goto-char (point-max))
7874     (recenter -3)
7875     (when gnus-page-broken
7876       (gnus-narrow-to-page))))
7877
7878 (defun gnus-summary-print-article (&optional filename n)
7879   "Generate and print a PostScript image of the N next (mail) articles.
7880
7881 If N is negative, print the N previous articles.  If N is nil and articles
7882 have been marked with the process mark, print these instead.
7883
7884 If the optional first argument FILENAME is nil, send the image to the
7885 printer.  If FILENAME is a string, save the PostScript image in a file with
7886 that name.  If FILENAME is a number, prompt the user for the name of the file
7887 to save in."
7888   (interactive (list (ps-print-preprint current-prefix-arg)))
7889   (dolist (article (gnus-summary-work-articles n))
7890     (gnus-summary-select-article nil nil 'pseudo article)
7891     (gnus-eval-in-buffer-window gnus-article-buffer
7892       (let ((buffer (generate-new-buffer " *print*")))
7893         (unwind-protect
7894             (progn
7895               (copy-to-buffer buffer (point-min) (point-max))
7896               (set-buffer buffer)
7897               (gnus-article-delete-invisible-text)
7898               (when (gnus-visual-p 'article-highlight 'highlight)
7899                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7900                 ;; highlight.
7901                 (let ((gnus-article-buffer buffer))
7902                   (gnus-article-highlight-citation t)
7903                   (gnus-article-highlight-signature)))
7904               (let ((ps-left-header
7905                      (list
7906                       (concat "("
7907                               (mail-header-subject gnus-current-headers) ")")
7908                       (concat "("
7909                               (mail-header-from gnus-current-headers) ")")))
7910                     (ps-right-header
7911                      (list
7912                       "/pagenumberstring load"
7913                       (concat "("
7914                               (mail-header-date gnus-current-headers) ")"))))
7915                 (gnus-run-hooks 'gnus-ps-print-hook)
7916                 (save-excursion
7917                   (if window-system
7918                       (ps-spool-buffer-with-faces)
7919                     (ps-spool-buffer)))))
7920           (kill-buffer buffer))))
7921     (gnus-summary-remove-process-mark article))
7922   (ps-despool filename))
7923
7924 (defun gnus-summary-show-article (&optional arg)
7925   "Force re-fetching of the current article.
7926 If ARG (the prefix) is a number, show the article with the charset
7927 defined in `gnus-summary-show-article-charset-alist', or the charset
7928 input.
7929 If ARG (the prefix) is non-nil and not a number, show the raw article
7930 without any article massaging functions being run."
7931   (interactive "P")
7932   (cond
7933    ((numberp arg)
7934     (gnus-summary-show-article t)
7935     (let ((gnus-newsgroup-charset
7936            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7937                (mm-read-coding-system
7938                 "View as charset: "
7939                 (save-excursion
7940                   (set-buffer gnus-article-buffer)
7941                   (detect-coding-region (point) (point-max) t)))))
7942           (gnus-newsgroup-ignored-charsets 'gnus-all))
7943       (gnus-summary-select-article nil 'force)
7944       (let ((deps gnus-newsgroup-dependencies)
7945             head header)
7946         (save-excursion
7947           (set-buffer gnus-original-article-buffer)
7948           (save-restriction
7949             (message-narrow-to-head)
7950             (setq head (buffer-string)))
7951           (with-temp-buffer
7952             (insert (format "211 %d Article retrieved.\n"
7953                             (cdr gnus-article-current)))
7954             (insert head)
7955             (insert ".\n")
7956             (let ((nntp-server-buffer (current-buffer)))
7957               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7958         (gnus-data-set-header
7959          (gnus-data-find (cdr gnus-article-current))
7960          header)
7961         (gnus-summary-update-article-line
7962          (cdr gnus-article-current) header)
7963         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7964           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7965    ((not arg)
7966     ;; Select the article the normal way.
7967     (gnus-summary-select-article nil 'force))
7968    (t
7969     ;; We have to require this here to make sure that the following
7970     ;; dynamic binding isn't shadowed by autoloading.
7971     (require 'gnus-async)
7972     (require 'gnus-art)
7973     ;; Bind the article treatment functions to nil.
7974     (let ((gnus-have-all-headers t)
7975           gnus-article-prepare-hook
7976           gnus-article-decode-hook
7977           gnus-break-pages
7978           gnus-show-mime
7979           (gnus-inhibit-treatment t))
7980       (gnus-summary-select-article nil 'force))))
7981   (gnus-summary-goto-subject gnus-current-article)
7982   (gnus-summary-position-point))
7983
7984 (defun gnus-summary-show-raw-article ()
7985   "Show the raw article without any article massaging functions being run."
7986   (interactive)
7987   (gnus-summary-show-article t))
7988
7989 (defun gnus-summary-verbose-headers (&optional arg)
7990   "Toggle permanent full header display.
7991 If ARG is a positive number, turn header display on.
7992 If ARG is a negative number, turn header display off."
7993   (interactive "P")
7994   (setq gnus-show-all-headers
7995         (cond ((or (not (numberp arg))
7996                    (zerop arg))
7997                (not gnus-show-all-headers))
7998               ((natnump arg)
7999                t)))
8000   (gnus-summary-show-article))
8001
8002 (defun gnus-summary-toggle-header (&optional arg)
8003   "Show the headers if they are hidden, or hide them if they are shown.
8004 If ARG is a positive number, show the entire header.
8005 If ARG is a negative number, hide the unwanted header lines."
8006   (interactive "P")
8007   (save-excursion
8008     (set-buffer gnus-article-buffer)
8009     (save-restriction
8010       (let* ((buffer-read-only nil)
8011              (inhibit-point-motion-hooks t)
8012              hidden e)
8013         (setq hidden
8014               (if (numberp arg)
8015                   (>= arg 0)
8016                 (save-restriction
8017                   (article-narrow-to-head)
8018                   (gnus-article-hidden-text-p 'headers))))
8019         (goto-char (point-min))
8020         (when (search-forward "\n\n" nil t)
8021           (delete-region (point-min) (1- (point))))
8022         (goto-char (point-min))
8023         (save-excursion
8024           (set-buffer gnus-original-article-buffer)
8025           (goto-char (point-min))
8026           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8027         (insert-buffer-substring gnus-original-article-buffer 1 e)
8028         (save-restriction
8029           (narrow-to-region (point-min) (point))
8030           (article-decode-encoded-words)
8031           (if  hidden
8032               (let ((gnus-treat-hide-headers nil)
8033                     (gnus-treat-hide-boring-headers nil))
8034                 (setq gnus-article-wash-types
8035                       (delq 'headers gnus-article-wash-types))
8036                 (gnus-treat-article 'head))
8037             (gnus-treat-article 'head)))
8038         (gnus-set-mode-line 'article)))))
8039
8040 (defun gnus-summary-show-all-headers ()
8041   "Make all header lines visible."
8042   (interactive)
8043   (gnus-summary-toggle-header 1))
8044
8045 (defun gnus-summary-toggle-mime (&optional arg)
8046   "Toggle MIME processing.
8047 If ARG is a positive number, turn MIME processing on."
8048   (interactive "P")
8049   (setq gnus-show-mime
8050         (if (null arg)
8051             (not gnus-show-mime)
8052           (> (prefix-numeric-value arg) 0)))
8053   (gnus-summary-select-article t 'force))
8054
8055 (defun gnus-summary-caesar-message (&optional arg)
8056   "Caesar rotate the current article by 13.
8057 The numerical prefix specifies how many places to rotate each letter
8058 forward."
8059   (interactive "P")
8060   (gnus-summary-select-article)
8061   (let ((mail-header-separator ""))
8062     (gnus-eval-in-buffer-window gnus-article-buffer
8063       (save-restriction
8064         (widen)
8065         (let ((start (window-start))
8066               buffer-read-only)
8067           (message-caesar-buffer-body arg)
8068           (set-window-start (get-buffer-window (current-buffer)) start))))))
8069
8070 (defun gnus-summary-stop-page-breaking ()
8071   "Stop page breaking in the current article."
8072   (interactive)
8073   (gnus-summary-select-article)
8074   (gnus-eval-in-buffer-window gnus-article-buffer
8075     (widen)
8076     (when (gnus-visual-p 'page-marker)
8077       (let ((buffer-read-only nil))
8078         (gnus-remove-text-with-property 'gnus-prev)
8079         (gnus-remove-text-with-property 'gnus-next))
8080       (setq gnus-page-broken nil))))
8081
8082 (defun gnus-summary-move-article (&optional n to-newsgroup
8083                                             select-method action)
8084   "Move the current article to a different newsgroup.
8085 If N is a positive number, move the N next articles.
8086 If N is a negative number, move the N previous articles.
8087 If N is nil and any articles have been marked with the process mark,
8088 move those articles instead.
8089 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8090 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8091 re-spool using this method.
8092
8093 For this function to work, both the current newsgroup and the
8094 newsgroup that you want to move to have to support the `request-move'
8095 and `request-accept' functions.
8096
8097 ACTION can be either `move' (the default), `crosspost' or `copy'."
8098   (interactive "P")
8099   (unless action
8100     (setq action 'move))
8101   ;; Check whether the source group supports the required functions.
8102   (cond ((and (eq action 'move)
8103               (not (gnus-check-backend-function
8104                     'request-move-article gnus-newsgroup-name)))
8105          (error "The current group does not support article moving"))
8106         ((and (eq action 'crosspost)
8107               (not (gnus-check-backend-function
8108                     'request-replace-article gnus-newsgroup-name)))
8109          (error "The current group does not support article editing")))
8110   (let ((articles (gnus-summary-work-articles n))
8111         (prefix (if (gnus-check-backend-function
8112                      'request-move-article gnus-newsgroup-name)
8113                     (gnus-group-real-prefix gnus-newsgroup-name)
8114                   ""))
8115         (names '((move "Move" "Moving")
8116                  (copy "Copy" "Copying")
8117                  (crosspost "Crosspost" "Crossposting")))
8118         (copy-buf (save-excursion
8119                     (nnheader-set-temp-buffer " *copy article*")))
8120         (default-marks gnus-article-mark-lists)
8121         (no-expire-marks (delete '(expirable . expire)
8122                                  (copy-sequence gnus-article-mark-lists)))
8123         art-group to-method new-xref article to-groups)
8124     (unless (assq action names)
8125       (error "Unknown action %s" action))
8126     ;; Read the newsgroup name.
8127     (when (and (not to-newsgroup)
8128                (not select-method))
8129       (setq to-newsgroup
8130             (gnus-read-move-group-name
8131              (cadr (assq action names))
8132              (symbol-value (intern (format "gnus-current-%s-group" action)))
8133              articles prefix))
8134       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8135     (setq to-method (or select-method
8136                         (gnus-server-to-method
8137                          (gnus-group-method to-newsgroup))))
8138     ;; Check the method we are to move this article to...
8139     (unless (gnus-check-backend-function
8140              'request-accept-article (car to-method))
8141       (error "%s does not support article copying" (car to-method)))
8142     (unless (gnus-check-server to-method)
8143       (error "Can't open server %s" (car to-method)))
8144     (gnus-message 6 "%s to %s: %s..."
8145                   (caddr (assq action names))
8146                   (or (car select-method) to-newsgroup) articles)
8147     (while articles
8148       (setq article (pop articles))
8149       (setq
8150        art-group
8151        (cond
8152         ;; Move the article.
8153         ((eq action 'move)
8154          ;; Remove this article from future suppression.
8155          (gnus-dup-unsuppress-article article)
8156          (gnus-request-move-article
8157           article                       ; Article to move
8158           gnus-newsgroup-name           ; From newsgroup
8159           (nth 1 (gnus-find-method-for-group
8160                   gnus-newsgroup-name)) ; Server
8161           (list 'gnus-request-accept-article
8162                 to-newsgroup (list 'quote select-method)
8163                 (not articles) t)       ; Accept form
8164           (not articles)))              ; Only save nov last time
8165         ;; Copy the article.
8166         ((eq action 'copy)
8167          (save-excursion
8168            (set-buffer copy-buf)
8169            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8170              (gnus-request-accept-article
8171               to-newsgroup select-method (not articles) t))))
8172         ;; Crosspost the article.
8173         ((eq action 'crosspost)
8174          (let ((xref (message-tokenize-header
8175                       (mail-header-xref (gnus-summary-article-header article))
8176                       " ")))
8177            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8178                                   ":" article))
8179            (unless xref
8180              (setq xref (list (system-name))))
8181            (setq new-xref
8182                  (concat
8183                   (mapconcat 'identity
8184                              (delete "Xref:" (delete new-xref xref))
8185                              " ")
8186                   " " new-xref))
8187            (save-excursion
8188              (set-buffer copy-buf)
8189              ;; First put the article in the destination group.
8190              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8191              (when (consp (setq art-group
8192                                 (gnus-request-accept-article
8193                                  to-newsgroup select-method (not articles))))
8194                (setq new-xref (concat new-xref " " (car art-group)
8195                                       ":" (cdr art-group)))
8196                ;; Now we have the new Xrefs header, so we insert
8197                ;; it and replace the new article.
8198                (nnheader-replace-header "Xref" new-xref)
8199                (gnus-request-replace-article
8200                 (cdr art-group) to-newsgroup (current-buffer))
8201                art-group))))))
8202       (cond
8203        ((not art-group)
8204         (gnus-message 1 "Couldn't %s article %s: %s"
8205                       (cadr (assq action names)) article
8206                       (nnheader-get-report (car to-method))))
8207        ((eq art-group 'junk)
8208         (when (eq action 'move)
8209           (gnus-summary-mark-article article gnus-canceled-mark)
8210           (gnus-message 4 "Deleted article %s" article)))
8211        (t
8212         (let* ((pto-group (gnus-group-prefixed-name
8213                            (car art-group) to-method))
8214                (entry
8215                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8216                (info (nth 2 entry))
8217                (to-group (gnus-info-group info))
8218                to-marks)
8219           ;; Update the group that has been moved to.
8220           (when (and info
8221                      (memq action '(move copy)))
8222             (unless (member to-group to-groups)
8223               (push to-group to-groups))
8224
8225             (unless (memq article gnus-newsgroup-unreads)
8226               (push 'read to-marks)
8227               (gnus-info-set-read
8228                info (gnus-add-to-range (gnus-info-read info)
8229                                        (list (cdr art-group)))))
8230
8231             ;; See whether the article is to be put in the cache.
8232             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8233                              default-marks
8234                            no-expire-marks))
8235                   (to-article (cdr art-group)))
8236
8237               ;; Enter the article into the cache in the new group,
8238               ;; if that is required.
8239               (when gnus-use-cache
8240                 (gnus-cache-possibly-enter-article
8241                  to-group to-article
8242                  (let ((header (copy-sequence
8243                                 (gnus-summary-article-header article))))
8244                    (mail-header-set-number header to-article)
8245                    header)
8246                  (memq article gnus-newsgroup-marked)
8247                  (memq article gnus-newsgroup-dormant)
8248                  (memq article gnus-newsgroup-unreads)))
8249
8250               (when gnus-preserve-marks
8251                 ;; Copy any marks over to the new group.
8252                 (when (and (equal to-group gnus-newsgroup-name)
8253                            (not (memq article gnus-newsgroup-unreads)))
8254                   ;; Mark this article as read in this group.
8255                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8256                   (setcdr (gnus-active to-group) to-article)
8257                   (setcdr gnus-newsgroup-active to-article))
8258
8259                 (while marks
8260                   (when (memq article (symbol-value
8261                                        (intern (format "gnus-newsgroup-%s"
8262                                                        (caar marks)))))
8263                     (push (cdar marks) to-marks)
8264                     ;; If the other group is the same as this group,
8265                     ;; then we have to add the mark to the list.
8266                     (when (equal to-group gnus-newsgroup-name)
8267                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8268                            (cons to-article
8269                                  (symbol-value
8270                                   (intern (format "gnus-newsgroup-%s"
8271                                                   (caar marks)))))))
8272                     ;; Copy the marks to other group.
8273                     (gnus-add-marked-articles
8274                      to-group (cdar marks) (list to-article) info))
8275                   (setq marks (cdr marks)))
8276
8277                 (gnus-request-set-mark to-group (list (list (list to-article)
8278                                                             'add
8279                                                             to-marks))))
8280
8281               (gnus-dribble-enter
8282                (concat "(gnus-group-set-info '"
8283                        (gnus-prin1-to-string (gnus-get-info to-group))
8284                        ")"))))
8285
8286           ;; Update the Xref header in this article to point to
8287           ;; the new crossposted article we have just created.
8288           (when (eq action 'crosspost)
8289             (save-excursion
8290               (set-buffer copy-buf)
8291               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8292               (nnheader-replace-header "Xref" new-xref)
8293               (gnus-request-replace-article
8294                article gnus-newsgroup-name (current-buffer)))))
8295
8296         ;;;!!!Why is this necessary?
8297         (set-buffer gnus-summary-buffer)
8298
8299         (gnus-summary-goto-subject article)
8300         (when (eq action 'move)
8301           (gnus-summary-mark-article article gnus-canceled-mark))))
8302       (gnus-summary-remove-process-mark article))
8303     ;; Re-activate all groups that have been moved to.
8304     (while to-groups
8305       (save-excursion
8306         (set-buffer gnus-group-buffer)
8307         (when (gnus-group-goto-group (car to-groups) t)
8308           (gnus-group-get-new-news-this-group 1 t))
8309         (pop to-groups)))
8310
8311     (gnus-kill-buffer copy-buf)
8312     (gnus-summary-position-point)
8313     (gnus-set-mode-line 'summary)))
8314
8315 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8316   "Move the current article to a different newsgroup.
8317 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8318 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8319 re-spool using this method."
8320   (interactive "P")
8321   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8322
8323 (defun gnus-summary-crosspost-article (&optional n)
8324   "Crosspost the current article to some other group."
8325   (interactive "P")
8326   (gnus-summary-move-article n nil nil 'crosspost))
8327
8328 (defcustom gnus-summary-respool-default-method nil
8329   "Default method for respooling an article.
8330 If nil, use to the current newsgroup method."
8331   :type '(choice (gnus-select-method :value (nnml ""))
8332                  (const nil))
8333   :group 'gnus-summary-mail)
8334
8335 (defun gnus-summary-respool-article (&optional n method)
8336   "Respool the current article.
8337 The article will be squeezed through the mail spooling process again,
8338 which means that it will be put in some mail newsgroup or other
8339 depending on `nnmail-split-methods'.
8340 If N is a positive number, respool the N next articles.
8341 If N is a negative number, respool the N previous articles.
8342 If N is nil and any articles have been marked with the process mark,
8343 respool those articles instead.
8344
8345 Respooling can be done both from mail groups and \"real\" newsgroups.
8346 In the former case, the articles in question will be moved from the
8347 current group into whatever groups they are destined to.  In the
8348 latter case, they will be copied into the relevant groups."
8349   (interactive
8350    (list current-prefix-arg
8351          (let* ((methods (gnus-methods-using 'respool))
8352                 (methname
8353                  (symbol-name (or gnus-summary-respool-default-method
8354                                   (car (gnus-find-method-for-group
8355                                         gnus-newsgroup-name)))))
8356                 (method
8357                  (gnus-completing-read
8358                   methname "What backend do you want to use when respooling?"
8359                   methods nil t nil 'gnus-mail-method-history))
8360                 ms)
8361            (cond
8362             ((zerop (length (setq ms (gnus-servers-using-backend
8363                                       (intern method)))))
8364              (list (intern method) ""))
8365             ((= 1 (length ms))
8366              (car ms))
8367             (t
8368              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8369                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8370                            ms-alist))))))))
8371   (unless method
8372     (error "No method given for respooling"))
8373   (if (assoc (symbol-name
8374               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8375              (gnus-methods-using 'respool))
8376       (gnus-summary-move-article n nil method)
8377     (gnus-summary-copy-article n nil method)))
8378
8379 (defun gnus-summary-import-article (file &optional edit)
8380   "Import an arbitrary file into a mail newsgroup."
8381   (interactive "fImport file: \nP")
8382   (let ((group gnus-newsgroup-name)
8383         (now (current-time))
8384         atts lines group-art)
8385     (unless (gnus-check-backend-function 'request-accept-article group)
8386       (error "%s does not support article importing" group))
8387     (or (file-readable-p file)
8388         (not (file-regular-p file))
8389         (error "Can't read %s" file))
8390     (save-excursion
8391       (set-buffer (gnus-get-buffer-create " *import file*"))
8392       (erase-buffer)
8393       (nnheader-insert-file-contents file)
8394       (goto-char (point-min))
8395       (if (nnheader-article-p)
8396           (save-restriction
8397             (goto-char (point-min))
8398             (search-forward "\n\n" nil t)
8399             (narrow-to-region (point-min) (1- (point)))
8400             (goto-char (point-min))
8401             (unless (re-search-forward "^date:" nil t)
8402               (goto-char (point-max))
8403               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8404         ;; This doesn't look like an article, so we fudge some headers.
8405         (setq atts (file-attributes file)
8406               lines (count-lines (point-min) (point-max)))
8407         (insert "From: " (read-string "From: ") "\n"
8408                 "Subject: " (read-string "Subject: ") "\n"
8409                 "Date: " (message-make-date (nth 5 atts)) "\n"
8410                 "Message-ID: " (message-make-message-id) "\n"
8411                 "Lines: " (int-to-string lines) "\n"
8412                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8413       (setq group-art (gnus-request-accept-article group nil t))
8414       (kill-buffer (current-buffer)))
8415     (setq gnus-newsgroup-active (gnus-activate-group group))
8416     (forward-line 1)
8417     (gnus-summary-goto-article (cdr group-art) nil t)
8418     (when edit
8419       (gnus-summary-edit-article))))
8420
8421 (defun gnus-summary-create-article ()
8422   "Create an article in a mail newsgroup."
8423   (interactive)
8424   (let ((group gnus-newsgroup-name)
8425         (now (current-time))
8426         group-art)
8427     (unless (gnus-check-backend-function 'request-accept-article group)
8428       (error "%s does not support article importing" group))
8429     (save-excursion
8430       (set-buffer (gnus-get-buffer-create " *import file*"))
8431       (erase-buffer)
8432       (goto-char (point-min))
8433       ;; This doesn't look like an article, so we fudge some headers.
8434       (insert "From: " (read-string "From: ") "\n"
8435               "Subject: " (read-string "Subject: ") "\n"
8436               "Date: " (message-make-date now) "\n"
8437               "Message-ID: " (message-make-message-id) "\n")
8438       (setq group-art (gnus-request-accept-article group nil t))
8439       (kill-buffer (current-buffer)))
8440     (setq gnus-newsgroup-active (gnus-activate-group group))
8441     (forward-line 1)
8442     (gnus-summary-goto-article (cdr group-art) nil t)
8443     (gnus-summary-edit-article)))
8444
8445 (defun gnus-summary-article-posted-p ()
8446   "Say whether the current (mail) article is available from news as well.
8447 This will be the case if the article has both been mailed and posted."
8448   (interactive)
8449   (let ((id (mail-header-references (gnus-summary-article-header)))
8450         (gnus-override-method (car (gnus-refer-article-methods))))
8451     (if (gnus-request-head id "")
8452         (gnus-message 2 "The current message was found on %s"
8453                       gnus-override-method)
8454       (gnus-message 2 "The current message couldn't be found on %s"
8455                     gnus-override-method)
8456       nil)))
8457
8458 (defun gnus-summary-expire-articles (&optional now)
8459   "Expire all articles that are marked as expirable in the current group."
8460   (interactive)
8461   (when (gnus-check-backend-function
8462          'request-expire-articles gnus-newsgroup-name)
8463     ;; This backend supports expiry.
8464     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8465            (expirable (if total
8466                           (progn
8467                             ;; We need to update the info for
8468                             ;; this group for `gnus-list-of-read-articles'
8469                             ;; to give us the right answer.
8470                             (gnus-run-hooks 'gnus-exit-group-hook)
8471                             (gnus-summary-update-info)
8472                             (gnus-list-of-read-articles gnus-newsgroup-name))
8473                         (setq gnus-newsgroup-expirable
8474                               (sort gnus-newsgroup-expirable '<))))
8475            (expiry-wait (if now 'immediate
8476                           (gnus-group-find-parameter
8477                            gnus-newsgroup-name 'expiry-wait)))
8478            (nnmail-expiry-target
8479             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8480                 nnmail-expiry-target))
8481            es)
8482       (when expirable
8483         ;; There are expirable articles in this group, so we run them
8484         ;; through the expiry process.
8485         (gnus-message 6 "Expiring articles...")
8486         (unless (gnus-check-group gnus-newsgroup-name)
8487           (error "Can't open server for %s" gnus-newsgroup-name))
8488         ;; The list of articles that weren't expired is returned.
8489         (save-excursion
8490           (if expiry-wait
8491               (let ((nnmail-expiry-wait-function nil)
8492                     (nnmail-expiry-wait expiry-wait))
8493                 (setq es (gnus-request-expire-articles
8494                           expirable gnus-newsgroup-name)))
8495             (setq es (gnus-request-expire-articles
8496                       expirable gnus-newsgroup-name)))
8497           (unless total
8498             (setq gnus-newsgroup-expirable es))
8499           ;; We go through the old list of expirable, and mark all
8500           ;; really expired articles as nonexistent.
8501           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8502             (let ((gnus-use-cache nil))
8503               (while expirable
8504                 (unless (memq (car expirable) es)
8505                   (when (gnus-data-find (car expirable))
8506                     (gnus-summary-mark-article
8507                      (car expirable) gnus-canceled-mark)))
8508                 (setq expirable (cdr expirable))))))
8509         (gnus-message 6 "Expiring articles...done")))))
8510
8511 (defun gnus-summary-expire-articles-now ()
8512   "Expunge all expirable articles in the current group.
8513 This means that *all* articles that are marked as expirable will be
8514 deleted forever, right now."
8515   (interactive)
8516   (or gnus-expert-user
8517       (gnus-yes-or-no-p
8518        "Are you really, really, really sure you want to delete all these messages? ")
8519       (error "Phew!"))
8520   (gnus-summary-expire-articles t))
8521
8522 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8523 (defun gnus-summary-delete-article (&optional n)
8524   "Delete the N next (mail) articles.
8525 This command actually deletes articles.  This is not a marking
8526 command.  The article will disappear forever from your life, never to
8527 return.
8528 If N is negative, delete backwards.
8529 If N is nil and articles have been marked with the process mark,
8530 delete these instead."
8531   (interactive "P")
8532   (unless (gnus-check-backend-function 'request-expire-articles
8533                                        gnus-newsgroup-name)
8534     (error "The current newsgroup does not support article deletion"))
8535   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8536     (error "Couldn't open server"))
8537   ;; Compute the list of articles to delete.
8538   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8539         not-deleted)
8540     (if (and gnus-novice-user
8541              (not (gnus-yes-or-no-p
8542                    (format "Do you really want to delete %s forever? "
8543                            (if (> (length articles) 1)
8544                                (format "these %s articles" (length articles))
8545                              "this article")))))
8546         ()
8547       ;; Delete the articles.
8548       (setq not-deleted (gnus-request-expire-articles
8549                          articles gnus-newsgroup-name 'force))
8550       (while articles
8551         (gnus-summary-remove-process-mark (car articles))
8552         ;; The backend might not have been able to delete the article
8553         ;; after all.
8554         (unless (memq (car articles) not-deleted)
8555           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8556         (setq articles (cdr articles)))
8557       (when not-deleted
8558         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8559     (gnus-summary-position-point)
8560     (gnus-set-mode-line 'summary)
8561     not-deleted))
8562
8563 (defun gnus-summary-edit-article (&optional force)
8564   "Edit the current article.
8565 This will have permanent effect only in mail groups.
8566 If FORCE is non-nil, allow editing of articles even in read-only
8567 groups."
8568   (interactive "P")
8569   (save-excursion
8570     (set-buffer gnus-summary-buffer)
8571     (let ((mail-parse-charset gnus-newsgroup-charset)
8572           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8573       (gnus-set-global-variables)
8574       (when (and (not force)
8575                  (gnus-group-read-only-p))
8576         (error "The current newsgroup does not support article editing"))
8577       (gnus-summary-show-article t)
8578       (gnus-article-edit-article
8579        'ignore
8580        `(lambda (no-highlight)
8581           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8582                 (message-options message-options)
8583                 (message-options-set-recipient)
8584                 (mail-parse-ignored-charsets
8585                  ',gnus-newsgroup-ignored-charsets))
8586             (gnus-summary-edit-article-done
8587              ,(or (mail-header-references gnus-current-headers) "")
8588              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8589
8590 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8591
8592 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8593                                                  no-highlight)
8594   "Make edits to the current article permanent."
8595   (interactive)
8596   (save-excursion
8597     ;; The buffer restriction contains the entire article if it exists.
8598     (when (article-goto-body)
8599       (let ((lines (count-lines (point) (point-max)))
8600             (length (- (point-max) (point)))
8601             (case-fold-search t)
8602             (body (copy-marker (point))))
8603         (goto-char (point-min))
8604         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8605           (delete-region (match-beginning 1) (match-end 1))
8606           (insert (number-to-string length)))
8607         (goto-char (point-min))
8608         (when (re-search-forward
8609                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8610           (delete-region (match-beginning 1) (match-end 1))
8611           (insert (number-to-string length)))
8612         (goto-char (point-min))
8613         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8614           (delete-region (match-beginning 1) (match-end 1))
8615           (insert (number-to-string lines))))))
8616   ;; Replace the article.
8617   (let ((buf (current-buffer)))
8618     (with-temp-buffer
8619       (insert-buffer-substring buf)
8620
8621       (if (and (not read-only)
8622                (not (gnus-request-replace-article
8623                      (cdr gnus-article-current) (car gnus-article-current)
8624                      (current-buffer) t)))
8625           (error "Couldn't replace article")
8626         ;; Update the summary buffer.
8627         (if (and references
8628                  (equal (message-tokenize-header references " ")
8629                         (message-tokenize-header
8630                          (or (message-fetch-field "references") "") " ")))
8631             ;; We only have to update this line.
8632             (save-excursion
8633               (save-restriction
8634                 (message-narrow-to-head)
8635                 (let ((head (buffer-string))
8636                       header)
8637                   (with-temp-buffer
8638                     (insert (format "211 %d Article retrieved.\n"
8639                                     (cdr gnus-article-current)))
8640                     (insert head)
8641                     (insert ".\n")
8642                     (let ((nntp-server-buffer (current-buffer)))
8643                       (setq header (car (gnus-get-newsgroup-headers
8644                                          (save-excursion
8645                                            (set-buffer gnus-summary-buffer)
8646                                            gnus-newsgroup-dependencies)
8647                                          t))))
8648                     (save-excursion
8649                       (set-buffer gnus-summary-buffer)
8650                       (gnus-data-set-header
8651                        (gnus-data-find (cdr gnus-article-current))
8652                        header)
8653                       (gnus-summary-update-article-line
8654                        (cdr gnus-article-current) header)
8655                       (if (gnus-summary-goto-subject
8656                            (cdr gnus-article-current) nil t)
8657                           (gnus-summary-update-secondary-mark
8658                            (cdr gnus-article-current))))))))
8659           ;; Update threads.
8660           (set-buffer (or buffer gnus-summary-buffer))
8661           (gnus-summary-update-article (cdr gnus-article-current))
8662           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8663               (gnus-summary-update-secondary-mark
8664                (cdr gnus-article-current))))
8665         ;; Prettify the article buffer again.
8666         (unless no-highlight
8667           (save-excursion
8668             (set-buffer gnus-article-buffer)
8669             ;;;!!! Fix this -- article should be rehighlighted.
8670             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8671             (set-buffer gnus-original-article-buffer)
8672             (gnus-request-article
8673              (cdr gnus-article-current)
8674              (car gnus-article-current) (current-buffer))))
8675         ;; Prettify the summary buffer line.
8676         (when (gnus-visual-p 'summary-highlight 'highlight)
8677           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8678
8679 (defun gnus-summary-edit-wash (key)
8680   "Perform editing command KEY in the article buffer."
8681   (interactive
8682    (list
8683     (progn
8684       (message "%s" (concat (this-command-keys) "- "))
8685       (read-char))))
8686   (message "")
8687   (gnus-summary-edit-article)
8688   (execute-kbd-macro (concat (this-command-keys) key))
8689   (gnus-article-edit-done))
8690
8691 ;;; Respooling
8692
8693 (defun gnus-summary-respool-query (&optional silent trace)
8694   "Query where the respool algorithm would put this article."
8695   (interactive)
8696   (let (gnus-mark-article-hook)
8697     (gnus-summary-select-article)
8698     (save-excursion
8699       (set-buffer gnus-original-article-buffer)
8700       (save-restriction
8701         (message-narrow-to-head)
8702         (let ((groups (nnmail-article-group 'identity trace)))
8703           (unless silent
8704             (if groups
8705                 (message "This message would go to %s"
8706                          (mapconcat 'car groups ", "))
8707               (message "This message would go to no groups"))
8708             groups))))))
8709
8710 (defun gnus-summary-respool-trace ()
8711   "Trace where the respool algorithm would put this article.
8712 Display a buffer showing all fancy splitting patterns which matched."
8713   (interactive)
8714   (gnus-summary-respool-query nil t))
8715
8716 ;; Summary marking commands.
8717
8718 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8719   "Mark articles which has the same subject as read, and then select the next.
8720 If UNMARK is positive, remove any kind of mark.
8721 If UNMARK is negative, tick articles."
8722   (interactive "P")
8723   (when unmark
8724     (setq unmark (prefix-numeric-value unmark)))
8725   (let ((count
8726          (gnus-summary-mark-same-subject
8727           (gnus-summary-article-subject) unmark)))
8728     ;; Select next unread article.  If auto-select-same mode, should
8729     ;; select the first unread article.
8730     (gnus-summary-next-article t (and gnus-auto-select-same
8731                                       (gnus-summary-article-subject)))
8732     (gnus-message 7 "%d article%s marked as %s"
8733                   count (if (= count 1) " is" "s are")
8734                   (if unmark "unread" "read"))))
8735
8736 (defun gnus-summary-kill-same-subject (&optional unmark)
8737   "Mark articles which has the same subject as read.
8738 If UNMARK is positive, remove any kind of mark.
8739 If UNMARK is negative, tick articles."
8740   (interactive "P")
8741   (when unmark
8742     (setq unmark (prefix-numeric-value unmark)))
8743   (let ((count
8744          (gnus-summary-mark-same-subject
8745           (gnus-summary-article-subject) unmark)))
8746     ;; If marked as read, go to next unread subject.
8747     (when (null unmark)
8748       ;; Go to next unread subject.
8749       (gnus-summary-next-subject 1 t))
8750     (gnus-message 7 "%d articles are marked as %s"
8751                   count (if unmark "unread" "read"))))
8752
8753 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8754   "Mark articles with same SUBJECT as read, and return marked number.
8755 If optional argument UNMARK is positive, remove any kinds of marks.
8756 If optional argument UNMARK is negative, mark articles as unread instead."
8757   (let ((count 1))
8758     (save-excursion
8759       (cond
8760        ((null unmark)                   ; Mark as read.
8761         (while (and
8762                 (progn
8763                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8764                   (gnus-summary-show-thread) t)
8765                 (gnus-summary-find-subject subject))
8766           (setq count (1+ count))))
8767        ((> unmark 0)                    ; Tick.
8768         (while (and
8769                 (progn
8770                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8771                   (gnus-summary-show-thread) t)
8772                 (gnus-summary-find-subject subject))
8773           (setq count (1+ count))))
8774        (t                               ; Mark as unread.
8775         (while (and
8776                 (progn
8777                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8778                   (gnus-summary-show-thread) t)
8779                 (gnus-summary-find-subject subject))
8780           (setq count (1+ count)))))
8781       (gnus-set-mode-line 'summary)
8782       ;; Return the number of marked articles.
8783       count)))
8784
8785 (defun gnus-summary-mark-as-processable (n &optional unmark)
8786   "Set the process mark on the next N articles.
8787 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8788 the process mark instead.  The difference between N and the actual
8789 number of articles marked is returned."
8790   (interactive "P")
8791   (if (and (null n) (gnus-region-active-p))
8792       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8793     (setq n (prefix-numeric-value n))
8794     (let ((backward (< n 0))
8795           (n (abs n)))
8796       (while (and
8797               (> n 0)
8798               (if unmark
8799                   (gnus-summary-remove-process-mark
8800                    (gnus-summary-article-number))
8801                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8802               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8803         (setq n (1- n)))
8804       (when (/= 0 n)
8805         (gnus-message 7 "No more articles"))
8806       (gnus-summary-recenter)
8807       (gnus-summary-position-point)
8808       n)))
8809
8810 (defun gnus-summary-unmark-as-processable (n)
8811   "Remove the process mark from the next N articles.
8812 If N is negative, unmark backward instead.  The difference between N and
8813 the actual number of articles unmarked is returned."
8814   (interactive "P")
8815   (gnus-summary-mark-as-processable n t))
8816
8817 (defun gnus-summary-unmark-all-processable ()
8818   "Remove the process mark from all articles."
8819   (interactive)
8820   (save-excursion
8821     (while gnus-newsgroup-processable
8822       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8823   (gnus-summary-position-point))
8824
8825 (defun gnus-summary-add-mark (article type)
8826   "Mark ARTICLE with a mark of TYPE."
8827   (let ((vtype (car (assq type gnus-article-mark-lists)))
8828         var)
8829     (if (not vtype)
8830         (error "No such mark type: %s" type)
8831       (setq var (intern (format "gnus-newsgroup-%s" type)))
8832       (set var (cons article (symbol-value var)))
8833       (if (memq type '(processable cached replied forwarded recent saved))
8834           (gnus-summary-update-secondary-mark article)
8835         ;;; !!! This is bogus.  We should find out what primary
8836         ;;; !!! mark we want to set.
8837         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8838
8839 (defun gnus-summary-mark-as-expirable (n)
8840   "Mark N articles forward as expirable.
8841 If N is negative, mark backward instead.  The difference between N and
8842 the actual number of articles marked is returned."
8843   (interactive "p")
8844   (gnus-summary-mark-forward n gnus-expirable-mark))
8845
8846 (defun gnus-summary-mark-article-as-replied (article)
8847   "Mark ARTICLE as replied to and update the summary line.
8848 ARTICLE can also be a list of articles."
8849   (interactive (list (gnus-summary-article-number)))
8850   (let ((articles (if (listp article) article (list article))))
8851     (dolist (article articles)
8852       (push article gnus-newsgroup-replied)
8853       (let ((buffer-read-only nil))
8854         (when (gnus-summary-goto-subject article nil t)
8855           (gnus-summary-update-secondary-mark article))))))
8856
8857 (defun gnus-summary-mark-article-as-forwarded (article)
8858   "Mark ARTICLE as forwarded and update the summary line.
8859 ARTICLE can also be a list of articles."
8860   (let ((articles (if (listp article) article (list article))))
8861     (dolist (article articles)
8862       (push article gnus-newsgroup-forwarded)
8863       (let ((buffer-read-only nil))
8864         (when (gnus-summary-goto-subject article nil t)
8865           (gnus-summary-update-secondary-mark article))))))
8866
8867 (defun gnus-summary-set-bookmark (article)
8868   "Set a bookmark in current article."
8869   (interactive (list (gnus-summary-article-number)))
8870   (when (or (not (get-buffer gnus-article-buffer))
8871             (not gnus-current-article)
8872             (not gnus-article-current)
8873             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8874     (error "No current article selected"))
8875   ;; Remove old bookmark, if one exists.
8876   (let ((old (assq article gnus-newsgroup-bookmarks)))
8877     (when old
8878       (setq gnus-newsgroup-bookmarks
8879             (delq old gnus-newsgroup-bookmarks))))
8880   ;; Set the new bookmark, which is on the form
8881   ;; (article-number . line-number-in-body).
8882   (push
8883    (cons article
8884          (save-excursion
8885            (set-buffer gnus-article-buffer)
8886            (count-lines
8887             (min (point)
8888                  (save-excursion
8889                    (goto-char (point-min))
8890                    (search-forward "\n\n" nil t)
8891                    (point)))
8892             (point))))
8893    gnus-newsgroup-bookmarks)
8894   (gnus-message 6 "A bookmark has been added to the current article."))
8895
8896 (defun gnus-summary-remove-bookmark (article)
8897   "Remove the bookmark from the current article."
8898   (interactive (list (gnus-summary-article-number)))
8899   ;; Remove old bookmark, if one exists.
8900   (let ((old (assq article gnus-newsgroup-bookmarks)))
8901     (if old
8902         (progn
8903           (setq gnus-newsgroup-bookmarks
8904                 (delq old gnus-newsgroup-bookmarks))
8905           (gnus-message 6 "Removed bookmark."))
8906       (gnus-message 6 "No bookmark in current article."))))
8907
8908 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8909 (defun gnus-summary-mark-as-dormant (n)
8910   "Mark N articles forward as dormant.
8911 If N is negative, mark backward instead.  The difference between N and
8912 the actual number of articles marked is returned."
8913   (interactive "p")
8914   (gnus-summary-mark-forward n gnus-dormant-mark))
8915
8916 (defun gnus-summary-set-process-mark (article)
8917   "Set the process mark on ARTICLE and update the summary line."
8918   (setq gnus-newsgroup-processable
8919         (cons article
8920               (delq article gnus-newsgroup-processable)))
8921   (when (gnus-summary-goto-subject article)
8922     (gnus-summary-show-thread)
8923     (gnus-summary-goto-subject article)
8924     (gnus-summary-update-secondary-mark article)))
8925
8926 (defun gnus-summary-remove-process-mark (article)
8927   "Remove the process mark from ARTICLE and update the summary line."
8928   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8929   (when (gnus-summary-goto-subject article)
8930     (gnus-summary-show-thread)
8931     (gnus-summary-goto-subject article)
8932     (gnus-summary-update-secondary-mark article)))
8933
8934 (defun gnus-summary-set-saved-mark (article)
8935   "Set the process mark on ARTICLE and update the summary line."
8936   (push article gnus-newsgroup-saved)
8937   (when (gnus-summary-goto-subject article)
8938     (gnus-summary-update-secondary-mark article)))
8939
8940 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8941   "Mark N articles as read forwards.
8942 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8943 The difference between N and the actual number of articles marked is
8944 returned.
8945 Iff NO-EXPIRE, auto-expiry will be inhibited."
8946   (interactive "p")
8947   (gnus-summary-show-thread)
8948   (let ((backward (< n 0))
8949         (gnus-summary-goto-unread
8950          (and gnus-summary-goto-unread
8951               (not (eq gnus-summary-goto-unread 'never))
8952               (not (memq mark (list gnus-unread-mark
8953                                     gnus-ticked-mark gnus-dormant-mark)))))
8954         (n (abs n))
8955         (mark (or mark gnus-del-mark)))
8956     (while (and (> n 0)
8957                 (gnus-summary-mark-article nil mark no-expire)
8958                 (zerop (gnus-summary-next-subject
8959                         (if backward -1 1)
8960                         (and gnus-summary-goto-unread
8961                              (not (eq gnus-summary-goto-unread 'never)))
8962                         t)))
8963       (setq n (1- n)))
8964     (when (/= 0 n)
8965       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8966     (gnus-summary-recenter)
8967     (gnus-summary-position-point)
8968     (gnus-set-mode-line 'summary)
8969     n))
8970
8971 (defun gnus-summary-mark-article-as-read (mark)
8972   "Mark the current article quickly as read with MARK."
8973   (let ((article (gnus-summary-article-number)))
8974     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8975     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8976     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8977     (push (cons article mark) gnus-newsgroup-reads)
8978     ;; Possibly remove from cache, if that is used.
8979     (when gnus-use-cache
8980       (gnus-cache-enter-remove-article article))
8981     ;; Allow the backend to change the mark.
8982     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8983     ;; Check for auto-expiry.
8984     (when (and gnus-newsgroup-auto-expire
8985                (memq mark gnus-auto-expirable-marks))
8986       (setq mark gnus-expirable-mark)
8987       ;; Let the backend know about the mark change.
8988       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8989       (push article gnus-newsgroup-expirable))
8990     ;; Set the mark in the buffer.
8991     (gnus-summary-update-mark mark 'unread)
8992     t))
8993
8994 (defun gnus-summary-mark-article-as-unread (mark)
8995   "Mark the current article quickly as unread with MARK."
8996   (let* ((article (gnus-summary-article-number))
8997          (old-mark (gnus-summary-article-mark article)))
8998     ;; Allow the backend to change the mark.
8999     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9000     (if (eq mark old-mark)
9001         t
9002       (if (<= article 0)
9003           (progn
9004             (gnus-error 1 "Can't mark negative article numbers")
9005             nil)
9006         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9007         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9008         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9009         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9010         (cond ((= mark gnus-ticked-mark)
9011                (push article gnus-newsgroup-marked))
9012               ((= mark gnus-dormant-mark)
9013                (push article gnus-newsgroup-dormant))
9014               (t
9015                (push article gnus-newsgroup-unreads)))
9016         (gnus-pull article gnus-newsgroup-reads)
9017
9018         ;; See whether the article is to be put in the cache.
9019         (and gnus-use-cache
9020              (vectorp (gnus-summary-article-header article))
9021              (save-excursion
9022                (gnus-cache-possibly-enter-article
9023                 gnus-newsgroup-name article
9024                 (gnus-summary-article-header article)
9025                 (= mark gnus-ticked-mark)
9026                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9027
9028         ;; Fix the mark.
9029         (gnus-summary-update-mark mark 'unread)
9030         t))))
9031
9032 (defun gnus-summary-mark-article (&optional article mark no-expire)
9033   "Mark ARTICLE with MARK.  MARK can be any character.
9034 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9035 `??' (dormant) and `?E' (expirable).
9036 If MARK is nil, then the default character `?r' is used.
9037 If ARTICLE is nil, then the article on the current line will be
9038 marked.
9039 Iff NO-EXPIRE, auto-expiry will be inhibited."
9040   ;; The mark might be a string.
9041   (when (stringp mark)
9042     (setq mark (aref mark 0)))
9043   ;; If no mark is given, then we check auto-expiring.
9044   (when (null mark)
9045     (setq mark gnus-del-mark))
9046   (when (and (not no-expire)
9047              gnus-newsgroup-auto-expire
9048              (memq mark gnus-auto-expirable-marks))
9049     (setq mark gnus-expirable-mark))
9050   (let ((article (or article (gnus-summary-article-number)))
9051         (old-mark (gnus-summary-article-mark article)))
9052     ;; Allow the backend to change the mark.
9053     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9054     (if (eq mark old-mark)
9055         t
9056       (unless article
9057         (error "No article on current line"))
9058       (if (not (if (or (= mark gnus-unread-mark)
9059                        (= mark gnus-ticked-mark)
9060                        (= mark gnus-dormant-mark))
9061                    (gnus-mark-article-as-unread article mark)
9062                  (gnus-mark-article-as-read article mark)))
9063           t
9064         ;; See whether the article is to be put in the cache.
9065         (and gnus-use-cache
9066              (not (= mark gnus-canceled-mark))
9067              (vectorp (gnus-summary-article-header article))
9068              (save-excursion
9069                (gnus-cache-possibly-enter-article
9070                 gnus-newsgroup-name article
9071                 (gnus-summary-article-header article)
9072                 (= mark gnus-ticked-mark)
9073                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9074
9075         (when (gnus-summary-goto-subject article nil t)
9076           (let ((buffer-read-only nil))
9077             (gnus-summary-show-thread)
9078             ;; Fix the mark.
9079             (gnus-summary-update-mark mark 'unread)
9080             t))))))
9081
9082 (defun gnus-summary-update-secondary-mark (article)
9083   "Update the secondary (read, process, cache) mark."
9084   (gnus-summary-update-mark
9085    (cond ((memq article gnus-newsgroup-processable)
9086           gnus-process-mark)
9087          ((memq article gnus-newsgroup-cached)
9088           gnus-cached-mark)
9089          ((memq article gnus-newsgroup-replied)
9090           gnus-replied-mark)
9091          ((memq article gnus-newsgroup-forwarded)
9092           gnus-forwarded-mark)
9093          ((memq article gnus-newsgroup-saved)
9094           gnus-saved-mark)
9095          ((memq article gnus-newsgroup-recent)
9096           gnus-recent-mark)
9097          ((memq article gnus-newsgroup-unseen)
9098           gnus-unseen-mark)
9099          (t gnus-no-mark))
9100    'replied)
9101   (when (gnus-visual-p 'summary-highlight 'highlight)
9102     (gnus-run-hooks 'gnus-summary-update-hook))
9103   t)
9104
9105 (defun gnus-summary-update-mark (mark type)
9106   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9107         (buffer-read-only nil))
9108     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9109     (when forward
9110       (when (looking-at "\r")
9111         (incf forward))
9112       (when (<= (+ forward (point)) (point-max))
9113         ;; Go to the right position on the line.
9114         (goto-char (+ forward (point)))
9115         ;; Replace the old mark with the new mark.
9116         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9117         ;; Optionally update the marks by some user rule.
9118         (when (eq type 'unread)
9119           (gnus-data-set-mark
9120            (gnus-data-find (gnus-summary-article-number)) mark)
9121           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9122
9123 (defun gnus-mark-article-as-read (article &optional mark)
9124   "Enter ARTICLE in the pertinent lists and remove it from others."
9125   ;; Make the article expirable.
9126   (let ((mark (or mark gnus-del-mark)))
9127     (if (= mark gnus-expirable-mark)
9128         (push article gnus-newsgroup-expirable)
9129       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9130     ;; Remove from unread and marked lists.
9131     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9132     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9133     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9134     (push (cons article mark) gnus-newsgroup-reads)
9135     ;; Possibly remove from cache, if that is used.
9136     (when gnus-use-cache
9137       (gnus-cache-enter-remove-article article))
9138     t))
9139
9140 (defun gnus-mark-article-as-unread (article &optional mark)
9141   "Enter ARTICLE in the pertinent lists and remove it from others."
9142   (let ((mark (or mark gnus-ticked-mark)))
9143     (if (<= article 0)
9144         (progn
9145           (gnus-error 1 "Can't mark negative article numbers")
9146           nil)
9147       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9148             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9149             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9150             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9151
9152       ;; Unsuppress duplicates?
9153       (when gnus-suppress-duplicates
9154         (gnus-dup-unsuppress-article article))
9155
9156       (cond ((= mark gnus-ticked-mark)
9157              (push article gnus-newsgroup-marked))
9158             ((= mark gnus-dormant-mark)
9159              (push article gnus-newsgroup-dormant))
9160             (t
9161              (push article gnus-newsgroup-unreads)))
9162       (gnus-pull article gnus-newsgroup-reads)
9163       t)))
9164
9165 (defalias 'gnus-summary-mark-as-unread-forward
9166   'gnus-summary-tick-article-forward)
9167 (make-obsolete 'gnus-summary-mark-as-unread-forward
9168                'gnus-summary-tick-article-forward)
9169 (defun gnus-summary-tick-article-forward (n)
9170   "Tick N articles forwards.
9171 If N is negative, tick backwards instead.
9172 The difference between N and the number of articles ticked is returned."
9173   (interactive "p")
9174   (gnus-summary-mark-forward n gnus-ticked-mark))
9175
9176 (defalias 'gnus-summary-mark-as-unread-backward
9177   'gnus-summary-tick-article-backward)
9178 (make-obsolete 'gnus-summary-mark-as-unread-backward
9179                'gnus-summary-tick-article-backward)
9180 (defun gnus-summary-tick-article-backward (n)
9181   "Tick N articles backwards.
9182 The difference between N and the number of articles ticked is returned."
9183   (interactive "p")
9184   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9185
9186 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9187 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9188 (defun gnus-summary-tick-article (&optional article clear-mark)
9189   "Mark current article as unread.
9190 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9191 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9192   (interactive)
9193   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9194                                        gnus-ticked-mark)))
9195
9196 (defun gnus-summary-mark-as-read-forward (n)
9197   "Mark N articles as read forwards.
9198 If N is negative, mark backwards instead.
9199 The difference between N and the actual number of articles marked is
9200 returned."
9201   (interactive "p")
9202   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9203
9204 (defun gnus-summary-mark-as-read-backward (n)
9205   "Mark the N articles as read backwards.
9206 The difference between N and the actual number of articles marked is
9207 returned."
9208   (interactive "p")
9209   (gnus-summary-mark-forward
9210    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9211
9212 (defun gnus-summary-mark-as-read (&optional article mark)
9213   "Mark current article as read.
9214 ARTICLE specifies the article to be marked as read.
9215 MARK specifies a string to be inserted at the beginning of the line."
9216   (gnus-summary-mark-article article mark))
9217
9218 (defun gnus-summary-clear-mark-forward (n)
9219   "Clear marks from N articles forward.
9220 If N is negative, clear backward instead.
9221 The difference between N and the number of marks cleared is returned."
9222   (interactive "p")
9223   (gnus-summary-mark-forward n gnus-unread-mark))
9224
9225 (defun gnus-summary-clear-mark-backward (n)
9226   "Clear marks from N articles backward.
9227 The difference between N and the number of marks cleared is returned."
9228   (interactive "p")
9229   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9230
9231 (defun gnus-summary-mark-unread-as-read ()
9232   "Intended to be used by `gnus-summary-mark-article-hook'."
9233   (when (memq gnus-current-article gnus-newsgroup-unreads)
9234     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9235
9236 (defun gnus-summary-mark-read-and-unread-as-read ()
9237   "Intended to be used by `gnus-summary-mark-article-hook'."
9238   (let ((mark (gnus-summary-article-mark)))
9239     (when (or (gnus-unread-mark-p mark)
9240               (gnus-read-mark-p mark))
9241       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9242
9243 (defun gnus-summary-mark-unread-as-ticked ()
9244   "Intended to be used by `gnus-summary-mark-article-hook'."
9245   (when (memq gnus-current-article gnus-newsgroup-unreads)
9246     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9247
9248 (defun gnus-summary-mark-region-as-read (point mark all)
9249   "Mark all unread articles between point and mark as read.
9250 If given a prefix, mark all articles between point and mark as read,
9251 even ticked and dormant ones."
9252   (interactive "r\nP")
9253   (save-excursion
9254     (let (article)
9255       (goto-char point)
9256       (beginning-of-line)
9257       (while (and
9258               (< (point) mark)
9259               (progn
9260                 (when (or all
9261                           (memq (setq article (gnus-summary-article-number))
9262                                 gnus-newsgroup-unreads))
9263                   (gnus-summary-mark-article article gnus-del-mark))
9264                 t)
9265               (gnus-summary-find-next))))))
9266
9267 (defun gnus-summary-mark-below (score mark)
9268   "Mark articles with score less than SCORE with MARK."
9269   (interactive "P\ncMark: ")
9270   (setq score (if score
9271                   (prefix-numeric-value score)
9272                 (or gnus-summary-default-score 0)))
9273   (save-excursion
9274     (set-buffer gnus-summary-buffer)
9275     (goto-char (point-min))
9276     (while
9277         (progn
9278           (and (< (gnus-summary-article-score) score)
9279                (gnus-summary-mark-article nil mark))
9280           (gnus-summary-find-next)))))
9281
9282 (defun gnus-summary-kill-below (&optional score)
9283   "Mark articles with score below SCORE as read."
9284   (interactive "P")
9285   (gnus-summary-mark-below score gnus-killed-mark))
9286
9287 (defun gnus-summary-clear-above (&optional score)
9288   "Clear all marks from articles with score above SCORE."
9289   (interactive "P")
9290   (gnus-summary-mark-above score gnus-unread-mark))
9291
9292 (defun gnus-summary-tick-above (&optional score)
9293   "Tick all articles with score above SCORE."
9294   (interactive "P")
9295   (gnus-summary-mark-above score gnus-ticked-mark))
9296
9297 (defun gnus-summary-mark-above (score mark)
9298   "Mark articles with score over SCORE with MARK."
9299   (interactive "P\ncMark: ")
9300   (setq score (if score
9301                   (prefix-numeric-value score)
9302                 (or gnus-summary-default-score 0)))
9303   (save-excursion
9304     (set-buffer gnus-summary-buffer)
9305     (goto-char (point-min))
9306     (while (and (progn
9307                   (when (> (gnus-summary-article-score) score)
9308                     (gnus-summary-mark-article nil mark))
9309                   t)
9310                 (gnus-summary-find-next)))))
9311
9312 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9313 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9314 (defun gnus-summary-limit-include-expunged (&optional no-error)
9315   "Display all the hidden articles that were expunged for low scores."
9316   (interactive)
9317   (let ((buffer-read-only nil))
9318     (let ((scored gnus-newsgroup-scored)
9319           headers h)
9320       (while scored
9321         (unless (gnus-summary-article-header (caar scored))
9322           (and (setq h (gnus-number-to-header (caar scored)))
9323                (< (cdar scored) gnus-summary-expunge-below)
9324                (push h headers)))
9325         (setq scored (cdr scored)))
9326       (if (not headers)
9327           (when (not no-error)
9328             (error "No expunged articles hidden"))
9329         (goto-char (point-min))
9330         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9331         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9332         (mapcar (lambda (x) (push (mail-header-number x)
9333                                   gnus-newsgroup-limit))
9334                 headers)
9335         (gnus-summary-prepare-unthreaded (nreverse headers))
9336         (goto-char (point-min))
9337         (gnus-summary-position-point)
9338         t))))
9339
9340 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9341   "Mark all unread articles in this newsgroup as read.
9342 If prefix argument ALL is non-nil, ticked and dormant articles will
9343 also be marked as read.
9344 If QUIETLY is non-nil, no questions will be asked.
9345 If TO-HERE is non-nil, it should be a point in the buffer.  All
9346 articles before (after, if REVERSE is set) this point will be marked as read.
9347 Note that this function will only catch up the unread article
9348 in the current summary buffer limitation.
9349 The number of articles marked as read is returned."
9350   (interactive "P")
9351   (prog1
9352       (save-excursion
9353         (when (or quietly
9354                   (not gnus-interactive-catchup) ;Without confirmation?
9355                   gnus-expert-user
9356                   (gnus-y-or-n-p
9357                    (if all
9358                        "Mark absolutely all articles as read? "
9359                      "Mark all unread articles as read? ")))
9360           (if (and not-mark
9361                    (not gnus-newsgroup-adaptive)
9362                    (not gnus-newsgroup-auto-expire)
9363                    (not gnus-suppress-duplicates)
9364                    (or (not gnus-use-cache)
9365                        (eq gnus-use-cache 'passive)))
9366               (progn
9367                 (when all
9368                   (setq gnus-newsgroup-marked nil
9369                         gnus-newsgroup-dormant nil))
9370                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9371             ;; We actually mark all articles as canceled, which we
9372             ;; have to do when using auto-expiry or adaptive scoring.
9373             (gnus-summary-show-all-threads)
9374             (if (and to-here reverse)
9375                 (progn
9376                   (goto-char to-here)
9377                   (while (and
9378                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9379                           (gnus-summary-find-next (not all) nil nil t))))
9380               (when (gnus-summary-first-subject (not all) t)
9381                 (while (and
9382                         (if to-here (< (point) to-here) t)
9383                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9384                         (gnus-summary-find-next (not all) nil nil t)))))
9385             (gnus-set-mode-line 'summary))
9386           t))
9387     (gnus-summary-position-point)))
9388
9389 (defun gnus-summary-catchup-to-here (&optional all)
9390   "Mark all unticked articles before the current one as read.
9391 If ALL is non-nil, also mark ticked and dormant articles as read."
9392   (interactive "P")
9393   (save-excursion
9394     (gnus-save-hidden-threads
9395       (let ((beg (point)))
9396         ;; We check that there are unread articles.
9397         (when (or all (gnus-summary-find-prev))
9398           (gnus-summary-catchup all t beg)))))
9399   (gnus-summary-position-point))
9400
9401 (defun gnus-summary-catchup-from-here (&optional all)
9402   "Mark all unticked articles after the current one as read.
9403 If ALL is non-nil, also mark ticked and dormant articles as read."
9404   (interactive "P")
9405   (save-excursion
9406     (gnus-save-hidden-threads
9407       (let ((beg (point)))
9408         ;; We check that there are unread articles.
9409         (when (or all (gnus-summary-find-next))
9410           (gnus-summary-catchup all t beg nil t)))))
9411   (gnus-summary-position-point))
9412
9413 (defun gnus-summary-catchup-all (&optional quietly)
9414   "Mark all articles in this newsgroup as read."
9415   (interactive "P")
9416   (gnus-summary-catchup t quietly))
9417
9418 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9419   "Mark all unread articles in this group as read, then exit.
9420 If prefix argument ALL is non-nil, all articles are marked as read.
9421 If QUIETLY is non-nil, no questions will be asked."
9422   (interactive "P")
9423   (when (gnus-summary-catchup all quietly nil 'fast)
9424     ;; Select next newsgroup or exit.
9425     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9426              (eq gnus-auto-select-next 'quietly))
9427         (gnus-summary-next-group nil)
9428       (gnus-summary-exit))))
9429
9430 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9431   "Mark all articles in this newsgroup as read, and then exit."
9432   (interactive "P")
9433   (gnus-summary-catchup-and-exit t quietly))
9434
9435 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9436   "Mark all articles in this group as read and select the next group.
9437 If given a prefix, mark all articles, unread as well as ticked, as
9438 read."
9439   (interactive "P")
9440   (save-excursion
9441     (gnus-summary-catchup all))
9442   (gnus-summary-next-group))
9443
9444 ;;;
9445 ;;; with article
9446 ;;;
9447
9448 (defmacro gnus-with-article (article &rest forms)
9449   "Select ARTICLE and perform FORMS in the original article buffer.
9450 Then replace the article with the result."
9451   `(progn
9452      ;; We don't want the article to be marked as read.
9453      (let (gnus-mark-article-hook)
9454        (gnus-summary-select-article t t nil ,article))
9455      (set-buffer gnus-original-article-buffer)
9456      ,@forms
9457      (if (not (gnus-check-backend-function
9458                'request-replace-article (car gnus-article-current)))
9459          (gnus-message 5 "Read-only group; not replacing")
9460        (unless (gnus-request-replace-article
9461                 ,article (car gnus-article-current)
9462                 (current-buffer) t)
9463          (error "Couldn't replace article")))
9464      ;; The cache and backlog have to be flushed somewhat.
9465      (when gnus-keep-backlog
9466        (gnus-backlog-remove-article
9467         (car gnus-article-current) (cdr gnus-article-current)))
9468      (when gnus-use-cache
9469        (gnus-cache-update-article
9470         (car gnus-article-current) (cdr gnus-article-current)))))
9471
9472 (put 'gnus-with-article 'lisp-indent-function 1)
9473 (put 'gnus-with-article 'edebug-form-spec '(form body))
9474
9475 ;; Thread-based commands.
9476
9477 (defun gnus-summary-articles-in-thread (&optional article)
9478   "Return a list of all articles in the current thread.
9479 If ARTICLE is non-nil, return all articles in the thread that starts
9480 with that article."
9481   (let* ((article (or article (gnus-summary-article-number)))
9482          (data (gnus-data-find-list article))
9483          (top-level (gnus-data-level (car data)))
9484          (top-subject
9485           (cond ((null gnus-thread-operation-ignore-subject)
9486                  (gnus-simplify-subject-re
9487                   (mail-header-subject (gnus-data-header (car data)))))
9488                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9489                  (gnus-simplify-subject-fuzzy
9490                   (mail-header-subject (gnus-data-header (car data)))))
9491                 (t nil)))
9492          (end-point (save-excursion
9493                       (if (gnus-summary-go-to-next-thread)
9494                           (point) (point-max))))
9495          articles)
9496     (while (and data
9497                 (< (gnus-data-pos (car data)) end-point))
9498       (when (or (not top-subject)
9499                 (string= top-subject
9500                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9501                              (gnus-simplify-subject-fuzzy
9502                               (mail-header-subject
9503                                (gnus-data-header (car data))))
9504                            (gnus-simplify-subject-re
9505                             (mail-header-subject
9506                              (gnus-data-header (car data)))))))
9507         (push (gnus-data-number (car data)) articles))
9508       (unless (and (setq data (cdr data))
9509                    (> (gnus-data-level (car data)) top-level))
9510         (setq data nil)))
9511     ;; Return the list of articles.
9512     (nreverse articles)))
9513
9514 (defun gnus-summary-rethread-current ()
9515   "Rethread the thread the current article is part of."
9516   (interactive)
9517   (let* ((gnus-show-threads t)
9518          (article (gnus-summary-article-number))
9519          (id (mail-header-id (gnus-summary-article-header)))
9520          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9521     (unless id
9522       (error "No article on the current line"))
9523     (gnus-rebuild-thread id)
9524     (gnus-summary-goto-subject article)))
9525
9526 (defun gnus-summary-reparent-thread ()
9527   "Make the current article child of the marked (or previous) article.
9528
9529 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9530 is non-nil or the Subject: of both articles are the same."
9531   (interactive)
9532   (unless (not (gnus-group-read-only-p))
9533     (error "The current newsgroup does not support article editing"))
9534   (unless (<= (length gnus-newsgroup-processable) 1)
9535     (error "No more than one article may be marked"))
9536   (save-window-excursion
9537     (let ((gnus-article-buffer " *reparent*")
9538           (current-article (gnus-summary-article-number))
9539           ;; First grab the marked article, otherwise one line up.
9540           (parent-article (if (not (null gnus-newsgroup-processable))
9541                               (car gnus-newsgroup-processable)
9542                             (save-excursion
9543                               (if (eq (forward-line -1) 0)
9544                                   (gnus-summary-article-number)
9545                                 (error "Beginning of summary buffer"))))))
9546       (unless (not (eq current-article parent-article))
9547         (error "An article may not be self-referential"))
9548       (let ((message-id (mail-header-id
9549                          (gnus-summary-article-header parent-article))))
9550         (unless (and message-id (not (equal message-id "")))
9551           (error "No message-id in desired parent"))
9552         (gnus-with-article current-article
9553           (save-restriction
9554             (goto-char (point-min))
9555             (message-narrow-to-head)
9556             (if (re-search-forward "^References: " nil t)
9557                 (progn
9558                   (re-search-forward "^[^ \t]" nil t)
9559                   (forward-line -1)
9560                   (end-of-line)
9561                   (insert " " message-id))
9562               (insert "References: " message-id "\n"))))
9563         (set-buffer gnus-summary-buffer)
9564         (gnus-summary-unmark-all-processable)
9565         (gnus-summary-update-article current-article)
9566         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9567             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9568         (gnus-summary-rethread-current)
9569         (gnus-message 3 "Article %d is now the child of article %d"
9570                       current-article parent-article)))))
9571
9572 (defun gnus-summary-toggle-threads (&optional arg)
9573   "Toggle showing conversation threads.
9574 If ARG is positive number, turn showing conversation threads on."
9575   (interactive "P")
9576   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9577     (setq gnus-show-threads
9578           (if (null arg) (not gnus-show-threads)
9579             (> (prefix-numeric-value arg) 0)))
9580     (gnus-summary-prepare)
9581     (gnus-summary-goto-subject current)
9582     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9583     (gnus-summary-position-point)))
9584
9585 (defun gnus-summary-show-all-threads ()
9586   "Show all threads."
9587   (interactive)
9588   (save-excursion
9589     (let ((buffer-read-only nil))
9590       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9591   (gnus-summary-position-point))
9592
9593 (defun gnus-summary-show-thread ()
9594   "Show thread subtrees.
9595 Returns nil if no thread was there to be shown."
9596   (interactive)
9597   (let ((buffer-read-only nil)
9598         (orig (point))
9599         ;; first goto end then to beg, to have point at beg after let
9600         (end (progn (end-of-line) (point)))
9601         (beg (progn (beginning-of-line) (point))))
9602     (prog1
9603         ;; Any hidden lines here?
9604         (search-forward "\r" end t)
9605       (subst-char-in-region beg end ?\^M ?\n t)
9606       (goto-char orig)
9607       (gnus-summary-position-point))))
9608
9609 (defun gnus-summary-hide-all-threads ()
9610   "Hide all thread subtrees."
9611   (interactive)
9612   (save-excursion
9613     (goto-char (point-min))
9614     (gnus-summary-hide-thread)
9615     (while (zerop (gnus-summary-next-thread 1 t))
9616       (gnus-summary-hide-thread)))
9617   (gnus-summary-position-point))
9618
9619 (defun gnus-summary-hide-thread ()
9620   "Hide thread subtrees.
9621 Returns nil if no threads were there to be hidden."
9622   (interactive)
9623   (let ((buffer-read-only nil)
9624         (start (point))
9625         (article (gnus-summary-article-number)))
9626     (goto-char start)
9627     ;; Go forward until either the buffer ends or the subthread
9628     ;; ends.
9629     (when (and (not (eobp))
9630                (or (zerop (gnus-summary-next-thread 1 t))
9631                    (goto-char (point-max))))
9632       (prog1
9633           (if (and (> (point) start)
9634                    (search-backward "\n" start t))
9635               (progn
9636                 (subst-char-in-region start (point) ?\n ?\^M)
9637                 (gnus-summary-goto-subject article))
9638             (goto-char start)
9639             nil)))))
9640
9641 (defun gnus-summary-go-to-next-thread (&optional previous)
9642   "Go to the same level (or less) next thread.
9643 If PREVIOUS is non-nil, go to previous thread instead.
9644 Return the article number moved to, or nil if moving was impossible."
9645   (let ((level (gnus-summary-thread-level))
9646         (way (if previous -1 1))
9647         (beg (point)))
9648     (forward-line way)
9649     (while (and (not (eobp))
9650                 (< level (gnus-summary-thread-level)))
9651       (forward-line way))
9652     (if (eobp)
9653         (progn
9654           (goto-char beg)
9655           nil)
9656       (setq beg (point))
9657       (prog1
9658           (gnus-summary-article-number)
9659         (goto-char beg)))))
9660
9661 (defun gnus-summary-next-thread (n &optional silent)
9662   "Go to the same level next N'th thread.
9663 If N is negative, search backward instead.
9664 Returns the difference between N and the number of skips actually
9665 done.
9666
9667 If SILENT, don't output messages."
9668   (interactive "p")
9669   (let ((backward (< n 0))
9670         (n (abs n)))
9671     (while (and (> n 0)
9672                 (gnus-summary-go-to-next-thread backward))
9673       (decf n))
9674     (unless silent
9675       (gnus-summary-position-point))
9676     (when (and (not silent) (/= 0 n))
9677       (gnus-message 7 "No more threads"))
9678     n))
9679
9680 (defun gnus-summary-prev-thread (n)
9681   "Go to the same level previous N'th thread.
9682 Returns the difference between N and the number of skips actually
9683 done."
9684   (interactive "p")
9685   (gnus-summary-next-thread (- n)))
9686
9687 (defun gnus-summary-go-down-thread ()
9688   "Go down one level in the current thread."
9689   (let ((children (gnus-summary-article-children)))
9690     (when children
9691       (gnus-summary-goto-subject (car children)))))
9692
9693 (defun gnus-summary-go-up-thread ()
9694   "Go up one level in the current thread."
9695   (let ((parent (gnus-summary-article-parent)))
9696     (when parent
9697       (gnus-summary-goto-subject parent))))
9698
9699 (defun gnus-summary-down-thread (n)
9700   "Go down thread N steps.
9701 If N is negative, go up instead.
9702 Returns the difference between N and how many steps down that were
9703 taken."
9704   (interactive "p")
9705   (let ((up (< n 0))
9706         (n (abs n)))
9707     (while (and (> n 0)
9708                 (if up (gnus-summary-go-up-thread)
9709                   (gnus-summary-go-down-thread)))
9710       (setq n (1- n)))
9711     (gnus-summary-position-point)
9712     (when (/= 0 n)
9713       (gnus-message 7 "Can't go further"))
9714     n))
9715
9716 (defun gnus-summary-up-thread (n)
9717   "Go up thread N steps.
9718 If N is negative, go down instead.
9719 Returns the difference between N and how many steps down that were
9720 taken."
9721   (interactive "p")
9722   (gnus-summary-down-thread (- n)))
9723
9724 (defun gnus-summary-top-thread ()
9725   "Go to the top of the thread."
9726   (interactive)
9727   (while (gnus-summary-go-up-thread))
9728   (gnus-summary-article-number))
9729
9730 (defun gnus-summary-kill-thread (&optional unmark)
9731   "Mark articles under current thread as read.
9732 If the prefix argument is positive, remove any kinds of marks.
9733 If the prefix argument is negative, tick articles instead."
9734   (interactive "P")
9735   (when unmark
9736     (setq unmark (prefix-numeric-value unmark)))
9737   (let ((articles (gnus-summary-articles-in-thread)))
9738     (save-excursion
9739       ;; Expand the thread.
9740       (gnus-summary-show-thread)
9741       ;; Mark all the articles.
9742       (while articles
9743         (gnus-summary-goto-subject (car articles))
9744         (cond ((null unmark)
9745                (gnus-summary-mark-article-as-read gnus-killed-mark))
9746               ((> unmark 0)
9747                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9748               (t
9749                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9750         (setq articles (cdr articles))))
9751     ;; Hide killed subtrees.
9752     (and (null unmark)
9753          gnus-thread-hide-killed
9754          (gnus-summary-hide-thread))
9755     ;; If marked as read, go to next unread subject.
9756     (when (null unmark)
9757       ;; Go to next unread subject.
9758       (gnus-summary-next-subject 1 t)))
9759   (gnus-set-mode-line 'summary))
9760
9761 ;; Summary sorting commands
9762
9763 (defun gnus-summary-sort-by-number (&optional reverse)
9764   "Sort the summary buffer by article number.
9765 Argument REVERSE means reverse order."
9766   (interactive "P")
9767   (gnus-summary-sort 'number reverse))
9768
9769 (defun gnus-summary-sort-by-author (&optional reverse)
9770   "Sort the summary buffer by author name alphabetically.
9771 If `case-fold-search' is non-nil, case of letters is ignored.
9772 Argument REVERSE means reverse order."
9773   (interactive "P")
9774   (gnus-summary-sort 'author reverse))
9775
9776 (defun gnus-summary-sort-by-subject (&optional reverse)
9777   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9778 If `case-fold-search' is non-nil, case of letters is ignored.
9779 Argument REVERSE means reverse order."
9780   (interactive "P")
9781   (gnus-summary-sort 'subject reverse))
9782
9783 (defun gnus-summary-sort-by-date (&optional reverse)
9784   "Sort the summary buffer by date.
9785 Argument REVERSE means reverse order."
9786   (interactive "P")
9787   (gnus-summary-sort 'date reverse))
9788
9789 (defun gnus-summary-sort-by-score (&optional reverse)
9790   "Sort the summary buffer by score.
9791 Argument REVERSE means reverse order."
9792   (interactive "P")
9793   (gnus-summary-sort 'score reverse))
9794
9795 (defun gnus-summary-sort-by-lines (&optional reverse)
9796   "Sort the summary buffer by the number of lines.
9797 Argument REVERSE means reverse order."
9798   (interactive "P")
9799   (gnus-summary-sort 'lines reverse))
9800
9801 (defun gnus-summary-sort-by-chars (&optional reverse)
9802   "Sort the summary buffer by article length.
9803 Argument REVERSE means reverse order."
9804   (interactive "P")
9805   (gnus-summary-sort 'chars reverse))
9806
9807 (defun gnus-summary-sort-by-original (&optional reverse)
9808   "Sort the summary buffer using the default sorting method.
9809 Argument REVERSE means reverse order."
9810   (interactive "P")
9811   (let* ((buffer-read-only)
9812          (gnus-summary-prepare-hook nil))
9813     ;; We do the sorting by regenerating the threads.
9814     (gnus-summary-prepare)
9815     ;; Hide subthreads if needed.
9816     (when (and gnus-show-threads gnus-thread-hide-subtree)
9817       (gnus-summary-hide-all-threads))))
9818
9819 (defun gnus-summary-sort (predicate reverse)
9820   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9821   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9822          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9823          (gnus-thread-sort-functions
9824           (if (not reverse)
9825               thread
9826             `(lambda (t1 t2)
9827                (,thread t2 t1))))
9828          (gnus-sort-gathered-threads-function
9829           gnus-thread-sort-functions)
9830          (gnus-article-sort-functions
9831           (if (not reverse)
9832               article
9833             `(lambda (t1 t2)
9834                (,article t2 t1))))
9835          (buffer-read-only)
9836          (gnus-summary-prepare-hook nil))
9837     ;; We do the sorting by regenerating the threads.
9838     (gnus-summary-prepare)
9839     ;; Hide subthreads if needed.
9840     (when (and gnus-show-threads gnus-thread-hide-subtree)
9841       (gnus-summary-hide-all-threads))))
9842
9843 ;; Summary saving commands.
9844
9845 (defun gnus-summary-save-article (&optional n not-saved)
9846   "Save the current article using the default saver function.
9847 If N is a positive number, save the N next articles.
9848 If N is a negative number, save the N previous articles.
9849 If N is nil and any articles have been marked with the process mark,
9850 save those articles instead.
9851 The variable `gnus-default-article-saver' specifies the saver function."
9852   (interactive "P")
9853   (let* ((articles (gnus-summary-work-articles n))
9854          (save-buffer (save-excursion
9855                         (nnheader-set-temp-buffer " *Gnus Save*")))
9856          (num (length articles))
9857          header file)
9858     (dolist (article articles)
9859       (setq header (gnus-summary-article-header article))
9860       (if (not (vectorp header))
9861           ;; This is a pseudo-article.
9862           (if (assq 'name header)
9863               (gnus-copy-file (cdr (assq 'name header)))
9864             (gnus-message 1 "Article %d is unsaveable" article))
9865         ;; This is a real article.
9866         (save-window-excursion
9867           (gnus-summary-select-article t nil nil article))
9868         (save-excursion
9869           (set-buffer save-buffer)
9870           (erase-buffer)
9871           (insert-buffer-substring gnus-original-article-buffer))
9872         (setq file (gnus-article-save save-buffer file num))
9873         (gnus-summary-remove-process-mark article)
9874         (unless not-saved
9875           (gnus-summary-set-saved-mark article))))
9876     (gnus-kill-buffer save-buffer)
9877     (gnus-summary-position-point)
9878     (gnus-set-mode-line 'summary)
9879     n))
9880
9881 (defun gnus-summary-pipe-output (&optional arg)
9882   "Pipe the current article to a subprocess.
9883 If N is a positive number, pipe the N next articles.
9884 If N is a negative number, pipe the N previous articles.
9885 If N is nil and any articles have been marked with the process mark,
9886 pipe those articles instead."
9887   (interactive "P")
9888   (require 'gnus-art)
9889   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9890     (gnus-summary-save-article arg t))
9891   (let ((buffer (get-buffer "*Shell Command Output*")))
9892     (if (and buffer
9893              (with-current-buffer buffer (> (point-max) (point-min))))
9894         (gnus-configure-windows 'pipe))))
9895
9896 (defun gnus-summary-save-article-mail (&optional arg)
9897   "Append the current article to an mail file.
9898 If N is a positive number, save the N next articles.
9899 If N is a negative number, save the N previous articles.
9900 If N is nil and any articles have been marked with the process mark,
9901 save those articles instead."
9902   (interactive "P")
9903   (require 'gnus-art)
9904   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9905     (gnus-summary-save-article arg)))
9906
9907 (defun gnus-summary-save-article-rmail (&optional arg)
9908   "Append the current article to an rmail file.
9909 If N is a positive number, save the N next articles.
9910 If N is a negative number, save the N previous articles.
9911 If N is nil and any articles have been marked with the process mark,
9912 save those articles instead."
9913   (interactive "P")
9914   (require 'gnus-art)
9915   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9916     (gnus-summary-save-article arg)))
9917
9918 (defun gnus-summary-save-article-file (&optional arg)
9919   "Append the current article to a file.
9920 If N is a positive number, save the N next articles.
9921 If N is a negative number, save the N previous articles.
9922 If N is nil and any articles have been marked with the process mark,
9923 save those articles instead."
9924   (interactive "P")
9925   (require 'gnus-art)
9926   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9927     (gnus-summary-save-article arg)))
9928
9929 (defun gnus-summary-write-article-file (&optional arg)
9930   "Write the current article to a file, deleting the previous file.
9931 If N is a positive number, save the N next articles.
9932 If N is a negative number, save the N previous articles.
9933 If N is nil and any articles have been marked with the process mark,
9934 save those articles instead."
9935   (interactive "P")
9936   (require 'gnus-art)
9937   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9938     (gnus-summary-save-article arg)))
9939
9940 (defun gnus-summary-save-article-body-file (&optional arg)
9941   "Append the current article body to a file.
9942 If N is a positive number, save the N next articles.
9943 If N is a negative number, save the N previous articles.
9944 If N is nil and any articles have been marked with the process mark,
9945 save those articles instead."
9946   (interactive "P")
9947   (require 'gnus-art)
9948   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9949     (gnus-summary-save-article arg)))
9950
9951 (defun gnus-summary-pipe-message (program)
9952   "Pipe the current article through PROGRAM."
9953   (interactive "sProgram: ")
9954   (gnus-summary-select-article)
9955   (let ((mail-header-separator ""))
9956     (gnus-eval-in-buffer-window gnus-article-buffer
9957       (save-restriction
9958         (widen)
9959         (let ((start (window-start))
9960               buffer-read-only)
9961           (message-pipe-buffer-body program)
9962           (set-window-start (get-buffer-window (current-buffer)) start))))))
9963
9964 (defun gnus-get-split-value (methods)
9965   "Return a value based on the split METHODS."
9966   (let (split-name method result match)
9967     (when methods
9968       (save-excursion
9969         (set-buffer gnus-original-article-buffer)
9970         (save-restriction
9971           (nnheader-narrow-to-headers)
9972           (while (and methods (not split-name))
9973             (goto-char (point-min))
9974             (setq method (pop methods))
9975             (setq match (car method))
9976             (when (cond
9977                    ((stringp match)
9978                     ;; Regular expression.
9979                     (ignore-errors
9980                       (re-search-forward match nil t)))
9981                    ((gnus-functionp match)
9982                     ;; Function.
9983                     (save-restriction
9984                       (widen)
9985                       (setq result (funcall match gnus-newsgroup-name))))
9986                    ((consp match)
9987                     ;; Form.
9988                     (save-restriction
9989                       (widen)
9990                       (setq result (eval match)))))
9991               (setq split-name (cdr method))
9992               (cond ((stringp result)
9993                      (push (expand-file-name
9994                             result gnus-article-save-directory)
9995                            split-name))
9996                     ((consp result)
9997                      (setq split-name (append result split-name)))))))))
9998     (nreverse split-name)))
9999
10000 (defun gnus-valid-move-group-p (group)
10001   (and (boundp group)
10002        (symbol-name group)
10003        (symbol-value group)
10004        (gnus-get-function (gnus-find-method-for-group
10005                            (symbol-name group)) 'request-accept-article t)))
10006
10007 (defun gnus-read-move-group-name (prompt default articles prefix)
10008   "Read a group name."
10009   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10010          (minibuffer-confirm-incomplete nil) ; XEmacs
10011          (prom
10012           (format "%s %s to:"
10013                   prompt
10014                   (if (> (length articles) 1)
10015                       (format "these %d articles" (length articles))
10016                     "this article")))
10017          (to-newsgroup
10018           (cond
10019            ((null split-name)
10020             (gnus-completing-read default prom
10021                                   gnus-active-hashtb
10022                                   'gnus-valid-move-group-p
10023                                   nil prefix
10024                                   'gnus-group-history))
10025            ((= 1 (length split-name))
10026             (gnus-completing-read (car split-name) prom
10027                                   gnus-active-hashtb
10028                                   'gnus-valid-move-group-p
10029                                   nil nil
10030                                   'gnus-group-history))
10031            (t
10032             (gnus-completing-read nil prom
10033                                   (mapcar (lambda (el) (list el))
10034                                           (nreverse split-name))
10035                                   nil nil nil
10036                                   'gnus-group-history))))
10037          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10038     (when to-newsgroup
10039       (if (or (string= to-newsgroup "")
10040               (string= to-newsgroup prefix))
10041           (setq to-newsgroup default))
10042       (unless to-newsgroup
10043         (error "No group name entered"))
10044       (or (gnus-active to-newsgroup)
10045           (gnus-activate-group to-newsgroup nil nil to-method)
10046           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10047                                      to-newsgroup))
10048               (or (and (gnus-request-create-group to-newsgroup to-method)
10049                        (gnus-activate-group
10050                         to-newsgroup nil nil to-method)
10051                        (gnus-subscribe-group to-newsgroup))
10052                   (error "Couldn't create group %s" to-newsgroup)))
10053           (error "No such group: %s" to-newsgroup)))
10054     to-newsgroup))
10055
10056 (defun gnus-summary-save-parts (type dir n &optional reverse)
10057   "Save parts matching TYPE to DIR.
10058 If REVERSE, save parts that do not match TYPE."
10059   (interactive
10060    (list (read-string "Save parts of type: "
10061                       (or (car gnus-summary-save-parts-type-history)
10062                           gnus-summary-save-parts-default-mime)
10063                       'gnus-summary-save-parts-type-history)
10064          (setq gnus-summary-save-parts-last-directory
10065                (read-file-name "Save to directory: "
10066                                gnus-summary-save-parts-last-directory
10067                                nil t))
10068          current-prefix-arg))
10069   (gnus-summary-iterate n
10070     (let ((gnus-display-mime-function nil)
10071           (gnus-inhibit-treatment t))
10072       (gnus-summary-select-article))
10073     (save-excursion
10074       (set-buffer gnus-article-buffer)
10075       (let ((handles (or gnus-article-mime-handles
10076                          (mm-dissect-buffer) (mm-uu-dissect))))
10077         (when handles
10078           (gnus-summary-save-parts-1 type dir handles reverse)
10079           (unless gnus-article-mime-handles ;; Don't destroy this case.
10080             (mm-destroy-parts handles)))))))
10081
10082 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10083   (if (stringp (car handle))
10084       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10085               (cdr handle))
10086     (when (if reverse
10087               (not (string-match type (mm-handle-media-type handle)))
10088             (string-match type (mm-handle-media-type handle)))
10089       (let ((file (expand-file-name
10090                    (file-name-nondirectory
10091                     (or
10092                      (mail-content-type-get
10093                       (mm-handle-disposition handle) 'filename)
10094                      (concat gnus-newsgroup-name
10095                              "." (number-to-string
10096                                   (cdr gnus-article-current)))))
10097                    dir)))
10098         (unless (file-exists-p file)
10099           (mm-save-part-to-file handle file))))))
10100
10101 ;; Summary extract commands
10102
10103 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10104   (let ((buffer-read-only nil)
10105         (article (gnus-summary-article-number))
10106         after-article b e)
10107     (unless (gnus-summary-goto-subject article)
10108       (error "No such article: %d" article))
10109     (gnus-summary-position-point)
10110     ;; If all commands are to be bunched up on one line, we collect
10111     ;; them here.
10112     (unless gnus-view-pseudos-separately
10113       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10114             files action)
10115         (while ps
10116           (setq action (cdr (assq 'action (car ps))))
10117           (setq files (list (cdr (assq 'name (car ps)))))
10118           (while (and ps (cdr ps)
10119                       (string= (or action "1")
10120                                (or (cdr (assq 'action (cadr ps))) "2")))
10121             (push (cdr (assq 'name (cadr ps))) files)
10122             (setcdr ps (cddr ps)))
10123           (when files
10124             (when (not (string-match "%s" action))
10125               (push " " files))
10126             (push " " files)
10127             (when (assq 'execute (car ps))
10128               (setcdr (assq 'execute (car ps))
10129                       (funcall (if (string-match "%s" action)
10130                                    'format 'concat)
10131                                action
10132                                (mapconcat
10133                                 (lambda (f)
10134                                   (if (equal f " ")
10135                                       f
10136                                     (gnus-quote-arg-for-sh-or-csh f)))
10137                                 files " ")))))
10138           (setq ps (cdr ps)))))
10139     (if (and gnus-view-pseudos (not not-view))
10140         (while pslist
10141           (when (assq 'execute (car pslist))
10142             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10143                                   (eq gnus-view-pseudos 'not-confirm)))
10144           (setq pslist (cdr pslist)))
10145       (save-excursion
10146         (while pslist
10147           (setq after-article (or (cdr (assq 'article (car pslist)))
10148                                   (gnus-summary-article-number)))
10149           (gnus-summary-goto-subject after-article)
10150           (forward-line 1)
10151           (setq b (point))
10152           (insert "    " (file-name-nondirectory
10153                           (cdr (assq 'name (car pslist))))
10154                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10155           (setq e (point))
10156           (forward-line -1)             ; back to `b'
10157           (gnus-add-text-properties
10158            b (1- e) (list 'gnus-number gnus-reffed-article-number
10159                           gnus-mouse-face-prop gnus-mouse-face))
10160           (gnus-data-enter
10161            after-article gnus-reffed-article-number
10162            gnus-unread-mark b (car pslist) 0 (- e b))
10163           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10164           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10165           (setq pslist (cdr pslist)))))))
10166
10167 (defun gnus-pseudos< (p1 p2)
10168   (let ((c1 (cdr (assq 'action p1)))
10169         (c2 (cdr (assq 'action p2))))
10170     (and c1 c2 (string< c1 c2))))
10171
10172 (defun gnus-request-pseudo-article (props)
10173   (cond ((assq 'execute props)
10174          (gnus-execute-command (cdr (assq 'execute props)))))
10175   (let ((gnus-current-article (gnus-summary-article-number)))
10176     (gnus-run-hooks 'gnus-mark-article-hook)))
10177
10178 (defun gnus-execute-command (command &optional automatic)
10179   (save-excursion
10180     (gnus-article-setup-buffer)
10181     (set-buffer gnus-article-buffer)
10182     (setq buffer-read-only nil)
10183     (let ((command (if automatic command
10184                      (read-string "Command: " (cons command 0)))))
10185       (erase-buffer)
10186       (insert "$ " command "\n\n")
10187       (if gnus-view-pseudo-asynchronously
10188           (start-process "gnus-execute" (current-buffer) shell-file-name
10189                          shell-command-switch command)
10190         (call-process shell-file-name nil t nil
10191                       shell-command-switch command)))))
10192
10193 ;; Summary kill commands.
10194
10195 (defun gnus-summary-edit-global-kill (article)
10196   "Edit the \"global\" kill file."
10197   (interactive (list (gnus-summary-article-number)))
10198   (gnus-group-edit-global-kill article))
10199
10200 (defun gnus-summary-edit-local-kill ()
10201   "Edit a local kill file applied to the current newsgroup."
10202   (interactive)
10203   (setq gnus-current-headers (gnus-summary-article-header))
10204   (gnus-group-edit-local-kill
10205    (gnus-summary-article-number) gnus-newsgroup-name))
10206
10207 ;;; Header reading.
10208
10209 (defun gnus-read-header (id &optional header)
10210   "Read the headers of article ID and enter them into the Gnus system."
10211   (let ((group gnus-newsgroup-name)
10212         (gnus-override-method
10213          (or
10214           gnus-override-method
10215           (and (gnus-news-group-p gnus-newsgroup-name)
10216                (car (gnus-refer-article-methods)))))
10217         where)
10218     ;; First we check to see whether the header in question is already
10219     ;; fetched.
10220     (if (stringp id)
10221         ;; This is a Message-ID.
10222         (setq header (or header (gnus-id-to-header id)))
10223       ;; This is an article number.
10224       (setq header (or header (gnus-summary-article-header id))))
10225     (if (and header
10226              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10227         ;; We have found the header.
10228         header
10229       ;; If this is a sparse article, we have to nix out its
10230       ;; previous entry in the thread hashtb.
10231       (when (and header
10232                  (gnus-summary-article-sparse-p (mail-header-number header)))
10233         (let* ((parent (gnus-parent-id (mail-header-references header)))
10234                (thread (and parent (gnus-id-to-thread parent))))
10235           (when thread
10236             (delq (assq header thread) thread))))
10237       ;; We have to really fetch the header to this article.
10238       (save-excursion
10239         (set-buffer nntp-server-buffer)
10240         (when (setq where (gnus-request-head id group))
10241           (nnheader-fold-continuation-lines)
10242           (goto-char (point-max))
10243           (insert ".\n")
10244           (goto-char (point-min))
10245           (insert "211 ")
10246           (princ (cond
10247                   ((numberp id) id)
10248                   ((cdr where) (cdr where))
10249                   (header (mail-header-number header))
10250                   (t gnus-reffed-article-number))
10251                  (current-buffer))
10252           (insert " Article retrieved.\n"))
10253         (if (or (not where)
10254                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10255             ()                          ; Malformed head.
10256           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10257             (when (and (stringp id)
10258                        (not (string= (gnus-group-real-name group)
10259                                      (car where))))
10260               ;; If we fetched by Message-ID and the article came
10261               ;; from a different group, we fudge some bogus article
10262               ;; numbers for this article.
10263               (mail-header-set-number header gnus-reffed-article-number))
10264             (save-excursion
10265               (set-buffer gnus-summary-buffer)
10266               (decf gnus-reffed-article-number)
10267               (gnus-remove-header (mail-header-number header))
10268               (push header gnus-newsgroup-headers)
10269               (setq gnus-current-headers header)
10270               (push (mail-header-number header) gnus-newsgroup-limit)))
10271           header)))))
10272
10273 (defun gnus-remove-header (number)
10274   "Remove header NUMBER from `gnus-newsgroup-headers'."
10275   (if (and gnus-newsgroup-headers
10276            (= number (mail-header-number (car gnus-newsgroup-headers))))
10277       (pop gnus-newsgroup-headers)
10278     (let ((headers gnus-newsgroup-headers))
10279       (while (and (cdr headers)
10280                   (not (= number (mail-header-number (cadr headers)))))
10281         (pop headers))
10282       (when (cdr headers)
10283         (setcdr headers (cddr headers))))))
10284
10285 ;;;
10286 ;;; summary highlights
10287 ;;;
10288
10289 (defun gnus-highlight-selected-summary ()
10290   "Highlight selected article in summary buffer."
10291   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10292   (when gnus-summary-selected-face
10293     (save-excursion
10294       (let* ((beg (progn (beginning-of-line) (point)))
10295              (end (progn (end-of-line) (point)))
10296              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10297              (from (if (get-text-property beg gnus-mouse-face-prop)
10298                        beg
10299                      (or (next-single-property-change
10300                           beg gnus-mouse-face-prop nil end)
10301                          beg)))
10302              (to
10303               (if (= from end)
10304                   (- from 2)
10305                 (or (next-single-property-change
10306                      from gnus-mouse-face-prop nil end)
10307                     end))))
10308         ;; If no mouse-face prop on line we will have to = from = end,
10309         ;; so we highlight the entire line instead.
10310         (when (= (+ to 2) from)
10311           (setq from beg)
10312           (setq to end))
10313         (if gnus-newsgroup-selected-overlay
10314             ;; Move old overlay.
10315             (gnus-move-overlay
10316              gnus-newsgroup-selected-overlay from to (current-buffer))
10317           ;; Create new overlay.
10318           (gnus-overlay-put
10319            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10320            'face gnus-summary-selected-face))))))
10321
10322 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10323 (defun gnus-summary-highlight-line ()
10324   "Highlight current line according to `gnus-summary-highlight'."
10325   (let* ((list gnus-summary-highlight)
10326          (p (point))
10327          (end (progn (end-of-line) (point)))
10328          ;; now find out where the line starts and leave point there.
10329          (beg (progn (beginning-of-line) (point)))
10330          (article (gnus-summary-article-number))
10331          (score (or (cdr (assq (or article gnus-current-article)
10332                                gnus-newsgroup-scored))
10333                     gnus-summary-default-score 0))
10334          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10335          (inhibit-read-only t))
10336     ;; Eval the cars of the lists until we find a match.
10337     (let ((default gnus-summary-default-score)
10338           (default-high gnus-summary-default-high-score)
10339           (default-low gnus-summary-default-low-score))
10340       (while (and list
10341                   (not (eval (caar list))))
10342         (setq list (cdr list))))
10343     (let ((face (cdar list)))
10344       (unless (eq face (get-text-property beg 'face))
10345         (gnus-put-text-property-excluding-characters-with-faces
10346          beg end 'face
10347          (setq face (if (boundp face) (symbol-value face) face)))
10348         (when gnus-summary-highlight-line-function
10349           (funcall gnus-summary-highlight-line-function article face))))
10350     (goto-char p)))
10351
10352 (defun gnus-update-read-articles (group unread &optional compute)
10353   "Update the list of read articles in GROUP."
10354   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10355          (entry (gnus-gethash group gnus-newsrc-hashtb))
10356          (info (nth 2 entry))
10357          (prev 1)
10358          (unread (sort (copy-sequence unread) '<))
10359          read)
10360     (if (or (not info) (not active))
10361         ;; There is no info on this group if it was, in fact,
10362         ;; killed.  Gnus stores no information on killed groups, so
10363         ;; there's nothing to be done.
10364         ;; One could store the information somewhere temporarily,
10365         ;; perhaps...  Hmmm...
10366         ()
10367       ;; Remove any negative articles numbers.
10368       (while (and unread (< (car unread) 0))
10369         (setq unread (cdr unread)))
10370       ;; Remove any expired article numbers
10371       (while (and unread (< (car unread) (car active)))
10372         (setq unread (cdr unread)))
10373       ;; Compute the ranges of read articles by looking at the list of
10374       ;; unread articles.
10375       (while unread
10376         (when (/= (car unread) prev)
10377           (push (if (= prev (1- (car unread))) prev
10378                   (cons prev (1- (car unread))))
10379                 read))
10380         (setq prev (1+ (car unread)))
10381         (setq unread (cdr unread)))
10382       (when (<= prev (cdr active))
10383         (push (cons prev (cdr active)) read))
10384       (setq read (if (> (length read) 1) (nreverse read) read))
10385       (if compute
10386           read
10387         (save-excursion
10388           (let (setmarkundo)
10389             ;; Propagate the read marks to the backend.
10390             (when (gnus-check-backend-function 'request-set-mark group)
10391               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10392                     (add (gnus-remove-from-range read (gnus-info-read info))))
10393                 (when (or add del)
10394                   (unless (gnus-check-group group)
10395                     (error "Can't open server for %s" group))
10396                   (gnus-request-set-mark
10397                    group (delq nil (list (if add (list add 'add '(read)))
10398                                          (if del (list del 'del '(read))))))
10399                   (setq setmarkundo
10400                         `(gnus-request-set-mark
10401                           ,group
10402                           ',(delq nil (list
10403                                        (if del (list del 'add '(read)))
10404                                        (if add (list add 'del '(read))))))))))
10405             (set-buffer gnus-group-buffer)
10406             (gnus-undo-register
10407               `(progn
10408                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10409                  (gnus-info-set-read ',info ',(gnus-info-read info))
10410                  (gnus-get-unread-articles-in-group ',info
10411                                                     (gnus-active ,group))
10412                  (gnus-group-update-group ,group t)
10413                  ,setmarkundo))))
10414         ;; Enter this list into the group info.
10415         (gnus-info-set-read info read)
10416         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10417         (gnus-get-unread-articles-in-group info (gnus-active group))
10418         t))))
10419
10420 (defun gnus-offer-save-summaries ()
10421   "Offer to save all active summary buffers."
10422   (let (buffers)
10423     ;; Go through all buffers and find all summaries.
10424     (dolist (buffer (buffer-list))
10425       (when (and (setq buffer (buffer-name buffer))
10426                  (string-match "Summary" buffer)
10427                  (save-excursion
10428                    (set-buffer buffer)
10429                    ;; We check that this is, indeed, a summary buffer.
10430                    (and (eq major-mode 'gnus-summary-mode)
10431                         ;; Also make sure this isn't bogus.
10432                         gnus-newsgroup-prepared
10433                         ;; Also make sure that this isn't a
10434                         ;; dead summary buffer.
10435                         (not gnus-dead-summary-mode))))
10436         (push buffer buffers)))
10437     ;; Go through all these summary buffers and offer to save them.
10438     (when buffers
10439       (save-excursion
10440         (map-y-or-n-p
10441          "Update summary buffer %s? "
10442          (lambda (buf)
10443            (switch-to-buffer buf)
10444            (gnus-summary-exit))
10445          buffers)))))
10446
10447
10448 ;;; @ for mime-partial
10449 ;;;
10450
10451 (defun gnus-request-partial-message ()
10452   (save-excursion
10453     (let ((number (gnus-summary-article-number))
10454           (group gnus-newsgroup-name)
10455           (mother gnus-article-buffer))
10456       (set-buffer (get-buffer-create " *Partial Article*"))
10457       (erase-buffer)
10458       (setq mime-preview-buffer mother)
10459       (gnus-request-article-this-buffer number group)
10460       (mime-parse-buffer)
10461       )))
10462
10463 (autoload 'mime-combine-message/partial-pieces-automatically
10464   "mime-partial"
10465   "Internal method to combine message/partial messages automatically.")
10466
10467 (mime-add-condition
10468  'action '((type . message)(subtype . partial)
10469            (major-mode . gnus-original-article-mode)
10470            (method . mime-combine-message/partial-pieces-automatically)
10471            (summary-buffer-exp . gnus-summary-buffer)
10472            (request-partial-message-method . gnus-request-partial-message)
10473            ))
10474
10475
10476 ;;; @ for message/rfc822
10477 ;;;
10478
10479 (defun gnus-mime-extract-message/rfc822 (entity situation)
10480   (let (group article num cwin swin cur)
10481     (with-temp-buffer
10482       (mime-insert-entity-content entity)
10483       (setq group (or (cdr (assq 'group situation))
10484                       (completing-read "Group: "
10485                                        gnus-active-hashtb
10486                                        nil
10487                                        (gnus-read-active-file-p)
10488                                        gnus-newsgroup-name))
10489             article (gnus-request-accept-article group)))
10490     (when (and (consp article)
10491                (numberp (setq article (cdr article))))
10492       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10493             cwin (get-buffer-window (current-buffer) t))
10494       (save-window-excursion
10495         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10496             (select-window swin)
10497           (set-buffer gnus-summary-buffer))
10498         (setq cur gnus-current-article)
10499         (forward-line num)
10500         (let (gnus-show-threads)
10501           (gnus-summary-goto-subject article t))
10502         (gnus-summary-clear-mark-forward 1)
10503         (gnus-summary-goto-subject cur))
10504       (when (and cwin (window-frame cwin))
10505         (select-frame (window-frame cwin)))
10506       (when (boundp 'mime-acting-situation-to-override)
10507         (set-alist 'mime-acting-situation-to-override
10508                    'group
10509                    group)
10510         (set-alist 'mime-acting-situation-to-override
10511                    'after-method
10512                    `(progn
10513                       (save-current-buffer
10514                         (set-buffer gnus-group-buffer)
10515                         (gnus-activate-group ,group))
10516                       (gnus-summary-goto-article ,cur
10517                                                  gnus-show-all-headers)))
10518         (set-alist 'mime-acting-situation-to-override
10519                    'number num)))))
10520
10521 (mime-add-condition
10522  'action '((type . message)(subtype . rfc822)
10523            (major-mode . gnus-original-article-mode)
10524            (method . gnus-mime-extract-message/rfc822)
10525            (mode . "extract")
10526            ))
10527
10528 (mime-add-condition
10529  'action '((type . message)(subtype . news)
10530            (major-mode . gnus-original-article-mode)
10531            (method . gnus-mime-extract-message/rfc822)
10532            (mode . "extract")
10533            ))
10534
10535 (defun gnus-mime-extract-multipart (entity situation)
10536   (let ((children (mime-entity-children entity))
10537         mime-acting-situation-to-override
10538         f)
10539     (while children
10540       (mime-play-entity (car children)
10541                         (cons (assq 'mode situation)
10542                               mime-acting-situation-to-override))
10543       (setq children (cdr children)))
10544     (if (setq f (cdr (assq 'after-method
10545                            mime-acting-situation-to-override)))
10546         (eval f)
10547       )))
10548
10549 (mime-add-condition
10550  'action '((type . multipart)
10551            (method . gnus-mime-extract-multipart)
10552            (mode . "extract")
10553            )
10554  'with-default)
10555
10556
10557 ;;; @ end
10558 ;;;
10559
10560 (defun gnus-summary-setup-default-charset ()
10561   "Setup newsgroup default charset."
10562   (if (equal gnus-newsgroup-name "nndraft:drafts")
10563       (setq gnus-newsgroup-charset nil)
10564     (let* ((ignored-charsets
10565             (or gnus-newsgroup-ephemeral-ignored-charsets
10566                 (append
10567                  (and gnus-newsgroup-name
10568                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10569                  gnus-newsgroup-ignored-charsets))))
10570       (setq gnus-newsgroup-charset
10571             (or gnus-newsgroup-ephemeral-charset
10572                 (and gnus-newsgroup-name
10573                      (gnus-parameter-charset gnus-newsgroup-name))
10574                 gnus-default-charset))
10575       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10576            ignored-charsets))))
10577
10578 ;;;
10579 ;;; Mime Commands
10580 ;;;
10581
10582 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10583   "Display the current article buffer fully MIME-buttonized.
10584 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10585 treated as multipart/mixed."
10586   (interactive "P")
10587   (require 'gnus-art)
10588   (let ((gnus-unbuttonized-mime-types nil)
10589         (gnus-mime-display-multipart-as-mixed show-all-parts))
10590     (gnus-summary-show-article)))
10591
10592 (defun gnus-summary-repair-multipart (article)
10593   "Add a Content-Type header to a multipart article without one."
10594   (interactive (list (gnus-summary-article-number)))
10595   (gnus-with-article article
10596     (message-narrow-to-head)
10597     (message-remove-header "Mime-Version")
10598     (goto-char (point-max))
10599     (insert "Mime-Version: 1.0\n")
10600     (widen)
10601     (when (search-forward "\n--" nil t)
10602       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10603         (message-narrow-to-head)
10604         (message-remove-header "Content-Type")
10605         (goto-char (point-max))
10606         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10607                         separator))
10608         (widen))))
10609   (let (gnus-mark-article-hook)
10610     (gnus-summary-select-article t t nil article)))
10611
10612 (defun gnus-summary-toggle-display-buttonized ()
10613   "Toggle the buttonizing of the article buffer."
10614   (interactive)
10615   (require 'gnus-art)
10616   (if (setq gnus-inhibit-mime-unbuttonizing
10617             (not gnus-inhibit-mime-unbuttonizing))
10618       (let ((gnus-unbuttonized-mime-types nil))
10619         (gnus-summary-show-article))
10620     (gnus-summary-show-article)))
10621
10622 ;;;
10623 ;;; Intelli-mouse commmands
10624 ;;;
10625
10626 (defun gnus-wheel-summary-scroll (event)
10627   (interactive "e")
10628   (let ((amount (if (memq 'shift (event-modifiers event))
10629                     (car gnus-wheel-scroll-amount)
10630                   (cdr gnus-wheel-scroll-amount)))
10631         (direction (- (* (static-if (featurep 'xemacs)
10632                              (event-button event)
10633                            (cond ((eq 'mouse-4 (event-basic-type event))
10634                                   4)
10635                                  ((eq 'mouse-5 (event-basic-type event))
10636                                   5)))
10637                          2) 9))
10638         edge)
10639     (gnus-summary-scroll-up (* amount direction))
10640     (when (gnus-eval-in-buffer-window gnus-article-buffer
10641             (save-restriction
10642               (widen)
10643               (and (if (< 0 direction)
10644                        (gnus-article-next-page 0)
10645                      (gnus-article-prev-page 0)
10646                      (bobp))
10647                    (if (setq edge (get-text-property
10648                                    (point-min) 'gnus-wheel-edge))
10649                        (setq edge (* edge direction))
10650                      (setq edge -1))
10651                    (or (plusp edge)
10652                        (let ((buffer-read-only nil)
10653                              (inhibit-read-only t))
10654                          (put-text-property (point-min) (point-max)
10655                                             'gnus-wheel-edge direction)
10656                          nil))
10657                    (or (> edge gnus-wheel-edge-resistance)
10658                        (let ((buffer-read-only nil)
10659                              (inhibit-read-only t))
10660                          (put-text-property (point-min) (point-max)
10661                                             'gnus-wheel-edge
10662                                             (* (1+ edge) direction))
10663                          nil))
10664                    (eq last-command 'gnus-wheel-summary-scroll))))
10665       (gnus-summary-next-article nil nil (minusp direction)))))
10666
10667 (defun gnus-wheel-install ()
10668   "Enable mouse wheel support on summary window."
10669   (when gnus-use-wheel
10670     (let ((keys
10671            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10672       (dolist (key keys)
10673         (define-key gnus-summary-mode-map key
10674           'gnus-wheel-summary-scroll)))))
10675
10676 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10677
10678 ;;;
10679 ;;; Traditional PGP commmands
10680 ;;;
10681
10682 (defun gnus-summary-decrypt-article (&optional force)
10683   "Decrypt the current article in traditional PGP way.
10684 This will have permanent effect only in mail groups.
10685 If FORCE is non-nil, allow editing of articles even in read-only
10686 groups."
10687   (interactive "P")
10688   (gnus-summary-select-article t)
10689   (gnus-eval-in-buffer-window gnus-article-buffer
10690     (save-excursion
10691       (save-restriction
10692         (widen)
10693         (goto-char (point-min))
10694         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10695           (error "Not a traditional PGP message!"))
10696         (let ((armor-start (match-beginning 0)))
10697           (if (and (pgg-decrypt-region armor-start (point-max))
10698                    (or force (not (gnus-group-read-only-p))))
10699               (let ((inhibit-read-only t)
10700                     buffer-read-only)
10701                 (delete-region armor-start
10702                                (progn
10703                                  (re-search-forward "^-+END PGP" nil t)
10704                                  (beginning-of-line 2)
10705                                  (point)))
10706                 (insert-buffer-substring pgg-output-buffer))))))))
10707
10708 (defun gnus-summary-verify-article ()
10709   "Verify the current article in traditional PGP way."
10710   (interactive)
10711   (save-excursion
10712     (set-buffer gnus-original-article-buffer)
10713     (goto-char (point-min))
10714     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10715       (error "Not a traditional PGP message!"))
10716     (re-search-forward "^-+END PGP" nil t)
10717     (beginning-of-line 2)
10718     (call-interactively (function pgg-verify-region))))
10719
10720 ;;;
10721 ;;; Generic summary marking commands
10722 ;;;
10723
10724 (defvar gnus-summary-marking-alist
10725   '((read gnus-del-mark "d")
10726     (unread gnus-unread-mark "u")
10727     (ticked gnus-ticked-mark "!")
10728     (dormant gnus-dormant-mark "?")
10729     (expirable gnus-expirable-mark "e"))
10730   "An alist of names/marks/keystrokes.")
10731
10732 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10733 (defvar gnus-summary-mark-map)
10734
10735 (defun gnus-summary-make-all-marking-commands ()
10736   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10737   (dolist (elem gnus-summary-marking-alist)
10738     (apply 'gnus-summary-make-marking-command elem)))
10739
10740 (defun gnus-summary-make-marking-command (name mark keystroke)
10741   (let ((map (make-sparse-keymap)))
10742     (define-key gnus-summary-generic-mark-map keystroke map)
10743     (dolist (lway `((next "next" next nil "n")
10744                     (next-unread "next unread" next t "N")
10745                     (prev "previous" prev nil "p")
10746                     (prev-unread "previous unread" prev t "P")
10747                     (nomove "" nil nil ,keystroke)))
10748       (let ((func (gnus-summary-make-marking-command-1
10749                    mark (car lway) lway name)))
10750         (setq func (eval func))
10751         (define-key map (nth 4 lway) func)))))
10752
10753 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10754   `(defun ,(intern
10755             (format "gnus-summary-put-mark-as-%s%s"
10756                     name (if (eq way 'nomove)
10757                              ""
10758                            (concat "-" (symbol-name way)))))
10759      (n)
10760      ,(format
10761        "Mark the current article as %s%s.
10762 If N, the prefix, then repeat N times.
10763 If N is negative, move in reverse order.
10764 The difference between N and the actual number of articles marked is
10765 returned."
10766        name (car (cdr lway)))
10767      (interactive "p")
10768      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10769
10770 (defun gnus-summary-generic-mark (n mark move unread)
10771   "Mark N articles with MARK."
10772   (unless (eq major-mode 'gnus-summary-mode)
10773     (error "This command can only be used in the summary buffer"))
10774   (gnus-summary-show-thread)
10775   (let ((nummove
10776          (cond
10777           ((eq move 'next) 1)
10778           ((eq move 'prev) -1)
10779           (t 0))))
10780     (if (zerop nummove)
10781         (setq n 1)
10782       (when (< n 0)
10783         (setq n (abs n)
10784               nummove (* -1 nummove))))
10785     (while (and (> n 0)
10786                 (gnus-summary-mark-article nil mark)
10787                 (zerop (gnus-summary-next-subject nummove unread t)))
10788       (setq n (1- n)))
10789     (when (/= 0 n)
10790       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10791     (gnus-summary-recenter)
10792     (gnus-summary-position-point)
10793     (gnus-set-mode-line 'summary)
10794     n))
10795
10796 (defun gnus-summary-insert-articles (articles)
10797   (when (setq articles
10798               (gnus-set-difference articles
10799                                    (mapcar (lambda (h) (mail-header-number h))
10800                                            gnus-newsgroup-headers)))
10801     (setq gnus-newsgroup-headers
10802           (merge 'list
10803                  gnus-newsgroup-headers
10804                  (gnus-fetch-headers articles)
10805                  'gnus-article-sort-by-number))
10806     ;; Suppress duplicates?
10807     (when gnus-suppress-duplicates
10808       (gnus-dup-suppress-articles))
10809
10810     ;; We might want to build some more threads first.
10811     (when (and gnus-fetch-old-headers
10812                (eq gnus-headers-retrieved-by 'nov))
10813       (if (eq gnus-fetch-old-headers 'invisible)
10814           (gnus-build-all-threads)
10815         (gnus-build-old-threads)))
10816     ;; Let the Gnus agent mark articles as read.
10817     (when gnus-agent
10818       (gnus-agent-get-undownloaded-list))
10819     ;; Remove list identifiers from subject
10820     (when gnus-list-identifiers
10821       (gnus-summary-remove-list-identifiers))
10822     ;; First and last article in this newsgroup.
10823     (when gnus-newsgroup-headers
10824       (setq gnus-newsgroup-begin
10825             (mail-header-number (car gnus-newsgroup-headers))
10826             gnus-newsgroup-end
10827             (mail-header-number
10828              (gnus-last-element gnus-newsgroup-headers))))
10829     (when gnus-use-scoring
10830       (gnus-possibly-score-headers))))
10831
10832 (defun gnus-summary-insert-old-articles (&optional all)
10833   "Insert all old articles in this group.
10834 If ALL is non-nil, already read articles become readable.
10835 If ALL is a number, fetch this number of articles."
10836   (interactive "P")
10837   (prog1
10838       (let ((old (mapcar 'car gnus-newsgroup-data))
10839             (i (car gnus-newsgroup-active))
10840             older len)
10841         (while (<= i (cdr gnus-newsgroup-active))
10842           (or (memq i old) (push i older))
10843           (incf i))
10844         (setq len (length older))
10845         (cond
10846          ((null older) nil)
10847          ((numberp all)
10848           (if (< all len)
10849               (setq older (subseq older 0 all))))
10850          (all nil)
10851          (t
10852           (if (and (numberp gnus-large-newsgroup)
10853                    (> len gnus-large-newsgroup))
10854               (let ((input
10855                      (read-string
10856                       (format
10857                        "How many articles from %s (default %d): "
10858                        (gnus-limit-string
10859                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10860                        len))))
10861                 (unless (string-match "^[ \t]*$" input)
10862                   (setq all (string-to-number input))
10863                   (if (< all len)
10864                       (setq older (subseq older 0 all))))))))
10865         (if (not older)
10866             (message "No old news.")
10867           (gnus-summary-insert-articles older)
10868           (gnus-summary-limit (gnus-union older old))))
10869     (gnus-summary-position-point)))
10870
10871 (defun gnus-summary-insert-new-articles ()
10872   "Insert all new articles in this group."
10873   (interactive)
10874   (prog1
10875       (let ((old (mapcar 'car gnus-newsgroup-data))
10876             (old-active gnus-newsgroup-active)
10877             (nnmail-fetched-sources (list t))
10878             i new)
10879         (setq gnus-newsgroup-active
10880               (gnus-activate-group gnus-newsgroup-name 'scan))
10881         (setq i (1+ (cdr old-active)))
10882         (while (<= i (cdr gnus-newsgroup-active))
10883           (push i new)
10884           (incf i))
10885         (if (not new)
10886             (message "No gnus is bad news.")
10887           (setq new (nreverse new))
10888           (gnus-summary-insert-articles new)
10889           (setq gnus-newsgroup-unreads
10890                 (append gnus-newsgroup-unreads new))
10891           (gnus-summary-limit (gnus-union old new))))
10892     (gnus-summary-position-point)))
10893
10894 (gnus-summary-make-all-marking-commands)
10895
10896 (gnus-ems-redefine)
10897
10898 (provide 'gnus-sum)
10899
10900 (run-hooks 'gnus-sum-load-hook)
10901
10902 ;;; gnus-sum.el ends here