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-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', `gnus-article-sort-by-random'
675 and `gnus-article-sort-by-score'.
676
677 When threading is turned on, the variable `gnus-thread-sort-functions'
678 controls how articles are sorted."
679   :group 'gnus-summary-sort
680   :type '(repeat (choice (function-item gnus-article-sort-by-number)
681                          (function-item gnus-article-sort-by-author)
682                          (function-item gnus-article-sort-by-subject)
683                          (function-item gnus-article-sort-by-date)
684                          (function-item gnus-article-sort-by-score)
685                          (function-item gnus-article-sort-by-random)
686                          (function :tag "other"))))
687
688 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
689   "*List of functions used for sorting threads in the summary buffer.
690 By default, threads are sorted by article number.
691
692 Each function takes two threads and returns non-nil if the first
693 thread should be sorted before the other.  If you use more than one
694 function, the primary sort function should be the last.  You should
695 probably always include `gnus-thread-sort-by-number' in the list of
696 sorting functions -- preferably first.  Also note that sorting by date
697 is often much slower than sorting by number, and the sorting order is
698 very similar.  (Sorting by date means sorting by the time the message
699 was sent, sorting by number means sorting by arrival time.)
700
701 Ready-made functions include `gnus-thread-sort-by-number',
702 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
703 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
704 `gnus-thread-sort-by-most-recent-number',
705 `gnus-thread-sort-by-most-recent-date',
706 `gnus-thread-sort-by-random', and
707 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
708
709 When threading is turned off, the variable
710 `gnus-article-sort-functions' controls how articles are sorted."
711   :group 'gnus-summary-sort
712   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
713                          (function-item gnus-thread-sort-by-author)
714                          (function-item gnus-thread-sort-by-subject)
715                          (function-item gnus-thread-sort-by-date)
716                          (function-item gnus-thread-sort-by-score)
717                          (function-item gnus-thread-sort-by-total-score)
718                          (function-item gnus-thread-sort-by-random)
719                          (function :tag "other"))))
720
721 (defcustom gnus-thread-score-function '+
722   "*Function used for calculating the total score of a thread.
723
724 The function is called with the scores of the article and each
725 subthread and should then return the score of the thread.
726
727 Some functions you can use are `+', `max', or `min'."
728   :group 'gnus-summary-sort
729   :type 'function)
730
731 (defcustom gnus-summary-expunge-below nil
732   "All articles that have a score less than this variable will be expunged.
733 This variable is local to the summary buffers."
734   :group 'gnus-score-default
735   :type '(choice (const :tag "off" nil)
736                  integer))
737
738 (defcustom gnus-thread-expunge-below nil
739   "All threads that have a total score less than this variable will be expunged.
740 See `gnus-thread-score-function' for en explanation of what a
741 \"thread score\" is.
742
743 This variable is local to the summary buffers."
744   :group 'gnus-threading
745   :group 'gnus-score-default
746   :type '(choice (const :tag "off" nil)
747                  integer))
748
749 (defcustom gnus-summary-mode-hook nil
750   "*A hook for Gnus summary mode.
751 This hook is run before any variables are set in the summary buffer."
752   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
753   :group 'gnus-summary-various
754   :type 'hook)
755
756 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
757 (when (featurep 'xemacs)
758   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
759   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
760   (add-hook 'gnus-summary-mode-hook
761             'gnus-xmas-switch-horizontal-scrollbar-off))
762
763 (defcustom gnus-summary-menu-hook nil
764   "*Hook run after the creation of the summary mode menu."
765   :group 'gnus-summary-visual
766   :type 'hook)
767
768 (defcustom gnus-summary-exit-hook nil
769   "*A hook called on exit from the summary buffer.
770 It will be called with point in the group buffer."
771   :group 'gnus-summary-exit
772   :type 'hook)
773
774 (defcustom gnus-summary-prepare-hook nil
775   "*A hook called after the summary buffer has been generated.
776 If you want to modify the summary buffer, you can use this hook."
777   :group 'gnus-summary-various
778   :type 'hook)
779
780 (defcustom gnus-summary-prepared-hook nil
781   "*A hook called as the last thing after the summary buffer has been generated."
782   :group 'gnus-summary-various
783   :type 'hook)
784
785 (defcustom gnus-summary-generate-hook nil
786   "*A hook run just before generating the summary buffer.
787 This hook is commonly used to customize threading variables and the
788 like."
789   :group 'gnus-summary-various
790   :type 'hook)
791
792 (defcustom gnus-select-group-hook nil
793   "*A hook called when a newsgroup is selected.
794
795 If you'd like to simplify subjects like the
796 `gnus-summary-next-same-subject' command does, you can use the
797 following hook:
798
799  (add-hook gnus-select-group-hook
800            (lambda ()
801              (mapcar (lambda (header)
802                        (mail-header-set-subject
803                         header
804                         (gnus-simplify-subject
805                          (mail-header-subject header) 're-only)))
806                      gnus-newsgroup-headers)))"
807   :group 'gnus-group-select
808   :type 'hook)
809
810 (defcustom gnus-select-article-hook nil
811   "*A hook called when an article is selected."
812   :group 'gnus-summary-choose
813   :type 'hook)
814
815 (defcustom gnus-visual-mark-article-hook
816   (list 'gnus-highlight-selected-summary)
817   "*Hook run after selecting an article in the summary buffer.
818 It is meant to be used for highlighting the article in some way.  It
819 is not run if `gnus-visual' is nil."
820   :group 'gnus-summary-visual
821   :type 'hook)
822
823 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
824   "*A hook called before parsing the headers."
825   :group 'gnus-various
826   :type 'hook)
827
828 (defcustom gnus-exit-group-hook nil
829   "*A hook called when exiting summary mode.
830 This hook is not called from the non-updating exit commands like `Q'."
831   :group 'gnus-various
832   :type 'hook)
833
834 (defcustom gnus-summary-update-hook
835   (list 'gnus-summary-highlight-line)
836   "*A hook called when a summary line is changed.
837 The hook will not be called if `gnus-visual' is nil.
838
839 The default function `gnus-summary-highlight-line' will
840 highlight the line according to the `gnus-summary-highlight'
841 variable."
842   :group 'gnus-summary-visual
843   :type 'hook)
844
845 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
846   "*A hook called when an article is selected for the first time.
847 The hook is intended to mark an article as read (or unread)
848 automatically when it is selected."
849   :group 'gnus-summary-choose
850   :type 'hook)
851
852 (defcustom gnus-group-no-more-groups-hook nil
853   "*A hook run when returning to group mode having no more (unread) groups."
854   :group 'gnus-group-select
855   :type 'hook)
856
857 (defcustom gnus-ps-print-hook nil
858   "*A hook run before ps-printing something from Gnus."
859   :group 'gnus-summary
860   :type 'hook)
861
862 (defcustom gnus-summary-display-arrow
863   (and (fboundp 'display-graphic-p)
864        (display-graphic-p))
865   "*If non-nil, display an arrow highlighting the current article."
866   :version "21.1"
867   :group 'gnus-summary
868   :type 'boolean)
869
870 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
871   "Face used for highlighting the current article in the summary buffer."
872   :group 'gnus-summary-visual
873   :type 'face)
874
875 (defcustom gnus-summary-highlight
876   '(((eq mark gnus-canceled-mark)
877      . gnus-summary-cancelled-face)
878     ((and (> score default-high)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-high-ticked-face)
882     ((and (< score default-low)
883           (or (eq mark gnus-dormant-mark)
884               (eq mark gnus-ticked-mark)))
885      . gnus-summary-low-ticked-face)
886     ((or (eq mark gnus-dormant-mark)
887          (eq mark gnus-ticked-mark))
888      . gnus-summary-normal-ticked-face)
889     ((and (> score default-high) (eq mark gnus-ancient-mark))
890      . gnus-summary-high-ancient-face)
891     ((and (< score default-low) (eq mark gnus-ancient-mark))
892      . gnus-summary-low-ancient-face)
893     ((eq mark gnus-ancient-mark)
894      . gnus-summary-normal-ancient-face)
895     ((and (> score default-high) (eq mark gnus-unread-mark))
896      . gnus-summary-high-unread-face)
897     ((and (< score default-low) (eq mark gnus-unread-mark))
898      . gnus-summary-low-unread-face)
899     ((eq mark gnus-unread-mark)
900      . gnus-summary-normal-unread-face)
901     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
902                                                   gnus-undownloaded-mark)))
903      . gnus-summary-high-unread-face)
904     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
905                                                  gnus-undownloaded-mark)))
906      . gnus-summary-low-unread-face)
907     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
908           (memq article gnus-newsgroup-unreads))
909      . gnus-summary-normal-unread-face)
910     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
911      . gnus-summary-normal-read-face)
912     ((> score default-high)
913      . gnus-summary-high-read-face)
914     ((< score default-low)
915      . gnus-summary-low-read-face)
916     (t
917      . gnus-summary-normal-read-face))
918   "*Controls the highlighting of summary buffer lines.
919
920 A list of (FORM . FACE) pairs.  When deciding how a a particular
921 summary line should be displayed, each form is evaluated.  The content
922 of the face field after the first true form is used.  You can change
923 how those summary lines are displayed, by editing the face field.
924
925 You can use the following variables in the FORM field.
926
927 score:        The article's score
928 default:      The default article score.
929 default-high: The default score for high scored articles.
930 default-low:  The default score for low scored articles.
931 below:        The score below which articles are automatically marked as read.
932 mark:         The articles mark."
933   :group 'gnus-summary-visual
934   :type '(repeat (cons (sexp :tag "Form" nil)
935                        face)))
936
937 (defcustom gnus-alter-header-function nil
938   "Function called to allow alteration of article header structures.
939 The function is called with one parameter, the article header vector,
940 which it may alter in any way.")
941
942 (defvar gnus-decode-encoded-word-function
943   (mime-find-field-decoder 'From 'nov)
944   "Variable that says which function should be used to decode a string with encoded words.")
945
946 (defcustom gnus-extra-headers '(To Newsgroups)
947   "*Extra headers to parse."
948   :version "21.1"
949   :group 'gnus-summary
950   :type '(repeat symbol))
951
952 (defcustom gnus-ignored-from-addresses
953   (and user-mail-address (regexp-quote user-mail-address))
954   "*Regexp of From headers that may be suppressed in favor of To headers."
955   :version "21.1"
956   :group 'gnus-summary
957   :type 'regexp)
958
959 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
960   "List of charsets that should be ignored.
961 When these charsets are used in the \"charset\" parameter, the
962 default charset will be used instead."
963   :version "21.1"
964   :type '(repeat symbol)
965   :group 'gnus-charset)
966
967 (gnus-define-group-parameter
968  ignored-charsets
969  :type list
970  :function-document
971  "Return the ignored charsets of GROUP."
972  :variable gnus-group-ignored-charsets-alist
973  :variable-default
974  '(("alt\\.chinese\\.text" iso-8859-1))
975  :variable-document
976  "Alist of regexps (to match group names) and charsets that should be ignored.
977 When these charsets are used in the \"charset\" parameter, the
978 default charset will be used instead."
979  :variable-group gnus-charset
980  :variable-type '(repeat (cons (regexp :tag "Group")
981                                (repeat symbol)))
982  :parameter-type '(choice :tag "Ignored charsets"
983                           :value nil
984                           (repeat (symbol)))
985  :parameter-document       "\
986 List of charsets that should be ignored.
987
988 When these charsets are used in the \"charset\" parameter, the
989 default charset will be used instead.")
990
991 (defcustom gnus-group-highlight-words-alist nil
992   "Alist of group regexps and highlight regexps.
993 This variable uses the same syntax as `gnus-emphasis-alist'."
994   :version "21.1"
995   :type '(repeat (cons (regexp :tag "Group")
996                        (repeat (list (regexp :tag "Highlight regexp")
997                                      (number :tag "Group for entire word" 0)
998                                      (number :tag "Group for displayed part" 0)
999                                      (symbol :tag "Face"
1000                                              gnus-emphasis-highlight-words)))))
1001   :group 'gnus-summary-visual)
1002
1003 (defcustom gnus-use-wheel nil
1004   "Use Intelli-mouse on summary movement"
1005   :type 'boolean
1006   :group 'gnus-summary-maneuvering)
1007
1008 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1009   "Amount to scroll messages by spinning the mouse wheel.
1010 This is actually a cons cell, where the first item is the amount to scroll
1011 on a normal wheel event, and the second is the amount to scroll when the
1012 wheel is moved with the shift key depressed."
1013   :type '(cons (integer :tag "Shift") integer)
1014   :group 'gnus-summary-maneuvering)
1015
1016 (defcustom gnus-wheel-edge-resistance 2
1017   "How hard it should be to change the current article
1018 by moving the mouse over the edge of the article window."
1019   :type 'integer
1020   :group 'gnus-summary-maneuvering)
1021
1022 (defcustom gnus-summary-show-article-charset-alist
1023   nil
1024   "Alist of number and charset.
1025 The article will be shown with the charset corresponding to the
1026 numbered argument.
1027 For example: ((1 . cn-gb-2312) (2 . big5))."
1028   :version "21.1"
1029   :type '(repeat (cons (number :tag "Argument" 1)
1030                        (symbol :tag "Charset")))
1031   :group 'gnus-charset)
1032
1033 (defcustom gnus-preserve-marks t
1034   "Whether marks are preserved when moving, copying and respooling messages."
1035   :version "21.1"
1036   :type 'boolean
1037   :group 'gnus-summary-marks)
1038
1039 (defcustom gnus-alter-articles-to-read-function nil
1040   "Function to be called to alter the list of articles to be selected."
1041   :type '(choice (const nil) function)
1042   :group 'gnus-summary)
1043
1044 (defcustom gnus-orphan-score nil
1045   "*All orphans get this score added.  Set in the score file."
1046   :group 'gnus-score-default
1047   :type '(choice (const nil)
1048                  integer))
1049
1050 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1051   "*A regexp to match MIME parts when saving multiple parts of a message
1052 with gnus-summary-save-parts (X m). This regexp will be used by default
1053 when prompting the user for which type of files to save."
1054   :group 'gnus-summary
1055   :type 'regexp)
1056
1057 (defcustom gnus-read-all-available-headers nil
1058   "Whether Gnus should parse all headers made available to it.
1059 This is mostly relevant for slow backends where the user may
1060 wish to widen the summary buffer to include all headers
1061 that were fetched.  Say, for nnultimate groups."
1062   :group 'gnus-summary
1063   :type '(choice boolean regexp))
1064
1065 (defcustom gnus-summary-muttprint-program "muttprint"
1066   "Command (and optional arguments) used to run Muttprint."
1067   :version "21.3"
1068   :group 'gnus-summary
1069   :type 'string)
1070
1071 (defcustom gnus-article-loose-mime nil
1072   "If non-nil, don't require MIME-Version header.
1073 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1074 supply the MIME-Version header or deliberately strip it From the mail.
1075 Set it to non-nil, Gnus will treat some articles as MIME even if
1076 the MIME-Version header is missed."
1077   :version "21.3"
1078   :type 'boolean
1079   :group 'gnus-article)
1080
1081 ;;; Internal variables
1082
1083 (defvar gnus-summary-display-cache nil)
1084 (defvar gnus-article-mime-handles nil)
1085 (defvar gnus-article-decoded-p nil)
1086 (defvar gnus-article-charset nil)
1087 (defvar gnus-article-ignored-charsets nil)
1088 (defvar gnus-scores-exclude-files nil)
1089 (defvar gnus-page-broken nil)
1090 (defvar gnus-inhibit-mime-unbuttonizing nil)
1091
1092 (defvar gnus-original-article nil)
1093 (defvar gnus-article-internal-prepare-hook nil)
1094 (defvar gnus-newsgroup-process-stack nil)
1095
1096 (defvar gnus-thread-indent-array nil)
1097 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1098 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1099   "Function called to sort the articles within a thread after it has been gathered together.")
1100
1101 (defvar gnus-summary-save-parts-type-history nil)
1102 (defvar gnus-summary-save-parts-last-directory nil)
1103
1104 ;; Avoid highlighting in kill files.
1105 (defvar gnus-summary-inhibit-highlight nil)
1106 (defvar gnus-newsgroup-selected-overlay nil)
1107 (defvar gnus-inhibit-limiting nil)
1108 (defvar gnus-newsgroup-adaptive-score-file nil)
1109 (defvar gnus-current-score-file nil)
1110 (defvar gnus-current-move-group nil)
1111 (defvar gnus-current-copy-group nil)
1112 (defvar gnus-current-crosspost-group nil)
1113 (defvar gnus-newsgroup-display nil)
1114
1115 (defvar gnus-newsgroup-dependencies nil)
1116 (defvar gnus-newsgroup-adaptive nil)
1117 (defvar gnus-summary-display-article-function nil)
1118 (defvar gnus-summary-highlight-line-function nil
1119   "Function called after highlighting a summary line.")
1120
1121 (defvar gnus-summary-line-format-alist
1122   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1123     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1124     (?s gnus-tmp-subject-or-nil ?s)
1125     (?n gnus-tmp-name ?s)
1126     (?A (std11-address-string
1127          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1128     (?a (or (std11-full-name-string
1129              (car (mime-entity-read-field gnus-tmp-header 'From)))
1130             gnus-tmp-from) ?s)
1131     (?F gnus-tmp-from ?s)
1132     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1133     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1134     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1135     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1136     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1137     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1138     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1139     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1140     (?L gnus-tmp-lines ?s)
1141     (?I gnus-tmp-indentation ?s)
1142     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1143     (?R gnus-tmp-replied ?c)
1144     (?\[ gnus-tmp-opening-bracket ?c)
1145     (?\] gnus-tmp-closing-bracket ?c)
1146     (?\> (make-string gnus-tmp-level ? ) ?s)
1147     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1148     (?i gnus-tmp-score ?d)
1149     (?z gnus-tmp-score-char ?c)
1150     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1151     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1152     (?U gnus-tmp-unread ?c)
1153     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1154         ?s)
1155     (?t (gnus-summary-number-of-articles-in-thread
1156          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1157         ?d)
1158     (?e (gnus-summary-number-of-articles-in-thread
1159          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1160         ?c)
1161     (?u gnus-tmp-user-defined ?s)
1162     (?P (gnus-pick-line-number) ?d)
1163     (?B gnus-tmp-thread-tree-header-string ?s)
1164     (user-date (gnus-user-date
1165                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1166   "An alist of format specifications that can appear in summary lines.
1167 These are paired with what variables they correspond with, along with
1168 the type of the variable (string, integer, character, etc).")
1169
1170 (defvar gnus-summary-dummy-line-format-alist
1171   `((?S gnus-tmp-subject ?s)
1172     (?N gnus-tmp-number ?d)
1173     (?u gnus-tmp-user-defined ?s)))
1174
1175 (defvar gnus-summary-mode-line-format-alist
1176   `((?G gnus-tmp-group-name ?s)
1177     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1178     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1179     (?A gnus-tmp-article-number ?d)
1180     (?Z gnus-tmp-unread-and-unselected ?s)
1181     (?V gnus-version ?s)
1182     (?U gnus-tmp-unread-and-unticked ?d)
1183     (?S gnus-tmp-subject ?s)
1184     (?e gnus-tmp-unselected ?d)
1185     (?u gnus-tmp-user-defined ?s)
1186     (?d (length gnus-newsgroup-dormant) ?d)
1187     (?t (length gnus-newsgroup-marked) ?d)
1188     (?h (length gnus-newsgroup-spam-marked) ?d)
1189     (?r (length gnus-newsgroup-reads) ?d)
1190     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1191     (?E gnus-newsgroup-expunged-tally ?d)
1192     (?s (gnus-current-score-file-nondirectory) ?s)))
1193
1194 (defvar gnus-last-search-regexp nil
1195   "Default regexp for article search command.")
1196
1197 (defvar gnus-summary-search-article-matched-data nil
1198   "Last matched data of article search command.  It is the local variable
1199 in `gnus-article-buffer' which consists of the list of start position,
1200 end position and text.")
1201
1202 (defvar gnus-last-shell-command nil
1203   "Default shell command on article.")
1204
1205 (defvar gnus-newsgroup-begin nil)
1206 (defvar gnus-newsgroup-end nil)
1207 (defvar gnus-newsgroup-last-rmail nil)
1208 (defvar gnus-newsgroup-last-mail nil)
1209 (defvar gnus-newsgroup-last-folder nil)
1210 (defvar gnus-newsgroup-last-file nil)
1211 (defvar gnus-newsgroup-auto-expire nil)
1212 (defvar gnus-newsgroup-active nil)
1213
1214 (defvar gnus-newsgroup-data nil)
1215 (defvar gnus-newsgroup-data-reverse nil)
1216 (defvar gnus-newsgroup-limit nil)
1217 (defvar gnus-newsgroup-limits nil)
1218
1219 (defvar gnus-newsgroup-unreads nil
1220   "Sorted list of unread articles in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-unselected nil
1223   "Sorted list of unselected unread articles in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-reads nil
1226   "Alist of read articles and article marks in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-expunged-tally nil)
1229
1230 (defvar gnus-newsgroup-marked nil
1231   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1232
1233 (defvar gnus-newsgroup-spam-marked nil
1234   "List of ranges of articles that have been marked as spam.")
1235
1236 (defvar gnus-newsgroup-killed nil
1237   "List of ranges of articles that have been through the scoring process.")
1238
1239 (defvar gnus-newsgroup-cached nil
1240   "Sorted list of articles that come from the article cache.")
1241
1242 (defvar gnus-newsgroup-saved nil
1243   "List of articles that have been saved.")
1244
1245 (defvar gnus-newsgroup-kill-headers nil)
1246
1247 (defvar gnus-newsgroup-replied nil
1248   "List of articles that have been replied to in the current newsgroup.")
1249
1250 (defvar gnus-newsgroup-forwarded nil
1251   "List of articles that have been forwarded in the current newsgroup.")
1252
1253 (defvar gnus-newsgroup-recent nil
1254   "List of articles that have are recent in the current newsgroup.")
1255
1256 (defvar gnus-newsgroup-expirable nil
1257   "Sorted list of articles in the current newsgroup that can be expired.")
1258
1259 (defvar gnus-newsgroup-processable nil
1260   "List of articles in the current newsgroup that can be processed.")
1261
1262 (defvar gnus-newsgroup-downloadable nil
1263   "Sorted list of articles in the current newsgroup that can be processed.")
1264
1265 (defvar gnus-newsgroup-undownloaded nil
1266   "List of articles in the current newsgroup that haven't been downloaded..")
1267
1268 (defvar gnus-newsgroup-unsendable nil
1269   "List of articles in the current newsgroup that won't be sent.")
1270
1271 (defvar gnus-newsgroup-bookmarks nil
1272   "List of articles in the current newsgroup that have bookmarks.")
1273
1274 (defvar gnus-newsgroup-dormant nil
1275   "Sorted list of dormant articles in the current newsgroup.")
1276
1277 (defvar gnus-newsgroup-unseen nil
1278   "List of unseen articles in the current newsgroup.")
1279
1280 (defvar gnus-newsgroup-seen nil
1281   "Range of seen articles in the current newsgroup.")
1282
1283 (defvar gnus-newsgroup-articles nil
1284   "List of articles in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-scored nil
1287   "List of scored articles in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-incorporated nil
1290   "List of incorporated articles in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-headers nil
1293   "List of article headers in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-threads nil)
1296
1297 (defvar gnus-newsgroup-prepared nil
1298   "Whether the current group has been prepared properly.")
1299
1300 (defvar gnus-newsgroup-ancient nil
1301   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-sparse nil)
1304
1305 (defvar gnus-current-article nil)
1306 (defvar gnus-article-current nil)
1307 (defvar gnus-current-headers nil)
1308 (defvar gnus-have-all-headers nil)
1309 (defvar gnus-last-article nil)
1310 (defvar gnus-newsgroup-history nil)
1311 (defvar gnus-newsgroup-charset nil)
1312 (defvar gnus-newsgroup-ephemeral-charset nil)
1313 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1314
1315 (defvar gnus-article-before-search nil)
1316
1317 (defconst gnus-summary-local-variables
1318   '(gnus-newsgroup-name
1319     gnus-newsgroup-begin gnus-newsgroup-end
1320     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1321     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1322     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1323     gnus-newsgroup-unselected gnus-newsgroup-marked
1324     gnus-newsgroup-spam-marked
1325     gnus-newsgroup-reads gnus-newsgroup-saved
1326     gnus-newsgroup-replied gnus-newsgroup-forwarded
1327     gnus-newsgroup-recent
1328     gnus-newsgroup-expirable
1329     gnus-newsgroup-processable gnus-newsgroup-killed
1330     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1331     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1332     gnus-newsgroup-seen gnus-newsgroup-articles
1333     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1334     gnus-newsgroup-headers gnus-newsgroup-threads
1335     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1336     gnus-current-article gnus-current-headers gnus-have-all-headers
1337     gnus-last-article gnus-article-internal-prepare-hook
1338     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1339     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1340     gnus-thread-expunge-below
1341     gnus-score-alist gnus-current-score-file
1342     (gnus-summary-expunge-below . global)
1343     (gnus-summary-mark-below . global)
1344     (gnus-orphan-score . global)
1345     gnus-newsgroup-active gnus-scores-exclude-files
1346     gnus-newsgroup-history gnus-newsgroup-ancient
1347     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1348     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1349     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1350     (gnus-newsgroup-expunged-tally . 0)
1351     gnus-cache-removable-articles gnus-newsgroup-cached
1352     gnus-newsgroup-data gnus-newsgroup-data-reverse
1353     gnus-newsgroup-limit gnus-newsgroup-limits
1354     gnus-newsgroup-charset gnus-newsgroup-display
1355     gnus-newsgroup-incorporated)
1356   "Variables that are buffer-local to the summary buffers.")
1357
1358 (defvar gnus-newsgroup-variables nil
1359   "A list of variables that have separate values in different newsgroups.
1360 A list of newsgroup (summary buffer) local variables, or cons of
1361 variables and their default values (when the default values are not
1362 nil), that should be made global while the summary buffer is active.
1363 These variables can be used to set variables in the group parameters
1364 while still allowing them to affect operations done in other
1365 buffers. For example:
1366
1367 \(setq gnus-newsgroup-variables
1368      '(message-use-followup-to
1369        (gnus-visible-headers .
1370          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1371 ")
1372
1373 ;; Byte-compiler warning.
1374 (eval-when-compile (defvar gnus-article-mode-map))
1375
1376 ;; Subject simplification.
1377
1378 (defun gnus-simplify-whitespace (str)
1379   "Remove excessive whitespace from STR."
1380   (let ((mystr str))
1381     ;; Multiple spaces.
1382     (while (string-match "[ \t][ \t]+" mystr)
1383       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1384                           " "
1385                           (substring mystr (match-end 0)))))
1386     ;; Leading spaces.
1387     (when (string-match "^[ \t]+" mystr)
1388       (setq mystr (substring mystr (match-end 0))))
1389     ;; Trailing spaces.
1390     (when (string-match "[ \t]+$" mystr)
1391       (setq mystr (substring mystr 0 (match-beginning 0))))
1392     mystr))
1393
1394 (defun gnus-simplify-all-whitespace (str)
1395   "Remove all whitespace from STR."
1396   (let ((mystr str))
1397     (while (string-match "[ \t\n]+" mystr)
1398       (setq mystr (replace-match "" nil nil mystr)))
1399     mystr))
1400
1401 (defsubst gnus-simplify-subject-re (subject)
1402   "Remove \"Re:\" from subject lines."
1403   (if (string-match message-subject-re-regexp subject)
1404       (substring subject (match-end 0))
1405     subject))
1406
1407 (defun gnus-simplify-subject (subject &optional re-only)
1408   "Remove `Re:' and words in parentheses.
1409 If RE-ONLY is non-nil, strip leading `Re:'s only."
1410   (let ((case-fold-search t))           ;Ignore case.
1411     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1412     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1413       (setq subject (substring subject (match-end 0))))
1414     ;; Remove uninteresting prefixes.
1415     (when (and (not re-only)
1416                gnus-simplify-ignored-prefixes
1417                (string-match gnus-simplify-ignored-prefixes subject))
1418       (setq subject (substring subject (match-end 0))))
1419     ;; Remove words in parentheses from end.
1420     (unless re-only
1421       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1422         (setq subject (substring subject 0 (match-beginning 0)))))
1423     ;; Return subject string.
1424     subject))
1425
1426 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1427 ;; all whitespace.
1428 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1429   (goto-char (point-min))
1430   (while (re-search-forward regexp nil t)
1431     (replace-match (or newtext ""))))
1432
1433 (defun gnus-simplify-buffer-fuzzy ()
1434   "Simplify string in the buffer fuzzily.
1435 The string in the accessible portion of the current buffer is simplified.
1436 It is assumed to be a single-line subject.
1437 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1438 matter is removed.  Additional things can be deleted by setting
1439 `gnus-simplify-subject-fuzzy-regexp'."
1440   (let ((case-fold-search t)
1441         (modified-tick))
1442     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1443
1444     (while (not (eq modified-tick (buffer-modified-tick)))
1445       (setq modified-tick (buffer-modified-tick))
1446       (cond
1447        ((listp gnus-simplify-subject-fuzzy-regexp)
1448         (mapcar 'gnus-simplify-buffer-fuzzy-step
1449                 gnus-simplify-subject-fuzzy-regexp))
1450        (gnus-simplify-subject-fuzzy-regexp
1451         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1452       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1453       (gnus-simplify-buffer-fuzzy-step
1454        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1455       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1456
1457     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1458     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1459     (gnus-simplify-buffer-fuzzy-step " $")
1460     (gnus-simplify-buffer-fuzzy-step "^ +")))
1461
1462 (defun gnus-simplify-subject-fuzzy (subject)
1463   "Simplify a subject string fuzzily.
1464 See `gnus-simplify-buffer-fuzzy' for details."
1465   (save-excursion
1466     (gnus-set-work-buffer)
1467     (let ((case-fold-search t))
1468       ;; Remove uninteresting prefixes.
1469       (when (and gnus-simplify-ignored-prefixes
1470                  (string-match gnus-simplify-ignored-prefixes subject))
1471         (setq subject (substring subject (match-end 0))))
1472       (insert subject)
1473       (inline (gnus-simplify-buffer-fuzzy))
1474       (buffer-string))))
1475
1476 (defsubst gnus-simplify-subject-fully (subject)
1477   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1478   (cond
1479    (gnus-simplify-subject-functions
1480     (gnus-map-function gnus-simplify-subject-functions subject))
1481    ((null gnus-summary-gather-subject-limit)
1482     (gnus-simplify-subject-re subject))
1483    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1484     (gnus-simplify-subject-fuzzy subject))
1485    ((numberp gnus-summary-gather-subject-limit)
1486     (gnus-limit-string (gnus-simplify-subject-re subject)
1487                        gnus-summary-gather-subject-limit))
1488    (t
1489     subject)))
1490
1491 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1492   "Check whether two subjects are equal.
1493 If optional argument simple-first is t, first argument is already
1494 simplified."
1495   (cond
1496    ((null simple-first)
1497     (equal (gnus-simplify-subject-fully s1)
1498            (gnus-simplify-subject-fully s2)))
1499    (t
1500     (equal s1
1501            (gnus-simplify-subject-fully s2)))))
1502
1503 (defun gnus-summary-bubble-group ()
1504   "Increase the score of the current group.
1505 This is a handy function to add to `gnus-summary-exit-hook' to
1506 increase the score of each group you read."
1507   (gnus-group-add-score gnus-newsgroup-name))
1508
1509 \f
1510 ;;;
1511 ;;; Gnus summary mode
1512 ;;;
1513
1514 (put 'gnus-summary-mode 'mode-class 'special)
1515
1516 (defvar gnus-article-commands-menu)
1517
1518 (when t
1519   ;; Non-orthogonal keys
1520
1521   (gnus-define-keys gnus-summary-mode-map
1522     " " gnus-summary-next-page
1523     "\177" gnus-summary-prev-page
1524     [delete] gnus-summary-prev-page
1525     [backspace] gnus-summary-prev-page
1526     "\r" gnus-summary-scroll-up
1527     "\M-\r" gnus-summary-scroll-down
1528     "n" gnus-summary-next-unread-article
1529     "p" gnus-summary-prev-unread-article
1530     "N" gnus-summary-next-article
1531     "P" gnus-summary-prev-article
1532     "\M-\C-n" gnus-summary-next-same-subject
1533     "\M-\C-p" gnus-summary-prev-same-subject
1534     "\M-n" gnus-summary-next-unread-subject
1535     "\M-p" gnus-summary-prev-unread-subject
1536     "." gnus-summary-first-unread-article
1537     "," gnus-summary-best-unread-article
1538     "\M-s" gnus-summary-search-article-forward
1539     "\M-r" gnus-summary-search-article-backward
1540     "<" gnus-summary-beginning-of-article
1541     ">" gnus-summary-end-of-article
1542     "j" gnus-summary-goto-article
1543     "^" gnus-summary-refer-parent-article
1544     "\M-^" gnus-summary-refer-article
1545     "u" gnus-summary-tick-article-forward
1546     "!" gnus-summary-tick-article-forward
1547     "U" gnus-summary-tick-article-backward
1548     "d" gnus-summary-mark-as-read-forward
1549     "D" gnus-summary-mark-as-read-backward
1550     "E" gnus-summary-mark-as-expirable
1551     "\M-u" gnus-summary-clear-mark-forward
1552     "\M-U" gnus-summary-clear-mark-backward
1553     "k" gnus-summary-kill-same-subject-and-select
1554     "\C-k" gnus-summary-kill-same-subject
1555     "\M-\C-k" gnus-summary-kill-thread
1556     "\M-\C-l" gnus-summary-lower-thread
1557     "e" gnus-summary-edit-article
1558     "#" gnus-summary-mark-as-processable
1559     "\M-#" gnus-summary-unmark-as-processable
1560     "\M-\C-t" gnus-summary-toggle-threads
1561     "\M-\C-s" gnus-summary-show-thread
1562     "\M-\C-h" gnus-summary-hide-thread
1563     "\M-\C-f" gnus-summary-next-thread
1564     "\M-\C-b" gnus-summary-prev-thread
1565     [(meta down)] gnus-summary-next-thread
1566     [(meta up)] gnus-summary-prev-thread
1567     "\M-\C-u" gnus-summary-up-thread
1568     "\M-\C-d" gnus-summary-down-thread
1569     "&" gnus-summary-execute-command
1570     "c" gnus-summary-catchup-and-exit
1571     "\C-w" gnus-summary-mark-region-as-read
1572     "\C-t" gnus-summary-toggle-truncation
1573     "?" gnus-summary-mark-as-dormant
1574     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1575     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1576     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1577     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1578     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1579     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1580     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1581     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1582     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1583     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1584     "=" gnus-summary-expand-window
1585     "\C-x\C-s" gnus-summary-reselect-current-group
1586     "\M-g" gnus-summary-rescan-group
1587     "w" gnus-summary-stop-page-breaking
1588     "\C-c\C-r" gnus-summary-caesar-message
1589     "\M-t" gnus-summary-toggle-mime
1590     "f" gnus-summary-followup
1591     "F" gnus-summary-followup-with-original
1592     "C" gnus-summary-cancel-article
1593     "r" gnus-summary-reply
1594     "R" gnus-summary-reply-with-original
1595     "\C-c\C-f" gnus-summary-mail-forward
1596     "o" gnus-summary-save-article
1597     "\C-o" gnus-summary-save-article-mail
1598     "|" gnus-summary-pipe-output
1599     "\M-k" gnus-summary-edit-local-kill
1600     "\M-K" gnus-summary-edit-global-kill
1601     ;; "V" gnus-version
1602     "\C-c\C-d" gnus-summary-describe-group
1603     "q" gnus-summary-exit
1604     "Q" gnus-summary-exit-no-update
1605     "\C-c\C-i" gnus-info-find-node
1606     gnus-mouse-2 gnus-mouse-pick-article
1607     "m" gnus-summary-mail-other-window
1608     "a" gnus-summary-post-news
1609     "i" gnus-summary-news-other-window
1610     "x" gnus-summary-limit-to-unread
1611     "s" gnus-summary-isearch-article
1612     "t" gnus-summary-toggle-header
1613     "g" gnus-summary-show-article
1614     "l" gnus-summary-goto-last-article
1615     "v" gnus-summary-preview-mime-message
1616     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1617     "\C-d" gnus-summary-enter-digest-group
1618     "\M-\C-d" gnus-summary-read-document
1619     "\M-\C-e" gnus-summary-edit-parameters
1620     "\M-\C-a" gnus-summary-customize-parameters
1621     "\C-c\C-b" gnus-bug
1622     "\C-c\C-n" gnus-namazu-search
1623     "*" gnus-cache-enter-article
1624     "\M-*" gnus-cache-remove-article
1625     "\M-&" gnus-summary-universal-argument
1626     "\C-l" gnus-recenter
1627     "I" gnus-summary-increase-score
1628     "L" gnus-summary-lower-score
1629     "\M-i" gnus-symbolic-argument
1630     "h" gnus-summary-select-article-buffer
1631
1632     "V" gnus-summary-score-map
1633     "X" gnus-uu-extract-map
1634     "S" gnus-summary-send-map)
1635
1636   ;; Sort of orthogonal keymap
1637   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1638     "t" gnus-summary-tick-article-forward
1639     "!" gnus-summary-tick-article-forward
1640     "d" gnus-summary-mark-as-read-forward
1641     "r" gnus-summary-mark-as-read-forward
1642     "c" gnus-summary-clear-mark-forward
1643     " " gnus-summary-clear-mark-forward
1644     "e" gnus-summary-mark-as-expirable
1645     "x" gnus-summary-mark-as-expirable
1646     "?" gnus-summary-mark-as-dormant
1647     "b" gnus-summary-set-bookmark
1648     "B" gnus-summary-remove-bookmark
1649     "#" gnus-summary-mark-as-processable
1650     "\M-#" gnus-summary-unmark-as-processable
1651     "S" gnus-summary-limit-include-expunged
1652     "C" gnus-summary-catchup
1653     "H" gnus-summary-catchup-to-here
1654     "h" gnus-summary-catchup-from-here
1655     "\C-c" gnus-summary-catchup-all
1656     "k" gnus-summary-kill-same-subject-and-select
1657     "K" gnus-summary-kill-same-subject
1658     "P" gnus-uu-mark-map)
1659
1660   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1661     "c" gnus-summary-clear-above
1662     "u" gnus-summary-tick-above
1663     "m" gnus-summary-mark-above
1664     "k" gnus-summary-kill-below)
1665
1666   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1667     "/" gnus-summary-limit-to-subject
1668     "n" gnus-summary-limit-to-articles
1669     "w" gnus-summary-pop-limit
1670     "s" gnus-summary-limit-to-subject
1671     "a" gnus-summary-limit-to-author
1672     "u" gnus-summary-limit-to-unread
1673     "m" gnus-summary-limit-to-marks
1674     "M" gnus-summary-limit-exclude-marks
1675     "v" gnus-summary-limit-to-score
1676     "*" gnus-summary-limit-include-cached
1677     "D" gnus-summary-limit-include-dormant
1678     "T" gnus-summary-limit-include-thread
1679     "d" gnus-summary-limit-exclude-dormant
1680     "t" gnus-summary-limit-to-age
1681     "." gnus-summary-limit-to-unseen
1682     "x" gnus-summary-limit-to-extra
1683     "p" gnus-summary-limit-to-display-predicate
1684     "E" gnus-summary-limit-include-expunged
1685     "c" gnus-summary-limit-exclude-childless-dormant
1686     "C" gnus-summary-limit-mark-excluded-as-read
1687     "o" gnus-summary-insert-old-articles
1688     "N" gnus-summary-insert-new-articles)
1689
1690   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1691     "n" gnus-summary-next-unread-article
1692     "p" gnus-summary-prev-unread-article
1693     "N" gnus-summary-next-article
1694     "P" gnus-summary-prev-article
1695     "\C-n" gnus-summary-next-same-subject
1696     "\C-p" gnus-summary-prev-same-subject
1697     "\M-n" gnus-summary-next-unread-subject
1698     "\M-p" gnus-summary-prev-unread-subject
1699     "f" gnus-summary-first-unread-article
1700     "b" gnus-summary-best-unread-article
1701     "j" gnus-summary-goto-article
1702     "g" gnus-summary-goto-subject
1703     "l" gnus-summary-goto-last-article
1704     "o" gnus-summary-pop-article)
1705
1706   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1707     "k" gnus-summary-kill-thread
1708     "l" gnus-summary-lower-thread
1709     "i" gnus-summary-raise-thread
1710     "T" gnus-summary-toggle-threads
1711     "t" gnus-summary-rethread-current
1712     "^" gnus-summary-reparent-thread
1713     "s" gnus-summary-show-thread
1714     "S" gnus-summary-show-all-threads
1715     "h" gnus-summary-hide-thread
1716     "H" gnus-summary-hide-all-threads
1717     "n" gnus-summary-next-thread
1718     "p" gnus-summary-prev-thread
1719     "u" gnus-summary-up-thread
1720     "o" gnus-summary-top-thread
1721     "d" gnus-summary-down-thread
1722     "#" gnus-uu-mark-thread
1723     "\M-#" gnus-uu-unmark-thread)
1724
1725   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1726     "g" gnus-summary-prepare
1727     "c" gnus-summary-insert-cached-articles)
1728
1729   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1730     "c" gnus-summary-catchup-and-exit
1731     "C" gnus-summary-catchup-all-and-exit
1732     "E" gnus-summary-exit-no-update
1733     "J" gnus-summary-jump-to-other-group
1734     "Q" gnus-summary-exit
1735     "Z" gnus-summary-exit
1736     "n" gnus-summary-catchup-and-goto-next-group
1737     "R" gnus-summary-reselect-current-group
1738     "G" gnus-summary-rescan-group
1739     "N" gnus-summary-next-group
1740     "s" gnus-summary-save-newsrc
1741     "P" gnus-summary-prev-group)
1742
1743   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1744     " " gnus-summary-next-page
1745     "n" gnus-summary-next-page
1746     "\177" gnus-summary-prev-page
1747     [delete] gnus-summary-prev-page
1748     "p" gnus-summary-prev-page
1749     "\r" gnus-summary-scroll-up
1750     "\M-\r" gnus-summary-scroll-down
1751     "<" gnus-summary-beginning-of-article
1752     ">" gnus-summary-end-of-article
1753     "b" gnus-summary-beginning-of-article
1754     "e" gnus-summary-end-of-article
1755     "^" gnus-summary-refer-parent-article
1756     "r" gnus-summary-refer-parent-article
1757     "D" gnus-summary-enter-digest-group
1758     "R" gnus-summary-refer-references
1759     "T" gnus-summary-refer-thread
1760     "g" gnus-summary-show-article
1761     "s" gnus-summary-isearch-article
1762     "P" gnus-summary-print-article
1763     "M" gnus-mailing-list-insinuate
1764     "t" gnus-article-babel)
1765
1766   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1767     "b" gnus-article-add-buttons
1768     "B" gnus-article-add-buttons-to-head
1769     "o" gnus-article-treat-overstrike
1770     "e" gnus-article-emphasize
1771     "w" gnus-article-fill-cited-article
1772     "Q" gnus-article-fill-long-lines
1773     "C" gnus-article-capitalize-sentences
1774     "c" gnus-article-remove-cr
1775     "Z" gnus-article-decode-HZ
1776     "h" gnus-article-wash-html
1777     "u" gnus-article-unsplit-urls
1778     "f" gnus-article-display-x-face
1779     "l" gnus-summary-stop-page-breaking
1780     "r" gnus-summary-caesar-message
1781     "t" gnus-summary-toggle-header
1782     "g" gnus-treat-smiley
1783     "v" gnus-summary-verbose-headers
1784     "m" gnus-summary-toggle-mime
1785     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1786     "p" gnus-article-verify-x-pgp-sig
1787     "d" gnus-article-treat-dumbquotes
1788     "k" gnus-article-outlook-deuglify-article)
1789
1790   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1791     "a" gnus-article-hide
1792     "h" gnus-article-hide-headers
1793     "b" gnus-article-hide-boring-headers
1794     "s" gnus-article-hide-signature
1795     "c" gnus-article-hide-citation
1796     "C" gnus-article-hide-citation-in-followups
1797     "l" gnus-article-hide-list-identifiers
1798     "p" gnus-article-hide-pgp
1799     "B" gnus-article-strip-banner
1800     "P" gnus-article-hide-pem
1801     "\C-c" gnus-article-hide-citation-maybe)
1802
1803   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1804     "a" gnus-article-highlight
1805     "h" gnus-article-highlight-headers
1806     "c" gnus-article-highlight-citation
1807     "s" gnus-article-highlight-signature)
1808
1809   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1810     "f" gnus-article-treat-fold-headers
1811     "u" gnus-article-treat-unfold-headers
1812     "n" gnus-article-treat-fold-newsgroups)
1813
1814   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1815     "x" gnus-article-display-x-face
1816     "s" gnus-treat-smiley
1817     "D" gnus-article-remove-images
1818     "f" gnus-treat-from-picon
1819     "m" gnus-treat-mail-picon
1820     "n" gnus-treat-newsgroups-picon)
1821
1822   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1823     "z" gnus-article-date-ut
1824     "u" gnus-article-date-ut
1825     "l" gnus-article-date-local
1826     "p" gnus-article-date-english
1827     "e" gnus-article-date-lapsed
1828     "o" gnus-article-date-original
1829     "i" gnus-article-date-iso8601
1830     "s" gnus-article-date-user)
1831
1832   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1833     "t" gnus-article-remove-trailing-blank-lines
1834     "l" gnus-article-strip-leading-blank-lines
1835     "m" gnus-article-strip-multiple-blank-lines
1836     "a" gnus-article-strip-blank-lines
1837     "A" gnus-article-strip-all-blank-lines
1838     "s" gnus-article-strip-leading-space
1839     "e" gnus-article-strip-trailing-space
1840     "w" gnus-article-remove-leading-whitespace)
1841
1842   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1843     "v" gnus-version
1844     "f" gnus-summary-fetch-faq
1845     "d" gnus-summary-describe-group
1846     "h" gnus-summary-describe-briefly
1847     "i" gnus-info-find-node)
1848
1849   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1850     "e" gnus-summary-expire-articles
1851     "\M-\C-e" gnus-summary-expire-articles-now
1852     "\177" gnus-summary-delete-article
1853     [delete] gnus-summary-delete-article
1854     [backspace] gnus-summary-delete-article
1855     "m" gnus-summary-move-article
1856     "r" gnus-summary-respool-article
1857     "w" gnus-summary-edit-article
1858     "c" gnus-summary-copy-article
1859     "B" gnus-summary-crosspost-article
1860     "q" gnus-summary-respool-query
1861     "t" gnus-summary-respool-trace
1862     "i" gnus-summary-import-article
1863     "I" gnus-summary-create-article
1864     "p" gnus-summary-article-posted-p)
1865
1866   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1867     "o" gnus-summary-save-article
1868     "m" gnus-summary-save-article-mail
1869     "F" gnus-summary-write-article-file
1870     "r" gnus-summary-save-article-rmail
1871     "f" gnus-summary-save-article-file
1872     "b" gnus-summary-save-article-body-file
1873     "h" gnus-summary-save-article-folder
1874     "v" gnus-summary-save-article-vm
1875     "p" gnus-summary-pipe-output
1876     "P" gnus-summary-muttprint
1877     "s" gnus-soup-add-article)
1878
1879   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1880     "b" gnus-summary-display-buttonized
1881     "m" gnus-summary-repair-multipart
1882     "v" gnus-article-view-part
1883     "o" gnus-article-save-part
1884     "c" gnus-article-copy-part
1885     "C" gnus-article-view-part-as-charset
1886     "e" gnus-article-view-part-externally
1887     "E" gnus-article-encrypt-body
1888     "i" gnus-article-inline-part
1889     "|" gnus-article-pipe-part)
1890
1891   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1892     "p" gnus-summary-mark-as-processable
1893     "u" gnus-summary-unmark-as-processable
1894     "U" gnus-summary-unmark-all-processable
1895     "v" gnus-uu-mark-over
1896     "s" gnus-uu-mark-series
1897     "r" gnus-uu-mark-region
1898     "g" gnus-uu-unmark-region
1899     "R" gnus-uu-mark-by-regexp
1900     "G" gnus-uu-unmark-by-regexp
1901     "t" gnus-uu-mark-thread
1902     "T" gnus-uu-unmark-thread
1903     "a" gnus-uu-mark-all
1904     "b" gnus-uu-mark-buffer
1905     "S" gnus-uu-mark-sparse
1906     "k" gnus-summary-kill-process-mark
1907     "y" gnus-summary-yank-process-mark
1908     "w" gnus-summary-save-process-mark
1909     "i" gnus-uu-invert-processable)
1910
1911   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1912     ;;"x" gnus-uu-extract-any
1913     "m" gnus-summary-save-parts
1914     "u" gnus-uu-decode-uu
1915     "U" gnus-uu-decode-uu-and-save
1916     "s" gnus-uu-decode-unshar
1917     "S" gnus-uu-decode-unshar-and-save
1918     "o" gnus-uu-decode-save
1919     "O" gnus-uu-decode-save
1920     "b" gnus-uu-decode-binhex
1921     "B" gnus-uu-decode-binhex
1922     "p" gnus-uu-decode-postscript
1923     "P" gnus-uu-decode-postscript-and-save)
1924
1925   (gnus-define-keys
1926       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1927     "u" gnus-uu-decode-uu-view
1928     "U" gnus-uu-decode-uu-and-save-view
1929     "s" gnus-uu-decode-unshar-view
1930     "S" gnus-uu-decode-unshar-and-save-view
1931     "o" gnus-uu-decode-save-view
1932     "O" gnus-uu-decode-save-view
1933     "b" gnus-uu-decode-binhex-view
1934     "B" gnus-uu-decode-binhex-view
1935     "p" gnus-uu-decode-postscript-view
1936     "P" gnus-uu-decode-postscript-and-save-view))
1937
1938 (defvar gnus-article-post-menu nil)
1939
1940 (defconst gnus-summary-menu-maxlen 20)
1941
1942 (defun gnus-summary-menu-split (menu)
1943   ;; If we have lots of elements, divide them into groups of 20
1944   ;; and make a pane (or submenu) for each one.
1945   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1946       (let ((menu menu) sublists next
1947             (i 1))
1948         (while menu
1949           ;; Pull off the next gnus-summary-menu-maxlen elements
1950           ;; and make them the next element of sublist.
1951           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1952           (if next
1953               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1954                       nil))
1955           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1956                                              (aref (car (last menu)) 0)) menu)
1957                                sublists))
1958           (setq i (1+ i))
1959           (setq menu next))
1960         (nreverse sublists))
1961     ;; Few elements--put them all in one pane.
1962     menu))
1963
1964 (defun gnus-summary-make-menu-bar ()
1965   (gnus-turn-off-edit-menu 'summary)
1966
1967   (unless (boundp 'gnus-summary-misc-menu)
1968
1969     (easy-menu-define
1970      gnus-summary-kill-menu gnus-summary-mode-map ""
1971      (cons
1972       "Score"
1973       (nconc
1974        (list
1975         ["Customize" gnus-score-customize t])
1976        (gnus-make-score-map 'increase)
1977        (gnus-make-score-map 'lower)
1978        '(("Mark"
1979           ["Kill below" gnus-summary-kill-below t]
1980           ["Mark above" gnus-summary-mark-above t]
1981           ["Tick above" gnus-summary-tick-above t]
1982           ["Clear above" gnus-summary-clear-above t])
1983          ["Current score" gnus-summary-current-score t]
1984          ["Set score" gnus-summary-set-score t]
1985          ["Switch current score file..." gnus-score-change-score-file t]
1986          ["Set mark below..." gnus-score-set-mark-below t]
1987          ["Set expunge below..." gnus-score-set-expunge-below t]
1988          ["Edit current score file" gnus-score-edit-current-scores t]
1989          ["Edit score file" gnus-score-edit-file t]
1990          ["Trace score" gnus-score-find-trace t]
1991          ["Find words" gnus-score-find-favourite-words t]
1992          ["Rescore buffer" gnus-summary-rescore t]
1993          ["Increase score..." gnus-summary-increase-score t]
1994          ["Lower score..." gnus-summary-lower-score t]))))
1995
1996     ;; Define both the Article menu in the summary buffer and the
1997     ;; equivalent Commands menu in the article buffer here for
1998     ;; consistency.
1999     (let ((innards
2000            `(("Hide"
2001               ["All" gnus-article-hide t]
2002               ["Headers" gnus-article-hide-headers t]
2003               ["Signature" gnus-article-hide-signature t]
2004               ["Citation" gnus-article-hide-citation t]
2005               ["List identifiers" gnus-article-hide-list-identifiers t]
2006               ["PGP" gnus-article-hide-pgp t]
2007               ["Banner" gnus-article-strip-banner t]
2008               ["Boring headers" gnus-article-hide-boring-headers t])
2009              ("Highlight"
2010               ["All" gnus-article-highlight t]
2011               ["Headers" gnus-article-highlight-headers t]
2012               ["Signature" gnus-article-highlight-signature t]
2013               ["Citation" gnus-article-highlight-citation t])
2014              ("Date"
2015               ["Local" gnus-article-date-local t]
2016               ["ISO8601" gnus-article-date-iso8601 t]
2017               ["UT" gnus-article-date-ut t]
2018               ["Original" gnus-article-date-original t]
2019               ["Lapsed" gnus-article-date-lapsed t]
2020               ["User-defined" gnus-article-date-user t])
2021              ("Display"
2022               ["Remove images" gnus-article-remove-images t]
2023               ["Toggle smiley" gnus-treat-smiley t]
2024               ["Show X-Face" gnus-article-display-x-face t]
2025               ["Show picons in From" gnus-treat-from-picon t]
2026               ["Show picons in mail headers" gnus-treat-mail-picon t]
2027               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2028               ("View as different encoding"
2029                ,@(gnus-summary-menu-split
2030                   (mapcar
2031                    (lambda (cs)
2032                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2033                      ;; forms for menu commands, we should provide intern'ed
2034                      ;; function symbols.
2035                      (let ((command (intern (format "\
2036 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2037                        (fset command
2038                              `(lambda ()
2039                                 (interactive)
2040                                 (let ((gnus-summary-show-article-charset-alist
2041                                        '((1 . ,cs))))
2042                                   (gnus-summary-show-article 1))))
2043                        `[,(symbol-name cs) ,command t]))
2044                    (sort (if (fboundp 'coding-system-list)
2045                              (coding-system-list)
2046                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2047                            )
2048                          (lambda (a b)
2049                            (string< (symbol-name a)
2050                                     (symbol-name b))))))))
2051              ("Washing"
2052               ("Remove Blanks"
2053                ["Leading" gnus-article-strip-leading-blank-lines t]
2054                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2055                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2056                ["All of the above" gnus-article-strip-blank-lines t]
2057                ["All" gnus-article-strip-all-blank-lines t]
2058                ["Leading space" gnus-article-strip-leading-space t]
2059                ["Trailing space" gnus-article-strip-trailing-space t]
2060                ["Leading space in headers"
2061                 gnus-article-remove-leading-whitespace t])
2062               ["Overstrike" gnus-article-treat-overstrike t]
2063               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2064               ["Emphasis" gnus-article-emphasize t]
2065               ["Word wrap" gnus-article-fill-cited-article t]
2066               ["Fill long lines" gnus-article-fill-long-lines t]
2067               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2068               ["CR" gnus-article-remove-cr t]
2069               ["Rot 13" gnus-summary-caesar-message
2070                ,@(if (featurep 'xemacs) '(t)
2071                    '(:help "\"Caesar rotate\" article by 13"))]
2072               ["Unix pipe..." gnus-summary-pipe-message t]
2073               ["Add buttons" gnus-article-add-buttons t]
2074               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2075               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2076               ["Toggle MIME" gnus-summary-toggle-mime t]
2077               ["Verbose header" gnus-summary-verbose-headers t]
2078               ["Toggle header" gnus-summary-toggle-header t]
2079               ["Unfold headers" gnus-article-treat-unfold-headers t]
2080               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2081               ["Html" gnus-article-wash-html t]
2082               ["URLs" gnus-article-unsplit-urls t]
2083               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2084               ["HZ" gnus-article-decode-HZ t]
2085               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2086               )
2087              ("Output"
2088               ["Save in default format" gnus-summary-save-article
2089                ,@(if (featurep 'xemacs) '(t)
2090                    '(:help "Save article using default method"))]
2091               ["Save in file" gnus-summary-save-article-file
2092                ,@(if (featurep 'xemacs) '(t)
2093                    '(:help "Save article in file"))]
2094               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2095               ["Save in MH folder" gnus-summary-save-article-folder t]
2096               ["Save in VM folder" gnus-summary-save-article-vm t]
2097               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2098               ["Save body in file" gnus-summary-save-article-body-file t]
2099               ["Pipe through a filter" gnus-summary-pipe-output t]
2100               ["Add to SOUP packet" gnus-soup-add-article t]
2101               ["Print with Muttprint" gnus-summary-muttprint t]
2102               ["Print" gnus-summary-print-article t])
2103              ("Backend"
2104               ["Respool article..." gnus-summary-respool-article t]
2105               ["Move article..." gnus-summary-move-article
2106                (gnus-check-backend-function
2107                 'request-move-article gnus-newsgroup-name)]
2108               ["Copy article..." gnus-summary-copy-article t]
2109               ["Crosspost article..." gnus-summary-crosspost-article
2110                (gnus-check-backend-function
2111                 'request-replace-article gnus-newsgroup-name)]
2112               ["Import file..." gnus-summary-import-article t]
2113               ["Create article..." gnus-summary-create-article t]
2114               ["Check if posted" gnus-summary-article-posted-p t]
2115               ["Edit article" gnus-summary-edit-article
2116                (not (gnus-group-read-only-p))]
2117               ["Delete article" gnus-summary-delete-article
2118                (gnus-check-backend-function
2119                 'request-expire-articles gnus-newsgroup-name)]
2120               ["Query respool" gnus-summary-respool-query t]
2121               ["Trace respool" gnus-summary-respool-trace t]
2122               ["Delete expirable articles" gnus-summary-expire-articles-now
2123                (gnus-check-backend-function
2124                 'request-expire-articles gnus-newsgroup-name)])
2125              ("Extract"
2126               ["Uudecode" gnus-uu-decode-uu
2127                ,@(if (featurep 'xemacs) '(t)
2128                    '(:help "Decode uuencoded article(s)"))]
2129               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2130               ["Unshar" gnus-uu-decode-unshar t]
2131               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2132               ["Save" gnus-uu-decode-save t]
2133               ["Binhex" gnus-uu-decode-binhex t]
2134               ["Postscript" gnus-uu-decode-postscript t])
2135              ("Cache"
2136               ["Enter article" gnus-cache-enter-article t]
2137               ["Remove article" gnus-cache-remove-article t])
2138              ["Translate" gnus-article-babel t]
2139              ["Select article buffer" gnus-summary-select-article-buffer t]
2140              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2141              ["Isearch article..." gnus-summary-isearch-article t]
2142              ["Beginning of the article" gnus-summary-beginning-of-article t]
2143              ["End of the article" gnus-summary-end-of-article t]
2144              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2145              ["Fetch referenced articles" gnus-summary-refer-references t]
2146              ["Fetch current thread" gnus-summary-refer-thread t]
2147              ["Fetch article with id..." gnus-summary-refer-article t]
2148              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2149              ["Redisplay" gnus-summary-show-article t]
2150              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2151       (easy-menu-define
2152        gnus-summary-article-menu gnus-summary-mode-map ""
2153        (cons "Article" innards))
2154
2155       (if (not (keymapp gnus-summary-article-menu))
2156           (easy-menu-define
2157            gnus-article-commands-menu gnus-article-mode-map ""
2158            (cons "Commands" innards))
2159         ;; in Emacs, don't share menu.
2160         (setq gnus-article-commands-menu
2161               (copy-keymap gnus-summary-article-menu))
2162         (define-key gnus-article-mode-map [menu-bar commands]
2163           (cons "Commands" gnus-article-commands-menu))))
2164
2165     (easy-menu-define
2166      gnus-summary-thread-menu gnus-summary-mode-map ""
2167      '("Threads"
2168        ["Toggle threading" gnus-summary-toggle-threads t]
2169        ["Hide threads" gnus-summary-hide-all-threads t]
2170        ["Show threads" gnus-summary-show-all-threads t]
2171        ["Hide thread" gnus-summary-hide-thread t]
2172        ["Show thread" gnus-summary-show-thread t]
2173        ["Go to next thread" gnus-summary-next-thread t]
2174        ["Go to previous thread" gnus-summary-prev-thread t]
2175        ["Go down thread" gnus-summary-down-thread t]
2176        ["Go up thread" gnus-summary-up-thread t]
2177        ["Top of thread" gnus-summary-top-thread t]
2178        ["Mark thread as read" gnus-summary-kill-thread t]
2179        ["Lower thread score" gnus-summary-lower-thread t]
2180        ["Raise thread score" gnus-summary-raise-thread t]
2181        ["Rethread current" gnus-summary-rethread-current t]))
2182
2183     (easy-menu-define
2184      gnus-summary-post-menu gnus-summary-mode-map ""
2185      `("Post"
2186        ["Send a message (mail or news)" gnus-summary-post-news
2187         ,@(if (featurep 'xemacs) '(t)
2188             '(:help "Post an article"))]
2189        ["Followup" gnus-summary-followup
2190         ,@(if (featurep 'xemacs) '(t)
2191             '(:help "Post followup to this article"))]
2192        ["Followup and yank" gnus-summary-followup-with-original
2193         ,@(if (featurep 'xemacs) '(t)
2194             '(:help "Post followup to this article, quoting its contents"))]
2195        ["Supersede article" gnus-summary-supersede-article t]
2196        ["Cancel article" gnus-summary-cancel-article
2197         ,@(if (featurep 'xemacs) '(t)
2198             '(:help "Cancel an article you posted"))]
2199        ["Reply" gnus-summary-reply t]
2200        ["Reply and yank" gnus-summary-reply-with-original t]
2201        ["Wide reply" gnus-summary-wide-reply t]
2202        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2203         ,@(if (featurep 'xemacs) '(t)
2204             '(:help "Mail a reply, quoting this article"))]
2205        ["Very wide reply" gnus-summary-very-wide-reply t]
2206        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2207         ,@(if (featurep 'xemacs) '(t)
2208             '(:help "Mail a very wide reply, quoting this article"))]
2209        ["Mail forward" gnus-summary-mail-forward t]
2210        ["Post forward" gnus-summary-post-forward t]
2211        ["Digest and mail" gnus-summary-digest-mail-forward t]
2212        ["Digest and post" gnus-summary-digest-post-forward t]
2213        ["Resend message" gnus-summary-resend-message t]
2214        ["Resend message edit" gnus-summary-resend-message-edit t]
2215        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2216        ["Send a mail" gnus-summary-mail-other-window t]
2217        ["Create a local message" gnus-summary-news-other-window t]
2218        ["Uuencode and post" gnus-uu-post-news
2219         ,@(if (featurep 'xemacs) '(t)
2220             '(:help "Post a uuencoded article"))]
2221        ["Followup via news" gnus-summary-followup-to-mail t]
2222        ["Followup via news and yank"
2223         gnus-summary-followup-to-mail-with-original t]
2224        ;;("Draft"
2225        ;;["Send" gnus-summary-send-draft t]
2226        ;;["Send bounced" gnus-resend-bounced-mail t])
2227        ))
2228
2229     (cond
2230      ((not (keymapp gnus-summary-post-menu))
2231       (setq gnus-article-post-menu gnus-summary-post-menu))
2232      ((not gnus-article-post-menu)
2233       ;; Don't share post menu.
2234       (setq gnus-article-post-menu
2235             (copy-keymap gnus-summary-post-menu))))
2236     (define-key gnus-article-mode-map [menu-bar post]
2237       (cons "Post" gnus-article-post-menu))
2238
2239     (easy-menu-define
2240      gnus-summary-misc-menu gnus-summary-mode-map ""
2241      `("Gnus"
2242        ("Mark Read"
2243         ["Mark as read" gnus-summary-mark-as-read-forward t]
2244         ["Mark same subject and select"
2245          gnus-summary-kill-same-subject-and-select t]
2246         ["Mark same subject" gnus-summary-kill-same-subject t]
2247         ["Catchup" gnus-summary-catchup
2248          ,@(if (featurep 'xemacs) '(t)
2249              '(:help "Mark unread articles in this group as read"))]
2250         ["Catchup all" gnus-summary-catchup-all t]
2251         ["Catchup to here" gnus-summary-catchup-to-here t]
2252         ["Catchup from here" gnus-summary-catchup-from-here t]
2253         ["Catchup region" gnus-summary-mark-region-as-read t]
2254         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2255        ("Mark Various"
2256         ["Tick" gnus-summary-tick-article-forward t]
2257         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2258         ["Remove marks" gnus-summary-clear-mark-forward t]
2259         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2260         ["Set bookmark" gnus-summary-set-bookmark t]
2261         ["Remove bookmark" gnus-summary-remove-bookmark t])
2262        ("Limit to"
2263         ["Marks..." gnus-summary-limit-to-marks t]
2264         ["Subject..." gnus-summary-limit-to-subject t]
2265         ["Author..." gnus-summary-limit-to-author t]
2266         ["Age..." gnus-summary-limit-to-age t]
2267         ["Extra..." gnus-summary-limit-to-extra t]
2268         ["Score..." gnus-summary-limit-to-score t]
2269         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2270         ["Unread" gnus-summary-limit-to-unread t]
2271         ["Unseen" gnus-summary-limit-to-unseen t]
2272         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2273         ["Articles" gnus-summary-limit-to-articles t]
2274         ["Pop limit" gnus-summary-pop-limit t]
2275         ["Show dormant" gnus-summary-limit-include-dormant t]
2276         ["Hide childless dormant"
2277          gnus-summary-limit-exclude-childless-dormant t]
2278         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2279         ["Hide marked" gnus-summary-limit-exclude-marks t]
2280         ["Show expunged" gnus-summary-limit-include-expunged t])
2281        ("Process Mark"
2282         ["Set mark" gnus-summary-mark-as-processable t]
2283         ["Remove mark" gnus-summary-unmark-as-processable t]
2284         ["Remove all marks" gnus-summary-unmark-all-processable t]
2285         ["Mark above" gnus-uu-mark-over t]
2286         ["Mark series" gnus-uu-mark-series t]
2287         ["Mark region" gnus-uu-mark-region t]
2288         ["Unmark region" gnus-uu-unmark-region t]
2289         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2290         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2291         ["Mark all" gnus-uu-mark-all t]
2292         ["Mark buffer" gnus-uu-mark-buffer t]
2293         ["Mark sparse" gnus-uu-mark-sparse t]
2294         ["Mark thread" gnus-uu-mark-thread t]
2295         ["Unmark thread" gnus-uu-unmark-thread t]
2296         ("Process Mark Sets"
2297          ["Kill" gnus-summary-kill-process-mark t]
2298          ["Yank" gnus-summary-yank-process-mark
2299           gnus-newsgroup-process-stack]
2300          ["Save" gnus-summary-save-process-mark t]))
2301        ("Scroll article"
2302         ["Page forward" gnus-summary-next-page
2303          ,@(if (featurep 'xemacs) '(t)
2304              '(:help "Show next page of article"))]
2305         ["Page backward" gnus-summary-prev-page
2306          ,@(if (featurep 'xemacs) '(t)
2307              '(:help "Show previous page of article"))]
2308         ["Line forward" gnus-summary-scroll-up t])
2309        ("Move"
2310         ["Next unread article" gnus-summary-next-unread-article t]
2311         ["Previous unread article" gnus-summary-prev-unread-article t]
2312         ["Next article" gnus-summary-next-article t]
2313         ["Previous article" gnus-summary-prev-article t]
2314         ["Next unread subject" gnus-summary-next-unread-subject t]
2315         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2316         ["Next article same subject" gnus-summary-next-same-subject t]
2317         ["Previous article same subject" gnus-summary-prev-same-subject t]
2318         ["First unread article" gnus-summary-first-unread-article t]
2319         ["Best unread article" gnus-summary-best-unread-article t]
2320         ["Go to subject number..." gnus-summary-goto-subject t]
2321         ["Go to article number..." gnus-summary-goto-article t]
2322         ["Go to the last article" gnus-summary-goto-last-article t]
2323         ["Pop article off history" gnus-summary-pop-article t])
2324        ("Sort"
2325         ["Sort by number" gnus-summary-sort-by-number t]
2326         ["Sort by author" gnus-summary-sort-by-author t]
2327         ["Sort by subject" gnus-summary-sort-by-subject t]
2328         ["Sort by date" gnus-summary-sort-by-date t]
2329         ["Sort by score" gnus-summary-sort-by-score t]
2330         ["Sort by lines" gnus-summary-sort-by-lines t]
2331         ["Sort by characters" gnus-summary-sort-by-chars t]
2332         ["Randomize" gnus-summary-sort-by-random t]
2333         ["Original sort" gnus-summary-sort-by-original t])
2334        ("Help"
2335         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2336         ["Describe group" gnus-summary-describe-group t]
2337         ["Read manual" gnus-info-find-node t])
2338        ("Modes"
2339         ["Pick and read" gnus-pick-mode t]
2340         ["Binary" gnus-binary-mode t])
2341        ("Regeneration"
2342         ["Regenerate" gnus-summary-prepare t]
2343         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2344         ["Toggle threading" gnus-summary-toggle-threads t])
2345        ["See old articles" gnus-summary-insert-old-articles t]
2346        ["See new articles" gnus-summary-insert-new-articles t]
2347        ["Filter articles..." gnus-summary-execute-command t]
2348        ["Run command on subjects..." gnus-summary-universal-argument t]
2349        ["Search articles forward..." gnus-summary-search-article-forward t]
2350        ["Search articles backward..." gnus-summary-search-article-backward t]
2351        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2352        ["Expand window" gnus-summary-expand-window t]
2353        ["Expire expirable articles" gnus-summary-expire-articles
2354         (gnus-check-backend-function
2355          'request-expire-articles gnus-newsgroup-name)]
2356        ["Edit local kill file" gnus-summary-edit-local-kill t]
2357        ["Edit main kill file" gnus-summary-edit-global-kill t]
2358        ["Edit group parameters" gnus-summary-edit-parameters t]
2359        ["Customize group parameters" gnus-summary-customize-parameters t]
2360        ["Send a bug report" gnus-bug t]
2361        ("Exit"
2362         ["Catchup and exit" gnus-summary-catchup-and-exit
2363          ,@(if (featurep 'xemacs) '(t)
2364              '(:help "Mark unread articles in this group as read, then exit"))]
2365         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2366         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2367         ["Exit group" gnus-summary-exit
2368          ,@(if (featurep 'xemacs) '(t)
2369              '(:help "Exit current group, return to group selection mode"))]
2370         ["Exit group without updating" gnus-summary-exit-no-update t]
2371         ["Exit and goto next group" gnus-summary-next-group t]
2372         ["Exit and goto prev group" gnus-summary-prev-group t]
2373         ["Reselect group" gnus-summary-reselect-current-group t]
2374         ["Rescan group" gnus-summary-rescan-group t]
2375         ["Update dribble" gnus-summary-save-newsrc t])))
2376
2377     (gnus-run-hooks 'gnus-summary-menu-hook)))
2378
2379 (defvar gnus-summary-tool-bar-map nil)
2380
2381 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2382 (defun gnus-summary-make-tool-bar ()
2383   (if (and (fboundp 'tool-bar-add-item-from-menu)
2384            (default-value 'tool-bar-mode)
2385            (not gnus-summary-tool-bar-map))
2386       (setq gnus-summary-tool-bar-map
2387             (let ((tool-bar-map (make-sparse-keymap))
2388                   (load-path (mm-image-load-path)))
2389               (tool-bar-add-item-from-menu
2390                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2391               (tool-bar-add-item-from-menu
2392                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2393               (tool-bar-add-item-from-menu
2394                'gnus-summary-post-news "post" gnus-summary-mode-map)
2395               (tool-bar-add-item-from-menu
2396                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2397               (tool-bar-add-item-from-menu
2398                'gnus-summary-followup "followup" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-summary-reply "reply" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2405               (tool-bar-add-item-from-menu
2406                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2407               (tool-bar-add-item-from-menu
2408                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2409               (tool-bar-add-item-from-menu
2410                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2411               (tool-bar-add-item-from-menu
2412                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2413               (tool-bar-add-item-from-menu
2414                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2419               tool-bar-map)))
2420   (if gnus-summary-tool-bar-map
2421       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2422
2423 (defun gnus-score-set-default (var value)
2424   "A version of set that updates the GNU Emacs menu-bar."
2425   (set var value)
2426   ;; It is the message that forces the active status to be updated.
2427   (message ""))
2428
2429 (defun gnus-make-score-map (type)
2430   "Make a summary score map of type TYPE."
2431   (if t
2432       nil
2433     (let ((headers '(("author" "from" string)
2434                      ("subject" "subject" string)
2435                      ("article body" "body" string)
2436                      ("article head" "head" string)
2437                      ("xref" "xref" string)
2438                      ("extra header" "extra" string)
2439                      ("lines" "lines" number)
2440                      ("followups to author" "followup" string)))
2441           (types '((number ("less than" <)
2442                            ("greater than" >)
2443                            ("equal" =))
2444                    (string ("substring" s)
2445                            ("exact string" e)
2446                            ("fuzzy string" f)
2447                            ("regexp" r))))
2448           (perms '(("temporary" (current-time-string))
2449                    ("permanent" nil)
2450                    ("immediate" now)))
2451           header)
2452       (list
2453        (apply
2454         'nconc
2455         (list
2456          (if (eq type 'lower)
2457              "Lower score"
2458            "Increase score"))
2459         (let (outh)
2460           (while headers
2461             (setq header (car headers))
2462             (setq outh
2463                   (cons
2464                    (apply
2465                     'nconc
2466                     (list (car header))
2467                     (let ((ts (cdr (assoc (nth 2 header) types)))
2468                           outt)
2469                       (while ts
2470                         (setq outt
2471                               (cons
2472                                (apply
2473                                 'nconc
2474                                 (list (caar ts))
2475                                 (let ((ps perms)
2476                                       outp)
2477                                   (while ps
2478                                     (setq outp
2479                                           (cons
2480                                            (vector
2481                                             (caar ps)
2482                                             (list
2483                                              'gnus-summary-score-entry
2484                                              (nth 1 header)
2485                                              (if (or (string= (nth 1 header)
2486                                                               "head")
2487                                                      (string= (nth 1 header)
2488                                                               "body"))
2489                                                  ""
2490                                                (list 'gnus-summary-header
2491                                                      (nth 1 header)))
2492                                              (list 'quote (nth 1 (car ts)))
2493                                              (list 'gnus-score-delta-default
2494                                                    nil)
2495                                              (nth 1 (car ps))
2496                                              t)
2497                                             t)
2498                                            outp))
2499                                     (setq ps (cdr ps)))
2500                                   (list (nreverse outp))))
2501                                outt))
2502                         (setq ts (cdr ts)))
2503                       (list (nreverse outt))))
2504                    outh))
2505             (setq headers (cdr headers)))
2506           (list (nreverse outh))))))))
2507
2508 \f
2509
2510 (defun gnus-summary-mode (&optional group)
2511   "Major mode for reading articles.
2512
2513 All normal editing commands are switched off.
2514 \\<gnus-summary-mode-map>
2515 Each line in this buffer represents one article.  To read an
2516 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2517 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2518 respectively.
2519
2520 You can also post articles and send mail from this buffer.  To
2521 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2522 of an article, type `\\[gnus-summary-reply]'.
2523
2524 There are approx. one gazillion commands you can execute in this
2525 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2526
2527 The following commands are available:
2528
2529 \\{gnus-summary-mode-map}"
2530   (interactive)
2531   (kill-all-local-variables)
2532   (when (gnus-visual-p 'summary-menu 'menu)
2533     (gnus-summary-make-menu-bar)
2534     (gnus-summary-make-tool-bar))
2535   (gnus-summary-make-local-variables)
2536   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2537     (gnus-summary-make-local-variables))
2538   (gnus-make-thread-indent-array)
2539   (gnus-simplify-mode-line)
2540   (setq major-mode 'gnus-summary-mode)
2541   (setq mode-name "Summary")
2542   (make-local-variable 'minor-mode-alist)
2543   (use-local-map gnus-summary-mode-map)
2544   (buffer-disable-undo)
2545   (setq buffer-read-only t)             ;Disable modification
2546   (setq truncate-lines t)
2547   (setq selective-display t)
2548   (setq selective-display-ellipses t)   ;Display `...'
2549   (gnus-summary-set-display-table)
2550   (gnus-set-default-directory)
2551   (setq gnus-newsgroup-name group)
2552   (unless (gnus-news-group-p group)
2553     (setq gnus-newsgroup-incorporated
2554           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2555   (make-local-variable 'gnus-summary-line-format)
2556   (make-local-variable 'gnus-summary-line-format-spec)
2557   (make-local-variable 'gnus-summary-dummy-line-format)
2558   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2559   (make-local-variable 'gnus-summary-mark-positions)
2560   (make-local-hook 'pre-command-hook)
2561   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2562   (gnus-run-hooks 'gnus-summary-mode-hook)
2563   (turn-on-gnus-mailing-list-mode)
2564   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2565   (gnus-update-summary-mark-positions))
2566
2567 (defun gnus-summary-make-local-variables ()
2568   "Make all the local summary buffer variables."
2569   (let (global)
2570     (dolist (local gnus-summary-local-variables)
2571       (if (consp local)
2572           (progn
2573             (if (eq (cdr local) 'global)
2574                 ;; Copy the global value of the variable.
2575                 (setq global (symbol-value (car local)))
2576               ;; Use the value from the list.
2577               (setq global (eval (cdr local))))
2578             (set (make-local-variable (car local)) global))
2579         ;; Simple nil-valued local variable.
2580         (set (make-local-variable local) nil)))))
2581
2582 (defun gnus-summary-clear-local-variables ()
2583   (let ((locals gnus-summary-local-variables))
2584     (while locals
2585       (if (consp (car locals))
2586           (and (vectorp (caar locals))
2587                (set (caar locals) nil))
2588         (and (vectorp (car locals))
2589              (set (car locals) nil)))
2590       (setq locals (cdr locals)))))
2591
2592 ;; Summary data functions.
2593
2594 (defmacro gnus-data-number (data)
2595   `(car ,data))
2596
2597 (defmacro gnus-data-set-number (data number)
2598   `(setcar ,data ,number))
2599
2600 (defmacro gnus-data-mark (data)
2601   `(nth 1 ,data))
2602
2603 (defmacro gnus-data-set-mark (data mark)
2604   `(setcar (nthcdr 1 ,data) ,mark))
2605
2606 (defmacro gnus-data-pos (data)
2607   `(nth 2 ,data))
2608
2609 (defmacro gnus-data-set-pos (data pos)
2610   `(setcar (nthcdr 2 ,data) ,pos))
2611
2612 (defmacro gnus-data-header (data)
2613   `(nth 3 ,data))
2614
2615 (defmacro gnus-data-set-header (data header)
2616   `(setcar (nthcdr 3 ,data) ,header))
2617
2618 (defmacro gnus-data-level (data)
2619   `(nth 4 ,data))
2620
2621 (defmacro gnus-data-unread-p (data)
2622   `(= (nth 1 ,data) gnus-unread-mark))
2623
2624 (defmacro gnus-data-read-p (data)
2625   `(/= (nth 1 ,data) gnus-unread-mark))
2626
2627 (defmacro gnus-data-pseudo-p (data)
2628   `(consp (nth 3 ,data)))
2629
2630 (defmacro gnus-data-find (number)
2631   `(assq ,number gnus-newsgroup-data))
2632
2633 (defmacro gnus-data-find-list (number &optional data)
2634   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2635      (memq (assq ,number bdata)
2636            bdata)))
2637
2638 (defmacro gnus-data-make (number mark pos header level)
2639   `(list ,number ,mark ,pos ,header ,level))
2640
2641 (defun gnus-data-enter (after-article number mark pos header level offset)
2642   (let ((data (gnus-data-find-list after-article)))
2643     (unless data
2644       (error "No such article: %d" after-article))
2645     (setcdr data (cons (gnus-data-make number mark pos header level)
2646                        (cdr data)))
2647     (setq gnus-newsgroup-data-reverse nil)
2648     (gnus-data-update-list (cddr data) offset)))
2649
2650 (defun gnus-data-enter-list (after-article list &optional offset)
2651   (when list
2652     (let ((data (and after-article (gnus-data-find-list after-article)))
2653           (ilist list))
2654       (if (not (or data
2655                    after-article))
2656           (let ((odata gnus-newsgroup-data))
2657             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2658             (when offset
2659               (gnus-data-update-list odata offset)))
2660         ;; Find the last element in the list to be spliced into the main
2661         ;; list.
2662         (while (cdr list)
2663           (setq list (cdr list)))
2664         (if (not data)
2665             (progn
2666               (setcdr list gnus-newsgroup-data)
2667               (setq gnus-newsgroup-data ilist)
2668               (when offset
2669                 (gnus-data-update-list (cdr list) offset)))
2670           (setcdr list (cdr data))
2671           (setcdr data ilist)
2672           (when offset
2673             (gnus-data-update-list (cdr list) offset))))
2674       (setq gnus-newsgroup-data-reverse nil))))
2675
2676 (defun gnus-data-remove (article &optional offset)
2677   (let ((data gnus-newsgroup-data))
2678     (if (= (gnus-data-number (car data)) article)
2679         (progn
2680           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2681                 gnus-newsgroup-data-reverse nil)
2682           (when offset
2683             (gnus-data-update-list gnus-newsgroup-data offset)))
2684       (while (cdr data)
2685         (when (= (gnus-data-number (cadr data)) article)
2686           (setcdr data (cddr data))
2687           (when offset
2688             (gnus-data-update-list (cdr data) offset))
2689           (setq data nil
2690                 gnus-newsgroup-data-reverse nil))
2691         (setq data (cdr data))))))
2692
2693 (defmacro gnus-data-list (backward)
2694   `(if ,backward
2695        (or gnus-newsgroup-data-reverse
2696            (setq gnus-newsgroup-data-reverse
2697                  (reverse gnus-newsgroup-data)))
2698      gnus-newsgroup-data))
2699
2700 (defun gnus-data-update-list (data offset)
2701   "Add OFFSET to the POS of all data entries in DATA."
2702   (setq gnus-newsgroup-data-reverse nil)
2703   (while data
2704     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2705     (setq data (cdr data))))
2706
2707 (defun gnus-summary-article-pseudo-p (article)
2708   "Say whether this article is a pseudo article or not."
2709   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2710
2711 (defmacro gnus-summary-article-sparse-p (article)
2712   "Say whether this article is a sparse article or not."
2713   `(memq ,article gnus-newsgroup-sparse))
2714
2715 (defmacro gnus-summary-article-ancient-p (article)
2716   "Say whether this article is a sparse article or not."
2717   `(memq ,article gnus-newsgroup-ancient))
2718
2719 (defun gnus-article-parent-p (number)
2720   "Say whether this article is a parent or not."
2721   (let ((data (gnus-data-find-list number)))
2722     (and (cdr data)                     ; There has to be an article after...
2723          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2724             (gnus-data-level (nth 1 data))))))
2725
2726 (defun gnus-article-children (number)
2727   "Return a list of all children to NUMBER."
2728   (let* ((data (gnus-data-find-list number))
2729          (level (gnus-data-level (car data)))
2730          children)
2731     (setq data (cdr data))
2732     (while (and data
2733                 (= (gnus-data-level (car data)) (1+ level)))
2734       (push (gnus-data-number (car data)) children)
2735       (setq data (cdr data)))
2736     children))
2737
2738 (defmacro gnus-summary-skip-intangible ()
2739   "If the current article is intangible, then jump to a different article."
2740   '(let ((to (get-text-property (point) 'gnus-intangible)))
2741      (and to (gnus-summary-goto-subject to))))
2742
2743 (defmacro gnus-summary-article-intangible-p ()
2744   "Say whether this article is intangible or not."
2745   '(get-text-property (point) 'gnus-intangible))
2746
2747 (defun gnus-article-read-p (article)
2748   "Say whether ARTICLE is read or not."
2749   (not (or (memq article gnus-newsgroup-marked)
2750            (memq article gnus-newsgroup-spam-marked)
2751            (memq article gnus-newsgroup-unreads)
2752            (memq article gnus-newsgroup-unselected)
2753            (memq article gnus-newsgroup-dormant))))
2754
2755 ;; Some summary mode macros.
2756
2757 (defmacro gnus-summary-article-number ()
2758   "The article number of the article on the current line.
2759 If there isn's an article number here, then we return the current
2760 article number."
2761   '(progn
2762      (gnus-summary-skip-intangible)
2763      (or (get-text-property (point) 'gnus-number)
2764          (gnus-summary-last-subject))))
2765
2766 (defmacro gnus-summary-article-header (&optional number)
2767   "Return the header of article NUMBER."
2768   `(gnus-data-header (gnus-data-find
2769                       ,(or number '(gnus-summary-article-number)))))
2770
2771 (defmacro gnus-summary-thread-level (&optional number)
2772   "Return the level of thread that starts with article NUMBER."
2773   `(if (and (eq gnus-summary-make-false-root 'dummy)
2774             (get-text-property (point) 'gnus-intangible))
2775        0
2776      (gnus-data-level (gnus-data-find
2777                        ,(or number '(gnus-summary-article-number))))))
2778
2779 (defmacro gnus-summary-article-mark (&optional number)
2780   "Return the mark of article NUMBER."
2781   `(gnus-data-mark (gnus-data-find
2782                     ,(or number '(gnus-summary-article-number)))))
2783
2784 (defmacro gnus-summary-article-pos (&optional number)
2785   "Return the position of the line of article NUMBER."
2786   `(gnus-data-pos (gnus-data-find
2787                    ,(or number '(gnus-summary-article-number)))))
2788
2789 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2790 (defmacro gnus-summary-article-subject (&optional number)
2791   "Return current subject string or nil if nothing."
2792   `(let ((headers
2793           ,(if number
2794                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2795              '(gnus-data-header (assq (gnus-summary-article-number)
2796                                       gnus-newsgroup-data)))))
2797      (and headers
2798           (vectorp headers)
2799           (mail-header-subject headers))))
2800
2801 (defmacro gnus-summary-article-score (&optional number)
2802   "Return current article score."
2803   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2804                   gnus-newsgroup-scored))
2805        gnus-summary-default-score 0))
2806
2807 (defun gnus-summary-article-children (&optional number)
2808   "Return a list of article numbers that are children of article NUMBER."
2809   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2810          (level (gnus-data-level (car data)))
2811          l children)
2812     (while (and (setq data (cdr data))
2813                 (> (setq l (gnus-data-level (car data))) level))
2814       (and (= (1+ level) l)
2815            (push (gnus-data-number (car data))
2816                  children)))
2817     (nreverse children)))
2818
2819 (defun gnus-summary-article-parent (&optional number)
2820   "Return the article number of the parent of article NUMBER."
2821   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2822                                     (gnus-data-list t)))
2823          (level (gnus-data-level (car data))))
2824     (if (zerop level)
2825         ()                              ; This is a root.
2826       ;; We search until we find an article with a level less than
2827       ;; this one.  That function has to be the parent.
2828       (while (and (setq data (cdr data))
2829                   (not (< (gnus-data-level (car data)) level))))
2830       (and data (gnus-data-number (car data))))))
2831
2832 (defun gnus-unread-mark-p (mark)
2833   "Say whether MARK is the unread mark."
2834   (= mark gnus-unread-mark))
2835
2836 (defun gnus-read-mark-p (mark)
2837   "Say whether MARK is one of the marks that mark as read.
2838 This is all marks except unread, ticked, dormant, and expirable."
2839   (not (or (= mark gnus-unread-mark)
2840            (= mark gnus-ticked-mark)
2841            (= mark gnus-dormant-mark)
2842            (= mark gnus-expirable-mark))))
2843
2844 (defmacro gnus-article-mark (number)
2845   "Return the MARK of article NUMBER.
2846 This macro should only be used when computing the mark the \"first\"
2847 time; i.e., when generating the summary lines.  After that,
2848 `gnus-summary-article-mark' should be used to examine the
2849 marks of articles."
2850   `(cond
2851     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2852     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2853     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2854     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2855     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2856     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2857     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2858     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2859     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2860            gnus-ancient-mark))))
2861
2862 ;; Saving hidden threads.
2863
2864 (defmacro gnus-save-hidden-threads (&rest forms)
2865   "Save hidden threads, eval FORMS, and restore the hidden threads."
2866   (let ((config (make-symbol "config")))
2867     `(let ((,config (gnus-hidden-threads-configuration)))
2868        (unwind-protect
2869            (save-excursion
2870              ,@forms)
2871          (gnus-restore-hidden-threads-configuration ,config)))))
2872 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2873 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2874
2875 (defun gnus-data-compute-positions ()
2876   "Compute the positions of all articles."
2877   (setq gnus-newsgroup-data-reverse nil)
2878   (let ((data gnus-newsgroup-data))
2879     (save-excursion
2880       (gnus-save-hidden-threads
2881         (gnus-summary-show-all-threads)
2882         (goto-char (point-min))
2883         (while data
2884           (while (get-text-property (point) 'gnus-intangible)
2885             (forward-line 1))
2886           (gnus-data-set-pos (car data) (+ (point) 3))
2887           (setq data (cdr data))
2888           (forward-line 1))))))
2889
2890 (defun gnus-hidden-threads-configuration ()
2891   "Return the current hidden threads configuration."
2892   (save-excursion
2893     (let (config)
2894       (goto-char (point-min))
2895       (while (search-forward "\r" nil t)
2896         (push (1- (point)) config))
2897       config)))
2898
2899 (defun gnus-restore-hidden-threads-configuration (config)
2900   "Restore hidden threads configuration from CONFIG."
2901   (save-excursion
2902     (let (point buffer-read-only)
2903       (while (setq point (pop config))
2904         (when (and (< point (point-max))
2905                    (goto-char point)
2906                    (eq (char-after) ?\n))
2907           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2908
2909 ;; Various summary mode internalish functions.
2910
2911 (defun gnus-mouse-pick-article (e)
2912   (interactive "e")
2913   (mouse-set-point e)
2914   (gnus-summary-next-page nil t))
2915
2916 (defun gnus-summary-set-display-table ()
2917   "Change the display table.
2918 Odd characters have a tendency to mess
2919 up nicely formatted displays - we make all possible glyphs
2920 display only a single character."
2921
2922   ;; We start from the standard display table, if any.
2923   (let ((table (or (copy-sequence standard-display-table)
2924                    (make-display-table)))
2925         (i 32))
2926     ;; Nix out all the control chars...
2927     (while (>= (setq i (1- i)) 0)
2928       (aset table i [??]))
2929     ;; ... but not newline and cr, of course.  (cr is necessary for the
2930     ;; selective display).
2931     (aset table ?\n nil)
2932     (aset table ?\r nil)
2933     ;; We keep TAB as well.
2934     (aset table ?\t nil)
2935     ;; We nix out any glyphs over 126 that are not set already.
2936     (let ((i 256))
2937       (while (>= (setq i (1- i)) 127)
2938         ;; Only modify if the entry is nil.
2939         (unless (aref table i)
2940           (aset table i [??]))))
2941     (setq buffer-display-table table)))
2942
2943 (defun gnus-summary-set-article-display-arrow (pos)
2944   "Update the overlay arrow to point to line at position POS."
2945   (when (and gnus-summary-display-arrow
2946              (boundp 'overlay-arrow-position)
2947              (boundp 'overlay-arrow-string))
2948     (save-excursion
2949       (goto-char pos)
2950       (beginning-of-line)
2951       (unless overlay-arrow-position
2952         (setq overlay-arrow-position (make-marker)))
2953       (setq overlay-arrow-string "=>"
2954             overlay-arrow-position (set-marker overlay-arrow-position
2955                                                (point)
2956                                                (current-buffer))))))
2957
2958 (defun gnus-summary-buffer-name (group)
2959   "Return the summary buffer name of GROUP."
2960   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2961
2962 (defun gnus-summary-setup-buffer (group)
2963   "Initialize summary buffer."
2964   (let ((buffer (gnus-summary-buffer-name group))
2965         (dead-name (concat "*Dead Summary "
2966                            (gnus-group-decoded-name group) "*")))
2967     ;; If a dead summary buffer exists, we kill it.
2968     (when (gnus-buffer-live-p dead-name)
2969       (gnus-kill-buffer dead-name))
2970     (if (get-buffer buffer)
2971         (progn
2972           (set-buffer buffer)
2973           (setq gnus-summary-buffer (current-buffer))
2974           (not gnus-newsgroup-prepared))
2975       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2976       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2977       (gnus-summary-mode group)
2978       (when gnus-carpal
2979         (gnus-carpal-setup-buffer 'summary))
2980       (unless gnus-single-article-buffer
2981         (make-local-variable 'gnus-article-buffer)
2982         (make-local-variable 'gnus-article-current)
2983         (make-local-variable 'gnus-original-article-buffer))
2984       (setq gnus-newsgroup-name group)
2985       ;; Set any local variables in the group parameters.
2986       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2987       t)))
2988
2989 (defun gnus-set-global-variables ()
2990   "Set the global equivalents of the buffer-local variables.
2991 They are set to the latest values they had.  These reflect the summary
2992 buffer that was in action when the last article was fetched."
2993   (when (eq major-mode 'gnus-summary-mode)
2994     (setq gnus-summary-buffer (current-buffer))
2995     (let ((name gnus-newsgroup-name)
2996           (marked gnus-newsgroup-marked)
2997           (spam gnus-newsgroup-spam-marked)
2998           (unread gnus-newsgroup-unreads)
2999           (headers gnus-current-headers)
3000           (data gnus-newsgroup-data)
3001           (summary gnus-summary-buffer)
3002           (article-buffer gnus-article-buffer)
3003           (original gnus-original-article-buffer)
3004           (gac gnus-article-current)
3005           (reffed gnus-reffed-article-number)
3006           (score-file gnus-current-score-file)
3007           (default-charset gnus-newsgroup-charset)
3008           vlist)
3009       (let ((locals gnus-newsgroup-variables))
3010         (while locals
3011           (if (consp (car locals))
3012               (push (eval (caar locals)) vlist)
3013             (push (eval (car locals)) vlist))
3014           (setq locals (cdr locals)))
3015         (setq vlist (nreverse vlist)))
3016       (save-excursion
3017         (set-buffer gnus-group-buffer)
3018         (setq gnus-newsgroup-name name
3019               gnus-newsgroup-marked marked
3020               gnus-newsgroup-spam-marked spam
3021               gnus-newsgroup-unreads unread
3022               gnus-current-headers headers
3023               gnus-newsgroup-data data
3024               gnus-article-current gac
3025               gnus-summary-buffer summary
3026               gnus-article-buffer article-buffer
3027               gnus-original-article-buffer original
3028               gnus-reffed-article-number reffed
3029               gnus-current-score-file score-file
3030               gnus-newsgroup-charset default-charset)
3031         (let ((locals gnus-newsgroup-variables))
3032           (while locals
3033             (if (consp (car locals))
3034                 (set (caar locals) (pop vlist))
3035               (set (car locals) (pop vlist)))
3036             (setq locals (cdr locals))))
3037         ;; The article buffer also has local variables.
3038         (when (gnus-buffer-live-p gnus-article-buffer)
3039           (set-buffer gnus-article-buffer)
3040           (setq gnus-summary-buffer summary))))))
3041
3042 (defun gnus-summary-article-unread-p (article)
3043   "Say whether ARTICLE is unread or not."
3044   (memq article gnus-newsgroup-unreads))
3045
3046 (defun gnus-summary-first-article-p (&optional article)
3047   "Return whether ARTICLE is the first article in the buffer."
3048   (if (not (setq article (or article (gnus-summary-article-number))))
3049       nil
3050     (eq article (caar gnus-newsgroup-data))))
3051
3052 (defun gnus-summary-last-article-p (&optional article)
3053   "Return whether ARTICLE is the last article in the buffer."
3054   (if (not (setq article (or article (gnus-summary-article-number))))
3055       ;; All non-existent numbers are the last article.  :-)
3056       t
3057     (not (cdr (gnus-data-find-list article)))))
3058
3059 (defun gnus-make-thread-indent-array ()
3060   (let ((n 200))
3061     (unless (and gnus-thread-indent-array
3062                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3063       (setq gnus-thread-indent-array (make-vector 201 "")
3064             gnus-thread-indent-array-level gnus-thread-indent-level)
3065       (while (>= n 0)
3066         (aset gnus-thread-indent-array n
3067               (make-string (* n gnus-thread-indent-level) ? ))
3068         (setq n (1- n))))))
3069
3070 (defun gnus-update-summary-mark-positions ()
3071   "Compute where the summary marks are to go."
3072   (save-excursion
3073     (when (gnus-buffer-exists-p gnus-summary-buffer)
3074       (set-buffer gnus-summary-buffer))
3075     (let ((gnus-replied-mark 129)
3076           (gnus-score-below-mark 130)
3077           (gnus-score-over-mark 130)
3078           (gnus-download-mark 131)
3079           (spec gnus-summary-line-format-spec)
3080           gnus-visual pos)
3081       (save-excursion
3082         (gnus-set-work-buffer)
3083         (let ((gnus-summary-line-format-spec spec)
3084               (gnus-newsgroup-downloadable '((0 . t))))
3085           (gnus-summary-insert-line
3086            (make-full-mail-header 0 "" "nobody"
3087                                   "05 Apr 2001 23:33:09 +0400"
3088                                   "" "" 0 0 "" nil)
3089            0 nil 128 t nil "" nil 1)
3090           (goto-char (point-min))
3091           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3092                                              (- (point) (point-min) 1)))))
3093           (goto-char (point-min))
3094           (push (cons 'replied (and (search-forward "\201" nil t)
3095                                     (- (point) (point-min) 1)))
3096                 pos)
3097           (goto-char (point-min))
3098           (push (cons 'score (and (search-forward "\202" nil t)
3099                                   (- (point) (point-min) 1)))
3100                 pos)
3101           (goto-char (point-min))
3102           (push (cons 'download
3103                       (and (search-forward "\203" nil t)
3104                            (- (point) (point-min) 1)))
3105                 pos)))
3106       (setq gnus-summary-mark-positions pos))))
3107
3108 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3109   "Insert a dummy root in the summary buffer."
3110   (beginning-of-line)
3111   (gnus-add-text-properties
3112    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3113    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3114
3115 (defun gnus-summary-extract-address-component (from)
3116   (or (car (funcall gnus-extract-address-components from))
3117       from))
3118
3119 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3120   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3121                                 default-mime-charset)))
3122     ;; Is it really necessary to do this next part for each summary line?
3123     ;; Luckily, doesn't seem to slow things down much.
3124     (or
3125      (and gnus-ignored-from-addresses
3126           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3127           (let ((extra-headers (mail-header-extra header))
3128                 to
3129                 newsgroups)
3130             (cond
3131              ((setq to (cdr (assq 'To extra-headers)))
3132               (concat "-> "
3133                       (inline
3134                         (gnus-summary-extract-address-component
3135                          (funcall gnus-decode-encoded-word-function to)))))
3136              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3137               (concat "=> " newsgroups)))))
3138      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3139
3140 (defun gnus-summary-insert-line (gnus-tmp-header
3141                                  gnus-tmp-level gnus-tmp-current
3142                                  gnus-tmp-unread gnus-tmp-replied
3143                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3144                                  &optional gnus-tmp-dummy gnus-tmp-score
3145                                  gnus-tmp-process)
3146   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3147          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3148          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3149          (gnus-tmp-score-char
3150           (if (or (null gnus-summary-default-score)
3151                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3152                       gnus-summary-zcore-fuzz))
3153               ?\ ;;;Whitespace
3154             (if (< gnus-tmp-score gnus-summary-default-score)
3155                 gnus-score-below-mark gnus-score-over-mark)))
3156          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3157          (gnus-tmp-replied
3158           (cond (gnus-tmp-process gnus-process-mark)
3159                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3160                  gnus-cached-mark)
3161                 (gnus-tmp-replied gnus-replied-mark)
3162                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3163                  gnus-forwarded-mark)
3164                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3165                  gnus-saved-mark)
3166                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3167                  gnus-recent-mark)
3168                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3169                  gnus-unseen-mark)
3170                 (t gnus-no-mark)))
3171          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3172          (gnus-tmp-name
3173           (cond
3174            ((string-match "<[^>]+> *$" gnus-tmp-from)
3175             (let ((beg (match-beginning 0)))
3176               (or (and (string-match "^\".+\"" gnus-tmp-from)
3177                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3178                   (substring gnus-tmp-from 0 beg))))
3179            ((string-match "(.+)" gnus-tmp-from)
3180             (substring gnus-tmp-from
3181                        (1+ (match-beginning 0)) (1- (match-end 0))))
3182            (t gnus-tmp-from)))
3183          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3184          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3185          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3186          (buffer-read-only nil))
3187     (when (string= gnus-tmp-name "")
3188       (setq gnus-tmp-name gnus-tmp-from))
3189     (unless (numberp gnus-tmp-lines)
3190       (setq gnus-tmp-lines -1))
3191     (if (= gnus-tmp-lines -1)
3192         (setq gnus-tmp-lines "?")
3193       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3194     (gnus-put-text-property-excluding-characters-with-faces
3195      (point)
3196      (progn (eval gnus-summary-line-format-spec) (point))
3197      'gnus-number gnus-tmp-number)
3198     (when (gnus-visual-p 'summary-highlight 'highlight)
3199       (forward-line -1)
3200       (gnus-run-hooks 'gnus-summary-update-hook)
3201       (forward-line 1))))
3202
3203 (defun gnus-summary-update-line (&optional dont-update)
3204   "Update summary line after change."
3205   (when (and gnus-summary-default-score
3206              (not gnus-summary-inhibit-highlight))
3207     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3208            (article (gnus-summary-article-number))
3209            (score (gnus-summary-article-score article)))
3210       (unless dont-update
3211         (if (and gnus-summary-mark-below
3212                  (< (gnus-summary-article-score)
3213                     gnus-summary-mark-below))
3214             ;; This article has a low score, so we mark it as read.
3215             (when (memq article gnus-newsgroup-unreads)
3216               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3217           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3218             ;; This article was previously marked as read on account
3219             ;; of a low score, but now it has risen, so we mark it as
3220             ;; unread.
3221             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3222         (gnus-summary-update-mark
3223          (if (or (null gnus-summary-default-score)
3224                  (<= (abs (- score gnus-summary-default-score))
3225                      gnus-summary-zcore-fuzz))
3226              ?\ ;;;Whitespace
3227            (if (< score gnus-summary-default-score)
3228                gnus-score-below-mark gnus-score-over-mark))
3229          'score))
3230       ;; Do visual highlighting.
3231       (when (gnus-visual-p 'summary-highlight 'highlight)
3232         (gnus-run-hooks 'gnus-summary-update-hook)))))
3233
3234 (defvar gnus-tmp-new-adopts nil)
3235
3236 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3237   "Return the number of articles in THREAD.
3238 This may be 0 in some cases -- if none of the articles in
3239 the thread are to be displayed."
3240   (let* ((number
3241           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3242           (cond
3243            ((not (listp thread))
3244             1)
3245            ((and (consp thread) (cdr thread))
3246             (apply
3247              '+ 1 (mapcar
3248                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3249            ((null thread)
3250             1)
3251            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3252             1)
3253            (t 0))))
3254     (when (and level (zerop level) gnus-tmp-new-adopts)
3255       (incf number
3256             (apply '+ (mapcar
3257                        'gnus-summary-number-of-articles-in-thread
3258                        gnus-tmp-new-adopts))))
3259     (if char
3260         (if (> number 1) gnus-not-empty-thread-mark
3261           gnus-empty-thread-mark)
3262       number)))
3263
3264 (defsubst gnus-summary-line-message-size (head)
3265   "Return pretty-printed version of message size.
3266 This function is intended to be used in
3267 `gnus-summary-line-format-alist', which see."
3268   (let ((c (or (mail-header-chars head) -1)))
3269     (cond ((< c 0) "n/a")               ; chars not available
3270           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3271           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3272           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3273           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3274
3275
3276 (defun gnus-summary-set-local-parameters (group)
3277   "Go through the local params of GROUP and set all variable specs in that list."
3278   (let ((params (gnus-group-find-parameter group))
3279         (vars '(quit-config))           ; Ignore quit-config.
3280         elem)
3281     (while params
3282       (setq elem (car params)
3283             params (cdr params))
3284       (and (consp elem)                 ; Has to be a cons.
3285            (consp (cdr elem))           ; The cdr has to be a list.
3286            (symbolp (car elem))         ; Has to be a symbol in there.
3287            (not (memq (car elem) vars))
3288            (ignore-errors               ; So we set it.
3289              (push (car elem) vars)
3290              (make-local-variable (car elem))
3291              (set (car elem) (eval (nth 1 elem))))))))
3292
3293 (defun gnus-summary-read-group (group &optional show-all no-article
3294                                       kill-buffer no-display backward
3295                                       select-articles)
3296   "Start reading news in newsgroup GROUP.
3297 If SHOW-ALL is non-nil, already read articles are also listed.
3298 If NO-ARTICLE is non-nil, no article is selected initially.
3299 If NO-DISPLAY, don't generate a summary buffer."
3300   (let (result)
3301     (while (and group
3302                 (null (setq result
3303                             (let ((gnus-auto-select-next nil))
3304                               (or (gnus-summary-read-group-1
3305                                    group show-all no-article
3306                                    kill-buffer no-display
3307                                    select-articles)
3308                                   (setq show-all nil
3309                                         select-articles nil)))))
3310                 (eq gnus-auto-select-next 'quietly))
3311       (set-buffer gnus-group-buffer)
3312       ;; The entry function called above goes to the next
3313       ;; group automatically, so we go two groups back
3314       ;; if we are searching for the previous group.
3315       (when backward
3316         (gnus-group-prev-unread-group 2))
3317       (if (not (equal group (gnus-group-group-name)))
3318           (setq group (gnus-group-group-name))
3319         (setq group nil)))
3320     result))
3321
3322 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3323   "Directly jump to the other GROUP from summary buffer.
3324 If SHOW-ALL is non-nil, already read articles are also listed."
3325   (interactive
3326    (if (eq gnus-summary-buffer (current-buffer))
3327        (list (completing-read
3328               "Group: " gnus-active-hashtb nil t
3329               (when (and gnus-newsgroup-name
3330                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3331                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3332               'gnus-group-history)
3333              current-prefix-arg)
3334      (error "%s must be invoked from a gnus summary buffer." this-command)))
3335   (unless (or (zerop (length group))
3336               (and gnus-newsgroup-name
3337                    (string-equal gnus-newsgroup-name group)))
3338     (gnus-summary-exit)
3339     (gnus-summary-read-group group show-all
3340                              gnus-dont-select-after-jump-to-other-group)))
3341
3342 (defun gnus-summary-read-group-1 (group show-all no-article
3343                                         kill-buffer no-display
3344                                         &optional select-articles)
3345   ;; Killed foreign groups can't be entered.
3346   ;;  (when (and (not (gnus-group-native-p group))
3347   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3348   ;;    (error "Dead non-native groups can't be entered"))
3349   (gnus-message 5 "Retrieving newsgroup: %s..."
3350                 (gnus-group-decoded-name group))
3351   (let* ((new-group (gnus-summary-setup-buffer group))
3352          (quit-config (gnus-group-quit-config group))
3353          (did-select (and new-group (gnus-select-newsgroup
3354                                      group show-all select-articles))))
3355     (cond
3356      ;; This summary buffer exists already, so we just select it.
3357      ((not new-group)
3358       (gnus-set-global-variables)
3359       (when kill-buffer
3360         (gnus-kill-or-deaden-summary kill-buffer))
3361       (gnus-configure-windows 'summary 'force)
3362       (gnus-set-mode-line 'summary)
3363       (gnus-summary-position-point)
3364       (message "")
3365       t)
3366      ;; We couldn't select this group.
3367      ((null did-select)
3368       (when (and (eq major-mode 'gnus-summary-mode)
3369                  (not (equal (current-buffer) kill-buffer)))
3370         (kill-buffer (current-buffer))
3371         (if (not quit-config)
3372             (progn
3373               ;; Update the info -- marks might need to be removed,
3374               ;; for instance.
3375               (gnus-summary-update-info)
3376               (set-buffer gnus-group-buffer)
3377               (gnus-group-jump-to-group group)
3378               (gnus-group-next-unread-group 1))
3379           (gnus-handle-ephemeral-exit quit-config)))
3380       (let ((grpinfo (gnus-get-info group)))
3381         (if (null (gnus-info-read grpinfo))
3382             (gnus-message 3 "Group %s contains no messages"
3383                           (gnus-group-decoded-name group))
3384           (gnus-message 3 "Can't select group")))
3385       nil)
3386      ;; The user did a `C-g' while prompting for number of articles,
3387      ;; so we exit this group.
3388      ((eq did-select 'quit)
3389       (and (eq major-mode 'gnus-summary-mode)
3390            (not (equal (current-buffer) kill-buffer))
3391            (kill-buffer (current-buffer)))
3392       (when kill-buffer
3393         (gnus-kill-or-deaden-summary kill-buffer))
3394       (if (not quit-config)
3395           (progn
3396             (set-buffer gnus-group-buffer)
3397             (gnus-group-jump-to-group group)
3398             (gnus-group-next-unread-group 1)
3399             (gnus-configure-windows 'group 'force))
3400         (gnus-handle-ephemeral-exit quit-config))
3401       ;; Finally signal the quit.
3402       (signal 'quit nil))
3403      ;; The group was successfully selected.
3404      (t
3405       (gnus-set-global-variables)
3406       ;; Save the active value in effect when the group was entered.
3407       (setq gnus-newsgroup-active
3408             (gnus-copy-sequence
3409              (gnus-active gnus-newsgroup-name)))
3410       ;; You can change the summary buffer in some way with this hook.
3411       (gnus-run-hooks 'gnus-select-group-hook)
3412       (gnus-update-format-specifications
3413        nil 'summary 'summary-mode 'summary-dummy)
3414       (gnus-update-summary-mark-positions)
3415       ;; Do score processing.
3416       (when gnus-use-scoring
3417         (gnus-possibly-score-headers))
3418       ;; Check whether to fill in the gaps in the threads.
3419       (when gnus-build-sparse-threads
3420         (gnus-build-sparse-threads))
3421       ;; Find the initial limit.
3422       (if gnus-show-threads
3423           (if show-all
3424               (let ((gnus-newsgroup-dormant nil))
3425                 (gnus-summary-initial-limit show-all))
3426             (gnus-summary-initial-limit show-all))
3427         ;; When untreaded, all articles are always shown.
3428         (setq gnus-newsgroup-limit
3429               (mapcar
3430                (lambda (header) (mail-header-number header))
3431                gnus-newsgroup-headers)))
3432       ;; Generate the summary buffer.
3433       (unless no-display
3434         (gnus-summary-prepare))
3435       (when gnus-use-trees
3436         (gnus-tree-open group)
3437         (setq gnus-summary-highlight-line-function
3438               'gnus-tree-highlight-article))
3439       ;; If the summary buffer is empty, but there are some low-scored
3440       ;; articles or some excluded dormants, we include these in the
3441       ;; buffer.
3442       (when (and (zerop (buffer-size))
3443                  (not no-display))
3444         (cond (gnus-newsgroup-dormant
3445                (gnus-summary-limit-include-dormant))
3446               ((and gnus-newsgroup-scored show-all)
3447                (gnus-summary-limit-include-expunged t))))
3448       ;; Function `gnus-apply-kill-file' must be called in this hook.
3449       (gnus-run-hooks 'gnus-apply-kill-hook)
3450       (if (and (zerop (buffer-size))
3451                (not no-display))
3452           (progn
3453             ;; This newsgroup is empty.
3454             (gnus-summary-catchup-and-exit nil t)
3455             (gnus-message 6 "No unread news")
3456             (when kill-buffer
3457               (gnus-kill-or-deaden-summary kill-buffer))
3458             ;; Return nil from this function.
3459             nil)
3460         ;; Hide conversation thread subtrees.  We cannot do this in
3461         ;; gnus-summary-prepare-hook since kill processing may not
3462         ;; work with hidden articles.
3463         (gnus-summary-maybe-hide-threads)
3464         (when kill-buffer
3465           (gnus-kill-or-deaden-summary kill-buffer))
3466         (gnus-summary-auto-select-subject)
3467         ;; Show first unread article if requested.
3468         (if (and (not no-article)
3469                  (not no-display)
3470                  gnus-newsgroup-unreads
3471                  gnus-auto-select-first)
3472             (progn
3473               (gnus-configure-windows 'summary)
3474               (let ((art (gnus-summary-article-number)))
3475                 (unless (or (memq art gnus-newsgroup-undownloaded)
3476                             (memq art gnus-newsgroup-downloadable))
3477                   (gnus-summary-goto-article art))))
3478           ;; Don't select any articles.
3479           (gnus-summary-position-point)
3480           (gnus-configure-windows 'summary 'force)
3481           (gnus-set-mode-line 'summary))
3482         (when (get-buffer-window gnus-group-buffer t)
3483           ;; Gotta use windows, because recenter does weird stuff if
3484           ;; the current buffer ain't the displayed window.
3485           (let ((owin (selected-window)))
3486             (select-window (get-buffer-window gnus-group-buffer t))
3487             (when (gnus-group-goto-group group)
3488               (recenter))
3489             (select-window owin)))
3490         ;; Mark this buffer as "prepared".
3491         (setq gnus-newsgroup-prepared t)
3492         (gnus-run-hooks 'gnus-summary-prepared-hook)
3493         t)))))
3494
3495 (defun gnus-summary-auto-select-subject ()
3496   "Select the subject line on initial group entry."
3497   (goto-char (point-min))
3498   (cond
3499    ((eq gnus-auto-select-subject 'best)
3500     (gnus-summary-best-unread-subject))
3501    ((eq gnus-auto-select-subject 'unread)
3502     (gnus-summary-first-unread-subject))
3503    ((eq gnus-auto-select-subject 'unseen)
3504     (gnus-summary-first-unseen-subject))
3505    ((eq gnus-auto-select-subject 'unseen-or-unread)
3506     (gnus-summary-first-unseen-or-unread-subject))
3507    ((eq gnus-auto-select-subject 'first)
3508     ;; Do nothing.
3509     )
3510    ((gnus-functionp gnus-auto-select-subject)
3511     (funcall gnus-auto-select-subject))))
3512
3513 (defun gnus-summary-prepare ()
3514   "Generate the summary buffer."
3515   (interactive)
3516   (let ((buffer-read-only nil))
3517     (erase-buffer)
3518     (setq gnus-newsgroup-data nil
3519           gnus-newsgroup-data-reverse nil)
3520     (gnus-run-hooks 'gnus-summary-generate-hook)
3521     ;; Generate the buffer, either with threads or without.
3522     (when gnus-newsgroup-headers
3523       (gnus-summary-prepare-threads
3524        (if gnus-show-threads
3525            (gnus-sort-gathered-threads
3526             (funcall gnus-summary-thread-gathering-function
3527                      (gnus-sort-threads
3528                       (gnus-cut-threads (gnus-make-threads)))))
3529          ;; Unthreaded display.
3530          (gnus-sort-articles gnus-newsgroup-headers))))
3531     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3532     ;; Call hooks for modifying summary buffer.
3533     (goto-char (point-min))
3534     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3535
3536 (defsubst gnus-general-simplify-subject (subject)
3537   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3538   (setq subject
3539         (cond
3540          ;; Truncate the subject.
3541          (gnus-simplify-subject-functions
3542           (gnus-map-function gnus-simplify-subject-functions subject))
3543          ((numberp gnus-summary-gather-subject-limit)
3544           (setq subject (gnus-simplify-subject-re subject))
3545           (if (> (length subject) gnus-summary-gather-subject-limit)
3546               (substring subject 0 gnus-summary-gather-subject-limit)
3547             subject))
3548          ;; Fuzzily simplify it.
3549          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3550           (gnus-simplify-subject-fuzzy subject))
3551          ;; Just remove the leading "Re:".
3552          (t
3553           (gnus-simplify-subject-re subject))))
3554
3555   (if (and gnus-summary-gather-exclude-subject
3556            (string-match gnus-summary-gather-exclude-subject subject))
3557       nil                               ; This article shouldn't be gathered
3558     subject))
3559
3560 (defun gnus-summary-simplify-subject-query ()
3561   "Query where the respool algorithm would put this article."
3562   (interactive)
3563   (gnus-summary-select-article)
3564   (message "%s"
3565            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3566
3567 (defun gnus-gather-threads-by-subject (threads)
3568   "Gather threads by looking at Subject headers."
3569   (if (not gnus-summary-make-false-root)
3570       threads
3571     (let ((hashtb (gnus-make-hashtable 1024))
3572           (prev threads)
3573           (result threads)
3574           subject hthread whole-subject)
3575       (while threads
3576         (setq subject (gnus-general-simplify-subject
3577                        (setq whole-subject (mail-header-subject
3578                                             (caar threads)))))
3579         (when subject
3580           (if (setq hthread (gnus-gethash subject hashtb))
3581               (progn
3582                 ;; We enter a dummy root into the thread, if we
3583                 ;; haven't done that already.
3584                 (unless (stringp (caar hthread))
3585                   (setcar hthread (list whole-subject (car hthread))))
3586                 ;; We add this new gathered thread to this gathered
3587                 ;; thread.
3588                 (setcdr (car hthread)
3589                         (nconc (cdar hthread) (list (car threads))))
3590                 ;; Remove it from the list of threads.
3591                 (setcdr prev (cdr threads))
3592                 (setq threads prev))
3593             ;; Enter this thread into the hash table.
3594             (gnus-sethash subject threads hashtb)))
3595         (setq prev threads)
3596         (setq threads (cdr threads)))
3597       result)))
3598
3599 (defun gnus-gather-threads-by-references (threads)
3600   "Gather threads by looking at References headers."
3601   (let ((idhashtb (gnus-make-hashtable 1024))
3602         (thhashtb (gnus-make-hashtable 1024))
3603         (prev threads)
3604         (result threads)
3605         ids references id gthread gid entered ref)
3606     (while threads
3607       (when (setq references (mail-header-references (caar threads)))
3608         (setq id (mail-header-id (caar threads))
3609               ids (inline (gnus-split-references references))
3610               entered nil)
3611         (while (setq ref (pop ids))
3612           (setq ids (delete ref ids))
3613           (if (not (setq gid (gnus-gethash ref idhashtb)))
3614               (progn
3615                 (gnus-sethash ref id idhashtb)
3616                 (gnus-sethash id threads thhashtb))
3617             (setq gthread (gnus-gethash gid thhashtb))
3618             (unless entered
3619               ;; We enter a dummy root into the thread, if we
3620               ;; haven't done that already.
3621               (unless (stringp (caar gthread))
3622                 (setcar gthread (list (mail-header-subject (caar gthread))
3623                                       (car gthread))))
3624               ;; We add this new gathered thread to this gathered
3625               ;; thread.
3626               (setcdr (car gthread)
3627                       (nconc (cdar gthread) (list (car threads)))))
3628             ;; Add it into the thread hash table.
3629             (gnus-sethash id gthread thhashtb)
3630             (setq entered t)
3631             ;; Remove it from the list of threads.
3632             (setcdr prev (cdr threads))
3633             (setq threads prev))))
3634       (setq prev threads)
3635       (setq threads (cdr threads)))
3636     result))
3637
3638 (defun gnus-sort-gathered-threads (threads)
3639   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3640   (let ((result threads))
3641     (while threads
3642       (when (stringp (caar threads))
3643         (setcdr (car threads)
3644                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3645       (setq threads (cdr threads)))
3646     result))
3647
3648 (defun gnus-thread-loop-p (root thread)
3649   "Say whether ROOT is in THREAD."
3650   (let ((stack (list thread))
3651         (infloop 0)
3652         th)
3653     (while (setq thread (pop stack))
3654       (setq th (cdr thread))
3655       (while (and th
3656                   (not (eq (caar th) root)))
3657         (pop th))
3658       (if th
3659           ;; We have found a loop.
3660           (let (ref-dep)
3661             (setcdr thread (delq (car th) (cdr thread)))
3662             (if (boundp (setq ref-dep (intern "none"
3663                                               gnus-newsgroup-dependencies)))
3664                 (setcdr (symbol-value ref-dep)
3665                         (nconc (cdr (symbol-value ref-dep))
3666                                (list (car th))))
3667               (set ref-dep (list nil (car th))))
3668             (setq infloop 1
3669                   stack nil))
3670         ;; Push all the subthreads onto the stack.
3671         (push (cdr thread) stack)))
3672     infloop))
3673
3674 (defun gnus-make-threads ()
3675   "Go through the dependency hashtb and find the roots.  Return all threads."
3676   (let (threads)
3677     (while (catch 'infloop
3678              (mapatoms
3679               (lambda (refs)
3680                 ;; Deal with self-referencing References loops.
3681                 (when (and (car (symbol-value refs))
3682                            (not (zerop
3683                                  (apply
3684                                   '+
3685                                   (mapcar
3686                                    (lambda (thread)
3687                                      (gnus-thread-loop-p
3688                                       (car (symbol-value refs)) thread))
3689                                    (cdr (symbol-value refs)))))))
3690                   (setq threads nil)
3691                   (throw 'infloop t))
3692                 (unless (car (symbol-value refs))
3693                   ;; These threads do not refer back to any other articles,
3694                   ;; so they're roots.
3695                   (setq threads (append (cdr (symbol-value refs)) threads))))
3696               gnus-newsgroup-dependencies)))
3697     threads))
3698
3699 ;; Build the thread tree.
3700 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3701   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3702
3703 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3704 if it was already present.
3705
3706 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3707 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3708 Message-IDs will be renamed to a unique Message-ID before being
3709 entered.
3710
3711 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3712   (let* ((id (mail-header-id header))
3713          (id-dep (and id (intern id dependencies)))
3714          parent-id ref ref-dep ref-header replaced)
3715     ;; Enter this `header' in the `dependencies' table.
3716     (cond
3717      ((not id-dep)
3718       (setq header nil))
3719      ;; The first two cases do the normal part: enter a new `header'
3720      ;; in the `dependencies' table.
3721      ((not (boundp id-dep))
3722       (set id-dep (list header)))
3723      ((null (car (symbol-value id-dep)))
3724       (setcar (symbol-value id-dep) header))
3725
3726      ;; From here the `header' was already present in the
3727      ;; `dependencies' table.
3728      (force-new
3729       ;; Overrides an existing entry;
3730       ;; just set the header part of the entry.
3731       (setcar (symbol-value id-dep) header)
3732       (setq replaced t))
3733
3734      ;; Renames the existing `header' to a unique Message-ID.
3735      ((not gnus-summary-ignore-duplicates)
3736       ;; An article with this Message-ID has already been seen.
3737       ;; We rename the Message-ID.
3738       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3739            (list header))
3740       (mail-header-set-id header id))
3741
3742      ;; The last case ignores an existing entry, except it adds any
3743      ;; additional Xrefs (in case the two articles came from different
3744      ;; servers.
3745      ;; Also sets `header' to `nil' meaning that the `dependencies'
3746      ;; table was *not* modified.
3747      (t
3748       (mail-header-set-xref
3749        (car (symbol-value id-dep))
3750        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3751                    "")
3752                (or (mail-header-xref header) "")))
3753       (setq header nil)))
3754
3755     (when (and header (not replaced))
3756       ;; First check that we are not creating a References loop.
3757       (setq parent-id (gnus-parent-id (mail-header-references header)))
3758       (setq ref parent-id)
3759       (while (and ref
3760                   (setq ref-dep (intern-soft ref dependencies))
3761                   (boundp ref-dep)
3762                   (setq ref-header (car (symbol-value ref-dep))))
3763         (if (string= id ref)
3764             ;; Yuk!  This is a reference loop.  Make the article be a
3765             ;; root article.
3766             (progn
3767               (mail-header-set-references (car (symbol-value id-dep)) "none")
3768               (setq ref nil)
3769               (setq parent-id nil))
3770           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3771       (setq ref-dep (intern (or parent-id "none") dependencies))
3772       (if (boundp ref-dep)
3773           (setcdr (symbol-value ref-dep)
3774                   (nconc (cdr (symbol-value ref-dep))
3775                          (list (symbol-value id-dep))))
3776         (set ref-dep (list nil (symbol-value id-dep)))))
3777     header))
3778
3779 (defun gnus-extract-message-id-from-in-reply-to (string)
3780   (if (string-match "<[^>]+>" string)
3781       (substring string (match-beginning 0) (match-end 0))
3782     nil))
3783
3784 (defun gnus-build-sparse-threads ()
3785   (let ((headers gnus-newsgroup-headers)
3786         (mail-parse-charset gnus-newsgroup-charset)
3787         (gnus-summary-ignore-duplicates t)
3788         header references generation relations
3789         subject child end new-child date)
3790     ;; First we create an alist of generations/relations, where
3791     ;; generations is how much we trust the relation, and the relation
3792     ;; is parent/child.
3793     (gnus-message 7 "Making sparse threads...")
3794     (save-excursion
3795       (nnheader-set-temp-buffer " *gnus sparse threads*")
3796       (while (setq header (pop headers))
3797         (when (and (setq references (mail-header-references header))
3798                    (not (string= references "")))
3799           (insert references)
3800           (setq child (mail-header-id header)
3801                 subject (mail-header-subject header)
3802                 date (mail-header-date header)
3803                 generation 0)
3804           (while (search-backward ">" nil t)
3805             (setq end (1+ (point)))
3806             (when (search-backward "<" nil t)
3807               (setq new-child (buffer-substring (point) end))
3808               (push (list (incf generation)
3809                           child (setq child new-child)
3810                           subject date)
3811                     relations)))
3812           (when child
3813             (push (list (1+ generation) child nil subject) relations))
3814           (erase-buffer)))
3815       (kill-buffer (current-buffer)))
3816     ;; Sort over trustworthiness.
3817     (mapcar
3818      (lambda (relation)
3819        (when (gnus-dependencies-add-header
3820               (make-full-mail-header-from-decoded-header
3821                gnus-reffed-article-number
3822                (nth 3 relation) "" (or (nth 4 relation) "")
3823                (nth 1 relation)
3824                (or (nth 2 relation) "") 0 0 "")
3825               gnus-newsgroup-dependencies nil)
3826          (push gnus-reffed-article-number gnus-newsgroup-limit)
3827          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3828          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3829                gnus-newsgroup-reads)
3830          (decf gnus-reffed-article-number)))
3831      (sort relations 'car-less-than-car))
3832     (gnus-message 7 "Making sparse threads...done")))
3833
3834 (defun gnus-build-old-threads ()
3835   ;; Look at all the articles that refer back to old articles, and
3836   ;; fetch the headers for the articles that aren't there.  This will
3837   ;; build complete threads - if the roots haven't been expired by the
3838   ;; server, that is.
3839   (let ((mail-parse-charset gnus-newsgroup-charset)
3840         id heads)
3841     (mapatoms
3842      (lambda (refs)
3843        (when (not (car (symbol-value refs)))
3844          (setq heads (cdr (symbol-value refs)))
3845          (while heads
3846            (if (memq (mail-header-number (caar heads))
3847                      gnus-newsgroup-dormant)
3848                (setq heads (cdr heads))
3849              (setq id (symbol-name refs))
3850              (while (and (setq id (gnus-build-get-header id))
3851                          (not (car (gnus-id-to-thread id)))))
3852              (setq heads nil)))))
3853      gnus-newsgroup-dependencies)))
3854
3855 ;; This function has to be called with point after the article number
3856 ;; on the beginning of the line.
3857 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3858   (let ((eol (gnus-point-at-eol))
3859         (buffer (current-buffer))
3860         header references in-reply-to)
3861
3862     ;; overview: [num subject from date id refs chars lines misc]
3863     (unwind-protect
3864         (progn
3865           (narrow-to-region (point) eol)
3866           (unless (eobp)
3867             (forward-char))
3868
3869           (setq header
3870                 (make-full-mail-header
3871                  number                         ; number
3872                  (nnheader-nov-field)           ; subject
3873                  (nnheader-nov-field)           ; from
3874                  (nnheader-nov-field)           ; date
3875                  (nnheader-nov-read-message-id) ; id
3876                  (nnheader-nov-field)           ; refs
3877                  (nnheader-nov-read-integer)    ; chars
3878                  (nnheader-nov-read-integer)    ; lines
3879                  (unless (eobp)
3880                    (if (looking-at "Xref: ")
3881                        (goto-char (match-end 0)))
3882                    (nnheader-nov-field))        ; Xref
3883                  (nnheader-nov-parse-extra))))  ; extra
3884
3885       (widen))
3886
3887     (when (and (string= references "")
3888                (setq in-reply-to (mail-header-extra header))
3889                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3890       (mail-header-set-references
3891        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3892
3893     (when gnus-alter-header-function
3894       (funcall gnus-alter-header-function header))
3895     (gnus-dependencies-add-header header dependencies force-new)))
3896
3897 (defun gnus-build-get-header (id)
3898   "Look through the buffer of NOV lines and find the header to ID.
3899 Enter this line into the dependencies hash table, and return
3900 the id of the parent article (if any)."
3901   (let ((deps gnus-newsgroup-dependencies)
3902         found header)
3903     (prog1
3904         (save-excursion
3905           (set-buffer nntp-server-buffer)
3906           (let ((case-fold-search nil))
3907             (goto-char (point-min))
3908             (while (and (not found)
3909                         (search-forward id nil t))
3910               (beginning-of-line)
3911               (setq found (looking-at
3912                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3913                                    (regexp-quote id))))
3914               (or found (beginning-of-line 2)))
3915             (when found
3916               (beginning-of-line)
3917               (and
3918                (setq header (gnus-nov-parse-line
3919                              (read (current-buffer)) deps))
3920                (gnus-parent-id (mail-header-references header))))))
3921       (when header
3922         (let ((number (mail-header-number header)))
3923           (push number gnus-newsgroup-limit)
3924           (push header gnus-newsgroup-headers)
3925           (if (memq number gnus-newsgroup-unselected)
3926               (progn
3927                 (setq gnus-newsgroup-unreads
3928                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3929                                                number))
3930                 (setq gnus-newsgroup-unselected
3931                       (delq number gnus-newsgroup-unselected)))
3932             (push number gnus-newsgroup-ancient)))))))
3933
3934 (defun gnus-build-all-threads ()
3935   "Read all the headers."
3936   (let ((gnus-summary-ignore-duplicates t)
3937         (mail-parse-charset gnus-newsgroup-charset)
3938         (dependencies gnus-newsgroup-dependencies)
3939         header article)
3940     (save-excursion
3941       (set-buffer nntp-server-buffer)
3942       (let ((case-fold-search nil))
3943         (goto-char (point-min))
3944         (while (not (eobp))
3945           (ignore-errors
3946             (setq article (read (current-buffer))
3947                   header (gnus-nov-parse-line article dependencies)))
3948           (when header
3949             (save-excursion
3950               (set-buffer gnus-summary-buffer)
3951               (push header gnus-newsgroup-headers)
3952               (if (memq (setq article (mail-header-number header))
3953                         gnus-newsgroup-unselected)
3954                   (progn
3955                     (setq gnus-newsgroup-unreads
3956                           (gnus-add-to-sorted-list
3957                            gnus-newsgroup-unreads article))
3958                     (setq gnus-newsgroup-unselected
3959                           (delq article gnus-newsgroup-unselected)))
3960                 (push article gnus-newsgroup-ancient)))
3961             (forward-line 1)))))))
3962
3963 (defun gnus-summary-update-article-line (article header)
3964   "Update the line for ARTICLE using HEADERS."
3965   (let* ((id (mail-header-id header))
3966          (thread (gnus-id-to-thread id)))
3967     (unless thread
3968       (error "Article in no thread"))
3969     ;; Update the thread.
3970     (setcar thread header)
3971     (gnus-summary-goto-subject article)
3972     (let* ((datal (gnus-data-find-list article))
3973            (data (car datal))
3974            (length (when (cdr datal)
3975                      (- (gnus-data-pos data)
3976                         (gnus-data-pos (cadr datal)))))
3977            (buffer-read-only nil)
3978            (level (gnus-summary-thread-level)))
3979       (gnus-delete-line)
3980       (gnus-summary-insert-line
3981        header level nil (gnus-article-mark article)
3982        (memq article gnus-newsgroup-replied)
3983        (memq article gnus-newsgroup-expirable)
3984        ;; Only insert the Subject string when it's different
3985        ;; from the previous Subject string.
3986        (if (and
3987             gnus-show-threads
3988             (gnus-subject-equal
3989              (condition-case ()
3990                  (mail-header-subject
3991                   (gnus-data-header
3992                    (cadr
3993                     (gnus-data-find-list
3994                      article
3995                      (gnus-data-list t)))))
3996                ;; Error on the side of excessive subjects.
3997                (error ""))
3998              (mail-header-subject header)))
3999            ""
4000          (mail-header-subject header))
4001        nil (cdr (assq article gnus-newsgroup-scored))
4002        (memq article gnus-newsgroup-processable))
4003       (when length
4004         (gnus-data-update-list
4005          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4006
4007 (defun gnus-summary-update-article (article &optional iheader)
4008   "Update ARTICLE in the summary buffer."
4009   (set-buffer gnus-summary-buffer)
4010   (let* ((header (gnus-summary-article-header article))
4011          (id (mail-header-id header))
4012          (data (gnus-data-find article))
4013          (thread (gnus-id-to-thread id))
4014          (references (mail-header-references header))
4015          (parent
4016           (gnus-id-to-thread
4017            (or (gnus-parent-id
4018                 (when (and references
4019                            (not (equal "" references)))
4020                   references))
4021                "none")))
4022          (buffer-read-only nil)
4023          (old (car thread)))
4024     (when thread
4025       (unless iheader
4026         (setcar thread nil)
4027         (when parent
4028           (delq thread parent)))
4029       (if (gnus-summary-insert-subject id header)
4030           ;; Set the (possibly) new article number in the data structure.
4031           (gnus-data-set-number data (gnus-id-to-article id))
4032         (setcar thread old)
4033         nil))))
4034
4035 (defun gnus-rebuild-thread (id &optional line)
4036   "Rebuild the thread containing ID.
4037 If LINE, insert the rebuilt thread starting on line LINE."
4038   (let ((buffer-read-only nil)
4039         old-pos current thread data)
4040     (if (not gnus-show-threads)
4041         (setq thread (list (car (gnus-id-to-thread id))))
4042       ;; Get the thread this article is part of.
4043       (setq thread (gnus-remove-thread id)))
4044     (setq old-pos (gnus-point-at-bol))
4045     (setq current (save-excursion
4046                     (and (re-search-backward "[\r\n]" nil t)
4047                          (gnus-summary-article-number))))
4048     ;; If this is a gathered thread, we have to go some re-gathering.
4049     (when (stringp (car thread))
4050       (let ((subject (car thread))
4051             roots thr)
4052         (setq thread (cdr thread))
4053         (while thread
4054           (unless (memq (setq thr (gnus-id-to-thread
4055                                    (gnus-root-id
4056                                     (mail-header-id (caar thread)))))
4057                         roots)
4058             (push thr roots))
4059           (setq thread (cdr thread)))
4060         ;; We now have all (unique) roots.
4061         (if (= (length roots) 1)
4062             ;; All the loose roots are now one solid root.
4063             (setq thread (car roots))
4064           (setq thread (cons subject (gnus-sort-threads roots))))))
4065     (let (threads)
4066       ;; We then insert this thread into the summary buffer.
4067       (when line
4068         (goto-char (point-min))
4069         (forward-line (1- line)))
4070       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4071         (if gnus-show-threads
4072             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4073           (gnus-summary-prepare-unthreaded thread))
4074         (setq data (nreverse gnus-newsgroup-data))
4075         (setq threads gnus-newsgroup-threads))
4076       ;; We splice the new data into the data structure.
4077       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4078       ;;!!! then we want to insert at the beginning of the buffer.
4079       ;;!!! That happens to be true with Gnus now, but that may
4080       ;;!!! change in the future.  Perhaps.
4081       (gnus-data-enter-list
4082        (if line nil current) data (- (point) old-pos))
4083       (setq gnus-newsgroup-threads
4084             (nconc threads gnus-newsgroup-threads))
4085       (gnus-data-compute-positions))))
4086
4087 (defun gnus-number-to-header (number)
4088   "Return the header for article NUMBER."
4089   (let ((headers gnus-newsgroup-headers))
4090     (while (and headers
4091                 (not (= number (mail-header-number (car headers)))))
4092       (pop headers))
4093     (when headers
4094       (car headers))))
4095
4096 (defun gnus-parent-headers (in-headers &optional generation)
4097   "Return the headers of the GENERATIONeth parent of HEADERS."
4098   (unless generation
4099     (setq generation 1))
4100   (let ((parent t)
4101         (headers in-headers)
4102         references)
4103     (while (and parent
4104                 (not (zerop generation))
4105                 (setq references (mail-header-references headers)))
4106       (setq headers (if (and references
4107                              (setq parent (gnus-parent-id references)))
4108                         (car (gnus-id-to-thread parent))
4109                       nil))
4110       (decf generation))
4111     (and (not (eq headers in-headers))
4112          headers)))
4113
4114 (defun gnus-id-to-thread (id)
4115   "Return the (sub-)thread where ID appears."
4116   (gnus-gethash id gnus-newsgroup-dependencies))
4117
4118 (defun gnus-id-to-article (id)
4119   "Return the article number of ID."
4120   (let ((thread (gnus-id-to-thread id)))
4121     (when (and thread
4122                (car thread))
4123       (mail-header-number (car thread)))))
4124
4125 (defun gnus-id-to-header (id)
4126   "Return the article headers of ID."
4127   (car (gnus-id-to-thread id)))
4128
4129 (defun gnus-article-displayed-root-p (article)
4130   "Say whether ARTICLE is a root(ish) article."
4131   (let ((level (gnus-summary-thread-level article))
4132         (refs (mail-header-references  (gnus-summary-article-header article)))
4133         particle)
4134     (cond
4135      ((null level) nil)
4136      ((zerop level) t)
4137      ((null refs) t)
4138      ((null (gnus-parent-id refs)) t)
4139      ((and (= 1 level)
4140            (null (setq particle (gnus-id-to-article
4141                                  (gnus-parent-id refs))))
4142            (null (gnus-summary-thread-level particle)))))))
4143
4144 (defun gnus-root-id (id)
4145   "Return the id of the root of the thread where ID appears."
4146   (let (last-id prev)
4147     (while (and id (setq prev (car (gnus-id-to-thread id))))
4148       (setq last-id id
4149             id (gnus-parent-id (mail-header-references prev))))
4150     last-id))
4151
4152 (defun gnus-articles-in-thread (thread)
4153   "Return the list of articles in THREAD."
4154   (cons (mail-header-number (car thread))
4155         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4156
4157 (defun gnus-remove-thread (id &optional dont-remove)
4158   "Remove the thread that has ID in it."
4159   (let (headers thread last-id)
4160     ;; First go up in this thread until we find the root.
4161     (setq last-id (gnus-root-id id)
4162           headers (message-flatten-list (gnus-id-to-thread last-id)))
4163     ;; We have now found the real root of this thread.  It might have
4164     ;; been gathered into some loose thread, so we have to search
4165     ;; through the threads to find the thread we wanted.
4166     (let ((threads gnus-newsgroup-threads)
4167           sub)
4168       (while threads
4169         (setq sub (car threads))
4170         (if (stringp (car sub))
4171             ;; This is a gathered thread, so we look at the roots
4172             ;; below it to find whether this article is in this
4173             ;; gathered root.
4174             (progn
4175               (setq sub (cdr sub))
4176               (while sub
4177                 (when (member (caar sub) headers)
4178                   (setq thread (car threads)
4179                         threads nil
4180                         sub nil))
4181                 (setq sub (cdr sub))))
4182           ;; It's an ordinary thread, so we check it.
4183           (when (eq (car sub) (car headers))
4184             (setq thread sub
4185                   threads nil)))
4186         (setq threads (cdr threads)))
4187       ;; If this article is in no thread, then it's a root.
4188       (if thread
4189           (unless dont-remove
4190             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4191         (setq thread (gnus-id-to-thread last-id)))
4192       (when thread
4193         (prog1
4194             thread                      ; We return this thread.
4195           (unless dont-remove
4196             (if (stringp (car thread))
4197                 (progn
4198                   ;; If we use dummy roots, then we have to remove the
4199                   ;; dummy root as well.
4200                   (when (eq gnus-summary-make-false-root 'dummy)
4201                     ;; We go to the dummy root by going to
4202                     ;; the first sub-"thread", and then one line up.
4203                     (gnus-summary-goto-article
4204                      (mail-header-number (caadr thread)))
4205                     (forward-line -1)
4206                     (gnus-delete-line)
4207                     (gnus-data-compute-positions))
4208                   (setq thread (cdr thread))
4209                   (while thread
4210                     (gnus-remove-thread-1 (car thread))
4211                     (setq thread (cdr thread))))
4212               (gnus-remove-thread-1 thread))))))))
4213
4214 (defun gnus-remove-thread-1 (thread)
4215   "Remove the thread THREAD recursively."
4216   (let ((number (mail-header-number (pop thread)))
4217         d)
4218     (setq thread (reverse thread))
4219     (while thread
4220       (gnus-remove-thread-1 (pop thread)))
4221     (when (setq d (gnus-data-find number))
4222       (goto-char (gnus-data-pos d))
4223       (gnus-summary-show-thread)
4224       (gnus-data-remove
4225        number
4226        (- (gnus-point-at-bol)
4227           (prog1
4228               (1+ (gnus-point-at-eol))
4229             (gnus-delete-line)))))))
4230
4231 (defun gnus-sort-threads-1 (threads func)
4232   (sort (mapcar (lambda (thread)
4233                   (cons (car thread)
4234                         (and (cdr thread)
4235                              (gnus-sort-threads-1 (cdr thread) func))))
4236                 threads) func))
4237
4238 (defun gnus-sort-threads (threads)
4239   "Sort THREADS."
4240   (if (not gnus-thread-sort-functions)
4241       threads
4242     (gnus-message 8 "Sorting threads...")
4243     (prog1
4244         (gnus-sort-threads-1
4245          threads
4246          (gnus-make-sort-function gnus-thread-sort-functions))
4247       (gnus-message 8 "Sorting threads...done"))))
4248
4249 (defun gnus-sort-articles (articles)
4250   "Sort ARTICLES."
4251   (when gnus-article-sort-functions
4252     (gnus-message 7 "Sorting articles...")
4253     (prog1
4254         (setq gnus-newsgroup-headers
4255               (sort articles (gnus-make-sort-function
4256                               gnus-article-sort-functions)))
4257       (gnus-message 7 "Sorting articles...done"))))
4258
4259 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4260 (defmacro gnus-thread-header (thread)
4261   "Return header of first article in THREAD.
4262 Note that THREAD must never, ever be anything else than a variable -
4263 using some other form will lead to serious barfage."
4264   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4265   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4266   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4267         (vector thread) 2))
4268
4269 (defsubst gnus-article-sort-by-number (h1 h2)
4270   "Sort articles by article number."
4271   (< (mail-header-number h1)
4272      (mail-header-number h2)))
4273
4274 (defun gnus-thread-sort-by-number (h1 h2)
4275   "Sort threads by root article number."
4276   (gnus-article-sort-by-number
4277    (gnus-thread-header h1) (gnus-thread-header h2)))
4278
4279 (defsubst gnus-article-sort-by-random (h1 h2)
4280   "Sort articles by article number."
4281   (zerop (random 2)))
4282
4283 (defun gnus-thread-sort-by-random (h1 h2)
4284   "Sort threads by root article number."
4285   (gnus-article-sort-by-random
4286    (gnus-thread-header h1) (gnus-thread-header h2)))
4287
4288 (defsubst gnus-article-sort-by-lines (h1 h2)
4289   "Sort articles by article Lines header."
4290   (< (mail-header-lines h1)
4291      (mail-header-lines h2)))
4292
4293 (defun gnus-thread-sort-by-lines (h1 h2)
4294   "Sort threads by root article Lines header."
4295   (gnus-article-sort-by-lines
4296    (gnus-thread-header h1) (gnus-thread-header h2)))
4297
4298 (defsubst gnus-article-sort-by-chars (h1 h2)
4299   "Sort articles by octet length."
4300   (< (mail-header-chars h1)
4301      (mail-header-chars h2)))
4302
4303 (defun gnus-thread-sort-by-chars (h1 h2)
4304   "Sort threads by root article octet length."
4305   (gnus-article-sort-by-chars
4306    (gnus-thread-header h1) (gnus-thread-header h2)))
4307
4308 (defsubst gnus-article-sort-by-author (h1 h2)
4309   "Sort articles by root author."
4310   (string-lessp
4311    (let ((addr (car (mime-entity-read-field h1 'From))))
4312      (or (std11-full-name-string addr)
4313          (std11-address-string addr)
4314          ""))
4315    (let ((addr (car (mime-entity-read-field h2 'From))))
4316      (or (std11-full-name-string addr)
4317          (std11-address-string addr)
4318          ""))
4319    ))
4320
4321 (defun gnus-thread-sort-by-author (h1 h2)
4322   "Sort threads by root author."
4323   (gnus-article-sort-by-author
4324    (gnus-thread-header h1)  (gnus-thread-header h2)))
4325
4326 (defsubst gnus-article-sort-by-subject (h1 h2)
4327   "Sort articles by root subject."
4328   (string-lessp
4329    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4330    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4331
4332 (defun gnus-thread-sort-by-subject (h1 h2)
4333   "Sort threads by root subject."
4334   (gnus-article-sort-by-subject
4335    (gnus-thread-header h1) (gnus-thread-header h2)))
4336
4337 (defsubst gnus-article-sort-by-date (h1 h2)
4338   "Sort articles by root article date."
4339   (time-less-p
4340    (gnus-date-get-time (mail-header-date h1))
4341    (gnus-date-get-time (mail-header-date h2))))
4342
4343 (defun gnus-thread-sort-by-date (h1 h2)
4344   "Sort threads by root article date."
4345   (gnus-article-sort-by-date
4346    (gnus-thread-header h1) (gnus-thread-header h2)))
4347
4348 (defsubst gnus-article-sort-by-score (h1 h2)
4349   "Sort articles by root article score.
4350 Unscored articles will be counted as having a score of zero."
4351   (> (or (cdr (assq (mail-header-number h1)
4352                     gnus-newsgroup-scored))
4353          gnus-summary-default-score 0)
4354      (or (cdr (assq (mail-header-number h2)
4355                     gnus-newsgroup-scored))
4356          gnus-summary-default-score 0)))
4357
4358 (defun gnus-thread-sort-by-score (h1 h2)
4359   "Sort threads by root article score."
4360   (gnus-article-sort-by-score
4361    (gnus-thread-header h1) (gnus-thread-header h2)))
4362
4363 (defun gnus-thread-sort-by-total-score (h1 h2)
4364   "Sort threads by the sum of all scores in the thread.
4365 Unscored articles will be counted as having a score of zero."
4366   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4367
4368 (defun gnus-thread-total-score (thread)
4369   ;; This function find the total score of THREAD.
4370   (cond
4371    ((null thread)
4372     0)
4373    ((consp thread)
4374     (if (stringp (car thread))
4375         (apply gnus-thread-score-function 0
4376                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4377       (gnus-thread-total-score-1 thread)))
4378    (t
4379     (gnus-thread-total-score-1 (list thread)))))
4380
4381 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4382   "Sort threads such that the thread with the most recently arrived article comes first."
4383   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4384
4385 (defun gnus-thread-highest-number (thread)
4386   "Return the highest article number in THREAD."
4387   (apply 'max (mapcar (lambda (header)
4388                         (mail-header-number header))
4389                       (message-flatten-list thread))))
4390
4391 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4392   "Sort threads such that the thread with the most recently dated article comes first."
4393   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4394
4395 (defun gnus-thread-latest-date (thread)
4396   "Return the highest article date in THREAD."
4397   (let ((previous-time 0))
4398     (apply 'max (mapcar
4399                  (lambda (header)
4400                    (setq previous-time
4401                          (time-to-seconds
4402                           (mail-header-parse-date
4403                            (condition-case ()
4404                                (mail-header-date header)
4405                              (error previous-time))))))
4406                  (sort
4407                   (message-flatten-list thread)
4408                   (lambda (h1 h2)
4409                     (< (mail-header-number h1)
4410                        (mail-header-number h2))))))))
4411
4412 (defun gnus-thread-total-score-1 (root)
4413   ;; This function find the total score of the thread below ROOT.
4414   (setq root (car root))
4415   (apply gnus-thread-score-function
4416          (or (append
4417               (mapcar 'gnus-thread-total-score
4418                       (cdr (gnus-id-to-thread (mail-header-id root))))
4419               (when (> (mail-header-number root) 0)
4420                 (list (or (cdr (assq (mail-header-number root)
4421                                      gnus-newsgroup-scored))
4422                           gnus-summary-default-score 0))))
4423              (list gnus-summary-default-score)
4424              '(0))))
4425
4426 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4427 (defvar gnus-tmp-prev-subject nil)
4428 (defvar gnus-tmp-false-parent nil)
4429 (defvar gnus-tmp-root-expunged nil)
4430 (defvar gnus-tmp-dummy-line nil)
4431
4432 (eval-when-compile (defvar gnus-tmp-header))
4433 (defun gnus-extra-header (type &optional header)
4434   "Return the extra header of TYPE."
4435   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4436       ""))
4437
4438 (defvar gnus-tmp-thread-tree-header-string "")
4439
4440 (defcustom gnus-sum-thread-tree-root "> "
4441   "With %B spec, used for the root of a thread.
4442 If nil, use subject instead."
4443   :type 'string
4444   :group 'gnus-thread)
4445 (defcustom gnus-sum-thread-tree-single-indent ""
4446   "With %B spec, used for a thread with just one message.
4447 If nil, use subject instead."
4448   :type 'string
4449   :group 'gnus-thread)
4450 (defcustom gnus-sum-thread-tree-vertical "| "
4451   "With %B spec, used for drawing a vertical line."
4452   :type 'string
4453   :group 'gnus-thread)
4454 (defcustom gnus-sum-thread-tree-indent "  "
4455   "With %B spec, used for indenting."
4456   :type 'string
4457   :group 'gnus-thread)
4458 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4459   "With %B spec, used for a leaf with brothers."
4460   :type 'string
4461   :group 'gnus-thread)
4462 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4463   "With %B spec, used for a leaf without brothers."
4464   :type 'string
4465   :group 'gnus-thread)
4466
4467 (defun gnus-summary-prepare-threads (threads)
4468   "Prepare summary buffer from THREADS and indentation LEVEL.
4469 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4470 or a straight list of headers."
4471   (gnus-message 7 "Generating summary...")
4472
4473   (setq gnus-newsgroup-threads threads)
4474   (beginning-of-line)
4475
4476   (let ((gnus-tmp-level 0)
4477         (default-score (or gnus-summary-default-score 0))
4478         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4479         thread number subject stack state gnus-tmp-gathered beg-match
4480         new-roots gnus-tmp-new-adopts thread-end simp-subject
4481         gnus-tmp-header gnus-tmp-unread
4482         gnus-tmp-replied gnus-tmp-subject-or-nil
4483         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4484         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4485         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4486         tree-stack)
4487
4488     (setq gnus-tmp-prev-subject nil)
4489
4490     (if (vectorp (car threads))
4491         ;; If this is a straight (sic) list of headers, then a
4492         ;; threaded summary display isn't required, so we just create
4493         ;; an unthreaded one.
4494         (gnus-summary-prepare-unthreaded threads)
4495
4496       ;; Do the threaded display.
4497
4498       (while (or threads stack gnus-tmp-new-adopts new-roots)
4499
4500         (if (and (= gnus-tmp-level 0)
4501                  (or (not stack)
4502                      (= (caar stack) 0))
4503                  (not gnus-tmp-false-parent)
4504                  (or gnus-tmp-new-adopts new-roots))
4505             (if gnus-tmp-new-adopts
4506                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4507                       thread (list (car gnus-tmp-new-adopts))
4508                       gnus-tmp-header (caar thread)
4509                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4510               (when new-roots
4511                 (setq thread (list (car new-roots))
4512                       gnus-tmp-header (caar thread)
4513                       new-roots (cdr new-roots))))
4514
4515           (if threads
4516               ;; If there are some threads, we do them before the
4517               ;; threads on the stack.
4518               (setq thread threads
4519                     gnus-tmp-header (caar thread))
4520             ;; There were no current threads, so we pop something off
4521             ;; the stack.
4522             (setq state (car stack)
4523                   gnus-tmp-level (car state)
4524                   tree-stack (cadr state)
4525                   thread (caddr state)
4526                   stack (cdr stack)
4527                   gnus-tmp-header (caar thread))))
4528
4529         (setq gnus-tmp-false-parent nil)
4530         (setq gnus-tmp-root-expunged nil)
4531         (setq thread-end nil)
4532
4533         (if (stringp gnus-tmp-header)
4534             ;; The header is a dummy root.
4535             (cond
4536              ((eq gnus-summary-make-false-root 'adopt)
4537               ;; We let the first article adopt the rest.
4538               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4539                                                (cddar thread)))
4540               (setq gnus-tmp-gathered
4541                     (nconc (mapcar
4542                             (lambda (h) (mail-header-number (car h)))
4543                             (cddar thread))
4544                            gnus-tmp-gathered))
4545               (setq thread (cons (list (caar thread)
4546                                        (cadar thread))
4547                                  (cdr thread)))
4548               (setq gnus-tmp-level -1
4549                     gnus-tmp-false-parent t))
4550              ((eq gnus-summary-make-false-root 'empty)
4551               ;; We print adopted articles with empty subject fields.
4552               (setq gnus-tmp-gathered
4553                     (nconc (mapcar
4554                             (lambda (h) (mail-header-number (car h)))
4555                             (cddar thread))
4556                            gnus-tmp-gathered))
4557               (setq gnus-tmp-level -1))
4558              ((eq gnus-summary-make-false-root 'dummy)
4559               ;; We remember that we probably want to output a dummy
4560               ;; root.
4561               (setq gnus-tmp-dummy-line gnus-tmp-header)
4562               (setq gnus-tmp-prev-subject gnus-tmp-header))
4563              (t
4564               ;; We do not make a root for the gathered
4565               ;; sub-threads at all.
4566               (setq gnus-tmp-level -1)))
4567
4568           (setq number (mail-header-number gnus-tmp-header)
4569                 subject (mail-header-subject gnus-tmp-header)
4570                 simp-subject (gnus-simplify-subject-fully subject))
4571
4572           (cond
4573            ;; If the thread has changed subject, we might want to make
4574            ;; this subthread into a root.
4575            ((and (null gnus-thread-ignore-subject)
4576                  (not (zerop gnus-tmp-level))
4577                  gnus-tmp-prev-subject
4578                  (not (string= gnus-tmp-prev-subject simp-subject)))
4579             (setq new-roots (nconc new-roots (list (car thread)))
4580                   thread-end t
4581                   gnus-tmp-header nil))
4582            ;; If the article lies outside the current limit,
4583            ;; then we do not display it.
4584            ((not (memq number gnus-newsgroup-limit))
4585             (setq gnus-tmp-gathered
4586                   (nconc (mapcar
4587                           (lambda (h) (mail-header-number (car h)))
4588                           (cdar thread))
4589                          gnus-tmp-gathered))
4590             (setq gnus-tmp-new-adopts (if (cdar thread)
4591                                           (append gnus-tmp-new-adopts
4592                                                   (cdar thread))
4593                                         gnus-tmp-new-adopts)
4594                   thread-end t
4595                   gnus-tmp-header nil)
4596             (when (zerop gnus-tmp-level)
4597               (setq gnus-tmp-root-expunged t)))
4598            ;; Perhaps this article is to be marked as read?
4599            ((and gnus-summary-mark-below
4600                  (< (or (cdr (assq number gnus-newsgroup-scored))
4601                         default-score)
4602                     gnus-summary-mark-below)
4603                  ;; Don't touch sparse articles.
4604                  (not (gnus-summary-article-sparse-p number))
4605                  (not (gnus-summary-article-ancient-p number)))
4606             (setq gnus-newsgroup-unreads
4607                   (delq number gnus-newsgroup-unreads))
4608             (if gnus-newsgroup-auto-expire
4609                 (setq gnus-newsgroup-expirable
4610                       (gnus-add-to-sorted-list
4611                        gnus-newsgroup-expirable number))
4612               (push (cons number gnus-low-score-mark)
4613                     gnus-newsgroup-reads))))
4614
4615           (when gnus-tmp-header
4616             ;; We may have an old dummy line to output before this
4617             ;; article.
4618             (when (and gnus-tmp-dummy-line
4619                        (gnus-subject-equal
4620                         gnus-tmp-dummy-line
4621                         (mail-header-subject gnus-tmp-header)))
4622               (gnus-summary-insert-dummy-line
4623                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4624               (setq gnus-tmp-dummy-line nil))
4625
4626             ;; Compute the mark.
4627             (setq gnus-tmp-unread (gnus-article-mark number))
4628
4629             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4630                                   gnus-tmp-header gnus-tmp-level)
4631                   gnus-newsgroup-data)
4632
4633             ;; Actually insert the line.
4634             (setq
4635              gnus-tmp-subject-or-nil
4636              (cond
4637               ((and gnus-thread-ignore-subject
4638                     gnus-tmp-prev-subject
4639                     (not (string= gnus-tmp-prev-subject simp-subject)))
4640                subject)
4641               ((zerop gnus-tmp-level)
4642                (if (and (eq gnus-summary-make-false-root 'empty)
4643                         (memq number gnus-tmp-gathered)
4644                         gnus-tmp-prev-subject
4645                         (string= gnus-tmp-prev-subject simp-subject))
4646                    gnus-summary-same-subject
4647                  subject))
4648               (t gnus-summary-same-subject)))
4649             (if (and (eq gnus-summary-make-false-root 'adopt)
4650                      (= gnus-tmp-level 1)
4651                      (memq number gnus-tmp-gathered))
4652                 (setq gnus-tmp-opening-bracket ?\<
4653                       gnus-tmp-closing-bracket ?\>)
4654               (setq gnus-tmp-opening-bracket ?\[
4655                     gnus-tmp-closing-bracket ?\]))
4656             (setq
4657              gnus-tmp-indentation
4658              (aref gnus-thread-indent-array gnus-tmp-level)
4659              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4660              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4661                                 gnus-summary-default-score 0)
4662              gnus-tmp-score-char
4663              (if (or (null gnus-summary-default-score)
4664                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4665                          gnus-summary-zcore-fuzz))
4666                  ?\ ;;;Whitespace
4667                (if (< gnus-tmp-score gnus-summary-default-score)
4668                    gnus-score-below-mark gnus-score-over-mark))
4669              gnus-tmp-replied
4670              (cond ((memq number gnus-newsgroup-processable)
4671                     gnus-process-mark)
4672                    ((memq number gnus-newsgroup-cached)
4673                     gnus-cached-mark)
4674                    ((memq number gnus-newsgroup-replied)
4675                     gnus-replied-mark)
4676                    ((memq number gnus-newsgroup-forwarded)
4677                     gnus-forwarded-mark)
4678                    ((memq number gnus-newsgroup-saved)
4679                     gnus-saved-mark)
4680                    ((memq number gnus-newsgroup-recent)
4681                     gnus-recent-mark)
4682                    ((memq number gnus-newsgroup-unseen)
4683                     gnus-unseen-mark)
4684                    (t gnus-no-mark))
4685              gnus-tmp-from (mail-header-from gnus-tmp-header)
4686              gnus-tmp-name
4687              (cond
4688               ((string-match "<[^>]+> *$" gnus-tmp-from)
4689                (setq beg-match (match-beginning 0))
4690                (or (and (string-match "^\".+\"" gnus-tmp-from)
4691                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4692                    (substring gnus-tmp-from 0 beg-match)))
4693               ((string-match "(.+)" gnus-tmp-from)
4694                (substring gnus-tmp-from
4695                           (1+ (match-beginning 0)) (1- (match-end 0))))
4696               (t gnus-tmp-from))
4697              gnus-tmp-thread-tree-header-string
4698              (cond
4699               ((not gnus-show-threads) "")
4700               ((zerop gnus-tmp-level)
4701                (if (cdar thread)
4702                    (or gnus-sum-thread-tree-root subject)
4703                  (or gnus-sum-thread-tree-single-indent subject)))
4704               (t
4705                (concat (apply 'concat
4706                               (mapcar (lambda (item)
4707                                         (if (= item 1)
4708                                             gnus-sum-thread-tree-vertical
4709                                           gnus-sum-thread-tree-indent))
4710                                       (cdr (reverse tree-stack))))
4711                        (if (nth 1 thread)
4712                            gnus-sum-thread-tree-leaf-with-other
4713                          gnus-sum-thread-tree-single-leaf)))))
4714             (when (string= gnus-tmp-name "")
4715               (setq gnus-tmp-name gnus-tmp-from))
4716             (unless (numberp gnus-tmp-lines)
4717               (setq gnus-tmp-lines -1))
4718             (if (= gnus-tmp-lines -1)
4719                 (setq gnus-tmp-lines "?")
4720               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4721             (gnus-put-text-property
4722              (point)
4723              (progn (eval gnus-summary-line-format-spec) (point))
4724              'gnus-number number)
4725             (when gnus-visual-p
4726               (forward-line -1)
4727               (gnus-run-hooks 'gnus-summary-update-hook)
4728               (forward-line 1))
4729
4730             (setq gnus-tmp-prev-subject simp-subject)))
4731
4732         (when (nth 1 thread)
4733           (push (list (max 0 gnus-tmp-level)
4734                       (copy-list tree-stack)
4735                       (nthcdr 1 thread))
4736                 stack))
4737         (push (if (nth 1 thread) 1 0) tree-stack)
4738         (incf gnus-tmp-level)
4739         (setq threads (if thread-end nil (cdar thread)))
4740         (unless threads
4741           (setq gnus-tmp-level 0)))))
4742   (gnus-message 7 "Generating summary...done"))
4743
4744 (defun gnus-summary-prepare-unthreaded (headers)
4745   "Generate an unthreaded summary buffer based on HEADERS."
4746   (let (header number mark)
4747
4748     (beginning-of-line)
4749
4750     (while headers
4751       ;; We may have to root out some bad articles...
4752       (when (memq (setq number (mail-header-number
4753                                 (setq header (pop headers))))
4754                   gnus-newsgroup-limit)
4755         ;; Mark article as read when it has a low score.
4756         (when (and gnus-summary-mark-below
4757                    (< (or (cdr (assq number gnus-newsgroup-scored))
4758                           gnus-summary-default-score 0)
4759                       gnus-summary-mark-below)
4760                    (not (gnus-summary-article-ancient-p number)))
4761           (setq gnus-newsgroup-unreads
4762                 (delq number gnus-newsgroup-unreads))
4763           (if gnus-newsgroup-auto-expire
4764               (push number gnus-newsgroup-expirable)
4765             (push (cons number gnus-low-score-mark)
4766                   gnus-newsgroup-reads)))
4767
4768         (setq mark (gnus-article-mark number))
4769         (push (gnus-data-make number mark (1+ (point)) header 0)
4770               gnus-newsgroup-data)
4771         (gnus-summary-insert-line
4772          header 0 number
4773          mark (memq number gnus-newsgroup-replied)
4774          (memq number gnus-newsgroup-expirable)
4775          (mail-header-subject header) nil
4776          (cdr (assq number gnus-newsgroup-scored))
4777          (memq number gnus-newsgroup-processable))))))
4778
4779 (defun gnus-summary-remove-list-identifiers ()
4780   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4781   (let ((regexp (if (consp gnus-list-identifiers)
4782                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4783                   gnus-list-identifiers))
4784         changed subject)
4785     (when regexp
4786       (dolist (header gnus-newsgroup-headers)
4787         (setq subject (mail-header-subject header)
4788               changed nil)
4789         (while (string-match
4790                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4791                 subject)
4792           (setq subject
4793                 (concat (substring subject 0 (match-beginning 2))
4794                         (substring subject (match-end 0)))
4795                 changed t))
4796         (when (and changed
4797                    (string-match
4798                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4799           (setq subject
4800                 (concat (substring subject 0 (match-beginning 1))
4801                         (substring subject (match-end 1)))))
4802         (when changed
4803           (mail-header-set-subject header subject))))))
4804
4805 (defun gnus-fetch-headers (articles)
4806   "Fetch headers of ARTICLES."
4807   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4808     (gnus-message 5 "Fetching headers for %s..." name)
4809     (prog1
4810         (if (eq 'nov
4811                 (setq gnus-headers-retrieved-by
4812                       (gnus-retrieve-headers
4813                        articles gnus-newsgroup-name
4814                        ;; We might want to fetch old headers, but
4815                        ;; not if there is only 1 article.
4816                        (and (or (and
4817                                  (not (eq gnus-fetch-old-headers 'some))
4818                                  (not (numberp gnus-fetch-old-headers)))
4819                                 (> (length articles) 1))
4820                             gnus-fetch-old-headers))))
4821             (gnus-get-newsgroup-headers-xover
4822              articles nil nil gnus-newsgroup-name t)
4823           (gnus-get-newsgroup-headers))
4824       (gnus-message 5 "Fetching headers for %s...done" name))))
4825
4826 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4827   "Select newsgroup GROUP.
4828 If READ-ALL is non-nil, all articles in the group are selected.
4829 If SELECT-ARTICLES, only select those articles from GROUP."
4830   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4831          ;;!!! Dirty hack; should be removed.
4832          (gnus-summary-ignore-duplicates
4833           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4834               t
4835             gnus-summary-ignore-duplicates))
4836          (info (nth 2 entry))
4837          articles fetched-articles cached)
4838
4839     (unless (gnus-check-server
4840              (set (make-local-variable 'gnus-current-select-method)
4841                   (gnus-find-method-for-group group)))
4842       (error "Couldn't open server"))
4843
4844     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4845         (gnus-activate-group group)     ; Or we can activate it...
4846         (progn                          ; Or we bug out.
4847           (when (equal major-mode 'gnus-summary-mode)
4848             (kill-buffer (current-buffer)))
4849           (error "Couldn't activate group %s: %s"
4850                  group (gnus-status-message group))))
4851
4852     (unless (gnus-request-group group t)
4853       (when (equal major-mode 'gnus-summary-mode)
4854         (kill-buffer (current-buffer)))
4855       (error "Couldn't request group %s: %s"
4856              group (gnus-status-message group)))
4857
4858     (setq gnus-newsgroup-name group
4859           gnus-newsgroup-unselected nil
4860           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4861
4862     (let ((display (gnus-group-find-parameter group 'display)))
4863       (setq gnus-newsgroup-display
4864             (cond
4865              ((not (zerop (or (car-safe read-all) 0)))
4866               ;; The user entered the group with C-u SPC/RET, let's show
4867               ;; all articles.
4868               'gnus-not-ignore)
4869              ((eq display 'all)
4870               'gnus-not-ignore)
4871              ((arrayp display)
4872               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4873              ((numberp display)
4874               ;; The following is probably the "correct" solution, but
4875               ;; it makes Gnus fetch all headers and then limit the
4876               ;; articles (which is slow), so instead we hack the
4877               ;; select-articles parameter instead. -- Simon Josefsson
4878               ;; <jas@kth.se>
4879               ;;
4880               ;; (gnus-byte-compile
4881               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4882               ;;                         display)))))
4883               (setq select-articles
4884                     (gnus-uncompress-range
4885                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4886                              (if (> tmp 0)
4887                                  tmp
4888                                1))
4889                            (cdr (gnus-active group)))))
4890               nil)
4891              (t
4892               nil))))
4893
4894     (gnus-summary-setup-default-charset)
4895
4896     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4897     (when (gnus-virtual-group-p group)
4898       (setq cached gnus-newsgroup-cached))
4899
4900     (setq gnus-newsgroup-unreads
4901           (gnus-sorted-ndifference
4902            (gnus-sorted-ndifference gnus-newsgroup-unreads
4903                                     gnus-newsgroup-marked)
4904            gnus-newsgroup-dormant))
4905
4906     (setq gnus-newsgroup-processable nil)
4907
4908     (gnus-update-read-articles group gnus-newsgroup-unreads)
4909
4910     ;; Adjust and set lists of article marks.
4911     (when info
4912       (gnus-adjust-marked-articles info))
4913
4914     (if (setq articles select-articles)
4915         (setq gnus-newsgroup-unselected
4916               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4917       (setq articles (gnus-articles-to-read group read-all)))
4918
4919     (cond
4920      ((null articles)
4921       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4922       'quit)
4923      ((eq articles 0) nil)
4924      (t
4925       ;; Init the dependencies hash table.
4926       (setq gnus-newsgroup-dependencies
4927             (gnus-make-hashtable (length articles)))
4928       (gnus-set-global-variables)
4929       ;; Retrieve the headers and read them in.
4930       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4931
4932       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4933       (when cached
4934         (setq gnus-newsgroup-cached cached))
4935
4936       ;; Suppress duplicates?
4937       (when gnus-suppress-duplicates
4938         (gnus-dup-suppress-articles))
4939
4940       ;; Set the initial limit.
4941       (setq gnus-newsgroup-limit (copy-sequence articles))
4942       ;; Remove canceled articles from the list of unread articles.
4943       (setq fetched-articles
4944             (mapcar (lambda (headers) (mail-header-number headers))
4945                     gnus-newsgroup-headers))
4946       (setq gnus-newsgroup-articles fetched-articles)
4947       (setq gnus-newsgroup-unreads
4948             (gnus-sorted-nintersection
4949              gnus-newsgroup-unreads fetched-articles))
4950       (gnus-compute-unseen-list)
4951
4952       ;; Removed marked articles that do not exist.
4953       (gnus-update-missing-marks
4954        (gnus-sorted-difference articles fetched-articles))
4955       ;; We might want to build some more threads first.
4956       (when (and gnus-fetch-old-headers
4957                  (eq gnus-headers-retrieved-by 'nov))
4958         (if (eq gnus-fetch-old-headers 'invisible)
4959             (gnus-build-all-threads)
4960           (gnus-build-old-threads)))
4961       ;; Let the Gnus agent mark articles as read.
4962       (when gnus-agent
4963         (gnus-agent-get-undownloaded-list))
4964       ;; Remove list identifiers from subject
4965       (when gnus-list-identifiers
4966         (gnus-summary-remove-list-identifiers))
4967       ;; Check whether auto-expire is to be done in this group.
4968       (setq gnus-newsgroup-auto-expire
4969             (gnus-group-auto-expirable-p group))
4970       ;; Set up the article buffer now, if necessary.
4971       (unless gnus-single-article-buffer
4972         (gnus-article-setup-buffer))
4973       ;; First and last article in this newsgroup.
4974       (when gnus-newsgroup-headers
4975         (setq gnus-newsgroup-begin
4976               (mail-header-number (car gnus-newsgroup-headers))
4977               gnus-newsgroup-end
4978               (mail-header-number
4979                (gnus-last-element gnus-newsgroup-headers))))
4980       ;; GROUP is successfully selected.
4981       (or gnus-newsgroup-headers t)))))
4982
4983 (defun gnus-compute-unseen-list ()
4984   ;; The `seen' marks are treated specially.
4985   (if (not gnus-newsgroup-seen)
4986       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4987     (setq gnus-newsgroup-unseen
4988           (gnus-inverse-list-range-intersection
4989            gnus-newsgroup-articles gnus-newsgroup-seen))))
4990
4991 (defun gnus-summary-display-make-predicate (display)
4992   (require 'gnus-agent)
4993   (when (= (length display) 1)
4994     (setq display (car display)))
4995   (unless gnus-summary-display-cache
4996     (dolist (elem (append '((unread . unread)
4997                             (read . read)
4998                             (unseen . unseen))
4999                           gnus-article-mark-lists))
5000       (push (cons (cdr elem)
5001                   (gnus-byte-compile
5002                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5003             gnus-summary-display-cache)))
5004   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5005         (gnus-category-predicate-cache gnus-summary-display-cache))
5006     (gnus-get-predicate display)))
5007
5008 ;; Uses the dynamically bound `number' variable.
5009 (defvar number)
5010 (defun gnus-article-marked-p (type &optional article)
5011   (let ((article (or article number)))
5012     (cond
5013      ((eq type 'tick)
5014       (memq article gnus-newsgroup-marked))
5015      ((eq type 'spam)
5016       (memq article gnus-newsgroup-spam-marked))
5017      ((eq type 'unsend)
5018       (memq article gnus-newsgroup-unsendable))
5019      ((eq type 'undownload)
5020       (memq article gnus-newsgroup-undownloaded))
5021      ((eq type 'download)
5022       (memq article gnus-newsgroup-downloadable))
5023      ((eq type 'unread)
5024       (memq article gnus-newsgroup-unreads))
5025      ((eq type 'read)
5026       (memq article gnus-newsgroup-reads))
5027      ((eq type 'dormant)
5028       (memq article gnus-newsgroup-dormant) )
5029      ((eq type 'expire)
5030       (memq article gnus-newsgroup-expirable))
5031      ((eq type 'reply)
5032       (memq article gnus-newsgroup-replied))
5033      ((eq type 'killed)
5034       (memq article gnus-newsgroup-killed))
5035      ((eq type 'bookmark)
5036       (assq article gnus-newsgroup-bookmarks))
5037      ((eq type 'score)
5038       (assq article gnus-newsgroup-scored))
5039      ((eq type 'save)
5040       (memq article gnus-newsgroup-saved))
5041      ((eq type 'cache)
5042       (memq article gnus-newsgroup-cached))
5043      ((eq type 'forward)
5044       (memq article gnus-newsgroup-forwarded))
5045      ((eq type 'seen)
5046       (not (memq article gnus-newsgroup-unseen)))
5047      ((eq type 'recent)
5048       (memq article gnus-newsgroup-recent))
5049      (t t))))
5050
5051 (defun gnus-articles-to-read (group &optional read-all)
5052   "Find out what articles the user wants to read."
5053   (let* ((articles
5054           ;; Select all articles if `read-all' is non-nil, or if there
5055           ;; are no unread articles.
5056           (if (or read-all
5057                   (and (zerop (length gnus-newsgroup-marked))
5058                        (zerop (length gnus-newsgroup-unreads)))
5059                   ;; Fetch all if the predicate is non-nil.
5060                   gnus-newsgroup-display)
5061               ;; We want to select the headers for all the articles in
5062               ;; the group, so we select either all the active
5063               ;; articles in the group, or (if that's nil), the
5064               ;; articles in the cache.
5065               (or
5066                (gnus-uncompress-range (gnus-active group))
5067                (gnus-cache-articles-in-group group))
5068             ;; Select only the "normal" subset of articles.
5069             (gnus-sorted-nunion
5070              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5071              gnus-newsgroup-unreads)))
5072          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5073          (scored (length scored-list))
5074          (number (length articles))
5075          (marked (+ (length gnus-newsgroup-marked)
5076                     (length gnus-newsgroup-dormant)))
5077          (select
5078           (cond
5079            ((numberp read-all)
5080             read-all)
5081            ((numberp gnus-newsgroup-display)
5082             gnus-newsgroup-display)
5083            (t
5084             (condition-case ()
5085                 (cond
5086                  ((and (or (<= scored marked) (= scored number))
5087                        (numberp gnus-large-newsgroup)
5088                        (> number gnus-large-newsgroup))
5089                   (let* ((cursor-in-echo-area nil)
5090                          (initial (gnus-parameter-large-newsgroup-initial
5091                                    gnus-newsgroup-name))
5092                          (input
5093                           (read-string
5094                            (format
5095                             "How many articles from %s (%s %d): "
5096                             (gnus-limit-string
5097                              (gnus-group-decoded-name gnus-newsgroup-name)
5098                              35)
5099                             (if initial "max" "default")
5100                             number)
5101                            (if initial
5102                                (cons (number-to-string initial)
5103                                      0)))))
5104                     (if (string-match "^[ \t]*$" input) number input)))
5105                  ((and (> scored marked) (< scored number)
5106                        (> (- scored number) 20))
5107                   (let ((input
5108                          (read-string
5109                           (format "%s %s (%d scored, %d total): "
5110                                   "How many articles from"
5111                                   (gnus-group-decoded-name group)
5112                                   scored number))))
5113                     (if (string-match "^[ \t]*$" input)
5114                         number input)))
5115                  (t number))
5116               (quit
5117                (message "Quit getting the articles to read")
5118                nil))))))
5119     (setq select (if (stringp select) (string-to-number select) select))
5120     (if (or (null select) (zerop select))
5121         select
5122       (if (and (not (zerop scored)) (<= (abs select) scored))
5123           (progn
5124             (setq articles (sort scored-list '<))
5125             (setq number (length articles)))
5126         (setq articles (copy-sequence articles)))
5127
5128       (when (< (abs select) number)
5129         (if (< select 0)
5130             ;; Select the N oldest articles.
5131             (setcdr (nthcdr (1- (abs select)) articles) nil)
5132           ;; Select the N most recent articles.
5133           (setq articles (nthcdr (- number select) articles))))
5134       (setq gnus-newsgroup-unselected
5135             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5136       (when gnus-alter-articles-to-read-function
5137         (setq gnus-newsgroup-unreads
5138               (sort
5139                (funcall gnus-alter-articles-to-read-function
5140                         gnus-newsgroup-name gnus-newsgroup-unreads)
5141                '<)))
5142       articles)))
5143
5144 (defun gnus-killed-articles (killed articles)
5145   (let (out)
5146     (while articles
5147       (when (inline (gnus-member-of-range (car articles) killed))
5148         (push (car articles) out))
5149       (setq articles (cdr articles)))
5150     out))
5151
5152 (defun gnus-uncompress-marks (marks)
5153   "Uncompress the mark ranges in MARKS."
5154   (let ((uncompressed '(score bookmark))
5155         out)
5156     (while marks
5157       (if (memq (caar marks) uncompressed)
5158           (push (car marks) out)
5159         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5160       (setq marks (cdr marks)))
5161     out))
5162
5163 (defun gnus-article-mark-to-type (mark)
5164   "Return the type of MARK."
5165   (or (cadr (assq mark gnus-article-special-mark-lists))
5166       'list))
5167
5168 (defun gnus-article-unpropagatable-p (mark)
5169   "Return whether MARK should be propagated to backend."
5170   (memq mark gnus-article-unpropagated-mark-lists))
5171
5172 (defun gnus-adjust-marked-articles (info)
5173   "Set all article lists and remove all marks that are no longer valid."
5174   (let* ((marked-lists (gnus-info-marks info))
5175          (active (gnus-active (gnus-info-group info)))
5176          (min (car active))
5177          (max (cdr active))
5178          (types gnus-article-mark-lists)
5179          marks var articles article mark mark-type)
5180
5181     (dolist (marks marked-lists)
5182       (setq mark (car marks)
5183             mark-type (gnus-article-mark-to-type mark)
5184             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5185
5186       ;; We set the variable according to the type of the marks list,
5187       ;; and then adjust the marks to a subset of the active articles.
5188       (cond
5189        ;; Adjust "simple" lists.
5190        ((eq mark-type 'list)
5191         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5192         (when (memq mark '(tick dormant expire reply save))
5193           (while articles
5194             (when (or (< (setq article (pop articles)) min) (> article max))
5195               (set var (delq article (symbol-value var)))))))
5196        ;; Adjust assocs.
5197        ((eq mark-type 'tuple)
5198         (set var (setq articles (cdr marks)))
5199         (when (not (listp (cdr (symbol-value var))))
5200           (set var (list (symbol-value var))))
5201         (when (not (listp (cdr articles)))
5202           (setq articles (list articles)))
5203         (while articles
5204           (when (or (not (consp (setq article (pop articles))))
5205                     (< (car article) min)
5206                     (> (car article) max))
5207             (set var (delq article (symbol-value var))))))
5208        ;; Adjust ranges (sloppily).
5209        ((eq mark-type 'range)
5210         (cond
5211          ((eq mark 'seen)
5212           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5213           ;; It should be (seen (NUM1 . NUM2)).
5214           (when (numberp (cddr marks))
5215             (setcdr marks (list (cdr marks))))
5216           (setq articles (cdr marks))
5217           (while (and articles
5218                       (or (and (consp (car articles))
5219                                (> min (cdar articles)))
5220                           (and (numberp (car articles))
5221                                (> min (car articles)))))
5222             (pop articles))
5223           (set var articles))))))))
5224
5225 (defun gnus-update-missing-marks (missing)
5226   "Go through the list of MISSING articles and remove them from the mark lists."
5227   (when missing
5228     (let (var m)
5229       ;; Go through all types.
5230       (dolist (elem gnus-article-mark-lists)
5231         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5232           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5233           (when (symbol-value var)
5234             ;; This list has articles.  So we delete all missing
5235             ;; articles from it.
5236             (setq m missing)
5237             (while m
5238               (set var (delq (pop m) (symbol-value var))))))))))
5239
5240 (defun gnus-update-marks ()
5241   "Enter the various lists of marked articles into the newsgroup info list."
5242   (let ((types gnus-article-mark-lists)
5243         (info (gnus-get-info gnus-newsgroup-name))
5244         type list newmarked symbol delta-marks)
5245     (when info
5246       ;; Add all marks lists to the list of marks lists.
5247       (while (setq type (pop types))
5248         (setq list (symbol-value
5249                     (setq symbol
5250                           (intern (format "gnus-newsgroup-%s" (car type))))))
5251
5252         (when list
5253           ;; Get rid of the entries of the articles that have the
5254           ;; default score.
5255           (when (and (eq (cdr type) 'score)
5256                      gnus-save-score
5257                      list)
5258             (let* ((arts list)
5259                    (prev (cons nil list))
5260                    (all prev))
5261               (while arts
5262                 (if (or (not (consp (car arts)))
5263                         (= (cdar arts) gnus-summary-default-score))
5264                     (setcdr prev (cdr arts))
5265                   (setq prev arts))
5266                 (setq arts (cdr arts)))
5267               (setq list (cdr all)))))
5268
5269         (when (eq (cdr type) 'seen)
5270           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5271
5272         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5273           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5274
5275         (when (and (gnus-check-backend-function
5276                     'request-set-mark gnus-newsgroup-name)
5277                    (not (gnus-article-unpropagatable-p (cdr type))))
5278           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5279                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5280                  (add (gnus-remove-from-range
5281                        (gnus-copy-sequence list) old)))
5282             (when add
5283               (push (list add 'add (list (cdr type))) delta-marks))
5284             (when del
5285               (push (list del 'del (list (cdr type))) delta-marks))))
5286
5287         (when list
5288           (push (cons (cdr type) list) newmarked)))
5289
5290       (when delta-marks
5291         (unless (gnus-check-group gnus-newsgroup-name)
5292           (error "Can't open server for %s" gnus-newsgroup-name))
5293         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5294
5295       ;; Enter these new marks into the info of the group.
5296       (if (nthcdr 3 info)
5297           (setcar (nthcdr 3 info) newmarked)
5298         ;; Add the marks lists to the end of the info.
5299         (when newmarked
5300           (setcdr (nthcdr 2 info) (list newmarked))))
5301
5302       ;; Cut off the end of the info if there's nothing else there.
5303       (let ((i 5))
5304         (while (and (> i 2)
5305                     (not (nth i info)))
5306           (when (nthcdr (decf i) info)
5307             (setcdr (nthcdr i info) nil)))))))
5308
5309 (defun gnus-set-mode-line (where)
5310   "Set the mode line of the article or summary buffers.
5311 If WHERE is `summary', the summary mode line format will be used."
5312   ;; Is this mode line one we keep updated?
5313   (when (and (memq where gnus-updated-mode-lines)
5314              (symbol-value
5315               (intern (format "gnus-%s-mode-line-format-spec" where))))
5316     (let (mode-string)
5317       (save-excursion
5318         ;; We evaluate this in the summary buffer since these
5319         ;; variables are buffer-local to that buffer.
5320         (set-buffer gnus-summary-buffer)
5321         ;; We bind all these variables that are used in the `eval' form
5322         ;; below.
5323         (let* ((mformat (symbol-value
5324                          (intern
5325                           (format "gnus-%s-mode-line-format-spec" where))))
5326                (gnus-tmp-group-name (gnus-group-decoded-name
5327                                      gnus-newsgroup-name))
5328                (gnus-tmp-article-number (or gnus-current-article 0))
5329                (gnus-tmp-unread gnus-newsgroup-unreads)
5330                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5331                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5332                (gnus-tmp-unread-and-unselected
5333                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5334                             (zerop gnus-tmp-unselected))
5335                        "")
5336                       ((zerop gnus-tmp-unselected)
5337                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5338                       (t (format "{%d(+%d) more}"
5339                                  gnus-tmp-unread-and-unticked
5340                                  gnus-tmp-unselected))))
5341                (gnus-tmp-subject
5342                 (if (and gnus-current-headers
5343                          (vectorp gnus-current-headers))
5344                     (gnus-mode-string-quote
5345                      (mail-header-subject gnus-current-headers))
5346                   ""))
5347                bufname-length max-len
5348                gnus-tmp-header);; passed as argument to any user-format-funcs
5349           (setq mode-string (eval mformat))
5350           (setq bufname-length (if (string-match "%b" mode-string)
5351                                    (- (length
5352                                        (buffer-name
5353                                         (if (eq where 'summary)
5354                                             nil
5355                                           (get-buffer gnus-article-buffer))))
5356                                       2)
5357                                  0))
5358           (setq max-len (max 4 (if gnus-mode-non-string-length
5359                                    (- (window-width)
5360                                       gnus-mode-non-string-length
5361                                       bufname-length)
5362                                  (length mode-string))))
5363           ;; We might have to chop a bit of the string off...
5364           (when (> (length mode-string) max-len)
5365             (setq mode-string
5366                   (concat (gnus-truncate-string mode-string (- max-len 3))
5367                           "...")))
5368           ;; Pad the mode string a bit.
5369           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5370       ;; Update the mode line.
5371       (setq mode-line-buffer-identification
5372             (gnus-mode-line-buffer-identification (list mode-string)))
5373       (set-buffer-modified-p t))))
5374
5375 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5376   "Go through the HEADERS list and add all Xrefs to a hash table.
5377 The resulting hash table is returned, or nil if no Xrefs were found."
5378   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5379          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5380          (xref-hashtb (gnus-make-hashtable))
5381          start group entry number xrefs header)
5382     (while headers
5383       (setq header (pop headers))
5384       (when (and (setq xrefs (mail-header-xref header))
5385                  (not (memq (setq number (mail-header-number header))
5386                             unreads)))
5387         (setq start 0)
5388         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5389           (setq start (match-end 0))
5390           (setq group (if prefix
5391                           (concat prefix (substring xrefs (match-beginning 1)
5392                                                     (match-end 1)))
5393                         (substring xrefs (match-beginning 1) (match-end 1))))
5394           (setq number
5395                 (string-to-int (substring xrefs (match-beginning 2)
5396                                           (match-end 2))))
5397           (if (setq entry (gnus-gethash group xref-hashtb))
5398               (setcdr entry (cons number (cdr entry)))
5399             (gnus-sethash group (cons number nil) xref-hashtb)))))
5400     (and start xref-hashtb)))
5401
5402 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5403   "Look through all the headers and mark the Xrefs as read."
5404   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5405         name entry info xref-hashtb idlist method nth4)
5406     (save-excursion
5407       (set-buffer gnus-group-buffer)
5408       (when (setq xref-hashtb
5409                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5410         (mapatoms
5411          (lambda (group)
5412            (unless (string= from-newsgroup (setq name (symbol-name group)))
5413              (setq idlist (symbol-value group))
5414              ;; Dead groups are not updated.
5415              (and (prog1
5416                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5417                             info (nth 2 entry))
5418                     (when (stringp (setq nth4 (gnus-info-method info)))
5419                       (setq nth4 (gnus-server-to-method nth4))))
5420                   ;; Only do the xrefs if the group has the same
5421                   ;; select method as the group we have just read.
5422                   (or (gnus-methods-equal-p
5423                        nth4 (gnus-find-method-for-group from-newsgroup))
5424                       virtual
5425                       (equal nth4 (setq method (gnus-find-method-for-group
5426                                                 from-newsgroup)))
5427                       (and (equal (car nth4) (car method))
5428                            (equal (nth 1 nth4) (nth 1 method))))
5429                   gnus-use-cross-reference
5430                   (or (not (eq gnus-use-cross-reference t))
5431                       virtual
5432                       ;; Only do cross-references on subscribed
5433                       ;; groups, if that is what is wanted.
5434                       (<= (gnus-info-level info) gnus-level-subscribed))
5435                   (gnus-group-make-articles-read name idlist))))
5436          xref-hashtb)))))
5437
5438 (defun gnus-compute-read-articles (group articles)
5439   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5440          (info (nth 2 entry))
5441          (active (gnus-active group))
5442          ninfo)
5443     (when entry
5444       ;; First peel off all invalid article numbers.
5445       (when active
5446         (let ((ids articles)
5447               id first)
5448           (while (setq id (pop ids))
5449             (when (and first (> id (cdr active)))
5450               ;; We'll end up in this situation in one particular
5451               ;; obscure situation.  If you re-scan a group and get
5452               ;; a new article that is cross-posted to a different
5453               ;; group that has not been re-scanned, you might get
5454               ;; crossposted article that has a higher number than
5455               ;; Gnus believes possible.  So we re-activate this
5456               ;; group as well.  This might mean doing the
5457               ;; crossposting thingy will *increase* the number
5458               ;; of articles in some groups.  Tsk, tsk.
5459               (setq active (or (gnus-activate-group group) active)))
5460             (when (or (> id (cdr active))
5461                       (< id (car active)))
5462               (setq articles (delq id articles))))))
5463       ;; If the read list is nil, we init it.
5464       (if (and active
5465                (null (gnus-info-read info))
5466                (> (car active) 1))
5467           (setq ninfo (cons 1 (1- (car active))))
5468         (setq ninfo (gnus-info-read info)))
5469       ;; Then we add the read articles to the range.
5470       (gnus-add-to-range
5471        ninfo (setq articles (sort articles '<))))))
5472
5473 (defun gnus-group-make-articles-read (group articles)
5474   "Update the info of GROUP to say that ARTICLES are read."
5475   (let* ((num 0)
5476          (entry (gnus-gethash group gnus-newsrc-hashtb))
5477          (info (nth 2 entry))
5478          (active (gnus-active group))
5479          range)
5480     (when entry
5481       (setq range (gnus-compute-read-articles group articles))
5482       (save-excursion
5483         (set-buffer gnus-group-buffer)
5484         (gnus-undo-register
5485           `(progn
5486              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5487              (gnus-info-set-read ',info ',(gnus-info-read info))
5488              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5489              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5490              (gnus-group-update-group ,group t))))
5491       ;; Add the read articles to the range.
5492       (gnus-info-set-read info range)
5493       (gnus-request-set-mark group (list (list range 'add '(read))))
5494       ;; Then we have to re-compute how many unread
5495       ;; articles there are in this group.
5496       (when active
5497         (cond
5498          ((not range)
5499           (setq num (- (1+ (cdr active)) (car active))))
5500          ((not (listp (cdr range)))
5501           (setq num (- (cdr active) (- (1+ (cdr range))
5502                                        (car range)))))
5503          (t
5504           (while range
5505             (if (numberp (car range))
5506                 (setq num (1+ num))
5507               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5508             (setq range (cdr range)))
5509           (setq num (- (cdr active) num))))
5510         ;; Update the number of unread articles.
5511         (setcar entry num)
5512         ;; Update the group buffer.
5513         (gnus-group-update-group group t)))))
5514
5515 (defvar gnus-newsgroup-none-id 0)
5516
5517 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5518   (let ((cur nntp-server-buffer)
5519         (dependencies
5520          (or dependencies
5521              (save-excursion (set-buffer gnus-summary-buffer)
5522                              gnus-newsgroup-dependencies)))
5523         headers id end ref
5524         (mail-parse-charset gnus-newsgroup-charset)
5525         (mail-parse-ignored-charsets
5526          (save-excursion (condition-case nil
5527                              (set-buffer gnus-summary-buffer)
5528                            (error))
5529                          gnus-newsgroup-ignored-charsets)))
5530     (save-excursion
5531       (set-buffer nntp-server-buffer)
5532       ;; Translate all TAB characters into SPACE characters.
5533       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5534       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5535       (gnus-run-hooks 'gnus-parse-headers-hook)
5536       (let ((case-fold-search t)
5537             in-reply-to header p lines chars ctype)
5538         (goto-char (point-min))
5539         ;; Search to the beginning of the next header.  Error messages
5540         ;; do not begin with 2 or 3.
5541         (while (re-search-forward "^[23][0-9]+ " nil t)
5542           (setq id nil
5543                 ref nil)
5544           ;; This implementation of this function, with nine
5545           ;; search-forwards instead of the one re-search-forward and
5546           ;; a case (which basically was the old function) is actually
5547           ;; about twice as fast, even though it looks messier.  You
5548           ;; can't have everything, I guess.  Speed and elegance
5549           ;; doesn't always go hand in hand.
5550           (setq
5551            header
5552            (make-full-mail-header
5553             ;; Number.
5554             (prog1
5555                 (read cur)
5556               (end-of-line)
5557               (setq p (point))
5558               (narrow-to-region (point)
5559                                 (or (and (search-forward "\n.\n" nil t)
5560                                          (- (point) 2))
5561                                     (point))))
5562             ;; Subject.
5563             (progn
5564               (goto-char p)
5565               (if (search-forward "\nsubject:" nil t)
5566                   (nnheader-header-value)
5567                 "(none)"))
5568             ;; From.
5569             (progn
5570               (goto-char p)
5571               (if (search-forward "\nfrom:" nil t)
5572                   (nnheader-header-value)
5573                 "(nobody)"))
5574             ;; Date.
5575             (progn
5576               (goto-char p)
5577               (if (search-forward "\ndate:" nil t)
5578                   (nnheader-header-value) ""))
5579             ;; Message-ID.
5580             (progn
5581               (goto-char p)
5582               (setq id (if (re-search-forward
5583                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5584                            ;; We do it this way to make sure the Message-ID
5585                            ;; is (somewhat) syntactically valid.
5586                            (buffer-substring (match-beginning 1)
5587                                              (match-end 1))
5588                          ;; If there was no message-id, we just fake one
5589                          ;; to make subsequent routines simpler.
5590                          (nnheader-generate-fake-message-id))))
5591             ;; References.
5592             (progn
5593               (goto-char p)
5594               (if (search-forward "\nreferences:" nil t)
5595                   (progn
5596                     (setq end (point))
5597                     (prog1
5598                         (nnheader-header-value)
5599                       (setq ref
5600                             (buffer-substring
5601                              (progn
5602                                ;; (end-of-line)
5603                                (search-backward ">" end t)
5604                                (1+ (point)))
5605                              (progn
5606                                (search-backward "<" end t)
5607                                (point))))))
5608                 ;; Get the references from the in-reply-to header if there
5609                 ;; were no references and the in-reply-to header looks
5610                 ;; promising.
5611                 (if (and (search-forward "\nin-reply-to:" nil t)
5612                          (setq in-reply-to (nnheader-header-value))
5613                          (string-match "<[^>]+>" in-reply-to))
5614                     (let (ref2)
5615                       (setq ref (substring in-reply-to (match-beginning 0)
5616                                            (match-end 0)))
5617                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5618                         (setq ref2 (substring in-reply-to (match-beginning 0)
5619                                               (match-end 0)))
5620                         (when (> (length ref2) (length ref))
5621                           (setq ref ref2)))
5622                       ref)
5623                   (setq ref nil))))
5624             ;; Chars.
5625             (progn
5626               (goto-char p)
5627               (if (search-forward "\nchars: " nil t)
5628                   (if (numberp (setq chars (ignore-errors (read cur))))
5629                       chars -1)
5630                 -1))
5631             ;; Lines.
5632             (progn
5633               (goto-char p)
5634               (if (search-forward "\nlines: " nil t)
5635                   (if (numberp (setq lines (ignore-errors (read cur))))
5636                       lines -1)
5637                 -1))
5638             ;; Xref.
5639             (progn
5640               (goto-char p)
5641               (and (search-forward "\nxref:" nil t)
5642                    (nnheader-header-value)))
5643             ;; Extra.
5644             (when gnus-extra-headers
5645               (let ((extra gnus-extra-headers)
5646                     out)
5647                 (while extra
5648                   (goto-char p)
5649                   (when (search-forward
5650                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5651                     (push (cons (car extra) (nnheader-header-value)) out))
5652                   (pop extra))
5653                 out))))
5654           (goto-char p)
5655           (if (and (search-forward "\ncontent-type: " nil t)
5656                    (setq ctype (nnheader-header-value)))
5657               (mime-entity-set-content-type-internal
5658                header (mime-parse-Content-Type ctype)))
5659           (when (equal id ref)
5660             (setq ref nil))
5661
5662           (when gnus-alter-header-function
5663             (funcall gnus-alter-header-function header)
5664             (setq id (mail-header-id header)
5665                   ref (gnus-parent-id (mail-header-references header))))
5666
5667           (when (setq header
5668                       (gnus-dependencies-add-header
5669                        header dependencies force-new))
5670             (push header headers))
5671           (goto-char (point-max))
5672           (widen))
5673         (nreverse headers)))))
5674
5675 ;; Goes through the xover lines and returns a list of vectors
5676 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5677                                                   force-new dependencies
5678                                                   group also-fetch-heads)
5679   "Parse the news overview data in the server buffer.
5680 Return a list of headers that match SEQUENCE (see
5681 `nntp-retrieve-headers')."
5682   ;; Get the Xref when the users reads the articles since most/some
5683   ;; NNTP servers do not include Xrefs when using XOVER.
5684   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5685   (let ((mail-parse-charset gnus-newsgroup-charset)
5686         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5687         (cur nntp-server-buffer)
5688         (dependencies (or dependencies gnus-newsgroup-dependencies))
5689         (allp (cond
5690                ((eq gnus-read-all-available-headers t)
5691                 t)
5692                ((stringp gnus-read-all-available-headers)
5693                 (string-match gnus-read-all-available-headers group))
5694                (t
5695                 nil)))
5696         number headers header)
5697     (save-excursion
5698       (set-buffer nntp-server-buffer)
5699       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5700       ;; Allow the user to mangle the headers before parsing them.
5701       (gnus-run-hooks 'gnus-parse-headers-hook)
5702       (goto-char (point-min))
5703       (gnus-parse-without-error
5704         (while (and (or sequence allp)
5705                     (not (eobp)))
5706           (setq number (read cur))
5707           (when (not allp)
5708             (while (and sequence
5709                         (< (car sequence) number))
5710               (setq sequence (cdr sequence))))
5711           (when (and (or allp
5712                          (and sequence
5713                               (eq number (car sequence))))
5714                      (progn
5715                        (setq sequence (cdr sequence))
5716                        (setq header (inline
5717                                       (gnus-nov-parse-line
5718                                        number dependencies force-new)))))
5719             (push header headers))
5720           (forward-line 1)))
5721       ;; A common bug in inn is that if you have posted an article and
5722       ;; then retrieves the active file, it will answer correctly --
5723       ;; the new article is included.  However, a NOV entry for the
5724       ;; article may not have been generated yet, so this may fail.
5725       ;; We work around this problem by retrieving the last few
5726       ;; headers using HEAD.
5727       (if (or (not also-fetch-heads)
5728               (not sequence))
5729           ;; We (probably) got all the headers.
5730           (nreverse headers)
5731         (let ((gnus-nov-is-evil t))
5732           (nconc
5733            (nreverse headers)
5734            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5735              (gnus-get-newsgroup-headers))))))))
5736
5737 (defun gnus-article-get-xrefs ()
5738   "Fill in the Xref value in `gnus-current-headers', if necessary.
5739 This is meant to be called in `gnus-article-internal-prepare-hook'."
5740   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5741                                  gnus-current-headers)))
5742     (or (not gnus-use-cross-reference)
5743         (not headers)
5744         (and (mail-header-xref headers)
5745              (not (string= (mail-header-xref headers) "")))
5746         (let ((case-fold-search t)
5747               xref)
5748           (save-restriction
5749             (nnheader-narrow-to-headers)
5750             (goto-char (point-min))
5751             (when (or (and (not (eobp))
5752                            (eq (downcase (char-after)) ?x)
5753                            (looking-at "Xref:"))
5754                       (search-forward "\nXref:" nil t))
5755               (goto-char (1+ (match-end 0)))
5756               (setq xref (buffer-substring (point)
5757                                            (progn (end-of-line) (point))))
5758               (mail-header-set-xref headers xref)))))))
5759
5760 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5761   "Find article ID and insert the summary line for that article.
5762 OLD-HEADER can either be a header or a line number to insert
5763 the subject line on."
5764   (let* ((line (and (numberp old-header) old-header))
5765          (old-header (and (vectorp old-header) old-header))
5766          (header (cond ((and old-header use-old-header)
5767                         old-header)
5768                        ((and (numberp id)
5769                              (gnus-number-to-header id))
5770                         (gnus-number-to-header id))
5771                        (t
5772                         (gnus-read-header id))))
5773          (number (and (numberp id) id))
5774          d)
5775     (when header
5776       ;; Rebuild the thread that this article is part of and go to the
5777       ;; article we have fetched.
5778       (when (and (not gnus-show-threads)
5779                  old-header)
5780         (when (and number
5781                    (setq d (gnus-data-find (mail-header-number old-header))))
5782           (goto-char (gnus-data-pos d))
5783           (gnus-data-remove
5784            number
5785            (- (gnus-point-at-bol)
5786               (prog1
5787                   (1+ (gnus-point-at-eol))
5788                 (gnus-delete-line))))))
5789       (when old-header
5790         (mail-header-set-number header (mail-header-number old-header)))
5791       (setq gnus-newsgroup-sparse
5792             (delq (setq number (mail-header-number header))
5793                   gnus-newsgroup-sparse))
5794       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5795       (push number gnus-newsgroup-limit)
5796       (gnus-rebuild-thread (mail-header-id header) line)
5797       (gnus-summary-goto-subject number nil t))
5798     (when (and (numberp number)
5799                (> number 0))
5800       ;; We have to update the boundaries even if we can't fetch the
5801       ;; article if ID is a number -- so that the next `P' or `N'
5802       ;; command will fetch the previous (or next) article even
5803       ;; if the one we tried to fetch this time has been canceled.
5804       (when (> number gnus-newsgroup-end)
5805         (setq gnus-newsgroup-end number))
5806       (when (< number gnus-newsgroup-begin)
5807         (setq gnus-newsgroup-begin number))
5808       (setq gnus-newsgroup-unselected
5809             (delq number gnus-newsgroup-unselected)))
5810     ;; Report back a success?
5811     (and header (mail-header-number header))))
5812
5813 ;;; Process/prefix in the summary buffer
5814
5815 (defun gnus-summary-work-articles (n)
5816   "Return a list of articles to be worked upon.
5817 The prefix argument, the list of process marked articles, and the
5818 current article will be taken into consideration."
5819   (save-excursion
5820     (set-buffer gnus-summary-buffer)
5821     (cond
5822      (n
5823       ;; A numerical prefix has been given.
5824       (setq n (prefix-numeric-value n))
5825       (let ((backward (< n 0))
5826             (n (abs (prefix-numeric-value n)))
5827             articles article)
5828         (save-excursion
5829           (while
5830               (and (> n 0)
5831                    (push (setq article (gnus-summary-article-number))
5832                          articles)
5833                    (if backward
5834                        (gnus-summary-find-prev nil article)
5835                      (gnus-summary-find-next nil article)))
5836             (decf n)))
5837         (nreverse articles)))
5838      ((and (gnus-region-active-p) (mark))
5839       (message "region active")
5840       ;; Work on the region between point and mark.
5841       (let ((max (max (point) (mark)))
5842             articles article)
5843         (save-excursion
5844           (goto-char (min (point) (mark)))
5845           (while
5846               (and
5847                (push (setq article (gnus-summary-article-number)) articles)
5848                (gnus-summary-find-next nil article)
5849                (< (point) max)))
5850           (nreverse articles))))
5851      (gnus-newsgroup-processable
5852       ;; There are process-marked articles present.
5853       ;; Save current state.
5854       (gnus-summary-save-process-mark)
5855       ;; Return the list.
5856       (reverse gnus-newsgroup-processable))
5857      (t
5858       ;; Just return the current article.
5859       (list (gnus-summary-article-number))))))
5860
5861 (defmacro gnus-summary-iterate (arg &rest forms)
5862   "Iterate over the process/prefixed articles and do FORMS.
5863 ARG is the interactive prefix given to the command.  FORMS will be
5864 executed with point over the summary line of the articles."
5865   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5866     `(let ((,articles (gnus-summary-work-articles ,arg)))
5867        (while ,articles
5868          (gnus-summary-goto-subject (car ,articles))
5869          ,@forms
5870          (pop ,articles)))))
5871
5872 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5873 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5874
5875 (defun gnus-summary-save-process-mark ()
5876   "Push the current set of process marked articles on the stack."
5877   (interactive)
5878   (push (copy-sequence gnus-newsgroup-processable)
5879         gnus-newsgroup-process-stack))
5880
5881 (defun gnus-summary-kill-process-mark ()
5882   "Push the current set of process marked articles on the stack and unmark."
5883   (interactive)
5884   (gnus-summary-save-process-mark)
5885   (gnus-summary-unmark-all-processable))
5886
5887 (defun gnus-summary-yank-process-mark ()
5888   "Pop the last process mark state off the stack and restore it."
5889   (interactive)
5890   (unless gnus-newsgroup-process-stack
5891     (error "Empty mark stack"))
5892   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5893
5894 (defun gnus-summary-process-mark-set (set)
5895   "Make SET into the current process marked articles."
5896   (gnus-summary-unmark-all-processable)
5897   (while set
5898     (gnus-summary-set-process-mark (pop set))))
5899
5900 ;;; Searching and stuff
5901
5902 (defun gnus-summary-search-group (&optional backward use-level)
5903   "Search for next unread newsgroup.
5904 If optional argument BACKWARD is non-nil, search backward instead."
5905   (save-excursion
5906     (set-buffer gnus-group-buffer)
5907     (when (gnus-group-search-forward
5908            backward nil (if use-level (gnus-group-group-level) nil))
5909       (gnus-group-group-name))))
5910
5911 (defun gnus-summary-best-group (&optional exclude-group)
5912   "Find the name of the best unread group.
5913 If EXCLUDE-GROUP, do not go to this group."
5914   (save-excursion
5915     (set-buffer gnus-group-buffer)
5916     (save-excursion
5917       (gnus-group-best-unread-group exclude-group))))
5918
5919 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5920   (if backward (gnus-summary-find-prev)
5921     (let* ((dummy (gnus-summary-article-intangible-p))
5922            (article (or article (gnus-summary-article-number)))
5923            (arts (gnus-data-find-list article))
5924            result)
5925       (when (and (not dummy)
5926                  (or (not gnus-summary-check-current)
5927                      (not unread)
5928                      (not (gnus-data-unread-p (car arts)))))
5929         (setq arts (cdr arts)))
5930       (when (setq result
5931                   (if unread
5932                       (progn
5933                         (while arts
5934                           (when (or (and undownloaded
5935                                          (eq gnus-undownloaded-mark
5936                                              (gnus-data-mark (car arts))))
5937                                     (gnus-data-unread-p (car arts)))
5938                             (setq result (car arts)
5939                                   arts nil))
5940                           (setq arts (cdr arts)))
5941                         result)
5942                     (car arts)))
5943         (goto-char (gnus-data-pos result))
5944         (gnus-data-number result)))))
5945
5946 (defun gnus-summary-find-prev (&optional unread article)
5947   (let* ((eobp (eobp))
5948          (article (or article (gnus-summary-article-number)))
5949          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5950          result)
5951     (when (and (not eobp)
5952                (or (not gnus-summary-check-current)
5953                    (not unread)
5954                    (not (gnus-data-unread-p (car arts)))))
5955       (setq arts (cdr arts)))
5956     (when (setq result
5957                 (if unread
5958                     (progn
5959                       (while arts
5960                         (when (gnus-data-unread-p (car arts))
5961                           (setq result (car arts)
5962                                 arts nil))
5963                         (setq arts (cdr arts)))
5964                       result)
5965                   (car arts)))
5966       (goto-char (gnus-data-pos result))
5967       (gnus-data-number result))))
5968
5969 (defun gnus-summary-find-subject (subject &optional unread backward article)
5970   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5971          (article (or article (gnus-summary-article-number)))
5972          (articles (gnus-data-list backward))
5973          (arts (gnus-data-find-list article articles))
5974          result)
5975     (when (or (not gnus-summary-check-current)
5976               (not unread)
5977               (not (gnus-data-unread-p (car arts))))
5978       (setq arts (cdr arts)))
5979     (while arts
5980       (and (or (not unread)
5981                (gnus-data-unread-p (car arts)))
5982            (vectorp (gnus-data-header (car arts)))
5983            (gnus-subject-equal
5984             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5985            (setq result (car arts)
5986                  arts nil))
5987       (setq arts (cdr arts)))
5988     (and result
5989          (goto-char (gnus-data-pos result))
5990          (gnus-data-number result))))
5991
5992 (defun gnus-summary-search-forward (&optional unread subject backward)
5993   "Search forward for an article.
5994 If UNREAD, look for unread articles.  If SUBJECT, look for
5995 articles with that subject.  If BACKWARD, search backward instead."
5996   (cond (subject (gnus-summary-find-subject subject unread backward))
5997         (backward (gnus-summary-find-prev unread))
5998         (t (gnus-summary-find-next unread))))
5999
6000 (defun gnus-recenter (&optional n)
6001   "Center point in window and redisplay frame.
6002 Also do horizontal recentering."
6003   (interactive "P")
6004   (when (and gnus-auto-center-summary
6005              (not (eq gnus-auto-center-summary 'vertical)))
6006     (gnus-horizontal-recenter))
6007   (recenter n))
6008
6009 (defun gnus-summary-recenter ()
6010   "Center point in the summary window.
6011 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6012 displayed, no centering will be performed."
6013   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6014   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6015   (interactive)
6016   (let* ((top (cond ((< (window-height) 4) 0)
6017                     ((< (window-height) 7) 1)
6018                     (t (if (numberp gnus-auto-center-summary)
6019                            gnus-auto-center-summary
6020                          2))))
6021          (height (1- (window-height)))
6022          (bottom (save-excursion (goto-char (point-max))
6023                                  (forward-line (- height))
6024                                  (point)))
6025          (window (get-buffer-window (current-buffer))))
6026     ;; The user has to want it.
6027     (when gnus-auto-center-summary
6028       (when (get-buffer-window gnus-article-buffer)
6029         ;; Only do recentering when the article buffer is displayed,
6030         ;; Set the window start to either `bottom', which is the biggest
6031         ;; possible valid number, or the second line from the top,
6032         ;; whichever is the least.
6033         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6034           (if (> bottom top-pos)
6035               ;; Keep the second line from the top visible
6036               (set-window-start window top-pos t)
6037             ;; Try to keep the bottom line visible; if it's partially
6038             ;; obscured, either scroll one more line to make it fully
6039             ;; visible, or revert to using TOP-POS.
6040             (save-excursion
6041               (goto-char (point-max))
6042               (forward-line -1)
6043               (let ((last-line-start (point)))
6044                 (goto-char bottom)
6045                 (set-window-start window (point) t)
6046                 (when (not (pos-visible-in-window-p last-line-start window))
6047                   (forward-line 1)
6048                   (set-window-start window (min (point) top-pos) t)))))))
6049       ;; Do horizontal recentering while we're at it.
6050       (when (and (get-buffer-window (current-buffer) t)
6051                  (not (eq gnus-auto-center-summary 'vertical)))
6052         (let ((selected (selected-window)))
6053           (select-window (get-buffer-window (current-buffer) t))
6054           (gnus-summary-position-point)
6055           (gnus-horizontal-recenter)
6056           (select-window selected))))))
6057
6058 (defun gnus-summary-jump-to-group (newsgroup)
6059   "Move point to NEWSGROUP in group mode buffer."
6060   ;; Keep update point of group mode buffer if visible.
6061   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6062       (save-window-excursion
6063         ;; Take care of tree window mode.
6064         (when (get-buffer-window gnus-group-buffer)
6065           (pop-to-buffer gnus-group-buffer))
6066         (gnus-group-jump-to-group newsgroup))
6067     (save-excursion
6068       ;; Take care of tree window mode.
6069       (if (get-buffer-window gnus-group-buffer)
6070           (pop-to-buffer gnus-group-buffer)
6071         (set-buffer gnus-group-buffer))
6072       (gnus-group-jump-to-group newsgroup))))
6073
6074 ;; This function returns a list of article numbers based on the
6075 ;; difference between the ranges of read articles in this group and
6076 ;; the range of active articles.
6077 (defun gnus-list-of-unread-articles (group)
6078   (let* ((read (gnus-info-read (gnus-get-info group)))
6079          (active (or (gnus-active group) (gnus-activate-group group)))
6080          (last (cdr active))
6081          first nlast unread)
6082     ;; If none are read, then all are unread.
6083     (if (not read)
6084         (setq first (car active))
6085       ;; If the range of read articles is a single range, then the
6086       ;; first unread article is the article after the last read
6087       ;; article.  Sounds logical, doesn't it?
6088       (if (and (not (listp (cdr read)))
6089                (or (< (car read) (car active))
6090                    (progn (setq read (list read))
6091                           nil)))
6092           (setq first (max (car active) (1+ (cdr read))))
6093         ;; `read' is a list of ranges.
6094         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6095                                   (caar read)))
6096                   1)
6097           (setq first (car active)))
6098         (while read
6099           (when first
6100             (while (< first nlast)
6101               (push first unread)
6102               (setq first (1+ first))))
6103           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6104           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6105           (setq read (cdr read)))))
6106     ;; And add the last unread articles.
6107     (while (<= first last)
6108       (push first unread)
6109       (setq first (1+ first)))
6110     ;; Return the list of unread articles.
6111     (delq 0 (nreverse unread))))
6112
6113 (defun gnus-list-of-read-articles (group)
6114   "Return a list of unread, unticked and non-dormant articles."
6115   (let* ((info (gnus-get-info group))
6116          (marked (gnus-info-marks info))
6117          (active (gnus-active group)))
6118     (and info active
6119          (gnus-list-range-difference
6120           (gnus-list-range-difference
6121            (gnus-sorted-complement
6122             (gnus-uncompress-range active)
6123             (gnus-list-of-unread-articles group))
6124            (cdr (assq 'dormant marked)))
6125           (cdr (assq 'tick marked))))))
6126
6127 ;; Various summary commands
6128
6129 (defun gnus-summary-select-article-buffer ()
6130   "Reconfigure windows to show article buffer."
6131   (interactive)
6132   (if (not (gnus-buffer-live-p gnus-article-buffer))
6133       (error "There is no article buffer for this summary buffer")
6134     (gnus-configure-windows 'article)
6135     (select-window (get-buffer-window gnus-article-buffer))))
6136
6137 (defun gnus-summary-universal-argument (arg)
6138   "Perform any operation on all articles that are process/prefixed."
6139   (interactive "P")
6140   (let ((articles (gnus-summary-work-articles arg))
6141         func article)
6142     (if (eq
6143          (setq
6144           func
6145           (key-binding
6146            (read-key-sequence
6147             (substitute-command-keys
6148              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6149          'undefined)
6150         (gnus-error 1 "Undefined key")
6151       (save-excursion
6152         (while articles
6153           (gnus-summary-goto-subject (setq article (pop articles)))
6154           (let (gnus-newsgroup-processable)
6155             (command-execute func))
6156           (gnus-summary-remove-process-mark article)))))
6157   (gnus-summary-position-point))
6158
6159 (defun gnus-summary-toggle-truncation (&optional arg)
6160   "Toggle truncation of summary lines.
6161 With arg, turn line truncation on iff arg is positive."
6162   (interactive "P")
6163   (setq truncate-lines
6164         (if (null arg) (not truncate-lines)
6165           (> (prefix-numeric-value arg) 0)))
6166   (redraw-display))
6167
6168 (defun gnus-summary-reselect-current-group (&optional all rescan)
6169   "Exit and then reselect the current newsgroup.
6170 The prefix argument ALL means to select all articles."
6171   (interactive "P")
6172   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6173     (error "Ephemeral groups can't be reselected"))
6174   (let ((current-subject (gnus-summary-article-number))
6175         (group gnus-newsgroup-name))
6176     (setq gnus-newsgroup-begin nil)
6177     (gnus-summary-exit)
6178     ;; We have to adjust the point of group mode buffer because
6179     ;; point was moved to the next unread newsgroup by exiting.
6180     (gnus-summary-jump-to-group group)
6181     (when rescan
6182       (save-excursion
6183         (save-window-excursion
6184           ;; Don't show group contents.
6185           (set-window-start (selected-window) (point-max))
6186           (gnus-group-get-new-news-this-group 1))))
6187     (gnus-group-read-group all t)
6188     (gnus-summary-goto-subject current-subject nil t)))
6189
6190 (defun gnus-summary-rescan-group (&optional all)
6191   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6192   (interactive "P")
6193   (gnus-summary-reselect-current-group all t))
6194
6195 (defun gnus-summary-update-info (&optional non-destructive)
6196   (save-excursion
6197     (let ((group gnus-newsgroup-name))
6198       (when group
6199         (when gnus-newsgroup-kill-headers
6200           (setq gnus-newsgroup-killed
6201                 (gnus-compress-sequence
6202                  (gnus-sorted-union
6203                   (gnus-list-range-intersection
6204                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6205                   gnus-newsgroup-unreads)
6206                  t)))
6207         (unless (listp (cdr gnus-newsgroup-killed))
6208           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6209         (let ((headers gnus-newsgroup-headers))
6210           ;; Set the new ranges of read articles.
6211           (save-excursion
6212             (set-buffer gnus-group-buffer)
6213             (gnus-undo-force-boundary))
6214           (gnus-update-read-articles
6215            group (gnus-sorted-union
6216                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6217           ;; Set the current article marks.
6218           (let ((gnus-newsgroup-scored
6219                  (if (and (not gnus-save-score)
6220                           (not non-destructive))
6221                      nil
6222                    gnus-newsgroup-scored)))
6223             (save-excursion
6224               (gnus-update-marks)))
6225           ;; Do the cross-ref thing.
6226           (when gnus-use-cross-reference
6227             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6228           ;; Do not switch windows but change the buffer to work.
6229           (set-buffer gnus-group-buffer)
6230           (unless (gnus-ephemeral-group-p group)
6231             (gnus-group-update-group group)))))))
6232
6233 (defun gnus-summary-save-newsrc (&optional force)
6234   "Save the current number of read/marked articles in the dribble buffer.
6235 The dribble buffer will then be saved.
6236 If FORCE (the prefix), also save the .newsrc file(s)."
6237   (interactive "P")
6238   (gnus-summary-update-info t)
6239   (if force
6240       (gnus-save-newsrc-file)
6241     (gnus-dribble-save)))
6242
6243 (defun gnus-summary-exit (&optional temporary)
6244   "Exit reading current newsgroup, and then return to group selection mode.
6245 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6246   (interactive)
6247   (gnus-set-global-variables)
6248   (gnus-kill-save-kill-buffer)
6249   (gnus-async-halt-prefetch)
6250   (let* ((group gnus-newsgroup-name)
6251          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6252          (mode major-mode)
6253          (group-point nil)
6254          (buf (current-buffer)))
6255     (unless quit-config
6256       ;; Do adaptive scoring, and possibly save score files.
6257       (when gnus-newsgroup-adaptive
6258         (gnus-score-adaptive))
6259       (when gnus-use-scoring
6260         (gnus-score-save)))
6261     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6262     ;; If we have several article buffers, we kill them at exit.
6263     (unless gnus-single-article-buffer
6264       (gnus-kill-buffer gnus-original-article-buffer)
6265       (setq gnus-article-current nil))
6266     (when gnus-use-cache
6267       (gnus-cache-possibly-remove-articles)
6268       (gnus-cache-save-buffers))
6269     (gnus-async-prefetch-remove-group group)
6270     (when gnus-suppress-duplicates
6271       (gnus-dup-enter-articles))
6272     (when gnus-use-trees
6273       (gnus-tree-close group))
6274     (when gnus-use-cache
6275       (gnus-cache-write-active))
6276     ;; Remove entries for this group.
6277     (nnmail-purge-split-history (gnus-group-real-name group))
6278     ;; Make all changes in this group permanent.
6279     (unless quit-config
6280       (gnus-run-hooks 'gnus-exit-group-hook)
6281       (gnus-summary-update-info))
6282     (gnus-close-group group)
6283     ;; Make sure where we were, and go to next newsgroup.
6284     (set-buffer gnus-group-buffer)
6285     (unless quit-config
6286       (gnus-group-jump-to-group group))
6287     (gnus-run-hooks 'gnus-summary-exit-hook)
6288     (unless (or quit-config
6289                 ;; If this group has disappeared from the summary
6290                 ;; buffer, don't skip forwards.
6291                 (not (string= group (gnus-group-group-name))))
6292       (gnus-group-next-unread-group 1))
6293     (setq group-point (point))
6294     (if temporary
6295         nil                             ;Nothing to do.
6296       ;; If we have several article buffers, we kill them at exit.
6297       (unless gnus-single-article-buffer
6298         (gnus-kill-buffer gnus-article-buffer)
6299         (gnus-kill-buffer gnus-original-article-buffer)
6300         (setq gnus-article-current nil))
6301       (set-buffer buf)
6302       (if (not gnus-kill-summary-on-exit)
6303           (progn
6304             (gnus-deaden-summary)
6305             (setq mode nil))
6306         ;; We set all buffer-local variables to nil.  It is unclear why
6307         ;; this is needed, but if we don't, buffer-local variables are
6308         ;; not garbage-collected, it seems.  This would the lead to en
6309         ;; ever-growing Emacs.
6310         (gnus-summary-clear-local-variables)
6311         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6312           (gnus-summary-clear-local-variables))
6313         (when (get-buffer gnus-article-buffer)
6314           (bury-buffer gnus-article-buffer))
6315         ;; We clear the global counterparts of the buffer-local
6316         ;; variables as well, just to be on the safe side.
6317         (set-buffer gnus-group-buffer)
6318         (gnus-summary-clear-local-variables)
6319         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6320           (gnus-summary-clear-local-variables)))
6321       (setq gnus-current-select-method gnus-select-method)
6322       (pop-to-buffer gnus-group-buffer)
6323       (if (not quit-config)
6324           (progn
6325             (goto-char group-point)
6326             (gnus-configure-windows 'group 'force)
6327             (unless (pos-visible-in-window-p)
6328               (forward-line (/ (static-if (featurep 'xemacs)
6329                                    (window-displayed-height)
6330                                  (1- (window-height)))
6331                                -2))
6332               (set-window-start (selected-window) (point))
6333               (goto-char group-point)))
6334         (gnus-handle-ephemeral-exit quit-config))
6335       ;; Return to group mode buffer.
6336       (when (eq mode 'gnus-summary-mode)
6337         (gnus-kill-buffer buf))
6338       ;; Clear the current group name.
6339       (unless quit-config
6340         (setq gnus-newsgroup-name nil)))))
6341
6342 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6343 (defun gnus-summary-exit-no-update (&optional no-questions)
6344   "Quit reading current newsgroup without updating read article info."
6345   (interactive)
6346   (let* ((group gnus-newsgroup-name)
6347          (quit-config (gnus-group-quit-config group)))
6348     (when (or no-questions
6349               gnus-expert-user
6350               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6351       (gnus-async-halt-prefetch)
6352       (mapcar 'funcall
6353               (delq 'gnus-summary-expire-articles
6354                     (copy-sequence gnus-summary-prepare-exit-hook)))
6355       ;; If we have several article buffers, we kill them at exit.
6356       (unless gnus-single-article-buffer
6357         (gnus-kill-buffer gnus-article-buffer)
6358         (gnus-kill-buffer gnus-original-article-buffer)
6359         (setq gnus-article-current nil))
6360       (if (not gnus-kill-summary-on-exit)
6361           (gnus-deaden-summary)
6362         (gnus-close-group group)
6363         (gnus-summary-clear-local-variables)
6364         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6365           (gnus-summary-clear-local-variables))
6366         (set-buffer gnus-group-buffer)
6367         (gnus-summary-clear-local-variables)
6368         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6369           (gnus-summary-clear-local-variables))
6370         (when (get-buffer gnus-summary-buffer)
6371           (kill-buffer gnus-summary-buffer)))
6372       (unless gnus-single-article-buffer
6373         (setq gnus-article-current nil))
6374       (when gnus-use-trees
6375         (gnus-tree-close group))
6376       (gnus-async-prefetch-remove-group group)
6377       (when (get-buffer gnus-article-buffer)
6378         (bury-buffer gnus-article-buffer))
6379       ;; Return to the group buffer.
6380       (gnus-configure-windows 'group 'force)
6381       ;; Clear the current group name.
6382       (setq gnus-newsgroup-name nil)
6383       (when (equal (gnus-group-group-name) group)
6384         (gnus-group-next-unread-group 1))
6385       (when quit-config
6386         (gnus-handle-ephemeral-exit quit-config)))))
6387
6388 (defun gnus-handle-ephemeral-exit (quit-config)
6389   "Handle movement when leaving an ephemeral group.
6390 The state which existed when entering the ephemeral is reset."
6391   (if (not (buffer-name (car quit-config)))
6392       (gnus-configure-windows 'group 'force)
6393     (set-buffer (car quit-config))
6394     (cond ((eq major-mode 'gnus-summary-mode)
6395            (gnus-set-global-variables))
6396           ((eq major-mode 'gnus-article-mode)
6397            (save-excursion
6398              ;; The `gnus-summary-buffer' variable may point
6399              ;; to the old summary buffer when using a single
6400              ;; article buffer.
6401              (unless (gnus-buffer-live-p gnus-summary-buffer)
6402                (set-buffer gnus-group-buffer))
6403              (set-buffer gnus-summary-buffer)
6404              (gnus-set-global-variables))))
6405     (if (or (eq (cdr quit-config) 'article)
6406             (eq (cdr quit-config) 'pick))
6407         (progn
6408           ;; The current article may be from the ephemeral group
6409           ;; thus it is best that we reload this article
6410           (gnus-summary-show-article)
6411           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6412               (gnus-configure-windows 'pick 'force)
6413             (gnus-configure-windows (cdr quit-config) 'force)))
6414       (gnus-configure-windows (cdr quit-config) 'force))
6415     (when (eq major-mode 'gnus-summary-mode)
6416       (gnus-summary-next-subject 1 nil t)
6417       (gnus-summary-recenter)
6418       (gnus-summary-position-point))))
6419
6420 (defun gnus-summary-preview-mime-message ()
6421   "MIME decode and play this message."
6422   (interactive)
6423   (let ((gnus-break-pages nil)
6424         (gnus-show-mime t))
6425     (gnus-summary-select-article gnus-show-all-headers t))
6426   (let ((w (get-buffer-window gnus-article-buffer)))
6427     (when w
6428       (select-window (get-buffer-window gnus-article-buffer)))))
6429
6430 ;;; Dead summaries.
6431
6432 (defvar gnus-dead-summary-mode-map nil)
6433
6434 (unless gnus-dead-summary-mode-map
6435   (setq gnus-dead-summary-mode-map (make-keymap))
6436   (suppress-keymap gnus-dead-summary-mode-map)
6437   (substitute-key-definition
6438    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6439   (dolist (key '("\C-d" "\r" "\177" [delete]))
6440     (define-key gnus-dead-summary-mode-map
6441       key 'gnus-summary-wake-up-the-dead))
6442   (dolist (key '("q" "Q"))
6443     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6444
6445 (defvar gnus-dead-summary-mode nil
6446   "Minor mode for Gnus summary buffers.")
6447
6448 (defun gnus-dead-summary-mode (&optional arg)
6449   "Minor mode for Gnus summary buffers."
6450   (interactive "P")
6451   (when (eq major-mode 'gnus-summary-mode)
6452     (make-local-variable 'gnus-dead-summary-mode)
6453     (setq gnus-dead-summary-mode
6454           (if (null arg) (not gnus-dead-summary-mode)
6455             (> (prefix-numeric-value arg) 0)))
6456     (when gnus-dead-summary-mode
6457       (gnus-add-minor-mode
6458        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6459
6460 (defun gnus-deaden-summary ()
6461   "Make the current summary buffer into a dead summary buffer."
6462   ;; Kill any previous dead summary buffer.
6463   (when (and gnus-dead-summary
6464              (buffer-name gnus-dead-summary))
6465     (save-excursion
6466       (set-buffer gnus-dead-summary)
6467       (when gnus-dead-summary-mode
6468         (kill-buffer (current-buffer)))))
6469   ;; Make this the current dead summary.
6470   (setq gnus-dead-summary (current-buffer))
6471   (gnus-dead-summary-mode 1)
6472   (let ((name (buffer-name)))
6473     (when (string-match "Summary" name)
6474       (rename-buffer
6475        (concat (substring name 0 (match-beginning 0)) "Dead "
6476                (substring name (match-beginning 0)))
6477        t)
6478       (bury-buffer))))
6479
6480 (defun gnus-kill-or-deaden-summary (buffer)
6481   "Kill or deaden the summary BUFFER."
6482   (save-excursion
6483     (when (and (buffer-name buffer)
6484                (not gnus-single-article-buffer))
6485       (save-excursion
6486         (set-buffer buffer)
6487         (gnus-kill-buffer gnus-article-buffer)
6488         (gnus-kill-buffer gnus-original-article-buffer)))
6489     (cond
6490      ;; Kill the buffer.
6491      (gnus-kill-summary-on-exit
6492       (when (and gnus-use-trees
6493                  (gnus-buffer-exists-p buffer))
6494         (save-excursion
6495           (set-buffer buffer)
6496           (gnus-tree-close gnus-newsgroup-name)))
6497       (gnus-kill-buffer buffer))
6498      ;; Deaden the buffer.
6499      ((gnus-buffer-exists-p buffer)
6500       (save-excursion
6501         (set-buffer buffer)
6502         (gnus-deaden-summary))))))
6503
6504 (defun gnus-summary-wake-up-the-dead (&rest args)
6505   "Wake up the dead summary buffer."
6506   (interactive)
6507   (gnus-dead-summary-mode -1)
6508   (let ((name (buffer-name)))
6509     (when (string-match "Dead " name)
6510       (rename-buffer
6511        (concat (substring name 0 (match-beginning 0))
6512                (substring name (match-end 0)))
6513        t)))
6514   (gnus-message 3 "This dead summary is now alive again"))
6515
6516 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6517 (defun gnus-summary-fetch-faq (&optional faq-dir)
6518   "Fetch the FAQ for the current group.
6519 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6520 in."
6521   (interactive
6522    (list
6523     (when current-prefix-arg
6524       (completing-read
6525        "Faq dir: " (and (listp gnus-group-faq-directory)
6526                         (mapcar (lambda (file) (list file))
6527                                 gnus-group-faq-directory))))))
6528   (let (gnus-faq-buffer)
6529     (when (setq gnus-faq-buffer
6530                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6531       (gnus-configure-windows 'summary-faq))))
6532
6533 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6534 (defun gnus-summary-describe-group (&optional force)
6535   "Describe the current newsgroup."
6536   (interactive "P")
6537   (gnus-group-describe-group force gnus-newsgroup-name))
6538
6539 (defun gnus-summary-describe-briefly ()
6540   "Describe summary mode commands briefly."
6541   (interactive)
6542   (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")))
6543
6544 ;; Walking around group mode buffer from summary mode.
6545
6546 (defun gnus-summary-next-group (&optional no-article target-group backward)
6547   "Exit current newsgroup and then select next unread newsgroup.
6548 If prefix argument NO-ARTICLE is non-nil, no article is selected
6549 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6550 previous group instead."
6551   (interactive "P")
6552   ;; Stop pre-fetching.
6553   (gnus-async-halt-prefetch)
6554   (let ((current-group gnus-newsgroup-name)
6555         (current-buffer (current-buffer))
6556         entered)
6557     (gnus-summary-exit t)
6558     (while (not entered)
6559       ;; Then we find what group we are supposed to enter.
6560       (set-buffer gnus-group-buffer)
6561       (gnus-group-jump-to-group current-group)
6562       (setq target-group
6563             (or target-group
6564                 (if (eq gnus-keep-same-level 'best)
6565                     (gnus-summary-best-group gnus-newsgroup-name)
6566                   (gnus-summary-search-group backward gnus-keep-same-level))))
6567       (if (not target-group)
6568           ;; There are no further groups, so we return to the group
6569           ;; buffer.
6570           (progn
6571             (gnus-message 5 "Returning to the group buffer")
6572             (setq entered t)
6573             (when (gnus-buffer-live-p current-buffer)
6574               (set-buffer current-buffer)
6575               (gnus-summary-exit))
6576             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6577         ;; We try to enter the target group.
6578         (gnus-group-jump-to-group target-group)
6579         (let ((unreads (gnus-group-group-unread)))
6580           (if (and (or (eq t unreads)
6581                        (and unreads (not (zerop unreads))))
6582                    (progn
6583                      ;; Now we semi-exit this group to update Xrefs
6584                      ;; and all variables.  We can't do a real exit,
6585                      ;; because the window conf must remain the same
6586                      ;; in case the user is prompted for info, and we
6587                      ;; don't want the window conf to change before
6588                      ;; that...
6589                      (when (gnus-buffer-live-p current-buffer)
6590                        (set-buffer current-buffer)
6591                        (gnus-summary-exit t))
6592                      (gnus-summary-read-group
6593                       target-group nil no-article
6594                       (and (buffer-name current-buffer) current-buffer)
6595                       nil backward)))
6596               (setq entered t)
6597             (setq current-group target-group
6598                   target-group nil)))))))
6599
6600 (defun gnus-summary-prev-group (&optional no-article)
6601   "Exit current newsgroup and then select previous unread newsgroup.
6602 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6603   (interactive "P")
6604   (gnus-summary-next-group no-article nil t))
6605
6606 ;; Walking around summary lines.
6607
6608 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6609   "Go to the first unread subject.
6610 If UNREAD is non-nil, go to the first unread article.
6611 Returns the article selected or nil if there are no unread articles."
6612   (interactive "P")
6613   (prog1
6614       (cond
6615        ;; Empty summary.
6616        ((null gnus-newsgroup-data)
6617         (gnus-message 3 "No articles in the group")
6618         nil)
6619        ;; Pick the first article.
6620        ((not unread)
6621         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6622         (gnus-data-number (car gnus-newsgroup-data)))
6623        ;; No unread articles.
6624        ((null gnus-newsgroup-unreads)
6625         (gnus-message 3 "No more unread articles")
6626         nil)
6627        ;; Find the first unread article.
6628        (t
6629         (let ((data gnus-newsgroup-data))
6630           (while (and data
6631                       (and (not (and undownloaded
6632                                      (eq gnus-undownloaded-mark
6633                                          (gnus-data-mark (car data)))))
6634                            (if unseen
6635                                (or (not (memq
6636                                          (gnus-data-number (car data))
6637                                          gnus-newsgroup-unseen))
6638                                    (not (gnus-data-unread-p (car data))))
6639                              (not (gnus-data-unread-p (car data))))))
6640             (setq data (cdr data)))
6641           (when data
6642             (goto-char (gnus-data-pos (car data)))
6643             (gnus-data-number (car data))))))
6644     (gnus-summary-position-point)))
6645
6646 (defun gnus-summary-next-subject (n &optional unread dont-display)
6647   "Go to next N'th summary line.
6648 If N is negative, go to the previous N'th subject line.
6649 If UNREAD is non-nil, only unread articles are selected.
6650 The difference between N and the actual number of steps taken is
6651 returned."
6652   (interactive "p")
6653   (let ((backward (< n 0))
6654         (n (abs n)))
6655     (while (and (> n 0)
6656                 (if backward
6657                     (gnus-summary-find-prev unread)
6658                   (gnus-summary-find-next unread)))
6659       (unless (zerop (setq n (1- n)))
6660         (gnus-summary-show-thread)))
6661     (when (/= 0 n)
6662       (gnus-message 7 "No more%s articles"
6663                     (if unread " unread" "")))
6664     (unless dont-display
6665       (gnus-summary-recenter)
6666       (gnus-summary-position-point))
6667     n))
6668
6669 (defun gnus-summary-next-unread-subject (n)
6670   "Go to next N'th unread summary line."
6671   (interactive "p")
6672   (gnus-summary-next-subject n t))
6673
6674 (defun gnus-summary-prev-subject (n &optional unread)
6675   "Go to previous N'th summary line.
6676 If optional argument UNREAD is non-nil, only unread article is selected."
6677   (interactive "p")
6678   (gnus-summary-next-subject (- n) unread))
6679
6680 (defun gnus-summary-prev-unread-subject (n)
6681   "Go to previous N'th unread summary line."
6682   (interactive "p")
6683   (gnus-summary-next-subject (- n) t))
6684
6685 (defun gnus-summary-goto-subject (article &optional force silent)
6686   "Go the subject line of ARTICLE.
6687 If FORCE, also allow jumping to articles not currently shown."
6688   (interactive "nArticle number: ")
6689   (unless (numberp article)
6690     (error "Article %s is not a number" article))
6691   (let ((b (point))
6692         (data (gnus-data-find article)))
6693     ;; We read in the article if we have to.
6694     (and (not data)
6695          force
6696          (gnus-summary-insert-subject
6697           article
6698           (if (or (numberp force) (vectorp force)) force)
6699           t)
6700          (setq data (gnus-data-find article)))
6701     (goto-char b)
6702     (if (not data)
6703         (progn
6704           (unless silent
6705             (gnus-message 3 "Can't find article %d" article))
6706           nil)
6707       (let ((pt (gnus-data-pos data)))
6708         (goto-char pt)
6709         (gnus-summary-set-article-display-arrow pt))
6710       (gnus-summary-position-point)
6711       article)))
6712
6713 ;; Walking around summary lines with displaying articles.
6714
6715 (defun gnus-summary-expand-window (&optional arg)
6716   "Make the summary buffer take up the entire Emacs frame.
6717 Given a prefix, will force an `article' buffer configuration."
6718   (interactive "P")
6719   (if arg
6720       (gnus-configure-windows 'article 'force)
6721     (gnus-configure-windows 'summary 'force)))
6722
6723 (defun gnus-summary-display-article (article &optional all-header)
6724   "Display ARTICLE in article buffer."
6725   (when (gnus-buffer-live-p gnus-article-buffer)
6726     (with-current-buffer gnus-article-buffer
6727       (set-buffer-multibyte t)))
6728   (gnus-set-global-variables)
6729   (when (gnus-buffer-live-p gnus-article-buffer)
6730     (with-current-buffer gnus-article-buffer
6731       (setq gnus-article-charset gnus-newsgroup-charset)
6732       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6733       (set-buffer-multibyte t)))
6734   (if (null article)
6735       nil
6736     (prog1
6737         (if gnus-summary-display-article-function
6738             (funcall gnus-summary-display-article-function article all-header)
6739           (gnus-article-prepare article all-header))
6740       (with-current-buffer gnus-article-buffer
6741         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6742              nil))
6743       (gnus-run-hooks 'gnus-select-article-hook)
6744       (when (and gnus-current-article
6745                  (not (zerop gnus-current-article)))
6746         (gnus-summary-goto-subject gnus-current-article))
6747       (gnus-summary-recenter)
6748       (when (and gnus-use-trees gnus-show-threads)
6749         (gnus-possibly-generate-tree article)
6750         (gnus-highlight-selected-tree article))
6751       ;; Successfully display article.
6752       (gnus-article-set-window-start
6753        (cdr (assq article gnus-newsgroup-bookmarks))))))
6754
6755 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6756   "Select the current article.
6757 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6758 non-nil, the article will be re-fetched even if it already present in
6759 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6760 be displayed."
6761   ;; Make sure we are in the summary buffer to work around bbdb bug.
6762   (unless (eq major-mode 'gnus-summary-mode)
6763     (set-buffer gnus-summary-buffer))
6764   (let ((article (or article (gnus-summary-article-number)))
6765         (all-headers (not (not all-headers))) ;Must be T or NIL.
6766         gnus-summary-display-article-function)
6767     (and (not pseudo)
6768          (gnus-summary-article-pseudo-p article)
6769          (error "This is a pseudo-article"))
6770     (save-excursion
6771       (set-buffer gnus-summary-buffer)
6772       (if (or (and gnus-single-article-buffer
6773                    (or (null gnus-current-article)
6774                        (null gnus-article-current)
6775                        (null (get-buffer gnus-article-buffer))
6776                        (not (eq article (cdr gnus-article-current)))
6777                        (not (equal (car gnus-article-current)
6778                                    gnus-newsgroup-name))))
6779               (and (not gnus-single-article-buffer)
6780                    (or (null gnus-current-article)
6781                        (not (eq gnus-current-article article))))
6782               force)
6783           ;; The requested article is different from the current article.
6784           (progn
6785             (gnus-summary-display-article article all-headers)
6786             (gnus-article-set-window-start
6787              (cdr (assq article gnus-newsgroup-bookmarks)))
6788             article)
6789         'old))))
6790
6791 (defun gnus-summary-force-verify-and-decrypt ()
6792   (interactive)
6793   (let ((mm-verify-option 'known)
6794         (mm-decrypt-option 'known)
6795         (gnus-buttonized-mime-types (append (list "multipart/signed"
6796                                                   "multipart/encrypted")
6797                                             gnus-buttonized-mime-types)))
6798     (gnus-summary-select-article nil 'force)))
6799
6800 (defun gnus-summary-set-current-mark (&optional current-mark)
6801   "Obsolete function."
6802   nil)
6803
6804 (defun gnus-summary-next-article (&optional unread subject backward push)
6805   "Select the next article.
6806 If UNREAD, only unread articles are selected.
6807 If SUBJECT, only articles with SUBJECT are selected.
6808 If BACKWARD, the previous article is selected instead of the next."
6809   (interactive "P")
6810   (cond
6811    ;; Is there such an article?
6812    ((and (gnus-summary-search-forward unread subject backward)
6813          (or (gnus-summary-display-article (gnus-summary-article-number))
6814              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6815     (gnus-summary-position-point))
6816    ;; If not, we try the first unread, if that is wanted.
6817    ((and subject
6818          gnus-auto-select-same
6819          (gnus-summary-first-unread-article))
6820     (gnus-summary-position-point)
6821     (gnus-message 6 "Wrapped"))
6822    ;; Try to get next/previous article not displayed in this group.
6823    ((and gnus-auto-extend-newsgroup
6824          (not unread) (not subject))
6825     (gnus-summary-goto-article
6826      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6827      nil (count-lines (point-min) (point))))
6828    ;; Go to next/previous group.
6829    (t
6830     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6831       (gnus-summary-jump-to-group gnus-newsgroup-name))
6832     (let ((cmd last-command-char)
6833           (point
6834            (save-excursion
6835              (set-buffer gnus-group-buffer)
6836              (point)))
6837           (group
6838            (if (eq gnus-keep-same-level 'best)
6839                (gnus-summary-best-group gnus-newsgroup-name)
6840              (gnus-summary-search-group backward gnus-keep-same-level))))
6841       ;; For some reason, the group window gets selected.  We change
6842       ;; it back.
6843       (select-window (get-buffer-window (current-buffer)))
6844       ;; Select next unread newsgroup automagically.
6845       (cond
6846        ((or (not gnus-auto-select-next)
6847             (not cmd))
6848         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6849        ((or (eq gnus-auto-select-next 'quietly)
6850             (and (eq gnus-auto-select-next 'slightly-quietly)
6851                  push)
6852             (and (eq gnus-auto-select-next 'almost-quietly)
6853                  (gnus-summary-last-article-p)))
6854         ;; Select quietly.
6855         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6856             (gnus-summary-exit)
6857           (gnus-message 7 "No more%s articles (%s)..."
6858                         (if unread " unread" "")
6859                         (if group (concat "selecting " group)
6860                           "exiting"))
6861           (gnus-summary-next-group nil group backward)))
6862        (t
6863         (when (gnus-key-press-event-p last-input-event)
6864           (gnus-summary-walk-group-buffer
6865            gnus-newsgroup-name cmd unread backward point))))))))
6866
6867 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6868   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6869                       (?\C-p (gnus-group-prev-unread-group 1))))
6870         (cursor-in-echo-area t)
6871         keve key group ended)
6872     (save-excursion
6873       (set-buffer gnus-group-buffer)
6874       (goto-char start)
6875       (setq group
6876             (if (eq gnus-keep-same-level 'best)
6877                 (gnus-summary-best-group gnus-newsgroup-name)
6878               (gnus-summary-search-group backward gnus-keep-same-level))))
6879     (while (not ended)
6880       (gnus-message
6881        5 "No more%s articles%s" (if unread " unread" "")
6882        (if (and group
6883                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6884            (format " (Type %s for %s [%s])"
6885                    (single-key-description cmd) group
6886                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6887          (format " (Type %s to exit %s)"
6888                  (single-key-description cmd)
6889                  gnus-newsgroup-name)))
6890       ;; Confirm auto selection.
6891       (setq key (car (setq keve (gnus-read-event-char))))
6892       (setq ended t)
6893       (cond
6894        ((assq key keystrokes)
6895         (let ((obuf (current-buffer)))
6896           (switch-to-buffer gnus-group-buffer)
6897           (when group
6898             (gnus-group-jump-to-group group))
6899           (eval (cadr (assq key keystrokes)))
6900           (setq group (gnus-group-group-name))
6901           (switch-to-buffer obuf))
6902         (setq ended nil))
6903        ((equal key cmd)
6904         (if (or (not group)
6905                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6906             (gnus-summary-exit)
6907           (gnus-summary-next-group nil group backward)))
6908        (t
6909         (push (cdr keve) unread-command-events))))))
6910
6911 (defun gnus-summary-next-unread-article ()
6912   "Select unread article after current one."
6913   (interactive)
6914   (gnus-summary-next-article
6915    (or (not (eq gnus-summary-goto-unread 'never))
6916        (gnus-summary-last-article-p (gnus-summary-article-number)))
6917    (and gnus-auto-select-same
6918         (gnus-summary-article-subject))))
6919
6920 (defun gnus-summary-prev-article (&optional unread subject)
6921   "Select the article after the current one.
6922 If UNREAD is non-nil, only unread articles are selected."
6923   (interactive "P")
6924   (gnus-summary-next-article unread subject t))
6925
6926 (defun gnus-summary-prev-unread-article ()
6927   "Select unread article before current one."
6928   (interactive)
6929   (gnus-summary-prev-article
6930    (or (not (eq gnus-summary-goto-unread 'never))
6931        (gnus-summary-first-article-p (gnus-summary-article-number)))
6932    (and gnus-auto-select-same
6933         (gnus-summary-article-subject))))
6934
6935 (defun gnus-summary-next-page (&optional lines circular)
6936   "Show next page of the selected article.
6937 If at the end of the current article, select the next article.
6938 LINES says how many lines should be scrolled up.
6939
6940 If CIRCULAR is non-nil, go to the start of the article instead of
6941 selecting the next article when reaching the end of the current
6942 article."
6943   (interactive "P")
6944   (setq gnus-summary-buffer (current-buffer))
6945   (gnus-set-global-variables)
6946   (let ((article (gnus-summary-article-number))
6947         (article-window (get-buffer-window gnus-article-buffer t))
6948         endp)
6949     ;; If the buffer is empty, we have no article.
6950     (unless article
6951       (error "No article to select"))
6952     (gnus-configure-windows 'article)
6953     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6954         (if (and (eq gnus-summary-goto-unread 'never)
6955                  (not (gnus-summary-last-article-p article)))
6956             (gnus-summary-next-article)
6957           (gnus-summary-next-unread-article))
6958       (if (or (null gnus-current-article)
6959               (null gnus-article-current)
6960               (/= article (cdr gnus-article-current))
6961               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6962           ;; Selected subject is different from current article's.
6963           (gnus-summary-display-article article)
6964         (when article-window
6965           (gnus-eval-in-buffer-window gnus-article-buffer
6966             (setq endp (gnus-article-next-page lines)))
6967           (when endp
6968             (cond (circular
6969                    (gnus-summary-beginning-of-article))
6970                   (lines
6971                    (gnus-message 3 "End of message"))
6972                   ((null lines)
6973                    (if (and (eq gnus-summary-goto-unread 'never)
6974                             (not (gnus-summary-last-article-p article)))
6975                        (gnus-summary-next-article)
6976                      (gnus-summary-next-unread-article))))))))
6977     (gnus-summary-recenter)
6978     (gnus-summary-position-point)))
6979
6980 (defun gnus-summary-prev-page (&optional lines move)
6981   "Show previous page of selected article.
6982 Argument LINES specifies lines to be scrolled down.
6983 If MOVE, move to the previous unread article if point is at
6984 the beginning of the buffer."
6985   (interactive "P")
6986   (let ((article (gnus-summary-article-number))
6987         (article-window (get-buffer-window gnus-article-buffer t))
6988         endp)
6989     (gnus-configure-windows 'article)
6990     (if (or (null gnus-current-article)
6991             (null gnus-article-current)
6992             (/= article (cdr gnus-article-current))
6993             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6994         ;; Selected subject is different from current article's.
6995         (gnus-summary-display-article article)
6996       (gnus-summary-recenter)
6997       (when article-window
6998         (gnus-eval-in-buffer-window gnus-article-buffer
6999           (setq endp (gnus-article-prev-page lines)))
7000         (when (and move endp)
7001           (cond (lines
7002                  (gnus-message 3 "Beginning of message"))
7003                 ((null lines)
7004                  (if (and (eq gnus-summary-goto-unread 'never)
7005                           (not (gnus-summary-first-article-p article)))
7006                      (gnus-summary-prev-article)
7007                    (gnus-summary-prev-unread-article))))))))
7008   (gnus-summary-position-point))
7009
7010 (defun gnus-summary-prev-page-or-article (&optional lines)
7011   "Show previous page of selected article.
7012 Argument LINES specifies lines to be scrolled down.
7013 If at the beginning of the article, go to the next article."
7014   (interactive "P")
7015   (gnus-summary-prev-page lines t))
7016
7017 (defun gnus-summary-scroll-up (lines)
7018   "Scroll up (or down) one line current article.
7019 Argument LINES specifies lines to be scrolled up (or down if negative)."
7020   (interactive "p")
7021   (gnus-configure-windows 'article)
7022   (gnus-summary-show-thread)
7023   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7024     (gnus-eval-in-buffer-window gnus-article-buffer
7025       (cond ((> lines 0)
7026              (when (gnus-article-next-page lines)
7027                (gnus-message 3 "End of message")))
7028             ((< lines 0)
7029              (gnus-article-prev-page (- lines))))))
7030   (gnus-summary-recenter)
7031   (gnus-summary-position-point))
7032
7033 (defun gnus-summary-scroll-down (lines)
7034   "Scroll down (or up) one line current article.
7035 Argument LINES specifies lines to be scrolled down (or up if negative)."
7036   (interactive "p")
7037   (gnus-summary-scroll-up (- lines)))
7038
7039 (defun gnus-summary-next-same-subject ()
7040   "Select next article which has the same subject as current one."
7041   (interactive)
7042   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7043
7044 (defun gnus-summary-prev-same-subject ()
7045   "Select previous article which has the same subject as current one."
7046   (interactive)
7047   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7048
7049 (defun gnus-summary-next-unread-same-subject ()
7050   "Select next unread article which has the same subject as current one."
7051   (interactive)
7052   (gnus-summary-next-article t (gnus-summary-article-subject)))
7053
7054 (defun gnus-summary-prev-unread-same-subject ()
7055   "Select previous unread article which has the same subject as current one."
7056   (interactive)
7057   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7058
7059 (defun gnus-summary-first-unread-article ()
7060   "Select the first unread article.
7061 Return nil if there are no unread articles."
7062   (interactive)
7063   (prog1
7064       (when (gnus-summary-first-subject t)
7065         (gnus-summary-show-thread)
7066         (gnus-summary-first-subject t)
7067         (gnus-summary-display-article (gnus-summary-article-number)))
7068     (gnus-summary-position-point)))
7069
7070 (defun gnus-summary-first-unread-subject ()
7071   "Place the point on the subject line of the first unread article.
7072 Return nil if there are no unread articles."
7073   (interactive)
7074   (prog1
7075       (when (gnus-summary-first-subject t)
7076         (gnus-summary-show-thread)
7077         (gnus-summary-first-subject t))
7078     (gnus-summary-position-point)))
7079
7080 (defun gnus-summary-first-unseen-subject ()
7081   "Place the point on the subject line of the first unseen article.
7082 Return nil if there are no unseen articles."
7083   (interactive)
7084   (prog1
7085       (when (gnus-summary-first-subject t t t)
7086         (gnus-summary-show-thread)
7087         (gnus-summary-first-subject t t t))
7088     (gnus-summary-position-point)))
7089
7090 (defun gnus-summary-first-unseen-or-unread-subject ()
7091   "Place the point on the subject line of the first unseen article.
7092 Return nil if there are no unseen articles."
7093   (interactive)
7094   (prog1
7095       (unless (when (gnus-summary-first-subject t t t)
7096                 (gnus-summary-show-thread)
7097                 (gnus-summary-first-subject t t t))
7098         (when (gnus-summary-first-subject t)
7099           (gnus-summary-show-thread)
7100           (gnus-summary-first-subject t)))
7101     (gnus-summary-position-point)))
7102
7103 (defun gnus-summary-first-article ()
7104   "Select the first article.
7105 Return nil if there are no articles."
7106   (interactive)
7107   (prog1
7108       (when (gnus-summary-first-subject)
7109         (gnus-summary-show-thread)
7110         (gnus-summary-first-subject)
7111         (gnus-summary-display-article (gnus-summary-article-number)))
7112     (gnus-summary-position-point)))
7113
7114 (defun gnus-summary-best-unread-article (&optional arg)
7115   "Select the unread article with the highest score.
7116 If given a prefix argument, select the next unread article that has a
7117 score higher than the default score."
7118   (interactive "P")
7119   (let ((article (if arg
7120                      (gnus-summary-better-unread-subject)
7121                    (gnus-summary-best-unread-subject))))
7122     (if article
7123         (gnus-summary-goto-article article)
7124       (error "No unread articles"))))
7125
7126 (defun gnus-summary-best-unread-subject ()
7127   "Select the unread subject with the highest score."
7128   (interactive)
7129   (let ((best -1000000)
7130         (data gnus-newsgroup-data)
7131         article score)
7132     (while data
7133       (and (gnus-data-unread-p (car data))
7134            (> (setq score
7135                     (gnus-summary-article-score (gnus-data-number (car data))))
7136               best)
7137            (setq best score
7138                  article (gnus-data-number (car data))))
7139       (setq data (cdr data)))
7140     (when article
7141       (gnus-summary-goto-subject article))
7142     (gnus-summary-position-point)
7143     article))
7144
7145 (defun gnus-summary-better-unread-subject ()
7146   "Select the first unread subject that has a score over the default score."
7147   (interactive)
7148   (let ((data gnus-newsgroup-data)
7149         article score)
7150     (while (and (setq article (gnus-data-number (car data)))
7151                 (or (gnus-data-read-p (car data))
7152                     (not (> (gnus-summary-article-score article)
7153                             gnus-summary-default-score))))
7154       (setq data (cdr data)))
7155     (when article
7156       (gnus-summary-goto-subject article))
7157     (gnus-summary-position-point)
7158     article))
7159
7160 (defun gnus-summary-last-subject ()
7161   "Go to the last displayed subject line in the group."
7162   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7163     (when article
7164       (gnus-summary-goto-subject article))))
7165
7166 (defun gnus-summary-goto-article (article &optional all-headers force)
7167   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7168 If ALL-HEADERS is non-nil, no header lines are hidden.
7169 If FORCE, go to the article even if it isn't displayed.  If FORCE
7170 is a number, it is the line the article is to be displayed on."
7171   (interactive
7172    (list
7173     (completing-read
7174      "Article number or Message-ID: "
7175      (mapcar (lambda (number) (list (int-to-string number)))
7176              gnus-newsgroup-limit))
7177     current-prefix-arg
7178     t))
7179   (prog1
7180       (if (and (stringp article)
7181                (string-match "@" article))
7182           (gnus-summary-refer-article article)
7183         (when (stringp article)
7184           (setq article (string-to-number article)))
7185         (if (gnus-summary-goto-subject article force)
7186             (gnus-summary-display-article article all-headers)
7187           (gnus-message 4 "Couldn't go to article %s" article) nil))
7188     (gnus-summary-position-point)))
7189
7190 (defun gnus-summary-goto-last-article ()
7191   "Go to the previously read article."
7192   (interactive)
7193   (prog1
7194       (when gnus-last-article
7195         (gnus-summary-goto-article gnus-last-article nil t))
7196     (gnus-summary-position-point)))
7197
7198 (defun gnus-summary-pop-article (number)
7199   "Pop one article off the history and go to the previous.
7200 NUMBER articles will be popped off."
7201   (interactive "p")
7202   (let (to)
7203     (setq gnus-newsgroup-history
7204           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7205     (if to
7206         (gnus-summary-goto-article (car to) nil t)
7207       (error "Article history empty")))
7208   (gnus-summary-position-point))
7209
7210 ;; Summary commands and functions for limiting the summary buffer.
7211
7212 (defun gnus-summary-limit-to-articles (n)
7213   "Limit the summary buffer to the next N articles.
7214 If not given a prefix, use the process marked articles instead."
7215   (interactive "P")
7216   (prog1
7217       (let ((articles (gnus-summary-work-articles n)))
7218         (setq gnus-newsgroup-processable nil)
7219         (gnus-summary-limit articles))
7220     (gnus-summary-position-point)))
7221
7222 (defun gnus-summary-pop-limit (&optional total)
7223   "Restore the previous limit.
7224 If given a prefix, remove all limits."
7225   (interactive "P")
7226   (when total
7227     (setq gnus-newsgroup-limits
7228           (list (mapcar (lambda (h) (mail-header-number h))
7229                         gnus-newsgroup-headers))))
7230   (unless gnus-newsgroup-limits
7231     (error "No limit to pop"))
7232   (prog1
7233       (gnus-summary-limit nil 'pop)
7234     (gnus-summary-position-point)))
7235
7236 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7237   "Limit the summary buffer to articles that have subjects that match a regexp.
7238 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7239   (interactive
7240    (list (read-string (if current-prefix-arg
7241                           "Exclude subject (regexp): "
7242                         "Limit to subject (regexp): "))
7243          nil current-prefix-arg))
7244   (unless header
7245     (setq header "subject"))
7246   (when (not (equal "" subject))
7247     (prog1
7248         (let ((articles (gnus-summary-find-matching
7249                          (or header "subject") subject 'all nil nil
7250                          not-matching)))
7251           (unless articles
7252             (error "Found no matches for \"%s\"" subject))
7253           (gnus-summary-limit articles))
7254       (gnus-summary-position-point))))
7255
7256 (defun gnus-summary-limit-to-author (from &optional not-matching)
7257   "Limit the summary buffer to articles that have authors that match a regexp.
7258 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7259   (interactive
7260    (list (read-string (if current-prefix-arg
7261                           "Exclude author (regexp): "
7262                         "Limit to author (regexp): "))
7263          current-prefix-arg))
7264   (gnus-summary-limit-to-subject from "from" not-matching))
7265
7266 (defun gnus-summary-limit-to-age (age &optional younger-p)
7267   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7268 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7269 articles that are younger than AGE days."
7270   (interactive
7271    (let ((younger current-prefix-arg)
7272          (days-got nil)
7273          days)
7274      (while (not days-got)
7275        (setq days (if younger
7276                       (read-string "Limit to articles within (in days): ")
7277                     (read-string "Limit to articles older than (in days): ")))
7278        (when (> (length days) 0)
7279          (setq days (read days)))
7280        (if (numberp days)
7281            (progn
7282              (setq days-got t)
7283              (if (< days 0)
7284                  (progn
7285                    (setq younger (not younger))
7286                    (setq days (* days -1)))))
7287          (message "Please enter a number.")
7288          (sleep-for 1)))
7289      (list days younger)))
7290   (prog1
7291       (let ((data gnus-newsgroup-data)
7292             (cutoff (days-to-time age))
7293             articles d date is-younger)
7294         (while (setq d (pop data))
7295           (when (and (vectorp (gnus-data-header d))
7296                      (setq date (mail-header-date (gnus-data-header d))))
7297             (setq is-younger (time-less-p
7298                               (time-since (condition-case ()
7299                                               (date-to-time date)
7300                                             (error '(0 0))))
7301                               cutoff))
7302             (when (if younger-p
7303                       is-younger
7304                     (not is-younger))
7305               (push (gnus-data-number d) articles))))
7306         (gnus-summary-limit (nreverse articles)))
7307     (gnus-summary-position-point)))
7308
7309 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7310   "Limit the summary buffer to articles that match an 'extra' header."
7311   (interactive
7312    (let ((header
7313           (intern
7314            (gnus-completing-read-with-default
7315             (symbol-name (car gnus-extra-headers))
7316             (if current-prefix-arg
7317                 "Exclude extra header:"
7318               "Limit extra header:")
7319             (mapcar (lambda (x)
7320                       (cons (symbol-name x) x))
7321                     gnus-extra-headers)
7322             nil
7323             t))))
7324      (list header
7325            (read-string (format "%s header %s (regexp): "
7326                                 (if current-prefix-arg "Exclude" "Limit to")
7327                                 header))
7328            current-prefix-arg)))
7329   (when (not (equal "" regexp))
7330     (prog1
7331         (let ((articles (gnus-summary-find-matching
7332                          (cons 'extra header) regexp 'all nil nil
7333                          not-matching)))
7334           (unless articles
7335             (error "Found no matches for \"%s\"" regexp))
7336           (gnus-summary-limit articles))
7337       (gnus-summary-position-point))))
7338
7339 (defun gnus-summary-limit-to-display-predicate ()
7340   "Limit the summary buffer to the predicated in the `display' group parameter."
7341   (interactive)
7342   (unless gnus-newsgroup-display
7343     (error "There is no `display' group parameter"))
7344   (let (articles)
7345     (dolist (number gnus-newsgroup-articles)
7346       (when (funcall gnus-newsgroup-display)
7347         (push number articles)))
7348     (gnus-summary-limit articles))
7349   (gnus-summary-position-point))
7350
7351 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7352 (make-obsolete
7353  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7354
7355 (defun gnus-summary-limit-to-unread (&optional all)
7356   "Limit the summary buffer to articles that are not marked as read.
7357 If ALL is non-nil, limit strictly to unread articles."
7358   (interactive "P")
7359   (if all
7360       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7361     (gnus-summary-limit-to-marks
7362      ;; Concat all the marks that say that an article is read and have
7363      ;; those removed.
7364      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7365            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7366            gnus-low-score-mark gnus-expirable-mark
7367            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7368            gnus-duplicate-mark gnus-souped-mark)
7369      'reverse)))
7370
7371 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7372 (make-obsolete 'gnus-summary-delete-marked-with
7373                'gnus-summary-limit-exclude-marks)
7374
7375 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7376   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7377 If REVERSE, limit the summary buffer to articles that are marked
7378 with MARKS.  MARKS can either be a string of marks or a list of marks.
7379 Returns how many articles were removed."
7380   (interactive "sMarks: ")
7381   (gnus-summary-limit-to-marks marks t))
7382
7383 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7384   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7385 If REVERSE (the prefix), limit the summary buffer to articles that are
7386 not marked with MARKS.  MARKS can either be a string of marks or a
7387 list of marks.
7388 Returns how many articles were removed."
7389   (interactive "sMarks: \nP")
7390   (prog1
7391       (let ((data gnus-newsgroup-data)
7392             (marks (if (listp marks) marks
7393                      (append marks nil))) ; Transform to list.
7394             articles)
7395         (while data
7396           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7397                   (memq (gnus-data-mark (car data)) marks))
7398             (push (gnus-data-number (car data)) articles))
7399           (setq data (cdr data)))
7400         (gnus-summary-limit articles))
7401     (gnus-summary-position-point)))
7402
7403 (defun gnus-summary-limit-to-score (score)
7404   "Limit to articles with score at or above SCORE."
7405   (interactive "NLimit to articles with score of at least: ")
7406   (let ((data gnus-newsgroup-data)
7407         articles)
7408     (while data
7409       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7410                 score)
7411         (push (gnus-data-number (car data)) articles))
7412       (setq data (cdr data)))
7413     (prog1
7414         (gnus-summary-limit articles)
7415       (gnus-summary-position-point))))
7416
7417 (defun gnus-summary-limit-to-unseen ()
7418   "Limit to unseen articles."
7419   (interactive)
7420   (prog1
7421       (gnus-summary-limit gnus-newsgroup-unseen)
7422     (gnus-summary-position-point)))
7423
7424 (defun gnus-summary-limit-include-thread (id)
7425   "Display all the hidden articles that is in the thread with ID in it.
7426 When called interactively, ID is the Message-ID of the current
7427 article."
7428   (interactive (list (mail-header-id (gnus-summary-article-header))))
7429   (let ((articles (gnus-articles-in-thread
7430                    (gnus-id-to-thread (gnus-root-id id)))))
7431     (prog1
7432         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7433       (gnus-summary-limit-include-matching-articles
7434        "subject"
7435        (regexp-quote (gnus-simplify-subject-re
7436                       (mail-header-subject (gnus-id-to-header id)))))
7437       (gnus-summary-position-point))))
7438
7439 (defun gnus-summary-limit-include-matching-articles (header regexp)
7440   "Display all the hidden articles that have HEADERs that match REGEXP."
7441   (interactive (list (read-string "Match on header: ")
7442                      (read-string "Regexp: ")))
7443   (let ((articles (gnus-find-matching-articles header regexp)))
7444     (prog1
7445         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7446       (gnus-summary-position-point))))
7447
7448 (defun gnus-summary-limit-include-dormant ()
7449   "Display all the hidden articles that are marked as dormant.
7450 Note that this command only works on a subset of the articles currently
7451 fetched for this group."
7452   (interactive)
7453   (unless gnus-newsgroup-dormant
7454     (error "There are no dormant articles in this group"))
7455   (prog1
7456       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7457     (gnus-summary-position-point)))
7458
7459 (defun gnus-summary-limit-exclude-dormant ()
7460   "Hide all dormant articles."
7461   (interactive)
7462   (prog1
7463       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7464     (gnus-summary-position-point)))
7465
7466 (defun gnus-summary-limit-exclude-childless-dormant ()
7467   "Hide all dormant articles that have no children."
7468   (interactive)
7469   (let ((data (gnus-data-list t))
7470         articles d children)
7471     ;; Find all articles that are either not dormant or have
7472     ;; children.
7473     (while (setq d (pop data))
7474       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7475                 (and (setq children
7476                            (gnus-article-children (gnus-data-number d)))
7477                      (let (found)
7478                        (while children
7479                          (when (memq (car children) articles)
7480                            (setq children nil
7481                                  found t))
7482                          (pop children))
7483                        found)))
7484         (push (gnus-data-number d) articles)))
7485     ;; Do the limiting.
7486     (prog1
7487         (gnus-summary-limit articles)
7488       (gnus-summary-position-point))))
7489
7490 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7491   "Mark all unread excluded articles as read.
7492 If ALL, mark even excluded ticked and dormants as read."
7493   (interactive "P")
7494   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7495   (let ((articles (gnus-sorted-ndifference
7496                    (sort
7497                     (mapcar (lambda (h) (mail-header-number h))
7498                             gnus-newsgroup-headers)
7499                     '<)
7500                    gnus-newsgroup-limit))
7501         article)
7502     (setq gnus-newsgroup-unreads
7503           (gnus-sorted-intersection gnus-newsgroup-unreads
7504                                     gnus-newsgroup-limit))
7505     (if all
7506         (setq gnus-newsgroup-dormant nil
7507               gnus-newsgroup-marked nil
7508               gnus-newsgroup-reads
7509               (nconc
7510                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7511                gnus-newsgroup-reads))
7512       (while (setq article (pop articles))
7513         (unless (or (memq article gnus-newsgroup-dormant)
7514                     (memq article gnus-newsgroup-marked))
7515           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7516
7517 (defun gnus-summary-limit (articles &optional pop)
7518   (if pop
7519       ;; We pop the previous limit off the stack and use that.
7520       (setq articles (car gnus-newsgroup-limits)
7521             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7522     ;; We use the new limit, so we push the old limit on the stack.
7523     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7524   ;; Set the limit.
7525   (setq gnus-newsgroup-limit articles)
7526   (let ((total (length gnus-newsgroup-data))
7527         (data (gnus-data-find-list (gnus-summary-article-number)))
7528         (gnus-summary-mark-below nil)   ; Inhibit this.
7529         found)
7530     ;; This will do all the work of generating the new summary buffer
7531     ;; according to the new limit.
7532     (gnus-summary-prepare)
7533     ;; Hide any threads, possibly.
7534     (gnus-summary-maybe-hide-threads)
7535     ;; Try to return to the article you were at, or one in the
7536     ;; neighborhood.
7537     (when data
7538       ;; We try to find some article after the current one.
7539       (while data
7540         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7541           (setq data nil
7542                 found t))
7543         (setq data (cdr data))))
7544     (unless found
7545       ;; If there is no data, that means that we were after the last
7546       ;; article.  The same goes when we can't find any articles
7547       ;; after the current one.
7548       (goto-char (point-max))
7549       (gnus-summary-find-prev))
7550     (gnus-set-mode-line 'summary)
7551     ;; We return how many articles were removed from the summary
7552     ;; buffer as a result of the new limit.
7553     (- total (length gnus-newsgroup-data))))
7554
7555 (defsubst gnus-invisible-cut-children (threads)
7556   (let ((num 0))
7557     (while threads
7558       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7559         (incf num))
7560       (pop threads))
7561     (< num 2)))
7562
7563 (defsubst gnus-cut-thread (thread)
7564   "Go forwards in the thread until we find an article that we want to display."
7565   (when (or (eq gnus-fetch-old-headers 'some)
7566             (eq gnus-fetch-old-headers 'invisible)
7567             (numberp gnus-fetch-old-headers)
7568             (eq gnus-build-sparse-threads 'some)
7569             (eq gnus-build-sparse-threads 'more))
7570     ;; Deal with old-fetched headers and sparse threads.
7571     (while (and
7572             thread
7573             (or
7574              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7575              (gnus-summary-article-ancient-p
7576               (mail-header-number (car thread))))
7577             (if (or (<= (length (cdr thread)) 1)
7578                     (eq gnus-fetch-old-headers 'invisible))
7579                 (setq gnus-newsgroup-limit
7580                       (delq (mail-header-number (car thread))
7581                             gnus-newsgroup-limit)
7582                       thread (cadr thread))
7583               (when (gnus-invisible-cut-children (cdr thread))
7584                 (let ((th (cdr thread)))
7585                   (while th
7586                     (if (memq (mail-header-number (caar th))
7587                               gnus-newsgroup-limit)
7588                         (setq thread (car th)
7589                               th nil)
7590                       (setq th (cdr th))))))))))
7591   thread)
7592
7593 (defun gnus-cut-threads (threads)
7594   "Cut off all uninteresting articles from the beginning of threads."
7595   (when (or (eq gnus-fetch-old-headers 'some)
7596             (eq gnus-fetch-old-headers 'invisible)
7597             (numberp gnus-fetch-old-headers)
7598             (eq gnus-build-sparse-threads 'some)
7599             (eq gnus-build-sparse-threads 'more))
7600     (let ((th threads))
7601       (while th
7602         (setcar th (gnus-cut-thread (car th)))
7603         (setq th (cdr th)))))
7604   ;; Remove nixed out threads.
7605   (delq nil threads))
7606
7607 (defun gnus-summary-initial-limit (&optional show-if-empty)
7608   "Figure out what the initial limit is supposed to be on group entry.
7609 This entails weeding out unwanted dormants, low-scored articles,
7610 fetch-old-headers verbiage, and so on."
7611   ;; Most groups have nothing to remove.
7612   (if (or gnus-inhibit-limiting
7613           (and (null gnus-newsgroup-dormant)
7614                (eq gnus-newsgroup-display 'gnus-not-ignore)
7615                (not (eq gnus-fetch-old-headers 'some))
7616                (not (numberp gnus-fetch-old-headers))
7617                (not (eq gnus-fetch-old-headers 'invisible))
7618                (null gnus-summary-expunge-below)
7619                (not (eq gnus-build-sparse-threads 'some))
7620                (not (eq gnus-build-sparse-threads 'more))
7621                (null gnus-thread-expunge-below)
7622                (not gnus-use-nocem)))
7623       ()                                ; Do nothing.
7624     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7625     (setq gnus-newsgroup-limit nil)
7626     (mapatoms
7627      (lambda (node)
7628        (unless (car (symbol-value node))
7629          ;; These threads have no parents -- they are roots.
7630          (let ((nodes (cdr (symbol-value node)))
7631                thread)
7632            (while nodes
7633              (if (and gnus-thread-expunge-below
7634                       (< (gnus-thread-total-score (car nodes))
7635                          gnus-thread-expunge-below))
7636                  (gnus-expunge-thread (pop nodes))
7637                (setq thread (pop nodes))
7638                (gnus-summary-limit-children thread))))))
7639      gnus-newsgroup-dependencies)
7640     ;; If this limitation resulted in an empty group, we might
7641     ;; pop the previous limit and use it instead.
7642     (when (and (not gnus-newsgroup-limit)
7643                show-if-empty)
7644       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7645     gnus-newsgroup-limit))
7646
7647 (defun gnus-summary-limit-children (thread)
7648   "Return 1 if this subthread is visible and 0 if it is not."
7649   ;; First we get the number of visible children to this thread.  This
7650   ;; is done by recursing down the thread using this function, so this
7651   ;; will really go down to a leaf article first, before slowly
7652   ;; working its way up towards the root.
7653   (when thread
7654     (let ((children
7655            (if (cdr thread)
7656                (apply '+ (mapcar 'gnus-summary-limit-children
7657                                  (cdr thread)))
7658              0))
7659           (number (mail-header-number (car thread)))
7660           score)
7661       (if (and
7662            (not (memq number gnus-newsgroup-marked))
7663            (or
7664             ;; If this article is dormant and has absolutely no visible
7665             ;; children, then this article isn't visible.
7666             (and (memq number gnus-newsgroup-dormant)
7667                  (zerop children))
7668             ;; If this is "fetch-old-headered" and there is no
7669             ;; visible children, then we don't want this article.
7670             (and (or (eq gnus-fetch-old-headers 'some)
7671                      (numberp gnus-fetch-old-headers))
7672                  (gnus-summary-article-ancient-p number)
7673                  (zerop children))
7674             ;; If this is "fetch-old-headered" and `invisible', then
7675             ;; we don't want this article.
7676             (and (eq gnus-fetch-old-headers 'invisible)
7677                  (gnus-summary-article-ancient-p number))
7678             ;; If this is a sparsely inserted article with no children,
7679             ;; we don't want it.
7680             (and (eq gnus-build-sparse-threads 'some)
7681                  (gnus-summary-article-sparse-p number)
7682                  (zerop children))
7683             ;; If we use expunging, and this article is really
7684             ;; low-scored, then we don't want this article.
7685             (when (and gnus-summary-expunge-below
7686                        (< (setq score
7687                                 (or (cdr (assq number gnus-newsgroup-scored))
7688                                     gnus-summary-default-score))
7689                           gnus-summary-expunge-below))
7690               ;; We increase the expunge-tally here, but that has
7691               ;; nothing to do with the limits, really.
7692               (incf gnus-newsgroup-expunged-tally)
7693               ;; We also mark as read here, if that's wanted.
7694               (when (and gnus-summary-mark-below
7695                          (< score gnus-summary-mark-below))
7696                 (setq gnus-newsgroup-unreads
7697                       (delq number gnus-newsgroup-unreads))
7698                 (if gnus-newsgroup-auto-expire
7699                     (push number gnus-newsgroup-expirable)
7700                   (push (cons number gnus-low-score-mark)
7701                         gnus-newsgroup-reads)))
7702               t)
7703             ;; Do the `display' group parameter.
7704             (and gnus-newsgroup-display
7705                  (not (funcall gnus-newsgroup-display)))
7706             ;; Check NoCeM things.
7707             (if (and gnus-use-nocem
7708                      (gnus-nocem-unwanted-article-p
7709                       (mail-header-id (car thread))))
7710                 (progn
7711                   (setq gnus-newsgroup-unreads
7712                         (delq number gnus-newsgroup-unreads))
7713                   t))))
7714           ;; Nope, invisible article.
7715           0
7716         ;; Ok, this article is to be visible, so we add it to the limit
7717         ;; and return 1.
7718         (push number gnus-newsgroup-limit)
7719         1))))
7720
7721 (defun gnus-expunge-thread (thread)
7722   "Mark all articles in THREAD as read."
7723   (let* ((number (mail-header-number (car thread))))
7724     (incf gnus-newsgroup-expunged-tally)
7725     ;; We also mark as read here, if that's wanted.
7726     (setq gnus-newsgroup-unreads
7727           (delq number gnus-newsgroup-unreads))
7728     (if gnus-newsgroup-auto-expire
7729         (push number gnus-newsgroup-expirable)
7730       (push (cons number gnus-low-score-mark)
7731             gnus-newsgroup-reads)))
7732   ;; Go recursively through all subthreads.
7733   (mapcar 'gnus-expunge-thread (cdr thread)))
7734
7735 ;; Summary article oriented commands
7736
7737 (defun gnus-summary-refer-parent-article (n)
7738   "Refer parent article N times.
7739 If N is negative, go to ancestor -N instead.
7740 The difference between N and the number of articles fetched is returned."
7741   (interactive "p")
7742   (let ((skip 1)
7743         error header ref)
7744     (when (not (natnump n))
7745       (setq skip (abs n)
7746             n 1))
7747     (while (and (> n 0)
7748                 (not error))
7749       (setq header (gnus-summary-article-header))
7750       (if (and (eq (mail-header-number header)
7751                    (cdr gnus-article-current))
7752                (equal gnus-newsgroup-name
7753                       (car gnus-article-current)))
7754           ;; If we try to find the parent of the currently
7755           ;; displayed article, then we take a look at the actual
7756           ;; References header, since this is slightly more
7757           ;; reliable than the References field we got from the
7758           ;; server.
7759           (save-excursion
7760             (set-buffer gnus-original-article-buffer)
7761             (nnheader-narrow-to-headers)
7762             (unless (setq ref (message-fetch-field "references"))
7763               (setq ref (message-fetch-field "in-reply-to")))
7764             (widen))
7765         (setq ref
7766               ;; It's not the current article, so we take a bet on
7767               ;; the value we got from the server.
7768               (mail-header-references header)))
7769       (if (and ref
7770                (not (equal ref "")))
7771           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7772             (gnus-message 1 "Couldn't find parent"))
7773         (gnus-message 1 "No references in article %d"
7774                       (gnus-summary-article-number))
7775         (setq error t))
7776       (decf n))
7777     (gnus-summary-position-point)
7778     n))
7779
7780 (defun gnus-summary-refer-references ()
7781   "Fetch all articles mentioned in the References header.
7782 Return the number of articles fetched."
7783   (interactive)
7784   (let ((ref (mail-header-references (gnus-summary-article-header)))
7785         (current (gnus-summary-article-number))
7786         (n 0))
7787     (if (or (not ref)
7788             (equal ref ""))
7789         (error "No References in the current article")
7790       ;; For each Message-ID in the References header...
7791       (while (string-match "<[^>]*>" ref)
7792         (incf n)
7793         ;; ... fetch that article.
7794         (gnus-summary-refer-article
7795          (prog1 (match-string 0 ref)
7796            (setq ref (substring ref (match-end 0))))))
7797       (gnus-summary-goto-subject current)
7798       (gnus-summary-position-point)
7799       n)))
7800
7801 (defun gnus-summary-refer-thread (&optional limit)
7802   "Fetch all articles in the current thread.
7803 If LIMIT (the numerical prefix), fetch that many old headers instead
7804 of what's specified by the `gnus-refer-thread-limit' variable."
7805   (interactive "P")
7806   (let ((id (mail-header-id (gnus-summary-article-header)))
7807         (limit (if limit (prefix-numeric-value limit)
7808                  gnus-refer-thread-limit)))
7809     ;; We want to fetch LIMIT *old* headers, but we also have to
7810     ;; re-fetch all the headers in the current buffer, because many of
7811     ;; them may be undisplayed.  So we adjust LIMIT.
7812     (when (numberp limit)
7813       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7814     (unless (eq gnus-fetch-old-headers 'invisible)
7815       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7816       ;; Retrieve the headers and read them in.
7817       (if (eq (gnus-retrieve-headers
7818                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7819               'nov)
7820           (gnus-build-all-threads)
7821         (error "Can't fetch thread from backends that don't support NOV"))
7822       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7823     (gnus-summary-limit-include-thread id)))
7824
7825 (defun gnus-summary-refer-article (message-id)
7826   "Fetch an article specified by MESSAGE-ID."
7827   (interactive "sMessage-ID: ")
7828   (when (and (stringp message-id)
7829              (not (zerop (length message-id))))
7830     ;; Construct the correct Message-ID if necessary.
7831     ;; Suggested by tale@pawl.rpi.edu.
7832     (unless (string-match "^<" message-id)
7833       (setq message-id (concat "<" message-id)))
7834     (unless (string-match ">$" message-id)
7835       (setq message-id (concat message-id ">")))
7836     (let* ((header (gnus-id-to-header message-id))
7837            (sparse (and header
7838                         (gnus-summary-article-sparse-p
7839                          (mail-header-number header))
7840                         (memq (mail-header-number header)
7841                               gnus-newsgroup-limit)))
7842            number)
7843       (cond
7844        ;; If the article is present in the buffer we just go to it.
7845        ((and header
7846              (or (not (gnus-summary-article-sparse-p
7847                        (mail-header-number header)))
7848                  sparse))
7849         (prog1
7850             (gnus-summary-goto-article
7851              (mail-header-number header) nil t)
7852           (when sparse
7853             (gnus-summary-update-article (mail-header-number header)))))
7854        (t
7855         ;; We fetch the article.
7856         (catch 'found
7857           (dolist (gnus-override-method (gnus-refer-article-methods))
7858             (gnus-check-server gnus-override-method)
7859             ;; Fetch the header, and display the article.
7860             (when (setq number (gnus-summary-insert-subject message-id))
7861               (gnus-summary-select-article nil nil nil number)
7862               (throw 'found t)))
7863           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7864
7865 (defun gnus-refer-article-methods ()
7866   "Return a list of referrable methods."
7867   (cond
7868    ;; No method, so we default to current and native.
7869    ((null gnus-refer-article-method)
7870     (list gnus-current-select-method gnus-select-method))
7871    ;; Current.
7872    ((eq 'current gnus-refer-article-method)
7873     (list gnus-current-select-method))
7874    ;; List of select methods.
7875    ((not (and (symbolp (car gnus-refer-article-method))
7876               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7877     (let (out)
7878       (dolist (method gnus-refer-article-method)
7879         (push (if (eq 'current method)
7880                   gnus-current-select-method
7881                 method)
7882               out))
7883       (nreverse out)))
7884    ;; One single select method.
7885    (t
7886     (list gnus-refer-article-method))))
7887
7888 (defun gnus-summary-edit-parameters ()
7889   "Edit the group parameters of the current group."
7890   (interactive)
7891   (gnus-group-edit-group gnus-newsgroup-name 'params))
7892
7893 (defun gnus-summary-customize-parameters ()
7894   "Customize the group parameters of the current group."
7895   (interactive)
7896   (gnus-group-customize gnus-newsgroup-name))
7897
7898 (defun gnus-summary-enter-digest-group (&optional force)
7899   "Enter an nndoc group based on the current article.
7900 If FORCE, force a digest interpretation.  If not, try
7901 to guess what the document format is."
7902   (interactive "P")
7903   (let ((conf gnus-current-window-configuration))
7904     (save-excursion
7905       (gnus-summary-select-article))
7906     (setq gnus-current-window-configuration conf)
7907     (let* ((name (format "%s-%d"
7908                          (gnus-group-prefixed-name
7909                           gnus-newsgroup-name (list 'nndoc ""))
7910                          (save-excursion
7911                            (set-buffer gnus-summary-buffer)
7912                            gnus-current-article)))
7913            (ogroup gnus-newsgroup-name)
7914            (params (append (gnus-info-params (gnus-get-info ogroup))
7915                            (list (cons 'to-group ogroup))
7916                            (list (cons 'save-article-group ogroup))))
7917            (case-fold-search t)
7918            (buf (current-buffer))
7919            dig to-address)
7920       (save-excursion
7921         (set-buffer gnus-original-article-buffer)
7922         ;; Have the digest group inherit the main mail address of
7923         ;; the parent article.
7924         (when (setq to-address (or (gnus-fetch-field "reply-to")
7925                                    (gnus-fetch-field "from")))
7926           (setq params (append
7927                         (list (cons 'to-address
7928                                     (funcall gnus-decode-encoded-word-function
7929                                              to-address))))))
7930         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7931         (insert-buffer-substring gnus-original-article-buffer)
7932         ;; Remove lines that may lead nndoc to misinterpret the
7933         ;; document type.
7934         (narrow-to-region
7935          (goto-char (point-min))
7936          (or (search-forward "\n\n" nil t) (point)))
7937         (goto-char (point-min))
7938         (delete-matching-lines "^Path:\\|^From ")
7939         (widen))
7940       (unwind-protect
7941           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7942                     (gnus-newsgroup-ephemeral-ignored-charsets
7943                      gnus-newsgroup-ignored-charsets))
7944                 (gnus-group-read-ephemeral-group
7945                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7946                               (nndoc-article-type
7947                                ,(if force 'mbox 'guess)))
7948                  t nil nil nil
7949                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7950                                                         "ADAPT")))))
7951               ;; Make all postings to this group go to the parent group.
7952               (nconc (gnus-info-params (gnus-get-info name))
7953                      params)
7954             ;; Couldn't select this doc group.
7955             (switch-to-buffer buf)
7956             (gnus-set-global-variables)
7957             (gnus-configure-windows 'summary)
7958             (gnus-message 3 "Article couldn't be entered?"))
7959         (kill-buffer dig)))))
7960
7961 (defun gnus-summary-read-document (n)
7962   "Open a new group based on the current article(s).
7963 This will allow you to read digests and other similar
7964 documents as newsgroups.
7965 Obeys the standard process/prefix convention."
7966   (interactive "P")
7967   (let* ((articles (gnus-summary-work-articles n))
7968          (ogroup gnus-newsgroup-name)
7969          (params (append (gnus-info-params (gnus-get-info ogroup))
7970                          (list (cons 'to-group ogroup))))
7971          article group egroup groups vgroup)
7972     (while (setq article (pop articles))
7973       (setq group (format "%s-%d" gnus-newsgroup-name article))
7974       (gnus-summary-remove-process-mark article)
7975       (when (gnus-summary-display-article article)
7976         (save-excursion
7977           (with-temp-buffer
7978             (insert-buffer-substring gnus-original-article-buffer)
7979             ;; Remove some headers that may lead nndoc to make
7980             ;; the wrong guess.
7981             (message-narrow-to-head)
7982             (goto-char (point-min))
7983             (delete-matching-lines "^\\(Path\\):\\|^From ")
7984             (widen)
7985             (if (setq egroup
7986                       (gnus-group-read-ephemeral-group
7987                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7988                                      (nndoc-article-type guess))
7989                        t nil t))
7990                 (progn
7991                   ;; Make all postings to this group go to the parent group.
7992                   (nconc (gnus-info-params (gnus-get-info egroup))
7993                          params)
7994                   (push egroup groups))
7995               ;; Couldn't select this doc group.
7996               (gnus-error 3 "Article couldn't be entered"))))))
7997     ;; Now we have selected all the documents.
7998     (cond
7999      ((not groups)
8000       (error "None of the articles could be interpreted as documents"))
8001      ((gnus-group-read-ephemeral-group
8002        (setq vgroup (format
8003                      "nnvirtual:%s-%s" gnus-newsgroup-name
8004                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8005        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8006        t
8007        (cons (current-buffer) 'summary)))
8008      (t
8009       (error "Couldn't select virtual nndoc group")))))
8010
8011 (defun gnus-summary-isearch-article (&optional regexp-p)
8012   "Do incremental search forward on the current article.
8013 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8014   (interactive "P")
8015   (let* ((gnus-inhibit-treatment t)
8016          (old (gnus-summary-select-article)))
8017     (gnus-configure-windows 'article)
8018     (gnus-eval-in-buffer-window gnus-article-buffer
8019       (save-restriction
8020         (widen)
8021         (when (eq 'old old)
8022           (gnus-article-show-all-headers))
8023         (goto-char (point-min))
8024         (isearch-forward regexp-p)))))
8025
8026 (defun gnus-summary-search-article-forward (regexp &optional backward)
8027   "Search for an article containing REGEXP forward.
8028 If BACKWARD, search backward instead."
8029   (interactive
8030    (list (read-string
8031           (format "Search article %s (regexp%s): "
8032                   (if current-prefix-arg "backward" "forward")
8033                   (if gnus-last-search-regexp
8034                       (concat ", default " gnus-last-search-regexp)
8035                     "")))
8036          current-prefix-arg))
8037   (if (string-equal regexp "")
8038       (setq regexp (or gnus-last-search-regexp ""))
8039     (setq gnus-last-search-regexp regexp)
8040     (setq gnus-article-before-search gnus-current-article))
8041   ;; Intentionally set gnus-last-article.
8042   (setq gnus-last-article gnus-article-before-search)
8043   (let ((gnus-last-article gnus-last-article))
8044     (if (gnus-summary-search-article regexp backward)
8045         (gnus-summary-show-thread)
8046       (error "Search failed: \"%s\"" regexp))))
8047
8048 (defun gnus-summary-search-article-backward (regexp)
8049   "Search for an article containing REGEXP backward."
8050   (interactive
8051    (list (read-string
8052           (format "Search article backward (regexp%s): "
8053                   (if gnus-last-search-regexp
8054                       (concat ", default " gnus-last-search-regexp)
8055                     "")))))
8056   (gnus-summary-search-article-forward regexp 'backward))
8057
8058 (eval-when-compile
8059   (defmacro gnus-summary-search-article-position-point (regexp backward)
8060     "Dehighlight the last matched text and goto the beginning position."
8061     (` (if (and gnus-summary-search-article-matched-data
8062                 (let ((text (caddr gnus-summary-search-article-matched-data))
8063                       (inhibit-read-only t)
8064                       buffer-read-only)
8065                   (delete-region
8066                    (goto-char (car gnus-summary-search-article-matched-data))
8067                    (cadr gnus-summary-search-article-matched-data))
8068                   (insert text)
8069                   (string-match (, regexp) text)))
8070            (if (, backward) (beginning-of-line) (end-of-line))
8071          (goto-char (if (, backward) (point-max) (point-min))))))
8072
8073   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8074     "Place point where X-Face image is displayed."
8075     (if (featurep 'xemacs)
8076         (` (let ((end (if (search-forward "\n\n" nil t)
8077                           (goto-char (1- (point)))
8078                         (point-min)))
8079                  extent)
8080              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8081              (unless (and (re-search-forward "^From:" end t)
8082                           (setq extent (extent-at (point)))
8083                           (extent-begin-glyph extent))
8084                (goto-char (, opoint)))))
8085       (` (let ((end (if (search-forward "\n\n" nil t)
8086                         (goto-char (1- (point)))
8087                       (point-min)))
8088                (start (or (search-backward "\n\n" nil t) (point-min))))
8089            (goto-char
8090             (or (text-property-any start end 'x-face-image t);; x-face-e21
8091                 (text-property-any start end 'x-face-mule-bitmap-image t)
8092                 (, opoint)))))))
8093
8094   (defmacro gnus-summary-search-article-highlight-matched-text
8095     (backward treated x-face)
8096     "Highlight matched text in the function `gnus-summary-search-article'."
8097     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8098              (end (set-marker (make-marker) (match-end 0)))
8099              (inhibit-read-only t)
8100              buffer-read-only)
8101          (unless treated
8102            (let ((,@
8103                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8104                     (mapcar
8105                      (lambda (item) (setq items (delq item items)))
8106                      '(gnus-treat-buttonize
8107                        gnus-treat-fill-article
8108                        gnus-treat-fill-long-lines
8109                        gnus-treat-emphasize
8110                        gnus-treat-highlight-headers
8111                        gnus-treat-highlight-citation
8112                        gnus-treat-highlight-signature
8113                        gnus-treat-overstrike
8114                        gnus-treat-display-xface
8115                        gnus-treat-buttonize-head
8116                        gnus-treat-decode-article-as-default-mime-charset))
8117                     (static-if (featurep 'xemacs)
8118                         items
8119                       (cons '(x-face-mule-delete-x-face-field
8120                               (quote never))
8121                             items))))
8122                  (gnus-treat-display-xface
8123                   (when (, x-face) gnus-treat-display-xface)))
8124              (gnus-article-prepare-mime-display)))
8125          (goto-char (if (, backward) start end))
8126          (when (, x-face)
8127            (gnus-summary-search-article-highlight-goto-x-face (point)))
8128          (setq gnus-summary-search-article-matched-data
8129                (list start end (buffer-substring start end)))
8130          (unless (eq start end);; matched text has been deleted. :-<
8131            (put-text-property start end 'face
8132                               (or (find-face 'isearch)
8133                                   'secondary-selection))))))
8134   )
8135
8136 (defun gnus-summary-search-article (regexp &optional backward)
8137   "Search for an article containing REGEXP.
8138 Optional argument BACKWARD means do search for backward.
8139 `gnus-select-article-hook' is not called during the search."
8140   ;; We have to require this here to make sure that the following
8141   ;; dynamic binding isn't shadowed by autoloading.
8142   (require 'gnus-async)
8143   (require 'gnus-art)
8144   (let ((gnus-select-article-hook nil)  ;Disable hook.
8145         (gnus-article-prepare-hook nil)
8146         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8147         (gnus-use-article-prefetch nil)
8148         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8149         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8150         (sum (current-buffer))
8151         (found nil)
8152         point treated)
8153     (gnus-save-hidden-threads
8154       (static-if (featurep 'xemacs)
8155           (let ((gnus-inhibit-treatment t))
8156             (setq treated (eq 'old (gnus-summary-select-article)))
8157             (when (and treated
8158                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8159                                  (window-live-p (get-buffer-window
8160                                                  gnus-article-buffer t)))))
8161               (gnus-summary-select-article nil t)
8162               (setq treated nil)))
8163         (let ((gnus-inhibit-treatment t)
8164               (x-face-mule-delete-x-face-field 'never))
8165           (setq treated (eq 'old (gnus-summary-select-article)))
8166           (when (and treated
8167                      (not
8168                       (and (gnus-buffer-live-p gnus-article-buffer)
8169                            (window-live-p (get-buffer-window
8170                                            gnus-article-buffer t))
8171                            (or (not (string-match "^\\^X-Face:" regexp))
8172                                (with-current-buffer gnus-article-buffer
8173                                  gnus-summary-search-article-matched-data)))))
8174             (gnus-summary-select-article nil t)
8175             (setq treated nil))))
8176       (set-buffer gnus-article-buffer)
8177       (widen)
8178       (if treated
8179           (progn
8180             (gnus-article-show-all-headers)
8181             (gnus-summary-search-article-position-point regexp backward))
8182         (goto-char (if backward (point-max) (point-min))))
8183       (while (not found)
8184         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8185         (if (if backward
8186                 (re-search-backward regexp nil t)
8187               (re-search-forward regexp nil t))
8188             ;; We found the regexp.
8189             (progn
8190               (gnus-summary-search-article-highlight-matched-text
8191                backward treated (string-match "^\\^X-Face:" regexp))
8192               (setq found 'found)
8193               (forward-line
8194                (/ (- 2 (window-height
8195                         (get-buffer-window gnus-article-buffer t)))
8196                   2))
8197               (set-window-start
8198                (get-buffer-window (current-buffer))
8199                (point))
8200               (set-buffer sum)
8201               (setq point (point)))
8202           ;; We didn't find it, so we go to the next article.
8203           (set-buffer sum)
8204           (setq found 'not)
8205           (while (eq found 'not)
8206             (if (not (if backward (gnus-summary-find-prev)
8207                        (gnus-summary-find-next)))
8208                 ;; No more articles.
8209                 (setq found t)
8210               ;; Select the next article and adjust point.
8211               (unless (gnus-summary-article-sparse-p
8212                        (gnus-summary-article-number))
8213                 (setq found nil)
8214                 (let ((gnus-inhibit-treatment t))
8215                   (gnus-summary-select-article))
8216                 (setq treated nil)
8217                 (set-buffer gnus-article-buffer)
8218                 (widen)
8219                 (goto-char (if backward (point-max) (point-min))))))))
8220       (gnus-message 7 ""))
8221     ;; Return whether we found the regexp.
8222     (when (eq found 'found)
8223       (goto-char point)
8224       (gnus-summary-show-thread)
8225       (gnus-summary-goto-subject gnus-current-article)
8226       (gnus-summary-position-point)
8227       t)))
8228
8229 (defun gnus-find-matching-articles (header regexp)
8230   "Return a list of all articles that match REGEXP on HEADER.
8231 This search includes all articles in the current group that Gnus has
8232 fetched headers for, whether they are displayed or not."
8233   (let ((articles nil)
8234         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8235         (case-fold-search t))
8236     (dolist (header gnus-newsgroup-headers)
8237       (when (string-match regexp (funcall func header))
8238         (push (mail-header-number header) articles)))
8239     (nreverse articles)))
8240
8241 (defun gnus-summary-find-matching (header regexp &optional backward unread
8242                                           not-case-fold not-matching)
8243   "Return a list of all articles that match REGEXP on HEADER.
8244 The search stars on the current article and goes forwards unless
8245 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8246 If UNREAD is non-nil, only unread articles will
8247 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8248 in the comparisons. If NOT-MATCHING, return a list of all articles that
8249 not match REGEXP on HEADER."
8250   (let ((case-fold-search (not not-case-fold))
8251         articles d func)
8252     (if (consp header)
8253         (if (eq (car header) 'extra)
8254             (setq func
8255                   `(lambda (h)
8256                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8257                          "")))
8258           (error "%s is an invalid header" header))
8259       (unless (fboundp (intern (concat "mail-header-" header)))
8260         (error "%s is not a valid header" header))
8261       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8262     (dolist (d (if (eq backward 'all)
8263                    gnus-newsgroup-data
8264                  (gnus-data-find-list
8265                   (gnus-summary-article-number)
8266                   (gnus-data-list backward))))
8267       (when (and (or (not unread)       ; We want all articles...
8268                      (gnus-data-unread-p d)) ; Or just unreads.
8269                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8270                  (if not-matching
8271                      (not (string-match
8272                            regexp
8273                            (funcall func (gnus-data-header d))))
8274                    (string-match regexp
8275                                  (funcall func (gnus-data-header d)))))
8276         (push (gnus-data-number d) articles))) ; Success!
8277     (nreverse articles)))
8278
8279 (defun gnus-summary-execute-command (header regexp command &optional backward)
8280   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8281 If HEADER is an empty string (or nil), the match is done on the entire
8282 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8283   (interactive
8284    (list (let ((completion-ignore-case t))
8285            (completing-read
8286             "Header name: "
8287             (mapcar (lambda (header) (list (format "%s" header)))
8288                     (append
8289                      '("Number" "Subject" "From" "Lines" "Date"
8290                        "Message-ID" "Xref" "References" "Body")
8291                      gnus-extra-headers))
8292             nil 'require-match))
8293          (read-string "Regexp: ")
8294          (read-key-sequence "Command: ")
8295          current-prefix-arg))
8296   (when (equal header "Body")
8297     (setq header ""))
8298   ;; Hidden thread subtrees must be searched as well.
8299   (gnus-summary-show-all-threads)
8300   ;; We don't want to change current point nor window configuration.
8301   (save-excursion
8302     (save-window-excursion
8303       (gnus-message 6 "Executing %s..." (key-description command))
8304       ;; We'd like to execute COMMAND interactively so as to give arguments.
8305       (gnus-execute header regexp
8306                     `(call-interactively ',(key-binding command))
8307                     backward)
8308       (gnus-message 6 "Executing %s...done" (key-description command)))))
8309
8310 (defun gnus-summary-beginning-of-article ()
8311   "Scroll the article back to the beginning."
8312   (interactive)
8313   (gnus-summary-select-article)
8314   (gnus-configure-windows 'article)
8315   (gnus-eval-in-buffer-window gnus-article-buffer
8316     (widen)
8317     (goto-char (point-min))
8318     (when gnus-page-broken
8319       (gnus-narrow-to-page))))
8320
8321 (defun gnus-summary-end-of-article ()
8322   "Scroll to the end of the article."
8323   (interactive)
8324   (gnus-summary-select-article)
8325   (gnus-configure-windows 'article)
8326   (gnus-eval-in-buffer-window gnus-article-buffer
8327     (widen)
8328     (goto-char (point-max))
8329     (recenter -3)
8330     (when gnus-page-broken
8331       (gnus-narrow-to-page))))
8332
8333 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8334   "Truncate to LEN and quote all \"(\"'s in STRING."
8335   (gnus-replace-in-string (if (and len (> (length string) len))
8336                               (substring string 0 len)
8337                             string)
8338                           "[()]" "\\\\\\&"))
8339
8340 (defun gnus-summary-print-article (&optional filename n)
8341   "Generate and print a PostScript image of the N next (mail) articles.
8342
8343 If N is negative, print the N previous articles.  If N is nil and articles
8344 have been marked with the process mark, print these instead.
8345
8346 If the optional first argument FILENAME is nil, send the image to the
8347 printer.  If FILENAME is a string, save the PostScript image in a file with
8348 that name.  If FILENAME is a number, prompt the user for the name of the file
8349 to save in."
8350   (interactive (list (ps-print-preprint current-prefix-arg)))
8351   (dolist (article (gnus-summary-work-articles n))
8352     (gnus-summary-select-article nil nil 'pseudo article)
8353     (gnus-eval-in-buffer-window gnus-article-buffer
8354       (gnus-print-buffer))
8355     (gnus-summary-remove-process-mark article))
8356   (ps-despool filename))
8357
8358 (defun gnus-print-buffer ()
8359   (let ((buffer (generate-new-buffer " *print*")))
8360     (unwind-protect
8361         (progn
8362           (copy-to-buffer buffer (point-min) (point-max))
8363           (set-buffer buffer)
8364           (gnus-article-delete-invisible-text)
8365           (gnus-remove-text-with-property 'gnus-decoration)
8366           (when (gnus-visual-p 'article-highlight 'highlight)
8367             ;; Copy-to-buffer doesn't copy overlay.  So redo
8368             ;; highlight.
8369             (let ((gnus-article-buffer buffer))
8370               (gnus-article-highlight-citation t)
8371               (gnus-article-highlight-signature)))
8372           (let ((ps-left-header
8373                  (list
8374                   (concat "("
8375                           (gnus-summary-print-truncate-and-quote
8376                            (mail-header-subject gnus-current-headers)
8377                            66) ")")
8378                   (concat "("
8379                           (gnus-summary-print-truncate-and-quote
8380                            (mail-header-from gnus-current-headers)
8381                            45) ")")))
8382                 (ps-right-header
8383                  (list
8384                   "/pagenumberstring load"
8385                   (concat "("
8386                           (mail-header-date gnus-current-headers) ")"))))
8387             (gnus-run-hooks 'gnus-ps-print-hook)
8388             (save-excursion
8389               (if window-system
8390                   (ps-spool-buffer-with-faces)
8391                 (ps-spool-buffer)))))
8392       (kill-buffer buffer))))
8393
8394 (defun gnus-summary-show-article (&optional arg)
8395   "Force redisplaying of the current article.
8396 If ARG (the prefix) is a number, show the article with the charset
8397 defined in `gnus-summary-show-article-charset-alist', or the charset
8398 input.
8399 If ARG (the prefix) is non-nil and not a number, show the raw article
8400 without any article massaging functions being run.  Normally, the key strokes
8401 are `C-u g'."
8402   (interactive "P")
8403   (cond
8404    ((numberp arg)
8405     (gnus-summary-show-article t)
8406     (let* ((gnus-newsgroup-charset
8407             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8408                 (mm-read-coding-system
8409                  "View as charset: " ;; actually it is coding system.
8410                  (save-excursion
8411                    (set-buffer gnus-article-buffer)
8412                    (mm-detect-coding-region (point) (point-max))))))
8413            (default-mime-charset gnus-newsgroup-charset)
8414            (gnus-newsgroup-ignored-charsets 'gnus-all))
8415       (gnus-summary-select-article nil 'force)
8416       (let ((deps gnus-newsgroup-dependencies)
8417             head header lines)
8418         (save-excursion
8419           (set-buffer gnus-original-article-buffer)
8420           (save-restriction
8421             (message-narrow-to-head)
8422             (setq head (buffer-string))
8423             (goto-char (point-min))
8424             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8425               (goto-char (point-max))
8426               (widen)
8427               (setq lines (1- (count-lines (point) (point-max))))))
8428           (with-temp-buffer
8429             (insert (format "211 %d Article retrieved.\n"
8430                             (cdr gnus-article-current)))
8431             (insert head)
8432             (if lines (insert (format "Lines: %d\n" lines)))
8433             (insert ".\n")
8434             (let ((nntp-server-buffer (current-buffer)))
8435               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8436         (gnus-data-set-header
8437          (gnus-data-find (cdr gnus-article-current))
8438          header)
8439         (gnus-summary-update-article-line
8440          (cdr gnus-article-current) header)
8441         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8442           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8443    ((not arg)
8444     ;; Select the article the normal way.
8445     (gnus-summary-select-article nil 'force))
8446    (t
8447     ;; We have to require this here to make sure that the following
8448     ;; dynamic binding isn't shadowed by autoloading.
8449     (require 'gnus-async)
8450     (require 'gnus-art)
8451     ;; Bind the article treatment functions to nil.
8452     (let ((gnus-have-all-headers t)
8453           gnus-article-prepare-hook
8454           gnus-article-decode-hook
8455           gnus-break-pages
8456           gnus-show-mime
8457           (gnus-inhibit-treatment t))
8458       (gnus-summary-select-article nil 'force))))
8459   (gnus-summary-goto-subject gnus-current-article)
8460   (gnus-summary-position-point))
8461
8462 (defun gnus-summary-show-raw-article ()
8463   "Show the raw article without any article massaging functions being run."
8464   (interactive)
8465   (gnus-summary-show-article t))
8466
8467 (defun gnus-summary-verbose-headers (&optional arg)
8468   "Toggle permanent full header display.
8469 If ARG is a positive number, turn header display on.
8470 If ARG is a negative number, turn header display off."
8471   (interactive "P")
8472   (setq gnus-show-all-headers
8473         (cond ((or (not (numberp arg))
8474                    (zerop arg))
8475                (not gnus-show-all-headers))
8476               ((natnump arg)
8477                t)))
8478   (gnus-summary-show-article))
8479
8480 (defun gnus-summary-toggle-header (&optional arg)
8481   "Show the headers if they are hidden, or hide them if they are shown.
8482 If ARG is a positive number, show the entire header.
8483 If ARG is a negative number, hide the unwanted header lines."
8484   (interactive "P")
8485   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8486                      (get-buffer-window gnus-article-buffer t))))
8487     (with-current-buffer gnus-article-buffer
8488       (widen)
8489       (article-narrow-to-head)
8490       (let* ((buffer-read-only nil)
8491              (inhibit-point-motion-hooks t)
8492              (hidden (if (numberp arg)
8493                          (>= arg 0)
8494                        (gnus-article-hidden-text-p 'headers)))
8495              s e)
8496         (delete-region (point-min) (point-max))
8497         (with-current-buffer gnus-original-article-buffer
8498           (goto-char (setq s (point-min)))
8499           (setq e (if (search-forward "\n\n" nil t)
8500                       (1- (point))
8501                     (point-max))))
8502         (insert-buffer-substring gnus-original-article-buffer s e)
8503         (article-decode-encoded-words)
8504         (if hidden
8505             (let ((gnus-treat-hide-headers nil)
8506                   (gnus-treat-hide-boring-headers nil))
8507               (gnus-delete-wash-type 'headers)
8508               (gnus-treat-article 'head))
8509           (gnus-treat-article 'head))
8510         (widen)
8511         (if window
8512             (set-window-start window (goto-char (point-min))))
8513         (setq gnus-page-broken
8514               (when gnus-break-pages
8515                 (gnus-narrow-to-page)
8516                 t))
8517         (gnus-set-mode-line 'article)))))
8518
8519 (defun gnus-summary-show-all-headers ()
8520   "Make all header lines visible."
8521   (interactive)
8522   (gnus-summary-toggle-header 1))
8523
8524 (defun gnus-summary-toggle-mime (&optional arg)
8525   "Toggle MIME processing.
8526 If ARG is a positive number, turn MIME processing on."
8527   (interactive "P")
8528   (setq gnus-show-mime
8529         (if (null arg)
8530             (not gnus-show-mime)
8531           (> (prefix-numeric-value arg) 0)))
8532   (gnus-summary-select-article t 'force))
8533
8534 (defun gnus-summary-caesar-message (&optional arg)
8535   "Caesar rotate the current article by 13.
8536 The numerical prefix specifies how many places to rotate each letter
8537 forward."
8538   (interactive "P")
8539   (gnus-summary-select-article)
8540   (let ((mail-header-separator ""))
8541     (gnus-eval-in-buffer-window gnus-article-buffer
8542       (save-restriction
8543         (widen)
8544         (let ((start (window-start))
8545               buffer-read-only)
8546           (message-caesar-buffer-body arg)
8547           (set-window-start (get-buffer-window (current-buffer)) start))))))
8548
8549 (defun gnus-summary-stop-page-breaking ()
8550   "Stop page breaking in the current article."
8551   (interactive)
8552   (gnus-summary-select-article)
8553   (gnus-eval-in-buffer-window gnus-article-buffer
8554     (widen)
8555     (when (gnus-visual-p 'page-marker)
8556       (let ((buffer-read-only nil))
8557         (gnus-remove-text-with-property 'gnus-prev)
8558         (gnus-remove-text-with-property 'gnus-next))
8559       (setq gnus-page-broken nil))))
8560
8561 (defun gnus-summary-move-article (&optional n to-newsgroup
8562                                             select-method action)
8563   "Move the current article to a different newsgroup.
8564 If N is a positive number, move the N next articles.
8565 If N is a negative number, move the N previous articles.
8566 If N is nil and any articles have been marked with the process mark,
8567 move those articles instead.
8568 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8569 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8570 re-spool using this method.
8571
8572 When called interactively with TO-NEWSGROUP being nil, the value of
8573 the variable `gnus-move-split-methods' is used for finding a default
8574 for the target newsgroup.
8575
8576 For this function to work, both the current newsgroup and the
8577 newsgroup that you want to move to have to support the `request-move'
8578 and `request-accept' functions.
8579
8580 ACTION can be either `move' (the default), `crosspost' or `copy'."
8581   (interactive "P")
8582   (unless action
8583     (setq action 'move))
8584   ;; Check whether the source group supports the required functions.
8585   (cond ((and (eq action 'move)
8586               (not (gnus-check-backend-function
8587                     'request-move-article gnus-newsgroup-name)))
8588          (error "The current group does not support article moving"))
8589         ((and (eq action 'crosspost)
8590               (not (gnus-check-backend-function
8591                     'request-replace-article gnus-newsgroup-name)))
8592          (error "The current group does not support article editing")))
8593   (let ((articles (gnus-summary-work-articles n))
8594         (prefix (if (gnus-check-backend-function
8595                      'request-move-article gnus-newsgroup-name)
8596                     (gnus-group-real-prefix gnus-newsgroup-name)
8597                   ""))
8598         (names '((move "Move" "Moving")
8599                  (copy "Copy" "Copying")
8600                  (crosspost "Crosspost" "Crossposting")))
8601         (copy-buf (save-excursion
8602                     (nnheader-set-temp-buffer " *copy article*")))
8603         (default-marks gnus-article-mark-lists)
8604         (no-expire-marks (delete '(expirable . expire)
8605                                  (copy-sequence gnus-article-mark-lists)))
8606         art-group to-method new-xref article to-groups)
8607     (unless (assq action names)
8608       (error "Unknown action %s" action))
8609     ;; Read the newsgroup name.
8610     (when (and (not to-newsgroup)
8611                (not select-method))
8612       (if (and gnus-move-split-methods
8613                (not
8614                 (and (memq gnus-current-article articles)
8615                      (gnus-buffer-live-p gnus-original-article-buffer))))
8616           ;; When `gnus-move-split-methods' is non-nil, we have to
8617           ;; select an article to give `gnus-read-move-group-name' an
8618           ;; opportunity to suggest an appropriate default.  However,
8619           ;; we needn't render or mark the article.
8620           (let ((gnus-display-mime-function nil)
8621                 (gnus-article-prepare-hook nil)
8622                 (gnus-mark-article-hook nil))
8623             (gnus-summary-select-article nil nil nil (car articles))))
8624       (setq to-newsgroup
8625             (gnus-read-move-group-name
8626              (cadr (assq action names))
8627              (symbol-value (intern (format "gnus-current-%s-group" action)))
8628              articles prefix))
8629       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8630     (setq to-method (or select-method
8631                         (gnus-server-to-method
8632                          (gnus-group-method to-newsgroup))))
8633     ;; Check the method we are to move this article to...
8634     (unless (gnus-check-backend-function
8635              'request-accept-article (car to-method))
8636       (error "%s does not support article copying" (car to-method)))
8637     (unless (gnus-check-server to-method)
8638       (error "Can't open server %s" (car to-method)))
8639     (gnus-message 6 "%s to %s: %s..."
8640                   (caddr (assq action names))
8641                   (or (car select-method) to-newsgroup) articles)
8642     (while articles
8643       (setq article (pop articles))
8644       (setq
8645        art-group
8646        (cond
8647         ;; Move the article.
8648         ((eq action 'move)
8649          ;; Remove this article from future suppression.
8650          (gnus-dup-unsuppress-article article)
8651          (gnus-request-move-article
8652           article                       ; Article to move
8653           gnus-newsgroup-name           ; From newsgroup
8654           (nth 1 (gnus-find-method-for-group
8655                   gnus-newsgroup-name)) ; Server
8656           (list 'gnus-request-accept-article
8657                 to-newsgroup (list 'quote select-method)
8658                 (not articles) t)       ; Accept form
8659           (not articles)))              ; Only save nov last time
8660         ;; Copy the article.
8661         ((eq action 'copy)
8662          (save-excursion
8663            (set-buffer copy-buf)
8664            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8665              (gnus-request-accept-article
8666               to-newsgroup select-method (not articles) t))))
8667         ;; Crosspost the article.
8668         ((eq action 'crosspost)
8669          (let ((xref (message-tokenize-header
8670                       (mail-header-xref (gnus-summary-article-header article))
8671                       " ")))
8672            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8673                                   ":" (number-to-string article)))
8674            (unless xref
8675              (setq xref (list (system-name))))
8676            (setq new-xref
8677                  (concat
8678                   (mapconcat 'identity
8679                              (delete "Xref:" (delete new-xref xref))
8680                              " ")
8681                   " " new-xref))
8682            (save-excursion
8683              (set-buffer copy-buf)
8684              ;; First put the article in the destination group.
8685              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8686              (when (consp (setq art-group
8687                                 (gnus-request-accept-article
8688                                  to-newsgroup select-method (not articles))))
8689                (setq new-xref (concat new-xref " " (car art-group)
8690                                       ":"
8691                                       (number-to-string (cdr art-group))))
8692                ;; Now we have the new Xrefs header, so we insert
8693                ;; it and replace the new article.
8694                (nnheader-replace-header "Xref" new-xref)
8695                (gnus-request-replace-article
8696                 (cdr art-group) to-newsgroup (current-buffer))
8697                art-group))))))
8698       (cond
8699        ((not art-group)
8700         (gnus-message 1 "Couldn't %s article %s: %s"
8701                       (cadr (assq action names)) article
8702                       (nnheader-get-report (car to-method))))
8703        ((eq art-group 'junk)
8704         (when (eq action 'move)
8705           (gnus-summary-mark-article article gnus-canceled-mark)
8706           (gnus-message 4 "Deleted article %s" article)))
8707        (t
8708         (let* ((pto-group (gnus-group-prefixed-name
8709                            (car art-group) to-method))
8710                (entry
8711                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8712                (info (nth 2 entry))
8713                (to-group (gnus-info-group info))
8714                to-marks)
8715           ;; Update the group that has been moved to.
8716           (when (and info
8717                      (memq action '(move copy)))
8718             (unless (member to-group to-groups)
8719               (push to-group to-groups))
8720
8721             (unless (memq article gnus-newsgroup-unreads)
8722               (push 'read to-marks)
8723               (gnus-info-set-read
8724                info (gnus-add-to-range (gnus-info-read info)
8725                                        (list (cdr art-group)))))
8726
8727             ;; See whether the article is to be put in the cache.
8728             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8729                              default-marks
8730                            no-expire-marks))
8731                   (to-article (cdr art-group)))
8732
8733               ;; Enter the article into the cache in the new group,
8734               ;; if that is required.
8735               (when gnus-use-cache
8736                 (gnus-cache-possibly-enter-article
8737                  to-group to-article
8738                  (let ((header (copy-sequence
8739                                 (gnus-summary-article-header article))))
8740                    (mail-header-set-number header to-article)
8741                    header)
8742                  (memq article gnus-newsgroup-marked)
8743                  (memq article gnus-newsgroup-dormant)
8744                  (memq article gnus-newsgroup-unreads)))
8745
8746               (when gnus-preserve-marks
8747                 ;; Copy any marks over to the new group.
8748                 (when (and (equal to-group gnus-newsgroup-name)
8749                            (not (memq article gnus-newsgroup-unreads)))
8750                   ;; Mark this article as read in this group.
8751                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8752                   (setcdr (gnus-active to-group) to-article)
8753                   (setcdr gnus-newsgroup-active to-article))
8754
8755                 (while marks
8756                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8757                     (when (memq article (symbol-value
8758                                          (intern (format "gnus-newsgroup-%s"
8759                                                          (caar marks)))))
8760                       (push (cdar marks) to-marks)
8761                       ;; If the other group is the same as this group,
8762                       ;; then we have to add the mark to the list.
8763                       (when (equal to-group gnus-newsgroup-name)
8764                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8765                              (cons to-article
8766                                    (symbol-value
8767                                     (intern (format "gnus-newsgroup-%s"
8768                                                     (caar marks)))))))
8769                       ;; Copy the marks to other group.
8770                       (gnus-add-marked-articles
8771                        to-group (cdar marks) (list to-article) info)))
8772                   (setq marks (cdr marks)))
8773
8774                 (gnus-request-set-mark to-group (list (list (list to-article)
8775                                                             'add
8776                                                             to-marks))))
8777
8778               (gnus-dribble-enter
8779                (concat "(gnus-group-set-info '"
8780                        (gnus-prin1-to-string (gnus-get-info to-group))
8781                        ")"))))
8782
8783           ;; Update the Xref header in this article to point to
8784           ;; the new crossposted article we have just created.
8785           (when (eq action 'crosspost)
8786             (save-excursion
8787               (set-buffer copy-buf)
8788               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8789               (nnheader-replace-header "Xref" new-xref)
8790               (gnus-request-replace-article
8791                article gnus-newsgroup-name (current-buffer)))))
8792
8793         ;;;!!!Why is this necessary?
8794         (set-buffer gnus-summary-buffer)
8795
8796         (gnus-summary-goto-subject article)
8797         (when (eq action 'move)
8798           (gnus-summary-mark-article article gnus-canceled-mark))))
8799       (gnus-summary-remove-process-mark article))
8800     ;; Re-activate all groups that have been moved to.
8801     (save-excursion
8802       (set-buffer gnus-group-buffer)
8803       (let ((gnus-group-marked to-groups))
8804         (gnus-group-get-new-news-this-group nil t)))
8805
8806     (gnus-kill-buffer copy-buf)
8807     (gnus-summary-position-point)
8808     (gnus-set-mode-line 'summary)))
8809
8810 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8811   "Move the current article to a different newsgroup.
8812 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8813 When called interactively, if TO-NEWSGROUP is nil, use the value of
8814 the variable `gnus-move-split-methods' for finding a default target
8815 newsgroup.
8816 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8817 re-spool using this method."
8818   (interactive "P")
8819   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8820
8821 (defun gnus-summary-crosspost-article (&optional n)
8822   "Crosspost the current article to some other group."
8823   (interactive "P")
8824   (gnus-summary-move-article n nil nil 'crosspost))
8825
8826 (defcustom gnus-summary-respool-default-method nil
8827   "Default method type for respooling an article.
8828 If nil, use to the current newsgroup method."
8829   :type 'symbol
8830   :group 'gnus-summary-mail)
8831
8832 (defun gnus-summary-respool-article (&optional n method)
8833   "Respool the current article.
8834 The article will be squeezed through the mail spooling process again,
8835 which means that it will be put in some mail newsgroup or other
8836 depending on `nnmail-split-methods'.
8837 If N is a positive number, respool the N next articles.
8838 If N is a negative number, respool the N previous articles.
8839 If N is nil and any articles have been marked with the process mark,
8840 respool those articles instead.
8841
8842 Respooling can be done both from mail groups and \"real\" newsgroups.
8843 In the former case, the articles in question will be moved from the
8844 current group into whatever groups they are destined to.  In the
8845 latter case, they will be copied into the relevant groups."
8846   (interactive
8847    (list current-prefix-arg
8848          (let* ((methods (gnus-methods-using 'respool))
8849                 (methname
8850                  (symbol-name (or gnus-summary-respool-default-method
8851                                   (car (gnus-find-method-for-group
8852                                         gnus-newsgroup-name)))))
8853                 (method
8854                  (gnus-completing-read-with-default
8855                   methname "What backend do you want to use when respooling?"
8856                   methods nil t nil 'gnus-mail-method-history))
8857                 ms)
8858            (cond
8859             ((zerop (length (setq ms (gnus-servers-using-backend
8860                                       (intern method)))))
8861              (list (intern method) ""))
8862             ((= 1 (length ms))
8863              (car ms))
8864             (t
8865              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8866                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8867                            ms-alist))))))))
8868   (unless method
8869     (error "No method given for respooling"))
8870   (if (assoc (symbol-name
8871               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8872              (gnus-methods-using 'respool))
8873       (gnus-summary-move-article n nil method)
8874     (gnus-summary-copy-article n nil method)))
8875
8876 (defun gnus-summary-import-article (file &optional edit)
8877   "Import an arbitrary file into a mail newsgroup."
8878   (interactive "fImport file: \nP")
8879   (let ((group gnus-newsgroup-name)
8880         (now (current-time))
8881         atts lines group-art)
8882     (unless (gnus-check-backend-function 'request-accept-article group)
8883       (error "%s does not support article importing" group))
8884     (or (file-readable-p file)
8885         (not (file-regular-p file))
8886         (error "Can't read %s" file))
8887     (save-excursion
8888       (set-buffer (gnus-get-buffer-create " *import file*"))
8889       (erase-buffer)
8890       (nnheader-insert-file-contents file)
8891       (goto-char (point-min))
8892       (if (nnheader-article-p)
8893           (save-restriction
8894             (goto-char (point-min))
8895             (search-forward "\n\n" nil t)
8896             (narrow-to-region (point-min) (1- (point)))
8897             (goto-char (point-min))
8898             (unless (re-search-forward "^date:" nil t)
8899               (goto-char (point-max))
8900               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8901         ;; This doesn't look like an article, so we fudge some headers.
8902         (setq atts (file-attributes file)
8903               lines (count-lines (point-min) (point-max)))
8904         (insert "From: " (read-string "From: ") "\n"
8905                 "Subject: " (read-string "Subject: ") "\n"
8906                 "Date: " (message-make-date (nth 5 atts)) "\n"
8907                 "Message-ID: " (message-make-message-id) "\n"
8908                 "Lines: " (int-to-string lines) "\n"
8909                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8910       (setq group-art (gnus-request-accept-article group nil t))
8911       (kill-buffer (current-buffer)))
8912     (setq gnus-newsgroup-active (gnus-activate-group group))
8913     (forward-line 1)
8914     (gnus-summary-goto-article (cdr group-art) nil t)
8915     (when edit
8916       (gnus-summary-edit-article))))
8917
8918 (defun gnus-summary-create-article ()
8919   "Create an article in a mail newsgroup."
8920   (interactive)
8921   (let ((group gnus-newsgroup-name)
8922         (now (current-time))
8923         group-art)
8924     (unless (gnus-check-backend-function 'request-accept-article group)
8925       (error "%s does not support article importing" group))
8926     (save-excursion
8927       (set-buffer (gnus-get-buffer-create " *import file*"))
8928       (erase-buffer)
8929       (goto-char (point-min))
8930       ;; This doesn't look like an article, so we fudge some headers.
8931       (insert "From: " (read-string "From: ") "\n"
8932               "Subject: " (read-string "Subject: ") "\n"
8933               "Date: " (message-make-date now) "\n"
8934               "Message-ID: " (message-make-message-id) "\n")
8935       (setq group-art (gnus-request-accept-article group nil t))
8936       (kill-buffer (current-buffer)))
8937     (setq gnus-newsgroup-active (gnus-activate-group group))
8938     (forward-line 1)
8939     (gnus-summary-goto-article (cdr group-art) nil t)
8940     (gnus-summary-edit-article)))
8941
8942 (defun gnus-summary-article-posted-p ()
8943   "Say whether the current (mail) article is available from news as well.
8944 This will be the case if the article has both been mailed and posted."
8945   (interactive)
8946   (let ((id (mail-header-references (gnus-summary-article-header)))
8947         (gnus-override-method (car (gnus-refer-article-methods))))
8948     (if (gnus-request-head id "")
8949         (gnus-message 2 "The current message was found on %s"
8950                       gnus-override-method)
8951       (gnus-message 2 "The current message couldn't be found on %s"
8952                     gnus-override-method)
8953       nil)))
8954
8955 (defun gnus-summary-expire-articles (&optional now)
8956   "Expire all articles that are marked as expirable in the current group."
8957   (interactive)
8958   (when (gnus-check-backend-function
8959          'request-expire-articles gnus-newsgroup-name)
8960     ;; This backend supports expiry.
8961     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8962            (expirable (if total
8963                           (progn
8964                             ;; We need to update the info for
8965                             ;; this group for `gnus-list-of-read-articles'
8966                             ;; to give us the right answer.
8967                             (gnus-run-hooks 'gnus-exit-group-hook)
8968                             (gnus-summary-update-info)
8969                             (gnus-list-of-read-articles gnus-newsgroup-name))
8970                         (setq gnus-newsgroup-expirable
8971                               (sort gnus-newsgroup-expirable '<))))
8972            (expiry-wait (if now 'immediate
8973                           (gnus-group-find-parameter
8974                            gnus-newsgroup-name 'expiry-wait)))
8975            (nnmail-expiry-target
8976             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8977                 nnmail-expiry-target))
8978            es)
8979       (when expirable
8980         ;; There are expirable articles in this group, so we run them
8981         ;; through the expiry process.
8982         (gnus-message 6 "Expiring articles...")
8983         (unless (gnus-check-group gnus-newsgroup-name)
8984           (error "Can't open server for %s" gnus-newsgroup-name))
8985         ;; The list of articles that weren't expired is returned.
8986         (save-excursion
8987           (if expiry-wait
8988               (let ((nnmail-expiry-wait-function nil)
8989                     (nnmail-expiry-wait expiry-wait))
8990                 (setq es (gnus-request-expire-articles
8991                           expirable gnus-newsgroup-name)))
8992             (setq es (gnus-request-expire-articles
8993                       expirable gnus-newsgroup-name)))
8994           (unless total
8995             (setq gnus-newsgroup-expirable es))
8996           ;; We go through the old list of expirable, and mark all
8997           ;; really expired articles as nonexistent.
8998           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8999             (let ((gnus-use-cache nil))
9000               (dolist (article expirable)
9001                 (when (and (not (memq article es))
9002                            (gnus-data-find article))
9003                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9004         (gnus-message 6 "Expiring articles...done")))))
9005
9006 (defun gnus-summary-expire-articles-now ()
9007   "Expunge all expirable articles in the current group.
9008 This means that *all* articles that are marked as expirable will be
9009 deleted forever, right now."
9010   (interactive)
9011   (or gnus-expert-user
9012       (gnus-yes-or-no-p
9013        "Are you really, really, really sure you want to delete all these messages? ")
9014       (error "Phew!"))
9015   (gnus-summary-expire-articles t))
9016
9017 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9018 (defun gnus-summary-delete-article (&optional n)
9019   "Delete the N next (mail) articles.
9020 This command actually deletes articles.  This is not a marking
9021 command.  The article will disappear forever from your life, never to
9022 return.
9023 If N is negative, delete backwards.
9024 If N is nil and articles have been marked with the process mark,
9025 delete these instead."
9026   (interactive "P")
9027   (unless (gnus-check-backend-function 'request-expire-articles
9028                                        gnus-newsgroup-name)
9029     (error "The current newsgroup does not support article deletion"))
9030   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9031     (error "Couldn't open server"))
9032   ;; Compute the list of articles to delete.
9033   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9034         (nnmail-expiry-target 'delete)
9035         not-deleted)
9036     (if (and gnus-novice-user
9037              (not (gnus-yes-or-no-p
9038                    (format "Do you really want to delete %s forever? "
9039                            (if (> (length articles) 1)
9040                                (format "these %s articles" (length articles))
9041                              "this article")))))
9042         ()
9043       ;; Delete the articles.
9044       (setq not-deleted (gnus-request-expire-articles
9045                          articles gnus-newsgroup-name 'force))
9046       (while articles
9047         (gnus-summary-remove-process-mark (car articles))
9048         ;; The backend might not have been able to delete the article
9049         ;; after all.
9050         (unless (memq (car articles) not-deleted)
9051           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9052         (setq articles (cdr articles)))
9053       (when not-deleted
9054         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9055     (gnus-summary-position-point)
9056     (gnus-set-mode-line 'summary)
9057     not-deleted))
9058
9059 (defun gnus-summary-edit-article (&optional force)
9060   "Edit the current article.
9061 This will have permanent effect only in mail groups.
9062 If FORCE is non-nil, allow editing of articles even in read-only
9063 groups."
9064   (interactive "P")
9065   (save-excursion
9066     (set-buffer gnus-summary-buffer)
9067     (let ((mail-parse-charset gnus-newsgroup-charset)
9068           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9069       (gnus-set-global-variables)
9070       (when (and (not force)
9071                  (gnus-group-read-only-p))
9072         (error "The current newsgroup does not support article editing"))
9073       (gnus-summary-show-article t)
9074       (gnus-article-edit-article
9075        'ignore
9076        `(lambda (no-highlight)
9077           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9078                 (message-options message-options)
9079                 (message-options-set-recipient)
9080                 (mail-parse-ignored-charsets
9081                  ',gnus-newsgroup-ignored-charsets))
9082             (gnus-summary-edit-article-done
9083              ,(or (mail-header-references gnus-current-headers) "")
9084              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9085
9086 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9087
9088 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9089                                                  no-highlight)
9090   "Make edits to the current article permanent."
9091   (interactive)
9092   (save-excursion
9093     ;; The buffer restriction contains the entire article if it exists.
9094     (when (article-goto-body)
9095       (let ((lines (count-lines (point) (point-max)))
9096             (length (- (point-max) (point)))
9097             (case-fold-search t)
9098             (body (copy-marker (point))))
9099         (goto-char (point-min))
9100         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9101           (delete-region (match-beginning 1) (match-end 1))
9102           (insert (number-to-string length)))
9103         (goto-char (point-min))
9104         (when (re-search-forward
9105                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9106           (delete-region (match-beginning 1) (match-end 1))
9107           (insert (number-to-string length)))
9108         (goto-char (point-min))
9109         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9110           (delete-region (match-beginning 1) (match-end 1))
9111           (insert (number-to-string lines))))))
9112   ;; Replace the article.
9113   (let ((buf (current-buffer)))
9114     (with-temp-buffer
9115       (insert-buffer-substring buf)
9116
9117       (if (and (not read-only)
9118                (not (gnus-request-replace-article
9119                      (cdr gnus-article-current) (car gnus-article-current)
9120                      (current-buffer) t)))
9121           (error "Couldn't replace article")
9122         ;; Update the summary buffer.
9123         (if (and references
9124                  (equal (message-tokenize-header references " ")
9125                         (message-tokenize-header
9126                          (or (message-fetch-field "references") "") " ")))
9127             ;; We only have to update this line.
9128             (save-excursion
9129               (save-restriction
9130                 (message-narrow-to-head)
9131                 (let ((head (buffer-string))
9132                       header)
9133                   (with-temp-buffer
9134                     (insert (format "211 %d Article retrieved.\n"
9135                                     (cdr gnus-article-current)))
9136                     (insert head)
9137                     (insert ".\n")
9138                     (let ((nntp-server-buffer (current-buffer)))
9139                       (setq header (car (gnus-get-newsgroup-headers
9140                                          nil t))))
9141                     (save-excursion
9142                       (set-buffer gnus-summary-buffer)
9143                       (gnus-data-set-header
9144                        (gnus-data-find (cdr gnus-article-current))
9145                        header)
9146                       (gnus-summary-update-article-line
9147                        (cdr gnus-article-current) header)
9148                       (if (gnus-summary-goto-subject
9149                            (cdr gnus-article-current) nil t)
9150                           (gnus-summary-update-secondary-mark
9151                            (cdr gnus-article-current))))))))
9152           ;; Update threads.
9153           (set-buffer (or buffer gnus-summary-buffer))
9154           (gnus-summary-update-article (cdr gnus-article-current))
9155           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9156               (gnus-summary-update-secondary-mark
9157                (cdr gnus-article-current))))
9158         ;; Prettify the article buffer again.
9159         (unless no-highlight
9160           (save-excursion
9161             (set-buffer gnus-article-buffer)
9162             ;;;!!! Fix this -- article should be rehighlighted.
9163             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9164             (set-buffer gnus-original-article-buffer)
9165             (gnus-request-article
9166              (cdr gnus-article-current)
9167              (car gnus-article-current) (current-buffer))))
9168         ;; Prettify the summary buffer line.
9169         (when (gnus-visual-p 'summary-highlight 'highlight)
9170           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9171
9172 (defun gnus-summary-edit-wash (key)
9173   "Perform editing command KEY in the article buffer."
9174   (interactive
9175    (list
9176     (progn
9177       (message "%s" (concat (this-command-keys) "- "))
9178       (read-char))))
9179   (message "")
9180   (gnus-summary-edit-article)
9181   (execute-kbd-macro (concat (this-command-keys) key))
9182   (gnus-article-edit-done))
9183
9184 ;;; Respooling
9185
9186 (defun gnus-summary-respool-query (&optional silent trace)
9187   "Query where the respool algorithm would put this article."
9188   (interactive)
9189   (let (gnus-mark-article-hook)
9190     (gnus-summary-select-article)
9191     (save-excursion
9192       (set-buffer gnus-original-article-buffer)
9193       (save-restriction
9194         (message-narrow-to-head)
9195         (let ((groups (nnmail-article-group 'identity trace)))
9196           (unless silent
9197             (if groups
9198                 (message "This message would go to %s"
9199                          (mapconcat 'car groups ", "))
9200               (message "This message would go to no groups"))
9201             groups))))))
9202
9203 (defun gnus-summary-respool-trace ()
9204   "Trace where the respool algorithm would put this article.
9205 Display a buffer showing all fancy splitting patterns which matched."
9206   (interactive)
9207   (gnus-summary-respool-query nil t))
9208
9209 ;; Summary marking commands.
9210
9211 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9212   "Mark articles which has the same subject as read, and then select the next.
9213 If UNMARK is positive, remove any kind of mark.
9214 If UNMARK is negative, tick articles."
9215   (interactive "P")
9216   (when unmark
9217     (setq unmark (prefix-numeric-value unmark)))
9218   (let ((count
9219          (gnus-summary-mark-same-subject
9220           (gnus-summary-article-subject) unmark)))
9221     ;; Select next unread article.  If auto-select-same mode, should
9222     ;; select the first unread article.
9223     (gnus-summary-next-article t (and gnus-auto-select-same
9224                                       (gnus-summary-article-subject)))
9225     (gnus-message 7 "%d article%s marked as %s"
9226                   count (if (= count 1) " is" "s are")
9227                   (if unmark "unread" "read"))))
9228
9229 (defun gnus-summary-kill-same-subject (&optional unmark)
9230   "Mark articles which has the same subject as read.
9231 If UNMARK is positive, remove any kind of mark.
9232 If UNMARK is negative, tick articles."
9233   (interactive "P")
9234   (when unmark
9235     (setq unmark (prefix-numeric-value unmark)))
9236   (let ((count
9237          (gnus-summary-mark-same-subject
9238           (gnus-summary-article-subject) unmark)))
9239     ;; If marked as read, go to next unread subject.
9240     (when (null unmark)
9241       ;; Go to next unread subject.
9242       (gnus-summary-next-subject 1 t))
9243     (gnus-message 7 "%d articles are marked as %s"
9244                   count (if unmark "unread" "read"))))
9245
9246 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9247   "Mark articles with same SUBJECT as read, and return marked number.
9248 If optional argument UNMARK is positive, remove any kinds of marks.
9249 If optional argument UNMARK is negative, mark articles as unread instead."
9250   (let ((count 1))
9251     (save-excursion
9252       (cond
9253        ((null unmark)                   ; Mark as read.
9254         (while (and
9255                 (progn
9256                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9257                   (gnus-summary-show-thread) t)
9258                 (gnus-summary-find-subject subject))
9259           (setq count (1+ count))))
9260        ((> unmark 0)                    ; Tick.
9261         (while (and
9262                 (progn
9263                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9264                   (gnus-summary-show-thread) t)
9265                 (gnus-summary-find-subject subject))
9266           (setq count (1+ count))))
9267        (t                               ; Mark as unread.
9268         (while (and
9269                 (progn
9270                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9271                   (gnus-summary-show-thread) t)
9272                 (gnus-summary-find-subject subject))
9273           (setq count (1+ count)))))
9274       (gnus-set-mode-line 'summary)
9275       ;; Return the number of marked articles.
9276       count)))
9277
9278 (defun gnus-summary-mark-as-processable (n &optional unmark)
9279   "Set the process mark on the next N articles.
9280 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9281 the process mark instead.  The difference between N and the actual
9282 number of articles marked is returned."
9283   (interactive "P")
9284   (if (and (null n) (gnus-region-active-p))
9285       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9286     (setq n (prefix-numeric-value n))
9287     (let ((backward (< n 0))
9288           (n (abs n)))
9289       (while (and
9290               (> n 0)
9291               (if unmark
9292                   (gnus-summary-remove-process-mark
9293                    (gnus-summary-article-number))
9294                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9295               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9296         (setq n (1- n)))
9297       (when (/= 0 n)
9298         (gnus-message 7 "No more articles"))
9299       (gnus-summary-recenter)
9300       (gnus-summary-position-point)
9301       n)))
9302
9303 (defun gnus-summary-unmark-as-processable (n)
9304   "Remove the process mark from the next N articles.
9305 If N is negative, unmark backward instead.  The difference between N and
9306 the actual number of articles unmarked is returned."
9307   (interactive "P")
9308   (gnus-summary-mark-as-processable n t))
9309
9310 (defun gnus-summary-unmark-all-processable ()
9311   "Remove the process mark from all articles."
9312   (interactive)
9313   (save-excursion
9314     (while gnus-newsgroup-processable
9315       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9316   (gnus-summary-position-point))
9317
9318 (defun gnus-summary-add-mark (article type)
9319   "Mark ARTICLE with a mark of TYPE."
9320   (let ((vtype (car (assq type gnus-article-mark-lists)))
9321         var)
9322     (if (not vtype)
9323         (error "No such mark type: %s" type)
9324       (setq var (intern (format "gnus-newsgroup-%s" type)))
9325       (set var (cons article (symbol-value var)))
9326       (if (memq type '(processable cached replied forwarded recent saved))
9327           (gnus-summary-update-secondary-mark article)
9328         ;;; !!! This is bogus.  We should find out what primary
9329         ;;; !!! mark we want to set.
9330         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9331
9332 (defun gnus-summary-mark-as-expirable (n)
9333   "Mark N articles forward as expirable.
9334 If N is negative, mark backward instead.  The difference between N and
9335 the actual number of articles marked is returned."
9336   (interactive "p")
9337   (gnus-summary-mark-forward n gnus-expirable-mark))
9338
9339 (defun gnus-summary-mark-as-spam (n)
9340   "Mark N articles forward as spam.
9341 If N is negative, mark backward instead.  The difference between N and
9342 the actual number of articles marked is returned."
9343   (interactive "p")
9344   (gnus-summary-mark-forward n gnus-spam-mark))
9345
9346 (defun gnus-summary-mark-article-as-replied (article)
9347   "Mark ARTICLE as replied to and update the summary line.
9348 ARTICLE can also be a list of articles."
9349   (interactive (list (gnus-summary-article-number)))
9350   (let ((articles (if (listp article) article (list article))))
9351     (dolist (article articles)
9352       (push article gnus-newsgroup-replied)
9353       (let ((buffer-read-only nil))
9354         (when (gnus-summary-goto-subject article nil t)
9355           (gnus-summary-update-secondary-mark article))))))
9356
9357 (defun gnus-summary-mark-article-as-forwarded (article)
9358   "Mark ARTICLE as forwarded and update the summary line.
9359 ARTICLE can also be a list of articles."
9360   (let ((articles (if (listp article) article (list article))))
9361     (dolist (article articles)
9362       (push article gnus-newsgroup-forwarded)
9363       (let ((buffer-read-only nil))
9364         (when (gnus-summary-goto-subject article nil t)
9365           (gnus-summary-update-secondary-mark article))))))
9366
9367 (defun gnus-summary-set-bookmark (article)
9368   "Set a bookmark in current article."
9369   (interactive (list (gnus-summary-article-number)))
9370   (when (or (not (get-buffer gnus-article-buffer))
9371             (not gnus-current-article)
9372             (not gnus-article-current)
9373             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9374     (error "No current article selected"))
9375   ;; Remove old bookmark, if one exists.
9376   (let ((old (assq article gnus-newsgroup-bookmarks)))
9377     (when old
9378       (setq gnus-newsgroup-bookmarks
9379             (delq old gnus-newsgroup-bookmarks))))
9380   ;; Set the new bookmark, which is on the form
9381   ;; (article-number . line-number-in-body).
9382   (push
9383    (cons article
9384          (save-excursion
9385            (set-buffer gnus-article-buffer)
9386            (count-lines
9387             (min (point)
9388                  (save-excursion
9389                    (goto-char (point-min))
9390                    (search-forward "\n\n" nil t)
9391                    (point)))
9392             (point))))
9393    gnus-newsgroup-bookmarks)
9394   (gnus-message 6 "A bookmark has been added to the current article."))
9395
9396 (defun gnus-summary-remove-bookmark (article)
9397   "Remove the bookmark from the current article."
9398   (interactive (list (gnus-summary-article-number)))
9399   ;; Remove old bookmark, if one exists.
9400   (let ((old (assq article gnus-newsgroup-bookmarks)))
9401     (if old
9402         (progn
9403           (setq gnus-newsgroup-bookmarks
9404                 (delq old gnus-newsgroup-bookmarks))
9405           (gnus-message 6 "Removed bookmark."))
9406       (gnus-message 6 "No bookmark in current article."))))
9407
9408 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9409 (defun gnus-summary-mark-as-dormant (n)
9410   "Mark N articles forward as dormant.
9411 If N is negative, mark backward instead.  The difference between N and
9412 the actual number of articles marked is returned."
9413   (interactive "p")
9414   (gnus-summary-mark-forward n gnus-dormant-mark))
9415
9416 (defun gnus-summary-set-process-mark (article)
9417   "Set the process mark on ARTICLE and update the summary line."
9418   (setq gnus-newsgroup-processable
9419         (cons article
9420               (delq article gnus-newsgroup-processable)))
9421   (when (gnus-summary-goto-subject article)
9422     (gnus-summary-show-thread)
9423     (gnus-summary-goto-subject article)
9424     (gnus-summary-update-secondary-mark article)))
9425
9426 (defun gnus-summary-remove-process-mark (article)
9427   "Remove the process mark from ARTICLE and update the summary line."
9428   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9429   (when (gnus-summary-goto-subject article)
9430     (gnus-summary-show-thread)
9431     (gnus-summary-goto-subject article)
9432     (gnus-summary-update-secondary-mark article)))
9433
9434 (defun gnus-summary-set-saved-mark (article)
9435   "Set the process mark on ARTICLE and update the summary line."
9436   (push article gnus-newsgroup-saved)
9437   (when (gnus-summary-goto-subject article)
9438     (gnus-summary-update-secondary-mark article)))
9439
9440 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9441   "Mark N articles as read forwards.
9442 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9443 The difference between N and the actual number of articles marked is
9444 returned.
9445 Iff NO-EXPIRE, auto-expiry will be inhibited."
9446   (interactive "p")
9447   (gnus-summary-show-thread)
9448   (let ((backward (< n 0))
9449         (gnus-summary-goto-unread
9450          (and gnus-summary-goto-unread
9451               (not (eq gnus-summary-goto-unread 'never))
9452               (not (memq mark (list gnus-unread-mark
9453                                     gnus-ticked-mark gnus-dormant-mark)))))
9454         (n (abs n))
9455         (mark (or mark gnus-del-mark)))
9456     (while (and (> n 0)
9457                 (gnus-summary-mark-article nil mark no-expire)
9458                 (zerop (gnus-summary-next-subject
9459                         (if backward -1 1)
9460                         (and gnus-summary-goto-unread
9461                              (not (eq gnus-summary-goto-unread 'never)))
9462                         t)))
9463       (setq n (1- n)))
9464     (when (/= 0 n)
9465       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9466     (gnus-summary-recenter)
9467     (gnus-summary-position-point)
9468     (gnus-set-mode-line 'summary)
9469     n))
9470
9471 (defun gnus-summary-mark-article-as-read (mark)
9472   "Mark the current article quickly as read with MARK."
9473   (let ((article (gnus-summary-article-number)))
9474     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9475     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9476     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9477     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9478     (push (cons article mark) gnus-newsgroup-reads)
9479     ;; Possibly remove from cache, if that is used.
9480     (when gnus-use-cache
9481       (gnus-cache-enter-remove-article article))
9482     ;; Allow the backend to change the mark.
9483     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9484     ;; Check for auto-expiry.
9485     (when (and gnus-newsgroup-auto-expire
9486                (memq mark gnus-auto-expirable-marks))
9487       (setq mark gnus-expirable-mark)
9488       ;; Let the backend know about the mark change.
9489       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9490       (push article gnus-newsgroup-expirable))
9491     ;; Set the mark in the buffer.
9492     (gnus-summary-update-mark mark 'unread)
9493     t))
9494
9495 (defun gnus-summary-mark-article-as-unread (mark)
9496   "Mark the current article quickly as unread with MARK."
9497   (let* ((article (gnus-summary-article-number))
9498          (old-mark (gnus-summary-article-mark article)))
9499     ;; Allow the backend to change the mark.
9500     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9501     (if (eq mark old-mark)
9502         t
9503       (if (<= article 0)
9504           (progn
9505             (gnus-error 1 "Can't mark negative article numbers")
9506             nil)
9507         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9508         (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9509         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9510         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9511         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9512         (cond ((= mark gnus-ticked-mark)
9513                (setq gnus-newsgroup-marked
9514                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9515                                               article)))
9516               ((= mark gnus-spam-mark)
9517                (setq gnus-newsgroup-spam-marked
9518                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9519                                               article)))
9520               ((= mark gnus-dormant-mark)
9521                (setq gnus-newsgroup-dormant
9522                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9523                                               article)))
9524               (t
9525                (setq gnus-newsgroup-unreads
9526                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9527                                               article))))
9528         (gnus-pull article gnus-newsgroup-reads)
9529
9530         ;; See whether the article is to be put in the cache.
9531         (and gnus-use-cache
9532              (vectorp (gnus-summary-article-header article))
9533              (save-excursion
9534                (gnus-cache-possibly-enter-article
9535                 gnus-newsgroup-name article
9536                 (gnus-summary-article-header article)
9537                 (= mark gnus-ticked-mark)
9538                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9539
9540         ;; Fix the mark.
9541         (gnus-summary-update-mark mark 'unread)
9542         t))))
9543
9544 (defun gnus-summary-mark-article (&optional article mark no-expire)
9545   "Mark ARTICLE with MARK.  MARK can be any character.
9546 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9547 `??' (dormant) and `?E' (expirable).
9548 If MARK is nil, then the default character `?r' is used.
9549 If ARTICLE is nil, then the article on the current line will be
9550 marked.
9551 Iff NO-EXPIRE, auto-expiry will be inhibited."
9552   ;; The mark might be a string.
9553   (when (stringp mark)
9554     (setq mark (aref mark 0)))
9555   ;; If no mark is given, then we check auto-expiring.
9556   (when (null mark)
9557     (setq mark gnus-del-mark))
9558   (when (and (not no-expire)
9559              gnus-newsgroup-auto-expire
9560              (memq mark gnus-auto-expirable-marks))
9561     (setq mark gnus-expirable-mark))
9562   (let ((article (or article (gnus-summary-article-number)))
9563         (old-mark (gnus-summary-article-mark article)))
9564     ;; Allow the backend to change the mark.
9565     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9566     (if (eq mark old-mark)
9567         t
9568       (unless article
9569         (error "No article on current line"))
9570       (if (not (if (or (= mark gnus-unread-mark)
9571                        (= mark gnus-ticked-mark)
9572                        (= mark gnus-spam-mark)
9573                        (= mark gnus-dormant-mark))
9574                    (gnus-mark-article-as-unread article mark)
9575                  (gnus-mark-article-as-read article mark)))
9576           t
9577         ;; See whether the article is to be put in the cache.
9578         (and gnus-use-cache
9579              (not (= mark gnus-canceled-mark))
9580              (vectorp (gnus-summary-article-header article))
9581              (save-excursion
9582                (gnus-cache-possibly-enter-article
9583                 gnus-newsgroup-name article
9584                 (gnus-summary-article-header article)
9585                 (= mark gnus-ticked-mark)
9586                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9587
9588         (when (gnus-summary-goto-subject article nil t)
9589           (let ((buffer-read-only nil))
9590             (gnus-summary-show-thread)
9591             ;; Fix the mark.
9592             (gnus-summary-update-mark mark 'unread)
9593             t))))))
9594
9595 (defun gnus-summary-update-secondary-mark (article)
9596   "Update the secondary (read, process, cache) mark."
9597   (gnus-summary-update-mark
9598    (cond ((memq article gnus-newsgroup-processable)
9599           gnus-process-mark)
9600          ((memq article gnus-newsgroup-cached)
9601           gnus-cached-mark)
9602          ((memq article gnus-newsgroup-replied)
9603           gnus-replied-mark)
9604          ((memq article gnus-newsgroup-forwarded)
9605           gnus-forwarded-mark)
9606          ((memq article gnus-newsgroup-saved)
9607           gnus-saved-mark)
9608          ((memq article gnus-newsgroup-recent)
9609           gnus-recent-mark)
9610          ((memq article gnus-newsgroup-unseen)
9611           gnus-unseen-mark)
9612          (t gnus-no-mark))
9613    'replied)
9614   (when (gnus-visual-p 'summary-highlight 'highlight)
9615     (gnus-run-hooks 'gnus-summary-update-hook))
9616   t)
9617
9618 (defun gnus-summary-update-mark (mark type)
9619   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9620         (buffer-read-only nil))
9621     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9622     (when forward
9623       (when (looking-at "\r")
9624         (incf forward))
9625       (when (<= (+ forward (point)) (point-max))
9626         ;; Go to the right position on the line.
9627         (goto-char (+ forward (point)))
9628         ;; Replace the old mark with the new mark.
9629         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9630         ;; Optionally update the marks by some user rule.
9631         (when (eq type 'unread)
9632           (gnus-data-set-mark
9633            (gnus-data-find (gnus-summary-article-number)) mark)
9634           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9635
9636 (defun gnus-mark-article-as-read (article &optional mark)
9637   "Enter ARTICLE in the pertinent lists and remove it from others."
9638   ;; Make the article expirable.
9639   (let ((mark (or mark gnus-del-mark)))
9640     (setq gnus-newsgroup-expirable
9641           (if (= mark gnus-expirable-mark)
9642               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9643             (delq article gnus-newsgroup-expirable)))
9644     ;; Remove from unread and marked lists.
9645     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9646     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9647     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9648     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9649     (push (cons article mark) gnus-newsgroup-reads)
9650     ;; Possibly remove from cache, if that is used.
9651     (when gnus-use-cache
9652       (gnus-cache-enter-remove-article article))
9653     t))
9654
9655 (defun gnus-mark-article-as-unread (article &optional mark)
9656   "Enter ARTICLE in the pertinent lists and remove it from others."
9657   (let ((mark (or mark gnus-ticked-mark)))
9658     (if (<= article 0)
9659         (progn
9660           (gnus-error 1 "Can't mark negative article numbers")
9661           nil)
9662       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9663             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9664             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9665             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9666             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9667
9668       ;; Unsuppress duplicates?
9669       (when gnus-suppress-duplicates
9670         (gnus-dup-unsuppress-article article))
9671
9672       (cond ((= mark gnus-ticked-mark)
9673              (setq gnus-newsgroup-marked
9674                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9675             ((= mark gnus-spam-mark)
9676              (setq gnus-newsgroup-spam-marked
9677                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked article)))
9678             ((= mark gnus-dormant-mark)
9679              (setq gnus-newsgroup-dormant
9680                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9681             (t
9682              (setq gnus-newsgroup-unreads
9683                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9684       (gnus-pull article gnus-newsgroup-reads)
9685       t)))
9686
9687 (defalias 'gnus-summary-mark-as-unread-forward
9688   'gnus-summary-tick-article-forward)
9689 (make-obsolete 'gnus-summary-mark-as-unread-forward
9690                'gnus-summary-tick-article-forward)
9691 (defun gnus-summary-tick-article-forward (n)
9692   "Tick N articles forwards.
9693 If N is negative, tick backwards instead.
9694 The difference between N and the number of articles ticked is returned."
9695   (interactive "p")
9696   (gnus-summary-mark-forward n gnus-ticked-mark))
9697
9698 (defalias 'gnus-summary-mark-as-unread-backward
9699   'gnus-summary-tick-article-backward)
9700 (make-obsolete 'gnus-summary-mark-as-unread-backward
9701                'gnus-summary-tick-article-backward)
9702 (defun gnus-summary-tick-article-backward (n)
9703   "Tick N articles backwards.
9704 The difference between N and the number of articles ticked is returned."
9705   (interactive "p")
9706   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9707
9708 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9709 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9710 (defun gnus-summary-tick-article (&optional article clear-mark)
9711   "Mark current article as unread.
9712 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9713 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9714   (interactive)
9715   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9716                                        gnus-ticked-mark)))
9717
9718 (defun gnus-summary-mark-as-read-forward (n)
9719   "Mark N articles as read forwards.
9720 If N is negative, mark backwards instead.
9721 The difference between N and the actual number of articles marked is
9722 returned."
9723   (interactive "p")
9724   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9725
9726 (defun gnus-summary-mark-as-read-backward (n)
9727   "Mark the N articles as read backwards.
9728 The difference between N and the actual number of articles marked is
9729 returned."
9730   (interactive "p")
9731   (gnus-summary-mark-forward
9732    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9733
9734 (defun gnus-summary-mark-as-read (&optional article mark)
9735   "Mark current article as read.
9736 ARTICLE specifies the article to be marked as read.
9737 MARK specifies a string to be inserted at the beginning of the line."
9738   (gnus-summary-mark-article article mark))
9739
9740 (defun gnus-summary-clear-mark-forward (n)
9741   "Clear marks from N articles forward.
9742 If N is negative, clear backward instead.
9743 The difference between N and the number of marks cleared is returned."
9744   (interactive "p")
9745   (gnus-summary-mark-forward n gnus-unread-mark))
9746
9747 (defun gnus-summary-clear-mark-backward (n)
9748   "Clear marks from N articles backward.
9749 The difference between N and the number of marks cleared is returned."
9750   (interactive "p")
9751   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9752
9753 (defun gnus-summary-mark-unread-as-read ()
9754   "Intended to be used by `gnus-summary-mark-article-hook'."
9755   (when (memq gnus-current-article gnus-newsgroup-unreads)
9756     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9757
9758 (defun gnus-summary-mark-read-and-unread-as-read ()
9759   "Intended to be used by `gnus-summary-mark-article-hook'."
9760   (let ((mark (gnus-summary-article-mark)))
9761     (when (or (gnus-unread-mark-p mark)
9762               (gnus-read-mark-p mark))
9763       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9764
9765 (defun gnus-summary-mark-unread-as-ticked ()
9766   "Intended to be used by `gnus-summary-mark-article-hook'."
9767   (when (memq gnus-current-article gnus-newsgroup-unreads)
9768     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9769
9770 (defun gnus-summary-mark-region-as-read (point mark all)
9771   "Mark all unread articles between point and mark as read.
9772 If given a prefix, mark all articles between point and mark as read,
9773 even ticked and dormant ones."
9774   (interactive "r\nP")
9775   (save-excursion
9776     (let (article)
9777       (goto-char point)
9778       (beginning-of-line)
9779       (while (and
9780               (< (point) mark)
9781               (progn
9782                 (when (or all
9783                           (memq (setq article (gnus-summary-article-number))
9784                                 gnus-newsgroup-unreads))
9785                   (gnus-summary-mark-article article gnus-del-mark))
9786                 t)
9787               (gnus-summary-find-next))))))
9788
9789 (defun gnus-summary-mark-below (score mark)
9790   "Mark articles with score less than SCORE with MARK."
9791   (interactive "P\ncMark: ")
9792   (setq score (if score
9793                   (prefix-numeric-value score)
9794                 (or gnus-summary-default-score 0)))
9795   (save-excursion
9796     (set-buffer gnus-summary-buffer)
9797     (goto-char (point-min))
9798     (while
9799         (progn
9800           (and (< (gnus-summary-article-score) score)
9801                (gnus-summary-mark-article nil mark))
9802           (gnus-summary-find-next)))))
9803
9804 (defun gnus-summary-kill-below (&optional score)
9805   "Mark articles with score below SCORE as read."
9806   (interactive "P")
9807   (gnus-summary-mark-below score gnus-killed-mark))
9808
9809 (defun gnus-summary-clear-above (&optional score)
9810   "Clear all marks from articles with score above SCORE."
9811   (interactive "P")
9812   (gnus-summary-mark-above score gnus-unread-mark))
9813
9814 (defun gnus-summary-tick-above (&optional score)
9815   "Tick all articles with score above SCORE."
9816   (interactive "P")
9817   (gnus-summary-mark-above score gnus-ticked-mark))
9818
9819 (defun gnus-summary-mark-above (score mark)
9820   "Mark articles with score over SCORE with MARK."
9821   (interactive "P\ncMark: ")
9822   (setq score (if score
9823                   (prefix-numeric-value score)
9824                 (or gnus-summary-default-score 0)))
9825   (save-excursion
9826     (set-buffer gnus-summary-buffer)
9827     (goto-char (point-min))
9828     (while (and (progn
9829                   (when (> (gnus-summary-article-score) score)
9830                     (gnus-summary-mark-article nil mark))
9831                   t)
9832                 (gnus-summary-find-next)))))
9833
9834 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9835 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9836 (defun gnus-summary-limit-include-expunged (&optional no-error)
9837   "Display all the hidden articles that were expunged for low scores."
9838   (interactive)
9839   (let ((buffer-read-only nil))
9840     (let ((scored gnus-newsgroup-scored)
9841           headers h)
9842       (while scored
9843         (unless (gnus-summary-article-header (caar scored))
9844           (and (setq h (gnus-number-to-header (caar scored)))
9845                (< (cdar scored) gnus-summary-expunge-below)
9846                (push h headers)))
9847         (setq scored (cdr scored)))
9848       (if (not headers)
9849           (when (not no-error)
9850             (error "No expunged articles hidden"))
9851         (goto-char (point-min))
9852         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9853         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9854         (mapcar (lambda (x) (push (mail-header-number x)
9855                                   gnus-newsgroup-limit))
9856                 headers)
9857         (gnus-summary-prepare-unthreaded (nreverse headers))
9858         (goto-char (point-min))
9859         (gnus-summary-position-point)
9860         t))))
9861
9862 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9863   "Mark all unread articles in this newsgroup as read.
9864 If prefix argument ALL is non-nil, ticked and dormant articles will
9865 also be marked as read.
9866 If QUIETLY is non-nil, no questions will be asked.
9867 If TO-HERE is non-nil, it should be a point in the buffer.  All
9868 articles before (after, if REVERSE is set) this point will be marked as read.
9869 Note that this function will only catch up the unread article
9870 in the current summary buffer limitation.
9871 The number of articles marked as read is returned."
9872   (interactive "P")
9873   (prog1
9874       (save-excursion
9875         (when (or quietly
9876                   (not gnus-interactive-catchup) ;Without confirmation?
9877                   gnus-expert-user
9878                   (gnus-y-or-n-p
9879                    (if all
9880                        "Mark absolutely all articles as read? "
9881                      "Mark all unread articles as read? ")))
9882           (if (and not-mark
9883                    (not gnus-newsgroup-adaptive)
9884                    (not gnus-newsgroup-auto-expire)
9885                    (not gnus-suppress-duplicates)
9886                    (or (not gnus-use-cache)
9887                        (eq gnus-use-cache 'passive)))
9888               (progn
9889                 (when all
9890                   (setq gnus-newsgroup-marked nil
9891                         gnus-newsgroup-spam-marked nil
9892                         gnus-newsgroup-dormant nil))
9893                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9894             ;; We actually mark all articles as canceled, which we
9895             ;; have to do when using auto-expiry or adaptive scoring.
9896             (gnus-summary-show-all-threads)
9897             (if (and to-here reverse)
9898                 (progn
9899                   (goto-char to-here)
9900                   (while (and
9901                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9902                           (gnus-summary-find-next (not all) nil nil t))))
9903               (when (gnus-summary-first-subject (not all) t)
9904                 (while (and
9905                         (if to-here (< (point) to-here) t)
9906                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9907                         (gnus-summary-find-next (not all) nil nil t)))))
9908             (gnus-set-mode-line 'summary))
9909           t))
9910     (gnus-summary-position-point)))
9911
9912 (defun gnus-summary-catchup-to-here (&optional all)
9913   "Mark all unticked articles before the current one as read.
9914 If ALL is non-nil, also mark ticked and dormant articles as read."
9915   (interactive "P")
9916   (save-excursion
9917     (gnus-save-hidden-threads
9918       (let ((beg (point)))
9919         ;; We check that there are unread articles.
9920         (when (or all (gnus-summary-find-prev))
9921           (gnus-summary-catchup all t beg)))))
9922   (gnus-summary-position-point))
9923
9924 (defun gnus-summary-catchup-from-here (&optional all)
9925   "Mark all unticked articles after the current one as read.
9926 If ALL is non-nil, also mark ticked and dormant articles as read."
9927   (interactive "P")
9928   (save-excursion
9929     (gnus-save-hidden-threads
9930       (let ((beg (point)))
9931         ;; We check that there are unread articles.
9932         (when (or all (gnus-summary-find-next))
9933           (gnus-summary-catchup all t beg nil t)))))
9934   (gnus-summary-position-point))
9935
9936 (defun gnus-summary-catchup-all (&optional quietly)
9937   "Mark all articles in this newsgroup as read."
9938   (interactive "P")
9939   (gnus-summary-catchup t quietly))
9940
9941 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9942   "Mark all unread articles in this group as read, then exit.
9943 If prefix argument ALL is non-nil, all articles are marked as read.
9944 If QUIETLY is non-nil, no questions will be asked."
9945   (interactive "P")
9946   (when (gnus-summary-catchup all quietly nil 'fast)
9947     ;; Select next newsgroup or exit.
9948     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9949              (eq gnus-auto-select-next 'quietly))
9950         (gnus-summary-next-group nil)
9951       (gnus-summary-exit))))
9952
9953 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9954   "Mark all articles in this newsgroup as read, and then exit."
9955   (interactive "P")
9956   (gnus-summary-catchup-and-exit t quietly))
9957
9958 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9959   "Mark all articles in this group as read and select the next group.
9960 If given a prefix, mark all articles, unread as well as ticked, as
9961 read."
9962   (interactive "P")
9963   (save-excursion
9964     (gnus-summary-catchup all))
9965   (gnus-summary-next-group))
9966
9967 ;;;
9968 ;;; with article
9969 ;;;
9970
9971 (defmacro gnus-with-article (article &rest forms)
9972   "Select ARTICLE and perform FORMS in the original article buffer.
9973 Then replace the article with the result."
9974   `(progn
9975      ;; We don't want the article to be marked as read.
9976      (let (gnus-mark-article-hook)
9977        (gnus-summary-select-article t t nil ,article))
9978      (set-buffer gnus-original-article-buffer)
9979      ,@forms
9980      (if (not (gnus-check-backend-function
9981                'request-replace-article (car gnus-article-current)))
9982          (gnus-message 5 "Read-only group; not replacing")
9983        (unless (gnus-request-replace-article
9984                 ,article (car gnus-article-current)
9985                 (current-buffer) t)
9986          (error "Couldn't replace article")))
9987      ;; The cache and backlog have to be flushed somewhat.
9988      (when gnus-keep-backlog
9989        (gnus-backlog-remove-article
9990         (car gnus-article-current) (cdr gnus-article-current)))
9991      (when gnus-use-cache
9992        (gnus-cache-update-article
9993         (car gnus-article-current) (cdr gnus-article-current)))))
9994
9995 (put 'gnus-with-article 'lisp-indent-function 1)
9996 (put 'gnus-with-article 'edebug-form-spec '(form body))
9997
9998 ;; Thread-based commands.
9999
10000 (defun gnus-summary-articles-in-thread (&optional article)
10001   "Return a list of all articles in the current thread.
10002 If ARTICLE is non-nil, return all articles in the thread that starts
10003 with that article."
10004   (let* ((article (or article (gnus-summary-article-number)))
10005          (data (gnus-data-find-list article))
10006          (top-level (gnus-data-level (car data)))
10007          (top-subject
10008           (cond ((null gnus-thread-operation-ignore-subject)
10009                  (gnus-simplify-subject-re
10010                   (mail-header-subject (gnus-data-header (car data)))))
10011                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10012                  (gnus-simplify-subject-fuzzy
10013                   (mail-header-subject (gnus-data-header (car data)))))
10014                 (t nil)))
10015          (end-point (save-excursion
10016                       (if (gnus-summary-go-to-next-thread)
10017                           (point) (point-max))))
10018          articles)
10019     (while (and data
10020                 (< (gnus-data-pos (car data)) end-point))
10021       (when (or (not top-subject)
10022                 (string= top-subject
10023                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10024                              (gnus-simplify-subject-fuzzy
10025                               (mail-header-subject
10026                                (gnus-data-header (car data))))
10027                            (gnus-simplify-subject-re
10028                             (mail-header-subject
10029                              (gnus-data-header (car data)))))))
10030         (push (gnus-data-number (car data)) articles))
10031       (unless (and (setq data (cdr data))
10032                    (> (gnus-data-level (car data)) top-level))
10033         (setq data nil)))
10034     ;; Return the list of articles.
10035     (nreverse articles)))
10036
10037 (defun gnus-summary-rethread-current ()
10038   "Rethread the thread the current article is part of."
10039   (interactive)
10040   (let* ((gnus-show-threads t)
10041          (article (gnus-summary-article-number))
10042          (id (mail-header-id (gnus-summary-article-header)))
10043          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10044     (unless id
10045       (error "No article on the current line"))
10046     (gnus-rebuild-thread id)
10047     (gnus-summary-goto-subject article)))
10048
10049 (defun gnus-summary-reparent-thread ()
10050   "Make the current article child of the marked (or previous) article.
10051
10052 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10053 is non-nil or the Subject: of both articles are the same."
10054   (interactive)
10055   (unless (not (gnus-group-read-only-p))
10056     (error "The current newsgroup does not support article editing"))
10057   (unless (<= (length gnus-newsgroup-processable) 1)
10058     (error "No more than one article may be marked"))
10059   (save-window-excursion
10060     (let ((gnus-article-buffer " *reparent*")
10061           (current-article (gnus-summary-article-number))
10062           ;; First grab the marked article, otherwise one line up.
10063           (parent-article (if (not (null gnus-newsgroup-processable))
10064                               (car gnus-newsgroup-processable)
10065                             (save-excursion
10066                               (if (eq (forward-line -1) 0)
10067                                   (gnus-summary-article-number)
10068                                 (error "Beginning of summary buffer"))))))
10069       (unless (not (eq current-article parent-article))
10070         (error "An article may not be self-referential"))
10071       (let ((message-id (mail-header-id
10072                          (gnus-summary-article-header parent-article))))
10073         (unless (and message-id (not (equal message-id "")))
10074           (error "No message-id in desired parent"))
10075         (gnus-with-article current-article
10076           (save-restriction
10077             (goto-char (point-min))
10078             (message-narrow-to-head)
10079             (if (re-search-forward "^References: " nil t)
10080                 (progn
10081                   (re-search-forward "^[^ \t]" nil t)
10082                   (forward-line -1)
10083                   (end-of-line)
10084                   (insert " " message-id))
10085               (insert "References: " message-id "\n"))))
10086         (set-buffer gnus-summary-buffer)
10087         (gnus-summary-unmark-all-processable)
10088         (gnus-summary-update-article current-article)
10089         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10090             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10091         (gnus-summary-rethread-current)
10092         (gnus-message 3 "Article %d is now the child of article %d"
10093                       current-article parent-article)))))
10094
10095 (defun gnus-summary-toggle-threads (&optional arg)
10096   "Toggle showing conversation threads.
10097 If ARG is positive number, turn showing conversation threads on."
10098   (interactive "P")
10099   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10100     (setq gnus-show-threads
10101           (if (null arg) (not gnus-show-threads)
10102             (> (prefix-numeric-value arg) 0)))
10103     (gnus-summary-prepare)
10104     (gnus-summary-goto-subject current)
10105     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10106     (gnus-summary-position-point)))
10107
10108 (defun gnus-summary-show-all-threads ()
10109   "Show all threads."
10110   (interactive)
10111   (save-excursion
10112     (let ((buffer-read-only nil))
10113       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10114   (gnus-summary-position-point))
10115
10116 (defun gnus-summary-show-thread ()
10117   "Show thread subtrees.
10118 Returns nil if no thread was there to be shown."
10119   (interactive)
10120   (let ((buffer-read-only nil)
10121         (orig (point))
10122         ;; first goto end then to beg, to have point at beg after let
10123         (end (progn (end-of-line) (point)))
10124         (beg (progn (beginning-of-line) (point))))
10125     (prog1
10126         ;; Any hidden lines here?
10127         (search-forward "\r" end t)
10128       (subst-char-in-region beg end ?\^M ?\n t)
10129       (goto-char orig)
10130       (gnus-summary-position-point))))
10131
10132 (defun gnus-summary-maybe-hide-threads ()
10133   "If requested, hide the threads that should be hidden."
10134   (when (and gnus-show-threads
10135              gnus-thread-hide-subtree)
10136     (gnus-summary-hide-all-threads
10137      (if (or (consp gnus-thread-hide-subtree)
10138              (gnus-functionp gnus-thread-hide-subtree))
10139          (gnus-make-predicate gnus-thread-hide-subtree)
10140        nil))))
10141
10142 ;;; Hiding predicates.
10143
10144 (defun gnus-article-unread-p (header)
10145   (memq (mail-header-number header) gnus-newsgroup-unreads))
10146
10147 (defun gnus-article-unseen-p (header)
10148   (memq (mail-header-number header) gnus-newsgroup-unseen))
10149
10150 (defun gnus-map-articles (predicate articles)
10151   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10152   (apply 'gnus-or (mapcar predicate
10153                           (mapcar 'gnus-summary-article-header articles))))
10154
10155 (defun gnus-summary-hide-all-threads (&optional predicate)
10156   "Hide all thread subtrees.
10157 If PREDICATE is supplied, threads that satisfy this predicate
10158 will not be hidden."
10159   (interactive)
10160   (save-excursion
10161     (goto-char (point-min))
10162     (let ((end nil))
10163       (while (not end)
10164         (when (or (not predicate)
10165                   (gnus-map-articles
10166                    predicate (gnus-summary-article-children)))
10167             (gnus-summary-hide-thread))
10168         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10169   (gnus-summary-position-point))
10170
10171 (defun gnus-summary-hide-thread ()
10172   "Hide thread subtrees.
10173 If PREDICATE is supplied, threads that satisfy this predicate
10174 will not be hidden.
10175 Returns nil if no threads were there to be hidden."
10176   (interactive)
10177   (let ((buffer-read-only nil)
10178         (start (point))
10179         (article (gnus-summary-article-number)))
10180     (goto-char start)
10181     ;; Go forward until either the buffer ends or the subthread
10182     ;; ends.
10183     (when (and (not (eobp))
10184                (or (zerop (gnus-summary-next-thread 1 t))
10185                    (goto-char (point-max))))
10186       (prog1
10187           (if (and (> (point) start)
10188                    (search-backward "\n" start t))
10189               (progn
10190                 (subst-char-in-region start (point) ?\n ?\^M)
10191                 (gnus-summary-goto-subject article))
10192             (goto-char start)
10193             nil)))))
10194
10195 (defun gnus-summary-go-to-next-thread (&optional previous)
10196   "Go to the same level (or less) next thread.
10197 If PREVIOUS is non-nil, go to previous thread instead.
10198 Return the article number moved to, or nil if moving was impossible."
10199   (let ((level (gnus-summary-thread-level))
10200         (way (if previous -1 1))
10201         (beg (point)))
10202     (forward-line way)
10203     (while (and (not (eobp))
10204                 (< level (gnus-summary-thread-level)))
10205       (forward-line way))
10206     (if (eobp)
10207         (progn
10208           (goto-char beg)
10209           nil)
10210       (setq beg (point))
10211       (prog1
10212           (gnus-summary-article-number)
10213         (goto-char beg)))))
10214
10215 (defun gnus-summary-next-thread (n &optional silent)
10216   "Go to the same level next N'th thread.
10217 If N is negative, search backward instead.
10218 Returns the difference between N and the number of skips actually
10219 done.
10220
10221 If SILENT, don't output messages."
10222   (interactive "p")
10223   (let ((backward (< n 0))
10224         (n (abs n)))
10225     (while (and (> n 0)
10226                 (gnus-summary-go-to-next-thread backward))
10227       (decf n))
10228     (unless silent
10229       (gnus-summary-position-point))
10230     (when (and (not silent) (/= 0 n))
10231       (gnus-message 7 "No more threads"))
10232     n))
10233
10234 (defun gnus-summary-prev-thread (n)
10235   "Go to the same level previous N'th thread.
10236 Returns the difference between N and the number of skips actually
10237 done."
10238   (interactive "p")
10239   (gnus-summary-next-thread (- n)))
10240
10241 (defun gnus-summary-go-down-thread ()
10242   "Go down one level in the current thread."
10243   (let ((children (gnus-summary-article-children)))
10244     (when children
10245       (gnus-summary-goto-subject (car children)))))
10246
10247 (defun gnus-summary-go-up-thread ()
10248   "Go up one level in the current thread."
10249   (let ((parent (gnus-summary-article-parent)))
10250     (when parent
10251       (gnus-summary-goto-subject parent))))
10252
10253 (defun gnus-summary-down-thread (n)
10254   "Go down thread N steps.
10255 If N is negative, go up instead.
10256 Returns the difference between N and how many steps down that were
10257 taken."
10258   (interactive "p")
10259   (let ((up (< n 0))
10260         (n (abs n)))
10261     (while (and (> n 0)
10262                 (if up (gnus-summary-go-up-thread)
10263                   (gnus-summary-go-down-thread)))
10264       (setq n (1- n)))
10265     (gnus-summary-position-point)
10266     (when (/= 0 n)
10267       (gnus-message 7 "Can't go further"))
10268     n))
10269
10270 (defun gnus-summary-up-thread (n)
10271   "Go up thread N steps.
10272 If N is negative, go down instead.
10273 Returns the difference between N and how many steps down that were
10274 taken."
10275   (interactive "p")
10276   (gnus-summary-down-thread (- n)))
10277
10278 (defun gnus-summary-top-thread ()
10279   "Go to the top of the thread."
10280   (interactive)
10281   (while (gnus-summary-go-up-thread))
10282   (gnus-summary-article-number))
10283
10284 (defun gnus-summary-kill-thread (&optional unmark)
10285   "Mark articles under current thread as read.
10286 If the prefix argument is positive, remove any kinds of marks.
10287 If the prefix argument is negative, tick articles instead."
10288   (interactive "P")
10289   (when unmark
10290     (setq unmark (prefix-numeric-value unmark)))
10291   (let ((articles (gnus-summary-articles-in-thread)))
10292     (save-excursion
10293       ;; Expand the thread.
10294       (gnus-summary-show-thread)
10295       ;; Mark all the articles.
10296       (while articles
10297         (gnus-summary-goto-subject (car articles))
10298         (cond ((null unmark)
10299                (gnus-summary-mark-article-as-read gnus-killed-mark))
10300               ((> unmark 0)
10301                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10302               (t
10303                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10304         (setq articles (cdr articles))))
10305     ;; Hide killed subtrees.
10306     (and (null unmark)
10307          gnus-thread-hide-killed
10308          (gnus-summary-hide-thread))
10309     ;; If marked as read, go to next unread subject.
10310     (when (null unmark)
10311       ;; Go to next unread subject.
10312       (gnus-summary-next-subject 1 t)))
10313   (gnus-set-mode-line 'summary))
10314
10315 ;; Summary sorting commands
10316
10317 (defun gnus-summary-sort-by-number (&optional reverse)
10318   "Sort the summary buffer by article number.
10319 Argument REVERSE means reverse order."
10320   (interactive "P")
10321   (gnus-summary-sort 'number reverse))
10322
10323 (defun gnus-summary-sort-by-random (&optional reverse)
10324   "Randomize the order in the summary buffer.
10325 Argument REVERSE means to randomize in reverse order."
10326   (interactive "P")
10327   (gnus-summary-sort 'random reverse))
10328
10329 (defun gnus-summary-sort-by-author (&optional reverse)
10330   "Sort the summary buffer by author name alphabetically.
10331 If `case-fold-search' is non-nil, case of letters is ignored.
10332 Argument REVERSE means reverse order."
10333   (interactive "P")
10334   (gnus-summary-sort 'author reverse))
10335
10336 (defun gnus-summary-sort-by-subject (&optional reverse)
10337   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10338 If `case-fold-search' is non-nil, case of letters is ignored.
10339 Argument REVERSE means reverse order."
10340   (interactive "P")
10341   (gnus-summary-sort 'subject reverse))
10342
10343 (defun gnus-summary-sort-by-date (&optional reverse)
10344   "Sort the summary buffer by date.
10345 Argument REVERSE means reverse order."
10346   (interactive "P")
10347   (gnus-summary-sort 'date reverse))
10348
10349 (defun gnus-summary-sort-by-score (&optional reverse)
10350   "Sort the summary buffer by score.
10351 Argument REVERSE means reverse order."
10352   (interactive "P")
10353   (gnus-summary-sort 'score reverse))
10354
10355 (defun gnus-summary-sort-by-lines (&optional reverse)
10356   "Sort the summary buffer by the number of lines.
10357 Argument REVERSE means reverse order."
10358   (interactive "P")
10359   (gnus-summary-sort 'lines reverse))
10360
10361 (defun gnus-summary-sort-by-chars (&optional reverse)
10362   "Sort the summary buffer by article length.
10363 Argument REVERSE means reverse order."
10364   (interactive "P")
10365   (gnus-summary-sort 'chars reverse))
10366
10367 (defun gnus-summary-sort-by-original (&optional reverse)
10368   "Sort the summary buffer using the default sorting method.
10369 Argument REVERSE means reverse order."
10370   (interactive "P")
10371   (let* ((buffer-read-only)
10372          (gnus-summary-prepare-hook nil))
10373     ;; We do the sorting by regenerating the threads.
10374     (gnus-summary-prepare)
10375     ;; Hide subthreads if needed.
10376     (gnus-summary-maybe-hide-threads)))
10377
10378 (defun gnus-summary-sort (predicate reverse)
10379   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10380   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10381          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10382          (gnus-thread-sort-functions
10383           (if (not reverse)
10384               thread
10385             `(lambda (t1 t2)
10386                (,thread t2 t1))))
10387          (gnus-sort-gathered-threads-function
10388           gnus-thread-sort-functions)
10389          (gnus-article-sort-functions
10390           (if (not reverse)
10391               article
10392             `(lambda (t1 t2)
10393                (,article t2 t1))))
10394          (buffer-read-only)
10395          (gnus-summary-prepare-hook nil))
10396     ;; We do the sorting by regenerating the threads.
10397     (gnus-summary-prepare)
10398     ;; Hide subthreads if needed.
10399     (gnus-summary-maybe-hide-threads)))
10400
10401 ;; Summary saving commands.
10402
10403 (defun gnus-summary-save-article (&optional n not-saved)
10404   "Save the current article using the default saver function.
10405 If N is a positive number, save the N next articles.
10406 If N is a negative number, save the N previous articles.
10407 If N is nil and any articles have been marked with the process mark,
10408 save those articles instead.
10409 The variable `gnus-default-article-saver' specifies the saver function."
10410   (interactive "P")
10411   (let* ((articles (gnus-summary-work-articles n))
10412          (save-buffer (save-excursion
10413                         (nnheader-set-temp-buffer " *Gnus Save*")))
10414          (num (length articles))
10415          header file)
10416     (dolist (article articles)
10417       (setq header (gnus-summary-article-header article))
10418       (if (not (vectorp header))
10419           ;; This is a pseudo-article.
10420           (if (assq 'name header)
10421               (gnus-copy-file (cdr (assq 'name header)))
10422             (gnus-message 1 "Article %d is unsaveable" article))
10423         ;; This is a real article.
10424         (save-window-excursion
10425           (let ((gnus-display-mime-function nil)
10426                 (gnus-article-prepare-hook nil))
10427             (gnus-summary-select-article t nil nil article)))
10428         (save-excursion
10429           (set-buffer save-buffer)
10430           (erase-buffer)
10431           (insert-buffer-substring gnus-original-article-buffer))
10432         (setq file (gnus-article-save save-buffer file num))
10433         (gnus-summary-remove-process-mark article)
10434         (unless not-saved
10435           (gnus-summary-set-saved-mark article))))
10436     (gnus-kill-buffer save-buffer)
10437     (gnus-summary-position-point)
10438     (gnus-set-mode-line 'summary)
10439     n))
10440
10441 (defun gnus-summary-pipe-output (&optional arg)
10442   "Pipe the current article to a subprocess.
10443 If N is a positive number, pipe the N next articles.
10444 If N is a negative number, pipe the N previous articles.
10445 If N is nil and any articles have been marked with the process mark,
10446 pipe those articles instead."
10447   (interactive "P")
10448   (require 'gnus-art)
10449   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10450     (gnus-summary-save-article arg t))
10451   (let ((buffer (get-buffer "*Shell Command Output*")))
10452     (if (and buffer
10453              (with-current-buffer buffer (> (point-max) (point-min))))
10454         (gnus-configure-windows 'pipe))))
10455
10456 (defun gnus-summary-save-article-mail (&optional arg)
10457   "Append the current article to an mail file.
10458 If N is a positive number, save the N next articles.
10459 If N is a negative number, save the N previous articles.
10460 If N is nil and any articles have been marked with the process mark,
10461 save those articles instead."
10462   (interactive "P")
10463   (require 'gnus-art)
10464   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10465     (gnus-summary-save-article arg)))
10466
10467 (defun gnus-summary-save-article-rmail (&optional arg)
10468   "Append the current article to an rmail file.
10469 If N is a positive number, save the N next articles.
10470 If N is a negative number, save the N previous articles.
10471 If N is nil and any articles have been marked with the process mark,
10472 save those articles instead."
10473   (interactive "P")
10474   (require 'gnus-art)
10475   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10476     (gnus-summary-save-article arg)))
10477
10478 (defun gnus-summary-save-article-file (&optional arg)
10479   "Append the current article to a file.
10480 If N is a positive number, save the N next articles.
10481 If N is a negative number, save the N previous articles.
10482 If N is nil and any articles have been marked with the process mark,
10483 save those articles instead."
10484   (interactive "P")
10485   (require 'gnus-art)
10486   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10487     (gnus-summary-save-article arg)))
10488
10489 (defun gnus-summary-write-article-file (&optional arg)
10490   "Write the current article to a file, deleting the previous file.
10491 If N is a positive number, save the N next articles.
10492 If N is a negative number, save the N previous articles.
10493 If N is nil and any articles have been marked with the process mark,
10494 save those articles instead."
10495   (interactive "P")
10496   (require 'gnus-art)
10497   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10498     (gnus-summary-save-article arg)))
10499
10500 (defun gnus-summary-save-article-body-file (&optional arg)
10501   "Append the current article body to a file.
10502 If N is a positive number, save the N next articles.
10503 If N is a negative number, save the N previous articles.
10504 If N is nil and any articles have been marked with the process mark,
10505 save those articles instead."
10506   (interactive "P")
10507   (require 'gnus-art)
10508   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10509     (gnus-summary-save-article arg)))
10510
10511 (defun gnus-summary-muttprint (&optional arg)
10512   "Print the current article using Muttprint.
10513 If N is a positive number, save the N next articles.
10514 If N is a negative number, save the N previous articles.
10515 If N is nil and any articles have been marked with the process mark,
10516 save those articles instead."
10517   (interactive "P")
10518   (require 'gnus-art)
10519   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10520     (gnus-summary-save-article arg t)))
10521
10522 (defun gnus-summary-pipe-message (program)
10523   "Pipe the current article through PROGRAM."
10524   (interactive "sProgram: ")
10525   (gnus-summary-select-article)
10526   (let ((mail-header-separator ""))
10527     (gnus-eval-in-buffer-window gnus-article-buffer
10528       (save-restriction
10529         (widen)
10530         (let ((start (window-start))
10531               buffer-read-only)
10532           (message-pipe-buffer-body program)
10533           (set-window-start (get-buffer-window (current-buffer)) start))))))
10534
10535 (defun gnus-get-split-value (methods)
10536   "Return a value based on the split METHODS."
10537   (let (split-name method result match)
10538     (when methods
10539       (save-excursion
10540         (set-buffer gnus-original-article-buffer)
10541         (save-restriction
10542           (nnheader-narrow-to-headers)
10543           (while (and methods (not split-name))
10544             (goto-char (point-min))
10545             (setq method (pop methods))
10546             (setq match (car method))
10547             (when (cond
10548                    ((stringp match)
10549                     ;; Regular expression.
10550                     (ignore-errors
10551                       (re-search-forward match nil t)))
10552                    ((gnus-functionp match)
10553                     ;; Function.
10554                     (save-restriction
10555                       (widen)
10556                       (setq result (funcall match gnus-newsgroup-name))))
10557                    ((consp match)
10558                     ;; Form.
10559                     (save-restriction
10560                       (widen)
10561                       (setq result (eval match)))))
10562               (setq split-name (cdr method))
10563               (cond ((stringp result)
10564                      (push (expand-file-name
10565                             result gnus-article-save-directory)
10566                            split-name))
10567                     ((consp result)
10568                      (setq split-name (append result split-name)))))))))
10569     (nreverse split-name)))
10570
10571 (defun gnus-valid-move-group-p (group)
10572   (and (boundp group)
10573        (symbol-name group)
10574        (symbol-value group)
10575        (gnus-get-function (gnus-find-method-for-group
10576                            (symbol-name group)) 'request-accept-article t)))
10577
10578 (defun gnus-read-move-group-name (prompt default articles prefix)
10579   "Read a group name."
10580   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10581          (minibuffer-confirm-incomplete nil) ; XEmacs
10582          (prom
10583           (format "%s %s to:"
10584                   prompt
10585                   (if (> (length articles) 1)
10586                       (format "these %d articles" (length articles))
10587                     "this article")))
10588          (to-newsgroup
10589           (cond
10590            ((null split-name)
10591             (gnus-completing-read-with-default
10592              default prom
10593              gnus-active-hashtb
10594              'gnus-valid-move-group-p
10595              nil prefix
10596              'gnus-group-history))
10597            ((= 1 (length split-name))
10598             (gnus-completing-read-with-default
10599              (car split-name) prom
10600              gnus-active-hashtb
10601              'gnus-valid-move-group-p
10602              nil nil
10603              'gnus-group-history))
10604            (t
10605             (gnus-completing-read-with-default
10606              nil prom
10607              (mapcar (lambda (el) (list el))
10608                      (nreverse split-name))
10609              nil nil nil
10610              'gnus-group-history))))
10611          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10612     (when to-newsgroup
10613       (if (or (string= to-newsgroup "")
10614               (string= to-newsgroup prefix))
10615           (setq to-newsgroup default))
10616       (unless to-newsgroup
10617         (error "No group name entered"))
10618       (or (gnus-active to-newsgroup)
10619           (gnus-activate-group to-newsgroup nil nil to-method)
10620           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10621                                      to-newsgroup))
10622               (or (and (gnus-request-create-group to-newsgroup to-method)
10623                        (gnus-activate-group
10624                         to-newsgroup nil nil to-method)
10625                        (gnus-subscribe-group to-newsgroup))
10626                   (error "Couldn't create group %s" to-newsgroup)))
10627           (error "No such group: %s" to-newsgroup)))
10628     to-newsgroup))
10629
10630 (defun gnus-summary-save-parts (type dir n &optional reverse)
10631   "Save parts matching TYPE to DIR.
10632 If REVERSE, save parts that do not match TYPE."
10633   (interactive
10634    (list (read-string "Save parts of type: "
10635                       (or (car gnus-summary-save-parts-type-history)
10636                           gnus-summary-save-parts-default-mime)
10637                       'gnus-summary-save-parts-type-history)
10638          (setq gnus-summary-save-parts-last-directory
10639                (read-file-name "Save to directory: "
10640                                gnus-summary-save-parts-last-directory
10641                                nil t))
10642          current-prefix-arg))
10643   (gnus-summary-iterate n
10644     (let ((gnus-display-mime-function nil)
10645           (gnus-inhibit-treatment t))
10646       (gnus-summary-select-article))
10647     (save-excursion
10648       (set-buffer gnus-article-buffer)
10649       (let ((handles (or gnus-article-mime-handles
10650                          (mm-dissect-buffer nil gnus-article-loose-mime)
10651                          (mm-uu-dissect))))
10652         (when handles
10653           (gnus-summary-save-parts-1 type dir handles reverse)
10654           (unless gnus-article-mime-handles ;; Don't destroy this case.
10655             (mm-destroy-parts handles)))))))
10656
10657 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10658   (if (stringp (car handle))
10659       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10660               (cdr handle))
10661     (when (if reverse
10662               (not (string-match type (mm-handle-media-type handle)))
10663             (string-match type (mm-handle-media-type handle)))
10664       (let ((file (expand-file-name
10665                    (file-name-nondirectory
10666                     (or
10667                      (mail-content-type-get
10668                       (mm-handle-disposition handle) 'filename)
10669                      (concat gnus-newsgroup-name
10670                              "." (number-to-string
10671                                   (cdr gnus-article-current)))))
10672                    dir)))
10673         (unless (file-exists-p file)
10674           (mm-save-part-to-file handle file))))))
10675
10676 ;; Summary extract commands
10677
10678 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10679   (let ((buffer-read-only nil)
10680         (article (gnus-summary-article-number))
10681         after-article b e)
10682     (unless (gnus-summary-goto-subject article)
10683       (error "No such article: %d" article))
10684     (gnus-summary-position-point)
10685     ;; If all commands are to be bunched up on one line, we collect
10686     ;; them here.
10687     (unless gnus-view-pseudos-separately
10688       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10689             files action)
10690         (while ps
10691           (setq action (cdr (assq 'action (car ps))))
10692           (setq files (list (cdr (assq 'name (car ps)))))
10693           (while (and ps (cdr ps)
10694                       (string= (or action "1")
10695                                (or (cdr (assq 'action (cadr ps))) "2")))
10696             (push (cdr (assq 'name (cadr ps))) files)
10697             (setcdr ps (cddr ps)))
10698           (when files
10699             (when (not (string-match "%s" action))
10700               (push " " files))
10701             (push " " files)
10702             (when (assq 'execute (car ps))
10703               (setcdr (assq 'execute (car ps))
10704                       (funcall (if (string-match "%s" action)
10705                                    'format 'concat)
10706                                action
10707                                (mapconcat
10708                                 (lambda (f)
10709                                   (if (equal f " ")
10710                                       f
10711                                     (gnus-quote-arg-for-sh-or-csh f)))
10712                                 files " ")))))
10713           (setq ps (cdr ps)))))
10714     (if (and gnus-view-pseudos (not not-view))
10715         (while pslist
10716           (when (assq 'execute (car pslist))
10717             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10718                                   (eq gnus-view-pseudos 'not-confirm)))
10719           (setq pslist (cdr pslist)))
10720       (save-excursion
10721         (while pslist
10722           (setq after-article (or (cdr (assq 'article (car pslist)))
10723                                   (gnus-summary-article-number)))
10724           (gnus-summary-goto-subject after-article)
10725           (forward-line 1)
10726           (setq b (point))
10727           (insert "    " (file-name-nondirectory
10728                           (cdr (assq 'name (car pslist))))
10729                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10730           (setq e (point))
10731           (forward-line -1)             ; back to `b'
10732           (gnus-add-text-properties
10733            b (1- e) (list 'gnus-number gnus-reffed-article-number
10734                           gnus-mouse-face-prop gnus-mouse-face))
10735           (gnus-data-enter
10736            after-article gnus-reffed-article-number
10737            gnus-unread-mark b (car pslist) 0 (- e b))
10738           (setq gnus-newsgroup-unreads
10739                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10740                                          gnus-reffed-article-number))
10741           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10742           (setq pslist (cdr pslist)))))))
10743
10744 (defun gnus-pseudos< (p1 p2)
10745   (let ((c1 (cdr (assq 'action p1)))
10746         (c2 (cdr (assq 'action p2))))
10747     (and c1 c2 (string< c1 c2))))
10748
10749 (defun gnus-request-pseudo-article (props)
10750   (cond ((assq 'execute props)
10751          (gnus-execute-command (cdr (assq 'execute props)))))
10752   (let ((gnus-current-article (gnus-summary-article-number)))
10753     (gnus-run-hooks 'gnus-mark-article-hook)))
10754
10755 (defun gnus-execute-command (command &optional automatic)
10756   (save-excursion
10757     (gnus-article-setup-buffer)
10758     (set-buffer gnus-article-buffer)
10759     (setq buffer-read-only nil)
10760     (let ((command (if automatic command
10761                      (read-string "Command: " (cons command 0)))))
10762       (erase-buffer)
10763       (insert "$ " command "\n\n")
10764       (if gnus-view-pseudo-asynchronously
10765           (start-process "gnus-execute" (current-buffer) shell-file-name
10766                          shell-command-switch command)
10767         (call-process shell-file-name nil t nil
10768                       shell-command-switch command)))))
10769
10770 ;; Summary kill commands.
10771
10772 (defun gnus-summary-edit-global-kill (article)
10773   "Edit the \"global\" kill file."
10774   (interactive (list (gnus-summary-article-number)))
10775   (gnus-group-edit-global-kill article))
10776
10777 (defun gnus-summary-edit-local-kill ()
10778   "Edit a local kill file applied to the current newsgroup."
10779   (interactive)
10780   (setq gnus-current-headers (gnus-summary-article-header))
10781   (gnus-group-edit-local-kill
10782    (gnus-summary-article-number) gnus-newsgroup-name))
10783
10784 ;;; Header reading.
10785
10786 (defun gnus-read-header (id &optional header)
10787   "Read the headers of article ID and enter them into the Gnus system."
10788   (let ((group gnus-newsgroup-name)
10789         (gnus-override-method
10790          (or
10791           gnus-override-method
10792           (and (gnus-news-group-p gnus-newsgroup-name)
10793                (car (gnus-refer-article-methods)))))
10794         where)
10795     ;; First we check to see whether the header in question is already
10796     ;; fetched.
10797     (if (stringp id)
10798         ;; This is a Message-ID.
10799         (setq header (or header (gnus-id-to-header id)))
10800       ;; This is an article number.
10801       (setq header (or header (gnus-summary-article-header id))))
10802     (if (and header
10803              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10804         ;; We have found the header.
10805         header
10806       ;; If this is a sparse article, we have to nix out its
10807       ;; previous entry in the thread hashtb.
10808       (when (and header
10809                  (gnus-summary-article-sparse-p (mail-header-number header)))
10810         (let* ((parent (gnus-parent-id (mail-header-references header)))
10811                (thread (and parent (gnus-id-to-thread parent))))
10812           (when thread
10813             (delq (assq header thread) thread))))
10814       ;; We have to really fetch the header to this article.
10815       (save-excursion
10816         (set-buffer nntp-server-buffer)
10817         (when (setq where (gnus-request-head id group))
10818           (nnheader-fold-continuation-lines)
10819           (goto-char (point-max))
10820           (insert ".\n")
10821           (goto-char (point-min))
10822           (insert "211 ")
10823           (princ (cond
10824                   ((numberp id) id)
10825                   ((cdr where) (cdr where))
10826                   (header (mail-header-number header))
10827                   (t gnus-reffed-article-number))
10828                  (current-buffer))
10829           (insert " Article retrieved.\n"))
10830         (if (or (not where)
10831                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10832             ()                          ; Malformed head.
10833           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10834             (when (and (stringp id)
10835                        (not (string= (gnus-group-real-name group)
10836                                      (car where))))
10837               ;; If we fetched by Message-ID and the article came
10838               ;; from a different group, we fudge some bogus article
10839               ;; numbers for this article.
10840               (mail-header-set-number header gnus-reffed-article-number))
10841             (save-excursion
10842               (set-buffer gnus-summary-buffer)
10843               (decf gnus-reffed-article-number)
10844               (gnus-remove-header (mail-header-number header))
10845               (push header gnus-newsgroup-headers)
10846               (setq gnus-current-headers header)
10847               (push (mail-header-number header) gnus-newsgroup-limit)))
10848           header)))))
10849
10850 (defun gnus-remove-header (number)
10851   "Remove header NUMBER from `gnus-newsgroup-headers'."
10852   (if (and gnus-newsgroup-headers
10853            (= number (mail-header-number (car gnus-newsgroup-headers))))
10854       (pop gnus-newsgroup-headers)
10855     (let ((headers gnus-newsgroup-headers))
10856       (while (and (cdr headers)
10857                   (not (= number (mail-header-number (cadr headers)))))
10858         (pop headers))
10859       (when (cdr headers)
10860         (setcdr headers (cddr headers))))))
10861
10862 ;;;
10863 ;;; summary highlights
10864 ;;;
10865
10866 (defun gnus-highlight-selected-summary ()
10867   "Highlight selected article in summary buffer."
10868   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10869   (when gnus-summary-selected-face
10870     (save-excursion
10871       (let* ((beg (progn (beginning-of-line) (point)))
10872              (end (progn (end-of-line) (point)))
10873              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10874              (from (if (get-text-property beg gnus-mouse-face-prop)
10875                        beg
10876                      (or (next-single-property-change
10877                           beg gnus-mouse-face-prop nil end)
10878                          beg)))
10879              (to
10880               (if (= from end)
10881                   (- from 2)
10882                 (or (next-single-property-change
10883                      from gnus-mouse-face-prop nil end)
10884                     end))))
10885         ;; If no mouse-face prop on line we will have to = from = end,
10886         ;; so we highlight the entire line instead.
10887         (when (= (+ to 2) from)
10888           (setq from beg)
10889           (setq to end))
10890         (if gnus-newsgroup-selected-overlay
10891             ;; Move old overlay.
10892             (gnus-move-overlay
10893              gnus-newsgroup-selected-overlay from to (current-buffer))
10894           ;; Create new overlay.
10895           (gnus-overlay-put
10896            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10897            'face gnus-summary-selected-face))))))
10898
10899 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10900 (defun gnus-summary-highlight-line ()
10901   "Highlight current line according to `gnus-summary-highlight'."
10902   (let* ((list gnus-summary-highlight)
10903          (beg (gnus-point-at-bol))
10904          (article (gnus-summary-article-number))
10905          (score (or (cdr (assq (or article gnus-current-article)
10906                                gnus-newsgroup-scored))
10907                     gnus-summary-default-score 0))
10908          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10909          (inhibit-read-only t)
10910          (default gnus-summary-default-score)
10911          (default-high gnus-summary-default-high-score)
10912          (default-low gnus-summary-default-low-score))
10913     ;; Eval the cars of the lists until we find a match.
10914     (while (and list
10915                 (not (eval (caar list))))
10916       (setq list (cdr list)))
10917     (let ((face (cdar list)))
10918       (unless (eq face (get-text-property beg 'face))
10919         (gnus-put-text-property-excluding-characters-with-faces
10920          beg (gnus-point-at-eol) 'face
10921          (setq face (if (boundp face) (symbol-value face) face)))
10922         (when gnus-summary-highlight-line-function
10923           (funcall gnus-summary-highlight-line-function article face))))))
10924
10925 (defun gnus-update-read-articles (group unread &optional compute)
10926   "Update the list of read articles in GROUP.
10927 UNREAD is a sorted list."
10928   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10929          (entry (gnus-gethash group gnus-newsrc-hashtb))
10930          (info (nth 2 entry))
10931          (prev 1)
10932          read)
10933     (if (or (not info) (not active))
10934         ;; There is no info on this group if it was, in fact,
10935         ;; killed.  Gnus stores no information on killed groups, so
10936         ;; there's nothing to be done.
10937         ;; One could store the information somewhere temporarily,
10938         ;; perhaps...  Hmmm...
10939         ()
10940       ;; Remove any negative articles numbers.
10941       (while (and unread (< (car unread) 0))
10942         (setq unread (cdr unread)))
10943       ;; Remove any expired article numbers
10944       (while (and unread (< (car unread) (car active)))
10945         (setq unread (cdr unread)))
10946       ;; Compute the ranges of read articles by looking at the list of
10947       ;; unread articles.
10948       (while unread
10949         (when (/= (car unread) prev)
10950           (push (if (= prev (1- (car unread))) prev
10951                   (cons prev (1- (car unread))))
10952                 read))
10953         (setq prev (1+ (car unread)))
10954         (setq unread (cdr unread)))
10955       (when (<= prev (cdr active))
10956         (push (cons prev (cdr active)) read))
10957       (setq read (if (> (length read) 1) (nreverse read) read))
10958       (if compute
10959           read
10960         (save-excursion
10961           (let (setmarkundo)
10962             ;; Propagate the read marks to the backend.
10963             (when (gnus-check-backend-function 'request-set-mark group)
10964               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10965                     (add (gnus-remove-from-range read (gnus-info-read info))))
10966                 (when (or add del)
10967                   (unless (gnus-check-group group)
10968                     (error "Can't open server for %s" group))
10969                   (gnus-request-set-mark
10970                    group (delq nil (list (if add (list add 'add '(read)))
10971                                          (if del (list del 'del '(read))))))
10972                   (setq setmarkundo
10973                         `(gnus-request-set-mark
10974                           ,group
10975                           ',(delq nil (list
10976                                        (if del (list del 'add '(read)))
10977                                        (if add (list add 'del '(read))))))))))
10978             (set-buffer gnus-group-buffer)
10979             (gnus-undo-register
10980               `(progn
10981                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10982                  (gnus-info-set-read ',info ',(gnus-info-read info))
10983                  (gnus-get-unread-articles-in-group ',info
10984                                                     (gnus-active ,group))
10985                  (gnus-group-update-group ,group t)
10986                  ,setmarkundo))))
10987         ;; Enter this list into the group info.
10988         (gnus-info-set-read info read)
10989         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10990         (gnus-get-unread-articles-in-group info (gnus-active group))
10991         t))))
10992
10993 (defun gnus-offer-save-summaries ()
10994   "Offer to save all active summary buffers."
10995   (let (buffers)
10996     ;; Go through all buffers and find all summaries.
10997     (dolist (buffer (buffer-list))
10998       (when (and (setq buffer (buffer-name buffer))
10999                  (string-match "Summary" buffer)
11000                  (save-excursion
11001                    (set-buffer buffer)
11002                    ;; We check that this is, indeed, a summary buffer.
11003                    (and (eq major-mode 'gnus-summary-mode)
11004                         ;; Also make sure this isn't bogus.
11005                         gnus-newsgroup-prepared
11006                         ;; Also make sure that this isn't a
11007                         ;; dead summary buffer.
11008                         (not gnus-dead-summary-mode))))
11009         (push buffer buffers)))
11010     ;; Go through all these summary buffers and offer to save them.
11011     (when buffers
11012       (save-excursion
11013         (map-y-or-n-p
11014          "Update summary buffer %s? "
11015          (lambda (buf)
11016            (switch-to-buffer buf)
11017            (gnus-summary-exit))
11018          buffers)))))
11019
11020
11021 ;;; @ for mime-partial
11022 ;;;
11023
11024 (defun gnus-request-partial-message ()
11025   (save-excursion
11026     (let ((number (gnus-summary-article-number))
11027           (group gnus-newsgroup-name)
11028           (mother gnus-article-buffer))
11029       (set-buffer (get-buffer-create " *Partial Article*"))
11030       (erase-buffer)
11031       (setq mime-preview-buffer mother)
11032       (gnus-request-article-this-buffer number group)
11033       (mime-parse-buffer)
11034       )))
11035
11036 (autoload 'mime-combine-message/partial-pieces-automatically
11037   "mime-partial"
11038   "Internal method to combine message/partial messages automatically.")
11039
11040 (mime-add-condition
11041  'action '((type . message)(subtype . partial)
11042            (major-mode . gnus-original-article-mode)
11043            (method . mime-combine-message/partial-pieces-automatically)
11044            (summary-buffer-exp . gnus-summary-buffer)
11045            (request-partial-message-method . gnus-request-partial-message)
11046            ))
11047
11048
11049 ;;; @ for message/rfc822
11050 ;;;
11051
11052 (defun gnus-mime-extract-message/rfc822 (entity situation)
11053   "Burst a forwarded article."
11054   (save-excursion
11055     (set-buffer gnus-summary-buffer)
11056     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11057                                    gnus-newsgroup-name 'gnus-group-history))
11058            (gnus-group-marked (list group))
11059            article info)
11060       (with-temp-buffer
11061         (mime-insert-entity-content entity)
11062         (setq article (gnus-request-accept-article group)))
11063       (when (and (consp article)
11064                  (numberp (setq article (cdr article))))
11065         (setq info (gnus-get-info group))
11066         (gnus-info-set-read info
11067                             (gnus-remove-from-range (gnus-info-read info)
11068                                                     (list article)))
11069         (when (string-equal group gnus-newsgroup-name)
11070           (forward-line 1)
11071           (let (gnus-show-threads)
11072             (gnus-summary-goto-subject article t))
11073           (gnus-summary-clear-mark-forward 1))
11074         (set-buffer gnus-group-buffer)
11075         (gnus-group-get-new-news-this-group nil t)))))
11076
11077 (mime-add-condition
11078  'action '((type . message)(subtype . rfc822)
11079            (major-mode . gnus-original-article-mode)
11080            (method . gnus-mime-extract-message/rfc822)
11081            (mode . "extract")
11082            ))
11083
11084 (mime-add-condition
11085  'action '((type . message)(subtype . news)
11086            (major-mode . gnus-original-article-mode)
11087            (method . gnus-mime-extract-message/rfc822)
11088            (mode . "extract")
11089            ))
11090
11091 (defun gnus-mime-extract-multipart (entity situation)
11092   (let ((children (mime-entity-children entity))
11093         mime-acting-situation-to-override
11094         f)
11095     (while children
11096       (mime-play-entity (car children)
11097                         (cons (assq 'mode situation)
11098                               mime-acting-situation-to-override))
11099       (setq children (cdr children)))
11100     (if (setq f (cdr (assq 'after-method
11101                            mime-acting-situation-to-override)))
11102         (eval f)
11103       )))
11104
11105 (mime-add-condition
11106  'action '((type . multipart)
11107            (method . gnus-mime-extract-multipart)
11108            (mode . "extract")
11109            )
11110  'with-default)
11111
11112
11113 ;;; @ end
11114 ;;;
11115
11116 (defun gnus-summary-inherit-default-charset ()
11117   "Import `default-mime-charset' from summary buffer."
11118   (if (buffer-live-p gnus-summary-buffer)
11119       (if (local-variable-p 'default-mime-charset gnus-summary-buffer)
11120           (progn
11121             (make-local-variable 'default-mime-charset)
11122             (setq default-mime-charset
11123                   (with-current-buffer gnus-summary-buffer
11124                     default-mime-charset)))
11125         (kill-local-variable 'default-mime-charset))))
11126
11127 (defun gnus-summary-setup-default-charset ()
11128   "Setup newsgroup default charset."
11129   (if (equal gnus-newsgroup-name "nndraft:drafts")
11130       (progn
11131         (setq gnus-newsgroup-charset nil)
11132         (make-local-variable 'default-mime-charset)
11133         (setq default-mime-charset nil))
11134     (let ((ignored-charsets
11135            (or gnus-newsgroup-ephemeral-ignored-charsets
11136                (append
11137                 (and gnus-newsgroup-name
11138                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11139                 gnus-newsgroup-ignored-charsets)))
11140           charset)
11141       (setq gnus-newsgroup-charset
11142             (or gnus-newsgroup-ephemeral-charset
11143                 (when (and gnus-newsgroup-name
11144                            (setq charset (gnus-parameter-charset
11145                                           gnus-newsgroup-name)))
11146                   (make-local-variable 'default-mime-charset)
11147                   (setq default-mime-charset charset))
11148                 gnus-default-charset))
11149       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11150            ignored-charsets))))
11151
11152 ;;;
11153 ;;; Mime Commands
11154 ;;;
11155
11156 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11157   "Display the current article buffer fully MIME-buttonized.
11158 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11159 treated as multipart/mixed."
11160   (interactive "P")
11161   (require 'gnus-art)
11162   (let ((gnus-unbuttonized-mime-types nil)
11163         (gnus-mime-display-multipart-as-mixed show-all-parts))
11164     (gnus-summary-show-article)))
11165
11166 (defun gnus-summary-repair-multipart (article)
11167   "Add a Content-Type header to a multipart article without one."
11168   (interactive (list (gnus-summary-article-number)))
11169   (gnus-with-article article
11170     (message-narrow-to-head)
11171     (message-remove-header "Mime-Version")
11172     (goto-char (point-max))
11173     (insert "Mime-Version: 1.0\n")
11174     (widen)
11175     (when (search-forward "\n--" nil t)
11176       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11177         (message-narrow-to-head)
11178         (message-remove-header "Content-Type")
11179         (goto-char (point-max))
11180         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11181                         separator))
11182         (widen))))
11183   (let (gnus-mark-article-hook)
11184     (gnus-summary-select-article t t nil article)))
11185
11186 (defun gnus-summary-toggle-display-buttonized ()
11187   "Toggle the buttonizing of the article buffer."
11188   (interactive)
11189   (require 'gnus-art)
11190   (if (setq gnus-inhibit-mime-unbuttonizing
11191             (not gnus-inhibit-mime-unbuttonizing))
11192       (let ((gnus-unbuttonized-mime-types nil))
11193         (gnus-summary-show-article))
11194     (gnus-summary-show-article)))
11195
11196 ;;;
11197 ;;; Intelli-mouse commmands
11198 ;;;
11199
11200 (defun gnus-wheel-summary-scroll (event)
11201   (interactive "e")
11202   (let ((amount (if (memq 'shift (event-modifiers event))
11203                     (car gnus-wheel-scroll-amount)
11204                   (cdr gnus-wheel-scroll-amount)))
11205         (direction (- (* (static-if (featurep 'xemacs)
11206                              (event-button event)
11207                            (cond ((eq 'mouse-4 (event-basic-type event))
11208                                   4)
11209                                  ((eq 'mouse-5 (event-basic-type event))
11210                                   5)))
11211                          2) 9))
11212         edge)
11213     (gnus-summary-scroll-up (* amount direction))
11214     (when (gnus-eval-in-buffer-window gnus-article-buffer
11215             (save-restriction
11216               (widen)
11217               (and (if (< 0 direction)
11218                        (gnus-article-next-page 0)
11219                      (gnus-article-prev-page 0)
11220                      (bobp))
11221                    (if (setq edge (get-text-property
11222                                    (point-min) 'gnus-wheel-edge))
11223                        (setq edge (* edge direction))
11224                      (setq edge -1))
11225                    (or (plusp edge)
11226                        (let ((buffer-read-only nil)
11227                              (inhibit-read-only t))
11228                          (put-text-property (point-min) (point-max)
11229                                             'gnus-wheel-edge direction)
11230                          nil))
11231                    (or (> edge gnus-wheel-edge-resistance)
11232                        (let ((buffer-read-only nil)
11233                              (inhibit-read-only t))
11234                          (put-text-property (point-min) (point-max)
11235                                             'gnus-wheel-edge
11236                                             (* (1+ edge) direction))
11237                          nil))
11238                    (eq last-command 'gnus-wheel-summary-scroll))))
11239       (gnus-summary-next-article nil nil (minusp direction)))))
11240
11241 (defun gnus-wheel-install ()
11242   "Enable mouse wheel support on summary window."
11243   (when gnus-use-wheel
11244     (let ((keys
11245            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11246       (dolist (key keys)
11247         (define-key gnus-summary-mode-map key
11248           'gnus-wheel-summary-scroll)))))
11249
11250 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11251
11252 ;;;
11253 ;;; Traditional PGP commmands
11254 ;;;
11255
11256 (defun gnus-summary-decrypt-article (&optional force)
11257   "Decrypt the current article in traditional PGP way.
11258 This will have permanent effect only in mail groups.
11259 If FORCE is non-nil, allow editing of articles even in read-only
11260 groups."
11261   (interactive "P")
11262   (gnus-summary-select-article t)
11263   (gnus-eval-in-buffer-window gnus-article-buffer
11264     (save-excursion
11265       (save-restriction
11266         (widen)
11267         (goto-char (point-min))
11268         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11269           (error "Not a traditional PGP message!"))
11270         (let ((armor-start (match-beginning 0)))
11271           (if (and (pgg-decrypt-region armor-start (point-max))
11272                    (or force (not (gnus-group-read-only-p))))
11273               (let ((inhibit-read-only t)
11274                     buffer-read-only)
11275                 (delete-region armor-start
11276                                (progn
11277                                  (re-search-forward "^-+END PGP" nil t)
11278                                  (beginning-of-line 2)
11279                                  (point)))
11280                 (insert-buffer-substring pgg-output-buffer))))))))
11281
11282 (defun gnus-summary-verify-article ()
11283   "Verify the current article in traditional PGP way."
11284   (interactive)
11285   (save-excursion
11286     (set-buffer gnus-original-article-buffer)
11287     (goto-char (point-min))
11288     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11289       (error "Not a traditional PGP message!"))
11290     (re-search-forward "^-+END PGP" nil t)
11291     (beginning-of-line 2)
11292     (call-interactively (function pgg-verify-region))))
11293
11294 ;;;
11295 ;;; Generic summary marking commands
11296 ;;;
11297
11298 (defvar gnus-summary-marking-alist
11299   '((read gnus-del-mark "d")
11300     (unread gnus-unread-mark "u")
11301     (ticked gnus-ticked-mark "!")
11302     (dormant gnus-dormant-mark "?")
11303     (expirable gnus-expirable-mark "e"))
11304   "An alist of names/marks/keystrokes.")
11305
11306 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11307 (defvar gnus-summary-mark-map)
11308
11309 (defun gnus-summary-make-all-marking-commands ()
11310   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11311   (dolist (elem gnus-summary-marking-alist)
11312     (apply 'gnus-summary-make-marking-command elem)))
11313
11314 (defun gnus-summary-make-marking-command (name mark keystroke)
11315   (let ((map (make-sparse-keymap)))
11316     (define-key gnus-summary-generic-mark-map keystroke map)
11317     (dolist (lway `((next "next" next nil "n")
11318                     (next-unread "next unread" next t "N")
11319                     (prev "previous" prev nil "p")
11320                     (prev-unread "previous unread" prev t "P")
11321                     (nomove "" nil nil ,keystroke)))
11322       (let ((func (gnus-summary-make-marking-command-1
11323                    mark (car lway) lway name)))
11324         (setq func (eval func))
11325         (define-key map (nth 4 lway) func)))))
11326
11327 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11328   `(defun ,(intern
11329             (format "gnus-summary-put-mark-as-%s%s"
11330                     name (if (eq way 'nomove)
11331                              ""
11332                            (concat "-" (symbol-name way)))))
11333      (n)
11334      ,(format
11335        "Mark the current article as %s%s.
11336 If N, the prefix, then repeat N times.
11337 If N is negative, move in reverse order.
11338 The difference between N and the actual number of articles marked is
11339 returned."
11340        name (car (cdr lway)))
11341      (interactive "p")
11342      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11343
11344 (defun gnus-summary-generic-mark (n mark move unread)
11345   "Mark N articles with MARK."
11346   (unless (eq major-mode 'gnus-summary-mode)
11347     (error "This command can only be used in the summary buffer"))
11348   (gnus-summary-show-thread)
11349   (let ((nummove
11350          (cond
11351           ((eq move 'next) 1)
11352           ((eq move 'prev) -1)
11353           (t 0))))
11354     (if (zerop nummove)
11355         (setq n 1)
11356       (when (< n 0)
11357         (setq n (abs n)
11358               nummove (* -1 nummove))))
11359     (while (and (> n 0)
11360                 (gnus-summary-mark-article nil mark)
11361                 (zerop (gnus-summary-next-subject nummove unread t)))
11362       (setq n (1- n)))
11363     (when (/= 0 n)
11364       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11365     (gnus-summary-recenter)
11366     (gnus-summary-position-point)
11367     (gnus-set-mode-line 'summary)
11368     n))
11369
11370 (defun gnus-summary-insert-articles (articles)
11371   (when (setq articles
11372               (gnus-sorted-difference articles
11373                                       (mapcar (lambda (h)
11374                                                 (mail-header-number h))
11375                                               gnus-newsgroup-headers)))
11376     (setq gnus-newsgroup-headers
11377           (merge 'list
11378                  gnus-newsgroup-headers
11379                  (gnus-fetch-headers articles)
11380                  'gnus-article-sort-by-number))
11381     ;; Suppress duplicates?
11382     (when gnus-suppress-duplicates
11383       (gnus-dup-suppress-articles))
11384
11385     ;; We might want to build some more threads first.
11386     (when (and gnus-fetch-old-headers
11387                (eq gnus-headers-retrieved-by 'nov))
11388       (if (eq gnus-fetch-old-headers 'invisible)
11389           (gnus-build-all-threads)
11390         (gnus-build-old-threads)))
11391     ;; Let the Gnus agent mark articles as read.
11392     (when gnus-agent
11393       (gnus-agent-get-undownloaded-list))
11394     ;; Remove list identifiers from subject
11395     (when gnus-list-identifiers
11396       (gnus-summary-remove-list-identifiers))
11397     ;; First and last article in this newsgroup.
11398     (when gnus-newsgroup-headers
11399       (setq gnus-newsgroup-begin
11400             (mail-header-number (car gnus-newsgroup-headers))
11401             gnus-newsgroup-end
11402             (mail-header-number
11403              (gnus-last-element gnus-newsgroup-headers))))
11404     (when gnus-use-scoring
11405       (gnus-possibly-score-headers))))
11406
11407 (defun gnus-summary-insert-old-articles (&optional all)
11408   "Insert all old articles in this group.
11409 If ALL is non-nil, already read articles become readable.
11410 If ALL is a number, fetch this number of articles."
11411   (interactive "P")
11412   (prog1
11413       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11414             older len)
11415         (setq older
11416               (gnus-sorted-difference
11417                (gnus-uncompress-range (list gnus-newsgroup-active))
11418                old))
11419         (setq len (length older))
11420         (cond
11421          ((null older) nil)
11422          ((numberp all)
11423           (if (< all len)
11424               (setq older (last older all))))
11425          (all nil)
11426          (t
11427           (if (and (numberp gnus-large-newsgroup)
11428                    (> len gnus-large-newsgroup))
11429               (let* ((cursor-in-echo-area nil)
11430                      (initial (gnus-parameter-large-newsgroup-initial
11431                                gnus-newsgroup-name))
11432                      (input
11433                       (read-string
11434                        (format
11435                         "How many articles from %s (%s %d): "
11436                         (gnus-limit-string
11437                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11438                         (if initial "max" "default")
11439                         len)
11440                        (if initial
11441                            (cons (number-to-string initial)
11442                                  0)))))
11443                 (unless (string-match "^[ \t]*$" input)
11444                   (setq all (string-to-number input))
11445                   (if (< all len)
11446                       (setq older (last older all))))))))
11447         (if (not older)
11448             (message "No old news.")
11449           (gnus-summary-insert-articles older)
11450           (gnus-summary-limit (gnus-sorted-nunion old older))))
11451     (gnus-summary-position-point)))
11452
11453 (defun gnus-summary-insert-new-articles ()
11454   "Insert all new articles in this group."
11455   (interactive)
11456   (prog1
11457       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11458             (old-active gnus-newsgroup-active)
11459             (nnmail-fetched-sources (list t))
11460             i new)
11461         (setq gnus-newsgroup-active
11462               (gnus-activate-group gnus-newsgroup-name 'scan))
11463         (setq i (cdr gnus-newsgroup-active))
11464         (while (> i (cdr old-active))
11465           (push i new)
11466           (decf i))
11467         (if (not new)
11468             (message "No gnus is bad news.")
11469           (gnus-summary-insert-articles new)
11470           (setq gnus-newsgroup-unreads
11471                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11472           (gnus-summary-limit (gnus-sorted-nunion old new))))
11473     (gnus-summary-position-point)))
11474
11475 (gnus-summary-make-all-marking-commands)
11476
11477 (gnus-ems-redefine)
11478
11479 (provide 'gnus-sum)
11480
11481 (run-hooks 'gnus-sum-load-hook)
11482
11483 ;;; gnus-sum.el ends here