Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 (require 'nnoo)
42 (require 'mime-view)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify" 
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
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:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-killed t
260   "*If non-nil, hide killed threads automatically."
261   :group 'gnus-thread
262   :type 'boolean)
263
264 (defcustom gnus-thread-ignore-subject t
265   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
266 If nil, articles that have different subjects from their parents will
267 start separate threads."
268   :group 'gnus-thread
269   :type 'boolean)
270
271 (defcustom gnus-thread-operation-ignore-subject t
272   "*If non-nil, subjects will be ignored when doing thread commands.
273 This affects commands like `gnus-summary-kill-thread' and
274 `gnus-summary-lower-thread'.
275
276 If this variable is nil, articles in the same thread with different
277 subjects will not be included in the operation in question.  If this
278 variable is `fuzzy', only articles that have subjects that are fuzzily
279 equal will be included."
280   :group 'gnus-thread
281   :type '(choice (const :tag "off" nil)
282                  (const fuzzy)
283                  (sexp :tag "on" t)))
284
285 (defcustom gnus-thread-indent-level 4
286   "*Number that says how much each sub-thread should be indented."
287   :group 'gnus-thread
288   :type 'integer)
289
290 (defcustom gnus-auto-extend-newsgroup t
291   "*If non-nil, extend newsgroup forward and backward when requested."
292   :group 'gnus-summary-choose
293   :type 'boolean)
294
295 (defcustom gnus-auto-select-first t
296   "*If non-nil, select the article under point.
297 Which article this is is controlled by the `gnus-auto-select-subject'
298 variable.
299
300 If you want to prevent automatic selection of articles in some
301 newsgroups, set the variable to nil in `gnus-select-group-hook'."
302   :group 'gnus-group-select
303   :type '(choice (const :tag "none" nil)
304                  (sexp :menu-tag "first" t)))
305
306 (defcustom gnus-auto-select-subject 'unread
307   "*Says what subject to place under point when entering a group.
308
309 This variable can either be the symbols `first' (place point on the
310 first subject), `unread' (place point on the subject line of the first
311 unread article), `best' (place point on the subject line of the
312 higest-scored article), `unseen' (place point on the subject line of
313 the first unseen article), 'unseen-or-unread' (place point on the subject
314 line of the first unseen article or, if all article have been seen, on the
315 subject line of the first unread article), or a function to be called to
316 place point on some subject line."
317   :group 'gnus-group-select
318   :type '(choice (const best)
319                  (const unread)
320                  (const first)
321                  (const unseen)
322                  (const unseen-or-unread)))
323
324 (defcustom gnus-dont-select-after-jump-to-other-group nil
325   "If non-nil, don't select the first unread article after entering the
326 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
327 it is depend on the value of `gnus-auto-select-first' whether to select
328 or not."
329   :group 'gnus-group-select
330   :type 'boolean)
331
332 (defcustom gnus-auto-select-next t
333   "*If non-nil, offer to go to the next group from the end of the previous.
334 If the value is t and the next newsgroup is empty, Gnus will exit
335 summary mode and go back to group mode.  If the value is neither nil
336 nor t, Gnus will select the following unread newsgroup.  In
337 particular, if the value is the symbol `quietly', the next unread
338 newsgroup will be selected without any confirmation, and if it is
339 `almost-quietly', the next group will be selected without any
340 confirmation if you are located on the last article in the group.
341 Finally, if this variable is `slightly-quietly', the `Z n' command
342 will go to the next group without confirmation."
343   :group 'gnus-summary-maneuvering
344   :type '(choice (const :tag "off" nil)
345                  (const quietly)
346                  (const almost-quietly)
347                  (const slightly-quietly)
348                  (sexp :menu-tag "on" t)))
349
350 (defcustom gnus-auto-select-same nil
351   "*If non-nil, select the next article with the same subject.
352 If there are no more articles with the same subject, go to
353 the first unread article."
354   :group 'gnus-summary-maneuvering
355   :type 'boolean)
356
357 (defcustom gnus-summary-check-current nil
358   "*If non-nil, consider the current article when moving.
359 The \"unread\" movement commands will stay on the same line if the
360 current article is unread."
361   :group 'gnus-summary-maneuvering
362   :type 'boolean)
363
364 (defcustom gnus-auto-center-summary t
365   "*If non-nil, always center the current summary buffer.
366 In particular, if `vertical' do only vertical recentering.  If non-nil
367 and non-`vertical', do both horizontal and vertical recentering."
368   :group 'gnus-summary-maneuvering
369   :type '(choice (const :tag "none" nil)
370                  (const vertical)
371                  (integer :tag "height")
372                  (sexp :menu-tag "both" t)))
373
374 (defcustom gnus-show-all-headers nil
375   "*If non-nil, don't hide any headers."
376   :group 'gnus-article-hiding
377   :group 'gnus-article-headers
378   :type 'boolean)
379
380 (defcustom gnus-summary-ignore-duplicates nil
381   "*If non-nil, ignore articles with identical Message-ID headers."
382   :group 'gnus-summary
383   :type 'boolean)
384
385 (defcustom gnus-single-article-buffer t
386   "*If non-nil, display all articles in the same buffer.
387 If nil, each group will get its own article buffer."
388   :group 'gnus-article-various
389   :type 'boolean)
390
391 (defcustom gnus-break-pages t
392   "*If non-nil, do page breaking on articles.
393 The page delimiter is specified by the `gnus-page-delimiter'
394 variable."
395   :group 'gnus-article-various
396   :type 'boolean)
397
398 (defcustom gnus-show-mime t
399   "*If non-nil, do mime processing of articles.
400 The articles will simply be fed to the function given by
401 `gnus-article-display-method-for-mime'."
402   :group 'gnus-article-mime
403   :type 'boolean)
404
405 (defcustom gnus-move-split-methods nil
406   "*Variable used to suggest where articles are to be moved to.
407 It uses the same syntax as the `gnus-split-methods' variable.
408 However, whereas `gnus-split-methods' specifies file names as targets,
409 this variable specifies group names."
410   :group 'gnus-summary-mail
411   :type '(repeat (choice (list :value (fun) function)
412                          (cons :value ("" "") regexp (repeat string))
413                          (sexp :value nil))))
414
415 (defcustom gnus-unread-mark ?\ ;;;Whitespace
416   "*Mark used for unread articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-ticked-mark ?!
421   "*Mark used for ticked articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-dormant-mark ??
426   "*Mark used for dormant articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-del-mark ?r
431   "*Mark used for del'd articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-read-mark ?R
436   "*Mark used for read articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-expirable-mark ?E
441   "*Mark used for expirable articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-killed-mark ?K
446   "*Mark used for killed articles."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-spam-mark ?H
451   "*Mark used for spam articles."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-souped-mark ?F
456   "*Mark used for souped articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-kill-file-mark ?X
461   "*Mark used for articles killed by kill files."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-low-score-mark ?Y
466   "*Mark used for articles with a low score."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-catchup-mark ?C
471   "*Mark used for articles that are caught up."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-replied-mark ?A
476   "*Mark used for articles that have been replied to."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-forwarded-mark ?F
481   "*Mark used for articles that have been forwarded."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-recent-mark ?N
486   "*Mark used for articles that are recent."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-cached-mark ?*
491   "*Mark used for articles that are in the cache."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-saved-mark ?S
496   "*Mark used for articles that have been saved."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-unseen-mark ?.
501   "*Mark used for articles that haven't been seen."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-no-mark ?\ ;;;Whitespace
506   "*Mark used for articles that have no other secondary mark."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-ancient-mark ?O
511   "*Mark used for ancient articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-sparse-mark ?Q
516   "*Mark used for sparsely reffed articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-canceled-mark ?G
521   "*Mark used for canceled articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-duplicate-mark ?M
526   "*Mark used for duplicate articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-undownloaded-mark ?@
531   "*Mark used for articles that weren't downloaded."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-downloadable-mark ?%
536   "*Mark used for articles that are to be downloaded."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-unsendable-mark ?=
541   "*Mark used for articles that won't be sent."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-score-over-mark ?+
546   "*Score mark used for articles with high scores."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-score-below-mark ?-
551   "*Score mark used for articles with low scores."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
556   "*There is no thread under the article."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-not-empty-thread-mark ?=
561   "*There is a thread under the article."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-view-pseudo-asynchronously nil
566   "*If non-nil, Gnus will view pseudo-articles asynchronously."
567   :group 'gnus-extract-view
568   :type 'boolean)
569
570 (defcustom gnus-auto-expirable-marks
571   (list gnus-spam-mark gnus-killed-mark gnus-del-mark gnus-catchup-mark
572         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
573         gnus-souped-mark gnus-duplicate-mark)
574   "*The list of marks converted into expiration if a group is auto-expirable."
575   :version "21.1"
576   :group 'gnus-summary
577   :type '(repeat character))
578
579 (defcustom gnus-inhibit-user-auto-expire t
580   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
581   :version "21.1"
582   :group 'gnus-summary
583   :type 'boolean)
584
585 (defcustom gnus-view-pseudos nil
586   "*If `automatic', pseudo-articles will be viewed automatically.
587 If `not-confirm', pseudos will be viewed automatically, and the user
588 will not be asked to confirm the command."
589   :group 'gnus-extract-view
590   :type '(choice (const :tag "off" nil)
591                  (const automatic)
592                  (const not-confirm)))
593
594 (defcustom gnus-view-pseudos-separately t
595   "*If non-nil, one pseudo-article will be created for each file to be viewed.
596 If nil, all files that use the same viewing command will be given as a
597 list of parameters to that command."
598   :group 'gnus-extract-view
599   :type 'boolean)
600
601 (defcustom gnus-insert-pseudo-articles t
602   "*If non-nil, insert pseudo-articles when decoding articles."
603   :group 'gnus-extract-view
604   :type 'boolean)
605
606 (defcustom gnus-summary-dummy-line-format
607   "  %(:                          :%) %S\n"
608   "*The format specification for the dummy roots in the summary buffer.
609 It works along the same lines as a normal formatting string,
610 with some simple extensions.
611
612 %S  The subject
613
614 General format specifiers can also be used.
615 See `(gnus)Formatting Variables'."
616   :link '(custom-manual "(gnus)Formatting Variables")
617   :group 'gnus-threading
618   :type 'string)
619
620 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
621   "*The format specification for the summary mode line.
622 It works along the same lines as a normal formatting string,
623 with some simple extensions:
624
625 %G  Group name
626 %p  Unprefixed group name
627 %A  Current article number
628 %z  Current article score
629 %V  Gnus version
630 %U  Number of unread articles in the group
631 %e  Number of unselected articles in the group
632 %Z  A string with unread/unselected article counts
633 %g  Shortish group name
634 %S  Subject of the current article
635 %u  User-defined spec
636 %s  Current score file name
637 %d  Number of dormant articles
638 %r  Number of articles that have been marked as read in this session
639 %E  Number of articles expunged by the score files"
640   :group 'gnus-summary-format
641   :type 'string)
642
643 (defcustom gnus-list-identifiers nil
644   "Regexp that matches list identifiers to be removed from subject.
645 This can also be a list of regexps."
646   :version "21.1"
647   :group 'gnus-summary-format
648   :group 'gnus-article-hiding
649   :type '(choice (const :tag "none" nil)
650                  (regexp :value ".*")
651                  (repeat :value (".*") regexp)))
652
653 (defcustom gnus-summary-mark-below 0
654   "*Mark all articles with a score below this variable as read.
655 This variable is local to each summary buffer and usually set by the
656 score file."
657   :group 'gnus-score-default
658   :type 'integer)
659
660 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
661   "*List of functions used for sorting articles in the summary buffer.
662
663 Each function takes two articles and returns non-nil if the first
664 article should be sorted before the other.  If you use more than one
665 function, the primary sort function should be the last.  You should
666 probably always include `gnus-article-sort-by-number' in the list of
667 sorting functions -- preferably first.  Also note that sorting by date
668 is often much slower than sorting by number, and the sorting order is
669 very similar.  (Sorting by date means sorting by the time the message
670 was sent, sorting by number means sorting by arrival time.)
671
672 Ready-made functions include `gnus-article-sort-by-number',
673 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
674 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
675
676 When threading is turned on, the variable `gnus-thread-sort-functions'
677 controls how articles are sorted."
678   :group 'gnus-summary-sort
679   :type '(repeat (choice (function-item gnus-article-sort-by-number)
680                          (function-item gnus-article-sort-by-author)
681                          (function-item gnus-article-sort-by-subject)
682                          (function-item gnus-article-sort-by-date)
683                          (function-item gnus-article-sort-by-score)
684                          (function :tag "other"))))
685
686 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
687   "*List of functions used for sorting threads in the summary buffer.
688 By default, threads are sorted by article number.
689
690 Each function takes two threads and returns non-nil if the first
691 thread should be sorted before the other.  If you use more than one
692 function, the primary sort function should be the last.  You should
693 probably always include `gnus-thread-sort-by-number' in the list of
694 sorting functions -- preferably first.  Also note that sorting by date
695 is often much slower than sorting by number, and the sorting order is
696 very similar.  (Sorting by date means sorting by the time the message
697 was sent, sorting by number means sorting by arrival time.)
698
699 Ready-made functions include `gnus-thread-sort-by-number',
700 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
701 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
702 `gnus-thread-sort-by-most-recent-number',
703 `gnus-thread-sort-by-most-recent-date', and
704 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
705
706 When threading is turned off, the variable
707 `gnus-article-sort-functions' controls how articles are sorted."
708   :group 'gnus-summary-sort
709   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
710                          (function-item gnus-thread-sort-by-author)
711                          (function-item gnus-thread-sort-by-subject)
712                          (function-item gnus-thread-sort-by-date)
713                          (function-item gnus-thread-sort-by-score)
714                          (function-item gnus-thread-sort-by-total-score)
715                          (function :tag "other"))))
716
717 (defcustom gnus-thread-score-function '+
718   "*Function used for calculating the total score of a thread.
719
720 The function is called with the scores of the article and each
721 subthread and should then return the score of the thread.
722
723 Some functions you can use are `+', `max', or `min'."
724   :group 'gnus-summary-sort
725   :type 'function)
726
727 (defcustom gnus-summary-expunge-below nil
728   "All articles that have a score less than this variable will be expunged.
729 This variable is local to the summary buffers."
730   :group 'gnus-score-default
731   :type '(choice (const :tag "off" nil)
732                  integer))
733
734 (defcustom gnus-thread-expunge-below nil
735   "All threads that have a total score less than this variable will be expunged.
736 See `gnus-thread-score-function' for en explanation of what a
737 \"thread score\" is.
738
739 This variable is local to the summary buffers."
740   :group 'gnus-threading
741   :group 'gnus-score-default
742   :type '(choice (const :tag "off" nil)
743                  integer))
744
745 (defcustom gnus-summary-mode-hook nil
746   "*A hook for Gnus summary mode.
747 This hook is run before any variables are set in the summary buffer."
748   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
749   :group 'gnus-summary-various
750   :type 'hook)
751
752 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
753 (when (featurep 'xemacs)
754   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
755   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
756   (add-hook 'gnus-summary-mode-hook
757             'gnus-xmas-switch-horizontal-scrollbar-off))
758
759 (defcustom gnus-summary-menu-hook nil
760   "*Hook run after the creation of the summary mode menu."
761   :group 'gnus-summary-visual
762   :type 'hook)
763
764 (defcustom gnus-summary-exit-hook nil
765   "*A hook called on exit from the summary buffer.
766 It will be called with point in the group buffer."
767   :group 'gnus-summary-exit
768   :type 'hook)
769
770 (defcustom gnus-summary-prepare-hook nil
771   "*A hook called after the summary buffer has been generated.
772 If you want to modify the summary buffer, you can use this hook."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-summary-prepared-hook nil
777   "*A hook called as the last thing after the summary buffer has been generated."
778   :group 'gnus-summary-various
779   :type 'hook)
780
781 (defcustom gnus-summary-generate-hook nil
782   "*A hook run just before generating the summary buffer.
783 This hook is commonly used to customize threading variables and the
784 like."
785   :group 'gnus-summary-various
786   :type 'hook)
787
788 (defcustom gnus-select-group-hook nil
789   "*A hook called when a newsgroup is selected.
790
791 If you'd like to simplify subjects like the
792 `gnus-summary-next-same-subject' command does, you can use the
793 following hook:
794
795  (add-hook gnus-select-group-hook
796            (lambda ()
797              (mapcar (lambda (header)
798                        (mail-header-set-subject
799                         header
800                         (gnus-simplify-subject
801                          (mail-header-subject header) 're-only)))
802                      gnus-newsgroup-headers)))"
803   :group 'gnus-group-select
804   :type 'hook)
805
806 (defcustom gnus-select-article-hook nil
807   "*A hook called when an article is selected."
808   :group 'gnus-summary-choose
809   :type 'hook)
810
811 (defcustom gnus-visual-mark-article-hook
812   (list 'gnus-highlight-selected-summary)
813   "*Hook run after selecting an article in the summary buffer.
814 It is meant to be used for highlighting the article in some way.  It
815 is not run if `gnus-visual' is nil."
816   :group 'gnus-summary-visual
817   :type 'hook)
818
819 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
820   "*A hook called before parsing the headers."
821   :group 'gnus-various
822   :type 'hook)
823
824 (defcustom gnus-exit-group-hook nil
825   "*A hook called when exiting summary mode.
826 This hook is not called from the non-updating exit commands like `Q'."
827   :group 'gnus-various
828   :type 'hook)
829
830 (defcustom gnus-summary-update-hook
831   (list 'gnus-summary-highlight-line)
832   "*A hook called when a summary line is changed.
833 The hook will not be called if `gnus-visual' is nil.
834
835 The default function `gnus-summary-highlight-line' will
836 highlight the line according to the `gnus-summary-highlight'
837 variable."
838   :group 'gnus-summary-visual
839   :type 'hook)
840
841 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
842   "*A hook called when an article is selected for the first time.
843 The hook is intended to mark an article as read (or unread)
844 automatically when it is selected."
845   :group 'gnus-summary-choose
846   :type 'hook)
847
848 (defcustom gnus-group-no-more-groups-hook nil
849   "*A hook run when returning to group mode having no more (unread) groups."
850   :group 'gnus-group-select
851   :type 'hook)
852
853 (defcustom gnus-ps-print-hook nil
854   "*A hook run before ps-printing something from Gnus."
855   :group 'gnus-summary
856   :type 'hook)
857
858 (defcustom gnus-summary-display-arrow
859   (and (fboundp 'display-graphic-p)
860        (display-graphic-p))
861   "*If non-nil, display an arrow highlighting the current article."
862   :version "21.1"
863   :group 'gnus-summary
864   :type 'boolean)
865
866 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
867   "Face used for highlighting the current article in the summary buffer."
868   :group 'gnus-summary-visual
869   :type 'face)
870
871 (defcustom gnus-summary-highlight
872   '(((eq mark gnus-canceled-mark)
873      . gnus-summary-cancelled-face)
874     ((and (> score default-high)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-high-ticked-face)
878     ((and (< score default-low)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-low-ticked-face)
882     ((or (eq mark gnus-dormant-mark)
883          (eq mark gnus-ticked-mark))
884      . gnus-summary-normal-ticked-face)
885     ((and (> score default-high) (eq mark gnus-ancient-mark))
886      . gnus-summary-high-ancient-face)
887     ((and (< score default-low) (eq mark gnus-ancient-mark))
888      . gnus-summary-low-ancient-face)
889     ((eq mark gnus-ancient-mark)
890      . gnus-summary-normal-ancient-face)
891     ((and (> score default-high) (eq mark gnus-unread-mark))
892      . gnus-summary-high-unread-face)
893     ((and (< score default-low) (eq mark gnus-unread-mark))
894      . gnus-summary-low-unread-face)
895     ((eq mark gnus-unread-mark)
896      . gnus-summary-normal-unread-face)
897     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
898                                                   gnus-undownloaded-mark)))
899      . gnus-summary-high-unread-face)
900     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
901                                                  gnus-undownloaded-mark)))
902      . gnus-summary-low-unread-face)
903     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
904           (memq article gnus-newsgroup-unreads))
905      . gnus-summary-normal-unread-face)
906     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
907      . gnus-summary-normal-read-face)
908     ((> score default-high)
909      . gnus-summary-high-read-face)
910     ((< score default-low)
911      . gnus-summary-low-read-face)
912     (t
913      . gnus-summary-normal-read-face))
914   "*Controls the highlighting of summary buffer lines.
915
916 A list of (FORM . FACE) pairs.  When deciding how a a particular
917 summary line should be displayed, each form is evaluated.  The content
918 of the face field after the first true form is used.  You can change
919 how those summary lines are displayed, by editing the face field.
920
921 You can use the following variables in the FORM field.
922
923 score:        The article's score
924 default:      The default article score.
925 default-high: The default score for high scored articles.
926 default-low:  The default score for low scored articles.
927 below:        The score below which articles are automatically marked as read.
928 mark:         The articles mark."
929   :group 'gnus-summary-visual
930   :type '(repeat (cons (sexp :tag "Form" nil)
931                        face)))
932
933 (defcustom gnus-alter-header-function nil
934   "Function called to allow alteration of article header structures.
935 The function is called with one parameter, the article header vector,
936 which it may alter in any way.")
937
938 (defvar gnus-decode-encoded-word-function
939   (mime-find-field-decoder 'From 'nov)
940   "Variable that says which function should be used to decode a string with encoded words.")
941
942 (defcustom gnus-extra-headers '(To Newsgroups)
943   "*Extra headers to parse."
944   :version "21.1"
945   :group 'gnus-summary
946   :type '(repeat symbol))
947
948 (defcustom gnus-ignored-from-addresses
949   (and user-mail-address (regexp-quote user-mail-address))
950   "*Regexp of From headers that may be suppressed in favor of To headers."
951   :version "21.1"
952   :group 'gnus-summary
953   :type 'regexp)
954
955 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
956   "List of charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959   :version "21.1"
960   :type '(repeat symbol)
961   :group 'gnus-charset)
962
963 (gnus-define-group-parameter
964  ignored-charsets
965  :type list
966  :function-document
967  "Return the ignored charsets of GROUP."
968  :variable gnus-group-ignored-charsets-alist
969  :variable-default
970  '(("alt\\.chinese\\.text" iso-8859-1))
971  :variable-document
972  "Alist of regexps (to match group names) and charsets that should be ignored.
973 When these charsets are used in the \"charset\" parameter, the
974 default charset will be used instead."
975  :variable-group gnus-charset
976  :variable-type '(repeat (cons (regexp :tag "Group")
977                                (repeat symbol)))
978  :parameter-type '(choice :tag "Ignored charsets"
979                           :value nil
980                           (repeat (symbol)))
981  :parameter-document       "\
982 List of charsets that should be ignored.
983
984 When these charsets are used in the \"charset\" parameter, the
985 default charset will be used instead.")
986
987 (defcustom gnus-group-highlight-words-alist nil
988   "Alist of group regexps and highlight regexps.
989 This variable uses the same syntax as `gnus-emphasis-alist'."
990   :version "21.1"
991   :type '(repeat (cons (regexp :tag "Group")
992                        (repeat (list (regexp :tag "Highlight regexp")
993                                      (number :tag "Group for entire word" 0)
994                                      (number :tag "Group for displayed part" 0)
995                                      (symbol :tag "Face"
996                                              gnus-emphasis-highlight-words)))))
997   :group 'gnus-summary-visual)
998
999 (defcustom gnus-use-wheel nil
1000   "Use Intelli-mouse on summary movement"
1001   :type 'boolean
1002   :group 'gnus-summary-maneuvering)
1003
1004 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1005   "Amount to scroll messages by spinning the mouse wheel.
1006 This is actually a cons cell, where the first item is the amount to scroll
1007 on a normal wheel event, and the second is the amount to scroll when the
1008 wheel is moved with the shift key depressed."
1009   :type '(cons (integer :tag "Shift") integer)
1010   :group 'gnus-summary-maneuvering)
1011
1012 (defcustom gnus-wheel-edge-resistance 2
1013   "How hard it should be to change the current article
1014 by moving the mouse over the edge of the article window."
1015   :type 'integer
1016   :group 'gnus-summary-maneuvering)
1017
1018 (defcustom gnus-summary-show-article-charset-alist
1019   nil
1020   "Alist of number and charset.
1021 The article will be shown with the charset corresponding to the
1022 numbered argument.
1023 For example: ((1 . cn-gb-2312) (2 . big5))."
1024   :version "21.1"
1025   :type '(repeat (cons (number :tag "Argument" 1)
1026                        (symbol :tag "Charset")))
1027   :group 'gnus-charset)
1028
1029 (defcustom gnus-preserve-marks t
1030   "Whether marks are preserved when moving, copying and respooling messages."
1031   :version "21.1"
1032   :type 'boolean
1033   :group 'gnus-summary-marks)
1034
1035 (defcustom gnus-alter-articles-to-read-function nil
1036   "Function to be called to alter the list of articles to be selected."
1037   :type '(choice (const nil) function)
1038   :group 'gnus-summary)
1039
1040 (defcustom gnus-orphan-score nil
1041   "*All orphans get this score added.  Set in the score file."
1042   :group 'gnus-score-default
1043   :type '(choice (const nil)
1044                  integer))
1045
1046 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1047   "*A regexp to match MIME parts when saving multiple parts of a message
1048 with gnus-summary-save-parts (X m). This regexp will be used by default
1049 when prompting the user for which type of files to save."
1050   :group 'gnus-summary
1051   :type 'regexp)
1052
1053 (defcustom gnus-read-all-available-headers nil
1054   "Whether Gnus should parse all headers made available to it.
1055 This is mostly relevant for slow backends where the user may
1056 wish to widen the summary buffer to include all headers
1057 that were fetched.  Say, for nnultimate groups."
1058   :group 'gnus-summary
1059   :type '(choice boolean regexp))
1060
1061 (defcustom gnus-summary-muttprint-program "muttprint"
1062   "Command (and optional arguments) used to run Muttprint."
1063   :version "21.3"
1064   :group 'gnus-summary
1065   :type 'string)
1066
1067 (defcustom gnus-article-loose-mime nil
1068   "If non-nil, don't require MIME-Version header.
1069 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1070 supply the MIME-Version header or deliberately strip it From the mail.
1071 Set it to non-nil, Gnus will treat some articles as MIME even if
1072 the MIME-Version header is missed."
1073   :version "21.3"
1074   :type 'boolean
1075   :group 'gnus-article)
1076
1077 ;;; Internal variables
1078
1079 (defvar gnus-summary-display-cache nil)
1080 (defvar gnus-article-mime-handles nil)
1081 (defvar gnus-article-decoded-p nil)
1082 (defvar gnus-article-charset nil)
1083 (defvar gnus-article-ignored-charsets nil)
1084 (defvar gnus-scores-exclude-files nil)
1085 (defvar gnus-page-broken nil)
1086 (defvar gnus-inhibit-mime-unbuttonizing nil)
1087
1088 (defvar gnus-original-article nil)
1089 (defvar gnus-article-internal-prepare-hook nil)
1090 (defvar gnus-newsgroup-process-stack nil)
1091
1092 (defvar gnus-thread-indent-array nil)
1093 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1094 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1095   "Function called to sort the articles within a thread after it has been gathered together.")
1096
1097 (defvar gnus-summary-save-parts-type-history nil)
1098 (defvar gnus-summary-save-parts-last-directory nil)
1099
1100 ;; Avoid highlighting in kill files.
1101 (defvar gnus-summary-inhibit-highlight nil)
1102 (defvar gnus-newsgroup-selected-overlay nil)
1103 (defvar gnus-inhibit-limiting nil)
1104 (defvar gnus-newsgroup-adaptive-score-file nil)
1105 (defvar gnus-current-score-file nil)
1106 (defvar gnus-current-move-group nil)
1107 (defvar gnus-current-copy-group nil)
1108 (defvar gnus-current-crosspost-group nil)
1109 (defvar gnus-newsgroup-display nil)
1110
1111 (defvar gnus-newsgroup-dependencies nil)
1112 (defvar gnus-newsgroup-adaptive nil)
1113 (defvar gnus-summary-display-article-function nil)
1114 (defvar gnus-summary-highlight-line-function nil
1115   "Function called after highlighting a summary line.")
1116
1117 (defvar gnus-summary-line-format-alist
1118   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1119     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1120     (?s gnus-tmp-subject-or-nil ?s)
1121     (?n gnus-tmp-name ?s)
1122     (?A (std11-address-string
1123          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1124     (?a (or (std11-full-name-string
1125              (car (mime-entity-read-field gnus-tmp-header 'From)))
1126             gnus-tmp-from) ?s)
1127     (?F gnus-tmp-from ?s)
1128     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1129     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1130     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1131     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1132     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1133     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1134     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1135     (?L gnus-tmp-lines ?s)
1136     (?I gnus-tmp-indentation ?s)
1137     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1138     (?R gnus-tmp-replied ?c)
1139     (?\[ gnus-tmp-opening-bracket ?c)
1140     (?\] gnus-tmp-closing-bracket ?c)
1141     (?\> (make-string gnus-tmp-level ? ) ?s)
1142     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1143     (?i gnus-tmp-score ?d)
1144     (?z gnus-tmp-score-char ?c)
1145     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1146     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1147     (?U gnus-tmp-unread ?c)
1148     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1149         ?s)
1150     (?t (gnus-summary-number-of-articles-in-thread
1151          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1152         ?d)
1153     (?e (gnus-summary-number-of-articles-in-thread
1154          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1155         ?c)
1156     (?u gnus-tmp-user-defined ?s)
1157     (?P (gnus-pick-line-number) ?d)
1158     (?B gnus-tmp-thread-tree-header-string ?s)
1159     (user-date (gnus-user-date
1160                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1161   "An alist of format specifications that can appear in summary lines.
1162 These are paired with what variables they correspond with, along with
1163 the type of the variable (string, integer, character, etc).")
1164
1165 (defvar gnus-summary-dummy-line-format-alist
1166   `((?S gnus-tmp-subject ?s)
1167     (?N gnus-tmp-number ?d)
1168     (?u gnus-tmp-user-defined ?s)))
1169
1170 (defvar gnus-summary-mode-line-format-alist
1171   `((?G gnus-tmp-group-name ?s)
1172     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1173     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1174     (?A gnus-tmp-article-number ?d)
1175     (?Z gnus-tmp-unread-and-unselected ?s)
1176     (?V gnus-version ?s)
1177     (?U gnus-tmp-unread-and-unticked ?d)
1178     (?S gnus-tmp-subject ?s)
1179     (?e gnus-tmp-unselected ?d)
1180     (?u gnus-tmp-user-defined ?s)
1181     (?d (length gnus-newsgroup-dormant) ?d)
1182     (?t (length gnus-newsgroup-marked) ?d)
1183     (?r (length gnus-newsgroup-reads) ?d)
1184     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1185     (?E gnus-newsgroup-expunged-tally ?d)
1186     (?s (gnus-current-score-file-nondirectory) ?s)))
1187
1188 (defvar gnus-last-search-regexp nil
1189   "Default regexp for article search command.")
1190
1191 (defvar gnus-summary-search-article-matched-data nil
1192   "Last matched data of article search command.  It is the local variable
1193 in `gnus-article-buffer' which consists of the list of start position,
1194 end position and text.")
1195
1196 (defvar gnus-last-shell-command nil
1197   "Default shell command on article.")
1198
1199 (defvar gnus-newsgroup-begin nil)
1200 (defvar gnus-newsgroup-end nil)
1201 (defvar gnus-newsgroup-last-rmail nil)
1202 (defvar gnus-newsgroup-last-mail nil)
1203 (defvar gnus-newsgroup-last-folder nil)
1204 (defvar gnus-newsgroup-last-file nil)
1205 (defvar gnus-newsgroup-auto-expire nil)
1206 (defvar gnus-newsgroup-active nil)
1207
1208 (defvar gnus-newsgroup-data nil)
1209 (defvar gnus-newsgroup-data-reverse nil)
1210 (defvar gnus-newsgroup-limit nil)
1211 (defvar gnus-newsgroup-limits nil)
1212
1213 (defvar gnus-newsgroup-unreads nil
1214   "Sorted list of unread articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-unselected nil
1217   "Sorted list of unselected unread articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-reads nil
1220   "Alist of read articles and article marks in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-expunged-tally nil)
1223
1224 (defvar gnus-newsgroup-marked nil
1225   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1226
1227 (defvar gnus-newsgroup-killed nil
1228   "List of ranges of articles that have been through the scoring process.")
1229
1230 (defvar gnus-newsgroup-cached nil
1231   "Sorted list of articles that come from the article cache.")
1232
1233 (defvar gnus-newsgroup-saved nil
1234   "List of articles that have been saved.")
1235
1236 (defvar gnus-newsgroup-kill-headers nil)
1237
1238 (defvar gnus-newsgroup-replied nil
1239   "List of articles that have been replied to in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-forwarded nil
1242   "List of articles that have been forwarded in the current newsgroup.")
1243
1244 (defvar gnus-newsgroup-recent nil
1245   "List of articles that have are recent in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-expirable nil
1248   "Sorted list of articles in the current newsgroup that can be expired.")
1249
1250 (defvar gnus-newsgroup-processable nil
1251   "List of articles in the current newsgroup that can be processed.")
1252
1253 (defvar gnus-newsgroup-downloadable nil
1254   "Sorted list of articles in the current newsgroup that can be processed.")
1255
1256 (defvar gnus-newsgroup-undownloaded nil
1257   "List of articles in the current newsgroup that haven't been downloaded..")
1258
1259 (defvar gnus-newsgroup-unsendable nil
1260   "List of articles in the current newsgroup that won't be sent.")
1261
1262 (defvar gnus-newsgroup-bookmarks nil
1263   "List of articles in the current newsgroup that have bookmarks.")
1264
1265 (defvar gnus-newsgroup-dormant nil
1266   "Sorted list of dormant articles in the current newsgroup.")
1267
1268 (defvar gnus-newsgroup-unseen nil
1269   "List of unseen articles in the current newsgroup.")
1270
1271 (defvar gnus-newsgroup-seen nil
1272   "Range of seen articles in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-articles nil
1275   "List of articles in the current newsgroup.")
1276
1277 (defvar gnus-newsgroup-scored nil
1278   "List of scored articles in the current newsgroup.")
1279
1280 (defvar gnus-newsgroup-incorporated nil
1281   "List of incorporated articles in the current newsgroup.")
1282
1283 (defvar gnus-newsgroup-headers nil
1284   "List of article headers in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-threads nil)
1287
1288 (defvar gnus-newsgroup-prepared nil
1289   "Whether the current group has been prepared properly.")
1290
1291 (defvar gnus-newsgroup-ancient nil
1292   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-sparse nil)
1295
1296 (defvar gnus-current-article nil)
1297 (defvar gnus-article-current nil)
1298 (defvar gnus-current-headers nil)
1299 (defvar gnus-have-all-headers nil)
1300 (defvar gnus-last-article nil)
1301 (defvar gnus-newsgroup-history nil)
1302 (defvar gnus-newsgroup-charset nil)
1303 (defvar gnus-newsgroup-ephemeral-charset nil)
1304 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1305
1306 (defvar gnus-article-before-search nil)
1307
1308 (defconst gnus-summary-local-variables
1309   '(gnus-newsgroup-name
1310     gnus-newsgroup-begin gnus-newsgroup-end
1311     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1312     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1313     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1314     gnus-newsgroup-unselected gnus-newsgroup-marked
1315     gnus-newsgroup-reads gnus-newsgroup-saved
1316     gnus-newsgroup-replied gnus-newsgroup-forwarded
1317     gnus-newsgroup-recent
1318     gnus-newsgroup-expirable
1319     gnus-newsgroup-processable gnus-newsgroup-killed
1320     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1321     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1322     gnus-newsgroup-seen gnus-newsgroup-articles
1323     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1324     gnus-newsgroup-headers gnus-newsgroup-threads
1325     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1326     gnus-current-article gnus-current-headers gnus-have-all-headers
1327     gnus-last-article gnus-article-internal-prepare-hook
1328     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1329     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1330     gnus-thread-expunge-below
1331     gnus-score-alist gnus-current-score-file
1332     (gnus-summary-expunge-below . global)
1333     (gnus-summary-mark-below . global)
1334     (gnus-orphan-score . global)
1335     gnus-newsgroup-active gnus-scores-exclude-files
1336     gnus-newsgroup-history gnus-newsgroup-ancient
1337     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1338     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1339     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1340     (gnus-newsgroup-expunged-tally . 0)
1341     gnus-cache-removable-articles gnus-newsgroup-cached
1342     gnus-newsgroup-data gnus-newsgroup-data-reverse
1343     gnus-newsgroup-limit gnus-newsgroup-limits
1344     gnus-newsgroup-charset gnus-newsgroup-display
1345     gnus-newsgroup-incorporated)
1346   "Variables that are buffer-local to the summary buffers.")
1347
1348 (defvar gnus-newsgroup-variables nil
1349   "A list of variables that have separate values in different newsgroups.
1350 A list of newsgroup (summary buffer) local variables, or cons of
1351 variables and their default values (when the default values are not
1352 nil), that should be made global while the summary buffer is active.
1353 These variables can be used to set variables in the group parameters
1354 while still allowing them to affect operations done in other
1355 buffers. For example:
1356
1357 \(setq gnus-newsgroup-variables
1358      '(message-use-followup-to
1359        (gnus-visible-headers .
1360          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1361 ")
1362
1363 ;; Byte-compiler warning.
1364 (eval-when-compile (defvar gnus-article-mode-map))
1365
1366 ;; Subject simplification.
1367
1368 (defun gnus-simplify-whitespace (str)
1369   "Remove excessive whitespace from STR."
1370   (let ((mystr str))
1371     ;; Multiple spaces.
1372     (while (string-match "[ \t][ \t]+" mystr)
1373       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1374                           " "
1375                           (substring mystr (match-end 0)))))
1376     ;; Leading spaces.
1377     (when (string-match "^[ \t]+" mystr)
1378       (setq mystr (substring mystr (match-end 0))))
1379     ;; Trailing spaces.
1380     (when (string-match "[ \t]+$" mystr)
1381       (setq mystr (substring mystr 0 (match-beginning 0))))
1382     mystr))
1383
1384 (defun gnus-simplify-all-whitespace (str)
1385   "Remove all whitespace from STR."
1386   (let ((mystr str))
1387     (while (string-match "[ \t\n]+" mystr)
1388       (setq mystr (replace-match "" nil nil mystr)))
1389     mystr))
1390
1391 (defsubst gnus-simplify-subject-re (subject)
1392   "Remove \"Re:\" from subject lines."
1393   (if (string-match message-subject-re-regexp subject)
1394       (substring subject (match-end 0))
1395     subject))
1396
1397 (defun gnus-simplify-subject (subject &optional re-only)
1398   "Remove `Re:' and words in parentheses.
1399 If RE-ONLY is non-nil, strip leading `Re:'s only."
1400   (let ((case-fold-search t))           ;Ignore case.
1401     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1402     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1403       (setq subject (substring subject (match-end 0))))
1404     ;; Remove uninteresting prefixes.
1405     (when (and (not re-only)
1406                gnus-simplify-ignored-prefixes
1407                (string-match gnus-simplify-ignored-prefixes subject))
1408       (setq subject (substring subject (match-end 0))))
1409     ;; Remove words in parentheses from end.
1410     (unless re-only
1411       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1412         (setq subject (substring subject 0 (match-beginning 0)))))
1413     ;; Return subject string.
1414     subject))
1415
1416 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1417 ;; all whitespace.
1418 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1419   (goto-char (point-min))
1420   (while (re-search-forward regexp nil t)
1421     (replace-match (or newtext ""))))
1422
1423 (defun gnus-simplify-buffer-fuzzy ()
1424   "Simplify string in the buffer fuzzily.
1425 The string in the accessible portion of the current buffer is simplified.
1426 It is assumed to be a single-line subject.
1427 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1428 matter is removed.  Additional things can be deleted by setting
1429 `gnus-simplify-subject-fuzzy-regexp'."
1430   (let ((case-fold-search t)
1431         (modified-tick))
1432     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1433
1434     (while (not (eq modified-tick (buffer-modified-tick)))
1435       (setq modified-tick (buffer-modified-tick))
1436       (cond
1437        ((listp gnus-simplify-subject-fuzzy-regexp)
1438         (mapcar 'gnus-simplify-buffer-fuzzy-step
1439                 gnus-simplify-subject-fuzzy-regexp))
1440        (gnus-simplify-subject-fuzzy-regexp
1441         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1442       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1443       (gnus-simplify-buffer-fuzzy-step
1444        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1445       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1446
1447     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1448     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1449     (gnus-simplify-buffer-fuzzy-step " $")
1450     (gnus-simplify-buffer-fuzzy-step "^ +")))
1451
1452 (defun gnus-simplify-subject-fuzzy (subject)
1453   "Simplify a subject string fuzzily.
1454 See `gnus-simplify-buffer-fuzzy' for details."
1455   (save-excursion
1456     (gnus-set-work-buffer)
1457     (let ((case-fold-search t))
1458       ;; Remove uninteresting prefixes.
1459       (when (and gnus-simplify-ignored-prefixes
1460                  (string-match gnus-simplify-ignored-prefixes subject))
1461         (setq subject (substring subject (match-end 0))))
1462       (insert subject)
1463       (inline (gnus-simplify-buffer-fuzzy))
1464       (buffer-string))))
1465
1466 (defsubst gnus-simplify-subject-fully (subject)
1467   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1468   (cond
1469    (gnus-simplify-subject-functions
1470     (gnus-map-function gnus-simplify-subject-functions subject))
1471    ((null gnus-summary-gather-subject-limit)
1472     (gnus-simplify-subject-re subject))
1473    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1474     (gnus-simplify-subject-fuzzy subject))
1475    ((numberp gnus-summary-gather-subject-limit)
1476     (gnus-limit-string (gnus-simplify-subject-re subject)
1477                        gnus-summary-gather-subject-limit))
1478    (t
1479     subject)))
1480
1481 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1482   "Check whether two subjects are equal.
1483 If optional argument simple-first is t, first argument is already
1484 simplified."
1485   (cond
1486    ((null simple-first)
1487     (equal (gnus-simplify-subject-fully s1)
1488            (gnus-simplify-subject-fully s2)))
1489    (t
1490     (equal s1
1491            (gnus-simplify-subject-fully s2)))))
1492
1493 (defun gnus-summary-bubble-group ()
1494   "Increase the score of the current group.
1495 This is a handy function to add to `gnus-summary-exit-hook' to
1496 increase the score of each group you read."
1497   (gnus-group-add-score gnus-newsgroup-name))
1498
1499 \f
1500 ;;;
1501 ;;; Gnus summary mode
1502 ;;;
1503
1504 (put 'gnus-summary-mode 'mode-class 'special)
1505
1506 (defvar gnus-article-commands-menu)
1507
1508 (when t
1509   ;; Non-orthogonal keys
1510
1511   (gnus-define-keys gnus-summary-mode-map
1512     " " gnus-summary-next-page
1513     "\177" gnus-summary-prev-page
1514     [delete] gnus-summary-prev-page
1515     [backspace] gnus-summary-prev-page
1516     "\r" gnus-summary-scroll-up
1517     "\M-\r" gnus-summary-scroll-down
1518     "n" gnus-summary-next-unread-article
1519     "p" gnus-summary-prev-unread-article
1520     "N" gnus-summary-next-article
1521     "P" gnus-summary-prev-article
1522     "\M-\C-n" gnus-summary-next-same-subject
1523     "\M-\C-p" gnus-summary-prev-same-subject
1524     "\M-n" gnus-summary-next-unread-subject
1525     "\M-p" gnus-summary-prev-unread-subject
1526     "." gnus-summary-first-unread-article
1527     "," gnus-summary-best-unread-article
1528     "\M-s" gnus-summary-search-article-forward
1529     "\M-r" gnus-summary-search-article-backward
1530     "<" gnus-summary-beginning-of-article
1531     ">" gnus-summary-end-of-article
1532     "j" gnus-summary-goto-article
1533     "^" gnus-summary-refer-parent-article
1534     "\M-^" gnus-summary-refer-article
1535     "u" gnus-summary-tick-article-forward
1536     "!" gnus-summary-tick-article-forward
1537     "U" gnus-summary-tick-article-backward
1538     "d" gnus-summary-mark-as-read-forward
1539     "D" gnus-summary-mark-as-read-backward
1540     "E" gnus-summary-mark-as-expirable
1541     "\M-u" gnus-summary-clear-mark-forward
1542     "\M-U" gnus-summary-clear-mark-backward
1543     "k" gnus-summary-kill-same-subject-and-select
1544     "\C-k" gnus-summary-kill-same-subject
1545     "\M-\C-k" gnus-summary-kill-thread
1546     "\M-\C-l" gnus-summary-lower-thread
1547     "e" gnus-summary-edit-article
1548     "#" gnus-summary-mark-as-processable
1549     "\M-#" gnus-summary-unmark-as-processable
1550     "\M-\C-t" gnus-summary-toggle-threads
1551     "\M-\C-s" gnus-summary-show-thread
1552     "\M-\C-h" gnus-summary-hide-thread
1553     "\M-\C-f" gnus-summary-next-thread
1554     "\M-\C-b" gnus-summary-prev-thread
1555     [(meta down)] gnus-summary-next-thread
1556     [(meta up)] gnus-summary-prev-thread
1557     "\M-\C-u" gnus-summary-up-thread
1558     "\M-\C-d" gnus-summary-down-thread
1559     "&" gnus-summary-execute-command
1560     "c" gnus-summary-catchup-and-exit
1561     "\C-w" gnus-summary-mark-region-as-read
1562     "\C-t" gnus-summary-toggle-truncation
1563     "?" gnus-summary-mark-as-dormant
1564     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1565     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1566     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1567     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1568     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1569     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1570     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1571     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1572     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1573     "=" gnus-summary-expand-window
1574     "\C-x\C-s" gnus-summary-reselect-current-group
1575     "\M-g" gnus-summary-rescan-group
1576     "w" gnus-summary-stop-page-breaking
1577     "\C-c\C-r" gnus-summary-caesar-message
1578     "\M-t" gnus-summary-toggle-mime
1579     "f" gnus-summary-followup
1580     "F" gnus-summary-followup-with-original
1581     "C" gnus-summary-cancel-article
1582     "r" gnus-summary-reply
1583     "R" gnus-summary-reply-with-original
1584     "\C-c\C-f" gnus-summary-mail-forward
1585     "o" gnus-summary-save-article
1586     "\C-o" gnus-summary-save-article-mail
1587     "|" gnus-summary-pipe-output
1588     "\M-k" gnus-summary-edit-local-kill
1589     "\M-K" gnus-summary-edit-global-kill
1590     ;; "V" gnus-version
1591     "\C-c\C-d" gnus-summary-describe-group
1592     "q" gnus-summary-exit
1593     "Q" gnus-summary-exit-no-update
1594     "\C-c\C-i" gnus-info-find-node
1595     gnus-mouse-2 gnus-mouse-pick-article
1596     "m" gnus-summary-mail-other-window
1597     "a" gnus-summary-post-news
1598     "i" gnus-summary-news-other-window
1599     "x" gnus-summary-limit-to-unread
1600     "s" gnus-summary-isearch-article
1601     "t" gnus-article-toggle-headers
1602     "g" gnus-summary-show-article
1603     "l" gnus-summary-goto-last-article
1604     "v" gnus-summary-preview-mime-message
1605     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1606     "\C-d" gnus-summary-enter-digest-group
1607     "\M-\C-d" gnus-summary-read-document
1608     "\M-\C-e" gnus-summary-edit-parameters
1609     "\M-\C-a" gnus-summary-customize-parameters
1610     "\C-c\C-b" gnus-bug
1611     "\C-c\C-n" gnus-namazu-search
1612     "*" gnus-cache-enter-article
1613     "\M-*" gnus-cache-remove-article
1614     "\M-&" gnus-summary-universal-argument
1615     "\C-l" gnus-recenter
1616     "I" gnus-summary-increase-score
1617     "L" gnus-summary-lower-score
1618     "\M-i" gnus-symbolic-argument
1619     "h" gnus-summary-select-article-buffer
1620
1621     "V" gnus-summary-score-map
1622     "X" gnus-uu-extract-map
1623     "S" gnus-summary-send-map)
1624
1625   ;; Sort of orthogonal keymap
1626   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1627     "t" gnus-summary-tick-article-forward
1628     "!" gnus-summary-tick-article-forward
1629     "d" gnus-summary-mark-as-read-forward
1630     "r" gnus-summary-mark-as-read-forward
1631     "c" gnus-summary-clear-mark-forward
1632     " " gnus-summary-clear-mark-forward
1633     "e" gnus-summary-mark-as-expirable
1634     "x" gnus-summary-mark-as-expirable
1635     "?" gnus-summary-mark-as-dormant
1636     "b" gnus-summary-set-bookmark
1637     "B" gnus-summary-remove-bookmark
1638     "#" gnus-summary-mark-as-processable
1639     "\M-#" gnus-summary-unmark-as-processable
1640     "S" gnus-summary-limit-include-expunged
1641     "C" gnus-summary-catchup
1642     "H" gnus-summary-catchup-to-here
1643     "h" gnus-summary-catchup-from-here
1644     "\C-c" gnus-summary-catchup-all
1645     "k" gnus-summary-kill-same-subject-and-select
1646     "K" gnus-summary-kill-same-subject
1647     "P" gnus-uu-mark-map)
1648
1649   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1650     "c" gnus-summary-clear-above
1651     "u" gnus-summary-tick-above
1652     "m" gnus-summary-mark-above
1653     "k" gnus-summary-kill-below)
1654
1655   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1656     "/" gnus-summary-limit-to-subject
1657     "n" gnus-summary-limit-to-articles
1658     "w" gnus-summary-pop-limit
1659     "s" gnus-summary-limit-to-subject
1660     "a" gnus-summary-limit-to-author
1661     "u" gnus-summary-limit-to-unread
1662     "m" gnus-summary-limit-to-marks
1663     "M" gnus-summary-limit-exclude-marks
1664     "v" gnus-summary-limit-to-score
1665     "*" gnus-summary-limit-include-cached
1666     "D" gnus-summary-limit-include-dormant
1667     "T" gnus-summary-limit-include-thread
1668     "d" gnus-summary-limit-exclude-dormant
1669     "t" gnus-summary-limit-to-age
1670     "x" gnus-summary-limit-to-extra
1671     "p" gnus-summary-limit-to-display-predicate
1672     "E" gnus-summary-limit-include-expunged
1673     "c" gnus-summary-limit-exclude-childless-dormant
1674     "C" gnus-summary-limit-mark-excluded-as-read
1675     "o" gnus-summary-insert-old-articles
1676     "N" gnus-summary-insert-new-articles)
1677
1678   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1679     "n" gnus-summary-next-unread-article
1680     "p" gnus-summary-prev-unread-article
1681     "N" gnus-summary-next-article
1682     "P" gnus-summary-prev-article
1683     "\C-n" gnus-summary-next-same-subject
1684     "\C-p" gnus-summary-prev-same-subject
1685     "\M-n" gnus-summary-next-unread-subject
1686     "\M-p" gnus-summary-prev-unread-subject
1687     "f" gnus-summary-first-unread-article
1688     "b" gnus-summary-best-unread-article
1689     "j" gnus-summary-goto-article
1690     "g" gnus-summary-goto-subject
1691     "l" gnus-summary-goto-last-article
1692     "o" gnus-summary-pop-article)
1693
1694   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1695     "k" gnus-summary-kill-thread
1696     "l" gnus-summary-lower-thread
1697     "i" gnus-summary-raise-thread
1698     "T" gnus-summary-toggle-threads
1699     "t" gnus-summary-rethread-current
1700     "^" gnus-summary-reparent-thread
1701     "s" gnus-summary-show-thread
1702     "S" gnus-summary-show-all-threads
1703     "h" gnus-summary-hide-thread
1704     "H" gnus-summary-hide-all-threads
1705     "n" gnus-summary-next-thread
1706     "p" gnus-summary-prev-thread
1707     "u" gnus-summary-up-thread
1708     "o" gnus-summary-top-thread
1709     "d" gnus-summary-down-thread
1710     "#" gnus-uu-mark-thread
1711     "\M-#" gnus-uu-unmark-thread)
1712
1713   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1714     "g" gnus-summary-prepare
1715     "c" gnus-summary-insert-cached-articles)
1716
1717   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1718     "c" gnus-summary-catchup-and-exit
1719     "C" gnus-summary-catchup-all-and-exit
1720     "E" gnus-summary-exit-no-update
1721     "J" gnus-summary-jump-to-other-group
1722     "Q" gnus-summary-exit
1723     "Z" gnus-summary-exit
1724     "n" gnus-summary-catchup-and-goto-next-group
1725     "R" gnus-summary-reselect-current-group
1726     "G" gnus-summary-rescan-group
1727     "N" gnus-summary-next-group
1728     "s" gnus-summary-save-newsrc
1729     "P" gnus-summary-prev-group)
1730
1731   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1732     " " gnus-summary-next-page
1733     "n" gnus-summary-next-page
1734     "\177" gnus-summary-prev-page
1735     [delete] gnus-summary-prev-page
1736     "p" gnus-summary-prev-page
1737     "\r" gnus-summary-scroll-up
1738     "\M-\r" gnus-summary-scroll-down
1739     "<" gnus-summary-beginning-of-article
1740     ">" gnus-summary-end-of-article
1741     "b" gnus-summary-beginning-of-article
1742     "e" gnus-summary-end-of-article
1743     "^" gnus-summary-refer-parent-article
1744     "r" gnus-summary-refer-parent-article
1745     "D" gnus-summary-enter-digest-group
1746     "R" gnus-summary-refer-references
1747     "T" gnus-summary-refer-thread
1748     "g" gnus-summary-show-article
1749     "s" gnus-summary-isearch-article
1750     "P" gnus-summary-print-article
1751     "M" gnus-mailing-list-insinuate
1752     "t" gnus-article-babel)
1753
1754   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1755     "b" gnus-article-add-buttons
1756     "B" gnus-article-add-buttons-to-head
1757     "o" gnus-article-treat-overstrike
1758     "e" gnus-article-emphasize
1759     "w" gnus-article-fill-cited-article
1760     "Q" gnus-article-fill-long-lines
1761     "C" gnus-article-capitalize-sentences
1762     "c" gnus-article-remove-cr
1763     "Z" gnus-article-decode-HZ
1764     "h" gnus-article-wash-html
1765     "u" gnus-article-unsplit-urls
1766     "f" gnus-article-display-x-face
1767     "l" gnus-summary-stop-page-breaking
1768     "r" gnus-summary-caesar-message
1769     "t" gnus-article-toggle-headers
1770     "g" gnus-treat-smiley
1771     "v" gnus-summary-verbose-headers
1772     "m" gnus-summary-toggle-mime
1773     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1774     "p" gnus-article-verify-x-pgp-sig
1775     "d" gnus-article-treat-dumbquotes
1776     "k" gnus-article-outlook-deuglify-article)
1777
1778   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1779     "a" gnus-article-hide
1780     "h" gnus-article-toggle-headers
1781     "b" gnus-article-hide-boring-headers
1782     "s" gnus-article-hide-signature
1783     "c" gnus-article-hide-citation
1784     "C" gnus-article-hide-citation-in-followups
1785     "l" gnus-article-hide-list-identifiers
1786     "p" gnus-article-hide-pgp
1787     "B" gnus-article-strip-banner
1788     "P" gnus-article-hide-pem
1789     "\C-c" gnus-article-hide-citation-maybe)
1790
1791   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1792     "a" gnus-article-highlight
1793     "h" gnus-article-highlight-headers
1794     "c" gnus-article-highlight-citation
1795     "s" gnus-article-highlight-signature)
1796
1797   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1798     "f" gnus-article-treat-fold-headers
1799     "u" gnus-article-treat-unfold-headers
1800     "n" gnus-article-treat-fold-newsgroups)
1801
1802   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1803     "x" gnus-article-display-x-face
1804     "s" gnus-treat-smiley
1805     "D" gnus-article-remove-images
1806     "f" gnus-treat-from-picon
1807     "m" gnus-treat-mail-picon
1808     "n" gnus-treat-newsgroups-picon)
1809
1810   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1811     "z" gnus-article-date-ut
1812     "u" gnus-article-date-ut
1813     "l" gnus-article-date-local
1814     "p" gnus-article-date-english
1815     "e" gnus-article-date-lapsed
1816     "o" gnus-article-date-original
1817     "i" gnus-article-date-iso8601
1818     "s" gnus-article-date-user)
1819
1820   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1821     "t" gnus-article-remove-trailing-blank-lines
1822     "l" gnus-article-strip-leading-blank-lines
1823     "m" gnus-article-strip-multiple-blank-lines
1824     "a" gnus-article-strip-blank-lines
1825     "A" gnus-article-strip-all-blank-lines
1826     "s" gnus-article-strip-leading-space
1827     "e" gnus-article-strip-trailing-space
1828     "w" gnus-article-remove-leading-whitespace)
1829
1830   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1831     "v" gnus-version
1832     "f" gnus-summary-fetch-faq
1833     "d" gnus-summary-describe-group
1834     "h" gnus-summary-describe-briefly
1835     "i" gnus-info-find-node)
1836
1837   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1838     "e" gnus-summary-expire-articles
1839     "\M-\C-e" gnus-summary-expire-articles-now
1840     "\177" gnus-summary-delete-article
1841     [delete] gnus-summary-delete-article
1842     [backspace] gnus-summary-delete-article
1843     "m" gnus-summary-move-article
1844     "r" gnus-summary-respool-article
1845     "w" gnus-summary-edit-article
1846     "c" gnus-summary-copy-article
1847     "B" gnus-summary-crosspost-article
1848     "q" gnus-summary-respool-query
1849     "t" gnus-summary-respool-trace
1850     "i" gnus-summary-import-article
1851     "I" gnus-summary-create-article
1852     "p" gnus-summary-article-posted-p)
1853
1854   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1855     "o" gnus-summary-save-article
1856     "m" gnus-summary-save-article-mail
1857     "F" gnus-summary-write-article-file
1858     "r" gnus-summary-save-article-rmail
1859     "f" gnus-summary-save-article-file
1860     "b" gnus-summary-save-article-body-file
1861     "h" gnus-summary-save-article-folder
1862     "v" gnus-summary-save-article-vm
1863     "p" gnus-summary-pipe-output
1864     "P" gnus-summary-muttprint
1865     "s" gnus-soup-add-article)
1866
1867   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1868     "b" gnus-summary-display-buttonized
1869     "m" gnus-summary-repair-multipart
1870     "v" gnus-article-view-part
1871     "o" gnus-article-save-part
1872     "c" gnus-article-copy-part
1873     "C" gnus-article-view-part-as-charset
1874     "e" gnus-article-view-part-externally
1875     "E" gnus-article-encrypt-body
1876     "i" gnus-article-inline-part
1877     "|" gnus-article-pipe-part)
1878
1879   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1880     "p" gnus-summary-mark-as-processable
1881     "u" gnus-summary-unmark-as-processable
1882     "U" gnus-summary-unmark-all-processable
1883     "v" gnus-uu-mark-over
1884     "s" gnus-uu-mark-series
1885     "r" gnus-uu-mark-region
1886     "g" gnus-uu-unmark-region
1887     "R" gnus-uu-mark-by-regexp
1888     "G" gnus-uu-unmark-by-regexp
1889     "t" gnus-uu-mark-thread
1890     "T" gnus-uu-unmark-thread
1891     "a" gnus-uu-mark-all
1892     "b" gnus-uu-mark-buffer
1893     "S" gnus-uu-mark-sparse
1894     "k" gnus-summary-kill-process-mark
1895     "y" gnus-summary-yank-process-mark
1896     "w" gnus-summary-save-process-mark
1897     "i" gnus-uu-invert-processable)
1898
1899   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1900     ;;"x" gnus-uu-extract-any
1901     "m" gnus-summary-save-parts
1902     "u" gnus-uu-decode-uu
1903     "U" gnus-uu-decode-uu-and-save
1904     "s" gnus-uu-decode-unshar
1905     "S" gnus-uu-decode-unshar-and-save
1906     "o" gnus-uu-decode-save
1907     "O" gnus-uu-decode-save
1908     "b" gnus-uu-decode-binhex
1909     "B" gnus-uu-decode-binhex
1910     "p" gnus-uu-decode-postscript
1911     "P" gnus-uu-decode-postscript-and-save)
1912
1913   (gnus-define-keys
1914       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1915     "u" gnus-uu-decode-uu-view
1916     "U" gnus-uu-decode-uu-and-save-view
1917     "s" gnus-uu-decode-unshar-view
1918     "S" gnus-uu-decode-unshar-and-save-view
1919     "o" gnus-uu-decode-save-view
1920     "O" gnus-uu-decode-save-view
1921     "b" gnus-uu-decode-binhex-view
1922     "B" gnus-uu-decode-binhex-view
1923     "p" gnus-uu-decode-postscript-view
1924     "P" gnus-uu-decode-postscript-and-save-view))
1925
1926 (defvar gnus-article-post-menu nil)
1927
1928 (defconst gnus-summary-menu-maxlen 20)
1929
1930 (defun gnus-summary-menu-split (menu)
1931   ;; If we have lots of elements, divide them into groups of 20
1932   ;; and make a pane (or submenu) for each one.
1933   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1934       (let ((menu menu) sublists next
1935             (i 1))
1936         (while menu
1937           ;; Pull off the next gnus-summary-menu-maxlen elements
1938           ;; and make them the next element of sublist.
1939           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1940           (if next
1941               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1942                       nil))
1943           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1944                                              (aref (car (last menu)) 0)) menu)
1945                                sublists))
1946           (setq i (1+ i))
1947           (setq menu next))
1948         (nreverse sublists))
1949     ;; Few elements--put them all in one pane.
1950     menu))
1951
1952 (defun gnus-summary-make-menu-bar ()
1953   (gnus-turn-off-edit-menu 'summary)
1954
1955   (unless (boundp 'gnus-summary-misc-menu)
1956
1957     (easy-menu-define
1958      gnus-summary-kill-menu gnus-summary-mode-map ""
1959      (cons
1960       "Score"
1961       (nconc
1962        (list
1963         ["Customize" gnus-score-customize t])
1964        (gnus-make-score-map 'increase)
1965        (gnus-make-score-map 'lower)
1966        '(("Mark"
1967           ["Kill below" gnus-summary-kill-below t]
1968           ["Mark above" gnus-summary-mark-above t]
1969           ["Tick above" gnus-summary-tick-above t]
1970           ["Clear above" gnus-summary-clear-above t])
1971          ["Current score" gnus-summary-current-score t]
1972          ["Set score" gnus-summary-set-score t]
1973          ["Switch current score file..." gnus-score-change-score-file t]
1974          ["Set mark below..." gnus-score-set-mark-below t]
1975          ["Set expunge below..." gnus-score-set-expunge-below t]
1976          ["Edit current score file" gnus-score-edit-current-scores t]
1977          ["Edit score file" gnus-score-edit-file t]
1978          ["Trace score" gnus-score-find-trace t]
1979          ["Find words" gnus-score-find-favourite-words t]
1980          ["Rescore buffer" gnus-summary-rescore t]
1981          ["Increase score..." gnus-summary-increase-score t]
1982          ["Lower score..." gnus-summary-lower-score t]))))
1983
1984     ;; Define both the Article menu in the summary buffer and the
1985     ;; equivalent Commands menu in the article buffer here for
1986     ;; consistency.
1987     (let ((innards
1988            `(("Hide"
1989               ["All" gnus-article-hide t]
1990               ["Headers" gnus-article-toggle-headers t]
1991               ["Signature" gnus-article-hide-signature t]
1992               ["Citation" gnus-article-hide-citation t]
1993               ["List identifiers" gnus-article-hide-list-identifiers t]
1994               ["PGP" gnus-article-hide-pgp t]
1995               ["Banner" gnus-article-strip-banner t]
1996               ["Boring headers" gnus-article-hide-boring-headers t])
1997              ("Highlight"
1998               ["All" gnus-article-highlight t]
1999               ["Headers" gnus-article-highlight-headers t]
2000               ["Signature" gnus-article-highlight-signature t]
2001               ["Citation" gnus-article-highlight-citation t])
2002              ("Date"
2003               ["Local" gnus-article-date-local t]
2004               ["ISO8601" gnus-article-date-iso8601 t]
2005               ["UT" gnus-article-date-ut t]
2006               ["Original" gnus-article-date-original t]
2007               ["Lapsed" gnus-article-date-lapsed t]
2008               ["User-defined" gnus-article-date-user t])
2009              ("Display"
2010               ["Remove images" gnus-article-remove-images t]
2011               ["Toggle smiley" gnus-treat-smiley t]
2012               ["Show X-Face" gnus-article-display-x-face t]
2013               ["Show picons in From" gnus-treat-from-picon t]
2014               ["Show picons in mail headers" gnus-treat-mail-picon t]
2015               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2016               ("View as different encoding"
2017                ,@(gnus-summary-menu-split
2018                   (mapcar
2019                    (lambda (cs)
2020                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2021                      ;; forms for menu commands, we should provide intern'ed
2022                      ;; function symbols.
2023                      (let ((command (intern (format "\
2024 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2025                        (fset command
2026                              `(lambda ()
2027                                 (interactive)
2028                                 (let ((gnus-summary-show-article-charset-alist
2029                                        '((1 . ,cs))))
2030                                   (gnus-summary-show-article 1))))
2031                        `[,(symbol-name cs) ,command t]))
2032                    (sort (if (fboundp 'coding-system-list)
2033                              (coding-system-list)
2034                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2035                            )
2036                          (lambda (a b)
2037                            (string< (symbol-name a)
2038                                     (symbol-name b))))))))
2039              ("Washing"
2040               ("Remove Blanks"
2041                ["Leading" gnus-article-strip-leading-blank-lines t]
2042                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2043                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2044                ["All of the above" gnus-article-strip-blank-lines t]
2045                ["All" gnus-article-strip-all-blank-lines t]
2046                ["Leading space" gnus-article-strip-leading-space t]
2047                ["Trailing space" gnus-article-strip-trailing-space t]
2048                ["Leading space in headers"
2049                 gnus-article-remove-leading-whitespace t])
2050               ["Overstrike" gnus-article-treat-overstrike t]
2051               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2052               ["Emphasis" gnus-article-emphasize t]
2053               ["Word wrap" gnus-article-fill-cited-article t]
2054               ["Fill long lines" gnus-article-fill-long-lines t]
2055               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2056               ["CR" gnus-article-remove-cr t]
2057               ["Rot 13" gnus-summary-caesar-message
2058                ,@(if (featurep 'xemacs) '(t)
2059                    '(:help "\"Caesar rotate\" article by 13"))]
2060               ["Unix pipe" gnus-summary-pipe-message t]
2061               ["Add buttons" gnus-article-add-buttons t]
2062               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2063               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2064               ["Toggle MIME" gnus-summary-toggle-mime t]
2065               ["Verbose header" gnus-summary-verbose-headers t]
2066               ["Toggle header" gnus-summary-toggle-header t]
2067               ["Unfold headers" gnus-article-treat-unfold-headers t]
2068               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2069               ["Html" gnus-article-wash-html t]
2070               ["URLs" gnus-article-unsplit-urls t]
2071               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2072               ["HZ" gnus-article-decode-HZ t]
2073               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2074               )
2075              ("Output"
2076               ["Save in default format" gnus-summary-save-article
2077                ,@(if (featurep 'xemacs) '(t)
2078                    '(:help "Save article using default method"))]
2079               ["Save in file" gnus-summary-save-article-file
2080                ,@(if (featurep 'xemacs) '(t)
2081                    '(:help "Save article in file"))]
2082               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2083               ["Save in MH folder" gnus-summary-save-article-folder t]
2084               ["Save in VM folder" gnus-summary-save-article-vm t]
2085               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2086               ["Save body in file" gnus-summary-save-article-body-file t]
2087               ["Pipe through a filter" gnus-summary-pipe-output t]
2088               ["Add to SOUP packet" gnus-soup-add-article t]
2089               ["Print with Muttprint" gnus-summary-muttprint t]
2090               ["Print" gnus-summary-print-article t])
2091              ("Backend"
2092               ["Respool article..." gnus-summary-respool-article t]
2093               ["Move article..." gnus-summary-move-article
2094                (gnus-check-backend-function
2095                 'request-move-article gnus-newsgroup-name)]
2096               ["Copy article..." gnus-summary-copy-article t]
2097               ["Crosspost article..." gnus-summary-crosspost-article
2098                (gnus-check-backend-function
2099                 'request-replace-article gnus-newsgroup-name)]
2100               ["Import file..." gnus-summary-import-article t]
2101               ["Create article..." gnus-summary-create-article t]
2102               ["Check if posted" gnus-summary-article-posted-p t]
2103               ["Edit article" gnus-summary-edit-article
2104                (not (gnus-group-read-only-p))]
2105               ["Delete article" gnus-summary-delete-article
2106                (gnus-check-backend-function
2107                 'request-expire-articles gnus-newsgroup-name)]
2108               ["Query respool" gnus-summary-respool-query t]
2109               ["Trace respool" gnus-summary-respool-trace t]
2110               ["Delete expirable articles" gnus-summary-expire-articles-now
2111                (gnus-check-backend-function
2112                 'request-expire-articles gnus-newsgroup-name)])
2113              ("Extract"
2114               ["Uudecode" gnus-uu-decode-uu
2115                ,@(if (featurep 'xemacs) '(t)
2116                    '(:help "Decode uuencoded article(s)"))]
2117               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2118               ["Unshar" gnus-uu-decode-unshar t]
2119               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2120               ["Save" gnus-uu-decode-save t]
2121               ["Binhex" gnus-uu-decode-binhex t]
2122               ["Postscript" gnus-uu-decode-postscript t])
2123              ("Cache"
2124               ["Enter article" gnus-cache-enter-article t]
2125               ["Remove article" gnus-cache-remove-article t])
2126              ["Translate" gnus-article-babel t]
2127              ["Select article buffer" gnus-summary-select-article-buffer t]
2128              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2129              ["Isearch article..." gnus-summary-isearch-article t]
2130              ["Beginning of the article" gnus-summary-beginning-of-article t]
2131              ["End of the article" gnus-summary-end-of-article t]
2132              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2133              ["Fetch referenced articles" gnus-summary-refer-references t]
2134              ["Fetch current thread" gnus-summary-refer-thread t]
2135              ["Fetch article with id..." gnus-summary-refer-article t]
2136              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2137              ["Redisplay" gnus-summary-show-article t]
2138              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2139       (easy-menu-define
2140        gnus-summary-article-menu gnus-summary-mode-map ""
2141        (cons "Article" innards))
2142
2143       (if (not (keymapp gnus-summary-article-menu))
2144           (easy-menu-define
2145            gnus-article-commands-menu gnus-article-mode-map ""
2146            (cons "Commands" innards))
2147         ;; in Emacs, don't share menu.
2148         (setq gnus-article-commands-menu
2149               (copy-keymap gnus-summary-article-menu))
2150         (define-key gnus-article-mode-map [menu-bar commands]
2151           (cons "Commands" gnus-article-commands-menu))))
2152
2153     (easy-menu-define
2154      gnus-summary-thread-menu gnus-summary-mode-map ""
2155      '("Threads"
2156        ["Toggle threading" gnus-summary-toggle-threads t]
2157        ["Hide threads" gnus-summary-hide-all-threads t]
2158        ["Show threads" gnus-summary-show-all-threads t]
2159        ["Hide thread" gnus-summary-hide-thread t]
2160        ["Show thread" gnus-summary-show-thread t]
2161        ["Go to next thread" gnus-summary-next-thread t]
2162        ["Go to previous thread" gnus-summary-prev-thread t]
2163        ["Go down thread" gnus-summary-down-thread t]
2164        ["Go up thread" gnus-summary-up-thread t]
2165        ["Top of thread" gnus-summary-top-thread t]
2166        ["Mark thread as read" gnus-summary-kill-thread t]
2167        ["Lower thread score" gnus-summary-lower-thread t]
2168        ["Raise thread score" gnus-summary-raise-thread t]
2169        ["Rethread current" gnus-summary-rethread-current t]))
2170
2171     (easy-menu-define
2172      gnus-summary-post-menu gnus-summary-mode-map ""
2173      `("Post"
2174        ["Send a message (mail or news)" gnus-summary-post-news
2175         ,@(if (featurep 'xemacs) '(t)
2176             '(:help "Post an article"))]
2177        ["Followup" gnus-summary-followup
2178         ,@(if (featurep 'xemacs) '(t)
2179             '(:help "Post followup to this article"))]
2180        ["Followup and yank" gnus-summary-followup-with-original
2181         ,@(if (featurep 'xemacs) '(t)
2182             '(:help "Post followup to this article, quoting its contents"))]
2183        ["Supersede article" gnus-summary-supersede-article t]
2184        ["Cancel article" gnus-summary-cancel-article
2185         ,@(if (featurep 'xemacs) '(t)
2186             '(:help "Cancel an article you posted"))]
2187        ["Reply" gnus-summary-reply t]
2188        ["Reply and yank" gnus-summary-reply-with-original t]
2189        ["Wide reply" gnus-summary-wide-reply t]
2190        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2191         ,@(if (featurep 'xemacs) '(t)
2192             '(:help "Mail a reply, quoting this article"))]
2193        ["Very wide reply" gnus-summary-very-wide-reply t]
2194        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2195         ,@(if (featurep 'xemacs) '(t)
2196             '(:help "Mail a very wide reply, quoting this article"))]
2197        ["Mail forward" gnus-summary-mail-forward t]
2198        ["Post forward" gnus-summary-post-forward t]
2199        ["Digest and mail" gnus-summary-digest-mail-forward t]
2200        ["Digest and post" gnus-summary-digest-post-forward t]
2201        ["Resend message" gnus-summary-resend-message t]
2202        ["Resend message edit" gnus-summary-resend-message-edit t]
2203        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2204        ["Send a mail" gnus-summary-mail-other-window t]
2205        ["Create a local message" gnus-summary-news-other-window t]
2206        ["Uuencode and post" gnus-uu-post-news
2207         ,@(if (featurep 'xemacs) '(t)
2208             '(:help "Post a uuencoded article"))]
2209        ["Followup via news" gnus-summary-followup-to-mail t]
2210        ["Followup via news and yank"
2211         gnus-summary-followup-to-mail-with-original t]
2212        ;;("Draft"
2213        ;;["Send" gnus-summary-send-draft t]
2214        ;;["Send bounced" gnus-resend-bounced-mail t])
2215        ))
2216
2217     (cond
2218      ((not (keymapp gnus-summary-post-menu))
2219       (setq gnus-article-post-menu gnus-summary-post-menu))
2220      ((not gnus-article-post-menu)
2221       ;; Don't share post menu.
2222       (setq gnus-article-post-menu
2223             (copy-keymap gnus-summary-post-menu))))
2224     (define-key gnus-article-mode-map [menu-bar post]
2225       (cons "Post" gnus-article-post-menu))
2226
2227     (easy-menu-define
2228      gnus-summary-misc-menu gnus-summary-mode-map ""
2229      `("Gnus"
2230        ("Mark Read"
2231         ["Mark as read" gnus-summary-mark-as-read-forward t]
2232         ["Mark same subject and select"
2233          gnus-summary-kill-same-subject-and-select t]
2234         ["Mark same subject" gnus-summary-kill-same-subject t]
2235         ["Catchup" gnus-summary-catchup
2236          ,@(if (featurep 'xemacs) '(t)
2237              '(:help "Mark unread articles in this group as read"))]
2238         ["Catchup all" gnus-summary-catchup-all t]
2239         ["Catchup to here" gnus-summary-catchup-to-here t]
2240         ["Catchup from here" gnus-summary-catchup-from-here t]
2241         ["Catchup region" gnus-summary-mark-region-as-read t]
2242         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2243        ("Mark Various"
2244         ["Tick" gnus-summary-tick-article-forward t]
2245         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2246         ["Remove marks" gnus-summary-clear-mark-forward t]
2247         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2248         ["Set bookmark" gnus-summary-set-bookmark t]
2249         ["Remove bookmark" gnus-summary-remove-bookmark t])
2250        ("Limit to"
2251         ["Marks..." gnus-summary-limit-to-marks t]
2252         ["Subject..." gnus-summary-limit-to-subject t]
2253         ["Author..." gnus-summary-limit-to-author t]
2254         ["Age..." gnus-summary-limit-to-age t]
2255         ["Extra..." gnus-summary-limit-to-extra t]
2256         ["Score" gnus-summary-limit-to-score t]
2257         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2258         ["Unread" gnus-summary-limit-to-unread t]
2259         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2260         ["Articles" gnus-summary-limit-to-articles t]
2261         ["Pop limit" gnus-summary-pop-limit t]
2262         ["Show dormant" gnus-summary-limit-include-dormant t]
2263         ["Hide childless dormant"
2264          gnus-summary-limit-exclude-childless-dormant t]
2265         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2266         ["Hide marked" gnus-summary-limit-exclude-marks t]
2267         ["Show expunged" gnus-summary-limit-include-expunged t])
2268        ("Process Mark"
2269         ["Set mark" gnus-summary-mark-as-processable t]
2270         ["Remove mark" gnus-summary-unmark-as-processable t]
2271         ["Remove all marks" gnus-summary-unmark-all-processable t]
2272         ["Mark above" gnus-uu-mark-over t]
2273         ["Mark series" gnus-uu-mark-series t]
2274         ["Mark region" gnus-uu-mark-region t]
2275         ["Unmark region" gnus-uu-unmark-region t]
2276         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2277         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2278         ["Mark all" gnus-uu-mark-all t]
2279         ["Mark buffer" gnus-uu-mark-buffer t]
2280         ["Mark sparse" gnus-uu-mark-sparse t]
2281         ["Mark thread" gnus-uu-mark-thread t]
2282         ["Unmark thread" gnus-uu-unmark-thread t]
2283         ("Process Mark Sets"
2284          ["Kill" gnus-summary-kill-process-mark t]
2285          ["Yank" gnus-summary-yank-process-mark
2286           gnus-newsgroup-process-stack]
2287          ["Save" gnus-summary-save-process-mark t]))
2288        ("Scroll article"
2289         ["Page forward" gnus-summary-next-page
2290          ,@(if (featurep 'xemacs) '(t)
2291              '(:help "Show next page of article"))]
2292         ["Page backward" gnus-summary-prev-page
2293          ,@(if (featurep 'xemacs) '(t)
2294              '(:help "Show previous page of article"))]
2295         ["Line forward" gnus-summary-scroll-up t])
2296        ("Move"
2297         ["Next unread article" gnus-summary-next-unread-article t]
2298         ["Previous unread article" gnus-summary-prev-unread-article t]
2299         ["Next article" gnus-summary-next-article t]
2300         ["Previous article" gnus-summary-prev-article t]
2301         ["Next unread subject" gnus-summary-next-unread-subject t]
2302         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2303         ["Next article same subject" gnus-summary-next-same-subject t]
2304         ["Previous article same subject" gnus-summary-prev-same-subject t]
2305         ["First unread article" gnus-summary-first-unread-article t]
2306         ["Best unread article" gnus-summary-best-unread-article t]
2307         ["Go to subject number..." gnus-summary-goto-subject t]
2308         ["Go to article number..." gnus-summary-goto-article t]
2309         ["Go to the last article" gnus-summary-goto-last-article t]
2310         ["Pop article off history" gnus-summary-pop-article t])
2311        ("Sort"
2312         ["Sort by number" gnus-summary-sort-by-number t]
2313         ["Sort by author" gnus-summary-sort-by-author t]
2314         ["Sort by subject" gnus-summary-sort-by-subject t]
2315         ["Sort by date" gnus-summary-sort-by-date t]
2316         ["Sort by score" gnus-summary-sort-by-score t]
2317         ["Sort by lines" gnus-summary-sort-by-lines t]
2318         ["Sort by characters" gnus-summary-sort-by-chars t]
2319         ["Original sort" gnus-summary-sort-by-original t])
2320        ("Help"
2321         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2322         ["Describe group" gnus-summary-describe-group t]
2323         ["Read manual" gnus-info-find-node t])
2324        ("Modes"
2325         ["Pick and read" gnus-pick-mode t]
2326         ["Binary" gnus-binary-mode t])
2327        ("Regeneration"
2328         ["Regenerate" gnus-summary-prepare t]
2329         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2330         ["Toggle threading" gnus-summary-toggle-threads t])
2331        ["See old articles" gnus-summary-insert-old-articles t]
2332        ["See new articles" gnus-summary-insert-new-articles t]
2333        ["Filter articles..." gnus-summary-execute-command t]
2334        ["Run command on subjects..." gnus-summary-universal-argument t]
2335        ["Search articles forward..." gnus-summary-search-article-forward t]
2336        ["Search articles backward..." gnus-summary-search-article-backward t]
2337        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2338        ["Expand window" gnus-summary-expand-window t]
2339        ["Expire expirable articles" gnus-summary-expire-articles
2340         (gnus-check-backend-function
2341          'request-expire-articles gnus-newsgroup-name)]
2342        ["Edit local kill file" gnus-summary-edit-local-kill t]
2343        ["Edit main kill file" gnus-summary-edit-global-kill t]
2344        ["Edit group parameters" gnus-summary-edit-parameters t]
2345        ["Customize group parameters" gnus-summary-customize-parameters t]
2346        ["Send a bug report" gnus-bug t]
2347        ("Exit"
2348         ["Catchup and exit" gnus-summary-catchup-and-exit
2349          ,@(if (featurep 'xemacs) '(t)
2350              '(:help "Mark unread articles in this group as read, then exit"))]
2351         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2352         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2353         ["Exit group" gnus-summary-exit
2354          ,@(if (featurep 'xemacs) '(t)
2355              '(:help "Exit current group, return to group selection mode"))]
2356         ["Exit group without updating" gnus-summary-exit-no-update t]
2357         ["Exit and goto next group" gnus-summary-next-group t]
2358         ["Exit and goto prev group" gnus-summary-prev-group t]
2359         ["Reselect group" gnus-summary-reselect-current-group t]
2360         ["Rescan group" gnus-summary-rescan-group t]
2361         ["Update dribble" gnus-summary-save-newsrc t])))
2362
2363     (gnus-run-hooks 'gnus-summary-menu-hook)))
2364
2365 (defvar gnus-summary-tool-bar-map nil)
2366
2367 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2368 (defun gnus-summary-make-tool-bar ()
2369   (if (and (fboundp 'tool-bar-add-item-from-menu)
2370            (default-value 'tool-bar-mode)
2371            (not gnus-summary-tool-bar-map))
2372       (setq gnus-summary-tool-bar-map
2373             (let ((tool-bar-map (make-sparse-keymap))
2374                   (load-path (mm-image-load-path)))
2375               (tool-bar-add-item-from-menu
2376                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2377               (tool-bar-add-item-from-menu
2378                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2379               (tool-bar-add-item-from-menu
2380                'gnus-summary-post-news "post" gnus-summary-mode-map)
2381               (tool-bar-add-item-from-menu
2382                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2383               (tool-bar-add-item-from-menu
2384                'gnus-summary-followup "followup" gnus-summary-mode-map)
2385               (tool-bar-add-item-from-menu
2386                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2387               (tool-bar-add-item-from-menu
2388                'gnus-summary-reply "reply" gnus-summary-mode-map)
2389               (tool-bar-add-item-from-menu
2390                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2391               (tool-bar-add-item-from-menu
2392                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2393               (tool-bar-add-item-from-menu
2394                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2395               (tool-bar-add-item-from-menu
2396                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2397               (tool-bar-add-item-from-menu
2398                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2405               tool-bar-map)))
2406   (if gnus-summary-tool-bar-map
2407       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2408
2409 (defun gnus-score-set-default (var value)
2410   "A version of set that updates the GNU Emacs menu-bar."
2411   (set var value)
2412   ;; It is the message that forces the active status to be updated.
2413   (message ""))
2414
2415 (defun gnus-make-score-map (type)
2416   "Make a summary score map of type TYPE."
2417   (if t
2418       nil
2419     (let ((headers '(("author" "from" string)
2420                      ("subject" "subject" string)
2421                      ("article body" "body" string)
2422                      ("article head" "head" string)
2423                      ("xref" "xref" string)
2424                      ("extra header" "extra" string)
2425                      ("lines" "lines" number)
2426                      ("followups to author" "followup" string)))
2427           (types '((number ("less than" <)
2428                            ("greater than" >)
2429                            ("equal" =))
2430                    (string ("substring" s)
2431                            ("exact string" e)
2432                            ("fuzzy string" f)
2433                            ("regexp" r))))
2434           (perms '(("temporary" (current-time-string))
2435                    ("permanent" nil)
2436                    ("immediate" now)))
2437           header)
2438       (list
2439        (apply
2440         'nconc
2441         (list
2442          (if (eq type 'lower)
2443              "Lower score"
2444            "Increase score"))
2445         (let (outh)
2446           (while headers
2447             (setq header (car headers))
2448             (setq outh
2449                   (cons
2450                    (apply
2451                     'nconc
2452                     (list (car header))
2453                     (let ((ts (cdr (assoc (nth 2 header) types)))
2454                           outt)
2455                       (while ts
2456                         (setq outt
2457                               (cons
2458                                (apply
2459                                 'nconc
2460                                 (list (caar ts))
2461                                 (let ((ps perms)
2462                                       outp)
2463                                   (while ps
2464                                     (setq outp
2465                                           (cons
2466                                            (vector
2467                                             (caar ps)
2468                                             (list
2469                                              'gnus-summary-score-entry
2470                                              (nth 1 header)
2471                                              (if (or (string= (nth 1 header)
2472                                                               "head")
2473                                                      (string= (nth 1 header)
2474                                                               "body"))
2475                                                  ""
2476                                                (list 'gnus-summary-header
2477                                                      (nth 1 header)))
2478                                              (list 'quote (nth 1 (car ts)))
2479                                              (list 'gnus-score-delta-default
2480                                                    nil)
2481                                              (nth 1 (car ps))
2482                                              t)
2483                                             t)
2484                                            outp))
2485                                     (setq ps (cdr ps)))
2486                                   (list (nreverse outp))))
2487                                outt))
2488                         (setq ts (cdr ts)))
2489                       (list (nreverse outt))))
2490                    outh))
2491             (setq headers (cdr headers)))
2492           (list (nreverse outh))))))))
2493
2494 \f
2495
2496 (defun gnus-summary-mode (&optional group)
2497   "Major mode for reading articles.
2498
2499 All normal editing commands are switched off.
2500 \\<gnus-summary-mode-map>
2501 Each line in this buffer represents one article.  To read an
2502 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2503 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2504 respectively.
2505
2506 You can also post articles and send mail from this buffer.  To
2507 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2508 of an article, type `\\[gnus-summary-reply]'.
2509
2510 There are approx. one gazillion commands you can execute in this
2511 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2512
2513 The following commands are available:
2514
2515 \\{gnus-summary-mode-map}"
2516   (interactive)
2517   (kill-all-local-variables)
2518   (when (gnus-visual-p 'summary-menu 'menu)
2519     (gnus-summary-make-menu-bar)
2520     (gnus-summary-make-tool-bar))
2521   (gnus-summary-make-local-variables)
2522   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2523     (gnus-summary-make-local-variables))
2524   (gnus-make-thread-indent-array)
2525   (gnus-simplify-mode-line)
2526   (setq major-mode 'gnus-summary-mode)
2527   (setq mode-name "Summary")
2528   (make-local-variable 'minor-mode-alist)
2529   (use-local-map gnus-summary-mode-map)
2530   (buffer-disable-undo)
2531   (setq buffer-read-only t)             ;Disable modification
2532   (setq truncate-lines t)
2533   (setq selective-display t)
2534   (setq selective-display-ellipses t)   ;Display `...'
2535   (gnus-summary-set-display-table)
2536   (gnus-set-default-directory)
2537   (setq gnus-newsgroup-name group)
2538   (unless (gnus-news-group-p group)
2539     (setq gnus-newsgroup-incorporated
2540           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2541   (make-local-variable 'gnus-summary-line-format)
2542   (make-local-variable 'gnus-summary-line-format-spec)
2543   (make-local-variable 'gnus-summary-dummy-line-format)
2544   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2545   (make-local-variable 'gnus-summary-mark-positions)
2546   (make-local-hook 'pre-command-hook)
2547   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2548   (gnus-run-hooks 'gnus-summary-mode-hook)
2549   (turn-on-gnus-mailing-list-mode)
2550   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2551   (gnus-update-summary-mark-positions))
2552
2553 (defun gnus-summary-make-local-variables ()
2554   "Make all the local summary buffer variables."
2555   (let (global)
2556     (dolist (local gnus-summary-local-variables)
2557       (if (consp local)
2558           (progn
2559             (if (eq (cdr local) 'global)
2560                 ;; Copy the global value of the variable.
2561                 (setq global (symbol-value (car local)))
2562               ;; Use the value from the list.
2563               (setq global (eval (cdr local))))
2564             (set (make-local-variable (car local)) global))
2565         ;; Simple nil-valued local variable.
2566         (set (make-local-variable local) nil)))))
2567
2568 (defun gnus-summary-clear-local-variables ()
2569   (let ((locals gnus-summary-local-variables))
2570     (while locals
2571       (if (consp (car locals))
2572           (and (vectorp (caar locals))
2573                (set (caar locals) nil))
2574         (and (vectorp (car locals))
2575              (set (car locals) nil)))
2576       (setq locals (cdr locals)))))
2577
2578 ;; Summary data functions.
2579
2580 (defmacro gnus-data-number (data)
2581   `(car ,data))
2582
2583 (defmacro gnus-data-set-number (data number)
2584   `(setcar ,data ,number))
2585
2586 (defmacro gnus-data-mark (data)
2587   `(nth 1 ,data))
2588
2589 (defmacro gnus-data-set-mark (data mark)
2590   `(setcar (nthcdr 1 ,data) ,mark))
2591
2592 (defmacro gnus-data-pos (data)
2593   `(nth 2 ,data))
2594
2595 (defmacro gnus-data-set-pos (data pos)
2596   `(setcar (nthcdr 2 ,data) ,pos))
2597
2598 (defmacro gnus-data-header (data)
2599   `(nth 3 ,data))
2600
2601 (defmacro gnus-data-set-header (data header)
2602   `(setcar (nthcdr 3 ,data) ,header))
2603
2604 (defmacro gnus-data-level (data)
2605   `(nth 4 ,data))
2606
2607 (defmacro gnus-data-unread-p (data)
2608   `(= (nth 1 ,data) gnus-unread-mark))
2609
2610 (defmacro gnus-data-read-p (data)
2611   `(/= (nth 1 ,data) gnus-unread-mark))
2612
2613 (defmacro gnus-data-pseudo-p (data)
2614   `(consp (nth 3 ,data)))
2615
2616 (defmacro gnus-data-find (number)
2617   `(assq ,number gnus-newsgroup-data))
2618
2619 (defmacro gnus-data-find-list (number &optional data)
2620   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2621      (memq (assq ,number bdata)
2622            bdata)))
2623
2624 (defmacro gnus-data-make (number mark pos header level)
2625   `(list ,number ,mark ,pos ,header ,level))
2626
2627 (defun gnus-data-enter (after-article number mark pos header level offset)
2628   (let ((data (gnus-data-find-list after-article)))
2629     (unless data
2630       (error "No such article: %d" after-article))
2631     (setcdr data (cons (gnus-data-make number mark pos header level)
2632                        (cdr data)))
2633     (setq gnus-newsgroup-data-reverse nil)
2634     (gnus-data-update-list (cddr data) offset)))
2635
2636 (defun gnus-data-enter-list (after-article list &optional offset)
2637   (when list
2638     (let ((data (and after-article (gnus-data-find-list after-article)))
2639           (ilist list))
2640       (if (not (or data
2641                    after-article))
2642           (let ((odata gnus-newsgroup-data))
2643             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2644             (when offset
2645               (gnus-data-update-list odata offset)))
2646         ;; Find the last element in the list to be spliced into the main
2647         ;; list.
2648         (while (cdr list)
2649           (setq list (cdr list)))
2650         (if (not data)
2651             (progn
2652               (setcdr list gnus-newsgroup-data)
2653               (setq gnus-newsgroup-data ilist)
2654               (when offset
2655                 (gnus-data-update-list (cdr list) offset)))
2656           (setcdr list (cdr data))
2657           (setcdr data ilist)
2658           (when offset
2659             (gnus-data-update-list (cdr list) offset))))
2660       (setq gnus-newsgroup-data-reverse nil))))
2661
2662 (defun gnus-data-remove (article &optional offset)
2663   (let ((data gnus-newsgroup-data))
2664     (if (= (gnus-data-number (car data)) article)
2665         (progn
2666           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2667                 gnus-newsgroup-data-reverse nil)
2668           (when offset
2669             (gnus-data-update-list gnus-newsgroup-data offset)))
2670       (while (cdr data)
2671         (when (= (gnus-data-number (cadr data)) article)
2672           (setcdr data (cddr data))
2673           (when offset
2674             (gnus-data-update-list (cdr data) offset))
2675           (setq data nil
2676                 gnus-newsgroup-data-reverse nil))
2677         (setq data (cdr data))))))
2678
2679 (defmacro gnus-data-list (backward)
2680   `(if ,backward
2681        (or gnus-newsgroup-data-reverse
2682            (setq gnus-newsgroup-data-reverse
2683                  (reverse gnus-newsgroup-data)))
2684      gnus-newsgroup-data))
2685
2686 (defun gnus-data-update-list (data offset)
2687   "Add OFFSET to the POS of all data entries in DATA."
2688   (setq gnus-newsgroup-data-reverse nil)
2689   (while data
2690     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2691     (setq data (cdr data))))
2692
2693 (defun gnus-summary-article-pseudo-p (article)
2694   "Say whether this article is a pseudo article or not."
2695   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2696
2697 (defmacro gnus-summary-article-sparse-p (article)
2698   "Say whether this article is a sparse article or not."
2699   `(memq ,article gnus-newsgroup-sparse))
2700
2701 (defmacro gnus-summary-article-ancient-p (article)
2702   "Say whether this article is a sparse article or not."
2703   `(memq ,article gnus-newsgroup-ancient))
2704
2705 (defun gnus-article-parent-p (number)
2706   "Say whether this article is a parent or not."
2707   (let ((data (gnus-data-find-list number)))
2708     (and (cdr data)                     ; There has to be an article after...
2709          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2710             (gnus-data-level (nth 1 data))))))
2711
2712 (defun gnus-article-children (number)
2713   "Return a list of all children to NUMBER."
2714   (let* ((data (gnus-data-find-list number))
2715          (level (gnus-data-level (car data)))
2716          children)
2717     (setq data (cdr data))
2718     (while (and data
2719                 (= (gnus-data-level (car data)) (1+ level)))
2720       (push (gnus-data-number (car data)) children)
2721       (setq data (cdr data)))
2722     children))
2723
2724 (defmacro gnus-summary-skip-intangible ()
2725   "If the current article is intangible, then jump to a different article."
2726   '(let ((to (get-text-property (point) 'gnus-intangible)))
2727      (and to (gnus-summary-goto-subject to))))
2728
2729 (defmacro gnus-summary-article-intangible-p ()
2730   "Say whether this article is intangible or not."
2731   '(get-text-property (point) 'gnus-intangible))
2732
2733 (defun gnus-article-read-p (article)
2734   "Say whether ARTICLE is read or not."
2735   (not (or (memq article gnus-newsgroup-marked)
2736            (memq article gnus-newsgroup-unreads)
2737            (memq article gnus-newsgroup-unselected)
2738            (memq article gnus-newsgroup-dormant))))
2739
2740 ;; Some summary mode macros.
2741
2742 (defmacro gnus-summary-article-number ()
2743   "The article number of the article on the current line.
2744 If there isn's an article number here, then we return the current
2745 article number."
2746   '(progn
2747      (gnus-summary-skip-intangible)
2748      (or (get-text-property (point) 'gnus-number)
2749          (gnus-summary-last-subject))))
2750
2751 (defmacro gnus-summary-article-header (&optional number)
2752   "Return the header of article NUMBER."
2753   `(gnus-data-header (gnus-data-find
2754                       ,(or number '(gnus-summary-article-number)))))
2755
2756 (defmacro gnus-summary-thread-level (&optional number)
2757   "Return the level of thread that starts with article NUMBER."
2758   `(if (and (eq gnus-summary-make-false-root 'dummy)
2759             (get-text-property (point) 'gnus-intangible))
2760        0
2761      (gnus-data-level (gnus-data-find
2762                        ,(or number '(gnus-summary-article-number))))))
2763
2764 (defmacro gnus-summary-article-mark (&optional number)
2765   "Return the mark of article NUMBER."
2766   `(gnus-data-mark (gnus-data-find
2767                     ,(or number '(gnus-summary-article-number)))))
2768
2769 (defmacro gnus-summary-article-pos (&optional number)
2770   "Return the position of the line of article NUMBER."
2771   `(gnus-data-pos (gnus-data-find
2772                    ,(or number '(gnus-summary-article-number)))))
2773
2774 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2775 (defmacro gnus-summary-article-subject (&optional number)
2776   "Return current subject string or nil if nothing."
2777   `(let ((headers
2778           ,(if number
2779                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2780              '(gnus-data-header (assq (gnus-summary-article-number)
2781                                       gnus-newsgroup-data)))))
2782      (and headers
2783           (vectorp headers)
2784           (mail-header-subject headers))))
2785
2786 (defmacro gnus-summary-article-score (&optional number)
2787   "Return current article score."
2788   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2789                   gnus-newsgroup-scored))
2790        gnus-summary-default-score 0))
2791
2792 (defun gnus-summary-article-children (&optional number)
2793   "Return a list of article numbers that are children of article NUMBER."
2794   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2795          (level (gnus-data-level (car data)))
2796          l children)
2797     (while (and (setq data (cdr data))
2798                 (> (setq l (gnus-data-level (car data))) level))
2799       (and (= (1+ level) l)
2800            (push (gnus-data-number (car data))
2801                  children)))
2802     (nreverse children)))
2803
2804 (defun gnus-summary-article-parent (&optional number)
2805   "Return the article number of the parent of article NUMBER."
2806   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2807                                     (gnus-data-list t)))
2808          (level (gnus-data-level (car data))))
2809     (if (zerop level)
2810         ()                              ; This is a root.
2811       ;; We search until we find an article with a level less than
2812       ;; this one.  That function has to be the parent.
2813       (while (and (setq data (cdr data))
2814                   (not (< (gnus-data-level (car data)) level))))
2815       (and data (gnus-data-number (car data))))))
2816
2817 (defun gnus-unread-mark-p (mark)
2818   "Say whether MARK is the unread mark."
2819   (= mark gnus-unread-mark))
2820
2821 (defun gnus-read-mark-p (mark)
2822   "Say whether MARK is one of the marks that mark as read.
2823 This is all marks except unread, ticked, dormant, and expirable."
2824   (not (or (= mark gnus-unread-mark)
2825            (= mark gnus-ticked-mark)
2826            (= mark gnus-dormant-mark)
2827            (= mark gnus-expirable-mark))))
2828
2829 (defmacro gnus-article-mark (number)
2830   "Return the MARK of article NUMBER.
2831 This macro should only be used when computing the mark the \"first\"
2832 time; i.e., when generating the summary lines.  After that,
2833 `gnus-summary-article-mark' should be used to examine the
2834 marks of articles."
2835   `(cond
2836     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2837     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2838     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2839     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2840     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2841     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2842     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2843     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2844            gnus-ancient-mark))))
2845
2846 ;; Saving hidden threads.
2847
2848 (defmacro gnus-save-hidden-threads (&rest forms)
2849   "Save hidden threads, eval FORMS, and restore the hidden threads."
2850   (let ((config (make-symbol "config")))
2851     `(let ((,config (gnus-hidden-threads-configuration)))
2852        (unwind-protect
2853            (save-excursion
2854              ,@forms)
2855          (gnus-restore-hidden-threads-configuration ,config)))))
2856 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2857 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2858
2859 (defun gnus-data-compute-positions ()
2860   "Compute the positions of all articles."
2861   (setq gnus-newsgroup-data-reverse nil)
2862   (let ((data gnus-newsgroup-data))
2863     (save-excursion
2864       (gnus-save-hidden-threads
2865         (gnus-summary-show-all-threads)
2866         (goto-char (point-min))
2867         (while data
2868           (while (get-text-property (point) 'gnus-intangible)
2869             (forward-line 1))
2870           (gnus-data-set-pos (car data) (+ (point) 3))
2871           (setq data (cdr data))
2872           (forward-line 1))))))
2873
2874 (defun gnus-hidden-threads-configuration ()
2875   "Return the current hidden threads configuration."
2876   (save-excursion
2877     (let (config)
2878       (goto-char (point-min))
2879       (while (search-forward "\r" nil t)
2880         (push (1- (point)) config))
2881       config)))
2882
2883 (defun gnus-restore-hidden-threads-configuration (config)
2884   "Restore hidden threads configuration from CONFIG."
2885   (save-excursion
2886     (let (point buffer-read-only)
2887       (while (setq point (pop config))
2888         (when (and (< point (point-max))
2889                    (goto-char point)
2890                    (eq (char-after) ?\n))
2891           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2892
2893 ;; Various summary mode internalish functions.
2894
2895 (defun gnus-mouse-pick-article (e)
2896   (interactive "e")
2897   (mouse-set-point e)
2898   (gnus-summary-next-page nil t))
2899
2900 (defun gnus-summary-set-display-table ()
2901   "Change the display table.
2902 Odd characters have a tendency to mess
2903 up nicely formatted displays - we make all possible glyphs
2904 display only a single character."
2905
2906   ;; We start from the standard display table, if any.
2907   (let ((table (or (copy-sequence standard-display-table)
2908                    (make-display-table)))
2909         (i 32))
2910     ;; Nix out all the control chars...
2911     (while (>= (setq i (1- i)) 0)
2912       (aset table i [??]))
2913     ;; ... but not newline and cr, of course.  (cr is necessary for the
2914     ;; selective display).
2915     (aset table ?\n nil)
2916     (aset table ?\r nil)
2917     ;; We keep TAB as well.
2918     (aset table ?\t nil)
2919     ;; We nix out any glyphs over 126 that are not set already.
2920     (let ((i 256))
2921       (while (>= (setq i (1- i)) 127)
2922         ;; Only modify if the entry is nil.
2923         (unless (aref table i)
2924           (aset table i [??]))))
2925     (setq buffer-display-table table)))
2926
2927 (defun gnus-summary-set-article-display-arrow (pos)
2928   "Update the overlay arrow to point to line at position POS."
2929   (when (and gnus-summary-display-arrow
2930              (boundp 'overlay-arrow-position)
2931              (boundp 'overlay-arrow-string))
2932     (save-excursion
2933       (goto-char pos)
2934       (beginning-of-line)
2935       (unless overlay-arrow-position
2936         (setq overlay-arrow-position (make-marker)))
2937       (setq overlay-arrow-string "=>"
2938             overlay-arrow-position (set-marker overlay-arrow-position
2939                                                (point)
2940                                                (current-buffer))))))
2941
2942 (defun gnus-summary-buffer-name (group)
2943   "Return the summary buffer name of GROUP."
2944   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2945
2946 (defun gnus-summary-setup-buffer (group)
2947   "Initialize summary buffer."
2948   (let ((buffer (gnus-summary-buffer-name group))
2949         (dead-name (concat "*Dead Summary "
2950                            (gnus-group-decoded-name group) "*")))
2951     ;; If a dead summary buffer exists, we kill it.
2952     (when (gnus-buffer-live-p dead-name)
2953       (gnus-kill-buffer dead-name))
2954     (if (get-buffer buffer)
2955         (progn
2956           (set-buffer buffer)
2957           (setq gnus-summary-buffer (current-buffer))
2958           (not gnus-newsgroup-prepared))
2959       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2960       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2961       (gnus-summary-mode group)
2962       (when gnus-carpal
2963         (gnus-carpal-setup-buffer 'summary))
2964       (unless gnus-single-article-buffer
2965         (make-local-variable 'gnus-article-buffer)
2966         (make-local-variable 'gnus-article-current)
2967         (make-local-variable 'gnus-original-article-buffer))
2968       (setq gnus-newsgroup-name group)
2969       ;; Set any local variables in the group parameters.
2970       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2971       t)))
2972
2973 (defun gnus-set-global-variables ()
2974   "Set the global equivalents of the buffer-local variables.
2975 They are set to the latest values they had.  These reflect the summary
2976 buffer that was in action when the last article was fetched."
2977   (when (eq major-mode 'gnus-summary-mode)
2978     (setq gnus-summary-buffer (current-buffer))
2979     (let ((name gnus-newsgroup-name)
2980           (marked gnus-newsgroup-marked)
2981           (unread gnus-newsgroup-unreads)
2982           (headers gnus-current-headers)
2983           (data gnus-newsgroup-data)
2984           (summary gnus-summary-buffer)
2985           (article-buffer gnus-article-buffer)
2986           (original gnus-original-article-buffer)
2987           (gac gnus-article-current)
2988           (reffed gnus-reffed-article-number)
2989           (score-file gnus-current-score-file)
2990           (default-charset gnus-newsgroup-charset)
2991           vlist)
2992       (let ((locals gnus-newsgroup-variables))
2993         (while locals
2994           (if (consp (car locals))
2995               (push (eval (caar locals)) vlist)
2996             (push (eval (car locals)) vlist))
2997           (setq locals (cdr locals)))
2998         (setq vlist (nreverse vlist)))
2999       (save-excursion
3000         (set-buffer gnus-group-buffer)
3001         (setq gnus-newsgroup-name name
3002               gnus-newsgroup-marked marked
3003               gnus-newsgroup-unreads unread
3004               gnus-current-headers headers
3005               gnus-newsgroup-data data
3006               gnus-article-current gac
3007               gnus-summary-buffer summary
3008               gnus-article-buffer article-buffer
3009               gnus-original-article-buffer original
3010               gnus-reffed-article-number reffed
3011               gnus-current-score-file score-file
3012               gnus-newsgroup-charset default-charset)
3013         (let ((locals gnus-newsgroup-variables))
3014           (while locals
3015             (if (consp (car locals))
3016                 (set (caar locals) (pop vlist))
3017               (set (car locals) (pop vlist)))
3018             (setq locals (cdr locals))))
3019         ;; The article buffer also has local variables.
3020         (when (gnus-buffer-live-p gnus-article-buffer)
3021           (set-buffer gnus-article-buffer)
3022           (setq gnus-summary-buffer summary))))))
3023
3024 (defun gnus-summary-article-unread-p (article)
3025   "Say whether ARTICLE is unread or not."
3026   (memq article gnus-newsgroup-unreads))
3027
3028 (defun gnus-summary-first-article-p (&optional article)
3029   "Return whether ARTICLE is the first article in the buffer."
3030   (if (not (setq article (or article (gnus-summary-article-number))))
3031       nil
3032     (eq article (caar gnus-newsgroup-data))))
3033
3034 (defun gnus-summary-last-article-p (&optional article)
3035   "Return whether ARTICLE is the last article in the buffer."
3036   (if (not (setq article (or article (gnus-summary-article-number))))
3037       ;; All non-existent numbers are the last article.  :-)
3038       t
3039     (not (cdr (gnus-data-find-list article)))))
3040
3041 (defun gnus-make-thread-indent-array ()
3042   (let ((n 200))
3043     (unless (and gnus-thread-indent-array
3044                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3045       (setq gnus-thread-indent-array (make-vector 201 "")
3046             gnus-thread-indent-array-level gnus-thread-indent-level)
3047       (while (>= n 0)
3048         (aset gnus-thread-indent-array n
3049               (make-string (* n gnus-thread-indent-level) ? ))
3050         (setq n (1- n))))))
3051
3052 (defun gnus-update-summary-mark-positions ()
3053   "Compute where the summary marks are to go."
3054   (save-excursion
3055     (when (gnus-buffer-exists-p gnus-summary-buffer)
3056       (set-buffer gnus-summary-buffer))
3057     (let ((gnus-replied-mark 129)
3058           (gnus-score-below-mark 130)
3059           (gnus-score-over-mark 130)
3060           (gnus-download-mark 131)
3061           (spec gnus-summary-line-format-spec)
3062           gnus-visual pos)
3063       (save-excursion
3064         (gnus-set-work-buffer)
3065         (let ((gnus-summary-line-format-spec spec)
3066               (gnus-newsgroup-downloadable '((0 . t))))
3067           (gnus-summary-insert-line
3068            (make-full-mail-header 0 "" "nobody"
3069                                   "05 Apr 2001 23:33:09 +0400"
3070                                   "" "" 0 0 "" nil)
3071            0 nil 128 t nil "" nil 1)
3072           (goto-char (point-min))
3073           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3074                                              (- (point) (point-min) 1)))))
3075           (goto-char (point-min))
3076           (push (cons 'replied (and (search-forward "\201" nil t)
3077                                     (- (point) (point-min) 1)))
3078                 pos)
3079           (goto-char (point-min))
3080           (push (cons 'score (and (search-forward "\202" nil t)
3081                                   (- (point) (point-min) 1)))
3082                 pos)
3083           (goto-char (point-min))
3084           (push (cons 'download
3085                       (and (search-forward "\203" nil t)
3086                            (- (point) (point-min) 1)))
3087                 pos)))
3088       (setq gnus-summary-mark-positions pos))))
3089
3090 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3091   "Insert a dummy root in the summary buffer."
3092   (beginning-of-line)
3093   (gnus-add-text-properties
3094    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3095    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3096
3097 (defun gnus-summary-extract-address-component (from)
3098   (or (car (funcall gnus-extract-address-components from))
3099       from))
3100
3101 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3102   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3103                                 default-mime-charset)))
3104     ;; Is it really necessary to do this next part for each summary line?
3105     ;; Luckily, doesn't seem to slow things down much.
3106     (or
3107      (and gnus-ignored-from-addresses
3108           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3109           (let ((extra-headers (mail-header-extra header))
3110                 to
3111                 newsgroups)
3112             (cond
3113              ((setq to (cdr (assq 'To extra-headers)))
3114               (concat "-> "
3115                       (inline
3116                         (gnus-summary-extract-address-component
3117                          (funcall gnus-decode-encoded-word-function to)))))
3118              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3119               (concat "=> " newsgroups)))))
3120      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3121
3122 (defun gnus-summary-insert-line (gnus-tmp-header
3123                                  gnus-tmp-level gnus-tmp-current
3124                                  gnus-tmp-unread gnus-tmp-replied
3125                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3126                                  &optional gnus-tmp-dummy gnus-tmp-score
3127                                  gnus-tmp-process)
3128   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3129          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3130          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3131          (gnus-tmp-score-char
3132           (if (or (null gnus-summary-default-score)
3133                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3134                       gnus-summary-zcore-fuzz))
3135               ?\ ;;;Whitespace
3136             (if (< gnus-tmp-score gnus-summary-default-score)
3137                 gnus-score-below-mark gnus-score-over-mark)))
3138          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3139          (gnus-tmp-replied
3140           (cond (gnus-tmp-process gnus-process-mark)
3141                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3142                  gnus-cached-mark)
3143                 (gnus-tmp-replied gnus-replied-mark)
3144                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3145                  gnus-forwarded-mark)
3146                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3147                  gnus-saved-mark)
3148                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3149                  gnus-recent-mark)
3150                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3151                  gnus-unseen-mark)
3152                 (t gnus-no-mark)))
3153          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3154          (gnus-tmp-name
3155           (cond
3156            ((string-match "<[^>]+> *$" gnus-tmp-from)
3157             (let ((beg (match-beginning 0)))
3158               (or (and (string-match "^\".+\"" gnus-tmp-from)
3159                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3160                   (substring gnus-tmp-from 0 beg))))
3161            ((string-match "(.+)" gnus-tmp-from)
3162             (substring gnus-tmp-from
3163                        (1+ (match-beginning 0)) (1- (match-end 0))))
3164            (t gnus-tmp-from)))
3165          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3166          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3167          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3168          (buffer-read-only nil))
3169     (when (string= gnus-tmp-name "")
3170       (setq gnus-tmp-name gnus-tmp-from))
3171     (unless (numberp gnus-tmp-lines)
3172       (setq gnus-tmp-lines -1))
3173     (if (= gnus-tmp-lines -1)
3174         (setq gnus-tmp-lines "?")
3175       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3176     (gnus-put-text-property-excluding-characters-with-faces
3177      (point)
3178      (progn (eval gnus-summary-line-format-spec) (point))
3179      'gnus-number gnus-tmp-number)
3180     (when (gnus-visual-p 'summary-highlight 'highlight)
3181       (forward-line -1)
3182       (gnus-run-hooks 'gnus-summary-update-hook)
3183       (forward-line 1))))
3184
3185 (defun gnus-summary-update-line (&optional dont-update)
3186   "Update summary line after change."
3187   (when (and gnus-summary-default-score
3188              (not gnus-summary-inhibit-highlight))
3189     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3190            (article (gnus-summary-article-number))
3191            (score (gnus-summary-article-score article)))
3192       (unless dont-update
3193         (if (and gnus-summary-mark-below
3194                  (< (gnus-summary-article-score)
3195                     gnus-summary-mark-below))
3196             ;; This article has a low score, so we mark it as read.
3197             (when (memq article gnus-newsgroup-unreads)
3198               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3199           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3200             ;; This article was previously marked as read on account
3201             ;; of a low score, but now it has risen, so we mark it as
3202             ;; unread.
3203             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3204         (gnus-summary-update-mark
3205          (if (or (null gnus-summary-default-score)
3206                  (<= (abs (- score gnus-summary-default-score))
3207                      gnus-summary-zcore-fuzz))
3208              ?\ ;;;Whitespace
3209            (if (< score gnus-summary-default-score)
3210                gnus-score-below-mark gnus-score-over-mark))
3211          'score))
3212       ;; Do visual highlighting.
3213       (when (gnus-visual-p 'summary-highlight 'highlight)
3214         (gnus-run-hooks 'gnus-summary-update-hook)))))
3215
3216 (defvar gnus-tmp-new-adopts nil)
3217
3218 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3219   "Return the number of articles in THREAD.
3220 This may be 0 in some cases -- if none of the articles in
3221 the thread are to be displayed."
3222   (let* ((number
3223           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3224           (cond
3225            ((not (listp thread))
3226             1)
3227            ((and (consp thread) (cdr thread))
3228             (apply
3229              '+ 1 (mapcar
3230                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3231            ((null thread)
3232             1)
3233            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3234             1)
3235            (t 0))))
3236     (when (and level (zerop level) gnus-tmp-new-adopts)
3237       (incf number
3238             (apply '+ (mapcar
3239                        'gnus-summary-number-of-articles-in-thread
3240                        gnus-tmp-new-adopts))))
3241     (if char
3242         (if (> number 1) gnus-not-empty-thread-mark
3243           gnus-empty-thread-mark)
3244       number)))
3245
3246 (defun gnus-summary-set-local-parameters (group)
3247   "Go through the local params of GROUP and set all variable specs in that list."
3248   (let ((params (gnus-group-find-parameter group))
3249         (vars '(quit-config))           ; Ignore quit-config.
3250         elem)
3251     (while params
3252       (setq elem (car params)
3253             params (cdr params))
3254       (and (consp elem)                 ; Has to be a cons.
3255            (consp (cdr elem))           ; The cdr has to be a list.
3256            (symbolp (car elem))         ; Has to be a symbol in there.
3257            (not (memq (car elem) vars))
3258            (ignore-errors               ; So we set it.
3259              (push (car elem) vars)
3260              (make-local-variable (car elem))
3261              (set (car elem) (eval (nth 1 elem))))))))
3262
3263 (defun gnus-summary-read-group (group &optional show-all no-article
3264                                       kill-buffer no-display backward
3265                                       select-articles)
3266   "Start reading news in newsgroup GROUP.
3267 If SHOW-ALL is non-nil, already read articles are also listed.
3268 If NO-ARTICLE is non-nil, no article is selected initially.
3269 If NO-DISPLAY, don't generate a summary buffer."
3270   (let (result)
3271     (while (and group
3272                 (null (setq result
3273                             (let ((gnus-auto-select-next nil))
3274                               (or (gnus-summary-read-group-1
3275                                    group show-all no-article
3276                                    kill-buffer no-display
3277                                    select-articles)
3278                                   (setq show-all nil
3279                                         select-articles nil)))))
3280                 (eq gnus-auto-select-next 'quietly))
3281       (set-buffer gnus-group-buffer)
3282       ;; The entry function called above goes to the next
3283       ;; group automatically, so we go two groups back
3284       ;; if we are searching for the previous group.
3285       (when backward
3286         (gnus-group-prev-unread-group 2))
3287       (if (not (equal group (gnus-group-group-name)))
3288           (setq group (gnus-group-group-name))
3289         (setq group nil)))
3290     result))
3291
3292 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3293   "Directly jump to the other GROUP from summary buffer.
3294 If SHOW-ALL is non-nil, already read articles are also listed."
3295   (interactive
3296    (if (eq gnus-summary-buffer (current-buffer))
3297        (list (completing-read
3298               "Group: " gnus-active-hashtb nil t
3299               (when (and gnus-newsgroup-name
3300                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3301                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3302               'gnus-group-history)
3303              current-prefix-arg)
3304      (error "%s must be invoked from a gnus summary buffer." this-command)))
3305   (unless (or (zerop (length group))
3306               (and gnus-newsgroup-name
3307                    (string-equal gnus-newsgroup-name group)))
3308     (gnus-summary-exit)
3309     (gnus-summary-read-group group show-all
3310                              gnus-dont-select-after-jump-to-other-group)))
3311
3312 (defun gnus-summary-read-group-1 (group show-all no-article
3313                                         kill-buffer no-display
3314                                         &optional select-articles)
3315   ;; Killed foreign groups can't be entered.
3316   ;;  (when (and (not (gnus-group-native-p group))
3317   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3318   ;;    (error "Dead non-native groups can't be entered"))
3319   (gnus-message 5 "Retrieving newsgroup: %s..."
3320                 (gnus-group-decoded-name group))
3321   (let* ((new-group (gnus-summary-setup-buffer group))
3322          (quit-config (gnus-group-quit-config group))
3323          (did-select (and new-group (gnus-select-newsgroup
3324                                      group show-all select-articles))))
3325     (cond
3326      ;; This summary buffer exists already, so we just select it.
3327      ((not new-group)
3328       (gnus-set-global-variables)
3329       (when kill-buffer
3330         (gnus-kill-or-deaden-summary kill-buffer))
3331       (gnus-configure-windows 'summary 'force)
3332       (gnus-set-mode-line 'summary)
3333       (gnus-summary-position-point)
3334       (message "")
3335       t)
3336      ;; We couldn't select this group.
3337      ((null did-select)
3338       (when (and (eq major-mode 'gnus-summary-mode)
3339                  (not (equal (current-buffer) kill-buffer)))
3340         (kill-buffer (current-buffer))
3341         (if (not quit-config)
3342             (progn
3343               ;; Update the info -- marks might need to be removed,
3344               ;; for instance.
3345               (gnus-summary-update-info)
3346               (set-buffer gnus-group-buffer)
3347               (gnus-group-jump-to-group group)
3348               (gnus-group-next-unread-group 1))
3349           (gnus-handle-ephemeral-exit quit-config)))
3350       (let ((grpinfo (gnus-get-info group)))
3351         (if (null (gnus-info-read grpinfo))
3352             (gnus-message 3 "Group %s contains no messages"
3353                           (gnus-group-decoded-name group))
3354           (gnus-message 3 "Can't select group")))
3355       nil)
3356      ;; The user did a `C-g' while prompting for number of articles,
3357      ;; so we exit this group.
3358      ((eq did-select 'quit)
3359       (and (eq major-mode 'gnus-summary-mode)
3360            (not (equal (current-buffer) kill-buffer))
3361            (kill-buffer (current-buffer)))
3362       (when kill-buffer
3363         (gnus-kill-or-deaden-summary kill-buffer))
3364       (if (not quit-config)
3365           (progn
3366             (set-buffer gnus-group-buffer)
3367             (gnus-group-jump-to-group group)
3368             (gnus-group-next-unread-group 1)
3369             (gnus-configure-windows 'group 'force))
3370         (gnus-handle-ephemeral-exit quit-config))
3371       ;; Finally signal the quit.
3372       (signal 'quit nil))
3373      ;; The group was successfully selected.
3374      (t
3375       (gnus-set-global-variables)
3376       ;; Save the active value in effect when the group was entered.
3377       (setq gnus-newsgroup-active
3378             (gnus-copy-sequence
3379              (gnus-active gnus-newsgroup-name)))
3380       ;; You can change the summary buffer in some way with this hook.
3381       (gnus-run-hooks 'gnus-select-group-hook)
3382       (gnus-update-format-specifications
3383        nil 'summary 'summary-mode 'summary-dummy)
3384       (gnus-update-summary-mark-positions)
3385       ;; Do score processing.
3386       (when gnus-use-scoring
3387         (gnus-possibly-score-headers))
3388       ;; Check whether to fill in the gaps in the threads.
3389       (when gnus-build-sparse-threads
3390         (gnus-build-sparse-threads))
3391       ;; Find the initial limit.
3392       (if gnus-show-threads
3393           (if show-all
3394               (let ((gnus-newsgroup-dormant nil))
3395                 (gnus-summary-initial-limit show-all))
3396             (gnus-summary-initial-limit show-all))
3397         ;; When untreaded, all articles are always shown.
3398         (setq gnus-newsgroup-limit
3399               (mapcar
3400                (lambda (header) (mail-header-number header))
3401                gnus-newsgroup-headers)))
3402       ;; Generate the summary buffer.
3403       (unless no-display
3404         (gnus-summary-prepare))
3405       (when gnus-use-trees
3406         (gnus-tree-open group)
3407         (setq gnus-summary-highlight-line-function
3408               'gnus-tree-highlight-article))
3409       ;; If the summary buffer is empty, but there are some low-scored
3410       ;; articles or some excluded dormants, we include these in the
3411       ;; buffer.
3412       (when (and (zerop (buffer-size))
3413                  (not no-display))
3414         (cond (gnus-newsgroup-dormant
3415                (gnus-summary-limit-include-dormant))
3416               ((and gnus-newsgroup-scored show-all)
3417                (gnus-summary-limit-include-expunged t))))
3418       ;; Function `gnus-apply-kill-file' must be called in this hook.
3419       (gnus-run-hooks 'gnus-apply-kill-hook)
3420       (if (and (zerop (buffer-size))
3421                (not no-display))
3422           (progn
3423             ;; This newsgroup is empty.
3424             (gnus-summary-catchup-and-exit nil t)
3425             (gnus-message 6 "No unread news")
3426             (when kill-buffer
3427               (gnus-kill-or-deaden-summary kill-buffer))
3428             ;; Return nil from this function.
3429             nil)
3430         ;; Hide conversation thread subtrees.  We cannot do this in
3431         ;; gnus-summary-prepare-hook since kill processing may not
3432         ;; work with hidden articles.
3433         (gnus-summary-maybe-hide-threads)
3434         (when kill-buffer
3435           (gnus-kill-or-deaden-summary kill-buffer))
3436         (gnus-summary-auto-select-subject)
3437         ;; Show first unread article if requested.
3438         (if (and (not no-article)
3439                  (not no-display)
3440                  gnus-newsgroup-unreads
3441                  gnus-auto-select-first)
3442             (progn
3443               (gnus-configure-windows 'summary)
3444               (let ((art (gnus-summary-article-number)))
3445                 (unless (or (memq art gnus-newsgroup-undownloaded)
3446                             (memq art gnus-newsgroup-downloadable))
3447                   (gnus-summary-goto-article art))))
3448           ;; Don't select any articles.
3449           (gnus-summary-position-point)
3450           (gnus-configure-windows 'summary 'force)
3451           (gnus-set-mode-line 'summary))
3452         (when (get-buffer-window gnus-group-buffer t)
3453           ;; Gotta use windows, because recenter does weird stuff if
3454           ;; the current buffer ain't the displayed window.
3455           (let ((owin (selected-window)))
3456             (select-window (get-buffer-window gnus-group-buffer t))
3457             (when (gnus-group-goto-group group)
3458               (recenter))
3459             (select-window owin)))
3460         ;; Mark this buffer as "prepared".
3461         (setq gnus-newsgroup-prepared t)
3462         (gnus-run-hooks 'gnus-summary-prepared-hook)
3463         t)))))
3464
3465 (defun gnus-summary-auto-select-subject ()
3466   "Select the subject line on initial group entry."
3467   (goto-char (point-min))
3468   (cond
3469    ((eq gnus-auto-select-subject 'best)
3470     (gnus-summary-best-unread-subject))
3471    ((eq gnus-auto-select-subject 'unread)
3472     (gnus-summary-first-unread-subject))
3473    ((eq gnus-auto-select-subject 'unseen)
3474     (gnus-summary-first-unseen-subject))
3475    ((eq gnus-auto-select-subject 'unseen-or-unread)
3476     (gnus-summary-first-unseen-or-unread-subject))
3477    ((eq gnus-auto-select-subject 'first)
3478     ;; Do nothing.
3479     )
3480    ((gnus-functionp gnus-auto-select-subject)
3481     (funcall gnus-auto-select-subject))))
3482
3483 (defun gnus-summary-prepare ()
3484   "Generate the summary buffer."
3485   (interactive)
3486   (let ((buffer-read-only nil))
3487     (erase-buffer)
3488     (setq gnus-newsgroup-data nil
3489           gnus-newsgroup-data-reverse nil)
3490     (gnus-run-hooks 'gnus-summary-generate-hook)
3491     ;; Generate the buffer, either with threads or without.
3492     (when gnus-newsgroup-headers
3493       (gnus-summary-prepare-threads
3494        (if gnus-show-threads
3495            (gnus-sort-gathered-threads
3496             (funcall gnus-summary-thread-gathering-function
3497                      (gnus-sort-threads
3498                       (gnus-cut-threads (gnus-make-threads)))))
3499          ;; Unthreaded display.
3500          (gnus-sort-articles gnus-newsgroup-headers))))
3501     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3502     ;; Call hooks for modifying summary buffer.
3503     (goto-char (point-min))
3504     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3505
3506 (defsubst gnus-general-simplify-subject (subject)
3507   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3508   (setq subject
3509         (cond
3510          ;; Truncate the subject.
3511          (gnus-simplify-subject-functions
3512           (gnus-map-function gnus-simplify-subject-functions subject))
3513          ((numberp gnus-summary-gather-subject-limit)
3514           (setq subject (gnus-simplify-subject-re subject))
3515           (if (> (length subject) gnus-summary-gather-subject-limit)
3516               (substring subject 0 gnus-summary-gather-subject-limit)
3517             subject))
3518          ;; Fuzzily simplify it.
3519          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3520           (gnus-simplify-subject-fuzzy subject))
3521          ;; Just remove the leading "Re:".
3522          (t
3523           (gnus-simplify-subject-re subject))))
3524
3525   (if (and gnus-summary-gather-exclude-subject
3526            (string-match gnus-summary-gather-exclude-subject subject))
3527       nil                               ; This article shouldn't be gathered
3528     subject))
3529
3530 (defun gnus-summary-simplify-subject-query ()
3531   "Query where the respool algorithm would put this article."
3532   (interactive)
3533   (gnus-summary-select-article)
3534   (message "%s"
3535            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3536
3537 (defun gnus-gather-threads-by-subject (threads)
3538   "Gather threads by looking at Subject headers."
3539   (if (not gnus-summary-make-false-root)
3540       threads
3541     (let ((hashtb (gnus-make-hashtable 1024))
3542           (prev threads)
3543           (result threads)
3544           subject hthread whole-subject)
3545       (while threads
3546         (setq subject (gnus-general-simplify-subject
3547                        (setq whole-subject (mail-header-subject
3548                                             (caar threads)))))
3549         (when subject
3550           (if (setq hthread (gnus-gethash subject hashtb))
3551               (progn
3552                 ;; We enter a dummy root into the thread, if we
3553                 ;; haven't done that already.
3554                 (unless (stringp (caar hthread))
3555                   (setcar hthread (list whole-subject (car hthread))))
3556                 ;; We add this new gathered thread to this gathered
3557                 ;; thread.
3558                 (setcdr (car hthread)
3559                         (nconc (cdar hthread) (list (car threads))))
3560                 ;; Remove it from the list of threads.
3561                 (setcdr prev (cdr threads))
3562                 (setq threads prev))
3563             ;; Enter this thread into the hash table.
3564             (gnus-sethash subject threads hashtb)))
3565         (setq prev threads)
3566         (setq threads (cdr threads)))
3567       result)))
3568
3569 (defun gnus-gather-threads-by-references (threads)
3570   "Gather threads by looking at References headers."
3571   (let ((idhashtb (gnus-make-hashtable 1024))
3572         (thhashtb (gnus-make-hashtable 1024))
3573         (prev threads)
3574         (result threads)
3575         ids references id gthread gid entered ref)
3576     (while threads
3577       (when (setq references (mail-header-references (caar threads)))
3578         (setq id (mail-header-id (caar threads))
3579               ids (inline (gnus-split-references references))
3580               entered nil)
3581         (while (setq ref (pop ids))
3582           (setq ids (delete ref ids))
3583           (if (not (setq gid (gnus-gethash ref idhashtb)))
3584               (progn
3585                 (gnus-sethash ref id idhashtb)
3586                 (gnus-sethash id threads thhashtb))
3587             (setq gthread (gnus-gethash gid thhashtb))
3588             (unless entered
3589               ;; We enter a dummy root into the thread, if we
3590               ;; haven't done that already.
3591               (unless (stringp (caar gthread))
3592                 (setcar gthread (list (mail-header-subject (caar gthread))
3593                                       (car gthread))))
3594               ;; We add this new gathered thread to this gathered
3595               ;; thread.
3596               (setcdr (car gthread)
3597                       (nconc (cdar gthread) (list (car threads)))))
3598             ;; Add it into the thread hash table.
3599             (gnus-sethash id gthread thhashtb)
3600             (setq entered t)
3601             ;; Remove it from the list of threads.
3602             (setcdr prev (cdr threads))
3603             (setq threads prev))))
3604       (setq prev threads)
3605       (setq threads (cdr threads)))
3606     result))
3607
3608 (defun gnus-sort-gathered-threads (threads)
3609   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3610   (let ((result threads))
3611     (while threads
3612       (when (stringp (caar threads))
3613         (setcdr (car threads)
3614                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3615       (setq threads (cdr threads)))
3616     result))
3617
3618 (defun gnus-thread-loop-p (root thread)
3619   "Say whether ROOT is in THREAD."
3620   (let ((stack (list thread))
3621         (infloop 0)
3622         th)
3623     (while (setq thread (pop stack))
3624       (setq th (cdr thread))
3625       (while (and th
3626                   (not (eq (caar th) root)))
3627         (pop th))
3628       (if th
3629           ;; We have found a loop.
3630           (let (ref-dep)
3631             (setcdr thread (delq (car th) (cdr thread)))
3632             (if (boundp (setq ref-dep (intern "none"
3633                                               gnus-newsgroup-dependencies)))
3634                 (setcdr (symbol-value ref-dep)
3635                         (nconc (cdr (symbol-value ref-dep))
3636                                (list (car th))))
3637               (set ref-dep (list nil (car th))))
3638             (setq infloop 1
3639                   stack nil))
3640         ;; Push all the subthreads onto the stack.
3641         (push (cdr thread) stack)))
3642     infloop))
3643
3644 (defun gnus-make-threads ()
3645   "Go through the dependency hashtb and find the roots.  Return all threads."
3646   (let (threads)
3647     (while (catch 'infloop
3648              (mapatoms
3649               (lambda (refs)
3650                 ;; Deal with self-referencing References loops.
3651                 (when (and (car (symbol-value refs))
3652                            (not (zerop
3653                                  (apply
3654                                   '+
3655                                   (mapcar
3656                                    (lambda (thread)
3657                                      (gnus-thread-loop-p
3658                                       (car (symbol-value refs)) thread))
3659                                    (cdr (symbol-value refs)))))))
3660                   (setq threads nil)
3661                   (throw 'infloop t))
3662                 (unless (car (symbol-value refs))
3663                   ;; These threads do not refer back to any other articles,
3664                   ;; so they're roots.
3665                   (setq threads (append (cdr (symbol-value refs)) threads))))
3666               gnus-newsgroup-dependencies)))
3667     threads))
3668
3669 ;; Build the thread tree.
3670 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3671   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3672
3673 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3674 if it was already present.
3675
3676 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3677 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3678 Message-IDs will be renamed to a unique Message-ID before being
3679 entered.
3680
3681 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3682   (let* ((id (mail-header-id header))
3683          (id-dep (and id (intern id dependencies)))
3684          parent-id ref ref-dep ref-header replaced)
3685     ;; Enter this `header' in the `dependencies' table.
3686     (cond
3687      ((not id-dep)
3688       (setq header nil))
3689      ;; The first two cases do the normal part: enter a new `header'
3690      ;; in the `dependencies' table.
3691      ((not (boundp id-dep))
3692       (set id-dep (list header)))
3693      ((null (car (symbol-value id-dep)))
3694       (setcar (symbol-value id-dep) header))
3695
3696      ;; From here the `header' was already present in the
3697      ;; `dependencies' table.
3698      (force-new
3699       ;; Overrides an existing entry;
3700       ;; just set the header part of the entry.
3701       (setcar (symbol-value id-dep) header)
3702       (setq replaced t))
3703
3704      ;; Renames the existing `header' to a unique Message-ID.
3705      ((not gnus-summary-ignore-duplicates)
3706       ;; An article with this Message-ID has already been seen.
3707       ;; We rename the Message-ID.
3708       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3709            (list header))
3710       (mail-header-set-id header id))
3711
3712      ;; The last case ignores an existing entry, except it adds any
3713      ;; additional Xrefs (in case the two articles came from different
3714      ;; servers.
3715      ;; Also sets `header' to `nil' meaning that the `dependencies'
3716      ;; table was *not* modified.
3717      (t
3718       (mail-header-set-xref
3719        (car (symbol-value id-dep))
3720        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3721                    "")
3722                (or (mail-header-xref header) "")))
3723       (setq header nil)))
3724
3725     (when (and header (not replaced))
3726       ;; First check that we are not creating a References loop.
3727       (setq parent-id (gnus-parent-id (mail-header-references header)))
3728       (setq ref parent-id)
3729       (while (and ref
3730                   (setq ref-dep (intern-soft ref dependencies))
3731                   (boundp ref-dep)
3732                   (setq ref-header (car (symbol-value ref-dep))))
3733         (if (string= id ref)
3734             ;; Yuk!  This is a reference loop.  Make the article be a
3735             ;; root article.
3736             (progn
3737               (mail-header-set-references (car (symbol-value id-dep)) "none")
3738               (setq ref nil)
3739               (setq parent-id nil))
3740           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3741       (setq ref-dep (intern (or parent-id "none") dependencies))
3742       (if (boundp ref-dep)
3743           (setcdr (symbol-value ref-dep)
3744                   (nconc (cdr (symbol-value ref-dep))
3745                          (list (symbol-value id-dep))))
3746         (set ref-dep (list nil (symbol-value id-dep)))))
3747     header))
3748
3749 (defun gnus-extract-message-id-from-in-reply-to (string)
3750   (if (string-match "<[^>]+>" string)
3751       (substring string (match-beginning 0) (match-end 0))
3752     nil))
3753
3754 (defun gnus-build-sparse-threads ()
3755   (let ((headers gnus-newsgroup-headers)
3756         (mail-parse-charset gnus-newsgroup-charset)
3757         (gnus-summary-ignore-duplicates t)
3758         header references generation relations
3759         subject child end new-child date)
3760     ;; First we create an alist of generations/relations, where
3761     ;; generations is how much we trust the relation, and the relation
3762     ;; is parent/child.
3763     (gnus-message 7 "Making sparse threads...")
3764     (save-excursion
3765       (nnheader-set-temp-buffer " *gnus sparse threads*")
3766       (while (setq header (pop headers))
3767         (when (and (setq references (mail-header-references header))
3768                    (not (string= references "")))
3769           (insert references)
3770           (setq child (mail-header-id header)
3771                 subject (mail-header-subject header)
3772                 date (mail-header-date header)
3773                 generation 0)
3774           (while (search-backward ">" nil t)
3775             (setq end (1+ (point)))
3776             (when (search-backward "<" nil t)
3777               (setq new-child (buffer-substring (point) end))
3778               (push (list (incf generation)
3779                           child (setq child new-child)
3780                           subject date)
3781                     relations)))
3782           (when child
3783             (push (list (1+ generation) child nil subject) relations))
3784           (erase-buffer)))
3785       (kill-buffer (current-buffer)))
3786     ;; Sort over trustworthiness.
3787     (mapcar
3788      (lambda (relation)
3789        (when (gnus-dependencies-add-header
3790               (make-full-mail-header-from-decoded-header
3791                gnus-reffed-article-number
3792                (nth 3 relation) "" (or (nth 4 relation) "")
3793                (nth 1 relation)
3794                (or (nth 2 relation) "") 0 0 "")
3795               gnus-newsgroup-dependencies nil)
3796          (push gnus-reffed-article-number gnus-newsgroup-limit)
3797          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3798          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3799                gnus-newsgroup-reads)
3800          (decf gnus-reffed-article-number)))
3801      (sort relations 'car-less-than-car))
3802     (gnus-message 7 "Making sparse threads...done")))
3803
3804 (defun gnus-build-old-threads ()
3805   ;; Look at all the articles that refer back to old articles, and
3806   ;; fetch the headers for the articles that aren't there.  This will
3807   ;; build complete threads - if the roots haven't been expired by the
3808   ;; server, that is.
3809   (let ((mail-parse-charset gnus-newsgroup-charset)
3810         id heads)
3811     (mapatoms
3812      (lambda (refs)
3813        (when (not (car (symbol-value refs)))
3814          (setq heads (cdr (symbol-value refs)))
3815          (while heads
3816            (if (memq (mail-header-number (caar heads))
3817                      gnus-newsgroup-dormant)
3818                (setq heads (cdr heads))
3819              (setq id (symbol-name refs))
3820              (while (and (setq id (gnus-build-get-header id))
3821                          (not (car (gnus-id-to-thread id)))))
3822              (setq heads nil)))))
3823      gnus-newsgroup-dependencies)))
3824
3825 ;; This function has to be called with point after the article number
3826 ;; on the beginning of the line.
3827 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3828   (let ((eol (gnus-point-at-eol))
3829         (buffer (current-buffer))
3830         header references in-reply-to)
3831
3832     ;; overview: [num subject from date id refs chars lines misc]
3833     (unwind-protect
3834         (progn
3835           (narrow-to-region (point) eol)
3836           (unless (eobp)
3837             (forward-char))
3838
3839           (setq header
3840                 (make-full-mail-header
3841                  number                         ; number
3842                  (nnheader-nov-field)           ; subject
3843                  (nnheader-nov-field)           ; from
3844                  (nnheader-nov-field)           ; date
3845                  (nnheader-nov-read-message-id) ; id
3846                  (nnheader-nov-field)           ; refs
3847                  (nnheader-nov-read-integer)    ; chars
3848                  (nnheader-nov-read-integer)    ; lines
3849                  (unless (eobp)
3850                    (if (looking-at "Xref: ")
3851                        (goto-char (match-end 0)))
3852                    (nnheader-nov-field))        ; Xref
3853                  (nnheader-nov-parse-extra))))  ; extra
3854
3855       (widen))
3856
3857     (when (and (string= references "")
3858                (setq in-reply-to (mail-header-extra header))
3859                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3860       (mail-header-set-references
3861        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3862
3863     (when gnus-alter-header-function
3864       (funcall gnus-alter-header-function header))
3865     (gnus-dependencies-add-header header dependencies force-new)))
3866
3867 (defun gnus-build-get-header (id)
3868   "Look through the buffer of NOV lines and find the header to ID.
3869 Enter this line into the dependencies hash table, and return
3870 the id of the parent article (if any)."
3871   (let ((deps gnus-newsgroup-dependencies)
3872         found header)
3873     (prog1
3874         (save-excursion
3875           (set-buffer nntp-server-buffer)
3876           (let ((case-fold-search nil))
3877             (goto-char (point-min))
3878             (while (and (not found)
3879                         (search-forward id nil t))
3880               (beginning-of-line)
3881               (setq found (looking-at
3882                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3883                                    (regexp-quote id))))
3884               (or found (beginning-of-line 2)))
3885             (when found
3886               (beginning-of-line)
3887               (and
3888                (setq header (gnus-nov-parse-line
3889                              (read (current-buffer)) deps))
3890                (gnus-parent-id (mail-header-references header))))))
3891       (when header
3892         (let ((number (mail-header-number header)))
3893           (push number gnus-newsgroup-limit)
3894           (push header gnus-newsgroup-headers)
3895           (if (memq number gnus-newsgroup-unselected)
3896               (progn
3897                 (setq gnus-newsgroup-unreads
3898                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3899                                                number))
3900                 (setq gnus-newsgroup-unselected
3901                       (delq number gnus-newsgroup-unselected)))
3902             (push number gnus-newsgroup-ancient)))))))
3903
3904 (defun gnus-build-all-threads ()
3905   "Read all the headers."
3906   (let ((gnus-summary-ignore-duplicates t)
3907         (mail-parse-charset gnus-newsgroup-charset)
3908         (dependencies gnus-newsgroup-dependencies)
3909         header article)
3910     (save-excursion
3911       (set-buffer nntp-server-buffer)
3912       (let ((case-fold-search nil))
3913         (goto-char (point-min))
3914         (while (not (eobp))
3915           (ignore-errors
3916             (setq article (read (current-buffer))
3917                   header (gnus-nov-parse-line article dependencies)))
3918           (when header
3919             (save-excursion
3920               (set-buffer gnus-summary-buffer)
3921               (push header gnus-newsgroup-headers)
3922               (if (memq (setq article (mail-header-number header))
3923                         gnus-newsgroup-unselected)
3924                   (progn
3925                     (setq gnus-newsgroup-unreads
3926                           (gnus-add-to-sorted-list
3927                            gnus-newsgroup-unreads article))
3928                     (setq gnus-newsgroup-unselected
3929                           (delq article gnus-newsgroup-unselected)))
3930                 (push article gnus-newsgroup-ancient)))
3931             (forward-line 1)))))))
3932
3933 (defun gnus-summary-update-article-line (article header)
3934   "Update the line for ARTICLE using HEADERS."
3935   (let* ((id (mail-header-id header))
3936          (thread (gnus-id-to-thread id)))
3937     (unless thread
3938       (error "Article in no thread"))
3939     ;; Update the thread.
3940     (setcar thread header)
3941     (gnus-summary-goto-subject article)
3942     (let* ((datal (gnus-data-find-list article))
3943            (data (car datal))
3944            (length (when (cdr datal)
3945                      (- (gnus-data-pos data)
3946                         (gnus-data-pos (cadr datal)))))
3947            (buffer-read-only nil)
3948            (level (gnus-summary-thread-level)))
3949       (gnus-delete-line)
3950       (gnus-summary-insert-line
3951        header level nil (gnus-article-mark article)
3952        (memq article gnus-newsgroup-replied)
3953        (memq article gnus-newsgroup-expirable)
3954        ;; Only insert the Subject string when it's different
3955        ;; from the previous Subject string.
3956        (if (and
3957             gnus-show-threads
3958             (gnus-subject-equal
3959              (condition-case ()
3960                  (mail-header-subject
3961                   (gnus-data-header
3962                    (cadr
3963                     (gnus-data-find-list
3964                      article
3965                      (gnus-data-list t)))))
3966                ;; Error on the side of excessive subjects.
3967                (error ""))
3968              (mail-header-subject header)))
3969            ""
3970          (mail-header-subject header))
3971        nil (cdr (assq article gnus-newsgroup-scored))
3972        (memq article gnus-newsgroup-processable))
3973       (when length
3974         (gnus-data-update-list
3975          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3976
3977 (defun gnus-summary-update-article (article &optional iheader)
3978   "Update ARTICLE in the summary buffer."
3979   (set-buffer gnus-summary-buffer)
3980   (let* ((header (gnus-summary-article-header article))
3981          (id (mail-header-id header))
3982          (data (gnus-data-find article))
3983          (thread (gnus-id-to-thread id))
3984          (references (mail-header-references header))
3985          (parent
3986           (gnus-id-to-thread
3987            (or (gnus-parent-id
3988                 (when (and references
3989                            (not (equal "" references)))
3990                   references))
3991                "none")))
3992          (buffer-read-only nil)
3993          (old (car thread)))
3994     (when thread
3995       (unless iheader
3996         (setcar thread nil)
3997         (when parent
3998           (delq thread parent)))
3999       (if (gnus-summary-insert-subject id header)
4000           ;; Set the (possibly) new article number in the data structure.
4001           (gnus-data-set-number data (gnus-id-to-article id))
4002         (setcar thread old)
4003         nil))))
4004
4005 (defun gnus-rebuild-thread (id &optional line)
4006   "Rebuild the thread containing ID.
4007 If LINE, insert the rebuilt thread starting on line LINE."
4008   (let ((buffer-read-only nil)
4009         old-pos current thread data)
4010     (if (not gnus-show-threads)
4011         (setq thread (list (car (gnus-id-to-thread id))))
4012       ;; Get the thread this article is part of.
4013       (setq thread (gnus-remove-thread id)))
4014     (setq old-pos (gnus-point-at-bol))
4015     (setq current (save-excursion
4016                     (and (re-search-backward "[\r\n]" nil t)
4017                          (gnus-summary-article-number))))
4018     ;; If this is a gathered thread, we have to go some re-gathering.
4019     (when (stringp (car thread))
4020       (let ((subject (car thread))
4021             roots thr)
4022         (setq thread (cdr thread))
4023         (while thread
4024           (unless (memq (setq thr (gnus-id-to-thread
4025                                    (gnus-root-id
4026                                     (mail-header-id (caar thread)))))
4027                         roots)
4028             (push thr roots))
4029           (setq thread (cdr thread)))
4030         ;; We now have all (unique) roots.
4031         (if (= (length roots) 1)
4032             ;; All the loose roots are now one solid root.
4033             (setq thread (car roots))
4034           (setq thread (cons subject (gnus-sort-threads roots))))))
4035     (let (threads)
4036       ;; We then insert this thread into the summary buffer.
4037       (when line
4038         (goto-char (point-min))
4039         (forward-line (1- line)))
4040       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4041         (if gnus-show-threads
4042             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4043           (gnus-summary-prepare-unthreaded thread))
4044         (setq data (nreverse gnus-newsgroup-data))
4045         (setq threads gnus-newsgroup-threads))
4046       ;; We splice the new data into the data structure.
4047       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4048       ;;!!! then we want to insert at the beginning of the buffer.
4049       ;;!!! That happens to be true with Gnus now, but that may
4050       ;;!!! change in the future.  Perhaps.
4051       (gnus-data-enter-list
4052        (if line nil current) data (- (point) old-pos))
4053       (setq gnus-newsgroup-threads
4054             (nconc threads gnus-newsgroup-threads))
4055       (gnus-data-compute-positions))))
4056
4057 (defun gnus-number-to-header (number)
4058   "Return the header for article NUMBER."
4059   (let ((headers gnus-newsgroup-headers))
4060     (while (and headers
4061                 (not (= number (mail-header-number (car headers)))))
4062       (pop headers))
4063     (when headers
4064       (car headers))))
4065
4066 (defun gnus-parent-headers (in-headers &optional generation)
4067   "Return the headers of the GENERATIONeth parent of HEADERS."
4068   (unless generation
4069     (setq generation 1))
4070   (let ((parent t)
4071         (headers in-headers)
4072         references)
4073     (while (and parent
4074                 (not (zerop generation))
4075                 (setq references (mail-header-references headers)))
4076       (setq headers (if (and references
4077                              (setq parent (gnus-parent-id references)))
4078                         (car (gnus-id-to-thread parent))
4079                       nil))
4080       (decf generation))
4081     (and (not (eq headers in-headers))
4082          headers)))
4083
4084 (defun gnus-id-to-thread (id)
4085   "Return the (sub-)thread where ID appears."
4086   (gnus-gethash id gnus-newsgroup-dependencies))
4087
4088 (defun gnus-id-to-article (id)
4089   "Return the article number of ID."
4090   (let ((thread (gnus-id-to-thread id)))
4091     (when (and thread
4092                (car thread))
4093       (mail-header-number (car thread)))))
4094
4095 (defun gnus-id-to-header (id)
4096   "Return the article headers of ID."
4097   (car (gnus-id-to-thread id)))
4098
4099 (defun gnus-article-displayed-root-p (article)
4100   "Say whether ARTICLE is a root(ish) article."
4101   (let ((level (gnus-summary-thread-level article))
4102         (refs (mail-header-references  (gnus-summary-article-header article)))
4103         particle)
4104     (cond
4105      ((null level) nil)
4106      ((zerop level) t)
4107      ((null refs) t)
4108      ((null (gnus-parent-id refs)) t)
4109      ((and (= 1 level)
4110            (null (setq particle (gnus-id-to-article
4111                                  (gnus-parent-id refs))))
4112            (null (gnus-summary-thread-level particle)))))))
4113
4114 (defun gnus-root-id (id)
4115   "Return the id of the root of the thread where ID appears."
4116   (let (last-id prev)
4117     (while (and id (setq prev (car (gnus-id-to-thread id))))
4118       (setq last-id id
4119             id (gnus-parent-id (mail-header-references prev))))
4120     last-id))
4121
4122 (defun gnus-articles-in-thread (thread)
4123   "Return the list of articles in THREAD."
4124   (cons (mail-header-number (car thread))
4125         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4126
4127 (defun gnus-remove-thread (id &optional dont-remove)
4128   "Remove the thread that has ID in it."
4129   (let (headers thread last-id)
4130     ;; First go up in this thread until we find the root.
4131     (setq last-id (gnus-root-id id)
4132           headers (message-flatten-list (gnus-id-to-thread last-id)))
4133     ;; We have now found the real root of this thread.  It might have
4134     ;; been gathered into some loose thread, so we have to search
4135     ;; through the threads to find the thread we wanted.
4136     (let ((threads gnus-newsgroup-threads)
4137           sub)
4138       (while threads
4139         (setq sub (car threads))
4140         (if (stringp (car sub))
4141             ;; This is a gathered thread, so we look at the roots
4142             ;; below it to find whether this article is in this
4143             ;; gathered root.
4144             (progn
4145               (setq sub (cdr sub))
4146               (while sub
4147                 (when (member (caar sub) headers)
4148                   (setq thread (car threads)
4149                         threads nil
4150                         sub nil))
4151                 (setq sub (cdr sub))))
4152           ;; It's an ordinary thread, so we check it.
4153           (when (eq (car sub) (car headers))
4154             (setq thread sub
4155                   threads nil)))
4156         (setq threads (cdr threads)))
4157       ;; If this article is in no thread, then it's a root.
4158       (if thread
4159           (unless dont-remove
4160             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4161         (setq thread (gnus-id-to-thread last-id)))
4162       (when thread
4163         (prog1
4164             thread                      ; We return this thread.
4165           (unless dont-remove
4166             (if (stringp (car thread))
4167                 (progn
4168                   ;; If we use dummy roots, then we have to remove the
4169                   ;; dummy root as well.
4170                   (when (eq gnus-summary-make-false-root 'dummy)
4171                     ;; We go to the dummy root by going to
4172                     ;; the first sub-"thread", and then one line up.
4173                     (gnus-summary-goto-article
4174                      (mail-header-number (caadr thread)))
4175                     (forward-line -1)
4176                     (gnus-delete-line)
4177                     (gnus-data-compute-positions))
4178                   (setq thread (cdr thread))
4179                   (while thread
4180                     (gnus-remove-thread-1 (car thread))
4181                     (setq thread (cdr thread))))
4182               (gnus-remove-thread-1 thread))))))))
4183
4184 (defun gnus-remove-thread-1 (thread)
4185   "Remove the thread THREAD recursively."
4186   (let ((number (mail-header-number (pop thread)))
4187         d)
4188     (setq thread (reverse thread))
4189     (while thread
4190       (gnus-remove-thread-1 (pop thread)))
4191     (when (setq d (gnus-data-find number))
4192       (goto-char (gnus-data-pos d))
4193       (gnus-summary-show-thread)
4194       (gnus-data-remove
4195        number
4196        (- (gnus-point-at-bol)
4197           (prog1
4198               (1+ (gnus-point-at-eol))
4199             (gnus-delete-line)))))))
4200
4201 (defun gnus-sort-threads-1 (threads func)
4202   (sort (mapcar (lambda (thread)
4203                   (cons (car thread)
4204                         (and (cdr thread)
4205                              (gnus-sort-threads-1 (cdr thread) func))))
4206                 threads) func))
4207
4208 (defun gnus-sort-threads (threads)
4209   "Sort THREADS."
4210   (if (not gnus-thread-sort-functions)
4211       threads
4212     (gnus-message 8 "Sorting threads...")
4213     (prog1
4214         (gnus-sort-threads-1
4215          threads
4216          (gnus-make-sort-function gnus-thread-sort-functions))
4217       (gnus-message 8 "Sorting threads...done"))))
4218
4219 (defun gnus-sort-articles (articles)
4220   "Sort ARTICLES."
4221   (when gnus-article-sort-functions
4222     (gnus-message 7 "Sorting articles...")
4223     (prog1
4224         (setq gnus-newsgroup-headers
4225               (sort articles (gnus-make-sort-function
4226                               gnus-article-sort-functions)))
4227       (gnus-message 7 "Sorting articles...done"))))
4228
4229 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4230 (defmacro gnus-thread-header (thread)
4231   "Return header of first article in THREAD.
4232 Note that THREAD must never, ever be anything else than a variable -
4233 using some other form will lead to serious barfage."
4234   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4235   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4236   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4237         (vector thread) 2))
4238
4239 (defsubst gnus-article-sort-by-number (h1 h2)
4240   "Sort articles by article number."
4241   (< (mail-header-number h1)
4242      (mail-header-number h2)))
4243
4244 (defun gnus-thread-sort-by-number (h1 h2)
4245   "Sort threads by root article number."
4246   (gnus-article-sort-by-number
4247    (gnus-thread-header h1) (gnus-thread-header h2)))
4248
4249 (defsubst gnus-article-sort-by-lines (h1 h2)
4250   "Sort articles by article Lines header."
4251   (< (mail-header-lines h1)
4252      (mail-header-lines h2)))
4253
4254 (defun gnus-thread-sort-by-lines (h1 h2)
4255   "Sort threads by root article Lines header."
4256   (gnus-article-sort-by-lines
4257    (gnus-thread-header h1) (gnus-thread-header h2)))
4258
4259 (defsubst gnus-article-sort-by-chars (h1 h2)
4260   "Sort articles by octet length."
4261   (< (mail-header-chars h1)
4262      (mail-header-chars h2)))
4263
4264 (defun gnus-thread-sort-by-chars (h1 h2)
4265   "Sort threads by root article octet length."
4266   (gnus-article-sort-by-chars
4267    (gnus-thread-header h1) (gnus-thread-header h2)))
4268
4269 (defsubst gnus-article-sort-by-author (h1 h2)
4270   "Sort articles by root author."
4271   (string-lessp
4272    (let ((addr (car (mime-entity-read-field h1 'From))))
4273      (or (std11-full-name-string addr)
4274          (std11-address-string addr)
4275          ""))
4276    (let ((addr (car (mime-entity-read-field h2 'From))))
4277      (or (std11-full-name-string addr)
4278          (std11-address-string addr)
4279          ""))
4280    ))
4281
4282 (defun gnus-thread-sort-by-author (h1 h2)
4283   "Sort threads by root author."
4284   (gnus-article-sort-by-author
4285    (gnus-thread-header h1)  (gnus-thread-header h2)))
4286
4287 (defsubst gnus-article-sort-by-subject (h1 h2)
4288   "Sort articles by root subject."
4289   (string-lessp
4290    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4291    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4292
4293 (defun gnus-thread-sort-by-subject (h1 h2)
4294   "Sort threads by root subject."
4295   (gnus-article-sort-by-subject
4296    (gnus-thread-header h1) (gnus-thread-header h2)))
4297
4298 (defsubst gnus-article-sort-by-date (h1 h2)
4299   "Sort articles by root article date."
4300   (time-less-p
4301    (gnus-date-get-time (mail-header-date h1))
4302    (gnus-date-get-time (mail-header-date h2))))
4303
4304 (defun gnus-thread-sort-by-date (h1 h2)
4305   "Sort threads by root article date."
4306   (gnus-article-sort-by-date
4307    (gnus-thread-header h1) (gnus-thread-header h2)))
4308
4309 (defsubst gnus-article-sort-by-score (h1 h2)
4310   "Sort articles by root article score.
4311 Unscored articles will be counted as having a score of zero."
4312   (> (or (cdr (assq (mail-header-number h1)
4313                     gnus-newsgroup-scored))
4314          gnus-summary-default-score 0)
4315      (or (cdr (assq (mail-header-number h2)
4316                     gnus-newsgroup-scored))
4317          gnus-summary-default-score 0)))
4318
4319 (defun gnus-thread-sort-by-score (h1 h2)
4320   "Sort threads by root article score."
4321   (gnus-article-sort-by-score
4322    (gnus-thread-header h1) (gnus-thread-header h2)))
4323
4324 (defun gnus-thread-sort-by-total-score (h1 h2)
4325   "Sort threads by the sum of all scores in the thread.
4326 Unscored articles will be counted as having a score of zero."
4327   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4328
4329 (defun gnus-thread-total-score (thread)
4330   ;; This function find the total score of THREAD.
4331   (cond
4332    ((null thread)
4333     0)
4334    ((consp thread)
4335     (if (stringp (car thread))
4336         (apply gnus-thread-score-function 0
4337                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4338       (gnus-thread-total-score-1 thread)))
4339    (t
4340     (gnus-thread-total-score-1 (list thread)))))
4341
4342 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4343   "Sort threads such that the thread with the most recently arrived article comes first."
4344   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4345
4346 (defun gnus-thread-highest-number (thread)
4347   "Return the highest article number in THREAD."
4348   (apply 'max (mapcar (lambda (header)
4349                         (mail-header-number header))
4350                       (message-flatten-list thread))))
4351
4352 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4353   "Sort threads such that the thread with the most recently dated article comes first."
4354   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4355
4356 (defun gnus-thread-latest-date (thread)
4357   "Return the highest article date in THREAD."
4358   (let ((previous-time 0))
4359     (apply 'max (mapcar
4360                  (lambda (header)
4361                    (setq previous-time
4362                          (time-to-seconds
4363                           (mail-header-parse-date
4364                            (condition-case ()
4365                                (mail-header-date header)
4366                              (error previous-time))))))
4367                  (sort
4368                   (message-flatten-list thread)
4369                   (lambda (h1 h2)
4370                     (< (mail-header-number h1)
4371                        (mail-header-number h2))))))))
4372
4373 (defun gnus-thread-total-score-1 (root)
4374   ;; This function find the total score of the thread below ROOT.
4375   (setq root (car root))
4376   (apply gnus-thread-score-function
4377          (or (append
4378               (mapcar 'gnus-thread-total-score
4379                       (cdr (gnus-id-to-thread (mail-header-id root))))
4380               (when (> (mail-header-number root) 0)
4381                 (list (or (cdr (assq (mail-header-number root)
4382                                      gnus-newsgroup-scored))
4383                           gnus-summary-default-score 0))))
4384              (list gnus-summary-default-score)
4385              '(0))))
4386
4387 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4388 (defvar gnus-tmp-prev-subject nil)
4389 (defvar gnus-tmp-false-parent nil)
4390 (defvar gnus-tmp-root-expunged nil)
4391 (defvar gnus-tmp-dummy-line nil)
4392
4393 (eval-when-compile (defvar gnus-tmp-header))
4394 (defun gnus-extra-header (type &optional header)
4395   "Return the extra header of TYPE."
4396   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4397       ""))
4398
4399 (defvar gnus-tmp-thread-tree-header-string "")
4400
4401 (defcustom gnus-sum-thread-tree-root "> "
4402   "With %B spec, used for the root of a thread.
4403 If nil, use subject instead."
4404   :type 'string
4405   :group 'gnus-thread)
4406 (defcustom gnus-sum-thread-tree-single-indent ""
4407   "With %B spec, used for a thread with just one message.
4408 If nil, use subject instead."
4409   :type 'string
4410   :group 'gnus-thread)
4411 (defcustom gnus-sum-thread-tree-vertical "| "
4412   "With %B spec, used for drawing a vertical line."
4413   :type 'string
4414   :group 'gnus-thread)
4415 (defcustom gnus-sum-thread-tree-indent "  "
4416   "With %B spec, used for indenting."
4417   :type 'string
4418   :group 'gnus-thread)
4419 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4420   "With %B spec, used for a leaf with brothers."
4421   :type 'string
4422   :group 'gnus-thread)
4423 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4424   "With %B spec, used for a leaf without brothers."
4425   :type 'string
4426   :group 'gnus-thread)
4427
4428 (defun gnus-summary-prepare-threads (threads)
4429   "Prepare summary buffer from THREADS and indentation LEVEL.
4430 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4431 or a straight list of headers."
4432   (gnus-message 7 "Generating summary...")
4433
4434   (setq gnus-newsgroup-threads threads)
4435   (beginning-of-line)
4436
4437   (let ((gnus-tmp-level 0)
4438         (default-score (or gnus-summary-default-score 0))
4439         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4440         thread number subject stack state gnus-tmp-gathered beg-match
4441         new-roots gnus-tmp-new-adopts thread-end simp-subject
4442         gnus-tmp-header gnus-tmp-unread
4443         gnus-tmp-replied gnus-tmp-subject-or-nil
4444         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4445         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4446         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4447         tree-stack)
4448
4449     (setq gnus-tmp-prev-subject nil)
4450
4451     (if (vectorp (car threads))
4452         ;; If this is a straight (sic) list of headers, then a
4453         ;; threaded summary display isn't required, so we just create
4454         ;; an unthreaded one.
4455         (gnus-summary-prepare-unthreaded threads)
4456
4457       ;; Do the threaded display.
4458
4459       (while (or threads stack gnus-tmp-new-adopts new-roots)
4460
4461         (if (and (= gnus-tmp-level 0)
4462                  (or (not stack)
4463                      (= (caar stack) 0))
4464                  (not gnus-tmp-false-parent)
4465                  (or gnus-tmp-new-adopts new-roots))
4466             (if gnus-tmp-new-adopts
4467                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4468                       thread (list (car gnus-tmp-new-adopts))
4469                       gnus-tmp-header (caar thread)
4470                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4471               (when new-roots
4472                 (setq thread (list (car new-roots))
4473                       gnus-tmp-header (caar thread)
4474                       new-roots (cdr new-roots))))
4475
4476           (if threads
4477               ;; If there are some threads, we do them before the
4478               ;; threads on the stack.
4479               (setq thread threads
4480                     gnus-tmp-header (caar thread))
4481             ;; There were no current threads, so we pop something off
4482             ;; the stack.
4483             (setq state (car stack)
4484                   gnus-tmp-level (car state)
4485                   tree-stack (cadr state)
4486                   thread (caddr state)
4487                   stack (cdr stack)
4488                   gnus-tmp-header (caar thread))))
4489
4490         (setq gnus-tmp-false-parent nil)
4491         (setq gnus-tmp-root-expunged nil)
4492         (setq thread-end nil)
4493
4494         (if (stringp gnus-tmp-header)
4495             ;; The header is a dummy root.
4496             (cond
4497              ((eq gnus-summary-make-false-root 'adopt)
4498               ;; We let the first article adopt the rest.
4499               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4500                                                (cddar thread)))
4501               (setq gnus-tmp-gathered
4502                     (nconc (mapcar
4503                             (lambda (h) (mail-header-number (car h)))
4504                             (cddar thread))
4505                            gnus-tmp-gathered))
4506               (setq thread (cons (list (caar thread)
4507                                        (cadar thread))
4508                                  (cdr thread)))
4509               (setq gnus-tmp-level -1
4510                     gnus-tmp-false-parent t))
4511              ((eq gnus-summary-make-false-root 'empty)
4512               ;; We print adopted articles with empty subject fields.
4513               (setq gnus-tmp-gathered
4514                     (nconc (mapcar
4515                             (lambda (h) (mail-header-number (car h)))
4516                             (cddar thread))
4517                            gnus-tmp-gathered))
4518               (setq gnus-tmp-level -1))
4519              ((eq gnus-summary-make-false-root 'dummy)
4520               ;; We remember that we probably want to output a dummy
4521               ;; root.
4522               (setq gnus-tmp-dummy-line gnus-tmp-header)
4523               (setq gnus-tmp-prev-subject gnus-tmp-header))
4524              (t
4525               ;; We do not make a root for the gathered
4526               ;; sub-threads at all.
4527               (setq gnus-tmp-level -1)))
4528
4529           (setq number (mail-header-number gnus-tmp-header)
4530                 subject (mail-header-subject gnus-tmp-header)
4531                 simp-subject (gnus-simplify-subject-fully subject))
4532
4533           (cond
4534            ;; If the thread has changed subject, we might want to make
4535            ;; this subthread into a root.
4536            ((and (null gnus-thread-ignore-subject)
4537                  (not (zerop gnus-tmp-level))
4538                  gnus-tmp-prev-subject
4539                  (not (string= gnus-tmp-prev-subject simp-subject)))
4540             (setq new-roots (nconc new-roots (list (car thread)))
4541                   thread-end t
4542                   gnus-tmp-header nil))
4543            ;; If the article lies outside the current limit,
4544            ;; then we do not display it.
4545            ((not (memq number gnus-newsgroup-limit))
4546             (setq gnus-tmp-gathered
4547                   (nconc (mapcar
4548                           (lambda (h) (mail-header-number (car h)))
4549                           (cdar thread))
4550                          gnus-tmp-gathered))
4551             (setq gnus-tmp-new-adopts (if (cdar thread)
4552                                           (append gnus-tmp-new-adopts
4553                                                   (cdar thread))
4554                                         gnus-tmp-new-adopts)
4555                   thread-end t
4556                   gnus-tmp-header nil)
4557             (when (zerop gnus-tmp-level)
4558               (setq gnus-tmp-root-expunged t)))
4559            ;; Perhaps this article is to be marked as read?
4560            ((and gnus-summary-mark-below
4561                  (< (or (cdr (assq number gnus-newsgroup-scored))
4562                         default-score)
4563                     gnus-summary-mark-below)
4564                  ;; Don't touch sparse articles.
4565                  (not (gnus-summary-article-sparse-p number))
4566                  (not (gnus-summary-article-ancient-p number)))
4567             (setq gnus-newsgroup-unreads
4568                   (delq number gnus-newsgroup-unreads))
4569             (if gnus-newsgroup-auto-expire
4570                 (setq gnus-newsgroup-expirable
4571                       (gnus-add-to-sorted-list
4572                        gnus-newsgroup-expirable number))
4573               (push (cons number gnus-low-score-mark)
4574                     gnus-newsgroup-reads))))
4575
4576           (when gnus-tmp-header
4577             ;; We may have an old dummy line to output before this
4578             ;; article.
4579             (when (and gnus-tmp-dummy-line
4580                        (gnus-subject-equal
4581                         gnus-tmp-dummy-line
4582                         (mail-header-subject gnus-tmp-header)))
4583               (gnus-summary-insert-dummy-line
4584                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4585               (setq gnus-tmp-dummy-line nil))
4586
4587             ;; Compute the mark.
4588             (setq gnus-tmp-unread (gnus-article-mark number))
4589
4590             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4591                                   gnus-tmp-header gnus-tmp-level)
4592                   gnus-newsgroup-data)
4593
4594             ;; Actually insert the line.
4595             (setq
4596              gnus-tmp-subject-or-nil
4597              (cond
4598               ((and gnus-thread-ignore-subject
4599                     gnus-tmp-prev-subject
4600                     (not (string= gnus-tmp-prev-subject simp-subject)))
4601                subject)
4602               ((zerop gnus-tmp-level)
4603                (if (and (eq gnus-summary-make-false-root 'empty)
4604                         (memq number gnus-tmp-gathered)
4605                         gnus-tmp-prev-subject
4606                         (string= gnus-tmp-prev-subject simp-subject))
4607                    gnus-summary-same-subject
4608                  subject))
4609               (t gnus-summary-same-subject)))
4610             (if (and (eq gnus-summary-make-false-root 'adopt)
4611                      (= gnus-tmp-level 1)
4612                      (memq number gnus-tmp-gathered))
4613                 (setq gnus-tmp-opening-bracket ?\<
4614                       gnus-tmp-closing-bracket ?\>)
4615               (setq gnus-tmp-opening-bracket ?\[
4616                     gnus-tmp-closing-bracket ?\]))
4617             (setq
4618              gnus-tmp-indentation
4619              (aref gnus-thread-indent-array gnus-tmp-level)
4620              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4621              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4622                                 gnus-summary-default-score 0)
4623              gnus-tmp-score-char
4624              (if (or (null gnus-summary-default-score)
4625                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4626                          gnus-summary-zcore-fuzz))
4627                  ?\ ;;;Whitespace
4628                (if (< gnus-tmp-score gnus-summary-default-score)
4629                    gnus-score-below-mark gnus-score-over-mark))
4630              gnus-tmp-replied
4631              (cond ((memq number gnus-newsgroup-processable)
4632                     gnus-process-mark)
4633                    ((memq number gnus-newsgroup-cached)
4634                     gnus-cached-mark)
4635                    ((memq number gnus-newsgroup-replied)
4636                     gnus-replied-mark)
4637                    ((memq number gnus-newsgroup-forwarded)
4638                     gnus-forwarded-mark)
4639                    ((memq number gnus-newsgroup-saved)
4640                     gnus-saved-mark)
4641                    ((memq number gnus-newsgroup-recent)
4642                     gnus-recent-mark)
4643                    ((memq number gnus-newsgroup-unseen)
4644                     gnus-unseen-mark)
4645                    (t gnus-no-mark))
4646              gnus-tmp-from (mail-header-from gnus-tmp-header)
4647              gnus-tmp-name
4648              (cond
4649               ((string-match "<[^>]+> *$" gnus-tmp-from)
4650                (setq beg-match (match-beginning 0))
4651                (or (and (string-match "^\".+\"" gnus-tmp-from)
4652                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4653                    (substring gnus-tmp-from 0 beg-match)))
4654               ((string-match "(.+)" gnus-tmp-from)
4655                (substring gnus-tmp-from
4656                           (1+ (match-beginning 0)) (1- (match-end 0))))
4657               (t gnus-tmp-from))
4658              gnus-tmp-thread-tree-header-string
4659              (cond
4660               ((not gnus-show-threads) "")
4661               ((zerop gnus-tmp-level)
4662                (if (cdar thread)
4663                    (or gnus-sum-thread-tree-root subject)
4664                  (or gnus-sum-thread-tree-single-indent subject)))
4665               (t
4666                (concat (apply 'concat
4667                               (mapcar (lambda (item)
4668                                         (if (= item 1)
4669                                             gnus-sum-thread-tree-vertical
4670                                           gnus-sum-thread-tree-indent))
4671                                       (cdr (reverse tree-stack))))
4672                        (if (nth 1 thread)
4673                            gnus-sum-thread-tree-leaf-with-other
4674                          gnus-sum-thread-tree-single-leaf)))))
4675             (when (string= gnus-tmp-name "")
4676               (setq gnus-tmp-name gnus-tmp-from))
4677             (unless (numberp gnus-tmp-lines)
4678               (setq gnus-tmp-lines -1))
4679             (if (= gnus-tmp-lines -1)
4680                 (setq gnus-tmp-lines "?")
4681               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4682             (gnus-put-text-property
4683              (point)
4684              (progn (eval gnus-summary-line-format-spec) (point))
4685              'gnus-number number)
4686             (when gnus-visual-p
4687               (forward-line -1)
4688               (gnus-run-hooks 'gnus-summary-update-hook)
4689               (forward-line 1))
4690
4691             (setq gnus-tmp-prev-subject simp-subject)))
4692
4693         (when (nth 1 thread)
4694           (push (list (max 0 gnus-tmp-level)
4695                       (copy-list tree-stack)
4696                       (nthcdr 1 thread))
4697                 stack))
4698         (push (if (nth 1 thread) 1 0) tree-stack)
4699         (incf gnus-tmp-level)
4700         (setq threads (if thread-end nil (cdar thread)))
4701         (unless threads
4702           (setq gnus-tmp-level 0)))))
4703   (gnus-message 7 "Generating summary...done"))
4704
4705 (defun gnus-summary-prepare-unthreaded (headers)
4706   "Generate an unthreaded summary buffer based on HEADERS."
4707   (let (header number mark)
4708
4709     (beginning-of-line)
4710
4711     (while headers
4712       ;; We may have to root out some bad articles...
4713       (when (memq (setq number (mail-header-number
4714                                 (setq header (pop headers))))
4715                   gnus-newsgroup-limit)
4716         ;; Mark article as read when it has a low score.
4717         (when (and gnus-summary-mark-below
4718                    (< (or (cdr (assq number gnus-newsgroup-scored))
4719                           gnus-summary-default-score 0)
4720                       gnus-summary-mark-below)
4721                    (not (gnus-summary-article-ancient-p number)))
4722           (setq gnus-newsgroup-unreads
4723                 (delq number gnus-newsgroup-unreads))
4724           (if gnus-newsgroup-auto-expire
4725               (push number gnus-newsgroup-expirable)
4726             (push (cons number gnus-low-score-mark)
4727                   gnus-newsgroup-reads)))
4728
4729         (setq mark (gnus-article-mark number))
4730         (push (gnus-data-make number mark (1+ (point)) header 0)
4731               gnus-newsgroup-data)
4732         (gnus-summary-insert-line
4733          header 0 number
4734          mark (memq number gnus-newsgroup-replied)
4735          (memq number gnus-newsgroup-expirable)
4736          (mail-header-subject header) nil
4737          (cdr (assq number gnus-newsgroup-scored))
4738          (memq number gnus-newsgroup-processable))))))
4739
4740 (defun gnus-summary-remove-list-identifiers ()
4741   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4742   (let ((regexp (if (consp gnus-list-identifiers)
4743                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4744                   gnus-list-identifiers))
4745         changed subject)
4746     (when regexp
4747       (dolist (header gnus-newsgroup-headers)
4748         (setq subject (mail-header-subject header)
4749               changed nil)
4750         (while (string-match
4751                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4752                 subject)
4753           (setq subject
4754                 (concat (substring subject 0 (match-beginning 2))
4755                         (substring subject (match-end 0)))
4756                 changed t))
4757         (when (and changed
4758                    (string-match
4759                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4760           (setq subject
4761                 (concat (substring subject 0 (match-beginning 1))
4762                         (substring subject (match-end 1)))))
4763         (when changed
4764           (mail-header-set-subject header subject))))))
4765
4766 (defun gnus-fetch-headers (articles)
4767   "Fetch headers of ARTICLES."
4768   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4769     (gnus-message 5 "Fetching headers for %s..." name)
4770     (prog1
4771         (if (eq 'nov
4772                 (setq gnus-headers-retrieved-by
4773                       (gnus-retrieve-headers
4774                        articles gnus-newsgroup-name
4775                        ;; We might want to fetch old headers, but
4776                        ;; not if there is only 1 article.
4777                        (and (or (and
4778                                  (not (eq gnus-fetch-old-headers 'some))
4779                                  (not (numberp gnus-fetch-old-headers)))
4780                                 (> (length articles) 1))
4781                             gnus-fetch-old-headers))))
4782             (gnus-get-newsgroup-headers-xover
4783              articles nil nil gnus-newsgroup-name t)
4784           (gnus-get-newsgroup-headers))
4785       (gnus-message 5 "Fetching headers for %s...done" name))))
4786
4787 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4788   "Select newsgroup GROUP.
4789 If READ-ALL is non-nil, all articles in the group are selected.
4790 If SELECT-ARTICLES, only select those articles from GROUP."
4791   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4792          ;;!!! Dirty hack; should be removed.
4793          (gnus-summary-ignore-duplicates
4794           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4795               t
4796             gnus-summary-ignore-duplicates))
4797          (info (nth 2 entry))
4798          articles fetched-articles cached)
4799
4800     (unless (gnus-check-server
4801              (set (make-local-variable 'gnus-current-select-method)
4802                   (gnus-find-method-for-group group)))
4803       (error "Couldn't open server"))
4804
4805     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4806         (gnus-activate-group group)     ; Or we can activate it...
4807         (progn                          ; Or we bug out.
4808           (when (equal major-mode 'gnus-summary-mode)
4809             (kill-buffer (current-buffer)))
4810           (error "Couldn't activate group %s: %s"
4811                  group (gnus-status-message group))))
4812
4813     (unless (gnus-request-group group t)
4814       (when (equal major-mode 'gnus-summary-mode)
4815         (kill-buffer (current-buffer)))
4816       (error "Couldn't request group %s: %s"
4817              group (gnus-status-message group)))
4818
4819     (setq gnus-newsgroup-name group
4820           gnus-newsgroup-unselected nil
4821           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4822
4823     (let ((display (gnus-group-find-parameter group 'display)))
4824       (setq gnus-newsgroup-display
4825             (cond
4826              ((not (zerop (or (car-safe read-all) 0)))
4827               ;; The user entered the group with C-u SPC/RET, let's show
4828               ;; all articles.
4829               'gnus-not-ignore)
4830              ((eq display 'all)
4831               'gnus-not-ignore)
4832              ((arrayp display)
4833               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4834              ((numberp display)
4835               ;; The following is probably the "correct" solution, but
4836               ;; it makes Gnus fetch all headers and then limit the
4837               ;; articles (which is slow), so instead we hack the
4838               ;; select-articles parameter instead. -- Simon Josefsson
4839               ;; <jas@kth.se>
4840               ;;
4841               ;; (gnus-byte-compile
4842               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4843               ;;                         display)))))
4844               (setq select-articles
4845                     (gnus-uncompress-range
4846                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4847                              (if (> tmp 0)
4848                                  tmp
4849                                1))
4850                            (cdr (gnus-active group)))))
4851               nil)
4852              (t
4853               nil))))
4854
4855     (gnus-summary-setup-default-charset)
4856
4857     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4858     (when (gnus-virtual-group-p group)
4859       (setq cached gnus-newsgroup-cached))
4860
4861     (setq gnus-newsgroup-unreads
4862           (gnus-sorted-ndifference
4863            (gnus-sorted-ndifference gnus-newsgroup-unreads
4864                                     gnus-newsgroup-marked)
4865            gnus-newsgroup-dormant))
4866
4867     (setq gnus-newsgroup-processable nil)
4868
4869     (gnus-update-read-articles group gnus-newsgroup-unreads)
4870
4871     ;; Adjust and set lists of article marks.
4872     (when info
4873       (gnus-adjust-marked-articles info))
4874
4875     (if (setq articles select-articles)
4876         (setq gnus-newsgroup-unselected
4877               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4878       (setq articles (gnus-articles-to-read group read-all)))
4879
4880     (cond
4881      ((null articles)
4882       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4883       'quit)
4884      ((eq articles 0) nil)
4885      (t
4886       ;; Init the dependencies hash table.
4887       (setq gnus-newsgroup-dependencies
4888             (gnus-make-hashtable (length articles)))
4889       (gnus-set-global-variables)
4890       ;; Retrieve the headers and read them in.
4891       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4892
4893       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4894       (when cached
4895         (setq gnus-newsgroup-cached cached))
4896
4897       ;; Suppress duplicates?
4898       (when gnus-suppress-duplicates
4899         (gnus-dup-suppress-articles))
4900
4901       ;; Set the initial limit.
4902       (setq gnus-newsgroup-limit (copy-sequence articles))
4903       ;; Remove canceled articles from the list of unread articles.
4904       (setq fetched-articles
4905             (mapcar (lambda (headers) (mail-header-number headers))
4906                     gnus-newsgroup-headers))
4907       (setq gnus-newsgroup-articles fetched-articles)
4908       (setq gnus-newsgroup-unreads
4909             (gnus-sorted-nintersection
4910              gnus-newsgroup-unreads fetched-articles))
4911       (gnus-compute-unseen-list)
4912
4913       ;; Removed marked articles that do not exist.
4914       (gnus-update-missing-marks
4915        (gnus-sorted-difference articles fetched-articles))
4916       ;; We might want to build some more threads first.
4917       (when (and gnus-fetch-old-headers
4918                  (eq gnus-headers-retrieved-by 'nov))
4919         (if (eq gnus-fetch-old-headers 'invisible)
4920             (gnus-build-all-threads)
4921           (gnus-build-old-threads)))
4922       ;; Let the Gnus agent mark articles as read.
4923       (when gnus-agent
4924         (gnus-agent-get-undownloaded-list))
4925       ;; Remove list identifiers from subject
4926       (when gnus-list-identifiers
4927         (gnus-summary-remove-list-identifiers))
4928       ;; Check whether auto-expire is to be done in this group.
4929       (setq gnus-newsgroup-auto-expire
4930             (gnus-group-auto-expirable-p group))
4931       ;; Set up the article buffer now, if necessary.
4932       (unless gnus-single-article-buffer
4933         (gnus-article-setup-buffer))
4934       ;; First and last article in this newsgroup.
4935       (when gnus-newsgroup-headers
4936         (setq gnus-newsgroup-begin
4937               (mail-header-number (car gnus-newsgroup-headers))
4938               gnus-newsgroup-end
4939               (mail-header-number
4940                (gnus-last-element gnus-newsgroup-headers))))
4941       ;; GROUP is successfully selected.
4942       (or gnus-newsgroup-headers t)))))
4943
4944 (defun gnus-compute-unseen-list ()
4945   ;; The `seen' marks are treated specially.
4946   (if (not gnus-newsgroup-seen)
4947       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4948     (setq gnus-newsgroup-unseen
4949           (gnus-inverse-list-range-intersection
4950            gnus-newsgroup-articles gnus-newsgroup-seen))))
4951
4952 (defun gnus-summary-display-make-predicate (display)
4953   (require 'gnus-agent)
4954   (when (= (length display) 1)
4955     (setq display (car display)))
4956   (unless gnus-summary-display-cache
4957     (dolist (elem (append '((unread . unread)
4958                             (read . read)
4959                             (unseen . unseen))
4960                           gnus-article-mark-lists))
4961       (push (cons (cdr elem)
4962                   (gnus-byte-compile
4963                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4964             gnus-summary-display-cache)))
4965   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4966         (gnus-category-predicate-cache gnus-summary-display-cache))
4967     (gnus-get-predicate display)))
4968
4969 ;; Uses the dynamically bound `number' variable.
4970 (defvar number)
4971 (defun gnus-article-marked-p (type &optional article)
4972   (let ((article (or article number)))
4973     (cond
4974      ((eq type 'tick)
4975       (memq article gnus-newsgroup-marked))
4976      ((eq type 'unsend)
4977       (memq article gnus-newsgroup-unsendable))
4978      ((eq type 'undownload)
4979       (memq article gnus-newsgroup-undownloaded))
4980      ((eq type 'download)
4981       (memq article gnus-newsgroup-downloadable))
4982      ((eq type 'unread)
4983       (memq article gnus-newsgroup-unreads))
4984      ((eq type 'read)
4985       (memq article gnus-newsgroup-reads))
4986      ((eq type 'dormant)
4987       (memq article gnus-newsgroup-dormant) )
4988      ((eq type 'expire)
4989       (memq article gnus-newsgroup-expirable))
4990      ((eq type 'reply)
4991       (memq article gnus-newsgroup-replied))
4992      ((eq type 'killed)
4993       (memq article gnus-newsgroup-killed))
4994      ((eq type 'bookmark)
4995       (assq article gnus-newsgroup-bookmarks))
4996      ((eq type 'score)
4997       (assq article gnus-newsgroup-scored))
4998      ((eq type 'save)
4999       (memq article gnus-newsgroup-saved))
5000      ((eq type 'cache)
5001       (memq article gnus-newsgroup-cached))
5002      ((eq type 'forward)
5003       (memq article gnus-newsgroup-forwarded))
5004      ((eq type 'seen)
5005       (not (memq article gnus-newsgroup-unseen)))
5006      ((eq type 'recent)
5007       (memq article gnus-newsgroup-recent))
5008      (t t))))
5009
5010 (defun gnus-articles-to-read (group &optional read-all)
5011   "Find out what articles the user wants to read."
5012   (let* ((articles
5013           ;; Select all articles if `read-all' is non-nil, or if there
5014           ;; are no unread articles.
5015           (if (or read-all
5016                   (and (zerop (length gnus-newsgroup-marked))
5017                        (zerop (length gnus-newsgroup-unreads)))
5018                   ;; Fetch all if the predicate is non-nil.
5019                   gnus-newsgroup-display)
5020               ;; We want to select the headers for all the articles in
5021               ;; the group, so we select either all the active
5022               ;; articles in the group, or (if that's nil), the
5023               ;; articles in the cache.
5024               (or
5025                (gnus-uncompress-range (gnus-active group))
5026                (gnus-cache-articles-in-group group))
5027             ;; Select only the "normal" subset of articles.
5028             (gnus-sorted-nunion
5029              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5030              gnus-newsgroup-unreads)))
5031          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5032          (scored (length scored-list))
5033          (number (length articles))
5034          (marked (+ (length gnus-newsgroup-marked)
5035                     (length gnus-newsgroup-dormant)))
5036          (select
5037           (cond
5038            ((numberp read-all)
5039             read-all)
5040            ((numberp gnus-newsgroup-display)
5041             gnus-newsgroup-display)
5042            (t
5043             (condition-case ()
5044                 (cond
5045                  ((and (or (<= scored marked) (= scored number))
5046                        (numberp gnus-large-newsgroup)
5047                        (> number gnus-large-newsgroup))
5048                   (let* ((cursor-in-echo-area nil)
5049                          (initial (gnus-parameter-large-newsgroup-initial
5050                                    gnus-newsgroup-name))
5051                          (input
5052                           (read-string
5053                            (format
5054                             "How many articles from %s (%s %d): "
5055                             (gnus-limit-string
5056                              (gnus-group-decoded-name gnus-newsgroup-name)
5057                              35)
5058                             (if initial "max" "default")
5059                             number)
5060                            (if initial
5061                                (cons (number-to-string initial)
5062                                      0)))))
5063                     (if (string-match "^[ \t]*$" input) number input)))
5064                  ((and (> scored marked) (< scored number)
5065                        (> (- scored number) 20))
5066                   (let ((input
5067                          (read-string
5068                           (format "%s %s (%d scored, %d total): "
5069                                   "How many articles from"
5070                                   (gnus-group-decoded-name group)
5071                                   scored number))))
5072                     (if (string-match "^[ \t]*$" input)
5073                         number input)))
5074                  (t number))
5075               (quit
5076                (message "Quit getting the articles to read")
5077                nil))))))
5078     (setq select (if (stringp select) (string-to-number select) select))
5079     (if (or (null select) (zerop select))
5080         select
5081       (if (and (not (zerop scored)) (<= (abs select) scored))
5082           (progn
5083             (setq articles (sort scored-list '<))
5084             (setq number (length articles)))
5085         (setq articles (copy-sequence articles)))
5086
5087       (when (< (abs select) number)
5088         (if (< select 0)
5089             ;; Select the N oldest articles.
5090             (setcdr (nthcdr (1- (abs select)) articles) nil)
5091           ;; Select the N most recent articles.
5092           (setq articles (nthcdr (- number select) articles))))
5093       (setq gnus-newsgroup-unselected
5094             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5095       (when gnus-alter-articles-to-read-function
5096         (setq gnus-newsgroup-unreads
5097               (sort
5098                (funcall gnus-alter-articles-to-read-function
5099                         gnus-newsgroup-name gnus-newsgroup-unreads)
5100                '<)))
5101       articles)))
5102
5103 (defun gnus-killed-articles (killed articles)
5104   (let (out)
5105     (while articles
5106       (when (inline (gnus-member-of-range (car articles) killed))
5107         (push (car articles) out))
5108       (setq articles (cdr articles)))
5109     out))
5110
5111 (defun gnus-uncompress-marks (marks)
5112   "Uncompress the mark ranges in MARKS."
5113   (let ((uncompressed '(score bookmark))
5114         out)
5115     (while marks
5116       (if (memq (caar marks) uncompressed)
5117           (push (car marks) out)
5118         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5119       (setq marks (cdr marks)))
5120     out))
5121
5122 (defun gnus-article-mark-to-type (mark)
5123   "Return the type of MARK."
5124   (or (cadr (assq mark gnus-article-special-mark-lists))
5125       'list))
5126
5127 (defun gnus-article-unpropagatable-p (mark)
5128   "Return whether MARK should be propagated to backend."
5129   (memq mark gnus-article-unpropagated-mark-lists))
5130
5131 (defun gnus-adjust-marked-articles (info)
5132   "Set all article lists and remove all marks that are no longer valid."
5133   (let* ((marked-lists (gnus-info-marks info))
5134          (active (gnus-active (gnus-info-group info)))
5135          (min (car active))
5136          (max (cdr active))
5137          (types gnus-article-mark-lists)
5138          marks var articles article mark mark-type)
5139
5140     (dolist (marks marked-lists)
5141       (setq mark (car marks)
5142             mark-type (gnus-article-mark-to-type mark)
5143             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5144
5145       ;; We set the variable according to the type of the marks list,
5146       ;; and then adjust the marks to a subset of the active articles.
5147       (cond
5148        ;; Adjust "simple" lists.
5149        ((eq mark-type 'list)
5150         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5151         (when (memq mark '(tick dormant expire reply save))
5152           (while articles
5153             (when (or (< (setq article (pop articles)) min) (> article max))
5154               (set var (delq article (symbol-value var)))))))
5155        ;; Adjust assocs.
5156        ((eq mark-type 'tuple)
5157         (set var (setq articles (cdr marks)))
5158         (when (not (listp (cdr (symbol-value var))))
5159           (set var (list (symbol-value var))))
5160         (when (not (listp (cdr articles)))
5161           (setq articles (list articles)))
5162         (while articles
5163           (when (or (not (consp (setq article (pop articles))))
5164                     (< (car article) min)
5165                     (> (car article) max))
5166             (set var (delq article (symbol-value var))))))
5167        ;; Adjust ranges (sloppily).
5168        ((eq mark-type 'range)
5169         (cond
5170          ((eq mark 'seen)
5171           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5172           ;; It should be (seen (NUM1 . NUM2)).
5173           (when (numberp (cddr marks))
5174             (setcdr marks (list (cdr marks))))
5175           (setq articles (cdr marks))
5176           (while (and articles
5177                       (or (and (consp (car articles))
5178                                (> min (cdar articles)))
5179                           (and (numberp (car articles))
5180                                (> min (car articles)))))
5181             (pop articles))
5182           (set var articles))))))))
5183
5184 (defun gnus-update-missing-marks (missing)
5185   "Go through the list of MISSING articles and remove them from the mark lists."
5186   (when missing
5187     (let (var m)
5188       ;; Go through all types.
5189       (dolist (elem gnus-article-mark-lists)
5190         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5191           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5192           (when (symbol-value var)
5193             ;; This list has articles.  So we delete all missing
5194             ;; articles from it.
5195             (setq m missing)
5196             (while m
5197               (set var (delq (pop m) (symbol-value var))))))))))
5198
5199 (defun gnus-update-marks ()
5200   "Enter the various lists of marked articles into the newsgroup info list."
5201   (let ((types gnus-article-mark-lists)
5202         (info (gnus-get-info gnus-newsgroup-name))
5203         type list newmarked symbol delta-marks)
5204     (when info
5205       ;; Add all marks lists to the list of marks lists.
5206       (while (setq type (pop types))
5207         (setq list (symbol-value
5208                     (setq symbol
5209                           (intern (format "gnus-newsgroup-%s" (car type))))))
5210
5211         (when list
5212           ;; Get rid of the entries of the articles that have the
5213           ;; default score.
5214           (when (and (eq (cdr type) 'score)
5215                      gnus-save-score
5216                      list)
5217             (let* ((arts list)
5218                    (prev (cons nil list))
5219                    (all prev))
5220               (while arts
5221                 (if (or (not (consp (car arts)))
5222                         (= (cdar arts) gnus-summary-default-score))
5223                     (setcdr prev (cdr arts))
5224                   (setq prev arts))
5225                 (setq arts (cdr arts)))
5226               (setq list (cdr all)))))
5227
5228         (when (eq (cdr type) 'seen)
5229           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5230
5231         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5232           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5233
5234         (when (and (gnus-check-backend-function
5235                     'request-set-mark gnus-newsgroup-name)
5236                    (not (gnus-article-unpropagatable-p (cdr type))))
5237           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5238                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5239                  (add (gnus-remove-from-range
5240                        (gnus-copy-sequence list) old)))
5241             (when add
5242               (push (list add 'add (list (cdr type))) delta-marks))
5243             (when del
5244               (push (list del 'del (list (cdr type))) delta-marks))))
5245
5246         (when list
5247           (push (cons (cdr type) list) newmarked)))
5248
5249       (when delta-marks
5250         (unless (gnus-check-group gnus-newsgroup-name)
5251           (error "Can't open server for %s" gnus-newsgroup-name))
5252         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5253
5254       ;; Enter these new marks into the info of the group.
5255       (if (nthcdr 3 info)
5256           (setcar (nthcdr 3 info) newmarked)
5257         ;; Add the marks lists to the end of the info.
5258         (when newmarked
5259           (setcdr (nthcdr 2 info) (list newmarked))))
5260
5261       ;; Cut off the end of the info if there's nothing else there.
5262       (let ((i 5))
5263         (while (and (> i 2)
5264                     (not (nth i info)))
5265           (when (nthcdr (decf i) info)
5266             (setcdr (nthcdr i info) nil)))))))
5267
5268 (defun gnus-set-mode-line (where)
5269   "Set the mode line of the article or summary buffers.
5270 If WHERE is `summary', the summary mode line format will be used."
5271   ;; Is this mode line one we keep updated?
5272   (when (and (memq where gnus-updated-mode-lines)
5273              (symbol-value
5274               (intern (format "gnus-%s-mode-line-format-spec" where))))
5275     (let (mode-string)
5276       (save-excursion
5277         ;; We evaluate this in the summary buffer since these
5278         ;; variables are buffer-local to that buffer.
5279         (set-buffer gnus-summary-buffer)
5280         ;; We bind all these variables that are used in the `eval' form
5281         ;; below.
5282         (let* ((mformat (symbol-value
5283                          (intern
5284                           (format "gnus-%s-mode-line-format-spec" where))))
5285                (gnus-tmp-group-name (gnus-group-decoded-name
5286                                      gnus-newsgroup-name))
5287                (gnus-tmp-article-number (or gnus-current-article 0))
5288                (gnus-tmp-unread gnus-newsgroup-unreads)
5289                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5290                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5291                (gnus-tmp-unread-and-unselected
5292                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5293                             (zerop gnus-tmp-unselected))
5294                        "")
5295                       ((zerop gnus-tmp-unselected)
5296                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5297                       (t (format "{%d(+%d) more}"
5298                                  gnus-tmp-unread-and-unticked
5299                                  gnus-tmp-unselected))))
5300                (gnus-tmp-subject
5301                 (if (and gnus-current-headers
5302                          (vectorp gnus-current-headers))
5303                     (gnus-mode-string-quote
5304                      (mail-header-subject gnus-current-headers))
5305                   ""))
5306                bufname-length max-len
5307                gnus-tmp-header);; passed as argument to any user-format-funcs
5308           (setq mode-string (eval mformat))
5309           (setq bufname-length (if (string-match "%b" mode-string)
5310                                    (- (length
5311                                        (buffer-name
5312                                         (if (eq where 'summary)
5313                                             nil
5314                                           (get-buffer gnus-article-buffer))))
5315                                       2)
5316                                  0))
5317           (setq max-len (max 4 (if gnus-mode-non-string-length
5318                                    (- (window-width)
5319                                       gnus-mode-non-string-length
5320                                       bufname-length)
5321                                  (length mode-string))))
5322           ;; We might have to chop a bit of the string off...
5323           (when (> (length mode-string) max-len)
5324             (setq mode-string
5325                   (concat (gnus-truncate-string mode-string (- max-len 3))
5326                           "...")))
5327           ;; Pad the mode string a bit.
5328           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5329       ;; Update the mode line.
5330       (setq mode-line-buffer-identification
5331             (gnus-mode-line-buffer-identification (list mode-string)))
5332       (set-buffer-modified-p t))))
5333
5334 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5335   "Go through the HEADERS list and add all Xrefs to a hash table.
5336 The resulting hash table is returned, or nil if no Xrefs were found."
5337   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5338          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5339          (xref-hashtb (gnus-make-hashtable))
5340          start group entry number xrefs header)
5341     (while headers
5342       (setq header (pop headers))
5343       (when (and (setq xrefs (mail-header-xref header))
5344                  (not (memq (setq number (mail-header-number header))
5345                             unreads)))
5346         (setq start 0)
5347         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5348           (setq start (match-end 0))
5349           (setq group (if prefix
5350                           (concat prefix (substring xrefs (match-beginning 1)
5351                                                     (match-end 1)))
5352                         (substring xrefs (match-beginning 1) (match-end 1))))
5353           (setq number
5354                 (string-to-int (substring xrefs (match-beginning 2)
5355                                           (match-end 2))))
5356           (if (setq entry (gnus-gethash group xref-hashtb))
5357               (setcdr entry (cons number (cdr entry)))
5358             (gnus-sethash group (cons number nil) xref-hashtb)))))
5359     (and start xref-hashtb)))
5360
5361 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5362   "Look through all the headers and mark the Xrefs as read."
5363   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5364         name entry info xref-hashtb idlist method nth4)
5365     (save-excursion
5366       (set-buffer gnus-group-buffer)
5367       (when (setq xref-hashtb
5368                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5369         (mapatoms
5370          (lambda (group)
5371            (unless (string= from-newsgroup (setq name (symbol-name group)))
5372              (setq idlist (symbol-value group))
5373              ;; Dead groups are not updated.
5374              (and (prog1
5375                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5376                             info (nth 2 entry))
5377                     (when (stringp (setq nth4 (gnus-info-method info)))
5378                       (setq nth4 (gnus-server-to-method nth4))))
5379                   ;; Only do the xrefs if the group has the same
5380                   ;; select method as the group we have just read.
5381                   (or (gnus-methods-equal-p
5382                        nth4 (gnus-find-method-for-group from-newsgroup))
5383                       virtual
5384                       (equal nth4 (setq method (gnus-find-method-for-group
5385                                                 from-newsgroup)))
5386                       (and (equal (car nth4) (car method))
5387                            (equal (nth 1 nth4) (nth 1 method))))
5388                   gnus-use-cross-reference
5389                   (or (not (eq gnus-use-cross-reference t))
5390                       virtual
5391                       ;; Only do cross-references on subscribed
5392                       ;; groups, if that is what is wanted.
5393                       (<= (gnus-info-level info) gnus-level-subscribed))
5394                   (gnus-group-make-articles-read name idlist))))
5395          xref-hashtb)))))
5396
5397 (defun gnus-compute-read-articles (group articles)
5398   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5399          (info (nth 2 entry))
5400          (active (gnus-active group))
5401          ninfo)
5402     (when entry
5403       ;; First peel off all invalid article numbers.
5404       (when active
5405         (let ((ids articles)
5406               id first)
5407           (while (setq id (pop ids))
5408             (when (and first (> id (cdr active)))
5409               ;; We'll end up in this situation in one particular
5410               ;; obscure situation.  If you re-scan a group and get
5411               ;; a new article that is cross-posted to a different
5412               ;; group that has not been re-scanned, you might get
5413               ;; crossposted article that has a higher number than
5414               ;; Gnus believes possible.  So we re-activate this
5415               ;; group as well.  This might mean doing the
5416               ;; crossposting thingy will *increase* the number
5417               ;; of articles in some groups.  Tsk, tsk.
5418               (setq active (or (gnus-activate-group group) active)))
5419             (when (or (> id (cdr active))
5420                       (< id (car active)))
5421               (setq articles (delq id articles))))))
5422       ;; If the read list is nil, we init it.
5423       (if (and active
5424                (null (gnus-info-read info))
5425                (> (car active) 1))
5426           (setq ninfo (cons 1 (1- (car active))))
5427         (setq ninfo (gnus-info-read info)))
5428       ;; Then we add the read articles to the range.
5429       (gnus-add-to-range
5430        ninfo (setq articles (sort articles '<))))))
5431
5432 (defun gnus-group-make-articles-read (group articles)
5433   "Update the info of GROUP to say that ARTICLES are read."
5434   (let* ((num 0)
5435          (entry (gnus-gethash group gnus-newsrc-hashtb))
5436          (info (nth 2 entry))
5437          (active (gnus-active group))
5438          range)
5439     (when entry
5440       (setq range (gnus-compute-read-articles group articles))
5441       (save-excursion
5442         (set-buffer gnus-group-buffer)
5443         (gnus-undo-register
5444           `(progn
5445              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5446              (gnus-info-set-read ',info ',(gnus-info-read info))
5447              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5448              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5449              (gnus-group-update-group ,group t))))
5450       ;; Add the read articles to the range.
5451       (gnus-info-set-read info range)
5452       (gnus-request-set-mark group (list (list range 'add '(read))))
5453       ;; Then we have to re-compute how many unread
5454       ;; articles there are in this group.
5455       (when active
5456         (cond
5457          ((not range)
5458           (setq num (- (1+ (cdr active)) (car active))))
5459          ((not (listp (cdr range)))
5460           (setq num (- (cdr active) (- (1+ (cdr range))
5461                                        (car range)))))
5462          (t
5463           (while range
5464             (if (numberp (car range))
5465                 (setq num (1+ num))
5466               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5467             (setq range (cdr range)))
5468           (setq num (- (cdr active) num))))
5469         ;; Update the number of unread articles.
5470         (setcar entry num)
5471         ;; Update the group buffer.
5472         (gnus-group-update-group group t)))))
5473
5474 (defvar gnus-newsgroup-none-id 0)
5475
5476 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5477   (let ((cur nntp-server-buffer)
5478         (dependencies
5479          (or dependencies
5480              (save-excursion (set-buffer gnus-summary-buffer)
5481                              gnus-newsgroup-dependencies)))
5482         headers id end ref
5483         (mail-parse-charset gnus-newsgroup-charset)
5484         (mail-parse-ignored-charsets
5485          (save-excursion (condition-case nil
5486                              (set-buffer gnus-summary-buffer)
5487                            (error))
5488                          gnus-newsgroup-ignored-charsets)))
5489     (save-excursion
5490       (set-buffer nntp-server-buffer)
5491       ;; Translate all TAB characters into SPACE characters.
5492       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5493       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5494       (gnus-run-hooks 'gnus-parse-headers-hook)
5495       (let ((case-fold-search t)
5496             in-reply-to header p lines chars ctype)
5497         (goto-char (point-min))
5498         ;; Search to the beginning of the next header.  Error messages
5499         ;; do not begin with 2 or 3.
5500         (while (re-search-forward "^[23][0-9]+ " nil t)
5501           (setq id nil
5502                 ref nil)
5503           ;; This implementation of this function, with nine
5504           ;; search-forwards instead of the one re-search-forward and
5505           ;; a case (which basically was the old function) is actually
5506           ;; about twice as fast, even though it looks messier.  You
5507           ;; can't have everything, I guess.  Speed and elegance
5508           ;; doesn't always go hand in hand.
5509           (setq
5510            header
5511            (make-full-mail-header
5512             ;; Number.
5513             (prog1
5514                 (read cur)
5515               (end-of-line)
5516               (setq p (point))
5517               (narrow-to-region (point)
5518                                 (or (and (search-forward "\n.\n" nil t)
5519                                          (- (point) 2))
5520                                     (point))))
5521             ;; Subject.
5522             (progn
5523               (goto-char p)
5524               (if (search-forward "\nsubject:" nil t)
5525                   (nnheader-header-value)
5526                 "(none)"))
5527             ;; From.
5528             (progn
5529               (goto-char p)
5530               (if (search-forward "\nfrom:" nil t)
5531                   (nnheader-header-value)
5532                 "(nobody)"))
5533             ;; Date.
5534             (progn
5535               (goto-char p)
5536               (if (search-forward "\ndate:" nil t)
5537                   (nnheader-header-value) ""))
5538             ;; Message-ID.
5539             (progn
5540               (goto-char p)
5541               (setq id (if (re-search-forward
5542                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5543                            ;; We do it this way to make sure the Message-ID
5544                            ;; is (somewhat) syntactically valid.
5545                            (buffer-substring (match-beginning 1)
5546                                              (match-end 1))
5547                          ;; If there was no message-id, we just fake one
5548                          ;; to make subsequent routines simpler.
5549                          (nnheader-generate-fake-message-id))))
5550             ;; References.
5551             (progn
5552               (goto-char p)
5553               (if (search-forward "\nreferences:" nil t)
5554                   (progn
5555                     (setq end (point))
5556                     (prog1
5557                         (nnheader-header-value)
5558                       (setq ref
5559                             (buffer-substring
5560                              (progn
5561                                ;; (end-of-line)
5562                                (search-backward ">" end t)
5563                                (1+ (point)))
5564                              (progn
5565                                (search-backward "<" end t)
5566                                (point))))))
5567                 ;; Get the references from the in-reply-to header if there
5568                 ;; were no references and the in-reply-to header looks
5569                 ;; promising.
5570                 (if (and (search-forward "\nin-reply-to:" nil t)
5571                          (setq in-reply-to (nnheader-header-value))
5572                          (string-match "<[^>]+>" in-reply-to))
5573                     (let (ref2)
5574                       (setq ref (substring in-reply-to (match-beginning 0)
5575                                            (match-end 0)))
5576                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5577                         (setq ref2 (substring in-reply-to (match-beginning 0)
5578                                               (match-end 0)))
5579                         (when (> (length ref2) (length ref))
5580                           (setq ref ref2)))
5581                       ref)
5582                   (setq ref nil))))
5583             ;; Chars.
5584             (progn
5585               (goto-char p)
5586               (if (search-forward "\nchars: " nil t)
5587                   (if (numberp (setq chars (ignore-errors (read cur))))
5588                       chars -1)
5589                 -1))
5590             ;; Lines.
5591             (progn
5592               (goto-char p)
5593               (if (search-forward "\nlines: " nil t)
5594                   (if (numberp (setq lines (ignore-errors (read cur))))
5595                       lines -1)
5596                 -1))
5597             ;; Xref.
5598             (progn
5599               (goto-char p)
5600               (and (search-forward "\nxref:" nil t)
5601                    (nnheader-header-value)))
5602             ;; Extra.
5603             (when gnus-extra-headers
5604               (let ((extra gnus-extra-headers)
5605                     out)
5606                 (while extra
5607                   (goto-char p)
5608                   (when (search-forward
5609                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5610                     (push (cons (car extra) (nnheader-header-value)) out))
5611                   (pop extra))
5612                 out))))
5613           (goto-char p)
5614           (if (and (search-forward "\ncontent-type: " nil t)
5615                    (setq ctype (nnheader-header-value)))
5616               (mime-entity-set-content-type-internal
5617                header (mime-parse-Content-Type ctype)))
5618           (when (equal id ref)
5619             (setq ref nil))
5620
5621           (when gnus-alter-header-function
5622             (funcall gnus-alter-header-function header)
5623             (setq id (mail-header-id header)
5624                   ref (gnus-parent-id (mail-header-references header))))
5625
5626           (when (setq header
5627                       (gnus-dependencies-add-header
5628                        header dependencies force-new))
5629             (push header headers))
5630           (goto-char (point-max))
5631           (widen))
5632         (nreverse headers)))))
5633
5634 ;; Goes through the xover lines and returns a list of vectors
5635 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5636                                                   force-new dependencies
5637                                                   group also-fetch-heads)
5638   "Parse the news overview data in the server buffer.
5639 Return a list of headers that match SEQUENCE (see
5640 `nntp-retrieve-headers')."
5641   ;; Get the Xref when the users reads the articles since most/some
5642   ;; NNTP servers do not include Xrefs when using XOVER.
5643   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5644   (let ((mail-parse-charset gnus-newsgroup-charset)
5645         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5646         (cur nntp-server-buffer)
5647         (dependencies (or dependencies gnus-newsgroup-dependencies))
5648         (allp (cond
5649                ((eq gnus-read-all-available-headers t)
5650                 t)
5651                ((stringp gnus-read-all-available-headers)
5652                 (string-match gnus-read-all-available-headers group))
5653                (t
5654                 nil)))
5655         number headers header)
5656     (save-excursion
5657       (set-buffer nntp-server-buffer)
5658       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5659       ;; Allow the user to mangle the headers before parsing them.
5660       (gnus-run-hooks 'gnus-parse-headers-hook)
5661       (goto-char (point-min))
5662       (gnus-parse-without-error
5663         (while (and (or sequence allp)
5664                     (not (eobp)))
5665           (setq number (read cur))
5666           (when (not allp)
5667             (while (and sequence
5668                         (< (car sequence) number))
5669               (setq sequence (cdr sequence))))
5670           (when (and (or allp
5671                          (and sequence
5672                               (eq number (car sequence))))
5673                      (progn
5674                        (setq sequence (cdr sequence))
5675                        (setq header (inline
5676                                       (gnus-nov-parse-line
5677                                        number dependencies force-new)))))
5678             (push header headers))
5679           (forward-line 1)))
5680       ;; A common bug in inn is that if you have posted an article and
5681       ;; then retrieves the active file, it will answer correctly --
5682       ;; the new article is included.  However, a NOV entry for the
5683       ;; article may not have been generated yet, so this may fail.
5684       ;; We work around this problem by retrieving the last few
5685       ;; headers using HEAD.
5686       (if (or (not also-fetch-heads)
5687               (not sequence))
5688           ;; We (probably) got all the headers.
5689           (nreverse headers)
5690         (let ((gnus-nov-is-evil t))
5691           (nconc
5692            (nreverse headers)
5693            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5694              (gnus-get-newsgroup-headers))))))))
5695
5696 (defun gnus-article-get-xrefs ()
5697   "Fill in the Xref value in `gnus-current-headers', if necessary.
5698 This is meant to be called in `gnus-article-internal-prepare-hook'."
5699   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5700                                  gnus-current-headers)))
5701     (or (not gnus-use-cross-reference)
5702         (not headers)
5703         (and (mail-header-xref headers)
5704              (not (string= (mail-header-xref headers) "")))
5705         (let ((case-fold-search t)
5706               xref)
5707           (save-restriction
5708             (nnheader-narrow-to-headers)
5709             (goto-char (point-min))
5710             (when (or (and (not (eobp))
5711                            (eq (downcase (char-after)) ?x)
5712                            (looking-at "Xref:"))
5713                       (search-forward "\nXref:" nil t))
5714               (goto-char (1+ (match-end 0)))
5715               (setq xref (buffer-substring (point)
5716                                            (progn (end-of-line) (point))))
5717               (mail-header-set-xref headers xref)))))))
5718
5719 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5720   "Find article ID and insert the summary line for that article.
5721 OLD-HEADER can either be a header or a line number to insert
5722 the subject line on."
5723   (let* ((line (and (numberp old-header) old-header))
5724          (old-header (and (vectorp old-header) old-header))
5725          (header (cond ((and old-header use-old-header)
5726                         old-header)
5727                        ((and (numberp id)
5728                              (gnus-number-to-header id))
5729                         (gnus-number-to-header id))
5730                        (t
5731                         (gnus-read-header id))))
5732          (number (and (numberp id) id))
5733          d)
5734     (when header
5735       ;; Rebuild the thread that this article is part of and go to the
5736       ;; article we have fetched.
5737       (when (and (not gnus-show-threads)
5738                  old-header)
5739         (when (and number
5740                    (setq d (gnus-data-find (mail-header-number old-header))))
5741           (goto-char (gnus-data-pos d))
5742           (gnus-data-remove
5743            number
5744            (- (gnus-point-at-bol)
5745               (prog1
5746                   (1+ (gnus-point-at-eol))
5747                 (gnus-delete-line))))))
5748       (when old-header
5749         (mail-header-set-number header (mail-header-number old-header)))
5750       (setq gnus-newsgroup-sparse
5751             (delq (setq number (mail-header-number header))
5752                   gnus-newsgroup-sparse))
5753       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5754       (push number gnus-newsgroup-limit)
5755       (gnus-rebuild-thread (mail-header-id header) line)
5756       (gnus-summary-goto-subject number nil t))
5757     (when (and (numberp number)
5758                (> number 0))
5759       ;; We have to update the boundaries even if we can't fetch the
5760       ;; article if ID is a number -- so that the next `P' or `N'
5761       ;; command will fetch the previous (or next) article even
5762       ;; if the one we tried to fetch this time has been canceled.
5763       (when (> number gnus-newsgroup-end)
5764         (setq gnus-newsgroup-end number))
5765       (when (< number gnus-newsgroup-begin)
5766         (setq gnus-newsgroup-begin number))
5767       (setq gnus-newsgroup-unselected
5768             (delq number gnus-newsgroup-unselected)))
5769     ;; Report back a success?
5770     (and header (mail-header-number header))))
5771
5772 ;;; Process/prefix in the summary buffer
5773
5774 (defun gnus-summary-work-articles (n)
5775   "Return a list of articles to be worked upon.
5776 The prefix argument, the list of process marked articles, and the
5777 current article will be taken into consideration."
5778   (save-excursion
5779     (set-buffer gnus-summary-buffer)
5780     (cond
5781      (n
5782       ;; A numerical prefix has been given.
5783       (setq n (prefix-numeric-value n))
5784       (let ((backward (< n 0))
5785             (n (abs (prefix-numeric-value n)))
5786             articles article)
5787         (save-excursion
5788           (while
5789               (and (> n 0)
5790                    (push (setq article (gnus-summary-article-number))
5791                          articles)
5792                    (if backward
5793                        (gnus-summary-find-prev nil article)
5794                      (gnus-summary-find-next nil article)))
5795             (decf n)))
5796         (nreverse articles)))
5797      ((and (gnus-region-active-p) (mark))
5798       (message "region active")
5799       ;; Work on the region between point and mark.
5800       (let ((max (max (point) (mark)))
5801             articles article)
5802         (save-excursion
5803           (goto-char (min (point) (mark)))
5804           (while
5805               (and
5806                (push (setq article (gnus-summary-article-number)) articles)
5807                (gnus-summary-find-next nil article)
5808                (< (point) max)))
5809           (nreverse articles))))
5810      (gnus-newsgroup-processable
5811       ;; There are process-marked articles present.
5812       ;; Save current state.
5813       (gnus-summary-save-process-mark)
5814       ;; Return the list.
5815       (reverse gnus-newsgroup-processable))
5816      (t
5817       ;; Just return the current article.
5818       (list (gnus-summary-article-number))))))
5819
5820 (defmacro gnus-summary-iterate (arg &rest forms)
5821   "Iterate over the process/prefixed articles and do FORMS.
5822 ARG is the interactive prefix given to the command.  FORMS will be
5823 executed with point over the summary line of the articles."
5824   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5825     `(let ((,articles (gnus-summary-work-articles ,arg)))
5826        (while ,articles
5827          (gnus-summary-goto-subject (car ,articles))
5828          ,@forms
5829          (pop ,articles)))))
5830
5831 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5832 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5833
5834 (defun gnus-summary-save-process-mark ()
5835   "Push the current set of process marked articles on the stack."
5836   (interactive)
5837   (push (copy-sequence gnus-newsgroup-processable)
5838         gnus-newsgroup-process-stack))
5839
5840 (defun gnus-summary-kill-process-mark ()
5841   "Push the current set of process marked articles on the stack and unmark."
5842   (interactive)
5843   (gnus-summary-save-process-mark)
5844   (gnus-summary-unmark-all-processable))
5845
5846 (defun gnus-summary-yank-process-mark ()
5847   "Pop the last process mark state off the stack and restore it."
5848   (interactive)
5849   (unless gnus-newsgroup-process-stack
5850     (error "Empty mark stack"))
5851   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5852
5853 (defun gnus-summary-process-mark-set (set)
5854   "Make SET into the current process marked articles."
5855   (gnus-summary-unmark-all-processable)
5856   (while set
5857     (gnus-summary-set-process-mark (pop set))))
5858
5859 ;;; Searching and stuff
5860
5861 (defun gnus-summary-search-group (&optional backward use-level)
5862   "Search for next unread newsgroup.
5863 If optional argument BACKWARD is non-nil, search backward instead."
5864   (save-excursion
5865     (set-buffer gnus-group-buffer)
5866     (when (gnus-group-search-forward
5867            backward nil (if use-level (gnus-group-group-level) nil))
5868       (gnus-group-group-name))))
5869
5870 (defun gnus-summary-best-group (&optional exclude-group)
5871   "Find the name of the best unread group.
5872 If EXCLUDE-GROUP, do not go to this group."
5873   (save-excursion
5874     (set-buffer gnus-group-buffer)
5875     (save-excursion
5876       (gnus-group-best-unread-group exclude-group))))
5877
5878 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5879   (if backward (gnus-summary-find-prev)
5880     (let* ((dummy (gnus-summary-article-intangible-p))
5881            (article (or article (gnus-summary-article-number)))
5882            (arts (gnus-data-find-list article))
5883            result)
5884       (when (and (not dummy)
5885                  (or (not gnus-summary-check-current)
5886                      (not unread)
5887                      (not (gnus-data-unread-p (car arts)))))
5888         (setq arts (cdr arts)))
5889       (when (setq result
5890                   (if unread
5891                       (progn
5892                         (while arts
5893                           (when (or (and undownloaded
5894                                          (eq gnus-undownloaded-mark
5895                                              (gnus-data-mark (car arts))))
5896                                     (gnus-data-unread-p (car arts)))
5897                             (setq result (car arts)
5898                                   arts nil))
5899                           (setq arts (cdr arts)))
5900                         result)
5901                     (car arts)))
5902         (goto-char (gnus-data-pos result))
5903         (gnus-data-number result)))))
5904
5905 (defun gnus-summary-find-prev (&optional unread article)
5906   (let* ((eobp (eobp))
5907          (article (or article (gnus-summary-article-number)))
5908          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5909          result)
5910     (when (and (not eobp)
5911                (or (not gnus-summary-check-current)
5912                    (not unread)
5913                    (not (gnus-data-unread-p (car arts)))))
5914       (setq arts (cdr arts)))
5915     (when (setq result
5916                 (if unread
5917                     (progn
5918                       (while arts
5919                         (when (gnus-data-unread-p (car arts))
5920                           (setq result (car arts)
5921                                 arts nil))
5922                         (setq arts (cdr arts)))
5923                       result)
5924                   (car arts)))
5925       (goto-char (gnus-data-pos result))
5926       (gnus-data-number result))))
5927
5928 (defun gnus-summary-find-subject (subject &optional unread backward article)
5929   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5930          (article (or article (gnus-summary-article-number)))
5931          (articles (gnus-data-list backward))
5932          (arts (gnus-data-find-list article articles))
5933          result)
5934     (when (or (not gnus-summary-check-current)
5935               (not unread)
5936               (not (gnus-data-unread-p (car arts))))
5937       (setq arts (cdr arts)))
5938     (while arts
5939       (and (or (not unread)
5940                (gnus-data-unread-p (car arts)))
5941            (vectorp (gnus-data-header (car arts)))
5942            (gnus-subject-equal
5943             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5944            (setq result (car arts)
5945                  arts nil))
5946       (setq arts (cdr arts)))
5947     (and result
5948          (goto-char (gnus-data-pos result))
5949          (gnus-data-number result))))
5950
5951 (defun gnus-summary-search-forward (&optional unread subject backward)
5952   "Search forward for an article.
5953 If UNREAD, look for unread articles.  If SUBJECT, look for
5954 articles with that subject.  If BACKWARD, search backward instead."
5955   (cond (subject (gnus-summary-find-subject subject unread backward))
5956         (backward (gnus-summary-find-prev unread))
5957         (t (gnus-summary-find-next unread))))
5958
5959 (defun gnus-recenter (&optional n)
5960   "Center point in window and redisplay frame.
5961 Also do horizontal recentering."
5962   (interactive "P")
5963   (when (and gnus-auto-center-summary
5964              (not (eq gnus-auto-center-summary 'vertical)))
5965     (gnus-horizontal-recenter))
5966   (recenter n))
5967
5968 (defun gnus-summary-recenter ()
5969   "Center point in the summary window.
5970 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5971 displayed, no centering will be performed."
5972   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5973   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5974   (interactive)
5975   (let* ((top (cond ((< (window-height) 4) 0)
5976                     ((< (window-height) 7) 1)
5977                     (t (if (numberp gnus-auto-center-summary)
5978                            gnus-auto-center-summary
5979                          2))))
5980          (height (1- (window-height)))
5981          (bottom (save-excursion (goto-char (point-max))
5982                                  (forward-line (- height))
5983                                  (point)))
5984          (window (get-buffer-window (current-buffer))))
5985     ;; The user has to want it.
5986     (when gnus-auto-center-summary
5987       (when (get-buffer-window gnus-article-buffer)
5988         ;; Only do recentering when the article buffer is displayed,
5989         ;; Set the window start to either `bottom', which is the biggest
5990         ;; possible valid number, or the second line from the top,
5991         ;; whichever is the least.
5992         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5993           (if (> bottom top-pos)
5994               ;; Keep the second line from the top visible
5995               (set-window-start window top-pos t)
5996             ;; Try to keep the bottom line visible; if it's partially
5997             ;; obscured, either scroll one more line to make it fully
5998             ;; visible, or revert to using TOP-POS.
5999             (save-excursion
6000               (goto-char (point-max))
6001               (forward-line -1)
6002               (let ((last-line-start (point)))
6003                 (goto-char bottom)
6004                 (set-window-start window (point) t)
6005                 (when (not (pos-visible-in-window-p last-line-start window))
6006                   (forward-line 1)
6007                   (set-window-start window (min (point) top-pos) t)))))))
6008       ;; Do horizontal recentering while we're at it.
6009       (when (and (get-buffer-window (current-buffer) t)
6010                  (not (eq gnus-auto-center-summary 'vertical)))
6011         (let ((selected (selected-window)))
6012           (select-window (get-buffer-window (current-buffer) t))
6013           (gnus-summary-position-point)
6014           (gnus-horizontal-recenter)
6015           (select-window selected))))))
6016
6017 (defun gnus-summary-jump-to-group (newsgroup)
6018   "Move point to NEWSGROUP in group mode buffer."
6019   ;; Keep update point of group mode buffer if visible.
6020   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6021       (save-window-excursion
6022         ;; Take care of tree window mode.
6023         (when (get-buffer-window gnus-group-buffer)
6024           (pop-to-buffer gnus-group-buffer))
6025         (gnus-group-jump-to-group newsgroup))
6026     (save-excursion
6027       ;; Take care of tree window mode.
6028       (if (get-buffer-window gnus-group-buffer)
6029           (pop-to-buffer gnus-group-buffer)
6030         (set-buffer gnus-group-buffer))
6031       (gnus-group-jump-to-group newsgroup))))
6032
6033 ;; This function returns a list of article numbers based on the
6034 ;; difference between the ranges of read articles in this group and
6035 ;; the range of active articles.
6036 (defun gnus-list-of-unread-articles (group)
6037   (let* ((read (gnus-info-read (gnus-get-info group)))
6038          (active (or (gnus-active group) (gnus-activate-group group)))
6039          (last (cdr active))
6040          first nlast unread)
6041     ;; If none are read, then all are unread.
6042     (if (not read)
6043         (setq first (car active))
6044       ;; If the range of read articles is a single range, then the
6045       ;; first unread article is the article after the last read
6046       ;; article.  Sounds logical, doesn't it?
6047       (if (and (not (listp (cdr read)))
6048                (or (< (car read) (car active))
6049                    (progn (setq read (list read))
6050                           nil)))
6051           (setq first (max (car active) (1+ (cdr read))))
6052         ;; `read' is a list of ranges.
6053         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6054                                   (caar read)))
6055                   1)
6056           (setq first (car active)))
6057         (while read
6058           (when first
6059             (while (< first nlast)
6060               (push first unread)
6061               (setq first (1+ first))))
6062           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6063           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6064           (setq read (cdr read)))))
6065     ;; And add the last unread articles.
6066     (while (<= first last)
6067       (push first unread)
6068       (setq first (1+ first)))
6069     ;; Return the list of unread articles.
6070     (delq 0 (nreverse unread))))
6071
6072 (defun gnus-list-of-read-articles (group)
6073   "Return a list of unread, unticked and non-dormant articles."
6074   (let* ((info (gnus-get-info group))
6075          (marked (gnus-info-marks info))
6076          (active (gnus-active group)))
6077     (and info active
6078          (gnus-list-range-difference
6079           (gnus-list-range-difference
6080            (gnus-sorted-complement
6081             (gnus-uncompress-range active)
6082             (gnus-list-of-unread-articles group))
6083            (cdr (assq 'dormant marked)))
6084           (cdr (assq 'tick marked))))))
6085
6086 ;; Various summary commands
6087
6088 (defun gnus-summary-select-article-buffer ()
6089   "Reconfigure windows to show article buffer."
6090   (interactive)
6091   (if (not (gnus-buffer-live-p gnus-article-buffer))
6092       (error "There is no article buffer for this summary buffer")
6093     (gnus-configure-windows 'article)
6094     (select-window (get-buffer-window gnus-article-buffer))))
6095
6096 (defun gnus-summary-universal-argument (arg)
6097   "Perform any operation on all articles that are process/prefixed."
6098   (interactive "P")
6099   (let ((articles (gnus-summary-work-articles arg))
6100         func article)
6101     (if (eq
6102          (setq
6103           func
6104           (key-binding
6105            (read-key-sequence
6106             (substitute-command-keys
6107              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6108          'undefined)
6109         (gnus-error 1 "Undefined key")
6110       (save-excursion
6111         (while articles
6112           (gnus-summary-goto-subject (setq article (pop articles)))
6113           (let (gnus-newsgroup-processable)
6114             (command-execute func))
6115           (gnus-summary-remove-process-mark article)))))
6116   (gnus-summary-position-point))
6117
6118 (defun gnus-summary-toggle-truncation (&optional arg)
6119   "Toggle truncation of summary lines.
6120 With arg, turn line truncation on iff arg is positive."
6121   (interactive "P")
6122   (setq truncate-lines
6123         (if (null arg) (not truncate-lines)
6124           (> (prefix-numeric-value arg) 0)))
6125   (redraw-display))
6126
6127 (defun gnus-summary-reselect-current-group (&optional all rescan)
6128   "Exit and then reselect the current newsgroup.
6129 The prefix argument ALL means to select all articles."
6130   (interactive "P")
6131   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6132     (error "Ephemeral groups can't be reselected"))
6133   (let ((current-subject (gnus-summary-article-number))
6134         (group gnus-newsgroup-name))
6135     (setq gnus-newsgroup-begin nil)
6136     (gnus-summary-exit)
6137     ;; We have to adjust the point of group mode buffer because
6138     ;; point was moved to the next unread newsgroup by exiting.
6139     (gnus-summary-jump-to-group group)
6140     (when rescan
6141       (save-excursion
6142         (save-window-excursion
6143           ;; Don't show group contents.
6144           (set-window-start (selected-window) (point-max))
6145           (gnus-group-get-new-news-this-group 1))))
6146     (gnus-group-read-group all t)
6147     (gnus-summary-goto-subject current-subject nil t)))
6148
6149 (defun gnus-summary-rescan-group (&optional all)
6150   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6151   (interactive "P")
6152   (gnus-summary-reselect-current-group all t))
6153
6154 (defun gnus-summary-update-info (&optional non-destructive)
6155   (save-excursion
6156     (let ((group gnus-newsgroup-name))
6157       (when group
6158         (when gnus-newsgroup-kill-headers
6159           (setq gnus-newsgroup-killed
6160                 (gnus-compress-sequence
6161                  (gnus-sorted-union
6162                   (gnus-list-range-intersection
6163                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6164                   gnus-newsgroup-unreads)
6165                  t)))
6166         (unless (listp (cdr gnus-newsgroup-killed))
6167           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6168         (let ((headers gnus-newsgroup-headers))
6169           ;; Set the new ranges of read articles.
6170           (save-excursion
6171             (set-buffer gnus-group-buffer)
6172             (gnus-undo-force-boundary))
6173           (gnus-update-read-articles
6174            group (gnus-sorted-union
6175                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6176           ;; Set the current article marks.
6177           (let ((gnus-newsgroup-scored
6178                  (if (and (not gnus-save-score)
6179                           (not non-destructive))
6180                      nil
6181                    gnus-newsgroup-scored)))
6182             (save-excursion
6183               (gnus-update-marks)))
6184           ;; Do the cross-ref thing.
6185           (when gnus-use-cross-reference
6186             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6187           ;; Do not switch windows but change the buffer to work.
6188           (set-buffer gnus-group-buffer)
6189           (unless (gnus-ephemeral-group-p group)
6190             (gnus-group-update-group group)))))))
6191
6192 (defun gnus-summary-save-newsrc (&optional force)
6193   "Save the current number of read/marked articles in the dribble buffer.
6194 The dribble buffer will then be saved.
6195 If FORCE (the prefix), also save the .newsrc file(s)."
6196   (interactive "P")
6197   (gnus-summary-update-info t)
6198   (if force
6199       (gnus-save-newsrc-file)
6200     (gnus-dribble-save)))
6201
6202 (defun gnus-summary-exit (&optional temporary)
6203   "Exit reading current newsgroup, and then return to group selection mode.
6204 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6205   (interactive)
6206   (gnus-set-global-variables)
6207   (gnus-kill-save-kill-buffer)
6208   (gnus-async-halt-prefetch)
6209   (let* ((group gnus-newsgroup-name)
6210          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6211          (mode major-mode)
6212          (group-point nil)
6213          (buf (current-buffer)))
6214     (unless quit-config
6215       ;; Do adaptive scoring, and possibly save score files.
6216       (when gnus-newsgroup-adaptive
6217         (gnus-score-adaptive))
6218       (when gnus-use-scoring
6219         (gnus-score-save)))
6220     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6221     ;; If we have several article buffers, we kill them at exit.
6222     (unless gnus-single-article-buffer
6223       (gnus-kill-buffer gnus-original-article-buffer)
6224       (setq gnus-article-current nil))
6225     (when gnus-use-cache
6226       (gnus-cache-possibly-remove-articles)
6227       (gnus-cache-save-buffers))
6228     (gnus-async-prefetch-remove-group group)
6229     (when gnus-suppress-duplicates
6230       (gnus-dup-enter-articles))
6231     (when gnus-use-trees
6232       (gnus-tree-close group))
6233     (when gnus-use-cache
6234       (gnus-cache-write-active))
6235     ;; Remove entries for this group.
6236     (nnmail-purge-split-history (gnus-group-real-name group))
6237     ;; Make all changes in this group permanent.
6238     (unless quit-config
6239       (gnus-run-hooks 'gnus-exit-group-hook)
6240       (gnus-summary-update-info))
6241     (gnus-close-group group)
6242     ;; Make sure where we were, and go to next newsgroup.
6243     (set-buffer gnus-group-buffer)
6244     (unless quit-config
6245       (gnus-group-jump-to-group group))
6246     (gnus-run-hooks 'gnus-summary-exit-hook)
6247     (unless (or quit-config
6248                 ;; If this group has disappeared from the summary
6249                 ;; buffer, don't skip forwards.
6250                 (not (string= group (gnus-group-group-name))))
6251       (gnus-group-next-unread-group 1))
6252     (setq group-point (point))
6253     (if temporary
6254         nil                             ;Nothing to do.
6255       ;; If we have several article buffers, we kill them at exit.
6256       (unless gnus-single-article-buffer
6257         (gnus-kill-buffer gnus-article-buffer)
6258         (gnus-kill-buffer gnus-original-article-buffer)
6259         (setq gnus-article-current nil))
6260       (set-buffer buf)
6261       (if (not gnus-kill-summary-on-exit)
6262           (progn
6263             (gnus-deaden-summary)
6264             (setq mode nil))
6265         ;; We set all buffer-local variables to nil.  It is unclear why
6266         ;; this is needed, but if we don't, buffer-local variables are
6267         ;; not garbage-collected, it seems.  This would the lead to en
6268         ;; ever-growing Emacs.
6269         (gnus-summary-clear-local-variables)
6270         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6271           (gnus-summary-clear-local-variables))
6272         (when (get-buffer gnus-article-buffer)
6273           (bury-buffer gnus-article-buffer))
6274         ;; We clear the global counterparts of the buffer-local
6275         ;; variables as well, just to be on the safe side.
6276         (set-buffer gnus-group-buffer)
6277         (gnus-summary-clear-local-variables)
6278         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6279           (gnus-summary-clear-local-variables)))
6280       (setq gnus-current-select-method gnus-select-method)
6281       (pop-to-buffer gnus-group-buffer)
6282       (if (not quit-config)
6283           (progn
6284             (goto-char group-point)
6285             (gnus-configure-windows 'group 'force)
6286             (unless (pos-visible-in-window-p)
6287               (forward-line (/ (static-if (featurep 'xemacs)
6288                                    (window-displayed-height)
6289                                  (1- (window-height)))
6290                                -2))
6291               (set-window-start (selected-window) (point))
6292               (goto-char group-point)))
6293         (gnus-handle-ephemeral-exit quit-config))
6294       ;; Return to group mode buffer.
6295       (when (eq mode 'gnus-summary-mode)
6296         (gnus-kill-buffer buf))
6297       ;; Clear the current group name.
6298       (unless quit-config
6299         (setq gnus-newsgroup-name nil)))))
6300
6301 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6302 (defun gnus-summary-exit-no-update (&optional no-questions)
6303   "Quit reading current newsgroup without updating read article info."
6304   (interactive)
6305   (let* ((group gnus-newsgroup-name)
6306          (quit-config (gnus-group-quit-config group)))
6307     (when (or no-questions
6308               gnus-expert-user
6309               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6310       (gnus-async-halt-prefetch)
6311       (mapcar 'funcall
6312               (delq 'gnus-summary-expire-articles
6313                     (copy-sequence gnus-summary-prepare-exit-hook)))
6314       ;; If we have several article buffers, we kill them at exit.
6315       (unless gnus-single-article-buffer
6316         (gnus-kill-buffer gnus-article-buffer)
6317         (gnus-kill-buffer gnus-original-article-buffer)
6318         (setq gnus-article-current nil))
6319       (if (not gnus-kill-summary-on-exit)
6320           (gnus-deaden-summary)
6321         (gnus-close-group group)
6322         (gnus-summary-clear-local-variables)
6323         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6324           (gnus-summary-clear-local-variables))
6325         (set-buffer gnus-group-buffer)
6326         (gnus-summary-clear-local-variables)
6327         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6328           (gnus-summary-clear-local-variables))
6329         (when (get-buffer gnus-summary-buffer)
6330           (kill-buffer gnus-summary-buffer)))
6331       (unless gnus-single-article-buffer
6332         (setq gnus-article-current nil))
6333       (when gnus-use-trees
6334         (gnus-tree-close group))
6335       (gnus-async-prefetch-remove-group group)
6336       (when (get-buffer gnus-article-buffer)
6337         (bury-buffer gnus-article-buffer))
6338       ;; Return to the group buffer.
6339       (gnus-configure-windows 'group 'force)
6340       ;; Clear the current group name.
6341       (setq gnus-newsgroup-name nil)
6342       (when (equal (gnus-group-group-name) group)
6343         (gnus-group-next-unread-group 1))
6344       (when quit-config
6345         (gnus-handle-ephemeral-exit quit-config)))))
6346
6347 (defun gnus-handle-ephemeral-exit (quit-config)
6348   "Handle movement when leaving an ephemeral group.
6349 The state which existed when entering the ephemeral is reset."
6350   (if (not (buffer-name (car quit-config)))
6351       (gnus-configure-windows 'group 'force)
6352     (set-buffer (car quit-config))
6353     (cond ((eq major-mode 'gnus-summary-mode)
6354            (gnus-set-global-variables))
6355           ((eq major-mode 'gnus-article-mode)
6356            (save-excursion
6357              ;; The `gnus-summary-buffer' variable may point
6358              ;; to the old summary buffer when using a single
6359              ;; article buffer.
6360              (unless (gnus-buffer-live-p gnus-summary-buffer)
6361                (set-buffer gnus-group-buffer))
6362              (set-buffer gnus-summary-buffer)
6363              (gnus-set-global-variables))))
6364     (if (or (eq (cdr quit-config) 'article)
6365             (eq (cdr quit-config) 'pick))
6366         (progn
6367           ;; The current article may be from the ephemeral group
6368           ;; thus it is best that we reload this article
6369           (gnus-summary-show-article)
6370           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6371               (gnus-configure-windows 'pick 'force)
6372             (gnus-configure-windows (cdr quit-config) 'force)))
6373       (gnus-configure-windows (cdr quit-config) 'force))
6374     (when (eq major-mode 'gnus-summary-mode)
6375       (gnus-summary-next-subject 1 nil t)
6376       (gnus-summary-recenter)
6377       (gnus-summary-position-point))))
6378
6379 (defun gnus-summary-preview-mime-message ()
6380   "MIME decode and play this message."
6381   (interactive)
6382   (let ((gnus-break-pages nil)
6383         (gnus-show-mime t))
6384     (gnus-summary-select-article gnus-show-all-headers t))
6385   (let ((w (get-buffer-window gnus-article-buffer)))
6386     (when w
6387       (select-window (get-buffer-window gnus-article-buffer)))))
6388
6389 ;;; Dead summaries.
6390
6391 (defvar gnus-dead-summary-mode-map nil)
6392
6393 (unless gnus-dead-summary-mode-map
6394   (setq gnus-dead-summary-mode-map (make-keymap))
6395   (suppress-keymap gnus-dead-summary-mode-map)
6396   (substitute-key-definition
6397    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6398   (dolist (key '("\C-d" "\r" "\177" [delete]))
6399     (define-key gnus-dead-summary-mode-map
6400       key 'gnus-summary-wake-up-the-dead))
6401   (dolist (key '("q" "Q"))
6402     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6403
6404 (defvar gnus-dead-summary-mode nil
6405   "Minor mode for Gnus summary buffers.")
6406
6407 (defun gnus-dead-summary-mode (&optional arg)
6408   "Minor mode for Gnus summary buffers."
6409   (interactive "P")
6410   (when (eq major-mode 'gnus-summary-mode)
6411     (make-local-variable 'gnus-dead-summary-mode)
6412     (setq gnus-dead-summary-mode
6413           (if (null arg) (not gnus-dead-summary-mode)
6414             (> (prefix-numeric-value arg) 0)))
6415     (when gnus-dead-summary-mode
6416       (gnus-add-minor-mode
6417        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6418
6419 (defun gnus-deaden-summary ()
6420   "Make the current summary buffer into a dead summary buffer."
6421   ;; Kill any previous dead summary buffer.
6422   (when (and gnus-dead-summary
6423              (buffer-name gnus-dead-summary))
6424     (save-excursion
6425       (set-buffer gnus-dead-summary)
6426       (when gnus-dead-summary-mode
6427         (kill-buffer (current-buffer)))))
6428   ;; Make this the current dead summary.
6429   (setq gnus-dead-summary (current-buffer))
6430   (gnus-dead-summary-mode 1)
6431   (let ((name (buffer-name)))
6432     (when (string-match "Summary" name)
6433       (rename-buffer
6434        (concat (substring name 0 (match-beginning 0)) "Dead "
6435                (substring name (match-beginning 0)))
6436        t)
6437       (bury-buffer))))
6438
6439 (defun gnus-kill-or-deaden-summary (buffer)
6440   "Kill or deaden the summary BUFFER."
6441   (save-excursion
6442     (when (and (buffer-name buffer)
6443                (not gnus-single-article-buffer))
6444       (save-excursion
6445         (set-buffer buffer)
6446         (gnus-kill-buffer gnus-article-buffer)
6447         (gnus-kill-buffer gnus-original-article-buffer)))
6448     (cond
6449      ;; Kill the buffer.
6450      (gnus-kill-summary-on-exit
6451       (when (and gnus-use-trees
6452                  (gnus-buffer-exists-p buffer))
6453         (save-excursion
6454           (set-buffer buffer)
6455           (gnus-tree-close gnus-newsgroup-name)))
6456       (gnus-kill-buffer buffer))
6457      ;; Deaden the buffer.
6458      ((gnus-buffer-exists-p buffer)
6459       (save-excursion
6460         (set-buffer buffer)
6461         (gnus-deaden-summary))))))
6462
6463 (defun gnus-summary-wake-up-the-dead (&rest args)
6464   "Wake up the dead summary buffer."
6465   (interactive)
6466   (gnus-dead-summary-mode -1)
6467   (let ((name (buffer-name)))
6468     (when (string-match "Dead " name)
6469       (rename-buffer
6470        (concat (substring name 0 (match-beginning 0))
6471                (substring name (match-end 0)))
6472        t)))
6473   (gnus-message 3 "This dead summary is now alive again"))
6474
6475 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6476 (defun gnus-summary-fetch-faq (&optional faq-dir)
6477   "Fetch the FAQ for the current group.
6478 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6479 in."
6480   (interactive
6481    (list
6482     (when current-prefix-arg
6483       (completing-read
6484        "Faq dir: " (and (listp gnus-group-faq-directory)
6485                         (mapcar (lambda (file) (list file))
6486                                 gnus-group-faq-directory))))))
6487   (let (gnus-faq-buffer)
6488     (when (setq gnus-faq-buffer
6489                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6490       (gnus-configure-windows 'summary-faq))))
6491
6492 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6493 (defun gnus-summary-describe-group (&optional force)
6494   "Describe the current newsgroup."
6495   (interactive "P")
6496   (gnus-group-describe-group force gnus-newsgroup-name))
6497
6498 (defun gnus-summary-describe-briefly ()
6499   "Describe summary mode commands briefly."
6500   (interactive)
6501   (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")))
6502
6503 ;; Walking around group mode buffer from summary mode.
6504
6505 (defun gnus-summary-next-group (&optional no-article target-group backward)
6506   "Exit current newsgroup and then select next unread newsgroup.
6507 If prefix argument NO-ARTICLE is non-nil, no article is selected
6508 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6509 previous group instead."
6510   (interactive "P")
6511   ;; Stop pre-fetching.
6512   (gnus-async-halt-prefetch)
6513   (let ((current-group gnus-newsgroup-name)
6514         (current-buffer (current-buffer))
6515         entered)
6516     ;; First we semi-exit this group to update Xrefs and all variables.
6517     ;; We can't do a real exit, because the window conf must remain
6518     ;; the same in case the user is prompted for info, and we don't
6519     ;; want the window conf to change before that...
6520     (gnus-summary-exit t)
6521     (while (not entered)
6522       ;; Then we find what group we are supposed to enter.
6523       (set-buffer gnus-group-buffer)
6524       (gnus-group-jump-to-group current-group)
6525       (setq target-group
6526             (or target-group
6527                 (if (eq gnus-keep-same-level 'best)
6528                     (gnus-summary-best-group gnus-newsgroup-name)
6529                   (gnus-summary-search-group backward gnus-keep-same-level))))
6530       (if (not target-group)
6531           ;; There are no further groups, so we return to the group
6532           ;; buffer.
6533           (progn
6534             (gnus-message 5 "Returning to the group buffer")
6535             (setq entered t)
6536             (when (gnus-buffer-live-p current-buffer)
6537               (set-buffer current-buffer)
6538               (gnus-summary-exit))
6539             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6540         ;; We try to enter the target group.
6541         (gnus-group-jump-to-group target-group)
6542         (let ((unreads (gnus-group-group-unread)))
6543           (if (and (or (eq t unreads)
6544                        (and unreads (not (zerop unreads))))
6545                    (gnus-summary-read-group
6546                     target-group nil no-article
6547                     (and (buffer-name current-buffer) current-buffer)
6548                     nil backward))
6549               (setq entered t)
6550             (setq current-group target-group
6551                   target-group nil)))))))
6552
6553 (defun gnus-summary-prev-group (&optional no-article)
6554   "Exit current newsgroup and then select previous unread newsgroup.
6555 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6556   (interactive "P")
6557   (gnus-summary-next-group no-article nil t))
6558
6559 ;; Walking around summary lines.
6560
6561 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6562   "Go to the first unread subject.
6563 If UNREAD is non-nil, go to the first unread article.
6564 Returns the article selected or nil if there are no unread articles."
6565   (interactive "P")
6566   (prog1
6567       (cond
6568        ;; Empty summary.
6569        ((null gnus-newsgroup-data)
6570         (gnus-message 3 "No articles in the group")
6571         nil)
6572        ;; Pick the first article.
6573        ((not unread)
6574         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6575         (gnus-data-number (car gnus-newsgroup-data)))
6576        ;; No unread articles.
6577        ((null gnus-newsgroup-unreads)
6578         (gnus-message 3 "No more unread articles")
6579         nil)
6580        ;; Find the first unread article.
6581        (t
6582         (let ((data gnus-newsgroup-data))
6583           (while (and data
6584                       (and (not (and undownloaded
6585                                      (eq gnus-undownloaded-mark
6586                                          (gnus-data-mark (car data)))))
6587                            (if unseen
6588                                (or (not (memq
6589                                          (gnus-data-number (car data))
6590                                          gnus-newsgroup-unseen))
6591                                    (not (gnus-data-unread-p (car data))))
6592                              (not (gnus-data-unread-p (car data))))))
6593             (setq data (cdr data)))
6594           (when data
6595             (goto-char (gnus-data-pos (car data)))
6596             (gnus-data-number (car data))))))
6597     (gnus-summary-position-point)))
6598
6599 (defun gnus-summary-next-subject (n &optional unread dont-display)
6600   "Go to next N'th summary line.
6601 If N is negative, go to the previous N'th subject line.
6602 If UNREAD is non-nil, only unread articles are selected.
6603 The difference between N and the actual number of steps taken is
6604 returned."
6605   (interactive "p")
6606   (let ((backward (< n 0))
6607         (n (abs n)))
6608     (while (and (> n 0)
6609                 (if backward
6610                     (gnus-summary-find-prev unread)
6611                   (gnus-summary-find-next unread)))
6612       (unless (zerop (setq n (1- n)))
6613         (gnus-summary-show-thread)))
6614     (when (/= 0 n)
6615       (gnus-message 7 "No more%s articles"
6616                     (if unread " unread" "")))
6617     (unless dont-display
6618       (gnus-summary-recenter)
6619       (gnus-summary-position-point))
6620     n))
6621
6622 (defun gnus-summary-next-unread-subject (n)
6623   "Go to next N'th unread summary line."
6624   (interactive "p")
6625   (gnus-summary-next-subject n t))
6626
6627 (defun gnus-summary-prev-subject (n &optional unread)
6628   "Go to previous N'th summary line.
6629 If optional argument UNREAD is non-nil, only unread article is selected."
6630   (interactive "p")
6631   (gnus-summary-next-subject (- n) unread))
6632
6633 (defun gnus-summary-prev-unread-subject (n)
6634   "Go to previous N'th unread summary line."
6635   (interactive "p")
6636   (gnus-summary-next-subject (- n) t))
6637
6638 (defun gnus-summary-goto-subject (article &optional force silent)
6639   "Go the subject line of ARTICLE.
6640 If FORCE, also allow jumping to articles not currently shown."
6641   (interactive "nArticle number: ")
6642   (unless (numberp article)
6643     (error "Article %s is not a number" article))
6644   (let ((b (point))
6645         (data (gnus-data-find article)))
6646     ;; We read in the article if we have to.
6647     (and (not data)
6648          force
6649          (gnus-summary-insert-subject
6650           article
6651           (if (or (numberp force) (vectorp force)) force)
6652           t)
6653          (setq data (gnus-data-find article)))
6654     (goto-char b)
6655     (if (not data)
6656         (progn
6657           (unless silent
6658             (gnus-message 3 "Can't find article %d" article))
6659           nil)
6660       (let ((pt (gnus-data-pos data)))
6661         (goto-char pt)
6662         (gnus-summary-set-article-display-arrow pt))
6663       (gnus-summary-position-point)
6664       article)))
6665
6666 ;; Walking around summary lines with displaying articles.
6667
6668 (defun gnus-summary-expand-window (&optional arg)
6669   "Make the summary buffer take up the entire Emacs frame.
6670 Given a prefix, will force an `article' buffer configuration."
6671   (interactive "P")
6672   (if arg
6673       (gnus-configure-windows 'article 'force)
6674     (gnus-configure-windows 'summary 'force)))
6675
6676 (defun gnus-summary-display-article (article &optional all-header)
6677   "Display ARTICLE in article buffer."
6678   (when (gnus-buffer-live-p gnus-article-buffer)
6679     (with-current-buffer gnus-article-buffer
6680       (set-buffer-multibyte t)))
6681   (gnus-set-global-variables)
6682   (when (gnus-buffer-live-p gnus-article-buffer)
6683     (with-current-buffer gnus-article-buffer
6684       (setq gnus-article-charset gnus-newsgroup-charset)
6685       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6686       (set-buffer-multibyte t)))
6687   (if (null article)
6688       nil
6689     (prog1
6690         (if gnus-summary-display-article-function
6691             (funcall gnus-summary-display-article-function article all-header)
6692           (gnus-article-prepare article all-header))
6693       (with-current-buffer gnus-article-buffer
6694         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6695              nil))
6696       (gnus-run-hooks 'gnus-select-article-hook)
6697       (when (and gnus-current-article
6698                  (not (zerop gnus-current-article)))
6699         (gnus-summary-goto-subject gnus-current-article))
6700       (gnus-summary-recenter)
6701       (when (and gnus-use-trees gnus-show-threads)
6702         (gnus-possibly-generate-tree article)
6703         (gnus-highlight-selected-tree article))
6704       ;; Successfully display article.
6705       (gnus-article-set-window-start
6706        (cdr (assq article gnus-newsgroup-bookmarks))))))
6707
6708 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6709   "Select the current article.
6710 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6711 non-nil, the article will be re-fetched even if it already present in
6712 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6713 be displayed."
6714   ;; Make sure we are in the summary buffer to work around bbdb bug.
6715   (unless (eq major-mode 'gnus-summary-mode)
6716     (set-buffer gnus-summary-buffer))
6717   (let ((article (or article (gnus-summary-article-number)))
6718         (all-headers (not (not all-headers))) ;Must be T or NIL.
6719         gnus-summary-display-article-function)
6720     (and (not pseudo)
6721          (gnus-summary-article-pseudo-p article)
6722          (error "This is a pseudo-article"))
6723     (save-excursion
6724       (set-buffer gnus-summary-buffer)
6725       (if (or (and gnus-single-article-buffer
6726                    (or (null gnus-current-article)
6727                        (null gnus-article-current)
6728                        (null (get-buffer gnus-article-buffer))
6729                        (not (eq article (cdr gnus-article-current)))
6730                        (not (equal (car gnus-article-current)
6731                                    gnus-newsgroup-name))))
6732               (and (not gnus-single-article-buffer)
6733                    (or (null gnus-current-article)
6734                        (not (eq gnus-current-article article))))
6735               force)
6736           ;; The requested article is different from the current article.
6737           (progn
6738             (gnus-summary-display-article article all-headers)
6739             (gnus-article-set-window-start
6740              (cdr (assq article gnus-newsgroup-bookmarks)))
6741             article)
6742         'old))))
6743
6744 (defun gnus-summary-force-verify-and-decrypt ()
6745   (interactive)
6746   (let ((mm-verify-option 'known)
6747         (mm-decrypt-option 'known)
6748         (gnus-buttonized-mime-types (append (list "multipart/signed"
6749                                                   "multipart/encrypted")
6750                                             gnus-buttonized-mime-types)))
6751     (gnus-summary-select-article nil 'force)))
6752
6753 (defun gnus-summary-set-current-mark (&optional current-mark)
6754   "Obsolete function."
6755   nil)
6756
6757 (defun gnus-summary-next-article (&optional unread subject backward push)
6758   "Select the next article.
6759 If UNREAD, only unread articles are selected.
6760 If SUBJECT, only articles with SUBJECT are selected.
6761 If BACKWARD, the previous article is selected instead of the next."
6762   (interactive "P")
6763   (cond
6764    ;; Is there such an article?
6765    ((and (gnus-summary-search-forward unread subject backward)
6766          (or (gnus-summary-display-article (gnus-summary-article-number))
6767              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6768     (gnus-summary-position-point))
6769    ;; If not, we try the first unread, if that is wanted.
6770    ((and subject
6771          gnus-auto-select-same
6772          (gnus-summary-first-unread-article))
6773     (gnus-summary-position-point)
6774     (gnus-message 6 "Wrapped"))
6775    ;; Try to get next/previous article not displayed in this group.
6776    ((and gnus-auto-extend-newsgroup
6777          (not unread) (not subject))
6778     (gnus-summary-goto-article
6779      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6780      nil (count-lines (point-min) (point))))
6781    ;; Go to next/previous group.
6782    (t
6783     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6784       (gnus-summary-jump-to-group gnus-newsgroup-name))
6785     (let ((cmd last-command-char)
6786           (point
6787            (save-excursion
6788              (set-buffer gnus-group-buffer)
6789              (point)))
6790           (group
6791            (if (eq gnus-keep-same-level 'best)
6792                (gnus-summary-best-group gnus-newsgroup-name)
6793              (gnus-summary-search-group backward gnus-keep-same-level))))
6794       ;; For some reason, the group window gets selected.  We change
6795       ;; it back.
6796       (select-window (get-buffer-window (current-buffer)))
6797       ;; Select next unread newsgroup automagically.
6798       (cond
6799        ((or (not gnus-auto-select-next)
6800             (not cmd))
6801         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6802        ((or (eq gnus-auto-select-next 'quietly)
6803             (and (eq gnus-auto-select-next 'slightly-quietly)
6804                  push)
6805             (and (eq gnus-auto-select-next 'almost-quietly)
6806                  (gnus-summary-last-article-p)))
6807         ;; Select quietly.
6808         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6809             (gnus-summary-exit)
6810           (gnus-message 7 "No more%s articles (%s)..."
6811                         (if unread " unread" "")
6812                         (if group (concat "selecting " group)
6813                           "exiting"))
6814           (gnus-summary-next-group nil group backward)))
6815        (t
6816         (when (gnus-key-press-event-p last-input-event)
6817           (gnus-summary-walk-group-buffer
6818            gnus-newsgroup-name cmd unread backward point))))))))
6819
6820 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6821   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6822                       (?\C-p (gnus-group-prev-unread-group 1))))
6823         (cursor-in-echo-area t)
6824         keve key group ended)
6825     (save-excursion
6826       (set-buffer gnus-group-buffer)
6827       (goto-char start)
6828       (setq group
6829             (if (eq gnus-keep-same-level 'best)
6830                 (gnus-summary-best-group gnus-newsgroup-name)
6831               (gnus-summary-search-group backward gnus-keep-same-level))))
6832     (while (not ended)
6833       (gnus-message
6834        5 "No more%s articles%s" (if unread " unread" "")
6835        (if (and group
6836                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6837            (format " (Type %s for %s [%s])"
6838                    (single-key-description cmd) group
6839                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6840          (format " (Type %s to exit %s)"
6841                  (single-key-description cmd)
6842                  gnus-newsgroup-name)))
6843       ;; Confirm auto selection.
6844       (setq key (car (setq keve (gnus-read-event-char))))
6845       (setq ended t)
6846       (cond
6847        ((assq key keystrokes)
6848         (let ((obuf (current-buffer)))
6849           (switch-to-buffer gnus-group-buffer)
6850           (when group
6851             (gnus-group-jump-to-group group))
6852           (eval (cadr (assq key keystrokes)))
6853           (setq group (gnus-group-group-name))
6854           (switch-to-buffer obuf))
6855         (setq ended nil))
6856        ((equal key cmd)
6857         (if (or (not group)
6858                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6859             (gnus-summary-exit)
6860           (gnus-summary-next-group nil group backward)))
6861        (t
6862         (push (cdr keve) unread-command-events))))))
6863
6864 (defun gnus-summary-next-unread-article ()
6865   "Select unread article after current one."
6866   (interactive)
6867   (gnus-summary-next-article
6868    (or (not (eq gnus-summary-goto-unread 'never))
6869        (gnus-summary-last-article-p (gnus-summary-article-number)))
6870    (and gnus-auto-select-same
6871         (gnus-summary-article-subject))))
6872
6873 (defun gnus-summary-prev-article (&optional unread subject)
6874   "Select the article after the current one.
6875 If UNREAD is non-nil, only unread articles are selected."
6876   (interactive "P")
6877   (gnus-summary-next-article unread subject t))
6878
6879 (defun gnus-summary-prev-unread-article ()
6880   "Select unread article before current one."
6881   (interactive)
6882   (gnus-summary-prev-article
6883    (or (not (eq gnus-summary-goto-unread 'never))
6884        (gnus-summary-first-article-p (gnus-summary-article-number)))
6885    (and gnus-auto-select-same
6886         (gnus-summary-article-subject))))
6887
6888 (defun gnus-summary-next-page (&optional lines circular)
6889   "Show next page of the selected article.
6890 If at the end of the current article, select the next article.
6891 LINES says how many lines should be scrolled up.
6892
6893 If CIRCULAR is non-nil, go to the start of the article instead of
6894 selecting the next article when reaching the end of the current
6895 article."
6896   (interactive "P")
6897   (setq gnus-summary-buffer (current-buffer))
6898   (gnus-set-global-variables)
6899   (let ((article (gnus-summary-article-number))
6900         (article-window (get-buffer-window gnus-article-buffer t))
6901         endp)
6902     ;; If the buffer is empty, we have no article.
6903     (unless article
6904       (error "No article to select"))
6905     (gnus-configure-windows 'article)
6906     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6907         (if (and (eq gnus-summary-goto-unread 'never)
6908                  (not (gnus-summary-last-article-p article)))
6909             (gnus-summary-next-article)
6910           (gnus-summary-next-unread-article))
6911       (if (or (null gnus-current-article)
6912               (null gnus-article-current)
6913               (/= article (cdr gnus-article-current))
6914               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6915           ;; Selected subject is different from current article's.
6916           (gnus-summary-display-article article)
6917         (when article-window
6918           (gnus-eval-in-buffer-window gnus-article-buffer
6919             (setq endp (gnus-article-next-page lines)))
6920           (when endp
6921             (cond (circular
6922                    (gnus-summary-beginning-of-article))
6923                   (lines
6924                    (gnus-message 3 "End of message"))
6925                   ((null lines)
6926                    (if (and (eq gnus-summary-goto-unread 'never)
6927                             (not (gnus-summary-last-article-p article)))
6928                        (gnus-summary-next-article)
6929                      (gnus-summary-next-unread-article))))))))
6930     (gnus-summary-recenter)
6931     (gnus-summary-position-point)))
6932
6933 (defun gnus-summary-prev-page (&optional lines move)
6934   "Show previous page of selected article.
6935 Argument LINES specifies lines to be scrolled down.
6936 If MOVE, move to the previous unread article if point is at
6937 the beginning of the buffer."
6938   (interactive "P")
6939   (let ((article (gnus-summary-article-number))
6940         (article-window (get-buffer-window gnus-article-buffer t))
6941         endp)
6942     (gnus-configure-windows 'article)
6943     (if (or (null gnus-current-article)
6944             (null gnus-article-current)
6945             (/= article (cdr gnus-article-current))
6946             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6947         ;; Selected subject is different from current article's.
6948         (gnus-summary-display-article article)
6949       (gnus-summary-recenter)
6950       (when article-window
6951         (gnus-eval-in-buffer-window gnus-article-buffer
6952           (setq endp (gnus-article-prev-page lines)))
6953         (when (and move endp)
6954           (cond (lines
6955                  (gnus-message 3 "Beginning of message"))
6956                 ((null lines)
6957                  (if (and (eq gnus-summary-goto-unread 'never)
6958                           (not (gnus-summary-first-article-p article)))
6959                      (gnus-summary-prev-article)
6960                    (gnus-summary-prev-unread-article))))))))
6961   (gnus-summary-position-point))
6962
6963 (defun gnus-summary-prev-page-or-article (&optional lines)
6964   "Show previous page of selected article.
6965 Argument LINES specifies lines to be scrolled down.
6966 If at the beginning of the article, go to the next article."
6967   (interactive "P")
6968   (gnus-summary-prev-page lines t))
6969
6970 (defun gnus-summary-scroll-up (lines)
6971   "Scroll up (or down) one line current article.
6972 Argument LINES specifies lines to be scrolled up (or down if negative)."
6973   (interactive "p")
6974   (gnus-configure-windows 'article)
6975   (gnus-summary-show-thread)
6976   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6977     (gnus-eval-in-buffer-window gnus-article-buffer
6978       (cond ((> lines 0)
6979              (when (gnus-article-next-page lines)
6980                (gnus-message 3 "End of message")))
6981             ((< lines 0)
6982              (gnus-article-prev-page (- lines))))))
6983   (gnus-summary-recenter)
6984   (gnus-summary-position-point))
6985
6986 (defun gnus-summary-scroll-down (lines)
6987   "Scroll down (or up) one line current article.
6988 Argument LINES specifies lines to be scrolled down (or up if negative)."
6989   (interactive "p")
6990   (gnus-summary-scroll-up (- lines)))
6991
6992 (defun gnus-summary-next-same-subject ()
6993   "Select next article which has the same subject as current one."
6994   (interactive)
6995   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6996
6997 (defun gnus-summary-prev-same-subject ()
6998   "Select previous article which has the same subject as current one."
6999   (interactive)
7000   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7001
7002 (defun gnus-summary-next-unread-same-subject ()
7003   "Select next unread article which has the same subject as current one."
7004   (interactive)
7005   (gnus-summary-next-article t (gnus-summary-article-subject)))
7006
7007 (defun gnus-summary-prev-unread-same-subject ()
7008   "Select previous unread article which has the same subject as current one."
7009   (interactive)
7010   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7011
7012 (defun gnus-summary-first-unread-article ()
7013   "Select the first unread article.
7014 Return nil if there are no unread articles."
7015   (interactive)
7016   (prog1
7017       (when (gnus-summary-first-subject t)
7018         (gnus-summary-show-thread)
7019         (gnus-summary-first-subject t)
7020         (gnus-summary-display-article (gnus-summary-article-number)))
7021     (gnus-summary-position-point)))
7022
7023 (defun gnus-summary-first-unread-subject ()
7024   "Place the point on the subject line of the first unread article.
7025 Return nil if there are no unread articles."
7026   (interactive)
7027   (prog1
7028       (when (gnus-summary-first-subject t)
7029         (gnus-summary-show-thread)
7030         (gnus-summary-first-subject t))
7031     (gnus-summary-position-point)))
7032
7033 (defun gnus-summary-first-unseen-subject ()
7034   "Place the point on the subject line of the first unseen article.
7035 Return nil if there are no unseen articles."
7036   (interactive)
7037   (prog1
7038       (when (gnus-summary-first-subject t t t)
7039         (gnus-summary-show-thread)
7040         (gnus-summary-first-subject t t t))
7041     (gnus-summary-position-point)))
7042
7043 (defun gnus-summary-first-unseen-or-unread-subject ()
7044   "Place the point on the subject line of the first unseen article.
7045 Return nil if there are no unseen articles."
7046   (interactive)
7047   (prog1
7048       (unless (when (gnus-summary-first-subject t t t)
7049                 (gnus-summary-show-thread)
7050                 (gnus-summary-first-subject t t t))
7051         (when (gnus-summary-first-subject t)
7052           (gnus-summary-show-thread)
7053           (gnus-summary-first-subject t)))
7054     (gnus-summary-position-point)))
7055
7056 (defun gnus-summary-first-article ()
7057   "Select the first article.
7058 Return nil if there are no articles."
7059   (interactive)
7060   (prog1
7061       (when (gnus-summary-first-subject)
7062         (gnus-summary-show-thread)
7063         (gnus-summary-first-subject)
7064         (gnus-summary-display-article (gnus-summary-article-number)))
7065     (gnus-summary-position-point)))
7066
7067 (defun gnus-summary-best-unread-article (&optional arg)
7068   "Select the unread article with the highest score.
7069 If given a prefix argument, select the next unread article that has a
7070 score higher than the default score."
7071   (interactive "P")
7072   (let ((article (if arg
7073                      (gnus-summary-better-unread-subject)
7074                    (gnus-summary-best-unread-subject))))
7075     (if article
7076         (gnus-summary-goto-article article)
7077       (error "No unread articles"))))
7078
7079 (defun gnus-summary-best-unread-subject ()
7080   "Select the unread subject with the highest score."
7081   (interactive)
7082   (let ((best -1000000)
7083         (data gnus-newsgroup-data)
7084         article score)
7085     (while data
7086       (and (gnus-data-unread-p (car data))
7087            (> (setq score
7088                     (gnus-summary-article-score (gnus-data-number (car data))))
7089               best)
7090            (setq best score
7091                  article (gnus-data-number (car data))))
7092       (setq data (cdr data)))
7093     (when article
7094       (gnus-summary-goto-subject article))
7095     (gnus-summary-position-point)
7096     article))
7097
7098 (defun gnus-summary-better-unread-subject ()
7099   "Select the first unread subject that has a score over the default score."
7100   (interactive)
7101   (let ((data gnus-newsgroup-data)
7102         article score)
7103     (while (and (setq article (gnus-data-number (car data)))
7104                 (or (gnus-data-read-p (car data))
7105                     (not (> (gnus-summary-article-score article)
7106                             gnus-summary-default-score))))
7107       (setq data (cdr data)))
7108     (when article
7109       (gnus-summary-goto-subject article))
7110     (gnus-summary-position-point)
7111     article))
7112
7113 (defun gnus-summary-last-subject ()
7114   "Go to the last displayed subject line in the group."
7115   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7116     (when article
7117       (gnus-summary-goto-subject article))))
7118
7119 (defun gnus-summary-goto-article (article &optional all-headers force)
7120   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7121 If ALL-HEADERS is non-nil, no header lines are hidden.
7122 If FORCE, go to the article even if it isn't displayed.  If FORCE
7123 is a number, it is the line the article is to be displayed on."
7124   (interactive
7125    (list
7126     (completing-read
7127      "Article number or Message-ID: "
7128      (mapcar (lambda (number) (list (int-to-string number)))
7129              gnus-newsgroup-limit))
7130     current-prefix-arg
7131     t))
7132   (prog1
7133       (if (and (stringp article)
7134                (string-match "@" article))
7135           (gnus-summary-refer-article article)
7136         (when (stringp article)
7137           (setq article (string-to-number article)))
7138         (if (gnus-summary-goto-subject article force)
7139             (gnus-summary-display-article article all-headers)
7140           (gnus-message 4 "Couldn't go to article %s" article) nil))
7141     (gnus-summary-position-point)))
7142
7143 (defun gnus-summary-goto-last-article ()
7144   "Go to the previously read article."
7145   (interactive)
7146   (prog1
7147       (when gnus-last-article
7148         (gnus-summary-goto-article gnus-last-article nil t))
7149     (gnus-summary-position-point)))
7150
7151 (defun gnus-summary-pop-article (number)
7152   "Pop one article off the history and go to the previous.
7153 NUMBER articles will be popped off."
7154   (interactive "p")
7155   (let (to)
7156     (setq gnus-newsgroup-history
7157           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7158     (if to
7159         (gnus-summary-goto-article (car to) nil t)
7160       (error "Article history empty")))
7161   (gnus-summary-position-point))
7162
7163 ;; Summary commands and functions for limiting the summary buffer.
7164
7165 (defun gnus-summary-limit-to-articles (n)
7166   "Limit the summary buffer to the next N articles.
7167 If not given a prefix, use the process marked articles instead."
7168   (interactive "P")
7169   (prog1
7170       (let ((articles (gnus-summary-work-articles n)))
7171         (setq gnus-newsgroup-processable nil)
7172         (gnus-summary-limit articles))
7173     (gnus-summary-position-point)))
7174
7175 (defun gnus-summary-pop-limit (&optional total)
7176   "Restore the previous limit.
7177 If given a prefix, remove all limits."
7178   (interactive "P")
7179   (when total
7180     (setq gnus-newsgroup-limits
7181           (list (mapcar (lambda (h) (mail-header-number h))
7182                         gnus-newsgroup-headers))))
7183   (unless gnus-newsgroup-limits
7184     (error "No limit to pop"))
7185   (prog1
7186       (gnus-summary-limit nil 'pop)
7187     (gnus-summary-position-point)))
7188
7189 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7190   "Limit the summary buffer to articles that have subjects that match a regexp.
7191 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7192   (interactive
7193    (list (read-string (if current-prefix-arg
7194                           "Exclude subject (regexp): "
7195                         "Limit to subject (regexp): "))
7196          nil current-prefix-arg))
7197   (unless header
7198     (setq header "subject"))
7199   (when (not (equal "" subject))
7200     (prog1
7201         (let ((articles (gnus-summary-find-matching
7202                          (or header "subject") subject 'all nil nil
7203                          not-matching)))
7204           (unless articles
7205             (error "Found no matches for \"%s\"" subject))
7206           (gnus-summary-limit articles))
7207       (gnus-summary-position-point))))
7208
7209 (defun gnus-summary-limit-to-author (from &optional not-matching)
7210   "Limit the summary buffer to articles that have authors that match a regexp.
7211 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7212   (interactive
7213    (list (read-string (if current-prefix-arg
7214                           "Exclude author (regexp): "
7215                         "Limit to author (regexp): "))
7216          current-prefix-arg))
7217   (gnus-summary-limit-to-subject from "from" not-matching))
7218
7219 (defun gnus-summary-limit-to-age (age &optional younger-p)
7220   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7221 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7222 articles that are younger than AGE days."
7223   (interactive
7224    (let ((younger current-prefix-arg)
7225          (days-got nil)
7226          days)
7227      (while (not days-got)
7228        (setq days (if younger
7229                       (read-string "Limit to articles within (in days): ")
7230                     (read-string "Limit to articles older than (in days): ")))
7231        (when (> (length days) 0)
7232          (setq days (read days)))
7233        (if (numberp days)
7234            (progn
7235              (setq days-got t)
7236              (if (< days 0)
7237                  (progn
7238                    (setq younger (not younger))
7239                    (setq days (* days -1)))))
7240          (message "Please enter a number.")
7241          (sleep-for 1)))
7242      (list days younger)))
7243   (prog1
7244       (let ((data gnus-newsgroup-data)
7245             (cutoff (days-to-time age))
7246             articles d date is-younger)
7247         (while (setq d (pop data))
7248           (when (and (vectorp (gnus-data-header d))
7249                      (setq date (mail-header-date (gnus-data-header d))))
7250             (setq is-younger (time-less-p
7251                               (time-since (condition-case ()
7252                                               (date-to-time date)
7253                                             (error '(0 0))))
7254                               cutoff))
7255             (when (if younger-p
7256                       is-younger
7257                     (not is-younger))
7258               (push (gnus-data-number d) articles))))
7259         (gnus-summary-limit (nreverse articles)))
7260     (gnus-summary-position-point)))
7261
7262 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7263   "Limit the summary buffer to articles that match an 'extra' header."
7264   (interactive
7265    (let ((header
7266           (intern
7267            (gnus-completing-read-with-default
7268             (symbol-name (car gnus-extra-headers))
7269             (if current-prefix-arg
7270                 "Exclude extra header:"
7271               "Limit extra header:")
7272             (mapcar (lambda (x)
7273                       (cons (symbol-name x) x))
7274                     gnus-extra-headers)
7275             nil
7276             t))))
7277      (list header
7278            (read-string (format "%s header %s (regexp): "
7279                                 (if current-prefix-arg "Exclude" "Limit to")
7280                                 header))
7281            current-prefix-arg)))
7282   (when (not (equal "" regexp))
7283     (prog1
7284         (let ((articles (gnus-summary-find-matching
7285                          (cons 'extra header) regexp 'all nil nil
7286                          not-matching)))
7287           (unless articles
7288             (error "Found no matches for \"%s\"" regexp))
7289           (gnus-summary-limit articles))
7290       (gnus-summary-position-point))))
7291
7292 (defun gnus-summary-limit-to-display-predicate ()
7293   "Limit the summary buffer to the predicated in the `display' group parameter."
7294   (interactive)
7295   (unless gnus-newsgroup-display
7296     (error "There is no `display' group parameter"))
7297   (let (articles)
7298     (dolist (number gnus-newsgroup-articles)
7299       (when (funcall gnus-newsgroup-display)
7300         (push number articles)))
7301     (gnus-summary-limit articles))
7302   (gnus-summary-position-point))
7303
7304 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7305 (make-obsolete
7306  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7307
7308 (defun gnus-summary-limit-to-unread (&optional all)
7309   "Limit the summary buffer to articles that are not marked as read.
7310 If ALL is non-nil, limit strictly to unread articles."
7311   (interactive "P")
7312   (if all
7313       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7314     (gnus-summary-limit-to-marks
7315      ;; Concat all the marks that say that an article is read and have
7316      ;; those removed.
7317      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7318            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7319            gnus-low-score-mark gnus-expirable-mark
7320            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7321            gnus-duplicate-mark gnus-souped-mark)
7322      'reverse)))
7323
7324 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7325 (make-obsolete 'gnus-summary-delete-marked-with
7326                'gnus-summary-limit-exclude-marks)
7327
7328 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7329   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7330 If REVERSE, limit the summary buffer to articles that are marked
7331 with MARKS.  MARKS can either be a string of marks or a list of marks.
7332 Returns how many articles were removed."
7333   (interactive "sMarks: ")
7334   (gnus-summary-limit-to-marks marks t))
7335
7336 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7337   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7338 If REVERSE (the prefix), limit the summary buffer to articles that are
7339 not marked with MARKS.  MARKS can either be a string of marks or a
7340 list of marks.
7341 Returns how many articles were removed."
7342   (interactive "sMarks: \nP")
7343   (prog1
7344       (let ((data gnus-newsgroup-data)
7345             (marks (if (listp marks) marks
7346                      (append marks nil))) ; Transform to list.
7347             articles)
7348         (while data
7349           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7350                   (memq (gnus-data-mark (car data)) marks))
7351             (push (gnus-data-number (car data)) articles))
7352           (setq data (cdr data)))
7353         (gnus-summary-limit articles))
7354     (gnus-summary-position-point)))
7355
7356 (defun gnus-summary-limit-to-score (score)
7357   "Limit to articles with score at or above SCORE."
7358   (interactive "NLimit to articles with score of at least: ")
7359   (let ((data gnus-newsgroup-data)
7360         articles)
7361     (while data
7362       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7363                 score)
7364         (push (gnus-data-number (car data)) articles))
7365       (setq data (cdr data)))
7366     (prog1
7367         (gnus-summary-limit articles)
7368       (gnus-summary-position-point))))
7369
7370 (defun gnus-summary-limit-include-thread (id)
7371   "Display all the hidden articles that is in the thread with ID in it.
7372 When called interactively, ID is the Message-ID of the current
7373 article."
7374   (interactive (list (mail-header-id (gnus-summary-article-header))))
7375   (let ((articles (gnus-articles-in-thread
7376                    (gnus-id-to-thread (gnus-root-id id)))))
7377     (prog1
7378         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7379       (gnus-summary-limit-include-matching-articles
7380        "subject"
7381        (regexp-quote (gnus-simplify-subject-re
7382                       (mail-header-subject (gnus-id-to-header id)))))
7383       (gnus-summary-position-point))))
7384
7385 (defun gnus-summary-limit-include-matching-articles (header regexp)
7386   "Display all the hidden articles that have HEADERs that match REGEXP."
7387   (interactive (list (read-string "Match on header: ")
7388                      (read-string "Regexp: ")))
7389   (let ((articles (gnus-find-matching-articles header regexp)))
7390     (prog1
7391         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7392       (gnus-summary-position-point))))
7393
7394 (defun gnus-summary-limit-include-dormant ()
7395   "Display all the hidden articles that are marked as dormant.
7396 Note that this command only works on a subset of the articles currently
7397 fetched for this group."
7398   (interactive)
7399   (unless gnus-newsgroup-dormant
7400     (error "There are no dormant articles in this group"))
7401   (prog1
7402       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7403     (gnus-summary-position-point)))
7404
7405 (defun gnus-summary-limit-exclude-dormant ()
7406   "Hide all dormant articles."
7407   (interactive)
7408   (prog1
7409       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7410     (gnus-summary-position-point)))
7411
7412 (defun gnus-summary-limit-exclude-childless-dormant ()
7413   "Hide all dormant articles that have no children."
7414   (interactive)
7415   (let ((data (gnus-data-list t))
7416         articles d children)
7417     ;; Find all articles that are either not dormant or have
7418     ;; children.
7419     (while (setq d (pop data))
7420       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7421                 (and (setq children
7422                            (gnus-article-children (gnus-data-number d)))
7423                      (let (found)
7424                        (while children
7425                          (when (memq (car children) articles)
7426                            (setq children nil
7427                                  found t))
7428                          (pop children))
7429                        found)))
7430         (push (gnus-data-number d) articles)))
7431     ;; Do the limiting.
7432     (prog1
7433         (gnus-summary-limit articles)
7434       (gnus-summary-position-point))))
7435
7436 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7437   "Mark all unread excluded articles as read.
7438 If ALL, mark even excluded ticked and dormants as read."
7439   (interactive "P")
7440   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7441   (let ((articles (gnus-sorted-ndifference
7442                    (sort
7443                     (mapcar (lambda (h) (mail-header-number h))
7444                             gnus-newsgroup-headers)
7445                     '<)
7446                    gnus-newsgroup-limit))
7447         article)
7448     (setq gnus-newsgroup-unreads
7449           (gnus-sorted-intersection gnus-newsgroup-unreads
7450                                     gnus-newsgroup-limit))
7451     (if all
7452         (setq gnus-newsgroup-dormant nil
7453               gnus-newsgroup-marked nil
7454               gnus-newsgroup-reads
7455               (nconc
7456                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7457                gnus-newsgroup-reads))
7458       (while (setq article (pop articles))
7459         (unless (or (memq article gnus-newsgroup-dormant)
7460                     (memq article gnus-newsgroup-marked))
7461           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7462
7463 (defun gnus-summary-limit (articles &optional pop)
7464   (if pop
7465       ;; We pop the previous limit off the stack and use that.
7466       (setq articles (car gnus-newsgroup-limits)
7467             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7468     ;; We use the new limit, so we push the old limit on the stack.
7469     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7470   ;; Set the limit.
7471   (setq gnus-newsgroup-limit articles)
7472   (let ((total (length gnus-newsgroup-data))
7473         (data (gnus-data-find-list (gnus-summary-article-number)))
7474         (gnus-summary-mark-below nil)   ; Inhibit this.
7475         found)
7476     ;; This will do all the work of generating the new summary buffer
7477     ;; according to the new limit.
7478     (gnus-summary-prepare)
7479     ;; Hide any threads, possibly.
7480     (gnus-summary-maybe-hide-threads)
7481     ;; Try to return to the article you were at, or one in the
7482     ;; neighborhood.
7483     (when data
7484       ;; We try to find some article after the current one.
7485       (while data
7486         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7487           (setq data nil
7488                 found t))
7489         (setq data (cdr data))))
7490     (unless found
7491       ;; If there is no data, that means that we were after the last
7492       ;; article.  The same goes when we can't find any articles
7493       ;; after the current one.
7494       (goto-char (point-max))
7495       (gnus-summary-find-prev))
7496     (gnus-set-mode-line 'summary)
7497     ;; We return how many articles were removed from the summary
7498     ;; buffer as a result of the new limit.
7499     (- total (length gnus-newsgroup-data))))
7500
7501 (defsubst gnus-invisible-cut-children (threads)
7502   (let ((num 0))
7503     (while threads
7504       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7505         (incf num))
7506       (pop threads))
7507     (< num 2)))
7508
7509 (defsubst gnus-cut-thread (thread)
7510   "Go forwards in the thread until we find an article that we want to display."
7511   (when (or (eq gnus-fetch-old-headers 'some)
7512             (eq gnus-fetch-old-headers 'invisible)
7513             (numberp gnus-fetch-old-headers)
7514             (eq gnus-build-sparse-threads 'some)
7515             (eq gnus-build-sparse-threads 'more))
7516     ;; Deal with old-fetched headers and sparse threads.
7517     (while (and
7518             thread
7519             (or
7520              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7521              (gnus-summary-article-ancient-p
7522               (mail-header-number (car thread))))
7523             (if (or (<= (length (cdr thread)) 1)
7524                     (eq gnus-fetch-old-headers 'invisible))
7525                 (setq gnus-newsgroup-limit
7526                       (delq (mail-header-number (car thread))
7527                             gnus-newsgroup-limit)
7528                       thread (cadr thread))
7529               (when (gnus-invisible-cut-children (cdr thread))
7530                 (let ((th (cdr thread)))
7531                   (while th
7532                     (if (memq (mail-header-number (caar th))
7533                               gnus-newsgroup-limit)
7534                         (setq thread (car th)
7535                               th nil)
7536                       (setq th (cdr th))))))))))
7537   thread)
7538
7539 (defun gnus-cut-threads (threads)
7540   "Cut off all uninteresting articles from the beginning of threads."
7541   (when (or (eq gnus-fetch-old-headers 'some)
7542             (eq gnus-fetch-old-headers 'invisible)
7543             (numberp gnus-fetch-old-headers)
7544             (eq gnus-build-sparse-threads 'some)
7545             (eq gnus-build-sparse-threads 'more))
7546     (let ((th threads))
7547       (while th
7548         (setcar th (gnus-cut-thread (car th)))
7549         (setq th (cdr th)))))
7550   ;; Remove nixed out threads.
7551   (delq nil threads))
7552
7553 (defun gnus-summary-initial-limit (&optional show-if-empty)
7554   "Figure out what the initial limit is supposed to be on group entry.
7555 This entails weeding out unwanted dormants, low-scored articles,
7556 fetch-old-headers verbiage, and so on."
7557   ;; Most groups have nothing to remove.
7558   (if (or gnus-inhibit-limiting
7559           (and (null gnus-newsgroup-dormant)
7560                (eq gnus-newsgroup-display 'gnus-not-ignore)
7561                (not (eq gnus-fetch-old-headers 'some))
7562                (not (numberp gnus-fetch-old-headers))
7563                (not (eq gnus-fetch-old-headers 'invisible))
7564                (null gnus-summary-expunge-below)
7565                (not (eq gnus-build-sparse-threads 'some))
7566                (not (eq gnus-build-sparse-threads 'more))
7567                (null gnus-thread-expunge-below)
7568                (not gnus-use-nocem)))
7569       ()                                ; Do nothing.
7570     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7571     (setq gnus-newsgroup-limit nil)
7572     (mapatoms
7573      (lambda (node)
7574        (unless (car (symbol-value node))
7575          ;; These threads have no parents -- they are roots.
7576          (let ((nodes (cdr (symbol-value node)))
7577                thread)
7578            (while nodes
7579              (if (and gnus-thread-expunge-below
7580                       (< (gnus-thread-total-score (car nodes))
7581                          gnus-thread-expunge-below))
7582                  (gnus-expunge-thread (pop nodes))
7583                (setq thread (pop nodes))
7584                (gnus-summary-limit-children thread))))))
7585      gnus-newsgroup-dependencies)
7586     ;; If this limitation resulted in an empty group, we might
7587     ;; pop the previous limit and use it instead.
7588     (when (and (not gnus-newsgroup-limit)
7589                show-if-empty)
7590       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7591     gnus-newsgroup-limit))
7592
7593 (defun gnus-summary-limit-children (thread)
7594   "Return 1 if this subthread is visible and 0 if it is not."
7595   ;; First we get the number of visible children to this thread.  This
7596   ;; is done by recursing down the thread using this function, so this
7597   ;; will really go down to a leaf article first, before slowly
7598   ;; working its way up towards the root.
7599   (when thread
7600     (let ((children
7601            (if (cdr thread)
7602                (apply '+ (mapcar 'gnus-summary-limit-children
7603                                  (cdr thread)))
7604              0))
7605           (number (mail-header-number (car thread)))
7606           score)
7607       (if (and
7608            (not (memq number gnus-newsgroup-marked))
7609            (or
7610             ;; If this article is dormant and has absolutely no visible
7611             ;; children, then this article isn't visible.
7612             (and (memq number gnus-newsgroup-dormant)
7613                  (zerop children))
7614             ;; If this is "fetch-old-headered" and there is no
7615             ;; visible children, then we don't want this article.
7616             (and (or (eq gnus-fetch-old-headers 'some)
7617                      (numberp gnus-fetch-old-headers))
7618                  (gnus-summary-article-ancient-p number)
7619                  (zerop children))
7620             ;; If this is "fetch-old-headered" and `invisible', then
7621             ;; we don't want this article.
7622             (and (eq gnus-fetch-old-headers 'invisible)
7623                  (gnus-summary-article-ancient-p number))
7624             ;; If this is a sparsely inserted article with no children,
7625             ;; we don't want it.
7626             (and (eq gnus-build-sparse-threads 'some)
7627                  (gnus-summary-article-sparse-p number)
7628                  (zerop children))
7629             ;; If we use expunging, and this article is really
7630             ;; low-scored, then we don't want this article.
7631             (when (and gnus-summary-expunge-below
7632                        (< (setq score
7633                                 (or (cdr (assq number gnus-newsgroup-scored))
7634                                     gnus-summary-default-score))
7635                           gnus-summary-expunge-below))
7636               ;; We increase the expunge-tally here, but that has
7637               ;; nothing to do with the limits, really.
7638               (incf gnus-newsgroup-expunged-tally)
7639               ;; We also mark as read here, if that's wanted.
7640               (when (and gnus-summary-mark-below
7641                          (< score gnus-summary-mark-below))
7642                 (setq gnus-newsgroup-unreads
7643                       (delq number gnus-newsgroup-unreads))
7644                 (if gnus-newsgroup-auto-expire
7645                     (push number gnus-newsgroup-expirable)
7646                   (push (cons number gnus-low-score-mark)
7647                         gnus-newsgroup-reads)))
7648               t)
7649             ;; Do the `display' group parameter.
7650             (and gnus-newsgroup-display
7651                  (not (funcall gnus-newsgroup-display)))
7652             ;; Check NoCeM things.
7653             (if (and gnus-use-nocem
7654                      (gnus-nocem-unwanted-article-p
7655                       (mail-header-id (car thread))))
7656                 (progn
7657                   (setq gnus-newsgroup-unreads
7658                         (delq number gnus-newsgroup-unreads))
7659                   t))))
7660           ;; Nope, invisible article.
7661           0
7662         ;; Ok, this article is to be visible, so we add it to the limit
7663         ;; and return 1.
7664         (push number gnus-newsgroup-limit)
7665         1))))
7666
7667 (defun gnus-expunge-thread (thread)
7668   "Mark all articles in THREAD as read."
7669   (let* ((number (mail-header-number (car thread))))
7670     (incf gnus-newsgroup-expunged-tally)
7671     ;; We also mark as read here, if that's wanted.
7672     (setq gnus-newsgroup-unreads
7673           (delq number gnus-newsgroup-unreads))
7674     (if gnus-newsgroup-auto-expire
7675         (push number gnus-newsgroup-expirable)
7676       (push (cons number gnus-low-score-mark)
7677             gnus-newsgroup-reads)))
7678   ;; Go recursively through all subthreads.
7679   (mapcar 'gnus-expunge-thread (cdr thread)))
7680
7681 ;; Summary article oriented commands
7682
7683 (defun gnus-summary-refer-parent-article (n)
7684   "Refer parent article N times.
7685 If N is negative, go to ancestor -N instead.
7686 The difference between N and the number of articles fetched is returned."
7687   (interactive "p")
7688   (let ((skip 1)
7689         error header ref)
7690     (when (not (natnump n))
7691       (setq skip (abs n)
7692             n 1))
7693     (while (and (> n 0)
7694                 (not error))
7695       (setq header (gnus-summary-article-header))
7696       (if (and (eq (mail-header-number header)
7697                    (cdr gnus-article-current))
7698                (equal gnus-newsgroup-name
7699                       (car gnus-article-current)))
7700           ;; If we try to find the parent of the currently
7701           ;; displayed article, then we take a look at the actual
7702           ;; References header, since this is slightly more
7703           ;; reliable than the References field we got from the
7704           ;; server.
7705           (save-excursion
7706             (set-buffer gnus-original-article-buffer)
7707             (nnheader-narrow-to-headers)
7708             (unless (setq ref (message-fetch-field "references"))
7709               (setq ref (message-fetch-field "in-reply-to")))
7710             (widen))
7711         (setq ref
7712               ;; It's not the current article, so we take a bet on
7713               ;; the value we got from the server.
7714               (mail-header-references header)))
7715       (if (and ref
7716                (not (equal ref "")))
7717           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7718             (gnus-message 1 "Couldn't find parent"))
7719         (gnus-message 1 "No references in article %d"
7720                       (gnus-summary-article-number))
7721         (setq error t))
7722       (decf n))
7723     (gnus-summary-position-point)
7724     n))
7725
7726 (defun gnus-summary-refer-references ()
7727   "Fetch all articles mentioned in the References header.
7728 Return the number of articles fetched."
7729   (interactive)
7730   (let ((ref (mail-header-references (gnus-summary-article-header)))
7731         (current (gnus-summary-article-number))
7732         (n 0))
7733     (if (or (not ref)
7734             (equal ref ""))
7735         (error "No References in the current article")
7736       ;; For each Message-ID in the References header...
7737       (while (string-match "<[^>]*>" ref)
7738         (incf n)
7739         ;; ... fetch that article.
7740         (gnus-summary-refer-article
7741          (prog1 (match-string 0 ref)
7742            (setq ref (substring ref (match-end 0))))))
7743       (gnus-summary-goto-subject current)
7744       (gnus-summary-position-point)
7745       n)))
7746
7747 (defun gnus-summary-refer-thread (&optional limit)
7748   "Fetch all articles in the current thread.
7749 If LIMIT (the numerical prefix), fetch that many old headers instead
7750 of what's specified by the `gnus-refer-thread-limit' variable."
7751   (interactive "P")
7752   (let ((id (mail-header-id (gnus-summary-article-header)))
7753         (limit (if limit (prefix-numeric-value limit)
7754                  gnus-refer-thread-limit)))
7755     ;; We want to fetch LIMIT *old* headers, but we also have to
7756     ;; re-fetch all the headers in the current buffer, because many of
7757     ;; them may be undisplayed.  So we adjust LIMIT.
7758     (when (numberp limit)
7759       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7760     (unless (eq gnus-fetch-old-headers 'invisible)
7761       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7762       ;; Retrieve the headers and read them in.
7763       (if (eq (gnus-retrieve-headers
7764                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7765               'nov)
7766           (gnus-build-all-threads)
7767         (error "Can't fetch thread from backends that don't support NOV"))
7768       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7769     (gnus-summary-limit-include-thread id)))
7770
7771 (defun gnus-summary-refer-article (message-id)
7772   "Fetch an article specified by MESSAGE-ID."
7773   (interactive "sMessage-ID: ")
7774   (when (and (stringp message-id)
7775              (not (zerop (length message-id))))
7776     ;; Construct the correct Message-ID if necessary.
7777     ;; Suggested by tale@pawl.rpi.edu.
7778     (unless (string-match "^<" message-id)
7779       (setq message-id (concat "<" message-id)))
7780     (unless (string-match ">$" message-id)
7781       (setq message-id (concat message-id ">")))
7782     (let* ((header (gnus-id-to-header message-id))
7783            (sparse (and header
7784                         (gnus-summary-article-sparse-p
7785                          (mail-header-number header))
7786                         (memq (mail-header-number header)
7787                               gnus-newsgroup-limit)))
7788            number)
7789       (cond
7790        ;; If the article is present in the buffer we just go to it.
7791        ((and header
7792              (or (not (gnus-summary-article-sparse-p
7793                        (mail-header-number header)))
7794                  sparse))
7795         (prog1
7796             (gnus-summary-goto-article
7797              (mail-header-number header) nil t)
7798           (when sparse
7799             (gnus-summary-update-article (mail-header-number header)))))
7800        (t
7801         ;; We fetch the article.
7802         (catch 'found
7803           (dolist (gnus-override-method (gnus-refer-article-methods))
7804             (gnus-check-server gnus-override-method)
7805             ;; Fetch the header, and display the article.
7806             (when (setq number (gnus-summary-insert-subject message-id))
7807               (gnus-summary-select-article nil nil nil number)
7808               (throw 'found t)))
7809           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7810
7811 (defun gnus-refer-article-methods ()
7812   "Return a list of referrable methods."
7813   (cond
7814    ;; No method, so we default to current and native.
7815    ((null gnus-refer-article-method)
7816     (list gnus-current-select-method gnus-select-method))
7817    ;; Current.
7818    ((eq 'current gnus-refer-article-method)
7819     (list gnus-current-select-method))
7820    ;; List of select methods.
7821    ((not (and (symbolp (car gnus-refer-article-method))
7822               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7823     (let (out)
7824       (dolist (method gnus-refer-article-method)
7825         (push (if (eq 'current method)
7826                   gnus-current-select-method
7827                 method)
7828               out))
7829       (nreverse out)))
7830    ;; One single select method.
7831    (t
7832     (list gnus-refer-article-method))))
7833
7834 (defun gnus-summary-edit-parameters ()
7835   "Edit the group parameters of the current group."
7836   (interactive)
7837   (gnus-group-edit-group gnus-newsgroup-name 'params))
7838
7839 (defun gnus-summary-customize-parameters ()
7840   "Customize the group parameters of the current group."
7841   (interactive)
7842   (gnus-group-customize gnus-newsgroup-name))
7843
7844 (defun gnus-summary-enter-digest-group (&optional force)
7845   "Enter an nndoc group based on the current article.
7846 If FORCE, force a digest interpretation.  If not, try
7847 to guess what the document format is."
7848   (interactive "P")
7849   (let ((conf gnus-current-window-configuration))
7850     (save-excursion
7851       (gnus-summary-select-article))
7852     (setq gnus-current-window-configuration conf)
7853     (let* ((name (format "%s-%d"
7854                          (gnus-group-prefixed-name
7855                           gnus-newsgroup-name (list 'nndoc ""))
7856                          (save-excursion
7857                            (set-buffer gnus-summary-buffer)
7858                            gnus-current-article)))
7859            (ogroup gnus-newsgroup-name)
7860            (params (append (gnus-info-params (gnus-get-info ogroup))
7861                            (list (cons 'to-group ogroup))
7862                            (list (cons 'save-article-group ogroup))))
7863            (case-fold-search t)
7864            (buf (current-buffer))
7865            dig to-address)
7866       (save-excursion
7867         (set-buffer gnus-original-article-buffer)
7868         ;; Have the digest group inherit the main mail address of
7869         ;; the parent article.
7870         (when (setq to-address (or (gnus-fetch-field "reply-to")
7871                                    (gnus-fetch-field "from")))
7872           (setq params (append
7873                         (list (cons 'to-address
7874                                     (funcall gnus-decode-encoded-word-function
7875                                              to-address))))))
7876         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7877         (insert-buffer-substring gnus-original-article-buffer)
7878         ;; Remove lines that may lead nndoc to misinterpret the
7879         ;; document type.
7880         (narrow-to-region
7881          (goto-char (point-min))
7882          (or (search-forward "\n\n" nil t) (point)))
7883         (goto-char (point-min))
7884         (delete-matching-lines "^Path:\\|^From ")
7885         (widen))
7886       (unwind-protect
7887           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7888                     (gnus-newsgroup-ephemeral-ignored-charsets
7889                      gnus-newsgroup-ignored-charsets))
7890                 (gnus-group-read-ephemeral-group
7891                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7892                               (nndoc-article-type
7893                                ,(if force 'mbox 'guess)))
7894                  t nil nil nil
7895                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7896                                                         "ADAPT")))))
7897               ;; Make all postings to this group go to the parent group.
7898               (nconc (gnus-info-params (gnus-get-info name))
7899                      params)
7900             ;; Couldn't select this doc group.
7901             (switch-to-buffer buf)
7902             (gnus-set-global-variables)
7903             (gnus-configure-windows 'summary)
7904             (gnus-message 3 "Article couldn't be entered?"))
7905         (kill-buffer dig)))))
7906
7907 (defun gnus-summary-read-document (n)
7908   "Open a new group based on the current article(s).
7909 This will allow you to read digests and other similar
7910 documents as newsgroups.
7911 Obeys the standard process/prefix convention."
7912   (interactive "P")
7913   (let* ((articles (gnus-summary-work-articles n))
7914          (ogroup gnus-newsgroup-name)
7915          (params (append (gnus-info-params (gnus-get-info ogroup))
7916                          (list (cons 'to-group ogroup))))
7917          article group egroup groups vgroup)
7918     (while (setq article (pop articles))
7919       (setq group (format "%s-%d" gnus-newsgroup-name article))
7920       (gnus-summary-remove-process-mark article)
7921       (when (gnus-summary-display-article article)
7922         (save-excursion
7923           (with-temp-buffer
7924             (insert-buffer-substring gnus-original-article-buffer)
7925             ;; Remove some headers that may lead nndoc to make
7926             ;; the wrong guess.
7927             (message-narrow-to-head)
7928             (goto-char (point-min))
7929             (delete-matching-lines "^\\(Path\\):\\|^From ")
7930             (widen)
7931             (if (setq egroup
7932                       (gnus-group-read-ephemeral-group
7933                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7934                                      (nndoc-article-type guess))
7935                        t nil t))
7936                 (progn
7937                   ;; Make all postings to this group go to the parent group.
7938                   (nconc (gnus-info-params (gnus-get-info egroup))
7939                          params)
7940                   (push egroup groups))
7941               ;; Couldn't select this doc group.
7942               (gnus-error 3 "Article couldn't be entered"))))))
7943     ;; Now we have selected all the documents.
7944     (cond
7945      ((not groups)
7946       (error "None of the articles could be interpreted as documents"))
7947      ((gnus-group-read-ephemeral-group
7948        (setq vgroup (format
7949                      "nnvirtual:%s-%s" gnus-newsgroup-name
7950                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7951        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7952        t
7953        (cons (current-buffer) 'summary)))
7954      (t
7955       (error "Couldn't select virtual nndoc group")))))
7956
7957 (defun gnus-summary-isearch-article (&optional regexp-p)
7958   "Do incremental search forward on the current article.
7959 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7960   (interactive "P")
7961   (let* ((gnus-inhibit-treatment t)
7962          (old (gnus-summary-select-article)))
7963     (gnus-configure-windows 'article)
7964     (gnus-eval-in-buffer-window gnus-article-buffer
7965       (save-restriction
7966         (widen)
7967         (when (eq 'old old)
7968           (gnus-article-show-all-headers))
7969         (goto-char (point-min))
7970         (isearch-forward regexp-p)))))
7971
7972 (defun gnus-summary-search-article-forward (regexp &optional backward)
7973   "Search for an article containing REGEXP forward.
7974 If BACKWARD, search backward instead."
7975   (interactive
7976    (list (read-string
7977           (format "Search article %s (regexp%s): "
7978                   (if current-prefix-arg "backward" "forward")
7979                   (if gnus-last-search-regexp
7980                       (concat ", default " gnus-last-search-regexp)
7981                     "")))
7982          current-prefix-arg))
7983   (if (string-equal regexp "")
7984       (setq regexp (or gnus-last-search-regexp ""))
7985     (setq gnus-last-search-regexp regexp)
7986     (setq gnus-article-before-search gnus-current-article))
7987   ;; Intentionally set gnus-last-article.
7988   (setq gnus-last-article gnus-article-before-search)
7989   (let ((gnus-last-article gnus-last-article))
7990     (if (gnus-summary-search-article regexp backward)
7991         (gnus-summary-show-thread)
7992       (error "Search failed: \"%s\"" regexp))))
7993
7994 (defun gnus-summary-search-article-backward (regexp)
7995   "Search for an article containing REGEXP backward."
7996   (interactive
7997    (list (read-string
7998           (format "Search article backward (regexp%s): "
7999                   (if gnus-last-search-regexp
8000                       (concat ", default " gnus-last-search-regexp)
8001                     "")))))
8002   (gnus-summary-search-article-forward regexp 'backward))
8003
8004 (eval-when-compile
8005   (defmacro gnus-summary-search-article-position-point (regexp backward)
8006     "Dehighlight the last matched text and goto the beginning position."
8007     (` (if (and gnus-summary-search-article-matched-data
8008                 (let ((text (caddr gnus-summary-search-article-matched-data))
8009                       (inhibit-read-only t)
8010                       buffer-read-only)
8011                   (delete-region
8012                    (goto-char (car gnus-summary-search-article-matched-data))
8013                    (cadr gnus-summary-search-article-matched-data))
8014                   (insert text)
8015                   (string-match (, regexp) text)))
8016            (if (, backward) (beginning-of-line) (end-of-line))
8017          (goto-char (if (, backward) (point-max) (point-min))))))
8018
8019   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8020     "Place point where X-Face image is displayed."
8021     (if (featurep 'xemacs)
8022         (` (let ((end (if (search-forward "\n\n" nil t)
8023                           (goto-char (1- (point)))
8024                         (point-min)))
8025                  extent)
8026              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8027              (unless (and (re-search-forward "^From:" end t)
8028                           (setq extent (extent-at (point)))
8029                           (extent-begin-glyph extent))
8030                (goto-char (, opoint)))))
8031       (` (let ((end (if (search-forward "\n\n" nil t)
8032                         (goto-char (1- (point)))
8033                       (point-min)))
8034                (start (or (search-backward "\n\n" nil t) (point-min))))
8035            (goto-char
8036             (or (text-property-any start end 'x-face-image t);; x-face-e21
8037                 (text-property-any start end 'x-face-mule-bitmap-image t)
8038                 (, opoint)))))))
8039
8040   (defmacro gnus-summary-search-article-highlight-matched-text
8041     (backward treated x-face)
8042     "Highlight matched text in the function `gnus-summary-search-article'."
8043     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8044              (end (set-marker (make-marker) (match-end 0)))
8045              (inhibit-read-only t)
8046              buffer-read-only)
8047          (unless treated
8048            (let ((,@
8049                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8050                     (mapcar
8051                      (lambda (item) (setq items (delq item items)))
8052                      '(gnus-treat-buttonize
8053                        gnus-treat-fill-article
8054                        gnus-treat-fill-long-lines
8055                        gnus-treat-emphasize
8056                        gnus-treat-highlight-headers
8057                        gnus-treat-highlight-citation
8058                        gnus-treat-highlight-signature
8059                        gnus-treat-overstrike
8060                        gnus-treat-display-xface
8061                        gnus-treat-buttonize-head
8062                        gnus-treat-decode-article-as-default-mime-charset))
8063                     (static-if (featurep 'xemacs)
8064                         items
8065                       (cons '(x-face-mule-delete-x-face-field
8066                               (quote never))
8067                             items))))
8068                  (gnus-treat-display-xface
8069                   (when (, x-face) gnus-treat-display-xface)))
8070              (gnus-article-prepare-mime-display)))
8071          (goto-char (if (, backward) start end))
8072          (when (, x-face)
8073            (gnus-summary-search-article-highlight-goto-x-face (point)))
8074          (setq gnus-summary-search-article-matched-data
8075                (list start end (buffer-substring start end)))
8076          (unless (eq start end);; matched text has been deleted. :-<
8077            (put-text-property start end 'face
8078                               (or (find-face 'isearch)
8079                                   'secondary-selection))))))
8080   )
8081
8082 (defun gnus-summary-search-article (regexp &optional backward)
8083   "Search for an article containing REGEXP.
8084 Optional argument BACKWARD means do search for backward.
8085 `gnus-select-article-hook' is not called during the search."
8086   ;; We have to require this here to make sure that the following
8087   ;; dynamic binding isn't shadowed by autoloading.
8088   (require 'gnus-async)
8089   (require 'gnus-art)
8090   (let ((gnus-select-article-hook nil)  ;Disable hook.
8091         (gnus-article-prepare-hook nil)
8092         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8093         (gnus-use-article-prefetch nil)
8094         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8095         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8096         (sum (current-buffer))
8097         (found nil)
8098         point treated)
8099     (gnus-save-hidden-threads
8100       (static-if (featurep 'xemacs)
8101           (let ((gnus-inhibit-treatment t))
8102             (setq treated (eq 'old (gnus-summary-select-article)))
8103             (when (and treated
8104                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8105                                  (window-live-p (get-buffer-window
8106                                                  gnus-article-buffer t)))))
8107               (gnus-summary-select-article nil t)
8108               (setq treated nil)))
8109         (let ((gnus-inhibit-treatment t)
8110               (x-face-mule-delete-x-face-field 'never))
8111           (setq treated (eq 'old (gnus-summary-select-article)))
8112           (when (and treated
8113                      (not
8114                       (and (gnus-buffer-live-p gnus-article-buffer)
8115                            (window-live-p (get-buffer-window
8116                                            gnus-article-buffer t))
8117                            (or (not (string-match "^\\^X-Face:" regexp))
8118                                (with-current-buffer gnus-article-buffer
8119                                  gnus-summary-search-article-matched-data)))))
8120             (gnus-summary-select-article nil t)
8121             (setq treated nil))))
8122       (set-buffer gnus-article-buffer)
8123       (widen)
8124       (if treated
8125           (progn
8126             (gnus-article-show-all-headers)
8127             (gnus-summary-search-article-position-point regexp backward))
8128         (goto-char (if backward (point-max) (point-min))))
8129       (while (not found)
8130         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8131         (if (if backward
8132                 (re-search-backward regexp nil t)
8133               (re-search-forward regexp nil t))
8134             ;; We found the regexp.
8135             (progn
8136               (gnus-summary-search-article-highlight-matched-text
8137                backward treated (string-match "^\\^X-Face:" regexp))
8138               (setq found 'found)
8139               (forward-line
8140                (/ (- 2 (window-height
8141                         (get-buffer-window gnus-article-buffer t)))
8142                   2))
8143               (set-window-start
8144                (get-buffer-window (current-buffer))
8145                (point))
8146               (set-buffer sum)
8147               (setq point (point)))
8148           ;; We didn't find it, so we go to the next article.
8149           (set-buffer sum)
8150           (setq found 'not)
8151           (while (eq found 'not)
8152             (if (not (if backward (gnus-summary-find-prev)
8153                        (gnus-summary-find-next)))
8154                 ;; No more articles.
8155                 (setq found t)
8156               ;; Select the next article and adjust point.
8157               (unless (gnus-summary-article-sparse-p
8158                        (gnus-summary-article-number))
8159                 (setq found nil)
8160                 (let ((gnus-inhibit-treatment t))
8161                   (gnus-summary-select-article))
8162                 (setq treated nil)
8163                 (set-buffer gnus-article-buffer)
8164                 (widen)
8165                 (goto-char (if backward (point-max) (point-min))))))))
8166       (gnus-message 7 ""))
8167     ;; Return whether we found the regexp.
8168     (when (eq found 'found)
8169       (goto-char point)
8170       (gnus-summary-show-thread)
8171       (gnus-summary-goto-subject gnus-current-article)
8172       (gnus-summary-position-point)
8173       t)))
8174
8175 (defun gnus-find-matching-articles (header regexp)
8176   "Return a list of all articles that match REGEXP on HEADER.
8177 This search includes all articles in the current group that Gnus has
8178 fetched headers for, whether they are displayed or not."
8179   (let ((articles nil)
8180         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8181         (case-fold-search t))
8182     (dolist (header gnus-newsgroup-headers)
8183       (when (string-match regexp (funcall func header))
8184         (push (mail-header-number header) articles)))
8185     (nreverse articles)))
8186
8187 (defun gnus-summary-find-matching (header regexp &optional backward unread
8188                                           not-case-fold not-matching)
8189   "Return a list of all articles that match REGEXP on HEADER.
8190 The search stars on the current article and goes forwards unless
8191 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8192 If UNREAD is non-nil, only unread articles will
8193 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8194 in the comparisons. If NOT-MATCHING, return a list of all articles that
8195 not match REGEXP on HEADER."
8196   (let ((case-fold-search (not not-case-fold))
8197         articles d func)
8198     (if (consp header)
8199         (if (eq (car header) 'extra)
8200             (setq func
8201                   `(lambda (h)
8202                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8203                          "")))
8204           (error "%s is an invalid header" header))
8205       (unless (fboundp (intern (concat "mail-header-" header)))
8206         (error "%s is not a valid header" header))
8207       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8208     (dolist (d (if (eq backward 'all)
8209                    gnus-newsgroup-data
8210                  (gnus-data-find-list
8211                   (gnus-summary-article-number)
8212                   (gnus-data-list backward))))
8213       (when (and (or (not unread)       ; We want all articles...
8214                      (gnus-data-unread-p d)) ; Or just unreads.
8215                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8216                  (if not-matching
8217                      (not (string-match
8218                            regexp
8219                            (funcall func (gnus-data-header d))))
8220                    (string-match regexp
8221                                  (funcall func (gnus-data-header d)))))
8222         (push (gnus-data-number d) articles))) ; Success!
8223     (nreverse articles)))
8224
8225 (defun gnus-summary-execute-command (header regexp command &optional backward)
8226   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8227 If HEADER is an empty string (or nil), the match is done on the entire
8228 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8229   (interactive
8230    (list (let ((completion-ignore-case t))
8231            (completing-read
8232             "Header name: "
8233             (mapcar (lambda (header) (list (format "%s" header)))
8234                     (append
8235                      '("Number" "Subject" "From" "Lines" "Date"
8236                        "Message-ID" "Xref" "References" "Body")
8237                      gnus-extra-headers))
8238             nil 'require-match))
8239          (read-string "Regexp: ")
8240          (read-key-sequence "Command: ")
8241          current-prefix-arg))
8242   (when (equal header "Body")
8243     (setq header ""))
8244   ;; Hidden thread subtrees must be searched as well.
8245   (gnus-summary-show-all-threads)
8246   ;; We don't want to change current point nor window configuration.
8247   (save-excursion
8248     (save-window-excursion
8249       (gnus-message 6 "Executing %s..." (key-description command))
8250       ;; We'd like to execute COMMAND interactively so as to give arguments.
8251       (gnus-execute header regexp
8252                     `(call-interactively ',(key-binding command))
8253                     backward)
8254       (gnus-message 6 "Executing %s...done" (key-description command)))))
8255
8256 (defun gnus-summary-beginning-of-article ()
8257   "Scroll the article back to the beginning."
8258   (interactive)
8259   (gnus-summary-select-article)
8260   (gnus-configure-windows 'article)
8261   (gnus-eval-in-buffer-window gnus-article-buffer
8262     (widen)
8263     (goto-char (point-min))
8264     (when gnus-page-broken
8265       (gnus-narrow-to-page))))
8266
8267 (defun gnus-summary-end-of-article ()
8268   "Scroll to the end of the article."
8269   (interactive)
8270   (gnus-summary-select-article)
8271   (gnus-configure-windows 'article)
8272   (gnus-eval-in-buffer-window gnus-article-buffer
8273     (widen)
8274     (goto-char (point-max))
8275     (recenter -3)
8276     (when gnus-page-broken
8277       (gnus-narrow-to-page))))
8278
8279 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8280   "Truncate to LEN and quote all \"(\"'s in STRING."
8281   (gnus-replace-in-string (if (and len (> (length string) len))
8282                               (substring string 0 len)
8283                             string)
8284                           "[()]" "\\\\\\&"))
8285
8286 (defun gnus-summary-print-article (&optional filename n)
8287   "Generate and print a PostScript image of the N next (mail) articles.
8288
8289 If N is negative, print the N previous articles.  If N is nil and articles
8290 have been marked with the process mark, print these instead.
8291
8292 If the optional first argument FILENAME is nil, send the image to the
8293 printer.  If FILENAME is a string, save the PostScript image in a file with
8294 that name.  If FILENAME is a number, prompt the user for the name of the file
8295 to save in."
8296   (interactive (list (ps-print-preprint current-prefix-arg)))
8297   (dolist (article (gnus-summary-work-articles n))
8298     (gnus-summary-select-article nil nil 'pseudo article)
8299     (gnus-eval-in-buffer-window gnus-article-buffer
8300       (gnus-print-buffer))
8301     (gnus-summary-remove-process-mark article))
8302   (ps-despool filename))
8303
8304 (defun gnus-print-buffer ()
8305   (let ((buffer (generate-new-buffer " *print*")))
8306     (unwind-protect
8307         (progn
8308           (copy-to-buffer buffer (point-min) (point-max))
8309           (set-buffer buffer)
8310           (gnus-article-delete-invisible-text)
8311           (gnus-remove-text-with-property 'gnus-decoration)
8312           (when (gnus-visual-p 'article-highlight 'highlight)
8313             ;; Copy-to-buffer doesn't copy overlay.  So redo
8314             ;; highlight.
8315             (let ((gnus-article-buffer buffer))
8316               (gnus-article-highlight-citation t)
8317               (gnus-article-highlight-signature)))
8318           (let ((ps-left-header
8319                  (list
8320                   (concat "("
8321                           (gnus-summary-print-truncate-and-quote
8322                            (mail-header-subject gnus-current-headers)
8323                            66) ")")
8324                   (concat "("
8325                           (gnus-summary-print-truncate-and-quote
8326                            (mail-header-from gnus-current-headers)
8327                            45) ")")))
8328                 (ps-right-header
8329                  (list
8330                   "/pagenumberstring load"
8331                   (concat "("
8332                           (mail-header-date gnus-current-headers) ")"))))
8333             (gnus-run-hooks 'gnus-ps-print-hook)
8334             (save-excursion
8335               (if window-system
8336                   (ps-spool-buffer-with-faces)
8337                 (ps-spool-buffer)))))
8338       (kill-buffer buffer))))
8339
8340 (defun gnus-summary-show-article (&optional arg)
8341   "Force redisplaying of the current article.
8342 If ARG (the prefix) is a number, show the article with the charset
8343 defined in `gnus-summary-show-article-charset-alist', or the charset
8344 input.
8345 If ARG (the prefix) is non-nil and not a number, show the raw article
8346 without any article massaging functions being run.  Normally, the key strokes
8347 are `C-u g'."
8348   (interactive "P")
8349   (cond
8350    ((numberp arg)
8351     (gnus-summary-show-article t)
8352     (let* ((gnus-newsgroup-charset
8353             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8354                 (mm-read-coding-system
8355                  "View as charset: " ;; actually it is coding system.
8356                  (save-excursion
8357                    (set-buffer gnus-article-buffer)
8358                    (mm-detect-coding-region (point) (point-max))))))
8359            (default-mime-charset gnus-newsgroup-charset)
8360            (gnus-newsgroup-ignored-charsets 'gnus-all))
8361       (gnus-summary-select-article nil 'force)
8362       (let ((deps gnus-newsgroup-dependencies)
8363             head header lines)
8364         (save-excursion
8365           (set-buffer gnus-original-article-buffer)
8366           (save-restriction
8367             (message-narrow-to-head)
8368             (setq head (buffer-string))
8369             (goto-char (point-min))
8370             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8371               (goto-char (point-max))
8372               (widen)
8373               (setq lines (1- (count-lines (point) (point-max))))))
8374           (with-temp-buffer
8375             (insert (format "211 %d Article retrieved.\n"
8376                             (cdr gnus-article-current)))
8377             (insert head)
8378             (if lines (insert (format "Lines: %d\n" lines)))
8379             (insert ".\n")
8380             (let ((nntp-server-buffer (current-buffer)))
8381               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8382         (gnus-data-set-header
8383          (gnus-data-find (cdr gnus-article-current))
8384          header)
8385         (gnus-summary-update-article-line
8386          (cdr gnus-article-current) header)
8387         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8388           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8389    ((not arg)
8390     ;; Select the article the normal way.
8391     (gnus-summary-select-article nil 'force))
8392    (t
8393     ;; We have to require this here to make sure that the following
8394     ;; dynamic binding isn't shadowed by autoloading.
8395     (require 'gnus-async)
8396     (require 'gnus-art)
8397     ;; Bind the article treatment functions to nil.
8398     (let ((gnus-have-all-headers t)
8399           gnus-article-prepare-hook
8400           gnus-article-decode-hook
8401           gnus-break-pages
8402           gnus-show-mime
8403           (gnus-inhibit-treatment t))
8404       (gnus-summary-select-article nil 'force))))
8405   (gnus-summary-goto-subject gnus-current-article)
8406   (gnus-summary-position-point))
8407
8408 (defun gnus-summary-show-raw-article ()
8409   "Show the raw article without any article massaging functions being run."
8410   (interactive)
8411   (gnus-summary-show-article t))
8412
8413 (defun gnus-summary-verbose-headers (&optional arg)
8414   "Toggle permanent full header display.
8415 If ARG is a positive number, turn header display on.
8416 If ARG is a negative number, turn header display off."
8417   (interactive "P")
8418   (setq gnus-show-all-headers
8419         (cond ((or (not (numberp arg))
8420                    (zerop arg))
8421                (not gnus-show-all-headers))
8422               ((natnump arg)
8423                t)))
8424   (gnus-summary-show-article))
8425
8426 (defun gnus-summary-toggle-header (&optional arg)
8427   "Show the headers if they are hidden, or hide them if they are shown.
8428 If ARG is a positive number, show the entire header.
8429 If ARG is a negative number, hide the unwanted header lines."
8430   (interactive "P")
8431   (save-excursion
8432     (set-buffer gnus-article-buffer)
8433     (save-restriction
8434       (let* ((buffer-read-only nil)
8435              (inhibit-point-motion-hooks t)
8436              hidden s e)
8437         (save-restriction
8438           (article-narrow-to-head)
8439           (setq e (point-max)
8440                 hidden (if (numberp arg)
8441                            (>= arg 0)
8442                          (gnus-article-hidden-text-p 'headers))))
8443         (delete-region (point-min) e)
8444         (goto-char (point-min))
8445         (with-current-buffer gnus-original-article-buffer
8446           (goto-char (setq s (point-min)))
8447           (setq e (search-forward "\n\n" nil t)
8448                 e (if e (1- e) (point-max))))
8449         (insert-buffer-substring gnus-original-article-buffer s e)
8450         (save-restriction
8451           (narrow-to-region (point-min) (point))
8452           (article-decode-encoded-words)
8453           (if  hidden
8454               (let ((gnus-treat-hide-headers nil)
8455                     (gnus-treat-hide-boring-headers nil))
8456                 (gnus-delete-wash-type 'headers)
8457                 (gnus-treat-article 'head))
8458             (gnus-treat-article 'head)))
8459         (gnus-set-mode-line 'article)))))
8460
8461 (defun gnus-summary-show-all-headers ()
8462   "Make all header lines visible."
8463   (interactive)
8464   (gnus-summary-toggle-header 1))
8465
8466 (defun gnus-summary-toggle-mime (&optional arg)
8467   "Toggle MIME processing.
8468 If ARG is a positive number, turn MIME processing on."
8469   (interactive "P")
8470   (setq gnus-show-mime
8471         (if (null arg)
8472             (not gnus-show-mime)
8473           (> (prefix-numeric-value arg) 0)))
8474   (gnus-summary-select-article t 'force))
8475
8476 (defun gnus-summary-caesar-message (&optional arg)
8477   "Caesar rotate the current article by 13.
8478 The numerical prefix specifies how many places to rotate each letter
8479 forward."
8480   (interactive "P")
8481   (gnus-summary-select-article)
8482   (let ((mail-header-separator ""))
8483     (gnus-eval-in-buffer-window gnus-article-buffer
8484       (save-restriction
8485         (widen)
8486         (let ((start (window-start))
8487               buffer-read-only)
8488           (message-caesar-buffer-body arg)
8489           (set-window-start (get-buffer-window (current-buffer)) start))))))
8490
8491 (defun gnus-summary-stop-page-breaking ()
8492   "Stop page breaking in the current article."
8493   (interactive)
8494   (gnus-summary-select-article)
8495   (gnus-eval-in-buffer-window gnus-article-buffer
8496     (widen)
8497     (when (gnus-visual-p 'page-marker)
8498       (let ((buffer-read-only nil))
8499         (gnus-remove-text-with-property 'gnus-prev)
8500         (gnus-remove-text-with-property 'gnus-next))
8501       (setq gnus-page-broken nil))))
8502
8503 (defun gnus-summary-move-article (&optional n to-newsgroup
8504                                             select-method action)
8505   "Move the current article to a different newsgroup.
8506 If N is a positive number, move the N next articles.
8507 If N is a negative number, move the N previous articles.
8508 If N is nil and any articles have been marked with the process mark,
8509 move those articles instead.
8510 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8511 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8512 re-spool using this method.
8513
8514 When called interactively with TO-NEWSGROUP being nil, the value of
8515 the variable `gnus-move-split-methods' is used for finding a default
8516 for the target newsgroup.
8517
8518 For this function to work, both the current newsgroup and the
8519 newsgroup that you want to move to have to support the `request-move'
8520 and `request-accept' functions.
8521
8522 ACTION can be either `move' (the default), `crosspost' or `copy'."
8523   (interactive "P")
8524   (unless action
8525     (setq action 'move))
8526   ;; Check whether the source group supports the required functions.
8527   (cond ((and (eq action 'move)
8528               (not (gnus-check-backend-function
8529                     'request-move-article gnus-newsgroup-name)))
8530          (error "The current group does not support article moving"))
8531         ((and (eq action 'crosspost)
8532               (not (gnus-check-backend-function
8533                     'request-replace-article gnus-newsgroup-name)))
8534          (error "The current group does not support article editing")))
8535   (let ((articles (gnus-summary-work-articles n))
8536         (prefix (if (gnus-check-backend-function
8537                      'request-move-article gnus-newsgroup-name)
8538                     (gnus-group-real-prefix gnus-newsgroup-name)
8539                   ""))
8540         (names '((move "Move" "Moving")
8541                  (copy "Copy" "Copying")
8542                  (crosspost "Crosspost" "Crossposting")))
8543         (copy-buf (save-excursion
8544                     (nnheader-set-temp-buffer " *copy article*")))
8545         (default-marks gnus-article-mark-lists)
8546         (no-expire-marks (delete '(expirable . expire)
8547                                  (copy-sequence gnus-article-mark-lists)))
8548         art-group to-method new-xref article to-groups)
8549     (unless (assq action names)
8550       (error "Unknown action %s" action))
8551     ;; Read the newsgroup name.
8552     (when (and (not to-newsgroup)
8553                (not select-method))
8554       (if (and gnus-move-split-methods
8555                (not
8556                 (and (memq gnus-current-article articles)
8557                      (gnus-buffer-live-p gnus-original-article-buffer))))
8558           ;; When `gnus-move-split-methods' is non-nil, we have to
8559           ;; select an article to give `gnus-read-move-group-name' an
8560           ;; opportunity to suggest an appropriate default.  However,
8561           ;; we needn't render or mark the article.
8562           (let ((gnus-display-mime-function nil)
8563                 (gnus-article-prepare-hook nil)
8564                 (gnus-mark-article-hook nil))
8565             (gnus-summary-select-article nil nil nil (car articles))))
8566       (setq to-newsgroup
8567             (gnus-read-move-group-name
8568              (cadr (assq action names))
8569              (symbol-value (intern (format "gnus-current-%s-group" action)))
8570              articles prefix))
8571       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8572     (setq to-method (or select-method
8573                         (gnus-server-to-method
8574                          (gnus-group-method to-newsgroup))))
8575     ;; Check the method we are to move this article to...
8576     (unless (gnus-check-backend-function
8577              'request-accept-article (car to-method))
8578       (error "%s does not support article copying" (car to-method)))
8579     (unless (gnus-check-server to-method)
8580       (error "Can't open server %s" (car to-method)))
8581     (gnus-message 6 "%s to %s: %s..."
8582                   (caddr (assq action names))
8583                   (or (car select-method) to-newsgroup) articles)
8584     (while articles
8585       (setq article (pop articles))
8586       (setq
8587        art-group
8588        (cond
8589         ;; Move the article.
8590         ((eq action 'move)
8591          ;; Remove this article from future suppression.
8592          (gnus-dup-unsuppress-article article)
8593          (gnus-request-move-article
8594           article                       ; Article to move
8595           gnus-newsgroup-name           ; From newsgroup
8596           (nth 1 (gnus-find-method-for-group
8597                   gnus-newsgroup-name)) ; Server
8598           (list 'gnus-request-accept-article
8599                 to-newsgroup (list 'quote select-method)
8600                 (not articles) t)       ; Accept form
8601           (not articles)))              ; Only save nov last time
8602         ;; Copy the article.
8603         ((eq action 'copy)
8604          (save-excursion
8605            (set-buffer copy-buf)
8606            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8607              (gnus-request-accept-article
8608               to-newsgroup select-method (not articles) t))))
8609         ;; Crosspost the article.
8610         ((eq action 'crosspost)
8611          (let ((xref (message-tokenize-header
8612                       (mail-header-xref (gnus-summary-article-header article))
8613                       " ")))
8614            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8615                                   ":" (number-to-string article)))
8616            (unless xref
8617              (setq xref (list (system-name))))
8618            (setq new-xref
8619                  (concat
8620                   (mapconcat 'identity
8621                              (delete "Xref:" (delete new-xref xref))
8622                              " ")
8623                   " " new-xref))
8624            (save-excursion
8625              (set-buffer copy-buf)
8626              ;; First put the article in the destination group.
8627              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8628              (when (consp (setq art-group
8629                                 (gnus-request-accept-article
8630                                  to-newsgroup select-method (not articles))))
8631                (setq new-xref (concat new-xref " " (car art-group)
8632                                       ":"
8633                                       (number-to-string (cdr art-group))))
8634                ;; Now we have the new Xrefs header, so we insert
8635                ;; it and replace the new article.
8636                (nnheader-replace-header "Xref" new-xref)
8637                (gnus-request-replace-article
8638                 (cdr art-group) to-newsgroup (current-buffer))
8639                art-group))))))
8640       (cond
8641        ((not art-group)
8642         (gnus-message 1 "Couldn't %s article %s: %s"
8643                       (cadr (assq action names)) article
8644                       (nnheader-get-report (car to-method))))
8645        ((eq art-group 'junk)
8646         (when (eq action 'move)
8647           (gnus-summary-mark-article article gnus-canceled-mark)
8648           (gnus-message 4 "Deleted article %s" article)))
8649        (t
8650         (let* ((pto-group (gnus-group-prefixed-name
8651                            (car art-group) to-method))
8652                (entry
8653                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8654                (info (nth 2 entry))
8655                (to-group (gnus-info-group info))
8656                to-marks)
8657           ;; Update the group that has been moved to.
8658           (when (and info
8659                      (memq action '(move copy)))
8660             (unless (member to-group to-groups)
8661               (push to-group to-groups))
8662
8663             (unless (memq article gnus-newsgroup-unreads)
8664               (push 'read to-marks)
8665               (gnus-info-set-read
8666                info (gnus-add-to-range (gnus-info-read info)
8667                                        (list (cdr art-group)))))
8668
8669             ;; See whether the article is to be put in the cache.
8670             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8671                              default-marks
8672                            no-expire-marks))
8673                   (to-article (cdr art-group)))
8674
8675               ;; Enter the article into the cache in the new group,
8676               ;; if that is required.
8677               (when gnus-use-cache
8678                 (gnus-cache-possibly-enter-article
8679                  to-group to-article
8680                  (let ((header (copy-sequence
8681                                 (gnus-summary-article-header article))))
8682                    (mail-header-set-number header to-article)
8683                    header)
8684                  (memq article gnus-newsgroup-marked)
8685                  (memq article gnus-newsgroup-dormant)
8686                  (memq article gnus-newsgroup-unreads)))
8687
8688               (when gnus-preserve-marks
8689                 ;; Copy any marks over to the new group.
8690                 (when (and (equal to-group gnus-newsgroup-name)
8691                            (not (memq article gnus-newsgroup-unreads)))
8692                   ;; Mark this article as read in this group.
8693                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8694                   (setcdr (gnus-active to-group) to-article)
8695                   (setcdr gnus-newsgroup-active to-article))
8696
8697                 (while marks
8698                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8699                     (when (memq article (symbol-value
8700                                          (intern (format "gnus-newsgroup-%s"
8701                                                          (caar marks)))))
8702                       (push (cdar marks) to-marks)
8703                       ;; If the other group is the same as this group,
8704                       ;; then we have to add the mark to the list.
8705                       (when (equal to-group gnus-newsgroup-name)
8706                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8707                              (cons to-article
8708                                    (symbol-value
8709                                     (intern (format "gnus-newsgroup-%s"
8710                                                     (caar marks)))))))
8711                       ;; Copy the marks to other group.
8712                       (gnus-add-marked-articles
8713                        to-group (cdar marks) (list to-article) info)))
8714                   (setq marks (cdr marks)))
8715
8716                 (gnus-request-set-mark to-group (list (list (list to-article)
8717                                                             'add
8718                                                             to-marks))))
8719
8720               (gnus-dribble-enter
8721                (concat "(gnus-group-set-info '"
8722                        (gnus-prin1-to-string (gnus-get-info to-group))
8723                        ")"))))
8724
8725           ;; Update the Xref header in this article to point to
8726           ;; the new crossposted article we have just created.
8727           (when (eq action 'crosspost)
8728             (save-excursion
8729               (set-buffer copy-buf)
8730               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8731               (nnheader-replace-header "Xref" new-xref)
8732               (gnus-request-replace-article
8733                article gnus-newsgroup-name (current-buffer)))))
8734
8735         ;;;!!!Why is this necessary?
8736         (set-buffer gnus-summary-buffer)
8737
8738         (gnus-summary-goto-subject article)
8739         (when (eq action 'move)
8740           (gnus-summary-mark-article article gnus-canceled-mark))))
8741       (gnus-summary-remove-process-mark article))
8742     ;; Re-activate all groups that have been moved to.
8743     (save-excursion
8744       (set-buffer gnus-group-buffer)
8745       (let ((gnus-group-marked to-groups))
8746         (gnus-group-get-new-news-this-group nil t)))
8747
8748     (gnus-kill-buffer copy-buf)
8749     (gnus-summary-position-point)
8750     (gnus-set-mode-line 'summary)))
8751
8752 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8753   "Move the current article to a different newsgroup.
8754 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8755 When called interactively, if TO-NEWSGROUP is nil, use the value of
8756 the variable `gnus-move-split-methods' for finding a default target
8757 newsgroup.
8758 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8759 re-spool using this method."
8760   (interactive "P")
8761   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8762
8763 (defun gnus-summary-crosspost-article (&optional n)
8764   "Crosspost the current article to some other group."
8765   (interactive "P")
8766   (gnus-summary-move-article n nil nil 'crosspost))
8767
8768 (defcustom gnus-summary-respool-default-method nil
8769   "Default method type for respooling an article.
8770 If nil, use to the current newsgroup method."
8771   :type 'symbol
8772   :group 'gnus-summary-mail)
8773
8774 (defun gnus-summary-respool-article (&optional n method)
8775   "Respool the current article.
8776 The article will be squeezed through the mail spooling process again,
8777 which means that it will be put in some mail newsgroup or other
8778 depending on `nnmail-split-methods'.
8779 If N is a positive number, respool the N next articles.
8780 If N is a negative number, respool the N previous articles.
8781 If N is nil and any articles have been marked with the process mark,
8782 respool those articles instead.
8783
8784 Respooling can be done both from mail groups and \"real\" newsgroups.
8785 In the former case, the articles in question will be moved from the
8786 current group into whatever groups they are destined to.  In the
8787 latter case, they will be copied into the relevant groups."
8788   (interactive
8789    (list current-prefix-arg
8790          (let* ((methods (gnus-methods-using 'respool))
8791                 (methname
8792                  (symbol-name (or gnus-summary-respool-default-method
8793                                   (car (gnus-find-method-for-group
8794                                         gnus-newsgroup-name)))))
8795                 (method
8796                  (gnus-completing-read-with-default
8797                   methname "What backend do you want to use when respooling?"
8798                   methods nil t nil 'gnus-mail-method-history))
8799                 ms)
8800            (cond
8801             ((zerop (length (setq ms (gnus-servers-using-backend
8802                                       (intern method)))))
8803              (list (intern method) ""))
8804             ((= 1 (length ms))
8805              (car ms))
8806             (t
8807              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8808                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8809                            ms-alist))))))))
8810   (unless method
8811     (error "No method given for respooling"))
8812   (if (assoc (symbol-name
8813               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8814              (gnus-methods-using 'respool))
8815       (gnus-summary-move-article n nil method)
8816     (gnus-summary-copy-article n nil method)))
8817
8818 (defun gnus-summary-import-article (file &optional edit)
8819   "Import an arbitrary file into a mail newsgroup."
8820   (interactive "fImport file: \nP")
8821   (let ((group gnus-newsgroup-name)
8822         (now (current-time))
8823         atts lines group-art)
8824     (unless (gnus-check-backend-function 'request-accept-article group)
8825       (error "%s does not support article importing" group))
8826     (or (file-readable-p file)
8827         (not (file-regular-p file))
8828         (error "Can't read %s" file))
8829     (save-excursion
8830       (set-buffer (gnus-get-buffer-create " *import file*"))
8831       (erase-buffer)
8832       (nnheader-insert-file-contents file)
8833       (goto-char (point-min))
8834       (if (nnheader-article-p)
8835           (save-restriction
8836             (goto-char (point-min))
8837             (search-forward "\n\n" nil t)
8838             (narrow-to-region (point-min) (1- (point)))
8839             (goto-char (point-min))
8840             (unless (re-search-forward "^date:" nil t)
8841               (goto-char (point-max))
8842               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8843         ;; This doesn't look like an article, so we fudge some headers.
8844         (setq atts (file-attributes file)
8845               lines (count-lines (point-min) (point-max)))
8846         (insert "From: " (read-string "From: ") "\n"
8847                 "Subject: " (read-string "Subject: ") "\n"
8848                 "Date: " (message-make-date (nth 5 atts)) "\n"
8849                 "Message-ID: " (message-make-message-id) "\n"
8850                 "Lines: " (int-to-string lines) "\n"
8851                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8852       (setq group-art (gnus-request-accept-article group nil t))
8853       (kill-buffer (current-buffer)))
8854     (setq gnus-newsgroup-active (gnus-activate-group group))
8855     (forward-line 1)
8856     (gnus-summary-goto-article (cdr group-art) nil t)
8857     (when edit
8858       (gnus-summary-edit-article))))
8859
8860 (defun gnus-summary-create-article ()
8861   "Create an article in a mail newsgroup."
8862   (interactive)
8863   (let ((group gnus-newsgroup-name)
8864         (now (current-time))
8865         group-art)
8866     (unless (gnus-check-backend-function 'request-accept-article group)
8867       (error "%s does not support article importing" group))
8868     (save-excursion
8869       (set-buffer (gnus-get-buffer-create " *import file*"))
8870       (erase-buffer)
8871       (goto-char (point-min))
8872       ;; This doesn't look like an article, so we fudge some headers.
8873       (insert "From: " (read-string "From: ") "\n"
8874               "Subject: " (read-string "Subject: ") "\n"
8875               "Date: " (message-make-date now) "\n"
8876               "Message-ID: " (message-make-message-id) "\n")
8877       (setq group-art (gnus-request-accept-article group nil t))
8878       (kill-buffer (current-buffer)))
8879     (setq gnus-newsgroup-active (gnus-activate-group group))
8880     (forward-line 1)
8881     (gnus-summary-goto-article (cdr group-art) nil t)
8882     (gnus-summary-edit-article)))
8883
8884 (defun gnus-summary-article-posted-p ()
8885   "Say whether the current (mail) article is available from news as well.
8886 This will be the case if the article has both been mailed and posted."
8887   (interactive)
8888   (let ((id (mail-header-references (gnus-summary-article-header)))
8889         (gnus-override-method (car (gnus-refer-article-methods))))
8890     (if (gnus-request-head id "")
8891         (gnus-message 2 "The current message was found on %s"
8892                       gnus-override-method)
8893       (gnus-message 2 "The current message couldn't be found on %s"
8894                     gnus-override-method)
8895       nil)))
8896
8897 (defun gnus-summary-expire-articles (&optional now)
8898   "Expire all articles that are marked as expirable in the current group."
8899   (interactive)
8900   (when (gnus-check-backend-function
8901          'request-expire-articles gnus-newsgroup-name)
8902     ;; This backend supports expiry.
8903     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8904            (expirable (if total
8905                           (progn
8906                             ;; We need to update the info for
8907                             ;; this group for `gnus-list-of-read-articles'
8908                             ;; to give us the right answer.
8909                             (gnus-run-hooks 'gnus-exit-group-hook)
8910                             (gnus-summary-update-info)
8911                             (gnus-list-of-read-articles gnus-newsgroup-name))
8912                         (setq gnus-newsgroup-expirable
8913                               (sort gnus-newsgroup-expirable '<))))
8914            (expiry-wait (if now 'immediate
8915                           (gnus-group-find-parameter
8916                            gnus-newsgroup-name 'expiry-wait)))
8917            (nnmail-expiry-target
8918             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8919                 nnmail-expiry-target))
8920            es)
8921       (when expirable
8922         ;; There are expirable articles in this group, so we run them
8923         ;; through the expiry process.
8924         (gnus-message 6 "Expiring articles...")
8925         (unless (gnus-check-group gnus-newsgroup-name)
8926           (error "Can't open server for %s" gnus-newsgroup-name))
8927         ;; The list of articles that weren't expired is returned.
8928         (save-excursion
8929           (if expiry-wait
8930               (let ((nnmail-expiry-wait-function nil)
8931                     (nnmail-expiry-wait expiry-wait))
8932                 (setq es (gnus-request-expire-articles
8933                           expirable gnus-newsgroup-name)))
8934             (setq es (gnus-request-expire-articles
8935                       expirable gnus-newsgroup-name)))
8936           (unless total
8937             (setq gnus-newsgroup-expirable es))
8938           ;; We go through the old list of expirable, and mark all
8939           ;; really expired articles as nonexistent.
8940           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8941             (let ((gnus-use-cache nil))
8942               (dolist (article expirable)
8943                 (when (and (not (memq article es))
8944                            (gnus-data-find article))
8945                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8946         (gnus-message 6 "Expiring articles...done")))))
8947
8948 (defun gnus-summary-expire-articles-now ()
8949   "Expunge all expirable articles in the current group.
8950 This means that *all* articles that are marked as expirable will be
8951 deleted forever, right now."
8952   (interactive)
8953   (or gnus-expert-user
8954       (gnus-yes-or-no-p
8955        "Are you really, really, really sure you want to delete all these messages? ")
8956       (error "Phew!"))
8957   (gnus-summary-expire-articles t))
8958
8959 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8960 (defun gnus-summary-delete-article (&optional n)
8961   "Delete the N next (mail) articles.
8962 This command actually deletes articles.  This is not a marking
8963 command.  The article will disappear forever from your life, never to
8964 return.
8965 If N is negative, delete backwards.
8966 If N is nil and articles have been marked with the process mark,
8967 delete these instead."
8968   (interactive "P")
8969   (unless (gnus-check-backend-function 'request-expire-articles
8970                                        gnus-newsgroup-name)
8971     (error "The current newsgroup does not support article deletion"))
8972   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8973     (error "Couldn't open server"))
8974   ;; Compute the list of articles to delete.
8975   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8976         not-deleted)
8977     (if (and gnus-novice-user
8978              (not (gnus-yes-or-no-p
8979                    (format "Do you really want to delete %s forever? "
8980                            (if (> (length articles) 1)
8981                                (format "these %s articles" (length articles))
8982                              "this article")))))
8983         ()
8984       ;; Delete the articles.
8985       (setq not-deleted (gnus-request-expire-articles
8986                          articles gnus-newsgroup-name 'force))
8987       (when (and gnus-agent gnus-agent-cache
8988                  (gnus-sorted-difference articles not-deleted))
8989         (gnus-agent-expire (gnus-sorted-difference articles not-deleted)
8990                            gnus-newsgroup-name 'force))
8991       (while articles
8992         (gnus-summary-remove-process-mark (car articles))
8993         ;; The backend might not have been able to delete the article
8994         ;; after all.
8995         (unless (memq (car articles) not-deleted)
8996           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8997         (setq articles (cdr articles)))
8998       (when not-deleted
8999         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9000     (gnus-summary-position-point)
9001     (gnus-set-mode-line 'summary)
9002     not-deleted))
9003
9004 (defun gnus-summary-edit-article (&optional force)
9005   "Edit the current article.
9006 This will have permanent effect only in mail groups.
9007 If FORCE is non-nil, allow editing of articles even in read-only
9008 groups."
9009   (interactive "P")
9010   (save-excursion
9011     (set-buffer gnus-summary-buffer)
9012     (let ((mail-parse-charset gnus-newsgroup-charset)
9013           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9014       (gnus-set-global-variables)
9015       (when (and (not force)
9016                  (gnus-group-read-only-p))
9017         (error "The current newsgroup does not support article editing"))
9018       (gnus-summary-show-article t)
9019       (gnus-article-edit-article
9020        'ignore
9021        `(lambda (no-highlight)
9022           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9023                 (message-options message-options)
9024                 (message-options-set-recipient)
9025                 (mail-parse-ignored-charsets
9026                  ',gnus-newsgroup-ignored-charsets))
9027             (gnus-summary-edit-article-done
9028              ,(or (mail-header-references gnus-current-headers) "")
9029              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9030
9031 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9032
9033 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9034                                                  no-highlight)
9035   "Make edits to the current article permanent."
9036   (interactive)
9037   (save-excursion
9038     ;; The buffer restriction contains the entire article if it exists.
9039     (when (article-goto-body)
9040       (let ((lines (count-lines (point) (point-max)))
9041             (length (- (point-max) (point)))
9042             (case-fold-search t)
9043             (body (copy-marker (point))))
9044         (goto-char (point-min))
9045         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9046           (delete-region (match-beginning 1) (match-end 1))
9047           (insert (number-to-string length)))
9048         (goto-char (point-min))
9049         (when (re-search-forward
9050                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9051           (delete-region (match-beginning 1) (match-end 1))
9052           (insert (number-to-string length)))
9053         (goto-char (point-min))
9054         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9055           (delete-region (match-beginning 1) (match-end 1))
9056           (insert (number-to-string lines))))))
9057   ;; Replace the article.
9058   (let ((buf (current-buffer)))
9059     (with-temp-buffer
9060       (insert-buffer-substring buf)
9061
9062       (if (and (not read-only)
9063                (not (gnus-request-replace-article
9064                      (cdr gnus-article-current) (car gnus-article-current)
9065                      (current-buffer) t)))
9066           (error "Couldn't replace article")
9067         ;; Update the summary buffer.
9068         (if (and references
9069                  (equal (message-tokenize-header references " ")
9070                         (message-tokenize-header
9071                          (or (message-fetch-field "references") "") " ")))
9072             ;; We only have to update this line.
9073             (save-excursion
9074               (save-restriction
9075                 (message-narrow-to-head)
9076                 (let ((head (buffer-string))
9077                       header)
9078                   (with-temp-buffer
9079                     (insert (format "211 %d Article retrieved.\n"
9080                                     (cdr gnus-article-current)))
9081                     (insert head)
9082                     (insert ".\n")
9083                     (let ((nntp-server-buffer (current-buffer)))
9084                       (setq header (car (gnus-get-newsgroup-headers
9085                                          nil t))))
9086                     (save-excursion
9087                       (set-buffer gnus-summary-buffer)
9088                       (gnus-data-set-header
9089                        (gnus-data-find (cdr gnus-article-current))
9090                        header)
9091                       (gnus-summary-update-article-line
9092                        (cdr gnus-article-current) header)
9093                       (if (gnus-summary-goto-subject
9094                            (cdr gnus-article-current) nil t)
9095                           (gnus-summary-update-secondary-mark
9096                            (cdr gnus-article-current))))))))
9097           ;; Update threads.
9098           (set-buffer (or buffer gnus-summary-buffer))
9099           (gnus-summary-update-article (cdr gnus-article-current))
9100           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9101               (gnus-summary-update-secondary-mark
9102                (cdr gnus-article-current))))
9103         ;; Prettify the article buffer again.
9104         (unless no-highlight
9105           (save-excursion
9106             (set-buffer gnus-article-buffer)
9107             ;;;!!! Fix this -- article should be rehighlighted.
9108             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9109             (set-buffer gnus-original-article-buffer)
9110             (gnus-request-article
9111              (cdr gnus-article-current)
9112              (car gnus-article-current) (current-buffer))))
9113         ;; Prettify the summary buffer line.
9114         (when (gnus-visual-p 'summary-highlight 'highlight)
9115           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9116
9117 (defun gnus-summary-edit-wash (key)
9118   "Perform editing command KEY in the article buffer."
9119   (interactive
9120    (list
9121     (progn
9122       (message "%s" (concat (this-command-keys) "- "))
9123       (read-char))))
9124   (message "")
9125   (gnus-summary-edit-article)
9126   (execute-kbd-macro (concat (this-command-keys) key))
9127   (gnus-article-edit-done))
9128
9129 ;;; Respooling
9130
9131 (defun gnus-summary-respool-query (&optional silent trace)
9132   "Query where the respool algorithm would put this article."
9133   (interactive)
9134   (let (gnus-mark-article-hook)
9135     (gnus-summary-select-article)
9136     (save-excursion
9137       (set-buffer gnus-original-article-buffer)
9138       (save-restriction
9139         (message-narrow-to-head)
9140         (let ((groups (nnmail-article-group 'identity trace)))
9141           (unless silent
9142             (if groups
9143                 (message "This message would go to %s"
9144                          (mapconcat 'car groups ", "))
9145               (message "This message would go to no groups"))
9146             groups))))))
9147
9148 (defun gnus-summary-respool-trace ()
9149   "Trace where the respool algorithm would put this article.
9150 Display a buffer showing all fancy splitting patterns which matched."
9151   (interactive)
9152   (gnus-summary-respool-query nil t))
9153
9154 ;; Summary marking commands.
9155
9156 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9157   "Mark articles which has the same subject as read, and then select the next.
9158 If UNMARK is positive, remove any kind of mark.
9159 If UNMARK is negative, tick articles."
9160   (interactive "P")
9161   (when unmark
9162     (setq unmark (prefix-numeric-value unmark)))
9163   (let ((count
9164          (gnus-summary-mark-same-subject
9165           (gnus-summary-article-subject) unmark)))
9166     ;; Select next unread article.  If auto-select-same mode, should
9167     ;; select the first unread article.
9168     (gnus-summary-next-article t (and gnus-auto-select-same
9169                                       (gnus-summary-article-subject)))
9170     (gnus-message 7 "%d article%s marked as %s"
9171                   count (if (= count 1) " is" "s are")
9172                   (if unmark "unread" "read"))))
9173
9174 (defun gnus-summary-kill-same-subject (&optional unmark)
9175   "Mark articles which has the same subject as read.
9176 If UNMARK is positive, remove any kind of mark.
9177 If UNMARK is negative, tick articles."
9178   (interactive "P")
9179   (when unmark
9180     (setq unmark (prefix-numeric-value unmark)))
9181   (let ((count
9182          (gnus-summary-mark-same-subject
9183           (gnus-summary-article-subject) unmark)))
9184     ;; If marked as read, go to next unread subject.
9185     (when (null unmark)
9186       ;; Go to next unread subject.
9187       (gnus-summary-next-subject 1 t))
9188     (gnus-message 7 "%d articles are marked as %s"
9189                   count (if unmark "unread" "read"))))
9190
9191 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9192   "Mark articles with same SUBJECT as read, and return marked number.
9193 If optional argument UNMARK is positive, remove any kinds of marks.
9194 If optional argument UNMARK is negative, mark articles as unread instead."
9195   (let ((count 1))
9196     (save-excursion
9197       (cond
9198        ((null unmark)                   ; Mark as read.
9199         (while (and
9200                 (progn
9201                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9202                   (gnus-summary-show-thread) t)
9203                 (gnus-summary-find-subject subject))
9204           (setq count (1+ count))))
9205        ((> unmark 0)                    ; Tick.
9206         (while (and
9207                 (progn
9208                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9209                   (gnus-summary-show-thread) t)
9210                 (gnus-summary-find-subject subject))
9211           (setq count (1+ count))))
9212        (t                               ; Mark as unread.
9213         (while (and
9214                 (progn
9215                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9216                   (gnus-summary-show-thread) t)
9217                 (gnus-summary-find-subject subject))
9218           (setq count (1+ count)))))
9219       (gnus-set-mode-line 'summary)
9220       ;; Return the number of marked articles.
9221       count)))
9222
9223 (defun gnus-summary-mark-as-processable (n &optional unmark)
9224   "Set the process mark on the next N articles.
9225 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9226 the process mark instead.  The difference between N and the actual
9227 number of articles marked is returned."
9228   (interactive "P")
9229   (if (and (null n) (gnus-region-active-p))
9230       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9231     (setq n (prefix-numeric-value n))
9232     (let ((backward (< n 0))
9233           (n (abs n)))
9234       (while (and
9235               (> n 0)
9236               (if unmark
9237                   (gnus-summary-remove-process-mark
9238                    (gnus-summary-article-number))
9239                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9240               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9241         (setq n (1- n)))
9242       (when (/= 0 n)
9243         (gnus-message 7 "No more articles"))
9244       (gnus-summary-recenter)
9245       (gnus-summary-position-point)
9246       n)))
9247
9248 (defun gnus-summary-unmark-as-processable (n)
9249   "Remove the process mark from the next N articles.
9250 If N is negative, unmark backward instead.  The difference between N and
9251 the actual number of articles unmarked is returned."
9252   (interactive "P")
9253   (gnus-summary-mark-as-processable n t))
9254
9255 (defun gnus-summary-unmark-all-processable ()
9256   "Remove the process mark from all articles."
9257   (interactive)
9258   (save-excursion
9259     (while gnus-newsgroup-processable
9260       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9261   (gnus-summary-position-point))
9262
9263 (defun gnus-summary-add-mark (article type)
9264   "Mark ARTICLE with a mark of TYPE."
9265   (let ((vtype (car (assq type gnus-article-mark-lists)))
9266         var)
9267     (if (not vtype)
9268         (error "No such mark type: %s" type)
9269       (setq var (intern (format "gnus-newsgroup-%s" type)))
9270       (set var (cons article (symbol-value var)))
9271       (if (memq type '(processable cached replied forwarded recent saved))
9272           (gnus-summary-update-secondary-mark article)
9273         ;;; !!! This is bogus.  We should find out what primary
9274         ;;; !!! mark we want to set.
9275         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9276
9277 (defun gnus-summary-mark-as-expirable (n)
9278   "Mark N articles forward as expirable.
9279 If N is negative, mark backward instead.  The difference between N and
9280 the actual number of articles marked is returned."
9281   (interactive "p")
9282   (gnus-summary-mark-forward n gnus-expirable-mark))
9283
9284 (defun gnus-summary-mark-as-spam (n)
9285   "Mark N articles forward as spam.
9286 If N is negative, mark backward instead.  The difference between N and
9287 the actual number of articles marked is returned."
9288   (interactive "p")
9289   (gnus-summary-mark-forward n gnus-spam-mark))
9290
9291 (defun gnus-summary-mark-article-as-replied (article)
9292   "Mark ARTICLE as replied to and update the summary line.
9293 ARTICLE can also be a list of articles."
9294   (interactive (list (gnus-summary-article-number)))
9295   (let ((articles (if (listp article) article (list article))))
9296     (dolist (article articles)
9297       (push article gnus-newsgroup-replied)
9298       (let ((buffer-read-only nil))
9299         (when (gnus-summary-goto-subject article nil t)
9300           (gnus-summary-update-secondary-mark article))))))
9301
9302 (defun gnus-summary-mark-article-as-forwarded (article)
9303   "Mark ARTICLE as forwarded and update the summary line.
9304 ARTICLE can also be a list of articles."
9305   (let ((articles (if (listp article) article (list article))))
9306     (dolist (article articles)
9307       (push article gnus-newsgroup-forwarded)
9308       (let ((buffer-read-only nil))
9309         (when (gnus-summary-goto-subject article nil t)
9310           (gnus-summary-update-secondary-mark article))))))
9311
9312 (defun gnus-summary-set-bookmark (article)
9313   "Set a bookmark in current article."
9314   (interactive (list (gnus-summary-article-number)))
9315   (when (or (not (get-buffer gnus-article-buffer))
9316             (not gnus-current-article)
9317             (not gnus-article-current)
9318             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9319     (error "No current article selected"))
9320   ;; Remove old bookmark, if one exists.
9321   (let ((old (assq article gnus-newsgroup-bookmarks)))
9322     (when old
9323       (setq gnus-newsgroup-bookmarks
9324             (delq old gnus-newsgroup-bookmarks))))
9325   ;; Set the new bookmark, which is on the form
9326   ;; (article-number . line-number-in-body).
9327   (push
9328    (cons article
9329          (save-excursion
9330            (set-buffer gnus-article-buffer)
9331            (count-lines
9332             (min (point)
9333                  (save-excursion
9334                    (goto-char (point-min))
9335                    (search-forward "\n\n" nil t)
9336                    (point)))
9337             (point))))
9338    gnus-newsgroup-bookmarks)
9339   (gnus-message 6 "A bookmark has been added to the current article."))
9340
9341 (defun gnus-summary-remove-bookmark (article)
9342   "Remove the bookmark from the current article."
9343   (interactive (list (gnus-summary-article-number)))
9344   ;; Remove old bookmark, if one exists.
9345   (let ((old (assq article gnus-newsgroup-bookmarks)))
9346     (if old
9347         (progn
9348           (setq gnus-newsgroup-bookmarks
9349                 (delq old gnus-newsgroup-bookmarks))
9350           (gnus-message 6 "Removed bookmark."))
9351       (gnus-message 6 "No bookmark in current article."))))
9352
9353 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9354 (defun gnus-summary-mark-as-dormant (n)
9355   "Mark N articles forward as dormant.
9356 If N is negative, mark backward instead.  The difference between N and
9357 the actual number of articles marked is returned."
9358   (interactive "p")
9359   (gnus-summary-mark-forward n gnus-dormant-mark))
9360
9361 (defun gnus-summary-set-process-mark (article)
9362   "Set the process mark on ARTICLE and update the summary line."
9363   (setq gnus-newsgroup-processable
9364         (cons article
9365               (delq article gnus-newsgroup-processable)))
9366   (when (gnus-summary-goto-subject article)
9367     (gnus-summary-show-thread)
9368     (gnus-summary-goto-subject article)
9369     (gnus-summary-update-secondary-mark article)))
9370
9371 (defun gnus-summary-remove-process-mark (article)
9372   "Remove the process mark from ARTICLE and update the summary line."
9373   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9374   (when (gnus-summary-goto-subject article)
9375     (gnus-summary-show-thread)
9376     (gnus-summary-goto-subject article)
9377     (gnus-summary-update-secondary-mark article)))
9378
9379 (defun gnus-summary-set-saved-mark (article)
9380   "Set the process mark on ARTICLE and update the summary line."
9381   (push article gnus-newsgroup-saved)
9382   (when (gnus-summary-goto-subject article)
9383     (gnus-summary-update-secondary-mark article)))
9384
9385 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9386   "Mark N articles as read forwards.
9387 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9388 The difference between N and the actual number of articles marked is
9389 returned.
9390 Iff NO-EXPIRE, auto-expiry will be inhibited."
9391   (interactive "p")
9392   (gnus-summary-show-thread)
9393   (let ((backward (< n 0))
9394         (gnus-summary-goto-unread
9395          (and gnus-summary-goto-unread
9396               (not (eq gnus-summary-goto-unread 'never))
9397               (not (memq mark (list gnus-unread-mark
9398                                     gnus-ticked-mark gnus-dormant-mark)))))
9399         (n (abs n))
9400         (mark (or mark gnus-del-mark)))
9401     (while (and (> n 0)
9402                 (gnus-summary-mark-article nil mark no-expire)
9403                 (zerop (gnus-summary-next-subject
9404                         (if backward -1 1)
9405                         (and gnus-summary-goto-unread
9406                              (not (eq gnus-summary-goto-unread 'never)))
9407                         t)))
9408       (setq n (1- n)))
9409     (when (/= 0 n)
9410       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9411     (gnus-summary-recenter)
9412     (gnus-summary-position-point)
9413     (gnus-set-mode-line 'summary)
9414     n))
9415
9416 (defun gnus-summary-mark-article-as-read (mark)
9417   "Mark the current article quickly as read with MARK."
9418   (let ((article (gnus-summary-article-number)))
9419     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9420     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9421     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9422     (push (cons article mark) gnus-newsgroup-reads)
9423     ;; Possibly remove from cache, if that is used.
9424     (when gnus-use-cache
9425       (gnus-cache-enter-remove-article article))
9426     ;; Allow the backend to change the mark.
9427     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9428     ;; Check for auto-expiry.
9429     (when (and gnus-newsgroup-auto-expire
9430                (memq mark gnus-auto-expirable-marks))
9431       (setq mark gnus-expirable-mark)
9432       ;; Let the backend know about the mark change.
9433       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9434       (push article gnus-newsgroup-expirable))
9435     ;; Set the mark in the buffer.
9436     (gnus-summary-update-mark mark 'unread)
9437     t))
9438
9439 (defun gnus-summary-mark-article-as-unread (mark)
9440   "Mark the current article quickly as unread with MARK."
9441   (let* ((article (gnus-summary-article-number))
9442          (old-mark (gnus-summary-article-mark article)))
9443     ;; Allow the backend to change the mark.
9444     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9445     (if (eq mark old-mark)
9446         t
9447       (if (<= article 0)
9448           (progn
9449             (gnus-error 1 "Can't mark negative article numbers")
9450             nil)
9451         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9452         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9453         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9454         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9455         (cond ((= mark gnus-ticked-mark)
9456                (setq gnus-newsgroup-marked
9457                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9458                                               article)))
9459               ((= mark gnus-dormant-mark)
9460                (setq gnus-newsgroup-dormant
9461                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9462                                               article)))
9463               (t
9464                (setq gnus-newsgroup-unreads
9465                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9466                                               article))))
9467         (gnus-pull article gnus-newsgroup-reads)
9468
9469         ;; See whether the article is to be put in the cache.
9470         (and gnus-use-cache
9471              (vectorp (gnus-summary-article-header article))
9472              (save-excursion
9473                (gnus-cache-possibly-enter-article
9474                 gnus-newsgroup-name article
9475                 (gnus-summary-article-header article)
9476                 (= mark gnus-ticked-mark)
9477                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9478
9479         ;; Fix the mark.
9480         (gnus-summary-update-mark mark 'unread)
9481         t))))
9482
9483 (defun gnus-summary-mark-article (&optional article mark no-expire)
9484   "Mark ARTICLE with MARK.  MARK can be any character.
9485 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9486 `??' (dormant) and `?E' (expirable).
9487 If MARK is nil, then the default character `?r' is used.
9488 If ARTICLE is nil, then the article on the current line will be
9489 marked.
9490 Iff NO-EXPIRE, auto-expiry will be inhibited."
9491   ;; The mark might be a string.
9492   (when (stringp mark)
9493     (setq mark (aref mark 0)))
9494   ;; If no mark is given, then we check auto-expiring.
9495   (when (null mark)
9496     (setq mark gnus-del-mark))
9497   (when (and (not no-expire)
9498              gnus-newsgroup-auto-expire
9499              (memq mark gnus-auto-expirable-marks))
9500     (setq mark gnus-expirable-mark))
9501   (let ((article (or article (gnus-summary-article-number)))
9502         (old-mark (gnus-summary-article-mark article)))
9503     ;; Allow the backend to change the mark.
9504     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9505     (if (eq mark old-mark)
9506         t
9507       (unless article
9508         (error "No article on current line"))
9509       (if (not (if (or (= mark gnus-unread-mark)
9510                        (= mark gnus-ticked-mark)
9511                        (= mark gnus-dormant-mark))
9512                    (gnus-mark-article-as-unread article mark)
9513                  (gnus-mark-article-as-read article mark)))
9514           t
9515         ;; See whether the article is to be put in the cache.
9516         (and gnus-use-cache
9517              (not (= mark gnus-canceled-mark))
9518              (vectorp (gnus-summary-article-header article))
9519              (save-excursion
9520                (gnus-cache-possibly-enter-article
9521                 gnus-newsgroup-name article
9522                 (gnus-summary-article-header article)
9523                 (= mark gnus-ticked-mark)
9524                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9525
9526         (when (gnus-summary-goto-subject article nil t)
9527           (let ((buffer-read-only nil))
9528             (gnus-summary-show-thread)
9529             ;; Fix the mark.
9530             (gnus-summary-update-mark mark 'unread)
9531             t))))))
9532
9533 (defun gnus-summary-update-secondary-mark (article)
9534   "Update the secondary (read, process, cache) mark."
9535   (gnus-summary-update-mark
9536    (cond ((memq article gnus-newsgroup-processable)
9537           gnus-process-mark)
9538          ((memq article gnus-newsgroup-cached)
9539           gnus-cached-mark)
9540          ((memq article gnus-newsgroup-replied)
9541           gnus-replied-mark)
9542          ((memq article gnus-newsgroup-forwarded)
9543           gnus-forwarded-mark)
9544          ((memq article gnus-newsgroup-saved)
9545           gnus-saved-mark)
9546          ((memq article gnus-newsgroup-recent)
9547           gnus-recent-mark)
9548          ((memq article gnus-newsgroup-unseen)
9549           gnus-unseen-mark)
9550          (t gnus-no-mark))
9551    'replied)
9552   (when (gnus-visual-p 'summary-highlight 'highlight)
9553     (gnus-run-hooks 'gnus-summary-update-hook))
9554   t)
9555
9556 (defun gnus-summary-update-mark (mark type)
9557   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9558         (buffer-read-only nil))
9559     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9560     (when forward
9561       (when (looking-at "\r")
9562         (incf forward))
9563       (when (<= (+ forward (point)) (point-max))
9564         ;; Go to the right position on the line.
9565         (goto-char (+ forward (point)))
9566         ;; Replace the old mark with the new mark.
9567         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9568         ;; Optionally update the marks by some user rule.
9569         (when (eq type 'unread)
9570           (gnus-data-set-mark
9571            (gnus-data-find (gnus-summary-article-number)) mark)
9572           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9573
9574 (defun gnus-mark-article-as-read (article &optional mark)
9575   "Enter ARTICLE in the pertinent lists and remove it from others."
9576   ;; Make the article expirable.
9577   (let ((mark (or mark gnus-del-mark)))
9578     (setq gnus-newsgroup-expirable
9579           (if (= mark gnus-expirable-mark)
9580               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9581             (delq article gnus-newsgroup-expirable)))
9582     ;; Remove from unread and marked lists.
9583     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9584     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9585     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9586     (push (cons article mark) gnus-newsgroup-reads)
9587     ;; Possibly remove from cache, if that is used.
9588     (when gnus-use-cache
9589       (gnus-cache-enter-remove-article article))
9590     t))
9591
9592 (defun gnus-mark-article-as-unread (article &optional mark)
9593   "Enter ARTICLE in the pertinent lists and remove it from others."
9594   (let ((mark (or mark gnus-ticked-mark)))
9595     (if (<= article 0)
9596         (progn
9597           (gnus-error 1 "Can't mark negative article numbers")
9598           nil)
9599       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9600             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9601             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9602             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9603
9604       ;; Unsuppress duplicates?
9605       (when gnus-suppress-duplicates
9606         (gnus-dup-unsuppress-article article))
9607
9608       (cond ((= mark gnus-ticked-mark)
9609              (setq gnus-newsgroup-marked
9610                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9611             ((= mark gnus-dormant-mark)
9612              (setq gnus-newsgroup-dormant
9613                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9614             (t
9615              (setq gnus-newsgroup-unreads
9616                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9617       (gnus-pull article gnus-newsgroup-reads)
9618       t)))
9619
9620 (defalias 'gnus-summary-mark-as-unread-forward
9621   'gnus-summary-tick-article-forward)
9622 (make-obsolete 'gnus-summary-mark-as-unread-forward
9623                'gnus-summary-tick-article-forward)
9624 (defun gnus-summary-tick-article-forward (n)
9625   "Tick N articles forwards.
9626 If N is negative, tick backwards instead.
9627 The difference between N and the number of articles ticked is returned."
9628   (interactive "p")
9629   (gnus-summary-mark-forward n gnus-ticked-mark))
9630
9631 (defalias 'gnus-summary-mark-as-unread-backward
9632   'gnus-summary-tick-article-backward)
9633 (make-obsolete 'gnus-summary-mark-as-unread-backward
9634                'gnus-summary-tick-article-backward)
9635 (defun gnus-summary-tick-article-backward (n)
9636   "Tick N articles backwards.
9637 The difference between N and the number of articles ticked is returned."
9638   (interactive "p")
9639   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9640
9641 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9642 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9643 (defun gnus-summary-tick-article (&optional article clear-mark)
9644   "Mark current article as unread.
9645 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9646 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9647   (interactive)
9648   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9649                                        gnus-ticked-mark)))
9650
9651 (defun gnus-summary-mark-as-read-forward (n)
9652   "Mark N articles as read forwards.
9653 If N is negative, mark backwards instead.
9654 The difference between N and the actual number of articles marked is
9655 returned."
9656   (interactive "p")
9657   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9658
9659 (defun gnus-summary-mark-as-read-backward (n)
9660   "Mark the N articles as read backwards.
9661 The difference between N and the actual number of articles marked is
9662 returned."
9663   (interactive "p")
9664   (gnus-summary-mark-forward
9665    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9666
9667 (defun gnus-summary-mark-as-read (&optional article mark)
9668   "Mark current article as read.
9669 ARTICLE specifies the article to be marked as read.
9670 MARK specifies a string to be inserted at the beginning of the line."
9671   (gnus-summary-mark-article article mark))
9672
9673 (defun gnus-summary-clear-mark-forward (n)
9674   "Clear marks from N articles forward.
9675 If N is negative, clear backward instead.
9676 The difference between N and the number of marks cleared is returned."
9677   (interactive "p")
9678   (gnus-summary-mark-forward n gnus-unread-mark))
9679
9680 (defun gnus-summary-clear-mark-backward (n)
9681   "Clear marks from N articles backward.
9682 The difference between N and the number of marks cleared is returned."
9683   (interactive "p")
9684   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9685
9686 (defun gnus-summary-mark-unread-as-read ()
9687   "Intended to be used by `gnus-summary-mark-article-hook'."
9688   (when (memq gnus-current-article gnus-newsgroup-unreads)
9689     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9690
9691 (defun gnus-summary-mark-read-and-unread-as-read ()
9692   "Intended to be used by `gnus-summary-mark-article-hook'."
9693   (let ((mark (gnus-summary-article-mark)))
9694     (when (or (gnus-unread-mark-p mark)
9695               (gnus-read-mark-p mark))
9696       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9697
9698 (defun gnus-summary-mark-unread-as-ticked ()
9699   "Intended to be used by `gnus-summary-mark-article-hook'."
9700   (when (memq gnus-current-article gnus-newsgroup-unreads)
9701     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9702
9703 (defun gnus-summary-mark-region-as-read (point mark all)
9704   "Mark all unread articles between point and mark as read.
9705 If given a prefix, mark all articles between point and mark as read,
9706 even ticked and dormant ones."
9707   (interactive "r\nP")
9708   (save-excursion
9709     (let (article)
9710       (goto-char point)
9711       (beginning-of-line)
9712       (while (and
9713               (< (point) mark)
9714               (progn
9715                 (when (or all
9716                           (memq (setq article (gnus-summary-article-number))
9717                                 gnus-newsgroup-unreads))
9718                   (gnus-summary-mark-article article gnus-del-mark))
9719                 t)
9720               (gnus-summary-find-next))))))
9721
9722 (defun gnus-summary-mark-below (score mark)
9723   "Mark articles with score less than SCORE with MARK."
9724   (interactive "P\ncMark: ")
9725   (setq score (if score
9726                   (prefix-numeric-value score)
9727                 (or gnus-summary-default-score 0)))
9728   (save-excursion
9729     (set-buffer gnus-summary-buffer)
9730     (goto-char (point-min))
9731     (while
9732         (progn
9733           (and (< (gnus-summary-article-score) score)
9734                (gnus-summary-mark-article nil mark))
9735           (gnus-summary-find-next)))))
9736
9737 (defun gnus-summary-kill-below (&optional score)
9738   "Mark articles with score below SCORE as read."
9739   (interactive "P")
9740   (gnus-summary-mark-below score gnus-killed-mark))
9741
9742 (defun gnus-summary-clear-above (&optional score)
9743   "Clear all marks from articles with score above SCORE."
9744   (interactive "P")
9745   (gnus-summary-mark-above score gnus-unread-mark))
9746
9747 (defun gnus-summary-tick-above (&optional score)
9748   "Tick all articles with score above SCORE."
9749   (interactive "P")
9750   (gnus-summary-mark-above score gnus-ticked-mark))
9751
9752 (defun gnus-summary-mark-above (score mark)
9753   "Mark articles with score over SCORE with MARK."
9754   (interactive "P\ncMark: ")
9755   (setq score (if score
9756                   (prefix-numeric-value score)
9757                 (or gnus-summary-default-score 0)))
9758   (save-excursion
9759     (set-buffer gnus-summary-buffer)
9760     (goto-char (point-min))
9761     (while (and (progn
9762                   (when (> (gnus-summary-article-score) score)
9763                     (gnus-summary-mark-article nil mark))
9764                   t)
9765                 (gnus-summary-find-next)))))
9766
9767 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9768 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9769 (defun gnus-summary-limit-include-expunged (&optional no-error)
9770   "Display all the hidden articles that were expunged for low scores."
9771   (interactive)
9772   (let ((buffer-read-only nil))
9773     (let ((scored gnus-newsgroup-scored)
9774           headers h)
9775       (while scored
9776         (unless (gnus-summary-article-header (caar scored))
9777           (and (setq h (gnus-number-to-header (caar scored)))
9778                (< (cdar scored) gnus-summary-expunge-below)
9779                (push h headers)))
9780         (setq scored (cdr scored)))
9781       (if (not headers)
9782           (when (not no-error)
9783             (error "No expunged articles hidden"))
9784         (goto-char (point-min))
9785         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9786         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9787         (mapcar (lambda (x) (push (mail-header-number x)
9788                                   gnus-newsgroup-limit))
9789                 headers)
9790         (gnus-summary-prepare-unthreaded (nreverse headers))
9791         (goto-char (point-min))
9792         (gnus-summary-position-point)
9793         t))))
9794
9795 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9796   "Mark all unread articles in this newsgroup as read.
9797 If prefix argument ALL is non-nil, ticked and dormant articles will
9798 also be marked as read.
9799 If QUIETLY is non-nil, no questions will be asked.
9800 If TO-HERE is non-nil, it should be a point in the buffer.  All
9801 articles before (after, if REVERSE is set) this point will be marked as read.
9802 Note that this function will only catch up the unread article
9803 in the current summary buffer limitation.
9804 The number of articles marked as read is returned."
9805   (interactive "P")
9806   (prog1
9807       (save-excursion
9808         (when (or quietly
9809                   (not gnus-interactive-catchup) ;Without confirmation?
9810                   gnus-expert-user
9811                   (gnus-y-or-n-p
9812                    (if all
9813                        "Mark absolutely all articles as read? "
9814                      "Mark all unread articles as read? ")))
9815           (if (and not-mark
9816                    (not gnus-newsgroup-adaptive)
9817                    (not gnus-newsgroup-auto-expire)
9818                    (not gnus-suppress-duplicates)
9819                    (or (not gnus-use-cache)
9820                        (eq gnus-use-cache 'passive)))
9821               (progn
9822                 (when all
9823                   (setq gnus-newsgroup-marked nil
9824                         gnus-newsgroup-dormant nil))
9825                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9826             ;; We actually mark all articles as canceled, which we
9827             ;; have to do when using auto-expiry or adaptive scoring.
9828             (gnus-summary-show-all-threads)
9829             (if (and to-here reverse)
9830                 (progn
9831                   (goto-char to-here)
9832                   (while (and
9833                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9834                           (gnus-summary-find-next (not all) nil nil t))))
9835               (when (gnus-summary-first-subject (not all) t)
9836                 (while (and
9837                         (if to-here (< (point) to-here) t)
9838                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9839                         (gnus-summary-find-next (not all) nil nil t)))))
9840             (gnus-set-mode-line 'summary))
9841           t))
9842     (gnus-summary-position-point)))
9843
9844 (defun gnus-summary-catchup-to-here (&optional all)
9845   "Mark all unticked articles before the current one as read.
9846 If ALL is non-nil, also mark ticked and dormant articles as read."
9847   (interactive "P")
9848   (save-excursion
9849     (gnus-save-hidden-threads
9850       (let ((beg (point)))
9851         ;; We check that there are unread articles.
9852         (when (or all (gnus-summary-find-prev))
9853           (gnus-summary-catchup all t beg)))))
9854   (gnus-summary-position-point))
9855
9856 (defun gnus-summary-catchup-from-here (&optional all)
9857   "Mark all unticked articles after the current one as read.
9858 If ALL is non-nil, also mark ticked and dormant articles as read."
9859   (interactive "P")
9860   (save-excursion
9861     (gnus-save-hidden-threads
9862       (let ((beg (point)))
9863         ;; We check that there are unread articles.
9864         (when (or all (gnus-summary-find-next))
9865           (gnus-summary-catchup all t beg nil t)))))
9866   (gnus-summary-position-point))
9867
9868 (defun gnus-summary-catchup-all (&optional quietly)
9869   "Mark all articles in this newsgroup as read."
9870   (interactive "P")
9871   (gnus-summary-catchup t quietly))
9872
9873 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9874   "Mark all unread articles in this group as read, then exit.
9875 If prefix argument ALL is non-nil, all articles are marked as read.
9876 If QUIETLY is non-nil, no questions will be asked."
9877   (interactive "P")
9878   (when (gnus-summary-catchup all quietly nil 'fast)
9879     ;; Select next newsgroup or exit.
9880     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9881              (eq gnus-auto-select-next 'quietly))
9882         (gnus-summary-next-group nil)
9883       (gnus-summary-exit))))
9884
9885 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9886   "Mark all articles in this newsgroup as read, and then exit."
9887   (interactive "P")
9888   (gnus-summary-catchup-and-exit t quietly))
9889
9890 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9891   "Mark all articles in this group as read and select the next group.
9892 If given a prefix, mark all articles, unread as well as ticked, as
9893 read."
9894   (interactive "P")
9895   (save-excursion
9896     (gnus-summary-catchup all))
9897   (gnus-summary-next-group))
9898
9899 ;;;
9900 ;;; with article
9901 ;;;
9902
9903 (defmacro gnus-with-article (article &rest forms)
9904   "Select ARTICLE and perform FORMS in the original article buffer.
9905 Then replace the article with the result."
9906   `(progn
9907      ;; We don't want the article to be marked as read.
9908      (let (gnus-mark-article-hook)
9909        (gnus-summary-select-article t t nil ,article))
9910      (set-buffer gnus-original-article-buffer)
9911      ,@forms
9912      (if (not (gnus-check-backend-function
9913                'request-replace-article (car gnus-article-current)))
9914          (gnus-message 5 "Read-only group; not replacing")
9915        (unless (gnus-request-replace-article
9916                 ,article (car gnus-article-current)
9917                 (current-buffer) t)
9918          (error "Couldn't replace article")))
9919      ;; The cache and backlog have to be flushed somewhat.
9920      (when gnus-keep-backlog
9921        (gnus-backlog-remove-article
9922         (car gnus-article-current) (cdr gnus-article-current)))
9923      (when gnus-use-cache
9924        (gnus-cache-update-article
9925         (car gnus-article-current) (cdr gnus-article-current)))))
9926
9927 (put 'gnus-with-article 'lisp-indent-function 1)
9928 (put 'gnus-with-article 'edebug-form-spec '(form body))
9929
9930 ;; Thread-based commands.
9931
9932 (defun gnus-summary-articles-in-thread (&optional article)
9933   "Return a list of all articles in the current thread.
9934 If ARTICLE is non-nil, return all articles in the thread that starts
9935 with that article."
9936   (let* ((article (or article (gnus-summary-article-number)))
9937          (data (gnus-data-find-list article))
9938          (top-level (gnus-data-level (car data)))
9939          (top-subject
9940           (cond ((null gnus-thread-operation-ignore-subject)
9941                  (gnus-simplify-subject-re
9942                   (mail-header-subject (gnus-data-header (car data)))))
9943                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9944                  (gnus-simplify-subject-fuzzy
9945                   (mail-header-subject (gnus-data-header (car data)))))
9946                 (t nil)))
9947          (end-point (save-excursion
9948                       (if (gnus-summary-go-to-next-thread)
9949                           (point) (point-max))))
9950          articles)
9951     (while (and data
9952                 (< (gnus-data-pos (car data)) end-point))
9953       (when (or (not top-subject)
9954                 (string= top-subject
9955                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9956                              (gnus-simplify-subject-fuzzy
9957                               (mail-header-subject
9958                                (gnus-data-header (car data))))
9959                            (gnus-simplify-subject-re
9960                             (mail-header-subject
9961                              (gnus-data-header (car data)))))))
9962         (push (gnus-data-number (car data)) articles))
9963       (unless (and (setq data (cdr data))
9964                    (> (gnus-data-level (car data)) top-level))
9965         (setq data nil)))
9966     ;; Return the list of articles.
9967     (nreverse articles)))
9968
9969 (defun gnus-summary-rethread-current ()
9970   "Rethread the thread the current article is part of."
9971   (interactive)
9972   (let* ((gnus-show-threads t)
9973          (article (gnus-summary-article-number))
9974          (id (mail-header-id (gnus-summary-article-header)))
9975          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9976     (unless id
9977       (error "No article on the current line"))
9978     (gnus-rebuild-thread id)
9979     (gnus-summary-goto-subject article)))
9980
9981 (defun gnus-summary-reparent-thread ()
9982   "Make the current article child of the marked (or previous) article.
9983
9984 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9985 is non-nil or the Subject: of both articles are the same."
9986   (interactive)
9987   (unless (not (gnus-group-read-only-p))
9988     (error "The current newsgroup does not support article editing"))
9989   (unless (<= (length gnus-newsgroup-processable) 1)
9990     (error "No more than one article may be marked"))
9991   (save-window-excursion
9992     (let ((gnus-article-buffer " *reparent*")
9993           (current-article (gnus-summary-article-number))
9994           ;; First grab the marked article, otherwise one line up.
9995           (parent-article (if (not (null gnus-newsgroup-processable))
9996                               (car gnus-newsgroup-processable)
9997                             (save-excursion
9998                               (if (eq (forward-line -1) 0)
9999                                   (gnus-summary-article-number)
10000                                 (error "Beginning of summary buffer"))))))
10001       (unless (not (eq current-article parent-article))
10002         (error "An article may not be self-referential"))
10003       (let ((message-id (mail-header-id
10004                          (gnus-summary-article-header parent-article))))
10005         (unless (and message-id (not (equal message-id "")))
10006           (error "No message-id in desired parent"))
10007         (gnus-with-article current-article
10008           (save-restriction
10009             (goto-char (point-min))
10010             (message-narrow-to-head)
10011             (if (re-search-forward "^References: " nil t)
10012                 (progn
10013                   (re-search-forward "^[^ \t]" nil t)
10014                   (forward-line -1)
10015                   (end-of-line)
10016                   (insert " " message-id))
10017               (insert "References: " message-id "\n"))))
10018         (set-buffer gnus-summary-buffer)
10019         (gnus-summary-unmark-all-processable)
10020         (gnus-summary-update-article current-article)
10021         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10022             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10023         (gnus-summary-rethread-current)
10024         (gnus-message 3 "Article %d is now the child of article %d"
10025                       current-article parent-article)))))
10026
10027 (defun gnus-summary-toggle-threads (&optional arg)
10028   "Toggle showing conversation threads.
10029 If ARG is positive number, turn showing conversation threads on."
10030   (interactive "P")
10031   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10032     (setq gnus-show-threads
10033           (if (null arg) (not gnus-show-threads)
10034             (> (prefix-numeric-value arg) 0)))
10035     (gnus-summary-prepare)
10036     (gnus-summary-goto-subject current)
10037     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10038     (gnus-summary-position-point)))
10039
10040 (defun gnus-summary-show-all-threads ()
10041   "Show all threads."
10042   (interactive)
10043   (save-excursion
10044     (let ((buffer-read-only nil))
10045       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10046   (gnus-summary-position-point))
10047
10048 (defun gnus-summary-show-thread ()
10049   "Show thread subtrees.
10050 Returns nil if no thread was there to be shown."
10051   (interactive)
10052   (let ((buffer-read-only nil)
10053         (orig (point))
10054         ;; first goto end then to beg, to have point at beg after let
10055         (end (progn (end-of-line) (point)))
10056         (beg (progn (beginning-of-line) (point))))
10057     (prog1
10058         ;; Any hidden lines here?
10059         (search-forward "\r" end t)
10060       (subst-char-in-region beg end ?\^M ?\n t)
10061       (goto-char orig)
10062       (gnus-summary-position-point))))
10063
10064 (defun gnus-summary-maybe-hide-threads ()
10065   "If requested, hide the threads that should be hidden."
10066   (when (and gnus-show-threads
10067              gnus-thread-hide-subtree)
10068     (gnus-summary-hide-all-threads
10069      (if (or (consp gnus-thread-hide-subtree)
10070              (gnus-functionp gnus-thread-hide-subtree))
10071          (gnus-make-predicate gnus-thread-hide-subtree)
10072        nil))))
10073
10074 ;;; Hiding predicates.
10075
10076 (defun gnus-article-unread-p (header)
10077   (memq (mail-header-number header) gnus-newsgroup-unreads))
10078
10079 (defun gnus-article-unseen-p (header)
10080   (memq (mail-header-number header) gnus-newsgroup-unseen))
10081
10082 (defun gnus-map-articles (predicate articles)
10083   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10084   (apply 'gnus-or (mapcar predicate
10085                           (mapcar 'gnus-summary-article-header articles))))
10086
10087 (defun gnus-summary-hide-all-threads (&optional predicate)
10088   "Hide all thread subtrees.
10089 If PREDICATE is supplied, threads that satisfy this predicate
10090 will not be hidden."
10091   (interactive)
10092   (save-excursion
10093     (goto-char (point-min))
10094     (let ((end nil))
10095       (while (not end)
10096         (when (or (not predicate)
10097                   (gnus-map-articles
10098                    predicate (gnus-summary-article-children)))
10099             (gnus-summary-hide-thread))
10100         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10101   (gnus-summary-position-point))
10102
10103 (defun gnus-summary-hide-thread ()
10104   "Hide thread subtrees.
10105 If PREDICATE is supplied, threads that satisfy this predicate
10106 will not be hidden.
10107 Returns nil if no threads were there to be hidden."
10108   (interactive)
10109   (let ((buffer-read-only nil)
10110         (start (point))
10111         (article (gnus-summary-article-number)))
10112     (goto-char start)
10113     ;; Go forward until either the buffer ends or the subthread
10114     ;; ends.
10115     (when (and (not (eobp))
10116                (or (zerop (gnus-summary-next-thread 1 t))
10117                    (goto-char (point-max))))
10118       (prog1
10119           (if (and (> (point) start)
10120                    (search-backward "\n" start t))
10121               (progn
10122                 (subst-char-in-region start (point) ?\n ?\^M)
10123                 (gnus-summary-goto-subject article))
10124             (goto-char start)
10125             nil)))))
10126
10127 (defun gnus-summary-go-to-next-thread (&optional previous)
10128   "Go to the same level (or less) next thread.
10129 If PREVIOUS is non-nil, go to previous thread instead.
10130 Return the article number moved to, or nil if moving was impossible."
10131   (let ((level (gnus-summary-thread-level))
10132         (way (if previous -1 1))
10133         (beg (point)))
10134     (forward-line way)
10135     (while (and (not (eobp))
10136                 (< level (gnus-summary-thread-level)))
10137       (forward-line way))
10138     (if (eobp)
10139         (progn
10140           (goto-char beg)
10141           nil)
10142       (setq beg (point))
10143       (prog1
10144           (gnus-summary-article-number)
10145         (goto-char beg)))))
10146
10147 (defun gnus-summary-next-thread (n &optional silent)
10148   "Go to the same level next N'th thread.
10149 If N is negative, search backward instead.
10150 Returns the difference between N and the number of skips actually
10151 done.
10152
10153 If SILENT, don't output messages."
10154   (interactive "p")
10155   (let ((backward (< n 0))
10156         (n (abs n)))
10157     (while (and (> n 0)
10158                 (gnus-summary-go-to-next-thread backward))
10159       (decf n))
10160     (unless silent
10161       (gnus-summary-position-point))
10162     (when (and (not silent) (/= 0 n))
10163       (gnus-message 7 "No more threads"))
10164     n))
10165
10166 (defun gnus-summary-prev-thread (n)
10167   "Go to the same level previous N'th thread.
10168 Returns the difference between N and the number of skips actually
10169 done."
10170   (interactive "p")
10171   (gnus-summary-next-thread (- n)))
10172
10173 (defun gnus-summary-go-down-thread ()
10174   "Go down one level in the current thread."
10175   (let ((children (gnus-summary-article-children)))
10176     (when children
10177       (gnus-summary-goto-subject (car children)))))
10178
10179 (defun gnus-summary-go-up-thread ()
10180   "Go up one level in the current thread."
10181   (let ((parent (gnus-summary-article-parent)))
10182     (when parent
10183       (gnus-summary-goto-subject parent))))
10184
10185 (defun gnus-summary-down-thread (n)
10186   "Go down thread N steps.
10187 If N is negative, go up instead.
10188 Returns the difference between N and how many steps down that were
10189 taken."
10190   (interactive "p")
10191   (let ((up (< n 0))
10192         (n (abs n)))
10193     (while (and (> n 0)
10194                 (if up (gnus-summary-go-up-thread)
10195                   (gnus-summary-go-down-thread)))
10196       (setq n (1- n)))
10197     (gnus-summary-position-point)
10198     (when (/= 0 n)
10199       (gnus-message 7 "Can't go further"))
10200     n))
10201
10202 (defun gnus-summary-up-thread (n)
10203   "Go up thread N steps.
10204 If N is negative, go down instead.
10205 Returns the difference between N and how many steps down that were
10206 taken."
10207   (interactive "p")
10208   (gnus-summary-down-thread (- n)))
10209
10210 (defun gnus-summary-top-thread ()
10211   "Go to the top of the thread."
10212   (interactive)
10213   (while (gnus-summary-go-up-thread))
10214   (gnus-summary-article-number))
10215
10216 (defun gnus-summary-kill-thread (&optional unmark)
10217   "Mark articles under current thread as read.
10218 If the prefix argument is positive, remove any kinds of marks.
10219 If the prefix argument is negative, tick articles instead."
10220   (interactive "P")
10221   (when unmark
10222     (setq unmark (prefix-numeric-value unmark)))
10223   (let ((articles (gnus-summary-articles-in-thread)))
10224     (save-excursion
10225       ;; Expand the thread.
10226       (gnus-summary-show-thread)
10227       ;; Mark all the articles.
10228       (while articles
10229         (gnus-summary-goto-subject (car articles))
10230         (cond ((null unmark)
10231                (gnus-summary-mark-article-as-read gnus-killed-mark))
10232               ((> unmark 0)
10233                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10234               (t
10235                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10236         (setq articles (cdr articles))))
10237     ;; Hide killed subtrees.
10238     (and (null unmark)
10239          gnus-thread-hide-killed
10240          (gnus-summary-hide-thread))
10241     ;; If marked as read, go to next unread subject.
10242     (when (null unmark)
10243       ;; Go to next unread subject.
10244       (gnus-summary-next-subject 1 t)))
10245   (gnus-set-mode-line 'summary))
10246
10247 ;; Summary sorting commands
10248
10249 (defun gnus-summary-sort-by-number (&optional reverse)
10250   "Sort the summary buffer by article number.
10251 Argument REVERSE means reverse order."
10252   (interactive "P")
10253   (gnus-summary-sort 'number reverse))
10254
10255 (defun gnus-summary-sort-by-author (&optional reverse)
10256   "Sort the summary buffer by author name alphabetically.
10257 If `case-fold-search' is non-nil, case of letters is ignored.
10258 Argument REVERSE means reverse order."
10259   (interactive "P")
10260   (gnus-summary-sort 'author reverse))
10261
10262 (defun gnus-summary-sort-by-subject (&optional reverse)
10263   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10264 If `case-fold-search' is non-nil, case of letters is ignored.
10265 Argument REVERSE means reverse order."
10266   (interactive "P")
10267   (gnus-summary-sort 'subject reverse))
10268
10269 (defun gnus-summary-sort-by-date (&optional reverse)
10270   "Sort the summary buffer by date.
10271 Argument REVERSE means reverse order."
10272   (interactive "P")
10273   (gnus-summary-sort 'date reverse))
10274
10275 (defun gnus-summary-sort-by-score (&optional reverse)
10276   "Sort the summary buffer by score.
10277 Argument REVERSE means reverse order."
10278   (interactive "P")
10279   (gnus-summary-sort 'score reverse))
10280
10281 (defun gnus-summary-sort-by-lines (&optional reverse)
10282   "Sort the summary buffer by the number of lines.
10283 Argument REVERSE means reverse order."
10284   (interactive "P")
10285   (gnus-summary-sort 'lines reverse))
10286
10287 (defun gnus-summary-sort-by-chars (&optional reverse)
10288   "Sort the summary buffer by article length.
10289 Argument REVERSE means reverse order."
10290   (interactive "P")
10291   (gnus-summary-sort 'chars reverse))
10292
10293 (defun gnus-summary-sort-by-original (&optional reverse)
10294   "Sort the summary buffer using the default sorting method.
10295 Argument REVERSE means reverse order."
10296   (interactive "P")
10297   (let* ((buffer-read-only)
10298          (gnus-summary-prepare-hook nil))
10299     ;; We do the sorting by regenerating the threads.
10300     (gnus-summary-prepare)
10301     ;; Hide subthreads if needed.
10302     (gnus-summary-maybe-hide-threads)))
10303
10304 (defun gnus-summary-sort (predicate reverse)
10305   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10306   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10307          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10308          (gnus-thread-sort-functions
10309           (if (not reverse)
10310               thread
10311             `(lambda (t1 t2)
10312                (,thread t2 t1))))
10313          (gnus-sort-gathered-threads-function
10314           gnus-thread-sort-functions)
10315          (gnus-article-sort-functions
10316           (if (not reverse)
10317               article
10318             `(lambda (t1 t2)
10319                (,article t2 t1))))
10320          (buffer-read-only)
10321          (gnus-summary-prepare-hook nil))
10322     ;; We do the sorting by regenerating the threads.
10323     (gnus-summary-prepare)
10324     ;; Hide subthreads if needed.
10325     (gnus-summary-maybe-hide-threads)))
10326
10327 ;; Summary saving commands.
10328
10329 (defun gnus-summary-save-article (&optional n not-saved)
10330   "Save the current article using the default saver function.
10331 If N is a positive number, save the N next articles.
10332 If N is a negative number, save the N previous articles.
10333 If N is nil and any articles have been marked with the process mark,
10334 save those articles instead.
10335 The variable `gnus-default-article-saver' specifies the saver function."
10336   (interactive "P")
10337   (let* ((articles (gnus-summary-work-articles n))
10338          (save-buffer (save-excursion
10339                         (nnheader-set-temp-buffer " *Gnus Save*")))
10340          (num (length articles))
10341          header file)
10342     (dolist (article articles)
10343       (setq header (gnus-summary-article-header article))
10344       (if (not (vectorp header))
10345           ;; This is a pseudo-article.
10346           (if (assq 'name header)
10347               (gnus-copy-file (cdr (assq 'name header)))
10348             (gnus-message 1 "Article %d is unsaveable" article))
10349         ;; This is a real article.
10350         (save-window-excursion
10351           (let ((gnus-display-mime-function nil)
10352                 (gnus-article-prepare-hook nil))
10353             (gnus-summary-select-article t nil nil article)))
10354         (save-excursion
10355           (set-buffer save-buffer)
10356           (erase-buffer)
10357           (insert-buffer-substring gnus-original-article-buffer))
10358         (setq file (gnus-article-save save-buffer file num))
10359         (gnus-summary-remove-process-mark article)
10360         (unless not-saved
10361           (gnus-summary-set-saved-mark article))))
10362     (gnus-kill-buffer save-buffer)
10363     (gnus-summary-position-point)
10364     (gnus-set-mode-line 'summary)
10365     n))
10366
10367 (defun gnus-summary-pipe-output (&optional arg)
10368   "Pipe the current article to a subprocess.
10369 If N is a positive number, pipe the N next articles.
10370 If N is a negative number, pipe the N previous articles.
10371 If N is nil and any articles have been marked with the process mark,
10372 pipe those articles instead."
10373   (interactive "P")
10374   (require 'gnus-art)
10375   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10376     (gnus-summary-save-article arg t))
10377   (let ((buffer (get-buffer "*Shell Command Output*")))
10378     (if (and buffer
10379              (with-current-buffer buffer (> (point-max) (point-min))))
10380         (gnus-configure-windows 'pipe))))
10381
10382 (defun gnus-summary-save-article-mail (&optional arg)
10383   "Append the current article to an mail file.
10384 If N is a positive number, save the N next articles.
10385 If N is a negative number, save the N previous articles.
10386 If N is nil and any articles have been marked with the process mark,
10387 save those articles instead."
10388   (interactive "P")
10389   (require 'gnus-art)
10390   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10391     (gnus-summary-save-article arg)))
10392
10393 (defun gnus-summary-save-article-rmail (&optional arg)
10394   "Append the current article to an rmail file.
10395 If N is a positive number, save the N next articles.
10396 If N is a negative number, save the N previous articles.
10397 If N is nil and any articles have been marked with the process mark,
10398 save those articles instead."
10399   (interactive "P")
10400   (require 'gnus-art)
10401   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10402     (gnus-summary-save-article arg)))
10403
10404 (defun gnus-summary-save-article-file (&optional arg)
10405   "Append the current article to a file.
10406 If N is a positive number, save the N next articles.
10407 If N is a negative number, save the N previous articles.
10408 If N is nil and any articles have been marked with the process mark,
10409 save those articles instead."
10410   (interactive "P")
10411   (require 'gnus-art)
10412   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10413     (gnus-summary-save-article arg)))
10414
10415 (defun gnus-summary-write-article-file (&optional arg)
10416   "Write the current article to a file, deleting the previous file.
10417 If N is a positive number, save the N next articles.
10418 If N is a negative number, save the N previous articles.
10419 If N is nil and any articles have been marked with the process mark,
10420 save those articles instead."
10421   (interactive "P")
10422   (require 'gnus-art)
10423   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10424     (gnus-summary-save-article arg)))
10425
10426 (defun gnus-summary-save-article-body-file (&optional arg)
10427   "Append the current article body to a file.
10428 If N is a positive number, save the N next articles.
10429 If N is a negative number, save the N previous articles.
10430 If N is nil and any articles have been marked with the process mark,
10431 save those articles instead."
10432   (interactive "P")
10433   (require 'gnus-art)
10434   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10435     (gnus-summary-save-article arg)))
10436
10437 (defun gnus-summary-muttprint (&optional arg)
10438   "Print the current article using Muttprint.
10439 If N is a positive number, save the N next articles.
10440 If N is a negative number, save the N previous articles.
10441 If N is nil and any articles have been marked with the process mark,
10442 save those articles instead."
10443   (interactive "P")
10444   (require 'gnus-art)
10445   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10446     (gnus-summary-save-article arg t)))
10447
10448 (defun gnus-summary-pipe-message (program)
10449   "Pipe the current article through PROGRAM."
10450   (interactive "sProgram: ")
10451   (gnus-summary-select-article)
10452   (let ((mail-header-separator ""))
10453     (gnus-eval-in-buffer-window gnus-article-buffer
10454       (save-restriction
10455         (widen)
10456         (let ((start (window-start))
10457               buffer-read-only)
10458           (message-pipe-buffer-body program)
10459           (set-window-start (get-buffer-window (current-buffer)) start))))))
10460
10461 (defun gnus-get-split-value (methods)
10462   "Return a value based on the split METHODS."
10463   (let (split-name method result match)
10464     (when methods
10465       (save-excursion
10466         (set-buffer gnus-original-article-buffer)
10467         (save-restriction
10468           (nnheader-narrow-to-headers)
10469           (while (and methods (not split-name))
10470             (goto-char (point-min))
10471             (setq method (pop methods))
10472             (setq match (car method))
10473             (when (cond
10474                    ((stringp match)
10475                     ;; Regular expression.
10476                     (ignore-errors
10477                       (re-search-forward match nil t)))
10478                    ((gnus-functionp match)
10479                     ;; Function.
10480                     (save-restriction
10481                       (widen)
10482                       (setq result (funcall match gnus-newsgroup-name))))
10483                    ((consp match)
10484                     ;; Form.
10485                     (save-restriction
10486                       (widen)
10487                       (setq result (eval match)))))
10488               (setq split-name (cdr method))
10489               (cond ((stringp result)
10490                      (push (expand-file-name
10491                             result gnus-article-save-directory)
10492                            split-name))
10493                     ((consp result)
10494                      (setq split-name (append result split-name)))))))))
10495     (nreverse split-name)))
10496
10497 (defun gnus-valid-move-group-p (group)
10498   (and (boundp group)
10499        (symbol-name group)
10500        (symbol-value group)
10501        (gnus-get-function (gnus-find-method-for-group
10502                            (symbol-name group)) 'request-accept-article t)))
10503
10504 (defun gnus-read-move-group-name (prompt default articles prefix)
10505   "Read a group name."
10506   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10507          (minibuffer-confirm-incomplete nil) ; XEmacs
10508          (prom
10509           (format "%s %s to:"
10510                   prompt
10511                   (if (> (length articles) 1)
10512                       (format "these %d articles" (length articles))
10513                     "this article")))
10514          (to-newsgroup
10515           (cond
10516            ((null split-name)
10517             (gnus-completing-read-with-default
10518              default prom
10519              gnus-active-hashtb
10520              'gnus-valid-move-group-p
10521              nil prefix
10522              'gnus-group-history))
10523            ((= 1 (length split-name))
10524             (gnus-completing-read-with-default
10525              (car split-name) prom
10526              gnus-active-hashtb
10527              'gnus-valid-move-group-p
10528              nil nil
10529              'gnus-group-history))
10530            (t
10531             (gnus-completing-read-with-default
10532              nil prom
10533              (mapcar (lambda (el) (list el))
10534                      (nreverse split-name))
10535              nil nil nil
10536              'gnus-group-history))))
10537          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10538     (when to-newsgroup
10539       (if (or (string= to-newsgroup "")
10540               (string= to-newsgroup prefix))
10541           (setq to-newsgroup default))
10542       (unless to-newsgroup
10543         (error "No group name entered"))
10544       (or (gnus-active to-newsgroup)
10545           (gnus-activate-group to-newsgroup nil nil to-method)
10546           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10547                                      to-newsgroup))
10548               (or (and (gnus-request-create-group to-newsgroup to-method)
10549                        (gnus-activate-group
10550                         to-newsgroup nil nil to-method)
10551                        (gnus-subscribe-group to-newsgroup))
10552                   (error "Couldn't create group %s" to-newsgroup)))
10553           (error "No such group: %s" to-newsgroup)))
10554     to-newsgroup))
10555
10556 (defun gnus-summary-save-parts (type dir n &optional reverse)
10557   "Save parts matching TYPE to DIR.
10558 If REVERSE, save parts that do not match TYPE."
10559   (interactive
10560    (list (read-string "Save parts of type: "
10561                       (or (car gnus-summary-save-parts-type-history)
10562                           gnus-summary-save-parts-default-mime)
10563                       'gnus-summary-save-parts-type-history)
10564          (setq gnus-summary-save-parts-last-directory
10565                (read-file-name "Save to directory: "
10566                                gnus-summary-save-parts-last-directory
10567                                nil t))
10568          current-prefix-arg))
10569   (gnus-summary-iterate n
10570     (let ((gnus-display-mime-function nil)
10571           (gnus-inhibit-treatment t))
10572       (gnus-summary-select-article))
10573     (save-excursion
10574       (set-buffer gnus-article-buffer)
10575       (let ((handles (or gnus-article-mime-handles
10576                          (mm-dissect-buffer nil gnus-article-loose-mime)
10577                          (mm-uu-dissect))))
10578         (when handles
10579           (gnus-summary-save-parts-1 type dir handles reverse)
10580           (unless gnus-article-mime-handles ;; Don't destroy this case.
10581             (mm-destroy-parts handles)))))))
10582
10583 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10584   (if (stringp (car handle))
10585       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10586               (cdr handle))
10587     (when (if reverse
10588               (not (string-match type (mm-handle-media-type handle)))
10589             (string-match type (mm-handle-media-type handle)))
10590       (let ((file (expand-file-name
10591                    (file-name-nondirectory
10592                     (or
10593                      (mail-content-type-get
10594                       (mm-handle-disposition handle) 'filename)
10595                      (concat gnus-newsgroup-name
10596                              "." (number-to-string
10597                                   (cdr gnus-article-current)))))
10598                    dir)))
10599         (unless (file-exists-p file)
10600           (mm-save-part-to-file handle file))))))
10601
10602 ;; Summary extract commands
10603
10604 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10605   (let ((buffer-read-only nil)
10606         (article (gnus-summary-article-number))
10607         after-article b e)
10608     (unless (gnus-summary-goto-subject article)
10609       (error "No such article: %d" article))
10610     (gnus-summary-position-point)
10611     ;; If all commands are to be bunched up on one line, we collect
10612     ;; them here.
10613     (unless gnus-view-pseudos-separately
10614       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10615             files action)
10616         (while ps
10617           (setq action (cdr (assq 'action (car ps))))
10618           (setq files (list (cdr (assq 'name (car ps)))))
10619           (while (and ps (cdr ps)
10620                       (string= (or action "1")
10621                                (or (cdr (assq 'action (cadr ps))) "2")))
10622             (push (cdr (assq 'name (cadr ps))) files)
10623             (setcdr ps (cddr ps)))
10624           (when files
10625             (when (not (string-match "%s" action))
10626               (push " " files))
10627             (push " " files)
10628             (when (assq 'execute (car ps))
10629               (setcdr (assq 'execute (car ps))
10630                       (funcall (if (string-match "%s" action)
10631                                    'format 'concat)
10632                                action
10633                                (mapconcat
10634                                 (lambda (f)
10635                                   (if (equal f " ")
10636                                       f
10637                                     (gnus-quote-arg-for-sh-or-csh f)))
10638                                 files " ")))))
10639           (setq ps (cdr ps)))))
10640     (if (and gnus-view-pseudos (not not-view))
10641         (while pslist
10642           (when (assq 'execute (car pslist))
10643             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10644                                   (eq gnus-view-pseudos 'not-confirm)))
10645           (setq pslist (cdr pslist)))
10646       (save-excursion
10647         (while pslist
10648           (setq after-article (or (cdr (assq 'article (car pslist)))
10649                                   (gnus-summary-article-number)))
10650           (gnus-summary-goto-subject after-article)
10651           (forward-line 1)
10652           (setq b (point))
10653           (insert "    " (file-name-nondirectory
10654                           (cdr (assq 'name (car pslist))))
10655                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10656           (setq e (point))
10657           (forward-line -1)             ; back to `b'
10658           (gnus-add-text-properties
10659            b (1- e) (list 'gnus-number gnus-reffed-article-number
10660                           gnus-mouse-face-prop gnus-mouse-face))
10661           (gnus-data-enter
10662            after-article gnus-reffed-article-number
10663            gnus-unread-mark b (car pslist) 0 (- e b))
10664           (setq gnus-newsgroup-unreads
10665                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10666                                          gnus-reffed-article-number))
10667           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10668           (setq pslist (cdr pslist)))))))
10669
10670 (defun gnus-pseudos< (p1 p2)
10671   (let ((c1 (cdr (assq 'action p1)))
10672         (c2 (cdr (assq 'action p2))))
10673     (and c1 c2 (string< c1 c2))))
10674
10675 (defun gnus-request-pseudo-article (props)
10676   (cond ((assq 'execute props)
10677          (gnus-execute-command (cdr (assq 'execute props)))))
10678   (let ((gnus-current-article (gnus-summary-article-number)))
10679     (gnus-run-hooks 'gnus-mark-article-hook)))
10680
10681 (defun gnus-execute-command (command &optional automatic)
10682   (save-excursion
10683     (gnus-article-setup-buffer)
10684     (set-buffer gnus-article-buffer)
10685     (setq buffer-read-only nil)
10686     (let ((command (if automatic command
10687                      (read-string "Command: " (cons command 0)))))
10688       (erase-buffer)
10689       (insert "$ " command "\n\n")
10690       (if gnus-view-pseudo-asynchronously
10691           (start-process "gnus-execute" (current-buffer) shell-file-name
10692                          shell-command-switch command)
10693         (call-process shell-file-name nil t nil
10694                       shell-command-switch command)))))
10695
10696 ;; Summary kill commands.
10697
10698 (defun gnus-summary-edit-global-kill (article)
10699   "Edit the \"global\" kill file."
10700   (interactive (list (gnus-summary-article-number)))
10701   (gnus-group-edit-global-kill article))
10702
10703 (defun gnus-summary-edit-local-kill ()
10704   "Edit a local kill file applied to the current newsgroup."
10705   (interactive)
10706   (setq gnus-current-headers (gnus-summary-article-header))
10707   (gnus-group-edit-local-kill
10708    (gnus-summary-article-number) gnus-newsgroup-name))
10709
10710 ;;; Header reading.
10711
10712 (defun gnus-read-header (id &optional header)
10713   "Read the headers of article ID and enter them into the Gnus system."
10714   (let ((group gnus-newsgroup-name)
10715         (gnus-override-method
10716          (or
10717           gnus-override-method
10718           (and (gnus-news-group-p gnus-newsgroup-name)
10719                (car (gnus-refer-article-methods)))))
10720         where)
10721     ;; First we check to see whether the header in question is already
10722     ;; fetched.
10723     (if (stringp id)
10724         ;; This is a Message-ID.
10725         (setq header (or header (gnus-id-to-header id)))
10726       ;; This is an article number.
10727       (setq header (or header (gnus-summary-article-header id))))
10728     (if (and header
10729              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10730         ;; We have found the header.
10731         header
10732       ;; If this is a sparse article, we have to nix out its
10733       ;; previous entry in the thread hashtb.
10734       (when (and header
10735                  (gnus-summary-article-sparse-p (mail-header-number header)))
10736         (let* ((parent (gnus-parent-id (mail-header-references header)))
10737                (thread (and parent (gnus-id-to-thread parent))))
10738           (when thread
10739             (delq (assq header thread) thread))))
10740       ;; We have to really fetch the header to this article.
10741       (save-excursion
10742         (set-buffer nntp-server-buffer)
10743         (when (setq where (gnus-request-head id group))
10744           (nnheader-fold-continuation-lines)
10745           (goto-char (point-max))
10746           (insert ".\n")
10747           (goto-char (point-min))
10748           (insert "211 ")
10749           (princ (cond
10750                   ((numberp id) id)
10751                   ((cdr where) (cdr where))
10752                   (header (mail-header-number header))
10753                   (t gnus-reffed-article-number))
10754                  (current-buffer))
10755           (insert " Article retrieved.\n"))
10756         (if (or (not where)
10757                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10758             ()                          ; Malformed head.
10759           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10760             (when (and (stringp id)
10761                        (not (string= (gnus-group-real-name group)
10762                                      (car where))))
10763               ;; If we fetched by Message-ID and the article came
10764               ;; from a different group, we fudge some bogus article
10765               ;; numbers for this article.
10766               (mail-header-set-number header gnus-reffed-article-number))
10767             (save-excursion
10768               (set-buffer gnus-summary-buffer)
10769               (decf gnus-reffed-article-number)
10770               (gnus-remove-header (mail-header-number header))
10771               (push header gnus-newsgroup-headers)
10772               (setq gnus-current-headers header)
10773               (push (mail-header-number header) gnus-newsgroup-limit)))
10774           header)))))
10775
10776 (defun gnus-remove-header (number)
10777   "Remove header NUMBER from `gnus-newsgroup-headers'."
10778   (if (and gnus-newsgroup-headers
10779            (= number (mail-header-number (car gnus-newsgroup-headers))))
10780       (pop gnus-newsgroup-headers)
10781     (let ((headers gnus-newsgroup-headers))
10782       (while (and (cdr headers)
10783                   (not (= number (mail-header-number (cadr headers)))))
10784         (pop headers))
10785       (when (cdr headers)
10786         (setcdr headers (cddr headers))))))
10787
10788 ;;;
10789 ;;; summary highlights
10790 ;;;
10791
10792 (defun gnus-highlight-selected-summary ()
10793   "Highlight selected article in summary buffer."
10794   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10795   (when gnus-summary-selected-face
10796     (save-excursion
10797       (let* ((beg (progn (beginning-of-line) (point)))
10798              (end (progn (end-of-line) (point)))
10799              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10800              (from (if (get-text-property beg gnus-mouse-face-prop)
10801                        beg
10802                      (or (next-single-property-change
10803                           beg gnus-mouse-face-prop nil end)
10804                          beg)))
10805              (to
10806               (if (= from end)
10807                   (- from 2)
10808                 (or (next-single-property-change
10809                      from gnus-mouse-face-prop nil end)
10810                     end))))
10811         ;; If no mouse-face prop on line we will have to = from = end,
10812         ;; so we highlight the entire line instead.
10813         (when (= (+ to 2) from)
10814           (setq from beg)
10815           (setq to end))
10816         (if gnus-newsgroup-selected-overlay
10817             ;; Move old overlay.
10818             (gnus-move-overlay
10819              gnus-newsgroup-selected-overlay from to (current-buffer))
10820           ;; Create new overlay.
10821           (gnus-overlay-put
10822            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10823            'face gnus-summary-selected-face))))))
10824
10825 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10826 (defun gnus-summary-highlight-line ()
10827   "Highlight current line according to `gnus-summary-highlight'."
10828   (let* ((list gnus-summary-highlight)
10829          (beg (gnus-point-at-bol))
10830          (article (gnus-summary-article-number))
10831          (score (or (cdr (assq (or article gnus-current-article)
10832                                gnus-newsgroup-scored))
10833                     gnus-summary-default-score 0))
10834          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10835          (inhibit-read-only t)
10836          (default gnus-summary-default-score)
10837          (default-high gnus-summary-default-high-score)
10838          (default-low gnus-summary-default-low-score))
10839     ;; Eval the cars of the lists until we find a match.
10840     (while (and list
10841                 (not (eval (caar list))))
10842       (setq list (cdr list)))
10843     (let ((face (cdar list)))
10844       (unless (eq face (get-text-property beg 'face))
10845         (gnus-put-text-property-excluding-characters-with-faces
10846          beg (gnus-point-at-eol) 'face
10847          (setq face (if (boundp face) (symbol-value face) face)))
10848         (when gnus-summary-highlight-line-function
10849           (funcall gnus-summary-highlight-line-function article face))))))
10850
10851 (defun gnus-update-read-articles (group unread &optional compute)
10852   "Update the list of read articles in GROUP.
10853 UNREAD is a sorted list."
10854   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10855          (entry (gnus-gethash group gnus-newsrc-hashtb))
10856          (info (nth 2 entry))
10857          (prev 1)
10858          read)
10859     (if (or (not info) (not active))
10860         ;; There is no info on this group if it was, in fact,
10861         ;; killed.  Gnus stores no information on killed groups, so
10862         ;; there's nothing to be done.
10863         ;; One could store the information somewhere temporarily,
10864         ;; perhaps...  Hmmm...
10865         ()
10866       ;; Remove any negative articles numbers.
10867       (while (and unread (< (car unread) 0))
10868         (setq unread (cdr unread)))
10869       ;; Remove any expired article numbers
10870       (while (and unread (< (car unread) (car active)))
10871         (setq unread (cdr unread)))
10872       ;; Compute the ranges of read articles by looking at the list of
10873       ;; unread articles.
10874       (while unread
10875         (when (/= (car unread) prev)
10876           (push (if (= prev (1- (car unread))) prev
10877                   (cons prev (1- (car unread))))
10878                 read))
10879         (setq prev (1+ (car unread)))
10880         (setq unread (cdr unread)))
10881       (when (<= prev (cdr active))
10882         (push (cons prev (cdr active)) read))
10883       (setq read (if (> (length read) 1) (nreverse read) read))
10884       (if compute
10885           read
10886         (save-excursion
10887           (let (setmarkundo)
10888             ;; Propagate the read marks to the backend.
10889             (when (gnus-check-backend-function 'request-set-mark group)
10890               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10891                     (add (gnus-remove-from-range read (gnus-info-read info))))
10892                 (when (or add del)
10893                   (unless (gnus-check-group group)
10894                     (error "Can't open server for %s" group))
10895                   (gnus-request-set-mark
10896                    group (delq nil (list (if add (list add 'add '(read)))
10897                                          (if del (list del 'del '(read))))))
10898                   (setq setmarkundo
10899                         `(gnus-request-set-mark
10900                           ,group
10901                           ',(delq nil (list
10902                                        (if del (list del 'add '(read)))
10903                                        (if add (list add 'del '(read))))))))))
10904             (set-buffer gnus-group-buffer)
10905             (gnus-undo-register
10906               `(progn
10907                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10908                  (gnus-info-set-read ',info ',(gnus-info-read info))
10909                  (gnus-get-unread-articles-in-group ',info
10910                                                     (gnus-active ,group))
10911                  (gnus-group-update-group ,group t)
10912                  ,setmarkundo))))
10913         ;; Enter this list into the group info.
10914         (gnus-info-set-read info read)
10915         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10916         (gnus-get-unread-articles-in-group info (gnus-active group))
10917         t))))
10918
10919 (defun gnus-offer-save-summaries ()
10920   "Offer to save all active summary buffers."
10921   (let (buffers)
10922     ;; Go through all buffers and find all summaries.
10923     (dolist (buffer (buffer-list))
10924       (when (and (setq buffer (buffer-name buffer))
10925                  (string-match "Summary" buffer)
10926                  (save-excursion
10927                    (set-buffer buffer)
10928                    ;; We check that this is, indeed, a summary buffer.
10929                    (and (eq major-mode 'gnus-summary-mode)
10930                         ;; Also make sure this isn't bogus.
10931                         gnus-newsgroup-prepared
10932                         ;; Also make sure that this isn't a
10933                         ;; dead summary buffer.
10934                         (not gnus-dead-summary-mode))))
10935         (push buffer buffers)))
10936     ;; Go through all these summary buffers and offer to save them.
10937     (when buffers
10938       (save-excursion
10939         (map-y-or-n-p
10940          "Update summary buffer %s? "
10941          (lambda (buf)
10942            (switch-to-buffer buf)
10943            (gnus-summary-exit))
10944          buffers)))))
10945
10946
10947 ;;; @ for mime-partial
10948 ;;;
10949
10950 (defun gnus-request-partial-message ()
10951   (save-excursion
10952     (let ((number (gnus-summary-article-number))
10953           (group gnus-newsgroup-name)
10954           (mother gnus-article-buffer))
10955       (set-buffer (get-buffer-create " *Partial Article*"))
10956       (erase-buffer)
10957       (setq mime-preview-buffer mother)
10958       (gnus-request-article-this-buffer number group)
10959       (mime-parse-buffer)
10960       )))
10961
10962 (autoload 'mime-combine-message/partial-pieces-automatically
10963   "mime-partial"
10964   "Internal method to combine message/partial messages automatically.")
10965
10966 (mime-add-condition
10967  'action '((type . message)(subtype . partial)
10968            (major-mode . gnus-original-article-mode)
10969            (method . mime-combine-message/partial-pieces-automatically)
10970            (summary-buffer-exp . gnus-summary-buffer)
10971            (request-partial-message-method . gnus-request-partial-message)
10972            ))
10973
10974
10975 ;;; @ for message/rfc822
10976 ;;;
10977
10978 (defun gnus-mime-extract-message/rfc822 (entity situation)
10979   "Burst a forwarded article."
10980   (save-excursion
10981     (set-buffer gnus-summary-buffer)
10982     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
10983                                    gnus-newsgroup-name 'gnus-group-history))
10984            (gnus-group-marked (list group))
10985            article info)
10986       (with-temp-buffer
10987         (mime-insert-entity-content entity)
10988         (setq article (gnus-request-accept-article group)))
10989       (when (and (consp article)
10990                  (numberp (setq article (cdr article))))
10991         (setq info (gnus-get-info group))
10992         (gnus-info-set-read info
10993                             (gnus-remove-from-range (gnus-info-read info)
10994                                                     (list article)))
10995         (when (string-equal group gnus-newsgroup-name)
10996           (forward-line 1)
10997           (let (gnus-show-threads)
10998             (gnus-summary-goto-subject article t))
10999           (gnus-summary-clear-mark-forward 1))
11000         (set-buffer gnus-group-buffer)
11001         (gnus-group-get-new-news-this-group nil t)))))
11002
11003 (mime-add-condition
11004  'action '((type . message)(subtype . rfc822)
11005            (major-mode . gnus-original-article-mode)
11006            (method . gnus-mime-extract-message/rfc822)
11007            (mode . "extract")
11008            ))
11009
11010 (mime-add-condition
11011  'action '((type . message)(subtype . news)
11012            (major-mode . gnus-original-article-mode)
11013            (method . gnus-mime-extract-message/rfc822)
11014            (mode . "extract")
11015            ))
11016
11017 (defun gnus-mime-extract-multipart (entity situation)
11018   (let ((children (mime-entity-children entity))
11019         mime-acting-situation-to-override
11020         f)
11021     (while children
11022       (mime-play-entity (car children)
11023                         (cons (assq 'mode situation)
11024                               mime-acting-situation-to-override))
11025       (setq children (cdr children)))
11026     (if (setq f (cdr (assq 'after-method
11027                            mime-acting-situation-to-override)))
11028         (eval f)
11029       )))
11030
11031 (mime-add-condition
11032  'action '((type . multipart)
11033            (method . gnus-mime-extract-multipart)
11034            (mode . "extract")
11035            )
11036  'with-default)
11037
11038
11039 ;;; @ end
11040 ;;;
11041
11042 (defun gnus-summary-inherit-default-charset ()
11043   "Import `default-mime-charset' from summary buffer."
11044   (if (buffer-live-p gnus-summary-buffer)
11045       (if (local-variable-p 'default-mime-charset gnus-summary-buffer)
11046           (progn
11047             (make-local-variable 'default-mime-charset)
11048             (setq default-mime-charset
11049                   (with-current-buffer gnus-summary-buffer
11050                     default-mime-charset)))
11051         (kill-local-variable 'default-mime-charset))))
11052
11053 (defun gnus-summary-setup-default-charset ()
11054   "Setup newsgroup default charset."
11055   (if (equal gnus-newsgroup-name "nndraft:drafts")
11056       (progn
11057         (setq gnus-newsgroup-charset nil)
11058         (make-local-variable 'default-mime-charset)
11059         (setq default-mime-charset nil))
11060     (let ((ignored-charsets
11061            (or gnus-newsgroup-ephemeral-ignored-charsets
11062                (append
11063                 (and gnus-newsgroup-name
11064                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11065                 gnus-newsgroup-ignored-charsets)))
11066           charset)
11067       (setq gnus-newsgroup-charset
11068             (or gnus-newsgroup-ephemeral-charset
11069                 (when (and gnus-newsgroup-name
11070                            (setq charset (gnus-parameter-charset
11071                                           gnus-newsgroup-name)))
11072                   (make-local-variable 'default-mime-charset)
11073                   (setq default-mime-charset charset))
11074                 gnus-default-charset))
11075       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11076            ignored-charsets))))
11077
11078 ;;;
11079 ;;; Mime Commands
11080 ;;;
11081
11082 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11083   "Display the current article buffer fully MIME-buttonized.
11084 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11085 treated as multipart/mixed."
11086   (interactive "P")
11087   (require 'gnus-art)
11088   (let ((gnus-unbuttonized-mime-types nil)
11089         (gnus-mime-display-multipart-as-mixed show-all-parts))
11090     (gnus-summary-show-article)))
11091
11092 (defun gnus-summary-repair-multipart (article)
11093   "Add a Content-Type header to a multipart article without one."
11094   (interactive (list (gnus-summary-article-number)))
11095   (gnus-with-article article
11096     (message-narrow-to-head)
11097     (message-remove-header "Mime-Version")
11098     (goto-char (point-max))
11099     (insert "Mime-Version: 1.0\n")
11100     (widen)
11101     (when (search-forward "\n--" nil t)
11102       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11103         (message-narrow-to-head)
11104         (message-remove-header "Content-Type")
11105         (goto-char (point-max))
11106         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11107                         separator))
11108         (widen))))
11109   (let (gnus-mark-article-hook)
11110     (gnus-summary-select-article t t nil article)))
11111
11112 (defun gnus-summary-toggle-display-buttonized ()
11113   "Toggle the buttonizing of the article buffer."
11114   (interactive)
11115   (require 'gnus-art)
11116   (if (setq gnus-inhibit-mime-unbuttonizing
11117             (not gnus-inhibit-mime-unbuttonizing))
11118       (let ((gnus-unbuttonized-mime-types nil))
11119         (gnus-summary-show-article))
11120     (gnus-summary-show-article)))
11121
11122 ;;;
11123 ;;; Intelli-mouse commmands
11124 ;;;
11125
11126 (defun gnus-wheel-summary-scroll (event)
11127   (interactive "e")
11128   (let ((amount (if (memq 'shift (event-modifiers event))
11129                     (car gnus-wheel-scroll-amount)
11130                   (cdr gnus-wheel-scroll-amount)))
11131         (direction (- (* (static-if (featurep 'xemacs)
11132                              (event-button event)
11133                            (cond ((eq 'mouse-4 (event-basic-type event))
11134                                   4)
11135                                  ((eq 'mouse-5 (event-basic-type event))
11136                                   5)))
11137                          2) 9))
11138         edge)
11139     (gnus-summary-scroll-up (* amount direction))
11140     (when (gnus-eval-in-buffer-window gnus-article-buffer
11141             (save-restriction
11142               (widen)
11143               (and (if (< 0 direction)
11144                        (gnus-article-next-page 0)
11145                      (gnus-article-prev-page 0)
11146                      (bobp))
11147                    (if (setq edge (get-text-property
11148                                    (point-min) 'gnus-wheel-edge))
11149                        (setq edge (* edge direction))
11150                      (setq edge -1))
11151                    (or (plusp edge)
11152                        (let ((buffer-read-only nil)
11153                              (inhibit-read-only t))
11154                          (put-text-property (point-min) (point-max)
11155                                             'gnus-wheel-edge direction)
11156                          nil))
11157                    (or (> edge gnus-wheel-edge-resistance)
11158                        (let ((buffer-read-only nil)
11159                              (inhibit-read-only t))
11160                          (put-text-property (point-min) (point-max)
11161                                             'gnus-wheel-edge
11162                                             (* (1+ edge) direction))
11163                          nil))
11164                    (eq last-command 'gnus-wheel-summary-scroll))))
11165       (gnus-summary-next-article nil nil (minusp direction)))))
11166
11167 (defun gnus-wheel-install ()
11168   "Enable mouse wheel support on summary window."
11169   (when gnus-use-wheel
11170     (let ((keys
11171            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11172       (dolist (key keys)
11173         (define-key gnus-summary-mode-map key
11174           'gnus-wheel-summary-scroll)))))
11175
11176 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11177
11178 ;;;
11179 ;;; Traditional PGP commmands
11180 ;;;
11181
11182 (defun gnus-summary-decrypt-article (&optional force)
11183   "Decrypt the current article in traditional PGP way.
11184 This will have permanent effect only in mail groups.
11185 If FORCE is non-nil, allow editing of articles even in read-only
11186 groups."
11187   (interactive "P")
11188   (gnus-summary-select-article t)
11189   (gnus-eval-in-buffer-window gnus-article-buffer
11190     (save-excursion
11191       (save-restriction
11192         (widen)
11193         (goto-char (point-min))
11194         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11195           (error "Not a traditional PGP message!"))
11196         (let ((armor-start (match-beginning 0)))
11197           (if (and (pgg-decrypt-region armor-start (point-max))
11198                    (or force (not (gnus-group-read-only-p))))
11199               (let ((inhibit-read-only t)
11200                     buffer-read-only)
11201                 (delete-region armor-start
11202                                (progn
11203                                  (re-search-forward "^-+END PGP" nil t)
11204                                  (beginning-of-line 2)
11205                                  (point)))
11206                 (insert-buffer-substring pgg-output-buffer))))))))
11207
11208 (defun gnus-summary-verify-article ()
11209   "Verify the current article in traditional PGP way."
11210   (interactive)
11211   (save-excursion
11212     (set-buffer gnus-original-article-buffer)
11213     (goto-char (point-min))
11214     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11215       (error "Not a traditional PGP message!"))
11216     (re-search-forward "^-+END PGP" nil t)
11217     (beginning-of-line 2)
11218     (call-interactively (function pgg-verify-region))))
11219
11220 ;;;
11221 ;;; Generic summary marking commands
11222 ;;;
11223
11224 (defvar gnus-summary-marking-alist
11225   '((read gnus-del-mark "d")
11226     (unread gnus-unread-mark "u")
11227     (ticked gnus-ticked-mark "!")
11228     (dormant gnus-dormant-mark "?")
11229     (expirable gnus-expirable-mark "e"))
11230   "An alist of names/marks/keystrokes.")
11231
11232 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11233 (defvar gnus-summary-mark-map)
11234
11235 (defun gnus-summary-make-all-marking-commands ()
11236   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11237   (dolist (elem gnus-summary-marking-alist)
11238     (apply 'gnus-summary-make-marking-command elem)))
11239
11240 (defun gnus-summary-make-marking-command (name mark keystroke)
11241   (let ((map (make-sparse-keymap)))
11242     (define-key gnus-summary-generic-mark-map keystroke map)
11243     (dolist (lway `((next "next" next nil "n")
11244                     (next-unread "next unread" next t "N")
11245                     (prev "previous" prev nil "p")
11246                     (prev-unread "previous unread" prev t "P")
11247                     (nomove "" nil nil ,keystroke)))
11248       (let ((func (gnus-summary-make-marking-command-1
11249                    mark (car lway) lway name)))
11250         (setq func (eval func))
11251         (define-key map (nth 4 lway) func)))))
11252
11253 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11254   `(defun ,(intern
11255             (format "gnus-summary-put-mark-as-%s%s"
11256                     name (if (eq way 'nomove)
11257                              ""
11258                            (concat "-" (symbol-name way)))))
11259      (n)
11260      ,(format
11261        "Mark the current article as %s%s.
11262 If N, the prefix, then repeat N times.
11263 If N is negative, move in reverse order.
11264 The difference between N and the actual number of articles marked is
11265 returned."
11266        name (car (cdr lway)))
11267      (interactive "p")
11268      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11269
11270 (defun gnus-summary-generic-mark (n mark move unread)
11271   "Mark N articles with MARK."
11272   (unless (eq major-mode 'gnus-summary-mode)
11273     (error "This command can only be used in the summary buffer"))
11274   (gnus-summary-show-thread)
11275   (let ((nummove
11276          (cond
11277           ((eq move 'next) 1)
11278           ((eq move 'prev) -1)
11279           (t 0))))
11280     (if (zerop nummove)
11281         (setq n 1)
11282       (when (< n 0)
11283         (setq n (abs n)
11284               nummove (* -1 nummove))))
11285     (while (and (> n 0)
11286                 (gnus-summary-mark-article nil mark)
11287                 (zerop (gnus-summary-next-subject nummove unread t)))
11288       (setq n (1- n)))
11289     (when (/= 0 n)
11290       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11291     (gnus-summary-recenter)
11292     (gnus-summary-position-point)
11293     (gnus-set-mode-line 'summary)
11294     n))
11295
11296 (defun gnus-summary-insert-articles (articles)
11297   (when (setq articles
11298               (gnus-sorted-difference articles
11299                                       (mapcar (lambda (h)
11300                                                 (mail-header-number h))
11301                                               gnus-newsgroup-headers)))
11302     (setq gnus-newsgroup-headers
11303           (merge 'list
11304                  gnus-newsgroup-headers
11305                  (gnus-fetch-headers articles)
11306                  'gnus-article-sort-by-number))
11307     ;; Suppress duplicates?
11308     (when gnus-suppress-duplicates
11309       (gnus-dup-suppress-articles))
11310
11311     ;; We might want to build some more threads first.
11312     (when (and gnus-fetch-old-headers
11313                (eq gnus-headers-retrieved-by 'nov))
11314       (if (eq gnus-fetch-old-headers 'invisible)
11315           (gnus-build-all-threads)
11316         (gnus-build-old-threads)))
11317     ;; Let the Gnus agent mark articles as read.
11318     (when gnus-agent
11319       (gnus-agent-get-undownloaded-list))
11320     ;; Remove list identifiers from subject
11321     (when gnus-list-identifiers
11322       (gnus-summary-remove-list-identifiers))
11323     ;; First and last article in this newsgroup.
11324     (when gnus-newsgroup-headers
11325       (setq gnus-newsgroup-begin
11326             (mail-header-number (car gnus-newsgroup-headers))
11327             gnus-newsgroup-end
11328             (mail-header-number
11329              (gnus-last-element gnus-newsgroup-headers))))
11330     (when gnus-use-scoring
11331       (gnus-possibly-score-headers))))
11332
11333 (defun gnus-summary-insert-old-articles (&optional all)
11334   "Insert all old articles in this group.
11335 If ALL is non-nil, already read articles become readable.
11336 If ALL is a number, fetch this number of articles."
11337   (interactive "P")
11338   (prog1
11339       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11340             older len)
11341         (setq older
11342               (gnus-sorted-difference
11343                (gnus-uncompress-range (list gnus-newsgroup-active))
11344                old))
11345         (setq len (length older))
11346         (cond
11347          ((null older) nil)
11348          ((numberp all)
11349           (if (< all len)
11350               (setq older (last older all))))
11351          (all nil)
11352          (t
11353           (if (and (numberp gnus-large-newsgroup)
11354                    (> len gnus-large-newsgroup))
11355               (let* ((cursor-in-echo-area nil)
11356                      (initial (gnus-parameter-large-newsgroup-initial 
11357                                gnus-newsgroup-name))
11358                      (input
11359                       (read-string
11360                        (format
11361                         "How many articles from %s (%s %d): "
11362                         (gnus-limit-string
11363                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11364                         (if initial "max" "default")
11365                         len)
11366                        (if initial
11367                            (cons (number-to-string initial)
11368                                  0)))))
11369                 (unless (string-match "^[ \t]*$" input)
11370                   (setq all (string-to-number input))
11371                   (if (< all len)
11372                       (setq older (last older all))))))))
11373         (if (not older)
11374             (message "No old news.")
11375           (gnus-summary-insert-articles older)
11376           (gnus-summary-limit (gnus-sorted-nunion old older))))
11377     (gnus-summary-position-point)))
11378
11379 (defun gnus-summary-insert-new-articles ()
11380   "Insert all new articles in this group."
11381   (interactive)
11382   (prog1
11383       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11384             (old-active gnus-newsgroup-active)
11385             (nnmail-fetched-sources (list t))
11386             i new)
11387         (setq gnus-newsgroup-active
11388               (gnus-activate-group gnus-newsgroup-name 'scan))
11389         (setq i (1+ (cdr old-active)))
11390         (while (<= i (cdr gnus-newsgroup-active))
11391           (push i new)
11392           (incf i))
11393         (if (not new)
11394             (message "No gnus is bad news.")
11395           (setq new (nreverse new))
11396           (gnus-summary-insert-articles new)
11397           (setq gnus-newsgroup-unreads
11398                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11399           (gnus-summary-limit (gnus-sorted-nunion old new))))
11400     (gnus-summary-position-point)))
11401
11402 (gnus-summary-make-all-marking-commands)
11403
11404 (gnus-ems-redefine)
11405
11406 (provide 'gnus-sum)
11407
11408 (run-hooks 'gnus-sum-load-hook)
11409
11410 ;;; gnus-sum.el ends here